中文

开发者指南 内部机制

数据模型

每块画布背后的共享 Yjs 文档,以及可移植的画布文件。

本页内容

一块画布就是一个 Yjs 文档。房间里的每个人编辑同一个文档,服务器把它保存为一个文件。类型定义在 app/src/model/types.ts、app/src/chat/model.ts 和 app/src/features/model.ts 中。

文档

openRoom 暴露了一个 Y.Doc 的六个 map:

Map 键 值
blocks 块 id Block
links 连接 id Link
notes 便利贴 id Note
chat 消息 id ChatMsg
features 功能 id Feature
meta 一个名称 一个字符串

每个值在变化时会被整体替换。例如,移动一个块,会设置一个带有新 x 和 y 的新 Block 对象。

meta map

键 值
title 画布标题。
version major.minor.patch,例如 1.4.12。
layers 层集合,是 LayerDef[] 的 JSON 字符串。缺失表示使用默认的四个层。
ai 画布的路由器设置,是 AIConfig 的 JSON 字符串。
source 画布从参考库打开时,对应的参考库条目 id。

块

ts
export interface Block {
  id: string;
  kind: Kind;
  x: number;
  y: number;
  w: number;
  h: number;
  title: string;
  subtitle?: string;
  /** The container (capability or zone) this block sits in. */
  parent?: string;
}
  • kind 是设计系统 KINDS 表中 34 个类型键之一,例如 agent 或 vectorstore。
  • x、y、w 和 h 是 100% 缩放下的世界像素。
  • parent 指向包含这个块的 capability 或 zone 类型的块。
ts
export type LinkKind = 'sync' | 'async' | 'flow';

export interface Link {
  id: string;
  from: string;
  to: string;
  kind: LinkKind;
  label?: string;
}

from 和 to 是块 id。flow 是信息流,用流动的虚线绘制。

便利贴

ts
export interface Note {
  id: string;
  text: string;
  color: StickyColor; // 'yellow' | 'pink' | 'blue' | 'green' | 'violet'
  /** World position when free; ignored while attached. */
  x: number;
  y: number;
  attach?: { type: 'block' | 'link'; id: string; dx: number; dy: number };
  author: { name: string; critter: string; color: string };
  createdAt: number;
}

钉住的便利贴与它的目标保持一个偏移量(dx、dy),所以它会跟随块或连接。

层

ts
export interface LayerDef {
  id: string;
  name: string;
  /** Height in world pixels, a whole number of grid steps. */
  height: number;
}

层按数组顺序,从 y = 0 开始向下堆叠。块属于包含其垂直中心的那个条带。

常量 值
GRID 24
NODE_W 168(7 个网格间距)
NODE_H 96(4 个网格间距)
LAYER_BAND GRID * 12,默认的条带高度
MIN_BAND GRID * 8
MAX_BAND GRID * 120

DEFAULT_LAYERS 保存四个内置的层:experience(Experience)、apps(Applications & Agents)、data(Data & Platforms)和 infra(Infrastructure)。每个层的高度都是 LAYER_BAND。resizeLayer 会在同一个事务中移动被调整条带下方的每个块,所以不会有块意外换层。

聊天消息

ts
export interface ChatMsg {
  id: string;
  at: number;
  text: string;
  author?: ChatAuthor;
  /** The Architect speaking. */
  ai?: boolean;
  status?: 'thinking' | 'streaming' | 'done' | 'error';
  model?: { id: string; provider: string; label: string };
  /** Who asked the Architect (their name), so only they get the automatic spotlight. */
  askedBy?: string;
  /** Blocks this message is about (the selection when it was sent). */
  about?: string[];
}

text 中的块引用是 [[block-id]] 形式的标记。见 Architect 协议。

功能

功能是画布之上的一个聚焦视图。它按 id 引用已有的块,从不复制它们。

ts
export interface FeatureStep {
  id: string;
  title: string;
  narration: string;
  /** The block the step starts at. */
  from: string;
  /** The block the step goes to; absent for a step that happens inside one block. */
  to?: string;
}

export interface Scenario {
  id: string;
  name: string;
  steps: FeatureStep[];
}

export interface Feature {
  id: string;
  name: string;
  description?: string;
  blockIds: string[];
  scenarios: Scenario[];
  source: 'manual' | 'ai';
  createdBy?: string;
  updatedAt: number;
}
  • 如果两个块之间存在连接,步骤会沿着这条连接前进,两个方向都可以(linkBetween)。
  • 步骤用到的每个块都是功能的成员(withStepMembers)。
  • Architect 的草稿会经过 cleanDraft。它会丢弃未知的块 id 和失去起点的步骤,并最多保留 6 个场景,每个场景最多 16 个步骤。
  • 向房间演示的状态不存储在文档中。演示者在 awareness 中发布 present(featureId、scenarioId、step),其他屏幕据此跟随。

id

newId(prefix) 返回前缀加九个随机的 36 进制字符。

前缀 用于
b_ 块
l_ 连接
n_ 便利贴
m_ 聊天消息
layer_ 在层编辑器中添加的层
f_ 功能
s_ 场景
st_ 功能步骤

画布 id 是 32 个随机字节的 URL 安全 base64:43 个字符。它位于链接的 c 查询参数中。服务器接受匹配 ^[A-Za-z0-9_-]{1,64}$ 的房间名。

版本

  • 新画布从 1.0.0 开始。
  • 一轮编辑稳定下来 1.5 秒后,服务器把 patch 数字加一。
  • 服务器从不因为自己的写入或加载文件而递增。
  • 只改变 meta.version 的事务不会触发递增。用户就是这样通过版本标签设置新的 minor 或 major 版本的。
  • 格式错误的版本会从 1.0.1 重新开始。

删除块

deleteBlock 在一个事务中运行:

  1. 被删除块里面的块会上移到它自己所在的容器。它们永远不会随它一起被删除。
  2. 钉在这个块上的便利贴会留在画布上,解除钉住,位置不变。
  3. 每个功能都会忘记这个块,以及以它为起点或终点的步骤(forgetBlock)。
  4. 从这个块出发或指向它的连接会被删除。

可移植文件

导出、导入、HTML 导出和参考库都使用 CanvasFile:

ts
export interface CanvasFile {
  format: 'archipelego';
  version: 1;
  title: string;
  /** The guided layer set; absent in files from before layers were stored. */
  layers?: LayerDef[];
  blocks: Block[];
  links: Link[];
  notes?: Note[];
  /** Feature architectures over this canvas. */
  features?: Feature[];
}

app/src/model/io.ts 中的 parseCanvasFile 会拒绝没有 format: 'archipelego' 的文件。它会丢弃类型未知或没有位置的块。功能只保留文件中存在的块和步骤。loadFile 在一个事务中替换画布内容,并且只保留两端都存在的连接。聊天不属于这个文件。