opencode-prompt-recorder 1.3.5 → 1.3.7

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
@@ -11,15 +11,17 @@ async function getVersion() {
11
11
  return "unknown";
12
12
  }
13
13
  }
14
- function getLastUpdateTime() {
15
- const now = /* @__PURE__ */ new Date();
16
- const yyyy = now.getFullYear();
17
- const MM = String(now.getMonth() + 1).padStart(2, "0");
18
- const dd = String(now.getDate()).padStart(2, "0");
19
- const HH = String(now.getHours()).padStart(2, "0");
20
- const mm = String(now.getMinutes()).padStart(2, "0");
21
- const ss = String(now.getSeconds()).padStart(2, "0");
22
- return `${yyyy}-${MM}-${dd} ${HH}:${mm}:${ss}`;
14
+ async function debugLog(directory, msg) {
15
+ const time = (/* @__PURE__ */ new Date()).toISOString();
16
+ const logLine = `[${time}] ${msg}
17
+ `;
18
+ try {
19
+ const logDir = join(directory, ".agent", "prompts-log");
20
+ await mkdir(logDir, { recursive: true });
21
+ await appendFile(join(logDir, "log.txt"), logLine);
22
+ } catch (e) {
23
+ console.error("debugLog failed:", e);
24
+ }
23
25
  }
24
26
  function sanitizeFilename(str) {
25
27
  return str.replace(/[<>:"/\\|?*\x00-\x1f]/g, "").substring(0, 50).trim();
@@ -61,16 +63,74 @@ async function findExistingFile(directory, sessionId) {
61
63
  }
62
64
  return null;
63
65
  }
64
- var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
66
+ var OpenCodePromptRecorder = async ({ directory, client }) => {
65
67
  let lastUserMessage = "";
66
68
  let versionFileWritten = false;
69
+ let lastProcessedMessageCount = 0;
70
+ const messageRoleMap = /* @__PURE__ */ new Map();
67
71
  return {
68
- // 使用 chat.message 事件监听用户消息(来自 SDK 类型定义)
69
- "chat.message": async (input, output) => {
72
+ "event": async ({ event }) => {
73
+ if (event.type === "message.updated") {
74
+ const info = event.properties.info;
75
+ const role = info?.role || info?.message?.role;
76
+ if (info?.id && role) {
77
+ messageRoleMap.set(info.id, role);
78
+ }
79
+ }
80
+ if (event.type === "message.part.updated") {
81
+ const part = event.properties.part;
82
+ if (part?.type === "text" && part?.text) {
83
+ const sessionID = part.sessionID;
84
+ const messageID = part.messageID;
85
+ const text2 = part.text;
86
+ let role = messageRoleMap.get(messageID);
87
+ if (!role) {
88
+ role = part.message?.role;
89
+ }
90
+ if (!role) {
91
+ role = event.properties.info?.role;
92
+ }
93
+ if (!role) {
94
+ role = event.properties.info?.message?.role;
95
+ }
96
+ if (!role) {
97
+ await debugLog(directory, `[prompt-recorder] WARNING: role not found, messageID=${messageID}, sessionID=${sessionID}, textPreview=${text2.substring(0, 30)}`);
98
+ } else if (role === "user" && text2 && sessionID) {
99
+ await debugLog(directory, `[prompt-recorder] event=${event.type}, role=${role}, sessionID=${sessionID}, textLength=${text2.length}, textPreview=${text2.substring(0, 50)}`);
100
+ const now2 = /* @__PURE__ */ new Date();
101
+ const { yyyy: yyyy2, MM: MM2, dd: dd2, HH: HH2, mm: mm2 } = formatDate(now2);
102
+ const topic2 = sanitizeFilename(text2);
103
+ const promptDir2 = join(directory, ".agent", "prompts", yyyy2, MM2, dd2);
104
+ await mkdir(promptDir2, { recursive: true });
105
+ const existingFile2 = await findExistingFile(directory, sessionID);
106
+ const time2 = formatTime(now2);
107
+ const dateStr2 = `${yyyy2}${MM2}${dd2}`;
108
+ const timeTitle2 = `============ ${time2} ============`;
109
+ const fileContent2 = existingFile2 ? `
110
+
111
+ ${timeTitle2}
112
+
113
+ ${text2}` : `${timeTitle2}
114
+
115
+ ${text2}`;
116
+ if (existingFile2) {
117
+ await appendFile(existingFile2, fileContent2);
118
+ } else {
119
+ const filename = `${dateStr2}-${HH2}${mm2}-${sessionID}-${topic2}.md`;
120
+ const filepath = join(promptDir2, filename);
121
+ await appendFile(filepath, fileContent2);
122
+ }
123
+ }
124
+ }
125
+ }
126
+ if (event.type !== "session.updated") {
127
+ return;
128
+ }
129
+ const messages = event.properties.info.messages;
130
+ const messageCount = messages.length;
70
131
  if (!versionFileWritten) {
71
132
  try {
72
133
  const version = await getVersion();
73
- const lastUpdate = getLastUpdateTime();
74
134
  const readmeDir = join(directory, ".agent");
75
135
  const readmeFile = join(readmeDir, "opencode-prompt-recorder-readme.txt");
76
136
  const content = `# OpenCode Prompt Recorder
@@ -78,7 +138,6 @@ var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
78
138
  \u81EA\u52A8\u8BB0\u5F55\u7528\u6237\u63D0\u793A\u8BCD\u5230 .agent/prompts \u76EE\u5F55\u7684\u63D2\u4EF6\u3002
79
139
 
80
140
  \u7248\u672C\uFF1A${version}
81
- \u6700\u540E\u66F4\u65B0\u65F6\u95F4\uFF1A${lastUpdate}
82
141
  \u4F5C\u8005\uFF1Aanarckk
83
142
  \u9879\u76EE\u5730\u5740\uFF1Ahttps://github.com/anarckk/opencode-prompt-recorder`;
84
143
  try {
@@ -95,39 +154,46 @@ var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
95
154
  } catch (e) {
96
155
  }
97
156
  }
98
- if (output.message.role === "user") {
99
- const now = /* @__PURE__ */ new Date();
100
- const text = output.parts.map((p) => p.type === "text" ? p.text : "").join("");
101
- const sessionId = input.sessionID;
102
- if (!sessionId) {
103
- return;
104
- }
105
- if (text && text !== lastUserMessage) {
106
- const { yyyy, MM, dd, HH, mm } = formatDate(now);
107
- const topic = sanitizeFilename(text);
108
- const promptDir = join(directory, ".agent", "prompts", yyyy, MM, dd);
109
- await mkdir(promptDir, { recursive: true });
110
- const existingFile = await findExistingFile(directory, sessionId);
111
- const time = formatTime(now);
112
- const dateStr = `${yyyy}${MM}${dd}`;
113
- const timeTitle = `============ ${time} ============`;
114
- const fileContent = existingFile ? `
157
+ if (messageCount <= lastProcessedMessageCount) {
158
+ return;
159
+ }
160
+ const latestMessage = messages[messages.length - 1];
161
+ if (latestMessage?.role !== "user") {
162
+ return;
163
+ }
164
+ const text = latestMessage.parts.map((p) => p.type === "text" ? p.text : "").join("");
165
+ const sessionId = event.properties.info.id;
166
+ if (!text || !sessionId) {
167
+ return;
168
+ }
169
+ if (text === lastUserMessage) {
170
+ return;
171
+ }
172
+ const now = /* @__PURE__ */ new Date();
173
+ const { yyyy, MM, dd, HH, mm } = formatDate(now);
174
+ const topic = sanitizeFilename(text);
175
+ const promptDir = join(directory, ".agent", "prompts", yyyy, MM, dd);
176
+ await mkdir(promptDir, { recursive: true });
177
+ const existingFile = await findExistingFile(directory, sessionId);
178
+ const time = formatTime(now);
179
+ const dateStr = `${yyyy}${MM}${dd}`;
180
+ const timeTitle = `============ ${time} ============`;
181
+ const fileContent = existingFile ? `
115
182
 
116
183
  ${timeTitle}
117
184
 
118
185
  ${text}` : `${timeTitle}
119
186
 
120
187
  ${text}`;
121
- if (existingFile) {
122
- await appendFile(existingFile, fileContent);
123
- } else {
124
- const filename = `${dateStr}-${HH}${mm}-${sessionId}-${topic}.md`;
125
- const filepath = join(promptDir, filename);
126
- await appendFile(filepath, fileContent);
127
- }
128
- lastUserMessage = text;
129
- }
188
+ if (existingFile) {
189
+ await appendFile(existingFile, fileContent);
190
+ } else {
191
+ const filename = `${dateStr}-${HH}${mm}-${sessionId}-${topic}.md`;
192
+ const filepath = join(promptDir, filename);
193
+ await appendFile(filepath, fileContent);
130
194
  }
195
+ lastUserMessage = text;
196
+ lastProcessedMessageCount = messageCount;
131
197
  }
132
198
  };
133
199
  };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-prompt-recorder",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
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",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-prompt-recorder",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
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",