@qqq123456789/codex-doctor 0.7.0 → 0.9.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/docs/13-codex-doctor.en.md +2 -0
- package/docs/13-codex-doctor.md +2 -0
- package/package.json +6 -1
- package/tool/cli.mjs +41 -2
- package/tool/sessions.mjs +55 -1
|
@@ -32,6 +32,8 @@ node codex-troubleshooting/tool/cli.mjs --help
|
|
|
32
32
|
| `archive delete <name\|--all>` | Delete archives (interactive confirm by default, `--yes` to skip) | medium (deletes, requires confirm) |
|
|
33
33
|
| `versions [-n 10]` | List the latest openai/codex releases (prerelease flagged) | read-only |
|
|
34
34
|
| `sessions [-n 10] [--dir keyword]` | Browse past sessions: time, workdir, source, first-prompt preview; `--dir` filters by directory — find "that conversation" | read-only |
|
|
35
|
+
| `sessions --search keyword [--deep]` | Search sessions by keyword (first 256KB of each file by default, `--deep` scans fully) | read-only |
|
|
36
|
+
| `sessions --show [--search keyword] [--pick N] [--full]` | Print a session's full transcript (latest by default; messages truncated to 400 chars unless `--full`) | read-only |
|
|
35
37
|
| `update` | Check the latest npm version and how to update | read-only |
|
|
36
38
|
|
|
37
39
|
## Design principles
|
package/docs/13-codex-doctor.md
CHANGED
|
@@ -32,6 +32,8 @@ node codex-troubleshooting/tool/cli.mjs --help
|
|
|
32
32
|
| `archive delete <名称\|--all>` | 删除归档(默认需交互确认,`--yes` 跳过) | 中(删除,需确认) |
|
|
33
33
|
| `versions [-n 10]` | 查看 openai/codex 最近 N 个版本(含预发布标记) | 只读 |
|
|
34
34
|
| `sessions [-n 10] [--dir 关键字]` | 浏览历史会话:时间、工作目录、来源、首条提问预览;`--dir` 按目录过滤,找回「上次那个对话」 | 只读 |
|
|
35
|
+
| `sessions --search 关键词 [--deep]` | 按关键词搜索会话(默认搜每个文件开头 256KB,`--deep` 全文扫描) | 只读 |
|
|
36
|
+
| `sessions --show [--search 关键词] [--pick N] [--full]` | 查看会话完整对话(默认最近一次;默认单条截断 400 字) | 只读 |
|
|
35
37
|
| `update` | 查询 npm 上工具的最新版本与更新方式 | 只读 |
|
|
36
38
|
|
|
37
39
|
## 设计原则
|
package/package.json
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qqq123456789/codex-doctor",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"test": "bash scripts/test-codex-doctor.sh",
|
|
6
|
+
"check:links": "bash scripts/check-links.sh",
|
|
7
|
+
"check:consistency": "bash scripts/check-consistency.sh"
|
|
8
|
+
},
|
|
4
9
|
"description": "Codex CLI 维护与排障工具:环境自检、会话/日志归档、配置与凭据备份恢复、版本追踪(零依赖)",
|
|
5
10
|
"keywords": [
|
|
6
11
|
"codex",
|
package/tool/cli.mjs
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
import { collectChecks, summarize, renderHuman } from './checks.mjs';
|
|
4
4
|
import { cleanTarget } from './clean.mjs';
|
|
5
5
|
import { backupConfig, restoreBackup, resetAuth, listVersions, listArchives, deleteArchive, checkUpdate } from './ops.mjs';
|
|
6
|
-
import { listSessions, searchSessions } from './sessions.mjs';
|
|
6
|
+
import { listSessions, searchSessions, readTranscript, exportTranscriptMarkdown } from './sessions.mjs';
|
|
7
7
|
import { CODEX_DIR } from './util.mjs';
|
|
8
8
|
import path from 'node:path';
|
|
9
9
|
|
|
10
|
-
const VERSION = '0.
|
|
10
|
+
const VERSION = '0.9.0';
|
|
11
11
|
|
|
12
12
|
const HELP = `codex-doctor v${VERSION} — Codex CLI 维护与排障工具(零依赖)
|
|
13
13
|
|
|
@@ -29,6 +29,7 @@ const HELP = `codex-doctor v${VERSION} — Codex CLI 维护与排障工具(零
|
|
|
29
29
|
versions [-n N] 查看 openai/codex 最近 N 个版本(默认 10)
|
|
30
30
|
sessions [-n N] [--dir 关键字] 浏览历史会话:时间、目录、来源、首条提问预览
|
|
31
31
|
[--search 关键词] [--deep] 按关键词搜索会话(--deep 全文扫描)
|
|
32
|
+
--show [--search 关键词] [--pick N] [--full] 查看会话完整对话
|
|
32
33
|
update 查询 npm 最新版本与更新方式
|
|
33
34
|
help 显示本帮助
|
|
34
35
|
|
|
@@ -48,6 +49,9 @@ function parseFlags(args) {
|
|
|
48
49
|
else if (a === '--dir') flags.dir = args[++i];
|
|
49
50
|
else if (a === '--search') flags.search = args[++i];
|
|
50
51
|
else if (a === '--deep') flags.deep = true;
|
|
52
|
+
else if (a === '--show') flags.show = true;
|
|
53
|
+
else if (a === '--pick') flags.pick = Number(args[++i]);
|
|
54
|
+
else if (a === '--full') flags.full = true;
|
|
51
55
|
else if (a === '--out') flags.out = args[++i];
|
|
52
56
|
else rest.push(a);
|
|
53
57
|
}
|
|
@@ -134,6 +138,41 @@ async function main() {
|
|
|
134
138
|
break;
|
|
135
139
|
}
|
|
136
140
|
case 'sessions': {
|
|
141
|
+
if (flags.show) {
|
|
142
|
+
// 展示会话完整对话:默认最近一次;--search 按关键词定位;--pick N 选第 N 个命中
|
|
143
|
+
const items = flags.search
|
|
144
|
+
? searchSessions({ keyword: flags.search, limit: 20, deep: true })
|
|
145
|
+
: listSessions({ limit: 20 });
|
|
146
|
+
if (items.length === 0) {
|
|
147
|
+
console.log(flags.search ? `没有找到包含「${flags.search}」的会话` : '~/.codex/sessions 里没有会话记录');
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
const pick = Math.min(Math.max(Number(flags.pick) || 1, 1), items.length);
|
|
151
|
+
const item = items[pick - 1];
|
|
152
|
+
console.log(`== 会话 ${item.date} @ ${item.dirName} ==`);
|
|
153
|
+
console.log(`文件: ${item.file}\n`);
|
|
154
|
+
if (flags.out) {
|
|
155
|
+
// 导出为 Markdown 文件
|
|
156
|
+
const r = exportTranscriptMarkdown(item.file, flags.out, { maxLen: flags.full ? Infinity : 400 });
|
|
157
|
+
if (!r) {
|
|
158
|
+
console.log('(该会话没有可导出的对话消息)');
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
console.log(`已导出 ${r.count} 条消息 → ${r.outFile}`);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
const transcript = readTranscript(item.file, { maxLen: flags.full ? Infinity : 400 });
|
|
165
|
+
if (!transcript || transcript.length === 0) {
|
|
166
|
+
console.log('(该会话没有可展示的对话消息)');
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
for (const msg of transcript) {
|
|
170
|
+
const who = msg.role === 'user' ? '[用户]' : '[Codex]';
|
|
171
|
+
console.log(`${who} ${msg.text}\n`);
|
|
172
|
+
}
|
|
173
|
+
console.log(`(共 ${transcript.length} 条消息${flags.full ? '' : ',单条超 400 字已截断,--full 查看全文'})`);
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
137
176
|
if (flags.search) {
|
|
138
177
|
const items = searchSessions({
|
|
139
178
|
keyword: flags.search,
|
package/tool/sessions.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// sessions:浏览 ~/.codex/sessions 下的历史会话(只读)
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
-
import { CODEX_DIR, exists, walkFiles } from './util.mjs';
|
|
4
|
+
import { CODEX_DIR, exists, walkFiles, ensureDir } from './util.mjs';
|
|
5
5
|
|
|
6
6
|
// 只读每个文件头部 64KB:meta 在第 1 行,真实提问通常也在最前面
|
|
7
7
|
function readHead(file, bytes = 65536) {
|
|
@@ -61,6 +61,7 @@ export function listSessions({ limit = 10, cwdFilter = null } = {}) {
|
|
|
61
61
|
const { meta, preview } = extract(readHead(f));
|
|
62
62
|
if (!meta) continue;
|
|
63
63
|
items.push({
|
|
64
|
+
file: f,
|
|
64
65
|
date: meta.date || st.mtime.toISOString().slice(0, 16).replace('T', ' '),
|
|
65
66
|
dirName: meta.cwd ? path.basename(meta.cwd) : '?',
|
|
66
67
|
cwd: meta.cwd,
|
|
@@ -117,6 +118,7 @@ export function searchSessions({ keyword, limit = 10, deep = false } = {}) {
|
|
|
117
118
|
|
|
118
119
|
const { meta } = extract(content);
|
|
119
120
|
results.push({
|
|
121
|
+
file: f,
|
|
120
122
|
date: (meta?.date) || new Date(mtimeMs).toISOString().slice(0, 16).replace('T', ' '),
|
|
121
123
|
dirName: meta?.cwd ? path.basename(meta.cwd) : '?',
|
|
122
124
|
snippet: snippet || '(匹配在元数据中)',
|
|
@@ -126,3 +128,55 @@ export function searchSessions({ keyword, limit = 10, deep = false } = {}) {
|
|
|
126
128
|
}
|
|
127
129
|
return results;
|
|
128
130
|
}
|
|
131
|
+
|
|
132
|
+
// 读取单个会话的完整对话(user / assistant 消息,跳过环境包装与工具输出)
|
|
133
|
+
export function readTranscript(file, { maxLen = 400 } = {}) {
|
|
134
|
+
if (!exists(file)) return null;
|
|
135
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
136
|
+
const out = [];
|
|
137
|
+
for (const line of content.split('\n')) {
|
|
138
|
+
if (!line.trim()) continue;
|
|
139
|
+
let j;
|
|
140
|
+
try {
|
|
141
|
+
j = JSON.parse(line);
|
|
142
|
+
} catch {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (j.type !== 'response_item' || j.payload?.type !== 'message') continue;
|
|
146
|
+
const role = j.payload.role;
|
|
147
|
+
if (role !== 'user' && role !== 'assistant') continue;
|
|
148
|
+
let text = (j.payload.content || []).map((c) => c.text || '').filter(Boolean).join('\n');
|
|
149
|
+
if (!text) continue;
|
|
150
|
+
if (role === 'user' && (text.startsWith('<environment_context>') || text.startsWith('<user_instructions>'))) continue;
|
|
151
|
+
if (role === 'user' && text.startsWith('<permissions')) continue;
|
|
152
|
+
if (text.length > maxLen) text = text.slice(0, maxLen) + '…';
|
|
153
|
+
out.push({ role, text });
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 把会话导出为 Markdown 文件(找回的对话可存档分享)
|
|
159
|
+
export function exportTranscriptMarkdown(file, outFile, { maxLen = 400 } = {}) {
|
|
160
|
+
if (!exists(file)) return null;
|
|
161
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
162
|
+
const { meta } = extract(content);
|
|
163
|
+
const transcript = readTranscript(file, { maxLen });
|
|
164
|
+
if (!transcript || transcript.length === 0) return null;
|
|
165
|
+
|
|
166
|
+
const lines = ['# Codex 会话记录', ''];
|
|
167
|
+
lines.push(`- 时间:${meta?.date || '?'}`);
|
|
168
|
+
lines.push(`- 工作目录:${meta?.cwd || '?'}`);
|
|
169
|
+
lines.push(`- 来源:${meta?.originator || '?'}`);
|
|
170
|
+
lines.push(`- 导出自:${file}`);
|
|
171
|
+
lines.push('');
|
|
172
|
+
for (const msg of transcript) {
|
|
173
|
+
lines.push(`## ${msg.role === 'user' ? '用户' : 'Codex'}`);
|
|
174
|
+
lines.push('');
|
|
175
|
+
lines.push(msg.text);
|
|
176
|
+
lines.push('');
|
|
177
|
+
}
|
|
178
|
+
const dest = path.resolve(outFile);
|
|
179
|
+
ensureDir(path.dirname(dest));
|
|
180
|
+
fs.writeFileSync(dest, lines.join('\n'), 'utf8');
|
|
181
|
+
return { outFile: dest, count: transcript.length };
|
|
182
|
+
}
|