---
title: '@runic-labs/sdk'
description: askRunic 和 storeResult —— 面向代理的完整契约。
sidebar:
  order: 3
---
`@runic-labs/sdk` 是大多数集成唯一需要直接导入的包。它将 `@runic-labs/cache` 和 `@runic-labs/ledger` 封装在两个函数之后。

```package-install
npm i @runic-labs/sdk
```

## 模块级函数

对于常见场景——一个代理进程、默认的文件支持存储——可直接使用自由函数：

```ts
import { askRunic, storeResult } from "@runic-labs/sdk";
```

### `askRunic(decision)`

检查这个完全相同的决策是否已被处理。

| Prop | Type | Default | Description |
| - | - | - | - |
| `decision` | `Decision` | - | { intent: string; params: Record<string, unknown> } |

返回 `Promise<CachedEntry | null>` —— 未命中时为 `null`，命中时为缓存条目（并将命中记录到分类账中）。

| Prop | Type | Default | Description |
| - | - | - | - |
| `signature?` | `string` | - | The normalized signature this entry is keyed on. |
| `artifact?` | `unknown` | - | Whatever was passed to storeResult() originally. |
| `tokensSpent?` | `number` | - | What producing this artifact cost the first time. |
| `createdAt?` | `number` | - | Epoch ms when this entry was first stored. |
| `lastUsedAt?` | `number` | - | Epoch ms of the most recent hit. |
| `hitCount?` | `number` | - | Number of times this entry has been reused. |
| `stale?` | `boolean` | - | Advisory only — Runic never acts on this itself. |

### `storeResult(decision, artifact, tokensSpent)`

为某个决策记录新生成的产物，并记录其成本，以便未来命中时知道节省了多少。

| Prop | Type | Default | Description |
| - | - | - | - |
| `decision` | `Decision` | - | Same shape as askRunic's argument. |
| `artifact` | `unknown` | - | Whatever you want returned on a future cache hit. |
| `tokensSpent` | `number` | - | Agent-reported cost of producing this artifact. Runic never measures this itself. |

返回 `Promise<void>`。

## `createRunicClient(options?)`

当你需要多个独立客户端时，请改用它而非模块级函数——最常见于测试，或明确选择内存存储时：

```ts
import { createRunicClient } from "@runic-labs/sdk";
import { createCache, MemoryCacheStore } from "@runic-labs/cache";
import { createLedger, MemoryLedgerStore } from "@runic-labs/ledger";

const runic = createRunicClient({
  cache: createCache(new MemoryCacheStore()),
  ledger: createLedger(new MemoryLedgerStore()),
});

await runic.askRunic({ intent: "summarize_pr", params: { repo, pr } });
await runic.storeResult({ intent: "summarize_pr", params: { repo, pr } }, artifact, tokensUsed);
```

| Prop | Type | Default | Description |
| - | - | - | - |
| `cache?` | `Cache` | - | Defaults to a file-backed cache under .runic/ if omitted. |
| `ledger?` | `Ledger` | - | Defaults to a file-backed ledger under .runic/ if omitted. |

返回一个 `RunicClient`，其 `askRunic` / `storeResult` 形式与模块级函数相同，并限定于你传入的 `cache`/`ledger` 实例。

## 下一步

- [缓存](/cache) —— 构建自定义 `CacheStore` 实现
- [分类账](/ledger) —— 直接读取分类账摘要
- [CLI](/cli) —— 从命令行检查状态
