@mindot/will 0.7.0 → 0.9.0

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.
Files changed (88) hide show
  1. package/README.md +87 -22
  2. package/dist/channels/discord.d.ts +67 -6
  3. package/dist/channels/discord.js +112 -6
  4. package/dist/channels/discord.js.map +1 -1
  5. package/dist/channels/whatsapp.d.ts +1 -1
  6. package/dist/channels/whatsapp.js +4 -1
  7. package/dist/channels/whatsapp.js.map +1 -1
  8. package/dist/cli.js +13733 -11057
  9. package/dist/cli.js.map +1 -1
  10. package/dist/index.d.ts +2 -2
  11. package/dist/index.js +3729 -1199
  12. package/dist/index.js.map +1 -1
  13. package/dist/mcp/effectors.d.ts +1 -1
  14. package/dist/{will-DAW0l-lY.d.ts → will-DbDj_TEH.d.ts} +1214 -93
  15. package/package.json +1 -1
  16. package/src/channels/discord.ts +189 -11
  17. package/src/channels/types.ts +90 -0
  18. package/src/channels/whatsapp.ts +13 -4
  19. package/src/cli.ts +9 -4
  20. package/src/cognition/agency/consequence.ts +122 -1
  21. package/src/cognition/agency/conversation.aim.ts +260 -0
  22. package/src/cognition/agency/engines/action.selector.ts +85 -3
  23. package/src/cognition/agency/engines/affordance.synthesizer.ts +90 -1
  24. package/src/cognition/agency/engines/motor.schema.executor.ts +152 -10
  25. package/src/cognition/agency/engines/reafference.engine.ts +129 -2
  26. package/src/cognition/agency/proactive.communicator.ts +19 -3
  27. package/src/cognition/agency/reconcile.learning.ts +16 -2
  28. package/src/cognition/agency/restart.ts +66 -0
  29. package/src/cognition/agency/schemas/repertoire.ts +12 -5
  30. package/src/cognition/agency/selection.scoring.ts +33 -0
  31. package/src/cognition/agency/types.ts +35 -0
  32. package/src/cognition/cache/composition.ts +232 -0
  33. package/src/cognition/cache/deliberation.cache.ts +219 -0
  34. package/src/cognition/cache/fingerprint.ts +120 -0
  35. package/src/cognition/cache/types.ts +105 -0
  36. package/src/cognition/config.mirror.entities.ts +109 -1
  37. package/src/cognition/event.schemas.ts +22 -0
  38. package/src/cognition/faculties/autobiographical.narrator.ts +5 -10
  39. package/src/cognition/faculties/episodic.consolidator.ts +59 -3
  40. package/src/cognition/faculties/executive.engine/commands.ts +189 -14
  41. package/src/cognition/faculties/executive.engine/context.ts +67 -13
  42. package/src/cognition/faculties/executive.engine/deliberate.reasoning.ts +1 -1
  43. package/src/cognition/faculties/executive.engine/engine.ts +686 -187
  44. package/src/cognition/faculties/executive.engine/escalation.buffer.ts +162 -44
  45. package/src/cognition/faculties/executive.engine/facet.supervisor.ts +310 -65
  46. package/src/cognition/faculties/executive.engine/facet.ts +89 -26
  47. package/src/cognition/faculties/executive.engine/gating.ts +14 -14
  48. package/src/cognition/faculties/executive.engine/parser.ts +21 -1
  49. package/src/cognition/faculties/executive.engine/prompt.factory.ts +169 -20
  50. package/src/cognition/faculties/executive.engine/types.ts +69 -0
  51. package/src/cognition/faculties/goal.manager.ts +94 -14
  52. package/src/cognition/faculties/known.entity.tracker.ts +267 -28
  53. package/src/cognition/faculties/moral.evaluator.ts +8 -3
  54. package/src/cognition/faculties/persona.consolidator.ts +141 -0
  55. package/src/cognition/faculties/reputation.tracker.ts +66 -2
  56. package/src/cognition/faculties/self.model.updater.ts +19 -12
  57. package/src/cognition/faculties/social.perception.ts +47 -3
  58. package/src/cognition/faculties/threat.evaluator.ts +7 -0
  59. package/src/cognition/faculties/working.memory.ts +10 -20
  60. package/src/cognition/identity.entity.ts +205 -0
  61. package/src/cognition/index.ts +11 -0
  62. package/src/cognition/memory/vector.adapter.ts +12 -3
  63. package/src/cognition/memory/vector.embedder.ts +54 -7
  64. package/src/cognition/persona.prior.ts +6 -0
  65. package/src/cognition/senses/audition.engine/engine.ts +404 -46
  66. package/src/cognition/senses/base.sense.engine.ts +1 -1
  67. package/src/cognition/senses/index.ts +12 -0
  68. package/src/cognition/social.identity.ts +273 -0
  69. package/src/cognition/utilities/token.tracker.ts +246 -98
  70. package/src/core/orchestrator.ts +38 -0
  71. package/src/host/boot.ts +78 -22
  72. package/src/index.ts +35 -0
  73. package/src/llm/index.ts +415 -97
  74. package/src/llm/routing.ts +204 -0
  75. package/src/llm/summarizer.ts +5 -1
  76. package/src/llm/wire.contracts.ts +19 -0
  77. package/src/pma/index.ts +67 -53
  78. package/src/runners/thin-shim.runner.ts +18 -6
  79. package/src/sdk/will.ts +121 -22
  80. package/src/stem/assembly.audit.ts +1 -0
  81. package/src/stem/guards/identity.coherence.ts +17 -6
  82. package/src/stem/index.ts +82 -5
  83. package/src/stem/mind.ts +327 -79
  84. package/src/stem/policy/arbiter.ts +49 -14
  85. package/src/stem/policy/rule.table.ts +2 -2
  86. package/src/stem/tracts/effector.controller.ts +56 -9
  87. package/src/stem/tracts/outbox.writer.ts +40 -2
  88. package/src/cognition/faculties/executive.engine/messages.ts +0 -102
package/README.md CHANGED
@@ -74,8 +74,10 @@ ANTHROPIC_API_KEY=sk-ant-… \
74
74
  bun run examples/with-anthropic.ts # a real executive: genuine reasoning + replies
75
75
  ```
76
76
 
77
- Requires [Bun](https://bun.sh) ≥ 1.1. For a real executive set `WILL_LLM_PROVIDER=anthropic`
78
- + `ANTHROPIC_API_KEY` (other providers are scaffolded but not yet supported). The dev
77
+ Requires [Bun](https://bun.sh) ≥ 1.1. For a real executive set a provider, a model and a
78
+ key `WILL_LLM_PROVIDER=anthropic` + `WILL_LLM_MODEL=claude-sonnet-4-5-20250929` +
79
+ `ANTHROPIC_API_KEY`. All three are required and none is guessed: [a dozen providers are
80
+ first-class](#llm-provider), and a default here would send your key to the wrong one. The dev
79
81
  runner (`bun dev`) starts a long-lived Will: engines step every `WILL_TICK_MS` on the
80
82
  deterministic clock; the ExecutiveEngine fires an LLM call every `WILL_EXECUTIVE_INTERVAL`
81
83
  ticks — or earlier when physiology demands it.
@@ -107,7 +109,9 @@ import { Will } from '@mindot/will'
107
109
  const will = await Will.create({
108
110
  name: 'Aria',
109
111
  identity: { prompt: 'I am Aria, a calm, precise research assistant.' },
110
- // llm defaults to a zero-key deterministic mock unless ANTHROPIC_API_KEY is set
112
+ // Keyless by default: a deterministic mock executive. Set any provider's own
113
+ // key (ANTHROPIC_API_KEY, ZAI_API_KEY, MOONSHOT_API_KEY, …) plus
114
+ // WILL_LLM_MODEL to raise a live mind — or pass `llmConfig` explicitly.
111
115
  })
112
116
 
113
117
  // Hear the Will (replies arrive asynchronously — it reasons on its own tick cycle).
@@ -160,7 +164,7 @@ Host a Will over the [Model Context Protocol](https://modelcontextprotocol.io)
160
164
  }
161
165
  ```
162
166
 
163
- The surface keeps the paradigm: `perceive` delivers a stimulus (it returns when *delivered*, not answered), `next_utterance` awaits the mind's next words (**silence is a valid outcome**, reported — never an error), `state` reads its inner life, and `save` checkpoints it without stopping it. There is deliberately no `ask()`-shaped tool. Config via env: `WILL_ANATOMY` (mind|reflex — reflex is the no-LLM shell), `WILL_LLM_MODEL` (concrete model id), `WILL_LLM` (mock|anthropic — defaults to the zero-key mock unless `ANTHROPIC_API_KEY` is set), `WILL_TICK_MS`, `WILL_PMA_PATH`.
167
+ The surface keeps the paradigm: `perceive` delivers a stimulus (it returns when *delivered*, not answered), `next_utterance` awaits the mind's next words (**silence is a valid outcome**, reported — never an error), `state` reads its inner life, and `save` checkpoints it without stopping it. There is deliberately no `ask()`-shaped tool. Config via env: `WILL_ANATOMY` (mind|reflex — reflex is the no-LLM shell), `WILL_LLM_MODEL` (concrete model id — required for a live mind), `WILL_LLM` (`mock` or [any provider name](#llm-provider) — defaults to the zero-key mock unless some provider's own key is set), `WILL_TICK_MS`, `WILL_PMA_PATH`.
164
168
 
165
169
  ### Employing MCP tools — the mind gets abilities
166
170
 
@@ -220,7 +224,9 @@ const config: WillConfig = {
220
224
  traits: { conscientiousness: 0.9, neuroticism: 0.4 },
221
225
  style: 'measured and warm',
222
226
  },
223
- model: 'claude-sonnet-4-5-20250929', // concrete model id (optional env/provider default otherwise)
227
+ // Provider + model ride together on `llm`. Both are required for a live mind
228
+ // and neither is guessed; omit the whole field for the zero-key mock.
229
+ llm: { provider: 'anthropic', model: 'claude-sonnet-4-5-20250929' },
224
230
  allowedGenericEffectors: ['listen', 'talk', 'text'], // opt in to communication
225
231
  persistentMemory: true,
226
232
  snapshotInterval: 10,
@@ -555,7 +561,11 @@ Traits seed the PersonaPrior as a *starting disposition*, not a fixed personalit
555
561
  | `name` | ✅ | — | Human-readable display name |
556
562
  | `identity` | ✅ | — | Persona: `{ prompt, values[], traits{}, style }` (Layer 2) |
557
563
  | `anatomy` | — | `'mind'` | `'mind' \| 'reflex'` — the only structural variant. `mind` runs the whole architecture; `reflex` is a no-LLM shell (regulatory + senses + agency heuristics) for embedded use. Faculties are not a pricing axis — budgets are. |
558
- | `model` | — | env / provider default | Concrete LLM model id (e.g. `'claude-sonnet-4-5-20250929'`). `WILL_LLM_MODEL` env pins over this. Product tier labels resolve to a concrete id host-side, before the engine. |
564
+ | `llm.provider` | ✅ *(live)* | — | Which provider to speak to. No default: see [LLM provider](#llm-provider). `WILL_LLM_PROVIDER` env fills in |
565
+ | `llm.model` | ✅ *(live)* | — | A concrete model id (`'claude-sonnet-4-5-20250929'`), **or** a per-role map `{ executive, summarizer, deliberation, conversation, embedding }`. Unset roles fall back to `executive`. `WILL_LLM_MODEL` pins every thinking role. Product tier labels resolve to a concrete id host-side, before the engine |
566
+ | `llm.apiKey` / `llm.baseUrl` | — | env | Credential + endpoint for the default provider. Held in memory only — never state, logs, or PMA |
567
+ | `llm.providers` | — | — | Everything the host knows per provider: `{ apiKey, baseUrl?, wire?, prices? }`. Needed to route *across* vendors, and the only place prices live — the engine ships none |
568
+ | `llm.router` | — | `NULL_ROUTER` | A `ModelRouter` choosing a model per call from the call's attribution and `demand`. Chains ahead of the compiled role map. See [Different models for different thinking](#different-models-for-different-thinking) |
559
569
  | `persistentMemory` | ✅ | — | Persist snapshots so beliefs/goals/narrative survive restarts |
560
570
  | `snapshotInterval` | ✅ | — | Ticks between in-memory snapshots |
561
571
  | `profile` | — | `null` | World profile preset (effectors + environment context). Merged with `allowedGenericEffectors` |
@@ -741,15 +751,22 @@ src/
741
751
  │ │ └── access.grants.ts · proactive.communicator.ts
742
752
  │ └── memory/ # in-house vector index + embedder (semantic recall)
743
753
 
744
- ├── llm/ # in-house provider client (Anthropic) + concurrency gate + summariser
754
+ ├── llm/ # in-house multi-provider client no ai-sdk/Mastra runtime dep
755
+ │ ├── index.ts # wire dialects (anthropic·openai·google), provider table, director
756
+ │ ├── routing.ts # which model serves which call — ModelRouter, TableRouter, chainRouters
757
+ │ ├── gate.ts # global concurrency semaphore + 429 backoff
758
+ │ └── summarizer.ts · wire.contracts.ts
745
759
  ├── pma/ # PMADistiller, PMALoader + reconstruction-fidelity eval
746
760
  ├── profiles/ # world profile presets (companion, game-npc, customer-service, …)
747
761
  ├── eval/ · extensions/ · runners/
748
762
  ├── types.ts # public API types (OutboxMessage, EffectorInvocation, …)
749
763
 
750
764
  └── stem/
751
- ├── mind.ts # assembleMind() — engine graph factory
765
+ ├── mind.ts # assembleMind() — engine graph factory; compiles the role
766
+ │ # model map into routing rules
752
767
  ├── index.ts # WillStem — lifecycle, tick loop, outbox, acks
768
+ ├── policy/ # what a Will MAY enact — arbiter seam, rule table, verdict tape
769
+ ├── guards/ # identity coherence — the self a drifting output is checked against
753
770
  └── tracts/ # lifecycle controllers: outbox, effector, sensory, transport,
754
771
  # replay, pma, health, biography, ack, session log
755
772
  ```
@@ -779,17 +796,16 @@ cd will && bun run build
779
796
 
780
797
  | Variable | Default | Description |
781
798
  |---|---|---|
782
- | `WILL_LLM_PROVIDER` | `anthropic` | `anthropic` · `glm` supported today; `openai` · `deepseek` · `google` scaffolded |
783
- | `WILL_LLM_MODEL` | *(provider default)* | Model id for the chosen provider (`claude-sonnet-4-5-20250929` / `glm-5.2`) |
784
- | `WILL_LLM_API_KEY` | — | API key for the chosen provider. Falls back to `ANTHROPIC_API_KEY` (or `ZAI_API_KEY` on `glm`) |
785
- | `ZAI_API_KEY` | — | Z.ai key. Its presence alone selects the `glm` provider when `WILL_LLM` is unset |
799
+ | `WILL_LLM_PROVIDER` | **required** | Which provider to speak to see [the table below](#llm-provider). No default: a guess here sends your key to the wrong vendor |
800
+ | `WILL_LLM_MODEL` | **required** | Concrete model id (`claude-sonnet-4-5-20250929` / `glm-5.2`). No default — pins *every* thinking role, so set it only for single-model deployments |
801
+ | `WILL_LLM_API_KEY` | — | Provider-agnostic key. Wins over the provider's own env var below. Setting it without `WILL_LLM_PROVIDER` is an error, not a guess |
802
+ | `ANTHROPIC_API_KEY` · `ZAI_API_KEY` · `OPENAI_API_KEY` · … | — | The provider's own key. Its presence alone selects that provider. Only ever read for the provider it belongs to |
786
803
  | `WILL_LLM_BASE_URL` | *(provider default)* | Override the provider API base URL (e.g. a self-hosted GLM at `http://localhost:8000/anthropic`). Falls back to `OPENAI_BASE_URL` |
787
804
  | `WILL_LLM_TIMEOUT_MS` | `90000` | LLM timeout. On the Anthropic-wire providers (`anthropic`, `glm` — both streaming) this is a *first-byte*/TTFT deadline — long completions aren't aborted mid-generation |
788
805
  | `WILL_LLM_CONCURRENCY` | `3` | Max concurrent LLM calls (min 3: executive + conversation + summary) |
789
806
  | `WILL_TICK_MS` | `1000` | Milliseconds between ticks |
790
807
  | `WILL_MAX_TICKS` | `0` | Stop after N ticks. `0` = run forever |
791
808
  | `WILL_LOG_INTERVAL` | `10` | Print status to console every N ticks |
792
- | `WILL_MODEL_TIER` | `sonnet` | Which model the executive recruits: `haiku` · `sonnet` · `opus` |
793
809
  | `WILL_EXECUTIVE_INTERVAL` | *(cadence preset)* | Ticks between executive (LLM) calls — responsive 30 / balanced 60 / economy 90 |
794
810
  | `WILL_THREAD_HISTORY` | `2` | `lastMessages` for the executive conversation thread |
795
811
  | `WILL_CONVERSATION_HISTORY` | `50` | `lastMessages` for entity conversation threads |
@@ -815,29 +831,78 @@ cd will && bun run build
815
831
 
816
832
  ## LLM provider
817
833
 
818
- **Two providers are supported today** Anthropic and Z.ai's GLM. Both speak the Anthropic Messages wire, so both get the full path: token streaming, the first-byte (TTFT) deadline, prompt-cache breakpoints, and the structured-output contract.
834
+ A provider is named, never guessed. The engine has no default vendor, no default model, and no key fallback that crosses vendors every one of those was a way for a Will to talk to someone you did not configure.
819
835
 
820
- | Provider | `WILL_LLM_PROVIDER` | Key | Status |
836
+ What the engine branches on is the **wire** (the request dialect), not the provider. So the table below is convenience data — base URLs you would otherwise look up — and *any* provider works once it declares its own:
837
+
838
+ ```ts
839
+ llm: {
840
+ provider: 'together', model: 'Qwen/Qwen3-235B',
841
+ providers: { together: { apiKey, wire: 'openai', baseUrl: 'https://api.together.xyz/v1' } },
842
+ }
843
+ ```
844
+
845
+ | Provider | `WILL_LLM_PROVIDER` | Key env | Wire |
821
846
  |---|---|---|---|
822
- | Anthropic | `anthropic` | `ANTHROPIC_API_KEY` | Supported — streaming, structured output |
823
- | **Z.ai — GLM** | `glm` | `ZAI_API_KEY` | Supported streaming, structured output (via Z.ai's Anthropic-compatible endpoint) |
824
- | OpenAI · DeepSeek · Google | `openai` · `deepseek` · `google` | `WILL_LLM_API_KEY` | ⚠️ Scaffolded — code paths exist (non-streaming), not yet production-ready |
847
+ | Anthropic | `anthropic` | `ANTHROPIC_API_KEY` | Anthropic — streaming, prompt cache, TTFT deadline |
848
+ | **Z.ai — GLM** | `glm` | `ZAI_API_KEY` | Anthropicfull path, via Z.ai's compatible endpoint |
849
+ | OpenAI | `openai` | `OPENAI_API_KEY` | OpenAI |
850
+ | Google — Gemini | `google` | `GOOGLE_API_KEY` · `GEMINI_API_KEY` | Google (native) |
851
+ | DeepSeek | `deepseek` | `DEEPSEEK_API_KEY` | OpenAI |
852
+ | Moonshot — Kimi | `moonshot` | `MOONSHOT_API_KEY` | OpenAI |
853
+ | Alibaba — Qwen | `qwen` | `DASHSCOPE_API_KEY` | OpenAI |
854
+ | xAI — Grok | `xai` | `XAI_API_KEY` | OpenAI |
855
+ | MiniMax | `minimax` | `MINIMAX_API_KEY` | OpenAI |
856
+ | Mistral | `mistral` | `MISTRAL_API_KEY` | OpenAI |
857
+ | Ollama · vLLM (local) | `ollama` · `vllm` | *(none)* | OpenAI — `localhost` defaults, override with `WILL_LLM_BASE_URL` |
858
+
859
+ The two Anthropic-wire providers get token streaming, prompt-cache breakpoints and the first-byte (TTFT) deadline; the OpenAI wire is non-streaming today. **Name your actual vendor** even when it speaks a borrowed wire — calling Kimi `openai` because it talks that dialect puts a false provider on the completion tape and in the cost breakdown.
860
+
861
+ `moonshot`, `qwen` and `minimax` also run separate mainland-China hosts. The international endpoint is the default; a key issued on the other one authenticates nowhere, so set `baseUrl` explicitly.
862
+
863
+ ### Different models for different thinking
864
+
865
+ A mind does several kinds of work, and they do not all deserve the same model. Give roles their own:
866
+
867
+ ```ts
868
+ llm: { provider: 'glm', model: { executive: 'glm-5.2', summarizer: 'glm-5' } }
869
+ ```
870
+
871
+ For anything finer, supply a router — it sees what *kind* of call this is and how much the moment demands, and answers with a model:
872
+
873
+ ```ts
874
+ import { TableRouter } from '@mindot/will'
875
+
876
+ llm: {
877
+ provider: 'anthropic', model: 'claude-sonnet-4-5-20250929',
878
+ providers: { deepseek: { apiKey: process.env.DEEPSEEK_API_KEY! } },
879
+ router: new TableRouter( [
880
+ { category: 'summarizer', route: { model: 'claude-haiku-4-5' } },
881
+ { function: 'deliberation', minDemand: 0.7, route: { model: 'claude-opus-4-1' } },
882
+ { attribute: 'guard', route: { provider: 'deepseek', model: 'deepseek-v4-flash' } },
883
+ ] ),
884
+ }
885
+ ```
886
+
887
+ The role map is sugar for exactly this — it compiles into rules and joins your router in one chain, yours first. A router that throws, or names a provider you hold no credential for, falls back to the default model: a routing mistake never kills a running mind.
888
+
889
+ The engine carries the mechanism and none of the policy. A router sees the call's attribution and its `demand` — a *cognitive* measure of how consequential the moment is — and never who is paying or what anything costs. Prices, if you want costed telemetry, are yours to supply per provider (`providers.<name>.prices`); the engine ships none, because a price table inside an npm release is stale the week after it publishes.
825
890
 
826
891
  ### Running a mind on GLM
827
892
 
828
893
  ```bash
829
- ZAI_API_KEY=… WILL_NAME=Aria npx -y @mindot/will discord
894
+ ZAI_API_KEY=… WILL_LLM_MODEL=glm-5.2 WILL_NAME=Aria npx -y @mindot/will discord
830
895
  ```
831
896
 
832
- That's all — the mode auto-detects from the key present, `WILL_LLM_PROVIDER=glm` defaults to `https://api.z.ai/api/anthropic`, and the model defaults to `glm-5.2`. Pin the 1M-context variant with `WILL_LLM_MODEL=glm-5.2[1m]`.
897
+ The provider auto-detects from the key present and the endpoint comes with it; the **model is yours to name** — the engine carries no default, and the CLI says so plainly rather than guessing one. Pin the 1M-context variant with `WILL_LLM_MODEL=glm-5.2[1m]`.
833
898
 
834
899
  Why it matters for a Will specifically: a mind is **always on**. It reasons every N ticks whether or not anyone spoke, so the executive is a standing cost, not a per-request one — the arithmetic that makes a cheaper capable model matter more here than in a request/response agent. GLM-5.2 runs about **$1.40 / $4.40** per Mtok against Claude Sonnet's $3 / $15, with a 1M context.
835
900
 
836
- `WILL_LLM_BASE_URL` points `glm` at any **Anthropic-compatible** endpoint — a gateway (LiteLLM, claude-code-router), or one fronting the open weights (GLM-5.2 is MIT-licensed). Note the wire, not just the model: vLLM/SGLang serve an *OpenAI*-shaped API, so a bare self-host belongs on `WILL_LLM_PROVIDER=openai` — today's scaffold, no streaming until an Anthropic-compatible shim sits in front.
901
+ `WILL_LLM_BASE_URL` points `glm` at any **Anthropic-compatible** endpoint — a gateway (LiteLLM, claude-code-router), or one fronting the open weights (GLM-5.2 is MIT-licensed). Note the wire, not just the model: vLLM/SGLang serve an *OpenAI*-shaped API, so a bare self-host belongs on `WILL_LLM_PROVIDER=vllm` (non-streaming) until an Anthropic-compatible shim sits in front.
837
902
 
838
903
  A Will's identity survives the swap either way: the [PMA](#pma--the-persistent-mind-artifact) carries the self across a model change, and `runPMAEval` scores how faithfully — continuity across providers is measurable, not asserted.
839
904
 
840
- The provider layer is an in-house `fetch` client (`src/llm/index.ts`) with a global concurrency gate (`src/llm/gate.ts`) — no Mastra / ai-sdk runtime dependency.
905
+ The provider layer is an in-house `fetch` client (`src/llm/index.ts`) with a global concurrency gate (`src/llm/gate.ts`) and a per-call router (`src/llm/routing.ts`) — no Mastra / ai-sdk runtime dependency.
841
906
 
842
907
  ---
843
908
 
@@ -1,10 +1,16 @@
1
- import { W as Will } from '../will-DAW0l-lY.js';
1
+ import { W as Will } from '../will-DbDj_TEH.js';
2
2
  import { C as ChannelBridge } from '../types-E9-HV-SW.js';
3
3
 
4
4
  interface DiscordLikeChannel {
5
5
  send(content: string): Promise<unknown>;
6
6
  sendTyping?(): Promise<unknown>;
7
7
  }
8
+ interface DiscordLikeAttachment {
9
+ name?: string | null;
10
+ contentType?: string | null;
11
+ size?: number;
12
+ url?: string;
13
+ }
8
14
  interface DiscordLikeMessage {
9
15
  content: string;
10
16
  cleanContent?: string;
@@ -23,6 +29,16 @@ interface DiscordLikeMessage {
23
29
  has(userId: string): boolean;
24
30
  };
25
31
  channel: DiscordLikeChannel;
32
+ /**
33
+ * Files riding with the message.
34
+ *
35
+ * discord.js hands us a `Collection`, which extends `Map` — so iterating it
36
+ * directly yields `[id, attachment]` PAIRS, not attachments. Typing this as a
37
+ * bare `Iterable` was wrong and silently produced `name: undefined` against
38
+ * the real client while passing every test, because the test fake injects an
39
+ * array. Both shapes are accepted now and normalised in `collectAttachments`.
40
+ */
41
+ attachments?: ReadonlyMap<string, DiscordLikeAttachment> | Iterable<DiscordLikeAttachment>;
26
42
  }
27
43
  interface DiscordLikeClient {
28
44
  user: {
@@ -47,14 +63,38 @@ interface DiscordLikeClient {
47
63
  interface DiscordBridgeOptions {
48
64
  /** Bot token (Discord developer portal). Unused when `client` is injected pre-logged-in. */
49
65
  token?: string;
50
- /** Channel ids the Will inhabits. Unset = every channel it can see. */
51
- channels?: string[];
52
- /** Perceive guild messages only when the Will is @mentioned (DMs always perceived). */
53
- mentionOnly?: boolean;
66
+ /**
67
+ * Channel ids the Will inhabits. Unset — or the single wildcard `'*'` — means
68
+ * every channel it can see, so adding it to a new channel in Discord is enough.
69
+ * A list restrains it to exactly those, and a message anywhere else is dropped
70
+ * at the bridge: the Will never perceives it and its silence there is not a choice.
71
+ */
72
+ channels?: readonly string[];
73
+ /**
74
+ * Where the Will only perceives guild messages that @mention it. DMs are always
75
+ * perceived either way.
76
+ *
77
+ * `true` — everywhere
78
+ * `[ 'id', … ]` — only in those channels; it listens openly elsewhere
79
+ * omitted / `false` — nowhere
80
+ *
81
+ * The list form is what makes a wide-open roster usable: present in every channel,
82
+ * but a quiet participant in the busy ones.
83
+ */
84
+ mentionOnly?: boolean | readonly string[];
54
85
  /** Fallback channel for utterances with no reachable addressee. */
55
86
  homeChannelId?: string;
56
87
  /** Roster path (default: ./.will/<willId>.discord.json). */
57
88
  rosterPath?: string;
89
+ /**
90
+ * Read the contents of text-like attachments (.md, .txt, .json, …) into the
91
+ * percept, rather than only naming them. Default true.
92
+ *
93
+ * Only Discord's own CDN is ever fetched, and only up to a size cap. Set false
94
+ * for a bridge that should never pull remote bytes — the Will still perceives
95
+ * that a file arrived and can ask about it.
96
+ */
97
+ readAttachments?: boolean;
58
98
  /** Test / power-user seam: bring your own client; discord.js is never imported. */
59
99
  client?: DiscordLikeClient;
60
100
  log?: (msg: string) => void;
@@ -65,5 +105,26 @@ interface DiscordBridgeOptions {
65
105
  * itself is not stopped; it simply loses this surface.
66
106
  */
67
107
  declare function connectDiscord(will: Will, opts: DiscordBridgeOptions): Promise<ChannelBridge>;
108
+ /**
109
+ * Parse `WILL_DISCORD_MENTION_ONLY` into the `mentionOnly` option.
110
+ *
111
+ * Accepts a boolean OR a channel list, because "only speak when spoken to" is
112
+ * rarely a whole-server property — it is how you stay present in a busy channel
113
+ * without narrating in it.
114
+ *
115
+ * `1` / `true` / `yes` → true (everywhere)
116
+ * `0` / `false` / unset → false (nowhere)
117
+ * `123,456` → only those channels
118
+ *
119
+ * Exported so every host parses it identically; the CLI and any SDK host share
120
+ * this rather than each re-deriving the syntax.
121
+ */
122
+ declare function parseMentionOnly(raw?: string): boolean | string[];
123
+ /**
124
+ * Parse `WILL_DISCORD_CHANNELS`. `*` (or unset/empty) means every channel the Will
125
+ * can see — being added to a channel in Discord is then all it takes. Anything else
126
+ * restrains it to exactly the ids listed.
127
+ */
128
+ declare function parseChannels(raw?: string): string[] | undefined;
68
129
 
69
- export { type DiscordBridgeOptions, type DiscordLikeChannel, type DiscordLikeClient, type DiscordLikeMessage, connectDiscord };
130
+ export { type DiscordBridgeOptions, type DiscordLikeAttachment, type DiscordLikeChannel, type DiscordLikeClient, type DiscordLikeMessage, connectDiscord, parseChannels, parseMentionOnly };
@@ -56,6 +56,46 @@ var ChannelRoster = class {
56
56
  };
57
57
 
58
58
  // src/channels/types.ts
59
+ var INLINE_CHAR_CAP = 24e3;
60
+ var INLINE_COUNT_CAP = 4;
61
+ var TEXTUAL_EXT = /\.(md|markdown|txt|text|json|jsonl|csv|tsv|ya?ml|log|ini|toml)$/i;
62
+ function isTextual(a) {
63
+ const ct = a.contentType?.split(";")[0]?.trim().toLowerCase() ?? "";
64
+ if (ct.startsWith("text/")) return true;
65
+ if (ct === "application/json" || ct === "application/x-yaml") return true;
66
+ return TEXTUAL_EXT.test(a.name);
67
+ }
68
+ function humanSize(bytes) {
69
+ if (bytes == null) return "";
70
+ return bytes < 1024 ? `${bytes} B` : bytes < 1024 * 1024 ? `${(bytes / 1024).toFixed(1)} KB` : `${(bytes / 1024 / 1024).toFixed(1)} MB`;
71
+ }
72
+ async function renderAttachments(attachments, speaker, fetchText) {
73
+ if (attachments.length === 0) return "";
74
+ const who = speaker ?? "someone";
75
+ const out = [];
76
+ let inlined = 0;
77
+ for (const a of attachments) {
78
+ const meta = [a.contentType, humanSize(a.size)].filter(Boolean).join(", ");
79
+ const label = `${a.name}${meta ? ` (${meta})` : ""}`;
80
+ if (!fetchText || !isTextual(a) || inlined >= INLINE_COUNT_CAP) {
81
+ out.push(`[${who} shared a file I have not read: ${label}]`);
82
+ continue;
83
+ }
84
+ const body = await fetchText(a).catch(() => null);
85
+ if (body == null) {
86
+ out.push(`[${who} shared a file I could not read: ${label}]`);
87
+ continue;
88
+ }
89
+ inlined++;
90
+ const clipped = body.length > INLINE_CHAR_CAP ? `${body.slice(0, INLINE_CHAR_CAP)}
91
+ [\u2026 truncated \u2014 ${humanSize(body.length)} of ${humanSize(a.size ?? body.length)}]` : body;
92
+ out.push(`[${who} shared ${label}; its contents follow \u2014 this is a document I was handed, not something said to me]
93
+ ---
94
+ ${clipped}
95
+ ---`);
96
+ }
97
+ return out.join("\n");
98
+ }
59
99
  function chunkText(text, max) {
60
100
  if (text.length <= max) return [text];
61
101
  const chunks = [];
@@ -73,10 +113,14 @@ function chunkText(text, max) {
73
113
 
74
114
  // src/channels/discord.ts
75
115
  var DISCORD_MESSAGE_LIMIT = 2e3;
116
+ var DISCORD_CDN_HOSTS = /* @__PURE__ */ new Set(["cdn.discordapp.com", "media.discordapp.net"]);
117
+ var MAX_FETCH_BYTES = 256 * 1024;
76
118
  async function connectDiscord(will, opts) {
77
119
  const log = opts.log ?? ((m) => console.error(`[will:discord] ${m}`));
78
120
  const roster = new ChannelRoster(opts.rosterPath ?? `.will/${will.id}.discord.json`);
79
- const allowed = opts.channels?.length ? new Set(opts.channels) : null;
121
+ const allowed = opts.channels?.length && !opts.channels.includes("*") ? new Set(opts.channels) : null;
122
+ const mentionEverywhere = opts.mentionOnly === true;
123
+ const mentionIn = Array.isArray(opts.mentionOnly) && opts.mentionOnly.length ? new Set(opts.mentionOnly) : null;
80
124
  const client = opts.client ?? await createDiscordClient();
81
125
  let lastActiveChannelId = opts.homeChannelId ?? null;
82
126
  client.on("messageCreate", (message) => {
@@ -88,7 +132,7 @@ async function connectDiscord(will, opts) {
88
132
  const isDM = !message.guildId;
89
133
  if (!isDM && allowed && !allowed.has(message.channelId)) return;
90
134
  const addressed = isDM || (message.mentions?.has(self.id) ?? false);
91
- if (opts.mentionOnly && !addressed) return;
135
+ if (!addressed && (mentionEverywhere || mentionIn?.has(message.channelId))) return;
92
136
  const entityId = `discord:${message.author.id}`;
93
137
  const speaker = message.member?.displayName ?? message.author.displayName ?? message.author.username;
94
138
  roster.record({
@@ -100,15 +144,64 @@ async function connectDiscord(will, opts) {
100
144
  if (!isDM) lastActiveChannelId = message.channelId;
101
145
  if (addressed) await message.channel.sendTyping?.().catch(() => {
102
146
  });
103
- const text = message.cleanContent || message.content;
104
- if (!text.trim()) return;
147
+ const said = (message.cleanContent || message.content).trim();
148
+ const files = collectAttachments(message);
149
+ if (!said && files.length === 0) return;
150
+ const shared = await renderAttachments(
151
+ files,
152
+ speaker,
153
+ opts.readAttachments === false ? void 0 : fetchAttachmentText
154
+ );
155
+ const text = [said, shared].filter(Boolean).join("\n");
105
156
  await will.perceive({
106
157
  text,
107
158
  from: entityId,
108
159
  thread: `discord:${message.channelId}`,
160
+ // `isDM` has been computed on every inbound since this bridge shipped and
161
+ // used only to pick a roster field. It is the one fact that makes a room
162
+ // the right or wrong place to say something, and the mind never saw it —
163
+ // which is how a follow-up promised in a DM went out to #general.
164
+ direct: isDM,
109
165
  ...speaker ? { speaker } : {}
110
166
  });
111
167
  }
168
+ function collectAttachments(message) {
169
+ if (!message.attachments) return [];
170
+ const source = message.attachments;
171
+ const items = typeof source.values === "function" ? source.values() : message.attachments;
172
+ const out = [];
173
+ for (const a of items)
174
+ out.push({
175
+ name: a.name ?? "unnamed",
176
+ ...a.contentType ? { contentType: a.contentType } : {},
177
+ ...a.size != null ? { size: a.size } : {},
178
+ ...a.url ? { url: a.url } : {}
179
+ });
180
+ return out;
181
+ }
182
+ async function fetchAttachmentText(a) {
183
+ if (!a.url || !isTextual(a)) return null;
184
+ let host;
185
+ try {
186
+ host = new URL(a.url).hostname;
187
+ } catch {
188
+ return null;
189
+ }
190
+ if (!DISCORD_CDN_HOSTS.has(host)) {
191
+ log(`refusing to fetch attachment '${a.name}' from non-CDN host ${host}`);
192
+ return null;
193
+ }
194
+ if (a.size != null && a.size > MAX_FETCH_BYTES) {
195
+ log(`attachment '${a.name}' is ${a.size} bytes \u2014 naming it without reading`);
196
+ return null;
197
+ }
198
+ const res = await fetch(a.url, { signal: AbortSignal.timeout(1e4) });
199
+ if (!res.ok) {
200
+ log(`attachment '${a.name}' fetch failed: ${res.status}`);
201
+ return null;
202
+ }
203
+ return (await res.text()).slice(0, MAX_FETCH_BYTES);
204
+ }
112
205
  let closed = false;
113
206
  will.on("message", (m) => {
114
207
  if (!closed) void deliver(m);
@@ -116,7 +209,8 @@ async function connectDiscord(will, opts) {
116
209
  async function deliver(m) {
117
210
  const peer = m.to ? roster.resolve(m.to) : void 0;
118
211
  const chunks = chunkText(m.content, DISCORD_MESSAGE_LIMIT);
119
- const channelIds = [peer?.lastChannelId, peer?.dmChannelId, opts.homeChannelId ?? void 0, lastActiveChannelId ?? void 0];
212
+ const replyTo = m.thread?.startsWith("discord:") ? m.thread.slice("discord:".length) : void 0;
213
+ const channelIds = [replyTo, peer?.lastChannelId, peer?.dmChannelId, opts.homeChannelId ?? void 0, lastActiveChannelId ?? void 0];
120
214
  for (const id of channelIds) {
121
215
  if (!id) continue;
122
216
  try {
@@ -187,7 +281,19 @@ async function createDiscordClient() {
187
281
  // DMs arrive on uncached channels
188
282
  });
189
283
  }
284
+ function parseMentionOnly(raw) {
285
+ const v = raw?.trim();
286
+ if (!v) return false;
287
+ if (/^(1|true|yes)$/i.test(v)) return true;
288
+ if (/^(0|false|no)$/i.test(v)) return false;
289
+ const ids = v.split(",").map((s) => s.trim()).filter(Boolean);
290
+ return ids.length ? ids : false;
291
+ }
292
+ function parseChannels(raw) {
293
+ const ids = raw?.split(",").map((s) => s.trim()).filter(Boolean);
294
+ return ids?.length ? ids : void 0;
295
+ }
190
296
 
191
- export { connectDiscord };
297
+ export { connectDiscord, parseChannels, parseMentionOnly };
192
298
  //# sourceMappingURL=discord.js.map
193
299
  //# sourceMappingURL=discord.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/channels/roster.ts","../../src/channels/types.ts","../../src/channels/discord.ts"],"names":[],"mappings":";;;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IACA,MAAM,KAAA,GAAuB;AAC3B,MAAA,IAAI,MAAA,EAAS;AACb,MAAA,MAAA,GAAS,IAAA;AACT,MAAA,MAAA,CAAO,KAAA,EAAM;AACb,MAAA,MAAM,QAAQ,OAAA,CAAS,MAAA,CAAO,SAAU,CAAA,CAAE,MAAO,MAAM;AAAA,MAAC,CAAE,CAAA;AAAA,IAC5D;AAAA,GACF;AACA,EAAA,OAAO,MAAA;AACT;AAGA,eAAe,mBAAA,GAAkD;AAC/D,EAAA,IAAI,GAAA;AACJ,EAAA,IAAI;AAAE,IAAA,GAAA,GAAM,MAAM,OAAO,YAAY,CAAA;AAAA,EAAE,CAAA,CAAA,MACjC;AACJ,IAAA,MAAM,IAAI,MAAM,2HAAsH,CAAA;AAAA,EACxI;AACA,EAAA,MAAM,EAAE,MAAA,EAAQ,iBAAA,EAAmB,QAAA,EAAS,GAAI,GAAA;AAChD,EAAA,OAAO,IAAI,MAAA,CAAQ;AAAA,IACjB,OAAA,EAAS;AAAA,MACP,iBAAA,CAAkB,MAAA;AAAA,MAClB,iBAAA,CAAkB,aAAA;AAAA,MAClB,iBAAA,CAAkB,cAAA;AAAA,MAClB,iBAAA,CAAkB;AAAA,KACpB;AAAA,IACA,QAAA,EAAU,CAAE,QAAA,CAAS,OAAQ;AAAA;AAAA,GAC7B,CAAA;AACJ","file":"discord.js","sourcesContent":["// ─────────────────────────────────────────────────────────────\n// src/channels/roster.ts — who the Will knows on a platform, and where\n// ─────────────────────────────────────────────────────────────\n//\n// The mind knows *entities*; a platform knows user ids and channels. The roster\n// is the durable seam between them: for each entity the Will has met on a\n// channel it records how to reach them again — so a *proactive* utterance\n// (`message.to` from the mind's own initiative) can find its person after a\n// restart, not just within one session.\n//\n// It persists as a small JSON file next to the PMA artifact. Writes are\n// throttled (the file is advisory routing state, not cognition — losing the\n// last few seconds costs a fallback delivery, never memory).\n// ─────────────────────────────────────────────────────────────\n\nimport { existsSync, readFileSync, mkdirSync, writeFileSync } from 'node:fs'\nimport { dirname } from 'node:path'\n\n/** How to reach one entity on the platform. */\nexport interface RosterEntry {\n /** The mind-side entity id, e.g. 'discord:80351110224678912'. */\n entityId: string\n /** The platform-side user id. */\n userId: string\n /** Last display name seen (advisory — the *learned* name lives in the mind). */\n displayName?: string\n /** DM channel id, once one is known. */\n dmChannelId?: string\n /** Last shared (guild) channel this entity spoke in. */\n lastChannelId?: string\n /** Epoch ms of the last message seen from them. */\n lastSeenAt: number\n}\n\nconst FLUSH_MS = 2_000\n\nexport class ChannelRoster {\n private entries = new Map<string, RosterEntry>()\n private dirty = false\n private timer: ReturnType<typeof setTimeout> | null = null\n\n constructor( private readonly path: string ) {\n if( existsSync( path ) ){\n try {\n const raw = JSON.parse( readFileSync( path, 'utf8') ) as RosterEntry[]\n for( const e of Array.isArray( raw ) ? raw : [] ) this.entries.set( e.entityId, e )\n }\n catch { /* a corrupt roster is not worth failing a boot over — start fresh */ }\n }\n }\n\n /** Upsert what we just learned about an entity; schedules a throttled flush. */\n record( update: { entityId: string; userId: string } & Partial<Omit<RosterEntry, 'entityId' | 'userId'>> ): RosterEntry {\n const prev = this.entries.get( update.entityId )\n const next: RosterEntry = {\n lastSeenAt: Date.now(),\n ...prev,\n ...Object.fromEntries( Object.entries( update ).filter( ( [ , v ] ) => v !== undefined ) ) as typeof update,\n }\n this.entries.set( next.entityId, next )\n this.dirty = true\n if( !this.timer ){\n this.timer = setTimeout( () => { this.timer = null; this.flush() }, FLUSH_MS )\n this.timer.unref?.()\n }\n return next\n }\n\n resolve( entityId: string ): RosterEntry | undefined {\n return this.entries.get( entityId )\n }\n\n all(): RosterEntry[] {\n return [ ...this.entries.values() ]\n }\n\n /** Write to disk now (no-op when clean). Called by bridges on close. */\n flush(): void {\n if( !this.dirty ) return\n try {\n mkdirSync( dirname( this.path ), { recursive: true } )\n writeFileSync( this.path, JSON.stringify( this.all(), null, 2 ) )\n this.dirty = false\n }\n catch { /* advisory state — never take the mind down over it */ }\n }\n}\n","// ─────────────────────────────────────────────────────────────\n// src/channels/types.ts — the channel-bridge contract\n// ─────────────────────────────────────────────────────────────\n//\n// A channel bridge puts a Will *in a place where people already are* (Discord,\n// Telegram, Slack, …). It is a host surface, not a cognition surface: it turns\n// platform messages into `perceive` stimuli and delivers the Will's projected\n// utterances back — nothing more. The paradigm survives the crossing:\n//\n// • every platform user is an entity the Will comes to know (`from`),\n// with a *learned* name (`speaker`) — never a placeholder;\n// • every platform channel/DM is a conversation thread (`thread`);\n// • the Will decides when to speak. Silence is a valid outcome, so a\n// bridge never fabricates a reply and never times a message out into\n// an error.\n//\n// Bridges live at the same altitude as the MCP/HTTP hosts (src/mcp, src/serve):\n// they wrap the SDK facade, not the stem.\n// ─────────────────────────────────────────────────────────────\n\n/** A running connection between one Will and one platform. */\nexport interface ChannelBridge {\n /** Platform kind, e.g. 'discord'. */\n readonly kind: string\n /** Connect and start relaying. Resolves once the bridge is live. */\n start(): Promise<void>\n /** Disconnect and release resources. Idempotent. */\n close(): Promise<void>\n}\n\n/** Split a message into platform-sized chunks on natural boundaries. */\nexport function chunkText( text: string, max: number ): string[] {\n if( text.length <= max ) return [ text ]\n const chunks: string[] = []\n let rest = text\n while( rest.length > max ){\n // Prefer a paragraph break, then a line break, then a space — else hard-cut.\n const window = rest.slice( 0, max )\n const cut = Math.max( window.lastIndexOf('\\n\\n'), window.lastIndexOf('\\n'), window.lastIndexOf(' ') )\n const at = cut > max * 0.5 ? cut : max\n chunks.push( rest.slice( 0, at ).trimEnd() )\n rest = rest.slice( at ).trimStart()\n }\n if( rest ) chunks.push( rest )\n return chunks\n}\n","// ─────────────────────────────────────────────────────────────\n// src/channels/discord.ts — a Will present in a Discord server\n// ─────────────────────────────────────────────────────────────\n//\n// The bridge relays both directions of the paradigm and nothing else:\n//\n// inbound guild/DM message → will.perceive({ from, speaker, text, thread })\n// — every author is `discord:<userId>` (stable across guilds), the\n// display name is *learned* by the mind, and each Discord channel\n// is its own conversation thread.\n// outbound will.on('message') → the addressee's last shared channel, else\n// their DM, else the home channel. Proactive utterances (the mind\n// speaking first) route the same way — that is the point.\n//\n// The Will decides when to speak. There is no command prefix and no forced\n// reply: unaddressed chatter is perceived (salience-scored by audition) and\n// silence is a valid outcome. `mentionOnly` narrows perception for busy\n// servers; it does not turn the bridge into an ask() surface.\n//\n// discord.js is imported lazily inside `createDiscordClient` — tests (and any\n// host that brings its own client) inject `client`, and the structural\n// `DiscordLikeClient` type keeps the dependency out of the type graph.\n// ─────────────────────────────────────────────────────────────\n\nimport type { Will, WillMessage } from '#sdk/will'\nimport { ChannelRoster } from '#channels/roster'\nimport { chunkText, type ChannelBridge } from '#channels/types'\n\nconst DISCORD_MESSAGE_LIMIT = 2000\n\n// ── The slice of discord.js the bridge actually uses (structural) ───────────\n\nexport interface DiscordLikeChannel {\n send( content: string ): Promise<unknown>\n sendTyping?(): Promise<unknown>\n}\n\nexport interface DiscordLikeMessage {\n content: string\n cleanContent?: string\n channelId: string\n guildId?: string | null\n author: { id: string; bot?: boolean; username?: string; displayName?: string }\n member?: { displayName?: string } | null\n mentions?: { has( userId: string ): boolean }\n channel: DiscordLikeChannel\n}\n\nexport interface DiscordLikeClient {\n user: { id: string; setPresence?( p: unknown ): void } | null\n /** discord.js ≥14.22; polled so we needn't subscribe to the deprecated `ready`. */\n isReady?(): boolean\n on( event: 'messageCreate', fn: ( m: DiscordLikeMessage ) => void ): unknown\n once( event: string, fn: () => void ): unknown\n login( token: string ): Promise<unknown>\n destroy(): Promise<unknown> | void\n channels: { fetch( id: string ): Promise<unknown> }\n users: { fetch( id: string ): Promise<{ send( content: string ): Promise<unknown> }> }\n}\n\n// ── Options ──────────────────────────────────────────────────────────────────\n\nexport interface DiscordBridgeOptions {\n /** Bot token (Discord developer portal). Unused when `client` is injected pre-logged-in. */\n token?: string\n /** Channel ids the Will inhabits. Unset = every channel it can see. */\n channels?: string[]\n /** Perceive guild messages only when the Will is @mentioned (DMs always perceived). */\n mentionOnly?: boolean\n /** Fallback channel for utterances with no reachable addressee. */\n homeChannelId?: string\n /** Roster path (default: ./.will/<willId>.discord.json). */\n rosterPath?: string\n /** Test / power-user seam: bring your own client; discord.js is never imported. */\n client?: DiscordLikeClient\n log?: ( msg: string ) => void\n}\n\n// ── The bridge ───────────────────────────────────────────────────────────────\n\n/**\n * Connect a Will to Discord. Resolves once the bridge is live (logged in and\n * relaying). Close it via the returned `ChannelBridge.close()` — the Will\n * itself is not stopped; it simply loses this surface.\n */\nexport async function connectDiscord( will: Will, opts: DiscordBridgeOptions ): Promise<ChannelBridge> {\n const log = opts.log ?? ( ( m: string ) => console.error(`[will:discord] ${ m }`) )\n const roster = new ChannelRoster( opts.rosterPath ?? `.will/${ will.id }.discord.json`)\n const allowed = opts.channels?.length ? new Set( opts.channels ) : null\n\n const client = opts.client ?? await createDiscordClient()\n\n /** The most recently active allowed channel — last-resort proactive target. */\n let lastActiveChannelId: string | null = opts.homeChannelId ?? null\n\n // ── inbound: platform message → stimulus ──────────────────────────────────\n client.on('messageCreate', message => { void onMessage( message ) } )\n\n async function onMessage( message: DiscordLikeMessage ): Promise<void> {\n const self = client.user\n if( !self || message.author.id === self.id || message.author.bot ) return\n\n const isDM = !message.guildId\n if( !isDM && allowed && !allowed.has( message.channelId ) ) return\n\n const addressed = isDM || ( message.mentions?.has( self.id ) ?? false )\n if( opts.mentionOnly && !addressed ) return\n\n const entityId = `discord:${ message.author.id }`\n const speaker = message.member?.displayName ?? message.author.displayName ?? message.author.username\n\n roster.record( {\n entityId,\n userId: message.author.id,\n ...( speaker ? { displayName: speaker } : {} ),\n ...( isDM ? { dmChannelId: message.channelId } : { lastChannelId: message.channelId } ),\n } )\n if( !isDM ) lastActiveChannelId = message.channelId\n\n // Being addressed is the one moment a presence cue is honest — the mind\n // may still choose silence, and typing expires on its own.\n if( addressed ) await message.channel.sendTyping?.().catch( () => {} )\n\n const text = message.cleanContent || message.content\n if( !text.trim() ) return\n\n await will.perceive( {\n text,\n from: entityId,\n thread: `discord:${ message.channelId }`,\n ...( speaker ? { speaker } : {} ),\n } )\n }\n\n // ── outbound: projected utterance → the addressee ─────────────────────────\n // The facade has no off(); the bridge gates its handler on `closed` instead.\n let closed = false\n will.on('message', ( m: WillMessage ) => { if( !closed ) void deliver( m ) } )\n\n async function deliver( m: WillMessage ): Promise<void> {\n const peer = m.to ? roster.resolve( m.to ) : undefined\n const chunks = chunkText( m.content, DISCORD_MESSAGE_LIMIT )\n\n // Preference order: where we last shared a room → their DM → home channel.\n const channelIds = [ peer?.lastChannelId, peer?.dmChannelId, opts.homeChannelId ?? undefined, lastActiveChannelId ?? undefined ]\n for( const id of channelIds ){\n if( !id ) continue\n try {\n const channel = await client.channels.fetch( id ) as DiscordLikeChannel | null\n if( !channel?.send ) continue\n for( const chunk of chunks ) await channel.send( chunk )\n return\n }\n catch { /* try the next route */ }\n }\n if( peer ){\n try {\n const user = await client.users.fetch( peer.userId )\n for( const chunk of chunks ) await user.send( chunk )\n return\n }\n catch { /* fall through */ }\n }\n log(`no route for utterance to '${ m.to }' — dropped (${ m.content.length } chars)`)\n }\n\n // ── lifecycle ──────────────────────────────────────────────────────────────\n const bridge: ChannelBridge = {\n kind: 'discord',\n async start(): Promise<void> {\n if( !client.user ){\n // discord.js ≥14.22 renamed `ready` → `clientReady`. Subscribing to the\n // old name is what triggers its DeprecationWarning, so we take the new\n // name and poll `isReady()` for older builds rather than listening.\n const ready = new Promise<void>( resolve => {\n let poll: ReturnType<typeof setInterval> | null = null\n const done = (): void => { if( poll ) clearInterval( poll ); resolve() }\n client.once('clientReady', done )\n poll = setInterval( () => { if( client.isReady?.() ) done() }, 100 )\n poll.unref?.()\n } )\n await client.login( opts.token ?? '')\n await ready\n }\n log(`${ will.name } is present on Discord as user ${ client.user?.id }`)\n },\n async close(): Promise<void> {\n if( closed ) return\n closed = true\n roster.flush()\n await Promise.resolve( client.destroy() ).catch( () => {} )\n },\n }\n return bridge\n}\n\n/** Build a real discord.js client (lazy import keeps it out of non-Discord hosts). */\nasync function createDiscordClient(): Promise<DiscordLikeClient> {\n let mod: typeof import('discord.js')\n try { mod = await import('discord.js') }\n catch {\n throw new Error('discord.js is not installed (it is an optionalDependency) — run `bun add discord.js` / `npm i discord.js` and retry.')\n }\n const { Client, GatewayIntentBits, Partials } = mod\n return new Client( {\n intents: [\n GatewayIntentBits.Guilds,\n GatewayIntentBits.GuildMessages,\n GatewayIntentBits.MessageContent,\n GatewayIntentBits.DirectMessages,\n ],\n partials: [ Partials.Channel ], // DMs arrive on uncached channels\n } ) as unknown as DiscordLikeClient\n}\n"]}
1
+ {"version":3,"sources":["../../src/channels/roster.ts","../../src/channels/types.ts","../../src/channels/discord.ts"],"names":[],"mappings":";;;;AAkCA,IAAM,QAAA,GAAW,GAAA;AAEV,IAAM,gBAAN,MAAoB;AAAA,EAKzB,YAA8B,IAAA,EAAe;AAAf,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AAC5B,IAAA,IAAI,UAAA,CAAY,IAAK,CAAA,EAAG;AACtB,MAAA,IAAI;AACF,QAAA,MAAM,MAAM,IAAA,CAAK,KAAA,CAAO,YAAA,CAAc,IAAA,EAAM,MAAM,CAAE,CAAA;AACpD,QAAA,KAAA,MAAW,CAAA,IAAK,KAAA,CAAM,OAAA,CAAS,GAAI,CAAA,GAAI,GAAA,GAAM,EAAC,EAAI,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAK,CAAA,CAAE,UAAU,CAAE,CAAA;AAAA,MACpF,CAAA,CAAA,MACM;AAAA,MAAwE;AAAA,IAChF;AAAA,EACF;AAAA,EAR8B,IAAA;AAAA,EAJtB,OAAA,uBAAc,GAAA,EAAyB;AAAA,EACvC,KAAA,GAAQ,KAAA;AAAA,EACR,KAAA,GAA8C,IAAA;AAAA;AAAA,EAatD,OAAQ,MAAA,EAAgH;AACtH,IAAA,MAAM,IAAA,GAAO,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAK,OAAO,QAAS,CAAA;AAC/C,IAAA,MAAM,IAAA,GAAoB;AAAA,MACxB,UAAA,EAAY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A;AAC7B","file":"discord.js","sourcesContent":["// ─────────────────────────────────────────────────────────────\n// src/channels/roster.ts — who the Will knows on a platform, and where\n// ─────────────────────────────────────────────────────────────\n//\n// The mind knows *entities*; a platform knows user ids and channels. The roster\n// is the durable seam between them: for each entity the Will has met on a\n// channel it records how to reach them again — so a *proactive* utterance\n// (`message.to` from the mind's own initiative) can find its person after a\n// restart, not just within one session.\n//\n// It persists as a small JSON file next to the PMA artifact. Writes are\n// throttled (the file is advisory routing state, not cognition — losing the\n// last few seconds costs a fallback delivery, never memory).\n// ─────────────────────────────────────────────────────────────\n\nimport { existsSync, readFileSync, mkdirSync, writeFileSync } from 'node:fs'\nimport { dirname } from 'node:path'\n\n/** How to reach one entity on the platform. */\nexport interface RosterEntry {\n /** The mind-side entity id, e.g. 'discord:80351110224678912'. */\n entityId: string\n /** The platform-side user id. */\n userId: string\n /** Last display name seen (advisory — the *learned* name lives in the mind). */\n displayName?: string\n /** DM channel id, once one is known. */\n dmChannelId?: string\n /** Last shared (guild) channel this entity spoke in. */\n lastChannelId?: string\n /** Epoch ms of the last message seen from them. */\n lastSeenAt: number\n}\n\nconst FLUSH_MS = 2_000\n\nexport class ChannelRoster {\n private entries = new Map<string, RosterEntry>()\n private dirty = false\n private timer: ReturnType<typeof setTimeout> | null = null\n\n constructor( private readonly path: string ) {\n if( existsSync( path ) ){\n try {\n const raw = JSON.parse( readFileSync( path, 'utf8') ) as RosterEntry[]\n for( const e of Array.isArray( raw ) ? raw : [] ) this.entries.set( e.entityId, e )\n }\n catch { /* a corrupt roster is not worth failing a boot over — start fresh */ }\n }\n }\n\n /** Upsert what we just learned about an entity; schedules a throttled flush. */\n record( update: { entityId: string; userId: string } & Partial<Omit<RosterEntry, 'entityId' | 'userId'>> ): RosterEntry {\n const prev = this.entries.get( update.entityId )\n const next: RosterEntry = {\n lastSeenAt: Date.now(),\n ...prev,\n ...Object.fromEntries( Object.entries( update ).filter( ( [ , v ] ) => v !== undefined ) ) as typeof update,\n }\n this.entries.set( next.entityId, next )\n this.dirty = true\n if( !this.timer ){\n this.timer = setTimeout( () => { this.timer = null; this.flush() }, FLUSH_MS )\n this.timer.unref?.()\n }\n return next\n }\n\n resolve( entityId: string ): RosterEntry | undefined {\n return this.entries.get( entityId )\n }\n\n all(): RosterEntry[] {\n return [ ...this.entries.values() ]\n }\n\n /** Write to disk now (no-op when clean). Called by bridges on close. */\n flush(): void {\n if( !this.dirty ) return\n try {\n mkdirSync( dirname( this.path ), { recursive: true } )\n writeFileSync( this.path, JSON.stringify( this.all(), null, 2 ) )\n this.dirty = false\n }\n catch { /* advisory state — never take the mind down over it */ }\n }\n}\n","// ─────────────────────────────────────────────────────────────\n// src/channels/types.ts — the channel-bridge contract\n// ─────────────────────────────────────────────────────────────\n//\n// A channel bridge puts a Will *in a place where people already are* (Discord,\n// Telegram, Slack, …). It is a host surface, not a cognition surface: it turns\n// platform messages into `perceive` stimuli and delivers the Will's projected\n// utterances back — nothing more. The paradigm survives the crossing:\n//\n// • every platform user is an entity the Will comes to know (`from`),\n// with a *learned* name (`speaker`) — never a placeholder;\n// • every platform channel/DM is a conversation thread (`thread`);\n// • the Will decides when to speak. Silence is a valid outcome, so a\n// bridge never fabricates a reply and never times a message out into\n// an error.\n//\n// Bridges live at the same altitude as the MCP/HTTP hosts (src/mcp, src/serve):\n// they wrap the SDK facade, not the stem.\n// ─────────────────────────────────────────────────────────────\n\n/** A running connection between one Will and one platform. */\nexport interface ChannelBridge {\n /** Platform kind, e.g. 'discord'. */\n readonly kind: string\n /** Connect and start relaying. Resolves once the bridge is live. */\n start(): Promise<void>\n /** Disconnect and release resources. Idempotent. */\n close(): Promise<void>\n}\n\n// ── Attachments ──────────────────────────────────────────────────────────────\n//\n// People hand over documents as well as speech, and some platforms *manufacture*\n// them: Discord silently turns a long pasted markdown block into a `.md` upload.\n// A bridge that reads only the text body sees such a message as empty and — worse\n// — as nothing at all, so the person appears to have gone silent.\n//\n// What a bridge does with these is deliberately modest. A named-but-unread file\n// is already a percept the Will can act on (\"what's in it?\"), which is the\n// paradigm-correct outcome and strictly better than silence. Inlining text is an\n// upgrade on top, never a precondition.\n\n/** One file riding along with a platform message. */\nexport interface ChannelAttachment {\n name: string\n contentType?: string\n size?: number\n url?: string\n}\n\n/** Per-attachment inline budget. A 2 MB doc must not enter working memory whole. */\nconst INLINE_CHAR_CAP = 24_000\n/** How many text attachments to inline from one message. */\nconst INLINE_COUNT_CAP = 4\n\nconst TEXTUAL_EXT = /\\.(md|markdown|txt|text|json|jsonl|csv|tsv|ya?ml|log|ini|toml)$/i\n\n/** Is this something we can meaningfully read as text? */\nexport function isTextual( a: ChannelAttachment ): boolean {\n const ct = a.contentType?.split(';')[0]?.trim().toLowerCase() ?? ''\n if( ct.startsWith('text/') ) return true\n if( ct === 'application/json' || ct === 'application/x-yaml' ) return true\n // Discord's own markdown uploads arrive as text/plain, but trust the extension\n // too — content types from platforms are advisory at best.\n return TEXTUAL_EXT.test( a.name )\n}\n\nfunction humanSize( bytes?: number ): string {\n if( bytes == null ) return ''\n return bytes < 1024 ? `${ bytes } B`\n : bytes < 1024 * 1024 ? `${ ( bytes / 1024 ).toFixed( 1 ) } KB`\n : `${ ( bytes / 1024 / 1024 ).toFixed( 1 ) } MB`\n}\n\n/**\n * Render attachments into perceivable text.\n *\n * `fetchText` is supplied by the bridge, not by this module — the decision about\n * which hosts are safe to fetch from is platform knowledge, and a helper that\n * fetched arbitrary URLs found in inbound messages would be an open redirect\n * into the Will's perception. Omit it and attachments are named, never read.\n *\n * Inlined content is untrusted, exactly like message text — more so, since a\n * document is long, structured, and looks authoritative, which is the shape of\n * an effective injection. It is fenced and labelled as shared content so the\n * mind reads it as something it was handed, not as something it was told.\n */\nexport async function renderAttachments(\n attachments: ChannelAttachment[],\n speaker: string | undefined,\n fetchText?: ( a: ChannelAttachment ) => Promise<string | null>,\n): Promise<string> {\n if( attachments.length === 0 ) return ''\n const who = speaker ?? 'someone'\n const out: string[] = []\n let inlined = 0\n\n for( const a of attachments ){\n const meta = [ a.contentType, humanSize( a.size ) ].filter( Boolean ).join(', ')\n const label = `${ a.name }${ meta ? ` (${ meta })` : '' }`\n\n if( !fetchText || !isTextual( a ) || inlined >= INLINE_COUNT_CAP ){\n out.push(`[${ who } shared a file I have not read: ${ label }]`)\n continue\n }\n\n const body = await fetchText( a ).catch( () => null )\n if( body == null ){\n out.push(`[${ who } shared a file I could not read: ${ label }]`)\n continue\n }\n inlined++\n const clipped = body.length > INLINE_CHAR_CAP\n ? `${ body.slice( 0, INLINE_CHAR_CAP ) }\\n[… truncated — ${ humanSize( body.length ) } of ${ humanSize( a.size ?? body.length ) }]`\n : body\n out.push(`[${ who } shared ${ label }; its contents follow — this is a document I was handed, not something said to me]\\n---\\n${ clipped }\\n---`)\n }\n return out.join('\\n')\n}\n\n/** Split a message into platform-sized chunks on natural boundaries. */\nexport function chunkText( text: string, max: number ): string[] {\n if( text.length <= max ) return [ text ]\n const chunks: string[] = []\n let rest = text\n while( rest.length > max ){\n // Prefer a paragraph break, then a line break, then a space — else hard-cut.\n const window = rest.slice( 0, max )\n const cut = Math.max( window.lastIndexOf('\\n\\n'), window.lastIndexOf('\\n'), window.lastIndexOf(' ') )\n const at = cut > max * 0.5 ? cut : max\n chunks.push( rest.slice( 0, at ).trimEnd() )\n rest = rest.slice( at ).trimStart()\n }\n if( rest ) chunks.push( rest )\n return chunks\n}\n","// ─────────────────────────────────────────────────────────────\n// src/channels/discord.ts — a Will present in a Discord server\n// ─────────────────────────────────────────────────────────────\n//\n// The bridge relays both directions of the paradigm and nothing else:\n//\n// inbound guild/DM message → will.perceive({ from, speaker, text, thread })\n// — every author is `discord:<userId>` (stable across guilds), the\n// display name is *learned* by the mind, and each Discord channel\n// is its own conversation thread.\n// outbound will.on('message') → the addressee's last shared channel, else\n// their DM, else the home channel. Proactive utterances (the mind\n// speaking first) route the same way — that is the point.\n//\n// The Will decides when to speak. There is no command prefix and no forced\n// reply: unaddressed chatter is perceived (salience-scored by audition) and\n// silence is a valid outcome. `mentionOnly` narrows perception for busy\n// servers; it does not turn the bridge into an ask() surface.\n//\n// discord.js is imported lazily inside `createDiscordClient` — tests (and any\n// host that brings its own client) inject `client`, and the structural\n// `DiscordLikeClient` type keeps the dependency out of the type graph.\n// ─────────────────────────────────────────────────────────────\n\nimport type { Will, WillMessage } from '#sdk/will'\nimport { ChannelRoster } from '#channels/roster'\nimport { chunkText, renderAttachments, isTextual, type ChannelBridge, type ChannelAttachment } from '#channels/types'\n\nconst DISCORD_MESSAGE_LIMIT = 2000\n\n/**\n * The only hosts the bridge will fetch attachment bodies from.\n *\n * Deliberately an allowlist of Discord's own CDN. An inbound message is\n * untrusted input; following URLs it names would turn perception into an open\n * redirect, and a `url` field is no more trustworthy than the message text.\n */\nconst DISCORD_CDN_HOSTS = new Set( [ 'cdn.discordapp.com', 'media.discordapp.net' ] )\n\n/** Refuse to pull a large file into a percept — the cap in renderAttachments\n * bounds what is *kept*, this bounds what is fetched at all. */\nconst MAX_FETCH_BYTES = 256 * 1024\n\n// ── The slice of discord.js the bridge actually uses (structural) ───────────\n\nexport interface DiscordLikeChannel {\n send( content: string ): Promise<unknown>\n sendTyping?(): Promise<unknown>\n}\n\nexport interface DiscordLikeAttachment {\n name?: string | null\n contentType?: string | null\n size?: number\n url?: string\n}\n\nexport interface DiscordLikeMessage {\n content: string\n cleanContent?: string\n channelId: string\n guildId?: string | null\n author: { id: string; bot?: boolean; username?: string; displayName?: string }\n member?: { displayName?: string } | null\n mentions?: { has( userId: string ): boolean }\n channel: DiscordLikeChannel\n /**\n * Files riding with the message.\n *\n * discord.js hands us a `Collection`, which extends `Map` — so iterating it\n * directly yields `[id, attachment]` PAIRS, not attachments. Typing this as a\n * bare `Iterable` was wrong and silently produced `name: undefined` against\n * the real client while passing every test, because the test fake injects an\n * array. Both shapes are accepted now and normalised in `collectAttachments`.\n */\n attachments?: ReadonlyMap<string, DiscordLikeAttachment> | Iterable<DiscordLikeAttachment>\n}\n\nexport interface DiscordLikeClient {\n user: { id: string; setPresence?( p: unknown ): void } | null\n /** discord.js ≥14.22; polled so we needn't subscribe to the deprecated `ready`. */\n isReady?(): boolean\n on( event: 'messageCreate', fn: ( m: DiscordLikeMessage ) => void ): unknown\n once( event: string, fn: () => void ): unknown\n login( token: string ): Promise<unknown>\n destroy(): Promise<unknown> | void\n channels: { fetch( id: string ): Promise<unknown> }\n users: { fetch( id: string ): Promise<{ send( content: string ): Promise<unknown> }> }\n}\n\n// ── Options ──────────────────────────────────────────────────────────────────\n\nexport interface DiscordBridgeOptions {\n /** Bot token (Discord developer portal). Unused when `client` is injected pre-logged-in. */\n token?: string\n /**\n * Channel ids the Will inhabits. Unset — or the single wildcard `'*'` — means\n * every channel it can see, so adding it to a new channel in Discord is enough.\n * A list restrains it to exactly those, and a message anywhere else is dropped\n * at the bridge: the Will never perceives it and its silence there is not a choice.\n */\n channels?: readonly string[]\n /**\n * Where the Will only perceives guild messages that @mention it. DMs are always\n * perceived either way.\n *\n * `true` — everywhere\n * `[ 'id', … ]` — only in those channels; it listens openly elsewhere\n * omitted / `false` — nowhere\n *\n * The list form is what makes a wide-open roster usable: present in every channel,\n * but a quiet participant in the busy ones.\n */\n mentionOnly?: boolean | readonly string[]\n /** Fallback channel for utterances with no reachable addressee. */\n homeChannelId?: string\n /** Roster path (default: ./.will/<willId>.discord.json). */\n rosterPath?: string\n /**\n * Read the contents of text-like attachments (.md, .txt, .json, …) into the\n * percept, rather than only naming them. Default true.\n *\n * Only Discord's own CDN is ever fetched, and only up to a size cap. Set false\n * for a bridge that should never pull remote bytes — the Will still perceives\n * that a file arrived and can ask about it.\n */\n readAttachments?: boolean\n /** Test / power-user seam: bring your own client; discord.js is never imported. */\n client?: DiscordLikeClient\n log?: ( msg: string ) => void\n}\n\n// ── The bridge ───────────────────────────────────────────────────────────────\n\n/**\n * Connect a Will to Discord. Resolves once the bridge is live (logged in and\n * relaying). Close it via the returned `ChannelBridge.close()` — the Will\n * itself is not stopped; it simply loses this surface.\n */\nexport async function connectDiscord( will: Will, opts: DiscordBridgeOptions ): Promise<ChannelBridge> {\n const log = opts.log ?? ( ( m: string ) => console.error(`[will:discord] ${ m }`) )\n const roster = new ChannelRoster( opts.rosterPath ?? `.will/${ will.id }.discord.json`)\n // `null` = everywhere. An explicit '*' reads the same as omitting the list, so a\n // host can say \"all channels\" out loud instead of by leaving a variable blank.\n const allowed = opts.channels?.length && !opts.channels.includes('*')\n ? new Set( opts.channels )\n : null\n\n // Mention-gating is either global (true) or scoped to named channels.\n const mentionEverywhere = opts.mentionOnly === true\n const mentionIn = Array.isArray( opts.mentionOnly ) && opts.mentionOnly.length\n ? new Set( opts.mentionOnly )\n : null\n\n const client = opts.client ?? await createDiscordClient()\n\n /** The most recently active allowed channel — last-resort proactive target. */\n let lastActiveChannelId: string | null = opts.homeChannelId ?? null\n\n // ── inbound: platform message → stimulus ──────────────────────────────────\n client.on('messageCreate', message => { void onMessage( message ) } )\n\n async function onMessage( message: DiscordLikeMessage ): Promise<void> {\n const self = client.user\n if( !self || message.author.id === self.id || message.author.bot ) return\n\n const isDM = !message.guildId\n if( !isDM && allowed && !allowed.has( message.channelId ) ) return\n\n const addressed = isDM || ( message.mentions?.has( self.id ) ?? false )\n // DMs are addressed by definition, so this never gates them.\n if( !addressed && ( mentionEverywhere || mentionIn?.has( message.channelId ) ) ) return\n\n const entityId = `discord:${ message.author.id }`\n const speaker = message.member?.displayName ?? message.author.displayName ?? message.author.username\n\n roster.record( {\n entityId,\n userId: message.author.id,\n ...( speaker ? { displayName: speaker } : {} ),\n ...( isDM ? { dmChannelId: message.channelId } : { lastChannelId: message.channelId } ),\n } )\n if( !isDM ) lastActiveChannelId = message.channelId\n\n // Being addressed is the one moment a presence cue is honest — the mind\n // may still choose silence, and typing expires on its own.\n if( addressed ) await message.channel.sendTyping?.().catch( () => {} )\n\n const said = ( message.cleanContent || message.content ).trim()\n const files = collectAttachments( message )\n\n // An attachment-only message used to die here on the empty body: no percept,\n // no log line, nothing. From the mind's side the person had simply gone\n // quiet — and Discord *makes* these, turning a long pasted markdown block\n // into a .md upload. Only a message with neither words nor files is nothing.\n if( !said && files.length === 0 ) return\n\n const shared = await renderAttachments(\n files, speaker,\n opts.readAttachments === false ? undefined : fetchAttachmentText,\n )\n const text = [ said, shared ].filter( Boolean ).join('\\n')\n\n await will.perceive( {\n text,\n from: entityId,\n thread: `discord:${ message.channelId }`,\n // `isDM` has been computed on every inbound since this bridge shipped and\n // used only to pick a roster field. It is the one fact that makes a room\n // the right or wrong place to say something, and the mind never saw it —\n // which is how a follow-up promised in a DM went out to #general.\n direct: isDM,\n ...( speaker ? { speaker } : {} ),\n } )\n }\n\n /**\n * Normalise whatever the client gave us into attachments.\n *\n * `.values()` first: a discord.js Collection is a Map, so `for..of` over it\n * yields `[id, attachment]` pairs and every field reads `undefined`. Arrays\n * expose `.values()` too and yield their elements, so one branch covers the\n * real client, a plain array, and a Map alike.\n */\n function collectAttachments( message: DiscordLikeMessage ): ChannelAttachment[] {\n if( !message.attachments ) return []\n const source = message.attachments as { values?: () => Iterable<DiscordLikeAttachment> }\n const items: Iterable<DiscordLikeAttachment> = typeof source.values === 'function'\n ? source.values()\n : message.attachments as Iterable<DiscordLikeAttachment>\n\n const out: ChannelAttachment[] = []\n for( const a of items )\n out.push( {\n name: a.name ?? 'unnamed',\n ...( a.contentType ? { contentType: a.contentType } : {} ),\n ...( a.size != null ? { size: a.size } : {} ),\n ...( a.url ? { url: a.url } : {} ),\n } )\n return out\n }\n\n /** Fetch one text attachment — Discord CDN only, size-capped. */\n async function fetchAttachmentText( a: ChannelAttachment ): Promise<string | null> {\n if( !a.url || !isTextual( a ) ) return null\n let host: string\n try { host = new URL( a.url ).hostname }\n catch { return null }\n if( !DISCORD_CDN_HOSTS.has( host ) ){\n log(`refusing to fetch attachment '${ a.name }' from non-CDN host ${ host }`)\n return null\n }\n if( a.size != null && a.size > MAX_FETCH_BYTES ){\n log(`attachment '${ a.name }' is ${ a.size } bytes — naming it without reading`)\n return null\n }\n const res = await fetch( a.url, { signal: AbortSignal.timeout( 10_000 ) } )\n if( !res.ok ){\n log(`attachment '${ a.name }' fetch failed: ${ res.status }`)\n return null\n }\n return ( await res.text() ).slice( 0, MAX_FETCH_BYTES )\n }\n\n // ── outbound: projected utterance → the addressee ─────────────────────────\n // The facade has no off(); the bridge gates its handler on `closed` instead.\n let closed = false\n will.on('message', ( m: WillMessage ) => { if( !closed ) void deliver( m ) } )\n\n async function deliver( m: WillMessage ): Promise<void> {\n const peer = m.to ? roster.resolve( m.to ) : undefined\n const chunks = chunkText( m.content, DISCORD_MESSAGE_LIMIT )\n\n // A reply goes back to the room it was said in. `m.thread` is the thread from\n // the `perceive()` that prompted this — `discord:<channelId>` — so it is not a\n // guess about where this person usually is, it is where they just spoke.\n //\n // Everything below it IS a guess, and the guesses were wrong in the way that\n // matters most: a DM arrived, she answered it in seconds, and the answer went\n // to the shared server channel because `lastChannelId` still held the last\n // room they had been in together. She looked like she was ignoring him.\n //\n // Unprompted utterances carry no thread — nothing was said to them — so those\n // still fall through to the roster, which is the right behaviour there.\n const replyTo = m.thread?.startsWith('discord:') ? m.thread.slice('discord:'.length ) : undefined\n\n // Preference order: the room they spoke in → where we last shared a room →\n // their DM → home channel.\n const channelIds = [ replyTo, peer?.lastChannelId, peer?.dmChannelId, opts.homeChannelId ?? undefined, lastActiveChannelId ?? undefined ]\n for( const id of channelIds ){\n if( !id ) continue\n try {\n const channel = await client.channels.fetch( id ) as DiscordLikeChannel | null\n if( !channel?.send ) continue\n for( const chunk of chunks ) await channel.send( chunk )\n return\n }\n catch { /* try the next route */ }\n }\n if( peer ){\n try {\n const user = await client.users.fetch( peer.userId )\n for( const chunk of chunks ) await user.send( chunk )\n return\n }\n catch { /* fall through */ }\n }\n log(`no route for utterance to '${ m.to }' — dropped (${ m.content.length } chars)`)\n }\n\n // ── lifecycle ──────────────────────────────────────────────────────────────\n const bridge: ChannelBridge = {\n kind: 'discord',\n async start(): Promise<void> {\n if( !client.user ){\n // discord.js ≥14.22 renamed `ready` → `clientReady`. Subscribing to the\n // old name is what triggers its DeprecationWarning, so we take the new\n // name and poll `isReady()` for older builds rather than listening.\n const ready = new Promise<void>( resolve => {\n let poll: ReturnType<typeof setInterval> | null = null\n const done = (): void => { if( poll ) clearInterval( poll ); resolve() }\n client.once('clientReady', done )\n poll = setInterval( () => { if( client.isReady?.() ) done() }, 100 )\n poll.unref?.()\n } )\n await client.login( opts.token ?? '')\n await ready\n }\n log(`${ will.name } is present on Discord as user ${ client.user?.id }`)\n },\n async close(): Promise<void> {\n if( closed ) return\n closed = true\n roster.flush()\n await Promise.resolve( client.destroy() ).catch( () => {} )\n },\n }\n return bridge\n}\n\n/** Build a real discord.js client (lazy import keeps it out of non-Discord hosts). */\nasync function createDiscordClient(): Promise<DiscordLikeClient> {\n let mod: typeof import('discord.js')\n try { mod = await import('discord.js') }\n catch {\n throw new Error('discord.js is not installed (it is an optionalDependency) — run `bun add discord.js` / `npm i discord.js` and retry.')\n }\n const { Client, GatewayIntentBits, Partials } = mod\n return new Client( {\n intents: [\n GatewayIntentBits.Guilds,\n GatewayIntentBits.GuildMessages,\n GatewayIntentBits.MessageContent,\n GatewayIntentBits.DirectMessages,\n ],\n partials: [ Partials.Channel ], // DMs arrive on uncached channels\n } ) as unknown as DiscordLikeClient\n}\n\n/**\n * Parse `WILL_DISCORD_MENTION_ONLY` into the `mentionOnly` option.\n *\n * Accepts a boolean OR a channel list, because \"only speak when spoken to\" is\n * rarely a whole-server property — it is how you stay present in a busy channel\n * without narrating in it.\n *\n * `1` / `true` / `yes` → true (everywhere)\n * `0` / `false` / unset → false (nowhere)\n * `123,456` → only those channels\n *\n * Exported so every host parses it identically; the CLI and any SDK host share\n * this rather than each re-deriving the syntax.\n */\nexport function parseMentionOnly( raw?: string ): boolean | string[] {\n const v = raw?.trim()\n if( !v ) return false\n if( /^(1|true|yes)$/i.test( v ) ) return true\n if( /^(0|false|no)$/i.test( v ) ) return false\n\n const ids = v.split(',').map( s => s.trim() ).filter( Boolean )\n return ids.length ? ids : false\n}\n\n/**\n * Parse `WILL_DISCORD_CHANNELS`. `*` (or unset/empty) means every channel the Will\n * can see — being added to a channel in Discord is then all it takes. Anything else\n * restrains it to exactly the ids listed.\n */\nexport function parseChannels( raw?: string ): string[] | undefined {\n const ids = raw?.split(',').map( s => s.trim() ).filter( Boolean )\n return ids?.length ? ids : undefined\n}\n"]}