@theoremjs/agents 0.3.4-next.634.g16decd2 → 0.3.4-next.636.g3b66d8b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// session and history read calls through this fold only.
|
|
3
3
|
import { TheoremError } from '../guardrails/error.js';
|
|
4
4
|
import { labelText } from '../guardrails/labels.js';
|
|
5
|
+
import { turnEventSchema } from '../kernel/turn-events.js';
|
|
5
6
|
import { partialJsonObject } from '../kernel/util/partial-json.js';
|
|
6
7
|
/**
|
|
7
8
|
* The raw call comes first for every call (`turnEventSchema`); a phase for a call never made is a
|
|
@@ -58,8 +59,9 @@ function applyToolEvent(call, tool) {
|
|
|
58
59
|
return { ...call, outputDraft: (call.outputDraft ?? '') + tool.text };
|
|
59
60
|
case 'artifact':
|
|
60
61
|
return { ...call, artifacts: [...call.artifacts, tool.artifact] };
|
|
61
|
-
case 'dispatch':
|
|
62
62
|
case 'progress':
|
|
63
|
+
return withAgentStep(call, tool.data);
|
|
64
|
+
case 'dispatch':
|
|
63
65
|
case 'trace':
|
|
64
66
|
case 'warning':
|
|
65
67
|
return call;
|
|
@@ -84,24 +86,62 @@ function applyToolDraft(call, draft) {
|
|
|
84
86
|
draft: (call?.draft ?? '') + draft.text,
|
|
85
87
|
};
|
|
86
88
|
}
|
|
89
|
+
/** Adds one call event to `calls`, keyed as `toolCallsOf` keys them. */
|
|
90
|
+
function foldToolCall(calls, event) {
|
|
91
|
+
if (event.type === 'tool_draft') {
|
|
92
|
+
calls.set(event.callId, applyToolDraft(calls.get(event.callId), event));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const { tool } = event;
|
|
96
|
+
const key = tool.operationId ?? tool.callId;
|
|
97
|
+
// why: The draft is keyed by the model's callId; the call that replaces it may carry an operation ID.
|
|
98
|
+
if (tool.phase === undefined && calls.get(tool.callId)?.draft !== undefined) {
|
|
99
|
+
calls.delete(tool.callId);
|
|
100
|
+
}
|
|
101
|
+
calls.set(key, applyToolEvent(calls.get(key), tool));
|
|
102
|
+
}
|
|
87
103
|
/** Every call in `events`, in the order the model made them. */
|
|
88
104
|
function toolCallsOf(events) {
|
|
89
105
|
const calls = new Map();
|
|
90
106
|
for (const event of events) {
|
|
91
|
-
if (event.type === 'tool_draft')
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
107
|
+
if (event.type === 'tool_draft' || event.type === 'tool')
|
|
108
|
+
foldToolCall(calls, event);
|
|
109
|
+
}
|
|
110
|
+
return [...calls.values()];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* A call event of the agent an agent tool ran, which the kernel carries as the call's progress
|
|
114
|
+
* (`{ agent, event }`); any other progress is not a step.
|
|
115
|
+
*/
|
|
116
|
+
function agentStep(data) {
|
|
117
|
+
if (typeof data !== 'object' || data === null || !('agent' in data) || !('event' in data)) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const { event } = data;
|
|
121
|
+
if (event?.type !== 'tool' && event?.type !== 'tool_draft')
|
|
122
|
+
return undefined;
|
|
123
|
+
const parsed = turnEventSchema.safeParse(event);
|
|
124
|
+
return parsed.success ? parsed.data : undefined;
|
|
125
|
+
}
|
|
126
|
+
/** `call` with the agent step its progress carries, if it carries one that fits. */
|
|
127
|
+
function withAgentStep(call, data) {
|
|
128
|
+
const step = agentStep(data);
|
|
129
|
+
const steps = step && withStep(call.steps ?? [], step);
|
|
130
|
+
return steps ? { ...call, steps } : call;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* `steps` with one more agent step folded in, or `undefined` when it does not fit.
|
|
134
|
+
* why: A step is progress, not the stream itself: one that does not fit its call is dropped, so it
|
|
135
|
+
* never breaks the transcript of the call that carries it.
|
|
136
|
+
*/
|
|
137
|
+
function withStep(steps, event) {
|
|
138
|
+
const calls = new Map(steps.map((step) => [step.operationId ?? step.callId, step]));
|
|
139
|
+
if (event.type === 'tool' && event.tool.phase !== undefined) {
|
|
140
|
+
const known = calls.get(event.tool.operationId ?? event.tool.callId);
|
|
141
|
+
if (known?.callId !== event.tool.callId || known.name !== event.tool.name)
|
|
142
|
+
return undefined;
|
|
104
143
|
}
|
|
144
|
+
foldToolCall(calls, event);
|
|
105
145
|
return [...calls.values()];
|
|
106
146
|
}
|
|
107
147
|
/** The arguments shown for approval, or the user's edit, or the model's original call. */
|
|
@@ -182,6 +182,8 @@ export interface ToolCall {
|
|
|
182
182
|
/** When it last settled: complete, failed, cancelled, or gated (epoch ms). */
|
|
183
183
|
endedAt?: number;
|
|
184
184
|
artifacts: unknown[];
|
|
185
|
+
/** An agent tool's: the calls its agent made, in order, from the call's `progress` phases. */
|
|
186
|
+
steps?: ToolCall[];
|
|
185
187
|
}
|
|
186
188
|
export interface ToolBlock extends TranscriptBlockBase {
|
|
187
189
|
kind: 'tool';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theoremjs/agents",
|
|
3
|
-
"version": "0.3.4-next.
|
|
3
|
+
"version": "0.3.4-next.636.g3b66d8b",
|
|
4
4
|
"description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|