@agentbean/daemon 0.1.7 → 0.1.8
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/post-process.js +51 -7
- package/package.json +1 -1
package/dist/post-process.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { readdirSync, statSync, existsSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
2
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
4
|
import { logger } from './log.js';
|
|
5
5
|
const CODE_BLOCK_RE = /```python\n([\s\S]*?)```/g;
|
|
6
6
|
const CODEX_IMG_DIR = join(homedir(), '.codex', 'generated_images');
|
|
7
|
+
const OUTPUT_FILE_EXT_RE = /\.(png|jpe?g|gif|webp|svg|pdf|txt|csv|json|md|mp4|mov|zip)$/i;
|
|
7
8
|
export function listAllFiles(dir, maxDepth = 10, depth = 0) {
|
|
8
9
|
if (!existsSync(dir) || depth > maxDepth)
|
|
9
10
|
return [];
|
|
@@ -19,8 +20,47 @@ export function listAllFiles(dir, maxDepth = 10, depth = 0) {
|
|
|
19
20
|
}
|
|
20
21
|
return results;
|
|
21
22
|
}
|
|
23
|
+
function normalizeCandidatePath(raw) {
|
|
24
|
+
const cleaned = raw
|
|
25
|
+
.trim()
|
|
26
|
+
.replace(/^file:\/\//, '')
|
|
27
|
+
.replace(/^["'`<({\[]+/, '')
|
|
28
|
+
.replace(/["'`>)}\].,;:]+$/, '');
|
|
29
|
+
if (!cleaned || !OUTPUT_FILE_EXT_RE.test(cleaned))
|
|
30
|
+
return null;
|
|
31
|
+
return cleaned.replace(/^~(?=$|\/)/, homedir());
|
|
32
|
+
}
|
|
33
|
+
function extractMentionedFiles(reply, workspace, dispatchStart) {
|
|
34
|
+
const candidates = new Set();
|
|
35
|
+
const markdownLinkRe = /!?\[[^\]]*]\(([^)\s]+)\)/g;
|
|
36
|
+
const plainPathRe = /(?:^|[\s"'`(<])((?:~?\/|\.{1,2}\/)?[\w@%+=:,./-]+\.(?:png|jpe?g|gif|webp|svg|pdf|txt|csv|json|md|mp4|mov|zip))(?:$|[\s"'`)>.,;:])/gim;
|
|
37
|
+
let match;
|
|
38
|
+
while ((match = markdownLinkRe.exec(reply)) !== null) {
|
|
39
|
+
const normalized = normalizeCandidatePath(match[1]);
|
|
40
|
+
if (normalized)
|
|
41
|
+
candidates.add(normalized);
|
|
42
|
+
}
|
|
43
|
+
while ((match = plainPathRe.exec(reply)) !== null) {
|
|
44
|
+
const normalized = normalizeCandidatePath(match[1]);
|
|
45
|
+
if (normalized)
|
|
46
|
+
candidates.add(normalized);
|
|
47
|
+
}
|
|
48
|
+
const files = [];
|
|
49
|
+
for (const candidate of candidates) {
|
|
50
|
+
const abs = isAbsolute(candidate) ? candidate : workspace ? resolve(workspace, candidate) : null;
|
|
51
|
+
if (!abs)
|
|
52
|
+
continue;
|
|
53
|
+
try {
|
|
54
|
+
const st = statSync(abs);
|
|
55
|
+
if (st.isFile() && st.mtimeMs > dispatchStart)
|
|
56
|
+
files.push(abs);
|
|
57
|
+
}
|
|
58
|
+
catch { }
|
|
59
|
+
}
|
|
60
|
+
return files;
|
|
61
|
+
}
|
|
22
62
|
export async function postProcess(reply, workspace, kind, dispatchStart) {
|
|
23
|
-
const outputFiles =
|
|
63
|
+
const outputFiles = new Set();
|
|
24
64
|
// Codex native image detection
|
|
25
65
|
if (kind === 'codex') {
|
|
26
66
|
const allCodexFiles = listAllFiles(CODEX_IMG_DIR);
|
|
@@ -28,12 +68,15 @@ export async function postProcess(reply, workspace, kind, dispatchStart) {
|
|
|
28
68
|
try {
|
|
29
69
|
const st = statSync(f);
|
|
30
70
|
if (st.mtimeMs > dispatchStart) {
|
|
31
|
-
outputFiles.
|
|
71
|
+
outputFiles.add(f);
|
|
32
72
|
}
|
|
33
73
|
}
|
|
34
74
|
catch { }
|
|
35
75
|
}
|
|
36
76
|
}
|
|
77
|
+
for (const filePath of extractMentionedFiles(reply, workspace, dispatchStart)) {
|
|
78
|
+
outputFiles.add(filePath);
|
|
79
|
+
}
|
|
37
80
|
// Detect files created during this dispatch
|
|
38
81
|
if (workspace) {
|
|
39
82
|
for (const entry of readdirSync(workspace, { withFileTypes: true })) {
|
|
@@ -45,7 +88,7 @@ export async function postProcess(reply, workspace, kind, dispatchStart) {
|
|
|
45
88
|
try {
|
|
46
89
|
const st = statSync(f);
|
|
47
90
|
if (st.mtimeMs > dispatchStart) {
|
|
48
|
-
outputFiles.
|
|
91
|
+
outputFiles.add(f);
|
|
49
92
|
}
|
|
50
93
|
}
|
|
51
94
|
catch { }
|
|
@@ -64,8 +107,9 @@ export async function postProcess(reply, workspace, kind, dispatchStart) {
|
|
|
64
107
|
}
|
|
65
108
|
}
|
|
66
109
|
let replyText = reply;
|
|
67
|
-
|
|
68
|
-
|
|
110
|
+
const files = [...outputFiles];
|
|
111
|
+
if (files.length > 0) {
|
|
112
|
+
replyText += '\n\n已生成文件:\n' + files.map((f) => `- ${f}`).join('\n');
|
|
69
113
|
}
|
|
70
|
-
return { replyText, outputFiles };
|
|
114
|
+
return { replyText, outputFiles: files };
|
|
71
115
|
}
|