@parcha/agentrun-serve 0.1.0-beta.38 → 0.1.0-beta.40
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 +455 -0
- package/README.md +376 -125
- 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-B8Zn1Iqn.css +1 -0
- package/dist/console/assets/index-Jbxjcmdp.js +705 -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 +83 -0
- package/dist/email/account.js +159 -0
- package/dist/email/answer.d.ts +15 -0
- package/dist/email/answer.js +217 -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 +373 -0
- package/dist/email/names.d.ts +50 -0
- package/dist/email/names.js +74 -0
- package/dist/email/routes.d.ts +10 -0
- package/dist/email/routes.js +50 -0
- package/dist/email/tools.d.ts +27 -0
- package/dist/email/tools.js +562 -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 +12 -4
- package/dist/learn/compile.js +57 -20
- package/dist/learn/grade.d.ts +4 -0
- package/dist/learn/grade.js +4 -1
- package/dist/learn/promote.d.ts +130 -64
- package/dist/learn/promote.js +113 -202
- 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 +35 -8
- package/dist/learning-cost.js +27 -9
- 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 +39 -2
- package/dist/local-models.js +49 -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 +2 -1
- package/dist/openapi.js +404 -93
- 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 +10 -2
- package/dist/port-models.js +6 -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 +14 -0
- package/dist/refusal-words.js +16 -2
- package/dist/run-launch.d.ts +12 -1
- package/dist/run-launch.js +13 -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 +44 -38
- package/dist/run-view.js +93 -56
- package/dist/runnable.d.ts +13 -5
- package/dist/runnable.js +69 -60
- package/dist/runner/adapter-loop.d.ts +48 -13
- package/dist/runner/adapter-loop.js +691 -254
- package/dist/runner/adapter-run-spec.d.ts +38 -12
- package/dist/runner/adapter-run-spec.js +175 -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 +95 -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 +59 -13
- package/dist/runner/journal-kinds.js +48 -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 +14 -3
- package/dist/runner/model-route.js +155 -19
- 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 +76 -10
- package/dist/runner/promotion.js +476 -154
- 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 +568 -137
- 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/dialect.d.ts +3 -0
- package/dist/store/dialect.js +1 -0
- 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/connection.d.ts +11 -5
- package/dist/store/postgres/connection.js +44 -17
- package/dist/store/postgres/dialect.js +5 -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.d.ts +7 -3
- package/dist/store/postgres/store.js +4 -0
- package/dist/store/schema.d.ts +7 -0
- package/dist/store/schema.js +20 -1
- package/dist/store/sqlite/connection.js +72 -8
- package/dist/store/sqlite/run-launch.d.ts +11 -0
- package/dist/store/sqlite/run-launch.js +8 -0
- 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 +170 -34
- package/dist/store/sqlite/worker.d.ts +2 -0
- package/dist/store/sqlite/worker.js +13 -10
- package/dist/store/store.d.ts +67 -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 +115 -18
- package/dist/store-reads/learning.js +170 -68
- 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 +84 -21
- package/dist/templates.js +254 -47
- 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 +46 -44
- package/dist/tool-gate.js +101 -92
- 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 +57 -16
- package/dist/tools.js +33 -28
- package/dist/trigger-adapter.d.ts +56 -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 +181 -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 +105 -8
- package/dist/triggers.js +286 -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/decision-recovery.js +5 -2
- package/dist/workflow/decisions.d.ts +7 -0
- 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,7 @@ 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 const PAYLOAD_KEPT_BYTES = 16 * 1024;
|
|
46
|
+
/** The most of a webhook's payload a firing passes on: the rest is cut, at a character's start (trigger-adapter.ts). */
|
|
48
47
|
/** How late a missed time may be and still fire once, by default: an hour. */
|
|
49
48
|
export const CATCHUP_WINDOW_S = 3600;
|
|
50
49
|
/** The failed tries in a row that turn a trigger off. */
|
|
@@ -70,6 +69,27 @@ export const SEEN_KEPT = 5_000;
|
|
|
70
69
|
/** The highest max_active_runs a trigger may set (the store's CHECK holds the same). */
|
|
71
70
|
export const MAX_ACTIVE_RUNS = 100;
|
|
72
71
|
const ENDED = new Set(["completed", "failed", "cancelled"]);
|
|
72
|
+
/** A polled source's trigger setting its last check is kept in (TriggerAdapter.poll), with the sign-ins refused in a row. */
|
|
73
|
+
export const LAST_CHECK = "last_check";
|
|
74
|
+
/** The sign-ins a polled source's server refuses in a row that turn its trigger off. */
|
|
75
|
+
export const REFUSALS_TO_STOP = 3;
|
|
76
|
+
/** How long a create or a turn-on of a polled source waits for its first poll (where it starts watching) before it
|
|
77
|
+
* answers: past it, the answer goes out and the poll carries on. */
|
|
78
|
+
export const FIRST_POLL_WAIT_MS = 10_000;
|
|
79
|
+
/** Whether `adapter`'s firings are events from outside (pushed, or found by a poll), each its own, never a time. */
|
|
80
|
+
const eventful = (adapter) => adapter?.delivery === "push" || adapter?.poll !== undefined;
|
|
81
|
+
/** A polled trigger's last check as kept (LAST_CHECK), when it reads. */
|
|
82
|
+
function checkOf(text) {
|
|
83
|
+
if (!text)
|
|
84
|
+
return undefined;
|
|
85
|
+
try {
|
|
86
|
+
const value = JSON.parse(text);
|
|
87
|
+
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;
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
73
93
|
/** Why a message_run trigger turned itself off: the run it messages is gone. */
|
|
74
94
|
const RUN_DELETED = "its run was deleted";
|
|
75
95
|
const REAL_CLOCK = {
|
|
@@ -142,29 +162,15 @@ function settingsOf(body, issues) {
|
|
|
142
162
|
}
|
|
143
163
|
const WRITABLE = ["name", "enabled", "source", "target", "catchup_window_s", "max_active_runs"];
|
|
144
164
|
/** 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. */
|
|
165
|
+
* or polled source does, with or without accept (set, each event starts its own run up to the cap); a schedule keeps
|
|
166
|
+
* the overlap skip, and a message_run target starts no run to bound. */
|
|
147
167
|
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." }];
|
|
168
|
+
if (!eventful(adapterIn(adapters, source.kind)))
|
|
169
|
+
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
170
|
if (target?.kind === "message_run")
|
|
151
171
|
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
172
|
return [];
|
|
153
173
|
}
|
|
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
174
|
/** A POST /triggers body's own fields, read as create reads them with the sources of `adapters`: its source, target,
|
|
169
175
|
* settings, name and enabled, with every issue. What it names (the agent, the run) is create's own check
|
|
170
176
|
* (TriggerDeps.targetIssues). */
|
|
@@ -213,6 +219,17 @@ export class Triggers {
|
|
|
213
219
|
waiting = false;
|
|
214
220
|
/** The triggers whose day-kept keys are at their cap, said once until they are under it again. */
|
|
215
221
|
seenFull = new Set();
|
|
222
|
+
/** The polled triggers whose poll is running, one at a time each: true when it is a kick's (kick), which a time come
|
|
223
|
+
* meanwhile waits for (the first poll, the watching start, is never the reason a time is lost); false for a time's own,
|
|
224
|
+
* which a time come meanwhile skips (a server slower than every_s is never polled back to back). */
|
|
225
|
+
polling = new Map();
|
|
226
|
+
/** The polled triggers kicked (kick), or whose time came during a kick's poll, while their poll ran: polled again once
|
|
227
|
+
* it is done, as a kick's poll (true) when a kick asked for it, else as a time's. */
|
|
228
|
+
pollAgain = new Map();
|
|
229
|
+
/** Who waits on the poll after the one running (a kick made while it ran): told when that one is done. */
|
|
230
|
+
pollWaiters = new Map();
|
|
231
|
+
/** Each polled trigger's count of turn-ons: a poll that sees it changed keeps nothing, its cursor read before. */
|
|
232
|
+
restarts = new Map();
|
|
216
233
|
closed = false;
|
|
217
234
|
constructor(deps) {
|
|
218
235
|
this.deps = deps;
|
|
@@ -263,39 +280,45 @@ export class Triggers {
|
|
|
263
280
|
const id = `trg_${randomBytes(12).toString("hex")}`;
|
|
264
281
|
const enabled = body.enabled !== false;
|
|
265
282
|
if (enabled)
|
|
266
|
-
this.needAvailable(source.kind);
|
|
283
|
+
await this.needAvailable(source.kind);
|
|
267
284
|
// Never made on for an archived agent: told before anything is minted (the store's own refusal is the backstop).
|
|
268
285
|
if (enabled)
|
|
269
286
|
await this.needAgentOn(agentId);
|
|
270
287
|
// A scope belongs to one trigger: told before anything is minted or written (the store's scope rows are the backstop).
|
|
271
288
|
if (enabled)
|
|
272
289
|
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
290
|
const name = typeof body.name === "string" ? body.name.trim() : id;
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
291
|
+
const keep = async (kept) => {
|
|
292
|
+
try {
|
|
293
|
+
// Its secrets are kept before the trigger is: one whose secret cannot be kept makes none.
|
|
294
|
+
const minted = await this.mint(kept, id);
|
|
295
|
+
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));
|
|
296
|
+
return { row, minted };
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
throw (await this.scopeTaken(error, kept, id)) ?? (await this.archivedRefusal(error)) ?? nameTaken(error, name);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
// What its sender keeps of it is made first (Composio's instance), the trigger saved after, and what was made undone
|
|
303
|
+
// there when it is not saved (the sender's own steps, one project's at a time).
|
|
304
|
+
const upstream = this.deps.upstream?.(source.kind);
|
|
305
|
+
const { row, minted } = upstream ? await upstream.make(source, id, enabled, keep) : await keep(source);
|
|
283
306
|
// Armed before the answer: a create that answered is in effect, its first time on the timer (arm reads the store).
|
|
284
307
|
await this.arm();
|
|
308
|
+
// A polled source polls at once, and is answered once it has (FIRST_POLL_WAIT_MS at most): its first poll, with no
|
|
309
|
+
// cursor, is where it starts watching, so mail after the answer starts runs, and the answer says the check.
|
|
310
|
+
if (enabled)
|
|
311
|
+
await this.firstPoll(asTrigger(row));
|
|
285
312
|
// What it minted, shown this once, by field: a webhook's `secret`.
|
|
286
313
|
return { ...(await this.view(asTrigger(row))), ...minted };
|
|
287
314
|
}
|
|
288
|
-
/** A trigger's new signing secret, shown this once; the old one signs nothing from now on.
|
|
289
|
-
|
|
290
|
-
async rotate(id, given) {
|
|
315
|
+
/** A trigger's new signing secret, shown this once; the old one signs nothing from now on. */
|
|
316
|
+
async rotate(id) {
|
|
291
317
|
const trigger = await this.need(id);
|
|
292
318
|
const adapter = this.adapter(trigger.source);
|
|
293
319
|
if (!mints(adapter))
|
|
294
320
|
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);
|
|
321
|
+
return this.mint(trigger.source, id);
|
|
299
322
|
}
|
|
300
323
|
/** Whether `id` is a trigger of the pushed source `kind` that is on: the one a request delivered for `kind` may fire. */
|
|
301
324
|
async hookable(id, kind) {
|
|
@@ -322,6 +345,11 @@ export class Triggers {
|
|
|
322
345
|
async listening(kind) {
|
|
323
346
|
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
347
|
}
|
|
348
|
+
/** Every trigger of source `kind`, on or off, with its name and source: what a source's sender keeps of them is shared
|
|
349
|
+
* between some (composio-triggers.ts: one Composio instance per server, event and config). */
|
|
350
|
+
async ofKind(kind) {
|
|
351
|
+
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 }));
|
|
352
|
+
}
|
|
325
353
|
/** Every trigger of source `kind` that is on, turned off because the connection it needs is gone (`disabled: {reason:
|
|
326
354
|
* "connection_lost", detail}`), and said; how many. They are kept: on again once it is back. */
|
|
327
355
|
async turnOff(kind, detail) {
|
|
@@ -333,6 +361,7 @@ export class Triggers {
|
|
|
333
361
|
}
|
|
334
362
|
if (on.length)
|
|
335
363
|
await this.arm();
|
|
364
|
+
await this.offThere(on);
|
|
336
365
|
return on.length;
|
|
337
366
|
}
|
|
338
367
|
/** 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 +374,7 @@ export class Triggers {
|
|
|
345
374
|
this.deps.say({ condition: "trigger_agent_archived", trigger: t.id, agent: agentId });
|
|
346
375
|
if (off.length)
|
|
347
376
|
await this.arm();
|
|
377
|
+
await this.offThere((await Promise.all(off.map((t) => this.find(t.id)))).filter((t) => t !== undefined));
|
|
348
378
|
return off;
|
|
349
379
|
}
|
|
350
380
|
/** Agent `agentId` unarchived: none of its triggers turns on again. The triggers its archive turned off that are off
|
|
@@ -372,8 +402,8 @@ export class Triggers {
|
|
|
372
402
|
return agent?.archivedAt != null ? `The agent ${agent.name} is archived.` : undefined;
|
|
373
403
|
}
|
|
374
404
|
/** 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);
|
|
405
|
+
async needAvailable(kind) {
|
|
406
|
+
const refusal = await this.deps.unavailable?.(kind);
|
|
377
407
|
if (refusal)
|
|
378
408
|
throw refusal;
|
|
379
409
|
}
|
|
@@ -412,7 +442,7 @@ export class Triggers {
|
|
|
412
442
|
async update(id, body) {
|
|
413
443
|
const was = await this.need(id);
|
|
414
444
|
const issues = unknownFields(body, WRITABLE);
|
|
415
|
-
|
|
445
|
+
let source = body.source === undefined ? undefined : sourceOf(this.adapters, body.source, this.clock.now(), issues);
|
|
416
446
|
const target = body.target === undefined ? undefined : targetOf(body.target, issues);
|
|
417
447
|
const settings = settingsOf(body, issues);
|
|
418
448
|
// 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 +461,10 @@ export class Triggers {
|
|
|
431
461
|
const trigger = await this.need(id);
|
|
432
462
|
if (source && source.kind !== trigger.source.kind)
|
|
433
463
|
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
|
-
|
|
464
|
+
// What its sender keeps of it stays as it was made (Composio's instance): only what reads it here changes.
|
|
465
|
+
const upstream = this.deps.upstream?.(trigger.source.kind);
|
|
466
|
+
if (source && upstream)
|
|
467
|
+
source = upstream.patched(trigger.source, source);
|
|
437
468
|
const now = this.clock.now();
|
|
438
469
|
const enabled = typeof body.enabled === "boolean" ? body.enabled : trigger.enabled;
|
|
439
470
|
const adapter = this.adapter(trigger.source);
|
|
@@ -447,7 +478,7 @@ export class Triggers {
|
|
|
447
478
|
const again = enabled && !trigger.enabled ? { failuresInARow: 0, disabled: null } : {};
|
|
448
479
|
const scopeSource = source ?? trigger.source;
|
|
449
480
|
if (enabled && (!trigger.enabled || source))
|
|
450
|
-
this.needAvailable(scopeSource.kind);
|
|
481
|
+
await this.needAvailable(scopeSource.kind);
|
|
451
482
|
if (enabled)
|
|
452
483
|
await this.scopeFree(scopeSource, trigger.id);
|
|
453
484
|
// A trigger belongs to its target's agent: aimed at another agent (or another agent's run), it moves to that agent,
|
|
@@ -456,6 +487,10 @@ export class Triggers {
|
|
|
456
487
|
// Turned on, or aimed elsewhere while on: never for an archived agent (the store's own refusal is the backstop).
|
|
457
488
|
if (enabled && (!trigger.enabled || target))
|
|
458
489
|
await this.needAgentOn(agentId);
|
|
490
|
+
// A polled source turned on again starts over: a poll still running from before is not its own any more.
|
|
491
|
+
const polls = enabled && adapter.poll !== undefined && (!trigger.enabled || source !== undefined);
|
|
492
|
+
if (polls && !trigger.enabled)
|
|
493
|
+
this.restarts.set(id, (this.restarts.get(id) ?? 0) + 1);
|
|
459
494
|
const patch = {
|
|
460
495
|
...(agentId !== trigger.agentId ? { agentId } : {}),
|
|
461
496
|
...(typeof body.name === "string" ? { name: body.name.trim() } : {}), ...(source ? { source } : {}), ...(target ? { target } : {}),
|
|
@@ -467,15 +502,28 @@ export class Triggers {
|
|
|
467
502
|
});
|
|
468
503
|
if (!row)
|
|
469
504
|
throw new ApiError("not_found", `no trigger ${id}`);
|
|
505
|
+
// Turned on again, a polled source forgets its cursor and last check, so its first poll watches from now, and is
|
|
506
|
+
// answered once it has (as a create is); an edit of its source keeps the cursor (a poll whose cursor was read on
|
|
507
|
+
// another mailbox or account starts over itself) and polls at once, unawaited: that poll may start runs.
|
|
508
|
+
if (polls && !trigger.enabled)
|
|
509
|
+
await this.deps.settings(adapter.id)?.remove(id);
|
|
470
510
|
await this.arm();
|
|
471
|
-
|
|
511
|
+
if (polls && !trigger.enabled)
|
|
512
|
+
await this.firstPoll(asTrigger(row));
|
|
513
|
+
else if (polls)
|
|
514
|
+
void this.kick(asTrigger(row));
|
|
515
|
+
// Turned on or off by the owner: the same at its sender, once it holds here; what could not be done there is said.
|
|
516
|
+
const said = upstream && enabled !== trigger.enabled ? await upstream.turned(row.source, id, enabled) : undefined;
|
|
517
|
+
return { ...(await this.view(asTrigger(row))), ...said };
|
|
472
518
|
}
|
|
519
|
+
/** The trigger deleted, and what its sender keeps of it removed after (said, when it could not be: the delete holds). */
|
|
473
520
|
async remove(id) {
|
|
474
521
|
const trigger = await this.need(id);
|
|
475
522
|
await this.store.triggers.remove(id);
|
|
476
523
|
if (this.adapter(trigger.source)?.settings?.scope === "trigger")
|
|
477
524
|
await this.deps.settings(trigger.source.kind)?.remove(id);
|
|
478
525
|
await this.arm();
|
|
526
|
+
return this.deps.upstream?.(trigger.source.kind)?.removed(trigger.source, id);
|
|
479
527
|
}
|
|
480
528
|
/** Run now: one firing per Idempotency-Key, driven before the answer. `created` false: the key's firing, as it stands. */
|
|
481
529
|
async fire(id, idempotencyKey) {
|
|
@@ -550,6 +598,7 @@ export class Triggers {
|
|
|
550
598
|
await this.store.triggers.update(t.id, { enabled: false, nextAt: null, disabled: { at: iso(now), reason: "source_unreadable", detail } });
|
|
551
599
|
this.deps.say({ condition: "trigger_source_unreadable", trigger: t.id, error: detail });
|
|
552
600
|
}
|
|
601
|
+
await this.offThere(triggers);
|
|
553
602
|
}
|
|
554
603
|
/** Records `trigger`'s firings up to now and moves its next time on from the last nominal one, in one write. Of the
|
|
555
604
|
* times that have come, the latest fires (on time, or late within catchup_window_s: a catch-up, under its own nominal
|
|
@@ -561,6 +610,8 @@ export class Triggers {
|
|
|
561
610
|
const clock = adapter.clock?.(trigger.source);
|
|
562
611
|
if (!clock)
|
|
563
612
|
return;
|
|
613
|
+
if (adapter.poll)
|
|
614
|
+
return this.pollDue(trigger, adapter, clock, now);
|
|
564
615
|
const by = adapter.id;
|
|
565
616
|
const windowMs = (trigger.catchupWindowS ?? CATCHUP_WINDOW_S) * 1000;
|
|
566
617
|
const first = trigger.nextAt;
|
|
@@ -598,6 +649,148 @@ export class Triggers {
|
|
|
598
649
|
await this.store.triggers.record(trigger.id, firings, { patch: { nextAt: next, ...(next === null ? { enabled: false } : {}) } });
|
|
599
650
|
await this.prune(trigger);
|
|
600
651
|
}
|
|
652
|
+
/** 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
|
|
653
|
+
* `missed` row: what came meanwhile is still there to read), then its poll, off the timer, so a slow server never
|
|
654
|
+
* holds the other triggers. With one still running for it: once that one is done when it is a kick's (pollDone),
|
|
655
|
+
* else skipped. */
|
|
656
|
+
async pollDue(trigger, adapter, clock, now) {
|
|
657
|
+
const last = clock.latest(trigger.nextAt, now);
|
|
658
|
+
const next = clock.next(last);
|
|
659
|
+
await this.store.triggers.update(trigger.id, { nextAt: next !== null && Number.isFinite(next) && Math.abs(next) <= DATE_MAX_MS ? next : null });
|
|
660
|
+
const running = this.polling.get(trigger.id);
|
|
661
|
+
if (running !== undefined)
|
|
662
|
+
return void (running && !this.pollAgain.has(trigger.id) && this.pollAgain.set(trigger.id, false));
|
|
663
|
+
this.polling.set(trigger.id, false);
|
|
664
|
+
void this.polled(trigger, adapter, last, now).finally(() => this.pollDone(trigger.id));
|
|
665
|
+
}
|
|
666
|
+
/** A poll of `trigger` now, answered once the poll that answers it is done: its own, or, when an edit of its source
|
|
667
|
+
* drops its result (polled), the one after it, which it asks for at once; when one is running, the one after that. */
|
|
668
|
+
kick(trigger, waitedFor = true) {
|
|
669
|
+
const adapter = this.adapter(trigger.source);
|
|
670
|
+
if (!adapter.poll || this.closed)
|
|
671
|
+
return Promise.resolve();
|
|
672
|
+
if (this.polling.has(trigger.id)) {
|
|
673
|
+
this.pollAgain.set(trigger.id, true);
|
|
674
|
+
return new Promise((done) => this.pollWaiters.set(trigger.id, [...(this.pollWaiters.get(trigger.id) ?? []), done]));
|
|
675
|
+
}
|
|
676
|
+
const now = this.clock.now();
|
|
677
|
+
this.polling.set(trigger.id, waitedFor);
|
|
678
|
+
return this.polled(trigger, adapter, now, now).then((kept) => {
|
|
679
|
+
// Dropped for an edit of its source: the poll after it (the edit's) runs at once, and is the one this answers by.
|
|
680
|
+
const after = kept || this.closed ? undefined : new Promise((done) => this.pollWaiters.set(trigger.id, [...(this.pollWaiters.get(trigger.id) ?? []), done]));
|
|
681
|
+
if (after)
|
|
682
|
+
this.pollAgain.set(trigger.id, true);
|
|
683
|
+
this.pollDone(trigger.id);
|
|
684
|
+
return after;
|
|
685
|
+
}, (error) => {
|
|
686
|
+
this.deps.say({ condition: "trigger_poll_unrecorded", trigger: trigger.id, error: String(error?.message ?? error) });
|
|
687
|
+
this.pollDone(trigger.id);
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
/** `trigger`'s first poll (made, or turned on again) kicked and waited on, FIRST_POLL_WAIT_MS at most. */
|
|
691
|
+
async firstPoll(trigger) {
|
|
692
|
+
let timer;
|
|
693
|
+
const bound = new Promise((done) => {
|
|
694
|
+
timer = setTimeout(done, FIRST_POLL_WAIT_MS);
|
|
695
|
+
timer.unref?.();
|
|
696
|
+
});
|
|
697
|
+
await Promise.race([this.kick(trigger), bound]).finally(() => clearTimeout(timer));
|
|
698
|
+
}
|
|
699
|
+
/** `id`'s poll done: a kick, or a time come during a kick's poll, polls now, once however many came, the trigger read
|
|
700
|
+
* again (on, and as it is now). A time come during this one skips, as during a time's own. */
|
|
701
|
+
pollDone(id) {
|
|
702
|
+
this.polling.delete(id);
|
|
703
|
+
const again = this.pollAgain.get(id);
|
|
704
|
+
this.pollAgain.delete(id);
|
|
705
|
+
const waiters = this.pollWaiters.get(id) ?? [];
|
|
706
|
+
this.pollWaiters.delete(id);
|
|
707
|
+
const told = () => waiters.forEach((done) => done());
|
|
708
|
+
if (again === undefined || this.closed)
|
|
709
|
+
return told();
|
|
710
|
+
void this.find(id).then((trigger) => (trigger?.enabled ? this.kick(trigger, again) : undefined)).then(told, (error) => {
|
|
711
|
+
this.deps.say({ condition: "trigger_poll_unrecorded", trigger: id, error: String(error?.message ?? error) });
|
|
712
|
+
told();
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
/** One poll of `trigger`'s source for its time `nominalAt`: each event it found recorded in order (its note a skipped
|
|
716
|
+
* history line; one it leaves, skipped and said; one its accept does not take, an ignored row kept for its day; any
|
|
717
|
+
* other due, and driven), its cursor kept once they are, then its last check, and at the REFUSALS_TO_STOPth refused
|
|
718
|
+
* sign-in in a row the trigger turned off. A poll that throws is a failed check. */
|
|
719
|
+
/** False when an edit of its source while it polled dropped what it found (the edit's poll comes after it); else true. */
|
|
720
|
+
async polled(trigger, adapter, nominalAt, now) {
|
|
721
|
+
const settings = this.deps.settings(adapter.id);
|
|
722
|
+
// Turned on again while it polled (off and on): its cursor was forgotten, and nothing this poll found is its own.
|
|
723
|
+
const generation = this.restarts.get(trigger.id);
|
|
724
|
+
const restarted = () => this.restarts.get(trigger.id) !== generation;
|
|
725
|
+
let result;
|
|
726
|
+
try {
|
|
727
|
+
result = await adapter.poll(trigger.source, { trigger: trigger.id, setting: (field) => settings?.get(trigger.id, field), credentials: async () => this.deps.credentials?.(adapter.id) });
|
|
728
|
+
}
|
|
729
|
+
catch (error) {
|
|
730
|
+
result = { check: { ok: false, message: String(error?.message ?? error) } };
|
|
731
|
+
}
|
|
732
|
+
if (this.closed)
|
|
733
|
+
return true;
|
|
734
|
+
try {
|
|
735
|
+
// Edited, turned off or deleted while it polled: nothing it found is kept. An edit of its source polls again.
|
|
736
|
+
const current = await this.find(trigger.id);
|
|
737
|
+
if (!current || !current.enabled || restarted())
|
|
738
|
+
return true;
|
|
739
|
+
if (JSON.stringify(current.source) !== JSON.stringify(trigger.source))
|
|
740
|
+
return false;
|
|
741
|
+
const by = adapter.id;
|
|
742
|
+
const at = Math.floor(nominalAt / 1000) * 1000;
|
|
743
|
+
if (result.note)
|
|
744
|
+
await this.record(current, { key: firingKey(by, trigger.id, `note\n${second(nominalAt)}`), by, nominalAt: at, status: "skipped", reason: result.note });
|
|
745
|
+
const events = result.events ?? [];
|
|
746
|
+
let cursor = events.length ? undefined : result.cursor;
|
|
747
|
+
let full = false;
|
|
748
|
+
for (const event of events) {
|
|
749
|
+
const key = firingKey(by, trigger.id, event.key);
|
|
750
|
+
const attrs = event.attrs && Object.keys(event.attrs).length ? { attrs: event.attrs } : {};
|
|
751
|
+
const refused = event.left ? undefined : adapter.takes?.(current.source, event);
|
|
752
|
+
try {
|
|
753
|
+
if (event.left)
|
|
754
|
+
await this.record(current, { key, by, nominalAt: at, status: "skipped", reason: event.left, ...attrs });
|
|
755
|
+
else if (refused)
|
|
756
|
+
await this.record(current, { key, by, nominalAt: at, status: "ignored", shown: false, ...attrs });
|
|
757
|
+
else {
|
|
758
|
+
const recorded = await this.record(current, { key, by, nominalAt: at, status: "due", payload: cut(event.text), ...attrs }, { room: true });
|
|
759
|
+
if (recorded.created)
|
|
760
|
+
void this.inTurn(trigger.id, key);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
catch (error) {
|
|
764
|
+
// Full (DUE_KEPT firings still due): this event and those after it wait for the next poll, the cursor before it.
|
|
765
|
+
if (!(error instanceof ApiError && error.code === "rate_limited"))
|
|
766
|
+
throw error;
|
|
767
|
+
full = true;
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
cursor = event.cursor;
|
|
771
|
+
}
|
|
772
|
+
if (!full && events.length)
|
|
773
|
+
cursor = result.cursor ?? cursor;
|
|
774
|
+
if (restarted())
|
|
775
|
+
return true;
|
|
776
|
+
for (const [field, value] of Object.entries(cursor ?? {}))
|
|
777
|
+
await settings?.set(trigger.id, field, value);
|
|
778
|
+
const refusals = result.refused ? (checkOf(settings?.get(trigger.id, LAST_CHECK))?.refused ?? 0) + 1 : 0;
|
|
779
|
+
if (result.refused && refusals >= REFUSALS_TO_STOP) {
|
|
780
|
+
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 } });
|
|
781
|
+
this.deps.say({ condition: "trigger_sign_in_refused", trigger: trigger.id, refused: refusals });
|
|
782
|
+
await this.arm();
|
|
783
|
+
}
|
|
784
|
+
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;
|
|
785
|
+
await settings?.set(trigger.id, LAST_CHECK, JSON.stringify({ at: iso(now), ok: result.check.ok, message, refused: refusals }));
|
|
786
|
+
if (!result.check.ok)
|
|
787
|
+
this.deps.say({ condition: "trigger_check_failed", trigger: trigger.id, error: result.check.message });
|
|
788
|
+
}
|
|
789
|
+
catch (error) {
|
|
790
|
+
this.deps.say({ condition: "trigger_poll_unrecorded", trigger: trigger.id, error: String(error?.message ?? error) });
|
|
791
|
+
}
|
|
792
|
+
return true;
|
|
793
|
+
}
|
|
601
794
|
/** A firing taken from due to its end: skipped when its trigger's last run is active, else its run started (or its
|
|
602
795
|
* message sent). A failed try keeps it due under the backoff table, or ends it failed. Each step reads the trigger and
|
|
603
796
|
* the firing again: either may be edited or deleted meanwhile. */
|
|
@@ -641,7 +834,8 @@ export class Triggers {
|
|
|
641
834
|
return void (await this.send(now, firing));
|
|
642
835
|
let started;
|
|
643
836
|
try {
|
|
644
|
-
|
|
837
|
+
const attrs = firing.attrs;
|
|
838
|
+
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
839
|
}
|
|
646
840
|
catch (error) {
|
|
647
841
|
// Stopping, the start may have been cut by the close: the firing stays due for the next start, whose origin key
|
|
@@ -677,9 +871,10 @@ export class Triggers {
|
|
|
677
871
|
await this.store.triggers.update(trigger.id, { enabled: false, nextAt: null, disabled: { at: iso(this.clock.now()), reason: "source_unreadable", detail: RUN_DELETED } });
|
|
678
872
|
await this.store.triggers.settle(trigger.id, firing.key, { status: "skipped", reason: RUN_DELETED });
|
|
679
873
|
this.deps.say({ condition: "trigger_source_unreadable", trigger: trigger.id, error: RUN_DELETED });
|
|
874
|
+
await this.offThere([trigger]);
|
|
680
875
|
return;
|
|
681
876
|
}
|
|
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.` };
|
|
877
|
+
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
878
|
let skipped;
|
|
684
879
|
try {
|
|
685
880
|
skipped = (await this.deps.message(target, this.inputOf(trigger, firing), firing.key, this.originOf(trigger), bound))?.skipped;
|
|
@@ -742,8 +937,10 @@ export class Triggers {
|
|
|
742
937
|
if (attempts <= BACKOFF_MS.length) {
|
|
743
938
|
const wait = Math.max(BACKOFF_MS[attempts - 1], error instanceof ApiError ? (error.extra.retry_after_ms ?? 0) : 0);
|
|
744
939
|
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)
|
|
940
|
+
if (settled?.paused) {
|
|
746
941
|
this.deps.say({ condition: "trigger_auto_paused", trigger: triggerId, firing: key });
|
|
942
|
+
await this.pausedThere(triggerId);
|
|
943
|
+
}
|
|
747
944
|
return;
|
|
748
945
|
}
|
|
749
946
|
await this.failed(triggerId, key, errorOf(error));
|
|
@@ -769,8 +966,37 @@ export class Triggers {
|
|
|
769
966
|
/** A failed firing: one more in its trigger's streak, the trigger turned off at FAILURES_TO_PAUSE, and trigger.failed
|
|
770
967
|
* queued (after the firing is written: a start finds one not yet queued and queues it, under the same webhook-id). */
|
|
771
968
|
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 });
|
|
969
|
+
const settled = await this.store.triggers.settle(triggerId, key, { status: "failed", error, ...(runId ? { runId } : {}), ...(reason ? { reason } : {}) }, { failure: true, pauseAt: FAILURES_TO_PAUSE });
|
|
773
970
|
await this.notifyFailed(triggerId, key);
|
|
971
|
+
if (settled?.paused)
|
|
972
|
+
await this.pausedThere(triggerId);
|
|
973
|
+
}
|
|
974
|
+
/** Trigger `triggerId`, turned off by its failure streak: off at its sender too (offThere). */
|
|
975
|
+
async pausedThere(triggerId) {
|
|
976
|
+
const trigger = await this.find(triggerId);
|
|
977
|
+
if (trigger && !trigger.enabled)
|
|
978
|
+
await this.offThere([trigger]);
|
|
979
|
+
}
|
|
980
|
+
/** Triggers the server turned off itself (their agent archived, a failure streak, a connection or a run gone, a source
|
|
981
|
+
* that no longer reads): off at their sender too, as an owner's pause is, by its own rule (Composio's instance only
|
|
982
|
+
* once no trigger holding it is on). What could not be done there is said, with what to do by hand; the change here
|
|
983
|
+
* holds either way. */
|
|
984
|
+
async offThere(triggers) {
|
|
985
|
+
for (const t of triggers) {
|
|
986
|
+
const upstream = this.deps.upstream?.(t.source.kind);
|
|
987
|
+
if (!upstream)
|
|
988
|
+
continue;
|
|
989
|
+
try {
|
|
990
|
+
const said = await upstream.turned(t.source, t.id, false);
|
|
991
|
+
for (const [where, what] of Object.entries(isObject(said) ? said : {})) {
|
|
992
|
+
if (isObject(what) && typeof what.note === "string")
|
|
993
|
+
this.deps.say({ condition: "trigger_off_upstream_failed", trigger: t.id, upstream: where, note: what.note });
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
catch (error) {
|
|
997
|
+
this.deps.say({ condition: "trigger_off_upstream_failed", trigger: t.id, upstream: t.source.kind, note: String(error?.message ?? error) });
|
|
998
|
+
}
|
|
999
|
+
}
|
|
774
1000
|
}
|
|
775
1001
|
async notifyFailed(triggerId, key) {
|
|
776
1002
|
const firing = await this.store.triggers.firing(triggerId, key);
|
|
@@ -811,7 +1037,7 @@ export class Triggers {
|
|
|
811
1037
|
* most SEEN_KEPT of them; past it the oldest go, said once each time the cap is reached. */
|
|
812
1038
|
async prune(trigger) {
|
|
813
1039
|
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)
|
|
1040
|
+
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
1041
|
if (over > 0) {
|
|
816
1042
|
if (!this.seenFull.has(trigger.id))
|
|
817
1043
|
this.deps.say({ condition: "trigger_seen_full", trigger: trigger.id, kept: cap });
|
|
@@ -870,9 +1096,12 @@ export class Triggers {
|
|
|
870
1096
|
async view(trigger) {
|
|
871
1097
|
const [last] = await this.store.triggers.firings(trigger.id);
|
|
872
1098
|
const adapter = adapterIn(this.adapters, trigger.source.kind);
|
|
873
|
-
const hook = adapter?.push ? `/v2/hooks/${adapter.settings?.scope === "server" ? adapter.id : trigger.id}` : undefined;
|
|
1099
|
+
const hook = adapter?.push ? `/v2/hooks/${adapter.hookOf?.(trigger.source) ?? (adapter.settings?.scope === "server" ? adapter.id : trigger.id)}` : undefined;
|
|
1100
|
+
// A polled source's last check: when, whether it read, in words; null before its first.
|
|
1101
|
+
const check = adapter?.poll ? checkOf(this.deps.settings(adapter.id)?.get(trigger.id, LAST_CHECK)) : undefined;
|
|
874
1102
|
return {
|
|
875
1103
|
...(hook ? { hook_url: `${this.deps.publicUrl ?? ""}${hook}` } : {}),
|
|
1104
|
+
...(adapter?.poll ? { last_check: check ? { at: check.at, ok: check.ok, message: check.message } : null } : {}),
|
|
876
1105
|
id: trigger.id, name: trigger.name, enabled: trigger.enabled, source: trigger.source, target: trigger.target, catchup_window_s: trigger.catchupWindowS ?? CATCHUP_WINDOW_S,
|
|
877
1106
|
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
1107
|
created_at: iso(trigger.createdAt), updated_at: iso(trigger.updatedAt), last_firing: last ? firingView(last) : null,
|
|
@@ -885,21 +1114,13 @@ export class Triggers {
|
|
|
885
1114
|
}
|
|
886
1115
|
/** Each field `source`'s adapter mints for its triggers, made now for trigger `id`, by field: none for one that keeps
|
|
887
1116
|
* no trigger-scoped secret. */
|
|
888
|
-
async mint(source, id
|
|
1117
|
+
async mint(source, id) {
|
|
889
1118
|
const adapter = this.adapter(source);
|
|
890
1119
|
if (!mints(adapter))
|
|
891
1120
|
return {};
|
|
892
1121
|
const store = this.deps.settings(adapter.id);
|
|
893
1122
|
if (!store)
|
|
894
1123
|
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
1124
|
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
1125
|
}
|
|
905
1126
|
/** 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 +1169,9 @@ export class Triggers {
|
|
|
948
1169
|
titleOf(trigger, firing) {
|
|
949
1170
|
const source = TRIGGER_LABELS[trigger.source.kind] ?? trigger.source.kind;
|
|
950
1171
|
const attrs = firing.attrs;
|
|
1172
|
+
const own = attrs && firing.by === trigger.source.kind ? this.adapter(trigger.source).title?.(attrs) : undefined;
|
|
1173
|
+
if (own)
|
|
1174
|
+
return `${source} ${trigger.name}: ${own}`;
|
|
951
1175
|
const event = attrs?.event;
|
|
952
1176
|
const action = attrs?.action;
|
|
953
1177
|
return `${source} ${trigger.name}${event ? `: ${event}${action ? ` ${action}` : ""}` : ""}`;
|
|
@@ -964,7 +1188,7 @@ export class Triggers {
|
|
|
964
1188
|
const adapter = this.adapter(trigger.source);
|
|
965
1189
|
const when = local(firing.nominalAt, adapter.zone?.(trigger.source) ?? "UTC");
|
|
966
1190
|
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)
|
|
1191
|
+
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
1192
|
// The payload is whatever its sender wrote: an instruction in it is data, and its closing tag, in any case, cannot end
|
|
969
1193
|
// the block.
|
|
970
1194
|
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. */
|