pi-subagentura 1.0.6 → 1.0.7

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.
Files changed (2) hide show
  1. package/helpers.ts +46 -9
  2. 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,49 @@ 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
+ : resolve(process.cwd(), ".pi/subagent-logs");
31
+
32
+ export function debugLog(level: string, event: string, data: Record<string, unknown> = {}) {
33
+ try {
34
+ if (!existsSync(DEBUG_LOG_DIR)) {
35
+ mkdirSync(DEBUG_LOG_DIR, { recursive: true });
36
+ }
37
+ const entry = JSON.stringify({
38
+ timestamp: new Date().toISOString(),
39
+ level,
40
+ event,
41
+ ...data,
42
+ }) + "\n";
43
+ const fileName = resolve(DEBUG_LOG_DIR, `debug-${new Date().toISOString().slice(0, 10)}.jsonl`);
44
+ appendFileSync(fileName, entry);
45
+ } catch {
46
+ // Silently fail to avoid polluting output
47
+ }
48
+ }
49
+
50
+ export function extractTextFromContent(content: unknown): string {
51
+ if (Array.isArray(content)) {
52
+ return content
53
+ .filter((c): c is { type: "text"; text: string } =>
54
+ typeof c === "object" && c !== null && c.type === "text" && typeof c.text === "string"
55
+ )
56
+ .map((c) => c.text)
57
+ .join("\n");
58
+ }
59
+ if (typeof content === "string") {
60
+ return content;
61
+ }
62
+ debugLog("warn", "unexpected_content_type", {
63
+ contentType: typeof content,
64
+ content: String(String(content).slice(0, 200)),
65
+ });
66
+ return "";
67
+ }
68
+
24
69
  // ── Constants ────────────────────────────────────────────────────────
25
70
 
26
71
  /**
@@ -468,15 +513,7 @@ export async function startSubagentJob(
468
513
  for (let i = messages.length - 1; i >= 0; i--) {
469
514
  const msg = messages[i];
470
515
  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");
516
+ const textParts = extractTextFromContent(msg.content);
480
517
  if (textParts) {
481
518
  finalOutput = textParts;
482
519
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-subagentura",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Public Pi package that adds in-process sub-agents via the SDK",
5
5
  "main": "subagent.ts",
6
6
  "type": "module",