AI 代理資料庫
AI 代理資料庫(Agent Databases)— Turso 官方文件。
正在尋找完整的 AI 代理狀態管理解決方案嗎?歡迎了解 AgentFS —— 我們專為 AI 代理打造的專屬 SDK,在單一資料庫檔案中整合了檔案系統操作、鍵值儲存以及自動工具呼叫追蹤功能。
AI 代理需要資料庫來儲存上下文、追蹤狀態,並在跨次執行間維持長期記憶。Turso Database 提供兩種強大的架構模式:
- 嵌入式資料庫(Embedded Databases):適用於本機優先的代理處理流程
- Turso Sync:適用於需要雲端持久化與分散式協作的代理系統
嵌入式資料庫(本機優先)
適合於單一工作流程內在本機處理資料、需要離線存取能力,或處理暫時性資料且不需與其他代理共享狀態的 AI 代理。
適用場景包括本機資料管線、敏感資料的裝置端處理、開發與測試工作流程,以及單一代理的分析任務。
import { connect } from "@tursodatabase/database";
// Create a local embedded database for this agent
const db = await connect("agent-workflow.db");
// Agent can now store and query data locally
const stmt = db.prepare(`
CREATE TABLE IF NOT EXISTS steps (
id INTEGER PRIMARY KEY AUTOINCREMENT,
action TEXT NOT NULL,
result TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
stmt.run();
// Track agent workflow steps
db.prepare("INSERT INTO steps (action, result) VALUES (?, ?)").run(
"fetch_data",
"success",
);
享有零網路延遲、完整離線能力、高度資料隱私,以及極簡的單一檔案部署體驗。
資料僅限於此代理實例內部隔離使用,未內建多代理協作或跨工作階段持久化機制(除非您自行管理資料庫檔案)。
Turso Sync(雲端同步)
適合需要跨工作階段持久化記憶、與其他代理協作,或針對長執行週期工作流程要求雲端耐久性的 AI 代理。
💡 適用場景包括具備工作階段記憶的對話式代理、多代理協作系統、需要錯誤復原能力的長執行工作流程,以及向中央系統回報結果的代理。
import { connect } from "@tursodatabase/database";
// Connect to a synced database (embedded locally, synced to Turso Cloud)
const db = await connect({
path: "agent-memory.db",
url: "https://your-database.turso.io",
authToken: "your-auth-token",
sync: "full", // Bidirectional sync
});
// Agent can access data from previous sessions or other agents
const previousContext = db
.prepare("SELECT * FROM conversation_history WHERE session_id = ?")
.all("session-123");
// New data is automatically synced to cloud and other agent instances
db.prepare("INSERT INTO agent_actions (agent_id, action) VALUES (?, ?)").run(
"agent-1",
"processed_document",
);
// Manually trigger sync if needed
await db.sync();
💡 提供跨重啟的持久化記憶、透過共享狀態實現的多代理協作、雲端備份與耐久性,同時兼具高速本機讀取與背景同步能力。
同步模式: sync(雙向同步)、pull(雲端 → 代理)、push(代理 → 雲端)。
多代理架構模式
模式一:獨立隔離的代理資料庫
每個代理皆擁有專屬的嵌入式資料庫。最適合彼此獨立、不需共享狀態的代理。
// Agent 1
const agent1DB = await connect("agent-1.db");
// Agent 2
const agent2DB = await connect("agent-2.db");
適合執行各自獨立任務、無需協調且追求最高隔離性的獨立代理。
模式二:共享同步資料庫
多個代理連線至同一個同步資料庫。最適合需要彼此協作的代理系統。
// Agent 1 connects and writes tasks
const agent1DB = await connect({
path: "agent-1-local.db",
url: "https://shared-tasks.turso.io",
authToken: process.env.TURSO_TOKEN,
sync: "full",
});
agent1DB.prepare("INSERT INTO tasks (status) VALUES (?)").run("pending");
// Agent 2 connects to same database and reads tasks
const agent2DB = await connect({
path: "agent-2-local.db",
url: "https://shared-tasks.turso.io", // Same database
authToken: process.env.TURSO_TOKEN,
sync: "full",
});
const pendingTasks = agent2DB
.prepare("SELECT * FROM tasks WHERE status = ?")
.all("pending");
💡 適合透過共享任務佇列或共同知識庫協同作業的多個代理。
模式三:中心輻射架構(Hub-and-Spoke)
以中央同步資料庫為核心,工作代理負責推送執行結果,協調代理負責拉取新任務或彙整資訊。
// Worker agents push results only
const workerDB = await connect({
path: "worker-local.db",
url: "https://hub.turso.io",
authToken: process.env.WORKER_TOKEN,
sync: "push", // Only push results to cloud
});
// Coordinator agent pulls from all workers
const coordinatorDB = await connect({
path: "coordinator-local.db",
url: "https://hub.turso.io",
authToken: process.env.COORDINATOR_TOKEN,
sync: "pull", // Only pull data from cloud
});
💡 適合大量工作代理將結果單向匯流至中央協調器進行監控或彙總的架構。
單一代理專屬資料庫模式(Database-Per-Agent)
對於管理大量代理的系統,您可以透過 Platform API 為每個代理動態建立獨立的資料庫:
import { createClient } from "@tursodatabase/api";
const turso = createClient({
token: process.env.TURSO_PLATFORM_API_TOKEN,
org: process.env.TURSO_ORG_NAME,
});
// Create a dedicated database for each agent
async function provisionAgentDatabase(agentId) {
const database = await turso.databases.create(`agent-${agentId}`, {
group: "default",
});
console.log(`Database created for agent ${agentId}: ${database.Name}`);
return database;
}
在代理之間提供完全隔離、個別代理獨立擴展、更佳的安全性(單一代理遭入侵僅影響其專屬資料庫),以及代理退役時更輕鬆的資料清理。
範例
瀏覽以下範例,實際體驗代理資料庫架構模式的應用方式:
- sync-node — Node.js 環境搭配 Turso Cloud 雙向同步
- sync-wasm-vite — 瀏覽器(WASM)環境搭配 Turso Cloud 雙向同步
- database-node — Node.js 本機檔案資料庫(不含同步)
- database-wasm-vite — 瀏覽器(WASM)本機資料庫