sarvam-agents-client 0.3.1 → 0.4.0
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/README.md +82 -0
- package/dist/apis/AgentConversationsApi.d.ts +2 -2
- package/dist/apis/AgentConversationsApi.js +2 -2
- package/dist/apis/AgentsApi.d.ts +195 -5
- package/dist/apis/AgentsApi.js +478 -5
- package/dist/apis/ArtifactsApi.d.ts +8 -4
- package/dist/apis/ArtifactsApi.js +8 -4
- package/dist/apis/ConversationsApi.d.ts +2 -0
- package/dist/apis/ConversationsApi.js +6 -0
- package/dist/compose/agent.d.ts +4 -0
- package/dist/compose/agent.js +8 -0
- package/dist/compose/deploy.d.ts +33 -7
- package/dist/compose/deploy.js +97 -19
- package/dist/compose/errors.d.ts +1 -1
- package/dist/compose/errors.js +1 -1
- package/dist/compose/index.d.ts +2 -1
- package/dist/compose/index.js +2 -1
- package/dist/compose/run.d.ts +5 -1
- package/dist/compose/run.js +9 -9
- package/dist/compose/settingsTransport.d.ts +17 -0
- package/dist/compose/settingsTransport.js +176 -12
- package/dist/compose/transport.d.ts +11 -0
- package/dist/compose/transport.js +3 -0
- package/dist/compose/webhook.d.ts +19 -0
- package/dist/compose/webhook.js +184 -0
- package/dist/esm/apis/AgentConversationsApi.d.ts +2 -2
- package/dist/esm/apis/AgentConversationsApi.js +2 -2
- package/dist/esm/apis/AgentsApi.d.ts +195 -5
- package/dist/esm/apis/AgentsApi.js +478 -5
- package/dist/esm/apis/ArtifactsApi.d.ts +8 -4
- package/dist/esm/apis/ArtifactsApi.js +8 -4
- package/dist/esm/apis/ConversationsApi.d.ts +2 -0
- package/dist/esm/apis/ConversationsApi.js +6 -0
- package/dist/esm/compose/agent.d.ts +4 -0
- package/dist/esm/compose/agent.js +8 -0
- package/dist/esm/compose/deploy.d.ts +33 -7
- package/dist/esm/compose/deploy.js +97 -20
- package/dist/esm/compose/errors.d.ts +1 -1
- package/dist/esm/compose/errors.js +1 -1
- package/dist/esm/compose/index.d.ts +2 -1
- package/dist/esm/compose/index.js +1 -1
- package/dist/esm/compose/run.d.ts +5 -1
- package/dist/esm/compose/run.js +9 -9
- package/dist/esm/compose/settingsTransport.d.ts +17 -0
- package/dist/esm/compose/settingsTransport.js +176 -12
- package/dist/esm/compose/transport.d.ts +11 -0
- package/dist/esm/compose/transport.js +2 -1
- package/dist/esm/compose/webhook.d.ts +19 -0
- package/dist/esm/compose/webhook.js +177 -0
- package/dist/esm/models/AgentAttachmentDiff.d.ts +50 -0
- package/dist/esm/models/AgentAttachmentDiff.js +55 -0
- package/dist/esm/models/AgentContentsResource.d.ts +71 -0
- package/dist/esm/models/AgentContentsResource.js +70 -0
- package/dist/esm/models/AgentDeploy.d.ts +33 -0
- package/dist/esm/models/AgentDeploy.js +41 -0
- package/dist/esm/models/AgentDraftResource.d.ts +51 -0
- package/dist/esm/models/AgentDraftResource.js +52 -0
- package/dist/esm/models/AgentFileContent.d.ts +38 -0
- package/dist/esm/models/AgentFileContent.js +47 -0
- package/dist/esm/models/AgentHistoryResource.d.ts +71 -0
- package/dist/esm/models/AgentHistoryResource.js +66 -0
- package/dist/esm/models/AgentResource.d.ts +21 -0
- package/dist/esm/models/AgentResource.js +12 -0
- package/dist/esm/models/AgentSkillFilePin.d.ts +44 -0
- package/dist/esm/models/AgentSkillFilePin.js +51 -0
- package/dist/esm/models/AgentSubagentEntry.d.ts +38 -0
- package/dist/esm/models/AgentSubagentEntry.js +47 -0
- package/dist/esm/models/AgentVersionCreate.d.ts +32 -0
- package/dist/esm/models/AgentVersionCreate.js +41 -0
- package/dist/esm/models/AgentVersionDiffResource.d.ts +87 -0
- package/dist/esm/models/AgentVersionDiffResource.js +78 -0
- package/dist/esm/models/AgentVersionPick.d.ts +32 -0
- package/dist/esm/models/AgentVersionPick.js +43 -0
- package/dist/esm/models/AgentVersionResource.d.ts +56 -0
- package/dist/esm/models/AgentVersionResource.js +57 -0
- package/dist/esm/models/AppResource.d.ts +12 -0
- package/dist/esm/models/AppResource.js +4 -0
- package/dist/esm/models/ResponseInput.d.ts +6 -0
- package/dist/esm/models/ResponseInput.js +2 -0
- package/dist/esm/models/SkillUpdateResource.d.ts +38 -0
- package/dist/esm/models/SkillUpdateResource.js +47 -0
- package/dist/esm/models/index.d.ts +13 -0
- package/dist/esm/models/index.js +13 -0
- package/dist/esm/webhooks.d.ts +203 -0
- package/dist/esm/webhooks.js +215 -0
- package/dist/models/AgentAttachmentDiff.d.ts +50 -0
- package/dist/models/AgentAttachmentDiff.js +62 -0
- package/dist/models/AgentContentsResource.d.ts +71 -0
- package/dist/models/AgentContentsResource.js +77 -0
- package/dist/models/AgentDeploy.d.ts +33 -0
- package/dist/models/AgentDeploy.js +48 -0
- package/dist/models/AgentDraftResource.d.ts +51 -0
- package/dist/models/AgentDraftResource.js +59 -0
- package/dist/models/AgentFileContent.d.ts +38 -0
- package/dist/models/AgentFileContent.js +54 -0
- package/dist/models/AgentHistoryResource.d.ts +71 -0
- package/dist/models/AgentHistoryResource.js +74 -0
- package/dist/models/AgentResource.d.ts +21 -0
- package/dist/models/AgentResource.js +13 -1
- package/dist/models/AgentSkillFilePin.d.ts +44 -0
- package/dist/models/AgentSkillFilePin.js +58 -0
- package/dist/models/AgentSubagentEntry.d.ts +38 -0
- package/dist/models/AgentSubagentEntry.js +54 -0
- package/dist/models/AgentVersionCreate.d.ts +32 -0
- package/dist/models/AgentVersionCreate.js +48 -0
- package/dist/models/AgentVersionDiffResource.d.ts +87 -0
- package/dist/models/AgentVersionDiffResource.js +85 -0
- package/dist/models/AgentVersionPick.d.ts +32 -0
- package/dist/models/AgentVersionPick.js +50 -0
- package/dist/models/AgentVersionResource.d.ts +56 -0
- package/dist/models/AgentVersionResource.js +64 -0
- package/dist/models/AppResource.d.ts +12 -0
- package/dist/models/AppResource.js +4 -0
- package/dist/models/ResponseInput.d.ts +6 -0
- package/dist/models/ResponseInput.js +2 -0
- package/dist/models/SkillUpdateResource.d.ts +38 -0
- package/dist/models/SkillUpdateResource.js +54 -0
- package/dist/models/index.d.ts +13 -0
- package/dist/models/index.js +13 -0
- package/dist/webhooks.d.ts +203 -0
- package/dist/webhooks.js +224 -0
- package/package.json +9 -1
- package/src/apis/AgentConversationsApi.ts +2 -2
- package/src/apis/AgentsApi.ts +737 -6
- package/src/apis/ArtifactsApi.ts +8 -4
- package/src/apis/ConversationsApi.ts +10 -0
- package/src/compose/agent.ts +9 -0
- package/src/compose/deploy.ts +113 -20
- package/src/compose/errors.ts +1 -1
- package/src/compose/index.ts +2 -1
- package/src/compose/run.ts +6 -2
- package/src/compose/settingsTransport.ts +167 -10
- package/src/compose/transport.ts +12 -0
- package/src/compose/webhook.ts +181 -0
- package/src/models/AgentAttachmentDiff.ts +93 -0
- package/src/models/AgentContentsResource.ts +142 -0
- package/src/models/AgentDeploy.ts +66 -0
- package/src/models/AgentDraftResource.ts +99 -0
- package/src/models/AgentFileContent.ts +75 -0
- package/src/models/AgentHistoryResource.ts +121 -0
- package/src/models/AgentResource.ts +26 -0
- package/src/models/AgentSkillFilePin.ts +84 -0
- package/src/models/AgentSubagentEntry.ts +75 -0
- package/src/models/AgentVersionCreate.ts +65 -0
- package/src/models/AgentVersionDiffResource.ts +154 -0
- package/src/models/AgentVersionPick.ts +66 -0
- package/src/models/AgentVersionResource.ts +101 -0
- package/src/models/AppResource.ts +16 -0
- package/src/models/ResponseInput.ts +8 -0
- package/src/models/SkillUpdateResource.ts +75 -0
- package/src/models/index.ts +13 -0
- package/src/webhooks.ts +390 -0
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
import { createHash, randomUUID } from "node:crypto";
|
|
3
3
|
import { AgentsApi } from "../apis/AgentsApi.js";
|
|
4
4
|
import { ConversationsApi } from "../apis/ConversationsApi.js";
|
|
5
|
+
import { CustomAPIsApi } from "../apis/CustomAPIsApi.js";
|
|
5
6
|
import { ResponsesApi } from "../apis/ResponsesApi.js";
|
|
6
7
|
import { FetchError, ResponseError } from "../runtime.js";
|
|
7
8
|
import { answerClarify, postToolResults, reconnect, streamResponse } from "../streaming.js";
|
|
8
9
|
import { NotSupportedByServerError, RunError, UserError } from "./errors.js";
|
|
9
10
|
import { chunksFromUiMessage, fromUiChunk } from "./events.js";
|
|
11
|
+
import { VERSION_NOTE } from "./transport.js";
|
|
10
12
|
const PAGE = 100;
|
|
11
13
|
/** A post the server is still delivering (409 in_progress) is retried this many times, Retry-After apart. */
|
|
12
14
|
const IN_PROGRESS_RETRIES = 5;
|
|
@@ -97,6 +99,24 @@ function firstDetail(error) {
|
|
|
97
99
|
const first = Array.isArray(details) ? details[0] : undefined;
|
|
98
100
|
return first !== null && typeof first === "object" && !Array.isArray(first) ? { ...first } : {};
|
|
99
101
|
}
|
|
102
|
+
/** A deploy's 409 MISSING_DEPENDENCIES (the version uses a skill, app, knowledge base or sub-agent that was deleted
|
|
103
|
+
* since) as a UserError naming each one, else null. */
|
|
104
|
+
async function missingDependencies(err, agentId) {
|
|
105
|
+
if ((await reason(err, 409)) !== "MISSING_DEPENDENCIES")
|
|
106
|
+
return null;
|
|
107
|
+
const missing = firstDetail(await chatError(err))["missing"];
|
|
108
|
+
const items = Array.isArray(missing)
|
|
109
|
+
? missing.filter((m) => m !== null && typeof m === "object")
|
|
110
|
+
: [];
|
|
111
|
+
const named = items.map((m) => `${m["kind"]} '${m["name"]}' (${m["id"]})`).join(", ");
|
|
112
|
+
return new UserError(`agent ${agentId}: the new version can't go live because it uses something that was deleted` +
|
|
113
|
+
`${named ? ": " + named : ""}. Remove it from the agent, then deploy again`);
|
|
114
|
+
}
|
|
115
|
+
/** `AgentResource.versionState`; undefined from a server without versions (or an agent that is not managed). */
|
|
116
|
+
function versionStateOf(resource) {
|
|
117
|
+
var _a;
|
|
118
|
+
return (_a = resource.versionState) !== null && _a !== void 0 ? _a : undefined;
|
|
119
|
+
}
|
|
100
120
|
function retryAfterMs(err) {
|
|
101
121
|
var _a;
|
|
102
122
|
const seconds = Number((_a = err.response.headers.get("retry-after")) !== null && _a !== void 0 ? _a : 2);
|
|
@@ -165,6 +185,14 @@ class Finished {
|
|
|
165
185
|
}
|
|
166
186
|
close() { }
|
|
167
187
|
}
|
|
188
|
+
/** `settings` for a new agent's first write: no `webhooks` (the server checks them against the agent's bound
|
|
189
|
+
* credentials, and a new agent has none yet) and no `sdk_hash` (it covers the webhooks, so an agent left in this
|
|
190
|
+
* state would otherwise read as deployed and never get them). */
|
|
191
|
+
function withoutWebhooks(settings) {
|
|
192
|
+
const { webhooks: _webhooks, ...out } = settings;
|
|
193
|
+
const { sdk_hash: _hash, ...metadata } = settings["metadata"];
|
|
194
|
+
return { ...out, metadata };
|
|
195
|
+
}
|
|
168
196
|
function isNotFound(err) {
|
|
169
197
|
return err instanceof ResponseError && err.response.status === 404;
|
|
170
198
|
}
|
|
@@ -201,6 +229,7 @@ export class SettingsTransport {
|
|
|
201
229
|
this.agents = new AgentsApi(config);
|
|
202
230
|
this.conversations = new ConversationsApi(config);
|
|
203
231
|
this.responses = new ResponsesApi(config);
|
|
232
|
+
this.customApis = new CustomAPIsApi(config);
|
|
204
233
|
}
|
|
205
234
|
// -- agents ---------------------------------------------------------------------------------------------
|
|
206
235
|
async findAgents(sdkName) {
|
|
@@ -229,20 +258,28 @@ export class SettingsTransport {
|
|
|
229
258
|
return this.remote(resource, await this.instructions(agentId));
|
|
230
259
|
}
|
|
231
260
|
async createAgent(agent) {
|
|
232
|
-
var _a, _b;
|
|
261
|
+
var _a, _b, _c;
|
|
233
262
|
const settings = agent.toSettings();
|
|
263
|
+
const webhooks = ((_a = settings["webhooks"]) !== null && _a !== void 0 ? _a : []);
|
|
264
|
+
// The server checks webhooks against the credentials bound to the agent, and a new agent has none: create it
|
|
265
|
+
// without them, bind the Custom APIs they name, then write the settings in full.
|
|
266
|
+
const { bind, unbound } = await this.webhookCredentials(webhooks);
|
|
234
267
|
let resource;
|
|
235
268
|
try {
|
|
236
269
|
resource = await this.agents.createAgent({
|
|
237
|
-
agentCreate: {
|
|
270
|
+
agentCreate: {
|
|
271
|
+
name: agent.name, instructions: agent.instructions,
|
|
272
|
+
settings: webhooks.length > 0 ? withoutWebhooks(settings) : settings,
|
|
273
|
+
},
|
|
238
274
|
});
|
|
239
275
|
}
|
|
240
276
|
catch (err) {
|
|
241
277
|
// A 422 stored nothing, so there is no agent to delete.
|
|
242
|
-
throw (
|
|
278
|
+
throw (_b = (await invalidSettings(err))) !== null && _b !== void 0 ? _b : err;
|
|
243
279
|
}
|
|
244
280
|
let envelope;
|
|
245
281
|
let warnings;
|
|
282
|
+
let live;
|
|
246
283
|
// The create response never echoes settings, so support is read back from the settings route, which 404s
|
|
247
284
|
// when the server has settings switched off (create then silently drops them).
|
|
248
285
|
try {
|
|
@@ -254,9 +291,25 @@ export class SettingsTransport {
|
|
|
254
291
|
throw new NotSupportedByServerError(["settings"]);
|
|
255
292
|
throw err;
|
|
256
293
|
}
|
|
294
|
+
if (webhooks.length > 0) {
|
|
295
|
+
for (const { path, name, customApiId } of bind)
|
|
296
|
+
await this.bind(resource.id, path, name, customApiId);
|
|
297
|
+
try {
|
|
298
|
+
envelope = await this.agents.putAgentSettings({ agentId: resource.id, body: settings });
|
|
299
|
+
}
|
|
300
|
+
catch (err) {
|
|
301
|
+
const invalid = await invalidSettings(err);
|
|
302
|
+
if (invalid === null)
|
|
303
|
+
throw err;
|
|
304
|
+
throw new UserError([invalid.message, ...unbound].join("; "), invalid.issues);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
257
307
|
warnings = checkWarnings(envelope, settings);
|
|
308
|
+
// Accepted unchecked (webhooks switched off): refused once they are on.
|
|
309
|
+
for (const sentence of unbound)
|
|
310
|
+
console.warn(`agent ${resource.id}: ${sentence}`);
|
|
258
311
|
checkMetadata(envelope, agent);
|
|
259
|
-
await this.
|
|
312
|
+
live = await this.goLive(resource.id, versionStateOf(resource), agent);
|
|
260
313
|
}
|
|
261
314
|
catch (err) {
|
|
262
315
|
// Don't leave an orphan agent behind; a failing cleanup must not mask the original error.
|
|
@@ -268,19 +321,77 @@ export class SettingsTransport {
|
|
|
268
321
|
}
|
|
269
322
|
throw err;
|
|
270
323
|
}
|
|
271
|
-
return {
|
|
324
|
+
return withVersion({
|
|
272
325
|
agentId: resource.id,
|
|
273
326
|
name: agent.name,
|
|
274
327
|
instructions: agent.instructions,
|
|
275
|
-
settings: { ...((
|
|
328
|
+
settings: { ...((_c = envelope.settings) !== null && _c !== void 0 ? _c : {}) },
|
|
276
329
|
warnings,
|
|
277
|
-
};
|
|
330
|
+
}, live, versionStateOf(resource));
|
|
331
|
+
}
|
|
332
|
+
/** The caller's Custom APIs the webhooks name, to bind to a new agent: one entry per name that matches exactly
|
|
333
|
+
* one, and a sentence per name that matches none or several (left to the server, which refuses it only when it
|
|
334
|
+
* checks credentials). */
|
|
335
|
+
async webhookCredentials(webhooks) {
|
|
336
|
+
const bind = [];
|
|
337
|
+
const unbound = [];
|
|
338
|
+
const seen = new Set();
|
|
339
|
+
for (const [i, hook] of webhooks.entries()) {
|
|
340
|
+
const name = hook.secret_credential;
|
|
341
|
+
if (seen.has(name))
|
|
342
|
+
continue;
|
|
343
|
+
seen.add(name);
|
|
344
|
+
const path = `webhooks.${i}.secret_credential`;
|
|
345
|
+
const ids = await this.customApiIds(name);
|
|
346
|
+
if (ids.length === 1) {
|
|
347
|
+
bind.push({ path, name, customApiId: ids[0] });
|
|
348
|
+
}
|
|
349
|
+
else if (ids.length === 0) {
|
|
350
|
+
unbound.push(`${path}: you have no Custom API named '${name}' to bind to the new agent; create it, then ` +
|
|
351
|
+
"deploy again");
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
unbound.push(`${path}: ${ids.length} of your Custom APIs are named '${name}', so none was bound to the new ` +
|
|
355
|
+
"agent; rename or delete all but one, then deploy again");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return { bind, unbound };
|
|
359
|
+
}
|
|
360
|
+
/** The ids of the caller's own Custom APIs named exactly `name` (`q` also matches substrings and hosts). */
|
|
361
|
+
async customApiIds(name) {
|
|
362
|
+
const ids = [];
|
|
363
|
+
let offset = 0;
|
|
364
|
+
for (;;) {
|
|
365
|
+
const result = await this.customApis.listCustomApis({ q: name, limit: PAGE, offset });
|
|
366
|
+
const items = result.data;
|
|
367
|
+
for (const item of items)
|
|
368
|
+
if (item.name === name)
|
|
369
|
+
ids.push(item.id);
|
|
370
|
+
offset += items.length;
|
|
371
|
+
if (items.length === 0 || !result.hasMore)
|
|
372
|
+
return ids;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async bind(agentId, path, name, customApiId) {
|
|
376
|
+
var _a;
|
|
377
|
+
try {
|
|
378
|
+
await this.customApis.bindCustomApi({ agentId, bindCustomApiBody: { customApiId } });
|
|
379
|
+
}
|
|
380
|
+
catch (err) {
|
|
381
|
+
const status = err instanceof ResponseError ? err.response.status : 0;
|
|
382
|
+
if (!(err instanceof ResponseError) || status < 400 || status >= 500)
|
|
383
|
+
throw err;
|
|
384
|
+
const message = (_a = (await chatError(err))) === null || _a === void 0 ? void 0 : _a["message"];
|
|
385
|
+
throw new UserError(`${path}: could not bind the Custom API '${name}' to the new agent: ` +
|
|
386
|
+
(typeof message === "string" && message ? message : `HTTP ${status}`));
|
|
387
|
+
}
|
|
278
388
|
}
|
|
279
389
|
async updateAgent(agentId, agent) {
|
|
280
390
|
var _a, _b;
|
|
281
391
|
const settings = agent.toSettings();
|
|
282
392
|
// findAgents searches by display name, so an adopted (or dashboard-renamed) agent must carry sdk_name.
|
|
283
|
-
|
|
393
|
+
const current = await this.agents.getAgent({ agentId });
|
|
394
|
+
if (current.name !== agent.name) {
|
|
284
395
|
await this.agents.updateAgent({ agentId, agentUpdate: { name: agent.name } });
|
|
285
396
|
}
|
|
286
397
|
// Rejections are only visible after the server stored the settings, so keep what was there to put it back.
|
|
@@ -326,14 +437,51 @@ export class SettingsTransport {
|
|
|
326
437
|
throw err;
|
|
327
438
|
}
|
|
328
439
|
checkMetadata(envelope, agent);
|
|
329
|
-
await this.
|
|
330
|
-
return {
|
|
440
|
+
const live = await this.goLive(agentId, versionStateOf(current), agent);
|
|
441
|
+
return withVersion({
|
|
331
442
|
agentId,
|
|
332
443
|
name: agent.name,
|
|
333
444
|
instructions: agent.instructions,
|
|
334
445
|
settings: { ...((_b = envelope.settings) !== null && _b !== void 0 ? _b : {}) },
|
|
335
446
|
warnings,
|
|
336
|
-
};
|
|
447
|
+
}, live, versionStateOf(current));
|
|
448
|
+
}
|
|
449
|
+
/** Make what was just written what end users get. A server without versions (no `versionState`) gets the bodiless
|
|
450
|
+
* deploy 0.3 sent; otherwise the draft is saved as a version and that version deployed. */
|
|
451
|
+
async goLive(agentId, versionState, agent) {
|
|
452
|
+
if (versionState === undefined) {
|
|
453
|
+
await this.agents.deployAgent({ agentId }); // idempotent; moves an adopted draft to deployed
|
|
454
|
+
return undefined;
|
|
455
|
+
}
|
|
456
|
+
return this.publish(agentId, VERSION_NOTE);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Save the draft as the next version and make it live. 409 NO_CHANGES means the newest saved version already is the
|
|
460
|
+
* draft, so that version is deployed, by its number (never "the newest", which a save landing in between could
|
|
461
|
+
* change).
|
|
462
|
+
*/
|
|
463
|
+
async publish(agentId, note) {
|
|
464
|
+
var _a, _b;
|
|
465
|
+
let version;
|
|
466
|
+
try {
|
|
467
|
+
version = (await this.agents.saveVersion({ agentId, agentVersionCreate: { note } })).version;
|
|
468
|
+
}
|
|
469
|
+
catch (err) {
|
|
470
|
+
if ((await reason(err, 409)) !== "NO_CHANGES")
|
|
471
|
+
throw err;
|
|
472
|
+
const newest = (await this.agents.getDraft({ agentId })).basedOnVersion;
|
|
473
|
+
if (newest === undefined || newest === null)
|
|
474
|
+
throw err;
|
|
475
|
+
version = newest;
|
|
476
|
+
}
|
|
477
|
+
let resource;
|
|
478
|
+
try {
|
|
479
|
+
resource = await this.agents.deployAgent({ agentId, agentDeploy: { version } });
|
|
480
|
+
}
|
|
481
|
+
catch (err) {
|
|
482
|
+
throw (_a = (await missingDependencies(err, agentId))) !== null && _a !== void 0 ? _a : err;
|
|
483
|
+
}
|
|
484
|
+
return (_b = resource.liveVersion) !== null && _b !== void 0 ? _b : undefined;
|
|
337
485
|
}
|
|
338
486
|
async deleteAgent(agentId) {
|
|
339
487
|
await this.agents.deleteAgent({ agentId });
|
|
@@ -511,9 +659,25 @@ export class SettingsTransport {
|
|
|
511
659
|
}
|
|
512
660
|
remote(resource, instructions) {
|
|
513
661
|
var _a;
|
|
514
|
-
|
|
662
|
+
const remote = {
|
|
663
|
+
agentId: resource.id, name: resource.name, instructions, settings: { ...((_a = resource.settings) !== null && _a !== void 0 ? _a : {}) }, warnings: [],
|
|
664
|
+
};
|
|
665
|
+
// Left out, not set to undefined, when the server has no versions: the shape a 0.3 transport returned.
|
|
666
|
+
if (resource.liveVersion !== undefined && resource.liveVersion !== null)
|
|
667
|
+
remote.liveVersion = resource.liveVersion;
|
|
668
|
+
const state = versionStateOf(resource);
|
|
669
|
+
if (state !== undefined)
|
|
670
|
+
remote.versionState = state;
|
|
671
|
+
return remote;
|
|
515
672
|
}
|
|
516
673
|
}
|
|
674
|
+
/** `remote` with the version fields set when known (left out otherwise, as a server without versions returns it). After a
|
|
675
|
+
* publish the agent is live whatever state it was read in. */
|
|
676
|
+
function withVersion(remote, live, state) {
|
|
677
|
+
if (live !== undefined)
|
|
678
|
+
return { ...remote, liveVersion: live, versionState: "live" };
|
|
679
|
+
return state === undefined ? remote : { ...remote, versionState: state };
|
|
680
|
+
}
|
|
517
681
|
/** The server's 422 CHAT_VALIDATION / invalid_agent_settings rejection as a UserError, else null. */
|
|
518
682
|
async function invalidSettings(err) {
|
|
519
683
|
if (!(err instanceof ResponseError) || err.response.status !== 422)
|
|
@@ -14,7 +14,15 @@ export interface RemoteAgent {
|
|
|
14
14
|
instructions: string;
|
|
15
15
|
settings: Record<string, unknown>;
|
|
16
16
|
warnings: SettingsWarning[];
|
|
17
|
+
/** The version end users get (`AgentResource.liveVersion`); undefined when nothing is live or the server has no
|
|
18
|
+
* versions. */
|
|
19
|
+
liveVersion?: number;
|
|
20
|
+
/** `"live"`, `"paused"` or `"not_live"` (`AgentResource.versionState`); undefined when the server has no versions,
|
|
21
|
+
* in which case deploy() never calls `publish`. */
|
|
22
|
+
versionState?: string;
|
|
17
23
|
}
|
|
24
|
+
/** The note on every version deploy() saves, so the dashboard's version list shows which came from code. */
|
|
25
|
+
export declare const VERSION_NOTE = "Deployed from the SDK";
|
|
18
26
|
/** Async iterable of Events; the ids are filled in as the stream progresses. */
|
|
19
27
|
export interface TurnStream extends AsyncIterable<Event> {
|
|
20
28
|
readonly conversationId: string | null;
|
|
@@ -37,6 +45,9 @@ export interface Transport {
|
|
|
37
45
|
createAgent(agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
|
|
38
46
|
updateAgent(agentId: string, agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
|
|
39
47
|
deleteAgent(agentId: string): Promise<void>;
|
|
48
|
+
/** Save the draft as the next version and make that version live; resolves to the live version. Called only for
|
|
49
|
+
* an agent whose `versionState` is set. Optional so a transport written before versions still type-checks. */
|
|
50
|
+
publish?(agentId: string, note: string): Promise<number | undefined>;
|
|
40
51
|
startTurn(agentId: string, conversationId: string | null, text: string, signal?: AbortSignal): Promise<TurnStream>;
|
|
41
52
|
answerClarify(conversationId: string, requestId: string, answers: Record<string, unknown>, signal?: AbortSignal): Promise<TurnStream>;
|
|
42
53
|
resume(conversationId: string, responseId: string, lastEventId: string | null, signal?: AbortSignal): Promise<TurnStream>;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
/** The note on every version deploy() saves, so the dashboard's version list shows which came from code. */
|
|
2
|
+
export const VERSION_NOTE = "Deployed from the SDK";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { WebhookEventType } from "../webhooks.js";
|
|
2
|
+
/** Where the agent reports a finished, failed or paused turn (agent.json `webhooks[]`). */
|
|
3
|
+
export interface Webhook {
|
|
4
|
+
/** An `https://` URL on port 443 with a DNS host; a query string is allowed. */
|
|
5
|
+
url: string;
|
|
6
|
+
/** 1 to 4 distinct events. `approval.requested` is reserved: accepted, not sent yet. */
|
|
7
|
+
events: WebhookEventType[];
|
|
8
|
+
/** The vault credential holding the `whsec_` signing secret (agent.json `secret_credential`). */
|
|
9
|
+
secretCredential: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const WEBHOOKS_MAX = 5;
|
|
12
|
+
/** Why `url` is not an allowed webhook URL, or null. Every URL the server refuses is refused, with the server's
|
|
13
|
+
* sentence for every URL Python's `urlsplit` parses; for a netloc its NFKC check refuses, the sentence may differ. */
|
|
14
|
+
export declare function urlProblem(url: string): string | null;
|
|
15
|
+
/** The server's pure `webhooks` rules (types first); throws `UserError` naming the agent.json path. */
|
|
16
|
+
export declare function checkWebhooks(webhooks: unknown): void;
|
|
17
|
+
/** Only the three fields, in agent.json's names (a copy). */
|
|
18
|
+
export declare function copyWebhooks(webhooks: readonly Webhook[]): Webhook[];
|
|
19
|
+
export declare function webhooksJson(webhooks: readonly Webhook[]): Record<string, unknown>[];
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `webhooks` entries of `agent.json`, checked locally with the server's cheap rules (as the Python SDK does).
|
|
3
|
+
*
|
|
4
|
+
* The server (`claw_common/agent_settings/webhooks.py`) also checks that the host resolves to a public address and
|
|
5
|
+
* that `secret_credential` names a vault credential bound to the agent whose allowed hosts cover the URL; those need
|
|
6
|
+
* I/O and stay on the server, which refuses the save with a 422 that `deploy` throws as `UserError`. A new agent has
|
|
7
|
+
* no credentials bound yet, so the deploy that creates it binds the caller's Custom API of each named credential first
|
|
8
|
+
* (`SettingsTransport.createAgent`).
|
|
9
|
+
*/
|
|
10
|
+
import { UserError } from "./errors.js";
|
|
11
|
+
import { WEBHOOK_EVENTS } from "../webhooks.js";
|
|
12
|
+
export const WEBHOOKS_MAX = 5;
|
|
13
|
+
const WEBHOOK_URL_MAX = 2048;
|
|
14
|
+
const SECRET_CREDENTIAL_MAX = 256;
|
|
15
|
+
const LABEL = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
16
|
+
// A last label that is a number (decimal, or 0x hex) makes resolvers read the whole host as an IPv4 address.
|
|
17
|
+
const NUMERIC_LABEL = /^(?:[0-9]+|0x[0-9a-f]*)$/;
|
|
18
|
+
const SCHEME = /^[A-Za-z][A-Za-z0-9+\-.]*$/;
|
|
19
|
+
/** Length in code points, as Python's `len`. */
|
|
20
|
+
function codePoints(text) {
|
|
21
|
+
return Array.from(text).length;
|
|
22
|
+
}
|
|
23
|
+
/** `[scheme, netloc, path, query]` as Python 3.12's `urlsplit` splits a URL without a fragment. */
|
|
24
|
+
function split(url) {
|
|
25
|
+
let rest = url;
|
|
26
|
+
let scheme = "";
|
|
27
|
+
const i = rest.indexOf(":");
|
|
28
|
+
if (i > 0 && SCHEME.test(rest.slice(0, i))) {
|
|
29
|
+
scheme = rest.slice(0, i).toLowerCase();
|
|
30
|
+
rest = rest.slice(i + 1);
|
|
31
|
+
}
|
|
32
|
+
let netloc = "";
|
|
33
|
+
if (rest.slice(0, 2) === "//") {
|
|
34
|
+
let end = rest.length;
|
|
35
|
+
for (const c of "/?#") {
|
|
36
|
+
const at = rest.indexOf(c, 2);
|
|
37
|
+
if (at >= 0)
|
|
38
|
+
end = Math.min(end, at);
|
|
39
|
+
}
|
|
40
|
+
netloc = rest.slice(2, end);
|
|
41
|
+
rest = rest.slice(end);
|
|
42
|
+
}
|
|
43
|
+
let query = "";
|
|
44
|
+
const q = rest.indexOf("?");
|
|
45
|
+
if (q >= 0) {
|
|
46
|
+
query = rest.slice(q + 1);
|
|
47
|
+
rest = rest.slice(0, q);
|
|
48
|
+
}
|
|
49
|
+
return [scheme, netloc, rest, query];
|
|
50
|
+
}
|
|
51
|
+
/** Python's `str.partition`. */
|
|
52
|
+
function partition(text, sep) {
|
|
53
|
+
const at = text.indexOf(sep);
|
|
54
|
+
return at < 0 ? [text, "", ""] : [text.slice(0, at), sep, text.slice(at + sep.length)];
|
|
55
|
+
}
|
|
56
|
+
/** `urlsplit`'s host (lowercased) and raw port (`null` when absent or empty). */
|
|
57
|
+
function hostPort(netloc) {
|
|
58
|
+
const hostinfo = netloc.slice(netloc.lastIndexOf("@") + 1);
|
|
59
|
+
const [, bracket, bracketed] = partition(hostinfo, "[");
|
|
60
|
+
let host;
|
|
61
|
+
let port;
|
|
62
|
+
if (bracket) {
|
|
63
|
+
const [inner, , after] = partition(bracketed, "]");
|
|
64
|
+
host = inner;
|
|
65
|
+
port = partition(after, ":")[2];
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
[host, , port] = partition(hostinfo, ":");
|
|
69
|
+
}
|
|
70
|
+
return [host.toLowerCase(), port || null];
|
|
71
|
+
}
|
|
72
|
+
function stripTrailingDots(host) {
|
|
73
|
+
return host.replace(/\.+$/, "");
|
|
74
|
+
}
|
|
75
|
+
/** Why `url` is not an allowed webhook URL, or null. Every URL the server refuses is refused, with the server's
|
|
76
|
+
* sentence for every URL Python's `urlsplit` parses; for a netloc its NFKC check refuses, the sentence may differ. */
|
|
77
|
+
export function urlProblem(url) {
|
|
78
|
+
if (codePoints(url) > WEBHOOK_URL_MAX)
|
|
79
|
+
return `url must be at most ${WEBHOOK_URL_MAX} characters.`;
|
|
80
|
+
for (let i = 0; i < url.length; i++) {
|
|
81
|
+
const code = url.charCodeAt(i);
|
|
82
|
+
if (code <= 0x20 || code === 0x7f)
|
|
83
|
+
return "url must not contain spaces or control characters.";
|
|
84
|
+
}
|
|
85
|
+
if (url.indexOf("#") !== -1)
|
|
86
|
+
return "url must not have a fragment.";
|
|
87
|
+
const [scheme, netloc, path] = split(url);
|
|
88
|
+
if (scheme !== "https")
|
|
89
|
+
return "url must start with https://.";
|
|
90
|
+
if (netloc.indexOf("@") !== -1)
|
|
91
|
+
return "url must not contain a user name or password.";
|
|
92
|
+
// urlsplit refuses unbalanced brackets outright
|
|
93
|
+
if ((netloc.indexOf("[") !== -1) !== (netloc.indexOf("]") !== -1)) {
|
|
94
|
+
return "url host must be a DNS name, not an IP address.";
|
|
95
|
+
}
|
|
96
|
+
const [rawHost, port] = hostPort(netloc);
|
|
97
|
+
if (port !== null) {
|
|
98
|
+
if (!/^[0-9]+$/.test(port) || Number(port) > 65535) {
|
|
99
|
+
return "url has an invalid port; leave the port out (443 is used).";
|
|
100
|
+
}
|
|
101
|
+
if (Number(port) !== 443)
|
|
102
|
+
return "url must use port 443; leave the port out.";
|
|
103
|
+
}
|
|
104
|
+
const host = stripTrailingDots(rawHost);
|
|
105
|
+
if (!host)
|
|
106
|
+
return "url must name a host, like https://hooks.example.com/panja.";
|
|
107
|
+
if (netloc.indexOf("[") !== -1)
|
|
108
|
+
return "url host must be a DNS name, not an IP address.";
|
|
109
|
+
const labels = host.split(".");
|
|
110
|
+
if (!labels.every((label) => LABEL.test(label)))
|
|
111
|
+
return "url host must be a DNS name, like hooks.example.com.";
|
|
112
|
+
if (NUMERIC_LABEL.test(labels[labels.length - 1]))
|
|
113
|
+
return "url host must be a DNS name, not an IP address.";
|
|
114
|
+
if (path.split("/").some((seg) => seg === "." || seg === ".."))
|
|
115
|
+
return "url path must not contain . or .. segments.";
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
/** Host (lowercased, no trailing dot, no port), path and query: two URLs with one key are one endpoint. */
|
|
119
|
+
function urlKey(url) {
|
|
120
|
+
const [, netloc, path, query] = split(url);
|
|
121
|
+
return `${stripTrailingDots(hostPort(netloc)[0])}${path}?${query}`;
|
|
122
|
+
}
|
|
123
|
+
/** The server's pure `webhooks` rules (types first); throws `UserError` naming the agent.json path. */
|
|
124
|
+
export function checkWebhooks(webhooks) {
|
|
125
|
+
if (!Array.isArray(webhooks))
|
|
126
|
+
throw new UserError("webhooks: must be a list of webhooks");
|
|
127
|
+
if (webhooks.length > WEBHOOKS_MAX) {
|
|
128
|
+
throw new UserError(`webhooks: webhooks may hold at most ${WEBHOOKS_MAX} endpoints.`);
|
|
129
|
+
}
|
|
130
|
+
const seen = new Set();
|
|
131
|
+
webhooks.forEach((hook, i) => {
|
|
132
|
+
const where = `webhooks.${i}.`;
|
|
133
|
+
if (typeof hook !== "object" || hook === null || Array.isArray(hook)) {
|
|
134
|
+
throw new UserError(`webhooks.${i}: must be an object with url, events and secretCredential`);
|
|
135
|
+
}
|
|
136
|
+
const { url, events, secretCredential } = hook;
|
|
137
|
+
if (typeof url !== "string")
|
|
138
|
+
throw new UserError(where + "url: must be a string");
|
|
139
|
+
if (!Array.isArray(events) || !events.every((e) => typeof e === "string")) {
|
|
140
|
+
throw new UserError(where + "events: must be a list of event names");
|
|
141
|
+
}
|
|
142
|
+
if (typeof secretCredential !== "string")
|
|
143
|
+
throw new UserError(where + "secretCredential: must be a string");
|
|
144
|
+
// Python's Webhook is extra="forbid": a key dropped silently (a misplaced secret) would never verify.
|
|
145
|
+
const extra = Object.keys(hook).find((k) => k !== "url" && k !== "events" && k !== "secretCredential");
|
|
146
|
+
if (extra !== undefined)
|
|
147
|
+
throw new UserError(where + extra + ": Extra inputs are not permitted");
|
|
148
|
+
const problem = urlProblem(url);
|
|
149
|
+
if (problem !== null)
|
|
150
|
+
throw new UserError(where + "url: " + problem);
|
|
151
|
+
if (events.length < 1 || events.length > WEBHOOK_EVENTS.length) {
|
|
152
|
+
throw new UserError(where + `events: events must list 1 to ${WEBHOOK_EVENTS.length} events.`);
|
|
153
|
+
}
|
|
154
|
+
if (events.some((e) => WEBHOOK_EVENTS.indexOf(e) === -1)) {
|
|
155
|
+
throw new UserError(where + `events: events must be from: ${WEBHOOK_EVENTS.join(", ")}.`);
|
|
156
|
+
}
|
|
157
|
+
if (new Set(events).size !== events.length)
|
|
158
|
+
throw new UserError(where + "events: events must not repeat an event.");
|
|
159
|
+
const n = codePoints(secretCredential);
|
|
160
|
+
if (n < 1 || n > SECRET_CREDENTIAL_MAX) {
|
|
161
|
+
throw new UserError(where + "secret_credential: secret_credential must be the name of a vault credential " +
|
|
162
|
+
`(1 to ${SECRET_CREDENTIAL_MAX} characters).`);
|
|
163
|
+
}
|
|
164
|
+
const key = urlKey(url);
|
|
165
|
+
if (seen.has(key)) {
|
|
166
|
+
throw new UserError(where + "url: webhooks must not list the same url twice; put all its events on one entry.");
|
|
167
|
+
}
|
|
168
|
+
seen.add(key);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/** Only the three fields, in agent.json's names (a copy). */
|
|
172
|
+
export function copyWebhooks(webhooks) {
|
|
173
|
+
return webhooks.map((h) => ({ url: h.url, events: [...h.events], secretCredential: h.secretCredential }));
|
|
174
|
+
}
|
|
175
|
+
export function webhooksJson(webhooks) {
|
|
176
|
+
return webhooks.map((h) => ({ url: h.url, events: [...h.events], secret_credential: h.secretCredential }));
|
|
177
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sarvam Agents API
|
|
3
|
+
* Chat with a Sarvam agent, exchange files with it, and set it up (instructions, skills, knowledge bases, connectors, deploy) from your own code. Authenticate with a service token sent as a Bearer token, and send the header X-Panja-Origin: managed_agents on every request.
|
|
4
|
+
*
|
|
5
|
+
* The version of the OpenAPI document: 0.1.0
|
|
6
|
+
* Contact: support@sarvam.ai
|
|
7
|
+
*
|
|
8
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
9
|
+
* https://openapi-generator.tech
|
|
10
|
+
* Do not edit the class manually.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @export
|
|
15
|
+
* @interface AgentAttachmentDiff
|
|
16
|
+
*/
|
|
17
|
+
export interface AgentAttachmentDiff {
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @type {Array<string>}
|
|
21
|
+
* @memberof AgentAttachmentDiff
|
|
22
|
+
*/
|
|
23
|
+
added: Array<string>;
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @type {Array<string>}
|
|
27
|
+
* @memberof AgentAttachmentDiff
|
|
28
|
+
*/
|
|
29
|
+
removed: Array<string>;
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* @type {Array<string>}
|
|
33
|
+
* @memberof AgentAttachmentDiff
|
|
34
|
+
*/
|
|
35
|
+
switchedOn: Array<string>;
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* @type {Array<string>}
|
|
39
|
+
* @memberof AgentAttachmentDiff
|
|
40
|
+
*/
|
|
41
|
+
switchedOff: Array<string>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if a given object implements the AgentAttachmentDiff interface.
|
|
45
|
+
*/
|
|
46
|
+
export declare function instanceOfAgentAttachmentDiff(value: object): value is AgentAttachmentDiff;
|
|
47
|
+
export declare function AgentAttachmentDiffFromJSON(json: any): AgentAttachmentDiff;
|
|
48
|
+
export declare function AgentAttachmentDiffFromJSONTyped(json: any, ignoreDiscriminator: boolean): AgentAttachmentDiff;
|
|
49
|
+
export declare function AgentAttachmentDiffToJSON(json: any): AgentAttachmentDiff;
|
|
50
|
+
export declare function AgentAttachmentDiffToJSONTyped(value?: AgentAttachmentDiff | null, ignoreDiscriminator?: boolean): any;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* Sarvam Agents API
|
|
5
|
+
* Chat with a Sarvam agent, exchange files with it, and set it up (instructions, skills, knowledge bases, connectors, deploy) from your own code. Authenticate with a service token sent as a Bearer token, and send the header X-Panja-Origin: managed_agents on every request.
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 0.1.0
|
|
8
|
+
* Contact: support@sarvam.ai
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Check if a given object implements the AgentAttachmentDiff interface.
|
|
16
|
+
*/
|
|
17
|
+
export function instanceOfAgentAttachmentDiff(value) {
|
|
18
|
+
if (!('added' in value) || value['added'] === undefined)
|
|
19
|
+
return false;
|
|
20
|
+
if (!('removed' in value) || value['removed'] === undefined)
|
|
21
|
+
return false;
|
|
22
|
+
if (!('switchedOn' in value) || value['switchedOn'] === undefined)
|
|
23
|
+
return false;
|
|
24
|
+
if (!('switchedOff' in value) || value['switchedOff'] === undefined)
|
|
25
|
+
return false;
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function AgentAttachmentDiffFromJSON(json) {
|
|
29
|
+
return AgentAttachmentDiffFromJSONTyped(json, false);
|
|
30
|
+
}
|
|
31
|
+
export function AgentAttachmentDiffFromJSONTyped(json, ignoreDiscriminator) {
|
|
32
|
+
if (json == null) {
|
|
33
|
+
return json;
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
'added': json['added'],
|
|
37
|
+
'removed': json['removed'],
|
|
38
|
+
'switchedOn': json['switchedOn'],
|
|
39
|
+
'switchedOff': json['switchedOff'],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function AgentAttachmentDiffToJSON(json) {
|
|
43
|
+
return AgentAttachmentDiffToJSONTyped(json, false);
|
|
44
|
+
}
|
|
45
|
+
export function AgentAttachmentDiffToJSONTyped(value, ignoreDiscriminator = false) {
|
|
46
|
+
if (value == null) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
'added': value['added'],
|
|
51
|
+
'removed': value['removed'],
|
|
52
|
+
'switchedOn': value['switchedOn'],
|
|
53
|
+
'switchedOff': value['switchedOff'],
|
|
54
|
+
};
|
|
55
|
+
}
|