mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:00:43 +00:00
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { normalizeOptionalString } from "../shared/string-coerce.js";
|
|
|
|
export type SessionTranscriptUpdate = {
|
|
sessionFile: string;
|
|
sessionKey?: string;
|
|
message?: unknown;
|
|
messageId?: string;
|
|
};
|
|
|
|
type SessionTranscriptListener = (update: SessionTranscriptUpdate) => void;
|
|
|
|
const SESSION_TRANSCRIPT_LISTENERS = new Set<SessionTranscriptListener>();
|
|
|
|
export function onSessionTranscriptUpdate(listener: SessionTranscriptListener): () => void {
|
|
SESSION_TRANSCRIPT_LISTENERS.add(listener);
|
|
return () => {
|
|
SESSION_TRANSCRIPT_LISTENERS.delete(listener);
|
|
};
|
|
}
|
|
|
|
export function emitSessionTranscriptUpdate(update: string | SessionTranscriptUpdate): void {
|
|
const normalized =
|
|
typeof update === "string"
|
|
? { sessionFile: update }
|
|
: {
|
|
sessionFile: update.sessionFile,
|
|
sessionKey: update.sessionKey,
|
|
message: update.message,
|
|
messageId: update.messageId,
|
|
};
|
|
const trimmed = normalizeOptionalString(normalized.sessionFile);
|
|
if (!trimmed) {
|
|
return;
|
|
}
|
|
const nextUpdate: SessionTranscriptUpdate = {
|
|
sessionFile: trimmed,
|
|
...(normalizeOptionalString(normalized.sessionKey)
|
|
? { sessionKey: normalizeOptionalString(normalized.sessionKey) }
|
|
: {}),
|
|
...(normalized.message !== undefined ? { message: normalized.message } : {}),
|
|
...(normalizeOptionalString(normalized.messageId)
|
|
? { messageId: normalizeOptionalString(normalized.messageId) }
|
|
: {}),
|
|
};
|
|
for (const listener of SESSION_TRANSCRIPT_LISTENERS) {
|
|
try {
|
|
listener(nextUpdate);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|