@ godspideai/ cognate- 客户端
无头的 Cognate 客户端: 启动和监视运行, 解决批准, 写入共享状态, 服务
并读取预测数——任何 RuntimeService,正在处理中或
遥控 所有客户端状态都来源于耐久事件,因此它能活下来再连接,并且可以
从零开始重建。 框架中性:无反应(反应绑定在
@godspeedai/cognate-react) (中文(简体) ).
何时使用此软件包 : 任何前端,服务, 或脚本, 谈论一个Cognate运行时间.
对于浏览器 UI, 将其与 React 绑定对齐; 对于远程运行时间, 将其交给
运输支持 RuntimeService 例如来自
@godspeedai/cognate-protocol-connect。 。 。 .
安装
bun add @godspeedai/cognate-client
要求数 阿宝 >= 1.4.0.
快开始
与进程内参考运行时间相对应的全程往返——同一流量工作不变 反对远程服务:
import { createClient } from "@godspeedai/cognate-client";
import { alice } from "@godspeedai/cognate-runtime-api/conformance"; // a ready-made caller
import type { AgentDefinition } from "@godspeedai/cognate-runtime-api";
import { createRuntime } from "@godspeedai/cognate-runtime-bun";
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 runtime = await createRuntime({
store: ":memory:",
policy: { authorize: () => ({ allow: true, policyId: "p", reason: "ok" }) },
actions: { authorize: () => ({ allow: true, reason: "ok" }) },
agents: [refundAgent],
});
// `caller` is the identity established by your transport's authenticator — never a request body.
const client = createClient({ service: runtime.service, caller: alice });
const run = await client.startRun("agent.refund", { amount: 75 });
async function waitUntil<T>(read: () => T, ok: (v: T) => boolean): Promise<T> {
for (;;) {
const value = read();
if (ok(value)) return value;
await Bun.sleep(10);
}
}
// `run(id)` is a live, reactive view derived only from the run's durable events.
const waiting = await waitUntil(
() => client.run(run.runId).get(),
(s) => s.openContinuations.length === 1,
);
const approval = waiting.openContinuations[0]!;
await client.resolveContinuation(approval.id, "resume", { approved: true });
const done = await waitUntil(
() => client.run(run.runId).get(),
(s) => s.status === "completed",
);
console.log(done.output); // { amount: 75, approved: true }
await client.close();
await runtime.close();
在适合你的地方订阅投票:
const stop = client.run(runId).subscribe((state) => {
console.log(state.status, state.text, state.state);
});
// state.text accumulates message.delta events; state.state folds state.changed
// snapshots and RFC 6902 patches; stop() unsubscribes.
客户所覆盖的
- 运行 —— 说
startRun(agent, input, { correlationId?, state? }), (中文(简体) ),cancelRun(runId, reason),以及run(runId)(单位:千美元)Readable<RunState>与status累积text,折叠共享state, (中文(简体) ),openContinuations, (中文(简体) ),artifacts, (中文(简体) ),output,以及error。 。 。 . - 共享状态, 双向 —— 说
startRun(..., { state })种子一跑的初始状态;updateState(runId, update)在运行等待( snapshot 或 JSON 补丁) 时写入, 代理 通过自己的状态来观察它.threadSharedState(threadId)编号 :updateThreadSharedState对线程中每跑一次共享的持久状态也做同样的事情. - 重新连接的事件流 —— 说
subscribe({ afterPosition?, runId? }, listener)从 断开后最后一个已交付位置:没有空白,没有重复.status报告connecting编号 :live编号 :reconnecting编号 :closed(后退默认为50 ms翻倍为2 s). - 前端能力 —— 说
provideCapability("map.focus", handler)执行打开frontend_capability继续将能力命名并解决它们以运行 代理方为invokeFrontend从@godspeedai/cognate-runtime-api。 。 。 .offerCapability(offer, handler)进一步公布这一能力,作为对 一个特工,自动续签,直到你解除注册. - 继续、预测、引用、文物 —— 说
resolveContinuation(id, "resume" | "cancel", input), (中文(简体) ),projection(name)联合国Readable随着事件的发生而更新refresh()供按需重新阅读,invoke(capability, input)通过运行时间 授权路径,以及artifact(artifactId)以获取字节. - 清理 —— 说
close()停止流并取消发布此客户端的任何远程报价 .
限制
- 衍生出,从不权威. 客户展示的都是租户耐用品
事件;运行时间及其商店拥有真理. 读取的投影可能会短暂地延后
生产它——呼叫
refresh()当你需要最新读取. - 这里没有认证 . 客户要一个
Caller在别处定居的人 运输的验证器;它从不发送证书本身. - 没有反应. 这个
Readable接口( e)get编号 :subscribe故意使框架中立; 使用情况@godspeedai/cognate-react页:1 反应19装订.
相关软件包
@godspeedai/cognate-runtime-bun—— 说 引用运行时间 此客户端正在处理中 .@godspeedai/cognate-runtime-api—— 说 页:1RuntimeService与客户订立合同时,加上符合合同要求者.@godspeedai/cognate-react- 反应19 约束此客户端.@godspeedai/cognate-protocol-connect—— 说 a 远程RuntimeService客户端,所以这个软件包在网络上运作不变.@godspeedai/cognate- SDK伞; 运行时间和客户端一起安装一个.
许可证
阿帕奇 - 2.0