@parcha/agentrun-serve 0.1.0-beta.38 → 0.1.0-beta.39
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 +418 -0
- package/README.md +325 -124
- package/dist/adapters.d.ts +4 -1
- package/dist/adapters.js +4 -2
- package/dist/agent-rows.d.ts +0 -2
- package/dist/agents-store.d.ts +5 -5
- package/dist/agents-store.js +14 -14
- package/dist/bash-read-only.js +2 -1
- package/dist/cap-short.d.ts +12 -0
- package/dist/cap-short.js +60 -0
- package/dist/cap.d.ts +3 -10
- package/dist/cap.js +2 -5
- package/dist/catalog.d.ts +0 -5
- package/dist/catalog.js +1 -5
- package/dist/chatgpt-plan.d.ts +163 -0
- package/dist/chatgpt-plan.js +549 -0
- package/dist/cli.js +26 -6
- package/dist/composio-preset.d.ts +8 -0
- package/dist/composio-preset.js +8 -7
- package/dist/composio-triggers.d.ts +113 -0
- package/dist/composio-triggers.js +411 -0
- package/dist/computer-calls.js +2 -2
- package/dist/computer-commands.d.ts +2 -0
- package/dist/computer-commands.js +7 -3
- package/dist/computer-live.d.ts +7 -2
- package/dist/computer-live.js +9 -5
- package/dist/computer-routes.d.ts +2 -2
- package/dist/computer-routes.js +2 -2
- package/dist/computer-setting.d.ts +3 -3
- package/dist/computer-setting.js +5 -5
- package/dist/computer-view.d.ts +15 -0
- package/dist/computer-view.js +16 -2
- package/dist/computer-viewer.js +14 -1
- package/dist/computers.d.ts +5 -2
- package/dist/computers.js +9 -5
- package/dist/console/assets/index-BNhZs-jp.css +1 -0
- package/dist/console/assets/index-BQv5aOGB.js +700 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.js +3 -3
- package/dist/decision-cap.d.ts +2 -5
- package/dist/decision-cap.js +3 -6
- package/dist/decision-rows.js +11 -11
- package/dist/email/account.d.ts +84 -0
- package/dist/email/account.js +159 -0
- package/dist/email/answer.d.ts +10 -0
- package/dist/email/answer.js +182 -0
- package/dist/email/email-faux.d.ts +95 -0
- package/dist/email/email-faux.js +849 -0
- package/dist/email/labels.d.ts +1 -0
- package/dist/email/labels.js +84 -0
- package/dist/email/mail.d.ts +186 -0
- package/dist/email/mail.js +380 -0
- package/dist/email/names.d.ts +20 -0
- package/dist/email/names.js +40 -0
- package/dist/email/routes.d.ts +9 -0
- package/dist/email/routes.js +50 -0
- package/dist/email/tools.d.ts +20 -0
- package/dist/email/tools.js +517 -0
- package/dist/environment.d.ts +17 -2
- package/dist/environment.js +36 -8
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +12 -0
- package/dist/eval-mailbox.d.ts +103 -0
- package/dist/eval-mailbox.js +236 -0
- package/dist/graph-view.d.ts +9 -2
- package/dist/graph-view.js +6 -2
- package/dist/harness-install.d.ts +28 -2
- package/dist/harness-install.js +147 -59
- package/dist/harness-opencode/adapter.js +2 -26
- package/dist/harness-opencode/facts.js +5 -2
- package/dist/harness-opencode/host/policy.d.ts +2 -0
- package/dist/harness-opencode/host/policy.js +10 -5
- package/dist/harness-opencode/index.d.ts +1 -1
- package/dist/harness-opencode/index.js +1 -1
- package/dist/harness-opencode/versions.d.ts +6 -0
- package/dist/harness-opencode/versions.js +6 -0
- package/dist/harness-table.js +8 -5
- package/dist/harness.d.ts +12 -0
- package/dist/harness.js +15 -3
- package/dist/hosted-search.d.ts +3 -1
- package/dist/hosted-search.js +51 -11
- package/dist/import-sources.d.ts +1 -1
- package/dist/import-sources.js +3 -3
- package/dist/import.d.ts +21 -0
- package/dist/import.js +186 -1
- package/dist/key-check.d.ts +0 -2
- package/dist/key-check.js +3 -4
- package/dist/key-refusal.d.ts +8 -2
- package/dist/key-refusal.js +16 -13
- package/dist/learn/check.d.ts +18 -3
- package/dist/learn/check.js +328 -3
- package/dist/learn/compile.d.ts +9 -2
- package/dist/learn/compile.js +52 -16
- package/dist/learn/grade.d.ts +4 -0
- package/dist/learn/grade.js +4 -1
- package/dist/learn/promote.d.ts +106 -51
- package/dist/learn/promote.js +82 -189
- package/dist/learn/refit.d.ts +21 -12
- package/dist/learn/refit.js +51 -23
- package/dist/learn/replay-calls.js +1 -1
- package/dist/learn/skills.d.ts +2 -7
- package/dist/learn/skills.js +28 -14
- package/dist/learned-nodes.d.ts +15 -7
- package/dist/learned-nodes.js +29 -5
- package/dist/learned-workflow.d.ts +41 -31
- package/dist/learned-workflow.js +122 -63
- package/dist/learning-batch.js +3 -3
- package/dist/learning-cost.d.ts +26 -8
- package/dist/learning-cost.js +17 -7
- package/dist/learning-routes.d.ts +2 -4
- package/dist/learning-routes.js +20 -3
- package/dist/learning-words.d.ts +1 -1
- package/dist/learning-words.js +4 -1
- package/dist/learning.d.ts +5 -5
- package/dist/link-check.d.ts +27 -0
- package/dist/link-check.js +164 -0
- package/dist/local-models.d.ts +33 -2
- package/dist/local-models.js +43 -5
- package/dist/mcp-pin-first.js +6 -0
- package/dist/mcp-secret-writes.js +3 -0
- package/dist/mcp-servers-store.d.ts +25 -16
- package/dist/mcp-servers-store.js +23 -3
- package/dist/model-form.d.ts +11 -0
- package/dist/model-form.js +17 -1
- package/dist/model.d.ts +26 -6
- package/dist/model.js +126 -18
- package/dist/monid.d.ts +26 -31
- package/dist/monid.js +84 -79
- package/dist/native/binding.d.ts +13 -0
- package/dist/native/binding.js +57 -0
- package/dist/native/cli.d.ts +2 -0
- package/dist/native/cli.js +48 -0
- package/dist/native/connection.d.ts +8 -0
- package/dist/native/connection.js +94 -0
- package/dist/native/context.d.ts +6 -0
- package/dist/native/context.js +22 -0
- package/dist/native/discovery.d.ts +27 -0
- package/dist/native/discovery.js +404 -0
- package/dist/native/environment.d.ts +4 -0
- package/dist/native/environment.js +18 -0
- package/dist/native/history.d.ts +20 -0
- package/dist/native/history.js +147 -0
- package/dist/native/instructions.d.ts +21 -0
- package/dist/native/instructions.js +173 -0
- package/dist/native/remote-worker-bridge.d.ts +3 -0
- package/dist/native/remote-worker-bridge.js +278 -0
- package/dist/native/remote.d.ts +187 -0
- package/dist/native/remote.js +492 -0
- package/dist/native/runtime.d.ts +17 -0
- package/dist/native/runtime.js +316 -0
- package/dist/native/runtimes.d.ts +66 -0
- package/dist/native/runtimes.js +41 -0
- package/dist/native/schema.d.ts +46 -0
- package/dist/native/schema.js +41 -0
- package/dist/native/types.d.ts +87 -0
- package/dist/native/types.js +1 -0
- package/dist/observation/http.d.ts +4 -0
- package/dist/observation/http.js +140 -0
- package/dist/observation/observer.d.ts +301 -0
- package/dist/observation/observer.js +919 -0
- package/dist/observation/schema.d.ts +52 -0
- package/dist/observation/schema.js +154 -0
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +394 -85
- package/dist/paid-calls.d.ts +8 -3
- package/dist/paid-calls.js +6 -2
- package/dist/place-offers.d.ts +4 -8
- package/dist/place-offers.js +4 -9
- package/dist/port-models.d.ts +7 -2
- package/dist/port-models.js +5 -2
- package/dist/print.d.ts +4 -2
- package/dist/print.js +4 -3
- package/dist/propose.d.ts +1 -2
- package/dist/propose.js +10 -1
- package/dist/provider.d.ts +5 -1
- package/dist/provider.js +8 -0
- package/dist/push-handler.d.ts +3 -2
- package/dist/push-handler.js +20 -6
- package/dist/refusal-words.d.ts +11 -0
- package/dist/refusal-words.js +13 -2
- package/dist/run-launch.d.ts +10 -1
- package/dist/run-launch.js +11 -4
- package/dist/run-learning-served.d.ts +75 -2
- package/dist/run-manifest.d.ts +3 -0
- package/dist/run-manifest.js +13 -1
- package/dist/run-reads-over-store.d.ts +3 -3
- package/dist/run-reads-over-store.js +13 -10
- package/dist/run-timeline-served.d.ts +3 -2
- package/dist/run-timeline-served.js +8 -9
- package/dist/run-view.d.ts +42 -35
- package/dist/run-view.js +79 -44
- package/dist/runnable.d.ts +13 -5
- package/dist/runnable.js +69 -60
- package/dist/runner/adapter-loop.d.ts +42 -11
- package/dist/runner/adapter-loop.js +651 -247
- package/dist/runner/adapter-run-spec.d.ts +37 -12
- package/dist/runner/adapter-run-spec.js +139 -106
- package/dist/runner/adapters/pi.d.ts +24 -0
- package/dist/runner/adapters/pi.js +59 -9
- package/dist/runner/beside.d.ts +3 -1
- package/dist/runner/beside.js +24 -8
- package/dist/runner/claim-secrets.d.ts +3 -1
- package/dist/runner/claim-secrets.js +28 -4
- package/dist/runner/cli-run-spec.d.ts +30 -0
- package/dist/runner/cli-run-spec.js +94 -50
- package/dist/runner/computer-channel.d.ts +5 -2
- package/dist/runner/computer-channel.js +3 -2
- package/dist/runner/computer-journal.d.ts +6 -0
- package/dist/runner/computer-journal.js +8 -10
- package/dist/runner/conformance/harness.js +6 -3
- package/dist/runner/end-of-run.d.ts +2 -0
- package/dist/runner/end-of-run.js +3 -1
- package/dist/runner/feedback-round.d.ts +40 -13
- package/dist/runner/feedback-round.js +64 -16
- package/dist/runner/harness-loops.js +17 -0
- package/dist/runner/journal-kinds.d.ts +37 -13
- package/dist/runner/journal-kinds.js +9 -5
- package/dist/runner/learning-dispatch.d.ts +3 -2
- package/dist/runner/learning-dispatch.js +6 -6
- package/dist/runner/learning-end.d.ts +4 -0
- package/dist/runner/learning-end.js +64 -36
- package/dist/runner/learning-factory.d.ts +3 -0
- package/dist/runner/learning-factory.js +8 -2
- package/dist/runner/learning-loop.d.ts +2 -0
- package/dist/runner/learning-loop.js +19 -4
- package/dist/runner/loop.d.ts +18 -0
- package/dist/runner/loop.js +34 -0
- package/dist/runner/main.d.ts +6 -1
- package/dist/runner/main.js +26 -4
- package/dist/runner/mcp-run.d.ts +4 -1
- package/dist/runner/mcp-run.js +11 -5
- package/dist/runner/meta-tools.d.ts +10 -3
- package/dist/runner/meta-tools.js +112 -36
- package/dist/runner/metrics.d.ts +80 -21
- package/dist/runner/metrics.js +114 -39
- package/dist/runner/model-route.d.ts +12 -3
- package/dist/runner/model-route.js +139 -18
- package/dist/runner/monid-run.d.ts +8 -6
- package/dist/runner/monid-run.js +117 -43
- package/dist/runner/native-loop.d.ts +11 -0
- package/dist/runner/native-loop.js +438 -0
- package/dist/runner/promotion.d.ts +74 -8
- package/dist/runner/promotion.js +365 -124
- package/dist/runner/redact.js +3 -0
- package/dist/runner/reflect-hook.js +9 -0
- package/dist/runner/runner.d.ts +7 -1
- package/dist/runner/runner.js +8 -4
- package/dist/runner/sdk-loop.d.ts +7 -0
- package/dist/runner/sdk-loop.js +470 -0
- package/dist/runner/spec-holds.d.ts +16 -2
- package/dist/runner/spec-holds.js +42 -14
- package/dist/runner/store-secrets.js +17 -2
- package/dist/runner/workflow-first.d.ts +30 -20
- package/dist/runner/workflow-first.js +22 -32
- package/dist/runner/workflow-loop.d.ts +1 -1
- package/dist/runner/workflow-loop.js +18 -12
- package/dist/runner/workflow-spec.d.ts +4 -1
- package/dist/runner/workflow-spec.js +56 -45
- package/dist/runs-over-store.d.ts +30 -2
- package/dist/runs-over-store.js +46 -14
- package/dist/sdk/runs.d.ts +115 -0
- package/dist/sdk/runs.js +422 -0
- package/dist/sdk/schema.d.ts +175 -0
- package/dist/sdk/schema.js +138 -0
- package/dist/sdk/workflows.d.ts +22 -0
- package/dist/sdk/workflows.js +39 -0
- package/dist/secrets.d.ts +2 -0
- package/dist/secrets.js +19 -1
- package/dist/server.d.ts +48 -32
- package/dist/server.js +554 -130
- package/dist/skill-route.d.ts +3 -0
- package/dist/skill-route.js +16 -0
- package/dist/skills/agentrun-serve/SKILL.md +368 -0
- package/dist/stats.d.ts +10 -10
- package/dist/store/agent-body.d.ts +12 -8
- package/dist/store/agent-body.js +9 -4
- package/dist/store/index.d.ts +1 -1
- package/dist/store/native-command-owner.d.ts +10 -0
- package/dist/store/native-command-owner.js +47 -0
- package/dist/store/postgres/migrations/0012_revision_monid_pin.sql +4 -0
- package/dist/store/postgres/migrations/0013_native_command_owner.sql +6 -0
- package/dist/store/postgres/migrations/0014_run_cost_upper_bound.sql +6 -0
- package/dist/store/postgres/store.js +3 -0
- package/dist/store/schema.d.ts +7 -0
- package/dist/store/schema.js +20 -1
- package/dist/store/sqlite/run-patch.d.ts +8 -1
- package/dist/store/sqlite/run-patch.js +14 -3
- package/dist/store/sqlite/store.js +133 -18
- package/dist/store/store.d.ts +52 -7
- package/dist/store-reads/analytics.d.ts +7 -7
- package/dist/store-reads/analytics.js +23 -5
- package/dist/store-reads/cap.js +8 -6
- package/dist/store-reads/journal.d.ts +5 -0
- package/dist/store-reads/journal.js +85 -41
- package/dist/store-reads/learning.d.ts +85 -5
- package/dist/store-reads/learning.js +45 -17
- package/dist/store-reads/runs.d.ts +5 -0
- package/dist/store-reads/runs.js +3 -2
- package/dist/store-view.js +75 -21
- package/dist/template-eval.d.ts +73 -11
- package/dist/template-eval.js +535 -75
- package/dist/templates.d.ts +70 -20
- package/dist/templates.js +191 -34
- package/dist/text-urls.d.ts +38 -0
- package/dist/text-urls.js +170 -0
- package/dist/timeline/index.d.ts +3 -2
- package/dist/timeline/index.js +3 -2
- package/dist/timeline/labels.d.ts +5 -2
- package/dist/timeline/labels.js +29 -6
- package/dist/timeline/origin.d.ts +1 -1
- package/dist/timeline/origin.js +2 -2
- package/dist/timeline/render-text.js +1 -1
- package/dist/timeline/timeline.d.ts +66 -4
- package/dist/timeline/timeline.js +160 -30
- package/dist/timeline/util.d.ts +5 -0
- package/dist/timeline/util.js +44 -8
- package/dist/timeline/web-calls.d.ts +17 -0
- package/dist/timeline/web-calls.js +106 -0
- package/dist/tool-gate.d.ts +42 -42
- package/dist/tool-gate.js +97 -91
- package/dist/tool-list.d.ts +4 -1
- package/dist/tool-list.js +8 -1
- package/dist/tool-names.d.ts +18 -13
- package/dist/tool-names.js +19 -17
- package/dist/tool-plane.d.ts +17 -13
- package/dist/tool-plane.js +12 -5
- package/dist/tools.d.ts +44 -16
- package/dist/tools.js +28 -28
- package/dist/trigger-adapter.d.ts +85 -5
- package/dist/trigger-adapter.js +6 -0
- package/dist/trigger-adapters.js +3 -1
- package/dist/trigger-composio.d.ts +10 -0
- package/dist/trigger-composio.js +122 -0
- package/dist/trigger-email.d.ts +4 -0
- package/dist/trigger-email.js +228 -0
- package/dist/trigger-signatures.d.ts +6 -10
- package/dist/trigger-signatures.js +16 -16
- package/dist/trigger-targets.d.ts +1 -1
- package/dist/trigger-targets.js +4 -3
- package/dist/trigger-webhook.js +6 -4
- package/dist/triggers.d.ts +94 -9
- package/dist/triggers.js +263 -62
- package/dist/uploads.d.ts +9 -0
- package/dist/uploads.js +27 -0
- package/dist/validate.js +1 -7
- package/dist/web-fake.d.ts +56 -0
- package/dist/web-fake.js +102 -0
- package/dist/web-fetch-direct.d.ts +3 -0
- package/dist/web-fetch-direct.js +4 -1
- package/dist/web-fetch.d.ts +5 -1
- package/dist/web-fetch.js +8 -31
- package/dist/web-search.d.ts +51 -2
- package/dist/web-search.js +116 -18
- package/dist/webhooks.d.ts +2 -1
- package/dist/webhooks.js +5 -1
- package/dist/workflow/node-runner.js +10 -2
- package/dist/workspace/archil-claim.d.ts +3 -0
- package/dist/workspace/archil-claim.js +9 -2
- package/dist/workspace/computer-deps.d.ts +22 -9
- package/dist/workspace/computer-deps.js +78 -37
- package/dist/workspace/computer-for-claim.d.ts +54 -4
- package/dist/workspace/computer-for-claim.js +179 -39
- package/dist/workspace/computer-janitor.d.ts +10 -3
- package/dist/workspace/computer-janitor.js +96 -29
- package/dist/workspace/computer-kept.d.ts +82 -0
- package/dist/workspace/computer-kept.js +147 -0
- package/dist/workspace/computer-mcp.js +4 -3
- package/dist/workspace/computer-ports.d.ts +23 -3
- package/dist/workspace/computer-ports.js +115 -5
- package/dist/workspace/index.d.ts +1 -2
- package/dist/workspace/index.js +1 -2
- package/dist/workspace/kind-skills/desktop/SKILL.md +25 -18
- package/dist/workspace/kind-skills/phone/SKILL.md +28 -17
- package/dist/workspace/resolve-computer.d.ts +15 -3
- package/dist/workspace/resolve-computer.js +10 -7
- package/dist/workspace/revision-skills.d.ts +6 -2
- package/dist/workspace/revision-skills.js +12 -5
- package/dist/workspace/run-end-computers.d.ts +30 -0
- package/dist/workspace/run-end-computers.js +137 -0
- package/dist/workspace/run-tools.d.ts +28 -29
- package/dist/workspace/run-tools.js +29 -30
- package/dist/wrap-up.d.ts +19 -5
- package/dist/wrap-up.js +31 -4
- package/package.json +13 -9
- package/skills/agentrun-serve/SKILL.md +248 -10
- package/templates/README.md +117 -23
- package/templates/TOOLS.md +168 -0
- package/templates/code-review/agent.md +18 -4
- package/templates/code-review/evals/mentions-defused/case.yaml +1 -1
- package/templates/code-review/evals/off-by-one/graders/no-repo-reads.md +2 -1
- package/templates/code-review/evals/other-repo/case.yaml +1 -1
- package/templates/code-review/evals/pasted-by-reference/case.yaml +5 -0
- package/templates/code-review/evals/pasted-by-reference/graders/by-reference.md +15 -0
- package/templates/code-review/evals/pasted-by-reference/graders/head-sha.md +6 -0
- package/templates/code-review/evals/pasted-by-reference/prompt.md +31 -0
- package/templates/code-review/skills/review/DEEP.md +2 -2
- package/templates/code-review/skills/review/REVIEWER.md +2 -2
- package/templates/code-review/skills/review/SKILL.md +7 -3
- package/templates/release-notes/evals/draft-not-publish/case.yaml +1 -1
- package/templates/release-notes/evals/issue-write-refused/case.yaml +1 -1
- package/templates/release-notes/evals/late-arrival/case.yaml +1 -1
- package/templates/release-notes/evals/other-repo/case.yaml +1 -1
- package/dist/console/assets/index-CjqQirbo.css +0 -1
- package/dist/console/assets/index-b6-XY5A9.js +0 -576
- package/dist/host-calls.d.ts +0 -20
- package/dist/host-calls.js +0 -12
- package/dist/runner/adapters/pi-computer.d.ts +0 -9
- package/dist/runner/adapters/pi-computer.js +0 -51
- package/dist/workspace/computer-script.d.ts +0 -53
- package/dist/workspace/computer-script.js +0 -172
package/dist/triggers.js
CHANGED
|
@@ -26,7 +26,7 @@ import { TRIGGER_LABELS } from "./timeline/index.js";
|
|
|
26
26
|
import { ApiError, invalid } from "./errors.js";
|
|
27
27
|
import { EFFORTS } from "./model.js";
|
|
28
28
|
import { AgentArchived, StoreConstraint, TriggerFull } from "./store/index.js";
|
|
29
|
-
import { second, validZone } from "./trigger-adapter.js";
|
|
29
|
+
import { carriesPrevious, PAYLOAD_KEPT_BYTES, second, validZone } from "./trigger-adapter.js";
|
|
30
30
|
import { TRIGGER_ADAPTERS } from "./trigger-adapters.js";
|
|
31
31
|
import { MAX_EVERY_S } from "./trigger-schedule.js";
|
|
32
32
|
import { ID, IDEMPOTENCY_TTL_S, isObject, unknownFields } from "./validate.js";
|
|
@@ -43,8 +43,8 @@ const FULL_RETRY_S = 30;
|
|
|
43
43
|
export const SOURCE_KINDS = TRIGGER_ADAPTERS.map((a) => a.id);
|
|
44
44
|
export const TARGET_KINDS = ["start_run", "message_run"];
|
|
45
45
|
export const FIRING_STATUSES = ["due", "started", "sent", "skipped", "failed", "missed"];
|
|
46
|
-
/** The most of a webhook's payload a firing passes on: the rest is cut, at a character's start. */
|
|
47
|
-
export
|
|
46
|
+
/** The most of a webhook's payload a firing passes on: the rest is cut, at a character's start (trigger-adapter.ts). */
|
|
47
|
+
export { PAYLOAD_KEPT_BYTES };
|
|
48
48
|
/** How late a missed time may be and still fire once, by default: an hour. */
|
|
49
49
|
export const CATCHUP_WINDOW_S = 3600;
|
|
50
50
|
/** The failed tries in a row that turn a trigger off. */
|
|
@@ -70,6 +70,27 @@ export const SEEN_KEPT = 5_000;
|
|
|
70
70
|
/** The highest max_active_runs a trigger may set (the store's CHECK holds the same). */
|
|
71
71
|
export const MAX_ACTIVE_RUNS = 100;
|
|
72
72
|
const ENDED = new Set(["completed", "failed", "cancelled"]);
|
|
73
|
+
/** A polled source's trigger setting its last check is kept in (TriggerAdapter.poll), with the sign-ins refused in a row. */
|
|
74
|
+
export const LAST_CHECK = "last_check";
|
|
75
|
+
/** The sign-ins a polled source's server refuses in a row that turn its trigger off. */
|
|
76
|
+
export const REFUSALS_TO_STOP = 3;
|
|
77
|
+
/** How long a trigger's create, edit or turn-on waits for its source's start (trigger-adapter.ts `start`): one not taken
|
|
78
|
+
* by then is taken at its first check. */
|
|
79
|
+
export const START_WAIT_MS = 10_000;
|
|
80
|
+
/** Whether `adapter`'s firings are events from outside (pushed, or found by a poll), each its own, never a time. */
|
|
81
|
+
const eventful = (adapter) => adapter?.delivery === "push" || adapter?.poll !== undefined;
|
|
82
|
+
/** A polled trigger's last check as kept (LAST_CHECK), when it reads. */
|
|
83
|
+
function checkOf(text) {
|
|
84
|
+
if (!text)
|
|
85
|
+
return undefined;
|
|
86
|
+
try {
|
|
87
|
+
const value = JSON.parse(text);
|
|
88
|
+
return typeof value.at === "string" && typeof value.ok === "boolean" && typeof value.message === "string" ? { at: value.at, ok: value.ok, message: value.message, refused: Number.isSafeInteger(value.refused) ? value.refused : 0 } : undefined;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
73
94
|
/** Why a message_run trigger turned itself off: the run it messages is gone. */
|
|
74
95
|
const RUN_DELETED = "its run was deleted";
|
|
75
96
|
const REAL_CLOCK = {
|
|
@@ -142,29 +163,15 @@ function settingsOf(body, issues) {
|
|
|
142
163
|
}
|
|
143
164
|
const WRITABLE = ["name", "enabled", "source", "target", "catchup_window_s", "max_active_runs"];
|
|
144
165
|
/** Why a trigger of `source` and `target` takes no max_active_runs, if it does not: any start_run trigger of a pushed
|
|
145
|
-
* source does, with or without accept (set, each event starts its own run up to the cap); a schedule keeps
|
|
146
|
-
* skip, and a message_run target starts no run to bound. */
|
|
166
|
+
* or polled source does, with or without accept (set, each event starts its own run up to the cap); a schedule keeps
|
|
167
|
+
* the overlap skip, and a message_run target starts no run to bound. */
|
|
147
168
|
function capIssues(adapters, source, target) {
|
|
148
|
-
if (adapterIn(adapters, source.kind)
|
|
149
|
-
return [{ field: "max_active_runs", reason: "not_allowed", message: "Only a trigger of a pushed source (a webhook) takes max_active_runs: a schedule keeps the overlap skip. Set it to null to clear it." }];
|
|
169
|
+
if (!eventful(adapterIn(adapters, source.kind)))
|
|
170
|
+
return [{ field: "max_active_runs", reason: "not_allowed", message: "Only a trigger of a pushed or polled source (a webhook, an email) takes max_active_runs: a schedule keeps the overlap skip. Set it to null to clear it." }];
|
|
150
171
|
if (target?.kind === "message_run")
|
|
151
172
|
return [{ field: "max_active_runs", reason: "not_allowed", message: "max_active_runs bounds the runs a trigger starts, and a message_run target starts none. Set it to null to clear it." }];
|
|
152
173
|
return [];
|
|
153
174
|
}
|
|
154
|
-
/** What is wrong with the secret a rotation gives for a trigger of `source`: a source whose sender gives its secret
|
|
155
|
-
* (Composio's) needs one, a string (the store checks its length and form); any other takes none, it is minted. A create
|
|
156
|
-
* never takes one: Composio makes the secret once the trigger's URL is registered, so a composio trigger starts with none
|
|
157
|
-
* and refuses every request until it is set. */
|
|
158
|
-
function givenSecretIssues(adapter, source, secret) {
|
|
159
|
-
const gives = adapter?.givesSecret?.(source) === true;
|
|
160
|
-
if (gives && secret === undefined)
|
|
161
|
-
return [{ field: "secret", reason: "missing", message: "This trigger's sender gives its secret (Composio's webhook subscription secret): send it as secret." }];
|
|
162
|
-
if (!gives && secret !== undefined)
|
|
163
|
-
return [{ field: "secret", reason: "not_allowed", message: "This trigger's secret is made by this server: send no secret." }];
|
|
164
|
-
if (gives && typeof secret !== "string")
|
|
165
|
-
return [{ field: "secret", reason: "wrong_type", message: "secret is a string" }];
|
|
166
|
-
return [];
|
|
167
|
-
}
|
|
168
175
|
/** A POST /triggers body's own fields, read as create reads them with the sources of `adapters`: its source, target,
|
|
169
176
|
* settings, name and enabled, with every issue. What it names (the agent, the run) is create's own check
|
|
170
177
|
* (TriggerDeps.targetIssues). */
|
|
@@ -213,6 +220,10 @@ export class Triggers {
|
|
|
213
220
|
waiting = false;
|
|
214
221
|
/** The triggers whose day-kept keys are at their cap, said once until they are under it again. */
|
|
215
222
|
seenFull = new Set();
|
|
223
|
+
/** The polled triggers whose poll is running: one at a time each. */
|
|
224
|
+
polling = new Set();
|
|
225
|
+
/** Each polled trigger's count of starts taken (takeStart): a poll or a start that sees it changed keeps nothing. */
|
|
226
|
+
starts = new Map();
|
|
216
227
|
closed = false;
|
|
217
228
|
constructor(deps) {
|
|
218
229
|
this.deps = deps;
|
|
@@ -263,39 +274,44 @@ export class Triggers {
|
|
|
263
274
|
const id = `trg_${randomBytes(12).toString("hex")}`;
|
|
264
275
|
const enabled = body.enabled !== false;
|
|
265
276
|
if (enabled)
|
|
266
|
-
this.needAvailable(source.kind);
|
|
277
|
+
await this.needAvailable(source.kind);
|
|
267
278
|
// Never made on for an archived agent: told before anything is minted (the store's own refusal is the backstop).
|
|
268
279
|
if (enabled)
|
|
269
280
|
await this.needAgentOn(agentId);
|
|
270
281
|
// A scope belongs to one trigger: told before anything is minted or written (the store's scope rows are the backstop).
|
|
271
282
|
if (enabled)
|
|
272
283
|
await this.scopeFree(source, id);
|
|
273
|
-
// Its secrets are kept before the trigger is: one whose secret cannot be kept makes none.
|
|
274
|
-
const minted = await this.mint(source, id);
|
|
275
284
|
const name = typeof body.name === "string" ? body.name.trim() : id;
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
285
|
+
const keep = async (kept) => {
|
|
286
|
+
try {
|
|
287
|
+
// Its secrets are kept before the trigger is: one whose secret cannot be kept makes none.
|
|
288
|
+
const minted = await this.mint(kept, id);
|
|
289
|
+
const row = await this.store.triggers.create({ id, agentId, name, enabled, source: kept, target, nextAt: enabled ? nextAfter(this.adapter(kept), kept, now) : null, catchupWindowS: settings.catchup_window_s ?? CATCHUP_WINDOW_S, ...(typeof settings.max_active_runs === "number" ? { maxActiveRuns: settings.max_active_runs } : {}) }, this.scopesOf(kept));
|
|
290
|
+
return { row, minted };
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
throw (await this.scopeTaken(error, kept, id)) ?? (await this.archivedRefusal(error)) ?? nameTaken(error, name);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
// What its sender keeps of it is made first (Composio's instance), the trigger saved after, and what was made undone
|
|
297
|
+
// there when it is not saved (the sender's own steps, one project's at a time).
|
|
298
|
+
const upstream = this.deps.upstream?.(source.kind);
|
|
299
|
+
const { row, minted } = upstream ? await upstream.make(source, id, enabled, keep) : await keep(source);
|
|
300
|
+
// Where a polled source starts watching: from now, taken before its first time is armed.
|
|
301
|
+
if (enabled)
|
|
302
|
+
await this.takeStart(asTrigger(row), "created");
|
|
283
303
|
// Armed before the answer: a create that answered is in effect, its first time on the timer (arm reads the store).
|
|
284
304
|
await this.arm();
|
|
285
305
|
// What it minted, shown this once, by field: a webhook's `secret`.
|
|
286
306
|
return { ...(await this.view(asTrigger(row))), ...minted };
|
|
287
307
|
}
|
|
288
|
-
/** A trigger's new signing secret, shown this once; the old one signs nothing from now on.
|
|
289
|
-
|
|
290
|
-
async rotate(id, given) {
|
|
308
|
+
/** A trigger's new signing secret, shown this once; the old one signs nothing from now on. */
|
|
309
|
+
async rotate(id) {
|
|
291
310
|
const trigger = await this.need(id);
|
|
292
311
|
const adapter = this.adapter(trigger.source);
|
|
293
312
|
if (!mints(adapter))
|
|
294
313
|
throw invalid([{ field: "source.kind", reason: "not_allowed", message: "Only a webhook trigger has a secret." }]);
|
|
295
|
-
|
|
296
|
-
if (issues.length)
|
|
297
|
-
throw invalid(issues);
|
|
298
|
-
return this.mint(trigger.source, id, given);
|
|
314
|
+
return this.mint(trigger.source, id);
|
|
299
315
|
}
|
|
300
316
|
/** Whether `id` is a trigger of the pushed source `kind` that is on: the one a request delivered for `kind` may fire. */
|
|
301
317
|
async hookable(id, kind) {
|
|
@@ -322,6 +338,11 @@ export class Triggers {
|
|
|
322
338
|
async listening(kind) {
|
|
323
339
|
return (await this.store.triggers.all()).map(asTrigger).filter((t) => t.enabled && t.source.kind === kind).map((t) => ({ id: t.id, source: t.source }));
|
|
324
340
|
}
|
|
341
|
+
/** Every trigger of source `kind`, on or off, with its name and source: what a source's sender keeps of them is shared
|
|
342
|
+
* between some (composio-triggers.ts: one Composio instance per server, event and config). */
|
|
343
|
+
async ofKind(kind) {
|
|
344
|
+
return (await this.store.triggers.all()).map(asTrigger).filter((t) => t.source.kind === kind).map((t) => ({ id: t.id, name: t.name, enabled: t.enabled, source: t.source }));
|
|
345
|
+
}
|
|
325
346
|
/** Every trigger of source `kind` that is on, turned off because the connection it needs is gone (`disabled: {reason:
|
|
326
347
|
* "connection_lost", detail}`), and said; how many. They are kept: on again once it is back. */
|
|
327
348
|
async turnOff(kind, detail) {
|
|
@@ -333,6 +354,7 @@ export class Triggers {
|
|
|
333
354
|
}
|
|
334
355
|
if (on.length)
|
|
335
356
|
await this.arm();
|
|
357
|
+
await this.offThere(on);
|
|
336
358
|
return on.length;
|
|
337
359
|
}
|
|
338
360
|
/** Agent `agentId` archived, and every trigger that runs as it (its rows: a start_run of it, a message_run to one of its
|
|
@@ -345,6 +367,7 @@ export class Triggers {
|
|
|
345
367
|
this.deps.say({ condition: "trigger_agent_archived", trigger: t.id, agent: agentId });
|
|
346
368
|
if (off.length)
|
|
347
369
|
await this.arm();
|
|
370
|
+
await this.offThere((await Promise.all(off.map((t) => this.find(t.id)))).filter((t) => t !== undefined));
|
|
348
371
|
return off;
|
|
349
372
|
}
|
|
350
373
|
/** Agent `agentId` unarchived: none of its triggers turns on again. The triggers its archive turned off that are off
|
|
@@ -372,8 +395,8 @@ export class Triggers {
|
|
|
372
395
|
return agent?.archivedAt != null ? `The agent ${agent.name} is archived.` : undefined;
|
|
373
396
|
}
|
|
374
397
|
/** Refused as its connection says when a trigger of `kind` cannot be on here (unavailable). */
|
|
375
|
-
needAvailable(kind) {
|
|
376
|
-
const refusal = this.deps.unavailable?.(kind);
|
|
398
|
+
async needAvailable(kind) {
|
|
399
|
+
const refusal = await this.deps.unavailable?.(kind);
|
|
377
400
|
if (refusal)
|
|
378
401
|
throw refusal;
|
|
379
402
|
}
|
|
@@ -412,7 +435,7 @@ export class Triggers {
|
|
|
412
435
|
async update(id, body) {
|
|
413
436
|
const was = await this.need(id);
|
|
414
437
|
const issues = unknownFields(body, WRITABLE);
|
|
415
|
-
|
|
438
|
+
let source = body.source === undefined ? undefined : sourceOf(this.adapters, body.source, this.clock.now(), issues);
|
|
416
439
|
const target = body.target === undefined ? undefined : targetOf(body.target, issues);
|
|
417
440
|
const settings = settingsOf(body, issues);
|
|
418
441
|
// A cap kept, or set, needs a pushed source and a target that starts runs: a PATCH that aims a capped trigger at a run
|
|
@@ -431,9 +454,10 @@ export class Triggers {
|
|
|
431
454
|
const trigger = await this.need(id);
|
|
432
455
|
if (source && source.kind !== trigger.source.kind)
|
|
433
456
|
throw invalid([{ field: "source.kind", reason: "not_allowed", message: "A trigger's source kind is fixed: make a new trigger for another." }]);
|
|
434
|
-
//
|
|
435
|
-
|
|
436
|
-
|
|
457
|
+
// What its sender keeps of it stays as it was made (Composio's instance): only what reads it here changes.
|
|
458
|
+
const upstream = this.deps.upstream?.(trigger.source.kind);
|
|
459
|
+
if (source && upstream)
|
|
460
|
+
source = upstream.patched(trigger.source, source);
|
|
437
461
|
const now = this.clock.now();
|
|
438
462
|
const enabled = typeof body.enabled === "boolean" ? body.enabled : trigger.enabled;
|
|
439
463
|
const adapter = this.adapter(trigger.source);
|
|
@@ -447,7 +471,7 @@ export class Triggers {
|
|
|
447
471
|
const again = enabled && !trigger.enabled ? { failuresInARow: 0, disabled: null } : {};
|
|
448
472
|
const scopeSource = source ?? trigger.source;
|
|
449
473
|
if (enabled && (!trigger.enabled || source))
|
|
450
|
-
this.needAvailable(scopeSource.kind);
|
|
474
|
+
await this.needAvailable(scopeSource.kind);
|
|
451
475
|
if (enabled)
|
|
452
476
|
await this.scopeFree(scopeSource, trigger.id);
|
|
453
477
|
// A trigger belongs to its target's agent: aimed at another agent (or another agent's run), it moves to that agent,
|
|
@@ -467,15 +491,71 @@ export class Triggers {
|
|
|
467
491
|
});
|
|
468
492
|
if (!row)
|
|
469
493
|
throw new ApiError("not_found", `no trigger ${id}`);
|
|
494
|
+
// A polled source with a start (trigger-adapter.ts) takes a new one when turned on again or when the edit changed
|
|
495
|
+
// where it reads; any other edit keeps its cursor. One without forgets its cursor and last check: its next poll its first.
|
|
496
|
+
const start = adapter.start;
|
|
497
|
+
const edited = source;
|
|
498
|
+
const moved = edited !== undefined && start !== undefined && start.on.some((field) => JSON.stringify(edited[field]) !== JSON.stringify(trigger.source[field]));
|
|
499
|
+
if (enabled && start && (!trigger.enabled || moved))
|
|
500
|
+
await this.takeStart(asTrigger(row), trigger.enabled ? "edited" : "enabled");
|
|
501
|
+
else if (enabled && !start && (!trigger.enabled || source) && adapter.poll)
|
|
502
|
+
await this.deps.settings(adapter.id)?.remove(id);
|
|
470
503
|
await this.arm();
|
|
471
|
-
|
|
504
|
+
// Turned on or off by the owner: the same at its sender, once it holds here; what could not be done there is said.
|
|
505
|
+
const said = upstream && enabled !== trigger.enabled ? await upstream.turned(row.source, id, enabled) : undefined;
|
|
506
|
+
return { ...(await this.view(asTrigger(row))), ...said };
|
|
472
507
|
}
|
|
508
|
+
/** Trigger `trigger`'s polled source started from now (its adapter's start, waited on at most START_WAIT_MS): its old
|
|
509
|
+
* cursor and last check forgotten, the start's cursor and check kept (no refusal counted), its note a skipped history
|
|
510
|
+
* line. A start not taken leaves no cursor, so its first poll takes it. Never throws: the create or edit stands. */
|
|
511
|
+
async takeStart(trigger, why) {
|
|
512
|
+
const adapter = this.adapter(trigger.source);
|
|
513
|
+
const start = adapter.start;
|
|
514
|
+
if (!start)
|
|
515
|
+
return;
|
|
516
|
+
const generation = (this.starts.get(trigger.id) ?? 0) + 1;
|
|
517
|
+
this.starts.set(trigger.id, generation);
|
|
518
|
+
const now = this.clock.now();
|
|
519
|
+
try {
|
|
520
|
+
const settings = this.deps.settings(adapter.id);
|
|
521
|
+
await settings?.remove(trigger.id);
|
|
522
|
+
let timer;
|
|
523
|
+
const late = new Promise((done) => {
|
|
524
|
+
timer = setTimeout(() => done({ check: { ok: false, message: start.unchecked(trigger.source, why) }, note: start.unchecked(trigger.source, why) }), START_WAIT_MS);
|
|
525
|
+
timer.unref?.();
|
|
526
|
+
});
|
|
527
|
+
const taken = start.take(trigger.source, { trigger: trigger.id, why, credentials: async () => this.deps.credentials?.(adapter.id) }).catch((error) => {
|
|
528
|
+
const message = String(error?.message ?? error);
|
|
529
|
+
return { check: { ok: false, message }, note: start.unchecked(trigger.source, why) };
|
|
530
|
+
});
|
|
531
|
+
const result = await Promise.race([taken, late]).finally(() => clearTimeout(timer));
|
|
532
|
+
// Turned off, deleted, moved (an edit of where it reads: start.on) or started again meanwhile: this start is not its
|
|
533
|
+
// own any more. An edit of anything else (accept, every_s) keeps it: that edit took no start of its own, and the
|
|
534
|
+
// cursor removed above would otherwise stay gone, its first poll starting from then and skipping the mail between.
|
|
535
|
+
const current = await this.find(trigger.id);
|
|
536
|
+
const readsAt = (source) => [source.kind, ...start.on.map((field) => source[field])];
|
|
537
|
+
if (this.closed || this.starts.get(trigger.id) !== generation || !current?.enabled || JSON.stringify(readsAt(current.source)) !== JSON.stringify(readsAt(trigger.source)))
|
|
538
|
+
return;
|
|
539
|
+
for (const [field, value] of Object.entries(result.cursor ?? {}))
|
|
540
|
+
await settings?.set(trigger.id, field, value);
|
|
541
|
+
await settings?.set(trigger.id, LAST_CHECK, JSON.stringify({ at: iso(now), ok: result.check.ok, message: result.check.message, refused: 0 }));
|
|
542
|
+
const at = Math.floor(now / 1000) * 1000;
|
|
543
|
+
await this.record(current, { key: firingKey(adapter.id, trigger.id, `start\n${second(now)}\n${why}\n${generation}`), by: adapter.id, nominalAt: at, status: "skipped", reason: result.note });
|
|
544
|
+
if (!result.check.ok)
|
|
545
|
+
this.deps.say({ condition: "trigger_check_failed", trigger: trigger.id, error: result.check.message });
|
|
546
|
+
}
|
|
547
|
+
catch (error) {
|
|
548
|
+
this.deps.say({ condition: "trigger_start_unrecorded", trigger: trigger.id, error: String(error?.message ?? error) });
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/** The trigger deleted, and what its sender keeps of it removed after (said, when it could not be: the delete holds). */
|
|
473
552
|
async remove(id) {
|
|
474
553
|
const trigger = await this.need(id);
|
|
475
554
|
await this.store.triggers.remove(id);
|
|
476
555
|
if (this.adapter(trigger.source)?.settings?.scope === "trigger")
|
|
477
556
|
await this.deps.settings(trigger.source.kind)?.remove(id);
|
|
478
557
|
await this.arm();
|
|
558
|
+
return this.deps.upstream?.(trigger.source.kind)?.removed(trigger.source, id);
|
|
479
559
|
}
|
|
480
560
|
/** Run now: one firing per Idempotency-Key, driven before the answer. `created` false: the key's firing, as it stands. */
|
|
481
561
|
async fire(id, idempotencyKey) {
|
|
@@ -550,6 +630,7 @@ export class Triggers {
|
|
|
550
630
|
await this.store.triggers.update(t.id, { enabled: false, nextAt: null, disabled: { at: iso(now), reason: "source_unreadable", detail } });
|
|
551
631
|
this.deps.say({ condition: "trigger_source_unreadable", trigger: t.id, error: detail });
|
|
552
632
|
}
|
|
633
|
+
await this.offThere(triggers);
|
|
553
634
|
}
|
|
554
635
|
/** Records `trigger`'s firings up to now and moves its next time on from the last nominal one, in one write. Of the
|
|
555
636
|
* times that have come, the latest fires (on time, or late within catchup_window_s: a catch-up, under its own nominal
|
|
@@ -561,6 +642,8 @@ export class Triggers {
|
|
|
561
642
|
const clock = adapter.clock?.(trigger.source);
|
|
562
643
|
if (!clock)
|
|
563
644
|
return;
|
|
645
|
+
if (adapter.poll)
|
|
646
|
+
return this.pollDue(trigger, adapter, clock, now);
|
|
564
647
|
const by = adapter.id;
|
|
565
648
|
const windowMs = (trigger.catchupWindowS ?? CATCHUP_WINDOW_S) * 1000;
|
|
566
649
|
const first = trigger.nextAt;
|
|
@@ -598,6 +681,93 @@ export class Triggers {
|
|
|
598
681
|
await this.store.triggers.record(trigger.id, firings, { patch: { nextAt: next, ...(next === null ? { enabled: false } : {}) } });
|
|
599
682
|
await this.prune(trigger);
|
|
600
683
|
}
|
|
684
|
+
/** A polled source's time come: its next time moved on from the latest that has come (a time it was not there for is no
|
|
685
|
+
* `missed` row: what came meanwhile is still there to read), then its poll, after none still running for it, off the
|
|
686
|
+
* timer, so a slow server never holds the other triggers. */
|
|
687
|
+
async pollDue(trigger, adapter, clock, now) {
|
|
688
|
+
const last = clock.latest(trigger.nextAt, now);
|
|
689
|
+
const next = clock.next(last);
|
|
690
|
+
await this.store.triggers.update(trigger.id, { nextAt: next !== null && Number.isFinite(next) && Math.abs(next) <= DATE_MAX_MS ? next : null });
|
|
691
|
+
if (this.polling.has(trigger.id))
|
|
692
|
+
return;
|
|
693
|
+
this.polling.add(trigger.id);
|
|
694
|
+
void this.polled(trigger, adapter, last, now).finally(() => this.polling.delete(trigger.id));
|
|
695
|
+
}
|
|
696
|
+
/** One poll of `trigger`'s source for its time `nominalAt`: each event it found recorded in order (its note a skipped
|
|
697
|
+
* history line; one it leaves, skipped and said; one its accept does not take, an ignored row kept for its day; any
|
|
698
|
+
* other due, and driven), its cursor kept once they are, then its last check, and at the REFUSALS_TO_STOPth refused
|
|
699
|
+
* sign-in in a row the trigger turned off. A poll that throws is a failed check. */
|
|
700
|
+
async polled(trigger, adapter, nominalAt, now) {
|
|
701
|
+
const settings = this.deps.settings(adapter.id);
|
|
702
|
+
// A start taken while it polled (an edit, a turn-on) is newer than anything this poll found.
|
|
703
|
+
const generation = this.starts.get(trigger.id);
|
|
704
|
+
const restarted = () => this.starts.get(trigger.id) !== generation;
|
|
705
|
+
let result;
|
|
706
|
+
try {
|
|
707
|
+
result = await adapter.poll(trigger.source, { trigger: trigger.id, setting: (field) => settings?.get(trigger.id, field), credentials: async () => this.deps.credentials?.(adapter.id) });
|
|
708
|
+
}
|
|
709
|
+
catch (error) {
|
|
710
|
+
result = { check: { ok: false, message: String(error?.message ?? error) } };
|
|
711
|
+
}
|
|
712
|
+
if (this.closed)
|
|
713
|
+
return;
|
|
714
|
+
try {
|
|
715
|
+
// Edited, turned off or deleted while it polled: nothing it found is kept.
|
|
716
|
+
const current = await this.find(trigger.id);
|
|
717
|
+
if (!current || !current.enabled || JSON.stringify(current.source) !== JSON.stringify(trigger.source) || restarted())
|
|
718
|
+
return;
|
|
719
|
+
const by = adapter.id;
|
|
720
|
+
const at = Math.floor(nominalAt / 1000) * 1000;
|
|
721
|
+
if (result.note)
|
|
722
|
+
await this.record(current, { key: firingKey(by, trigger.id, `note\n${second(nominalAt)}`), by, nominalAt: at, status: "skipped", reason: result.note });
|
|
723
|
+
const events = result.events ?? [];
|
|
724
|
+
let cursor = events.length ? undefined : result.cursor;
|
|
725
|
+
let full = false;
|
|
726
|
+
for (const event of events) {
|
|
727
|
+
const key = firingKey(by, trigger.id, event.key);
|
|
728
|
+
const attrs = event.attrs && Object.keys(event.attrs).length ? { attrs: event.attrs } : {};
|
|
729
|
+
const refused = event.left ? undefined : adapter.takes?.(current.source, event);
|
|
730
|
+
try {
|
|
731
|
+
if (event.left)
|
|
732
|
+
await this.record(current, { key, by, nominalAt: at, status: "skipped", reason: event.left, ...attrs });
|
|
733
|
+
else if (refused)
|
|
734
|
+
await this.record(current, { key, by, nominalAt: at, status: "ignored", shown: false, ...attrs });
|
|
735
|
+
else {
|
|
736
|
+
const recorded = await this.record(current, { key, by, nominalAt: at, status: "due", payload: cut(event.text), ...attrs }, { room: true });
|
|
737
|
+
if (recorded.created)
|
|
738
|
+
void this.inTurn(trigger.id, key);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
catch (error) {
|
|
742
|
+
// Full (DUE_KEPT firings still due): this event and those after it wait for the next poll, the cursor before it.
|
|
743
|
+
if (!(error instanceof ApiError && error.code === "rate_limited"))
|
|
744
|
+
throw error;
|
|
745
|
+
full = true;
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
cursor = event.cursor;
|
|
749
|
+
}
|
|
750
|
+
if (!full && events.length)
|
|
751
|
+
cursor = result.cursor ?? cursor;
|
|
752
|
+
if (restarted())
|
|
753
|
+
return;
|
|
754
|
+
for (const [field, value] of Object.entries(cursor ?? {}))
|
|
755
|
+
await settings?.set(trigger.id, field, value);
|
|
756
|
+
const refusals = result.refused ? (checkOf(settings?.get(trigger.id, LAST_CHECK))?.refused ?? 0) + 1 : 0;
|
|
757
|
+
if (result.refused && refusals >= REFUSALS_TO_STOP) {
|
|
758
|
+
await this.store.triggers.update(trigger.id, { enabled: false, nextAt: null, disabled: { at: iso(this.clock.now()), reason: "sign_in_refused", detail: result.refused.detail } });
|
|
759
|
+
this.deps.say({ condition: "trigger_sign_in_refused", trigger: trigger.id, refused: refusals });
|
|
760
|
+
await this.arm();
|
|
761
|
+
}
|
|
762
|
+
const message = full ? `${result.check.message} ${DUE_KEPT} firings are still waiting for their runs: the rest wait for the next check.` : result.check.message;
|
|
763
|
+
await settings?.set(trigger.id, LAST_CHECK, JSON.stringify({ at: iso(now), ok: result.check.ok, message, refused: refusals }));
|
|
764
|
+
if (!result.check.ok)
|
|
765
|
+
this.deps.say({ condition: "trigger_check_failed", trigger: trigger.id, error: result.check.message });
|
|
766
|
+
}
|
|
767
|
+
catch (error) {
|
|
768
|
+
this.deps.say({ condition: "trigger_poll_unrecorded", trigger: trigger.id, error: String(error?.message ?? error) });
|
|
769
|
+
}
|
|
770
|
+
}
|
|
601
771
|
/** A firing taken from due to its end: skipped when its trigger's last run is active, else its run started (or its
|
|
602
772
|
* message sent). A failed try keeps it due under the backoff table, or ends it failed. Each step reads the trigger and
|
|
603
773
|
* the firing again: either may be edited or deleted meanwhile. */
|
|
@@ -641,7 +811,8 @@ export class Triggers {
|
|
|
641
811
|
return void (await this.send(now, firing));
|
|
642
812
|
let started;
|
|
643
813
|
try {
|
|
644
|
-
|
|
814
|
+
const attrs = firing.attrs;
|
|
815
|
+
started = await this.deps.start(now.target, this.inputOf(now, firing), { id: triggerId, firing_key: key, name: now.name, ...(attrs && Object.keys(attrs).length ? { attrs } : {}) }, this.originOf(now), this.titleOf(now, firing), carriesPrevious(this.adapter(now.source)));
|
|
645
816
|
}
|
|
646
817
|
catch (error) {
|
|
647
818
|
// Stopping, the start may have been cut by the close: the firing stays due for the next start, whose origin key
|
|
@@ -677,9 +848,10 @@ export class Triggers {
|
|
|
677
848
|
await this.store.triggers.update(trigger.id, { enabled: false, nextAt: null, disabled: { at: iso(this.clock.now()), reason: "source_unreadable", detail: RUN_DELETED } });
|
|
678
849
|
await this.store.triggers.settle(trigger.id, firing.key, { status: "skipped", reason: RUN_DELETED });
|
|
679
850
|
this.deps.say({ condition: "trigger_source_unreadable", trigger: trigger.id, error: RUN_DELETED });
|
|
851
|
+
await this.offThere([trigger]);
|
|
680
852
|
return;
|
|
681
853
|
}
|
|
682
|
-
const bound = adapterIn(this.adapters, firing.by)?.delivery === "clock" ? { max: 1, reason: "A message from this trigger is already waiting." } : { max: DUE_KEPT, reason: `${DUE_KEPT} messages from this trigger are already waiting for its run.` };
|
|
854
|
+
const bound = adapterIn(this.adapters, firing.by)?.delivery === "clock" && !eventful(adapterIn(this.adapters, firing.by)) ? { max: 1, reason: "A message from this trigger is already waiting." } : { max: DUE_KEPT, reason: `${DUE_KEPT} messages from this trigger are already waiting for its run.` };
|
|
683
855
|
let skipped;
|
|
684
856
|
try {
|
|
685
857
|
skipped = (await this.deps.message(target, this.inputOf(trigger, firing), firing.key, this.originOf(trigger), bound))?.skipped;
|
|
@@ -742,8 +914,10 @@ export class Triggers {
|
|
|
742
914
|
if (attempts <= BACKOFF_MS.length) {
|
|
743
915
|
const wait = Math.max(BACKOFF_MS[attempts - 1], error instanceof ApiError ? (error.extra.retry_after_ms ?? 0) : 0);
|
|
744
916
|
const settled = await this.store.triggers.settle(triggerId, key, { attempts, retryAt: this.clock.now() + wait, error: errorOf(error), reason: errorOf(error).message, waitsOn: null }, { failure: true, pauseAt: FAILURES_TO_PAUSE });
|
|
745
|
-
if (settled?.paused)
|
|
917
|
+
if (settled?.paused) {
|
|
746
918
|
this.deps.say({ condition: "trigger_auto_paused", trigger: triggerId, firing: key });
|
|
919
|
+
await this.pausedThere(triggerId);
|
|
920
|
+
}
|
|
747
921
|
return;
|
|
748
922
|
}
|
|
749
923
|
await this.failed(triggerId, key, errorOf(error));
|
|
@@ -769,8 +943,37 @@ export class Triggers {
|
|
|
769
943
|
/** A failed firing: one more in its trigger's streak, the trigger turned off at FAILURES_TO_PAUSE, and trigger.failed
|
|
770
944
|
* queued (after the firing is written: a start finds one not yet queued and queues it, under the same webhook-id). */
|
|
771
945
|
async failed(triggerId, key, error, runId, reason) {
|
|
772
|
-
await this.store.triggers.settle(triggerId, key, { status: "failed", error, ...(runId ? { runId } : {}), ...(reason ? { reason } : {}) }, { failure: true, pauseAt: FAILURES_TO_PAUSE });
|
|
946
|
+
const settled = await this.store.triggers.settle(triggerId, key, { status: "failed", error, ...(runId ? { runId } : {}), ...(reason ? { reason } : {}) }, { failure: true, pauseAt: FAILURES_TO_PAUSE });
|
|
773
947
|
await this.notifyFailed(triggerId, key);
|
|
948
|
+
if (settled?.paused)
|
|
949
|
+
await this.pausedThere(triggerId);
|
|
950
|
+
}
|
|
951
|
+
/** Trigger `triggerId`, turned off by its failure streak: off at its sender too (offThere). */
|
|
952
|
+
async pausedThere(triggerId) {
|
|
953
|
+
const trigger = await this.find(triggerId);
|
|
954
|
+
if (trigger && !trigger.enabled)
|
|
955
|
+
await this.offThere([trigger]);
|
|
956
|
+
}
|
|
957
|
+
/** Triggers the server turned off itself (their agent archived, a failure streak, a connection or a run gone, a source
|
|
958
|
+
* that no longer reads): off at their sender too, as an owner's pause is, by its own rule (Composio's instance only
|
|
959
|
+
* once no trigger holding it is on). What could not be done there is said, with what to do by hand; the change here
|
|
960
|
+
* holds either way. */
|
|
961
|
+
async offThere(triggers) {
|
|
962
|
+
for (const t of triggers) {
|
|
963
|
+
const upstream = this.deps.upstream?.(t.source.kind);
|
|
964
|
+
if (!upstream)
|
|
965
|
+
continue;
|
|
966
|
+
try {
|
|
967
|
+
const said = await upstream.turned(t.source, t.id, false);
|
|
968
|
+
for (const [where, what] of Object.entries(isObject(said) ? said : {})) {
|
|
969
|
+
if (isObject(what) && typeof what.note === "string")
|
|
970
|
+
this.deps.say({ condition: "trigger_off_upstream_failed", trigger: t.id, upstream: where, note: what.note });
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
catch (error) {
|
|
974
|
+
this.deps.say({ condition: "trigger_off_upstream_failed", trigger: t.id, upstream: t.source.kind, note: String(error?.message ?? error) });
|
|
975
|
+
}
|
|
976
|
+
}
|
|
774
977
|
}
|
|
775
978
|
async notifyFailed(triggerId, key) {
|
|
776
979
|
const firing = await this.store.triggers.firing(triggerId, key);
|
|
@@ -811,7 +1014,7 @@ export class Triggers {
|
|
|
811
1014
|
* most SEEN_KEPT of them; past it the oldest go, said once each time the cap is reached. */
|
|
812
1015
|
async prune(trigger) {
|
|
813
1016
|
const cap = this.deps.seenKept ?? SEEN_KEPT;
|
|
814
|
-
const over = await this.store.triggers.prune(trigger.id, { history: HISTORY_KEPT, dayMs: DAY_MS, keepByKey: (by) => by === "manual" || adapterIn(this.adapters, by)
|
|
1017
|
+
const over = await this.store.triggers.prune(trigger.id, { history: HISTORY_KEPT, dayMs: DAY_MS, keepByKey: (by) => by === "manual" || eventful(adapterIn(this.adapters, by)), kept: cap });
|
|
815
1018
|
if (over > 0) {
|
|
816
1019
|
if (!this.seenFull.has(trigger.id))
|
|
817
1020
|
this.deps.say({ condition: "trigger_seen_full", trigger: trigger.id, kept: cap });
|
|
@@ -870,9 +1073,12 @@ export class Triggers {
|
|
|
870
1073
|
async view(trigger) {
|
|
871
1074
|
const [last] = await this.store.triggers.firings(trigger.id);
|
|
872
1075
|
const adapter = adapterIn(this.adapters, trigger.source.kind);
|
|
873
|
-
const hook = adapter?.push ? `/v2/hooks/${adapter.settings?.scope === "server" ? adapter.id : trigger.id}` : undefined;
|
|
1076
|
+
const hook = adapter?.push ? `/v2/hooks/${adapter.hookOf?.(trigger.source) ?? (adapter.settings?.scope === "server" ? adapter.id : trigger.id)}` : undefined;
|
|
1077
|
+
// A polled source's last check: when, whether it read, in words; null before its first.
|
|
1078
|
+
const check = adapter?.poll ? checkOf(this.deps.settings(adapter.id)?.get(trigger.id, LAST_CHECK)) : undefined;
|
|
874
1079
|
return {
|
|
875
1080
|
...(hook ? { hook_url: `${this.deps.publicUrl ?? ""}${hook}` } : {}),
|
|
1081
|
+
...(adapter?.poll ? { last_check: check ? { at: check.at, ok: check.ok, message: check.message } : null } : {}),
|
|
876
1082
|
id: trigger.id, name: trigger.name, enabled: trigger.enabled, source: trigger.source, target: trigger.target, catchup_window_s: trigger.catchupWindowS ?? CATCHUP_WINDOW_S,
|
|
877
1083
|
max_active_runs: trigger.maxActiveRuns ?? null, failures_in_a_row: trigger.failuresInARow, disabled: trigger.disabled ?? null, next_at: trigger.nextAt === null ? null : iso(trigger.nextAt), last_run_id: trigger.lastRunId,
|
|
878
1084
|
created_at: iso(trigger.createdAt), updated_at: iso(trigger.updatedAt), last_firing: last ? firingView(last) : null,
|
|
@@ -885,21 +1091,13 @@ export class Triggers {
|
|
|
885
1091
|
}
|
|
886
1092
|
/** Each field `source`'s adapter mints for its triggers, made now for trigger `id`, by field: none for one that keeps
|
|
887
1093
|
* no trigger-scoped secret. */
|
|
888
|
-
async mint(source, id
|
|
1094
|
+
async mint(source, id) {
|
|
889
1095
|
const adapter = this.adapter(source);
|
|
890
1096
|
if (!mints(adapter))
|
|
891
1097
|
return {};
|
|
892
1098
|
const store = this.deps.settings(adapter.id);
|
|
893
1099
|
if (!store)
|
|
894
1100
|
throw new Error(`no sealed settings for ${adapter.id}`);
|
|
895
|
-
// The sender's own secret, sealed as a minted one is, and never answered; none yet, none made.
|
|
896
|
-
if (adapter.givesSecret?.(source)) {
|
|
897
|
-
if (given === undefined)
|
|
898
|
-
return {};
|
|
899
|
-
const field = Object.entries(adapter.settings.fields).find(([, f]) => f.mint)[0];
|
|
900
|
-
await store.set(id, field, given, "utf8");
|
|
901
|
-
return {};
|
|
902
|
-
}
|
|
903
1101
|
return Object.fromEntries(await Promise.all(Object.entries(adapter.settings.fields).filter(([, f]) => f.mint).map(async ([field]) => [field, await store.mint(id, field)])));
|
|
904
1102
|
}
|
|
905
1103
|
/** A scope belongs to one trigger: `source`'s (its adapter's scopeOf), for a trigger `id` that is to be on, is refused
|
|
@@ -948,6 +1146,9 @@ export class Triggers {
|
|
|
948
1146
|
titleOf(trigger, firing) {
|
|
949
1147
|
const source = TRIGGER_LABELS[trigger.source.kind] ?? trigger.source.kind;
|
|
950
1148
|
const attrs = firing.attrs;
|
|
1149
|
+
const own = attrs && firing.by === trigger.source.kind ? this.adapter(trigger.source).title?.(attrs) : undefined;
|
|
1150
|
+
if (own)
|
|
1151
|
+
return `${source} ${trigger.name}: ${own}`;
|
|
951
1152
|
const event = attrs?.event;
|
|
952
1153
|
const action = attrs?.action;
|
|
953
1154
|
return `${source} ${trigger.name}${event ? `: ${event}${action ? ` ${action}` : ""}` : ""}`;
|
|
@@ -964,7 +1165,7 @@ export class Triggers {
|
|
|
964
1165
|
const adapter = this.adapter(trigger.source);
|
|
965
1166
|
const when = local(firing.nominalAt, adapter.zone?.(trigger.source) ?? "UTC");
|
|
966
1167
|
const what = `the ${adapter.label} "${trigger.name}"`;
|
|
967
|
-
const line = firing.by === "manual" ? `(Started by hand from ${what} at ${when}.)` : adapterIn(this.adapters, firing.by)
|
|
1168
|
+
const line = firing.by === "manual" ? `(Started by hand from ${what} at ${when}.)` : eventful(adapterIn(this.adapters, firing.by)) ? `(From ${what}, received at ${when}.)` : `(Started by ${what} for ${when}.)`;
|
|
968
1169
|
// The payload is whatever its sender wrote: an instruction in it is data, and its closing tag, in any case, cannot end
|
|
969
1170
|
// the block.
|
|
970
1171
|
const payload = firing.payload === null ? "" : `\n\n<trigger-payload source="${firing.by}"${tagAttrs(firing.attrs)} untrusted>\n${firing.payload.replace(/<(\/trigger-payload)/gi, "<\\$1")}\n</trigger-payload>`;
|
package/dist/uploads.d.ts
CHANGED
|
@@ -6,6 +6,11 @@ export declare const MAX_UPLOAD_BYTES: number;
|
|
|
6
6
|
export declare const MAX_FILES_PER_RUN = 20;
|
|
7
7
|
/** Where a run finds its files: `work/inputs/<path>`, each file's `path` with its folders. */
|
|
8
8
|
export declare const INPUTS_DIR = "inputs";
|
|
9
|
+
/** The run's opening message as a file of its inputs/ (`inputs/message.md`): the start's text `content` byte for byte,
|
|
10
|
+
* so a long one (a pasted diff) is read by its path and named to a subagent, never copied into a call. The name is the
|
|
11
|
+
* message's alone: an upload at that path takes the next free one (`message-2.md`), with a message or without, and a
|
|
12
|
+
* run with no text message has no such file. */
|
|
13
|
+
export declare const MESSAGE_FILE = "message.md";
|
|
9
14
|
/** The data directory's folder of uploads. */
|
|
10
15
|
export declare const UPLOADS_DIR = "files";
|
|
11
16
|
/** Where a file's bytes go: its own PUT, as a path from the server's root, so it resolves against the server's address
|
|
@@ -123,6 +128,10 @@ export declare class Uploads {
|
|
|
123
128
|
}
|
|
124
129
|
/** The words a run fails in when its files cannot be copied into its workspace at its claim. */
|
|
125
130
|
export declare const notCopied: (why: string) => string;
|
|
131
|
+
/** At a run's claim: its opening message (`message`, the start's text content) saved as inputs/message.md
|
|
132
|
+
* (MESSAGE_FILE) when it has one and the file is not there yet, then its uploaded files (`ids`) copied in as
|
|
133
|
+
* copyRunFiles copies them. Neither, and nothing is written. */
|
|
134
|
+
export declare function placeRunInputs(data: string, runId: string, workspace: Workspace, ids: readonly string[], message: string): Promise<void>;
|
|
126
135
|
/** At a run's claim: its uploaded files (`ids`, the launch's file_ids) copied into its work/inputs/ once, from its hold
|
|
127
136
|
* (Uploads.hold), and the hold let go. A resumed claim finds inputs/ there and copies nothing, so the copy's suffixes
|
|
128
137
|
* never duplicate a file across claims. A copy that fails throws in notCopied's words: the run ends failed, saying why. */
|
package/dist/uploads.js
CHANGED
|
@@ -18,6 +18,11 @@ export const MAX_UPLOAD_BYTES = 50 * 2 ** 20;
|
|
|
18
18
|
export const MAX_FILES_PER_RUN = 20;
|
|
19
19
|
/** Where a run finds its files: `work/inputs/<path>`, each file's `path` with its folders. */
|
|
20
20
|
export const INPUTS_DIR = "inputs";
|
|
21
|
+
/** The run's opening message as a file of its inputs/ (`inputs/message.md`): the start's text `content` byte for byte,
|
|
22
|
+
* so a long one (a pasted diff) is read by its path and named to a subagent, never copied into a call. The name is the
|
|
23
|
+
* message's alone: an upload at that path takes the next free one (`message-2.md`), with a message or without, and a
|
|
24
|
+
* run with no text message has no such file. */
|
|
25
|
+
export const MESSAGE_FILE = "message.md";
|
|
21
26
|
/** The data directory's folder of uploads. */
|
|
22
27
|
export const UPLOADS_DIR = "files";
|
|
23
28
|
const RECORD = "file.json";
|
|
@@ -435,6 +440,10 @@ export class Uploads {
|
|
|
435
440
|
const workspace = workspaceAt(work);
|
|
436
441
|
await uploadTestHooks.beforeCopy?.(workspace);
|
|
437
442
|
const names = takenNames(workspace);
|
|
443
|
+
// inputs/message.md is the run's message's (MESSAGE_FILE), never an upload's.
|
|
444
|
+
const top = await names(INPUTS_DIR);
|
|
445
|
+
if (!top.has(MESSAGE_FILE))
|
|
446
|
+
top.set(MESSAGE_FILE, "file");
|
|
438
447
|
for (const file of files) {
|
|
439
448
|
const segments = file.path.split("/");
|
|
440
449
|
let dir = INPUTS_DIR;
|
|
@@ -494,6 +503,24 @@ export class Uploads {
|
|
|
494
503
|
}
|
|
495
504
|
/** The words a run fails in when its files cannot be copied into its workspace at its claim. */
|
|
496
505
|
export const notCopied = (why) => `The run's files could not be copied into its workspace: ${why}`;
|
|
506
|
+
/** At a run's claim: its opening message (`message`, the start's text content) saved as inputs/message.md
|
|
507
|
+
* (MESSAGE_FILE) when it has one and the file is not there yet, then its uploaded files (`ids`) copied in as
|
|
508
|
+
* copyRunFiles copies them. Neither, and nothing is written. */
|
|
509
|
+
export async function placeRunInputs(data, runId, workspace, ids, message) {
|
|
510
|
+
if (ids.length)
|
|
511
|
+
await copyRunFiles(data, runId, workspace, ids);
|
|
512
|
+
if (!message)
|
|
513
|
+
return;
|
|
514
|
+
const there = await workspace.list(INPUTS_DIR).then((entries) => entries.some((entry) => entry.name === MESSAGE_FILE), () => false);
|
|
515
|
+
if (there)
|
|
516
|
+
return;
|
|
517
|
+
try {
|
|
518
|
+
await workspace.write(`${INPUTS_DIR}/${MESSAGE_FILE}`, Buffer.from(message, "utf8"));
|
|
519
|
+
}
|
|
520
|
+
catch (error) {
|
|
521
|
+
throw new Error(`The run's message could not be saved in its workspace as ${INPUTS_DIR}/${MESSAGE_FILE}: ${error.message}`, { cause: error });
|
|
522
|
+
}
|
|
523
|
+
}
|
|
497
524
|
/** At a run's claim: its uploaded files (`ids`, the launch's file_ids) copied into its work/inputs/ once, from its hold
|
|
498
525
|
* (Uploads.hold), and the hold let go. A resumed claim finds inputs/ there and copies nothing, so the copy's suffixes
|
|
499
526
|
* never duplicate a file across claims. A copy that fails throws in notCopied's words: the run ends failed, saying why. */
|