opencode-haimati 1.1.3 → 1.1.5
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 +46 -200
- package/dist/index.js +79 -79
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
# opencode-haimati
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
为 OpenCode 提供基于文件系统的长期记忆存储和检索功能。
|
|
4
4
|
|
|
5
5
|
## 功能特性
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- ✨ 自动增强首条提示词:自动查询 `/start-work` 命令的 template 并添加到用户首条消息前
|
|
14
|
-
- 📋 自动记录会话结束:会话空闲时自动注入 `/record-memory` 命令提示用户记录
|
|
15
|
-
- 🚫 用户停止保护:用户主动停止会话时不会注入提示词
|
|
16
|
-
- 📋 日志追踪:完整的操作日志记录(按天覆盖)
|
|
17
|
-
- ⚡ 纯异步架构:所有文件操作非阻塞执行
|
|
18
|
-
- 🔐 文件锁机制:支持网络共享挂载时的多用户协调
|
|
7
|
+
- 类海马体记忆机制:模拟人脑记忆系统的分类存储
|
|
8
|
+
- 树形索引结构 + 分组书页存储
|
|
9
|
+
- 多模式搜索:序号精确查询 + 关键字模糊搜索
|
|
10
|
+
- 完整 CRUD:读取、写入、更新、删除、移动
|
|
11
|
+
- 版本并发控制:基于 version 的乐观锁
|
|
12
|
+
- 自动增强首条提示词 + 会话空闲时自动提示记录
|
|
19
13
|
|
|
20
14
|
## 安装
|
|
21
15
|
|
|
@@ -25,229 +19,81 @@ npm install -g opencode-haimati
|
|
|
25
19
|
bun add -g opencode-haimati
|
|
26
20
|
```
|
|
27
21
|
|
|
28
|
-
##
|
|
22
|
+
## 配置
|
|
29
23
|
|
|
30
|
-
|
|
24
|
+
在 `opencode.json` 中添加插件:
|
|
31
25
|
|
|
32
26
|
```json
|
|
33
27
|
{
|
|
34
28
|
"$schema": "https://opencode.ai/config.json",
|
|
35
|
-
"plugin": ["opencode-haimati"]
|
|
29
|
+
"plugin": ["opencode-haimati"]
|
|
36
30
|
}
|
|
37
31
|
```
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
在项目根目录创建 `opencode-haimati.conf` 配置文件:
|
|
33
|
+
在项目根目录创建 `opencode-haimati.conf`(可选):
|
|
42
34
|
|
|
43
35
|
```ini
|
|
44
|
-
# 海马体存储路径(可选,默认 .haimati)
|
|
45
36
|
haimati-path: .haimati
|
|
46
37
|
```
|
|
47
38
|
|
|
48
|
-
##
|
|
39
|
+
## 目录结构
|
|
49
40
|
|
|
50
41
|
```
|
|
51
42
|
.haimati/
|
|
52
|
-
├── .lock
|
|
53
|
-
├── 索引.md
|
|
54
|
-
├── next-id.txt
|
|
55
|
-
└── 书页/
|
|
56
|
-
├── 001-050/
|
|
57
|
-
│ ├── 001.md
|
|
58
|
-
│ └── 002.md
|
|
59
|
-
└── 051-100/
|
|
60
|
-
└── ...
|
|
43
|
+
├── .lock # 文件锁(支持网络共享挂载多用户协调)
|
|
44
|
+
├── 索引.md # 树形索引
|
|
45
|
+
├── next-id.txt # 下一个序号
|
|
46
|
+
└── 书页/ # 记忆内容(每50个序号一分组)
|
|
61
47
|
```
|
|
62
48
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
## 可用工具
|
|
49
|
+
## 工具
|
|
66
50
|
|
|
67
51
|
| 工具 | 说明 |
|
|
68
52
|
|------|------|
|
|
69
|
-
| `haimati_read` |
|
|
70
|
-
| `haimati_write` |
|
|
71
|
-
| `haimati_search` |
|
|
72
|
-
| `haimati_edit` |
|
|
73
|
-
| `haimati_move` |
|
|
74
|
-
| `haimati_delete` |
|
|
75
|
-
| `haimati_list` |
|
|
76
|
-
|
|
77
|
-
### 工具入参说明
|
|
78
|
-
|
|
79
|
-
#### haimati_read
|
|
80
|
-
- `query`: 序号(必填),如 "086"
|
|
81
|
-
- `offset`: 起始行号(可选,默认 1)
|
|
82
|
-
- `limit`: 最大读取行数(可选,默认 2000)
|
|
83
|
-
|
|
84
|
-
#### haimati_write
|
|
85
|
-
- `category`: 分类路径(必填),如 "xxx项目/签章" 或 "通用"
|
|
86
|
-
- `title`: 记忆标题(必填)
|
|
87
|
-
- `content`: 要写入的详细内容(必填)
|
|
88
|
-
- 注意:标题不能以 " - 序号" 格式结尾,序号由系统自动分配
|
|
89
|
-
|
|
90
|
-
#### haimati_search
|
|
91
|
-
- `keyword`: 搜索关键词(必填),支持多关键字用空格分隔
|
|
92
|
-
- `match`: 匹配模式(必填),`and`=所有关键字都匹配,`or`=任意关键字匹配
|
|
93
|
-
- `limit`: 返回结果数量限制(可选,默认 10)
|
|
94
|
-
- `offset`: 分页偏移量(可选,默认 0)
|
|
95
|
-
|
|
96
|
-
#### haimati_edit
|
|
97
|
-
- `query`: 序号(必填)
|
|
98
|
-
- `offsetBegin`: 起始行号(包括)
|
|
99
|
-
- `offsetEnd`: 结束行号(不包括)
|
|
100
|
-
- `content`: 替换内容
|
|
101
|
-
- `version`: read 时返回的 version,用于并发控制(必填)
|
|
102
|
-
|
|
103
|
-
#### haimati_delete
|
|
104
|
-
- `query`: 序号或完整路径(必填)
|
|
105
|
-
|
|
106
|
-
#### haimati_move
|
|
107
|
-
- `query`: 序号或完整路径(必填)
|
|
108
|
-
- `newCategory`: 新的分类路径(必填)
|
|
109
|
-
|
|
110
|
-
#### haimati_list
|
|
111
|
-
- `category`: 可选的分类路径
|
|
112
|
-
- `recursive`: 是否递归遍历子分类(可选,默认 false)
|
|
113
|
-
|
|
114
|
-
## 海马体三大原则
|
|
115
|
-
|
|
116
|
-
1. **信息不丢失**: 完整保留所有有价值的信息
|
|
117
|
-
2. **错误信息修正**: 不再保留已被纠正的错误信息
|
|
118
|
-
3. **拒绝冗余**: 不要重复记录相同内容
|
|
53
|
+
| `haimati_read` | 读取记忆(序号查询,支持分页) |
|
|
54
|
+
| `haimati_write` | 写入记忆(不允许覆盖已存在) |
|
|
55
|
+
| `haimati_search` | 搜索(标题/分类/内容,match必填) |
|
|
56
|
+
| `haimati_edit` | 部分修改(需 version 并发控制) |
|
|
57
|
+
| `haimati_move` | 修改分类路径 |
|
|
58
|
+
| `haimati_delete` | 删除记忆 |
|
|
59
|
+
| `haimati_list` | 列出索引 |
|
|
119
60
|
|
|
120
61
|
## 使用示例
|
|
121
62
|
|
|
122
63
|
```typescript
|
|
123
|
-
//
|
|
124
|
-
|
|
125
|
-
haimati_write({
|
|
126
|
-
category: "xxx项目/签章",
|
|
127
|
-
title: "签章服务WS通信机制", // ✓ 正确
|
|
128
|
-
// title: "签章服务WS通信机制 - 001", // ✗ 错误,会报错
|
|
129
|
-
content: "WebSocket通信采用base64编码..."
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
// 读取记忆(只支持序号查询)
|
|
133
|
-
haimati_read({ query: "086" })
|
|
134
|
-
|
|
135
|
-
// 分页读取
|
|
136
|
-
haimati_read({ query: "086", offset: 10, limit: 50 })
|
|
137
|
-
|
|
138
|
-
// 搜索记忆
|
|
139
|
-
haimati_search({ keyword: "WebSocket", match: "or", limit: 10 })
|
|
140
|
-
|
|
141
|
-
// 部分修改记忆(注意:offsetEnd 不包括此行,[3, 5) 表示替换第 3、4 两行)
|
|
142
|
-
// 先用 haimati_read 获取 version,然后传入进行并发控制
|
|
143
|
-
haimati_edit({
|
|
144
|
-
query: "086",
|
|
145
|
-
offsetBegin: 3,
|
|
146
|
-
offsetEnd: 5, // 不包括第 5 行
|
|
147
|
-
content: "新的替换内容",
|
|
148
|
-
version: 1 // 从 haimati_read 获取的 version
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
// 移动记忆到新分类
|
|
152
|
-
haimati_move({
|
|
153
|
-
query: "086",
|
|
154
|
-
newCategory: "xxx项目/客户端"
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
// 删除记忆(支持序号或完整路径)
|
|
158
|
-
haimati_delete({ query: "086" })
|
|
159
|
-
// 或
|
|
160
|
-
haimati_delete({ query: "xxx项目/签章/签章服务WS通信机制" })
|
|
64
|
+
// 写入(title 不能以 " - 序号" 结尾)
|
|
65
|
+
haimati_write({ category: "xxx项目/签章", title: "签章服务WS通信机制", content: "..." })
|
|
161
66
|
|
|
162
|
-
//
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
// 列出指定分类下的直接子分类和条目
|
|
166
|
-
haimati_list({ category: "xxx项目" })
|
|
167
|
-
|
|
168
|
-
// 递归列出指定分类下所有后代条目
|
|
169
|
-
haimati_list({ category: "xxx项目", recursive: true })
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
### 并发控制示例
|
|
67
|
+
// 读取
|
|
68
|
+
haimati_read({ query: "086" })
|
|
173
69
|
|
|
174
|
-
|
|
70
|
+
// 搜索
|
|
71
|
+
haimati_search({ keyword: "WebSocket", match: "or" })
|
|
175
72
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
会话B: haimati_read({ query: "086" }) → version:1
|
|
179
|
-
会话A: haimati_edit({ query: "086", ..., version:1 }) → 成功,version变为2
|
|
180
|
-
会话B: haimati_edit({ query: "086", ..., version:1 }) → 错误:版本冲突
|
|
181
|
-
```
|
|
73
|
+
// 修改(先 read 获取 version)
|
|
74
|
+
haimati_edit({ query: "086", offsetBegin: 3, offsetEnd: 5, content: "新内容", version: 1 })
|
|
182
75
|
|
|
183
|
-
|
|
76
|
+
// 移动
|
|
77
|
+
haimati_move({ query: "086", newCategory: "xxx项目/客户端" })
|
|
184
78
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
├── 签章/
|
|
188
|
-
│ └── 签章服务WS通信机制 - 086
|
|
189
|
-
└── 通用/
|
|
190
|
-
└── 用户注册密码 - 033
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
## 书页文件格式
|
|
79
|
+
// 删除
|
|
80
|
+
haimati_delete({ query: "086" })
|
|
194
81
|
|
|
82
|
+
// 列出
|
|
83
|
+
haimati_list({ category: "xxx项目", recursive: true })
|
|
195
84
|
```
|
|
196
|
-
正文内容第一行
|
|
197
|
-
正文内容第二行
|
|
198
|
-
正文内容第三行
|
|
199
|
-
------system------
|
|
200
|
-
标题
|
|
201
|
-
创建时间 (ISO 8601)
|
|
202
|
-
version:1
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
最后4行是系统行:分隔符、标题、创建时间、版本号。
|
|
206
85
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
## 依赖要求
|
|
86
|
+
## 海马体三大原则
|
|
210
87
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
88
|
+
1. **信息不丢失**: 完整保留所有有价值的信息
|
|
89
|
+
2. **错误信息修正**: 不再保留已被纠正的错误信息
|
|
90
|
+
3. **拒绝冗余**: 不要重复记录相同内容
|
|
214
91
|
|
|
215
92
|
## 日志
|
|
216
93
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
日志按天覆盖,新的一天开始时覆盖旧日志。日志内容超长时自动截断(保留前后各一部分)。
|
|
220
|
-
|
|
221
|
-
**日志格式**:
|
|
222
|
-
```
|
|
223
|
-
[时间戳] [级别] 消息
|
|
224
|
-
|
|
225
|
-
例如:
|
|
226
|
-
[14:30:25] [INFO] [haimati_write] 入参: category="通用", title="测试记忆"
|
|
227
|
-
[14:30:26] [INFO] [haimati_write] 出参:
|
|
228
|
-
已写入海马体 #001
|
|
229
|
-
路径: 通用/测试记忆
|
|
230
|
-
版本: 1
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
**日志级别**:DEBUG、INFO、WARN、ERROR
|
|
234
|
-
|
|
235
|
-
## 自动增强与记录
|
|
236
|
-
|
|
237
|
-
### 首条提示词增强
|
|
238
|
-
|
|
239
|
-
插件会在会话开始时自动增强用户发送的第一条消息:
|
|
240
|
-
- 查找 `/start-work` 命令的 template
|
|
241
|
-
- 如果存在,将 template 添加到用户消息前面
|
|
242
|
-
- 只增强首条消息,后续消息不受影响
|
|
243
|
-
|
|
244
|
-
### 会话结束记录
|
|
245
|
-
|
|
246
|
-
会话空闲时,插件会自动注入 `/record-memory` 命令提示用户记录:
|
|
247
|
-
- 只有当 `/record-memory` 命令存在且有 template 时才会注入
|
|
248
|
-
- 如果用户主动停止会话,不会注入提示词
|
|
249
|
-
- 使用内存集合跟踪已注入的会话,防止重复注入
|
|
94
|
+
日志位于:`{系统临时目录}/haimati_logs/haimati.log`(按天覆盖)
|
|
250
95
|
|
|
251
|
-
##
|
|
96
|
+
## 依赖
|
|
252
97
|
|
|
253
|
-
|
|
98
|
+
- OpenCode v0.15.0+
|
|
99
|
+
- Node.js 或 Bun
|
package/dist/index.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import{tool as I}from"@opencode-ai/plugin";import
|
|
2
|
-
`);for(let t of
|
|
3
|
-
`),
|
|
4
|
-
`)
|
|
5
|
-
`);if(
|
|
6
|
-
`);return
|
|
7
|
-
`);return{title:
|
|
8
|
-
`)
|
|
1
|
+
import{tool as I}from"@opencode-ai/plugin";import _ from"path";import Dt from"os";import q from"fs";var z=".haimati",xt="\u7D22\u5F15.md";var It="next-id.txt",St="info.log",Et="error.log";var _t="log.conf";var Pt="opencode-haimati.conf",bt="haimati-path";var G="/start-work";var X="/record-memory";function ee(e){let i=_.join(e,Pt);if(!q.existsSync(i))return null;try{let c=q.readFileSync(i,"utf-8").split(`
|
|
2
|
+
`);for(let t of c){let r=t.trim();if(!r||r.startsWith("#"))continue;let l=r.match(/^([^:]+):\s*(.+)$/);if(l){let o=l[1].trim(),a=l[2].trim();if(o===bt&&a){let u=a.match(/^\$\{(\w+)\}$/);if(u){let f=process.env[u[1]];return f?_.resolve(e,f):(console.warn(`[haimati] \u73AF\u5883\u53D8\u91CF ${u[1]} \u672A\u5B9A\u4E49\u6216\u503C\u4E3A\u7A7A`),null)}return _.resolve(e,a)}}}}catch(n){console.error("[haimati] \u8BFB\u53D6\u914D\u7F6E\u6587\u4EF6\u5931\u8D25:",n)}return null}function v(e){let i=ee(e);return i||_.join(e,z)}function L(e){return _.join(v(e),xt)}function Q(e){return _.join(v(e),It)}function ut(e){return _.join(v(e),".lock")}function gt(e){return _.join(v(e),"\u4E66\u9875")}function B(e,i){let n=parseInt(i,10),c=Math.floor((n-1)/50)*50+1,t=c+50-1,r=`${String(c).padStart(3,"0")}-${String(t).padStart(3,"0")}`;return _.join(gt(e),r)}function Z(e,i){let n=String(parseInt(i,10)).padStart(3,"0");return _.join(B(e,i),`${n}.md`)}function H(){let e=Dt.homedir();return _.join(e,".opencode-haimati","log")}function ne(){let e=Dt.homedir();return _.join(e,".opencode-haimati",_t)}function tt(){return _.join(H(),St)}function Lt(){return _.join(H(),Et)}function mt(){let e=ne(),i={level:"info",logRetentionDays:180};if(!q.existsSync(e))return i;try{let c=q.readFileSync(e,"utf-8").split(`
|
|
3
|
+
`),t=i.level,r=i.logRetentionDays;for(let l of c){let o=l.trim();if(!o||o.startsWith("#"))continue;let a=o.match(/^([^:]+):\s*(.+)$/);if(a){let u=a[1].trim(),f=a[2].trim();if(u==="level")["debug","info","warn","error"].includes(f)&&(t=f);else if(u==="log_retention_days"){let d=parseInt(f,10);!isNaN(d)&&d>0&&(r=d)}}}return{level:t,logRetentionDays:r}}catch(n){return console.error("[haimati] \u8BFB\u53D6\u65E5\u5FD7\u914D\u7F6E\u5931\u8D25:",n),i}}function C(e){return q.promises.mkdir(e,{recursive:!0}).then(()=>{})}function y(e){return q.promises.access(e).then(()=>!0).catch(()=>!1)}import Mt from"fs";import ie from"path";import{promisify as Ct}from"util";var kt=Ct(Mt.readFile),re=Ct(Mt.writeFile);function et(e){let i=0,n=0,c=e.length;for(;n<c;)if(e[n]==="\u2502"&&n+3<c&&e.slice(n+1,n+4)===" ")i++,n+=4;else if(e.slice(n,n+4)===" ")i++,n+=4;else{if((e[n]==="\u251C"||e[n]==="\u2514")&&n+3<c&&e.slice(n+1,n+4)==="\u2500\u2500 ")break;break}return i}function nt(e){let n=e.trim().replace(/^[│├└─\s]+/,"").trim();return n==="/"?"/":!n.endsWith("/")||(n=n.slice(0,-1),!n)?null:n}function oe(e){let i=[],n=e.split(`
|
|
4
|
+
`),c=[];for(let t of n){if(!t.trim())continue;let r=et(t),l=t.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(l){let o=l[1].trim(),a=String(parseInt(l[2],10)),f=c.slice(0,r).filter(d=>d!=="").join("/");i.push({id:a,category:f,title:o})}else{let o=nt(t);if(o)if(o==="/"){c[r]="";for(let a=r+1;a<c.length;a++)delete c[a]}else{c[r]=o;for(let a=r+1;a<c.length;a++)delete c[a]}}}return i}function Y(e,i){return e.find(n=>n.id===i)||null}function it(e,i,n){return e.find(c=>c.category===i&&c.title===n)||null}function vt(e,i){return e.filter(n=>n.id!==i)}function ht(e){if(e.length===0)return"";let i={name:"/",children:[],entry:null};for(let t of e){let r=t.category.split("/"),l=i;for(let a=0;a<r.length;a++){let u=r[a];if(!u)continue;let f=l.children.find(d=>d.name===u);f||(f={name:u,children:[],entry:null},l.children.push(f)),l=f}let o={name:t.id,children:[],entry:t};l.children.push(o)}function n(t){t.children.sort((r,l)=>r.name.localeCompare(l.name,void 0,{sensitivity:"base"}));for(let r of t.children)n(r)}n(i);function c(t,r,l){let o=[],a=r+(l?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 "),u=t.name==="/"?"/":t.name+"/";o.push(`${a}${u}`),r=r+(l?" ":"\u2502 ");let f=t.children.filter(g=>g.entry===null),d=t.children.filter(g=>g.entry!==null),p=[...f,...d];for(let g=0;g<p.length;g++){let m=p[g],w=g===p.length-1;if(m.entry){let h=m.entry.id.padStart(3,"0"),S=r+(w?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ");o.push(`${S}${m.entry.title} - ${h}`)}else o.push(...c(m,r,w))}return o}return c(i,"",!0).join(`
|
|
5
|
+
`)}async function M(e){let i=L(e);if(!await y(i))return[];let n=await kt(i,"utf-8");return oe(n)}async function J(e,i){let n=ie.join(e,z);await y(n)||await C(n);let c=ht(i),t=L(e);await re(t,c,"utf-8")}async function Tt(e){let i=L(e);return await y(i)?kt(i,"utf-8"):""}import rt from"fs";import{promisify as ot}from"util";var se=ot(rt.readFile),ae=ot(rt.writeFile),ce=ot(rt.unlink),Rt=ot(rt.mkdir);async function T(e,i){let n=Z(e,i);if(!await y(n))return null;let t=(await se(n,"utf-8")).split(`
|
|
6
|
+
`);if(t.length<4)return null;let r="------system------",l=Math.max(0,t.length-5),o=-1;for(let g=t.length-1;g>=l;g--)if(t[g].trim()===r){o=g;break}if(o===-1){if(t.length>=3&&t[2].startsWith("version:")&&/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(t[1])){let h=t[0],S=t[1],x=parseInt(t[2].substring(8).trim(),10)||1,D=t.slice(3).join(`
|
|
7
|
+
`);return{title:h,createdAt:S,content:D,version:x}}let m=t[0],w=t.join(`
|
|
8
|
+
`);return{title:m,createdAt:"",content:w,version:1}}if(o+3>=t.length)return null;let a=t.slice(0,o).join(`
|
|
9
|
+
`),u=t[o+1],f=t[o+2],d=t[o+3],p=parseInt(d.substring(8).trim(),10)||1;return{title:u,createdAt:f,content:a,version:p}}async function U(e,i,n,c,t,r=1){let l=gt(e);await y(l)||await Rt(l,{recursive:!0});let o=B(e,i);await y(o)||await Rt(o,{recursive:!0});let a=Z(e,i),f=`------system------
|
|
9
10
|
${n}
|
|
10
|
-
${
|
|
11
|
-
version:${
|
|
12
|
-
${
|
|
13
|
-
`,"utf-8"):await zt(t,s
|
|
14
|
-
|
|
15
|
-
${$(
|
|
16
|
-
${$(
|
|
17
|
-
${$(
|
|
18
|
-
|
|
19
|
-
`)
|
|
20
|
-
`);return await
|
|
21
|
-
${$
|
|
22
|
-
\u5806\u6808: ${c}`),`\u9519\u8BEF: ${i}`}}}),haimati_write:I({description:"\u5C06\u65B0\u4FE1\u606F\u5199\u5165\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u3002\u9700\u8981\u63D0\u4F9B\u5206\u7C7B\u8DEF\u5F84\uFF08\u5982 'xxx\u9879\u76EE/\u7B7E\u7AE0'\uFF09\u3001\u6807\u9898\u548C\u5185\u5BB9\u3002\u5982\u679C\u76F8\u540C\u5206\u7C7B\u548C\u6807\u9898\u7684\u8BB0\u5FC6\u5DF2\u5B58\u5728\uFF0C\u5219\u4F1A\u62A5\u9519\u3002",args:{category:I.schema.string().describe("\u5206\u7C7B\u8DEF\u5F84\uFF0C\u5982 'xxx\u9879\u76EE/\u7B7E\u7AE0' \u6216 '\u901A\u7528'"),title:I.schema.string().describe("\u8BB0\u5FC6\u6807\u9898\uFF0C\u5982 '\u7B7E\u7AE0\u670D\u52A1WS\u901A\u4FE1\u673A\u5236'"),content:I.schema.string().describe("\u8981\u5199\u5165\u7684\u8BE6\u7EC6\u5185\u5BB9")},async execute(e,o){let{directory:n}=o;await r("info",`[haimati_write] \u5165\u53C2: category="${e.category}", title="${e.title}", content\u957F\u5EA6=${e.content.length}\u5B57\u7B26, content=
|
|
11
|
+
${c}
|
|
12
|
+
version:${r}`,d=t?`${t}
|
|
13
|
+
${f}`:f;await ae(a,d,"utf-8")}async function Ot(e,i){let n=Z(e,i);return await y(n)?(await ce(n),!0):!1}async function st(e,i,n,c,t=!1){let r=i.toLowerCase().split(/\s+/).filter(o=>o.length>0);if(r.length===0)return[];let l=[];for(let o of e){let a=o.title.toLowerCase(),u=o.category.toLowerCase(),f=o.id.toLowerCase(),d=0,p=0;for(let m of r)(a.includes(m)||u.includes(m)||f.includes(m))&&d++;if(t&&c){let m=await T(c,o.id);if(m&&m.content){let w=m.content.toLowerCase();for(let h of r)w.includes(h)&&p++}}let g=d+p;n==="and"?g>=r.length&&l.push({entry:o,score:g}):(d>0||t&&p>0)&&l.push({entry:o,score:g})}return l.sort((o,a)=>a.score-o.score),l.map(o=>o.entry)}import Kt from"fs";import{promisify as Vt}from"util";import Nt from"fs";import{promisify as At}from"util";var fe=At(Nt.writeFile),de=At(Nt.unlink);async function Ft(e){let i=v(e);await y(i)||await C(i);let n=ut(e),c=Date.now(),t=50+Math.random()*20;for(;await y(n);){if(Date.now()-c>5e3)return!1;await new Promise(r=>setTimeout(r,t))}return await fe(n,String(process.pid),"utf-8"),!0}async function jt(e){let i=ut(e);await y(i)&&await de(i)}import N from"fs";import O from"path";import{promisify as A}from"util";var qt=A(N.appendFile),ue=A(N.readdir),Bt=A(N.unlink),wt=A(N.stat),ge=A(N.rename),Ht=A(N.readFile),me=A(N.writeFile),Wt={debug:0,info:1,warn:2,error:3},k="",he=".log_state";function Yt(){return O.join(H(),he)}async function yt(){let e=Yt();try{if(await y(e)){let n=(await Ht(e,"utf-8")).match(/lastCheckedDate=(\d{4}-\d{2}-\d{2})/);if(n)return n[1]}}catch(i){console.error("[haimati] \u8BFB\u53D6\u65E5\u5FD7\u72B6\u6001\u6587\u4EF6\u5931\u8D25:",i)}return""}async function K(e){let i=Yt();try{let n=O.dirname(i);await y(n)||await C(n),await me(i,`lastCheckedDate=${e}`,"utf-8")}catch(n){console.error("[haimati] \u5199\u5165\u65E5\u5FD7\u72B6\u6001\u6587\u4EF6\u5931\u8D25:",n)}}function $(e,i=5e3){if(e.length<=i)return e;let n=Math.floor((i-20)/2);return e.substring(0,n)+"...(\u7701\u7565"+(e.length-i)+"\u5B57\u7B26)..."+e.substring(e.length-n)}function V(e){let i=e.getFullYear(),n=String(e.getMonth()+1).padStart(2,"0"),c=String(e.getDate()).padStart(2,"0");return`${i}-${n}-${c}`}function pe(){let e=new Date;return e.setDate(e.getDate()-1),V(e)}function we(e){let i=e.getFullYear(),n=String(e.getMonth()+1).padStart(2,"0"),c=String(e.getDate()).padStart(2,"0"),t=String(e.getHours()).padStart(2,"0"),r=String(e.getMinutes()).padStart(2,"0"),l=String(e.getSeconds()).padStart(2,"0");return`${i}-${n}-${c} ${t}:${r}:${l}`}async function ye(e){try{let i=tt();if(!await y(i)||(await wt(i)).size===0)return;let c=O.join(H(),"history");await y(c)||await C(c);let t=O.join(c,`${e}.log`),r=await Ht(i,"utf-8");await qt(t,r,"utf-8"),await ge(i,i+".tmp"),await Bt(i+".tmp")}catch(i){console.error("[haimati] \u8F6E\u8F6C info.log \u5931\u8D25:",i)}}async function $e(){let e=V(new Date);if(await yt()===e){k=e;return}let n=pe();await ye(n),k=e,await K(e)}async function Gt(e,i){try{let n=O.dirname(e);await y(n)||await C(n),await qt(e,i+`
|
|
14
|
+
`,"utf-8")}catch(n){console.error("[haimati] \u5199\u5165\u65E5\u5FD7\u6587\u4EF6\u5931\u8D25:",n)}}async function Jt(){let e=new Date,i=V(e);if(await yt()===i){k=i;return}try{let c=mt(),t=O.join(H(),"history");if(!await y(t)){k=i,await K(i);return}let r=await ue(t),l=e.getTime()-c.logRetentionDays*24*60*60*1e3;for(let o of r){if(!o.match(/^\d{4}-\d{2}-\d{2}\.log$/))continue;let a=O.join(t,o);(await wt(a)).mtime.getTime()<l&&await Bt(a)}k=i,await K(i)}catch(c){console.error("[haimati] \u6E05\u7406\u5386\u53F2\u65E5\u5FD7\u5931\u8D25:",c)}}async function s(e,i,n){try{let t=mt().level;if(Wt[e]<Wt[t])return;let l=we(new Date),o=n?`[${n}] `:"",a=`[${l}] [${e.toUpperCase()}] ${o}${i}`,u=a.length>5e3?$(a):a;await $e(),e==="error"?await Gt(Lt(),u):await Gt(tt(),u),Jt().catch(f=>{console.error("[haimati] \u6E05\u7406\u5386\u53F2\u65E5\u5FD7\u5931\u8D25:",f)})}catch(c){console.error("[haimati] \u65E5\u5FD7\u8BB0\u5F55\u5931\u8D25:",c)}}async function Ut(){let e=V(new Date),i=tt();if(k=await yt(),k!==e){try{if(await y(i)){let n=await wt(i);if(V(n.mtime)===e){k=e,await K(e);return}}}catch(n){console.error("[haimati] \u68C0\u67E5 info.log \u72B6\u6001\u5931\u8D25:",n)}k=e,await K(e),await Jt()}}var xe=Vt(Kt.readFile),Ie=Vt(Kt.writeFile);async function Se(e){let i=Q(e);if(!await y(i))return 1;let c=(await xe(i,"utf-8")).trim(),t=parseInt(c,10);return!isNaN(t)&&t>0?t:1}async function Ee(e){let i=Q(e);if(await y(i))return;let n=await M(e),c=0;for(let t of n){let r=parseInt(t.id,10);r>c&&(c=r)}await zt(e,c+1)}async function zt(e,i){let n=v(e);await y(n)||await C(n);let c=Q(e);await Ie(c,String(i),"utf-8")}async function Xt(e,i,n,c,t,r=!1){if(/ - \d{3}$/.test(n))throw await s("warn",`[allocateIdWithLock] title \u683C\u5F0F\u4E0D\u5408\u89C4: "${n}", \u629B\u51FA\u9519\u8BEF`),new Error('\u6807\u9898\u4E0D\u80FD\u4EE5 " - \u5E8F\u53F7" \u683C\u5F0F\u7ED3\u5C3E\uFF0C\u5E8F\u53F7\u7531\u7CFB\u7EDF\u81EA\u52A8\u5206\u914D\uFF0C\u8BF7\u4FEE\u6539\u6807\u9898');if(await s("info",`[allocateIdWithLock] \u5F00\u59CB\u5206\u914D\u5E8F\u53F7: category="${i}", title="${n}"`),!await Ft(e))return await s("warn",`[allocateIdWithLock] \u83B7\u53D6\u9501\u5931\u8D25: category="${i}", title="${n}"`),null;await s("debug",`[allocateIdWithLock] \u83B7\u53D6\u9501\u6210\u529F: category="${i}", title="${n}"`);try{await Ee(e);let l=await M(e),o=l.find(d=>d.category===i&&d.title===n);if(o){if(!r)throw new Error(`\u8BB0\u5FC6\u5DF2\u5B58\u5728: ${i}/${n}\uFF0C\u5982\u9700\u66F4\u65B0\u8BF7\u4F7F\u7528 haimati_edit \u5DE5\u5177`);let d=await T(e,o.id),p=d?d.createdAt:c,g=d?d.version+1:1;return await U(e,o.id,n,p,t,g),await s("info",`[allocateIdWithLock] \u66F4\u65B0\u5DF2\u6709\u6761\u76EE: #${o.id} ${i}/${n}, \u65B0\u7248\u672C: ${g}`),null}let a=await Se(e),u=String(a).padStart(3,"0");await U(e,u,n,c,t,1);let f={id:u,category:i,title:n};return l.push(f),await J(e,l),await zt(e,a+1),await s("info",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u6210\u529F: #${u} ${i}/${n}`),u}catch(l){let o=l instanceof Error?l.message:String(l);if(await s("error",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u5931\u8D25: ${o}`),o.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw l;return null}finally{await jt(e)}}function Qt(){return{haimati_read:I({description:"\u4ECE\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u8BFB\u53D6\u5185\u5BB9\u3002\u901A\u8FC7\u5E8F\u53F7\uFF08\u5982 '086'\uFF09\u6765\u5B9A\u4F4D\u5E76\u8BFB\u53D6\u8BB0\u5FC6\u5185\u5BB9\u3002\u652F\u6301\u5206\u9875\u8BFB\u53D6\uFF08offset/limit\uFF09\uFF0C\u8FD4\u56DE\u5185\u5BB9\u5E26\u884C\u53F7\u548C\u7248\u672C\u53F7\u3002",args:{query:I.schema.string().describe("\u5E8F\u53F7\u3002\u4F8B\u5982\uFF1A'086'"),offset:I.schema.number().optional().default(1).describe("\u8D77\u59CB\u884C\u53F7\uFF08\u9ED8\u8BA4 1\uFF09"),limit:I.schema.number().optional().default(2e3).describe("\u6700\u5927\u8BFB\u53D6\u884C\u6570\uFF08\u9ED8\u8BA4 2000\uFF09")},async execute(e,i){let{directory:n}=i,c=L(n);if(await s("info",`[haimati_read] \u5165\u53C2: query="${e.query}", offset=${e.offset||1}, limit=${e.limit||2e3}`),!await y(c)){let t=`\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728\u4E8E ${c}`;return await s("warn",`[haimati_read] \u51FA\u53C2:
|
|
15
|
+
${$(t)}`),t}try{let t=await M(n);await s("debug",`[haimati_read] \u8BFB\u53D6\u7D22\u5F15: \u5171 ${t.length} \u6761\u8BB0\u5F55`);let r=e.query.trim();if(!/^\d+$/.test(r)){let x="\u9519\u8BEF\uFF1Aquery \u5FC5\u987B\u662F\u5E8F\u53F7\uFF0C\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2";return await s("warn",`[haimati_read] \u51FA\u53C2:
|
|
16
|
+
${$(x)}`),x}let l=String(parseInt(r,10)),o=Y(t,l);if(await s("debug",`[haimati_read] \u5E8F\u53F7\u67E5\u8BE2 #${r}: ${o?"\u627E\u5230":"\u672A\u627E\u5230"}`),!o){let x=`\u672A\u627E\u5230\u8BB0\u5FC6: ${e.query}`;return await s("info",`[haimati_read] \u51FA\u53C2:
|
|
17
|
+
${$(x)}`),x}let a=await T(n,o.id);if(!a){let x=`\u672A\u627E\u5230\u4E66\u9875: #${o.id.padStart(3,"0")}`;return await s("error",`[haimati_read] \u51FA\u53C2:
|
|
18
|
+
${$(x)}`),x}let u=B(n,o.id);await s("debug",`[haimati_read] \u8BFB\u53D6\u4E66\u9875: \u4E66\u9875/${u.split(/[\\/]/).pop()}/${o.id}.md, version=${a.version}`);let f=a.content.split(`
|
|
19
|
+
`),d=e.offset||1,p=e.limit||2e3,g=f.length,m=Math.max(0,d-1),w=Math.min(g,m+p),h=[];for(let x=m;x<w;x++)h.push(`${x+1}|${f[x]}`);h.push("------system------"),h.push(`\u6807\u9898:${o.title}`),h.push(`\u521B\u5EFA\u65F6\u95F4:${a.createdAt}`),h.push(`version:${a.version}`);let S=h.join(`
|
|
20
|
+
`);return await s("info",`[haimati_read] \u51FA\u53C2:
|
|
21
|
+
${$(S)}`),S}catch(t){let r=t instanceof Error?t.message:String(t),l=t instanceof Error?t.stack:"";return await s("error",`[haimati_read] \u51FA\u53C2: \u9519\u8BEF: ${r}
|
|
22
|
+
\u5806\u6808: ${l}`),`\u9519\u8BEF: ${r}`}}}),haimati_write:I({description:"\u5C06\u65B0\u4FE1\u606F\u5199\u5165\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u3002\u9700\u8981\u63D0\u4F9B\u5206\u7C7B\u8DEF\u5F84\uFF08\u5982 'xxx\u9879\u76EE/\u7B7E\u7AE0'\uFF09\u3001\u6807\u9898\u548C\u5185\u5BB9\u3002\u5982\u679C\u76F8\u540C\u5206\u7C7B\u548C\u6807\u9898\u7684\u8BB0\u5FC6\u5DF2\u5B58\u5728\uFF0C\u5219\u4F1A\u62A5\u9519\u3002",args:{category:I.schema.string().describe("\u5206\u7C7B\u8DEF\u5F84\uFF0C\u5982 'xxx\u9879\u76EE/\u7B7E\u7AE0' \u6216 '\u901A\u7528'"),title:I.schema.string().describe("\u8BB0\u5FC6\u6807\u9898\uFF0C\u5982 '\u7B7E\u7AE0\u670D\u52A1WS\u901A\u4FE1\u673A\u5236'"),content:I.schema.string().describe("\u8981\u5199\u5165\u7684\u8BE6\u7EC6\u5185\u5BB9")},async execute(e,i){let{directory:n}=i;await s("info",`[haimati_write] \u5165\u53C2: category="${e.category}", title="${e.title}", content\u957F\u5EA6=${e.content.length}\u5B57\u7B26, content=
|
|
23
23
|
${$(e.content)}
|
|
24
|
-
`);try{let
|
|
24
|
+
`);try{let c=new Date().toLocaleString("zh-CN",{hour12:!1}),t=null;for(let a=0;a<3;a++)try{if(t=await Xt(n,e.category,e.title,c,e.content,!1),t)break;await s("warn",`[haimati_write] \u9501\u83B7\u53D6\u5931\u8D25\uFF0C\u91CD\u8BD5\u7B2C ${a+1} \u6B21`)}catch(u){let f=u instanceof Error?u.message:String(u);if(f.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw u;await s("warn",`[haimati_write] \u83B7\u53D6\u9501\u5F02\u5E38: ${f}, \u91CD\u8BD5\u7B2C ${a+1} \u6B21`)}if(t){let a=B(n,t);await s("debug",`[haimati_write] \u5206\u914D\u65B0\u5E8F\u53F7: #${t}`),await s("debug",`[haimati_write] \u4E66\u9875\u548C\u7D22\u5F15\u5DF2\u66F4\u65B0: \u4E66\u9875/${a.split(/[\\/]/).pop()}/${t}.md`);let u=`\u5DF2\u5199\u5165\u6D77\u9A6C\u4F53 #${t}
|
|
25
25
|
\u8DEF\u5F84: ${e.category}/${e.title}
|
|
26
|
-
\u7248\u672C: 1`;return await
|
|
27
|
-
${$(u)}`),u}let
|
|
28
|
-
${$(a)}`),a}let
|
|
29
|
-
${$(
|
|
30
|
-
\u5806\u6808: ${
|
|
31
|
-
${$(t)}`),t}try{let t=await
|
|
32
|
-
${$(
|
|
33
|
-
`),
|
|
34
|
-
`);for(let
|
|
35
|
-
`).filter(
|
|
36
|
-
`);return await
|
|
37
|
-
${$(w)}`),w}catch(t){let
|
|
38
|
-
\u5806\u6808: ${
|
|
39
|
-
${$(e.content)}`),!await
|
|
40
|
-
${$(t)}`),t}try{let t=await
|
|
41
|
-
${$(
|
|
42
|
-
${$(
|
|
43
|
-
${$(
|
|
44
|
-
`).length;if(u<1||u
|
|
45
|
-
${$(
|
|
46
|
-
${$(
|
|
47
|
-
`){if(ft===lt)return
|
|
48
|
-
`),
|
|
49
|
-
`)&&!
|
|
50
|
-
`:"")+a.content.substring(
|
|
51
|
-
\u8DEF\u5F84: ${
|
|
52
|
-
\u65B0\u7248\u672C: ${
|
|
53
|
-
${$(
|
|
54
|
-
\u5806\u6808: ${
|
|
55
|
-
${$(t)}`),t}try{let t=await
|
|
56
|
-
${
|
|
57
|
-
`)}`}}if(!
|
|
58
|
-
${$(
|
|
59
|
-
\u8DEF\u5F84: ${a}`;return await
|
|
60
|
-
${$(u)}`),u}catch(t){let
|
|
61
|
-
\u5806\u6808: ${
|
|
62
|
-
${$(t)}`),t}try{let t=await
|
|
63
|
-
${
|
|
64
|
-
`)}`}}if(!
|
|
65
|
-
${$(
|
|
26
|
+
\u7248\u672C: 1`;return await s("info",`[haimati_write] \u51FA\u53C2:
|
|
27
|
+
${$(u)}`),u}let r=await M(n),l=it(r,e.category,e.title);if(l){let a=`\u9519\u8BEF\uFF1A\u8BB0\u5FC6\u5DF2\u5B58\u5728 #${l.id} (${e.category}/${e.title})\uFF0C\u5982\u9700\u66F4\u65B0\u8BF7\u4F7F\u7528 haimati_edit \u5DE5\u5177`;return await s("error",`[haimati_write] \u51FA\u53C2:
|
|
28
|
+
${$(a)}`),a}let o="\u9519\u8BEF\uFF1A\u65E0\u6CD5\u5206\u914D\u5E8F\u53F7\uFF08\u9501\u7B49\u5F85\u8D85\u65F6\uFF09";return await s("error",`[haimati_write] \u51FA\u53C2:
|
|
29
|
+
${$(o)}`),o}catch(c){let t=c instanceof Error?c.message:String(c),r=c instanceof Error?c.stack:"";return await s("error",`[haimati_write] \u51FA\u53C2: \u9519\u8BEF: ${t}
|
|
30
|
+
\u5806\u6808: ${r}`),`\u9519\u8BEF: ${t}`}}}),haimati_search:I({description:"\u641C\u7D22\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\uFF08\u652F\u6301\u591A\u5173\u952E\u5B57\uFF0C\u7528\u7A7A\u683C\u5206\u9694\uFF09\u3002\u641C\u7D22\u8303\u56F4\u5305\u62EC\uFF1A\u6807\u9898\u3001\u5206\u7C7B\u3001\u5E8F\u53F7\u3001\u4E66\u9875\u5185\u5BB9\u3002\u8F93\u5165\u5173\u952E\u8BCD\uFF0C\u8FD4\u56DE\u5339\u914D\u7684\u6761\u76EE\u5217\u8868\u548C\u5185\u5BB9\u7247\u6BB5\uFF0C\u652F\u6301\u5206\u9875\uFF08limit/offset\uFF09\u3002",args:{keyword:I.schema.string().describe("\u641C\u7D22\u5173\u952E\u8BCD\uFF08\u652F\u6301\u591A\u5173\u952E\u5B57\uFF0C\u7528\u7A7A\u683C\u5206\u9694\uFF09"),match:I.schema.string().describe("\u5339\u914D\u6A21\u5F0F\uFF1Aand=\u6240\u6709\u5173\u952E\u5B57\u90FD\u5339\u914D\uFF0Cor=\u4EFB\u610F\u5173\u952E\u5B57\u5339\u914D"),limit:I.schema.number().default(10).describe("\u8FD4\u56DE\u7ED3\u679C\u6570\u91CF\u9650\u5236"),offset:I.schema.number().default(0).describe("\u5206\u9875\u504F\u79FB\u91CF\uFF0C\u7528\u4E8E\u83B7\u53D6\u540E\u7EED\u9875\u9762")},async execute(e,i){let{directory:n}=i,c=L(n);if(await s("info",`[haimati_search] \u5165\u53C2: keyword="${e.keyword}", match="${e.match}", limit=${e.limit||10}, offset=${e.offset||0}`),!await y(c)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await s("warn",`[haimati_search] \u51FA\u53C2:
|
|
31
|
+
${$(t)}`),t}try{let t=await M(n),r=e.keyword.trim(),l=e.match,o=e.limit||10,a=e.offset||0,u=await st(t,r,l,n,!0),f=u.length,d=u.slice(a,a+o);if(d.length===0){let h=`\u672A\u627E\u5230\u5305\u542B '${r}' \u7684\u8BB0\u5FC6`;return await s("info",`[haimati_search] \u51FA\u53C2:
|
|
32
|
+
${$(h)}`),h}let p=[],g=a+d.length<f,m=g?`${a+1}-${a+d.length}/${f}`:`${a+1}-${a+d.length}/${f}`;p.push(`\u627E\u5230 ${m} \u6761\u5339\u914D\uFF1A
|
|
33
|
+
`),g&&p.push(`\uFF08\u5982\u9700\u83B7\u53D6\u66F4\u591A\uFF0C\u8BF7\u4F7F\u7528 offset=${a+o} \u7EE7\u7EED\u7FFB\u9875\uFF09
|
|
34
|
+
`);for(let h of d){let S=await T(n,h.id),x=`${h.category}/${h.title}`,D="";S&&(D=S.content.split(`
|
|
35
|
+
`).filter(ct=>ct.trim()).slice(0,3).join(" | ").substring(0,150),D.length===150&&(D+="...")),p.push(`## ${x} [${h.id}]`),D&&p.push(`> ${D}`),p.push("")}let w=p.join(`
|
|
36
|
+
`);return await s("info",`[haimati_search] \u51FA\u53C2:
|
|
37
|
+
${$(w)}`),w}catch(t){let r=t instanceof Error?t.message:String(t),l=t instanceof Error?t.stack:"";return await s("error",`[haimati_search] \u51FA\u53C2: \u9519\u8BEF: ${r}
|
|
38
|
+
\u5806\u6808: ${l}`),`\u9519\u8BEF: ${r}`}}}),haimati_edit:I({description:"\u4FEE\u6539\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7684\u90E8\u5206\u5185\u5BB9\u3002\u5C06\u6307\u5B9A\u884C\u53F7\u8303\u56F4 [offsetBegin, offsetEnd] \u7684\u5185\u5BB9\u66FF\u6362\u4E3A\u65B0 content\uFF0CoffsetEnd \u5305\u62EC\u6B64\u884C\u3002\u9700\u8981\u63D0\u4F9B read \u65F6\u8FD4\u56DE\u7684 version \u8FDB\u884C\u5E76\u53D1\u63A7\u5236\u3002",args:{query:I.schema.string().describe("\u5E8F\u53F7\uFF0C\u7528\u4E8E\u5B9A\u4F4D\u8981\u66F4\u65B0\u7684\u8BB0\u5FC6"),offsetBegin:I.schema.number().describe("\u8D77\u59CB\u884C\u53F7\uFF08\u5305\u62EC\uFF09"),offsetEnd:I.schema.number().describe("\u7ED3\u675F\u884C\u53F7\uFF08\u5305\u62EC\u6B64\u884C\uFF09"),content:I.schema.string().describe("\u66FF\u6362\u5185\u5BB9"),version:I.schema.number().describe("read \u65F6\u8FD4\u56DE\u7684 version\uFF0C\u7528\u4E8E\u5E76\u53D1\u63A7\u5236")},async execute(e,i){let{directory:n}=i,c=L(n);if(await s("info",`[haimati_edit] \u5165\u53C2: query="${e.query}", offsetBegin=${e.offsetBegin}, offsetEnd=${e.offsetEnd}, content\u957F\u5EA6=${e.content.length}\u5B57\u7B26, version=${e.version}, content=
|
|
39
|
+
${$(e.content)}`),!await y(c)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await s("warn",`[haimati_edit] \u51FA\u53C2:
|
|
40
|
+
${$(t)}`),t}try{let t=await M(n),r=e.query.trim();if(!/^\d+$/.test(r)){let E="\u9519\u8BEF\uFF1Aquery \u5FC5\u987B\u662F\u5E8F\u53F7\uFF0C\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2";return await s("warn",`[haimati_edit] \u51FA\u53C2:
|
|
41
|
+
${$(E)}`),E}let l=String(parseInt(r,10)),o=Y(t,l);if(!o){let E=`\u672A\u627E\u5230\u4E0E '${r}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await s("info",`[haimati_edit] \u51FA\u53C2:
|
|
42
|
+
${$(E)}`),E}let a=await T(n,o.id);if(!a){let E=`\u672A\u627E\u5230\u4E66\u9875: #${o.id.padStart(3,"0")}`;return await s("error",`[haimati_edit] \u51FA\u53C2:
|
|
43
|
+
${$(E)}`),E}let u=e.offsetBegin,f=e.offsetEnd,d=a.content.split(`
|
|
44
|
+
`).length;if(u<1||u>d||u>f||f>d){let E=`\u9519\u8BEF\uFF1Aoffset \u975E\u6CD5\uFF0CoffsetBegin=${u}, offsetEnd=${f}, \u6587\u4EF6\u603B\u884C\u6570=${d}\uFF0C\u6709\u6548\u8303\u56F4 offsetBegin=[1, ${d}], offsetEnd=[offsetBegin, ${d}]`;return await s("warn",`[haimati_edit] \u51FA\u53C2:
|
|
45
|
+
${$(E)}`),E}if(a.version!==e.version){let E=`\u9519\u8BEF\uFF1A\u7248\u672C\u51B2\u7A81\uFF0C\u5F53\u524D version=${a.version}\uFF0C\u4F20\u5165 version=${e.version}\uFF0C\u8BF7\u91CD\u65B0\u8BFB\u53D6\u540E\u518D\u64CD\u4F5C`;return await s("warn",`[haimati_edit] \u51FA\u53C2:
|
|
46
|
+
${$(E)}`),E}let p=(E,lt)=>{if(lt===0)return 0;let ft=1,W=0;for(;W<E.length;){if(E[W]===`
|
|
47
|
+
`){if(ft===lt)return W+1;ft++}W++}return W},g=u===1?0:p(a.content,u-1),m=p(a.content,f),w=a.content.substring(0,Math.min(100,a.content.length));await s("debug",`[haimati_edit] content\u957F\u5EA6=${a.content.length}, content\u524D100\u5B57\u7B26: ${JSON.stringify(w)}, offsetBegin=${u}, offsetEnd=${f}, beforeEndIndex=${g}, afterStartIndex=${m}`);let S=(m<a.content.length?a.content.substring(m):"").startsWith(`
|
|
48
|
+
`),x=m<a.content.length&&!e.content.endsWith(`
|
|
49
|
+
`)&&!S,D=a.content.substring(0,g)+e.content+(x?`
|
|
50
|
+
`:"")+a.content.substring(m),j=a.version+1;await U(n,o.id,o.title,a.createdAt,D,j);let ct=`${o.category}/${o.title}`,$t=`\u5DF2\u66F4\u65B0\u6D77\u9A6C\u4F53 #${o.id.padStart(3,"0")}
|
|
51
|
+
\u8DEF\u5F84: ${ct}
|
|
52
|
+
\u65B0\u7248\u672C: ${j}`;return await s("info",`[haimati_edit] \u51FA\u53C2:
|
|
53
|
+
${$($t)}`),$t}catch(t){let r=t instanceof Error?t.message:String(t),l=t instanceof Error?t.stack:"";return await s("error",`[haimati_edit] \u51FA\u53C2: \u9519\u8BEF: ${r}
|
|
54
|
+
\u5806\u6808: ${l}`),`\u9519\u8BEF: ${r}`}}}),haimati_delete:I({description:"\u5220\u9664\u6D77\u9A6C\u4F53\u4E2D\u7684\u8BB0\u5FC6\u3002\u901A\u8FC7\u5E8F\u53F7\uFF08\u5982 '086'\uFF09\u6216\u5B8C\u6574\u8DEF\u5F84\uFF08\u5982 'xxx\u9879\u76EE/\u7B7E\u7AE0/\u7B7E\u7AE0\u670D\u52A1WS\u901A\u4FE1\u673A\u5236'\uFF09\u6765\u5B9A\u4F4D\u8981\u5220\u9664\u7684\u8BB0\u5FC6\u3002",args:{query:I.schema.string().describe("\u5E8F\u53F7\u6216\u5B8C\u6574\u8DEF\u5F84\uFF0C\u7528\u4E8E\u5B9A\u4F4D\u8981\u5220\u9664\u7684\u8BB0\u5FC6")},async execute(e,i){let{directory:n}=i,c=L(n);if(await s("info",`[haimati_delete] \u5165\u53C2: query="${e.query}"`),!await y(c)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await s("warn",`[haimati_delete] \u51FA\u53C2:
|
|
55
|
+
${$(t)}`),t}try{let t=await M(n),r=e.query.trim(),l=null;if(/^\d+$/.test(r))l=Y(t,String(parseInt(r,10)));else{let f=r.split("/").filter(Boolean);if(f.length>=2){let d=f.pop(),p=f.join("/");l=it(t,p,d)}else{let d=await st(t,r,"or",n);if(d.length===1)l=d[0];else if(d.length>1)return`\u627E\u5230\u591A\u6761\u5339\u914D\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u7CBE\u786E\u6307\u5B9A\uFF1A
|
|
56
|
+
${d.slice(0,10).map(g=>` - ${g.category}/${g.title} [${g.id.padStart(3,"0")}]`).join(`
|
|
57
|
+
`)}`}}if(!l){let f=`\u672A\u627E\u5230\u4E0E '${r}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await s("info",`[haimati_delete] \u51FA\u53C2:
|
|
58
|
+
${$(f)}`),f}await Ot(n,l.id);let o=vt(t,l.id);await J(n,o);let a=`${l.category}/${l.title}`,u=`\u5DF2\u5220\u9664\u6D77\u9A6C\u4F53 #${l.id.padStart(3,"0")}
|
|
59
|
+
\u8DEF\u5F84: ${a}`;return await s("info",`[haimati_delete] \u51FA\u53C2:
|
|
60
|
+
${$(u)}`),u}catch(t){let r=t instanceof Error?t.message:String(t),l=t instanceof Error?t.stack:"";return await s("error",`[haimati_delete] \u51FA\u53C2: \u9519\u8BEF: ${r}
|
|
61
|
+
\u5806\u6808: ${l}`),`\u9519\u8BEF: ${r}`}}}),haimati_move:I({description:"\u4FEE\u6539\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7684\u5206\u7C7B\u8DEF\u5F84\u3002\u901A\u8FC7\u5E8F\u53F7\u6216\u5B8C\u6574\u8DEF\u5F84\u5B9A\u4F4D\u8BB0\u5FC6\uFF0C\u7136\u540E\u5C06\u5176\u79FB\u52A8\u5230\u65B0\u7684\u5206\u7C7B\u3002",args:{query:I.schema.string().describe("\u5E8F\u53F7\u6216\u5B8C\u6574\u8DEF\u5F84\uFF0C\u7528\u4E8E\u5B9A\u4F4D\u8981\u79FB\u52A8\u7684\u8BB0\u5FC6"),newCategory:I.schema.string().describe("\u65B0\u7684\u5206\u7C7B\u8DEF\u5F84\uFF0C\u5982 'xxx\u9879\u76EE/\u5BA2\u6237\u7AEF' \u6216 '\u901A\u7528'")},async execute(e,i){let{directory:n}=i,c=L(n);if(await s("info",`[haimati_move] \u5165\u53C2: query="${e.query}", newCategory="${e.newCategory}"`),!await y(c)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await s("warn",`[haimati_move] \u51FA\u53C2:
|
|
62
|
+
${$(t)}`),t}try{let t=await M(n),r=e.query.trim(),l=null;if(/^\d+$/.test(r))l=Y(t,String(parseInt(r,10)));else{let g=r.split("/").filter(Boolean);if(g.length>=2){let m=g.pop(),w=g.join("/");l=it(t,w,m)}else{let m=await st(t,r,"or",n);if(m.length===1)l=m[0];else if(m.length>1)return`\u627E\u5230\u591A\u6761\u5339\u914D\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u7CBE\u786E\u6307\u5B9A\uFF1A
|
|
63
|
+
${m.slice(0,10).map(h=>` - ${h.category}/${h.title} [${h.id.padStart(3,"0")}]`).join(`
|
|
64
|
+
`)}`}}if(!l){let g=`\u672A\u627E\u5230\u4E0E '${r}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await s("info",`[haimati_move] \u51FA\u53C2:
|
|
65
|
+
${$(g)}`),g}let a=`${l.category}/${l.title}`,u=e.newCategory,f=`${u}/${l.title}`,d=t.map(g=>g.id===l.id?{...g,category:u}:g);await J(n,d);let p=`\u5DF2\u79FB\u52A8\u6D77\u9A6C\u4F53 #${l.id}
|
|
66
66
|
\u65E7\u8DEF\u5F84: ${a}
|
|
67
|
-
\u65B0\u8DEF\u5F84: ${
|
|
68
|
-
${$(
|
|
69
|
-
\u5806\u6808: ${
|
|
70
|
-
${$(t)}`),t}try{let t=await
|
|
71
|
-
`),u=0,
|
|
67
|
+
\u65B0\u8DEF\u5F84: ${f}`;return await s("info",`[haimati_move] \u51FA\u53C2:
|
|
68
|
+
${$(p)}`),p}catch(t){let r=t instanceof Error?t.message:String(t),l=t instanceof Error?t.stack:"";return await s("error",`[haimati_move] \u51FA\u53C2: \u9519\u8BEF: ${r}
|
|
69
|
+
\u5806\u6808: ${l}`),`\u9519\u8BEF: ${r}`}}}),haimati_list:I({description:"\u5217\u51FA\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u7684\u5B8C\u6574\u7D22\u5F15\u7ED3\u6784\u3002",args:{category:I.schema.string().optional().describe("\u53EF\u9009\u7684\u5206\u7C7B\u8DEF\u5F84\uFF0C\u5217\u51FA\u8BE5\u5206\u7C7B\u4E0B\u7684\u6761\u76EE"),recursive:I.schema.boolean().optional().default(!1).describe("\u662F\u5426\u9012\u5F52\u904D\u5386\u5B50\u5206\u7C7B\uFF0C\u9ED8\u8BA4false")},async execute(e,i){let{directory:n}=i,c=L(n);if(await s("info",`[haimati_list] \u5165\u53C2: category="${e.category||"(\u65E0)"}, recursive=${e.recursive}"`),!await y(c)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await s("warn",`[haimati_list] \u51FA\u53C2:
|
|
70
|
+
${$(t)}`),t}try{let t=await M(n);if(e.category){let a=(await Tt(n)).split(`
|
|
71
|
+
`),u=0,f=[];for(let m of a){if(!m.trim())continue;let w=et(m);if(m.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/))continue;let S=nt(m);if(S&&(S==="/"?f[w]="":f[w]=S,f=f.slice(0,w+1),f.filter(Boolean).join("/")===e.category)){u=w;break}}let d=[],p=[];f=[];for(let m of a){if(!m.trim())continue;let w=et(m),h=m.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(h){let x=h[1].trim(),D=h[2];if(w===u+1){let j=f.slice(1,w).filter(Boolean).join("/");j===e.category&&p.push({category:j,title:x,id:D})}continue}let S=nt(m);S&&(S==="/"?f[w]="":f[w]=S,f=f.slice(0,w+1),w===u+1&&f.filter(Boolean).join("/")===e.category&&d.push(S))}let g=`## ${e.category}
|
|
72
72
|
|
|
73
|
-
`;if(e.recursive){let
|
|
74
|
-
`,
|
|
73
|
+
`;if(e.recursive){let m=t.filter(h=>h.category===e.category||h.category.startsWith(e.category+"/")),w=new Map;for(let h of m){let x=h.category.slice(e.category.length+1).split("/")[0]||"(\u76F4\u63A5)";w.has(x)||w.set(x,[]),w.get(x).push(h)}for(let[h,S]of w)g+=`### ${h}/
|
|
74
|
+
`,g+=S.map(x=>`- ${x.title} - ${x.id.padStart(3,"0")}`).join(`
|
|
75
75
|
`)+`
|
|
76
76
|
|
|
77
|
-
`}else
|
|
78
|
-
`+
|
|
77
|
+
`}else d.length>0&&(g+=`**\u5B50\u5206\u7C7B\uFF1A**
|
|
78
|
+
`+d.map(m=>`- ${m}/`).join(`
|
|
79
79
|
`)+`
|
|
80
80
|
|
|
81
|
-
`),
|
|
82
|
-
`+
|
|
83
|
-
`));return await
|
|
84
|
-
${$(
|
|
81
|
+
`),p.length>0&&(g+=`**\u76F4\u63A5\u6761\u76EE\uFF1A**
|
|
82
|
+
`+p.map(m=>`- ${m.title} - ${m.id.padStart(3,"0")}`).join(`
|
|
83
|
+
`));return await s("info",`[haimati_list] \u51FA\u53C2:
|
|
84
|
+
${$(g)}`),g}let l=`## \u6D77\u9A6C\u4F53\u7D22\u5F15
|
|
85
85
|
|
|
86
|
-
${
|
|
87
|
-
${$(
|
|
88
|
-
\u5806\u6808: ${
|
|
86
|
+
${ht(t)||"(\u7A7A)"}`;return await s("info",`[haimati_list] \u51FA\u53C2:
|
|
87
|
+
${$(l)}`),l}catch(t){let r=t instanceof Error?t.message:String(t),l=t instanceof Error?t.stack:"";return await s("error",`[haimati_list] \u51FA\u53C2: \u9519\u8BEF: ${r}
|
|
88
|
+
\u5806\u6808: ${l}`),`\u9519\u8BEF: ${r}`}}})}}var R=new Map,at=new Map,F=new Set,P=new Set,b=new Set,Zt=new Map,bn=async e=>{await Ut(),await s("info","\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u63D2\u4EF6(\u6587\u4EF6\u7248)debug\u7248\u672C04091402\u5DF2\u52A0\u8F7D");let{client:i}=e;return{tool:Qt(),"chat.message":async(c,t)=>{let{message:r,parts:l}=t,o=r.sessionID,a=r.id;if(await s("debug","[chat.message] ====== \u5F00\u59CB\u5904\u7406 ======"),await s("debug",`[chat.message] sessionId=${o}, messageId=${a}`),await s("debug",`[chat.message] enhancedSessions: ${JSON.stringify([...R.entries()])}`),R.get(o)===a){await s("debug",`[chat.message] \u4F1A\u8BDD ${o} \u7684\u6D88\u606F ${a} \u5DF2\u589E\u5F3A\u8FC7\uFF0C\u8DF3\u8FC7`);return}let u=l.some(d=>d.type==="text"&&d.text);if(await s("debug",`[chat.message] hasUserContent=${u}`),!u){await s("debug",`[chat.message] \u6D88\u606F ${a} \u4E0D\u5305\u542B\u7528\u6237\u5185\u5BB9\uFF0C\u8DF3\u8FC7`);return}if(R.has(o)){await s("debug",`[chat.message] \u4F1A\u8BDD ${o} \u5DF2\u589E\u5F3A\u8FC7\uFF0C\u4E0D\u662F\u9996\u6761\u7528\u6237\u6D88\u606F\uFF0C\u8DF3\u8FC7 | enhancedSessions.get=${R.get(o)}`);return}let f=null;try{let d=await i.command.list({});if(d.data){let g=d.data.find(m=>m.name===G.replace(/^\//,""));g&&g.template?(f=g.template,await s("info",`[chat.message] \u627E\u5230 ${G} \u547D\u4EE4\u7684 template`)):await s("warn",`[chat.message] \u672A\u627E\u5230 ${G} \u547D\u4EE4\u7684 template`)}}catch(d){let p=d instanceof Error?d.message:String(d);await s("error",`[chat.message] \u67E5\u8BE2 ${G} \u547D\u4EE4 template \u5931\u8D25: ${p}`)}if(f===null){await s("info",`[chat.message] \u4F1A\u8BDD ${o} \u672A\u627E\u5230 ${G} \u547D\u4EE4\u7684 template\uFF0C\u6807\u8BB0\u5E76\u8DF3\u8FC7`),R.set(o,a),await s("debug",`[chat.message] \u6807\u8BB0\u540E enhancedSessions: ${JSON.stringify([...R.entries()])}`);return}await s("info",`[chat.message] \u589E\u5F3A\u4F1A\u8BDD ${o} \u7684\u9996\u6761\u7528\u6237\u6D88\u606F ${a}`);for(let d of l)d.type==="text"&&(d.text=f+`
|
|
89
89
|
|
|
90
|
-
`+
|
|
90
|
+
`+d.text,await s("debug",`[chat.message] \u5DF2\u589E\u5F3A part ${d.id}\uFF0Ctemplate \u957F\u5EA6=${f.length}`));R.set(o,a),await s("info",`[chat.message] \u5B8C\u6210\u9996\u6761\u6D88\u606F\u589E\u5F3A (sessionID: ${o}, messageID: ${a})`),await s("debug",`[chat.message] \u6807\u8BB0\u540E enhancedSessions: ${JSON.stringify([...R.entries()])}`),await s("debug","[chat.message] ====== \u5904\u7406\u5B8C\u6210 ======")},event:async({event:c})=>{try{if(c.type==="session.created"){await s("debug",`[session.created] \u4E8B\u4EF6\u5C5E\u6027: ${JSON.stringify(c.properties)}`);let t=c.properties?.info?.id,r=c.properties?.info?.directory;if(await s("debug",`[session.created] \u5C1D\u8BD5\u4FDD\u5B58\u4F1A\u8BDD\u76EE\u5F55: (evt.properties as any).sessionID=${c.properties?.sessionID}, info.id=${t}, directory=${r}`),await s("debug",`[session.created] sessionDirectories.size=${at.size}, \u73B0\u6709key: ${Array.from(at.keys()).join(",")}`),r&&(at.set(t,r),await s("debug",`[session.created] \u4FDD\u5B58\u4F1A\u8BDD\u76EE\u5F55\u5B8C\u6210: sessionID=${t}, directory=${r}`)),!t){await s("warn","[session.created] \u65E0\u6CD5\u83B7\u53D6 sessionID\uFF0C\u8DF3\u8FC7");return}return}if(c.type==="session.error"){let t=c.properties?.sessionID,r=c.properties?.error;await s("debug",`[session.error] \u4E8B\u4EF6\u5C5E\u6027: ${JSON.stringify(c.properties)}, sessionID=${t}, error=${JSON.stringify(r)}`),r&&r.name==="MessageAbortedError"&&t&&(b.add(t),await s("info",`[session.error] \u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\u4F1A\u8BDD ${t}`));return}if(c.type==="session.idle"){let t=c.properties?.sessionID,r=(Zt.get(t)||0)+1;if(Zt.set(t,r),r===1&&await s("info",`[session.idle] \u7B2C1\u6B21\u89E6\u53D1 | \u72B6\u6001: userStopped=${b.has(t)}, injected=${F.has(t)}, injecting=${P.has(t)}`),r>1&&(P.has(t)||F.has(t))){await s("debug",`[session.idle] \u7B2C${r}\u6B21\u89E6\u53D1\u91CD\u590D\uFF0C\u8DF3\u8FC7 | sessionId=${t}`);return}if(!t){await s("warn","[session.idle] \u65E0\u6CD5\u83B7\u53D6 sessionID\uFF0C\u8DF3\u8FC7");return}if(r===1&&await s("debug",`[session.idle] \u8BE6\u7EC6\u72B6\u6001: userStoppedSessions=${JSON.stringify([...b])}, injectedSessions=${JSON.stringify([...F])}, injectingSessions=${JSON.stringify([...P])}`),F.has(t)){await s("debug",`[session.idle] \u4F1A\u8BDD ${t} \u5DF2\u6CE8\u5165\u8FC7\u63D0\u793A\u8BCD\uFF0C\u8DF3\u8FC7`);return}if(P.has(t)){if(await s("debug",`[session.idle] \u4F1A\u8BDD ${t} \u6B63\u5728\u6CE8\u5165\u4E2D\uFF0C\u7B49\u5F85...`),await new Promise(o=>setTimeout(o,1e3)),b.has(t)){await s("info",`[session.idle] \u7B49\u5F85\u540E\u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F | sessionId=${t}`),b.delete(t),P.delete(t);return}if(F.has(t)){await s("debug",`[session.idle] \u7B49\u5F85\u540E\u786E\u8BA4\u5DF2\u6CE8\u5165\uFF0C\u8DF3\u8FC7 | sessionId=${t}`);return}if(!b.has(t)){await s("debug",`[session.idle] \u7B49\u5F85\u540E\u672A\u68C0\u6D4B\u5230\u7528\u6237\u505C\u6B62\uFF0C\u7531\u9996\u6B21\u89E6\u53D1\u5904\u7406 | sessionId=${t}`),P.delete(t);return}await s("warn",`[session.idle] \u4F1A\u8BDD ${t} \u7B49\u5F85\u540E\u4ECD\u672A\u6CE8\u5165\u4F46\u4ECD\u5728\u6CE8\u5165\u4E2D\uFF0C\u8DF3\u8FC7`);return}if(P.add(t),await s("debug",`[session.idle] \u4F1A\u8BDD ${t} \u5F00\u59CB\u6CE8\u5165\uFF0C\u6807\u8BB0\u4E3A\u6B63\u5728\u6CE8\u5165`),await new Promise(o=>setTimeout(o,500)),await s("debug",`[session.idle] \u7B49\u5F85500ms\u540E\u68C0\u67E5: userStopped=${b.has(t)}, sessionId=${t}`),b.has(t)){await s("info",`[session.idle] \u7B49\u5F85\u540E\u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F | sessionId=${t}`),b.delete(t),P.delete(t);return}let l=null;try{let o=await i.command.list({});if(o.data){let u=o.data.find(f=>f.name===X.replace(/^\//,""));u&&u.template?(l=u.template,await s("info",`[session.idle] \u627E\u5230 ${X} \u547D\u4EE4\u7684 template`)):await s("warn",`[session.idle] \u672A\u627E\u5230 ${X} \u547D\u4EE4\u7684 template`)}}catch(o){let a=o instanceof Error?o.message:String(o);await s("error",`[session.idle] \u67E5\u8BE2\u547D\u4EE4 template \u5931\u8D25: ${a}`)}if(l===null){await s("info",`[session.idle] \u4F1A\u8BDD ${t} \u672A\u627E\u5230 /record-memory \u547D\u4EE4\u7684 template\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F`),P.delete(t);return}if(b.has(t)){await s("info",`[session.idle] \u6CE8\u5165\u524D\u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F | sessionId=${t}`),b.delete(t),P.delete(t);return}await s("info",`[session.idle] \u5F00\u59CB\u6CE8\u5165\u63D0\u793A\u8BCD | sessionId=${t}, promptText=${l.substring(0,50)}...`);try{await i.session.prompt({path:{id:t},body:{parts:[{type:"text",text:l}],noReply:!1}}),await s("info",`[session.idle] \u2705 \u6210\u529F\u6CE8\u5165\u63D0\u793A\u8BCD\u5230\u4F1A\u8BDD ${t}`),F.add(t),P.delete(t),await s("debug",`[session.idle] \u6CE8\u5165\u5B8C\u6210\uFF0C\u6E05\u7406\u72B6\u6001: injectedSessions=${JSON.stringify([...F])}, injectingSessions=${JSON.stringify([...P])}, userStoppedSessions=${JSON.stringify([...b])}`),at.delete(t)}catch(o){let a=o instanceof Error?o.message:String(o);await s("error",`[session.idle] \u274C \u6CE8\u5165\u63D0\u793A\u8BCD\u5931\u8D25: ${a}`),P.delete(t),await s("debug",`[session.idle] \u6CE8\u5165\u5931\u8D25\uFF0C\u6E05\u7406\u72B6\u6001: injectingSessions=${JSON.stringify([...P])}, userStoppedSessions=${JSON.stringify([...b])}`)}return}}catch(t){let r=t instanceof Error?t.message:String(t);await s("error",`[event] \u5904\u7406\u4E8B\u4EF6 ${c.type} \u5931\u8D25: ${r}`)}}}};export{bn as HaimatiPlugin};
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-haimati",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "OpenCode plugin for a file-based memory system inspired by the hippocampus, providing long-term memory storage and retrieval across sessions.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"type": "module",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-haimati",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "OpenCode plugin for a file-based memory system inspired by the hippocampus, providing long-term memory storage and retrieval across sessions.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"type": "module",
|