dsh-typst-plugin 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.en.md +121 -0
- package/README.md +120 -2
- package/cordis.patch.yml +6 -0
- package/docs/images/icon.png +0 -0
- package/docs/plugin-detail.en.md +2 -0
- package/docs/plugin-detail.md +2 -0
- package/lib/client.js +41996 -0
- package/lib/client.js.map +7 -0
- package/lib/config.d.ts +107 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +167 -0
- package/lib/config.js.map +1 -0
- package/lib/constants.d.ts +77 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +85 -0
- package/lib/constants.js.map +1 -0
- package/lib/contract.d.ts +263 -0
- package/lib/contract.d.ts.map +1 -0
- package/lib/contract.js +9 -0
- package/lib/contract.js.map +1 -0
- package/lib/diagnostic-text.d.ts +13 -0
- package/lib/diagnostic-text.d.ts.map +1 -0
- package/lib/diagnostic-text.js +28 -0
- package/lib/diagnostic-text.js.map +1 -0
- package/lib/errors.d.ts +119 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +161 -0
- package/lib/errors.js.map +1 -0
- package/lib/host/facts.d.ts +31 -0
- package/lib/host/facts.d.ts.map +1 -0
- package/lib/host/facts.js +145 -0
- package/lib/host/facts.js.map +1 -0
- package/lib/host/which.d.ts +20 -0
- package/lib/host/which.d.ts.map +1 -0
- package/lib/host/which.js +50 -0
- package/lib/host/which.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/remote.d.ts +70 -0
- package/lib/remote.d.ts.map +1 -0
- package/lib/remote.js +156 -0
- package/lib/remote.js.map +1 -0
- package/lib/service.d.ts +71 -0
- package/lib/service.d.ts.map +1 -0
- package/lib/service.js +213 -0
- package/lib/service.js.map +1 -0
- package/lib/skills/typst.d.ts +13 -0
- package/lib/skills/typst.d.ts.map +1 -0
- package/lib/skills/typst.js +78 -0
- package/lib/skills/typst.js.map +1 -0
- package/lib/status-codec.d.ts +11 -0
- package/lib/status-codec.d.ts.map +1 -0
- package/lib/status-codec.js +11 -0
- package/lib/status-codec.js.map +1 -0
- package/lib/tools/registration.d.ts +23 -0
- package/lib/tools/registration.d.ts.map +1 -0
- package/lib/tools/registration.js +758 -0
- package/lib/tools/registration.js.map +1 -0
- package/lib/tools/shared.d.ts +30 -0
- package/lib/tools/shared.d.ts.map +1 -0
- package/lib/tools/shared.js +79 -0
- package/lib/tools/shared.js.map +1 -0
- package/lib/typert.d.ts +25 -0
- package/lib/typert.d.ts.map +1 -0
- package/lib/typert.js +116 -0
- package/lib/typert.js.map +1 -0
- package/lib/typst/diagnostics.d.ts +58 -0
- package/lib/typst/diagnostics.d.ts.map +1 -0
- package/lib/typst/diagnostics.js +237 -0
- package/lib/typst/diagnostics.js.map +1 -0
- package/lib/typst/flags.d.ts +8 -0
- package/lib/typst/flags.d.ts.map +1 -0
- package/lib/typst/flags.js +8 -0
- package/lib/typst/flags.js.map +1 -0
- package/lib/typst/locate.d.ts +61 -0
- package/lib/typst/locate.d.ts.map +1 -0
- package/lib/typst/locate.js +152 -0
- package/lib/typst/locate.js.map +1 -0
- package/lib/typst/mainfile.d.ts +40 -0
- package/lib/typst/mainfile.d.ts.map +1 -0
- package/lib/typst/mainfile.js +136 -0
- package/lib/typst/mainfile.js.map +1 -0
- package/lib/typst/match.d.ts +67 -0
- package/lib/typst/match.d.ts.map +1 -0
- package/lib/typst/match.js +227 -0
- package/lib/typst/match.js.map +1 -0
- package/lib/typst/oneshot.d.ts +48 -0
- package/lib/typst/oneshot.d.ts.map +1 -0
- package/lib/typst/oneshot.js +86 -0
- package/lib/typst/oneshot.js.map +1 -0
- package/lib/typst/owners.d.ts +28 -0
- package/lib/typst/owners.d.ts.map +1 -0
- package/lib/typst/owners.js +98 -0
- package/lib/typst/owners.js.map +1 -0
- package/lib/typst/pdf-extract.d.ts +28 -0
- package/lib/typst/pdf-extract.d.ts.map +1 -0
- package/lib/typst/pdf-extract.js +111 -0
- package/lib/typst/pdf-extract.js.map +1 -0
- package/lib/typst/process.d.ts +73 -0
- package/lib/typst/process.d.ts.map +1 -0
- package/lib/typst/process.js +147 -0
- package/lib/typst/process.js.map +1 -0
- package/lib/typst/text.d.ts +62 -0
- package/lib/typst/text.d.ts.map +1 -0
- package/lib/typst/text.js +282 -0
- package/lib/typst/text.js.map +1 -0
- package/lib/typst/watcher.d.ts +76 -0
- package/lib/typst/watcher.d.ts.map +1 -0
- package/lib/typst/watcher.js +210 -0
- package/lib/typst/watcher.js.map +1 -0
- package/lib/wire-schemas.d.ts +433 -0
- package/lib/wire-schemas.d.ts.map +1 -0
- package/lib/wire-schemas.js +428 -0
- package/lib/wire-schemas.js.map +1 -0
- package/lib/workspace/containment.d.ts +25 -0
- package/lib/workspace/containment.d.ts.map +1 -0
- package/lib/workspace/containment.js +54 -0
- package/lib/workspace/containment.js.map +1 -0
- package/lib/workspace/session.d.ts +268 -0
- package/lib/workspace/session.d.ts.map +1 -0
- package/lib/workspace/session.js +1576 -0
- package/lib/workspace/session.js.map +1 -0
- package/locale/en.json +15 -0
- package/locale/zh.json +15 -0
- package/package.json +113 -4
- package/release-provenance.json +137 -0
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The seven agent tools of the Typst workbench. Semantics ported from the
|
|
3
|
+
* ZCode typst-workbench server (0.2.0); schemas follow the dsh-zotero tool
|
|
4
|
+
* conventions (value-schema literals with per-property `required`, canonical
|
|
5
|
+
* JSON output plus a model-facing render, one registration effect per tool).
|
|
6
|
+
* Host half only.
|
|
7
|
+
*
|
|
8
|
+
* `typst_render_pages` returns durable attachment references in its canonical
|
|
9
|
+
* value and the client's `tool.call.toolview` view displays them inline; the
|
|
10
|
+
* model-facing render stays text (the canonical JSON is persistence and
|
|
11
|
+
* toolview data).
|
|
12
|
+
* @module dsh-typst/tools/registration
|
|
13
|
+
*/
|
|
14
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
15
|
+
import { LIMITS } from "../constants.js";
|
|
16
|
+
import { TYPST_ATTACHMENT_UNAVAILABLE, TYPST_COMPILE_FAILED, TYPST_INVALID_ARGUMENT, TYPST_INVALID_TARGET, TYPST_NOT_COMPOSED, TypstError, } from "../errors.js";
|
|
17
|
+
import { compileSummary, countOf, diagnosticsText, sessionOfExec, } from "./shared.js";
|
|
18
|
+
function summarize(session) {
|
|
19
|
+
const state = session.state();
|
|
20
|
+
const { errors, warnings } = countOf(state.diagnostics);
|
|
21
|
+
const pages = state.snapshot?.pages ?? null;
|
|
22
|
+
return {
|
|
23
|
+
main: state.main,
|
|
24
|
+
phase: state.status.phase,
|
|
25
|
+
errors,
|
|
26
|
+
warnings,
|
|
27
|
+
pages,
|
|
28
|
+
durationMs: state.status.durationMs ?? null,
|
|
29
|
+
summary: compileSummary(state.status, state.main, state.diagnostics, pages ?? undefined, state.diagnosticsTotal),
|
|
30
|
+
problems: state.diagnostics.length
|
|
31
|
+
? diagnosticsText(state.diagnostics, LIMITS.diagnostics, state.diagnosticsTotal)
|
|
32
|
+
: "",
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function textOf(summary) {
|
|
36
|
+
return summary.problems !== ""
|
|
37
|
+
? `${summary.summary}\n\n${summary.problems}`
|
|
38
|
+
: summary.summary;
|
|
39
|
+
}
|
|
40
|
+
/** Canonical JSON must be lossless: optional facts are stripped, never null-typed. */
|
|
41
|
+
function stripAbsent(value) {
|
|
42
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
|
|
43
|
+
}
|
|
44
|
+
/** One file location for a tool card's editor follow-along, or none. */
|
|
45
|
+
function fileLocations(path, line) {
|
|
46
|
+
if (typeof path !== "string" || path.length === 0)
|
|
47
|
+
return undefined;
|
|
48
|
+
return [
|
|
49
|
+
{
|
|
50
|
+
path,
|
|
51
|
+
...(typeof line === "number" && Number.isInteger(line) ? { line } : {}),
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// typst_status
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
function typstStatusTool(ctx, service) {
|
|
59
|
+
return defineTool({
|
|
60
|
+
name: "typst_status",
|
|
61
|
+
description: [
|
|
62
|
+
"Show the Typst workbench: the Typst installation in use (version and path), the main .typ file and the other .typ files in the workspace, and the latest compile result.",
|
|
63
|
+
"Call this first when working on a Typst document.",
|
|
64
|
+
"The live preview panel updates by itself when files are saved; this tool reports the state.",
|
|
65
|
+
].join(" "),
|
|
66
|
+
parameters: {},
|
|
67
|
+
output: {
|
|
68
|
+
schema: {
|
|
69
|
+
type: "object",
|
|
70
|
+
properties: {
|
|
71
|
+
toolchain: {
|
|
72
|
+
type: "object",
|
|
73
|
+
required: true,
|
|
74
|
+
additionalProperties: false,
|
|
75
|
+
description: "Typst toolchain state view.",
|
|
76
|
+
properties: {
|
|
77
|
+
state: {
|
|
78
|
+
type: "string",
|
|
79
|
+
required: true,
|
|
80
|
+
enum: ["ready", "missing", "too_old", "error"],
|
|
81
|
+
},
|
|
82
|
+
version: { type: "string" },
|
|
83
|
+
path: { type: "string" },
|
|
84
|
+
source: {
|
|
85
|
+
type: "string",
|
|
86
|
+
enum: ["setting", "path", "common", "shell"],
|
|
87
|
+
},
|
|
88
|
+
error: { type: "string" },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
main: {
|
|
92
|
+
type: "string",
|
|
93
|
+
description: "The chosen main .typ file (workspace-relative).",
|
|
94
|
+
},
|
|
95
|
+
candidates: {
|
|
96
|
+
type: "array",
|
|
97
|
+
required: true,
|
|
98
|
+
items: { type: "string" },
|
|
99
|
+
description: "The workspace's .typ files.",
|
|
100
|
+
},
|
|
101
|
+
phase: {
|
|
102
|
+
type: "string",
|
|
103
|
+
required: true,
|
|
104
|
+
description: "Latest compile phase.",
|
|
105
|
+
},
|
|
106
|
+
errors: { type: "integer", required: true },
|
|
107
|
+
warnings: { type: "integer", required: true },
|
|
108
|
+
pages: { type: "integer", description: "Preview page count." },
|
|
109
|
+
summary: {
|
|
110
|
+
type: "string",
|
|
111
|
+
required: true,
|
|
112
|
+
description: "One-line compile summary.",
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
additionalProperties: false,
|
|
116
|
+
},
|
|
117
|
+
render: (_args, value) => {
|
|
118
|
+
const toolchain = value.toolchain;
|
|
119
|
+
const install = toolchain?.state === "ready"
|
|
120
|
+
? `typst ${toolchain.version} at ${toolchain.path} (${toolchain.source})`
|
|
121
|
+
: `typst ${toolchain?.state ?? "unknown"}${toolchain?.error ? ` — ${toolchain.error}` : ""}`;
|
|
122
|
+
const candidates = value.candidates.length > 0 ? value.candidates.join(", ") : "(none)";
|
|
123
|
+
return [
|
|
124
|
+
{
|
|
125
|
+
type: "text",
|
|
126
|
+
text: [
|
|
127
|
+
install,
|
|
128
|
+
`main: ${value.main ?? "(none)"}`,
|
|
129
|
+
`candidates: ${candidates}`,
|
|
130
|
+
value.summary,
|
|
131
|
+
].join("\n"),
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
presentCall: () => ({ card: "generic", title: "Typst status" }),
|
|
137
|
+
isConcurrencySafe: () => true,
|
|
138
|
+
async execute(_args, exec) {
|
|
139
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
140
|
+
await session.activate();
|
|
141
|
+
const state = session.state();
|
|
142
|
+
const summary = summarize(session);
|
|
143
|
+
return stripAbsent({
|
|
144
|
+
toolchain: state.toolchain,
|
|
145
|
+
...(state.main !== null ? { main: state.main } : {}),
|
|
146
|
+
candidates: state.candidates,
|
|
147
|
+
phase: summary.phase,
|
|
148
|
+
errors: summary.errors,
|
|
149
|
+
warnings: summary.warnings,
|
|
150
|
+
...(summary.pages !== null ? { pages: summary.pages } : {}),
|
|
151
|
+
summary: summary.summary,
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// typst_set_main
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
function typstSetMainTool(ctx, service) {
|
|
160
|
+
return defineTool({
|
|
161
|
+
name: "typst_set_main",
|
|
162
|
+
description: [
|
|
163
|
+
"Choose the main .typ file of the document (the one you would pass to `typst compile`).",
|
|
164
|
+
"The preview restarts with it, and the choice is remembered for this workspace.",
|
|
165
|
+
].join(" "),
|
|
166
|
+
parameters: {
|
|
167
|
+
path: {
|
|
168
|
+
type: "string",
|
|
169
|
+
required: true,
|
|
170
|
+
description: "Workspace-relative path of a .typ file.",
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
output: {
|
|
174
|
+
schema: {
|
|
175
|
+
type: "object",
|
|
176
|
+
properties: {
|
|
177
|
+
main: { type: "string", required: true },
|
|
178
|
+
phase: { type: "string", required: true },
|
|
179
|
+
errors: { type: "integer", required: true },
|
|
180
|
+
warnings: { type: "integer", required: true },
|
|
181
|
+
settled: {
|
|
182
|
+
type: "boolean",
|
|
183
|
+
required: true,
|
|
184
|
+
description: "Whether the new main file finished compiling within the check budget.",
|
|
185
|
+
},
|
|
186
|
+
reason: {
|
|
187
|
+
type: "string",
|
|
188
|
+
description: "Why waiting for the new main file to compile failed.",
|
|
189
|
+
},
|
|
190
|
+
summary: { type: "string", required: true },
|
|
191
|
+
},
|
|
192
|
+
additionalProperties: false,
|
|
193
|
+
},
|
|
194
|
+
render: (_args, value) => [{ type: "text", text: value.summary }],
|
|
195
|
+
},
|
|
196
|
+
presentCall: (args) => ({
|
|
197
|
+
card: "generic",
|
|
198
|
+
title: "Set Typst main file",
|
|
199
|
+
rawInput: String(args.path ?? ""),
|
|
200
|
+
locations: fileLocations(args.path),
|
|
201
|
+
}),
|
|
202
|
+
isConcurrencySafe: () => false,
|
|
203
|
+
async execute(args, exec) {
|
|
204
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
205
|
+
const main = await session.setMain(args.path);
|
|
206
|
+
let settled = true;
|
|
207
|
+
let reason;
|
|
208
|
+
try {
|
|
209
|
+
await session.settle(LIMITS.checkTimeoutMs, exec.signal);
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if (exec.signal.aborted)
|
|
213
|
+
throw error;
|
|
214
|
+
settled = false;
|
|
215
|
+
reason = error instanceof Error ? error.message : String(error);
|
|
216
|
+
}
|
|
217
|
+
const summary = summarize(session);
|
|
218
|
+
return {
|
|
219
|
+
main,
|
|
220
|
+
phase: summary.phase,
|
|
221
|
+
errors: summary.errors,
|
|
222
|
+
warnings: summary.warnings,
|
|
223
|
+
settled,
|
|
224
|
+
...(reason !== undefined ? { reason } : {}),
|
|
225
|
+
summary: settled
|
|
226
|
+
? textOf(summary)
|
|
227
|
+
: `${summary.summary}\n\nCould not wait for the new main file to compile: ${reason ?? "unknown error"}`,
|
|
228
|
+
};
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
// typst_check
|
|
234
|
+
// ---------------------------------------------------------------------------
|
|
235
|
+
function typstCheckTool(ctx, service) {
|
|
236
|
+
return defineTool({
|
|
237
|
+
name: "typst_check",
|
|
238
|
+
description: [
|
|
239
|
+
"Compile the document with the user's Typst and report errors and warnings with file:line:column, the source line and Typst's hints.",
|
|
240
|
+
"Waits until every change already saved to disk has been compiled, so call it right after editing.",
|
|
241
|
+
"The preview panel updates by itself; this is how you see the result.",
|
|
242
|
+
].join(" "),
|
|
243
|
+
parameters: {
|
|
244
|
+
timeoutMs: {
|
|
245
|
+
type: "integer",
|
|
246
|
+
description: `How long to wait for Typst in milliseconds (default ${LIMITS.checkTimeoutMs / 1e3} s).`,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
output: {
|
|
250
|
+
schema: {
|
|
251
|
+
type: "object",
|
|
252
|
+
properties: {
|
|
253
|
+
main: { type: "string", description: "The chosen main .typ file." },
|
|
254
|
+
phase: { type: "string", required: true },
|
|
255
|
+
errors: { type: "integer", required: true },
|
|
256
|
+
warnings: { type: "integer", required: true },
|
|
257
|
+
pages: { type: "integer", description: "Preview page count." },
|
|
258
|
+
durationMs: { type: "integer", description: "Compile duration." },
|
|
259
|
+
summary: { type: "string", required: true },
|
|
260
|
+
diagnostics: {
|
|
261
|
+
type: "array",
|
|
262
|
+
description: "The bounded diagnostic views for the latest compile.",
|
|
263
|
+
items: {
|
|
264
|
+
type: "object",
|
|
265
|
+
additionalProperties: false,
|
|
266
|
+
properties: {
|
|
267
|
+
severity: {
|
|
268
|
+
type: "string",
|
|
269
|
+
required: true,
|
|
270
|
+
enum: ["error", "warning"],
|
|
271
|
+
},
|
|
272
|
+
message: { type: "string", required: true },
|
|
273
|
+
location: {
|
|
274
|
+
oneOf: [
|
|
275
|
+
{
|
|
276
|
+
type: "object",
|
|
277
|
+
additionalProperties: false,
|
|
278
|
+
properties: {
|
|
279
|
+
file: { type: "string", required: true },
|
|
280
|
+
line: { type: "integer", required: true },
|
|
281
|
+
column: { type: "integer", required: true },
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
{ type: "null" },
|
|
285
|
+
],
|
|
286
|
+
},
|
|
287
|
+
excerpt: { type: "string" },
|
|
288
|
+
hints: { type: "array", items: { type: "string" } },
|
|
289
|
+
trace: {
|
|
290
|
+
type: "array",
|
|
291
|
+
items: {
|
|
292
|
+
type: "object",
|
|
293
|
+
additionalProperties: false,
|
|
294
|
+
properties: {
|
|
295
|
+
message: { type: "string", required: true },
|
|
296
|
+
location: {
|
|
297
|
+
oneOf: [
|
|
298
|
+
{
|
|
299
|
+
type: "object",
|
|
300
|
+
additionalProperties: false,
|
|
301
|
+
properties: {
|
|
302
|
+
file: { type: "string", required: true },
|
|
303
|
+
line: { type: "integer", required: true },
|
|
304
|
+
column: { type: "integer", required: true },
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
{ type: "null" },
|
|
308
|
+
],
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
diagnosticsTotal: {
|
|
317
|
+
type: "integer",
|
|
318
|
+
required: true,
|
|
319
|
+
description: "Total diagnostics before truncation.",
|
|
320
|
+
},
|
|
321
|
+
diagnosticsTruncated: {
|
|
322
|
+
type: "boolean",
|
|
323
|
+
required: true,
|
|
324
|
+
description: "Whether the diagnostic cap omitted any entries.",
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
additionalProperties: false,
|
|
328
|
+
},
|
|
329
|
+
render: (_args, value) => {
|
|
330
|
+
const diagnostics = Array.isArray(value.diagnostics)
|
|
331
|
+
? value.diagnostics
|
|
332
|
+
: [];
|
|
333
|
+
const body = diagnostics.length > 0 ? `\n\n${diagnosticsText(diagnostics)}` : "";
|
|
334
|
+
return [{ type: "text", text: `${value.summary}${body}` }];
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
presentCall: () => ({ card: "generic", title: "Check Typst document" }),
|
|
338
|
+
isConcurrencySafe: () => true,
|
|
339
|
+
async execute(args, exec) {
|
|
340
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
341
|
+
const timeoutMs = typeof args.timeoutMs === "number" && Number.isFinite(args.timeoutMs)
|
|
342
|
+
? Math.min(Math.max(args.timeoutMs, 1_000), 600_000)
|
|
343
|
+
: LIMITS.checkTimeoutMs;
|
|
344
|
+
await session.settle(timeoutMs, exec.signal);
|
|
345
|
+
const state = session.state();
|
|
346
|
+
const summary = summarize(session);
|
|
347
|
+
return stripAbsent({
|
|
348
|
+
...(state.main !== null ? { main: state.main } : {}),
|
|
349
|
+
phase: summary.phase,
|
|
350
|
+
errors: summary.errors,
|
|
351
|
+
warnings: summary.warnings,
|
|
352
|
+
...(summary.pages !== null ? { pages: summary.pages } : {}),
|
|
353
|
+
...(summary.durationMs !== null
|
|
354
|
+
? { durationMs: Math.round(summary.durationMs) }
|
|
355
|
+
: {}),
|
|
356
|
+
summary: summary.summary,
|
|
357
|
+
diagnostics: state.diagnostics,
|
|
358
|
+
diagnosticsTotal: state.diagnosticsTotal,
|
|
359
|
+
diagnosticsTruncated: state.diagnosticsTruncated,
|
|
360
|
+
});
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
// ---------------------------------------------------------------------------
|
|
365
|
+
// typst_render_pages
|
|
366
|
+
// ---------------------------------------------------------------------------
|
|
367
|
+
function typstRenderPagesTool(ctx, service) {
|
|
368
|
+
return defineTool({
|
|
369
|
+
name: "typst_render_pages",
|
|
370
|
+
description: [
|
|
371
|
+
"Render pages of the document to PNG images so you can look at the layout (overfull lines, figure placement, tables, title page, spacing).",
|
|
372
|
+
"Compiles the files on disk with the user's Typst.",
|
|
373
|
+
`At most ${LIMITS.renderPages} pages per call; default ${LIMITS.ppiDefault} ppi, raise ppi to inspect details.`,
|
|
374
|
+
"Compile errors do not catch layout problems: render the affected pages and look at them.",
|
|
375
|
+
].join(" "),
|
|
376
|
+
parameters: {
|
|
377
|
+
pages: {
|
|
378
|
+
type: "string",
|
|
379
|
+
required: true,
|
|
380
|
+
description: '1-based pages, e.g. "1", "2-3", "1,5".',
|
|
381
|
+
},
|
|
382
|
+
ppi: {
|
|
383
|
+
type: "integer",
|
|
384
|
+
description: `Resolution in pixels per inch (default ${LIMITS.ppiDefault}).`,
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
output: {
|
|
388
|
+
schema: {
|
|
389
|
+
type: "object",
|
|
390
|
+
properties: {
|
|
391
|
+
pages: {
|
|
392
|
+
type: "array",
|
|
393
|
+
required: true,
|
|
394
|
+
items: { type: "integer" },
|
|
395
|
+
description: "The rendered 1-based pages.",
|
|
396
|
+
},
|
|
397
|
+
total: {
|
|
398
|
+
type: "integer",
|
|
399
|
+
description: "Total pages of the document.",
|
|
400
|
+
},
|
|
401
|
+
images: {
|
|
402
|
+
type: "array",
|
|
403
|
+
required: true,
|
|
404
|
+
items: {
|
|
405
|
+
type: "object",
|
|
406
|
+
additionalProperties: false,
|
|
407
|
+
properties: {
|
|
408
|
+
page: { type: "integer", required: true },
|
|
409
|
+
attachment: {
|
|
410
|
+
type: "object",
|
|
411
|
+
required: true,
|
|
412
|
+
additionalProperties: false,
|
|
413
|
+
properties: {
|
|
414
|
+
attachmentId: { type: "string", required: true },
|
|
415
|
+
mediaType: { type: "string", required: true },
|
|
416
|
+
bytes: { type: "integer", required: true },
|
|
417
|
+
width: { type: "integer", required: true },
|
|
418
|
+
height: { type: "integer", required: true },
|
|
419
|
+
name: { type: "string" },
|
|
420
|
+
},
|
|
421
|
+
},
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
description: "Durable PNG attachments for the model and the card.",
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
additionalProperties: false,
|
|
428
|
+
},
|
|
429
|
+
render: (_args, value) => {
|
|
430
|
+
const images = value.images;
|
|
431
|
+
const rendered = images.map((image) => `page ${image.page}`).join(", ");
|
|
432
|
+
const total = value.total !== undefined ? ` of ${value.total}` : "";
|
|
433
|
+
return [
|
|
434
|
+
{ type: "text", text: `Rendered ${rendered}${total}.` },
|
|
435
|
+
...images.map((image) => ({
|
|
436
|
+
type: "image",
|
|
437
|
+
attachment: image.attachment,
|
|
438
|
+
})),
|
|
439
|
+
];
|
|
440
|
+
},
|
|
441
|
+
// The card's data channel: durable references only. The attachment
|
|
442
|
+
// service stores the bytes; the settled content's image blocks are the
|
|
443
|
+
// model-facing copy, so repeating base64 here would double the record.
|
|
444
|
+
presentationMeta: (_args, value) => ({
|
|
445
|
+
pages: value.pages,
|
|
446
|
+
...(value.total !== undefined ? { total: value.total } : {}),
|
|
447
|
+
images: value.images.map((image) => ({
|
|
448
|
+
page: image.page,
|
|
449
|
+
attachment: image.attachment,
|
|
450
|
+
})),
|
|
451
|
+
}),
|
|
452
|
+
},
|
|
453
|
+
presentCall: (args) => ({
|
|
454
|
+
card: "generic",
|
|
455
|
+
title: "Render Typst pages",
|
|
456
|
+
rawInput: String(args.pages ?? ""),
|
|
457
|
+
}),
|
|
458
|
+
isConcurrencySafe: () => false,
|
|
459
|
+
async execute(args, exec) {
|
|
460
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
461
|
+
const ppi = typeof args.ppi === "number" && Number.isFinite(args.ppi)
|
|
462
|
+
? Math.min(Math.max(args.ppi, LIMITS.ppiMin), LIMITS.ppiMax)
|
|
463
|
+
: LIMITS.ppiDefault;
|
|
464
|
+
const result = await session.renderPages(args.pages, ppi, exec.signal);
|
|
465
|
+
if (!result.ok) {
|
|
466
|
+
throw new TypstError(`The document does not compile, so no pages were rendered.\n\n${diagnosticsText(result.diagnostics)}`, TYPST_COMPILE_FAILED);
|
|
467
|
+
}
|
|
468
|
+
const attachments = ctx.get("attachments");
|
|
469
|
+
if (attachments === undefined) {
|
|
470
|
+
throw new TypstError("The attachment store is not composed, so rendered pages cannot be returned to the model.", TYPST_ATTACHMENT_UNAVAILABLE);
|
|
471
|
+
}
|
|
472
|
+
const images = await Promise.all(result.images.map(async (image) => ({
|
|
473
|
+
page: image.page,
|
|
474
|
+
attachment: await attachments.saveImage({
|
|
475
|
+
data: image.png,
|
|
476
|
+
mediaType: "image/png",
|
|
477
|
+
name: `page-${image.page}.png`,
|
|
478
|
+
}),
|
|
479
|
+
})));
|
|
480
|
+
return stripAbsent({
|
|
481
|
+
pages: result.pages,
|
|
482
|
+
...(result.total !== undefined ? { total: result.total } : {}),
|
|
483
|
+
images,
|
|
484
|
+
});
|
|
485
|
+
},
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
// ---------------------------------------------------------------------------
|
|
489
|
+
// typst_show
|
|
490
|
+
// ---------------------------------------------------------------------------
|
|
491
|
+
function typstShowTool(ctx, service) {
|
|
492
|
+
return defineTool({
|
|
493
|
+
name: "typst_show",
|
|
494
|
+
description: [
|
|
495
|
+
"Point the user at something in the live preview: request a page location, or highlight rendered text with an optional comment. The panel notifies the user, who chooses when to navigate.",
|
|
496
|
+
"Give exactly one of: page; quote (words as they appear in the rendered document — markup is ignored); or file and line (and optionally endLine) to highlight what those source lines produce.",
|
|
497
|
+
"Add a short comment explaining why. Highlights follow the text as the document changes and last for this session.",
|
|
498
|
+
].join(" "),
|
|
499
|
+
parameters: {
|
|
500
|
+
page: { type: "integer", description: "1-based page to request." },
|
|
501
|
+
quote: { type: "string", description: "Rendered words to highlight." },
|
|
502
|
+
file: { type: "string", description: "Workspace-relative .typ file." },
|
|
503
|
+
line: { type: "integer", description: "1-based line in that file." },
|
|
504
|
+
endLine: {
|
|
505
|
+
type: "integer",
|
|
506
|
+
description: "Last line of the range (default: line).",
|
|
507
|
+
},
|
|
508
|
+
comment: {
|
|
509
|
+
type: "string",
|
|
510
|
+
description: "Short comment shown with the highlight.",
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
output: {
|
|
514
|
+
schema: {
|
|
515
|
+
type: "object",
|
|
516
|
+
properties: {
|
|
517
|
+
highlighted: { type: "boolean", required: true },
|
|
518
|
+
exact: {
|
|
519
|
+
type: "boolean",
|
|
520
|
+
description: "Whether the quote matched exactly.",
|
|
521
|
+
},
|
|
522
|
+
id: { type: "string", description: "The highlight id." },
|
|
523
|
+
page: {
|
|
524
|
+
type: "integer",
|
|
525
|
+
description: "The 1-based page requested for viewing.",
|
|
526
|
+
},
|
|
527
|
+
},
|
|
528
|
+
additionalProperties: false,
|
|
529
|
+
},
|
|
530
|
+
render: (_args, value) => [
|
|
531
|
+
{
|
|
532
|
+
type: "text",
|
|
533
|
+
text: value.highlighted
|
|
534
|
+
? value.exact === false
|
|
535
|
+
? "Highlighted in the preview (approximate match; quote the rendered words for an exact one)."
|
|
536
|
+
: "Highlighted in the preview."
|
|
537
|
+
: `Requested viewing page ${value.page}; the user can open the location notification.`,
|
|
538
|
+
},
|
|
539
|
+
],
|
|
540
|
+
},
|
|
541
|
+
presentCall: (args) => ({
|
|
542
|
+
card: "generic",
|
|
543
|
+
title: "Show in preview",
|
|
544
|
+
rawInput: String(args.quote ??
|
|
545
|
+
args.file ??
|
|
546
|
+
(args.page !== undefined ? `page ${args.page}` : "")),
|
|
547
|
+
locations: fileLocations(args.file, args.line),
|
|
548
|
+
}),
|
|
549
|
+
isConcurrencySafe: () => false,
|
|
550
|
+
async execute(args, exec) {
|
|
551
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
552
|
+
await session.activate();
|
|
553
|
+
const hasPage = args.page !== undefined;
|
|
554
|
+
const hasQuote = args.quote !== undefined;
|
|
555
|
+
const hasFile = args.file !== undefined;
|
|
556
|
+
const targets = [hasPage, hasQuote, hasFile].filter(Boolean).length;
|
|
557
|
+
if (targets !== 1) {
|
|
558
|
+
throw new TypstError("Give exactly one target: page, quote, or file (with line).", TYPST_INVALID_TARGET);
|
|
559
|
+
}
|
|
560
|
+
if (hasPage && (!Number.isInteger(args.page) || (args.page ?? 0) < 1)) {
|
|
561
|
+
throw new TypstError("page must be an integer of at least 1.", TYPST_INVALID_TARGET);
|
|
562
|
+
}
|
|
563
|
+
if (hasQuote &&
|
|
564
|
+
args.quote !== undefined &&
|
|
565
|
+
args.quote.trim().length === 0) {
|
|
566
|
+
throw new TypstError("quote must contain the rendered words to highlight.", TYPST_INVALID_TARGET);
|
|
567
|
+
}
|
|
568
|
+
if (hasFile) {
|
|
569
|
+
if (typeof args.file !== "string" || args.file.length === 0) {
|
|
570
|
+
throw new TypstError("file must be a workspace-relative .typ path.", TYPST_INVALID_TARGET);
|
|
571
|
+
}
|
|
572
|
+
if (args.line === undefined) {
|
|
573
|
+
throw new TypstError("file needs a 1-based line.", TYPST_INVALID_TARGET);
|
|
574
|
+
}
|
|
575
|
+
if (!Number.isInteger(args.line) || args.line < 1) {
|
|
576
|
+
throw new TypstError("line must be an integer of at least 1.", TYPST_INVALID_TARGET);
|
|
577
|
+
}
|
|
578
|
+
if (args.endLine !== undefined &&
|
|
579
|
+
(!Number.isInteger(args.endLine) || args.endLine < args.line)) {
|
|
580
|
+
throw new TypstError("endLine must be an integer not smaller than line.", TYPST_INVALID_TARGET);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (session.state().snapshot === null) {
|
|
584
|
+
await session
|
|
585
|
+
.settle(LIMITS.checkTimeoutMs, exec.signal)
|
|
586
|
+
.catch(() => undefined);
|
|
587
|
+
}
|
|
588
|
+
if (typeof args.page === "number") {
|
|
589
|
+
session.showPage(args.page - 1);
|
|
590
|
+
return { highlighted: false, page: args.page };
|
|
591
|
+
}
|
|
592
|
+
const target = {};
|
|
593
|
+
if (typeof args.quote === "string")
|
|
594
|
+
target.quote = args.quote;
|
|
595
|
+
if (typeof args.file === "string") {
|
|
596
|
+
target.file = args.file;
|
|
597
|
+
if (typeof args.line === "number")
|
|
598
|
+
target.line = args.line;
|
|
599
|
+
if (typeof args.endLine === "number")
|
|
600
|
+
target.endLine = args.endLine;
|
|
601
|
+
}
|
|
602
|
+
const created = await session.highlight(target, typeof args.comment === "string" ? args.comment : "");
|
|
603
|
+
return {
|
|
604
|
+
highlighted: true,
|
|
605
|
+
exact: created.exact,
|
|
606
|
+
id: created.highlight.id,
|
|
607
|
+
};
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
// ---------------------------------------------------------------------------
|
|
612
|
+
// typst_clear_highlights
|
|
613
|
+
// ---------------------------------------------------------------------------
|
|
614
|
+
function typstClearHighlightsTool(ctx, service) {
|
|
615
|
+
return defineTool({
|
|
616
|
+
name: "typst_clear_highlights",
|
|
617
|
+
description: "Remove the highlights you placed in the live preview (all of them, or the named ids).",
|
|
618
|
+
parameters: {
|
|
619
|
+
ids: {
|
|
620
|
+
type: "array",
|
|
621
|
+
items: { type: "string" },
|
|
622
|
+
description: "Highlight ids to remove; omit to remove all.",
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
output: {
|
|
626
|
+
schema: {
|
|
627
|
+
type: "object",
|
|
628
|
+
properties: { removed: { type: "integer", required: true } },
|
|
629
|
+
additionalProperties: false,
|
|
630
|
+
},
|
|
631
|
+
render: (_args, value) => [
|
|
632
|
+
{ type: "text", text: `Removed ${value.removed} highlight(s).` },
|
|
633
|
+
],
|
|
634
|
+
},
|
|
635
|
+
presentCall: () => ({ card: "generic", title: "Clear preview highlights" }),
|
|
636
|
+
isConcurrencySafe: () => true,
|
|
637
|
+
async execute(args, exec) {
|
|
638
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
639
|
+
const ids = args.ids;
|
|
640
|
+
if (ids !== undefined) {
|
|
641
|
+
if (!Array.isArray(ids) ||
|
|
642
|
+
ids.length === 0 ||
|
|
643
|
+
!ids.every((id) => typeof id === "string" && id.length > 0)) {
|
|
644
|
+
throw new TypstError("ids must be a non-empty array of highlight ids; omit it to remove all highlights.", TYPST_INVALID_ARGUMENT);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return { removed: session.clearHighlights(ids) };
|
|
648
|
+
},
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
// ---------------------------------------------------------------------------
|
|
652
|
+
// typst_export_pdf
|
|
653
|
+
// ---------------------------------------------------------------------------
|
|
654
|
+
const PDF_STANDARDS = [
|
|
655
|
+
"a-1b",
|
|
656
|
+
"a-2b",
|
|
657
|
+
"a-2u",
|
|
658
|
+
"a-3b",
|
|
659
|
+
"a-3u",
|
|
660
|
+
"a-4",
|
|
661
|
+
"a-4f",
|
|
662
|
+
];
|
|
663
|
+
function typstExportPdfTool(ctx, service) {
|
|
664
|
+
return defineTool({
|
|
665
|
+
name: "typst_export_pdf",
|
|
666
|
+
description: [
|
|
667
|
+
"Compile the document with the user's Typst and write a PDF into the workspace.",
|
|
668
|
+
"Optionally a PDF standard (pdfStandard) and a page selection.",
|
|
669
|
+
"The preview's own PDF is not in the workspace; this is how you produce the deliverable.",
|
|
670
|
+
].join(" "),
|
|
671
|
+
parameters: {
|
|
672
|
+
path: {
|
|
673
|
+
type: "string",
|
|
674
|
+
required: true,
|
|
675
|
+
description: "Workspace-relative output path ending in .pdf.",
|
|
676
|
+
},
|
|
677
|
+
pdfStandard: {
|
|
678
|
+
type: "string",
|
|
679
|
+
enum: [...PDF_STANDARDS],
|
|
680
|
+
description: "PDF standard to embed.",
|
|
681
|
+
},
|
|
682
|
+
pages: {
|
|
683
|
+
type: "string",
|
|
684
|
+
description: '1-based page selection, e.g. "1-3".',
|
|
685
|
+
},
|
|
686
|
+
},
|
|
687
|
+
output: {
|
|
688
|
+
schema: {
|
|
689
|
+
type: "object",
|
|
690
|
+
properties: {
|
|
691
|
+
path: { type: "string", required: true },
|
|
692
|
+
bytes: { type: "integer", description: "PDF byte size." },
|
|
693
|
+
pages: {
|
|
694
|
+
type: "integer",
|
|
695
|
+
description: "Page count of the exported PDF.",
|
|
696
|
+
},
|
|
697
|
+
summary: { type: "string", required: true },
|
|
698
|
+
},
|
|
699
|
+
additionalProperties: false,
|
|
700
|
+
},
|
|
701
|
+
render: (_args, value) => [{ type: "text", text: value.summary }],
|
|
702
|
+
},
|
|
703
|
+
presentCall: (args) => ({
|
|
704
|
+
card: "generic",
|
|
705
|
+
title: "Export Typst PDF",
|
|
706
|
+
rawInput: String(args.path ?? ""),
|
|
707
|
+
locations: fileLocations(args.path),
|
|
708
|
+
}),
|
|
709
|
+
isConcurrencySafe: () => false,
|
|
710
|
+
async execute(args, exec) {
|
|
711
|
+
const session = await sessionOfExec(ctx, exec, service);
|
|
712
|
+
const policyService = ctx.get("sandboxPolicy");
|
|
713
|
+
if (policyService === undefined) {
|
|
714
|
+
throw new TypstError("The sandbox policy service is not composed; refusing to write a PDF.", TYPST_NOT_COMPOSED);
|
|
715
|
+
}
|
|
716
|
+
const policy = policyService.resolve(exec.agent === undefined ? {} : { session: exec.agent.session });
|
|
717
|
+
const result = await session.exportPdf(args.path, {
|
|
718
|
+
...(typeof args.pdfStandard === "string"
|
|
719
|
+
? { pdfStandard: args.pdfStandard }
|
|
720
|
+
: {}),
|
|
721
|
+
...(typeof args.pages === "string" ? { pages: args.pages } : {}),
|
|
722
|
+
}, exec.signal, policy);
|
|
723
|
+
if (!result.ok) {
|
|
724
|
+
throw new TypstError(`The PDF export failed.\n\n${diagnosticsText(result.diagnostics)}`, TYPST_COMPILE_FAILED);
|
|
725
|
+
}
|
|
726
|
+
return stripAbsent({
|
|
727
|
+
path: result.path,
|
|
728
|
+
...(result.bytes !== undefined ? { bytes: result.bytes } : {}),
|
|
729
|
+
...(result.pages !== undefined ? { pages: result.pages } : {}),
|
|
730
|
+
summary: `Exported ${result.path}${result.bytes !== undefined ? ` (${result.bytes} bytes)` : ""}${result.pages !== undefined ? ` — ${result.pages} page(s)` : ""}.`,
|
|
731
|
+
});
|
|
732
|
+
},
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
// ---------------------------------------------------------------------------
|
|
736
|
+
// Registration
|
|
737
|
+
// ---------------------------------------------------------------------------
|
|
738
|
+
/**
|
|
739
|
+
* Register the seven tools, one effect each (unregistration is the
|
|
740
|
+
* effect's own teardown).
|
|
741
|
+
* @param ctx - the plugin context.
|
|
742
|
+
* @param service - the workbench service the tools resolve sessions through.
|
|
743
|
+
*/
|
|
744
|
+
export function registerTypstTools(ctx, service) {
|
|
745
|
+
const definitions = [
|
|
746
|
+
typstStatusTool(ctx, service),
|
|
747
|
+
typstSetMainTool(ctx, service),
|
|
748
|
+
typstCheckTool(ctx, service),
|
|
749
|
+
typstRenderPagesTool(ctx, service),
|
|
750
|
+
typstShowTool(ctx, service),
|
|
751
|
+
typstClearHighlightsTool(ctx, service),
|
|
752
|
+
typstExportPdfTool(ctx, service),
|
|
753
|
+
];
|
|
754
|
+
for (const definition of definitions) {
|
|
755
|
+
ctx.effect(() => ctx.tools.register(definition), `typst: ${definition.name}`);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
//# sourceMappingURL=registration.js.map
|