Skip to content
Source
Sign inSign up

threadContextAttachment.ts

24 lines957 Bytes

1import type { EnvironmentId, OrchestrationV2ThreadTranscript } from "@t3tools/contracts";
2import { toKindScopedComposerContextId } from "@t3tools/shared/composerContextReferences";
3import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript";
4import * as DateTime from "effect/DateTime";
5​
6export function threadContextAttachment(
7 environmentId: EnvironmentId,
8 transcript: OrchestrationV2ThreadTranscript,
9): File {
10 const updatedAt = DateTime.formatIso(transcript.updatedAt);
11 const name = toKindScopedComposerContextId(
12 "thread",
13 `${environmentId}:${transcript.threadId}:${updatedAt}`,
14 );
15 const title = transcript.title.replace(/[^\p{L}\p{N}._ -]/gu, "_").slice(0, 64) || "Thread";
16 return new File(
17 [
18 threadTranscriptHeader(environmentId, transcript),
19 ...transcript.items.map((row) => `${JSON.stringify(row)}\n`),
20 ],
21 `${title}-${name.slice(-16)}.jsonl`,
22 { type: "application/x-ndjson" },
23 );