@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 +35 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +35 -21
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
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
|
-
|
|
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 = {
|