veryfront 0.1.224 → 0.1.226
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/esm/deno.js +1 -1
- package/esm/src/agent/ag-ui-browser-response-stream.d.ts +28 -0
- package/esm/src/agent/ag-ui-browser-response-stream.d.ts.map +1 -0
- package/esm/src/agent/ag-ui-browser-response-stream.js +92 -0
- package/esm/src/agent/ag-ui-runtime-handler.d.ts +8 -0
- package/esm/src/agent/ag-ui-runtime-handler.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-runtime-handler.js +70 -6
- package/esm/src/agent/index.d.ts +2 -1
- package/esm/src/agent/index.d.ts.map +1 -1
- package/esm/src/agent/index.js +1 -0
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +1 -1
- package/src/deno.js +1 -1
- package/src/src/agent/ag-ui-browser-response-stream.ts +142 -0
- package/src/src/agent/ag-ui-runtime-handler.ts +111 -4
- package/src/src/agent/index.ts +8 -0
- package/src/src/utils/version-constant.ts +1 -1
package/esm/deno.js
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AgentResponse } from "./types.js";
|
|
2
|
+
import type { AgUiSseEvent } from "./ag-ui-host-support.js";
|
|
3
|
+
export interface AgUiBrowserResponseRequestState {
|
|
4
|
+
runId?: string;
|
|
5
|
+
threadId?: string;
|
|
6
|
+
state?: unknown;
|
|
7
|
+
messages: unknown[];
|
|
8
|
+
}
|
|
9
|
+
export interface AgUiBrowserResponseExecution<TChunk> {
|
|
10
|
+
agentUIStream: AsyncIterable<TChunk>;
|
|
11
|
+
fail: (error: unknown) => Promise<void>;
|
|
12
|
+
waitForFinish: () => Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export interface AgUiBrowserResponseEncoder<TChunk> {
|
|
15
|
+
encode: (chunk: TChunk) => AgUiSseEvent[];
|
|
16
|
+
finalize: (response: AgentResponse | null) => AgUiSseEvent[];
|
|
17
|
+
}
|
|
18
|
+
export interface CreateAgUiBrowserResponseStreamInput<TChunk, TState> {
|
|
19
|
+
agUiInput: AgUiBrowserResponseRequestState;
|
|
20
|
+
agentId: string;
|
|
21
|
+
execution: AgUiBrowserResponseExecution<TChunk>;
|
|
22
|
+
encoder: AgUiBrowserResponseEncoder<TChunk>;
|
|
23
|
+
initialState: TState;
|
|
24
|
+
onChunk?: (state: TState, chunk: TChunk) => void;
|
|
25
|
+
getFinalResponse?: (state: TState) => AgentResponse | null;
|
|
26
|
+
}
|
|
27
|
+
export declare function createAgUiBrowserResponseStream<TChunk, TState>(input: CreateAgUiBrowserResponseStreamInput<TChunk, TState>): ReadableStream<Uint8Array>;
|
|
28
|
+
//# sourceMappingURL=ag-ui-browser-response-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ag-ui-browser-response-stream.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-browser-response-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAkB5D,MAAM,WAAW,+BAA+B;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,OAAO,EAAE,CAAC;CACrB;AAED,MAAM,WAAW,4BAA4B,CAAC,MAAM;IAClD,aAAa,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IACrC,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACxC,aAAa,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,0BAA0B,CAAC,MAAM;IAChD,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,YAAY,EAAE,CAAC;IAC1C,QAAQ,EAAE,CAAC,QAAQ,EAAE,aAAa,GAAG,IAAI,KAAK,YAAY,EAAE,CAAC;CAC9D;AAED,MAAM,WAAW,oCAAoC,CAAC,MAAM,EAAE,MAAM;IAClE,SAAS,EAAE,+BAA+B,CAAC;IAC3C,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,4BAA4B,CAAC,MAAM,CAAC,CAAC;IAChD,OAAO,EAAE,0BAA0B,CAAC,MAAM,CAAC,CAAC;IAC5C,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjD,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,aAAa,GAAG,IAAI,CAAC;CAC5D;AAED,wBAAgB,+BAA+B,CAAC,MAAM,EAAE,MAAM,EAC5D,KAAK,EAAE,oCAAoC,CAAC,MAAM,EAAE,MAAM,CAAC,GAC1D,cAAc,CAAC,UAAU,CAAC,CA4F5B"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
function formatAgUiSseEventWithId(event, eventId) {
|
|
3
|
+
const idLine = eventId === null ? "" : `id: ${eventId}\n`;
|
|
4
|
+
return encoder.encode(`${idLine}event: ${event.event}\ndata: ${JSON.stringify(event.payload)}\n\n`);
|
|
5
|
+
}
|
|
6
|
+
function invokeFailWithoutLeaking(fail, error) {
|
|
7
|
+
return fail(error).catch(() => undefined);
|
|
8
|
+
}
|
|
9
|
+
export function createAgUiBrowserResponseStream(input) {
|
|
10
|
+
let streamClosed = false;
|
|
11
|
+
let nextEventId = 1;
|
|
12
|
+
return new ReadableStream({
|
|
13
|
+
start(controller) {
|
|
14
|
+
const writeEvent = (event) => {
|
|
15
|
+
if (streamClosed) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
controller.enqueue(formatAgUiSseEventWithId(event, nextEventId));
|
|
20
|
+
nextEventId += 1;
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
streamClosed = true;
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const closeStream = () => {
|
|
29
|
+
if (streamClosed) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (controller.desiredSize === null) {
|
|
33
|
+
streamClosed = true;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
controller.close();
|
|
37
|
+
streamClosed = true;
|
|
38
|
+
};
|
|
39
|
+
void (async () => {
|
|
40
|
+
const state = input.initialState;
|
|
41
|
+
try {
|
|
42
|
+
writeEvent({
|
|
43
|
+
event: "RunStarted",
|
|
44
|
+
payload: {
|
|
45
|
+
runId: input.agUiInput.runId,
|
|
46
|
+
threadId: input.agUiInput.threadId,
|
|
47
|
+
agentId: input.agentId,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
writeEvent({
|
|
51
|
+
event: "StateSnapshot",
|
|
52
|
+
payload: {
|
|
53
|
+
snapshot: input.agUiInput.state,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
writeEvent({
|
|
57
|
+
event: "MessagesSnapshot",
|
|
58
|
+
payload: {
|
|
59
|
+
messages: input.agUiInput.messages,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
for await (const chunk of input.execution.agentUIStream) {
|
|
63
|
+
input.onChunk?.(state, chunk);
|
|
64
|
+
for (const event of input.encoder.encode(chunk)) {
|
|
65
|
+
writeEvent(event);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
await input.execution.waitForFinish();
|
|
69
|
+
for (const event of input.encoder.finalize(input.getFinalResponse?.(state) ?? null)) {
|
|
70
|
+
writeEvent(event);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
await invokeFailWithoutLeaking(input.execution.fail, error);
|
|
75
|
+
writeEvent({
|
|
76
|
+
event: "RunError",
|
|
77
|
+
payload: {
|
|
78
|
+
code: "STREAM_ERROR",
|
|
79
|
+
message: error instanceof Error ? error.message : String(error),
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
closeStream();
|
|
85
|
+
}
|
|
86
|
+
})();
|
|
87
|
+
},
|
|
88
|
+
cancel() {
|
|
89
|
+
streamClosed = true;
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -5,6 +5,11 @@ type AgUiResumeValue = {
|
|
|
5
5
|
result: unknown;
|
|
6
6
|
isError: boolean;
|
|
7
7
|
};
|
|
8
|
+
export interface AgUiRuntimeLifecycleContext {
|
|
9
|
+
request: AgUiRuntimeRequest;
|
|
10
|
+
toolCallId?: string;
|
|
11
|
+
error?: unknown;
|
|
12
|
+
}
|
|
8
13
|
export interface AgUiRuntimeHandlerExecuteInput {
|
|
9
14
|
request: Request;
|
|
10
15
|
agUiInput: AgUiRuntimeRequest;
|
|
@@ -16,6 +21,9 @@ export interface AgUiRuntimeHandlerOptions {
|
|
|
16
21
|
context?: Record<string, unknown> | ((request: Request) => Record<string, unknown> | Promise<Record<string, unknown>>);
|
|
17
22
|
sessionManager?: RunResumeSessionManager<AgUiResumeValue>;
|
|
18
23
|
execute?: AgUiRuntimeHandlerExecute;
|
|
24
|
+
onToolCallSeen?: (context: AgUiRuntimeLifecycleContext) => Promise<void> | void;
|
|
25
|
+
onFinish?: (context: AgUiRuntimeLifecycleContext) => Promise<void> | void;
|
|
26
|
+
onError?: (context: AgUiRuntimeLifecycleContext) => Promise<void> | void;
|
|
19
27
|
}
|
|
20
28
|
export interface AgUiRuntimeHandlerConfigWithAgent extends AgUiRuntimeHandlerOptions {
|
|
21
29
|
agent: Agent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ag-ui-runtime-handler.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-runtime-handler.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAEL,KAAK,kBAAkB,EAExB,MAAM,6BAA6B,CAAC;AAgBrC,KAAK,eAAe,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"ag-ui-runtime-handler.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-runtime-handler.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAEL,KAAK,kBAAkB,EAExB,MAAM,6BAA6B,CAAC;AAgBrC,KAAK,eAAe,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAG7D,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,kBAAkB,CAAC;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAqWD,MAAM,WAAW,8BAA8B;IAC7C,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,kBAAkB,CAAC;IAC9B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,qBAAqB,CAAC,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;CACjD;AAED,MAAM,MAAM,yBAAyB,GAAG,CACtC,KAAK,EAAE,8BAA8B,KAClC,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;AAElC,MAAM,WAAW,yBAAyB;IACxC,OAAO,CAAC,EACJ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACvB,CAAC,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACvF,cAAc,CAAC,EAAE,uBAAuB,CAAC,eAAe,CAAC,CAAC;IAC1D,OAAO,CAAC,EAAE,yBAAyB,CAAC;IACpC,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,2BAA2B,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAChF,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,2BAA2B,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC1E,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,2BAA2B,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC1E;AAED,MAAM,WAAW,iCAAkC,SAAQ,yBAAyB;IAClF,KAAK,EAAE,KAAK,CAAC;CACd;AAED,MAAM,MAAM,wBAAwB,GAChC,iCAAiC,GACjC,CAAC,yBAAyB,GAAG;IAAE,KAAK,CAAC,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,yBAAyB,CAAA;CAAE,CAAC,CAAC;AAE5F,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,wBAAwB,GAC/B,CAAC,YAAY,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAqI9C"}
|
|
@@ -12,6 +12,27 @@ const AG_UI_HEADERS = {
|
|
|
12
12
|
"Cache-Control": "no-cache",
|
|
13
13
|
Connection: "keep-alive",
|
|
14
14
|
};
|
|
15
|
+
function invokeLifecycleCallback(callback, context) {
|
|
16
|
+
if (!callback)
|
|
17
|
+
return;
|
|
18
|
+
try {
|
|
19
|
+
const result = callback(context);
|
|
20
|
+
void Promise.resolve(result).catch(() => undefined);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async function invokeLifecycleCallbackAndWait(callback, context) {
|
|
27
|
+
if (!callback)
|
|
28
|
+
return;
|
|
29
|
+
try {
|
|
30
|
+
await callback(context);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
15
36
|
function isRequest(obj) {
|
|
16
37
|
return (typeof obj === "object" &&
|
|
17
38
|
obj !== null &&
|
|
@@ -140,7 +161,7 @@ async function createAgUiRuntimeStreamResponse(options) {
|
|
|
140
161
|
headers: { ...AG_UI_HEADERS },
|
|
141
162
|
});
|
|
142
163
|
}
|
|
143
|
-
async function createAgUiRuntimeDirectStreamResponse(agent, request, baseContext) {
|
|
164
|
+
async function createAgUiRuntimeDirectStreamResponse(agent, request, baseContext, lifecycle) {
|
|
144
165
|
await agent.clearMemory();
|
|
145
166
|
const result = await agent.stream({
|
|
146
167
|
messages: normalizeAgUiRuntimeMessages(request.messages),
|
|
@@ -153,6 +174,9 @@ async function createAgUiRuntimeDirectStreamResponse(agent, request, baseContext
|
|
|
153
174
|
upstreamBody: upstream.body,
|
|
154
175
|
upstreamStatus: upstream.status,
|
|
155
176
|
upstreamStatusText: upstream.statusText,
|
|
177
|
+
onFinish: lifecycle?.onFinish,
|
|
178
|
+
onError: lifecycle?.onError,
|
|
179
|
+
onToolCallSeen: lifecycle?.onToolCallSeen,
|
|
156
180
|
});
|
|
157
181
|
}
|
|
158
182
|
function createInjectedAgUiTool(runId, tool, sessionManager) {
|
|
@@ -199,7 +223,7 @@ function buildMergedTools(agent, request, sessionManager) {
|
|
|
199
223
|
}
|
|
200
224
|
return { ...agent.config.tools, ...injectedTools };
|
|
201
225
|
}
|
|
202
|
-
async function createAgUiRuntimeInjectedToolsStreamResponse(agent, request, baseContext, sessionManager) {
|
|
226
|
+
async function createAgUiRuntimeInjectedToolsStreamResponse(agent, request, baseContext, sessionManager, lifecycle) {
|
|
203
227
|
try {
|
|
204
228
|
sessionManager.startRun({ runId: request.runId, threadId: request.threadId });
|
|
205
229
|
}
|
|
@@ -228,12 +252,15 @@ async function createAgUiRuntimeInjectedToolsStreamResponse(agent, request, base
|
|
|
228
252
|
upstreamStatus: 200,
|
|
229
253
|
onFinish: () => {
|
|
230
254
|
sessionManager.completeRun(request.runId);
|
|
255
|
+
void lifecycle?.onFinish?.();
|
|
231
256
|
},
|
|
232
|
-
onError: () => {
|
|
257
|
+
onError: (error) => {
|
|
233
258
|
sessionManager.failRun(request.runId);
|
|
259
|
+
void lifecycle?.onError?.(error);
|
|
234
260
|
},
|
|
235
261
|
onToolCallSeen: (toolCallId) => {
|
|
236
262
|
sessionManager.prepareForSignal(request.runId, toolCallId);
|
|
263
|
+
void lifecycle?.onToolCallSeen?.(toolCallId);
|
|
237
264
|
},
|
|
238
265
|
});
|
|
239
266
|
}
|
|
@@ -260,16 +287,53 @@ export function createAgUiRuntimeHandler(config) {
|
|
|
260
287
|
}, { status: 501 }))
|
|
261
288
|
: createAgUiRuntimeDirectStreamResponse(config.agent, parsed, context)
|
|
262
289
|
: undefined;
|
|
290
|
+
const invokeLifecycle = (type, extra = {}) => {
|
|
291
|
+
invokeLifecycleCallback(config[type], {
|
|
292
|
+
request: parsed,
|
|
293
|
+
...extra,
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
const createDefaultResponseWithLifecycle = createDefaultResponse
|
|
297
|
+
? async () => {
|
|
298
|
+
try {
|
|
299
|
+
if (!config.agent) {
|
|
300
|
+
return await createDefaultResponse();
|
|
301
|
+
}
|
|
302
|
+
if (parsed.tools.length > 0) {
|
|
303
|
+
if (!config.sessionManager) {
|
|
304
|
+
return await createDefaultResponse();
|
|
305
|
+
}
|
|
306
|
+
return await createAgUiRuntimeInjectedToolsStreamResponse(config.agent, parsed, context, config.sessionManager, {
|
|
307
|
+
onFinish: () => invokeLifecycle("onFinish"),
|
|
308
|
+
onError: (error) => invokeLifecycle("onError", { error }),
|
|
309
|
+
onToolCallSeen: (toolCallId) => invokeLifecycle("onToolCallSeen", { toolCallId }),
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
return await createAgUiRuntimeDirectStreamResponse(config.agent, parsed, context, {
|
|
313
|
+
onFinish: () => invokeLifecycle("onFinish"),
|
|
314
|
+
onError: (error) => invokeLifecycle("onError", { error }),
|
|
315
|
+
onToolCallSeen: (toolCallId) => invokeLifecycle("onToolCallSeen", { toolCallId }),
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
await invokeLifecycleCallbackAndWait(config.onError, {
|
|
320
|
+
request: parsed,
|
|
321
|
+
error,
|
|
322
|
+
});
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
: undefined;
|
|
263
327
|
if (config.execute) {
|
|
264
328
|
return await config.execute({
|
|
265
329
|
request,
|
|
266
330
|
agUiInput: parsed,
|
|
267
331
|
context,
|
|
268
|
-
createDefaultResponse,
|
|
332
|
+
createDefaultResponse: createDefaultResponseWithLifecycle,
|
|
269
333
|
});
|
|
270
334
|
}
|
|
271
|
-
if (
|
|
272
|
-
return await
|
|
335
|
+
if (createDefaultResponseWithLifecycle) {
|
|
336
|
+
return await createDefaultResponseWithLifecycle();
|
|
273
337
|
}
|
|
274
338
|
throw new Error("createAgUiRuntimeHandler configuration became invalid during execution.");
|
|
275
339
|
}
|
package/esm/src/agent/index.d.ts
CHANGED
|
@@ -84,10 +84,11 @@ export { getTextFromParts, getToolArguments, hasArgs, hasInput } from "./types.j
|
|
|
84
84
|
export { BufferMemory, ConversationMemory, createMemory, createRedisMemory, type Memory, type MemoryPersistence, type MemoryStats, type RedisClient, RedisMemory, type RedisMemoryConfig, SummaryMemory, } from "./memory/index.js";
|
|
85
85
|
export { agentAsTool, createWorkflow, getAgent, getAgentsAsTools, getAllAgentIds, registerAgent, type WorkflowConfig, type WorkflowResult, type WorkflowStep, } from "./composition/index.js";
|
|
86
86
|
export { agent } from "./factory.js";
|
|
87
|
-
export { type AgUiRuntimeHandlerConfig, type AgUiRuntimeHandlerConfigWithAgent, type AgUiRuntimeHandlerExecute, type AgUiRuntimeHandlerExecuteInput, type AgUiRuntimeHandlerOptions, createAgUiRuntimeHandler, } from "./ag-ui-runtime-handler.js";
|
|
87
|
+
export { type AgUiRuntimeHandlerConfig, type AgUiRuntimeHandlerConfigWithAgent, type AgUiRuntimeHandlerExecute, type AgUiRuntimeHandlerExecuteInput, type AgUiRuntimeHandlerOptions, type AgUiRuntimeLifecycleContext, createAgUiRuntimeHandler, } from "./ag-ui-runtime-handler.js";
|
|
88
88
|
export { type AgUiRuntimeContextItem, AgUiRuntimeContextItemSchema, type AgUiRuntimeInjectedTool, AgUiRuntimeInjectedToolSchema, type AgUiRuntimeMessage, AgUiRuntimeMessageSchema, type AgUiRuntimeRequest, AgUiRuntimeRequestSchema, parseAgUiRuntimeRequest, parseAgUiRuntimeRequestOrError, } from "./runtime-ag-ui-contract.js";
|
|
89
89
|
export { normalizeAgUiRuntimeMessages } from "./ag-ui-runtime-support.js";
|
|
90
90
|
export { type AgUiBrowserEncodedEvent, type AgUiBrowserEncoderState, type AgUiBrowserRunFinishedMetadata, type AgUiRuntimeStreamEvent, createAgUiBrowserEncoderState, finalizeAgUiBrowserEvents, mapRuntimeStreamEventToAgUiBrowserEvents, } from "./ag-ui-browser-encoder.js";
|
|
91
|
+
export { type AgUiBrowserResponseEncoder, type AgUiBrowserResponseExecution, type AgUiBrowserResponseRequestState, createAgUiBrowserResponseStream, type CreateAgUiBrowserResponseStreamInput, } from "./ag-ui-browser-response-stream.js";
|
|
91
92
|
export { mergeToolCallInput, mergeToolInputDelta, parseDataStreamSseEvents, parseToolInputObject, streamDataStreamEvents, stripLeadingEmptyObjectPlaceholder, } from "./data-stream.js";
|
|
92
93
|
export { expandAllowedRemoteToolNames, getProviderNativeToolNames, type ProviderNativeToolInventoryOptions, } from "./provider-native-tool-inventory.js";
|
|
93
94
|
export { type AgUiDetachedStartAccepted, AgUiDetachedStartAcceptedSchema, type AgUiDetachedStartHandlerOptions, type AgUiDetachedStartRequest, AgUiDetachedStartRequestSchema, createAgUiDetachedStartHandler, executeAgUiDetachedStart, type ExecuteAgUiDetachedStartInput, } from "./ag-ui-detached-start.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/src/agent/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+EG;AACH,OAAO,yBAAyB,CAAC;AAGjC,YAAY,EACV,KAAK,EACL,WAAW,EACX,YAAY,EACZ,eAAe,EACf,aAAa,EACb,WAAW,EACX,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,OAAO,IAAI,YAAY,EACvB,WAAW,EACX,aAAa,EACb,WAAW,EACX,qBAAqB,EACrB,sBAAsB,EACtB,mBAAmB,EACnB,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,oBAAoB,EACpB,qBAAqB,EACrB,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEnF,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,EACjB,KAAK,MAAM,EACX,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,WAAW,EACX,KAAK,iBAAiB,EACtB,aAAa,GACd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,WAAW,EACX,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,cAAc,EACd,aAAa,EACb,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,YAA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K,YAAY,EACjB,uBAAuB,EACvB,0BAA0B,EAC1B,qBAAqB,EACrB,gBAAgB,EAChB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,0BAA0B,EAC/B,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,qBAAqB,EACrB,KAAK,gBAAgB,EACrB,sBAAsB,EACtB,KAAK,wBAAwB,EAC7B,8BAA8B,EAC9B,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,EAC3B,uBAAuB,EACvB,KAAK,gBAAgB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,4BAA4B,EAC5B,iBAAiB,EACjB,KAAK,wBAAwB,GAC9B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,KAAK,uBAAuB,EAC5B,KAAK,8BAA8B,EACnC,KAAK,6BAA6B,EAClC,KAAK,0BAA0B,EAC/B,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,KAAK,8BAA8B,EACnC,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC7B,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/src/agent/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+EG;AACH,OAAO,yBAAyB,CAAC;AAGjC,YAAY,EACV,KAAK,EACL,WAAW,EACX,YAA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ACL,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,qBAAqB,EACrB,KAAK,gBAAgB,EACrB,sBAAsB,EACtB,KAAK,wBAAwB,EAC7B,8BAA8B,EAC9B,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,EAC3B,uBAAuB,EACvB,KAAK,gBAAgB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,4BAA4B,EAC5B,iBAAiB,EACjB,KAAK,wBAAwB,GAC9B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,KAAK,uBAAuB,EAC5B,KAAK,8BAA8B,EACnC,KAAK,6BAA6B,EAClC,KAAK,0BAA0B,EAC/B,KAAK,uBAAuB,EAC5B,KAAK,kBAAkB,EACvB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,KAAK,8BAA8B,EACnC,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC7B,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,oBAAoB,CAAC"}
|
package/esm/src/agent/index.js
CHANGED
|
@@ -87,6 +87,7 @@ export { createAgUiRuntimeHandler, } from "./ag-ui-runtime-handler.js";
|
|
|
87
87
|
export { AgUiRuntimeContextItemSchema, AgUiRuntimeInjectedToolSchema, AgUiRuntimeMessageSchema, AgUiRuntimeRequestSchema, parseAgUiRuntimeRequest, parseAgUiRuntimeRequestOrError, } from "./runtime-ag-ui-contract.js";
|
|
88
88
|
export { normalizeAgUiRuntimeMessages } from "./ag-ui-runtime-support.js";
|
|
89
89
|
export { createAgUiBrowserEncoderState, finalizeAgUiBrowserEvents, mapRuntimeStreamEventToAgUiBrowserEvents, } from "./ag-ui-browser-encoder.js";
|
|
90
|
+
export { createAgUiBrowserResponseStream, } from "./ag-ui-browser-response-stream.js";
|
|
90
91
|
export { mergeToolCallInput, mergeToolInputDelta, parseDataStreamSseEvents, parseToolInputObject, streamDataStreamEvents, stripLeadingEmptyObjectPlaceholder, } from "./data-stream.js";
|
|
91
92
|
export { expandAllowedRemoteToolNames, getProviderNativeToolNames, } from "./provider-native-tool-inventory.js";
|
|
92
93
|
export { AgUiDetachedStartAcceptedSchema, AgUiDetachedStartRequestSchema, createAgUiDetachedStartHandler, executeAgUiDetachedStart, } from "./ag-ui-detached-start.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.226";
|
|
2
2
|
//# sourceMappingURL=version-constant.d.ts.map
|
package/package.json
CHANGED
package/src/deno.js
CHANGED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { AgentResponse } from "./types.js";
|
|
2
|
+
import type { AgUiSseEvent } from "./ag-ui-host-support.js";
|
|
3
|
+
|
|
4
|
+
const encoder = new TextEncoder();
|
|
5
|
+
|
|
6
|
+
function formatAgUiSseEventWithId(event: AgUiSseEvent, eventId: number | null): Uint8Array {
|
|
7
|
+
const idLine = eventId === null ? "" : `id: ${eventId}\n`;
|
|
8
|
+
return encoder.encode(
|
|
9
|
+
`${idLine}event: ${event.event}\ndata: ${JSON.stringify(event.payload)}\n\n`,
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function invokeFailWithoutLeaking(
|
|
14
|
+
fail: (error: unknown) => Promise<void>,
|
|
15
|
+
error: unknown,
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
return fail(error).catch(() => undefined);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AgUiBrowserResponseRequestState {
|
|
21
|
+
runId?: string;
|
|
22
|
+
threadId?: string;
|
|
23
|
+
state?: unknown;
|
|
24
|
+
messages: unknown[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AgUiBrowserResponseExecution<TChunk> {
|
|
28
|
+
agentUIStream: AsyncIterable<TChunk>;
|
|
29
|
+
fail: (error: unknown) => Promise<void>;
|
|
30
|
+
waitForFinish: () => Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AgUiBrowserResponseEncoder<TChunk> {
|
|
34
|
+
encode: (chunk: TChunk) => AgUiSseEvent[];
|
|
35
|
+
finalize: (response: AgentResponse | null) => AgUiSseEvent[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface CreateAgUiBrowserResponseStreamInput<TChunk, TState> {
|
|
39
|
+
agUiInput: AgUiBrowserResponseRequestState;
|
|
40
|
+
agentId: string;
|
|
41
|
+
execution: AgUiBrowserResponseExecution<TChunk>;
|
|
42
|
+
encoder: AgUiBrowserResponseEncoder<TChunk>;
|
|
43
|
+
initialState: TState;
|
|
44
|
+
onChunk?: (state: TState, chunk: TChunk) => void;
|
|
45
|
+
getFinalResponse?: (state: TState) => AgentResponse | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function createAgUiBrowserResponseStream<TChunk, TState>(
|
|
49
|
+
input: CreateAgUiBrowserResponseStreamInput<TChunk, TState>,
|
|
50
|
+
): ReadableStream<Uint8Array> {
|
|
51
|
+
let streamClosed = false;
|
|
52
|
+
let nextEventId = 1;
|
|
53
|
+
|
|
54
|
+
return new ReadableStream<Uint8Array>({
|
|
55
|
+
start(controller) {
|
|
56
|
+
const writeEvent = (event: AgUiSseEvent) => {
|
|
57
|
+
if (streamClosed) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
controller.enqueue(formatAgUiSseEventWithId(event, nextEventId));
|
|
63
|
+
nextEventId += 1;
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
streamClosed = true;
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const closeStream = () => {
|
|
72
|
+
if (streamClosed) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (controller.desiredSize === null) {
|
|
77
|
+
streamClosed = true;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
controller.close();
|
|
82
|
+
streamClosed = true;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
void (async () => {
|
|
86
|
+
const state = input.initialState;
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
writeEvent({
|
|
90
|
+
event: "RunStarted",
|
|
91
|
+
payload: {
|
|
92
|
+
runId: input.agUiInput.runId,
|
|
93
|
+
threadId: input.agUiInput.threadId,
|
|
94
|
+
agentId: input.agentId,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
writeEvent({
|
|
99
|
+
event: "StateSnapshot",
|
|
100
|
+
payload: {
|
|
101
|
+
snapshot: input.agUiInput.state,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
writeEvent({
|
|
106
|
+
event: "MessagesSnapshot",
|
|
107
|
+
payload: {
|
|
108
|
+
messages: input.agUiInput.messages,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
for await (const chunk of input.execution.agentUIStream) {
|
|
113
|
+
input.onChunk?.(state, chunk);
|
|
114
|
+
for (const event of input.encoder.encode(chunk)) {
|
|
115
|
+
writeEvent(event);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
await input.execution.waitForFinish();
|
|
120
|
+
|
|
121
|
+
for (const event of input.encoder.finalize(input.getFinalResponse?.(state) ?? null)) {
|
|
122
|
+
writeEvent(event);
|
|
123
|
+
}
|
|
124
|
+
} catch (error) {
|
|
125
|
+
await invokeFailWithoutLeaking(input.execution.fail, error);
|
|
126
|
+
writeEvent({
|
|
127
|
+
event: "RunError",
|
|
128
|
+
payload: {
|
|
129
|
+
code: "STREAM_ERROR",
|
|
130
|
+
message: error instanceof Error ? error.message : String(error),
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
} finally {
|
|
134
|
+
closeStream();
|
|
135
|
+
}
|
|
136
|
+
})();
|
|
137
|
+
},
|
|
138
|
+
cancel() {
|
|
139
|
+
streamClosed = true;
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
@@ -31,6 +31,39 @@ const AG_UI_HEADERS: Record<string, string> = {
|
|
|
31
31
|
type AgUiResumeValue = { result: unknown; isError: boolean };
|
|
32
32
|
type AgUiRuntimePart = Record<string, unknown> & { type: string };
|
|
33
33
|
|
|
34
|
+
export interface AgUiRuntimeLifecycleContext {
|
|
35
|
+
request: AgUiRuntimeRequest;
|
|
36
|
+
toolCallId?: string;
|
|
37
|
+
error?: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function invokeLifecycleCallback(
|
|
41
|
+
callback: ((context: AgUiRuntimeLifecycleContext) => Promise<void> | void) | undefined,
|
|
42
|
+
context: AgUiRuntimeLifecycleContext,
|
|
43
|
+
): void {
|
|
44
|
+
if (!callback) return;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const result = callback(context);
|
|
48
|
+
void Promise.resolve(result).catch(() => undefined);
|
|
49
|
+
} catch {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function invokeLifecycleCallbackAndWait(
|
|
55
|
+
callback: ((context: AgUiRuntimeLifecycleContext) => Promise<void> | void) | undefined,
|
|
56
|
+
context: AgUiRuntimeLifecycleContext,
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
if (!callback) return;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
await callback(context);
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
34
67
|
function isRequest(obj: unknown): obj is Request {
|
|
35
68
|
return (
|
|
36
69
|
typeof obj === "object" &&
|
|
@@ -212,6 +245,11 @@ async function createAgUiRuntimeDirectStreamResponse(
|
|
|
212
245
|
agent: Agent,
|
|
213
246
|
request: AgUiRuntimeRequest,
|
|
214
247
|
baseContext: Record<string, unknown>,
|
|
248
|
+
lifecycle?: {
|
|
249
|
+
onFinish?: () => Promise<void> | void;
|
|
250
|
+
onError?: (error: unknown) => Promise<void> | void;
|
|
251
|
+
onToolCallSeen?: (toolCallId: string) => Promise<void> | void;
|
|
252
|
+
},
|
|
215
253
|
): Promise<Response> {
|
|
216
254
|
await agent.clearMemory();
|
|
217
255
|
|
|
@@ -227,6 +265,9 @@ async function createAgUiRuntimeDirectStreamResponse(
|
|
|
227
265
|
upstreamBody: upstream.body,
|
|
228
266
|
upstreamStatus: upstream.status,
|
|
229
267
|
upstreamStatusText: upstream.statusText,
|
|
268
|
+
onFinish: lifecycle?.onFinish,
|
|
269
|
+
onError: lifecycle?.onError,
|
|
270
|
+
onToolCallSeen: lifecycle?.onToolCallSeen,
|
|
230
271
|
});
|
|
231
272
|
}
|
|
232
273
|
|
|
@@ -297,6 +338,11 @@ async function createAgUiRuntimeInjectedToolsStreamResponse(
|
|
|
297
338
|
request: AgUiRuntimeRequest,
|
|
298
339
|
baseContext: Record<string, unknown>,
|
|
299
340
|
sessionManager: RunResumeSessionManager<AgUiResumeValue>,
|
|
341
|
+
lifecycle?: {
|
|
342
|
+
onFinish?: () => Promise<void> | void;
|
|
343
|
+
onError?: (error: unknown) => Promise<void> | void;
|
|
344
|
+
onToolCallSeen?: (toolCallId: string) => Promise<void> | void;
|
|
345
|
+
},
|
|
300
346
|
): Promise<Response> {
|
|
301
347
|
try {
|
|
302
348
|
sessionManager.startRun({ runId: request.runId, threadId: request.threadId });
|
|
@@ -333,12 +379,15 @@ async function createAgUiRuntimeInjectedToolsStreamResponse(
|
|
|
333
379
|
upstreamStatus: 200,
|
|
334
380
|
onFinish: () => {
|
|
335
381
|
sessionManager.completeRun(request.runId);
|
|
382
|
+
void lifecycle?.onFinish?.();
|
|
336
383
|
},
|
|
337
|
-
onError: () => {
|
|
384
|
+
onError: (error) => {
|
|
338
385
|
sessionManager.failRun(request.runId);
|
|
386
|
+
void lifecycle?.onError?.(error);
|
|
339
387
|
},
|
|
340
388
|
onToolCallSeen: (toolCallId) => {
|
|
341
389
|
sessionManager.prepareForSignal(request.runId, toolCallId);
|
|
390
|
+
void lifecycle?.onToolCallSeen?.(toolCallId);
|
|
342
391
|
},
|
|
343
392
|
});
|
|
344
393
|
}
|
|
@@ -360,6 +409,9 @@ export interface AgUiRuntimeHandlerOptions {
|
|
|
360
409
|
| ((request: Request) => Record<string, unknown> | Promise<Record<string, unknown>>);
|
|
361
410
|
sessionManager?: RunResumeSessionManager<AgUiResumeValue>;
|
|
362
411
|
execute?: AgUiRuntimeHandlerExecute;
|
|
412
|
+
onToolCallSeen?: (context: AgUiRuntimeLifecycleContext) => Promise<void> | void;
|
|
413
|
+
onFinish?: (context: AgUiRuntimeLifecycleContext) => Promise<void> | void;
|
|
414
|
+
onError?: (context: AgUiRuntimeLifecycleContext) => Promise<void> | void;
|
|
363
415
|
}
|
|
364
416
|
|
|
365
417
|
export interface AgUiRuntimeHandlerConfigWithAgent extends AgUiRuntimeHandlerOptions {
|
|
@@ -414,17 +466,72 @@ export function createAgUiRuntimeHandler(
|
|
|
414
466
|
: createAgUiRuntimeDirectStreamResponse(config.agent, parsed, context)
|
|
415
467
|
: undefined;
|
|
416
468
|
|
|
469
|
+
const invokeLifecycle = (
|
|
470
|
+
type: "onFinish" | "onError" | "onToolCallSeen",
|
|
471
|
+
extra: Partial<AgUiRuntimeLifecycleContext> = {},
|
|
472
|
+
): void => {
|
|
473
|
+
invokeLifecycleCallback(config[type], {
|
|
474
|
+
request: parsed,
|
|
475
|
+
...extra,
|
|
476
|
+
});
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const createDefaultResponseWithLifecycle = createDefaultResponse
|
|
480
|
+
? async () => {
|
|
481
|
+
try {
|
|
482
|
+
if (!config.agent) {
|
|
483
|
+
return await createDefaultResponse();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (parsed.tools.length > 0) {
|
|
487
|
+
if (!config.sessionManager) {
|
|
488
|
+
return await createDefaultResponse();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return await createAgUiRuntimeInjectedToolsStreamResponse(
|
|
492
|
+
config.agent,
|
|
493
|
+
parsed,
|
|
494
|
+
context,
|
|
495
|
+
config.sessionManager,
|
|
496
|
+
{
|
|
497
|
+
onFinish: () => invokeLifecycle("onFinish"),
|
|
498
|
+
onError: (error) => invokeLifecycle("onError", { error }),
|
|
499
|
+
onToolCallSeen: (toolCallId) => invokeLifecycle("onToolCallSeen", { toolCallId }),
|
|
500
|
+
},
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return await createAgUiRuntimeDirectStreamResponse(
|
|
505
|
+
config.agent,
|
|
506
|
+
parsed,
|
|
507
|
+
context,
|
|
508
|
+
{
|
|
509
|
+
onFinish: () => invokeLifecycle("onFinish"),
|
|
510
|
+
onError: (error) => invokeLifecycle("onError", { error }),
|
|
511
|
+
onToolCallSeen: (toolCallId) => invokeLifecycle("onToolCallSeen", { toolCallId }),
|
|
512
|
+
},
|
|
513
|
+
);
|
|
514
|
+
} catch (error) {
|
|
515
|
+
await invokeLifecycleCallbackAndWait(config.onError, {
|
|
516
|
+
request: parsed,
|
|
517
|
+
error,
|
|
518
|
+
});
|
|
519
|
+
throw error;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
: undefined;
|
|
523
|
+
|
|
417
524
|
if (config.execute) {
|
|
418
525
|
return await config.execute({
|
|
419
526
|
request,
|
|
420
527
|
agUiInput: parsed,
|
|
421
528
|
context,
|
|
422
|
-
createDefaultResponse,
|
|
529
|
+
createDefaultResponse: createDefaultResponseWithLifecycle,
|
|
423
530
|
});
|
|
424
531
|
}
|
|
425
532
|
|
|
426
|
-
if (
|
|
427
|
-
return await
|
|
533
|
+
if (createDefaultResponseWithLifecycle) {
|
|
534
|
+
return await createDefaultResponseWithLifecycle();
|
|
428
535
|
}
|
|
429
536
|
|
|
430
537
|
throw new Error("createAgUiRuntimeHandler configuration became invalid during execution.");
|
package/src/src/agent/index.ts
CHANGED
|
@@ -147,6 +147,7 @@ export {
|
|
|
147
147
|
type AgUiRuntimeHandlerExecute,
|
|
148
148
|
type AgUiRuntimeHandlerExecuteInput,
|
|
149
149
|
type AgUiRuntimeHandlerOptions,
|
|
150
|
+
type AgUiRuntimeLifecycleContext,
|
|
150
151
|
createAgUiRuntimeHandler,
|
|
151
152
|
} from "./ag-ui-runtime-handler.js";
|
|
152
153
|
export {
|
|
@@ -171,6 +172,13 @@ export {
|
|
|
171
172
|
finalizeAgUiBrowserEvents,
|
|
172
173
|
mapRuntimeStreamEventToAgUiBrowserEvents,
|
|
173
174
|
} from "./ag-ui-browser-encoder.js";
|
|
175
|
+
export {
|
|
176
|
+
type AgUiBrowserResponseEncoder,
|
|
177
|
+
type AgUiBrowserResponseExecution,
|
|
178
|
+
type AgUiBrowserResponseRequestState,
|
|
179
|
+
createAgUiBrowserResponseStream,
|
|
180
|
+
type CreateAgUiBrowserResponseStreamInput,
|
|
181
|
+
} from "./ag-ui-browser-response-stream.js";
|
|
174
182
|
export {
|
|
175
183
|
mergeToolCallInput,
|
|
176
184
|
mergeToolInputDelta,
|