opencode-prompt-recorder 1.3.5 → 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
@@ -11,16 +11,6 @@ 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}`;
23
- }
24
14
  function sanitizeFilename(str) {
25
15
  return str.replace(/[<>:"/\\|?*\x00-\x1f]/g, "").substring(0, 50).trim();
26
16
  }
@@ -61,16 +51,71 @@ async function findExistingFile(directory, sessionId) {
61
51
  }
62
52
  return null;
63
53
  }
64
- var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
54
+ var OpenCodePromptRecorder = async ({ directory, client }) => {
65
55
  let lastUserMessage = "";
66
56
  let versionFileWritten = false;
57
+ let lastProcessedMessageCount = 0;
58
+ const messageRoleMap = /* @__PURE__ */ new Map();
67
59
  return {
68
- // 使用 chat.message 事件监听用户消息(来自 SDK 类型定义)
69
- "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;
70
116
  if (!versionFileWritten) {
71
117
  try {
72
118
  const version = await getVersion();
73
- const lastUpdate = getLastUpdateTime();
74
119
  const readmeDir = join(directory, ".agent");
75
120
  const readmeFile = join(readmeDir, "opencode-prompt-recorder-readme.txt");
76
121
  const content = `# OpenCode Prompt Recorder
@@ -78,7 +123,6 @@ var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
78
123
  \u81EA\u52A8\u8BB0\u5F55\u7528\u6237\u63D0\u793A\u8BCD\u5230 .agent/prompts \u76EE\u5F55\u7684\u63D2\u4EF6\u3002
79
124
 
80
125
  \u7248\u672C\uFF1A${version}
81
- \u6700\u540E\u66F4\u65B0\u65F6\u95F4\uFF1A${lastUpdate}
82
126
  \u4F5C\u8005\uFF1Aanarckk
83
127
  \u9879\u76EE\u5730\u5740\uFF1Ahttps://github.com/anarckk/opencode-prompt-recorder`;
84
128
  try {
@@ -95,39 +139,46 @@ var OpenCodePromptRecorder = async ({ directory, client: _client }) => {
95
139
  } catch (e) {
96
140
  }
97
141
  }
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 ? `
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 ? `
115
167
 
116
168
  ${timeTitle}
117
169
 
118
170
  ${text}` : `${timeTitle}
119
171
 
120
172
  ${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
- }
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);
130
179
  }
180
+ lastUserMessage = text;
181
+ lastProcessedMessageCount = messageCount;
131
182
  }
132
183
  };
133
184
  };
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.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",
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.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",