@kenkaiiii/gg-ai 5.77.0 → 5.79.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.
package/dist/index.cjs CHANGED
@@ -1207,6 +1207,7 @@ function remapAnthropicToolCallId(id, idMap) {
1207
1207
  function toAnthropicMessages(messages, cacheControl, options) {
1208
1208
  let systemText;
1209
1209
  const out = [];
1210
+ const userTurnIdx = [];
1210
1211
  const idMap = /* @__PURE__ */ new Map();
1211
1212
  const keepFallbackBlocks = options?.fallbackBlocks === true;
1212
1213
  const droppedToolCallIds = /* @__PURE__ */ new Set();
@@ -1227,6 +1228,7 @@ function toAnthropicMessages(messages, cacheControl, options) {
1227
1228
  } else if (!msg.content.some((p) => !(p.type === "text" && p.text === ""))) {
1228
1229
  continue;
1229
1230
  }
1231
+ userTurnIdx.push(out.length);
1230
1232
  out.push({
1231
1233
  role: "user",
1232
1234
  content: typeof msg.content === "string" ? msg.content : msg.content.filter((part) => !(part.type === "text" && part.text === "")).map((part) => {
@@ -1281,28 +1283,21 @@ function toAnthropicMessages(messages, cacheControl, options) {
1281
1283
  }
1282
1284
  }
1283
1285
  if (cacheControl && out.length > 0) {
1284
- for (let i = out.length - 1; i >= 0; i--) {
1285
- if (out[i].role === "user") {
1286
- const content = out[i].content;
1287
- if (typeof content === "string") {
1288
- out[i] = {
1289
- role: "user",
1290
- content: [
1291
- {
1292
- type: "text",
1293
- text: content,
1294
- cache_control: cacheControl
1295
- }
1296
- ]
1297
- };
1298
- } else if (Array.isArray(content) && content.length > 0) {
1299
- const last = content[content.length - 1];
1300
- content[content.length - 1] = {
1301
- ...last,
1302
- cache_control: cacheControl
1303
- };
1286
+ let tail = out.length - 1;
1287
+ while (tail >= 0 && out[tail]?.role !== "user") tail--;
1288
+ if (tail !== -1) {
1289
+ if (cacheControl.ttl === "1h") {
1290
+ const anchor = userTurnIdx.at(-1);
1291
+ if (anchor === tail) {
1292
+ const previous = userTurnIdx.at(-2);
1293
+ if (previous !== void 0) markAnthropicCacheBreakpoint(out, previous, cacheControl);
1294
+ markAnthropicCacheBreakpoint(out, tail, cacheControl);
1295
+ } else {
1296
+ if (anchor !== void 0) markAnthropicCacheBreakpoint(out, anchor, cacheControl);
1297
+ markAnthropicCacheBreakpoint(out, tail, { type: "ephemeral" });
1304
1298
  }
1305
- break;
1299
+ } else {
1300
+ markAnthropicCacheBreakpoint(out, tail, cacheControl);
1306
1301
  }
1307
1302
  }
1308
1303
  }
@@ -1329,6 +1324,25 @@ function toAnthropicMessages(messages, cacheControl, options) {
1329
1324
  }
1330
1325
  return { system, messages: out };
1331
1326
  }
1327
+ function markAnthropicCacheBreakpoint(out, index, cacheControl) {
1328
+ const message = out[index];
1329
+ if (!message) return;
1330
+ const content = message.content;
1331
+ if (typeof content === "string") {
1332
+ out[index] = {
1333
+ role: message.role,
1334
+ content: [
1335
+ { type: "text", text: content, cache_control: cacheControl }
1336
+ ]
1337
+ };
1338
+ } else if (content.length > 0) {
1339
+ const last = content[content.length - 1];
1340
+ content[content.length - 1] = {
1341
+ ...last,
1342
+ cache_control: cacheControl
1343
+ };
1344
+ }
1345
+ }
1332
1346
  function toAnthropicTools(tools, options) {
1333
1347
  return tools.map((tool, index) => {
1334
1348
  const anthropicTool = {