pi-open-agents 0.1.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.
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Recursion environment variable management.
3
+ *
4
+ * Controls subagent spawning depth, allowed agents, and system prompt mode
5
+ * via environment variables passed from parent to child process.
6
+ *
7
+ * Renamed from PI_SUBAGENT_* (pi-subagents) to PI_OPEN_AGENTS_*.
8
+ */
9
+
10
+ import type { SystemPromptMode } from "../types.ts";
11
+
12
+ // ─── Env Vars ────────────────────────────────────────────────────────────────
13
+
14
+ export type RecursionEnv = Partial<
15
+ Record<
16
+ | "PI_OPEN_AGENTS_DEPTH"
17
+ | "PI_OPEN_AGENTS_MAX_DEPTH"
18
+ | "PI_OPEN_AGENTS_NAME"
19
+ | "PI_OPEN_AGENTS_SYSTEM_PROMPT_MODE"
20
+ | "PI_OPEN_AGENTS_ALLOWED"
21
+ | "PI_OPEN_AGENTS_SESSION"
22
+ | "PI_OPEN_AGENTS_PERMISSION"
23
+ | "PI_OPEN_AGENTS_DEBUG",
24
+ string
25
+ >
26
+ >;
27
+
28
+ // ─── Parsers ─────────────────────────────────────────────────────────────────
29
+
30
+ function parseEnvNumber(value: string | undefined): number | undefined {
31
+ if (value === undefined) return undefined;
32
+ const parsed = Number(value);
33
+ return Number.isFinite(parsed) ? parsed : undefined;
34
+ }
35
+
36
+ // ─── Queries ─────────────────────────────────────────────────────────────────
37
+
38
+ /**
39
+ * Get the set of allowed agent names from env, or undefined if no restriction.
40
+ */
41
+ export function allowedAgentNames(env: RecursionEnv): Set<string> | undefined {
42
+ const raw = env?.PI_OPEN_AGENTS_ALLOWED;
43
+ if (!raw) return undefined;
44
+ return new Set(
45
+ raw
46
+ .split(",")
47
+ .map((name) => name.trim())
48
+ .filter(Boolean),
49
+ );
50
+ }
51
+
52
+ /**
53
+ * Check if the current process is past the max recursion depth.
54
+ */
55
+ export function isPastMaxDepth(env: RecursionEnv): boolean {
56
+ const depth = parseEnvNumber(env?.PI_OPEN_AGENTS_DEPTH);
57
+ const maxDepth = parseEnvNumber(env?.PI_OPEN_AGENTS_MAX_DEPTH);
58
+ return depth !== undefined && maxDepth !== undefined && depth > maxDepth;
59
+ }
60
+
61
+ /**
62
+ * Check if the current process is a subagent (depth > 0).
63
+ */
64
+ export function isSubagentProcess(env: RecursionEnv): boolean {
65
+ const depth = parseEnvNumber(env?.PI_OPEN_AGENTS_DEPTH);
66
+ return depth !== undefined && depth > 0;
67
+ }
68
+
69
+ /**
70
+ * Get the system prompt mode from env.
71
+ */
72
+ export function getSystemPromptMode(env: RecursionEnv): SystemPromptMode | undefined {
73
+ const mode = env?.PI_OPEN_AGENTS_SYSTEM_PROMPT_MODE;
74
+ if (mode === "replace" || mode === "replace-all" || mode === "append") return mode;
75
+ return undefined;
76
+ }
77
+
78
+ /**
79
+ * Check if we're a subagent running with replace/replace-all system prompt mode.
80
+ */
81
+ export function isSubagentReplaceSystemPrompt(env: RecursionEnv): boolean {
82
+ const mode = getSystemPromptMode(env);
83
+ return isSubagentProcess(env) && (mode === "replace" || mode === "replace-all");
84
+ }
85
+
86
+ // ─── Builders ────────────────────────────────────────────────────────────────
87
+
88
+ export interface SubagentEnvVars {
89
+ depth: number;
90
+ maxDepth: number;
91
+ agentName: string;
92
+ systemPromptMode: SystemPromptMode;
93
+ session: "none" | "fork";
94
+ allowedAgents?: string[];
95
+ debug: boolean;
96
+ }
97
+
98
+ /**
99
+ * Build the environment variables to pass to a child subagent process.
100
+ */
101
+ export function buildSubagentEnv(vars: SubagentEnvVars): Record<string, string> {
102
+ const env: Record<string, string> = {
103
+ PI_OPEN_AGENTS_DEPTH: String(vars.depth),
104
+ PI_OPEN_AGENTS_MAX_DEPTH: String(vars.maxDepth),
105
+ PI_OPEN_AGENTS_NAME: vars.agentName,
106
+ PI_OPEN_AGENTS_SYSTEM_PROMPT_MODE: vars.systemPromptMode,
107
+ PI_OPEN_AGENTS_SESSION: vars.session,
108
+ PI_OPEN_AGENTS_DEBUG: vars.debug ? "true" : "false",
109
+ };
110
+
111
+ if (vars.allowedAgents && vars.allowedAgents.length > 0) {
112
+ env.PI_OPEN_AGENTS_ALLOWED = vars.allowedAgents.join(",");
113
+ }
114
+
115
+ return env;
116
+ }