refactor: migrate embedded pi to sdk
This commit is contained in:
@@ -1,24 +1,17 @@
|
||||
// Lazy-load pi-ai model metadata so we can infer context windows when the agent
|
||||
// reports a model id. pi-coding-agent depends on @mariozechner/pi-ai, so it
|
||||
// should be present whenever CLAWDIS is installed from npm.
|
||||
// Lazy-load pi-coding-agent model metadata so we can infer context windows when
|
||||
// the agent reports a model id. This includes custom models.json entries.
|
||||
|
||||
type ModelEntry = { id: string; contextWindow?: number };
|
||||
|
||||
const MODEL_CACHE = new Map<string, number>();
|
||||
const loadPromise = (async () => {
|
||||
try {
|
||||
const piAi = (await import("@mariozechner/pi-ai")) as {
|
||||
getProviders: () => string[];
|
||||
getModels: (provider: string) => ModelEntry[];
|
||||
};
|
||||
const providers = piAi.getProviders();
|
||||
for (const p of providers) {
|
||||
const models = piAi.getModels(p) as ModelEntry[];
|
||||
for (const m of models) {
|
||||
if (!m?.id) continue;
|
||||
if (typeof m.contextWindow === "number" && m.contextWindow > 0) {
|
||||
MODEL_CACHE.set(m.id, m.contextWindow);
|
||||
}
|
||||
const { discoverModels } = await import("@mariozechner/pi-coding-agent");
|
||||
const models = discoverModels() as ModelEntry[];
|
||||
for (const m of models) {
|
||||
if (!m?.id) continue;
|
||||
if (typeof m.contextWindow === "number" && m.contextWindow > 0) {
|
||||
MODEL_CACHE.set(m.id, m.contextWindow);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
|
||||
@@ -1,28 +1,26 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import {
|
||||
Agent,
|
||||
type AgentEvent,
|
||||
type AppMessage,
|
||||
ProviderTransport,
|
||||
type ThinkingLevel,
|
||||
import type {
|
||||
AgentEvent,
|
||||
AppMessage,
|
||||
ThinkingLevel,
|
||||
} from "@mariozechner/pi-agent-core";
|
||||
import {
|
||||
type AgentToolResult,
|
||||
type Api,
|
||||
type AssistantMessage,
|
||||
getApiKey,
|
||||
getModels,
|
||||
getProviders,
|
||||
type KnownProvider,
|
||||
type Model,
|
||||
import type {
|
||||
AgentToolResult,
|
||||
Api,
|
||||
AssistantMessage,
|
||||
Model,
|
||||
} from "@mariozechner/pi-ai";
|
||||
import {
|
||||
AgentSession,
|
||||
messageTransformer,
|
||||
buildSystemPrompt,
|
||||
createAgentSession,
|
||||
defaultGetApiKey,
|
||||
findModel,
|
||||
SessionManager,
|
||||
SettingsManager,
|
||||
type Skill,
|
||||
} from "@mariozechner/pi-coding-agent";
|
||||
import type { ThinkLevel, VerboseLevel } from "../auto-reply/thinking.js";
|
||||
import {
|
||||
@@ -39,7 +37,6 @@ import {
|
||||
extractAssistantText,
|
||||
inferToolMetaFromArgs,
|
||||
} from "./pi-embedded-utils.js";
|
||||
import { getAnthropicOAuthToken } from "./pi-oauth.js";
|
||||
import {
|
||||
createClawdisCodingTools,
|
||||
sanitizeContentBlocksImages,
|
||||
@@ -49,10 +46,14 @@ import {
|
||||
applySkillEnvOverridesFromSnapshot,
|
||||
buildWorkspaceSkillSnapshot,
|
||||
loadWorkspaceSkillEntries,
|
||||
type SkillEntry,
|
||||
type SkillSnapshot,
|
||||
} from "./skills.js";
|
||||
import { buildAgentSystemPrompt } from "./system-prompt.js";
|
||||
import { loadWorkspaceBootstrapFiles } from "./workspace.js";
|
||||
import { buildAgentSystemPromptAppend } from "./system-prompt.js";
|
||||
import {
|
||||
loadWorkspaceBootstrapFiles,
|
||||
type WorkspaceBootstrapFile,
|
||||
} from "./workspace.js";
|
||||
|
||||
export type EmbeddedPiAgentMeta = {
|
||||
sessionId: string;
|
||||
@@ -106,18 +107,16 @@ function mapThinkingLevel(level?: ThinkLevel): ThinkingLevel {
|
||||
return level;
|
||||
}
|
||||
|
||||
function isKnownProvider(provider: string): provider is KnownProvider {
|
||||
return getProviders().includes(provider as KnownProvider);
|
||||
}
|
||||
|
||||
function resolveModel(
|
||||
provider: string,
|
||||
modelId: string,
|
||||
): Model<Api> | undefined {
|
||||
if (!isKnownProvider(provider)) return undefined;
|
||||
const models = getModels(provider);
|
||||
const model = models.find((m) => m.id === modelId);
|
||||
return model as Model<Api> | undefined;
|
||||
agentDir?: string,
|
||||
): { model?: Model<Api>; error?: string } {
|
||||
const result = findModel(provider, modelId, agentDir);
|
||||
return {
|
||||
model: (result.model ?? undefined) as Model<Api> | undefined,
|
||||
error: result.error ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
async function ensureSessionHeader(params: {
|
||||
@@ -148,20 +147,22 @@ async function ensureSessionHeader(params: {
|
||||
await fs.writeFile(file, `${JSON.stringify(entry)}\n`, "utf-8");
|
||||
}
|
||||
|
||||
async function getApiKeyForProvider(
|
||||
provider: string,
|
||||
): Promise<string | undefined> {
|
||||
if (provider === "anthropic") {
|
||||
const oauthToken = await getAnthropicOAuthToken();
|
||||
if (oauthToken) return oauthToken;
|
||||
const defaultApiKey = defaultGetApiKey();
|
||||
|
||||
async function getApiKeyForModel(model: { provider: string }): Promise<string> {
|
||||
if (model.provider === "anthropic") {
|
||||
const oauthEnv = process.env.ANTHROPIC_OAUTH_TOKEN;
|
||||
if (oauthEnv?.trim()) return oauthEnv.trim();
|
||||
}
|
||||
return getApiKey(provider) ?? undefined;
|
||||
const key = await defaultApiKey(model as unknown as Model<Api>);
|
||||
if (key) return key;
|
||||
throw new Error(`No API key found for provider "${model.provider}"`);
|
||||
}
|
||||
|
||||
type ContentBlock = AgentToolResult<unknown>["content"][number];
|
||||
|
||||
type ContextFile = { path: string; content: string };
|
||||
|
||||
async function sanitizeSessionMessagesImages(
|
||||
messages: AppMessage[],
|
||||
label: string,
|
||||
@@ -205,6 +206,36 @@ async function sanitizeSessionMessagesImages(
|
||||
return out;
|
||||
}
|
||||
|
||||
function buildBootstrapContextFiles(
|
||||
files: WorkspaceBootstrapFile[],
|
||||
): ContextFile[] {
|
||||
return files.map((file) => ({
|
||||
path: file.name,
|
||||
content: file.missing
|
||||
? `[MISSING] Expected at: ${file.path}`
|
||||
: (file.content ?? ""),
|
||||
}));
|
||||
}
|
||||
|
||||
function resolvePromptSkills(
|
||||
snapshot: SkillSnapshot,
|
||||
entries: SkillEntry[],
|
||||
): Skill[] {
|
||||
if (snapshot.resolvedSkills?.length) {
|
||||
return snapshot.resolvedSkills;
|
||||
}
|
||||
|
||||
const snapshotNames = snapshot.skills.map((entry) => entry.name);
|
||||
if (snapshotNames.length === 0) return [];
|
||||
|
||||
const entryByName = new Map(
|
||||
entries.map((entry) => [entry.skill.name, entry.skill]),
|
||||
);
|
||||
return snapshotNames
|
||||
.map((name) => entryByName.get(name))
|
||||
.filter((skill): skill is Skill => Boolean(skill));
|
||||
}
|
||||
|
||||
function formatAssistantErrorText(msg: AssistantMessage): string | undefined {
|
||||
if (msg.stopReason !== "error") return undefined;
|
||||
const raw = (msg.errorMessage ?? "").trim();
|
||||
@@ -259,9 +290,12 @@ export async function runEmbeddedPiAgent(params: {
|
||||
const provider =
|
||||
(params.provider ?? DEFAULT_PROVIDER).trim() || DEFAULT_PROVIDER;
|
||||
const modelId = (params.model ?? DEFAULT_MODEL).trim() || DEFAULT_MODEL;
|
||||
const model = resolveModel(provider, modelId);
|
||||
const agentDir =
|
||||
process.env.PI_CODING_AGENT_DIR ??
|
||||
path.join(os.homedir(), ".pi", "agent");
|
||||
const { model, error } = resolveModel(provider, modelId, agentDir);
|
||||
if (!model) {
|
||||
throw new Error(`Unknown model: ${provider}/${modelId}`);
|
||||
throw new Error(error ?? `Unknown model: ${provider}/${modelId}`);
|
||||
}
|
||||
|
||||
const thinkingLevel = mapThinkingLevel(params.thinkLevel);
|
||||
@@ -279,11 +313,11 @@ export async function runEmbeddedPiAgent(params: {
|
||||
let restoreSkillEnv: (() => void) | undefined;
|
||||
process.chdir(resolvedWorkspace);
|
||||
try {
|
||||
const skillEntries = params.skillsSnapshot
|
||||
? undefined
|
||||
: loadWorkspaceSkillEntries(resolvedWorkspace, {
|
||||
config: params.config,
|
||||
});
|
||||
const shouldLoadSkillEntries =
|
||||
!params.skillsSnapshot || !params.skillsSnapshot.resolvedSkills;
|
||||
const skillEntries = shouldLoadSkillEntries
|
||||
? loadWorkspaceSkillEntries(resolvedWorkspace)
|
||||
: [];
|
||||
const skillsSnapshot =
|
||||
params.skillsSnapshot ??
|
||||
buildWorkspaceSkillSnapshot(resolvedWorkspace, {
|
||||
@@ -302,60 +336,48 @@ export async function runEmbeddedPiAgent(params: {
|
||||
|
||||
const bootstrapFiles =
|
||||
await loadWorkspaceBootstrapFiles(resolvedWorkspace);
|
||||
const systemPrompt = buildAgentSystemPrompt({
|
||||
workspaceDir: resolvedWorkspace,
|
||||
bootstrapFiles: bootstrapFiles.map((f) => ({
|
||||
name: f.name,
|
||||
path: f.path,
|
||||
content: f.content,
|
||||
missing: f.missing,
|
||||
})),
|
||||
defaultThinkLevel: params.thinkLevel,
|
||||
});
|
||||
const systemPromptWithSkills = systemPrompt + skillsSnapshot.prompt;
|
||||
|
||||
const sessionManager = new SessionManager(false, params.sessionFile);
|
||||
const settingsManager = new SettingsManager();
|
||||
|
||||
const agent = new Agent({
|
||||
initialState: {
|
||||
systemPrompt: systemPromptWithSkills,
|
||||
model,
|
||||
thinkingLevel,
|
||||
// TODO(steipete): Once pi-mono publishes file-magic MIME detection in `read` image payloads,
|
||||
// remove `createClawdisCodingTools()` and use upstream `codingTools` again.
|
||||
tools: createClawdisCodingTools(),
|
||||
},
|
||||
messageTransformer,
|
||||
queueMode: settingsManager.getQueueMode(),
|
||||
transport: new ProviderTransport({
|
||||
getApiKey: async (providerName) => {
|
||||
const key = await getApiKeyForProvider(providerName);
|
||||
if (!key) {
|
||||
throw new Error(
|
||||
`No API key found for provider "${providerName}"`,
|
||||
);
|
||||
}
|
||||
return key;
|
||||
},
|
||||
const contextFiles = buildBootstrapContextFiles(bootstrapFiles);
|
||||
const promptSkills = resolvePromptSkills(skillsSnapshot, skillEntries);
|
||||
const tools = createClawdisCodingTools();
|
||||
const systemPrompt = buildSystemPrompt({
|
||||
appendPrompt: buildAgentSystemPromptAppend({
|
||||
workspaceDir: resolvedWorkspace,
|
||||
defaultThinkLevel: params.thinkLevel,
|
||||
}),
|
||||
contextFiles,
|
||||
skills: promptSkills,
|
||||
cwd: resolvedWorkspace,
|
||||
});
|
||||
|
||||
const sessionManager = SessionManager.open(params.sessionFile, agentDir);
|
||||
const settingsManager = SettingsManager.create(
|
||||
resolvedWorkspace,
|
||||
agentDir,
|
||||
);
|
||||
|
||||
const { session } = await createAgentSession({
|
||||
cwd: resolvedWorkspace,
|
||||
agentDir,
|
||||
model,
|
||||
thinkingLevel,
|
||||
systemPrompt,
|
||||
// TODO(steipete): Once pi-mono publishes file-magic MIME detection in `read` image payloads,
|
||||
// remove `createClawdisCodingTools()` and use upstream `codingTools` again.
|
||||
tools,
|
||||
sessionManager,
|
||||
settingsManager,
|
||||
getApiKey: getApiKeyForModel,
|
||||
skills: promptSkills,
|
||||
contextFiles,
|
||||
});
|
||||
|
||||
// Resume messages from the transcript if present.
|
||||
const priorRaw = sessionManager.loadSession().messages;
|
||||
const prior = await sanitizeSessionMessagesImages(
|
||||
priorRaw,
|
||||
session.messages,
|
||||
"session:history",
|
||||
);
|
||||
if (prior.length > 0) {
|
||||
agent.replaceMessages(prior);
|
||||
session.agent.replaceMessages(prior);
|
||||
}
|
||||
|
||||
const session = new AgentSession({
|
||||
agent,
|
||||
sessionManager,
|
||||
settingsManager,
|
||||
});
|
||||
const queueHandle: EmbeddedPiQueueHandle = {
|
||||
queueMessage: async (text: string) => {
|
||||
await session.queueMessage(text);
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
const PI_AGENT_DIR_ENV = "PI_CODING_AGENT_DIR";
|
||||
|
||||
type OAuthCredentials = {
|
||||
type: "oauth";
|
||||
refresh: string;
|
||||
access: string;
|
||||
/** Unix ms timestamp (already includes buffer) */
|
||||
expires: number;
|
||||
};
|
||||
|
||||
type OAuthStorageFormat = Record<string, OAuthCredentials | undefined>;
|
||||
|
||||
const ANTHROPIC_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
||||
const ANTHROPIC_TOKEN_URL = "https://console.anthropic.com/v1/oauth/token";
|
||||
|
||||
function getPiAgentDir(): string {
|
||||
const override = process.env[PI_AGENT_DIR_ENV];
|
||||
if (override?.trim()) return override.trim();
|
||||
return path.join(os.homedir(), ".pi", "agent");
|
||||
}
|
||||
|
||||
function getPiOAuthPath(): string {
|
||||
return path.join(getPiAgentDir(), "oauth.json");
|
||||
}
|
||||
|
||||
async function loadOAuthStorage(): Promise<OAuthStorageFormat> {
|
||||
const filePath = getPiOAuthPath();
|
||||
try {
|
||||
const raw = await fs.readFile(filePath, "utf-8");
|
||||
const parsed = JSON.parse(raw);
|
||||
if (parsed && typeof parsed === "object") {
|
||||
return parsed as OAuthStorageFormat;
|
||||
}
|
||||
} catch {
|
||||
// missing/invalid: treat as empty
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
async function saveOAuthStorage(storage: OAuthStorageFormat): Promise<void> {
|
||||
const filePath = getPiOAuthPath();
|
||||
await fs.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
||||
await fs.writeFile(filePath, JSON.stringify(storage, null, 2), {
|
||||
encoding: "utf-8",
|
||||
mode: 0o600,
|
||||
});
|
||||
try {
|
||||
await fs.chmod(filePath, 0o600);
|
||||
} catch {
|
||||
// best effort (windows / restricted fs)
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAnthropicToken(
|
||||
refreshToken: string,
|
||||
): Promise<OAuthCredentials> {
|
||||
const tokenResponse = await fetch(ANTHROPIC_TOKEN_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
grant_type: "refresh_token",
|
||||
client_id: ANTHROPIC_CLIENT_ID,
|
||||
refresh_token: refreshToken,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!tokenResponse.ok) {
|
||||
const error = await tokenResponse.text();
|
||||
throw new Error(`Anthropic OAuth token refresh failed: ${error}`);
|
||||
}
|
||||
|
||||
const tokenData = (await tokenResponse.json()) as {
|
||||
refresh_token: string;
|
||||
access_token: string;
|
||||
expires_in: number;
|
||||
};
|
||||
|
||||
// 5 min buffer
|
||||
const expiresAt = Date.now() + tokenData.expires_in * 1000 - 5 * 60 * 1000;
|
||||
return {
|
||||
type: "oauth",
|
||||
refresh: tokenData.refresh_token,
|
||||
access: tokenData.access_token,
|
||||
expires: expiresAt,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getAnthropicOAuthToken(): Promise<string | null> {
|
||||
const storage = await loadOAuthStorage();
|
||||
const creds = storage.anthropic;
|
||||
if (!creds) return null;
|
||||
|
||||
// If expired, attempt refresh; on failure, remove creds.
|
||||
if (Date.now() >= creds.expires) {
|
||||
try {
|
||||
const refreshed = await refreshAnthropicToken(creds.refresh);
|
||||
storage.anthropic = refreshed;
|
||||
await saveOAuthStorage(storage);
|
||||
return refreshed.access;
|
||||
} catch {
|
||||
delete storage.anthropic;
|
||||
await saveOAuthStorage(storage);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return creds.access;
|
||||
}
|
||||
@@ -51,6 +51,7 @@ export type SkillEntry = {
|
||||
export type SkillSnapshot = {
|
||||
prompt: string;
|
||||
skills: Array<{ name: string; primaryEnv?: string }>;
|
||||
resolvedSkills?: Skill[];
|
||||
};
|
||||
|
||||
function resolveBundledSkillsDir(): string | undefined {
|
||||
@@ -505,12 +506,14 @@ export function buildWorkspaceSkillSnapshot(
|
||||
): SkillSnapshot {
|
||||
const skillEntries = opts?.entries ?? loadSkillEntries(workspaceDir, opts);
|
||||
const eligible = filterSkillEntries(skillEntries, opts?.config);
|
||||
const resolvedSkills = eligible.map((entry) => entry.skill);
|
||||
return {
|
||||
prompt: formatSkillsForPrompt(eligible.map((entry) => entry.skill)),
|
||||
prompt: formatSkillsForPrompt(resolvedSkills),
|
||||
skills: eligible.map((entry) => ({
|
||||
name: entry.skill.name,
|
||||
primaryEnv: entry.clawdis?.primaryEnv,
|
||||
})),
|
||||
resolvedSkills,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,58 +1,9 @@
|
||||
import type { ThinkLevel } from "../auto-reply/thinking.js";
|
||||
|
||||
type BootstrapFile = {
|
||||
name:
|
||||
| "AGENTS.md"
|
||||
| "SOUL.md"
|
||||
| "TOOLS.md"
|
||||
| "IDENTITY.md"
|
||||
| "USER.md"
|
||||
| "BOOTSTRAP.md";
|
||||
path: string;
|
||||
content?: string;
|
||||
missing: boolean;
|
||||
};
|
||||
|
||||
function formatBootstrapFile(file: BootstrapFile): string {
|
||||
if (file.missing) {
|
||||
return `## ${file.name}\n\n[MISSING] Expected at: ${file.path}`;
|
||||
}
|
||||
return `## ${file.name}\n\n${file.content ?? ""}`.trimEnd();
|
||||
}
|
||||
|
||||
function describeBuiltInTools(): string {
|
||||
// Keep this short and stable; TOOLS.md is for user-editable external tool notes.
|
||||
return [
|
||||
"- read: read file contents",
|
||||
"- bash: run shell commands",
|
||||
"- edit: apply precise in-file replacements",
|
||||
"- write: create/overwrite files",
|
||||
"- whatsapp_login: generate a WhatsApp QR code and wait for linking",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function formatDateTime(now: Date): string {
|
||||
return now.toLocaleString("en-US", {
|
||||
weekday: "long",
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
timeZoneName: "short",
|
||||
});
|
||||
}
|
||||
|
||||
export function buildAgentSystemPrompt(params: {
|
||||
export function buildAgentSystemPromptAppend(params: {
|
||||
workspaceDir: string;
|
||||
bootstrapFiles: BootstrapFile[];
|
||||
now?: Date;
|
||||
defaultThinkLevel?: ThinkLevel;
|
||||
}) {
|
||||
const now = params.now ?? new Date();
|
||||
const boot = params.bootstrapFiles.map(formatBootstrapFile).join("\n\n");
|
||||
|
||||
const thinkHint =
|
||||
params.defaultThinkLevel && params.defaultThinkLevel !== "off"
|
||||
? `Default thinking level: ${params.defaultThinkLevel}.`
|
||||
@@ -61,17 +12,20 @@ export function buildAgentSystemPrompt(params: {
|
||||
return [
|
||||
"You are Clawd, a personal assistant running inside Clawdis.",
|
||||
"",
|
||||
"## Built-in Tools (internal)",
|
||||
"These tools are always available. TOOLS.md does not control tool availability; it is user guidance for how to use external tools.",
|
||||
describeBuiltInTools(),
|
||||
"## Tooling",
|
||||
"Pi lists the standard tools above. This runtime enables:",
|
||||
"- grep: search file contents for patterns",
|
||||
"- find: find files by glob pattern",
|
||||
"- ls: list directory contents",
|
||||
"- whatsapp_login: generate a WhatsApp QR code and wait for linking",
|
||||
"TOOLS.md does not control tool availability; it is user guidance for how to use external tools.",
|
||||
"",
|
||||
"## Workspace",
|
||||
`Your working directory is: ${params.workspaceDir}`,
|
||||
"Treat this directory as the single global workspace for file operations unless explicitly instructed otherwise.",
|
||||
"",
|
||||
"## Workspace Files (injected)",
|
||||
"These user-editable files are loaded by Clawdis and included here directly (no separate read step):",
|
||||
boot,
|
||||
"These user-editable files are loaded by Clawdis and included below in Project Context.",
|
||||
"",
|
||||
"## Messaging Safety",
|
||||
"Never send streaming/partial replies to external messaging surfaces; only final replies should be delivered there.",
|
||||
@@ -82,8 +36,6 @@ export function buildAgentSystemPrompt(params: {
|
||||
'If something needs attention, do NOT include "HEARTBEAT_OK"; reply with the alert text instead.',
|
||||
"",
|
||||
"## Runtime",
|
||||
`Current date and time: ${formatDateTime(now)}`,
|
||||
`Current working directory: ${params.workspaceDir}`,
|
||||
thinkHint,
|
||||
]
|
||||
.filter(Boolean)
|
||||
|
||||
Reference in New Issue
Block a user