@xgjktech/xg_cwork_im 1.18.8 → 1.19.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/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -15
- package/dist/index.js.map +1 -1
- package/dist/src/action-store-lifecycle.test.js +0 -2
- package/dist/src/action-store-lifecycle.test.js.map +1 -1
- package/dist/src/block-streaming-config.test.js.map +1 -1
- package/dist/src/dispatch-mentioned-reply.d.ts.map +1 -1
- package/dist/src/dispatch-mentioned-reply.js +38 -6
- package/dist/src/dispatch-mentioned-reply.js.map +1 -1
- package/dist/src/plan-core-adapter.d.ts +46 -0
- package/dist/src/plan-core-adapter.d.ts.map +1 -0
- package/dist/src/plan-core-adapter.js +103 -0
- package/dist/src/plan-core-adapter.js.map +1 -0
- package/dist/src/plan-core-adapter.test.d.ts +2 -0
- package/dist/src/plan-core-adapter.test.d.ts.map +1 -0
- package/dist/src/plan-core-adapter.test.js +164 -0
- package/dist/src/plan-core-adapter.test.js.map +1 -0
- package/dist/src/plan-core-db.d.ts +22 -0
- package/dist/src/plan-core-db.d.ts.map +1 -0
- package/dist/src/plan-core-db.js +91 -0
- package/dist/src/plan-core-db.js.map +1 -0
- package/dist/src/plan-core-db.test.d.ts +2 -0
- package/dist/src/plan-core-db.test.d.ts.map +1 -0
- package/dist/src/plan-core-db.test.js +115 -0
- package/dist/src/plan-core-db.test.js.map +1 -0
- package/dist/src/plan-core-snapshot.d.ts +23 -0
- package/dist/src/plan-core-snapshot.d.ts.map +1 -0
- package/dist/src/plan-core-snapshot.js +65 -0
- package/dist/src/plan-core-snapshot.js.map +1 -0
- package/dist/src/plan-core-snapshot.test.d.ts +2 -0
- package/dist/src/plan-core-snapshot.test.d.ts.map +1 -0
- package/dist/src/plan-core-snapshot.test.js +155 -0
- package/dist/src/plan-core-snapshot.test.js.map +1 -0
- package/dist/src/plan-stat-event.d.ts +4 -19
- package/dist/src/plan-stat-event.d.ts.map +1 -1
- package/dist/src/plan-stat-event.js +4 -88
- package/dist/src/plan-stat-event.js.map +1 -1
- package/dist/src/plan-stat-event.test.js +1 -245
- package/dist/src/plan-stat-event.test.js.map +1 -1
- package/dist/src/plan-tool-middleware.d.ts +2 -10
- package/dist/src/plan-tool-middleware.d.ts.map +1 -1
- package/dist/src/plan-tool-middleware.js +11 -60
- package/dist/src/plan-tool-middleware.js.map +1 -1
- package/dist/src/plan-tool-middleware.test.js +83 -603
- package/dist/src/plan-tool-middleware.test.js.map +1 -1
- package/dist/src/plugin-dispose.d.ts +1 -1
- package/dist/src/plugin-dispose.d.ts.map +1 -1
- package/dist/src/plugin-dispose.js +3 -5
- package/dist/src/plugin-dispose.js.map +1 -1
- package/dist/src/turn-agent-event-subscription.d.ts +1 -1
- package/dist/src/turn-agent-event-subscription.js +1 -1
- package/package.json +9 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-adapter.test.d.ts","sourceRoot":"","sources":["../../src/plan-core-adapter.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it, beforeEach } from "node:test";
|
|
3
|
+
import { buildCorePlanId, buildPlanFromCoreUpdate, handleCorePlanUpdate, normalizeCorePlanSteps, resetCorePlanFirstSeenForTest, } from "./plan-core-adapter.js";
|
|
4
|
+
import { resetPlanWsRegistryForTest, setPlanWsClient } from "./plan-ws-registry.js";
|
|
5
|
+
const NOW = Date.parse("2026-09-08T00:00:00.000Z");
|
|
6
|
+
function makeRecordingClient() {
|
|
7
|
+
const calls = [];
|
|
8
|
+
const client = {
|
|
9
|
+
start: async () => { },
|
|
10
|
+
streamProgress: async () => { },
|
|
11
|
+
streamUsage: async () => { },
|
|
12
|
+
sendEvent: async (opts) => {
|
|
13
|
+
calls.push(opts);
|
|
14
|
+
},
|
|
15
|
+
end: async () => { },
|
|
16
|
+
};
|
|
17
|
+
return { client, calls };
|
|
18
|
+
}
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
resetPlanWsRegistryForTest();
|
|
21
|
+
resetCorePlanFirstSeenForTest();
|
|
22
|
+
});
|
|
23
|
+
describe("buildCorePlanId", () => {
|
|
24
|
+
it("对同一 sessionKey 稳定、不同 sessionKey 不同,且带 core- 前缀", () => {
|
|
25
|
+
const a1 = buildCorePlanId("sess-1");
|
|
26
|
+
const a2 = buildCorePlanId("sess-1");
|
|
27
|
+
const b = buildCorePlanId("sess-2");
|
|
28
|
+
assert.match(a1, /^core-/);
|
|
29
|
+
assert.equal(a1, a2);
|
|
30
|
+
assert.notEqual(a1, b);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe("normalizeCorePlanSteps", () => {
|
|
34
|
+
it("typed 步骤透传 step/status", () => {
|
|
35
|
+
assert.deepEqual(normalizeCorePlanSteps([
|
|
36
|
+
{ step: " 做 A ", status: "in_progress" },
|
|
37
|
+
{ step: "做 B", status: "completed" },
|
|
38
|
+
]), [
|
|
39
|
+
{ step: "做 A", status: "in_progress" },
|
|
40
|
+
{ step: "做 B", status: "completed" },
|
|
41
|
+
]);
|
|
42
|
+
});
|
|
43
|
+
it("旧版 string 步骤归一为 pending", () => {
|
|
44
|
+
assert.deepEqual(normalizeCorePlanSteps(["做 A", " "]), [{ step: "做 A", status: "pending" }]);
|
|
45
|
+
});
|
|
46
|
+
it("非法 status / 空文本丢弃,非法 status 回退 pending", () => {
|
|
47
|
+
assert.deepEqual(normalizeCorePlanSteps([
|
|
48
|
+
{ step: "ok", status: "blocked" },
|
|
49
|
+
{ step: "", status: "pending" },
|
|
50
|
+
"valid",
|
|
51
|
+
null,
|
|
52
|
+
]), [
|
|
53
|
+
{ step: "ok", status: "pending" },
|
|
54
|
+
{ step: "valid", status: "pending" },
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
it("undefined / 非数组 → 空", () => {
|
|
58
|
+
assert.deepEqual(normalizeCorePlanSteps(undefined), []);
|
|
59
|
+
assert.deepEqual(normalizeCorePlanSteps("x"), []);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("buildPlanFromCoreUpdate", () => {
|
|
63
|
+
it("steps 映射为 tasks,origin 为内部路由字段", () => {
|
|
64
|
+
const plan = buildPlanFromCoreUpdate({
|
|
65
|
+
sessionKey: "sess-1",
|
|
66
|
+
groupId: "g-1",
|
|
67
|
+
payload: {
|
|
68
|
+
phase: "update",
|
|
69
|
+
title: "Plan updated",
|
|
70
|
+
explanation: "迁移配置",
|
|
71
|
+
steps: [
|
|
72
|
+
{ step: "备份", status: "completed" },
|
|
73
|
+
{ step: "校验", status: "in_progress" },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
nowMs: NOW,
|
|
77
|
+
});
|
|
78
|
+
assert.equal(plan.planId, buildCorePlanId("sess-1"));
|
|
79
|
+
assert.equal(plan.goal, "迁移配置");
|
|
80
|
+
assert.equal(plan.updatedAt, new Date(NOW).toISOString());
|
|
81
|
+
assert.deepEqual(plan.tasks, [
|
|
82
|
+
{ title: "备份", status: "completed" },
|
|
83
|
+
{ title: "校验", status: "in_progress" },
|
|
84
|
+
]);
|
|
85
|
+
assert.deepEqual(plan.origin, {
|
|
86
|
+
channel: "xg_cwork_im",
|
|
87
|
+
to: "g-1",
|
|
88
|
+
sessionKey: "sess-1",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
it("completion 计数 explanation 回退固定 goal", () => {
|
|
92
|
+
const plan = buildPlanFromCoreUpdate({
|
|
93
|
+
sessionKey: "sess-1",
|
|
94
|
+
groupId: "g-1",
|
|
95
|
+
payload: { steps: [{ step: "A", status: "completed" }], explanation: "2/5 complete" },
|
|
96
|
+
nowMs: NOW,
|
|
97
|
+
});
|
|
98
|
+
assert.equal(plan.goal, "任务执行计划");
|
|
99
|
+
});
|
|
100
|
+
it("createdAtIso 覆盖进程内 firstSeen", () => {
|
|
101
|
+
const first = buildPlanFromCoreUpdate({
|
|
102
|
+
sessionKey: "sess-1",
|
|
103
|
+
groupId: "g-1",
|
|
104
|
+
payload: { steps: [{ step: "A", status: "pending" }] },
|
|
105
|
+
nowMs: NOW,
|
|
106
|
+
});
|
|
107
|
+
const second = buildPlanFromCoreUpdate({
|
|
108
|
+
sessionKey: "sess-1",
|
|
109
|
+
groupId: "g-1",
|
|
110
|
+
payload: { steps: [{ step: "A", status: "completed" }] },
|
|
111
|
+
nowMs: NOW + 60_000,
|
|
112
|
+
});
|
|
113
|
+
assert.equal(second.createdAt, first.createdAt, "同会话 createdAt 保持首次");
|
|
114
|
+
const fromDb = buildPlanFromCoreUpdate({
|
|
115
|
+
sessionKey: "sess-db",
|
|
116
|
+
groupId: "g-1",
|
|
117
|
+
payload: { steps: [{ step: "A", status: "pending" }] },
|
|
118
|
+
nowMs: NOW,
|
|
119
|
+
createdAtIso: "2026-09-01T00:00:00.000Z",
|
|
120
|
+
});
|
|
121
|
+
assert.equal(fromDb.createdAt, "2026-09-01T00:00:00.000Z");
|
|
122
|
+
});
|
|
123
|
+
it("空 steps → undefined(不产计划)", () => {
|
|
124
|
+
assert.equal(buildPlanFromCoreUpdate({
|
|
125
|
+
sessionKey: "sess-1",
|
|
126
|
+
groupId: "g-1",
|
|
127
|
+
payload: { steps: [] },
|
|
128
|
+
nowMs: NOW,
|
|
129
|
+
}), undefined);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
describe("handleCorePlanUpdate", () => {
|
|
133
|
+
it("有 steps 且 WS 已登记 → 发一条 source=tool 的 PLAN_STAT", () => {
|
|
134
|
+
const { client, calls } = makeRecordingClient();
|
|
135
|
+
setPlanWsClient("g-1", client, "msg-1");
|
|
136
|
+
handleCorePlanUpdate({
|
|
137
|
+
sessionKey: "sess-1",
|
|
138
|
+
groupId: "g-1",
|
|
139
|
+
payload: { steps: [{ step: "备份", status: "pending" }] },
|
|
140
|
+
});
|
|
141
|
+
assert.equal(calls.length, 1);
|
|
142
|
+
assert.equal(calls[0].event, "PLAN_STAT");
|
|
143
|
+
assert.equal(calls[0].data.source, "tool");
|
|
144
|
+
assert.equal(calls[0].data.plan.planId, buildCorePlanId("sess-1"));
|
|
145
|
+
});
|
|
146
|
+
it("空 steps → 不发送、不外抛", () => {
|
|
147
|
+
const { client, calls } = makeRecordingClient();
|
|
148
|
+
setPlanWsClient("g-1", client, "msg-1");
|
|
149
|
+
assert.doesNotThrow(() => handleCorePlanUpdate({
|
|
150
|
+
sessionKey: "sess-1",
|
|
151
|
+
groupId: "g-1",
|
|
152
|
+
payload: { steps: [] },
|
|
153
|
+
}));
|
|
154
|
+
assert.equal(calls.length, 0);
|
|
155
|
+
});
|
|
156
|
+
it("未登记 WS client → 不外抛", () => {
|
|
157
|
+
assert.doesNotThrow(() => handleCorePlanUpdate({
|
|
158
|
+
sessionKey: "sess-1",
|
|
159
|
+
groupId: "g-nowhere",
|
|
160
|
+
payload: { steps: [{ step: "A", status: "pending" }] },
|
|
161
|
+
}));
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
//# sourceMappingURL=plan-core-adapter.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-adapter.test.js","sourceRoot":"","sources":["../../src/plan-core-adapter.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAGrD,OAAO,EACH,eAAe,EACf,uBAAuB,EACvB,oBAAoB,EACpB,sBAAsB,EACtB,6BAA6B,GAChC,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,0BAA0B,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAEpF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC;AAEnD,SAAS,mBAAmB;IACxB,MAAM,KAAK,GAA4B,EAAE,CAAC;IAC1C,MAAM,MAAM,GAAmB;QAC3B,KAAK,EA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AE,QAAQ;YACpB,OAAO,EAAE,KAAK;YACd,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE;SACzB,CAAC,CACL,CAAC;QACF,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qBAAqB,EAAE,GAAG,EAAE;QAC3B,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,CACrB,oBAAoB,CAAC;YACjB,UAAU,EAAE,QAAQ;YACpB,OAAO,EAAE,WAAW;YACpB,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE;SACzD,CAAC,CACL,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type StoredCoreProgressCard = {
|
|
2
|
+
steps: Array<{
|
|
3
|
+
step: string;
|
|
4
|
+
status: "pending" | "in_progress" | "completed";
|
|
5
|
+
}>;
|
|
6
|
+
markdown?: string;
|
|
7
|
+
revision: number;
|
|
8
|
+
createdAtMs: number;
|
|
9
|
+
updatedAtMs: number;
|
|
10
|
+
};
|
|
11
|
+
/** 供 index.ts 装配:注入 api.runtime.state.resolveStateDir()(与旧 plans.sqlite 同一基址)。 */
|
|
12
|
+
export declare function setCoreAgentStateBase(dir: string): void;
|
|
13
|
+
export declare function resolveCoreAgentSqlitePath(agentId: string): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* 只读当前 agent 的 openclaw-agent.sqlite 里 session_progress_cards 一行。
|
|
16
|
+
* 文件/表/行缺失、任何解析或打开异常 → undefined,绝不外抛(调用点在热路径)。
|
|
17
|
+
*/
|
|
18
|
+
export declare function readCoreProgressCard(params: {
|
|
19
|
+
agentId: string;
|
|
20
|
+
sessionKey: string;
|
|
21
|
+
}): StoredCoreProgressCard | undefined;
|
|
22
|
+
//# sourceMappingURL=plan-core-db.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-db.d.ts","sourceRoot":"","sources":["../../src/plan-core-db.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,sBAAsB,GAAG;IACjC,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,SAAS,GAAG,aAAa,GAAG,WAAW,CAAA;KAAE,CAAC,CAAC;IAChF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;CACvB,CAAC;AAIF,kFAAkF;AAClF,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAEvD;AAED,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAK9E;AA0BD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IACzC,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACtB,GAAG,sBAAsB,GAAG,SAAS,CAsDrC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
const LOG_PREFIX = "[xg_cwork_im] [plan-core-db]";
|
|
4
|
+
const CORE_STATUSES = new Set(["pending", "in_progress", "completed"]);
|
|
5
|
+
let coreStateBase;
|
|
6
|
+
/** 供 index.ts 装配:注入 api.runtime.state.resolveStateDir()(与旧 plans.sqlite 同一基址)。 */
|
|
7
|
+
export function setCoreAgentStateBase(dir) {
|
|
8
|
+
coreStateBase = dir;
|
|
9
|
+
}
|
|
10
|
+
export function resolveCoreAgentSqlitePath(agentId) {
|
|
11
|
+
if (!coreStateBase) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return join(coreStateBase, "agents", agentId, "agent", "openclaw-agent.sqlite");
|
|
15
|
+
}
|
|
16
|
+
function isStoredCoreStep(entry) {
|
|
17
|
+
if (!entry || typeof entry !== "object") {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const raw = entry;
|
|
21
|
+
if (typeof raw.step !== "string" || !raw.step.trim()) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return typeof raw.status === "string" && CORE_STATUSES.has(raw.status);
|
|
25
|
+
}
|
|
26
|
+
function parseStoredSteps(raw) {
|
|
27
|
+
if (!Array.isArray(raw)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
const out = [];
|
|
31
|
+
for (const entry of raw) {
|
|
32
|
+
if (isStoredCoreStep(entry)) {
|
|
33
|
+
out.push({ step: entry.step.trim(), status: entry.status });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 只读当前 agent 的 openclaw-agent.sqlite 里 session_progress_cards 一行。
|
|
40
|
+
* 文件/表/行缺失、任何解析或打开异常 → undefined,绝不外抛(调用点在热路径)。
|
|
41
|
+
*/
|
|
42
|
+
export function readCoreProgressCard(params) {
|
|
43
|
+
const { agentId, sessionKey } = params;
|
|
44
|
+
const dbPath = resolveCoreAgentSqlitePath(agentId);
|
|
45
|
+
if (!dbPath) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
let db;
|
|
49
|
+
try {
|
|
50
|
+
db = new DatabaseSync(dbPath, { readOnly: true });
|
|
51
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
52
|
+
const row = db
|
|
53
|
+
.prepare("SELECT markdown, steps_json, revision, created_at, updated_at " +
|
|
54
|
+
"FROM session_progress_cards WHERE session_key = ?;")
|
|
55
|
+
.get(sessionKey);
|
|
56
|
+
if (!row) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
let stepsJson;
|
|
60
|
+
try {
|
|
61
|
+
stepsJson = row.steps_json ? JSON.parse(row.steps_json) : [];
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
stepsJson = [];
|
|
65
|
+
}
|
|
66
|
+
const steps = parseStoredSteps(stepsJson);
|
|
67
|
+
if (steps.length === 0) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
steps,
|
|
72
|
+
...(row.markdown ? { markdown: row.markdown } : {}),
|
|
73
|
+
revision: row.revision ?? 0,
|
|
74
|
+
createdAtMs: row.created_at ?? 0,
|
|
75
|
+
updatedAtMs: row.updated_at ?? 0,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.warn(`${LOG_PREFIX} 读取 core progress card 失败 agentId=${agentId}: ${String(err)}`);
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
try {
|
|
84
|
+
db?.close();
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// close 失败可忽略
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=plan-core-db.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-db.js","sourceRoot":"","sources":["../../src/plan-core-db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,UAAU,GAAG,8BAA8B,CAAC;AAElD,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,aAAa,EAAE,WAAW,CAAC,CAAC,CAAC;AAU5F,IAAI,aAAiC,CAAC;AAEtC,kFAAkF;AAClF,MAAM,UAAU,qBAAqB,CAAC,GAAW;IAC7C,aAAa,GAAG,GAAG,CAAC;AACxB,CAAC;AAED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X,OAAO,CAAC,IAAI,CAAC,GAAG,UAAU,qCAAqC,OAAO,KAAK,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1F,OAAO,SAAS,CAAC;IACrB,CAAC;YAAS,CAAC;QACP,IAAI,CAAC;YACD,EAAE,EAAE,KAAK,EAAE,CAAC;QAChB,CAAC;QAAC,MAAM,CAAC;YACL,cAAc;QAClB,CAAC;IACL,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-db.test.d.ts","sourceRoot":"","sources":["../../src/plan-core-db.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { DatabaseSync } from "node:sqlite";
|
|
6
|
+
import { after, afterEach, describe, it } from "node:test";
|
|
7
|
+
import { readCoreProgressCard, resolveCoreAgentSqlitePath, setCoreAgentStateBase, } from "./plan-core-db.js";
|
|
8
|
+
const tempDirs = [];
|
|
9
|
+
function makeTempBase() {
|
|
10
|
+
const dir = mkdtempSync(join(tmpdir(), "xg-plan-core-db-"));
|
|
11
|
+
tempDirs.push(dir);
|
|
12
|
+
return dir;
|
|
13
|
+
}
|
|
14
|
+
/** 造一个带 session_progress_cards 的 openclaw-agent.sqlite。 */
|
|
15
|
+
function seedAgentDb(baseDir, agentId, rows) {
|
|
16
|
+
const agentDir = join(baseDir, "agents", agentId, "agent");
|
|
17
|
+
mkdirSync(agentDir, { recursive: true });
|
|
18
|
+
const dbPath = join(agentDir, "openclaw-agent.sqlite");
|
|
19
|
+
const db = new DatabaseSync(dbPath);
|
|
20
|
+
try {
|
|
21
|
+
db.exec(`
|
|
22
|
+
CREATE TABLE session_progress_cards (
|
|
23
|
+
session_key TEXT NOT NULL PRIMARY KEY,
|
|
24
|
+
markdown TEXT,
|
|
25
|
+
steps_json TEXT,
|
|
26
|
+
revision INTEGER NOT NULL,
|
|
27
|
+
created_at INTEGER NOT NULL,
|
|
28
|
+
updated_at INTEGER NOT NULL
|
|
29
|
+
) STRICT;
|
|
30
|
+
`);
|
|
31
|
+
const insert = db.prepare("INSERT INTO session_progress_cards (session_key, markdown, steps_json, revision, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?);");
|
|
32
|
+
for (const row of rows) {
|
|
33
|
+
insert.run(row.sessionKey, null, row.stepsJson, row.revision ?? 1, row.createdAtMs ?? 1_700_000_000_000, row.updatedAtMs ?? 1_700_000_600_000);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
db.close();
|
|
38
|
+
}
|
|
39
|
+
return dbPath;
|
|
40
|
+
}
|
|
41
|
+
after(() => {
|
|
42
|
+
for (const dir of tempDirs.splice(0)) {
|
|
43
|
+
try {
|
|
44
|
+
rmSync(dir, { recursive: true, force: true });
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Windows 偶发句柄未释放导致删除失败,忽略
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
afterEach(() => {
|
|
52
|
+
// 避免 module 级 base 残留影响后续用例
|
|
53
|
+
setCoreAgentStateBase("");
|
|
54
|
+
});
|
|
55
|
+
describe("resolveCoreAgentSqlitePath / readCoreProgressCard", () => {
|
|
56
|
+
it("未注入 base → 路径与读取均为 undefined,不外抛", () => {
|
|
57
|
+
setCoreAgentStateBase("");
|
|
58
|
+
assert.equal(resolveCoreAgentSqlitePath("main"), undefined);
|
|
59
|
+
assert.equal(readCoreProgressCard({ agentId: "main", sessionKey: "s-1" }), undefined);
|
|
60
|
+
});
|
|
61
|
+
it("读取到 typed steps 与时间戳/revision", () => {
|
|
62
|
+
const base = makeTempBase();
|
|
63
|
+
setCoreAgentStateBase(base);
|
|
64
|
+
seedAgentDb(base, "main", [
|
|
65
|
+
{
|
|
66
|
+
sessionKey: "agent:main:s-1",
|
|
67
|
+
stepsJson: JSON.stringify([
|
|
68
|
+
{ step: "备份", status: "completed" },
|
|
69
|
+
{ step: "校验", status: "in_progress" },
|
|
70
|
+
]),
|
|
71
|
+
revision: 3,
|
|
72
|
+
createdAtMs: 1_700_000_000_000,
|
|
73
|
+
updatedAtMs: 1_700_000_900_000,
|
|
74
|
+
},
|
|
75
|
+
]);
|
|
76
|
+
const card = readCoreProgressCard({ agentId: "main", sessionKey: "agent:main:s-1" });
|
|
77
|
+
assert.ok(card);
|
|
78
|
+
assert.deepEqual(card.steps, [
|
|
79
|
+
{ step: "备份", status: "completed" },
|
|
80
|
+
{ step: "校验", status: "in_progress" },
|
|
81
|
+
]);
|
|
82
|
+
assert.equal(card.revision, 3);
|
|
83
|
+
assert.equal(card.createdAtMs, 1_700_000_000_000);
|
|
84
|
+
assert.equal(card.updatedAtMs, 1_700_000_900_000);
|
|
85
|
+
});
|
|
86
|
+
it("steps_json 为 null → undefined;非法 JSON → undefined;非法步骤被过滤", () => {
|
|
87
|
+
const base = makeTempBase();
|
|
88
|
+
setCoreAgentStateBase(base);
|
|
89
|
+
seedAgentDb(base, "main", [
|
|
90
|
+
{ sessionKey: "s-empty", stepsJson: null },
|
|
91
|
+
{ sessionKey: "s-bad-json", stepsJson: "{not json" },
|
|
92
|
+
{
|
|
93
|
+
sessionKey: "s-filtered",
|
|
94
|
+
stepsJson: JSON.stringify([
|
|
95
|
+
{ step: "keep", status: "pending" },
|
|
96
|
+
{ step: "", status: "pending" },
|
|
97
|
+
{ status: "completed" },
|
|
98
|
+
{ step: "bad-status", status: "blocked" },
|
|
99
|
+
]),
|
|
100
|
+
},
|
|
101
|
+
]);
|
|
102
|
+
assert.equal(readCoreProgressCard({ agentId: "main", sessionKey: "s-empty" }), undefined);
|
|
103
|
+
assert.equal(readCoreProgressCard({ agentId: "main", sessionKey: "s-bad-json" }), undefined);
|
|
104
|
+
const filtered = readCoreProgressCard({ agentId: "main", sessionKey: "s-filtered" });
|
|
105
|
+
assert.deepEqual(filtered?.steps, [{ step: "keep", status: "pending" }]);
|
|
106
|
+
});
|
|
107
|
+
it("行/库不存在 → undefined;库文件不存在 → undefined 不外抛", () => {
|
|
108
|
+
const base = makeTempBase();
|
|
109
|
+
setCoreAgentStateBase(base);
|
|
110
|
+
assert.equal(readCoreProgressCard({ agentId: "missing-agent", sessionKey: "s-1" }), undefined);
|
|
111
|
+
seedAgentDb(base, "main", [{ sessionKey: "s-1", stepsJson: "[]" }]);
|
|
112
|
+
assert.equal(readCoreProgressCard({ agentId: "main", sessionKey: "s-not-exist" }), undefined);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
//# sourceMappingURL=plan-core-db.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-db.test.js","sourceRoot":"","sources":["../../src/plan-core-db.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EACH,oBAAoB,EACpB,0BAA0B,EAC1B,qBAAqB,GACxB,MAAM,mBAAmB,CAAC;AAE3B,MAAM,QAAQ,GAAa,EAAE,CAAC;AAE9B,SAAS,YAAY;IACjB,MAAM,GAAG,GAAG,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,kBAAkB,CAAC,CAAC,CAAC;IAC5D,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACnB,OAAO,GAAG,CAAC;AACf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,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,CAAC;QACrF,MAAM,CAAC,SAAS,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,2CAA2C,EAAE,GAAG,EAAE;QACjD,MAAM,IAAI,GAAG,YAAY,EAAE,CAAC;QAC5B,qBAAqB,CAAC,IAAI,CAAC,CAAC;QAC5B,MAAM,CAAC,KAAK,CAAC,oBAAoB,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;QAC/F,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACpE,MAAM,CAAC,KAAK,CAAC,oBAAoB,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;IAClG,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Plan } from "@xgjktech/xg-openclaw-shared";
|
|
2
|
+
import type { ImStreamClient } from "./connection.js";
|
|
3
|
+
import { type StoredCoreProgressCard } from "./plan-core-db.js";
|
|
4
|
+
/** 由 core 落库卡合成 Plan(source:"snapshot");任何失败静默返回 undefined。 */
|
|
5
|
+
export declare function buildPlanFromCoreCard(params: {
|
|
6
|
+
sessionKey: string;
|
|
7
|
+
groupId: string;
|
|
8
|
+
card: StoredCoreProgressCard;
|
|
9
|
+
accountId?: string;
|
|
10
|
+
}): Plan | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* 每轮 @ 触发时(热路径入口):登记 WS client 后 fire-and-forget 推送一次快照。
|
|
13
|
+
* 纪律:登记必须先于快照、快照失败绝不影响登记(沿用原 PLAN_STAT 快照语义)。
|
|
14
|
+
*/
|
|
15
|
+
export declare function registerCorePlanSnapshotClient(params: {
|
|
16
|
+
groupId: string;
|
|
17
|
+
client: ImStreamClient;
|
|
18
|
+
replyToMsgId: string;
|
|
19
|
+
agentId: string;
|
|
20
|
+
sessionKey: string;
|
|
21
|
+
nowMs?: number;
|
|
22
|
+
}): void;
|
|
23
|
+
//# sourceMappingURL=plan-core-snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-snapshot.d.ts","sourceRoot":"","sources":["../../src/plan-core-snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,8BAA8B,CAAC;AACzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAwB,KAAK,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAetF,+DAA+D;AAC/D,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC1C,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,sBAAsB,CAAC;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,GAAG,IAAI,GAAG,SAAS,CAWnB;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAAC,MAAM,EAAE;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,cAAc,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,IAAI,CAqCP"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { readCoreProgressCard } from "./plan-core-db.js";
|
|
2
|
+
import { buildPlanFromCoreUpdate } from "./plan-core-adapter.js";
|
|
3
|
+
import { sendPlanStat } from "./plan-stat-event.js";
|
|
4
|
+
import { setPlanWsClient } from "./plan-ws-registry.js";
|
|
5
|
+
const LOG_PREFIX = "[xg_cwork_im] [plan-core-snapshot]";
|
|
6
|
+
/** 快照新鲜度阈值:core 卡超过 1 小时未更新,视为已失败/过期,快照不再推给前端。 */
|
|
7
|
+
const CORE_PLAN_SNAPSHOT_STALE_MS = 60 * 60 * 1_000;
|
|
8
|
+
/** 「未完成」判定:存在 status !== "completed" 的步骤。空 steps 已被读取层过滤。 */
|
|
9
|
+
function isUnfinished(card) {
|
|
10
|
+
return card.steps.some((step) => step.status !== "completed");
|
|
11
|
+
}
|
|
12
|
+
/** 由 core 落库卡合成 Plan(source:"snapshot");任何失败静默返回 undefined。 */
|
|
13
|
+
export function buildPlanFromCoreCard(params) {
|
|
14
|
+
const { sessionKey, groupId, card, accountId } = params;
|
|
15
|
+
const createdAtIso = card.createdAtMs > 0 ? new Date(card.createdAtMs).toISOString() : undefined;
|
|
16
|
+
return buildPlanFromCoreUpdate({
|
|
17
|
+
sessionKey,
|
|
18
|
+
groupId,
|
|
19
|
+
accountId,
|
|
20
|
+
payload: { phase: "update", title: "Plan updated", steps: card.steps },
|
|
21
|
+
nowMs: card.updatedAtMs > 0 ? card.updatedAtMs : Date.now(),
|
|
22
|
+
...(createdAtIso ? { createdAtIso } : {}),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 每轮 @ 触发时(热路径入口):登记 WS client 后 fire-and-forget 推送一次快照。
|
|
27
|
+
* 纪律:登记必须先于快照、快照失败绝不影响登记(沿用原 PLAN_STAT 快照语义)。
|
|
28
|
+
*/
|
|
29
|
+
export function registerCorePlanSnapshotClient(params) {
|
|
30
|
+
const { groupId, client, replyToMsgId, agentId, sessionKey } = params;
|
|
31
|
+
setPlanWsClient(groupId, client, replyToMsgId);
|
|
32
|
+
try {
|
|
33
|
+
const card = readCoreProgressCard({ agentId, sessionKey });
|
|
34
|
+
if (!card) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const nowMs = params.nowMs ?? Date.now();
|
|
38
|
+
if (nowMs - card.updatedAtMs > CORE_PLAN_SNAPSHOT_STALE_MS) {
|
|
39
|
+
console.warn("[xg_cwork_im] [plan-telemetry] stale-core-plan-dropped " +
|
|
40
|
+
JSON.stringify({
|
|
41
|
+
sessionKey,
|
|
42
|
+
planId: sessionKey,
|
|
43
|
+
updatedAt: new Date(card.updatedAtMs).toISOString(),
|
|
44
|
+
revision: card.revision,
|
|
45
|
+
}));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (!isUnfinished(card)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const plan = buildPlanFromCoreCard({
|
|
52
|
+
sessionKey,
|
|
53
|
+
groupId,
|
|
54
|
+
card,
|
|
55
|
+
});
|
|
56
|
+
if (!plan) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
sendPlanStat({ plan, source: "snapshot" });
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.warn(`${LOG_PREFIX} 快照失败已吞 sessionKey=${sessionKey} groupId=${groupId}: ${String(err)}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=plan-core-snapshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan-core-snapshot.js","sourceRoot":"","sources":["../../src/plan-core-snapshot.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAA+B,MAAM,mBAAmB,CAAC;AACtF,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,UAAU,GAAG,oCAAoC,CAAC;AAExD,kDAAkD;AAClD,MAAM,2BAA2B,GAAG,EAAE,GAAG,EAAE,GAAG,KAA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