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

workflowAgentTranscript.ts

99 lines3.7 KB

1// @effect-diagnostics nodeBuiltinImport:off
2/**
3 * A workflow member never becomes a provider task and the CLI forwards none of
4 * its messages, so the transcript the harness writes is the only record of its
5 * conversation. The path is derived here from the run's transcript directory
6 * and the member's provider id rather than accepted from the caller, and the
7 * read itself is contained by readContainedWorkflowFile.
8 */
9import * as NodePath from "node:path";
10​
11import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
12import * as Context from "effect/Context";
13import * as Effect from "effect/Effect";
14import * as Option from "effect/Option";
15import * as Schema from "effect/Schema";
16​
17import { field, text } from "./unknownField.ts";
18import { readContainedWorkflowFile, type WorkflowFileIdentity } from "./workflowFileRead.ts";
19​
20const TRANSCRIPT_READ_BYTE_CAP = 512 * 1024;
21​
22const AGENT_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
23​
24const decodeJsonLine = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
25​
26export interface WorkflowAgentTranscriptCursor {
27 readonly offset: number;
28 readonly identity: WorkflowFileIdentity | null;
30​
31export const initialWorkflowAgentTranscriptCursor: WorkflowAgentTranscriptCursor = {
32 offset: 0,
33 identity: null,
34};
35​
36/** Sees every transcript read: where it started and ended, and how many lines it kept. */
37export const WorkflowAgentTranscriptReadObserver = Context.Reference<
38 (read: {
39 readonly agentId: string;
40 readonly startOffset: number;
41 readonly endOffset: number;
42 readonly lineCount: number;
43 }) => Effect.Effect<void>
44>("t3/orchestration/WorkflowAgentTranscriptReadObserver", {
45 defaultValue: () => () => Effect.void,
46});
47​
48/**
49 * Conversation lines only: `attachment` lines and `isMeta` user lines are
50 * harness bookkeeping (hook output, skill listings, injected context), and a
51 * line without a uuid cannot be projected idempotently.
52 */
53function parseWorkflowAgentTranscriptLines(contents: string): ReadonlyArray<unknown> {
54 return contents.split("\n").flatMap((line) => {
55 if (line.trim().length === 0) return [];
56 const entry = Option.getOrUndefined(decodeJsonLine(line));
57 const type = text(field(entry, "type"));
58 if (type !== "assistant" && type !== "user") return [];
59 if (field(entry, "isMeta") === true || text(field(entry, "uuid")) === undefined) return [];
60 return [entry];
61 });
63​
64/** Reads the complete lines written since `cursor`, and the cursor after them. */
65export const readWorkflowAgentTranscript = Effect.fn("orchestration.readWorkflowAgentTranscript")(
66 function* (input: {
67 readonly transcriptDir: string;
68 readonly agentId: string;
69 readonly cursor: WorkflowAgentTranscriptCursor;
70 readonly configDir?: string;
71 }) {
72 if (!AGENT_ID_PATTERN.test(input.agentId)) {
73 return yield* new OrchestrationWorkflowFileError({
74 reason: "invalid-path",
75 path: input.agentId,
76 });
77 }
78 const file = yield* readContainedWorkflowFile({
79 path: NodePath.join(input.transcriptDir, `agent-${input.agentId}.jsonl`),
80 extension: ".jsonl",
81 byteCap: TRANSCRIPT_READ_BYTE_CAP,
82 fromOffset: input.cursor,
83 ...(input.configDir === undefined ? {} : { configDir: input.configDir }),
84 });
85 const lines = parseWorkflowAgentTranscriptLines(file.contents);
86 yield* (yield* WorkflowAgentTranscriptReadObserver)({
87 agentId: input.agentId,
88 startOffset: file.startOffset,
89 endOffset: file.endOffset,
90 lineCount: lines.length,
91 });
92 return {
93 lines,
94 cursor: { offset: file.endOffset, identity: file.identity },
95 // More complete lines may follow past the per-read cap.
96 truncated: file.truncated,
97 };
98 },
99);