sarvam-agents-client 0.3.0 → 0.3.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/README.md +98 -1
- package/dist/apis/FunctionToolsApi.d.ts +33 -0
- package/dist/apis/FunctionToolsApi.js +65 -0
- package/dist/apis/index.d.ts +1 -0
- package/dist/apis/index.js +1 -0
- package/dist/compose/agent.d.ts +20 -3
- package/dist/compose/agent.js +84 -7
- package/dist/compose/errors.d.ts +5 -1
- package/dist/compose/errors.js +4 -1
- package/dist/compose/events.d.ts +4 -0
- package/dist/compose/events.js +34 -0
- package/dist/compose/functionTool.d.ts +101 -0
- package/dist/compose/functionTool.js +420 -0
- package/dist/compose/index.d.ts +6 -2
- package/dist/compose/index.js +3 -1
- package/dist/compose/output.d.ts +11 -0
- package/dist/compose/output.js +32 -7
- package/dist/compose/run.d.ts +5 -1
- package/dist/compose/run.js +118 -25
- package/dist/compose/runState.d.ts +26 -1
- package/dist/compose/runState.js +30 -0
- package/dist/compose/settingsTransport.d.ts +28 -1
- package/dist/compose/settingsTransport.js +389 -7
- package/dist/compose/transport.d.ts +18 -1
- package/dist/compose/webhook.d.ts +19 -0
- package/dist/compose/webhook.js +184 -0
- package/dist/esm/apis/FunctionToolsApi.d.ts +33 -0
- package/dist/esm/apis/FunctionToolsApi.js +61 -0
- package/dist/esm/apis/index.d.ts +1 -0
- package/dist/esm/apis/index.js +1 -0
- package/dist/esm/compose/agent.d.ts +20 -3
- package/dist/esm/compose/agent.js +84 -7
- package/dist/esm/compose/errors.d.ts +5 -1
- package/dist/esm/compose/errors.js +4 -1
- package/dist/esm/compose/events.d.ts +4 -0
- package/dist/esm/compose/events.js +33 -0
- package/dist/esm/compose/functionTool.d.ts +101 -0
- package/dist/esm/compose/functionTool.js +406 -0
- package/dist/esm/compose/index.d.ts +6 -2
- package/dist/esm/compose/index.js +1 -0
- package/dist/esm/compose/output.d.ts +11 -0
- package/dist/esm/compose/output.js +30 -9
- package/dist/esm/compose/run.d.ts +5 -1
- package/dist/esm/compose/run.js +118 -25
- package/dist/esm/compose/runState.d.ts +26 -1
- package/dist/esm/compose/runState.js +31 -1
- package/dist/esm/compose/settingsTransport.d.ts +28 -1
- package/dist/esm/compose/settingsTransport.js +392 -11
- package/dist/esm/compose/transport.d.ts +18 -1
- package/dist/esm/compose/webhook.d.ts +19 -0
- package/dist/esm/compose/webhook.js +177 -0
- package/dist/esm/models/AgentConversationDetail.d.ts +7 -0
- package/dist/esm/models/AgentConversationDetail.js +3 -0
- package/dist/esm/models/ClarifyBody.d.ts +6 -0
- package/dist/esm/models/ClarifyBody.js +2 -0
- package/dist/esm/models/ConversationDetail.d.ts +7 -0
- package/dist/esm/models/ConversationDetail.js +3 -0
- package/dist/esm/models/ErrorDetail.d.ts +1 -0
- package/dist/esm/models/ErrorDetail.js +2 -1
- package/dist/esm/models/FunctionResultBody.d.ts +44 -0
- package/dist/esm/models/FunctionResultBody.js +49 -0
- package/dist/esm/models/QueueMessageInput.d.ts +23 -0
- package/dist/esm/models/QueueMessageInput.js +14 -0
- package/dist/esm/models/RequiredAction.d.ts +77 -0
- package/dist/esm/models/RequiredAction.js +69 -0
- package/dist/esm/models/ResponseDetail.d.ts +8 -0
- package/dist/esm/models/ResponseDetail.js +2 -0
- package/dist/esm/models/ResponseInput.d.ts +23 -0
- package/dist/esm/models/ResponseInput.js +14 -0
- package/dist/esm/models/ToolResults200Response.d.ts +38 -0
- package/dist/esm/models/ToolResults200Response.js +47 -0
- package/dist/esm/models/ToolResultsBody.d.ts +39 -0
- package/dist/esm/models/ToolResultsBody.js +48 -0
- package/dist/esm/models/index.d.ts +4 -0
- package/dist/esm/models/index.js +4 -0
- package/dist/esm/streaming.d.ts +14 -2
- package/dist/esm/streaming.js +26 -4
- package/dist/esm/webhooks.d.ts +203 -0
- package/dist/esm/webhooks.js +215 -0
- package/dist/models/AgentConversationDetail.d.ts +7 -0
- package/dist/models/AgentConversationDetail.js +3 -0
- package/dist/models/ClarifyBody.d.ts +6 -0
- package/dist/models/ClarifyBody.js +2 -0
- package/dist/models/ConversationDetail.d.ts +7 -0
- package/dist/models/ConversationDetail.js +3 -0
- package/dist/models/ErrorDetail.d.ts +1 -0
- package/dist/models/ErrorDetail.js +2 -1
- package/dist/models/FunctionResultBody.d.ts +44 -0
- package/dist/models/FunctionResultBody.js +56 -0
- package/dist/models/QueueMessageInput.d.ts +23 -0
- package/dist/models/QueueMessageInput.js +15 -1
- package/dist/models/RequiredAction.d.ts +77 -0
- package/dist/models/RequiredAction.js +77 -0
- package/dist/models/ResponseDetail.d.ts +8 -0
- package/dist/models/ResponseDetail.js +2 -0
- package/dist/models/ResponseInput.d.ts +23 -0
- package/dist/models/ResponseInput.js +15 -0
- package/dist/models/ToolResults200Response.d.ts +38 -0
- package/dist/models/ToolResults200Response.js +54 -0
- package/dist/models/ToolResultsBody.d.ts +39 -0
- package/dist/models/ToolResultsBody.js +55 -0
- package/dist/models/index.d.ts +4 -0
- package/dist/models/index.js +4 -0
- package/dist/streaming.d.ts +14 -2
- package/dist/streaming.js +27 -4
- package/dist/webhooks.d.ts +203 -0
- package/dist/webhooks.js +224 -0
- package/package.json +9 -1
- package/src/apis/FunctionToolsApi.ts +102 -0
- package/src/apis/index.ts +1 -0
- package/src/compose/agent.ts +96 -9
- package/src/compose/errors.ts +7 -1
- package/src/compose/events.ts +30 -0
- package/src/compose/functionTool.ts +441 -0
- package/src/compose/index.ts +6 -2
- package/src/compose/output.ts +30 -9
- package/src/compose/run.ts +115 -18
- package/src/compose/runState.ts +44 -5
- package/src/compose/settingsTransport.ts +379 -8
- package/src/compose/transport.ts +24 -1
- package/src/compose/webhook.ts +181 -0
- package/src/models/AgentConversationDetail.ts +15 -0
- package/src/models/ClarifyBody.ts +8 -0
- package/src/models/ConversationDetail.ts +15 -0
- package/src/models/ErrorDetail.ts +2 -1
- package/src/models/FunctionResultBody.ts +83 -0
- package/src/models/QueueMessageInput.ts +28 -0
- package/src/models/RequiredAction.ts +128 -0
- package/src/models/ResponseDetail.ts +8 -0
- package/src/models/ResponseInput.ts +30 -0
- package/src/models/ToolResults200Response.ts +75 -0
- package/src/models/ToolResultsBody.ts +83 -0
- package/src/models/index.ts +4 -0
- package/src/streaming.ts +39 -3
- package/src/webhooks.ts +390 -0
package/dist/compose/run.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.run = run;
|
|
|
5
5
|
const runtime_js_1 = require("../runtime.js");
|
|
6
6
|
const deploy_js_1 = require("./deploy.js");
|
|
7
7
|
const errors_js_1 = require("./errors.js");
|
|
8
|
+
const functionTool_js_1 = require("./functionTool.js");
|
|
8
9
|
const output_js_1 = require("./output.js");
|
|
9
10
|
const runState_js_1 = require("./runState.js");
|
|
10
11
|
const MAX_RECONNECTS = 3;
|
|
@@ -106,12 +107,14 @@ class Stopper {
|
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
class Loop {
|
|
109
|
-
constructor(transport, onClarify, emit, signal, onConversation) {
|
|
110
|
+
constructor(transport, onClarify, emit, signal, onConversation, functions = new Map(), raiseErrors = false) {
|
|
110
111
|
this.transport = transport;
|
|
111
112
|
this.onClarify = onClarify;
|
|
112
113
|
this.emit = emit;
|
|
113
114
|
this.signal = signal;
|
|
114
115
|
this.onConversation = onConversation;
|
|
116
|
+
this.functions = functions;
|
|
117
|
+
this.raiseErrors = raiseErrors;
|
|
115
118
|
this.conversationId = null;
|
|
116
119
|
this.items = [];
|
|
117
120
|
this.stopReason = undefined;
|
|
@@ -121,6 +124,11 @@ class Loop {
|
|
|
121
124
|
// of the block it dropped in, doesn't repeat text.
|
|
122
125
|
this.blocks = [];
|
|
123
126
|
this.openBlock = null;
|
|
127
|
+
/** Requests this run has answered. A stream read without a cursor (a duplicate post's reconnect, a resume before
|
|
128
|
+
* any event id) replays the turn from its start, these requests too: one is a marker, not a pause. */
|
|
129
|
+
this.answered = new Set();
|
|
130
|
+
/** A full-turn replay repeats what this run already showed: emit nothing until it passes an answered request. */
|
|
131
|
+
this.replaying = false;
|
|
124
132
|
}
|
|
125
133
|
get text() {
|
|
126
134
|
return this.blocks.map(([, parts]) => parts.join("")).join("");
|
|
@@ -198,14 +206,17 @@ class Loop {
|
|
|
198
206
|
start(agentId, conversationId, input) {
|
|
199
207
|
return this.open(() => this.transport.startTurn(agentId, conversationId, input, this.signal));
|
|
200
208
|
}
|
|
201
|
-
/** Read one turn segment to turn_finished, resuming dropped streams; return
|
|
209
|
+
/** Read one turn segment to turn_finished, resuming dropped streams; return what it paused on, if anything. */
|
|
202
210
|
async consume(stream) {
|
|
203
|
-
let
|
|
211
|
+
let pause = null;
|
|
204
212
|
let reconnects = 0;
|
|
205
213
|
const itemsAtStart = this.items.length;
|
|
214
|
+
// The last pause read is one this run answered (kept across a resume, which goes on from where it was).
|
|
215
|
+
let afterAnswered = false;
|
|
206
216
|
for (;;) {
|
|
207
217
|
let finished = false;
|
|
208
218
|
const current = stream;
|
|
219
|
+
this.replaying = current.replay === true;
|
|
209
220
|
const onAbort = () => current.close();
|
|
210
221
|
this.signal.addEventListener("abort", onAbort, { once: true });
|
|
211
222
|
try {
|
|
@@ -219,8 +230,22 @@ class Loop {
|
|
|
219
230
|
finished = true;
|
|
220
231
|
break;
|
|
221
232
|
}
|
|
233
|
+
if ((event.kind === "clarify_requested" || event.kind === "function_calls_requested")
|
|
234
|
+
&& this.answered.has(event.requestId)) {
|
|
235
|
+
// Replayed up to a pause this run already answered: what came before it is not new.
|
|
236
|
+
this.items.length = itemsAtStart;
|
|
237
|
+
this.pastAnswered();
|
|
238
|
+
this.replaying = false;
|
|
239
|
+
afterAnswered = true;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (event.kind === "clarify_requested" || event.kind === "function_calls_requested")
|
|
243
|
+
afterAnswered = false;
|
|
244
|
+
// The skip of a pause this run answered (a replay): not the turn's.
|
|
245
|
+
if (afterAnswered && event.kind === "output" && event.skipped === "paused")
|
|
246
|
+
continue;
|
|
222
247
|
if (this.handle(event))
|
|
223
|
-
|
|
248
|
+
pause = event;
|
|
224
249
|
}
|
|
225
250
|
}
|
|
226
251
|
catch (err) {
|
|
@@ -237,7 +262,7 @@ class Loop {
|
|
|
237
262
|
if (this.signal.aborted)
|
|
238
263
|
throw new Cancelled();
|
|
239
264
|
if (finished)
|
|
240
|
-
return
|
|
265
|
+
return pause;
|
|
241
266
|
const responseId = current.responseId;
|
|
242
267
|
const lastEventId = current.lastEventId;
|
|
243
268
|
if (reconnects >= MAX_RECONNECTS || this.conversationId === null) {
|
|
@@ -252,7 +277,7 @@ class Loop {
|
|
|
252
277
|
stream = await this.open(() => this.transport.resume(conversationId, responseId, lastEventId, this.signal));
|
|
253
278
|
}
|
|
254
279
|
}
|
|
255
|
-
/** Handle one event other than turn_finished; returns true for a clarify request. */
|
|
280
|
+
/** Handle one event other than turn_finished; returns true for a clarify or function-call request. */
|
|
256
281
|
handle(event) {
|
|
257
282
|
switch (event.kind) {
|
|
258
283
|
case "turn_failed":
|
|
@@ -274,22 +299,23 @@ class Loop {
|
|
|
274
299
|
return false;
|
|
275
300
|
case "text_delta":
|
|
276
301
|
this.onText(event);
|
|
277
|
-
this.
|
|
302
|
+
if (!this.replaying) {
|
|
303
|
+
this.emit({ type: "raw_model_stream_event", data: { type: "output_text_delta", delta: event.delta } });
|
|
304
|
+
}
|
|
278
305
|
return false;
|
|
279
306
|
case "reasoning_delta":
|
|
280
|
-
this.
|
|
307
|
+
if (!this.replaying) {
|
|
308
|
+
this.emit({ type: "raw_model_stream_event", data: { type: "reasoning_delta", delta: event.delta } });
|
|
309
|
+
}
|
|
281
310
|
return false;
|
|
282
311
|
default:
|
|
283
312
|
break;
|
|
284
313
|
}
|
|
285
314
|
this.items.push(event);
|
|
286
|
-
if (event.kind === "tool_called" || event.kind === "tool_output") {
|
|
315
|
+
if ((event.kind === "tool_called" || event.kind === "tool_output") && !this.replaying) {
|
|
287
316
|
this.emit({ type: "run_item_stream_event", name: event.kind, item: event });
|
|
288
317
|
}
|
|
289
|
-
|
|
290
|
-
throw new errors_js_1.NotSupportedError("function tools are not supported yet");
|
|
291
|
-
}
|
|
292
|
-
return event.kind === "clarify_requested";
|
|
318
|
+
return event.kind === "clarify_requested" || event.kind === "function_calls_requested";
|
|
293
319
|
}
|
|
294
320
|
async ask(questions) {
|
|
295
321
|
const handler = this.onClarify;
|
|
@@ -300,13 +326,51 @@ class Loop {
|
|
|
300
326
|
}
|
|
301
327
|
return answers;
|
|
302
328
|
}
|
|
329
|
+
/** Past a pause this run answers: its text and its `outputSkipped: "paused"` belong to the paused segment, and the
|
|
330
|
+
* continuation (or its replay) says what the turn's own are. */
|
|
331
|
+
pastAnswered() {
|
|
332
|
+
this.resetText();
|
|
333
|
+
this.output = undefined;
|
|
334
|
+
}
|
|
335
|
+
/** Mark `requestId` answered by this run, before its answer is sent. */
|
|
336
|
+
markAnswered(requestId) {
|
|
337
|
+
this.answered.add(requestId);
|
|
338
|
+
this.pastAnswered();
|
|
339
|
+
}
|
|
303
340
|
answer(requestId, answers) {
|
|
341
|
+
this.markAnswered(requestId);
|
|
304
342
|
const conversationId = this.conversationId;
|
|
305
343
|
if (conversationId === null)
|
|
306
344
|
throw new errors_js_1.RunError("stream_lost", "no conversation to answer the question in");
|
|
307
345
|
return this.open(() => this.transport.answerClarify(conversationId, requestId, answers, this.signal));
|
|
308
346
|
}
|
|
309
|
-
/** Run
|
|
347
|
+
/** Run the request's calls here and post their results; the returned stream continues the turn. */
|
|
348
|
+
async answerFunctions(request) {
|
|
349
|
+
const conversationId = this.conversationId;
|
|
350
|
+
if (conversationId === null)
|
|
351
|
+
throw new errors_js_1.RunError("stream_lost", "no conversation to answer the function calls in");
|
|
352
|
+
const transport = this.transport;
|
|
353
|
+
if (transport.postFunctionResults === undefined) {
|
|
354
|
+
throw new errors_js_1.NotSupportedError("this transport cannot post function results");
|
|
355
|
+
}
|
|
356
|
+
let results;
|
|
357
|
+
try {
|
|
358
|
+
// Raced against the signal: an execute that never settles must not keep a cancelled run alive.
|
|
359
|
+
results = await raceAbort((0, functionTool_js_1.runCalls)(this.functions, request.calls, { signal: this.signal, raiseErrors: this.raiseErrors }), this.signal);
|
|
360
|
+
}
|
|
361
|
+
catch (err) {
|
|
362
|
+
if (err instanceof functionTool_js_1.CallsCancelled)
|
|
363
|
+
throw new Cancelled();
|
|
364
|
+
throw err;
|
|
365
|
+
}
|
|
366
|
+
if (this.signal.aborted)
|
|
367
|
+
throw new Cancelled();
|
|
368
|
+
this.markAnswered(request.requestId);
|
|
369
|
+
const post = transport.postFunctionResults.bind(transport);
|
|
370
|
+
return this.open(() => post(conversationId, request.requestId, results, this.signal));
|
|
371
|
+
}
|
|
372
|
+
/** Run to completion, running function calls and answering questions with onClarify; return an Interruption if a
|
|
373
|
+
* question has no handler. */
|
|
310
374
|
async drive(stream) {
|
|
311
375
|
for (;;) {
|
|
312
376
|
const clarify = await this.consume(stream);
|
|
@@ -314,6 +378,10 @@ class Loop {
|
|
|
314
378
|
this.emit({ type: "run_item_stream_event", name: "message_output_created", item: this.text });
|
|
315
379
|
return null;
|
|
316
380
|
}
|
|
381
|
+
if (clarify.kind === "function_calls_requested") {
|
|
382
|
+
stream = await this.answerFunctions(clarify);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
317
385
|
if (this.onClarify === undefined) {
|
|
318
386
|
return { kind: "clarify", requestId: clarify.requestId, payload: { questions: clarify.questions.slice() } };
|
|
319
387
|
}
|
|
@@ -322,7 +390,6 @@ class Loop {
|
|
|
322
390
|
const answers = await this.ask(clarify.questions);
|
|
323
391
|
if (this.signal.aborted)
|
|
324
392
|
throw new Cancelled();
|
|
325
|
-
this.resetText();
|
|
326
393
|
stream = await this.answer(clarify.requestId, answers);
|
|
327
394
|
}
|
|
328
395
|
}
|
|
@@ -330,9 +397,31 @@ class Loop {
|
|
|
330
397
|
async answerPending(state) {
|
|
331
398
|
this.conversationId = state.conversationId;
|
|
332
399
|
this.onConversation(state.conversationId);
|
|
400
|
+
// A saved state can be stale: another process may have answered its calls since. Run only what the server still
|
|
401
|
+
// waits on, so no function's side effects happen twice.
|
|
402
|
+
const transport = this.transport;
|
|
403
|
+
let waiting = null;
|
|
404
|
+
if (state.pending.some((item) => item.kind === "function_call")
|
|
405
|
+
&& transport.pending !== undefined && transport.follow !== undefined) {
|
|
406
|
+
const live = await raceAbort(transport.pending(state.conversationId), this.signal);
|
|
407
|
+
waiting = new Set(live.map((request) => request.requestId));
|
|
408
|
+
}
|
|
333
409
|
for (let n = 0; n < state.pending.length; n++) {
|
|
334
410
|
const item = state.pending[n];
|
|
335
|
-
|
|
411
|
+
let stream;
|
|
412
|
+
if (item.kind === "function_call" && waiting !== null && !waiting.has(item.requestId)) {
|
|
413
|
+
this.markAnswered(item.requestId);
|
|
414
|
+
const follow = transport.follow.bind(transport);
|
|
415
|
+
stream = await this.open(() => follow(state.conversationId, this.signal));
|
|
416
|
+
}
|
|
417
|
+
else
|
|
418
|
+
stream = item.kind === "function_call"
|
|
419
|
+
? await this.answerFunctions({
|
|
420
|
+
requestId: item.requestId,
|
|
421
|
+
calls: item.payload.calls.map((c) => ({ callId: c.call_id, name: c.name, arguments: { ...c.arguments } })),
|
|
422
|
+
})
|
|
423
|
+
: await this.answer(item.requestId, state.answers[item.requestId]);
|
|
424
|
+
const interruption = await this.drive(stream);
|
|
336
425
|
if (interruption !== null)
|
|
337
426
|
return [interruption, ...state.pending.slice(n + 1)];
|
|
338
427
|
}
|
|
@@ -349,7 +438,10 @@ function checkMaxTurns(opts) {
|
|
|
349
438
|
function checkInput(input) {
|
|
350
439
|
if (input instanceof runState_js_1.RunState) {
|
|
351
440
|
for (const item of input.pending) {
|
|
441
|
+
if (item.kind === "function_call")
|
|
442
|
+
continue; // run here when the state is resumed; nothing to answer
|
|
352
443
|
if (item.kind !== "clarify") {
|
|
444
|
+
// fromString keeps kinds it doesn't know, so this is reachable at runtime.
|
|
353
445
|
throw new errors_js_1.NotSupportedError(`resuming "${item.kind}" interruptions is not supported yet`);
|
|
354
446
|
}
|
|
355
447
|
if (!Object.prototype.hasOwnProperty.call(input.answers, item.requestId)) {
|
|
@@ -393,13 +485,14 @@ function structuredOutput(outputType, event) {
|
|
|
393
485
|
return undefined;
|
|
394
486
|
}
|
|
395
487
|
async function execute(p, controller, emit) {
|
|
396
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
488
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
397
489
|
const signal = controller.signal;
|
|
398
490
|
const unlink = link(p.opts.signal, controller);
|
|
399
491
|
const stopper = new Stopper(p.transport, signal);
|
|
400
492
|
const onAbort = () => stopper.send();
|
|
401
493
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
402
|
-
const
|
|
494
|
+
const functions = new Map(p.agent.functionTools.map((t) => [t.name, t]));
|
|
495
|
+
const loop = new Loop(p.transport, p.opts.onClarify, emit, signal, (c) => stopper.onConversation(c), functions, (_a = p.opts.raiseErrors) !== null && _a !== void 0 ? _a : false);
|
|
403
496
|
let interruptions = [];
|
|
404
497
|
let cancelled = false;
|
|
405
498
|
const fallbackConversation = p.input instanceof runState_js_1.RunState ? p.input.conversationId : p.opts.conversationId;
|
|
@@ -411,7 +504,7 @@ async function execute(p, controller, emit) {
|
|
|
411
504
|
}
|
|
412
505
|
else {
|
|
413
506
|
const agentId = await raceAbort((0, deploy_js_1.agentIdFor)(p.agent, p.transport, p.owner), signal);
|
|
414
|
-
const stream = await loop.start(agentId, (
|
|
507
|
+
const stream = await loop.start(agentId, (_b = p.opts.conversationId) !== null && _b !== void 0 ? _b : null, p.input);
|
|
415
508
|
const interruption = await loop.drive(stream);
|
|
416
509
|
interruptions = interruption ? [interruption] : [];
|
|
417
510
|
}
|
|
@@ -439,7 +532,7 @@ async function execute(p, controller, emit) {
|
|
|
439
532
|
}
|
|
440
533
|
catch (err) {
|
|
441
534
|
if (err instanceof errors_js_1.ModelBehaviorError)
|
|
442
|
-
err.conversationId = (
|
|
535
|
+
err.conversationId = (_c = loop.conversationId) !== null && _c !== void 0 ? _c : fallbackConversation;
|
|
443
536
|
throw err;
|
|
444
537
|
}
|
|
445
538
|
}
|
|
@@ -451,12 +544,12 @@ async function execute(p, controller, emit) {
|
|
|
451
544
|
newItems: loop.items,
|
|
452
545
|
interruptions,
|
|
453
546
|
stopReason: loop.stopReason,
|
|
454
|
-
conversationId: (
|
|
547
|
+
conversationId: (_d = loop.conversationId) !== null && _d !== void 0 ? _d : fallbackConversation,
|
|
455
548
|
cancelled,
|
|
456
|
-
outputCitations: ((
|
|
457
|
-
outputNote: (
|
|
458
|
-
outputSkipped: ((
|
|
459
|
-
outputMode: ((
|
|
549
|
+
outputCitations: ((_e = loop.output) === null || _e === void 0 ? void 0 : _e.kind) === "output" ? ((_f = loop.output.citations) !== null && _f !== void 0 ? _f : []).slice() : [],
|
|
550
|
+
outputNote: (_g = loop.output) === null || _g === void 0 ? void 0 : _g.note,
|
|
551
|
+
outputSkipped: ((_h = loop.output) === null || _h === void 0 ? void 0 : _h.kind) === "output" ? loop.output.skipped : undefined,
|
|
552
|
+
outputMode: ((_j = loop.output) === null || _j === void 0 ? void 0 : _j.kind) === "output" ? loop.output.mode : undefined,
|
|
460
553
|
};
|
|
461
554
|
}
|
|
462
555
|
// -- streaming ---------------------------------------------------------------------------------------------------
|
|
@@ -1,14 +1,31 @@
|
|
|
1
1
|
/** RunState: what a paused run needs to continue (conversation, pending interruptions and the caller's answers). */
|
|
2
2
|
import type { Agent } from "./agent.js";
|
|
3
3
|
import type { OutputType } from "./output.js";
|
|
4
|
+
import type { Configuration } from "../runtime.js";
|
|
4
5
|
import type { ClarifyQuestion } from "./events.js";
|
|
5
|
-
|
|
6
|
+
import type { Transport } from "./transport.js";
|
|
7
|
+
/** One function call waiting on this process, in the JSON shape Python's `RunState` uses. */
|
|
8
|
+
export interface PendingFunctionCall {
|
|
9
|
+
call_id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
arguments: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
export type Interruption = {
|
|
6
14
|
kind: "clarify";
|
|
7
15
|
requestId: string;
|
|
8
16
|
payload: {
|
|
9
17
|
questions: ClarifyQuestion[];
|
|
10
18
|
};
|
|
11
19
|
}
|
|
20
|
+
/** Calls a stopped process left waiting (`RunState.fromSession`); `run(agent, state)` runs them. */
|
|
21
|
+
| {
|
|
22
|
+
kind: "function_call";
|
|
23
|
+
requestId: string;
|
|
24
|
+
payload: {
|
|
25
|
+
calls: PendingFunctionCall[];
|
|
26
|
+
expires_at: string | null;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
12
29
|
export declare class RunState {
|
|
13
30
|
readonly conversationId: string;
|
|
14
31
|
readonly pending: Interruption[];
|
|
@@ -18,6 +35,14 @@ export declare class RunState {
|
|
|
18
35
|
readonly transportKind: string;
|
|
19
36
|
/** @internal Use `result.state` or `RunState.fromString`. */
|
|
20
37
|
constructor(conversationId: string, pending: Interruption[], answers?: Record<string, Record<string, unknown>>, transportKind?: string);
|
|
38
|
+
/**
|
|
39
|
+
* What the conversation is waiting on, read from the server: the function calls of a run whose process stopped
|
|
40
|
+
* before answering them. `run(agent, state)` then runs them here and continues the turn.
|
|
41
|
+
*/
|
|
42
|
+
static fromSession(conversationId: string, opts?: {
|
|
43
|
+
client?: Configuration;
|
|
44
|
+
transport?: Transport;
|
|
45
|
+
}): Promise<RunState>;
|
|
21
46
|
/** Record the answers to a pending interruption; `run(agent, state)` sends them. */
|
|
22
47
|
answer(item: Interruption, answers: Record<string, unknown>): void;
|
|
23
48
|
/** JSON in the format Python's `RunState.to_json()` writes. */
|
package/dist/compose/runState.js
CHANGED
|
@@ -14,11 +14,41 @@ class RunState {
|
|
|
14
14
|
this.answers = answers;
|
|
15
15
|
this.transportKind = transportKind;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* What the conversation is waiting on, read from the server: the function calls of a run whose process stopped
|
|
19
|
+
* before answering them. `run(agent, state)` then runs them here and continues the turn.
|
|
20
|
+
*/
|
|
21
|
+
static async fromSession(conversationId, opts = {}) {
|
|
22
|
+
const { resolveTransport } = await Promise.resolve().then(() => require("./deploy.js")); // deploy imports run, which imports this module
|
|
23
|
+
const transport = resolveTransport(opts.client, opts.transport);
|
|
24
|
+
if (typeof conversationId !== "string" || conversationId === "") {
|
|
25
|
+
throw new errors_js_1.UserError("fromSession needs a conversation id (a previous result.conversationId)");
|
|
26
|
+
}
|
|
27
|
+
if (transport.pending === undefined) {
|
|
28
|
+
throw new errors_js_1.NotSupportedError("this transport cannot list pending function calls");
|
|
29
|
+
}
|
|
30
|
+
const pending = (await transport.pending(conversationId)).map((r) => {
|
|
31
|
+
var _a;
|
|
32
|
+
return ({
|
|
33
|
+
kind: "function_call",
|
|
34
|
+
requestId: r.requestId,
|
|
35
|
+
payload: {
|
|
36
|
+
calls: r.calls.map((c) => ({ call_id: c.callId, name: c.name, arguments: { ...c.arguments } })),
|
|
37
|
+
expires_at: (_a = r.expiresAt) !== null && _a !== void 0 ? _a : null,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
return new RunState(conversationId, pending, {});
|
|
42
|
+
}
|
|
17
43
|
/** Record the answers to a pending interruption; `run(agent, state)` sends them. */
|
|
18
44
|
answer(item, answers) {
|
|
19
45
|
if (!this.pending.some((p) => p.requestId === item.requestId)) {
|
|
20
46
|
throw new errors_js_1.UserError(`no pending item with request id "${item.requestId}"`);
|
|
21
47
|
}
|
|
48
|
+
if (item.kind === "function_call") {
|
|
49
|
+
throw new errors_js_1.UserError("function calls are answered by running the agent's function tools: pass this state to " +
|
|
50
|
+
"run() with the agent");
|
|
51
|
+
}
|
|
22
52
|
this.answers[item.requestId] = { ...answers };
|
|
23
53
|
}
|
|
24
54
|
/** JSON in the format Python's `RunState.to_json()` writes. */
|
|
@@ -1,23 +1,50 @@
|
|
|
1
1
|
import type { Configuration } from "../runtime.js";
|
|
2
2
|
import type { Agent } from "./agent.js";
|
|
3
3
|
import type { OutputType } from "./output.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { FunctionResult } from "./functionTool.js";
|
|
5
|
+
import type { PendingFunctionCalls, RemoteAgent, Transport, TurnStream } from "./transport.js";
|
|
6
|
+
/** After a 409 expired or already_answered the server goes on with the turn; chat_service may still be picking that
|
|
7
|
+
* continuation up. The conversation is read again every `followPollMs.value` ms, at most FOLLOW_POLLS times. Mutable
|
|
8
|
+
* for tests; not exported from the compose root. */
|
|
9
|
+
export declare const followPollMs: {
|
|
10
|
+
value: number;
|
|
11
|
+
};
|
|
12
|
+
/** The `Idempotency-Key` for answering `requestId`: the same for every post of it, from any process. */
|
|
13
|
+
export declare function idempotencyKey(requestId: string): string;
|
|
5
14
|
export declare class SettingsTransport implements Transport {
|
|
6
15
|
private readonly config;
|
|
7
16
|
readonly kind: "settings";
|
|
8
17
|
private readonly agents;
|
|
9
18
|
private readonly conversations;
|
|
10
19
|
private readonly responses;
|
|
20
|
+
private readonly customApis;
|
|
11
21
|
constructor(config: Configuration);
|
|
12
22
|
findAgents(sdkName: string): Promise<RemoteAgent[]>;
|
|
13
23
|
readAgent(agentId: string): Promise<RemoteAgent>;
|
|
14
24
|
createAgent(agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
|
|
25
|
+
/** The caller's Custom APIs the webhooks name, to bind to a new agent: one entry per name that matches exactly
|
|
26
|
+
* one, and a sentence per name that matches none or several (left to the server, which refuses it only when it
|
|
27
|
+
* checks credentials). */
|
|
28
|
+
private webhookCredentials;
|
|
29
|
+
/** The ids of the caller's own Custom APIs named exactly `name` (`q` also matches substrings and hosts). */
|
|
30
|
+
private customApiIds;
|
|
31
|
+
private bind;
|
|
15
32
|
updateAgent(agentId: string, agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
|
|
16
33
|
deleteAgent(agentId: string): Promise<void>;
|
|
17
34
|
startTurn(agentId: string, conversationId: string | null, text: string, signal?: AbortSignal): Promise<TurnStream>;
|
|
18
35
|
answerClarify(conversationId: string, requestId: string, answers: Record<string, unknown>, signal?: AbortSignal): Promise<TurnStream>;
|
|
19
36
|
resume(conversationId: string, responseId: string, lastEventId: string | null, signal?: AbortSignal): Promise<TurnStream>;
|
|
20
37
|
stop(conversationId: string): Promise<void>;
|
|
38
|
+
postFunctionResults(conversationId: string, requestId: string, results: FunctionResult[], signal?: AbortSignal): Promise<TurnStream>;
|
|
39
|
+
/** The turn after a request someone already answered, as a replay of it from its start (the run skips up to the
|
|
40
|
+
* answered request): the running response by reconnecting without a cursor, else the conversation's last reply
|
|
41
|
+
* as stored. A turn that paused again ends on its new request, unanswered. */
|
|
42
|
+
follow(conversationId: string, signal?: AbortSignal): Promise<TurnStream>;
|
|
43
|
+
/** `follow`, after a 409 expired / already_answered for `requestId`: a turn not yet picked up again (no active
|
|
44
|
+
* response, and the last reply still ends on that request) is read again every second, up to 30 times; then
|
|
45
|
+
* RunError(reason). An abort ends the wait. */
|
|
46
|
+
private followAfter;
|
|
47
|
+
pending(conversationId: string): Promise<PendingFunctionCalls[]>;
|
|
21
48
|
/** Put back what updateAgent overwrote; a failing restore must not mask the original error. */
|
|
22
49
|
private restore;
|
|
23
50
|
private instructions;
|