dsh-project-recall 0.3.1 → 0.4.1
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 +22 -0
- package/README.md +15 -2
- package/client.js +157 -0
- package/index.js +9 -1
- package/lib/http.js +25 -0
- package/lib/store.js +45 -6
- package/lib/sync.js +246 -0
- package/lib/tools.js +31 -0
- package/package.json +6 -5
- package/scripts/import-from-nas.mjs +28 -86
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.1 — 2026-11-22
|
|
4
|
+
|
|
5
|
+
自动对齐加了「只在白天跑」。
|
|
6
|
+
|
|
7
|
+
- 新增活跃时段(默认 9:00–24:00):窗口外一个请求都不发,晚上和凌晨完全安静;`start > end` 表示跨夜(例如 22 点到 6 点)。
|
|
8
|
+
- 设置页可以直接改起止小时,跟间隔放在一起;「立即对齐」不受时段限制,随时能手动跑一次。
|
|
9
|
+
- 间隔默认仍是 30 分钟,可以调到 90 分钟之类——流量实测一次约 78 KB(`/api/projects` 5.3 KB + `/api/plans` 73.5 KB)。
|
|
10
|
+
- 测试:新增活跃时段与时段外跳过的用例,共 44 个用例全绿。
|
|
11
|
+
|
|
12
|
+
## 0.4.0 — 2026-11-22
|
|
13
|
+
|
|
14
|
+
对齐不必再手动跑脚本:续忆自己会定时把 Codex 版补齐。
|
|
15
|
+
|
|
16
|
+
- 设置页新增「自动对齐 Codex 版」:开关、地址、间隔(分钟)都在里面,还有一个「立即对齐」。地址填 `http://192.168.5.8:4318`(飞牛)或本机 `:4318`。
|
|
17
|
+
- 插件启动 15 秒后试一次,之后每分钟看一眼是否到点;按「项目 + 标题」去重,只增不改,失败的只记日志并在设置页显示,不打扰面板。
|
|
18
|
+
- 环境变量 `PROJECT_MEMORY_NAS_URL` / `PROJECT_MEMORY_SYNC_TOKEN` 可以预设地址和令牌,首次装好就自动开着。
|
|
19
|
+
- 新 HTTP 接口 `GET /project-memory/api/sync`(看配置与上次结果)、`POST /project-memory/api/sync`(立刻对齐,body 可带 `source` / `interval_minutes` / `include_closed`)。
|
|
20
|
+
- 新 Agent 工具 `project_memory_sync_now`:问「为什么两边条数不一样 / 对齐一下」时,Agent 自己就能跑。
|
|
21
|
+
- `scripts/import-from-nas.mjs` 改为复用 `lib/sync.js` 的同一套去重逻辑,脚本和插件不会再算出两种结果。
|
|
22
|
+
- 测试:新增 `test/sync.test.js`,HTTP 侧补 `/sync` 用例,共 40 个用例全绿。
|
|
23
|
+
- 修 `package.json` 里的仓库地址(原 `hhse` 账号已不存在)指向备份仓库 `hseons/dsh-backup`。
|
|
24
|
+
|
|
3
25
|
## 0.3.1 — 2026-11-22
|
|
4
26
|
|
|
5
27
|
把 Codex 版 / 飞牛上的待办对齐进续忆。
|
package/README.md
CHANGED
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
- **完整上下文**:为什么现在不做、什么条件下回来、下一步从哪继续,都记在详情里。
|
|
22
22
|
- **搜索与项目筛选**:按关键词、项目查找已经记过的事。
|
|
23
23
|
- **Agent 原生工具**:DSH 里的模型可以直接提出、搜索、更新计划。
|
|
24
|
+
- **自动对齐 Codex 版**:设置页填一次地址,之后按间隔把 Codex 版 / 飞牛里还没结束的计划补进来,只增不改。
|
|
24
25
|
- **备份与恢复**:设置页导出/导入 JSON;支持合并或覆盖,清空数据需要手动输入确认。
|
|
25
26
|
- **本地优先**:数据只保存在本机 DSH 数据域,不发送任何遥测。
|
|
26
27
|
|
|
27
28
|
## 要求
|
|
28
29
|
|
|
29
30
|
- DSH `>= 0.2.1-rc.3`
|
|
30
|
-
- 当前版本:`0.
|
|
31
|
+
- 当前版本:`0.4.1`(自动对齐版)
|
|
31
32
|
|
|
32
33
|
## 安装
|
|
33
34
|
|
|
@@ -86,6 +87,7 @@ HTTP 接口:`POST /project-memory/api/plans/:id/dispatch`,body 可带 `mode`
|
|
|
86
87
|
| `project_memory_get_plan` | 读取单条计划的完整上下文 |
|
|
87
88
|
| `project_memory_update_plan` | 修改状态、复盘时间、内容 |
|
|
88
89
|
| `project_memory_dispatch_plan` | 把一件计划交给一个会真正动手的会话执行 |
|
|
90
|
+
| `project_memory_sync_now` | 立刻与 Codex 版 / 飞牛对一次齐 |
|
|
89
91
|
|
|
90
92
|
模型会被提示:只记录**明确推迟的具体工作**,模糊想法和普通待办不记;调用时尽量传入当前工作目录的绝对路径。
|
|
91
93
|
|
|
@@ -105,7 +107,18 @@ HTTP 接口:`POST /project-memory/api/plans/:id/dispatch`,body 可带 `mode`
|
|
|
105
107
|
|
|
106
108
|
续忆用的是 DSH 独立数据域,**不读** Codex 版(本机 SQLite + 飞牛 4318 服务)那份库,两边各记各的——所以同一个项目的待办会在两个面板里看到不同的条数,这是设计如此,不是丢数据。
|
|
107
109
|
|
|
108
|
-
要把 Codex
|
|
110
|
+
要把 Codex 版里还没结束的计划并进来,两种办法:
|
|
111
|
+
|
|
112
|
+
**自动(推荐)**:设置 →「续忆」→「自动对齐 Codex 版」
|
|
113
|
+
|
|
114
|
+
- 打开开关、填地址(`http://192.168.5.8:4318` 或本机 `:4318`)、设间隔(默认 30 分钟,90 分钟也很常见),下面有「立即对齐」和上次结果。
|
|
115
|
+
- **活跃时段**(默认 9:00–24:00):窗口外一个请求都不发,凌晨完全安静;`start > end` 表示跨夜。手动「立即对齐」不受时段限制。
|
|
116
|
+
- 一次对齐大约 78 KB 局域网流量(5.3 KB + 73.5 KB 两个接口),30 分钟一次约 3.6 MB/天。
|
|
117
|
+
- 插件启动 15 秒后试一次,之后每分钟检查是否到点;按「项目 + 标题」去重,只增不改。
|
|
118
|
+
- 失败不打扰面板,只在设置页留一行原因;地址填 `PROJECT_MEMORY_NAS_URL` 环境变量可在装好时就默认开着。
|
|
119
|
+
- Agent 侧有 `project_memory_sync_now`,问一句「对齐一下」它就能跑;接口是 `POST /project-memory/api/sync`。
|
|
120
|
+
|
|
121
|
+
**手动**:仓库带了一个一次性对齐脚本(与自动对齐共用同一套去重逻辑):
|
|
109
122
|
|
|
110
123
|
```bash
|
|
111
124
|
node scripts/import-from-nas.mjs # dry-run,只打印会新增哪些
|
package/client.js
CHANGED
|
@@ -1469,6 +1469,7 @@ window.__ModuleLoader__.load({
|
|
|
1469
1469
|
const [mode, setMode] = useState('merge');
|
|
1470
1470
|
const [resetOpen, setResetOpen] = useState(false);
|
|
1471
1471
|
const [resetText, setResetText] = useState('');
|
|
1472
|
+
const [syncForm, setSyncForm] = useState(null);
|
|
1472
1473
|
const fileRef = useRef(null);
|
|
1473
1474
|
|
|
1474
1475
|
const refresh = useCallback(async () => {
|
|
@@ -1483,6 +1484,10 @@ window.__ModuleLoader__.load({
|
|
|
1483
1484
|
refresh();
|
|
1484
1485
|
}, [refresh]);
|
|
1485
1486
|
|
|
1487
|
+
useEffect(() => {
|
|
1488
|
+
if (data?.preferences?.sync) setSyncForm(data.preferences.sync);
|
|
1489
|
+
}, [data]);
|
|
1490
|
+
|
|
1486
1491
|
async function exportData() {
|
|
1487
1492
|
setBusy(true);
|
|
1488
1493
|
try {
|
|
@@ -1539,6 +1544,54 @@ window.__ModuleLoader__.load({
|
|
|
1539
1544
|
}
|
|
1540
1545
|
}
|
|
1541
1546
|
|
|
1547
|
+
async function saveSync(patch) {
|
|
1548
|
+
setSyncForm((current) => ({ ...(current ?? {}), ...patch }));
|
|
1549
|
+
setBusy(true);
|
|
1550
|
+
try {
|
|
1551
|
+
const result = await api('/settings', {
|
|
1552
|
+
method: 'PATCH',
|
|
1553
|
+
body: JSON.stringify({ sync: patch }),
|
|
1554
|
+
});
|
|
1555
|
+
setData((current) => (current ? { ...current, preferences: result.preferences } : current));
|
|
1556
|
+
setMessage('已保存对齐设置');
|
|
1557
|
+
} catch (error) {
|
|
1558
|
+
setMessage(error.message);
|
|
1559
|
+
} finally {
|
|
1560
|
+
setBusy(false);
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
function saveHours(patch) {
|
|
1565
|
+
const hours = {
|
|
1566
|
+
start: Number(syncForm?.hours?.start ?? 9),
|
|
1567
|
+
end: Number(syncForm?.hours?.end ?? 24),
|
|
1568
|
+
...patch,
|
|
1569
|
+
};
|
|
1570
|
+
setSyncForm({ ...(syncForm ?? {}), hours });
|
|
1571
|
+
saveSync({ hours });
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
async function syncNow() {
|
|
1575
|
+
setBusy(true);
|
|
1576
|
+
try {
|
|
1577
|
+
const result = await api('/sync', { method: 'POST', body: JSON.stringify({}) });
|
|
1578
|
+
const imported = result?.result?.imported ?? 0;
|
|
1579
|
+
const projects = result?.result?.projects ?? 0;
|
|
1580
|
+
setMessage(
|
|
1581
|
+
imported > 0
|
|
1582
|
+
? `已对齐:补进 ${imported} 条计划、${projects} 个项目`
|
|
1583
|
+
: result?.result?.error
|
|
1584
|
+
? `对齐失败:${result.result.error}`
|
|
1585
|
+
: '已对齐:两边本来就是一致的',
|
|
1586
|
+
);
|
|
1587
|
+
await refresh();
|
|
1588
|
+
} catch (error) {
|
|
1589
|
+
setMessage(error.message);
|
|
1590
|
+
} finally {
|
|
1591
|
+
setBusy(false);
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1542
1595
|
async function resetAll() {
|
|
1543
1596
|
setBusy(true);
|
|
1544
1597
|
try {
|
|
@@ -1583,6 +1636,104 @@ window.__ModuleLoader__.load({
|
|
|
1583
1636
|
h('button', { className: 'pm-btn pm-btn-ghost', onClick: exportData, disabled: busy }, busy ? '导出中…' : '导出 JSON'),
|
|
1584
1637
|
h('button', { className: 'pm-btn pm-btn-danger', onClick: () => setResetOpen(true), disabled: busy }, '清空数据'),
|
|
1585
1638
|
),
|
|
1639
|
+
h(
|
|
1640
|
+
'div',
|
|
1641
|
+
{ className: 'pm-sync' },
|
|
1642
|
+
h('h4', null, '自动对齐 Codex 版'),
|
|
1643
|
+
h(
|
|
1644
|
+
'p',
|
|
1645
|
+
{ className: 'pm-muted' },
|
|
1646
|
+
'续忆用的是 DSH 自己的数据域,不读 Codex 版(本机 SQLite / 飞牛 4318)那份库,所以两个面板条数会不同。打开后按间隔把那边还没结束的计划补进来,只增不改。',
|
|
1647
|
+
),
|
|
1648
|
+
h(
|
|
1649
|
+
'label',
|
|
1650
|
+
{ className: 'pm-sync-check' },
|
|
1651
|
+
h('input', {
|
|
1652
|
+
type: 'checkbox',
|
|
1653
|
+
checked: syncForm?.enabled === true,
|
|
1654
|
+
disabled: busy,
|
|
1655
|
+
onChange: (event) => saveSync({ enabled: event.target.checked }),
|
|
1656
|
+
}),
|
|
1657
|
+
h('span', null, '打开自动对齐'),
|
|
1658
|
+
),
|
|
1659
|
+
h(
|
|
1660
|
+
'label',
|
|
1661
|
+
{ className: 'pm-sync-row' },
|
|
1662
|
+
h('span', null, 'Codex 版地址'),
|
|
1663
|
+
h('input', {
|
|
1664
|
+
type: 'text',
|
|
1665
|
+
value: syncForm?.source ?? '',
|
|
1666
|
+
placeholder: 'http://192.168.5.8:4318',
|
|
1667
|
+
'aria-label': 'Codex 版地址',
|
|
1668
|
+
onChange: (event) => setSyncForm({ ...(syncForm ?? {}), source: event.target.value }),
|
|
1669
|
+
onBlur: (event) => saveSync({ source: event.target.value, enabled: event.target.value.trim() !== '' }),
|
|
1670
|
+
}),
|
|
1671
|
+
),
|
|
1672
|
+
h(
|
|
1673
|
+
'label',
|
|
1674
|
+
{ className: 'pm-sync-row' },
|
|
1675
|
+
h('span', null, '间隔(分钟)'),
|
|
1676
|
+
h('input', {
|
|
1677
|
+
type: 'number',
|
|
1678
|
+
min: 5,
|
|
1679
|
+
step: 5,
|
|
1680
|
+
value: syncForm?.interval_minutes ?? 30,
|
|
1681
|
+
'aria-label': '对齐间隔(分钟)',
|
|
1682
|
+
onChange: (event) => setSyncForm({ ...(syncForm ?? {}), interval_minutes: Number(event.target.value) }),
|
|
1683
|
+
onBlur: (event) => {
|
|
1684
|
+
const minutes = Number(event.target.value);
|
|
1685
|
+
if (Number.isInteger(minutes) && minutes >= 5) saveSync({ interval_minutes: minutes });
|
|
1686
|
+
},
|
|
1687
|
+
}),
|
|
1688
|
+
),
|
|
1689
|
+
h(
|
|
1690
|
+
'label',
|
|
1691
|
+
{ className: 'pm-sync-row' },
|
|
1692
|
+
h('span', null, '活跃时段'),
|
|
1693
|
+
h('input', {
|
|
1694
|
+
type: 'number',
|
|
1695
|
+
min: 0,
|
|
1696
|
+
max: 23,
|
|
1697
|
+
step: 1,
|
|
1698
|
+
value: syncForm?.hours?.start ?? 9,
|
|
1699
|
+
'aria-label': '活跃时段开始(点)',
|
|
1700
|
+
onChange: (event) =>
|
|
1701
|
+
setSyncForm({ ...(syncForm ?? {}), hours: { ...(syncForm?.hours ?? {}), start: Number(event.target.value) } }),
|
|
1702
|
+
onBlur: (event) => {
|
|
1703
|
+
const start = Number(event.target.value);
|
|
1704
|
+
if (Number.isInteger(start) && start >= 0 && start <= 23) saveHours({ start });
|
|
1705
|
+
},
|
|
1706
|
+
}),
|
|
1707
|
+
h('span', null, '点到'),
|
|
1708
|
+
h('input', {
|
|
1709
|
+
type: 'number',
|
|
1710
|
+
min: 1,
|
|
1711
|
+
max: 24,
|
|
1712
|
+
step: 1,
|
|
1713
|
+
value: syncForm?.hours?.end ?? 24,
|
|
1714
|
+
'aria-label': '活跃时段结束(点)',
|
|
1715
|
+
onChange: (event) =>
|
|
1716
|
+
setSyncForm({ ...(syncForm ?? {}), hours: { ...(syncForm?.hours ?? {}), end: Number(event.target.value) } }),
|
|
1717
|
+
onBlur: (event) => {
|
|
1718
|
+
const end = Number(event.target.value);
|
|
1719
|
+
if (Number.isInteger(end) && end >= 1 && end <= 24) saveHours({ end });
|
|
1720
|
+
},
|
|
1721
|
+
}),
|
|
1722
|
+
h('span', null, '点,其余时间不动'),
|
|
1723
|
+
),
|
|
1724
|
+
h(
|
|
1725
|
+
'div',
|
|
1726
|
+
{ className: 'pm-settings-actions' },
|
|
1727
|
+
h('button', { className: 'pm-btn pm-btn-ghost', onClick: syncNow, disabled: busy }, busy ? '对齐中…' : '立即对齐'),
|
|
1728
|
+
),
|
|
1729
|
+
h(
|
|
1730
|
+
'p',
|
|
1731
|
+
{ className: 'pm-muted' },
|
|
1732
|
+
syncForm?.last_run_at
|
|
1733
|
+
? `上次对齐 ${fmtDate(syncForm.last_run_at, true)} · 新增 ${syncForm.last_imported ?? 0} 条${syncForm.last_error ? ` · 失败:${syncForm.last_error}` : ''}`
|
|
1734
|
+
: '还没跑过对齐。',
|
|
1735
|
+
),
|
|
1736
|
+
),
|
|
1586
1737
|
h('input', {
|
|
1587
1738
|
ref: fileRef,
|
|
1588
1739
|
type: 'file',
|
|
@@ -1866,6 +2017,12 @@ window.__ModuleLoader__.load({
|
|
|
1866
2017
|
.pm-settings { padding: 4px 0 10px; }
|
|
1867
2018
|
.pm-settings h3 { margin: 0 0 5px; font-size: 14px; }
|
|
1868
2019
|
.pm-settings p { margin: 0 0 12px; font-size: 12px; line-height: 1.6; }
|
|
2020
|
+
.pm-sync { border-top: 1px solid var(--dsw-alias-border-l1); margin: 14px 0 4px; padding-top: 12px; display: flex; flex-direction: column; gap: 8px; }
|
|
2021
|
+
.pm-sync h4 { margin: 0; font-size: 13px; font-weight: 600; }
|
|
2022
|
+
.pm-sync-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--pm-muted); }
|
|
2023
|
+
.pm-sync-row input[type="text"], .pm-sync-row input[type="number"] { flex: 1; min-width: 120px; background: var(--dsw-alias-bg-layer-1); border: 1px solid var(--dsw-alias-border-l1); border-radius: 8px; color: inherit; font-size: 12px; padding: 6px 9px; }
|
|
2024
|
+
.pm-sync-row input[type="number"] { flex: 0 0 84px; }
|
|
2025
|
+
.pm-sync-check { display: flex; align-items: center; gap: 7px; font-size: 12px; }
|
|
1869
2026
|
.pm-settings-grid { display: flex; gap: 10px; margin: 12px 0; flex-wrap: wrap; }
|
|
1870
2027
|
.pm-settings-grid > div { border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-1); border-radius: 10px; padding: 9px 12px; min-width: 82px; display: flex; flex-direction: column; gap: 2px; }
|
|
1871
2028
|
.pm-settings-grid strong { font-size: 19px; }
|
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { domainSpec } from './lib/model.js';
|
|
|
3
3
|
import { registerTools } from './lib/tools.js';
|
|
4
4
|
import { API_PREFIX, createApi } from './lib/http.js';
|
|
5
5
|
import { dispatchPlan } from './lib/dispatch.js';
|
|
6
|
+
import { startAutoSync } from './lib/sync.js';
|
|
6
7
|
|
|
7
8
|
export const name = 'project-memory';
|
|
8
9
|
export const inject = ['storageDomain', 'tools', 'systemPrompt'];
|
|
@@ -16,6 +17,7 @@ const STORE_METHODS = [
|
|
|
16
17
|
'capturePlan',
|
|
17
18
|
'updatePlan',
|
|
18
19
|
'markDispatched',
|
|
20
|
+
'recordSyncRun',
|
|
19
21
|
'reviewPlan',
|
|
20
22
|
'decorate',
|
|
21
23
|
'listPlans',
|
|
@@ -50,7 +52,12 @@ export function apply(ctx, config = {}) {
|
|
|
50
52
|
lazyStore[method] = (...args) => storePromise.then((store) => store[method](...args));
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
const autoSync = startAutoSync(ctx, storePromise, { logger: ctx.logger });
|
|
56
|
+
|
|
57
|
+
registerTools(ctx, lazyStore, {
|
|
58
|
+
dispatch: (plan, options) => dispatchPlan(ctx, plan, options),
|
|
59
|
+
sync: (options) => autoSync.tick(options),
|
|
60
|
+
});
|
|
54
61
|
|
|
55
62
|
ctx.effect(
|
|
56
63
|
() =>
|
|
@@ -70,6 +77,7 @@ export function apply(ctx, config = {}) {
|
|
|
70
77
|
path: API_PREFIX,
|
|
71
78
|
handler: createApi(storePromise, {
|
|
72
79
|
dispatch: (plan, options) => dispatchPlan(hostCtx, plan, options),
|
|
80
|
+
sync: (options) => autoSync.tick(options),
|
|
73
81
|
}),
|
|
74
82
|
}),
|
|
75
83
|
'project-memory api',
|
package/lib/http.js
CHANGED
|
@@ -137,6 +137,31 @@ export function createApi(storeSource, options = {}) {
|
|
|
137
137
|
return;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
if (method === 'GET' && sub === '/sync') {
|
|
141
|
+
const preferences = store.getPreferences();
|
|
142
|
+
sendJson(res, 200, { sync: preferences.sync ?? null });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (method === 'POST' && sub === '/sync') {
|
|
147
|
+
if (typeof options.sync !== 'function') {
|
|
148
|
+
throw new HttpError(503, '这个运行时没有启用自动对齐');
|
|
149
|
+
}
|
|
150
|
+
const body = await readJsonBody(req);
|
|
151
|
+
if (body && Object.keys(body).length > 0) {
|
|
152
|
+
await store.updatePreferences({ sync: body });
|
|
153
|
+
}
|
|
154
|
+
const preferences = store.getPreferences();
|
|
155
|
+
const config = preferences.sync ?? {};
|
|
156
|
+
if (!config.source) throw new HttpError(400, '先填 Codex 版地址(例如 http://192.168.5.8:4318)');
|
|
157
|
+
if (config.enabled === false) await store.updatePreferences({ sync: { enabled: true } });
|
|
158
|
+
const result = await options.sync({ force: true });
|
|
159
|
+
if (result && typeof result === 'object' && result.at) await store.recordSyncRun(result);
|
|
160
|
+
const after = store.getPreferences();
|
|
161
|
+
sendJson(res, 200, { result, sync: after.sync ?? null });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
140
165
|
if (method === 'POST' && sub === '/plans') {
|
|
141
166
|
const body = await readJsonBody(req);
|
|
142
167
|
const input = {
|
package/lib/store.js
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
projectSchema,
|
|
11
11
|
settingsSchema,
|
|
12
12
|
} from './model.js';
|
|
13
|
+
import { defaultSyncConfig, normalizeHours } from './sync.js';
|
|
13
14
|
|
|
14
15
|
const ALLOWED_UPDATE_FIELDS = new Set([
|
|
15
16
|
'title',
|
|
@@ -87,6 +88,7 @@ export class ProjectMemoryStore {
|
|
|
87
88
|
locale: 'zh',
|
|
88
89
|
default_project_id: null,
|
|
89
90
|
stale_days: this.staleDays,
|
|
91
|
+
sync: defaultSyncConfig(),
|
|
90
92
|
updated_at: nowIso(),
|
|
91
93
|
});
|
|
92
94
|
}
|
|
@@ -399,12 +401,17 @@ export class ProjectMemoryStore {
|
|
|
399
401
|
}
|
|
400
402
|
|
|
401
403
|
getPreferences() {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
404
|
+
const stored = this.settings.get('preferences');
|
|
405
|
+
if (!stored) {
|
|
406
|
+
return {
|
|
407
|
+
locale: 'zh',
|
|
408
|
+
default_project_id: null,
|
|
409
|
+
stale_days: this.staleDays,
|
|
410
|
+
sync: defaultSyncConfig(),
|
|
411
|
+
updated_at: nowIso(),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
return stored.sync ? stored : { ...stored, sync: defaultSyncConfig() };
|
|
408
415
|
}
|
|
409
416
|
|
|
410
417
|
async updatePreferences(patch = {}) {
|
|
@@ -418,10 +425,42 @@ export class ProjectMemoryStore {
|
|
|
418
425
|
next.stale_days = patch.stale_days;
|
|
419
426
|
this.staleDays = patch.stale_days;
|
|
420
427
|
}
|
|
428
|
+
if (patch.sync && typeof patch.sync === 'object' && !Array.isArray(patch.sync)) {
|
|
429
|
+
const current = next.sync ?? defaultSyncConfig();
|
|
430
|
+
const merged = { ...current };
|
|
431
|
+
if (typeof patch.sync.enabled === 'boolean') merged.enabled = patch.sync.enabled;
|
|
432
|
+
if (typeof patch.sync.source === 'string') merged.source = patch.sync.source.trim().replace(/\/+$/, '');
|
|
433
|
+
if (typeof patch.sync.token === 'string') merged.token = patch.sync.token.trim();
|
|
434
|
+
if (Number.isInteger(patch.sync.interval_minutes) && patch.sync.interval_minutes >= 5) {
|
|
435
|
+
merged.interval_minutes = patch.sync.interval_minutes;
|
|
436
|
+
}
|
|
437
|
+
if (typeof patch.sync.include_closed === 'boolean') merged.include_closed = patch.sync.include_closed;
|
|
438
|
+
if (patch.sync.hours && typeof patch.sync.hours === 'object' && !Array.isArray(patch.sync.hours)) {
|
|
439
|
+
merged.hours = normalizeHours(patch.sync.hours);
|
|
440
|
+
}
|
|
441
|
+
if (merged.enabled && !merged.source) merged.enabled = false;
|
|
442
|
+
next.sync = merged;
|
|
443
|
+
}
|
|
421
444
|
await this.settings.put('preferences', next);
|
|
422
445
|
return next;
|
|
423
446
|
}
|
|
424
447
|
|
|
448
|
+
async recordSyncRun({ at, imported = 0, projects = 0, skipped = 0, error = '', source } = {}) {
|
|
449
|
+
const current = this.getPreferences();
|
|
450
|
+
const sync = {
|
|
451
|
+
...(current.sync ?? defaultSyncConfig()),
|
|
452
|
+
last_run_at: at || nowIso(),
|
|
453
|
+
last_imported: Number(imported) || 0,
|
|
454
|
+
last_projects: Number(projects) || 0,
|
|
455
|
+
last_skipped: Number(skipped) || 0,
|
|
456
|
+
last_error: String(error || ''),
|
|
457
|
+
};
|
|
458
|
+
if (typeof source === 'string' && source.trim() !== '') sync.last_source = source;
|
|
459
|
+
const next = { ...current, sync, updated_at: nowIso() };
|
|
460
|
+
await this.settings.put('preferences', next);
|
|
461
|
+
return sync;
|
|
462
|
+
}
|
|
463
|
+
|
|
425
464
|
exportAll() {
|
|
426
465
|
return {
|
|
427
466
|
format: 'dsh-project-recall',
|
package/lib/sync.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 与 Codex 版 project-memory(本机 SQLite / 飞牛上的 4318 服务)对齐。
|
|
3
|
+
*
|
|
4
|
+
* 续忆用 DSH 独立数据域,不读那份 SQLite;这里通过它的 HTTP 接口把「还没结束」的计划
|
|
5
|
+
* 拉过来,按「项目名 + 标题」去重,只增不改。可以手动触发,也可以按间隔自动跑。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const OPEN_STATUSES = new Set(['candidate', 'idea', 'someday', 'planned', 'blocked']);
|
|
9
|
+
export const DEFAULT_INTERVAL_MINUTES = 30;
|
|
10
|
+
/** 只在白天对齐:默认 9:00 到 24:00,凌晨不动。 */
|
|
11
|
+
export const DEFAULT_HOURS = { start: 9, end: 24 };
|
|
12
|
+
const FETCH_TIMEOUT_MS = 8000;
|
|
13
|
+
|
|
14
|
+
function trim(value) {
|
|
15
|
+
return String(value ?? '').trim();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function normalizeKey(projectName, title) {
|
|
19
|
+
return `${trim(projectName).toLowerCase()}::${trim(title).replace(/\s+/g, ' ').toLowerCase()}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function defaultSyncConfig(env = process.env) {
|
|
23
|
+
const source = trim(env.PROJECT_MEMORY_NAS_URL).replace(/\/+$/, '');
|
|
24
|
+
return {
|
|
25
|
+
enabled: Boolean(source),
|
|
26
|
+
source,
|
|
27
|
+
token: trim(env.PROJECT_MEMORY_SYNC_TOKEN),
|
|
28
|
+
interval_minutes: DEFAULT_INTERVAL_MINUTES,
|
|
29
|
+
include_closed: false,
|
|
30
|
+
hours: { ...DEFAULT_HOURS },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function normalizeHours(value) {
|
|
35
|
+
const start = Number(value?.start);
|
|
36
|
+
const end = Number(value?.end);
|
|
37
|
+
const safeStart = Number.isInteger(start) && start >= 0 && start <= 23 ? start : DEFAULT_HOURS.start;
|
|
38
|
+
const safeEnd = Number.isInteger(end) && end >= 1 && end <= 24 ? end : DEFAULT_HOURS.end;
|
|
39
|
+
if (safeStart === safeEnd) return { ...DEFAULT_HOURS };
|
|
40
|
+
return { start: safeStart, end: safeEnd };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 当前本地时间是否落在活跃时段里;start > end 表示跨夜,end 为 24 表示到 24:00。 */
|
|
44
|
+
export function withinHours(date, hours) {
|
|
45
|
+
const window = normalizeHours(hours);
|
|
46
|
+
const hour = date.getHours();
|
|
47
|
+
if (window.start < window.end) return hour >= window.start && hour < window.end;
|
|
48
|
+
return hour >= window.start || hour < window.end;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function codexPlanToImport(plan) {
|
|
52
|
+
return {
|
|
53
|
+
id: plan.id,
|
|
54
|
+
project_id: plan.project_id,
|
|
55
|
+
title: plan.title,
|
|
56
|
+
details: plan.details ?? '',
|
|
57
|
+
status: plan.status,
|
|
58
|
+
reason_deferred: plan.reason_deferred ?? '',
|
|
59
|
+
reactivation_conditions: Array.isArray(plan.reactivation_conditions) ? plan.reactivation_conditions : [],
|
|
60
|
+
tags: Array.isArray(plan.tags) ? plan.tags : [],
|
|
61
|
+
priority: plan.priority ?? 'medium',
|
|
62
|
+
effort: plan.effort ?? 'unknown',
|
|
63
|
+
source_type: plan.source_type ?? 'conversation',
|
|
64
|
+
source_ref: plan.source_ref ?? '',
|
|
65
|
+
source_excerpt: plan.source_excerpt ?? '',
|
|
66
|
+
review_at: plan.review_at ?? null,
|
|
67
|
+
last_reviewed_at: plan.last_reviewed_at ?? null,
|
|
68
|
+
completed_at: plan.completed_at ?? null,
|
|
69
|
+
created_at: plan.created_at,
|
|
70
|
+
updated_at: plan.updated_at,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 纯函数:算出要补哪些项目和计划。store 侧只传 exportAll() 的快照。
|
|
76
|
+
*/
|
|
77
|
+
export function buildAlignPayload({ remoteProjects = [], remotePlans = [], existingProjects = [], existingPlans = [], includeClosed = false } = {}) {
|
|
78
|
+
const projectName = new Map(remoteProjects.map((project) => [project.id, project.name]));
|
|
79
|
+
const existingProjectNames = new Map(existingProjects.map((project) => [project.id, project.name]));
|
|
80
|
+
const existingByName = new Map(existingProjects.map((project) => [trim(project.name).toLowerCase(), project]));
|
|
81
|
+
// 同名项目直接复用本地的 id,避免同一个项目在面板里出现两次
|
|
82
|
+
const remoteToLocal = new Map();
|
|
83
|
+
for (const project of remoteProjects) {
|
|
84
|
+
const match = existingByName.get(trim(project.name).toLowerCase());
|
|
85
|
+
if (match) remoteToLocal.set(project.id, match.id);
|
|
86
|
+
}
|
|
87
|
+
const localProjectId = (remoteId) => remoteToLocal.get(remoteId) ?? remoteId;
|
|
88
|
+
|
|
89
|
+
const seen = new Set(
|
|
90
|
+
existingPlans.map((plan) => normalizeKey(plan.project?.name ?? existingProjectNames.get(plan.project_id) ?? '', plan.title)),
|
|
91
|
+
);
|
|
92
|
+
const knownProjectIds = new Set(existingProjects.map((project) => project.id));
|
|
93
|
+
|
|
94
|
+
const wanted = remotePlans.filter((plan) => includeClosed || OPEN_STATUSES.has(plan.status));
|
|
95
|
+
const plans = [];
|
|
96
|
+
for (const plan of wanted) {
|
|
97
|
+
const projectId = localProjectId(plan.project_id);
|
|
98
|
+
const name = projectName.get(plan.project_id) ?? '未归档';
|
|
99
|
+
const key = normalizeKey(name, plan.title);
|
|
100
|
+
if (seen.has(key)) continue;
|
|
101
|
+
seen.add(key);
|
|
102
|
+
plans.push({ ...codexPlanToImport(plan), project_id: projectId });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const projects = [];
|
|
106
|
+
for (const plan of plans) {
|
|
107
|
+
if (projects.some((project) => project.id === plan.project_id)) continue;
|
|
108
|
+
if (knownProjectIds.has(plan.project_id)) continue;
|
|
109
|
+
const project = remoteProjects.find((item) => localProjectId(item.id) === plan.project_id);
|
|
110
|
+
projects.push({
|
|
111
|
+
id: plan.project_id,
|
|
112
|
+
name: project?.name ?? '未归档',
|
|
113
|
+
root_path: trim(project?.root_path) || 'inbox',
|
|
114
|
+
created_at: project?.created_at ?? plan.created_at,
|
|
115
|
+
updated_at: project?.updated_at ?? plan.updated_at,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return { projects, plans, checked: wanted.length, skipped: wanted.length - plans.length };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function fetchJson(url, { token = '', timeoutMs = FETCH_TIMEOUT_MS, fetchImpl = globalThis.fetch } = {}) {
|
|
123
|
+
if (typeof fetchImpl !== 'function') throw new Error('当前运行时没有 fetch');
|
|
124
|
+
const controller = new AbortController();
|
|
125
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
126
|
+
try {
|
|
127
|
+
const response = await fetchImpl(url, {
|
|
128
|
+
headers: token ? { 'x-sync-token': token } : {},
|
|
129
|
+
signal: controller.signal,
|
|
130
|
+
});
|
|
131
|
+
if (!response.ok) throw new Error(`${url} → HTTP ${response.status}`);
|
|
132
|
+
return await response.json();
|
|
133
|
+
} finally {
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function alignFromCodex(store, options = {}) {
|
|
139
|
+
const source = trim(options.source).replace(/\/+$/, '');
|
|
140
|
+
if (!source) throw new Error('没有配置 Codex 版地址');
|
|
141
|
+
const token = trim(options.token);
|
|
142
|
+
const includeClosed = options.include_closed === true;
|
|
143
|
+
const fetchOptions = { token, timeoutMs: options.timeoutMs, fetchImpl: options.fetchImpl };
|
|
144
|
+
|
|
145
|
+
const [remoteProjects, remotePlans] = await Promise.all([
|
|
146
|
+
fetchJson(`${source}/api/projects`, fetchOptions),
|
|
147
|
+
fetchJson(`${source}/api/plans`, fetchOptions),
|
|
148
|
+
]);
|
|
149
|
+
const snapshot = store.exportAll();
|
|
150
|
+
const payload = buildAlignPayload({
|
|
151
|
+
remoteProjects: Array.isArray(remoteProjects) ? remoteProjects : remoteProjects.projects ?? [],
|
|
152
|
+
remotePlans: Array.isArray(remotePlans) ? remotePlans : remotePlans.plans ?? [],
|
|
153
|
+
existingProjects: snapshot.projects ?? [],
|
|
154
|
+
existingPlans: snapshot.plans ?? [],
|
|
155
|
+
includeClosed,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const at = new Date().toISOString();
|
|
159
|
+
if (payload.plans.length === 0) {
|
|
160
|
+
return { at, source, checked: payload.checked, imported: 0, projects: 0, skipped: payload.skipped };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const result = await store.importAll(
|
|
164
|
+
{ format: 'dsh-project-recall', version: 1, projects: payload.projects, plans: payload.plans },
|
|
165
|
+
{ mode: 'merge' },
|
|
166
|
+
);
|
|
167
|
+
return {
|
|
168
|
+
at,
|
|
169
|
+
source,
|
|
170
|
+
checked: payload.checked,
|
|
171
|
+
imported: result.plans?.imported ?? payload.plans.length,
|
|
172
|
+
projects: result.projects?.imported ?? payload.projects.length,
|
|
173
|
+
skipped: payload.skipped,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 按偏好里的 sync 配置定时对齐:启动后 15 秒试一次,之后每分钟看一眼是否到点。
|
|
179
|
+
* 失败只记日志,不打扰界面;成功且真有新增时才会写一条 info 日志。
|
|
180
|
+
*/
|
|
181
|
+
export function startAutoSync(ctx, storePromise, options = {}) {
|
|
182
|
+
const logger = options.logger ?? ctx.logger;
|
|
183
|
+
const tickMs = options.tickMs ?? 60_000;
|
|
184
|
+
const initialDelayMs = options.initialDelayMs ?? 15_000;
|
|
185
|
+
let interval;
|
|
186
|
+
let boot;
|
|
187
|
+
|
|
188
|
+
const tick = async ({ force = false } = {}) => {
|
|
189
|
+
if (tick.running) return { skipped: 'running' };
|
|
190
|
+
tick.running = true;
|
|
191
|
+
try {
|
|
192
|
+
const store = await storePromise;
|
|
193
|
+
const config = store.getPreferences().sync ?? {};
|
|
194
|
+
const source = trim(config.source);
|
|
195
|
+
if (!config.enabled || !source) return { skipped: 'disabled' };
|
|
196
|
+
const minutes = Math.max(5, Number(config.interval_minutes) || DEFAULT_INTERVAL_MINUTES);
|
|
197
|
+
const last = config.last_run_at ? Date.parse(config.last_run_at) : Number.NaN;
|
|
198
|
+
if (!force && Number.isFinite(last) && Date.now() - last < minutes * 60_000) return { skipped: 'waiting' };
|
|
199
|
+
if (!force && !withinHours(new Date(), config.hours)) return { skipped: 'outside-hours' };
|
|
200
|
+
try {
|
|
201
|
+
const result = await alignFromCodex(store, {
|
|
202
|
+
source,
|
|
203
|
+
token: config.token,
|
|
204
|
+
include_closed: config.include_closed === true,
|
|
205
|
+
});
|
|
206
|
+
await store.recordSyncRun({ ...result, error: '' });
|
|
207
|
+
if (result.imported > 0) {
|
|
208
|
+
logger?.info?.(`[project-memory] 自动对齐:从 Codex 版补进 ${result.imported} 条计划`);
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
const message = String(error?.message || error);
|
|
213
|
+
await store.recordSyncRun({ at: new Date().toISOString(), imported: 0, error: message });
|
|
214
|
+
logger?.warn?.(`[project-memory] 自动对齐失败:${message}`);
|
|
215
|
+
return { error: message };
|
|
216
|
+
}
|
|
217
|
+
} finally {
|
|
218
|
+
tick.running = false;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const start = () => {
|
|
223
|
+
boot = setTimeout(() => {
|
|
224
|
+
void tick();
|
|
225
|
+
interval = setInterval(() => void tick(), tickMs);
|
|
226
|
+
if (typeof interval.unref === 'function') interval.unref();
|
|
227
|
+
}, initialDelayMs);
|
|
228
|
+
if (typeof boot.unref === 'function') boot.unref();
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const dispose = () => {
|
|
232
|
+
clearTimeout(boot);
|
|
233
|
+
clearInterval(interval);
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
if (typeof ctx.effect === 'function') {
|
|
237
|
+
ctx.effect(() => {
|
|
238
|
+
start();
|
|
239
|
+
return dispose;
|
|
240
|
+
});
|
|
241
|
+
} else {
|
|
242
|
+
start();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return { tick, dispose };
|
|
246
|
+
}
|
package/lib/tools.js
CHANGED
|
@@ -264,6 +264,37 @@ export function registerTools(ctx, store, options = {}) {
|
|
|
264
264
|
},
|
|
265
265
|
),
|
|
266
266
|
|
|
267
|
+
defineTool(
|
|
268
|
+
'project_memory_sync_now',
|
|
269
|
+
'Align 续忆 with the Codex edition (local SQLite / NAS 4318 service) right now: pull plans that are missing here, dedupe by project name + title, only add. Use when the user asks why the two panels differ, or says 对齐 / 同步一下 / sync the backlog.',
|
|
270
|
+
{
|
|
271
|
+
type: 'object',
|
|
272
|
+
properties: {
|
|
273
|
+
source: { type: 'string', description: 'Base URL of the Codex edition, e.g. http://192.168.5.8:4318. Defaults to the saved setting.' },
|
|
274
|
+
include_closed: { type: 'boolean', description: 'Also pull done / dropped plans.' },
|
|
275
|
+
},
|
|
276
|
+
additionalProperties: false,
|
|
277
|
+
},
|
|
278
|
+
async (args) => {
|
|
279
|
+
if (typeof options.sync !== 'function') {
|
|
280
|
+
throw new Error('这个运行时没有启用对齐服务');
|
|
281
|
+
}
|
|
282
|
+
if (typeof args.source === 'string' && args.source.trim() !== '') {
|
|
283
|
+
await store.updatePreferences({ sync: { enabled: true, source: args.source } });
|
|
284
|
+
}
|
|
285
|
+
const preferences = await store.getPreferences();
|
|
286
|
+
if (!preferences.sync?.source) {
|
|
287
|
+
throw new Error('先填 Codex 版地址,例如 http://192.168.5.8:4318');
|
|
288
|
+
}
|
|
289
|
+
if (args.include_closed === true) {
|
|
290
|
+
await store.updatePreferences({ sync: { include_closed: true } });
|
|
291
|
+
}
|
|
292
|
+
const result = await options.sync({ force: true });
|
|
293
|
+
const after = await store.getPreferences();
|
|
294
|
+
return { result, sync: after.sync ?? null };
|
|
295
|
+
},
|
|
296
|
+
),
|
|
297
|
+
|
|
267
298
|
defineTool(
|
|
268
299
|
'project_memory_update_plan',
|
|
269
300
|
'Update a saved plan. Use for status changes, rescheduling review, editing title/details, or marking done/dropped after the user confirms.',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-project-recall",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "续忆 — 为 DeepSeek Harness 保存跨会话的延后计划,并在合适的时候提醒你回来继续。",
|
|
@@ -50,16 +50,17 @@
|
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"test": "node --test",
|
|
53
|
-
"check": "node --check index.js && node --check client.js && node --check lib/model.js && node --check lib/store.js && node --check lib/http.js && node --check lib/tools.js && node --check lib/dispatch.js",
|
|
53
|
+
"check": "node --check index.js && node --check client.js && node --check lib/model.js && node --check lib/store.js && node --check lib/http.js && node --check lib/tools.js && node --check lib/dispatch.js && node --check lib/sync.js && node --check scripts/import-from-nas.mjs",
|
|
54
54
|
"import:nas": "node scripts/import-from-nas.mjs"
|
|
55
55
|
},
|
|
56
56
|
"repository": {
|
|
57
57
|
"type": "git",
|
|
58
|
-
"url": "git+https://github.com/
|
|
58
|
+
"url": "git+https://github.com/hseons/dsh-backup.git",
|
|
59
|
+
"directory": "local-plugins/dsh-project-recall"
|
|
59
60
|
},
|
|
60
|
-
"homepage": "https://github.com/
|
|
61
|
+
"homepage": "https://github.com/hseons/dsh-backup/tree/main/local-plugins/dsh-project-recall#readme",
|
|
61
62
|
"bugs": {
|
|
62
|
-
"url": "https://github.com/
|
|
63
|
+
"url": "https://github.com/hseons/dsh-backup/issues"
|
|
63
64
|
},
|
|
64
65
|
"publishConfig": {
|
|
65
66
|
"access": "public"
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* 合并进 DSH 续忆(dsh-project-recall)的 project_memory 存储域。
|
|
5
5
|
*
|
|
6
6
|
* 默认只做 dry-run,打印「会新增哪些」,加 --apply 才真的写。
|
|
7
|
+
* 插件里的自动对齐用的是同一套去重逻辑(lib/sync.js),所以两边结果一致。
|
|
7
8
|
*
|
|
8
9
|
* node scripts/import-from-nas.mjs # 看差异
|
|
9
10
|
* node scripts/import-from-nas.mjs --apply # 真导入
|
|
@@ -11,7 +12,7 @@
|
|
|
11
12
|
* node scripts/import-from-nas.mjs --source http://127.0.0.1:4318 --target http://127.0.0.1:52497
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
import { buildAlignPayload, fetchJson } from '../lib/sync.js';
|
|
15
16
|
|
|
16
17
|
function parseArgs(argv) {
|
|
17
18
|
const options = {
|
|
@@ -41,84 +42,47 @@ function parseArgs(argv) {
|
|
|
41
42
|
return options;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
function trim(value) {
|
|
45
|
-
return String(value ?? '').trim();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function key(projectName, title) {
|
|
49
|
-
return `${trim(projectName).toLowerCase()}::${trim(title).replace(/\s+/g, ' ').toLowerCase()}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
45
|
async function getJson(url, headers = {}) {
|
|
53
46
|
const response = await fetch(url, { headers });
|
|
54
|
-
if (!response.ok) {
|
|
55
|
-
throw new Error(`${url} → HTTP ${response.status}`);
|
|
56
|
-
}
|
|
47
|
+
if (!response.ok) throw new Error(`${url} → HTTP ${response.status}`);
|
|
57
48
|
return response.json();
|
|
58
49
|
}
|
|
59
50
|
|
|
60
51
|
async function main() {
|
|
61
52
|
const options = parseArgs(process.argv.slice(2));
|
|
62
|
-
const headers = options.token ? { 'x-sync-token': options.token } : {};
|
|
63
53
|
|
|
64
54
|
console.log(`源(Codex 版): ${options.source}`);
|
|
65
55
|
console.log(`目标(DSH 续忆): ${options.target}`);
|
|
66
56
|
|
|
67
57
|
const [remoteProjects, remotePlans, exported] = await Promise.all([
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
fetchJson(`${options.source}/api/projects`, { token: options.token }),
|
|
59
|
+
fetchJson(`${options.source}/api/plans`, { token: options.token }),
|
|
70
60
|
// /plans 只返回未结束的计划,去重要用 /export 的全量(含 done/dropped)
|
|
71
61
|
getJson(`${options.target}/project-memory/api/export`, { origin: options.target }),
|
|
72
62
|
]);
|
|
73
63
|
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
64
|
+
const payload = buildAlignPayload({
|
|
65
|
+
remoteProjects: Array.isArray(remoteProjects) ? remoteProjects : remoteProjects.projects ?? [],
|
|
66
|
+
remotePlans: Array.isArray(remotePlans) ? remotePlans : remotePlans.plans ?? [],
|
|
67
|
+
existingProjects: exported.projects ?? [],
|
|
68
|
+
existingPlans: exported.plans ?? [],
|
|
69
|
+
includeClosed: options.all,
|
|
70
|
+
});
|
|
80
71
|
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
console.log(
|
|
73
|
+
`源里 ${payload.checked} 条(${options.all ? '全部状态' : '仅未结束'}),DSH 侧现有 ${(exported.plans ?? []).length} 条。`,
|
|
83
74
|
);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const wanted = plans.filter((plan) => options.all || OPEN_STATUSES.has(plan.status));
|
|
87
|
-
const fresh = [];
|
|
88
|
-
for (const plan of wanted) {
|
|
89
|
-
const name = projectName.get(plan.project_id) ?? '未归档';
|
|
90
|
-
const planKey = key(name, plan.title);
|
|
91
|
-
if (seen.has(planKey)) continue;
|
|
92
|
-
seen.add(planKey);
|
|
93
|
-
fresh.push({ plan, name });
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const wantedProjects = new Map();
|
|
97
|
-
for (const { plan, name } of fresh) {
|
|
98
|
-
const project = projects.find((item) => item.id === plan.project_id);
|
|
99
|
-
const id = project?.id ?? 'inbox';
|
|
100
|
-
if (knownProjectIds.has(id)) continue;
|
|
101
|
-
wantedProjects.set(id, {
|
|
102
|
-
id,
|
|
103
|
-
name: name || '未归档',
|
|
104
|
-
root_path: project?.root_path ?? 'inbox',
|
|
105
|
-
created_at: project?.created_at ?? plan.created_at,
|
|
106
|
-
updated_at: project?.updated_at ?? plan.updated_at,
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
console.log(`源里 ${plans.length} 条(${options.all ? '全部状态' : '仅未结束'} ${wanted.length} 条),DSH 侧现有 ${existing.length} 条。`);
|
|
111
|
-
if (fresh.length === 0) {
|
|
75
|
+
if (payload.plans.length === 0) {
|
|
112
76
|
console.log('没有需要新增的计划,两边已经对齐。');
|
|
113
77
|
return;
|
|
114
78
|
}
|
|
115
79
|
|
|
116
|
-
console.log(`\n会新增 ${
|
|
117
|
-
for (const
|
|
118
|
-
console.log(` • [${plan.status}] ${
|
|
80
|
+
console.log(`\n会新增 ${payload.plans.length} 条:`);
|
|
81
|
+
for (const plan of payload.plans) {
|
|
82
|
+
console.log(` • [${plan.status}] ${String(plan.title).slice(0, 70)}`);
|
|
119
83
|
}
|
|
120
|
-
if (
|
|
121
|
-
console.log(`\n会补 ${
|
|
84
|
+
if (payload.projects.length > 0) {
|
|
85
|
+
console.log(`\n会补 ${payload.projects.length} 个项目:${payload.projects.map((project) => project.name).join('、')}`);
|
|
122
86
|
}
|
|
123
87
|
|
|
124
88
|
if (!options.apply) {
|
|
@@ -126,41 +90,19 @@ async function main() {
|
|
|
126
90
|
return;
|
|
127
91
|
}
|
|
128
92
|
|
|
129
|
-
const payload = {
|
|
130
|
-
format: 'dsh-project-recall',
|
|
131
|
-
version: 1,
|
|
132
|
-
projects: [...wantedProjects.values()],
|
|
133
|
-
plans: fresh.map(({ plan }) => ({
|
|
134
|
-
id: plan.id,
|
|
135
|
-
project_id: plan.project_id,
|
|
136
|
-
title: plan.title,
|
|
137
|
-
details: plan.details ?? '',
|
|
138
|
-
status: plan.status,
|
|
139
|
-
reason_deferred: plan.reason_deferred ?? '',
|
|
140
|
-
reactivation_conditions: Array.isArray(plan.reactivation_conditions) ? plan.reactivation_conditions : [],
|
|
141
|
-
tags: Array.isArray(plan.tags) ? plan.tags : [],
|
|
142
|
-
priority: plan.priority ?? 'medium',
|
|
143
|
-
effort: plan.effort ?? 'unknown',
|
|
144
|
-
source_type: plan.source_type ?? 'conversation',
|
|
145
|
-
source_ref: plan.source_ref ?? '',
|
|
146
|
-
source_excerpt: plan.source_excerpt ?? '',
|
|
147
|
-
review_at: plan.review_at ?? null,
|
|
148
|
-
last_reviewed_at: plan.last_reviewed_at ?? null,
|
|
149
|
-
completed_at: plan.completed_at ?? null,
|
|
150
|
-
created_at: plan.created_at,
|
|
151
|
-
updated_at: plan.updated_at,
|
|
152
|
-
})),
|
|
153
|
-
};
|
|
154
|
-
|
|
155
93
|
const response = await fetch(`${options.target}/project-memory/api/import`, {
|
|
156
94
|
method: 'POST',
|
|
157
95
|
headers: { 'content-type': 'application/json', origin: options.target },
|
|
158
|
-
body: JSON.stringify({
|
|
96
|
+
body: JSON.stringify({
|
|
97
|
+
format: 'dsh-project-recall',
|
|
98
|
+
version: 1,
|
|
99
|
+
projects: payload.projects,
|
|
100
|
+
plans: payload.plans,
|
|
101
|
+
mode: 'merge',
|
|
102
|
+
}),
|
|
159
103
|
});
|
|
160
104
|
const text = await response.text();
|
|
161
|
-
if (!response.ok) {
|
|
162
|
-
throw new Error(`导入失败 HTTP ${response.status}: ${text.slice(0, 300)}`);
|
|
163
|
-
}
|
|
105
|
+
if (!response.ok) throw new Error(`导入失败 HTTP ${response.status}: ${text.slice(0, 300)}`);
|
|
164
106
|
console.log(`\n导入完成: ${text}`);
|
|
165
107
|
}
|
|
166
108
|
|