toc-ocra 0.3.24 → 0.4.0
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.md +43 -26
- package/dist/cli/cli.mjs +6121 -5939
- package/dist/service/agent.mjs +6644 -1816
- package/dist/src/config.d.ts +18 -22
- package/dist/src/config.js +66 -49
- package/dist/src/config.js.map +1 -1
- package/dist/src/hub/dpop.d.ts +61 -0
- package/dist/src/hub/dpop.js +131 -0
- package/dist/src/hub/dpop.js.map +1 -0
- package/dist/src/hub/event-store.d.ts +33 -0
- package/dist/src/hub/event-store.js +109 -0
- package/dist/src/hub/event-store.js.map +1 -0
- package/dist/src/hub/fs-list.d.ts +11 -0
- package/dist/src/hub/fs-list.js +40 -0
- package/dist/src/hub/fs-list.js.map +1 -0
- package/dist/src/hub/hub-link.d.ts +138 -0
- package/dist/src/hub/hub-link.js +616 -0
- package/dist/src/hub/hub-link.js.map +1 -0
- package/dist/src/hub/link-guard.d.ts +22 -0
- package/dist/src/hub/link-guard.js +37 -0
- package/dist/src/hub/link-guard.js.map +1 -0
- package/dist/src/hub/protocol.d.ts +59 -0
- package/dist/src/hub/protocol.js +40 -0
- package/dist/src/hub/protocol.js.map +1 -0
- package/dist/src/hub/proxy.d.ts +23 -0
- package/dist/src/hub/proxy.js +292 -0
- package/dist/src/hub/proxy.js.map +1 -0
- package/dist/src/index.js +84 -28
- package/dist/src/index.js.map +1 -1
- package/dist/src/providers/tb-switch-control.d.ts +1 -1
- package/dist/src/runs/run-supervisor.d.ts +6 -0
- package/dist/src/runs/run-supervisor.js +2 -0
- package/dist/src/runs/run-supervisor.js.map +1 -1
- package/dist/src/runtime/agent-service.d.ts +3 -2
- package/dist/src/runtime/agent-service.js +20 -17
- package/dist/src/runtime/agent-service.js.map +1 -1
- package/dist/src/runtime/hub-runtime.d.ts +37 -0
- package/dist/src/runtime/hub-runtime.js +109 -0
- package/dist/src/runtime/hub-runtime.js.map +1 -0
- package/dist/src/t3/provider-catalog.d.ts +75 -0
- package/dist/src/t3/provider-catalog.js +145 -0
- package/dist/src/t3/provider-catalog.js.map +1 -0
- package/dist/src/t3/t3-backend.d.ts +86 -0
- package/dist/src/t3/t3-backend.js +604 -0
- package/dist/src/t3/t3-backend.js.map +1 -0
- package/dist/src/t3/thread-store.d.ts +81 -0
- package/dist/src/t3/thread-store.js +162 -0
- package/dist/src/t3/thread-store.js.map +1 -0
- package/dist/src/t3/turn-items.d.ts +39 -0
- package/dist/src/t3/turn-items.js +180 -0
- package/dist/src/t3/turn-items.js.map +1 -0
- package/dist/src/types/protocol.d.ts +1 -1
- package/package.json +4 -4
- package/scripts/service.mjs +3 -3
- package/dist/src/control/device-link.d.ts +0 -13
- package/dist/src/control/device-link.js +0 -113
- package/dist/src/control/device-link.js.map +0 -1
- package/dist/src/control/terminal-qr.d.ts +0 -1
- package/dist/src/control/terminal-qr.js +0 -27
- package/dist/src/control/terminal-qr.js.map +0 -1
- package/dist/src/runtime/agent-runtime.d.ts +0 -81
- package/dist/src/runtime/agent-runtime.js +0 -523
- package/dist/src/runtime/agent-runtime.js.map +0 -1
- package/dist/src/transport/gateway-client.d.ts +0 -53
- package/dist/src/transport/gateway-client.js +0 -138
- package/dist/src/transport/gateway-client.js.map +0 -1
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { clearEnrollment, enrollDevice, loadBaseConfig, normalizeServerUrl } from '../config.js';
|
|
1
|
+
import { clearEnrollment, enrollDevice, loadBaseConfig, normalizeServerUrl, readHubCredentials } from '../config.js';
|
|
2
2
|
import { PairingStore } from '../control/pairing.js';
|
|
3
3
|
import { ControlError, HealthServer } from '../health/server.js';
|
|
4
4
|
import { createDiagnosticLogger } from '../logging/diagnostic-log.js';
|
|
5
5
|
import { cliDetection } from '../providers/detection.js';
|
|
6
|
-
import {
|
|
6
|
+
import { unpairFromHub } from '../hub/dpop.js';
|
|
7
|
+
import { HubRuntime } from './hub-runtime.js';
|
|
7
8
|
import { INSTANCE_LOCK_PORT } from './instance-lock.js';
|
|
8
9
|
/** Bumped when the control API changes incompatibly. */
|
|
9
10
|
export const CONTROL_PROTOCOL_VERSION = 1;
|
|
@@ -12,8 +13,9 @@ export class LocalServerUnavailableError extends Error {
|
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
14
15
|
* The long-running `toc-ocra run` process. It owns the local server (the
|
|
15
|
-
* dashboard plus the plugin control API) and runs the
|
|
16
|
-
*
|
|
16
|
+
* dashboard plus the plugin control API) and runs the Hub runtime (the
|
|
17
|
+
* `/api/t3/device` link and the CLI execution backend) while this machine is
|
|
18
|
+
* paired. Pairing and unpairing from the plugin start and
|
|
17
19
|
* stop the runtime without restarting the process.
|
|
18
20
|
*/
|
|
19
21
|
export class AgentService {
|
|
@@ -36,7 +38,7 @@ export class AgentService {
|
|
|
36
38
|
this.config = loadBaseConfig();
|
|
37
39
|
this.logger = createDiagnosticLogger(this.config.logFile, { level: this.config.logLevel, maxAgeMs: 5 * 60 * 1_000 });
|
|
38
40
|
this.pairing = new PairingStore(this.config.dataDir, Date.now, options.hostToken ?? '');
|
|
39
|
-
this.server = new HealthServer(this.config.healthHost, this.config.healthPort, () => this.runtime?.snapshot() ?? this.idleSnapshot(), this.config.logFile, this.logger, async (refresh) => (this.runtime ? (refresh ? this.runtime.refreshDetection() : this.runtime.snapshot().detection ?? this.detect(false)) : this.detect(refresh)), this.controlApi());
|
|
41
|
+
this.server = new HealthServer(this.config.healthHost, this.config.healthPort, () => this.runtime?.snapshot() ?? this.idleSnapshot(), this.config.logFile, this.logger, async (refresh) => (this.runtime ? (refresh ? (await this.runtime.refreshDetection()) ?? this.detect(true) : this.runtime.snapshot().detection ?? this.detect(false)) : this.detect(refresh)), this.controlApi());
|
|
40
42
|
}
|
|
41
43
|
async start() {
|
|
42
44
|
try {
|
|
@@ -70,7 +72,7 @@ export class AgentService {
|
|
|
70
72
|
}
|
|
71
73
|
}
|
|
72
74
|
else
|
|
73
|
-
process.stdout.write(
|
|
75
|
+
process.stdout.write(`本机尚未配对:在 Echo 设备页生成配对码后运行 toc-ocra pair <配对码>,或在「设备代理」插件中完成绑定\n当前配置:${this.config.configPath}\n`);
|
|
74
76
|
}
|
|
75
77
|
stop() {
|
|
76
78
|
return this.stopPromise ??= (async () => {
|
|
@@ -92,12 +94,13 @@ export class AgentService {
|
|
|
92
94
|
})();
|
|
93
95
|
}
|
|
94
96
|
async startRuntime() {
|
|
95
|
-
const
|
|
97
|
+
const credentials = readHubCredentials(this.config.configPath);
|
|
98
|
+
if (!credentials)
|
|
99
|
+
return;
|
|
100
|
+
const runtime = new HubRuntime(this.config, this.version, credentials, this.logger);
|
|
96
101
|
this.runtime = runtime;
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
// reaping runs, instead of repeatedly restarting the whole service.
|
|
100
|
-
await runtime.start({ waitForLease: true });
|
|
102
|
+
// The link reconnects by itself; an unreachable Hub never fails the start.
|
|
103
|
+
await runtime.start();
|
|
101
104
|
}
|
|
102
105
|
async stopRuntime() {
|
|
103
106
|
const runtime = this.runtime;
|
|
@@ -141,7 +144,7 @@ export class AgentService {
|
|
|
141
144
|
connection_error: snapshot?.connectionError?.reason ?? '',
|
|
142
145
|
active_runs: snapshot?.activeRunIds.length ?? 0,
|
|
143
146
|
allowed_roots: this.config.allowedRoots,
|
|
144
|
-
token_from_env:
|
|
147
|
+
token_from_env: false,
|
|
145
148
|
providers: detection.providers.map((provider) => ({
|
|
146
149
|
id: provider.id,
|
|
147
150
|
installed: provider.installed,
|
|
@@ -170,9 +173,7 @@ export class AgentService {
|
|
|
170
173
|
catch {
|
|
171
174
|
throw new ControlError(400, `服务器地址无效:${rawUrl || '(空)'}`, 'invalid_server_url');
|
|
172
175
|
}
|
|
173
|
-
if
|
|
174
|
-
throw new ControlError(409, '设备令牌来自环境变量 TOC_OCRA_TOKEN,请先移除该变量再绑定', 'token_from_env');
|
|
175
|
-
// Enroll first: if the code is wrong the current binding keeps running.
|
|
176
|
+
// Pair first: if the code is wrong the current pairing keeps running.
|
|
176
177
|
let enrolled;
|
|
177
178
|
try {
|
|
178
179
|
enrolled = await enrollDevice({ code, serverUrl, configPath: this.config.configPath, agentVersion: this.version });
|
|
@@ -187,9 +188,11 @@ export class AgentService {
|
|
|
187
188
|
return this.status();
|
|
188
189
|
}),
|
|
189
190
|
unpair: () => this.serialize(async () => {
|
|
190
|
-
if (process.env.TOC_OCRA_TOKEN)
|
|
191
|
-
throw new ControlError(409, '设备令牌来自环境变量 TOC_OCRA_TOKEN,请先移除该变量', 'token_from_env');
|
|
192
191
|
await this.stopRuntime();
|
|
192
|
+
// Best effort: the Hub also forgets the device; a failure leaves it to be removed on the web.
|
|
193
|
+
const credentials = readHubCredentials(this.config.configPath);
|
|
194
|
+
if (credentials)
|
|
195
|
+
await unpairFromHub(credentials).catch(() => undefined);
|
|
193
196
|
await clearEnrollment(this.config.configPath);
|
|
194
197
|
this.config = loadBaseConfig();
|
|
195
198
|
this.logger.info('device-unpaired');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-service.js","sourceRoot":"","sources":["../../../src/runtime/agent-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,kBAAkB,EAAoB,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"agent-service.js","sourceRoot":"","sources":["../../../src/runtime/agent-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,kBAAkB,EAAE,kBAAkB,EAAoB,MAAM,cAAc,CAAA;AACtI,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAmB,MAAM,qBAAqB,CAAA;AACjF,OAAO,EAAE,sBAAsB,EAAyB,MAAM,8BAA8B,CAAA;AAC5F,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAExD,OAAO,EAAE,aAAa,EAAE,MAAM,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IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QACjD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QAC7C,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,sDAAsD;AACtD,MAAM,UAAU,cAAc,CAAC,QAAyB;IACtD,IAAI,QAAQ,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IACvC,IAAI,QAAQ,KAAK,OAAO;QAAE,OAAO,KAAK,CAAA;IACtC,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC/D,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type AgentConfig } from '../config.js';
|
|
2
|
+
import type { HealthSnapshot } from '../health/server.js';
|
|
3
|
+
import type { HubCredentials } from '../hub/dpop.js';
|
|
4
|
+
import { type DiagnosticLogger } from '../logging/diagnostic-log.js';
|
|
5
|
+
import type { CliDetectionSnapshot } from '../shared/cli-detection-types.js';
|
|
6
|
+
/** `appVersion` of the hello frame and the token requests. */
|
|
7
|
+
export declare function hubAppVersion(version: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* The paired device at work: the Hub link (`src/hub`) plus the execution
|
|
10
|
+
* backend that runs the CLIs (`src/t3`). It replaces the former `/api/agent`
|
|
11
|
+
* runtime (HTTP heartbeat + long poll); the Hub's `/api/t3/device` protocol
|
|
12
|
+
* is the only channel to the server.
|
|
13
|
+
*/
|
|
14
|
+
export declare class HubRuntime {
|
|
15
|
+
private readonly config;
|
|
16
|
+
private readonly version;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
private readonly ownsLogger;
|
|
19
|
+
private readonly backend;
|
|
20
|
+
private readonly link;
|
|
21
|
+
private readonly startedAt;
|
|
22
|
+
private catalogTimer;
|
|
23
|
+
private catalogSignature;
|
|
24
|
+
private stopPromise;
|
|
25
|
+
constructor(config: AgentConfig, version: string, credentials: HubCredentials, logger?: DiagnosticLogger);
|
|
26
|
+
start(): Promise<void>;
|
|
27
|
+
stop(): Promise<void>;
|
|
28
|
+
/** Probes CLIs and models; the catalog is sent only when it differs from the last one sent. */
|
|
29
|
+
private publishCatalog;
|
|
30
|
+
/** Re-detects installed CLIs (the plugin's "重新检测") and reports the result. */
|
|
31
|
+
refreshDetection(): Promise<CliDetectionSnapshot | undefined>;
|
|
32
|
+
/** Current state, for the local dashboard and the plugin. */
|
|
33
|
+
snapshot(): HealthSnapshot & {
|
|
34
|
+
unauthorized: boolean;
|
|
35
|
+
activeRunIds: string[];
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { saveHubCredentials, readHubCredentials } from '../config.js';
|
|
3
|
+
import { HubLink } from '../hub/hub-link.js';
|
|
4
|
+
import { createDiagnosticLogger } from '../logging/diagnostic-log.js';
|
|
5
|
+
import { T3Backend } from '../t3/t3-backend.js';
|
|
6
|
+
/** How often the CLIs and their models are re-probed while the service runs. */
|
|
7
|
+
const CATALOG_REFRESH_MS = 10 * 60_000;
|
|
8
|
+
/** `appVersion` of the hello frame and the token requests. */
|
|
9
|
+
export function hubAppVersion(version) {
|
|
10
|
+
return `ocra-${version}`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The paired device at work: the Hub link (`src/hub`) plus the execution
|
|
14
|
+
* backend that runs the CLIs (`src/t3`). It replaces the former `/api/agent`
|
|
15
|
+
* runtime (HTTP heartbeat + long poll); the Hub's `/api/t3/device` protocol
|
|
16
|
+
* is the only channel to the server.
|
|
17
|
+
*/
|
|
18
|
+
export class HubRuntime {
|
|
19
|
+
config;
|
|
20
|
+
version;
|
|
21
|
+
logger;
|
|
22
|
+
ownsLogger;
|
|
23
|
+
backend;
|
|
24
|
+
link;
|
|
25
|
+
startedAt = new Date().toISOString();
|
|
26
|
+
catalogTimer;
|
|
27
|
+
catalogSignature = '';
|
|
28
|
+
stopPromise;
|
|
29
|
+
constructor(config, version, credentials, logger) {
|
|
30
|
+
this.config = config;
|
|
31
|
+
this.version = version;
|
|
32
|
+
this.ownsLogger = !logger;
|
|
33
|
+
this.logger = logger ?? createDiagnosticLogger(config.logFile, { level: config.logLevel, maxAgeMs: 5 * 60 * 1_000 });
|
|
34
|
+
this.backend = new T3Backend(config, this.logger);
|
|
35
|
+
this.link = new HubLink({
|
|
36
|
+
credentials,
|
|
37
|
+
store: {
|
|
38
|
+
load: async () => readHubCredentials(config.configPath),
|
|
39
|
+
save: (next) => saveHubCredentials(config.configPath, next)
|
|
40
|
+
},
|
|
41
|
+
backend: this.backend,
|
|
42
|
+
appVersion: hubAppVersion(version),
|
|
43
|
+
eventStatePath: join(config.dataDir, 'hub-events.json'),
|
|
44
|
+
logger: this.logger
|
|
45
|
+
});
|
|
46
|
+
this.backend.attach({ event: (envelope) => this.link.publishEvent(envelope) });
|
|
47
|
+
}
|
|
48
|
+
async start() {
|
|
49
|
+
// Before the link opens: nothing of a previous process is left running or open.
|
|
50
|
+
await this.backend.recover();
|
|
51
|
+
await this.publishCatalog(false).catch((error) => this.logger.warn('catalog-probe-failed', { error: errorMessage(error) }));
|
|
52
|
+
this.link.start();
|
|
53
|
+
this.catalogTimer = setInterval(() => void this.publishCatalog(false).catch(() => undefined), CATALOG_REFRESH_MS);
|
|
54
|
+
this.catalogTimer.unref?.();
|
|
55
|
+
this.logger.info('agent-started', { serverUrl: this.config.serverUrl, deviceId: this.config.deviceId });
|
|
56
|
+
process.stdout.write(`toc-ocra ${this.version} 已启动:${this.config.serverUrl}(设备 ${this.config.deviceName})\n`);
|
|
57
|
+
}
|
|
58
|
+
stop() {
|
|
59
|
+
return this.stopPromise ??= (async () => {
|
|
60
|
+
if (this.catalogTimer)
|
|
61
|
+
clearInterval(this.catalogTimer);
|
|
62
|
+
// Runs end (and report their final events) before the link saves what the Hub has not acknowledged.
|
|
63
|
+
await this.backend.stop();
|
|
64
|
+
this.link.stop();
|
|
65
|
+
this.logger.info('agent-stopped');
|
|
66
|
+
if (this.ownsLogger)
|
|
67
|
+
this.logger.close();
|
|
68
|
+
})();
|
|
69
|
+
}
|
|
70
|
+
/** Probes CLIs and models; the catalog is sent only when it differs from the last one sent. */
|
|
71
|
+
async publishCatalog(force) {
|
|
72
|
+
const providers = await this.backend.catalog.refresh({ force });
|
|
73
|
+
const signature = JSON.stringify(providers.map((provider) => ({ ...provider, checkedAt: '' })));
|
|
74
|
+
if (signature === this.catalogSignature)
|
|
75
|
+
return;
|
|
76
|
+
this.catalogSignature = signature;
|
|
77
|
+
this.link.publishCatalog(providers);
|
|
78
|
+
}
|
|
79
|
+
/** Re-detects installed CLIs (the plugin's "重新检测") and reports the result. */
|
|
80
|
+
async refreshDetection() {
|
|
81
|
+
await this.publishCatalog(true);
|
|
82
|
+
return this.backend.catalog.lastDetection();
|
|
83
|
+
}
|
|
84
|
+
/** Current state, for the local dashboard and the plugin. */
|
|
85
|
+
snapshot() {
|
|
86
|
+
const link = this.link.status();
|
|
87
|
+
const detection = this.backend.catalog.lastDetection();
|
|
88
|
+
const connected = link.state === 'connected';
|
|
89
|
+
return {
|
|
90
|
+
deviceId: this.config.deviceId,
|
|
91
|
+
deviceName: this.config.deviceName,
|
|
92
|
+
serverUrl: this.config.serverUrl,
|
|
93
|
+
connected,
|
|
94
|
+
activeSessions: this.backend.activeRuns(),
|
|
95
|
+
storedRuns: this.backend.recentRuns().length,
|
|
96
|
+
startedAt: this.startedAt,
|
|
97
|
+
runs: this.backend.recentRuns().map((run) => ({ runId: run.runId, status: run.status, sessionId: run.sessionKey, updatedAt: new Date(run.at).toISOString() })),
|
|
98
|
+
capabilities: detection?.detectedIds ?? [],
|
|
99
|
+
...(detection ? { detection } : {}),
|
|
100
|
+
...(!connected && link.lastDisconnectReason ? { connectionError: { reason: link.lastDisconnectReason, at: link.lastDisconnectAt ?? this.startedAt } } : {}),
|
|
101
|
+
unauthorized: link.unauthorized,
|
|
102
|
+
activeRunIds: this.backend.activeRunIds()
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function errorMessage(error) {
|
|
107
|
+
return error instanceof Error ? error.message : String(error);
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=hub-runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hub-runtime.js","sourceRoot":"","sources":["../../../src/runtime/hub-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAoB,MAAM,cAAc,CAAA;AAEvF,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAE5C,OAAO,EAAE,sBAAsB,EAAyB,MAAM,8BAA8B,CAAA;AAE5F,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAE/C,gFAAgF;AAChF,MAAM,kBAAkB,GAAG,EAAE,GAAG,MAAM,CAAA;AAEtC,8DAA8D;AAC9D,MAAM,UAAU,aAAa,CAAC,OAAe;IAC3C,OAAO,QAAQ,OAAO,EAAE,CAAA;AAC1B,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,UAAU;IAUQ;IAAsC;IATlD,MAAM,CAAkB;IACxB,UAAU,CAAS;IACnB,OAAO,CAAW;IAClB,IAAI,CAAS;IACb,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAA;IAC7C,YAAY,CAA4B;IACxC,gBAAgB,GAAG,EAAE,CAAA;IACrB,WAAW,CAA2B;IAE9C,YAA6B,MAAmB,EAAmB,OAAe,EAAE,WAA2B,EAAE,MAAyB;QAA7G,WAAM,GAAN,MAAM,CAAa;QAAmB,YAAO,GAAP,OAAO,CAAQ;QAChF,IA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K,EAAE,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,EAAE,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;YAC9J,YAAY,EAAE,SAAS,EAAE,WAAW,IAAI,EAAE;YAC1C,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACnC,GAAG,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,EAAE,EAAE,IAAI,CAAC,gBAAgB,IAAI,IAAI,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC3J,YAAY,EAAE,IAAI,CAAC,YAAY;YAC/B,YAAY,EAAE,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;SAC1C,CAAA;IACH,CAAC;CACF;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC/D,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { CLIQuotaWindow } from '../providers/quota-types.js';
|
|
2
|
+
import type { CliDetectionSnapshot } from '../shared/cli-detection-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The device catalog the Hub stores (`catalog` frame, `providers.refresh`),
|
|
5
|
+
* in the provider shape `service/t3/providers_view.go` reads. This device
|
|
6
|
+
* package exposes exactly three CLIs through the Hub protocol; every other
|
|
7
|
+
* CLI ocra can run stays local.
|
|
8
|
+
*/
|
|
9
|
+
export type HubCli = {
|
|
10
|
+
/** ocra's CLI id (cli-specs.ts). */
|
|
11
|
+
cli: 'codex' | 'claude' | 'antigravity';
|
|
12
|
+
/** `instanceId` / `driver` the Hub and the web know the CLI by. */
|
|
13
|
+
instanceId: string;
|
|
14
|
+
driver: string;
|
|
15
|
+
displayName: string;
|
|
16
|
+
/** Option id of the reasoning level in a model's `optionDescriptors`. */
|
|
17
|
+
effortOptionId: string;
|
|
18
|
+
};
|
|
19
|
+
export declare const HUB_CLIS: readonly HubCli[];
|
|
20
|
+
/** The Hub CLI a model selection's `instanceId` (or a driver / alias) names. */
|
|
21
|
+
export declare function hubCliFor(instanceId: string): HubCli | undefined;
|
|
22
|
+
type ProviderModel = {
|
|
23
|
+
slug: string;
|
|
24
|
+
name: string;
|
|
25
|
+
isDefault: boolean;
|
|
26
|
+
capabilities: {
|
|
27
|
+
optionDescriptors: Array<Record<string, unknown>>;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export type HubProvider = {
|
|
31
|
+
instanceId: string;
|
|
32
|
+
driver: string;
|
|
33
|
+
displayName: string;
|
|
34
|
+
enabled: boolean;
|
|
35
|
+
installed: boolean;
|
|
36
|
+
status: 'ready' | 'warning' | 'error';
|
|
37
|
+
version?: string;
|
|
38
|
+
message?: string;
|
|
39
|
+
checkedAt: string;
|
|
40
|
+
models: ProviderModel[];
|
|
41
|
+
usageLimits?: {
|
|
42
|
+
checkedAt: string;
|
|
43
|
+
windows: Array<Record<string, unknown>>;
|
|
44
|
+
};
|
|
45
|
+
/** Not reported by this device package. */
|
|
46
|
+
skills: never[];
|
|
47
|
+
slashCommands: never[];
|
|
48
|
+
};
|
|
49
|
+
/** ocra's quota windows in the Hub's `usageLimits.windows` shape; credit-only rows have no percentage and are left out. */
|
|
50
|
+
export declare function usageWindows(windows: readonly CLIQuotaWindow[]): Array<Record<string, unknown>>;
|
|
51
|
+
/** Probes the three CLIs and builds the catalog; keeps the last result for `providers()`. */
|
|
52
|
+
export declare class ProviderCatalog {
|
|
53
|
+
private readonly collector;
|
|
54
|
+
private last;
|
|
55
|
+
private detection;
|
|
56
|
+
private readonly quotas;
|
|
57
|
+
private inflight;
|
|
58
|
+
current(): HubProvider[];
|
|
59
|
+
lastDetection(): CliDetectionSnapshot | undefined;
|
|
60
|
+
/** Whether the CLI was found runnable by the last probe (unknown before the first one counts as runnable). */
|
|
61
|
+
runnable(cli: HubCli): {
|
|
62
|
+
ok: true;
|
|
63
|
+
} | {
|
|
64
|
+
ok: false;
|
|
65
|
+
message: string;
|
|
66
|
+
};
|
|
67
|
+
refresh(options?: {
|
|
68
|
+
force?: boolean;
|
|
69
|
+
quota?: boolean;
|
|
70
|
+
providerId?: string;
|
|
71
|
+
}): Promise<HubProvider[]>;
|
|
72
|
+
private probe;
|
|
73
|
+
private readQuotas;
|
|
74
|
+
}
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { cliDetection } from '../providers/detection.js';
|
|
2
|
+
import { ModelCatalogCollector } from '../providers/model-catalog-collector.js';
|
|
3
|
+
import { catalogEntry } from '../providers/registry.js';
|
|
4
|
+
export const HUB_CLIS = [
|
|
5
|
+
{ cli: 'codex', instanceId: 'codex', driver: 'codex', displayName: 'Codex', effortOptionId: 'reasoning_effort' },
|
|
6
|
+
{ cli: 'claude', instanceId: 'claudeAgent', driver: 'claudeAgent', displayName: 'Claude', effortOptionId: 'reasoning_effort' },
|
|
7
|
+
{ cli: 'antigravity', instanceId: 'antigravity', driver: 'antigravity', displayName: 'Antigravity', effortOptionId: 'thought_level' }
|
|
8
|
+
];
|
|
9
|
+
/** The Hub CLI a model selection's `instanceId` (or a driver / alias) names. */
|
|
10
|
+
export function hubCliFor(instanceId) {
|
|
11
|
+
const wanted = instanceId.trim().toLowerCase();
|
|
12
|
+
if (!wanted)
|
|
13
|
+
return undefined;
|
|
14
|
+
return HUB_CLIS.find((entry) => entry.instanceId.toLowerCase() === wanted || entry.driver.toLowerCase() === wanted || entry.cli === wanted
|
|
15
|
+
|| catalogEntry(wanted)?.id === entry.cli);
|
|
16
|
+
}
|
|
17
|
+
const EFFORT_LABELS = { minimal: 'Minimal', low: 'Low', medium: 'Medium', high: 'High', xhigh: 'Extra High', max: 'Max' };
|
|
18
|
+
function models(cli, item) {
|
|
19
|
+
const catalog = item?.catalog;
|
|
20
|
+
if (!catalog)
|
|
21
|
+
return [];
|
|
22
|
+
const defaultModel = catalog.default_model ?? catalog.models[0]?.id;
|
|
23
|
+
return catalog.models.map((model) => {
|
|
24
|
+
const efforts = model.supported_reasoning_efforts ?? [];
|
|
25
|
+
const defaultEffort = model.default_reasoning_effort ?? catalog.default_reasoning_effort ?? '';
|
|
26
|
+
return {
|
|
27
|
+
slug: model.id,
|
|
28
|
+
name: model.name || model.id,
|
|
29
|
+
isDefault: model.id === defaultModel,
|
|
30
|
+
capabilities: {
|
|
31
|
+
optionDescriptors: efforts.length ? [{
|
|
32
|
+
id: cli.effortOptionId,
|
|
33
|
+
label: 'Reasoning',
|
|
34
|
+
type: 'select',
|
|
35
|
+
options: efforts.map((effort) => ({ id: effort, label: EFFORT_LABELS[effort] ?? effort, ...(effort === defaultEffort ? { isDefault: true } : {}) })),
|
|
36
|
+
...(efforts.includes(defaultEffort) ? { currentValue: defaultEffort } : {})
|
|
37
|
+
}] : []
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** ocra's quota windows in the Hub's `usageLimits.windows` shape; credit-only rows have no percentage and are left out. */
|
|
43
|
+
export function usageWindows(windows) {
|
|
44
|
+
return windows.filter((window) => typeof window.used_percent === 'number').map((window) => {
|
|
45
|
+
const minutes = window.window_seconds ? Math.round(window.window_seconds / 60) : undefined;
|
|
46
|
+
const kind = minutes === undefined ? 'other' : minutes <= 6 * 60 ? 'session' : minutes <= 8 * 24 * 60 ? 'weekly' : minutes <= 32 * 24 * 60 ? 'monthly' : 'other';
|
|
47
|
+
return {
|
|
48
|
+
id: window.key,
|
|
49
|
+
kind,
|
|
50
|
+
label: window.label,
|
|
51
|
+
usedPercent: Math.min(100, Math.max(0, window.used_percent)),
|
|
52
|
+
...(window.resets_at > 0 ? { resetsAt: new Date(window.resets_at * 1_000).toISOString() } : {}),
|
|
53
|
+
...(minutes ? { windowDurationMins: minutes } : {})
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const QUOTA_TIMEOUT_MS = 20_000;
|
|
58
|
+
/** Probes the three CLIs and builds the catalog; keeps the last result for `providers()`. */
|
|
59
|
+
export class ProviderCatalog {
|
|
60
|
+
collector = new ModelCatalogCollector();
|
|
61
|
+
last = [];
|
|
62
|
+
detection;
|
|
63
|
+
quotas = new Map();
|
|
64
|
+
inflight;
|
|
65
|
+
current() {
|
|
66
|
+
return this.last;
|
|
67
|
+
}
|
|
68
|
+
lastDetection() {
|
|
69
|
+
return this.detection;
|
|
70
|
+
}
|
|
71
|
+
/** Whether the CLI was found runnable by the last probe (unknown before the first one counts as runnable). */
|
|
72
|
+
runnable(cli) {
|
|
73
|
+
const provider = this.last.find((entry) => entry.instanceId === cli.instanceId);
|
|
74
|
+
if (!provider || provider.status !== 'error')
|
|
75
|
+
return { ok: true };
|
|
76
|
+
return { ok: false, message: provider.message || `${cli.displayName} 在这台设备上不可用` };
|
|
77
|
+
}
|
|
78
|
+
refresh(options = {}) {
|
|
79
|
+
// Refreshes arriving together (web, app, reconnect) share one probe.
|
|
80
|
+
if (this.inflight && !options.quota)
|
|
81
|
+
return this.inflight;
|
|
82
|
+
const task = this.probe(options).finally(() => { if (this.inflight === task)
|
|
83
|
+
this.inflight = undefined; });
|
|
84
|
+
this.inflight = task;
|
|
85
|
+
return task;
|
|
86
|
+
}
|
|
87
|
+
async probe(options) {
|
|
88
|
+
const detection = await cliDetection.detect(Boolean(options.force || options.quota));
|
|
89
|
+
this.detection = detection;
|
|
90
|
+
const detected = HUB_CLIS.filter((cli) => detection.providers.some((provider) => provider.id === cli.cli && provider.installed)).map((cli) => cli.cli);
|
|
91
|
+
const report = await this.collector.collect(detected, { force: Boolean(options.force) });
|
|
92
|
+
if (options.quota)
|
|
93
|
+
await this.readQuotas(detected, options.providerId);
|
|
94
|
+
const checkedAt = new Date().toISOString();
|
|
95
|
+
this.last = HUB_CLIS.map((cli) => {
|
|
96
|
+
const found = detection.providers.find((provider) => provider.id === cli.cli);
|
|
97
|
+
const installed = Boolean(found?.installed);
|
|
98
|
+
const runnable = installed && Boolean(found?.available);
|
|
99
|
+
const item = report.items.find((entry) => entry.agent_type === cli.cli);
|
|
100
|
+
const list = runnable ? models(cli, item) : [];
|
|
101
|
+
const hint = catalogEntry(cli.cli)?.installHint;
|
|
102
|
+
const message = !installed ? `这台设备没有安装 ${cli.displayName}${hint ? `(${hint})` : ''}`
|
|
103
|
+
: !runnable ? (found?.detail || `${cli.displayName} 无法在这台设备上运行`)
|
|
104
|
+
: item?.error ? `读取模型列表失败:${item.error}` : '';
|
|
105
|
+
const usage = this.quotas.get(cli.cli);
|
|
106
|
+
return {
|
|
107
|
+
instanceId: cli.instanceId,
|
|
108
|
+
driver: cli.driver,
|
|
109
|
+
displayName: cli.displayName,
|
|
110
|
+
enabled: true,
|
|
111
|
+
installed,
|
|
112
|
+
status: !runnable ? 'error' : list.length ? 'ready' : 'warning',
|
|
113
|
+
...(found?.version ? { version: found.version } : {}),
|
|
114
|
+
...(message ? { message } : {}),
|
|
115
|
+
checkedAt,
|
|
116
|
+
models: list,
|
|
117
|
+
...(usage && runnable ? { usageLimits: usage } : {}),
|
|
118
|
+
skills: [],
|
|
119
|
+
slashCommands: []
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
return this.last;
|
|
123
|
+
}
|
|
124
|
+
async readQuotas(detected, providerId) {
|
|
125
|
+
const only = providerId ? hubCliFor(providerId)?.cli : undefined;
|
|
126
|
+
await Promise.all(detected.filter((cli) => !only || cli === only).map(async (cli) => {
|
|
127
|
+
const read = catalogEntry(cli)?.quota;
|
|
128
|
+
if (!read)
|
|
129
|
+
return;
|
|
130
|
+
try {
|
|
131
|
+
const windows = await Promise.race([
|
|
132
|
+
read(),
|
|
133
|
+
new Promise((_resolve, reject) => { setTimeout(() => reject(new Error('timeout')), QUOTA_TIMEOUT_MS).unref?.(); })
|
|
134
|
+
]);
|
|
135
|
+
const shaped = usageWindows(windows);
|
|
136
|
+
if (shaped.length)
|
|
137
|
+
this.quotas.set(cli, { checkedAt: new Date().toISOString(), windows: shaped });
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// No usage this time: the Hub keeps what it had for this CLI.
|
|
141
|
+
}
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=provider-catalog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-catalog.js","sourceRoot":"","sources":["../../../src/t3/provider-catalog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAA;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAuBvD,MAAM,CAAC,MAAM,QAAQ,GAAsB;IACzC,EAAE,GAAG,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE;IAChH,EAAE,GAAG,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,QAAQ,EAAE,cAAc,EAAE,kBAAkB,EAAE;IAC9H,EAAE,GAAG,EAAE,aAAa,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAE;CACtI,CAAA;AAED,gFAAgF;AAChF,MAAM,UAAU,SAAS,CAAC,UAAkB;IAC1C,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,EAAE,C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M,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,IAAI,IAAI,GAAG,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YAClF,MAAM,IAAI,GAAG,YAAY,CAAC,GAAG,CAAC,EAAE,KAAK,CAAA;YACrC,IAAI,CAAC,IAAI;gBAAE,OAAM;YACjB,IAAI,CAAC;gBACH,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC;oBACjC,IAAI,EAAE;oBACN,IAAI,OAAO,CAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,KAAK,EAAE,EAAE,CAAA,CAAC,CAAC,CAAC;iBACzH,CAAC,CAAA;gBACF,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,CAAA;gBACpC,IAAI,MAAM,CAAC,MAAM;oBAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAA;YACnG,CAAC;YAAC,MAAM,CAAC;gBACP,8DAA8D;YAChE,CAAC;QACH,CAAC,CAAC,CAAC,CAAA;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type AgentConfig } from '../config.js';
|
|
2
|
+
import type { DeviceBackend } from '../hub/hub-link.js';
|
|
3
|
+
import { type EventEnvelope } from '../hub/protocol.js';
|
|
4
|
+
import type { DiagnosticLogger } from '../logging/diagnostic-log.js';
|
|
5
|
+
import { ProviderCatalog } from './provider-catalog.js';
|
|
6
|
+
type Publisher = {
|
|
7
|
+
event(envelope: EventEnvelope): void;
|
|
8
|
+
};
|
|
9
|
+
/** The reasoning level of a model selection (`options` is a list of `{id, value}` or a map). */
|
|
10
|
+
export declare function selectionEffort(selection: Record<string, unknown>): string;
|
|
11
|
+
export declare class T3Backend implements DeviceBackend {
|
|
12
|
+
private readonly config;
|
|
13
|
+
private readonly logger;
|
|
14
|
+
readonly catalog: ProviderCatalog;
|
|
15
|
+
private readonly store;
|
|
16
|
+
private readonly supervisor;
|
|
17
|
+
private readonly orphans;
|
|
18
|
+
private readonly live;
|
|
19
|
+
/** Run id → thread id for every run this process started. */
|
|
20
|
+
private readonly runThreads;
|
|
21
|
+
private publisher;
|
|
22
|
+
constructor(config: AgentConfig, logger: DiagnosticLogger);
|
|
23
|
+
/** Where events go. Until the link attaches (and while it is down) the link buffers them itself. */
|
|
24
|
+
attach(publisher: Publisher): void;
|
|
25
|
+
/**
|
|
26
|
+
* After a restart: CLI processes the previous agent left behind are
|
|
27
|
+
* stopped, and the runs it had open are closed as interrupted (their
|
|
28
|
+
* process is gone, nothing will ever report them).
|
|
29
|
+
*/
|
|
30
|
+
recover(): Promise<void>;
|
|
31
|
+
stop(): Promise<void>;
|
|
32
|
+
activeRuns(): number;
|
|
33
|
+
activeRunIds(): string[];
|
|
34
|
+
recentRuns(): {
|
|
35
|
+
runId: string;
|
|
36
|
+
sessionKey: string;
|
|
37
|
+
status: string;
|
|
38
|
+
at: number;
|
|
39
|
+
}[];
|
|
40
|
+
providers(): unknown[];
|
|
41
|
+
refreshProviders(options: {
|
|
42
|
+
quota: boolean;
|
|
43
|
+
providerId?: string;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
providers: unknown[];
|
|
46
|
+
}>;
|
|
47
|
+
ensureProject(workspaceRoot?: string): Promise<{
|
|
48
|
+
projectId: string;
|
|
49
|
+
workspaceRoot: string;
|
|
50
|
+
}>;
|
|
51
|
+
snapshot(threadId: string): Promise<unknown>;
|
|
52
|
+
replayAfter(after: number, held: (sequence: number) => boolean): void;
|
|
53
|
+
noteHubCursor(sequence: number): void;
|
|
54
|
+
dispatchCommand(command: Record<string, unknown>): Promise<unknown>;
|
|
55
|
+
private requireThread;
|
|
56
|
+
private cliOf;
|
|
57
|
+
/** The CLI a model selection names; a selection without one keeps the thread's CLI. */
|
|
58
|
+
private selectionCli;
|
|
59
|
+
private createThread;
|
|
60
|
+
private deleteThread;
|
|
61
|
+
private archiveThread;
|
|
62
|
+
private renameThread;
|
|
63
|
+
/** A model change inside the thread's CLI; another CLI is refused (one thread, one CLI). */
|
|
64
|
+
private selectModel;
|
|
65
|
+
private applySelection;
|
|
66
|
+
private dispatchMessage;
|
|
67
|
+
/** Saves inline images next to the device's data and names them in the prompt; other attachments are listed by URL. */
|
|
68
|
+
private saveAttachments;
|
|
69
|
+
private launch;
|
|
70
|
+
private startNextQueued;
|
|
71
|
+
private interruptRun;
|
|
72
|
+
private runOf;
|
|
73
|
+
private onRunStarted;
|
|
74
|
+
private onRunContent;
|
|
75
|
+
private flushItems;
|
|
76
|
+
private dropLive;
|
|
77
|
+
private onRunFinished;
|
|
78
|
+
/** Ends a run with its final state, then starts the thread's next queued turn. */
|
|
79
|
+
private closeRun;
|
|
80
|
+
private emit;
|
|
81
|
+
private emitRun;
|
|
82
|
+
private emitItem;
|
|
83
|
+
private threadState;
|
|
84
|
+
private emitThread;
|
|
85
|
+
}
|
|
86
|
+
export {};
|