@rudderjs/ai 1.13.0 → 1.15.0
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/dist/agent-sse.d.ts +149 -0
- package/dist/agent-sse.d.ts.map +1 -0
- package/dist/agent-sse.js +280 -0
- package/dist/agent-sse.js.map +1 -0
- package/dist/agent.d.ts +16 -1
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +4 -1
- package/dist/agent.js.map +1 -1
- package/dist/conversation-orm/index.d.ts.map +1 -1
- package/dist/conversation-orm/index.js +5 -1
- package/dist/conversation-orm/index.js.map +1 -1
- package/dist/gateway/http-gateway-adapter.d.ts +94 -0
- package/dist/gateway/http-gateway-adapter.d.ts.map +1 -0
- package/dist/gateway/http-gateway-adapter.js +106 -0
- package/dist/gateway/http-gateway-adapter.js.map +1 -0
- package/dist/gateway/index.d.ts +11 -0
- package/dist/gateway/index.d.ts.map +1 -0
- package/dist/gateway/index.js +11 -0
- package/dist/gateway/index.js.map +1 -0
- package/dist/gateway/sse.d.ts +28 -0
- package/dist/gateway/sse.d.ts.map +1 -0
- package/dist/gateway/sse.js +78 -0
- package/dist/gateway/sse.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/sanitize-conversation.d.ts +43 -0
- package/dist/sanitize-conversation.d.ts.map +1 -0
- package/dist/sanitize-conversation.js +85 -0
- package/dist/sanitize-conversation.js.map +1 -0
- package/package.json +7 -3
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named-event SSE protocol for streaming an agent loop to a browser.
|
|
3
|
+
*
|
|
4
|
+
* `@rudderjs/ai` already ships the Vercel AI SDK data-stream protocol
|
|
5
|
+
* ({@link toVercelDataStream}) - the numeric-prefix wire (`0:` / `9:` / `a:`
|
|
6
|
+
* ...). This is the alternative for apps that want a plain
|
|
7
|
+
* `text/event-stream` with self-describing event names that mirror the agent
|
|
8
|
+
* loop's own lifecycle (`text`, `tool_call`, `tool_update`, `tool_result`,
|
|
9
|
+
* `pending_client_tools`, `tool_approval_required`, `handoff`, `complete`,
|
|
10
|
+
* `error`).
|
|
11
|
+
*
|
|
12
|
+
* Both ends live here so the wire vocabulary can never drift:
|
|
13
|
+
*
|
|
14
|
+
* - Server: {@link toAgentSseStream} / {@link toAgentSseResponse} project an
|
|
15
|
+
* `agent.stream()` result onto the named events and frame them as SSE.
|
|
16
|
+
* - Browser: {@link readAgentStream} decodes the same events back into an
|
|
17
|
+
* {@link AgentStreamTurn} and fires per-event callbacks for UI side effects.
|
|
18
|
+
* {@link applyAgentSseEvent} is exposed so the per-event reducer can be
|
|
19
|
+
* unit-tested against a synthetic turn.
|
|
20
|
+
*
|
|
21
|
+
* Runtime-agnostic: uses only web globals (`ReadableStream`, `Response`,
|
|
22
|
+
* `TextEncoder` / `TextDecoder`, `crypto.randomUUID`), no `node:` imports, so
|
|
23
|
+
* the module is safe in the main entry and runs server-side (Node / edge) and
|
|
24
|
+
* client-side alike.
|
|
25
|
+
*
|
|
26
|
+
* This ships the framework-generic core. App-specific events (conversation
|
|
27
|
+
* ids, billing, sub-run fan-out bookkeeping, server-authoritative history
|
|
28
|
+
* sync) are not part of it - emit and decode those alongside this protocol on
|
|
29
|
+
* your own channel.
|
|
30
|
+
*/
|
|
31
|
+
import type { AgentStreamResponse, AiMessage, FinishReason, TokenUsage, ToolCall } from './types.js';
|
|
32
|
+
/** The named SSE events this protocol emits, in agent-loop order. */
|
|
33
|
+
export type AgentSseEventName = 'text' | 'tool_call' | 'tool_update' | 'tool_result' | 'pending_client_tools' | 'tool_approval_required' | 'handoff' | 'complete' | 'error';
|
|
34
|
+
/** What the loop parked on when it paused, surfaced on the `complete` event. */
|
|
35
|
+
export type AgentAwaiting = 'client_tools' | 'approval';
|
|
36
|
+
export interface AgentSseTextPayload {
|
|
37
|
+
text: string;
|
|
38
|
+
}
|
|
39
|
+
export interface AgentSseToolCallPayload {
|
|
40
|
+
id?: string;
|
|
41
|
+
tool: string;
|
|
42
|
+
input?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
export interface AgentSseToolUpdatePayload {
|
|
45
|
+
id?: string;
|
|
46
|
+
tool?: string;
|
|
47
|
+
update: unknown;
|
|
48
|
+
}
|
|
49
|
+
export interface AgentSseToolResultPayload {
|
|
50
|
+
id?: string;
|
|
51
|
+
toolCallId?: string;
|
|
52
|
+
tool?: string;
|
|
53
|
+
/** String passthrough when the tool returned a string, else JSON-encoded. */
|
|
54
|
+
content: string;
|
|
55
|
+
}
|
|
56
|
+
export interface AgentSsePendingClientToolsPayload {
|
|
57
|
+
toolCalls: ToolCall[];
|
|
58
|
+
}
|
|
59
|
+
export interface AgentSseApprovalPayload {
|
|
60
|
+
toolCall: ToolCall;
|
|
61
|
+
isClientTool: boolean;
|
|
62
|
+
}
|
|
63
|
+
export interface AgentSseHandoffPayload {
|
|
64
|
+
from: string;
|
|
65
|
+
to: string;
|
|
66
|
+
message?: string;
|
|
67
|
+
}
|
|
68
|
+
export interface AgentSseCompletePayload {
|
|
69
|
+
done: true;
|
|
70
|
+
finishReason?: FinishReason;
|
|
71
|
+
awaiting?: AgentAwaiting;
|
|
72
|
+
/** Number of model steps the run took (`response.steps.length`). */
|
|
73
|
+
steps?: number;
|
|
74
|
+
usage?: TokenUsage;
|
|
75
|
+
}
|
|
76
|
+
export interface AgentSseErrorPayload {
|
|
77
|
+
message: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Project an `agent.stream()` result onto the named-event SSE wire as a
|
|
81
|
+
* `ReadableStream<Uint8Array>`.
|
|
82
|
+
*
|
|
83
|
+
* Iterates the chunk stream, emits one named event per loop chunk, then
|
|
84
|
+
* awaits the `response` promise and emits a terminal `complete` event
|
|
85
|
+
* carrying `done`, `finishReason`, the `awaiting` pause (if any), step count,
|
|
86
|
+
* and usage. If iteration or the response throws, an `error` event is emitted
|
|
87
|
+
* and the stream closes cleanly so the browser reader's `onError` fires.
|
|
88
|
+
*/
|
|
89
|
+
export declare function toAgentSseStream(streaming: AgentStreamResponse): ReadableStream<Uint8Array>;
|
|
90
|
+
/**
|
|
91
|
+
* Wrap {@link toAgentSseStream} in a `Response` with the standard
|
|
92
|
+
* `text/event-stream` headers (no caching, no proxy buffering). Return it
|
|
93
|
+
* directly from a route handler.
|
|
94
|
+
*/
|
|
95
|
+
export declare function toAgentSseResponse(streaming: AgentStreamResponse, init?: ResponseInit): Response;
|
|
96
|
+
/**
|
|
97
|
+
* The accumulated state of one streamed agent turn, built up event by event
|
|
98
|
+
* by {@link readAgentStream}. Mirrors {@link AgentResponse} fields the browser
|
|
99
|
+
* needs to render the turn and build the next continuation request.
|
|
100
|
+
*/
|
|
101
|
+
export interface AgentStreamTurn {
|
|
102
|
+
/** Concatenated `text` event deltas. */
|
|
103
|
+
assistantText: string;
|
|
104
|
+
/** Tool calls stamped by `tool_call` events (for the next continuation). */
|
|
105
|
+
assistantToolCalls: ToolCall[];
|
|
106
|
+
/** Server-side `role:'tool'` result messages from `tool_result` events. */
|
|
107
|
+
serverToolResults: AiMessage[];
|
|
108
|
+
/** Client tool calls from a `pending_client_tools` event to run locally. */
|
|
109
|
+
pendingClientTools: ToolCall[];
|
|
110
|
+
/** Approval pause from a `tool_approval_required` event. */
|
|
111
|
+
pendingApproval: AgentSseApprovalPayload | null;
|
|
112
|
+
/** Chain of agent class names traversed via `handoff` events. */
|
|
113
|
+
handoffPath: string[];
|
|
114
|
+
/** `true` once a `complete` event with `done: true` arrived. */
|
|
115
|
+
done: boolean;
|
|
116
|
+
/** What the run paused on, from the `complete` event. */
|
|
117
|
+
awaiting: AgentAwaiting | undefined;
|
|
118
|
+
}
|
|
119
|
+
/** A fresh, empty {@link AgentStreamTurn}. */
|
|
120
|
+
export declare function newAgentStreamTurn(): AgentStreamTurn;
|
|
121
|
+
/** Per-event callbacks fired by {@link readAgentStream} for UI side effects. */
|
|
122
|
+
export interface AgentStreamCallbacks {
|
|
123
|
+
onText?: (text: string) => void;
|
|
124
|
+
onToolCall?: (call: AgentSseToolCallPayload) => void;
|
|
125
|
+
onToolUpdate?: (update: AgentSseToolUpdatePayload) => void;
|
|
126
|
+
onToolResult?: (result: AgentSseToolResultPayload) => void;
|
|
127
|
+
onPendingClientTools?: (toolCalls: ToolCall[]) => void;
|
|
128
|
+
onToolApprovalRequired?: (approval: AgentSseApprovalPayload) => void;
|
|
129
|
+
onHandoff?: (handoff: AgentSseHandoffPayload) => void;
|
|
130
|
+
onComplete?: (data: AgentSseCompletePayload) => void;
|
|
131
|
+
onError?: (error: AgentSseErrorPayload) => void;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Read a named-event agent-SSE response body, applying each event to a fresh
|
|
135
|
+
* {@link AgentStreamTurn} and firing the matching callback. Resolves with the
|
|
136
|
+
* accumulated turn once the stream closes.
|
|
137
|
+
*
|
|
138
|
+
* The caller owns the `fetch` and the `!resp.ok` branch (so a rich error body
|
|
139
|
+
* can be read for non-2xx responses); pass an already-OK response. A missing
|
|
140
|
+
* body resolves to an empty turn. Malformed event JSON is skipped.
|
|
141
|
+
*/
|
|
142
|
+
export declare function readAgentStream(resp: Response, callbacks?: AgentStreamCallbacks): Promise<AgentStreamTurn>;
|
|
143
|
+
/**
|
|
144
|
+
* Apply a single parsed SSE event to the turn state and fire the matching
|
|
145
|
+
* callback. Mutates `turn`; otherwise pure. Exposed for unit-testing the
|
|
146
|
+
* reducer against a synthetic turn without a live stream.
|
|
147
|
+
*/
|
|
148
|
+
export declare function applyAgentSseEvent(event: string, data: unknown, turn: AgentStreamTurn, callbacks?: AgentStreamCallbacks): void;
|
|
149
|
+
//# sourceMappingURL=agent-sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-sse.d.ts","sourceRoot":"","sources":["../src/agent-sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;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YAAY,CAAC,EAAY,CAAC,MAAM,EAAE,yBAAyB,KAAK,IAAI,CAAA;IACpE,oBAAoB,CAAC,EAAI,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,IAAI,CAAA;IACxD,sBAAsB,CAAC,EAAE,CAAC,QAAQ,EAAE,uBAAuB,KAAK,IAAI,CAAA;IACpE,SAAS,CAAC,EAAe,CAAC,OAAO,EAAE,sBAAsB,KAAK,IAAI,CAAA;IAClE,UAAU,CAAC,EAAc,CAAC,IAAI,EAAE,uBAAuB,KAAK,IAAI,CAAA;IAChE,OAAO,CAAC,EAAiB,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,CAAA;CAC/D;AAQD;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAQ,QAAQ,EACpB,SAAS,GAAG,oBAAyB,GACpC,OAAO,CAAC,eAAe,CAAC,CAgC1B;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAM,MAAM,EACjB,IAAI,EAAO,OAAO,EAClB,IAAI,EAAO,eAAe,EAC1B,SAAS,GAAE,oBAAyB,GACnC,IAAI,CA+DN"}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named-event SSE protocol for streaming an agent loop to a browser.
|
|
3
|
+
*
|
|
4
|
+
* `@rudderjs/ai` already ships the Vercel AI SDK data-stream protocol
|
|
5
|
+
* ({@link toVercelDataStream}) - the numeric-prefix wire (`0:` / `9:` / `a:`
|
|
6
|
+
* ...). This is the alternative for apps that want a plain
|
|
7
|
+
* `text/event-stream` with self-describing event names that mirror the agent
|
|
8
|
+
* loop's own lifecycle (`text`, `tool_call`, `tool_update`, `tool_result`,
|
|
9
|
+
* `pending_client_tools`, `tool_approval_required`, `handoff`, `complete`,
|
|
10
|
+
* `error`).
|
|
11
|
+
*
|
|
12
|
+
* Both ends live here so the wire vocabulary can never drift:
|
|
13
|
+
*
|
|
14
|
+
* - Server: {@link toAgentSseStream} / {@link toAgentSseResponse} project an
|
|
15
|
+
* `agent.stream()` result onto the named events and frame them as SSE.
|
|
16
|
+
* - Browser: {@link readAgentStream} decodes the same events back into an
|
|
17
|
+
* {@link AgentStreamTurn} and fires per-event callbacks for UI side effects.
|
|
18
|
+
* {@link applyAgentSseEvent} is exposed so the per-event reducer can be
|
|
19
|
+
* unit-tested against a synthetic turn.
|
|
20
|
+
*
|
|
21
|
+
* Runtime-agnostic: uses only web globals (`ReadableStream`, `Response`,
|
|
22
|
+
* `TextEncoder` / `TextDecoder`, `crypto.randomUUID`), no `node:` imports, so
|
|
23
|
+
* the module is safe in the main entry and runs server-side (Node / edge) and
|
|
24
|
+
* client-side alike.
|
|
25
|
+
*
|
|
26
|
+
* This ships the framework-generic core. App-specific events (conversation
|
|
27
|
+
* ids, billing, sub-run fan-out bookkeeping, server-authoritative history
|
|
28
|
+
* sync) are not part of it - emit and decode those alongside this protocol on
|
|
29
|
+
* your own channel.
|
|
30
|
+
*/
|
|
31
|
+
// ─── Server: project a stream onto SSE ────────────────────
|
|
32
|
+
/** Map a finish reason to the pause it represents, if any. */
|
|
33
|
+
function awaitingFor(reason) {
|
|
34
|
+
if (reason === 'client_tool_calls')
|
|
35
|
+
return 'client_tools';
|
|
36
|
+
if (reason === 'tool_approval_required')
|
|
37
|
+
return 'approval';
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
function frame(event, data) {
|
|
41
|
+
return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Project an `agent.stream()` result onto the named-event SSE wire as a
|
|
45
|
+
* `ReadableStream<Uint8Array>`.
|
|
46
|
+
*
|
|
47
|
+
* Iterates the chunk stream, emits one named event per loop chunk, then
|
|
48
|
+
* awaits the `response` promise and emits a terminal `complete` event
|
|
49
|
+
* carrying `done`, `finishReason`, the `awaiting` pause (if any), step count,
|
|
50
|
+
* and usage. If iteration or the response throws, an `error` event is emitted
|
|
51
|
+
* and the stream closes cleanly so the browser reader's `onError` fires.
|
|
52
|
+
*/
|
|
53
|
+
export function toAgentSseStream(streaming) {
|
|
54
|
+
const { stream, response } = streaming;
|
|
55
|
+
const encoder = new TextEncoder();
|
|
56
|
+
return new ReadableStream({
|
|
57
|
+
async start(controller) {
|
|
58
|
+
const emit = (event, data) => controller.enqueue(encoder.encode(frame(event, data)));
|
|
59
|
+
try {
|
|
60
|
+
for await (const chunk of stream) {
|
|
61
|
+
emitChunk(chunk, emit);
|
|
62
|
+
}
|
|
63
|
+
const res = await response;
|
|
64
|
+
const awaiting = awaitingFor(res.finishReason);
|
|
65
|
+
const complete = {
|
|
66
|
+
done: true,
|
|
67
|
+
steps: res.steps.length,
|
|
68
|
+
usage: res.usage,
|
|
69
|
+
};
|
|
70
|
+
if (res.finishReason)
|
|
71
|
+
complete.finishReason = res.finishReason;
|
|
72
|
+
if (awaiting)
|
|
73
|
+
complete.awaiting = awaiting;
|
|
74
|
+
emit('complete', complete);
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
78
|
+
emit('error', { message });
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
controller.close();
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function emitChunk(chunk, emit) {
|
|
87
|
+
switch (chunk.type) {
|
|
88
|
+
case 'text-delta':
|
|
89
|
+
if (chunk.text)
|
|
90
|
+
emit('text', { text: chunk.text });
|
|
91
|
+
return;
|
|
92
|
+
case 'tool-call':
|
|
93
|
+
emit('tool_call', {
|
|
94
|
+
...(chunk.toolCall?.id ? { id: chunk.toolCall.id } : {}),
|
|
95
|
+
tool: chunk.toolCall?.name ?? '',
|
|
96
|
+
input: chunk.toolCall?.arguments ?? {},
|
|
97
|
+
});
|
|
98
|
+
return;
|
|
99
|
+
case 'tool-update':
|
|
100
|
+
emit('tool_update', {
|
|
101
|
+
...(chunk.toolCall?.id ? { id: chunk.toolCall.id } : {}),
|
|
102
|
+
...(chunk.toolCall?.name ? { tool: chunk.toolCall.name } : {}),
|
|
103
|
+
update: chunk.update,
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
case 'tool-result': {
|
|
107
|
+
const id = chunk.toolCall?.id;
|
|
108
|
+
emit('tool_result', {
|
|
109
|
+
...(id ? { id, toolCallId: id } : {}),
|
|
110
|
+
...(chunk.toolCall?.name ? { tool: chunk.toolCall.name } : {}),
|
|
111
|
+
content: typeof chunk.result === 'string' ? chunk.result : JSON.stringify(chunk.result),
|
|
112
|
+
});
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
case 'pending-client-tools':
|
|
116
|
+
emit('pending_client_tools', { toolCalls: chunk.toolCalls ?? [] });
|
|
117
|
+
return;
|
|
118
|
+
case 'pending-approval':
|
|
119
|
+
if (chunk.toolCall) {
|
|
120
|
+
emit('tool_approval_required', {
|
|
121
|
+
toolCall: chunk.toolCall,
|
|
122
|
+
isClientTool: chunk.isClientTool ?? false,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
case 'handoff':
|
|
127
|
+
if (chunk.handoff)
|
|
128
|
+
emit('handoff', chunk.handoff);
|
|
129
|
+
return;
|
|
130
|
+
// 'tool-call-delta' | 'usage' | 'finish' carry no named event - the
|
|
131
|
+
// terminal `complete` event reports finish reason + usage from the
|
|
132
|
+
// resolved AgentResponse.
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Wrap {@link toAgentSseStream} in a `Response` with the standard
|
|
137
|
+
* `text/event-stream` headers (no caching, no proxy buffering). Return it
|
|
138
|
+
* directly from a route handler.
|
|
139
|
+
*/
|
|
140
|
+
export function toAgentSseResponse(streaming, init) {
|
|
141
|
+
const headers = new Headers(init?.headers);
|
|
142
|
+
headers.set('Content-Type', 'text/event-stream; charset=utf-8');
|
|
143
|
+
headers.set('Cache-Control', 'no-cache, no-transform');
|
|
144
|
+
headers.set('Connection', 'keep-alive');
|
|
145
|
+
// Disable nginx response buffering so events flush as they're produced.
|
|
146
|
+
headers.set('X-Accel-Buffering', 'no');
|
|
147
|
+
return new Response(toAgentSseStream(streaming), { ...init, headers });
|
|
148
|
+
}
|
|
149
|
+
/** A fresh, empty {@link AgentStreamTurn}. */
|
|
150
|
+
export function newAgentStreamTurn() {
|
|
151
|
+
return {
|
|
152
|
+
assistantText: '',
|
|
153
|
+
assistantToolCalls: [],
|
|
154
|
+
serverToolResults: [],
|
|
155
|
+
pendingClientTools: [],
|
|
156
|
+
pendingApproval: null,
|
|
157
|
+
handoffPath: [],
|
|
158
|
+
done: false,
|
|
159
|
+
awaiting: undefined,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
function randomId() {
|
|
163
|
+
return typeof crypto !== 'undefined' && crypto.randomUUID
|
|
164
|
+
? crypto.randomUUID()
|
|
165
|
+
: `tc-${Date.now()}-${Math.round(Math.random() * 1e9)}`;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Read a named-event agent-SSE response body, applying each event to a fresh
|
|
169
|
+
* {@link AgentStreamTurn} and firing the matching callback. Resolves with the
|
|
170
|
+
* accumulated turn once the stream closes.
|
|
171
|
+
*
|
|
172
|
+
* The caller owns the `fetch` and the `!resp.ok` branch (so a rich error body
|
|
173
|
+
* can be read for non-2xx responses); pass an already-OK response. A missing
|
|
174
|
+
* body resolves to an empty turn. Malformed event JSON is skipped.
|
|
175
|
+
*/
|
|
176
|
+
export async function readAgentStream(resp, callbacks = {}) {
|
|
177
|
+
const turn = newAgentStreamTurn();
|
|
178
|
+
if (!resp.body)
|
|
179
|
+
return turn;
|
|
180
|
+
const reader = resp.body.getReader();
|
|
181
|
+
const decoder = new TextDecoder();
|
|
182
|
+
let buffer = '';
|
|
183
|
+
// Hoisted across reads: an `event:` line and its `data:` line can land in
|
|
184
|
+
// separate `read()` chunks when the body is sliced mid-frame.
|
|
185
|
+
let currentEvent = '';
|
|
186
|
+
for (;;) {
|
|
187
|
+
const { done, value } = await reader.read();
|
|
188
|
+
if (done)
|
|
189
|
+
break;
|
|
190
|
+
buffer += decoder.decode(value, { stream: true });
|
|
191
|
+
const lines = buffer.split('\n');
|
|
192
|
+
buffer = lines.pop() ?? '';
|
|
193
|
+
for (const line of lines) {
|
|
194
|
+
if (line.startsWith('event: ')) {
|
|
195
|
+
currentEvent = line.slice(7);
|
|
196
|
+
}
|
|
197
|
+
else if (line.startsWith('data: ') && currentEvent) {
|
|
198
|
+
try {
|
|
199
|
+
applyAgentSseEvent(currentEvent, JSON.parse(line.slice(6)), turn, callbacks);
|
|
200
|
+
}
|
|
201
|
+
catch { /* skip malformed JSON */ }
|
|
202
|
+
currentEvent = '';
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return turn;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Apply a single parsed SSE event to the turn state and fire the matching
|
|
210
|
+
* callback. Mutates `turn`; otherwise pure. Exposed for unit-testing the
|
|
211
|
+
* reducer against a synthetic turn without a live stream.
|
|
212
|
+
*/
|
|
213
|
+
export function applyAgentSseEvent(event, data, turn, callbacks = {}) {
|
|
214
|
+
switch (event) {
|
|
215
|
+
case 'text': {
|
|
216
|
+
const d = data;
|
|
217
|
+
turn.assistantText += d.text;
|
|
218
|
+
callbacks.onText?.(d.text);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
case 'tool_call': {
|
|
222
|
+
const d = data;
|
|
223
|
+
turn.assistantToolCalls.push({
|
|
224
|
+
id: d.id ?? randomId(),
|
|
225
|
+
name: d.tool,
|
|
226
|
+
arguments: d.input ?? {},
|
|
227
|
+
});
|
|
228
|
+
callbacks.onToolCall?.(d);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
case 'tool_update': {
|
|
232
|
+
callbacks.onToolUpdate?.(data);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
case 'tool_result': {
|
|
236
|
+
const d = data;
|
|
237
|
+
const toolCallId = d.toolCallId ?? d.id;
|
|
238
|
+
if (toolCallId) {
|
|
239
|
+
turn.serverToolResults.push({ role: 'tool', content: d.content, toolCallId });
|
|
240
|
+
}
|
|
241
|
+
callbacks.onToolResult?.(d);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
case 'pending_client_tools': {
|
|
245
|
+
const d = data;
|
|
246
|
+
turn.pendingClientTools = d.toolCalls ?? [];
|
|
247
|
+
callbacks.onPendingClientTools?.(turn.pendingClientTools);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
case 'tool_approval_required': {
|
|
251
|
+
const d = data;
|
|
252
|
+
turn.pendingApproval = { toolCall: d.toolCall, isClientTool: d.isClientTool };
|
|
253
|
+
callbacks.onToolApprovalRequired?.(d);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
case 'handoff': {
|
|
257
|
+
const d = data;
|
|
258
|
+
if (turn.handoffPath.length === 0 && d.from)
|
|
259
|
+
turn.handoffPath.push(d.from);
|
|
260
|
+
if (d.to)
|
|
261
|
+
turn.handoffPath.push(d.to);
|
|
262
|
+
callbacks.onHandoff?.(d);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
case 'complete': {
|
|
266
|
+
const d = data;
|
|
267
|
+
if (d.done === true)
|
|
268
|
+
turn.done = true;
|
|
269
|
+
turn.awaiting = d.awaiting;
|
|
270
|
+
callbacks.onComplete?.(d);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
case 'error': {
|
|
274
|
+
const d = data;
|
|
275
|
+
callbacks.onError?.(d);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
//# sourceMappingURL=agent-sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-sse.js","sourceRoot":"","sources":["../src/agent-sse.ts"],"names":[],"mappings":"AA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C,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAA;YAC1B,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAA;YACzB,OAAM;QACR,CAAC;QACD,KAAK,OAAO,CAAC,CAAC,CAAC;YACb,MAAM,CAAC,GAAG,IAA4B,CAAA;YACtC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAA;YACtB,OAAM;QACR,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/agent.d.ts
CHANGED
|
@@ -384,8 +384,23 @@ export declare abstract class Agent {
|
|
|
384
384
|
* Projects an inner-agent {@link StreamChunk} into a {@link SubAgentUpdate} the
|
|
385
385
|
* host can render, or `null` to suppress it. Used by both {@link Agent.asTool}
|
|
386
386
|
* (`streaming`) and the streaming resume path ({@link SubAgentResumeOptions.streaming}).
|
|
387
|
+
*
|
|
388
|
+
* On the resume paths the projector also receives a `ctx` 2nd arg carrying the
|
|
389
|
+
* originating sub-run's `originalSubRunId` (and the host `key` when batched via
|
|
390
|
+
* {@link Agent.resumeManyAsTool}), so a side-effect projector can fan a raw
|
|
391
|
+
* chunk out to the correct per-sub-agent channel and return `null`:
|
|
392
|
+
*
|
|
393
|
+
* ```ts
|
|
394
|
+
* streaming: (chunk, ctx) => { pumpToChannel(ctx!.originalSubRunId, chunk); return null }
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* `ctx` is optional — {@link Agent.asTool}'s initial-dispatch path omits it, and
|
|
398
|
+
* existing projectors that ignore the arg are unaffected.
|
|
387
399
|
*/
|
|
388
|
-
export type ChunkProjector = (chunk: StreamChunk
|
|
400
|
+
export type ChunkProjector = (chunk: StreamChunk, ctx?: {
|
|
401
|
+
originalSubRunId: string;
|
|
402
|
+
key?: string;
|
|
403
|
+
}) => SubAgentUpdate | null;
|
|
389
404
|
/**
|
|
390
405
|
* Live-progress option shared by {@link Agent.asTool} and the streaming resume
|
|
391
406
|
* surface: `true` uses {@link defaultSubAgentProjector}; a function is your own
|
package/dist/agent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAyD,YAAY,EAAE,MAAM,WAAW,CAAA;AAC/F,OAAO,KAAK,EAAmD,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAGnG,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AASpD,OAAO,KAAK,EAAE,iBAAiB,EAAuB,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAoBxG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,KAAK,EACV,kBAAkB,EAClB,SAAS,EACT,YAAY,EAEZ,aAAa,EACb,SAAS,EACT,mBAAmB,EACnB,OAAO,EACP,eAAe,EAIf,kBAAkB,EAClB,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,iBAAiB,EAEjB,aAAa,EACb,aAAa,EACb,WAAW,EAEX,QAAQ,EAGR,UAAU,EAEV,UAAU,EACX,MAAM,YAAY,CAAA;AA8BnB,yBAAyB;AACzB,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,aAAa,CAEpD;AAED,6DAA6D;AAC7D,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,CAK3D;AAID,8CAA8C;AAC9C,MAAM,WAAW,qBAAqB;IACpC,8CAA8C;IAC9C,QAAQ,EAAc,gBAAgB,CAAA;IACtC,wCAAwC;IACxC,KAAK,EAAiB,KAAK,CAAA;IAC3B,4CAA4C;IAC5C,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAA;IAC9B,4CAA4C;IAC5C,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAA;IAC9B;;;;;;;;;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QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAA;IAGxG,QAAQ,CAAC,QAAQ,EAAkB,SAAS,EAAE,CAAA;IAC9C,QAAQ,CAAC,KAAK,EAAqB,SAAS,EAAE,CAAA;IAC9C,QAAQ,CAAC,UAAU,EAAgB,UAAU,CAAA;IAC7C,QAAQ,CAAC,sBAAsB,EAAI,QAAQ,EAAE,CAAA;IAC7C,uBAAuB,EAAY;QAAE,QAAQ,EAAE,QAAQ,CAAC;QAAC,YAAY,EAAE,OAAO,CAAA;KAAE,GAAG,SAAS,CAAA;IAC5F,gBAAgB,EAAmB,YAAY,GAAG,SAAS,CAAA;IAC3D,kBAAkB,EAAiB,OAAO,CAAA;IAC1C,eAAe,EAAoB,OAAO,CAAA;IAC1C,mBAAmB,EAAgB,SAAS,EAAE,CAAA;IAC9C,gBAAgB,EAAmB,MAAM,CAAA;IACzC;;;;OAIG;IACH,cAAc,CAAC,EAAoB,cAAc,CAAA;IACjD,cAAc,EAAqB,OAAO,CAAA;CAC3C;AAED,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAgrB1D,YAAY,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/agent.js
CHANGED
|
@@ -369,8 +369,9 @@ export class Agent {
|
|
|
369
369
|
if (options.streaming) {
|
|
370
370
|
const project = options.streaming === true ? defaultSubAgentProjector : options.streaming;
|
|
371
371
|
const { stream, response } = options.agent.stream('', promptOpts);
|
|
372
|
+
const ctx = { originalSubRunId: subRunId, ...(options.key !== undefined ? { key: options.key } : {}) };
|
|
372
373
|
for await (const chunk of stream) {
|
|
373
|
-
const update = project(chunk);
|
|
374
|
+
const update = project(chunk, ctx);
|
|
374
375
|
if (update && options.onUpdate)
|
|
375
376
|
await options.onUpdate(update);
|
|
376
377
|
}
|
|
@@ -460,6 +461,8 @@ export class Agent {
|
|
|
460
461
|
// Thread the shared projector + correlate each update back to this item.
|
|
461
462
|
if (options.streaming !== undefined)
|
|
462
463
|
opts.streaming = options.streaming;
|
|
464
|
+
if (req.key !== undefined)
|
|
465
|
+
opts.key = req.key;
|
|
463
466
|
if (options.onUpdate) {
|
|
464
467
|
const batchOnUpdate = options.onUpdate;
|
|
465
468
|
opts.onUpdate = (update) => batchOnUpdate(update, base);
|