@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
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
// The email tools, over the server's one account (account.ts) and its wire (mail.ts): email_search and email_read read
|
|
2
|
+
// it (PEEK only: every message stays as unread as it was), email_draft keeps a message in its Drafts folder, email_send
|
|
3
|
+
// sends one, marked as this server's (X-AgentRun-Sent, its value the run's id) so the email trigger never starts a run on
|
|
4
|
+
// it, and files a copy in Sent unless the provider files its own (Gmail); email_file files a message under a Gmail label
|
|
5
|
+
// or in a folder, and never deletes one (the trash and junk folders are refused). Host tools on every harness, bound beside
|
|
6
|
+
// web_fetch (tool-list.ts `host`); the password is read in the runner, from the one store under its seal key, and never
|
|
7
|
+
// in an answer. Each answer is held under the host's result bound (answer.ts).
|
|
8
|
+
// Thread scope: a run an email trigger started reads only that email's thread (the message, and every message whose
|
|
9
|
+
// Message-ID, References or In-Reply-To names it or one it names, in any mailbox, Drafts and Sent included, that the
|
|
10
|
+
// email's sender took part in: their address in its From, To or Cc); a message outside it is answered "outside this run's
|
|
11
|
+
// thread" and never answered or filed. The thread is read from the message where the trigger found it (its mailbox,
|
|
12
|
+
// UIDVALIDITY and UID, in the firing's attrs), or, once it has moved, its copy from the same sender (the firing's `from`),
|
|
13
|
+
// never from another's that carries the same Message-ID, and the ids a sender writes grant nothing alone (Mail.allows). The agent's own tool setting `scope:
|
|
14
|
+
// "mailbox"` on email_read, email_search or email_file reaches the whole account; any other run reaches the whole account.
|
|
15
|
+
// So an email's words can never send the agent through the rest of the inbox.
|
|
16
|
+
import { defineExtension, defineTool } from "@earendil-works/pi-durable";
|
|
17
|
+
import { forModel } from "../result.js";
|
|
18
|
+
import { isObject } from "../validate.js";
|
|
19
|
+
import { isGmailAccount, keepsSentCopy, keptAccount, NO_ACCOUNT, openAccount } from "./account.js";
|
|
20
|
+
import { EMAIL_ANSWER_MAX_CHARS, emailError, emailText } from "./answer.js";
|
|
21
|
+
import { archiveDescription, EMAIL_ADDRESS, EMAIL_DESCRIPTIONS, EMAIL_DRAFT, EMAIL_FILE, EMAIL_READ, EMAIL_SEARCH, EMAIL_SEND, fileDescription, LIMIT_DEFAULT, LIMIT_MAX, OUTSIDE_THREAD, SEND_CAP } from "./names.js";
|
|
22
|
+
import { composed, foundOf, inThread, involves, MailError, READ_BYTES, readOf, SEARCH_BYTES, SENT_HEADER, smtpSend, TOOL_SOCKET_TIMEOUT_MS, withImap } from "./mail.js";
|
|
23
|
+
export { EMAIL_DRAFT, EMAIL_FILE, EMAIL_READ, EMAIL_SCOPES, EMAIL_SEARCH, EMAIL_SEND, OUTSIDE_THREAD, SCOPED_TOOLS, SEND_CAP, sendSummary } from "./names.js";
|
|
24
|
+
/** The most ids a thread is read by: its message and the 49 nearest it. */
|
|
25
|
+
const THREAD_IDS = 50;
|
|
26
|
+
/** A thread read's bound, kept before any message is fetched whole: the newest THREAD_READ_MAX messages, their bodies
|
|
27
|
+
* together at most THREAD_READ_BYTES (each counted at its size, at most READ_BYTES; the newest always read). The ids of
|
|
28
|
+
* the earlier ones are answered, at most THREAD_OMITTED_MAX of them, nearest first kept. */
|
|
29
|
+
const THREAD_READ_MAX = 20;
|
|
30
|
+
const THREAD_READ_BYTES = 10 * EMAIL_ANSWER_MAX_CHARS;
|
|
31
|
+
const THREAD_OMITTED_MAX = 100;
|
|
32
|
+
/** The most addresses a draft or a send names, to and cc together. */
|
|
33
|
+
const MAX_RECIPIENTS = 50;
|
|
34
|
+
/** The longest text a draft or a send carries. */
|
|
35
|
+
const MAX_TEXT = 200_000;
|
|
36
|
+
/** A call's arguments that do not read: the words the model is answered with. */
|
|
37
|
+
class BadArgs extends Error {
|
|
38
|
+
}
|
|
39
|
+
const text = (args, field, max, required = false) => {
|
|
40
|
+
const value = args[field];
|
|
41
|
+
if (value === undefined || value === null) {
|
|
42
|
+
if (required)
|
|
43
|
+
throw new BadArgs(`${field} is required.`);
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
if (typeof value !== "string" || (required && !value.trim()) || value.length > max)
|
|
47
|
+
throw new BadArgs(`${field} is text of at most ${max} characters.`);
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
const addressList = (args, field, required) => {
|
|
51
|
+
const value = args[field];
|
|
52
|
+
if (value === undefined || value === null) {
|
|
53
|
+
if (required)
|
|
54
|
+
throw new BadArgs(`${field} is a list of email addresses.`);
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
const list = typeof value === "string" ? [value] : value;
|
|
58
|
+
if (!Array.isArray(list) || (required && !list.length) || !list.every((one) => typeof one === "string" && EMAIL_ADDRESS.test(one.trim())))
|
|
59
|
+
throw new BadArgs(`${field} is a list of email addresses, such as ["ana@example.com"].`);
|
|
60
|
+
return list.map((one) => one.trim());
|
|
61
|
+
};
|
|
62
|
+
const messageId = (value) => {
|
|
63
|
+
const id = value.trim();
|
|
64
|
+
return id.startsWith("<") ? id : `<${id}>`;
|
|
65
|
+
};
|
|
66
|
+
/** The root a firing's attrs name (written by the server from the message the trigger read, never by its words): its
|
|
67
|
+
* sender, its Message-ID and where the trigger found it. Null when any of them is missing or does not read, so the run
|
|
68
|
+
* reads nothing (fail closed). */
|
|
69
|
+
export function threadRootOf(attrs) {
|
|
70
|
+
const { from, message_id, mailbox, uidvalidity, uid } = attrs ?? {};
|
|
71
|
+
const named = (value) => typeof value === "string" && value.trim() !== "";
|
|
72
|
+
if (!named(from) || !named(message_id) || !named(mailbox) || !named(uidvalidity) || !named(uid) || !/^[1-9]\d{0,15}$/.test(uid))
|
|
73
|
+
return null;
|
|
74
|
+
return { from: from.trim().toLowerCase(), message_id, mailbox, uidvalidity, uid: Number(uid) };
|
|
75
|
+
}
|
|
76
|
+
/** The scope of `tool`'s calls in the run `host` binds for: the agent's `scope: "mailbox"` reads the whole account; a run
|
|
77
|
+
* an email trigger started reads its thread; any other run, the whole account. */
|
|
78
|
+
async function scopeOf(host, tool) {
|
|
79
|
+
const settings = isObject(host.toolSettings) ? host.toolSettings[tool] : undefined;
|
|
80
|
+
if (isObject(settings) && settings.scope === "mailbox")
|
|
81
|
+
return { kind: "mailbox" };
|
|
82
|
+
const started = host.startedBy;
|
|
83
|
+
if (!started || started.source !== "email")
|
|
84
|
+
return { kind: "mailbox" };
|
|
85
|
+
// The run's launch names the message it fired for and where it was found (its firing's attrs); one that does not reads
|
|
86
|
+
// nothing (fail closed).
|
|
87
|
+
return { kind: "thread", root: threadRootOf(started.trigger.attrs) };
|
|
88
|
+
}
|
|
89
|
+
/** Each run's thread, kept across its tools' calls on one host so it is read once; a host after a pause reads it again
|
|
90
|
+
* (located finds the message where it went). */
|
|
91
|
+
const threads = new WeakMap();
|
|
92
|
+
const threadOf = (host) => {
|
|
93
|
+
let held = threads.get(host);
|
|
94
|
+
if (!held)
|
|
95
|
+
threads.set(host, (held = {}));
|
|
96
|
+
return held;
|
|
97
|
+
};
|
|
98
|
+
const nothing = () => ({ ids: new Set(), sender: null });
|
|
99
|
+
/** The thread of `root`: its sender the firing's own `from`, its ids those the run's message names. That message is read
|
|
100
|
+
* where the trigger found it (that mailbox, still at that UIDVALIDITY, that UID, that Message-ID and that sender), else,
|
|
101
|
+
* once it has moved (filed, archived) or its mailbox was rebuilt, as its copy found by Message-ID from the same sender:
|
|
102
|
+
* never another's that carries the same Message-ID. No such message: nothing. */
|
|
103
|
+
async function located(session, root) {
|
|
104
|
+
if (root === null)
|
|
105
|
+
return nothing();
|
|
106
|
+
const head = (await recorded(session, root)) ?? (await moved(session, root));
|
|
107
|
+
return head ? { ids: new Set(threadIds(root.message_id, head)), sender: root.from } : nothing();
|
|
108
|
+
}
|
|
109
|
+
/** Whether `head` is `root`'s message: its Message-ID, from its sender. */
|
|
110
|
+
const isRoot = (head, root) => head.message_id === root.message_id && head.from.trim().toLowerCase() === root.from;
|
|
111
|
+
/** `root`'s message where the trigger found it, when it is still there. */
|
|
112
|
+
async function recorded(session, root) {
|
|
113
|
+
if (root.mailbox.toUpperCase() !== "INBOX" && !(await session.mailboxes()).some((box) => box.path === root.mailbox))
|
|
114
|
+
return undefined;
|
|
115
|
+
const box = await session.open(root.mailbox);
|
|
116
|
+
if (box.uidValidity !== root.uidvalidity)
|
|
117
|
+
return undefined;
|
|
118
|
+
const [head] = await session.heads(box.path, [root.uid]);
|
|
119
|
+
return head && head.uid === root.uid && isRoot(head, root) ? head : undefined;
|
|
120
|
+
}
|
|
121
|
+
/** `root`'s message wherever it went, by its Message-ID (an indexed search): on Gmail (X-GM-EXT-1) in All Mail, which holds
|
|
122
|
+
* every label's; elsewhere across the account's mailboxes, INBOX first. Only a copy from its sender counts. */
|
|
123
|
+
async function moved(session, root) {
|
|
124
|
+
const all = session.can("X-GM-EXT-1") ? (await session.mailboxes()).find((box) => box.specialUse === "\\All")?.path : undefined;
|
|
125
|
+
for (const path of all ? [all] : await session.searchable()) {
|
|
126
|
+
await session.open(path);
|
|
127
|
+
const uids = await session.uids({ header: { "message-id": root.message_id } });
|
|
128
|
+
const head = (await session.heads(path, uids)).find((one) => isRoot(one, root));
|
|
129
|
+
if (head)
|
|
130
|
+
return head;
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
/** One run's view of the account: the account opened, its scope, and the thread once read (its ids and its sender), kept
|
|
135
|
+
* in `held` for the run. */
|
|
136
|
+
class Mail {
|
|
137
|
+
account;
|
|
138
|
+
scope;
|
|
139
|
+
held;
|
|
140
|
+
now;
|
|
141
|
+
constructor(account, scope, held = {},
|
|
142
|
+
/** The run's clock (a template eval case's `now`): the Date of what it drafts or sends. Absent: the real clock. */
|
|
143
|
+
now) {
|
|
144
|
+
this.account = account;
|
|
145
|
+
this.scope = scope;
|
|
146
|
+
this.held = held;
|
|
147
|
+
this.now = now;
|
|
148
|
+
}
|
|
149
|
+
/** The thread as read once a run (none when the run reads the whole account). A failure to read it (the server out of
|
|
150
|
+
* reach) is not kept: the next call tries again. */
|
|
151
|
+
threadView(session) {
|
|
152
|
+
if (this.scope.kind === "mailbox")
|
|
153
|
+
return undefined;
|
|
154
|
+
if (this.held.view)
|
|
155
|
+
return this.held.view;
|
|
156
|
+
const view = located(session, this.scope.root);
|
|
157
|
+
this.held.view = view;
|
|
158
|
+
view.catch(() => {
|
|
159
|
+
if (this.held.view === view)
|
|
160
|
+
this.held.view = undefined;
|
|
161
|
+
});
|
|
162
|
+
return view;
|
|
163
|
+
}
|
|
164
|
+
/** The thread's ids, read once a run: none when the run reads the whole account. */
|
|
165
|
+
async thread(session) {
|
|
166
|
+
return (await this.threadView(session))?.ids;
|
|
167
|
+
}
|
|
168
|
+
/** The UIDs of `path` (open) that `query` takes, in the run's scope: a thread's search is the intersection of the two,
|
|
169
|
+
* so a message outside it is never fetched. */
|
|
170
|
+
async uids(session, query) {
|
|
171
|
+
const thread = await this.thread(session);
|
|
172
|
+
const found = await session.uids(query);
|
|
173
|
+
if (!thread)
|
|
174
|
+
return found;
|
|
175
|
+
if (!thread.size)
|
|
176
|
+
return [];
|
|
177
|
+
const inside = new Set(await session.uids(session.threadQuery([...thread])));
|
|
178
|
+
return found.filter((uid) => inside.has(uid));
|
|
179
|
+
}
|
|
180
|
+
/** The thread's people, lowercased: the account's own address and every From, To and Cc of the thread's messages (those
|
|
181
|
+
* `allows` takes), across the account's mailboxes; undefined when the run reads the whole account. */
|
|
182
|
+
async people(session) {
|
|
183
|
+
const ids = await this.thread(session);
|
|
184
|
+
if (!ids)
|
|
185
|
+
return undefined;
|
|
186
|
+
const people = new Set([this.account.address.trim().toLowerCase()]);
|
|
187
|
+
if (!ids.size)
|
|
188
|
+
return people;
|
|
189
|
+
for (const path of await session.searchable()) {
|
|
190
|
+
await session.open(path);
|
|
191
|
+
for (const head of await session.heads(path, await session.uids(session.threadQuery([...ids])))) {
|
|
192
|
+
if (inThread(head, ids) && (await this.allows(session, head)))
|
|
193
|
+
for (const one of [head.from, ...head.to, ...head.cc])
|
|
194
|
+
people.add(one.trim().toLowerCase());
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return people;
|
|
198
|
+
}
|
|
199
|
+
/** Whether `m` is in the run's scope. On a thread: a message linked to the one that started the run (by its ids,
|
|
200
|
+
* In-Reply-To or References), that message among them, that its sender took part in: their address in its From, To or
|
|
201
|
+
* Cc, as on the account's own mail to them. The ids a sender writes grant nothing alone: naming another conversation's
|
|
202
|
+
* Message-ID in References, or reusing it as their own, reads none of it. What is left: a forged From is the receiving
|
|
203
|
+
* server's to refuse (DMARC), and even then the run reaches only the conversations that address took part in. */
|
|
204
|
+
async allows(session, m) {
|
|
205
|
+
const view = await this.threadView(session);
|
|
206
|
+
if (!view)
|
|
207
|
+
return true;
|
|
208
|
+
return view.sender !== null && inThread(m, view.ids) && involves(m, view.sender);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/** `work` in one IMAP session of the run's account, the thread's ids read first (they open mailboxes of their own). It waits
|
|
212
|
+
* on the server TOOL_SOCKET_TIMEOUT_MS: a thread's search by In-Reply-To, unindexed, takes minutes on a large mailbox. */
|
|
213
|
+
async function withMail(mail, work) {
|
|
214
|
+
return withImap(mail.account, async (session) => {
|
|
215
|
+
await mail.thread(session);
|
|
216
|
+
return work(session);
|
|
217
|
+
}, { socketTimeoutMs: TOOL_SOCKET_TIMEOUT_MS });
|
|
218
|
+
}
|
|
219
|
+
/** The ids a thread is read by: its message's own, then those it names, nearest first, at most THREAD_IDS. */
|
|
220
|
+
function threadIds(root, found) {
|
|
221
|
+
const named = found ? [...(found.in_reply_to ? [found.in_reply_to] : []), ...[...found.references].reverse()] : [];
|
|
222
|
+
return [...new Set([root, ...named])].slice(0, THREAD_IDS);
|
|
223
|
+
}
|
|
224
|
+
/** The message of Message-ID `id`, the first found across the account's mailboxes (INBOX first), read in full; with
|
|
225
|
+
* `mail`, only within its scope. */
|
|
226
|
+
async function findById(session, id, mail) {
|
|
227
|
+
for (const path of await session.searchable()) {
|
|
228
|
+
await session.open(path);
|
|
229
|
+
const query = { header: { "message-id": id } };
|
|
230
|
+
const uids = mail ? await mail.uids(session, query) : await session.uids(query);
|
|
231
|
+
for (const m of await session.fetch(path, uids.slice(0, 5), READ_BYTES))
|
|
232
|
+
if (m.message_id === id && (!mail || (await mail.allows(session, m))))
|
|
233
|
+
return m;
|
|
234
|
+
}
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
/** Newest first, a message with no date last. */
|
|
238
|
+
const newestFirst = (a, b) => (b.date ?? "").localeCompare(a.date ?? "") || b.uid - a.uid;
|
|
239
|
+
async function search(mail, args) {
|
|
240
|
+
const from = text(args, "from", 200);
|
|
241
|
+
const to = text(args, "to", 200);
|
|
242
|
+
const subject = text(args, "subject", 200);
|
|
243
|
+
const words = text(args, "text", 200);
|
|
244
|
+
const sinceText = text(args, "since", 40);
|
|
245
|
+
const since = sinceText === undefined ? undefined : new Date(sinceText);
|
|
246
|
+
if (since && !Number.isFinite(since.getTime()))
|
|
247
|
+
throw new BadArgs("since is a date, such as 2026-06-01.");
|
|
248
|
+
if (args.unread !== undefined && typeof args.unread !== "boolean")
|
|
249
|
+
throw new BadArgs("unread is true or false.");
|
|
250
|
+
const limit = args.limit === undefined ? LIMIT_DEFAULT : args.limit;
|
|
251
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > LIMIT_MAX)
|
|
252
|
+
throw new BadArgs(`limit is a whole number, 1 to ${LIMIT_MAX}.`);
|
|
253
|
+
const name = text(args, "mailbox", 300) ?? "INBOX";
|
|
254
|
+
return withMail(mail, async (session) => {
|
|
255
|
+
const path = await session.resolve(name);
|
|
256
|
+
await session.open(path);
|
|
257
|
+
const query = { ...(from ? { from } : {}), ...(to ? { to } : {}), ...(subject ? { subject } : {}), ...(words ? { text: words } : {}), ...(since ? { since } : {}), ...(args.unread === true ? { seen: false } : args.unread === false ? { seen: true } : {}) };
|
|
258
|
+
const uids = await mail.uids(session, query);
|
|
259
|
+
// On a thread, the messages the sender may read are judged on their headers first, so the limit counts only those:
|
|
260
|
+
// a newer one they took no part in never takes the slot of an older one of theirs. The newest arrivals, then by
|
|
261
|
+
// their dates.
|
|
262
|
+
const readable = mail.scope.kind === "thread" ? await headsAllowed(session, mail, path, uids) : uids;
|
|
263
|
+
const found = await session.fetch(path, readable.slice(-limit), SEARCH_BYTES);
|
|
264
|
+
const kept = [];
|
|
265
|
+
for (const m of found)
|
|
266
|
+
if (await mail.allows(session, m))
|
|
267
|
+
kept.push(m);
|
|
268
|
+
return { messages: kept.sort(newestFirst).slice(0, limit).map(foundOf) };
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/** The UIDs of `uids` (of `path`, open) whose headers the run's scope allows, lowest first: no body is fetched. */
|
|
272
|
+
async function headsAllowed(session, mail, path, uids) {
|
|
273
|
+
const kept = [];
|
|
274
|
+
for (const head of await session.heads(path, uids))
|
|
275
|
+
if (await mail.allows(session, head))
|
|
276
|
+
kept.push(head.uid);
|
|
277
|
+
return kept;
|
|
278
|
+
}
|
|
279
|
+
async function read(mail, args) {
|
|
280
|
+
const id = messageId(text(args, "message_id", 998, true));
|
|
281
|
+
if (args.thread !== undefined && typeof args.thread !== "boolean")
|
|
282
|
+
throw new BadArgs("thread is true or false.");
|
|
283
|
+
return withMail(mail, async (session) => {
|
|
284
|
+
const found = await findById(session, id, mail);
|
|
285
|
+
if (!found)
|
|
286
|
+
throw new MailError("missing", mail.scope.kind === "thread" ? `${id} is ${OUTSIDE_THREAD}, or not in this account: this run reads only the thread of the email that started it.` : `No message ${id} in this account.`);
|
|
287
|
+
if (args.thread !== true)
|
|
288
|
+
return { messages: [readOf(found)] };
|
|
289
|
+
const ids = new Set(threadIds(id, found));
|
|
290
|
+
// The thread found by its headers alone (ids, date, size: no body), each message once, the first mailbox's copy.
|
|
291
|
+
const heads = new Map();
|
|
292
|
+
for (const path of await session.searchable()) {
|
|
293
|
+
await session.open(path);
|
|
294
|
+
const uids = await mail.uids(session, session.threadQuery([...ids]));
|
|
295
|
+
for (const head of await session.heads(path, uids)) {
|
|
296
|
+
if (!inThread(head, ids) || !(await mail.allows(session, head)))
|
|
297
|
+
continue;
|
|
298
|
+
const key = head.message_id ?? `${path}\n${head.uid}`;
|
|
299
|
+
if (!heads.has(key))
|
|
300
|
+
heads.set(key, head);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// The newest that fit the bound; the first that does not ends it, so every one left out is earlier than those read.
|
|
304
|
+
const newest = [...heads.values()].sort(newestFirst);
|
|
305
|
+
const picked = [];
|
|
306
|
+
let bytes = 0;
|
|
307
|
+
for (const head of newest) {
|
|
308
|
+
const size = Math.min(head.size > 0 ? head.size : READ_BYTES, READ_BYTES);
|
|
309
|
+
if (picked.length === THREAD_READ_MAX || (picked.length && bytes + size > THREAD_READ_BYTES))
|
|
310
|
+
break;
|
|
311
|
+
picked.push(head);
|
|
312
|
+
bytes += size;
|
|
313
|
+
}
|
|
314
|
+
// Only those fetched whole, mailbox by mailbox.
|
|
315
|
+
const byPath = new Map();
|
|
316
|
+
for (const head of picked)
|
|
317
|
+
byPath.set(head.mailbox, [...(byPath.get(head.mailbox) ?? []), head.uid]);
|
|
318
|
+
const thread = [];
|
|
319
|
+
for (const [path, uids] of byPath) {
|
|
320
|
+
await session.open(path);
|
|
321
|
+
for (const m of await session.fetch(path, uids, READ_BYTES))
|
|
322
|
+
if (inThread(m, ids) && (await mail.allows(session, m)))
|
|
323
|
+
thread.push(m);
|
|
324
|
+
}
|
|
325
|
+
thread.sort((a, b) => (a.date ?? "").localeCompare(b.date ?? "") || a.uid - b.uid);
|
|
326
|
+
const left = newest.slice(picked.length).reverse();
|
|
327
|
+
if (!left.length)
|
|
328
|
+
return { messages: thread.map(readOf) };
|
|
329
|
+
const omitted = left.flatMap((head) => (head.message_id ? [head.message_id] : [])).slice(-THREAD_OMITTED_MAX);
|
|
330
|
+
const n = left.length;
|
|
331
|
+
return { messages: thread.map(readOf), truncated: true, more: n, omitted, note: `${n} earlier message${n === 1 ? "" : "s"} in this thread not read; email_read one by message_id` };
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/** What a draft or a send says: its recipients, subject, text and the message it answers. */
|
|
335
|
+
function letterOf(args) {
|
|
336
|
+
const to = addressList(args, "to", true);
|
|
337
|
+
const cc = addressList(args, "cc", false);
|
|
338
|
+
if (to.length + cc.length > MAX_RECIPIENTS)
|
|
339
|
+
throw new BadArgs(`At most ${MAX_RECIPIENTS} addresses, to and cc together.`);
|
|
340
|
+
const subject = text(args, "subject", 998, true);
|
|
341
|
+
if (/[\r\n]/.test(subject))
|
|
342
|
+
throw new BadArgs("subject is one line.");
|
|
343
|
+
const body = text(args, "text", MAX_TEXT, true);
|
|
344
|
+
const answers = text(args, "in_reply_to", 998);
|
|
345
|
+
return { to, cc, subject, text: body, ...(answers ? { inReplyTo: messageId(answers) } : {}) };
|
|
346
|
+
}
|
|
347
|
+
/** The References of a reply to `id`: the message's own and its id, read within the run's scope; just `id` when it is not
|
|
348
|
+
* found there. */
|
|
349
|
+
async function referencesOf(session, mail, id) {
|
|
350
|
+
const found = await findById(session, id, mail);
|
|
351
|
+
const before = found ? (found.references.length ? found.references : found.in_reply_to ? [found.in_reply_to] : []) : [];
|
|
352
|
+
return [...new Set([...before, id])];
|
|
353
|
+
}
|
|
354
|
+
async function draft(mail, args) {
|
|
355
|
+
const letter = letterOf(args);
|
|
356
|
+
return withMail(mail, async (session) => {
|
|
357
|
+
const references = letter.inReplyTo ? await referencesOf(session, mail, letter.inReplyTo) : undefined;
|
|
358
|
+
const built = await composed({ from: mail.account.address, ...letter, ...(references ? { references } : {}), ...(mail.now ? { date: mail.now } : {}) });
|
|
359
|
+
const drafts = await session.resolve("Drafts");
|
|
360
|
+
await session.append(drafts, built.raw, ["\\Draft", "\\Seen"]);
|
|
361
|
+
return { saved: true, mailbox: drafts, message_id: built.messageId };
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
async function send(mail, args, runId) {
|
|
365
|
+
const letter = letterOf(args);
|
|
366
|
+
// The thread it answers, read first; a mail server that can't be read leaves the reply threaded by In-Reply-To alone.
|
|
367
|
+
const references = letter.inReplyTo ? await withMail(mail, (session) => referencesOf(session, mail, letter.inReplyTo)).catch(() => [letter.inReplyTo]) : undefined;
|
|
368
|
+
const built = await composed({ from: mail.account.address, ...letter, ...(references ? { references } : {}), headers: { [SENT_HEADER]: runId }, ...(mail.now ? { date: mail.now } : {}) });
|
|
369
|
+
const { rejected } = await smtpSend(mail.account, [...letter.to, ...letter.cc], built.raw);
|
|
370
|
+
// Some refused, the rest taken: sent to those, said by name, and nobody sent to again.
|
|
371
|
+
const refused = rejected.length ? `not delivered to ${rejected.join(", ")}: the mail server refused them` : undefined;
|
|
372
|
+
// Sent, whatever the copy does: a copy not filed is said, never a send that failed.
|
|
373
|
+
const filed = keepsSentCopy(mail.account) ? undefined : await withMail(mail, async (session) => session.append(await session.resolve("Sent"), built.raw, ["\\Seen"])).then(() => undefined, (error) => error.message);
|
|
374
|
+
const notes = [refused, filed ? `Sent, but no copy was filed in Sent: ${filed}` : undefined].filter(Boolean);
|
|
375
|
+
return { sent: true, message_id: built.messageId, ...(rejected.length ? { rejected } : {}), ...(notes.length ? { note: notes.join("; ") } : {}) };
|
|
376
|
+
}
|
|
377
|
+
/** "a", "a and b", "a, b and c". */
|
|
378
|
+
const listed = (names) => (names.length < 2 ? names.join("") : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`);
|
|
379
|
+
/** Why an email_send its setting allows asks a person anyway (tools.ts ToolEntry.asks), or nothing: the run has sent
|
|
380
|
+
* SEND_CAP emails or more (its record's count, across pauses); or, on a run that reads only its email's thread (email_read's
|
|
381
|
+
* scope), it sends to someone outside the thread's people (Mail.people), each named. Both, when both. A thread that can't
|
|
382
|
+
* be read asks too, saying so. A call whose arguments don't read asks nothing: the tool refuses it in its own words. */
|
|
383
|
+
export async function sendAsks(args, ctx) {
|
|
384
|
+
const why = [];
|
|
385
|
+
const sent = await ctx.ran(EMAIL_SEND);
|
|
386
|
+
if (sent >= SEND_CAP)
|
|
387
|
+
why.push(`this run has sent ${sent} emails`);
|
|
388
|
+
const outside = await outsideWords(ctx.host, args);
|
|
389
|
+
if (outside)
|
|
390
|
+
why.push(outside);
|
|
391
|
+
return why.length ? why.join("; ") : undefined;
|
|
392
|
+
}
|
|
393
|
+
/** The recipients of the send `args` outside the run's thread, in words; nothing on a run that reads the whole account. */
|
|
394
|
+
async function outsideWords(host, args) {
|
|
395
|
+
const scope = await scopeOf(host, EMAIL_READ);
|
|
396
|
+
if (scope.kind === "mailbox" || !host.store || !host.sealKey)
|
|
397
|
+
return undefined;
|
|
398
|
+
let recipients;
|
|
399
|
+
try {
|
|
400
|
+
const letter = letterOf(args);
|
|
401
|
+
recipients = [...new Set([...letter.to, ...letter.cc].map((one) => one.toLowerCase()))];
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
return undefined;
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
const account = await openAccount(host.store, host.sealKey);
|
|
408
|
+
// No account, or one a template eval's run may not open: the send itself is refused in its own words.
|
|
409
|
+
if (!account || !fakeHeld(host, account))
|
|
410
|
+
return undefined;
|
|
411
|
+
const mail = new Mail(account, scope, threadOf(host));
|
|
412
|
+
const people = await withMail(mail, (session) => mail.people(session));
|
|
413
|
+
const outside = people ? recipients.filter((one) => !people.has(one)) : [];
|
|
414
|
+
return outside.length ? `${listed(outside)} ${outside.length === 1 ? "is" : "are"} outside this email's thread` : undefined;
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
return `its recipients couldn't be checked against this email's thread (${error?.message ?? String(error)})`;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/** Whether two servers are the same host and port. */
|
|
421
|
+
const same = (a, b) => a.host === b.host && a.port === b.port;
|
|
422
|
+
/** False when `account` is not the one this run may open: a template eval's run opens only its case's fake account, so a
|
|
423
|
+
* real one saved meanwhile is never reached. */
|
|
424
|
+
function fakeHeld(host, account) {
|
|
425
|
+
const fake = host.emailFake;
|
|
426
|
+
return !fake || (same(account.imap, fake.imap) && same(account.smtp, fake.smtp));
|
|
427
|
+
}
|
|
428
|
+
/** The names email_file refuses whatever the account flags: the trash and junk folders by their usual names. */
|
|
429
|
+
const BINS = new Set(["trash", "junk", "spam", "[gmail]/trash", "[gmail]/spam", "\\trash", "\\junk"]);
|
|
430
|
+
/** A folders server's answer to an explicit archive: false, a filing that would keep the message in the inbox. */
|
|
431
|
+
const FOLDERS_REFUSAL = "this server has folders: filing moves the message out of the inbox; pass archive: true to move it";
|
|
432
|
+
/** Gmail's label for a message in the inbox: taken off, the message is archived. */
|
|
433
|
+
const INBOX_LABEL = "\\Inbox";
|
|
434
|
+
/** The message `id` filed under `to`: a Gmail label (archive: true takes its \Inbox label off too), else a move to the
|
|
435
|
+
* folder `to`, created if missing, with no archive or archive: true. Never a delete: the trash and junk folders are
|
|
436
|
+
* refused before anything is touched; and an explicit archive: false never takes mail out of the inbox. */
|
|
437
|
+
async function file(mail, args) {
|
|
438
|
+
const id = messageId(text(args, "message_id", 998, true));
|
|
439
|
+
const to = text(args, "to", 300, true).trim();
|
|
440
|
+
if (/[\u0000-\u001f]/.test(to))
|
|
441
|
+
throw new BadArgs("to is a folder's or a label's name, on one line.");
|
|
442
|
+
if (args.archive !== undefined && typeof args.archive !== "boolean")
|
|
443
|
+
throw new BadArgs("archive is true or false.");
|
|
444
|
+
const refused = () => new BadArgs(`${EMAIL_FILE} files mail, it never deletes it: ${to} is the trash or junk folder.`);
|
|
445
|
+
if (BINS.has(to.toLowerCase()))
|
|
446
|
+
throw refused();
|
|
447
|
+
return withMail(mail, async (session) => {
|
|
448
|
+
const bin = (await session.mailboxes()).find((box) => box.path === to && (box.specialUse === "\\Trash" || box.specialUse === "\\Junk"));
|
|
449
|
+
if (bin)
|
|
450
|
+
throw refused();
|
|
451
|
+
const found = await findById(session, id, mail);
|
|
452
|
+
if (!found)
|
|
453
|
+
throw new MailError("missing", mail.scope.kind === "thread" ? `${id} is ${OUTSIDE_THREAD}, or not in this account: this run files only the thread of the email that started it.` : `No message ${id} in this account.`);
|
|
454
|
+
const done = (archived, note) => ({ filed: true, message_id: id, to, archived, subject: found.subject, ...(note ? { note } : {}) });
|
|
455
|
+
if (session.can("X-GM-EXT-1")) {
|
|
456
|
+
await session.ensure(to);
|
|
457
|
+
await session.select(found.mailbox);
|
|
458
|
+
await session.label(found.uid, [to]);
|
|
459
|
+
const leaves = args.archive === true && found.mailbox.toUpperCase() === "INBOX";
|
|
460
|
+
if (leaves)
|
|
461
|
+
await session.label(found.uid, [INBOX_LABEL], true);
|
|
462
|
+
return done(leaves);
|
|
463
|
+
}
|
|
464
|
+
// A folders server: filing is a move out of where the message is, so no archive files and moves it as archive: true
|
|
465
|
+
// does; an explicit archive: false never takes mail out of the inbox.
|
|
466
|
+
if (args.archive === false)
|
|
467
|
+
throw new BadArgs(FOLDERS_REFUSAL);
|
|
468
|
+
await session.ensure(to);
|
|
469
|
+
await session.select(found.mailbox);
|
|
470
|
+
await session.move(found.uid, to);
|
|
471
|
+
// The answer says the move: the model reads the answer, not the timeline.
|
|
472
|
+
return done(true, `moved out of the inbox to ${to}`);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
const STRING = { type: "string" };
|
|
476
|
+
const ADDRESSES = { type: "array", items: STRING, description: "Email addresses, such as [\"ana@example.com\"]." };
|
|
477
|
+
/** The five tools, by name: their words for the model (names.ts EMAIL_DESCRIPTIONS, the catalog's too), their arguments,
|
|
478
|
+
* how a call cut off by a crash is replayed, and what a call does. */
|
|
479
|
+
const TOOLS = {
|
|
480
|
+
[EMAIL_SEARCH]: {
|
|
481
|
+
description: EMAIL_DESCRIPTIONS[EMAIL_SEARCH],
|
|
482
|
+
properties: { from: STRING, to: STRING, subject: STRING, text: { type: "string", description: "Words anywhere in the message." }, since: { type: "string", description: "A date, such as 2026-06-01: messages from then on." }, unread: { type: "boolean" }, mailbox: { type: "string", description: "INBOX (default), Drafts, Sent, or a folder's name." }, limit: { type: "number", description: `1-${LIMIT_MAX} (default ${LIMIT_DEFAULT}).` } },
|
|
483
|
+
required: [],
|
|
484
|
+
replay: "safe",
|
|
485
|
+
run: (mail, args) => search(mail, args),
|
|
486
|
+
},
|
|
487
|
+
[EMAIL_READ]: {
|
|
488
|
+
description: EMAIL_DESCRIPTIONS[EMAIL_READ],
|
|
489
|
+
properties: { message_id: { type: "string", minLength: 1, description: "The Message-ID, such as <abc@example.com>." }, thread: { type: "boolean", description: "Read the whole thread (default false)." } },
|
|
490
|
+
required: ["message_id"],
|
|
491
|
+
replay: "safe",
|
|
492
|
+
run: (mail, args) => read(mail, args),
|
|
493
|
+
},
|
|
494
|
+
[EMAIL_DRAFT]: {
|
|
495
|
+
description: EMAIL_DESCRIPTIONS[EMAIL_DRAFT],
|
|
496
|
+
properties: { to: ADDRESSES, cc: ADDRESSES, subject: STRING, text: { type: "string", description: "The message, as plain text." }, in_reply_to: { type: "string", description: "The message_id it answers." } },
|
|
497
|
+
required: ["to", "subject", "text"],
|
|
498
|
+
replay: "unsafe",
|
|
499
|
+
run: (mail, args) => draft(mail, args),
|
|
500
|
+
},
|
|
501
|
+
[EMAIL_SEND]: {
|
|
502
|
+
description: EMAIL_DESCRIPTIONS[EMAIL_SEND],
|
|
503
|
+
properties: { to: ADDRESSES, cc: ADDRESSES, subject: STRING, text: { type: "string", description: "The message, as plain text." }, in_reply_to: { type: "string", description: "The message_id it answers." } },
|
|
504
|
+
required: ["to", "subject", "text"],
|
|
505
|
+
replay: "unsafe",
|
|
506
|
+
run: (mail, args, host) => send(mail, args, host.where.ref.id),
|
|
507
|
+
},
|
|
508
|
+
[EMAIL_FILE]: {
|
|
509
|
+
description: EMAIL_DESCRIPTIONS[EMAIL_FILE],
|
|
510
|
+
properties: { message_id: { type: "string", minLength: 1, description: "The Message-ID, such as <abc@example.com>." }, to: { type: "string", minLength: 1, description: "The folder or label, such as Receipts." }, archive: { type: "boolean", description: archiveDescription(undefined) } },
|
|
511
|
+
required: ["message_id", "to"],
|
|
512
|
+
replay: "unsafe",
|
|
513
|
+
run: (mail, args) => file(mail, args),
|
|
514
|
+
},
|
|
515
|
+
};
|
|
516
|
+
/** The five tools, for a run `host` serves (host-bounds.test.mjs lists them as the Claude Agent CLI is given them). */
|
|
517
|
+
export const emailRegistrations = (host) => Object.keys(TOOLS).map((name) => emailTool(name, host));
|
|
518
|
+
/** `name`'s tool, bound for the run `host` serves: its account opened at each call (a password saved since counts), its
|
|
519
|
+
* scope read once. email_file's description and archive parameter say what filing does on `kind`'s server, both kinds
|
|
520
|
+
* when it is not known. */
|
|
521
|
+
function emailTool(name, host, kind) {
|
|
522
|
+
const tool = TOOLS[name];
|
|
523
|
+
let scope;
|
|
524
|
+
const filing = name === EMAIL_FILE;
|
|
525
|
+
const properties = filing ? { ...tool.properties, archive: { type: "boolean", description: archiveDescription(kind) } } : tool.properties;
|
|
526
|
+
return defineTool({
|
|
527
|
+
name,
|
|
528
|
+
description: filing ? fileDescription(kind) : tool.description,
|
|
529
|
+
parameters: { type: "object", additionalProperties: false, properties, required: tool.required },
|
|
530
|
+
replay: tool.replay,
|
|
531
|
+
execute: async (args) => {
|
|
532
|
+
try {
|
|
533
|
+
if (!host.store || !host.sealKey)
|
|
534
|
+
return forModel(`${name}: the email tools run on the runner, which this run is not on.`, { isError: true });
|
|
535
|
+
const account = await openAccount(host.store, host.sealKey);
|
|
536
|
+
if (!account)
|
|
537
|
+
return forModel(`${name}: ${NO_ACCOUNT}`, { isError: true });
|
|
538
|
+
if (!fakeHeld(host, account))
|
|
539
|
+
return forModel(`${name}: this eval run reads only its case's fake mailbox, and the server's email account is another.`, { isError: true });
|
|
540
|
+
const mail = new Mail(account, await (scope ??= scopeOf(host, name)), threadOf(host), host.now);
|
|
541
|
+
const answer = await tool.run(mail, (isObject(args) ? args : {}), host);
|
|
542
|
+
return forModel(emailText(answer));
|
|
543
|
+
}
|
|
544
|
+
catch (error) {
|
|
545
|
+
// Words that already start with the tool's name say it once.
|
|
546
|
+
if (error instanceof BadArgs || error instanceof MailError)
|
|
547
|
+
return forModel(emailError(error.message.startsWith(`${name} `) ? error.message : `${name}: ${error.message}`), { isError: true });
|
|
548
|
+
throw error;
|
|
549
|
+
}
|
|
550
|
+
},
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
/** `name` bound for the run `host` serves (tools.ts emailTool, its catalog entry). email_file says the saved account's
|
|
554
|
+
* kind of server, read once at bind from the account's row (no connection; the password is never used); a template
|
|
555
|
+
* eval's fake account says the kind it plays. */
|
|
556
|
+
export async function bindEmail(name, host) {
|
|
557
|
+
const kept = name === EMAIL_FILE && !host.emailFake && host.store && host.sealKey ? await keptAccount(host.store, host.sealKey) : undefined;
|
|
558
|
+
const registration = emailTool(name, host, host.emailFake ? host.emailFake.kind : kept ? (isGmailAccount(kept) ? "gmail" : "folders") : undefined);
|
|
559
|
+
const extension = defineExtension({ name: name.replaceAll("_", "-"), tools: [registration] });
|
|
560
|
+
host.install(extension);
|
|
561
|
+
return { tools: [registration], extensions: [extension] };
|
|
562
|
+
}
|
package/dist/environment.d.ts
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
export declare
|
|
1
|
+
/** This server's zone (TZ), UTC when that is not one ICU knows. */
|
|
2
|
+
export declare const serverZone: () => string;
|
|
3
|
+
/** `at`'s day as a person in `zone` reads it ("Sunday 15 November 2026"); an unknown zone reads as UTC. */
|
|
4
|
+
export declare function dayIn(at: Date, zone: string): string;
|
|
5
|
+
/** The zone a run's day is told in: its trigger's when a schedule started it, else this server's (TZ), else UTC. */
|
|
6
|
+
export declare function runZone(store: {
|
|
7
|
+
triggers: {
|
|
8
|
+
get(id: string): Promise<{
|
|
9
|
+
source: unknown;
|
|
10
|
+
} | undefined | null>;
|
|
11
|
+
};
|
|
12
|
+
}, origin?: {
|
|
13
|
+
kind: string;
|
|
14
|
+
trigger_id?: string;
|
|
15
|
+
}): Promise<string>;
|
|
16
|
+
/** The environment line on `now`'s day in `zone` (default: this server's zone, UTC when that is not valid). */
|
|
17
|
+
export declare function environmentLine(now?: Date, zone?: string): string;
|
package/dist/environment.js
CHANGED
|
@@ -1,9 +1,37 @@
|
|
|
1
|
-
// The line every harness's system prompt starts with: today's date
|
|
2
|
-
// the date its training ended, so a question about "the latest" release is answered from then unless the
|
|
3
|
-
// day it is. One line for every harness, made as each claim opens. pi re-makes its system prompt at each
|
|
4
|
-
// its own current date to each turn's context; claude-agent's CLI resends the prompt recorded at the
|
|
5
|
-
// request, so its harness tells a later day in that day's first turn (run-adapters-claude-agent
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// The line every harness's system prompt starts with: today's date in the run's time zone, and where the run works. A
|
|
2
|
+
// model knows only the date its training ended, so a question about "the latest" release is answered from then unless the
|
|
3
|
+
// run says what day it is. One line for every harness, made as each claim opens. pi re-makes its system prompt at each
|
|
4
|
+
// open; Codex adds its own current date to each turn's context; claude-agent's CLI resends the prompt recorded at the
|
|
5
|
+
// session's first request, so its harness tells a later day in that day's first turn (run-adapters-claude-agent
|
|
6
|
+
// harness.ts dateLine).
|
|
7
|
+
//
|
|
8
|
+
// The line carries no hour: it changes once a day, so a prompt cache holds within the day. The hour comes from the
|
|
9
|
+
// model's clock tool or a trigger's own line (triggers.ts inputOf).
|
|
10
|
+
import { validZone } from "./trigger-adapter.js";
|
|
11
|
+
import { TRIGGER_ADAPTERS } from "./trigger-adapters.js";
|
|
12
|
+
/** A zone this server's ICU knows, else UTC. */
|
|
13
|
+
const known = (zone) => (zone !== undefined && validZone(zone) ? zone : "UTC");
|
|
14
|
+
/** This server's zone (TZ), UTC when that is not one ICU knows. */
|
|
15
|
+
export const serverZone = () => known(Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
16
|
+
/** `at`'s day as a person in `zone` reads it ("Sunday 15 November 2026"); an unknown zone reads as UTC. */
|
|
17
|
+
export function dayIn(at, zone) {
|
|
18
|
+
return new Intl.DateTimeFormat("en-GB", { timeZone: known(zone), weekday: "long", day: "numeric", month: "long", year: "numeric" })
|
|
19
|
+
.format(at)
|
|
20
|
+
.replace(",", "");
|
|
21
|
+
}
|
|
22
|
+
/** The zone a run's day is told in: its trigger's when a schedule started it, else this server's (TZ), else UTC. */
|
|
23
|
+
export async function runZone(store, origin) {
|
|
24
|
+
if (origin?.kind === "trigger" && origin.trigger_id !== undefined) {
|
|
25
|
+
const row = await store.triggers.get(origin.trigger_id);
|
|
26
|
+
const source = row?.source;
|
|
27
|
+
const zone = source?.kind === undefined ? undefined : TRIGGER_ADAPTERS.find((a) => a.id === source.kind)?.zone?.(source);
|
|
28
|
+
if (zone !== undefined)
|
|
29
|
+
return known(zone);
|
|
30
|
+
}
|
|
31
|
+
return serverZone();
|
|
32
|
+
}
|
|
33
|
+
/** The environment line on `now`'s day in `zone` (default: this server's zone, UTC when that is not valid). */
|
|
34
|
+
export function environmentLine(now = new Date(), zone = serverZone()) {
|
|
35
|
+
const named = known(zone);
|
|
36
|
+
return `Today is ${dayIn(now, named)} (${named}). You are running on AgentRun; your workspace is your working directory, and file paths are relative to it.`;
|
|
9
37
|
}
|
package/dist/errors.d.ts
CHANGED
|
@@ -46,8 +46,14 @@ export declare const ERROR_STATUS: {
|
|
|
46
46
|
readonly github_no_public_url: 409;
|
|
47
47
|
readonly github_wrong_origin: 409;
|
|
48
48
|
readonly github_failed: 502;
|
|
49
|
+
readonly composio_no_public_url: 409;
|
|
50
|
+
readonly composio_subscription_elsewhere: 409;
|
|
51
|
+
readonly composio_not_connected: 409;
|
|
52
|
+
readonly composio_account_ambiguous: 409;
|
|
53
|
+
readonly composio_in_use: 409;
|
|
49
54
|
readonly external_body_conflict: 409;
|
|
50
55
|
readonly learned_not_active: 409;
|
|
56
|
+
readonly replay_conflict: 409;
|
|
51
57
|
};
|
|
52
58
|
export type ErrorCode = keyof typeof ERROR_STATUS;
|
|
53
59
|
/** A field that is missing or wrong. `example`: a value the field accepts, read from the request body's schema (explain.ts). */
|