@onkernel/cua-agent 0.2.0 → 0.3.0
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.
- package/CHANGELOG.md +13 -0
- package/README.md +24 -9
- package/dist/index.d.ts +138 -8
- package/dist/index.js +752 -5
- package/package.json +11 -10
- package/dist/agent.d.ts +0 -102
- package/dist/agent.d.ts.map +0 -1
- package/dist/agent.js +0 -207
- package/dist/agent.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/tools.d.ts +0 -39
- package/dist/tools.d.ts.map +0 -1
- package/dist/tools.js +0 -116
- package/dist/tools.js.map +0 -1
- package/dist/translator/translator.d.ts +0 -22
- package/dist/translator/translator.d.ts.map +0 -1
- package/dist/translator/translator.js +0 -227
- package/dist/translator/translator.js.map +0 -1
- package/dist/translator/types.d.ts +0 -39
- package/dist/translator/types.d.ts.map +0 -1
- package/dist/translator/types.js +0 -2
- package/dist/translator/types.js.map +0 -1
- package/dist/vendor/pi-agent-core/agent-loop.d.ts +0 -24
- package/dist/vendor/pi-agent-core/agent-loop.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/agent-loop.js +0 -479
- package/dist/vendor/pi-agent-core/agent-loop.js.map +0 -1
- package/dist/vendor/pi-agent-core/agent.d.ts +0 -125
- package/dist/vendor/pi-agent-core/agent.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/agent.js +0 -402
- package/dist/vendor/pi-agent-core/agent.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/agent-harness.d.ts +0 -85
- package/dist/vendor/pi-agent-core/harness/agent-harness.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/agent-harness.js +0 -728
- package/dist/vendor/pi-agent-core/harness/agent-harness.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/compaction/branch-summarization.d.ts +0 -88
- package/dist/vendor/pi-agent-core/harness/compaction/branch-summarization.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/compaction/branch-summarization.js +0 -243
- package/dist/vendor/pi-agent-core/harness/compaction/branch-summarization.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/compaction/compaction.d.ts +0 -122
- package/dist/vendor/pi-agent-core/harness/compaction/compaction.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/compaction/compaction.js +0 -616
- package/dist/vendor/pi-agent-core/harness/compaction/compaction.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/compaction/utils.d.ts +0 -38
- package/dist/vendor/pi-agent-core/harness/compaction/utils.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/compaction/utils.js +0 -153
- package/dist/vendor/pi-agent-core/harness/compaction/utils.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/env/nodejs.d.ts +0 -44
- package/dist/vendor/pi-agent-core/harness/env/nodejs.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/env/nodejs.js +0 -348
- package/dist/vendor/pi-agent-core/harness/env/nodejs.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/execution-env.d.ts +0 -4
- package/dist/vendor/pi-agent-core/harness/execution-env.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/execution-env.js +0 -3
- package/dist/vendor/pi-agent-core/harness/execution-env.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/messages.d.ts +0 -51
- package/dist/vendor/pi-agent-core/harness/messages.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/messages.js +0 -102
- package/dist/vendor/pi-agent-core/harness/messages.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/prompt-templates.d.ts +0 -45
- package/dist/vendor/pi-agent-core/harness/prompt-templates.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/prompt-templates.js +0 -200
- package/dist/vendor/pi-agent-core/harness/prompt-templates.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/repo/jsonl.d.ts +0 -20
- package/dist/vendor/pi-agent-core/harness/session/repo/jsonl.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/repo/jsonl.js +0 -92
- package/dist/vendor/pi-agent-core/harness/session/repo/jsonl.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/repo/memory.d.ts +0 -18
- package/dist/vendor/pi-agent-core/harness/session/repo/memory.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/repo/memory.js +0 -42
- package/dist/vendor/pi-agent-core/harness/session/repo/memory.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/repo/shared.d.ts +0 -10
- package/dist/vendor/pi-agent-core/harness/session/repo/shared.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/repo/shared.js +0 -31
- package/dist/vendor/pi-agent-core/harness/session/repo/shared.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/session.d.ts +0 -32
- package/dist/vendor/pi-agent-core/harness/session/session.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/session.js +0 -196
- package/dist/vendor/pi-agent-core/harness/session/session.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/storage/jsonl.d.ts +0 -30
- package/dist/vendor/pi-agent-core/harness/session/storage/jsonl.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/storage/jsonl.js +0 -170
- package/dist/vendor/pi-agent-core/harness/session/storage/jsonl.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/storage/memory.d.ts +0 -26
- package/dist/vendor/pi-agent-core/harness/session/storage/memory.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/storage/memory.js +0 -90
- package/dist/vendor/pi-agent-core/harness/session/storage/memory.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/uuid.d.ts +0 -2
- package/dist/vendor/pi-agent-core/harness/session/uuid.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/session/uuid.js +0 -40
- package/dist/vendor/pi-agent-core/harness/session/uuid.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/skills.d.ts +0 -41
- package/dist/vendor/pi-agent-core/harness/skills.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/skills.js +0 -259
- package/dist/vendor/pi-agent-core/harness/skills.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/system-prompt.d.ts +0 -3
- package/dist/vendor/pi-agent-core/harness/system-prompt.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/system-prompt.js +0 -30
- package/dist/vendor/pi-agent-core/harness/system-prompt.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/types.d.ts +0 -525
- package/dist/vendor/pi-agent-core/harness/types.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/types.js +0 -16
- package/dist/vendor/pi-agent-core/harness/types.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/utils/shell-output.d.ts +0 -14
- package/dist/vendor/pi-agent-core/harness/utils/shell-output.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/utils/shell-output.js +0 -97
- package/dist/vendor/pi-agent-core/harness/utils/shell-output.js.map +0 -1
- package/dist/vendor/pi-agent-core/harness/utils/truncate.d.ts +0 -70
- package/dist/vendor/pi-agent-core/harness/utils/truncate.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/harness/utils/truncate.js +0 -205
- package/dist/vendor/pi-agent-core/harness/utils/truncate.js.map +0 -1
- package/dist/vendor/pi-agent-core/index.d.ts +0 -21
- package/dist/vendor/pi-agent-core/index.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/index.js +0 -26
- package/dist/vendor/pi-agent-core/index.js.map +0 -1
- package/dist/vendor/pi-agent-core/proxy.d.ts +0 -69
- package/dist/vendor/pi-agent-core/proxy.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/proxy.js +0 -278
- package/dist/vendor/pi-agent-core/proxy.js.map +0 -1
- package/dist/vendor/pi-agent-core/types.d.ts +0 -386
- package/dist/vendor/pi-agent-core/types.d.ts.map +0 -1
- package/dist/vendor/pi-agent-core/types.js +0 -2
- package/dist/vendor/pi-agent-core/types.js.map +0 -1
- package/src/vendor/pi-agent-core/LICENSE +0 -21
- package/src/vendor/pi-agent-core/README.md +0 -15
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Proxy stream function for apps that route LLM calls through a server.
|
|
3
|
-
* The server manages auth and proxies requests to LLM providers.
|
|
4
|
-
*/
|
|
5
|
-
// Internal import for JSON parsing utility
|
|
6
|
-
import { EventStream, parseStreamingJson, } from "@earendil-works/pi-ai";
|
|
7
|
-
// Create stream class matching ProxyMessageEventStream
|
|
8
|
-
class ProxyMessageEventStream extends EventStream {
|
|
9
|
-
constructor() {
|
|
10
|
-
super((event) => event.type === "done" || event.type === "error", (event) => {
|
|
11
|
-
if (event.type === "done")
|
|
12
|
-
return event.message;
|
|
13
|
-
if (event.type === "error")
|
|
14
|
-
return event.error;
|
|
15
|
-
throw new Error("Unexpected event type");
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Stream function that proxies through a server instead of calling LLM providers directly.
|
|
21
|
-
* The server strips the partial field from delta events to reduce bandwidth.
|
|
22
|
-
* We reconstruct the partial message client-side.
|
|
23
|
-
*
|
|
24
|
-
* Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const agent = new Agent({
|
|
29
|
-
* streamFn: (model, context, options) =>
|
|
30
|
-
* streamProxy(model, context, {
|
|
31
|
-
* ...options,
|
|
32
|
-
* authToken: await getAuthToken(),
|
|
33
|
-
* proxyUrl: "https://genai.example.com",
|
|
34
|
-
* }),
|
|
35
|
-
* });
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
function buildProxyRequestOptions(options) {
|
|
39
|
-
return {
|
|
40
|
-
temperature: options.temperature,
|
|
41
|
-
maxTokens: options.maxTokens,
|
|
42
|
-
reasoning: options.reasoning,
|
|
43
|
-
cacheRetention: options.cacheRetention,
|
|
44
|
-
sessionId: options.sessionId,
|
|
45
|
-
headers: options.headers,
|
|
46
|
-
metadata: options.metadata,
|
|
47
|
-
transport: options.transport,
|
|
48
|
-
thinkingBudgets: options.thinkingBudgets,
|
|
49
|
-
maxRetryDelayMs: options.maxRetryDelayMs,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
export function streamProxy(model, context, options) {
|
|
53
|
-
const stream = new ProxyMessageEventStream();
|
|
54
|
-
(async () => {
|
|
55
|
-
// Initialize the partial message that we'll build up from events
|
|
56
|
-
const partial = {
|
|
57
|
-
role: "assistant",
|
|
58
|
-
stopReason: "stop",
|
|
59
|
-
content: [],
|
|
60
|
-
api: model.api,
|
|
61
|
-
provider: model.provider,
|
|
62
|
-
model: model.id,
|
|
63
|
-
usage: {
|
|
64
|
-
input: 0,
|
|
65
|
-
output: 0,
|
|
66
|
-
cacheRead: 0,
|
|
67
|
-
cacheWrite: 0,
|
|
68
|
-
totalTokens: 0,
|
|
69
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
70
|
-
},
|
|
71
|
-
timestamp: Date.now(),
|
|
72
|
-
};
|
|
73
|
-
let reader;
|
|
74
|
-
const abortHandler = () => {
|
|
75
|
-
if (reader) {
|
|
76
|
-
reader.cancel("Request aborted by user").catch(() => { });
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
if (options.signal) {
|
|
80
|
-
options.signal.addEventListener("abort", abortHandler);
|
|
81
|
-
}
|
|
82
|
-
try {
|
|
83
|
-
const response = await fetch(`${options.proxyUrl}/api/stream`, {
|
|
84
|
-
method: "POST",
|
|
85
|
-
headers: {
|
|
86
|
-
Authorization: `Bearer ${options.authToken}`,
|
|
87
|
-
"Content-Type": "application/json",
|
|
88
|
-
},
|
|
89
|
-
body: JSON.stringify({
|
|
90
|
-
model,
|
|
91
|
-
context,
|
|
92
|
-
options: buildProxyRequestOptions(options),
|
|
93
|
-
}),
|
|
94
|
-
signal: options.signal,
|
|
95
|
-
});
|
|
96
|
-
if (!response.ok) {
|
|
97
|
-
let errorMessage = `Proxy error: ${response.status} ${response.statusText}`;
|
|
98
|
-
try {
|
|
99
|
-
const errorData = (await response.json());
|
|
100
|
-
if (errorData.error) {
|
|
101
|
-
errorMessage = `Proxy error: ${errorData.error}`;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
catch {
|
|
105
|
-
// Couldn't parse error response
|
|
106
|
-
}
|
|
107
|
-
throw new Error(errorMessage);
|
|
108
|
-
}
|
|
109
|
-
reader = response.body.getReader();
|
|
110
|
-
const decoder = new TextDecoder();
|
|
111
|
-
let buffer = "";
|
|
112
|
-
while (true) {
|
|
113
|
-
const { done, value } = await reader.read();
|
|
114
|
-
if (done)
|
|
115
|
-
break;
|
|
116
|
-
if (options.signal?.aborted) {
|
|
117
|
-
throw new Error("Request aborted by user");
|
|
118
|
-
}
|
|
119
|
-
buffer += decoder.decode(value, { stream: true });
|
|
120
|
-
const lines = buffer.split("\n");
|
|
121
|
-
buffer = lines.pop() || "";
|
|
122
|
-
for (const line of lines) {
|
|
123
|
-
if (line.startsWith("data: ")) {
|
|
124
|
-
const data = line.slice(6).trim();
|
|
125
|
-
if (data) {
|
|
126
|
-
const proxyEvent = JSON.parse(data);
|
|
127
|
-
const event = processProxyEvent(proxyEvent, partial);
|
|
128
|
-
if (event) {
|
|
129
|
-
stream.push(event);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (options.signal?.aborted) {
|
|
136
|
-
throw new Error("Request aborted by user");
|
|
137
|
-
}
|
|
138
|
-
stream.end();
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
142
|
-
const reason = options.signal?.aborted ? "aborted" : "error";
|
|
143
|
-
partial.stopReason = reason;
|
|
144
|
-
partial.errorMessage = errorMessage;
|
|
145
|
-
stream.push({
|
|
146
|
-
type: "error",
|
|
147
|
-
reason,
|
|
148
|
-
error: partial,
|
|
149
|
-
});
|
|
150
|
-
stream.end();
|
|
151
|
-
}
|
|
152
|
-
finally {
|
|
153
|
-
if (options.signal) {
|
|
154
|
-
options.signal.removeEventListener("abort", abortHandler);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
})();
|
|
158
|
-
return stream;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Process a proxy event and update the partial message.
|
|
162
|
-
*/
|
|
163
|
-
function processProxyEvent(proxyEvent, partial) {
|
|
164
|
-
switch (proxyEvent.type) {
|
|
165
|
-
case "start":
|
|
166
|
-
return { type: "start", partial };
|
|
167
|
-
case "text_start":
|
|
168
|
-
partial.content[proxyEvent.contentIndex] = { type: "text", text: "" };
|
|
169
|
-
return { type: "text_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
170
|
-
case "text_delta": {
|
|
171
|
-
const content = partial.content[proxyEvent.contentIndex];
|
|
172
|
-
if (content?.type === "text") {
|
|
173
|
-
content.text += proxyEvent.delta;
|
|
174
|
-
return {
|
|
175
|
-
type: "text_delta",
|
|
176
|
-
contentIndex: proxyEvent.contentIndex,
|
|
177
|
-
delta: proxyEvent.delta,
|
|
178
|
-
partial,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
throw new Error("Received text_delta for non-text content");
|
|
182
|
-
}
|
|
183
|
-
case "text_end": {
|
|
184
|
-
const content = partial.content[proxyEvent.contentIndex];
|
|
185
|
-
if (content?.type === "text") {
|
|
186
|
-
content.textSignature = proxyEvent.contentSignature;
|
|
187
|
-
return {
|
|
188
|
-
type: "text_end",
|
|
189
|
-
contentIndex: proxyEvent.contentIndex,
|
|
190
|
-
content: content.text,
|
|
191
|
-
partial,
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
throw new Error("Received text_end for non-text content");
|
|
195
|
-
}
|
|
196
|
-
case "thinking_start":
|
|
197
|
-
partial.content[proxyEvent.contentIndex] = { type: "thinking", thinking: "" };
|
|
198
|
-
return { type: "thinking_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
199
|
-
case "thinking_delta": {
|
|
200
|
-
const content = partial.content[proxyEvent.contentIndex];
|
|
201
|
-
if (content?.type === "thinking") {
|
|
202
|
-
content.thinking += proxyEvent.delta;
|
|
203
|
-
return {
|
|
204
|
-
type: "thinking_delta",
|
|
205
|
-
contentIndex: proxyEvent.contentIndex,
|
|
206
|
-
delta: proxyEvent.delta,
|
|
207
|
-
partial,
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
throw new Error("Received thinking_delta for non-thinking content");
|
|
211
|
-
}
|
|
212
|
-
case "thinking_end": {
|
|
213
|
-
const content = partial.content[proxyEvent.contentIndex];
|
|
214
|
-
if (content?.type === "thinking") {
|
|
215
|
-
content.thinkingSignature = proxyEvent.contentSignature;
|
|
216
|
-
return {
|
|
217
|
-
type: "thinking_end",
|
|
218
|
-
contentIndex: proxyEvent.contentIndex,
|
|
219
|
-
content: content.thinking,
|
|
220
|
-
partial,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
throw new Error("Received thinking_end for non-thinking content");
|
|
224
|
-
}
|
|
225
|
-
case "toolcall_start":
|
|
226
|
-
partial.content[proxyEvent.contentIndex] = {
|
|
227
|
-
type: "toolCall",
|
|
228
|
-
id: proxyEvent.id,
|
|
229
|
-
name: proxyEvent.toolName,
|
|
230
|
-
arguments: {},
|
|
231
|
-
partialJson: "",
|
|
232
|
-
};
|
|
233
|
-
return { type: "toolcall_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
234
|
-
case "toolcall_delta": {
|
|
235
|
-
const content = partial.content[proxyEvent.contentIndex];
|
|
236
|
-
if (content?.type === "toolCall") {
|
|
237
|
-
content.partialJson += proxyEvent.delta;
|
|
238
|
-
content.arguments = parseStreamingJson(content.partialJson) || {};
|
|
239
|
-
partial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity
|
|
240
|
-
return {
|
|
241
|
-
type: "toolcall_delta",
|
|
242
|
-
contentIndex: proxyEvent.contentIndex,
|
|
243
|
-
delta: proxyEvent.delta,
|
|
244
|
-
partial,
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
throw new Error("Received toolcall_delta for non-toolCall content");
|
|
248
|
-
}
|
|
249
|
-
case "toolcall_end": {
|
|
250
|
-
const content = partial.content[proxyEvent.contentIndex];
|
|
251
|
-
if (content?.type === "toolCall") {
|
|
252
|
-
delete content.partialJson;
|
|
253
|
-
return {
|
|
254
|
-
type: "toolcall_end",
|
|
255
|
-
contentIndex: proxyEvent.contentIndex,
|
|
256
|
-
toolCall: content,
|
|
257
|
-
partial,
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
return undefined;
|
|
261
|
-
}
|
|
262
|
-
case "done":
|
|
263
|
-
partial.stopReason = proxyEvent.reason;
|
|
264
|
-
partial.usage = proxyEvent.usage;
|
|
265
|
-
return { type: "done", reason: proxyEvent.reason, message: partial };
|
|
266
|
-
case "error":
|
|
267
|
-
partial.stopReason = proxyEvent.reason;
|
|
268
|
-
partial.errorMessage = proxyEvent.errorMessage;
|
|
269
|
-
partial.usage = proxyEvent.usage;
|
|
270
|
-
return { type: "error", reason: proxyEvent.reason, error: partial };
|
|
271
|
-
default: {
|
|
272
|
-
const _exhaustiveCheck = proxyEvent;
|
|
273
|
-
console.warn(`Unhandled proxy event type: ${proxyEvent.type}`);
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
//# sourceMappingURL=proxy.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../src/vendor/pi-agent-core/proxy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,2CAA2C;AAC3C,OAAO,EAIN,WAAW,EAEX,kBAAkB,GAIlB,MAAM,uBAAuB,CAAC;AAE/B,uDAAuD;AACvD,MAAM,uBAA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C;QAED,KAAK,MAAM;YACV,OAAO,CAAC,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC;YACvC,OAAO,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;YACjC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;QAEtE,KAAK,OAAO;YACX,OAAO,CAAC,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC;YACvC,OAAO,CAAC,YAAY,GAAG,UAAU,CAAC,YAAY,CAAC;YAC/C,OAAO,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;YACjC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;QAErE,OAAO,CAAC,CAAC,CAAC;YACT,MAAM,gBAAgB,GAAU,UAAU,CAAC;YAC3C,OAAO,CAAC,IAAI,CAAC,+BAAgC,UAAkB,CAAC,IAAI,EAAE,CAAC,CAAC;YACxE,OAAO,SAAS,CAAC;QAClB,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -1,386 +0,0 @@
|
|
|
1
|
-
import type { AssistantMessage, AssistantMessageEvent, ImageContent, Message, Model, SimpleStreamOptions, streamSimple, TextContent, Tool, ToolResultMessage } from "@earendil-works/pi-ai";
|
|
2
|
-
import type { Static, TSchema } from "typebox";
|
|
3
|
-
/**
|
|
4
|
-
* Stream function used by the agent loop.
|
|
5
|
-
*
|
|
6
|
-
* Contract:
|
|
7
|
-
* - Must not throw or return a rejected promise for request/model/runtime failures.
|
|
8
|
-
* - Must return an AssistantMessageEventStream.
|
|
9
|
-
* - Failures must be encoded in the returned stream via protocol events and a
|
|
10
|
-
* final AssistantMessage with stopReason "error" or "aborted" and errorMessage.
|
|
11
|
-
*/
|
|
12
|
-
export type StreamFn = (...args: Parameters<typeof streamSimple>) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;
|
|
13
|
-
/**
|
|
14
|
-
* Configuration for how tool calls from a single assistant message are executed.
|
|
15
|
-
*
|
|
16
|
-
* - "sequential": each tool call is prepared, executed, and finalized before the next one starts.
|
|
17
|
-
* - "parallel": tool calls are prepared sequentially, then allowed tools execute concurrently.
|
|
18
|
-
* `tool_execution_end` is emitted in tool completion order after each tool is finalized,
|
|
19
|
-
* while tool-result message artifacts are emitted later in assistant source order.
|
|
20
|
-
*/
|
|
21
|
-
export type ToolExecutionMode = "sequential" | "parallel";
|
|
22
|
-
/** A single tool call content block emitted by an assistant message. */
|
|
23
|
-
export type AgentToolCall = Extract<AssistantMessage["content"][number], {
|
|
24
|
-
type: "toolCall";
|
|
25
|
-
}>;
|
|
26
|
-
/**
|
|
27
|
-
* Result returned from `beforeToolCall`.
|
|
28
|
-
*
|
|
29
|
-
* Returning `{ block: true }` prevents the tool from executing. The loop emits an error tool result instead.
|
|
30
|
-
* `reason` becomes the text shown in that error result. If omitted, a default blocked message is used.
|
|
31
|
-
*/
|
|
32
|
-
export interface BeforeToolCallResult {
|
|
33
|
-
block?: boolean;
|
|
34
|
-
reason?: string;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Partial override returned from `afterToolCall`.
|
|
38
|
-
*
|
|
39
|
-
* Merge semantics are field-by-field:
|
|
40
|
-
* - `content`: if provided, replaces the tool result content array in full
|
|
41
|
-
* - `details`: if provided, replaces the tool result details value in full
|
|
42
|
-
* - `isError`: if provided, replaces the tool result error flag
|
|
43
|
-
* - `terminate`: if provided, replaces the early-termination hint
|
|
44
|
-
*
|
|
45
|
-
* Omitted fields keep the original executed tool result values.
|
|
46
|
-
* There is no deep merge for `content` or `details`.
|
|
47
|
-
*/
|
|
48
|
-
export interface AfterToolCallResult {
|
|
49
|
-
content?: (TextContent | ImageContent)[];
|
|
50
|
-
details?: unknown;
|
|
51
|
-
isError?: boolean;
|
|
52
|
-
/**
|
|
53
|
-
* Hint that the agent should stop after the current tool batch.
|
|
54
|
-
* Early termination only happens when every finalized tool result in the batch sets this to true.
|
|
55
|
-
*/
|
|
56
|
-
terminate?: boolean;
|
|
57
|
-
}
|
|
58
|
-
/** Context passed to `beforeToolCall`. */
|
|
59
|
-
export interface BeforeToolCallContext {
|
|
60
|
-
/** The assistant message that requested the tool call. */
|
|
61
|
-
assistantMessage: AssistantMessage;
|
|
62
|
-
/** The raw tool call block from `assistantMessage.content`. */
|
|
63
|
-
toolCall: AgentToolCall;
|
|
64
|
-
/** Validated tool arguments for the target tool schema. */
|
|
65
|
-
args: unknown;
|
|
66
|
-
/** Current agent context at the time the tool call is prepared. */
|
|
67
|
-
context: AgentContext;
|
|
68
|
-
}
|
|
69
|
-
/** Context passed to `afterToolCall`. */
|
|
70
|
-
export interface AfterToolCallContext {
|
|
71
|
-
/** The assistant message that requested the tool call. */
|
|
72
|
-
assistantMessage: AssistantMessage;
|
|
73
|
-
/** The raw tool call block from `assistantMessage.content`. */
|
|
74
|
-
toolCall: AgentToolCall;
|
|
75
|
-
/** Validated tool arguments for the target tool schema. */
|
|
76
|
-
args: unknown;
|
|
77
|
-
/** The executed tool result before any `afterToolCall` overrides are applied. */
|
|
78
|
-
result: AgentToolResult<any>;
|
|
79
|
-
/** Whether the executed tool result is currently treated as an error. */
|
|
80
|
-
isError: boolean;
|
|
81
|
-
/** Current agent context at the time the tool call is finalized. */
|
|
82
|
-
context: AgentContext;
|
|
83
|
-
}
|
|
84
|
-
/** Context passed to `shouldStopAfterTurn`. */
|
|
85
|
-
export interface ShouldStopAfterTurnContext {
|
|
86
|
-
/** The assistant message that completed the turn. */
|
|
87
|
-
message: AssistantMessage;
|
|
88
|
-
/** Tool result messages passed to the preceding `turn_end` event. */
|
|
89
|
-
toolResults: ToolResultMessage[];
|
|
90
|
-
/** Current agent context after the turn's assistant message and tool results have been appended. */
|
|
91
|
-
context: AgentContext;
|
|
92
|
-
/** Messages that this loop invocation will return if it exits at this point. Prompt runs include the initial prompt messages; continuation runs do not include pre-existing context messages. */
|
|
93
|
-
newMessages: AgentMessage[];
|
|
94
|
-
}
|
|
95
|
-
/** Replacement runtime state used by the agent loop before starting another provider request. */
|
|
96
|
-
export interface AgentLoopTurnUpdate {
|
|
97
|
-
/** Context for the next provider request. */
|
|
98
|
-
context?: AgentContext;
|
|
99
|
-
/** Model for the next provider request. */
|
|
100
|
-
model?: Model<any>;
|
|
101
|
-
/** Thinking level for the next provider request. */
|
|
102
|
-
thinkingLevel?: ThinkingLevel;
|
|
103
|
-
}
|
|
104
|
-
export interface PrepareNextTurnContext extends ShouldStopAfterTurnContext {
|
|
105
|
-
}
|
|
106
|
-
export interface AgentLoopConfig extends SimpleStreamOptions {
|
|
107
|
-
model: Model<any>;
|
|
108
|
-
/**
|
|
109
|
-
* Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
|
|
110
|
-
*
|
|
111
|
-
* Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage
|
|
112
|
-
* that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
|
|
113
|
-
* status messages) should be filtered out.
|
|
114
|
-
*
|
|
115
|
-
* Contract: must not throw or reject. Return a safe fallback value instead.
|
|
116
|
-
* Throwing interrupts the low-level agent loop without producing a normal event sequence.
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```typescript
|
|
120
|
-
* convertToLlm: (messages) => messages.flatMap(m => {
|
|
121
|
-
* if (m.role === "custom") {
|
|
122
|
-
* // Convert custom message to user message
|
|
123
|
-
* return [{ role: "user", content: m.content, timestamp: m.timestamp }];
|
|
124
|
-
* }
|
|
125
|
-
* if (m.role === "notification") {
|
|
126
|
-
* // Filter out UI-only messages
|
|
127
|
-
* return [];
|
|
128
|
-
* }
|
|
129
|
-
* // Pass through standard LLM messages
|
|
130
|
-
* return [m];
|
|
131
|
-
* })
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
|
|
135
|
-
/**
|
|
136
|
-
* Optional transform applied to the context before `convertToLlm`.
|
|
137
|
-
*
|
|
138
|
-
* Use this for operations that work at the AgentMessage level:
|
|
139
|
-
* - Context window management (pruning old messages)
|
|
140
|
-
* - Injecting context from external sources
|
|
141
|
-
*
|
|
142
|
-
* Contract: must not throw or reject. Return the original messages or another
|
|
143
|
-
* safe fallback value instead.
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* ```typescript
|
|
147
|
-
* transformContext: async (messages) => {
|
|
148
|
-
* if (estimateTokens(messages) > MAX_TOKENS) {
|
|
149
|
-
* return pruneOldMessages(messages);
|
|
150
|
-
* }
|
|
151
|
-
* return messages;
|
|
152
|
-
* }
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
|
|
156
|
-
/**
|
|
157
|
-
* Resolves an API key dynamically for each LLM call.
|
|
158
|
-
*
|
|
159
|
-
* Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
|
|
160
|
-
* during long-running tool execution phases.
|
|
161
|
-
*
|
|
162
|
-
* Contract: must not throw or reject. Return undefined when no key is available.
|
|
163
|
-
*/
|
|
164
|
-
getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
|
|
165
|
-
/**
|
|
166
|
-
* Called after each turn fully completes and `turn_end` has been emitted.
|
|
167
|
-
*
|
|
168
|
-
* If it returns true, the loop emits `agent_end` and exits before polling steering or follow-up queues,
|
|
169
|
-
* without starting another LLM call. The current assistant response and any tool executions finish normally.
|
|
170
|
-
*
|
|
171
|
-
* Use this to request a graceful stop after the current turn, e.g. before context gets too full.
|
|
172
|
-
*
|
|
173
|
-
* Contract: must not throw or reject. Throwing interrupts the low-level agent loop without producing a normal event sequence.
|
|
174
|
-
*/
|
|
175
|
-
shouldStopAfterTurn?: (context: ShouldStopAfterTurnContext) => boolean | Promise<boolean>;
|
|
176
|
-
/**
|
|
177
|
-
* Called after `turn_end` and before the loop decides whether another provider request should start.
|
|
178
|
-
* Return replacement context/model/thinking state to affect the next turn in this run.
|
|
179
|
-
* Return undefined to keep using the current context/config.
|
|
180
|
-
*/
|
|
181
|
-
prepareNextTurn?: (context: PrepareNextTurnContext) => AgentLoopTurnUpdate | undefined | Promise<AgentLoopTurnUpdate | undefined>;
|
|
182
|
-
/**
|
|
183
|
-
* Returns steering messages to inject into the conversation mid-run.
|
|
184
|
-
*
|
|
185
|
-
* Called after the current assistant turn finishes executing its tool calls, unless `shouldStopAfterTurn` exits first.
|
|
186
|
-
* If messages are returned, they are added to the context before the next LLM call.
|
|
187
|
-
* Tool calls from the current assistant message are not skipped.
|
|
188
|
-
*
|
|
189
|
-
* Use this for "steering" the agent while it's working.
|
|
190
|
-
*
|
|
191
|
-
* Contract: must not throw or reject. Return [] when no steering messages are available.
|
|
192
|
-
*/
|
|
193
|
-
getSteeringMessages?: () => Promise<AgentMessage[]>;
|
|
194
|
-
/**
|
|
195
|
-
* Returns follow-up messages to process after the agent would otherwise stop.
|
|
196
|
-
*
|
|
197
|
-
* Called when the agent has no more tool calls and no steering messages.
|
|
198
|
-
* If messages are returned, they're added to the context and the agent
|
|
199
|
-
* continues with another turn.
|
|
200
|
-
*
|
|
201
|
-
* Use this for follow-up messages that should wait until the agent finishes.
|
|
202
|
-
*
|
|
203
|
-
* Contract: must not throw or reject. Return [] when no follow-up messages are available.
|
|
204
|
-
*/
|
|
205
|
-
getFollowUpMessages?: () => Promise<AgentMessage[]>;
|
|
206
|
-
/**
|
|
207
|
-
* Tool execution mode.
|
|
208
|
-
* - "sequential": execute tool calls one by one
|
|
209
|
-
* - "parallel": preflight tool calls sequentially, then execute allowed tools concurrently;
|
|
210
|
-
* emit `tool_execution_end` in tool completion order after each tool is finalized,
|
|
211
|
-
* then emit tool-result message artifacts later in assistant source order
|
|
212
|
-
*
|
|
213
|
-
* Default: "parallel"
|
|
214
|
-
*/
|
|
215
|
-
toolExecution?: ToolExecutionMode;
|
|
216
|
-
/**
|
|
217
|
-
* Called before a tool is executed, after arguments have been validated.
|
|
218
|
-
*
|
|
219
|
-
* Return `{ block: true }` to prevent execution. The loop emits an error tool result instead.
|
|
220
|
-
* The hook receives the agent abort signal and is responsible for honoring it.
|
|
221
|
-
*/
|
|
222
|
-
beforeToolCall?: (context: BeforeToolCallContext, signal?: AbortSignal) => Promise<BeforeToolCallResult | undefined>;
|
|
223
|
-
/**
|
|
224
|
-
* Called after a tool finishes executing, before `tool_execution_end` and tool-result message events are emitted.
|
|
225
|
-
*
|
|
226
|
-
* Return an `AfterToolCallResult` to override parts of the executed tool result:
|
|
227
|
-
* - `content` replaces the full content array
|
|
228
|
-
* - `details` replaces the full details payload
|
|
229
|
-
* - `isError` replaces the error flag
|
|
230
|
-
* - `terminate` replaces the early-termination hint
|
|
231
|
-
*
|
|
232
|
-
* Any omitted fields keep their original values. No deep merge is performed.
|
|
233
|
-
* The hook receives the agent abort signal and is responsible for honoring it.
|
|
234
|
-
*/
|
|
235
|
-
afterToolCall?: (context: AfterToolCallContext, signal?: AbortSignal) => Promise<AfterToolCallResult | undefined>;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Thinking/reasoning level for models that support it.
|
|
239
|
-
* Note: "xhigh" is only supported by selected model families. Use model thinking-level metadata
|
|
240
|
-
* from @earendil-works/pi-ai to detect support for a concrete model.
|
|
241
|
-
*/
|
|
242
|
-
export type ThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
243
|
-
/**
|
|
244
|
-
* Extensible interface for custom app messages.
|
|
245
|
-
* Apps can extend via declaration merging:
|
|
246
|
-
*
|
|
247
|
-
* @example
|
|
248
|
-
* ```typescript
|
|
249
|
-
* declare module "@mariozechner/agent" {
|
|
250
|
-
* interface CustomAgentMessages {
|
|
251
|
-
* artifact: ArtifactMessage;
|
|
252
|
-
* notification: NotificationMessage;
|
|
253
|
-
* }
|
|
254
|
-
* }
|
|
255
|
-
* ```
|
|
256
|
-
*/
|
|
257
|
-
export interface CustomAgentMessages {
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* AgentMessage: Union of LLM messages + custom messages.
|
|
261
|
-
* This abstraction allows apps to add custom message types while maintaining
|
|
262
|
-
* type safety and compatibility with the base LLM messages.
|
|
263
|
-
*/
|
|
264
|
-
export type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];
|
|
265
|
-
/**
|
|
266
|
-
* Public agent state.
|
|
267
|
-
*
|
|
268
|
-
* `tools` and `messages` use accessor properties so implementations can copy
|
|
269
|
-
* assigned arrays before storing them.
|
|
270
|
-
*/
|
|
271
|
-
export interface AgentState {
|
|
272
|
-
/** System prompt sent with each model request. */
|
|
273
|
-
systemPrompt: string;
|
|
274
|
-
/** Active model used for future turns. */
|
|
275
|
-
model: Model<any>;
|
|
276
|
-
/** Requested reasoning level for future turns. */
|
|
277
|
-
thinkingLevel: ThinkingLevel;
|
|
278
|
-
/** Available tools. Assigning a new array copies the top-level array. */
|
|
279
|
-
set tools(tools: AgentTool<any>[]);
|
|
280
|
-
get tools(): AgentTool<any>[];
|
|
281
|
-
/** Conversation transcript. Assigning a new array copies the top-level array. */
|
|
282
|
-
set messages(messages: AgentMessage[]);
|
|
283
|
-
get messages(): AgentMessage[];
|
|
284
|
-
/**
|
|
285
|
-
* True while the agent is processing a prompt or continuation.
|
|
286
|
-
*
|
|
287
|
-
* This remains true until awaited `agent_end` listeners settle.
|
|
288
|
-
*/
|
|
289
|
-
readonly isStreaming: boolean;
|
|
290
|
-
/** Partial assistant message for the current streamed response, if any. */
|
|
291
|
-
readonly streamingMessage?: AgentMessage;
|
|
292
|
-
/** Tool call ids currently executing. */
|
|
293
|
-
readonly pendingToolCalls: ReadonlySet<string>;
|
|
294
|
-
/** Error message from the most recent failed or aborted assistant turn, if any. */
|
|
295
|
-
readonly errorMessage?: string;
|
|
296
|
-
}
|
|
297
|
-
/** Final or partial result produced by a tool. */
|
|
298
|
-
export interface AgentToolResult<T> {
|
|
299
|
-
/** Text or image content returned to the model. */
|
|
300
|
-
content: (TextContent | ImageContent)[];
|
|
301
|
-
/** Arbitrary structured details for logs or UI rendering. */
|
|
302
|
-
details: T;
|
|
303
|
-
/**
|
|
304
|
-
* Hint that the agent should stop after the current tool batch.
|
|
305
|
-
* Early termination only happens when every finalized tool result in the batch sets this to true.
|
|
306
|
-
*/
|
|
307
|
-
terminate?: boolean;
|
|
308
|
-
}
|
|
309
|
-
/** Callback used by tools to stream partial execution updates. */
|
|
310
|
-
export type AgentToolUpdateCallback<T = any> = (partialResult: AgentToolResult<T>) => void;
|
|
311
|
-
/** Tool definition used by the agent runtime. */
|
|
312
|
-
export interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any> extends Tool<TParameters> {
|
|
313
|
-
/** Human-readable label for UI display. */
|
|
314
|
-
label: string;
|
|
315
|
-
/**
|
|
316
|
-
* Optional compatibility shim for raw tool-call arguments before schema validation.
|
|
317
|
-
* Must return an object that matches `TParameters`.
|
|
318
|
-
*/
|
|
319
|
-
prepareArguments?: (args: unknown) => Static<TParameters>;
|
|
320
|
-
/** Execute the tool call. Throw on failure instead of encoding errors in `content`. */
|
|
321
|
-
execute: (toolCallId: string, params: Static<TParameters>, signal?: AbortSignal, onUpdate?: AgentToolUpdateCallback<TDetails>) => Promise<AgentToolResult<TDetails>>;
|
|
322
|
-
/**
|
|
323
|
-
* Per-tool execution mode override.
|
|
324
|
-
* - "sequential": this tool must execute one at a time with other tool calls.
|
|
325
|
-
* - "parallel": this tool can execute concurrently with other tool calls.
|
|
326
|
-
*
|
|
327
|
-
* If omitted, the default execution mode applies.
|
|
328
|
-
*/
|
|
329
|
-
executionMode?: ToolExecutionMode;
|
|
330
|
-
}
|
|
331
|
-
/** Context snapshot passed into the low-level agent loop. */
|
|
332
|
-
export interface AgentContext {
|
|
333
|
-
/** System prompt included with the request. */
|
|
334
|
-
systemPrompt: string;
|
|
335
|
-
/** Transcript visible to the model. */
|
|
336
|
-
messages: AgentMessage[];
|
|
337
|
-
/** Tools available for this run. */
|
|
338
|
-
tools?: AgentTool<any>[];
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* Events emitted by the Agent for UI updates.
|
|
342
|
-
*
|
|
343
|
-
* `agent_end` is the last event emitted for a run, but awaited `Agent.subscribe()`
|
|
344
|
-
* listeners for that event are still part of run settlement. The agent becomes
|
|
345
|
-
* idle only after those listeners finish.
|
|
346
|
-
*/
|
|
347
|
-
export type AgentEvent = {
|
|
348
|
-
type: "agent_start";
|
|
349
|
-
} | {
|
|
350
|
-
type: "agent_end";
|
|
351
|
-
messages: AgentMessage[];
|
|
352
|
-
} | {
|
|
353
|
-
type: "turn_start";
|
|
354
|
-
} | {
|
|
355
|
-
type: "turn_end";
|
|
356
|
-
message: AgentMessage;
|
|
357
|
-
toolResults: ToolResultMessage[];
|
|
358
|
-
} | {
|
|
359
|
-
type: "message_start";
|
|
360
|
-
message: AgentMessage;
|
|
361
|
-
} | {
|
|
362
|
-
type: "message_update";
|
|
363
|
-
message: AgentMessage;
|
|
364
|
-
assistantMessageEvent: AssistantMessageEvent;
|
|
365
|
-
} | {
|
|
366
|
-
type: "message_end";
|
|
367
|
-
message: AgentMessage;
|
|
368
|
-
} | {
|
|
369
|
-
type: "tool_execution_start";
|
|
370
|
-
toolCallId: string;
|
|
371
|
-
toolName: string;
|
|
372
|
-
args: any;
|
|
373
|
-
} | {
|
|
374
|
-
type: "tool_execution_update";
|
|
375
|
-
toolCallId: string;
|
|
376
|
-
toolName: string;
|
|
377
|
-
args: any;
|
|
378
|
-
partialResult: any;
|
|
379
|
-
} | {
|
|
380
|
-
type: "tool_execution_end";
|
|
381
|
-
toolCallId: string;
|
|
382
|
-
toolName: string;
|
|
383
|
-
result: any;
|
|
384
|
-
isError: boolean;
|
|
385
|
-
};
|
|
386
|
-
//# sourceMappingURL=types.d.ts.map
|