opencode-haimati 1.0.10 → 1.0.11
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/dist/index.js +73 -74
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,92 +1,91 @@
|
|
|
1
|
-
import{tool as I}from"@opencode-ai/plugin";import
|
|
2
|
-
`);for(let e of
|
|
3
|
-
`),
|
|
4
|
-
`)}async function b(t){let
|
|
5
|
-
`);if(e.length<
|
|
6
|
-
`);return{title:
|
|
7
|
-
`);return{title:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
${
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import{tool as I}from"@opencode-ai/plugin";import S from"path";import vt from"os";import O from"fs";var j=".haimati",it="\u7D22\u5F15.md";var ot="next-id.txt",at="haimati.log";var st="opencode-haimati.conf",ct="haimati-path";function Mt(t){let r=S.join(t,st);if(!O.existsSync(r))return null;try{let s=O.readFileSync(r,"utf-8").split(`
|
|
2
|
+
`);for(let e of s){let i=e.trim();if(!i||i.startsWith("#"))continue;let o=i.match(/^([^:]+):\s*(.+)$/);if(o){let c=o[1].trim(),a=o[2].trim();if(c===ct&&a)return S.resolve(t,a)}}}catch(n){console.error("[haimati] \u8BFB\u53D6\u914D\u7F6E\u6587\u4EF6\u5931\u8D25:",n)}return null}function M(t){let r=Mt(t);return r||S.join(t,j)}function P(t){return S.join(M(t),it)}function W(t){return S.join(M(t),ot)}function V(t){return S.join(M(t),".lock")}function J(t){return S.join(M(t),"\u4E66\u9875")}function T(t,r){let n=parseInt(r,10),s=Math.floor((n-1)/50)*50+1,e=s+50-1,i=`${String(s).padStart(3,"0")}-${String(e).padStart(3,"0")}`;return S.join(J(t),i)}function q(t,r){let n=String(parseInt(r,10)).padStart(3,"0");return S.join(T(t,r),`${n}.md`)}function lt(){let t=vt.tmpdir();return S.join(t,"haimati_logs",at)}function k(t){return O.promises.mkdir(t,{recursive:!0}).then(()=>{})}function x(t){return O.promises.access(t).then(()=>!0).catch(()=>!1)}import ft from"fs";import kt from"path";import{promisify as mt}from"util";var ut=mt(ft.readFile),Lt=mt(ft.writeFile);function B(t){let r=0,n=0,s=t.length;for(;n<s;)if(t[n]==="\u2502"&&n+3<s&&t.slice(n+1,n+4)===" ")r++,n+=4;else if(t.slice(n,n+4)===" ")r++,n+=4;else{if((t[n]==="\u251C"||t[n]==="\u2514")&&n+3<s&&t.slice(n+1,n+4)==="\u2500\u2500 ")break;break}return r}function R(t){let n=t.trim().replace(/^[│├└─\s]+/,"").trim();return n==="/"?"/":!n.endsWith("/")||(n=n.slice(0,-1),!n)?null:n}function Dt(t){let r=[],n=t.split(`
|
|
3
|
+
`),s=[];for(let e of n){if(!e.trim())continue;let i=B(e),o=e.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(o){let c=o[1].trim(),a=String(parseInt(o[2],10)),f=s.slice(0,i).filter(h=>h!=="").join("/");r.push({id:a,category:f,title:c})}else{let c=R(e);if(c)if(c==="/"){s[i]="";for(let a=i+1;a<s.length;a++)delete s[a]}else{s[i]=c;for(let a=i+1;a<s.length;a++)delete s[a]}}}return r}function N(t,r){return t.find(n=>n.id===r)||null}function G(t,r,n){return t.find(s=>s.category===r&&s.title===n)||null}function dt(t,r){return t.filter(n=>n.id!==r)}function Q(t){if(t.length===0)return"";let r={name:"/",children:[],entry:null};for(let e of t){let i=e.category.split("/"),o=r;for(let a=0;a<i.length;a++){let m=i[a];if(!m)continue;let f=o.children.find(h=>h.name===m);f||(f={name:m,children:[],entry:null},o.children.push(f)),o=f}let c={name:e.id,children:[],entry:e};o.children.push(c)}function n(e){e.children.sort((i,o)=>i.name.localeCompare(o.name,void 0,{sensitivity:"base"}));for(let i of e.children)n(i)}n(r);function s(e,i,o){let c=[],a=i+(o?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 "),m=e.name==="/"?"/":e.name+"/";c.push(`${a}${m}`),i=i+(o?" ":"\u2502 ");let f=e.children.filter(d=>d.entry===null),h=e.children.filter(d=>d.entry!==null),g=[...f,...h];for(let d=0;d<g.length;d++){let u=g[d],y=d===g.length-1;if(u.entry){let p=u.entry.id.padStart(3,"0"),E=i+(y?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ");c.push(`${E}${u.entry.title} - ${p}`)}else c.push(...s(u,i,y))}return c}return s(r,"",!0).join(`
|
|
4
|
+
`)}async function b(t){let r=P(t);if(!await x(r))return[];let n=await ut(r,"utf-8");return Dt(n)}async function F(t,r){let n=kt.join(t,j);await x(n)||await k(n);let s=Q(r),e=P(t);await Lt(e,s,"utf-8")}async function ht(t){let r=P(t);return await x(r)?ut(r,"utf-8"):""}import H from"fs";import{promisify as U}from"util";var Tt=U(H.readFile),Ct=U(H.writeFile),Nt=U(H.unlink),gt=U(H.mkdir);async function L(t,r){let n=q(t,r);if(!await x(n))return null;let e=(await Tt(n,"utf-8")).split(`
|
|
5
|
+
`);if(e.length<4)return null;let i=Math.max(0,e.length-5),o=-1;for(let g=e.length-1;g>=i;g--)if(e[g].trim()==="---"){o=g;break}if(o===-1){if(e.length>=3&&e[2].startsWith("version:")&&/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(e[1])){let y=e[0],p=e[1],E=parseInt(e[2].substring(8).trim(),10)||1,w=e.slice(3).join(`
|
|
6
|
+
`);return{title:y,createdAt:p,content:w,version:E}}let d=e[0],u=e.join(`
|
|
7
|
+
`);return{title:d,createdAt:"",content:u,version:1}}if(o+3>=e.length)return null;let c=e.slice(0,o).join(`
|
|
8
|
+
`),a=e[o+1],m=e[o+2],f=e[o+3],h=parseInt(f.substring(8).trim(),10)||1;return{title:a,createdAt:m,content:c,version:h}}async function A(t,r,n,s,e,i=1){let o=J(t);await x(o)||await gt(o,{recursive:!0});let c=T(t,r);await x(c)||await gt(c,{recursive:!0});let a=q(t,r),m=`---
|
|
9
|
+
${n}
|
|
10
|
+
${s}
|
|
11
|
+
version:${i}`,f=e?`${e}
|
|
12
|
+
${m}`:m;await Ct(a,f,"utf-8")}async function pt(t,r){let n=q(t,r);return await x(n)?(await Nt(n),!0):!1}async function X(t,r,n,s,e=!1){let i=r.toLowerCase().split(/\s+/).filter(c=>c.length>0);if(i.length===0)return[];let o=[];for(let c of t){let a=c.title.toLowerCase(),m=c.category.toLowerCase(),f=c.id.toLowerCase(),h=0,g=0;for(let u of i)(a.includes(u)||m.includes(u)||f.includes(u))&&h++;if(e&&s){let u=await L(s,c.id);if(u&&u.content){let y=u.content.toLowerCase();for(let p of i)y.includes(p)&&g++}}let d=h+g;n==="and"?d>=i.length&&o.push({entry:c,score:d}):(h>0||e&&g>0)&&o.push({entry:c,score:d})}return o.sort((c,a)=>a.score-c.score),o.map(c=>c.entry)}import It from"fs";import{promisify as Et}from"util";import yt from"fs";import{promisify as wt}from"util";var At=wt(yt.writeFile),jt=wt(yt.unlink);async function $t(t){let r=M(t);await x(r)||await k(r);let n=V(t),s=Date.now(),e=50+Math.random()*20;for(;await x(n);){if(Date.now()-s>5e3)return!1;await new Promise(i=>setTimeout(i,e))}return await At(n,String(process.pid),"utf-8"),!0}async function xt(t){let r=V(t);await x(r)&&await jt(r)}import Z from"fs";import Ot from"path";import{promisify as tt}from"util";var Wt=tt(Z.appendFile),qt=tt(Z.writeFile),Bt=tt(Z.stat);function $(t,r=500){if(t.length<=r)return t;let n=Math.floor((r-20)/2);return t.substring(0,n)+"...(\u7701\u7565"+(t.length-r)+"\u5B57\u7B26)..."+t.substring(t.length-n)}async function Rt(t){try{let r=new Date,n=r.toLocaleDateString("zh-CN"),s=r.toLocaleTimeString("zh-CN",{hour12:!1}),e=lt(),i=Ot.dirname(e);await x(i)||await k(i);let o=!1;await x(e)&&(await Bt(e)).mtime.toLocaleDateString("zh-CN")!==n&&(o=!0);let c=`[${s}] ${t}`;o?await qt(e,c+`
|
|
13
13
|
`,"utf-8"):await Wt(e,c+`
|
|
14
|
-
`,"utf-8")}catch(
|
|
15
|
-
${
|
|
16
|
-
${w
|
|
17
|
-
${w
|
|
18
|
-
${w
|
|
19
|
-
`),
|
|
14
|
+
`,"utf-8")}catch(r){console.error("[haimati] \u5199\u5165\u65E5\u5FD7\u6587\u4EF6\u5931\u8D25:",r)}}async function l(t,r){await Rt(`[${t.toUpperCase()}] ${r}`)}var Gt=Et(It.readFile),Ht=Et(It.writeFile);async function Ut(t){let r=W(t);if(!await x(r))return 1;let s=(await Gt(r,"utf-8")).trim(),e=parseInt(s,10);return!isNaN(e)&&e>0?e:1}async function Xt(t){let r=W(t);if(await x(r))return;let n=await b(t),s=0;for(let e of n){let i=parseInt(e.id,10);i>s&&(s=i)}await _t(t,s+1)}async function _t(t,r){let n=M(t);await x(n)||await k(n);let s=W(t);await Ht(s,String(r),"utf-8")}async function Pt(t,r,n,s,e,i=!1){if(/ - \d{3}$/.test(n))throw await l("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 l("info",`[allocateIdWithLock] \u5F00\u59CB\u5206\u914D\u5E8F\u53F7: category="${r}", title="${n}"`),!await $t(t))return await l("warn",`[allocateIdWithLock] \u83B7\u53D6\u9501\u5931\u8D25: category="${r}", title="${n}"`),null;await l("debug",`[allocateIdWithLock] \u83B7\u53D6\u9501\u6210\u529F: category="${r}", title="${n}"`);try{await Xt(t);let o=await b(t),c=o.find(h=>h.category===r&&h.title===n);if(c){if(!i)throw new Error(`\u8BB0\u5FC6\u5DF2\u5B58\u5728: ${r}/${n}\uFF0C\u5982\u9700\u66F4\u65B0\u8BF7\u4F7F\u7528 haimati_edit \u5DE5\u5177`);let h=await L(t,c.id),g=h?h.createdAt:s,d=h?h.version+1:1;return await A(t,c.id,n,g,e,d),await l("info",`[allocateIdWithLock] \u66F4\u65B0\u5DF2\u6709\u6761\u76EE: #${c.id} ${r}/${n}, \u65B0\u7248\u672C: ${d}`),null}let a=await Ut(t),m=String(a).padStart(3,"0");await A(t,m,n,s,e,1);let f={id:m,category:r,title:n};return o.push(f),await F(t,o),await _t(t,a+1),await l("info",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u6210\u529F: #${m} ${r}/${n}`),m}catch(o){let c=o instanceof Error?o.message:String(o);if(await l("error",`[allocateIdWithLock] \u5206\u914D\u5E8F\u53F7\u5931\u8D25: ${c}`),c.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw o;return null}finally{await xt(t)}}function bt(){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(t,r){let{directory:n}=r,s=P(n);if(await l("info",`[haimati_read] \u5165\u53C2: query="${t.query}", offset=${t.offset||1}, limit=${t.limit||2e3}`),!await x(s)){let e=`\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728\u4E8E ${s}`;return await l("warn",`[haimati_read] \u51FA\u53C2:
|
|
15
|
+
${$(e)}`),e}try{let e=await b(n);await l("debug",`[haimati_read] \u8BFB\u53D6\u7D22\u5F15: \u5171 ${e.length} \u6761\u8BB0\u5F55`);let i=t.query.trim();if(!/^\d+$/.test(i)){let w="\u9519\u8BEF\uFF1Aquery \u5FC5\u987B\u662F\u5E8F\u53F7\uFF0C\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2";return await l("warn",`[haimati_read] \u51FA\u53C2:
|
|
16
|
+
${$(w)}`),w}let o=String(parseInt(i,10)),c=N(e,o);if(await l("debug",`[haimati_read] \u5E8F\u53F7\u67E5\u8BE2 #${i}: ${c?"\u627E\u5230":"\u672A\u627E\u5230"}`),!c){let w=`\u672A\u627E\u5230\u8BB0\u5FC6: ${t.query}`;return await l("info",`[haimati_read] \u51FA\u53C2:
|
|
17
|
+
${$(w)}`),w}let a=await L(n,c.id);if(!a){let w=`\u672A\u627E\u5230\u4E66\u9875: #${c.id.padStart(3,"0")}`;return await l("error",`[haimati_read] \u51FA\u53C2:
|
|
18
|
+
${$(w)}`),w}let m=T(n,c.id);await l("debug",`[haimati_read] \u8BFB\u53D6\u4E66\u9875: \u4E66\u9875/${m.split(/[\\/]/).pop()}/${c.id}.md, version=${a.version}`);let f=a.content.split(`
|
|
19
|
+
`),h=t.offset||1,g=t.limit||2e3,d=f.length,u=Math.max(0,h-1),y=Math.min(d,u+g),p=[];p.push(`version:${a.version}`);for(let w=u;w<y;w++)p.push(`${w+1}|${f[w]}`);let E=p.join(`
|
|
20
20
|
`);return await l("info",`[haimati_read] \u51FA\u53C2:
|
|
21
|
-
${
|
|
22
|
-
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${
|
|
23
|
-
${
|
|
24
|
-
`);try{let
|
|
21
|
+
${$(E)}`),E}catch(e){let i=e instanceof Error?e.message:String(e),o=e instanceof Error?e.stack:"";return await l("error",`[haimati_read] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
22
|
+
\u5806\u6808: ${o}`),`\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(t,r){let{directory:n}=r;await l("info",`[haimati_write] \u5165\u53C2: category="${t.category}", title="${t.title}", content\u957F\u5EA6=${t.content.length}\u5B57\u7B26, content=
|
|
23
|
+
${$(t.content)}
|
|
24
|
+
`);try{let s=new Date().toISOString(),e=null;for(let a=0;a<3;a++)try{if(e=await Pt(n,t.category,t.title,s,t.content,!1),e)break;await l("warn",`[haimati_write] \u9501\u83B7\u53D6\u5931\u8D25\uFF0C\u91CD\u8BD5\u7B2C ${a+1} \u6B21`)}catch(m){let f=m instanceof Error?m.message:String(m);if(f.includes("\u8BB0\u5FC6\u5DF2\u5B58\u5728"))throw m;await l("warn",`[haimati_write] \u83B7\u53D6\u9501\u5F02\u5E38: ${f}, \u91CD\u8BD5\u7B2C ${a+1} \u6B21`)}if(e){let a=T(n,e);await l("debug",`[haimati_write] \u5206\u914D\u65B0\u5E8F\u53F7: #${e}`),await l("debug",`[haimati_write] \u4E66\u9875\u548C\u7D22\u5F15\u5DF2\u66F4\u65B0: \u4E66\u9875/${a.split(/[\\/]/).pop()}/${e}.md`);let m=`\u5DF2\u5199\u5165\u6D77\u9A6C\u4F53 #${e}
|
|
25
25
|
\u8DEF\u5F84: ${t.category}/${t.title}
|
|
26
26
|
\u7248\u672C: 1`;return await l("info",`[haimati_write] \u51FA\u53C2:
|
|
27
|
-
${
|
|
28
|
-
${
|
|
29
|
-
${
|
|
30
|
-
\u5806\u6808: ${
|
|
31
|
-
${
|
|
32
|
-
${
|
|
33
|
-
`),
|
|
34
|
-
`);for(let
|
|
35
|
-
`).filter(K=>K.trim()).slice(0,3).join(" | ").substring(0,150),
|
|
27
|
+
${$(m)}`),m}let i=await b(n),o=G(i,t.category,t.title);if(o){let a=`\u9519\u8BEF\uFF1A\u8BB0\u5FC6\u5DF2\u5B58\u5728 #${o.id} (${t.category}/${t.title})\uFF0C\u5982\u9700\u66F4\u65B0\u8BF7\u4F7F\u7528 haimati_edit \u5DE5\u5177`;return await l("error",`[haimati_write] \u51FA\u53C2:
|
|
28
|
+
${$(a)}`),a}let c="\u9519\u8BEF\uFF1A\u65E0\u6CD5\u5206\u914D\u5E8F\u53F7\uFF08\u9501\u7B49\u5F85\u8D85\u65F6\uFF09";return await l("error",`[haimati_write] \u51FA\u53C2:
|
|
29
|
+
${$(c)}`),c}catch(s){let e=s instanceof Error?s.message:String(s),i=s instanceof Error?s.stack:"";return await l("error",`[haimati_write] \u51FA\u53C2: \u9519\u8BEF: ${e}
|
|
30
|
+
\u5806\u6808: ${i}`),`\u9519\u8BEF: ${e}`}}}),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(t,r){let{directory:n}=r,s=P(n);if(await l("info",`[haimati_search] \u5165\u53C2: keyword="${t.keyword}", match="${t.match}", limit=${t.limit||10}, offset=${t.offset||0}`),!await x(s)){let e="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await l("warn",`[haimati_search] \u51FA\u53C2:
|
|
31
|
+
${$(e)}`),e}try{let e=await b(n),i=t.keyword.trim(),o=t.match,c=t.limit||10,a=t.offset||0,m=await X(e,i,o,n,!0),f=m.length,h=m.slice(a,a+c);if(h.length===0){let p=`\u672A\u627E\u5230\u5305\u542B '${i}' \u7684\u8BB0\u5FC6`;return await l("info",`[haimati_search] \u51FA\u53C2:
|
|
32
|
+
${$(p)}`),p}let g=[],d=a+h.length<f,u=d?`${a+1}-${a+h.length}/${f}`:`${a+1}-${a+h.length}/${f}`;g.push(`\u627E\u5230 ${u} \u6761\u5339\u914D\uFF1A
|
|
33
|
+
`),d&&g.push(`\uFF08\u5982\u9700\u83B7\u53D6\u66F4\u591A\uFF0C\u8BF7\u4F7F\u7528 offset=${a+c} \u7EE7\u7EED\u7FFB\u9875\uFF09
|
|
34
|
+
`);for(let p of h){let E=await L(n,p.id),w=`${p.category}/${p.title}`,v="";E&&(v=E.content.split(`
|
|
35
|
+
`).filter(K=>K.trim()).slice(0,3).join(" | ").substring(0,150),v.length===150&&(v+="...")),g.push(`## ${w} [${p.id}]`),v&&g.push(`> ${v}`),g.push("")}let y=g.join(`
|
|
36
36
|
`);return await l("info",`[haimati_search] \u51FA\u53C2:
|
|
37
|
-
${
|
|
38
|
-
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${
|
|
39
|
-
${
|
|
40
|
-
`)
|
|
41
|
-
${
|
|
42
|
-
${
|
|
43
|
-
${
|
|
44
|
-
|
|
45
|
-
`)
|
|
46
|
-
${
|
|
47
|
-
|
|
48
|
-
`)
|
|
49
|
-
`)
|
|
50
|
-
|
|
51
|
-
`:"")+s.content.substring(m),D=s.version+1;await A(n,c.id,c.title,s.createdAt,M,D);let K=`${c.category}/${c.title}`,tt=`\u5DF2\u66F4\u65B0\u6D77\u9A6C\u4F53 #${c.id}
|
|
37
|
+
${$(y)}`),y}catch(e){let i=e instanceof Error?e.message:String(e),o=e instanceof Error?e.stack:"";return await l("error",`[haimati_search] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
38
|
+
\u5806\u6808: ${o}`),`\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(t,r){let{directory:n}=r,s=P(n);if(await l("info",`[haimati_edit] \u5165\u53C2: query="${t.query}", offsetBegin=${t.offsetBegin}, offsetEnd=${t.offsetEnd}, content\u957F\u5EA6=${t.content.length}\u5B57\u7B26, version=${t.version}, content=
|
|
39
|
+
${$(t.content)}`),!await x(s)){let e="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await l("warn",`[haimati_edit] \u51FA\u53C2:
|
|
40
|
+
${$(e)}`),e}try{let e=await b(n),i=t.query.trim();if(!/^\d+$/.test(i)){let _="\u9519\u8BEF\uFF1Aquery \u5FC5\u987B\u662F\u5E8F\u53F7\uFF0C\u53EA\u652F\u6301\u5E8F\u53F7\u67E5\u8BE2";return await l("warn",`[haimati_edit] \u51FA\u53C2:
|
|
41
|
+
${$(_)}`),_}let o=String(parseInt(i,10)),c=N(e,o);if(!c){let _=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await l("info",`[haimati_edit] \u51FA\u53C2:
|
|
42
|
+
${$(_)}`),_}let a=await L(n,c.id);if(!a){let _=`\u672A\u627E\u5230\u4E66\u9875: #${c.id.padStart(3,"0")}`;return await l("error",`[haimati_edit] \u51FA\u53C2:
|
|
43
|
+
${$(_)}`),_}let m=t.offsetBegin,f=t.offsetEnd,h=a.content.split(`
|
|
44
|
+
`).length;if(m<1||m>=h+1||m>=f||f>h+1){let _=`\u9519\u8BEF\uFF1Aoffset \u975E\u6CD5\uFF0CoffsetBegin=${m}, offsetEnd=${f}, \u6587\u4EF6\u603B\u884C\u6570=${h}\uFF0C\u6709\u6548\u8303\u56F4 offsetBegin=[1, ${h+1}), offsetEnd=[offsetBegin+1, ${h+1}]`;return await l("warn",`[haimati_edit] \u51FA\u53C2:
|
|
45
|
+
${$(_)}`),_}if(a.version!==t.version){let _=`\u9519\u8BEF\uFF1A\u7248\u672C\u51B2\u7A81\uFF0C\u5F53\u524D version=${a.version}\uFF0C\u4F20\u5165 version=${t.version}\uFF0C\u8BF7\u91CD\u65B0\u8BFB\u53D6\u540E\u518D\u64CD\u4F5C`;return await l("warn",`[haimati_edit] \u51FA\u53C2:
|
|
46
|
+
${$(_)}`),_}let g=(_,nt)=>{if(nt===0)return 0;let rt=1,C=0;for(;C<_.length;){if(_[C]===`
|
|
47
|
+
`){if(rt===nt)return C+1;rt++}C++}return C},d=g(a.content,m-1),u=g(a.content,f-1),y=a.content.substring(0,Math.min(100,a.content.length));await l("debug",`[haimati_edit] content\u957F\u5EA6=${a.content.length}, content\u524D100\u5B57\u7B26: ${JSON.stringify(y)}, offsetBegin=${m}, offsetEnd=${f}, beforeEndIndex=${d}, afterStartIndex=${u}`);let E=(u<a.content.length?a.content.substring(u):"").startsWith(`
|
|
48
|
+
`),w=u<a.content.length&&!t.content.endsWith(`
|
|
49
|
+
`)&&!E,v=a.content.substring(0,d)+t.content+(w?`
|
|
50
|
+
`:"")+a.content.substring(u),D=a.version+1;await A(n,c.id,c.title,a.createdAt,v,D);let K=`${c.category}/${c.title}`,et=`\u5DF2\u66F4\u65B0\u6D77\u9A6C\u4F53 #${c.id.padStart(3,"0")}
|
|
52
51
|
\u8DEF\u5F84: ${K}
|
|
53
52
|
\u65B0\u7248\u672C: ${D}`;return await l("info",`[haimati_edit] \u51FA\u53C2:
|
|
54
|
-
${
|
|
55
|
-
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${
|
|
56
|
-
${
|
|
57
|
-
${
|
|
58
|
-
`)}`}}if(!o){let f=`\u672A\u627E\u5230\u4E0E '${
|
|
59
|
-
${
|
|
60
|
-
\u8DEF\u5F84: ${
|
|
61
|
-
${
|
|
62
|
-
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${
|
|
63
|
-
${
|
|
64
|
-
${
|
|
65
|
-
`)}`}}if(!o){let
|
|
66
|
-
${
|
|
67
|
-
\u65E7\u8DEF\u5F84: ${
|
|
53
|
+
${$(et)}`),et}catch(e){let i=e instanceof Error?e.message:String(e),o=e instanceof Error?e.stack:"";return await l("error",`[haimati_edit] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
54
|
+
\u5806\u6808: ${o}`),`\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(t,r){let{directory:n}=r,s=P(n);if(await l("info",`[haimati_delete] \u5165\u53C2: query="${t.query}"`),!await x(s)){let e="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await l("warn",`[haimati_delete] \u51FA\u53C2:
|
|
55
|
+
${$(e)}`),e}try{let e=await b(n),i=t.query.trim(),o=null;if(/^\d+$/.test(i))o=N(e,String(parseInt(i,10)));else{let f=i.split("/").filter(Boolean);if(f.length>=2){let h=f.pop(),g=f.join("/");o=G(e,g,h)}else{let h=await X(e,i,"or",n);if(h.length===1)o=h[0];else if(h.length>1)return`\u627E\u5230\u591A\u6761\u5339\u914D\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u7CBE\u786E\u6307\u5B9A\uFF1A
|
|
56
|
+
${h.slice(0,10).map(d=>` - ${d.category}/${d.title} [${d.id.padStart(3,"0")}]`).join(`
|
|
57
|
+
`)}`}}if(!o){let f=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await l("info",`[haimati_delete] \u51FA\u53C2:
|
|
58
|
+
${$(f)}`),f}await pt(n,o.id);let c=dt(e,o.id);await F(n,c);let a=`${o.category}/${o.title}`,m=`\u5DF2\u5220\u9664\u6D77\u9A6C\u4F53 #${o.id.padStart(3,"0")}
|
|
59
|
+
\u8DEF\u5F84: ${a}`;return await l("info",`[haimati_delete] \u51FA\u53C2:
|
|
60
|
+
${$(m)}`),m}catch(e){let i=e instanceof Error?e.message:String(e),o=e instanceof Error?e.stack:"";return await l("error",`[haimati_delete] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
61
|
+
\u5806\u6808: ${o}`),`\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(t,r){let{directory:n}=r,s=P(n);if(await l("info",`[haimati_move] \u5165\u53C2: query="${t.query}", newCategory="${t.newCategory}"`),!await x(s)){let e="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await l("warn",`[haimati_move] \u51FA\u53C2:
|
|
62
|
+
${$(e)}`),e}try{let e=await b(n),i=t.query.trim(),o=null;if(/^\d+$/.test(i))o=N(e,String(parseInt(i,10)));else{let d=i.split("/").filter(Boolean);if(d.length>=2){let u=d.pop(),y=d.join("/");o=G(e,y,u)}else{let u=await X(e,i,"or",n);if(u.length===1)o=u[0];else if(u.length>1)return`\u627E\u5230\u591A\u6761\u5339\u914D\uFF0C\u8BF7\u4F7F\u7528\u5E8F\u53F7\u7CBE\u786E\u6307\u5B9A\uFF1A
|
|
63
|
+
${u.slice(0,10).map(p=>` - ${p.category}/${p.title} [${p.id.padStart(3,"0")}]`).join(`
|
|
64
|
+
`)}`}}if(!o){let d=`\u672A\u627E\u5230\u4E0E '${i}' \u76F8\u5173\u7684\u8BB0\u5FC6`;return await l("info",`[haimati_move] \u51FA\u53C2:
|
|
65
|
+
${$(d)}`),d}let a=`${o.category}/${o.title}`,m=t.newCategory,f=`${m}/${o.title}`,h=e.map(d=>d.id===o.id?{...d,category:m}:d);await F(n,h);let g=`\u5DF2\u79FB\u52A8\u6D77\u9A6C\u4F53 #${o.id}
|
|
66
|
+
\u65E7\u8DEF\u5F84: ${a}
|
|
68
67
|
\u65B0\u8DEF\u5F84: ${f}`;return await l("info",`[haimati_move] \u51FA\u53C2:
|
|
69
|
-
${
|
|
70
|
-
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${
|
|
71
|
-
${
|
|
72
|
-
`),
|
|
68
|
+
${$(g)}`),g}catch(e){let i=e instanceof Error?e.message:String(e),o=e instanceof Error?e.stack:"";return await l("error",`[haimati_move] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
69
|
+
\u5806\u6808: ${o}`),`\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(t,r){let{directory:n}=r,s=P(n);if(await l("info",`[haimati_list] \u5165\u53C2: category="${t.category||"(\u65E0)"}, recursive=${t.recursive}"`),!await x(s)){let e="\u9519\u8BEF\uFF1A\u6D77\u9A6C\u4F53\u7D22\u5F15\u4E0D\u5B58\u5728";return await l("warn",`[haimati_list] \u51FA\u53C2:
|
|
70
|
+
${$(e)}`),e}try{let e=await b(n);if(t.category){let a=(await ht(n)).split(`
|
|
71
|
+
`),m=0,f=[];for(let u of a){if(!u.trim())continue;let y=B(u);if(u.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/))continue;let E=R(u);if(E&&(E==="/"?f[y]="":f[y]=E,f=f.slice(0,y+1),f.filter(Boolean).join("/")===t.category)){m=y;break}}let h=[],g=[];f=[];for(let u of a){if(!u.trim())continue;let y=B(u),p=u.match(/^\s*[│├└─\s]*(.+?)\s*-\s*(\d+)\s*$/);if(p){let w=p[1].trim(),v=p[2];if(y===m+1){let D=f.slice(1,y).filter(Boolean).join("/");D===t.category&&g.push({category:D,title:w,id:v})}continue}let E=R(u);E&&(E==="/"?f[y]="":f[y]=E,f=f.slice(0,y+1),y===m+1&&f.filter(Boolean).join("/")===t.category&&h.push(E))}let d=`## ${t.category}
|
|
73
72
|
|
|
74
|
-
`;if(t.recursive){let
|
|
75
|
-
`,
|
|
73
|
+
`;if(t.recursive){let u=e.filter(p=>p.category===t.category||p.category.startsWith(t.category+"/")),y=new Map;for(let p of u){let w=p.category.slice(t.category.length+1).split("/")[0]||"(\u76F4\u63A5)";y.has(w)||y.set(w,[]),y.get(w).push(p)}for(let[p,E]of y)d+=`### ${p}/
|
|
74
|
+
`,d+=E.map(w=>`- ${w.title} - ${w.id.padStart(3,"0")}`).join(`
|
|
76
75
|
`)+`
|
|
77
76
|
|
|
78
|
-
`}else
|
|
79
|
-
`+
|
|
77
|
+
`}else h.length>0&&(d+=`**\u5B50\u5206\u7C7B\uFF1A**
|
|
78
|
+
`+h.map(u=>`- ${u}/`).join(`
|
|
80
79
|
`)+`
|
|
81
80
|
|
|
82
|
-
`),
|
|
83
|
-
`+
|
|
81
|
+
`),g.length>0&&(d+=`**\u76F4\u63A5\u6761\u76EE\uFF1A**
|
|
82
|
+
`+g.map(u=>`- ${u.title} - ${u.id.padStart(3,"0")}`).join(`
|
|
84
83
|
`));return await l("info",`[haimati_list] \u51FA\u53C2:
|
|
85
|
-
${
|
|
84
|
+
${$(d)}`),d}let o=`## \u6D77\u9A6C\u4F53\u7D22\u5F15
|
|
86
85
|
|
|
87
86
|
${Q(e)||"(\u7A7A)"}`;return await l("info",`[haimati_list] \u51FA\u53C2:
|
|
88
|
-
${
|
|
89
|
-
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${
|
|
87
|
+
${$(o)}`),o}catch(e){let i=e instanceof Error?e.message:String(e),o=e instanceof Error?e.stack:"";return await l("error",`[haimati_list] \u51FA\u53C2: \u9519\u8BEF: ${i}
|
|
88
|
+
\u5806\u6808: ${o}`),`\u9519\u8BEF: ${i}`}}})}}var Ke=async t=>(await l("info","\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u63D2\u4EF6(\u6587\u4EF6\u7248)debug\u7248\u672C04091356\u5DF2\u52A0\u8F7D"),{tool:bt(),event:async({event:n})=>{if(n.type==="session.created")try{let s=n.properties?.info?.id;if(!s){await l("warn","[session.created] \u65E0\u6CD5\u83B7\u53D6 sessionID\uFF0C\u8DF3\u8FC7\u6CE8\u5165");return}if(!!1){await l("info","[session.created] \u81EA\u52A8\u6CE8\u5165\u5DF2\u5173\u95ED\uFF0C\u8DF3\u8FC7\u6CE8\u5165 (AUTO_INJECT_CONTEXT=false)");return}await t.client.session.prompt({path:{id:s},body:{noReply:!0,parts:[{type:"text",text:`## \u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u4F7F\u7528\u89C4\u5219
|
|
90
89
|
|
|
91
90
|
\u4F60\u6709\u4E00\u4E2A\u6D77\u9A6C\u4F53\u8BB0\u5FC6\u7CFB\u7EDF\u53EF\u7528\uFF0C\u7528\u4E8E\u5B58\u50A8\u548C\u68C0\u7D22\u8DE8\u4F1A\u8BDD\u7684\u77E5\u8BC6\u3002
|
|
92
91
|
|
|
@@ -120,4 +119,4 @@ ${w(o)}`),o}catch(e){let r=e instanceof Error?e.message:String(e),o=e instanceof
|
|
|
120
119
|
|
|
121
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
|
|
122
121
|
|
|
123
|
-
**\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: ${
|
|
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};
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-haimati",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
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.0.
|
|
3
|
+
"version": "1.0.11",
|
|
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",
|