opencode-prompt-recorder 1.8.0 → 1.8.2
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 +11 -4
- package/dist/index.js +12 -8
- package/dist/package.json +3 -6
- package/package.json +3 -6
package/README.md
CHANGED
|
@@ -31,10 +31,11 @@ bun add -g opencode-prompt-recorder
|
|
|
31
31
|
|
|
32
32
|
## 工作原理
|
|
33
33
|
|
|
34
|
-
1. 插件监听 OpenCode
|
|
34
|
+
1. 插件监听 OpenCode 事件(`message.updated`、`message.part.updated`、`session.created`、`session.updated`)
|
|
35
35
|
2. 每次用户消息时,提取提示词文本和会话 ID
|
|
36
36
|
3. 同一 session 的消息合并到同一个文件,按日期组织存储
|
|
37
|
-
4.
|
|
37
|
+
4. task 工具创建的子 agent 会话自动存入 `task/` 子目录,与主会话隔离
|
|
38
|
+
5. 文件首行记录 SessionID,每条消息带有时间戳
|
|
38
39
|
|
|
39
40
|
## 文件结构
|
|
40
41
|
|
|
@@ -43,6 +44,11 @@ bun add -g opencode-prompt-recorder
|
|
|
43
44
|
```
|
|
44
45
|
.agent/
|
|
45
46
|
└── prompts/
|
|
47
|
+
├── task/
|
|
48
|
+
│ └── 2026/
|
|
49
|
+
│ └── 06/
|
|
50
|
+
│ └── 05/
|
|
51
|
+
│ └── 2606051003-子任务分析.txt
|
|
46
52
|
└── 2026/
|
|
47
53
|
└── 06/
|
|
48
54
|
└── 05/
|
|
@@ -66,11 +72,11 @@ bun add -g opencode-prompt-recorder
|
|
|
66
72
|
```markdown
|
|
67
73
|
============ SessionID: ses_xxxxx ============
|
|
68
74
|
|
|
69
|
-
============ 2026-06-05 10:05 ============
|
|
75
|
+
============ 2026-06-05 10:05:30 ============
|
|
70
76
|
|
|
71
77
|
什么是 AI?
|
|
72
78
|
|
|
73
|
-
============ 2026-06-05 10:50 ============
|
|
79
|
+
============ 2026-06-05 10:50:15 ============
|
|
74
80
|
|
|
75
81
|
如何编写 hello world 程序?
|
|
76
82
|
```
|
|
@@ -90,6 +96,7 @@ bun add -g opencode-prompt-recorder
|
|
|
90
96
|
- 同一 session 的后续消息追加到同一文件,文件名不变
|
|
91
97
|
- 文件名中的特殊字符会被自动清理(移除 `<>:"/\|?*` 及控制字符)
|
|
92
98
|
- 文件名截断至最多 40 个字符
|
|
99
|
+
- 若目标文件名已存在(极低概率),自动附加 `-xxxx` 随机后缀以避免覆盖
|
|
93
100
|
|
|
94
101
|
## 使用场景
|
|
95
102
|
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import{mkdir as
|
|
2
|
-
`;try{let
|
|
3
|
-
`)[0].trim().replace(/<[^>]*>/g,"").replace(/\s+/g," ").trim().replace(
|
|
1
|
+
import{mkdir as H,appendFile as z,writeFile as ee,readFile as J,rename as te,readdir as ve}from"fs/promises";import{join as p,dirname as ne,basename as Ee}from"path";import{fileURLToPath as Ae}from"url";import{readFile as ge,rm as ye}from"fs/promises";import{basename as P,dirname as h,join as L}from"path";import{fileURLToPath as we}from"url";var he="opencode-prompt-recorder",Y=!1;function Q(s,e){if(!e||Y)return;Y=!0;let n=new AbortController,o=setTimeout(()=>n.abort(),1e4);$e(n.signal).then(r=>{r.updated&&setTimeout(()=>{s.client.tui.showToast({body:{title:"Prompt Recorder \u66F4\u65B0",message:`${r.name} \u5DF2\u4ECE ${r.current} \u66F4\u65B0\u5230 ${r.latest}\uFF0C\u91CD\u542F OpenCode \u5B8C\u6210\u66F4\u65B0`,variant:"info"}})},5e3)}).catch(r=>{console.error("[prompt-recorder] auto-update check failed:",r)}).finally(()=>clearTimeout(o))}async function $e(s){let e=await ke(he);if(!e)return{updated:!1};let n=await N(L(e,"package.json"));if(!n?.name||!n.version)return{updated:!1};let o=await Se(n.name,s);if(!o||!Me(o,n.version))return{updated:!1};let r=await be(e,n.name);if(!r)return{updated:!1};try{await ye(r,{recursive:!0,force:!0})}catch{return{updated:!1,error:"remove_failed",name:n.name,current:n.version,latest:o}}return{updated:!0,name:n.name,current:n.version,latest:o}}async function ke(s){let e=h(we(import.meta.url));for(;;){if((await N(L(e,"package.json")))?.name===s)return P(e)==="dist"?h(e):e;let o=h(e);if(o===e)return;e=o}}async function be(s,e){let n=h(s),o=P(n).startsWith("@")?h(n):n;if(P(o)!=="node_modules")return;let r=h(o),c=await N(L(r,"package.json")),u=De(r,e)??c?.dependencies?.[e];if(!(!u||!Pe(u)))return r}function De(s,e){if(e.startsWith("@")){let[r,c]=e.split("/");if(!r||!c||P(h(s))!==r)return;let u=`${c}@`,g=P(s);return g.startsWith(u)?g.slice(u.length):void 0}let n=`${e}@`,o=P(s);return o.startsWith(n)?o.slice(n.length):void 0}function Pe(s){let e=s.trim();return e?!!(e==="latest"||e==="*"||/^[~^]/.test(e)||/^(?:>=|>|<=|<)/.test(e)||/\s+(?:\|\||-|[<>=])\s+/.test(e)):!1}async function N(s){try{let e=JSON.parse(await ge(s,"utf-8"));return e&&typeof e=="object"?e:void 0}catch{return}}async function Se(s,e){try{let n=await fetch(`https://registry.npmjs.org/${encodeURIComponent(s)}/latest`,{signal:e});if(!n.ok)return;let o=await n.json();if(!o||typeof o!="object")return;let r=o.version;return typeof r=="string"?r:void 0}catch{return}}function Me(s,e){let n=q(s),o=q(e);if(!n||!o)return!1;for(let r=0;r<3;r++)if(n.parts[r]!==o.parts[r])return n.parts[r]>o.parts[r];if(!n.pre.length&&o.pre.length)return!0;if(n.pre.length&&!o.pre.length)return!1;for(let r=0;r<Math.max(n.pre.length,o.pre.length);r++){let c=n.pre[r],u=o.pre[r];if(c===void 0)return!1;if(u===void 0)return!0;if(c===u)continue;let g=/^\d+$/.test(c)?Number(c):void 0,y=/^\d+$/.test(u)?Number(u):void 0;return g!==void 0&&y!==void 0?g>y:g!==void 0?!1:y!==void 0?!0:c>u}return!1}function q(s){let e=s.match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+.+)?$/);if(e)return{parts:[Number(e[1]),Number(e[2]),Number(e[3])],pre:e[4]?.split(".")??[]}}var _e=ne(Ae(import.meta.url));async function j(s,e){if(process.env.PROMPT_RECORDER_DEBUG!=="1"&&process.env.PROMPT_RECORDER_DEBUG!=="true")return;let o=`[${new Date().toISOString()}] ${e}
|
|
2
|
+
`;try{let r=p(s,".agent","prompts-log");await H(r,{recursive:!0}),await z(p(r,"log.txt"),o)}catch(r){console.error("debugLog failed:",r)}}var T="";async function Ie(){if(T)return T;try{return T=JSON.parse(await J(p(_e,"package.json"),"utf-8")).version||"unknown",T}catch{return"unknown"}}var Re=/[<>:"/\\|?*\x00-\x1f\u200B-\u200F\u2028-\u202E\uFEFF]/g;function O(s){return s.split(`
|
|
3
|
+
`)[0].trim().replace(/<[^>]*>/g,"").replace(/\s+/g," ").trim().replace(Re,"").substring(0,40).trim()||"untitled"}function Te(s){let e=s.trimStart();return e.startsWith("<system-reminder>")||e.startsWith("<system>")}function xe(s){let e=s.getFullYear().toString(),n=String(s.getMonth()+1).padStart(2,"0"),o=String(s.getDate()).padStart(2,"0"),r=String(s.getHours()).padStart(2,"0"),c=String(s.getMinutes()).padStart(2,"0"),u=String(s.getSeconds()).padStart(2,"0");return{yyyy:e,MM:n,dd:o,HH:r,mm:c,ss:u}}var x=".txt";async function Fe(s,e){return re(p(s,".agent","prompts"),e)}async function re(s,e){let n;try{n=await ve(s)}catch{return}for(let o of n){let r=p(s,o);try{if((await J(r,"utf-8")).includes(`SessionID: ${e}`))return r}catch{if(o.endsWith(x))continue;let c=await re(r,e);if(c)return c}}}var Ce=async s=>{Q(s,!0);let{directory:e}=s;fe();let n=new Map,o=new Map,r=new Map,c=new Map,u=1440*60*1e3,g=200,y=2e3,S=1440*60*1e3,ie=3600*1e3,m=new Map,$=new Map,F=new Map;function se(){if(m.size<g)return;let a=Date.now();for(let[t,i]of m)a-i.time>u&&m.delete(t)}function oe(){let a=Date.now();if(r.size>y)for(let[t,i]of r)a-i>S&&r.delete(t);if(o.size>y)for(let[t,i]of o)a-i.time>S&&o.delete(t);if(c.size>y)for(let[t,i]of c)a-i>S&&c.delete(t);if(n.size>y)for(let[t,i]of n)a-i.time>S&&n.delete(t);if($.size>0)for(let[t,i]of $)a-i.time>ie&&$.delete(t)}async function M(a,t){let i=ne(a.filepath),f=Ee(a.filepath).match(/^(\d{10})-/);if(!f)return;let l=p(i,`${f[1]}-${O(t)}${x}`);if(l!==a.filepath)try{await te(a.filepath,l),a.filepath=l}catch{let v=crypto.randomUUID().slice(0,8),E=p(i,`${f[1]}-${O(t)}-${v}${x}`);try{await te(a.filepath,E),a.filepath=E}catch(k){console.error(`[prompt-recorder] rename failed: ${a.filepath}`,k)}}}async function ae(a){let t=a.properties.info,i=t?.id,d=t?.role||t?.message?.role;i&&d==="user"&&o.set(i,{role:d,time:Date.now()})}async function ce(a){let t=a.properties.part;if(t?.type==="tool"&&t?.tool==="task"){let w=t.state?.metadata??t.metadata;if(w){let b=w.sessionId??w.sessionID;b&&(c.set(b,Date.now()),await j(e,`[prompt-recorder] tracked task session: ${b}`))}}if(t?.type!=="text"||!t?.text||t.synthetic||t.ignored)return;let i=t.sessionID,d=t.messageID,f=t.text,l=o.get(d)?.role;if(l||(l=t.message?.role),l||(l=a.properties.info?.role),l||(l=a.properties.info?.message?.role),l!=="user"||!f||!i)return;if(Te(f)){await j(e,`[prompt-recorder] filtered system-injected: sessionID=${i}`);return}let v=d?`${d}:${f}`:`${i}:${f}`;if(r.has(v))return;r.set(v,Date.now()),oe(),await j(e,`[prompt-recorder] event=${a.type}, role=${l}, sessionID=${i}, textLength=${f.length}, textPreview=${f.substring(0,50)}`);let E=new Date,{yyyy:k,MM:A,dd:_,HH:W,mm:G,ss:pe}=xe(E),V=p(e,".agent","prompts"),X=c.has(i)?p(V,"task",k,A,_):p(V,k,A,_);await H(X,{recursive:!0});let le=k.slice(-2),C=`============ ${k}-${A}-${_} ${W}:${G}:${pe} ============`,U=m.get(i);if(U)U.time=Date.now(),await z(U.filepath,`
|
|
4
4
|
|
|
5
|
-
${
|
|
5
|
+
${C}
|
|
6
6
|
|
|
7
|
-
${
|
|
7
|
+
${f}`);else{let w=await Fe(e,i);if(w)m.set(i,{filepath:w,time:Date.now()}),await z(w,`
|
|
8
8
|
|
|
9
|
-
${
|
|
9
|
+
${C}
|
|
10
10
|
|
|
11
|
-
${
|
|
11
|
+
${f}`);else{let B=O(n.get(i)?.title??f),D=`${le}${A}${_}${W}${G}-${B}${x}`,I=p(X,D),R=`============ SessionID: ${i} ============`;await ee(I,`${R}
|
|
12
|
+
|
|
13
|
+
${C}
|
|
14
|
+
|
|
15
|
+
${f}`),m.set(i,{filepath:I,time:Date.now()}),se();let K=$.get(i);if(K){$.delete(i);let Z=m.get(i);Z&&await M(Z,K.title)}}let b=F.get(i);b&&clearTimeout(b);let me=setTimeout(async()=>{F.delete(i);try{let D=(await s.client.session.get({path:{id:i}}))?.data?.title;if(!D)return;let I=n.get(i)?.title;if(D===I)return;n.set(i,{title:D,time:Date.now()});let R=m.get(i);R&&await M(R,D)}catch{}},5e3);F.set(i,me)}}async function ue(a){let t=a.properties.info;if(t?.id&&t?.title){n.set(t.id,{title:t.title,time:Date.now()});let i=m.get(t.id);i&&await M(i,t.title)}}async function fe(){try{let a=await Ie(),t=p(e,".agent"),i=p(t,"opencode-prompt-recorder-readme.txt"),d=`# OpenCode Prompt Recorder
|
|
12
16
|
|
|
13
17
|
\u81EA\u52A8\u8BB0\u5F55\u7528\u6237\u63D0\u793A\u8BCD\u5230 .agent/prompts \u76EE\u5F55\u7684\u63D2\u4EF6\u3002
|
|
14
18
|
|
|
15
19
|
\u7248\u672C\uFF1A${a}
|
|
16
20
|
\u4F5C\u8005\uFF1Aanarckk
|
|
17
|
-
\u9879\u76EE\u5730\u5740\uFF1Ahttps://github.com/anarckk/opencode-prompt-recorder`;try{if(await
|
|
21
|
+
\u9879\u76EE\u5730\u5740\uFF1Ahttps://github.com/anarckk/opencode-prompt-recorder`;try{if(await J(i,"utf-8")===d)return}catch{}await H(t,{recursive:!0}),await ee(i,d)}catch{}}async function de(a){let t=a.properties.info;if(t?.id&&t?.title){let i=n.get(t.id)?.title;if(n.set(t.id,{title:t.title,time:Date.now()}),i!==t.title){let d=m.get(t.id);d?await M(d,t.title):$.set(t.id,{title:t.title,time:Date.now()})}}}return{event:async({event:a})=>{switch(a.type){case"message.updated":await ae(a);break;case"message.part.updated":await ce(a);break;case"session.created":await ue(a);break;case"session.updated":await de(a);break}}}},Ge=Ce;export{Ge as default};
|
package/dist/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-prompt-recorder",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "OpenCode plugin for recording user prompts. Automatically saves user messages to a local file system with organized directory structure.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.js",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"import": "./dist/index.js"
|
|
11
|
-
"require": "./dist/index.js"
|
|
10
|
+
"import": "./dist/index.js"
|
|
12
11
|
}
|
|
13
12
|
},
|
|
14
13
|
"files": [
|
|
@@ -18,8 +17,7 @@
|
|
|
18
17
|
"build": "node -e \"fs.rmSync('dist',{force:true,recursive:true})\" && npx esbuild index.ts --bundle --platform=node --outdir=dist --format=esm --external:@opencode-ai/plugin --minify && node -e \"fs.copyFileSync('package.json','dist/package.json')\"",
|
|
19
18
|
"build:uncompressed": "node -e \"fs.rmSync('dist',{force:true,recursive:true})\" && npx esbuild index.ts --bundle --platform=node --outdir=dist --format=esm --external:@opencode-ai/plugin && node -e \"fs.copyFileSync('package.json','dist/package.json')\"",
|
|
20
19
|
"prepublishOnly": "npm run build",
|
|
21
|
-
"publish": "node scripts/publish-npmjs.js"
|
|
22
|
-
"test": "npx tsx test/index.ts"
|
|
20
|
+
"publish": "node scripts/publish-npmjs.js"
|
|
23
21
|
},
|
|
24
22
|
"keywords": [
|
|
25
23
|
"opencode",
|
|
@@ -46,7 +44,6 @@
|
|
|
46
44
|
},
|
|
47
45
|
"devDependencies": {
|
|
48
46
|
"@opencode-ai/plugin": "^0.15.0",
|
|
49
|
-
"@types/bun": "^1.3.1",
|
|
50
47
|
"esbuild": "^0.25.0",
|
|
51
48
|
"typescript": "^5.9.3"
|
|
52
49
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-prompt-recorder",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "OpenCode plugin for recording user prompts. Automatically saves user messages to a local file system with organized directory structure.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.js",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"import": "./dist/index.js"
|
|
11
|
-
"require": "./dist/index.js"
|
|
10
|
+
"import": "./dist/index.js"
|
|
12
11
|
}
|
|
13
12
|
},
|
|
14
13
|
"files": [
|
|
@@ -18,8 +17,7 @@
|
|
|
18
17
|
"build": "node -e \"fs.rmSync('dist',{force:true,recursive:true})\" && npx esbuild index.ts --bundle --platform=node --outdir=dist --format=esm --external:@opencode-ai/plugin --minify && node -e \"fs.copyFileSync('package.json','dist/package.json')\"",
|
|
19
18
|
"build:uncompressed": "node -e \"fs.rmSync('dist',{force:true,recursive:true})\" && npx esbuild index.ts --bundle --platform=node --outdir=dist --format=esm --external:@opencode-ai/plugin && node -e \"fs.copyFileSync('package.json','dist/package.json')\"",
|
|
20
19
|
"prepublishOnly": "npm run build",
|
|
21
|
-
"publish": "node scripts/publish-npmjs.js"
|
|
22
|
-
"test": "npx tsx test/index.ts"
|
|
20
|
+
"publish": "node scripts/publish-npmjs.js"
|
|
23
21
|
},
|
|
24
22
|
"keywords": [
|
|
25
23
|
"opencode",
|
|
@@ -46,7 +44,6 @@
|
|
|
46
44
|
},
|
|
47
45
|
"devDependencies": {
|
|
48
46
|
"@opencode-ai/plugin": "^0.15.0",
|
|
49
|
-
"@types/bun": "^1.3.1",
|
|
50
47
|
"esbuild": "^0.25.0",
|
|
51
48
|
"typescript": "^5.9.3"
|
|
52
49
|
}
|