@oxmumu3/dsh-project-companion 0.28.0 → 0.30.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/README.md +1 -1
- package/docs/install.md +8 -4
- package/docs/mobile-control.md +14 -0
- package/docs/roadmap.md +1 -1
- package/docs/verification.md +22 -0
- package/index.js +8 -1
- package/package.json +5 -2
- package/scripts/validate-state.mjs +68 -0
- package/src/project-pulse.js +110 -0
package/README.md
CHANGED
|
@@ -96,7 +96,7 @@ dsh plugin --profile web add @oxmumu3/dsh-project-companion
|
|
|
96
96
|
|
|
97
97
|
## 版本与数据
|
|
98
98
|
|
|
99
|
-
- 当前版本 **`0.
|
|
99
|
+
- 当前版本 **`0.30.0`**:整理项目进度时会读本机 git 的最近提交与未提交改动;`0.29.0` 新增升级前状态自查;`0.28.0` 早报失败会说明下一步。
|
|
100
100
|
- 兼容官方 Harness `0.2.0-rc.2`、`0.2.1-rc.2` 至 `rc.5`、`0.2.2-rc.1`;清单在 [compatibility.json](compatibility.json)。
|
|
101
101
|
- 数据留在本机 Harness profile 内,不经过第三方服务器;微信组件内置部分保留上游 MIT 许可,见 [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md)。
|
|
102
102
|
- 已知边界(Google 数据权限审核、flomo MAX、主动关注与手机收件的验收状态)统一写在[验证记录](docs/verification.md),本页不重复。
|
package/docs/install.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# 安装 DSH Dot 0.
|
|
1
|
+
# 安装 DSH Dot 0.30.0
|
|
2
2
|
|
|
3
3
|
## 在创造模式中安装/更新
|
|
4
4
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
请安装或更新并启用 DSH Dot。先检查现有插件,避免重复添加和降级。从 npm 官方源实时查询 @oxmumu3/dsh-project-companion 的 latest,按查到的精确版本安装或更新,不直接采用本机缓存的旧 latest。保留已有微信绑定、账号授权、资料范围、设置和其他插件,不自动读取或新建计划。核对实际版本、启用状态,再打开 DSH Dot。
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
新用户也可在「插件 → 添加插件」填写 `@oxmumu3/dsh-project-companion`(或精确版本 `@oxmumu3/dsh-project-companion@0.
|
|
11
|
+
新用户也可在「插件 → 添加插件」填写 `@oxmumu3/dsh-project-companion`(或精确版本 `@oxmumu3/dsh-project-companion@0.30.0`),安装后启用。它是一个标准插件包,已内置独立微信组件。已有未带 namespace 的历史安装应沿原包名更新;不要将两个名称当成同一个插件重复添加。
|
|
12
12
|
|
|
13
13
|
牛村木木山制作的 DeepSeek Harness 社区插件,非 DeepSeek 或 OpenAI 官方产品。本版内置微信组件,只需添加一个标准插件包,不包含 Harness 应用或 Family 家庭助手。
|
|
14
14
|
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
|
|
27
27
|
无法联网或想用离线包时,到 [Releases](https://github.com/oxmumu3/dsh-project-companion/releases) 下载对应版本的 `.tgz`,在官方管理器填写下载后的文件实际路径;无需解压、另装微信组件或运行 macOS 脚本。
|
|
28
28
|
|
|
29
|
-
[本版发布页](https://github.com/oxmumu3/dsh-project-companion/releases/tag/v0.
|
|
29
|
+
[本版发布页](https://github.com/oxmumu3/dsh-project-companion/releases/tag/v0.30.0)记录本版变更。插件包从 npm 安装,npm 页面提供 integrity 与 provenance 可核;0.24 起 release 不再附带离线包。插件尚未上架官方插件市场,npm latest 同步为本版;不要用旧版 ZIP 或第二个微信安装包代替这条单插件路径。
|
|
30
30
|
|
|
31
31
|
## 已有用户
|
|
32
32
|
|
|
@@ -68,7 +68,7 @@ Google 品牌已验证,Gmail、日历、Drive 数据权限已提交审核但
|
|
|
68
68
|
|
|
69
69
|
## 本版验证与已知问题
|
|
70
70
|
|
|
71
|
-
0.
|
|
71
|
+
0.30.0 源码全量回归 2,365 项 / 2,328 通过 / 0 失败 / 37 跳过(跳过项均需 Family 源码树,不在本包范围)。自检只读本机状态,不发外部请求;单项探测 2 秒上限,超时或抛错显示「未知」。私人外发确认只覆盖 `personal_review.run_now` 这条路径,宿主自带的 `send_wechat`、定时投递和带内容的 `remind` / `coordinate` 不经过这道门禁——插件单方面无法覆盖全部外发路径。
|
|
72
72
|
|
|
73
73
|
自动回顾仍为实验能力:自然语言 `focus` 尚未用真实模型验证实际效果,手机端呈现也未验证。历史 flomo 范围投影与模型失败未全部归因。新用户独立扫码到收件、多日与休眠恢复、真实 macOS 输入法仍分别验收。通道报告发送不等于手机收到。详见[验证记录](verification.md)。
|
|
74
74
|
|
|
@@ -81,3 +81,7 @@ Google 品牌已验证,Gmail、日历、Drive 数据权限已提交审核但
|
|
|
81
81
|
## 微信首次配对
|
|
82
82
|
|
|
83
83
|
使用本人微信扫码并在手机确认后,内置组件直接锁定该账号;随后发送“你好”只是开始聊天,不参与抢先认领。入站白名单留空时仅允许扫码确认账号;填写后还会施加额外限制。无法确认身份或旧绑定冲突时,电脑微信设置页会给出恢复提示。重新扫码会保留原会话与归属于原账号的队列,本身不触发补发。独立安装的第三方微信组件保留其自身配对策略。
|
|
84
|
+
|
|
85
|
+
## 升级前自查
|
|
86
|
+
|
|
87
|
+
跑 `node scripts/validate-state.mjs`:只读本机状态,用当前包自带的 schema 逐个解析,列出不兼容的域与字段;返回 0 兼容、1 有不兼容、2 路径不存在。不启动 Host,不改文件。
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# 在微信继续当前任务
|
|
2
|
+
|
|
3
|
+
手机端能停、能续、能答,靠的是两个插件都在:家庭助手(`@oxmumu3/dsh-family-assistant`)提供手机入口,Dot 注册任务范围。只装 Dot 时这部分不启用,电脑端功能不受影响;`health` 的「手机控制」一行会说明当前是哪种情况。
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
直接告诉 Dot 要继续哪个项目、做什么;沿用原生会话和原结果记录。
|
|
7
|
+
|
|
8
|
+
- 问“任务状态”或“现在到哪了”:查询当前处理对象,不新建执行。
|
|
9
|
+
- 说“停一下”:请求停止当前任务,并清掉同一会话尚未执行的本人输入。保存或已发出的内容会保留。
|
|
10
|
+
- 多项任务并行时,回复“任务状态:项目名称”或“停止:项目名称”。
|
|
11
|
+
- 收到补充问题后,回复选项编号或文字;多个问题用显示的短码和题号。宿主转入稍后回答时,原短码仍在本次手机等待窗口内有效,回答接回原问题。
|
|
12
|
+
- 权限操作要按消息明确回复“同意本次 短码”或“拒绝 短码”;不会把“好”当批准,也不会记为永久批准。
|
|
13
|
+
|
|
14
|
+
窗口最长10分钟。过期、重绑、离线、停止、卸载或执行身份不明时,不自动继续。会话混入其他来源时停止自动回传,避免把不同任务内容串在一起。结果仍走原回流和验收机制;接口接受与手机实收分别记录。
|
package/docs/roadmap.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 当前收尾 · 0.24.2 公开版本
|
|
2
2
|
|
|
3
|
-
最新公开版本 0.28.0:早报授权失败时说明下一步(换绑要重设、离线下次再试),自检同步提醒;0.27.0:自检新增「手机控制」一行并在自检时补一次注册,手机控制两边是否接通一眼可见;0.26.0:并入手机端任务控制的 Dot 侧(`mobileTaskScope` / `mobileQuestionState` / 桥接注册),等家庭助手暴露接口后即可用;`0.25.0`:补上滴答账户级只读搜索(`search_task` / `search` / `list_all_tasks`);此前 0.24.2:项目工具收敛到 `look`/`manage` 两个入口,新增自检、私人外发确认与回顾关注点,首次打开有示例卡,交办记录可复用。
|
|
3
|
+
最新公开版本 0.30.0:项目简报先读本机 git 的真实变化(分支、最近提交、未提交改动),为按变化推送打底;0.29.0:新增升级前状态自查脚本;0.28.0:早报授权失败时说明下一步(换绑要重设、离线下次再试),自检同步提醒;0.27.0:自检新增「手机控制」一行并在自检时补一次注册,手机控制两边是否接通一眼可见;0.26.0:并入手机端任务控制的 Dot 侧(`mobileTaskScope` / `mobileQuestionState` / 桥接注册),等家庭助手暴露接口后即可用;`0.25.0`:补上滴答账户级只读搜索(`search_task` / `search` / `list_all_tasks`);此前 0.24.2:项目工具收敛到 `look`/`manage` 两个入口,新增自检、私人外发确认与回顾关注点,首次打开有示例卡,交办记录可复用。
|
|
4
4
|
|
|
5
5
|
下一步按影响排序:
|
|
6
6
|
|
package/docs/verification.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
# 0.30.0 验证增量
|
|
2
|
+
|
|
3
|
+
2026-11-09,项目简报新增「最近变化」一层的证据来自本机 git。
|
|
4
|
+
|
|
5
|
+
- `node --max-old-space-size=256 --test --test-concurrency=1 test/*.test.mjs`:2,382 项 / 2,345 通过 / 0 失败 / 37 跳过(0.29.0 基线 2,376 / 2,339 / 0 / 37)。
|
|
6
|
+
- 新增 6 条用例:命令面只包含 `git -C <root> rev-parse/symbolic-ref/log/status` 的只读调用;摘要含分支、提交与未提交改动;非仓库返回明确原因;单条命令失败标为不可用而不是整体失败;未提交改动超 200 条截断并明说;空根目录直接拒绝。
|
|
7
|
+
- 未做真实 git 仓库的集成测试:用例用注入的命令执行器,避免依赖 CI 的 git 版本;真实仓库行为等价于同一组只读命令。
|
|
8
|
+
|
|
9
|
+
**没有验证的**:20 个项目同时读脉搏的耗时上限;本轮只做了单项目读取与上限计数。
|
|
10
|
+
|
|
11
|
+
# 0.29.0 验证增量
|
|
12
|
+
|
|
13
|
+
2026-11-09,新增升级前状态自查脚本 `scripts/validate-state.mjs`。
|
|
14
|
+
|
|
15
|
+
- `node --max-old-space-size=256 --test --test-concurrency=1 test/*.test.mjs`:2,376 项 / 2,339 通过 / 0 失败 / 37 跳过(0.28.0 基线 2,372 / 2,335 / 0 / 37)。
|
|
16
|
+
- 新增 4 条用例:合法状态返回 0 且逐域「兼容」、多出未知字段返回 1 并点名域与字段、目录不存在返回 2、无 schema 的域标「未核对」。
|
|
17
|
+
- 本机实跑:用 0.28.0 的自带 schema 解开 7 个域,0 个不兼容;三个未导出 schema 的域标为未核对。这是重启前的补充证据,不替代真实 Host 激活。
|
|
18
|
+
|
|
19
|
+
**隔离目录里的全新安装(追加)**:在空目录 `npm install @oxmumu3/dsh-project-companion@0.29.0`,2 分钟后装上 24 个包;`index.js` 可导入,导出 `Config/apply/inject/installCompanion/name`;`cordis.patch.yml` 与内置桥 `vendor/dsh-wechat`(0.11.1-dot.3-embedded.2)都在;包内 `scripts/validate-state.mjs` 直接跑通,读本机 7 个域 0 个不兼容。这只证明发布产物能被新用户装上并加载,不等于真实 Host 里已激活。
|
|
20
|
+
|
|
21
|
+
**没有验证的**:脚本在激活失败的真实场景里的表现(只有一处历史失败,已从存储中清理)。
|
|
22
|
+
|
|
1
23
|
# 0.28.0 验证增量
|
|
2
24
|
|
|
3
25
|
2026-11-09,早报授权失败时给出下一步。触发证据来自本机 `~/.dsh/storages/project_companion_news.json`:11-06 的 `NEWS_AUTHORIZATION_CHANGED/owner_offline`、11-07 与 11-08 的 `NEWS_AUTHORIZATION_CHANGED/owner_changed`。
|
package/index.js
CHANGED
|
@@ -14,6 +14,7 @@ import {createFamilyProjectAdmission} from './src/family-project-admission.js';
|
|
|
14
14
|
import {createTaskResults,taskResultsRowSchema} from './src/task-results.js';
|
|
15
15
|
import {readWechatBinding,readWechatRoute,readWechatSetup} from './src/wechat-route.js';
|
|
16
16
|
import {createProjectMemory} from './src/project-memory.js';
|
|
17
|
+
import {createProjectPulse,summarize as summarizePulse} from './src/project-pulse.js';
|
|
17
18
|
import {createProjectFollowup} from './src/project-followup.js';
|
|
18
19
|
import {createOnboarding,registerOnboardingRoute} from './src/onboarding.js';
|
|
19
20
|
import {createWelcomeGuard} from './src/welcome-guard.js';
|
|
@@ -158,7 +159,7 @@ export async function installCompanion(ctx,options={}){
|
|
|
158
159
|
let disposed=false;
|
|
159
160
|
const coordinates=new Set();
|
|
160
161
|
let lastRuntimeError='';
|
|
161
|
-
let memory,followup,onboarding,welcomeGuard,contextRoute,workbench,workbenchActions,projectUpdates,projectUpdateModel,contextLoop,context,familyDelegate,familyContext,personalReview,personalReviewModel;
|
|
162
|
+
let projectPulse,memory,followup,onboarding,welcomeGuard,contextRoute,workbench,workbenchActions,projectUpdates,projectUpdateModel,contextLoop,context,familyDelegate,familyContext,personalReview,personalReviewModel;
|
|
162
163
|
// 污点标记只活在本进程内存里;这次装载从干净状态开始,不继承上一次运行的标记。
|
|
163
164
|
clearPrivateReads();
|
|
164
165
|
const lifecycle=new AbortController();
|
|
@@ -415,6 +416,7 @@ export async function installCompanion(ctx,options={}){
|
|
|
415
416
|
const noteError=error=>{lastRuntimeError=String(error?.message??error).slice(0,200);};
|
|
416
417
|
const authorizeRoot=async root=>(await service.coordinationTarget(root))?.root===root;
|
|
417
418
|
const familyRead=createFamilyReadAdapter({getBridge:getFamilyBridge,isCurrent:scope=>familyDelegate.current(scope),authorizeRoot,recheck:scope=>familyDelegate.recheck(scope)});
|
|
419
|
+
projectPulse=createProjectPulse({...(options.clock?{clock:options.clock}:{})});
|
|
418
420
|
memory=createProjectMemory({authorizeRoot,...(options.clock?{clock:options.clock}:{}),...(options.hostTimeZone?{hostTimeZone:options.hostTimeZone}:{})});
|
|
419
421
|
// Personal review is Owner-wide; it never borrows a synthetic project or a
|
|
420
422
|
// one-project follow-up capability. Persisted runs re-enter the exact grant.
|
|
@@ -924,7 +926,12 @@ export async function installCompanion(ctx,options={}){
|
|
|
924
926
|
for(const project of projects){
|
|
925
927
|
try{const snapshot=await memory.read({root:project.root,sessionId:owner(ctx,agent).sessionId});if(snapshot.goals.length||snapshot.entries.length)context.push(`${project.label}\n${snapshot.text}`);}
|
|
926
928
|
catch(error){context.push(`${project.label}:项目记忆读取失败,内容未知。${String(error?.message??error).slice(0,120)}`);}
|
|
929
|
+
if(projects.indexOf(project)<20){
|
|
930
|
+
try{const pulse=summarizePulse(await projectPulse.capture(project.root));if(pulse)context.push(`${project.label} 最近变化\n${pulse}`);}
|
|
931
|
+
catch{context.push(`${project.label} 最近变化:本机读取失败,状态未知。`);}
|
|
932
|
+
}
|
|
927
933
|
}
|
|
934
|
+
if(projects.length>20)context.push(`另外 ${projects.length-20} 个项目本轮没有读取最近变化。`);
|
|
928
935
|
if(context.length)result.memoryContext=context.join('\n\n').slice(0,10000);
|
|
929
936
|
}
|
|
930
937
|
syncTimer();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxmumu3/dsh-project-companion",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"description": "DSH Dot: WeChat personal agent, project memory and proactive follow-up for DeepSeek Harness",
|
|
5
5
|
"author": "牛村木木山",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,7 +52,10 @@
|
|
|
52
52
|
"docs/continuous-agent.md",
|
|
53
53
|
"scripts/compatibility.mjs",
|
|
54
54
|
"compatibility.json",
|
|
55
|
-
"scripts/official-channels.mjs"
|
|
55
|
+
"scripts/official-channels.mjs",
|
|
56
|
+
"scripts/validate-state.mjs",
|
|
57
|
+
"docs/mobile-control.md",
|
|
58
|
+
"src/project-pulse.js"
|
|
56
59
|
],
|
|
57
60
|
"scripts": {
|
|
58
61
|
"test": "node --max-old-space-size=128 --test --test-concurrency=1 test/*.test.mjs",
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 升级前自查:用当前安装包里的 schema 解析本机已存的状态。
|
|
3
|
+
//
|
|
4
|
+
// 0.24.2 升级时踩过一次「旧状态多了个字段,插件激活失败」。这个脚本把同样的
|
|
5
|
+
// 检查提前到升级之前:只读本机 JSON,不启动 Host、不写任何文件。
|
|
6
|
+
//
|
|
7
|
+
// 用法:node scripts/validate-state.mjs [状态目录]
|
|
8
|
+
// 默认读 $DSH_HOME/storages,未设置时读 ~/.dsh/storages。
|
|
9
|
+
import {readFileSync, existsSync, readdirSync} from 'node:fs';
|
|
10
|
+
import {homedir} from 'node:os';
|
|
11
|
+
import {join, resolve} from 'node:path';
|
|
12
|
+
import {fileURLToPath, pathToFileURL} from 'node:url';
|
|
13
|
+
|
|
14
|
+
const root = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
|
15
|
+
const storages = resolve(process.argv[2] ?? join(process.env.DSH_HOME ?? join(homedir(), '.dsh'), 'storages'));
|
|
16
|
+
|
|
17
|
+
// 只有导出了 schema 的域才能在这里核对;其余域在升级后由 Host 自己解析。
|
|
18
|
+
const DOMAINS = {
|
|
19
|
+
project_companion_goals: ['goal-board-store.js', 'goalBoardRowSchema'],
|
|
20
|
+
project_companion_news: ['news-service.js', 'newsRowSchema'],
|
|
21
|
+
project_companion_personal_review: ['personal-review.js', 'personalReviewRowSchema'],
|
|
22
|
+
project_companion_context: ['context-service.js', 'contextRowSchema'],
|
|
23
|
+
project_companion_context_loop: ['context-loop.js', 'contextLoopRowSchema'],
|
|
24
|
+
project_companion_task_results: ['task-results.js', 'taskResultsRowSchema'],
|
|
25
|
+
project_companion_workspace_access: ['workspace-access.js', 'workspaceAccessRowSchema'],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
if (!existsSync(storages)) {
|
|
29
|
+
console.error(`找不到状态目录:${storages}`);
|
|
30
|
+
process.exit(2);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let checked = 0;
|
|
34
|
+
let failed = 0;
|
|
35
|
+
for (const [domain, [file, exportName]] of Object.entries(DOMAINS)) {
|
|
36
|
+
const path = join(storages, `${domain}.json`);
|
|
37
|
+
if (!existsSync(path)) continue;
|
|
38
|
+
const data = JSON.parse(readFileSync(path, 'utf8'));
|
|
39
|
+
const record = data?.tables?.state?.profile;
|
|
40
|
+
if (record === undefined) {
|
|
41
|
+
console.log(`${domain}:跳过(没有 profile 记录)`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
let schema;
|
|
45
|
+
try {
|
|
46
|
+
schema = (await import(pathToFileURL(join(root, 'src', file)).href))[exportName];
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.log(`${domain}:跳过(本包没有 ${exportName}:${error?.code ?? error?.message})`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
checked += 1;
|
|
52
|
+
const result = schema.safeParse(record);
|
|
53
|
+
if (result.success) {
|
|
54
|
+
console.log(`${domain}:兼容`);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
failed += 1;
|
|
58
|
+
console.log(`${domain}:不兼容`);
|
|
59
|
+
for (const issue of result.error.issues.slice(0, 3)) {
|
|
60
|
+
console.log(` - ${issue.path.join('.') || '(root)'}:${issue.message}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const unreadable = readdirSync(storages).filter(name => name.startsWith('project_companion') && name.endsWith('.json') && !Object.keys(DOMAINS).includes(name.replace(/\.json$/, '')));
|
|
65
|
+
for (const name of unreadable) console.log(`${name.replace(/\.json$/, '')}:未核对(本包不导出该域的 schema)`);
|
|
66
|
+
|
|
67
|
+
console.log(`\n核对 ${checked} 个域,${failed} 个不兼容。`);
|
|
68
|
+
process.exit(failed ? 1 : 0);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {execFile} from 'node:child_process';
|
|
2
|
+
import {promisify} from 'node:util';
|
|
3
|
+
|
|
4
|
+
/** 项目脉搏:只读的判断"这个项目最近发生了什么"。
|
|
5
|
+
*
|
|
6
|
+
* 只跑本机 git 读取命令(不联网、不写索引、不加锁),把结果压成一段可进上下文的
|
|
7
|
+
* 中文摘要。不是 git 仓库、命令超时或失败时返回明确的不可用状态,不猜、不伪造进度。
|
|
8
|
+
*/
|
|
9
|
+
const runFile = promisify(execFile);
|
|
10
|
+
const GIT_ENV = {LC_ALL: 'C', GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0'};
|
|
11
|
+
const MAX_COMMITS = 5;
|
|
12
|
+
const MAX_DIRTY_LINES = 200;
|
|
13
|
+
const MAX_SUBJECT = 120;
|
|
14
|
+
const MAX_BRANCH = 120;
|
|
15
|
+
const TIMEOUT_MS = 4000;
|
|
16
|
+
const SUMMARY_MAX = 700;
|
|
17
|
+
|
|
18
|
+
const clean = (value, max) => String(value ?? '')
|
|
19
|
+
.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, '')
|
|
20
|
+
.replace(/\s+/g, ' ')
|
|
21
|
+
.trim()
|
|
22
|
+
.slice(0, max);
|
|
23
|
+
|
|
24
|
+
async function defaultRun(args, {timeout = TIMEOUT_MS} = {}) {
|
|
25
|
+
const {stdout} = await runFile('git', args, {
|
|
26
|
+
encoding: 'utf8', maxBuffer: 65536, timeout, env: {...process.env, ...GIT_ENV},
|
|
27
|
+
});
|
|
28
|
+
return stdout;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} options
|
|
33
|
+
* @param {(args: string[], options?: object) => Promise<string>} [options.run] 已带 `git -C <root>` 前缀的命令执行器,测试可注入
|
|
34
|
+
* @param {() => Date} [options.clock]
|
|
35
|
+
*/
|
|
36
|
+
export function createProjectPulse({run = defaultRun, clock = () => new Date()} = {}) {
|
|
37
|
+
if (typeof run !== 'function' || typeof clock !== 'function') throw new Error('Project pulse requires git runner and clock');
|
|
38
|
+
|
|
39
|
+
async function capture(root) {
|
|
40
|
+
if (typeof root !== 'string' || !root.trim() || root.length > 4096) throw new Error('Project pulse requires a project root');
|
|
41
|
+
const at = clock().toISOString();
|
|
42
|
+
const git = (...args) => run(['-C', root, ...args]);
|
|
43
|
+
|
|
44
|
+
let inside;
|
|
45
|
+
try {
|
|
46
|
+
inside = (await git('rev-parse', '--is-inside-work-tree')).trim();
|
|
47
|
+
} catch {
|
|
48
|
+
return {at, repo: false, reason: 'not_a_repository'};
|
|
49
|
+
}
|
|
50
|
+
if (inside !== 'true') return {at, repo: false, reason: 'not_a_repository'};
|
|
51
|
+
|
|
52
|
+
const read = async (promise) => {
|
|
53
|
+
try { return {ok: true, value: await promise}; } catch { return {ok: false}; }
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const [branch, head, log, status] = await Promise.all([
|
|
57
|
+
read(git('symbolic-ref', '--short', 'HEAD')),
|
|
58
|
+
read(git('rev-parse', '--short', 'HEAD')),
|
|
59
|
+
read(git('log', `-${MAX_COMMITS}`, '--date=short', '--pretty=format:%h%x1f%ad%x1f%s')),
|
|
60
|
+
read(git('status', '--porcelain')),
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
const commits = log.ok
|
|
64
|
+
? log.value.split('\n').filter(Boolean).slice(0, MAX_COMMITS).map(line => {
|
|
65
|
+
const [short, date, subject] = line.split('\x1f');
|
|
66
|
+
return {short: clean(short, 16), date: clean(date, 24), subject: clean(subject, MAX_SUBJECT)};
|
|
67
|
+
}).filter(row => row.short)
|
|
68
|
+
: [];
|
|
69
|
+
|
|
70
|
+
let dirty = null;
|
|
71
|
+
if (status.ok) {
|
|
72
|
+
const lines = status.value.split('\n').filter(Boolean);
|
|
73
|
+
const truncated = lines.length > MAX_DIRTY_LINES;
|
|
74
|
+
const visible = lines.slice(0, MAX_DIRTY_LINES);
|
|
75
|
+
dirty = {
|
|
76
|
+
total: lines.length,
|
|
77
|
+
modified: visible.filter(line => !line.startsWith('??')).length,
|
|
78
|
+
untracked: visible.filter(line => line.startsWith('??')).length,
|
|
79
|
+
truncated,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
at,
|
|
85
|
+
repo: true,
|
|
86
|
+
branch: branch.ok && branch.value.trim() ? clean(branch.value, MAX_BRANCH) : null,
|
|
87
|
+
head: head.ok ? clean(head.value, 40) : null,
|
|
88
|
+
commits,
|
|
89
|
+
dirty,
|
|
90
|
+
unavailable: [...(branch.ok ? [] : ['branch']), ...(head.ok ? [] : ['head']), ...(log.ok ? [] : ['commits']), ...(status.ok ? [] : ['status'])],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {capture, summarize};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 把脉搏压成给模型看的一小段;没有可说的就返回空串,不凑字数。 */
|
|
98
|
+
export function summarize(pulse) {
|
|
99
|
+
if (!pulse || pulse.repo !== true) return '';
|
|
100
|
+
const parts = [];
|
|
101
|
+
if (pulse.branch) parts.push(`分支 ${pulse.branch}`);
|
|
102
|
+
if (pulse.commits?.length) {
|
|
103
|
+
parts.push(`最近 ${pulse.commits.length} 次提交:${pulse.commits.map(row => `${row.short} ${row.subject}`).join(';')}`);
|
|
104
|
+
}
|
|
105
|
+
if (pulse.dirty) {
|
|
106
|
+
parts.push(pulse.dirty.total ? `未提交改动 ${pulse.dirty.total} 个文件(修改 ${pulse.dirty.modified}、未跟踪 ${pulse.dirty.untracked}${pulse.dirty.truncated ? ',超过上限已截断' : ''})` : '工作区没有未提交改动');
|
|
107
|
+
}
|
|
108
|
+
if (pulse.unavailable?.length) parts.push(`未能读取:${pulse.unavailable.join('、')}`);
|
|
109
|
+
return parts.length ? clean(parts.join(';'), SUMMARY_MAX) : '';
|
|
110
|
+
}
|