@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.
- package/README.i18n.yaml +2 -2
- package/README.md +50 -17
- package/README.zh.md +50 -17
- package/lib/client.js +274 -253
- package/lib/client.js.map +1 -1
- package/lib/index.js +6690 -2277
- package/lib/types/client/PairEntry.d.ts +2 -2
- package/lib/types/client/PairPanel.d.ts +10 -2
- package/lib/types/client/icons.d.ts +5 -13
- package/lib/types/client/locales.d.ts +10 -14
- package/lib/types/client/semantic.d.ts +9 -5
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +68 -0
- package/lib/types/core/devices.d.ts +98 -2
- package/lib/types/core/hub.d.ts +65 -15
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +260 -1
- package/lib/types/core/push.d.ts +178 -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/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/usage-stats.d.ts +75 -0
- package/lib/types/host/watchers.d.ts +88 -0
- package/lib/types/index.d.ts +18 -2
- package/package.json +13 -13
- package/src/client/PairEntry.tsx +58 -31
- package/src/client/PairPanel.tsx +181 -97
- package/src/client/icons.tsx +17 -60
- package/src/client/locales.ts +15 -23
- package/src/client/panel.module.css +239 -186
- package/src/client/semantic.ts +10 -6
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +646 -10
- package/src/core/devices.ts +221 -3
- package/src/core/hub.ts +362 -42
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +294 -1
- package/src/core/push.ts +354 -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/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/usage-stats.ts +155 -0
- package/src/host/watchers.ts +272 -0
- package/src/index.ts +257 -8
package/src/core/connection.ts
CHANGED
|
@@ -17,38 +17,54 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import type { ApprovalRegistry } from './approvals.ts'
|
|
20
|
-
import
|
|
20
|
+
import { commandItems, commandsUnavailable } from './commands.ts'
|
|
21
|
+
import type { DeviceRoster, PushEnvironment, PushLiveActivityToken, PushRegistration } from './devices.ts'
|
|
21
22
|
import { ROSTER_DELTA_MAX_ROWS, RemoteBridgeDeltas, type DeltaRow } from './delta.ts'
|
|
22
23
|
import { deltaOfFollowFrame, MessageIndex, oldestEntrySeq, pageTurns, readFollowBaseline, SpawnCallIndex, ToolCallIndex, trimUnrenderedEntries, turnsAbove, turnsOfRecords, type FollowBaseline, type SnapshotTurn } from './dsh-frames.ts'
|
|
23
24
|
import { EventSequence, FRAME_TOO_LARGE, MAX_FRAME_BYTES, decodeClientFrame, encodeFrame, errorFrame, responseFrame, type ReqFrame, type ServerFrame } from './envelope.ts'
|
|
24
25
|
import type { RemoteBridgeMethodResult, RemoteBridgeRegistry } from './extension.ts'
|
|
25
26
|
import { errorCodeOf, failureText, withServiceUnavailableRetry } from './gateway-errors.ts'
|
|
26
27
|
import { scopeKey, type JobRoster, type JobsPayload } from './jobs.ts'
|
|
28
|
+
import type { LiveActivityNotifier } from './liveactivity.ts'
|
|
27
29
|
import type { PairingService } from './pairing.ts'
|
|
28
|
-
import type {
|
|
30
|
+
import type { PushNotifier } from './push.ts'
|
|
31
|
+
import { pushEventOfTurnEnd } from './push.ts'
|
|
32
|
+
import type { ListedQuestion, QuestionAnswerItem, QuestionClaims, QuestionListPayload } from './questions.ts'
|
|
29
33
|
import {
|
|
30
34
|
CAP_AGENTS,
|
|
35
|
+
CAP_COMMANDS,
|
|
31
36
|
CAP_DELTA,
|
|
32
37
|
CAP_FRAME_DEFLATE,
|
|
33
38
|
CAP_JOBS,
|
|
39
|
+
CAP_PUSH,
|
|
34
40
|
CAP_QUESTIONS,
|
|
41
|
+
CAP_QUESTION_LIST,
|
|
35
42
|
CAP_ROSTER_COMPACT,
|
|
43
|
+
CAP_SESSION_ADMIN,
|
|
36
44
|
CAP_SESSION_FORK,
|
|
37
45
|
CAP_SESSION_SNAPSHOT_PAGE,
|
|
38
46
|
CAP_SESSION_SNAPSHOT_SINCE,
|
|
39
47
|
CAP_SESSION_SNAPSHOT_TRIM,
|
|
48
|
+
CAP_SQUADS,
|
|
40
49
|
CAP_SUBAGENT_THREAD,
|
|
50
|
+
CAP_USAGE,
|
|
51
|
+
CAP_USAGE_STATS,
|
|
52
|
+
CAP_WORKSPACE,
|
|
41
53
|
CODE_APPROVAL_UNKNOWN,
|
|
54
|
+
CODE_COMMANDS_UNAVAILABLE,
|
|
42
55
|
CODE_INTERNAL,
|
|
43
56
|
CODE_INVALID_ARGS,
|
|
44
57
|
CODE_JOBS_UNAVAILABLE,
|
|
45
58
|
CODE_NONCE_INVALID,
|
|
46
59
|
CODE_PROTO_UNSUPPORTED,
|
|
60
|
+
CODE_QUESTION_LIST_UNAVAILABLE,
|
|
47
61
|
CODE_SESSION_NOT_FOLLOWABLE,
|
|
48
62
|
CODE_SNAPSHOT_TOO_LARGE,
|
|
49
63
|
CODE_SNAPSHOT_UNAVAILABLE,
|
|
64
|
+
CODE_SQUADS_UNAVAILABLE,
|
|
50
65
|
CODE_TOKEN_INVALID,
|
|
51
66
|
CODE_UNKNOWN_METHOD,
|
|
67
|
+
CODE_UNKNOWN_SESSION,
|
|
52
68
|
CODE_UNPAIRED,
|
|
53
69
|
KNOWN_METHODS,
|
|
54
70
|
METHOD_APPROVAL_RESOLVE,
|
|
@@ -57,8 +73,13 @@ import {
|
|
|
57
73
|
METHOD_PAIR_BEGIN,
|
|
58
74
|
METHOD_PAIR_FINISH,
|
|
59
75
|
METHOD_PRESETS_LIST,
|
|
76
|
+
METHOD_PUSH_REGISTER,
|
|
60
77
|
METHOD_QUESTION_ANSWER,
|
|
78
|
+
METHOD_QUESTION_LIST,
|
|
79
|
+
METHOD_SESSION_ARCHIVE,
|
|
61
80
|
METHOD_SESSION_ATTACHMENT,
|
|
81
|
+
METHOD_SESSION_COMMAND,
|
|
82
|
+
METHOD_SESSION_COMMANDS,
|
|
62
83
|
METHOD_SESSION_CREATE,
|
|
63
84
|
METHOD_SESSION_FORK,
|
|
64
85
|
METHOD_SESSION_HISTORY,
|
|
@@ -67,11 +88,24 @@ import {
|
|
|
67
88
|
METHOD_SESSION_LIST,
|
|
68
89
|
METHOD_SESSION_MODELS,
|
|
69
90
|
METHOD_SESSION_PERMISSIONS,
|
|
91
|
+
METHOD_SESSION_PIN,
|
|
70
92
|
METHOD_SESSION_PROMPT,
|
|
93
|
+
METHOD_SESSION_RENAME,
|
|
71
94
|
METHOD_SESSION_SET_MODEL,
|
|
72
95
|
METHOD_SESSION_SET_PERMISSION,
|
|
73
96
|
METHOD_SESSION_SNAPSHOT,
|
|
74
97
|
METHOD_SESSION_TITLES,
|
|
98
|
+
METHOD_SESSION_UNARCHIVE,
|
|
99
|
+
METHOD_SESSION_UNPIN,
|
|
100
|
+
METHOD_SQUADS_LIST,
|
|
101
|
+
METHOD_SQUADS_RUNS,
|
|
102
|
+
METHOD_SQUADS_RUN_MEMBER,
|
|
103
|
+
METHOD_SQUADS_RUN_SQUAD,
|
|
104
|
+
METHOD_SQUADS_STATE,
|
|
105
|
+
METHOD_USAGE_BALANCE,
|
|
106
|
+
METHOD_USAGE_SESSION,
|
|
107
|
+
METHOD_USAGE_STATS,
|
|
108
|
+
METHOD_WORKSPACE_LIST,
|
|
75
109
|
MINOR,
|
|
76
110
|
PROTO,
|
|
77
111
|
SERVED_CAPABILITIES,
|
|
@@ -81,8 +115,10 @@ import {
|
|
|
81
115
|
UNPAIRED_METHODS,
|
|
82
116
|
} from './protocol.ts'
|
|
83
117
|
import { base64UrlDecode, verifyEs256 } from './crypto.ts'
|
|
118
|
+
import { isSquadRefusal, parseSquadMemberRun, parseSquadRun, squadRunOf, SQUAD_METHODS, squadsStateOf, type SquadTransport, type SquadsState } from './squads.ts'
|
|
84
119
|
import { TITLES_MAX_IDS, type SessionMeta } from './session-meta.ts'
|
|
85
120
|
import type { SessionPermissions } from './session-permissions.ts'
|
|
121
|
+
import { USAGE_METHODS, type UsageService } from './usage.ts'
|
|
86
122
|
import type { BridgeGateway, GatewayPeer } from './wire.ts'
|
|
87
123
|
import { CODE_SUBAGENT_ADDRESS_REQUIRED, sessionAddress, subagentAddress, wireArgs, type SessionAddress } from './wire.ts'
|
|
88
124
|
import { CLOSE_LIVENESS_TIMEOUT } from './ws-frames.ts'
|
|
@@ -150,6 +186,12 @@ export interface BridgeConnectionOptions {
|
|
|
150
186
|
readonly jobs: JobRoster
|
|
151
187
|
/** Process-local session metadata: live titles and the host's archive set. */
|
|
152
188
|
readonly sessionMeta: SessionMeta
|
|
189
|
+
/**
|
|
190
|
+
* `usage.v1` 背后两半读数:一条会话的上下文窗口与请求组成估算,以及某条
|
|
191
|
+
* provider 路由背后账户的余额。两者都是宿主侧的事实而不是某个设备的事实,
|
|
192
|
+
* 所以一个宿主一份、所有连接共享(见 `core/usage.ts`)。
|
|
193
|
+
*/
|
|
194
|
+
readonly usage: UsageService
|
|
153
195
|
/** Host-process permission presets, read and switched through their real service objects. */
|
|
154
196
|
readonly permissions: SessionPermissions
|
|
155
197
|
/**
|
|
@@ -176,6 +218,38 @@ export interface BridgeConnectionOptions {
|
|
|
176
218
|
* `KNOWN_METHODS` answers `unknown-method`.
|
|
177
219
|
*/
|
|
178
220
|
readonly extensions?: RemoteBridgeRegistry
|
|
221
|
+
/**
|
|
222
|
+
* The host's squad domain, behind the `squads.v1` channel. Absent is a host
|
|
223
|
+
* that serves no `dsh-agents` domain: the capability stays advertised and only
|
|
224
|
+
* a call that needs the domain answers `squads-unavailable`.
|
|
225
|
+
*/
|
|
226
|
+
readonly squads?: SquadTransport
|
|
227
|
+
/**
|
|
228
|
+
* The outbound notification path, behind the `push.v1` channel. Absent is a
|
|
229
|
+
* bridge that sends nothing: `push.register` still answers `ok:true` (the
|
|
230
|
+
* device row is written either way), and no notification is ever built. That
|
|
231
|
+
* is the default, because a sender needs an Apple signing key this bridge is
|
|
232
|
+
* not shipped with.
|
|
233
|
+
*/
|
|
234
|
+
readonly push?: PushNotifier
|
|
235
|
+
/**
|
|
236
|
+
* The lock-screen card path, behind the same `push.v1` channel. Absent — and it
|
|
237
|
+
* is absent whenever no APNs key is configured — is a bridge that never builds a
|
|
238
|
+
* card: the roster is still served and a question is still answered, because
|
|
239
|
+
* registering the phone's tokens and pushing to them are two different things
|
|
240
|
+
* (§28.3, §29).
|
|
241
|
+
*/
|
|
242
|
+
readonly liveActivity?: LiveActivityNotifier
|
|
243
|
+
/**
|
|
244
|
+
* Called after a registration changed the device table.
|
|
245
|
+
*
|
|
246
|
+
* A session's first live-activity token is what asks this bridge to keep a
|
|
247
|
+
* follow stream of its own open for that session (`host/watchers.ts`), so the
|
|
248
|
+
* roster is the trigger. The hook is guarded at the call site: a standing
|
|
249
|
+
* watcher is this bridge's own background work and may never turn a
|
|
250
|
+
* `push.register` into a failure.
|
|
251
|
+
*/
|
|
252
|
+
readonly onPushRegistered?: () => void
|
|
179
253
|
}
|
|
180
254
|
|
|
181
255
|
/** A device the connection proved itself to be, resolved from the upgrade request. */
|
|
@@ -285,6 +359,31 @@ function optionalText(value: unknown): string | undefined {
|
|
|
285
359
|
return typeof value === 'string' && value !== '' ? value : undefined
|
|
286
360
|
}
|
|
287
361
|
|
|
362
|
+
/**
|
|
363
|
+
* One roster row's own title, as the host's projection carries it.
|
|
364
|
+
*
|
|
365
|
+
* `SessionSummary` has no top-level `title` — which is why this bridge once read
|
|
366
|
+
* its own live-title cache and nothing else. Every row nevertheless carries
|
|
367
|
+
* `projections.values.title`: the host folds that out of its projection cache
|
|
368
|
+
* synchronously for a cold session as well as a live one, with no log read and no
|
|
369
|
+
* follow stream, and its own desktop list renders exactly that field. Reading it
|
|
370
|
+
* costs no call, because it is already inside the listing the bridge just paid
|
|
371
|
+
* for.
|
|
372
|
+
*
|
|
373
|
+
* The guard is the one every optional field here takes: a `projections` block
|
|
374
|
+
* that is not an object, a `values` member that is not one, and a `title` that is
|
|
375
|
+
* not a non-empty string are all simply absent, so an answer of another shape
|
|
376
|
+
* costs a row its title rather than failing the listing.
|
|
377
|
+
* @param item - one row of the host's `session/list` answer.
|
|
378
|
+
* @returns the projected title, or undefined when the row carries none.
|
|
379
|
+
*/
|
|
380
|
+
function projectedTitleOf(item: Readonly<Record<string, unknown>>): string | undefined {
|
|
381
|
+
const projections = isRecord(item.projections) ? item.projections : undefined
|
|
382
|
+
if (projections === undefined) return undefined
|
|
383
|
+
const values = isRecord(projections.values) ? projections.values : undefined
|
|
384
|
+
return values === undefined ? undefined : optionalText(values.title)
|
|
385
|
+
}
|
|
386
|
+
|
|
288
387
|
/** @returns a non-negative safe integer page argument, or undefined. */
|
|
289
388
|
function pageNumber(value: unknown): number | undefined {
|
|
290
389
|
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : undefined
|
|
@@ -405,15 +504,23 @@ function answersOf(value: unknown): readonly QuestionAnswerItem[] | undefined {
|
|
|
405
504
|
return answers
|
|
406
505
|
}
|
|
407
506
|
|
|
408
|
-
/**
|
|
409
|
-
|
|
507
|
+
/**
|
|
508
|
+
* One model selection, in the field names the protocol publishes.
|
|
509
|
+
*
|
|
510
|
+
* Exported beside {@link selectionOf} and {@link modelSelectionOf} because the
|
|
511
|
+
* host half resolves a session's current route for `usage.stats`'s neighbour
|
|
512
|
+
* key `usage.session`/`modelContextWindow` with exactly this fold: two readings
|
|
513
|
+
* of "which model is this session on" would let the two methods name different
|
|
514
|
+
* routes for the same session.
|
|
515
|
+
*/
|
|
516
|
+
export type ModelSelection = {
|
|
410
517
|
readonly provider: string
|
|
411
518
|
readonly model: string
|
|
412
519
|
readonly reasoningEffort?: string
|
|
413
520
|
}
|
|
414
521
|
|
|
415
522
|
/** @returns the selection this value describes, or undefined when it describes none. */
|
|
416
|
-
function selectionOf(value: unknown): ModelSelection | undefined {
|
|
523
|
+
export function selectionOf(value: unknown): ModelSelection | undefined {
|
|
417
524
|
if (!isRecord(value)) return undefined
|
|
418
525
|
const provider = optionalText(value.provider)
|
|
419
526
|
const model = optionalText(value.model)
|
|
@@ -428,7 +535,7 @@ function selectionOf(value: unknown): ModelSelection | undefined {
|
|
|
428
535
|
* Both halves stay optional: a session that never ran has neither, and the
|
|
429
536
|
* catalog default is then the only truthful current value.
|
|
430
537
|
*/
|
|
431
|
-
function modelSelectionOf(value: unknown): { readonly next?: ModelSelection; readonly lastUsed?: ModelSelection } {
|
|
538
|
+
export function modelSelectionOf(value: unknown): { readonly next?: ModelSelection; readonly lastUsed?: ModelSelection } {
|
|
432
539
|
if (!isRecord(value) || !isRecord(value.values)) return {}
|
|
433
540
|
const selection = value.values.modelSelection
|
|
434
541
|
if (!isRecord(selection)) return {}
|
|
@@ -704,6 +811,12 @@ export class BridgeConnection {
|
|
|
704
811
|
if (frame.m === METHOD_QUESTION_ANSWER && !this.declares(CAP_QUESTIONS)) {
|
|
705
812
|
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
706
813
|
}
|
|
814
|
+
// `question.list` is its own channel capability, declared by the client and
|
|
815
|
+
// announced by the bridge: a client that never declared it is told what it was
|
|
816
|
+
// told before the method existed, and no claim registry is read on its behalf.
|
|
817
|
+
if (frame.m === METHOD_QUESTION_LIST && !this.declares(CAP_QUESTION_LIST)) {
|
|
818
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
819
|
+
}
|
|
707
820
|
// `jobs.list` is the whole of the job channel: without the capability the
|
|
708
821
|
// method does not exist, and without the method no scope is ever watched, so
|
|
709
822
|
// a client that never declared it can receive no `jobs.changed` either.
|
|
@@ -722,6 +835,55 @@ export class BridgeConnection {
|
|
|
722
835
|
if (frame.m === METHOD_SESSION_FORK && !this.declares(CAP_SESSION_FORK)) {
|
|
723
836
|
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
724
837
|
}
|
|
838
|
+
// The command channel is one capability: without `commands.v1` neither the
|
|
839
|
+
// directory nor the dispatch is part of this client's protocol, so no
|
|
840
|
+
// registry and no skill catalog is read on a client's behalf that never
|
|
841
|
+
// asked for either.
|
|
842
|
+
if ((frame.m === METHOD_SESSION_COMMANDS || frame.m === METHOD_SESSION_COMMAND) && !this.declares(CAP_COMMANDS)) {
|
|
843
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
844
|
+
}
|
|
845
|
+
// Reading workspaces is its own capability, apart from the five writes beside
|
|
846
|
+
// it: a client may ask what this host has and where without ever renaming,
|
|
847
|
+
// pinning or archiving anything, so the reading half is declared on its own
|
|
848
|
+
// and a client that declared only the writing half reads no registry.
|
|
849
|
+
if (frame.m === METHOD_WORKSPACE_LIST && !this.declares(CAP_WORKSPACE)) {
|
|
850
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
851
|
+
}
|
|
852
|
+
// The session-admin writes are one capability: without it none of the five is
|
|
853
|
+
// part of this client's protocol, so no title is written and no set is
|
|
854
|
+
// mutated on behalf of a client that predates the channel.
|
|
855
|
+
if (!this.declares(CAP_SESSION_ADMIN)
|
|
856
|
+
&& (frame.m === METHOD_SESSION_RENAME
|
|
857
|
+
|| frame.m === METHOD_SESSION_PIN
|
|
858
|
+
|| frame.m === METHOD_SESSION_UNPIN
|
|
859
|
+
|| frame.m === METHOD_SESSION_ARCHIVE
|
|
860
|
+
|| frame.m === METHOD_SESSION_UNARCHIVE)) {
|
|
861
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
862
|
+
}
|
|
863
|
+
// The squads channel is one capability: without `squads.v1` none of the five
|
|
864
|
+
// commands is part of this client's protocol, so no squad domain is read and
|
|
865
|
+
// no session is launched on behalf of a client that never asked to drive one.
|
|
866
|
+
if (SQUAD_METHODS.has(frame.m) && !this.declares(CAP_SQUADS)) {
|
|
867
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
868
|
+
}
|
|
869
|
+
// Registering an APNs token is its own capability: without `push.v1` the
|
|
870
|
+
// method does not exist for this client, and no device row is written on its
|
|
871
|
+
// behalf. The capability gates the WRITE only — whether a notification ever
|
|
872
|
+
// leaves this process is a deployment fact the wire cannot express (§28.3).
|
|
873
|
+
if (frame.m === METHOD_PUSH_REGISTER && !this.declares(CAP_PUSH)) {
|
|
874
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
875
|
+
}
|
|
876
|
+
// 用量通道是一个能力:不宣告 `usage.v1` 的客户端看到的仍是它从前看到的
|
|
877
|
+
// `unknown-method`,本桥也不会替它折一条会话事件、更不会替它打一次上游
|
|
878
|
+
// 余额端点。
|
|
879
|
+
if (USAGE_METHODS.has(frame.m) && !this.declares(CAP_USAGE)) {
|
|
880
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
881
|
+
}
|
|
882
|
+
// 台账聚合是另一个能力,因为它读的是另一个插件挂在这个宿主上的路由:不宣告
|
|
883
|
+
// `usage.stats.v1` 的客户端看不到这个方法,本桥也不会替它去读那两条路由。
|
|
884
|
+
if (frame.m === METHOD_USAGE_STATS && !this.declares(CAP_USAGE_STATS)) {
|
|
885
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
886
|
+
}
|
|
725
887
|
// Unconditional: a loopback Host buys nothing here. Without a device token
|
|
726
888
|
// only the handshake and the two pairing steps run.
|
|
727
889
|
if (!this.paired && !UNPAIRED_METHODS.has(frame.m)) return refusal(CODE_UNPAIRED, 'this connection is not paired')
|
|
@@ -748,10 +910,28 @@ export class BridgeConnection {
|
|
|
748
910
|
case METHOD_SESSION_SET_MODEL: return await this.#sessionSetModel(frame.p)
|
|
749
911
|
case METHOD_APPROVAL_RESOLVE: return this.#approvalResolve(frame.p)
|
|
750
912
|
case METHOD_QUESTION_ANSWER: return await this.#questionAnswer(frame.p)
|
|
913
|
+
case METHOD_QUESTION_LIST: return this.#questionList(frame.p)
|
|
914
|
+
case METHOD_PUSH_REGISTER: return await this.#pushRegister(frame.p)
|
|
915
|
+
case METHOD_USAGE_SESSION: return await this.#usageSession(frame.p)
|
|
916
|
+
case METHOD_USAGE_BALANCE: return await this.#usageBalance(frame.p)
|
|
917
|
+
case METHOD_USAGE_STATS: return await this.#usageStats(frame.p)
|
|
751
918
|
case METHOD_JOBS_LIST: return await this.#jobsList(frame.p)
|
|
752
919
|
case METHOD_PRESETS_LIST: return await this.#presetsList()
|
|
753
920
|
case METHOD_SESSION_CREATE: return await this.#sessionCreate(frame.p)
|
|
754
921
|
case METHOD_SESSION_FORK: return await this.#sessionFork(frame.p)
|
|
922
|
+
case METHOD_SESSION_COMMANDS: return await this.#sessionCommands(frame.p)
|
|
923
|
+
case METHOD_SESSION_COMMAND: return await this.#sessionCommand(frame.p)
|
|
924
|
+
case METHOD_SQUADS_STATE: return await this.#squadsState()
|
|
925
|
+
case METHOD_SQUADS_LIST: return await this.#squadsProjection('squads')
|
|
926
|
+
case METHOD_SQUADS_RUNS: return await this.#squadsProjection('runs')
|
|
927
|
+
case METHOD_SQUADS_RUN_MEMBER: return await this.#squadsRunMember(frame.p)
|
|
928
|
+
case METHOD_SQUADS_RUN_SQUAD: return await this.#squadsRunSquad(frame.p)
|
|
929
|
+
case METHOD_WORKSPACE_LIST: return this.#workspaceList()
|
|
930
|
+
case METHOD_SESSION_RENAME: return await this.#sessionRename(frame.p)
|
|
931
|
+
case METHOD_SESSION_PIN: return await this.#sessionSetWrite(METHOD_SESSION_PIN, 'pinSession', frame.p)
|
|
932
|
+
case METHOD_SESSION_UNPIN: return await this.#sessionSetWrite(METHOD_SESSION_UNPIN, 'unpinSession', frame.p)
|
|
933
|
+
case METHOD_SESSION_ARCHIVE: return await this.#sessionArchive(frame.p)
|
|
934
|
+
case METHOD_SESSION_UNARCHIVE: return await this.#sessionSetWrite(METHOD_SESSION_UNARCHIVE, 'unarchiveSession', frame.p)
|
|
755
935
|
default: return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
756
936
|
}
|
|
757
937
|
}
|
|
@@ -928,10 +1108,13 @@ export class BridgeConnection {
|
|
|
928
1108
|
const parentSessionId = optionalText(item.parentSessionId)
|
|
929
1109
|
sessions.push({
|
|
930
1110
|
id: item.sessionId,
|
|
931
|
-
// The
|
|
932
|
-
//
|
|
933
|
-
//
|
|
934
|
-
|
|
1111
|
+
// The row's own title. `SessionSummary` has no top-level `title`, but the
|
|
1112
|
+
// projection block beside it does — `projections.values.title` — and the
|
|
1113
|
+
// host answers that synchronously for a cold session too, which is the
|
|
1114
|
+
// field its own desktop list renders. The live-title cache is the
|
|
1115
|
+
// fallback for a row whose projection carries none, and no lookup is
|
|
1116
|
+
// spent here, so listing stays one call either way.
|
|
1117
|
+
title: projectedTitleOf(item) ?? this.#options.sessionMeta.cachedTitle(item.sessionId) ?? '',
|
|
935
1118
|
updatedAt: typeof item.updatedAt === 'number' ? item.updatedAt : 0,
|
|
936
1119
|
agentRunning: item.running === true,
|
|
937
1120
|
...(cwd === undefined ? {} : { cwd }),
|
|
@@ -945,6 +1128,15 @@ export class BridgeConnection {
|
|
|
945
1128
|
// child's durable parent is on its own row, so a client that lists before it
|
|
946
1129
|
// opens a session never costs a second read to address that session.
|
|
947
1130
|
this.#rememberLineage(sessions)
|
|
1131
|
+
// The workspace short name a notification carries is picked up from a roster
|
|
1132
|
+
// this bridge just served, because the alternative is a gateway read on the
|
|
1133
|
+
// notification path. Best effort by construction: a session never listed
|
|
1134
|
+
// simply produces a payload without `w`.
|
|
1135
|
+
this.#options.push?.noteSessions(sessions)
|
|
1136
|
+
// The same roster answers how many sessions are running, which is what a card
|
|
1137
|
+
// reports; the call returns before any delivery starts, so listing pays no
|
|
1138
|
+
// APNs round trip.
|
|
1139
|
+
this.#options.liveActivity?.noteRoster(sessions)
|
|
948
1140
|
return { ok: true, rows: sessions }
|
|
949
1141
|
}
|
|
950
1142
|
|
|
@@ -1543,6 +1735,282 @@ export class BridgeConnection {
|
|
|
1543
1735
|
return call.ok ? { ok: true, r: call.value } : call
|
|
1544
1736
|
}
|
|
1545
1737
|
|
|
1738
|
+
/**
|
|
1739
|
+
* Answer `squads.state`: the host's squad domain as this deployment publishes
|
|
1740
|
+
* it.
|
|
1741
|
+
*
|
|
1742
|
+
* The read is the `dsh-agents` plugin's own `{teams, runs, host, presets?}`
|
|
1743
|
+
* document — reached over loopback, or read out of its ledger when loopback
|
|
1744
|
+
* cannot answer — mapped onto the frozen body: a definition keeps its identity,
|
|
1745
|
+
* its pins and its members, a run names the squad it belongs to, and both lose
|
|
1746
|
+
* the bookkeeping fields this channel does not publish. A body this build cannot
|
|
1747
|
+
* read is refused rather than repaired, and a host that serves no domain answers
|
|
1748
|
+
* `squads-unavailable` instead of an empty board.
|
|
1749
|
+
*/
|
|
1750
|
+
async #squadsState(): Promise<DispatchOutcome> {
|
|
1751
|
+
const read = await this.#squadState()
|
|
1752
|
+
return read.ok ? { ok: true, r: read.state } : read
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* Answer `squads.list` or `squads.runs`: one half of the same read.
|
|
1757
|
+
*
|
|
1758
|
+
* Both commands read the domain once, exactly as `squads.state` does, and
|
|
1759
|
+
* publish the half they are named after. There is no second source: a squad
|
|
1760
|
+
* listing and a run listing can never disagree about a definition a launch
|
|
1761
|
+
* touched, because both are cut from one answer.
|
|
1762
|
+
* @param half - the body key this command publishes.
|
|
1763
|
+
*/
|
|
1764
|
+
async #squadsProjection(half: 'squads' | 'runs'): Promise<DispatchOutcome> {
|
|
1765
|
+
const read = await this.#squadState()
|
|
1766
|
+
if (!read.ok) return read
|
|
1767
|
+
return { ok: true, r: { [half]: read.state[half] } }
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
/**
|
|
1771
|
+
* Read the squad domain once.
|
|
1772
|
+
*
|
|
1773
|
+
* The transport answers the plugin's rows unread and this bridge maps them
|
|
1774
|
+
* here, so the ledger fallback and the loopback route are the same answer as far
|
|
1775
|
+
* as a client can tell.
|
|
1776
|
+
*/
|
|
1777
|
+
async #squadState(): Promise<{ readonly ok: true; readonly state: SquadsState } | CallFailure> {
|
|
1778
|
+
const transport = this.#options.squads
|
|
1779
|
+
if (transport === undefined) {
|
|
1780
|
+
return refusal(CODE_SQUADS_UNAVAILABLE, 'this host serves no dsh-agents squad domain')
|
|
1781
|
+
}
|
|
1782
|
+
try {
|
|
1783
|
+
return { ok: true, state: squadsStateOf(await transport.state()) }
|
|
1784
|
+
} catch (error) {
|
|
1785
|
+
return this.#squadFailure(error)
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
/**
|
|
1790
|
+
* Answer `squads.runMember`: launch one member session.
|
|
1791
|
+
*
|
|
1792
|
+
* Only the two names the command needs are read here. Which preset, permission,
|
|
1793
|
+
* model and brief that session is composed from is the definition's own, and
|
|
1794
|
+
* whether this host can launch at all is the transport's: an unknown squad or
|
|
1795
|
+
* member, an unusable preset and a squad that pins no workspace all cross as the
|
|
1796
|
+
* plugin's own code.
|
|
1797
|
+
* @param payload - `{squadId, memberId, firstMessage?}`.
|
|
1798
|
+
*/
|
|
1799
|
+
async #squadsRunMember(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1800
|
+
const transport = this.#options.squads
|
|
1801
|
+
if (transport === undefined) {
|
|
1802
|
+
return refusal(CODE_SQUADS_UNAVAILABLE, 'this host serves no dsh-agents squad domain')
|
|
1803
|
+
}
|
|
1804
|
+
const request = parseSquadMemberRun(payload)
|
|
1805
|
+
if (request === undefined) return refusal(CODE_INVALID_ARGS, 'squads.runMember requires squadId and memberId')
|
|
1806
|
+
try {
|
|
1807
|
+
return { ok: true, r: { run: squadRunOf(await transport.runMember(request)) } }
|
|
1808
|
+
} catch (error) {
|
|
1809
|
+
return this.#squadFailure(error)
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
/**
|
|
1814
|
+
* Answer `squads.runSquad`: launch the Lead session of one whole squad.
|
|
1815
|
+
*
|
|
1816
|
+
* The goal travels as the plugin's own `goal` — it becomes part of the Lead's
|
|
1817
|
+
* system-prompt context rather than its first user message — so nothing about
|
|
1818
|
+
* what the squad must achieve is modelled here.
|
|
1819
|
+
* @param payload - `{squadId, goal?}`.
|
|
1820
|
+
*/
|
|
1821
|
+
async #squadsRunSquad(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1822
|
+
const transport = this.#options.squads
|
|
1823
|
+
if (transport === undefined) {
|
|
1824
|
+
return refusal(CODE_SQUADS_UNAVAILABLE, 'this host serves no dsh-agents squad domain')
|
|
1825
|
+
}
|
|
1826
|
+
const request = parseSquadRun(payload)
|
|
1827
|
+
if (request === undefined) return refusal(CODE_INVALID_ARGS, 'squads.runSquad requires squadId')
|
|
1828
|
+
try {
|
|
1829
|
+
return { ok: true, r: { run: squadRunOf(await transport.runSquad(request)) } }
|
|
1830
|
+
} catch (error) {
|
|
1831
|
+
return this.#squadFailure(error)
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
/**
|
|
1836
|
+
* Convert whatever the squad transport raised into a refusal that names a code.
|
|
1837
|
+
*
|
|
1838
|
+
* A refusal the transport built keeps its own code — that is how a domain
|
|
1839
|
+
* refusal and this bridge's two capability codes stay distinguishable. Anything
|
|
1840
|
+
* else is a host failure this channel could not attribute, and it is refused as
|
|
1841
|
+
* an unreachable domain rather than as an internal error: no squad answer was
|
|
1842
|
+
* read, which is exactly what `squads-unavailable` says.
|
|
1843
|
+
*/
|
|
1844
|
+
#squadFailure(error: unknown): CallFailure {
|
|
1845
|
+
if (isSquadRefusal(error)) return refusal(error.code, error.message)
|
|
1846
|
+
this.#options.log(`squad domain read failed: ${failureText(error)}`)
|
|
1847
|
+
return refusal(CODE_SQUADS_UNAVAILABLE, failureText(error))
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
/**
|
|
1851
|
+
* Answer `workspace.list`: the host's workspaces and its two global sets.
|
|
1852
|
+
*
|
|
1853
|
+
* A pure in-process read: `list()` answers a fresh array in registry order and
|
|
1854
|
+
* both sets are synchronous getters, so nothing here awaits, subscribes or
|
|
1855
|
+
* touches persistence, and a write this bridge forwarded a moment ago is
|
|
1856
|
+
* visible to the very next call. Nothing is sorted — the registry's own order
|
|
1857
|
+
* is the display order — and no row carries a workspace's `sessionIds`: the
|
|
1858
|
+
* roster already carries every session's `cwd`, a client groups for itself, and
|
|
1859
|
+
* that account would make one frame grow with the session count.
|
|
1860
|
+
*
|
|
1861
|
+
* A host that composes no registry is refused rather than answered with an
|
|
1862
|
+
* empty list: an empty list says "this host has no workspaces", which is a
|
|
1863
|
+
* different sentence from "there is nothing here to ask".
|
|
1864
|
+
*/
|
|
1865
|
+
#workspaceList(): DispatchOutcome {
|
|
1866
|
+
const listed = this.#options.sessionMeta.workspaceList()
|
|
1867
|
+
if (listed === undefined) return refusal(CODE_INTERNAL, 'this host composes no workspaceRegistry service')
|
|
1868
|
+
return { ok: true, r: listed }
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
/**
|
|
1872
|
+
* Answer `session.rename`: forward one `SessionRenameRequest` and hand back
|
|
1873
|
+
* the host's own `SessionRenameValue`.
|
|
1874
|
+
*
|
|
1875
|
+
* The write goes through the gateway — `session/rename`, never the title
|
|
1876
|
+
* service this process holds — because that endpoint restores a cold session
|
|
1877
|
+
* before it writes, so a client needs no resume path of its own. The title is
|
|
1878
|
+
* required and is text: an empty string is not a name anyone could have meant,
|
|
1879
|
+
* and it is refused here instead of being written. Everything after that is
|
|
1880
|
+
* the host's: the normalized title, the durable seq that committed it, and a
|
|
1881
|
+
* refusal such as `session/not-found` cross verbatim.
|
|
1882
|
+
* @param payload - `{sessionId, title}`.
|
|
1883
|
+
*/
|
|
1884
|
+
async #sessionRename(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1885
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
1886
|
+
const title = stringArg(payload, 'title')
|
|
1887
|
+
if (sessionId === undefined || title === undefined) {
|
|
1888
|
+
return refusal(CODE_INVALID_ARGS, 'session.rename requires sessionId and title')
|
|
1889
|
+
}
|
|
1890
|
+
const call = await this.#unary('session', 'rename', { sessionId, title })
|
|
1891
|
+
return call.ok ? { ok: true, r: call.value } : call
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
/**
|
|
1895
|
+
* Forward one registry set mutation and answer `{ok:true}`.
|
|
1896
|
+
*
|
|
1897
|
+
* Three of the five session-admin writes are the same call under a different
|
|
1898
|
+
* endpoint name, so they share one body: `{sessionId}` in, one decided answer
|
|
1899
|
+
* out. Nothing is branched on — pin, unpin and unarchive take no second
|
|
1900
|
+
* argument to branch on — and the host's own set answer
|
|
1901
|
+
* (`WorkspacePinValue` / `WorkspaceArchiveValue`) is deliberately not
|
|
1902
|
+
* republished: a client that wants the new set reads `workspace.list`, the one
|
|
1903
|
+
* read that serves both. An archive's optional `stopActivity` is
|
|
1904
|
+
* `#sessionArchive`'s own concern.
|
|
1905
|
+
* @param method - the protocol method being answered, named in the refusal.
|
|
1906
|
+
* @param endpoint - the `workspace` registry method behind it.
|
|
1907
|
+
* @param payload - `{sessionId}`.
|
|
1908
|
+
*/
|
|
1909
|
+
async #sessionSetWrite(method: string, endpoint: string, payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1910
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
1911
|
+
if (sessionId === undefined) return refusal(CODE_INVALID_ARGS, `${method} requires sessionId`)
|
|
1912
|
+
const call = await this.#unary('workspace', endpoint, { sessionId })
|
|
1913
|
+
return call.ok ? { ok: true, r: { ok: true } } : call
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
/**
|
|
1917
|
+
* Answer `session.archive`, whose request may ask the host to stop the
|
|
1918
|
+
* session's running work first.
|
|
1919
|
+
*
|
|
1920
|
+
* `stopActivity` is the host's own choice and only a boolean may cross: sent
|
|
1921
|
+
* without it, a session that is still running is refused by the host as
|
|
1922
|
+
* `workspace/session-active`; sent as `true`, the archive is written and that
|
|
1923
|
+
* work is stopped. Archiving is a session-level act — it never touches the
|
|
1924
|
+
* workspace the session belongs to — and the archived session keeps its slot,
|
|
1925
|
+
* so `session.unarchive` puts it back where it was. None of that is modelled
|
|
1926
|
+
* here.
|
|
1927
|
+
* @param payload - `{sessionId, stopActivity?}`.
|
|
1928
|
+
*/
|
|
1929
|
+
async #sessionArchive(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1930
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
1931
|
+
if (sessionId === undefined) return refusal(CODE_INVALID_ARGS, 'session.archive requires sessionId')
|
|
1932
|
+
const stopActivity = payload.stopActivity
|
|
1933
|
+
if (stopActivity !== undefined && typeof stopActivity !== 'boolean') {
|
|
1934
|
+
return refusal(CODE_INVALID_ARGS, 'session.archive stopActivity must be a boolean when present')
|
|
1935
|
+
}
|
|
1936
|
+
const call = await this.#unary('workspace', 'archiveSession', { sessionId, ...(stopActivity === undefined ? {} : { stopActivity }) })
|
|
1937
|
+
return call.ok ? { ok: true, r: { ok: true } } : call
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
/**
|
|
1941
|
+
* Answer `session.commands`: one session's slash commands and skills, in one
|
|
1942
|
+
* directory.
|
|
1943
|
+
*
|
|
1944
|
+
* Both catalogs are read live and neither is modelled here: `commands/list`
|
|
1945
|
+
* is the host registry's own effective view for this Agent — a command
|
|
1946
|
+
* registered through an Agent-scoped child shadows the global one — and
|
|
1947
|
+
* `skills/list` is the catalog this session's cwd and preset select, so what
|
|
1948
|
+
* a phone lists is what the desktop composer would offer that same session. A
|
|
1949
|
+
* row crosses with the name and the summary the host publishes for it, plus
|
|
1950
|
+
* the whole draft line that invokes it, separator space included.
|
|
1951
|
+
*
|
|
1952
|
+
* Nothing is paged, re-sorted or cached: every command first, every skill
|
|
1953
|
+
* after, each in the host's own order, and the next call reads both again.
|
|
1954
|
+
* @param payload - `{sessionId}`.
|
|
1955
|
+
*/
|
|
1956
|
+
async #sessionCommands(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1957
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
1958
|
+
if (sessionId === undefined) return refusal(CODE_INVALID_ARGS, 'session.commands requires sessionId')
|
|
1959
|
+
const commands = await this.#unary('commands', 'list', { agentId: sessionId })
|
|
1960
|
+
if (!commands.ok) return this.#commandsFailure('commands', 'list', commands)
|
|
1961
|
+
if (!Array.isArray(commands.value)) return refusal(CODE_INTERNAL, 'commands/list returned no command directory')
|
|
1962
|
+
const skills = await this.#unary('skills', 'list', { sessionId })
|
|
1963
|
+
if (!skills.ok) return this.#commandsFailure('skills', 'list', skills)
|
|
1964
|
+
if (!isRecord(skills.value) || !Array.isArray(skills.value.skills)) {
|
|
1965
|
+
return refusal(CODE_INTERNAL, 'skills/list returned no skill catalog')
|
|
1966
|
+
}
|
|
1967
|
+
return { ok: true, r: { sessionId, items: commandItems(commands.value, skills.value.skills) } }
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
/**
|
|
1971
|
+
* Answer `session.command`: run one draft line on the host and report whether
|
|
1972
|
+
* the host resolved it.
|
|
1973
|
+
*
|
|
1974
|
+
* The line crosses exactly as the client wrote it, because what a line means
|
|
1975
|
+
* belongs to the host: its parser decides whether the text is a slash command
|
|
1976
|
+
* at all, and its registry decides whether that name is one this Agent has. A
|
|
1977
|
+
* line the host resolves runs — this method does not pre-judge a command,
|
|
1978
|
+
* replay one, or refuse a line it does not recognise — and the answer is then
|
|
1979
|
+
* `matched: true`. A line the host resolves to nothing (no leading slash, an
|
|
1980
|
+
* unknown name, or a skill's `/name` gesture, which is prompt text rather than
|
|
1981
|
+
* a registry entry) answers `matched: false`, and nothing ran.
|
|
1982
|
+
*
|
|
1983
|
+
* A plain invocation carries no attachments: a draft line has none, the
|
|
1984
|
+
* endpoint requires the key, so an empty array is what crosses.
|
|
1985
|
+
* @param payload - `{sessionId, line}`.
|
|
1986
|
+
*/
|
|
1987
|
+
async #sessionCommand(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1988
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
1989
|
+
const line = stringArg(payload, 'line')
|
|
1990
|
+
if (sessionId === undefined || line === undefined) {
|
|
1991
|
+
return refusal(CODE_INVALID_ARGS, 'session.command requires sessionId and line')
|
|
1992
|
+
}
|
|
1993
|
+
const call = await this.#unary('commands', 'execute', { agentId: sessionId, line, submittedAttachments: [] })
|
|
1994
|
+
if (!call.ok) return this.#commandsFailure('commands', 'execute', call)
|
|
1995
|
+
return { ok: true, r: { matched: call.value !== undefined } }
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
/**
|
|
1999
|
+
* Re-answer one command-channel failure.
|
|
2000
|
+
*
|
|
2001
|
+
* An endpoint this host does not serve at all is this bridge's own
|
|
2002
|
+
* `commands-unavailable`, so a phone stops offering the directory instead of
|
|
2003
|
+
* showing a call that fails every time; a call the host refused, and a channel
|
|
2004
|
+
* that is merely still activating, cross with the failure's own code and text.
|
|
2005
|
+
* @param namespace - the Remote namespace that failed.
|
|
2006
|
+
* @param method - the endpoint that failed.
|
|
2007
|
+
* @param failure - the refusal `#unary` produced.
|
|
2008
|
+
*/
|
|
2009
|
+
#commandsFailure(namespace: string, method: string, failure: CallFailure): CallFailure {
|
|
2010
|
+
if (!commandsUnavailable(failure.code)) return failure
|
|
2011
|
+
return refusal(CODE_COMMANDS_UNAVAILABLE, `this host serves no ${namespace}/${method} endpoint`)
|
|
2012
|
+
}
|
|
2013
|
+
|
|
1546
2014
|
#approvalResolve(payload: Readonly<Record<string, unknown>>): DispatchOutcome {
|
|
1547
2015
|
const id = stringArg(payload, 'id')
|
|
1548
2016
|
const decision = payload.decision
|
|
@@ -1575,6 +2043,166 @@ export class BridgeConnection {
|
|
|
1575
2043
|
return reply.ok ? { ok: true, r: { ok: true } } : refusal(reply.code, reply.msg)
|
|
1576
2044
|
}
|
|
1577
2045
|
|
|
2046
|
+
/**
|
|
2047
|
+
* Answer one `question.list`: the questions this bridge is still holding.
|
|
2048
|
+
*
|
|
2049
|
+
* A pure read — it claims nothing, moves no deadline, triggers no `continued`
|
|
2050
|
+
* and writes no host state — because its whole purpose is to let a client that
|
|
2051
|
+
* was disconnected reconcile cards it already holds: a question missing from
|
|
2052
|
+
* this list is one nobody can answer any more, since the bridge withdraws every
|
|
2053
|
+
* claim when the last paired device leaves and the phone is not there to hear
|
|
2054
|
+
* it.
|
|
2055
|
+
*
|
|
2056
|
+
* Only a COMPLETE list may retire a card on the client, so a registry this
|
|
2057
|
+
* bridge cannot read answers `question-list-unavailable` rather than an empty
|
|
2058
|
+
* list: an unreadable set is not an empty one.
|
|
2059
|
+
* @param payload - must be the empty object: the held set is global, not scoped.
|
|
2060
|
+
*/
|
|
2061
|
+
#questionList(payload: Readonly<Record<string, unknown>>): DispatchOutcome {
|
|
2062
|
+
if (Object.keys(payload).length > 0) {
|
|
2063
|
+
return refusal(CODE_INVALID_ARGS, 'question.list takes no arguments')
|
|
2064
|
+
}
|
|
2065
|
+
let questions: readonly ListedQuestion[]
|
|
2066
|
+
try {
|
|
2067
|
+
questions = this.#options.questions.listed()
|
|
2068
|
+
} catch (error) {
|
|
2069
|
+
this.#options.log(`question.list could not read the held questions: ${failureText(error)}`)
|
|
2070
|
+
return refusal(CODE_QUESTION_LIST_UNAVAILABLE, 'this bridge could not read the questions it is holding')
|
|
2071
|
+
}
|
|
2072
|
+
const answer: QuestionListPayload = { questions, complete: true }
|
|
2073
|
+
return { ok: true, r: answer }
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
/**
|
|
2077
|
+
* The body of `push.register` (protocol §28.3).
|
|
2078
|
+
*
|
|
2079
|
+
* A write, and the only one this channel has: the device's APNs registration
|
|
2080
|
+
* lands in its own row of the roster file, beside the identity it already
|
|
2081
|
+
* proved, so revoking the device revokes its notifications with it and no
|
|
2082
|
+
* second identity or second table exists.
|
|
2083
|
+
*
|
|
2084
|
+
* The answer is `{ok:true}` and nothing else. There is deliberately no echo of
|
|
2085
|
+
* the token and no "ready" flag: this bridge cannot know whether APNs will
|
|
2086
|
+
* accept the token, and a field claiming it did would be a lie a client might
|
|
2087
|
+
* render. A bridge with no signing key answers identically — a client has no
|
|
2088
|
+
* second code path for that difference.
|
|
2089
|
+
* @param payload - `{deviceToken, environment, bundle, liveActivityTokens?, locale?}`.
|
|
2090
|
+
*/
|
|
2091
|
+
async #pushRegister(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
2092
|
+
const deviceToken = payload.deviceToken
|
|
2093
|
+
const environment = payload.environment
|
|
2094
|
+
const bundle = payload.bundle
|
|
2095
|
+
if (typeof deviceToken !== 'string' || deviceToken === '' || !/^[0-9a-fA-F]+$/.test(deviceToken)) {
|
|
2096
|
+
return refusal(CODE_INVALID_ARGS, 'deviceToken must be the hex string APNs handed the app')
|
|
2097
|
+
}
|
|
2098
|
+
if (environment !== 'sandbox' && environment !== 'production') {
|
|
2099
|
+
return refusal(CODE_INVALID_ARGS, 'environment must be "sandbox" or "production"')
|
|
2100
|
+
}
|
|
2101
|
+
if (typeof bundle !== 'string' || bundle === '') return refusal(CODE_INVALID_ARGS, 'bundle must be a non-empty string')
|
|
2102
|
+
const locale = optionalText(payload.locale)
|
|
2103
|
+
let liveActivityTokens: readonly PushLiveActivityToken[] | undefined
|
|
2104
|
+
if (payload.liveActivityTokens !== undefined) {
|
|
2105
|
+
const raw = payload.liveActivityTokens
|
|
2106
|
+
if (!Array.isArray(raw)) return refusal(CODE_INVALID_ARGS, 'liveActivityTokens must be an array')
|
|
2107
|
+
const collected: PushLiveActivityToken[] = []
|
|
2108
|
+
for (const entry of raw) {
|
|
2109
|
+
if (!isRecord(entry)) return refusal(CODE_INVALID_ARGS, 'each liveActivityTokens entry needs an activityId and a token')
|
|
2110
|
+
const activityId = entry.activityId
|
|
2111
|
+
const token = entry.token
|
|
2112
|
+
if (typeof activityId !== 'string' || activityId === '' || typeof token !== 'string' || token === '') {
|
|
2113
|
+
return refusal(CODE_INVALID_ARGS, 'each liveActivityTokens entry needs an activityId and a token')
|
|
2114
|
+
}
|
|
2115
|
+
// §29.9: a card names the session it belongs to. The key is optional —
|
|
2116
|
+
// a client that omits it is registered exactly as before — but a value
|
|
2117
|
+
// that is present has to be a session id, because a card carrying a
|
|
2118
|
+
// malformed address would be pushed to nobody while looking registered.
|
|
2119
|
+
const sessionId = entry.sessionId
|
|
2120
|
+
if (sessionId !== undefined && (typeof sessionId !== 'string' || sessionId === '')) {
|
|
2121
|
+
return refusal(CODE_INVALID_ARGS, 'a liveActivityTokens sessionId must be a non-empty string')
|
|
2122
|
+
}
|
|
2123
|
+
collected.push({ activityId, token, ...(sessionId === undefined ? {} : { sessionId }) })
|
|
2124
|
+
}
|
|
2125
|
+
liveActivityTokens = collected
|
|
2126
|
+
}
|
|
2127
|
+
const deviceId = this.#deviceId
|
|
2128
|
+
if (deviceId === undefined) return refusal(CODE_UNPAIRED, 'this connection is not paired')
|
|
2129
|
+
const registration: PushRegistration = {
|
|
2130
|
+
deviceToken,
|
|
2131
|
+
environment: environment as PushEnvironment,
|
|
2132
|
+
bundle,
|
|
2133
|
+
...(locale === undefined ? {} : { locale }),
|
|
2134
|
+
...(liveActivityTokens === undefined ? {} : { liveActivityTokens }),
|
|
2135
|
+
registeredAt: this.#options.now(),
|
|
2136
|
+
}
|
|
2137
|
+
try {
|
|
2138
|
+
const stored = await this.#options.roster.registerPush(deviceId, registration)
|
|
2139
|
+
if (!stored) return refusal(CODE_INTERNAL, 'this connection names no paired device')
|
|
2140
|
+
// The roster is the only place a standing watcher is armed from: the
|
|
2141
|
+
// session a card names gets a follow stream of this bridge's own, and it
|
|
2142
|
+
// keeps it until the last token for that session is gone.
|
|
2143
|
+
try {
|
|
2144
|
+
this.#options.onPushRegistered?.()
|
|
2145
|
+
} catch (error) {
|
|
2146
|
+
this.#options.log(`standing watchers could not be reconciled: ${failureText(error)}`)
|
|
2147
|
+
}
|
|
2148
|
+
} catch (error) {
|
|
2149
|
+
// Registration failing has to reach the client: it decides whether to
|
|
2150
|
+
// retry, and a token that never reached the disk is a device that will
|
|
2151
|
+
// never be notified.
|
|
2152
|
+
return refusal(CODE_INTERNAL, `could not write the device roster: ${failureText(error)}`)
|
|
2153
|
+
}
|
|
2154
|
+
return { ok: true, r: { ok: true } }
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
/**
|
|
2158
|
+
* 答一次 `usage.session`:一条会话的上下文窗口与下一次请求的组成估算。
|
|
2159
|
+
*
|
|
2160
|
+
* 两个读数各有自己的来源,也各有自己的缺席方式:`contextWindow` 只在宿主
|
|
2161
|
+
* 广播过 `request/context` 时出现,`composition` 只在观察到任何原文时出现,
|
|
2162
|
+
* 两项都没有就是空对象。会话本身不存在是另一种情况,回的是
|
|
2163
|
+
* `unknown-session`,而不是一个读不到任何东西的空对象;而「存在」由活会话
|
|
2164
|
+
* 存储、本进程观察到的事件与宿主名册三处中任意一处证明(见 `core/usage.ts`)。
|
|
2165
|
+
* @param payload - `{sessionId}`。
|
|
2166
|
+
*/
|
|
2167
|
+
async #usageSession(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
2168
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
2169
|
+
if (sessionId === undefined) return refusal(CODE_INVALID_ARGS, 'usage.session requires sessionId')
|
|
2170
|
+
const answer = await this.#options.usage.session(sessionId)
|
|
2171
|
+
if (answer === undefined) return refusal(CODE_UNKNOWN_SESSION, `session ${sessionId} does not exist on this host`)
|
|
2172
|
+
return { ok: true, r: answer }
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
/**
|
|
2176
|
+
* 答一次 `usage.balance`:某条 provider 路由背后账户的余额。
|
|
2177
|
+
*
|
|
2178
|
+
* 判定与缓存全在 `core/usage.ts`:不是官方路由、不指向官方 origin、凭据拿
|
|
2179
|
+
* 不到,都不发上游请求,各自的理由按 `unsupported` 的三种取值过线。这里
|
|
2180
|
+
* 只转发,因为余额是账户的事实,本连接既不改写也不补默认值。
|
|
2181
|
+
* @param payload - `{provider}`。
|
|
2182
|
+
*/
|
|
2183
|
+
async #usageBalance(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
2184
|
+
const provider = stringArg(payload, 'provider')
|
|
2185
|
+
if (provider === undefined) return refusal(CODE_INVALID_ARGS, 'usage.balance requires provider')
|
|
2186
|
+
return { ok: true, r: await this.#options.usage.balance(provider) }
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
/**
|
|
2190
|
+
* 答一次 `usage.stats`:宿主自己那份按天记的 token 台账的聚合。
|
|
2191
|
+
*
|
|
2192
|
+
* `days` 省略按 14,超出 60 按 60 收——收窗口的是 `core/usage-stats.ts`,不是
|
|
2193
|
+
* 协议:客户端给的天数只是「想看多深」,不该被当成一次分配。这里只拒绝根本
|
|
2194
|
+
* 不是数字的那种值,其余一律转发,包括读不到时那份 `unsupported`,
|
|
2195
|
+
* 因为「这台 Mac 没挂那个插件」不是一次失败。
|
|
2196
|
+
* @param payload - `{days?}`。
|
|
2197
|
+
*/
|
|
2198
|
+
async #usageStats(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
2199
|
+
const days = payload.days
|
|
2200
|
+
if (days !== undefined && (typeof days !== 'number' || !Number.isFinite(days))) {
|
|
2201
|
+
return refusal(CODE_INVALID_ARGS, 'usage.stats requires days to be a number')
|
|
2202
|
+
}
|
|
2203
|
+
return { ok: true, r: await this.#options.usage.stats(days) }
|
|
2204
|
+
}
|
|
2205
|
+
|
|
1578
2206
|
/**
|
|
1579
2207
|
* Answer one `jobs.list` and start watching that scope.
|
|
1580
2208
|
*
|
|
@@ -1809,6 +2437,14 @@ export class BridgeConnection {
|
|
|
1809
2437
|
const delta = deltaOfFollowFrame(next.value, this.#toolCalls, this.#messages, memory)
|
|
1810
2438
|
if (delta === undefined) continue
|
|
1811
2439
|
this.#emit(TOPIC_SESSION_DELTA, { sessionId: subscription.sessionId, ...delta })
|
|
2440
|
+
// One turn ended, so a phone that is not holding this socket may want to
|
|
2441
|
+
// know. The call returns before anything is sent and swallows its own
|
|
2442
|
+
// failures: the frame above is already gone, and a notification is not
|
|
2443
|
+
// worth a delayed or aborted stream (protocol §28.5).
|
|
2444
|
+
if (delta.kind === 'turn-end') {
|
|
2445
|
+
this.#options.push?.notify(pushEventOfTurnEnd(delta.reason), subscription.sessionId)
|
|
2446
|
+
this.#options.liveActivity?.noteTurnEnd(subscription.sessionId, delta.reason)
|
|
2447
|
+
}
|
|
1812
2448
|
}
|
|
1813
2449
|
} catch (error) {
|
|
1814
2450
|
if (!subscription.controller.signal.aborted) {
|