@glassly/miniapp-cli 0.1.17-dev.0 → 0.1.17-dev.2

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,320 @@
1
+ import {MiniappErrorCode, MiniappRequestType, MiniappResponseType, MiniappStreamType} from "@glassly/miniapp/protocol"
2
+
3
+ import type {SimTypeContext, SimTypeHandler} from "./context.js"
4
+
5
+ type LatLng = {lat: number; lng: number}
6
+
7
+ interface Trip {
8
+ state: "navigating" | "arrived"
9
+ mode: string
10
+ stops: LatLng[]
11
+ maneuver?: Record<string, unknown>
12
+ nativeNav: boolean
13
+ timers: ReturnType<typeof setTimeout>[]
14
+ }
15
+
16
+ interface NavState {
17
+ trip?: Trip
18
+ lastOrigin?: LatLng
19
+ }
20
+
21
+ const states = new WeakMap<SimTypeContext, NavState>()
22
+ const stateOf = (ctx: SimTypeContext): NavState => {
23
+ let state = states.get(ctx)
24
+ if (!state) states.set(ctx, (state = {}))
25
+ return state
26
+ }
27
+
28
+ const SIM = "(glassly sim)"
29
+ const SPEED_MPS: Record<string, number> = {walking: 1.4, cycling: 4.5, two_wheeler: 8, driving: 13.9}
30
+ const DEFAULT_ORIGIN: LatLng = {lat: 37.7749, lng: -122.4194}
31
+
32
+ function toLatLng(raw: unknown): LatLng | undefined {
33
+ if (!raw || typeof raw !== "object") return undefined
34
+ const obj = raw as Record<string, unknown>
35
+ const lat = Number(obj.lat)
36
+ const lng = Number(obj.lng)
37
+ return Number.isFinite(lat) && Number.isFinite(lng) ? {lat, lng} : undefined
38
+ }
39
+
40
+ function meters(a: LatLng, b: LatLng): number {
41
+ const rad = Math.PI / 180
42
+ const dLat = (b.lat - a.lat) * rad
43
+ const dLng = (b.lng - a.lng) * rad
44
+ const h = Math.sin(dLat / 2) ** 2 + Math.cos(a.lat * rad) * Math.cos(b.lat * rad) * Math.sin(dLng / 2) ** 2
45
+ return Math.round(2 * 6371000 * Math.asin(Math.sqrt(h)))
46
+ }
47
+
48
+ const pathMeters = (points: LatLng[]): number =>
49
+ points.slice(1).reduce((sum, point, i) => sum + meters(points[i], point), 0)
50
+
51
+ function snapshot(trip: Trip): Record<string, unknown> {
52
+ const m = trip.maneuver
53
+ return {
54
+ active: trip.state !== "arrived",
55
+ mode: trip.mode,
56
+ stops: trip.stops,
57
+ currentStopIndex: 0,
58
+ maneuver: m,
59
+ distanceToDestinationMeters: m?.distanceToDestinationMeters,
60
+ timeToDestinationSeconds: m?.timeToDestinationSeconds,
61
+ currentSpeedMps: m?.currentSpeedMps,
62
+ speedLimitMps: m?.speedLimitMps,
63
+ }
64
+ }
65
+
66
+ function sendUpdate(ctx: SimTypeContext, data: Record<string, unknown>): void {
67
+ ctx.deliver({type: MiniappResponseType.EVENT, streamType: MiniappStreamType.NAVIGATION_UPDATE, data})
68
+ }
69
+
70
+ function endTrip(nav: NavState): void {
71
+ for (const timer of nav.trip?.timers ?? []) clearTimeout(timer)
72
+ nav.trip = undefined
73
+ }
74
+
75
+ // A straight walk to the last stop: depart, halfway, arrive. Only simulated trips advance on their own.
76
+ function scriptTrip(ctx: SimTypeContext, nav: NavState, trip: Trip, simulate: boolean, speedMultiplier: number): void {
77
+ const total = pathMeters([nav.lastOrigin ?? trip.stops[0], ...trip.stops])
78
+ const speed = SPEED_MPS[trip.mode] ?? SPEED_MPS.walking
79
+ const maneuver = (maneuverType: string, remaining: number, instruction: string) => ({
80
+ kind: "maneuver",
81
+ maneuverType,
82
+ distanceMeters: remaining,
83
+ fromRoad: null,
84
+ toRoad: null,
85
+ nextStepRoad: null,
86
+ distanceToDestinationMeters: remaining,
87
+ timeToDestinationSeconds: Math.round(remaining / speed),
88
+ currentSpeedMps: simulate ? speed : null,
89
+ speedLimitMps: null,
90
+ routeHeadingDeg: null,
91
+ instruction: `${instruction} ${SIM}`,
92
+ })
93
+ const steps: (() => void)[] = [
94
+ () => {
95
+ trip.maneuver = maneuver("DEPART", total, "Head to your destination")
96
+ sendUpdate(ctx, trip.maneuver)
97
+ },
98
+ ]
99
+ if (simulate) {
100
+ steps.push(
101
+ () => {
102
+ trip.maneuver = maneuver("ARRIVE", Math.round(total / 2), "Continue to your destination")
103
+ sendUpdate(ctx, trip.maneuver)
104
+ },
105
+ () => {
106
+ trip.state = "arrived"
107
+ trip.nativeNav = false
108
+ sendUpdate(ctx, {kind: "arrived"})
109
+ ctx.effect("navigation", {action: "arrived"})
110
+ },
111
+ )
112
+ }
113
+ const stepMs = 3000 / speedMultiplier
114
+ steps.forEach((step, i) => {
115
+ const timer = setTimeout(() => nav.trip === trip && step(), i * stepMs)
116
+ ;(timer as {unref?: () => void}).unref?.()
117
+ trip.timers.push(timer)
118
+ })
119
+ }
120
+
121
+ function handleStart(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
122
+ const stops = (Array.isArray(payload.stops) ? payload.stops : []).map(toLatLng).filter((stop) => stop !== undefined)
123
+ if (stops.length === 0) {
124
+ const stop = toLatLng(payload)
125
+ if (stop) stops.push(stop)
126
+ }
127
+ if (stops.length === 0) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, "stops or lat/lng required")
128
+ const speedNum = Number(payload.speedMultiplier)
129
+ const speedMultiplier = Number.isFinite(speedNum) && speedNum > 0 ? speedNum : 5
130
+ const mode = typeof payload.mode === "string" ? payload.mode : "walking"
131
+
132
+ const nav = stateOf(ctx)
133
+ if (nav.trip?.state === "navigating") {
134
+ return ctx.reply(requestId, true, {ok: true, resumed: true, snapshot: snapshot(nav.trip)})
135
+ }
136
+ endTrip(nav)
137
+ ctx.fake(MiniappRequestType.NAVIGATION_START, "starts a scripted trip; no navigation engine or Mapbox token")
138
+ const trip: Trip = {
139
+ state: "navigating",
140
+ mode,
141
+ stops,
142
+ nativeNav: ctx.device.capabilities.hasNativeNavigation === true,
143
+ timers: [],
144
+ }
145
+ nav.trip = trip
146
+ ctx.effect("navigation", {action: "start", stops, mode, simulate: payload.simulate === true})
147
+ ctx.reply(requestId, true, {ok: true, simulated: true})
148
+ scriptTrip(ctx, nav, trip, payload.simulate === true, speedMultiplier)
149
+ }
150
+
151
+ function handleComputeRoute(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
152
+ const origin = toLatLng(payload.origin)
153
+ const stops = Array.isArray(payload.stops) ? payload.stops.map(toLatLng) : []
154
+ if (!origin || stops.length === 0 || stops.some((stop) => !stop)) {
155
+ return ctx.fail(requestId, MiniappErrorCode.INTERNAL, "HTTP 400 on POST /api/maps/directions")
156
+ }
157
+ ctx.fake(MiniappRequestType.NAVIGATION_COMPUTE_ROUTE, "a straight line through the stops")
158
+ stateOf(ctx).lastOrigin = origin
159
+ const points = [origin, ...(stops as LatLng[])]
160
+ const speed = SPEED_MPS[typeof payload.mode === "string" ? payload.mode : "walking"] ?? SPEED_MPS.walking
161
+ const steps = points.slice(1).map((end, i) => ({
162
+ lat: points[i].lat,
163
+ lng: points[i].lng,
164
+ endLat: end.lat,
165
+ endLng: end.lng,
166
+ distanceMeters: meters(points[i], end),
167
+ maneuver: "ARRIVE",
168
+ instruction: `Head to stop ${i + 1} ${SIM}`,
169
+ road: null,
170
+ }))
171
+ const totalDistanceMeters = pathMeters(points)
172
+ ctx.reply(requestId, true, {
173
+ ok: true,
174
+ simulated: true,
175
+ routes: [
176
+ {
177
+ points,
178
+ totalDistanceMeters,
179
+ totalDurationSeconds: Math.round(totalDistanceMeters / speed),
180
+ summary: `Straight line ${SIM}`,
181
+ steps,
182
+ },
183
+ ],
184
+ })
185
+ }
186
+
187
+ function handlePlaceAutocomplete(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
188
+ const query = typeof payload.query === "string" ? payload.query.trim() : ""
189
+ ctx.fake(MiniappRequestType.NAVIGATION_PLACE_AUTOCOMPLETE, "two made-up places near the search point")
190
+ const near = toLatLng(payload.near)
191
+ const center = near ?? DEFAULT_ORIGIN
192
+ const suggestions = !query
193
+ ? []
194
+ : [0.003, 0.006].map((offset, i) => {
195
+ const at = {lat: +(center.lat + offset).toFixed(6), lng: +(center.lng + offset).toFixed(6)}
196
+ const mainText = `${query} ${i + 1}`
197
+ return {
198
+ placeId: `glassly-sim:${at.lat},${at.lng}:${mainText}`,
199
+ mainText,
200
+ secondaryText: `Simulated place ${SIM}`,
201
+ ...(near ? {distanceMeters: meters(near, at)} : {}),
202
+ }
203
+ })
204
+ ctx.reply(requestId, true, {ok: true, simulated: true, suggestions})
205
+ }
206
+
207
+ function handlePlaceDetails(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
208
+ const placeId = typeof payload.placeId === "string" ? payload.placeId : ""
209
+ if (!placeId) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, "placeId required")
210
+ ctx.fake(MiniappRequestType.NAVIGATION_PLACE_DETAILS, "resolves the simulator's own suggestions only")
211
+ const match = /^glassly-sim:(-?[\d.]+),(-?[\d.]+):(.*)$/.exec(placeId)
212
+ if (!match) return ctx.fail(requestId, MiniappErrorCode.INTERNAL, "place details failed")
213
+ ctx.reply(requestId, true, {
214
+ ok: true,
215
+ simulated: true,
216
+ place: {placeId, name: match[3], address: `Simulated place ${SIM}`, lat: Number(match[1]), lng: Number(match[2])},
217
+ })
218
+ }
219
+
220
+ function handleNativeNav(ctx: SimTypeContext, payload: Record<string, unknown>, requestId?: string): void {
221
+ if (!stateOf(ctx).trip?.nativeNav) {
222
+ return ctx.reply(requestId, true, {status: "unsupported", reason: "navigation mode not active"})
223
+ }
224
+ const map = (payload.map ?? {}) as {data?: unknown}
225
+ const sent =
226
+ payload.action === "info" ||
227
+ ((payload.action === "mini_map" || payload.action === "overview_map") && typeof map.data === "string" && map.data.length > 0)
228
+ if (sent) ctx.effect("native_nav", {action: payload.action})
229
+ ctx.reply(requestId, true, {status: sent ? "sent" : "unsupported", ...(sent ? {} : {reason: "native navigation unavailable"})})
230
+ }
231
+
232
+ export const handleNavigation: SimTypeHandler = (ctx, type, payload, requestId) => {
233
+ const nav = stateOf(ctx)
234
+ switch (type) {
235
+ case MiniappRequestType.NAVIGATION_START:
236
+ handleStart(ctx, payload, requestId)
237
+ return true
238
+ case MiniappRequestType.NAVIGATION_STOP:
239
+ if (nav.trip) ctx.effect("navigation", {action: "stop"})
240
+ endTrip(nav)
241
+ ctx.reply(requestId, true, {ok: true})
242
+ return true
243
+ case MiniappRequestType.NAVIGATION_DEVIATE: {
244
+ const offsetNum = Number(payload.offsetMeters)
245
+ const offsetMeters = Number.isFinite(offsetNum) && offsetNum > 0 ? offsetNum : 20
246
+ ctx.reply(requestId, true, {ok: true})
247
+ if (nav.trip?.state === "navigating") {
248
+ sendUpdate(ctx, {kind: "off_route", offRouteDistanceMeters: offsetMeters})
249
+ sendUpdate(ctx, {kind: "rerouting"})
250
+ }
251
+ return true
252
+ }
253
+ case MiniappRequestType.NAVIGATION_SET_WRONG_SIDEWALK:
254
+ case MiniappRequestType.NAVIGATION_SET_SKIP_CROSSINGS:
255
+ ctx.effect("navigation", {action: type.replace("miniapp_navigation_", ""), enabled: payload.enabled === true})
256
+ ctx.reply(requestId, true, {ok: true})
257
+ return true
258
+ case MiniappRequestType.NAVIGATION_GET_STATE:
259
+ ctx.reply(requestId, true, {state: nav.trip ? snapshot(nav.trip) : null})
260
+ return true
261
+ case MiniappRequestType.NAVIGATION_REQUEST_PERMISSION:
262
+ ctx.reply(requestId, true, {ok: true, accepted: true})
263
+ return true
264
+ case MiniappRequestType.NAVIGATION_COMPUTE_ROUTE:
265
+ handleComputeRoute(ctx, payload, requestId)
266
+ return true
267
+ case MiniappRequestType.NAVIGATION_REVERSE_GEOCODE: {
268
+ const at = toLatLng(payload)
269
+ if (!at) {
270
+ ctx.fail(requestId, MiniappErrorCode.INTERNAL, "lat/lng required")
271
+ return true
272
+ }
273
+ ctx.fake(type, "a placeholder road and address")
274
+ ctx.reply(requestId, true, {
275
+ ok: true,
276
+ simulated: true,
277
+ road: "Simulated Street",
278
+ address: `${at.lat.toFixed(5)}, ${at.lng.toFixed(5)} ${SIM}`,
279
+ })
280
+ return true
281
+ }
282
+ case MiniappRequestType.NAVIGATION_PLACE_AUTOCOMPLETE:
283
+ handlePlaceAutocomplete(ctx, payload, requestId)
284
+ return true
285
+ case MiniappRequestType.NAVIGATION_PLACE_DETAILS:
286
+ handlePlaceDetails(ctx, payload, requestId)
287
+ return true
288
+ case MiniappRequestType.NATIVE_NAV:
289
+ handleNativeNav(ctx, payload, requestId)
290
+ return true
291
+ case MiniappRequestType.SHOW_NATIVE_DASHBOARD:
292
+ if (nav.trip?.nativeNav) {
293
+ ctx.reply(requestId, true, {status: "unsupported", reason: "native navigation active"})
294
+ } else if (!ctx.glassesConnected) {
295
+ ctx.reply(requestId, true, {status: "unsupported", reason: "no glasses connected"})
296
+ } else {
297
+ ctx.effect("native_dashboard", {action: "show"})
298
+ ctx.reply(requestId, true, {status: "shown"})
299
+ }
300
+ return true
301
+ case MiniappRequestType.DASHBOARD_CONTENT_UPDATE:
302
+ ctx.fail(requestId, MiniappErrorCode.NOT_IMPLEMENTED, "Dashboard API is deferred in v1")
303
+ return true
304
+ case MiniappRequestType.DISPLAY:
305
+ if (!payload.layout || typeof payload.layout !== "object") {
306
+ ctx.fail(requestId, MiniappErrorCode.INTERNAL, "display request missing layout object")
307
+ return true
308
+ }
309
+ ctx.fake(type, "answered as the phone does; legacy layouts are not drawn on the simulated glasses")
310
+ ctx.effect("display", {view: payload.view ?? "main", layout: payload.layout})
311
+ ctx.reply(requestId, true)
312
+ return true
313
+ case MiniappRequestType.DISPLAY_DIAGNOSTICS:
314
+ ctx.reply(requestId, true, {enabled: false, generatedAt: Date.now(), records: []})
315
+ return true
316
+ }
317
+ return false
318
+ }
319
+
320
+ export const NAVIGATION_MESSAGES: Record<string, string> = {}
@@ -0,0 +1,140 @@
1
+ /// <reference types="bun-types" />
2
+
3
+ import {afterEach, describe, expect, test} from "bun:test"
4
+ import {MiniappSession, type Transport} from "@glassly/miniapp"
5
+
6
+ import {simDevice} from "../device.js"
7
+ import {SimHost, type SimHostEvent, type SimManifest} from "../host.js"
8
+ import {createSimStorage} from "../storage.js"
9
+
10
+ async function connect(manifest: Partial<SimManifest> = {}) {
11
+ const events: SimHostEvent[] = []
12
+ let deliver: (raw: string) => void = () => {}
13
+ const host = new SimHost({
14
+ manifest: {packageName: "com.example.sim", name: "Sim Test", ...manifest},
15
+ device: simDevice("simulated"),
16
+ storage: createSimStorage(),
17
+ toBackground: (raw) => queueMicrotask(() => deliver(raw)),
18
+ emit: (event) => events.push(event),
19
+ frameLatencyMs: 0,
20
+ speechMsPerChar: 0,
21
+ })
22
+ const transport: Transport = {
23
+ open: async () => {},
24
+ send: (raw) => queueMicrotask(() => host.fromBackground(raw)),
25
+ onMessage: (handler) => {
26
+ deliver = handler
27
+ },
28
+ onDisconnect: () => {},
29
+ close: () => {},
30
+ isOpen: () => true,
31
+ }
32
+ host.backgroundStarted()
33
+ const session = new MiniappSession({transport, packageName: "com.example.sim"})
34
+ await session.connect()
35
+ const logs = () => events.flatMap((event) => (event.type === "log" ? [event.entry.message] : []))
36
+ return {host, session, logs}
37
+ }
38
+
39
+ const permissions = (...types: string[]) => ({permissions: types.map((type) => ({type}))})
40
+
41
+ describe("ai", () => {
42
+ const addNote = {
43
+ name: "com_glassly_notes__add_note",
44
+ description: "Save a note",
45
+ inputSchema: {type: "object", properties: {text: {type: "string"}}, required: ["text"]},
46
+ }
47
+ const startMiniapp = {name: "start_miniapp", description: "Open a miniapp", inputSchema: {type: "object", properties: {}}}
48
+
49
+ test("drives a tool loop: calls the tool the prompt names, then answers the tool result", async () => {
50
+ const {session} = await connect(permissions("AI"))
51
+ const first = await session.ai.complete({messages: [{role: "user", content: "add note buy milk"}], tools: [startMiniapp, addNote]})
52
+ expect(first.stopReason).toBe("tool_use")
53
+ expect(first.toolCalls).toHaveLength(1)
54
+ const call = first.toolCalls![0]
55
+ expect(call).toMatchObject({name: "com_glassly_notes__add_note", input: {text: "add note buy milk"}})
56
+
57
+ const second = await session.ai.complete({
58
+ messages: [
59
+ {role: "user", content: "add note buy milk"},
60
+ {role: "assistant", content: "", toolCalls: first.toolCalls},
61
+ {role: "user", content: "", toolResults: [{toolCallId: call.id, content: "Saved"}]},
62
+ ],
63
+ tools: [startMiniapp, addNote],
64
+ })
65
+ expect(second.stopReason).toBe("end_turn")
66
+ expect(second.text).toBe("[glassly sim] tool results: Saved")
67
+ })
68
+
69
+ test("answers a plain prompt with marked text", async () => {
70
+ const {session} = await connect(permissions("AI"))
71
+ expect(await session.ai.prompt("What time is it?")).toBe('[glassly sim] you said "What time is it?"')
72
+ })
73
+
74
+ test("generate ticks progress and returns text without images", async () => {
75
+ const {session} = await connect(permissions("AI"))
76
+ const ticks: string[] = []
77
+ const result = await session.ai.generate({prompt: "a cat"}, {onProgress: (p) => ticks.push(p.activity)})
78
+ expect(ticks).toEqual(["drawing"])
79
+ expect(result).toMatchObject({text: '[glassly sim] generated for "a cat"', images: [], stopReason: "end_turn"})
80
+ })
81
+
82
+ test("glassly sim fail sends the phone's quota message", async () => {
83
+ const {session, host} = await connect(permissions("AI"))
84
+ host.failNext("ai_complete", "QUOTA_EXCEEDED")
85
+ await expect(session.ai.prompt("hi")).rejects.toMatchObject({
86
+ code: "QUOTA_EXCEEDED",
87
+ message:
88
+ "AI credits are used up. Get more in Settings > Membership, or add your own API key in Settings > Privacy > API Keys.",
89
+ })
90
+ })
91
+ })
92
+
93
+ describe("keys", () => {
94
+ afterEach(() => {
95
+ delete process.env.GLASSLY_SIM_KEY_OPENAI
96
+ })
97
+
98
+ test("serves a key from its environment variable without logging it", async () => {
99
+ process.env.GLASSLY_SIM_KEY_OPENAI = "sk-test-value"
100
+ const {session, logs} = await connect(permissions("API_KEYS"))
101
+ expect(await session.keys.get("openai")).toBe("sk-test-value")
102
+ expect(await session.keys.get("gemini")).toBeNull()
103
+ expect(logs().join("\n")).not.toContain("sk-test-value")
104
+ })
105
+ })
106
+
107
+ describe("calendar", () => {
108
+ test("lists sample events inside the window, up to the limit", async () => {
109
+ const {session} = await connect(permissions("CALENDAR"))
110
+ const now = Date.now()
111
+ const day = await session.phone.calendar.listEvents({startsAt: new Date(now), endsAt: new Date(now + 12 * 3_600_000)})
112
+ expect(day.events.map((e) => e.title)).toEqual(["Team standup (glassly sim)", "Lunch with Sam (glassly sim)"])
113
+ expect(day.events[0].links).toEqual(["https://meet.example.com/glassly-sim"])
114
+
115
+ const one = await session.phone.calendar.listEvents({
116
+ startsAt: new Date(now),
117
+ endsAt: new Date(now + 2 * 24 * 3_600_000),
118
+ limit: 1,
119
+ })
120
+ expect(one.events).toHaveLength(1)
121
+ expect(one.truncated).toBe(true)
122
+ })
123
+ })
124
+
125
+ describe("system", () => {
126
+ test("web login completes with a value for each named cookie", async () => {
127
+ const {session} = await connect()
128
+ const result = await session.system.webLogin({
129
+ url: "https://example.com/login",
130
+ doneUrlPrefix: "https://example.com/home",
131
+ cookies: ["sid"],
132
+ })
133
+ expect(result).toMatchObject({status: "completed", cookies: {sid: "glassly-sim-sid"}, url: "https://example.com/home"})
134
+ })
135
+
136
+ test("scan QR answers the scanned data", async () => {
137
+ const {session} = await connect()
138
+ expect(await session.system.scanQr()).toMatchObject({data: "glassly-sim-qr"})
139
+ })
140
+ })