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.
Files changed (151) hide show
  1. package/dist/compat/core/agents.d.ts +53 -0
  2. package/dist/compat/core/agents.js +338 -0
  3. package/dist/compat/core/data.d.ts +10 -0
  4. package/dist/compat/core/data.js +275 -0
  5. package/dist/compat/core/errors.d.ts +37 -0
  6. package/dist/compat/core/errors.js +155 -0
  7. package/dist/compat/core/events.d.ts +107 -0
  8. package/dist/compat/core/events.js +160 -0
  9. package/dist/compat/core/files.d.ts +65 -0
  10. package/dist/compat/core/files.js +851 -0
  11. package/dist/compat/core/history.d.ts +9 -0
  12. package/dist/compat/core/history.js +112 -0
  13. package/dist/compat/core/http.d.ts +112 -0
  14. package/dist/compat/core/http.js +753 -0
  15. package/dist/compat/core/ids.d.ts +4 -0
  16. package/dist/compat/core/ids.js +36 -0
  17. package/dist/compat/core/index.d.ts +14 -0
  18. package/dist/compat/core/index.js +30 -0
  19. package/dist/compat/core/models.d.ts +20 -0
  20. package/dist/compat/core/models.js +75 -0
  21. package/dist/compat/core/paging.d.ts +14 -0
  22. package/dist/compat/core/paging.js +124 -0
  23. package/dist/compat/core/refusals.d.ts +9 -0
  24. package/dist/compat/core/refusals.js +25 -0
  25. package/dist/compat/core/sessions.d.ts +65 -0
  26. package/dist/compat/core/sessions.js +317 -0
  27. package/dist/compat/core/toolResults.d.ts +43 -0
  28. package/dist/compat/core/toolResults.js +208 -0
  29. package/dist/compat/core/turns.d.ts +104 -0
  30. package/dist/compat/core/turns.js +504 -0
  31. package/dist/compat/core/warnings.d.ts +6 -0
  32. package/dist/compat/core/warnings.js +26 -0
  33. package/dist/compat/index.d.ts +3 -0
  34. package/dist/compat/index.js +9 -0
  35. package/dist/compat/openai/client.d.ts +62 -0
  36. package/dist/compat/openai/client.js +108 -0
  37. package/dist/compat/openai/errors.d.ts +72 -0
  38. package/dist/compat/openai/errors.js +150 -0
  39. package/dist/compat/openai/index.d.ts +14 -0
  40. package/dist/compat/openai/index.js +42 -0
  41. package/dist/compat/openai/pages.d.ts +19 -0
  42. package/dist/compat/openai/pages.js +39 -0
  43. package/dist/compat/openai/resources/agents.d.ts +36 -0
  44. package/dist/compat/openai/resources/agents.js +549 -0
  45. package/dist/compat/openai/resources/events.d.ts +23 -0
  46. package/dist/compat/openai/resources/events.js +485 -0
  47. package/dist/compat/openai/resources/sessions.d.ts +33 -0
  48. package/dist/compat/openai/resources/sessions.js +283 -0
  49. package/dist/compat/openai/stream.d.ts +40 -0
  50. package/dist/compat/openai/stream.js +585 -0
  51. package/dist/compat/openai/types.d.ts +125 -0
  52. package/dist/compat/openai/types.js +98 -0
  53. package/dist/compose/agent.d.ts +2 -0
  54. package/dist/compose/agent.js +27 -0
  55. package/dist/compose/functionTool.d.ts +2 -0
  56. package/dist/compose/functionTool.js +46 -0
  57. package/dist/compose/output.d.ts +2 -0
  58. package/dist/compose/output.js +15 -7
  59. package/dist/esm/compat/core/agents.d.ts +53 -0
  60. package/dist/esm/compat/core/agents.js +334 -0
  61. package/dist/esm/compat/core/data.d.ts +10 -0
  62. package/dist/esm/compat/core/data.js +272 -0
  63. package/dist/esm/compat/core/errors.d.ts +37 -0
  64. package/dist/esm/compat/core/errors.js +146 -0
  65. package/dist/esm/compat/core/events.d.ts +107 -0
  66. package/dist/esm/compat/core/events.js +156 -0
  67. package/dist/esm/compat/core/files.d.ts +65 -0
  68. package/dist/esm/compat/core/files.js +829 -0
  69. package/dist/esm/compat/core/history.d.ts +9 -0
  70. package/dist/esm/compat/core/history.js +109 -0
  71. package/dist/esm/compat/core/http.d.ts +112 -0
  72. package/dist/esm/compat/core/http.js +746 -0
  73. package/dist/esm/compat/core/ids.d.ts +4 -0
  74. package/dist/esm/compat/core/ids.js +30 -0
  75. package/dist/esm/compat/core/index.d.ts +14 -0
  76. package/dist/esm/compat/core/index.js +14 -0
  77. package/dist/esm/compat/core/models.d.ts +20 -0
  78. package/dist/esm/compat/core/models.js +71 -0
  79. package/dist/esm/compat/core/paging.d.ts +14 -0
  80. package/dist/esm/compat/core/paging.js +118 -0
  81. package/dist/esm/compat/core/refusals.d.ts +9 -0
  82. package/dist/esm/compat/core/refusals.js +21 -0
  83. package/dist/esm/compat/core/sessions.d.ts +65 -0
  84. package/dist/esm/compat/core/sessions.js +312 -0
  85. package/dist/esm/compat/core/toolResults.d.ts +43 -0
  86. package/dist/esm/compat/core/toolResults.js +203 -0
  87. package/dist/esm/compat/core/turns.d.ts +104 -0
  88. package/dist/esm/compat/core/turns.js +497 -0
  89. package/dist/esm/compat/core/warnings.d.ts +6 -0
  90. package/dist/esm/compat/core/warnings.js +20 -0
  91. package/dist/esm/compat/index.d.ts +3 -0
  92. package/dist/esm/compat/index.js +3 -0
  93. package/dist/esm/compat/openai/client.d.ts +62 -0
  94. package/dist/esm/compat/openai/client.js +104 -0
  95. package/dist/esm/compat/openai/errors.d.ts +72 -0
  96. package/dist/esm/compat/openai/errors.js +130 -0
  97. package/dist/esm/compat/openai/index.d.ts +14 -0
  98. package/dist/esm/compat/openai/index.js +17 -0
  99. package/dist/esm/compat/openai/pages.d.ts +19 -0
  100. package/dist/esm/compat/openai/pages.js +35 -0
  101. package/dist/esm/compat/openai/resources/agents.d.ts +36 -0
  102. package/dist/esm/compat/openai/resources/agents.js +540 -0
  103. package/dist/esm/compat/openai/resources/events.d.ts +23 -0
  104. package/dist/esm/compat/openai/resources/events.js +479 -0
  105. package/dist/esm/compat/openai/resources/sessions.d.ts +33 -0
  106. package/dist/esm/compat/openai/resources/sessions.js +278 -0
  107. package/dist/esm/compat/openai/stream.d.ts +40 -0
  108. package/dist/esm/compat/openai/stream.js +579 -0
  109. package/dist/esm/compat/openai/types.d.ts +125 -0
  110. package/dist/esm/compat/openai/types.js +91 -0
  111. package/dist/esm/compose/agent.d.ts +2 -0
  112. package/dist/esm/compose/agent.js +28 -2
  113. package/dist/esm/compose/functionTool.d.ts +2 -0
  114. package/dist/esm/compose/functionTool.js +45 -0
  115. package/dist/esm/compose/output.d.ts +2 -0
  116. package/dist/esm/compose/output.js +14 -7
  117. package/dist/esm/index.d.ts +2 -0
  118. package/dist/esm/index.js +2 -0
  119. package/dist/index.d.ts +2 -0
  120. package/dist/index.js +5 -1
  121. package/package.json +53 -11
  122. package/src/compat/core/agents.ts +437 -0
  123. package/src/compat/core/data.ts +283 -0
  124. package/src/compat/core/errors.ts +162 -0
  125. package/src/compat/core/events.ts +198 -0
  126. package/src/compat/core/files.ts +913 -0
  127. package/src/compat/core/history.ts +116 -0
  128. package/src/compat/core/http.ts +827 -0
  129. package/src/compat/core/ids.ts +35 -0
  130. package/src/compat/core/index.ts +14 -0
  131. package/src/compat/core/models.ts +85 -0
  132. package/src/compat/core/paging.ts +119 -0
  133. package/src/compat/core/refusals.ts +31 -0
  134. package/src/compat/core/sessions.ts +358 -0
  135. package/src/compat/core/toolResults.ts +235 -0
  136. package/src/compat/core/turns.ts +622 -0
  137. package/src/compat/core/warnings.ts +23 -0
  138. package/src/compat/index.ts +3 -0
  139. package/src/compat/openai/client.ts +177 -0
  140. package/src/compat/openai/errors.ts +165 -0
  141. package/src/compat/openai/index.ts +65 -0
  142. package/src/compat/openai/pages.ts +55 -0
  143. package/src/compat/openai/resources/agents.ts +603 -0
  144. package/src/compat/openai/resources/events.ts +523 -0
  145. package/src/compat/openai/resources/sessions.ts +369 -0
  146. package/src/compat/openai/stream.ts +599 -0
  147. package/src/compat/openai/types.ts +217 -0
  148. package/src/compose/agent.ts +30 -3
  149. package/src/compose/functionTool.ts +44 -0
  150. package/src/compose/output.ts +14 -8
  151. package/src/index.ts +3 -0
@@ -0,0 +1,235 @@
1
+ /** Per-step tool-result collector: buffers per-call results until a Panja step is complete. */
2
+ import { createHash } from "node:crypto";
3
+ import { fit } from "../../compose/functionTool.js";
4
+ import { postToolResults } from "../../streaming.js";
5
+ import { CoreAPIError } from "./errors.js";
6
+ import { Http } from "./http.js";
7
+ import { Sessions, type PendingStep } from "./sessions.js";
8
+ import { Turns } from "./turns.js";
9
+
10
+ export type SubmitOutcome = "held" | "posted" | "duplicate";
11
+
12
+ const BUFFER_TTL_MS = 3_600_000;
13
+ const MAX_BUFFERS = 1024;
14
+
15
+ interface BufferedResult {
16
+ output: string;
17
+ isError: boolean;
18
+ }
19
+
20
+ interface StepBuffer {
21
+ step: PendingStep;
22
+ results: Map<string, BufferedResult>;
23
+ callKeys: Map<string, string[]>;
24
+ createdAt: number;
25
+ postFailed: boolean;
26
+ idemKey: string | null;
27
+ }
28
+
29
+ function stepKey(requestId: string): string {
30
+ return createHash("sha256").update(requestId, "utf8").digest("hex");
31
+ }
32
+
33
+ export class ToolResultStore {
34
+ readonly buffers = new Map<string, StepBuffer>();
35
+ readonly callIndex = new Map<string, string>();
36
+ readonly completed = new Map<string, number>();
37
+
38
+ bufKey(sessionId: string, requestId: string): string {
39
+ return `${sessionId}::${requestId}`;
40
+ }
41
+
42
+ callKey(sessionId: string, callId: string): string {
43
+ return `${sessionId}::${callId}`;
44
+ }
45
+
46
+ completedKey(sessionId: string, callId: string, key: string): string {
47
+ return `${sessionId}::${callId}::${key}`;
48
+ }
49
+
50
+ removeBuffer(key: string): void {
51
+ const buf = this.buffers.get(key);
52
+ if (!buf) return;
53
+ this.buffers.delete(key);
54
+ const sid = key.slice(0, key.indexOf("::"));
55
+ for (const c of buf.step.calls) {
56
+ this.callIndex.delete(this.callKey(sid, c.callId));
57
+ }
58
+ }
59
+
60
+ evictStale(): void {
61
+ const now = Date.now();
62
+ for (const [k, b] of this.buffers.entries()) {
63
+ if (now - b.createdAt > BUFFER_TTL_MS) this.removeBuffer(k);
64
+ }
65
+ while (this.buffers.size > MAX_BUFFERS) {
66
+ let oldestKey: string | null = null;
67
+ let oldestTs = Infinity;
68
+ for (const [k, b] of this.buffers.entries()) {
69
+ if (b.createdAt < oldestTs) {
70
+ oldestTs = b.createdAt;
71
+ oldestKey = k;
72
+ }
73
+ }
74
+ if (!oldestKey) break;
75
+ this.removeBuffer(oldestKey);
76
+ }
77
+ for (const [k, ts] of this.completed.entries()) {
78
+ if (now - ts > BUFFER_TTL_MS) this.completed.delete(k);
79
+ }
80
+ while (this.completed.size > MAX_BUFFERS) {
81
+ const oldest = this.completed.keys().next().value;
82
+ if (oldest === undefined) break;
83
+ this.completed.delete(oldest);
84
+ }
85
+ }
86
+
87
+ markCompleted(sessionId: string, callIds: string[], keys: string[]): void {
88
+ const now = Date.now();
89
+ for (const cid of callIds) {
90
+ for (const k of keys) {
91
+ if (k) this.completed.set(this.completedKey(sessionId, cid, k), now);
92
+ }
93
+ }
94
+ }
95
+
96
+ isCompleted(sessionId: string, callId: string, key?: string): boolean {
97
+ if (!key) return false;
98
+ const ck = this.completedKey(sessionId, callId, key);
99
+ const ts = this.completed.get(ck);
100
+ if (ts === undefined) return false;
101
+ if (Date.now() - ts > BUFFER_TTL_MS) {
102
+ this.completed.delete(ck);
103
+ return false;
104
+ }
105
+ return true;
106
+ }
107
+ }
108
+
109
+ export class ToolResults {
110
+ private readonly store: ToolResultStore;
111
+
112
+ constructor(
113
+ private readonly http: Http,
114
+ private readonly sessions: Sessions,
115
+ private readonly turns: Turns,
116
+ opts: { store?: ToolResultStore } = {},
117
+ ) {
118
+ this.store = opts.store ?? new ToolResultStore();
119
+ }
120
+
121
+ private async findStep(sessionId: string, callId: string): Promise<StepBuffer | null> {
122
+ this.store.evictStale();
123
+ const existingKey = this.store.callIndex.get(this.store.callKey(sessionId, callId));
124
+ if (existingKey) {
125
+ const existing = this.store.buffers.get(existingKey);
126
+ if (existing) return existing;
127
+ }
128
+ const rec = await this.sessions.retrieve(sessionId);
129
+ for (const step of rec.required) {
130
+ if (step.kind === "function" && step.calls.some((c) => c.callId === callId)) {
131
+ const key = this.store.bufKey(sessionId, step.requestId);
132
+ let buf = this.store.buffers.get(key);
133
+ if (!buf) {
134
+ buf = { step, results: new Map(), callKeys: new Map(), createdAt: Date.now(), postFailed: false, idemKey: null };
135
+ this.store.buffers.set(key, buf);
136
+ for (const c of step.calls) {
137
+ this.store.callIndex.set(this.store.callKey(sessionId, c.callId), key);
138
+ }
139
+ }
140
+ return buf;
141
+ }
142
+ }
143
+ return null;
144
+ }
145
+
146
+ async submit(
147
+ sessionId: string,
148
+ callId: string,
149
+ output: string,
150
+ opts: { isError?: boolean; key?: string } = {},
151
+ ): Promise<SubmitOutcome> {
152
+ this.store.evictStale();
153
+ if (this.store.isCompleted(sessionId, callId, opts.key)) {
154
+ return "duplicate";
155
+ }
156
+
157
+ const buf = await this.findStep(sessionId, callId);
158
+ if (!buf) {
159
+ if (this.store.isCompleted(sessionId, callId, opts.key)) {
160
+ return "duplicate";
161
+ }
162
+ throw new CoreAPIError(`No pending function call with id ${JSON.stringify(callId)} in session ${sessionId}.`, {
163
+ status: 404,
164
+ code: "not_found",
165
+ param: callId,
166
+ });
167
+ }
168
+ if (buf.results.has(callId) && !buf.postFailed) return "duplicate";
169
+ buf.results.set(callId, { output, isError: Boolean(opts.isError) });
170
+ if (opts.key) {
171
+ let seen = buf.callKeys.get(callId);
172
+ if (!seen) {
173
+ seen = [];
174
+ buf.callKeys.set(callId, seen);
175
+ }
176
+ if (!seen.includes(opts.key)) seen.push(opts.key);
177
+ }
178
+ const expectedIds = buf.step.calls.map((c) => c.callId);
179
+ if (!expectedIds.every((cid) => buf.results.has(cid))) return "held";
180
+
181
+ const rawOutputs = expectedIds.map((cid) => buf.results.get(cid)!.output);
182
+ const flags = expectedIds.map((cid) => buf.results.get(cid)!.isError);
183
+ const truncated = fit(rawOutputs);
184
+ const requestId = buf.step.requestId;
185
+ if (!buf.idemKey) buf.idemKey = opts.key ?? stepKey(requestId);
186
+ const idem = buf.idemKey;
187
+
188
+ try {
189
+ const stream = await this.http.call(
190
+ () => postToolResults(
191
+ this.http.sdkConfig,
192
+ sessionId,
193
+ {
194
+ requestId,
195
+ results: expectedIds.map((cid, i) => ({
196
+ toolCallId: cid,
197
+ output: truncated[i],
198
+ isError: flags[i],
199
+ })),
200
+ },
201
+ { idempotencyKey: idem },
202
+ ),
203
+ { idempotent: true },
204
+ );
205
+ this.store.removeBuffer(this.store.bufKey(sessionId, requestId));
206
+ for (const cid of expectedIds) {
207
+ const cidKeys = [...(buf.callKeys.get(cid) ?? [])];
208
+ if (idem && !cidKeys.includes(idem)) cidKeys.push(idem);
209
+ if (opts.key && !cidKeys.includes(opts.key)) cidKeys.push(opts.key);
210
+ this.store.markCompleted(sessionId, [cid], cidKeys);
211
+ }
212
+ if (!stream.duplicate) this.turns.handoffOrClose(sessionId, stream);
213
+ else stream.close();
214
+ return "posted";
215
+ } catch (err) {
216
+ if (
217
+ err instanceof CoreAPIError &&
218
+ err.status === 409 &&
219
+ (err.code === "expired" || err.code === "already_answered" || err.code === "stopped" || err.code === "ended")
220
+ ) {
221
+ this.store.removeBuffer(this.store.bufKey(sessionId, requestId));
222
+ throw new CoreAPIError(`Tool call ${JSON.stringify(callId)} (${err.code}): ${err.message}`, {
223
+ status: 409,
224
+ code: err.code,
225
+ param: callId,
226
+ requestId: err.requestId,
227
+ body: err.body,
228
+ headers: err.headers,
229
+ });
230
+ }
231
+ buf.postFailed = true;
232
+ throw err;
233
+ }
234
+ }
235
+ }