Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadContextAttachment.ts

26 lines1.1 KB

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