opencode-haimati 1.0.11 → 1.1.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/README.md +36 -6
- package/dist/index.js +82 -114
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
- 🔍 多模式搜索:支持序号精确查询和关键字模糊搜索
|
|
11
11
|
- ✏️ 完整 CRUD:读取、写入、更新(部分替换)、删除、移动记忆
|
|
12
12
|
- 🔄 版本并发控制:基于 version 的乐观锁,防止多会话并发冲突
|
|
13
|
-
-
|
|
14
|
-
- 📋
|
|
13
|
+
- ✨ 自动增强首条提示词:自动查询 `/start-work` 命令的 template 并添加到用户首条消息前
|
|
14
|
+
- 📋 日志追踪:完整的操作日志记录(按天覆盖)
|
|
15
15
|
- ⚡ 纯异步架构:所有文件操作非阻塞执行
|
|
16
16
|
|
|
17
17
|
## 安装
|
|
@@ -33,6 +33,15 @@ bun add -g opencode-haimati
|
|
|
33
33
|
}
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
## 配置文件
|
|
37
|
+
|
|
38
|
+
在项目根目录创建 `opencode-haimati.conf` 配置文件:
|
|
39
|
+
|
|
40
|
+
```ini
|
|
41
|
+
# 海马体存储路径(可选,默认 .haimati)
|
|
42
|
+
haimati-path: .haimati
|
|
43
|
+
```
|
|
44
|
+
|
|
36
45
|
## 海马体结构
|
|
37
46
|
|
|
38
47
|
```
|
|
@@ -53,12 +62,12 @@ bun add -g opencode-haimati
|
|
|
53
62
|
| 工具 | 说明 |
|
|
54
63
|
|------|------|
|
|
55
64
|
| `haimati_read` | 读取记忆内容(只支持序号查询,支持分页,返回带行号和版本号) |
|
|
56
|
-
| `haimati_write` |
|
|
57
|
-
| `haimati_search` |
|
|
65
|
+
| `haimati_write` | 写入新记忆(如果已存在相同分类和标题的记忆则报错,不允许覆盖) |
|
|
66
|
+
| `haimati_search` | 搜索记忆内容(标题、分类、序号、书页内容,match参数必填) |
|
|
58
67
|
| `haimati_edit` | 修改记忆内容(部分替换,行号范围 [offsetBegin, offsetEnd),offsetEnd 不包括) |
|
|
59
68
|
| `haimati_move` | 修改记忆的分类路径 |
|
|
60
|
-
| `haimati_delete` |
|
|
61
|
-
| `haimati_list` |
|
|
69
|
+
| `haimati_delete` | 删除记忆(支持序号或完整路径定位) |
|
|
70
|
+
| `haimati_list` | 列出记忆索引(支持递归遍历) |
|
|
62
71
|
|
|
63
72
|
## 海马体三大原则
|
|
64
73
|
|
|
@@ -81,6 +90,9 @@ haimati_write({
|
|
|
81
90
|
// 读取记忆(只支持序号查询)
|
|
82
91
|
haimati_read({ query: "086" })
|
|
83
92
|
|
|
93
|
+
// 分页读取
|
|
94
|
+
haimati_read({ query: "086", offset: 10, limit: 50 })
|
|
95
|
+
|
|
84
96
|
// 搜索记忆
|
|
85
97
|
haimati_search({ keyword: "WebSocket", match: "or", limit: 10 })
|
|
86
98
|
|
|
@@ -119,12 +131,30 @@ xxx项目/
|
|
|
119
131
|
└── 用户注册密码 - 033
|
|
120
132
|
```
|
|
121
133
|
|
|
134
|
+
## 书页文件格式
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
正文内容...
|
|
138
|
+
------system------
|
|
139
|
+
标题
|
|
140
|
+
创建时间 (ISO 8601)
|
|
141
|
+
version:1
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
最后4行是系统行:分隔符、标题、创建时间、版本号。
|
|
145
|
+
|
|
122
146
|
## 依赖要求
|
|
123
147
|
|
|
124
148
|
- OpenCode v0.15.0 或更高版本
|
|
125
149
|
- Node.js 或 Bun 运行时
|
|
126
150
|
- 项目目录的文件系统写入权限
|
|
127
151
|
|
|
152
|
+
## 日志
|
|
153
|
+
|
|
154
|
+
日志文件位于:`{系统临时目录}/haimati_logs/haimati.log`
|
|
155
|
+
|
|
156
|
+
日志按天覆盖,新的一天开始时清空旧日志。
|
|
157
|
+
|
|
128
158
|
## 许可证
|
|
129
159
|
|
|
130
160
|
MIT
|
package/dist/index.js
CHANGED
|
@@ -1,122 +1,90 @@
|
|
|
1
|
-
import{tool as I}from"@opencode-ai/plugin";import
|
|
2
|
-
`);for(let
|
|
3
|
-
`),
|
|
4
|
-
`)}async function
|
|
5
|
-
`);if(
|
|
6
|
-
`);return{title:
|
|
7
|
-
`);return{title:
|
|
8
|
-
`),
|
|
1
|
+
import{tool as I}from"@opencode-ai/plugin";import C from"path";import At from"os";import G from"fs";var q=".haimati",dt="\u7D22\u5F15.md";var mt="next-id.txt",ut="haimati.log";var gt="opencode-haimati.conf",ht="haimati-path";var A="/start-work";var B="/record-memory";function Ft(e){let o=C.join(e,gt);if(!G.existsSync(o))return null;try{let l=G.readFileSync(o,"utf-8").split(`
|
|
2
|
+
`);for(let t of l){let i=t.trim();if(!i||i.startsWith("#"))continue;let a=i.match(/^([^:]+):\s*(.+)$/);if(a){let s=a[1].trim(),c=a[2].trim();if(s===ht&&c)return C.resolve(e,c)}}}catch(n){console.error("[haimati] \u8BFB\u53D6\u914D\u7F6E\u6587\u4EF6\u5931\u8D25:",n)}return null}function k(e){let o=Ft(e);return o||C.join(e,q)}function M(e){return C.join(k(e),dt)}function H(e){return C.join(k(e),mt)}function et(e){return C.join(k(e),".lock")}function nt(e){return C.join(k(e),"\u4E66\u9875")}function F(e,o){let n=parseInt(o,10),l=Math.floor((n-1)/50)*50+1,t=l+50-1,i=`${String(l).padStart(3,"0")}-${String(t).padStart(3,"0")}`;return C.join(nt(e),i)}function J(e,o){let n=String(parseInt(o,10)).padStart(3,"0");return C.join(F(e,o),`${n}.md`)}function pt(){let e=At.tmpdir();return C.join(e,"haimati_logs",ut)}function L(e){return G.promises.mkdir(e,{recursive:!0}).then(()=>{})}function x(e){return G.promises.access(e).then(()=>!0).catch(()=>!1)}import wt from"fs";import jt from"path";import{promisify as yt}from"util";var $t=yt(wt.readFile),Ot=yt(wt.writeFile);function K(e){let o=0,n=0,l=e.length;for(;n<l;)if(e[n]==="\u2502"&&n+3<l&&e.slice(n+1,n+4)===" ")o++,n+=4;else if(e.slice(n,n+4)===" ")o++,n+=4;else{if((e[n]==="\u251C"||e[n]==="\u2514")&&n+3<l&&e.slice(n+1,n+4)==="\u2500\u2500 ")break;break}return o}function z(e){let n=e.trim().replace(/^[│├└─\s]+/,"").trim();return n==="/"?"/":!n.endsWith("/")||(n=n.slice(0,-1),!n)?null:n}function Rt(e){let o=[],n=e.split(`
|
|
3
|
+
`),l=[];for(let t of n){if(!t.trim())continue;let i=K(t),a=t.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(a){let s=a[1].trim(),c=String(parseInt(a[2],10)),d=l.slice(0,i).filter(f=>f!=="").join("/");o.push({id:c,category:d,title:s})}else{let s=z(t);if(s)if(s==="/"){l[i]="";for(let c=i+1;c<l.length;c++)delete l[c]}else{l[i]=s;for(let c=i+1;c<l.length;c++)delete l[c]}}}return o}function O(e,o){return e.find(n=>n.id===o)||null}function U(e,o,n){return e.find(l=>l.category===o&&l.title===n)||null}function xt(e,o){return e.filter(n=>n.id!==o)}function it(e){if(e.length===0)return"";let o={name:"/",children:[],entry:null};for(let t of e){let i=t.category.split("/"),a=o;for(let c=0;c<i.length;c++){let u=i[c];if(!u)continue;let d=a.children.find(f=>f.name===u);d||(d={name:u,children:[],entry:null},a.children.push(d)),a=d}let s={name:t.id,children:[],entry:t};a.children.push(s)}function n(t){t.children.sort((i,a)=>i.name.localeCompare(a.name,void 0,{sensitivity:"base"}));for(let i of t.children)n(i)}n(o);function l(t,i,a){let s=[],c=i+(a?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 "),u=t.name==="/"?"/":t.name+"/";s.push(`${c}${u}`),i=i+(a?" ":"\u2502 ");let d=t.children.filter(m=>m.entry===null),f=t.children.filter(m=>m.entry!==null),h=[...d,...f];for(let m=0;m<h.length;m++){let g=h[m],w=m===h.length-1;if(g.entry){let p=g.entry.id.padStart(3,"0"),E=i+(w?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ");s.push(`${E}${g.entry.title} - ${p}`)}else s.push(...l(g,i,w))}return s}return l(o,"",!0).join(`
|
|
4
|
+
`)}async function D(e){let o=M(e);if(!await x(o))return[];let n=await $t(o,"utf-8");return Rt(n)}async function R(e,o){let n=jt.join(e,q);await x(n)||await L(n);let l=it(o),t=M(e);await Ot(t,l,"utf-8")}async function It(e){let o=M(e);return await x(o)?$t(o,"utf-8"):""}import Y from"fs";import{promisify as V}from"util";var Wt=V(Y.readFile),qt=V(Y.writeFile),Bt=V(Y.unlink),Et=V(Y.mkdir);async function N(e,o){let n=J(e,o);if(!await x(n))return null;let t=(await Wt(n,"utf-8")).split(`
|
|
5
|
+
`);if(t.length<4)return null;let i="------system------",a=Math.max(0,t.length-5),s=-1;for(let m=t.length-1;m>=a;m--)if(t[m].trim()===i){s=m;break}if(s===-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 p=t[0],E=t[1],y=parseInt(t[2].substring(8).trim(),10)||1,b=t.slice(3).join(`
|
|
6
|
+
`);return{title:p,createdAt:E,content:b,version:y}}let g=t[0],w=t.join(`
|
|
7
|
+
`);return{title:g,createdAt:"",content:w,version:1}}if(s+3>=t.length)return null;let c=t.slice(0,s).join(`
|
|
8
|
+
`),u=t[s+1],d=t[s+2],f=t[s+3],h=parseInt(f.substring(8).trim(),10)||1;return{title:u,createdAt:d,content:c,version:h}}async function W(e,o,n,l,t,i=1){let a=nt(e);await x(a)||await Et(a,{recursive:!0});let s=F(e,o);await x(s)||await Et(s,{recursive:!0});let c=J(e,o),d=`------system------
|
|
9
9
|
${n}
|
|
10
|
-
${
|
|
11
|
-
version:${i}`,f=
|
|
12
|
-
${
|
|
13
|
-
`,"utf-8"):await
|
|
14
|
-
`,"utf-8")}catch(
|
|
15
|
-
${$(
|
|
16
|
-
${$(
|
|
17
|
-
${$(
|
|
18
|
-
${$(
|
|
19
|
-
`),
|
|
20
|
-
`);return await
|
|
21
|
-
${$(E)}`),E}catch(
|
|
22
|
-
\u5806\u6808: ${
|
|
23
|
-
${$(
|
|
24
|
-
`);try{let
|
|
25
|
-
\u8DEF\u5F84: ${
|
|
26
|
-
\u7248\u672C: 1`;return await
|
|
27
|
-
${$(
|
|
28
|
-
${$(
|
|
29
|
-
${$(
|
|
30
|
-
\u5806\u6808: ${i}`),`\u9519\u8BEF: ${
|
|
31
|
-
${$(
|
|
32
|
-
${$(p)}`),p}let
|
|
33
|
-
`),
|
|
34
|
-
`);for(let p of
|
|
35
|
-
`).filter(
|
|
36
|
-
`);return await
|
|
37
|
-
${$(
|
|
38
|
-
\u5806\u6808: ${
|
|
39
|
-
${$(
|
|
40
|
-
${$(
|
|
41
|
-
${$(
|
|
42
|
-
${$(
|
|
43
|
-
${$(
|
|
44
|
-
`).length;if(
|
|
45
|
-
${$(
|
|
46
|
-
${$(
|
|
47
|
-
`){if(
|
|
48
|
-
`),
|
|
49
|
-
`)&&!E,
|
|
50
|
-
`:"")+
|
|
51
|
-
\u8DEF\u5F84: ${
|
|
52
|
-
\u65B0\u7248\u672C: ${
|
|
53
|
-
${$(
|
|
54
|
-
\u5806\u6808: ${
|
|
55
|
-
${$(
|
|
56
|
-
${
|
|
57
|
-
`)}`}}if(!
|
|
58
|
-
${$(
|
|
59
|
-
\u8DEF\u5F84: ${
|
|
60
|
-
${$(
|
|
61
|
-
\u5806\u6808: ${
|
|
62
|
-
${$(
|
|
63
|
-
${
|
|
64
|
-
`)}`}}if(!
|
|
65
|
-
${$(
|
|
66
|
-
\u65E7\u8DEF\u5F84: ${
|
|
67
|
-
\u65B0\u8DEF\u5F84: ${
|
|
68
|
-
${$(
|
|
69
|
-
\u5806\u6808: ${
|
|
70
|
-
${$(
|
|
71
|
-
`),
|
|
10
|
+
${l}
|
|
11
|
+
version:${i}`,f=t?`${t}
|
|
12
|
+
${d}`:d;await qt(c,f,"utf-8")}async function Pt(e,o){let n=J(e,o);return await x(n)?(await Bt(n),!0):!1}async function X(e,o,n,l,t=!1){let i=o.toLowerCase().split(/\s+/).filter(s=>s.length>0);if(i.length===0)return[];let a=[];for(let s of e){let c=s.title.toLowerCase(),u=s.category.toLowerCase(),d=s.id.toLowerCase(),f=0,h=0;for(let g of i)(c.includes(g)||u.includes(g)||d.includes(g))&&f++;if(t&&l){let g=await N(l,s.id);if(g&&g.content){let w=g.content.toLowerCase();for(let p of i)w.includes(p)&&h++}}let m=f+h;n==="and"?m>=i.length&&a.push({entry:s,score:m}):(f>0||t&&h>0)&&a.push({entry:s,score:m})}return a.sort((s,c)=>c.score-s.score),a.map(s=>s.entry)}import Dt from"fs";import{promisify as Ct}from"util";import St from"fs";import{promisify as _t}from"util";var Ht=_t(St.writeFile),Jt=_t(St.unlink);async function bt(e){let o=k(e);await x(o)||await L(o);let n=et(e),l=Date.now(),t=50+Math.random()*20;for(;await x(n);){if(Date.now()-l>5e3)return!1;await new Promise(i=>setTimeout(i,t))}return await Ht(n,String(process.pid),"utf-8"),!0}async function Mt(e){let o=et(e);await x(o)&&await Jt(o)}import st from"fs";import Kt from"path";import{promisify as ot}from"util";var zt=ot(st.appendFile),Ut=ot(st.writeFile),Yt=ot(st.stat);function $(e,o=500){if(e.length<=o)return e;let n=Math.floor((o-20)/2);return e.substring(0,n)+"...(\u7701\u7565"+(e.length-o)+"\u5B57\u7B26)..."+e.substring(e.length-n)}async function Vt(e){try{let o=new Date,n=o.toLocaleDateString("zh-CN"),l=o.toLocaleTimeString("zh-CN",{hour12:!1}),t=pt(),i=Kt.dirname(t);await x(i)||await L(i);let a=!1;await x(t)&&(await Yt(t)).mtime.toLocaleDateString("zh-CN")!==n&&(a=!0);let s=`[${l}] ${e}`;a?await Ut(t,s+`
|
|
13
|
+
`,"utf-8"):await zt(t,s+`
|
|
14
|
+
`,"utf-8")}catch(o){console.error("[haimati] \u5199\u5165\u65E5\u5FD7\u6587\u4EF6\u5931\u8D25:",o)}}async function r(e,o){await Vt(`[${e.toUpperCase()}] ${o}`)}var Xt=Ct(Dt.readFile),Qt=Ct(Dt.writeFile);async function Zt(e){let o=H(e);if(!await x(o))return 1;let l=(await Xt(o,"utf-8")).trim(),t=parseInt(l,10);return!isNaN(t)&&t>0?t:1}async function te(e){let o=H(e);if(await x(o))return;let n=await D(e),l=0;for(let t of n){let i=parseInt(t.id,10);i>l&&(l=i)}await kt(e,l+1)}async function kt(e,o){let n=k(e);await x(n)||await L(n);let l=H(e);await Qt(l,String(o),"utf-8")}async function Lt(e,o,n,l,t,i=!1){if(/ - \d{3}$/.test(n))throw await r("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 r("info",`[allocateIdWithLock] \u5F00\u59CB\u5206\u914D\u5E8F\u53F7: category="${o}", title="${n}"`),!await bt(e))return await r("warn",`[allocateIdWithLock] \u83B7\u53D6\u9501\u5931\u8D25: category="${o}", title="${n}"`),null;await r("debug",`[allocateIdWithLock] \u83B7\u53D6\u9501\u6210\u529F: category="${o}", title="${n}"`);try{await te(e);let a=await D(e),s=a.find(f=>f.category===o&&f.title===n);if(s){if(!i)throw new Error(`\u8BB0\u5FC6\u5DF2\u5B58\u5728: ${o}/${n}\uFF0C\u5982\u9700\u66F4\u65B0\u8BF7\u4F7F\u7528 haimati_edit \u5DE5\u5177`);let f=await N(e,s.id),h=f?f.createdAt:l,m=f?f.version+1:1;return await W(e,s.id,n,h,t,m),await r("info",`[allocateIdWithLock] \u66F4\u65B0\u5DF2\u6709\u6761\u76EE: #${s.id} ${o}/${n}, \u65B0\u7248\u672C: ${m}`),null}let c=await Zt(e),u=String(c).padStart(3,"0");await W(e,u,n,l,t,1);let d={id:u,category:o,title:n};return a.push(d),await R(e,a),await kt(e,c+1),await r("info",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u6210\u529F: #${u} ${o}/${n}`),u}catch(a){let s=a instanceof Error?a.message:String(a);if(await r("error",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u5931\u8D25: ${s}`),s.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw a;return null}finally{await Mt(e)}}function Nt(){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,o){let{directory:n}=o,l=M(n);if(await r("info",`[haimati_read] \u5165\u53C2: query="${e.query}", offset=${e.offset||1}, limit=${e.limit||2e3}`),!await x(l)){let t=`\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728\u4E8E ${l}`;return await r("warn",`[haimati_read] \u51FA\u53C2:
|
|
15
|
+
${$(t)}`),t}try{let t=await D(n);await r("debug",`[haimati_read] \u8BFB\u53D6\u7D22\u5F15: \u5171 ${t.length} \u6761\u8BB0\u5F55`);let i=e.query.trim();if(!/^\d+$/.test(i)){let y="\u9519\u8BEF\uFF1Aquery \u5FC5\u987B\u662F\u5E8F\u53F7\uFF0C\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2";return await r("warn",`[haimati_read] \u51FA\u53C2:
|
|
16
|
+
${$(y)}`),y}let a=String(parseInt(i,10)),s=O(t,a);if(await r("debug",`[haimati_read] \u5E8F\u53F7\u67E5\u8BE2 #${i}: ${s?"\u627E\u5230":"\u672A\u627E\u5230"}`),!s){let y=`\u672A\u627E\u5230\u8BB0\u5FC6: ${e.query}`;return await r("info",`[haimati_read] \u51FA\u53C2:
|
|
17
|
+
${$(y)}`),y}let c=await N(n,s.id);if(!c){let y=`\u672A\u627E\u5230\u4E66\u9875: #${s.id.padStart(3,"0")}`;return await r("error",`[haimati_read] \u51FA\u53C2:
|
|
18
|
+
${$(y)}`),y}let u=F(n,s.id);await r("debug",`[haimati_read] \u8BFB\u53D6\u4E66\u9875: \u4E66\u9875/${u.split(/[\\/]/).pop()}/${s.id}.md, version=${c.version}`);let d=c.content.split(`
|
|
19
|
+
`),f=e.offset||1,h=e.limit||2e3,m=d.length,g=Math.max(0,f-1),w=Math.min(m,g+h),p=[];p.push(`version:${c.version}`);for(let y=g;y<w;y++)p.push(`${y+1}|${d[y]}`);let E=p.join(`
|
|
20
|
+
`);return await r("info",`[haimati_read] \u51FA\u53C2:
|
|
21
|
+
${$(E)}`),E}catch(t){let i=t instanceof Error?t.message:String(t),a=t instanceof Error?t.stack:"";return await r("error",`[haimati_read] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
22
|
+
\u5806\u6808: ${a}`),`\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=
|
|
23
|
+
${$(e.content)}
|
|
24
|
+
`);try{let l=new Date().toLocaleString("zh-CN",{hour12:!1}),t=null;for(let c=0;c<3;c++)try{if(t=await Lt(n,e.category,e.title,l,e.content,!1),t)break;await r("warn",`[haimati_write] \u9501\u83B7\u53D6\u5931\u8D25\uFF0C\u91CD\u8BD5\u7B2C ${c+1} \u6B21`)}catch(u){let d=u instanceof Error?u.message:String(u);if(d.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw u;await r("warn",`[haimati_write] \u83B7\u53D6\u9501\u5F02\u5E38: ${d}, \u91CD\u8BD5\u7B2C ${c+1} \u6B21`)}if(t){let c=F(n,t);await r("debug",`[haimati_write] \u5206\u914D\u65B0\u5E8F\u53F7: #${t}`),await r("debug",`[haimati_write] \u4E66\u9875\u548C\u7D22\u5F15\u5DF2\u66F4\u65B0: \u4E66\u9875/${c.split(/[\\/]/).pop()}/${t}.md`);let u=`\u5DF2\u5199\u5165\u6D77\u9A6C\u4F53 #${t}
|
|
25
|
+
\u8DEF\u5F84: ${e.category}/${e.title}
|
|
26
|
+
\u7248\u672C: 1`;return await r("info",`[haimati_write] \u51FA\u53C2:
|
|
27
|
+
${$(u)}`),u}let i=await D(n),a=U(i,e.category,e.title);if(a){let c=`\u9519\u8BEF\uFF1A\u8BB0\u5FC6\u5DF2\u5B58\u5728 #${a.id} (${e.category}/${e.title})\uFF0C\u5982\u9700\u66F4\u65B0\u8BF7\u4F7F\u7528 haimati_edit \u5DE5\u5177`;return await r("error",`[haimati_write] \u51FA\u53C2:
|
|
28
|
+
${$(c)}`),c}let s="\u9519\u8BEF\uFF1A\u65E0\u6CD5\u5206\u914D\u5E8F\u53F7\uFF08\u9501\u7B49\u5F85\u8D85\u65F6\uFF09";return await r("error",`[haimati_write] \u51FA\u53C2:
|
|
29
|
+
${$(s)}`),s}catch(l){let t=l instanceof Error?l.message:String(l),i=l instanceof Error?l.stack:"";return await r("error",`[haimati_write] \u51FA\u53C2: \u9519\u8BEF: ${t}
|
|
30
|
+
\u5806\u6808: ${i}`),`\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,o){let{directory:n}=o,l=M(n);if(await r("info",`[haimati_search] \u5165\u53C2: keyword="${e.keyword}", match="${e.match}", limit=${e.limit||10}, offset=${e.offset||0}`),!await x(l)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await r("warn",`[haimati_search] \u51FA\u53C2:
|
|
31
|
+
${$(t)}`),t}try{let t=await D(n),i=e.keyword.trim(),a=e.match,s=e.limit||10,c=e.offset||0,u=await X(t,i,a,n,!0),d=u.length,f=u.slice(c,c+s);if(f.length===0){let p=`\u672A\u627E\u5230\u5305\u542B '${i}' \u7684\u8BB0\u5FC6`;return await r("info",`[haimati_search] \u51FA\u53C2:
|
|
32
|
+
${$(p)}`),p}let h=[],m=c+f.length<d,g=m?`${c+1}-${c+f.length}/${d}`:`${c+1}-${c+f.length}/${d}`;h.push(`\u627E\u5230 ${g} \u6761\u5339\u914D\uFF1A
|
|
33
|
+
`),m&&h.push(`\uFF08\u5982\u9700\u83B7\u53D6\u66F4\u591A\uFF0C\u8BF7\u4F7F\u7528 offset=${c+s} \u7EE7\u7EED\u7FFB\u9875\uFF09
|
|
34
|
+
`);for(let p of f){let E=await N(n,p.id),y=`${p.category}/${p.title}`,b="";E&&(b=E.content.split(`
|
|
35
|
+
`).filter(Z=>Z.trim()).slice(0,3).join(" | ").substring(0,150),b.length===150&&(b+="...")),h.push(`## ${y} [${p.id}]`),b&&h.push(`> ${b}`),h.push("")}let w=h.join(`
|
|
36
|
+
`);return await r("info",`[haimati_search] \u51FA\u53C2:
|
|
37
|
+
${$(w)}`),w}catch(t){let i=t instanceof Error?t.message:String(t),a=t instanceof Error?t.stack:"";return await r("error",`[haimati_search] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
38
|
+
\u5806\u6808: ${a}`),`\u9519\u8BEF: ${i}`}}}),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 \u4E0D\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\u4E0D\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,o){let{directory:n}=o,l=M(n);if(await r("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 x(l)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await r("warn",`[haimati_edit] \u51FA\u53C2:
|
|
40
|
+
${$(t)}`),t}try{let t=await D(n),i=e.query.trim();if(!/^\d+$/.test(i)){let P="\u9519\u8BEF\uFF1Aquery \u5FC5\u987B\u662F\u5E8F\u53F7\uFF0C\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2";return await r("warn",`[haimati_edit] \u51FA\u53C2:
|
|
41
|
+
${$(P)}`),P}let a=String(parseInt(i,10)),s=O(t,a);if(!s){let P=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await r("info",`[haimati_edit] \u51FA\u53C2:
|
|
42
|
+
${$(P)}`),P}let c=await N(n,s.id);if(!c){let P=`\u672A\u627E\u5230\u4E66\u9875: #${s.id.padStart(3,"0")}`;return await r("error",`[haimati_edit] \u51FA\u53C2:
|
|
43
|
+
${$(P)}`),P}let u=e.offsetBegin,d=e.offsetEnd,f=c.content.split(`
|
|
44
|
+
`).length;if(u<1||u>=f+1||u>=d||d>f+1){let P=`\u9519\u8BEF\uFF1Aoffset \u975E\u6CD5\uFF0CoffsetBegin=${u}, offsetEnd=${d}, \u6587\u4EF6\u603B\u884C\u6570=${f}\uFF0C\u6709\u6548\u8303\u56F4 offsetBegin=[1, ${f+1}), offsetEnd=[offsetBegin+1, ${f+1}]`;return await r("warn",`[haimati_edit] \u51FA\u53C2:
|
|
45
|
+
${$(P)}`),P}if(c.version!==e.version){let P=`\u9519\u8BEF\uFF1A\u7248\u672C\u51B2\u7A81\uFF0C\u5F53\u524D version=${c.version}\uFF0C\u4F20\u5165 version=${e.version}\uFF0C\u8BF7\u91CD\u65B0\u8BFB\u53D6\u540E\u518D\u64CD\u4F5C`;return await r("warn",`[haimati_edit] \u51FA\u53C2:
|
|
46
|
+
${$(P)}`),P}let h=(P,lt)=>{if(lt===0)return 0;let ft=1,j=0;for(;j<P.length;){if(P[j]===`
|
|
47
|
+
`){if(ft===lt)return j+1;ft++}j++}return j},m=h(c.content,u-1),g=h(c.content,d-1),w=c.content.substring(0,Math.min(100,c.content.length));await r("debug",`[haimati_edit] content\u957F\u5EA6=${c.content.length}, content\u524D100\u5B57\u7B26: ${JSON.stringify(w)}, offsetBegin=${u}, offsetEnd=${d}, beforeEndIndex=${m}, afterStartIndex=${g}`);let E=(g<c.content.length?c.content.substring(g):"").startsWith(`
|
|
48
|
+
`),y=g<c.content.length&&!e.content.endsWith(`
|
|
49
|
+
`)&&!E,b=c.content.substring(0,m)+e.content+(y?`
|
|
50
|
+
`:"")+c.content.substring(g),v=c.version+1;await W(n,s.id,s.title,c.createdAt,b,v);let Z=`${s.category}/${s.title}`,ct=`\u5DF2\u66F4\u65B0\u6D77\u9A6C\u4F53 #${s.id.padStart(3,"0")}
|
|
51
|
+
\u8DEF\u5F84: ${Z}
|
|
52
|
+
\u65B0\u7248\u672C: ${v}`;return await r("info",`[haimati_edit] \u51FA\u53C2:
|
|
53
|
+
${$(ct)}`),ct}catch(t){let i=t instanceof Error?t.message:String(t),a=t instanceof Error?t.stack:"";return await r("error",`[haimati_edit] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
54
|
+
\u5806\u6808: ${a}`),`\u9519\u8BEF: ${i}`}}}),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,o){let{directory:n}=o,l=M(n);if(await r("info",`[haimati_delete] \u5165\u53C2: query="${e.query}"`),!await x(l)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await r("warn",`[haimati_delete] \u51FA\u53C2:
|
|
55
|
+
${$(t)}`),t}try{let t=await D(n),i=e.query.trim(),a=null;if(/^\d+$/.test(i))a=O(t,String(parseInt(i,10)));else{let d=i.split("/").filter(Boolean);if(d.length>=2){let f=d.pop(),h=d.join("/");a=U(t,h,f)}else{let f=await X(t,i,"or",n);if(f.length===1)a=f[0];else if(f.length>1)return`\u627E\u5230\u591A\u6761\u5339\u914D\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u7CBE\u786E\u6307\u5B9A\uFF1A
|
|
56
|
+
${f.slice(0,10).map(m=>` - ${m.category}/${m.title} [${m.id.padStart(3,"0")}]`).join(`
|
|
57
|
+
`)}`}}if(!a){let d=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await r("info",`[haimati_delete] \u51FA\u53C2:
|
|
58
|
+
${$(d)}`),d}await Pt(n,a.id);let s=xt(t,a.id);await R(n,s);let c=`${a.category}/${a.title}`,u=`\u5DF2\u5220\u9664\u6D77\u9A6C\u4F53 #${a.id.padStart(3,"0")}
|
|
59
|
+
\u8DEF\u5F84: ${c}`;return await r("info",`[haimati_delete] \u51FA\u53C2:
|
|
60
|
+
${$(u)}`),u}catch(t){let i=t instanceof Error?t.message:String(t),a=t instanceof Error?t.stack:"";return await r("error",`[haimati_delete] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
61
|
+
\u5806\u6808: ${a}`),`\u9519\u8BEF: ${i}`}}}),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,o){let{directory:n}=o,l=M(n);if(await r("info",`[haimati_move] \u5165\u53C2: query="${e.query}", newCategory="${e.newCategory}"`),!await x(l)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await r("warn",`[haimati_move] \u51FA\u53C2:
|
|
62
|
+
${$(t)}`),t}try{let t=await D(n),i=e.query.trim(),a=null;if(/^\d+$/.test(i))a=O(t,String(parseInt(i,10)));else{let m=i.split("/").filter(Boolean);if(m.length>=2){let g=m.pop(),w=m.join("/");a=U(t,w,g)}else{let g=await X(t,i,"or",n);if(g.length===1)a=g[0];else if(g.length>1)return`\u627E\u5230\u591A\u6761\u5339\u914D\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u7CBE\u786E\u6307\u5B9A\uFF1A
|
|
63
|
+
${g.slice(0,10).map(p=>` - ${p.category}/${p.title} [${p.id.padStart(3,"0")}]`).join(`
|
|
64
|
+
`)}`}}if(!a){let m=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await r("info",`[haimati_move] \u51FA\u53C2:
|
|
65
|
+
${$(m)}`),m}let c=`${a.category}/${a.title}`,u=e.newCategory,d=`${u}/${a.title}`,f=t.map(m=>m.id===a.id?{...m,category:u}:m);await R(n,f);let h=`\u5DF2\u79FB\u52A8\u6D77\u9A6C\u4F53 #${a.id}
|
|
66
|
+
\u65E7\u8DEF\u5F84: ${c}
|
|
67
|
+
\u65B0\u8DEF\u5F84: ${d}`;return await r("info",`[haimati_move] \u51FA\u53C2:
|
|
68
|
+
${$(h)}`),h}catch(t){let i=t instanceof Error?t.message:String(t),a=t instanceof Error?t.stack:"";return await r("error",`[haimati_move] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
69
|
+
\u5806\u6808: ${a}`),`\u9519\u8BEF: ${i}`}}}),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,o){let{directory:n}=o,l=M(n);if(await r("info",`[haimati_list] \u5165\u53C2: category="${e.category||"(\u65E0)"}, recursive=${e.recursive}"`),!await x(l)){let t="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await r("warn",`[haimati_list] \u51FA\u53C2:
|
|
70
|
+
${$(t)}`),t}try{let t=await D(n);if(e.category){let c=(await It(n)).split(`
|
|
71
|
+
`),u=0,d=[];for(let g of c){if(!g.trim())continue;let w=K(g);if(g.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/))continue;let E=z(g);if(E&&(E==="/"?d[w]="":d[w]=E,d=d.slice(0,w+1),d.filter(Boolean).join("/")===e.category)){u=w;break}}let f=[],h=[];d=[];for(let g of c){if(!g.trim())continue;let w=K(g),p=g.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(p){let y=p[1].trim(),b=p[2];if(w===u+1){let v=d.slice(1,w).filter(Boolean).join("/");v===e.category&&h.push({category:v,title:y,id:b})}continue}let E=z(g);E&&(E==="/"?d[w]="":d[w]=E,d=d.slice(0,w+1),w===u+1&&d.filter(Boolean).join("/")===e.category&&f.push(E))}let m=`## ${e.category}
|
|
72
72
|
|
|
73
|
-
`;if(
|
|
74
|
-
`,
|
|
73
|
+
`;if(e.recursive){let g=t.filter(p=>p.category===e.category||p.category.startsWith(e.category+"/")),w=new Map;for(let p of g){let y=p.category.slice(e.category.length+1).split("/")[0]||"(\u76F4\u63A5)";w.has(y)||w.set(y,[]),w.get(y).push(p)}for(let[p,E]of w)m+=`### ${p}/
|
|
74
|
+
`,m+=E.map(y=>`- ${y.title} - ${y.id.padStart(3,"0")}`).join(`
|
|
75
75
|
`)+`
|
|
76
76
|
|
|
77
|
-
`}else
|
|
78
|
-
`+
|
|
77
|
+
`}else f.length>0&&(m+=`**\u5B50\u5206\u7C7B\uFF1A**
|
|
78
|
+
`+f.map(g=>`- ${g}/`).join(`
|
|
79
79
|
`)+`
|
|
80
80
|
|
|
81
|
-
`),
|
|
82
|
-
`+
|
|
83
|
-
`));return await
|
|
84
|
-
${$(
|
|
81
|
+
`),h.length>0&&(m+=`**\u76F4\u63A5\u6761\u76EE\uFF1A**
|
|
82
|
+
`+h.map(g=>`- ${g.title} - ${g.id.padStart(3,"0")}`).join(`
|
|
83
|
+
`));return await r("info",`[haimati_list] \u51FA\u53C2:
|
|
84
|
+
${$(m)}`),m}let a=`## \u6D77\u9A6C\u4F53\u7D22\u5F15
|
|
85
85
|
|
|
86
|
-
${
|
|
87
|
-
${$(
|
|
88
|
-
\u5806\u6808: ${
|
|
86
|
+
${it(t)||"(\u7A7A)"}`;return await r("info",`[haimati_list] \u51FA\u53C2:
|
|
87
|
+
${$(a)}`),a}catch(t){let i=t instanceof Error?t.message:String(t),a=t instanceof Error?t.stack:"";return await r("error",`[haimati_list] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
88
|
+
\u5806\u6808: ${a}`),`\u9519\u8BEF: ${i}`}}})}}var at=new Map,Q=new Map,T=new Set,S=new Set,_=new Set,Tt=new Map,tn=async e=>{await r("info","\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u63D2\u4EF6(\u6587\u4EF6\u7248)debug\u7248\u672C04091402\u5DF2\u52A0\u8F7D");let{client:o}=e;return{tool:Nt(),"chat.message":async(l,t)=>{let{message:i,parts:a}=t,s=i.sessionID,c=i.id;if(at.get(s)===c){await r("debug",`[chat.message] \u4F1A\u8BDD ${s} \u7684\u6D88\u606F ${c} \u5DF2\u589E\u5F3A\u8FC7\uFF0C\u8DF3\u8FC7`);return}let u=a.some(f=>f.type==="text"&&f.text);if(await r("debug",`[chat.message] sessionId=${s}, messageId=${c}, hasUserContent=${u}`),!u){await r("debug",`[chat.message] \u6D88\u606F ${c} \u4E0D\u5305\u542B\u7528\u6237\u5185\u5BB9\uFF0C\u8DF3\u8FC7`);return}if(at.has(s)){await r("debug",`[chat.message] \u4F1A\u8BDD ${s} \u5DF2\u589E\u5F3A\u8FC7\uFF0C\u4E0D\u662F\u9996\u6761\u7528\u6237\u6D88\u606F\uFF0C\u8DF3\u8FC7`);return}let d=null;try{let f=await o.command.list({});if(f.data){let m=f.data.find(g=>g.name===A.replace(/^\//,""));m&&m.template?(d=m.template,await r("info",`[chat.message] \u627E\u5230 ${A} \u547D\u4EE4\u7684 template`)):await r("warn",`[chat.message] \u672A\u627E\u5230 ${A} \u547D\u4EE4\u7684 template`)}}catch(f){let h=f instanceof Error?f.message:String(f);await r("error",`[chat.message] \u67E5\u8BE2 ${A} \u547D\u4EE4 template \u5931\u8D25: ${h}`)}if(d===null){await r("info",`[chat.message] \u4F1A\u8BDD ${s} \u672A\u627E\u5230 ${A} \u547D\u4EE4\u7684 template\uFF0C\u4E0D\u589E\u5F3A\u9996\u6761\u63D0\u793A\u8BCD`);return}await r("info",`[chat.message] \u589E\u5F3A\u4F1A\u8BDD ${s} \u7684\u9996\u6761\u7528\u6237\u6D88\u606F ${c}`);for(let f of a)f.type==="text"&&(f.text=d+`
|
|
89
89
|
|
|
90
|
-
\
|
|
91
|
-
|
|
92
|
-
### \u6D77\u9A6C\u4F53\u4E09\u5927\u539F\u5219
|
|
93
|
-
|
|
94
|
-
1. **\u4FE1\u606F\u4E0D\u4E22\u5931**: \u5B8C\u6574\u4FDD\u7559\u6240\u6709\u6709\u4EF7\u503C\u7684\u4FE1\u606F\uFF0C\u8BB0\u4F4F
|
|
95
|
-
- \u5B8C\u6574\u7684\u4E1A\u52A1\u548C\u6280\u672F\u77E5\u8BC6
|
|
96
|
-
- \u4E0E\u7528\u6237\u7684\u9700\u6C42\u9897\u7C92\u5EA6\u5BF9\u9F50
|
|
97
|
-
- \u9009\u62E9\u7684\u51B3\u7B56\uFF0C\u4EE5\u53CA\u597D\u5904\u4E0E\u574F\u5904
|
|
98
|
-
- xxx\u662F\u4EC0\u4E48\u3001\u4E3A\u4EC0\u4E48xxx\uFF08\u4E3A\u4EC0\u4E48\u8FD9\u4E48\u53BB\u505A\uFF09\u3001xxx\u600E\u4E48\u529E\uFF08\u9047\u5230\u67D0\u67D0\u60C5\u51B5\uFF0C\u662F\u600E\u4E48\u529E\u7684\uFF09
|
|
99
|
-
- \u7B2C\u4E09\u65B9\u63A5\u53E3\u5B8C\u6574\u7684\u5165\u53C2\u3001\u51FA\u53C2\u3001\u8BF4\u660E\u3001\u8C03\u7528\u65F6\u673A
|
|
100
|
-
- \u5176\u4ED6\u5404\u79CD\u6709\u4EF7\u503C\u7684\u77E5\u8BC6
|
|
101
|
-
|
|
102
|
-
2. **\u9519\u8BEF\u4FE1\u606F\u4FEE\u6B63**: \u4E0D\u518D\u4FDD\u7559\u5DF2\u88AB\u7EA0\u6B63\u7684\u9519\u8BEF\u4FE1\u606F\u3001\u4E0D\u4FDD\u7559\u5DF2\u7ECF\u8FC7\u671F\u7684\u5E9F\u5F03\u7684\u4FE1\u606F
|
|
103
|
-
|
|
104
|
-
3. **\u62D2\u7EDD\u5197\u4F59**: \u4E0D\u8981\u91CD\u590D\u8BB0\u5F55\u76F8\u540C\u5185\u5BB9\uFF0C\u8282\u7EA6TOKEN
|
|
105
|
-
|
|
106
|
-
### \u53EF\u7528\u5DE5\u5177
|
|
107
|
-
|
|
108
|
-
**\u6CE8\u610F**\uFF1A\u4EE5\u4E0B\u5DE5\u5177\u5217\u8868\u5DF2\u66F4\u65B0\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u67E5\u8BE2\u4EE3\u66FF\u8DEF\u5F84\u67E5\u8BE2\u3002haimati_read/haimati_edit \u8FD4\u56DE\u7684 version \u7528\u4E8E\u5E76\u53D1\u63A7\u5236\u3002
|
|
109
|
-
|
|
110
|
-
- \`haimati_read\` - \u8BFB\u53D6\u8BB0\u5FC6\u5185\u5BB9\uFF08\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2\uFF0C\u652F\u6301\u5206\u9875\uFF0C\u8FD4\u56DE\u5E26\u884C\u53F7\u548C\u7248\u672C\u53F7\uFF09
|
|
111
|
-
- \`haimati_write\` - \u5199\u5165\u65B0\u8BB0\u5FC6\uFF08\u5982\u679C\u5DF2\u5B58\u5728\u76F8\u540C\u5206\u7C7B\u548C\u6807\u9898\u7684\u8BB0\u5FC6\u5219\u62A5\u9519\uFF0C\u4E0D\u5141\u8BB8\u8986\u76D6\uFF09
|
|
112
|
-
- \`haimati_search\` - \u641C\u7D22\u8BB0\u5FC6\u5185\u5BB9\uFF08\u641C\u7D22\u8303\u56F4\uFF1A\u6807\u9898\u3001\u5206\u7C7B\u3001\u5E8F\u53F7\u3001\u4E66\u9875\u5185\u5BB9\u3002**\u6CE8\u610F\uFF1Amatch\u53C2\u6570\u5FC5\u586B\uFF0Cand=\u6240\u6709\u5173\u952E\u5B57\u90FD\u5339\u914D\uFF0Cor=\u4EFB\u610F\u5173\u952E\u5B57\u5339\u914D**\uFF09
|
|
113
|
-
- \`haimati_edit\` - \u4FEE\u6539\u8BB0\u5FC6\u5185\u5BB9\uFF08\u90E8\u5206\u66FF\u6362\uFF0C\u901A\u8FC7\u884C\u53F7\u8303\u56F4\u66FF\u6362\u5185\u5BB9\uFF0C\u652F\u6301\u7248\u672C\u5E76\u53D1\u63A7\u5236\uFF09
|
|
114
|
-
- \`haimati_move\` - \u4FEE\u6539\u8BB0\u5FC6\u7684\u5206\u7C7B\u8DEF\u5F84
|
|
115
|
-
- \`haimati_delete\` - \u5220\u9664\u8BB0\u5FC6
|
|
116
|
-
- \`haimati_list\` - \u5217\u51FA\u8BB0\u5FC6\u7D22\u5F15
|
|
117
|
-
|
|
118
|
-
### \u4F7F\u7528\u65F6\u673A
|
|
119
|
-
|
|
120
|
-
**\u4F1A\u8BDD\u5F00\u59CB\u524D**: \u4F7F\u7528 haimati_list \u6216 haimati_search \u8BFB\u53D6\u4E0E\u5F53\u524D\u4EFB\u52A1\u76F8\u5173\u7684\u8BB0\u5FC6\u5185\u5BB9\uFF0C\u518D\u5F00\u59CB\u5DE5\u4F5C\u3002
|
|
121
|
-
|
|
122
|
-
**\u4F1A\u8BDD\u7ED3\u675F\u540E**: \u4F7F\u7528 haimati_write \u5C06\u65B0\u83B7\u5F97\u7684\u77E5\u8BC6\u3001\u7ECF\u9A8C\u3001\u51B3\u7B56\u5199\u5165\u6D77\u9A6C\u4F53\uFF0C\u4FDD\u6301\u4FE1\u606F\u7684\u6301\u7EED\u79EF\u7D2F\u3002`}]}}),await l("info",`[session.created] \u6D77\u9A6C\u4F53\u89C4\u5219\u5DF2\u6CE8\u5165\u65B0\u4F1A\u8BDD (sessionID: ${s})`)}catch(s){let e=s instanceof Error?s.message:String(s);await l("error",`[session.created] \u6CE8\u5165\u6D77\u9A6C\u4F53\u89C4\u5219\u5931\u8D25: ${e}`)}}});export{Ke as HaimatiPlugin};
|
|
90
|
+
`+f.text,await r("debug",`[chat.message] \u5DF2\u589E\u5F3A part ${f.id}\uFF0Ctemplate \u957F\u5EA6=${d.length}`));at.set(s,c),await r("info",`[chat.message] \u5B8C\u6210\u9996\u6761\u6D88\u606F\u589E\u5F3A (sessionID: ${s}, messageID: ${c})`)},event:async({event:l})=>{try{if(l.type==="session.created"){await r("debug",`[session.created] \u4E8B\u4EF6\u5C5E\u6027: ${JSON.stringify(l.properties)}`);let t=l.properties?.info?.id,i=l.properties?.info?.directory;if(await r("debug",`[session.created] \u5C1D\u8BD5\u4FDD\u5B58\u4F1A\u8BDD\u76EE\u5F55: evt.properties.sessionID=${l.properties?.sessionID}, info.id=${t}, directory=${i}`),await r("debug",`[session.created] sessionDirectories.size=${Q.size}, \u73B0\u6709key: ${Array.from(Q.keys()).join(",")}`),i&&(Q.set(t,i),await r("debug",`[session.created] \u4FDD\u5B58\u4F1A\u8BDD\u76EE\u5F55\u5B8C\u6210: sessionID=${t}, directory=${i}`)),!t){await r("warn","[session.created] \u65E0\u6CD5\u83B7\u53D6 sessionID\uFF0C\u8DF3\u8FC7");return}return}if(l.type==="session.error"){let t=l.properties?.sessionID,i=l.properties?.error;await r("debug",`[session.error] \u4E8B\u4EF6\u5C5E\u6027: ${JSON.stringify(l.properties)}, sessionID=${t}, error=${JSON.stringify(i)}`),i&&i.name==="MessageAbortedError"&&t&&(_.add(t),await r("info",`[session.error] \u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\u4F1A\u8BDD ${t}`));return}if(l.type==="session.idle"){let t=l.properties?.sessionID,i=(Tt.get(t)||0)+1;if(Tt.set(t,i),i===1&&await r("info",`[session.idle] \u7B2C1\u6B21\u89E6\u53D1 | \u72B6\u6001: userStopped=${_.has(t)}, injected=${T.has(t)}, injecting=${S.has(t)}`),i>1&&(S.has(t)||T.has(t))){await r("debug",`[session.idle] \u7B2C${i}\u6B21\u89E6\u53D1\u91CD\u590D\uFF0C\u8DF3\u8FC7 | sessionId=${t}`);return}if(!t){await r("warn","[session.idle] \u65E0\u6CD5\u83B7\u53D6 sessionID\uFF0C\u8DF3\u8FC7");return}if(i===1&&await r("debug",`[session.idle] \u8BE6\u7EC6\u72B6\u6001: userStoppedSessions=${JSON.stringify([..._])}, injectedSessions=${JSON.stringify([...T])}, injectingSessions=${JSON.stringify([...S])}`),T.has(t)){await r("debug",`[session.idle] \u4F1A\u8BDD ${t} \u5DF2\u6CE8\u5165\u8FC7\u63D0\u793A\u8BCD\uFF0C\u8DF3\u8FC7`);return}if(S.has(t)){if(await r("debug",`[session.idle] \u4F1A\u8BDD ${t} \u6B63\u5728\u6CE8\u5165\u4E2D\uFF0C\u7B49\u5F85...`),await new Promise(s=>setTimeout(s,1e3)),_.has(t)){await r("info",`[session.idle] \u7B49\u5F85\u540E\u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F | sessionId=${t}`),_.delete(t),S.delete(t);return}if(T.has(t)){await r("debug",`[session.idle] \u7B49\u5F85\u540E\u786E\u8BA4\u5DF2\u6CE8\u5165\uFF0C\u8DF3\u8FC7 | sessionId=${t}`);return}if(!_.has(t)){await r("debug",`[session.idle] \u7B49\u5F85\u540E\u672A\u68C0\u6D4B\u5230\u7528\u6237\u505C\u6B62\uFF0C\u7531\u9996\u6B21\u89E6\u53D1\u5904\u7406 | sessionId=${t}`),S.delete(t);return}await r("warn",`[session.idle] \u4F1A\u8BDD ${t} \u7B49\u5F85\u540E\u4ECD\u672A\u6CE8\u5165\u4F46\u4ECD\u5728\u6CE8\u5165\u4E2D\uFF0C\u8DF3\u8FC7`);return}if(S.add(t),await r("debug",`[session.idle] \u4F1A\u8BDD ${t} \u5F00\u59CB\u6CE8\u5165\uFF0C\u6807\u8BB0\u4E3A\u6B63\u5728\u6CE8\u5165`),await new Promise(s=>setTimeout(s,500)),await r("debug",`[session.idle] \u7B49\u5F85500ms\u540E\u68C0\u67E5: userStopped=${_.has(t)}, sessionId=${t}`),_.has(t)){await r("info",`[session.idle] \u7B49\u5F85\u540E\u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F | sessionId=${t}`),_.delete(t),S.delete(t);return}let a=null;try{let s=await o.command.list({});if(s.data){let u=s.data.find(d=>d.name===B.replace(/^\//,""));u&&u.template?(a=u.template,await r("info",`[session.idle] \u627E\u5230 ${B} \u547D\u4EE4\u7684 template`)):await r("warn",`[session.idle] \u672A\u627E\u5230 ${B} \u547D\u4EE4\u7684 template`)}}catch(s){let c=s instanceof Error?s.message:String(s);await r("error",`[session.idle] \u67E5\u8BE2\u547D\u4EE4 template \u5931\u8D25: ${c}`)}if(a===null){await r("info",`[session.idle] \u4F1A\u8BDD ${t} \u672A\u627E\u5230 /record-memory \u547D\u4EE4\u7684 template\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F`),S.delete(t);return}if(_.has(t)){await r("info",`[session.idle] \u6CE8\u5165\u524D\u68C0\u6D4B\u5230\u7528\u6237\u4E3B\u52A8\u505C\u6B62\uFF0C\u6E05\u7406\u5E76\u7ED3\u675F | sessionId=${t}`),_.delete(t),S.delete(t);return}await r("info",`[session.idle] \u5F00\u59CB\u6CE8\u5165\u63D0\u793A\u8BCD | sessionId=${t}, promptText=${a.substring(0,50)}...`);try{await o.session.prompt({path:{id:t},body:{parts:[{type:"text",text:a}],noReply:!1}}),await r("info",`[session.idle] \u2705 \u6210\u529F\u6CE8\u5165\u63D0\u793A\u8BCD\u5230\u4F1A\u8BDD ${t}`),T.add(t),S.delete(t),await r("debug",`[session.idle] \u6CE8\u5165\u5B8C\u6210\uFF0C\u6E05\u7406\u72B6\u6001: injectedSessions=${JSON.stringify([...T])}, injectingSessions=${JSON.stringify([...S])}, userStoppedSessions=${JSON.stringify([..._])}`),Q.delete(t)}catch(s){let c=s instanceof Error?s.message:String(s);await r("error",`[session.idle] \u274C \u6CE8\u5165\u63D0\u793A\u8BCD\u5931\u8D25: ${c}`),S.delete(t),await r("debug",`[session.idle] \u6CE8\u5165\u5931\u8D25\uFF0C\u6E05\u7406\u72B6\u6001: injectingSessions=${JSON.stringify([...S])}, userStoppedSessions=${JSON.stringify([..._])}`)}return}}catch(t){let i=t instanceof Error?t.message:String(t);await r("error",`[event] \u5904\u7406\u4E8B\u4EF6 ${l.type} \u5931\u8D25: ${i}`)}}}};export{tn as HaimatiPlugin};
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-haimati",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
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.
|
|
3
|
+
"version": "1.1.1",
|
|
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",
|