@evandu/super-pi 0.1.274 → 0.1.275
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agents/super-pi-documents/index.ts +16 -1
- package/agents/super-pi-task/core.ts +1 -1
- package/bin/render-presentation.ps1 +35 -0
- package/desktop/main.cjs +16 -9
- package/desktop/notifications.cjs +2 -1
- package/desktop/relaunch.cjs +5 -2
- package/desktop/restart-handoff.cjs +35 -0
- package/dist/client/.super-pi-client-sources.json +26 -19
- package/dist/client/assets/CodeMirrorEditor-DJgZdigq.js +2 -0
- package/dist/client/assets/{GitDiffEditor-CpoDDQrA.css → GitDiffEditor-DIRyUxnb.css} +1 -1
- package/dist/client/assets/GitDiffEditor-lOnvlR42.js +29 -0
- package/dist/client/assets/index-BWm699nV.js +350 -0
- package/dist/client/assets/index-CnNljc5p.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/agents.d.ts +1 -1
- package/dist/server/agents.js +10 -3
- package/dist/server/application/document-service.d.ts +134 -0
- package/dist/server/application/document-service.js +9 -0
- package/dist/server/http-server.js +32 -3
- package/dist/server/presentations/brief.d.ts +49 -0
- package/dist/server/presentations/brief.js +132 -0
- package/dist/server/presentations/inspect.d.ts +15 -0
- package/dist/server/presentations/inspect.js +30 -0
- package/dist/server/presentations/native.d.ts +8 -0
- package/dist/server/presentations/native.js +33 -0
- package/dist/server/presentations/render.d.ts +16 -0
- package/dist/server/presentations/render.js +152 -0
- package/dist/server/presentations/service.d.ts +176 -0
- package/dist/server/presentations/service.js +155 -0
- package/dist/server/runtime.js +3 -2
- package/dist/shared/message-source-link.d.ts +1 -0
- package/dist/shared/message-source-link.js +4 -0
- package/dist/shared/workspace-file-preview.d.ts +1 -1
- package/dist/shared/workspace-file-preview.js +2 -0
- package/package.json +4 -1
- package/skills/presentation/SKILL.md +23 -1
- package/skills/presentation/references/spec.md +15 -0
- package/dist/client/assets/CodeMirrorEditor-DjMbNm7l.js +0 -2
- package/dist/client/assets/GitDiffEditor-Cwim9Ua1.js +0 -29
- package/dist/client/assets/index-BL9o4WZN.js +0 -350
- package/dist/client/assets/index-D1qJmoVY.css +0 -1
package/dist/client/index.html
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
<link rel="alternate icon" href="/favicon.ico" />
|
|
9
9
|
<link rel="apple-touch-icon" href="/favicon.svg" />
|
|
10
10
|
<title>Super Pi</title>
|
|
11
|
-
<script type="module" crossorigin src="/assets/index-
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-BWm699nV.js"></script>
|
|
12
12
|
<link rel="modulepreload" crossorigin href="/assets/rolldown-runtime-XKyaHtl9.js">
|
|
13
|
-
<link rel="stylesheet" crossorigin href="/assets/index-
|
|
13
|
+
<link rel="stylesheet" crossorigin href="/assets/index-CnNljc5p.css">
|
|
14
14
|
</head>
|
|
15
15
|
<body>
|
|
16
16
|
<div id="root"></div>
|
package/dist/server/agents.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ type Frontmatter = Record<string, unknown>;
|
|
|
38
38
|
*/
|
|
39
39
|
export declare const PI_TOOL_NAMES: readonly ["read", "bash", "powershell", "edit", "write", "grep", "find", "ls", "codemode", "tool_search", "list_mcp_resources", "list_mcp_resource_templates", "read_mcp_resource"];
|
|
40
40
|
/** Tools shipped by this package. Authors must be able to explicitly deny host extras. */
|
|
41
|
-
export declare const SUPER_PI_TOOL_NAMES: readonly ["task", "todowrite", "workflow", "run_async", "worktree", "scheduled_task", "project", "question", "tool_register", "tool_shortcut", "tool_remove", "memory", "skill_manage", "session_search", "session_spawn", "session_status", "session_dispatch", "session_stop", "session_report", "session_stop_subtree", "session_remove", "session_reminder", "knowledge_search", "knowledge_import", "document_read", "document_write", "web_search", "web_fetch", "feishu_doc_read", "feishu_doc_write"];
|
|
41
|
+
export declare const SUPER_PI_TOOL_NAMES: readonly ["task", "todowrite", "workflow", "run_async", "worktree", "scheduled_task", "project", "question", "tool_register", "tool_shortcut", "tool_remove", "memory", "skill_manage", "session_search", "session_spawn", "session_status", "session_dispatch", "session_stop", "session_report", "session_stop_subtree", "session_remove", "session_reminder", "knowledge_search", "knowledge_import", "document_read", "document_write", "presentation_create", "presentation_read", "presentation_edit", "presentation_render", "web_search", "web_fetch", "feishu_doc_read", "feishu_doc_write"];
|
|
42
42
|
/**
|
|
43
43
|
* Whether a name can be a real Pi tool. A name is real if it is one of `PI_TOOL_NAMES`, an MCP
|
|
44
44
|
* server tool, or a PATTERN.
|
package/dist/server/agents.js
CHANGED
|
@@ -43,7 +43,7 @@ export const SUPER_PI_TOOL_NAMES = [
|
|
|
43
43
|
"memory", "skill_manage", "session_search",
|
|
44
44
|
"session_spawn", "session_status", "session_dispatch", "session_stop", "session_report", "session_stop_subtree", "session_remove", "session_reminder",
|
|
45
45
|
"knowledge_search", "knowledge_import",
|
|
46
|
-
"document_read", "document_write", "web_search", "web_fetch", "feishu_doc_read", "feishu_doc_write",
|
|
46
|
+
"document_read", "document_write", "presentation_create", "presentation_read", "presentation_edit", "presentation_render", "web_search", "web_fetch", "feishu_doc_read", "feishu_doc_write",
|
|
47
47
|
];
|
|
48
48
|
const KNOWN_TOOL_NAMES = [...PI_TOOL_NAMES, ...SUPER_PI_TOOL_NAMES];
|
|
49
49
|
const PI_TOOL_NAME_SET = new Set(KNOWN_TOOL_NAMES);
|
|
@@ -249,6 +249,13 @@ function unquote(value) {
|
|
|
249
249
|
return value.slice(1, -1);
|
|
250
250
|
return value;
|
|
251
251
|
}
|
|
252
|
+
/** New artifact writers must not widen an existing read-only or document-write-denied role. */
|
|
253
|
+
function presentationWriteDenials(tools, options) {
|
|
254
|
+
if (!tools || typeof tools !== "object")
|
|
255
|
+
return [];
|
|
256
|
+
const readOnly = tools["*"] === false && !["write", "edit", "bash", "powershell", "document_write"].some(name => tools[name] === true);
|
|
257
|
+
return ["presentation_create", "presentation_edit"].filter(name => options.extraAllowedTools?.includes(name) && tools[name] !== true && (tools.document_write === false || readOnly));
|
|
258
|
+
}
|
|
252
259
|
/**
|
|
253
260
|
* The Pi CLI flags an agent's `tools` map becomes. Returned flat so the caller can push them into the
|
|
254
261
|
* spawn argv in order, and so a test can assert on the exact list.
|
|
@@ -268,7 +275,7 @@ function unquote(value) {
|
|
|
268
275
|
* see a schema for it. Naming a tool on the allowlist also activates it (agent-session.ts:3569-3574).
|
|
269
276
|
*/
|
|
270
277
|
export function toolFlags(tools, options = {}) {
|
|
271
|
-
const denied = new Set((options.denyTools ?? []).filter((name) => name !== ""));
|
|
278
|
+
const denied = new Set([...(options.denyTools ?? []), ...presentationWriteDenials(tools, options)].filter((name) => name !== ""));
|
|
272
279
|
// An agent with no `tools:` map runs on Pi's own default set, so there is nothing to translate - but a
|
|
273
280
|
// DENY inherited from a parent still has to become an explicit exclusion, because there is no allowlist
|
|
274
281
|
// here to drop the name from.
|
|
@@ -330,7 +337,7 @@ export function deniesAllTools(tools) {
|
|
|
330
337
|
* do for every shape of `tools:` map.
|
|
331
338
|
*/
|
|
332
339
|
export function toolSelection(tools, options = {}) {
|
|
333
|
-
const denied = new Set((options.denyTools ?? []).filter((name) => name !== ""));
|
|
340
|
+
const denied = new Set([...(options.denyTools ?? []), ...presentationWriteDenials(tools, options)].filter((name) => name !== ""));
|
|
334
341
|
// No map at all is Pi's own default set. An inherited deny still has to become an exclusion, because
|
|
335
342
|
// there is no allowlist here to drop the name from.
|
|
336
343
|
if (!tools || typeof tools !== "object") {
|
|
@@ -22,6 +22,140 @@ export declare const MAX_DOCUMENT_REQUEST_BYTES: number;
|
|
|
22
22
|
export declare class DocumentService {
|
|
23
23
|
private readonly store;
|
|
24
24
|
constructor(store: Store);
|
|
25
|
+
presentation(conversationId: string, body: Record<string, unknown>): Promise<{
|
|
26
|
+
version: 1;
|
|
27
|
+
etag: string;
|
|
28
|
+
operation: string;
|
|
29
|
+
sourceHash: string;
|
|
30
|
+
title: string;
|
|
31
|
+
verification: "layout-only" | "native-office";
|
|
32
|
+
renderer: string;
|
|
33
|
+
nativeReason?: string;
|
|
34
|
+
inspection: Awaited<ReturnType<typeof import("../presentations/inspect.js").inspectPptx>>;
|
|
35
|
+
contactSheets: {
|
|
36
|
+
asset: string;
|
|
37
|
+
hash: string;
|
|
38
|
+
}[];
|
|
39
|
+
warnings: {
|
|
40
|
+
slideId: string;
|
|
41
|
+
elementId: string;
|
|
42
|
+
message: string;
|
|
43
|
+
}[];
|
|
44
|
+
baseline?: {
|
|
45
|
+
path: string;
|
|
46
|
+
etag: string;
|
|
47
|
+
};
|
|
48
|
+
slides: {
|
|
49
|
+
id: string;
|
|
50
|
+
title: string;
|
|
51
|
+
notes: string;
|
|
52
|
+
asset: string;
|
|
53
|
+
hash: string;
|
|
54
|
+
}[];
|
|
55
|
+
source: import("../presentations/brief.js").PresentationSource;
|
|
56
|
+
path: string;
|
|
57
|
+
contactSheetPaths: string[];
|
|
58
|
+
link: string;
|
|
59
|
+
} | {
|
|
60
|
+
version: 1;
|
|
61
|
+
etag: string;
|
|
62
|
+
operation: string;
|
|
63
|
+
sourceHash: string;
|
|
64
|
+
title: string;
|
|
65
|
+
verification: "layout-only" | "native-office";
|
|
66
|
+
renderer: string;
|
|
67
|
+
nativeReason?: string;
|
|
68
|
+
inspection: Awaited<ReturnType<typeof import("../presentations/inspect.js").inspectPptx>>;
|
|
69
|
+
contactSheets: {
|
|
70
|
+
asset: string;
|
|
71
|
+
hash: string;
|
|
72
|
+
}[];
|
|
73
|
+
slides: {
|
|
74
|
+
id: string;
|
|
75
|
+
title: string;
|
|
76
|
+
notes: string;
|
|
77
|
+
asset: string;
|
|
78
|
+
hash: string;
|
|
79
|
+
}[];
|
|
80
|
+
warnings: {
|
|
81
|
+
slideId: string;
|
|
82
|
+
elementId: string;
|
|
83
|
+
message: string;
|
|
84
|
+
}[];
|
|
85
|
+
baseline?: {
|
|
86
|
+
path: string;
|
|
87
|
+
etag: string;
|
|
88
|
+
};
|
|
89
|
+
path: string;
|
|
90
|
+
contactSheetPaths: string[];
|
|
91
|
+
reused: boolean;
|
|
92
|
+
link: string;
|
|
93
|
+
} | {
|
|
94
|
+
version: 1;
|
|
95
|
+
etag: string;
|
|
96
|
+
operation: string;
|
|
97
|
+
sourceHash: string;
|
|
98
|
+
title: string;
|
|
99
|
+
verification: "layout-only" | "native-office";
|
|
100
|
+
renderer: string;
|
|
101
|
+
nativeReason?: string;
|
|
102
|
+
inspection: Awaited<ReturnType<typeof import("../presentations/inspect.js").inspectPptx>>;
|
|
103
|
+
contactSheets: {
|
|
104
|
+
asset: string;
|
|
105
|
+
hash: string;
|
|
106
|
+
}[];
|
|
107
|
+
slides: {
|
|
108
|
+
id: string;
|
|
109
|
+
title: string;
|
|
110
|
+
notes: string;
|
|
111
|
+
asset: string;
|
|
112
|
+
hash: string;
|
|
113
|
+
}[];
|
|
114
|
+
warnings: {
|
|
115
|
+
slideId: string;
|
|
116
|
+
elementId: string;
|
|
117
|
+
message: string;
|
|
118
|
+
}[];
|
|
119
|
+
baseline?: {
|
|
120
|
+
path: string;
|
|
121
|
+
etag: string;
|
|
122
|
+
};
|
|
123
|
+
path: string;
|
|
124
|
+
bytes: number;
|
|
125
|
+
contactSheetPaths: string[];
|
|
126
|
+
link: string;
|
|
127
|
+
} | {
|
|
128
|
+
path: string;
|
|
129
|
+
etag: string;
|
|
130
|
+
verification: "layout-only" | "native-office";
|
|
131
|
+
renderer: string;
|
|
132
|
+
nativeReason: string | undefined;
|
|
133
|
+
inspection: {
|
|
134
|
+
slides: number;
|
|
135
|
+
notes: number;
|
|
136
|
+
nativeCharts: number;
|
|
137
|
+
nativeTables: number;
|
|
138
|
+
editableTextShapes: number;
|
|
139
|
+
embeddedWorkbooks: number;
|
|
140
|
+
canvasInches: {
|
|
141
|
+
width: number;
|
|
142
|
+
height: number;
|
|
143
|
+
} | undefined;
|
|
144
|
+
relationships: string;
|
|
145
|
+
verification: string;
|
|
146
|
+
};
|
|
147
|
+
warnings: {
|
|
148
|
+
slideId: string;
|
|
149
|
+
elementId: string;
|
|
150
|
+
message: string;
|
|
151
|
+
}[];
|
|
152
|
+
sheet: number;
|
|
153
|
+
totalSheets: number;
|
|
154
|
+
images: {
|
|
155
|
+
mimeType: string;
|
|
156
|
+
data: string;
|
|
157
|
+
}[];
|
|
158
|
+
}>;
|
|
25
159
|
/**
|
|
26
160
|
* The workspace a conversation works in, taken from the store.
|
|
27
161
|
*
|
|
@@ -2,6 +2,8 @@ import { mkdir, stat, writeFile } from "node:fs/promises";
|
|
|
2
2
|
import { dirname, extname } from "node:path";
|
|
3
3
|
import { buildDocument, documentFormatOf, DocumentError, extractText } from "../documents.js";
|
|
4
4
|
import { fileEtag, resolveInsideRoot } from "../files.js";
|
|
5
|
+
import { resolveFileCwd } from "../session-cwd.js";
|
|
6
|
+
import { presentationTool } from "../presentations/service.js";
|
|
5
7
|
/**
|
|
6
8
|
* The largest request the document endpoint accepts.
|
|
7
9
|
*
|
|
@@ -26,6 +28,13 @@ export class DocumentService {
|
|
|
26
28
|
constructor(store) {
|
|
27
29
|
this.store = store;
|
|
28
30
|
}
|
|
31
|
+
async presentation(conversationId, body) {
|
|
32
|
+
const conversation = this.store.snapshot().conversations.find(item => item.id === conversationId);
|
|
33
|
+
if (!conversation?.workspaceId)
|
|
34
|
+
throw new Error("会话或工作区不存在");
|
|
35
|
+
const root = await resolveFileCwd(this.workspacePath(conversationId), conversation.cwd ?? "");
|
|
36
|
+
return presentationTool(root, body);
|
|
37
|
+
}
|
|
29
38
|
/**
|
|
30
39
|
* The workspace a conversation works in, taken from the store.
|
|
31
40
|
*
|
|
@@ -38,6 +38,7 @@ import { updateIsActive } from "../shared/update-status.js";
|
|
|
38
38
|
import { FileIndex, refreshWorkspaceIndex } from "./file-index.js";
|
|
39
39
|
import { ensureNameIndex, QUICK_OPEN_PAGE_SIZE, recordWorkspacePath } from "./file-name-index.js";
|
|
40
40
|
import { resolveFileCwd, sessionScopeKey, workspacePathFromSession } from "./session-cwd.js";
|
|
41
|
+
import { presentationAsset, readPresentation } from "./presentations/service.js";
|
|
41
42
|
import { explorerInternalPath, explorerPath, inspectWorkspaceUpload, listWorkspaceTrash, MAX_WORKSPACE_UPLOAD_BYTES, mutateWorkspaceTree, serializeWorkspaceWrite, uploadWorkspaceFile } from "./workspace-file-operations.js";
|
|
42
43
|
import { readWorkspaceDocumentPreview, respondWithWorkspaceFile } from "./workspace-file-response.js";
|
|
43
44
|
import { createExplicitFileOpen } from "./explicit-file-open.js";
|
|
@@ -1363,9 +1364,11 @@ export function createHttpServer(options) {
|
|
|
1363
1364
|
response.writeHead(403, { "content-type": "application/json; charset=utf-8" }).end(JSON.stringify({ error: verdict.message }));
|
|
1364
1365
|
return;
|
|
1365
1366
|
}
|
|
1366
|
-
const result = body.action === "
|
|
1367
|
-
? await context.documentService.
|
|
1368
|
-
:
|
|
1367
|
+
const result = typeof body.action === "string" && body.action.startsWith("presentation_")
|
|
1368
|
+
? await context.documentService.presentation(conversationId, body)
|
|
1369
|
+
: body.action === "write"
|
|
1370
|
+
? await context.documentService.write(conversationId, String(body.path ?? ""), String(body.content ?? ""), typeof body.title === "string" ? body.title : undefined, typeof body.baseEtag === "string" ? body.baseEtag : undefined)
|
|
1371
|
+
: await context.documentService.read(conversationId, String(body.path ?? ""));
|
|
1369
1372
|
response.writeHead(200, { "content-type": "application/json; charset=utf-8" }).end(JSON.stringify(result));
|
|
1370
1373
|
}
|
|
1371
1374
|
catch (error) {
|
|
@@ -1373,6 +1376,32 @@ export function createHttpServer(options) {
|
|
|
1373
1376
|
}
|
|
1374
1377
|
return;
|
|
1375
1378
|
}
|
|
1379
|
+
if (url.pathname === "/api/presentations" || url.pathname === "/api/presentations/asset") {
|
|
1380
|
+
if (request.method !== "GET") {
|
|
1381
|
+
json(405, { error: "不支持此操作" });
|
|
1382
|
+
return;
|
|
1383
|
+
}
|
|
1384
|
+
try {
|
|
1385
|
+
const workspaceId = url.searchParams.get("workspaceId")?.trim();
|
|
1386
|
+
const workspace = workspaceId ? store.workspace(workspaceId) : undefined;
|
|
1387
|
+
if (!workspace || workspace.closedAt)
|
|
1388
|
+
throw new Error("请先选择有效工作区");
|
|
1389
|
+
const root = await resolveFileCwd(workspace.path, url.searchParams.get("cwd"));
|
|
1390
|
+
const path = url.searchParams.get("path");
|
|
1391
|
+
if (!path)
|
|
1392
|
+
throw new Error("缺少 path");
|
|
1393
|
+
if (url.pathname.endsWith("/asset")) {
|
|
1394
|
+
const asset = await presentationAsset(root, path, url.searchParams.get("asset") ?? "");
|
|
1395
|
+
response.writeHead(200, { "content-type": asset.type, "cache-control": "no-store", "x-content-type-options": "nosniff", "content-security-policy": "default-src 'none'; style-src 'none'; sandbox" }).end(asset.bytes);
|
|
1396
|
+
}
|
|
1397
|
+
else
|
|
1398
|
+
json(200, await readPresentation(root, path));
|
|
1399
|
+
}
|
|
1400
|
+
catch (error) {
|
|
1401
|
+
json(400, { error: error instanceof Error ? error.message : String(error) });
|
|
1402
|
+
}
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1376
1405
|
if (url.pathname === "/api/tavily/settings") {
|
|
1377
1406
|
const origin = request.headers.origin;
|
|
1378
1407
|
const fetchSite = request.headers["sec-fetch-site"];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type PresentationSpec } from "./schema.js";
|
|
2
|
+
export type SlideBrief = {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
layout?: "cover" | "body" | "comparison" | "chart" | "table" | "timeline" | "metrics";
|
|
6
|
+
subtitle?: string;
|
|
7
|
+
bullets?: string[];
|
|
8
|
+
columns?: {
|
|
9
|
+
title: string;
|
|
10
|
+
bullets: string[];
|
|
11
|
+
}[];
|
|
12
|
+
chart?: {
|
|
13
|
+
type: "bar" | "column" | "line" | "area" | "pie" | "doughnut";
|
|
14
|
+
categories: string[];
|
|
15
|
+
series: {
|
|
16
|
+
name: string;
|
|
17
|
+
values: number[];
|
|
18
|
+
}[];
|
|
19
|
+
};
|
|
20
|
+
table?: (string | number | boolean | null)[][];
|
|
21
|
+
steps?: {
|
|
22
|
+
title: string;
|
|
23
|
+
body: string;
|
|
24
|
+
}[];
|
|
25
|
+
metrics?: {
|
|
26
|
+
label: string;
|
|
27
|
+
value: string;
|
|
28
|
+
detail?: string;
|
|
29
|
+
}[];
|
|
30
|
+
notes?: string;
|
|
31
|
+
source?: string;
|
|
32
|
+
};
|
|
33
|
+
export type PresentationBrief = {
|
|
34
|
+
title: string;
|
|
35
|
+
author?: string;
|
|
36
|
+
data?: Record<string, unknown>;
|
|
37
|
+
slides: SlideBrief[];
|
|
38
|
+
};
|
|
39
|
+
export type PresentationSource = {
|
|
40
|
+
kind: "brief";
|
|
41
|
+
brief: PresentationBrief;
|
|
42
|
+
} | {
|
|
43
|
+
kind: "spec";
|
|
44
|
+
spec: PresentationSpec;
|
|
45
|
+
};
|
|
46
|
+
export declare function safeJson(text: string): any;
|
|
47
|
+
export declare function compile(source: PresentationSource): PresentationSpec;
|
|
48
|
+
/** Stable slide IDs make a local revision independent of pagination. */
|
|
49
|
+
export declare function patchSource(source: PresentationSource, patch: any): PresentationSource;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { validatePresentationSpec } from "./schema.js";
|
|
2
|
+
const object = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
3
|
+
const forbidden = new Set(["__proto__", "prototype", "constructor"]);
|
|
4
|
+
export function safeJson(text) {
|
|
5
|
+
return JSON.parse(text, (key, value) => { if (forbidden.has(key))
|
|
6
|
+
throw new Error("不支持的 JSON 属性"); return value; });
|
|
7
|
+
}
|
|
8
|
+
function bind(value, data, depth = 0) {
|
|
9
|
+
if (depth > 24)
|
|
10
|
+
throw new Error("数据引用或对象嵌套过深");
|
|
11
|
+
const lookup = (key) => {
|
|
12
|
+
let found = data;
|
|
13
|
+
for (const part of key.split(".")) {
|
|
14
|
+
if (forbidden.has(part) || (!object(found) && !Array.isArray(found)) || !Object.hasOwn(found, part))
|
|
15
|
+
throw new Error(`数据引用不存在:${key}`);
|
|
16
|
+
found = found[part];
|
|
17
|
+
}
|
|
18
|
+
return found;
|
|
19
|
+
};
|
|
20
|
+
if (object(value) && Object.keys(value).length === 1 && typeof value.$ref === "string")
|
|
21
|
+
return bind(lookup(value.$ref), data, depth + 1);
|
|
22
|
+
if (typeof value === "string")
|
|
23
|
+
return value.replace(/\{\{([\w.-]+)\}\}/g, (_match, key) => {
|
|
24
|
+
const found = lookup(key);
|
|
25
|
+
if (!["string", "number", "boolean"].includes(typeof found))
|
|
26
|
+
throw new Error(`数据引用必须为标量:${key}`);
|
|
27
|
+
return String(found);
|
|
28
|
+
});
|
|
29
|
+
if (Array.isArray(value))
|
|
30
|
+
return value.map(item => bind(item, data, depth + 1));
|
|
31
|
+
if (object(value))
|
|
32
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, bind(item, data, depth + 1)]));
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
export function compile(source) {
|
|
36
|
+
let spec;
|
|
37
|
+
if (source.kind === "spec")
|
|
38
|
+
spec = source.spec;
|
|
39
|
+
else {
|
|
40
|
+
const brief = source.brief;
|
|
41
|
+
if (!object(brief) || typeof brief.title !== "string" || !Array.isArray(brief.slides) || !brief.slides.length || brief.slides.length > 200)
|
|
42
|
+
throw new Error("brief 需要 title 和 1–200 个 slides");
|
|
43
|
+
spec = { version: 1, canvas: { width: 13.333, height: 7.5 }, metadata: { title: brief.title, author: brief.author, language: "zh-CN" }, slides: [] };
|
|
44
|
+
for (const [index, original] of brief.slides.entries()) {
|
|
45
|
+
const slide = bind(original, brief.data ?? {});
|
|
46
|
+
if (!object(slide) || typeof slide.id !== "string" || typeof slide.title !== "string")
|
|
47
|
+
throw new Error(`第 ${index + 1} 页需要 id/title`);
|
|
48
|
+
const elements = [];
|
|
49
|
+
const text = (key, content, x, y, width, height, size = 22, bold = false, color = "233047") => {
|
|
50
|
+
if (typeof content !== "string")
|
|
51
|
+
throw new Error(`第 ${index + 1} 页 ${key} 必须为文字`);
|
|
52
|
+
elements.push({ id: `${slide.id}-${key}`, type: "text", x, y, width, height, text: content, fontSize: size, fontFamily: "Noto Sans SC", bold, color });
|
|
53
|
+
};
|
|
54
|
+
text("number", `${String(index + 1).padStart(2, "0")} / ${brief.slides.length}`, .6, 7.05, 2, .22, 10, false, "52627A");
|
|
55
|
+
text("title", slide.title, .65, slide.layout === "cover" ? 1.6 : .5, 12, slide.layout === "cover" ? 1.6 : .9, slide.layout === "cover" ? 36 : 28, true);
|
|
56
|
+
if (slide.source)
|
|
57
|
+
text("source", slide.source, 2.7, 7.02, 10, .3, 10, false, "52627A");
|
|
58
|
+
const bullets = (items, x, y, width, height, key) => text(key, items.map(item => `• ${item}`).join("\n\n"), x, y, width, height, 22);
|
|
59
|
+
const layout = slide.layout ?? (slide.chart ? "chart" : slide.table ? "table" : "body");
|
|
60
|
+
if (layout === "cover")
|
|
61
|
+
text("subtitle", slide.subtitle ?? "", .7, 3.7, 11.9, 1.5, 24, false, "52627A");
|
|
62
|
+
else if (layout === "body")
|
|
63
|
+
bullets(slide.bullets ?? [slide.subtitle ?? ""], .75, 1.75, 11.8, 4.85, "body");
|
|
64
|
+
else if (layout === "comparison") {
|
|
65
|
+
if (!slide.columns?.length || slide.columns.length > 3)
|
|
66
|
+
throw new Error("comparison 需要 1–3 个 columns");
|
|
67
|
+
const w = 12 / slide.columns.length;
|
|
68
|
+
slide.columns.forEach((col, i) => { text(`col-${i}`, col.title, .7 + w * i, 1.65, w - .35, .7, 24, true, "245BA8"); bullets(col.bullets, .7 + w * i, 2.6, w - .4, 3.9, `body-${i}`); });
|
|
69
|
+
}
|
|
70
|
+
else if (layout === "chart") {
|
|
71
|
+
if (!slide.chart)
|
|
72
|
+
throw new Error("chart 页面缺少 chart 数据");
|
|
73
|
+
elements.push({ id: `${slide.id}-chart`, type: "chart", x: .8, y: 1.6, width: 11.7, height: slide.subtitle ? 4.5 : 5.1, chart: slide.chart.type, categories: slide.chart.categories, series: slide.chart.series, showLegend: true, showValues: true });
|
|
74
|
+
if (slide.subtitle)
|
|
75
|
+
text("takeaway", slide.subtitle, .8, 6.25, 11.7, .6, 18);
|
|
76
|
+
}
|
|
77
|
+
else if (layout === "table") {
|
|
78
|
+
if (!slide.table?.length)
|
|
79
|
+
throw new Error("table 页面缺少 table");
|
|
80
|
+
elements.push({ id: `${slide.id}-table`, type: "table", x: .75, y: 1.65, width: 11.8, height: 5, rows: slide.table, headerRows: 1 });
|
|
81
|
+
}
|
|
82
|
+
else if (layout === "timeline" || layout === "metrics") {
|
|
83
|
+
const entries = layout === "timeline" ? slide.steps?.map(step => ({ label: step.title, value: step.body })) : slide.metrics;
|
|
84
|
+
if (!entries?.length || entries.length > 5)
|
|
85
|
+
throw new Error(`${layout} 需要 1–5 项`);
|
|
86
|
+
const w = 12 / entries.length;
|
|
87
|
+
entries.forEach((item, i) => {
|
|
88
|
+
text(`label-${i}`, item.label, .7 + w * i, 1.9, w - .35, 1, 20, true, "245BA8");
|
|
89
|
+
text(`value-${i}`, item.value, .7 + w * i, 3.05, w - .35, 2.1, layout === "metrics" ? 30 : 22, layout === "metrics");
|
|
90
|
+
if ("detail" in item && typeof item.detail === "string")
|
|
91
|
+
text(`detail-${i}`, item.detail, .7 + w * i, 5.4, w - .35, 1.1, 16);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
else
|
|
95
|
+
throw new Error(`不支持的 layout:${layout}`);
|
|
96
|
+
spec.slides.push({ id: slide.id, title: slide.title, notes: slide.notes, background: "F8FAFD", elements });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const result = validatePresentationSpec(spec);
|
|
100
|
+
if (!result.valid)
|
|
101
|
+
throw new Error(JSON.stringify(result.diagnostics));
|
|
102
|
+
if (!spec.slides.length || spec.canvas.width > 56 || spec.canvas.height > 56)
|
|
103
|
+
throw new Error("页面数或画布大小非法");
|
|
104
|
+
return spec;
|
|
105
|
+
}
|
|
106
|
+
/** Stable slide IDs make a local revision independent of pagination. */
|
|
107
|
+
export function patchSource(source, patch) {
|
|
108
|
+
if (!object(patch))
|
|
109
|
+
throw new Error("patch 必须为对象");
|
|
110
|
+
const copy = structuredClone(source);
|
|
111
|
+
const deck = copy.kind === "brief" ? copy.brief : copy.spec;
|
|
112
|
+
if (patch.data !== undefined) {
|
|
113
|
+
if (copy.kind !== "brief" || !object(patch.data))
|
|
114
|
+
throw new Error("data 补丁仅用于 brief");
|
|
115
|
+
copy.brief.data = { ...copy.brief.data, ...patch.data };
|
|
116
|
+
}
|
|
117
|
+
if (patch.slides !== undefined) {
|
|
118
|
+
if (!object(patch.slides))
|
|
119
|
+
throw new Error("slides 补丁格式为 {页面ID: 字段补丁}");
|
|
120
|
+
for (const [id, changes] of Object.entries(patch.slides)) {
|
|
121
|
+
const index = deck.slides.findIndex(slide => slide.id === id);
|
|
122
|
+
if (index < 0 || !object(changes) || (changes.id !== undefined && changes.id !== id))
|
|
123
|
+
throw new Error(`页面补丁非法:${id}`);
|
|
124
|
+
Object.assign(deck.slides[index], changes);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const unknown = Object.keys(patch).filter(key => key !== "data" && key !== "slides");
|
|
128
|
+
if (unknown.length)
|
|
129
|
+
throw new Error(`不支持的补丁字段:${unknown.join(",")}`);
|
|
130
|
+
compile(copy);
|
|
131
|
+
return copy;
|
|
132
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Inspect generated OOXML rather than making the model write a ZIP/XML checker for every deck. */
|
|
2
|
+
export declare function inspectPptx(bytes: Buffer): Promise<{
|
|
3
|
+
slides: number;
|
|
4
|
+
notes: number;
|
|
5
|
+
nativeCharts: number;
|
|
6
|
+
nativeTables: number;
|
|
7
|
+
editableTextShapes: number;
|
|
8
|
+
embeddedWorkbooks: number;
|
|
9
|
+
canvasInches: {
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
} | undefined;
|
|
13
|
+
relationships: string;
|
|
14
|
+
verification: string;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
/** Inspect generated OOXML rather than making the model write a ZIP/XML checker for every deck. */
|
|
3
|
+
export async function inspectPptx(bytes) {
|
|
4
|
+
const { unzipSync, strFromU8 } = await import("fflate");
|
|
5
|
+
const files = unzipSync(bytes);
|
|
6
|
+
const names = Object.keys(files);
|
|
7
|
+
const slideNames = names.filter(name => /^ppt\/slides\/slide\d+\.xml$/.test(name));
|
|
8
|
+
const notes = names.filter(name => /^ppt\/notesSlides\/notesSlide\d+\.xml$/.test(name));
|
|
9
|
+
const charts = names.filter(name => /^ppt\/charts\/chart\d+\.xml$/.test(name));
|
|
10
|
+
const tables = slideNames.reduce((sum, name) => sum + (strFromU8(files[name]).match(/<a:tbl>/g)?.length ?? 0), 0);
|
|
11
|
+
const textShapes = slideNames.reduce((sum, name) => sum + (strFromU8(files[name]).match(/<p:sp>/g)?.length ?? 0), 0);
|
|
12
|
+
const missing = [];
|
|
13
|
+
for (const name of names.filter(name => name.endsWith(".rels"))) {
|
|
14
|
+
const directory = posix.dirname(name).replace(/(?:^|\/)\_rels$/, "");
|
|
15
|
+
for (const relation of strFromU8(files[name]).matchAll(/<Relationship\b[^>]*\/>/g)) {
|
|
16
|
+
if (/TargetMode="External"/.test(relation[0]))
|
|
17
|
+
continue;
|
|
18
|
+
const target = /\bTarget="([^"]+)"/.exec(relation[0])?.[1];
|
|
19
|
+
if (!target)
|
|
20
|
+
throw new Error("PPTX 包含缺少 Target 的关系");
|
|
21
|
+
const member = posix.normalize(target.startsWith("/") ? target.slice(1) : posix.join(directory, target));
|
|
22
|
+
if (!Object.hasOwn(files, member))
|
|
23
|
+
missing.push(`${name}: ${member}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (!slideNames.length || missing.length)
|
|
27
|
+
throw new Error(`PPTX 结构检查失败:${missing.join("; ") || "没有页面"}`);
|
|
28
|
+
const size = /<p:sldSz\b[^>]*\bcx="(\d+)"[^>]*\bcy="(\d+)"/.exec(strFromU8(files["ppt/presentation.xml"]));
|
|
29
|
+
return { slides: slideNames.length, notes: notes.length, nativeCharts: charts.length, nativeTables: tables, editableTextShapes: textShapes, embeddedWorkbooks: names.filter(name => /^ppt\/embeddings\/.*\.xlsx$/.test(name)).length, canvasInches: size ? { width: Number(size[1]) / 914400, height: Number(size[2]) / 914400 } : undefined, relationships: "complete", verification: "OOXML structure; native rendering is reported separately" };
|
|
30
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type NativeResult = {
|
|
2
|
+
pages: Buffer[];
|
|
3
|
+
engine: string;
|
|
4
|
+
} | {
|
|
5
|
+
reason: string;
|
|
6
|
+
};
|
|
7
|
+
/** Native rendering is optional and serialized; a failed engine never masquerades as verified. */
|
|
8
|
+
export declare function nativePreview(path: string, count: number): Promise<NativeResult>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
let queue = Promise.resolve();
|
|
7
|
+
/** Native rendering is optional and serialized; a failed engine never masquerades as verified. */
|
|
8
|
+
export async function nativePreview(path, count) {
|
|
9
|
+
if (process.env.SUPER_PI_PRESENTATION_NATIVE === "0" || process.platform !== "win32")
|
|
10
|
+
return { reason: "此服务端未启用原生办公引擎,提供 PNG 布局预览。" };
|
|
11
|
+
const task = queue.catch(() => { }).then(async () => {
|
|
12
|
+
const directory = await mkdtemp(join(tmpdir(), "super-pi-native-ppt-"));
|
|
13
|
+
try {
|
|
14
|
+
await new Promise((resolve, reject) => execFile("powershell.exe", ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", fileURLToPath(new URL("../../../bin/render-presentation.ps1", import.meta.url)), "-InputFile", path, "-OutputDirectory", directory], { windowsHide: true, timeout: 60_000, maxBuffer: 64 * 1024 }, (error, _stdout, stderr) => error ? reject(new Error(stderr.trim().slice(0, 1000) || error.message)) : resolve()));
|
|
15
|
+
const pages = [];
|
|
16
|
+
for (let index = 1; index <= count; index++) {
|
|
17
|
+
const bytes = await readFile(join(directory, `slide-${index}.png`));
|
|
18
|
+
if (bytes.length < 100 || bytes.subarray(1, 4).toString() !== "PNG")
|
|
19
|
+
throw new Error("原生引擎未导出完整 PNG");
|
|
20
|
+
pages.push(bytes);
|
|
21
|
+
}
|
|
22
|
+
return { pages, engine: "Microsoft PowerPoint" };
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return { reason: `原生渲染未完成,保留布局预览:${String(error instanceof Error ? error.message : error).slice(0, 1200)}` };
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
await rm(directory, { recursive: true, force: true, maxRetries: 4 });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
queue = task;
|
|
32
|
+
return task;
|
|
33
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PresentationSpec, PresentationSlide } from "./schema.js";
|
|
2
|
+
export type LayoutWarning = {
|
|
3
|
+
slideId: string;
|
|
4
|
+
elementId: string;
|
|
5
|
+
message: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function rasterPreviews(spec: PresentationSpec, previews: string[]): Promise<{
|
|
8
|
+
pages: Buffer<ArrayBufferLike>[];
|
|
9
|
+
sheets: Buffer<ArrayBufferLike>[];
|
|
10
|
+
}>;
|
|
11
|
+
export declare function writePptx(spec: PresentationSpec): Promise<Buffer>;
|
|
12
|
+
/** SVG is a layout preview, never evidence that an office engine rendered the PPTX. */
|
|
13
|
+
export declare function layoutPreview(spec: PresentationSpec, page: PresentationSlide): {
|
|
14
|
+
svg: string;
|
|
15
|
+
warnings: LayoutWarning[];
|
|
16
|
+
};
|