mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 16:04:03 +00:00
Breaking changes: - Custom tools now require index.ts entry point in subdirectory (e.g., tools/mytool/index.ts instead of tools/mytool.ts) Subagent tool improvements: - Refactored to use Message[] from ai package instead of custom types - Extracted agent discovery to separate agents.ts module - Added parallel mode streaming (shows progress from all tasks) - Added turn count to usage stats footer - Removed redundant Query section from scout output Fixes: - JSON mode stdout flush: Fixed race condition where pi --mode json could exit before all output was written, causing consumers to miss final events Also: - Added signal/timeout support to pi.exec() for custom tools and hooks - Renamed pi-pods bin to avoid conflict with pi
157 lines
4.1 KiB
TypeScript
157 lines
4.1 KiB
TypeScript
/**
|
|
* Agent discovery and configuration
|
|
*/
|
|
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
|
|
export type AgentScope = "user" | "project" | "both";
|
|
|
|
export interface AgentConfig {
|
|
name: string;
|
|
description: string;
|
|
tools?: string[];
|
|
model?: string;
|
|
systemPrompt: string;
|
|
source: "user" | "project";
|
|
filePath: string;
|
|
}
|
|
|
|
export interface AgentDiscoveryResult {
|
|
agents: AgentConfig[];
|
|
projectAgentsDir: string | null;
|
|
}
|
|
|
|
function parseFrontmatter(content: string): { frontmatter: Record<string, string>; body: string } {
|
|
const frontmatter: Record<string, string> = {};
|
|
const normalized = content.replace(/\r\n/g, "\n");
|
|
|
|
if (!normalized.startsWith("---")) {
|
|
return { frontmatter, body: normalized };
|
|
}
|
|
|
|
const endIndex = normalized.indexOf("\n---", 3);
|
|
if (endIndex === -1) {
|
|
return { frontmatter, body: normalized };
|
|
}
|
|
|
|
const frontmatterBlock = normalized.slice(4, endIndex);
|
|
const body = normalized.slice(endIndex + 4).trim();
|
|
|
|
for (const line of frontmatterBlock.split("\n")) {
|
|
const match = line.match(/^([\w-]+):\s*(.*)$/);
|
|
if (match) {
|
|
let value = match[2].trim();
|
|
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
value = value.slice(1, -1);
|
|
}
|
|
frontmatter[match[1]] = value;
|
|
}
|
|
}
|
|
|
|
return { frontmatter, body };
|
|
}
|
|
|
|
function loadAgentsFromDir(dir: string, source: "user" | "project"): AgentConfig[] {
|
|
const agents: AgentConfig[] = [];
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
return agents;
|
|
}
|
|
|
|
let entries: fs.Dirent[];
|
|
try {
|
|
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
} catch {
|
|
return agents;
|
|
}
|
|
|
|
for (const entry of entries) {
|
|
if (!entry.name.endsWith(".md")) continue;
|
|
if (!entry.isFile() && !entry.isSymbolicLink()) continue;
|
|
|
|
const filePath = path.join(dir, entry.name);
|
|
let content: string;
|
|
try {
|
|
content = fs.readFileSync(filePath, "utf-8");
|
|
} catch {
|
|
continue;
|
|
}
|
|
|
|
const { frontmatter, body } = parseFrontmatter(content);
|
|
|
|
if (!frontmatter.name || !frontmatter.description) {
|
|
continue;
|
|
}
|
|
|
|
const tools = frontmatter.tools
|
|
?.split(",")
|
|
.map((t) => t.trim())
|
|
.filter(Boolean);
|
|
|
|
agents.push({
|
|
name: frontmatter.name,
|
|
description: frontmatter.description,
|
|
tools: tools && tools.length > 0 ? tools : undefined,
|
|
model: frontmatter.model,
|
|
systemPrompt: body,
|
|
source,
|
|
filePath,
|
|
});
|
|
}
|
|
|
|
return agents;
|
|
}
|
|
|
|
function isDirectory(p: string): boolean {
|
|
try {
|
|
return fs.statSync(p).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function findNearestProjectAgentsDir(cwd: string): string | null {
|
|
let currentDir = cwd;
|
|
while (true) {
|
|
const candidate = path.join(currentDir, ".pi", "agents");
|
|
if (isDirectory(candidate)) return candidate;
|
|
|
|
const parentDir = path.dirname(currentDir);
|
|
if (parentDir === currentDir) return null;
|
|
currentDir = parentDir;
|
|
}
|
|
}
|
|
|
|
export function discoverAgents(cwd: string, scope: AgentScope): AgentDiscoveryResult {
|
|
const userDir = path.join(os.homedir(), ".pi", "agent", "agents");
|
|
const projectAgentsDir = findNearestProjectAgentsDir(cwd);
|
|
|
|
const userAgents = scope === "project" ? [] : loadAgentsFromDir(userDir, "user");
|
|
const projectAgents =
|
|
scope === "user" || !projectAgentsDir ? [] : loadAgentsFromDir(projectAgentsDir, "project");
|
|
|
|
const agentMap = new Map<string, AgentConfig>();
|
|
|
|
if (scope === "both") {
|
|
for (const agent of userAgents) agentMap.set(agent.name, agent);
|
|
for (const agent of projectAgents) agentMap.set(agent.name, agent);
|
|
} else if (scope === "user") {
|
|
for (const agent of userAgents) agentMap.set(agent.name, agent);
|
|
} else {
|
|
for (const agent of projectAgents) agentMap.set(agent.name, agent);
|
|
}
|
|
|
|
return { agents: Array.from(agentMap.values()), projectAgentsDir };
|
|
}
|
|
|
|
export function formatAgentList(agents: AgentConfig[], maxItems: number): { text: string; remaining: number } {
|
|
if (agents.length === 0) return { text: "none", remaining: 0 };
|
|
const listed = agents.slice(0, maxItems);
|
|
const remaining = agents.length - listed.length;
|
|
return {
|
|
text: listed.map((a) => `${a.name} (${a.source}): ${a.description}`).join("; "),
|
|
remaining,
|
|
};
|
|
}
|