opencode-prompt-recorder 1.3.4 → 1.3.6

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 CHANGED
@@ -2,10 +2,10 @@
2
2
  import { mkdir, appendFile, readdir, writeFile, readFile } from "fs/promises";
3
3
  import { join, dirname } from "path";
4
4
  import { fileURLToPath } from "url";
5
- var __dirname2 = dirname(fileURLToPath(import.meta.url));
5
+ var __dirname = dirname(fileURLToPath(import.meta.url));
6
6
  async function getVersion() {
7
7
  try {
8
- const packageJson = JSON.parse(await readFile(join(__dirname2, "package.json"), "utf-8"));
8
+ const packageJson = JSON.parse(await readFile(join(__dirname, "package.json"), "utf-8"));
9
9
  return packageJson.version;
10
10
  } catch {
11
11
  return "unknown";
@@ -26,8 +26,7 @@ function formatTime(date) {
26
26
  return `${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}`;
27
27
  }
28
28
  async function findExistingFile(directory, sessionId) {
29
- if (!sessionId)
30
- return null;
29
+ if (!sessionId) return null;
31
30
  const promptsBaseDir = join(directory, ".agent", "prompts");
32
31
  try {
33
32
  const years = await readdir(promptsBaseDir);
@@ -48,14 +47,72 @@ async function findExistingFile(directory, sessionId) {
48
47
  }
49
48
  }
50
49
  }
51
- } catch {}
50
+ } catch {
51
+ }
52
52
  return null;
53
53
  }
54
- var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
54
+ var OpenCodePromptRecorder = async ({ directory, client }) => {
55
55
  let lastUserMessage = "";
56
56
  let versionFileWritten = false;
57
+ let lastProcessedMessageCount = 0;
58
+ const messageRoleMap = /* @__PURE__ */ new Map();
57
59
  return {
58
- "chat.message": async (input, output) => {
60
+ "event": async ({ event }) => {
61
+ if (event.type === "message.updated") {
62
+ const info = event.properties.info;
63
+ const role = info?.role || info?.message?.role;
64
+ if (info?.id && role) {
65
+ messageRoleMap.set(info.id, role);
66
+ }
67
+ }
68
+ if (event.type === "message.part.updated") {
69
+ const part = event.properties.part;
70
+ if (part?.type === "text" && part?.text) {
71
+ const sessionID = part.sessionID;
72
+ const messageID = part.messageID;
73
+ const text2 = part.text;
74
+ let role = messageRoleMap.get(messageID);
75
+ if (!role) {
76
+ role = part.message?.role;
77
+ }
78
+ if (!role) {
79
+ role = event.properties.info?.role;
80
+ }
81
+ if (!role) {
82
+ role = event.properties.info?.message?.role;
83
+ }
84
+ if (role === "user" && text2 && sessionID) {
85
+ const now2 = /* @__PURE__ */ new Date();
86
+ const { yyyy: yyyy2, MM: MM2, dd: dd2, HH: HH2, mm: mm2 } = formatDate(now2);
87
+ const topic2 = sanitizeFilename(text2);
88
+ const promptDir2 = join(directory, ".agent", "prompts", yyyy2, MM2, dd2);
89
+ await mkdir(promptDir2, { recursive: true });
90
+ const existingFile2 = await findExistingFile(directory, sessionID);
91
+ const time2 = formatTime(now2);
92
+ const dateStr2 = `${yyyy2}${MM2}${dd2}`;
93
+ const timeTitle2 = `============ ${time2} ============`;
94
+ const fileContent2 = existingFile2 ? `
95
+
96
+ ${timeTitle2}
97
+
98
+ ${text2}` : `${timeTitle2}
99
+
100
+ ${text2}`;
101
+ if (existingFile2) {
102
+ await appendFile(existingFile2, fileContent2);
103
+ } else {
104
+ const filename = `${dateStr2}-${HH2}${mm2}-${sessionID}-${topic2}.md`;
105
+ const filepath = join(promptDir2, filename);
106
+ await appendFile(filepath, fileContent2);
107
+ }
108
+ }
109
+ }
110
+ }
111
+ if (event.type !== "session.updated") {
112
+ return;
113
+ }
114
+ const messages = event.properties.info.messages;
115
+ const messageCount = messages.length;
59
116
  if (!versionFileWritten) {
60
117
  try {
61
118
  const version = await getVersion();
@@ -63,60 +120,70 @@ var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
63
120
  const readmeFile = join(readmeDir, "opencode-prompt-recorder-readme.txt");
64
121
  const content = `# OpenCode Prompt Recorder
65
122
 
66
- 自动记录用户提示词到 .agent/prompts 目录的插件。
123
+ \u81EA\u52A8\u8BB0\u5F55\u7528\u6237\u63D0\u793A\u8BCD\u5230 .agent/prompts \u76EE\u5F55\u7684\u63D2\u4EF6\u3002
67
124
 
68
- 版本:${version}
69
- 作者:anarckk
70
- 项目地址:https://github.com/anarckk/opencode-prompt-recorder`;
125
+ \u7248\u672C\uFF1A${version}
126
+ \u4F5C\u8005\uFF1Aanarckk
127
+ \u9879\u76EE\u5730\u5740\uFF1Ahttps://github.com/anarckk/opencode-prompt-recorder`;
71
128
  try {
72
129
  const existing = await readFile(readmeFile, "utf-8");
73
130
  if (existing === content) {
74
131
  versionFileWritten = true;
75
132
  return;
76
133
  }
77
- } catch {}
134
+ } catch {
135
+ }
78
136
  await mkdir(readmeDir, { recursive: true });
79
137
  await writeFile(readmeFile, content);
80
138
  versionFileWritten = true;
81
- } catch (e) {}
82
- }
83
- if (output.message.role === "user") {
84
- const now = new Date;
85
- const text = output.parts.map((p) => p.type === "text" ? p.text : "").join("");
86
- const sessionId = input.sessionID;
87
- if (!sessionId) {
88
- return;
139
+ } catch (e) {
89
140
  }
90
- if (text && text !== lastUserMessage) {
91
- const { yyyy, MM, dd, HH, mm } = formatDate(now);
92
- const topic = sanitizeFilename(text);
93
- const promptDir = join(directory, ".agent", "prompts", yyyy, MM, dd);
94
- await mkdir(promptDir, { recursive: true });
95
- const existingFile = await findExistingFile(directory, sessionId);
96
- const time = formatTime(now);
97
- const timeTitle = `============ ${time} ============`;
98
- const fileContent = existingFile ? `
141
+ }
142
+ if (messageCount <= lastProcessedMessageCount) {
143
+ return;
144
+ }
145
+ const latestMessage = messages[messages.length - 1];
146
+ if (latestMessage?.role !== "user") {
147
+ return;
148
+ }
149
+ const text = latestMessage.parts.map((p) => p.type === "text" ? p.text : "").join("");
150
+ const sessionId = event.properties.info.id;
151
+ if (!text || !sessionId) {
152
+ return;
153
+ }
154
+ if (text === lastUserMessage) {
155
+ return;
156
+ }
157
+ const now = /* @__PURE__ */ new Date();
158
+ const { yyyy, MM, dd, HH, mm } = formatDate(now);
159
+ const topic = sanitizeFilename(text);
160
+ const promptDir = join(directory, ".agent", "prompts", yyyy, MM, dd);
161
+ await mkdir(promptDir, { recursive: true });
162
+ const existingFile = await findExistingFile(directory, sessionId);
163
+ const time = formatTime(now);
164
+ const dateStr = `${yyyy}${MM}${dd}`;
165
+ const timeTitle = `============ ${time} ============`;
166
+ const fileContent = existingFile ? `
99
167
 
100
168
  ${timeTitle}
101
169
 
102
170
  ${text}` : `${timeTitle}
103
171
 
104
172
  ${text}`;
105
- if (existingFile) {
106
- await appendFile(existingFile, fileContent);
107
- } else {
108
- const filename = `${HH}${mm}-${sessionId}-${topic}.md`;
109
- const filepath = join(promptDir, filename);
110
- await appendFile(filepath, fileContent);
111
- }
112
- lastUserMessage = text;
113
- }
173
+ if (existingFile) {
174
+ await appendFile(existingFile, fileContent);
175
+ } else {
176
+ const filename = `${dateStr}-${HH}${mm}-${sessionId}-${topic}.md`;
177
+ const filepath = join(promptDir, filename);
178
+ await appendFile(filepath, fileContent);
114
179
  }
180
+ lastUserMessage = text;
181
+ lastProcessedMessageCount = messageCount;
115
182
  }
116
183
  };
117
184
  };
118
- var opencode_prompt_recorder_default = OpenCodePromptRecorder;
185
+ var index_default = OpenCodePromptRecorder;
119
186
  export {
120
- opencode_prompt_recorder_default as default,
121
- OpenCodePromptRecorder
187
+ OpenCodePromptRecorder,
188
+ index_default as default
122
189
  };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-prompt-recorder",
3
- "version": "1.3.3",
3
+ "version": "1.3.6",
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",
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "build": "bun build index.ts --outdir dist --target node && cp package.json dist/",
21
- "prepublishOnly": "bun run build",
21
+ "prepublishOnly": "npx esbuild index.ts --bundle --platform=node --outdir=dist --format=esm && cp package.json dist/",
22
22
  "test": "npx tsx test/index.ts"
23
23
  },
24
24
  "keywords": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-prompt-recorder",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
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",
@@ -18,7 +18,7 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "build": "bun build index.ts --outdir dist --target node && cp package.json dist/",
21
- "prepublishOnly": "bun run build",
21
+ "prepublishOnly": "npx esbuild index.ts --bundle --platform=node --outdir=dist --format=esm && cp package.json dist/",
22
22
  "test": "npx tsx test/index.ts"
23
23
  },
24
24
  "keywords": [