-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
68 lines (60 loc) · 2.1 KB
/
Copy pathutils.ts
File metadata and controls
68 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Small helpers shared across the plugin runtime.
import { appendFileSync } from "node:fs";
import { randomBytes } from "node:crypto";
import type { ContentPart } from "./types";
export const DEBUG_LOG_PATH = "/tmp/guardian-debug.log";
const PROCESS_PID = process.pid;
// Stable per-process prefix so concurrent opencode sessions are easy to
// distinguish when they all append to the same shared log file.
const INSTANCE_ID = randomBytes(3).toString("hex");
export function debugLog(tag: string, ...args: unknown[]): void {
try {
const line =
`${new Date().toISOString()} [GUARDIAN pid=${PROCESS_PID} inst=${INSTANCE_ID}] [${tag}] ` +
formatArgs(args) +
"\n";
appendFileSync(DEBUG_LOG_PATH, line);
} catch {
// never throw from logging
}
}
// Walk args: a string ending in `=` is a label, paired with the
// following value (rendered as `key=<value>`). Any other string is
// an orphan and prints as-is. Bare values get auto-expanded when
// they are objects.
function formatArgs(args: unknown[]): string {
const out: string[] = [];
for (let i = 0; i < args.length; i++) {
const a = args[i];
if (typeof a === "string" && a.endsWith("=") && i + 1 < args.length) {
out.push(`${a}${formatScalar(args[i + 1])}`);
i++;
} else {
out.push(formatArg(a));
}
}
return out.join(" ");
}
function formatArg(arg: unknown): string {
if (arg === null || arg === undefined) return String(arg);
if (typeof arg === "string") return arg;
if (typeof arg === "object") {
return Reflect.ownKeys(arg as object)
.filter((k) => typeof k === "string")
.map((k) => [k, Reflect.get(arg as object, k)] as const)
.map(([k, v]) => `${k}=${formatScalar(v)}`)
.join(" ");
}
return formatScalar(arg);
}
function formatScalar(value: unknown): string {
if (typeof value === "string") return value;
return JSON.stringify(value);
}
export function textFromParts(parts: ContentPart[] = []): string {
return parts
.filter((p) => p.type === "text" && typeof p.text === "string")
.map((p) => (p.text ?? "").trim())
.filter(Boolean)
.join("\n");
}