Files
trade-message-center/apps/chrome-extension/src/onetalk/main-page/message-observer/index.ts
T
YBF 784bbec2a0 feat: 支持 OneTalk 文本引用回复协议 (#66)
* feat: add OneTalk quote reply support

* chore(task): archive 09-17-onetalk-quote-reply-protocol

* chore: record journal

* docs: format OneTalk DOM card collection plan

* chore: release 0.8.29

* chore: release 0.8.30
2026-09-17 11:56:35 +08:00

36 lines
1.4 KiB
TypeScript

// 分发 OneTalk WebSocket 帧到消息解析分支
import { isObjectRecord } from "../../../lib/guards.ts";
import { parseNewMessages } from "./new.ts";
import type { OneTalkPageWindow } from "../model.ts";
import { createParsedMessageBatch, type OneTalkParsedMessageBatch } from "./model.ts";
import type { OneTalkQuoteSourceIndex } from "./quote-source-index.ts";
/** 识别响应帧并委派到历史或新消息解析器。 */
export const parseOneTalkMessages = (
pageWindow: OneTalkPageWindow,
data: unknown,
quoteSources?: OneTalkQuoteSourceIndex,
): OneTalkParsedMessageBatch => {
if (typeof data !== "string") return createParsedMessageBatch([]);
let frame: unknown;
try {
frame = JSON.parse(data);
} catch {
return createParsedMessageBatch([]);
}
if (!isObjectRecord(frame) || frame.code !== 200) return createParsedMessageBatch([]);
// History responses are consumed once through the SDK history adapter. Observing the raw
// WebSocket response as well creates a second source of truth whose wrapper lacks SDK-only
// classification fields (for example the one-hour system-notice marker).
if (isObjectRecord(frame.body) && Array.isArray(frame.body.userMessageModels))
return createParsedMessageBatch([]);
return Array.isArray(frame.body)
? createParsedMessageBatch(parseNewMessages(frame.body, pageWindow, quoteSources))
: createParsedMessageBatch([]);
};