sarvam-agents-client 0.3.2 → 0.4.1
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 +36 -23
- 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/client.d.ts +16 -0
- package/dist/client.js +21 -0
- package/dist/compose/deploy.d.ts +37 -7
- package/dist/compose/deploy.js +105 -19
- package/dist/compose/errors.d.ts +1 -1
- package/dist/compose/errors.js +1 -1
- package/dist/compose/index.d.ts +1 -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 +14 -0
- package/dist/compose/settingsTransport.js +151 -14
- package/dist/compose/transport.d.ts +14 -0
- package/dist/compose/transport.js +3 -0
- package/dist/compose/webhook.d.ts +18 -3
- package/dist/compose/webhook.js +59 -10
- package/dist/compose/webhookSecrets.d.ts +64 -0
- package/dist/compose/webhookSecrets.js +255 -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/client.d.ts +16 -0
- package/dist/esm/client.js +17 -0
- package/dist/esm/compose/deploy.d.ts +37 -7
- package/dist/esm/compose/deploy.js +105 -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 +1 -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 +14 -0
- package/dist/esm/compose/settingsTransport.js +151 -14
- package/dist/esm/compose/transport.d.ts +14 -0
- package/dist/esm/compose/transport.js +2 -1
- package/dist/esm/compose/webhook.d.ts +18 -3
- package/dist/esm/compose/webhook.js +55 -9
- package/dist/esm/compose/webhookSecrets.d.ts +64 -0
- package/dist/esm/compose/webhookSecrets.js +250 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -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/index.d.ts +3 -0
- package/dist/index.js +6 -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/package.json +1 -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/client.ts +21 -0
- package/src/compose/deploy.ts +126 -20
- package/src/compose/errors.ts +1 -1
- package/src/compose/index.ts +1 -1
- package/src/compose/run.ts +6 -2
- package/src/compose/settingsTransport.ts +154 -16
- package/src/compose/transport.ts +15 -0
- package/src/compose/webhook.ts +64 -11
- package/src/compose/webhookSecrets.ts +257 -0
- package/src/index.ts +4 -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
|
@@ -12,6 +12,9 @@ const runtime_js_1 = require("../runtime.js");
|
|
|
12
12
|
const streaming_js_1 = require("../streaming.js");
|
|
13
13
|
const errors_js_1 = require("./errors.js");
|
|
14
14
|
const events_js_1 = require("./events.js");
|
|
15
|
+
const transport_js_1 = require("./transport.js");
|
|
16
|
+
const webhook_js_1 = require("./webhook.js");
|
|
17
|
+
const webhookSecrets_js_1 = require("./webhookSecrets.js");
|
|
15
18
|
const PAGE = 100;
|
|
16
19
|
/** A post the server is still delivering (409 in_progress) is retried this many times, Retry-After apart. */
|
|
17
20
|
const IN_PROGRESS_RETRIES = 5;
|
|
@@ -102,6 +105,24 @@ function firstDetail(error) {
|
|
|
102
105
|
const first = Array.isArray(details) ? details[0] : undefined;
|
|
103
106
|
return first !== null && typeof first === "object" && !Array.isArray(first) ? { ...first } : {};
|
|
104
107
|
}
|
|
108
|
+
/** A deploy's 409 MISSING_DEPENDENCIES (the version uses a skill, app, knowledge base or sub-agent that was deleted
|
|
109
|
+
* since) as a UserError naming each one, else null. */
|
|
110
|
+
async function missingDependencies(err, agentId) {
|
|
111
|
+
if ((await reason(err, 409)) !== "MISSING_DEPENDENCIES")
|
|
112
|
+
return null;
|
|
113
|
+
const missing = firstDetail(await chatError(err))["missing"];
|
|
114
|
+
const items = Array.isArray(missing)
|
|
115
|
+
? missing.filter((m) => m !== null && typeof m === "object")
|
|
116
|
+
: [];
|
|
117
|
+
const named = items.map((m) => `${m["kind"]} '${m["name"]}' (${m["id"]})`).join(", ");
|
|
118
|
+
return new errors_js_1.UserError(`agent ${agentId}: the new version can't go live because it uses something that was deleted` +
|
|
119
|
+
`${named ? ": " + named : ""}. Remove it from the agent, then deploy again`);
|
|
120
|
+
}
|
|
121
|
+
/** `AgentResource.versionState`; undefined from a server without versions (or an agent that is not managed). */
|
|
122
|
+
function versionStateOf(resource) {
|
|
123
|
+
var _a;
|
|
124
|
+
return (_a = resource.versionState) !== null && _a !== void 0 ? _a : undefined;
|
|
125
|
+
}
|
|
105
126
|
function retryAfterMs(err) {
|
|
106
127
|
var _a;
|
|
107
128
|
const seconds = Number((_a = err.response.headers.get("retry-after")) !== null && _a !== void 0 ? _a : 2);
|
|
@@ -178,6 +199,30 @@ function withoutWebhooks(settings) {
|
|
|
178
199
|
const { sdk_hash: _hash, ...metadata } = settings["metadata"];
|
|
179
200
|
return { ...out, metadata };
|
|
180
201
|
}
|
|
202
|
+
function hooksOf(settings) {
|
|
203
|
+
const hooks = settings["webhooks"];
|
|
204
|
+
return Array.isArray(hooks) ? hooks.filter((h) => typeof h === "object" && h !== null && !Array.isArray(h)) : [];
|
|
205
|
+
}
|
|
206
|
+
function sdkNamed(agentId, hook) {
|
|
207
|
+
const url = hook["url"];
|
|
208
|
+
return typeof url === "string" && (0, webhook_js_1.urlProblem)(url) === null &&
|
|
209
|
+
hook["secret_credential"] === (0, webhook_js_1.secretCredentialName)(agentId, url);
|
|
210
|
+
}
|
|
211
|
+
/** `settings` as a definition writes them: a webhook whose `secret_credential` is the one `deploy` filled in loses it
|
|
212
|
+
* again, so the stored `sdk_hash` (computed without it) still matches. */
|
|
213
|
+
function asDefined(agentId, settings) {
|
|
214
|
+
if (!hooksOf(settings).some((h) => sdkNamed(agentId, h)))
|
|
215
|
+
return settings;
|
|
216
|
+
return {
|
|
217
|
+
...settings,
|
|
218
|
+
webhooks: settings["webhooks"].map((h) => {
|
|
219
|
+
if (typeof h !== "object" || h === null || !sdkNamed(agentId, h))
|
|
220
|
+
return h;
|
|
221
|
+
const { secret_credential: _name, ...rest } = h;
|
|
222
|
+
return rest;
|
|
223
|
+
}),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
181
226
|
function isNotFound(err) {
|
|
182
227
|
return err instanceof runtime_js_1.ResponseError && err.response.status === 404;
|
|
183
228
|
}
|
|
@@ -215,6 +260,7 @@ class SettingsTransport {
|
|
|
215
260
|
this.conversations = new ConversationsApi_js_1.ConversationsApi(config);
|
|
216
261
|
this.responses = new ResponsesApi_js_1.ResponsesApi(config);
|
|
217
262
|
this.customApis = new CustomAPIsApi_js_1.CustomAPIsApi(config);
|
|
263
|
+
this.secrets = (0, webhookSecrets_js_1.secretBackend)({ customApis: this.customApis });
|
|
218
264
|
}
|
|
219
265
|
// -- agents ---------------------------------------------------------------------------------------------
|
|
220
266
|
async findAgents(sdkName) {
|
|
@@ -244,7 +290,8 @@ class SettingsTransport {
|
|
|
244
290
|
}
|
|
245
291
|
async createAgent(agent) {
|
|
246
292
|
var _a, _b, _c;
|
|
247
|
-
|
|
293
|
+
let settings = agent.toSettings();
|
|
294
|
+
let missing = [];
|
|
248
295
|
const webhooks = ((_a = settings["webhooks"]) !== null && _a !== void 0 ? _a : []);
|
|
249
296
|
// The server checks webhooks against the credentials bound to the agent, and a new agent has none: create it
|
|
250
297
|
// without them, bind the Custom APIs they name, then write the settings in full.
|
|
@@ -264,6 +311,7 @@ class SettingsTransport {
|
|
|
264
311
|
}
|
|
265
312
|
let envelope;
|
|
266
313
|
let warnings;
|
|
314
|
+
let live;
|
|
267
315
|
// The create response never echoes settings, so support is read back from the settings route, which 404s
|
|
268
316
|
// when the server has settings switched off (create then silently drops them).
|
|
269
317
|
try {
|
|
@@ -278,6 +326,7 @@ class SettingsTransport {
|
|
|
278
326
|
if (webhooks.length > 0) {
|
|
279
327
|
for (const { path, name, customApiId } of bind)
|
|
280
328
|
await this.bind(resource.id, path, name, customApiId);
|
|
329
|
+
({ settings, missing } = await this.withSecretCredentials(resource.id, settings));
|
|
281
330
|
try {
|
|
282
331
|
envelope = await this.agents.putAgentSettings({ agentId: resource.id, body: settings });
|
|
283
332
|
}
|
|
@@ -293,7 +342,7 @@ class SettingsTransport {
|
|
|
293
342
|
for (const sentence of unbound)
|
|
294
343
|
console.warn(`agent ${resource.id}: ${sentence}`);
|
|
295
344
|
checkMetadata(envelope, agent);
|
|
296
|
-
await this.
|
|
345
|
+
live = await this.goLive(resource.id, versionStateOf(resource), agent);
|
|
297
346
|
}
|
|
298
347
|
catch (err) {
|
|
299
348
|
// Don't leave an orphan agent behind; a failing cleanup must not mask the original error.
|
|
@@ -305,13 +354,36 @@ class SettingsTransport {
|
|
|
305
354
|
}
|
|
306
355
|
throw err;
|
|
307
356
|
}
|
|
308
|
-
return {
|
|
357
|
+
return withVersion({
|
|
309
358
|
agentId: resource.id,
|
|
310
359
|
name: agent.name,
|
|
311
360
|
instructions: agent.instructions,
|
|
312
|
-
settings: { ...((_c = envelope.settings) !== null && _c !== void 0 ? _c : {}) },
|
|
361
|
+
settings: asDefined(resource.id, { ...((_c = envelope.settings) !== null && _c !== void 0 ? _c : {}) }),
|
|
313
362
|
warnings,
|
|
314
|
-
|
|
363
|
+
...(missing.length > 0 ? { webhooksMissingSecret: missing } : {}),
|
|
364
|
+
}, live, versionStateOf(resource));
|
|
365
|
+
}
|
|
366
|
+
/** `settings` with each webhook that names no `secret_credential` given the one `client.webhooks` keeps for the
|
|
367
|
+
* agent and host (stored, with a random secret nobody holds, when there is none yet), and the URLs of those
|
|
368
|
+
* webhooks still without a secret set by `client.webhooks`. */
|
|
369
|
+
async withSecretCredentials(agentId, settings) {
|
|
370
|
+
const hooks = hooksOf(settings);
|
|
371
|
+
if (hooks.every((h) => "secret_credential" in h))
|
|
372
|
+
return { settings, missing: [] };
|
|
373
|
+
const filled = [];
|
|
374
|
+
const missing = [];
|
|
375
|
+
for (const h of settings["webhooks"]) {
|
|
376
|
+
if (typeof h !== "object" || h === null || "secret_credential" in h) {
|
|
377
|
+
filled.push(h);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
const url = h["url"];
|
|
381
|
+
const { name, status } = await this.secrets.ensure(agentId, url);
|
|
382
|
+
filled.push({ ...h, secret_credential: name });
|
|
383
|
+
if (status === "missing")
|
|
384
|
+
missing.push(url);
|
|
385
|
+
}
|
|
386
|
+
return { settings: { ...settings, webhooks: filled }, missing };
|
|
315
387
|
}
|
|
316
388
|
/** The caller's Custom APIs the webhooks name, to bind to a new agent: one entry per name that matches exactly
|
|
317
389
|
* one, and a sentence per name that matches none or several (left to the server, which refuses it only when it
|
|
@@ -322,8 +394,8 @@ class SettingsTransport {
|
|
|
322
394
|
const seen = new Set();
|
|
323
395
|
for (const [i, hook] of webhooks.entries()) {
|
|
324
396
|
const name = hook.secret_credential;
|
|
325
|
-
if (seen.has(name))
|
|
326
|
-
continue;
|
|
397
|
+
if (name === undefined || seen.has(name))
|
|
398
|
+
continue; // left out: deploy fills in the one client.webhooks keeps
|
|
327
399
|
seen.add(name);
|
|
328
400
|
const path = `webhooks.${i}.secret_credential`;
|
|
329
401
|
const ids = await this.customApiIds(name);
|
|
@@ -372,9 +444,10 @@ class SettingsTransport {
|
|
|
372
444
|
}
|
|
373
445
|
async updateAgent(agentId, agent) {
|
|
374
446
|
var _a, _b;
|
|
375
|
-
|
|
447
|
+
let settings = agent.toSettings();
|
|
376
448
|
// findAgents searches by display name, so an adopted (or dashboard-renamed) agent must carry sdk_name.
|
|
377
|
-
|
|
449
|
+
const current = await this.agents.getAgent({ agentId });
|
|
450
|
+
if (current.name !== agent.name) {
|
|
378
451
|
await this.agents.updateAgent({ agentId, agentUpdate: { name: agent.name } });
|
|
379
452
|
}
|
|
380
453
|
// Rejections are only visible after the server stored the settings, so keep what was there to put it back.
|
|
@@ -394,6 +467,15 @@ class SettingsTransport {
|
|
|
394
467
|
if (instructionsChanged) {
|
|
395
468
|
await this.agents.putInstructions({ agentId, instructionsBody: { data: agent.instructions } });
|
|
396
469
|
}
|
|
470
|
+
let missing;
|
|
471
|
+
try {
|
|
472
|
+
({ settings, missing } = await this.withSecretCredentials(agentId, settings));
|
|
473
|
+
}
|
|
474
|
+
catch (err) {
|
|
475
|
+
if (instructionsChanged)
|
|
476
|
+
await this.restore(agentId, null, previousInstructions);
|
|
477
|
+
throw err;
|
|
478
|
+
}
|
|
397
479
|
let envelope;
|
|
398
480
|
try {
|
|
399
481
|
envelope = await this.agents.putAgentSettings({ agentId, body: settings });
|
|
@@ -420,14 +502,52 @@ class SettingsTransport {
|
|
|
420
502
|
throw err;
|
|
421
503
|
}
|
|
422
504
|
checkMetadata(envelope, agent);
|
|
423
|
-
await this.
|
|
424
|
-
return {
|
|
505
|
+
const live = await this.goLive(agentId, versionStateOf(current), agent);
|
|
506
|
+
return withVersion({
|
|
425
507
|
agentId,
|
|
426
508
|
name: agent.name,
|
|
427
509
|
instructions: agent.instructions,
|
|
428
|
-
settings: { ...((_b = envelope.settings) !== null && _b !== void 0 ? _b : {}) },
|
|
510
|
+
settings: asDefined(agentId, { ...((_b = envelope.settings) !== null && _b !== void 0 ? _b : {}) }),
|
|
429
511
|
warnings,
|
|
430
|
-
|
|
512
|
+
...(missing.length > 0 ? { webhooksMissingSecret: missing } : {}),
|
|
513
|
+
}, live, versionStateOf(current));
|
|
514
|
+
}
|
|
515
|
+
/** Make what was just written what end users get. A server without versions (no `versionState`) gets the bodiless
|
|
516
|
+
* deploy 0.3 sent; otherwise the draft is saved as a version and that version deployed. */
|
|
517
|
+
async goLive(agentId, versionState, agent) {
|
|
518
|
+
if (versionState === undefined) {
|
|
519
|
+
await this.agents.deployAgent({ agentId }); // idempotent; moves an adopted draft to deployed
|
|
520
|
+
return undefined;
|
|
521
|
+
}
|
|
522
|
+
return this.publish(agentId, transport_js_1.VERSION_NOTE);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Save the draft as the next version and make it live. 409 NO_CHANGES means the newest saved version already is the
|
|
526
|
+
* draft, so that version is deployed, by its number (never "the newest", which a save landing in between could
|
|
527
|
+
* change).
|
|
528
|
+
*/
|
|
529
|
+
async publish(agentId, note) {
|
|
530
|
+
var _a, _b;
|
|
531
|
+
let version;
|
|
532
|
+
try {
|
|
533
|
+
version = (await this.agents.saveVersion({ agentId, agentVersionCreate: { note } })).version;
|
|
534
|
+
}
|
|
535
|
+
catch (err) {
|
|
536
|
+
if ((await reason(err, 409)) !== "NO_CHANGES")
|
|
537
|
+
throw err;
|
|
538
|
+
const newest = (await this.agents.getDraft({ agentId })).basedOnVersion;
|
|
539
|
+
if (newest === undefined || newest === null)
|
|
540
|
+
throw err;
|
|
541
|
+
version = newest;
|
|
542
|
+
}
|
|
543
|
+
let resource;
|
|
544
|
+
try {
|
|
545
|
+
resource = await this.agents.deployAgent({ agentId, agentDeploy: { version } });
|
|
546
|
+
}
|
|
547
|
+
catch (err) {
|
|
548
|
+
throw (_a = (await missingDependencies(err, agentId))) !== null && _a !== void 0 ? _a : err;
|
|
549
|
+
}
|
|
550
|
+
return (_b = resource.liveVersion) !== null && _b !== void 0 ? _b : undefined;
|
|
431
551
|
}
|
|
432
552
|
async deleteAgent(agentId) {
|
|
433
553
|
await this.agents.deleteAgent({ agentId });
|
|
@@ -605,10 +725,27 @@ class SettingsTransport {
|
|
|
605
725
|
}
|
|
606
726
|
remote(resource, instructions) {
|
|
607
727
|
var _a;
|
|
608
|
-
|
|
728
|
+
const remote = {
|
|
729
|
+
agentId: resource.id, name: resource.name, instructions, warnings: [],
|
|
730
|
+
settings: asDefined(resource.id, { ...((_a = resource.settings) !== null && _a !== void 0 ? _a : {}) }),
|
|
731
|
+
};
|
|
732
|
+
// Left out, not set to undefined, when the server has no versions: the shape a 0.3 transport returned.
|
|
733
|
+
if (resource.liveVersion !== undefined && resource.liveVersion !== null)
|
|
734
|
+
remote.liveVersion = resource.liveVersion;
|
|
735
|
+
const state = versionStateOf(resource);
|
|
736
|
+
if (state !== undefined)
|
|
737
|
+
remote.versionState = state;
|
|
738
|
+
return remote;
|
|
609
739
|
}
|
|
610
740
|
}
|
|
611
741
|
exports.SettingsTransport = SettingsTransport;
|
|
742
|
+
/** `remote` with the version fields set when known (left out otherwise, as a server without versions returns it). After a
|
|
743
|
+
* publish the agent is live whatever state it was read in. */
|
|
744
|
+
function withVersion(remote, live, state) {
|
|
745
|
+
if (live !== undefined)
|
|
746
|
+
return { ...remote, liveVersion: live, versionState: "live" };
|
|
747
|
+
return state === undefined ? remote : { ...remote, versionState: state };
|
|
748
|
+
}
|
|
612
749
|
/** The server's 422 CHAT_VALIDATION / invalid_agent_settings rejection as a UserError, else null. */
|
|
613
750
|
async function invalidSettings(err) {
|
|
614
751
|
if (!(err instanceof runtime_js_1.ResponseError) || err.response.status !== 422)
|
|
@@ -14,7 +14,18 @@ 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;
|
|
23
|
+
/** URLs of the webhooks this write left without a secret set by `client.webhooks` (a placeholder secret nobody
|
|
24
|
+
* holds stands in until `client.webhooks.setSecret`); left out when there are none. */
|
|
25
|
+
webhooksMissingSecret?: string[];
|
|
17
26
|
}
|
|
27
|
+
/** The note on every version deploy() saves, so the dashboard's version list shows which came from code. */
|
|
28
|
+
export declare const VERSION_NOTE = "Deployed from the SDK";
|
|
18
29
|
/** Async iterable of Events; the ids are filled in as the stream progresses. */
|
|
19
30
|
export interface TurnStream extends AsyncIterable<Event> {
|
|
20
31
|
readonly conversationId: string | null;
|
|
@@ -37,6 +48,9 @@ export interface Transport {
|
|
|
37
48
|
createAgent(agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
|
|
38
49
|
updateAgent(agentId: string, agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
|
|
39
50
|
deleteAgent(agentId: string): Promise<void>;
|
|
51
|
+
/** Save the draft as the next version and make that version live; resolves to the live version. Called only for
|
|
52
|
+
* an agent whose `versionState` is set. Optional so a transport written before versions still type-checks. */
|
|
53
|
+
publish?(agentId: string, note: string): Promise<number | undefined>;
|
|
40
54
|
startTurn(agentId: string, conversationId: string | null, text: string, signal?: AbortSignal): Promise<TurnStream>;
|
|
41
55
|
answerClarify(conversationId: string, requestId: string, answers: Record<string, unknown>, signal?: AbortSignal): Promise<TurnStream>;
|
|
42
56
|
resume(conversationId: string, responseId: string, lastEventId: string | null, signal?: AbortSignal): Promise<TurnStream>;
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VERSION_NOTE = void 0;
|
|
4
|
+
/** The note on every version deploy() saves, so the dashboard's version list shows which came from code. */
|
|
5
|
+
exports.VERSION_NOTE = "Deployed from the SDK";
|
|
@@ -5,8 +5,13 @@ export interface Webhook {
|
|
|
5
5
|
url: string;
|
|
6
6
|
/** 1 to 4 distinct events. `approval.requested` is reserved: accepted, not sent yet. */
|
|
7
7
|
events: WebhookEventType[];
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The vault credential holding the `whsec_` signing secret (agent.json `secret_credential`). Leave it out: `deploy`
|
|
10
|
+
* fills in the credential `client.webhooks` keeps, and `client.webhooks.setSecret(agentId, url)` sets the secret.
|
|
11
|
+
*
|
|
12
|
+
* @deprecated Use `client.webhooks.setSecret` instead of creating a Custom API and naming it here.
|
|
13
|
+
*/
|
|
14
|
+
secretCredential?: string;
|
|
10
15
|
}
|
|
11
16
|
export declare const WEBHOOKS_MAX = 5;
|
|
12
17
|
/** Why `url` is not an allowed webhook URL, or null. Every URL the server refuses is refused, with the server's
|
|
@@ -14,6 +19,16 @@ export declare const WEBHOOKS_MAX = 5;
|
|
|
14
19
|
export declare function urlProblem(url: string): string | null;
|
|
15
20
|
/** The server's pure `webhooks` rules (types first); throws `UserError` naming the agent.json path. */
|
|
16
21
|
export declare function checkWebhooks(webhooks: unknown): void;
|
|
17
|
-
/** Only the three fields
|
|
22
|
+
/** Only the three fields (a copy). `secretCredential` is left out when absent, or when it is the name `deploy`
|
|
23
|
+
* writes anyway (one spelling keeps one fingerprint). */
|
|
18
24
|
export declare function copyWebhooks(webhooks: readonly Webhook[]): Webhook[];
|
|
19
25
|
export declare function webhooksJson(webhooks: readonly Webhook[]): Record<string, unknown>[];
|
|
26
|
+
/** The host of an allowed webhook URL, lowercased, without a trailing dot or a port. */
|
|
27
|
+
export declare function webhookHost(url: string): string;
|
|
28
|
+
/** Every Custom API the SDK creates for a webhook secret is named `webhook-secret:<agentId>:<host>`. */
|
|
29
|
+
export declare const SECRET_NAME_PREFIX = "webhook-secret:";
|
|
30
|
+
/** The credential name the SDK uses for `url`'s signing secret on agent `agentId` (what `deploy` writes as
|
|
31
|
+
* `secret_credential` when a webhook leaves it out). A host too long to fit 256 characters is hashed. */
|
|
32
|
+
export declare function secretCredentialName(agentId: string, url: string): string;
|
|
33
|
+
/** Whether `name` is the SDK's credential name for `url`'s host, for any agent. */
|
|
34
|
+
export declare function isSdkCredentialName(name: unknown, url: string): boolean;
|
package/dist/compose/webhook.js
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WEBHOOKS_MAX = void 0;
|
|
3
|
+
exports.SECRET_NAME_PREFIX = exports.WEBHOOKS_MAX = void 0;
|
|
4
4
|
exports.urlProblem = urlProblem;
|
|
5
5
|
exports.checkWebhooks = checkWebhooks;
|
|
6
6
|
exports.copyWebhooks = copyWebhooks;
|
|
7
7
|
exports.webhooksJson = webhooksJson;
|
|
8
|
+
exports.webhookHost = webhookHost;
|
|
9
|
+
exports.secretCredentialName = secretCredentialName;
|
|
10
|
+
exports.isSdkCredentialName = isSdkCredentialName;
|
|
8
11
|
/**
|
|
9
12
|
* `webhooks` entries of `agent.json`, checked locally with the server's cheap rules (as the Python SDK does).
|
|
10
13
|
*
|
|
11
14
|
* The server (`claw_common/agent_settings/webhooks.py`) also checks that the host resolves to a public address and
|
|
12
15
|
* that `secret_credential` names a vault credential bound to the agent whose allowed hosts cover the URL; those need
|
|
13
|
-
* I/O and stay on the server, which refuses the save with a 422 that `deploy` throws as `UserError`.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
+
* I/O and stay on the server, which refuses the save with a 422 that `deploy` throws as `UserError`.
|
|
17
|
+
*
|
|
18
|
+
* `secretCredential` is optional and deprecated: left out, `deploy` writes the name `client.webhooks` uses for the
|
|
19
|
+
* agent and host (`secretCredentialName`). Given, the deploy that creates the agent binds the caller's Custom API of
|
|
20
|
+
* that name first (`SettingsTransport.createAgent`).
|
|
16
21
|
*/
|
|
22
|
+
const node_crypto_1 = require("node:crypto");
|
|
17
23
|
const errors_js_1 = require("./errors.js");
|
|
18
24
|
const webhooks_js_1 = require("../webhooks.js");
|
|
19
25
|
exports.WEBHOOKS_MAX = 5;
|
|
@@ -138,7 +144,7 @@ function checkWebhooks(webhooks) {
|
|
|
138
144
|
webhooks.forEach((hook, i) => {
|
|
139
145
|
const where = `webhooks.${i}.`;
|
|
140
146
|
if (typeof hook !== "object" || hook === null || Array.isArray(hook)) {
|
|
141
|
-
throw new errors_js_1.UserError(`webhooks.${i}: must be an object with url
|
|
147
|
+
throw new errors_js_1.UserError(`webhooks.${i}: must be an object with url and events`);
|
|
142
148
|
}
|
|
143
149
|
const { url, events, secretCredential } = hook;
|
|
144
150
|
if (typeof url !== "string")
|
|
@@ -146,8 +152,9 @@ function checkWebhooks(webhooks) {
|
|
|
146
152
|
if (!Array.isArray(events) || !events.every((e) => typeof e === "string")) {
|
|
147
153
|
throw new errors_js_1.UserError(where + "events: must be a list of event names");
|
|
148
154
|
}
|
|
149
|
-
if (typeof secretCredential !== "string")
|
|
155
|
+
if (secretCredential !== undefined && typeof secretCredential !== "string") {
|
|
150
156
|
throw new errors_js_1.UserError(where + "secretCredential: must be a string");
|
|
157
|
+
}
|
|
151
158
|
// Python's Webhook is extra="forbid": a key dropped silently (a misplaced secret) would never verify.
|
|
152
159
|
const extra = Object.keys(hook).find((k) => k !== "url" && k !== "events" && k !== "secretCredential");
|
|
153
160
|
if (extra !== undefined)
|
|
@@ -163,7 +170,7 @@ function checkWebhooks(webhooks) {
|
|
|
163
170
|
}
|
|
164
171
|
if (new Set(events).size !== events.length)
|
|
165
172
|
throw new errors_js_1.UserError(where + "events: events must not repeat an event.");
|
|
166
|
-
const n = codePoints(secretCredential);
|
|
173
|
+
const n = typeof secretCredential === "string" ? codePoints(secretCredential) : 1;
|
|
167
174
|
if (n < 1 || n > SECRET_CREDENTIAL_MAX) {
|
|
168
175
|
throw new errors_js_1.UserError(where + "secret_credential: secret_credential must be the name of a vault credential " +
|
|
169
176
|
`(1 to ${SECRET_CREDENTIAL_MAX} characters).`);
|
|
@@ -175,10 +182,52 @@ function checkWebhooks(webhooks) {
|
|
|
175
182
|
seen.add(key);
|
|
176
183
|
});
|
|
177
184
|
}
|
|
178
|
-
/** Only the three fields
|
|
185
|
+
/** Only the three fields (a copy). `secretCredential` is left out when absent, or when it is the name `deploy`
|
|
186
|
+
* writes anyway (one spelling keeps one fingerprint). */
|
|
179
187
|
function copyWebhooks(webhooks) {
|
|
180
|
-
return webhooks.map((h) =>
|
|
188
|
+
return webhooks.map((h) => {
|
|
189
|
+
const copy = { url: h.url, events: [...h.events] };
|
|
190
|
+
const name = h.secretCredential;
|
|
191
|
+
if (name !== undefined && !isSdkCredentialName(name, h.url))
|
|
192
|
+
copy.secretCredential = name;
|
|
193
|
+
return copy;
|
|
194
|
+
});
|
|
181
195
|
}
|
|
182
196
|
function webhooksJson(webhooks) {
|
|
183
|
-
return webhooks.map((h) =>
|
|
197
|
+
return webhooks.map((h) => {
|
|
198
|
+
const out = { url: h.url, events: [...h.events] };
|
|
199
|
+
if (h.secretCredential !== undefined)
|
|
200
|
+
out["secret_credential"] = h.secretCredential;
|
|
201
|
+
return out;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/** The host of an allowed webhook URL, lowercased, without a trailing dot or a port. */
|
|
205
|
+
function webhookHost(url) {
|
|
206
|
+
return stripTrailingDots(hostPort(split(url)[1])[0]);
|
|
207
|
+
}
|
|
208
|
+
/** Every Custom API the SDK creates for a webhook secret is named `webhook-secret:<agentId>:<host>`. */
|
|
209
|
+
exports.SECRET_NAME_PREFIX = "webhook-secret:";
|
|
210
|
+
const SECRET_NAME_MAX = 256;
|
|
211
|
+
function hashedHost(host) {
|
|
212
|
+
return "sha256-" + (0, node_crypto_1.createHash)("sha256").update(host, "utf8").digest("hex").slice(0, 32);
|
|
213
|
+
}
|
|
214
|
+
/** The credential name the SDK uses for `url`'s signing secret on agent `agentId` (what `deploy` writes as
|
|
215
|
+
* `secret_credential` when a webhook leaves it out). A host too long to fit 256 characters is hashed. */
|
|
216
|
+
function secretCredentialName(agentId, url) {
|
|
217
|
+
const host = webhookHost(url);
|
|
218
|
+
const name = `${exports.SECRET_NAME_PREFIX}${agentId}:${host}`;
|
|
219
|
+
return codePoints(name) <= SECRET_NAME_MAX ? name : `${exports.SECRET_NAME_PREFIX}${agentId}:${hashedHost(host)}`;
|
|
220
|
+
}
|
|
221
|
+
/** Whether `name` is the SDK's credential name for `url`'s host, for any agent. */
|
|
222
|
+
function isSdkCredentialName(name, url) {
|
|
223
|
+
if (typeof name !== "string" || !name.startsWith(exports.SECRET_NAME_PREFIX) || urlProblem(url) !== null)
|
|
224
|
+
return false;
|
|
225
|
+
const rest = name.slice(exports.SECRET_NAME_PREFIX.length);
|
|
226
|
+
const at = rest.lastIndexOf(":");
|
|
227
|
+
if (at <= 0)
|
|
228
|
+
return false;
|
|
229
|
+
const agentId = rest.slice(0, at);
|
|
230
|
+
const tail = rest.slice(at + 1);
|
|
231
|
+
const host = webhookHost(url);
|
|
232
|
+
return !agentId.includes(":") && (tail === host || tail === hashedHost(host));
|
|
184
233
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `client.webhooks`: set, rotate, check and delete an agent webhook's signing secret (as Python's
|
|
3
|
+
* `sarvam_agents.compose.webhook_secrets`).
|
|
4
|
+
*
|
|
5
|
+
* The public surface is `WebhookSecrets`. Where the secret lives is behind `SecretBackend`, one small interface with
|
|
6
|
+
* one implementation today, `CustomApiBackend`: the secret is a bearer Custom API the SDK creates, owns and binds to
|
|
7
|
+
* the agent, always with `enabledForChats: false` (`CustomApiCreate` defaults it to true, which would let the agent's
|
|
8
|
+
* sandbox be handed the secret on requests to the webhook's host). When the server ships dedicated secret endpoints
|
|
9
|
+
* (SARC-2597: PUT/DELETE secret, rotate-secret, secretStatus), a second backend replaces this one in `secretBackend`
|
|
10
|
+
* and nothing else changes.
|
|
11
|
+
*
|
|
12
|
+
* No secret is logged, kept on an object or put in an error message.
|
|
13
|
+
*/
|
|
14
|
+
import { CustomAPIsApi } from "../apis/CustomAPIsApi.js";
|
|
15
|
+
import type { Configuration } from "../runtime.js";
|
|
16
|
+
import type { Webhook } from "./webhook.js";
|
|
17
|
+
export type SecretStatus = "set" | "missing";
|
|
18
|
+
/** Every Custom API the SDK creates for a webhook secret carries this description (and its name). */
|
|
19
|
+
export declare const SECRET_DESCRIPTION = "Webhook signing secret managed by the Sarvam Agents SDK (client.webhooks). Do not edit.";
|
|
20
|
+
/** The description of the random secret `deploy` stores when none is set yet: `secretStatus` reads it as
|
|
21
|
+
* `"missing"`, and `setSecret`/`rotateSecret` overwrite it with `SECRET_DESCRIPTION`. */
|
|
22
|
+
export declare const PLACEHOLDER_DESCRIPTION = "Webhook signing secret managed by the Sarvam Agents SDK (client.webhooks). Placeholder: call client.webhooks.set_secret";
|
|
23
|
+
/** Where webhook secrets live. Swap the implementation in `secretBackend`, nowhere else. */
|
|
24
|
+
export interface SecretBackend {
|
|
25
|
+
/** Store `secret` for `url` on the agent, replacing any current one. */
|
|
26
|
+
put(agentId: string, url: string, secret: string): Promise<void>;
|
|
27
|
+
/** `"set"` only for a secret stored by `put`; a placeholder from `ensure` is `"missing"`. */
|
|
28
|
+
status(agentId: string, url: string): Promise<SecretStatus>;
|
|
29
|
+
/** Remove the secret; nothing to remove is not an error. */
|
|
30
|
+
delete(agentId: string, url: string): Promise<void>;
|
|
31
|
+
/** For `deploy`: make sure the agent has a secret for `url` (a random placeholder one if none) and return the
|
|
32
|
+
* credential name for agent.json `secret_credential` and `status` (`"missing"` while it is a placeholder). */
|
|
33
|
+
ensure(agentId: string, url: string): Promise<{
|
|
34
|
+
name: string;
|
|
35
|
+
status: SecretStatus;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
/** The one place that picks where secrets live. */
|
|
39
|
+
export declare function secretBackend(api: {
|
|
40
|
+
config?: Configuration;
|
|
41
|
+
customApis?: CustomAPIsApi;
|
|
42
|
+
}): SecretBackend;
|
|
43
|
+
/**
|
|
44
|
+
* `client.webhooks`: the signing secret of each agent.json webhook.
|
|
45
|
+
*
|
|
46
|
+
* `webhook` is the webhook's URL (or a webhook object); the secret covers the URL's host. Secrets are `whsec_` plus
|
|
47
|
+
* base64 of 24 to 64 bytes.
|
|
48
|
+
*/
|
|
49
|
+
export declare class WebhookSecrets {
|
|
50
|
+
#private;
|
|
51
|
+
constructor(config: Configuration);
|
|
52
|
+
/** Store `secret` (a new generated one when left out) as the webhook's signing secret and return it. A malformed
|
|
53
|
+
* secret throws `SecretFormatError` before anything is sent. */
|
|
54
|
+
setSecret(agentId: string, webhook: string | Webhook, secret?: string): Promise<string>;
|
|
55
|
+
/** Replace the webhook's signing secret with a new generated one and return it. Have your receiver accept the old
|
|
56
|
+
* and the new secret for a few minutes: a delivery still retrying is signed with the new one. */
|
|
57
|
+
rotateSecret(agentId: string, webhook: string | Webhook): Promise<string>;
|
|
58
|
+
/** `"set"` when the agent has a signing secret stored by `setSecret` or `rotateSecret` for the webhook; `"missing"`
|
|
59
|
+
* otherwise, including while it holds the placeholder a deploy stored. */
|
|
60
|
+
secretStatus(agentId: string, webhook: string | Webhook): Promise<SecretStatus>;
|
|
61
|
+
/** Remove the webhook's signing secret. Only what the SDK stored is removed; a Custom API you made yourself is
|
|
62
|
+
* never touched. Deliveries to the webhook fail until a new secret is set or the webhook is removed. */
|
|
63
|
+
deleteSecret(agentId: string, webhook: string | Webhook): Promise<void>;
|
|
64
|
+
}
|