Repository navigation
Expand file tree
/
Copy pathai-document-read.test.ts
More file actions
253 lines (244 loc) · 7.56 KB
/
Copy pathai-document-read.test.ts
File metadata and controls
253 lines (244 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { expect, it } from "vitest";
import {
cellTargetId,
documentReadPayload,
documentOutline,
textTargetId,
} from "../packages/core/src/modules/ai/document-read.js";
import {
documentReadCapabilities,
editToolInputExamples,
} from "../packages/core/src/modules/ai/capabilities.js";
import { taskStateHint } from "../apps/server/src/services/ai/context-budget.js";
const sheetId = "11111111-1111-4111-8111-111111111111";
it("gives each table cell its paragraph id and character length", () => {
const value = [
{
id: "table",
type: "table",
children: [
{
id: "row",
type: "table-row",
children: [
{
id: "cell",
type: "table-cell",
children: [
{
id: "para",
type: "paragraph",
children: [{ text: "网页钩子" }],
},
],
},
],
},
],
},
];
const outline = documentOutline("rich_text", value) as any;
expect(outline.blocks[0].rows[0].cells[0]).toMatchObject({
id: "cell",
paragraphId: "para",
textLength: 4,
});
expect(textTargetId(value, "cell")).toBe("para");
expect(textTargetId(value, "para")).toBe("para");
expect(cellTargetId(value, "para")).toBe("cell");
expect(cellTargetId(value, "cell")).toBe("cell");
expect(cellTargetId(value, "missing")).toBe("missing");
});
it("reports full UTF-16 text length even when a rich-text preview is clipped", () => {
const value = [{ id: "p", type: "paragraph", children: [
{ text: "正文".repeat(80) },
{ type: "link", children: [{ text: "📄链接" }] },
] }];
const outline = documentOutline("rich_text", value) as any;
expect(outline.blocks[0].textLength).toBe(164);
expect(outline.blocks[0].preview.length).toBeLessThan(164);
});
const spreadsheet = {
sheetOrder: [sheetId],
sheets: {
[sheetId]: {
id: sheetId,
name: "Sheet1",
rowCount: 1000,
columnCount: 100,
cellData: {
0: { 0: { v: "销量".repeat(80), f: null }, 1: { v: 10, f: null } },
1: { 1: { v: null, f: "=B1*2" } },
},
},
},
};
it("defaults document reads to an outline with stable IDs instead of native JSON", () => {
const blocks = [
{
id: "title",
type: "paragraph",
title: "h1",
children: [{ text: "项目计划正文会很长".repeat(20) }],
},
{
id: "table",
type: "table",
children: [
{
id: "row-1",
type: "table-row",
children: [
{
id: "cell-1",
type: "table-cell",
rowId: "row-1",
columnId: "col-1",
children: [{ text: "目标" }],
},
],
},
],
},
];
const result = documentReadPayload("rich_text", blocks);
expect(result.view).toBe("outline");
expect(result.nextOffset).toBeNull();
expect("content" in result).toBe(false);
expect(result.outline).toMatchObject({
blockCount: 2,
blocks: [
{ id: "title", type: "paragraph", title: "h1" },
{ id: "table", type: "table", rows: [{ id: "row-1", cells: [{ id: "cell-1" }] }] },
],
});
expect(JSON.stringify(result.outline).length).toBeLessThan(
JSON.stringify(blocks).length,
);
});
it("reads a rich_text region by blockId and full content when requested", () => {
const blocks = [
{ id: "a", type: "paragraph", children: [{ text: "保留" }] },
{ id: "b", type: "paragraph", children: [{ text: "要改的段落" }] },
];
const region = documentReadPayload("rich_text", blocks, { blockId: "b" });
expect(region.view).toBe("content");
expect(JSON.parse(region.content!)).toMatchObject({
id: "b",
children: [{ text: "要改的段落" }],
});
const missing = documentReadPayload("rich_text", blocks, { blockId: "missing" });
expect(missing.error).toContain("blockId");
const page = documentReadPayload("markdown", "# 标题\n\n" + "正文".repeat(8000), {
view: "content",
offset: 0,
limit: 120,
});
expect(page.content).toHaveLength(120);
expect(page.nextOffset).toBe(120);
});
it("keeps spreadsheet sheetOrder and presentation size on the outline", () => {
const sheet = documentReadPayload("spreadsheet", spreadsheet);
expect(sheet.outline).toMatchObject({
sheetOrder: [sheetId],
sheets: [{ sheetId, name: "Sheet1", used: { cellCount: 3 } }],
});
expect(JSON.stringify(sheet.outline)).not.toContain("销量".repeat(80));
const cells = documentReadPayload("spreadsheet", spreadsheet, {
sheetId,
startRow: 0,
endRow: 0,
});
expect(JSON.parse(cells.content!).cells["0"]["0"].v).toBe("销量".repeat(80));
expect(JSON.parse(cells.content!).cells["1"]).toBeUndefined();
const deck = {
size: { width: 12191600, height: 6858000 },
slideOrder: ["s1"],
slides: {
s1: {
name: "封面",
elementOrder: ["t1"],
elements: {
t1: {
id: "t1",
type: "text",
paragraphs: [{ children: [{ text: "项目计划" }] }],
},
},
},
},
};
const outline = documentOutline("presentation", deck);
expect(outline).toMatchObject({
size: deck.size,
slideOrder: ["s1"],
slides: [{ id: "s1", name: "封面", elements: [{ id: "t1", preview: "项目计划" }] }],
});
});
it("outlines canvas nodes and markdown headings", () => {
expect(
documentOutline("canvas", {
scene: {
children: [
{ id: "box", tag: "Rect", x: 8, y: 10, width: 40, height: 20 },
{ id: "label", tag: "Text", text: "需求评审" },
],
},
}),
).toMatchObject({
childCount: 2,
children: [
{ id: "box", tag: "Rect" },
{ id: "label", tag: "Text", text: "需求评审" },
],
});
expect(
documentOutline("markdown", "# 目标\n\n正文\n\n## 验收\n检查"),
).toMatchObject({
headings: [
{ level: 1, text: "目标" },
{ level: 2, text: "验收" },
],
});
});
it("pairs nested Canvas label UTF-16 lengths with the complete unchanged native text", () => {
const label = {
id: "orders-label",
tag: "Text",
data: { kind: "node-label" },
text: "orders\n───\nid\nuser_id\nbook_id\ncreated_at\n📄",
};
const value = { scene: { children: [{ id: "orders", tag: "Frame", children: [label] }] } };
const before = structuredClone(value);
const outlined = documentReadPayload("canvas", value).outline as any;
expect(outlined.children[0].children[0]).toMatchObject({
id: "orders-label",
parentId: "orders",
textLength: 43,
});
expect(outlined.children[0].children[0].text).not.toBe(label.text);
const full = documentReadPayload("canvas", value, { elementId: "orders-label" });
expect(JSON.parse(full.content!)).toEqual(label);
expect(value).toEqual(before);
});
it("moves first-call command shapes onto edit tools and recites edit state", () => {
expect("firstEdit" in documentReadCapabilities("rich_text", 0)).toBe(false);
expect(documentReadCapabilities("rich_text", 0).editTool).toBe("rich_text_edit");
const operations = editToolInputExamples("spreadsheet")[0]!.input
.operations as { type: string }[];
expect(operations[0]).toMatchObject({ type: "cells" });
expect(
taskStateHint({
resourceId: "doc",
seq: 4,
epochId: "e1",
applied: ["append"],
plan: { goal: "写项目计划", steps: ["起草", "保存"] },
}),
).toMatchObject({
current: { resourceId: "doc", seq: 4, epochId: "e1" },
done: ["append"],
next: "保存",
seq: 4,
});
});