@linxin666/dsh-remote-bridge 0.4.12 → 0.4.14
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 +53 -11
- package/README.zh.md +53 -11
- package/lib/client.js +112 -90
- package/lib/client.js.map +1 -1
- package/lib/index.js +18800 -334
- package/lib/types/client/PairPanel.d.ts +7 -0
- package/lib/types/client/locales.d.ts +13 -10
- package/lib/types/core/connection.d.ts +50 -0
- package/lib/types/core/devices.d.ts +80 -6
- package/lib/types/core/liveactivity.d.ts +7 -0
- package/lib/types/core/model-efforts.d.ts +83 -0
- package/lib/types/core/protocol.d.ts +24 -0
- package/lib/types/core/push.d.ts +70 -3
- package/lib/types/host/firewall.d.ts +74 -0
- package/lib/types/host/host-bind-capability.d.ts +90 -0
- package/lib/types/host/ingress-gate.d.ts +59 -0
- package/lib/types/host/ingress-sync.d.ts +429 -0
- package/lib/types/host/lan-bind-plan.d.ts +96 -0
- package/lib/types/host/lan-bind.d.ts +106 -0
- package/lib/types/host/lan-forward.d.ts +112 -0
- package/lib/types/host/lan.d.ts +61 -0
- package/lib/types/host/pair-origin.d.ts +34 -19
- package/lib/types/host/relay-bridge-connector.d.ts +234 -0
- package/lib/types/host/relay-connector.d.ts +156 -0
- package/lib/types/host/relay-control.d.ts +195 -0
- package/lib/types/host/relay-registry.d.ts +123 -0
- package/lib/types/host/relay-secondary.d.ts +209 -0
- package/lib/types/host/relay.d.ts +170 -0
- package/lib/types/host/routes.d.ts +29 -1
- package/lib/types/host/tunnel-orphan.d.ts +145 -0
- package/lib/types/host/tunnel-plan.d.ts +41 -0
- package/lib/types/host/tunnel.d.ts +289 -0
- package/lib/types/index.d.ts +488 -1
- package/package.json +4 -2
- package/src/client/PairPanel.tsx +21 -0
- package/src/client/locales.ts +19 -15
- package/src/client/panel.module.css +48 -1
- package/src/core/connection.ts +316 -11
- package/src/core/devices.ts +165 -16
- package/src/core/hub.ts +17 -5
- package/src/core/liveactivity.ts +16 -2
- package/src/core/model-efforts.ts +223 -0
- package/src/core/protocol.ts +27 -0
- package/src/core/push.ts +90 -5
- package/src/host/firewall.ts +230 -0
- package/src/host/host-bind-capability.ts +249 -0
- package/src/host/ingress-gate.ts +203 -0
- package/src/host/ingress-sync.ts +1074 -0
- package/src/host/lan-bind-plan.ts +134 -0
- package/src/host/lan-bind.ts +250 -0
- package/src/host/lan-forward.ts +321 -0
- package/src/host/lan.ts +142 -0
- package/src/host/pair-origin.ts +44 -38
- package/src/host/relay-bridge-connector.ts +849 -0
- package/src/host/relay-connector.ts +324 -0
- package/src/host/relay-control.ts +315 -0
- package/src/host/relay-registry.ts +275 -0
- package/src/host/relay-secondary.ts +478 -0
- package/src/host/relay.ts +294 -0
- package/src/host/routes.ts +57 -3
- package/src/host/tunnel-orphan.ts +325 -0
- package/src/host/tunnel-plan.ts +58 -0
- package/src/host/tunnel.ts +688 -0
- package/src/index.ts +1516 -23
|
@@ -55,7 +55,9 @@
|
|
|
55
55
|
background: transparent;
|
|
56
56
|
color: var(--dsw-alias-label-secondary);
|
|
57
57
|
cursor: pointer;
|
|
58
|
-
/*
|
|
58
|
+
/* Rail stack: the trigger leads the seat, which is where a centered 36px
|
|
59
|
+
column reads best. The wide seat places it on the trailing edge instead —
|
|
60
|
+
see the wide rule below. */
|
|
59
61
|
order: -1;
|
|
60
62
|
transition: background-color var(--ds-transition-duration) ease, color var(--ds-transition-duration) ease;
|
|
61
63
|
}
|
|
@@ -88,6 +90,19 @@
|
|
|
88
90
|
outline-offset: var(--pair-focus-offset);
|
|
89
91
|
}
|
|
90
92
|
|
|
93
|
+
/* Wide seat: the shell lays `sidebar.footer.action` out as a left-aligned row, so
|
|
94
|
+
a position in flow alone leaves this trigger at the row's leading edge. Ordering
|
|
95
|
+
it last and handing it the free space in front puts it on the trailing edge,
|
|
96
|
+
beside the seat's other occupants rather than ahead of them. Both halves are
|
|
97
|
+
needed: order alone would only place it next to the previous occupant. The
|
|
98
|
+
margin is scoped to the wide layout because the rail stacks the seat into a
|
|
99
|
+
centered column, where an auto inline margin would pull the icon off that
|
|
100
|
+
column's axis. */
|
|
101
|
+
.trigger[data-wide='wide'] {
|
|
102
|
+
order: 1;
|
|
103
|
+
margin-inline-start: auto;
|
|
104
|
+
}
|
|
105
|
+
|
|
91
106
|
/* ── Rail seat geometry (official-shell compatibility) ──
|
|
92
107
|
|
|
93
108
|
The shell renders `sidebar.footer.action` as a centered horizontal row, so
|
|
@@ -321,6 +336,38 @@
|
|
|
321
336
|
line-height: var(--pair-text-line);
|
|
322
337
|
}
|
|
323
338
|
|
|
339
|
+
/* ── Beta community invite ──
|
|
340
|
+
|
|
341
|
+
The body's closing line in every state, and the card's only outbound
|
|
342
|
+
destination while the app is in testing. It sits in the supporting tier the
|
|
343
|
+
subtitle already uses and borrows its type from the same token, so the view
|
|
344
|
+
keeps three sizes; the hue is the secondary label slot rather than the
|
|
345
|
+
tertiary hint slot above it, because this line is the one interactive string
|
|
346
|
+
on the card and the tertiary slot does not clear the text contrast floor on
|
|
347
|
+
the light surface. Underlining it is deliberate: the affordance must not be
|
|
348
|
+
carried by colour alone, and the whole sentence is the target, so the
|
|
349
|
+
element is sized to its text instead of filling the row (A6 keeps no empty
|
|
350
|
+
stretch of card clickable). Hover and focus return the two slots the seat
|
|
351
|
+
trigger already uses, so one family of states reads the same everywhere. */
|
|
352
|
+
.invite {
|
|
353
|
+
align-self: flex-start;
|
|
354
|
+
margin: 0;
|
|
355
|
+
color: var(--dsw-alias-label-secondary);
|
|
356
|
+
font-size: var(--pair-text-size);
|
|
357
|
+
line-height: var(--pair-text-line);
|
|
358
|
+
text-decoration: underline;
|
|
359
|
+
transition: color var(--ds-transition-duration) ease;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
.invite:hover {
|
|
363
|
+
color: var(--dsw-alias-label-primary);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
.invite:focus-visible {
|
|
367
|
+
outline: var(--dsw-focus-ring-width) solid var(--dsw-focus-ring-color, var(--dsw-alias-state-business-primary));
|
|
368
|
+
outline-offset: var(--pair-focus-offset);
|
|
369
|
+
}
|
|
370
|
+
|
|
324
371
|
/* ── Refusals ── */
|
|
325
372
|
|
|
326
373
|
.notice {
|
package/src/core/connection.ts
CHANGED
|
@@ -26,6 +26,7 @@ import type { RemoteBridgeMethodResult, RemoteBridgeRegistry } from './extension
|
|
|
26
26
|
import { errorCodeOf, failureText, withServiceUnavailableRetry } from './gateway-errors.ts'
|
|
27
27
|
import { scopeKey, type JobRoster, type JobsPayload } from './jobs.ts'
|
|
28
28
|
import type { LiveActivityNotifier } from './liveactivity.ts'
|
|
29
|
+
import type { ModelEffortPreferences } from './model-efforts.ts'
|
|
29
30
|
import type { PairingService } from './pairing.ts'
|
|
30
31
|
import type { PushNotifier } from './push.ts'
|
|
31
32
|
import { pushEventOfTurnEnd } from './push.ts'
|
|
@@ -97,11 +98,13 @@ import {
|
|
|
97
98
|
METHOD_SESSION_PIN,
|
|
98
99
|
METHOD_SESSION_PROMPT,
|
|
99
100
|
METHOD_SESSION_RENAME,
|
|
101
|
+
METHOD_SESSION_SET_EFFORT,
|
|
100
102
|
METHOD_SESSION_SET_MODEL,
|
|
101
103
|
METHOD_SESSION_SET_PERMISSION,
|
|
102
104
|
METHOD_SESSION_SNAPSHOT,
|
|
103
105
|
METHOD_SESSION_TITLES,
|
|
104
106
|
METHOD_SESSION_UNARCHIVE,
|
|
107
|
+
METHOD_RELAY_GRANT,
|
|
105
108
|
METHOD_UI_EVENT,
|
|
106
109
|
METHOD_UI_LIST,
|
|
107
110
|
METHOD_SESSION_UNPIN,
|
|
@@ -114,6 +117,7 @@ import {
|
|
|
114
117
|
METHOD_USAGE_SESSION,
|
|
115
118
|
METHOD_USAGE_STATS,
|
|
116
119
|
METHOD_WORKSPACE_LIST,
|
|
120
|
+
CAP_RELAY_TIER,
|
|
117
121
|
MINOR,
|
|
118
122
|
PROTO,
|
|
119
123
|
SERVED_CAPABILITIES,
|
|
@@ -183,6 +187,34 @@ export interface BridgeTransport {
|
|
|
183
187
|
frameDeflate?(declared: boolean): void
|
|
184
188
|
}
|
|
185
189
|
|
|
190
|
+
/**
|
|
191
|
+
* 第二会合点在协议面上的那一半:一条公告与一次收帧。
|
|
192
|
+
*
|
|
193
|
+
* 只有配了第二会合点的宿主才提供这个面,而它存在与否就是 `relay.tier.v1` 这个
|
|
194
|
+
* 能力名与 `relay.grant` 这个方法存在与否——没有它,握手与应答与今天逐字相同。
|
|
195
|
+
* 宿主侧的动作(写名册、去第二会合点申领)全在插件半区,core 里只留这两处接线,
|
|
196
|
+
* 所以一个不认识的宿主不会因为这里多了一层而改变任何一帧。
|
|
197
|
+
*/
|
|
198
|
+
export interface RelayTierFace {
|
|
199
|
+
/**
|
|
200
|
+
* 这次握手要在 `hello.r.relay` 里公告的会合点,或 undefined(此刻没有可公告的)。
|
|
201
|
+
*
|
|
202
|
+
* 公告的含义是「这台宿主此刻确实持有一份中继承认的、按 6 小时周期刷新的租约」,
|
|
203
|
+
* 而不是「它曾经申到过一次」:公告出去的是「拨这个地址」,而一个没有有效租约的
|
|
204
|
+
* 地址背后没有人服务。还没申到、降级被拒、grant 过期、主动释放,四种情况都回
|
|
205
|
+
* undefined,于是一条连接的下一个世代自己退回市场边缘,不需要服务端推送任何东西。
|
|
206
|
+
* @returns `wss://<id><suffix>`,或 undefined。
|
|
207
|
+
*/
|
|
208
|
+
origin(): string | undefined
|
|
209
|
+
/**
|
|
210
|
+
* 一条新授权已经落进名册行:第二会合点要立刻重新申领一次。
|
|
211
|
+
*
|
|
212
|
+
* 调用发生在名册写完之后,所以插件半区读到的就是这条授权。它是**触发**而不是
|
|
213
|
+
* 等待:`relay.grant` 的应答不等那一次网络往返,申领失败由半区自己的退避收着。
|
|
214
|
+
*/
|
|
215
|
+
grantLanded(): void
|
|
216
|
+
}
|
|
217
|
+
|
|
186
218
|
export interface BridgeConnectionOptions {
|
|
187
219
|
readonly gateway: BridgeGateway
|
|
188
220
|
readonly pairing: PairingService
|
|
@@ -207,6 +239,14 @@ export interface BridgeConnectionOptions {
|
|
|
207
239
|
readonly usage: UsageService
|
|
208
240
|
/** Host-process permission presets, read and switched through their real service objects. */
|
|
209
241
|
readonly permissions: SessionPermissions
|
|
242
|
+
/**
|
|
243
|
+
* 每个模型自己的思考档偏好表(`core/model-efforts.ts`)。
|
|
244
|
+
*
|
|
245
|
+
* 一个宿主一份、所有连接与所有会话共享:它记的是这个模型在这台机器上被选过
|
|
246
|
+
* 哪一档,与哪条会话无关(契约 §4)。它与宿主自己的 `reasoning.defaultEffort`
|
|
247
|
+
* 是两件事,`session.models` 用另一个键名 `preferredEffort` 公布它。
|
|
248
|
+
*/
|
|
249
|
+
readonly modelEfforts: ModelEffortPreferences
|
|
210
250
|
/**
|
|
211
251
|
* The in-process owner peer every gateway call speaks for, minted by the
|
|
212
252
|
* bridge itself (`core/owner-peer.ts`). Dispatch refuses unpaired
|
|
@@ -262,6 +302,11 @@ export interface BridgeConnectionOptions {
|
|
|
262
302
|
* read the host's own answer.
|
|
263
303
|
*/
|
|
264
304
|
readonly turnEndQuiet?: TurnEndQuietGate
|
|
305
|
+
/**
|
|
306
|
+
* 第二会合点(`relay.tier.v1`)。缺省就是没有第二会合点:`hello` 不带
|
|
307
|
+
* `relay` 键、能力表里没有那个名字、`relay.grant` 仍是 `unknown-method`。
|
|
308
|
+
*/
|
|
309
|
+
readonly relay?: RelayTierFace
|
|
265
310
|
/**
|
|
266
311
|
* Called after a registration changed the device table.
|
|
267
312
|
*
|
|
@@ -272,6 +317,15 @@ export interface BridgeConnectionOptions {
|
|
|
272
317
|
* `push.register` into a failure.
|
|
273
318
|
*/
|
|
274
319
|
readonly onPushRegistered?: () => void
|
|
320
|
+
/**
|
|
321
|
+
* Called after `pair.finish` admits a device into the roster.
|
|
322
|
+
*
|
|
323
|
+
* A machine that started with no paired devices defers claiming its relay
|
|
324
|
+
* lease until a remote device is actually paired (or a pairing code is
|
|
325
|
+
* requested); guarded at the call site so background relay work never fails
|
|
326
|
+
* the pairing response.
|
|
327
|
+
*/
|
|
328
|
+
readonly onDeviceEnrolled?: () => void
|
|
275
329
|
}
|
|
276
330
|
|
|
277
331
|
/** A device the connection proved itself to be, resolved from the upgrade request. */
|
|
@@ -609,6 +663,66 @@ export function modelSelectionOf(value: unknown): { readonly next?: ModelSelecti
|
|
|
609
663
|
}
|
|
610
664
|
}
|
|
611
665
|
|
|
666
|
+
/**
|
|
667
|
+
* The effort ids one catalog row offers, for the model a preference names.
|
|
668
|
+
*
|
|
669
|
+
* Read out of the catalog's own `reasoning.efforts`, which is the only list this
|
|
670
|
+
* bridge may validate against: an effort no row advertises is not a level of
|
|
671
|
+
* that model, and answering `ok` for it would store a value the next
|
|
672
|
+
* `selectModel` has to refuse.
|
|
673
|
+
* @param groups - the catalog's `groups`, exactly as the harness published them.
|
|
674
|
+
* @param provider - provider id, matched against a group's own `id`.
|
|
675
|
+
* @param model - model id, matched against a row's own `id`.
|
|
676
|
+
* @returns the ids the model offers, or undefined when no such model exists —
|
|
677
|
+
* a host that never heard of a pair is a different answer from a model that
|
|
678
|
+
* offers no level at all, and the two refusals say so.
|
|
679
|
+
*/
|
|
680
|
+
function effortIdsOf(groups: unknown, provider: string, model: string): readonly string[] | undefined {
|
|
681
|
+
if (!Array.isArray(groups)) return undefined
|
|
682
|
+
for (const group of groups) {
|
|
683
|
+
if (!isRecord(group) || optionalText(group.id) !== provider || !Array.isArray(group.models)) continue
|
|
684
|
+
for (const row of group.models) {
|
|
685
|
+
if (!isRecord(row) || optionalText(row.id) !== model) continue
|
|
686
|
+
const reasoning = row.reasoning
|
|
687
|
+
if (!isRecord(reasoning) || !Array.isArray(reasoning.efforts)) return []
|
|
688
|
+
return reasoning.efforts
|
|
689
|
+
.map(effort => (isRecord(effort) ? optionalText(effort.id) : undefined))
|
|
690
|
+
.filter((id): id is string => id !== undefined)
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return undefined
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* The catalog's groups with this host's remembered level beside every model.
|
|
698
|
+
*
|
|
699
|
+
* A row this table has nothing for crosses UNCHANGED — no `preferredEffort`, not
|
|
700
|
+
* even a null one: the key means "the host remembers a level for this model", and
|
|
701
|
+
* a client that showed a level for a model nobody ever chose would be showing the
|
|
702
|
+
* catalog's default under the wrong name. Every other key, and the order of the
|
|
703
|
+
* rows, stays exactly as the harness wrote it.
|
|
704
|
+
* @param groups - the catalog's `groups`, exactly as the harness published them.
|
|
705
|
+
* @param efforts - the host's preference table.
|
|
706
|
+
* @returns the rows to publish, with the remembered levels injected.
|
|
707
|
+
*/
|
|
708
|
+
function withPreferredEfforts(groups: unknown, efforts: ModelEffortPreferences): readonly unknown[] {
|
|
709
|
+
if (!Array.isArray(groups)) return []
|
|
710
|
+
return groups.map(group => {
|
|
711
|
+
if (!isRecord(group) || !Array.isArray(group.models)) return group
|
|
712
|
+
const provider = optionalText(group.id)
|
|
713
|
+
if (provider === undefined) return group
|
|
714
|
+
return {
|
|
715
|
+
...group,
|
|
716
|
+
models: group.models.map(row => {
|
|
717
|
+
if (!isRecord(row)) return row
|
|
718
|
+
const model = optionalText(row.id)
|
|
719
|
+
const preferred = model === undefined ? undefined : efforts.effortOf(provider, model)
|
|
720
|
+
return preferred === undefined ? row : { ...row, preferredEffort: preferred }
|
|
721
|
+
}),
|
|
722
|
+
}
|
|
723
|
+
})
|
|
724
|
+
}
|
|
725
|
+
|
|
612
726
|
/** One prompt attachment, already projected to the harness's image content part. */
|
|
613
727
|
type PromptAttachment = Readonly<Record<string, unknown>>
|
|
614
728
|
|
|
@@ -956,6 +1070,11 @@ export class BridgeConnection {
|
|
|
956
1070
|
if (frame.m === METHOD_UI_LIST && !this.declares(CAP_UI_LIST)) {
|
|
957
1071
|
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
958
1072
|
}
|
|
1073
|
+
// 第二会合点只在配了它的宿主上存在:没有这个面时这一帧与它出现之前逐字一样是
|
|
1074
|
+
// `unknown-method`,名册上也不会为它多出一块授权。
|
|
1075
|
+
if (frame.m === METHOD_RELAY_GRANT && this.#options.relay === undefined) {
|
|
1076
|
+
return refusal(CODE_UNKNOWN_METHOD, `unknown method ${frame.m}`)
|
|
1077
|
+
}
|
|
959
1078
|
// Unconditional: a loopback Host buys nothing here. Without a device token
|
|
960
1079
|
// only the handshake and the two pairing steps run.
|
|
961
1080
|
if (!this.paired && !UNPAIRED_METHODS.has(frame.m)) return refusal(CODE_UNPAIRED, 'this connection is not paired')
|
|
@@ -980,11 +1099,13 @@ export class BridgeConnection {
|
|
|
980
1099
|
case METHOD_SESSION_SET_PERMISSION: return this.#sessionSetPermission(frame.p)
|
|
981
1100
|
case METHOD_SESSION_MODELS: return await this.#sessionModels(frame.p)
|
|
982
1101
|
case METHOD_SESSION_SET_MODEL: return await this.#sessionSetModel(frame.p)
|
|
1102
|
+
case METHOD_SESSION_SET_EFFORT: return await this.#sessionSetEffort(frame.p)
|
|
983
1103
|
case METHOD_APPROVAL_RESOLVE: return this.#approvalResolve(frame.p)
|
|
984
1104
|
case METHOD_QUESTION_ANSWER: return await this.#questionAnswer(frame.p)
|
|
985
1105
|
case METHOD_QUESTION_LIST: return this.#questionList(frame.p)
|
|
986
1106
|
case METHOD_UI_EVENT: return this.#uiEvent(frame.p)
|
|
987
1107
|
case METHOD_UI_LIST: return this.#uiList(frame.p)
|
|
1108
|
+
case METHOD_RELAY_GRANT: return await this.#relayGrant(frame.p)
|
|
988
1109
|
case METHOD_PUSH_REGISTER: return await this.#pushRegister(frame.p)
|
|
989
1110
|
case METHOD_USAGE_SESSION: return await this.#usageSession(frame.p)
|
|
990
1111
|
case METHOD_USAGE_BALANCE: return await this.#usageBalance(frame.p)
|
|
@@ -1055,6 +1176,10 @@ export class BridgeConnection {
|
|
|
1055
1176
|
*/
|
|
1056
1177
|
#servedCapabilities(): string[] {
|
|
1057
1178
|
const capabilities = [...SERVED_CAPABILITIES]
|
|
1179
|
+
// 第二会合点是条件能力:配了它才公告,所以它不在 {@link SERVED_CAPABILITIES}
|
|
1180
|
+
// 里(那张表是每条连接逐字相同的开头),而是紧跟其后追加——没配它的宿主因此
|
|
1181
|
+
// 仍然答出那张表本身。
|
|
1182
|
+
if (this.#options.relay !== undefined) capabilities.push(CAP_RELAY_TIER)
|
|
1058
1183
|
for (const capability of this.#options.extensions?.capabilities() ?? []) {
|
|
1059
1184
|
if (!capabilities.includes(capability)) capabilities.push(capability)
|
|
1060
1185
|
}
|
|
@@ -1071,6 +1196,9 @@ export class BridgeConnection {
|
|
|
1071
1196
|
// The hub replays anything this client can now receive but missed: a phone
|
|
1072
1197
|
// that connected after a question was raised has no other way to learn of it.
|
|
1073
1198
|
this.#options.onDeclare?.(this)
|
|
1199
|
+
// 第二会合点的公告。钩子缺省存在与否就是「这台宿主有没有第二个会合点」,因此
|
|
1200
|
+
// 没有它、或它此刻答不出地址时,这个键整个不出现——应答与今天逐字相同。
|
|
1201
|
+
const relayOrigin = this.#options.relay?.origin()
|
|
1074
1202
|
return {
|
|
1075
1203
|
ok: true,
|
|
1076
1204
|
r: {
|
|
@@ -1080,6 +1208,7 @@ export class BridgeConnection {
|
|
|
1080
1208
|
capabilities: this.#servedCapabilities(),
|
|
1081
1209
|
resume: 'snapshot',
|
|
1082
1210
|
auth: this.paired ? 'paired' : 'unpaired',
|
|
1211
|
+
...(relayOrigin === undefined ? {} : { relay: { origin: relayOrigin } }),
|
|
1083
1212
|
},
|
|
1084
1213
|
}
|
|
1085
1214
|
}
|
|
@@ -1120,6 +1249,11 @@ export class BridgeConnection {
|
|
|
1120
1249
|
const enrollment = await this.#options.roster.enroll(devicePubKey)
|
|
1121
1250
|
if (enrollment === undefined) return refusal(CODE_INVALID_ARGS, 'devicePubKey is not a P-256 SPKI public key')
|
|
1122
1251
|
this.#deviceId = enrollment.deviceId
|
|
1252
|
+
try {
|
|
1253
|
+
this.#options.onDeviceEnrolled?.()
|
|
1254
|
+
} catch {
|
|
1255
|
+
// Relay lease activation is background work and must never fail pairing.
|
|
1256
|
+
}
|
|
1123
1257
|
return { ok: true, r: { deviceToken: enrollment.deviceToken, deviceId: enrollment.deviceId } }
|
|
1124
1258
|
}
|
|
1125
1259
|
|
|
@@ -1726,7 +1860,10 @@ export class BridgeConnection {
|
|
|
1726
1860
|
* `current` is the session's FOLD — next, else last used, else the catalog
|
|
1727
1861
|
* default — and never the default on its own: a phone that showed the default
|
|
1728
1862
|
* while another model was in force would name the wrong model for the next
|
|
1729
|
-
* turn. The catalog's
|
|
1863
|
+
* turn. The catalog's failures and default cross unchanged, and every row
|
|
1864
|
+
* carries this host's remembered level as `preferredEffort` when it has one:
|
|
1865
|
+
* that key is the table's, deliberately not the row's own
|
|
1866
|
+
* `reasoning.defaultEffort`, which stays the catalog's property.
|
|
1730
1867
|
* @param payload - `{sessionId}`.
|
|
1731
1868
|
*/
|
|
1732
1869
|
async #sessionModels(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
@@ -1735,28 +1872,55 @@ export class BridgeConnection {
|
|
|
1735
1872
|
const catalog = await this.#unary('session', 'modelCatalog')
|
|
1736
1873
|
if (!catalog.ok) return catalog
|
|
1737
1874
|
if (!isRecord(catalog.value)) return refusal(CODE_INTERNAL, 'session/modelCatalog returned no catalog')
|
|
1738
|
-
const
|
|
1739
|
-
if (
|
|
1740
|
-
|
|
1741
|
-
if (!projections.ok) return projections
|
|
1742
|
-
const folded = modelSelectionOf(projections.value)
|
|
1875
|
+
const folded = await this.#sessionCurrent(sessionId, catalog.value)
|
|
1876
|
+
if (!folded.ok) return folded
|
|
1877
|
+
await this.#options.modelEfforts.load()
|
|
1743
1878
|
return {
|
|
1744
1879
|
ok: true,
|
|
1745
1880
|
r: {
|
|
1746
|
-
current: folded.
|
|
1747
|
-
default: fallback,
|
|
1748
|
-
groups: catalog.value.groups
|
|
1881
|
+
current: folded.current,
|
|
1882
|
+
default: folded.fallback,
|
|
1883
|
+
groups: withPreferredEfforts(catalog.value.groups, this.#options.modelEfforts),
|
|
1749
1884
|
failures: catalog.value.failures ?? [],
|
|
1750
1885
|
},
|
|
1751
1886
|
}
|
|
1752
1887
|
}
|
|
1753
1888
|
|
|
1889
|
+
/**
|
|
1890
|
+
* The selection one session folds to: the pending next choice, else the last
|
|
1891
|
+
* one used, else the catalog default.
|
|
1892
|
+
*
|
|
1893
|
+
* One fold shared by `session.models`, `session.setModel` and
|
|
1894
|
+
* `session.setEffort`, so no two of them can name a different model for the
|
|
1895
|
+
* same session.
|
|
1896
|
+
* @param sessionId - the session whose projection is read.
|
|
1897
|
+
* @param catalog - the catalog the fold falls back into.
|
|
1898
|
+
*/
|
|
1899
|
+
async #sessionCurrent(
|
|
1900
|
+
sessionId: string,
|
|
1901
|
+
catalog: Readonly<Record<string, unknown>>,
|
|
1902
|
+
): Promise<{ readonly ok: true; readonly current: ModelSelection; readonly fallback: ModelSelection } | CallFailure> {
|
|
1903
|
+
const fallback = selectionOf(catalog.default)
|
|
1904
|
+
if (fallback === undefined) return refusal(CODE_INTERNAL, 'session/modelCatalog returned no default model')
|
|
1905
|
+
const projections = await this.#unary('session', 'projections', { sessionId })
|
|
1906
|
+
if (!projections.ok) return projections
|
|
1907
|
+
const folded = modelSelectionOf(projections.value)
|
|
1908
|
+
return { ok: true, current: folded.next ?? folded.lastUsed ?? fallback, fallback }
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1754
1911
|
/**
|
|
1755
1912
|
* Answer `session.setModel`: register the next turn's selection.
|
|
1756
1913
|
*
|
|
1757
1914
|
* The harness only records the choice for the NEXT request, so the answer
|
|
1758
1915
|
* echoes the `selected` selection it accepted and nothing here claims the
|
|
1759
1916
|
* running turn changed.
|
|
1917
|
+
*
|
|
1918
|
+
* A request with no effort of its own is answered with the level this host
|
|
1919
|
+
* remembers for that model: a phone that picked "high" for a model once means
|
|
1920
|
+
* it on the next switch too, and sending nothing would silently reset the
|
|
1921
|
+
* model to the harness's own default. A model this table has nothing for is
|
|
1922
|
+
* sent exactly as before — no `reasoningEffort` key at all. The remembered
|
|
1923
|
+
* level is proven against the catalog first; see {@link #rememberedEffort}.
|
|
1760
1924
|
* @param payload - `{sessionId, provider, model, effort?}`.
|
|
1761
1925
|
*/
|
|
1762
1926
|
async #sessionSetModel(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
@@ -1766,7 +1930,9 @@ export class BridgeConnection {
|
|
|
1766
1930
|
if (sessionId === undefined || provider === undefined || model === undefined) {
|
|
1767
1931
|
return refusal(CODE_INVALID_ARGS, 'session.setModel requires sessionId, provider and model')
|
|
1768
1932
|
}
|
|
1769
|
-
|
|
1933
|
+
await this.#options.modelEfforts.load()
|
|
1934
|
+
const named = optionalText(payload.effort)
|
|
1935
|
+
const effort = named ?? await this.#rememberedEffort(provider, model)
|
|
1770
1936
|
const call = await this.#unary('session', 'selectModel', {
|
|
1771
1937
|
sessionId,
|
|
1772
1938
|
provider,
|
|
@@ -1779,6 +1945,90 @@ export class BridgeConnection {
|
|
|
1779
1945
|
return { ok: true, r: { ok: true, current: selected } }
|
|
1780
1946
|
}
|
|
1781
1947
|
|
|
1948
|
+
/**
|
|
1949
|
+
* The level this host remembers for one model, proven against the catalog.
|
|
1950
|
+
*
|
|
1951
|
+
* A remembered level is a fact about the past: the harness can change a model's
|
|
1952
|
+
* offered ladder under it, and sending a level the model no longer publishes
|
|
1953
|
+
* makes `selectModel` refuse — which leaves that model unswitchable, with the
|
|
1954
|
+
* slider (which only writes the session's current model) unable to heal it. So
|
|
1955
|
+
* the backfill asks the catalog first and withholds a level the model does not
|
|
1956
|
+
* offer, dropping the stale row on the way: one catalog read for the stale
|
|
1957
|
+
* switch, then the row is gone and the next switch costs nothing.
|
|
1958
|
+
* @param provider - provider id.
|
|
1959
|
+
* @param model - model id.
|
|
1960
|
+
* @returns the level to send, or undefined when there is none this host can prove.
|
|
1961
|
+
*/
|
|
1962
|
+
async #rememberedEffort(provider: string, model: string): Promise<string | undefined> {
|
|
1963
|
+
const remembered = this.#options.modelEfforts.effortOf(provider, model)
|
|
1964
|
+
if (remembered === undefined) return undefined
|
|
1965
|
+
const catalog = await this.#unary('session', 'modelCatalog')
|
|
1966
|
+
// A catalog this call cannot read is not evidence the level went stale: the
|
|
1967
|
+
// stored value crosses exactly as it did before this check existed.
|
|
1968
|
+
if (!catalog.ok || !isRecord(catalog.value)) return remembered
|
|
1969
|
+
const offered = effortIdsOf(catalog.value.groups, provider, model)
|
|
1970
|
+
// A model this answer leaves out is NOT evidence its level went stale: a
|
|
1971
|
+
// provider that failed to enumerate this time is reported under the catalog's
|
|
1972
|
+
// own `failures`, and one transient absence must never erase a level the user
|
|
1973
|
+
// set. The level is withheld for this switch and the row is kept. Only a model
|
|
1974
|
+
// that IS published, with a ladder that does not contain this level, is a
|
|
1975
|
+
// level no selection can ever accept.
|
|
1976
|
+
if (offered === undefined) return undefined
|
|
1977
|
+
if (offered.includes(remembered)) return remembered
|
|
1978
|
+
await this.#options.modelEfforts.forget(provider, model, remembered, `the catalog no longer offers it: ${offered.join(', ') || 'no levels'}`)
|
|
1979
|
+
.catch((error: unknown) => {
|
|
1980
|
+
this.#options.log(`could not drop the stale remembered effort for ${provider}/${model}: ${failureText(error)}`)
|
|
1981
|
+
})
|
|
1982
|
+
return undefined
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
/**
|
|
1986
|
+
* Answer `session.setEffort`: remember one model's reasoning level.
|
|
1987
|
+
*
|
|
1988
|
+
* The table is the host's — keyed by provider and model, shared by every
|
|
1989
|
+
* session — so this writes a PREFERENCE and switches no model. The harness is
|
|
1990
|
+
* asked for a selection only when the pair is already this session's folded
|
|
1991
|
+
* choice, where the new level has to reach the next turn; for any other model
|
|
1992
|
+
* `session/selectModel` is never called and the answer echoes the fold
|
|
1993
|
+
* unchanged. A model the catalog does not publish, and a level that model does
|
|
1994
|
+
* not offer, are refused as `invalid-args` before anything is written.
|
|
1995
|
+
* @param payload - `{sessionId, provider, model, effort}`.
|
|
1996
|
+
*/
|
|
1997
|
+
async #sessionSetEffort(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
1998
|
+
const sessionId = stringArg(payload, 'sessionId')
|
|
1999
|
+
const provider = stringArg(payload, 'provider')
|
|
2000
|
+
const model = stringArg(payload, 'model')
|
|
2001
|
+
const effort = stringArg(payload, 'effort')
|
|
2002
|
+
if (sessionId === undefined || provider === undefined || model === undefined || effort === undefined) {
|
|
2003
|
+
return refusal(CODE_INVALID_ARGS, 'session.setEffort requires sessionId, provider, model and effort')
|
|
2004
|
+
}
|
|
2005
|
+
// Read before any write: the table is rewritten whole, so a save must never
|
|
2006
|
+
// overwrite rows this process has not read yet.
|
|
2007
|
+
await this.#options.modelEfforts.load()
|
|
2008
|
+
const catalog = await this.#unary('session', 'modelCatalog')
|
|
2009
|
+
if (!catalog.ok) return catalog
|
|
2010
|
+
if (!isRecord(catalog.value)) return refusal(CODE_INTERNAL, 'session/modelCatalog returned no catalog')
|
|
2011
|
+
const offered = effortIdsOf(catalog.value.groups, provider, model)
|
|
2012
|
+
if (offered === undefined) {
|
|
2013
|
+
return refusal(CODE_INVALID_ARGS, `session.setEffort: this host publishes no model ${provider}/${model}`)
|
|
2014
|
+
}
|
|
2015
|
+
if (!offered.includes(effort)) {
|
|
2016
|
+
return refusal(CODE_INVALID_ARGS, `session.setEffort: ${effort} is not a reasoning effort of ${provider}/${model}`)
|
|
2017
|
+
}
|
|
2018
|
+
const folded = await this.#sessionCurrent(sessionId, catalog.value)
|
|
2019
|
+
if (!folded.ok) return folded
|
|
2020
|
+
await this.#options.modelEfforts.save(provider, model, effort)
|
|
2021
|
+
const saved = { provider, model, effort }
|
|
2022
|
+
if (folded.current.provider !== provider || folded.current.model !== model) {
|
|
2023
|
+
return { ok: true, r: { ok: true, saved, current: folded.current } }
|
|
2024
|
+
}
|
|
2025
|
+
const call = await this.#unary('session', 'selectModel', { sessionId, provider, model, reasoningEffort: effort })
|
|
2026
|
+
if (!call.ok) return call
|
|
2027
|
+
const selected = isRecord(call.value) ? selectionOf(call.value.selected) : undefined
|
|
2028
|
+
if (selected === undefined) return refusal(CODE_INTERNAL, 'session/selectModel returned no selected model')
|
|
2029
|
+
return { ok: true, r: { ok: true, saved, current: selected } }
|
|
2030
|
+
}
|
|
2031
|
+
|
|
1782
2032
|
/**
|
|
1783
2033
|
* Answer `presets.list`: the host's Agent-preset roster, forwarded verbatim.
|
|
1784
2034
|
*
|
|
@@ -2242,7 +2492,7 @@ export class BridgeConnection {
|
|
|
2242
2492
|
* accept the token, and a field claiming it did would be a lie a client might
|
|
2243
2493
|
* render. A bridge with no signing key answers identically — a client has no
|
|
2244
2494
|
* second code path for that difference.
|
|
2245
|
-
* @param payload - `{deviceToken, environment, bundle, liveActivityTokens?, locale?}`.
|
|
2495
|
+
* @param payload - `{deviceToken, environment, bundle, liveActivityTokens?, locale?, relayToken?}`.
|
|
2246
2496
|
*/
|
|
2247
2497
|
async #pushRegister(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
2248
2498
|
const deviceToken = payload.deviceToken
|
|
@@ -2256,6 +2506,13 @@ export class BridgeConnection {
|
|
|
2256
2506
|
}
|
|
2257
2507
|
if (typeof bundle !== 'string' || bundle === '') return refusal(CODE_INVALID_ARGS, 'bundle must be a non-empty string')
|
|
2258
2508
|
const locale = optionalText(payload.locale)
|
|
2509
|
+
// The relay credential (§28 over the hosted service). Optional and opaque: the
|
|
2510
|
+
// bridge never mints, parses or reissues it — it is the App's, scoped to this
|
|
2511
|
+
// pairing's fingerprint, and this row is the only place it is kept.
|
|
2512
|
+
const relayToken = payload.relayToken
|
|
2513
|
+
if (relayToken !== undefined && (typeof relayToken !== 'string' || relayToken === '')) {
|
|
2514
|
+
return refusal(CODE_INVALID_ARGS, 'relayToken must be the non-empty string the account service issued')
|
|
2515
|
+
}
|
|
2259
2516
|
let liveActivityTokens: readonly PushLiveActivityToken[] | undefined
|
|
2260
2517
|
if (payload.liveActivityTokens !== undefined) {
|
|
2261
2518
|
const raw = payload.liveActivityTokens
|
|
@@ -2288,6 +2545,7 @@ export class BridgeConnection {
|
|
|
2288
2545
|
bundle,
|
|
2289
2546
|
...(locale === undefined ? {} : { locale }),
|
|
2290
2547
|
...(liveActivityTokens === undefined ? {} : { liveActivityTokens }),
|
|
2548
|
+
...(relayToken === undefined ? {} : { relayToken }),
|
|
2291
2549
|
registeredAt: this.#options.now(),
|
|
2292
2550
|
}
|
|
2293
2551
|
try {
|
|
@@ -2310,6 +2568,53 @@ export class BridgeConnection {
|
|
|
2310
2568
|
return { ok: true, r: { ok: true } }
|
|
2311
2569
|
}
|
|
2312
2570
|
|
|
2571
|
+
/**
|
|
2572
|
+
* The body of `relay.grant`: one device hands its relay tier grant over.
|
|
2573
|
+
*
|
|
2574
|
+
* A write, and the only one this channel has. The grant lands in the sending
|
|
2575
|
+
* device's own roster row (schema 4), beside the identity it already proved, so
|
|
2576
|
+
* revoking the device withdraws the admission with it and no second table exists.
|
|
2577
|
+
* The host then asks the second meeting point to refresh its lease, and that ask
|
|
2578
|
+
* is fired rather than awaited: a phone hands a fresh grant over precisely when a
|
|
2579
|
+
* claim was just refused, and this frame must not hang on a meeting point that
|
|
2580
|
+
* stopped answering. The roster row is what the next claim reads, so the ask
|
|
2581
|
+
* never depends on this answer reaching the client first.
|
|
2582
|
+
*
|
|
2583
|
+
* The answer is `{ok:true}` and nothing else. There is deliberately no echo, no
|
|
2584
|
+
* tier and no "admitted" flag: the tier is decided out of band by the meeting
|
|
2585
|
+
* point, this bridge never learns it, and a field claiming one would be a lie the
|
|
2586
|
+
* client might render.
|
|
2587
|
+
* @param payload - `{grant}`.
|
|
2588
|
+
*/
|
|
2589
|
+
async #relayGrant(payload: Readonly<Record<string, unknown>>): Promise<DispatchOutcome> {
|
|
2590
|
+
const relay = this.#options.relay
|
|
2591
|
+
// Unreachable through the dispatcher (the gate above refuses first), and kept
|
|
2592
|
+
// because this handler reads its own precondition rather than trusting it.
|
|
2593
|
+
if (relay === undefined) return refusal(CODE_UNKNOWN_METHOD, `unknown method ${METHOD_RELAY_GRANT}`)
|
|
2594
|
+
const grant = payload.grant
|
|
2595
|
+
if (typeof grant !== 'string' || grant === '') {
|
|
2596
|
+
return refusal(CODE_INVALID_ARGS, 'grant must be the string the account service issued')
|
|
2597
|
+
}
|
|
2598
|
+
const deviceId = this.#deviceId
|
|
2599
|
+
if (deviceId === undefined) return refusal(CODE_UNPAIRED, 'this connection is not paired')
|
|
2600
|
+
try {
|
|
2601
|
+
const stored = await this.#options.roster.setRelayGrant(deviceId, grant)
|
|
2602
|
+
if (!stored) return refusal(CODE_INTERNAL, 'this connection names no paired device')
|
|
2603
|
+
try {
|
|
2604
|
+
relay.grantLanded()
|
|
2605
|
+
} catch (error) {
|
|
2606
|
+
// The re-claim is this bridge's own background work and may never turn a
|
|
2607
|
+
// grant the roster already holds into a failure of this frame.
|
|
2608
|
+
this.#options.log(`the second meeting point could not re-claim: ${failureText(error)}`)
|
|
2609
|
+
}
|
|
2610
|
+
} catch (error) {
|
|
2611
|
+
// A grant that never reached the disk is an admission the host does not
|
|
2612
|
+
// have, so the client has to be told instead of assuming it landed.
|
|
2613
|
+
return refusal(CODE_INTERNAL, `could not write the device roster: ${failureText(error)}`)
|
|
2614
|
+
}
|
|
2615
|
+
return { ok: true, r: { ok: true } }
|
|
2616
|
+
}
|
|
2617
|
+
|
|
2313
2618
|
/**
|
|
2314
2619
|
* 答一次 `usage.session`:一条会话的上下文窗口与下一次请求的组成估算。
|
|
2315
2620
|
*
|