 | |
你其实已经(某种程度上、勉强)可以通过任务做到这一点了(请原谅这段 agent 生成的糙代码,现在是午夜,而且今天太漫长了): ```
const SyncToPostgres = defineTask<{ entryId: string }, { phase: "send" }, void>({
kind: "app.sync-postgres",
version: 1,
initial: () => ({ phase: "send" }),
phases: {
send: async (task, runtime, context) => {
const entry = await runtime.read(/* the entry */);
await postgres.upsert("messages", { id: task.input.entryId, ...entry }); // idempotent by id
await runtime.commit(() => ({ status: "terminal", outcome: { status: "completed" } }),
context);
},
},
}); await root.commit(async (tx) => {
const id = await tx.entry(AssistantEntry, answer);
await tx.createTask(SyncToPostgres, { entryId: id }, { ownership: { kind: "conversation" },
background: true });
}, context);
``` 对根对话的提交会从记录中挑出最后一个 agent 回答的 id,并持久地调度一个任务,再由该任务把它同步到 postgres。在任务内部,你按 id 取出该回答,然后以幂等方式发送给 postgres。 这里缺少的是语法糖,基本上就是一个在每次提交中运行的 hook,让发件箱写入与状态变更保持原子性,具备有序投递,可能还带有一个带游标的持久化变更流。 感谢你的反馈! |