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
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// No UI copy that narrates the product. Its words are labels, data and a placeholder of a word or
|
|
3
|
+
// two, never a caption, hint, subtitle or empty-state line saying what the app does or how it
|
|
4
|
+
// works ("I find what bears on them…", "Here you can…", "This shows…", "Tap to…"), and no dimmed
|
|
5
|
+
// state word or age beside a name ("quiet 5m", "archived · Nov 12", "open", "new": a row's ring,
|
|
6
|
+
// name and live marks say how it stands).
|
|
7
|
+
//
|
|
8
|
+
// ui-captions [--allow <file>] [--list] [path…] (paths default to .)
|
|
9
|
+
//
|
|
10
|
+
// It reads TypeScript and TSX (JSX text, copy attributes, A2UI-style `text`/`label` properties,
|
|
11
|
+
// the ink each is drawn in from its elements' classes) and Swift (Text, prompts, labels, the
|
|
12
|
+
// foregroundStyle after them). The allowlist (default ./ui-captions.allow) is a line each: a path
|
|
13
|
+
// from here, a tab, words from the string; for real information such as a permission that's off,
|
|
14
|
+
// never for narration. `--list` prints every literal it reads.
|
|
15
|
+
import { readdirSync, readFileSync, existsSync, statSync } from "node:fs"
|
|
16
|
+
import path from "node:path"
|
|
17
|
+
import { parseSync, visitorKeys } from "oxc-parser"
|
|
18
|
+
|
|
19
|
+
interface Copy {
|
|
20
|
+
readonly line: number
|
|
21
|
+
readonly words: string
|
|
22
|
+
/** It reaches the screen. */
|
|
23
|
+
readonly sink: boolean
|
|
24
|
+
/** Drawn in a dimmed ink. */
|
|
25
|
+
readonly dim: boolean
|
|
26
|
+
/** A tooltip (one word) or a placeholder (two); null for neither. */
|
|
27
|
+
readonly most: 1 | 2 | null
|
|
28
|
+
/** An age: the call that makes one, of a row's own time. */
|
|
29
|
+
readonly age: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const SKIP =
|
|
33
|
+
/(?:^|\/)(?:node_modules|dist|build|\.git|tests?|__tests__|fixtures|UITests|Gallery)\/|\.(?:test|spec|d)\.tsx?$|(?:Demo|Samples)\.swift$/u
|
|
34
|
+
const CODE = /\.(?:tsx?|jsx?|mts|swift)$/u
|
|
35
|
+
|
|
36
|
+
const FIRST_PERSON = /\b(?:I|I'll|I'm|I've|I'd|we|We|we'll|We'll|we're|We're|our|Our|me)\b/u
|
|
37
|
+
const EXPLAINS =
|
|
38
|
+
/here you can|\bthis (?:shows|lists|is where|page|view|card|box|closes|opens)|\byou can\b|\blets you\b|\btype (?:a few|anything|here)|one tap|\btap to\b|\bclick to\b|\bpress to\b|will appear|appears? here|\bwhat you (?:said|did|asked)\b|\bsends here\b|\bopens at once\b|\bin your own words\b|\bget started\b|\bto get\b|\bstart by\b/iu
|
|
39
|
+
const SENTENCES = /[.!?] +[A-Z][a-z]+ [a-z]/u
|
|
40
|
+
// How a row stands, said in words: its ring, name and live marks already say it.
|
|
41
|
+
const STATUS = new Set([
|
|
42
|
+
"quiet",
|
|
43
|
+
"open",
|
|
44
|
+
"opened",
|
|
45
|
+
"folded",
|
|
46
|
+
"archived",
|
|
47
|
+
"new",
|
|
48
|
+
"idle",
|
|
49
|
+
"active",
|
|
50
|
+
"inactive",
|
|
51
|
+
"stale",
|
|
52
|
+
"recent",
|
|
53
|
+
"recently",
|
|
54
|
+
"fresh",
|
|
55
|
+
"ago",
|
|
56
|
+
"now",
|
|
57
|
+
"just",
|
|
58
|
+
"since",
|
|
59
|
+
"last",
|
|
60
|
+
"touched",
|
|
61
|
+
"seen",
|
|
62
|
+
"updated",
|
|
63
|
+
])
|
|
64
|
+
// Relative-time helpers, and a row's own times: an age beside a name decorates. Ages of asks,
|
|
65
|
+
// events and surprises are data and stay.
|
|
66
|
+
const AGE_CALL = /^(?:ago|agoOf|ageOf|since|sinceOf|relative\w*|fromNow)$/u
|
|
67
|
+
const ROW_TIME = /\b(?:lastAt|last_at|archivedAt|archived_at|touchedAt|seenAt|updatedAt)\b/u
|
|
68
|
+
|
|
69
|
+
// TSX: the classes that dim an element's text, and those that ink it plainly.
|
|
70
|
+
const DIM_CLASS = /\btext-(?:ink2|ink3|band-ink2|muted\S*)\b|\bopacity-[1-6]0\b/u
|
|
71
|
+
const INK_CLASS = /\btext-(?:ink|band-ink|white|black|red|orange|yellow)\b/u
|
|
72
|
+
// Attributes and properties whose string reaches the screen.
|
|
73
|
+
const SINK_ATTR = new Set([
|
|
74
|
+
"placeholder",
|
|
75
|
+
"title",
|
|
76
|
+
"aria-label",
|
|
77
|
+
"aria-description",
|
|
78
|
+
"aria-placeholder",
|
|
79
|
+
"alt",
|
|
80
|
+
"label",
|
|
81
|
+
])
|
|
82
|
+
/** The most words each may have: a tooltip's one, a placeholder's two. */
|
|
83
|
+
const MOST = new Map<string, 1 | 2>([
|
|
84
|
+
["title", 1],
|
|
85
|
+
["aria-description", 1],
|
|
86
|
+
["placeholder", 2],
|
|
87
|
+
["aria-placeholder", 2],
|
|
88
|
+
["tooltip", 1],
|
|
89
|
+
["help", 1],
|
|
90
|
+
])
|
|
91
|
+
const UI_KEY = new Set([
|
|
92
|
+
"text",
|
|
93
|
+
"label",
|
|
94
|
+
"placeholder",
|
|
95
|
+
"caption",
|
|
96
|
+
"hint",
|
|
97
|
+
"subtitle",
|
|
98
|
+
"empty",
|
|
99
|
+
"tooltip",
|
|
100
|
+
"help",
|
|
101
|
+
])
|
|
102
|
+
// What a literal passes through on its way to an element.
|
|
103
|
+
const THROUGH = new Set([
|
|
104
|
+
"ConditionalExpression",
|
|
105
|
+
"LogicalExpression",
|
|
106
|
+
"BinaryExpression",
|
|
107
|
+
"TemplateLiteral",
|
|
108
|
+
"ParenthesizedExpression",
|
|
109
|
+
"TSAsExpression",
|
|
110
|
+
"TSSatisfiesExpression",
|
|
111
|
+
"TSNonNullExpression",
|
|
112
|
+
"ArrayExpression",
|
|
113
|
+
])
|
|
114
|
+
|
|
115
|
+
// Swift.
|
|
116
|
+
const SWIFT_SINK =
|
|
117
|
+
/\.help\(|foregroundStyle|\bText\(|\bLabel\(|\bButton\(|prompt:|placeholder|accessibility(?:Label|Hint)\(|\.navigationTitle\(|message:|subtitle|caption|\bhint|\btitle:/u
|
|
118
|
+
const SWIFT_DIM = /\.meta\(|foregroundStyle\((?:SIS|Look)\.(?:ink2|ink3|bandInk2)\)|\.secondary\b/u
|
|
119
|
+
/** A literal's most words: a tooltip's (`.help("…")`), a placeholder's (`TextField("…"`, `prompt:`). */
|
|
120
|
+
const swiftMost = (line: string, at: number) => {
|
|
121
|
+
const before = line.slice(0, at)
|
|
122
|
+
if (/\.help\(\s*$/u.test(before)) {
|
|
123
|
+
return 1
|
|
124
|
+
}
|
|
125
|
+
return /(?:TextField|SecureField)\(\s*$|prompt:\s*(?:Text\()?\s*$/u.test(before) ? 2 : null
|
|
126
|
+
}
|
|
127
|
+
const LITERAL = /"(?<words>(?:[^"\\\n]|\\.)*)"/gu
|
|
128
|
+
const WORDY = /[A-Za-z]{2}/u
|
|
129
|
+
|
|
130
|
+
/** Its fixed words (placeholders, interpolations and separators aside) all state words. */
|
|
131
|
+
const status = (words: string) => {
|
|
132
|
+
const fixed = words
|
|
133
|
+
.replaceAll(/\{[^}]*\}|\\\([^)]*\)|[^A-Za-z ]/gu, " ")
|
|
134
|
+
.toLowerCase()
|
|
135
|
+
.split(/\s+/u)
|
|
136
|
+
.filter(Boolean)
|
|
137
|
+
return fixed.length > 0 && fixed.every((w) => STATUS.has(w))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const why = (c: Copy) => {
|
|
141
|
+
if (c.age) {
|
|
142
|
+
return c.dim && c.sink ? "a dimmed age beside a name decorates" : null
|
|
143
|
+
}
|
|
144
|
+
if (c.dim && c.sink && status(c.words)) {
|
|
145
|
+
return "a dimmed state word decorates"
|
|
146
|
+
}
|
|
147
|
+
if (!c.words.includes(" ")) {
|
|
148
|
+
return null
|
|
149
|
+
}
|
|
150
|
+
const count = c.words.trim().split(/\s+/u).length
|
|
151
|
+
if (c.most === 1 && count > 1) {
|
|
152
|
+
return "a tooltip says more than a control's one-word name"
|
|
153
|
+
}
|
|
154
|
+
if (c.most === 2 && count > 2) {
|
|
155
|
+
return "a placeholder says more than a word or two"
|
|
156
|
+
}
|
|
157
|
+
const fixed = c.words
|
|
158
|
+
.replaceAll(/\{[^}]*\}|\\\([^)]*\)|[^A-Za-z' ]/gu, " ")
|
|
159
|
+
.split(/\s+/u)
|
|
160
|
+
.filter(Boolean)
|
|
161
|
+
if (c.dim && c.sink && fixed.length >= 3) {
|
|
162
|
+
return "a dimmed caption decorates"
|
|
163
|
+
}
|
|
164
|
+
if (FIRST_PERSON.test(c.words)) {
|
|
165
|
+
return "speaks as the app (first person)"
|
|
166
|
+
}
|
|
167
|
+
if (EXPLAINS.test(c.words)) {
|
|
168
|
+
return "explains the interface"
|
|
169
|
+
}
|
|
170
|
+
if (c.sink && (SENTENCES.test(c.words) || c.words.length > 90)) {
|
|
171
|
+
return "runs to more than a sentence"
|
|
172
|
+
}
|
|
173
|
+
return null
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// TypeScript, through oxc's ESTree.
|
|
177
|
+
interface Node {
|
|
178
|
+
readonly type: string
|
|
179
|
+
readonly start: number
|
|
180
|
+
readonly end: number
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const isNode = (v: unknown): v is Node =>
|
|
184
|
+
typeof v === "object" && v !== null && typeof Reflect.get(v, "type") === "string"
|
|
185
|
+
|
|
186
|
+
const field = (n: Node, key: string): unknown => Reflect.get(n, key)
|
|
187
|
+
const child = (n: Node, key: string) => {
|
|
188
|
+
const v = field(n, key)
|
|
189
|
+
return isNode(v) ? v : null
|
|
190
|
+
}
|
|
191
|
+
const text = (n: Node, key: string) => {
|
|
192
|
+
const v = field(n, key)
|
|
193
|
+
return typeof v === "string" ? v : ""
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const nameOf = (n: Node | null): string => {
|
|
197
|
+
if (n === null) {
|
|
198
|
+
return ""
|
|
199
|
+
}
|
|
200
|
+
if (n.type === "Identifier" || n.type === "JSXIdentifier") {
|
|
201
|
+
return text(n, "name")
|
|
202
|
+
}
|
|
203
|
+
if (n.type === "Literal") {
|
|
204
|
+
return String(field(n, "value"))
|
|
205
|
+
}
|
|
206
|
+
if (n.type === "MemberExpression") {
|
|
207
|
+
return nameOf(child(n, "property"))
|
|
208
|
+
}
|
|
209
|
+
if (n.type === "JSXNamespacedName") {
|
|
210
|
+
return `${nameOf(child(n, "namespace"))}:${nameOf(child(n, "name"))}`
|
|
211
|
+
}
|
|
212
|
+
return ""
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const walk = (node: Node, up: readonly Node[], visit: (n: Node, up: readonly Node[]) => void) => {
|
|
216
|
+
visit(node, up)
|
|
217
|
+
const inner = [...up, node]
|
|
218
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
219
|
+
const v = field(node, key)
|
|
220
|
+
if (Array.isArray(v)) {
|
|
221
|
+
for (const item of v) {
|
|
222
|
+
if (isNode(item)) {
|
|
223
|
+
walk(item, inner, visit)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
} else if (isNode(v)) {
|
|
227
|
+
walk(v, inner, visit)
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const ENTITIES: Readonly<Record<string, string>> = {
|
|
233
|
+
amp: "&",
|
|
234
|
+
apos: "'",
|
|
235
|
+
quot: '"',
|
|
236
|
+
lt: "<",
|
|
237
|
+
gt: ">",
|
|
238
|
+
nbsp: " ",
|
|
239
|
+
rsquo: "’",
|
|
240
|
+
lsquo: "‘",
|
|
241
|
+
ldquo: "“",
|
|
242
|
+
rdquo: "”",
|
|
243
|
+
hellip: "…",
|
|
244
|
+
middot: "·",
|
|
245
|
+
}
|
|
246
|
+
/** JSX text as it reads. */
|
|
247
|
+
const entities = (raw: string) =>
|
|
248
|
+
raw.replaceAll(/&(?<e>\w+);/gu, (m, e: string) => ENTITIES[e] ?? m)
|
|
249
|
+
|
|
250
|
+
/** A template element's text. */
|
|
251
|
+
const cooked = (q: Node) => {
|
|
252
|
+
const value = field(q, "value")
|
|
253
|
+
return typeof value === "object" && value !== null ? String(Reflect.get(value, "cooked")) : ""
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** A template's words, its expressions as `{}`. */
|
|
257
|
+
const templateWords = (n: Node) => {
|
|
258
|
+
const quasis = field(n, "quasis")
|
|
259
|
+
return Array.isArray(quasis) ? quasis.filter(isNode).map(cooked).join("{}") : ""
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Where a literal is code, not copy: a type, an import, a case.
|
|
263
|
+
const NOT_COPY = new Set([
|
|
264
|
+
"ImportDeclaration",
|
|
265
|
+
"ExportNamedDeclaration",
|
|
266
|
+
"ExportAllDeclaration",
|
|
267
|
+
"ImportExpression",
|
|
268
|
+
"SwitchCase",
|
|
269
|
+
])
|
|
270
|
+
|
|
271
|
+
interface Reach {
|
|
272
|
+
readonly sink: boolean
|
|
273
|
+
readonly most: 1 | 2 | null
|
|
274
|
+
}
|
|
275
|
+
const PLAIN: Reach = { sink: false, most: null }
|
|
276
|
+
|
|
277
|
+
/** A JSX attribute's: copy when it's a copy attribute; a tooltip's limit only on an element. */
|
|
278
|
+
const attribute = (attr: Node, opening: Node | undefined): Reach | null => {
|
|
279
|
+
const name = nameOf(child(attr, "name"))
|
|
280
|
+
if (!SINK_ATTR.has(name)) {
|
|
281
|
+
return null
|
|
282
|
+
}
|
|
283
|
+
// On a component (`<Row title=…>`) it's a prop, not a tooltip.
|
|
284
|
+
const dom =
|
|
285
|
+
opening?.type === "JSXOpeningElement" && /^[a-z]/u.test(nameOf(child(opening, "name")))
|
|
286
|
+
return { sink: true, most: dom ? (MOST.get(name) ?? null) : null }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Whether it reaches the screen, and its most words; null for code (a class, a key, a type). */
|
|
290
|
+
const reach = (n: Node, up: readonly Node[]): Reach | null => {
|
|
291
|
+
let at = n
|
|
292
|
+
for (let i = up.length - 1; i >= 0; i -= 1) {
|
|
293
|
+
const p = up[i]
|
|
294
|
+
if (p === undefined) {
|
|
295
|
+
break
|
|
296
|
+
}
|
|
297
|
+
if (p.type === "JSXAttribute") {
|
|
298
|
+
return attribute(p, up[i - 1])
|
|
299
|
+
}
|
|
300
|
+
if (p.type === "Property") {
|
|
301
|
+
const key = child(p, "key")
|
|
302
|
+
return key === at
|
|
303
|
+
? null
|
|
304
|
+
: { sink: UI_KEY.has(nameOf(key)), most: MOST.get(nameOf(key)) ?? null }
|
|
305
|
+
}
|
|
306
|
+
if (p.type === "JSXExpressionContainer") {
|
|
307
|
+
const q = up[i - 1]?.type
|
|
308
|
+
if (q === "JSXElement" || q === "JSXFragment") {
|
|
309
|
+
return { sink: true, most: null }
|
|
310
|
+
}
|
|
311
|
+
} else if (NOT_COPY.has(p.type) || (p.type.startsWith("TS") && !THROUGH.has(p.type))) {
|
|
312
|
+
return null
|
|
313
|
+
} else if (
|
|
314
|
+
!THROUGH.has(p.type) ||
|
|
315
|
+
(p.type === "BinaryExpression" && text(p, "operator") !== "+")
|
|
316
|
+
) {
|
|
317
|
+
return PLAIN
|
|
318
|
+
}
|
|
319
|
+
at = p
|
|
320
|
+
}
|
|
321
|
+
return PLAIN
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const typescript = (file: string, source: string): Copy[] => {
|
|
325
|
+
const { program } = parseSync(file, source, { lang: file.endsWith("x") ? "tsx" : "ts" })
|
|
326
|
+
const tsx = file.endsWith("x")
|
|
327
|
+
const lineAt = (offset: number) => source.slice(0, offset).split("\n").length
|
|
328
|
+
|
|
329
|
+
// Module-level string constants, for classes kept in one (`const LINE = "… text-ink3"`).
|
|
330
|
+
const consts = new Map<string, string>()
|
|
331
|
+
walk(program, [], (n, up) => {
|
|
332
|
+
if (n.type === "VariableDeclarator" && up.length <= 3) {
|
|
333
|
+
const init = child(n, "init")
|
|
334
|
+
if (init?.type === "Literal" && typeof field(init, "value") === "string") {
|
|
335
|
+
consts.set(nameOf(child(n, "id")), text(init, "value"))
|
|
336
|
+
} else if (init?.type === "TemplateLiteral") {
|
|
337
|
+
consts.set(nameOf(child(n, "id")), templateWords(init))
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
const classOf = (element: Node) => {
|
|
343
|
+
const opening = child(element, "openingElement")
|
|
344
|
+
const attrs = opening === null ? [] : field(opening, "attributes")
|
|
345
|
+
const out: string[] = []
|
|
346
|
+
for (const a of Array.isArray(attrs) ? attrs.filter(isNode) : []) {
|
|
347
|
+
if (a.type === "JSXAttribute" && nameOf(child(a, "name")) === "className") {
|
|
348
|
+
const value = child(a, "value")
|
|
349
|
+
if (value !== null) {
|
|
350
|
+
walk(value, [], (v) => {
|
|
351
|
+
if (v.type === "Literal") {
|
|
352
|
+
out.push(String(field(v, "value")))
|
|
353
|
+
} else if (v.type === "TemplateElement") {
|
|
354
|
+
out.push(cooked(v))
|
|
355
|
+
} else if (v.type === "Identifier") {
|
|
356
|
+
out.push(consts.get(text(v, "name")) ?? "")
|
|
357
|
+
}
|
|
358
|
+
})
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return out.join(" ")
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** The ink it's drawn in: the nearest element whose classes say. */
|
|
366
|
+
const dimmed = (up: readonly Node[]) => {
|
|
367
|
+
for (let i = up.length - 1; i >= 0; i -= 1) {
|
|
368
|
+
const n = up[i]
|
|
369
|
+
if (n?.type === "JSXElement") {
|
|
370
|
+
const classes = classOf(n)
|
|
371
|
+
if (DIM_CLASS.test(classes)) {
|
|
372
|
+
return true
|
|
373
|
+
}
|
|
374
|
+
if (INK_CLASS.test(classes)) {
|
|
375
|
+
return false
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return false
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const out: Copy[] = []
|
|
383
|
+
const add = (n: Node, up: readonly Node[], words: string, age = false) => {
|
|
384
|
+
const r = reach(n, up)
|
|
385
|
+
if (r === null) {
|
|
386
|
+
return
|
|
387
|
+
}
|
|
388
|
+
// In a .ts file only what's plainly copy counts; a .tsx is UI throughout.
|
|
389
|
+
if (!r.sink && !tsx) {
|
|
390
|
+
return
|
|
391
|
+
}
|
|
392
|
+
const glued = n.type === "TemplateLiteral" && status(words) && words.includes("{}")
|
|
393
|
+
out.push({
|
|
394
|
+
line: lineAt(n.start),
|
|
395
|
+
words,
|
|
396
|
+
sink: r.sink || glued,
|
|
397
|
+
dim: (r.sink && dimmed(up)) || glued,
|
|
398
|
+
most: r.most,
|
|
399
|
+
age,
|
|
400
|
+
})
|
|
401
|
+
}
|
|
402
|
+
const visit = (n: Node, up: readonly Node[]) => {
|
|
403
|
+
const parent = up.at(-1)?.type
|
|
404
|
+
if (n.type === "JSXText") {
|
|
405
|
+
const words = entities(text(n, "value")).replaceAll(/\s+/gu, " ").trim()
|
|
406
|
+
if (WORDY.test(words)) {
|
|
407
|
+
out.push({
|
|
408
|
+
line: lineAt(n.start),
|
|
409
|
+
words,
|
|
410
|
+
sink: true,
|
|
411
|
+
dim: dimmed(up),
|
|
412
|
+
most: null,
|
|
413
|
+
age: false,
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
} else if (n.type === "Literal" && typeof field(n, "value") === "string") {
|
|
417
|
+
if (WORDY.test(text(n, "value")) && parent !== "TemplateLiteral") {
|
|
418
|
+
add(n, up, text(n, "value"))
|
|
419
|
+
}
|
|
420
|
+
} else if (n.type === "TemplateLiteral" && parent !== "TaggedTemplateExpression") {
|
|
421
|
+
if (WORDY.test(templateWords(n))) {
|
|
422
|
+
add(n, up, templateWords(n))
|
|
423
|
+
}
|
|
424
|
+
} else if (n.type === "CallExpression" && AGE_CALL.test(nameOf(child(n, "callee")))) {
|
|
425
|
+
const call = source.slice(n.start, n.end)
|
|
426
|
+
if (ROW_TIME.test(call)) {
|
|
427
|
+
add(n, up, call, true)
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
walk(program, [], visit)
|
|
432
|
+
return out
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Swift: a line at a time, its modifiers on the lines after it.
|
|
436
|
+
const SWIFT_AGE = /\b(?:ago|since)\((?<of>[^()]*(?:\([^()]*\)[^()]*)*)\)/gu
|
|
437
|
+
|
|
438
|
+
/** A dimmed Text on this line, by its own modifiers or those on the lines after it. */
|
|
439
|
+
const swiftDim = (rows: readonly string[], k: number) => {
|
|
440
|
+
const line = rows[k] ?? ""
|
|
441
|
+
const after = rows
|
|
442
|
+
.slice(k + 1, k + 5)
|
|
443
|
+
.filter((r) => r.trim().startsWith("."))
|
|
444
|
+
.join("")
|
|
445
|
+
return (
|
|
446
|
+
line.includes("Text(") &&
|
|
447
|
+
!line.includes("prompt:") &&
|
|
448
|
+
(SWIFT_DIM.test(line) || SWIFT_DIM.test(after.split("{")[0] ?? ""))
|
|
449
|
+
)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** One line's copy. */
|
|
453
|
+
const swiftLine = (rows: readonly string[], k: number): Copy[] => {
|
|
454
|
+
const line = rows[k] ?? ""
|
|
455
|
+
const code = line.trim()
|
|
456
|
+
if (code.startsWith("//") || code.startsWith("*") || line.includes('"summary"')) {
|
|
457
|
+
return []
|
|
458
|
+
}
|
|
459
|
+
const sink = SWIFT_SINK.test(line)
|
|
460
|
+
const dim = swiftDim(rows, k)
|
|
461
|
+
const out: Copy[] = dim
|
|
462
|
+
? [...line.matchAll(SWIFT_AGE)]
|
|
463
|
+
.filter((m) => ROW_TIME.test(m[0]))
|
|
464
|
+
.map((m) => ({ line: k + 1, words: m[0], sink: true, dim: true, most: null, age: true }))
|
|
465
|
+
: []
|
|
466
|
+
const glued = /"\s*\+|\+\s*"/u.test(line)
|
|
467
|
+
for (const m of line.matchAll(LITERAL)) {
|
|
468
|
+
const words = m.groups?.words ?? ""
|
|
469
|
+
// A fragment between an interpolation's own quotes is code, not copy.
|
|
470
|
+
if (words.split("(").length !== words.split(")").length || words.includes("$0")) {
|
|
471
|
+
continue
|
|
472
|
+
}
|
|
473
|
+
if (glued && status(words)) {
|
|
474
|
+
out.push({ line: k + 1, words, sink: true, dim: true, most: null, age: false })
|
|
475
|
+
} else if (WORDY.test(words) && (words.includes(" ") || (sink && dim))) {
|
|
476
|
+
out.push({ line: k + 1, words, sink, dim, most: swiftMost(line, m.index), age: false })
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return out
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Swift: a line at a time, its modifiers on the lines after it.
|
|
483
|
+
const swift = (source: string): Copy[] => {
|
|
484
|
+
const rows = source.split("\n")
|
|
485
|
+
return rows.flatMap((_, k) => swiftLine(rows, k))
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const files = (at: string): string[] => {
|
|
489
|
+
if (!existsSync(at)) {
|
|
490
|
+
return []
|
|
491
|
+
}
|
|
492
|
+
if (statSync(at).isFile()) {
|
|
493
|
+
return CODE.test(at) ? [at] : []
|
|
494
|
+
}
|
|
495
|
+
return readdirSync(at, { recursive: true, encoding: "utf-8" })
|
|
496
|
+
.map((f) => path.join(at, f))
|
|
497
|
+
.filter((f) => CODE.test(f) && !SKIP.test(`/${path.relative(".", f)}`) && statSync(f).isFile())
|
|
498
|
+
.toSorted()
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const args = process.argv.slice(2)
|
|
502
|
+
const flag = (name: string) => {
|
|
503
|
+
const i = args.indexOf(name)
|
|
504
|
+
return i === -1 ? null : (args.splice(i, 2)[1] ?? "")
|
|
505
|
+
}
|
|
506
|
+
const listing = args.includes("--list")
|
|
507
|
+
const allowFile = flag("--allow") ?? "ui-captions.allow"
|
|
508
|
+
const roots = args.filter((a) => a !== "--list")
|
|
509
|
+
|
|
510
|
+
const allow = existsSync(allowFile)
|
|
511
|
+
? readFileSync(allowFile, "utf-8")
|
|
512
|
+
.split("\n")
|
|
513
|
+
.filter((l) => l !== "" && !l.startsWith("#") && l.includes("\t"))
|
|
514
|
+
.map((l) => {
|
|
515
|
+
const tab = l.indexOf("\t")
|
|
516
|
+
return { file: l.slice(0, tab), words: l.slice(tab + 1) }
|
|
517
|
+
})
|
|
518
|
+
: []
|
|
519
|
+
|
|
520
|
+
const used = new Set<(typeof allow)[number]>()
|
|
521
|
+
const found: string[] = []
|
|
522
|
+
for (const file of (roots.length > 0 ? roots : ["."]).flatMap(files)) {
|
|
523
|
+
const source = readFileSync(file, "utf-8")
|
|
524
|
+
const rel = path.relative(".", file)
|
|
525
|
+
for (const c of file.endsWith(".swift") ? swift(source) : typescript(file, source)) {
|
|
526
|
+
if (listing) {
|
|
527
|
+
console.log(`${rel}:${c.line}: ${c.words}`)
|
|
528
|
+
continue
|
|
529
|
+
}
|
|
530
|
+
const reason = why(c)
|
|
531
|
+
if (reason === null) {
|
|
532
|
+
continue
|
|
533
|
+
}
|
|
534
|
+
const hit = allow.find((a) => a.file === rel && c.words.includes(a.words))
|
|
535
|
+
if (hit) {
|
|
536
|
+
used.add(hit)
|
|
537
|
+
} else {
|
|
538
|
+
found.push(`${rel}:${c.line}: ${reason}: "${c.words.slice(0, 160)}"`)
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (!listing) {
|
|
543
|
+
for (const a of allow.filter((x) => !used.has(x))) {
|
|
544
|
+
console.log(
|
|
545
|
+
`${allowFile} names "${a.words}" in ${a.file}, which isn't there anymore: take its line out.`,
|
|
546
|
+
)
|
|
547
|
+
}
|
|
548
|
+
for (const f of found) {
|
|
549
|
+
console.log(f)
|
|
550
|
+
}
|
|
551
|
+
if (found.length > 0) {
|
|
552
|
+
console.log(
|
|
553
|
+
`UI copy is labels, data and a word-or-two placeholder; the interface explains itself. Cut these to a label or delete them. Only real information (a permission that's off, an error) may go in ${allowFile}.`,
|
|
554
|
+
)
|
|
555
|
+
}
|
|
556
|
+
if (found.length > 0 || used.size < allow.length) {
|
|
557
|
+
process.exit(1)
|
|
558
|
+
}
|
|
559
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "switch-ext-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "The extension convention for Switch: tools, events, widgets and a manifest, served as MCP.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/zachsents/switch",
|
|
9
|
+
"directory": "packages/kit"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"ui-captions": "./bin/ui-captions.ts"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"bin"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./src/index.ts",
|
|
22
|
+
"default": "./src/index.ts"
|
|
23
|
+
},
|
|
24
|
+
"./sealed": {
|
|
25
|
+
"types": "./src/sealed.ts",
|
|
26
|
+
"default": "./src/sealed.ts"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"typecheck": "tsgo --noEmit && tsgo --noEmit -p tsconfig.template.json",
|
|
32
|
+
"lint": "oxlint --type-aware --disable-nested-config --fix --fix-suggestions",
|
|
33
|
+
"format": "oxfmt --write .",
|
|
34
|
+
"fix": "bun run lint && bun run format",
|
|
35
|
+
"check": "bun run typecheck && bun run captions && bun run fix && bun run package",
|
|
36
|
+
"package": "publint && attw --pack . --profile esm-only",
|
|
37
|
+
"captions": "bun bin/ui-captions.ts template"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@modelcontextprotocol/server": "^2.4.0",
|
|
41
|
+
"oxc-parser": "^0.159.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
45
|
+
"@types/bun": "^1.4.4",
|
|
46
|
+
"@typescript/native-preview": "^7.0.0-dev.20260707.2",
|
|
47
|
+
"effect": "^4.1.4",
|
|
48
|
+
"oxfmt": "^0.79.0",
|
|
49
|
+
"oxlint": "^1.94.0",
|
|
50
|
+
"oxlint-tsgolint": "^7.0.2007",
|
|
51
|
+
"publint": "^0.3.25",
|
|
52
|
+
"ultracite": "^8.3.0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"effect": "^4.1.4"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/a2ui.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Widget helpers: a widget's A2UI messages for one surface, an MCP App inside A2UI's `McpApp`, and
|
|
2
|
+
// an MCP App view as one HTML document with its script bundled in, since a view loads nothing from
|
|
3
|
+
// outside. `view` runs bun's CLI, so call it as the extension starts: Bun runs no macros from an
|
|
4
|
+
// installed package.
|
|
5
|
+
|
|
6
|
+
/** Switch's own A2UI catalog: the house components, rendered by each client. */
|
|
7
|
+
export const SWITCH_CATALOG = "https://switch.ax/catalogs/v1/catalog.json"
|
|
8
|
+
export const BASIC_CATALOG = "https://a2ui.org/specification/v1_0/catalogs/basic/catalog.json"
|
|
9
|
+
export const MCP_CATALOG = "https://a2ui.org/specification/v1_0/catalogs/mcp/catalog.json"
|
|
10
|
+
|
|
11
|
+
/** One surface's messages: made in `catalog`, then its components (one has id `root`). */
|
|
12
|
+
export const surface = (
|
|
13
|
+
id: string,
|
|
14
|
+
catalog: string,
|
|
15
|
+
components: readonly Record<string, unknown>[],
|
|
16
|
+
): readonly unknown[] => [
|
|
17
|
+
{ version: "1.0", createSurface: { surfaceId: id, catalogId: catalog } },
|
|
18
|
+
{ version: "1.0", updateComponents: { surfaceId: id, components } },
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
/** A surface that is one MCP App: `html` in A2UI's `McpApp`, calling only `allowedTools`. */
|
|
22
|
+
export const mcpApp = (
|
|
23
|
+
id: string,
|
|
24
|
+
options: {
|
|
25
|
+
readonly title: string
|
|
26
|
+
readonly html: string
|
|
27
|
+
readonly allowedTools?: readonly string[]
|
|
28
|
+
/** JSON Pointer to the widget input the host sends as MCP App tool-input. */
|
|
29
|
+
readonly toolInput?: string
|
|
30
|
+
},
|
|
31
|
+
): readonly unknown[] =>
|
|
32
|
+
surface(id, MCP_CATALOG, [
|
|
33
|
+
{
|
|
34
|
+
id: "root",
|
|
35
|
+
component: "McpApp",
|
|
36
|
+
title: options.title,
|
|
37
|
+
htmlContent: options.html,
|
|
38
|
+
...(options.toolInput === undefined
|
|
39
|
+
? {}
|
|
40
|
+
: { data: { paths: { toolInput: options.toolInput } } }),
|
|
41
|
+
...(options.allowedTools === undefined ? {} : { allowedTools: [...options.allowedTools] }),
|
|
42
|
+
},
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
/** `entry` (a browser script, absolute) bundled into an HTML document with `style` and `body`. */
|
|
46
|
+
export const view = (
|
|
47
|
+
entry: string,
|
|
48
|
+
options: { readonly style?: string; readonly body?: string; readonly head?: string } = {},
|
|
49
|
+
): string => {
|
|
50
|
+
const built = Bun.spawnSync([
|
|
51
|
+
process.execPath,
|
|
52
|
+
"build",
|
|
53
|
+
entry,
|
|
54
|
+
"--target=browser",
|
|
55
|
+
"--format=iife",
|
|
56
|
+
"--minify",
|
|
57
|
+
])
|
|
58
|
+
if (!built.success) {
|
|
59
|
+
throw new Error(`${entry} didn't build: ${built.stderr.toString()}`)
|
|
60
|
+
}
|
|
61
|
+
const script = built.stdout.toString().replaceAll("</script", String.raw`<\/script`)
|
|
62
|
+
return `<!doctype html><html><head><meta charset="utf-8">${options.head ?? ""}<style>${options.style ?? ""}</style></head><body>${options.body ?? ""}<script>${script}</script></body></html>`
|
|
63
|
+
}
|