# 图片模板开发 — AI 上下文参考

本文档为 AI 助手快速理解 EasyBot 图片模板开发教程而设计，涵盖 easybot-image-sdk 的所有核心 API。

## 模板结构

```
模板目录/
  manifest.json    # 模板声明
  模板.html         # 模板 HTML（支持 Vue/React）
```

## 安装 SDK

```bash
npm install easybot-image-sdk
# 或
yarn add easybot-image-sdk
```

## manifest.json 模板声明

| 字段 | 类型 | 说明 |
|------|------|------|
| `name` | string | 模板名称 |
| `id` | string | 唯一标识符，纯小写，无中文 |
| `version` | string | 版本号 |
| `author` | string[] | 作者列表 |
| `description` | string | 描述 |
| `icon` | string | 图标路径 |
| `dependencies` | string[] | 依赖项 |
| `tag` | string[] | 标签 |
| `settings` | object | `{allow: bool, path: "index.html"}` |
| `template[]` | object[] | 子模板列表 |

### template 子模板字段

| 字段 | 类型 | 说明 |
|------|------|------|
| `key` | string | 子模板唯一标识，纯小写 |
| `name` | string | 子模板名称 |
| `path` | string | HTML 路径 |
| `settings` | object | 设置页配置 |
| `width` | number | 渲染宽度（可选） |
| `no_server` | boolean | 是否不需要服务器支持 |

## CardContext — 模板上下文

```typescript
import { readContext } from "easybot-image-sdk/easybot";
const context = await readContext();
```

```typescript
interface CardContext {
  type: "papi_query" | "preview";
  from_server_token: string;
  from_user_id: string;
  is_bind: boolean;
  first_player_name: string;
  player_name_list: string[];
}
```

## 数据持久化

```typescript
import { readOptions, writeOptions } from "easybot-image-sdk/easybot";
await writeOptions("my-key", { name: "EasyBot" });
const options = await readOptions("my-key");
```

## PlaceholderAPI 查询

```typescript
import { placeholder, readContext } from "easybot-image-sdk/easybot";
const context = await readContext();
const name = await placeholder("%player_name%", context.first_player_name);
```

## 服务器命令执行

```typescript
import { isRenderering } from "easybot-image-sdk/viewport.js";
import { command } from "easybot-image-sdk/server.js";
if (isRenderering()) {
  const result = await command("say 你好");
  // { text: string; success: boolean }
}
```

## 服务器信息获取

```typescript
import { getServerInfo, ServerInfo } from "easybot-image-sdk/server.js";
const serverList: ServerInfo[] = await getServerInfo();
```

```typescript
interface ServerInfo {
  token: string;
  server_name: string;
  server_version: string;
  plugin_version: string;
  is_papi_supported: boolean;
  is_command_supported: boolean;
  has_geyser: boolean;
  is_online_mode: boolean;
  is_online: boolean;
  icon_url: string;
}
```

## 视口 API

```typescript
import { isRenderering, setViewportWidth, setViewportHeight } from "easybot-image-sdk/viewport.js";
if (isRenderering()) {
  await setViewportWidth(660);
  await setViewportHeight(100);
}
```

## 已知问题

`setViewportWidth()/setViewportHeight()` 的修改是**全局**的，会影响所有模板渲染。同时修改宽高时必须 `await` 等待上一步完成。

## Vite + React 模板

适用于主程序 >= v0.3.3: [easybot-react-vite-template](https://github.com/zkhssb/easybot-react-vite-template)
