pi-subagentura 1.0.6 → 1.0.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/helpers.ts +47 -9
- package/package.json +1 -1
package/helpers.ts
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { randomBytes } from "node:crypto";
|
|
9
|
+
import { appendFileSync, mkdirSync, existsSync } from "node:fs";
|
|
10
|
+
import { resolve } from "node:path";
|
|
9
11
|
import { getModel, getProviders } from "@mariozechner/pi-ai";
|
|
10
12
|
import type { Model } from "@mariozechner/pi-ai";
|
|
11
13
|
|
|
@@ -21,6 +23,50 @@ import {
|
|
|
21
23
|
type AgentSession,
|
|
22
24
|
} from "@mariozechner/pi-coding-agent";
|
|
23
25
|
|
|
26
|
+
// ── Debug Logging ─────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
const DEBUG_LOG_DIR = process.env.SUBAGENT_DEBUG_LOG_DIR
|
|
29
|
+
? resolve(process.env.SUBAGENT_DEBUG_LOG_DIR)
|
|
30
|
+
: undefined;
|
|
31
|
+
|
|
32
|
+
export function debugLog(level: string, event: string, data: Record<string, unknown> = {}) {
|
|
33
|
+
if (!DEBUG_LOG_DIR) return;
|
|
34
|
+
try {
|
|
35
|
+
if (!existsSync(DEBUG_LOG_DIR)) {
|
|
36
|
+
mkdirSync(DEBUG_LOG_DIR, { recursive: true });
|
|
37
|
+
}
|
|
38
|
+
const entry = JSON.stringify({
|
|
39
|
+
timestamp: new Date().toISOString(),
|
|
40
|
+
level,
|
|
41
|
+
event,
|
|
42
|
+
...data,
|
|
43
|
+
}) + "\n";
|
|
44
|
+
const fileName = resolve(DEBUG_LOG_DIR, `debug-${new Date().toISOString().slice(0, 10)}.jsonl`);
|
|
45
|
+
appendFileSync(fileName, entry);
|
|
46
|
+
} catch {
|
|
47
|
+
// Silently fail to avoid polluting output
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function extractTextFromContent(content: unknown): string {
|
|
52
|
+
if (Array.isArray(content)) {
|
|
53
|
+
return content
|
|
54
|
+
.filter((c): c is { type: "text"; text: string } =>
|
|
55
|
+
typeof c === "object" && c !== null && c.type === "text" && typeof c.text === "string"
|
|
56
|
+
)
|
|
57
|
+
.map((c) => c.text)
|
|
58
|
+
.join("\n");
|
|
59
|
+
}
|
|
60
|
+
if (typeof content === "string") {
|
|
61
|
+
return content;
|
|
62
|
+
}
|
|
63
|
+
debugLog("warn", "unexpected_content_type", {
|
|
64
|
+
contentType: typeof content,
|
|
65
|
+
content: String(String(content).slice(0, 200)),
|
|
66
|
+
});
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
|
|
24
70
|
// ── Constants ────────────────────────────────────────────────────────
|
|
25
71
|
|
|
26
72
|
/**
|
|
@@ -468,15 +514,7 @@ export async function startSubagentJob(
|
|
|
468
514
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
469
515
|
const msg = messages[i];
|
|
470
516
|
if (msg.role === "assistant") {
|
|
471
|
-
const textParts = msg.content
|
|
472
|
-
?.filter(
|
|
473
|
-
(c: {
|
|
474
|
-
type: string;
|
|
475
|
-
text?: string;
|
|
476
|
-
}): c is { type: "text"; text: string } => c.type === "text",
|
|
477
|
-
)
|
|
478
|
-
.map((c) => c.text)
|
|
479
|
-
.join("\n");
|
|
517
|
+
const textParts = extractTextFromContent(msg.content);
|
|
480
518
|
if (textParts) {
|
|
481
519
|
finalOutput = textParts;
|
|
482
520
|
break;
|