@yolk-sdk/agent 0.1.0-canary.114 → 0.1.0-canary.115
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 +6 -5
- package/dist/client/index.d.mts +2 -2
- package/dist/client/state.d.mts +7 -8
- package/dist/client/state.d.mts.map +1 -1
- package/dist/client/state.mjs +157 -169
- package/dist/client/state.mjs.map +1 -1
- package/dist/loop/error.d.mts.map +1 -1
- package/dist/loop/error.mjs +4 -7
- package/dist/loop/error.mjs.map +1 -1
- package/dist/loop/run.d.mts.map +1 -1
- package/dist/loop/run.mjs +2 -4
- package/dist/loop/run.mjs.map +1 -1
- package/dist/protocol/event.d.mts +13 -2
- package/dist/protocol/event.d.mts.map +1 -1
- package/dist/protocol/event.mjs +23 -2
- package/dist/protocol/event.mjs.map +1 -1
- package/dist/protocol/index.d.mts +3 -2
- package/dist/protocol/index.d.mts.map +1 -1
- package/dist/protocol/index.mjs +3 -2
- package/dist/protocol/status.d.mts +133 -0
- package/dist/protocol/status.d.mts.map +1 -0
- package/dist/protocol/status.mjs +198 -0
- package/dist/protocol/status.mjs.map +1 -0
- package/dist/react/chat-actions.d.mts +35 -35
- package/dist/react/chat-actions.d.mts.map +1 -1
- package/dist/react/chat-core.d.mts +4 -7
- package/dist/react/chat-core.d.mts.map +1 -1
- package/dist/react/chat-core.mjs +30 -87
- package/dist/react/chat-core.mjs.map +1 -1
- package/dist/react/chat-items.d.mts +30 -30
- package/dist/react/chat-items.d.mts.map +1 -1
- package/dist/react/chat-messages.d.mts +76 -76
- package/dist/react/chat-messages.d.mts.map +1 -1
- package/dist/react/index.d.mts +2 -2
- package/dist/react/use-agent-chat.d.mts +10 -3
- package/dist/react/use-agent-chat.d.mts.map +1 -1
- package/dist/react/use-agent-chat.mjs +4 -4
- package/dist/react/use-agent-chat.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client/index.ts +0 -1
- package/src/client/state.ts +125 -127
- package/src/loop/error.ts +9 -3
- package/src/loop/run.ts +5 -12
- package/src/protocol/event.ts +34 -1
- package/src/protocol/index.ts +36 -0
- package/src/protocol/status.ts +427 -0
- package/src/react/chat-core.ts +143 -231
- package/src/react/index.ts +0 -1
- package/src/react/use-agent-chat.ts +4 -4
package/README.md
CHANGED
|
@@ -121,9 +121,9 @@ import { FauxProvider, Reply, TestToolExecutor } from '@yolk-sdk/agent/loop/test
|
|
|
121
121
|
|
|
122
122
|
## Headless React chat
|
|
123
123
|
|
|
124
|
-
`useAgentChat` exposes protocol messages, render-oriented chat messages, run
|
|
125
|
-
|
|
126
|
-
package supplies no components, styling, auth, or route ownership, and React remains an optional
|
|
124
|
+
`useAgentChat` exposes protocol messages, render-oriented chat messages, the run `status`
|
|
125
|
+
(`AgentStatus`), per-subagent `subagents` statuses, running/waiting flags, and actions for submit,
|
|
126
|
+
stop, edit, regenerate, delete, tool approval, question, and typed input responses. The package supplies no components, styling, auth, or route ownership, and React remains an optional
|
|
127
127
|
peer used only by React subpaths.
|
|
128
128
|
|
|
129
129
|
Chat ADTs on `@yolk-sdk/agent/react` are value constructors: `AgentChatPart`, `ChatToolState`,
|
|
@@ -455,8 +455,9 @@ policy and emits protocol-visible retry/error state:
|
|
|
455
455
|
- `AgentError.provider` preserves final terminal metadata.
|
|
456
456
|
- `AgentErrorCode` includes `rate_limit`, `overloaded`, `context_overflow`, and generic
|
|
457
457
|
`provider_error`.
|
|
458
|
-
- Client and React state
|
|
459
|
-
`
|
|
458
|
+
- Client and React state carry an `AgentStatus` (ADR 0010): terminal errors are `Failed` (with the
|
|
459
|
+
`AgentError` when the stream sent one) and pending retries `Working(Retrying)`
|
|
460
|
+
(`agentStatusRetry`).
|
|
460
461
|
- `buildAgentChatItems` can project active retry state as an `AgentChatItem` with `_tag: 'Retry'`.
|
|
461
462
|
- Anthropic prompt-too-long responses become non-retryable `context_overflow`; the host-owned
|
|
462
463
|
compaction wrapper may compact and retry once.
|
package/dist/client/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { documentPartFromTextFile, textFromBlob } from "./attachments.mjs";
|
|
2
|
-
import { AgentClientState,
|
|
2
|
+
import { AgentClientState, AgentToolRun, AgentTranscript, ApplyAgentEventOptions, appendAgentMessage, applyAgentEvent, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests } from "./state.mjs";
|
|
3
3
|
import { AgentHttpResponseInfo, AgentRunContinuationOptions, AgentRunIdleReconnectOptions, AgentTransportError, CancelAgentRunRequest, StreamAgentEventHandler, StreamAgentEventsRequest, StreamAgentEventsUntilTerminalRequest, StreamAgentRunEventsRequest, StreamAgentRunEventsUntilTerminalRequest, StreamAgentRunHitlResponseEventsRequest, StreamAgentRunHitlResponseEventsUntilTerminalRequest, StreamCloudflareAgentEventsRequest, SubmitInputResponseRequest, SubmitInteractionResponseRequest, SubmitQuestionResponseRequest, SubmitToolApprovalResponseRequest, agentRunEndpointWithStartIndex, agentRunIdFromHeaders, agentRunStreamStartIndexFromHeaders, agentRunStreamTailIndexFromHeaders, cancelAgentRun, collectAgentEvents, streamAgentEventStream, streamAgentEventStreamUntilTerminal, streamAgentRunEventStream, streamAgentRunEventStreamUntilTerminal, streamAgentRunHitlResponseEventStream, streamAgentRunHitlResponseEventStreamUntilTerminal, streamCloudflareAgentEventStream, streamInputResponseEventStream, streamInteractionResponseEventStream, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream } from "./transport.mjs";
|
|
4
|
-
export { type AgentClientState, type AgentHttpResponseInfo, type AgentRunContinuationOptions, type AgentRunIdleReconnectOptions, type
|
|
4
|
+
export { type AgentClientState, type AgentHttpResponseInfo, type AgentRunContinuationOptions, type AgentRunIdleReconnectOptions, type AgentToolRun, type AgentTranscript, AgentTransportError, type ApplyAgentEventOptions, type CancelAgentRunRequest, type StreamAgentEventHandler, type StreamAgentEventsRequest, type StreamAgentEventsUntilTerminalRequest, type StreamAgentRunEventsRequest, type StreamAgentRunEventsUntilTerminalRequest, type StreamAgentRunHitlResponseEventsRequest, type StreamAgentRunHitlResponseEventsUntilTerminalRequest, type StreamCloudflareAgentEventsRequest, type SubmitInputResponseRequest, type SubmitInteractionResponseRequest, type SubmitQuestionResponseRequest, type SubmitToolApprovalResponseRequest, agentRunEndpointWithStartIndex, agentRunIdFromHeaders, agentRunStreamStartIndexFromHeaders, agentRunStreamTailIndexFromHeaders, appendAgentMessage, applyAgentEvent, cancelAgentRun, collectAgentEvents, completedToolRuns, documentPartFromTextFile, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, streamAgentEventStream, streamAgentEventStreamUntilTerminal, streamAgentRunEventStream, streamAgentRunEventStreamUntilTerminal, streamAgentRunHitlResponseEventStream, streamAgentRunHitlResponseEventStreamUntilTerminal, streamCloudflareAgentEventStream, streamInputResponseEventStream, streamInteractionResponseEventStream, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitAgentUserMessage, textFromBlob, toolRunsFromHitlRequests };
|
package/dist/client/state.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { Data } from "effect";
|
|
2
|
-
import { AgentError, AgentEvent, AgentMessage,
|
|
2
|
+
import { AgentError, AgentEvent, AgentMessage, AgentStatus, AgentStatusTree, HitlRequest, InputRequest, InputResponse, InteractionRequest, InteractionResponse, QuestionRequest, QuestionResponse, ToolApprovalRequest, ToolCall, ToolResult, UserMessage } from "@yolk-sdk/agent/protocol";
|
|
3
3
|
|
|
4
4
|
//#region src/client/state.d.ts
|
|
5
|
-
type AgentRunStatus = 'idle' | 'running' | 'waiting' | 'done' | 'error' | 'aborted';
|
|
6
5
|
type AgentToolRun = {
|
|
7
6
|
readonly _tag: 'InputStreaming';
|
|
8
7
|
readonly id: string;
|
|
@@ -479,15 +478,13 @@ declare const AgentToolRun: {
|
|
|
479
478
|
};
|
|
480
479
|
};
|
|
481
480
|
type AgentClientState = {
|
|
482
|
-
readonly status:
|
|
481
|
+
/** Run status; failures and pending retries live here (`Failed`, `Working(Retrying)`). */readonly status: AgentStatus;
|
|
482
|
+
readonly subagents: AgentStatusTree['subagents'];
|
|
483
483
|
readonly messages: ReadonlyArray<AgentMessage>;
|
|
484
484
|
readonly liveMessages: ReadonlyArray<AgentMessage>;
|
|
485
485
|
readonly text: string;
|
|
486
486
|
readonly reasoning: string;
|
|
487
487
|
readonly toolRuns: ReadonlyArray<AgentToolRun>;
|
|
488
|
-
readonly error: string | null;
|
|
489
|
-
readonly errorInfo: AgentError | null;
|
|
490
|
-
readonly retryInfo: AgentRetry | null;
|
|
491
488
|
readonly seenEventIds: ReadonlyArray<string>;
|
|
492
489
|
};
|
|
493
490
|
type ApplyAgentEventOptions = {
|
|
@@ -538,9 +535,11 @@ declare const appendAgentMessage: (messages: ReadonlyArray<AgentMessage>, messag
|
|
|
538
535
|
declare const applyAgentEvent: (state: AgentClientState, event: AgentEvent) => AgentClientState;
|
|
539
536
|
declare const applyAgentEventWithOptions: (state: AgentClientState, event: AgentEvent, options?: ApplyAgentEventOptions) => AgentClientState;
|
|
540
537
|
declare const submitAgentUserMessage: (state: AgentClientState, message: UserMessage) => AgentClientState;
|
|
541
|
-
|
|
538
|
+
/** Marks the run `Failed`, e.g. after a transport failure that produced no `AgentError` event. */
|
|
539
|
+
declare const markAgentError: (state: AgentClientState, message?: string, error?: AgentError) => AgentClientState;
|
|
540
|
+
/** The person stopped the run; per OSC 7501 a cancelled run is `Idle`. */
|
|
542
541
|
declare const markAgentAborted: (state: AgentClientState) => AgentClientState;
|
|
543
542
|
declare const reduceAgentEvents: (events: ReadonlyArray<AgentEvent>, initialState?: AgentClientState, options?: ApplyAgentEventOptions) => AgentClientState;
|
|
544
543
|
//#endregion
|
|
545
|
-
export { AgentClientState,
|
|
544
|
+
export { AgentClientState, AgentToolRun, AgentTranscript, ApplyAgentEventOptions, appendAgentMessage, applyAgentEvent, applyAgentEventWithOptions, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests };
|
|
546
545
|
//# sourceMappingURL=state.d.mts.map
|
|
@@ -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":";;;;KA0BY,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,EAAU,gBAAA;EAAA,SACV,OAAA,GAAU,eAAA;AAAA;EAAA,SAGV,IAAA;EAAA,SACA,QAAA,EAAU,gBAAA;EAAA,SACV,OAAA,GAAU,eAAA;AAAA;EAAA,SAEV,IAAA;EAAA,SAAiC,OAAA,EAAS,YAAA;AAAA;EAAA,SAE1C,IAAA;EAAA,SACA,QAAA,EAAU,aAAA;EAAA,SACV,OAAA,GAAU,YAAA;AAAA;EAAA,SAGV,IAAA;EAAA,SACA,QAAA,EAAU,aAAA;EAAA,SACV,OAAA,GAAU,YAAA;AAAA;EAAA,SAEV,IAAA;EAAA,SAAuC,OAAA,EAAS,kBAAA;AAAA;EAAA,SAEhD,IAAA;EAAA,SACA,QAAA,EAAU,mBAAA;EAAA,SACV,OAAA,GAAU,kBAAA;AAAA;EAAA,SAGV,IAAA;EAAA,SACA,QAAA,EAAU,mBAAA;EAAA,SACV,OAAA,GAAU,kBAAA;AAAA;EAAA,SAGV,IAAA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A;oBAWM,kBAAA;AAAA;EAAA;qBAGtC,mBAAA;EAAA,mBACA,kBAAA;AAAA;EAAA;iBASJ,QAAA;EAAA,8BAb0C;EAAA;;cAgJlD,iBAAA,GAAqB,IAAA,EAAM,aAAA,CAAc,YAAA;EAAA;iBA5HjC,QAAA;EAAA,iBACE,UAAA;EAAA;;;cAwIV,wBAAA,GACX,QAAA,EAAU,aAAA,CAAc,WAAA,MACvB,aAAA,CAAc,YAAA;AAAA,cAwNJ,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,cAmSU,sBAAA,GACX,KAAA,EAAO,gBAAA,EACP,OAAA,EAAS,WAAA,KACR,gBAAA;;cAeU,cAAA,GACX,KAAA,EAAO,gBAAA,EACP,OAAA,WACA,KAAA,GAAQ,UAAA,KACP,gBAAA;;cAUU,gBAAA,GAAoB,KAAA,EAAO,gBAAA,KAAmB,gBAIxD;AAAA,cAEU,iBAAA,GACX,MAAA,EAAQ,aAAA,CAAc,UAAA,GACtB,YAAA,GAAc,gBAAA,EACd,OAAA,GAAS,sBAAA,KAA2B,gBAAA"}
|
package/dist/client/state.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Data, Match, Predicate } from "effect";
|
|
2
2
|
import * as Schema$1 from "effect/Schema";
|
|
3
|
+
import { AgentFailed, idleAgentStatus, nextAgentStatusTree, settleAgentStatusTree, startAgentStatusTree } from "@yolk-sdk/agent/protocol";
|
|
3
4
|
//#region src/client/state.ts
|
|
4
5
|
const AgentToolRun = Data.taggedEnum();
|
|
5
6
|
const AgentToolRunInputStreaming = Schema$1.TaggedStruct("InputStreaming", {
|
|
@@ -12,20 +13,23 @@ const AgentToolRunDenied = Schema$1.TaggedStruct("Denied", {
|
|
|
12
13
|
reason: Schema$1.String
|
|
13
14
|
});
|
|
14
15
|
const initialAgentClientState = {
|
|
15
|
-
status:
|
|
16
|
+
status: idleAgentStatus,
|
|
17
|
+
subagents: [],
|
|
16
18
|
messages: [],
|
|
17
19
|
liveMessages: [],
|
|
18
20
|
text: "",
|
|
19
21
|
reasoning: "",
|
|
20
22
|
toolRuns: [],
|
|
21
|
-
error: null,
|
|
22
|
-
errorInfo: null,
|
|
23
|
-
retryInfo: null,
|
|
24
23
|
seenEventIds: []
|
|
25
24
|
};
|
|
26
|
-
const
|
|
25
|
+
const statusTree = (state) => ({
|
|
26
|
+
root: state.status,
|
|
27
|
+
subagents: state.subagents
|
|
28
|
+
});
|
|
29
|
+
const withStatusTree = (state, tree) => tree.root === state.status && tree.subagents === state.subagents ? state : {
|
|
27
30
|
...state,
|
|
28
|
-
|
|
31
|
+
status: tree.root,
|
|
32
|
+
subagents: tree.subagents
|
|
29
33
|
};
|
|
30
34
|
const hasSeenEvent = (state, event) => event.eventId !== void 0 && state.seenEventIds.includes(event.eventId);
|
|
31
35
|
const rememberEvent = (state, event) => event.eventId === void 0 ? state : {
|
|
@@ -107,186 +111,170 @@ const applyAgentEvent = (state, event) => {
|
|
|
107
111
|
const applyAgentEventWithOptions = (state, event, options = {}) => {
|
|
108
112
|
const nowMs = options.nowMs ?? 0;
|
|
109
113
|
if (hasSeenEvent(state, event)) return state;
|
|
110
|
-
return rememberEvent(applyAgentEventUnchecked(state, event, nowMs), event);
|
|
111
|
-
};
|
|
112
|
-
const activeEventToolCallId = (event) => Match.value(event).pipe(Match.tag("ToolInputStart", "ToolInputDelta", (current) => current.id), Match.tag("ToolInputEnd", "ToolExecutionStarted", "ToolApprovalRequested", (current) => current.call.id), Match.tag("QuestionRequested", (current) => current.request.toolCallId), Match.tag("InputRequested", (current) => current.request.toolCallId), Match.tag("InteractionRequested", (current) => current.request.toolCallId), Match.orElse(() => void 0));
|
|
113
|
-
const applyAgentEventUnchecked = (state, event, nowMs) => {
|
|
114
114
|
const activeCallId = activeEventToolCallId(event);
|
|
115
|
-
|
|
116
|
-
if (activeCallId !== void 0 && (state.messages.some(acknowledgesActiveCall) || state.liveMessages.some(acknowledgesActiveCall))) return {
|
|
115
|
+
if (activeCallId !== void 0 && isAcknowledgedCall(state, activeCallId)) return rememberEvent({
|
|
117
116
|
...state,
|
|
118
117
|
toolRuns: state.toolRuns.filter((run) => toolRunId(run) !== activeCallId || !isActiveToolRun(run))
|
|
119
|
-
};
|
|
120
|
-
return
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
118
|
+
}, event);
|
|
119
|
+
return rememberEvent(withStatusTree(applyAgentEventUnchecked(state, event, nowMs), nextAgentStatusTree(statusTree(state), event)), event);
|
|
120
|
+
};
|
|
121
|
+
const isAcknowledgedCall = (state, toolCallId) => {
|
|
122
|
+
const acknowledges = (message) => Predicate.isTagged(message, "ToolResult") && message.toolCallId === toolCallId && message.acceptance !== void 0;
|
|
123
|
+
return state.messages.some(acknowledges) || state.liveMessages.some(acknowledges);
|
|
124
|
+
};
|
|
125
|
+
const activeEventToolCallId = (event) => Match.value(event).pipe(Match.tag("ToolInputStart", "ToolInputDelta", (current) => current.id), Match.tag("ToolInputEnd", "ToolExecutionStarted", "ToolApprovalRequested", (current) => current.call.id), Match.tag("QuestionRequested", (current) => current.request.toolCallId), Match.tag("InputRequested", (current) => current.request.toolCallId), Match.tag("InteractionRequested", (current) => current.request.toolCallId), Match.orElse(() => void 0));
|
|
126
|
+
const applyAgentEventUnchecked = (state, event, nowMs) => Match.value(event).pipe(Match.withReturnType(), Match.tag("AgentStart", () => ({
|
|
127
|
+
...state,
|
|
128
|
+
text: "",
|
|
129
|
+
reasoning: "",
|
|
130
|
+
liveMessages: [],
|
|
131
|
+
toolRuns: retainedSettledToolRuns(state.toolRuns)
|
|
132
|
+
})), Match.tag("AgentError", () => ({
|
|
133
|
+
...state,
|
|
134
|
+
toolRuns: retainedSettledToolRuns(state.toolRuns)
|
|
135
|
+
})), Match.tag("LLMTextDelta", (current) => ({
|
|
136
|
+
...state,
|
|
137
|
+
text: `${state.text}${current.text}`
|
|
138
|
+
})), Match.tag("LLMReasoningDelta", (current) => ({
|
|
139
|
+
...state,
|
|
140
|
+
reasoning: `${state.reasoning}${current.text}`
|
|
141
|
+
})), Match.tag("ToolInputStart", (current) => ({
|
|
142
|
+
...state,
|
|
143
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRunInputStreaming.make({
|
|
144
|
+
id: current.id,
|
|
145
|
+
name: current.name,
|
|
146
|
+
input: ""
|
|
147
|
+
}))
|
|
148
|
+
})), Match.tag("ToolInputDelta", (current) => ({
|
|
149
|
+
...state,
|
|
150
|
+
toolRuns: appendToolInputDelta(state.toolRuns, current.id, current.delta)
|
|
151
|
+
})), Match.tag("ToolInputEnd", (current) => ({
|
|
152
|
+
...state,
|
|
153
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.InputReady({ call: current.call }))
|
|
154
|
+
})), Match.tag("LLMReasoningReplay", () => state), Match.tag("ToolApprovalRequested", (current) => ({
|
|
155
|
+
...state,
|
|
156
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.ApprovalRequested({
|
|
157
|
+
call: current.call,
|
|
158
|
+
request: current.request
|
|
159
|
+
}))
|
|
160
|
+
})), Match.tag("ToolApprovalGranted", () => state), Match.tag("ToolApprovalDenied", (current) => ({
|
|
161
|
+
...state,
|
|
162
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRunDenied.make({
|
|
163
|
+
toolCallId: current.toolCallId,
|
|
164
|
+
reason: current.reason
|
|
165
|
+
}))
|
|
166
|
+
})), Match.tag("QuestionRequested", (current) => ({
|
|
167
|
+
...state,
|
|
168
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.QuestionRequested({ request: current.request }))
|
|
169
|
+
})), Match.tag("InputRequested", (current) => ({
|
|
170
|
+
...state,
|
|
171
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.InputRequested({ request: current.request }))
|
|
172
|
+
})), Match.tag("InteractionRequested", (current) => ({
|
|
173
|
+
...state,
|
|
174
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.InteractionRequested({ request: current.request }))
|
|
175
|
+
}))).pipe(Match.tag("QuestionAnswered", (current) => ({
|
|
176
|
+
...state,
|
|
177
|
+
toolRuns: replaceToolRun(state.toolRuns, questionAnsweredRun(current.response, questionRequestForToolCall(state.toolRuns, current.response.toolCallId)))
|
|
178
|
+
})), Match.tag("QuestionCancelled", (current) => ({
|
|
179
|
+
...state,
|
|
180
|
+
toolRuns: replaceToolRun(state.toolRuns, questionCancelledRun(current.response, questionRequestForToolCall(state.toolRuns, current.response.toolCallId)))
|
|
181
|
+
})), Match.tag("InputSubmitted", (current) => ({
|
|
182
|
+
...state,
|
|
183
|
+
toolRuns: replaceToolRun(state.toolRuns, inputSubmittedRun(current.response, inputRequestForToolCall(state.toolRuns, current.response.toolCallId)))
|
|
184
|
+
})), Match.tag("InputCancelled", (current) => ({
|
|
185
|
+
...state,
|
|
186
|
+
toolRuns: replaceToolRun(state.toolRuns, inputCancelledRun(current.response, inputRequestForToolCall(state.toolRuns, current.response.toolCallId)))
|
|
187
|
+
})), Match.tag("InteractionSubmitted", (current) => ({
|
|
188
|
+
...state,
|
|
189
|
+
toolRuns: replaceToolRun(state.toolRuns, interactionSubmittedRun(current.response, interactionRequestForToolCall(state.toolRuns, current.response.toolCallId)))
|
|
190
|
+
})), Match.tag("InteractionCancelled", (current) => ({
|
|
191
|
+
...state,
|
|
192
|
+
toolRuns: replaceToolRun(state.toolRuns, interactionCancelledRun(current.response, interactionRequestForToolCall(state.toolRuns, current.response.toolCallId)))
|
|
193
|
+
})), Match.tag("ToolExecutionStarted", (current) => {
|
|
194
|
+
const prior = state.toolRuns.find((run) => toolRunId(run) === current.call.id);
|
|
195
|
+
const executing = AgentToolRun.Executing({
|
|
196
|
+
call: current.call,
|
|
197
|
+
startedAtMs: nowMs
|
|
198
|
+
});
|
|
199
|
+
const isInteraction = Predicate.isTagged(prior, "InteractionSubmitted") || Predicate.isTagged(prior, "Executing") && prior.interaction === true;
|
|
200
|
+
return {
|
|
186
201
|
...state,
|
|
187
|
-
toolRuns: replaceToolRun(state.toolRuns,
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
...state,
|
|
197
|
-
toolRuns: replaceToolRun(state.toolRuns, isInteraction ? {
|
|
198
|
-
...executing,
|
|
199
|
-
interaction: true
|
|
200
|
-
} : executing)
|
|
201
|
-
};
|
|
202
|
-
}), Match.tag("ToolExecutionAccepted", "ToolExecutionCompleted", (current) => {
|
|
203
|
-
const endedAtMs = nowMs;
|
|
204
|
-
const startedAtMs = startedAtMsFor(state.toolRuns, current.call.id) ?? endedAtMs;
|
|
205
|
-
return {
|
|
206
|
-
...state,
|
|
207
|
-
toolRuns: replaceToolRun(state.toolRuns, Predicate.isTagged(current, "ToolExecutionAccepted") ? AgentToolRun.Accepted({
|
|
208
|
-
call: current.call,
|
|
209
|
-
result: current.result,
|
|
210
|
-
startedAtMs,
|
|
211
|
-
endedAtMs
|
|
212
|
-
}) : AgentToolRun.Completed({
|
|
213
|
-
call: current.call,
|
|
214
|
-
result: current.result,
|
|
215
|
-
startedAtMs,
|
|
216
|
-
endedAtMs
|
|
217
|
-
}))
|
|
218
|
-
};
|
|
219
|
-
}), Match.tag("ToolExecutionError", (current) => ({
|
|
202
|
+
toolRuns: replaceToolRun(state.toolRuns, isInteraction ? {
|
|
203
|
+
...executing,
|
|
204
|
+
interaction: true
|
|
205
|
+
} : executing)
|
|
206
|
+
};
|
|
207
|
+
}), Match.tag("ToolExecutionAccepted", "ToolExecutionCompleted", (current) => {
|
|
208
|
+
const endedAtMs = nowMs;
|
|
209
|
+
const startedAtMs = startedAtMsFor(state.toolRuns, current.call.id) ?? endedAtMs;
|
|
210
|
+
return {
|
|
220
211
|
...state,
|
|
221
|
-
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.
|
|
212
|
+
toolRuns: replaceToolRun(state.toolRuns, Predicate.isTagged(current, "ToolExecutionAccepted") ? AgentToolRun.Accepted({
|
|
222
213
|
call: current.call,
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
...state,
|
|
228
|
-
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.ProviderCompleted({
|
|
214
|
+
result: current.result,
|
|
215
|
+
startedAtMs,
|
|
216
|
+
endedAtMs
|
|
217
|
+
}) : AgentToolRun.Completed({
|
|
229
218
|
call: current.call,
|
|
230
|
-
result: current.result
|
|
219
|
+
result: current.result,
|
|
220
|
+
startedAtMs,
|
|
221
|
+
endedAtMs
|
|
231
222
|
}))
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
223
|
+
};
|
|
224
|
+
}), Match.tag("ToolExecutionError", (current) => ({
|
|
225
|
+
...state,
|
|
226
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.Errored({
|
|
227
|
+
call: current.call,
|
|
228
|
+
message: current.message,
|
|
229
|
+
endedAtMs: nowMs
|
|
230
|
+
}))
|
|
231
|
+
})), Match.tag("ProviderToolResult", (current) => ({
|
|
232
|
+
...state,
|
|
233
|
+
toolRuns: replaceToolRun(state.toolRuns, AgentToolRun.ProviderCompleted({
|
|
234
|
+
call: current.call,
|
|
235
|
+
result: current.result
|
|
236
|
+
}))
|
|
237
|
+
})), Match.tag("UserMessage", "AssistantMessage", (current) => ({
|
|
238
|
+
...state,
|
|
239
|
+
liveMessages: [...state.liveMessages, current.message],
|
|
240
|
+
text: "",
|
|
241
|
+
reasoning: ""
|
|
242
|
+
})), Match.tag("AgentEnd", (current) => ({
|
|
243
|
+
...state,
|
|
244
|
+
messages: [...state.messages, ...current.messages],
|
|
245
|
+
liveMessages: [],
|
|
246
|
+
text: "",
|
|
247
|
+
reasoning: "",
|
|
248
|
+
toolRuns: retainedSettledToolRuns(state.toolRuns)
|
|
249
|
+
})), Match.tag("AgentAwaitingInput", (current) => ({
|
|
250
|
+
...state,
|
|
251
|
+
messages: [...state.messages, ...current.messages],
|
|
252
|
+
liveMessages: [],
|
|
253
|
+
text: "",
|
|
254
|
+
reasoning: ""
|
|
255
|
+
})), Match.tag("AgentRetry", "CompactionEnd", "CompactionStart", "LLMStreamEnd", "LLMStreamStart", "SubagentCompleted", "SubagentStarted", "TurnEnd", "TurnStart", "UsageUpdate", () => state), Match.exhaustive);
|
|
256
|
+
const submitAgentUserMessage = (state, message) => withStatusTree({
|
|
262
257
|
...state,
|
|
263
|
-
status: "running",
|
|
264
258
|
messages: appendAgentMessage(state.messages, message),
|
|
265
259
|
liveMessages: [],
|
|
266
260
|
text: "",
|
|
267
261
|
reasoning: "",
|
|
268
262
|
toolRuns: retainedSettledToolRuns(state.toolRuns),
|
|
269
|
-
error: null,
|
|
270
|
-
errorInfo: null,
|
|
271
|
-
retryInfo: null,
|
|
272
263
|
seenEventIds: []
|
|
273
|
-
});
|
|
274
|
-
|
|
264
|
+
}, startAgentStatusTree(statusTree(state)));
|
|
265
|
+
/** Marks the run `Failed`, e.g. after a transport failure that produced no `AgentError` event. */
|
|
266
|
+
const markAgentError = (state, message = "Agent request failed", error) => withStatusTree({
|
|
275
267
|
...state,
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const markAgentAborted = (state) => ({
|
|
268
|
+
toolRuns: retainedSettledToolRuns(state.toolRuns)
|
|
269
|
+
}, settleAgentStatusTree(statusTree(state), error === void 0 ? AgentFailed.make({ message }) : AgentFailed.make({
|
|
270
|
+
message,
|
|
271
|
+
error
|
|
272
|
+
})));
|
|
273
|
+
/** The person stopped the run; per OSC 7501 a cancelled run is `Idle`. */
|
|
274
|
+
const markAgentAborted = (state) => withStatusTree({
|
|
283
275
|
...state,
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
error: null,
|
|
287
|
-
errorInfo: null,
|
|
288
|
-
retryInfo: null
|
|
289
|
-
});
|
|
276
|
+
toolRuns: retainedSettledToolRuns(state.toolRuns)
|
|
277
|
+
}, settleAgentStatusTree(statusTree(state), idleAgentStatus));
|
|
290
278
|
const reduceAgentEvents = (events, initialState = initialAgentClientState, options = {}) => events.reduce((state, event) => applyAgentEventWithOptions(state, event, options), initialState);
|
|
291
279
|
//#endregion
|
|
292
280
|
export { AgentToolRun, appendAgentMessage, applyAgentEvent, applyAgentEventWithOptions, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests };
|