@pi-agents/orchid 0.1.0-beta.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.
- package/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
4
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import {
|
|
6
|
+
type AgentConfig,
|
|
7
|
+
type AgentScope,
|
|
8
|
+
type AgentSource,
|
|
9
|
+
type ChainConfig,
|
|
10
|
+
type ChainStepConfig,
|
|
11
|
+
defaultInheritProjectContext,
|
|
12
|
+
defaultInheritSkills,
|
|
13
|
+
defaultSystemPromptMode,
|
|
14
|
+
discoverAgentsAll,
|
|
15
|
+
buildRuntimeName,
|
|
16
|
+
frontmatterNameForConfig,
|
|
17
|
+
parsePackageName,
|
|
18
|
+
} from "./agents.ts";
|
|
19
|
+
import { serializeAgent } from "./agent-serializer.ts";
|
|
20
|
+
import { serializeChain } from "./chain-serializer.ts";
|
|
21
|
+
import { discoverAvailableSkills } from "./skills.ts";
|
|
22
|
+
import type { Details } from "../shared/types.ts";
|
|
23
|
+
|
|
24
|
+
type ManagementAction = "list" | "get" | "create" | "update" | "delete";
|
|
25
|
+
type ManagementScope = "user" | "project";
|
|
26
|
+
type ManagementContext = Pick<ExtensionContext, "cwd" | "modelRegistry">;
|
|
27
|
+
|
|
28
|
+
interface ManagementParams {
|
|
29
|
+
action?: string;
|
|
30
|
+
agent?: string;
|
|
31
|
+
chainName?: string;
|
|
32
|
+
agentScope?: string;
|
|
33
|
+
config?: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function result(text: string, isError = false): AgentToolResult<Details> {
|
|
37
|
+
return { content: [{ type: "text", text }], isError, details: { mode: "management", results: [] } };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parseCsv(value: string): string[] {
|
|
41
|
+
return [...new Set(value.split(",").map((v) => v.trim()).filter(Boolean))];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function configObject(config: unknown): { value?: Record<string, unknown>; error?: string } {
|
|
45
|
+
let val = config;
|
|
46
|
+
if (typeof val === "string") {
|
|
47
|
+
try {
|
|
48
|
+
val = JSON.parse(val);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
51
|
+
return { error: `config must be valid JSON: ${message}` };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!val || typeof val !== "object" || Array.isArray(val)) return {};
|
|
55
|
+
return { value: val as Record<string, unknown> };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function hasKey(obj: Record<string, unknown>, key: string): boolean {
|
|
59
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function asDisambiguationScope(scope: unknown): ManagementScope | undefined {
|
|
63
|
+
if (scope === "user" || scope === "project") return scope;
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function normalizeListScope(scope: unknown): AgentScope | undefined {
|
|
68
|
+
if (scope === undefined) return "both";
|
|
69
|
+
if (scope === "user" || scope === "project" || scope === "both") return scope;
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function sanitizeName(name: string): string {
|
|
74
|
+
return name.toLowerCase().trim().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function parsePackageConfig(value: unknown): { packageName?: string; error?: string } {
|
|
78
|
+
return parsePackageName(value, "config.package");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function allAgents(d: { builtin: AgentConfig[]; user: AgentConfig[]; project: AgentConfig[] }): AgentConfig[] {
|
|
82
|
+
return [...d.builtin, ...d.user, ...d.project];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function availableNames(cwd: string, kind: "agent" | "chain"): string[] {
|
|
86
|
+
const d = discoverAgentsAll(cwd);
|
|
87
|
+
const items = kind === "agent" ? allAgents(d) : d.chains;
|
|
88
|
+
return [...new Set(items.map((x) => x.name))].sort((a, b) => a.localeCompare(b));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function findAgents(name: string, cwd: string, scope: AgentScope = "both"): AgentConfig[] {
|
|
92
|
+
const d = discoverAgentsAll(cwd);
|
|
93
|
+
const raw = name.trim();
|
|
94
|
+
const sanitized = sanitizeName(raw);
|
|
95
|
+
return allAgents(d)
|
|
96
|
+
.filter((a) => (scope === "both" || a.source === scope) && (a.name === raw || a.name === sanitized))
|
|
97
|
+
.sort((a, b) => a.source.localeCompare(b.source));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function findChains(name: string, cwd: string, scope: AgentScope = "both"): ChainConfig[] {
|
|
101
|
+
const raw = name.trim();
|
|
102
|
+
const sanitized = sanitizeName(raw);
|
|
103
|
+
return discoverAgentsAll(cwd).chains
|
|
104
|
+
.filter((c) => (scope === "both" || c.source === scope) && (c.name === raw || c.name === sanitized))
|
|
105
|
+
.sort((a, b) => a.source.localeCompare(b.source));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function nameExistsInScope(cwd: string, scope: ManagementScope, name: string, excludePath?: string): boolean {
|
|
109
|
+
const d = discoverAgentsAll(cwd);
|
|
110
|
+
for (const a of scope === "user" ? d.user : d.project) {
|
|
111
|
+
if (a.name === name && a.filePath !== excludePath) return true;
|
|
112
|
+
}
|
|
113
|
+
for (const c of d.chains) {
|
|
114
|
+
if (c.source === scope && c.name === name && c.filePath !== excludePath) return true;
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function unknownChainAgents(cwd: string, steps: ChainStepConfig[]): string[] {
|
|
120
|
+
const d = discoverAgentsAll(cwd);
|
|
121
|
+
const known = new Set(allAgents(d).map((a) => a.name));
|
|
122
|
+
return [...new Set(steps.map((s) => s.agent).filter((a) => !known.has(a)))].sort((a, b) => a.localeCompare(b));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function chainStepWarnings(ctx: ManagementContext, steps: ChainStepConfig[]): string[] {
|
|
126
|
+
const warnings: string[] = [];
|
|
127
|
+
const available = new Set(discoverAvailableSkills(ctx.cwd).map((s) => s.name));
|
|
128
|
+
for (let i = 0; i < steps.length; i++) {
|
|
129
|
+
const s = steps[i]!;
|
|
130
|
+
if (s.model) {
|
|
131
|
+
const found = ctx.modelRegistry.getAvailable().some((m) => `${m.provider}/${m.id}` === s.model || m.id === s.model);
|
|
132
|
+
if (!found) warnings.push(`Warning: step ${i + 1} (${s.agent}): model '${s.model}' is not in the current model registry.`);
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(s.skills) && s.skills.length > 0) {
|
|
135
|
+
const missing = s.skills.filter((sk) => !available.has(sk));
|
|
136
|
+
if (missing.length) warnings.push(`Warning: step ${i + 1} (${s.agent}): skills not found: ${missing.join(", ")}.`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return warnings;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function modelWarning(ctx: ManagementContext, model: string | undefined): string | undefined {
|
|
143
|
+
if (!model) return undefined;
|
|
144
|
+
const found = ctx.modelRegistry.getAvailable().some((m) => `${m.provider}/${m.id}` === model || m.id === model);
|
|
145
|
+
return found ? undefined : `Warning: model '${model}' is not in the current model registry.`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function fallbackModelsWarning(ctx: ManagementContext, fallbackModels: string[] | undefined): string | undefined {
|
|
149
|
+
if (!fallbackModels || fallbackModels.length === 0) return undefined;
|
|
150
|
+
const available = new Set(ctx.modelRegistry.getAvailable().flatMap((m) => [`${m.provider}/${m.id}`, m.id]));
|
|
151
|
+
const missing = fallbackModels.filter((model) => !available.has(model));
|
|
152
|
+
return missing.length ? `Warning: fallback models not in the current model registry: ${missing.join(", ")}.` : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function skillsWarning(cwd: string, skills: string[] | undefined): string | undefined {
|
|
156
|
+
if (!skills || skills.length === 0) return undefined;
|
|
157
|
+
const available = new Set(discoverAvailableSkills(cwd).map((s) => s.name));
|
|
158
|
+
const missing = skills.filter((s) => !available.has(s));
|
|
159
|
+
return missing.length ? `Warning: skills not found: ${missing.join(", ")}.` : undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function parseStepList(raw: unknown): { steps?: ChainStepConfig[]; error?: string } {
|
|
163
|
+
if (!Array.isArray(raw)) return { error: "config.steps must be an array." };
|
|
164
|
+
if (raw.length === 0) return { error: "config.steps must include at least one step." };
|
|
165
|
+
const steps: ChainStepConfig[] = [];
|
|
166
|
+
for (let i = 0; i < raw.length; i++) {
|
|
167
|
+
const item = raw[i];
|
|
168
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) return { error: `config.steps[${i}] must be an object.` };
|
|
169
|
+
const s = item as Record<string, unknown>;
|
|
170
|
+
if (typeof s.agent !== "string" || !s.agent.trim()) return { error: `config.steps[${i}].agent must be a non-empty string.` };
|
|
171
|
+
const step: ChainStepConfig = { agent: s.agent.trim(), task: typeof s.task === "string" ? s.task : "" };
|
|
172
|
+
if (hasKey(s, "output")) {
|
|
173
|
+
if (s.output === false) step.output = false;
|
|
174
|
+
else if (typeof s.output === "string") step.output = s.output;
|
|
175
|
+
else return { error: `config.steps[${i}].output must be a string or false.` };
|
|
176
|
+
}
|
|
177
|
+
if (hasKey(s, "outputMode")) {
|
|
178
|
+
if (s.outputMode === "inline" || s.outputMode === "file-only") step.outputMode = s.outputMode;
|
|
179
|
+
else return { error: `config.steps[${i}].outputMode must be 'inline' or 'file-only'.` };
|
|
180
|
+
}
|
|
181
|
+
if (hasKey(s, "reads")) {
|
|
182
|
+
if (s.reads === false) step.reads = false;
|
|
183
|
+
else if (Array.isArray(s.reads)) step.reads = s.reads.filter((v): v is string => typeof v === "string").map((v) => v.trim()).filter(Boolean);
|
|
184
|
+
else return { error: `config.steps[${i}].reads must be an array or false.` };
|
|
185
|
+
}
|
|
186
|
+
if (hasKey(s, "model")) {
|
|
187
|
+
if (typeof s.model === "string") step.model = s.model;
|
|
188
|
+
else return { error: `config.steps[${i}].model must be a string.` };
|
|
189
|
+
}
|
|
190
|
+
if (hasKey(s, "skills")) {
|
|
191
|
+
if (s.skills === false) step.skills = false;
|
|
192
|
+
else if (Array.isArray(s.skills)) step.skills = s.skills.filter((v): v is string => typeof v === "string").map((v) => v.trim()).filter(Boolean);
|
|
193
|
+
else return { error: `config.steps[${i}].skills must be an array or false.` };
|
|
194
|
+
}
|
|
195
|
+
if (hasKey(s, "progress")) {
|
|
196
|
+
if (typeof s.progress === "boolean") step.progress = s.progress;
|
|
197
|
+
else return { error: `config.steps[${i}].progress must be a boolean.` };
|
|
198
|
+
}
|
|
199
|
+
steps.push(step);
|
|
200
|
+
}
|
|
201
|
+
return { steps };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function parseTools(raw: string): { tools?: string[]; mcpDirectTools?: string[] } {
|
|
205
|
+
const tools: string[] = [];
|
|
206
|
+
const mcpDirectTools: string[] = [];
|
|
207
|
+
for (const item of parseCsv(raw)) {
|
|
208
|
+
if (item.startsWith("mcp:")) {
|
|
209
|
+
const direct = item.slice(4).trim();
|
|
210
|
+
if (direct) mcpDirectTools.push(direct);
|
|
211
|
+
} else tools.push(item);
|
|
212
|
+
}
|
|
213
|
+
return { tools: tools.length ? tools : undefined, mcpDirectTools: mcpDirectTools.length ? mcpDirectTools : undefined };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function applyAgentConfig(target: AgentConfig, cfg: Record<string, unknown>): string | undefined {
|
|
217
|
+
if (hasKey(cfg, "systemPrompt")) {
|
|
218
|
+
if (cfg.systemPrompt === false || cfg.systemPrompt === "") target.systemPrompt = "";
|
|
219
|
+
else if (typeof cfg.systemPrompt === "string") target.systemPrompt = cfg.systemPrompt;
|
|
220
|
+
else return "config.systemPrompt must be a string or false when provided.";
|
|
221
|
+
}
|
|
222
|
+
if (hasKey(cfg, "model")) {
|
|
223
|
+
if (cfg.model === false || cfg.model === "") target.model = undefined;
|
|
224
|
+
else if (typeof cfg.model === "string") target.model = cfg.model.trim() || undefined;
|
|
225
|
+
else return "config.model must be a string or false when provided.";
|
|
226
|
+
}
|
|
227
|
+
if (hasKey(cfg, "fallbackModels")) {
|
|
228
|
+
if (cfg.fallbackModels === false || cfg.fallbackModels === "") target.fallbackModels = undefined;
|
|
229
|
+
else if (typeof cfg.fallbackModels === "string") {
|
|
230
|
+
const models = parseCsv(cfg.fallbackModels);
|
|
231
|
+
target.fallbackModels = models.length ? models : undefined;
|
|
232
|
+
} else if (Array.isArray(cfg.fallbackModels)) {
|
|
233
|
+
const models = cfg.fallbackModels
|
|
234
|
+
.filter((value): value is string => typeof value === "string")
|
|
235
|
+
.map((value) => value.trim())
|
|
236
|
+
.filter(Boolean);
|
|
237
|
+
target.fallbackModels = models.length ? [...new Set(models)] : undefined;
|
|
238
|
+
} else return "config.fallbackModels must be a comma-separated string, string array, or false when provided.";
|
|
239
|
+
}
|
|
240
|
+
if (hasKey(cfg, "tools")) {
|
|
241
|
+
if (cfg.tools === false || cfg.tools === "") { target.tools = undefined; target.mcpDirectTools = undefined; }
|
|
242
|
+
else if (typeof cfg.tools === "string") { const parsed = parseTools(cfg.tools); target.tools = parsed.tools; target.mcpDirectTools = parsed.mcpDirectTools; }
|
|
243
|
+
else return "config.tools must be a comma-separated string or false when provided.";
|
|
244
|
+
}
|
|
245
|
+
if (hasKey(cfg, "skills")) {
|
|
246
|
+
if (cfg.skills === false || cfg.skills === "") target.skills = undefined;
|
|
247
|
+
else if (typeof cfg.skills === "string") { const skills = parseCsv(cfg.skills); target.skills = skills.length ? skills : undefined; }
|
|
248
|
+
else return "config.skills must be a comma-separated string or false when provided.";
|
|
249
|
+
}
|
|
250
|
+
if (hasKey(cfg, "extensions")) {
|
|
251
|
+
if (cfg.extensions === false) target.extensions = undefined;
|
|
252
|
+
else if (cfg.extensions === "") target.extensions = [];
|
|
253
|
+
else if (typeof cfg.extensions === "string") target.extensions = parseCsv(cfg.extensions);
|
|
254
|
+
else return "config.extensions must be a comma-separated string, empty string, or false when provided.";
|
|
255
|
+
}
|
|
256
|
+
if (hasKey(cfg, "thinking")) {
|
|
257
|
+
if (cfg.thinking === false || cfg.thinking === "") target.thinking = undefined;
|
|
258
|
+
else if (typeof cfg.thinking === "string") target.thinking = cfg.thinking.trim() || undefined;
|
|
259
|
+
else return "config.thinking must be a string or false when provided.";
|
|
260
|
+
}
|
|
261
|
+
if (hasKey(cfg, "systemPromptMode")) {
|
|
262
|
+
if (cfg.systemPromptMode === "append" || cfg.systemPromptMode === "replace") target.systemPromptMode = cfg.systemPromptMode;
|
|
263
|
+
else return "config.systemPromptMode must be 'append' or 'replace' when provided.";
|
|
264
|
+
}
|
|
265
|
+
if (hasKey(cfg, "inheritProjectContext")) {
|
|
266
|
+
if (typeof cfg.inheritProjectContext !== "boolean") return "config.inheritProjectContext must be a boolean when provided.";
|
|
267
|
+
target.inheritProjectContext = cfg.inheritProjectContext;
|
|
268
|
+
}
|
|
269
|
+
if (hasKey(cfg, "inheritSkills")) {
|
|
270
|
+
if (typeof cfg.inheritSkills !== "boolean") return "config.inheritSkills must be a boolean when provided.";
|
|
271
|
+
target.inheritSkills = cfg.inheritSkills;
|
|
272
|
+
}
|
|
273
|
+
if (hasKey(cfg, "defaultContext")) {
|
|
274
|
+
if (cfg.defaultContext === false || cfg.defaultContext === "") target.defaultContext = undefined;
|
|
275
|
+
else if (cfg.defaultContext === "fresh" || cfg.defaultContext === "fork") target.defaultContext = cfg.defaultContext;
|
|
276
|
+
else return "config.defaultContext must be 'fresh', 'fork', or false when provided.";
|
|
277
|
+
}
|
|
278
|
+
if (hasKey(cfg, "output")) {
|
|
279
|
+
if (cfg.output === false || cfg.output === "") target.output = undefined;
|
|
280
|
+
else if (typeof cfg.output === "string") target.output = cfg.output;
|
|
281
|
+
else return "config.output must be a string or false when provided.";
|
|
282
|
+
}
|
|
283
|
+
if (hasKey(cfg, "reads")) {
|
|
284
|
+
if (cfg.reads === false || cfg.reads === "") target.defaultReads = undefined;
|
|
285
|
+
else if (typeof cfg.reads === "string") {
|
|
286
|
+
const reads = parseCsv(cfg.reads);
|
|
287
|
+
target.defaultReads = reads.length ? reads : undefined;
|
|
288
|
+
} else return "config.reads must be a comma-separated string or false when provided.";
|
|
289
|
+
}
|
|
290
|
+
if (hasKey(cfg, "progress")) {
|
|
291
|
+
if (typeof cfg.progress !== "boolean") return "config.progress must be a boolean when provided.";
|
|
292
|
+
target.defaultProgress = cfg.progress;
|
|
293
|
+
}
|
|
294
|
+
if (hasKey(cfg, "maxSubagentDepth")) {
|
|
295
|
+
if (cfg.maxSubagentDepth === false || cfg.maxSubagentDepth === "") target.maxSubagentDepth = undefined;
|
|
296
|
+
else if (typeof cfg.maxSubagentDepth === "number" && Number.isInteger(cfg.maxSubagentDepth) && cfg.maxSubagentDepth >= 0) {
|
|
297
|
+
target.maxSubagentDepth = cfg.maxSubagentDepth;
|
|
298
|
+
} else return "config.maxSubagentDepth must be an integer >= 0 or false when provided.";
|
|
299
|
+
}
|
|
300
|
+
if (hasKey(cfg, "completionGuard")) {
|
|
301
|
+
if (typeof cfg.completionGuard !== "boolean") return "config.completionGuard must be a boolean when provided.";
|
|
302
|
+
target.completionGuard = cfg.completionGuard;
|
|
303
|
+
}
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function resolveTarget<T extends { source: AgentSource; filePath: string }>(
|
|
308
|
+
kind: "agent" | "chain",
|
|
309
|
+
name: string,
|
|
310
|
+
matches: T[],
|
|
311
|
+
cwd: string,
|
|
312
|
+
scopeHint?: string,
|
|
313
|
+
): T | AgentToolResult<Details> {
|
|
314
|
+
const mutable = matches.filter((m) => m.source !== "builtin");
|
|
315
|
+
if (mutable.length === 0) {
|
|
316
|
+
if (matches.length > 0) {
|
|
317
|
+
return result(`${kind === "agent" ? "Agent" : "Chain"} '${name}' is builtin and cannot be modified. Create a same-named ${kind} in user or project scope to override it.`, true);
|
|
318
|
+
}
|
|
319
|
+
const available = availableNames(cwd, kind);
|
|
320
|
+
return result(`${kind === "agent" ? "Agent" : "Chain"} '${name}' not found. Available: ${available.join(", ") || "none"}.`, true);
|
|
321
|
+
}
|
|
322
|
+
if (mutable.length === 1) return mutable[0]!;
|
|
323
|
+
const scope = asDisambiguationScope(scopeHint);
|
|
324
|
+
if (!scope) {
|
|
325
|
+
const paths = mutable.map((m) => `${m.source}: ${m.filePath}`).join("\n");
|
|
326
|
+
return result(`${kind === "agent" ? "Agent" : "Chain"} '${name}' exists in both scopes. Specify agentScope: 'user' or 'project'.\n${paths}`, true);
|
|
327
|
+
}
|
|
328
|
+
const scoped = mutable.filter((m) => m.source === scope);
|
|
329
|
+
if (scoped.length === 0) return result(`${kind === "agent" ? "Agent" : "Chain"} '${name}' not found in scope '${scope}'.`, true);
|
|
330
|
+
if (scoped.length > 1) return result(`Multiple ${kind}s named '${name}' found in scope '${scope}': ${scoped.map((m) => m.filePath).join(", ")}`, true);
|
|
331
|
+
return scoped[0]!;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function renamePath(
|
|
335
|
+
kind: "agent" | "chain",
|
|
336
|
+
currentPath: string,
|
|
337
|
+
newName: string,
|
|
338
|
+
scope: ManagementScope,
|
|
339
|
+
cwd: string,
|
|
340
|
+
): { filePath?: string; error?: string } {
|
|
341
|
+
if (nameExistsInScope(cwd, scope, newName, currentPath)) return { error: `Name '${newName}' already exists in ${scope} scope.` };
|
|
342
|
+
const ext = kind === "agent" ? ".md" : ".chain.md";
|
|
343
|
+
const filePath = path.join(path.dirname(currentPath), `${newName}${ext}`);
|
|
344
|
+
if (fs.existsSync(filePath) && filePath !== currentPath) {
|
|
345
|
+
return { error: `File already exists at ${filePath} but is not a valid ${kind} definition. Remove or rename it first.` };
|
|
346
|
+
}
|
|
347
|
+
fs.renameSync(currentPath, filePath);
|
|
348
|
+
return { filePath };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function formatAgentDetail(agent: AgentConfig): string {
|
|
352
|
+
const tools = [...(agent.tools ?? []), ...(agent.mcpDirectTools ?? []).map((t) => `mcp:${t}`)];
|
|
353
|
+
const lines: string[] = [`Agent: ${agent.name} (${agent.source})`, `Path: ${agent.filePath}`, `Description: ${agent.description}`];
|
|
354
|
+
if (agent.packageName) {
|
|
355
|
+
lines.push(`Local name: ${frontmatterNameForConfig(agent)}`);
|
|
356
|
+
lines.push(`Package: ${agent.packageName}`);
|
|
357
|
+
}
|
|
358
|
+
if (agent.model) lines.push(`Model: ${agent.model}`);
|
|
359
|
+
if (agent.fallbackModels?.length) lines.push(`Fallback models: ${agent.fallbackModels.join(", ")}`);
|
|
360
|
+
if (tools.length) lines.push(`Tools: ${tools.join(", ")}`);
|
|
361
|
+
if (agent.skills?.length) lines.push(`Skills: ${agent.skills.join(", ")}`);
|
|
362
|
+
lines.push(`System prompt mode: ${agent.systemPromptMode}`);
|
|
363
|
+
lines.push(`Inherit project context: ${agent.inheritProjectContext ? "true" : "false"}`);
|
|
364
|
+
lines.push(`Inherit skills: ${agent.inheritSkills ? "true" : "false"}`);
|
|
365
|
+
if (agent.defaultContext) lines.push(`Default context: ${agent.defaultContext}`);
|
|
366
|
+
if (agent.source === "builtin") lines.push(`Disabled: ${agent.disabled ? "true" : "false"}`);
|
|
367
|
+
if (agent.extensions !== undefined) lines.push(`Extensions: ${agent.extensions.length ? agent.extensions.join(", ") : "(none)"}`);
|
|
368
|
+
if (agent.thinking) lines.push(`Thinking: ${agent.thinking}`);
|
|
369
|
+
if (agent.output) lines.push(`Output: ${agent.output}`);
|
|
370
|
+
if (agent.defaultReads?.length) lines.push(`Reads: ${agent.defaultReads.join(", ")}`);
|
|
371
|
+
if (agent.defaultProgress) lines.push("Progress: true");
|
|
372
|
+
if (agent.maxSubagentDepth !== undefined) lines.push(`Max subagent depth: ${agent.maxSubagentDepth}`);
|
|
373
|
+
if (agent.completionGuard === false) lines.push("Completion guard: false");
|
|
374
|
+
if (agent.systemPrompt.trim()) lines.push("", "System Prompt:", agent.systemPrompt);
|
|
375
|
+
return lines.join("\n");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function formatChainDetail(chain: ChainConfig): string {
|
|
379
|
+
const lines: string[] = [`Chain: ${chain.name} (${chain.source})`, `Path: ${chain.filePath}`, `Description: ${chain.description}`];
|
|
380
|
+
if (chain.packageName) {
|
|
381
|
+
lines.push(`Local name: ${frontmatterNameForConfig(chain)}`);
|
|
382
|
+
lines.push(`Package: ${chain.packageName}`);
|
|
383
|
+
}
|
|
384
|
+
lines.push("", "Steps:");
|
|
385
|
+
for (let i = 0; i < chain.steps.length; i++) {
|
|
386
|
+
const s = chain.steps[i]!;
|
|
387
|
+
lines.push(`${i + 1}. ${s.agent}`);
|
|
388
|
+
if (s.task.trim()) lines.push(` Task: ${s.task}`);
|
|
389
|
+
if (s.output === false) lines.push(" Output: false");
|
|
390
|
+
else if (s.output) lines.push(` Output: ${s.output}`);
|
|
391
|
+
if (s.outputMode) lines.push(` Output mode: ${s.outputMode}`);
|
|
392
|
+
if (s.reads === false) lines.push(" Reads: false");
|
|
393
|
+
else if (Array.isArray(s.reads) && s.reads.length > 0) lines.push(` Reads: ${s.reads.join(", ")}`);
|
|
394
|
+
if (s.model) lines.push(` Model: ${s.model}`);
|
|
395
|
+
if (s.skills === false) lines.push(" Skills: false");
|
|
396
|
+
else if (Array.isArray(s.skills) && s.skills.length > 0) lines.push(` Skills: ${s.skills.join(", ")}`);
|
|
397
|
+
if (s.progress !== undefined) lines.push(` Progress: ${s.progress ? "true" : "false"}`);
|
|
398
|
+
}
|
|
399
|
+
return lines.join("\n");
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function handleList(params: ManagementParams, ctx: ManagementContext): AgentToolResult<Details> {
|
|
403
|
+
const scope = normalizeListScope(params.agentScope) ?? "both";
|
|
404
|
+
const d = discoverAgentsAll(ctx.cwd);
|
|
405
|
+
const scopedAgents = allAgents(d).filter((a) => scope === "both" || a.source === "builtin" || a.source === scope).sort((a, b) => a.name.localeCompare(b.name));
|
|
406
|
+
const agents = scopedAgents.filter((a) => !a.disabled);
|
|
407
|
+
const chains = d.chains.filter((c) => scope === "both" || c.source === scope).sort((a, b) => a.name.localeCompare(b.name));
|
|
408
|
+
const lines = [
|
|
409
|
+
"Executable agents:",
|
|
410
|
+
...(agents.length
|
|
411
|
+
? agents.map((a) => `- ${a.name} (${a.source}${a.defaultContext ? `, context: ${a.defaultContext}` : ""}): ${a.description}`)
|
|
412
|
+
: ["- (none)"]),
|
|
413
|
+
"",
|
|
414
|
+
"Chains:",
|
|
415
|
+
...(chains.length ? chains.map((c) => `- ${c.name} (${c.source}): ${c.description}`) : ["- (none)"]),
|
|
416
|
+
];
|
|
417
|
+
return result(lines.join("\n"));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function handleGet(params: ManagementParams, ctx: ManagementContext): AgentToolResult<Details> {
|
|
421
|
+
if (!params.agent && !params.chainName) return result("Specify 'agent' or 'chainName' for get.", true);
|
|
422
|
+
const hasBoth = Boolean(params.agent && params.chainName);
|
|
423
|
+
const blocks: string[] = [];
|
|
424
|
+
let anyFound = false;
|
|
425
|
+
if (params.agent) {
|
|
426
|
+
const matches = findAgents(params.agent, ctx.cwd, "both");
|
|
427
|
+
if (!matches.length) {
|
|
428
|
+
const msg = `Agent '${params.agent}' not found. Available: ${availableNames(ctx.cwd, "agent").join(", ") || "none"}.`;
|
|
429
|
+
if (!hasBoth) return result(msg, true);
|
|
430
|
+
blocks.push(msg);
|
|
431
|
+
} else {
|
|
432
|
+
anyFound = true;
|
|
433
|
+
blocks.push(...matches.map(formatAgentDetail));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (params.chainName) {
|
|
437
|
+
const matches = findChains(params.chainName, ctx.cwd, "both");
|
|
438
|
+
if (!matches.length) {
|
|
439
|
+
const msg = `Chain '${params.chainName}' not found. Available: ${availableNames(ctx.cwd, "chain").join(", ") || "none"}.`;
|
|
440
|
+
if (!hasBoth) return result(msg, true);
|
|
441
|
+
blocks.push(msg);
|
|
442
|
+
} else {
|
|
443
|
+
anyFound = true;
|
|
444
|
+
blocks.push(...matches.map(formatChainDetail));
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return result(blocks.join("\n\n"), !anyFound);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function handleCreate(params: ManagementParams, ctx: ManagementContext): AgentToolResult<Details> {
|
|
451
|
+
const parsedConfig = configObject(params.config);
|
|
452
|
+
if (parsedConfig.error) return result(parsedConfig.error, true);
|
|
453
|
+
const cfg = parsedConfig.value;
|
|
454
|
+
if (!cfg) return result("config required for create.", true);
|
|
455
|
+
if (typeof cfg.name !== "string" || !cfg.name.trim()) return result("config.name is required and must be a non-empty string.", true);
|
|
456
|
+
if (typeof cfg.description !== "string" || !cfg.description.trim()) return result("config.description is required and must be a non-empty string.", true);
|
|
457
|
+
const name = sanitizeName(cfg.name);
|
|
458
|
+
if (!name) return result("config.name is invalid after sanitization. Use letters, numbers, spaces, or hyphens.", true);
|
|
459
|
+
const parsedPackage = parsePackageConfig(cfg.package);
|
|
460
|
+
if (parsedPackage.error) return result(parsedPackage.error, true);
|
|
461
|
+
const runtimeName = buildRuntimeName(name, parsedPackage.packageName);
|
|
462
|
+
const scopeRaw = cfg.scope ?? "user";
|
|
463
|
+
if (scopeRaw !== "user" && scopeRaw !== "project") return result("config.scope must be 'user' or 'project'.", true);
|
|
464
|
+
const scope = scopeRaw as ManagementScope;
|
|
465
|
+
const isChain = hasKey(cfg, "steps");
|
|
466
|
+
const d = discoverAgentsAll(ctx.cwd);
|
|
467
|
+
const targetDir = isChain
|
|
468
|
+
? scope === "user" ? d.userChainDir : d.projectChainDir ?? path.join(ctx.cwd, ".pi", "chains")
|
|
469
|
+
: scope === "user" ? d.userDir : d.projectDir ?? path.join(ctx.cwd, ".pi", "agents");
|
|
470
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
471
|
+
if (nameExistsInScope(ctx.cwd, scope, runtimeName)) return result(`Name '${runtimeName}' already exists in ${scope} scope. Use update instead.`, true);
|
|
472
|
+
const targetPath = path.join(targetDir, isChain ? `${runtimeName}.chain.md` : `${runtimeName}.md`);
|
|
473
|
+
if (fs.existsSync(targetPath)) return result(`File already exists at ${targetPath} but is not a valid ${isChain ? "chain" : "agent"} definition. Remove or rename it first.`, true);
|
|
474
|
+
const warnings: string[] = [];
|
|
475
|
+
if (!isChain && d.builtin.some((a) => a.name === runtimeName)) warnings.push(`Note: this shadows the builtin agent '${runtimeName}'.`);
|
|
476
|
+
if (isChain) {
|
|
477
|
+
const parsed = parseStepList(cfg.steps);
|
|
478
|
+
if (parsed.error) return result(parsed.error, true);
|
|
479
|
+
const chain: ChainConfig = { name: runtimeName, localName: name, packageName: parsedPackage.packageName, description: cfg.description.trim(), source: scope, filePath: targetPath, steps: parsed.steps! };
|
|
480
|
+
fs.writeFileSync(targetPath, serializeChain(chain), "utf-8");
|
|
481
|
+
const missing = unknownChainAgents(ctx.cwd, chain.steps);
|
|
482
|
+
if (missing.length) warnings.push(`Warning: chain steps reference unknown agents: ${missing.join(", ")}.`);
|
|
483
|
+
warnings.push(...chainStepWarnings(ctx, chain.steps));
|
|
484
|
+
return result([`Created chain '${runtimeName}' at ${targetPath}.`, ...warnings].join("\n"));
|
|
485
|
+
}
|
|
486
|
+
const agent: AgentConfig = {
|
|
487
|
+
name: runtimeName,
|
|
488
|
+
localName: name,
|
|
489
|
+
packageName: parsedPackage.packageName,
|
|
490
|
+
description: cfg.description.trim(),
|
|
491
|
+
source: scope,
|
|
492
|
+
filePath: targetPath,
|
|
493
|
+
systemPrompt: "",
|
|
494
|
+
systemPromptMode: defaultSystemPromptMode(name),
|
|
495
|
+
inheritProjectContext: defaultInheritProjectContext(name),
|
|
496
|
+
inheritSkills: defaultInheritSkills(),
|
|
497
|
+
};
|
|
498
|
+
const applyError = applyAgentConfig(agent, cfg);
|
|
499
|
+
if (applyError) return result(applyError, true);
|
|
500
|
+
const mw = modelWarning(ctx, agent.model);
|
|
501
|
+
if (mw) warnings.push(mw);
|
|
502
|
+
const fmw = fallbackModelsWarning(ctx, agent.fallbackModels);
|
|
503
|
+
if (fmw) warnings.push(fmw);
|
|
504
|
+
const sw = skillsWarning(ctx.cwd, agent.skills);
|
|
505
|
+
if (sw) warnings.push(sw);
|
|
506
|
+
fs.writeFileSync(targetPath, serializeAgent(agent), "utf-8");
|
|
507
|
+
return result([`Created agent '${runtimeName}' at ${targetPath}.`, ...warnings].join("\n"));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
export function handleUpdate(params: ManagementParams, ctx: ManagementContext): AgentToolResult<Details> {
|
|
511
|
+
if (!params.agent && !params.chainName) return result("Specify 'agent' or 'chainName' for update.", true);
|
|
512
|
+
if (params.agent && params.chainName) return result("Specify either 'agent' or 'chainName', not both.", true);
|
|
513
|
+
const parsedConfig = configObject(params.config);
|
|
514
|
+
if (parsedConfig.error) return result(parsedConfig.error, true);
|
|
515
|
+
const cfg = parsedConfig.value;
|
|
516
|
+
if (!cfg) return result("config required for update.", true);
|
|
517
|
+
const warnings: string[] = [];
|
|
518
|
+
if (params.agent) {
|
|
519
|
+
const scopeHint = asDisambiguationScope(params.agentScope);
|
|
520
|
+
const targetOrError = resolveTarget("agent", params.agent, findAgents(params.agent, ctx.cwd, scopeHint ?? "both"), ctx.cwd, params.agentScope);
|
|
521
|
+
if ("content" in targetOrError) return targetOrError;
|
|
522
|
+
const target = targetOrError;
|
|
523
|
+
const updated: AgentConfig = { ...target };
|
|
524
|
+
const oldName = target.name;
|
|
525
|
+
if (hasKey(cfg, "name") && (typeof cfg.name !== "string" || !cfg.name.trim())) return result("config.name must be a non-empty string when provided.", true);
|
|
526
|
+
if (hasKey(cfg, "description") && (typeof cfg.description !== "string" || !cfg.description.trim())) return result("config.description must be a non-empty string when provided.", true);
|
|
527
|
+
let newLocalName = target.localName ?? frontmatterNameForConfig(target);
|
|
528
|
+
if (hasKey(cfg, "name")) {
|
|
529
|
+
newLocalName = sanitizeName(cfg.name as string);
|
|
530
|
+
if (!newLocalName) return result("config.name is invalid after sanitization.", true);
|
|
531
|
+
}
|
|
532
|
+
let newPackageName = target.packageName;
|
|
533
|
+
if (hasKey(cfg, "package")) {
|
|
534
|
+
const parsedPackage = parsePackageConfig(cfg.package);
|
|
535
|
+
if (parsedPackage.error) return result(parsedPackage.error, true);
|
|
536
|
+
newPackageName = parsedPackage.packageName;
|
|
537
|
+
}
|
|
538
|
+
const applyError = applyAgentConfig(updated, cfg);
|
|
539
|
+
if (applyError) return result(applyError, true);
|
|
540
|
+
updated.localName = newLocalName;
|
|
541
|
+
updated.packageName = newPackageName;
|
|
542
|
+
updated.name = buildRuntimeName(newLocalName, newPackageName);
|
|
543
|
+
if (hasKey(cfg, "description")) updated.description = (cfg.description as string).trim();
|
|
544
|
+
if (hasKey(cfg, "model")) {
|
|
545
|
+
const mw = modelWarning(ctx, updated.model);
|
|
546
|
+
if (mw) warnings.push(mw);
|
|
547
|
+
}
|
|
548
|
+
if (hasKey(cfg, "fallbackModels")) {
|
|
549
|
+
const fmw = fallbackModelsWarning(ctx, updated.fallbackModels);
|
|
550
|
+
if (fmw) warnings.push(fmw);
|
|
551
|
+
}
|
|
552
|
+
if (hasKey(cfg, "skills")) {
|
|
553
|
+
const sw = skillsWarning(ctx.cwd, updated.skills);
|
|
554
|
+
if (sw) warnings.push(sw);
|
|
555
|
+
}
|
|
556
|
+
if (updated.name !== oldName) {
|
|
557
|
+
const renamed = renamePath("agent", target.filePath, updated.name, target.source, ctx.cwd);
|
|
558
|
+
if (renamed.error) return result(renamed.error, true);
|
|
559
|
+
updated.filePath = renamed.filePath!;
|
|
560
|
+
}
|
|
561
|
+
fs.writeFileSync(updated.filePath, serializeAgent(updated), "utf-8");
|
|
562
|
+
if (updated.name !== oldName) {
|
|
563
|
+
const refs = discoverAgentsAll(ctx.cwd).chains.filter((c) => c.steps.some((s) => s.agent === oldName)).map((c) => `${c.name} (${c.source})`);
|
|
564
|
+
if (refs.length) warnings.push(`Warning: chains still reference '${oldName}': ${refs.join(", ")}.`);
|
|
565
|
+
}
|
|
566
|
+
const headline = updated.name === oldName
|
|
567
|
+
? `Updated agent '${updated.name}' at ${updated.filePath}.`
|
|
568
|
+
: `Updated agent '${oldName}' to '${updated.name}' at ${updated.filePath}.`;
|
|
569
|
+
return result([headline, ...warnings].join("\n"));
|
|
570
|
+
}
|
|
571
|
+
const scopeHint = asDisambiguationScope(params.agentScope);
|
|
572
|
+
const targetOrError = resolveTarget("chain", params.chainName!, findChains(params.chainName!, ctx.cwd, scopeHint ?? "both"), ctx.cwd, params.agentScope);
|
|
573
|
+
if ("content" in targetOrError) return targetOrError;
|
|
574
|
+
const target = targetOrError;
|
|
575
|
+
const updated: ChainConfig = { ...target, steps: [...target.steps] };
|
|
576
|
+
const oldName = target.name;
|
|
577
|
+
if (hasKey(cfg, "name") && (typeof cfg.name !== "string" || !cfg.name.trim())) return result("config.name must be a non-empty string when provided.", true);
|
|
578
|
+
if (hasKey(cfg, "description") && (typeof cfg.description !== "string" || !cfg.description.trim())) return result("config.description must be a non-empty string when provided.", true);
|
|
579
|
+
let newLocalName = target.localName ?? frontmatterNameForConfig(target);
|
|
580
|
+
if (hasKey(cfg, "name")) {
|
|
581
|
+
newLocalName = sanitizeName(cfg.name as string);
|
|
582
|
+
if (!newLocalName) return result("config.name is invalid after sanitization.", true);
|
|
583
|
+
}
|
|
584
|
+
let newPackageName = target.packageName;
|
|
585
|
+
if (hasKey(cfg, "package")) {
|
|
586
|
+
const parsedPackage = parsePackageConfig(cfg.package);
|
|
587
|
+
if (parsedPackage.error) return result(parsedPackage.error, true);
|
|
588
|
+
newPackageName = parsedPackage.packageName;
|
|
589
|
+
}
|
|
590
|
+
let parsedSteps: ChainStepConfig[] | undefined;
|
|
591
|
+
if (hasKey(cfg, "steps")) {
|
|
592
|
+
const parsed = parseStepList(cfg.steps);
|
|
593
|
+
if (parsed.error) return result(parsed.error, true);
|
|
594
|
+
parsedSteps = parsed.steps!;
|
|
595
|
+
}
|
|
596
|
+
updated.localName = newLocalName;
|
|
597
|
+
updated.packageName = newPackageName;
|
|
598
|
+
updated.name = buildRuntimeName(newLocalName, newPackageName);
|
|
599
|
+
if (hasKey(cfg, "description")) updated.description = (cfg.description as string).trim();
|
|
600
|
+
if (parsedSteps) {
|
|
601
|
+
updated.steps = parsedSteps;
|
|
602
|
+
const missing = unknownChainAgents(ctx.cwd, updated.steps);
|
|
603
|
+
if (missing.length) warnings.push(`Warning: chain steps reference unknown agents: ${missing.join(", ")}.`);
|
|
604
|
+
warnings.push(...chainStepWarnings(ctx, updated.steps));
|
|
605
|
+
}
|
|
606
|
+
if (updated.name !== oldName) {
|
|
607
|
+
const renamed = renamePath("chain", target.filePath, updated.name, target.source, ctx.cwd);
|
|
608
|
+
if (renamed.error) return result(renamed.error, true);
|
|
609
|
+
updated.filePath = renamed.filePath!;
|
|
610
|
+
}
|
|
611
|
+
fs.writeFileSync(updated.filePath, serializeChain(updated), "utf-8");
|
|
612
|
+
const headline = updated.name === oldName
|
|
613
|
+
? `Updated chain '${updated.name}' at ${updated.filePath}.`
|
|
614
|
+
: `Updated chain '${oldName}' to '${updated.name}' at ${updated.filePath}.`;
|
|
615
|
+
return result([headline, ...warnings].join("\n"));
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function handleDelete(params: ManagementParams, ctx: ManagementContext): AgentToolResult<Details> {
|
|
619
|
+
if (!params.agent && !params.chainName) return result("Specify 'agent' or 'chainName' for delete.", true);
|
|
620
|
+
if (params.agent && params.chainName) return result("Specify either 'agent' or 'chainName', not both.", true);
|
|
621
|
+
const scopeHint = asDisambiguationScope(params.agentScope);
|
|
622
|
+
if (params.agent) {
|
|
623
|
+
const targetOrError = resolveTarget("agent", params.agent, findAgents(params.agent, ctx.cwd, scopeHint ?? "both"), ctx.cwd, params.agentScope);
|
|
624
|
+
if ("content" in targetOrError) return targetOrError;
|
|
625
|
+
const target = targetOrError;
|
|
626
|
+
fs.unlinkSync(target.filePath);
|
|
627
|
+
const refs = discoverAgentsAll(ctx.cwd).chains.filter((c) => c.steps.some((s) => s.agent === target.name)).map((c) => `${c.name} (${c.source})`);
|
|
628
|
+
const lines = [`Deleted agent '${target.name}' at ${target.filePath}.`];
|
|
629
|
+
if (refs.length) lines.push(`Warning: chains reference deleted agent '${target.name}': ${refs.join(", ")}.`);
|
|
630
|
+
return result(lines.join("\n"));
|
|
631
|
+
}
|
|
632
|
+
const targetOrError = resolveTarget("chain", params.chainName!, findChains(params.chainName!, ctx.cwd, scopeHint ?? "both"), ctx.cwd, params.agentScope);
|
|
633
|
+
if ("content" in targetOrError) return targetOrError;
|
|
634
|
+
const target = targetOrError;
|
|
635
|
+
fs.unlinkSync(target.filePath);
|
|
636
|
+
return result(`Deleted chain '${target.name}' at ${target.filePath}.`);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
export function handleManagementAction(action: string, params: ManagementParams, ctx: ManagementContext): AgentToolResult<Details> {
|
|
640
|
+
switch (action as ManagementAction) {
|
|
641
|
+
case "list": return handleList(params, ctx);
|
|
642
|
+
case "get": return handleGet(params, ctx);
|
|
643
|
+
case "create": return handleCreate(params, ctx);
|
|
644
|
+
case "update": return handleUpdate(params, ctx);
|
|
645
|
+
case "delete": return handleDelete(params, ctx);
|
|
646
|
+
default: return result(`Unknown action: ${action}`, true);
|
|
647
|
+
}
|
|
648
|
+
}
|