Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2/**
3 * A member agent never becomes a provider task and emits nothing on the event
4 * stream, so the transcript the harness writes is the only full record of its
5 * turn — the progress snapshot carries a capped excerpt. The path is derived
6 * here from the run's transcript directory and the member's provider id rather
7 * than accepted from the caller, and the read itself is contained by
8 * readContainedWorkflowFile.
9 */
10import * as NodePath from "node:path";
11
12import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
13import * as Effect from "effect/Effect";
14
15import { field, text } from "./unknownField.ts";
16import { readContainedWorkflowFile } from "./workflowFileRead.ts";
17
18const TRANSCRIPT_BYTE_CAP = 512 * 1024;
19
20const AGENT_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
21
22/**
23 * Two shapes have to be handled: `attachment` lines and `isMeta` user lines are
24 * harness bookkeeping (hook output, skill listings, injected context) and are
25 * never conversation, and one assistant API message is split across several
26 * lines sharing a `message.id`, so those are joined into a single turn rather
27 * than read as separate ones.
28 */
29export function parseWorkflowAgentAnswers(contents: string): ReadonlyArray<string> {
30 const turns: string[] = [];
31 const turnIndexById = new Map<string, number>();
32
33 for (const line of contents.split("\n")) {
34 if (line.trim().length === 0) continue;
35 let entry: unknown;
36 try {
37 entry = JSON.parse(line);
38 } catch {
39 // A capped read can cut the final line mid-object; drop it and keep the rest.
40 continue;
41 }
42 if (text(field(entry, "type")) !== "assistant") continue;
43 if (field(entry, "isMeta") === true) continue;
44
45 const message = field(entry, "message");
46 const content = field(message, "content");
47 if (!Array.isArray(content)) continue;
48 const body = content
49 .filter((block) => text(field(block, "type")) === "text")
50 .map((block) => field(block, "text"))
51 .filter((value): value is string => typeof value === "string" && value.trim().length > 0)
52 .join("\n\n");
53 if (body.length === 0) continue;
54
55 const messageId = text(field(message, "id"));
56 const existing = messageId === undefined ? undefined : turnIndexById.get(messageId);
57 if (existing !== undefined) {
58 turns[existing] = `${turns[existing]}\n\n${body}`;
59 continue;
60 }
61 if (messageId !== undefined) turnIndexById.set(messageId, turns.length);
62 turns.push(body);
63 }
64
65 return turns;
66}
67
68export const readWorkflowAgentAnswers = Effect.fn("orchestration.readWorkflowAgentAnswers")(
69 function* (input: {
70 readonly transcriptDir: string;
71 readonly agentId: string;
72 readonly configDir?: string;
73 }) {
74 if (!AGENT_ID_PATTERN.test(input.agentId)) {
75 return yield* new OrchestrationWorkflowFileError({
76 reason: "invalid-path",
77 path: input.agentId,
78 });
79 }
80 const file = yield* readContainedWorkflowFile({
81 path: NodePath.join(input.transcriptDir, `agent-${input.agentId}.jsonl`),
82 extension: ".jsonl",
83 byteCap: TRANSCRIPT_BYTE_CAP,
84 tail: true,
85 ...(input.configDir === undefined ? {} : { configDir: input.configDir }),
86 });
87 return parseWorkflowAgentAnswers(
88 file.truncated ? file.contents.slice(file.contents.indexOf("\n") + 1) : file.contents,
89 );
90 },
91);