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,437 @@
1
+ /** Core Agents service: neutral AgentSpec <-> Panja agent.json, AGENT.md, and versions. */
2
+ import { AgentsApi } from "../../apis/AgentsApi.js";
3
+ import { rawAgent } from "../../compose/agent.js";
4
+ import { NotSupportedByServerError, UserError } from "../../compose/errors.js";
5
+ import { settingsFingerprint } from "../../compose/fingerprint.js";
6
+ import { SettingsTransport } from "../../compose/settingsTransport.js";
7
+ import { CoreAPIError } from "./errors.js";
8
+ import { Http } from "./http.js";
9
+ import { inlineAgentName } from "./ids.js";
10
+ import { ModelResolver } from "./models.js";
11
+ import { refuse } from "./refusals.js";
12
+ import { warnOnce } from "./warnings.js";
13
+
14
+ const ALLOWED_SARVAM_KEYS = new Set([
15
+ "limits",
16
+ "webhooks",
17
+ "triggers",
18
+ "fallback",
19
+ "endpoint",
20
+ "temperature",
21
+ "top_p",
22
+ "max_output_tokens",
23
+ "function_timeout_seconds",
24
+ "output_schema",
25
+ "builtin_tools",
26
+ "skills",
27
+ "knowledge_bases",
28
+ "clarify",
29
+ "apps",
30
+ ]);
31
+
32
+ export interface AgentSpec {
33
+ name: string;
34
+ instructions: string;
35
+ model?: string | null;
36
+ reasoningEffort?: string | null;
37
+ reasoningSummary?: string | null;
38
+ toolChoice?: string | null;
39
+ builtins?: Record<string, unknown> | null;
40
+ functions: Record<string, unknown>[];
41
+ outputSchema?: Record<string, unknown> | null;
42
+ outputSchemaName?: string | null;
43
+ metadata: Record<string, string>;
44
+ description?: string | null;
45
+ sarvam: Record<string, unknown>;
46
+ provider: string;
47
+ requestedModel?: string | null;
48
+ multiAgentEnabled?: boolean | null;
49
+ archived?: boolean;
50
+ }
51
+
52
+ export interface AgentRecord {
53
+ id: string;
54
+ spec: AgentSpec;
55
+ version: number | null;
56
+ createdAt: string;
57
+ archived: boolean;
58
+ sarvamModel: string | null;
59
+ rawSettings: Record<string, unknown>;
60
+ }
61
+
62
+ function isObj(v: unknown): v is Record<string, unknown> {
63
+ return v !== null && typeof v === "object" && !Array.isArray(v);
64
+ }
65
+
66
+ function clone<T>(v: T): T {
67
+ return JSON.parse(JSON.stringify(v)) as T;
68
+ }
69
+
70
+ function iso(v: unknown): string {
71
+ if (v instanceof Date) return v.toISOString();
72
+ if (typeof v === "string" && v) return v;
73
+ return new Date().toISOString();
74
+ }
75
+
76
+ function mapUserError(err: UserError, pathOf?: (field: string) => string): CoreAPIError {
77
+ const msg = err.message;
78
+ const colon = msg.indexOf(":");
79
+ const prefix = colon !== -1 ? msg.slice(0, colon).trim() : "";
80
+ const param = prefix ? (pathOf ? pathOf(prefix) : prefix) : null;
81
+ return new CoreAPIError(msg, { status: 400, code: "invalid_request_error", param });
82
+ }
83
+
84
+ function validateSarvamKeys(sarvam: Record<string, unknown>, provider: string): void {
85
+ for (const k of Object.keys(sarvam)) {
86
+ if (!ALLOWED_SARVAM_KEYS.has(k)) {
87
+ refuse(provider, `sarvam_config.${k}`, {
88
+ reason: `unknown Sarvam extension key ${JSON.stringify(k)}`,
89
+ param: `sarvam_config.${k}`,
90
+ });
91
+ }
92
+ }
93
+ }
94
+
95
+ function specToSettings(
96
+ spec: AgentSpec,
97
+ sarvamModel: string | null,
98
+ baseSettings?: Record<string, unknown>,
99
+ ): Record<string, unknown> {
100
+ validateSarvamKeys(spec.sarvam ?? {}, spec.provider);
101
+
102
+ // Preserve unmapped settings & non-sdk_compat metadata from baseSettings.
103
+ const out: Record<string, unknown> = baseSettings ? clone(baseSettings) : {};
104
+ if (!("version" in out)) out["version"] = 1;
105
+
106
+ if (sarvamModel !== null) out["model"] = sarvamModel;
107
+ else delete out["model"];
108
+
109
+ if (spec.reasoningEffort !== undefined && spec.reasoningEffort !== null) out["reasoning"] = spec.reasoningEffort;
110
+ else delete out["reasoning"];
111
+
112
+ if (spec.toolChoice !== undefined && spec.toolChoice !== null) out["tool_choice"] = spec.toolChoice;
113
+ else delete out["tool_choice"];
114
+
115
+ if (spec.builtins !== undefined && spec.builtins !== null) {
116
+ const b = clone(spec.builtins);
117
+ if (spec.multiAgentEnabled !== undefined && spec.multiAgentEnabled !== null) {
118
+ b["subagents"] = spec.multiAgentEnabled;
119
+ }
120
+ out["builtin_tools"] = b;
121
+ } else if (spec.multiAgentEnabled !== undefined && spec.multiAgentEnabled !== null) {
122
+ const existingB = isObj(out["builtin_tools"]) ? clone(out["builtin_tools"]) : {};
123
+ existingB["subagents"] = spec.multiAgentEnabled;
124
+ out["builtin_tools"] = existingB;
125
+ } else {
126
+ delete out["builtin_tools"];
127
+ }
128
+
129
+ if (spec.functions.length > 0) out["functions"] = clone(spec.functions);
130
+ else delete out["functions"];
131
+
132
+ if (spec.outputSchema !== undefined && spec.outputSchema !== null) out["output_schema"] = clone(spec.outputSchema);
133
+ else delete out["output_schema"];
134
+
135
+ for (const [k, v] of Object.entries(spec.sarvam ?? {})) {
136
+ if (v === undefined) continue;
137
+ if (v === null) {
138
+ delete out[k];
139
+ continue;
140
+ }
141
+ if (k === "builtin_tools" && isObj(v)) {
142
+ const existing = isObj(out["builtin_tools"]) ? clone(out["builtin_tools"]) : {};
143
+ Object.assign(existing, clone(v));
144
+ out["builtin_tools"] = existing;
145
+ } else {
146
+ out[k] = clone(v);
147
+ }
148
+ }
149
+
150
+ const compatMeta: Record<string, unknown> = {
151
+ provider: spec.provider,
152
+ metadata: { ...(spec.metadata ?? {}) },
153
+ archived: Boolean(spec.archived),
154
+ };
155
+ if (spec.description !== undefined && spec.description !== null) compatMeta["description"] = spec.description;
156
+ if (spec.requestedModel !== undefined && spec.requestedModel !== null) compatMeta["requested_model"] = spec.requestedModel;
157
+ if (spec.reasoningSummary !== undefined && spec.reasoningSummary !== null) compatMeta["reasoning_summary"] = spec.reasoningSummary;
158
+ if (spec.outputSchemaName !== undefined && spec.outputSchemaName !== null) compatMeta["output_schema_name"] = spec.outputSchemaName;
159
+
160
+ const existingMeta = isObj(out["metadata"]) ? clone(out["metadata"]) : {};
161
+ delete existingMeta["sdk_name"];
162
+ delete existingMeta["sdk_hash"];
163
+ existingMeta["sdk_compat"] = compatMeta;
164
+ out["metadata"] = existingMeta;
165
+ return out;
166
+ }
167
+
168
+ function settingsToRecord(
169
+ id: string,
170
+ name: string,
171
+ instructions: string,
172
+ settings: Record<string, unknown>,
173
+ version: number | null,
174
+ createdAt: string,
175
+ ): AgentRecord {
176
+ const rawMeta = isObj(settings["metadata"]) ? settings["metadata"] : {};
177
+ const compat = isObj(rawMeta["sdk_compat"]) ? rawMeta["sdk_compat"] : {};
178
+ const userMetaRaw = isObj(compat["metadata"]) ? compat["metadata"] : {};
179
+ const userMeta: Record<string, string> = {};
180
+ for (const [k, v] of Object.entries(userMetaRaw)) userMeta[k] = String(v);
181
+ const sarvamModel = typeof settings["model"] === "string" ? settings["model"] : null;
182
+ const requestedModel = typeof compat["requested_model"] === "string" ? compat["requested_model"] : sarvamModel;
183
+ const builtins = isObj(settings["builtin_tools"]) ? clone(settings["builtin_tools"]) : null;
184
+ const multiAgentEnabled = builtins && "subagents" in builtins ? Boolean(builtins["subagents"]) : null;
185
+ const archived = Boolean(compat["archived"]);
186
+
187
+ const spec: AgentSpec = {
188
+ name,
189
+ instructions,
190
+ model: requestedModel,
191
+ reasoningEffort: typeof settings["reasoning"] === "string" ? settings["reasoning"] : null,
192
+ reasoningSummary: typeof compat["reasoning_summary"] === "string" ? compat["reasoning_summary"] : null,
193
+ toolChoice: typeof settings["tool_choice"] === "string" ? settings["tool_choice"] : null,
194
+ builtins,
195
+ functions: Array.isArray(settings["functions"]) ? clone(settings["functions"] as Record<string, unknown>[]) : [],
196
+ outputSchema: isObj(settings["output_schema"]) ? clone(settings["output_schema"]) : null,
197
+ outputSchemaName: typeof compat["output_schema_name"] === "string" ? compat["output_schema_name"] : null,
198
+ metadata: userMeta,
199
+ description: typeof compat["description"] === "string" ? compat["description"] : null,
200
+ sarvam: {},
201
+ provider: typeof compat["provider"] === "string" ? compat["provider"] : "openai",
202
+ requestedModel,
203
+ multiAgentEnabled,
204
+ archived,
205
+ };
206
+ return { id, spec, version, createdAt, archived, sarvamModel, rawSettings: clone(settings) };
207
+ }
208
+
209
+ export class Agents {
210
+ private readonly transport: SettingsTransport;
211
+ private readonly api: AgentsApi;
212
+ private readonly inlineInflight = new Map<string, Promise<AgentRecord>>();
213
+
214
+ constructor(
215
+ private readonly http: Http,
216
+ private readonly models: ModelResolver,
217
+ ) {
218
+ this.transport = new SettingsTransport(http.sdkConfig);
219
+ this.api = new AgentsApi(http.sdkConfig);
220
+ }
221
+
222
+ private async buildRaw(
223
+ spec: AgentSpec,
224
+ pathOf?: (field: string) => string,
225
+ baseSettings?: Record<string, unknown>,
226
+ ) {
227
+ const resolved = await this.models.resolve(spec.model);
228
+ const effective = clone(spec);
229
+ if (!effective.requestedModel) effective.requestedModel = resolved.requested;
230
+ const settings = specToSettings(effective, resolved.sarvamId, baseSettings);
231
+ try {
232
+ return { agent: rawAgent(effective.name, effective.instructions, settings), sarvamModel: resolved.sarvamId };
233
+ } catch (err) {
234
+ if (err instanceof UserError) throw mapUserError(err, pathOf);
235
+ throw err;
236
+ }
237
+ }
238
+
239
+ async create(
240
+ spec: AgentSpec,
241
+ opts: { pathOf?: (field: string) => string } = {},
242
+ ): Promise<AgentRecord> {
243
+ const { agent } = await this.buildRaw(spec, opts.pathOf);
244
+ try {
245
+ const remote = await this.http.call(
246
+ () => this.transport.createAgent(agent),
247
+ { idempotent: false },
248
+ );
249
+ return settingsToRecord(
250
+ remote.agentId,
251
+ remote.name,
252
+ remote.instructions,
253
+ remote.settings,
254
+ remote.liveVersion ?? null,
255
+ iso((remote as unknown as { createdAt?: unknown }).createdAt),
256
+ );
257
+ } catch (err) {
258
+ if (err instanceof UserError) throw mapUserError(err, opts.pathOf);
259
+ if (err instanceof NotSupportedByServerError) {
260
+ throw new CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
261
+ }
262
+ throw err;
263
+ }
264
+ }
265
+
266
+ async retrieve(agentId: string): Promise<AgentRecord> {
267
+ const [resource, instr] = await Promise.all([
268
+ this.http.call(() => this.api.getAgent({ agentId }), { idempotent: true }),
269
+ this.http.call(() => this.api.getInstructions({ agentId }), { idempotent: true }),
270
+ ]);
271
+ let settings: Record<string, unknown> = isObj(resource.settings) ? { ...resource.settings } : {};
272
+ if (Object.keys(settings).length === 0) {
273
+ try {
274
+ const env = await this.http.call(() => this.api.getAgentSettings({ agentId }), { idempotent: true });
275
+ settings = isObj(env.settings) ? { ...env.settings } : {};
276
+ } catch (err) {
277
+ if (!(err instanceof CoreAPIError && err.status === 404)) throw err;
278
+ }
279
+ }
280
+ return settingsToRecord(
281
+ resource.id,
282
+ resource.name,
283
+ instr.data,
284
+ settings,
285
+ resource.liveVersion ?? null,
286
+ iso(resource.createdAt),
287
+ );
288
+ }
289
+
290
+ async update(
291
+ agentId: string,
292
+ change: (spec: AgentSpec) => AgentSpec,
293
+ opts: { expectedVersion?: number; pathOf?: (field: string) => string } = {},
294
+ ): Promise<AgentRecord> {
295
+ const current = await this.retrieve(agentId);
296
+ if (current.archived) {
297
+ throw new CoreAPIError(`Cannot update archived agent ${agentId}.`, {
298
+ status: 409,
299
+ code: "agent_archived",
300
+ });
301
+ }
302
+ if (opts.expectedVersion !== undefined) {
303
+ warnOnce(
304
+ "version_check_not_atomic",
305
+ "Expected-version check on agent update is not atomic on Sarvam yet; concurrent writers are last-write-wins.",
306
+ );
307
+ if (current.version !== opts.expectedVersion) {
308
+ throw new CoreAPIError(
309
+ `Agent ${agentId} is at version ${current.version}, expected ${opts.expectedVersion}`,
310
+ { status: 409, code: "version_conflict" },
311
+ );
312
+ }
313
+ }
314
+ const updatedSpec = change(clone(current.spec));
315
+ const { agent } = await this.buildRaw(updatedSpec, opts.pathOf, current.rawSettings);
316
+
317
+ const currentHash = settingsFingerprint(current.spec.instructions, current.rawSettings);
318
+ if (updatedSpec.name === current.spec.name && agent.specHash() === currentHash) {
319
+ return current;
320
+ }
321
+
322
+ try {
323
+ const remote = await this.http.call(() => this.transport.updateAgent(agentId, agent), { idempotent: false });
324
+ return settingsToRecord(
325
+ remote.agentId,
326
+ remote.name,
327
+ remote.instructions,
328
+ remote.settings,
329
+ remote.liveVersion ?? null,
330
+ current.createdAt,
331
+ );
332
+ } catch (err) {
333
+ if (err instanceof UserError) throw mapUserError(err, opts.pathOf);
334
+ if (err instanceof NotSupportedByServerError) {
335
+ throw new CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
336
+ }
337
+ throw err;
338
+ }
339
+ }
340
+
341
+ async list(offset = 0, limit = 20): Promise<[AgentRecord[], boolean]> {
342
+ const resp = await this.http.call(
343
+ () => this.api.listAgents({ limit, offset }),
344
+ { idempotent: true },
345
+ );
346
+ const records = (resp.data ?? []).map((item) => {
347
+ const instrRaw = (item as unknown as Record<string, unknown>)["instructions"];
348
+ return settingsToRecord(
349
+ item.id,
350
+ item.name,
351
+ typeof instrRaw === "string" ? instrRaw : "",
352
+ isObj(item.settings) ? { ...item.settings } : {},
353
+ item.liveVersion ?? null,
354
+ iso(item.createdAt),
355
+ );
356
+ });
357
+ return [records, Boolean(resp.hasMore)];
358
+ }
359
+
360
+ async delete(agentId: string): Promise<void> {
361
+ await this.http.call(() => this.transport.deleteAgent(agentId), { idempotent: true });
362
+ }
363
+
364
+ private async findInlineByName(name: string): Promise<AgentRecord | null> {
365
+ const matches: Array<{
366
+ id: string;
367
+ name: string;
368
+ instructions: string;
369
+ settings: Record<string, unknown>;
370
+ version: number | null;
371
+ createdAt: string;
372
+ }> = [];
373
+ let offset = 0;
374
+ for (;;) {
375
+ const off = offset;
376
+ const page = await this.http.call(
377
+ () => this.api.listAgents({ limit: 100, offset: off }),
378
+ { idempotent: true },
379
+ );
380
+ const items = page.data ?? [];
381
+ for (const item of items) {
382
+ const settings = isObj(item.settings) ? { ...item.settings } : {};
383
+ const meta = isObj(settings["metadata"]) ? settings["metadata"] : {};
384
+ const sdkName = typeof meta["sdk_name"] === "string" ? meta["sdk_name"] : null;
385
+ if (item.name === name || sdkName === name) {
386
+ const instrRaw = (item as unknown as Record<string, unknown>)["instructions"];
387
+ matches.push({
388
+ id: item.id,
389
+ name: item.name,
390
+ instructions: typeof instrRaw === "string" ? instrRaw : "",
391
+ settings,
392
+ version: item.liveVersion ?? null,
393
+ createdAt: iso(item.createdAt),
394
+ });
395
+ }
396
+ }
397
+ if (items.length === 0 || !page.hasMore) break;
398
+ offset += items.length;
399
+ }
400
+ if (matches.length === 0) return null;
401
+ matches.sort((a, b) => a.id.localeCompare(b.id));
402
+ const chosen = matches[0];
403
+ return settingsToRecord(
404
+ chosen.id,
405
+ chosen.name,
406
+ chosen.instructions,
407
+ chosen.settings,
408
+ chosen.version,
409
+ chosen.createdAt,
410
+ );
411
+ }
412
+
413
+ async ensureInline(spec: AgentSpec, opts: { pathOf?: (field: string) => string } = {}): Promise<AgentRecord> {
414
+ const resolved = await this.models.resolve(spec.model);
415
+ const temp = clone(spec);
416
+ if (!temp.requestedModel) temp.requestedModel = resolved.requested;
417
+ const tempSettings = specToSettings(temp, resolved.sarvamId);
418
+ const fp = settingsFingerprint(temp.instructions, tempSettings);
419
+ temp.name = inlineAgentName(fp);
420
+ const key = temp.name;
421
+
422
+ const inflight = this.inlineInflight.get(key);
423
+ if (inflight) return inflight;
424
+
425
+ const promise = (async () => {
426
+ const existing = await this.findInlineByName(key);
427
+ if (existing) return existing;
428
+ return this.create(temp, opts);
429
+ })();
430
+ this.inlineInflight.set(key, promise);
431
+ try {
432
+ return await promise;
433
+ } finally {
434
+ this.inlineInflight.delete(key);
435
+ }
436
+ }
437
+ }