sarvam-agents-client 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -29
- package/dist/apis/AgentsApi.d.ts +96 -11
- package/dist/apis/AgentsApi.js +229 -9
- package/dist/apis/ConversationsApi.d.ts +5 -2
- package/dist/apis/ConversationsApi.js +7 -2
- package/dist/client.d.ts +3 -2
- package/dist/client.js +2 -1
- 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/compose/deploy.d.ts +14 -3
- package/dist/compose/deploy.js +60 -7
- package/dist/compose/errors.d.ts +4 -0
- package/dist/compose/errors.js +6 -1
- package/dist/compose/settingsTransport.d.ts +18 -13
- package/dist/compose/settingsTransport.js +78 -130
- package/dist/compose/transport.d.ts +12 -2
- package/dist/compose/webhook.d.ts +10 -13
- package/dist/compose/webhook.js +48 -45
- package/dist/compose/webhookSecrets.d.ts +79 -49
- package/dist/compose/webhookSecrets.js +244 -195
- package/dist/esm/apis/AgentsApi.d.ts +96 -11
- package/dist/esm/apis/AgentsApi.js +230 -10
- package/dist/esm/apis/ConversationsApi.d.ts +5 -2
- package/dist/esm/apis/ConversationsApi.js +7 -2
- package/dist/esm/client.d.ts +3 -2
- package/dist/esm/client.js +2 -1
- 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/dist/esm/compose/deploy.d.ts +14 -3
- package/dist/esm/compose/deploy.js +60 -7
- package/dist/esm/compose/errors.d.ts +4 -0
- package/dist/esm/compose/errors.js +4 -0
- package/dist/esm/compose/settingsTransport.d.ts +18 -13
- package/dist/esm/compose/settingsTransport.js +80 -132
- package/dist/esm/compose/transport.d.ts +12 -2
- package/dist/esm/compose/webhook.d.ts +10 -13
- package/dist/esm/compose/webhook.js +45 -42
- package/dist/esm/compose/webhookSecrets.d.ts +79 -49
- package/dist/esm/compose/webhookSecrets.js +241 -193
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/models/AgentConversationDetail.d.ts +12 -0
- package/dist/esm/models/AgentConversationDetail.js +4 -0
- package/dist/esm/models/AgentConversationView.d.ts +12 -0
- package/dist/esm/models/AgentConversationView.js +4 -0
- package/dist/esm/models/AgentResource.d.ts +7 -0
- package/dist/esm/models/AgentResource.js +3 -0
- package/dist/esm/models/AgentVersionSavedResource.d.ts +65 -0
- package/dist/esm/models/AgentVersionSavedResource.js +60 -0
- package/dist/esm/models/AgentWebhookList.d.ts +33 -0
- package/dist/esm/models/AgentWebhookList.js +44 -0
- package/dist/esm/models/AgentWebhookView.d.ts +76 -0
- package/dist/esm/models/AgentWebhookView.js +70 -0
- package/dist/esm/models/ConversationDetail.d.ts +6 -0
- package/dist/esm/models/ConversationDetail.js +2 -0
- package/dist/esm/models/ConversationView.d.ts +6 -0
- package/dist/esm/models/ConversationView.js +2 -0
- package/dist/esm/models/ErrorDetailItem.d.ts +3 -0
- package/dist/esm/models/FileNodeView.d.ts +7 -0
- package/dist/esm/models/FileNodeView.js +3 -0
- package/dist/esm/models/QueueMessageInput.d.ts +6 -0
- package/dist/esm/models/QueueMessageInput.js +2 -0
- package/dist/esm/models/RotatedWebhookSecretView.d.ts +58 -0
- package/dist/esm/models/RotatedWebhookSecretView.js +61 -0
- package/dist/esm/models/SaveWarningView.d.ts +44 -0
- package/dist/esm/models/SaveWarningView.js +51 -0
- package/dist/esm/models/ShareSkippedResource.d.ts +41 -0
- package/dist/esm/models/ShareSkippedResource.js +47 -0
- package/dist/esm/models/WebhookSecretPutBody.d.ts +32 -0
- package/dist/esm/models/WebhookSecretPutBody.js +43 -0
- package/dist/esm/models/WebhookSecretStateView.d.ts +52 -0
- package/dist/esm/models/WebhookSecretStateView.js +56 -0
- package/dist/esm/models/WebhookSecretValueView.d.ts +32 -0
- package/dist/esm/models/WebhookSecretValueView.js +44 -0
- package/dist/esm/models/WebhookWarning.d.ts +44 -0
- package/dist/esm/models/WebhookWarning.js +51 -0
- package/dist/esm/models/index.d.ts +10 -0
- package/dist/esm/models/index.js +10 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -1
- package/dist/models/AgentConversationDetail.d.ts +12 -0
- package/dist/models/AgentConversationDetail.js +4 -0
- package/dist/models/AgentConversationView.d.ts +12 -0
- package/dist/models/AgentConversationView.js +4 -0
- package/dist/models/AgentResource.d.ts +7 -0
- package/dist/models/AgentResource.js +3 -0
- package/dist/models/AgentVersionSavedResource.d.ts +65 -0
- package/dist/models/AgentVersionSavedResource.js +67 -0
- package/dist/models/AgentWebhookList.d.ts +33 -0
- package/dist/models/AgentWebhookList.js +51 -0
- package/dist/models/AgentWebhookView.d.ts +76 -0
- package/dist/models/AgentWebhookView.js +78 -0
- package/dist/models/ConversationDetail.d.ts +6 -0
- package/dist/models/ConversationDetail.js +2 -0
- package/dist/models/ConversationView.d.ts +6 -0
- package/dist/models/ConversationView.js +2 -0
- package/dist/models/ErrorDetailItem.d.ts +3 -0
- package/dist/models/FileNodeView.d.ts +7 -0
- package/dist/models/FileNodeView.js +3 -0
- package/dist/models/QueueMessageInput.d.ts +6 -0
- package/dist/models/QueueMessageInput.js +2 -0
- package/dist/models/RotatedWebhookSecretView.d.ts +58 -0
- package/dist/models/RotatedWebhookSecretView.js +69 -0
- package/dist/models/SaveWarningView.d.ts +44 -0
- package/dist/models/SaveWarningView.js +58 -0
- package/dist/models/ShareSkippedResource.d.ts +41 -0
- package/dist/models/ShareSkippedResource.js +54 -0
- package/dist/models/WebhookSecretPutBody.d.ts +32 -0
- package/dist/models/WebhookSecretPutBody.js +50 -0
- package/dist/models/WebhookSecretStateView.d.ts +52 -0
- package/dist/models/WebhookSecretStateView.js +64 -0
- package/dist/models/WebhookSecretValueView.d.ts +32 -0
- package/dist/models/WebhookSecretValueView.js +51 -0
- package/dist/models/WebhookWarning.d.ts +44 -0
- package/dist/models/WebhookWarning.js +58 -0
- package/dist/models/index.d.ts +10 -0
- package/dist/models/index.js +10 -0
- package/package.json +1 -1
- package/src/apis/AgentsApi.ts +366 -11
- package/src/apis/ConversationsApi.ts +9 -2
- package/src/client.ts +3 -2
- 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
- package/src/compose/deploy.ts +71 -10
- package/src/compose/errors.ts +4 -0
- package/src/compose/settingsTransport.ts +78 -131
- package/src/compose/transport.ts +10 -2
- package/src/compose/webhook.ts +49 -45
- package/src/compose/webhookSecrets.ts +227 -179
- package/src/index.ts +2 -1
- package/src/models/AgentConversationDetail.ts +16 -0
- package/src/models/AgentConversationView.ts +16 -0
- package/src/models/AgentResource.ts +15 -0
- package/src/models/AgentVersionSavedResource.ts +119 -0
- package/src/models/AgentWebhookList.ts +74 -0
- package/src/models/AgentWebhookView.ts +129 -0
- package/src/models/ConversationDetail.ts +8 -0
- package/src/models/ConversationView.ts +8 -0
- package/src/models/ErrorDetailItem.ts +3 -0
- package/src/models/FileNodeView.ts +16 -0
- package/src/models/QueueMessageInput.ts +8 -0
- package/src/models/RotatedWebhookSecretView.ts +103 -0
- package/src/models/SaveWarningView.ts +84 -0
- package/src/models/ShareSkippedResource.ts +78 -0
- package/src/models/WebhookSecretPutBody.ts +66 -0
- package/src/models/WebhookSecretStateView.ts +94 -0
- package/src/models/WebhookSecretValueView.ts +66 -0
- package/src/models/WebhookWarning.ts +84 -0
- package/src/models/index.ts +10 -0
|
@@ -1,52 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `client.webhooks`:
|
|
3
|
-
* `sarvam_agents.compose.webhook_secrets`).
|
|
2
|
+
* `client.webhooks`: list an agent's webhooks, and create, view, rotate, set and delete each one's signing secret (as
|
|
3
|
+
* Python's `sarvam_agents.compose.webhook_secrets`).
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* (SARC-2597: PUT/DELETE secret, rotate-secret, secretStatus), a second backend replaces this one in `secretBackend`
|
|
10
|
-
* and nothing else changes.
|
|
5
|
+
* Built on the owner-only routes of the server's webhook secret store (panja #1857, SARC-2597):
|
|
6
|
+
* `GET /v1/agents/{id}/webhooks` and `POST`/`GET`/`PUT`/`DELETE .../webhooks/{webhookKey}/secret` and
|
|
7
|
+
* `POST .../rotate-secret`. The `webhookKey` is always read from the list, by the webhook's exact URL; it is never
|
|
8
|
+
* computed here.
|
|
11
9
|
*
|
|
12
10
|
* No secret is logged, kept on an object or put in an error message.
|
|
13
11
|
*/
|
|
14
|
-
import {
|
|
12
|
+
import { AgentsApi } from "../apis/AgentsApi.js";
|
|
13
|
+
import type { AgentWebhookView } from "../models/AgentWebhookView.js";
|
|
15
14
|
import { ResponseError } from "../runtime.js";
|
|
16
15
|
import type { Configuration } from "../runtime.js";
|
|
17
|
-
import { UserError } from "./errors.js";
|
|
16
|
+
import { SecretExistsError, UserError } from "./errors.js";
|
|
18
17
|
import type { Webhook } from "./webhook.js";
|
|
19
18
|
|
|
20
19
|
// Loaded on first use, so the root entry point (which exports `Client`) does not import `node:crypto` up front.
|
|
21
20
|
const loadWebhook = () => import("./webhook.js");
|
|
22
21
|
const loadCrypto = () => import("../webhooks.js");
|
|
23
22
|
|
|
23
|
+
/** `"set"`: deliveries are signed with the webhook's secret. `"missing"`: it has none, so nothing is sent to it. */
|
|
24
24
|
export type SecretStatus = "set" | "missing";
|
|
25
25
|
|
|
26
|
-
/**
|
|
27
|
-
export const
|
|
28
|
-
"Webhook signing secret managed by the Sarvam Agents SDK (client.webhooks). Do not edit.";
|
|
29
|
-
/** The description of the random secret `deploy` stores when none is set yet: `secretStatus` reads it as
|
|
30
|
-
* `"missing"`, and `setSecret`/`rotateSecret` overwrite it with `SECRET_DESCRIPTION`. */
|
|
31
|
-
export const PLACEHOLDER_DESCRIPTION =
|
|
32
|
-
"Webhook signing secret managed by the Sarvam Agents SDK (client.webhooks). Placeholder: call client.webhooks.set_secret";
|
|
33
|
-
const OWNED = [SECRET_DESCRIPTION, PLACEHOLDER_DESCRIPTION];
|
|
34
|
-
const PAGE = 100;
|
|
26
|
+
/** How long a replaced secret keeps signing after `rotateSecret` or `setSecret` (fixed by the server). */
|
|
27
|
+
export const PREVIOUS_SECRET_SECONDS = 86400;
|
|
35
28
|
|
|
36
29
|
/** Throws `SecretFormatError` unless `secret` is `whsec_` plus base64 of 24 to 64 bytes (the verifier's own parser). */
|
|
37
30
|
async function checkSecret(secret: string): Promise<void> {
|
|
38
31
|
(await loadCrypto()).sign(secret, { msgId: "check", timestamp: 0, body: "" });
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
async function urlOf(
|
|
42
|
-
const url = typeof
|
|
43
|
-
if (typeof url !== "string") throw new UserError("
|
|
34
|
+
async function urlOf(webhook: unknown): Promise<string> {
|
|
35
|
+
const url = typeof webhook === "string" ? webhook : (webhook as Webhook | null | undefined)?.url;
|
|
36
|
+
if (typeof url !== "string") throw new UserError("url: pass the webhook's URL (a string) or a webhook");
|
|
44
37
|
const problem = (await loadWebhook()).urlProblem(url);
|
|
45
|
-
if (problem !== null) throw new UserError("
|
|
38
|
+
if (problem !== null) throw new UserError("url: " + problem);
|
|
46
39
|
return url;
|
|
47
40
|
}
|
|
48
41
|
|
|
49
|
-
function agent(agentId:
|
|
42
|
+
function agent(agentId: unknown): string {
|
|
50
43
|
if (typeof agentId !== "string" || agentId.trim() === "") throw new UserError("agentId: pass the agent's id");
|
|
51
44
|
return agentId;
|
|
52
45
|
}
|
|
@@ -55,203 +48,258 @@ function status(err: unknown): number {
|
|
|
55
48
|
return err instanceof ResponseError ? err.response.status : 0;
|
|
56
49
|
}
|
|
57
50
|
|
|
58
|
-
/** The
|
|
59
|
-
async function
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
51
|
+
/** The chat_service `error` object of a refusal, or null (the framework's own answer, e.g. no such route). */
|
|
52
|
+
async function chatError(err: unknown): Promise<{ message?: unknown; details?: unknown } | null> {
|
|
53
|
+
if (!(err instanceof ResponseError)) return null;
|
|
54
|
+
try {
|
|
55
|
+
const error = ((await err.response.clone().json()) as { error?: unknown })?.error;
|
|
56
|
+
return typeof error === "object" && error !== null ? (error as { details?: unknown }) : null;
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
67
59
|
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The server's reason, never its raw body: a 422 may echo the request, and the request may carry the secret. */
|
|
63
|
+
async function apiMessage(err: unknown, secret?: string): Promise<string> {
|
|
64
|
+
const message = (await chatError(err))?.message;
|
|
68
65
|
if (typeof message !== "string" || !message || (secret !== undefined && message.includes(secret))) {
|
|
69
66
|
return `HTTP ${status(err) || "error"}`;
|
|
70
67
|
}
|
|
71
68
|
return message;
|
|
72
69
|
}
|
|
73
70
|
|
|
74
|
-
/**
|
|
75
|
-
export
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
71
|
+
/** The fine-grained reason of a chat_service error (`details[0].code` or `.reason`), else undefined. */
|
|
72
|
+
export async function errorCode(err: unknown): Promise<string | undefined> {
|
|
73
|
+
const details = (await chatError(err))?.details;
|
|
74
|
+
if (Array.isArray(details) && typeof details[0] === "object" && details[0] !== null) {
|
|
75
|
+
const { reason, code } = details[0] as { reason?: unknown; code?: unknown };
|
|
76
|
+
const value = code ?? reason;
|
|
77
|
+
return typeof value === "string" ? value : undefined;
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** What to do about each refusal the secret routes answer with (`error.details[0].code`). */
|
|
83
|
+
const REFUSALS: Record<string, string> = {
|
|
84
|
+
webhook_not_found: "the agent has no such webhook (any more); deploy the agent with it first",
|
|
85
|
+
secret_missing: "it has no signing secret; create one with createSecret",
|
|
86
|
+
invalid_secret: "the server refused the secret (a whsec_ secret: base64 of 24 to 64 bytes)",
|
|
87
|
+
secret_unreadable: "its secret can no longer be decrypted on the server; rotate it (rotateSecret) or set a new " +
|
|
88
|
+
"one (setSecret)",
|
|
89
|
+
secret_store_unavailable: "the server cannot store webhook secrets right now; try again later",
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
async function refusal(err: unknown, what: string, secret?: string): Promise<unknown> {
|
|
93
|
+
if (!(err instanceof ResponseError)) return err;
|
|
94
|
+
const code = await errorCode(err);
|
|
95
|
+
if (status(err) === 409 && code === "secret_exists") {
|
|
96
|
+
return new SecretExistsError(`${what}: it already has a signing secret; replace it with rotateSecret, view it ` +
|
|
97
|
+
"with getSecret, or delete it first");
|
|
98
|
+
}
|
|
99
|
+
if (code !== undefined && code in REFUSALS) return new UserError(`${what}: ${REFUSALS[code]}`);
|
|
100
|
+
if (status(err) === 404) {
|
|
101
|
+
return new UserError(`${what}: not found (or not yours; only the agent's owner manages its webhook secrets)`);
|
|
102
|
+
}
|
|
103
|
+
if (status(err) === 403) return new UserError(`${what}: only the agent's owner manages its webhook secrets`);
|
|
104
|
+
return new UserError(`${what}: ${await apiMessage(err, secret)}`);
|
|
85
105
|
}
|
|
86
106
|
|
|
87
|
-
|
|
107
|
+
/**
|
|
108
|
+
* `client.webhooks`: an agent's agent.json webhooks and each one's signing secret.
|
|
109
|
+
*
|
|
110
|
+
* `url` is the webhook's URL exactly as in agent.json (or a webhook object). Its `webhookKey` is read from `list` by
|
|
111
|
+
* exact URL match, so the agent must have the webhook (`deploy` it first). Secrets are `whsec_` plus base64 of 24 to
|
|
112
|
+
* 64 bytes. Only the agent's owner can call these.
|
|
113
|
+
*
|
|
114
|
+
* `deploy` already creates the secret of each webhook it adds and returns it once (`Deployment.webhookSecrets`).
|
|
115
|
+
* Nothing is delivered to a webhook without a secret.
|
|
116
|
+
*/
|
|
117
|
+
export class WebhookSecrets {
|
|
118
|
+
/** The generated API these helpers call. */
|
|
119
|
+
readonly api: AgentsApi;
|
|
88
120
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
121
|
+
constructor(config: Configuration) {
|
|
122
|
+
this.api = new AgentsApi(config);
|
|
123
|
+
}
|
|
92
124
|
|
|
93
|
-
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
125
|
+
/**
|
|
126
|
+
* The agent's webhooks, each with `webhookKey`, `url`, `events`, `inAgentJson`, `secretStatus` (`"set"` or
|
|
127
|
+
* `"missing"`), `secretUpdatedAt` and, during the 24 hours after a rotation, `previousExpiresAt`.
|
|
128
|
+
*
|
|
129
|
+
* After them come orphans: a secret whose webhook was removed from agent.json is kept (the same URL added back signs
|
|
130
|
+
* with it again) and listed with `inAgentJson: false` and `url: null` (the server keeps only the key). Remove one
|
|
131
|
+
* with `deleteSecret(agentId, { webhookKey })`.
|
|
132
|
+
*/
|
|
133
|
+
async list(agentId: string): Promise<AgentWebhookView[]> {
|
|
134
|
+
const id = agent(agentId);
|
|
135
|
+
try {
|
|
136
|
+
return (await this.api.listAgentWebhooks({ agentId: id })).items ?? [];
|
|
137
|
+
} catch (err) {
|
|
138
|
+
throw await refusal(err, `agent ${id}`);
|
|
102
139
|
}
|
|
103
140
|
}
|
|
104
141
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
throw new UserError(`a Custom API named '${name}' exists that the SDK did not create; it was left alone. ` +
|
|
111
|
-
"Rename or delete it, then try again");
|
|
142
|
+
async #view(agentId: string, url: string): Promise<AgentWebhookView> {
|
|
143
|
+
const found = (await this.list(agentId)).find((v) => v.inAgentJson && v.url === url);
|
|
144
|
+
if (found === undefined) {
|
|
145
|
+
throw new UserError(`webhook ${url} is not on agent ${agentId}: deploy the agent with it first, or check the ` +
|
|
146
|
+
"URL (it must match agent.json exactly)");
|
|
112
147
|
}
|
|
113
|
-
return
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** `"set"` or `"missing"` (nothing is sent to a webhook without a secret). */
|
|
152
|
+
async secretStatus(agentId: string, url: string | Webhook): Promise<SecretStatus> {
|
|
153
|
+
const id = agent(agentId);
|
|
154
|
+
return (await this.#view(id, await urlOf(url))).secretStatus as SecretStatus;
|
|
114
155
|
}
|
|
115
156
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
157
|
+
/** The webhook's current signing secret, to view again at any time (never the previous one during a rotation).
|
|
158
|
+
* Throws `UserError` when it has none. If it leaked, rotate it. */
|
|
159
|
+
async getSecret(agentId: string, url: string | Webhook): Promise<string> {
|
|
160
|
+
const id = agent(agentId);
|
|
161
|
+
const u = await urlOf(url);
|
|
162
|
+
const { webhookKey } = await this.#view(id, u);
|
|
119
163
|
try {
|
|
120
|
-
await this.api.
|
|
164
|
+
return (await this.api.viewAgentWebhookSecret({ agentId: id, webhookKey })).secret;
|
|
121
165
|
} catch (err) {
|
|
122
|
-
|
|
123
|
-
throw new UserError(`could not bind the webhook secret to agent ${agentId}: ${await apiMessage(err)}`);
|
|
166
|
+
throw await refusal(err, `webhook ${u} of agent ${id}`);
|
|
124
167
|
}
|
|
125
168
|
}
|
|
126
169
|
|
|
127
|
-
/**
|
|
128
|
-
*
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
host, description, auth: { type: "bearer", token: secret }, ...off,
|
|
134
|
-
} });
|
|
170
|
+
/** Create the webhook's first signing secret (or a new one after a delete) and return it. Give it to your receiver.
|
|
171
|
+
* Throws `SecretExistsError` when the webhook already has one: rotate it instead. */
|
|
172
|
+
async createSecret(agentId: string, url: string | Webhook): Promise<string> {
|
|
173
|
+
const id = agent(agentId);
|
|
174
|
+
const u = await urlOf(url);
|
|
175
|
+
const { webhookKey } = await this.#view(id, u);
|
|
135
176
|
try {
|
|
136
|
-
await
|
|
177
|
+
return (await this.api.createAgentWebhookSecret({ agentId: id, webhookKey })).secret;
|
|
137
178
|
} catch (err) {
|
|
138
|
-
|
|
139
|
-
if ((await this.api.getCustomApi({ customApiId })).enabledForChats !== false) throw err; // gone (404) or on
|
|
140
|
-
await patch({});
|
|
179
|
+
throw await refusal(err, `webhook ${u} of agent ${id}`);
|
|
141
180
|
}
|
|
142
181
|
}
|
|
143
182
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
183
|
+
/**
|
|
184
|
+
* Replace the webhook's signing secret with a new one the server generates, and return it.
|
|
185
|
+
*
|
|
186
|
+
* The old secret keeps signing for 24 hours (fixed), so every delivery meanwhile carries both signatures: switch
|
|
187
|
+
* your receiver to the new secret within that time. There is one previous slot: rotating again within the 24 hours
|
|
188
|
+
* drops the first old secret at once. After a leak, cut over at once instead: `deleteSecret`, then `createSecret`.
|
|
189
|
+
* Throws `UserError` when the webhook has no secret: create one first.
|
|
190
|
+
*/
|
|
191
|
+
async rotateSecret(agentId: string, url: string | Webhook): Promise<string> {
|
|
192
|
+
const id = agent(agentId);
|
|
193
|
+
const u = await urlOf(url);
|
|
194
|
+
const { webhookKey } = await this.#view(id, u);
|
|
150
195
|
try {
|
|
151
|
-
|
|
152
|
-
customApiId = owned[0].id;
|
|
153
|
-
await this.update(customApiId, host, description, secret);
|
|
154
|
-
} else {
|
|
155
|
-
customApiId = (await this.api.createCustomApi({ customApiCreate: {
|
|
156
|
-
name, description, host, enabledForChats: false, auth: { type: "bearer", token: secret },
|
|
157
|
-
} })).id;
|
|
158
|
-
}
|
|
196
|
+
return (await this.api.rotateAgentWebhookSecret({ agentId: id, webhookKey })).secret;
|
|
159
197
|
} catch (err) {
|
|
160
|
-
|
|
161
|
-
throw new UserError(`could not store the webhook secret for ${host}: ${await apiMessage(err, secret)}`);
|
|
198
|
+
throw await refusal(err, `webhook ${u} of agent ${id}`);
|
|
162
199
|
}
|
|
163
|
-
await this.bind(agentId, customApiId);
|
|
164
|
-
return name;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async put(agentId: string, url: string, secret: string): Promise<void> {
|
|
168
|
-
await this.store(agentId, url, secret, await this.owned((await loadWebhook()).secretCredentialName(agentId, url)));
|
|
169
200
|
}
|
|
170
201
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Use `secret`, one you made, as the webhook's signing secret (bring your own). A secret it replaces keeps signing
|
|
204
|
+
* for 24 hours. The server never echoes it. A malformed secret throws `SecretFormatError` before anything is sent.
|
|
205
|
+
*
|
|
206
|
+
* Called without `secret` (the 0.5 form, deprecated) it generates one here, sets it and returns it, with a warning.
|
|
207
|
+
*/
|
|
208
|
+
async setSecret(agentId: string, url: string | Webhook, secret: string): Promise<void>;
|
|
209
|
+
/** @deprecated The 0.5 form: generates a secret, sets it and returns it. Call `createSecret` (no secret yet) or
|
|
210
|
+
* `rotateSecret` (replace it) instead. */
|
|
211
|
+
async setSecret(agentId: string, url: string | Webhook): Promise<string>;
|
|
212
|
+
async setSecret(agentId: string, url: string | Webhook, secret?: string): Promise<string | void> {
|
|
213
|
+
const id = agent(agentId);
|
|
214
|
+
const u = await urlOf(url);
|
|
215
|
+
const generated = secret === undefined;
|
|
216
|
+
let value: string;
|
|
217
|
+
if (generated) {
|
|
218
|
+
console.warn("client.webhooks.setSecret(agentId, url) without a secret is deprecated: call createSecret (no " +
|
|
219
|
+
"secret yet) or rotateSecret (replace it) instead");
|
|
220
|
+
value = (await loadCrypto()).generateSecret();
|
|
221
|
+
} else {
|
|
222
|
+
if (typeof secret !== "string") throw new UserError("secret: pass the whsec_ secret as a string");
|
|
223
|
+
await checkSecret(secret);
|
|
224
|
+
value = secret;
|
|
225
|
+
}
|
|
226
|
+
const { webhookKey } = await this.#view(id, u);
|
|
227
|
+
try {
|
|
228
|
+
await this.api.putAgentWebhookSecret({ agentId: id, webhookKey, webhookSecretPutBody: { secret: value } });
|
|
229
|
+
} catch (err) {
|
|
230
|
+
throw await refusal(err, `webhook ${u} of agent ${id}`, value);
|
|
231
|
+
}
|
|
232
|
+
if (generated) return value;
|
|
178
233
|
}
|
|
179
234
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Remove the webhook's signing secret (the current one and any previous one). Nothing is sent to the webhook until
|
|
237
|
+
* it gets a new one (`createSecret` or `setSecret`), even if it is removed from agent.json and added back. Nothing
|
|
238
|
+
* to remove is not an error.
|
|
239
|
+
*
|
|
240
|
+
* Pass the webhook's `url`, or `{ webhookKey }` (from `list`) for an orphan: a secret whose webhook was removed from
|
|
241
|
+
* agent.json has no URL.
|
|
242
|
+
*/
|
|
243
|
+
async deleteSecret(agentId: string, url: string | Webhook | { webhookKey: string }): Promise<void> {
|
|
244
|
+
const id = agent(agentId);
|
|
245
|
+
let webhookKey: string;
|
|
246
|
+
let what: string;
|
|
247
|
+
const byKey = typeof url === "object" && url !== null && "webhookKey" in url;
|
|
248
|
+
if (byKey) {
|
|
249
|
+
const key = (url as { webhookKey: unknown }).webhookKey;
|
|
250
|
+
if (typeof key !== "string" || key === "" || "url" in (url as object)) {
|
|
251
|
+
throw new UserError("deleteSecret: pass the webhook's url, or { webhookKey } from client.webhooks.list for " +
|
|
252
|
+
"an orphan (not both)");
|
|
186
253
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
254
|
+
webhookKey = key;
|
|
255
|
+
what = `webhook ${key} of agent ${id}`;
|
|
256
|
+
} else {
|
|
257
|
+
const u = await urlOf(url);
|
|
258
|
+
const found = (await this.list(id)).find((v) => v.url === u);
|
|
259
|
+
if (found === undefined) {
|
|
260
|
+
throw new UserError(`webhook ${u} is not on agent ${id}. A webhook removed from agent.json is listed without ` +
|
|
261
|
+
"its url: find it in client.webhooks.list(agentId) (inAgentJson false) and pass { webhookKey }");
|
|
191
262
|
}
|
|
263
|
+
webhookKey = found.webhookKey;
|
|
264
|
+
what = `webhook ${u} of agent ${id}`;
|
|
192
265
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const owned = await this.owned(name);
|
|
198
|
-
if (owned.length > 0) {
|
|
199
|
-
await this.bind(agentId, owned[0].id);
|
|
200
|
-
return { name, status: owned[0].description === SECRET_DESCRIPTION ? "set" : "missing" };
|
|
266
|
+
try {
|
|
267
|
+
await this.api.deleteAgentWebhookSecret({ agentId: id, webhookKey });
|
|
268
|
+
} catch (err) {
|
|
269
|
+
throw await refusal(err, what);
|
|
201
270
|
}
|
|
202
|
-
console.warn(`agent ${agentId}: webhook ${url} has no signing secret yet; stored a random one nobody holds, so ` +
|
|
203
|
-
"deliveries fail verification until you call client.webhooks.setSecret");
|
|
204
|
-
const stored = await this.store(agentId, url, (await loadCrypto()).generateSecret(), owned, PLACEHOLDER_DESCRIPTION);
|
|
205
|
-
return { name: stored, status: "missing" };
|
|
206
271
|
}
|
|
207
272
|
}
|
|
208
273
|
|
|
209
|
-
/** The one place that picks where secrets live. */
|
|
210
|
-
export function secretBackend(api: { config?: Configuration; customApis?: CustomAPIsApi }): SecretBackend {
|
|
211
|
-
return new CustomApiBackend(api.customApis ?? new CustomAPIsApi(api.config));
|
|
212
|
-
}
|
|
213
|
-
|
|
214
274
|
/**
|
|
215
|
-
* `
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
275
|
+
* For `deploy`: create the secret of each of `urls` that is in `added` (the webhooks the deploy added) and whose
|
|
276
|
+
* `secretStatus` is `missing`. Returns the new secrets by URL and the URLs still without a secret: a failed create, a
|
|
277
|
+
* webhook not listed, and a webhook deployed before whose secret is missing (never recreated here). Never rotates or
|
|
278
|
+
* overwrites a secret. A failed create is reported as missing, never thrown; a failed list throws.
|
|
219
279
|
*/
|
|
220
|
-
export
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
/** `"set"` when the agent has a signing secret stored by `setSecret` or `rotateSecret` for the webhook; `"missing"`
|
|
245
|
-
* otherwise, including while it holds the placeholder a deploy stored. */
|
|
246
|
-
async secretStatus(agentId: string, webhook: string | Webhook): Promise<SecretStatus> {
|
|
247
|
-
const id = agent(agentId);
|
|
248
|
-
return this.#backend.status(id, await urlOf(webhook));
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/** Remove the webhook's signing secret. Only what the SDK stored is removed; a Custom API you made yourself is
|
|
252
|
-
* never touched. Deliveries to the webhook fail until a new secret is set or the webhook is removed. */
|
|
253
|
-
async deleteSecret(agentId: string, webhook: string | Webhook): Promise<void> {
|
|
254
|
-
const id = agent(agentId);
|
|
255
|
-
await this.#backend.delete(id, await urlOf(webhook));
|
|
280
|
+
export async function createMissingSecrets(api: AgentsApi, agentId: string, urls: readonly string[],
|
|
281
|
+
added: ReadonlySet<string>): Promise<{ created: Record<string, string>; missing: string[] }> {
|
|
282
|
+
const created: Record<string, string> = {};
|
|
283
|
+
const missing: string[] = [];
|
|
284
|
+
if (urls.length === 0) return { created, missing };
|
|
285
|
+
const views = (await api.listAgentWebhooks({ agentId })).items ?? [];
|
|
286
|
+
for (const url of urls) {
|
|
287
|
+
const view = views.find((v) => v.inAgentJson && v.url === url);
|
|
288
|
+
if (view === undefined) {
|
|
289
|
+
missing.push(url);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (view.secretStatus !== "missing") continue;
|
|
293
|
+
if (!added.has(url)) {
|
|
294
|
+
missing.push(url); // deployed before: its owner may have deleted the secret on purpose
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
created[url] = (await api.createAgentWebhookSecret({ agentId, webhookKey: view.webhookKey })).secret;
|
|
299
|
+
} catch (err) {
|
|
300
|
+
if (status(err) === 409 && (await errorCode(err)) === "secret_exists") continue; // created meanwhile: not ours
|
|
301
|
+
missing.push(url);
|
|
302
|
+
}
|
|
256
303
|
}
|
|
304
|
+
return { created, missing };
|
|
257
305
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,8 @@ export * from './models/index.js';
|
|
|
7
7
|
export * from './streaming.js';
|
|
8
8
|
// Hand-written: the high-level client (client.webhooks, client.triggers).
|
|
9
9
|
export { Client } from './client.js';
|
|
10
|
-
export { WebhookSecrets } from './compose/webhookSecrets.js';
|
|
10
|
+
export { WebhookSecrets, PREVIOUS_SECRET_SECONDS } from './compose/webhookSecrets.js';
|
|
11
|
+
export { SecretExistsError } from './compose/errors.js';
|
|
11
12
|
export type { SecretStatus } from './compose/webhookSecrets.js';
|
|
12
13
|
export {
|
|
13
14
|
Triggers, EVENT_WINDOW_DAYS, EVENTS_PAGE_DEFAULT, EVENTS_PAGE_MAX, GRACE_SECONDS_MAX, PROVIDER_OUTAGE_ERROR,
|
|
@@ -126,6 +126,12 @@ export interface AgentConversationDetail {
|
|
|
126
126
|
* @memberof AgentConversationDetail
|
|
127
127
|
*/
|
|
128
128
|
createdAt: Date;
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @type {number}
|
|
132
|
+
* @memberof AgentConversationDetail
|
|
133
|
+
*/
|
|
134
|
+
lastAgentVersion?: number | null;
|
|
129
135
|
/**
|
|
130
136
|
*
|
|
131
137
|
* @type {Array<FieldHighlight>}
|
|
@@ -180,6 +186,12 @@ export interface AgentConversationDetail {
|
|
|
180
186
|
* @memberof AgentConversationDetail
|
|
181
187
|
*/
|
|
182
188
|
panjaWsId: string | null;
|
|
189
|
+
/**
|
|
190
|
+
*
|
|
191
|
+
* @type {string}
|
|
192
|
+
* @memberof AgentConversationDetail
|
|
193
|
+
*/
|
|
194
|
+
startedByName?: string | null;
|
|
183
195
|
/**
|
|
184
196
|
*
|
|
185
197
|
* @type {boolean}
|
|
@@ -241,6 +253,7 @@ export function AgentConversationDetailFromJSONTyped(json: any, ignoreDiscrimina
|
|
|
241
253
|
'lastActiveAt': (new Date(json['lastActiveAt'])),
|
|
242
254
|
'lastReadAt': (json['lastReadAt'] == null ? null : new Date(json['lastReadAt'])),
|
|
243
255
|
'createdAt': (new Date(json['createdAt'])),
|
|
256
|
+
'lastAgentVersion': json['lastAgentVersion'] == null ? undefined : json['lastAgentVersion'],
|
|
244
257
|
'highlight': json['highlight'] == null ? undefined : ((json['highlight'] as Array<any>).map(FieldHighlightFromJSON)),
|
|
245
258
|
'messages': ((json['messages'] as Array<any>).map(UIMessageFromJSON)),
|
|
246
259
|
'lastEventId': json['lastEventId'],
|
|
@@ -250,6 +263,7 @@ export function AgentConversationDetailFromJSONTyped(json: any, ignoreDiscrimina
|
|
|
250
263
|
'requiredActions': json['requiredActions'] == null ? undefined : ((json['requiredActions'] as Array<any>).map(RequiredActionFromJSON)),
|
|
251
264
|
'isMine': json['isMine'] == null ? undefined : json['isMine'],
|
|
252
265
|
'panjaWsId': json['panjaWsId'],
|
|
266
|
+
'startedByName': json['startedByName'] == null ? undefined : json['startedByName'],
|
|
253
267
|
'readOnly': json['readOnly'] == null ? undefined : json['readOnly'],
|
|
254
268
|
'panjaUserId': json['panjaUserId'] == null ? undefined : json['panjaUserId'],
|
|
255
269
|
};
|
|
@@ -279,6 +293,7 @@ export function AgentConversationDetailToJSONTyped(value?: AgentConversationDeta
|
|
|
279
293
|
'lastActiveAt': ((value['lastActiveAt']).toISOString()),
|
|
280
294
|
'lastReadAt': (value['lastReadAt'] == null ? null : (value['lastReadAt'] as any).toISOString()),
|
|
281
295
|
'createdAt': ((value['createdAt']).toISOString()),
|
|
296
|
+
'lastAgentVersion': value['lastAgentVersion'],
|
|
282
297
|
'highlight': value['highlight'] == null ? undefined : ((value['highlight'] as Array<any>).map(FieldHighlightToJSON)),
|
|
283
298
|
'messages': ((value['messages'] as Array<any>).map(UIMessageToJSON)),
|
|
284
299
|
'lastEventId': value['lastEventId'],
|
|
@@ -288,6 +303,7 @@ export function AgentConversationDetailToJSONTyped(value?: AgentConversationDeta
|
|
|
288
303
|
'requiredActions': value['requiredActions'] == null ? undefined : ((value['requiredActions'] as Array<any>).map(RequiredActionToJSON)),
|
|
289
304
|
'isMine': value['isMine'],
|
|
290
305
|
'panjaWsId': value['panjaWsId'],
|
|
306
|
+
'startedByName': value['startedByName'],
|
|
291
307
|
'readOnly': value['readOnly'],
|
|
292
308
|
'panjaUserId': value['panjaUserId'],
|
|
293
309
|
};
|
|
@@ -106,6 +106,12 @@ export interface AgentConversationView {
|
|
|
106
106
|
* @memberof AgentConversationView
|
|
107
107
|
*/
|
|
108
108
|
createdAt: Date;
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* @type {number}
|
|
112
|
+
* @memberof AgentConversationView
|
|
113
|
+
*/
|
|
114
|
+
lastAgentVersion?: number | null;
|
|
109
115
|
/**
|
|
110
116
|
*
|
|
111
117
|
* @type {Array<FieldHighlight>}
|
|
@@ -124,6 +130,12 @@ export interface AgentConversationView {
|
|
|
124
130
|
* @memberof AgentConversationView
|
|
125
131
|
*/
|
|
126
132
|
panjaWsId: string | null;
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
* @type {string}
|
|
136
|
+
* @memberof AgentConversationView
|
|
137
|
+
*/
|
|
138
|
+
startedByName?: string | null;
|
|
127
139
|
/**
|
|
128
140
|
* Deprecated: the server no longer sends this field, so it is always undefined. Use isMine.
|
|
129
141
|
* @type {string}
|
|
@@ -177,9 +189,11 @@ export function AgentConversationViewFromJSONTyped(json: any, ignoreDiscriminato
|
|
|
177
189
|
'lastActiveAt': (new Date(json['lastActiveAt'])),
|
|
178
190
|
'lastReadAt': (json['lastReadAt'] == null ? null : new Date(json['lastReadAt'])),
|
|
179
191
|
'createdAt': (new Date(json['createdAt'])),
|
|
192
|
+
'lastAgentVersion': json['lastAgentVersion'] == null ? undefined : json['lastAgentVersion'],
|
|
180
193
|
'highlight': json['highlight'] == null ? undefined : ((json['highlight'] as Array<any>).map(FieldHighlightFromJSON)),
|
|
181
194
|
'isMine': json['isMine'] == null ? undefined : json['isMine'],
|
|
182
195
|
'panjaWsId': json['panjaWsId'],
|
|
196
|
+
'startedByName': json['startedByName'] == null ? undefined : json['startedByName'],
|
|
183
197
|
'panjaUserId': json['panjaUserId'] == null ? undefined : json['panjaUserId'],
|
|
184
198
|
};
|
|
185
199
|
}
|
|
@@ -208,9 +222,11 @@ export function AgentConversationViewToJSONTyped(value?: AgentConversationView |
|
|
|
208
222
|
'lastActiveAt': ((value['lastActiveAt']).toISOString()),
|
|
209
223
|
'lastReadAt': (value['lastReadAt'] == null ? null : (value['lastReadAt'] as any).toISOString()),
|
|
210
224
|
'createdAt': ((value['createdAt']).toISOString()),
|
|
225
|
+
'lastAgentVersion': value['lastAgentVersion'],
|
|
211
226
|
'highlight': value['highlight'] == null ? undefined : ((value['highlight'] as Array<any>).map(FieldHighlightToJSON)),
|
|
212
227
|
'isMine': value['isMine'],
|
|
213
228
|
'panjaWsId': value['panjaWsId'],
|
|
229
|
+
'startedByName': value['startedByName'],
|
|
214
230
|
'panjaUserId': value['panjaUserId'],
|
|
215
231
|
};
|
|
216
232
|
}
|