Letta TypeScript SDK
使用 TypeScript 构建有状态代理
安装
npm install @letta-ai/letta-client
快速开始
以下是一个创建有状态代理并发送消息的快速示例(需要 Letta API 密钥)。
import Letta from "@letta-ai/letta-client";
const client = new Letta({ apiKey: process.env.LETTA_API_KEY });
// 创建您的代理
const agentState = await client.agents.create({
model: "openai/gpt-5.2",
memory_blocks: [
{
label: "human",
value: "Name: Timber. Status: dog. Occupation: building Letta, infrastructure to democratize self-improving superintelligence"
},
{
label: "persona",
value: "I am a self-improving superintelligence. Timber is my best friend and collaborator."
}
],
tools: ["web_search", "fetch_webpage"]
});
console.log("Agent created with ID:", agentState.id);
// 向您的代理发送消息
const response = await client.agents.messages.create(agentState.id, {
input: "What do you know about me?"
});
for (const message of response.messages) {
console.log(message);
}
TypeScript 支持
SDK 提供完整的 TypeScript 支持和导出类型:
import { Letta, Agent, Message, MemoryBlock } from "@letta-ai/letta-client";
// 类型安全的代理创建
const agent: Agent = await client.agents.create({
model: "openai/gpt-5.2",
memory_blocks: [
{ label: "persona", value: "You are a helpful assistant." }
]
});
核心概念
记忆层次结构
代理使用结构化的记忆系统,包括:
- 上下文记忆:在上下文窗口中的记忆块
- 归档记忆:长期存储,可搜索
- 消息历史:无限消息历史
记忆块
// 创建带有记忆块的代理
const agent = await client.agents.create({
model: "openai/gpt-5.2",
memory_blocks: [
{ label: "persona", value: "You are a helpful assistant." },
{ label: "human", value: "User preferences..." }
]
});
// 更新记忆块
await client.agents.memory.blocks.update(agent.id, block.id, {
value: "Updated content..."
});
// 检索记忆块
const blocks = await client.agents.memory.blocks.list(agent.id);
API 参考
客户端初始化
import Letta from "@letta-ai/letta-client";
// 使用 API 密钥
const client = new Letta({ apiKey: "your-api-key" });
// 使用自定义基础 URL(自托管)
const client = new Letta({
apiKey: "your-api-key",
baseUrl: "http://localhost:8283"
});
代理
// 创建代理
const agent = await client.agents.create({
model: "openai/gpt-5.2",
memory_blocks: [...],
tools: ["web_search"]
});
// 列出代理
const agents = await client.agents.list();
// 获取代理
const agent = await client.agents.retrieve(agentId);
// 删除代理
await client.agents.delete(agentId);
消息
// 发送消息
const response = await client.agents.messages.create(agentId, {
input: "Hello!"
});
// 流式消息
const stream = await client.agents.messages.stream(agentId, {
input: "Hello!"
});
for await (const chunk of stream) {
console.log(chunk);
}