Files
letta-code/src/agent/import.ts
2026-01-02 23:35:40 -08:00

56 lines
1.7 KiB
TypeScript

/**
* Import an agent from an AgentFile (.af) template
*/
import { createReadStream } from "node:fs";
import { resolve } from "node:path";
import type { AgentState } from "@letta-ai/letta-client/resources/agents/agents";
import { getClient } from "./client";
import { getModelUpdateArgs } from "./model";
import { updateAgentLLMConfig } from "./modify";
export interface ImportAgentOptions {
filePath: string;
modelOverride?: string;
stripMessages?: boolean;
}
export interface ImportAgentResult {
agent: AgentState;
}
export async function importAgentFromFile(
options: ImportAgentOptions,
): Promise<ImportAgentResult> {
const client = await getClient();
const resolvedPath = resolve(options.filePath);
// Create a file stream for the API (compatible with Node.js and Bun)
const file = createReadStream(resolvedPath);
// Import the agent via API
const importResponse = await client.agents.importFile({
file: file,
strip_messages: options.stripMessages ?? true,
override_existing_tools: false,
});
if (!importResponse.agent_ids || importResponse.agent_ids.length === 0) {
throw new Error("Import failed: no agent IDs returned");
}
const agentId = importResponse.agent_ids[0] as string;
let agent = await client.agents.retrieve(agentId);
// Override model if specified
if (options.modelOverride) {
const updateArgs = getModelUpdateArgs(options.modelOverride);
await updateAgentLLMConfig(agentId, options.modelOverride, updateArgs);
// Ensure the correct memory tool is attached for the new model
const { ensureCorrectMemoryTool } = await import("../tools/toolset");
await ensureCorrectMemoryTool(agentId, options.modelOverride);
agent = await client.agents.retrieve(agentId);
}
return { agent };
}