@deep-podder/dsh-remote-mobile 0.0.5 → 0.0.7

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.
Files changed (46) hide show
  1. package/PROTOCOL.md +97 -9
  2. package/README.md +49 -12
  3. package/SECURITY.md +8 -0
  4. package/auth.js +16 -0
  5. package/bridge.js +336 -9
  6. package/client.js +6 -1
  7. package/docs/api-notes.md +12 -4
  8. package/docs/engineering-audit.md +252 -0
  9. package/docs/reference-chat-demo.md +41 -0
  10. package/docs/screenshots/chat-light.png +0 -0
  11. package/docs/screenshots/commands-dark.png +0 -0
  12. package/docs/screenshots/pairing-light.png +0 -0
  13. package/docs/screenshots/sessions-light.png +0 -0
  14. package/docs/verification.md +40 -4
  15. package/index.js +1 -1
  16. package/mobile/README.md +81 -35
  17. package/mobile/app.js +4268 -906
  18. package/mobile/index.html +28 -17
  19. package/mobile/lib/away.js +45 -0
  20. package/mobile/lib/constants.js +5 -0
  21. package/mobile/lib/errors.js +42 -0
  22. package/mobile/lib/format.js +94 -0
  23. package/mobile/lib/lock.js +21 -0
  24. package/mobile/manifest.webmanifest +24 -0
  25. package/mobile/mock-server.js +442 -17
  26. package/mobile/model/history.js +119 -0
  27. package/mobile/model/question.js +318 -0
  28. package/mobile/model/turns.js +57 -0
  29. package/mobile/styles/base.css +143 -0
  30. package/mobile/{styles.css → styles/components.css} +1170 -826
  31. package/mobile/styles/overrides.css +269 -0
  32. package/mobile/styles/tokens.css +169 -0
  33. package/mobile/sw.js +28 -4
  34. package/package.json +34 -20
  35. package/protocol.js +87 -0
  36. package/push-policy.js +58 -0
  37. package/server/frames/activity.js +110 -0
  38. package/server/frames/connection.js +21 -0
  39. package/server/frames/history.js +23 -0
  40. package/server/frames/interactions.js +77 -0
  41. package/server/frames/prompts.js +29 -0
  42. package/server/frames/queue.js +58 -0
  43. package/server/frames/sessions.js +201 -0
  44. package/server/frames/settings.js +38 -0
  45. package/server/frames/subagents.js +50 -0
  46. package/server.js +341 -423
package/push-policy.js ADDED
@@ -0,0 +1,58 @@
1
+ // Per-device Web Push policy. Evaluated by the Host so preferences still work
2
+ // when the phone page is closed. Pure and unit-testable.
3
+ export const DEFAULT_PUSH_PREFS = Object.freeze({
4
+ muted: false,
5
+ decisionsOnly: false,
6
+ quietStart: null,
7
+ quietEnd: null,
8
+ utcOffsetMinutes: 0,
9
+ })
10
+
11
+ function hour(value) {
12
+ if (value === null || value === undefined || value === '') return null
13
+ const n = Number(value)
14
+ if (!Number.isInteger(n) || n < 0 || n > 23) return null
15
+ return n
16
+ }
17
+
18
+ export function normalizePushPrefs(raw) {
19
+ const input = raw && typeof raw === 'object' ? raw : {}
20
+ const offset = Number(input.utcOffsetMinutes)
21
+ const start = hour(input.quietStart)
22
+ const end = hour(input.quietEnd)
23
+ return {
24
+ muted: input.muted === true,
25
+ decisionsOnly: input.decisionsOnly === true,
26
+ quietStart: start,
27
+ quietEnd: end,
28
+ utcOffsetMinutes:
29
+ Number.isFinite(offset) && Math.abs(offset) <= 14 * 60 ? Math.round(offset) : 0,
30
+ }
31
+ }
32
+
33
+ function localHour(prefs, now) {
34
+ const shifted = new Date(now + (Number(prefs.utcOffsetMinutes) || 0) * 60_000)
35
+ return shifted.getUTCHours()
36
+ }
37
+
38
+ export function inQuietHours(prefs, now = Date.now()) {
39
+ const start = prefs?.quietStart
40
+ const end = prefs?.quietEnd
41
+ if (!Number.isInteger(start) || !Number.isInteger(end) || start === end) return false
42
+ const h = localHour(prefs, now)
43
+ if (start < end) return h >= start && h < end
44
+ return h >= start || h < end
45
+ }
46
+
47
+ /** @param {'approval'|'question'|'reply'|'step'|'test'|string} kind */
48
+ export function pushAllowed(prefs, kind, now = Date.now()) {
49
+ if (!prefs) return true
50
+ // A test push must always prove delivery, whatever the preferences say.
51
+ if (kind === 'test') return true
52
+ if (prefs.muted === true) return false
53
+ if (prefs.decisionsOnly === true && kind !== 'approval' && kind !== 'question') {
54
+ return false
55
+ }
56
+ if (inQuietHours(prefs, now)) return false
57
+ return true
58
+ }
@@ -0,0 +1,110 @@
1
+ // activity frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ export const activityFrameMethods = {
3
+ async onContextGetFrame(peer, frame, type) {
4
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
5
+ const result = await this.callBridgeResult('contextFor', frame.sessionId)
6
+ if (!result.ok) {
7
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
8
+ return
9
+ }
10
+ peer.sendObject({
11
+ type: 'context',
12
+ sessionId: frame.sessionId,
13
+ usedTokens: result.value?.usedTokens ?? null,
14
+ pressureTokens: result.value?.pressureTokens ?? null,
15
+ projectedTokens: result.value?.projectedTokens ?? null,
16
+ contextWindow: result.value?.contextWindow ?? null,
17
+ basis: typeof result.value?.basis === 'string' ? result.value.basis : 'unknown',
18
+ tokenUsage: result.value?.tokenUsage ?? null,
19
+ })
20
+ return
21
+ },
22
+ async onChangesListFrame(peer, frame, type) {
23
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
24
+ const result = await this.callBridgeResult('changesList', frame.sessionId, frame.limit)
25
+ if (!result.ok) {
26
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
27
+ return
28
+ }
29
+ peer.sendObject({
30
+ type: 'changes',
31
+ sessionId: frame.sessionId,
32
+ items: Array.isArray(result.value?.items) ? result.value.items : [],
33
+ })
34
+ return
35
+ },
36
+ async onChangesDiffFrame(peer, frame, type) {
37
+ if (typeof frame.sessionId !== 'string' || !Number.isInteger(frame.seq) || !Number.isInteger(frame.index)) {
38
+ throw new Error('sessionId, seq and index required')
39
+ }
40
+ const result = await this.callBridgeResult('changesDiff', frame.sessionId, frame.seq, frame.index)
41
+ if (!result.ok) {
42
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
43
+ return
44
+ }
45
+ peer.sendObject({
46
+ type: 'changes.diff',
47
+ sessionId: frame.sessionId,
48
+ seq: result.value?.seq ?? frame.seq,
49
+ index: result.value?.index ?? frame.index,
50
+ kind: typeof result.value?.kind === 'string' ? result.value.kind : 'text',
51
+ path: typeof result.value?.path === 'string' ? result.value.path : '',
52
+ coarse: result.value?.coarse === true,
53
+ hunks: Array.isArray(result.value?.hunks) ? result.value.hunks : [],
54
+ })
55
+ return
56
+ },
57
+ async onCommandsListFrame(peer, frame, type) {
58
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
59
+ const result = await this.callBridgeResult('listCommands', frame.sessionId)
60
+ if (!result.ok) {
61
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
62
+ return
63
+ }
64
+ peer.sendObject({
65
+ type: 'commands',
66
+ sessionId: frame.sessionId,
67
+ available: result.value?.available === true,
68
+ items: Array.isArray(result.value?.items) ? result.value.items : [],
69
+ reason: typeof result.value?.reason === 'string' ? result.value.reason : null,
70
+ })
71
+ return
72
+ },
73
+ async onCommandsExecuteFrame(peer, frame, type) {
74
+ if (typeof frame.sessionId !== 'string' || typeof frame.line !== 'string') {
75
+ throw new Error('sessionId and line required')
76
+ }
77
+ const result = await this.callBridgeResult('executeCommand', frame.sessionId, frame.line)
78
+ if (!result.ok) {
79
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
80
+ return
81
+ }
82
+ peer.sendObject({
83
+ type: 'command.result',
84
+ sessionId: frame.sessionId,
85
+ commandId: result.value?.commandId ?? null,
86
+ name: typeof result.value?.name === 'string' ? result.value.name : '',
87
+ kind: result.value?.kind === 'error' ? 'error' : 'success',
88
+ text: typeof result.value?.text === 'string' ? result.value.text : null,
89
+ sourceEventSeq: result.value?.sourceEventSeq ?? null,
90
+ })
91
+ return
92
+ },
93
+ async onLimitsFrame(peer, frame, type) {
94
+ const result = await this.callBridgeResult('imageLimits')
95
+ peer.sendObject({
96
+ type: 'limits',
97
+ image: result.ok && result.value && typeof result.value === 'object' ? result.value.image ?? null : null,
98
+ })
99
+ return
100
+ },
101
+ }
102
+
103
+ export const activityFrameMap = {
104
+ 'context.get': 'onContextGetFrame',
105
+ 'changes.list': 'onChangesListFrame',
106
+ 'changes.diff': 'onChangesDiffFrame',
107
+ 'commands.list': 'onCommandsListFrame',
108
+ 'commands.execute': 'onCommandsExecuteFrame',
109
+ 'limits': 'onLimitsFrame',
110
+ }
@@ -0,0 +1,21 @@
1
+ // connection frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ export const connectionFrameMethods = {
3
+ async onPingFrame(peer, frame, type) {
4
+ peer.sendObject({ type: 'pong', t: frame.t ?? 0 })
5
+ return
6
+ },
7
+ async onClientVisibilityFrame(peer, frame, type) {
8
+ // The SPA reports whether its page is visible (foreground). While any
9
+ // phone page is visible no Web Push is sent; unknown/backgrounded is the
10
+ // default so a phone that never reports still receives notifications.
11
+ const visible = frame.visible === true
12
+ this.deviceVisibility.set(peer.device.id, visible)
13
+ peer.sendObject({ type: 'visibility', visible })
14
+ return
15
+ },
16
+ }
17
+
18
+ export const connectionFrameMap = {
19
+ 'ping': 'onPingFrame',
20
+ 'client.visibility': 'onClientVisibilityFrame',
21
+ }
@@ -0,0 +1,23 @@
1
+ // history frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ import { HISTORY_DEFAULT_LIMIT, HISTORY_MAX_LIMIT } from '../../protocol.js';
3
+ export const historyFrameMethods = {
4
+ async onHistoryLoadFrame(peer, frame, type) {
5
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
6
+ const limit = Number.isInteger(frame.limit)
7
+ ? Math.min(Math.max(frame.limit, 1), HISTORY_MAX_LIMIT)
8
+ : HISTORY_DEFAULT_LIMIT
9
+ const items = await this.callBridge('history', frame.sessionId, { limit })
10
+ peer.sendObject({
11
+ type: 'history',
12
+ sessionId: frame.sessionId,
13
+ items: Array.isArray(items) ? items : [],
14
+ // Best effort: a full page means older events may exist.
15
+ hasMore: Array.isArray(items) && items.length >= limit,
16
+ })
17
+ return
18
+ },
19
+ }
20
+
21
+ export const historyFrameMap = {
22
+ 'history.load': 'onHistoryLoadFrame',
23
+ }
@@ -0,0 +1,77 @@
1
+ // interactions frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ const interactionMethods = {
3
+ async onApprovalAnswerFrame(peer, frame, type) {
4
+ if (typeof frame.requestId !== 'string') throw new Error('requestId required')
5
+ // Per-device permission: a device the owner restricted may not answer.
6
+ if (peer.device.canApprove === false) {
7
+ peer.sendObject({
8
+ type: 'error',
9
+ code: 'forbidden',
10
+ message: 'this device is not allowed to answer approvals',
11
+ })
12
+ return
13
+ }
14
+ const decision = frame.decision === 'allow' ? 'allow' : frame.decision === 'reject' ? 'reject' : null
15
+ if (decision === null) throw new Error('decision must be allow or reject')
16
+ this.answerOrigins.set(frame.requestId, 'phone')
17
+ let accepted
18
+ try {
19
+ accepted = await this.callBridge('answerApproval', frame.requestId, decision)
20
+ } finally {
21
+ this.answerOrigins.delete(frame.requestId)
22
+ }
23
+ this.appendDebug(`approval.answer req=${frame.requestId} decision=${decision} accepted=${accepted !== false}`)
24
+ if (accepted === false) {
25
+ peer.sendObject({ type: 'error', code: 'not-found', message: 'approval is no longer pending' })
26
+ }
27
+ return
28
+ },
29
+ async onQuestionAnswerFrame(peer, frame, type) {
30
+ if (!Array.isArray(frame.answers)) throw new Error('answers required')
31
+ const requestId = typeof frame.requestId === 'string' ? frame.requestId : undefined
32
+ if (requestId !== undefined) this.answerOrigins.set(requestId, 'phone')
33
+ let result
34
+ try {
35
+ result = await this.callBridge('answerQuestion', {
36
+ requestId,
37
+ sessionId: typeof frame.sessionId === 'string' ? frame.sessionId : undefined,
38
+ callId: typeof frame.callId === 'string' ? frame.callId : undefined,
39
+ answers: frame.answers,
40
+ })
41
+ } finally {
42
+ if (requestId !== undefined) this.answerOrigins.delete(requestId)
43
+ }
44
+ if (result === false) {
45
+ peer.sendObject({ type: 'error', code: 'not-found', message: 'the question is no longer open' })
46
+ }
47
+ return
48
+ },
49
+ async onQuestionCancelFrame(peer, frame, type) {
50
+ const requestId = typeof frame.requestId === 'string' ? frame.requestId : undefined
51
+ if (requestId === undefined) throw new Error('requestId required')
52
+ const result = await this.callBridgeResult('cancelQuestion', {
53
+ requestId,
54
+ callId: typeof frame.callId === 'string' ? frame.callId : undefined,
55
+ })
56
+ if (!result.ok || result.value !== true) {
57
+ peer.sendObject({ type: 'error', code: 'not-found', message: 'the question is no longer open' })
58
+ return
59
+ }
60
+ this.appendDebug(`question.cancel req=${requestId}`)
61
+ return
62
+ },
63
+ }
64
+
65
+ export const interactionsFrameMethods = {
66
+ ...interactionMethods,
67
+ async onPendingListFrame(peer, frame, type) {
68
+ peer.sendObject({ type: 'pending', ...this.pendingSnapshot() })
69
+ },
70
+ }
71
+
72
+ export const interactionsFrameMap = {
73
+ 'approval.answer': 'onApprovalAnswerFrame',
74
+ 'question.answer': 'onQuestionAnswerFrame',
75
+ 'question.cancel': 'onQuestionCancelFrame',
76
+ 'pending.list': 'onPendingListFrame',
77
+ }
@@ -0,0 +1,29 @@
1
+ // prompts frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ export const promptsFrameMethods = {
3
+ async onPromptFrame(peer, frame, type) {
4
+ const hasText = typeof frame.text === 'string' && frame.text.trim().length > 0
5
+ const hasAttachments = Array.isArray(frame.attachments) && frame.attachments.length > 0
6
+ if (typeof frame.sessionId !== 'string' || (!hasText && !hasAttachments)) {
7
+ throw new Error('sessionId and text or attachments required')
8
+ }
9
+ await this.callBridge(
10
+ 'prompt',
11
+ frame.sessionId,
12
+ typeof frame.text === 'string' ? frame.text : '',
13
+ typeof frame.requestId === 'string' ? frame.requestId : undefined,
14
+ Array.isArray(frame.attachments) ? frame.attachments : undefined,
15
+ frame.mode === 'steer' ? 'steer' : 'queue',
16
+ )
17
+ return
18
+ },
19
+ async onStopFrame(peer, frame, type) {
20
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
21
+ await this.callBridge('cancel', frame.sessionId)
22
+ return
23
+ },
24
+ }
25
+
26
+ export const promptsFrameMap = {
27
+ 'prompt': 'onPromptFrame',
28
+ 'stop': 'onStopFrame',
29
+ }
@@ -0,0 +1,58 @@
1
+ // queue frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ export const queueFrameMethods = {
3
+ async onQueueListFrame(peer, frame, type) {
4
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
5
+ const result = await this.callBridgeResult('listQueue', frame.sessionId)
6
+ if (!result.ok) {
7
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
8
+ return
9
+ }
10
+ peer.sendObject({
11
+ type: 'queue',
12
+ sessionId: frame.sessionId,
13
+ items: Array.isArray(result.value?.items) ? result.value.items : [],
14
+ pendingInput: Array.isArray(result.value?.pendingInput) ? result.value.pendingInput : [],
15
+ running: result.value?.running === true,
16
+ active: result.value?.active === true,
17
+ })
18
+ return
19
+ },
20
+ async onQueueUpdateFrame(peer, frame, type) {
21
+ if (typeof frame.sessionId !== 'string' || typeof frame.itemId !== 'string') {
22
+ throw new Error('sessionId and itemId required')
23
+ }
24
+ const action = frame.action === 'edit' || frame.action === 'remove' || frame.action === 'steer'
25
+ ? frame.action
26
+ : null
27
+ if (action === null) throw new Error('action must be edit, remove or steer')
28
+ const result = await this.callBridgeResult(
29
+ 'updateQueue',
30
+ frame.sessionId,
31
+ frame.itemId,
32
+ action,
33
+ typeof frame.text === 'string' ? frame.text : undefined,
34
+ )
35
+ if (!result.ok) {
36
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
37
+ return
38
+ }
39
+ // Ack only to the requesting device; every device learns the new queue
40
+ // from the bridge's own `queue.changed` push. Read the live state once
41
+ // more so the ack carries the full queue-frame shape.
42
+ const state = await this.callBridgeResult('listQueue', frame.sessionId)
43
+ peer.sendObject({
44
+ type: 'queue',
45
+ sessionId: frame.sessionId,
46
+ items: Array.isArray(result.value?.items) ? result.value.items : [],
47
+ pendingInput: state.ok && Array.isArray(state.value?.pendingInput) ? state.value.pendingInput : [],
48
+ running: state.ok ? state.value?.running === true : true,
49
+ active: state.ok ? state.value?.active === true : true,
50
+ })
51
+ return
52
+ },
53
+ }
54
+
55
+ export const queueFrameMap = {
56
+ 'queue.list': 'onQueueListFrame',
57
+ 'queue.update': 'onQueueUpdateFrame',
58
+ }
@@ -0,0 +1,201 @@
1
+ // sessions frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ export const sessionsFrameMethods = {
3
+ async onSessionsListFrame(peer, frame, type) {
4
+ const sessions = await this.callBridge('listSessions')
5
+ peer.sendObject({
6
+ type: 'sessions',
7
+ sessions: Array.isArray(sessions) ? sessions : [],
8
+ workspaces: await this.fetchWorkspaces(),
9
+ activeSessionId: null,
10
+ })
11
+ return
12
+ },
13
+ async onSessionsOpenFrame(peer, frame, type) {
14
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
15
+ const opened = await this.callBridge('openSession', frame.sessionId)
16
+ peer.sendObject({
17
+ type: 'session.opened',
18
+ session: opened?.session ?? null,
19
+ history: opened?.history ?? [],
20
+ queue: Array.isArray(opened?.queue) ? opened.queue : [],
21
+ context: opened?.context ?? null,
22
+ })
23
+ return
24
+ },
25
+ async onSessionsCreateFrame(peer, frame, type) {
26
+ const cwd = typeof frame.cwd === 'string' ? frame.cwd.trim() : ''
27
+ const workspaceId = typeof frame.workspaceId === 'string' ? frame.workspaceId.trim() : ''
28
+ const created = await this.callBridge('createSession', {
29
+ preset: typeof frame.preset === 'string' ? frame.preset : undefined,
30
+ // A project files the new Session under that Workspace; the Host rejects
31
+ // both fields at once, so the project wins over the directory.
32
+ workspaceId: workspaceId.length > 0 ? workspaceId : undefined,
33
+ cwd: workspaceId.length === 0 && cwd.length > 0 ? cwd : undefined,
34
+ })
35
+ peer.sendObject({
36
+ type: 'sessions',
37
+ sessions: await this.fetchSessions(),
38
+ workspaces: await this.fetchWorkspaces(),
39
+ activeSessionId: created?.id ?? null,
40
+ })
41
+ // Creating a session is also opening it: send the same authoritative
42
+ // session.opened payload the explicit open route uses, so the phone never
43
+ // sits on a "Creating session…" timer waiting for a frame that never comes.
44
+ if (typeof created?.id === 'string' && created.id.length > 0) {
45
+ let opened = null
46
+ try {
47
+ opened = await this.callBridge('openSession', created.id)
48
+ } catch {
49
+ // The session exists even if the follow-up open failed; opening the
50
+ // created row is better than leaving the phone on a create timeout.
51
+ opened = null
52
+ }
53
+ peer.sendObject({
54
+ type: 'session.opened',
55
+ session: opened?.session ?? created,
56
+ history: Array.isArray(opened?.history) ? opened.history : [],
57
+ queue: Array.isArray(opened?.queue) ? opened.queue : [],
58
+ context: opened?.context ?? null,
59
+ })
60
+ }
61
+ return
62
+ },
63
+ async onSessionsSearchFrame(peer, frame, type) {
64
+ if (typeof frame.query !== 'string') throw new Error('query required')
65
+ const result = await this.callBridgeResult('searchSessions', frame.query)
66
+ if (!result.ok) {
67
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
68
+ return
69
+ }
70
+ peer.sendObject({
71
+ type: 'sessions.found',
72
+ query: result.value?.query ?? frame.query,
73
+ results: Array.isArray(result.value?.results) ? result.value.results : [],
74
+ hasMore: result.value?.hasMore === true,
75
+ })
76
+ return
77
+ },
78
+ async onSessionRenameFrame(peer, frame, type) {
79
+ if (typeof frame.sessionId !== 'string' || typeof frame.title !== 'string') {
80
+ throw new Error('sessionId and title required')
81
+ }
82
+ const result = await this.callBridgeResult('renameSession', frame.sessionId, frame.title)
83
+ if (!result.ok) {
84
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
85
+ return
86
+ }
87
+ const sessions = await this.fetchSessions()
88
+ const session = sessions.find((item) => item && item.id === frame.sessionId) ?? null
89
+ this.broadcast({
90
+ type: 'session.updated',
91
+ sessionId: frame.sessionId,
92
+ title: result.value?.title ?? frame.title,
93
+ session,
94
+ })
95
+ this.broadcast({ type: 'sessions', sessions, workspaces: await this.fetchWorkspaces(), activeSessionId: null })
96
+ return
97
+ },
98
+ async onSessionForkFrame(peer, frame, type) {
99
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
100
+ const atSeq = Number.isInteger(frame.atSeq) && frame.atSeq >= 0 ? frame.atSeq : undefined
101
+ const result = await this.callBridgeResult('forkSession', frame.sessionId, atSeq)
102
+ if (!result.ok) {
103
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
104
+ return
105
+ }
106
+ const forkId = result.value?.sessionId
107
+ this.broadcastSessionRefresh()
108
+ if (typeof forkId !== 'string') return
109
+ // Open the fork for the requesting phone through the normal path; the
110
+ // other devices get the new row from the `sessions` refresh.
111
+ try {
112
+ const opened = await this.callBridge('openSession', forkId)
113
+ peer.sendObject({
114
+ type: 'session.opened',
115
+ session: opened?.session ?? null,
116
+ history: opened?.history ?? [],
117
+ queue: Array.isArray(opened?.queue) ? opened.queue : [],
118
+ context: opened?.context ?? null,
119
+ })
120
+ } catch (error) {
121
+ peer.sendObject({
122
+ type: 'error',
123
+ code: 'internal',
124
+ message: `the fork was created but could not be opened: ${error instanceof Error ? error.message : String(error)}`,
125
+ })
126
+ }
127
+ return
128
+ },
129
+ async onSessionAttachFrame(peer, frame, type) {
130
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
131
+ if (typeof frame.workspaceId !== 'string') throw new Error('workspaceId required')
132
+ const result = await this.callBridgeResult(
133
+ 'attachSessionToWorkspace',
134
+ frame.sessionId,
135
+ frame.workspaceId,
136
+ )
137
+ if (!result.ok) {
138
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
139
+ return
140
+ }
141
+ peer.sendObject({
142
+ type: 'session.attached',
143
+ sessionId: frame.sessionId,
144
+ workspaceId: result.value?.workspaceId ?? frame.workspaceId,
145
+ })
146
+ this.broadcastSessionRefresh()
147
+ return
148
+ },
149
+ async onSessionUnpinFrame(peer, frame, type) {
150
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
151
+ const result = await this.callBridgeResult('setSessionPinned', frame.sessionId, type === 'session.pin')
152
+ if (!result.ok) {
153
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
154
+ return
155
+ }
156
+ peer.sendObject({
157
+ type: 'session.pinned',
158
+ sessionId: frame.sessionId,
159
+ pinned: result.value?.pinned === true,
160
+ archived: result.value?.archived === true,
161
+ })
162
+ this.broadcastSessionRefresh()
163
+ return
164
+ },
165
+ async onSessionUnarchiveFrame(peer, frame, type) {
166
+ if (typeof frame.sessionId !== 'string') throw new Error('sessionId required')
167
+ const stopActivity = frame.stopActivity === true
168
+ const result = await this.callBridgeResult(
169
+ 'setSessionArchived',
170
+ frame.sessionId,
171
+ type === 'session.archive',
172
+ stopActivity,
173
+ )
174
+ if (!result.ok) {
175
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
176
+ return
177
+ }
178
+ peer.sendObject({
179
+ type: 'session.archived',
180
+ sessionId: frame.sessionId,
181
+ archived: result.value?.archived === true,
182
+ pinned: result.value?.pinned === true,
183
+ })
184
+ this.broadcastSessionRefresh()
185
+ return
186
+ },
187
+ }
188
+
189
+ export const sessionsFrameMap = {
190
+ 'sessions.list': 'onSessionsListFrame',
191
+ 'sessions.open': 'onSessionsOpenFrame',
192
+ 'sessions.create': 'onSessionsCreateFrame',
193
+ 'sessions.search': 'onSessionsSearchFrame',
194
+ 'session.rename': 'onSessionRenameFrame',
195
+ 'session.fork': 'onSessionForkFrame',
196
+ 'session.attach': 'onSessionAttachFrame',
197
+ 'session.pin': 'onSessionUnpinFrame',
198
+ 'session.unpin': 'onSessionUnpinFrame',
199
+ 'session.archive': 'onSessionUnarchiveFrame',
200
+ 'session.unarchive': 'onSessionUnarchiveFrame',
201
+ }
@@ -0,0 +1,38 @@
1
+ // settings frame handlers. Mixed into RemoteMobileServer.prototype.
2
+ export const settingsFrameMethods = {
3
+ async onModelListFrame(peer, frame, type) {
4
+ const models = await this.callBridge('listModels')
5
+ peer.sendObject({ type: 'models', catalog: models?.catalog ?? [], current: models?.current ?? null })
6
+ return
7
+ },
8
+ async onModelSelectFrame(peer, frame, type) {
9
+ if (typeof frame.sessionId !== 'string' || typeof frame.provider !== 'string' || typeof frame.model !== 'string') {
10
+ throw new Error('sessionId, provider and model required')
11
+ }
12
+ await this.callBridge('selectModel', frame.sessionId, {
13
+ provider: frame.provider,
14
+ model: frame.model,
15
+ reasoningEffort: frame.reasoningEffort ?? null,
16
+ })
17
+ return
18
+ },
19
+ async onPresetListFrame(peer, frame, type) {
20
+ const presets = await this.callBridge('listPresets')
21
+ peer.sendObject({ type: 'presets', presets: presets?.presets ?? [], current: presets?.current ?? null })
22
+ return
23
+ },
24
+ async onPresetSelectFrame(peer, frame, type) {
25
+ if (typeof frame.sessionId !== 'string' || typeof frame.preset !== 'string') {
26
+ throw new Error('sessionId and preset required')
27
+ }
28
+ await this.callBridge('selectPreset', frame.sessionId, frame.preset)
29
+ return
30
+ },
31
+ }
32
+
33
+ export const settingsFrameMap = {
34
+ 'model.list': 'onModelListFrame',
35
+ 'model.select': 'onModelSelectFrame',
36
+ 'preset.list': 'onPresetListFrame',
37
+ 'preset.select': 'onPresetSelectFrame',
38
+ }
@@ -0,0 +1,50 @@
1
+ // Subagent catalog/status frames. Read-only: the phone observes child sessions,
2
+ // it never prompts or interrupts them.
3
+ export const subagentsFrameMethods = {
4
+ async onSubagentsListFrame(peer, frame, type) {
5
+ if (typeof frame.sessionId !== 'string' || frame.sessionId.length === 0) {
6
+ throw new Error('sessionId required')
7
+ }
8
+ const result = await this.callBridgeResult('listSubagents', frame.sessionId)
9
+ if (!result.ok) {
10
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
11
+ return
12
+ }
13
+ peer.sendObject({
14
+ type: 'subagents',
15
+ sessionId: frame.sessionId,
16
+ items: Array.isArray(result.value) ? result.value : [],
17
+ })
18
+ },
19
+
20
+ async onSubagentsHistoryFrame(peer, frame, type) {
21
+ if (typeof frame.subagentId !== 'string' || frame.subagentId.length === 0) {
22
+ throw new Error('subagentId required')
23
+ }
24
+ if (typeof frame.sessionId !== 'string' || frame.sessionId.length === 0) {
25
+ throw new Error('sessionId required')
26
+ }
27
+ const limit = Number.isInteger(frame.limit) ? Math.min(Math.max(frame.limit, 1), 200) : 100
28
+ const result = await this.callBridgeResult(
29
+ 'subagentHistory',
30
+ frame.sessionId,
31
+ frame.subagentId,
32
+ { limit },
33
+ )
34
+ if (!result.ok) {
35
+ peer.sendObject({ type: 'error', code: result.code, message: result.message })
36
+ return
37
+ }
38
+ peer.sendObject({
39
+ type: 'subagent.history',
40
+ sessionId: typeof frame.sessionId === 'string' ? frame.sessionId : null,
41
+ subagentId: frame.subagentId,
42
+ items: Array.isArray(result.value) ? result.value : [],
43
+ })
44
+ },
45
+ }
46
+
47
+ export const subagentsFrameMap = {
48
+ 'subagents.list': 'onSubagentsListFrame',
49
+ 'subagents.history': 'onSubagentsHistoryFrame',
50
+ }