@botlearn-course/daemon 0.0.29-beta.2 → 0.0.29-beta.4
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 +29 -89
- package/dist/auth-store.d.ts +1 -11
- package/dist/auth-store.js +2 -83
- package/dist/cli.d.ts +0 -15
- package/dist/cli.js +12 -204
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/run-dispatcher.d.ts +1 -5
- package/dist/run-dispatcher.js +41 -116
- package/dist/runtime-capabilities.js +3 -11
- package/dist/runtimes/engine.js +15 -52
- package/dist/runtimes/index.d.ts +1 -7
- package/dist/runtimes/index.js +1 -45
- package/dist/runtimes/pi-agent.js +73 -10
- package/dist/runtimes/pi-managed-launch.d.ts +5 -0
- package/dist/runtimes/pi-managed-launch.js +41 -0
- package/dist/runtimes/pi-managed-subagent-extension.d.ts +127 -0
- package/dist/runtimes/pi-managed-subagent-extension.js +303 -0
- package/dist/runtimes/pi-managed.d.ts +16 -0
- package/dist/runtimes/pi-managed.js +57 -2
- package/dist/runtimes/probe.d.ts +0 -5
- package/dist/runtimes/probe.js +0 -16
- package/dist/runtimes/tool-semantics.js +1 -0
- package/dist/types.d.ts +10 -73
- package/dist/workspace.d.ts +0 -2
- package/dist/workspace.js +0 -10
- package/package.json +2 -2
- package/dist/course-client.d.ts +0 -46
- package/dist/course-client.js +0 -158
- package/dist/doctor.d.ts +0 -15
- package/dist/doctor.js +0 -85
- package/dist/runtime-profile.d.ts +0 -8
- package/dist/runtime-profile.js +0 -236
- package/dist/runtimes/acp-stream.d.ts +0 -96
- package/dist/runtimes/acp-stream.js +0 -490
- package/dist/runtimes/claude-code.d.ts +0 -41
- package/dist/runtimes/claude-code.js +0 -364
- package/dist/runtimes/codex.d.ts +0 -44
- package/dist/runtimes/codex.js +0 -348
- package/dist/runtimes/gemini.d.ts +0 -39
- package/dist/runtimes/gemini.js +0 -264
- package/dist/runtimes/hermes-agent.d.ts +0 -61
- package/dist/runtimes/hermes-agent.js +0 -191
- package/dist/runtimes/kimi.d.ts +0 -35
- package/dist/runtimes/kimi.js +0 -343
- package/dist/runtimes/native-tool-blocks.d.ts +0 -3
- package/dist/runtimes/native-tool-blocks.js +0 -98
- package/dist/runtimes/ndjson-stream.d.ts +0 -48
- package/dist/runtimes/ndjson-stream.js +0 -210
- package/dist/runtimes/openclaw-acp.d.ts +0 -52
- package/dist/runtimes/openclaw-acp.js +0 -893
- package/dist/runtimes/runtime-errors.d.ts +0 -20
- package/dist/runtimes/runtime-errors.js +0 -95
- package/dist/runtimes/text-cap.d.ts +0 -7
- package/dist/runtimes/text-cap.js +0 -25
|
@@ -1,8 +1,31 @@
|
|
|
1
|
-
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
3
4
|
import { runtimeChildEnv } from "../runtime-env.js";
|
|
4
5
|
export const MANAGED_PI_BINARY = "/opt/pi-agent/0.85.1/bin/pi";
|
|
5
6
|
export const MANAGED_PI_WEB_ACCESS_EXTENSION = "/opt/pi-agent/0.85.1/node_modules/pi-web-access/dist/index.js";
|
|
7
|
+
export const MANAGED_PI_SUBAGENT_EXTENSION = fileURLToPath(new URL("./pi-managed-subagent-extension.js", import.meta.url));
|
|
8
|
+
/**
|
|
9
|
+
* The only delegation role. The managed extension loads this root-owned prompt directly;
|
|
10
|
+
* workspace agent files are never discovered.
|
|
11
|
+
*/
|
|
12
|
+
const SUBAGENT_WORKER_ROLE = `---
|
|
13
|
+
name: worker
|
|
14
|
+
description: 通用子任务执行者。在独立上下文中完成一项明确的委派任务并返回结果。
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
你是练习场的子任务执行者。你在独立上下文中运行,只处理本次委派的任务。
|
|
18
|
+
|
|
19
|
+
委派你的智能体看不到你读过的内容。返回结论、依据和它继续工作所需的事实,
|
|
20
|
+
不要复制无关的大段原文。
|
|
21
|
+
|
|
22
|
+
所有回复使用简体中文。代码、命令、文件路径、标识符和日志原文保持原样。
|
|
23
|
+
|
|
24
|
+
只做委派任务要求的事。不要判断学员的学习进度,不要声称检查点或课程完成,
|
|
25
|
+
不要修改与任务无关的文件。
|
|
26
|
+
|
|
27
|
+
如实报告结果。任务无法完成时说明卡在哪里,不要把未验证的结果写成已经成功。
|
|
28
|
+
`;
|
|
6
29
|
/** The image verifies the CLI once; per-turn probes read immutable installed metadata. */
|
|
7
30
|
export function managedPiVersion() {
|
|
8
31
|
try {
|
|
@@ -63,10 +86,42 @@ export async function prepareManagedPi(opts) {
|
|
|
63
86
|
// The supervisor inherits umask 0077 in veFaaS. Restore group-read after
|
|
64
87
|
// creation so the separate runtime UID can load the immutable model contract.
|
|
65
88
|
chmodSync(modelsFile, 0o640);
|
|
66
|
-
|
|
89
|
+
const delegationDir = path.join(home, "managed-delegation");
|
|
90
|
+
mkdirSync(delegationDir, { mode: 0o750 });
|
|
91
|
+
chmodSync(delegationDir, 0o750);
|
|
92
|
+
return { env: { ...source, HOME: home, PI_CODING_AGENT_DIR: agentDir }, delegationDir, cleanup };
|
|
67
93
|
}
|
|
68
94
|
catch (error) {
|
|
69
95
|
cleanup();
|
|
70
96
|
throw error;
|
|
71
97
|
}
|
|
72
98
|
}
|
|
99
|
+
/** Called after the course Skills socket exists, before the first Pi process starts. */
|
|
100
|
+
export function writeManagedPiDelegation(dir, opts, skillsSocket) {
|
|
101
|
+
const modelIndex = opts.extraArgs?.indexOf("--model") ?? -1;
|
|
102
|
+
const thinkingIndex = opts.extraArgs?.indexOf("--thinking") ?? -1;
|
|
103
|
+
const model = modelIndex >= 0 ? opts.extraArgs?.[modelIndex + 1] : undefined;
|
|
104
|
+
const thinking = thinkingIndex >= 0 ? opts.extraArgs?.[thinkingIndex + 1] : undefined;
|
|
105
|
+
if (!model || opts.systemPrompt === undefined || !skillsSocket.startsWith("/")) {
|
|
106
|
+
throw new Error("pi-agent managed delegation configuration is incomplete");
|
|
107
|
+
}
|
|
108
|
+
const workerPromptPath = path.join(dir, "worker.md");
|
|
109
|
+
writeFileSync(workerPromptPath, SUBAGENT_WORKER_ROLE, { mode: 0o640, flag: "wx" });
|
|
110
|
+
chmodSync(workerPromptPath, 0o640);
|
|
111
|
+
const manifest = {
|
|
112
|
+
binary: MANAGED_PI_BINARY,
|
|
113
|
+
model: `botlearn/${model}`,
|
|
114
|
+
workspaceDir: realpathSync(opts.cwd),
|
|
115
|
+
...(thinking ? { thinking } : {}),
|
|
116
|
+
systemPrompt: opts.systemPrompt,
|
|
117
|
+
workerPromptPath,
|
|
118
|
+
webExtension: MANAGED_PI_WEB_ACCESS_EXTENSION,
|
|
119
|
+
skillsExtension: path.resolve(fileURLToPath(new URL("./pi-course-skills-extension.js", import.meta.url))),
|
|
120
|
+
skillsSocket,
|
|
121
|
+
subagentExtension: MANAGED_PI_SUBAGENT_EXTENSION,
|
|
122
|
+
};
|
|
123
|
+
const file = path.join(dir, "launch.json");
|
|
124
|
+
writeFileSync(file, JSON.stringify(manifest), { mode: 0o640, flag: "wx" });
|
|
125
|
+
chmodSync(file, 0o640);
|
|
126
|
+
return file;
|
|
127
|
+
}
|
package/dist/runtimes/probe.d.ts
CHANGED
|
@@ -3,15 +3,10 @@ import { execFileSync } from "node:child_process";
|
|
|
3
3
|
export interface ProbeDeps {
|
|
4
4
|
platform?: NodeJS.Platform;
|
|
5
5
|
env?: NodeJS.ProcessEnv;
|
|
6
|
-
homeDir?: string;
|
|
7
6
|
execFileSyncFn?: typeof execFileSync;
|
|
8
7
|
existsSyncFn?: (p: string) => boolean;
|
|
9
8
|
}
|
|
10
9
|
/** Resolve a command name on PATH via `which`/`where`; returns null when missing. */
|
|
11
10
|
export declare function resolveCommandOnPath(command: string, deps?: ProbeDeps): string | null;
|
|
12
|
-
/** Return the first path in `candidates` that exists on disk, or null. */
|
|
13
|
-
export declare function firstExistingPath(candidates: string[], deps?: ProbeDeps): string | null;
|
|
14
11
|
/** Run `<command> [...args] --version` and return the first output line, or null. */
|
|
15
12
|
export declare function readCommandVersion(command: string, args?: string[], deps?: ProbeDeps): string | null;
|
|
16
|
-
/** Join `relativePath` against HOME (falls back to empty when unset). */
|
|
17
|
-
export declare function resolveHomePath(relativePath: string, deps?: ProbeDeps): string;
|
package/dist/runtimes/probe.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
2
|
function normalizeExecOutput(raw) {
|
|
5
3
|
return Buffer.isBuffer(raw) ? raw.toString("utf8") : String(raw ?? "");
|
|
6
4
|
}
|
|
@@ -22,15 +20,6 @@ export function resolveCommandOnPath(command, deps = {}) {
|
|
|
22
20
|
return null;
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
|
-
/** Return the first path in `candidates` that exists on disk, or null. */
|
|
26
|
-
export function firstExistingPath(candidates, deps = {}) {
|
|
27
|
-
const exists = deps.existsSyncFn ?? existsSync;
|
|
28
|
-
for (const c of candidates) {
|
|
29
|
-
if (exists(c))
|
|
30
|
-
return c;
|
|
31
|
-
}
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
23
|
/** Run `<command> [...args] --version` and return the first output line, or null. */
|
|
35
24
|
export function readCommandVersion(command, args = [], deps = {}) {
|
|
36
25
|
const env = deps.env ?? process.env;
|
|
@@ -47,8 +36,3 @@ export function readCommandVersion(command, args = [], deps = {}) {
|
|
|
47
36
|
return null;
|
|
48
37
|
}
|
|
49
38
|
}
|
|
50
|
-
/** Join `relativePath` against HOME (falls back to empty when unset). */
|
|
51
|
-
export function resolveHomePath(relativePath, deps = {}) {
|
|
52
|
-
const home = deps.homeDir ?? deps.env?.HOME ?? process.env.HOME ?? "";
|
|
53
|
-
return path.join(home, relativePath);
|
|
54
|
-
}
|
|
@@ -19,6 +19,7 @@ const ACTIONS = {
|
|
|
19
19
|
tool_search: 'discover_tools', tool_search_tool_regex: 'discover_tools',
|
|
20
20
|
mcp_courseskills_list_skills: 'load_skill', mcp_courseskills_load_skill: 'load_skill', mcp_courseskills_load_reference: 'load_skill',
|
|
21
21
|
load_skill: 'load_skill',
|
|
22
|
+
subagent: 'delegate',
|
|
22
23
|
todo_write: 'plan', update_plan: 'plan', request_user_input: 'request_input',
|
|
23
24
|
};
|
|
24
25
|
export function toolAction(name) {
|
package/dist/types.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface RunInputAttachment {
|
|
|
18
18
|
export interface MaterializedInputAttachment extends RunInputAttachment {
|
|
19
19
|
workspace_path: string;
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
21
|
+
/** Agent Service `turn.start` 下发的 run.start 载荷(snake_case,与后端 RunStartPayloadOut 一致)。 */
|
|
22
22
|
export interface RunStartPayload {
|
|
23
23
|
runtime_prompt?: RuntimePrompt;
|
|
24
24
|
agent_run_id: string;
|
|
@@ -28,7 +28,7 @@ export interface RunStartPayload {
|
|
|
28
28
|
task_id: string | null;
|
|
29
29
|
agent_instance_id: string | null;
|
|
30
30
|
runtime: {
|
|
31
|
-
id
|
|
31
|
+
id: string;
|
|
32
32
|
model?: string;
|
|
33
33
|
reasoning_effort?: string;
|
|
34
34
|
model_contract?: {
|
|
@@ -39,8 +39,6 @@ export interface RunStartPayload {
|
|
|
39
39
|
max_output_tokens_limit: number;
|
|
40
40
|
reasoning_efforts: string[];
|
|
41
41
|
};
|
|
42
|
-
thinking?: boolean;
|
|
43
|
-
web_search?: boolean;
|
|
44
42
|
[key: string]: unknown;
|
|
45
43
|
};
|
|
46
44
|
input: {
|
|
@@ -50,7 +48,6 @@ export interface RunStartPayload {
|
|
|
50
48
|
attachments?: RunInputAttachment[];
|
|
51
49
|
};
|
|
52
50
|
context: {
|
|
53
|
-
instructions?: string[];
|
|
54
51
|
metadata?: Record<string, unknown>;
|
|
55
52
|
[key: string]: unknown;
|
|
56
53
|
};
|
|
@@ -62,8 +59,8 @@ export interface RunStartPayload {
|
|
|
62
59
|
};
|
|
63
60
|
}
|
|
64
61
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
62
|
+
* 经 Agent Service `turn.event` 上报的 run 事件类型。
|
|
63
|
+
* 必须与 Course Service 接受的事件类型一致,发送其他类型会被拒绝。
|
|
67
64
|
*/
|
|
68
65
|
export type RunEventType = "run.accepted" | "run.started" | "run.block" | "run.observation" | "run.trace" | "run.message" | "run.completed" | "run.failed" | "run.cancelled";
|
|
69
66
|
export interface RunEvent {
|
|
@@ -84,7 +81,7 @@ export interface RunEventReceipt {
|
|
|
84
81
|
candidate_attempt?: number;
|
|
85
82
|
retry_feedback?: string;
|
|
86
83
|
}
|
|
87
|
-
/**
|
|
84
|
+
/** 经 Agent Service 上报的文件候选。 */
|
|
88
85
|
export interface RunFileCandidate {
|
|
89
86
|
event?: "created" | "modified" | "deleted";
|
|
90
87
|
/** workspace 相对路径,正斜杠;不得包含 `..`、绝对路径或反斜杠(后端 400)。 */
|
|
@@ -101,43 +98,6 @@ export interface RunFileRecord extends RunFileCandidate {
|
|
|
101
98
|
storage_url?: string | null;
|
|
102
99
|
status: "candidate" | "saved" | "ignored" | "failed";
|
|
103
100
|
}
|
|
104
|
-
export interface RuntimeProfileArchiveFile {
|
|
105
|
-
path: string;
|
|
106
|
-
content: string;
|
|
107
|
-
sha256?: string;
|
|
108
|
-
size?: number;
|
|
109
|
-
}
|
|
110
|
-
export interface RuntimeProfileSkillPackage {
|
|
111
|
-
id: string;
|
|
112
|
-
version: string;
|
|
113
|
-
digest: string;
|
|
114
|
-
archiveManifest: {
|
|
115
|
-
name?: string;
|
|
116
|
-
skillMd: string;
|
|
117
|
-
files?: RuntimeProfileArchiveFile[];
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
export interface CourseRuntimeProfile {
|
|
121
|
-
schemaVersion: "botlearn-course-runtime-profile/0.1";
|
|
122
|
-
profileId: string;
|
|
123
|
-
profileHash: string;
|
|
124
|
-
courseVersionId: string;
|
|
125
|
-
promptPack: {
|
|
126
|
-
id: string;
|
|
127
|
-
version: string;
|
|
128
|
-
digest: string;
|
|
129
|
-
systemInstructions: string;
|
|
130
|
-
};
|
|
131
|
-
skillPackages: RuntimeProfileSkillPackage[];
|
|
132
|
-
requiredCapabilities: string[];
|
|
133
|
-
}
|
|
134
|
-
export interface AppliedRunRuntimeProfile {
|
|
135
|
-
profileId: string;
|
|
136
|
-
profileHash: string;
|
|
137
|
-
promptPackPath: string;
|
|
138
|
-
skillsRoot: string;
|
|
139
|
-
skillRefs: string[];
|
|
140
|
-
}
|
|
141
101
|
/** runtime adapter 产出的普通归一化块;raw 仅进本地 transcript。 */
|
|
142
102
|
export interface RuntimeContentBlock {
|
|
143
103
|
activity?: RuntimeActivity;
|
|
@@ -192,17 +152,11 @@ export interface RuntimeCompletionSummary {
|
|
|
192
152
|
turn_status?: string;
|
|
193
153
|
finish_reason?: string;
|
|
194
154
|
}
|
|
195
|
-
|
|
196
|
-
checked: boolean;
|
|
197
|
-
ok: boolean;
|
|
198
|
-
message?: string;
|
|
199
|
-
}
|
|
200
|
-
/** doctor / capabilities 使用的探测结果。 */
|
|
155
|
+
/** runtime 安装/版本探测结果。 */
|
|
201
156
|
export interface RuntimeProbe {
|
|
202
157
|
available: boolean;
|
|
203
158
|
path?: string;
|
|
204
159
|
version?: string;
|
|
205
|
-
auth?: RuntimeAuthProbe;
|
|
206
160
|
}
|
|
207
161
|
/** runtime 执行期间向 dispatcher 汇报的通道。adapter 不直接触碰 Course API。 */
|
|
208
162
|
export interface CourseRuntimeSink {
|
|
@@ -277,32 +231,15 @@ export interface RuntimeFailureSummary {
|
|
|
277
231
|
export interface RuntimeModule {
|
|
278
232
|
id: string;
|
|
279
233
|
displayName: string;
|
|
280
|
-
/** PATH
|
|
234
|
+
/** PATH 上的规范二进制名。 */
|
|
281
235
|
binary: string;
|
|
282
236
|
/** 覆盖二进制路径的环境变量;缺省为 `BOTLEARN_<ID>_BIN`(id 大写、`-`→`_`)。 */
|
|
283
237
|
envVar?: string;
|
|
284
|
-
/**
|
|
238
|
+
/** 快速探测(安装/版本)。必须廉价,不得触发真实模型调用。 */
|
|
285
239
|
probe(): Promise<RuntimeProbe>;
|
|
286
|
-
/** 登录态探测,可能昂贵(如真实调用一次 CLI)。仅 doctor 按需调用。 */
|
|
287
|
-
probeAuth?(): Promise<RuntimeAuthProbe>;
|
|
288
240
|
create(): CourseRuntime;
|
|
289
|
-
/** probe
|
|
241
|
+
/** probe 不可用时的安装提示。 */
|
|
290
242
|
installHint?: string;
|
|
291
|
-
/** 仅测试/demo 用(如 fake
|
|
243
|
+
/** 仅测试/demo 用(如 fake),除非显式启用否则不注册。 */
|
|
292
244
|
hidden?: boolean;
|
|
293
245
|
}
|
|
294
|
-
export interface RuntimeProbeEntry {
|
|
295
|
-
id: string;
|
|
296
|
-
displayName: string;
|
|
297
|
-
binary: string;
|
|
298
|
-
result: RuntimeProbe;
|
|
299
|
-
installHint?: string;
|
|
300
|
-
}
|
|
301
|
-
export interface DaemonAuth {
|
|
302
|
-
daemonId: string;
|
|
303
|
-
userId: string;
|
|
304
|
-
label: string;
|
|
305
|
-
accessToken: string;
|
|
306
|
-
refreshToken: string;
|
|
307
|
-
courseApiUrl: string;
|
|
308
|
-
}
|
package/dist/workspace.d.ts
CHANGED
|
@@ -16,8 +16,6 @@ export declare function runRootDir(agentRunId: string): string;
|
|
|
16
16
|
/** runtime 的 cwd。 */
|
|
17
17
|
export declare function runWorkspaceDir(agentRunId: string): string;
|
|
18
18
|
export declare function transcriptPath(agentRunId: string): string;
|
|
19
|
-
export declare function runtimeProfileDir(agentRunId: string): string;
|
|
20
|
-
export declare function runtimeProfileRunRootDir(agentRunId: string): string;
|
|
21
19
|
export declare function runtimeSessionRootDir(runtimeSessionId: string, sandboxGeneration: number): string;
|
|
22
20
|
/**
|
|
23
21
|
* durable workspace:managed sandbox 使用稳定路径
|
package/dist/workspace.js
CHANGED
|
@@ -32,16 +32,6 @@ export function runWorkspaceDir(agentRunId) {
|
|
|
32
32
|
export function transcriptPath(agentRunId) {
|
|
33
33
|
return path.join(runRootDir(agentRunId), "transcript.jsonl");
|
|
34
34
|
}
|
|
35
|
-
export function runtimeProfileDir(agentRunId) {
|
|
36
|
-
assertSafeId(agentRunId, "agent_run_id");
|
|
37
|
-
const managedRoot = process.env.BOTLEARN_AGENT_SERVICE_PROFILE_ROOT?.trim();
|
|
38
|
-
return managedRoot
|
|
39
|
-
? path.join(managedRoot, agentRunId, "runtime-profile")
|
|
40
|
-
: path.join(runRootDir(agentRunId), "runtime-profile");
|
|
41
|
-
}
|
|
42
|
-
export function runtimeProfileRunRootDir(agentRunId) {
|
|
43
|
-
return path.dirname(runtimeProfileDir(agentRunId));
|
|
44
|
-
}
|
|
45
35
|
export function runtimeSessionRootDir(runtimeSessionId, sandboxGeneration) {
|
|
46
36
|
assertSafeId(runtimeSessionId, "runtime_session_id");
|
|
47
37
|
if (!Number.isInteger(sandboxGeneration) || sandboxGeneration < 1) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botlearn-course/daemon",
|
|
3
|
-
"version": "0.0.29-beta.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.29-beta.4",
|
|
4
|
+
"description": "BotLearn Course daemon for Agent Service managed sandboxes (internal).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"botlearn-course-daemon": "dist/cli.js",
|
package/dist/course-client.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { CourseRuntimeProfile, DaemonAuth, RunEvent, RunEventReceipt, RunFileCandidate, RunFileRecord, RunStartPayload } from "./types.js";
|
|
2
|
-
export declare class CourseClientError extends Error {
|
|
3
|
-
readonly status: number;
|
|
4
|
-
constructor(status: number, message: string);
|
|
5
|
-
}
|
|
6
|
-
/** 401:daemon 凭据失效(refresh 也救不回来),调用方应引导重新 login。 */
|
|
7
|
-
export declare function isAuthFailure(err: unknown): boolean;
|
|
8
|
-
/** 409:run 在服务端已进入终态(如用户取消),后续上报应静默停止。 */
|
|
9
|
-
export declare function isRunTerminal(err: unknown): boolean;
|
|
10
|
-
export interface CourseClientOptions {
|
|
11
|
-
refreshToken?: string;
|
|
12
|
-
onAuthRefreshed?: (auth: DaemonAuth) => void;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Course Service HTTP 客户端。
|
|
16
|
-
*
|
|
17
|
-
* Course Service 是真相源;client 只用短期 access token 回报已分配给本 daemon 的 run。
|
|
18
|
-
* 第一版用 POST poll/events,不做 WS。401 时自动用 refresh token 换新并单次重试。
|
|
19
|
-
*/
|
|
20
|
-
export declare class CourseClient {
|
|
21
|
-
private readonly baseUrl;
|
|
22
|
-
private accessToken;
|
|
23
|
-
private refreshToken?;
|
|
24
|
-
private readonly onAuthRefreshed?;
|
|
25
|
-
constructor(baseUrl: string, accessToken: string, opts?: CourseClientOptions);
|
|
26
|
-
static fromAuth(auth: DaemonAuth, opts?: Omit<CourseClientOptions, "refreshToken">): CourseClient;
|
|
27
|
-
setAccessToken(token: string): void;
|
|
28
|
-
private url;
|
|
29
|
-
fetchJson<T>(method: string, p: string, body: unknown, accessToken: string | null, traceId?: string, signal?: AbortSignal): Promise<T>;
|
|
30
|
-
private refreshAuth;
|
|
31
|
-
private request;
|
|
32
|
-
/** 领取下一个分配给本 daemon 的 queued run(无则返回 null)。 */
|
|
33
|
-
claimNextRun(): Promise<RunStartPayload | null>;
|
|
34
|
-
postEvent(agentRunId: string, event: RunEvent): Promise<RunEventReceipt>;
|
|
35
|
-
postFiles(agentRunId: string, files: RunFileCandidate[], truncated?: boolean, signal?: AbortSignal): Promise<RunFileRecord[]>;
|
|
36
|
-
getRunRuntimeProfile(agentRunId: string): Promise<CourseRuntimeProfile>;
|
|
37
|
-
}
|
|
38
|
-
export interface LoginOptions {
|
|
39
|
-
courseApiUrl: string;
|
|
40
|
-
code: string;
|
|
41
|
-
label?: string;
|
|
42
|
-
machineFingerprint?: string;
|
|
43
|
-
capabilities?: Record<string, unknown>;
|
|
44
|
-
}
|
|
45
|
-
export declare function loginCourseDaemon(opts: LoginOptions): Promise<DaemonAuth>;
|
|
46
|
-
export declare function refreshCourseDaemonSession(courseApiUrl: string, refreshToken: string): Promise<DaemonAuth>;
|
package/dist/course-client.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { redactSecretString, redactSecretsDeep } from "./redaction.js";
|
|
2
|
-
export class CourseClientError extends Error {
|
|
3
|
-
status;
|
|
4
|
-
constructor(status, message) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.status = status;
|
|
7
|
-
this.name = "CourseClientError";
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
/** 401:daemon 凭据失效(refresh 也救不回来),调用方应引导重新 login。 */
|
|
11
|
-
export function isAuthFailure(err) {
|
|
12
|
-
return err instanceof CourseClientError && err.status === 401;
|
|
13
|
-
}
|
|
14
|
-
/** 409:run 在服务端已进入终态(如用户取消),后续上报应静默停止。 */
|
|
15
|
-
export function isRunTerminal(err) {
|
|
16
|
-
return err instanceof CourseClientError && err.status === 409;
|
|
17
|
-
}
|
|
18
|
-
// 错误信息里只保留响应文本前 500 字符,且先脱敏——绝不回显请求体(可能含 code/token)。
|
|
19
|
-
const ERROR_BODY_MAX_CHARS = 500;
|
|
20
|
-
/**
|
|
21
|
-
* Course Service HTTP 客户端。
|
|
22
|
-
*
|
|
23
|
-
* Course Service 是真相源;client 只用短期 access token 回报已分配给本 daemon 的 run。
|
|
24
|
-
* 第一版用 POST poll/events,不做 WS。401 时自动用 refresh token 换新并单次重试。
|
|
25
|
-
*/
|
|
26
|
-
export class CourseClient {
|
|
27
|
-
baseUrl;
|
|
28
|
-
accessToken;
|
|
29
|
-
refreshToken;
|
|
30
|
-
onAuthRefreshed;
|
|
31
|
-
constructor(baseUrl, accessToken, opts = {}) {
|
|
32
|
-
this.baseUrl = baseUrl;
|
|
33
|
-
this.accessToken = accessToken;
|
|
34
|
-
this.refreshToken = opts.refreshToken;
|
|
35
|
-
this.onAuthRefreshed = opts.onAuthRefreshed;
|
|
36
|
-
}
|
|
37
|
-
static fromAuth(auth, opts = {}) {
|
|
38
|
-
return new CourseClient(auth.courseApiUrl, auth.accessToken, {
|
|
39
|
-
...opts,
|
|
40
|
-
refreshToken: auth.refreshToken,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
setAccessToken(token) {
|
|
44
|
-
this.accessToken = token;
|
|
45
|
-
}
|
|
46
|
-
// courseApiUrl 可能是 host 根,也可能已带 /course/v1 —— 去重避免拼出 /course/v1/course/v1。
|
|
47
|
-
url(p) {
|
|
48
|
-
const base = this.baseUrl.replace(/\/+$/, "");
|
|
49
|
-
if (base.endsWith("/course/v1") && p.startsWith("/course/v1/")) {
|
|
50
|
-
return `${base}${p.slice("/course/v1".length)}`;
|
|
51
|
-
}
|
|
52
|
-
return `${base}${p}`;
|
|
53
|
-
}
|
|
54
|
-
async fetchJson(method, p, body, accessToken, traceId, signal) {
|
|
55
|
-
const res = await fetch(this.url(p), {
|
|
56
|
-
method,
|
|
57
|
-
signal,
|
|
58
|
-
headers: {
|
|
59
|
-
...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}),
|
|
60
|
-
...(traceId ? { "x-trace-id": traceId } : {}),
|
|
61
|
-
...(body !== undefined ? { "content-type": "application/json" } : {}),
|
|
62
|
-
},
|
|
63
|
-
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
64
|
-
});
|
|
65
|
-
if (!res.ok) {
|
|
66
|
-
const text = await res.text().catch(() => "");
|
|
67
|
-
const detail = redactSecretString(text.slice(0, ERROR_BODY_MAX_CHARS));
|
|
68
|
-
throw new CourseClientError(res.status, `${method} ${p} -> ${res.status} ${detail}`);
|
|
69
|
-
}
|
|
70
|
-
const raw = await res.text();
|
|
71
|
-
return (raw ? JSON.parse(raw) : null);
|
|
72
|
-
}
|
|
73
|
-
async refreshAuth() {
|
|
74
|
-
if (!this.refreshToken)
|
|
75
|
-
return false;
|
|
76
|
-
try {
|
|
77
|
-
const auth = await refreshCourseDaemonSession(this.baseUrl, this.refreshToken);
|
|
78
|
-
this.accessToken = auth.accessToken;
|
|
79
|
-
this.refreshToken = auth.refreshToken;
|
|
80
|
-
this.onAuthRefreshed?.(auth);
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
async request(method, p, body, traceId, signal) {
|
|
88
|
-
try {
|
|
89
|
-
return await this.fetchJson(method, p, body, this.accessToken, traceId, signal);
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
if (!(err instanceof CourseClientError) || err.status !== 401)
|
|
93
|
-
throw err;
|
|
94
|
-
const refreshed = await this.refreshAuth();
|
|
95
|
-
if (!refreshed)
|
|
96
|
-
throw err;
|
|
97
|
-
return await this.fetchJson(method, p, body, this.accessToken, traceId, signal);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
/** 领取下一个分配给本 daemon 的 queued run(无则返回 null)。 */
|
|
101
|
-
async claimNextRun() {
|
|
102
|
-
return this.request("GET", "/course/v1/daemon/runs/next");
|
|
103
|
-
}
|
|
104
|
-
async postEvent(agentRunId, event) {
|
|
105
|
-
const credentials = [this.accessToken, this.refreshToken].filter((value) => typeof value === "string");
|
|
106
|
-
const sanitized = redactSecretsDeep(event, 8, credentials);
|
|
107
|
-
const response = await this.request("POST", `/course/v1/daemon/runs/${agentRunId}/events`, sanitized, event.trace_id);
|
|
108
|
-
return response?.disposition === "retry"
|
|
109
|
-
? {
|
|
110
|
-
disposition: "retry",
|
|
111
|
-
...(typeof response.candidate_attempt === "number"
|
|
112
|
-
? { candidate_attempt: response.candidate_attempt }
|
|
113
|
-
: {}),
|
|
114
|
-
...(typeof response.retry_feedback === "string"
|
|
115
|
-
? { retry_feedback: response.retry_feedback }
|
|
116
|
-
: {}),
|
|
117
|
-
}
|
|
118
|
-
: { disposition: "accepted" };
|
|
119
|
-
}
|
|
120
|
-
async postFiles(agentRunId, files, truncated = false, signal) {
|
|
121
|
-
return this.request("POST", `/course/v1/daemon/runs/${agentRunId}/files`, { files, truncated }, undefined, signal);
|
|
122
|
-
}
|
|
123
|
-
async getRunRuntimeProfile(agentRunId) {
|
|
124
|
-
return this.request("GET", `/course/v1/daemon/runs/${agentRunId}/runtime-profile`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function authFromResponse(baseUrl, raw) {
|
|
128
|
-
const daemonId = raw.daemonId ?? raw.daemon_id;
|
|
129
|
-
const userId = raw.userId ?? raw.user_id;
|
|
130
|
-
const accessToken = raw.accessToken ?? raw.access_token;
|
|
131
|
-
const refreshToken = raw.refreshToken ?? raw.refresh_token;
|
|
132
|
-
if (!daemonId || !userId || !accessToken || !refreshToken) {
|
|
133
|
-
throw new Error("daemon auth response is missing required fields");
|
|
134
|
-
}
|
|
135
|
-
return {
|
|
136
|
-
daemonId,
|
|
137
|
-
userId,
|
|
138
|
-
label: raw.label ?? "",
|
|
139
|
-
accessToken,
|
|
140
|
-
refreshToken,
|
|
141
|
-
courseApiUrl: raw.courseApiUrl ?? raw.course_api_url ?? baseUrl,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
export async function loginCourseDaemon(opts) {
|
|
145
|
-
const client = new CourseClient(opts.courseApiUrl, "");
|
|
146
|
-
const raw = await client.fetchJson("POST", "/course/v1/daemon/login", {
|
|
147
|
-
code: opts.code,
|
|
148
|
-
label: opts.label ?? "",
|
|
149
|
-
machine_fingerprint: opts.machineFingerprint,
|
|
150
|
-
capabilities: opts.capabilities ?? {},
|
|
151
|
-
}, null);
|
|
152
|
-
return authFromResponse(opts.courseApiUrl, raw);
|
|
153
|
-
}
|
|
154
|
-
export async function refreshCourseDaemonSession(courseApiUrl, refreshToken) {
|
|
155
|
-
const client = new CourseClient(courseApiUrl, "");
|
|
156
|
-
const raw = await client.fetchJson("POST", "/course/v1/daemon/session", { refresh_token: refreshToken }, null);
|
|
157
|
-
return authFromResponse(courseApiUrl, raw);
|
|
158
|
-
}
|
package/dist/doctor.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { RuntimeProbeEntry } from "./types.js";
|
|
2
|
-
export interface DoctorResult {
|
|
3
|
-
text: string;
|
|
4
|
-
availableCount: number;
|
|
5
|
-
totalCount: number;
|
|
6
|
-
}
|
|
7
|
-
export declare function renderRuntimeTable(entries: RuntimeProbeEntry[]): string[];
|
|
8
|
-
export interface CourseDoctorOptions {
|
|
9
|
-
includeHidden?: boolean;
|
|
10
|
-
/** 探测函数注入点(测试用);缺省走真实 registry probe。 */
|
|
11
|
-
detect?: (opts: {
|
|
12
|
-
includeHidden?: boolean;
|
|
13
|
-
}) => Promise<RuntimeProbeEntry[]>;
|
|
14
|
-
}
|
|
15
|
-
export declare function runCourseDoctor(opts?: CourseDoctorOptions): Promise<DoctorResult>;
|
package/dist/doctor.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { authFilePath, readAuth } from "./auth-store.js";
|
|
3
|
-
import { detectRuntimes, getRuntimeModule } from "./runtimes/index.js";
|
|
4
|
-
function pad(s, n) {
|
|
5
|
-
return s + " ".repeat(Math.max(0, n - s.length));
|
|
6
|
-
}
|
|
7
|
-
// token 绝不出现在 doctor 输出:Auth 区只显示非敏感身份字段。
|
|
8
|
-
function renderAuthSection() {
|
|
9
|
-
const lines = ["Auth"];
|
|
10
|
-
const file = authFilePath();
|
|
11
|
-
const auth = readAuth();
|
|
12
|
-
if (!auth) {
|
|
13
|
-
const state = existsSync(file) ? "unreadable" : "missing";
|
|
14
|
-
lines.push(` auth.json: ${state} (${file})`);
|
|
15
|
-
lines.push(" run: botlearn-course-daemon course login --api-url <url> --code <blic_xxx>");
|
|
16
|
-
return lines;
|
|
17
|
-
}
|
|
18
|
-
lines.push(` auth.json: present (${file})`);
|
|
19
|
-
lines.push(` courseApiUrl: ${auth.courseApiUrl}`);
|
|
20
|
-
lines.push(` daemonId: ${auth.daemonId}`);
|
|
21
|
-
lines.push(` label: ${auth.label || "—"}`);
|
|
22
|
-
return lines;
|
|
23
|
-
}
|
|
24
|
-
export function renderRuntimeTable(entries) {
|
|
25
|
-
const lines = [];
|
|
26
|
-
const rows = entries.map((e) => ({
|
|
27
|
-
runtime: e.id,
|
|
28
|
-
name: e.displayName,
|
|
29
|
-
status: e.result.available ? "ok" : "missing",
|
|
30
|
-
version: e.result.version ?? "—",
|
|
31
|
-
path: e.result.path ?? "—",
|
|
32
|
-
}));
|
|
33
|
-
const widths = {
|
|
34
|
-
runtime: Math.max(7, ...rows.map((r) => r.runtime.length)),
|
|
35
|
-
name: Math.max(4, ...rows.map((r) => r.name.length)),
|
|
36
|
-
status: Math.max(6, ...rows.map((r) => r.status.length)),
|
|
37
|
-
version: Math.max(7, ...rows.map((r) => r.version.length)),
|
|
38
|
-
};
|
|
39
|
-
lines.push(`${pad("RUNTIME", widths.runtime)} ${pad("NAME", widths.name)} ${pad("STATUS", widths.status)} ${pad("VERSION", widths.version)} PATH`);
|
|
40
|
-
for (let i = 0; i < rows.length; i += 1) {
|
|
41
|
-
const r = rows[i];
|
|
42
|
-
const e = entries[i];
|
|
43
|
-
lines.push(`${pad(r.runtime, widths.runtime)} ${pad(r.name, widths.name)} ${pad(r.status, widths.status)} ${pad(r.version, widths.version)} ${r.path}`);
|
|
44
|
-
if (!e.result.available && e.installHint) {
|
|
45
|
-
lines.push(` → ${e.installHint}`);
|
|
46
|
-
}
|
|
47
|
-
if (e.result.auth) {
|
|
48
|
-
const auth = e.result.auth;
|
|
49
|
-
const authStatus = auth.checked ? (auth.ok ? "ok" : "failed") : "skipped";
|
|
50
|
-
lines.push(` auth ${authStatus}: ${auth.message ?? ""}`.trimEnd());
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return lines;
|
|
54
|
-
}
|
|
55
|
-
export async function runCourseDoctor(opts = {}) {
|
|
56
|
-
const detect = opts.detect ?? detectRuntimes;
|
|
57
|
-
const entries = await detect({ includeHidden: opts.includeHidden });
|
|
58
|
-
// 登录态探测可能昂贵(真实调用一次 CLI),只在 doctor 里对可用 runtime 补跑;
|
|
59
|
-
// 注入的 entry 已带 auth 时保留,避免测试注入被覆盖。
|
|
60
|
-
for (const e of entries) {
|
|
61
|
-
if (!e.result.available || e.result.auth)
|
|
62
|
-
continue;
|
|
63
|
-
const probeAuth = getRuntimeModule(e.id)?.probeAuth;
|
|
64
|
-
if (!probeAuth)
|
|
65
|
-
continue;
|
|
66
|
-
try {
|
|
67
|
-
e.result.auth = await probeAuth();
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
e.result.auth = {
|
|
71
|
-
checked: true,
|
|
72
|
-
ok: false,
|
|
73
|
-
message: err instanceof Error ? err.message : String(err),
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const availableCount = entries.filter((e) => e.result.available).length;
|
|
78
|
-
const lines = [];
|
|
79
|
-
lines.push(...renderAuthSection());
|
|
80
|
-
lines.push("");
|
|
81
|
-
lines.push("Runtimes");
|
|
82
|
-
lines.push(...renderRuntimeTable(entries));
|
|
83
|
-
lines.push(`\n${availableCount}/${entries.length} runtimes available`);
|
|
84
|
-
return { text: lines.join("\n"), availableCount, totalCount: entries.length };
|
|
85
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { AppliedRunRuntimeProfile, RunStartPayload } from "./types.js";
|
|
2
|
-
export declare class RuntimeProfileApplyError extends Error {
|
|
3
|
-
readonly code = "profile_apply_failed";
|
|
4
|
-
constructor(message: string);
|
|
5
|
-
}
|
|
6
|
-
export declare function applyRunRuntimeProfile(payload: RunStartPayload, raw: unknown): AppliedRunRuntimeProfile;
|
|
7
|
-
export declare function cleanupRunRuntimeProfile(agentRunId: string): void;
|
|
8
|
-
export declare function runtimeProfileInstructions(payload: RunStartPayload, applied: AppliedRunRuntimeProfile): string[];
|