@parcha/agentrun-serve 0.1.0-beta.45 → 0.1.0-beta.47
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/CHANGELOG.md +180 -0
- package/NOTICE +4 -0
- package/README.md +283 -47
- package/dist/acp-package.d.ts +35 -6
- package/dist/acp-package.js +3 -3
- package/dist/adapter-facts.d.ts +5 -2
- package/dist/adapter-facts.js +6 -3
- package/dist/adapters.d.ts +7 -0
- package/dist/adapters.js +39 -0
- package/dist/agents-store.d.ts +5 -3
- package/dist/agents-store.js +13 -8
- package/dist/api-only.d.ts +7 -10
- package/dist/api-only.js +7 -10
- package/dist/archil-routes.js +2 -1
- package/dist/cap.js +12 -2
- package/dist/chatgpt-plan.js +2 -1
- package/dist/cli.js +119 -44
- package/dist/composio-made.d.ts +19 -2
- package/dist/composio-made.js +17 -5
- package/dist/composio-preset.d.ts +42 -7
- package/dist/composio-preset.js +106 -70
- package/dist/composio-triggers.d.ts +9 -4
- package/dist/composio-triggers.js +20 -13
- package/dist/computer-attach.d.ts +11 -10
- package/dist/computer-attach.js +13 -14
- package/dist/computer-commands.d.ts +3 -0
- package/dist/computer-commands.js +3 -2
- package/dist/computer-lost.d.ts +4 -0
- package/dist/computer-lost.js +8 -3
- package/dist/computer-setting.d.ts +2 -0
- package/dist/computer-setting.js +2 -0
- package/dist/computers.d.ts +9 -75
- package/dist/computers.js +20 -66
- package/dist/console/assets/index-BBFzAxLB.js +715 -0
- package/dist/console/assets/index-BRi-Rgq6.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/cost-heal.d.ts +13 -0
- package/dist/cost-heal.js +27 -0
- package/dist/dial-relay.d.ts +6 -2
- package/dist/dial-relay.js +9 -12
- package/dist/email/routes.js +2 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +5 -0
- package/dist/exa-keyless.d.ts +1 -1
- package/dist/exa-keyless.js +1 -1
- package/dist/faux-computers.d.ts +2 -3
- package/dist/faux-computers.js +9 -31
- package/dist/github-connection.js +2 -1
- package/dist/harness-archive.d.ts +19 -2
- package/dist/harness-archive.js +49 -15
- package/dist/harness-check.d.ts +73 -0
- package/dist/harness-check.js +489 -0
- package/dist/harness-cursor.d.ts +45 -0
- package/dist/harness-cursor.js +121 -0
- package/dist/harness-recipe.d.ts +127 -0
- package/dist/harness-recipe.js +357 -0
- package/dist/harness-table.d.ts +12 -0
- package/dist/harness-table.js +62 -2
- package/dist/harness.d.ts +2 -0
- package/dist/harness.js +3 -2
- package/dist/host-check.d.ts +6 -0
- package/dist/host-check.js +14 -0
- package/dist/host.d.ts +10 -3
- package/dist/host.js +17 -3
- package/dist/import-sources.d.ts +2 -0
- package/dist/import-sources.js +2 -0
- package/dist/import-summary.d.ts +15 -5
- package/dist/import-summary.js +24 -8
- package/dist/import.d.ts +34 -4
- package/dist/import.js +134 -32
- package/dist/key-refusal.d.ts +1 -0
- package/dist/key-refusal.js +8 -3
- package/dist/key-words.d.ts +1 -1
- package/dist/key-words.js +4 -4
- package/dist/learn/check.d.ts +3 -0
- package/dist/learn/check.js +31 -20
- package/dist/learn/compile.js +11 -4
- package/dist/learn/promote.d.ts +77 -4
- package/dist/learn/promote.js +123 -2
- package/dist/learned-nodes.d.ts +4 -3
- package/dist/learned-workflow.js +4 -1
- package/dist/learning-cost.d.ts +6 -16
- package/dist/learning-cost.js +6 -12
- package/dist/learning-setting.d.ts +17 -6
- package/dist/learning-setting.js +41 -5
- package/dist/learning-words.d.ts +2 -0
- package/dist/learning-words.js +1 -1
- package/dist/listen-first.d.ts +10 -0
- package/dist/listen-first.js +25 -1
- package/dist/llms.txt +1 -1
- package/dist/mcp-gateway.d.ts +2 -1
- package/dist/mcp-gateway.js +31 -15
- package/dist/mcp-probe.d.ts +18 -6
- package/dist/mcp-probe.js +41 -8
- package/dist/mcp-secret-writes.d.ts +8 -0
- package/dist/mcp-secret-writes.js +104 -4
- package/dist/mcp-servers-store.d.ts +11 -5
- package/dist/mcp-servers-store.js +46 -20
- package/dist/model-form.d.ts +2 -1
- package/dist/model-form.js +22 -8
- package/dist/model-gateway.d.ts +113 -0
- package/dist/model-gateway.js +433 -0
- package/dist/model.d.ts +2 -1
- package/dist/model.js +9 -7
- package/dist/monid.js +1 -1
- package/dist/native/binding.d.ts +4 -0
- package/dist/native/binding.js +9 -0
- package/dist/native/cli.d.ts +3 -2
- package/dist/native/cli.js +6 -5
- package/dist/openapi.d.ts +2 -0
- package/dist/openapi.js +326 -41
- package/dist/owner.d.ts +4 -2
- package/dist/owner.js +10 -4
- package/dist/page.d.ts +8 -0
- package/dist/page.js +9 -1
- package/dist/port-models.js +17 -10
- package/dist/provider.d.ts +3 -3
- package/dist/provider.js +12 -3
- package/dist/public-address.d.ts +28 -0
- package/dist/public-address.js +62 -0
- package/dist/run-launch.d.ts +1 -1
- package/dist/run-launch.js +1 -1
- package/dist/run-secrets.d.ts +3 -2
- package/dist/run-secrets.js +6 -4
- package/dist/runner/adapter-loop.d.ts +10 -7
- package/dist/runner/adapter-loop.js +57 -22
- package/dist/runner/adapter-run-spec.d.ts +12 -0
- package/dist/runner/adapter-run-spec.js +40 -6
- package/dist/runner/adapters/index.js +16 -0
- package/dist/runner/adapters/pi.d.ts +5 -2
- package/dist/runner/adapters/pi.js +7 -4
- package/dist/runner/audit.d.ts +7 -0
- package/dist/runner/audit.js +24 -6
- package/dist/runner/claim-secrets.js +5 -1
- package/dist/runner/cli-run-spec.js +15 -7
- package/dist/runner/compare-end.d.ts +43 -0
- package/dist/runner/compare-end.js +95 -0
- package/dist/runner/computer-journal.d.ts +3 -0
- package/dist/runner/computer-journal.js +22 -2
- package/dist/runner/conformance/harness.js +19 -8
- package/dist/runner/conformance/subject.d.ts +6 -0
- package/dist/runner/harness-loops.d.ts +6 -1
- package/dist/runner/harness-loops.js +94 -54
- package/dist/runner/held.d.ts +5 -4
- package/dist/runner/journal-kinds.d.ts +4 -1
- package/dist/runner/journal-kinds.js +3 -0
- package/dist/runner/loop-contract.d.ts +10 -0
- package/dist/runner/main.d.ts +5 -5
- package/dist/runner/main.js +81 -11
- package/dist/runner/mcp-claim.js +40 -12
- package/dist/runner/metrics.d.ts +26 -0
- package/dist/runner/metrics.js +44 -1
- package/dist/runner/presence.d.ts +41 -0
- package/dist/runner/presence.js +89 -0
- package/dist/runner/promotion.d.ts +10 -3
- package/dist/runner/promotion.js +42 -21
- package/dist/runner/run-child-host.js +52 -14
- package/dist/runner/runner.d.ts +5 -0
- package/dist/runner/runner.js +8 -0
- package/dist/runner/store-port.d.ts +3 -0
- package/dist/runner/store-secrets.js +19 -7
- package/dist/runner/workflow-first.d.ts +5 -2
- package/dist/runner/workflow-first.js +6 -3
- package/dist/runs-over-store.d.ts +6 -2
- package/dist/runs-over-store.js +14 -4
- package/dist/sdk/runs.d.ts +1 -1
- package/dist/sdk/runs.js +6 -1
- package/dist/sdk/schema.js +1 -1
- package/dist/secret-writes.d.ts +16 -3
- package/dist/secret-writes.js +28 -3
- package/dist/server-secrets-store.js +5 -0
- package/dist/server.d.ts +31 -2
- package/dist/server.js +326 -53
- package/dist/sign-ins-secrets.js +4 -1
- package/dist/skills/agentrun-serve/SKILL.md +11 -0
- package/dist/slack-url.d.ts +5 -0
- package/dist/slack-url.js +11 -0
- package/dist/slack.d.ts +107 -0
- package/dist/slack.js +292 -0
- package/dist/start-switches.d.ts +28 -0
- package/dist/start-switches.js +37 -0
- package/dist/stats.d.ts +1 -1
- package/dist/stats.js +2 -1
- package/dist/stop-signals.d.ts +11 -0
- package/dist/stop-signals.js +22 -0
- package/dist/store/agent-body.d.ts +1 -1
- package/dist/store/agent-body.js +2 -2
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +1 -1
- package/dist/store/postgres/migrations/0020_runners.sql +18 -0
- package/dist/store/postgres/store.d.ts +1 -1
- package/dist/store/postgres/store.js +4 -2
- package/dist/store/schema.d.ts +13 -5
- package/dist/store/schema.js +20 -11
- package/dist/store/seal.d.ts +16 -4
- package/dist/store/sqlite/store.d.ts +1 -1
- package/dist/store/sqlite/store.js +106 -27
- package/dist/store/store.d.ts +75 -23
- package/dist/store/store.js +11 -0
- package/dist/store-reads/analytics.js +3 -2
- package/dist/store-reads/cap.d.ts +7 -0
- package/dist/store-reads/cap.js +11 -1
- package/dist/store-reads/journal.js +27 -1
- package/dist/store-reads/learning.d.ts +23 -2
- package/dist/store-reads/learning.js +29 -8
- package/dist/store-reads/runs.d.ts +9 -0
- package/dist/store-reads/runs.js +19 -6
- package/dist/store-reads/stats.d.ts +4 -1
- package/dist/store-reads/stats.js +15 -6
- package/dist/store-view.d.ts +123 -14
- package/dist/store-view.js +154 -21
- package/dist/telemetry.d.ts +7 -7
- package/dist/telemetry.js +38 -15
- package/dist/templates.d.ts +6 -3
- package/dist/templates.js +38 -3
- package/dist/timeline/timeline.js +18 -0
- package/dist/tool-keys-store.js +2 -2
- package/dist/tool-keys.js +2 -1
- package/dist/tool-plane.js +2 -2
- package/dist/trigger-adapter.d.ts +3 -4
- package/dist/trigger-adapter.js +2 -2
- package/dist/trigger-composio.js +3 -3
- package/dist/trigger-email.js +0 -2
- package/dist/trigger-feed.js +0 -2
- package/dist/trigger-schedule.js +3 -0
- package/dist/trigger-secrets-shown.d.ts +6 -3
- package/dist/trigger-secrets-shown.js +22 -13
- package/dist/trigger-targets.d.ts +1 -1
- package/dist/trigger-targets.js +19 -8
- package/dist/triggers.d.ts +7 -3
- package/dist/triggers.js +2 -1
- package/dist/webhooks.d.ts +85 -9
- package/dist/webhooks.js +129 -46
- package/dist/wires.d.ts +6 -1
- package/dist/wires.js +5 -0
- package/dist/workspace/attach.d.ts +3 -2
- package/dist/workspace/attach.js +2 -1
- package/dist/workspace/attached-computers.d.ts +6 -8
- package/dist/workspace/attached-computers.js +27 -24
- package/dist/workspace/computer-deps.d.ts +2 -3
- package/dist/workspace/computer-deps.js +3 -6
- package/dist/workspace/computer-for-claim.js +72 -96
- package/dist/workspace/computer-kept.d.ts +0 -7
- package/dist/workspace/computer-kept.js +0 -17
- package/dist/workspace/resolve-computer.d.ts +18 -14
- package/dist/workspace/resolve-computer.js +47 -60
- package/dist/workspace/synced-folder.d.ts +20 -15
- package/dist/workspace/synced-folder.js +33 -43
- package/launcher/agentrun-serve +4 -1
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +11 -0
- package/templates/README.md +1 -1
- package/templates/inbound-lead/agent.md +2 -1
- package/templates/kyb/agent.md +2 -1
- package/dist/console/assets/index-3O2Yl6hZ.js +0 -701
- package/dist/console/assets/index-DaFsZuzQ.css +0 -1
package/dist/webhooks.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { fetch as undiciFetch } from "undici";
|
|
2
|
+
import { type ErrorCode } from "./errors.js";
|
|
3
|
+
import { type SealedSettingsOverStore, type SettingsSpec } from "./sealed-settings-store.js";
|
|
1
4
|
import type { Store } from "./store/index.js";
|
|
2
5
|
import type { TriggerFailed } from "./triggers.js";
|
|
3
6
|
/** A run's two notices, which the server's run-change follower tells. */
|
|
@@ -27,6 +30,31 @@ export declare const WEBHOOK_SECRET_SPEC: {
|
|
|
27
30
|
};
|
|
28
31
|
};
|
|
29
32
|
};
|
|
33
|
+
/** Where an outbox keeps what it sends and what it says, beside the webhook's own: its channel in the store, its setting,
|
|
34
|
+
* the sealed field that holds its credential (the webhook's minted secret; Slack's URL, slack.ts), its test message's
|
|
35
|
+
* type, and its words. */
|
|
36
|
+
export type Outbound = {
|
|
37
|
+
channel: string;
|
|
38
|
+
setting: string;
|
|
39
|
+
spec: SettingsSpec;
|
|
40
|
+
field: string;
|
|
41
|
+
testType: string;
|
|
42
|
+
words: {
|
|
43
|
+
removed: string;
|
|
44
|
+
off: string;
|
|
45
|
+
unset: string;
|
|
46
|
+
offWith: (reason: string) => string;
|
|
47
|
+
unreadable: string;
|
|
48
|
+
};
|
|
49
|
+
offCode: ErrorCode;
|
|
50
|
+
};
|
|
51
|
+
/** The webhook's own: every message type and word as they always were. */
|
|
52
|
+
export declare const WEBHOOK_OUTBOUND: Outbound;
|
|
53
|
+
/** What a receiver's answer turns off: the reason the outbox keeps, and the words its queued messages are dropped with. */
|
|
54
|
+
export type Gone = {
|
|
55
|
+
reason: string;
|
|
56
|
+
drop: string;
|
|
57
|
+
};
|
|
30
58
|
/** The webhook's URL as the server shows it (GET, its log): its origin and the last four characters of its path, never
|
|
31
59
|
* the whole URL, which can be a credential itself (a chat service's incoming-hook URL). */
|
|
32
60
|
export declare function shownUrl(url: string): string;
|
|
@@ -46,16 +74,32 @@ export type WebhooksOptions = {
|
|
|
46
74
|
* refused the write, so the caller tells it again; `off`, no webhook set, or one a 410 turned off, so nothing is to be
|
|
47
75
|
* sent; `closed`, the server is stopping. */
|
|
48
76
|
export type Emitted = "queued" | "unsaved" | "off" | "closed";
|
|
49
|
-
/** What a notice says of its run: never its tools' arguments, its output or its input, which a receiver reads through the API.
|
|
77
|
+
/** What a notice says of its run: never its tools' arguments, its output or its input, which a receiver reads through the API.
|
|
78
|
+
* `agent_name`: its agent's name as the notice is built (server.ts agentNameOf), its id when the agent has no name. */
|
|
50
79
|
export type RunNotice = {
|
|
51
80
|
run_id: string;
|
|
52
81
|
agent_id: string;
|
|
82
|
+
agent_name: string;
|
|
53
83
|
status: string;
|
|
54
84
|
title: string;
|
|
55
85
|
pending?: {
|
|
56
86
|
kind: "question" | "approval" | "stopped" | "budget" | "browser" | "computer";
|
|
57
87
|
};
|
|
58
88
|
};
|
|
89
|
+
/** The webhook as the store keeps it, but its secret: `secret_at`, when the secret was made (the same URL keeps it);
|
|
90
|
+
* `secret_scope`, the scope its secret is sealed under in trigger_settings, one of its own for each secret made, so the
|
|
91
|
+
* setting names the one secret that signs for its URL, and the two go live in the setting's one write. */
|
|
92
|
+
export type OutboundConfig = Config;
|
|
93
|
+
type Config = {
|
|
94
|
+
url: string;
|
|
95
|
+
secret_at: string;
|
|
96
|
+
secret_scope: string;
|
|
97
|
+
updated_at: string;
|
|
98
|
+
disabled?: {
|
|
99
|
+
at: string;
|
|
100
|
+
reason: string;
|
|
101
|
+
};
|
|
102
|
+
};
|
|
59
103
|
export type Attempt = {
|
|
60
104
|
webhook_id: string;
|
|
61
105
|
type: MessageType;
|
|
@@ -90,16 +134,18 @@ export declare function waitMarkerOf(rows: readonly {
|
|
|
90
134
|
export type WebhooksStore = Pick<Store, "settings" | "triggerSettings" | "channels" | "flush">;
|
|
91
135
|
export declare class Webhooks {
|
|
92
136
|
private readonly store;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
137
|
+
protected readonly say: (fields: Record<string, string | number>) => void;
|
|
138
|
+
/** Which outbox this is: the webhook's (WEBHOOK_OUTBOUND), or another destination's over the same machinery. */
|
|
139
|
+
protected readonly outbound: Outbound;
|
|
140
|
+
protected readonly secrets: SealedSettingsOverStore;
|
|
141
|
+
protected config?: Config;
|
|
96
142
|
/** The ledger, by run, as channel_told holds it (this server is its one writer), and the waits seen while nothing is
|
|
97
143
|
* sent, held here only. */
|
|
98
144
|
private readonly notices;
|
|
99
145
|
/** The runs told of since this server started: their ledger entries are this server's news, which a start's catch-up
|
|
100
146
|
* never drops. */
|
|
101
147
|
private readonly told;
|
|
102
|
-
|
|
148
|
+
protected base: string;
|
|
103
149
|
private readonly timers;
|
|
104
150
|
/** The tries in flight, which close() stops and waits for: nothing is written once it is closed. */
|
|
105
151
|
private readonly trying;
|
|
@@ -127,7 +173,9 @@ export declare class Webhooks {
|
|
|
127
173
|
private readonly storeRetryMs;
|
|
128
174
|
private readonly timeoutMs;
|
|
129
175
|
/** `key` is the server's seal key; `say` takes a log line's fields. */
|
|
130
|
-
constructor(store: WebhooksStore, key: Buffer, say: (fields: Record<string, string | number>) => void, options?: WebhooksOptions
|
|
176
|
+
constructor(store: WebhooksStore, key: Buffer, say: (fields: Record<string, string | number>) => void, options?: WebhooksOptions,
|
|
177
|
+
/** Which outbox this is: the webhook's (WEBHOOK_OUTBOUND), or another destination's over the same machinery. */
|
|
178
|
+
outbound?: Outbound);
|
|
131
179
|
/** Reads the webhook, its secret and its ledger once, and goes on with the messages an earlier server left queued, each
|
|
132
180
|
* at its next try's time; a test send is tried once, so one left queued failed. Awaited once at start. */
|
|
133
181
|
load(): Promise<void>;
|
|
@@ -167,13 +215,23 @@ export declare class Webhooks {
|
|
|
167
215
|
}>;
|
|
168
216
|
/** The PUT's change, once every change before it has landed: the webhook as the last one left it, then the new one. */
|
|
169
217
|
private saveNow;
|
|
218
|
+
/** POST .../secret: a new secret for the URL set now, answered once; the webhook is otherwise as it was (its URL, when
|
|
219
|
+
* it was set, and off if a 410 turned it off). The secret it replaces goes at once, retired (still cut from what a run
|
|
220
|
+
* says): every try from then on, of a message queued before it too, is signed with the new one, since a try signs with
|
|
221
|
+
* the secret the setting names when it sends (tryOnce). A try already sending was signed with the old one. As a PUT
|
|
222
|
+
* does, the new secret is sealed under a scope of its own before the setting that names it is written, and a write
|
|
223
|
+
* that fails leaves the old secret signing, after a restart too. */
|
|
224
|
+
rotate(): Promise<{
|
|
225
|
+
view: ReturnType<Webhooks["view"]>;
|
|
226
|
+
secret: string;
|
|
227
|
+
}>;
|
|
170
228
|
/** DELETE: the webhook gone, its queued messages with it, and its secret (retired: still cut from what a run says); the
|
|
171
229
|
* log of attempts stays. In that order: shut to new notices, its queued messages dropped, the setting removed, then
|
|
172
230
|
* every secret of the webhook, on every DELETE (none once gone), so a removal of a secret that failed is finished by
|
|
173
231
|
* the next DELETE, and whatever fails, nothing of the removed webhook is left to send, to it or to a later one. */
|
|
174
232
|
remove(): Promise<void>;
|
|
175
233
|
/** `work`, once every change of the webhook asked for before it has settled, failed or not. */
|
|
176
|
-
|
|
234
|
+
protected change<T>(work: () => Promise<T>): Promise<T>;
|
|
177
235
|
/** GET .../deliveries: the last attempts, newest first. */
|
|
178
236
|
attemptsView(): Promise<{
|
|
179
237
|
attempts: Attempt[];
|
|
@@ -203,6 +261,10 @@ export declare class Webhooks {
|
|
|
203
261
|
endedAt?: string;
|
|
204
262
|
notice: RunNotice;
|
|
205
263
|
} | null>): Promise<string[]>;
|
|
264
|
+
/** A moment's message id, made from `key` (the same moment, told again by a later server too, has the same id). The
|
|
265
|
+
* store keeps one message per id across every channel, so another outbox's ids are made under its channel too: Slack's
|
|
266
|
+
* message for a run's end is never the webhook's, whichever is written first. The webhook's ids are as they always were. */
|
|
267
|
+
private messageId;
|
|
206
268
|
/** POST .../test: a `webhook.test` message tried once now, never again; its attempt. */
|
|
207
269
|
test(): Promise<Attempt>;
|
|
208
270
|
/** A message (and its ledger row, `told`) queued in one transaction, then made durable (the store's flush, as every write
|
|
@@ -226,9 +288,11 @@ export declare class Webhooks {
|
|
|
226
288
|
private signing;
|
|
227
289
|
/** The secret the webhook's setting names, opened here; undefined when none is set, or it does not open here. */
|
|
228
290
|
private signingSecret;
|
|
229
|
-
|
|
291
|
+
protected body(type: string, data: unknown): string;
|
|
230
292
|
/** The setting written, then held here: a write the store refuses throws, and what was held stays held. */
|
|
231
|
-
|
|
293
|
+
protected write(config: Config): Promise<void>;
|
|
294
|
+
/** The setting removed, durably, and none held here: a removal the store refuses throws, and what was held stays held. */
|
|
295
|
+
protected unwrite(): Promise<void>;
|
|
232
296
|
/** Whether an end's run.ended is still to be queued: at or after the webhook was set and the day began (a marker that
|
|
233
297
|
* names no time is not judged by one). The catch-up and the ledger's pruning read the same line, so the ledger never
|
|
234
298
|
* forgets an end a start would look for. */
|
|
@@ -242,6 +306,18 @@ export declare class Webhooks {
|
|
|
242
306
|
* next wait (`retry`), else dropped, and the log says so. A 410 turns the webhook off. A redelivery is a try of its
|
|
243
307
|
* own: it marks a message sent when it lands, and never drops one. */
|
|
244
308
|
private tryOnce;
|
|
309
|
+
/** What a try sends to (`url`) and with which headers, for the outbox's credential: the webhook's URL, signed with its
|
|
310
|
+
* secret. Slack's outbox sends to its sealed URL (slack.ts). */
|
|
311
|
+
protected request(config: Config, credential: string, id: string, timestamp: string, body: string): {
|
|
312
|
+
url: URL;
|
|
313
|
+
headers: Record<string, string>;
|
|
314
|
+
};
|
|
315
|
+
/** What a receiver's answer says: why the try failed (none when it landed), and whether the receiver is gone, which
|
|
316
|
+
* turns the outbox off. The webhook's: 2xx lands; a redirect is not followed; a 410 is gone. */
|
|
317
|
+
protected judged(res: Awaited<ReturnType<typeof undiciFetch>>): Promise<{
|
|
318
|
+
failure?: string;
|
|
319
|
+
gone?: Gone;
|
|
320
|
+
}>;
|
|
245
321
|
/** A try's outcome written: the message's new state and the try, in one transaction; its next try armed; a 410 that
|
|
246
322
|
* still names the webhook as it stands turns it off.
|
|
247
323
|
*
|
package/dist/webhooks.js
CHANGED
|
@@ -20,6 +20,7 @@ import { ApiError, invalid } from "./errors.js";
|
|
|
20
20
|
import { SERVER_SCOPE, sealedSettingsOverStore } from "./sealed-settings-store.js";
|
|
21
21
|
import { signatureOf } from "./standard-webhooks.js";
|
|
22
22
|
import { webhookSecretMeta } from "./trigger-secrets-shown.js";
|
|
23
|
+
import { restartToAllow, WEBHOOK_PRIVATE_TARGETS_ENV } from "./start-switches.js";
|
|
23
24
|
import { isObject, unknownFields } from "./validate.js";
|
|
24
25
|
/** A run's two notices, which the server's run-change follower tells. */
|
|
25
26
|
export const RUN_EVENTS = ["run.needs_input", "run.ended"];
|
|
@@ -48,13 +49,27 @@ const MAX_URL = 2048;
|
|
|
48
49
|
const SECRET_FIELD = "secret";
|
|
49
50
|
/** Where its secret is kept in trigger_settings (adapter WEBHOOK_CHANNEL): the server's one scope, one minted field. */
|
|
50
51
|
export const WEBHOOK_SECRET_SPEC = { scope: "server", fields: { [SECRET_FIELD]: { mint: true } } };
|
|
52
|
+
/** The webhook's own: every message type and word as they always were. */
|
|
53
|
+
export const WEBHOOK_OUTBOUND = {
|
|
54
|
+
channel: WEBHOOK_CHANNEL, setting: WEBHOOK_SETTING, spec: WEBHOOK_SECRET_SPEC, field: SECRET_FIELD, testType: "webhook.test",
|
|
55
|
+
words: {
|
|
56
|
+
removed: "the webhook was removed", off: "the webhook is off", unset: "no webhook is set: PUT /v2/settings/webhook sets one",
|
|
57
|
+
offWith: (reason) => `the webhook is off (${reason}): set it again with PUT /v2/settings/webhook`,
|
|
58
|
+
unreadable: "the secret does not open here (unreadable): set the webhook again",
|
|
59
|
+
},
|
|
60
|
+
offCode: "webhook_off",
|
|
61
|
+
};
|
|
51
62
|
/** How long a try's claim on its message holds (Store.channels.claim): past the try's own bound (TIMEOUT_MS, or the
|
|
52
63
|
* option's, which the constructor holds under this). */
|
|
53
64
|
const CLAIM_MS = 60_000;
|
|
54
65
|
/** Names that are this machine or its own network, whatever they resolve to. */
|
|
55
66
|
const LOCAL_NAME = /^(localhost|.*\.localhost|.*\.local|.*\.internal)$/i;
|
|
56
|
-
/**
|
|
57
|
-
const
|
|
67
|
+
/** How a refusal of a private network or this machine is lifted: the operator's switch, at a restart (start-switches.ts). */
|
|
68
|
+
const RESTART_WORDS = restartToAllow(WEBHOOK_PRIVATE_TARGETS_ENV);
|
|
69
|
+
/** The words a URL on a private network or this machine is refused in, at its PUT. */
|
|
70
|
+
const PRIVATE_WORDS = `url is on a private network or this machine, which this server does not send to; ${RESTART_WORDS}`;
|
|
71
|
+
/** The words a try refuses `host` in, when it or an address its name resolves to now is not public. */
|
|
72
|
+
const notPublic = (host) => `refused: ${host} is not a public host; ${RESTART_WORDS}`;
|
|
58
73
|
/** The webhook's URL as the server shows it (GET, its log): its origin and the last four characters of its path, never
|
|
59
74
|
* the whole URL, which can be a credential itself (a chat service's incoming-hook URL). */
|
|
60
75
|
export function shownUrl(url) {
|
|
@@ -116,6 +131,7 @@ const attemptView = (row) => ({
|
|
|
116
131
|
export class Webhooks {
|
|
117
132
|
store;
|
|
118
133
|
say;
|
|
134
|
+
outbound;
|
|
119
135
|
secrets;
|
|
120
136
|
config;
|
|
121
137
|
/** The ledger, by run, as channel_told holds it (this server is its one writer), and the waits seen while nothing is
|
|
@@ -152,14 +168,17 @@ export class Webhooks {
|
|
|
152
168
|
storeRetryMs;
|
|
153
169
|
timeoutMs;
|
|
154
170
|
/** `key` is the server's seal key; `say` takes a log line's fields. */
|
|
155
|
-
constructor(store, key, say, options = {}
|
|
171
|
+
constructor(store, key, say, options = {},
|
|
172
|
+
/** Which outbox this is: the webhook's (WEBHOOK_OUTBOUND), or another destination's over the same machinery. */
|
|
173
|
+
outbound = WEBHOOK_OUTBOUND) {
|
|
156
174
|
this.store = store;
|
|
157
175
|
this.say = say;
|
|
176
|
+
this.outbound = outbound;
|
|
158
177
|
this.timeoutMs = options.timeoutMs ?? TIMEOUT_MS;
|
|
159
178
|
// Whole milliseconds: AbortSignal.timeout takes no other, and would throw at every send, after its claim.
|
|
160
179
|
if (!(Number.isInteger(this.timeoutMs) && this.timeoutMs > 0 && this.timeoutMs < CLAIM_MS))
|
|
161
180
|
throw new RangeError(`a webhook try's bound is whole milliseconds between 0 and ${CLAIM_MS} (its claim), not ${this.timeoutMs}`);
|
|
162
|
-
this.secrets = sealedSettingsOverStore(store,
|
|
181
|
+
this.secrets = sealedSettingsOverStore(store, outbound.channel, outbound.spec, key, say);
|
|
163
182
|
this.retryMs = options.retryMs ?? RETRY_MS;
|
|
164
183
|
this.storeRetryMs = options.storeRetryMs ?? STORE_RETRY_MS;
|
|
165
184
|
this.privateTargets = options.privateTargets ?? false;
|
|
@@ -169,7 +188,7 @@ export class Webhooks {
|
|
|
169
188
|
* at its next try's time; a test send is tried once, so one left queued failed. Awaited once at start. */
|
|
170
189
|
async load() {
|
|
171
190
|
await this.secrets.load();
|
|
172
|
-
const kept = await this.store.settings.get(
|
|
191
|
+
const kept = await this.store.settings.get(this.outbound.setting);
|
|
173
192
|
this.config = isObject(kept) && typeof kept.url === "string" && typeof kept.secret_scope === "string" && typeof kept.updated_at === "string" ? kept : undefined;
|
|
174
193
|
// A secret no setting names (one made by a PUT whose setting never landed, or the one a later PUT replaced and could not
|
|
175
194
|
// remove) signs nothing: it goes, retired, still cut from what runs say.
|
|
@@ -177,7 +196,7 @@ export class Webhooks {
|
|
|
177
196
|
if (scope !== this.config?.secret_scope)
|
|
178
197
|
await this.secrets.remove(scope).catch((error) => this.say({ condition: "secret_unremoved", error: error.message }));
|
|
179
198
|
}
|
|
180
|
-
for (const row of await this.store.channels.told(
|
|
199
|
+
for (const row of await this.store.channels.told(this.outbound.channel)) {
|
|
181
200
|
const noticed = this.notices.get(row.runId) ?? {};
|
|
182
201
|
if (row.marker.startsWith(ENDED))
|
|
183
202
|
noticed.ended = row.marker.slice(ENDED.length);
|
|
@@ -186,9 +205,9 @@ export class Webhooks {
|
|
|
186
205
|
this.notices.set(row.runId, noticed);
|
|
187
206
|
}
|
|
188
207
|
// The store's one server (its lock): a claim an earlier server held is that server's try, cut by its stop.
|
|
189
|
-
await this.store.channels.release(
|
|
190
|
-
for (const post of await this.store.channels.queued(
|
|
191
|
-
if (post.moment ===
|
|
208
|
+
await this.store.channels.release(this.outbound.channel);
|
|
209
|
+
for (const post of await this.store.channels.queued(this.outbound.channel)) {
|
|
210
|
+
if (post.moment === this.outbound.testType)
|
|
192
211
|
await this.store.channels.settle(post.id, { state: "dropped", reason: "a test send is tried once" });
|
|
193
212
|
else
|
|
194
213
|
this.schedule(post.id, Math.max(0, post.nextAt - Date.now()));
|
|
@@ -256,6 +275,36 @@ export class Webhooks {
|
|
|
256
275
|
await this.secrets.remove(was.secret_scope).catch((removal) => this.say({ condition: "secret_unremoved", error: removal.message }));
|
|
257
276
|
return { created: !was, view: this.view(), ...(secret ? { secret } : {}) };
|
|
258
277
|
}
|
|
278
|
+
/** POST .../secret: a new secret for the URL set now, answered once; the webhook is otherwise as it was (its URL, when
|
|
279
|
+
* it was set, and off if a 410 turned it off). The secret it replaces goes at once, retired (still cut from what a run
|
|
280
|
+
* says): every try from then on, of a message queued before it too, is signed with the new one, since a try signs with
|
|
281
|
+
* the secret the setting names when it sends (tryOnce). A try already sending was signed with the old one. As a PUT
|
|
282
|
+
* does, the new secret is sealed under a scope of its own before the setting that names it is written, and a write
|
|
283
|
+
* that fails leaves the old secret signing, after a restart too. */
|
|
284
|
+
rotate() {
|
|
285
|
+
return this.change(async () => {
|
|
286
|
+
const was = this.config;
|
|
287
|
+
if (!was)
|
|
288
|
+
throw new ApiError("not_found", "no webhook is set: PUT /v2/settings/webhook sets one");
|
|
289
|
+
const scope = `${SERVER_SCOPE}:${randomBytes(8).toString("hex")}`;
|
|
290
|
+
const secret = await this.secrets.mint(scope, SECRET_FIELD);
|
|
291
|
+
try {
|
|
292
|
+
await this.write({ ...was, secret_at: new Date().toISOString(), secret_scope: scope });
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
// The store may hold the setting that names the new secret (a put that landed, its flush failed). The old setting
|
|
296
|
+
// goes back first, and only once it is durable does the new secret go: the store never names a secret that is gone.
|
|
297
|
+
// If it can't go back, the new secret stays, and the next start removes whichever secret the setting does not name
|
|
298
|
+
// (load).
|
|
299
|
+
const restored = await this.write(was).then(() => true, () => false);
|
|
300
|
+
if (restored)
|
|
301
|
+
await this.secrets.remove(scope).catch((removal) => this.say({ condition: "secret_unremoved", error: removal.message }));
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
await this.secrets.remove(was.secret_scope).catch((removal) => this.say({ condition: "secret_unremoved", error: removal.message }));
|
|
305
|
+
return { view: this.view(), secret };
|
|
306
|
+
});
|
|
307
|
+
}
|
|
259
308
|
/** DELETE: the webhook gone, its queued messages with it, and its secret (retired: still cut from what a run says); the
|
|
260
309
|
* log of attempts stays. In that order: shut to new notices, its queued messages dropped, the setting removed, then
|
|
261
310
|
* every secret of the webhook, on every DELETE (none once gone), so a removal of a secret that failed is finished by
|
|
@@ -268,8 +317,8 @@ export class Webhooks {
|
|
|
268
317
|
// Every message whose write passed the check before the DELETE began lands first (on Postgres another pooled
|
|
269
318
|
// connection may commit it later): then the drop takes it too, and none outlives the DELETE.
|
|
270
319
|
await Promise.all([...this.posting]);
|
|
271
|
-
await this.drop(
|
|
272
|
-
await this.store.settings.remove(
|
|
320
|
+
await this.drop(this.outbound.words.removed, true);
|
|
321
|
+
await this.store.settings.remove(this.outbound.setting);
|
|
273
322
|
await this.store.flush();
|
|
274
323
|
this.config = undefined;
|
|
275
324
|
}
|
|
@@ -289,7 +338,7 @@ export class Webhooks {
|
|
|
289
338
|
}
|
|
290
339
|
/** GET .../deliveries: the last attempts, newest first. */
|
|
291
340
|
async attemptsView() {
|
|
292
|
-
return { attempts: (await this.store.channels.attempts(
|
|
341
|
+
return { attempts: (await this.store.channels.attempts(this.outbound.channel, ATTEMPTS_KEPT)).map(attemptView) };
|
|
293
342
|
}
|
|
294
343
|
/** A run's moment, told by the server each time it sees it (a wait reached, an end the store has): queued once, when a
|
|
295
344
|
* webhook is set and on, with its ledger row in the same transaction. `marker` makes its webhook-id: the same moment,
|
|
@@ -317,14 +366,14 @@ export class Webhooks {
|
|
|
317
366
|
}
|
|
318
367
|
if (ends && !this.due(marker, config))
|
|
319
368
|
return "queued";
|
|
320
|
-
const id =
|
|
321
|
-
const data = { run_id: run.run_id, agent_id: run.agent_id, status: run.status, title: run.title, url: `${this.base}/#/runs/${encodeURIComponent(run.run_id)}`, ...(run.pending ? { pending: run.pending } : {}) };
|
|
369
|
+
const id = this.messageId(`${run.run_id}\n${type}\n${marker}`);
|
|
370
|
+
const data = { run_id: run.run_id, agent_id: run.agent_id, agent_name: run.agent_name, status: run.status, title: run.title, url: `${this.base}/#/runs/${encodeURIComponent(run.run_id)}`, ...(run.pending ? { pending: run.pending } : {}) };
|
|
322
371
|
// A new wait goes beside the run's earlier waits and replaces its end (one after an end is a continued run's, past that
|
|
323
372
|
// end); an end replaces the run's last end and keeps its waits.
|
|
324
373
|
const told = ends ? { runId: run.run_id, marker: `${ENDED}${marker}`, replacing: ENDED } : { runId: run.run_id, marker: `${WAIT}${marker}`, replacing: ENDED };
|
|
325
374
|
let added;
|
|
326
375
|
try {
|
|
327
|
-
added = await this.posted({ id, channel:
|
|
376
|
+
added = await this.posted({ id, channel: this.outbound.channel, runId: run.run_id, moment: type, body: this.body(type, data), nextAt: Date.now() }, told);
|
|
328
377
|
}
|
|
329
378
|
catch (error) {
|
|
330
379
|
this.say({ condition: "outbox_unwritable", webhook_id: id, error: error.message });
|
|
@@ -333,7 +382,7 @@ export class Webhooks {
|
|
|
333
382
|
this.notices.set(run.run_id, ends ? { ...seen, ended: marker } : { waits: kept });
|
|
334
383
|
// The waits past WAITS_KEPT leave the store's ledger too; one not forgotten now is forgotten with the next.
|
|
335
384
|
if (!ends && waits.length > kept.length)
|
|
336
|
-
await this.store.channels.forget(
|
|
385
|
+
await this.store.channels.forget(this.outbound.channel, waits.slice(0, waits.length - kept.length).map((gone) => ({ runId: run.run_id, marker: `${WAIT}${gone}` }))).catch(() => undefined);
|
|
337
386
|
await this.prune(config);
|
|
338
387
|
// A message already there (an earlier telling whose durable commit failed after its write) is armed too, unless it is.
|
|
339
388
|
if (added || !this.timers.has(id))
|
|
@@ -347,9 +396,9 @@ export class Webhooks {
|
|
|
347
396
|
return "closed";
|
|
348
397
|
if (!this.config || this.config.disabled || this.removing)
|
|
349
398
|
return "off";
|
|
350
|
-
const id =
|
|
399
|
+
const id = this.messageId(`trigger.failed\n${firingKey}`);
|
|
351
400
|
try {
|
|
352
|
-
if ((await this.posted({ id, channel:
|
|
401
|
+
if ((await this.posted({ id, channel: this.outbound.channel, moment: "trigger.failed", body: this.body("trigger.failed", failed), nextAt: Date.now() })) || !this.timers.has(id))
|
|
353
402
|
this.schedule(id, 0);
|
|
354
403
|
}
|
|
355
404
|
catch (error) {
|
|
@@ -383,7 +432,7 @@ export class Webhooks {
|
|
|
383
432
|
forget.push({ runId: id, marker: `${ENDED}${noticed.ended}` });
|
|
384
433
|
}
|
|
385
434
|
if (forget.length) {
|
|
386
|
-
await this.store.channels.forget(
|
|
435
|
+
await this.store.channels.forget(this.outbound.channel, forget);
|
|
387
436
|
for (const { runId } of forget) {
|
|
388
437
|
const noticed = this.notices.get(runId);
|
|
389
438
|
if (store.has(runId) && noticed?.ended !== undefined)
|
|
@@ -410,11 +459,18 @@ export class Webhooks {
|
|
|
410
459
|
}
|
|
411
460
|
return unsaved;
|
|
412
461
|
}
|
|
462
|
+
/** A moment's message id, made from `key` (the same moment, told again by a later server too, has the same id). The
|
|
463
|
+
* store keeps one message per id across every channel, so another outbox's ids are made under its channel too: Slack's
|
|
464
|
+
* message for a run's end is never the webhook's, whichever is written first. The webhook's ids are as they always were. */
|
|
465
|
+
messageId(key) {
|
|
466
|
+
const made = this.outbound.channel === WEBHOOK_CHANNEL ? key : `${this.outbound.channel}\n${key}`;
|
|
467
|
+
return `msg_${createHash("sha256").update(made).digest("hex").slice(0, 32)}`;
|
|
468
|
+
}
|
|
413
469
|
/** POST .../test: a `webhook.test` message tried once now, never again; its attempt. */
|
|
414
470
|
async test() {
|
|
415
471
|
this.need();
|
|
416
472
|
const id = `msg_${randomBytes(16).toString("hex")}`;
|
|
417
|
-
await this.posted({ id, channel:
|
|
473
|
+
await this.posted({ id, channel: this.outbound.channel, moment: this.outbound.testType, body: this.body(this.outbound.testType, {}), nextAt: Date.now() });
|
|
418
474
|
return this.attempt(id, { retry: false, redelivery: false });
|
|
419
475
|
}
|
|
420
476
|
/** A message (and its ledger row, `told`) queued in one transaction, then made durable (the store's flush, as every write
|
|
@@ -435,7 +491,7 @@ export class Webhooks {
|
|
|
435
491
|
async redeliver(id) {
|
|
436
492
|
this.need();
|
|
437
493
|
const post = await this.store.channels.get(id);
|
|
438
|
-
if (!post || post.channel !==
|
|
494
|
+
if (!post || post.channel !== this.outbound.channel)
|
|
439
495
|
throw new ApiError("not_found", `no message ${id} among the last ${ATTEMPTS_KEPT} attempts`);
|
|
440
496
|
return this.attempt(id, { retry: false, redelivery: true });
|
|
441
497
|
}
|
|
@@ -478,7 +534,7 @@ export class Webhooks {
|
|
|
478
534
|
return {};
|
|
479
535
|
const host = url.hostname.replace(/^\[|\]$/g, "");
|
|
480
536
|
if (LOCAL_NAME.test(host))
|
|
481
|
-
return { refused:
|
|
537
|
+
return { refused: notPublic(host) };
|
|
482
538
|
const { isPrivateAddress, pinnedLookup } = await import("@parcha/pi-browser");
|
|
483
539
|
let addresses;
|
|
484
540
|
try {
|
|
@@ -490,8 +546,11 @@ export class Webhooks {
|
|
|
490
546
|
return { refused: signal.reason?.name === "TimeoutError" ? `no answer in ${this.timeoutMs / 1000} s` : "not reached: the server is stopping" };
|
|
491
547
|
return { refused: `not reached: ${error.code ?? "its name does not resolve"}` };
|
|
492
548
|
}
|
|
493
|
-
|
|
549
|
+
// A name that resolves to nothing is no switch's refusal: the operator's switch would not reach it either.
|
|
550
|
+
if (!addresses.length)
|
|
494
551
|
return { refused: `refused: ${host} is not a public host` };
|
|
552
|
+
if (addresses.some(isPrivateAddress))
|
|
553
|
+
return { refused: notPublic(host) };
|
|
495
554
|
return { dispatcher: new Agent({ connect: { lookup: pinnedLookup(addresses[0]) } }) };
|
|
496
555
|
}
|
|
497
556
|
signing() {
|
|
@@ -499,18 +558,24 @@ export class Webhooks {
|
|
|
499
558
|
}
|
|
500
559
|
/** The secret the webhook's setting names, opened here; undefined when none is set, or it does not open here. */
|
|
501
560
|
signingSecret(config = this.config) {
|
|
502
|
-
return config ? this.secrets.get(config.secret_scope,
|
|
561
|
+
return config ? this.secrets.get(config.secret_scope, this.outbound.field) : undefined;
|
|
503
562
|
}
|
|
504
563
|
body(type, data) {
|
|
505
564
|
return JSON.stringify({ type, timestamp: new Date().toISOString(), data });
|
|
506
565
|
}
|
|
507
566
|
/** The setting written, then held here: a write the store refuses throws, and what was held stays held. */
|
|
508
567
|
async write(config) {
|
|
509
|
-
await this.store.settings.put(
|
|
568
|
+
await this.store.settings.put(this.outbound.setting, config);
|
|
510
569
|
// Durable before the PUT (or the 410 that turns it off) answers, as the store's other confirmed writes are.
|
|
511
570
|
await this.store.flush();
|
|
512
571
|
this.config = config;
|
|
513
572
|
}
|
|
573
|
+
/** The setting removed, durably, and none held here: a removal the store refuses throws, and what was held stays held. */
|
|
574
|
+
async unwrite() {
|
|
575
|
+
await this.store.settings.remove(this.outbound.setting);
|
|
576
|
+
await this.store.flush();
|
|
577
|
+
this.config = undefined;
|
|
578
|
+
}
|
|
514
579
|
/** Whether an end's run.ended is still to be queued: at or after the webhook was set and the day began (a marker that
|
|
515
580
|
* names no time is not judged by one). The catch-up and the ledger's pruning read the same line, so the ledger never
|
|
516
581
|
* forgets an end a start would look for. */
|
|
@@ -525,7 +590,7 @@ export class Webhooks {
|
|
|
525
590
|
if (!stale.length)
|
|
526
591
|
return;
|
|
527
592
|
try {
|
|
528
|
-
await this.store.channels.forget(
|
|
593
|
+
await this.store.channels.forget(this.outbound.channel, stale.map(([runId, noticed]) => ({ runId, marker: `${ENDED}${noticed.ended}` })));
|
|
529
594
|
}
|
|
530
595
|
catch {
|
|
531
596
|
return;
|
|
@@ -576,7 +641,7 @@ export class Webhooks {
|
|
|
576
641
|
async tryOnce(id, how) {
|
|
577
642
|
const now = Date.now();
|
|
578
643
|
const post = await this.store.channels.get(id);
|
|
579
|
-
const attempt = { webhook_id: id, type: (post?.moment ??
|
|
644
|
+
const attempt = { webhook_id: id, type: (post?.moment ?? this.outbound.testType), at: iso(now), status_code: null, latency_ms: 0, error: null, retry_at: null, redelivery: how.redelivery };
|
|
580
645
|
if (this.closed || !post)
|
|
581
646
|
return attempt;
|
|
582
647
|
// The try's own id: its claim's token, which fences its outcome's write (settleTry), and its row in the log, once.
|
|
@@ -600,16 +665,17 @@ export class Webhooks {
|
|
|
600
665
|
// Signed with the secret this setting names: the URL and its secret, as the store holds them together.
|
|
601
666
|
const secret = this.signingSecret(config);
|
|
602
667
|
let failure;
|
|
668
|
+
let gone;
|
|
603
669
|
if (!config)
|
|
604
|
-
failure =
|
|
670
|
+
failure = this.outbound.words.removed;
|
|
605
671
|
else if (config.disabled)
|
|
606
|
-
failure =
|
|
672
|
+
failure = this.outbound.words.off;
|
|
607
673
|
else if (secret === undefined)
|
|
608
|
-
failure =
|
|
674
|
+
failure = this.outbound.words.unreadable;
|
|
609
675
|
else {
|
|
610
676
|
const timestamp = String(Math.floor(now / 1000));
|
|
611
677
|
const started = performance.now();
|
|
612
|
-
const target =
|
|
678
|
+
const { url: target, headers } = this.request(config, secret, id, timestamp, post.body);
|
|
613
679
|
// One bound for the whole try, its name's lookup included, and the server's stop cuts it all (close()).
|
|
614
680
|
const signal = AbortSignal.any([AbortSignal.timeout(this.timeoutMs), this.stop.signal]);
|
|
615
681
|
const dial = await this.dialled(target, signal);
|
|
@@ -619,14 +685,12 @@ export class Webhooks {
|
|
|
619
685
|
try {
|
|
620
686
|
const res = await undiciFetch(target, {
|
|
621
687
|
method: "POST", redirect: "manual", signal,
|
|
622
|
-
headers
|
|
688
|
+
headers,
|
|
623
689
|
body: post.body,
|
|
624
690
|
...(dial.dispatcher ? { dispatcher: dial.dispatcher } : {}),
|
|
625
691
|
});
|
|
626
|
-
await res.body?.cancel().catch(() => undefined);
|
|
627
692
|
attempt.status_code = res.status;
|
|
628
|
-
|
|
629
|
-
failure = res.status >= 300 && res.status < 400 ? `answered ${res.status}: a redirect is not followed` : `answered ${res.status}`;
|
|
693
|
+
({ failure, gone } = await this.judged(res));
|
|
630
694
|
}
|
|
631
695
|
catch (error) {
|
|
632
696
|
failure = error.name === "TimeoutError" ? `no answer in ${this.timeoutMs / 1000} s` : `not reached: ${error.cause?.code ?? error.message}`;
|
|
@@ -637,13 +701,30 @@ export class Webhooks {
|
|
|
637
701
|
}
|
|
638
702
|
attempt.latency_ms = Math.round(performance.now() - started);
|
|
639
703
|
}
|
|
704
|
+
// A failure's words never hold the credential (an outbox whose URL is its credential: Slack's).
|
|
705
|
+
if (failure !== undefined && secret)
|
|
706
|
+
failure = failure.split(secret).join("…");
|
|
640
707
|
// In its words, written or not: a try the server's stop cut short still says why it ended.
|
|
641
708
|
attempt.error = failure ?? null;
|
|
642
709
|
if (this.closed)
|
|
643
710
|
return attempt;
|
|
644
711
|
// What the try found is settled against the webhook as it stands once every change asked for before it (a PUT, a
|
|
645
712
|
// DELETE, another try's 410) has landed: a 410 never turns off a webhook a PUT is setting again meanwhile.
|
|
646
|
-
return this.change(() => this.settleTry(id, how, now, tryId, claimed, config, failure, attempt));
|
|
713
|
+
return this.change(() => this.settleTry(id, how, now, tryId, claimed, config, failure, attempt, gone));
|
|
714
|
+
}
|
|
715
|
+
/** What a try sends to (`url`) and with which headers, for the outbox's credential: the webhook's URL, signed with its
|
|
716
|
+
* secret. Slack's outbox sends to its sealed URL (slack.ts). */
|
|
717
|
+
request(config, credential, id, timestamp, body) {
|
|
718
|
+
return { url: new URL(config.url), headers: { "content-type": "application/json", "user-agent": "agentrun-serve-webhook/1", "webhook-id": id, "webhook-timestamp": timestamp, "webhook-signature": signatureOf(credential, id, timestamp, body) } };
|
|
719
|
+
}
|
|
720
|
+
/** What a receiver's answer says: why the try failed (none when it landed), and whether the receiver is gone, which
|
|
721
|
+
* turns the outbox off. The webhook's: 2xx lands; a redirect is not followed; a 410 is gone. */
|
|
722
|
+
async judged(res) {
|
|
723
|
+
await res.body?.cancel().catch(() => undefined);
|
|
724
|
+
if (res.status >= 200 && res.status <= 299)
|
|
725
|
+
return {};
|
|
726
|
+
const failure = res.status >= 300 && res.status < 400 ? `answered ${res.status}: a redirect is not followed` : `answered ${res.status}`;
|
|
727
|
+
return res.status === 410 ? { failure, gone: { reason: "the receiver answered 410 Gone", drop: "the webhook is off: its receiver answered 410" } } : { failure };
|
|
647
728
|
}
|
|
648
729
|
/** A try's outcome written: the message's new state and the try, in one transaction; its next try armed; a 410 that
|
|
649
730
|
* still names the webhook as it stands turns it off.
|
|
@@ -653,7 +734,7 @@ export class Webhooks {
|
|
|
653
734
|
* holds that claim (Channels.settle), so a write that lands late, after a redelivery sent the message, a newer try
|
|
654
735
|
* claimed it or a DELETE dropped it, changes nothing and arms nothing. A redelivery claims nothing: a landed one writes
|
|
655
736
|
* sent, which no try's write undoes (sent is final), and a failed one writes no state at all, only its try. */
|
|
656
|
-
async settleTry(id, how, now, tryId, claimed, config, failure, attempt) {
|
|
737
|
+
async settleTry(id, how, now, tryId, claimed, config, failure, attempt, goneBy) {
|
|
657
738
|
if (this.closed)
|
|
658
739
|
return attempt;
|
|
659
740
|
// The schedule counts the message's own tries, from the count its claim took it at; a redelivery counts none.
|
|
@@ -661,7 +742,7 @@ export class Webhooks {
|
|
|
661
742
|
// A 410 turns off the webhook as this try found it: one set again or removed while the try waited is not its to turn
|
|
662
743
|
// off, and the 410 is then a failure like any other.
|
|
663
744
|
const standing = this.config;
|
|
664
|
-
const gone =
|
|
745
|
+
const gone = goneBy !== undefined && config !== undefined && standing !== undefined && standing.url === config.url && standing.updated_at === config.updated_at && !standing.disabled;
|
|
665
746
|
let to;
|
|
666
747
|
let delay;
|
|
667
748
|
if (attempts === undefined) {
|
|
@@ -688,7 +769,7 @@ export class Webhooks {
|
|
|
688
769
|
// The same write each time it is tried: its fence and its values fixed here, so one whose outcome is unknown (the store
|
|
689
770
|
// threw) is written again as it was, and lands once.
|
|
690
771
|
const write = () => this.store.channels.settle(id, to, row, { kept: ATTEMPTS_KEPT, sending: [...this.sending.keys()], ...(claimed ? { claim: tryId } : {}) });
|
|
691
|
-
const after = (written) => this.afterSettle(id, written, to, delay, attempt, attempts, failure, gone, standing);
|
|
772
|
+
const after = (written) => this.afterSettle(id, written, to, delay, attempt, attempts, failure, gone ? goneBy : undefined, standing);
|
|
692
773
|
let written;
|
|
693
774
|
try {
|
|
694
775
|
written = await write();
|
|
@@ -722,10 +803,12 @@ export class Webhooks {
|
|
|
722
803
|
this.say({ condition: "delivery_failed", type: attempt.type, webhook_id: id, attempts: attempts ?? 0, ...(attempt.status_code !== null ? { status_code: attempt.status_code } : {}), error: failure });
|
|
723
804
|
}
|
|
724
805
|
const current = this.config;
|
|
725
|
-
const stillGone = gone && standing !== undefined && current !== undefined && !current.disabled && current.url === standing.url && current.secret_scope === standing.secret_scope && current.updated_at === standing.updated_at;
|
|
806
|
+
const stillGone = gone !== undefined && standing !== undefined && current !== undefined && !current.disabled && current.url === standing.url && current.secret_scope === standing.secret_scope && current.updated_at === standing.updated_at;
|
|
726
807
|
if (stillGone) {
|
|
727
|
-
// Gone: the receiver says this endpoint is no more. Off, its queued messages dropped, until a PUT sets it again.
|
|
728
|
-
|
|
808
|
+
// Gone: the receiver says this endpoint is no more. Off, its queued messages dropped, until a PUT sets it again. It is
|
|
809
|
+
// the setting as it stands now that goes off (the same URL, secret and cutoff as the try's): a change since that
|
|
810
|
+
// kept those (Slack's events, slack.ts) is kept, never written over by the try's older copy.
|
|
811
|
+
const off = { ...current, disabled: { at: attempt.at, reason: gone.reason } };
|
|
729
812
|
try {
|
|
730
813
|
await this.write(off);
|
|
731
814
|
}
|
|
@@ -738,7 +821,7 @@ export class Webhooks {
|
|
|
738
821
|
// Every message whose write passed the check before the webhook went off lands first (as a DELETE waits, remove):
|
|
739
822
|
// then the drop takes it too, and none reaches a receiver a later PUT sets.
|
|
740
823
|
await Promise.all([...this.posting]);
|
|
741
|
-
await this.drop(
|
|
824
|
+
await this.drop(gone.drop);
|
|
742
825
|
}
|
|
743
826
|
}
|
|
744
827
|
/** A try's write the store refused (`write`), written again after the store's wait until it lands, then what follows it
|
|
@@ -778,7 +861,7 @@ export class Webhooks {
|
|
|
778
861
|
async drop(why, strict = false) {
|
|
779
862
|
let count = 0;
|
|
780
863
|
try {
|
|
781
|
-
count = await this.store.channels.dropAll(
|
|
864
|
+
count = await this.store.channels.dropAll(this.outbound.channel, why);
|
|
782
865
|
}
|
|
783
866
|
catch (error) {
|
|
784
867
|
// Refused: a DELETE fails with the webhook still set, its messages still queued and their timers kept, so each is
|
|
@@ -796,9 +879,9 @@ export class Webhooks {
|
|
|
796
879
|
need() {
|
|
797
880
|
const config = this.config;
|
|
798
881
|
if (!config)
|
|
799
|
-
throw new ApiError("not_found",
|
|
882
|
+
throw new ApiError("not_found", this.outbound.words.unset);
|
|
800
883
|
if (config.disabled)
|
|
801
|
-
throw new ApiError(
|
|
884
|
+
throw new ApiError(this.outbound.offCode, this.outbound.words.offWith(config.disabled.reason));
|
|
802
885
|
return config;
|
|
803
886
|
}
|
|
804
887
|
}
|
package/dist/wires.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Wire } from "@parcha/agentrun-kernel";
|
|
1
|
+
import type { HarnessWire, Wire } from "@parcha/agentrun-kernel";
|
|
2
2
|
/** Each wire's own settings: the vendor whose models it runs (a MODEL_KEYS row's id, model.ts), the endpoint a host may
|
|
3
3
|
* name for it, the vendor's key, and whether it is serve's gateway (AGENT_MODEL_BASE_URL), which pi's models take too and
|
|
4
4
|
* which runs any id (the gateway's own string, naming no vendor here). */
|
|
@@ -9,3 +9,8 @@ export declare const WIRES: Readonly<Record<Wire, {
|
|
|
9
9
|
key: string;
|
|
10
10
|
gateway: boolean;
|
|
11
11
|
}>>;
|
|
12
|
+
/** Whether a harness's wire is a model wire of ours (WIRES): not "plan", a harness billed to its own vendor's plan
|
|
13
|
+
* (Cursor's), whose requests never reach a route of ours, so it has no vendor, endpoint, key or gateway here. */
|
|
14
|
+
export declare const modelWire: (wire: HarnessWire) => wire is Wire;
|
|
15
|
+
/** A harness's wire's settings, or undefined for one billed to its own plan (modelWire). */
|
|
16
|
+
export declare const wireOf: (wire: HarnessWire) => (typeof WIRES)[Wire] | undefined;
|
package/dist/wires.js
CHANGED
|
@@ -5,3 +5,8 @@ export const WIRES = Object.freeze({
|
|
|
5
5
|
"anthropic-messages": { vendor: "anthropic", label: "Anthropic", base: "ANTHROPIC_BASE_URL", key: "ANTHROPIC_API_KEY", gateway: false },
|
|
6
6
|
"openai-responses": { vendor: "openai", label: "OpenAI", base: "AGENT_MODEL_BASE_URL", key: "OPENAI_API_KEY", gateway: true },
|
|
7
7
|
});
|
|
8
|
+
/** Whether a harness's wire is a model wire of ours (WIRES): not "plan", a harness billed to its own vendor's plan
|
|
9
|
+
* (Cursor's), whose requests never reach a route of ours, so it has no vendor, endpoint, key or gateway here. */
|
|
10
|
+
export const modelWire = (wire) => wire !== "plan";
|
|
11
|
+
/** A harness's wire's settings, or undefined for one billed to its own plan (modelWire). */
|
|
12
|
+
export const wireOf = (wire) => (modelWire(wire) ? WIRES[wire] : undefined);
|