sarvam-agents-client 0.5.2 → 0.5.4

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 (174) hide show
  1. package/README.md +52 -29
  2. package/dist/apis/AgentsApi.d.ts +96 -11
  3. package/dist/apis/AgentsApi.js +229 -9
  4. package/dist/apis/ConversationsApi.d.ts +5 -2
  5. package/dist/apis/ConversationsApi.js +7 -2
  6. package/dist/client.d.ts +3 -2
  7. package/dist/client.js +2 -1
  8. package/dist/compat/core/agents.d.ts +6 -1
  9. package/dist/compat/core/agents.js +237 -27
  10. package/dist/compat/core/customApis.d.ts +76 -0
  11. package/dist/compat/core/customApis.js +424 -0
  12. package/dist/compat/core/data.js +1 -1
  13. package/dist/compat/openai/client.d.ts +3 -0
  14. package/dist/compat/openai/client.js +5 -1
  15. package/dist/compat/openai/index.d.ts +1 -0
  16. package/dist/compat/openai/index.js +0 -1
  17. package/dist/compat/openai/resources/agents.js +8 -1
  18. package/dist/compat/openai/resources/sarvam.d.ts +26 -0
  19. package/dist/compat/openai/resources/sarvam.js +56 -0
  20. package/dist/compose/deploy.d.ts +14 -3
  21. package/dist/compose/deploy.js +60 -7
  22. package/dist/compose/errors.d.ts +4 -0
  23. package/dist/compose/errors.js +6 -1
  24. package/dist/compose/settingsTransport.d.ts +18 -13
  25. package/dist/compose/settingsTransport.js +78 -130
  26. package/dist/compose/transport.d.ts +12 -2
  27. package/dist/compose/webhook.d.ts +10 -13
  28. package/dist/compose/webhook.js +48 -45
  29. package/dist/compose/webhookSecrets.d.ts +79 -49
  30. package/dist/compose/webhookSecrets.js +244 -195
  31. package/dist/esm/apis/AgentsApi.d.ts +96 -11
  32. package/dist/esm/apis/AgentsApi.js +230 -10
  33. package/dist/esm/apis/ConversationsApi.d.ts +5 -2
  34. package/dist/esm/apis/ConversationsApi.js +7 -2
  35. package/dist/esm/client.d.ts +3 -2
  36. package/dist/esm/client.js +2 -1
  37. package/dist/esm/compat/core/agents.d.ts +6 -1
  38. package/dist/esm/compat/core/agents.js +237 -27
  39. package/dist/esm/compat/core/customApis.d.ts +76 -0
  40. package/dist/esm/compat/core/customApis.js +419 -0
  41. package/dist/esm/compat/core/data.js +1 -1
  42. package/dist/esm/compat/openai/client.d.ts +3 -0
  43. package/dist/esm/compat/openai/client.js +5 -1
  44. package/dist/esm/compat/openai/index.d.ts +1 -0
  45. package/dist/esm/compat/openai/index.js +0 -1
  46. package/dist/esm/compat/openai/resources/agents.js +8 -1
  47. package/dist/esm/compat/openai/resources/sarvam.d.ts +26 -0
  48. package/dist/esm/compat/openai/resources/sarvam.js +51 -0
  49. package/dist/esm/compose/deploy.d.ts +14 -3
  50. package/dist/esm/compose/deploy.js +60 -7
  51. package/dist/esm/compose/errors.d.ts +4 -0
  52. package/dist/esm/compose/errors.js +4 -0
  53. package/dist/esm/compose/settingsTransport.d.ts +18 -13
  54. package/dist/esm/compose/settingsTransport.js +80 -132
  55. package/dist/esm/compose/transport.d.ts +12 -2
  56. package/dist/esm/compose/webhook.d.ts +10 -13
  57. package/dist/esm/compose/webhook.js +45 -42
  58. package/dist/esm/compose/webhookSecrets.d.ts +79 -49
  59. package/dist/esm/compose/webhookSecrets.js +241 -193
  60. package/dist/esm/index.d.ts +2 -1
  61. package/dist/esm/index.js +2 -1
  62. package/dist/esm/models/AgentConversationDetail.d.ts +12 -0
  63. package/dist/esm/models/AgentConversationDetail.js +4 -0
  64. package/dist/esm/models/AgentConversationView.d.ts +12 -0
  65. package/dist/esm/models/AgentConversationView.js +4 -0
  66. package/dist/esm/models/AgentResource.d.ts +7 -0
  67. package/dist/esm/models/AgentResource.js +3 -0
  68. package/dist/esm/models/AgentVersionSavedResource.d.ts +65 -0
  69. package/dist/esm/models/AgentVersionSavedResource.js +60 -0
  70. package/dist/esm/models/AgentWebhookList.d.ts +33 -0
  71. package/dist/esm/models/AgentWebhookList.js +44 -0
  72. package/dist/esm/models/AgentWebhookView.d.ts +76 -0
  73. package/dist/esm/models/AgentWebhookView.js +70 -0
  74. package/dist/esm/models/ConversationDetail.d.ts +6 -0
  75. package/dist/esm/models/ConversationDetail.js +2 -0
  76. package/dist/esm/models/ConversationView.d.ts +6 -0
  77. package/dist/esm/models/ConversationView.js +2 -0
  78. package/dist/esm/models/ErrorDetailItem.d.ts +3 -0
  79. package/dist/esm/models/FileNodeView.d.ts +7 -0
  80. package/dist/esm/models/FileNodeView.js +3 -0
  81. package/dist/esm/models/QueueMessageInput.d.ts +6 -0
  82. package/dist/esm/models/QueueMessageInput.js +2 -0
  83. package/dist/esm/models/RotatedWebhookSecretView.d.ts +58 -0
  84. package/dist/esm/models/RotatedWebhookSecretView.js +61 -0
  85. package/dist/esm/models/SaveWarningView.d.ts +44 -0
  86. package/dist/esm/models/SaveWarningView.js +51 -0
  87. package/dist/esm/models/ShareSkippedResource.d.ts +41 -0
  88. package/dist/esm/models/ShareSkippedResource.js +47 -0
  89. package/dist/esm/models/WebhookSecretPutBody.d.ts +32 -0
  90. package/dist/esm/models/WebhookSecretPutBody.js +43 -0
  91. package/dist/esm/models/WebhookSecretStateView.d.ts +52 -0
  92. package/dist/esm/models/WebhookSecretStateView.js +56 -0
  93. package/dist/esm/models/WebhookSecretValueView.d.ts +32 -0
  94. package/dist/esm/models/WebhookSecretValueView.js +44 -0
  95. package/dist/esm/models/WebhookWarning.d.ts +44 -0
  96. package/dist/esm/models/WebhookWarning.js +51 -0
  97. package/dist/esm/models/index.d.ts +10 -0
  98. package/dist/esm/models/index.js +10 -0
  99. package/dist/index.d.ts +2 -1
  100. package/dist/index.js +4 -1
  101. package/dist/models/AgentConversationDetail.d.ts +12 -0
  102. package/dist/models/AgentConversationDetail.js +4 -0
  103. package/dist/models/AgentConversationView.d.ts +12 -0
  104. package/dist/models/AgentConversationView.js +4 -0
  105. package/dist/models/AgentResource.d.ts +7 -0
  106. package/dist/models/AgentResource.js +3 -0
  107. package/dist/models/AgentVersionSavedResource.d.ts +65 -0
  108. package/dist/models/AgentVersionSavedResource.js +67 -0
  109. package/dist/models/AgentWebhookList.d.ts +33 -0
  110. package/dist/models/AgentWebhookList.js +51 -0
  111. package/dist/models/AgentWebhookView.d.ts +76 -0
  112. package/dist/models/AgentWebhookView.js +78 -0
  113. package/dist/models/ConversationDetail.d.ts +6 -0
  114. package/dist/models/ConversationDetail.js +2 -0
  115. package/dist/models/ConversationView.d.ts +6 -0
  116. package/dist/models/ConversationView.js +2 -0
  117. package/dist/models/ErrorDetailItem.d.ts +3 -0
  118. package/dist/models/FileNodeView.d.ts +7 -0
  119. package/dist/models/FileNodeView.js +3 -0
  120. package/dist/models/QueueMessageInput.d.ts +6 -0
  121. package/dist/models/QueueMessageInput.js +2 -0
  122. package/dist/models/RotatedWebhookSecretView.d.ts +58 -0
  123. package/dist/models/RotatedWebhookSecretView.js +69 -0
  124. package/dist/models/SaveWarningView.d.ts +44 -0
  125. package/dist/models/SaveWarningView.js +58 -0
  126. package/dist/models/ShareSkippedResource.d.ts +41 -0
  127. package/dist/models/ShareSkippedResource.js +54 -0
  128. package/dist/models/WebhookSecretPutBody.d.ts +32 -0
  129. package/dist/models/WebhookSecretPutBody.js +50 -0
  130. package/dist/models/WebhookSecretStateView.d.ts +52 -0
  131. package/dist/models/WebhookSecretStateView.js +64 -0
  132. package/dist/models/WebhookSecretValueView.d.ts +32 -0
  133. package/dist/models/WebhookSecretValueView.js +51 -0
  134. package/dist/models/WebhookWarning.d.ts +44 -0
  135. package/dist/models/WebhookWarning.js +58 -0
  136. package/dist/models/index.d.ts +10 -0
  137. package/dist/models/index.js +10 -0
  138. package/package.json +1 -1
  139. package/src/apis/AgentsApi.ts +366 -11
  140. package/src/apis/ConversationsApi.ts +9 -2
  141. package/src/client.ts +3 -2
  142. package/src/compat/core/agents.ts +265 -36
  143. package/src/compat/core/customApis.ts +506 -0
  144. package/src/compat/core/data.ts +1 -1
  145. package/src/compat/openai/client.ts +7 -1
  146. package/src/compat/openai/index.ts +9 -0
  147. package/src/compat/openai/resources/agents.ts +9 -1
  148. package/src/compat/openai/resources/sarvam.ts +94 -0
  149. package/src/compose/deploy.ts +71 -10
  150. package/src/compose/errors.ts +4 -0
  151. package/src/compose/settingsTransport.ts +78 -131
  152. package/src/compose/transport.ts +10 -2
  153. package/src/compose/webhook.ts +49 -45
  154. package/src/compose/webhookSecrets.ts +227 -179
  155. package/src/index.ts +2 -1
  156. package/src/models/AgentConversationDetail.ts +16 -0
  157. package/src/models/AgentConversationView.ts +16 -0
  158. package/src/models/AgentResource.ts +15 -0
  159. package/src/models/AgentVersionSavedResource.ts +119 -0
  160. package/src/models/AgentWebhookList.ts +74 -0
  161. package/src/models/AgentWebhookView.ts +129 -0
  162. package/src/models/ConversationDetail.ts +8 -0
  163. package/src/models/ConversationView.ts +8 -0
  164. package/src/models/ErrorDetailItem.ts +3 -0
  165. package/src/models/FileNodeView.ts +16 -0
  166. package/src/models/QueueMessageInput.ts +8 -0
  167. package/src/models/RotatedWebhookSecretView.ts +103 -0
  168. package/src/models/SaveWarningView.ts +84 -0
  169. package/src/models/ShareSkippedResource.ts +78 -0
  170. package/src/models/WebhookSecretPutBody.ts +66 -0
  171. package/src/models/WebhookSecretStateView.ts +94 -0
  172. package/src/models/WebhookSecretValueView.ts +66 -0
  173. package/src/models/WebhookWarning.ts +84 -0
  174. package/src/models/index.ts +10 -0
@@ -7,6 +7,7 @@ const agent_js_1 = require("../../compose/agent.js");
7
7
  const errors_js_1 = require("../../compose/errors.js");
8
8
  const fingerprint_js_1 = require("../../compose/fingerprint.js");
9
9
  const settingsTransport_js_1 = require("../../compose/settingsTransport.js");
10
+ const customApis_js_1 = require("./customApis.js");
10
11
  const errors_js_2 = require("./errors.js");
11
12
  const ids_js_1 = require("./ids.js");
12
13
  const refusals_js_1 = require("./refusals.js");
@@ -27,7 +28,31 @@ const ALLOWED_SARVAM_KEYS = new Set([
27
28
  "knowledge_bases",
28
29
  "clarify",
29
30
  "apps",
31
+ "custom_apis",
30
32
  ]);
33
+ function customApiEntries(infos) {
34
+ return infos.map((c) => ({ id: c.id, name: c.name, host: c.host }));
35
+ }
36
+ function readManagedCustomApis(settings) {
37
+ if (!settings)
38
+ return [];
39
+ const rawMeta = isObj(settings["metadata"]) ? settings["metadata"] : {};
40
+ const compat = isObj(rawMeta["sdk_compat"]) ? rawMeta["sdk_compat"] : {};
41
+ const rawList = compat["custom_apis"];
42
+ if (!Array.isArray(rawList))
43
+ return [];
44
+ const out = [];
45
+ for (const item of rawList) {
46
+ if (isObj(item) && typeof item["id"] === "string" && item["id"]) {
47
+ out.push({
48
+ id: item["id"],
49
+ name: typeof item["name"] === "string" ? item["name"] : "",
50
+ host: item["host"] !== undefined && item["host"] !== null ? String(item["host"]) : null,
51
+ });
52
+ }
53
+ }
54
+ return out;
55
+ }
31
56
  function isObj(v) {
32
57
  return v !== null && typeof v === "object" && !Array.isArray(v);
33
58
  }
@@ -58,7 +83,7 @@ function validateSarvamKeys(sarvam, provider) {
58
83
  }
59
84
  }
60
85
  }
61
- function specToSettings(spec, sarvamModel, baseSettings) {
86
+ function specToSettings(spec, sarvamModel, baseSettings, managedCustomApis) {
62
87
  var _a, _b, _c;
63
88
  validateSarvamKeys((_a = spec.sarvam) !== null && _a !== void 0 ? _a : {}, spec.provider);
64
89
  // Preserve unmapped settings & non-sdk_compat metadata from baseSettings.
@@ -101,6 +126,8 @@ function specToSettings(spec, sarvamModel, baseSettings) {
101
126
  else
102
127
  delete out["output_schema"];
103
128
  for (const [k, v] of Object.entries((_b = spec.sarvam) !== null && _b !== void 0 ? _b : {})) {
129
+ if (k === "custom_apis")
130
+ continue;
104
131
  if (v === undefined)
105
132
  continue;
106
133
  if (v === null) {
@@ -129,6 +156,20 @@ function specToSettings(spec, sarvamModel, baseSettings) {
129
156
  compatMeta["reasoning_summary"] = spec.reasoningSummary;
130
157
  if (spec.outputSchemaName !== undefined && spec.outputSchemaName !== null)
131
158
  compatMeta["output_schema_name"] = spec.outputSchemaName;
159
+ const effectiveCustomApis = managedCustomApis !== undefined && managedCustomApis !== null
160
+ ? managedCustomApis
161
+ : readManagedCustomApis(baseSettings);
162
+ if (effectiveCustomApis.length > 0) {
163
+ // Panja rejects null values inside agent.json, so omit keys whose value is null/undefined.
164
+ compatMeta["custom_apis"] = effectiveCustomApis.map((entry) => {
165
+ const cleaned = {};
166
+ for (const [k, v] of Object.entries(entry)) {
167
+ if (v !== null && v !== undefined)
168
+ cleaned[k] = clone(v);
169
+ }
170
+ return cleaned;
171
+ });
172
+ }
132
173
  const existingMeta = isObj(out["metadata"]) ? clone(out["metadata"]) : {};
133
174
  delete existingMeta["sdk_name"];
134
175
  delete existingMeta["sdk_hash"];
@@ -167,22 +208,33 @@ function settingsToRecord(id, name, instructions, settings, version, createdAt)
167
208
  multiAgentEnabled,
168
209
  archived,
169
210
  };
170
- return { id, spec, version, createdAt, archived, sarvamModel, rawSettings: clone(settings) };
211
+ return {
212
+ id,
213
+ spec,
214
+ version,
215
+ createdAt,
216
+ archived,
217
+ sarvamModel,
218
+ rawSettings: clone(settings),
219
+ customApis: readManagedCustomApis(settings),
220
+ unmanagedCustomApis: [],
221
+ };
171
222
  }
172
223
  class Agents {
173
- constructor(http, models) {
224
+ constructor(http, models, customApis) {
174
225
  this.http = http;
175
226
  this.models = models;
176
227
  this.inlineInflight = new Map();
177
228
  this.transport = new settingsTransport_js_1.SettingsTransport(http.sdkConfig);
178
229
  this.api = new AgentsApi_js_1.AgentsApi(http.sdkConfig);
230
+ this.customApis = customApis !== null && customApis !== void 0 ? customApis : new customApis_js_1.CustomApis(http);
179
231
  }
180
- async buildRaw(spec, pathOf, baseSettings) {
232
+ async buildRaw(spec, pathOf, baseSettings, managedCustomApis) {
181
233
  const resolved = await this.models.resolve(spec.model);
182
234
  const effective = clone(spec);
183
235
  if (!effective.requestedModel)
184
236
  effective.requestedModel = resolved.requested;
185
- const settings = specToSettings(effective, resolved.sarvamId, baseSettings);
237
+ const settings = specToSettings(effective, resolved.sarvamId, baseSettings, managedCustomApis);
186
238
  try {
187
239
  return { agent: (0, agent_js_1.rawAgent)(effective.name, effective.instructions, settings), sarvamModel: resolved.sarvamId };
188
240
  }
@@ -193,11 +245,21 @@ class Agents {
193
245
  }
194
246
  }
195
247
  async create(spec, opts = {}) {
196
- var _a;
197
- const { agent } = await this.buildRaw(spec, opts.pathOf);
248
+ var _a, _b;
249
+ const sarvamCfg = (_a = spec.sarvam) !== null && _a !== void 0 ? _a : {};
250
+ let resolvedCas = [];
251
+ if (opts._resolvedCustomApis !== undefined) {
252
+ resolvedCas = [...opts._resolvedCustomApis];
253
+ }
254
+ else if ("custom_apis" in sarvamCfg && sarvamCfg["custom_apis"] !== null && sarvamCfg["custom_apis"] !== undefined) {
255
+ const refs = (0, customApis_js_1.validateCustomApiRefs)(sarvamCfg["custom_apis"]);
256
+ resolvedCas = await this.customApis.resolveRefs(refs);
257
+ }
258
+ const managedEntries = customApiEntries(resolvedCas);
259
+ const { agent } = await this.buildRaw(spec, opts.pathOf, undefined, managedEntries);
260
+ let remote;
198
261
  try {
199
- const remote = await this.http.call(() => this.transport.createAgent(agent), { idempotent: false });
200
- return settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_a = remote.liveVersion) !== null && _a !== void 0 ? _a : null, iso(remote.createdAt));
262
+ remote = await this.http.call(() => this.transport.createAgent(agent), { idempotent: false });
201
263
  }
202
264
  catch (err) {
203
265
  if (err instanceof errors_js_1.UserError)
@@ -207,13 +269,40 @@ class Agents {
207
269
  }
208
270
  throw err;
209
271
  }
272
+ if (resolvedCas.length > 0) {
273
+ try {
274
+ for (const ca of resolvedCas) {
275
+ await this.customApis.bind(remote.agentId, ca.id);
276
+ }
277
+ }
278
+ catch (err) {
279
+ try {
280
+ await this.http.call(() => this.transport.deleteAgent(remote.agentId), { idempotent: true });
281
+ }
282
+ catch (_c) {
283
+ // Best-effort cleanup of half-created agent.
284
+ }
285
+ throw err;
286
+ }
287
+ }
288
+ const rec = settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_b = remote.liveVersion) !== null && _b !== void 0 ? _b : null, iso(remote.createdAt));
289
+ if (managedEntries.length > 0) {
290
+ rec.customApis = managedEntries;
291
+ }
292
+ return rec;
210
293
  }
211
294
  async retrieve(agentId) {
212
295
  var _a;
213
- const [resource, instr] = await Promise.all([
214
- this.http.call(() => this.api.getAgent({ agentId }), { idempotent: true }),
215
- this.http.call(() => this.api.getInstructions({ agentId }), { idempotent: true }),
216
- ]);
296
+ const resource = await this.http.call(() => this.api.getAgent({ agentId }), { idempotent: true });
297
+ let liveCustomApis = null;
298
+ try {
299
+ liveCustomApis = await this.customApis.listForAgent(agentId);
300
+ }
301
+ catch (err) {
302
+ if (!(err instanceof errors_js_2.CoreAPIError && err.status === 404))
303
+ throw err;
304
+ }
305
+ const instr = await this.http.call(() => this.api.getInstructions({ agentId }), { idempotent: true });
217
306
  let settings = isObj(resource.settings) ? { ...resource.settings } : {};
218
307
  if (Object.keys(settings).length === 0) {
219
308
  try {
@@ -225,10 +314,24 @@ class Agents {
225
314
  throw err;
226
315
  }
227
316
  }
228
- return settingsToRecord(resource.id, resource.name, instr.data, settings, (_a = resource.liveVersion) !== null && _a !== void 0 ? _a : null, iso(resource.createdAt));
317
+ const rec = settingsToRecord(resource.id, resource.name, instr.data, settings, (_a = resource.liveVersion) !== null && _a !== void 0 ? _a : null, iso(resource.createdAt));
318
+ if (liveCustomApis !== null) {
319
+ const liveById = new Map();
320
+ for (const item of liveCustomApis) {
321
+ if (item.id)
322
+ liveById.set(item.id, item);
323
+ }
324
+ const managedIds = rec.customApis.map((c) => c.id).filter(Boolean);
325
+ const managedSet = new Set(managedIds);
326
+ rec.customApis = managedIds
327
+ .map((cid) => liveById.get(cid))
328
+ .filter((x) => x !== undefined);
329
+ rec.unmanagedCustomApis = liveCustomApis.filter((item) => !managedSet.has(item.id));
330
+ }
331
+ return rec;
229
332
  }
230
333
  async update(agentId, change, opts = {}) {
231
- var _a;
334
+ var _a, _b, _c;
232
335
  const current = await this.retrieve(agentId);
233
336
  if (current.archived) {
234
337
  throw new errors_js_2.CoreAPIError(`Cannot update archived agent ${agentId}.`, {
@@ -242,24 +345,112 @@ class Agents {
242
345
  throw new errors_js_2.CoreAPIError(`Agent ${agentId} is at version ${current.version}, expected ${opts.expectedVersion}`, { status: 409, code: "version_conflict" });
243
346
  }
244
347
  }
348
+ const prevManaged = readManagedCustomApis(current.rawSettings);
245
349
  const updatedSpec = change(clone(current.spec));
246
- const { agent } = await this.buildRaw(updatedSpec, opts.pathOf, current.rawSettings);
350
+ const sarvamCfg = (_a = updatedSpec.sarvam) !== null && _a !== void 0 ? _a : {};
351
+ const customApisSpecified = "custom_apis" in sarvamCfg;
352
+ if (!customApisSpecified) {
353
+ const { agent } = await this.buildRaw(updatedSpec, opts.pathOf, current.rawSettings);
354
+ const currentHash = (0, fingerprint_js_1.settingsFingerprint)(current.spec.instructions, current.rawSettings);
355
+ if (updatedSpec.name === current.spec.name && agent.specHash() === currentHash) {
356
+ return current;
357
+ }
358
+ try {
359
+ const remote = await this.http.call(() => this.transport.updateAgent(agentId, agent), { idempotent: false });
360
+ const rec = settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_b = remote.liveVersion) !== null && _b !== void 0 ? _b : null, current.createdAt);
361
+ rec.customApis = [...current.customApis];
362
+ rec.unmanagedCustomApis = [...current.unmanagedCustomApis];
363
+ return rec;
364
+ }
365
+ catch (err) {
366
+ if (err instanceof errors_js_1.UserError)
367
+ throw mapUserError(err, opts.pathOf);
368
+ if (err instanceof errors_js_1.NotSupportedByServerError) {
369
+ throw new errors_js_2.CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
370
+ }
371
+ throw err;
372
+ }
373
+ }
374
+ const rawRefs = sarvamCfg["custom_apis"];
375
+ let resolvedCas = [];
376
+ if (rawRefs !== null && rawRefs !== undefined) {
377
+ const refs = (0, customApis_js_1.validateCustomApiRefs)(rawRefs);
378
+ resolvedCas = await this.customApis.resolveRefs(refs);
379
+ }
380
+ const managedEntries = customApiEntries(resolvedCas);
381
+ const { agent } = await this.buildRaw(updatedSpec, opts.pathOf, current.rawSettings, managedEntries);
382
+ const live = await this.customApis.listForAgent(agentId);
383
+ const liveIds = new Set(live.map((i) => i.id).filter(Boolean));
384
+ const prevManagedIds = prevManaged.map((i) => i.id).filter(Boolean);
385
+ const prevManagedSet = new Set(prevManagedIds);
386
+ const targetIds = resolvedCas.map((c) => c.id);
387
+ const targetSet = new Set(targetIds);
388
+ const toBind = targetIds.filter((cid) => !liveIds.has(cid));
389
+ const toUnbind = prevManagedIds.filter((cid) => !targetSet.has(cid) && liveIds.has(cid));
390
+ const unmanaged = live.filter((item) => !targetSet.has(item.id) && !prevManagedSet.has(item.id));
247
391
  const currentHash = (0, fingerprint_js_1.settingsFingerprint)(current.spec.instructions, current.rawSettings);
248
- if (updatedSpec.name === current.spec.name && agent.specHash() === currentHash) {
392
+ const settingsChanged = updatedSpec.name !== current.spec.name || agent.specHash() !== currentHash;
393
+ if (!settingsChanged && toBind.length === 0 && toUnbind.length === 0) {
394
+ current.customApis = managedEntries;
395
+ current.unmanagedCustomApis = unmanaged;
249
396
  return current;
250
397
  }
398
+ const boundNow = [];
399
+ const unboundNow = [];
400
+ const rollbackBindings = async () => {
401
+ for (const cid of boundNow) {
402
+ try {
403
+ await this.customApis.unbind(agentId, cid);
404
+ }
405
+ catch (_a) {
406
+ // Best-effort rollback of newly added binds.
407
+ }
408
+ }
409
+ for (const cid of unboundNow) {
410
+ try {
411
+ await this.customApis.bind(agentId, cid);
412
+ }
413
+ catch (_b) {
414
+ // Best-effort rollback of newly removed binds.
415
+ }
416
+ }
417
+ };
251
418
  try {
252
- const remote = await this.http.call(() => this.transport.updateAgent(agentId, agent), { idempotent: false });
253
- return settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_a = remote.liveVersion) !== null && _a !== void 0 ? _a : null, current.createdAt);
419
+ for (const cid of toUnbind) {
420
+ await this.customApis.unbind(agentId, cid);
421
+ unboundNow.push(cid);
422
+ }
423
+ for (const cid of toBind) {
424
+ await this.customApis.bind(agentId, cid);
425
+ boundNow.push(cid);
426
+ }
254
427
  }
255
428
  catch (err) {
256
- if (err instanceof errors_js_1.UserError)
257
- throw mapUserError(err, opts.pathOf);
258
- if (err instanceof errors_js_1.NotSupportedByServerError) {
259
- throw new errors_js_2.CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
260
- }
429
+ await rollbackBindings();
261
430
  throw err;
262
431
  }
432
+ let rec;
433
+ if (settingsChanged) {
434
+ try {
435
+ const remote = await this.http.call(() => this.transport.updateAgent(agentId, agent), { idempotent: false });
436
+ rec = settingsToRecord(remote.agentId, remote.name, remote.instructions, remote.settings, (_c = remote.liveVersion) !== null && _c !== void 0 ? _c : null, current.createdAt);
437
+ }
438
+ catch (err) {
439
+ await rollbackBindings();
440
+ if (err instanceof errors_js_1.UserError)
441
+ throw mapUserError(err, opts.pathOf);
442
+ if (err instanceof errors_js_1.NotSupportedByServerError) {
443
+ throw new errors_js_2.CoreAPIError(err.message, { status: 400, code: "unsupported_by_sarvam" });
444
+ }
445
+ throw err;
446
+ }
447
+ }
448
+ else {
449
+ rec = clone(current);
450
+ }
451
+ rec.customApis = managedEntries;
452
+ rec.unmanagedCustomApis = unmanaged;
453
+ return rec;
263
454
  }
264
455
  async list(offset = 0, limit = 20) {
265
456
  var _a;
@@ -309,11 +500,19 @@ class Agents {
309
500
  return settingsToRecord(chosen.id, chosen.name, chosen.instructions, chosen.settings, chosen.version, chosen.createdAt);
310
501
  }
311
502
  async ensureInline(spec, opts = {}) {
503
+ var _a;
504
+ const sarvamCfg = (_a = spec.sarvam) !== null && _a !== void 0 ? _a : {};
505
+ let resolvedCas = [];
506
+ if ("custom_apis" in sarvamCfg && sarvamCfg["custom_apis"] !== null && sarvamCfg["custom_apis"] !== undefined) {
507
+ const refs = (0, customApis_js_1.validateCustomApiRefs)(sarvamCfg["custom_apis"]);
508
+ resolvedCas = await this.customApis.resolveRefs(refs);
509
+ }
510
+ const managedEntries = customApiEntries(resolvedCas);
312
511
  const resolved = await this.models.resolve(spec.model);
313
512
  const temp = clone(spec);
314
513
  if (!temp.requestedModel)
315
514
  temp.requestedModel = resolved.requested;
316
- const tempSettings = specToSettings(temp, resolved.sarvamId);
515
+ const tempSettings = specToSettings(temp, resolved.sarvamId, undefined, managedEntries);
317
516
  const fp = (0, fingerprint_js_1.settingsFingerprint)(temp.instructions, tempSettings);
318
517
  temp.name = (0, ids_js_1.inlineAgentName)(fp);
319
518
  const key = temp.name;
@@ -322,9 +521,20 @@ class Agents {
322
521
  return inflight;
323
522
  const promise = (async () => {
324
523
  const existing = await this.findInlineByName(key);
325
- if (existing)
524
+ if (existing) {
525
+ if (resolvedCas.length > 0) {
526
+ const live = await this.customApis.listForAgent(existing.id);
527
+ const liveIds = new Set(live.map((i) => i.id).filter(Boolean));
528
+ for (const ca of resolvedCas) {
529
+ if (!liveIds.has(ca.id)) {
530
+ await this.customApis.bind(existing.id, ca.id);
531
+ }
532
+ }
533
+ existing.customApis = managedEntries;
534
+ }
326
535
  return existing;
327
- return this.create(temp, opts);
536
+ }
537
+ return this.create(temp, { ...opts, _resolvedCustomApis: resolvedCas });
328
538
  })();
329
539
  this.inlineInflight.set(key, promise);
330
540
  try {
@@ -0,0 +1,76 @@
1
+ import { Http } from "./http.js";
2
+ export interface CustomApiAttachedAgent {
3
+ readonly id: string;
4
+ readonly name: string;
5
+ }
6
+ export interface CustomApiAgentEntry {
7
+ readonly id: string;
8
+ readonly name: string;
9
+ readonly host: string | null;
10
+ }
11
+ export interface CustomApiInfo {
12
+ readonly id: string;
13
+ readonly name: string;
14
+ readonly description: string;
15
+ readonly host: string;
16
+ readonly authType: string;
17
+ readonly headerNames: readonly string[];
18
+ readonly enabledForChats: boolean;
19
+ readonly agents: readonly CustomApiAttachedAgent[];
20
+ readonly createdAt: string;
21
+ readonly credentialsUpdatedAt: string;
22
+ readonly enabledForChatsError: string | null;
23
+ }
24
+ export interface CustomApiHeaderInput {
25
+ name: string;
26
+ value?: string | null;
27
+ previousName?: string | null;
28
+ previous_name?: string | null;
29
+ }
30
+ export type CustomApiAuthInput = {
31
+ type: "bearer";
32
+ token: string;
33
+ } | {
34
+ type: "basic";
35
+ username: string;
36
+ password: string;
37
+ } | {
38
+ type: "headers";
39
+ headers: CustomApiHeaderInput[] | Record<string, string | null>;
40
+ };
41
+ export interface CustomApiCreateParams {
42
+ name: string;
43
+ host: string;
44
+ auth: CustomApiAuthInput;
45
+ description?: string | null;
46
+ enabledForChats?: boolean;
47
+ enabled_for_chats?: boolean;
48
+ }
49
+ export interface CustomApiUpdateParams {
50
+ name?: string;
51
+ description?: string | null;
52
+ host?: string;
53
+ auth?: CustomApiAuthInput;
54
+ enabledForChats?: boolean;
55
+ enabled_for_chats?: boolean;
56
+ }
57
+ export declare function validateCustomApiRefs(refs: unknown, param?: string): string[];
58
+ export declare class CustomApis {
59
+ private readonly http;
60
+ private readonly api;
61
+ constructor(http: Http);
62
+ list(opts?: {
63
+ q?: string;
64
+ }): Promise<CustomApiInfo[]>;
65
+ get(customApiId: string): Promise<CustomApiInfo>;
66
+ retrieve(customApiId: string): Promise<CustomApiInfo>;
67
+ create(params: CustomApiCreateParams): Promise<CustomApiInfo>;
68
+ update(customApiId: string, params: CustomApiUpdateParams): Promise<CustomApiInfo>;
69
+ delete(customApiId: string, opts?: {
70
+ force?: boolean;
71
+ }): Promise<void>;
72
+ listForAgent(agentId: string): Promise<CustomApiAgentEntry[]>;
73
+ bind(agentId: string, customApiId: string): Promise<void>;
74
+ unbind(agentId: string, customApiId: string): Promise<void>;
75
+ resolveRefs(refs: readonly string[], paramPrefix?: string): Promise<CustomApiInfo[]>;
76
+ }