| 1 | import type { EnvironmentId, OrchestrationV2ThreadTranscript } from "@t3tools/contracts"; |
| 2 | import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript"; |
| 3 | import * as DateTime from "effect/DateTime"; |
| 4 | |
| 5 | import { toKindScopedComposerContextId } from "./composerContextReferences"; |
| 6 | |
| 7 | export 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 title = transcript.title.replace(/[^\p{L}\p{N}._ -]/gu, "_").slice(0, 64) || "Thread"; |
| 17 | return new File( |
| 18 | [ |
| 19 | threadTranscriptHeader(environmentId, transcript), |
| 20 | ...transcript.items.map((row) => `${JSON.stringify(row)}\n`), |
| 21 | ], |
| 22 | `${title}-${name.slice(-16)}.jsonl`, |
| 23 | { type: "application/x-ndjson" }, |
| 24 | ); |
| 25 | } |