sarvam-agents-client 0.5.3 → 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.
- package/dist/compat/core/agents.d.ts +6 -1
- package/dist/compat/core/agents.js +237 -27
- package/dist/compat/core/customApis.d.ts +76 -0
- package/dist/compat/core/customApis.js +424 -0
- package/dist/compat/core/data.js +1 -1
- package/dist/compat/openai/client.d.ts +3 -0
- package/dist/compat/openai/client.js +5 -1
- package/dist/compat/openai/index.d.ts +1 -0
- package/dist/compat/openai/index.js +0 -1
- package/dist/compat/openai/resources/agents.js +8 -1
- package/dist/compat/openai/resources/sarvam.d.ts +26 -0
- package/dist/compat/openai/resources/sarvam.js +56 -0
- package/dist/esm/compat/core/agents.d.ts +6 -1
- package/dist/esm/compat/core/agents.js +237 -27
- package/dist/esm/compat/core/customApis.d.ts +76 -0
- package/dist/esm/compat/core/customApis.js +419 -0
- package/dist/esm/compat/core/data.js +1 -1
- package/dist/esm/compat/openai/client.d.ts +3 -0
- package/dist/esm/compat/openai/client.js +5 -1
- package/dist/esm/compat/openai/index.d.ts +1 -0
- package/dist/esm/compat/openai/index.js +0 -1
- package/dist/esm/compat/openai/resources/agents.js +8 -1
- package/dist/esm/compat/openai/resources/sarvam.d.ts +26 -0
- package/dist/esm/compat/openai/resources/sarvam.js +51 -0
- package/package.json +1 -1
- package/src/compat/core/agents.ts +265 -36
- package/src/compat/core/customApis.ts +506 -0
- package/src/compat/core/data.ts +1 -1
- package/src/compat/openai/client.ts +7 -1
- package/src/compat/openai/index.ts +9 -0
- package/src/compat/openai/resources/agents.ts +9 -1
- package/src/compat/openai/resources/sarvam.ts +94 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CustomApis, type CustomApiAgentEntry, type CustomApiInfo } from "./customApis.js";
|
|
1
2
|
import { Http } from "./http.js";
|
|
2
3
|
import { ModelResolver } from "./models.js";
|
|
3
4
|
export interface AgentSpec {
|
|
@@ -27,17 +28,21 @@ export interface AgentRecord {
|
|
|
27
28
|
archived: boolean;
|
|
28
29
|
sarvamModel: string | null;
|
|
29
30
|
rawSettings: Record<string, unknown>;
|
|
31
|
+
customApis: CustomApiAgentEntry[];
|
|
32
|
+
unmanagedCustomApis: CustomApiAgentEntry[];
|
|
30
33
|
}
|
|
31
34
|
export declare class Agents {
|
|
32
35
|
private readonly http;
|
|
33
36
|
private readonly models;
|
|
34
37
|
private readonly transport;
|
|
35
38
|
private readonly api;
|
|
39
|
+
private readonly customApis;
|
|
36
40
|
private readonly inlineInflight;
|
|
37
|
-
constructor(http: Http, models: ModelResolver);
|
|
41
|
+
constructor(http: Http, models: ModelResolver, customApis?: CustomApis);
|
|
38
42
|
private buildRaw;
|
|
39
43
|
create(spec: AgentSpec, opts?: {
|
|
40
44
|
pathOf?: (field: string) => string;
|
|
45
|
+
_resolvedCustomApis?: CustomApiInfo[];
|
|
41
46
|
}): Promise<AgentRecord>;
|
|
42
47
|
retrieve(agentId: string): Promise<AgentRecord>;
|
|
43
48
|
update(agentId: string, change: (spec: AgentSpec) => AgentSpec, opts?: {
|
|
@@ -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 {
|
|
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
|
|
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
|
-
|
|
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
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
253
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
}
|