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/src/events.ts ADDED
@@ -0,0 +1,81 @@
1
+ // An extension's events: a type, a version and an Effect Schema for the payload. The brain checks
2
+ // each one against its schema on the way in; an extension checks it before it's sent.
3
+ import { Result, Schema } from "effect"
4
+ import type { Cause } from "effect"
5
+
6
+ /** A payload schema that needs no services, so it can be checked synchronously. */
7
+ export type Payload<T> = Schema.Codec<T, unknown>
8
+
9
+ /**
10
+ * An event before it's in the log: its type and payload, and optionally its own id and time. An
11
+ * id the extension gives (stable for one occurrence) is recorded once however often it's sent.
12
+ */
13
+ export interface Draft {
14
+ readonly type: string
15
+ readonly payload: unknown
16
+ readonly id?: string
17
+ /** When it happened, as an ISO instant; else when it's emitted. */
18
+ readonly time?: string
19
+ }
20
+
21
+ /** Checks a payload against its schema and returns its encoded form, or why it failed. */
22
+ export type Check = (payload: unknown) => Result.Result<unknown, string>
23
+
24
+ export const checker = <P>(schema: Payload<P>): Check => {
25
+ const decode = Schema.decodeUnknownResult(schema)
26
+ const encode = Schema.encodeSync(schema)
27
+ return (payload) =>
28
+ Result.mapBoth(decode(payload), { onSuccess: encode, onFailure: (e) => e.message })
29
+ }
30
+
31
+ /** An event type with its payload type erased. */
32
+ export interface EventDef {
33
+ readonly type: string
34
+ readonly version: number
35
+ readonly check: Check
36
+ }
37
+
38
+ /** An event type as `defineEvent` makes it: typed, with its schema, decoder and maker. */
39
+ export interface DefinedEvent<T extends string, P> extends EventDef {
40
+ readonly type: T
41
+ readonly payload: Payload<P>
42
+ readonly decode: (encoded: unknown) => P
43
+ readonly make: (p: P) => Draft
44
+ }
45
+
46
+ export const defineEvent = <const T extends string, P>(
47
+ type: T,
48
+ version: number,
49
+ payload: Payload<P>,
50
+ ): DefinedEvent<T, P> => ({
51
+ type,
52
+ version,
53
+ payload,
54
+ check: checker(payload),
55
+ decode: Schema.decodeUnknownSync(payload),
56
+ make: (p: P): Draft => ({ type, payload: p }),
57
+ })
58
+
59
+ const TaggedRefusal = Schema.TaggedError
60
+
61
+ const RejectedBase: Schema.Class<
62
+ Rejected,
63
+ Schema.TaggedStruct<
64
+ "Rejected",
65
+ {
66
+ readonly reason: Schema.String
67
+ readonly check: Schema.optionalKey<Schema.Boolean>
68
+ }
69
+ >,
70
+ Cause.YieldableError
71
+ > = TaggedRefusal<Rejected>()("Rejected", {
72
+ reason: Schema.String,
73
+ check: Schema.optionalKey(Schema.Boolean),
74
+ })
75
+
76
+ /**
77
+ * Something refused: a bad payload, an unknown type, or a decision that said no. `check`: a check
78
+ * said no to what the caller sent, so the tool worked; its answer is marked `REFUSED`, which no one
79
+ * counts as the extension failing.
80
+ */
81
+ export class Rejected extends RejectedBase {}
package/src/files.ts ADDED
@@ -0,0 +1,22 @@
1
+ // A file an extension keeps beside its log, written whole: to a temp file next to it, flushed, then
2
+ // renamed over it, so a crash leaves the old file or the new one, never half of either.
3
+ import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, rmSync, writeSync } from "node:fs"
4
+ import path from "node:path"
5
+
6
+ /**
7
+ * Writes `data` to `file` in one step: the old contents until the new are whole on disk. `mode` is
8
+ * the new file's from its first byte (0o600 for a secret).
9
+ */
10
+ export const writeFileAtomic = (file: string, data: string, mode = 0o666): void => {
11
+ mkdirSync(path.dirname(file), { recursive: true })
12
+ const temp = `${file}.${process.pid}.tmp`
13
+ rmSync(temp, { force: true })
14
+ const fd = openSync(temp, "w", mode)
15
+ try {
16
+ writeSync(fd, data)
17
+ fsyncSync(fd)
18
+ } finally {
19
+ closeSync(fd)
20
+ }
21
+ renameSync(temp, file)
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,135 @@
1
+ export {
2
+ A2UI_MIME_TYPE,
3
+ ACK,
4
+ answer,
5
+ AppEvents,
6
+ Caller,
7
+ CALLER,
8
+ ONCE,
9
+ REFUSED,
10
+ CONNECTIONS,
11
+ REACHES,
12
+ defineApp,
13
+ Progress,
14
+ EVENTS,
15
+ serve,
16
+ size,
17
+ SUBSCRIPTION,
18
+ tool,
19
+ VERSION,
20
+ widget,
21
+ widgetSetting,
22
+ WIDGET,
23
+ } from "./app.ts"
24
+ export type {
25
+ Answer,
26
+ App,
27
+ Aspect,
28
+ AppEvent,
29
+ AppEventsService,
30
+ AppEventsShape,
31
+ CallerShape,
32
+ Occurrence,
33
+ ProgressShape,
34
+ Reaches,
35
+ Size,
36
+ Subscribed,
37
+ Surface,
38
+ Tool,
39
+ Units,
40
+ Widget,
41
+ WidgetDef,
42
+ WidgetSetting,
43
+ } from "./app.ts"
44
+ export { BASIC_CATALOG, MCP_CATALOG, mcpApp, surface, SWITCH_CATALOG, view } from "./a2ui.ts"
45
+ export { BASIC_PARTS, ICONS, SWITCH_PARTS } from "./catalog.ts"
46
+ export type {
47
+ Action,
48
+ BasicParts,
49
+ Bound,
50
+ CheckRule,
51
+ ChildList,
52
+ Choices,
53
+ DataBinding,
54
+ Day,
55
+ Figures,
56
+ FunctionCall,
57
+ GalleryImage,
58
+ Points,
59
+ Series,
60
+ Slice,
61
+ SwitchComponent,
62
+ SwitchParts,
63
+ } from "./catalog.ts"
64
+ export { checker, defineEvent, Rejected } from "./events.ts"
65
+ export type { Check, DefinedEvent, Draft, EventDef, Payload } from "./events.ts"
66
+ export { defineCommand, Log, on } from "./log.ts"
67
+ export type {
68
+ Command,
69
+ CommandDef,
70
+ Decision as CommandDecision,
71
+ DefinedCommand,
72
+ LogConfig,
73
+ LogService,
74
+ LogShape,
75
+ On,
76
+ Projection,
77
+ Receipt,
78
+ Snapshot,
79
+ StoredEvent,
80
+ } from "./log.ts"
81
+ export { manifest, need, readManifest } from "./manifest.ts"
82
+ export { AGENT, Asks, ASKS, Host, ModelError, Models, MODELS, Peer, Switch } from "./models.ts"
83
+ export type {
84
+ AskAttachment,
85
+ AskDraft,
86
+ AskInput,
87
+ AsksService,
88
+ AsksShape,
89
+ Answer as ModelAnswer,
90
+ Choice,
91
+ Completion,
92
+ Decision,
93
+ Followed,
94
+ HostService,
95
+ HostShape,
96
+ Input,
97
+ ModelsService,
98
+ ModelsShape,
99
+ PeerService,
100
+ PeerShape,
101
+ Question,
102
+ Role,
103
+ Send,
104
+ SwitchAnswer,
105
+ SwitchAsk,
106
+ SwitchService,
107
+ SwitchShape,
108
+ } from "./models.ts"
109
+ export type { Manifest, Need } from "./manifest.ts"
110
+ export { chain, isJson, readOnly, switchboard } from "./shim.ts"
111
+ export type { Filter, Json } from "./shim.ts"
112
+ export { OUTBOX_FILE, serveStdio } from "./stdio.ts"
113
+ export { writeFileAtomic } from "./files.ts"
114
+ export { SIDE, sideOfThread, sideThread } from "./side.ts"
115
+ export { defineRecord, RecordPayload } from "./record.ts"
116
+ export type { Envelope, RecordEvent, RecordShape } from "./record.ts"
117
+ export { BACKED_OUT_MS, BackedOut, Dwelled, left, Switched, uiEvents, Viewed } from "./ui.ts"
118
+ export type { Moved, Seen, Stayed } from "./ui.ts"
119
+ export { isSender, MESSAGE_SCHEMA, messageOf, SENDER_SCHEMA } from "./messages.ts"
120
+ export {
121
+ asked,
122
+ heavy,
123
+ PERSON_ASKED,
124
+ PERSONAL,
125
+ personal,
126
+ personAsked,
127
+ provides,
128
+ refusal,
129
+ simpleCommands,
130
+ standIns,
131
+ } from "./laptop.ts"
132
+ export type { Reader } from "./laptop.ts"
133
+ export type { Message, Received, Sender } from "./messages.ts"
134
+ export { openSealed, seal, SEALED_DOMAIN, sealedInfo, sealedKey } from "./sealed.ts"
135
+ export type { SealedAnswer } from "./sealed.ts"