@godspideai/cognate-runtime-bun (英语)
Bun的Cognate参考运行时间:耐用剂运行,人行后继,
工艺品、预测和可选认知,由单项工艺组成
RuntimeService。将其指向一个 SQLite 文件, 您应用程序的历史可以重新开始; 将其指向 PostgreSQL (或一个 pg0 管理的本地实例) , 多个进程安全地共享同一历史 .
何时使用此软件包 : 在服务器、工人或脚本中运行代理。 说对了
与 @godspeedai/cognate-client 改为
或用一个运输适配器在网络上服务.
安装
bun add @godspeedai/cognate-runtime-bun
要求数 阿宝 >= 1. 4.0 (默认商店使用 Bun 内置) bun:sqlite) (中文(简体) ).
快开始
import { createRuntime } from "@godspeedai/cognate-runtime-bun";
import type { AgentDefinition } from "@godspeedai/cognate-runtime-api";
const agent: AgentDefinition = {
id: "agent.order",
version: "1.0.0",
async run(_input, ctx) {
// Shared state: a snapshot, then JSON Patch deltas — clients derive the same state.
await ctx.emit("state.changed", { snapshot: { order: { id: "o-1", status: "open", total: 0 } } });
await ctx.emit("state.changed", { patch: [{ op: "replace", path: "/order/status", value: "paid" }] });
await ctx.emit("state.changed", { patch: [{ op: "replace", path: "/order/total", value: 42 }] });
return { done: true };
},
};
const caller = { actor: { id: "alice", kind: "user" }, tenant: "tenant-a" } as const;
const runtime = await createRuntime({
store: "./app.sqlite", // a durable file; use ":memory:" for an ephemeral runtime
// policy authorizes capability invocations; actions authorizes run/continuation actions.
policy: { authorize: () => ({ allow: true, policyId: "p", reason: "allowed" }) },
actions: { authorize: () => ({ allow: true, reason: "allowed" }) },
agents: [agent],
});
const started = await runtime.service.startRun(caller, {
agent: "agent.order",
input: {},
idempotencyKey: "order-1", // repeating the key returns the same run, never a duplicate
});
await runtime.idle(); // background work queued so far has been processed
const run = await runtime.service.getRun(caller, { runId: started.runId });
console.log(run.status, run.output); // completed { done: true }
await runtime.close();
持续拖延
ctx.waitFor 暂停运行直到有人解决其打开的续作。 跑步的进展
具有耐用性:它可以通过重启来坐等.
const refundAgent: AgentDefinition = {
id: "agent.refund",
version: "1.0.0",
async run(input, ctx) {
const { amount } = input as { amount: number };
const decision = (await ctx.waitFor("approve", {
type: "refund-approval",
condition: { kind: "human_approval" },
payload: { amount },
})) as { approved: boolean };
return { amount, approved: decision.approved };
},
};
const waiting = await runtime.service.startRun(caller, {
agent: "agent.refund",
input: { amount: 75 },
idempotencyKey: "refund-1",
});
// Meanwhile — from a webhook, an admin UI, a shell — list and resolve open continuations.
const [continuation] = await runtime.service.listContinuations(caller, {
runId: waiting.runId,
status: "OPEN",
});
await runtime.service.resolveContinuation(caller, {
continuationId: continuation!.id,
action: "resume",
input: { approved: true },
idempotencyKey: "approve-1",
});
跑道从上而下再入代理,接取有记录的决定,并完成. 重放
从未重复记录的工作: 每 ctx.step, (中文(简体) ), emit, (中文(简体) ), invoke, (中文(简体) ), artifact,以及 waitFor 结果是
返回这些结果而不是重新生效.
你从哪里得到的 createRuntime
回来的 Runtime 揭露:
service- 满分RuntimeService: 启动/ 获得/ 取消/ 列表运行, 可重新连接的事件流 (单位:千美元)service.events(caller, { follow: true })),继续,直接能力invoke, (中文(简体) ),getArtifact,公共投影读取,共享运行/线程状态读取和更新.store- 基础DurableStore如果您需要直接访问事件日志.kernel——嵌入能力内核.inspect(correlationId)关联的因果链 id:运行,事件,引用, 文物,观测.erase(eventId, reason, actor)——去除事件的有效载荷;衍生观点和认知指数 也放下来.rebuildCognition(), (中文(简体) ),pollApprovals(), (中文(简体) ),idle(timeoutMs?),以及close()。 。 。 .
快速启动后的显著选项 : components (能力提供内核组件
探员可以 ctx.invoke, (中文(简体) ), projections (单位:千美元)@godspeedai/cognate-projections
自动保持当前 , cognition (索引完成运行后通过 ctx.recall, (中文(简体) ),
outboxFor (在每次运行中,在解剖学上犯下了极端的放行意图), governance,以及
tickMs (背景回路间隔,默认为10ms).
限制
- 每家店有一位执行人. PostgreSQL 支持多个可持久存储的写法;
并不确定分布式经营所有权。 运行执行默认为串行 。 选入
execution.maxConcurrentRuns承认受限独立运行在一个过程中,需要 信赖execution.resourceKeys资格政策高于1。 共享资源密钥序列化; 呼叫者必须化作化名。 切勿与同一可运行的工作相竞争. - 台阶至少是一档. 一个在飞机失事期间 重新开始飞行的阶段. 保持同位素步骤,并使用本地承诺收款和外部明确调节 行动。 仅凭一个延续或许可门并不能确定实际效果.
- 投票,而不是推动,在过程内部. 背景工作(执行、恢复交付、,
续行已过期)在环行中运行
tickMs。 。 。 . - Erasure有副本.
runtime.erase从日志中去除事件的有效载荷, 衍生视图, 和认知指数——但内容被复制成延续有效载荷、一能快照, 或召回结果留下. - 公共预测由租户分担. 关键状态的登记册预测
<tenant>/<key>· ;service.readProjection只返回呼叫者的租户分区. - 拒绝意味着看不见. 阅读文物时,不允许您看到同样的返回 "没有找到"作为一个缺失的.
- 召回 优雅地贬低. 没有
cognition组合,ctx.recall返回时{ status: "degraded" }而不是失败运行.
相关软件包
@godspeedai/cognate-client- 那个 消耗这个运行时间的无头客户端; 为你获取运行状态 .@godspeedai/cognate-runtime-api—— 说 服务与代理授权合同 此套件工具.@godspeedai/cognate-store-sqlite—— 说 后面的耐用仓库store: "<path>"。 。 。 .@godspeedai/cognate-store-memory—— 说 带自己的麻黄店过来store: { store }。 。 。 .@godspeedai/cognate- SDK伞; 用于运行时间和客户端的安装.
许可证
阿帕奇 - 2.0