@glassly/miniapp-cli 0.1.17-dev.0 → 0.1.17-dev.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.
@@ -0,0 +1,237 @@
1
+ import {MiniappErrorCode, MiniappRequestType, MiniappResponseType} from "@glassly/miniapp/protocol"
2
+
3
+ import type {SimTypeContext, SimTypeHandler} from "./context.js"
4
+
5
+ const QUOTA_EXCEEDED_MESSAGE =
6
+ "AI credits are used up. Get more in Settings > Membership, or add your own API key in Settings > Privacy > API Keys."
7
+
8
+ export const SERVICE_MESSAGES: Record<string, string> = {
9
+ [MiniappErrorCode.QUOTA_EXCEEDED]: QUOTA_EXCEEDED_MESSAGE,
10
+ [MiniappErrorCode.AI_LOCAL_UNAVAILABLE]:
11
+ "The on-device model (Qwen3 1.7B) is not downloaded yet. Manage it in Settings > Offline Models.",
12
+ }
13
+
14
+ const READABLE_KEY_PROVIDERS = ["anthropic", "gemini", "openai", "deepseek", "elevenlabs", "mapbox"]
15
+ const SIM_MODEL = "glassly-sim"
16
+ const CALENDAR_MAX_LIMIT = 100
17
+ const CALENDAR_MAX_RANGE_MS = 31 * 24 * 60 * 60 * 1000
18
+
19
+ export const handleServices: SimTypeHandler = (ctx, type, payload, requestId) => {
20
+ switch (type) {
21
+ case MiniappRequestType.AI_COMPLETE:
22
+ aiComplete(ctx, payload, requestId)
23
+ return true
24
+ case MiniappRequestType.AI_GENERATE:
25
+ aiGenerate(ctx, payload, requestId)
26
+ return true
27
+ case MiniappRequestType.KEYS_GET:
28
+ keysGet(ctx, payload, requestId)
29
+ return true
30
+ case MiniappRequestType.SPEAKERS_RESOLVE_NAME:
31
+ speakersResolveName(ctx, payload, requestId)
32
+ return true
33
+ case MiniappRequestType.CALENDAR_LIST_EVENTS:
34
+ calendarListEvents(ctx, payload, requestId)
35
+ return true
36
+ case MiniappRequestType.WEB_LOGIN:
37
+ webLogin(ctx, payload, requestId)
38
+ return true
39
+ case MiniappRequestType.SCAN_QR:
40
+ scanQr(ctx, payload, requestId)
41
+ return true
42
+ }
43
+ return false
44
+ }
45
+
46
+ interface Message {
47
+ role?: string
48
+ content?: unknown
49
+ toolResults?: {content?: unknown}[]
50
+ }
51
+
52
+ interface Tool {
53
+ name?: unknown
54
+ inputSchema?: {properties?: Record<string, {type?: unknown}>; required?: unknown}
55
+ }
56
+
57
+ const brief = (text: string, max = 80) => (text.length > max ? `${text.slice(0, max - 1)}…` : text)
58
+ const tokens = (text: string) => Math.max(1, Math.ceil(text.length / 4))
59
+ let toolCallSeq = 0
60
+
61
+ function aiComplete(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
62
+ if (!ctx.requirePermission(requestId, "AI", MiniappRequestType.AI_COMPLETE)) return
63
+ if (!Array.isArray(payload.messages) || payload.messages.length === 0) {
64
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "ai.complete requires a non-empty messages array.")
65
+ }
66
+ ctx.fake(MiniappRequestType.AI_COMPLETE, "stand-in completion: echoes the prompt; calls the tool the prompt names")
67
+ const messages = payload.messages as Message[]
68
+ const last = messages[messages.length - 1]
69
+ const prompt = typeof last?.content === "string" ? last.content : ""
70
+ const inputTokens = tokens(JSON.stringify(messages) + (typeof payload.system === "string" ? payload.system : ""))
71
+ const base = {
72
+ provider: payload.forceLocal === true ? "device" : "anthropic",
73
+ model: SIM_MODEL,
74
+ billedToUserKey: false,
75
+ simulated: true,
76
+ }
77
+
78
+ const tool = last?.toolResults?.length ? undefined : pickTool(payload.tools, prompt)
79
+ if (tool) {
80
+ const call = {id: `glassly_sim_call_${++toolCallSeq}`, name: tool.name as string, input: toolInput(tool, prompt)}
81
+ ctx.log("info", `ai.complete (glassly sim) calls ${call.name}`)
82
+ return ctx.reply(requestId, true, {text: "", stopReason: "tool_use", toolCalls: [call], usage: {inputTokens, outputTokens: 16}, ...base})
83
+ }
84
+
85
+ const said = last?.toolResults?.length
86
+ ? `tool results: ${brief(last.toolResults.map((r) => String(r.content ?? "")).join("; "))}`
87
+ : `you said "${brief(prompt)}"`
88
+ const text = payload.jsonMode === true ? JSON.stringify({simulated: true, reply: said}) : `[glassly sim] ${said}`
89
+ ctx.reply(requestId, true, {text, stopReason: "end_turn", usage: {inputTokens, outputTokens: tokens(text)}, ...base})
90
+ }
91
+
92
+ /** The offered tool whose name's words all appear in the prompt (the most specific wins), else the only tool. */
93
+ function pickTool(tools: unknown, prompt: string): Tool | undefined {
94
+ const named = (Array.isArray(tools) ? tools : []).filter((t): t is Tool => typeof t?.name === "string")
95
+ const words = (tool: Tool) => (tool.name as string).split("__").pop()!.toLowerCase().split(/[_\-\s]+/).filter(Boolean)
96
+ const said = prompt.toLowerCase()
97
+ const matches = named.filter((tool) => words(tool).every((word) => said.includes(word)))
98
+ matches.sort((a, b) => words(b).length - words(a).length)
99
+ return matches[0] ?? (named.length === 1 ? named[0] : undefined)
100
+ }
101
+
102
+ function toolInput(tool: Tool, prompt: string): Record<string, unknown> {
103
+ const properties = tool.inputSchema?.properties ?? {}
104
+ const required = Array.isArray(tool.inputSchema?.required) ? (tool.inputSchema.required as string[]) : Object.keys(properties)
105
+ const input: Record<string, unknown> = {}
106
+ for (const name of required) {
107
+ const kind = properties[name]?.type
108
+ input[name] =
109
+ kind === "number" || kind === "integer" ? 0 : kind === "boolean" ? false : kind === "array" ? [] : kind === "object" ? {} : prompt
110
+ }
111
+ return input
112
+ }
113
+
114
+ function aiGenerate(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
115
+ if (!ctx.requirePermission(requestId, "AI", MiniappRequestType.AI_GENERATE)) return
116
+ if (typeof payload.prompt !== "string" || !payload.prompt.trim()) {
117
+ return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "ai.generate requires a non-empty prompt string.")
118
+ }
119
+ ctx.fake(MiniappRequestType.AI_GENERATE, "stand-in generation: text only, no images")
120
+ if (typeof payload.progressToken === "string" && payload.progressToken) {
121
+ ctx.deliver({
122
+ type: MiniappResponseType.EVENT,
123
+ streamType: "_ai_generate_progress",
124
+ data: {progressToken: payload.progressToken, activity: "drawing", timestamp: Date.now()},
125
+ })
126
+ }
127
+ const text = `[glassly sim] generated for "${brief(payload.prompt)}"`
128
+ ctx.reply(requestId, true, {
129
+ text,
130
+ images: [],
131
+ stopReason: "end_turn",
132
+ usage: {inputTokens: tokens(payload.prompt), outputTokens: tokens(text), rounds: 1},
133
+ provider: "anthropic",
134
+ model: SIM_MODEL,
135
+ billedToUserKey: false,
136
+ simulated: true,
137
+ })
138
+ }
139
+
140
+ function keysGet(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
141
+ if (!ctx.requirePermission(requestId, "API_KEYS", MiniappRequestType.KEYS_GET)) return
142
+ const provider = typeof payload.provider === "string" ? payload.provider : ""
143
+ if (!READABLE_KEY_PROVIDERS.includes(provider)) {
144
+ return ctx.fail(
145
+ requestId,
146
+ MiniappErrorCode.INVALID_ARGUMENT,
147
+ `API key provider "${provider}" is not readable by miniapps. Readable providers: ${READABLE_KEY_PROVIDERS.join(", ")}.`,
148
+ )
149
+ }
150
+ const variable = `GLASSLY_SIM_KEY_${provider.toUpperCase()}`
151
+ const key = process.env[variable]?.trim() || null
152
+ ctx.log("info", key ? `keys.get("${provider}") answered from ${variable}` : `keys.get("${provider}"): no key; set ${variable} to give one`)
153
+ ctx.reply(requestId, true, {key})
154
+ }
155
+
156
+ function speakersResolveName(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
157
+ if (!ctx.requirePermission(requestId, "SPEAKERS", MiniappRequestType.SPEAKERS_RESOLVE_NAME)) return
158
+ const id = typeof payload.id === "string" ? payload.id : ""
159
+ if (!id) return ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, "speakers_resolve_name requires id")
160
+ ctx.fake(MiniappRequestType.SPEAKERS_RESOLVE_NAME, "stand-in profiles: spk_<name> resolves to <Name>, other ids to null")
161
+ const enrolled = /^spk_(.+)$/.exec(id)?.[1]
162
+ const name = enrolled ? enrolled.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()) : null
163
+ ctx.reply(requestId, true, {name, simulated: true})
164
+ }
165
+
166
+ function calendarListEvents(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
167
+ if (!ctx.declares("CALENDAR")) {
168
+ const message = 'CALENDAR permission not declared in miniapp.json. Add {"type": "CALENDAR"} to the permissions array.'
169
+ ctx.log("error", message)
170
+ return ctx.reply(requestId, false, undefined, {code: MiniappErrorCode.PERMISSION_NOT_DECLARED, message, permission: "CALENDAR"})
171
+ }
172
+ const invalid = (message: string) => ctx.fail(requestId, MiniappErrorCode.INVALID_ARGUMENT, message)
173
+ const date = (field: string): Date | string => {
174
+ const value = payload[field]
175
+ if (typeof value !== "string") return `${field} must be an ISO 8601 string`
176
+ const parsed = new Date(value)
177
+ return Number.isNaN(parsed.getTime()) ? `${field} must be a valid ISO 8601 string` : parsed
178
+ }
179
+ const startsAt = date("startsAt")
180
+ if (typeof startsAt === "string") return invalid(startsAt)
181
+ const endsAt = date("endsAt")
182
+ if (typeof endsAt === "string") return invalid(endsAt)
183
+ if (endsAt.getTime() <= startsAt.getTime()) return invalid("endsAt must be after startsAt")
184
+ if (endsAt.getTime() - startsAt.getTime() > CALENDAR_MAX_RANGE_MS) return invalid("Calendar query range may not exceed 31 days")
185
+ const limit = payload.limit === undefined ? 50 : payload.limit
186
+ if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > CALENDAR_MAX_LIMIT) {
187
+ return invalid(`limit must be an integer between 1 and ${CALENDAR_MAX_LIMIT}`)
188
+ }
189
+
190
+ ctx.fake(MiniappRequestType.CALENDAR_LIST_EVENTS, "stand-in calendar: three sample events around now")
191
+ const hour = new Date()
192
+ hour.setMinutes(0, 0, 0)
193
+ const at = (hours: number) => new Date(hour.getTime() + hours * 3_600_000).toISOString()
194
+ const event = (eventId: string, title: string, from: number, to: number, extra: Record<string, unknown> = {}) => ({
195
+ id: `glassly-sim:${eventId}:${at(from)}`,
196
+ calendarId: "glassly-sim",
197
+ title,
198
+ startsAt: at(from),
199
+ endsAt: at(to),
200
+ allDay: false,
201
+ links: [],
202
+ ...extra,
203
+ })
204
+ const events = [
205
+ event("standup", "Team standup (glassly sim)", 1, 1.25, {
206
+ notes: "Join at https://meet.example.com/glassly-sim",
207
+ links: ["https://meet.example.com/glassly-sim"],
208
+ }),
209
+ event("lunch", "Lunch with Sam (glassly sim)", 3, 4, {location: "Corner Cafe"}),
210
+ event("review", "Design review (glassly sim)", 26, 27),
211
+ ].filter((e) => Date.parse(e.endsAt) > startsAt.getTime() && Date.parse(e.startsAt) < endsAt.getTime())
212
+ ctx.reply(requestId, true, {events: events.slice(0, limit), truncated: events.length > limit, simulated: true})
213
+ }
214
+
215
+ function webLogin(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
216
+ const httpsHost = (value: unknown) => typeof value === "string" && /^https:\/\/([^/?#]+)(?:[/?#]|$)/i.test(value)
217
+ if (!httpsHost(payload.url)) return ctx.fail(requestId, "INVALID_ARGS", "webLogin: url must be an https URL")
218
+ if (!httpsHost(payload.doneUrlPrefix)) return ctx.fail(requestId, "INVALID_ARGS", "webLogin: doneUrlPrefix must be an https URL")
219
+ const cookies = Array.isArray(payload.cookies)
220
+ ? payload.cookies.filter((name): name is string => typeof name === "string" && /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(name))
221
+ : []
222
+ if (cookies.length === 0) return ctx.fail(requestId, "INVALID_ARGS", "webLogin: cookies must name at least one cookie")
223
+ ctx.fake(MiniappRequestType.WEB_LOGIN, "stand-in sign-in: completes at once with placeholder cookies")
224
+ ctx.effect("web_login", {url: payload.url, doneUrlPrefix: payload.doneUrlPrefix, cookies})
225
+ ctx.reply(requestId, true, {
226
+ status: "completed",
227
+ cookies: Object.fromEntries(cookies.map((name) => [name, `glassly-sim-${name}`])),
228
+ url: payload.doneUrlPrefix,
229
+ simulated: true,
230
+ })
231
+ }
232
+
233
+ function scanQr(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
234
+ ctx.fake(MiniappRequestType.SCAN_QR, "stand-in scan: GLASSLY_SIM_QR_DATA, else glassly-sim-qr")
235
+ ctx.effect("scan_qr", {title: payload.title, hint: payload.hint})
236
+ ctx.reply(requestId, true, {data: process.env.GLASSLY_SIM_QR_DATA || "glassly-sim-qr", simulated: true})
237
+ }
@@ -0,0 +1,36 @@
1
+ /// <reference types="bun-types" />
2
+
3
+ import {expect, test} from "bun:test"
4
+
5
+ import {readWavPcm} from "./wav.js"
6
+
7
+ function wav(rate: number, channels: number, samples: number[]): Uint8Array {
8
+ const data = new Uint8Array(samples)
9
+ const file = new Uint8Array(44 + data.length)
10
+ const view = new DataView(file.buffer)
11
+ file.set(new TextEncoder().encode("RIFF"), 0)
12
+ view.setUint32(4, 36 + data.length, true)
13
+ file.set(new TextEncoder().encode("WAVEfmt "), 8)
14
+ view.setUint32(16, 16, true)
15
+ view.setUint16(20, 1, true)
16
+ view.setUint16(22, channels, true)
17
+ view.setUint32(24, rate, true)
18
+ view.setUint32(28, rate * channels * 2, true)
19
+ view.setUint16(32, channels * 2, true)
20
+ view.setUint16(34, 16, true)
21
+ file.set(new TextEncoder().encode("data"), 36)
22
+ view.setUint32(40, data.length, true)
23
+ file.set(data, 44)
24
+ return file
25
+ }
26
+
27
+ test("a 16 kHz mono WAV yields its PCM samples", () => {
28
+ expect([...readWavPcm(wav(16000, 1, [1, 2, 3, 4]))]).toEqual([1, 2, 3, 4])
29
+ })
30
+
31
+ test("other formats are refused with the conversion command", () => {
32
+ expect(() => readWavPcm(wav(44100, 2, [0, 0, 0, 0]))).toThrow(
33
+ "this file is 44100 Hz, 2 channel(s), 16-bit. Convert it with: ffmpeg -i in.wav -ac 1 -ar 16000 -sample_fmt s16 out.wav",
34
+ )
35
+ expect(() => readWavPcm(new TextEncoder().encode("ID3 not a wav"))).toThrow("Not a WAV file")
36
+ })
package/src/sim/wav.ts ADDED
@@ -0,0 +1,34 @@
1
+ // The glasses' microphone sends 16 kHz mono 16-bit PCM, so that is the only
2
+ // WAV the simulator feeds; anything else would reach the miniapp in a format
3
+ // the phone never sends.
4
+ export const MIC_SAMPLE_RATE = 16000
5
+
6
+ export function readWavPcm(file: Uint8Array): Uint8Array {
7
+ const view = new DataView(file.buffer, file.byteOffset, file.byteLength)
8
+ const tag = (at: number) => String.fromCharCode(...file.subarray(at, at + 4))
9
+ if (file.byteLength < 12 || tag(0) !== "RIFF" || tag(8) !== "WAVE") throw new Error("Not a WAV file (no RIFF/WAVE header)")
10
+ let format: {code: number; channels: number; rate: number; bits: number} | null = null
11
+ for (let at = 12; at + 8 <= file.byteLength; ) {
12
+ const size = view.getUint32(at + 4, true)
13
+ const body = at + 8
14
+ if (tag(at) === "fmt ") {
15
+ format = {
16
+ code: view.getUint16(body, true),
17
+ channels: view.getUint16(body + 2, true),
18
+ rate: view.getUint32(body + 4, true),
19
+ bits: view.getUint16(body + 14, true),
20
+ }
21
+ } else if (tag(at) === "data") {
22
+ if (!format) throw new Error("WAV data comes before its format chunk")
23
+ if (format.code !== 1 || format.channels !== 1 || format.rate !== MIC_SAMPLE_RATE || format.bits !== 16) {
24
+ throw new Error(
25
+ `The glasses' microphone sends 16 kHz mono 16-bit PCM; this file is ${format.rate} Hz, ${format.channels} channel(s), ${format.bits}-bit. ` +
26
+ "Convert it with: ffmpeg -i in.wav -ac 1 -ar 16000 -sample_fmt s16 out.wav",
27
+ )
28
+ }
29
+ return file.subarray(body, Math.min(body + size, file.byteLength))
30
+ }
31
+ at = body + size + (size % 2)
32
+ }
33
+ throw new Error("WAV file has no data chunk")
34
+ }