> ## Documentation Index
> Fetch the complete documentation index at: https://docs.archetype.ing/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK

> 从你自己的代码连接到 Archetype 主机。

```bash theme={null}
npm i @pairful/archetype-sdk
```

SDK 用于连接主机，本身不启动主机，也没有自己的 I/O，因此也能在浏览器中运行。

## 连接、提问、回答

```ts theme={null}
import {
  ClientConnectionService,
  ConnectorAdapter,
} from "@pairful/archetype-sdk";

const client = await ClientConnectionService.connect(
  ConnectorAdapter.webSocketConnector(url),
  { client: "my-app" },
);
const turn = await client.startTurn("main", "run the tests");
for await (const { event } of client.events(turn)) {
  if (event.type === "approval.requested")
    await client.respond(turn, event.requestId, { action: "allow" });
}
```

一轮的事件按顺序到达，从 `turn.started` 到 `turn.finished`。重新连接后，`events(turnId, after)` 会从你上次中断的地方继续。

## 连接器

| 连接器 | 适用场景 |
| - | - |
| `ConnectorAdapter.webSocketConnector(url)` | 监听 WebSocket 的主机，例如 `archetype --serve --listen` |
| `ConnectorAdapter.inProcess(server)` | 同一进程中的主机 |
| `ConnectorAdapter.lineConnector({ lines, write })` | 以换行分隔的 JSON，例如子进程的 stdio |

## 询问决策模型

```ts theme={null}
const { answers } = await client.decide(ticket, {
  urgent: { type: "yes_no", instructions: "Does this need attention now?" },
});
if (answers.urgent.probability > 0.8) notify(ticket);
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.