switch-ext-kit 0.0.0-stage → 0.2.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.
package/README.md CHANGED
@@ -1,3 +1,188 @@
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), and charts that
89
+ take plain data: `BarChart` (grouped or `stacked`, `horizontal`), `LineChart`, `AreaChart`,
90
+ `Sparkline`, `DonutChart`, `ScatterChart`, `Histogram`, `CalendarHeatmap` and `Meter`.
91
+
92
+ ```ts
93
+ { id: "sales", component: "BarChart", labels: ["Mon", "Tue", "Wed"],
94
+ series: [{ name: "Orders", values: [12, 19, 7] }], prefix: "$" }
95
+ ```
96
+
97
+ A property can be bound to the widget's data (its tool's result): `series: { "@path": "/series" }`.
98
+ `McpApp` (`mcpApp`) is for drawing neither catalog can.
99
+
100
+ ## UI copy
101
+
102
+ `ui-captions [--allow <file>] [path…]`, a bin, fails on UI copy that narrates: a caption, hint or
103
+ empty-state line saying what the app does or how it works, first person, a tooltip past one word,
104
+ a placeholder past two, or a dimmed state word or age beside a name. It reads TSX (JSX text, copy
105
+ attributes, the ink from classes), A2UI `text`/`label` properties in TypeScript, and Swift. Its
106
+ allowlist (`ui-captions.allow`: a path, a tab, words from the string) is for real information only,
107
+ such as a permission that's off. An extension runs it in its check, as the template does.
108
+
109
+ ## Its state
110
+
111
+ Its host runs it in a folder of its own, its working directory, and moves that folder with it to
112
+ another machine. Everything it keeps goes there:
113
+ - **Events it emits** wait in an outbox there (`events.db`, `serveStdio`'s) until the core acknowledges
114
+ each one (`ACK`), and go again on the next `events/stream` of their type: a core restart loses
115
+ none. The core keeps an event once however often it comes, by its id; give one an `id` that's stable
116
+ for what happened when emitting it again could happen.
117
+ - **Tool calls made once**: a call whose `_meta` carries `ONCE` (a stable id, as the core gives an
118
+ item's answer) runs once per id, recorded in the same file. The same id again doesn't run it, and
119
+ one cut off as the extension stopped isn't run again, as it may have acted.
120
+ - **Its own records** in its log (below).
121
+ - **Any other file** written whole with `writeFileAtomic` (to a temp file, then renamed), never in
122
+ place.
123
+
124
+ The brain's events an extension follows (`switch/follow`, `{ types, after? }`) come as `switch/event`
125
+ notifications, each with its `seq`; following again `after` the last one handled misses none.
126
+
127
+ ## Its own log
128
+
129
+ `Log.layer({ path, events, projections, commands })` keeps an extension's events on its own SQLite file,
130
+ with read models over them (`defineCommand`, `on`, `Projection`): `dispatch` a command, `read` the
131
+ projections, follow `changes`.
132
+
133
+ ## Models
134
+
135
+ An extension holds no model keys. `Models.layer` asks the host that runs it, over MCP, which answers
136
+ with the user's profile: `decide(input, questions, role?)` picks among choices with
137
+ probabilities, `complete(role, system, prompt)` writes text. The host declares the
138
+ `ax.switch/models` client extension and answers `models/decide` and `models/complete`; without
139
+ one, each call fails with `ModelError`.
140
+
141
+ ## Action items
142
+
143
+ `Asks` (provided by `Models.layer`) puts an item in the user's queue through the host:
144
+ `open({ title, body, kind?, options?, drafts?, attachments?, space?, answerTo? })` and
145
+ `close(id, status)`. The host declares `ax.switch/asks` and answers `asks/open` and
146
+ `asks/close`. `answerTo` names one of the extension's tools (make it `appOnly`), called once the item
147
+ closes with `{ item, status, answer }`.
148
+
149
+ A sealed ask takes a secret without it entering the brain, a chat or a model's context: make a
150
+ one-time key with `sealedKey()` (`switch-ext-kit/sealed`), open the ask with `kind: "sealed"`,
151
+ `sealed: { key, use }` and `answerTo`, and keep `secretKey`. The user's client seals what they type
152
+ to `key` (HPKE, RFC 9180: X25519, HKDF-SHA256, AES-256-GCM, bound to the item's id and `use`); the
153
+ core hands the sealed bytes only to `answerTo`, as `{ item, use, enc, ct }`, and records only that
154
+ it was answered. `openSealed(secretKey, item, use, { enc, ct })` gives the secret back.
155
+
156
+ Switch installs it from a git URL or package name: `extensions_install`, or Extensions in the desktop. A
157
+ monorepo folder is installed at a commit from the core machine's `~/.switch/extensions.json`
158
+ (`{ "source": "github:<owner>/<repo>#<commit>", "path": "<folder>" }`), packed from that folder.
159
+
160
+ ## Progress
161
+
162
+ A tool tells its caller how it's going with `Progress` (an Effect reference): `yield* (yield*
163
+ Progress)("Ringing")`, sent as MCP's `notifications/progress` when the call asked with a
164
+ `progressToken`, else nothing.
165
+
166
+ ## The host
167
+
168
+ `Host` (provided by `Models.layer`) is the rest an extension asks of the core: `call(name, args)`
169
+ another extension's tool (named in `reaches.calls`), `accounts(provider)` and `token(provider,
170
+ account)` for a connection it needs, `request` for anything else, and `follow(types, handle)` the
171
+ brain's events (named in `reaches.follows`): each handled once, in order, resuming after the last
172
+ one handled (`follow.json` in its folder) on every new client.
173
+
174
+ ## Ported appendages
175
+
176
+ An extension ported from a Switchboard appendage emits what the shim made of its events:
177
+ `defineRecord("<extension>.<name>", { kind, stream })` is a version 1 type whose payload is the
178
+ record (`kind`, `stream`, `data` and the envelope), and `.record(data, { id, stream, about, refs,
179
+ importance, sender, message, eventId, at })` one occurrence, its id the record's own. Its state stays
180
+ where it was, `home/` in its folder (the shim's `SB_HOME`).
181
+
182
+ ## Session connections
183
+
184
+ A session need declares `session: { signIn: "sign_in", status: "connection_status" }`. Both tools take
185
+ `{ account? }`. `signIn` prepares the exact browser session and returns `{ view: { app, name, input } }`
186
+ for a widget, or `{ url }` for a client link. `status` only reads the real session and returns
187
+ `{ accounts: [{ account }] }`, empty when signed out. Switch verifies it before closing connection asks.
188
+ Never infer a signed-in account from a checkout that permits guests.