@codeproxy/core 0.1.5 → 0.1.6

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
@@ -43,13 +43,21 @@ var DEFAULT_REASONING_BUDGETS = {
43
43
  function translateRequest(data, options = {}) {
44
44
  const model = data.model;
45
45
  const maxTokens = typeof data.max_output_tokens === "number" && data.max_output_tokens || typeof data.max_tokens === "number" && data.max_tokens || options.defaultMaxTokens || 8192;
46
- const systemBlocks = extractSystemBlocks(data.instructions);
46
+ let systemBlocks = extractSystemBlocks(data.instructions);
47
47
  const built = buildMessages(data, systemBlocks);
48
48
  let messages = built.messages;
49
- const hasPromptCache = built.hasPromptCache;
49
+ let hasPromptCache = built.hasPromptCache;
50
+ if (data.prompt_cache_key) {
51
+ hasPromptCache = true;
52
+ systemBlocks = markBlocksForCache(systemBlocks);
53
+ messages = markCacheBreakpoint(messages);
54
+ }
50
55
  messages = repairToolAdjacency(messages);
51
56
  messages = sanitizeMessages(messages);
52
57
  messages = ensureEndsWithUser(messages);
58
+ if (data.prompt_cache_key) {
59
+ messages = markCacheBreakpoint(messages);
60
+ }
53
61
  const request = {
54
62
  model,
55
63
  messages,
@@ -190,7 +198,15 @@ function buildMessages(data, systemBlocks) {
190
198
  } else if (part && typeof part === "object") {
191
199
  const contentPart = part;
192
200
  if (contentPart.type === "input_text" || contentPart.type === "text" || contentPart.type === "output_text") {
193
- contentBlocks.push({ type: "text", text: String(contentPart.text ?? "") });
201
+ const textBlock = {
202
+ type: "text",
203
+ text: String(contentPart.text ?? "")
204
+ };
205
+ const cc = contentPart.cache_control;
206
+ if (cc) {
207
+ textBlock.cache_control = cc;
208
+ }
209
+ contentBlocks.push(textBlock);
194
210
  } else if (contentPart.type === "input_image" || contentPart.type === "image" || contentPart.type === "image_url") {
195
211
  const imgUrlPart = contentPart;
196
212
  const imgUrl = imgUrlPart.image_url;
@@ -508,6 +524,43 @@ function ensureEndsWithUser(messages) {
508
524
  }
509
525
  return [...messages, { role: "user", content: [{ type: "text", text: "Continue." }] }];
510
526
  }
527
+ function markBlocksForCache(blocks) {
528
+ for (const block of blocks) {
529
+ if (!block.cache_control) {
530
+ block.cache_control = { type: "ephemeral" };
531
+ }
532
+ }
533
+ return blocks;
534
+ }
535
+ function markCacheBreakpoint(messages) {
536
+ for (const msg of messages) {
537
+ if (msg.role === "assistant" && Array.isArray(msg.content)) {
538
+ for (let j = msg.content.length - 1; j >= 0; j--) {
539
+ const block = msg.content[j];
540
+ if (block.type === "text") {
541
+ if (!block.cache_control) {
542
+ block.cache_control = { type: "ephemeral" };
543
+ }
544
+ return messages;
545
+ }
546
+ }
547
+ }
548
+ }
549
+ for (const msg of messages) {
550
+ if (msg.role === "user" && Array.isArray(msg.content)) {
551
+ for (let j = msg.content.length - 1; j >= 0; j--) {
552
+ const block = msg.content[j];
553
+ if (block.type === "text") {
554
+ if (!block.cache_control) {
555
+ block.cache_control = { type: "ephemeral" };
556
+ }
557
+ return messages;
558
+ }
559
+ }
560
+ }
561
+ }
562
+ return messages;
563
+ }
511
564
 
512
565
  // src/utils/json.ts
513
566
  function safeJsonParse(text) {