@heripo/document-processor 0.2.1 → 0.2.3

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/dist/index.js CHANGED
@@ -92,6 +92,50 @@ function detectProvider(model) {
92
92
  if (providerId.includes("lmstudio")) return "lmstudio";
93
93
  return "unknown";
94
94
  }
95
+ function withSystemPromptCache(model, systemPrompt, providerOptions) {
96
+ const unchanged = {
97
+ ...systemPrompt === void 0 ? {} : { instructions: systemPrompt },
98
+ ...providerOptions === void 0 ? {} : { providerOptions }
99
+ };
100
+ const provider = detectProvider(model);
101
+ if (provider === "anthropic")
102
+ return systemPrompt ? {
103
+ ...unchanged,
104
+ instructions: cachedSystem(systemPrompt, {
105
+ anthropic: { cacheControl: { type: "ephemeral" } }
106
+ })
107
+ } : unchanged;
108
+ if (provider !== "openai" || !supportsOpenAICacheBreakpoints(modelIdOf(model)) || providerOptions?.openai?.promptCacheOptions !== void 0)
109
+ return unchanged;
110
+ return {
111
+ ...systemPrompt ? {
112
+ instructions: cachedSystem(systemPrompt, {
113
+ openai: { promptCacheBreakpoint: { mode: "explicit" } }
114
+ })
115
+ } : unchanged,
116
+ providerOptions: {
117
+ ...providerOptions,
118
+ openai: {
119
+ ...providerOptions?.openai,
120
+ promptCacheOptions: { mode: "explicit" }
121
+ }
122
+ }
123
+ };
124
+ }
125
+ function cachedSystem(content, providerOptions) {
126
+ return { role: "system", content, providerOptions };
127
+ }
128
+ function modelIdOf(model) {
129
+ const modelId = model.modelId;
130
+ return typeof modelId === "string" ? modelId : "";
131
+ }
132
+ function supportsOpenAICacheBreakpoints(modelId) {
133
+ const version = /^(?:openai\/)?gpt-(\d+)(?:\.(\d+))?(?:-.+)?$/u.exec(modelId);
134
+ if (!version) return false;
135
+ const major = Number(version[1]);
136
+ const minor = Number(version[2] ?? 0);
137
+ return major > 5 || major === 5 && minor >= 6;
138
+ }
95
139
  function cacheWrite1hTokens(steps) {
96
140
  let total;
97
141
  for (const step of steps ?? []) {
@@ -293,7 +337,8 @@ var LLMCaller = class {
293
337
  * 2. If all fail and fallbackModel provided, try fallback up to maxRetries times
294
338
  * 3. Throw error if all attempts exhausted
295
339
  *
296
- * Provider-aware strategy is automatically applied based on the model's provider field.
340
+ * Provider-aware strategy is automatically applied based on the model's provider field, and the prompt cache
341
+ * breakpoint is placed only at the end of the system prompt (see `withSystemPromptCache`).
297
342
  *
298
343
  * @template TOutput - Output type from schema validation
299
344
  * @param config - LLM call configuration
@@ -304,17 +349,12 @@ var LLMCaller = class {
304
349
  return this.executeWithFallback(
305
350
  config,
306
351
  (model) => this.generateStructuredOutput(model, config.schema, {
307
- instructions: config.cacheSystemPrompt ? {
308
- role: "system",
309
- content: config.systemPrompt,
310
- providerOptions: {
311
- anthropic: { cacheControl: { type: "ephemeral" } }
312
- }
313
- } : config.systemPrompt,
352
+ ...withSystemPromptCache(
353
+ model,
354
+ config.systemPrompt,
355
+ config.providerOptions
356
+ ),
314
357
  prompt: config.userPrompt,
315
- ...config.providerOptions && {
316
- providerOptions: config.providerOptions
317
- },
318
358
  temperature: config.temperature,
319
359
  maxRetries: config.maxRetries,
320
360
  abortSignal: config.abortSignal
@@ -337,6 +377,7 @@ var LLMCaller = class {
337
377
  config,
338
378
  (model) => this.generateStructuredOutput(model, config.schema, {
339
379
  messages: config.messages,
380
+ ...withSystemPromptCache(model, void 0, void 0),
340
381
  temperature: config.temperature,
341
382
  maxRetries: config.maxRetries,
342
383
  abortSignal: config.abortSignal