bs-agent 0.0.28 → 0.0.29
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 +81 -55
- package/dist/react/index.cjs +149 -179
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.cts +27 -33
- package/dist/react/index.d.ts +27 -33
- package/dist/react/index.js +150 -179
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
package/dist/react/index.d.cts
CHANGED
|
@@ -1,35 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { m as ToolType, I as ImagePart, F as FilePart, B as BuildShipAgent, b as AgentInput } from '../agent-D9xuE8wR.cjs';
|
|
2
2
|
export { d as ContentPart, j as TextPart } from '../agent-D9xuE8wR.cjs';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
4
|
import * as react from 'react';
|
|
5
5
|
import { ReactNode } from 'react';
|
|
6
6
|
import 'zod';
|
|
7
7
|
|
|
8
|
-
type ToolExecutionItem = {
|
|
9
|
-
itemType: "tool_call";
|
|
10
|
-
toolName: string;
|
|
11
|
-
callId: string;
|
|
12
|
-
toolType: ToolType;
|
|
13
|
-
status: "progress" | "complete" | "error";
|
|
14
|
-
inputs?: unknown;
|
|
15
|
-
output?: unknown;
|
|
16
|
-
error?: string;
|
|
17
|
-
serverName?: string;
|
|
18
|
-
};
|
|
19
|
-
type ReasoningItem = {
|
|
20
|
-
itemType: "reasoning";
|
|
21
|
-
reasoning: string;
|
|
22
|
-
index?: number;
|
|
23
|
-
};
|
|
24
|
-
type HandoffItem = {
|
|
25
|
-
itemType: "handoff";
|
|
26
|
-
agentName: string;
|
|
27
|
-
};
|
|
28
|
-
type RunErrorItem = {
|
|
29
|
-
itemType: "run_error";
|
|
30
|
-
message: string;
|
|
31
|
-
code?: string;
|
|
32
|
-
};
|
|
33
8
|
type WidgetExecutionItem = {
|
|
34
9
|
toolName: string;
|
|
35
10
|
callId: string;
|
|
@@ -41,7 +16,6 @@ type ClientToolDefinition = {
|
|
|
41
16
|
parameters: unknown;
|
|
42
17
|
await?: boolean;
|
|
43
18
|
};
|
|
44
|
-
type DebugDataType = Array<ToolExecutionItem | ReasoningItem | HandoffItem | RunErrorItem>;
|
|
45
19
|
type MessagePart = {
|
|
46
20
|
type: "text";
|
|
47
21
|
text: string;
|
|
@@ -58,6 +32,31 @@ type MessagePart = {
|
|
|
58
32
|
status?: "pending" | "submitted";
|
|
59
33
|
/** Persisted result from a tool submission (handler or widget submit). */
|
|
60
34
|
result?: any;
|
|
35
|
+
} | {
|
|
36
|
+
type: "tool_call";
|
|
37
|
+
toolName: string;
|
|
38
|
+
callId: string;
|
|
39
|
+
toolType: ToolType;
|
|
40
|
+
status: "progress" | "complete" | "error";
|
|
41
|
+
inputs?: unknown;
|
|
42
|
+
output?: unknown;
|
|
43
|
+
error?: string;
|
|
44
|
+
serverName?: string;
|
|
45
|
+
sequence: number;
|
|
46
|
+
} | {
|
|
47
|
+
type: "reasoning";
|
|
48
|
+
reasoning: string;
|
|
49
|
+
index?: number;
|
|
50
|
+
sequence: number;
|
|
51
|
+
} | {
|
|
52
|
+
type: "handoff";
|
|
53
|
+
agentName: string;
|
|
54
|
+
sequence: number;
|
|
55
|
+
} | {
|
|
56
|
+
type: "run_error";
|
|
57
|
+
message: string;
|
|
58
|
+
code?: string;
|
|
59
|
+
sequence: number;
|
|
61
60
|
};
|
|
62
61
|
type Message = {
|
|
63
62
|
role: "user" | "agent";
|
|
@@ -111,7 +110,6 @@ declare function useAgent(agent: BuildShipAgent, options?: UseAgentOptions): {
|
|
|
111
110
|
switchSession: (sessionId?: string) => void;
|
|
112
111
|
deleteSession: (sessionId: string) => void;
|
|
113
112
|
sessions: Session[];
|
|
114
|
-
debugData: Record<string, DebugDataType>;
|
|
115
113
|
};
|
|
116
114
|
|
|
117
115
|
/**
|
|
@@ -233,7 +231,6 @@ interface AgentRunner {
|
|
|
233
231
|
inProgress: boolean;
|
|
234
232
|
sessionId: string;
|
|
235
233
|
sessions: Session[];
|
|
236
|
-
debugData: Record<string, DebugDataType>;
|
|
237
234
|
handleSend: (input: AgentInput, options?: {
|
|
238
235
|
context?: Record<string, unknown>;
|
|
239
236
|
skipUserMessage?: boolean;
|
|
@@ -261,12 +258,9 @@ declare function useAgentContext(agentId: string, agentUrl: string, accessKey?:
|
|
|
261
258
|
declare function useAgentGlobalState(): {
|
|
262
259
|
allSessions: Record<string, Record<string, Session>>;
|
|
263
260
|
setAllSessions: (value: Record<string, Record<string, Session>> | ((prev: Record<string, Record<string, Session>>) => Record<string, Record<string, Session>>)) => void;
|
|
264
|
-
debugData: Record<string, DebugDataType>;
|
|
265
|
-
setDebugData: (value: Record<string, DebugDataType> | ((prev: Record<string, DebugDataType>) => Record<string, DebugDataType>)) => void;
|
|
266
261
|
};
|
|
267
262
|
|
|
268
263
|
declare const AGENT_SESSIONS_KEY = "buildship:agent:conversations";
|
|
269
|
-
declare const AGENT_DEBUG_DATA_KEY = "buildship:agent:debug";
|
|
270
264
|
declare const DEFAULT_SESSION_NAME = "New Chat";
|
|
271
265
|
declare const TEMPORARY_SESSION_ID = "sess_temp";
|
|
272
266
|
|
|
@@ -288,4 +282,4 @@ declare function tryParseJSON(value: unknown): any;
|
|
|
288
282
|
*/
|
|
289
283
|
declare function updateAgentMessageParts(parts: MessagePart[], newPart: MessagePart): MessagePart[];
|
|
290
284
|
|
|
291
|
-
export {
|
|
285
|
+
export { AGENT_SESSIONS_KEY, AgentContextProvider, AgentInput, type AgentRunner, AgentToolContext, type AgentToolContextValue, type ClientToolConfig, type ClientToolDefinition, type ClientToolRenderProps, DEFAULT_SESSION_NAME, FilePart, ImagePart, type Message, type MessagePart, type Session, TEMPORARY_SESSION_ID, type ToolConfig, ToolRenderer, ToolType, type WidgetExecutionItem, cleanSchema, tryParseJSON, updateAgentMessageParts, useAgent, useAgentContext, useAgentGlobalState, useClientTool };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,35 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { m as ToolType, I as ImagePart, F as FilePart, B as BuildShipAgent, b as AgentInput } from '../agent-D9xuE8wR.js';
|
|
2
2
|
export { d as ContentPart, j as TextPart } from '../agent-D9xuE8wR.js';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
4
|
import * as react from 'react';
|
|
5
5
|
import { ReactNode } from 'react';
|
|
6
6
|
import 'zod';
|
|
7
7
|
|
|
8
|
-
type ToolExecutionItem = {
|
|
9
|
-
itemType: "tool_call";
|
|
10
|
-
toolName: string;
|
|
11
|
-
callId: string;
|
|
12
|
-
toolType: ToolType;
|
|
13
|
-
status: "progress" | "complete" | "error";
|
|
14
|
-
inputs?: unknown;
|
|
15
|
-
output?: unknown;
|
|
16
|
-
error?: string;
|
|
17
|
-
serverName?: string;
|
|
18
|
-
};
|
|
19
|
-
type ReasoningItem = {
|
|
20
|
-
itemType: "reasoning";
|
|
21
|
-
reasoning: string;
|
|
22
|
-
index?: number;
|
|
23
|
-
};
|
|
24
|
-
type HandoffItem = {
|
|
25
|
-
itemType: "handoff";
|
|
26
|
-
agentName: string;
|
|
27
|
-
};
|
|
28
|
-
type RunErrorItem = {
|
|
29
|
-
itemType: "run_error";
|
|
30
|
-
message: string;
|
|
31
|
-
code?: string;
|
|
32
|
-
};
|
|
33
8
|
type WidgetExecutionItem = {
|
|
34
9
|
toolName: string;
|
|
35
10
|
callId: string;
|
|
@@ -41,7 +16,6 @@ type ClientToolDefinition = {
|
|
|
41
16
|
parameters: unknown;
|
|
42
17
|
await?: boolean;
|
|
43
18
|
};
|
|
44
|
-
type DebugDataType = Array<ToolExecutionItem | ReasoningItem | HandoffItem | RunErrorItem>;
|
|
45
19
|
type MessagePart = {
|
|
46
20
|
type: "text";
|
|
47
21
|
text: string;
|
|
@@ -58,6 +32,31 @@ type MessagePart = {
|
|
|
58
32
|
status?: "pending" | "submitted";
|
|
59
33
|
/** Persisted result from a tool submission (handler or widget submit). */
|
|
60
34
|
result?: any;
|
|
35
|
+
} | {
|
|
36
|
+
type: "tool_call";
|
|
37
|
+
toolName: string;
|
|
38
|
+
callId: string;
|
|
39
|
+
toolType: ToolType;
|
|
40
|
+
status: "progress" | "complete" | "error";
|
|
41
|
+
inputs?: unknown;
|
|
42
|
+
output?: unknown;
|
|
43
|
+
error?: string;
|
|
44
|
+
serverName?: string;
|
|
45
|
+
sequence: number;
|
|
46
|
+
} | {
|
|
47
|
+
type: "reasoning";
|
|
48
|
+
reasoning: string;
|
|
49
|
+
index?: number;
|
|
50
|
+
sequence: number;
|
|
51
|
+
} | {
|
|
52
|
+
type: "handoff";
|
|
53
|
+
agentName: string;
|
|
54
|
+
sequence: number;
|
|
55
|
+
} | {
|
|
56
|
+
type: "run_error";
|
|
57
|
+
message: string;
|
|
58
|
+
code?: string;
|
|
59
|
+
sequence: number;
|
|
61
60
|
};
|
|
62
61
|
type Message = {
|
|
63
62
|
role: "user" | "agent";
|
|
@@ -111,7 +110,6 @@ declare function useAgent(agent: BuildShipAgent, options?: UseAgentOptions): {
|
|
|
111
110
|
switchSession: (sessionId?: string) => void;
|
|
112
111
|
deleteSession: (sessionId: string) => void;
|
|
113
112
|
sessions: Session[];
|
|
114
|
-
debugData: Record<string, DebugDataType>;
|
|
115
113
|
};
|
|
116
114
|
|
|
117
115
|
/**
|
|
@@ -233,7 +231,6 @@ interface AgentRunner {
|
|
|
233
231
|
inProgress: boolean;
|
|
234
232
|
sessionId: string;
|
|
235
233
|
sessions: Session[];
|
|
236
|
-
debugData: Record<string, DebugDataType>;
|
|
237
234
|
handleSend: (input: AgentInput, options?: {
|
|
238
235
|
context?: Record<string, unknown>;
|
|
239
236
|
skipUserMessage?: boolean;
|
|
@@ -261,12 +258,9 @@ declare function useAgentContext(agentId: string, agentUrl: string, accessKey?:
|
|
|
261
258
|
declare function useAgentGlobalState(): {
|
|
262
259
|
allSessions: Record<string, Record<string, Session>>;
|
|
263
260
|
setAllSessions: (value: Record<string, Record<string, Session>> | ((prev: Record<string, Record<string, Session>>) => Record<string, Record<string, Session>>)) => void;
|
|
264
|
-
debugData: Record<string, DebugDataType>;
|
|
265
|
-
setDebugData: (value: Record<string, DebugDataType> | ((prev: Record<string, DebugDataType>) => Record<string, DebugDataType>)) => void;
|
|
266
261
|
};
|
|
267
262
|
|
|
268
263
|
declare const AGENT_SESSIONS_KEY = "buildship:agent:conversations";
|
|
269
|
-
declare const AGENT_DEBUG_DATA_KEY = "buildship:agent:debug";
|
|
270
264
|
declare const DEFAULT_SESSION_NAME = "New Chat";
|
|
271
265
|
declare const TEMPORARY_SESSION_ID = "sess_temp";
|
|
272
266
|
|
|
@@ -288,4 +282,4 @@ declare function tryParseJSON(value: unknown): any;
|
|
|
288
282
|
*/
|
|
289
283
|
declare function updateAgentMessageParts(parts: MessagePart[], newPart: MessagePart): MessagePart[];
|
|
290
284
|
|
|
291
|
-
export {
|
|
285
|
+
export { AGENT_SESSIONS_KEY, AgentContextProvider, AgentInput, type AgentRunner, AgentToolContext, type AgentToolContextValue, type ClientToolConfig, type ClientToolDefinition, type ClientToolRenderProps, DEFAULT_SESSION_NAME, FilePart, ImagePart, type Message, type MessagePart, type Session, TEMPORARY_SESSION_ID, type ToolConfig, ToolRenderer, ToolType, type WidgetExecutionItem, cleanSchema, tryParseJSON, updateAgentMessageParts, useAgent, useAgentContext, useAgentGlobalState, useClientTool };
|
package/dist/react/index.js
CHANGED
|
@@ -10,11 +10,10 @@ import {
|
|
|
10
10
|
} from "react";
|
|
11
11
|
|
|
12
12
|
// src/react/use-agent.ts
|
|
13
|
-
import { useCallback as useCallback3, useRef as useRef2, useState, useEffect, useContext as useContext2
|
|
13
|
+
import { useCallback as useCallback3, useRef as useRef2, useState, useEffect, useContext as useContext2 } from "react";
|
|
14
14
|
|
|
15
15
|
// src/react/constants.ts
|
|
16
16
|
var AGENT_SESSIONS_KEY = "buildship:agent:conversations";
|
|
17
|
-
var AGENT_DEBUG_DATA_KEY = "buildship:agent:debug";
|
|
18
17
|
var DEFAULT_SESSION_NAME = "New Chat";
|
|
19
18
|
var TEMPORARY_SESSION_ID = "sess_temp";
|
|
20
19
|
|
|
@@ -120,122 +119,6 @@ var useSessionUtils = (agentId, allSessions, setAllSessions, currentSessionId, s
|
|
|
120
119
|
};
|
|
121
120
|
};
|
|
122
121
|
|
|
123
|
-
// src/react/debug-handlers.ts
|
|
124
|
-
var createDebugHandlers = (setDebugData) => {
|
|
125
|
-
const handleStreamEvent = (event) => {
|
|
126
|
-
const executionId = event.meta.executionId;
|
|
127
|
-
switch (event.type) {
|
|
128
|
-
case "tool_call_start": {
|
|
129
|
-
const { callId, toolName, toolType, inputs, serverName } = event.data;
|
|
130
|
-
setDebugData((prev) => ({
|
|
131
|
-
...prev,
|
|
132
|
-
[executionId]: [
|
|
133
|
-
...prev[executionId] || [],
|
|
134
|
-
{
|
|
135
|
-
itemType: "tool_call",
|
|
136
|
-
toolName,
|
|
137
|
-
callId,
|
|
138
|
-
toolType,
|
|
139
|
-
status: "progress",
|
|
140
|
-
inputs,
|
|
141
|
-
serverName
|
|
142
|
-
}
|
|
143
|
-
]
|
|
144
|
-
}));
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
case "tool_call_end": {
|
|
148
|
-
const { callId, result, error } = event.data;
|
|
149
|
-
setDebugData((prev) => {
|
|
150
|
-
const currentData = [...prev[executionId] || []];
|
|
151
|
-
for (let i = currentData.length - 1; i >= 0; i--) {
|
|
152
|
-
if (currentData[i].itemType === "tool_call") {
|
|
153
|
-
const toolItem = currentData[i];
|
|
154
|
-
if (toolItem.callId === callId) {
|
|
155
|
-
currentData[i] = {
|
|
156
|
-
...toolItem,
|
|
157
|
-
status: error ? "error" : "complete",
|
|
158
|
-
output: result,
|
|
159
|
-
error
|
|
160
|
-
};
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return {
|
|
166
|
-
...prev,
|
|
167
|
-
[executionId]: currentData
|
|
168
|
-
};
|
|
169
|
-
});
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
case "reasoning_delta": {
|
|
173
|
-
const { delta, index } = event.data;
|
|
174
|
-
setDebugData((prev) => {
|
|
175
|
-
const currentData = [...prev[executionId] || []];
|
|
176
|
-
let existingItemIndex = -1;
|
|
177
|
-
for (let i = currentData.length - 1; i >= 0; i--) {
|
|
178
|
-
const item = currentData[i];
|
|
179
|
-
if (item.itemType === "reasoning" && item.index === index) {
|
|
180
|
-
existingItemIndex = i;
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
if (existingItemIndex === -1) {
|
|
185
|
-
currentData.push({ itemType: "reasoning", reasoning: delta, index });
|
|
186
|
-
} else {
|
|
187
|
-
const hasInterleavedItems = currentData.slice(existingItemIndex + 1).some((item) => item.itemType !== "reasoning");
|
|
188
|
-
if (hasInterleavedItems) {
|
|
189
|
-
currentData.push({ itemType: "reasoning", reasoning: delta, index });
|
|
190
|
-
} else {
|
|
191
|
-
currentData[existingItemIndex] = {
|
|
192
|
-
itemType: "reasoning",
|
|
193
|
-
reasoning: currentData[existingItemIndex].reasoning + delta,
|
|
194
|
-
index
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return {
|
|
199
|
-
...prev,
|
|
200
|
-
[executionId]: currentData
|
|
201
|
-
};
|
|
202
|
-
});
|
|
203
|
-
break;
|
|
204
|
-
}
|
|
205
|
-
case "agent_handoff": {
|
|
206
|
-
setDebugData((prev) => ({
|
|
207
|
-
...prev,
|
|
208
|
-
[executionId]: [
|
|
209
|
-
...prev[executionId] || [],
|
|
210
|
-
{
|
|
211
|
-
itemType: "handoff",
|
|
212
|
-
agentName: event.data.agentName
|
|
213
|
-
}
|
|
214
|
-
]
|
|
215
|
-
}));
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
case "run_error": {
|
|
219
|
-
setDebugData((prev) => ({
|
|
220
|
-
...prev,
|
|
221
|
-
[executionId]: [
|
|
222
|
-
...prev[executionId] || [],
|
|
223
|
-
{
|
|
224
|
-
itemType: "run_error",
|
|
225
|
-
message: event.data.message,
|
|
226
|
-
code: event.data.code
|
|
227
|
-
}
|
|
228
|
-
]
|
|
229
|
-
}));
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
return {
|
|
235
|
-
handleStreamEvent
|
|
236
|
-
};
|
|
237
|
-
};
|
|
238
|
-
|
|
239
122
|
// src/react/client-tools.ts
|
|
240
123
|
import { useCallback as useCallback2 } from "react";
|
|
241
124
|
import { toJSONSchema } from "zod";
|
|
@@ -340,7 +223,7 @@ function updateAgentMessageParts(parts, newPart) {
|
|
|
340
223
|
}
|
|
341
224
|
|
|
342
225
|
// src/react/stream-callbacks.ts
|
|
343
|
-
function buildStreamCallbacks(deps
|
|
226
|
+
function buildStreamCallbacks(deps) {
|
|
344
227
|
const {
|
|
345
228
|
setMessages,
|
|
346
229
|
setInProgress,
|
|
@@ -355,16 +238,10 @@ function buildStreamCallbacks(deps, debugKey) {
|
|
|
355
238
|
onComplete: () => {
|
|
356
239
|
console.log("Agent closed");
|
|
357
240
|
setInProgress(false);
|
|
358
|
-
if (syncSessionRef.current) {
|
|
359
|
-
syncSessionRef.current(messagesRef.current);
|
|
360
|
-
}
|
|
361
241
|
},
|
|
362
242
|
onError: (error) => {
|
|
363
243
|
console.log("Agent error", error);
|
|
364
244
|
setInProgress(false);
|
|
365
|
-
if (syncSessionRef.current) {
|
|
366
|
-
syncSessionRef.current(messagesRef.current);
|
|
367
|
-
}
|
|
368
245
|
},
|
|
369
246
|
onEvent: (event) => {
|
|
370
247
|
if (event.type === "text_delta") {
|
|
@@ -378,33 +255,36 @@ function buildStreamCallbacks(deps, debugKey) {
|
|
|
378
255
|
);
|
|
379
256
|
} else if (event.type === "tool_call_start" && event.data.toolType === "client") {
|
|
380
257
|
handleClientToolCall(event, setMessages, syncSessionRef, toolContext, agentId);
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
callId: event.data.callId,
|
|
392
|
-
toolName: event.data.toolName,
|
|
393
|
-
toolType: event.data.toolType
|
|
394
|
-
},
|
|
395
|
-
meta: debugMeta
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
} else if (debugKey) {
|
|
400
|
-
deps.debugHandlers.handleStreamEvent({
|
|
401
|
-
...event,
|
|
402
|
-
meta: { ...event.meta, executionId: debugKey }
|
|
403
|
-
});
|
|
258
|
+
} else if (event.type === "tool_call_start") {
|
|
259
|
+
handleToolCallStart(event, setMessages, syncSessionRef);
|
|
260
|
+
} else if (event.type === "tool_call_end") {
|
|
261
|
+
handleToolCallEnd(event, setMessages, syncSessionRef);
|
|
262
|
+
} else if (event.type === "reasoning_delta") {
|
|
263
|
+
handleReasoningDelta(event, setMessages, syncSessionRef);
|
|
264
|
+
} else if (event.type === "agent_handoff") {
|
|
265
|
+
handleAgentHandoff(event, setMessages, syncSessionRef);
|
|
266
|
+
} else if (event.type === "run_error") {
|
|
267
|
+
handleRunError(event, setMessages, syncSessionRef);
|
|
404
268
|
}
|
|
405
269
|
}
|
|
406
270
|
};
|
|
407
271
|
}
|
|
272
|
+
function ensureAgentMessage(prev, executionId) {
|
|
273
|
+
const last = prev[prev.length - 1];
|
|
274
|
+
if (last?.role === "agent") {
|
|
275
|
+
return [prev, last];
|
|
276
|
+
}
|
|
277
|
+
const newMsg = {
|
|
278
|
+
role: "agent",
|
|
279
|
+
content: "",
|
|
280
|
+
parts: [],
|
|
281
|
+
executionId
|
|
282
|
+
};
|
|
283
|
+
return [[...prev, newMsg], newMsg];
|
|
284
|
+
}
|
|
285
|
+
function replaceLastAgent(prev, updated) {
|
|
286
|
+
return [...prev.slice(0, -1), updated];
|
|
287
|
+
}
|
|
408
288
|
function handleTextDelta(event, setMessages, syncSessionRef, modifier, fullTextModifier, agentId) {
|
|
409
289
|
const sequence = event.meta.sequence;
|
|
410
290
|
const originalText = event.data;
|
|
@@ -525,11 +405,126 @@ function handleClientToolCall(event, setMessages, syncSessionRef, toolContext, a
|
|
|
525
405
|
return updatedMessages;
|
|
526
406
|
});
|
|
527
407
|
}
|
|
408
|
+
function handleToolCallStart(event, setMessages, syncSessionRef) {
|
|
409
|
+
const { callId, toolName, toolType, inputs, serverName } = event.data;
|
|
410
|
+
setMessages((prev) => {
|
|
411
|
+
const [msgs, agentMsg] = ensureAgentMessage(prev, event.meta.executionId);
|
|
412
|
+
const newPart = {
|
|
413
|
+
type: "tool_call",
|
|
414
|
+
toolName,
|
|
415
|
+
callId,
|
|
416
|
+
toolType,
|
|
417
|
+
status: "progress",
|
|
418
|
+
inputs,
|
|
419
|
+
serverName,
|
|
420
|
+
sequence: event.meta.sequence
|
|
421
|
+
};
|
|
422
|
+
const updated = {
|
|
423
|
+
...agentMsg,
|
|
424
|
+
parts: [...agentMsg.parts || [], newPart]
|
|
425
|
+
};
|
|
426
|
+
const updatedMessages = replaceLastAgent(msgs, updated);
|
|
427
|
+
if (syncSessionRef.current) syncSessionRef.current(updatedMessages);
|
|
428
|
+
return updatedMessages;
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
function handleToolCallEnd(event, setMessages, syncSessionRef) {
|
|
432
|
+
const { callId, result, error } = event.data;
|
|
433
|
+
setMessages((prev) => {
|
|
434
|
+
const last = prev[prev.length - 1];
|
|
435
|
+
if (last?.role !== "agent" || !last.parts) return prev;
|
|
436
|
+
const updatedParts = [...last.parts];
|
|
437
|
+
for (let i = updatedParts.length - 1; i >= 0; i--) {
|
|
438
|
+
const part = updatedParts[i];
|
|
439
|
+
if (part.type === "tool_call" && part.callId === callId) {
|
|
440
|
+
updatedParts[i] = {
|
|
441
|
+
...part,
|
|
442
|
+
status: error ? "error" : "complete",
|
|
443
|
+
output: result,
|
|
444
|
+
error
|
|
445
|
+
};
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const updated = { ...last, parts: updatedParts };
|
|
450
|
+
const updatedMessages = replaceLastAgent(prev, updated);
|
|
451
|
+
if (syncSessionRef.current) syncSessionRef.current(updatedMessages);
|
|
452
|
+
return updatedMessages;
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
function handleReasoningDelta(event, setMessages, syncSessionRef) {
|
|
456
|
+
const { delta, index } = event.data;
|
|
457
|
+
setMessages((prev) => {
|
|
458
|
+
const [msgs, agentMsg] = ensureAgentMessage(prev, event.meta.executionId);
|
|
459
|
+
const parts = [...agentMsg.parts || []];
|
|
460
|
+
let existingIdx = -1;
|
|
461
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
462
|
+
const p = parts[i];
|
|
463
|
+
if (p.type === "reasoning" && p.index === index) {
|
|
464
|
+
existingIdx = i;
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (existingIdx === -1) {
|
|
469
|
+
parts.push({ type: "reasoning", reasoning: delta, index, sequence: event.meta.sequence });
|
|
470
|
+
} else {
|
|
471
|
+
const hasInterleavedItems = parts.slice(existingIdx + 1).some((p) => p.type !== "reasoning");
|
|
472
|
+
if (hasInterleavedItems) {
|
|
473
|
+
parts.push({ type: "reasoning", reasoning: delta, index, sequence: event.meta.sequence });
|
|
474
|
+
} else {
|
|
475
|
+
const existing = parts[existingIdx];
|
|
476
|
+
parts[existingIdx] = {
|
|
477
|
+
...existing,
|
|
478
|
+
reasoning: existing.reasoning + delta
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const updated = { ...agentMsg, parts };
|
|
483
|
+
const updatedMessages = replaceLastAgent(msgs, updated);
|
|
484
|
+
if (syncSessionRef.current) syncSessionRef.current(updatedMessages);
|
|
485
|
+
return updatedMessages;
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
function handleAgentHandoff(event, setMessages, syncSessionRef) {
|
|
489
|
+
setMessages((prev) => {
|
|
490
|
+
const [msgs, agentMsg] = ensureAgentMessage(prev, event.meta.executionId);
|
|
491
|
+
const newPart = {
|
|
492
|
+
type: "handoff",
|
|
493
|
+
agentName: event.data.agentName,
|
|
494
|
+
sequence: event.meta.sequence
|
|
495
|
+
};
|
|
496
|
+
const updated = {
|
|
497
|
+
...agentMsg,
|
|
498
|
+
parts: [...agentMsg.parts || [], newPart]
|
|
499
|
+
};
|
|
500
|
+
const updatedMessages = replaceLastAgent(msgs, updated);
|
|
501
|
+
if (syncSessionRef.current) syncSessionRef.current(updatedMessages);
|
|
502
|
+
return updatedMessages;
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
function handleRunError(event, setMessages, syncSessionRef) {
|
|
506
|
+
setMessages((prev) => {
|
|
507
|
+
const [msgs, agentMsg] = ensureAgentMessage(prev, event.meta.executionId);
|
|
508
|
+
const newPart = {
|
|
509
|
+
type: "run_error",
|
|
510
|
+
message: event.data.message,
|
|
511
|
+
code: event.data.code,
|
|
512
|
+
sequence: event.meta.sequence
|
|
513
|
+
};
|
|
514
|
+
const updated = {
|
|
515
|
+
...agentMsg,
|
|
516
|
+
parts: [...agentMsg.parts || [], newPart]
|
|
517
|
+
};
|
|
518
|
+
const updatedMessages = replaceLastAgent(msgs, updated);
|
|
519
|
+
if (syncSessionRef.current) syncSessionRef.current(updatedMessages);
|
|
520
|
+
return updatedMessages;
|
|
521
|
+
});
|
|
522
|
+
}
|
|
528
523
|
|
|
529
524
|
// src/react/use-agent.ts
|
|
530
525
|
function useAgent(agent, options) {
|
|
531
526
|
const agentId = agent._agentId;
|
|
532
|
-
const { allSessions, setAllSessions
|
|
527
|
+
const { allSessions, setAllSessions } = useAgentGlobalState();
|
|
533
528
|
const toolContext = useContext2(AgentToolContext);
|
|
534
529
|
const [inProgress, setInProgress] = useState(false);
|
|
535
530
|
const [messages, setMessages] = useState([]);
|
|
@@ -547,7 +542,6 @@ function useAgent(agent, options) {
|
|
|
547
542
|
const initialSessionId = sessionUtils.getInitialSessionId();
|
|
548
543
|
setCurrentSessionId(initialSessionId);
|
|
549
544
|
}, [agentId]);
|
|
550
|
-
const debugHandlers = useMemo2(() => createDebugHandlers(setDebugData), [setDebugData]);
|
|
551
545
|
useEffect(() => {
|
|
552
546
|
messagesRef.current = messages;
|
|
553
547
|
}, [messages]);
|
|
@@ -580,7 +574,6 @@ function useAgent(agent, options) {
|
|
|
580
574
|
async (input, runOptions) => {
|
|
581
575
|
const isNewSession = !currentSessionId || currentSessionId === TEMPORARY_SESSION_ID;
|
|
582
576
|
setInProgress(true);
|
|
583
|
-
const debugKey = runOptions?.optimisticExecutionId || messagesRef.current.findLast((m) => m.role === "user")?.executionId;
|
|
584
577
|
const deps = {
|
|
585
578
|
agentId,
|
|
586
579
|
currentSessionId,
|
|
@@ -588,12 +581,11 @@ function useAgent(agent, options) {
|
|
|
588
581
|
setMessages,
|
|
589
582
|
setInProgress,
|
|
590
583
|
syncSessionRef: sessionUtils.syncSessionRef,
|
|
591
|
-
debugHandlers,
|
|
592
584
|
toolContext,
|
|
593
585
|
textDeltaModifier: optionsRef.current?.textDeltaModifier,
|
|
594
586
|
fullTextModifier: optionsRef.current?.fullTextModifier
|
|
595
587
|
};
|
|
596
|
-
const callbacks = buildStreamCallbacks(deps
|
|
588
|
+
const callbacks = buildStreamCallbacks(deps);
|
|
597
589
|
const executeOptions = {
|
|
598
590
|
context: runOptions?.context,
|
|
599
591
|
headers: runOptions?.additionalHeaders,
|
|
@@ -655,7 +647,7 @@ function useAgent(agent, options) {
|
|
|
655
647
|
activeSessionRef.current = null;
|
|
656
648
|
}
|
|
657
649
|
},
|
|
658
|
-
[currentSessionId, sessionUtils,
|
|
650
|
+
[currentSessionId, sessionUtils, agentId, toolContext, agent, getClientToolsMap]
|
|
659
651
|
);
|
|
660
652
|
const handleSend = useCallback3(
|
|
661
653
|
async (input, options2) => {
|
|
@@ -754,14 +746,6 @@ function useAgent(agent, options) {
|
|
|
754
746
|
return updatedMessages;
|
|
755
747
|
});
|
|
756
748
|
const lastUserMessage = messagesRef.current.findLast((m) => m.role === "user");
|
|
757
|
-
const debugKey = lastUserMessage?.executionId;
|
|
758
|
-
if (debugKey) {
|
|
759
|
-
debugHandlers.handleStreamEvent({
|
|
760
|
-
type: "tool_call_end",
|
|
761
|
-
data: { callId, toolName: "", toolType: "client", result },
|
|
762
|
-
meta: { executionId: debugKey, sequence: 0 }
|
|
763
|
-
});
|
|
764
|
-
}
|
|
765
749
|
await runAgent(void 0, {
|
|
766
750
|
resumeToolCallId: callId,
|
|
767
751
|
resumeToolResult: result,
|
|
@@ -770,7 +754,7 @@ function useAgent(agent, options) {
|
|
|
770
754
|
context: lastUserMessage?.context ?? lastRunOptionsRef.current.context
|
|
771
755
|
});
|
|
772
756
|
},
|
|
773
|
-
[runAgent, sessionUtils.syncSessionRef
|
|
757
|
+
[runAgent, sessionUtils.syncSessionRef]
|
|
774
758
|
);
|
|
775
759
|
const addOptimisticMessage = useCallback3(
|
|
776
760
|
(input) => {
|
|
@@ -826,8 +810,7 @@ function useAgent(agent, options) {
|
|
|
826
810
|
sessionId: currentSessionId,
|
|
827
811
|
switchSession: sessionUtils.switchSession,
|
|
828
812
|
deleteSession: sessionUtils.deleteSession,
|
|
829
|
-
sessions: sessionUtils.sessionsList
|
|
830
|
-
debugData
|
|
813
|
+
sessions: sessionUtils.sessionsList
|
|
831
814
|
};
|
|
832
815
|
}
|
|
833
816
|
|
|
@@ -1435,10 +1418,6 @@ function AgentContextProvider({ children }) {
|
|
|
1435
1418
|
const toolRegistryRef = useRef3(/* @__PURE__ */ new Map());
|
|
1436
1419
|
const [, forceUpdate] = useState3({});
|
|
1437
1420
|
const [allSessions, setAllSessions] = useSyncedLocalStorage(AGENT_SESSIONS_KEY, {});
|
|
1438
|
-
const [debugData, setDebugData] = useSyncedLocalStorage(
|
|
1439
|
-
AGENT_DEBUG_DATA_KEY,
|
|
1440
|
-
{}
|
|
1441
|
-
);
|
|
1442
1421
|
const initializeAgent = useCallback5(
|
|
1443
1422
|
(agentId, agentUrl, accessKey, options) => {
|
|
1444
1423
|
const existing = activeAgentsRef.current.get(agentId);
|
|
@@ -1495,8 +1474,6 @@ function AgentContextProvider({ children }) {
|
|
|
1495
1474
|
getRunner,
|
|
1496
1475
|
allSessions,
|
|
1497
1476
|
setAllSessions,
|
|
1498
|
-
debugData,
|
|
1499
|
-
setDebugData,
|
|
1500
1477
|
runnersRef,
|
|
1501
1478
|
listenersRef
|
|
1502
1479
|
}),
|
|
@@ -1505,9 +1482,7 @@ function AgentContextProvider({ children }) {
|
|
|
1505
1482
|
registerRunner,
|
|
1506
1483
|
getRunner,
|
|
1507
1484
|
allSessions,
|
|
1508
|
-
setAllSessions
|
|
1509
|
-
debugData,
|
|
1510
|
-
setDebugData
|
|
1485
|
+
setAllSessions
|
|
1511
1486
|
]
|
|
1512
1487
|
);
|
|
1513
1488
|
const toolContextValue = useMemo3(
|
|
@@ -1592,7 +1567,6 @@ function useAgentContext(agentId, agentUrl, accessKey, options) {
|
|
|
1592
1567
|
inProgress: false,
|
|
1593
1568
|
sessionId: "",
|
|
1594
1569
|
sessions: [],
|
|
1595
|
-
debugData: {},
|
|
1596
1570
|
handleSend: async () => {
|
|
1597
1571
|
},
|
|
1598
1572
|
resumeTool: async () => {
|
|
@@ -1617,9 +1591,7 @@ function useAgentGlobalState() {
|
|
|
1617
1591
|
}
|
|
1618
1592
|
return {
|
|
1619
1593
|
allSessions: context.allSessions,
|
|
1620
|
-
setAllSessions: context.setAllSessions
|
|
1621
|
-
debugData: context.debugData,
|
|
1622
|
-
setDebugData: context.setDebugData
|
|
1594
|
+
setAllSessions: context.setAllSessions
|
|
1623
1595
|
};
|
|
1624
1596
|
}
|
|
1625
1597
|
|
|
@@ -1667,7 +1639,6 @@ function ToolRenderer({ agentId, part }) {
|
|
|
1667
1639
|
});
|
|
1668
1640
|
}
|
|
1669
1641
|
export {
|
|
1670
|
-
AGENT_DEBUG_DATA_KEY,
|
|
1671
1642
|
AGENT_SESSIONS_KEY,
|
|
1672
1643
|
AgentContextProvider,
|
|
1673
1644
|
AgentToolContext,
|