@linxin666/dsh-remote-bridge 0.4.8 → 0.4.9

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 (55) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +50 -17
  3. package/README.zh.md +50 -17
  4. package/lib/client.js +274 -253
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +6690 -2277
  7. package/lib/types/client/PairEntry.d.ts +2 -2
  8. package/lib/types/client/PairPanel.d.ts +10 -2
  9. package/lib/types/client/icons.d.ts +5 -13
  10. package/lib/types/client/locales.d.ts +10 -14
  11. package/lib/types/client/semantic.d.ts +9 -5
  12. package/lib/types/core/commands.d.ts +59 -0
  13. package/lib/types/core/connection.d.ts +68 -0
  14. package/lib/types/core/devices.d.ts +98 -2
  15. package/lib/types/core/hub.d.ts +65 -15
  16. package/lib/types/core/liveactivity.d.ts +324 -0
  17. package/lib/types/core/protocol.d.ts +260 -1
  18. package/lib/types/core/push.d.ts +178 -0
  19. package/lib/types/core/questions.d.ts +61 -9
  20. package/lib/types/core/session-meta.d.ts +55 -1
  21. package/lib/types/core/squads.d.ts +130 -0
  22. package/lib/types/core/usage-stats.d.ts +160 -0
  23. package/lib/types/core/usage.d.ts +294 -0
  24. package/lib/types/host/apns.d.ts +151 -0
  25. package/lib/types/host/squads-ledger.d.ts +39 -0
  26. package/lib/types/host/squads.d.ts +77 -0
  27. package/lib/types/host/usage-stats.d.ts +75 -0
  28. package/lib/types/host/watchers.d.ts +88 -0
  29. package/lib/types/index.d.ts +18 -2
  30. package/package.json +13 -13
  31. package/src/client/PairEntry.tsx +58 -31
  32. package/src/client/PairPanel.tsx +181 -97
  33. package/src/client/icons.tsx +17 -60
  34. package/src/client/locales.ts +15 -23
  35. package/src/client/panel.module.css +239 -186
  36. package/src/client/semantic.ts +10 -6
  37. package/src/core/commands.ts +95 -0
  38. package/src/core/connection.ts +646 -10
  39. package/src/core/devices.ts +221 -3
  40. package/src/core/hub.ts +362 -42
  41. package/src/core/liveactivity.ts +833 -0
  42. package/src/core/protocol.ts +294 -1
  43. package/src/core/push.ts +354 -0
  44. package/src/core/questions.ts +81 -10
  45. package/src/core/session-meta.ts +73 -1
  46. package/src/core/squads.ts +345 -0
  47. package/src/core/usage-stats.ts +424 -0
  48. package/src/core/usage.ts +937 -0
  49. package/src/core/wire.ts +8 -0
  50. package/src/host/apns.ts +320 -0
  51. package/src/host/squads-ledger.ts +105 -0
  52. package/src/host/squads.ts +206 -0
  53. package/src/host/usage-stats.ts +155 -0
  54. package/src/host/watchers.ts +272 -0
  55. package/src/index.ts +257 -8
@@ -0,0 +1,345 @@
1
+ /**
2
+ * The squads channel: the squad domain of the `dsh-agents` plugin — its
3
+ * definitions and the runs that launch sessions — as a phone reads and drives
4
+ * it.
5
+ *
6
+ * Nothing about a squad is modelled here. A squad is the plugin's own team row
7
+ * with the definition timestamps dropped, a run is the plugin's own run record
8
+ * with the goal it was given dropped, and a refusal the plugin raised crosses
9
+ * with the plugin's own code. What this module owns is the mapping onto the
10
+ * frozen `squads.v1` bodies plus the transport seam a Host half implements to
11
+ * reach the domain.
12
+ *
13
+ * The seam answers the plugin's rows unread (`unknown`), so one mapping serves
14
+ * both ways a Host can reach them: the plugin's HTTP routes over loopback, and
15
+ * the read-only ledger fallback. A row this build cannot read is refused rather
16
+ * than repaired, because a repaired squad is a squad the user never wrote.
17
+ * @module dsh-remote-bridge/core/squads
18
+ */
19
+
20
+ import {
21
+ CODE_SQUADS_UNAVAILABLE,
22
+ METHOD_SQUADS_LIST,
23
+ METHOD_SQUADS_RUN_MEMBER,
24
+ METHOD_SQUADS_RUNS,
25
+ METHOD_SQUADS_RUN_SQUAD,
26
+ METHOD_SQUADS_STATE,
27
+ } from './protocol.ts'
28
+
29
+ /**
30
+ * Every method the squads capability puts in a client's protocol. The gate in
31
+ * `connection.ts` reads this set once, so a method added beside these is gated
32
+ * by the same declaration rather than by a second list to keep in step.
33
+ */
34
+ export const SQUAD_METHODS: ReadonlySet<string> = new Set([
35
+ METHOD_SQUADS_STATE,
36
+ METHOD_SQUADS_LIST,
37
+ METHOD_SQUADS_RUNS,
38
+ METHOD_SQUADS_RUN_MEMBER,
39
+ METHOD_SQUADS_RUN_SQUAD,
40
+ ])
41
+
42
+ /** Sandbox modes a member may pin; the DSH permission presets, as the plugin spells them. */
43
+ export type SquadMemberPermission = 'read-only' | 'workspace-write' | 'danger-full-access'
44
+
45
+ const PERMISSIONS: ReadonlySet<string> = new Set<SquadMemberPermission>(['read-only', 'workspace-write', 'danger-full-access'])
46
+
47
+ /** One member of a squad: a display name and the pins its session is composed from. */
48
+ export interface SquadMember {
49
+ id: string
50
+ name: string
51
+ preset?: string
52
+ permission?: SquadMemberPermission
53
+ model?: string
54
+ reasoningEffort?: string
55
+ brief?: string
56
+ }
57
+
58
+ /** One squad: an ordered set of member definitions sharing a workspace. */
59
+ export interface Squad {
60
+ id: string
61
+ name: string
62
+ description?: string
63
+ workspaceId?: string
64
+ leadPreset?: string
65
+ order?: number
66
+ members: SquadMember[]
67
+ }
68
+
69
+ /** One launched session: a member session, or the Lead session of a whole squad. */
70
+ export interface SquadRun {
71
+ id: string
72
+ squadId: string
73
+ kind: 'squad' | 'member'
74
+ memberId?: string
75
+ sessionId: string
76
+ createdAt: number
77
+ status: 'running' | 'settled' | 'failed'
78
+ failure?: string
79
+ }
80
+
81
+ /** The `squads.state` body: definitions, run history, the offered presets and the owning pid. */
82
+ export interface SquadsState {
83
+ squads: Squad[]
84
+ runs: SquadRun[]
85
+ presets?: string[]
86
+ host: { ownerPid?: number }
87
+ }
88
+
89
+ /** What `squads.runMember` asks the domain to launch. */
90
+ export interface SquadMemberRunRequest {
91
+ squadId: string
92
+ memberId: string
93
+ firstMessage?: string
94
+ }
95
+
96
+ /** What `squads.runSquad` asks the domain to launch. */
97
+ export interface SquadRunRequest {
98
+ squadId: string
99
+ goal?: string
100
+ }
101
+
102
+ /**
103
+ * A refusal this channel answers with. The transport raises it and the
104
+ * connection publishes its `code` and text unchanged, so a domain refusal
105
+ * (`team-not-found`, `preset-unknown`) and a capability refusal
106
+ * (`squads-unavailable`, `squads-run-unavailable`) reach the client the same way.
107
+ */
108
+ export class SquadRefusal extends Error {
109
+ /** Stable machine code the client switches on. */
110
+ readonly code: string
111
+
112
+ constructor(code: string, message: string) {
113
+ super(message)
114
+ this.name = 'SquadRefusal'
115
+ this.code = code
116
+ }
117
+ }
118
+
119
+ /** @returns whether a caught value is this channel's own refusal. */
120
+ export function isSquadRefusal(value: unknown): value is SquadRefusal {
121
+ return value instanceof SquadRefusal
122
+ }
123
+
124
+ /**
125
+ * How a Host reaches the squad domain. Every method answers the plugin's own
126
+ * body unread: `state` is one `{teams, runs, host, presets?}` document, and the
127
+ * two run methods answer one `{run}` document.
128
+ */
129
+ export interface SquadTransport {
130
+ /** Definitions, run history and the owning pid, as the domain publishes them. */
131
+ state(): Promise<unknown>
132
+ /** Launch one member session. */
133
+ runMember(request: SquadMemberRunRequest): Promise<unknown>
134
+ /** Launch the Lead session of one whole squad. */
135
+ runSquad(request: SquadRunRequest): Promise<unknown>
136
+ }
137
+
138
+ function record(value: unknown): Record<string, unknown> | undefined {
139
+ return typeof value === 'object' && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : undefined
140
+ }
141
+
142
+ /**
143
+ * Read one optional text field.
144
+ * @returns the text, `undefined` when the row does not carry it, or `null` when
145
+ * it carries something this build cannot read as text.
146
+ */
147
+ function optionalText(value: unknown): string | undefined | null {
148
+ if (value === undefined) return undefined
149
+ return typeof value === 'string' && value !== '' ? value : null
150
+ }
151
+
152
+ function unusable(what: string): SquadRefusal {
153
+ return new SquadRefusal(CODE_SQUADS_UNAVAILABLE, `the dsh-agents answer carries ${what} this bridge cannot read`)
154
+ }
155
+
156
+ /**
157
+ * Map one persisted member row.
158
+ * @param value - the row as the domain stored it.
159
+ * @param squadId - the squad the row belongs to, when the row is read from a squad.
160
+ * @returns the member, without the definition timestamps and the team backlink.
161
+ */
162
+ function memberOf(value: unknown, squadId?: string): SquadMember {
163
+ const row = record(value)
164
+ if (row === undefined) throw unusable('a member row that is not an object')
165
+ if (typeof row.id !== 'string' || row.id === '' || typeof row.name !== 'string' || row.name === '') {
166
+ throw unusable('a member row without an id and a name')
167
+ }
168
+ if (squadId !== undefined && row.teamId !== undefined && row.teamId !== squadId) {
169
+ throw unusable('a member row of another squad')
170
+ }
171
+ const preset = optionalText(row.preset)
172
+ const model = optionalText(row.model)
173
+ const reasoningEffort = optionalText(row.reasoningEffort)
174
+ const brief = optionalText(row.brief)
175
+ if (preset === null || model === null || reasoningEffort === null || brief === null) throw unusable('a member pin that is not text')
176
+ if (row.permission !== undefined && !PERMISSIONS.has(String(row.permission))) throw unusable('a member permission outside the DSH presets')
177
+ return {
178
+ id: row.id,
179
+ name: row.name,
180
+ ...(preset === undefined ? {} : { preset }),
181
+ ...(row.permission === undefined ? {} : { permission: row.permission as SquadMemberPermission }),
182
+ ...(model === undefined ? {} : { model }),
183
+ ...(reasoningEffort === undefined ? {} : { reasoningEffort }),
184
+ ...(brief === undefined ? {} : { brief }),
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Map one persisted squad row.
190
+ * @param value - the team row as the domain stored it.
191
+ * @returns the squad, its members' own definition timestamps dropped with it.
192
+ */
193
+ function squadOf(value: unknown): Squad {
194
+ const row = record(value)
195
+ if (row === undefined) throw unusable('a squad row that is not an object')
196
+ if (typeof row.id !== 'string' || row.id === '' || typeof row.name !== 'string' || row.name === '') {
197
+ throw unusable('a squad row without an id and a name')
198
+ }
199
+ const description = optionalText(row.description)
200
+ const workspaceId = optionalText(row.workspaceId)
201
+ const leadPreset = optionalText(row.leadPreset)
202
+ if (description === null || workspaceId === null || leadPreset === null) throw unusable('a squad pin that is not text')
203
+ if (row.order !== undefined && (typeof row.order !== 'number' || !Number.isInteger(row.order))) throw unusable('a squad order that is not a whole number')
204
+ if (!Array.isArray(row.members)) throw unusable('a squad row with no member list')
205
+ return {
206
+ id: row.id,
207
+ name: row.name,
208
+ ...(description === undefined ? {} : { description }),
209
+ ...(workspaceId === undefined ? {} : { workspaceId }),
210
+ ...(leadPreset === undefined ? {} : { leadPreset }),
211
+ ...(row.order === undefined ? {} : { order: row.order }),
212
+ members: row.members.map(member => memberOf(member, row.id as string)),
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Map one run record. `kind` is required on this wire and absent on every run the
218
+ * domain recorded before whole-squad runs existed, so an absent kind reads as the
219
+ * member run it is by construction — the same default the plugin itself applies.
220
+ * @param value - the run as the domain stored it.
221
+ * @returns the run, the goal its Lead was given dropped.
222
+ */
223
+ function runOf(value: unknown): SquadRun {
224
+ const row = record(value)
225
+ if (row === undefined) throw unusable('a run row that is not an object')
226
+ if (typeof row.id !== 'string' || row.id === '' || typeof row.teamId !== 'string' || row.teamId === '') {
227
+ throw unusable('a run row without an id and a squad id')
228
+ }
229
+ if (typeof row.sessionId !== 'string' || row.sessionId === '') throw unusable('a run row without a session id')
230
+ if (typeof row.createdAt !== 'number' || !Number.isFinite(row.createdAt)) throw unusable('a run row without a creation time')
231
+ if (row.status !== 'running' && row.status !== 'settled' && row.status !== 'failed') throw unusable('a run row outside the three statuses')
232
+ if (row.kind !== undefined && row.kind !== 'member' && row.kind !== 'squad') throw unusable('a run of an unknown kind')
233
+ if (row.failure !== undefined && typeof row.failure !== 'string') throw unusable('a run failure that is not text')
234
+ const kind = row.kind === 'squad' ? 'squad' : 'member'
235
+ if (kind === 'member' && (typeof row.memberId !== 'string' || row.memberId === '')) throw unusable('a member run without a member id')
236
+ if (kind === 'squad' && row.memberId !== undefined) throw unusable('a squad run that names a member')
237
+ return {
238
+ id: row.id,
239
+ squadId: row.teamId,
240
+ kind,
241
+ ...(kind === 'squad' ? {} : { memberId: row.memberId as string }),
242
+ sessionId: row.sessionId,
243
+ createdAt: row.createdAt,
244
+ status: row.status,
245
+ ...(row.failure === undefined ? {} : { failure: row.failure }),
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Read the preset ids the domain offers as a Lead preset.
251
+ * @param value - the answer's `presets`.
252
+ * @returns the ids, or `undefined` when the host published no usable list — an
253
+ * empty list would say this deployment serves none, which is a different claim.
254
+ */
255
+ function presetsOf(value: unknown): string[] | undefined {
256
+ if (!Array.isArray(value) || value.length === 0) return undefined
257
+ return value.every(entry => typeof entry === 'string' && entry !== '') ? [...value] as string[] : undefined
258
+ }
259
+
260
+ /**
261
+ * Read the owning pid beside the definitions.
262
+ * @param value - the answer's `host`.
263
+ * @returns `{ownerPid}` when the domain named one, or an empty object.
264
+ */
265
+ function hostOf(value: unknown): SquadsState['host'] {
266
+ const pid = record(value)?.ownerPid
267
+ return typeof pid === 'number' && Number.isInteger(pid) && pid > 0 ? { ownerPid: pid } : {}
268
+ }
269
+
270
+ /**
271
+ * Map one `squads.state` answer.
272
+ * @param source - the domain's `{teams, runs, host, presets?}` document, from
273
+ * either the plugin's route or its ledger.
274
+ * @returns the frozen state body.
275
+ */
276
+ export function squadsStateOf(source: unknown): SquadsState {
277
+ const body = record(source)
278
+ if (body === undefined || !Array.isArray(body.teams) || !Array.isArray(body.runs)) {
279
+ throw new SquadRefusal(CODE_SQUADS_UNAVAILABLE, 'the dsh-agents state answer is not a squad domain this bridge can read')
280
+ }
281
+ const presets = presetsOf(body.presets)
282
+ return {
283
+ squads: body.teams.map(squadOf),
284
+ runs: body.runs.map(runOf),
285
+ ...(presets === undefined ? {} : { presets }),
286
+ host: hostOf(body.host),
287
+ }
288
+ }
289
+
290
+ /**
291
+ * Map one run answer.
292
+ * @param source - the domain's `{run}` document.
293
+ * @returns the frozen run row.
294
+ */
295
+ export function squadRunOf(source: unknown): SquadRun {
296
+ const run = record(record(source)?.run)
297
+ if (run === undefined) throw new SquadRefusal(CODE_SQUADS_UNAVAILABLE, 'the dsh-agents run answer carries no run this bridge can read')
298
+ return runOf(run)
299
+ }
300
+
301
+ /** @returns a non-empty string payload value, or undefined. */
302
+ function textArg(payload: Readonly<Record<string, unknown>>, key: string): string | undefined {
303
+ const value = payload[key]
304
+ return typeof value === 'string' && value !== '' ? value : undefined
305
+ }
306
+
307
+ /**
308
+ * Read an optional message field.
309
+ * @returns the text as the client wrote it, `undefined` when it is absent or
310
+ * blank, or `null` when the field carries something that is not text.
311
+ */
312
+ function messageArg(value: unknown): string | undefined | null {
313
+ if (value === undefined) return undefined
314
+ if (typeof value !== 'string') return null
315
+ return value.trim() === '' ? undefined : value
316
+ }
317
+
318
+ /**
319
+ * Gate `squads.runMember`'s payload. Only the two names this command needs are
320
+ * required; the lengths the domain accepts are the domain's own, so an oversized
321
+ * message is forwarded and refused there instead of being cut here.
322
+ * @param payload - the request's `p`.
323
+ * @returns the request, or undefined when it does not name a squad and a member.
324
+ */
325
+ export function parseSquadMemberRun(payload: Readonly<Record<string, unknown>>): SquadMemberRunRequest | undefined {
326
+ const squadId = textArg(payload, 'squadId')
327
+ const memberId = textArg(payload, 'memberId')
328
+ if (squadId === undefined || memberId === undefined) return undefined
329
+ const firstMessage = messageArg(payload.firstMessage)
330
+ if (firstMessage === null) return undefined
331
+ return { squadId, memberId, ...(firstMessage === undefined ? {} : { firstMessage }) }
332
+ }
333
+
334
+ /**
335
+ * Gate `squads.runSquad`'s payload.
336
+ * @param payload - the request's `p`.
337
+ * @returns the request, or undefined when it does not name a squad.
338
+ */
339
+ export function parseSquadRun(payload: Readonly<Record<string, unknown>>): SquadRunRequest | undefined {
340
+ const squadId = textArg(payload, 'squadId')
341
+ if (squadId === undefined) return undefined
342
+ const goal = messageArg(payload.goal)
343
+ if (goal === null) return undefined
344
+ return { squadId, ...(goal === undefined ? {} : { goal }) }
345
+ }