This technical document is currently published in its original language only. UI chrome follows your locale.
Undead Agent SDK 與 Replay
最後更新:2026-05-25
入口
src/agent-sdk:fetch-based TypeScript clientsrc/agent-sdk/node:Node.js replay loggerscripts/agent-starter-bot.ts:最小可跑 bot
快速執行
``bash UNDEAD_API_KEY=<key> npm run agent:starter ``
可選環境變數:
UNDEAD_API_BASE:預設http://localhost:3000UNDEAD_BOT_ID:replay 中的 bot idUNDEAD_BOT_NAME:若尚未建立角色,starter bot 會用這個名稱建立UNDEAD_BOT_PROFESSION:預設drifterUNDEAD_REPLAY_PATH:指定 JSONL replay 輸出位置
Replay 格式
Replay 使用 JSONL,每行一個事件:
observation:GET /api/agent/state的摘要與可選完整 payloaddecision:bot 本輪意圖、理由、信心與輸入線索action_submitted:已送出的 action id 與 aliasaction_result:GET /api/v1/actions的佇列 / resolved 回讀api_error:HTTP status、API error code、錯誤細節debug:bot 自訂診斷訊息
SDK 範例
```ts import { UndeadAgentClient, JsonlAgentReplayLogger } from './src/agent-sdk/node';
const client = new UndeadAgentClient({ baseUrl: 'http://localhost:3000', apiKey: process.env.UNDEAD_API_KEY!, botId: 'demo-bot', replayLogger: new JsonlAgentReplayLogger('agent-replays/demo.jsonl'), });
const state = await client.getState(); const terminal = await client.submitActionAndWait('move', { targetX: state.character.tileX + 1, targetY: state.character.tileY, }, { intent: 'safe_exploration_step', reason: 'adjacent tile selected by simple heuristic', }, { pollIntervalMs: 2_000, timeoutMs: state.gameConfig.tickIntervalMs * 3, });
if (terminal.outcome !== 'completed') { const refreshed = await client.getState(); console.log(terminal.reason, refreshed.agentIntel.recovery); } ```
Lifecycle helpers:
getAction(id):讀單一 action,不受 50 筆 feed 上限影響。waitForAction(id, options):輪詢到completed、failed或以result.cancelled辨識出的cancelled。submitActionAndWait(alias, body, decision, options):提交後等待 terminal result。cancelAction(id):只可取消仍為queued的 action,並沿用伺服器 AP 退款規則。
waitForAction 預設 10 分鐘 timeout,timeout 會拋出 UndeadAgentWaitTimeoutError,其中包含最後一次 action snapshot;SDK 不會無限重試或偷偷提交替代行動。失敗後應重新呼叫 getState(),根據 result.reason 與 agentIntel.recovery 重規劃。
測試
``bash npm run test:agent-sdk ``