@kenkaiiii/gg-ai 5.77.0 → 5.78.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.js CHANGED
@@ -1130,6 +1130,7 @@ function remapAnthropicToolCallId(id, idMap) {
1130
1130
  function toAnthropicMessages(messages, cacheControl, options) {
1131
1131
  let systemText;
1132
1132
  const out = [];
1133
+ const userTurnIdx = [];
1133
1134
  const idMap = /* @__PURE__ */ new Map();
1134
1135
  const keepFallbackBlocks = options?.fallbackBlocks === true;
1135
1136
  const droppedToolCallIds = /* @__PURE__ */ new Set();
@@ -1150,6 +1151,7 @@ function toAnthropicMessages(messages, cacheControl, options) {
1150
1151
  } else if (!msg.content.some((p) => !(p.type === "text" && p.text === ""))) {
1151
1152
  continue;
1152
1153
  }
1154
+ userTurnIdx.push(out.length);
1153
1155
  out.push({
1154
1156
  role: "user",
1155
1157
  content: typeof msg.content === "string" ? msg.content : msg.content.filter((part) => !(part.type === "text" && part.text === "")).map((part) => {
@@ -1204,28 +1206,21 @@ function toAnthropicMessages(messages, cacheControl, options) {
1204
1206
  }
1205
1207
  }
1206
1208
  if (cacheControl && out.length > 0) {
1207
- for (let i = out.length - 1; i >= 0; i--) {
1208
- if (out[i].role === "user") {
1209
- const content = out[i].content;
1210
- if (typeof content === "string") {
1211
- out[i] = {
1212
- role: "user",
1213
- content: [
1214
- {
1215
- type: "text",
1216
- text: content,
1217
- cache_control: cacheControl
1218
- }
1219
- ]
1220
- };
1221
- } else if (Array.isArray(content) && content.length > 0) {
1222
- const last = content[content.length - 1];
1223
- content[content.length - 1] = {
1224
- ...last,
1225
- cache_control: cacheControl
1226
- };
1209
+ let tail = out.length - 1;
1210
+ while (tail >= 0 && out[tail]?.role !== "user") tail--;
1211
+ if (tail !== -1) {
1212
+ if (cacheControl.ttl === "1h") {
1213
+ const anchor = userTurnIdx.at(-1);
1214
+ if (anchor === tail) {
1215
+ const previous = userTurnIdx.at(-2);
1216
+ if (previous !== void 0) markAnthropicCacheBreakpoint(out, previous, cacheControl);
1217
+ markAnthropicCacheBreakpoint(out, tail, cacheControl);
1218
+ } else {
1219
+ if (anchor !== void 0) markAnthropicCacheBreakpoint(out, anchor, cacheControl);
1220
+ markAnthropicCacheBreakpoint(out, tail, { type: "ephemeral" });
1227
1221
  }
1228
- break;
1222
+ } else {
1223
+ markAnthropicCacheBreakpoint(out, tail, cacheControl);
1229
1224
  }
1230
1225
  }
1231
1226
  }
@@ -1252,6 +1247,25 @@ function toAnthropicMessages(messages, cacheControl, options) {
1252
1247
  }
1253
1248
  return { system, messages: out };
1254
1249
  }
1250
+ function markAnthropicCacheBreakpoint(out, index, cacheControl) {
1251
+ const message = out[index];
1252
+ if (!message) return;
1253
+ const content = message.content;
1254
+ if (typeof content === "string") {
1255
+ out[index] = {
1256
+ role: message.role,
1257
+ content: [
1258
+ { type: "text", text: content, cache_control: cacheControl }
1259
+ ]
1260
+ };
1261
+ } else if (content.length > 0) {
1262
+ const last = content[content.length - 1];
1263
+ content[content.length - 1] = {
1264
+ ...last,
1265
+ cache_control: cacheControl
1266
+ };
1267
+ }
1268
+ }
1255
1269
  function toAnthropicTools(tools, options) {
1256
1270
  return tools.map((tool, index) => {
1257
1271
  const anthropicTool = {