opencode-haimati 1.1.2 → 1.1.4
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 +2 -2
- package/dist/index.js +67 -67
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ haimati-path: .haimati
|
|
|
69
69
|
| `haimati_read` | 读取记忆内容(只支持序号查询,支持分页,返回带行号和版本号) |
|
|
70
70
|
| `haimati_write` | 写入新记忆(如果已存在相同分类和标题的记忆则报错,不允许覆盖) |
|
|
71
71
|
| `haimati_search` | 搜索记忆内容(标题、分类、序号、书页内容,match参数必填) |
|
|
72
|
-
| `haimati_edit` | 修改记忆内容(部分替换,行号范围 [offsetBegin, offsetEnd
|
|
72
|
+
| `haimati_edit` | 修改记忆内容(部分替换,行号范围 [offsetBegin, offsetEnd],offsetEnd 包括,需传入 version 并发控制) |
|
|
73
73
|
| `haimati_move` | 修改记忆的分类路径 |
|
|
74
74
|
| `haimati_delete` | 删除记忆(支持序号或完整路径定位) |
|
|
75
75
|
| `haimati_list` | 列出记忆索引(支持递归遍历) |
|
|
@@ -96,7 +96,7 @@ haimati-path: .haimati
|
|
|
96
96
|
#### haimati_edit
|
|
97
97
|
- `query`: 序号(必填)
|
|
98
98
|
- `offsetBegin`: 起始行号(包括)
|
|
99
|
-
- `offsetEnd`:
|
|
99
|
+
- `offsetEnd`: 结束行号(包括)
|
|
100
100
|
- `content`: 替换内容
|
|
101
101
|
- `version`: read 时返回的 version,用于并发控制(必填)
|
|
102
102
|
|
package/dist/index.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import{tool as I}from"@opencode-ai/plugin";import
|
|
2
|
-
`);for(let t of l){let i=t.trim();if(!i||i.startsWith("#"))continue;let
|
|
3
|
-
`),l=[];for(let t of n){if(!t.trim())continue;let i=
|
|
4
|
-
`)}async function
|
|
5
|
-
`);if(t.length<4)return null;let i="------system------",
|
|
6
|
-
`);return{title:p,createdAt:E,content:
|
|
7
|
-
`);return{title:g,createdAt:"",content:w,version:1}}if(s+3>=t.length)return null;let
|
|
8
|
-
`),u=t[s+1],
|
|
1
|
+
import{tool as I}from"@opencode-ai/plugin";import M from"path";import At from"os";import H from"fs";var B=".haimati",dt="\u7D22\u5F15.md";var ut="next-id.txt",mt="haimati.log";var gt="opencode-haimati.conf",ht="haimati-path";var O="/start-work";var G="/record-memory";function Ft(e){let o=M.join(e,gt);if(!H.existsSync(o))return null;try{let l=H.readFileSync(o,"utf-8").split(`
|
|
2
|
+
`);for(let t of l){let i=t.trim();if(!i||i.startsWith("#"))continue;let c=i.match(/^([^:]+):\s*(.+)$/);if(c){let s=c[1].trim(),a=c[2].trim();if(s===ht&&a){let u=a.match(/^\$\{(\w+)\}$/);if(u){let f=process.env[u[1]];return f?M.resolve(e,f):(console.warn(`[haimati] \u73AF\u5883\u53D8\u91CF ${u[1]} \u672A\u5B9A\u4E49\u6216\u503C\u4E3A\u7A7A`),null)}return M.resolve(e,a)}}}}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||M.join(e,B)}function D(e){return M.join(k(e),dt)}function J(e){return M.join(k(e),ut)}function rt(e){return M.join(k(e),".lock")}function st(e){return M.join(k(e),"\u4E66\u9875")}function j(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 M.join(st(e),i)}function K(e,o){let n=String(parseInt(o,10)).padStart(3,"0");return M.join(j(e,o),`${n}.md`)}function pt(){let e=At.tmpdir();return M.join(e,"haimati_logs",mt)}function L(e){return H.promises.mkdir(e,{recursive:!0}).then(()=>{})}function x(e){return H.promises.access(e).then(()=>!0).catch(()=>!1)}import wt from"fs";import Ot from"path";import{promisify as yt}from"util";var $t=yt(wt.readFile),jt=yt(wt.writeFile);function z(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 U(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=z(t),c=t.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(c){let s=c[1].trim(),a=String(parseInt(c[2],10)),f=l.slice(0,i).filter(d=>d!=="").join("/");o.push({id:a,category:f,title:s})}else{let s=U(t);if(s)if(s==="/"){l[i]="";for(let a=i+1;a<l.length;a++)delete l[a]}else{l[i]=s;for(let a=i+1;a<l.length;a++)delete l[a]}}}return o}function R(e,o){return e.find(n=>n.id===o)||null}function V(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 ot(e){if(e.length===0)return"";let o={name:"/",children:[],entry:null};for(let t of e){let i=t.category.split("/"),c=o;for(let a=0;a<i.length;a++){let u=i[a];if(!u)continue;let f=c.children.find(d=>d.name===u);f||(f={name:u,children:[],entry:null},c.children.push(f)),c=f}let s={name:t.id,children:[],entry:t};c.children.push(s)}function n(t){t.children.sort((i,c)=>i.name.localeCompare(c.name,void 0,{sensitivity:"base"}));for(let i of t.children)n(i)}n(o);function l(t,i,c){let s=[],a=i+(c?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 "),u=t.name==="/"?"/":t.name+"/";s.push(`${a}${u}`),i=i+(c?" ":"\u2502 ");let f=t.children.filter(m=>m.entry===null),d=t.children.filter(m=>m.entry!==null),h=[...f,...d];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 C(e){let o=D(e);if(!await x(o))return[];let n=await $t(o,"utf-8");return Rt(n)}async function W(e,o){let n=Ot.join(e,B);await x(n)||await L(n);let l=ot(o),t=D(e);await jt(t,l,"utf-8")}async function It(e){let o=D(e);return await x(o)?$t(o,"utf-8"):""}import Y from"fs";import{promisify as X}from"util";var Wt=X(Y.readFile),qt=X(Y.writeFile),Bt=X(Y.unlink),Et=X(Y.mkdir);async function N(e,o){let n=K(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------",c=Math.max(0,t.length-5),s=-1;for(let m=t.length-1;m>=c;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,_=t.slice(3).join(`
|
|
6
|
+
`);return{title:p,createdAt:E,content:_,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 a=t.slice(0,s).join(`
|
|
8
|
+
`),u=t[s+1],f=t[s+2],d=t[s+3],h=parseInt(d.substring(8).trim(),10)||1;return{title:u,createdAt:f,content:a,version:h}}async function q(e,o,n,l,t,i=1){let c=st(e);await x(c)||await Et(c,{recursive:!0});let s=j(e,o);await x(s)||await Et(s,{recursive:!0});let a=K(e,o),f=`------system------
|
|
9
9
|
${n}
|
|
10
10
|
${l}
|
|
11
|
-
version:${i}`,
|
|
12
|
-
${
|
|
11
|
+
version:${i}`,d=t?`${t}
|
|
12
|
+
${f}`:f;await qt(a,d,"utf-8")}async function St(e,o){let n=K(e,o);return await x(n)?(await Bt(n),!0):!1}async function Q(e,o,n,l,t=!1){let i=o.toLowerCase().split(/\s+/).filter(s=>s.length>0);if(i.length===0)return[];let c=[];for(let s of e){let a=s.title.toLowerCase(),u=s.category.toLowerCase(),f=s.id.toLowerCase(),d=0,h=0;for(let g of i)(a.includes(g)||u.includes(g)||f.includes(g))&&d++;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=d+h;n==="and"?m>=i.length&&c.push({entry:s,score:m}):(d>0||t&&h>0)&&c.push({entry:s,score:m})}return c.sort((s,a)=>a.score-s.score),c.map(s=>s.entry)}import Dt from"fs";import{promisify as Ct}from"util";import Pt from"fs";import{promisify as bt}from"util";var Ht=bt(Pt.writeFile),Jt=bt(Pt.unlink);async function _t(e){let o=k(e);await x(o)||await L(o);let n=rt(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=rt(e);await x(o)&&await Jt(o)}import ct from"fs";import Kt from"path";import{promisify as lt}from"util";var zt=lt(ct.appendFile),Ut=lt(ct.writeFile),Vt=lt(ct.stat);function $(e,o=5e3){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 Yt(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 c=!1;await x(t)&&(await Vt(t)).mtime.toLocaleDateString("zh-CN")!==n&&(c=!0);let s=`[${l}] ${e}`;c?await Ut(t,s+`
|
|
13
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
|
|
15
|
-
${$(t)}`),t}try{let t=await
|
|
16
|
-
${$(y)}`),y}let
|
|
17
|
-
${$(y)}`),y}let
|
|
18
|
-
${$(y)}`),y}let u=
|
|
19
|
-
`),
|
|
14
|
+
`,"utf-8")}catch(o){console.error("[haimati] \u5199\u5165\u65E5\u5FD7\u6587\u4EF6\u5931\u8D25:",o)}}async function r(e,o){await Yt(`[${e.toUpperCase()}] ${o}`)}var Xt=Ct(Dt.readFile),Qt=Ct(Dt.writeFile);async function Zt(e){let o=J(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=J(e);if(await x(o))return;let n=await C(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=J(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 _t(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 c=await C(e),s=c.find(d=>d.category===o&&d.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 d=await N(e,s.id),h=d?d.createdAt:l,m=d?d.version+1:1;return await q(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 a=await Zt(e),u=String(a).padStart(3,"0");await q(e,u,n,l,t,1);let f={id:u,category:o,title:n};return c.push(f),await W(e,c),await kt(e,a+1),await r("info",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u6210\u529F: #${u} ${o}/${n}`),u}catch(c){let s=c instanceof Error?c.message:String(c);if(await r("error",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u5931\u8D25: ${s}`),s.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw c;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=D(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 C(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 c=String(parseInt(i,10)),s=R(t,c);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 a=await N(n,s.id);if(!a){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=j(n,s.id);await r("debug",`[haimati_read] \u8BFB\u53D6\u4E66\u9875: \u4E66\u9875/${u.split(/[\\/]/).pop()}/${s.id}.md, version=${a.version}`);let f=a.content.split(`
|
|
19
|
+
`),d=e.offset||1,h=e.limit||2e3,m=f.length,g=Math.max(0,d-1),w=Math.min(m,g+h),p=[];p.push(`version:${a.version}`);for(let y=g;y<w;y++)p.push(`${y+1}|${f[y]}`);let E=p.join(`
|
|
20
20
|
`);return await r("info",`[haimati_read] \u51FA\u53C2:
|
|
21
|
-
${$(E)}`),E}catch(t){let i=t instanceof Error?t.message:String(t),
|
|
22
|
-
\u5806\u6808: ${
|
|
21
|
+
${$(E)}`),E}catch(t){let i=t instanceof Error?t.message:String(t),c=t instanceof Error?t.stack:"";return await r("error",`[haimati_read] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
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=
|
|
23
23
|
${$(e.content)}
|
|
24
|
-
`);try{let l=new Date().toLocaleString("zh-CN",{hour12:!1}),t=null;for(let
|
|
24
|
+
`);try{let l=new Date().toLocaleString("zh-CN",{hour12:!1}),t=null;for(let a=0;a<3;a++)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 ${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 r("warn",`[haimati_write] \u83B7\u53D6\u9501\u5F02\u5E38: ${f}, \u91CD\u8BD5\u7B2C ${a+1} \u6B21`)}if(t){let a=j(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/${a.split(/[\\/]/).pop()}/${t}.md`);let u=`\u5DF2\u5199\u5165\u6D77\u9A6C\u4F53 #${t}
|
|
25
25
|
\u8DEF\u5F84: ${e.category}/${e.title}
|
|
26
26
|
\u7248\u672C: 1`;return await r("info",`[haimati_write] \u51FA\u53C2:
|
|
27
|
-
${$(u)}`),u}let i=await
|
|
28
|
-
${$(
|
|
27
|
+
${$(u)}`),u}let i=await C(n),c=V(i,e.category,e.title);if(c){let a=`\u9519\u8BEF\uFF1A\u8BB0\u5FC6\u5DF2\u5B58\u5728 #${c.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
|
+
${$(a)}`),a}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
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=
|
|
31
|
-
${$(t)}`),t}try{let t=await
|
|
32
|
-
${$(p)}`),p}let h=[],m=
|
|
33
|
-
`),m&&h.push(`\uFF08\u5982\u9700\u83B7\u53D6\u66F4\u591A\uFF0C\u8BF7\u4F7F\u7528 offset=${
|
|
34
|
-
`);for(let p of
|
|
35
|
-
`).filter(
|
|
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=D(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 C(n),i=e.keyword.trim(),c=e.match,s=e.limit||10,a=e.offset||0,u=await Q(t,i,c,n,!0),f=u.length,d=u.slice(a,a+s);if(d.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=a+d.length<f,g=m?`${a+1}-${a+d.length}/${f}`:`${a+1}-${a+d.length}/${f}`;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=${a+s} \u7EE7\u7EED\u7FFB\u9875\uFF09
|
|
34
|
+
`);for(let p of d){let E=await N(n,p.id),y=`${p.category}/${p.title}`,_="";E&&(_=E.content.split(`
|
|
35
|
+
`).filter(tt=>tt.trim()).slice(0,3).join(" | ").substring(0,150),_.length===150&&(_+="...")),h.push(`## ${y} [${p.id}]`),_&&h.push(`> ${_}`),h.push("")}let w=h.join(`
|
|
36
36
|
`);return await r("info",`[haimati_search] \u51FA\u53C2:
|
|
37
|
-
${$(w)}`),w}catch(t){let i=t instanceof Error?t.message:String(t),
|
|
38
|
-
\u5806\u6808: ${
|
|
37
|
+
${$(w)}`),w}catch(t){let i=t instanceof Error?t.message:String(t),c=t instanceof Error?t.stack:"";return await r("error",`[haimati_search] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
38
|
+
\u5806\u6808: ${c}`),`\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 \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,o){let{directory:n}=o,l=D(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
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
|
|
41
|
-
${$(
|
|
42
|
-
${$(
|
|
43
|
-
${$(
|
|
44
|
-
`).length;if(u<1||u
|
|
45
|
-
${$(
|
|
46
|
-
${$(
|
|
47
|
-
`){if(
|
|
48
|
-
`),y=g<
|
|
49
|
-
`)&&!E,
|
|
50
|
-
`:"")+
|
|
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: ${
|
|
60
|
-
${$(u)}`),u}catch(t){let i=t instanceof Error?t.message:String(t),
|
|
61
|
-
\u5806\u6808: ${
|
|
62
|
-
${$(t)}`),t}try{let t=await
|
|
40
|
+
${$(t)}`),t}try{let t=await C(n),i=e.query.trim();if(!/^\d+$/.test(i)){let S="\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
|
+
${$(S)}`),S}let c=String(parseInt(i,10)),s=R(t,c);if(!s){let S=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await r("info",`[haimati_edit] \u51FA\u53C2:
|
|
42
|
+
${$(S)}`),S}let a=await N(n,s.id);if(!a){let S=`\u672A\u627E\u5230\u4E66\u9875: #${s.id.padStart(3,"0")}`;return await r("error",`[haimati_edit] \u51FA\u53C2:
|
|
43
|
+
${$(S)}`),S}let u=e.offsetBegin,f=e.offsetEnd,d=a.content.split(`
|
|
44
|
+
`).length;if(u<1||u>d||u>f||f>d){let S=`\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 r("warn",`[haimati_edit] \u51FA\u53C2:
|
|
45
|
+
${$(S)}`),S}if(a.version!==e.version){let S=`\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 r("warn",`[haimati_edit] \u51FA\u53C2:
|
|
46
|
+
${$(S)}`),S}let h=(S,et)=>{if(et===0)return 0;let nt=1,F=0;for(;F<S.length;){if(S[F]===`
|
|
47
|
+
`){if(nt===et)return F+1;nt++}F++}return F},m=u===1?0:h(a.content,u-1),g=h(a.content,f),w=a.content.substring(0,Math.min(100,a.content.length));await r("debug",`[haimati_edit] content\u957F\u5EA6=${a.content.length}, content\u524D100\u5B57\u7B26: ${JSON.stringify(w)}, offsetBegin=${u}, offsetEnd=${f}, beforeEndIndex=${m}, afterStartIndex=${g}`);let E=(g<a.content.length?a.content.substring(g):"").startsWith(`
|
|
48
|
+
`),y=g<a.content.length&&!e.content.endsWith(`
|
|
49
|
+
`)&&!E,_=a.content.substring(0,m)+e.content+(y?`
|
|
50
|
+
`:"")+a.content.substring(g),A=a.version+1;await q(n,s.id,s.title,a.createdAt,_,A);let tt=`${s.category}/${s.title}`,ft=`\u5DF2\u66F4\u65B0\u6D77\u9A6C\u4F53 #${s.id.padStart(3,"0")}
|
|
51
|
+
\u8DEF\u5F84: ${tt}
|
|
52
|
+
\u65B0\u7248\u672C: ${A}`;return await r("info",`[haimati_edit] \u51FA\u53C2:
|
|
53
|
+
${$(ft)}`),ft}catch(t){let i=t instanceof Error?t.message:String(t),c=t instanceof Error?t.stack:"";return await r("error",`[haimati_edit] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
54
|
+
\u5806\u6808: ${c}`),`\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=D(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 C(n),i=e.query.trim(),c=null;if(/^\d+$/.test(i))c=R(t,String(parseInt(i,10)));else{let f=i.split("/").filter(Boolean);if(f.length>=2){let d=f.pop(),h=f.join("/");c=V(t,h,d)}else{let d=await Q(t,i,"or",n);if(d.length===1)c=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(m=>` - ${m.category}/${m.title} [${m.id.padStart(3,"0")}]`).join(`
|
|
57
|
+
`)}`}}if(!c){let f=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await r("info",`[haimati_delete] \u51FA\u53C2:
|
|
58
|
+
${$(f)}`),f}await St(n,c.id);let s=xt(t,c.id);await W(n,s);let a=`${c.category}/${c.title}`,u=`\u5DF2\u5220\u9664\u6D77\u9A6C\u4F53 #${c.id.padStart(3,"0")}
|
|
59
|
+
\u8DEF\u5F84: ${a}`;return await r("info",`[haimati_delete] \u51FA\u53C2:
|
|
60
|
+
${$(u)}`),u}catch(t){let i=t instanceof Error?t.message:String(t),c=t instanceof Error?t.stack:"";return await r("error",`[haimati_delete] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
61
|
+
\u5806\u6808: ${c}`),`\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=D(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 C(n),i=e.query.trim(),c=null;if(/^\d+$/.test(i))c=R(t,String(parseInt(i,10)));else{let m=i.split("/").filter(Boolean);if(m.length>=2){let g=m.pop(),w=m.join("/");c=V(t,w,g)}else{let g=await Q(t,i,"or",n);if(g.length===1)c=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
63
|
${g.slice(0,10).map(p=>` - ${p.category}/${p.title} [${p.id.padStart(3,"0")}]`).join(`
|
|
64
|
-
`)}`}}if(!
|
|
65
|
-
${$(m)}`),m}let
|
|
66
|
-
\u65E7\u8DEF\u5F84: ${
|
|
67
|
-
\u65B0\u8DEF\u5F84: ${
|
|
68
|
-
${$(h)}`),h}catch(t){let i=t instanceof Error?t.message:String(t),
|
|
69
|
-
\u5806\u6808: ${
|
|
70
|
-
${$(t)}`),t}try{let t=await
|
|
71
|
-
`),u=0,
|
|
64
|
+
`)}`}}if(!c){let m=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await r("info",`[haimati_move] \u51FA\u53C2:
|
|
65
|
+
${$(m)}`),m}let a=`${c.category}/${c.title}`,u=e.newCategory,f=`${u}/${c.title}`,d=t.map(m=>m.id===c.id?{...m,category:u}:m);await W(n,d);let h=`\u5DF2\u79FB\u52A8\u6D77\u9A6C\u4F53 #${c.id}
|
|
66
|
+
\u65E7\u8DEF\u5F84: ${a}
|
|
67
|
+
\u65B0\u8DEF\u5F84: ${f}`;return await r("info",`[haimati_move] \u51FA\u53C2:
|
|
68
|
+
${$(h)}`),h}catch(t){let i=t instanceof Error?t.message:String(t),c=t instanceof Error?t.stack:"";return await r("error",`[haimati_move] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
69
|
+
\u5806\u6808: ${c}`),`\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=D(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 C(n);if(e.category){let a=(await It(n)).split(`
|
|
71
|
+
`),u=0,f=[];for(let g of a){if(!g.trim())continue;let w=z(g);if(g.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/))continue;let E=U(g);if(E&&(E==="/"?f[w]="":f[w]=E,f=f.slice(0,w+1),f.filter(Boolean).join("/")===e.category)){u=w;break}}let d=[],h=[];f=[];for(let g of a){if(!g.trim())continue;let w=z(g),p=g.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(p){let y=p[1].trim(),_=p[2];if(w===u+1){let A=f.slice(1,w).filter(Boolean).join("/");A===e.category&&h.push({category:A,title:y,id:_})}continue}let E=U(g);E&&(E==="/"?f[w]="":f[w]=E,f=f.slice(0,w+1),w===u+1&&f.filter(Boolean).join("/")===e.category&&d.push(E))}let m=`## ${e.category}
|
|
72
72
|
|
|
73
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
74
|
`,m+=E.map(y=>`- ${y.title} - ${y.id.padStart(3,"0")}`).join(`
|
|
75
75
|
`)+`
|
|
76
76
|
|
|
77
|
-
`}else
|
|
78
|
-
`+
|
|
77
|
+
`}else d.length>0&&(m+=`**\u5B50\u5206\u7C7B\uFF1A**
|
|
78
|
+
`+d.map(g=>`- ${g}/`).join(`
|
|
79
79
|
`)+`
|
|
80
80
|
|
|
81
81
|
`),h.length>0&&(m+=`**\u76F4\u63A5\u6761\u76EE\uFF1A**
|
|
82
82
|
`+h.map(g=>`- ${g.title} - ${g.id.padStart(3,"0")}`).join(`
|
|
83
83
|
`));return await r("info",`[haimati_list] \u51FA\u53C2:
|
|
84
|
-
${$(m)}`),m}let
|
|
84
|
+
${$(m)}`),m}let c=`## \u6D77\u9A6C\u4F53\u7D22\u5F15
|
|
85
85
|
|
|
86
|
-
${
|
|
87
|
-
${$(
|
|
88
|
-
\u5806\u6808: ${
|
|
86
|
+
${ot(t)||"(\u7A7A)"}`;return await r("info",`[haimati_list] \u51FA\u53C2:
|
|
87
|
+
${$(c)}`),c}catch(t){let i=t instanceof Error?t.message:String(t),c=t instanceof Error?t.stack:"";return await r("error",`[haimati_list] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
88
|
+
\u5806\u6808: ${c}`),`\u9519\u8BEF: ${i}`}}})}}var v=new Map,Z=new Map,T=new Set,P=new Set,b=new Set,vt=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:c}=t,s=i.sessionID,a=i.id;if(await r("debug","[chat.message] ====== \u5F00\u59CB\u5904\u7406 ======"),await r("debug",`[chat.message] sessionId=${s}, messageId=${a}`),await r("debug",`[chat.message] enhancedSessions: ${JSON.stringify([...v.entries()])}`),v.get(s)===a){await r("debug",`[chat.message] \u4F1A\u8BDD ${s} \u7684\u6D88\u606F ${a} \u5DF2\u589E\u5F3A\u8FC7\uFF0C\u8DF3\u8FC7`);return}let u=c.some(d=>d.type==="text"&&d.text);if(await r("debug",`[chat.message] hasUserContent=${u}`),!u){await r("debug",`[chat.message] \u6D88\u606F ${a} \u4E0D\u5305\u542B\u7528\u6237\u5185\u5BB9\uFF0C\u8DF3\u8FC7`);return}if(v.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 | enhancedSessions.get=${v.get(s)}`);return}let f=null;try{let d=await o.command.list({});if(d.data){let m=d.data.find(g=>g.name===O.replace(/^\//,""));m&&m.template?(f=m.template,await r("info",`[chat.message] \u627E\u5230 ${O} \u547D\u4EE4\u7684 template`)):await r("warn",`[chat.message] \u672A\u627E\u5230 ${O} \u547D\u4EE4\u7684 template`)}}catch(d){let h=d instanceof Error?d.message:String(d);await r("error",`[chat.message] \u67E5\u8BE2 ${O} \u547D\u4EE4 template \u5931\u8D25: ${h}`)}if(f===null){await r("info",`[chat.message] \u4F1A\u8BDD ${s} \u672A\u627E\u5230 ${O} \u547D\u4EE4\u7684 template\uFF0C\u6807\u8BB0\u5E76\u8DF3\u8FC7`),v.set(s,a),await r("debug",`[chat.message] \u6807\u8BB0\u540E enhancedSessions: ${JSON.stringify([...v.entries()])}`);return}await r("info",`[chat.message] \u589E\u5F3A\u4F1A\u8BDD ${s} \u7684\u9996\u6761\u7528\u6237\u6D88\u606F ${a}`);for(let d of c)d.type==="text"&&(d.text=f+`
|
|
89
89
|
|
|
90
|
-
`+
|
|
90
|
+
`+d.text,await r("debug",`[chat.message] \u5DF2\u589E\u5F3A part ${d.id}\uFF0Ctemplate \u957F\u5EA6=${f.length}`));v.set(s,a),await r("info",`[chat.message] \u5B8C\u6210\u9996\u6761\u6D88\u606F\u589E\u5F3A (sessionID: ${s}, messageID: ${a})`),await r("debug",`[chat.message] \u6807\u8BB0\u540E enhancedSessions: ${JSON.stringify([...v.entries()])}`),await r("debug","[chat.message] ====== \u5904\u7406\u5B8C\u6210 ======")},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 as any).sessionID=${l.properties?.sessionID}, info.id=${t}, directory=${i}`),await r("debug",`[session.created] sessionDirectories.size=${Z.size}, \u73B0\u6709key: ${Array.from(Z.keys()).join(",")}`),i&&(Z.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&&(b.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=(vt.get(t)||0)+1;if(vt.set(t,i),i===1&&await r("info",`[session.idle] \u7B2C1\u6B21\u89E6\u53D1 | \u72B6\u6001: userStopped=${b.has(t)}, injected=${T.has(t)}, injecting=${P.has(t)}`),i>1&&(P.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([...b])}, injectedSessions=${JSON.stringify([...T])}, injectingSessions=${JSON.stringify([...P])}`),T.has(t)){await r("debug",`[session.idle] \u4F1A\u8BDD ${t} \u5DF2\u6CE8\u5165\u8FC7\u63D0\u793A\u8BCD\uFF0C\u8DF3\u8FC7`);return}if(P.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)),b.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}`),b.delete(t),P.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(!b.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}`),P.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(P.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=${b.has(t)}, sessionId=${t}`),b.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}`),b.delete(t),P.delete(t);return}let c=null;try{let s=await o.command.list({});if(s.data){let u=s.data.find(f=>f.name===G.replace(/^\//,""));u&&u.template?(c=u.template,await r("info",`[session.idle] \u627E\u5230 ${G} \u547D\u4EE4\u7684 template`)):await r("warn",`[session.idle] \u672A\u627E\u5230 ${G} \u547D\u4EE4\u7684 template`)}}catch(s){let a=s instanceof Error?s.message:String(s);await r("error",`[session.idle] \u67E5\u8BE2\u547D\u4EE4 template \u5931\u8D25: ${a}`)}if(c===null){await r("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 r("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 r("info",`[session.idle] \u5F00\u59CB\u6CE8\u5165\u63D0\u793A\u8BCD | sessionId=${t}, promptText=${c.substring(0,50)}...`);try{await o.session.prompt({path:{id:t},body:{parts:[{type:"text",text:c}],noReply:!1}}),await r("info",`[session.idle] \u2705 \u6210\u529F\u6CE8\u5165\u63D0\u793A\u8BCD\u5230\u4F1A\u8BDD ${t}`),T.add(t),P.delete(t),await r("debug",`[session.idle] \u6CE8\u5165\u5B8C\u6210\uFF0C\u6E05\u7406\u72B6\u6001: injectedSessions=${JSON.stringify([...T])}, injectingSessions=${JSON.stringify([...P])}, userStoppedSessions=${JSON.stringify([...b])}`),Z.delete(t)}catch(s){let a=s instanceof Error?s.message:String(s);await r("error",`[session.idle] \u274C \u6CE8\u5165\u63D0\u793A\u8BCD\u5931\u8D25: ${a}`),P.delete(t),await r("debug",`[session.idle] \u6CE8\u5165\u5931\u8D25\uFF0C\u6E05\u7406\u72B6\u6001: injectingSessions=${JSON.stringify([...P])}, userStoppedSessions=${JSON.stringify([...b])}`)}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.1.
|
|
3
|
+
"version": "1.1.4",
|
|
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.4",
|
|
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",
|