chore: checkpoint pi lab rust integration work
This commit is contained in:
@@ -16,6 +16,7 @@ import { spawn } from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||
import type { Message } from "@mariozechner/pi-ai";
|
||||
import { StringEnum } from "@mariozechner/pi-ai";
|
||||
@@ -27,6 +28,22 @@ import { type AgentConfig, type AgentScope, discoverAgents } from "./agents.js";
|
||||
const MAX_PARALLEL_TASKS = 8;
|
||||
const MAX_CONCURRENCY = 4;
|
||||
const COLLAPSED_ITEM_COUNT = 10;
|
||||
const EXTENSION_DIR = path.dirname(fileURLToPath(import.meta.url));
|
||||
const MNOTE_CONTEXT_PATH = path.join(path.dirname(EXTENSION_DIR), "mnote-bridge", "mnote-context.json");
|
||||
|
||||
function readMnoteParentModel(): { provider?: string; id?: string } | undefined {
|
||||
try {
|
||||
const payload = JSON.parse(fs.readFileSync(MNOTE_CONTEXT_PATH, "utf-8")) as {
|
||||
modelProvider?: unknown;
|
||||
modelId?: unknown;
|
||||
};
|
||||
const provider = typeof payload.modelProvider === "string" ? payload.modelProvider.trim() : "";
|
||||
const id = typeof payload.modelId === "string" ? payload.modelId.trim() : "";
|
||||
return provider || id ? { provider: provider || undefined, id: id || undefined } : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function formatTokens(count: number): string {
|
||||
if (count < 1000) return count.toString();
|
||||
@@ -227,6 +244,7 @@ async function runSingleAgent(
|
||||
signal: AbortSignal | undefined,
|
||||
onUpdate: OnUpdateCallback | undefined,
|
||||
makeDetails: (results: SingleResult[]) => SubagentDetails,
|
||||
parentModel: { provider?: string; id?: string } | undefined,
|
||||
): Promise<SingleResult> {
|
||||
const agent = agents.find((a) => a.name === agentName);
|
||||
|
||||
@@ -244,7 +262,12 @@ async function runSingleAgent(
|
||||
}
|
||||
|
||||
const args: string[] = ["--mode", "json", "-p", "--no-session"];
|
||||
if (agent.model) args.push("--model", agent.model);
|
||||
if (agent.model) {
|
||||
args.push("--model", agent.model);
|
||||
} else {
|
||||
if (parentModel?.provider) args.push("--provider", parentModel.provider);
|
||||
if (parentModel?.id) args.push("--model", parentModel.id);
|
||||
}
|
||||
if (agent.tools && agent.tools.length > 0) args.push("--tools", agent.tools.join(","));
|
||||
|
||||
let tmpPromptDir: string | null = null;
|
||||
@@ -258,7 +281,7 @@ async function runSingleAgent(
|
||||
messages: [],
|
||||
stderr: "",
|
||||
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
|
||||
model: agent.model,
|
||||
model: agent.model ?? (parentModel?.provider && parentModel.id ? `${parentModel.provider}/${parentModel.id}` : undefined),
|
||||
step,
|
||||
};
|
||||
|
||||
@@ -411,12 +434,17 @@ export default function (pi: ExtensionAPI) {
|
||||
description: [
|
||||
"Delegate tasks to specialized subagents with isolated context.",
|
||||
"Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
|
||||
'Default agent scope is "user" (from ~/.pi/agent/agents).',
|
||||
'Default agent scope is "user" (bundled agents plus ~/.pi/agent/agents overrides).',
|
||||
'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
|
||||
].join(" "),
|
||||
parameters: SubagentParams,
|
||||
|
||||
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
||||
const mnoteModel = readMnoteParentModel();
|
||||
const parentModel = {
|
||||
provider: mnoteModel?.provider ?? process.env.MNOTE_PI_MODEL_PROVIDER ?? ctx.model?.provider,
|
||||
id: mnoteModel?.id ?? process.env.MNOTE_PI_MODEL_ID ?? ctx.model?.id,
|
||||
};
|
||||
const agentScope: AgentScope = params.agentScope ?? "user";
|
||||
const discovery = discoverAgents(ctx.cwd, agentScope);
|
||||
const agents = discovery.agents;
|
||||
@@ -504,10 +532,11 @@ export default function (pi: ExtensionAPI) {
|
||||
taskWithContext,
|
||||
step.cwd,
|
||||
i + 1,
|
||||
signal,
|
||||
chainUpdate,
|
||||
makeDetails("chain"),
|
||||
);
|
||||
signal,
|
||||
chainUpdate,
|
||||
makeDetails("chain"),
|
||||
parentModel,
|
||||
);
|
||||
results.push(result);
|
||||
|
||||
const isError =
|
||||
@@ -585,9 +614,10 @@ export default function (pi: ExtensionAPI) {
|
||||
allResults[index] = partial.details.results[0];
|
||||
emitParallelUpdate();
|
||||
}
|
||||
},
|
||||
makeDetails("parallel"),
|
||||
);
|
||||
},
|
||||
makeDetails("parallel"),
|
||||
parentModel,
|
||||
);
|
||||
allResults[index] = result;
|
||||
emitParallelUpdate();
|
||||
return result;
|
||||
@@ -618,10 +648,11 @@ export default function (pi: ExtensionAPI) {
|
||||
params.task,
|
||||
params.cwd,
|
||||
undefined,
|
||||
signal,
|
||||
onUpdate,
|
||||
makeDetails("single"),
|
||||
);
|
||||
signal,
|
||||
onUpdate,
|
||||
makeDetails("single"),
|
||||
parentModel,
|
||||
);
|
||||
const isError = result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
|
||||
if (isError) {
|
||||
const errorMsg =
|
||||
|
||||
Reference in New Issue
Block a user