sarvam-agents-client 0.4.0 → 0.5.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.
Files changed (173) hide show
  1. package/README.md +136 -23
  2. package/dist/apis/AgentConversationsApi.d.ts +8 -2
  3. package/dist/apis/AgentConversationsApi.js +8 -2
  4. package/dist/apis/AgentsApi.d.ts +6 -6
  5. package/dist/apis/AgentsApi.js +6 -6
  6. package/dist/apis/ConversationsApi.d.ts +6 -2
  7. package/dist/apis/ConversationsApi.js +10 -2
  8. package/dist/apis/MessagesApi.d.ts +1 -0
  9. package/dist/apis/MessagesApi.js +3 -0
  10. package/dist/apis/TriggersApi.d.ts +154 -0
  11. package/dist/apis/TriggersApi.js +376 -0
  12. package/dist/apis/index.d.ts +1 -0
  13. package/dist/apis/index.js +1 -0
  14. package/dist/client.d.ts +21 -0
  15. package/dist/client.js +24 -0
  16. package/dist/compose/agent.d.ts +4 -0
  17. package/dist/compose/agent.js +9 -0
  18. package/dist/compose/deploy.d.ts +8 -0
  19. package/dist/compose/deploy.js +32 -6
  20. package/dist/compose/index.d.ts +5 -1
  21. package/dist/compose/index.js +12 -1
  22. package/dist/compose/settingsTransport.d.ts +22 -3
  23. package/dist/compose/settingsTransport.js +180 -15
  24. package/dist/compose/transport.d.ts +19 -3
  25. package/dist/compose/trigger.d.ts +166 -0
  26. package/dist/compose/trigger.js +525 -0
  27. package/dist/compose/triggers.d.ts +96 -0
  28. package/dist/compose/triggers.js +319 -0
  29. package/dist/compose/webhook.d.ts +18 -3
  30. package/dist/compose/webhook.js +59 -10
  31. package/dist/compose/webhookSecrets.d.ts +64 -0
  32. package/dist/compose/webhookSecrets.js +255 -0
  33. package/dist/esm/apis/AgentConversationsApi.d.ts +8 -2
  34. package/dist/esm/apis/AgentConversationsApi.js +8 -2
  35. package/dist/esm/apis/AgentsApi.d.ts +6 -6
  36. package/dist/esm/apis/AgentsApi.js +6 -6
  37. package/dist/esm/apis/ConversationsApi.d.ts +6 -2
  38. package/dist/esm/apis/ConversationsApi.js +10 -2
  39. package/dist/esm/apis/MessagesApi.d.ts +1 -0
  40. package/dist/esm/apis/MessagesApi.js +3 -0
  41. package/dist/esm/apis/TriggersApi.d.ts +154 -0
  42. package/dist/esm/apis/TriggersApi.js +372 -0
  43. package/dist/esm/apis/index.d.ts +1 -0
  44. package/dist/esm/apis/index.js +1 -0
  45. package/dist/esm/client.d.ts +21 -0
  46. package/dist/esm/client.js +20 -0
  47. package/dist/esm/compose/agent.d.ts +4 -0
  48. package/dist/esm/compose/agent.js +9 -0
  49. package/dist/esm/compose/deploy.d.ts +8 -0
  50. package/dist/esm/compose/deploy.js +32 -6
  51. package/dist/esm/compose/index.d.ts +5 -1
  52. package/dist/esm/compose/index.js +2 -0
  53. package/dist/esm/compose/settingsTransport.d.ts +22 -3
  54. package/dist/esm/compose/settingsTransport.js +179 -15
  55. package/dist/esm/compose/transport.d.ts +19 -3
  56. package/dist/esm/compose/trigger.d.ts +166 -0
  57. package/dist/esm/compose/trigger.js +514 -0
  58. package/dist/esm/compose/triggers.d.ts +96 -0
  59. package/dist/esm/compose/triggers.js +315 -0
  60. package/dist/esm/compose/webhook.d.ts +18 -3
  61. package/dist/esm/compose/webhook.js +55 -9
  62. package/dist/esm/compose/webhookSecrets.d.ts +64 -0
  63. package/dist/esm/compose/webhookSecrets.js +250 -0
  64. package/dist/esm/index.d.ts +5 -0
  65. package/dist/esm/index.js +4 -0
  66. package/dist/esm/models/AgentResource.d.ts +7 -0
  67. package/dist/esm/models/AgentResource.js +3 -0
  68. package/dist/esm/models/FireResponse.d.ts +63 -0
  69. package/dist/esm/models/FireResponse.js +63 -0
  70. package/dist/esm/models/MessagePart.d.ts +8 -0
  71. package/dist/esm/models/MessagePart.js +2 -0
  72. package/dist/esm/models/ResponseRotateTriggerSecret.d.ts +50 -0
  73. package/dist/esm/models/ResponseRotateTriggerSecret.js +54 -0
  74. package/dist/esm/models/RotatedSecret.d.ts +44 -0
  75. package/dist/esm/models/RotatedSecret.js +50 -0
  76. package/dist/esm/models/RotatedSigningSecret.d.ts +40 -0
  77. package/dist/esm/models/RotatedSigningSecret.js +46 -0
  78. package/dist/esm/models/TriggerDryRun.d.ts +44 -0
  79. package/dist/esm/models/TriggerDryRun.js +47 -0
  80. package/dist/esm/models/TriggerEventList.d.ts +39 -0
  81. package/dist/esm/models/TriggerEventList.js +46 -0
  82. package/dist/esm/models/TriggerEventView.d.ts +117 -0
  83. package/dist/esm/models/TriggerEventView.js +89 -0
  84. package/dist/esm/models/TriggerList.d.ts +33 -0
  85. package/dist/esm/models/TriggerList.js +44 -0
  86. package/dist/esm/models/TriggerSecretPut.d.ts +38 -0
  87. package/dist/esm/models/TriggerSecretPut.js +45 -0
  88. package/dist/esm/models/TriggerSecretState.d.ts +52 -0
  89. package/dist/esm/models/TriggerSecretState.js +56 -0
  90. package/dist/esm/models/TriggerTestFireRequest.d.ts +38 -0
  91. package/dist/esm/models/TriggerTestFireRequest.js +43 -0
  92. package/dist/esm/models/TriggerToken.d.ts +50 -0
  93. package/dist/esm/models/TriggerToken.js +56 -0
  94. package/dist/esm/models/TriggerView.d.ts +105 -0
  95. package/dist/esm/models/TriggerView.js +92 -0
  96. package/dist/esm/models/index.d.ts +13 -0
  97. package/dist/esm/models/index.js +13 -0
  98. package/dist/esm/runtime.d.ts +5 -0
  99. package/dist/esm/runtime.js +11 -0
  100. package/dist/esm/webhooks.d.ts +5 -1
  101. package/dist/index.d.ts +5 -0
  102. package/dist/index.js +14 -0
  103. package/dist/models/AgentResource.d.ts +7 -0
  104. package/dist/models/AgentResource.js +3 -0
  105. package/dist/models/FireResponse.d.ts +63 -0
  106. package/dist/models/FireResponse.js +71 -0
  107. package/dist/models/MessagePart.d.ts +8 -0
  108. package/dist/models/MessagePart.js +2 -0
  109. package/dist/models/ResponseRotateTriggerSecret.d.ts +50 -0
  110. package/dist/models/ResponseRotateTriggerSecret.js +61 -0
  111. package/dist/models/RotatedSecret.d.ts +44 -0
  112. package/dist/models/RotatedSecret.js +57 -0
  113. package/dist/models/RotatedSigningSecret.d.ts +40 -0
  114. package/dist/models/RotatedSigningSecret.js +53 -0
  115. package/dist/models/TriggerDryRun.d.ts +44 -0
  116. package/dist/models/TriggerDryRun.js +54 -0
  117. package/dist/models/TriggerEventList.d.ts +39 -0
  118. package/dist/models/TriggerEventList.js +53 -0
  119. package/dist/models/TriggerEventView.d.ts +117 -0
  120. package/dist/models/TriggerEventView.js +97 -0
  121. package/dist/models/TriggerList.d.ts +33 -0
  122. package/dist/models/TriggerList.js +51 -0
  123. package/dist/models/TriggerSecretPut.d.ts +38 -0
  124. package/dist/models/TriggerSecretPut.js +52 -0
  125. package/dist/models/TriggerSecretState.d.ts +52 -0
  126. package/dist/models/TriggerSecretState.js +64 -0
  127. package/dist/models/TriggerTestFireRequest.d.ts +38 -0
  128. package/dist/models/TriggerTestFireRequest.js +50 -0
  129. package/dist/models/TriggerToken.d.ts +50 -0
  130. package/dist/models/TriggerToken.js +63 -0
  131. package/dist/models/TriggerView.d.ts +105 -0
  132. package/dist/models/TriggerView.js +100 -0
  133. package/dist/models/index.d.ts +13 -0
  134. package/dist/models/index.js +13 -0
  135. package/dist/runtime.d.ts +5 -0
  136. package/dist/runtime.js +12 -0
  137. package/dist/webhooks.d.ts +5 -1
  138. package/package.json +1 -1
  139. package/src/apis/AgentConversationsApi.ts +8 -2
  140. package/src/apis/AgentsApi.ts +6 -6
  141. package/src/apis/ConversationsApi.ts +14 -2
  142. package/src/apis/MessagesApi.ts +5 -0
  143. package/src/apis/TriggersApi.ts +584 -0
  144. package/src/apis/index.ts +1 -0
  145. package/src/client.ts +27 -0
  146. package/src/compose/agent.ts +11 -0
  147. package/src/compose/deploy.ts +42 -7
  148. package/src/compose/index.ts +8 -1
  149. package/src/compose/settingsTransport.ts +183 -18
  150. package/src/compose/transport.ts +20 -3
  151. package/src/compose/trigger.ts +635 -0
  152. package/src/compose/triggers.ts +337 -0
  153. package/src/compose/webhook.ts +64 -11
  154. package/src/compose/webhookSecrets.ts +257 -0
  155. package/src/index.ts +9 -0
  156. package/src/models/AgentResource.ts +15 -0
  157. package/src/models/FireResponse.ts +107 -0
  158. package/src/models/MessagePart.ts +8 -0
  159. package/src/models/ResponseRotateTriggerSecret.ts +107 -0
  160. package/src/models/RotatedSecret.ts +83 -0
  161. package/src/models/RotatedSigningSecret.ts +76 -0
  162. package/src/models/TriggerDryRun.ts +82 -0
  163. package/src/models/TriggerEventList.ts +82 -0
  164. package/src/models/TriggerEventView.ts +183 -0
  165. package/src/models/TriggerList.ts +74 -0
  166. package/src/models/TriggerSecretPut.ts +74 -0
  167. package/src/models/TriggerSecretState.ts +94 -0
  168. package/src/models/TriggerTestFireRequest.ts +73 -0
  169. package/src/models/TriggerToken.ts +93 -0
  170. package/src/models/TriggerView.ts +168 -0
  171. package/src/models/index.ts +13 -0
  172. package/src/runtime.ts +12 -0
  173. package/src/webhooks.ts +5 -1
@@ -3,12 +3,20 @@ import type { Configuration } from "../runtime.js";
3
3
  import type { Agent } from "./agent.js";
4
4
  import type { OutputType } from "./output.js";
5
5
  import type { Transport } from "./transport.js";
6
+ import type { MintedTriggerToken } from "./trigger.js";
6
7
  export interface Deployment {
7
8
  agentId: string;
8
9
  action: "created" | "updated" | "unchanged";
9
10
  /** The live version, or while the agent is paused, the paused version (what `resumeAgent` brings back); undefined
10
11
  * from a server without versions, or when no version was ever live. */
11
12
  version?: number;
13
+ /** URLs of the webhooks that still have no signing secret: this deploy stored a placeholder secret nobody holds so
14
+ * the server accepts them. Call `client.webhooks.setSecret(agentId, url)` for each before pointing a provider at
15
+ * the agent; until then deliveries fail verification. Left out when there are none. */
16
+ webhooksMissingSecret?: string[];
17
+ /** The token (and fire URL) of each bearer trigger this deploy created. The server shows a token only once: keep
18
+ * it now. Left out when nothing was minted; `client.triggers.list(agentId)` lists every trigger's URL. */
19
+ triggerTokens?: MintedTriggerToken[];
12
20
  }
13
21
  /** Set (or clear, with null) the client used when deploy/remove/run get no client. */
14
22
  export declare function setDefaultClient(config: Configuration | null): void;
@@ -63,10 +63,33 @@ async function update(transport, remote, agent, strict) {
63
63
  return settle(transport, remote, agent);
64
64
  }
65
65
  const updated = await transport.updateAgent(remote.agentId, agent);
66
- return deployment(remote.agentId, "updated", liveVersion(updated));
66
+ return deployment(remote.agentId, "updated", liveVersion(updated), missingSecrets(updated), updated === null || updated === void 0 ? void 0 : updated.triggerTokens);
67
+ }
68
+ function deployment(agentId, action, version, webhooksMissingSecret = [], tokens) {
69
+ const out = version === undefined ? { agentId, action } : { agentId, action, version };
70
+ if (webhooksMissingSecret.length > 0)
71
+ out.webhooksMissingSecret = webhooksMissingSecret;
72
+ if (tokens !== undefined && tokens.length > 0)
73
+ out.triggerTokens = tokens;
74
+ return out;
75
+ }
76
+ function missingSecrets(remote) {
77
+ const missing = remote === null || remote === void 0 ? void 0 : remote.webhooksMissingSecret;
78
+ return Array.isArray(missing) ? [...missing] : [];
79
+ }
80
+ /** `Transport.publish`'s answer: the live version, or a `Published` with the tokens its deploy minted. */
81
+ function published(agentId, result) {
82
+ if (typeof result === "object" && result !== null) {
83
+ return deployment(agentId, "updated", result.liveVersion, [], result.triggerTokens);
84
+ }
85
+ return deployment(agentId, "updated", typeof result === "number" ? result : undefined);
67
86
  }
68
- function deployment(agentId, action, version) {
69
- return version === undefined ? { agentId, action } : { agentId, action, version };
87
+ /** An unchanged live agent is never deployed again, not even to finish a trigger reconcile that failed (502) after
88
+ * its deploy went live: the owner may have deleted, disabled or rolled back triggers since, and a deploy would undo
89
+ * that. The transport only reads the trigger rows and warns with the fix (deploy the live version again). */
90
+ async function checkTriggers(transport, remote, agent) {
91
+ if (transport.checkTriggers !== undefined)
92
+ await transport.checkTriggers(remote.agentId, agent, remote.liveVersion);
70
93
  }
71
94
  // Optional chaining: a custom transport written for 0.3 may resolve anything from updateAgent/createAgent.
72
95
  function liveVersion(remote) {
@@ -82,9 +105,11 @@ function liveVersion(remote) {
82
105
  async function settle(transport, remote, agent) {
83
106
  const state = remote.versionState;
84
107
  if (state === undefined || state === "live" || state === "paused" || !transport.publish) {
108
+ if (state === "live")
109
+ await checkTriggers(transport, remote, agent);
85
110
  return deployment(remote.agentId, "unchanged", remote.liveVersion);
86
111
  }
87
- return deployment(remote.agentId, "updated", await transport.publish(remote.agentId, transport_js_1.VERSION_NOTE));
112
+ return published(remote.agentId, await transport.publish(remote.agentId, transport_js_1.VERSION_NOTE));
88
113
  }
89
114
  async function deployWith(agent, transport, strict, adopt) {
90
115
  const found = await transport.findAgents(agent.name);
@@ -99,13 +124,14 @@ async function deployWith(agent, transport, strict, adopt) {
99
124
  if (others.length) {
100
125
  throw new errors_js_1.UserError(`cannot adopt ${adopt}: agent "${agent.name}" is already deployed as ${others.join(", ")}`);
101
126
  }
102
- return deployment(adopt, "updated", liveVersion(await transport.updateAgent(adopt, agent)));
127
+ const adopted = await transport.updateAgent(adopt, agent);
128
+ return deployment(adopt, "updated", liveVersion(adopted), missingSecrets(adopted), adopted === null || adopted === void 0 ? void 0 : adopted.triggerTokens);
103
129
  }
104
130
  if (found.length > 1)
105
131
  throw new errors_js_1.DuplicateAgentsError(found.map((r) => r.agentId));
106
132
  if (found.length === 0) {
107
133
  const created = await transport.createAgent(agent);
108
- return deployment(created.agentId, "created", liveVersion(created));
134
+ return deployment(created.agentId, "created", liveVersion(created), missingSecrets(created), created.triggerTokens);
109
135
  }
110
136
  return update(transport, found[0], agent, strict);
111
137
  }
@@ -5,9 +5,13 @@ export * from "./agent.js";
5
5
  export { tool } from "./functionTool.js";
6
6
  export type { FunctionCallDetails, FunctionTool, FunctionToolArgs, FunctionToolOptions } from "./functionTool.js";
7
7
  export type { Webhook } from "./webhook.js";
8
+ export { ALLOW_ALL, EXACT_NAME_TOOLS, ToolPolicy, Trigger, keyed, newSession } from "./trigger.js";
9
+ export type { BearerVerify, FilterRule, FilterValue, HmacVerify, SecretVerify, SlackVerify, TriggerLimits, TriggerOptions, TriggerSession, MintedTriggerToken, TriggerVerify, } from "./trigger.js";
10
+ export { Triggers, EVENT_WINDOW_DAYS, PROVIDER_OUTAGE_ERROR } from "./triggers.js";
11
+ export type { TriggerRef, TriggerSecretStatus } from "./triggers.js";
8
12
  export type { Event, ClarifyQuestion, FunctionCallRequest, OutputCitation } from "./events.js";
9
13
  export { SettingsTransport } from "./settingsTransport.js";
10
- export type { PendingFunctionCalls, RemoteAgent, SettingsWarning, TurnStream, Transport } from "./transport.js";
14
+ export type { PendingFunctionCalls, Published, RemoteAgent, SettingsWarning, TurnStream, Transport } from "./transport.js";
11
15
  export type { FunctionResult } from "./functionTool.js";
12
16
  export { deploy, remove, undeploy, setDefaultClient } from "./deploy.js";
13
17
  export type { Deployment } from "./deploy.js";
@@ -14,13 +14,24 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.RunState = exports.StreamedRunResult = exports.RunResult = exports.run = exports.setDefaultClient = exports.undeploy = exports.remove = exports.deploy = exports.SettingsTransport = exports.tool = void 0;
17
+ exports.RunState = exports.StreamedRunResult = exports.RunResult = exports.run = exports.setDefaultClient = exports.undeploy = exports.remove = exports.deploy = exports.SettingsTransport = exports.PROVIDER_OUTAGE_ERROR = exports.EVENT_WINDOW_DAYS = exports.Triggers = exports.newSession = exports.keyed = exports.Trigger = exports.ToolPolicy = exports.EXACT_NAME_TOOLS = exports.ALLOW_ALL = exports.tool = void 0;
18
18
  __exportStar(require("./errors.js"), exports);
19
19
  __exportStar(require("./fingerprint.js"), exports);
20
20
  __exportStar(require("./tools.js"), exports);
21
21
  __exportStar(require("./agent.js"), exports);
22
22
  var functionTool_js_1 = require("./functionTool.js");
23
23
  Object.defineProperty(exports, "tool", { enumerable: true, get: function () { return functionTool_js_1.tool; } });
24
+ var trigger_js_1 = require("./trigger.js");
25
+ Object.defineProperty(exports, "ALLOW_ALL", { enumerable: true, get: function () { return trigger_js_1.ALLOW_ALL; } });
26
+ Object.defineProperty(exports, "EXACT_NAME_TOOLS", { enumerable: true, get: function () { return trigger_js_1.EXACT_NAME_TOOLS; } });
27
+ Object.defineProperty(exports, "ToolPolicy", { enumerable: true, get: function () { return trigger_js_1.ToolPolicy; } });
28
+ Object.defineProperty(exports, "Trigger", { enumerable: true, get: function () { return trigger_js_1.Trigger; } });
29
+ Object.defineProperty(exports, "keyed", { enumerable: true, get: function () { return trigger_js_1.keyed; } });
30
+ Object.defineProperty(exports, "newSession", { enumerable: true, get: function () { return trigger_js_1.newSession; } });
31
+ var triggers_js_1 = require("./triggers.js");
32
+ Object.defineProperty(exports, "Triggers", { enumerable: true, get: function () { return triggers_js_1.Triggers; } });
33
+ Object.defineProperty(exports, "EVENT_WINDOW_DAYS", { enumerable: true, get: function () { return triggers_js_1.EVENT_WINDOW_DAYS; } });
34
+ Object.defineProperty(exports, "PROVIDER_OUTAGE_ERROR", { enumerable: true, get: function () { return triggers_js_1.PROVIDER_OUTAGE_ERROR; } });
24
35
  var settingsTransport_js_1 = require("./settingsTransport.js");
25
36
  Object.defineProperty(exports, "SettingsTransport", { enumerable: true, get: function () { return settingsTransport_js_1.SettingsTransport; } });
26
37
  var deploy_js_1 = require("./deploy.js");
@@ -2,7 +2,9 @@ import type { Configuration } from "../runtime.js";
2
2
  import type { Agent } from "./agent.js";
3
3
  import type { OutputType } from "./output.js";
4
4
  import type { FunctionResult } from "./functionTool.js";
5
- import type { PendingFunctionCalls, RemoteAgent, Transport, TurnStream } from "./transport.js";
5
+ import type { PendingFunctionCalls, Published, RemoteAgent, Transport, TurnStream } from "./transport.js";
6
+ /** @internal Forget this process's deploys and warnings (tests: a new process). */
7
+ export declare function forgetDeploys(): void;
6
8
  /** After a 409 expired or already_answered the server goes on with the turn; chat_service may still be picking that
7
9
  * continuation up. The conversation is read again every `followPollMs.value` ms, at most FOLLOW_POLLS times. Mutable
8
10
  * for tests; not exported from the compose root. */
@@ -18,10 +20,16 @@ export declare class SettingsTransport implements Transport {
18
20
  private readonly conversations;
19
21
  private readonly responses;
20
22
  private readonly customApis;
23
+ private readonly triggers;
24
+ private readonly secrets;
21
25
  constructor(config: Configuration);
22
26
  findAgents(sdkName: string): Promise<RemoteAgent[]>;
23
27
  readAgent(agentId: string): Promise<RemoteAgent>;
24
28
  createAgent(agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
29
+ /** `settings` with each webhook that names no `secret_credential` given the one `client.webhooks` keeps for the
30
+ * agent and host (stored, with a random secret nobody holds, when there is none yet), and the URLs of those
31
+ * webhooks still without a secret set by `client.webhooks`. */
32
+ private withSecretCredentials;
25
33
  /** The caller's Custom APIs the webhooks name, to bind to a new agent: one entry per name that matches exactly
26
34
  * one, and a sentence per name that matches none or several (left to the server, which refuses it only when it
27
35
  * checks credentials). */
@@ -34,11 +42,22 @@ export declare class SettingsTransport implements Transport {
34
42
  * deploy 0.3 sent; otherwise the draft is saved as a version and that version deployed. */
35
43
  private goLive;
36
44
  /**
37
- * Save the draft as the next version and make it live. 409 NO_CHANGES means the newest saved version already is the
45
+ * Save the draft as the next version and make it live; resolves to the live version and the bearer tokens the
46
+ * deploy's trigger reconcile minted (shown once). 409 NO_CHANGES means the newest saved version already is the
38
47
  * draft, so that version is deployed, by its number (never "the newest", which a save landing in between could
39
48
  * change).
40
49
  */
41
- publish(agentId: string, note: string): Promise<number | undefined>;
50
+ publish(agentId: string, note: string): Promise<Published>;
51
+ /** Read the trigger rows of an unchanged live agent and warn, once per process, when they do not match
52
+ * `agent.triggers`. Never deploys: an unchanged definition writes nothing, and the rows may differ because the
53
+ * owner deleted, disabled or rolled back triggers in the dashboard, which a deploy would undo. A deploy whose
54
+ * trigger reconcile failed (502) went live without them; the warning names the fix, deploying the live version
55
+ * again. A failing read is ignored and never fails the deploy. */
56
+ checkTriggers(agentId: string, agent: Agent<OutputType | undefined>, liveVersion?: number): Promise<void>;
57
+ /** What differs and the auto-paused trigger names when the agent's trigger rows do not match `agent.triggers` (a
58
+ * trigger with no row, or a removed one still enabled), else null (also when the server has triggers switched
59
+ * off). */
60
+ private triggerMismatch;
42
61
  deleteAgent(agentId: string): Promise<void>;
43
62
  startTurn(agentId: string, conversationId: string | null, text: string, signal?: AbortSignal): Promise<TurnStream>;
44
63
  answerClarify(conversationId: string, requestId: string, answers: Record<string, unknown>, signal?: AbortSignal): Promise<TurnStream>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SettingsTransport = exports.followPollMs = void 0;
4
+ exports.forgetDeploys = forgetDeploys;
4
5
  exports.idempotencyKey = idempotencyKey;
5
6
  /** SettingsTransport: the compose transport on the generated API (agent settings file + responses stream). */
6
7
  const node_crypto_1 = require("node:crypto");
@@ -8,12 +9,28 @@ const AgentsApi_js_1 = require("../apis/AgentsApi.js");
8
9
  const ConversationsApi_js_1 = require("../apis/ConversationsApi.js");
9
10
  const CustomAPIsApi_js_1 = require("../apis/CustomAPIsApi.js");
10
11
  const ResponsesApi_js_1 = require("../apis/ResponsesApi.js");
12
+ const TriggersApi_js_1 = require("../apis/TriggersApi.js");
11
13
  const runtime_js_1 = require("../runtime.js");
12
14
  const streaming_js_1 = require("../streaming.js");
13
15
  const errors_js_1 = require("./errors.js");
14
16
  const events_js_1 = require("./events.js");
15
17
  const transport_js_1 = require("./transport.js");
18
+ const webhook_js_1 = require("./webhook.js");
19
+ const webhookSecrets_js_1 = require("./webhookSecrets.js");
20
+ const trigger_js_1 = require("./trigger.js");
16
21
  const PAGE = 100;
22
+ /** A trigger row the server paused after failed turns has a `pausedReason` starting with this (PAUSED_AUTO_PREFIX). */
23
+ const AUTO_PAUSED = "auto_paused";
24
+ /** `${agentId} v${version}` of each deploy this process sent that answered 200. A 200 deploy ran the trigger
25
+ * reconcile, or the server skipped it (the gateway reads no `agent.json`): checkTriggers() names that cause. */
26
+ const deployed = new Set();
27
+ /** `${agentId} v${version}` checkTriggers() has warned about, so an unchanged deploy warns once per process. */
28
+ const warned = new Set();
29
+ /** @internal Forget this process's deploys and warnings (tests: a new process). */
30
+ function forgetDeploys() {
31
+ deployed.clear();
32
+ warned.clear();
33
+ }
17
34
  /** A post the server is still delivering (409 in_progress) is retried this many times, Retry-After apart. */
18
35
  const IN_PROGRESS_RETRIES = 5;
19
36
  /** A post the server could not deliver to the agent (503; nothing was kept) is re-posted this many times,
@@ -197,6 +214,47 @@ function withoutWebhooks(settings) {
197
214
  const { sdk_hash: _hash, ...metadata } = settings["metadata"];
198
215
  return { ...out, metadata };
199
216
  }
217
+ function hooksOf(settings) {
218
+ const hooks = settings["webhooks"];
219
+ return Array.isArray(hooks) ? hooks.filter((h) => typeof h === "object" && h !== null && !Array.isArray(h)) : [];
220
+ }
221
+ function sdkNamed(agentId, hook) {
222
+ const url = hook["url"];
223
+ return typeof url === "string" && (0, webhook_js_1.urlProblem)(url) === null &&
224
+ hook["secret_credential"] === (0, webhook_js_1.secretCredentialName)(agentId, url);
225
+ }
226
+ /** `settings` as a definition writes them: a webhook whose `secret_credential` is the one `deploy` filled in loses it
227
+ * again, so the stored `sdk_hash` (computed without it) still matches. */
228
+ function asDefined(agentId, settings) {
229
+ if (!hooksOf(settings).some((h) => sdkNamed(agentId, h)))
230
+ return settings;
231
+ return {
232
+ ...settings,
233
+ webhooks: settings["webhooks"].map((h) => {
234
+ if (typeof h !== "object" || h === null || !sdkNamed(agentId, h))
235
+ return h;
236
+ const { secret_credential: _name, ...rest } = h;
237
+ return rest;
238
+ }),
239
+ };
240
+ }
241
+ /** The deploy response's `triggerTokens` (the server shows each token once); never logged. An entry missing a field
242
+ * is skipped with a warning that names the trigger, never the token. */
243
+ function triggerTokens(resource) {
244
+ var _a, _b;
245
+ const out = [];
246
+ for (const t of (_a = resource === null || resource === void 0 ? void 0 : resource.triggerTokens) !== null && _a !== void 0 ? _a : []) {
247
+ const ok = [t === null || t === void 0 ? void 0 : t.name, t === null || t === void 0 ? void 0 : t.triggerId, t === null || t === void 0 ? void 0 : t.url, t === null || t === void 0 ? void 0 : t.token].every((f) => typeof f === "string" && f !== "");
248
+ if (!ok) {
249
+ console.warn(`the deploy response has a trigger token entry without a name, id, url or token (trigger ` +
250
+ `${JSON.stringify((_b = t === null || t === void 0 ? void 0 : t.name) !== null && _b !== void 0 ? _b : null)}); skipped it. Rotate that trigger's token ` +
251
+ "(client.triggers.rotateSecret) to get one");
252
+ continue;
253
+ }
254
+ out.push((0, trigger_js_1.triggerToken)(t.name, t.triggerId, t.url, t.token));
255
+ }
256
+ return out;
257
+ }
200
258
  function isNotFound(err) {
201
259
  return err instanceof runtime_js_1.ResponseError && err.response.status === 404;
202
260
  }
@@ -234,6 +292,8 @@ class SettingsTransport {
234
292
  this.conversations = new ConversationsApi_js_1.ConversationsApi(config);
235
293
  this.responses = new ResponsesApi_js_1.ResponsesApi(config);
236
294
  this.customApis = new CustomAPIsApi_js_1.CustomAPIsApi(config);
295
+ this.triggers = new TriggersApi_js_1.TriggersApi(config);
296
+ this.secrets = (0, webhookSecrets_js_1.secretBackend)({ customApis: this.customApis });
237
297
  }
238
298
  // -- agents ---------------------------------------------------------------------------------------------
239
299
  async findAgents(sdkName) {
@@ -262,8 +322,9 @@ class SettingsTransport {
262
322
  return this.remote(resource, await this.instructions(agentId));
263
323
  }
264
324
  async createAgent(agent) {
265
- var _a, _b, _c;
266
- const settings = agent.toSettings();
325
+ var _a, _b, _c, _d;
326
+ let settings = agent.toSettings();
327
+ let missing = [];
267
328
  const webhooks = ((_a = settings["webhooks"]) !== null && _a !== void 0 ? _a : []);
268
329
  // The server checks webhooks against the credentials bound to the agent, and a new agent has none: create it
269
330
  // without them, bind the Custom APIs they name, then write the settings in full.
@@ -298,6 +359,7 @@ class SettingsTransport {
298
359
  if (webhooks.length > 0) {
299
360
  for (const { path, name, customApiId } of bind)
300
361
  await this.bind(resource.id, path, name, customApiId);
362
+ ({ settings, missing } = await this.withSecretCredentials(resource.id, settings));
301
363
  try {
302
364
  envelope = await this.agents.putAgentSettings({ agentId: resource.id, body: settings });
303
365
  }
@@ -329,9 +391,33 @@ class SettingsTransport {
329
391
  agentId: resource.id,
330
392
  name: agent.name,
331
393
  instructions: agent.instructions,
332
- settings: { ...((_c = envelope.settings) !== null && _c !== void 0 ? _c : {}) },
394
+ settings: asDefined(resource.id, { ...((_c = envelope.settings) !== null && _c !== void 0 ? _c : {}) }),
333
395
  warnings,
334
- }, live, versionStateOf(resource));
396
+ ...(missing.length > 0 ? { webhooksMissingSecret: missing } : {}),
397
+ triggerTokens: (_d = live.triggerTokens) !== null && _d !== void 0 ? _d : [],
398
+ }, live.liveVersion, versionStateOf(resource));
399
+ }
400
+ /** `settings` with each webhook that names no `secret_credential` given the one `client.webhooks` keeps for the
401
+ * agent and host (stored, with a random secret nobody holds, when there is none yet), and the URLs of those
402
+ * webhooks still without a secret set by `client.webhooks`. */
403
+ async withSecretCredentials(agentId, settings) {
404
+ const hooks = hooksOf(settings);
405
+ if (hooks.every((h) => "secret_credential" in h))
406
+ return { settings, missing: [] };
407
+ const filled = [];
408
+ const missing = [];
409
+ for (const h of settings["webhooks"]) {
410
+ if (typeof h !== "object" || h === null || "secret_credential" in h) {
411
+ filled.push(h);
412
+ continue;
413
+ }
414
+ const url = h["url"];
415
+ const { name, status } = await this.secrets.ensure(agentId, url);
416
+ filled.push({ ...h, secret_credential: name });
417
+ if (status === "missing")
418
+ missing.push(url);
419
+ }
420
+ return { settings: { ...settings, webhooks: filled }, missing };
335
421
  }
336
422
  /** The caller's Custom APIs the webhooks name, to bind to a new agent: one entry per name that matches exactly
337
423
  * one, and a sentence per name that matches none or several (left to the server, which refuses it only when it
@@ -342,8 +428,8 @@ class SettingsTransport {
342
428
  const seen = new Set();
343
429
  for (const [i, hook] of webhooks.entries()) {
344
430
  const name = hook.secret_credential;
345
- if (seen.has(name))
346
- continue;
431
+ if (name === undefined || seen.has(name))
432
+ continue; // left out: deploy fills in the one client.webhooks keeps
347
433
  seen.add(name);
348
434
  const path = `webhooks.${i}.secret_credential`;
349
435
  const ids = await this.customApiIds(name);
@@ -391,8 +477,8 @@ class SettingsTransport {
391
477
  }
392
478
  }
393
479
  async updateAgent(agentId, agent) {
394
- var _a, _b;
395
- const settings = agent.toSettings();
480
+ var _a, _b, _c;
481
+ let settings = agent.toSettings();
396
482
  // findAgents searches by display name, so an adopted (or dashboard-renamed) agent must carry sdk_name.
397
483
  const current = await this.agents.getAgent({ agentId });
398
484
  if (current.name !== agent.name) {
@@ -415,6 +501,15 @@ class SettingsTransport {
415
501
  if (instructionsChanged) {
416
502
  await this.agents.putInstructions({ agentId, instructionsBody: { data: agent.instructions } });
417
503
  }
504
+ let missing;
505
+ try {
506
+ ({ settings, missing } = await this.withSecretCredentials(agentId, settings));
507
+ }
508
+ catch (err) {
509
+ if (instructionsChanged)
510
+ await this.restore(agentId, null, previousInstructions);
511
+ throw err;
512
+ }
418
513
  let envelope;
419
514
  try {
420
515
  envelope = await this.agents.putAgentSettings({ agentId, body: settings });
@@ -446,21 +541,24 @@ class SettingsTransport {
446
541
  agentId,
447
542
  name: agent.name,
448
543
  instructions: agent.instructions,
449
- settings: { ...((_b = envelope.settings) !== null && _b !== void 0 ? _b : {}) },
544
+ settings: asDefined(agentId, { ...((_b = envelope.settings) !== null && _b !== void 0 ? _b : {}) }),
450
545
  warnings,
451
- }, live, versionStateOf(current));
546
+ ...(missing.length > 0 ? { webhooksMissingSecret: missing } : {}),
547
+ triggerTokens: (_c = live.triggerTokens) !== null && _c !== void 0 ? _c : [],
548
+ }, live.liveVersion, versionStateOf(current));
452
549
  }
453
550
  /** Make what was just written what end users get. A server without versions (no `versionState`) gets the bodiless
454
551
  * deploy 0.3 sent; otherwise the draft is saved as a version and that version deployed. */
455
552
  async goLive(agentId, versionState, agent) {
456
553
  if (versionState === undefined) {
457
- await this.agents.deployAgent({ agentId }); // idempotent; moves an adopted draft to deployed
458
- return undefined;
554
+ const resource = await this.agents.deployAgent({ agentId }); // idempotent; moves an adopted draft to deployed
555
+ return { triggerTokens: triggerTokens(resource) };
459
556
  }
460
557
  return this.publish(agentId, transport_js_1.VERSION_NOTE);
461
558
  }
462
559
  /**
463
- * Save the draft as the next version and make it live. 409 NO_CHANGES means the newest saved version already is the
560
+ * Save the draft as the next version and make it live; resolves to the live version and the bearer tokens the
561
+ * deploy's trigger reconcile minted (shown once). 409 NO_CHANGES means the newest saved version already is the
464
562
  * draft, so that version is deployed, by its number (never "the newest", which a save landing in between could
465
563
  * change).
466
564
  */
@@ -485,7 +583,73 @@ class SettingsTransport {
485
583
  catch (err) {
486
584
  throw (_a = (await missingDependencies(err, agentId))) !== null && _a !== void 0 ? _a : err;
487
585
  }
488
- return (_b = resource.liveVersion) !== null && _b !== void 0 ? _b : undefined;
586
+ const live = (_b = resource.liveVersion) !== null && _b !== void 0 ? _b : undefined;
587
+ if (live !== undefined)
588
+ deployed.add(`${agentId} v${live}`);
589
+ return live === undefined ? { triggerTokens: triggerTokens(resource) }
590
+ : { liveVersion: live, triggerTokens: triggerTokens(resource) };
591
+ }
592
+ /** Read the trigger rows of an unchanged live agent and warn, once per process, when they do not match
593
+ * `agent.triggers`. Never deploys: an unchanged definition writes nothing, and the rows may differ because the
594
+ * owner deleted, disabled or rolled back triggers in the dashboard, which a deploy would undo. A deploy whose
595
+ * trigger reconcile failed (502) went live without them; the warning names the fix, deploying the live version
596
+ * again. A failing read is ignored and never fails the deploy. */
597
+ async checkTriggers(agentId, agent, liveVersion) {
598
+ if (liveVersion === undefined)
599
+ return;
600
+ let mismatch;
601
+ try {
602
+ mismatch = await this.triggerMismatch(agentId, agent);
603
+ }
604
+ catch (_a) {
605
+ return; // a read only to warn; the deploy itself is done
606
+ }
607
+ if (mismatch === null)
608
+ return;
609
+ const key = `${agentId} v${liveVersion}`;
610
+ if (warned.has(key))
611
+ return;
612
+ warned.add(key);
613
+ const fix = `AgentsApi.deployAgent({ agentId, agentDeploy: { version: ${liveVersion} } })`;
614
+ if (mismatch.paused.length > 0) {
615
+ console.warn(`agent ${agentId}: its triggers do not match its definition (${mismatch.which}). deploy() writes ` +
616
+ `nothing for an unchanged definition; a deploy would also clear the auto-pause of ` +
617
+ `${mismatch.paused.join(", ")}. Once what fails their turns is fixed, deploy v${liveVersion} again (${fix}) ` +
618
+ `or enable each (TriggersApi.enableTrigger)`);
619
+ }
620
+ else if (deployed.has(key)) {
621
+ console.warn(`agent ${agentId}: v${liveVersion} is deployed, but the server did not create its triggers ` +
622
+ `(${mismatch.which}). It skips that while the gateway reads no agent.json (CLAW_AGENT_SETTINGS_ENABLED off ` +
623
+ `on the gateway). Once it is on, deploy v${liveVersion} again (${fix})`);
624
+ }
625
+ else {
626
+ console.warn(`agent ${agentId}: its triggers do not match its definition (${mismatch.which}): the trigger step ` +
627
+ `of the deploy that made v${liveVersion} live failed, or they were changed in the dashboard. deploy() writes ` +
628
+ `nothing for an unchanged definition; to create them, deploy v${liveVersion} again (${fix})`);
629
+ }
630
+ }
631
+ /** What differs and the auto-paused trigger names when the agent's trigger rows do not match `agent.triggers` (a
632
+ * trigger with no row, or a removed one still enabled), else null (also when the server has triggers switched
633
+ * off). */
634
+ async triggerMismatch(agentId, agent) {
635
+ var _a;
636
+ let rows;
637
+ try {
638
+ rows = (await this.triggers.listAgentTriggers({ agentId })).items;
639
+ }
640
+ catch (err) {
641
+ if (isNotFound(err))
642
+ return null; // triggers switched off on the server
643
+ throw err;
644
+ }
645
+ const names = new Set(((_a = agent.triggers) !== null && _a !== void 0 ? _a : []).map((t) => t.name));
646
+ const have = new Set(rows.map((r) => r.name));
647
+ const missing = [...names].filter((n) => !have.has(n)).sort();
648
+ const stale = rows.filter((r) => !names.has(r.name) && r.enabled).map((r) => r.name).sort();
649
+ if (missing.length === 0 && stale.length === 0)
650
+ return null;
651
+ const paused = rows.filter((r) => { var _a; return String((_a = r.pausedReason) !== null && _a !== void 0 ? _a : "").startsWith(AUTO_PAUSED); }).map((r) => r.name).sort();
652
+ return { which: `missing: ${missing.join(", ") || "none"}; still enabled: ${stale.join(", ") || "none"}`, paused };
489
653
  }
490
654
  async deleteAgent(agentId) {
491
655
  await this.agents.deleteAgent({ agentId });
@@ -664,7 +828,8 @@ class SettingsTransport {
664
828
  remote(resource, instructions) {
665
829
  var _a;
666
830
  const remote = {
667
- agentId: resource.id, name: resource.name, instructions, settings: { ...((_a = resource.settings) !== null && _a !== void 0 ? _a : {}) }, warnings: [],
831
+ agentId: resource.id, name: resource.name, instructions, warnings: [],
832
+ settings: asDefined(resource.id, { ...((_a = resource.settings) !== null && _a !== void 0 ? _a : {}) }),
668
833
  };
669
834
  // Left out, not set to undefined, when the server has no versions: the shape a 0.3 transport returned.
670
835
  if (resource.liveVersion !== undefined && resource.liveVersion !== null)
@@ -3,6 +3,7 @@ import type { Agent } from "./agent.js";
3
3
  import type { OutputType } from "./output.js";
4
4
  import type { Event, FunctionCallRequest } from "./events.js";
5
5
  import type { FunctionResult } from "./functionTool.js";
6
+ import type { MintedTriggerToken } from "./trigger.js";
6
7
  export interface SettingsWarning {
7
8
  code: string;
8
9
  path: string;
@@ -20,6 +21,16 @@ export interface RemoteAgent {
20
21
  /** `"live"`, `"paused"` or `"not_live"` (`AgentResource.versionState`); undefined when the server has no versions,
21
22
  * in which case deploy() never calls `publish`. */
22
23
  versionState?: string;
24
+ /** URLs of the webhooks this write left without a secret set by `client.webhooks` (a placeholder secret nobody
25
+ * holds stands in until `client.webhooks.setSecret`); left out when there are none. */
26
+ webhooksMissingSecret?: string[];
27
+ /** The bearer tokens the write's deploy minted (the server shows each once). */
28
+ triggerTokens?: MintedTriggerToken[];
29
+ }
30
+ /** What `Transport.publish` made live: the live version, and the bearer tokens that deploy minted (shown once). */
31
+ export interface Published {
32
+ liveVersion?: number;
33
+ triggerTokens?: MintedTriggerToken[];
23
34
  }
24
35
  /** The note on every version deploy() saves, so the dashboard's version list shows which came from code. */
25
36
  export declare const VERSION_NOTE = "Deployed from the SDK";
@@ -45,9 +56,14 @@ export interface Transport {
45
56
  createAgent(agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
46
57
  updateAgent(agentId: string, agent: Agent<OutputType | undefined>): Promise<RemoteAgent>;
47
58
  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>;
59
+ /** Save the draft as the next version and make that version live; resolves to the live version, or a `Published`
60
+ * that also carries the trigger tokens the deploy minted. Called only for an agent whose `versionState` is set.
61
+ * Optional so a transport written before versions still type-checks. */
62
+ publish?(agentId: string, note: string): Promise<number | undefined | Published>;
63
+ /** Optional: deploy() calls it for an unchanged live agent. It only reads: when the trigger rows do not match
64
+ * `agent.triggers` (a deploy whose trigger reconcile failed went live without them, or the owner changed them) it
65
+ * warns, naming the fix. It never deploys: an unchanged definition writes nothing. */
66
+ checkTriggers?(agentId: string, agent: Agent<OutputType | undefined>, liveVersion?: number): Promise<void>;
51
67
  startTurn(agentId: string, conversationId: string | null, text: string, signal?: AbortSignal): Promise<TurnStream>;
52
68
  answerClarify(conversationId: string, requestId: string, answers: Record<string, unknown>, signal?: AbortSignal): Promise<TurnStream>;
53
69
  resume(conversationId: string, responseId: string, lastEventId: string | null, signal?: AbortSignal): Promise<TurnStream>;