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 +214 -2
- package/bin/ui-captions.ts +559 -0
- package/package.json +55 -4
- package/src/a2ui.ts +63 -0
- package/src/app.ts +852 -0
- package/src/catalog.ts +810 -0
- package/src/events.ts +81 -0
- package/src/files.ts +22 -0
- package/src/index.ts +135 -0
- package/src/laptop.ts +475 -0
- package/src/log.ts +373 -0
- package/src/manifest.ts +113 -0
- package/src/messages.ts +94 -0
- package/src/models.ts +512 -0
- package/src/record.ts +84 -0
- package/src/sealed.ts +177 -0
- package/src/shell.ts +244 -0
- package/src/shim.ts +302 -0
- package/src/side.ts +27 -0
- package/src/stdio.ts +27 -0
- package/src/ui.ts +67 -0
package/src/models.ts
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
// Models for an extension, through the host that runs it: an extension holds no model keys. Its
|
|
2
|
+
// requests go to the client over MCP, under the `ax.switch/models` extension, and the core answers
|
|
3
|
+
// them with the user's own profile: `models/decide` picks among choices, `models/complete` writes
|
|
4
|
+
// text. The same connection carries an extension's action items (`ax.switch/asks`): `asks/open` and
|
|
5
|
+
// `asks/close`, and the rest it asks of the host (`Host`): other extensions' tools, the user's
|
|
6
|
+
// connections, and brain events it follows. And it reaches the agent itself (`Switch`, under
|
|
7
|
+
// `ax.switch/agent`): asks it in a side thread of its own, steers a live session, reads the brain
|
|
8
|
+
// within its reach.
|
|
9
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
10
|
+
import path from "node:path"
|
|
11
|
+
import { Context, Deferred, Effect, Layer, Option, Queue, Schema } from "effect"
|
|
12
|
+
import type { Cause } from "effect"
|
|
13
|
+
|
|
14
|
+
import { writeFileAtomic } from "./files.ts"
|
|
15
|
+
|
|
16
|
+
/** The client capability extension a host declares when it answers model requests. */
|
|
17
|
+
export const MODELS = "ax.switch/models"
|
|
18
|
+
|
|
19
|
+
/** A role in the user's profile, which says what model runs it. */
|
|
20
|
+
export type Role = "turn" | "triage" | "gate" | "decision" | "browse"
|
|
21
|
+
|
|
22
|
+
export interface Choice {
|
|
23
|
+
readonly value: string
|
|
24
|
+
readonly description?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Pick one of `choices`. */
|
|
28
|
+
export interface Question {
|
|
29
|
+
readonly instructions: string
|
|
30
|
+
readonly choices: readonly Choice[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `stable` goes first and is the same across many calls, so the provider can cache it. */
|
|
34
|
+
export interface Input {
|
|
35
|
+
readonly stable: string
|
|
36
|
+
readonly item: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Answer {
|
|
40
|
+
readonly choice: string
|
|
41
|
+
/** Every choice's probability. */
|
|
42
|
+
readonly probabilities: Readonly<Record<string, number>>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Decision {
|
|
46
|
+
readonly answers: Readonly<Record<string, Answer>>
|
|
47
|
+
readonly ms: number
|
|
48
|
+
/** Dollars. */
|
|
49
|
+
readonly cost: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface Completion {
|
|
53
|
+
readonly text: string
|
|
54
|
+
readonly ms: number
|
|
55
|
+
readonly cost: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const answer = Schema.Struct({
|
|
59
|
+
choice: Schema.String,
|
|
60
|
+
probabilities: Schema.Record(Schema.String, Schema.Number),
|
|
61
|
+
})
|
|
62
|
+
const decision = Schema.Struct({
|
|
63
|
+
answers: Schema.Record(Schema.String, answer),
|
|
64
|
+
ms: Schema.Number,
|
|
65
|
+
cost: Schema.Number,
|
|
66
|
+
})
|
|
67
|
+
const completion = Schema.Struct({ text: Schema.String, ms: Schema.Number, cost: Schema.Number })
|
|
68
|
+
|
|
69
|
+
const TaggedFailure = Schema.TaggedError
|
|
70
|
+
|
|
71
|
+
const ModelErrorBase: Schema.Class<
|
|
72
|
+
ModelError,
|
|
73
|
+
Schema.TaggedStruct<"ModelError", { readonly message: Schema.String }>,
|
|
74
|
+
Cause.YieldableError
|
|
75
|
+
> = TaggedFailure<ModelError>()("ModelError", { message: Schema.String })
|
|
76
|
+
|
|
77
|
+
/** No host to ask, or the host's model failed. */
|
|
78
|
+
export class ModelError extends ModelErrorBase {}
|
|
79
|
+
|
|
80
|
+
export interface ModelsShape {
|
|
81
|
+
/** Asks the role's model every question about one input, in one call. */
|
|
82
|
+
readonly decide: (
|
|
83
|
+
input: Input,
|
|
84
|
+
questions: Readonly<Record<string, Question>>,
|
|
85
|
+
role?: Role,
|
|
86
|
+
) => Effect.Effect<Decision, ModelError>
|
|
87
|
+
readonly complete: (
|
|
88
|
+
role: Role,
|
|
89
|
+
system: string,
|
|
90
|
+
prompt: string,
|
|
91
|
+
) => Effect.Effect<Completion, ModelError>
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The `Models` service's identifier, as a requirement. */
|
|
95
|
+
export interface ModelsService {
|
|
96
|
+
readonly Models: unique symbol
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Sends one request to the client. With `progress`, it hears how the request is going and waits as
|
|
101
|
+
* long as that goes on.
|
|
102
|
+
*/
|
|
103
|
+
export type Send = (
|
|
104
|
+
method: string,
|
|
105
|
+
params: Readonly<Record<string, unknown>>,
|
|
106
|
+
progress?: (message: string) => void,
|
|
107
|
+
) => Promise<unknown>
|
|
108
|
+
|
|
109
|
+
/** A brain event an extension follows (`switch/follow`), as the host sends it (`switch/event`). */
|
|
110
|
+
export interface Followed {
|
|
111
|
+
readonly seq: number
|
|
112
|
+
readonly id: string
|
|
113
|
+
readonly type: string
|
|
114
|
+
readonly source: string
|
|
115
|
+
readonly time: number
|
|
116
|
+
readonly payload: unknown
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Where `serve` hands over its connection, once the client has said what it offers. */
|
|
120
|
+
export interface PeerShape {
|
|
121
|
+
/** `null`: the client answers no model requests. Called again on each new client. */
|
|
122
|
+
readonly attach: (send: Send | null) => void
|
|
123
|
+
/** A brain event it follows, as it comes. */
|
|
124
|
+
readonly heard?: (event: unknown) => void
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The `Peer` service's identifier, as a requirement. */
|
|
128
|
+
export interface PeerService {
|
|
129
|
+
readonly Peer: unique symbol
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const Peer: Context.Service<PeerService, PeerShape> = Context.Service<
|
|
133
|
+
PeerService,
|
|
134
|
+
PeerShape
|
|
135
|
+
>("switch-ext-kit/Peer")
|
|
136
|
+
|
|
137
|
+
/** How long a call waits for the client to connect before it gives up. */
|
|
138
|
+
const CONNECT_MS = 30_000
|
|
139
|
+
|
|
140
|
+
const failed = (e: unknown) =>
|
|
141
|
+
new ModelError({ message: e instanceof Error ? e.message : String(e) })
|
|
142
|
+
|
|
143
|
+
const modelsKey: Context.Service<ModelsService, ModelsShape> = Context.Service<
|
|
144
|
+
ModelsService,
|
|
145
|
+
ModelsShape
|
|
146
|
+
>("switch-ext-kit/Models")
|
|
147
|
+
|
|
148
|
+
/** The client capability extension a host declares when it takes an extension's action items. */
|
|
149
|
+
export const ASKS = "ax.switch/asks"
|
|
150
|
+
|
|
151
|
+
/** A draft: words to send in the user's name, editable on the card. */
|
|
152
|
+
export interface AskDraft {
|
|
153
|
+
readonly name: string
|
|
154
|
+
readonly label: string
|
|
155
|
+
readonly text: string
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A file on the card: `blob` is its key in the brain's blob store, or `url` a link. */
|
|
159
|
+
export interface AskAttachment {
|
|
160
|
+
readonly name: string
|
|
161
|
+
readonly type: string
|
|
162
|
+
readonly size?: number
|
|
163
|
+
readonly blob?: string
|
|
164
|
+
readonly url?: string
|
|
165
|
+
/** Beside this option. */
|
|
166
|
+
readonly option?: number
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* An action item an extension puts in the user's queue. `answerTo` names one of its own tools (best
|
|
171
|
+
* `appOnly`) the core calls once it closes: with `{ item, status, answer }`, or for a sealed ask
|
|
172
|
+
* `{ item, use, enc, ct }`, which `openSealed` (switch-ext-kit/sealed) opens with the ask's secret key.
|
|
173
|
+
* A sealed ask's answer reaches only that tool; the brain records only that it was answered.
|
|
174
|
+
*/
|
|
175
|
+
export interface AskInput {
|
|
176
|
+
readonly title: string
|
|
177
|
+
readonly body: string
|
|
178
|
+
readonly kind?: "task" | "question" | "approval" | "sealed"
|
|
179
|
+
/** Only this extension verifies completion; user dismissal remains available. */
|
|
180
|
+
readonly completion?: "owner"
|
|
181
|
+
/** An explicit action, such as retrying the private challenge. */
|
|
182
|
+
readonly action?: {
|
|
183
|
+
readonly label: string
|
|
184
|
+
readonly app: string
|
|
185
|
+
readonly tool: string
|
|
186
|
+
readonly args?: Readonly<Record<string, unknown>>
|
|
187
|
+
}
|
|
188
|
+
/** Its space; Home when absent. */
|
|
189
|
+
readonly space?: string
|
|
190
|
+
readonly options?: readonly string[]
|
|
191
|
+
readonly drafts?: readonly AskDraft[]
|
|
192
|
+
readonly attachments?: readonly AskAttachment[]
|
|
193
|
+
readonly resolveWhen?: string
|
|
194
|
+
readonly notify?: boolean
|
|
195
|
+
readonly answerTo?: string
|
|
196
|
+
/** A sealed ask's one-time key (`sealedKey().key`) and what the secret is for. */
|
|
197
|
+
readonly sealed?: { readonly key: string; readonly use: string }
|
|
198
|
+
/**
|
|
199
|
+
* Approved only with the user's passkey: one of its own tools (best `appOnly`) the core calls with
|
|
200
|
+
* `{ item, step: "options", origin, rp }`, answering `{ mode: "create" | "get", challenge, … }`
|
|
201
|
+
* for the page its link opens, then `{ item, step: "verify", origin, rp, credential, window }`
|
|
202
|
+
* with what the authenticator answered, answering `{ ok, said? }`. It verifies and closes the item.
|
|
203
|
+
*/
|
|
204
|
+
readonly passkey?: string
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface AsksShape {
|
|
208
|
+
/** Opens it, or revises the open one with `id` (an id this extension gave before). */
|
|
209
|
+
readonly open: (ask: AskInput, id?: string) => Effect.Effect<{ readonly id: string }, ModelError>
|
|
210
|
+
readonly close: (id: string, status: "done" | "dismissed") => Effect.Effect<void, ModelError>
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** The `Asks` service's identifier, as a requirement. */
|
|
214
|
+
export interface AsksService {
|
|
215
|
+
readonly Asks: unique symbol
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const asksKey: Context.Service<AsksService, AsksShape> = Context.Service<AsksService, AsksShape>(
|
|
219
|
+
"switch-ext-kit/Asks",
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
const opened = Schema.Struct({ id: Schema.String })
|
|
223
|
+
|
|
224
|
+
/** What the host does for an extension beyond models and asks. */
|
|
225
|
+
export interface HostShape {
|
|
226
|
+
/** Any request to the host, its result decoded. */
|
|
227
|
+
readonly request: <A>(
|
|
228
|
+
method: string,
|
|
229
|
+
params: Readonly<Record<string, unknown>>,
|
|
230
|
+
result: Schema.Codec<A, unknown>,
|
|
231
|
+
) => Effect.Effect<A, ModelError>
|
|
232
|
+
/** Another extension's tool (`switch/call`), one the manifest's `reaches.calls` names. */
|
|
233
|
+
readonly call: (
|
|
234
|
+
name: string,
|
|
235
|
+
args: Readonly<Record<string, unknown>>,
|
|
236
|
+
) => Effect.Effect<Readonly<Record<string, unknown>>, ModelError>
|
|
237
|
+
/** The user's connected accounts for a provider the manifest needs. */
|
|
238
|
+
readonly accounts: (provider: string) => Effect.Effect<readonly string[], ModelError>
|
|
239
|
+
/** A fresh token (or key) for one of them. */
|
|
240
|
+
readonly token: (provider: string, account: string) => Effect.Effect<string, ModelError>
|
|
241
|
+
/**
|
|
242
|
+
* The brain's events of `types` (exact, or a prefix ending in `*`), each handled once, in order:
|
|
243
|
+
* the last one handled is kept in `follow.json` in its folder, and following resumes after it on
|
|
244
|
+
* every new client, so a restart on either side misses none.
|
|
245
|
+
*/
|
|
246
|
+
readonly follow: (
|
|
247
|
+
types: readonly string[],
|
|
248
|
+
handle: (event: Followed) => Effect.Effect<void>,
|
|
249
|
+
) => Effect.Effect<void, ModelError>
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The `Host` service's identifier, as a requirement. */
|
|
253
|
+
export interface HostService {
|
|
254
|
+
readonly Host: unique symbol
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const hostKey: Context.Service<HostService, HostShape> = Context.Service<HostService, HostShape>(
|
|
258
|
+
"switch-ext-kit/Host",
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
const followed = Schema.Struct({
|
|
262
|
+
seq: Schema.Finite,
|
|
263
|
+
id: Schema.String,
|
|
264
|
+
type: Schema.String,
|
|
265
|
+
source: Schema.String,
|
|
266
|
+
time: Schema.Finite,
|
|
267
|
+
payload: Schema.Unknown,
|
|
268
|
+
})
|
|
269
|
+
const decodeFollowed = Schema.decodeUnknownOption(followed)
|
|
270
|
+
const accountList = Schema.Struct({
|
|
271
|
+
accounts: Schema.Array(Schema.Struct({ account: Schema.String })),
|
|
272
|
+
})
|
|
273
|
+
const tokenOf = Schema.Struct({ token: Schema.String })
|
|
274
|
+
const record = Schema.Record(Schema.String, Schema.Unknown)
|
|
275
|
+
const lastHandled = Schema.decodeUnknownOption(
|
|
276
|
+
Schema.fromJsonString(Schema.Struct({ after: Schema.Finite })),
|
|
277
|
+
)
|
|
278
|
+
const FOLLOW_FILE = "follow.json"
|
|
279
|
+
|
|
280
|
+
const matches = (patterns: readonly string[], type: string) =>
|
|
281
|
+
patterns.some((p) => (p.endsWith("*") ? type.startsWith(p.slice(0, -1)) : p === type))
|
|
282
|
+
|
|
283
|
+
/** The client capability extension a host declares when it reaches the agent for extensions. */
|
|
284
|
+
export const AGENT = "ax.switch/agent"
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* An ask of the agent: the user's words (`text`), asked in the extension's side thread `key` (its
|
|
288
|
+
* own name for it: 1 to 64 letters, digits, `_`, `.` or `-`) in `space` (an id or a name; Home
|
|
289
|
+
* when absent). `note` goes beside the words for the agent: what they're about (a document, the
|
|
290
|
+
* passage). `title` names the thread when it opens. `id` makes it once: the same id again (after a
|
|
291
|
+
* restart) answers from the same turn; a fresh one when absent.
|
|
292
|
+
*/
|
|
293
|
+
export interface SwitchAsk {
|
|
294
|
+
readonly key: string
|
|
295
|
+
readonly text: string
|
|
296
|
+
readonly space?: string
|
|
297
|
+
readonly note?: string
|
|
298
|
+
readonly title?: string
|
|
299
|
+
readonly id?: string
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* What the turn that took it in said back (each say, in order), its thread (`side:<app>:<space>:<key>`)
|
|
304
|
+
* and turn, and how it ended: `done`, or `interrupted`, `failed` or `limited` with `result` saying why.
|
|
305
|
+
*/
|
|
306
|
+
export interface SwitchAnswer {
|
|
307
|
+
readonly thread: string
|
|
308
|
+
readonly turn: string | null
|
|
309
|
+
readonly said: readonly string[]
|
|
310
|
+
readonly status: string
|
|
311
|
+
readonly result: string
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export interface SwitchShape {
|
|
315
|
+
/**
|
|
316
|
+
* Asks the agent: a real turn of Switch's, the space's memory and context in it, in a thread the
|
|
317
|
+
* extension owns, never in the space's chat. `progress` hears its words as they stream: the turn's
|
|
318
|
+
* text so far, then what it said.
|
|
319
|
+
*/
|
|
320
|
+
readonly ask: (
|
|
321
|
+
ask: SwitchAsk,
|
|
322
|
+
progress?: (soFar: string) => Effect.Effect<void>,
|
|
323
|
+
) => Effect.Effect<SwitchAnswer, ModelError>
|
|
324
|
+
/**
|
|
325
|
+
* Words into a live session, between its tool calls, or a turn of their own: one of its own side
|
|
326
|
+
* threads, or work its manifest's `reaches.steers` names.
|
|
327
|
+
*/
|
|
328
|
+
readonly steer: (thread: string, text: string) => Effect.Effect<void, ModelError>
|
|
329
|
+
/**
|
|
330
|
+
* The brain, as `brain_query` reads it, within its reach: the log's events of its own and of the
|
|
331
|
+
* types it follows (`from: "events"`), and its own side threads (`from: "work"`).
|
|
332
|
+
*/
|
|
333
|
+
readonly read: (
|
|
334
|
+
query: Readonly<Record<string, unknown>>,
|
|
335
|
+
) => Effect.Effect<readonly Readonly<Record<string, unknown>>[], ModelError>
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** The `Switch` service's identifier, as a requirement. */
|
|
339
|
+
export interface SwitchService {
|
|
340
|
+
readonly Switch: unique symbol
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const switchKey: Context.Service<SwitchService, SwitchShape> = Context.Service<
|
|
344
|
+
SwitchService,
|
|
345
|
+
SwitchShape
|
|
346
|
+
>("switch-ext-kit/Switch")
|
|
347
|
+
|
|
348
|
+
const switchAnswer = Schema.Struct({
|
|
349
|
+
thread: Schema.String,
|
|
350
|
+
turn: Schema.NullOr(Schema.String),
|
|
351
|
+
said: Schema.Array(Schema.String),
|
|
352
|
+
status: Schema.String,
|
|
353
|
+
result: Schema.String,
|
|
354
|
+
})
|
|
355
|
+
const rows = Schema.Struct({ rows: Schema.Array(Schema.Record(Schema.String, Schema.Unknown)) })
|
|
356
|
+
|
|
357
|
+
/** What `Models.layer` provides. */
|
|
358
|
+
type HostServices = ModelsService | AsksService | HostService | PeerService | SwitchService
|
|
359
|
+
|
|
360
|
+
const modelsLayer: Layer.Layer<HostServices> = Layer.effectContext(
|
|
361
|
+
Effect.gen(function* making() {
|
|
362
|
+
const ready = yield* Deferred.make<Send | null>()
|
|
363
|
+
let current: Send | null = null
|
|
364
|
+
const handlers: {
|
|
365
|
+
readonly types: readonly string[]
|
|
366
|
+
readonly handle: (e: Followed) => Effect.Effect<void>
|
|
367
|
+
}[] = []
|
|
368
|
+
const file = path.resolve(FOLLOW_FILE)
|
|
369
|
+
let after = Option.match(
|
|
370
|
+
existsSync(file) ? lastHandled(readFileSync(file, "utf-8")) : Option.none(),
|
|
371
|
+
{ onNone: () => 0, onSome: (x) => x.after },
|
|
372
|
+
)
|
|
373
|
+
const heard = yield* Queue.unbounded<Followed>()
|
|
374
|
+
yield* Effect.forkScoped(
|
|
375
|
+
Effect.forever(
|
|
376
|
+
Effect.gen(function* handling() {
|
|
377
|
+
const e = yield* Queue.take(heard)
|
|
378
|
+
if (e.seq <= after) {
|
|
379
|
+
return
|
|
380
|
+
}
|
|
381
|
+
for (const h of handlers) {
|
|
382
|
+
if (matches(h.types, e.type)) {
|
|
383
|
+
yield* h.handle(e)
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
after = e.seq
|
|
387
|
+
writeFileAtomic(file, JSON.stringify({ after }))
|
|
388
|
+
}),
|
|
389
|
+
),
|
|
390
|
+
)
|
|
391
|
+
const refollow = (send: Send) => {
|
|
392
|
+
const types = [...new Set(handlers.flatMap((h) => h.types))]
|
|
393
|
+
if (types.length > 0) {
|
|
394
|
+
void send("switch/follow", { types, after }).catch(() => {
|
|
395
|
+
/* the next client follows again */
|
|
396
|
+
})
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const peer: PeerShape = {
|
|
400
|
+
attach: (send) => {
|
|
401
|
+
current = send
|
|
402
|
+
Deferred.doneUnsafe(ready, Effect.succeed(send))
|
|
403
|
+
if (send !== null) {
|
|
404
|
+
refollow(send)
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
heard: (raw) => {
|
|
408
|
+
const e = decodeFollowed(raw)
|
|
409
|
+
if (Option.isSome(e)) {
|
|
410
|
+
Queue.offerUnsafe(heard, e.value)
|
|
411
|
+
}
|
|
412
|
+
},
|
|
413
|
+
}
|
|
414
|
+
const ask = <A>(
|
|
415
|
+
method: string,
|
|
416
|
+
params: Readonly<Record<string, unknown>>,
|
|
417
|
+
result: Schema.Codec<A, unknown>,
|
|
418
|
+
progress?: (message: string) => void,
|
|
419
|
+
) =>
|
|
420
|
+
Effect.gen(function* asking() {
|
|
421
|
+
const send = yield* Deferred.await(ready).pipe(
|
|
422
|
+
Effect.timeoutOrElse({
|
|
423
|
+
duration: CONNECT_MS,
|
|
424
|
+
orElse: () => Effect.fail(new ModelError({ message: "No host connected" })),
|
|
425
|
+
}),
|
|
426
|
+
)
|
|
427
|
+
const now = current ?? send
|
|
428
|
+
if (now === null) {
|
|
429
|
+
return yield* Effect.fail(new ModelError({ message: "The host offers none" }))
|
|
430
|
+
}
|
|
431
|
+
const raw = yield* Effect.tryPromise({
|
|
432
|
+
try: async () => await now(method, params, progress),
|
|
433
|
+
catch: failed,
|
|
434
|
+
})
|
|
435
|
+
return yield* Schema.decodeUnknownEffect(result)(raw).pipe(Effect.mapError(failed))
|
|
436
|
+
})
|
|
437
|
+
const models: ModelsShape = {
|
|
438
|
+
decide: (input, questions, role = "decision") =>
|
|
439
|
+
ask("models/decide", { input, questions, role }, decision),
|
|
440
|
+
complete: (role, system, prompt) =>
|
|
441
|
+
ask("models/complete", { role, system, prompt }, completion),
|
|
442
|
+
}
|
|
443
|
+
const asks: AsksShape = {
|
|
444
|
+
open: (input, id) =>
|
|
445
|
+
ask("asks/open", { ask: input, ...(id !== undefined && { id }) }, opened),
|
|
446
|
+
close: (id, status) => Effect.asVoid(ask("asks/close", { id, status }, Schema.Unknown)),
|
|
447
|
+
}
|
|
448
|
+
const host: HostShape = {
|
|
449
|
+
request: ask,
|
|
450
|
+
call: (name, args) => ask("switch/call", { name, arguments: args }, record),
|
|
451
|
+
accounts: (provider) =>
|
|
452
|
+
Effect.map(ask("connections/list", { provider }, accountList), (r) =>
|
|
453
|
+
r.accounts.map((a) => a.account),
|
|
454
|
+
),
|
|
455
|
+
token: (provider, account) =>
|
|
456
|
+
Effect.map(ask("connections/token", { provider, account }, tokenOf), (t) => t.token),
|
|
457
|
+
follow: (types, handle) =>
|
|
458
|
+
Effect.gen(function* following() {
|
|
459
|
+
handlers.push({ types, handle })
|
|
460
|
+
const all = [...new Set(handlers.flatMap((h) => h.types))]
|
|
461
|
+
yield* ask("switch/follow", { types: all, after }, Schema.Unknown)
|
|
462
|
+
}),
|
|
463
|
+
}
|
|
464
|
+
const agent: SwitchShape = {
|
|
465
|
+
ask: (input, progress) =>
|
|
466
|
+
Effect.gen(function* asking() {
|
|
467
|
+
const id = input.id ?? crypto.randomUUID()
|
|
468
|
+
const streamed = yield* Queue.unbounded<string>()
|
|
469
|
+
const telling = progress
|
|
470
|
+
if (telling !== undefined) {
|
|
471
|
+
yield* Effect.forkScoped(
|
|
472
|
+
Effect.forever(Effect.flatMap(Queue.take(streamed), (t) => telling(t))),
|
|
473
|
+
)
|
|
474
|
+
}
|
|
475
|
+
return yield* ask("switch/ask", { ...input, id }, switchAnswer, (message) => {
|
|
476
|
+
Queue.offerUnsafe(streamed, message)
|
|
477
|
+
})
|
|
478
|
+
}).pipe(Effect.scoped),
|
|
479
|
+
steer: (thread, text) =>
|
|
480
|
+
Effect.asVoid(
|
|
481
|
+
ask("switch/steer", { thread, text, id: crypto.randomUUID() }, Schema.Unknown),
|
|
482
|
+
),
|
|
483
|
+
read: (query) => Effect.map(ask("switch/read", query, rows), (r) => r.rows),
|
|
484
|
+
}
|
|
485
|
+
return Context.make(modelsKey, models).pipe(
|
|
486
|
+
Context.add(asksKey, asks),
|
|
487
|
+
Context.add(hostKey, host),
|
|
488
|
+
Context.add(switchKey, agent),
|
|
489
|
+
Context.add(Peer, peer),
|
|
490
|
+
)
|
|
491
|
+
}),
|
|
492
|
+
)
|
|
493
|
+
|
|
494
|
+
/** The agent through the host: provided by `Models.layer`, which `serve` connects. */
|
|
495
|
+
export const Switch: Context.Service<SwitchService, SwitchShape> & {
|
|
496
|
+
readonly layer: typeof modelsLayer
|
|
497
|
+
} = Object.assign(switchKey, { layer: modelsLayer })
|
|
498
|
+
|
|
499
|
+
/** The host's other services (calls, connections, follows): provided by `Models.layer`. */
|
|
500
|
+
export const Host: Context.Service<HostService, HostShape> & {
|
|
501
|
+
readonly layer: typeof modelsLayer
|
|
502
|
+
} = Object.assign(hostKey, { layer: modelsLayer })
|
|
503
|
+
|
|
504
|
+
/** Action items through the host: provided by `Models.layer`, which `serve` connects. */
|
|
505
|
+
export const Asks: Context.Service<AsksService, AsksShape> & {
|
|
506
|
+
readonly layer: typeof modelsLayer
|
|
507
|
+
} = Object.assign(asksKey, { layer: modelsLayer })
|
|
508
|
+
|
|
509
|
+
/** Models through the host: `serve` connects it. */
|
|
510
|
+
export const Models: Context.Service<ModelsService, ModelsShape> & {
|
|
511
|
+
readonly layer: typeof modelsLayer
|
|
512
|
+
} = Object.assign(modelsKey, { layer: modelsLayer })
|
package/src/record.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The events of an extension ported from a Switchboard appendage, as the shim made them: each type
|
|
2
|
+
// version 1, its payload the appendage's record (`kind`, `stream`, its body as `data`, and the
|
|
3
|
+
// envelope's `about`, `refs`, `importance`, `sender` and `message`), its id the record's own when it
|
|
4
|
+
// gives one. A port emits these, so what lands in the brain is what landed before.
|
|
5
|
+
import { Schema } from "effect"
|
|
6
|
+
|
|
7
|
+
import { defineEvent } from "./events.ts"
|
|
8
|
+
import type { DefinedEvent, Draft } from "./events.ts"
|
|
9
|
+
import type { Message, Sender } from "./messages.ts"
|
|
10
|
+
|
|
11
|
+
export const RecordPayload: Schema.Codec<RecordShape, unknown> = Schema.Struct({
|
|
12
|
+
kind: Schema.String,
|
|
13
|
+
stream: Schema.String,
|
|
14
|
+
data: Schema.Unknown,
|
|
15
|
+
about: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
16
|
+
refs: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
17
|
+
importance: Schema.optionalKey(Schema.Finite),
|
|
18
|
+
sender: Schema.optionalKey(Schema.Unknown),
|
|
19
|
+
message: Schema.optionalKey(Schema.Unknown),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
export interface RecordShape {
|
|
23
|
+
readonly kind: string
|
|
24
|
+
readonly stream: string
|
|
25
|
+
readonly data: unknown
|
|
26
|
+
readonly about?: readonly string[]
|
|
27
|
+
readonly refs?: readonly string[]
|
|
28
|
+
readonly importance?: number
|
|
29
|
+
readonly sender?: unknown
|
|
30
|
+
readonly message?: unknown
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What an appendage's `emit` took beside the body. */
|
|
34
|
+
export interface Envelope {
|
|
35
|
+
readonly about?: readonly string[]
|
|
36
|
+
readonly refs?: readonly string[]
|
|
37
|
+
readonly importance?: number
|
|
38
|
+
/** The record's own id: the event's id, so emitting it again is one event. */
|
|
39
|
+
readonly id?: string
|
|
40
|
+
/** Where this occurrence lands, within its type's. */
|
|
41
|
+
readonly stream?: string
|
|
42
|
+
readonly sender?: Sender
|
|
43
|
+
readonly message?: Message
|
|
44
|
+
/** The event's id when the record has none (`event_id`). */
|
|
45
|
+
readonly eventId?: string
|
|
46
|
+
/** When it happened, as an ISO instant. */
|
|
47
|
+
readonly at?: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface RecordEvent<T extends string> extends DefinedEvent<T, RecordShape> {
|
|
51
|
+
/** One occurrence: its body and envelope as the appendage's `emit` took them. */
|
|
52
|
+
readonly record: (data: unknown, envelope?: Envelope) => Draft
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `<extension>.<name>`, landing as `kind` in `stream` (the appendage's `event()` placement; else an
|
|
57
|
+
* `event` in `events/<extension>`).
|
|
58
|
+
*/
|
|
59
|
+
export const defineRecord = <const T extends string>(
|
|
60
|
+
type: T,
|
|
61
|
+
placed: { readonly kind?: string; readonly stream?: string } = {},
|
|
62
|
+
): RecordEvent<T> => {
|
|
63
|
+
const def = defineEvent(type, 1, RecordPayload)
|
|
64
|
+
const extension = type.slice(0, type.indexOf("."))
|
|
65
|
+
return {
|
|
66
|
+
...def,
|
|
67
|
+
record: (data, envelope = {}) => {
|
|
68
|
+
const { id, eventId, at, stream, ...rest } = envelope
|
|
69
|
+
const payload: RecordShape = {
|
|
70
|
+
...rest,
|
|
71
|
+
kind: placed.kind ?? "event",
|
|
72
|
+
stream: stream ?? placed.stream ?? `events/${extension}`,
|
|
73
|
+
data: data ?? null,
|
|
74
|
+
}
|
|
75
|
+
const given = id ?? eventId
|
|
76
|
+
return {
|
|
77
|
+
type,
|
|
78
|
+
payload,
|
|
79
|
+
...(given !== undefined && { id: given }),
|
|
80
|
+
...(at !== undefined && { time: at }),
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
}
|