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