sarvam-agents-client 0.5.0 → 0.5.2
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/compat/core/agents.d.ts +53 -0
- package/dist/compat/core/agents.js +338 -0
- package/dist/compat/core/data.d.ts +10 -0
- package/dist/compat/core/data.js +275 -0
- package/dist/compat/core/errors.d.ts +37 -0
- package/dist/compat/core/errors.js +155 -0
- package/dist/compat/core/events.d.ts +107 -0
- package/dist/compat/core/events.js +160 -0
- package/dist/compat/core/files.d.ts +65 -0
- package/dist/compat/core/files.js +851 -0
- package/dist/compat/core/history.d.ts +9 -0
- package/dist/compat/core/history.js +112 -0
- package/dist/compat/core/http.d.ts +112 -0
- package/dist/compat/core/http.js +753 -0
- package/dist/compat/core/ids.d.ts +4 -0
- package/dist/compat/core/ids.js +36 -0
- package/dist/compat/core/index.d.ts +14 -0
- package/dist/compat/core/index.js +30 -0
- package/dist/compat/core/models.d.ts +20 -0
- package/dist/compat/core/models.js +75 -0
- package/dist/compat/core/paging.d.ts +14 -0
- package/dist/compat/core/paging.js +124 -0
- package/dist/compat/core/refusals.d.ts +9 -0
- package/dist/compat/core/refusals.js +25 -0
- package/dist/compat/core/sessions.d.ts +65 -0
- package/dist/compat/core/sessions.js +317 -0
- package/dist/compat/core/toolResults.d.ts +43 -0
- package/dist/compat/core/toolResults.js +208 -0
- package/dist/compat/core/turns.d.ts +104 -0
- package/dist/compat/core/turns.js +504 -0
- package/dist/compat/core/warnings.d.ts +6 -0
- package/dist/compat/core/warnings.js +26 -0
- package/dist/compat/index.d.ts +3 -0
- package/dist/compat/index.js +9 -0
- package/dist/compat/openai/client.d.ts +62 -0
- package/dist/compat/openai/client.js +108 -0
- package/dist/compat/openai/errors.d.ts +72 -0
- package/dist/compat/openai/errors.js +150 -0
- package/dist/compat/openai/index.d.ts +14 -0
- package/dist/compat/openai/index.js +42 -0
- package/dist/compat/openai/pages.d.ts +19 -0
- package/dist/compat/openai/pages.js +39 -0
- package/dist/compat/openai/resources/agents.d.ts +36 -0
- package/dist/compat/openai/resources/agents.js +549 -0
- package/dist/compat/openai/resources/events.d.ts +23 -0
- package/dist/compat/openai/resources/events.js +485 -0
- package/dist/compat/openai/resources/sessions.d.ts +33 -0
- package/dist/compat/openai/resources/sessions.js +283 -0
- package/dist/compat/openai/stream.d.ts +40 -0
- package/dist/compat/openai/stream.js +585 -0
- package/dist/compat/openai/types.d.ts +125 -0
- package/dist/compat/openai/types.js +98 -0
- package/dist/compose/agent.d.ts +2 -0
- package/dist/compose/agent.js +27 -0
- package/dist/compose/functionTool.d.ts +2 -0
- package/dist/compose/functionTool.js +46 -0
- package/dist/compose/output.d.ts +2 -0
- package/dist/compose/output.js +15 -7
- package/dist/esm/compat/core/agents.d.ts +53 -0
- package/dist/esm/compat/core/agents.js +334 -0
- package/dist/esm/compat/core/data.d.ts +10 -0
- package/dist/esm/compat/core/data.js +272 -0
- package/dist/esm/compat/core/errors.d.ts +37 -0
- package/dist/esm/compat/core/errors.js +146 -0
- package/dist/esm/compat/core/events.d.ts +107 -0
- package/dist/esm/compat/core/events.js +156 -0
- package/dist/esm/compat/core/files.d.ts +65 -0
- package/dist/esm/compat/core/files.js +829 -0
- package/dist/esm/compat/core/history.d.ts +9 -0
- package/dist/esm/compat/core/history.js +109 -0
- package/dist/esm/compat/core/http.d.ts +112 -0
- package/dist/esm/compat/core/http.js +746 -0
- package/dist/esm/compat/core/ids.d.ts +4 -0
- package/dist/esm/compat/core/ids.js +30 -0
- package/dist/esm/compat/core/index.d.ts +14 -0
- package/dist/esm/compat/core/index.js +14 -0
- package/dist/esm/compat/core/models.d.ts +20 -0
- package/dist/esm/compat/core/models.js +71 -0
- package/dist/esm/compat/core/paging.d.ts +14 -0
- package/dist/esm/compat/core/paging.js +118 -0
- package/dist/esm/compat/core/refusals.d.ts +9 -0
- package/dist/esm/compat/core/refusals.js +21 -0
- package/dist/esm/compat/core/sessions.d.ts +65 -0
- package/dist/esm/compat/core/sessions.js +312 -0
- package/dist/esm/compat/core/toolResults.d.ts +43 -0
- package/dist/esm/compat/core/toolResults.js +203 -0
- package/dist/esm/compat/core/turns.d.ts +104 -0
- package/dist/esm/compat/core/turns.js +497 -0
- package/dist/esm/compat/core/warnings.d.ts +6 -0
- package/dist/esm/compat/core/warnings.js +20 -0
- package/dist/esm/compat/index.d.ts +3 -0
- package/dist/esm/compat/index.js +3 -0
- package/dist/esm/compat/openai/client.d.ts +62 -0
- package/dist/esm/compat/openai/client.js +104 -0
- package/dist/esm/compat/openai/errors.d.ts +72 -0
- package/dist/esm/compat/openai/errors.js +130 -0
- package/dist/esm/compat/openai/index.d.ts +14 -0
- package/dist/esm/compat/openai/index.js +17 -0
- package/dist/esm/compat/openai/pages.d.ts +19 -0
- package/dist/esm/compat/openai/pages.js +35 -0
- package/dist/esm/compat/openai/resources/agents.d.ts +36 -0
- package/dist/esm/compat/openai/resources/agents.js +540 -0
- package/dist/esm/compat/openai/resources/events.d.ts +23 -0
- package/dist/esm/compat/openai/resources/events.js +479 -0
- package/dist/esm/compat/openai/resources/sessions.d.ts +33 -0
- package/dist/esm/compat/openai/resources/sessions.js +278 -0
- package/dist/esm/compat/openai/stream.d.ts +40 -0
- package/dist/esm/compat/openai/stream.js +579 -0
- package/dist/esm/compat/openai/types.d.ts +125 -0
- package/dist/esm/compat/openai/types.js +91 -0
- package/dist/esm/compose/agent.d.ts +2 -0
- package/dist/esm/compose/agent.js +28 -2
- package/dist/esm/compose/functionTool.d.ts +2 -0
- package/dist/esm/compose/functionTool.js +45 -0
- package/dist/esm/compose/output.d.ts +2 -0
- package/dist/esm/compose/output.js +14 -7
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/package.json +53 -11
- package/src/compat/core/agents.ts +437 -0
- package/src/compat/core/data.ts +283 -0
- package/src/compat/core/errors.ts +162 -0
- package/src/compat/core/events.ts +198 -0
- package/src/compat/core/files.ts +913 -0
- package/src/compat/core/history.ts +116 -0
- package/src/compat/core/http.ts +827 -0
- package/src/compat/core/ids.ts +35 -0
- package/src/compat/core/index.ts +14 -0
- package/src/compat/core/models.ts +85 -0
- package/src/compat/core/paging.ts +119 -0
- package/src/compat/core/refusals.ts +31 -0
- package/src/compat/core/sessions.ts +358 -0
- package/src/compat/core/toolResults.ts +235 -0
- package/src/compat/core/turns.ts +622 -0
- package/src/compat/core/warnings.ts +23 -0
- package/src/compat/index.ts +3 -0
- package/src/compat/openai/client.ts +177 -0
- package/src/compat/openai/errors.ts +165 -0
- package/src/compat/openai/index.ts +65 -0
- package/src/compat/openai/pages.ts +55 -0
- package/src/compat/openai/resources/agents.ts +603 -0
- package/src/compat/openai/resources/events.ts +523 -0
- package/src/compat/openai/resources/sessions.ts +369 -0
- package/src/compat/openai/stream.ts +599 -0
- package/src/compat/openai/types.ts +217 -0
- package/src/compose/agent.ts +30 -3
- package/src/compose/functionTool.ts +44 -0
- package/src/compose/output.ts +14 -8
- package/src/index.ts +3 -0
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
/** OpenAI event translation (§5.1) and AgentSessionEventStream with result collection. */
|
|
2
|
+
import type { ResponseStream } from "../../streaming.js";
|
|
3
|
+
import { CoreAPIError, CoreConnectionError, CoreTimeoutError } from "../core/errors.js";
|
|
4
|
+
import { TurnEvents, type TurnEvent } from "../core/events.js";
|
|
5
|
+
import { Listener } from "../core/turns.js";
|
|
6
|
+
import { toOpenAIError } from "./errors.js";
|
|
7
|
+
import type { AgentSession, AgentSessionEvent, FinalResult, RequiredAction, Usage } from "./types.js";
|
|
8
|
+
|
|
9
|
+
const EARLY_CLOSE_MSG = "Stream closed before a turn ended. Retrieve the saved state.";
|
|
10
|
+
|
|
11
|
+
export interface StreamState {
|
|
12
|
+
sessionId: string;
|
|
13
|
+
turnId: string | null;
|
|
14
|
+
outputIndex: number;
|
|
15
|
+
contentIndex: number;
|
|
16
|
+
pendingTurnLimit: Record<string, unknown> | null;
|
|
17
|
+
outputParsed: unknown;
|
|
18
|
+
collectedText: string[];
|
|
19
|
+
finalStatus: string;
|
|
20
|
+
finalUsage: Usage | null;
|
|
21
|
+
turnEnded: boolean;
|
|
22
|
+
pausedForAction: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createStreamState(sessionId: string): StreamState {
|
|
26
|
+
return {
|
|
27
|
+
sessionId,
|
|
28
|
+
turnId: null,
|
|
29
|
+
outputIndex: -1,
|
|
30
|
+
contentIndex: 0,
|
|
31
|
+
pendingTurnLimit: null,
|
|
32
|
+
outputParsed: undefined,
|
|
33
|
+
collectedText: [],
|
|
34
|
+
finalStatus: "completed",
|
|
35
|
+
finalUsage: null,
|
|
36
|
+
turnEnded: false,
|
|
37
|
+
pausedForAction: false,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function toOpenAIEvents(ev: TurnEvent, state: StreamState): AgentSessionEvent[] {
|
|
42
|
+
const sid = state.sessionId;
|
|
43
|
+
const out: AgentSessionEvent[] = [];
|
|
44
|
+
|
|
45
|
+
// When resuming after requires_action (tool_result / clarify continuation), clear the pause
|
|
46
|
+
// flag on the first non-finish continuation event — whether or not Panja emits a new `start`.
|
|
47
|
+
// A trailing turn_finished in the paused stream itself must remain suppressed.
|
|
48
|
+
if (state.pausedForAction && ev.kind !== "turn_finished") {
|
|
49
|
+
state.pausedForAction = false;
|
|
50
|
+
state.turnEnded = false;
|
|
51
|
+
if (ev.kind === "turn_started") {
|
|
52
|
+
if (ev.responseId) state.turnId = ev.responseId;
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (ev.kind === "turn_started") {
|
|
58
|
+
state.turnId = ev.responseId;
|
|
59
|
+
state.outputIndex = -1;
|
|
60
|
+
state.turnEnded = false;
|
|
61
|
+
state.pausedForAction = false;
|
|
62
|
+
out.push({ type: "agent.session.in_progress", session_id: sid });
|
|
63
|
+
out.push({
|
|
64
|
+
type: "agent.session.turn.created",
|
|
65
|
+
session_id: sid,
|
|
66
|
+
turn_id: ev.responseId,
|
|
67
|
+
turn: { id: ev.responseId, status: "in_progress" },
|
|
68
|
+
});
|
|
69
|
+
out.push({
|
|
70
|
+
type: "agent.session.turn.in_progress",
|
|
71
|
+
session_id: sid,
|
|
72
|
+
turn_id: ev.responseId,
|
|
73
|
+
turn: { id: ev.responseId, status: "in_progress" },
|
|
74
|
+
});
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const tid = state.turnId;
|
|
79
|
+
|
|
80
|
+
switch (ev.kind) {
|
|
81
|
+
case "text_started": {
|
|
82
|
+
state.outputIndex += 1;
|
|
83
|
+
state.contentIndex = 0;
|
|
84
|
+
out.push({
|
|
85
|
+
type: "agent.session.turn.item.added",
|
|
86
|
+
session_id: sid,
|
|
87
|
+
turn_id: tid,
|
|
88
|
+
item_id: ev.itemId,
|
|
89
|
+
output_index: state.outputIndex,
|
|
90
|
+
item: { id: ev.itemId, type: "message", role: "assistant", status: "in_progress", content: [] },
|
|
91
|
+
});
|
|
92
|
+
out.push({
|
|
93
|
+
type: "agent.session.turn.content_part.added",
|
|
94
|
+
session_id: sid,
|
|
95
|
+
turn_id: tid,
|
|
96
|
+
item_id: ev.itemId,
|
|
97
|
+
output_index: state.outputIndex,
|
|
98
|
+
content_index: state.contentIndex,
|
|
99
|
+
part: { type: "output_text", text: "" },
|
|
100
|
+
});
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case "text_delta": {
|
|
104
|
+
state.collectedText.push(ev.delta);
|
|
105
|
+
out.push({
|
|
106
|
+
type: "agent.session.turn.output_text.delta",
|
|
107
|
+
session_id: sid,
|
|
108
|
+
turn_id: tid,
|
|
109
|
+
item_id: ev.itemId,
|
|
110
|
+
output_index: Math.max(0, state.outputIndex),
|
|
111
|
+
content_index: state.contentIndex,
|
|
112
|
+
delta: ev.delta,
|
|
113
|
+
});
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case "text_done": {
|
|
117
|
+
const idx = Math.max(0, state.outputIndex);
|
|
118
|
+
out.push({
|
|
119
|
+
type: "agent.session.turn.output_text.done",
|
|
120
|
+
session_id: sid,
|
|
121
|
+
turn_id: tid,
|
|
122
|
+
item_id: ev.itemId,
|
|
123
|
+
output_index: idx,
|
|
124
|
+
content_index: state.contentIndex,
|
|
125
|
+
text: ev.text,
|
|
126
|
+
});
|
|
127
|
+
out.push({
|
|
128
|
+
type: "agent.session.turn.content_part.done",
|
|
129
|
+
session_id: sid,
|
|
130
|
+
turn_id: tid,
|
|
131
|
+
item_id: ev.itemId,
|
|
132
|
+
output_index: idx,
|
|
133
|
+
content_index: state.contentIndex,
|
|
134
|
+
part: { type: "output_text", text: ev.text },
|
|
135
|
+
});
|
|
136
|
+
out.push({
|
|
137
|
+
type: "agent.session.turn.item.done",
|
|
138
|
+
session_id: sid,
|
|
139
|
+
turn_id: tid,
|
|
140
|
+
item_id: ev.itemId,
|
|
141
|
+
output_index: idx,
|
|
142
|
+
item: {
|
|
143
|
+
id: ev.itemId,
|
|
144
|
+
type: "message",
|
|
145
|
+
role: "assistant",
|
|
146
|
+
status: "completed",
|
|
147
|
+
content: [{ type: "output_text", text: ev.text }],
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case "reasoning_started": {
|
|
153
|
+
state.outputIndex += 1;
|
|
154
|
+
out.push({
|
|
155
|
+
type: "agent.session.turn.item.added",
|
|
156
|
+
session_id: sid,
|
|
157
|
+
turn_id: tid,
|
|
158
|
+
item_id: ev.itemId,
|
|
159
|
+
output_index: state.outputIndex,
|
|
160
|
+
item: { id: ev.itemId, type: "reasoning", status: "in_progress", summary: [] },
|
|
161
|
+
});
|
|
162
|
+
out.push({
|
|
163
|
+
type: "agent.session.turn.reasoning_summary_part.added",
|
|
164
|
+
session_id: sid,
|
|
165
|
+
turn_id: tid,
|
|
166
|
+
item_id: ev.itemId,
|
|
167
|
+
output_index: state.outputIndex,
|
|
168
|
+
summary_index: 0,
|
|
169
|
+
part: { type: "summary_text", text: "" },
|
|
170
|
+
});
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
case "reasoning_delta": {
|
|
174
|
+
out.push({
|
|
175
|
+
type: "agent.session.turn.reasoning_summary_text.delta",
|
|
176
|
+
session_id: sid,
|
|
177
|
+
turn_id: tid,
|
|
178
|
+
item_id: ev.itemId,
|
|
179
|
+
output_index: Math.max(0, state.outputIndex),
|
|
180
|
+
summary_index: 0,
|
|
181
|
+
delta: ev.delta,
|
|
182
|
+
});
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case "reasoning_done": {
|
|
186
|
+
const idx = Math.max(0, state.outputIndex);
|
|
187
|
+
out.push({
|
|
188
|
+
type: "agent.session.turn.reasoning_summary_text.done",
|
|
189
|
+
session_id: sid,
|
|
190
|
+
turn_id: tid,
|
|
191
|
+
item_id: ev.itemId,
|
|
192
|
+
output_index: idx,
|
|
193
|
+
summary_index: 0,
|
|
194
|
+
text: ev.text,
|
|
195
|
+
});
|
|
196
|
+
out.push({
|
|
197
|
+
type: "agent.session.turn.reasoning_summary_part.done",
|
|
198
|
+
session_id: sid,
|
|
199
|
+
turn_id: tid,
|
|
200
|
+
item_id: ev.itemId,
|
|
201
|
+
output_index: idx,
|
|
202
|
+
summary_index: 0,
|
|
203
|
+
part: { type: "summary_text", text: ev.text },
|
|
204
|
+
});
|
|
205
|
+
out.push({
|
|
206
|
+
type: "agent.session.turn.item.done",
|
|
207
|
+
session_id: sid,
|
|
208
|
+
turn_id: tid,
|
|
209
|
+
item_id: ev.itemId,
|
|
210
|
+
output_index: idx,
|
|
211
|
+
item: {
|
|
212
|
+
id: ev.itemId,
|
|
213
|
+
type: "reasoning",
|
|
214
|
+
status: "completed",
|
|
215
|
+
summary: [{ type: "summary_text", text: ev.text }],
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case "tool_called": {
|
|
221
|
+
state.outputIndex += 1;
|
|
222
|
+
const itemType = ev.name === "web_search" ? "web_search_call" : "sarvam.tool_call";
|
|
223
|
+
const item: Record<string, unknown> = {
|
|
224
|
+
id: ev.itemId,
|
|
225
|
+
type: itemType,
|
|
226
|
+
call_id: ev.callId,
|
|
227
|
+
name: ev.name,
|
|
228
|
+
input: ev.input,
|
|
229
|
+
status: "completed",
|
|
230
|
+
};
|
|
231
|
+
out.push({
|
|
232
|
+
type: "agent.session.turn.item.added",
|
|
233
|
+
session_id: sid,
|
|
234
|
+
turn_id: tid,
|
|
235
|
+
item_id: ev.itemId,
|
|
236
|
+
output_index: state.outputIndex,
|
|
237
|
+
item: { ...item, status: "in_progress" },
|
|
238
|
+
});
|
|
239
|
+
out.push({
|
|
240
|
+
type: "agent.session.turn.item.done",
|
|
241
|
+
session_id: sid,
|
|
242
|
+
turn_id: tid,
|
|
243
|
+
item_id: ev.itemId,
|
|
244
|
+
output_index: state.outputIndex,
|
|
245
|
+
item,
|
|
246
|
+
});
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "tool_output": {
|
|
250
|
+
state.outputIndex += 1;
|
|
251
|
+
const outId = `${ev.itemId}:output`;
|
|
252
|
+
if (ev.isSandbox) {
|
|
253
|
+
const deltaText = typeof ev.output === "string" ? ev.output : JSON.stringify(ev.output);
|
|
254
|
+
out.push({
|
|
255
|
+
type: "agent.output.command_execution_output.delta",
|
|
256
|
+
session_id: sid,
|
|
257
|
+
turn_id: tid,
|
|
258
|
+
item_id: outId,
|
|
259
|
+
delta: deltaText,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
const outItem: Record<string, unknown> = {
|
|
263
|
+
id: outId,
|
|
264
|
+
type: "sarvam.tool_output",
|
|
265
|
+
call_id: ev.callId,
|
|
266
|
+
output: ev.output,
|
|
267
|
+
status: "completed",
|
|
268
|
+
};
|
|
269
|
+
out.push({
|
|
270
|
+
type: "agent.session.turn.item.added",
|
|
271
|
+
session_id: sid,
|
|
272
|
+
turn_id: tid,
|
|
273
|
+
item_id: outId,
|
|
274
|
+
output_index: state.outputIndex,
|
|
275
|
+
item: { ...outItem, status: "in_progress" },
|
|
276
|
+
});
|
|
277
|
+
out.push({
|
|
278
|
+
type: "agent.session.turn.item.done",
|
|
279
|
+
session_id: sid,
|
|
280
|
+
turn_id: tid,
|
|
281
|
+
item_id: outId,
|
|
282
|
+
output_index: state.outputIndex,
|
|
283
|
+
item: outItem,
|
|
284
|
+
});
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
case "function_calls_requested": {
|
|
288
|
+
const actions: RequiredAction[] = [];
|
|
289
|
+
for (let i = 0; i < ev.calls.length; i++) {
|
|
290
|
+
const call = ev.calls[i];
|
|
291
|
+
state.outputIndex += 1;
|
|
292
|
+
const iid = i < ev.itemIds.length ? ev.itemIds[i] : `${tid || "turn"}:fn:${i}`;
|
|
293
|
+
const argsStr = JSON.stringify(call.arguments);
|
|
294
|
+
const fnItem: Record<string, unknown> = {
|
|
295
|
+
id: iid,
|
|
296
|
+
type: "function_call",
|
|
297
|
+
call_id: call.callId,
|
|
298
|
+
name: call.name,
|
|
299
|
+
arguments: argsStr,
|
|
300
|
+
status: "completed",
|
|
301
|
+
};
|
|
302
|
+
out.push({
|
|
303
|
+
type: "agent.session.turn.item.added",
|
|
304
|
+
session_id: sid,
|
|
305
|
+
turn_id: tid,
|
|
306
|
+
item_id: iid,
|
|
307
|
+
output_index: state.outputIndex,
|
|
308
|
+
item: { ...fnItem, status: "in_progress" },
|
|
309
|
+
});
|
|
310
|
+
out.push({
|
|
311
|
+
type: "agent.session.turn.item.done",
|
|
312
|
+
session_id: sid,
|
|
313
|
+
turn_id: tid,
|
|
314
|
+
item_id: iid,
|
|
315
|
+
output_index: state.outputIndex,
|
|
316
|
+
item: fnItem,
|
|
317
|
+
});
|
|
318
|
+
actions.push({
|
|
319
|
+
type: "function",
|
|
320
|
+
turn_id: tid,
|
|
321
|
+
call_id: call.callId,
|
|
322
|
+
name: call.name,
|
|
323
|
+
arguments: argsStr,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
state.finalStatus = "requires_action";
|
|
327
|
+
state.turnEnded = true;
|
|
328
|
+
state.pausedForAction = true;
|
|
329
|
+
out.push({
|
|
330
|
+
type: "agent.session.requires_action",
|
|
331
|
+
session_id: sid,
|
|
332
|
+
turn_id: tid,
|
|
333
|
+
session: {
|
|
334
|
+
id: sid,
|
|
335
|
+
object: "agent.session",
|
|
336
|
+
status: "requires_action",
|
|
337
|
+
required_actions: actions,
|
|
338
|
+
metadata: {},
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case "clarify_requested": {
|
|
344
|
+
const action: RequiredAction = {
|
|
345
|
+
type: "sarvam.clarify",
|
|
346
|
+
turn_id: tid,
|
|
347
|
+
request_id: ev.requestId,
|
|
348
|
+
questions: ev.questions.slice(),
|
|
349
|
+
};
|
|
350
|
+
out.push({
|
|
351
|
+
type: "sarvam.clarify.requested",
|
|
352
|
+
session_id: sid,
|
|
353
|
+
turn_id: tid,
|
|
354
|
+
request_id: ev.requestId,
|
|
355
|
+
questions: ev.questions.slice(),
|
|
356
|
+
});
|
|
357
|
+
state.finalStatus = "requires_action";
|
|
358
|
+
state.turnEnded = true;
|
|
359
|
+
state.pausedForAction = true;
|
|
360
|
+
out.push({
|
|
361
|
+
type: "agent.session.requires_action",
|
|
362
|
+
session_id: sid,
|
|
363
|
+
turn_id: tid,
|
|
364
|
+
session: {
|
|
365
|
+
id: sid,
|
|
366
|
+
object: "agent.session",
|
|
367
|
+
status: "requires_action",
|
|
368
|
+
required_actions: [action],
|
|
369
|
+
metadata: {},
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
case "output_ready": {
|
|
375
|
+
if (!ev.skipped) {
|
|
376
|
+
state.outputParsed = ev.output;
|
|
377
|
+
}
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
case "output_mismatch": {
|
|
381
|
+
if (state.turnEnded) return out;
|
|
382
|
+
state.finalStatus = "failed";
|
|
383
|
+
state.turnEnded = true;
|
|
384
|
+
const msg = ev.errors.length > 0 ? ev.errors.join("; ") : "Output did not match schema.";
|
|
385
|
+
out.push({
|
|
386
|
+
type: "agent.session.turn.failed",
|
|
387
|
+
session_id: sid,
|
|
388
|
+
turn_id: tid,
|
|
389
|
+
error: { code: "schema_mismatch", message: msg },
|
|
390
|
+
});
|
|
391
|
+
out.push({ type: "agent.session.idle", session_id: sid });
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
case "turn_limit_reached": {
|
|
395
|
+
state.pendingTurnLimit = { kind: ev.capKind, limit: ev.limit, value: ev.value };
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
case "file_shared": {
|
|
399
|
+
out.push({ type: "sarvam.file_shared", session_id: sid, turn_id: tid, data: { ...ev.data } });
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case "stopped": {
|
|
403
|
+
if (state.turnEnded) return out;
|
|
404
|
+
state.finalStatus = "cancelled";
|
|
405
|
+
state.turnEnded = true;
|
|
406
|
+
out.push({ type: "agent.session.turn.cancelled", session_id: sid, turn_id: tid });
|
|
407
|
+
out.push({ type: "agent.session.idle", session_id: sid });
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "turn_failed": {
|
|
411
|
+
if (state.turnEnded) return out;
|
|
412
|
+
state.finalStatus = "failed";
|
|
413
|
+
state.turnEnded = true;
|
|
414
|
+
out.push({
|
|
415
|
+
type: "agent.session.turn.failed",
|
|
416
|
+
session_id: sid,
|
|
417
|
+
turn_id: tid,
|
|
418
|
+
error: { code: ev.code, message: ev.message },
|
|
419
|
+
});
|
|
420
|
+
out.push({ type: "agent.session.idle", session_id: sid });
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case "turn_finished": {
|
|
424
|
+
const usage: Usage = {
|
|
425
|
+
input_tokens: ev.usage.inputTokens,
|
|
426
|
+
input_tokens_details: { cached_tokens: ev.usage.cachedTokens },
|
|
427
|
+
output_tokens: ev.usage.outputTokens,
|
|
428
|
+
output_tokens_details: { reasoning_tokens: ev.usage.reasoningTokens },
|
|
429
|
+
total_tokens: ev.usage.totalTokens,
|
|
430
|
+
};
|
|
431
|
+
state.finalUsage = usage;
|
|
432
|
+
if (!state.turnEnded) {
|
|
433
|
+
state.finalStatus = "completed";
|
|
434
|
+
state.turnEnded = true;
|
|
435
|
+
const sarvamExt = state.pendingTurnLimit ? { turn_limit: state.pendingTurnLimit } : undefined;
|
|
436
|
+
const completedEv: AgentSessionEvent = {
|
|
437
|
+
type: "agent.session.turn.completed",
|
|
438
|
+
session_id: sid,
|
|
439
|
+
turn_id: tid,
|
|
440
|
+
usage,
|
|
441
|
+
};
|
|
442
|
+
if (sarvamExt) completedEv.sarvam = sarvamExt;
|
|
443
|
+
out.push(completedEv);
|
|
444
|
+
out.push({ type: "agent.session.idle", session_id: sid });
|
|
445
|
+
}
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
case "unknown": {
|
|
449
|
+
out.push({ type: "sarvam.unknown", session_id: sid, turn_id: tid, data: { ...ev.raw } });
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export class AgentSessionEventStream implements AsyncIterable<AgentSessionEvent> {
|
|
458
|
+
readonly controller = new AbortController();
|
|
459
|
+
private readonly state: StreamState;
|
|
460
|
+
private collecting = false;
|
|
461
|
+
private consumed = false;
|
|
462
|
+
private closed = false;
|
|
463
|
+
|
|
464
|
+
constructor(
|
|
465
|
+
readonly sessionId: string,
|
|
466
|
+
private readonly opts: {
|
|
467
|
+
listener?: Listener;
|
|
468
|
+
rawStream?: ResponseStream;
|
|
469
|
+
sessionCreated?: AgentSession;
|
|
470
|
+
},
|
|
471
|
+
) {
|
|
472
|
+
this.state = createStreamState(sessionId);
|
|
473
|
+
this.controller.signal.addEventListener("abort", () => {
|
|
474
|
+
this.close();
|
|
475
|
+
}, { once: true });
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
withResultCollection(): this {
|
|
479
|
+
this.collecting = true;
|
|
480
|
+
return this;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
close(): void {
|
|
484
|
+
this.closed = true;
|
|
485
|
+
this.opts.listener?.close();
|
|
486
|
+
this.opts.rawStream?.close();
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
private async *iterNeutral(): AsyncGenerator<TurnEvent, void, unknown> {
|
|
490
|
+
if (this.opts.listener) {
|
|
491
|
+
const listener = this.opts.listener;
|
|
492
|
+
try {
|
|
493
|
+
for await (const ev of listener) {
|
|
494
|
+
if (this.closed) return;
|
|
495
|
+
yield ev;
|
|
496
|
+
}
|
|
497
|
+
} finally {
|
|
498
|
+
listener.close();
|
|
499
|
+
}
|
|
500
|
+
} else if (this.opts.rawStream) {
|
|
501
|
+
const rs = this.opts.rawStream;
|
|
502
|
+
const machine = new TurnEvents(rs.responseId ?? "");
|
|
503
|
+
try {
|
|
504
|
+
for await (const chunk of rs) {
|
|
505
|
+
if (this.closed) return;
|
|
506
|
+
for (const ev of machine.feed(chunk as unknown as Record<string, unknown>)) {
|
|
507
|
+
if (this.closed) return;
|
|
508
|
+
yield ev;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
} finally {
|
|
512
|
+
rs.close();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<AgentSessionEvent, void, unknown> {
|
|
518
|
+
this.consumed = true;
|
|
519
|
+
try {
|
|
520
|
+
if (this.opts.sessionCreated) {
|
|
521
|
+
if (this.closed) return;
|
|
522
|
+
yield {
|
|
523
|
+
type: "agent.session.created",
|
|
524
|
+
session_id: this.sessionId,
|
|
525
|
+
session: this.opts.sessionCreated,
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
for await (const neutral of this.iterNeutral()) {
|
|
529
|
+
for (const ev of toOpenAIEvents(neutral, this.state)) {
|
|
530
|
+
if (this.closed) return;
|
|
531
|
+
yield ev;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (!this.state.turnEnded && !this.closed) {
|
|
535
|
+
throw new Error(EARLY_CLOSE_MSG);
|
|
536
|
+
}
|
|
537
|
+
} catch (err) {
|
|
538
|
+
if (this.closed) return;
|
|
539
|
+
if (err instanceof CoreAPIError) {
|
|
540
|
+
yield {
|
|
541
|
+
type: "error",
|
|
542
|
+
session_id: this.sessionId,
|
|
543
|
+
turn_id: this.state.turnId,
|
|
544
|
+
error: { code: err.code ?? "api_error", message: err.message },
|
|
545
|
+
};
|
|
546
|
+
throw toOpenAIError(err);
|
|
547
|
+
}
|
|
548
|
+
if (err instanceof Error && err.message === EARLY_CLOSE_MSG) {
|
|
549
|
+
throw err;
|
|
550
|
+
}
|
|
551
|
+
if (err instanceof Error && (err.name === "AbortError" || err.name === "TimeoutError")) {
|
|
552
|
+
const ce = new CoreTimeoutError(`Stream timed out: ${err.message}`);
|
|
553
|
+
yield {
|
|
554
|
+
type: "error",
|
|
555
|
+
session_id: this.sessionId,
|
|
556
|
+
turn_id: this.state.turnId,
|
|
557
|
+
error: { code: "timeout_error", message: ce.message },
|
|
558
|
+
};
|
|
559
|
+
throw toOpenAIError(ce);
|
|
560
|
+
}
|
|
561
|
+
if (err instanceof Error) {
|
|
562
|
+
const ce = new CoreConnectionError(`Stream connection error: ${err.message}`);
|
|
563
|
+
yield {
|
|
564
|
+
type: "error",
|
|
565
|
+
session_id: this.sessionId,
|
|
566
|
+
turn_id: this.state.turnId,
|
|
567
|
+
error: { code: "connection_error", message: ce.message },
|
|
568
|
+
};
|
|
569
|
+
throw toOpenAIError(ce);
|
|
570
|
+
}
|
|
571
|
+
throw err;
|
|
572
|
+
} finally {
|
|
573
|
+
this.close();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
async finalResult(): Promise<FinalResult> {
|
|
578
|
+
if (!this.consumed) {
|
|
579
|
+
for await (const _ of this) {
|
|
580
|
+
// drain
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (!this.state.turnEnded) {
|
|
584
|
+
throw new Error(EARLY_CLOSE_MSG);
|
|
585
|
+
}
|
|
586
|
+
return {
|
|
587
|
+
output_text: this.state.collectedText.join(""),
|
|
588
|
+
session_id: this.sessionId,
|
|
589
|
+
turn_id: this.state.turnId,
|
|
590
|
+
status: this.state.finalStatus,
|
|
591
|
+
usage: this.state.finalUsage,
|
|
592
|
+
output_parsed: this.state.outputParsed ?? null,
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
async getFinalResult(): Promise<FinalResult> {
|
|
597
|
+
return this.finalResult();
|
|
598
|
+
}
|
|
599
|
+
}
|