@keyou007/agent-bot 0.1.25 → 0.1.27-alpha.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/CHANGELOG.md +10 -0
- package/README.md +12 -6
- package/README.zh.md +12 -6
- package/config.example.yaml +3 -2
- package/dist/cli/AutostartManager.js +1 -1
- package/dist/config/SettingsStore.js +10 -5
- package/dist/config/SettingsStore.js.map +1 -1
- package/dist/config/loadConfig.js +2 -1
- package/dist/config/loadConfig.js.map +1 -1
- package/dist/config/schema.js +5 -1
- package/dist/config/schema.js.map +1 -1
- package/dist/feishu/CardRenderer.js +5 -7
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/FeishuConnector.js +1 -1
- package/dist/feishu/FeishuConnector.js.map +1 -1
- package/dist/feishu/FeishuTurnPresenter.js +1 -1
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
- package/dist/feishu/SettingsCard.js +4 -3
- package/dist/feishu/SettingsCard.js.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/presentation/TurnStateReducer.js +1 -1
- package/dist/presentation/TurnStateReducer.js.map +1 -1
- package/dist/proxy/ProxySessionController.js +191 -12
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/state/StateStore.js +67 -3
- package/dist/state/StateStore.js.map +1 -1
- package/dist/terminal/TerminalCards.js +1 -1
- package/dist/terminal/TerminalPage.js +17 -14
- package/dist/terminal/TerminalPage.js.map +1 -1
- package/dist/terminal/TerminalServer.js +60 -66
- package/dist/terminal/TerminalServer.js.map +1 -1
- package/docs/technical-reference.md +14 -15
- package/docs/technical-reference.zh.md +14 -15
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +6 -3
- package/src/cli/AutostartManager.ts +1 -1
- package/src/config/SettingsStore.ts +12 -7
- package/src/config/loadConfig.ts +2 -1
- package/src/config/schema.ts +6 -1
- package/src/feishu/CardRenderer.ts +5 -7
- package/src/feishu/FeishuConnector.ts +1 -1
- package/src/feishu/FeishuTurnPresenter.ts +1 -1
- package/src/feishu/SettingsCard.ts +3 -2
- package/src/index.ts +3 -1
- package/src/presentation/TurnStateReducer.ts +1 -1
- package/src/proxy/ProxySessionController.ts +191 -12
- package/src/runtime/types.ts +1 -1
- package/src/state/ExecutionProvenanceStore.ts +1 -1
- package/src/state/StateStore.ts +71 -3
- package/src/terminal/TerminalCards.ts +1 -1
- package/src/terminal/TerminalPage.ts +17 -14
- package/src/terminal/TerminalServer.ts +54 -56
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash, randomBytes,
|
|
1
|
+
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
4
4
|
import type { Duplex } from "node:stream";
|
|
@@ -8,6 +8,7 @@ import path from "node:path";
|
|
|
8
8
|
import type { Logger } from "pino";
|
|
9
9
|
import { WebSocket, WebSocketServer } from "ws";
|
|
10
10
|
import { z } from "zod";
|
|
11
|
+
import { DEFAULT_TERMINAL_PASSWORD } from "../config/schema.js";
|
|
11
12
|
import type { CardAction } from "../feishu/types.js";
|
|
12
13
|
import { spawnTerminal, type TerminalProcess, type TerminalSpawner } from "./TerminalProcess.js";
|
|
13
14
|
import { terminalPage, TERMINAL_CSS, TERMINAL_SCRIPT } from "./TerminalPage.js";
|
|
@@ -30,11 +31,11 @@ const frame = z.discriminatedUnion("type", [
|
|
|
30
31
|
]);
|
|
31
32
|
|
|
32
33
|
export interface TerminalAuthorization {
|
|
33
|
-
requestId: string; approvalToken: string;
|
|
34
|
+
requestId: string; approvalToken: string; cwd: string; expiresAt: number;
|
|
34
35
|
}
|
|
35
36
|
export interface TerminalCardView extends TerminalAuthorization {
|
|
36
37
|
contextKey: string; commandCardMessageId: string; shellStarted: boolean;
|
|
37
|
-
terminalId: string; reopenToken: string; reopenUrl: string; recovery: boolean; state: "
|
|
38
|
+
terminalId: string; reopenToken: string; reopenUrl: string; recovery: boolean; state: "approved" | "running" | "closed";
|
|
38
39
|
connected: boolean; message?: string;
|
|
39
40
|
}
|
|
40
41
|
export interface ActiveTerminal {
|
|
@@ -45,6 +46,7 @@ export interface ActiveTerminal {
|
|
|
45
46
|
export interface TerminalServerOptions {
|
|
46
47
|
stateDirectory: string;
|
|
47
48
|
ownerId(): string | undefined;
|
|
49
|
+
password?(): string;
|
|
48
50
|
canStart?(): boolean;
|
|
49
51
|
readJob(jobId: string): Promise<{ cwd: string; contextKey: string; cardMessageId?: string }>;
|
|
50
52
|
requestApproval(authorization: TerminalCardView): Promise<string | undefined>;
|
|
@@ -56,9 +58,9 @@ export interface TerminalServerOptions {
|
|
|
56
58
|
interface TerminalSession extends TerminalAuthorization {
|
|
57
59
|
reopenToken: string;
|
|
58
60
|
contextKey: string; commandCardMessageId: string;
|
|
59
|
-
ownerId: string; keyHash: Buffer; createdAt: number; lastActivity: number;
|
|
61
|
+
ownerId: string; keyHash: Buffer; passwordHash: Buffer; createdAt: number; lastActivity: number;
|
|
60
62
|
activeAccessId: string;
|
|
61
|
-
state: "
|
|
63
|
+
state: "approved" | "running" | "closed";
|
|
62
64
|
message?: string; cardId?: string; pty?: TerminalProcess; socket?: WebSocket;
|
|
63
65
|
starting?: Promise<void>; output: string;
|
|
64
66
|
attached?: boolean;
|
|
@@ -69,7 +71,7 @@ interface TerminalSession extends TerminalAuthorization {
|
|
|
69
71
|
}
|
|
70
72
|
interface TerminalRecovery extends TerminalAuthorization {
|
|
71
73
|
targetId: string; ownerId: string; keyHash: Buffer; cardId?: string;
|
|
72
|
-
state: "
|
|
74
|
+
state: "approved" | "closed"; message?: string;
|
|
73
75
|
}
|
|
74
76
|
|
|
75
77
|
export class TerminalServer {
|
|
@@ -79,6 +81,7 @@ export class TerminalServer {
|
|
|
79
81
|
private readonly recoveries = new Map<string, TerminalRecovery>();
|
|
80
82
|
private cardUpdates = Promise.resolve();
|
|
81
83
|
private readonly requests: number[] = [];
|
|
84
|
+
private readonly passwordFailures: Array<{ address: string; at: number }> = [];
|
|
82
85
|
private timer?: NodeJS.Timeout;
|
|
83
86
|
private links?: TerminalLinks;
|
|
84
87
|
private base?: URL;
|
|
@@ -189,32 +192,7 @@ export class TerminalServer {
|
|
|
189
192
|
this.publishCards(session); await this.cardUpdates;
|
|
190
193
|
return recovery?.state === "closed" && session.state !== "closed" ? "恢复连接请求已取消,原终端不受影响。" : "终端已关闭。";
|
|
191
194
|
}
|
|
192
|
-
|
|
193
|
-
if (action.value.decision === "allow") {
|
|
194
|
-
if (recovery) {
|
|
195
|
-
if (!this.canReopen(session)) throw new Error("原终端已结束,不能恢复。");
|
|
196
|
-
if (session.state === "approved") {
|
|
197
|
-
this.assertCanStart();
|
|
198
|
-
session.expiresAt = this.now() + AUTH_TTL;
|
|
199
|
-
}
|
|
200
|
-
for (const other of this.recoveries.values()) {
|
|
201
|
-
if (other.targetId === session.requestId && other !== recovery && other.state !== "closed") this.closeRecovery(other, "已由另一网页恢复连接");
|
|
202
|
-
}
|
|
203
|
-
recovery.state = "approved"; session.activeAccessId = recovery.requestId;
|
|
204
|
-
session.socket?.close(1000, "Reconnected from another page"); session.socket = undefined;
|
|
205
|
-
this.markDisconnected(session);
|
|
206
|
-
} else session.state = "approved";
|
|
207
|
-
this.publishCards(session); await this.cardUpdates;
|
|
208
|
-
this.options.logger.info({ requestId: session.requestId }, "Terminal authorized by owner.");
|
|
209
|
-
return session.state === "approved"
|
|
210
|
-
? "已授权,请返回网页连接;若原网页已关闭,点击本卡片的“打开终端”。"
|
|
211
|
-
: "已授权,请返回网页;也可点击本卡片的“重新打开终端”或“关闭终端”。";
|
|
212
|
-
}
|
|
213
|
-
if (action.value.decision !== "deny") throw new Error("无效的终端授权操作。");
|
|
214
|
-
if (recovery) this.closeRecovery(recovery, "恢复连接已拒绝,原终端不受影响");
|
|
215
|
-
else this.disposeSession(session, "终端授权已拒绝");
|
|
216
|
-
this.publishCards(session); await this.cardUpdates;
|
|
217
|
-
return "已拒绝终端连接。";
|
|
195
|
+
throw new Error("请在网页中输入终端密码连接;此卡片仅用于管理终端。");
|
|
218
196
|
}
|
|
219
197
|
|
|
220
198
|
async close(): Promise<void> {
|
|
@@ -264,8 +242,8 @@ export class TerminalServer {
|
|
|
264
242
|
}
|
|
265
243
|
private cardView(session: TerminalSession, recovery?: TerminalRecovery): TerminalCardView {
|
|
266
244
|
const auth = recovery ?? session;
|
|
267
|
-
const state = recovery
|
|
268
|
-
return { requestId: auth.requestId, approvalToken: auth.approvalToken,
|
|
245
|
+
const state = recovery?.state === "closed" ? "closed" : session.state;
|
|
246
|
+
return { requestId: auth.requestId, approvalToken: auth.approvalToken, cwd: session.cwd,
|
|
269
247
|
contextKey: session.contextKey, commandCardMessageId: session.commandCardMessageId, shellStarted: Boolean(session.pty),
|
|
270
248
|
expiresAt: auth.expiresAt, terminalId: session.requestId, reopenToken: session.reopenToken, reopenUrl: this.reopenUrl(session), recovery: Boolean(recovery), state,
|
|
271
249
|
connected: session.socket?.readyState === WebSocket.OPEN, message: recovery?.message ?? session.message };
|
|
@@ -303,7 +281,7 @@ export class TerminalServer {
|
|
|
303
281
|
}
|
|
304
282
|
private authenticated(value: unknown): TerminalSession {
|
|
305
283
|
const { authorization, session } = this.access(value);
|
|
306
|
-
if (authorization.state === "
|
|
284
|
+
if (authorization.state === "closed" || session.activeAccessId !== authorization.requestId) {
|
|
307
285
|
throw new Error("终端尚未授权、已结束或已由其他网页恢复。");
|
|
308
286
|
}
|
|
309
287
|
return session;
|
|
@@ -335,7 +313,20 @@ export class TerminalServer {
|
|
|
335
313
|
}
|
|
336
314
|
const data = await readJson(req);
|
|
337
315
|
if (route === "/api/request") {
|
|
338
|
-
const
|
|
316
|
+
const result = z.object({ launch: z.string().max(300), browserKey: KEY, password: z.string().max(256).optional() }).safeParse(data);
|
|
317
|
+
if (!result.success) { this.json(res, 400, { error: "终端连接参数无效。" }); return; }
|
|
318
|
+
const parsed = result.data;
|
|
319
|
+
const now = this.now();
|
|
320
|
+
const address = req.socket.remoteAddress ?? "unknown";
|
|
321
|
+
while (this.passwordFailures[0] && this.passwordFailures[0].at <= now - 60_000) this.passwordFailures.shift();
|
|
322
|
+
if (this.passwordFailures.length >= 50 || this.passwordFailures.filter((failure) => failure.address === address).length >= 5) {
|
|
323
|
+
this.json(res, 429, { error: "密码尝试过于频繁,请一分钟后重试。" }); return;
|
|
324
|
+
}
|
|
325
|
+
const passwordHash = hash(this.options.password?.() ?? DEFAULT_TERMINAL_PASSWORD);
|
|
326
|
+
if (parsed.password === undefined || !timingSafeEqual(hash(parsed.password), passwordHash)) {
|
|
327
|
+
this.passwordFailures.push({ address, at: now });
|
|
328
|
+
this.json(res, 401, { error: "终端密码错误,请重新输入。" }); return;
|
|
329
|
+
}
|
|
339
330
|
const resumeId = this.resumeId(parsed.launch);
|
|
340
331
|
const jobId = this.launchJob(parsed.launch);
|
|
341
332
|
const ownerId = this.options.ownerId();
|
|
@@ -345,41 +336,51 @@ export class TerminalServer {
|
|
|
345
336
|
throw new Error("原终端尚未授权、已结束或服务已重启;请从原命令卡片创建新终端。");
|
|
346
337
|
}
|
|
347
338
|
if (!existing || existing.state === "approved") this.assertCanStart();
|
|
348
|
-
const now = this.now();
|
|
349
339
|
while (this.requests[0] !== undefined && this.requests[0] < now - 3600_000) this.requests.shift();
|
|
350
340
|
if (this.requests.length >= 20 || (this.requests.length > 0 && now - this.requests.at(-1)! < 10_000)
|
|
351
|
-
|| (existing
|
|
352
|
-
: [...this.sessions.values()].filter((s) => s.state !== "closed").length >= 3)) {
|
|
341
|
+
|| (!existing && [...this.sessions.values()].filter((s) => s.state !== "closed").length >= 3)) {
|
|
353
342
|
this.json(res, 429, { error: "终端请求过于频繁或已达 3 个会话上限,请稍后重试。" }); return;
|
|
354
343
|
}
|
|
355
344
|
this.requests.push(now);
|
|
356
345
|
if (existing) {
|
|
357
346
|
const recovery: TerminalRecovery = { requestId: randomBytes(16).toString("hex"), targetId: existing.requestId, ownerId,
|
|
358
|
-
keyHash: hash(parsed.browserKey), approvalToken: randomBytes(32).toString("hex"),
|
|
359
|
-
cwd: existing.cwd, expiresAt: now + AUTH_TTL, state: "
|
|
347
|
+
keyHash: hash(parsed.browserKey), approvalToken: randomBytes(32).toString("hex"),
|
|
348
|
+
cwd: existing.cwd, expiresAt: now + AUTH_TTL, state: "approved" };
|
|
360
349
|
this.recoveries.set(recovery.requestId, recovery);
|
|
361
350
|
try {
|
|
362
351
|
recovery.cardId = await this.options.requestApproval(this.cardView(existing, recovery));
|
|
363
|
-
if (!recovery.cardId) throw new Error("
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
352
|
+
if (!recovery.cardId) throw new Error("无法发送终端管理卡片。");
|
|
353
|
+
if (!this.canReopen(existing) || existing.ownerId !== this.options.ownerId()
|
|
354
|
+
|| !timingSafeEqual(passwordHash, hash(this.options.password?.() ?? DEFAULT_TERMINAL_PASSWORD))) {
|
|
355
|
+
throw new Error("终端已结束或连接设置已变化,请重新连接。");
|
|
356
|
+
}
|
|
357
|
+
if (existing.state === "approved") { this.assertCanStart(); existing.expiresAt = this.now() + AUTH_TTL; }
|
|
358
|
+
} catch (error) { this.closeRecovery(recovery, "恢复连接未完成,原终端不受影响"); this.publishCards(existing); throw error; }
|
|
359
|
+
for (const other of this.recoveries.values()) {
|
|
360
|
+
if (other !== recovery && other.targetId === existing.requestId && other.state !== "closed") this.closeRecovery(other, "已由另一网页恢复连接");
|
|
361
|
+
}
|
|
362
|
+
existing.activeAccessId = recovery.requestId;
|
|
363
|
+
existing.socket?.close(1000, "Reconnected from another page"); existing.socket = undefined;
|
|
364
|
+
this.markDisconnected(existing);
|
|
365
|
+
this.publishCards(existing);
|
|
366
|
+
this.json(res, 200, { requestId: recovery.requestId }); return;
|
|
367
367
|
}
|
|
368
368
|
const job = await this.options.readJob(jobId!);
|
|
369
369
|
this.assertCanStart();
|
|
370
370
|
if (!job.cardMessageId || !job.contextKey?.trim()) throw new Error("命令卡片或原始会话信息尚未就绪。");
|
|
371
371
|
const requestId = randomBytes(16).toString("hex");
|
|
372
372
|
const session: TerminalSession = { requestId, reopenToken: this.links!.createRecovery(), activeAccessId: requestId, ownerId, cwd: job.cwd, keyHash: hash(parsed.browserKey),
|
|
373
|
+
passwordHash,
|
|
373
374
|
contextKey: job.contextKey, commandCardMessageId: job.cardMessageId,
|
|
374
|
-
approvalToken: randomBytes(32).toString("hex"),
|
|
375
|
-
createdAt: now, lastActivity: now, expiresAt: now + AUTH_TTL, state: "
|
|
375
|
+
approvalToken: randomBytes(32).toString("hex"),
|
|
376
|
+
createdAt: now, lastActivity: now, expiresAt: now + AUTH_TTL, state: "approved", output: "" };
|
|
376
377
|
this.sessions.set(requestId, session);
|
|
377
378
|
try {
|
|
378
379
|
session.cardId = await this.options.requestApproval(this.cardView(session));
|
|
379
|
-
if (!session.cardId) throw new Error("
|
|
380
|
-
|
|
381
|
-
} catch (error) { this.disposeSession(session, "
|
|
382
|
-
this.json(res, 200, { requestId
|
|
380
|
+
if (!session.cardId) throw new Error("无法发送终端管理卡片。");
|
|
381
|
+
this.publishCards(session);
|
|
382
|
+
} catch (error) { this.disposeSession(session, "终端管理卡发送失败"); throw error; }
|
|
383
|
+
this.json(res, 200, { requestId }); return;
|
|
383
384
|
}
|
|
384
385
|
if (route !== "/api/status" && route !== "/api/close") { this.json(res, 404, { error: "Not found." }); return; }
|
|
385
386
|
const { authorization, session } = this.access(data);
|
|
@@ -490,14 +491,11 @@ export class TerminalServer {
|
|
|
490
491
|
const now = this.now();
|
|
491
492
|
for (const recovery of this.recoveries.values()) {
|
|
492
493
|
const target = this.sessions.get(recovery.targetId);
|
|
493
|
-
if (recovery.state === "pending" && now >= recovery.expiresAt) {
|
|
494
|
-
this.closeRecovery(recovery, "恢复授权已过期,原终端不受影响");
|
|
495
|
-
if (target) this.publishCards(target);
|
|
496
|
-
}
|
|
497
494
|
if (!target || (recovery.state === "closed" && now >= recovery.expiresAt + AUTH_TTL)) this.recoveries.delete(recovery.requestId);
|
|
498
495
|
}
|
|
499
496
|
for (const s of this.sessions.values()) {
|
|
500
|
-
if (s.ownerId !== this.options.ownerId() ||
|
|
497
|
+
if (s.ownerId !== this.options.ownerId() || !timingSafeEqual(s.passwordHash, hash(this.options.password?.() ?? DEFAULT_TERMINAL_PASSWORD))
|
|
498
|
+
|| now - s.createdAt >= MAX_TTL
|
|
501
499
|
|| (s.state !== "running" && s.state !== "closed" && now >= s.expiresAt)) {
|
|
502
500
|
this.disposeSession(s, "终端已超时或授权已失效,请重新连接");
|
|
503
501
|
}
|