@yolk-sdk/agent 0.1.0-canary.20 → 0.1.0-canary.22
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/README.md +39 -6
- package/dist/client/state.d.mts +4 -2
- package/dist/client/state.d.mts.map +1 -1
- package/dist/client/state.mjs +42 -22
- package/dist/client/state.mjs.map +1 -1
- package/dist/loop/error.d.mts +3 -2
- package/dist/loop/error.d.mts.map +1 -1
- package/dist/loop/error.mjs +6 -3
- package/dist/loop/error.mjs.map +1 -1
- package/dist/loop/run.d.mts.map +1 -1
- package/dist/loop/run.mjs +55 -22
- package/dist/loop/run.mjs.map +1 -1
- package/dist/loop/services/loop-config.mjs +1 -1
- package/dist/loop/services/loop-config.mjs.map +1 -1
- package/dist/protocol/event.d.mts +17 -5
- package/dist/protocol/event.d.mts.map +1 -1
- package/dist/protocol/event.mjs +24 -3
- package/dist/protocol/event.mjs.map +1 -1
- package/dist/protocol/index.d.mts +3 -3
- package/dist/protocol/index.d.mts.map +1 -1
- package/dist/protocol/index.mjs +3 -3
- package/dist/protocol/message.d.mts +20 -1
- package/dist/protocol/message.d.mts.map +1 -1
- package/dist/protocol/message.mjs +59 -1
- package/dist/protocol/message.mjs.map +1 -1
- package/dist/providers/anthropic/claude-provider.d.mts.map +1 -1
- package/dist/providers/anthropic/claude-provider.mjs +38 -15
- package/dist/providers/anthropic/claude-provider.mjs.map +1 -1
- package/dist/providers/openai/codex-provider.d.mts.map +1 -1
- package/dist/providers/openai/codex-provider.mjs +61 -17
- package/dist/providers/openai/codex-provider.mjs.map +1 -1
- package/dist/providers/openai/provider.d.mts.map +1 -1
- package/dist/providers/openai/provider.mjs +18 -10
- package/dist/providers/openai/provider.mjs.map +1 -1
- package/dist/providers/provider-error.d.mts +32 -0
- package/dist/providers/provider-error.d.mts.map +1 -0
- package/dist/providers/provider-error.mjs +91 -0
- package/dist/providers/provider-error.mjs.map +1 -0
- package/dist/providers/transcript.d.mts +9 -0
- package/dist/providers/transcript.d.mts.map +1 -0
- package/dist/providers/transcript.mjs +17 -0
- package/dist/providers/transcript.mjs.map +1 -0
- package/dist/react/chat-core.d.mts +3 -1
- package/dist/react/chat-core.d.mts.map +1 -1
- package/dist/react/chat-core.mjs +42 -6
- package/dist/react/chat-core.mjs.map +1 -1
- package/dist/react/chat-items.d.mts +8 -2
- package/dist/react/chat-items.d.mts.map +1 -1
- package/dist/react/chat-items.mjs +16 -9
- package/dist/react/chat-items.mjs.map +1 -1
- package/package.json +1 -1
- package/src/client/state.ts +43 -18
- package/src/loop/error.ts +6 -3
- package/src/loop/run.ts +83 -21
- package/src/loop/services/loop-config.ts +1 -1
- package/src/protocol/event.ts +26 -2
- package/src/protocol/index.ts +11 -1
- package/src/protocol/message.ts +127 -0
- package/src/providers/anthropic/claude-provider.ts +53 -22
- package/src/providers/openai/codex-provider.ts +83 -19
- package/src/providers/openai/provider.ts +24 -18
- package/src/providers/provider-error.ts +213 -0
- package/src/providers/transcript.ts +24 -0
- package/src/react/chat-core.ts +46 -5
- package/src/react/chat-items.ts +10 -3
- package/src/tools/README.md +3 -2
package/README.md
CHANGED
|
@@ -22,16 +22,16 @@ Canary APIs are unstable. Keep all `@yolk-sdk/*` packages on the same version.
|
|
|
22
22
|
| `@yolk-sdk/agent/loop` | Stateless LLM/tool loop |
|
|
23
23
|
| `@yolk-sdk/agent/loop/testing` | Faux provider and tool executor test helpers |
|
|
24
24
|
| `@yolk-sdk/agent/runtime` | Transcript or append-backed runtime orchestration |
|
|
25
|
-
| `@yolk-sdk/agent/client` | HTTP/NDJSON transport, HITL resume,
|
|
25
|
+
| `@yolk-sdk/agent/client` | HTTP/NDJSON transport, HITL resume, retry/error state helpers |
|
|
26
26
|
| `@yolk-sdk/agent/compaction` | Pure host-owned context budget, planning, and transformer utils |
|
|
27
27
|
| `@yolk-sdk/agent/tools` | Tool module registry, `makeTool`, task/question tool contracts |
|
|
28
28
|
| `@yolk-sdk/agent/react` | Headless React chat hook, reducer, selectors, and render model |
|
|
29
29
|
| `@yolk-sdk/agent/oauth` | Provider-neutral OAuth token and broker contracts |
|
|
30
|
-
| `@yolk-sdk/agent/providers/openai` | OpenAI/Codex OAuth and
|
|
30
|
+
| `@yolk-sdk/agent/providers/openai` | OpenAI/Codex OAuth and broker helpers |
|
|
31
31
|
| `@yolk-sdk/agent/providers/openai/codex` | OpenAI Codex request and auth helpers |
|
|
32
32
|
| `@yolk-sdk/agent/providers/openai/codex-provider` | Codex LLM provider factory |
|
|
33
33
|
| `@yolk-sdk/agent/providers/openai/provider` | OpenAI-compatible LLM provider factory |
|
|
34
|
-
| `@yolk-sdk/agent/providers/anthropic` | Anthropic/Claude OAuth and
|
|
34
|
+
| `@yolk-sdk/agent/providers/anthropic` | Anthropic/Claude OAuth and broker helpers |
|
|
35
35
|
| `@yolk-sdk/agent/providers/anthropic/claude` | Claude request and auth helpers |
|
|
36
36
|
| `@yolk-sdk/agent/providers/anthropic/claude-provider` | Claude LLM provider factory |
|
|
37
37
|
| `@yolk-sdk/agent/skillset` | Portable skill and slash-command parsing/catalogs |
|
|
@@ -41,10 +41,14 @@ Canary APIs are unstable. Keep all `@yolk-sdk/*` packages on the same version.
|
|
|
41
41
|
|
|
42
42
|
```ts
|
|
43
43
|
import {
|
|
44
|
+
danglingHostToolCalls,
|
|
44
45
|
hitlResponseEvent,
|
|
45
46
|
makeSubagentRunId,
|
|
47
|
+
ProviderErrorInfo,
|
|
46
48
|
questionResponseStructuredContent,
|
|
47
|
-
|
|
49
|
+
repairDanglingHostToolCalls,
|
|
50
|
+
UserMessage,
|
|
51
|
+
validateNoDanglingHostToolCalls
|
|
48
52
|
} from '@yolk-sdk/agent/protocol'
|
|
49
53
|
import { run } from '@yolk-sdk/agent/loop'
|
|
50
54
|
import { runRuntime } from '@yolk-sdk/agent/runtime'
|
|
@@ -89,6 +93,24 @@ const program = run({
|
|
|
89
93
|
// Provide LLM provider, loop config, context transformer, and tool executor layers in the host app.
|
|
90
94
|
```
|
|
91
95
|
|
|
96
|
+
## Provider failures and retries
|
|
97
|
+
|
|
98
|
+
Provider adapters classify safe failure metadata at the boundary. The loop owns bounded retry
|
|
99
|
+
policy and emits protocol-visible retry/error state:
|
|
100
|
+
|
|
101
|
+
- `ProviderErrorInfo` carries safe provider id, failure kind, HTTP status, provider code, and
|
|
102
|
+
optional `retryAfterMs`.
|
|
103
|
+
- `AgentRetry.provider` exposes current retry metadata and chosen `delayMs`.
|
|
104
|
+
- `AgentError.provider` preserves final terminal metadata.
|
|
105
|
+
- `AgentErrorCode` includes `rate_limit`, `overloaded`, `context_overflow`, and generic
|
|
106
|
+
`provider_error`.
|
|
107
|
+
- Client and React state keep `error: string | null` for compatibility and add typed `errorInfo` /
|
|
108
|
+
`retryInfo`.
|
|
109
|
+
- `buildAgentChatItems` can project active retry state as an `AgentChatItem` with `_tag: 'Retry'`.
|
|
110
|
+
|
|
111
|
+
Raw provider response bodies stay out of protocol/UI. Hosts own durable persistence and display of
|
|
112
|
+
typed retry/error state.
|
|
113
|
+
|
|
92
114
|
## Context compaction
|
|
93
115
|
|
|
94
116
|
`@yolk-sdk/agent/compaction` provides pure scaffolding only. It does not call a model, persist
|
|
@@ -170,6 +192,14 @@ Annotations must be JSON-compatible. Use stable app-owned keys, preferably `snak
|
|
|
170
192
|
strings for dates inside annotations. Never put secrets, credentials, private ids, auth state, or
|
|
171
193
|
hidden policy in annotations, author, or timestamps; providers may send them to models.
|
|
172
194
|
|
|
195
|
+
## Transcript invariants
|
|
196
|
+
|
|
197
|
+
Every assistant host tool call must be followed by a matching `ToolResultMessage` before the next
|
|
198
|
+
non-tool message/provider request. Use `validateNoDanglingHostToolCalls` for preflight checks,
|
|
199
|
+
`danglingHostToolCalls` for diagnostics, and `repairDanglingHostToolCalls` only when loading older
|
|
200
|
+
persisted transcripts that already have gaps. Built-in providers reject dangling host tool calls
|
|
201
|
+
before vendor lowering with a non-retryable validation error.
|
|
202
|
+
|
|
173
203
|
## Human-in-the-loop
|
|
174
204
|
|
|
175
205
|
HITL is protocol-level, not UI-level:
|
|
@@ -213,13 +243,16 @@ data. `makeTool` converts these failures into `ToolResult.isError = true` so the
|
|
|
213
243
|
see the message and continue. The result includes structured content with `type`, `tool`,
|
|
214
244
|
`reason`, `message`, and optional `details` for UI/runtime handling.
|
|
215
245
|
|
|
216
|
-
|
|
217
|
-
|
|
246
|
+
Thrown `ToolError`s become model-visible failed tool results plus `ToolExecutionError` events,
|
|
247
|
+
so keep messages safe and non-secret. Reserve stream failure for provider/runtime defects,
|
|
248
|
+
aborts, and implementation bugs outside typed tool execution.
|
|
218
249
|
|
|
219
250
|
## Host responsibilities
|
|
220
251
|
|
|
221
252
|
- Choose models/providers and map provider streams into protocol events.
|
|
222
253
|
- Persist sessions, transcripts, and append logs.
|
|
254
|
+
- Persist/return one `ToolResultMessage` for every host tool call, including `isError` failures.
|
|
255
|
+
- Persist terminal provider failures and clear active run ids where applicable.
|
|
223
256
|
- Provide tools, approval policy, auth, storage, and observability.
|
|
224
257
|
- Compact context and decide memory/search policy.
|
|
225
258
|
|
package/dist/client/state.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentEvent, AgentMessage, HitlRequest, QuestionRequest, QuestionResponse, ToolApprovalRequest, ToolCall, ToolResult, UserMessage } from "@yolk-sdk/agent/protocol";
|
|
1
|
+
import { AgentError, AgentEvent, AgentMessage, AgentRetry, HitlRequest, QuestionRequest, QuestionResponse, ToolApprovalRequest, ToolCall, ToolResult, UserMessage } from "@yolk-sdk/agent/protocol";
|
|
2
2
|
|
|
3
3
|
//#region src/client/state.d.ts
|
|
4
4
|
type AgentRunStatus = 'idle' | 'running' | 'waiting' | 'done' | 'error' | 'aborted';
|
|
@@ -57,6 +57,8 @@ type AgentClientState = {
|
|
|
57
57
|
readonly reasoning: string;
|
|
58
58
|
readonly toolRuns: ReadonlyArray<AgentToolRun>;
|
|
59
59
|
readonly error: string | null;
|
|
60
|
+
readonly errorInfo: AgentError | null;
|
|
61
|
+
readonly retryInfo: AgentRetry | null;
|
|
60
62
|
readonly seenEventIds: ReadonlyArray<string>;
|
|
61
63
|
};
|
|
62
64
|
type ApplyAgentEventOptions = {
|
|
@@ -96,7 +98,7 @@ declare const appendAgentMessage: (messages: ReadonlyArray<AgentMessage>, messag
|
|
|
96
98
|
declare const applyAgentEvent: (state: AgentClientState, event: AgentEvent) => AgentClientState;
|
|
97
99
|
declare const applyAgentEventWithOptions: (state: AgentClientState, event: AgentEvent, options?: ApplyAgentEventOptions) => AgentClientState;
|
|
98
100
|
declare const submitAgentUserMessage: (state: AgentClientState, message: UserMessage) => AgentClientState;
|
|
99
|
-
declare const markAgentError: (state: AgentClientState, message?: string) => AgentClientState;
|
|
101
|
+
declare const markAgentError: (state: AgentClientState, message?: string, errorInfo?: AgentError | null) => AgentClientState;
|
|
100
102
|
declare const markAgentAborted: (state: AgentClientState) => AgentClientState;
|
|
101
103
|
declare const reduceAgentEvents: (events: ReadonlyArray<AgentEvent>, initialState?: AgentClientState, options?: ApplyAgentEventOptions) => AgentClientState;
|
|
102
104
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.d.mts","names":[],"sources":["../../src/client/state.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"state.d.mts","names":[],"sources":["../../src/client/state.ts"],"mappings":";;;KAcY,cAAA;AAAA,KAEA,YAAA;EAAA,SAEG,IAAA;EAAA,SACA,EAAA;EAAA,SACA,IAAA;EAAA,SACA,KAAA;AAAA;EAAA,SAEA,IAAA;EAAA,SAA6B,IAAA,EAAM,QAAA;AAAA;EAAA,SAEnC,IAAA;EAAA,SACA,IAAA,EAAM,QAAA;EAAA,SACN,OAAA,GAAU,mBAAA;AAAA;EAAA,SAEV,IAAA;EAAA,SAAyB,UAAA;EAAA,SAA6B,MAAA;AAAA;EAAA,SACtD,IAAA;EAAA,SAAoC,OAAA,EAAS,eAAA;AAAA;EAAA,SAE7C,IAAA;EAAA,SACA,QAAA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;EAAA;;;cAyFV,wBAAA,GACX,QAAA,EAAU,aAAA,CAAc,WAAA,MACvB,aAAA,CAAc,YAAA;AAAA,cAwFJ,kBAAA,GACX,QAAA,EAAU,aAAA,CAAc,YAAA,GACxB,OAAA,EAAS,YAAA,KACR,eAAA;AAAA,cAUU,eAAA,GAAmB,KAAA,EAAO,gBAAA,EAAkB,KAAA,EAAO,UAAA,KAAa,gBAAA;AAAA,cAMhE,0BAAA,GACX,KAAA,EAAO,gBAAA,EACP,KAAA,EAAO,UAAA,EACP,OAAA,GAAS,sBAAA,KACR,gBAAA;AAAA,cAgMU,sBAAA,GACX,KAAA,EAAO,gBAAA,EACP,OAAA,EAAS,WAAA,KACR,gBAAA;AAAA,cAcU,cAAA,GACX,KAAA,EAAO,gBAAA,EACP,OAAA,WACA,SAAA,GAAW,UAAA,YACV,gBAAA;AAAA,cASU,gBAAA,GAAoB,KAAA,EAAO,gBAAA,KAAmB,gBAOzD;AAAA,cAEW,iBAAA,GACX,MAAA,EAAQ,aAAA,CAAc,UAAA,GACtB,YAAA,GAAc,gBAAA,EACd,OAAA,GAAS,sBAAA,KAA2B,gBAAA"}
|
package/dist/client/state.mjs
CHANGED
|
@@ -7,8 +7,14 @@ const initialAgentClientState = {
|
|
|
7
7
|
reasoning: "",
|
|
8
8
|
toolRuns: [],
|
|
9
9
|
error: null,
|
|
10
|
+
errorInfo: null,
|
|
11
|
+
retryInfo: null,
|
|
10
12
|
seenEventIds: []
|
|
11
13
|
};
|
|
14
|
+
const clearRetryInfo = (state) => state.retryInfo === null ? state : {
|
|
15
|
+
...state,
|
|
16
|
+
retryInfo: null
|
|
17
|
+
};
|
|
12
18
|
const hasSeenEvent = (state, event) => event.eventId !== void 0 && state.seenEventIds.includes(event.eventId);
|
|
13
19
|
const rememberEvent = (state, event) => event.eventId === void 0 ? state : {
|
|
14
20
|
...state,
|
|
@@ -117,18 +123,20 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
|
|
|
117
123
|
reasoning: "",
|
|
118
124
|
liveMessages: [],
|
|
119
125
|
toolRuns: completedToolRuns(state.toolRuns),
|
|
120
|
-
error: null
|
|
126
|
+
error: null,
|
|
127
|
+
errorInfo: null,
|
|
128
|
+
retryInfo: null
|
|
121
129
|
};
|
|
122
|
-
case "AgentError": return markAgentError(state, event.message);
|
|
123
|
-
case "LLMTextDelta": return {
|
|
130
|
+
case "AgentError": return markAgentError(state, event.message, event);
|
|
131
|
+
case "LLMTextDelta": return clearRetryInfo({
|
|
124
132
|
...state,
|
|
125
133
|
text: `${state.text}${event.text}`
|
|
126
|
-
};
|
|
127
|
-
case "LLMReasoningDelta": return {
|
|
134
|
+
});
|
|
135
|
+
case "LLMReasoningDelta": return clearRetryInfo({
|
|
128
136
|
...state,
|
|
129
137
|
reasoning: `${state.reasoning}${event.text}`
|
|
130
|
-
};
|
|
131
|
-
case "ToolInputStart": return {
|
|
138
|
+
});
|
|
139
|
+
case "ToolInputStart": return clearRetryInfo({
|
|
132
140
|
...state,
|
|
133
141
|
toolRuns: replaceToolRun(state.toolRuns, {
|
|
134
142
|
_tag: "InputStreaming",
|
|
@@ -136,18 +144,18 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
|
|
|
136
144
|
name: event.name,
|
|
137
145
|
input: ""
|
|
138
146
|
})
|
|
139
|
-
};
|
|
140
|
-
case "ToolInputDelta": return {
|
|
147
|
+
});
|
|
148
|
+
case "ToolInputDelta": return clearRetryInfo({
|
|
141
149
|
...state,
|
|
142
150
|
toolRuns: appendToolInputDelta(state.toolRuns, event.id, event.delta)
|
|
143
|
-
};
|
|
144
|
-
case "ToolInputEnd": return {
|
|
151
|
+
});
|
|
152
|
+
case "ToolInputEnd": return clearRetryInfo({
|
|
145
153
|
...state,
|
|
146
154
|
toolRuns: replaceToolRun(state.toolRuns, {
|
|
147
155
|
_tag: "InputReady",
|
|
148
156
|
call: event.call
|
|
149
157
|
})
|
|
150
|
-
};
|
|
158
|
+
});
|
|
151
159
|
case "ToolApprovalRequested": return {
|
|
152
160
|
...state,
|
|
153
161
|
toolRuns: replaceToolRun(state.toolRuns, {
|
|
@@ -211,20 +219,20 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
|
|
|
211
219
|
endedAtMs: nowMs
|
|
212
220
|
})
|
|
213
221
|
};
|
|
214
|
-
case "ProviderToolResult": return {
|
|
222
|
+
case "ProviderToolResult": return clearRetryInfo({
|
|
215
223
|
...state,
|
|
216
224
|
toolRuns: replaceToolRun(state.toolRuns, {
|
|
217
225
|
_tag: "ProviderCompleted",
|
|
218
226
|
call: event.call,
|
|
219
227
|
result: event.result
|
|
220
228
|
})
|
|
221
|
-
};
|
|
222
|
-
case "AssistantMessage": return {
|
|
229
|
+
});
|
|
230
|
+
case "AssistantMessage": return clearRetryInfo({
|
|
223
231
|
...state,
|
|
224
232
|
liveMessages: [...state.liveMessages, event.message],
|
|
225
233
|
text: "",
|
|
226
234
|
reasoning: ""
|
|
227
|
-
};
|
|
235
|
+
});
|
|
228
236
|
case "AgentEnd": return {
|
|
229
237
|
...state,
|
|
230
238
|
status: "done",
|
|
@@ -232,7 +240,8 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
|
|
|
232
240
|
liveMessages: [],
|
|
233
241
|
text: "",
|
|
234
242
|
reasoning: "",
|
|
235
|
-
toolRuns: completedToolRuns(state.toolRuns)
|
|
243
|
+
toolRuns: completedToolRuns(state.toolRuns),
|
|
244
|
+
retryInfo: null
|
|
236
245
|
};
|
|
237
246
|
case "AgentAwaitingInput": return {
|
|
238
247
|
...state,
|
|
@@ -241,9 +250,14 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
|
|
|
241
250
|
liveMessages: [],
|
|
242
251
|
text: "",
|
|
243
252
|
reasoning: "",
|
|
244
|
-
error: null
|
|
253
|
+
error: null,
|
|
254
|
+
errorInfo: null,
|
|
255
|
+
retryInfo: null
|
|
256
|
+
};
|
|
257
|
+
case "AgentRetry": return {
|
|
258
|
+
...state,
|
|
259
|
+
retryInfo: event
|
|
245
260
|
};
|
|
246
|
-
case "AgentRetry":
|
|
247
261
|
case "CompactionEnd":
|
|
248
262
|
case "CompactionStart":
|
|
249
263
|
case "LLMStreamEnd":
|
|
@@ -264,19 +278,25 @@ const submitAgentUserMessage = (state, message) => ({
|
|
|
264
278
|
reasoning: "",
|
|
265
279
|
toolRuns: completedToolRuns(state.toolRuns),
|
|
266
280
|
error: null,
|
|
281
|
+
errorInfo: null,
|
|
282
|
+
retryInfo: null,
|
|
267
283
|
seenEventIds: []
|
|
268
284
|
});
|
|
269
|
-
const markAgentError = (state, message = "Agent request failed") => ({
|
|
285
|
+
const markAgentError = (state, message = "Agent request failed", errorInfo = null) => ({
|
|
270
286
|
...state,
|
|
271
287
|
status: "error",
|
|
272
288
|
toolRuns: completedToolRuns(state.toolRuns),
|
|
273
|
-
error: message
|
|
289
|
+
error: message,
|
|
290
|
+
errorInfo,
|
|
291
|
+
retryInfo: null
|
|
274
292
|
});
|
|
275
293
|
const markAgentAborted = (state) => ({
|
|
276
294
|
...state,
|
|
277
295
|
status: "aborted",
|
|
278
296
|
toolRuns: completedToolRuns(state.toolRuns),
|
|
279
|
-
error: null
|
|
297
|
+
error: null,
|
|
298
|
+
errorInfo: null,
|
|
299
|
+
retryInfo: null
|
|
280
300
|
});
|
|
281
301
|
const reduceAgentEvents = (events, initialState = initialAgentClientState, options = {}) => events.reduce((state, event) => applyAgentEventWithOptions(state, event, options), initialState);
|
|
282
302
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.mjs","names":[],"sources":["../../src/client/state.ts"],"sourcesContent":["import {\n type AgentEvent,\n type AgentMessage,\n type HitlRequest,\n type QuestionRequest,\n type QuestionResponse,\n type ToolCall,\n type ToolApprovalRequest,\n type ToolResult,\n type UserMessage\n} from '@yolk-sdk/agent/protocol'\n\nexport type AgentRunStatus = 'idle' | 'running' | 'waiting' | 'done' | 'error' | 'aborted'\n\nexport type AgentToolRun =\n | {\n readonly _tag: 'InputStreaming'\n readonly id: string\n readonly name?: string\n readonly input: string\n }\n | { readonly _tag: 'InputReady'; readonly call: ToolCall }\n | {\n readonly _tag: 'ApprovalRequested'\n readonly call: ToolCall\n readonly request?: ToolApprovalRequest\n }\n | { readonly _tag: 'Denied'; readonly toolCallId: string; readonly reason: string }\n | { readonly _tag: 'QuestionRequested'; readonly request: QuestionRequest }\n | {\n readonly _tag: 'QuestionAnswered'\n readonly response: QuestionResponse\n readonly request?: QuestionRequest\n }\n | {\n readonly _tag: 'QuestionCancelled'\n readonly response: QuestionResponse\n readonly request?: QuestionRequest\n }\n | { readonly _tag: 'Executing'; readonly call: ToolCall; readonly startedAtMs: number }\n | {\n readonly _tag: 'Completed'\n readonly call: ToolCall\n readonly result: ToolResult\n readonly startedAtMs: number\n readonly endedAtMs: number\n }\n | {\n readonly _tag: 'Errored'\n readonly call: ToolCall\n readonly message: string\n readonly endedAtMs: number\n }\n | { readonly _tag: 'ProviderCompleted'; readonly call: ToolCall; readonly result: ToolResult }\n\ntype StartedAgentToolRun = Extract<AgentToolRun, { readonly _tag: 'Executing' | 'Completed' }>\n\nexport type AgentClientState = {\n readonly status: AgentRunStatus\n readonly messages: ReadonlyArray<AgentMessage>\n readonly liveMessages: ReadonlyArray<AgentMessage>\n readonly text: string\n readonly reasoning: string\n readonly toolRuns: ReadonlyArray<AgentToolRun>\n readonly error: string | null\n readonly seenEventIds: ReadonlyArray<string>\n}\n\nexport type ApplyAgentEventOptions = {\n readonly nowMs?: number\n}\n\nexport type AgentTranscript = readonly [AgentMessage, ...Array<AgentMessage>]\n\nexport const initialAgentClientState: AgentClientState = {\n status: 'idle',\n messages: [],\n liveMessages: [],\n text: '',\n reasoning: '',\n toolRuns: [],\n error: null,\n seenEventIds: []\n}\n\nconst hasSeenEvent = (state: AgentClientState, event: AgentEvent) =>\n event.eventId !== undefined && state.seenEventIds.includes(event.eventId)\n\nconst rememberEvent = (state: AgentClientState, event: AgentEvent): AgentClientState =>\n event.eventId === undefined\n ? state\n : { ...state, seenEventIds: [...state.seenEventIds, event.eventId] }\n\nconst toolRunId = (run: AgentToolRun) => {\n switch (run._tag) {\n case 'InputStreaming':\n return run.id\n case 'Denied':\n return run.toolCallId\n case 'QuestionRequested':\n return run.request.toolCallId\n case 'QuestionAnswered':\n case 'QuestionCancelled':\n return run.response.toolCallId\n case 'InputReady':\n case 'ApprovalRequested':\n case 'Executing':\n case 'Completed':\n case 'Errored':\n case 'ProviderCompleted':\n return run.call.id\n }\n}\n\nexport const isActiveToolRun = (run: AgentToolRun) =>\n run._tag !== 'Completed' &&\n run._tag !== 'Errored' &&\n run._tag !== 'Denied' &&\n run._tag !== 'QuestionAnswered' &&\n run._tag !== 'QuestionCancelled' &&\n run._tag !== 'ProviderCompleted'\n\nexport const completedToolRuns = (runs: ReadonlyArray<AgentToolRun>) =>\n runs.filter(run => run._tag === 'Completed')\n\nexport const toolRunsFromHitlRequests = (\n requests: ReadonlyArray<HitlRequest>\n): ReadonlyArray<AgentToolRun> =>\n requests.map(request => {\n switch (request._tag) {\n case 'QuestionRequest':\n return { _tag: 'QuestionRequested', request }\n case 'ToolApprovalRequest':\n return { _tag: 'ApprovalRequested', call: request.call, request }\n }\n })\n\nconst replaceToolRun = (\n runs: ReadonlyArray<AgentToolRun>,\n run: AgentToolRun\n): ReadonlyArray<AgentToolRun> => {\n const id = toolRunId(run)\n const replaceIndex = runs.findIndex(current => toolRunId(current) === id)\n\n if (replaceIndex === -1) {\n return [...runs, run]\n }\n\n return runs.flatMap((current, index) => {\n if (toolRunId(current) !== id) {\n return [current]\n }\n\n return index === replaceIndex ? [run] : []\n })\n}\n\nconst isStartedToolRun = (run: AgentToolRun): run is StartedAgentToolRun =>\n run._tag === 'Executing' || run._tag === 'Completed'\n\nconst startedAtMsFor = (runs: ReadonlyArray<AgentToolRun>, toolCallId: string) =>\n runs.filter(isStartedToolRun).find(run => run.call.id === toolCallId)?.startedAtMs\n\nconst appendToolInputDelta = (\n runs: ReadonlyArray<AgentToolRun>,\n id: string,\n delta: string\n): ReadonlyArray<AgentToolRun> =>\n runs.map(run =>\n run._tag === 'InputStreaming' && run.id === id ? { ...run, input: `${run.input}${delta}` } : run\n )\n\nconst questionRequestForToolCall = (\n runs: ReadonlyArray<AgentToolRun>,\n toolCallId: string\n): QuestionRequest | undefined =>\n runs.flatMap(run => {\n if (toolRunId(run) !== toolCallId) {\n return []\n }\n\n switch (run._tag) {\n case 'QuestionRequested':\n return [run.request]\n case 'QuestionAnswered':\n case 'QuestionCancelled':\n return run.request === undefined ? [] : [run.request]\n case 'InputStreaming':\n case 'InputReady':\n case 'ApprovalRequested':\n case 'Denied':\n case 'Executing':\n case 'Completed':\n case 'Errored':\n case 'ProviderCompleted':\n return []\n }\n })[0]\n\nconst questionAnsweredRun = (\n response: QuestionResponse,\n request: QuestionRequest | undefined\n): AgentToolRun =>\n request === undefined\n ? { _tag: 'QuestionAnswered', response }\n : { _tag: 'QuestionAnswered', response, request }\n\nconst questionCancelledRun = (\n response: QuestionResponse,\n request: QuestionRequest | undefined\n): AgentToolRun =>\n request === undefined\n ? { _tag: 'QuestionCancelled', response }\n : { _tag: 'QuestionCancelled', response, request }\n\nexport const appendAgentMessage = (\n messages: ReadonlyArray<AgentMessage>,\n message: AgentMessage\n): AgentTranscript => {\n const first = messages[0]\n\n if (first === undefined) {\n return [message]\n }\n\n return [first, ...messages.slice(1), message]\n}\n\nexport const applyAgentEvent = (state: AgentClientState, event: AgentEvent): AgentClientState => {\n const nowMs = 0\n\n return applyAgentEventWithOptions(state, event, { nowMs })\n}\n\nexport const applyAgentEventWithOptions = (\n state: AgentClientState,\n event: AgentEvent,\n options: ApplyAgentEventOptions = {}\n): AgentClientState => {\n const nowMs = options.nowMs ?? 0\n\n if (hasSeenEvent(state, event)) {\n return state\n }\n\n return rememberEvent(applyAgentEventUnchecked(state, event, nowMs), event)\n}\n\nconst applyAgentEventUnchecked = (\n state: AgentClientState,\n event: AgentEvent,\n nowMs: number\n): AgentClientState => {\n switch (event._tag) {\n case 'AgentStart':\n return {\n ...state,\n status: 'running',\n text: '',\n reasoning: '',\n liveMessages: [],\n toolRuns: completedToolRuns(state.toolRuns),\n error: null\n }\n case 'AgentError':\n return markAgentError(state, event.message)\n case 'LLMTextDelta':\n return { ...state, text: `${state.text}${event.text}` }\n case 'LLMReasoningDelta':\n return { ...state, reasoning: `${state.reasoning}${event.text}` }\n case 'ToolInputStart':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'InputStreaming',\n id: event.id,\n name: event.name,\n input: ''\n })\n }\n case 'ToolInputDelta':\n return { ...state, toolRuns: appendToolInputDelta(state.toolRuns, event.id, event.delta) }\n case 'ToolInputEnd':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, { _tag: 'InputReady', call: event.call })\n }\n case 'ToolApprovalRequested':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'ApprovalRequested',\n call: event.call,\n request: event.request\n })\n }\n case 'ToolApprovalGranted':\n return state\n case 'ToolApprovalDenied':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Denied',\n toolCallId: event.toolCallId,\n reason: event.reason\n })\n }\n case 'QuestionRequested':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'QuestionRequested',\n request: event.request\n })\n }\n case 'QuestionAnswered':\n return {\n ...state,\n toolRuns: replaceToolRun(\n state.toolRuns,\n questionAnsweredRun(\n event.response,\n questionRequestForToolCall(state.toolRuns, event.response.toolCallId)\n )\n )\n }\n case 'QuestionCancelled':\n return {\n ...state,\n toolRuns: replaceToolRun(\n state.toolRuns,\n questionCancelledRun(\n event.response,\n questionRequestForToolCall(state.toolRuns, event.response.toolCallId)\n )\n )\n }\n case 'ToolExecutionStarted':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Executing',\n call: event.call,\n startedAtMs: nowMs\n })\n }\n case 'ToolExecutionCompleted': {\n const endedAtMs = nowMs\n const startedAtMs = startedAtMsFor(state.toolRuns, event.call.id) ?? endedAtMs\n\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Completed',\n call: event.call,\n result: event.result,\n startedAtMs,\n endedAtMs\n })\n }\n }\n case 'ToolExecutionError':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Errored',\n call: event.call,\n message: event.message,\n endedAtMs: nowMs\n })\n }\n case 'ProviderToolResult':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'ProviderCompleted',\n call: event.call,\n result: event.result\n })\n }\n case 'AssistantMessage':\n return {\n ...state,\n liveMessages: [...state.liveMessages, event.message],\n text: '',\n reasoning: ''\n }\n case 'AgentEnd':\n return {\n ...state,\n status: 'done',\n messages: [...state.messages, ...event.messages],\n liveMessages: [],\n text: '',\n reasoning: '',\n toolRuns: completedToolRuns(state.toolRuns)\n }\n case 'AgentAwaitingInput':\n return {\n ...state,\n status: 'waiting',\n messages: [...state.messages, ...event.messages],\n liveMessages: [],\n text: '',\n reasoning: '',\n error: null\n }\n case 'AgentRetry':\n case 'CompactionEnd':\n case 'CompactionStart':\n case 'LLMStreamEnd':\n case 'LLMStreamStart':\n case 'SubagentCompleted':\n case 'SubagentStarted':\n case 'TurnEnd':\n case 'TurnStart':\n case 'UsageUpdate':\n return state\n }\n}\n\nexport const submitAgentUserMessage = (\n state: AgentClientState,\n message: UserMessage\n): AgentClientState => ({\n ...state,\n status: 'running',\n messages: appendAgentMessage(state.messages, message),\n liveMessages: [],\n text: '',\n reasoning: '',\n toolRuns: completedToolRuns(state.toolRuns),\n error: null,\n seenEventIds: []\n})\n\nexport const markAgentError = (\n state: AgentClientState,\n message = 'Agent request failed'\n): AgentClientState => ({\n ...state,\n status: 'error',\n toolRuns: completedToolRuns(state.toolRuns),\n error: message\n})\n\nexport const markAgentAborted = (state: AgentClientState): AgentClientState => ({\n ...state,\n status: 'aborted',\n toolRuns: completedToolRuns(state.toolRuns),\n error: null\n})\n\nexport const reduceAgentEvents = (\n events: ReadonlyArray<AgentEvent>,\n initialState: AgentClientState = initialAgentClientState,\n options: ApplyAgentEventOptions = {}\n) =>\n events.reduce((state, event) => applyAgentEventWithOptions(state, event, options), initialState)\n"],"mappings":";AA0EA,MAAa,0BAA4C;CACvD,QAAQ;CACR,UAAU,CAAC;CACX,cAAc,CAAC;CACf,MAAM;CACN,WAAW;CACX,UAAU,CAAC;CACX,OAAO;CACP,cAAc,CAAC;AACjB;AAEA,MAAM,gBAAgB,OAAyB,UAC7C,MAAM,YAAY,KAAA,KAAa,MAAM,aAAa,SAAS,MAAM,OAAO;AAE1E,MAAM,iBAAiB,OAAyB,UAC9C,MAAM,YAAY,KAAA,IACd,QACA;CAAE,GAAG;CAAO,cAAc,CAAC,GAAG,MAAM,cAAc,MAAM,OAAO;AAAE;AAEvE,MAAM,aAAa,QAAsB;CACvC,QAAQ,IAAI,MAAZ;EACE,KAAK,kBACH,OAAO,IAAI;EACb,KAAK,UACH,OAAO,IAAI;EACb,KAAK,qBACH,OAAO,IAAI,QAAQ;EACrB,KAAK;EACL,KAAK,qBACH,OAAO,IAAI,SAAS;EACtB,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,qBACH,OAAO,IAAI,KAAK;CACpB;AACF;AAEA,MAAa,mBAAmB,QAC9B,IAAI,SAAS,eACb,IAAI,SAAS,aACb,IAAI,SAAS,YACb,IAAI,SAAS,sBACb,IAAI,SAAS,uBACb,IAAI,SAAS;AAEf,MAAa,qBAAqB,SAChC,KAAK,QAAO,QAAO,IAAI,SAAS,WAAW;AAE7C,MAAa,4BACX,aAEA,SAAS,KAAI,YAAW;CACtB,QAAQ,QAAQ,MAAhB;EACE,KAAK,mBACH,OAAO;GAAE,MAAM;GAAqB;EAAQ;EAC9C,KAAK,uBACH,OAAO;GAAE,MAAM;GAAqB,MAAM,QAAQ;GAAM;EAAQ;CACpE;AACF,CAAC;AAE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;GAChB,CAAC;EACH;EACF,KAAK,oBACH,OAAO;GACL,GAAG;GACH,cAAc,CAAC,GAAG,MAAM,cAAc,MAAM,OAAO;GACnD,MAAM;GACN,WAAW;EACb;EACF,KAAK,YACH,OAAO;GACL,GAAG;GACH,QAAQ;GACR,UAAU,CAAC,GAAG,MAAM,UAAU,GAAG,MAAM,QAAQ;GAC/C,cAAc,CAAC;GACf,MAAM;GACN,WAAW;GACX,UAAU,kBAAkB,MAAM,QAAQ;EAC5C;EACF,KAAK,sBACH,OAAO;GACL,GAAG;GACH,QAAQ;GACR,UAAU,CAAC,GAAG,MAAM,UAAU,GAAG,MAAM,QAAQ;GAC/C,cAAc,CAAC;GACf,MAAM;GACN,WAAW;GACX,OAAO;EACT;EACF,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,eACH,OAAO;CACX;AACF;AAEA,MAAa,0BACX,OACA,aACsB;CACtB,GAAG;CACH,QAAQ;CACR,UAAU,mBAAmB,MAAM,UAAU,OAAO;CACpD,cAAc,CAAC;CACf,MAAM;CACN,WAAW;CACX,UAAU,kBAAkB,MAAM,QAAQ;CAC1C,OAAO;CACP,cAAc,CAAC;AACjB;AAEA,MAAa,kBACX,OACA,UAAU,4BACY;CACtB,GAAG;CACH,QAAQ;CACR,UAAU,kBAAkB,MAAM,QAAQ;CAC1C,OAAO;AACT;AAEA,MAAa,oBAAoB,WAA+C;CAC9E,GAAG;CACH,QAAQ;CACR,UAAU,kBAAkB,MAAM,QAAQ;CAC1C,OAAO;AACT;AAEA,MAAa,qBACX,QACA,eAAiC,yBACjC,UAAkC,CAAC,MAEnC,OAAO,QAAQ,OAAO,UAAU,2BAA2B,OAAO,OAAO,OAAO,GAAG,YAAY"}
|
|
1
|
+
{"version":3,"file":"state.mjs","names":[],"sources":["../../src/client/state.ts"],"sourcesContent":["import {\n type AgentError,\n type AgentEvent,\n type AgentMessage,\n type AgentRetry,\n type HitlRequest,\n type QuestionRequest,\n type QuestionResponse,\n type ToolCall,\n type ToolApprovalRequest,\n type ToolResult,\n type UserMessage\n} from '@yolk-sdk/agent/protocol'\n\nexport type AgentRunStatus = 'idle' | 'running' | 'waiting' | 'done' | 'error' | 'aborted'\n\nexport type AgentToolRun =\n | {\n readonly _tag: 'InputStreaming'\n readonly id: string\n readonly name?: string\n readonly input: string\n }\n | { readonly _tag: 'InputReady'; readonly call: ToolCall }\n | {\n readonly _tag: 'ApprovalRequested'\n readonly call: ToolCall\n readonly request?: ToolApprovalRequest\n }\n | { readonly _tag: 'Denied'; readonly toolCallId: string; readonly reason: string }\n | { readonly _tag: 'QuestionRequested'; readonly request: QuestionRequest }\n | {\n readonly _tag: 'QuestionAnswered'\n readonly response: QuestionResponse\n readonly request?: QuestionRequest\n }\n | {\n readonly _tag: 'QuestionCancelled'\n readonly response: QuestionResponse\n readonly request?: QuestionRequest\n }\n | { readonly _tag: 'Executing'; readonly call: ToolCall; readonly startedAtMs: number }\n | {\n readonly _tag: 'Completed'\n readonly call: ToolCall\n readonly result: ToolResult\n readonly startedAtMs: number\n readonly endedAtMs: number\n }\n | {\n readonly _tag: 'Errored'\n readonly call: ToolCall\n readonly message: string\n readonly endedAtMs: number\n }\n | { readonly _tag: 'ProviderCompleted'; readonly call: ToolCall; readonly result: ToolResult }\n\ntype StartedAgentToolRun = Extract<AgentToolRun, { readonly _tag: 'Executing' | 'Completed' }>\n\nexport type AgentClientState = {\n readonly status: AgentRunStatus\n readonly messages: ReadonlyArray<AgentMessage>\n readonly liveMessages: ReadonlyArray<AgentMessage>\n readonly text: string\n readonly reasoning: string\n readonly toolRuns: ReadonlyArray<AgentToolRun>\n readonly error: string | null\n readonly errorInfo: AgentError | null\n readonly retryInfo: AgentRetry | null\n readonly seenEventIds: ReadonlyArray<string>\n}\n\nexport type ApplyAgentEventOptions = {\n readonly nowMs?: number\n}\n\nexport type AgentTranscript = readonly [AgentMessage, ...Array<AgentMessage>]\n\nexport const initialAgentClientState: AgentClientState = {\n status: 'idle',\n messages: [],\n liveMessages: [],\n text: '',\n reasoning: '',\n toolRuns: [],\n error: null,\n errorInfo: null,\n retryInfo: null,\n seenEventIds: []\n}\n\nconst clearRetryInfo = (state: AgentClientState): AgentClientState =>\n state.retryInfo === null ? state : { ...state, retryInfo: null }\n\nconst hasSeenEvent = (state: AgentClientState, event: AgentEvent) =>\n event.eventId !== undefined && state.seenEventIds.includes(event.eventId)\n\nconst rememberEvent = (state: AgentClientState, event: AgentEvent): AgentClientState =>\n event.eventId === undefined\n ? state\n : { ...state, seenEventIds: [...state.seenEventIds, event.eventId] }\n\nconst toolRunId = (run: AgentToolRun) => {\n switch (run._tag) {\n case 'InputStreaming':\n return run.id\n case 'Denied':\n return run.toolCallId\n case 'QuestionRequested':\n return run.request.toolCallId\n case 'QuestionAnswered':\n case 'QuestionCancelled':\n return run.response.toolCallId\n case 'InputReady':\n case 'ApprovalRequested':\n case 'Executing':\n case 'Completed':\n case 'Errored':\n case 'ProviderCompleted':\n return run.call.id\n }\n}\n\nexport const isActiveToolRun = (run: AgentToolRun) =>\n run._tag !== 'Completed' &&\n run._tag !== 'Errored' &&\n run._tag !== 'Denied' &&\n run._tag !== 'QuestionAnswered' &&\n run._tag !== 'QuestionCancelled' &&\n run._tag !== 'ProviderCompleted'\n\nexport const completedToolRuns = (runs: ReadonlyArray<AgentToolRun>) =>\n runs.filter(run => run._tag === 'Completed')\n\nexport const toolRunsFromHitlRequests = (\n requests: ReadonlyArray<HitlRequest>\n): ReadonlyArray<AgentToolRun> =>\n requests.map(request => {\n switch (request._tag) {\n case 'QuestionRequest':\n return { _tag: 'QuestionRequested', request }\n case 'ToolApprovalRequest':\n return { _tag: 'ApprovalRequested', call: request.call, request }\n }\n })\n\nconst replaceToolRun = (\n runs: ReadonlyArray<AgentToolRun>,\n run: AgentToolRun\n): ReadonlyArray<AgentToolRun> => {\n const id = toolRunId(run)\n const replaceIndex = runs.findIndex(current => toolRunId(current) === id)\n\n if (replaceIndex === -1) {\n return [...runs, run]\n }\n\n return runs.flatMap((current, index) => {\n if (toolRunId(current) !== id) {\n return [current]\n }\n\n return index === replaceIndex ? [run] : []\n })\n}\n\nconst isStartedToolRun = (run: AgentToolRun): run is StartedAgentToolRun =>\n run._tag === 'Executing' || run._tag === 'Completed'\n\nconst startedAtMsFor = (runs: ReadonlyArray<AgentToolRun>, toolCallId: string) =>\n runs.filter(isStartedToolRun).find(run => run.call.id === toolCallId)?.startedAtMs\n\nconst appendToolInputDelta = (\n runs: ReadonlyArray<AgentToolRun>,\n id: string,\n delta: string\n): ReadonlyArray<AgentToolRun> =>\n runs.map(run =>\n run._tag === 'InputStreaming' && run.id === id ? { ...run, input: `${run.input}${delta}` } : run\n )\n\nconst questionRequestForToolCall = (\n runs: ReadonlyArray<AgentToolRun>,\n toolCallId: string\n): QuestionRequest | undefined =>\n runs.flatMap(run => {\n if (toolRunId(run) !== toolCallId) {\n return []\n }\n\n switch (run._tag) {\n case 'QuestionRequested':\n return [run.request]\n case 'QuestionAnswered':\n case 'QuestionCancelled':\n return run.request === undefined ? [] : [run.request]\n case 'InputStreaming':\n case 'InputReady':\n case 'ApprovalRequested':\n case 'Denied':\n case 'Executing':\n case 'Completed':\n case 'Errored':\n case 'ProviderCompleted':\n return []\n }\n })[0]\n\nconst questionAnsweredRun = (\n response: QuestionResponse,\n request: QuestionRequest | undefined\n): AgentToolRun =>\n request === undefined\n ? { _tag: 'QuestionAnswered', response }\n : { _tag: 'QuestionAnswered', response, request }\n\nconst questionCancelledRun = (\n response: QuestionResponse,\n request: QuestionRequest | undefined\n): AgentToolRun =>\n request === undefined\n ? { _tag: 'QuestionCancelled', response }\n : { _tag: 'QuestionCancelled', response, request }\n\nexport const appendAgentMessage = (\n messages: ReadonlyArray<AgentMessage>,\n message: AgentMessage\n): AgentTranscript => {\n const first = messages[0]\n\n if (first === undefined) {\n return [message]\n }\n\n return [first, ...messages.slice(1), message]\n}\n\nexport const applyAgentEvent = (state: AgentClientState, event: AgentEvent): AgentClientState => {\n const nowMs = 0\n\n return applyAgentEventWithOptions(state, event, { nowMs })\n}\n\nexport const applyAgentEventWithOptions = (\n state: AgentClientState,\n event: AgentEvent,\n options: ApplyAgentEventOptions = {}\n): AgentClientState => {\n const nowMs = options.nowMs ?? 0\n\n if (hasSeenEvent(state, event)) {\n return state\n }\n\n return rememberEvent(applyAgentEventUnchecked(state, event, nowMs), event)\n}\n\nconst applyAgentEventUnchecked = (\n state: AgentClientState,\n event: AgentEvent,\n nowMs: number\n): AgentClientState => {\n switch (event._tag) {\n case 'AgentStart':\n return {\n ...state,\n status: 'running',\n text: '',\n reasoning: '',\n liveMessages: [],\n toolRuns: completedToolRuns(state.toolRuns),\n error: null,\n errorInfo: null,\n retryInfo: null\n }\n case 'AgentError':\n return markAgentError(state, event.message, event)\n case 'LLMTextDelta':\n return clearRetryInfo({ ...state, text: `${state.text}${event.text}` })\n case 'LLMReasoningDelta':\n return clearRetryInfo({ ...state, reasoning: `${state.reasoning}${event.text}` })\n case 'ToolInputStart':\n return clearRetryInfo({\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'InputStreaming',\n id: event.id,\n name: event.name,\n input: ''\n })\n })\n case 'ToolInputDelta':\n return clearRetryInfo({\n ...state,\n toolRuns: appendToolInputDelta(state.toolRuns, event.id, event.delta)\n })\n case 'ToolInputEnd':\n return clearRetryInfo({\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, { _tag: 'InputReady', call: event.call })\n })\n case 'ToolApprovalRequested':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'ApprovalRequested',\n call: event.call,\n request: event.request\n })\n }\n case 'ToolApprovalGranted':\n return state\n case 'ToolApprovalDenied':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Denied',\n toolCallId: event.toolCallId,\n reason: event.reason\n })\n }\n case 'QuestionRequested':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'QuestionRequested',\n request: event.request\n })\n }\n case 'QuestionAnswered':\n return {\n ...state,\n toolRuns: replaceToolRun(\n state.toolRuns,\n questionAnsweredRun(\n event.response,\n questionRequestForToolCall(state.toolRuns, event.response.toolCallId)\n )\n )\n }\n case 'QuestionCancelled':\n return {\n ...state,\n toolRuns: replaceToolRun(\n state.toolRuns,\n questionCancelledRun(\n event.response,\n questionRequestForToolCall(state.toolRuns, event.response.toolCallId)\n )\n )\n }\n case 'ToolExecutionStarted':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Executing',\n call: event.call,\n startedAtMs: nowMs\n })\n }\n case 'ToolExecutionCompleted': {\n const endedAtMs = nowMs\n const startedAtMs = startedAtMsFor(state.toolRuns, event.call.id) ?? endedAtMs\n\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Completed',\n call: event.call,\n result: event.result,\n startedAtMs,\n endedAtMs\n })\n }\n }\n case 'ToolExecutionError':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'Errored',\n call: event.call,\n message: event.message,\n endedAtMs: nowMs\n })\n }\n case 'ProviderToolResult':\n return clearRetryInfo({\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'ProviderCompleted',\n call: event.call,\n result: event.result\n })\n })\n case 'AssistantMessage':\n return clearRetryInfo({\n ...state,\n liveMessages: [...state.liveMessages, event.message],\n text: '',\n reasoning: ''\n })\n case 'AgentEnd':\n return {\n ...state,\n status: 'done',\n messages: [...state.messages, ...event.messages],\n liveMessages: [],\n text: '',\n reasoning: '',\n toolRuns: completedToolRuns(state.toolRuns),\n retryInfo: null\n }\n case 'AgentAwaitingInput':\n return {\n ...state,\n status: 'waiting',\n messages: [...state.messages, ...event.messages],\n liveMessages: [],\n text: '',\n reasoning: '',\n error: null,\n errorInfo: null,\n retryInfo: null\n }\n case 'AgentRetry':\n return { ...state, retryInfo: event }\n case 'CompactionEnd':\n case 'CompactionStart':\n case 'LLMStreamEnd':\n case 'LLMStreamStart':\n case 'SubagentCompleted':\n case 'SubagentStarted':\n case 'TurnEnd':\n case 'TurnStart':\n case 'UsageUpdate':\n return state\n }\n}\n\nexport const submitAgentUserMessage = (\n state: AgentClientState,\n message: UserMessage\n): AgentClientState => ({\n ...state,\n status: 'running',\n messages: appendAgentMessage(state.messages, message),\n liveMessages: [],\n text: '',\n reasoning: '',\n toolRuns: completedToolRuns(state.toolRuns),\n error: null,\n errorInfo: null,\n retryInfo: null,\n seenEventIds: []\n})\n\nexport const markAgentError = (\n state: AgentClientState,\n message = 'Agent request failed',\n errorInfo: AgentError | null = null\n): AgentClientState => ({\n ...state,\n status: 'error',\n toolRuns: completedToolRuns(state.toolRuns),\n error: message,\n errorInfo,\n retryInfo: null\n})\n\nexport const markAgentAborted = (state: AgentClientState): AgentClientState => ({\n ...state,\n status: 'aborted',\n toolRuns: completedToolRuns(state.toolRuns),\n error: null,\n errorInfo: null,\n retryInfo: null\n})\n\nexport const reduceAgentEvents = (\n events: ReadonlyArray<AgentEvent>,\n initialState: AgentClientState = initialAgentClientState,\n options: ApplyAgentEventOptions = {}\n) =>\n events.reduce((state, event) => applyAgentEventWithOptions(state, event, options), initialState)\n"],"mappings":";AA8EA,MAAa,0BAA4C;CACvD,QAAQ;CACR,UAAU,CAAC;CACX,cAAc,CAAC;CACf,MAAM;CACN,WAAW;CACX,UAAU,CAAC;CACX,OAAO;CACP,WAAW;CACX,WAAW;CACX,cAAc,CAAC;AACjB;AAEA,MAAM,kBAAkB,UACtB,MAAM,cAAc,OAAO,QAAQ;CAAE,GAAG;CAAO,WAAW;AAAK;AAEjE,MAAM,gBAAgB,OAAyB,UAC7C,MAAM,YAAY,KAAA,KAAa,MAAM,aAAa,SAAS,MAAM,OAAO;AAE1E,MAAM,iBAAiB,OAAyB,UAC9C,MAAM,YAAY,KAAA,IACd,QACA;CAAE,GAAG;CAAO,cAAc,CAAC,GAAG,MAAM,c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MAAM,QAAQ;GAC/C,cAAc,CAAC;GACf,MAAM;GACN,WAAW;GACX,OAAO;GACP,WAAW;GACX,WAAW;EACb;EACF,KAAK,cACH,OAAO;GAAE,GAAG;GAAO,WAAW;EAAM;EACtC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,eACH,OAAO;CACX;AACF;AAEA,MAAa,0BACX,OACA,aACsB;CACtB,GAAG;CACH,QAAQ;CACR,UAAU,mBAAmB,MAAM,UAAU,OAAO;CACpD,cAAc,CAAC;CACf,MAAM;CACN,WAAW;CACX,UAAU,kBAAkB,MAAM,QAAQ;CAC1C,OAAO;CACP,WAAW;CACX,WAAW;CACX,cAAc,CAAC;AACjB;AAEA,MAAa,kBACX,OACA,UAAU,wBACV,YAA+B,UACT;CACtB,GAAG;CACH,QAAQ;CACR,UAAU,kBAAkB,MAAM,QAAQ;CAC1C,OAAO;CACP;CACA,WAAW;AACb;AAEA,MAAa,oBAAoB,WAA+C;CAC9E,GAAG;CACH,QAAQ;CACR,UAAU,kBAAkB,MAAM,QAAQ;CAC1C,OAAO;CACP,WAAW;CACX,WAAW;AACb;AAEA,MAAa,qBACX,QACA,eAAiC,yBACjC,UAAkC,CAAC,MAEnC,OAAO,QAAQ,OAAO,UAAU,2BAA2B,OAAO,OAAO,OAAO,GAAG,YAAY"}
|
package/dist/loop/error.d.mts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { AgentError } from "@yolk-sdk/agent/protocol";
|
|
1
|
+
import { AgentError, ProviderErrorInfo } from "@yolk-sdk/agent/protocol";
|
|
2
2
|
import * as Schema from "effect/Schema";
|
|
3
3
|
|
|
4
4
|
//#region src/loop/error.d.ts
|
|
5
5
|
declare const LLMError_base: Schema.Class<LLMError, Schema.TaggedStruct<"LLMError", {
|
|
6
|
-
readonly cause: Schema.Literals<readonly ["validation_error", "provider_error", "rate_limit", "context_overflow", "invalid_response"]>;
|
|
6
|
+
readonly cause: Schema.Literals<readonly ["validation_error", "provider_error", "rate_limit", "overloaded", "context_overflow", "invalid_response"]>;
|
|
7
7
|
readonly message: Schema.String;
|
|
8
8
|
readonly retryable: Schema.Boolean;
|
|
9
|
+
readonly provider: Schema.optional<typeof ProviderErrorInfo>;
|
|
9
10
|
}>, import("effect/Cause").YieldableError>;
|
|
10
11
|
declare class LLMError extends LLMError_base {}
|
|
11
12
|
declare const FauxExhaustedError_base: Schema.Class<FauxExhaustedError, Schema.TaggedStruct<"FauxExhaustedError", {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.mts","names":[],"sources":["../../src/loop/error.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"error.d.mts","names":[],"sources":["../../src/loop/error.ts"],"mappings":";;;;cAC6F,aAAA;;;;;;cAEhF,QAAA,SAAiB,aAY5B;AAAA,cAAG,uBAAA;;;cAEQ,kBAAA,SAA2B,uBAKvC;AAAA,cAAG,cAAA;;;;;cAES,SAAA,SAAkB,cAa7B;AAAA,cAAG,0BAAA;;;;;cAEQ,qBAAA,SAA8B,0BAO1C;AAAA,cAAG,eAAA;;;cAES,UAAA,SAAmB,eAE9B;AAAA,KAEU,gBAAA,GAAmB,QAAA,GAAW,kBAAA,GAAqB,UAAA;AAAA,KAEnD,cAAA,GAAiB,gBAAA,GAAmB,SAAA,GAAY,qBAAA;AAAA,cAmB/C,0BAAA,GAA8B,KAAA,EAAO,cAAA,KAAiB,UAkClE"}
|
package/dist/loop/error.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentError } from "@yolk-sdk/agent/protocol";
|
|
1
|
+
import { AgentError, ProviderErrorInfo } from "@yolk-sdk/agent/protocol";
|
|
2
2
|
import * as Schema from "effect/Schema";
|
|
3
3
|
//#region src/loop/error.ts
|
|
4
4
|
var LLMError = class extends Schema.TaggedErrorClass()("LLMError", {
|
|
@@ -6,11 +6,13 @@ var LLMError = class extends Schema.TaggedErrorClass()("LLMError", {
|
|
|
6
6
|
"validation_error",
|
|
7
7
|
"provider_error",
|
|
8
8
|
"rate_limit",
|
|
9
|
+
"overloaded",
|
|
9
10
|
"context_overflow",
|
|
10
11
|
"invalid_response"
|
|
11
12
|
]),
|
|
12
13
|
message: Schema.String,
|
|
13
|
-
retryable: Schema.Boolean
|
|
14
|
+
retryable: Schema.Boolean,
|
|
15
|
+
provider: Schema.optional(ProviderErrorInfo)
|
|
14
16
|
}) {};
|
|
15
17
|
var FauxExhaustedError = class extends Schema.TaggedErrorClass()("FauxExhaustedError", { message: Schema.String }) {};
|
|
16
18
|
var ToolError = class extends Schema.TaggedErrorClass()("ToolError", {
|
|
@@ -54,7 +56,8 @@ const agentLoopErrorToAgentError = (error) => {
|
|
|
54
56
|
case "LLMError": return AgentError.make({
|
|
55
57
|
code: error.cause,
|
|
56
58
|
message: error.message,
|
|
57
|
-
retryable: error.retryable
|
|
59
|
+
retryable: error.retryable,
|
|
60
|
+
...error.provider === void 0 ? {} : { provider: error.provider }
|
|
58
61
|
});
|
|
59
62
|
case "ToolError": return AgentError.make({
|
|
60
63
|
code: toolErrorCode(error),
|
package/dist/loop/error.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.mjs","names":[],"sources":["../../src/loop/error.ts"],"sourcesContent":["import * as Schema from 'effect/Schema'\nimport { AgentError, type AgentErrorCode } from '@yolk-sdk/agent/protocol'\n\nexport class LLMError extends Schema.TaggedErrorClass<LLMError>()('LLMError', {\n cause: Schema.Literals([\n 'validation_error',\n 'provider_error',\n 'rate_limit',\n 'context_overflow',\n 'invalid_response'\n ]),\n message: Schema.String,\n retryable: Schema.Boolean\n}) {}\n\nexport class FauxExhaustedError extends Schema.TaggedErrorClass<FauxExhaustedError>()(\n 'FauxExhaustedError',\n {\n message: Schema.String\n }\n) {}\n\nexport class ToolError extends Schema.TaggedErrorClass<ToolError>()('ToolError', {\n tool: Schema.String,\n message: Schema.String,\n cause: Schema.Literals([\n 'validation',\n 'invalid_input',\n 'execution',\n 'timeout',\n 'permission',\n 'denied',\n 'not_found',\n 'unavailable'\n ])\n}) {}\n\nexport class ContextTransformError extends Schema.TaggedErrorClass<ContextTransformError>()(\n 'ContextTransformError',\n {\n cause: Schema.Literals(['context_overflow', 'invalid_response']),\n message: Schema.String,\n retryable: Schema.Boolean\n }\n) {}\n\nexport class AbortError extends Schema.TaggedErrorClass<AbortError>()('AbortError', {\n reason: Schema.Literals(['user', 'system', 'max_turns'])\n}) {}\n\nexport type LLMProviderError = LLMError | FauxExhaustedError | AbortError\n\nexport type AgentLoopError = LLMProviderError | ToolError | ContextTransformError\n\nconst toolErrorCode = (error: ToolError): AgentErrorCode => {\n switch (error.cause) {\n case 'validation':\n case 'invalid_input':\n return 'validation_error'\n case 'timeout':\n return 'tool_timeout'\n case 'permission':\n case 'denied':\n return 'tool_denied'\n case 'execution':\n case 'not_found':\n case 'unavailable':\n return 'tool_error'\n }\n}\n\nexport const agentLoopErrorToAgentError = (error: AgentLoopError): AgentError => {\n switch (error._tag) {\n case 'LLMError':\n return AgentError.make({\n code: error.cause,\n message: error.message,\n retryable: error.retryable\n })\n case 'ToolError':\n return AgentError.make({\n code: toolErrorCode(error),\n message: error.message,\n retryable: error.cause === 'timeout'\n })\n case 'ContextTransformError':\n return AgentError.make({\n code: error.cause,\n message: error.message,\n retryable: error.retryable\n })\n case 'AbortError':\n return AgentError.make({\n code: 'aborted',\n message: `Agent run aborted: ${error.reason}`,\n retryable: error.reason === 'system'\n })\n case 'FauxExhaustedError':\n return AgentError.make({\n code: 'provider_error',\n message: error.message,\n retryable: false\n })\n }\n}\n"],"mappings":";;;AAGA,IAAa,WAAb,cAA8B,OAAO,iBAA2B,EAAE,YAAY;CAC5E,OAAO,OAAO,SAAS;EACrB;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,SAAS,OAAO;CAChB,WAAW,OAAO;
|
|
1
|
+
{"version":3,"file":"error.mjs","names":[],"sources":["../../src/loop/error.ts"],"sourcesContent":["import * as Schema from 'effect/Schema'\nimport { AgentError, ProviderErrorInfo, type AgentErrorCode } from '@yolk-sdk/agent/protocol'\n\nexport class LLMError extends Schema.TaggedErrorClass<LLMError>()('LLMError', {\n cause: Schema.Literals([\n 'validation_error',\n 'provider_error',\n 'rate_limit',\n 'overloaded',\n 'context_overflow',\n 'invalid_response'\n ]),\n message: Schema.String,\n retryable: Schema.Boolean,\n provider: Schema.optional(ProviderErrorInfo)\n}) {}\n\nexport class FauxExhaustedError extends Schema.TaggedErrorClass<FauxExhaustedError>()(\n 'FauxExhaustedError',\n {\n message: Schema.String\n }\n) {}\n\nexport class ToolError extends Schema.TaggedErrorClass<ToolError>()('ToolError', {\n tool: Schema.String,\n message: Schema.String,\n cause: Schema.Literals([\n 'validation',\n 'invalid_input',\n 'execution',\n 'timeout',\n 'permission',\n 'denied',\n 'not_found',\n 'unavailable'\n ])\n}) {}\n\nexport class ContextTransformError extends Schema.TaggedErrorClass<ContextTransformError>()(\n 'ContextTransformError',\n {\n cause: Schema.Literals(['context_overflow', 'invalid_response']),\n message: Schema.String,\n retryable: Schema.Boolean\n }\n) {}\n\nexport class AbortError extends Schema.TaggedErrorClass<AbortError>()('AbortError', {\n reason: Schema.Literals(['user', 'system', 'max_turns'])\n}) {}\n\nexport type LLMProviderError = LLMError | FauxExhaustedError | AbortError\n\nexport type AgentLoopError = LLMProviderError | ToolError | ContextTransformError\n\nconst toolErrorCode = (error: ToolError): AgentErrorCode => {\n switch (error.cause) {\n case 'validation':\n case 'invalid_input':\n return 'validation_error'\n case 'timeout':\n return 'tool_timeout'\n case 'permission':\n case 'denied':\n return 'tool_denied'\n case 'execution':\n case 'not_found':\n case 'unavailable':\n return 'tool_error'\n }\n}\n\nexport const agentLoopErrorToAgentError = (error: AgentLoopError): AgentError => {\n switch (error._tag) {\n case 'LLMError':\n return AgentError.make({\n code: error.cause,\n message: error.message,\n retryable: error.retryable,\n ...(error.provider === undefined ? {} : { provider: error.provider })\n })\n case 'ToolError':\n return AgentError.make({\n code: toolErrorCode(error),\n message: error.message,\n retryable: error.cause === 'timeout'\n })\n case 'ContextTransformError':\n return AgentError.make({\n code: error.cause,\n message: error.message,\n retryable: error.retryable\n })\n case 'AbortError':\n return AgentError.make({\n code: 'aborted',\n message: `Agent run aborted: ${error.reason}`,\n retryable: error.reason === 'system'\n })\n case 'FauxExhaustedError':\n return AgentError.make({\n code: 'provider_error',\n message: error.message,\n retryable: false\n })\n }\n}\n"],"mappings":";;;AAGA,IAAa,WAAb,cAA8B,OAAO,iBAA2B,EAAE,YAAY;CAC5E,OAAO,OAAO,SAAS;EACrB;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,SAAS,OAAO;CAChB,WAAW,OAAO;CAClB,UAAU,OAAO,SAAS,iBAAiB;AAC7C,CAAC,EAAE,CAAC;AAEJ,IAAa,qBAAb,cAAwC,OAAO,iBAAqC,EAClF,sBACA,EACE,SAAS,OAAO,OAClB,CACF,EAAE,CAAC;AAEH,IAAa,YAAb,cAA+B,OAAO,iBAA4B,EAAE,aAAa;CAC/E,MAAM,OAAO;CACb,SAAS,OAAO;CAChB,OAAO,OAAO,SAAS;EACrB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;AACH,CAAC,EAAE,CAAC;AAEJ,IAAa,wBAAb,cAA2C,OAAO,iBAAwC,EACxF,yBACA;CACE,OAAO,OAAO,SAAS,CAAC,oBAAoB,kBAAkB,CAAC;CAC/D,SAAS,OAAO;CAChB,WAAW,OAAO;AACpB,CACF,EAAE,CAAC;AAEH,IAAa,aAAb,cAAgC,OAAO,iBAA6B,EAAE,cAAc,EAClF,QAAQ,OAAO,SAAS;CAAC;CAAQ;CAAU;AAAW,CAAC,EACzD,CAAC,EAAE,CAAC;AAMJ,MAAM,iBAAiB,UAAqC;CAC1D,QAAQ,MAAM,OAAd;EACE,KAAK;EACL,KAAK,iBACH,OAAO;EACT,KAAK,WACH,OAAO;EACT,KAAK;EACL,KAAK,UACH,OAAO;EACT,KAAK;EACL,KAAK;EACL,KAAK,eACH,OAAO;CACX;AACF;AAEA,MAAa,8BAA8B,UAAsC;CAC/E,QAAQ,MAAM,MAAd;EACE,KAAK,YACH,OAAO,WAAW,KAAK;GACrB,MAAM,MAAM;GACZ,SAAS,MAAM;GACf,WAAW,MAAM;GACjB,GAAI,MAAM,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU,MAAM,SAAS;EACrE,CAAC;EACH,KAAK,aACH,OAAO,WAAW,KAAK;GACrB,MAAM,cAAc,KAAK;GACzB,SAAS,MAAM;GACf,WAAW,MAAM,UAAU;EAC7B,CAAC;EACH,KAAK,yBACH,OAAO,WAAW,KAAK;GACrB,MAAM,MAAM;GACZ,SAAS,MAAM;GACf,WAAW,MAAM;EACnB,CAAC;EACH,KAAK,cACH,OAAO,WAAW,KAAK;GACrB,MAAM;GACN,SAAS,sBAAsB,MAAM;GACrC,WAAW,MAAM,WAAW;EAC9B,CAAC;EACH,KAAK,sBACH,OAAO,WAAW,KAAK;GACrB,MAAM;GACN,SAAS,MAAM;GACf,WAAW;EACb,CAAC;CACL;AACF"}
|
package/dist/loop/run.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.mts","names":[],"sources":["../../src/loop/run.ts"],"mappings":";;;;;;;;;KAoEY,cAAA;AAAA,KAEA,SAAA;EAAA,SACD,QAAA,EAAU,aAAA,CAAc,YAAA;EAAA,SACxB,YAAA;EAAA,SACA,KAAA,EAAO,aAAA,CAAc,OAAA;EAAA,SACrB,aAAA,GAAgB,aAAA,CAAc,YAAA;EAAA,SAC9B,KAAA;EAAA,SACA,eAAA,GAAkB,oBAAA;EAAA,SAClB,YAAA,GAAe,sBAAA;AAAA;AAAA,KAGd,eAAA,GAAkB,SAAS;EAAA,SAC5B,IAAI;AAAA;AAAA,KAGH,eAAA;EAAA,SACD,KAAA,EAAO,aAAA,CAAc,QAAA;EAAA,SACrB,KAA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AA"}
|
package/dist/loop/run.mjs
CHANGED
|
@@ -50,6 +50,32 @@ const subagentCompletedEvent = (input) => {
|
|
|
50
50
|
createdAtMs: input.endedAtMs
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
|
+
const toolCompletionEvents = (input) => {
|
|
54
|
+
const completed = subagentCompletedEvent(input);
|
|
55
|
+
const toolCompleted = ToolExecutionCompleted.make({
|
|
56
|
+
call: input.call,
|
|
57
|
+
result: input.result,
|
|
58
|
+
createdAtMs: input.endedAtMs
|
|
59
|
+
});
|
|
60
|
+
return completed === void 0 ? [toolCompleted] : [toolCompleted, completed];
|
|
61
|
+
};
|
|
62
|
+
const toolErrorResult = (call, error) => ToolResult.make({
|
|
63
|
+
toolCallId: call.id,
|
|
64
|
+
content: error.message,
|
|
65
|
+
isError: true
|
|
66
|
+
});
|
|
67
|
+
const toolErrorEvents = (input) => [ToolExecutionError.make({
|
|
68
|
+
call: input.call,
|
|
69
|
+
message: input.error.message,
|
|
70
|
+
code: toolErrorCode(input.error),
|
|
71
|
+
createdAtMs: input.endedAtMs
|
|
72
|
+
}), ...toolCompletionEvents({
|
|
73
|
+
call: input.call,
|
|
74
|
+
result: toolErrorResult(input.call, input.error),
|
|
75
|
+
model: input.model,
|
|
76
|
+
startedAtMs: input.startedAtMs,
|
|
77
|
+
endedAtMs: input.endedAtMs
|
|
78
|
+
})];
|
|
53
79
|
const unsupportedInputError = (message) => new LLMError({
|
|
54
80
|
cause: "validation_error",
|
|
55
81
|
message,
|
|
@@ -111,7 +137,20 @@ const isLlmEvent = (event) => {
|
|
|
111
137
|
default: return false;
|
|
112
138
|
}
|
|
113
139
|
};
|
|
114
|
-
const
|
|
140
|
+
const maxUnhintedRetryDelayMs = 3e4;
|
|
141
|
+
const maxHintedRetryDelayMs = 2147483647;
|
|
142
|
+
const validDelayMs = (delayMs) => Number.isFinite(delayMs) && delayMs >= 0 ? Math.floor(delayMs) : void 0;
|
|
143
|
+
const hintedRetryDelayMs = (error) => {
|
|
144
|
+
const delayMs = error.provider?.retryAfterMs;
|
|
145
|
+
const validDelay = delayMs === void 0 ? void 0 : validDelayMs(delayMs);
|
|
146
|
+
return validDelay === void 0 ? void 0 : Math.min(validDelay, maxHintedRetryDelayMs);
|
|
147
|
+
};
|
|
148
|
+
const retryDelayMs = (baseDelayMs, attempt, error) => {
|
|
149
|
+
const hintedDelay = hintedRetryDelayMs(error);
|
|
150
|
+
if (hintedDelay !== void 0) return hintedDelay;
|
|
151
|
+
const delayMs = validDelayMs(baseDelayMs * 2 ** Math.max(0, attempt - 1)) ?? 0;
|
|
152
|
+
return Math.min(delayMs, maxUnhintedRetryDelayMs);
|
|
153
|
+
};
|
|
115
154
|
const retryReason = (error) => error.cause;
|
|
116
155
|
const retrySleep = (delayMs) => delayMs === 0 ? Effect.void : Effect.sleep(`${delayMs} millis`);
|
|
117
156
|
const failAgentLoopError = (error) => Stream.fail(error);
|
|
@@ -119,12 +158,13 @@ const sleepStream = (delayMs) => Stream.fromEffect(retrySleep(delayMs)).pipe(Str
|
|
|
119
158
|
const withProviderRetries = (stream, loopConfig, makeStream, attempt) => Stream.unwrap(Ref.make(false).pipe(Effect.map((emittedProviderEvent) => stream.pipe(Stream.tap(() => Ref.set(emittedProviderEvent, true)), Stream.catchTags({
|
|
120
159
|
LLMError: (error) => Stream.unwrap(Ref.get(emittedProviderEvent).pipe(Effect.map((emitted) => {
|
|
121
160
|
if (emitted || !error.retryable || error.cause === "context_overflow" || attempt > loopConfig.maxRetries) return failAgentLoopError(error);
|
|
122
|
-
const delayMs = retryDelayMs(loopConfig.retryBaseDelayMs, attempt);
|
|
161
|
+
const delayMs = retryDelayMs(loopConfig.retryBaseDelayMs, attempt, error);
|
|
123
162
|
return Stream.make(AgentRetry.make({
|
|
124
163
|
attempt,
|
|
125
164
|
reason: retryReason(error),
|
|
126
165
|
delayMs,
|
|
127
|
-
message: error.message
|
|
166
|
+
message: error.message,
|
|
167
|
+
...error.provider === void 0 ? {} : { provider: error.provider }
|
|
128
168
|
})).pipe(Stream.concat(sleepStream(delayMs)), Stream.concat(withProviderRetries(makeStream(), loopConfig, makeStream, attempt + 1)));
|
|
129
169
|
}))),
|
|
130
170
|
AbortError: failAgentLoopError,
|
|
@@ -144,26 +184,19 @@ const makeToolExecutionStream = (executor, call, model) => Stream.unwrap(Effect.
|
|
|
144
184
|
call,
|
|
145
185
|
createdAtMs: startedAtMs
|
|
146
186
|
}), started];
|
|
147
|
-
return Stream.fromIterable(startEvents).pipe(Stream.concat(Stream.fromEffect(executor.execute(call).pipe(Effect.flatMap((result) => Clock.currentTimeMillis.pipe(Effect.map((endedAtMs) => {
|
|
148
|
-
const completed = subagentCompletedEvent({
|
|
149
|
-
call,
|
|
150
|
-
result,
|
|
151
|
-
model,
|
|
152
|
-
startedAtMs,
|
|
153
|
-
endedAtMs
|
|
154
|
-
});
|
|
155
|
-
const toolCompleted = ToolExecutionCompleted.make({
|
|
156
|
-
call,
|
|
157
|
-
result,
|
|
158
|
-
createdAtMs: endedAtMs
|
|
159
|
-
});
|
|
160
|
-
return completed === void 0 ? [toolCompleted] : [toolCompleted, completed];
|
|
161
|
-
}))))).pipe(Stream.flatMap(Stream.fromIterable), Stream.catchTag("ToolError", (error) => Stream.fromEffect(Clock.currentTimeMillis).pipe(Stream.flatMap((endedAtMs) => Stream.make(ToolExecutionError.make({
|
|
187
|
+
return Stream.fromIterable(startEvents).pipe(Stream.concat(Stream.fromEffect(executor.execute(call).pipe(Effect.flatMap((result) => Clock.currentTimeMillis.pipe(Effect.map((endedAtMs) => toolCompletionEvents({
|
|
162
188
|
call,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
189
|
+
result,
|
|
190
|
+
model,
|
|
191
|
+
startedAtMs,
|
|
192
|
+
endedAtMs
|
|
193
|
+
})))))).pipe(Stream.flatMap(Stream.fromIterable), Stream.catchTag("ToolError", (error) => Stream.fromEffect(Clock.currentTimeMillis).pipe(Stream.flatMap((endedAtMs) => Stream.fromIterable(toolErrorEvents({
|
|
194
|
+
call,
|
|
195
|
+
error,
|
|
196
|
+
model,
|
|
197
|
+
startedAtMs,
|
|
198
|
+
endedAtMs
|
|
199
|
+
}))))))));
|
|
167
200
|
}));
|
|
168
201
|
const boundedToolConcurrency = (loopConfig) => Math.max(1, loopConfig.toolConcurrency);
|
|
169
202
|
const toolResultMessageFromResult = (result) => ToolResultMessage.make({
|