@downcity/agent 1.1.70 → 1.1.72
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/bin/executor/tools/shell/ShellToolFormatting.js +1 -1
- package/bin/executor/tools/shell/ShellToolFormatting.js.map +1 -1
- package/bin/index.d.ts +1 -4
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +1 -5
- package/bin/index.js.map +1 -1
- package/bin/rpc/Client.d.ts +20 -1
- package/bin/rpc/Client.d.ts.map +1 -1
- package/bin/rpc/Client.js +33 -0
- package/bin/rpc/Client.js.map +1 -1
- package/bin/rpc/Server.d.ts.map +1 -1
- package/bin/rpc/Server.js +31 -1
- package/bin/rpc/Server.js.map +1 -1
- package/bin/runtime/auth/AuthEnv.d.ts.map +1 -0
- package/bin/runtime/auth/AuthEnv.js.map +1 -0
- package/bin/runtime/auth/CliAuthStateStore.d.ts.map +1 -0
- package/bin/runtime/auth/CliAuthStateStore.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/AuthControlService.d.ts +3 -3
- package/bin/runtime/control/AuthControlService.d.ts.map +1 -0
- package/bin/runtime/{server/http/control → control}/AuthControlService.js +3 -3
- package/bin/runtime/control/AuthControlService.js.map +1 -0
- package/bin/runtime/control/CommonHelpers.d.ts.map +1 -0
- package/bin/runtime/control/CommonHelpers.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/ExecuteBySession.d.ts +4 -4
- package/bin/runtime/control/ExecuteBySession.d.ts.map +1 -0
- package/bin/runtime/control/ExecuteBySession.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/ExecuteInput.d.ts +1 -1
- package/bin/runtime/control/ExecuteInput.d.ts.map +1 -0
- package/bin/runtime/{server/http/control → control}/ExecuteInput.js +2 -2
- package/bin/runtime/control/ExecuteInput.js.map +1 -0
- package/bin/runtime/control/Helpers.d.ts.map +1 -0
- package/bin/runtime/control/Helpers.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/MessageTimeline.d.ts +2 -2
- package/bin/runtime/control/MessageTimeline.d.ts.map +1 -0
- package/bin/runtime/{server/http/control → control}/MessageTimeline.js +2 -2
- package/bin/runtime/control/MessageTimeline.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/SessionSummaryStore.d.ts +2 -2
- package/bin/runtime/control/SessionSummaryStore.d.ts.map +1 -0
- package/bin/runtime/{server/http/control → control}/SessionSummaryStore.js +1 -1
- package/bin/runtime/control/SessionSummaryStore.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/TaskStore.d.ts +1 -1
- package/bin/runtime/control/TaskStore.d.ts.map +1 -0
- package/bin/runtime/{server/http/control → control}/TaskStore.js +1 -1
- package/bin/runtime/control/TaskStore.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/types/AuthControl.d.ts +1 -1
- package/bin/runtime/control/types/AuthControl.d.ts.map +1 -0
- package/bin/runtime/control/types/AuthControl.js.map +1 -0
- package/bin/runtime/control/types/ControlSessionExecute.d.ts.map +1 -0
- package/bin/runtime/control/types/ControlSessionExecute.js.map +1 -0
- package/bin/runtime/{server/http/control → control}/types/ControlViewData.d.ts +1 -1
- package/bin/runtime/control/types/ControlViewData.d.ts.map +1 -0
- package/bin/runtime/control/types/ControlViewData.js.map +1 -0
- package/bin/runtime/host/ProjectSetup.d.ts.map +1 -0
- package/bin/runtime/host/{daemon/ProjectSetup.js → ProjectSetup.js} +1 -1
- package/bin/runtime/host/ProjectSetup.js.map +1 -0
- package/package.json +3 -3
- package/src/executor/tools/shell/ShellToolFormatting.ts +1 -1
- package/src/index.ts +1 -15
- package/src/rpc/Client.ts +67 -1
- package/src/rpc/Server.ts +64 -2
- package/src/runtime/{server/http/control → control}/AuthControlService.ts +4 -4
- package/src/runtime/{server/http/control → control}/ExecuteBySession.ts +1 -1
- package/src/runtime/{server/http/control → control}/ExecuteInput.ts +1 -1
- package/src/runtime/{server/http/control → control}/MessageTimeline.ts +1 -1
- package/src/runtime/{server/http/control → control}/SessionSummaryStore.ts +1 -1
- package/src/runtime/{server/http/control → control}/TaskStore.ts +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/runtime/host/daemon/Api.d.ts +0 -60
- package/bin/runtime/host/daemon/Api.d.ts.map +0 -1
- package/bin/runtime/host/daemon/Api.js +0 -10
- package/bin/runtime/host/daemon/Api.js.map +0 -1
- package/bin/runtime/host/daemon/Client.d.ts +0 -17
- package/bin/runtime/host/daemon/Client.d.ts.map +0 -1
- package/bin/runtime/host/daemon/Client.js +0 -168
- package/bin/runtime/host/daemon/Client.js.map +0 -1
- package/bin/runtime/host/daemon/Paths.d.ts +0 -20
- package/bin/runtime/host/daemon/Paths.d.ts.map +0 -1
- package/bin/runtime/host/daemon/Paths.js +0 -23
- package/bin/runtime/host/daemon/Paths.js.map +0 -1
- package/bin/runtime/host/daemon/ProjectSetup.d.ts.map +0 -1
- package/bin/runtime/host/daemon/ProjectSetup.js.map +0 -1
- package/bin/runtime/server/http/Server.d.ts +0 -48
- package/bin/runtime/server/http/Server.d.ts.map +0 -1
- package/bin/runtime/server/http/Server.js +0 -129
- package/bin/runtime/server/http/Server.js.map +0 -1
- package/bin/runtime/server/http/auth/AuthEnv.d.ts.map +0 -1
- package/bin/runtime/server/http/auth/AuthEnv.js.map +0 -1
- package/bin/runtime/server/http/auth/CliAuthStateStore.d.ts.map +0 -1
- package/bin/runtime/server/http/auth/CliAuthStateStore.js.map +0 -1
- package/bin/runtime/server/http/control/AuthControlService.d.ts.map +0 -1
- package/bin/runtime/server/http/control/AuthControlService.js.map +0 -1
- package/bin/runtime/server/http/control/CommonHelpers.d.ts.map +0 -1
- package/bin/runtime/server/http/control/CommonHelpers.js.map +0 -1
- package/bin/runtime/server/http/control/ControlApiRoutes.d.ts +0 -14
- package/bin/runtime/server/http/control/ControlApiRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/control/ControlApiRoutes.js +0 -25
- package/bin/runtime/server/http/control/ControlApiRoutes.js.map +0 -1
- package/bin/runtime/server/http/control/ControlAuthorizationRoutes.d.ts +0 -17
- package/bin/runtime/server/http/control/ControlAuthorizationRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/control/ControlAuthorizationRoutes.js +0 -84
- package/bin/runtime/server/http/control/ControlAuthorizationRoutes.js.map +0 -1
- package/bin/runtime/server/http/control/ControlRouter.d.ts +0 -35
- package/bin/runtime/server/http/control/ControlRouter.d.ts.map +0 -1
- package/bin/runtime/server/http/control/ControlRouter.js +0 -28
- package/bin/runtime/server/http/control/ControlRouter.js.map +0 -1
- package/bin/runtime/server/http/control/ExecuteBySession.d.ts.map +0 -1
- package/bin/runtime/server/http/control/ExecuteBySession.js.map +0 -1
- package/bin/runtime/server/http/control/ExecuteInput.d.ts.map +0 -1
- package/bin/runtime/server/http/control/ExecuteInput.js.map +0 -1
- package/bin/runtime/server/http/control/Helpers.d.ts.map +0 -1
- package/bin/runtime/server/http/control/Helpers.js.map +0 -1
- package/bin/runtime/server/http/control/MessageTimeline.d.ts.map +0 -1
- package/bin/runtime/server/http/control/MessageTimeline.js.map +0 -1
- package/bin/runtime/server/http/control/OverviewRoutes.d.ts +0 -13
- package/bin/runtime/server/http/control/OverviewRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/control/OverviewRoutes.js +0 -101
- package/bin/runtime/server/http/control/OverviewRoutes.js.map +0 -1
- package/bin/runtime/server/http/control/SessionRoutes.d.ts +0 -14
- package/bin/runtime/server/http/control/SessionRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/control/SessionRoutes.js +0 -334
- package/bin/runtime/server/http/control/SessionRoutes.js.map +0 -1
- package/bin/runtime/server/http/control/SessionSummaryStore.d.ts.map +0 -1
- package/bin/runtime/server/http/control/SessionSummaryStore.js.map +0 -1
- package/bin/runtime/server/http/control/TaskRoutes.d.ts +0 -13
- package/bin/runtime/server/http/control/TaskRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/control/TaskRoutes.js +0 -364
- package/bin/runtime/server/http/control/TaskRoutes.js.map +0 -1
- package/bin/runtime/server/http/control/TaskStore.d.ts.map +0 -1
- package/bin/runtime/server/http/control/TaskStore.js.map +0 -1
- package/bin/runtime/server/http/control/types/AuthControl.d.ts.map +0 -1
- package/bin/runtime/server/http/control/types/AuthControl.js.map +0 -1
- package/bin/runtime/server/http/control/types/ControlRoutes.d.ts +0 -28
- package/bin/runtime/server/http/control/types/ControlRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/control/types/ControlRoutes.js +0 -9
- package/bin/runtime/server/http/control/types/ControlRoutes.js.map +0 -1
- package/bin/runtime/server/http/control/types/ControlSessionExecute.d.ts.map +0 -1
- package/bin/runtime/server/http/control/types/ControlSessionExecute.js.map +0 -1
- package/bin/runtime/server/http/control/types/ControlViewData.d.ts.map +0 -1
- package/bin/runtime/server/http/control/types/ControlViewData.js.map +0 -1
- package/bin/runtime/server/http/execute/execute.d.ts +0 -25
- package/bin/runtime/server/http/execute/execute.d.ts.map +0 -1
- package/bin/runtime/server/http/execute/execute.js +0 -63
- package/bin/runtime/server/http/execute/execute.js.map +0 -1
- package/bin/runtime/server/http/health/health.d.ts +0 -14
- package/bin/runtime/server/http/health/health.d.ts.map +0 -1
- package/bin/runtime/server/http/health/health.js +0 -24
- package/bin/runtime/server/http/health/health.js.map +0 -1
- package/bin/runtime/server/http/plugins/plugins.d.ts +0 -25
- package/bin/runtime/server/http/plugins/plugins.d.ts.map +0 -1
- package/bin/runtime/server/http/plugins/plugins.js +0 -114
- package/bin/runtime/server/http/plugins/plugins.js.map +0 -1
- package/bin/runtime/server/http/sdk/Router.d.ts +0 -14
- package/bin/runtime/server/http/sdk/Router.d.ts.map +0 -1
- package/bin/runtime/server/http/sdk/Router.js +0 -18
- package/bin/runtime/server/http/sdk/Router.js.map +0 -1
- package/bin/runtime/server/http/sdk/SessionRoutes.d.ts +0 -15
- package/bin/runtime/server/http/sdk/SessionRoutes.d.ts.map +0 -1
- package/bin/runtime/server/http/sdk/SessionRoutes.js +0 -249
- package/bin/runtime/server/http/sdk/SessionRoutes.js.map +0 -1
- package/bin/runtime/server/http/static/static.d.ts +0 -25
- package/bin/runtime/server/http/static/static.d.ts.map +0 -1
- package/bin/runtime/server/http/static/static.js +0 -99
- package/bin/runtime/server/http/static/static.js.map +0 -1
- package/bin/types/runtime/daemon/Daemon.d.ts +0 -46
- package/bin/types/runtime/daemon/Daemon.d.ts.map +0 -1
- package/bin/types/runtime/daemon/Daemon.js +0 -14
- package/bin/types/runtime/daemon/Daemon.js.map +0 -1
- package/src/runtime/host/daemon/Api.ts +0 -64
- package/src/runtime/host/daemon/Client.ts +0 -204
- package/src/runtime/host/daemon/Paths.ts +0 -33
- package/src/runtime/server/http/Server.ts +0 -183
- package/src/runtime/server/http/control/ControlApiRoutes.ts +0 -29
- package/src/runtime/server/http/control/ControlAuthorizationRoutes.ts +0 -103
- package/src/runtime/server/http/control/ControlRouter.ts +0 -47
- package/src/runtime/server/http/control/OverviewRoutes.ts +0 -115
- package/src/runtime/server/http/control/SessionRoutes.ts +0 -427
- package/src/runtime/server/http/control/TaskRoutes.ts +0 -436
- package/src/runtime/server/http/control/types/ControlRoutes.ts +0 -31
- package/src/runtime/server/http/execute/execute.ts +0 -97
- package/src/runtime/server/http/health/health.ts +0 -27
- package/src/runtime/server/http/plugins/plugins.ts +0 -150
- package/src/runtime/server/http/sdk/Router.ts +0 -22
- package/src/runtime/server/http/sdk/SessionRoutes.ts +0 -300
- package/src/runtime/server/http/static/static.ts +0 -120
- package/src/types/runtime/daemon/Daemon.ts +0 -53
- /package/bin/runtime/{server/http/auth → auth}/AuthEnv.d.ts +0 -0
- /package/bin/runtime/{server/http/auth → auth}/AuthEnv.js +0 -0
- /package/bin/runtime/{server/http/auth → auth}/CliAuthStateStore.d.ts +0 -0
- /package/bin/runtime/{server/http/auth → auth}/CliAuthStateStore.js +0 -0
- /package/bin/runtime/{server/http/control → control}/CommonHelpers.d.ts +0 -0
- /package/bin/runtime/{server/http/control → control}/CommonHelpers.js +0 -0
- /package/bin/runtime/{server/http/control → control}/ExecuteBySession.js +0 -0
- /package/bin/runtime/{server/http/control → control}/Helpers.d.ts +0 -0
- /package/bin/runtime/{server/http/control → control}/Helpers.js +0 -0
- /package/bin/runtime/{server/http/control → control}/types/AuthControl.js +0 -0
- /package/bin/runtime/{server/http/control → control}/types/ControlSessionExecute.d.ts +0 -0
- /package/bin/runtime/{server/http/control → control}/types/ControlSessionExecute.js +0 -0
- /package/bin/runtime/{server/http/control → control}/types/ControlViewData.js +0 -0
- /package/bin/runtime/host/{daemon/ProjectSetup.d.ts → ProjectSetup.d.ts} +0 -0
- /package/src/runtime/{server/http/auth → auth}/AuthEnv.ts +0 -0
- /package/src/runtime/{server/http/auth → auth}/CliAuthStateStore.ts +0 -0
- /package/src/runtime/{server/http/control → control}/CommonHelpers.ts +0 -0
- /package/src/runtime/{server/http/control → control}/Helpers.ts +0 -0
- /package/src/runtime/{server/http/control → control}/types/AuthControl.ts +0 -0
- /package/src/runtime/{server/http/control → control}/types/ControlSessionExecute.ts +0 -0
- /package/src/runtime/{server/http/control → control}/types/ControlViewData.ts +0 -0
- /package/src/runtime/host/{daemon/ProjectSetup.ts → ProjectSetup.ts} +0 -0
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent Control API 路由入口模块。
|
|
3
|
-
*
|
|
4
|
-
* 职责说明:
|
|
5
|
-
* 1. 为单 agent control API 暴露独立 router。
|
|
6
|
-
* 2. 将控制域的 API 装配收敛在 `server/http/control/` 下。
|
|
7
|
-
* 3. server 只负责挂载该 router,不再持有控制面细节。
|
|
8
|
-
*
|
|
9
|
-
* 命名说明(中文)
|
|
10
|
-
* - 这里的 control 指“单 agent 控制域”。
|
|
11
|
-
* - 它不是 city 的 gateway / control plane。
|
|
12
|
-
* - 当前公开路径统一使用 `/api/control/*`。
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { Hono } from "hono";
|
|
16
|
-
import type { AgentRuntime } from "@/types/runtime/agent/AgentRuntime.js";
|
|
17
|
-
import type { AgentContext } from "@/types/runtime/agent/AgentContext.js";
|
|
18
|
-
import { registerControlApiRoutes } from "@/runtime/server/http/control/ControlApiRoutes.js";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* control router 参数。
|
|
22
|
-
*/
|
|
23
|
-
type ControlRouterOptions = {
|
|
24
|
-
/**
|
|
25
|
-
* 读取当前 agent runtime。
|
|
26
|
-
*/
|
|
27
|
-
getAgentRuntime: () => AgentRuntime;
|
|
28
|
-
/**
|
|
29
|
-
* 读取当前 agent 执行上下文。
|
|
30
|
-
*/
|
|
31
|
-
getAgentContext: () => AgentContext;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 创建单 agent control API 专用路由。
|
|
36
|
-
*/
|
|
37
|
-
export function createControlRouter(
|
|
38
|
-
options: ControlRouterOptions,
|
|
39
|
-
): Hono {
|
|
40
|
-
const router = new Hono();
|
|
41
|
-
registerControlApiRoutes({
|
|
42
|
-
app: router,
|
|
43
|
-
getAgentRuntime: options.getAgentRuntime,
|
|
44
|
-
getAgentContext: options.getAgentContext,
|
|
45
|
-
});
|
|
46
|
-
return router;
|
|
47
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Control 概览路由。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 聚合 overview 与 plugin 运行态两块轻量只读接口。
|
|
6
|
-
* - 只负责路由层拼装,不承载复杂业务状态机。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import fs from "fs-extra";
|
|
10
|
-
import { dirname, join } from "path";
|
|
11
|
-
import { fileURLToPath } from "url";
|
|
12
|
-
import { listPluginStates } from "@/plugin/core/Manager.js";
|
|
13
|
-
import { buildControlRouteAliases, toLimit } from "./CommonHelpers.js";
|
|
14
|
-
import { listControlSessionSummaries, readRecentLogs } from "./Helpers.js";
|
|
15
|
-
import type { ControlRouteRegistrationParams } from "@/runtime/server/http/control/types/ControlRoutes.js";
|
|
16
|
-
|
|
17
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
18
|
-
const __dirname = dirname(__filename);
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 当前 DC 版本号(用于 Overview 显示)。
|
|
22
|
-
*/
|
|
23
|
-
const DC_VERSION = (() => {
|
|
24
|
-
try {
|
|
25
|
-
const pkg = fs.readJsonSync(join(__dirname, "../../../package.json")) as {
|
|
26
|
-
version?: string;
|
|
27
|
-
};
|
|
28
|
-
const version = String(pkg?.version || "").trim();
|
|
29
|
-
return version || "unknown";
|
|
30
|
-
} catch {
|
|
31
|
-
return "unknown";
|
|
32
|
-
}
|
|
33
|
-
})();
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 注册概览与运行态 plugin 路由。
|
|
37
|
-
*/
|
|
38
|
-
export function registerControlOverviewRoutes(
|
|
39
|
-
params: ControlRouteRegistrationParams,
|
|
40
|
-
): void {
|
|
41
|
-
const { app } = params;
|
|
42
|
-
|
|
43
|
-
for (const routePath of buildControlRouteAliases("/overview")) {
|
|
44
|
-
app.get(routePath, async (c) => {
|
|
45
|
-
try {
|
|
46
|
-
const runtime = params.getAgentRuntime();
|
|
47
|
-
const sessionLimit = toLimit(
|
|
48
|
-
c.req.query("sessionLimit") || c.req.query("contextLimit"),
|
|
49
|
-
20,
|
|
50
|
-
);
|
|
51
|
-
const sessions = await listControlSessionSummaries({
|
|
52
|
-
projectRoot: runtime.rootPath,
|
|
53
|
-
agentId: runtime.paths.agentId,
|
|
54
|
-
executionContext: params.getAgentContext(),
|
|
55
|
-
limit: sessionLimit,
|
|
56
|
-
});
|
|
57
|
-
const runtimePlugins = listPluginStates({
|
|
58
|
-
context: params.getAgentContext(),
|
|
59
|
-
});
|
|
60
|
-
const taskResult = await params.getAgentContext().plugins.runAction({
|
|
61
|
-
plugin: "task",
|
|
62
|
-
action: "list",
|
|
63
|
-
});
|
|
64
|
-
const taskData =
|
|
65
|
-
taskResult.data && typeof taskResult.data === "object" && !Array.isArray(taskResult.data)
|
|
66
|
-
? taskResult.data as { tasks?: Array<{ status?: string }> }
|
|
67
|
-
: {};
|
|
68
|
-
const tasks = Array.isArray(taskData.tasks) ? taskData.tasks : [];
|
|
69
|
-
const logs = await readRecentLogs({
|
|
70
|
-
projectRoot: runtime.rootPath,
|
|
71
|
-
limit: 50,
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const statusCount = {
|
|
75
|
-
enabled: tasks.filter((x) => x.status === "enabled").length,
|
|
76
|
-
paused: tasks.filter((x) => x.status === "paused").length,
|
|
77
|
-
disabled: tasks.filter((x) => x.status === "disabled").length,
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
return c.json({
|
|
81
|
-
success: true,
|
|
82
|
-
cityVersion: DC_VERSION,
|
|
83
|
-
now: new Date().toISOString(),
|
|
84
|
-
agent: {
|
|
85
|
-
id: runtime.config.id,
|
|
86
|
-
status: "running",
|
|
87
|
-
},
|
|
88
|
-
sessions: {
|
|
89
|
-
total: sessions.length,
|
|
90
|
-
items: sessions,
|
|
91
|
-
},
|
|
92
|
-
plugins: runtimePlugins,
|
|
93
|
-
tasks: {
|
|
94
|
-
total: tasks.length,
|
|
95
|
-
statusCount,
|
|
96
|
-
},
|
|
97
|
-
logs,
|
|
98
|
-
});
|
|
99
|
-
} catch (error) {
|
|
100
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
for (const routePath of buildControlRouteAliases("/plugins/list")) {
|
|
106
|
-
app.get(routePath, (c) => {
|
|
107
|
-
return c.json({
|
|
108
|
-
success: true,
|
|
109
|
-
plugins: listPluginStates({
|
|
110
|
-
context: params.getAgentContext(),
|
|
111
|
-
}),
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 单 agent control API 会话路由。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 聚合控制面会话消息、归档、system prompt 与执行相关接口。
|
|
6
|
-
* - 仅负责编排请求与响应;消息读取、时间线映射、执行拼装复用 helper。
|
|
7
|
-
* - 会话控制接口统一暴露在 `/api/control/*` 下。
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { SystemModelMessage } from "ai";
|
|
11
|
-
import fs from "fs-extra";
|
|
12
|
-
import { dirname } from "path";
|
|
13
|
-
import { resolveSessionSystemMessages } from "@executor/composer/system/default/SystemDomain.js";
|
|
14
|
-
import {
|
|
15
|
-
getDowncityChatHistoryPath,
|
|
16
|
-
getDowncitySessionMessagesArchiveDirPath,
|
|
17
|
-
getDowncitySessionMessagesArchivePath,
|
|
18
|
-
getDowncitySessionMessagesPath,
|
|
19
|
-
} from "@/config/Paths.js";
|
|
20
|
-
import type { ControlSessionExecuteRequestBody } from "@/runtime/server/http/control/types/ControlSessionExecute.js";
|
|
21
|
-
import type { ControlRouteRegistrationParams } from "@/runtime/server/http/control/types/ControlRoutes.js";
|
|
22
|
-
import {
|
|
23
|
-
buildControlRouteAliases,
|
|
24
|
-
decodeMaybe,
|
|
25
|
-
toLimit,
|
|
26
|
-
} from "./CommonHelpers.js";
|
|
27
|
-
import {
|
|
28
|
-
listControlSessionSummaries,
|
|
29
|
-
loadSessionMessagesFromFile,
|
|
30
|
-
toUiMessageTimeline,
|
|
31
|
-
} from "./Helpers.js";
|
|
32
|
-
import { executeBySessionId } from "./ExecuteBySession.js";
|
|
33
|
-
const CONSOLEUI_SESSION_ID = "consoleui-chat-main";
|
|
34
|
-
|
|
35
|
-
function normalizeSystemText(input: string | null | undefined): string {
|
|
36
|
-
return String(input || "").trim();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function toSystemMessageText(message: SystemModelMessage): string {
|
|
40
|
-
const content = message.content as unknown;
|
|
41
|
-
if (typeof content === "string") return normalizeSystemText(content);
|
|
42
|
-
if (!Array.isArray(content)) return "";
|
|
43
|
-
const parts = content as Array<{ text?: unknown }>;
|
|
44
|
-
const texts: string[] = [];
|
|
45
|
-
for (const part of parts) {
|
|
46
|
-
if (!part || typeof part !== "object") continue;
|
|
47
|
-
const text = normalizeSystemText(String(part.text || ""));
|
|
48
|
-
if (!text) continue;
|
|
49
|
-
texts.push(text);
|
|
50
|
-
}
|
|
51
|
-
return texts.join("\n").trim();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 把 system messages 转成 control UI 可渲染结构。
|
|
56
|
-
*/
|
|
57
|
-
function toSystemPromptPayload(messages: SystemModelMessage[]): {
|
|
58
|
-
sections: Array<{
|
|
59
|
-
key: string;
|
|
60
|
-
title: string;
|
|
61
|
-
items: Array<{ index: number; content: string }>;
|
|
62
|
-
}>;
|
|
63
|
-
totalMessages: number;
|
|
64
|
-
totalChars: number;
|
|
65
|
-
} {
|
|
66
|
-
const items = messages
|
|
67
|
-
.map((message, index) => ({
|
|
68
|
-
index: index + 1,
|
|
69
|
-
content: toSystemMessageText(message),
|
|
70
|
-
}))
|
|
71
|
-
.filter((item) => item.content);
|
|
72
|
-
const totalChars = items.reduce(
|
|
73
|
-
(acc, item) => acc + String(item.content || "").length,
|
|
74
|
-
0,
|
|
75
|
-
);
|
|
76
|
-
return {
|
|
77
|
-
sections: [
|
|
78
|
-
{
|
|
79
|
-
key: "resolved",
|
|
80
|
-
title: "Resolved System Messages",
|
|
81
|
-
items,
|
|
82
|
-
},
|
|
83
|
-
],
|
|
84
|
-
totalMessages: items.length,
|
|
85
|
-
totalChars,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 注册上下文相关路由。
|
|
91
|
-
*/
|
|
92
|
-
export function registerControlSessionRoutes(
|
|
93
|
-
params: ControlRouteRegistrationParams,
|
|
94
|
-
): void {
|
|
95
|
-
const { app } = params;
|
|
96
|
-
|
|
97
|
-
for (const routePath of buildControlRouteAliases("/sessions")) {
|
|
98
|
-
app.get(routePath, async (c) => {
|
|
99
|
-
try {
|
|
100
|
-
const runtime = params.getAgentRuntime();
|
|
101
|
-
const limit = toLimit(c.req.query("limit"));
|
|
102
|
-
const executingSessionIds = new Set<string>(
|
|
103
|
-
runtime.listExecutingSessionIds(),
|
|
104
|
-
);
|
|
105
|
-
const sessions = await listControlSessionSummaries({
|
|
106
|
-
projectRoot: runtime.rootPath,
|
|
107
|
-
agentId: runtime.paths.agentId,
|
|
108
|
-
executionContext: params.getAgentContext(),
|
|
109
|
-
limit,
|
|
110
|
-
executingSessionIds,
|
|
111
|
-
});
|
|
112
|
-
const hasConsoleSession = sessions.some(
|
|
113
|
-
(item) => String(item.sessionId || "").trim() === CONSOLEUI_SESSION_ID,
|
|
114
|
-
);
|
|
115
|
-
const enrichedSessions = hasConsoleSession
|
|
116
|
-
? sessions
|
|
117
|
-
: [
|
|
118
|
-
{
|
|
119
|
-
sessionId: CONSOLEUI_SESSION_ID,
|
|
120
|
-
messageCount: 0,
|
|
121
|
-
updatedAt: Date.now(),
|
|
122
|
-
lastRole: "system" as const,
|
|
123
|
-
lastText: "consoleui channel",
|
|
124
|
-
channel: "consoleui",
|
|
125
|
-
...(executingSessionIds.has(CONSOLEUI_SESSION_ID) ? { executing: true } : {}),
|
|
126
|
-
},
|
|
127
|
-
...sessions,
|
|
128
|
-
];
|
|
129
|
-
return c.json({
|
|
130
|
-
success: true,
|
|
131
|
-
sessions: enrichedSessions,
|
|
132
|
-
});
|
|
133
|
-
} catch (error) {
|
|
134
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
for (const routePath of buildControlRouteAliases("/sessions/:sessionId/messages")) {
|
|
140
|
-
app.get(routePath, async (c) => {
|
|
141
|
-
try {
|
|
142
|
-
const runtime = params.getAgentRuntime();
|
|
143
|
-
const limit = toLimit(c.req.query("limit"), 200);
|
|
144
|
-
const sessionId = decodeMaybe(String(c.req.param("sessionId") || "").trim());
|
|
145
|
-
if (!sessionId) {
|
|
146
|
-
return c.json({ success: false, error: "Missing sessionId" }, 400);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const filePath = getDowncitySessionMessagesPath(
|
|
150
|
-
runtime.rootPath,
|
|
151
|
-
runtime.paths.agentId,
|
|
152
|
-
sessionId,
|
|
153
|
-
);
|
|
154
|
-
const messages = await loadSessionMessagesFromFile(filePath);
|
|
155
|
-
const sliced = messages
|
|
156
|
-
.slice(-limit)
|
|
157
|
-
.flatMap((message) => toUiMessageTimeline(message));
|
|
158
|
-
return c.json({
|
|
159
|
-
success: true,
|
|
160
|
-
sessionId,
|
|
161
|
-
total: sliced.length,
|
|
162
|
-
rawTotal: messages.length,
|
|
163
|
-
messages: sliced,
|
|
164
|
-
});
|
|
165
|
-
} catch (error) {
|
|
166
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
for (const routePath of buildControlRouteAliases("/sessions/:sessionId/messages")) {
|
|
172
|
-
app.delete(routePath, async (c) => {
|
|
173
|
-
try {
|
|
174
|
-
const runtime = params.getAgentRuntime();
|
|
175
|
-
const sessionId = decodeMaybe(String(c.req.param("sessionId") || "").trim());
|
|
176
|
-
if (!sessionId) {
|
|
177
|
-
return c.json({ success: false, error: "Missing sessionId" }, 400);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const messagesPath = getDowncitySessionMessagesPath(
|
|
181
|
-
runtime.rootPath,
|
|
182
|
-
runtime.paths.agentId,
|
|
183
|
-
sessionId,
|
|
184
|
-
);
|
|
185
|
-
const messagesDirPath = dirname(messagesPath);
|
|
186
|
-
await fs.remove(messagesDirPath);
|
|
187
|
-
// 关键点(中文):清理消息文件后,同步清掉内存中的 session runtime,避免旧上下文继续运行。
|
|
188
|
-
runtime.getSession(sessionId).clearExecutor();
|
|
189
|
-
|
|
190
|
-
return c.json({
|
|
191
|
-
success: true,
|
|
192
|
-
sessionId,
|
|
193
|
-
cleared: true,
|
|
194
|
-
});
|
|
195
|
-
} catch (error) {
|
|
196
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
for (const routePath of buildControlRouteAliases("/sessions/:sessionId/chat-history")) {
|
|
202
|
-
app.delete(routePath, async (c) => {
|
|
203
|
-
try {
|
|
204
|
-
const runtime = params.getAgentRuntime();
|
|
205
|
-
const sessionId = decodeMaybe(String(c.req.param("sessionId") || "").trim());
|
|
206
|
-
if (!sessionId) {
|
|
207
|
-
return c.json({ success: false, error: "Missing sessionId" }, 400);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const historyPath = getDowncityChatHistoryPath(runtime.rootPath, sessionId);
|
|
211
|
-
await fs.remove(historyPath);
|
|
212
|
-
|
|
213
|
-
return c.json({
|
|
214
|
-
success: true,
|
|
215
|
-
sessionId,
|
|
216
|
-
cleared: true,
|
|
217
|
-
});
|
|
218
|
-
} catch (error) {
|
|
219
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
for (const routePath of buildControlRouteAliases("/sessions/:sessionId/archives")) {
|
|
225
|
-
app.get(routePath, async (c) => {
|
|
226
|
-
try {
|
|
227
|
-
const runtime = params.getAgentRuntime();
|
|
228
|
-
const limit = toLimit(c.req.query("limit"), 100);
|
|
229
|
-
const sessionId = decodeMaybe(String(c.req.param("sessionId") || "").trim());
|
|
230
|
-
if (!sessionId) {
|
|
231
|
-
return c.json({ success: false, error: "Missing sessionId" }, 400);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const archiveDirPath = getDowncitySessionMessagesArchiveDirPath(
|
|
235
|
-
runtime.rootPath,
|
|
236
|
-
runtime.paths.agentId,
|
|
237
|
-
sessionId,
|
|
238
|
-
);
|
|
239
|
-
if (!(await fs.pathExists(archiveDirPath))) {
|
|
240
|
-
return c.json({
|
|
241
|
-
success: true,
|
|
242
|
-
sessionId,
|
|
243
|
-
archives: [],
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const entries = await fs.readdir(archiveDirPath, { withFileTypes: true });
|
|
248
|
-
const archives: Array<{
|
|
249
|
-
archiveId: string;
|
|
250
|
-
archivedAt?: number;
|
|
251
|
-
messageCount: number;
|
|
252
|
-
}> = [];
|
|
253
|
-
|
|
254
|
-
for (const entry of entries) {
|
|
255
|
-
if (!entry.isFile() || !entry.name.endsWith(".json")) continue;
|
|
256
|
-
const archiveId = decodeMaybe(entry.name.slice(0, -5));
|
|
257
|
-
if (!archiveId) continue;
|
|
258
|
-
|
|
259
|
-
const archivePath = getDowncitySessionMessagesArchivePath(
|
|
260
|
-
runtime.rootPath,
|
|
261
|
-
runtime.paths.agentId,
|
|
262
|
-
sessionId,
|
|
263
|
-
archiveId,
|
|
264
|
-
);
|
|
265
|
-
const payload = (await fs.readJson(archivePath).catch(() => null)) as
|
|
266
|
-
| {
|
|
267
|
-
archivedAt?: unknown;
|
|
268
|
-
messages?: unknown;
|
|
269
|
-
}
|
|
270
|
-
| null;
|
|
271
|
-
const archivedAtFromPayload =
|
|
272
|
-
typeof payload?.archivedAt === "number" &&
|
|
273
|
-
Number.isFinite(payload.archivedAt)
|
|
274
|
-
? payload.archivedAt
|
|
275
|
-
: undefined;
|
|
276
|
-
const archivedAtFromStat =
|
|
277
|
-
typeof archivedAtFromPayload === "number"
|
|
278
|
-
? undefined
|
|
279
|
-
: await fs
|
|
280
|
-
.stat(archivePath)
|
|
281
|
-
.then((stat) => stat.mtimeMs)
|
|
282
|
-
.catch(() => undefined);
|
|
283
|
-
const messageCount = Array.isArray(payload?.messages)
|
|
284
|
-
? payload.messages.length
|
|
285
|
-
: 0;
|
|
286
|
-
|
|
287
|
-
archives.push({
|
|
288
|
-
archiveId,
|
|
289
|
-
...(typeof archivedAtFromPayload === "number"
|
|
290
|
-
? { archivedAt: archivedAtFromPayload }
|
|
291
|
-
: typeof archivedAtFromStat === "number"
|
|
292
|
-
? { archivedAt: archivedAtFromStat }
|
|
293
|
-
: {}),
|
|
294
|
-
messageCount,
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
archives.sort((a, b) => (b.archivedAt || 0) - (a.archivedAt || 0));
|
|
299
|
-
|
|
300
|
-
return c.json({
|
|
301
|
-
success: true,
|
|
302
|
-
sessionId,
|
|
303
|
-
archives: archives.slice(0, limit),
|
|
304
|
-
});
|
|
305
|
-
} catch (error) {
|
|
306
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
for (const routePath of buildControlRouteAliases("/sessions/:sessionId/archives/:archiveId")) {
|
|
312
|
-
app.get(routePath, async (c) => {
|
|
313
|
-
try {
|
|
314
|
-
const runtime = params.getAgentRuntime();
|
|
315
|
-
const sessionId = decodeMaybe(String(c.req.param("sessionId") || "").trim());
|
|
316
|
-
const archiveId = decodeMaybe(String(c.req.param("archiveId") || "").trim());
|
|
317
|
-
if (!sessionId) {
|
|
318
|
-
return c.json({ success: false, error: "Missing sessionId" }, 400);
|
|
319
|
-
}
|
|
320
|
-
if (!archiveId) {
|
|
321
|
-
return c.json({ success: false, error: "Missing archiveId" }, 400);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const archivePath = getDowncitySessionMessagesArchivePath(
|
|
325
|
-
runtime.rootPath,
|
|
326
|
-
runtime.paths.agentId,
|
|
327
|
-
sessionId,
|
|
328
|
-
archiveId,
|
|
329
|
-
);
|
|
330
|
-
if (!(await fs.pathExists(archivePath))) {
|
|
331
|
-
return c.json(
|
|
332
|
-
{ success: false, error: `Archive not found: ${archiveId}` },
|
|
333
|
-
404,
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const payload = (await fs.readJson(archivePath).catch(() => null)) as
|
|
338
|
-
| {
|
|
339
|
-
archivedAt?: unknown;
|
|
340
|
-
messages?: unknown;
|
|
341
|
-
}
|
|
342
|
-
| null;
|
|
343
|
-
const archivedAt =
|
|
344
|
-
typeof payload?.archivedAt === "number" &&
|
|
345
|
-
Number.isFinite(payload.archivedAt)
|
|
346
|
-
? payload.archivedAt
|
|
347
|
-
: undefined;
|
|
348
|
-
const archivedMessages = Array.isArray(payload?.messages)
|
|
349
|
-
? payload.messages
|
|
350
|
-
: [];
|
|
351
|
-
const messages = archivedMessages.flatMap((message) =>
|
|
352
|
-
toUiMessageTimeline(message as Parameters<typeof toUiMessageTimeline>[0]),
|
|
353
|
-
);
|
|
354
|
-
|
|
355
|
-
return c.json({
|
|
356
|
-
success: true,
|
|
357
|
-
sessionId,
|
|
358
|
-
archiveId,
|
|
359
|
-
...(typeof archivedAt === "number" ? { archivedAt } : {}),
|
|
360
|
-
total: messages.length,
|
|
361
|
-
rawTotal: archivedMessages.length,
|
|
362
|
-
messages,
|
|
363
|
-
});
|
|
364
|
-
} catch (error) {
|
|
365
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
for (const routePath of buildControlRouteAliases("/system-prompt")) {
|
|
371
|
-
app.get(routePath, async (c) => {
|
|
372
|
-
try {
|
|
373
|
-
const runtime = params.getAgentRuntime();
|
|
374
|
-
const sessionId =
|
|
375
|
-
decodeMaybe(String(c.req.query("sessionId") || "").trim()) ||
|
|
376
|
-
CONSOLEUI_SESSION_ID;
|
|
377
|
-
const systemMessages = await resolveSessionSystemMessages({
|
|
378
|
-
projectRoot: runtime.rootPath,
|
|
379
|
-
sessionId,
|
|
380
|
-
profile: "chat",
|
|
381
|
-
staticSystemPrompts: runtime.systems,
|
|
382
|
-
context: params.getAgentContext(),
|
|
383
|
-
});
|
|
384
|
-
return c.json({
|
|
385
|
-
success: true,
|
|
386
|
-
sessionId,
|
|
387
|
-
...toSystemPromptPayload(systemMessages),
|
|
388
|
-
});
|
|
389
|
-
} catch (error) {
|
|
390
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
391
|
-
}
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
for (const routePath of buildControlRouteAliases("/sessions/:sessionId/execute")) {
|
|
396
|
-
app.post(routePath, async (c) => {
|
|
397
|
-
try {
|
|
398
|
-
const runtime = params.getAgentRuntime();
|
|
399
|
-
const sessionId = decodeMaybe(String(c.req.param("sessionId") || "").trim());
|
|
400
|
-
const body = (await c.req.json().catch(() => ({}))) as Partial<ControlSessionExecuteRequestBody>;
|
|
401
|
-
const instructions = String(body.instructions || "").trim();
|
|
402
|
-
if (!sessionId) {
|
|
403
|
-
return c.json({ success: false, error: "Missing sessionId" }, 400);
|
|
404
|
-
}
|
|
405
|
-
if (!instructions) {
|
|
406
|
-
return c.json({ success: false, error: "Missing instructions" }, 400);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
const result = await executeBySessionId({
|
|
410
|
-
agentState: runtime,
|
|
411
|
-
executionContext: params.getAgentContext(),
|
|
412
|
-
sessionId,
|
|
413
|
-
instructions,
|
|
414
|
-
attachments: Array.isArray(body.attachments) ? body.attachments : undefined,
|
|
415
|
-
});
|
|
416
|
-
return c.json({
|
|
417
|
-
success: true,
|
|
418
|
-
sessionId,
|
|
419
|
-
result,
|
|
420
|
-
});
|
|
421
|
-
} catch (error) {
|
|
422
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
}
|