@parcha/agentrun-serve 0.1.0-beta.37 → 0.1.0-beta.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +700 -0
- package/README.md +441 -177
- package/dist/adapters.d.ts +4 -1
- package/dist/adapters.js +4 -2
- package/dist/agent-rows.d.ts +1 -3
- package/dist/agents-store.d.ts +35 -3
- package/dist/agents-store.js +48 -23
- package/dist/answer-json.d.ts +34 -1
- package/dist/answer-json.js +91 -9
- package/dist/api-only.d.ts +1 -1
- package/dist/api-only.js +2 -2
- package/dist/bash-read-only.js +2 -1
- package/dist/browser-cost.js +52 -93
- package/dist/cap-short.d.ts +12 -0
- package/dist/cap-short.js +60 -0
- package/dist/cap.d.ts +3 -400
- package/dist/cap.js +6 -942
- package/dist/catalog.d.ts +0 -5
- package/dist/catalog.js +2 -6
- 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 +25 -0
- package/dist/composio-preset.js +51 -8
- package/dist/composio-triggers.d.ts +113 -0
- package/dist/composio-triggers.js +411 -0
- package/dist/computer-calls.d.ts +36 -24
- package/dist/computer-calls.js +110 -27
- package/dist/computer-commands.d.ts +2 -0
- package/dist/computer-commands.js +7 -3
- package/dist/computer-live.d.ts +12 -2
- package/dist/computer-live.js +21 -5
- package/dist/computer-routes.d.ts +2 -2
- package/dist/computer-routes.js +2 -2
- package/dist/computer-setting.d.ts +15 -2
- package/dist/computer-setting.js +20 -4
- package/dist/computer-view.d.ts +18 -2
- package/dist/computer-view.js +21 -6
- package/dist/computer-viewer.js +40 -13
- package/dist/computers.d.ts +8 -2
- package/dist/computers.js +18 -8
- package/dist/console/assets/index-BNhZs-jp.css +1 -0
- package/dist/console/assets/index-BQv5aOGB.js +700 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.d.ts +2 -0
- package/dist/console-links.js +6 -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/decisions.js +4 -0
- package/dist/email/account.d.ts +84 -0
- package/dist/email/account.js +159 -0
- package/dist/email/answer.d.ts +10 -0
- package/dist/email/answer.js +182 -0
- package/dist/email/email-faux.d.ts +95 -0
- package/dist/email/email-faux.js +849 -0
- package/dist/email/labels.d.ts +1 -0
- package/dist/email/labels.js +84 -0
- package/dist/email/mail.d.ts +186 -0
- package/dist/email/mail.js +380 -0
- package/dist/email/names.d.ts +20 -0
- package/dist/email/names.js +40 -0
- package/dist/email/routes.d.ts +9 -0
- package/dist/email/routes.js +50 -0
- package/dist/email/tools.d.ts +20 -0
- package/dist/email/tools.js +517 -0
- package/dist/environment.d.ts +17 -2
- package/dist/environment.js +36 -8
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +12 -0
- package/dist/eval-mailbox.d.ts +103 -0
- package/dist/eval-mailbox.js +236 -0
- package/dist/faux-computers.js +4 -2
- 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 -22
- package/dist/harness-opencode/facts.js +5 -4
- package/dist/harness-opencode/host/policy.d.ts +4 -5
- package/dist/harness-opencode/host/policy.js +14 -33
- 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.d.ts +0 -2
- package/dist/harness-table.js +8 -7
- package/dist/harness.d.ts +12 -0
- package/dist/harness.js +15 -3
- package/dist/host-tools.d.ts +1 -5
- package/dist/host-tools.js +7 -10
- package/dist/hosted-search.d.ts +3 -3
- package/dist/hosted-search.js +51 -32
- 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/key-words.d.ts +3 -2
- package/dist/key-words.js +4 -2
- package/dist/learn/check.d.ts +24 -4
- package/dist/learn/check.js +340 -7
- package/dist/learn/commit.d.ts +10 -0
- package/dist/learn/commit.js +18 -4
- package/dist/learn/compile.d.ts +250 -0
- package/dist/learn/compile.js +507 -0
- package/dist/learn/general.d.ts +3 -0
- package/dist/learn/general.js +4 -2
- package/dist/learn/grade.d.ts +5 -1
- package/dist/learn/grade.js +11 -7
- package/dist/learn/notes.d.ts +8 -0
- package/dist/learn/notes.js +18 -0
- package/dist/learn/promote.d.ts +308 -0
- package/dist/learn/promote.js +321 -0
- package/dist/learn/refit.d.ts +48 -0
- package/dist/learn/refit.js +264 -0
- package/dist/learn/reflect.d.ts +5 -25
- package/dist/learn/reflect.js +14 -131
- package/dist/learn/replay-calls.d.ts +63 -0
- package/dist/learn/replay-calls.js +158 -0
- package/dist/learn/skills.d.ts +19 -16
- package/dist/learn/skills.js +157 -53
- package/dist/learn/workflow-nodes.d.ts +15 -0
- package/dist/learn/workflow-nodes.js +44 -0
- package/dist/learned-nodes.d.ts +58 -2
- package/dist/learned-nodes.js +68 -5
- package/dist/learned-workflow.d.ts +58 -32
- package/dist/learned-workflow.js +215 -77
- package/dist/learning-batch.d.ts +2 -2
- package/dist/learning-batch.js +4 -4
- package/dist/learning-cost.d.ts +41 -11
- package/dist/learning-cost.js +31 -13
- package/dist/learning-routes.d.ts +12 -2
- package/dist/learning-routes.js +34 -6
- package/dist/learning-words.d.ts +8 -1
- package/dist/learning-words.js +11 -1
- package/dist/learning.d.ts +26 -1
- package/dist/learning.js +7 -4
- package/dist/link-check.d.ts +27 -0
- package/dist/link-check.js +164 -0
- package/dist/llm.js +0 -3
- package/dist/local-models.d.ts +33 -2
- package/dist/local-models.js +43 -5
- package/dist/mcp-gateway.d.ts +12 -20
- package/dist/mcp-gateway.js +16 -44
- package/dist/mcp-oauth.d.ts +5 -0
- package/dist/mcp-oauth.js +1 -1
- package/dist/mcp-pin-first.d.ts +37 -0
- package/dist/mcp-pin-first.js +113 -0
- package/dist/mcp-probe.d.ts +8 -9
- package/dist/mcp-probe.js +16 -33
- package/dist/mcp-secret-writes.js +3 -0
- package/dist/mcp-servers-store.d.ts +102 -26
- package/dist/mcp-servers-store.js +151 -25
- package/dist/mcp-sources.d.ts +39 -0
- package/dist/mcp-sources.js +74 -0
- package/dist/mcp-store.d.ts +11 -0
- package/dist/mcp-store.js +16 -2
- package/dist/mcp-wire.d.ts +22 -0
- package/dist/mcp-wire.js +36 -0
- package/dist/mcp.js +3 -3
- package/dist/meta-tool-names.d.ts +1 -0
- package/dist/meta-tool-names.js +3 -0
- package/dist/model-form.d.ts +11 -0
- package/dist/model-form.js +17 -1
- package/dist/model.d.ts +39 -6
- package/dist/model.js +171 -32
- package/dist/monid.d.ts +50 -29
- package/dist/monid.js +234 -143
- package/dist/native/binding.d.ts +13 -0
- package/dist/native/binding.js +57 -0
- package/dist/native/cli.d.ts +2 -0
- package/dist/native/cli.js +48 -0
- package/dist/native/connection.d.ts +8 -0
- package/dist/native/connection.js +94 -0
- package/dist/native/context.d.ts +6 -0
- package/dist/native/context.js +22 -0
- package/dist/native/discovery.d.ts +27 -0
- package/dist/native/discovery.js +404 -0
- package/dist/native/environment.d.ts +4 -0
- package/dist/native/environment.js +18 -0
- package/dist/native/history.d.ts +20 -0
- package/dist/native/history.js +147 -0
- package/dist/native/instructions.d.ts +21 -0
- package/dist/native/instructions.js +173 -0
- package/dist/native/remote-worker-bridge.d.ts +3 -0
- package/dist/native/remote-worker-bridge.js +278 -0
- package/dist/native/remote.d.ts +187 -0
- package/dist/native/remote.js +492 -0
- package/dist/native/runtime.d.ts +17 -0
- package/dist/native/runtime.js +316 -0
- package/dist/native/runtimes.d.ts +66 -0
- package/dist/native/runtimes.js +41 -0
- package/dist/native/schema.d.ts +46 -0
- package/dist/native/schema.js +41 -0
- package/dist/native/types.d.ts +87 -0
- package/dist/native/types.js +1 -0
- package/dist/observation/http.d.ts +4 -0
- package/dist/observation/http.js +140 -0
- package/dist/observation/observer.d.ts +301 -0
- package/dist/observation/observer.js +919 -0
- package/dist/observation/schema.d.ts +52 -0
- package/dist/observation/schema.js +154 -0
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +705 -126
- package/dist/paid-calls.d.ts +34 -32
- package/dist/paid-calls.js +52 -104
- package/dist/place-offers.d.ts +4 -8
- package/dist/place-offers.js +4 -9
- package/dist/port-models.d.ts +7 -2
- package/dist/port-models.js +5 -2
- package/dist/print.d.ts +4 -2
- package/dist/print.js +4 -3
- package/dist/propose.d.ts +8 -3
- package/dist/propose.js +28 -7
- package/dist/provider.d.ts +5 -1
- package/dist/provider.js +17 -2
- package/dist/push-handler.d.ts +3 -2
- package/dist/push-handler.js +20 -6
- package/dist/reflect-range.js +2 -1
- package/dist/refusal-words.d.ts +11 -0
- package/dist/refusal-words.js +13 -2
- package/dist/rows.d.ts +5 -3
- package/dist/rows.js +15 -4
- package/dist/run-launch.d.ts +23 -1
- package/dist/run-launch.js +17 -4
- package/dist/run-learning-served.d.ts +84 -0
- package/dist/run-manifest.d.ts +8 -0
- package/dist/run-manifest.js +33 -1
- package/dist/run-reads-over-store.d.ts +3 -3
- package/dist/run-reads-over-store.js +13 -10
- package/dist/run-secrets.js +2 -3
- package/dist/run-timeline-served.d.ts +3 -2
- package/dist/run-timeline-served.js +12 -10
- package/dist/run-view.d.ts +59 -41
- package/dist/run-view.js +128 -64
- package/dist/runnable.d.ts +13 -8
- package/dist/runnable.js +77 -60
- package/dist/runner/adapter-loop.d.ts +60 -15
- package/dist/runner/adapter-loop.js +1406 -464
- package/dist/runner/adapter-run-spec.d.ts +45 -14
- package/dist/runner/adapter-run-spec.js +200 -116
- package/dist/runner/adapters/pi.d.ts +40 -7
- package/dist/runner/adapters/pi.js +149 -38
- 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 +150 -66
- package/dist/runner/computer-channel.d.ts +5 -2
- package/dist/runner/computer-channel.js +5 -3
- package/dist/runner/computer-journal.d.ts +6 -0
- package/dist/runner/computer-journal.js +8 -10
- package/dist/runner/conformance/harness.d.ts +3 -0
- package/dist/runner/conformance/harness.js +149 -5
- package/dist/runner/conformance/subject.d.ts +11 -4
- package/dist/runner/end-of-run.d.ts +8 -5
- package/dist/runner/end-of-run.js +11 -6
- package/dist/runner/feedback-round.d.ts +79 -0
- package/dist/runner/feedback-round.js +127 -0
- package/dist/runner/harness-loops.js +17 -0
- package/dist/runner/journal-kinds.d.ts +55 -13
- package/dist/runner/journal-kinds.js +19 -0
- package/dist/runner/learning-dispatch.d.ts +3 -2
- package/dist/runner/learning-dispatch.js +6 -6
- package/dist/runner/learning-end.d.ts +58 -19
- package/dist/runner/learning-end.js +316 -115
- package/dist/runner/learning-factory.d.ts +3 -0
- package/dist/runner/learning-factory.js +8 -2
- package/dist/runner/learning-loop.d.ts +21 -1
- package/dist/runner/learning-loop.js +24 -6
- package/dist/runner/learning-seed.d.ts +4 -0
- package/dist/runner/learning-seed.js +14 -0
- package/dist/runner/loop-contract.d.ts +19 -3
- package/dist/runner/loop.d.ts +34 -1
- package/dist/runner/loop.js +55 -1
- package/dist/runner/main.d.ts +6 -1
- package/dist/runner/main.js +26 -4
- package/dist/runner/mcp-claim.d.ts +5 -1
- package/dist/runner/mcp-claim.js +66 -50
- package/dist/runner/mcp-run.d.ts +20 -53
- package/dist/runner/mcp-run.js +22 -88
- package/dist/runner/meta-tools.d.ts +44 -0
- package/dist/runner/meta-tools.js +344 -0
- package/dist/runner/metrics.d.ts +80 -21
- package/dist/runner/metrics.js +128 -42
- package/dist/runner/model-route.d.ts +12 -3
- package/dist/runner/model-route.js +149 -19
- package/dist/runner/monid-run.d.ts +18 -0
- package/dist/runner/monid-run.js +170 -0
- package/dist/runner/native-loop.d.ts +11 -0
- package/dist/runner/native-loop.js +438 -0
- package/dist/runner/promotion.d.ts +146 -0
- package/dist/runner/promotion.js +803 -0
- package/dist/runner/redact.js +3 -0
- package/dist/runner/reflect-hook.d.ts +36 -21
- package/dist/runner/reflect-hook.js +147 -49
- package/dist/runner/runner.d.ts +7 -1
- package/dist/runner/runner.js +34 -8
- package/dist/runner/sdk-loop.d.ts +7 -0
- package/dist/runner/sdk-loop.js +470 -0
- package/dist/runner/sessions.d.ts +8 -3
- package/dist/runner/sessions.js +5 -1
- package/dist/runner/spec-holds.d.ts +16 -2
- package/dist/runner/spec-holds.js +42 -14
- package/dist/runner/step-guard.d.ts +62 -0
- package/dist/runner/step-guard.js +93 -0
- package/dist/runner/store-secrets.js +17 -2
- package/dist/runner/subagent.d.ts +0 -3
- package/dist/runner/subagent.js +7 -12
- package/dist/runner/workflow-first.d.ts +226 -0
- package/dist/runner/workflow-first.js +293 -0
- package/dist/runner/workflow-loop.d.ts +1 -1
- package/dist/runner/workflow-loop.js +32 -19
- package/dist/runner/workflow-spec.d.ts +5 -1
- package/dist/runner/workflow-spec.js +63 -48
- package/dist/runs-over-store.d.ts +41 -4
- package/dist/runs-over-store.js +64 -22
- 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 +108 -20
- package/dist/server.js +874 -157
- package/dist/settings-wake.d.ts +8 -6
- package/dist/settings-wake.js +13 -7
- 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 +22 -7
- package/dist/store/agent-body.js +23 -14
- package/dist/store/command-wake.d.ts +3 -2
- package/dist/store/command-wake.js +4 -4
- package/dist/store/index.d.ts +1 -1
- package/dist/store/native-command-owner.d.ts +10 -0
- package/dist/store/native-command-owner.js +47 -0
- package/dist/store/postgres/connection.js +3 -1
- package/dist/store/postgres/migrations/0010_imported_templates.sql +18 -0
- package/dist/store/postgres/migrations/0011_revision_mcp_pin.sql +13 -0
- package/dist/store/postgres/migrations/0012_revision_monid_pin.sql +4 -0
- package/dist/store/postgres/migrations/0013_native_command_owner.sql +6 -0
- package/dist/store/postgres/migrations/0014_run_cost_upper_bound.sql +6 -0
- package/dist/store/postgres/store.js +3 -0
- package/dist/store/schema.d.ts +16 -0
- package/dist/store/schema.js +47 -8
- package/dist/store/sqlite/connection.d.ts +2 -2
- package/dist/store/sqlite/connection.js +2 -2
- package/dist/store/sqlite/run-patch.d.ts +8 -1
- package/dist/store/sqlite/run-patch.js +33 -10
- package/dist/store/sqlite/store.js +184 -28
- package/dist/store/store.d.ts +98 -21
- 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 +30 -1
- package/dist/store-reads/journal.js +141 -83
- package/dist/store-reads/learning.d.ts +179 -13
- package/dist/store-reads/learning.js +230 -68
- package/dist/store-reads/loop.d.ts +61 -0
- package/dist/store-reads/loop.js +111 -0
- package/dist/store-reads/runs.d.ts +5 -0
- package/dist/store-reads/runs.js +3 -2
- package/dist/store-view.d.ts +3 -2
- package/dist/store-view.js +109 -37
- package/dist/template-eval.d.ts +87 -16
- package/dist/template-eval.js +605 -120
- package/dist/template-github-permissions.d.ts +2 -2
- package/dist/template-github-permissions.js +2 -1
- package/dist/template-import.d.ts +31 -0
- package/dist/template-import.js +121 -0
- package/dist/template-source.d.ts +51 -0
- package/dist/template-source.js +371 -0
- package/dist/templates.d.ts +274 -52
- package/dist/templates.js +670 -201
- package/dist/text-urls.d.ts +38 -0
- package/dist/text-urls.js +170 -0
- package/dist/timeline/computer.d.ts +48 -0
- package/dist/timeline/computer.js +169 -0
- package/dist/timeline/index.d.ts +7 -5
- package/dist/timeline/index.js +7 -5
- package/dist/timeline/labels.d.ts +11 -4
- package/dist/timeline/labels.js +122 -8
- package/dist/timeline/origin.d.ts +20 -6
- package/dist/timeline/origin.js +13 -6
- package/dist/timeline/render-text.js +1 -1
- package/dist/timeline/timeline.d.ts +76 -20
- package/dist/timeline/timeline.js +184 -39
- 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 +103 -76
- package/dist/tool-gate.js +200 -127
- package/dist/tool-keys.d.ts +4 -1
- package/dist/tool-keys.js +8 -3
- package/dist/tool-list.d.ts +29 -0
- package/dist/tool-list.js +50 -0
- package/dist/tool-names.d.ts +76 -0
- package/dist/tool-names.js +101 -0
- package/dist/tool-plane.d.ts +34 -28
- package/dist/tool-plane.js +72 -37
- package/dist/tools.d.ts +59 -15
- package/dist/tools.js +46 -42
- package/dist/trigger-adapter.d.ts +85 -5
- package/dist/trigger-adapter.js +6 -0
- package/dist/trigger-adapters.js +3 -1
- package/dist/trigger-composio.d.ts +10 -0
- package/dist/trigger-composio.js +122 -0
- package/dist/trigger-email.d.ts +4 -0
- package/dist/trigger-email.js +228 -0
- package/dist/trigger-signatures.d.ts +6 -10
- package/dist/trigger-signatures.js +16 -16
- package/dist/trigger-targets.d.ts +1 -1
- package/dist/trigger-targets.js +4 -3
- package/dist/trigger-webhook.js +6 -4
- package/dist/triggers.d.ts +94 -9
- package/dist/triggers.js +263 -62
- package/dist/uploads.d.ts +9 -0
- package/dist/uploads.js +27 -0
- package/dist/validate.d.ts +15 -7
- package/dist/validate.js +91 -50
- 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 +6 -8
- package/dist/web-fetch.js +10 -53
- package/dist/web-search.d.ts +51 -2
- package/dist/web-search.js +116 -18
- package/dist/webhooks.d.ts +2 -1
- package/dist/webhooks.js +5 -1
- package/dist/workflow/node-runner.js +10 -2
- package/dist/workspace/archil-claim.d.ts +3 -0
- package/dist/workspace/archil-claim.js +9 -2
- package/dist/workspace/computer-deps.d.ts +25 -9
- package/dist/workspace/computer-deps.js +82 -38
- package/dist/workspace/computer-for-claim.d.ts +60 -4
- package/dist/workspace/computer-for-claim.js +203 -45
- package/dist/workspace/computer-hold.js +7 -1
- 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 +2 -3
- package/dist/workspace/index.js +1 -2
- package/dist/workspace/kind-skills/desktop/SKILL.md +31 -11
- package/dist/workspace/kind-skills/phone/SKILL.md +29 -14
- package/dist/workspace/local.js +2 -2
- package/dist/workspace/resolve-computer.d.ts +33 -1
- package/dist/workspace/resolve-computer.js +37 -16
- 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 +40 -23
- package/dist/workspace/run-tools.js +46 -19
- package/dist/wrap-up.d.ts +35 -0
- package/dist/wrap-up.js +48 -0
- package/launcher/agentrun-serve +4 -2
- package/package.json +13 -9
- package/skills/agentrun-serve/SKILL.md +248 -10
- package/templates/README.md +220 -60
- package/templates/TOOLS.md +168 -0
- package/templates/code-review/agent.md +46 -16
- package/templates/code-review/evals/clean-refactor/case.yaml +2 -1
- package/templates/code-review/evals/clean-refactor/graders/head-sha.md +6 -0
- package/templates/code-review/evals/injection/case.yaml +2 -1
- package/templates/code-review/evals/injection/graders/head-sha.md +6 -0
- package/templates/code-review/evals/mentions-defused/case.yaml +3 -3
- package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +0 -23
- package/templates/code-review/evals/mentions-defused/graders/head-sha.md +6 -0
- package/templates/code-review/evals/never-flag/case.yaml +2 -1
- package/templates/code-review/evals/never-flag/graders/head-sha.md +6 -0
- package/templates/code-review/evals/null-name/case.yaml +2 -1
- package/templates/code-review/evals/null-name/graders/head-sha.md +6 -0
- package/templates/code-review/evals/off-by-one/case.yaml +2 -1
- package/templates/code-review/evals/off-by-one/graders/head-sha.md +6 -0
- package/templates/code-review/evals/off-by-one/graders/no-repo-reads.md +2 -1
- package/templates/code-review/evals/other-repo/case.yaml +16 -0
- package/templates/code-review/evals/other-repo/fixtures/github-mcp.json +130 -0
- package/templates/code-review/evals/other-repo/graders/head-sha.md +6 -0
- package/templates/code-review/evals/other-repo/graders/no-canary.md +6 -0
- package/templates/code-review/evals/other-repo/graders/no-other-repo.md +8 -0
- package/templates/code-review/evals/other-repo/graders/refused-not-quoted.md +18 -0
- package/templates/code-review/evals/other-repo/prompt.md +10 -0
- 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/evals/re-review/case.yaml +2 -1
- package/templates/code-review/evals/re-review/graders/head-sha.md +6 -0
- package/templates/code-review/evals/re-review-open/case.yaml +2 -1
- package/templates/code-review/evals/re-review-open/graders/head-sha.md +6 -0
- package/templates/code-review/evals/repo-rule/case.yaml +2 -1
- package/templates/code-review/evals/repo-rule/graders/head-sha.md +6 -0
- package/templates/code-review/evals/two-defects/case.yaml +2 -1
- package/templates/code-review/evals/two-defects/graders/head-sha.md +6 -0
- package/templates/code-review/rules/CODEREVIEW.md +1 -1
- package/templates/code-review/skills/review/DEEP.md +5 -4
- package/templates/code-review/skills/review/REVIEWER.md +5 -4
- package/templates/code-review/skills/review/SKILL.md +11 -6
- package/templates/code-review/{findings.schema.json → skills/review/findings.schema.json} +1 -0
- package/templates/inbound-lead/agent.md +26 -16
- package/templates/inbound-lead/evals/injection-in-form/graders/no-exfil.md +16 -0
- package/templates/inbound-lead/evals/injection-in-form/prompt.md +2 -2
- package/templates/inbound-lead/evals/write-back/case.yaml +2 -6
- package/templates/inbound-lead/evals/write-back/fixtures/fake-hubspot.json +34 -225
- package/templates/inbound-lead/evals/write-back/graders/note-and-score.md +10 -14
- package/templates/inbound-lead/evals/write-back/graders/writes-once.md +7 -0
- package/templates/inbound-lead/evals/write-back/prompt.md +1 -1
- package/templates/inbound-lead/evals/write-back-wrong-record/case.yaml +3 -7
- package/templates/inbound-lead/evals/write-back-wrong-record/fixtures/fake-hubspot.json +34 -225
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/form-id.md +17 -0
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/no-write.md +3 -3
- package/templates/inbound-lead/evals/write-back-wrong-record/prompt.md +2 -2
- package/templates/inbound-lead/skills/lead/SKILL.md +23 -27
- package/templates/kyb/agent.md +15 -5
- package/templates/kyb/evals/adverse-media/prompt.md +2 -7
- package/templates/kyb/evals/canada-active-filed/prompt.md +2 -6
- package/templates/kyb/evals/clean-public-company/prompt.md +2 -6
- package/templates/kyb/evals/injection-in-page/prompt.md +3 -7
- package/templates/kyb/evals/name-collision/prompt.md +2 -6
- package/templates/kyb/evals/no-invention/prompt.md +2 -6
- package/templates/kyb/evals/opensanctions-on/prompt.md +2 -6
- package/templates/kyb/evals/sanctioned-entity/prompt.md +2 -6
- package/templates/kyb/kyb.schema.json +8 -7
- package/templates/kyb/skills/kyb/SKILL.md +9 -6
- package/templates/market-monitor/agent.md +19 -12
- package/templates/market-monitor/evals/post-happens/case.yaml +4 -4
- package/templates/market-monitor/evals/post-happens/fixtures/slack-mcp.json +13 -9
- package/templates/market-monitor/evals/post-happens/graders/post-is-digest.md +1 -1
- package/templates/market-monitor/evals/post-happens/graders/posted-once.md +3 -3
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/templates/release-notes/agent.md +32 -10
- package/templates/release-notes/evals/draft-not-publish/case.yaml +5 -4
- package/templates/release-notes/evals/draft-not-publish/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/every-pr-accounted/case.yaml +2 -1
- package/templates/release-notes/evals/issue-write-refused/case.yaml +5 -4
- package/templates/release-notes/evals/issue-write-refused/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/late-arrival/case.yaml +6 -5
- package/templates/release-notes/evals/late-arrival/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/learned-style/case.yaml +2 -1
- package/templates/release-notes/evals/no-pr-numbers-on-top/case.yaml +2 -1
- package/templates/release-notes/evals/other-repo/case.yaml +24 -0
- package/templates/release-notes/evals/other-repo/fixtures/github-mcp.json +907 -0
- package/templates/release-notes/evals/other-repo/graders/no-canary.md +7 -0
- package/templates/release-notes/evals/other-repo/graders/no-other-repo.md +8 -0
- package/templates/release-notes/evals/other-repo/graders/refused-not-quoted.md +18 -0
- package/templates/release-notes/evals/other-repo/prompt.md +32 -0
- package/templates/release-notes/evals/upgrade-line/case.yaml +2 -1
- package/templates/release-notes/evals/user-facing-only/case.yaml +2 -1
- package/templates/release-notes/notes.schema.json +1 -1
- package/templates/release-notes/skills/release-notes/SKILL.md +26 -17
- package/dist/console/assets/index-Cn6JX1uH.js +0 -563
- package/dist/console/assets/index-DRN62V5v.css +0 -1
- package/dist/host-calls.d.ts +0 -20
- package/dist/host-calls.js +0 -12
- package/dist/runner/adapters/pi-computer.d.ts +0 -7
- package/dist/runner/adapters/pi-computer.js +0 -43
- package/dist/tool-blurbs.d.ts +0 -4
- package/dist/tool-blurbs.js +0 -28
- package/dist/tool-budget.d.ts +0 -38
- package/dist/tool-budget.js +0 -62
- package/dist/workspace/computer-script.d.ts +0 -37
- package/dist/workspace/computer-script.js +0 -138
- package/templates/code-review/evals/clean-refactor/graders/schema.md +0 -5
- package/templates/code-review/evals/injection/graders/schema.md +0 -5
- package/templates/code-review/evals/mentions-defused/graders/schema.md +0 -5
- package/templates/code-review/evals/never-flag/graders/schema.md +0 -5
- package/templates/code-review/evals/null-name/graders/schema.md +0 -5
- package/templates/code-review/evals/off-by-one/graders/schema.md +0 -5
- package/templates/code-review/evals/re-review/graders/schema.md +0 -5
- package/templates/code-review/evals/re-review-open/graders/schema.md +0 -5
- package/templates/code-review/evals/repo-rule/graders/schema.md +0 -5
- package/templates/code-review/evals/two-defects/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/clear-disqualifier/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/injection-in-form/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/learned-correction/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/strong-fit/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/thin-data/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/write-back/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/write-back/graders/writes-twice.md +0 -7
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/proves-record.md +0 -18
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/schema.md +0 -5
- package/templates/kyb/evals/adverse-media/graders/schema.md +0 -5
- package/templates/kyb/evals/canada-active-filed/graders/schema.md +0 -5
- package/templates/kyb/evals/clean-public-company/graders/schema.md +0 -5
- package/templates/kyb/evals/injection-in-page/graders/schema.md +0 -5
- package/templates/kyb/evals/name-collision/graders/schema.md +0 -5
- package/templates/kyb/evals/no-invention/graders/schema.md +0 -5
- package/templates/kyb/evals/opensanctions-on/graders/schema.md +0 -5
- package/templates/kyb/evals/sanctioned-entity/graders/schema.md +0 -5
- package/templates/market-monitor/evals/blocked-source/graders/schema.md +0 -5
- package/templates/market-monitor/evals/every-competitor-named/graders/schema.md +0 -5
- package/templates/market-monitor/evals/learned-preference/graders/schema.md +0 -5
- package/templates/market-monitor/evals/no-repeats/graders/schema.md +0 -5
- package/templates/market-monitor/evals/nothing-changed/graders/schema.md +0 -5
- package/templates/market-monitor/evals/post-happens/graders/schema.md +0 -5
- package/templates/market-monitor/evals/pricing-change/graders/schema.md +0 -5
- package/templates/market-monitor/evals/pricing-moved/graders/schema.md +0 -5
- package/templates/release-notes/evals/draft-not-publish/graders/schema.md +0 -5
- package/templates/release-notes/evals/every-pr-accounted/graders/schema.md +0 -5
- package/templates/release-notes/evals/issue-write-refused/graders/schema.md +0 -5
- package/templates/release-notes/evals/late-arrival/graders/schema.md +0 -5
- package/templates/release-notes/evals/learned-style/graders/schema.md +0 -5
- package/templates/release-notes/evals/no-pr-numbers-on-top/graders/schema.md +0 -5
- package/templates/release-notes/evals/upgrade-line/graders/schema.md +0 -5
- package/templates/release-notes/evals/user-facing-only/graders/schema.md +0 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The five email tools on the timeline (timeline/labels.ts registerLabel), in a person's words: what was searched,
|
|
2
|
+
// read, drafted, sent or filed. A line reads the call's arguments while it runs and its answer once that is in, as
|
|
3
|
+
// browser_read's does, so the call's row and its result's row say the same one line ("Read Plan", then "Read Plan: ok").
|
|
4
|
+
// Addresses: the first, then " and N more"; cc is left out. Subjects are cut at 60 characters and a search's words at
|
|
5
|
+
// 40, their URLs scrubbed, as web_search's query is. No body text: no message's text (read, drafted or sent) and no
|
|
6
|
+
// snippet is in any line; a search's `text` is the agent's own words, and shows.
|
|
7
|
+
import { cut, registerLabel, scrubUrls } from "../timeline/index.js";
|
|
8
|
+
import { isObject } from "../validate.js";
|
|
9
|
+
import { EMAIL_DRAFT, EMAIL_FILE, EMAIL_READ, EMAIL_SEARCH, EMAIL_SEND } from "./names.js";
|
|
10
|
+
const SUBJECT_MAX = 60;
|
|
11
|
+
const WORDS_MAX = 40;
|
|
12
|
+
const words = (value) => (typeof value === "string" && value.trim() ? value.trim() : undefined);
|
|
13
|
+
/** A subject as a line shows it: one line, its URLs scrubbed, cut at SUBJECT_MAX. */
|
|
14
|
+
const subjectOf = (value) => {
|
|
15
|
+
const said = words(value)?.replace(/\s+/g, " ");
|
|
16
|
+
return said === undefined ? undefined : cut(scrubUrls(said), SUBJECT_MAX);
|
|
17
|
+
};
|
|
18
|
+
/** `to` as a line names it: its first address, then " and N more". */
|
|
19
|
+
const addressed = (value) => {
|
|
20
|
+
const all = (Array.isArray(value) ? value : [value]).flatMap((one) => words(one) ?? []);
|
|
21
|
+
if (!all.length)
|
|
22
|
+
return undefined;
|
|
23
|
+
return all.length > 1 ? `${all[0]} and ${all.length - 1} more` : all[0];
|
|
24
|
+
};
|
|
25
|
+
/** A finished call's answer, parsed: none for an error or an answer that is not a JSON object. */
|
|
26
|
+
const answerOf = (result) => {
|
|
27
|
+
if (!result || result.isError)
|
|
28
|
+
return undefined;
|
|
29
|
+
try {
|
|
30
|
+
const parsed = JSON.parse(result.text);
|
|
31
|
+
return isObject(parsed) ? parsed : undefined;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
/** email_search: its query as a search box reads it (its words quoted, then from:, to:, subject:"", since:, unread or
|
|
38
|
+
* read), " in <mailbox>" past INBOX. */
|
|
39
|
+
const searchLabel = (args) => {
|
|
40
|
+
const said = words(args.text)?.replace(/\s+/g, " ");
|
|
41
|
+
const subject = subjectOf(args.subject);
|
|
42
|
+
const terms = [
|
|
43
|
+
...(said ? [`"${cut(scrubUrls(said), WORDS_MAX)}"`] : []),
|
|
44
|
+
...(words(args.from) ? [`from:${words(args.from)}`] : []),
|
|
45
|
+
...(words(args.to) ? [`to:${words(args.to)}`] : []),
|
|
46
|
+
...(subject ? [`subject:"${subject}"`] : []),
|
|
47
|
+
...(words(args.since) ? [`since:${words(args.since)}`] : []),
|
|
48
|
+
...(args.unread === true ? ["unread"] : args.unread === false ? ["read"] : []),
|
|
49
|
+
];
|
|
50
|
+
const box = words(args.mailbox);
|
|
51
|
+
const where = box && box.toUpperCase() !== "INBOX" ? ` in ${box}` : "";
|
|
52
|
+
return terms.length ? `Searched mail for ${terms.join(" ")}${where}` : `Searched mail${where}`;
|
|
53
|
+
};
|
|
54
|
+
/** email_read: "Read an email" (or "an email thread") while it runs; then the subject of the message it named. */
|
|
55
|
+
const readLabel = (args, result) => {
|
|
56
|
+
const thread = args.thread === true;
|
|
57
|
+
const messages = answerOf(result)?.messages;
|
|
58
|
+
const listed = Array.isArray(messages) ? messages.filter(isObject) : [];
|
|
59
|
+
const named = listed.find((message) => message.message_id === args.message_id) ?? listed[0];
|
|
60
|
+
const subject = subjectOf(named?.subject);
|
|
61
|
+
if (subject === undefined)
|
|
62
|
+
return thread ? "Read an email thread" : "Read an email";
|
|
63
|
+
return thread ? `Read the thread ${subject}` : `Read ${subject}`;
|
|
64
|
+
};
|
|
65
|
+
/** email_draft and email_send: a reply (in_reply_to) or an email, to whom, and its subject; a send some of whose
|
|
66
|
+
* recipients the mail server refused says how many, from its answer's `rejected`. */
|
|
67
|
+
const writeLabel = (verb) => (args, result) => {
|
|
68
|
+
const to = addressed(args.to);
|
|
69
|
+
const subject = subjectOf(args.subject);
|
|
70
|
+
const rejected = answerOf(result)?.rejected;
|
|
71
|
+
const refused = Array.isArray(rejected) && rejected.length ? ` (refused: ${rejected.length})` : "";
|
|
72
|
+
return `${verb} ${words(args.in_reply_to) ? "a reply" : "an email"}${to ? ` to ${to}` : ""}${subject ? `: ${subject}` : ""}${refused}`;
|
|
73
|
+
};
|
|
74
|
+
/** email_file: "Filed an email under <to>" while it runs; then its answer's subject, and ", out of the inbox" when it left it. */
|
|
75
|
+
const fileLabel = (args, result) => {
|
|
76
|
+
const answer = answerOf(result);
|
|
77
|
+
const to = words(args.to) ?? "a folder";
|
|
78
|
+
return `Filed ${subjectOf(answer?.subject) ?? "an email"} under ${to}${answer?.archived === true ? ", out of the inbox" : ""}`;
|
|
79
|
+
};
|
|
80
|
+
registerLabel(EMAIL_SEARCH, searchLabel, { many: (n) => `Searched mail ${n} times` }, { name: "Search mail" });
|
|
81
|
+
registerLabel(EMAIL_READ, readLabel, { many: (n) => `Read ${n} emails` }, { name: "Read mail" });
|
|
82
|
+
registerLabel(EMAIL_DRAFT, writeLabel("Drafted"), { many: (n) => `Drafted ${n} emails` }, { name: "Draft mail" });
|
|
83
|
+
registerLabel(EMAIL_SEND, writeLabel("Sent"), { many: (n) => `Sent ${n} emails` }, { name: "Send mail" });
|
|
84
|
+
registerLabel(EMAIL_FILE, fileLabel, { many: (n) => `Filed ${n} emails` }, { name: "File mail" });
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { ImapFlow, type FetchMessageObject, type ListResponse, type SearchObject } from "imapflow";
|
|
2
|
+
import { type Endpoint, type OpenedAccount } from "./account.js";
|
|
3
|
+
/** How long a connection may take to open and greet. */
|
|
4
|
+
export declare const CONNECT_TIMEOUT_MS = 15000;
|
|
5
|
+
/** How long a connection may sit with no answer. */
|
|
6
|
+
export declare const SOCKET_TIMEOUT_MS = 60000;
|
|
7
|
+
/** How long a run's email tool waits on an answer: a search the server has not indexed takes minutes on a large mailbox. */
|
|
8
|
+
export declare const TOOL_SOCKET_TIMEOUT_MS: number;
|
|
9
|
+
/** Tests only: every IMAP session's socket timeout made `ms` (undefined: each its own again), so a server that never
|
|
10
|
+
* answers is given up on in a test's time. */
|
|
11
|
+
export declare function setSocketTimeoutForTests(ms: number | undefined): void;
|
|
12
|
+
/** The most of a message a search reads: its headers and the start of its text, for the snippet. */
|
|
13
|
+
export declare const SEARCH_BYTES: number;
|
|
14
|
+
/** The most of a message a read or a poll takes. */
|
|
15
|
+
export declare const READ_BYTES: number;
|
|
16
|
+
/** The header email_send marks everything it sends with (its value the run's id): a poll leaves such a message. */
|
|
17
|
+
export declare const SENT_HEADER = "X-AgentRun-Sent";
|
|
18
|
+
/** Why a mail server did not do what was asked: `unreachable`, it could not be reached; `refused`, it refused the
|
|
19
|
+
* sign-in; `missing`, a mailbox or message is not there; `failed`, anything else. Its message is fit to show. */
|
|
20
|
+
export declare class MailError extends Error {
|
|
21
|
+
readonly kind: "unreachable" | "refused" | "missing" | "failed";
|
|
22
|
+
constructor(kind: "unreachable" | "refused" | "missing" | "failed", message: string);
|
|
23
|
+
}
|
|
24
|
+
/** `error`, from the server at `endpoint` over `protocol`, as a MailError whose words hold nothing of `password`. */
|
|
25
|
+
export declare function mailError(error: unknown, endpoint: Endpoint, protocol: "IMAP" | "SMTP", password: string): MailError;
|
|
26
|
+
/** A message as the tools and the trigger read it: where it is, its flags, and its fields. */
|
|
27
|
+
export type MailMessage = {
|
|
28
|
+
uid: number;
|
|
29
|
+
mailbox: string;
|
|
30
|
+
flags: string[];
|
|
31
|
+
from: string;
|
|
32
|
+
from_name: string | null;
|
|
33
|
+
to: string[];
|
|
34
|
+
cc: string[];
|
|
35
|
+
subject: string;
|
|
36
|
+
date: string | null;
|
|
37
|
+
message_id: string | null;
|
|
38
|
+
in_reply_to: string | null;
|
|
39
|
+
references: string[];
|
|
40
|
+
text: string;
|
|
41
|
+
attachments: {
|
|
42
|
+
name: string;
|
|
43
|
+
size: number;
|
|
44
|
+
type: string;
|
|
45
|
+
}[];
|
|
46
|
+
/** The X-AgentRun-Sent header's value, when it has one: this server sent it. */
|
|
47
|
+
sent_here?: string;
|
|
48
|
+
};
|
|
49
|
+
/** A fetched message, parsed: `source` its bytes (the first `READ_BYTES` or `SEARCH_BYTES` of it). */
|
|
50
|
+
export declare function messageOf(row: Pick<FetchMessageObject, "uid" | "flags" | "source" | "internalDate">, mailbox: string): Promise<MailMessage>;
|
|
51
|
+
/** A message as a thread read first sees it: where it is, who it is between, its ids, its date and its size, no body. */
|
|
52
|
+
export type MailHead = Pick<MailMessage, "uid" | "mailbox" | "from" | "to" | "cc" | "date" | "message_id" | "in_reply_to" | "references"> & {
|
|
53
|
+
size: number;
|
|
54
|
+
};
|
|
55
|
+
/** Whether `address` (bare, any case) is one of `m`'s From, To or Cc. */
|
|
56
|
+
export declare const involves: (m: Pick<MailMessage, "from" | "to" | "cc">, address: string) => boolean;
|
|
57
|
+
/** The payload's fields (a trigger's firing, a read's message). */
|
|
58
|
+
export declare const payloadOf: (m: MailMessage) => {
|
|
59
|
+
from: string;
|
|
60
|
+
from_name: string | null;
|
|
61
|
+
to: string[];
|
|
62
|
+
cc: string[];
|
|
63
|
+
subject: string;
|
|
64
|
+
date: string | null;
|
|
65
|
+
message_id: string | null;
|
|
66
|
+
in_reply_to: string | null;
|
|
67
|
+
text: string;
|
|
68
|
+
attachments: {
|
|
69
|
+
name: string;
|
|
70
|
+
size: number;
|
|
71
|
+
type: string;
|
|
72
|
+
}[];
|
|
73
|
+
};
|
|
74
|
+
/** A read's message: the payload and its references. */
|
|
75
|
+
export declare const readOf: (m: MailMessage) => {
|
|
76
|
+
references: string[];
|
|
77
|
+
from: string;
|
|
78
|
+
from_name: string | null;
|
|
79
|
+
to: string[];
|
|
80
|
+
cc: string[];
|
|
81
|
+
subject: string;
|
|
82
|
+
date: string | null;
|
|
83
|
+
message_id: string | null;
|
|
84
|
+
in_reply_to: string | null;
|
|
85
|
+
text: string;
|
|
86
|
+
attachments: {
|
|
87
|
+
name: string;
|
|
88
|
+
size: number;
|
|
89
|
+
type: string;
|
|
90
|
+
}[];
|
|
91
|
+
};
|
|
92
|
+
/** A search's message: who, what, when, the start of its text, whether it is unread. */
|
|
93
|
+
export declare const foundOf: (m: MailMessage) => {
|
|
94
|
+
message_id: string | null;
|
|
95
|
+
from: string;
|
|
96
|
+
to: string[];
|
|
97
|
+
subject: string;
|
|
98
|
+
date: string | null;
|
|
99
|
+
snippet: string;
|
|
100
|
+
unread: boolean;
|
|
101
|
+
attachments: {
|
|
102
|
+
name: string;
|
|
103
|
+
size: number;
|
|
104
|
+
type: string;
|
|
105
|
+
}[];
|
|
106
|
+
};
|
|
107
|
+
/** The IMAP search clause that takes the messages of a thread whose ids are `ids`: a message is in it when its Message-ID,
|
|
108
|
+
* one of its References or (with `inReplyTo`) its In-Reply-To is one of them. */
|
|
109
|
+
export declare const threadClause: (ids: readonly string[], inReplyTo?: boolean) => SearchObject;
|
|
110
|
+
/** Whether `m` is in the thread of `ids` (the clause's own test, checked again on what the server answered). */
|
|
111
|
+
export declare const inThread: (m: Pick<MailMessage, "message_id" | "references" | "in_reply_to">, ids: ReadonlySet<string>) => boolean;
|
|
112
|
+
/** One signed-in IMAP session. */
|
|
113
|
+
export declare class ImapSession {
|
|
114
|
+
private readonly client;
|
|
115
|
+
private listed?;
|
|
116
|
+
constructor(client: ImapFlow);
|
|
117
|
+
/** The account's mailboxes, listed once a session. */
|
|
118
|
+
mailboxes(): Promise<ListResponse[]>;
|
|
119
|
+
/** The folder a mailbox name stands for: "INBOX"; "Drafts", "Sent", "\\Drafts" or "\\Sent" the special-use folder (its
|
|
120
|
+
* folder of that name when the server flags none); any other a folder's own name. MailError `missing` when there is
|
|
121
|
+
* none. */
|
|
122
|
+
resolve(name: string): Promise<string>;
|
|
123
|
+
/** Every mailbox a search across the account reads: INBOX first, then each one that holds messages, junk and trash
|
|
124
|
+
* apart. */
|
|
125
|
+
searchable(): Promise<string[]>;
|
|
126
|
+
/** `path` opened read-only (EXAMINE): its UIDVALIDITY, its next UID, and how many messages it holds. */
|
|
127
|
+
open(path: string): Promise<{
|
|
128
|
+
path: string;
|
|
129
|
+
uidValidity: string;
|
|
130
|
+
uidNext: number;
|
|
131
|
+
exists: number;
|
|
132
|
+
}>;
|
|
133
|
+
/** The UIDs of the open mailbox's messages `query` takes, lowest first. */
|
|
134
|
+
uids(query: SearchObject): Promise<number[]>;
|
|
135
|
+
/** The open mailbox's messages of `uids`, each read with BODY.PEEK (at most `bytes` of it) and parsed, in UID order. */
|
|
136
|
+
fetch(path: string, uids: readonly number[], bytes: number): Promise<MailMessage[]>;
|
|
137
|
+
/** The open mailbox's messages of `uids`, read cheaply: who they are between, their ids, their date and their size from a few headers,
|
|
138
|
+
* INTERNALDATE and RFC822.SIZE, never a body. What a thread read picks from before it fetches any message whole. */
|
|
139
|
+
heads(path: string, uids: readonly number[]): Promise<MailHead[]>;
|
|
140
|
+
/** The clause that takes the thread of `ids` on this server. Gmail (X-GM-EXT-1) indexes Message-ID and References but
|
|
141
|
+
* not In-Reply-To, a search by which takes minutes on a large mailbox: there a reply is found by its References, which
|
|
142
|
+
* a mail client writes with its In-Reply-To. */
|
|
143
|
+
threadQuery(ids: readonly string[]): SearchObject;
|
|
144
|
+
/** Whether the server says it does `capability` (X-GM-EXT-1: Gmail's labels). */
|
|
145
|
+
can(capability: string): boolean;
|
|
146
|
+
/** `path` opened read-write (SELECT), to change what it holds. */
|
|
147
|
+
select(path: string): Promise<void>;
|
|
148
|
+
/** The folder (or Gmail label) `path`, created when the account has none of that name; whether it was. */
|
|
149
|
+
ensure(path: string): Promise<boolean>;
|
|
150
|
+
/** Gmail labels `labels` added to (or, `remove`, taken off) message `uid` of the open mailbox. */
|
|
151
|
+
label(uid: number, labels: string[], remove?: boolean): Promise<void>;
|
|
152
|
+
/** Message `uid` of the open mailbox moved to `path` (UID MOVE, else UID COPY and UID EXPUNGE). */
|
|
153
|
+
move(uid: number, path: string): Promise<void>;
|
|
154
|
+
/** `raw` appended to `path` with `flags`. */
|
|
155
|
+
append(path: string, raw: Buffer, flags: string[]): Promise<void>;
|
|
156
|
+
}
|
|
157
|
+
/** `work` in one IMAP session of `account`, signed in, then signed out, waiting on each answer `socketTimeoutMs` at most
|
|
158
|
+
* (SOCKET_TIMEOUT_MS by default). A failure is a MailError; a server that answers nothing for that long, one that says
|
|
159
|
+
* the server took too long. */
|
|
160
|
+
export declare function withImap<T>(account: OpenedAccount, work: (session: ImapSession) => Promise<T>, options?: {
|
|
161
|
+
socketTimeoutMs?: number;
|
|
162
|
+
}): Promise<T>;
|
|
163
|
+
/** Signs in to `account`'s SMTP and out again, sending nothing. A failure is a MailError. */
|
|
164
|
+
export declare function smtpSignIn(account: OpenedAccount): Promise<void>;
|
|
165
|
+
/** `raw` sent through `account`'s SMTP to `to`, from its address: the addresses the server took and those it refused (a
|
|
166
|
+
* server refuses some at RCPT and takes the rest, and nodemailer sends to those, throwing nothing). A failure, every
|
|
167
|
+
* recipient refused among them, is a MailError. */
|
|
168
|
+
export declare function smtpSend(account: OpenedAccount, to: readonly string[], raw: Buffer): Promise<{
|
|
169
|
+
accepted: string[];
|
|
170
|
+
rejected: string[];
|
|
171
|
+
}>;
|
|
172
|
+
/** A message built whole: its bytes and its Message-ID. */
|
|
173
|
+
export declare function composed(fields: {
|
|
174
|
+
from: string;
|
|
175
|
+
to: readonly string[];
|
|
176
|
+
cc: readonly string[];
|
|
177
|
+
subject: string;
|
|
178
|
+
text: string;
|
|
179
|
+
inReplyTo?: string;
|
|
180
|
+
references?: readonly string[];
|
|
181
|
+
headers?: Record<string, string>;
|
|
182
|
+
date?: Date;
|
|
183
|
+
}): Promise<{
|
|
184
|
+
raw: Buffer;
|
|
185
|
+
messageId: string;
|
|
186
|
+
}>;
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
// The wire to the server's mail account (account.ts): one IMAP session at a time (imapflow), SMTP for what is sent
|
|
2
|
+
// (nodemailer), a message parsed (mailparser) into the one shape the trigger's payload and the tools answer, and a
|
|
3
|
+
// message built (nodemailer's composer) for a draft or a send. TLS always: 993 and 465 from the start, any other port
|
|
4
|
+
// with STARTTLS required, plaintext only to the loopback address (the fakes, account.ts securityOf). Every mailbox a read
|
|
5
|
+
// or a poll opens is opened read-only (EXAMINE) and every message read with BODY.PEEK, so no read or poll changes a flag;
|
|
6
|
+
// only filing (email_file) opens one read-write, to label or move the one message it files. An error is one
|
|
7
|
+
// of four kinds (MailError), said in words that never hold the password: the server could not be reached, it refused
|
|
8
|
+
// the sign-in, a mailbox or message is not there, or it answered something else.
|
|
9
|
+
import { ImapFlow } from "imapflow";
|
|
10
|
+
import { simpleParser } from "mailparser";
|
|
11
|
+
import nodemailer from "nodemailer";
|
|
12
|
+
import MailComposer from "nodemailer/lib/mail-composer";
|
|
13
|
+
import { securityOf } from "./account.js";
|
|
14
|
+
/** How long a connection may take to open and greet. */
|
|
15
|
+
export const CONNECT_TIMEOUT_MS = 15_000;
|
|
16
|
+
/** How long a connection may sit with no answer. */
|
|
17
|
+
export const SOCKET_TIMEOUT_MS = 60_000;
|
|
18
|
+
/** How long a run's email tool waits on an answer: a search the server has not indexed takes minutes on a large mailbox. */
|
|
19
|
+
export const TOOL_SOCKET_TIMEOUT_MS = 5 * 60_000;
|
|
20
|
+
/** A test's IMAP socket timeout, in place of every other (setSocketTimeoutForTests). */
|
|
21
|
+
let testSocketTimeoutMs;
|
|
22
|
+
/** Tests only: every IMAP session's socket timeout made `ms` (undefined: each its own again), so a server that never
|
|
23
|
+
* answers is given up on in a test's time. */
|
|
24
|
+
export function setSocketTimeoutForTests(ms) {
|
|
25
|
+
testSocketTimeoutMs = ms;
|
|
26
|
+
}
|
|
27
|
+
/** The most of a message a search reads: its headers and the start of its text, for the snippet. */
|
|
28
|
+
export const SEARCH_BYTES = 256 * 1024;
|
|
29
|
+
/** The most of a message a read or a poll takes. */
|
|
30
|
+
export const READ_BYTES = 2 * 1024 * 1024;
|
|
31
|
+
/** The header email_send marks everything it sends with (its value the run's id): a poll leaves such a message. */
|
|
32
|
+
export const SENT_HEADER = "X-AgentRun-Sent";
|
|
33
|
+
/** A snippet's characters. */
|
|
34
|
+
const SNIPPET_CHARS = 200;
|
|
35
|
+
/** Why a mail server did not do what was asked: `unreachable`, it could not be reached; `refused`, it refused the
|
|
36
|
+
* sign-in; `missing`, a mailbox or message is not there; `failed`, anything else. Its message is fit to show. */
|
|
37
|
+
export class MailError extends Error {
|
|
38
|
+
kind;
|
|
39
|
+
constructor(kind, message) {
|
|
40
|
+
super(message);
|
|
41
|
+
this.kind = kind;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** The error codes of a server that could not be reached, imapflow's and nodemailer's. */
|
|
45
|
+
const UNREACHABLE = new Set(["ECONNREFUSED", "ENOTFOUND", "EAI_AGAIN", "ETIMEDOUT", "EHOSTUNREACH", "ENETUNREACH", "ECONNRESET", "EPIPE", "CONNECT_TIMEOUT", "GREETING_TIMEOUT", "UPGRADE_TIMEOUT", "NoConnection", "ECONNECTION", "ESOCKET", "EDNS", "ETIMEOUT"]);
|
|
46
|
+
/** `error`, from the server at `endpoint` over `protocol`, as a MailError whose words hold nothing of `password`. */
|
|
47
|
+
export function mailError(error, endpoint, protocol, password) {
|
|
48
|
+
if (error instanceof MailError)
|
|
49
|
+
return error;
|
|
50
|
+
const e = (error ?? {});
|
|
51
|
+
const code = typeof e.code === "string" ? e.code : undefined;
|
|
52
|
+
const words = scrub(String(e.message ?? error), password);
|
|
53
|
+
if (e.authenticationFailed === true || e.serverResponseCode === "AUTHENTICATIONFAILED" || code === "EAUTH")
|
|
54
|
+
return new MailError("refused", `${endpoint.host} refused the sign-in (${protocol}): check the user and the app password in Settings → Email.`);
|
|
55
|
+
if (code && UNREACHABLE.has(code))
|
|
56
|
+
return new MailError("unreachable", `Couldn't reach ${endpoint.host}:${endpoint.port} (${protocol}, ${code}).`);
|
|
57
|
+
if (code === "ETLS" || /certificate|\bTLS\b|\bSSL\b|STARTTLS/i.test(words))
|
|
58
|
+
return new MailError("failed", `The secure connection to ${endpoint.host}:${endpoint.port} (${protocol}) failed: ${clip(words, 300)}`);
|
|
59
|
+
return new MailError("failed", `${endpoint.host} (${protocol}) answered: ${clip(words, 300)}`);
|
|
60
|
+
}
|
|
61
|
+
const clip = (text, n) => (text.length <= n ? text : `${text.slice(0, n - 1)}…`);
|
|
62
|
+
/** `text` with every copy of `password` taken out. */
|
|
63
|
+
const scrub = (text, password) => (password ? text.split(password).join("[password]") : text);
|
|
64
|
+
/** The first `<...>` id of a header's value, or null. */
|
|
65
|
+
const firstId = (value) => (typeof value === "string" ? (/<[^<>\s]+>/.exec(value)?.[0] ?? (value.trim() || null)) : null);
|
|
66
|
+
/** Every `<...>` id of a header's value (a string or a list). */
|
|
67
|
+
const idsOf = (value) => (Array.isArray(value) ? value : typeof value === "string" ? [value] : []).flatMap((one) => (typeof one === "string" ? (one.match(/<[^<>\s]+>/g) ?? []) : []));
|
|
68
|
+
/** The bare addresses of an address header, groups opened. */
|
|
69
|
+
function addresses(value) {
|
|
70
|
+
const out = [];
|
|
71
|
+
const walk = (list) => {
|
|
72
|
+
for (const one of list) {
|
|
73
|
+
if (one.group)
|
|
74
|
+
walk(one.group);
|
|
75
|
+
else if (one.address)
|
|
76
|
+
out.push({ address: one.address, name: one.name });
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
for (const object of Array.isArray(value) ? value : value ? [value] : [])
|
|
80
|
+
walk(object.value);
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
/** A fetched message, parsed: `source` its bytes (the first `READ_BYTES` or `SEARCH_BYTES` of it). */
|
|
84
|
+
export async function messageOf(row, mailbox) {
|
|
85
|
+
const parsed = await simpleParser(row.source ?? Buffer.alloc(0), { skipImageLinks: true, skipTextLinks: true });
|
|
86
|
+
const [from] = addresses(parsed.from);
|
|
87
|
+
const internal = row.internalDate ? new Date(row.internalDate) : undefined;
|
|
88
|
+
const date = parsed.date ?? (internal && Number.isFinite(internal.getTime()) ? internal : undefined);
|
|
89
|
+
const sentHere = parsed.headers.get(SENT_HEADER.toLowerCase());
|
|
90
|
+
return {
|
|
91
|
+
uid: row.uid,
|
|
92
|
+
mailbox,
|
|
93
|
+
flags: [...(row.flags ?? [])],
|
|
94
|
+
from: from?.address ?? "",
|
|
95
|
+
from_name: from?.name || null,
|
|
96
|
+
to: addresses(parsed.to).map((a) => a.address),
|
|
97
|
+
cc: addresses(parsed.cc).map((a) => a.address),
|
|
98
|
+
subject: parsed.subject ?? "",
|
|
99
|
+
date: date ? date.toISOString() : null,
|
|
100
|
+
message_id: firstId(parsed.messageId),
|
|
101
|
+
in_reply_to: firstId(parsed.inReplyTo),
|
|
102
|
+
references: idsOf(parsed.references),
|
|
103
|
+
text: (parsed.text ?? "").replace(/\s+$/, ""),
|
|
104
|
+
attachments: parsed.attachments.map((a) => ({ name: a.filename ?? "attachment", size: a.size, type: a.contentType })),
|
|
105
|
+
...(typeof sentHere === "string" && sentHere.trim() ? { sent_here: sentHere.trim() } : {}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** A fetched row of `heads`: its few headers parsed as a whole message's are (messageOf), its date the Date header's, else
|
|
109
|
+
* INTERNALDATE. */
|
|
110
|
+
async function headOf(row, mailbox) {
|
|
111
|
+
const parsed = await simpleParser(Buffer.concat([row.headers ?? Buffer.alloc(0), Buffer.from("\r\n")]), { skipImageLinks: true, skipTextLinks: true });
|
|
112
|
+
const [from] = addresses(parsed.from);
|
|
113
|
+
const internal = row.internalDate ? new Date(row.internalDate) : undefined;
|
|
114
|
+
const date = parsed.date ?? (internal && Number.isFinite(internal.getTime()) ? internal : undefined);
|
|
115
|
+
return {
|
|
116
|
+
uid: row.uid,
|
|
117
|
+
mailbox,
|
|
118
|
+
size: typeof row.size === "number" ? row.size : 0,
|
|
119
|
+
from: from?.address ?? "",
|
|
120
|
+
to: addresses(parsed.to).map((a) => a.address),
|
|
121
|
+
cc: addresses(parsed.cc).map((a) => a.address),
|
|
122
|
+
date: date ? date.toISOString() : null,
|
|
123
|
+
message_id: firstId(parsed.messageId),
|
|
124
|
+
in_reply_to: firstId(parsed.inReplyTo),
|
|
125
|
+
references: idsOf(parsed.references),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Whether `address` (bare, any case) is one of `m`'s From, To or Cc. */
|
|
129
|
+
export const involves = (m, address) => {
|
|
130
|
+
const wanted = address.trim().toLowerCase();
|
|
131
|
+
return wanted !== "" && [m.from, ...m.to, ...m.cc].some((one) => one.trim().toLowerCase() === wanted);
|
|
132
|
+
};
|
|
133
|
+
/** The payload's fields (a trigger's firing, a read's message). */
|
|
134
|
+
export const payloadOf = (m) => ({ from: m.from, from_name: m.from_name, to: m.to, cc: m.cc, subject: m.subject, date: m.date, message_id: m.message_id, in_reply_to: m.in_reply_to, text: m.text, attachments: m.attachments });
|
|
135
|
+
/** A read's message: the payload and its references. */
|
|
136
|
+
export const readOf = (m) => ({ ...payloadOf(m), references: m.references });
|
|
137
|
+
/** A search's message: who, what, when, the start of its text, whether it is unread. */
|
|
138
|
+
export const foundOf = (m) => ({ message_id: m.message_id, from: m.from, to: m.to, subject: m.subject, date: m.date, snippet: clip(m.text.replace(/\s+/g, " ").trim(), SNIPPET_CHARS), unread: !m.flags.includes("\\Seen"), attachments: m.attachments });
|
|
139
|
+
/** The IMAP search clause that takes the messages of a thread whose ids are `ids`: a message is in it when its Message-ID,
|
|
140
|
+
* one of its References or (with `inReplyTo`) its In-Reply-To is one of them. */
|
|
141
|
+
export const threadClause = (ids, inReplyTo = true) => ({
|
|
142
|
+
or: ids.flatMap((id) => ["message-id", "references", ...(inReplyTo ? ["in-reply-to"] : [])].map((name) => ({ header: { [name]: id } }))),
|
|
143
|
+
});
|
|
144
|
+
/** Whether `m` is in the thread of `ids` (the clause's own test, checked again on what the server answered). */
|
|
145
|
+
export const inThread = (m, ids) => (m.message_id !== null && ids.has(m.message_id)) || (m.in_reply_to !== null && ids.has(m.in_reply_to)) || m.references.some((id) => ids.has(id));
|
|
146
|
+
/** The special-use folders a mailbox name may stand for: "Drafts" and "Sent", or their flags. */
|
|
147
|
+
const SPECIAL = { drafts: "\\Drafts", "\\drafts": "\\Drafts", sent: "\\Sent", "\\sent": "\\Sent" };
|
|
148
|
+
/** The folders a search across the account skips: junk and trash are no one's mail. */
|
|
149
|
+
const SKIPPED = new Set(["\\Junk", "\\Trash"]);
|
|
150
|
+
/** One signed-in IMAP session. */
|
|
151
|
+
export class ImapSession {
|
|
152
|
+
client;
|
|
153
|
+
listed;
|
|
154
|
+
constructor(client) {
|
|
155
|
+
this.client = client;
|
|
156
|
+
}
|
|
157
|
+
/** The account's mailboxes, listed once a session. */
|
|
158
|
+
async mailboxes() {
|
|
159
|
+
return (this.listed ??= await this.client.list());
|
|
160
|
+
}
|
|
161
|
+
/** The folder a mailbox name stands for: "INBOX"; "Drafts", "Sent", "\\Drafts" or "\\Sent" the special-use folder (its
|
|
162
|
+
* folder of that name when the server flags none); any other a folder's own name. MailError `missing` when there is
|
|
163
|
+
* none. */
|
|
164
|
+
async resolve(name) {
|
|
165
|
+
if (name.toUpperCase() === "INBOX")
|
|
166
|
+
return "INBOX";
|
|
167
|
+
const boxes = await this.mailboxes();
|
|
168
|
+
const flag = SPECIAL[name.toLowerCase()];
|
|
169
|
+
if (flag) {
|
|
170
|
+
const found = boxes.find((box) => box.specialUse === flag) ?? boxes.find((box) => box.path.toLowerCase() === flag.slice(1).toLowerCase());
|
|
171
|
+
if (found)
|
|
172
|
+
return found.path;
|
|
173
|
+
throw new MailError("missing", `This account has no ${flag.slice(1)} folder.`);
|
|
174
|
+
}
|
|
175
|
+
const found = boxes.find((box) => box.path === name);
|
|
176
|
+
if (!found || found.flags.has("\\Noselect") || found.flags.has("\\NonExistent"))
|
|
177
|
+
throw new MailError("missing", `This account has no mailbox named ${JSON.stringify(name)}.`);
|
|
178
|
+
return found.path;
|
|
179
|
+
}
|
|
180
|
+
/** Every mailbox a search across the account reads: INBOX first, then each one that holds messages, junk and trash
|
|
181
|
+
* apart. */
|
|
182
|
+
async searchable() {
|
|
183
|
+
const boxes = (await this.mailboxes()).filter((box) => !box.flags.has("\\Noselect") && !box.flags.has("\\NonExistent") && !SKIPPED.has(box.specialUse ?? ""));
|
|
184
|
+
return ["INBOX", ...boxes.map((box) => box.path).filter((path) => path.toUpperCase() !== "INBOX")];
|
|
185
|
+
}
|
|
186
|
+
/** `path` opened read-only (EXAMINE): its UIDVALIDITY, its next UID, and how many messages it holds. */
|
|
187
|
+
async open(path) {
|
|
188
|
+
const box = await this.client.mailboxOpen(path, { readOnly: true });
|
|
189
|
+
return { path: box.path, uidValidity: String(box.uidValidity), uidNext: box.uidNext, exists: box.exists };
|
|
190
|
+
}
|
|
191
|
+
/** The UIDs of the open mailbox's messages `query` takes, lowest first. */
|
|
192
|
+
async uids(query) {
|
|
193
|
+
const found = await this.client.search(Object.keys(query).length ? query : { all: true }, { uid: true });
|
|
194
|
+
return (Array.isArray(found) ? found : []).sort((a, b) => a - b);
|
|
195
|
+
}
|
|
196
|
+
/** The open mailbox's messages of `uids`, each read with BODY.PEEK (at most `bytes` of it) and parsed, in UID order. */
|
|
197
|
+
async fetch(path, uids, bytes) {
|
|
198
|
+
if (!uids.length)
|
|
199
|
+
return [];
|
|
200
|
+
const rows = await this.client.fetchAll(uids.join(","), { uid: true, flags: true, internalDate: true, source: { maxLength: bytes } }, { uid: true });
|
|
201
|
+
const messages = await Promise.all(rows.map((row) => messageOf(row, path)));
|
|
202
|
+
return messages.sort((a, b) => a.uid - b.uid);
|
|
203
|
+
}
|
|
204
|
+
/** The open mailbox's messages of `uids`, read cheaply: who they are between, their ids, their date and their size from a few headers,
|
|
205
|
+
* INTERNALDATE and RFC822.SIZE, never a body. What a thread read picks from before it fetches any message whole. */
|
|
206
|
+
async heads(path, uids) {
|
|
207
|
+
if (!uids.length)
|
|
208
|
+
return [];
|
|
209
|
+
const rows = await this.client.fetchAll(uids.join(","), { uid: true, internalDate: true, size: true, headers: ["message-id", "in-reply-to", "references", "date", "from", "to", "cc"] }, { uid: true });
|
|
210
|
+
return (await Promise.all(rows.map((row) => headOf(row, path)))).sort((a, b) => a.uid - b.uid);
|
|
211
|
+
}
|
|
212
|
+
/** The clause that takes the thread of `ids` on this server. Gmail (X-GM-EXT-1) indexes Message-ID and References but
|
|
213
|
+
* not In-Reply-To, a search by which takes minutes on a large mailbox: there a reply is found by its References, which
|
|
214
|
+
* a mail client writes with its In-Reply-To. */
|
|
215
|
+
threadQuery(ids) {
|
|
216
|
+
return threadClause(ids, !this.can("X-GM-EXT-1"));
|
|
217
|
+
}
|
|
218
|
+
/** Whether the server says it does `capability` (X-GM-EXT-1: Gmail's labels). */
|
|
219
|
+
can(capability) {
|
|
220
|
+
return this.client.capabilities.has(capability);
|
|
221
|
+
}
|
|
222
|
+
/** `path` opened read-write (SELECT), to change what it holds. */
|
|
223
|
+
async select(path) {
|
|
224
|
+
await this.client.mailboxOpen(path, { readOnly: false });
|
|
225
|
+
}
|
|
226
|
+
/** The folder (or Gmail label) `path`, created when the account has none of that name; whether it was. */
|
|
227
|
+
async ensure(path) {
|
|
228
|
+
if ((await this.mailboxes()).some((box) => box.path === path))
|
|
229
|
+
return false;
|
|
230
|
+
const made = await this.client.mailboxCreate(path);
|
|
231
|
+
this.listed = undefined;
|
|
232
|
+
return made?.created !== false;
|
|
233
|
+
}
|
|
234
|
+
/** Gmail labels `labels` added to (or, `remove`, taken off) message `uid` of the open mailbox. */
|
|
235
|
+
async label(uid, labels, remove = false) {
|
|
236
|
+
const options = { uid: true, useLabels: true };
|
|
237
|
+
const done = remove ? await this.client.messageFlagsRemove(String(uid), labels, options) : await this.client.messageFlagsAdd(String(uid), labels, options);
|
|
238
|
+
if (!done)
|
|
239
|
+
throw new MailError("failed", `The mail server did not ${remove ? "take the label off" : "label"} the message.`);
|
|
240
|
+
}
|
|
241
|
+
/** Message `uid` of the open mailbox moved to `path` (UID MOVE, else UID COPY and UID EXPUNGE). */
|
|
242
|
+
async move(uid, path) {
|
|
243
|
+
const done = await this.client.messageMove(String(uid), path, { uid: true });
|
|
244
|
+
if (!done)
|
|
245
|
+
throw new MailError("failed", `The mail server did not move the message to ${path}.`);
|
|
246
|
+
}
|
|
247
|
+
/** `raw` appended to `path` with `flags`. */
|
|
248
|
+
async append(path, raw, flags) {
|
|
249
|
+
const done = await this.client.append(path, raw, flags);
|
|
250
|
+
if (done === false)
|
|
251
|
+
throw new MailError("failed", `The mail server did not keep the message in ${path}.`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** `work` in one IMAP session of `account`, signed in, then signed out, waiting on each answer `socketTimeoutMs` at most
|
|
255
|
+
* (SOCKET_TIMEOUT_MS by default). A failure is a MailError; a server that answers nothing for that long, one that says
|
|
256
|
+
* the server took too long. */
|
|
257
|
+
export async function withImap(account, work, options = {}) {
|
|
258
|
+
const security = securityOf(account.imap);
|
|
259
|
+
const socketTimeoutMs = testSocketTimeoutMs ?? options.socketTimeoutMs ?? SOCKET_TIMEOUT_MS;
|
|
260
|
+
const client = new ImapFlow({
|
|
261
|
+
host: account.imap.host,
|
|
262
|
+
port: account.imap.port,
|
|
263
|
+
secure: security === "tls",
|
|
264
|
+
...(security === "starttls" ? { doSTARTTLS: true } : security === "plaintext" ? { doSTARTTLS: false } : {}),
|
|
265
|
+
auth: { user: account.user, pass: account.password },
|
|
266
|
+
logger: false,
|
|
267
|
+
disableAutoIdle: true,
|
|
268
|
+
disableCompression: true,
|
|
269
|
+
connectionTimeout: CONNECT_TIMEOUT_MS,
|
|
270
|
+
greetingTimeout: CONNECT_TIMEOUT_MS,
|
|
271
|
+
socketTimeout: socketTimeoutMs,
|
|
272
|
+
});
|
|
273
|
+
// A socket error after a command is answered by the command that waits on it; one with none waiting must not throw. A
|
|
274
|
+
// socket timeout (the server silent past socketTimeoutMs) is kept, so the words say the server was slow, not unreachable.
|
|
275
|
+
let silent = false;
|
|
276
|
+
client.on("error", (error) => {
|
|
277
|
+
if (error?.code === "ETIMEOUT")
|
|
278
|
+
silent = true;
|
|
279
|
+
});
|
|
280
|
+
try {
|
|
281
|
+
await client.connect();
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
client.close();
|
|
285
|
+
throw mailError(error, account.imap, "IMAP", account.password);
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
return await work(new ImapSession(client));
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
if (silent && !(error instanceof MailError))
|
|
292
|
+
throw new MailError("failed", `${account.imap.host} took too long to search: it answered nothing for ${Math.round(socketTimeoutMs / 1000)} s, so the connection was closed (IMAP). Try again later.`);
|
|
293
|
+
throw mailError(error, account.imap, "IMAP", account.password);
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
await client.logout().catch(() => client.close());
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/** The SMTP transport of `account`: one connection per use. */
|
|
300
|
+
function transportOf(account) {
|
|
301
|
+
const security = securityOf(account.smtp);
|
|
302
|
+
return nodemailer.createTransport({
|
|
303
|
+
host: account.smtp.host,
|
|
304
|
+
port: account.smtp.port,
|
|
305
|
+
secure: security === "tls",
|
|
306
|
+
requireTLS: security === "starttls",
|
|
307
|
+
ignoreTLS: security === "plaintext",
|
|
308
|
+
auth: { user: account.user, pass: account.password },
|
|
309
|
+
logger: false,
|
|
310
|
+
debug: false,
|
|
311
|
+
connectionTimeout: CONNECT_TIMEOUT_MS,
|
|
312
|
+
greetingTimeout: CONNECT_TIMEOUT_MS,
|
|
313
|
+
socketTimeout: SOCKET_TIMEOUT_MS,
|
|
314
|
+
disableFileAccess: true,
|
|
315
|
+
disableUrlAccess: true,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
/** Signs in to `account`'s SMTP and out again, sending nothing. A failure is a MailError. */
|
|
319
|
+
export async function smtpSignIn(account) {
|
|
320
|
+
const transport = transportOf(account);
|
|
321
|
+
try {
|
|
322
|
+
await transport.verify();
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
throw mailError(error, account.smtp, "SMTP", account.password);
|
|
326
|
+
}
|
|
327
|
+
finally {
|
|
328
|
+
transport.close();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/** `raw` sent through `account`'s SMTP to `to`, from its address: the addresses the server took and those it refused (a
|
|
332
|
+
* server refuses some at RCPT and takes the rest, and nodemailer sends to those, throwing nothing). A failure, every
|
|
333
|
+
* recipient refused among them, is a MailError. */
|
|
334
|
+
export async function smtpSend(account, to, raw) {
|
|
335
|
+
const transport = transportOf(account);
|
|
336
|
+
try {
|
|
337
|
+
const info = await transport.sendMail({ envelope: { from: account.address, to: [...to] }, raw });
|
|
338
|
+
const address = (one) => (typeof one === "string" ? one : isAddress(one) ? one.address : String(one));
|
|
339
|
+
return { accepted: (info.accepted ?? []).map(address), rejected: (info.rejected ?? []).map(address) };
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
throw mailError(error, account.smtp, "SMTP", account.password);
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
transport.close();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
/** A nodemailer address object ({name, address}). */
|
|
349
|
+
const isAddress = (one) => typeof one === "object" && one !== null && typeof one.address === "string";
|
|
350
|
+
/** A message built whole: its bytes and its Message-ID. */
|
|
351
|
+
export async function composed(fields) {
|
|
352
|
+
const node = new MailComposer({
|
|
353
|
+
from: fields.from,
|
|
354
|
+
...(fields.to.length ? { to: [...fields.to] } : {}),
|
|
355
|
+
...(fields.cc.length ? { cc: [...fields.cc] } : {}),
|
|
356
|
+
subject: fields.subject,
|
|
357
|
+
text: fields.text,
|
|
358
|
+
...(fields.inReplyTo ? { inReplyTo: fields.inReplyTo } : {}),
|
|
359
|
+
...(fields.references?.length ? { references: [...fields.references] } : {}),
|
|
360
|
+
...(fields.headers ? { headers: fields.headers } : {}),
|
|
361
|
+
date: fields.date ?? new Date(),
|
|
362
|
+
xMailer: false,
|
|
363
|
+
disableFileAccess: true,
|
|
364
|
+
disableUrlAccess: true,
|
|
365
|
+
}).compile();
|
|
366
|
+
return { raw: ownCase(await node.build(), Object.keys(fields.headers ?? {})), messageId: node.messageId() };
|
|
367
|
+
}
|
|
368
|
+
/** `raw` with each of `names` written as given in its header block: the composer writes a header's name in its own case
|
|
369
|
+
* (X-Agentrun-Sent), and a name is case-blind to a reader but not to a person's filter. */
|
|
370
|
+
function ownCase(raw, names) {
|
|
371
|
+
if (!names.length)
|
|
372
|
+
return raw;
|
|
373
|
+
const end = raw.indexOf("\r\n\r\n");
|
|
374
|
+
if (end < 0)
|
|
375
|
+
return raw;
|
|
376
|
+
let head = raw.subarray(0, end).toString("latin1");
|
|
377
|
+
for (const name of names)
|
|
378
|
+
head = head.replace(new RegExp(`^${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}:`, "gim"), `${name}:`);
|
|
379
|
+
return Buffer.concat([Buffer.from(head, "latin1"), raw.subarray(end)]);
|
|
380
|
+
}
|