switch-ext-kit 0.0.0-stage → 0.2.1

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/README.md CHANGED
@@ -1,3 +1,215 @@
1
- # Temporary Holding Version
1
+ # switch-ext-kit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The convention for Switch extensions: an extension's tools (MCP tools), events (MCP Events, with an Effect
4
+ Schema and a version), widgets (A2UI, with `McpApp` for custom drawing), its manifest, and serving
5
+ it as MCP on stdio. Also the shim that makes a Switchboard appendage an extension.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ bun add switch-ext-kit effect
11
+ ```
12
+
13
+ ## What an extension is
14
+
15
+ One general capability that makes repeated work efficient: not a category (travel, errands), and
16
+ not a one-off site script (a one-off is done with the general extensions, such as the browser and email).
17
+ How the user likes it used (pay once they've approved) is learned in Switch's brain, never written
18
+ into the extension. It has one of two homes:
19
+
20
+ - **A folder of Switch's `extensions/`**, for an important, general one, with nothing personal in it.
21
+ - **A folder in the user's private extensions monorepo**, for a personal one. Names and businesses may be
22
+ there; secrets never are.
23
+
24
+ ## An extension
25
+
26
+ ```ts
27
+ import { defineApp, serveStdio, tool } from "switch-ext-kit"
28
+ import { Effect, Layer } from "effect"
29
+
30
+ const app = defineApp({
31
+ name: "hello",
32
+ version: "1",
33
+ tools: [tool("hi", { description: "Says hi.", readOnly: true }, () => Effect.succeed("hi"))],
34
+ })
35
+
36
+ await serveStdio(app, Layer.empty)
37
+ ```
38
+
39
+ Its `package.json` carries the manifest:
40
+
41
+ ```json
42
+ "extension": {
43
+ "name": "hello",
44
+ "main": "src/main.ts",
45
+ "needs": ["macos"],
46
+ "heartbeat": 300000,
47
+ "connections": [{ "provider": "google", "scopes": ["…"], "multiple": true }],
48
+ "widgets": ["face"]
49
+ }
50
+ ```
51
+
52
+ `heartbeat` is its promise, in ms: each of its heartbeat events (`<name>.heartbeat`, or
53
+ `<name>.<part>.heartbeat` for a part) comes at least that often while it runs, and three missed is
54
+ trouble the agent is sent to fix. A sense says in its heartbeat what's wrong now (`facts.failing`,
55
+ null when nothing is) and, if it reads a source that moves on without it (messages, mail), how far
56
+ behind it is (`facts.behind_ms`: 0 when its last look read everything there was, else how long
57
+ since one did), beating at once when what's failing changes and when it falls behind or catches up.
58
+ Switch installs no extension without a heartbeat.
59
+
60
+ `template/` is one to copy: a read-only tool, a tool that emits an event, a widget, the manifest
61
+ and the checks (`bun run check`: types, UI copy, lint, format).
62
+
63
+ An extension that isn't TypeScript gives `command` instead of `main` (its `./` paths from the package's
64
+ folder), and `"shim": "switchboard"` if it's a Switchboard appendage.
65
+
66
+ ## Widgets
67
+
68
+ An extension never suggests or makes a space: spaces are the user's. A widget goes on no board until
69
+ the user (or the agent, asked) adds it to a space; until then it's in the space's picker.
70
+
71
+ A widget's `size` says how it sits in a space's grid, in units (one square cell): its `aspect`
72
+ (`square`, `wide`, `tall` or `auto`), a `ratio` a wide or tall one keeps (`16 / 9`), its `min`,
73
+ `preferred` and `max` `{ cols, rows }`, whether it `grows` past preferred, and what it is with
74
+ nothing to show (`empty`: a single `line`, the default, or `hide`).
75
+
76
+ ### Catalogs
77
+
78
+ A widget is A2UI 1.0, drawn by every client in Switch's look, from two catalogs a component can mix
79
+ (a component names its catalog with `catalogId` when it isn't its surface's):
80
+
81
+ - **A2UI's basic catalog** (`BASIC_CATALOG`, `BASIC_PARTS`): `Text` (Markdown; a `#` heading is a
82
+ section's heading; `variant: "caption"` is supporting words), `Image`, `Icon`, `Row`, `Column`,
83
+ `List`, `Card`, `Tabs`, `Modal`, `Divider`, `Button`, `TextField`, `CheckBox`, `ChoicePicker`,
84
+ `Slider`, `DateTimeInput`, `Video`, `AudioPlayer`.
85
+ - **Switch's catalog** (`SWITCH_CATALOG`, `SWITCH_PARTS`, its types in `SwitchParts`): the house
86
+ parts `Line`, `Slot`, `Field`, `Title`, `Row`, `Pick`, `Feed`, `Gallery` (`images: [{ url, description? }]`, a row that scrolls sideways),
87
+ `Check`, `Entry`, `Playing` (what's playing: art, title, who by, back, play/pause and next keys
88
+ calling its tools, a rule running while it plays; paused, one quiet line), `Document` (a
89
+ document its extension reads: a line that opens it, and in its own window the reader, its words,
90
+ lookups, threads and highlights from its tools), and charts that
91
+ take plain data: `BarChart` (grouped or `stacked`, `horizontal`), `LineChart`, `AreaChart`,
92
+ `Sparkline`, `DonutChart`, `ScatterChart`, `Histogram`, `CalendarHeatmap` and `Meter`.
93
+
94
+ ```ts
95
+ { id: "sales", component: "BarChart", labels: ["Mon", "Tue", "Wed"],
96
+ series: [{ name: "Orders", values: [12, 19, 7] }], prefix: "$" }
97
+ ```
98
+
99
+ A property can be bound to the widget's data (its tool's result): `series: { "@path": "/series" }`.
100
+ The data comes again whenever an event its `refresh` names lands: a type, or `*.<name>` for any
101
+ extension's `<extension>.<name>`.
102
+ `McpApp` (`mcpApp`) is for drawing neither catalog can.
103
+
104
+ ## UI copy
105
+
106
+ `ui-captions [--allow <file>] [path…]`, a bin, fails on UI copy that narrates: a caption, hint or
107
+ empty-state line saying what the app does or how it works, first person, a tooltip past one word,
108
+ a placeholder past two, or a dimmed state word or age beside a name. It reads TSX (JSX text, copy
109
+ attributes, the ink from classes), A2UI `text`/`label` properties in TypeScript, and Swift. Its
110
+ allowlist (`ui-captions.allow`: a path, a tab, words from the string) is for real information only,
111
+ such as a permission that's off. An extension runs it in its check, as the template does.
112
+
113
+ ## Its state
114
+
115
+ Its host runs it in a folder of its own, its working directory, and moves that folder with it to
116
+ another machine. Everything it keeps goes there:
117
+ - **Events it emits** wait in an outbox there (`events.db`, `serveStdio`'s) until the core acknowledges
118
+ each one (`ACK`), and go again on the next `events/stream` of their type: a core restart loses
119
+ none. The core keeps an event once however often it comes, by its id; give one an `id` that's stable
120
+ for what happened when emitting it again could happen.
121
+ - **Tool calls made once**: a call whose `_meta` carries `ONCE` (a stable id, as the core gives an
122
+ item's answer) runs once per id, recorded in the same file. The same id again doesn't run it, and
123
+ one cut off as the extension stopped isn't run again, as it may have acted.
124
+ - **Its own records** in its log (below).
125
+ - **Any other file** written whole with `writeFileAtomic` (to a temp file, then renamed), never in
126
+ place.
127
+
128
+ The brain's events an extension follows (`switch/follow`, `{ types, after? }`) come as `switch/event`
129
+ notifications, each with its `seq`; following again `after` the last one handled misses none.
130
+
131
+ ## Its own log
132
+
133
+ `Log.layer({ path, events, projections, commands })` keeps an extension's events on its own SQLite file,
134
+ with read models over them (`defineCommand`, `on`, `Projection`): `dispatch` a command, `read` the
135
+ projections, follow `changes`.
136
+
137
+ ## Models
138
+
139
+ An extension holds no model keys. `Models.layer` asks the host that runs it, over MCP, which answers
140
+ with the user's profile: `decide(input, questions, role?)` picks among choices with
141
+ probabilities, `complete(role, system, prompt)` writes text. The host declares the
142
+ `ax.switch/models` client extension and answers `models/decide` and `models/complete`; without
143
+ one, each call fails with `ModelError`.
144
+
145
+ ## Action items
146
+
147
+ `Asks` (provided by `Models.layer`) puts an item in the user's queue through the host:
148
+ `open({ title, body, kind?, options?, drafts?, attachments?, space?, answerTo? })` and
149
+ `close(id, status)`. The host declares `ax.switch/asks` and answers `asks/open` and
150
+ `asks/close`. `answerTo` names one of the extension's tools (make it `appOnly`), called once the item
151
+ closes with `{ item, status, answer }`.
152
+
153
+ A sealed ask takes a secret without it entering the brain, a chat or a model's context: make a
154
+ one-time key with `sealedKey()` (`switch-ext-kit/sealed`), open the ask with `kind: "sealed"`,
155
+ `sealed: { key, use }` and `answerTo`, and keep `secretKey`. The user's client seals what they type
156
+ to `key` (HPKE, RFC 9180: X25519, HKDF-SHA256, AES-256-GCM, bound to the item's id and `use`); the
157
+ core hands the sealed bytes only to `answerTo`, as `{ item, use, enc, ct }`, and records only that
158
+ it was answered. `openSealed(secretKey, item, use, { enc, ct })` gives the secret back.
159
+
160
+ Switch installs it from a git URL or package name: `extensions_install`, or Extensions in the desktop. A
161
+ monorepo folder is installed at a commit from the core machine's `~/.switch/extensions.json`
162
+ (`{ "source": "github:<owner>/<repo>#<commit>", "path": "<folder>" }`), packed from that folder. Code
163
+ extensions share lives once in the repo, linked from each one's folder: a symlink out of the folder
164
+ packs as what it points to at that commit (Switch's vitals widget, `packages/vitals`), so a change
165
+ reaches each extension as it's pinned, with no release.
166
+
167
+ ## Progress
168
+
169
+ A tool tells its caller how it's going with `Progress` (an Effect reference): `yield* (yield*
170
+ Progress)("Ringing")`, sent as MCP's `notifications/progress` when the call asked with a
171
+ `progressToken`, else nothing.
172
+
173
+ ## The host
174
+
175
+ `Host` (provided by `Models.layer`) is the rest an extension asks of the core: `call(name, args)`
176
+ another extension's tool (named in `reaches.calls`), `accounts(provider)` and `token(provider,
177
+ account)` for a connection it needs, `request` for anything else, and `follow(types, handle)` the
178
+ brain's events (named in `reaches.follows`): each handled once, in order, resuming after the last
179
+ one handled (`follow.json` in its folder) on every new client.
180
+
181
+ ## The agent
182
+
183
+ `Switch` (provided by `Models.layer`) reaches Switch itself, through a host that declares
184
+ `ax.switch/agent`:
185
+
186
+ - `ask({ key, text, space?, note?, title?, id? }, progress?)` asks the agent: a real turn of
187
+ Switch's (its memory, tools and the space's context) in the extension's own side thread `key` in
188
+ `space` (Home when absent), with `note` beside the user's words. What it says comes back
189
+ (`said`, with the turn's `status`), never in the space's chat; the space's own turns see these
190
+ threads and `history_search` finds them. `progress` hears the words as they stream. The same `id`
191
+ again (after a restart) answers from the turn that took it in. The thread is
192
+ `side:<extension>:<space>:<key>`.
193
+ - `steer(thread, text)` puts words into a live session, between its tool calls, or a turn of their
194
+ own: the extension's own side threads, or work its manifest's `reaches.steers` names (`task-*`).
195
+ - `read(query)` reads the brain as `brain_query` does, within its reach: the log's events of its own
196
+ and the types it follows, and its own side threads (`from: "work"`).
197
+
198
+ An event of its own named `<name>.wake` (or `<name>.<part>.wake`), with `{ text, space? }`, always
199
+ wakes a turn in that space (else Home) with `text`, as the senses wake one: not left to triage.
200
+
201
+ ## Ported appendages
202
+
203
+ An extension ported from a Switchboard appendage emits what the shim made of its events:
204
+ `defineRecord("<extension>.<name>", { kind, stream })` is a version 1 type whose payload is the
205
+ record (`kind`, `stream`, `data` and the envelope), and `.record(data, { id, stream, about, refs,
206
+ importance, sender, message, eventId, at })` one occurrence, its id the record's own. Its state stays
207
+ where it was, `home/` in its folder (the shim's `SB_HOME`).
208
+
209
+ ## Session connections
210
+
211
+ A session need declares `session: { signIn: "sign_in", status: "connection_status" }`. Both tools take
212
+ `{ account? }`. `signIn` prepares the exact browser session and returns `{ view: { app, name, input } }`
213
+ for a widget, or `{ url }` for a client link. `status` only reads the real session and returns
214
+ `{ accounts: [{ account }] }`, empty when signed out. Switch verifies it before closing connection asks.
215
+ Never infer a signed-in account from a checkout that permits guests.