@yolk-sdk/agent 0.0.1-canary.11 → 0.0.1-canary.12

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 CHANGED
@@ -26,10 +26,15 @@ Canary APIs are unstable. Keep all `@yolk-sdk/*` packages on the same version.
26
26
  ## Imports
27
27
 
28
28
  ```ts
29
- import { makeSubagentRunId, UserMessage } from '@yolk-sdk/agent/protocol'
29
+ import {
30
+ hitlResponseEvent,
31
+ makeSubagentRunId,
32
+ questionResponseStructuredContent,
33
+ UserMessage
34
+ } from '@yolk-sdk/agent/protocol'
30
35
  import { run } from '@yolk-sdk/agent/loop'
31
36
  import { runRuntime } from '@yolk-sdk/agent/runtime'
32
- import { initialAgentClientState } from '@yolk-sdk/agent/client'
37
+ import { initialAgentClientState, toolRunsFromHitlRequests } from '@yolk-sdk/agent/client'
33
38
  import {
34
39
  makeNonRecursiveTaskToolModule,
35
40
  makeTaskToolResult,
@@ -87,6 +92,9 @@ HITL is protocol-level, not UI-level:
87
92
  - Resume by passing `hitlResponses` or using client helpers like `submitToolApprovalResponse`.
88
93
  - Denials become model-visible `ToolResult` messages with `isError = true`.
89
94
  - Use `makeQuestionToolModule` to expose the package-owned `question` tool; answers resume as structured tool results and model-visible text with selected labels.
95
+ - Use `questionResponseStructuredContent` / `plainHitlResponse` before storing durable HITL payloads that must be plain JSON.
96
+ - Use `toolRunsFromHitlRequests` to hydrate paused UI state from `AgentAwaitingInput.requests`.
97
+ - Use `hitlResponseEvent` when a client needs optimistic approval/question UI updates before resumed stream events arrive.
90
98
  - Approval is a host-enforced per-call gate for normal tools, not a model-callable permission tool or persisted allow-always system.
91
99
 
92
100
  HTTP client helpers treat `AgentEnd`, `AgentError`, and `AgentAwaitingInput` as logical stream
@@ -1,3 +1,3 @@
1
- import { AgentClientState, AgentRunStatus, AgentToolRun, AgentTranscript, ApplyAgentEventOptions, appendAgentMessage, applyAgentEvent, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage } from "./state.mjs";
1
+ import { AgentClientState, AgentRunStatus, AgentToolRun, AgentTranscript, ApplyAgentEventOptions, appendAgentMessage, applyAgentEvent, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests } from "./state.mjs";
2
2
  import { AgentHttpResponseInfo, AgentTransportError, CancelAgentRunRequest, StreamAgentEventsRequest, StreamAgentRunEventsRequest, StreamCloudflareAgentEventsRequest, SubmitQuestionResponseRequest, SubmitToolApprovalResponseRequest, cancelAgentRun, collectAgentEvents, collectAgentEventsEffect, streamAgentEventStream, streamAgentEvents, streamAgentRunEventStream, streamAgentRunEvents, streamCloudflareAgentEventStream, streamCloudflareAgentEvents, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitQuestionResponse, submitToolApprovalResponse } from "./transport.mjs";
3
- export { type AgentClientState, type AgentHttpResponseInfo, type AgentRunStatus, type AgentToolRun, type AgentTranscript, AgentTransportError, type ApplyAgentEventOptions, type CancelAgentRunRequest, type StreamAgentEventsRequest, type StreamAgentRunEventsRequest, type StreamCloudflareAgentEventsRequest, type SubmitQuestionResponseRequest, type SubmitToolApprovalResponseRequest, appendAgentMessage, applyAgentEvent, cancelAgentRun, collectAgentEvents, collectAgentEventsEffect, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, streamAgentEventStream, streamAgentEvents, streamAgentRunEventStream, streamAgentRunEvents, streamCloudflareAgentEventStream, streamCloudflareAgentEvents, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitAgentUserMessage, submitQuestionResponse, submitToolApprovalResponse };
3
+ export { type AgentClientState, type AgentHttpResponseInfo, type AgentRunStatus, type AgentToolRun, type AgentTranscript, AgentTransportError, type ApplyAgentEventOptions, type CancelAgentRunRequest, type StreamAgentEventsRequest, type StreamAgentRunEventsRequest, type StreamCloudflareAgentEventsRequest, type SubmitQuestionResponseRequest, type SubmitToolApprovalResponseRequest, appendAgentMessage, applyAgentEvent, cancelAgentRun, collectAgentEvents, collectAgentEventsEffect, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, streamAgentEventStream, streamAgentEvents, streamAgentRunEventStream, streamAgentRunEvents, streamCloudflareAgentEventStream, streamCloudflareAgentEvents, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitAgentUserMessage, submitQuestionResponse, submitToolApprovalResponse, toolRunsFromHitlRequests };
@@ -1,3 +1,3 @@
1
- import { appendAgentMessage, applyAgentEvent, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage } from "./state.mjs";
1
+ import { appendAgentMessage, applyAgentEvent, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests } from "./state.mjs";
2
2
  import { AgentTransportError, cancelAgentRun, collectAgentEvents, collectAgentEventsEffect, streamAgentEventStream, streamAgentEvents, streamAgentRunEventStream, streamAgentRunEvents, streamCloudflareAgentEventStream, streamCloudflareAgentEvents, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitQuestionResponse, submitToolApprovalResponse } from "./transport.mjs";
3
- export { AgentTransportError, appendAgentMessage, applyAgentEvent, cancelAgentRun, collectAgentEvents, collectAgentEventsEffect, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, streamAgentEventStream, streamAgentEvents, streamAgentRunEventStream, streamAgentRunEvents, streamCloudflareAgentEventStream, streamCloudflareAgentEvents, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitAgentUserMessage, submitQuestionResponse, submitToolApprovalResponse };
3
+ export { AgentTransportError, appendAgentMessage, applyAgentEvent, cancelAgentRun, collectAgentEvents, collectAgentEventsEffect, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, streamAgentEventStream, streamAgentEvents, streamAgentRunEventStream, streamAgentRunEvents, streamCloudflareAgentEventStream, streamCloudflareAgentEvents, streamQuestionResponseEventStream, streamToolApprovalResponseEventStream, submitAgentUserMessage, submitQuestionResponse, submitToolApprovalResponse, toolRunsFromHitlRequests };
@@ -1,4 +1,4 @@
1
- import { AgentEvent, AgentMessage, QuestionRequest, QuestionResponse, ToolCall, ToolResult, UserMessage } from "@yolk-sdk/agent/protocol";
1
+ import { AgentEvent, AgentMessage, 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';
@@ -13,6 +13,7 @@ type AgentToolRun = {
13
13
  } | {
14
14
  readonly _tag: 'ApprovalRequested';
15
15
  readonly call: ToolCall;
16
+ readonly request?: ToolApprovalRequest;
16
17
  } | {
17
18
  readonly _tag: 'Denied';
18
19
  readonly toolCallId: string;
@@ -23,9 +24,11 @@ type AgentToolRun = {
23
24
  } | {
24
25
  readonly _tag: 'QuestionAnswered';
25
26
  readonly response: QuestionResponse;
27
+ readonly request?: QuestionRequest;
26
28
  } | {
27
29
  readonly _tag: 'QuestionCancelled';
28
30
  readonly response: QuestionResponse;
31
+ readonly request?: QuestionRequest;
29
32
  } | {
30
33
  readonly _tag: 'Executing';
31
34
  readonly call: ToolCall;
@@ -72,6 +75,7 @@ declare const isActiveToolRun: (run: AgentToolRun) => run is {
72
75
  } | {
73
76
  readonly _tag: "ApprovalRequested";
74
77
  readonly call: ToolCall;
78
+ readonly request?: ToolApprovalRequest;
75
79
  } | {
76
80
  readonly _tag: "QuestionRequested";
77
81
  readonly request: QuestionRequest;
@@ -87,6 +91,7 @@ declare const completedToolRuns: (runs: ReadonlyArray<AgentToolRun>) => {
87
91
  readonly startedAtMs: number;
88
92
  readonly endedAtMs: number;
89
93
  }[];
94
+ declare const toolRunsFromHitlRequests: (requests: ReadonlyArray<HitlRequest>) => ReadonlyArray<AgentToolRun>;
90
95
  declare const appendAgentMessage: (messages: ReadonlyArray<AgentMessage>, message: AgentMessage) => AgentTranscript;
91
96
  declare const applyAgentEvent: (state: AgentClientState, event: AgentEvent) => AgentClientState;
92
97
  declare const applyAgentEventWithOptions: (state: AgentClientState, event: AgentEvent, options?: ApplyAgentEventOptions) => AgentClientState;
@@ -95,5 +100,5 @@ declare const markAgentError: (state: AgentClientState, message?: string) => Age
95
100
  declare const markAgentAborted: (state: AgentClientState) => AgentClientState;
96
101
  declare const reduceAgentEvents: (events: ReadonlyArray<AgentEvent>, initialState?: AgentClientState, options?: ApplyAgentEventOptions) => AgentClientState;
97
102
  //#endregion
98
- export { AgentClientState, AgentRunStatus, AgentToolRun, AgentTranscript, ApplyAgentEventOptions, appendAgentMessage, applyAgentEvent, applyAgentEventWithOptions, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage };
103
+ export { AgentClientState, AgentRunStatus, AgentToolRun, AgentTranscript, ApplyAgentEventOptions, appendAgentMessage, applyAgentEvent, applyAgentEventWithOptions, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests };
99
104
  //# sourceMappingURL=state.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.d.mts","names":[],"sources":["../../src/client/state.ts"],"mappings":";;;KAUY,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,SACnC,IAAA;EAAA,SAAoC,IAAA,EAAM,QAAA;AAAA;EAAA,SAC1C,IAAA;EAAA,SAAyB,UAAA;EAAA,SAA6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BAAA;AAAA,cAMhE,0BAAA,GACX,KAAA,EAAO,gBAAA,EACP,KAAA,EAAO,UAAA,EACP,OAAA,GAAS,sBAAA,KACR,gBAAA;AAAA,cAuLU,sBAAA,GACX,KAAA,EAAO,gBAAA,EACP,OAAA,EAAS,WAAA,KACR,gBAAA;AAAA,cAYU,cAAA,GACX,KAAA,EAAO,gBAAA,EACP,OAAA,cACC,gBAKD;AAAA,cAEW,gBAAA,GAAoB,KAAA,EAAO,gBAAA,KAAmB,gBAKzD;AAAA,cAEW,iBAAA,GACX,MAAA,EAAQ,aAAA,CAAc,UAAA,GACtB,YAAA,GAAc,gBAAA,EACd,OAAA,GAAS,sBAAA,KAA2B,gBAAA"}
@@ -31,6 +31,19 @@ const toolRunId = (run) => {
31
31
  };
32
32
  const isActiveToolRun = (run) => run._tag !== "Completed" && run._tag !== "Errored" && run._tag !== "Denied" && run._tag !== "QuestionAnswered" && run._tag !== "QuestionCancelled" && run._tag !== "ProviderCompleted";
33
33
  const completedToolRuns = (runs) => runs.filter((run) => run._tag === "Completed");
34
+ const toolRunsFromHitlRequests = (requests) => requests.map((request) => {
35
+ switch (request._tag) {
36
+ case "QuestionRequest": return {
37
+ _tag: "QuestionRequested",
38
+ request
39
+ };
40
+ case "ToolApprovalRequest": return {
41
+ _tag: "ApprovalRequested",
42
+ call: request.call,
43
+ request
44
+ };
45
+ }
46
+ });
34
47
  const replaceToolRun = (runs, run) => {
35
48
  const id = toolRunId(run);
36
49
  const replaceIndex = runs.findIndex((current) => toolRunId(current) === id);
@@ -46,6 +59,38 @@ const appendToolInputDelta = (runs, id, delta) => runs.map((run) => run._tag ===
46
59
  ...run,
47
60
  input: `${run.input}${delta}`
48
61
  } : run);
62
+ const questionRequestForToolCall = (runs, toolCallId) => runs.flatMap((run) => {
63
+ if (toolRunId(run) !== toolCallId) return [];
64
+ switch (run._tag) {
65
+ case "QuestionRequested": return [run.request];
66
+ case "QuestionAnswered":
67
+ case "QuestionCancelled": return run.request === void 0 ? [] : [run.request];
68
+ case "InputStreaming":
69
+ case "InputReady":
70
+ case "ApprovalRequested":
71
+ case "Denied":
72
+ case "Executing":
73
+ case "Completed":
74
+ case "Errored":
75
+ case "ProviderCompleted": return [];
76
+ }
77
+ })[0];
78
+ const questionAnsweredRun = (response, request) => request === void 0 ? {
79
+ _tag: "QuestionAnswered",
80
+ response
81
+ } : {
82
+ _tag: "QuestionAnswered",
83
+ response,
84
+ request
85
+ };
86
+ const questionCancelledRun = (response, request) => request === void 0 ? {
87
+ _tag: "QuestionCancelled",
88
+ response
89
+ } : {
90
+ _tag: "QuestionCancelled",
91
+ response,
92
+ request
93
+ };
49
94
  const appendAgentMessage = (messages, message) => {
50
95
  const first = messages[0];
51
96
  if (first === void 0) return [message];
@@ -107,7 +152,8 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
107
152
  ...state,
108
153
  toolRuns: replaceToolRun(state.toolRuns, {
109
154
  _tag: "ApprovalRequested",
110
- call: event.call
155
+ call: event.call,
156
+ request: event.request
111
157
  })
112
158
  };
113
159
  case "ToolApprovalGranted": return state;
@@ -128,17 +174,11 @@ const applyAgentEventUnchecked = (state, event, nowMs) => {
128
174
  };
129
175
  case "QuestionAnswered": return {
130
176
  ...state,
131
- toolRuns: replaceToolRun(state.toolRuns, {
132
- _tag: "QuestionAnswered",
133
- response: event.response
134
- })
177
+ toolRuns: replaceToolRun(state.toolRuns, questionAnsweredRun(event.response, questionRequestForToolCall(state.toolRuns, event.response.toolCallId)))
135
178
  };
136
179
  case "QuestionCancelled": return {
137
180
  ...state,
138
- toolRuns: replaceToolRun(state.toolRuns, {
139
- _tag: "QuestionCancelled",
140
- response: event.response
141
- })
181
+ toolRuns: replaceToolRun(state.toolRuns, questionCancelledRun(event.response, questionRequestForToolCall(state.toolRuns, event.response.toolCallId)))
142
182
  };
143
183
  case "ToolExecutionStarted": return {
144
184
  ...state,
@@ -240,6 +280,6 @@ const markAgentAborted = (state) => ({
240
280
  });
241
281
  const reduceAgentEvents = (events, initialState = initialAgentClientState, options = {}) => events.reduce((state, event) => applyAgentEventWithOptions(state, event, options), initialState);
242
282
  //#endregion
243
- export { appendAgentMessage, applyAgentEvent, applyAgentEventWithOptions, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage };
283
+ export { appendAgentMessage, applyAgentEvent, applyAgentEventWithOptions, completedToolRuns, initialAgentClientState, isActiveToolRun, markAgentAborted, markAgentError, reduceAgentEvents, submitAgentUserMessage, toolRunsFromHitlRequests };
244
284
 
245
285
  //# sourceMappingURL=state.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.mjs","names":[],"sources":["../../src/client/state.ts"],"sourcesContent":["import {\n type AgentEvent,\n type AgentMessage,\n type QuestionRequest,\n type QuestionResponse,\n type ToolCall,\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 | { readonly _tag: 'ApprovalRequested'; readonly call: ToolCall }\n | { readonly _tag: 'Denied'; readonly toolCallId: string; readonly reason: string }\n | { readonly _tag: 'QuestionRequested'; readonly request: QuestionRequest }\n | { readonly _tag: 'QuestionAnswered'; readonly response: QuestionResponse }\n | { readonly _tag: 'QuestionCancelled'; readonly response: QuestionResponse }\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\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\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, { _tag: 'ApprovalRequested', call: event.call })\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(state.toolRuns, {\n _tag: 'QuestionAnswered',\n response: event.response\n })\n }\n case 'QuestionCancelled':\n return {\n ...state,\n toolRuns: replaceToolRun(state.toolRuns, {\n _tag: 'QuestionCancelled',\n response: event.response\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":";AA4DA,MAAa,0BAA4C;CACvD,QAAQ;CACR,UAAU,CAAC;CACX,cAAc,CAAC;CACf,MAAM;CACN,WAAW;CACX,UAAU,CAAC;CACX,OAAO;CACP,cAAc,CAAC;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U;IACvC,MAAM;IACN,MAAM,MAAM;IACZ,QAAQ,MAAM;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 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,KA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C1C,OAAO;AACT;AAEA,MAAa,qBACX,QACA,eAAiC,yBACjC,UAAkC,CAAC,MAEnC,OAAO,QAAQ,OAAO,UAAU,2BAA2B,OAAO,OAAO,OAAO,GAAG,YAAY"}
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.mts","names":[],"sources":["../../src/loop/run.ts"],"mappings":";;;;;;;;;KAsEY,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,KAAA,GAAQ,aAAA,CAAc,OAAA;EAAA,SACtB,aAAA,GAAgB,aAAA,CAAc,YAAA;EAAA,SAC9B,KAAA;EAAA,SACA,eAAA,GAAkB,aAAA,CAAc,YAAA;EAAA,SAChC,IAAA;EAAA,SACA,KAAA,GAAQ,UAAA;AAAA;AAAA,cAqhCN,YAAA,GACX,MAAA,EAAQ,eAAA,KACP,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,cAAA,EAAgB,kBAAA,GAAqB,WAAA,GAAc,UAAA;AAAA,cAuBnE,YAAA,GACX,MAAA,EAAQ,eAAA,KACP,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,cAAA,EAAgB,UAAA,GAAa,YAAA;AAAA,cA4C7C,GAAA,GACX,MAAA,EAAQ,SAAA,KACP,MAAA,CAAO,MAAA,CACR,UAAA,EACA,cAAA,EACA,kBAAA,GAAqB,WAAA,GAAc,UAAA,GAAa,YAAA"}
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,KAAA,GAAQ,aAAA,CAAc,OAAA;EAAA,SACtB,aAAA,GAAgB,aAAA,CAAc,YAAA;EAAA,SAC9B,KAAA;EAAA,SACA,eAAA,GAAkB,aAAA,CAAc,YAAA;EAAA,SAChC,IAAA;EAAA,SACA,KAAA,GAAQ,UAAA;AAAA;AAAA,cAqhCN,YAAA,GACX,MAAA,EAAQ,eAAA,KACP,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,cAAA,EAAgB,kBAAA,GAAqB,WAAA,GAAc,UAAA;AAAA,cAuBnE,YAAA,GACX,MAAA,EAAQ,eAAA,KACP,MAAA,CAAO,MAAA,CAAO,UAAA,EAAY,cAAA,EAAgB,UAAA,GAAa,YAAA;AAAA,cA6C7C,GAAA,GACX,MAAA,EAAQ,SAAA,KACP,MAAA,CAAO,MAAA,CACR,UAAA,EACA,cAAA,EACA,kBAAA,GAAqB,WAAA,GAAc,UAAA,GAAa,YAAA"}
package/dist/loop/run.mjs CHANGED
@@ -6,7 +6,7 @@ import { LoopConfig } from "./services/loop-config.mjs";
6
6
  import { ToolExecutor } from "./services/tool-executor.mjs";
7
7
  import { Clock, Effect, Ref, Stream } from "effect";
8
8
  import * as Schema from "effect/Schema";
9
- import { AgentAwaitingInput, AgentEnd, AgentRetry, AgentStart, AssistantMessageEvent, LLMReasoningDelta, LLMStreamEnd, LLMStreamStart, LLMTextDelta, ProviderToolResult, QuestionAnswered, QuestionCancelled, QuestionRequest, QuestionRequested, QuestionToolParams, SubagentCompleted, SubagentStarted, ToolApprovalDenied, ToolApprovalGranted, ToolApprovalRequest, ToolApprovalRequested, ToolExecutionCompleted, ToolExecutionError, ToolExecutionStarted, ToolInputDelta, ToolInputEnd, ToolInputStart, ToolResult, ToolResultMessage, TurnEnd, TurnStart, UsageUpdate, addAgentUsage, assistantHostToolCalls, contentParts, contentPreview, formatQuestionResponseContent, makeSubagentRunId, zeroAgentUsage } from "@yolk-sdk/agent/protocol";
9
+ import { AgentAwaitingInput, AgentEnd, AgentRetry, AgentStart, AssistantMessageEvent, LLMReasoningDelta, LLMStreamEnd, LLMStreamStart, LLMTextDelta, ProviderToolResult, QuestionRequest, QuestionRequested, QuestionToolParams, SubagentCompleted, SubagentStarted, ToolApprovalRequest, ToolApprovalRequested, ToolExecutionCompleted, ToolExecutionError, ToolExecutionStarted, ToolInputDelta, ToolInputEnd, ToolInputStart, ToolResult, ToolResultMessage, TurnEnd, TurnStart, UsageUpdate, addAgentUsage, assistantHostToolCalls, contentParts, contentPreview, formatQuestionResponseContent, hitlResponseEvent, makeSubagentRunId, questionResponseStructuredContent, zeroAgentUsage } from "@yolk-sdk/agent/protocol";
10
10
  //#region src/loop/run.ts
11
11
  const questionToolName = "question";
12
12
  const objectField = (input, key) => input !== null && typeof input === "object" ? Object.getOwnPropertyDescriptor(input, key)?.value : void 0;
@@ -204,13 +204,7 @@ const questionToolResult = (response, questions) => {
204
204
  toolCallId: response.toolCallId,
205
205
  content: formatQuestionResponseContent(response, questions),
206
206
  isError: response.outcome === "cancelled" ? true : void 0,
207
- structuredContent: {
208
- type: "question_response",
209
- outcome: response.outcome,
210
- answers: response.answers ?? [],
211
- reason: response.reason,
212
- source: response.source
213
- }
207
+ structuredContent: questionResponseStructuredContent(response)
214
208
  });
215
209
  };
216
210
  const invalidQuestionToolResult = (call) => ToolResult.make({
@@ -234,7 +228,7 @@ const prepareQuestionCall = (call, index, responses) => Effect.gen(function* ()
234
228
  index,
235
229
  call,
236
230
  result: questionToolResult(response, decoded.success.questions),
237
- events: [response.outcome === "answered" ? QuestionAnswered.make({ response }) : QuestionCancelled.make({ response })]
231
+ events: [hitlResponseEvent(response)]
238
232
  };
239
233
  const request = QuestionRequest.make({
240
234
  requestId: questionRequestId(call),
@@ -270,20 +264,13 @@ const prepareApprovalCall = (tools, call, index, responses) => {
270
264
  index,
271
265
  call,
272
266
  result: deniedToolResult(call, response),
273
- events: [ToolApprovalDenied.make({
274
- toolCallId: call.id,
275
- reason: response.reason ?? "Denied by user",
276
- response
277
- })]
267
+ events: [hitlResponseEvent(response)]
278
268
  };
279
269
  return {
280
270
  _tag: "Execute",
281
271
  index,
282
272
  call,
283
- events: [ToolApprovalGranted.make({
284
- toolCallId: call.id,
285
- response
286
- })]
273
+ events: [hitlResponseEvent(response)]
287
274
  };
288
275
  };
289
276
  const prepareToolCall = (input) => input.call.name === questionToolName ? prepareQuestionCall(input.call, input.index, input.responses) : Effect.succeed(prepareApprovalCall(input.tools, input.call, input.index, input.responses));