sarvam-agents-client 0.2.2 → 0.2.3

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.
@@ -45,6 +45,16 @@ export type Event = {
45
45
  } | {
46
46
  kind: "stopped";
47
47
  reason: string;
48
+ }
49
+ /** An agent.json limit ended the turn (`data-turn-limit`); the wrap-up answer still follows. `capKind` is the
50
+ * server's cap name: `max_turns`, `tool_calls` or `tokens`. `limit` is the agent.json `limits` key that set it
51
+ * (`max_turns`, `max_tool_calls_per_turn` or `max_tokens_per_turn`) and `value` the configured number; both are
52
+ * absent from a server that predates them. */
53
+ | {
54
+ kind: "turn_limit";
55
+ capKind: string;
56
+ limit?: string;
57
+ value?: number;
48
58
  } | {
49
59
  kind: "turn_finished";
50
60
  reason: string;
@@ -87,6 +87,13 @@ function map(chunk) {
87
87
  ev.expiresAt = data["expiresAt"];
88
88
  return ev;
89
89
  }
90
+ case "data-turn-limit":
91
+ return {
92
+ kind: "turn_limit",
93
+ capKind: str(data["kind"], ""),
94
+ ...(typeof data["limit"] === "string" && data["limit"] ? { limit: data["limit"] } : {}),
95
+ ...(Number.isInteger(data["value"]) ? { value: data["value"] } : {}),
96
+ };
90
97
  case "data-stopped":
91
98
  return { kind: "stopped", reason: str(data["reason"], "stopped") };
92
99
  case "error":
@@ -8,6 +8,6 @@ export type { RemoteAgent, SettingsWarning, TurnStream, Transport } from "./tran
8
8
  export { deploy, undeploy, setDefaultClient } from "./deploy.js";
9
9
  export type { Deployment } from "./deploy.js";
10
10
  export { run, RunResult, StreamedRunResult } from "./run.js";
11
- export type { RunOptions, RunStreamEvent, ClarifyHandler } from "./run.js";
11
+ export type { RunOptions, RunStreamEvent, ClarifyHandler, StopReason } from "./run.js";
12
12
  export { RunState } from "./runState.js";
13
13
  export type { Interruption } from "./runState.js";
@@ -29,11 +29,14 @@ export type RunStreamEvent = {
29
29
  name: "tool_called" | "tool_output" | "message_output_created";
30
30
  item: unknown;
31
31
  };
32
+ /** Why the run ended early: the `limits` field whose cap the server hit. */
33
+ export type StopReason = "max_turns" | "max_tool_calls" | "max_tokens";
32
34
  interface Outcome {
33
35
  finalOutput: string | undefined;
34
36
  outputText: string;
35
37
  newItems: Event[];
36
38
  interruptions: Interruption[];
39
+ stopReason: StopReason | undefined;
37
40
  conversationId: string | undefined;
38
41
  cancelled: boolean;
39
42
  }
@@ -42,6 +45,8 @@ export declare class RunResult {
42
45
  readonly outputText: string;
43
46
  readonly newItems: Event[];
44
47
  readonly interruptions: Interruption[];
48
+ /** Set when an agent.json limit ended the turn; `finalOutput` still holds the wrap-up answer. */
49
+ readonly stopReason: StopReason | undefined;
45
50
  readonly conversationId: string | undefined;
46
51
  /** @internal */
47
52
  constructor(outcome: Outcome);
@@ -63,6 +68,8 @@ export declare class StreamedRunResult implements AsyncIterable<RunStreamEvent>
63
68
  outputText: string;
64
69
  newItems: Event[];
65
70
  interruptions: Interruption[];
71
+ /** Set when an agent.json limit ended the turn; read after `completed` settles. */
72
+ stopReason: StopReason | undefined;
66
73
  conversationId: string | undefined;
67
74
  cancelled: boolean;
68
75
  private readonly queue;
@@ -9,6 +9,12 @@ const runState_js_1 = require("./runState.js");
9
9
  const MAX_RECONNECTS = 3;
10
10
  /** The abort reason ResponseStream (src/streaming.ts) uses when a stream goes quiet. */
11
11
  const IDLE_TIMEOUT_MESSAGE = "stream idle timeout";
12
+ /** Server cap kind (`data-turn-limit`) -> stop reason, named like the `limits` field that set it. */
13
+ const STOP_REASONS = {
14
+ max_turns: "max_turns",
15
+ tool_calls: "max_tool_calls",
16
+ tokens: "max_tokens",
17
+ };
12
18
  function stateOf(conversationId, interruptions) {
13
19
  if (conversationId === undefined)
14
20
  throw new errors_js_1.UserError("this run has no conversation to resume");
@@ -21,6 +27,7 @@ class RunResult {
21
27
  this.outputText = outcome.outputText;
22
28
  this.newItems = outcome.newItems;
23
29
  this.interruptions = outcome.interruptions;
30
+ this.stopReason = outcome.stopReason;
24
31
  this.conversationId = outcome.conversationId;
25
32
  }
26
33
  /** A RunState to answer the interruptions and continue; throws UserError when the run has no conversation. */
@@ -102,6 +109,7 @@ class Loop {
102
109
  this.onConversation = onConversation;
103
110
  this.conversationId = null;
104
111
  this.items = [];
112
+ this.stopReason = undefined;
105
113
  // Text of the current segment, per text block. Kept per block so a resumed stream, which replays from the start
106
114
  // of the block it dropped in, doesn't repeat text.
107
115
  this.blocks = [];
@@ -246,6 +254,9 @@ class Loop {
246
254
  if (this.signal.aborted)
247
255
  throw new Cancelled();
248
256
  throw new errors_js_1.RunError("stopped", `the turn was stopped on the server (${event.reason})`);
257
+ case "turn_limit":
258
+ this.stopReason = STOP_REASONS[event.capKind];
259
+ return false;
249
260
  case "text_start":
250
261
  case "text_end":
251
262
  this.onText(event);
@@ -402,6 +413,7 @@ async function execute(p, controller, emit) {
402
413
  outputText: text,
403
414
  newItems: loop.items,
404
415
  interruptions,
416
+ stopReason: loop.stopReason,
405
417
  conversationId: (_b = loop.conversationId) !== null && _b !== void 0 ? _b : fallbackConversation,
406
418
  cancelled,
407
419
  };
@@ -415,6 +427,8 @@ class StreamedRunResult {
415
427
  this.outputText = "";
416
428
  this.newItems = [];
417
429
  this.interruptions = [];
430
+ /** Set when an agent.json limit ended the turn; read after `completed` settles. */
431
+ this.stopReason = undefined;
418
432
  this.conversationId = undefined;
419
433
  this.cancelled = false;
420
434
  this.queue = [];
@@ -429,6 +443,7 @@ class StreamedRunResult {
429
443
  this.outputText = outcome.outputText;
430
444
  this.newItems = outcome.newItems;
431
445
  this.interruptions = outcome.interruptions;
446
+ this.stopReason = outcome.stopReason;
432
447
  this.conversationId = outcome.conversationId;
433
448
  this.cancelled = outcome.cancelled;
434
449
  this.end();
@@ -45,6 +45,16 @@ export type Event = {
45
45
  } | {
46
46
  kind: "stopped";
47
47
  reason: string;
48
+ }
49
+ /** An agent.json limit ended the turn (`data-turn-limit`); the wrap-up answer still follows. `capKind` is the
50
+ * server's cap name: `max_turns`, `tool_calls` or `tokens`. `limit` is the agent.json `limits` key that set it
51
+ * (`max_turns`, `max_tool_calls_per_turn` or `max_tokens_per_turn`) and `value` the configured number; both are
52
+ * absent from a server that predates them. */
53
+ | {
54
+ kind: "turn_limit";
55
+ capKind: string;
56
+ limit?: string;
57
+ value?: number;
48
58
  } | {
49
59
  kind: "turn_finished";
50
60
  reason: string;
@@ -84,6 +84,13 @@ function map(chunk) {
84
84
  ev.expiresAt = data["expiresAt"];
85
85
  return ev;
86
86
  }
87
+ case "data-turn-limit":
88
+ return {
89
+ kind: "turn_limit",
90
+ capKind: str(data["kind"], ""),
91
+ ...(typeof data["limit"] === "string" && data["limit"] ? { limit: data["limit"] } : {}),
92
+ ...(Number.isInteger(data["value"]) ? { value: data["value"] } : {}),
93
+ };
87
94
  case "data-stopped":
88
95
  return { kind: "stopped", reason: str(data["reason"], "stopped") };
89
96
  case "error":
@@ -8,6 +8,6 @@ export type { RemoteAgent, SettingsWarning, TurnStream, Transport } from "./tran
8
8
  export { deploy, undeploy, setDefaultClient } from "./deploy.js";
9
9
  export type { Deployment } from "./deploy.js";
10
10
  export { run, RunResult, StreamedRunResult } from "./run.js";
11
- export type { RunOptions, RunStreamEvent, ClarifyHandler } from "./run.js";
11
+ export type { RunOptions, RunStreamEvent, ClarifyHandler, StopReason } from "./run.js";
12
12
  export { RunState } from "./runState.js";
13
13
  export type { Interruption } from "./runState.js";
@@ -29,11 +29,14 @@ export type RunStreamEvent = {
29
29
  name: "tool_called" | "tool_output" | "message_output_created";
30
30
  item: unknown;
31
31
  };
32
+ /** Why the run ended early: the `limits` field whose cap the server hit. */
33
+ export type StopReason = "max_turns" | "max_tool_calls" | "max_tokens";
32
34
  interface Outcome {
33
35
  finalOutput: string | undefined;
34
36
  outputText: string;
35
37
  newItems: Event[];
36
38
  interruptions: Interruption[];
39
+ stopReason: StopReason | undefined;
37
40
  conversationId: string | undefined;
38
41
  cancelled: boolean;
39
42
  }
@@ -42,6 +45,8 @@ export declare class RunResult {
42
45
  readonly outputText: string;
43
46
  readonly newItems: Event[];
44
47
  readonly interruptions: Interruption[];
48
+ /** Set when an agent.json limit ended the turn; `finalOutput` still holds the wrap-up answer. */
49
+ readonly stopReason: StopReason | undefined;
45
50
  readonly conversationId: string | undefined;
46
51
  /** @internal */
47
52
  constructor(outcome: Outcome);
@@ -63,6 +68,8 @@ export declare class StreamedRunResult implements AsyncIterable<RunStreamEvent>
63
68
  outputText: string;
64
69
  newItems: Event[];
65
70
  interruptions: Interruption[];
71
+ /** Set when an agent.json limit ended the turn; read after `completed` settles. */
72
+ stopReason: StopReason | undefined;
66
73
  conversationId: string | undefined;
67
74
  cancelled: boolean;
68
75
  private readonly queue;
@@ -5,6 +5,12 @@ import { RunState } from "./runState.js";
5
5
  const MAX_RECONNECTS = 3;
6
6
  /** The abort reason ResponseStream (src/streaming.ts) uses when a stream goes quiet. */
7
7
  const IDLE_TIMEOUT_MESSAGE = "stream idle timeout";
8
+ /** Server cap kind (`data-turn-limit`) -> stop reason, named like the `limits` field that set it. */
9
+ const STOP_REASONS = {
10
+ max_turns: "max_turns",
11
+ tool_calls: "max_tool_calls",
12
+ tokens: "max_tokens",
13
+ };
8
14
  function stateOf(conversationId, interruptions) {
9
15
  if (conversationId === undefined)
10
16
  throw new UserError("this run has no conversation to resume");
@@ -17,6 +23,7 @@ export class RunResult {
17
23
  this.outputText = outcome.outputText;
18
24
  this.newItems = outcome.newItems;
19
25
  this.interruptions = outcome.interruptions;
26
+ this.stopReason = outcome.stopReason;
20
27
  this.conversationId = outcome.conversationId;
21
28
  }
22
29
  /** A RunState to answer the interruptions and continue; throws UserError when the run has no conversation. */
@@ -97,6 +104,7 @@ class Loop {
97
104
  this.onConversation = onConversation;
98
105
  this.conversationId = null;
99
106
  this.items = [];
107
+ this.stopReason = undefined;
100
108
  // Text of the current segment, per text block. Kept per block so a resumed stream, which replays from the start
101
109
  // of the block it dropped in, doesn't repeat text.
102
110
  this.blocks = [];
@@ -241,6 +249,9 @@ class Loop {
241
249
  if (this.signal.aborted)
242
250
  throw new Cancelled();
243
251
  throw new RunError("stopped", `the turn was stopped on the server (${event.reason})`);
252
+ case "turn_limit":
253
+ this.stopReason = STOP_REASONS[event.capKind];
254
+ return false;
244
255
  case "text_start":
245
256
  case "text_end":
246
257
  this.onText(event);
@@ -397,6 +408,7 @@ async function execute(p, controller, emit) {
397
408
  outputText: text,
398
409
  newItems: loop.items,
399
410
  interruptions,
411
+ stopReason: loop.stopReason,
400
412
  conversationId: (_b = loop.conversationId) !== null && _b !== void 0 ? _b : fallbackConversation,
401
413
  cancelled,
402
414
  };
@@ -410,6 +422,8 @@ export class StreamedRunResult {
410
422
  this.outputText = "";
411
423
  this.newItems = [];
412
424
  this.interruptions = [];
425
+ /** Set when an agent.json limit ended the turn; read after `completed` settles. */
426
+ this.stopReason = undefined;
413
427
  this.conversationId = undefined;
414
428
  this.cancelled = false;
415
429
  this.queue = [];
@@ -424,6 +438,7 @@ export class StreamedRunResult {
424
438
  this.outputText = outcome.outputText;
425
439
  this.newItems = outcome.newItems;
426
440
  this.interruptions = outcome.interruptions;
441
+ this.stopReason = outcome.stopReason;
427
442
  this.conversationId = outcome.conversationId;
428
443
  this.cancelled = outcome.cancelled;
429
444
  this.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sarvam-agents-client",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Client for the Sarvam Agents API: chat with an agent, exchange files with it, and set it up from your own code.",
5
5
  "author": "Sarvam AI <support@sarvam.ai>",
6
6
  "license": "MIT",
@@ -24,6 +24,11 @@ export type Event =
24
24
  | { kind: "clarify_requested"; requestId: string; questions: ClarifyQuestion[] }
25
25
  | { kind: "function_calls_requested"; requestId: string; calls: FunctionCallRequest[]; expiresAt?: string }
26
26
  | { kind: "stopped"; reason: string }
27
+ /** An agent.json limit ended the turn (`data-turn-limit`); the wrap-up answer still follows. `capKind` is the
28
+ * server's cap name: `max_turns`, `tool_calls` or `tokens`. `limit` is the agent.json `limits` key that set it
29
+ * (`max_turns`, `max_tool_calls_per_turn` or `max_tokens_per_turn`) and `value` the configured number; both are
30
+ * absent from a server that predates them. */
31
+ | { kind: "turn_limit"; capKind: string; limit?: string; value?: number }
27
32
  | { kind: "turn_finished"; reason: string }
28
33
  | { kind: "turn_failed"; code: string; message: string }
29
34
  | { kind: "unknown"; chunk: Record<string, unknown> };
@@ -109,6 +114,13 @@ function map(chunk: Record<string, unknown>): Event | null {
109
114
  if (typeof data["expiresAt"] === "string") ev.expiresAt = data["expiresAt"];
110
115
  return ev;
111
116
  }
117
+ case "data-turn-limit":
118
+ return {
119
+ kind: "turn_limit",
120
+ capKind: str(data["kind"], ""),
121
+ ...(typeof data["limit"] === "string" && data["limit"] ? { limit: data["limit"] } : {}),
122
+ ...(Number.isInteger(data["value"]) ? { value: data["value"] as number } : {}),
123
+ };
112
124
  case "data-stopped":
113
125
  return { kind: "stopped", reason: str(data["reason"], "stopped") };
114
126
  case "error":
@@ -8,6 +8,6 @@ export type { RemoteAgent, SettingsWarning, TurnStream, Transport } from "./tran
8
8
  export { deploy, undeploy, setDefaultClient } from "./deploy.js";
9
9
  export type { Deployment } from "./deploy.js";
10
10
  export { run, RunResult, StreamedRunResult } from "./run.js";
11
- export type { RunOptions, RunStreamEvent, ClarifyHandler } from "./run.js";
11
+ export type { RunOptions, RunStreamEvent, ClarifyHandler, StopReason } from "./run.js";
12
12
  export { RunState } from "./runState.js";
13
13
  export type { Interruption } from "./runState.js";
@@ -37,11 +37,22 @@ export type RunStreamEvent =
37
37
  type ClarifyEvent = Extract<Event, { kind: "clarify_requested" }>;
38
38
  type Emit = (event: RunStreamEvent) => void;
39
39
 
40
+ /** Why the run ended early: the `limits` field whose cap the server hit. */
41
+ export type StopReason = "max_turns" | "max_tool_calls" | "max_tokens";
42
+
43
+ /** Server cap kind (`data-turn-limit`) -> stop reason, named like the `limits` field that set it. */
44
+ const STOP_REASONS: Record<string, StopReason> = {
45
+ max_turns: "max_turns",
46
+ tool_calls: "max_tool_calls",
47
+ tokens: "max_tokens",
48
+ };
49
+
40
50
  interface Outcome {
41
51
  finalOutput: string | undefined;
42
52
  outputText: string;
43
53
  newItems: Event[];
44
54
  interruptions: Interruption[];
55
+ stopReason: StopReason | undefined;
45
56
  conversationId: string | undefined;
46
57
  cancelled: boolean;
47
58
  }
@@ -56,6 +67,8 @@ export class RunResult {
56
67
  readonly outputText: string;
57
68
  readonly newItems: Event[];
58
69
  readonly interruptions: Interruption[];
70
+ /** Set when an agent.json limit ended the turn; `finalOutput` still holds the wrap-up answer. */
71
+ readonly stopReason: StopReason | undefined;
59
72
  readonly conversationId: string | undefined;
60
73
 
61
74
  /** @internal */
@@ -64,6 +77,7 @@ export class RunResult {
64
77
  this.outputText = outcome.outputText;
65
78
  this.newItems = outcome.newItems;
66
79
  this.interruptions = outcome.interruptions;
80
+ this.stopReason = outcome.stopReason;
67
81
  this.conversationId = outcome.conversationId;
68
82
  }
69
83
 
@@ -145,6 +159,7 @@ class Stopper {
145
159
  class Loop {
146
160
  conversationId: string | null = null;
147
161
  readonly items: Event[] = [];
162
+ stopReason: StopReason | undefined = undefined;
148
163
  // Text of the current segment, per text block. Kept per block so a resumed stream, which replays from the start
149
164
  // of the block it dropped in, doesn't repeat text.
150
165
  private blocks: [string | null, string[]][] = [];
@@ -285,6 +300,9 @@ class Loop {
285
300
  case "stopped":
286
301
  if (this.signal.aborted) throw new Cancelled();
287
302
  throw new RunError("stopped", `the turn was stopped on the server (${event.reason})`);
303
+ case "turn_limit":
304
+ this.stopReason = STOP_REASONS[event.capKind];
305
+ return false;
288
306
  case "text_start":
289
307
  case "text_end":
290
308
  this.onText(event);
@@ -447,6 +465,7 @@ async function execute(p: Prepared, controller: AbortController, emit: Emit): Pr
447
465
  outputText: text,
448
466
  newItems: loop.items,
449
467
  interruptions,
468
+ stopReason: loop.stopReason,
450
469
  conversationId: loop.conversationId ?? fallbackConversation,
451
470
  cancelled,
452
471
  };
@@ -462,6 +481,8 @@ export class StreamedRunResult implements AsyncIterable<RunStreamEvent> {
462
481
  outputText = "";
463
482
  newItems: Event[] = [];
464
483
  interruptions: Interruption[] = [];
484
+ /** Set when an agent.json limit ended the turn; read after `completed` settles. */
485
+ stopReason: StopReason | undefined = undefined;
465
486
  conversationId: string | undefined = undefined;
466
487
  cancelled = false;
467
488
 
@@ -482,6 +503,7 @@ export class StreamedRunResult implements AsyncIterable<RunStreamEvent> {
482
503
  this.outputText = outcome.outputText;
483
504
  this.newItems = outcome.newItems;
484
505
  this.interruptions = outcome.interruptions;
506
+ this.stopReason = outcome.stopReason;
485
507
  this.conversationId = outcome.conversationId;
486
508
  this.cancelled = outcome.cancelled;
487
509
  this.end();