@pi-unipi/subagents 2.3.0 → 2.4.1
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/README.md +3 -1
- package/dist/agent-manager.d.ts +81 -0
- package/dist/agent-manager.d.ts.map +1 -0
- package/dist/agent-manager.js +292 -0
- package/dist/agent-manager.js.map +1 -0
- package/dist/agent-runner.d.ts +51 -0
- package/dist/agent-runner.d.ts.map +1 -0
- package/dist/agent-runner.js +262 -0
- package/dist/agent-runner.js.map +1 -0
- package/dist/config.d.ts +24 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +132 -0
- package/dist/config.js.map +1 -0
- package/dist/conversation-viewer.d.ts +40 -0
- package/dist/conversation-viewer.d.ts.map +1 -0
- package/dist/conversation-viewer.js +276 -0
- package/dist/conversation-viewer.js.map +1 -0
- package/dist/core-compat.d.ts +14 -0
- package/dist/core-compat.d.ts.map +1 -0
- package/dist/core-compat.js +24 -0
- package/dist/core-compat.js.map +1 -0
- package/dist/custom-agents.d.ts +14 -0
- package/dist/custom-agents.d.ts.map +1 -0
- package/dist/custom-agents.js +106 -0
- package/dist/custom-agents.js.map +1 -0
- package/dist/file-lock.d.ts +42 -0
- package/dist/file-lock.d.ts.map +1 -0
- package/dist/file-lock.js +91 -0
- package/dist/file-lock.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +751 -0
- package/dist/index.js.map +1 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.d.ts.map +1 -0
- package/dist/model-resolver.js +61 -0
- package/dist/model-resolver.js.map +1 -0
- package/dist/types.d.ts +96 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +47 -0
- package/dist/types.js.map +1 -0
- package/dist/widget.d.ts +56 -0
- package/dist/widget.d.ts.map +1 -0
- package/dist/widget.js +396 -0
- package/dist/widget.js.map +1 -0
- package/package.json +10 -6
- package/src/__tests__/badge-generation.test.ts +0 -315
- package/src/__tests__/config.test.ts +0 -240
- package/src/__tests__/esc-propagation.test.ts +0 -162
- package/src/__tests__/file-lock.test.ts +0 -244
- package/src/__tests__/shutdown-stale-ctx.test.ts +0 -185
- package/src/__tests__/workflow-integration.test.ts +0 -334
- package/src/agent-manager.ts +0 -334
- package/src/agent-runner.ts +0 -329
- package/src/config.ts +0 -147
- package/src/conversation-viewer.ts +0 -299
- package/src/custom-agents.ts +0 -118
- package/src/file-lock.ts +0 -102
- package/src/index.ts +0 -862
- package/src/model-resolver.ts +0 -79
- package/src/prompts.ts +0 -39
- package/src/skills/explore/SKILL.md +0 -32
- package/src/skills/work/SKILL.md +0 -40
- package/src/types.ts +0 -146
- package/src/widget.ts +0 -454
- package/tsconfig.json +0 -19
package/src/agent-runner.ts
DELETED
|
@@ -1,329 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @pi-unipi/subagents — Agent runner
|
|
3
|
-
*
|
|
4
|
-
* Creates sessions, runs agents, collects results.
|
|
5
|
-
* Forwards abort signals for ESC propagation.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Model } from "@earendil-works/pi-ai";
|
|
9
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
10
|
-
import {
|
|
11
|
-
type AgentSession,
|
|
12
|
-
type AgentSessionEvent,
|
|
13
|
-
createAgentSession,
|
|
14
|
-
DefaultResourceLoader,
|
|
15
|
-
type ExtensionAPI,
|
|
16
|
-
getAgentDir,
|
|
17
|
-
SessionManager,
|
|
18
|
-
SettingsManager,
|
|
19
|
-
} from "@earendil-works/pi-coding-agent";
|
|
20
|
-
import { BUILTIN_CONFIGS, type AgentConfig, type AgentType, type ThinkingLevel } from "./types.js";
|
|
21
|
-
|
|
22
|
-
/** Tools excluded from subagents to prevent nesting. */
|
|
23
|
-
const EXCLUDED_TOOL_NAMES = ["Agent", "get_result"];
|
|
24
|
-
|
|
25
|
-
/** All known built-in tool names. */
|
|
26
|
-
const BUILTIN_TOOL_NAMES = ["read", "bash", "edit", "write", "grep", "find", "ls"];
|
|
27
|
-
|
|
28
|
-
/** Default max turns. undefined = unlimited. */
|
|
29
|
-
let defaultMaxTurns: number | undefined;
|
|
30
|
-
|
|
31
|
-
export function getDefaultMaxTurns(): number | undefined {
|
|
32
|
-
return defaultMaxTurns;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function setDefaultMaxTurns(n: number | undefined): void {
|
|
36
|
-
defaultMaxTurns = n == null || n === 0 ? undefined : Math.max(1, n);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Grace turns after soft limit. */
|
|
40
|
-
let graceTurns = 5;
|
|
41
|
-
|
|
42
|
-
export function getGraceTurns(): number {
|
|
43
|
-
return graceTurns;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function setGraceTurns(n: number): void {
|
|
47
|
-
graceTurns = Math.max(1, n);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Tool activity info. */
|
|
51
|
-
export interface ToolActivity {
|
|
52
|
-
type: "start" | "end";
|
|
53
|
-
toolName: string;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Options for running an agent. */
|
|
57
|
-
export interface RunOptions {
|
|
58
|
-
pi: ExtensionAPI;
|
|
59
|
-
model?: Model<any>;
|
|
60
|
-
agentConfig?: AgentConfig;
|
|
61
|
-
maxTurns?: number;
|
|
62
|
-
signal?: AbortSignal;
|
|
63
|
-
isolated?: boolean;
|
|
64
|
-
inheritContext?: boolean;
|
|
65
|
-
thinkingLevel?: ThinkingLevel;
|
|
66
|
-
cwd?: string;
|
|
67
|
-
onToolActivity?: (activity: ToolActivity) => void;
|
|
68
|
-
onTextDelta?: (delta: string, fullText: string) => void;
|
|
69
|
-
onSessionCreated?: (session: AgentSession) => void;
|
|
70
|
-
onTurnEnd?: (turnCount: number) => void;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Result from running an agent. */
|
|
74
|
-
export interface RunResult {
|
|
75
|
-
responseText: string;
|
|
76
|
-
session: AgentSession;
|
|
77
|
-
aborted: boolean;
|
|
78
|
-
steered: boolean;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Collect last assistant message text. */
|
|
82
|
-
function collectResponseText(session: AgentSession) {
|
|
83
|
-
let text = "";
|
|
84
|
-
const unsubscribe = session.subscribe((event: AgentSessionEvent) => {
|
|
85
|
-
if (event.type === "message_start") {
|
|
86
|
-
text = "";
|
|
87
|
-
}
|
|
88
|
-
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
|
89
|
-
text += event.assistantMessageEvent.delta;
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
return { getText: () => text, unsubscribe };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Get last assistant text from session history. */
|
|
96
|
-
function getLastAssistantText(session: AgentSession): string {
|
|
97
|
-
for (let i = session.messages.length - 1; i >= 0; i--) {
|
|
98
|
-
const msg = session.messages[i];
|
|
99
|
-
if (msg.role !== "assistant") continue;
|
|
100
|
-
const text = msg.content
|
|
101
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
102
|
-
.map((c) => c.text)
|
|
103
|
-
.join("")
|
|
104
|
-
.trim();
|
|
105
|
-
if (text) return text;
|
|
106
|
-
}
|
|
107
|
-
return "";
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** Wire abort signal to session. */
|
|
111
|
-
function forwardAbortSignal(session: AgentSession, signal?: AbortSignal): () => void {
|
|
112
|
-
if (!signal) return () => {};
|
|
113
|
-
const onAbort = () => session.abort();
|
|
114
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
115
|
-
return () => signal.removeEventListener("abort", onAbort);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** Get tool names for agent type. */
|
|
119
|
-
function getToolNamesForType(type: AgentType, config?: AgentConfig): string[] {
|
|
120
|
-
if (config?.builtinToolNames?.length) {
|
|
121
|
-
return [...config.builtinToolNames];
|
|
122
|
-
}
|
|
123
|
-
return [...BUILTIN_TOOL_NAMES];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Resolve agent config for a type.
|
|
128
|
-
* Priority: explicit config > builtin config > default
|
|
129
|
-
*/
|
|
130
|
-
function resolveAgentConfig(type: AgentType, explicitConfig?: AgentConfig): AgentConfig | undefined {
|
|
131
|
-
if (explicitConfig) return explicitConfig;
|
|
132
|
-
return BUILTIN_CONFIGS[type];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/** Resolve model from config. */
|
|
136
|
-
function resolveDefaultModel(
|
|
137
|
-
parentModel: Model<any> | undefined,
|
|
138
|
-
configModel?: string,
|
|
139
|
-
): Model<any> | undefined {
|
|
140
|
-
// For now, just use parent model. Full model resolution requires registry.
|
|
141
|
-
return parentModel;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Run an agent session.
|
|
146
|
-
*/
|
|
147
|
-
export async function runAgent(
|
|
148
|
-
ctx: ExtensionContext,
|
|
149
|
-
type: AgentType,
|
|
150
|
-
prompt: string,
|
|
151
|
-
options: RunOptions,
|
|
152
|
-
): Promise<RunResult> {
|
|
153
|
-
const effectiveCwd = options.cwd ?? ctx.cwd;
|
|
154
|
-
|
|
155
|
-
// Resolve agent config
|
|
156
|
-
const agentConfig = resolveAgentConfig(type, options.agentConfig);
|
|
157
|
-
const parentSystemPrompt = ctx.getSystemPrompt();
|
|
158
|
-
|
|
159
|
-
// Build system prompt using config or defaults
|
|
160
|
-
let systemPrompt: string;
|
|
161
|
-
if (agentConfig?.systemPrompt && agentConfig.promptMode === "replace") {
|
|
162
|
-
systemPrompt = agentConfig.systemPrompt;
|
|
163
|
-
} else if (options.isolated) {
|
|
164
|
-
const base = agentConfig?.systemPrompt ?? `You are a ${type} agent.`;
|
|
165
|
-
systemPrompt = `${base} Follow the task instructions precisely. Do not ask questions.`;
|
|
166
|
-
} else {
|
|
167
|
-
const agentPrompt = agentConfig?.systemPrompt ?? `You are a ${type} agent.`;
|
|
168
|
-
systemPrompt = parentSystemPrompt + `\n\n${agentPrompt}`;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Get tool names from config
|
|
172
|
-
let toolNames = getToolNamesForType(type, agentConfig);
|
|
173
|
-
|
|
174
|
-
// Create resource loader
|
|
175
|
-
// Respect agentConfig.extensions/skills flags: if explicitly false, skip loading.
|
|
176
|
-
// This prevents explore/work agents from loading all parent extensions.
|
|
177
|
-
const agentDir = getAgentDir();
|
|
178
|
-
const skipExtensions = options.isolated || agentConfig?.extensions === false;
|
|
179
|
-
const skipSkills = options.isolated || agentConfig?.skills === false;
|
|
180
|
-
const loader = new DefaultResourceLoader({
|
|
181
|
-
cwd: effectiveCwd,
|
|
182
|
-
agentDir,
|
|
183
|
-
noExtensions: skipExtensions,
|
|
184
|
-
noSkills: skipSkills,
|
|
185
|
-
noPromptTemplates: true,
|
|
186
|
-
noThemes: true,
|
|
187
|
-
noContextFiles: true,
|
|
188
|
-
systemPromptOverride: () => systemPrompt,
|
|
189
|
-
appendSystemPromptOverride: () => [],
|
|
190
|
-
});
|
|
191
|
-
await loader.reload();
|
|
192
|
-
|
|
193
|
-
// Resolve model
|
|
194
|
-
const model = options.model ?? resolveDefaultModel(ctx.model);
|
|
195
|
-
|
|
196
|
-
// Create session
|
|
197
|
-
const sessionOpts: Parameters<typeof createAgentSession>[0] = {
|
|
198
|
-
cwd: effectiveCwd,
|
|
199
|
-
agentDir,
|
|
200
|
-
sessionManager: SessionManager.inMemory(effectiveCwd),
|
|
201
|
-
settingsManager: SettingsManager.create(effectiveCwd, agentDir),
|
|
202
|
-
modelRegistry: ctx.modelRegistry,
|
|
203
|
-
model,
|
|
204
|
-
tools: toolNames,
|
|
205
|
-
resourceLoader: loader,
|
|
206
|
-
};
|
|
207
|
-
if (options.thinkingLevel) {
|
|
208
|
-
sessionOpts.thinkingLevel = options.thinkingLevel;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const { session } = await createAgentSession(sessionOpts);
|
|
212
|
-
|
|
213
|
-
// Filter out our tools to prevent nesting
|
|
214
|
-
const activeTools = session.getActiveToolNames().filter((t) => {
|
|
215
|
-
if (EXCLUDED_TOOL_NAMES.includes(t)) return false;
|
|
216
|
-
return true;
|
|
217
|
-
});
|
|
218
|
-
session.setActiveToolsByName(activeTools);
|
|
219
|
-
|
|
220
|
-
// Bind extensions — only if extensions were loaded.
|
|
221
|
-
// Skipping for agents with extensions: false avoids firing session_start
|
|
222
|
-
// on an empty extension set, preventing unnecessary MODULE_READY cascade.
|
|
223
|
-
if (!skipExtensions) {
|
|
224
|
-
await session.bindExtensions({
|
|
225
|
-
onError: (err) => {
|
|
226
|
-
options.onToolActivity?.({
|
|
227
|
-
type: "end",
|
|
228
|
-
toolName: `extension-error:${err.extensionPath}`,
|
|
229
|
-
});
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
options.onSessionCreated?.(session);
|
|
235
|
-
|
|
236
|
-
// Track turns
|
|
237
|
-
let turnCount = 0;
|
|
238
|
-
const maxTurns = options.maxTurns ?? defaultMaxTurns;
|
|
239
|
-
let softLimitReached = false;
|
|
240
|
-
let aborted = false;
|
|
241
|
-
|
|
242
|
-
let currentMessageText = "";
|
|
243
|
-
const unsubTurns = session.subscribe((event: AgentSessionEvent) => {
|
|
244
|
-
if (event.type === "turn_end") {
|
|
245
|
-
turnCount++;
|
|
246
|
-
options.onTurnEnd?.(turnCount);
|
|
247
|
-
if (maxTurns != null) {
|
|
248
|
-
if (!softLimitReached && turnCount >= maxTurns) {
|
|
249
|
-
softLimitReached = true;
|
|
250
|
-
session.steer("You have reached your turn limit. Wrap up immediately — provide your final answer now.");
|
|
251
|
-
} else if (softLimitReached && turnCount >= maxTurns + graceTurns) {
|
|
252
|
-
aborted = true;
|
|
253
|
-
session.abort();
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
if (event.type === "message_start") {
|
|
258
|
-
currentMessageText = "";
|
|
259
|
-
}
|
|
260
|
-
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
|
261
|
-
currentMessageText += event.assistantMessageEvent.delta;
|
|
262
|
-
options.onTextDelta?.(event.assistantMessageEvent.delta, currentMessageText);
|
|
263
|
-
}
|
|
264
|
-
if (event.type === "tool_execution_start") {
|
|
265
|
-
options.onToolActivity?.({ type: "start", toolName: event.toolName });
|
|
266
|
-
}
|
|
267
|
-
if (event.type === "tool_execution_end") {
|
|
268
|
-
options.onToolActivity?.({ type: "end", toolName: event.toolName });
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
const collector = collectResponseText(session);
|
|
273
|
-
const cleanupAbort = forwardAbortSignal(session, options.signal);
|
|
274
|
-
|
|
275
|
-
try {
|
|
276
|
-
await session.prompt(prompt);
|
|
277
|
-
} finally {
|
|
278
|
-
unsubTurns();
|
|
279
|
-
collector.unsubscribe();
|
|
280
|
-
cleanupAbort();
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const responseText = collector.getText().trim() || getLastAssistantText(session);
|
|
284
|
-
return { responseText, session, aborted, steered: softLimitReached };
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Get conversation text from a session.
|
|
289
|
-
*/
|
|
290
|
-
export function getAgentConversation(session: AgentSession): string {
|
|
291
|
-
const parts: string[] = [];
|
|
292
|
-
|
|
293
|
-
for (const msg of session.messages) {
|
|
294
|
-
if (msg.role === "user") {
|
|
295
|
-
const content = msg.content;
|
|
296
|
-
const text = typeof content === "string"
|
|
297
|
-
? content
|
|
298
|
-
: content
|
|
299
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
300
|
-
.map((c) => c.text)
|
|
301
|
-
.join("");
|
|
302
|
-
if (text.trim()) parts.push(`[User]: ${text.trim()}`);
|
|
303
|
-
} else if (msg.role === "assistant") {
|
|
304
|
-
const textParts: string[] = [];
|
|
305
|
-
const toolCalls: string[] = [];
|
|
306
|
-
const content = msg.content;
|
|
307
|
-
if (typeof content !== "string") {
|
|
308
|
-
for (const c of content) {
|
|
309
|
-
if (c.type === "text" && c.text) textParts.push(c.text);
|
|
310
|
-
else if (c.type === "toolCall") toolCalls.push(` Tool: ${(c as any).name ?? "unknown"}`);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
if (textParts.length > 0) parts.push(`[Assistant]: ${textParts.join("\n")}`);
|
|
314
|
-
if (toolCalls.length > 0) parts.push(`[Tool Calls]:\n${toolCalls.join("\n")}`);
|
|
315
|
-
} else if (msg.role === "toolResult") {
|
|
316
|
-
const content = msg.content;
|
|
317
|
-
const text = typeof content === "string"
|
|
318
|
-
? content
|
|
319
|
-
: content
|
|
320
|
-
.filter((c): c is { type: "text"; text: string } => c.type === "text")
|
|
321
|
-
.map((c) => c.text)
|
|
322
|
-
.join("");
|
|
323
|
-
const truncated = text.length > 200 ? text.slice(0, 200) + "..." : text;
|
|
324
|
-
parts.push(`[Tool Result (${msg.toolName})]: ${truncated}`);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
return parts.join("\n\n");
|
|
329
|
-
}
|
package/src/config.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @pi-unipi/subagents — Config management
|
|
3
|
-
*
|
|
4
|
-
* Loads config from ~/.unipi/config/subagents.json (global)
|
|
5
|
-
* and <workspace>/.unipi/config/subagents.json (override).
|
|
6
|
-
* Auto-generates on first run. Repairs corrupted files.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
10
|
-
import { join } from "node:path";
|
|
11
|
-
import { homedir } from "node:os";
|
|
12
|
-
import type { SubagentsConfig } from "./types.js";
|
|
13
|
-
|
|
14
|
-
const DEFAULT_CONFIG: SubagentsConfig = {
|
|
15
|
-
maxConcurrent: 4,
|
|
16
|
-
enabled: true,
|
|
17
|
-
types: {
|
|
18
|
-
explore: { enabled: true },
|
|
19
|
-
work: { enabled: true },
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/** Get global config path: ~/.unipi/config/subagents.json */
|
|
24
|
-
function getGlobalConfigPath(): string {
|
|
25
|
-
return join(homedir(), ".unipi", "config", "subagents.json");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Get workspace config path: <cwd>/.unipi/config/subagents.json */
|
|
29
|
-
function getWorkspaceConfigPath(cwd: string): string {
|
|
30
|
-
return join(cwd, ".unipi", "config", "subagents.json");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Ensure directory exists. */
|
|
34
|
-
function ensureDir(filePath: string): void {
|
|
35
|
-
const dir = filePath.substring(0, filePath.lastIndexOf("/"));
|
|
36
|
-
if (!existsSync(dir)) {
|
|
37
|
-
mkdirSync(dir, { recursive: true });
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Ensure a directory exists (not a file path). */
|
|
42
|
-
function ensureDirExists(dirPath: string): void {
|
|
43
|
-
if (!existsSync(dirPath)) {
|
|
44
|
-
mkdirSync(dirPath, { recursive: true });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** Write config atomically (write then rename). */
|
|
49
|
-
function writeConfigAtomic(filePath: string, config: SubagentsConfig): void {
|
|
50
|
-
const tmpPath = filePath + ".tmp";
|
|
51
|
-
writeFileSync(tmpPath, JSON.stringify(config, null, 2), "utf-8");
|
|
52
|
-
renameSync(tmpPath, filePath);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Load and parse config from a path. Returns null on failure. */
|
|
56
|
-
function loadConfigFromPath(filePath: string): SubagentsConfig | null {
|
|
57
|
-
if (!existsSync(filePath)) return null;
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
const content = readFileSync(filePath, "utf-8");
|
|
61
|
-
const parsed = JSON.parse(content);
|
|
62
|
-
// Basic validation
|
|
63
|
-
if (typeof parsed !== "object" || parsed === null) return null;
|
|
64
|
-
return parsed as SubagentsConfig;
|
|
65
|
-
} catch {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** Repair corrupted config: rename to .bak and generate fresh. */
|
|
71
|
-
function repairCorrupted(filePath: string): SubagentsConfig {
|
|
72
|
-
const backupPath = filePath + ".bak";
|
|
73
|
-
try {
|
|
74
|
-
renameSync(filePath, backupPath);
|
|
75
|
-
} catch {
|
|
76
|
-
// If rename fails, just overwrite
|
|
77
|
-
}
|
|
78
|
-
writeConfigAtomic(filePath, DEFAULT_CONFIG);
|
|
79
|
-
return DEFAULT_CONFIG;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Initialize config on extension start.
|
|
84
|
-
* - If missing: generate with defaults
|
|
85
|
-
* - If corrupted: rename to .bak, generate fresh
|
|
86
|
-
* - If valid: load
|
|
87
|
-
*/
|
|
88
|
-
export function initConfig(cwd: string): SubagentsConfig {
|
|
89
|
-
const globalPath = getGlobalConfigPath();
|
|
90
|
-
const globalDir = join(homedir(), ".unipi", "config");
|
|
91
|
-
const globalAgentsDir = join(homedir(), ".unipi", "config", "agents");
|
|
92
|
-
|
|
93
|
-
// Ensure directories exist
|
|
94
|
-
ensureDirExists(globalDir);
|
|
95
|
-
ensureDirExists(globalAgentsDir);
|
|
96
|
-
|
|
97
|
-
// Load or create global config
|
|
98
|
-
let globalConfig = loadConfigFromPath(globalPath);
|
|
99
|
-
if (globalConfig === null) {
|
|
100
|
-
globalConfig = repairCorrupted(globalPath);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Ensure workspace directories exist if workspace exists
|
|
104
|
-
const workspaceDir = join(cwd, ".unipi", "config");
|
|
105
|
-
const workspaceAgentsDir = join(cwd, ".unipi", "config", "agents");
|
|
106
|
-
if (cwd && !cwd.startsWith(homedir())) {
|
|
107
|
-
// Only create workspace dirs if not in home directory
|
|
108
|
-
ensureDirExists(workspaceDir);
|
|
109
|
-
ensureDirExists(workspaceAgentsDir);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Load workspace override if exists
|
|
113
|
-
const workspacePath = getWorkspaceConfigPath(cwd);
|
|
114
|
-
const workspaceConfig = loadConfigFromPath(workspacePath);
|
|
115
|
-
|
|
116
|
-
if (workspaceConfig) {
|
|
117
|
-
// Merge: workspace overrides global on any field present
|
|
118
|
-
return {
|
|
119
|
-
...globalConfig,
|
|
120
|
-
...workspaceConfig,
|
|
121
|
-
types: {
|
|
122
|
-
...globalConfig.types,
|
|
123
|
-
...workspaceConfig.types,
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return globalConfig;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Save global config.
|
|
133
|
-
*/
|
|
134
|
-
export function saveGlobalConfig(config: SubagentsConfig): void {
|
|
135
|
-
const globalPath = getGlobalConfigPath();
|
|
136
|
-
ensureDir(globalPath);
|
|
137
|
-
writeConfigAtomic(globalPath, config);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Save workspace config.
|
|
142
|
-
*/
|
|
143
|
-
export function saveWorkspaceConfig(cwd: string, config: SubagentsConfig): void {
|
|
144
|
-
const workspacePath = getWorkspaceConfigPath(cwd);
|
|
145
|
-
ensureDir(workspacePath);
|
|
146
|
-
writeConfigAtomic(workspacePath, config);
|
|
147
|
-
}
|