opencode-prompt-recorder 1.0.0
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 +116 -0
- package/dist/index.js +72 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# opencode-prompt-recorder
|
|
2
|
+
|
|
3
|
+
一个用于记录用户提示词的 OpenCode 插件。自动将用户消息保存到本地文件系统,并按目录结构组织。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- ✨ 自动捕获聊天对话中的用户提示词
|
|
8
|
+
- 📁 有组织的文件存储:`.agent/prompts/yyyy/MM/dd/` 目录结构
|
|
9
|
+
- 🏷️ 基于会话的组织:每个对话会话单独跟踪
|
|
10
|
+
- 📝 丰富的元数据:包含时间戳、会话 ID 和提示词主题
|
|
11
|
+
- 🔄 会话持久化:追加到现有文件以继续对话
|
|
12
|
+
- 🛡️ 安全的文件名处理:清理文件名以防止文件系统错误
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install -g opencode-prompt-recorder
|
|
18
|
+
# 或
|
|
19
|
+
bun add -g opencode-prompt-recorder
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 使用方法
|
|
23
|
+
|
|
24
|
+
将插件添加到你的 `opencode.json`:
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"$schema": "https://opencode.ai/config.json",
|
|
29
|
+
"plugin": ["opencode-prompt-recorder"],
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 工作原理
|
|
34
|
+
|
|
35
|
+
1. 插件监听 OpenCode 聊天事件(`chat.message`)
|
|
36
|
+
2. 每次用户消息时,提取提示词文本和会话 ID
|
|
37
|
+
3. 提示词保存到 `.agent/prompts/` 目录,按日期组织
|
|
38
|
+
4. 每个提示词保存为带有时间戳和会话信息的 markdown 文件
|
|
39
|
+
|
|
40
|
+
## 文件结构
|
|
41
|
+
|
|
42
|
+
插件创建有组织的目录结构:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
.agent/
|
|
46
|
+
└── prompts/
|
|
47
|
+
└── 2026/
|
|
48
|
+
└── 02/
|
|
49
|
+
└── 22/
|
|
50
|
+
├── 1005-abc123-什么是AI.md
|
|
51
|
+
├── 1050-def456-如何编程.md
|
|
52
|
+
└── 1120-ghi789-复制README.md
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 文件格式
|
|
56
|
+
|
|
57
|
+
每个提示词文件遵循以下格式:
|
|
58
|
+
|
|
59
|
+
```markdown
|
|
60
|
+
============ 10:05 ============
|
|
61
|
+
|
|
62
|
+
什么是 AI?
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
============ 10:50 ============
|
|
67
|
+
|
|
68
|
+
如何编写 hello world 程序?
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 配置
|
|
72
|
+
|
|
73
|
+
无需额外配置。插件将自动:
|
|
74
|
+
|
|
75
|
+
- 保存到项目根目录的 `.agent/prompts/` 目录
|
|
76
|
+
- 使用当前工作目录作为基础路径
|
|
77
|
+
- 根据需要创建目录
|
|
78
|
+
|
|
79
|
+
## 会话管理
|
|
80
|
+
|
|
81
|
+
- 每个唯一的 `sessionId` 获得自己的文件
|
|
82
|
+
- 相同的会话 ID 追加到现有文件
|
|
83
|
+
- 不同的会话 ID 创建新文件
|
|
84
|
+
- 会话 ID 来自 `chat.message` 事件输入
|
|
85
|
+
|
|
86
|
+
## 使用场景
|
|
87
|
+
|
|
88
|
+
- ✅ LLM 交互的审计跟踪
|
|
89
|
+
- ✅ 训练数据收集和标注
|
|
90
|
+
- ✅ 会话历史和对话回放
|
|
91
|
+
- ✅ 质量保证和监控
|
|
92
|
+
- ✅ 研发洞察
|
|
93
|
+
|
|
94
|
+
## 依赖要求
|
|
95
|
+
|
|
96
|
+
- OpenCode v0.15.0 或更高版本
|
|
97
|
+
- Node.js 或 Bun 运行时
|
|
98
|
+
- 项目目录的文件系统写入权限
|
|
99
|
+
|
|
100
|
+
## 故障排除
|
|
101
|
+
|
|
102
|
+
### 提示词未被记录
|
|
103
|
+
|
|
104
|
+
- 确保插件已列入 `opencode.json` 配置
|
|
105
|
+
- 检查项目目录的写入权限
|
|
106
|
+
- 验证 `.agent/prompts/` 目录正在创建
|
|
107
|
+
|
|
108
|
+
### 文件名包含特殊字符
|
|
109
|
+
|
|
110
|
+
- 插件会自动清理文件名,移除非法字符
|
|
111
|
+
- 文件名截断至最多 50 个字符
|
|
112
|
+
- 如果提示词过长,文件名只使用前 50 个字符
|
|
113
|
+
|
|
114
|
+
## 许可证
|
|
115
|
+
|
|
116
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { mkdir, appendFile, readdir } from "fs/promises";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
function sanitizeFilename(str) {
|
|
5
|
+
return str.replace(/[<>:"/\\|?*\x00-\x1f]/g, "").substring(0, 50).trim();
|
|
6
|
+
}
|
|
7
|
+
function formatDate(date) {
|
|
8
|
+
const yyyy = date.getFullYear().toString();
|
|
9
|
+
const MM = String(date.getMonth() + 1).padStart(2, "0");
|
|
10
|
+
const dd = String(date.getDate()).padStart(2, "0");
|
|
11
|
+
const HH = String(date.getHours()).padStart(2, "0");
|
|
12
|
+
const mm = String(date.getMinutes()).padStart(2, "0");
|
|
13
|
+
return { yyyy, MM, dd, HH, mm };
|
|
14
|
+
}
|
|
15
|
+
function formatTime(date) {
|
|
16
|
+
return `${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}`;
|
|
17
|
+
}
|
|
18
|
+
async function findExistingFile(promptDir, sessionId) {
|
|
19
|
+
if (!sessionId)
|
|
20
|
+
return null;
|
|
21
|
+
const files = await readdir(promptDir);
|
|
22
|
+
for (const file of files) {
|
|
23
|
+
if (file.includes(`-${sessionId}-`)) {
|
|
24
|
+
return join(promptDir, file);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
var OpenCodePromptRecorder = async ({ directory, client }) => {
|
|
30
|
+
let lastUserMessage = "";
|
|
31
|
+
return {
|
|
32
|
+
"chat.message": async (input, output) => {
|
|
33
|
+
if (output.message.role === "user") {
|
|
34
|
+
const text = output.parts.map((p) => p.type === "text" ? p.text : "").join("");
|
|
35
|
+
const sessionId = input.sessionID;
|
|
36
|
+
if (!sessionId) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (text && text !== lastUserMessage) {
|
|
40
|
+
const { yyyy, MM, dd, HH, mm } = formatDate(new Date);
|
|
41
|
+
const topic = sanitizeFilename(text);
|
|
42
|
+
const promptDir = join(directory, ".agent", "prompts", yyyy, MM, dd);
|
|
43
|
+
await mkdir(promptDir, { recursive: true });
|
|
44
|
+
const existingFile = await findExistingFile(promptDir, sessionId);
|
|
45
|
+
const time = formatTime(new Date);
|
|
46
|
+
const timeTitle = `============ ${time} ============`;
|
|
47
|
+
const content = existingFile ? `
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
${timeTitle}
|
|
51
|
+
|
|
52
|
+
${text}` : `${timeTitle}
|
|
53
|
+
|
|
54
|
+
${text}`;
|
|
55
|
+
if (existingFile) {
|
|
56
|
+
await appendFile(existingFile, content);
|
|
57
|
+
} else {
|
|
58
|
+
const filename = `${HH}${mm}-${sessionId}-${topic}.md`;
|
|
59
|
+
const filepath = join(promptDir, filename);
|
|
60
|
+
await appendFile(filepath, content);
|
|
61
|
+
}
|
|
62
|
+
lastUserMessage = text;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
var opencode_prompt_recorder_default = OpenCodePromptRecorder;
|
|
69
|
+
export {
|
|
70
|
+
opencode_prompt_recorder_default as default,
|
|
71
|
+
OpenCodePromptRecorder
|
|
72
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-prompt-recorder",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "OpenCode plugin for recording user prompts. Automatically saves user messages to a local file system with organized directory structure.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "bun build index.ts --outdir dist --target node",
|
|
21
|
+
"prepublishOnly": "bun run build",
|
|
22
|
+
"test": "bun test"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"opencode",
|
|
26
|
+
"prompt",
|
|
27
|
+
"recording",
|
|
28
|
+
"logging",
|
|
29
|
+
"plugin",
|
|
30
|
+
"llm",
|
|
31
|
+
"ai",
|
|
32
|
+
"audit"
|
|
33
|
+
],
|
|
34
|
+
"author": "anarckk",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/anarckk/opencode-prompt-recorder.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/anarckk/opencode-prompt-recorder/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/anarckk/opencode-prompt-recorder#readme",
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@opencode-ai/plugin": ">=0.15.0",
|
|
46
|
+
"typescript": ">=5.0.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@opencode-ai/plugin": "^0.15.0",
|
|
50
|
+
"@types/bun": "^1.3.1",
|
|
51
|
+
"typescript": "^5.9.3"
|
|
52
|
+
}
|
|
53
|
+
}
|