@superdoc-dev/sdk 1.8.0-next.8 → 1.8.0-next.80

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.
@@ -55,6 +55,7 @@ function dispatchIntentTool(toolName, args, execute) {
55
55
  case 'create': return execute('doc.lists.create', rest);
56
56
  case 'attach': return execute('doc.lists.attach', rest);
57
57
  case 'detach': return execute('doc.lists.detach', rest);
58
+ case 'delete': return execute('doc.lists.delete', rest);
58
59
  case 'indent': return execute('doc.lists.indent', rest);
59
60
  case 'outdent': return execute('doc.lists.outdent', rest);
60
61
  case 'merge': return execute('doc.lists.merge', rest);
@@ -95,6 +96,29 @@ function dispatchIntentTool(toolName, args, execute) {
95
96
  default: throw new Error(`Unknown action for superdoc_mutations: ${action}`);
96
97
  }
97
98
  }
99
+ case 'superdoc_table': {
100
+ const { action, ...rest } = args;
101
+ switch (action) {
102
+ case 'delete': return execute('doc.tables.delete', rest);
103
+ case 'set_layout': return execute('doc.tables.setLayout', rest);
104
+ case 'insert_row': return execute('doc.tables.insertRow', rest);
105
+ case 'delete_row': return execute('doc.tables.deleteRow', rest);
106
+ case 'set_row': return execute('doc.tables.setRowHeight', rest);
107
+ case 'set_row_options': return execute('doc.tables.setRowOptions', rest);
108
+ case 'insert_column': return execute('doc.tables.insertColumn', rest);
109
+ case 'delete_column': return execute('doc.tables.deleteColumn', rest);
110
+ case 'set_column': return execute('doc.tables.setColumnWidth', rest);
111
+ case 'merge_cells': return execute('doc.tables.mergeCells', rest);
112
+ case 'unmerge_cells': return execute('doc.tables.unmergeCells', rest);
113
+ case 'set_cell': return execute('doc.tables.setCellProperties', rest);
114
+ case 'set_cell_text': return execute('doc.tables.setCellText', rest);
115
+ case 'set_shading': return execute('doc.tables.setShading', rest);
116
+ case 'set_style_options': return execute('doc.tables.applyStyle', rest);
117
+ case 'set_borders': return execute('doc.tables.setBorders', rest);
118
+ case 'set_options': return execute('doc.tables.setTableOptions', rest);
119
+ default: throw new Error(`Unknown action for superdoc_table: ${action}`);
120
+ }
121
+ }
98
122
  default:
99
123
  throw new Error(`Unknown intent tool: ${toolName}`);
100
124
  }
@@ -1 +1 @@
1
- {"version":3,"file":"intent-dispatch.generated.d.ts","sourceRoot":"","sources":["../../src/generated/intent-dispatch.generated.ts"],"names":[],"mappings":"AAEA,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,GACxE,OAAO,CAiGT"}
1
+ {"version":3,"file":"intent-dispatch.generated.d.ts","sourceRoot":"","sources":["../../src/generated/intent-dispatch.generated.ts"],"names":[],"mappings":"AAEA,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,GACxE,OAAO,CAyHT"}
@@ -53,6 +53,7 @@ export function dispatchIntentTool(toolName, args, execute) {
53
53
  case 'create': return execute('doc.lists.create', rest);
54
54
  case 'attach': return execute('doc.lists.attach', rest);
55
55
  case 'detach': return execute('doc.lists.detach', rest);
56
+ case 'delete': return execute('doc.lists.delete', rest);
56
57
  case 'indent': return execute('doc.lists.indent', rest);
57
58
  case 'outdent': return execute('doc.lists.outdent', rest);
58
59
  case 'merge': return execute('doc.lists.merge', rest);
@@ -93,6 +94,29 @@ export function dispatchIntentTool(toolName, args, execute) {
93
94
  default: throw new Error(`Unknown action for superdoc_mutations: ${action}`);
94
95
  }
95
96
  }
97
+ case 'superdoc_table': {
98
+ const { action, ...rest } = args;
99
+ switch (action) {
100
+ case 'delete': return execute('doc.tables.delete', rest);
101
+ case 'set_layout': return execute('doc.tables.setLayout', rest);
102
+ case 'insert_row': return execute('doc.tables.insertRow', rest);
103
+ case 'delete_row': return execute('doc.tables.deleteRow', rest);
104
+ case 'set_row': return execute('doc.tables.setRowHeight', rest);
105
+ case 'set_row_options': return execute('doc.tables.setRowOptions', rest);
106
+ case 'insert_column': return execute('doc.tables.insertColumn', rest);
107
+ case 'delete_column': return execute('doc.tables.deleteColumn', rest);
108
+ case 'set_column': return execute('doc.tables.setColumnWidth', rest);
109
+ case 'merge_cells': return execute('doc.tables.mergeCells', rest);
110
+ case 'unmerge_cells': return execute('doc.tables.unmergeCells', rest);
111
+ case 'set_cell': return execute('doc.tables.setCellProperties', rest);
112
+ case 'set_cell_text': return execute('doc.tables.setCellText', rest);
113
+ case 'set_shading': return execute('doc.tables.setShading', rest);
114
+ case 'set_style_options': return execute('doc.tables.applyStyle', rest);
115
+ case 'set_borders': return execute('doc.tables.setBorders', rest);
116
+ case 'set_options': return execute('doc.tables.setTableOptions', rest);
117
+ default: throw new Error(`Unknown action for superdoc_table: ${action}`);
118
+ }
119
+ }
96
120
  default:
97
121
  throw new Error(`Unknown intent tool: ${toolName}`);
98
122
  }
package/dist/index.cjs CHANGED
@@ -161,6 +161,7 @@ exports.chooseTools = tools.chooseTools;
161
161
  exports.dispatchSuperDocTool = tools.dispatchSuperDocTool;
162
162
  exports.getMcpPrompt = tools.getMcpPrompt;
163
163
  exports.getSystemPrompt = tools.getSystemPrompt;
164
+ exports.getSystemPromptForProvider = tools.getSystemPromptForProvider;
164
165
  exports.getToolCatalog = tools.getToolCatalog;
165
166
  exports.listTools = tools.listTools;
166
167
  exports.dispatchIntentTool = intentDispatch_generated.dispatchIntentTool;
package/dist/index.d.ts CHANGED
@@ -77,10 +77,10 @@ export declare class SuperDocClient {
77
77
  }
78
78
  export declare function createSuperDocClient(options?: SuperDocClientOptions): SuperDocClient;
79
79
  export { getSkill, installSkill, listSkills } from './skills.js';
80
- export { chooseTools, dispatchSuperDocTool, getMcpPrompt, getSystemPrompt, getToolCatalog, listTools, } from './tools.js';
80
+ export { chooseTools, dispatchSuperDocTool, getMcpPrompt, getSystemPrompt, getSystemPromptForProvider, getToolCatalog, listTools, } from './tools.js';
81
+ export type { AnthropicSystemPrompt, CacheStrategy, SystemPromptForProviderResult, ToolChooserInput, ToolProvider, } from './tools.js';
81
82
  export { dispatchIntentTool } from './generated/intent-dispatch.generated.js';
82
83
  export { SuperDocCliError } from './runtime/errors.js';
83
84
  export type { InvokeOptions, OperationSpec, OperationParamSpec, RuntimeInvoker, SuperDocClientOptions, } from './runtime/process.js';
84
- export type { ToolChooserInput, ToolProvider } from './tools.js';
85
85
  export type { DocOpenResult } from './generated/client.js';
86
86
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,aAAa,IAAI,sBAAsB,EAC5C,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACnB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,eAAe,EACf,KAAK,qBAAqB,EAC1B,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,cAAc,EACpB,MAAM,sBAAsB,CAAC;AAO9B;;;;;GAKG;AACH,cAAM,YAAa,YAAW,cAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;IAC1C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,MAAM,CAAS;gBAEX,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM;IAKjD,MAAM,CAAC,KAAK,GAAG,OAAO,EAC1B,SAAS,EAAE,aAAa,EACxB,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,EACpC,OAAO,GAAE,aAAkB,GAC1B,OAAO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S,EAAE,MAAM,GAAG,IAAI;CAGtC;AAED,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,qBAA0B,GAAG,cAAc,CAExF;AAED,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EACL,WAAW,EACX,oBAAoB,EACpB,YAAY,EACZ,eAAe,EACf,0BAA0B,EAC1B,cAAc,EACd,SAAS,GACV,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,qBAAqB,EACrB,aAAa,EACb,6BAA6B,EAC7B,gBAAgB,EAChB,YAAY,GACb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,0CAA0C,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,YAAY,EACV,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,qBAAqB,GACtB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -147,6 +147,6 @@ export function createSuperDocClient(options = {}) {
147
147
  return new SuperDocClient(options);
148
148
  }
149
149
  export { getSkill, installSkill, listSkills } from './skills.js';
150
- export { chooseTools, dispatchSuperDocTool, getMcpPrompt, getSystemPrompt, getToolCatalog, listTools, } from './tools.js';
150
+ export { chooseTools, dispatchSuperDocTool, getMcpPrompt, getSystemPrompt, getSystemPromptForProvider, getToolCatalog, listTools, } from './tools.js';
151
151
  export { dispatchIntentTool } from './generated/intent-dispatch.generated.js';
152
152
  export { SuperDocCliError } from './runtime/errors.js';
package/dist/tools.cjs CHANGED
@@ -72,7 +72,7 @@ async function loadCatalog() {
72
72
  return readJson('catalog.json');
73
73
  }
74
74
  async function getToolCatalog() {
75
- return loadCatalog();
75
+ return getCachedCatalog();
76
76
  }
77
77
  async function listTools(provider) {
78
78
  const bundle = await loadProviderBundle(provider);
@@ -88,24 +88,66 @@ async function listTools(provider) {
88
88
  /**
89
89
  * Select all intent tools for a specific provider.
90
90
  *
91
- * Returns all intent tools in the requested provider format.
91
+ * Returns all intent tools in the requested provider format. Pass
92
+ * `cache: true` to apply provider-specific caching markers (see
93
+ * {@link ToolChooserInput.cache}).
92
94
  *
93
95
  * @example
94
96
  * ```ts
95
- * const { tools } = await chooseTools({ provider: 'openai' });
97
+ * // Anthropic last tool gets cache_control automatically.
98
+ * const { tools, meta } = await chooseTools({ provider: 'anthropic', cache: true });
99
+ *
100
+ * // OpenAI — caching is automatic when prompts exceed 1024 tokens.
101
+ * const { tools } = await chooseTools({ provider: 'openai', cache: true });
96
102
  * ```
97
103
  */
98
104
  async function chooseTools(input) {
99
105
  const bundle = await loadProviderBundle(input.provider);
100
- const tools = Array.isArray(bundle.tools) ? bundle.tools : [];
106
+ const rawTools = Array.isArray(bundle.tools) ? bundle.tools : [];
107
+ const cacheRequested = input.cache === true;
108
+ const { tools, cacheStrategy } = applyCacheMarkers(rawTools, input.provider, cacheRequested);
101
109
  return {
102
110
  tools,
103
111
  meta: {
104
112
  provider: input.provider,
105
113
  toolCount: tools.length,
114
+ cacheStrategy,
106
115
  },
107
116
  };
108
117
  }
118
+ /**
119
+ * Apply provider-specific caching markers to the tools array. Mutates a clone,
120
+ * never the input. Anthropic gets an explicit `cache_control` on the last
121
+ * tool; other providers pass through.
122
+ */
123
+ function applyCacheMarkers(tools, provider, cacheRequested) {
124
+ if (!cacheRequested) {
125
+ return { tools, cacheStrategy: 'disabled' };
126
+ }
127
+ if (provider === 'anthropic') {
128
+ if (tools.length === 0)
129
+ return { tools, cacheStrategy: 'explicit' };
130
+ // Anthropic: marking the LAST tool with cache_control caches the entire
131
+ // tools block (and everything before it in the request — system prompt
132
+ // first if it also has cache_control). Shallow-spread the last entry so we
133
+ // don't mutate the cached bundle in place.
134
+ const next = tools.slice(0, -1);
135
+ const last = {
136
+ ...tools[tools.length - 1],
137
+ cache_control: { type: 'ephemeral' },
138
+ };
139
+ next.push(last);
140
+ return { tools: next, cacheStrategy: 'explicit' };
141
+ }
142
+ if (provider === 'openai') {
143
+ // OpenAI caches prompts ≥ 1024 tokens automatically. No marker needed,
144
+ // but we still report cacheStrategy:'automatic' so callers can branch on
145
+ // it (e.g. for measurement).
146
+ return { tools, cacheStrategy: 'automatic' };
147
+ }
148
+ // vercel / generic — depends on underlying model.
149
+ return { tools, cacheStrategy: 'unsupported' };
150
+ }
109
151
  function resolveDocApiMethod(documentHandle, operationId) {
110
152
  const tokens = operationId.split('.').slice(1);
111
153
  let cursor = documentHandle;
@@ -288,10 +330,55 @@ async function getMcpPrompt() {
288
330
  });
289
331
  }
290
332
  }
333
+ /**
334
+ * Get the system prompt formatted for a specific LLM provider, with optional
335
+ * prompt caching applied.
336
+ *
337
+ * - **anthropic** with `cache: true`: returns a content array with
338
+ * `cache_control: { type: "ephemeral" }` so the system prompt block is
339
+ * cached. Pass directly as the `system` parameter on `messages.create()`.
340
+ * - **openai**: returns the prompt as a string. OpenAI caches prompts
341
+ * ≥ 1024 tokens automatically — `cache: true` is informational only and
342
+ * sets `cacheStrategy: 'automatic'`.
343
+ * - **vercel** / **generic**: returns the prompt as a string. Caching is
344
+ * delegated to the underlying model.
345
+ *
346
+ * @example
347
+ * ```ts
348
+ * // Anthropic
349
+ * const sys = await getSystemPromptForProvider({ provider: 'anthropic', cache: true });
350
+ * await client.messages.create({ system: sys.content, tools, messages, model });
351
+ *
352
+ * // OpenAI
353
+ * const sys = await getSystemPromptForProvider({ provider: 'openai', cache: true });
354
+ * messages.unshift({ role: 'system', content: sys.content });
355
+ * ```
356
+ */
357
+ async function getSystemPromptForProvider(input) {
358
+ const text = await getSystemPrompt();
359
+ const cacheRequested = input.cache === true;
360
+ if (input.provider === 'anthropic') {
361
+ const block = { type: 'text', text };
362
+ if (cacheRequested)
363
+ block.cache_control = { type: 'ephemeral' };
364
+ return {
365
+ provider: 'anthropic',
366
+ content: [block],
367
+ cacheStrategy: cacheRequested ? 'explicit' : 'disabled',
368
+ };
369
+ }
370
+ const cacheStrategy = !cacheRequested
371
+ ? 'disabled'
372
+ : input.provider === 'openai'
373
+ ? 'automatic'
374
+ : 'unsupported';
375
+ return { provider: input.provider, content: text, cacheStrategy };
376
+ }
291
377
 
292
378
  exports.chooseTools = chooseTools;
293
379
  exports.dispatchSuperDocTool = dispatchSuperDocTool;
294
380
  exports.getMcpPrompt = getMcpPrompt;
295
381
  exports.getSystemPrompt = getSystemPrompt;
382
+ exports.getSystemPromptForProvider = getSystemPromptForProvider;
296
383
  exports.getToolCatalog = getToolCatalog;
297
384
  exports.listTools = listTools;
package/dist/tools.d.ts CHANGED
@@ -24,15 +24,37 @@ export declare function getToolCatalog(): Promise<ToolCatalog>;
24
24
  export declare function listTools(provider: ToolProvider): Promise<unknown[]>;
25
25
  export type ToolChooserInput = {
26
26
  provider: ToolProvider;
27
+ /**
28
+ * When `true`, applies provider-specific prompt-caching markers to the
29
+ * returned tools so subsequent identical requests reuse the cached prefix.
30
+ *
31
+ * Per-provider behavior:
32
+ * - **anthropic**: marks the last tool entry with
33
+ * `cache_control: { type: "ephemeral" }`. The full tools block becomes
34
+ * cacheable; cache TTL is ~5 minutes by default.
35
+ * - **openai**: no-op. OpenAI caches prompts ≥ 1024 tokens automatically;
36
+ * the helper returns tools unchanged but still reports
37
+ * `cacheStrategy: 'automatic'` so callers can rely on the indicator.
38
+ * - **vercel** / **generic**: pass-through. Caching depends on the
39
+ * underlying model; reported as `'unsupported'`.
40
+ */
41
+ cache?: boolean;
27
42
  };
43
+ export type CacheStrategy = 'explicit' | 'automatic' | 'unsupported' | 'disabled';
28
44
  /**
29
45
  * Select all intent tools for a specific provider.
30
46
  *
31
- * Returns all intent tools in the requested provider format.
47
+ * Returns all intent tools in the requested provider format. Pass
48
+ * `cache: true` to apply provider-specific caching markers (see
49
+ * {@link ToolChooserInput.cache}).
32
50
  *
33
51
  * @example
34
52
  * ```ts
35
- * const { tools } = await chooseTools({ provider: 'openai' });
53
+ * // Anthropic last tool gets cache_control automatically.
54
+ * const { tools, meta } = await chooseTools({ provider: 'anthropic', cache: true });
55
+ *
56
+ * // OpenAI — caching is automatic when prompts exceed 1024 tokens.
57
+ * const { tools } = await chooseTools({ provider: 'openai', cache: true });
36
58
  * ```
37
59
  */
38
60
  export declare function chooseTools(input: ToolChooserInput): Promise<{
@@ -40,6 +62,7 @@ export declare function chooseTools(input: ToolChooserInput): Promise<{
40
62
  meta: {
41
63
  provider: ToolProvider;
42
64
  toolCount: number;
65
+ cacheStrategy: CacheStrategy;
43
66
  };
44
67
  }>;
45
68
  /**
@@ -64,5 +87,53 @@ export declare function getSystemPrompt(): Promise<string>;
64
87
  * instructions (open/save/close) suitable for MCP server `instructions`.
65
88
  */
66
89
  export declare function getMcpPrompt(): Promise<string>;
90
+ /**
91
+ * Anthropic content block representation of the system prompt with optional
92
+ * `cache_control` for prompt caching.
93
+ */
94
+ export type AnthropicSystemPrompt = Array<{
95
+ type: 'text';
96
+ text: string;
97
+ cache_control?: {
98
+ type: 'ephemeral';
99
+ };
100
+ }>;
101
+ export type SystemPromptForProviderResult = {
102
+ provider: 'anthropic';
103
+ content: AnthropicSystemPrompt;
104
+ cacheStrategy: CacheStrategy;
105
+ } | {
106
+ provider: 'openai' | 'vercel' | 'generic';
107
+ content: string;
108
+ cacheStrategy: CacheStrategy;
109
+ };
110
+ /**
111
+ * Get the system prompt formatted for a specific LLM provider, with optional
112
+ * prompt caching applied.
113
+ *
114
+ * - **anthropic** with `cache: true`: returns a content array with
115
+ * `cache_control: { type: "ephemeral" }` so the system prompt block is
116
+ * cached. Pass directly as the `system` parameter on `messages.create()`.
117
+ * - **openai**: returns the prompt as a string. OpenAI caches prompts
118
+ * ≥ 1024 tokens automatically — `cache: true` is informational only and
119
+ * sets `cacheStrategy: 'automatic'`.
120
+ * - **vercel** / **generic**: returns the prompt as a string. Caching is
121
+ * delegated to the underlying model.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * // Anthropic
126
+ * const sys = await getSystemPromptForProvider({ provider: 'anthropic', cache: true });
127
+ * await client.messages.create({ system: sys.content, tools, messages, model });
128
+ *
129
+ * // OpenAI
130
+ * const sys = await getSystemPromptForProvider({ provider: 'openai', cache: true });
131
+ * messages.unshift({ role: 'system', content: sys.content });
132
+ * ```
133
+ */
134
+ export declare function getSystemPromptForProvider(input: {
135
+ provider: ToolProvider;
136
+ cache?: boolean;
137
+ }): Promise<SystemPromptForProviderResult>;
67
138
  export {};
68
139
  //# sourceMappingURL=tools.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAI1D,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,WAAW,GAAG,QAAQ,GAAG,SAAS,CAAC;AAWzE,MAAM,MAAM,WAAW,GAAG;IACxB,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,gBAAgB,EAAE,CAAC;CAC3B,CAAC;AAEF,KAAK,cAAc,GAAG;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC;CAC5B,CAAC;AAEF,KAAK,gBAAgB,GAAG;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,cAAc,EAAE,CAAC;CAC9B,CAAC;AAmEF,wBAAsB,cAAc,IAAI,OAAO,CAAC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AO,CAAC,CAuClB;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,MAAM,CAAC,CAUvD;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAUpD;AAMD;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,KAAK,CAAC;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE;QAAE,IAAI,EAAE,WAAW,CAAA;KAAE,CAAC;CACvC,CAAC,CAAC;AAEH,MAAM,MAAM,6BAA6B,GACrC;IAAE,QAAQ,EAAE,WAAW,CAAC;IAAC,OAAO,EAAE,qBAAqB,CAAC;IAAC,aAAa,EAAE,aAAa,CAAA;CAAE,GACvF;IAAE,QAAQ,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,aAAa,CAAA;CAAE,CAAC;AAEjG;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,0BAA0B,CAAC,KAAK,EAAE;IACtD,QAAQ,EAAE,YAAY,CAAC;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,GAAG,OAAO,CAAC,6BAA6B,CAAC,CAqBzC"}
package/dist/tools.js CHANGED
@@ -68,7 +68,7 @@ async function loadCatalog() {
68
68
  return readJson('catalog.json');
69
69
  }
70
70
  export async function getToolCatalog() {
71
- return loadCatalog();
71
+ return getCachedCatalog();
72
72
  }
73
73
  export async function listTools(provider) {
74
74
  const bundle = await loadProviderBundle(provider);
@@ -84,24 +84,66 @@ export async function listTools(provider) {
84
84
  /**
85
85
  * Select all intent tools for a specific provider.
86
86
  *
87
- * Returns all intent tools in the requested provider format.
87
+ * Returns all intent tools in the requested provider format. Pass
88
+ * `cache: true` to apply provider-specific caching markers (see
89
+ * {@link ToolChooserInput.cache}).
88
90
  *
89
91
  * @example
90
92
  * ```ts
91
- * const { tools } = await chooseTools({ provider: 'openai' });
93
+ * // Anthropic last tool gets cache_control automatically.
94
+ * const { tools, meta } = await chooseTools({ provider: 'anthropic', cache: true });
95
+ *
96
+ * // OpenAI — caching is automatic when prompts exceed 1024 tokens.
97
+ * const { tools } = await chooseTools({ provider: 'openai', cache: true });
92
98
  * ```
93
99
  */
94
100
  export async function chooseTools(input) {
95
101
  const bundle = await loadProviderBundle(input.provider);
96
- const tools = Array.isArray(bundle.tools) ? bundle.tools : [];
102
+ const rawTools = Array.isArray(bundle.tools) ? bundle.tools : [];
103
+ const cacheRequested = input.cache === true;
104
+ const { tools, cacheStrategy } = applyCacheMarkers(rawTools, input.provider, cacheRequested);
97
105
  return {
98
106
  tools,
99
107
  meta: {
100
108
  provider: input.provider,
101
109
  toolCount: tools.length,
110
+ cacheStrategy,
102
111
  },
103
112
  };
104
113
  }
114
+ /**
115
+ * Apply provider-specific caching markers to the tools array. Mutates a clone,
116
+ * never the input. Anthropic gets an explicit `cache_control` on the last
117
+ * tool; other providers pass through.
118
+ */
119
+ function applyCacheMarkers(tools, provider, cacheRequested) {
120
+ if (!cacheRequested) {
121
+ return { tools, cacheStrategy: 'disabled' };
122
+ }
123
+ if (provider === 'anthropic') {
124
+ if (tools.length === 0)
125
+ return { tools, cacheStrategy: 'explicit' };
126
+ // Anthropic: marking the LAST tool with cache_control caches the entire
127
+ // tools block (and everything before it in the request — system prompt
128
+ // first if it also has cache_control). Shallow-spread the last entry so we
129
+ // don't mutate the cached bundle in place.
130
+ const next = tools.slice(0, -1);
131
+ const last = {
132
+ ...tools[tools.length - 1],
133
+ cache_control: { type: 'ephemeral' },
134
+ };
135
+ next.push(last);
136
+ return { tools: next, cacheStrategy: 'explicit' };
137
+ }
138
+ if (provider === 'openai') {
139
+ // OpenAI caches prompts ≥ 1024 tokens automatically. No marker needed,
140
+ // but we still report cacheStrategy:'automatic' so callers can branch on
141
+ // it (e.g. for measurement).
142
+ return { tools, cacheStrategy: 'automatic' };
143
+ }
144
+ // vercel / generic — depends on underlying model.
145
+ return { tools, cacheStrategy: 'unsupported' };
146
+ }
105
147
  function resolveDocApiMethod(documentHandle, operationId) {
106
148
  const tokens = operationId.split('.').slice(1);
107
149
  let cursor = documentHandle;
@@ -284,3 +326,47 @@ export async function getMcpPrompt() {
284
326
  });
285
327
  }
286
328
  }
329
+ /**
330
+ * Get the system prompt formatted for a specific LLM provider, with optional
331
+ * prompt caching applied.
332
+ *
333
+ * - **anthropic** with `cache: true`: returns a content array with
334
+ * `cache_control: { type: "ephemeral" }` so the system prompt block is
335
+ * cached. Pass directly as the `system` parameter on `messages.create()`.
336
+ * - **openai**: returns the prompt as a string. OpenAI caches prompts
337
+ * ≥ 1024 tokens automatically — `cache: true` is informational only and
338
+ * sets `cacheStrategy: 'automatic'`.
339
+ * - **vercel** / **generic**: returns the prompt as a string. Caching is
340
+ * delegated to the underlying model.
341
+ *
342
+ * @example
343
+ * ```ts
344
+ * // Anthropic
345
+ * const sys = await getSystemPromptForProvider({ provider: 'anthropic', cache: true });
346
+ * await client.messages.create({ system: sys.content, tools, messages, model });
347
+ *
348
+ * // OpenAI
349
+ * const sys = await getSystemPromptForProvider({ provider: 'openai', cache: true });
350
+ * messages.unshift({ role: 'system', content: sys.content });
351
+ * ```
352
+ */
353
+ export async function getSystemPromptForProvider(input) {
354
+ const text = await getSystemPrompt();
355
+ const cacheRequested = input.cache === true;
356
+ if (input.provider === 'anthropic') {
357
+ const block = { type: 'text', text };
358
+ if (cacheRequested)
359
+ block.cache_control = { type: 'ephemeral' };
360
+ return {
361
+ provider: 'anthropic',
362
+ content: [block],
363
+ cacheStrategy: cacheRequested ? 'explicit' : 'disabled',
364
+ };
365
+ }
366
+ const cacheStrategy = !cacheRequested
367
+ ? 'disabled'
368
+ : input.provider === 'openai'
369
+ ? 'automatic'
370
+ : 'unsupported';
371
+ return { provider: input.provider, content: text, cacheStrategy };
372
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdoc-dev/sdk",
3
- "version": "1.8.0-next.8",
3
+ "version": "1.8.0-next.80",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -26,11 +26,11 @@
26
26
  "typescript": "^5.9.2"
27
27
  },
28
28
  "optionalDependencies": {
29
- "@superdoc-dev/sdk-darwin-x64": "1.8.0-next.8",
30
- "@superdoc-dev/sdk-darwin-arm64": "1.8.0-next.8",
31
- "@superdoc-dev/sdk-linux-x64": "1.8.0-next.8",
32
- "@superdoc-dev/sdk-linux-arm64": "1.8.0-next.8",
33
- "@superdoc-dev/sdk-windows-x64": "1.8.0-next.8"
29
+ "@superdoc-dev/sdk-darwin-arm64": "1.8.0-next.80",
30
+ "@superdoc-dev/sdk-darwin-x64": "1.8.0-next.80",
31
+ "@superdoc-dev/sdk-linux-x64": "1.8.0-next.80",
32
+ "@superdoc-dev/sdk-linux-arm64": "1.8.0-next.80",
33
+ "@superdoc-dev/sdk-windows-x64": "1.8.0-next.80"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"