@linxin666/dsh-remote-bridge 0.4.8 → 0.4.10
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.i18n.yaml +2 -2
- package/README.md +75 -18
- package/README.zh.md +74 -18
- package/lib/client.js +783 -260
- package/lib/client.js.map +1 -1
- package/lib/index.js +15849 -2913
- package/lib/types/client/PairEntry.d.ts +2 -2
- package/lib/types/client/PairPanel.d.ts +10 -2
- package/lib/types/client/UiPresentRow.d.ts +13 -0
- package/lib/types/client/icons.d.ts +5 -13
- package/lib/types/client/index.d.ts +4 -0
- package/lib/types/client/locales.d.ts +24 -15
- package/lib/types/client/semantic.d.ts +9 -5
- package/lib/types/client/toolview-slot.d.ts +68 -0
- package/lib/types/client/ui-card.d.ts +160 -0
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +106 -0
- package/lib/types/core/devices.d.ts +102 -2
- package/lib/types/core/extension.d.ts +13 -0
- package/lib/types/core/hub.d.ts +129 -15
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +328 -5
- package/lib/types/core/push.d.ts +186 -0
- package/lib/types/core/questions.d.ts +61 -9
- package/lib/types/core/session-meta.d.ts +55 -1
- package/lib/types/core/squads.d.ts +130 -0
- package/lib/types/core/turn-end-quiet.d.ts +135 -0
- package/lib/types/core/ui.d.ts +238 -0
- package/lib/types/core/usage-stats.d.ts +160 -0
- package/lib/types/core/usage.d.ts +294 -0
- package/lib/types/host/apns.d.ts +151 -0
- package/lib/types/host/squads-ledger.d.ts +39 -0
- package/lib/types/host/squads.d.ts +77 -0
- package/lib/types/host/ui-tools.d.ts +80 -0
- package/lib/types/host/usage-stats.d.ts +75 -0
- package/lib/types/host/watchers.d.ts +95 -0
- package/lib/types/index.d.ts +47 -3
- package/package.json +15 -14
- package/src/client/PairEntry.tsx +58 -31
- package/src/client/PairPanel.tsx +181 -97
- package/src/client/UiPresentRow.tsx +149 -0
- package/src/client/icons.tsx +17 -60
- package/src/client/index.ts +11 -0
- package/src/client/locales.ts +36 -24
- package/src/client/panel.module.css +239 -186
- package/src/client/semantic.ts +10 -6
- package/src/client/toolview-slot.ts +84 -0
- package/src/client/ui-card.ts +368 -0
- package/src/client/ui-present.module.css +87 -0
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +811 -12
- package/src/core/devices.ts +337 -5
- package/src/core/extension.ts +20 -0
- package/src/core/hub.ts +518 -43
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +370 -5
- package/src/core/push.ts +385 -0
- package/src/core/questions.ts +81 -10
- package/src/core/session-meta.ts +73 -1
- package/src/core/squads.ts +345 -0
- package/src/core/turn-end-quiet.ts +388 -0
- package/src/core/ui.ts +398 -0
- package/src/core/usage-stats.ts +424 -0
- package/src/core/usage.ts +937 -0
- package/src/core/wire.ts +8 -0
- package/src/host/apns.ts +320 -0
- package/src/host/squads-ledger.ts +105 -0
- package/src/host/squads.ts +206 -0
- package/src/host/ui-tools.ts +233 -0
- package/src/host/usage-stats.ts +155 -0
- package/src/host/watchers.ts +296 -0
- package/src/index.ts +327 -10
package/src/client/semantic.ts
CHANGED
|
@@ -14,13 +14,10 @@ export const PART_ENTRY = 'entry'
|
|
|
14
14
|
/** The modal panel root. */
|
|
15
15
|
export const PART_PANEL = 'panel'
|
|
16
16
|
|
|
17
|
-
/** The QR symbol's
|
|
17
|
+
/** The QR symbol's box — it also mirrors the encoded string. */
|
|
18
18
|
export const PART_QR = 'qr'
|
|
19
19
|
|
|
20
|
-
/**
|
|
21
|
-
export const PART_TOKEN = 'token'
|
|
22
|
-
|
|
23
|
-
/** A state notice: the origin warning, or a refusal the host raised. */
|
|
20
|
+
/** A refusal the host raised. */
|
|
24
21
|
export const PART_NOTICE = 'notice'
|
|
25
22
|
|
|
26
23
|
/** The mint-again button. */
|
|
@@ -32,6 +29,13 @@ export const PART_COPY = 'copy'
|
|
|
32
29
|
/**
|
|
33
30
|
* Attribute carrying the exact string the QR symbol encodes, so the operator
|
|
34
31
|
* (and a UI check) can compare it against the host route's answer without
|
|
35
|
-
* decoding the symbol.
|
|
32
|
+
* decoding the symbol. It is the only machine trace the panel renders.
|
|
36
33
|
*/
|
|
37
34
|
export const QR_VALUE_ATTR = 'data-dsh-qr-value'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Marks the QR part of a code that has run out of time. Expiry is state on the
|
|
38
|
+
* existing part, not a new part value: the cross-repository part enum is frozen
|
|
39
|
+
* and a plugin may not extend it for one visual state.
|
|
40
|
+
*/
|
|
41
|
+
export const EXPIRED_ATTR = 'data-expired'
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one seam through which this package registers its keyed Tool view.
|
|
3
|
+
*
|
|
4
|
+
* `tool.call.toolview` is declared by `@deepseek-ai/dsh-client-ui-tool`
|
|
5
|
+
* (`lib/types/client/contract/slots.d.ts`). That package is not installed in this
|
|
6
|
+
* workspace, so its types cannot be imported — a type-only import would itself be
|
|
7
|
+
* a new dependency, and a local `declare module` extension of the SDK's `SlotMap`
|
|
8
|
+
* would duplicate a contract another package owns and collide with the real
|
|
9
|
+
* declaration wherever both are loaded. This module therefore narrows
|
|
10
|
+
* `ctx.slots` to the two members the registration needs and states the slot's own
|
|
11
|
+
* shape locally:
|
|
12
|
+
*
|
|
13
|
+
* - the slot is keyed and session-scoped, and its key is the wire Tool name
|
|
14
|
+
* (`ui.present`), which is what makes one registration draw the card's body
|
|
15
|
+
* without shadowing the official `tool-call` renderer;
|
|
16
|
+
* - the owner publishes the call identity plus a stage union, mirrored here from
|
|
17
|
+
* the upstream `ToolCallPhaseProps`: `preparing`, `start` and `result`. The
|
|
18
|
+
* row reads the arguments at every stage but `result`, which is the only arm
|
|
19
|
+
* that carries the result node.
|
|
20
|
+
*
|
|
21
|
+
* Upstream changing that shape is a change to this file: the phase names, the
|
|
22
|
+
* keyed dispatch and the `locale` seat are the three facts the mirror carries.
|
|
23
|
+
*
|
|
24
|
+
* Technical debt, recorded rather than hidden: when this package next takes a
|
|
25
|
+
* dependency pass, adding `@deepseek-ai/dsh-client-ui-tool` as a devDependency
|
|
26
|
+
* lets this seam be replaced by `PropsRuntime<'tool.call.toolview'>` and by the
|
|
27
|
+
* typed `ctx.slots.register` overload directly.
|
|
28
|
+
* @module dsh-remote-bridge/client/toolview-slot
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import type { ComponentType } from 'react'
|
|
32
|
+
import { UiPresentRow, type UiPresentRowProps } from './UiPresentRow.tsx'
|
|
33
|
+
|
|
34
|
+
/** The keyed Tool view slot the official row dispatches into. */
|
|
35
|
+
export const TOOLVIEW_SLOT = 'tool.call.toolview'
|
|
36
|
+
|
|
37
|
+
/** The wire Tool name this package renders: the keyed cell it claims. */
|
|
38
|
+
export const PRESENT_TOOL = 'ui.present'
|
|
39
|
+
|
|
40
|
+
/** The registration options this seam needs. */
|
|
41
|
+
export interface ToolviewRegistration {
|
|
42
|
+
/** Exact SlotMap key. */
|
|
43
|
+
readonly name: 'tool.call.toolview'
|
|
44
|
+
/** Keyed cell: the wire Tool name the owner dispatches by. */
|
|
45
|
+
readonly key: string
|
|
46
|
+
/** Dictionary namespace the render machinery synthesizes `t` from. */
|
|
47
|
+
readonly locale: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The narrowed face of `ctx.slots` this package uses. */
|
|
51
|
+
export interface ToolviewSlotPort {
|
|
52
|
+
/** Run `seats` once the slot is declared; drop them if the declarer leaves. */
|
|
53
|
+
inject(name: string, seats: () => (() => void) | Iterable<() => void>): void
|
|
54
|
+
/** Contribute one view to one keyed cell; the answer is its disposer. */
|
|
55
|
+
register(options: ToolviewRegistration, view: ComponentType<UiPresentRowProps>): () => void
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The host this seam needs: the client Context, structurally. */
|
|
59
|
+
export interface ToolviewSlotHost {
|
|
60
|
+
readonly slots: unknown
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Contribute the `ui.present` card into the Tool row.
|
|
65
|
+
*
|
|
66
|
+
* The contribution is declaration-aware: `inject` waits for the official slot
|
|
67
|
+
* declaration, so a GUI composed without a Tool layer has no keyed cell and thus
|
|
68
|
+
* no card — that is the capability being absent, not an error to report. A
|
|
69
|
+
* registration refused by the registry (an occupied cell at the same priority)
|
|
70
|
+
* is swallowed the same way it is for this package's other seat, so one occupied
|
|
71
|
+
* slot never takes the sidebar entry down with it.
|
|
72
|
+
* @param host - the client Context.
|
|
73
|
+
* @param locale - the dictionary namespace this package registered.
|
|
74
|
+
*/
|
|
75
|
+
export function registerPresentToolview(host: ToolviewSlotHost, locale: string): void {
|
|
76
|
+
const slots = host.slots as unknown as ToolviewSlotPort
|
|
77
|
+
slots.inject(TOOLVIEW_SLOT, () => {
|
|
78
|
+
try {
|
|
79
|
+
return slots.register({ name: TOOLVIEW_SLOT, key: PRESENT_TOOL, locale }, UiPresentRow)
|
|
80
|
+
} catch {
|
|
81
|
+
return () => {}
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transcript reading of one `ui.present` call (protocol §35.10, §35.11):
|
|
3
|
+
* what the desktop row draws, decided without touching the DOM.
|
|
4
|
+
*
|
|
5
|
+
* A card has two sources and neither may be dropped. While the call is still
|
|
6
|
+
* running the arguments are the only material on the wire, so the page comes out
|
|
7
|
+
* of `args` and the header says the operator has not acted yet. Once the result
|
|
8
|
+
* lands, the presentation payload `meta.ui` is the card's own record — its
|
|
9
|
+
* `state`, its `title` and the very document that crossed — and the arguments
|
|
10
|
+
* are only the fallback for a result that carries no payload.
|
|
11
|
+
*
|
|
12
|
+
* The argument view is read by shape rather than by import: a toolview receives a
|
|
13
|
+
* lazy `PartialArguments` when the call arrived as argument text and a plain
|
|
14
|
+
* payload when the call was dispatched as an object, and §35.10 fixes neither. A
|
|
15
|
+
* field reader therefore answers from all three shapes it can meet (lazy view,
|
|
16
|
+
* JSON text, parsed object) so a page is never lost to a shape this row did not
|
|
17
|
+
* anticipate.
|
|
18
|
+
*
|
|
19
|
+
* Two rules live here rather than in the renderer. A document above 256 KiB is
|
|
20
|
+
* never handed to a frame (§35.2 caps it on this side too), and a frame message is
|
|
21
|
+
* read by shape alone: the sandboxed document has an opaque origin, so identity
|
|
22
|
+
* comes from the frame element the row owns, never from what the message claims.
|
|
23
|
+
* Nothing read here is forwarded anywhere — the desktop renders and does not
|
|
24
|
+
* answer (§35.11).
|
|
25
|
+
* @module dsh-remote-bridge/client/ui-card
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { UI_HTML_MAX_BYTES, type UiState } from '../core/ui.ts'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The Tool slot's stage literals, as the composing contract publishes them
|
|
32
|
+
* (`ToolCallPhaseProps`). `result` is the settled arm; every other stage still
|
|
33
|
+
* has the call's arguments only.
|
|
34
|
+
*/
|
|
35
|
+
export type PresentPhase = 'preparing' | 'start' | 'result'
|
|
36
|
+
|
|
37
|
+
/** One card as a row draws it, whichever source it came from. */
|
|
38
|
+
export interface PresentCard {
|
|
39
|
+
/** The whole self-contained document, `<!doctype html>` first. */
|
|
40
|
+
readonly html: string
|
|
41
|
+
/** Whether the card still holds the call (`open`) or is only a notice. */
|
|
42
|
+
readonly state: UiState
|
|
43
|
+
/** One header line; absent when the presenter named none. */
|
|
44
|
+
readonly title?: string
|
|
45
|
+
/** The presenter's suggested first height in points; the measurement wins. */
|
|
46
|
+
readonly height?: number
|
|
47
|
+
/** `"<sessionId>/<callId>"`, when the payload carries the card identity. */
|
|
48
|
+
readonly key?: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What one row read produced. */
|
|
52
|
+
export type PresentRead =
|
|
53
|
+
| { readonly kind: 'card'; readonly card: PresentCard }
|
|
54
|
+
/** A page over {@link UI_HTML_MAX_BYTES}: refused visibly, never rendered. */
|
|
55
|
+
| { readonly kind: 'oversize' }
|
|
56
|
+
/** No page in either source: the row keeps a neutral line instead of a card. */
|
|
57
|
+
| { readonly kind: 'empty' }
|
|
58
|
+
|
|
59
|
+
/** The call material one row can read at its stage. */
|
|
60
|
+
interface PresentBlock {
|
|
61
|
+
/** The lazy argument view (`ToolArgs`), or a parsed payload. */
|
|
62
|
+
readonly args?: unknown
|
|
63
|
+
/** Finished argument text, for a stage that publishes it beside the view. */
|
|
64
|
+
readonly argsRaw?: unknown
|
|
65
|
+
/** `ToolResultNode.meta`, carried verbatim by the protocol. */
|
|
66
|
+
readonly meta?: unknown
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** One theme token as it is handed to a card's document. */
|
|
70
|
+
export type ThemeToken = readonly [name: string, value: string]
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The theme tokens a card inherits from the GUI's own root element.
|
|
74
|
+
*
|
|
75
|
+
* Four roles and no more: the page needs a text colour it can trust, a quieter
|
|
76
|
+
* one, a surface and a border. A token the running theme does not define is
|
|
77
|
+
* simply not injected — the page then falls back to its own CSS, which is why an
|
|
78
|
+
* unreadable theme is a degraded look rather than a broken card.
|
|
79
|
+
*/
|
|
80
|
+
export const CARD_THEME_TOKENS = [
|
|
81
|
+
'--dsw-alias-label-primary',
|
|
82
|
+
'--dsw-alias-label-secondary',
|
|
83
|
+
'--dsw-alias-bg-layer-1',
|
|
84
|
+
'--dsw-alias-border-l2',
|
|
85
|
+
] as const
|
|
86
|
+
|
|
87
|
+
/** The one sandbox value a card frame is allowed (§35.7, §35.11). */
|
|
88
|
+
export const FRAME_SANDBOX = 'allow-scripts'
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The permissions a card frame is explicitly denied.
|
|
92
|
+
*
|
|
93
|
+
* §35.7 refuses camera, microphone, location and clipboard reads on both ends.
|
|
94
|
+
* A sandboxed document already has an opaque origin, and this policy states the
|
|
95
|
+
* denial where the browser enforces it rather than leaving it to the default.
|
|
96
|
+
*/
|
|
97
|
+
export const FRAME_PERMISSIONS = [
|
|
98
|
+
"camera 'none'",
|
|
99
|
+
"microphone 'none'",
|
|
100
|
+
"geolocation 'none'",
|
|
101
|
+
"clipboard-read 'none'",
|
|
102
|
+
].join('; ')
|
|
103
|
+
|
|
104
|
+
/** The message type the injected reporter uses to publish the content height. */
|
|
105
|
+
export const FRAME_HEIGHT_CHANNEL = 'dsh-ui-card-height'
|
|
106
|
+
|
|
107
|
+
/** The first height of a card whose presenter suggested none. */
|
|
108
|
+
export const CARD_DEFAULT_HEIGHT = 320
|
|
109
|
+
|
|
110
|
+
/** The smallest and largest heights a measured card may take, in CSS pixels. */
|
|
111
|
+
export const CARD_MIN_HEIGHT = 96
|
|
112
|
+
export const CARD_MAX_HEIGHT = 720
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Read one field as text out of every argument shape a toolview can receive.
|
|
116
|
+
* @param args - the lazy view, finished JSON text, or a parsed payload.
|
|
117
|
+
* @param name - the top-level field name.
|
|
118
|
+
* @returns the field's decoded string, or undefined when absent or not a string.
|
|
119
|
+
*/
|
|
120
|
+
function fieldText(args: unknown, name: string): string | undefined {
|
|
121
|
+
if (typeof args === 'string') {
|
|
122
|
+
const parsed = parseJson(args)
|
|
123
|
+
return parsed === undefined ? undefined : fieldText(parsed, name)
|
|
124
|
+
}
|
|
125
|
+
if (typeof args !== 'object' || args === null) return undefined
|
|
126
|
+
const view = args as Record<string, unknown> & { text?: unknown }
|
|
127
|
+
if (typeof view.text === 'function') {
|
|
128
|
+
const read: unknown = (view.text as (key: string) => unknown).call(args, name)
|
|
129
|
+
return typeof read === 'string' ? read : undefined
|
|
130
|
+
}
|
|
131
|
+
const direct = view[name]
|
|
132
|
+
return typeof direct === 'string' ? direct : undefined
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Read one field as any completed value out of the same three shapes.
|
|
137
|
+
* @param args - the lazy view, finished JSON text, or a parsed payload.
|
|
138
|
+
* @param name - the top-level field name.
|
|
139
|
+
* @returns the value, or undefined when absent.
|
|
140
|
+
*/
|
|
141
|
+
function fieldValue(args: unknown, name: string): unknown {
|
|
142
|
+
if (typeof args === 'string') {
|
|
143
|
+
const parsed = parseJson(args)
|
|
144
|
+
return parsed === undefined ? undefined : fieldValue(parsed, name)
|
|
145
|
+
}
|
|
146
|
+
if (typeof args !== 'object' || args === null) return undefined
|
|
147
|
+
const view = args as Record<string, unknown> & { value?: unknown }
|
|
148
|
+
if (typeof view.value === 'function') return (view.value as (key: string) => unknown).call(args, name)
|
|
149
|
+
return view[name]
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** @returns the parsed payload, or undefined when the text is not one object. */
|
|
153
|
+
function parseJson(text: string): unknown {
|
|
154
|
+
try {
|
|
155
|
+
return JSON.parse(text) as unknown
|
|
156
|
+
} catch {
|
|
157
|
+
return undefined
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** @returns one suggested height as a whole number of CSS pixels, when usable. */
|
|
162
|
+
function heightOf(raw: unknown): number | undefined {
|
|
163
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw) || raw <= 0) return undefined
|
|
164
|
+
return Math.ceil(raw)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Read the presentation payload a settled call carries (§35.10).
|
|
169
|
+
*
|
|
170
|
+
* The payload is the card's own record, so it is read only when it is one: a
|
|
171
|
+
* `html` that is not a string is no card at all, which keeps a malformed payload
|
|
172
|
+
* from becoming an empty frame. `state` outside the frozen pair reads as
|
|
173
|
+
* `settled` — a result-phase payload is a finished card by definition, and the
|
|
174
|
+
* `open` arm exists for a presenter that answers before its first event.
|
|
175
|
+
* @param meta - the result's `meta` value, carried verbatim.
|
|
176
|
+
* @returns the card, or undefined when this result carries no presentation.
|
|
177
|
+
*/
|
|
178
|
+
export function readPresentation(meta: unknown): PresentCard | undefined {
|
|
179
|
+
if (typeof meta !== 'object' || meta === null) return undefined
|
|
180
|
+
const ui = (meta as { ui?: unknown }).ui
|
|
181
|
+
if (typeof ui !== 'object' || ui === null) return undefined
|
|
182
|
+
const record = ui as Record<string, unknown>
|
|
183
|
+
const html = record.html
|
|
184
|
+
if (typeof html !== 'string') return undefined
|
|
185
|
+
const title = record.title
|
|
186
|
+
const key = record.key
|
|
187
|
+
const height = heightOf(record.height)
|
|
188
|
+
return {
|
|
189
|
+
html,
|
|
190
|
+
state: record.state === 'open' ? 'open' : 'settled',
|
|
191
|
+
...(typeof title === 'string' ? { title } : {}),
|
|
192
|
+
...(height === undefined ? {} : { height }),
|
|
193
|
+
...(typeof key === 'string' ? { key } : {}),
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Build the card a running call shows from its own arguments.
|
|
199
|
+
* @param block - the call material of the current stage.
|
|
200
|
+
* @returns the card, or undefined when the arguments carry no page.
|
|
201
|
+
*/
|
|
202
|
+
function argumentCard(block: PresentBlock): PresentCard | undefined {
|
|
203
|
+
const html = fieldText(block.args, 'html') ?? fieldText(block.argsRaw, 'html')
|
|
204
|
+
if (html === undefined) return undefined
|
|
205
|
+
const title = fieldText(block.args, 'title') ?? fieldText(block.argsRaw, 'title')
|
|
206
|
+
const height = heightOf(fieldValue(block.args, 'height') ?? fieldValue(block.argsRaw, 'height'))
|
|
207
|
+
return {
|
|
208
|
+
html,
|
|
209
|
+
state: 'open',
|
|
210
|
+
...(title === undefined ? {} : { title }),
|
|
211
|
+
...(height === undefined ? {} : { height }),
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @returns whether one document is within {@link UI_HTML_MAX_BYTES}. */
|
|
216
|
+
export function cardHtmlFits(html: string): boolean {
|
|
217
|
+
return new TextEncoder().encode(html).length <= UI_HTML_MAX_BYTES
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Decide what the row draws at one stage of the call.
|
|
222
|
+
*
|
|
223
|
+
* The settled stage prefers `meta.ui`, because that payload is the presentation
|
|
224
|
+
* the host published and carries its ending. A result without the payload falls
|
|
225
|
+
* back to the arguments — and then the card is still over, so it reads
|
|
226
|
+
* `settled`: the stage decides that, not a field that is absent.
|
|
227
|
+
* @param phase - the stage the Tool slot published.
|
|
228
|
+
* @param block - the call material of that stage.
|
|
229
|
+
* @returns a card, a refusal, or the neutral line.
|
|
230
|
+
*/
|
|
231
|
+
export function presentReadOf(phase: unknown, block: unknown): PresentRead {
|
|
232
|
+
if (typeof block !== 'object' || block === null) return { kind: 'empty' }
|
|
233
|
+
const material = block as PresentBlock
|
|
234
|
+
const card = phase === 'result'
|
|
235
|
+
? readPresentation(material.meta) ?? settledFromArguments(material)
|
|
236
|
+
: argumentCard(material)
|
|
237
|
+
if (card === undefined) return { kind: 'empty' }
|
|
238
|
+
return cardHtmlFits(card.html) ? { kind: 'card', card } : { kind: 'oversize' }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** @returns the argument page of a settled call, as a finished card. */
|
|
242
|
+
function settledFromArguments(material: PresentBlock): PresentCard | undefined {
|
|
243
|
+
const card = argumentCard(material)
|
|
244
|
+
return card === undefined ? undefined : { ...card, state: 'settled' }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Read the theme tokens off a computed style, skipping the ones it does not carry.
|
|
249
|
+
* @param style - the GUI root element's computed style.
|
|
250
|
+
* @returns the tokens, in {@link CARD_THEME_TOKENS} order.
|
|
251
|
+
*/
|
|
252
|
+
export function cardThemeTokens(style: { getPropertyValue(name: string): string }): readonly ThemeToken[] {
|
|
253
|
+
const tokens: ThemeToken[] = []
|
|
254
|
+
for (const name of CARD_THEME_TOKENS) {
|
|
255
|
+
let value = ''
|
|
256
|
+
try {
|
|
257
|
+
value = style.getPropertyValue(name)
|
|
258
|
+
} catch {
|
|
259
|
+
value = ''
|
|
260
|
+
}
|
|
261
|
+
const trimmed = value.trim()
|
|
262
|
+
if (trimmed !== '') tokens.push([name, trimmed])
|
|
263
|
+
}
|
|
264
|
+
return tokens
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** @returns the `<style>` handing the theme tokens to a card's document. */
|
|
268
|
+
function themeStyle(tokens: readonly ThemeToken[]): string {
|
|
269
|
+
if (tokens.length === 0) return ''
|
|
270
|
+
const body = tokens.map(([name, value]) => `${name}:${value}`).join(';')
|
|
271
|
+
return `<style data-dsh-card-theme>:root{${body}}</style>`
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The reporter a card's document runs to publish its own content height.
|
|
276
|
+
*
|
|
277
|
+
* The frame cannot be measured from the outside — the document is opaque and
|
|
278
|
+
* cross-origin — so the measurement has to be pushed out. It is deliberately the
|
|
279
|
+
* only code this row adds to a page: it reads a height and posts a number, and a
|
|
280
|
+
* page that removes it only loses the measurement, never the card.
|
|
281
|
+
* @param channel - the message type the parent listens for.
|
|
282
|
+
* @returns one script element as text.
|
|
283
|
+
*/
|
|
284
|
+
function heightReporter(channel: string): string {
|
|
285
|
+
const parts = [
|
|
286
|
+
'<script data-dsh-card-frame>(function(){',
|
|
287
|
+
'function post(){try{',
|
|
288
|
+
'var d=document.documentElement,b=document.body;',
|
|
289
|
+
'var h=Math.max(d?d.scrollHeight:0,b?b.scrollHeight:0);',
|
|
290
|
+
'if(h>0){parent.postMessage({type:' + JSON.stringify(channel) + ',height:h},"*")}',
|
|
291
|
+
'}catch(e){}}',
|
|
292
|
+
'window.addEventListener("load",post);',
|
|
293
|
+
'document.addEventListener("DOMContentLoaded",post);',
|
|
294
|
+
'try{if(typeof ResizeObserver==="function"){var ro=new ResizeObserver(post);',
|
|
295
|
+
'if(document.documentElement)ro.observe(document.documentElement);',
|
|
296
|
+
'if(document.body)ro.observe(document.body)}}catch(e){}',
|
|
297
|
+
'post()})()</script>',
|
|
298
|
+
]
|
|
299
|
+
return parts.join('')
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Put one snippet inside an existing element rather than before the document.
|
|
304
|
+
* @param html - the card document.
|
|
305
|
+
* @param closingTag - the tag to insert ahead of, lower case.
|
|
306
|
+
* @param snippet - the markup to insert.
|
|
307
|
+
* @returns the document with the snippet inside it.
|
|
308
|
+
*/
|
|
309
|
+
function injectBefore(html: string, closingTag: string, snippet: string): string {
|
|
310
|
+
if (snippet === '') return html
|
|
311
|
+
const at = html.toLowerCase().lastIndexOf(closingTag)
|
|
312
|
+
return at < 0 ? html + snippet : html.slice(0, at) + snippet + html.slice(at)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Build the document handed to a card's frame.
|
|
317
|
+
*
|
|
318
|
+
* The agent's page stays byte for byte what crossed; this only adds the theme
|
|
319
|
+
* variables its CSS may use and the height reporter. Insertion happens ahead of
|
|
320
|
+
* the closing tags rather than at the start of the string, because a snippet
|
|
321
|
+
* placed before `<!doctype html>` would put the document into quirks mode.
|
|
322
|
+
* @param html - the card document as the presenter wrote it.
|
|
323
|
+
* @param tokens - the theme tokens to hand the page.
|
|
324
|
+
* @param channel - the message type the reporter publishes on.
|
|
325
|
+
* @returns the frame document; the original page when it takes no injection.
|
|
326
|
+
*/
|
|
327
|
+
export function frameDocument(html: string, tokens: readonly ThemeToken[], channel: string = FRAME_HEIGHT_CHANNEL): string {
|
|
328
|
+
const style = themeStyle(tokens)
|
|
329
|
+
const withTheme = style === '' ? html : injectBefore(html, '</head>', style)
|
|
330
|
+
return injectBefore(withTheme, '</body>', heightReporter(channel))
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** One message a card's frame sent the parent. */
|
|
334
|
+
export type FrameEvent =
|
|
335
|
+
| { readonly kind: 'height'; readonly height: number }
|
|
336
|
+
/** Anything else the page posted: an action this desktop never answers. */
|
|
337
|
+
| { readonly kind: 'interaction' }
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Read one message a card's frame posted.
|
|
341
|
+
*
|
|
342
|
+
* Only the shape is read. The frame's origin is opaque, so the parent identifies
|
|
343
|
+
* the sender by the frame element it owns; what is left to decide here is what
|
|
344
|
+
* the payload means, and a page's own `{type, payload}` action means nothing
|
|
345
|
+
* beyond "the operator tried to act here".
|
|
346
|
+
* @param data - the message payload.
|
|
347
|
+
* @returns the event, or undefined when the message is not one this row reads.
|
|
348
|
+
*/
|
|
349
|
+
export function frameEventOf(data: unknown): FrameEvent | undefined {
|
|
350
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data)) return undefined
|
|
351
|
+
const record = data as Record<string, unknown>
|
|
352
|
+
if (record.type === FRAME_HEIGHT_CHANNEL) {
|
|
353
|
+
const height = record.height
|
|
354
|
+
if (typeof height !== 'number' || !Number.isFinite(height) || height <= 0) return undefined
|
|
355
|
+
return { kind: 'height', height: Math.ceil(height) }
|
|
356
|
+
}
|
|
357
|
+
return typeof record.type === 'string' && record.type !== '' ? { kind: 'interaction' } : undefined
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Hold a measured height inside the range a card may occupy.
|
|
362
|
+
* @param height - the content height the frame reported, in CSS pixels.
|
|
363
|
+
* @returns the height the card takes.
|
|
364
|
+
*/
|
|
365
|
+
export function clampCardHeight(height: number): number {
|
|
366
|
+
if (!Number.isFinite(height)) return CARD_DEFAULT_HEIGHT
|
|
367
|
+
return Math.min(CARD_MAX_HEIGHT, Math.max(CARD_MIN_HEIGHT, Math.ceil(height)))
|
|
368
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* The `ui.present` transcript card: a header line, the page in a controlled
|
|
2
|
+
frame, and the one sentence telling the operator where an answer happens.
|
|
3
|
+
Colour comes only from the theme's semantic aliases; geometry and type resolve
|
|
4
|
+
through the scale below, so no element carries a one-off literal (KFL R4
|
|
5
|
+
centralizes the value, A3 keeps one spacing rhythm, H5 holds the card to two
|
|
6
|
+
type sizes: 13 for the header title and the page, 12 for state and notice). */
|
|
7
|
+
|
|
8
|
+
.card,
|
|
9
|
+
.row {
|
|
10
|
+
--card-space-1: 4px;
|
|
11
|
+
--card-space-2: 8px;
|
|
12
|
+
--card-space-3: 12px;
|
|
13
|
+
--card-title-size: 13px;
|
|
14
|
+
--card-title-line: 20px;
|
|
15
|
+
--card-state-size: 12px;
|
|
16
|
+
--card-state-line: 18px;
|
|
17
|
+
--card-radius: 8px;
|
|
18
|
+
--card-edge: 1px;
|
|
19
|
+
--card-default-height: 320px;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* The header keeps one box across both states: the badge sits at the trailing
|
|
23
|
+
edge, so a title that grows or disappears never moves it. */
|
|
24
|
+
.card {
|
|
25
|
+
display: flex;
|
|
26
|
+
flex-direction: column;
|
|
27
|
+
gap: 0;
|
|
28
|
+
margin: var(--card-space-1) 0;
|
|
29
|
+
overflow: hidden;
|
|
30
|
+
border: var(--card-edge) solid var(--dsw-alias-border-l2);
|
|
31
|
+
border-radius: var(--card-radius);
|
|
32
|
+
background: var(--dsw-alias-bg-layer-1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.head {
|
|
36
|
+
display: flex;
|
|
37
|
+
align-items: center;
|
|
38
|
+
gap: var(--card-space-2);
|
|
39
|
+
min-height: calc(var(--card-title-line) + var(--card-space-2) * 2);
|
|
40
|
+
padding: var(--card-space-2) var(--card-space-3);
|
|
41
|
+
border-bottom: var(--card-edge) solid var(--dsw-alias-border-l2);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.title {
|
|
45
|
+
flex: 1 1 auto;
|
|
46
|
+
min-width: 0;
|
|
47
|
+
overflow: hidden;
|
|
48
|
+
color: var(--dsw-alias-label-primary);
|
|
49
|
+
font-size: var(--card-title-size);
|
|
50
|
+
line-height: var(--card-title-line);
|
|
51
|
+
text-overflow: ellipsis;
|
|
52
|
+
white-space: nowrap;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.badge {
|
|
56
|
+
flex: 0 0 auto;
|
|
57
|
+
margin-inline-start: auto;
|
|
58
|
+
color: var(--dsw-alias-label-secondary);
|
|
59
|
+
font-size: var(--card-state-size);
|
|
60
|
+
line-height: var(--card-state-line);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* The frame is a block of its own: its height is measured inside the sandboxed
|
|
64
|
+
document and applied here, and the presenter's suggestion only covers the
|
|
65
|
+
first paint. */
|
|
66
|
+
.frame {
|
|
67
|
+
display: block;
|
|
68
|
+
width: 100%;
|
|
69
|
+
min-height: var(--card-default-height);
|
|
70
|
+
border: 0;
|
|
71
|
+
background: transparent;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.notice {
|
|
75
|
+
margin: 0;
|
|
76
|
+
padding: var(--card-space-2) var(--card-space-3);
|
|
77
|
+
border-top: var(--card-edge) solid var(--dsw-alias-border-l2);
|
|
78
|
+
color: var(--dsw-alias-label-secondary);
|
|
79
|
+
font-size: var(--card-state-size);
|
|
80
|
+
line-height: var(--card-state-line);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* The neutral line of a call that carries no page: the keyed entry owns this
|
|
84
|
+
row, so it keeps a plain sentence rather than leaving the cell blank. */
|
|
85
|
+
.row {
|
|
86
|
+
margin: var(--card-space-1) 0;
|
|
87
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The command channel's projection: the host's slash-command registry and one
|
|
3
|
+
* session's skill catalog folded into the single flat directory a phone lists.
|
|
4
|
+
*
|
|
5
|
+
* Nothing here is stateful. Every answer is read from the host on the call that
|
|
6
|
+
* answers it — no cache, no event, no snapshot — so a command a plugin
|
|
7
|
+
* registered a moment ago and a skill the session's own preset withdrew are
|
|
8
|
+
* both reflected in the next answer, and a phone never renders a directory that
|
|
9
|
+
* this bridge remembered on its behalf.
|
|
10
|
+
*
|
|
11
|
+
* The `line` of an item is the whole text a client completes into its draft,
|
|
12
|
+
* separator space included, because both host surfaces resolve a bare gesture
|
|
13
|
+
* only when whitespace or the end of the text follows the name: the command
|
|
14
|
+
* registry splits a line into name and input at that separator, and a skill
|
|
15
|
+
* gesture is `/(^|\s)\/([a-z0-9]+(?:-[a-z0-9]+)*)(?=\s|$)/` in
|
|
16
|
+
* `@deepseek-ai/dsh-tool-skill`. A skill line is therefore text a client writes
|
|
17
|
+
* and the host reads out of the submitted prompt, never a command line: a skill
|
|
18
|
+
* is not registered with the command registry, so `commands/execute` answers
|
|
19
|
+
* `undefined` for it — which is exactly what a caller of `session.command` is
|
|
20
|
+
* told with `matched: false`.
|
|
21
|
+
* @module dsh-remote-bridge/core/commands
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** One row of the directory: a slash command or a skill, as the phone renders it. */
|
|
25
|
+
export interface CommandItem {
|
|
26
|
+
/** The host's own name for the row, without a leading slash. */
|
|
27
|
+
readonly name: string
|
|
28
|
+
/** The host's own one-line summary, verbatim. */
|
|
29
|
+
readonly description: string
|
|
30
|
+
/** Which host catalog the row came from; the two never mix order. */
|
|
31
|
+
readonly kind: 'command' | 'skill'
|
|
32
|
+
/** The complete draft text that invokes this row. */
|
|
33
|
+
readonly line: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The draft text one host name completes to: the gesture plus its separator. */
|
|
37
|
+
function lineOf(name: string): string {
|
|
38
|
+
return `/${name} `
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The name and description of one host row, or undefined when the row carries
|
|
43
|
+
* neither a name nor a summary a client could render.
|
|
44
|
+
*
|
|
45
|
+
* The host's strict codec already guarantees the shape, so this only keeps a
|
|
46
|
+
* row this bridge could not render out of a phone's directory: a row with no
|
|
47
|
+
* name would cross as `/undefined`, which is worse than a shorter list.
|
|
48
|
+
* @param row - one element of a host catalog answer.
|
|
49
|
+
*/
|
|
50
|
+
function namedRow(row: unknown): { readonly name: string; readonly description: string } | undefined {
|
|
51
|
+
if (typeof row !== 'object' || row === null) return undefined
|
|
52
|
+
const { name, description } = row as { name?: unknown; description?: unknown }
|
|
53
|
+
if (typeof name !== 'string' || name === '' || typeof description !== 'string') return undefined
|
|
54
|
+
return { name, description }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Fold the host's two catalogs into one directory: every command first, in the
|
|
59
|
+
* order the host listed them, then every skill, in the order the host listed
|
|
60
|
+
* them. Neither list is re-sorted, de-duplicated or cut into pages — a name
|
|
61
|
+
* both catalogs carry stays two rows, because the two rows do different things.
|
|
62
|
+
* @param commands - the `commands/list` answer, as it arrived.
|
|
63
|
+
* @param skills - the `skills/list` answer's `skills` array, as it arrived.
|
|
64
|
+
* @returns the rows a client renders, in the one order this protocol promises.
|
|
65
|
+
*/
|
|
66
|
+
export function commandItems(commands: readonly unknown[], skills: readonly unknown[]): readonly CommandItem[] {
|
|
67
|
+
const items: CommandItem[] = []
|
|
68
|
+
for (const [kind, rows] of [['command', commands], ['skill', skills]] as const) {
|
|
69
|
+
for (const row of rows) {
|
|
70
|
+
const named = namedRow(row)
|
|
71
|
+
if (named === undefined) continue
|
|
72
|
+
items.push({ name: named.name, description: named.description, kind, line: lineOf(named.name) })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return items
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Whether a gateway failure means this host serves no endpoint behind the
|
|
80
|
+
* channel at all, rather than refusing this one call.
|
|
81
|
+
*
|
|
82
|
+
* The gateway reports an endpoint nothing exports as `invocation-unavailable`
|
|
83
|
+
* and a withdrawn strict definition as `definition-unavailable`; both leave the
|
|
84
|
+
* call with nowhere to go, which is the one case this bridge answers with its
|
|
85
|
+
* own `commands-unavailable`. A service that is merely still activating keeps
|
|
86
|
+
* its own code: that window is retried before it arrives here, and a retried
|
|
87
|
+
* failure is a broken deployment rather than an absent channel.
|
|
88
|
+
*
|
|
89
|
+
* Both spellings of each code are recognized, exactly as `gateway-errors.ts`
|
|
90
|
+
* recognizes them for the activation window.
|
|
91
|
+
* @param code - the failure's own code, as it crossed to the refusal.
|
|
92
|
+
*/
|
|
93
|
+
export function commandsUnavailable(code: string): boolean {
|
|
94
|
+
return ['invocation-unavailable', 'definition-unavailable'].some(bare => code === bare || code === `gateway/${bare}`)
|
|
95
|
+
}
|