dsh-typst-plugin 0.1.0-alpha.0 → 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.
Files changed (120) hide show
  1. package/README.en.md +86 -52
  2. package/README.md +93 -59
  3. package/cordis.patch.yml +1 -1
  4. package/docs/images/icon.png +0 -0
  5. package/lib/client.js +2088 -1136
  6. package/lib/client.js.map +4 -4
  7. package/lib/config.d.ts +66 -9
  8. package/lib/config.d.ts.map +1 -1
  9. package/lib/config.js +161 -25
  10. package/lib/config.js.map +1 -7
  11. package/lib/constants.d.ts +21 -2
  12. package/lib/constants.d.ts.map +1 -1
  13. package/lib/constants.js +83 -60
  14. package/lib/constants.js.map +1 -7
  15. package/lib/contract.d.ts +90 -6
  16. package/lib/contract.d.ts.map +1 -1
  17. package/lib/contract.js +9 -1
  18. package/lib/contract.js.map +1 -7
  19. package/lib/diagnostic-text.d.ts +13 -0
  20. package/lib/diagnostic-text.d.ts.map +1 -0
  21. package/lib/diagnostic-text.js +28 -0
  22. package/lib/diagnostic-text.js.map +1 -0
  23. package/lib/errors.d.ts +57 -15
  24. package/lib/errors.d.ts.map +1 -1
  25. package/lib/errors.js +154 -79
  26. package/lib/errors.js.map +1 -7
  27. package/lib/host/facts.d.ts.map +1 -1
  28. package/lib/host/facts.js +133 -139
  29. package/lib/host/facts.js.map +1 -7
  30. package/lib/host/which.d.ts +20 -0
  31. package/lib/host/which.d.ts.map +1 -0
  32. package/lib/host/which.js +50 -0
  33. package/lib/host/which.js.map +1 -0
  34. package/lib/index.js +2 -5
  35. package/lib/index.js.map +1 -7
  36. package/lib/remote.d.ts +5 -4
  37. package/lib/remote.d.ts.map +1 -1
  38. package/lib/remote.js +153 -143
  39. package/lib/remote.js.map +1 -7
  40. package/lib/service.d.ts +28 -4
  41. package/lib/service.d.ts.map +1 -1
  42. package/lib/service.js +202 -99
  43. package/lib/service.js.map +1 -7
  44. package/lib/skills/typst.js +74 -68
  45. package/lib/skills/typst.js.map +1 -7
  46. package/lib/status-codec.js +10 -5
  47. package/lib/status-codec.js.map +1 -7
  48. package/lib/tools/registration.d.ts +4 -3
  49. package/lib/tools/registration.d.ts.map +1 -1
  50. package/lib/tools/registration.js +726 -493
  51. package/lib/tools/registration.js.map +1 -7
  52. package/lib/tools/shared.d.ts +4 -5
  53. package/lib/tools/shared.d.ts.map +1 -1
  54. package/lib/tools/shared.js +73 -64
  55. package/lib/tools/shared.js.map +1 -7
  56. package/lib/typert.d.ts +5 -3
  57. package/lib/typert.d.ts.map +1 -1
  58. package/lib/typert.js +113 -64
  59. package/lib/typert.js.map +1 -7
  60. package/lib/typst/diagnostics.d.ts.map +1 -1
  61. package/lib/typst/diagnostics.js +218 -152
  62. package/lib/typst/diagnostics.js.map +1 -7
  63. package/lib/typst/flags.d.ts +8 -0
  64. package/lib/typst/flags.d.ts.map +1 -0
  65. package/lib/typst/flags.js +8 -0
  66. package/lib/typst/flags.js.map +1 -0
  67. package/lib/typst/locate.d.ts +6 -13
  68. package/lib/typst/locate.d.ts.map +1 -1
  69. package/lib/typst/locate.js +138 -180
  70. package/lib/typst/locate.js.map +1 -7
  71. package/lib/typst/mainfile.d.ts +23 -4
  72. package/lib/typst/mainfile.d.ts.map +1 -1
  73. package/lib/typst/mainfile.js +131 -59
  74. package/lib/typst/mainfile.js.map +1 -7
  75. package/lib/typst/match.d.ts +9 -1
  76. package/lib/typst/match.d.ts.map +1 -1
  77. package/lib/typst/match.js +213 -157
  78. package/lib/typst/match.js.map +1 -7
  79. package/lib/typst/oneshot.d.ts +8 -5
  80. package/lib/typst/oneshot.d.ts.map +1 -1
  81. package/lib/typst/oneshot.js +76 -66
  82. package/lib/typst/oneshot.js.map +1 -7
  83. package/lib/typst/owners.d.ts +28 -0
  84. package/lib/typst/owners.d.ts.map +1 -0
  85. package/lib/typst/owners.js +98 -0
  86. package/lib/typst/owners.js.map +1 -0
  87. package/lib/typst/pdf-extract.d.ts +1 -2
  88. package/lib/typst/pdf-extract.d.ts.map +1 -1
  89. package/lib/typst/pdf-extract.js +98 -74
  90. package/lib/typst/pdf-extract.js.map +1 -7
  91. package/lib/typst/process.d.ts +73 -0
  92. package/lib/typst/process.d.ts.map +1 -0
  93. package/lib/typst/process.js +147 -0
  94. package/lib/typst/process.js.map +1 -0
  95. package/lib/typst/text.d.ts +4 -7
  96. package/lib/typst/text.d.ts.map +1 -1
  97. package/lib/typst/text.js +258 -290
  98. package/lib/typst/text.js.map +1 -7
  99. package/lib/typst/watcher.d.ts +16 -17
  100. package/lib/typst/watcher.d.ts.map +1 -1
  101. package/lib/typst/watcher.js +199 -208
  102. package/lib/typst/watcher.js.map +1 -7
  103. package/lib/wire-schemas.d.ts +8 -153
  104. package/lib/wire-schemas.d.ts.map +1 -1
  105. package/lib/wire-schemas.js +405 -361
  106. package/lib/wire-schemas.js.map +1 -7
  107. package/lib/workspace/containment.d.ts +25 -0
  108. package/lib/workspace/containment.d.ts.map +1 -0
  109. package/lib/workspace/containment.js +54 -0
  110. package/lib/workspace/containment.js.map +1 -0
  111. package/lib/workspace/session.d.ts +73 -14
  112. package/lib/workspace/session.d.ts.map +1 -1
  113. package/lib/workspace/session.js +1516 -1172
  114. package/lib/workspace/session.js.map +1 -7
  115. package/locale/en.json +8 -2
  116. package/locale/zh.json +8 -2
  117. package/package.json +36 -179
  118. package/release-provenance.json +137 -0
  119. package/lib/typst/pdfjs-worker.d.js +0 -1
  120. package/lib/typst/pdfjs-worker.d.js.map +0 -7
@@ -1,525 +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
+ */
1
14
  import { defineTool } from "@deepseek-ai/dsh-tools";
2
15
  import { LIMITS } from "../constants.js";
3
- import {
4
- compileSummary,
5
- countOf,
6
- diagnosticsText,
7
- sessionOfExec
8
- } from "./shared.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";
9
18
  function summarize(session) {
10
- const state = session.state();
11
- const { errors, warnings } = countOf(state.diagnostics);
12
- const pages = state.snapshot?.pages ?? null;
13
- return {
14
- main: state.main,
15
- phase: state.status.phase,
16
- errors,
17
- warnings,
18
- pages,
19
- durationMs: state.status.durationMs ?? null,
20
- summary: compileSummary(
21
- state.status,
22
- state.main,
23
- state.diagnostics,
24
- pages ?? void 0
25
- ),
26
- problems: state.diagnostics.length ? diagnosticsText(state.diagnostics) : ""
27
- };
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
+ };
28
34
  }
29
35
  function textOf(summary) {
30
- return summary.problems !== "" ? `${summary.summary}
31
-
32
- ${summary.problems}` : summary.summary;
36
+ return summary.problems !== ""
37
+ ? `${summary.summary}\n\n${summary.problems}`
38
+ : summary.summary;
33
39
  }
40
+ /** Canonical JSON must be lossless: optional facts are stripped, never null-typed. */
34
41
  function stripAbsent(value) {
35
- return Object.fromEntries(
36
- Object.entries(value).filter(([, v]) => v !== void 0)
37
- );
42
+ return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
38
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
+ // ---------------------------------------------------------------------------
39
58
  function typstStatusTool(ctx, service) {
40
- return defineTool({
41
- name: "typst_status",
42
- description: [
43
- "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.",
44
- "Call this first when working on a Typst document.",
45
- "The live preview panel updates by itself when files are saved; this tool reports the state."
46
- ].join(" "),
47
- parameters: {},
48
- output: {
49
- schema: {
50
- type: "object",
51
- properties: {
52
- toolchain: {
53
- type: "json",
54
- required: true,
55
- description: "Typst toolchain state view."
56
- },
57
- main: {
58
- type: "string",
59
- description: "The chosen main .typ file (workspace-relative)."
60
- },
61
- candidates: {
62
- type: "array",
63
- required: true,
64
- items: { type: "string" },
65
- description: "The workspace's .typ files."
66
- },
67
- phase: {
68
- type: "string",
69
- required: true,
70
- description: "Latest compile phase."
71
- },
72
- errors: { type: "integer", required: true },
73
- warnings: { type: "integer", required: true },
74
- pages: { type: "integer", description: "Preview page count." },
75
- summary: {
76
- type: "string",
77
- required: true,
78
- description: "One-line compile summary."
79
- }
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
+ },
80
135
  },
81
- additionalProperties: false
82
- },
83
- render: (_args, value) => [{ type: "text", text: value.summary }]
84
- },
85
- presentCall: () => ({ card: "generic", title: "Typst status" }),
86
- isConcurrencySafe: () => true,
87
- async execute(_args, exec) {
88
- const session = await sessionOfExec(ctx, exec, service);
89
- await session.activate();
90
- const state = session.state();
91
- const summary = summarize(session);
92
- return stripAbsent({
93
- toolchain: state.toolchain,
94
- ...state.main !== null ? { main: state.main } : {},
95
- candidates: state.candidates,
96
- phase: summary.phase,
97
- errors: summary.errors,
98
- warnings: summary.warnings,
99
- ...summary.pages !== null ? { pages: summary.pages } : {},
100
- summary: summary.summary
101
- });
102
- }
103
- });
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
+ });
104
155
  }
156
+ // ---------------------------------------------------------------------------
157
+ // typst_set_main
158
+ // ---------------------------------------------------------------------------
105
159
  function typstSetMainTool(ctx, service) {
106
- return defineTool({
107
- name: "typst_set_main",
108
- description: [
109
- "Choose the main .typ file of the document (the one you would pass to `typst compile`).",
110
- "The preview restarts with it, and the choice is remembered for this workspace."
111
- ].join(" "),
112
- parameters: {
113
- path: {
114
- type: "string",
115
- required: true,
116
- description: "Workspace-relative path of a .typ file."
117
- }
118
- },
119
- output: {
120
- schema: {
121
- type: "object",
122
- properties: {
123
- main: { type: "string", required: true },
124
- phase: { type: "string", required: true },
125
- errors: { type: "integer", required: true },
126
- warnings: { type: "integer", required: true },
127
- summary: { type: "string", required: true }
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
+ },
128
172
  },
129
- additionalProperties: false
130
- },
131
- render: (_args, value) => [{ type: "text", text: value.summary }]
132
- },
133
- presentCall: (args) => ({
134
- card: "generic",
135
- title: "Set Typst main file",
136
- rawInput: String(args.path ?? "")
137
- }),
138
- isConcurrencySafe: () => false,
139
- async execute(args, exec) {
140
- const session = await sessionOfExec(ctx, exec, service);
141
- const main = await session.setMain(args.path);
142
- await session.settle(LIMITS.checkTimeoutMs).catch(() => void 0);
143
- const summary = summarize(session);
144
- return {
145
- main,
146
- phase: summary.phase,
147
- errors: summary.errors,
148
- warnings: summary.warnings,
149
- summary: textOf(summary)
150
- };
151
- }
152
- });
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
+ });
153
231
  }
232
+ // ---------------------------------------------------------------------------
233
+ // typst_check
234
+ // ---------------------------------------------------------------------------
154
235
  function typstCheckTool(ctx, service) {
155
- return defineTool({
156
- name: "typst_check",
157
- description: [
158
- "Compile the document with the user's Typst and report errors and warnings with file:line:column, the source line and Typst's hints.",
159
- "Waits until every change already saved to disk has been compiled, so call it right after editing.",
160
- "The preview panel updates by itself; this is how you see the result."
161
- ].join(" "),
162
- parameters: {
163
- timeoutMs: {
164
- type: "integer",
165
- description: `How long to wait for Typst in milliseconds (default ${LIMITS.checkTimeoutMs / 1e3} s).`
166
- }
167
- },
168
- output: {
169
- schema: {
170
- type: "object",
171
- properties: {
172
- main: { type: "string", description: "The chosen main .typ file." },
173
- phase: { type: "string", required: true },
174
- errors: { type: "integer", required: true },
175
- warnings: { type: "integer", required: true },
176
- pages: { type: "integer", description: "Preview page count." },
177
- durationMs: { type: "integer", description: "Compile duration." },
178
- summary: { type: "string", required: true },
179
- diagnostics: {
180
- type: "json",
181
- description: "The full diagnostic views."
182
- }
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
+ },
183
248
  },
184
- additionalProperties: false
185
- },
186
- render: (_args, value) => [{ type: "text", text: value.summary }]
187
- },
188
- presentCall: () => ({ card: "generic", title: "Check Typst document" }),
189
- isConcurrencySafe: () => true,
190
- async execute(args, exec) {
191
- const session = await sessionOfExec(ctx, exec, service);
192
- const timeoutMs = typeof args.timeoutMs === "number" && Number.isFinite(args.timeoutMs) ? Math.min(Math.max(args.timeoutMs, 1e3), 6e5) : LIMITS.checkTimeoutMs;
193
- await session.settle(timeoutMs);
194
- const state = session.state();
195
- const summary = summarize(session);
196
- return stripAbsent({
197
- ...state.main !== null ? { main: state.main } : {},
198
- phase: summary.phase,
199
- errors: summary.errors,
200
- warnings: summary.warnings,
201
- ...summary.pages !== null ? { pages: summary.pages } : {},
202
- ...summary.durationMs !== null ? { durationMs: Math.round(summary.durationMs) } : {},
203
- summary: summary.summary,
204
- diagnostics: state.diagnostics
205
- });
206
- }
207
- });
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
+ });
208
363
  }
364
+ // ---------------------------------------------------------------------------
365
+ // typst_render_pages
366
+ // ---------------------------------------------------------------------------
209
367
  function typstRenderPagesTool(ctx, service) {
210
- return defineTool({
211
- name: "typst_render_pages",
212
- description: [
213
- "Render pages of the document to PNG images so you can look at the layout (overfull lines, figure placement, tables, title page, spacing).",
214
- "Compiles the files on disk with the user's Typst.",
215
- `At most ${LIMITS.renderPages} pages per call; default ${LIMITS.ppiDefault} ppi, raise ppi to inspect details.`,
216
- "Compile errors do not catch layout problems: render the affected pages and look at them."
217
- ].join(" "),
218
- parameters: {
219
- pages: {
220
- type: "string",
221
- required: true,
222
- description: '1-based pages, e.g. "1", "2-3", "1,5".'
223
- },
224
- ppi: {
225
- type: "integer",
226
- description: `Resolution in pixels per inch (default ${LIMITS.ppiDefault}).`
227
- }
228
- },
229
- output: {
230
- schema: {
231
- type: "object",
232
- properties: {
233
- pages: {
234
- type: "array",
235
- required: true,
236
- items: { type: "integer" },
237
- description: "The rendered 1-based pages."
238
- },
239
- total: {
240
- type: "integer",
241
- description: "Total pages of the document."
242
- },
243
- images: {
244
- type: "array",
245
- required: true,
246
- items: {
247
- type: "object",
248
- additionalProperties: false,
249
- properties: {
250
- page: { type: "integer", required: true },
251
- dataUrl: { type: "string", required: true }
252
- }
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}).`,
253
385
  },
254
- description: "Rendered PNGs for the tool card."
255
- }
256
386
  },
257
- additionalProperties: false
258
- },
259
- render: (_args, value) => {
260
- const images = value.images;
261
- const rendered = images.map((image) => `page ${image.page}`).join(", ");
262
- const total = value.total !== void 0 ? ` of ${value.total}` : "";
263
- return [
264
- {
265
- type: "text",
266
- text: `Rendered ${rendered}${total}. The images are on the tool card.`
267
- }
268
- ];
269
- },
270
- // The card's data channel: bounded so a pathological render cannot
271
- // bloat the replay record (the toolview shows text above the cap).
272
- presentationMeta: (_args, value) => {
273
- const images = value.images;
274
- const bytes = images.reduce(
275
- (sum, image) => sum + image.dataUrl.length,
276
- 0
277
- );
278
- return {
279
- pages: value.pages,
280
- ...value.total !== void 0 ? { total: value.total } : {},
281
- images: bytes > 6e6 ? [] : images
282
- };
283
- }
284
- },
285
- presentCall: (args) => ({
286
- card: "generic",
287
- title: "Render Typst pages",
288
- rawInput: String(args.pages ?? "")
289
- }),
290
- isConcurrencySafe: () => false,
291
- async execute(args, exec) {
292
- const session = await sessionOfExec(ctx, exec, service);
293
- const ppi = typeof args.ppi === "number" && Number.isFinite(args.ppi) ? Math.min(Math.max(args.ppi, LIMITS.ppiMin), LIMITS.ppiMax) : LIMITS.ppiDefault;
294
- const result = await session.renderPages(args.pages, ppi);
295
- if (!result.ok) {
296
- const error = new Error(
297
- `The document does not compile, so no pages were rendered.
298
-
299
- ${diagnosticsText(result.diagnostics)}`
300
- );
301
- error.name = "TYPST_COMPILE_FAILED";
302
- throw error;
303
- }
304
- return stripAbsent({
305
- pages: result.pages,
306
- ...result.total !== void 0 ? { total: result.total } : {},
307
- images: result.images.map((image) => ({
308
- page: image.page,
309
- dataUrl: `data:image/png;base64,${Buffer.from(image.png).toString("base64")}`
310
- }))
311
- });
312
- }
313
- });
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
+ });
314
487
  }
488
+ // ---------------------------------------------------------------------------
489
+ // typst_show
490
+ // ---------------------------------------------------------------------------
315
491
  function typstShowTool(ctx, service) {
316
- return defineTool({
317
- name: "typst_show",
318
- description: [
319
- "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.",
320
- "Give exactly one of: page; quote (words as they appear in the rendered document \u2014 markup is ignored); or file and line (and optionally endLine) to highlight what those source lines produce.",
321
- "Add a short comment explaining why. Highlights follow the text as the document changes and last for this session."
322
- ].join(" "),
323
- parameters: {
324
- page: { type: "integer", description: "1-based page to request." },
325
- quote: { type: "string", description: "Rendered words to highlight." },
326
- file: { type: "string", description: "Workspace-relative .typ file." },
327
- line: { type: "integer", description: "1-based line in that file." },
328
- endLine: {
329
- type: "integer",
330
- description: "Last line of the range (default: line)."
331
- },
332
- comment: {
333
- type: "string",
334
- description: "Short comment shown with the highlight."
335
- }
336
- },
337
- output: {
338
- schema: {
339
- type: "object",
340
- properties: {
341
- highlighted: { type: "boolean", required: true },
342
- exact: {
343
- type: "boolean",
344
- description: "Whether the quote matched exactly."
345
- },
346
- id: { type: "string", description: "The highlight id." },
347
- page: {
348
- type: "integer",
349
- description: "The 1-based page requested for viewing."
350
- }
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
+ },
351
512
  },
352
- additionalProperties: false
353
- },
354
- render: (_args, value) => [
355
- {
356
- type: "text",
357
- text: value.highlighted ? "Highlighted in the preview." : `Requested viewing page ${value.page}; the user can open the location notification.`
358
- }
359
- ]
360
- },
361
- presentCall: (args) => ({
362
- card: "generic",
363
- title: "Show in preview",
364
- rawInput: String(
365
- args.quote ?? args.file ?? (args.page !== void 0 ? `page ${args.page}` : "")
366
- )
367
- }),
368
- isConcurrencySafe: () => false,
369
- async execute(args, exec) {
370
- const session = await sessionOfExec(ctx, exec, service);
371
- if (typeof args.page === "number") {
372
- session.showPage(args.page - 1);
373
- return { highlighted: false, page: args.page };
374
- }
375
- const target = {};
376
- if (typeof args.quote === "string") target.quote = args.quote;
377
- if (typeof args.file === "string") {
378
- target.file = args.file;
379
- if (typeof args.line === "number") target.line = args.line;
380
- if (typeof args.endLine === "number") target.endLine = args.endLine;
381
- }
382
- const created = await session.highlight(
383
- target,
384
- typeof args.comment === "string" ? args.comment : ""
385
- );
386
- return {
387
- highlighted: true,
388
- exact: created.exact,
389
- id: created.highlight.id
390
- };
391
- }
392
- });
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
+ });
393
610
  }
611
+ // ---------------------------------------------------------------------------
612
+ // typst_clear_highlights
613
+ // ---------------------------------------------------------------------------
394
614
  function typstClearHighlightsTool(ctx, service) {
395
- return defineTool({
396
- name: "typst_clear_highlights",
397
- description: "Remove the highlights you placed in the live preview (all of them, or the named ids).",
398
- parameters: {
399
- ids: {
400
- type: "json",
401
- description: "Highlight ids to remove; omit to remove all."
402
- }
403
- },
404
- output: {
405
- schema: {
406
- type: "object",
407
- properties: { removed: { type: "integer", required: true } },
408
- additionalProperties: false
409
- },
410
- render: (_args, value) => [
411
- { type: "text", text: `Removed ${value.removed} highlight(s).` }
412
- ]
413
- },
414
- presentCall: () => ({ card: "generic", title: "Clear preview highlights" }),
415
- isConcurrencySafe: () => true,
416
- async execute(args, exec) {
417
- const session = await sessionOfExec(ctx, exec, service);
418
- const ids = args.ids;
419
- const removed = session.clearHighlights(
420
- Array.isArray(ids) ? ids.map(String) : void 0
421
- );
422
- return { removed };
423
- }
424
- });
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
+ });
425
650
  }
651
+ // ---------------------------------------------------------------------------
652
+ // typst_export_pdf
653
+ // ---------------------------------------------------------------------------
426
654
  const PDF_STANDARDS = [
427
- "a-1b",
428
- "a-2b",
429
- "a-2u",
430
- "a-3b",
431
- "a-3u",
432
- "a-4",
433
- "a-4f"
655
+ "a-1b",
656
+ "a-2b",
657
+ "a-2u",
658
+ "a-3b",
659
+ "a-3u",
660
+ "a-4",
661
+ "a-4f",
434
662
  ];
435
663
  function typstExportPdfTool(ctx, service) {
436
- return defineTool({
437
- name: "typst_export_pdf",
438
- description: [
439
- "Compile the document with the user's Typst and write a PDF into the workspace.",
440
- "Optionally a PDF standard (pdfStandard) and a page selection.",
441
- "The preview's own PDF is not in the workspace; this is how you produce the deliverable."
442
- ].join(" "),
443
- parameters: {
444
- path: {
445
- type: "string",
446
- required: true,
447
- description: "Workspace-relative output path ending in .pdf."
448
- },
449
- pdfStandard: {
450
- type: "string",
451
- enum: [...PDF_STANDARDS],
452
- description: "PDF standard to embed."
453
- },
454
- pages: {
455
- type: "string",
456
- description: '1-based page selection, e.g. "1-3".'
457
- }
458
- },
459
- output: {
460
- schema: {
461
- type: "object",
462
- properties: {
463
- path: { type: "string", required: true },
464
- bytes: { type: "integer", description: "PDF byte size." },
465
- pages: {
466
- type: "integer",
467
- description: "Page count of the exported PDF."
468
- },
469
- summary: { type: "string", required: true }
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
+ },
470
686
  },
471
- additionalProperties: false
472
- },
473
- render: (_args, value) => [{ type: "text", text: value.summary }]
474
- },
475
- presentCall: (args) => ({
476
- card: "generic",
477
- title: "Export Typst PDF",
478
- rawInput: String(args.path ?? "")
479
- }),
480
- isConcurrencySafe: () => false,
481
- async execute(args, exec) {
482
- const session = await sessionOfExec(ctx, exec, service);
483
- const result = await session.exportPdf(args.path, {
484
- ...typeof args.pdfStandard === "string" ? { pdfStandard: args.pdfStandard } : {},
485
- ...typeof args.pages === "string" ? { pages: args.pages } : {}
486
- });
487
- if (!result.ok) {
488
- const error = new Error(
489
- `The PDF export failed.
490
-
491
- ${diagnosticsText(result.diagnostics)}`
492
- );
493
- error.name = "TYPST_COMPILE_FAILED";
494
- throw error;
495
- }
496
- return stripAbsent({
497
- path: result.path,
498
- ...result.bytes !== void 0 ? { bytes: result.bytes } : {},
499
- ...result.pages !== void 0 ? { pages: result.pages } : {},
500
- summary: `Exported ${result.path}${result.bytes !== void 0 ? ` (${result.bytes} bytes)` : ""}${result.pages !== void 0 ? ` \u2014 ${result.pages} page(s)` : ""}.`
501
- });
502
- }
503
- });
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
+ });
504
734
  }
505
- function registerTypstTools(ctx, service) {
506
- const definitions = [
507
- typstStatusTool(ctx, service),
508
- typstSetMainTool(ctx, service),
509
- typstCheckTool(ctx, service),
510
- typstRenderPagesTool(ctx, service),
511
- typstShowTool(ctx, service),
512
- typstClearHighlightsTool(ctx, service),
513
- typstExportPdfTool(ctx, service)
514
- ];
515
- for (const definition of definitions) {
516
- ctx.effect(
517
- () => ctx.tools.register(definition),
518
- `typst: ${definition.name}`
519
- );
520
- }
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
+ }
521
757
  }
522
- export {
523
- registerTypstTools
524
- };
525
- //# sourceMappingURL=registration.js.map
758
+ //# sourceMappingURL=registration.js.map