Skip to content

SDK 快速开始

DotCraft 为 TypeScript.NETPython 提供 SDK。

安装

bash
npm install @dotcraft/sdk
bash
dotnet add package DotCraft.Sdk
bash
pip install dotcraft

1. 连接

local 会发现或启动本地 Hub,并为你的工作区确保一个 AppServer。若要连接已知的 AppServer WebSocket,则用 remote

ts
import { DotCraft } from "@dotcraft/sdk";

const dotcraft = await DotCraft.local({ workspacePath: "/path/to/workspace" });
csharp
using DotCraft.Sdk.AppServer;

await using var client = await DotCraftClient.ConnectLocalAsync(
    "/path/to/workspace",
    new DotCraftLocalClientOptions { ClientName = "my-app", ClientVersion = "0.1.0" });
python
from dotcraft import DotCraft, LocalOptions

dotcraft = await DotCraft.connect_local(LocalOptions(workspace_path="/path/to/workspace"))

2. 开启线程

线程是一段持久化对话。可以新建一个,或用 getOrCreate / get_or_create 复用某个身份已有的线程。

ts
const thread = await dotcraft.threads.start({ userId: "me" });
csharp
var thread = await client.Threads.StartAsync(
    new DotCraftThreadStartRequest(new SessionIdentity("my-app", Environment.UserName)));
python
thread = await dotcraft.threads.start(user_id="me")

3. 运行一轮

run 提交输入并等待该轮结束,返回合并后的助手回复。

ts
const result = await thread.run("Summarize this project.");
console.log(result.text);
csharp
var result = await thread.RunAsync("Summarize this project.");
Console.WriteLine(result.Text);
python
result = await thread.run("Summarize this project.")
print(result.text)

4. 流式接收事件

runStreamed 随到随发地产出归一化事件——文本增量、条目生命周期,以及终止轮次。

ts
for await (const event of thread.runStreamed("And list the open questions.")) {
  if (event.type === "agent_message_delta") {
    process.stdout.write(event.delta ?? "");
  }
}
csharp
await foreach (var runEvent in thread.RunStreamedAsync("And list the open questions."))
{
    if (runEvent.Type == DotCraftRunEventTypes.AgentMessageDelta)
    {
        Console.Write(runEvent.Params.GetProperty("delta").GetString());
    }
}
python
async for event in thread.run_streamed("And list the open questions."):
    if event.type == "agent_message_delta":
        print(event.params["delta"], end="", flush=True)

下一步

Apache License 2.0