MCPcopy
hub / github.com/Narcooo/inkos / appendTranscriptEvents

Function appendTranscriptEvents

packages/core/src/interaction/session-transcript.ts:60–90  ·  view source on GitHub ↗
(
  projectRoot: string,
  sessionId: string,
  buildEvents: (context: {
    readonly events: ReadonlyArray<TranscriptEvent>;
    readonly nextSeq: number;
  }) => ReadonlyArray<TranscriptEvent> | Promise<ReadonlyArray<TranscriptEvent>>,
)

Source from the content-addressed store, hash-verified

58}
59
60export async function appendTranscriptEvents(
61 projectRoot: string,
62 sessionId: string,
63 buildEvents: (context: {
64 readonly events: ReadonlyArray<TranscriptEvent>;
65 readonly nextSeq: number;
66 }) => ReadonlyArray<TranscriptEvent> | Promise<ReadonlyArray<TranscriptEvent>>,
67): Promise<TranscriptEvent[]> {
68 const key = `${projectRoot}:${sessionId}`;
69 const previous = appendQueues.get(key) ?? Promise.resolve();
70 let result: TranscriptEvent[] = [];
71
72 const next = previous.then(async () => {
73 const events = await readTranscriptEvents(projectRoot, sessionId);
74 const nextSeq = events.reduce((max, event) => Math.max(max, event.seq), 0) + 1;
75 const built = await buildEvents({ events, nextSeq });
76 result = built.map((event) => TranscriptEventSchema.parse(event));
77 if (result.length === 0) return;
78
79 await mkdir(sessionsDir(projectRoot), { recursive: true });
80 await appendFile(
81 transcriptPath(projectRoot, sessionId),
82 `${result.map((event) => JSON.stringify(event)).join("\n")}\n`,
83 "utf-8",
84 );
85 });
86
87 appendQueues.set(key, next.catch(() => undefined));
88 await next;
89 return result;
90}
91
92function transcriptRoleForMessage(message: AgentMessage): TranscriptRole | null {
93 if (!message || typeof message !== "object" || !("role" in message)) return null;

Calls 4

readTranscriptEventsFunction · 0.85
sessionsDirFunction · 0.85
transcriptPathFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected