@openhoo/hoopilot 2.1.2 → 2.1.3
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/cli.js +130 -31
- package/dist/cli.js.map +1 -1
- package/dist/index.js +130 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -195,7 +195,7 @@ function chatCompletionToResponse(completion, responseId) {
|
|
|
195
195
|
}
|
|
196
196
|
function responsesCompactionResult(upstreamText, isSse) {
|
|
197
197
|
const summary = compactionSummaryText(upstreamText, isSse);
|
|
198
|
-
return { output: [
|
|
198
|
+
return { output: [compactionSummaryOutputMessageItem(summary)] };
|
|
199
199
|
}
|
|
200
200
|
function isResponsesCompactionRequest(request) {
|
|
201
201
|
return responseInputItems(request.input).some(
|
|
@@ -839,8 +839,14 @@ function messageOutputItem(text, id = `msg_${randomId()}`) {
|
|
|
839
839
|
type: "message"
|
|
840
840
|
};
|
|
841
841
|
}
|
|
842
|
-
function
|
|
843
|
-
return {
|
|
842
|
+
function compactionSummaryOutputMessageItem(text) {
|
|
843
|
+
return compactionSummaryMessageItem(text, `msg_${randomId()}`);
|
|
844
|
+
}
|
|
845
|
+
function compactionSummaryInputMessageItem(text) {
|
|
846
|
+
return compactionSummaryMessageItem(text);
|
|
847
|
+
}
|
|
848
|
+
function compactionSummaryMessageItem(text, id) {
|
|
849
|
+
return removeUndefined({
|
|
844
850
|
content: [
|
|
845
851
|
{
|
|
846
852
|
text: `${COMPACTION_SUMMARY_PREFIX}
|
|
@@ -851,7 +857,7 @@ ${text}`,
|
|
|
851
857
|
id,
|
|
852
858
|
role: "user",
|
|
853
859
|
type: "message"
|
|
854
|
-
};
|
|
860
|
+
});
|
|
855
861
|
}
|
|
856
862
|
function compactionOutputItem(text, id = `cmpct_${randomId()}`) {
|
|
857
863
|
return {
|
|
@@ -875,7 +881,7 @@ function normalizeCompactionInputForCopilot(input, options) {
|
|
|
875
881
|
if (type === "compaction" || type === "compaction_summary" || type === "context_compaction") {
|
|
876
882
|
const text = contentToText(record.encrypted_content);
|
|
877
883
|
if (text) {
|
|
878
|
-
normalized.push(
|
|
884
|
+
normalized.push(compactionSummaryInputMessageItem(text));
|
|
879
885
|
}
|
|
880
886
|
continue;
|
|
881
887
|
}
|
|
@@ -956,6 +962,7 @@ function extractTokenUsage(usage) {
|
|
|
956
962
|
asRecord(record.output_tokens_details).reasoning_tokens
|
|
957
963
|
);
|
|
958
964
|
const cached = firstNumber(
|
|
965
|
+
record.cache_read_input_tokens,
|
|
959
966
|
asRecord(record.prompt_tokens_details).cached_tokens,
|
|
960
967
|
asRecord(record.input_tokens_details).cached_tokens
|
|
961
968
|
);
|
|
@@ -1128,9 +1135,10 @@ var AnthropicCompatibilityError = class extends Error {
|
|
|
1128
1135
|
}
|
|
1129
1136
|
};
|
|
1130
1137
|
function anthropicMessagesToResponsesRequest(request) {
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1138
|
+
const system = anthropicSystemToResponses(request.system);
|
|
1139
|
+
const response = removeUndefined({
|
|
1140
|
+
input: [...system.input, ...anthropicMessagesToResponsesInput(request.messages)],
|
|
1141
|
+
instructions: system.instructions,
|
|
1134
1142
|
max_output_tokens: typeof request.max_tokens === "number" && Number.isFinite(request.max_tokens) ? request.max_tokens : void 0,
|
|
1135
1143
|
metadata: request.metadata,
|
|
1136
1144
|
model: normalizeRequestedModel(request.model),
|
|
@@ -1143,6 +1151,8 @@ function anthropicMessagesToResponsesRequest(request) {
|
|
|
1143
1151
|
tools: anthropicTools(request.tools),
|
|
1144
1152
|
top_p: request.top_p
|
|
1145
1153
|
});
|
|
1154
|
+
applyCacheControlToLastBlock(response, anthropicCacheControl(request.cache_control));
|
|
1155
|
+
return response;
|
|
1146
1156
|
}
|
|
1147
1157
|
function responsesResponseToAnthropicMessage(response, fallbackModel) {
|
|
1148
1158
|
const content = anthropicContentFromResponsesOutput(response);
|
|
@@ -1239,6 +1249,7 @@ function anthropicMessagesToResponsesInput(messages) {
|
|
|
1239
1249
|
throw new AnthropicCompatibilityError("Anthropic Messages requests require messages[].");
|
|
1240
1250
|
}
|
|
1241
1251
|
const input = [];
|
|
1252
|
+
let fallbackToolCallIndex = 0;
|
|
1242
1253
|
for (const message of messages) {
|
|
1243
1254
|
const record = asRecord(message);
|
|
1244
1255
|
const role = anthropicRole(record.role);
|
|
@@ -1260,10 +1271,13 @@ function anthropicMessagesToResponsesInput(messages) {
|
|
|
1260
1271
|
if (type === "text") {
|
|
1261
1272
|
const text = textValue(part.text);
|
|
1262
1273
|
if (text) {
|
|
1263
|
-
messageParts.push(
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1274
|
+
messageParts.push(
|
|
1275
|
+
removeUndefined({
|
|
1276
|
+
cache_control: anthropicCacheControl(part.cache_control),
|
|
1277
|
+
text,
|
|
1278
|
+
type: role === "assistant" ? "output_text" : "input_text"
|
|
1279
|
+
})
|
|
1280
|
+
);
|
|
1267
1281
|
}
|
|
1268
1282
|
continue;
|
|
1269
1283
|
}
|
|
@@ -1278,21 +1292,27 @@ function anthropicMessagesToResponsesInput(messages) {
|
|
|
1278
1292
|
}
|
|
1279
1293
|
if (type === "tool_use") {
|
|
1280
1294
|
flushMessage();
|
|
1281
|
-
input.push(
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1295
|
+
input.push(
|
|
1296
|
+
removeUndefined({
|
|
1297
|
+
arguments: JSON.stringify(asRecord(part.input)),
|
|
1298
|
+
cache_control: anthropicCacheControl(part.cache_control),
|
|
1299
|
+
call_id: textValue(part.id) || `call_hoopilot_${fallbackToolCallIndex++}`,
|
|
1300
|
+
name: textValue(part.name),
|
|
1301
|
+
type: "function_call"
|
|
1302
|
+
})
|
|
1303
|
+
);
|
|
1287
1304
|
continue;
|
|
1288
1305
|
}
|
|
1289
1306
|
if (type === "tool_result") {
|
|
1290
1307
|
flushMessage();
|
|
1291
|
-
input.push(
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1308
|
+
input.push(
|
|
1309
|
+
removeUndefined({
|
|
1310
|
+
cache_control: anthropicCacheControl(part.cache_control),
|
|
1311
|
+
call_id: textValue(part.tool_use_id),
|
|
1312
|
+
output: anthropicToolResultOutput(part.content),
|
|
1313
|
+
type: "function_call_output"
|
|
1314
|
+
})
|
|
1315
|
+
);
|
|
1296
1316
|
continue;
|
|
1297
1317
|
}
|
|
1298
1318
|
if (type === "thinking" || type === "redacted_thinking") {
|
|
@@ -1339,22 +1359,24 @@ function anthropicImageToResponsesPart(part) {
|
|
|
1339
1359
|
if (!data) {
|
|
1340
1360
|
throw new AnthropicCompatibilityError("Anthropic base64 image content requires source.data.");
|
|
1341
1361
|
}
|
|
1342
|
-
return {
|
|
1362
|
+
return removeUndefined({
|
|
1363
|
+
cache_control: anthropicCacheControl(part.cache_control),
|
|
1343
1364
|
detail: "auto",
|
|
1344
1365
|
image_url: `data:${mediaType};base64,${data}`,
|
|
1345
1366
|
type: "input_image"
|
|
1346
|
-
};
|
|
1367
|
+
});
|
|
1347
1368
|
}
|
|
1348
1369
|
if (sourceType === "url") {
|
|
1349
1370
|
const url = textValue(source.url);
|
|
1350
1371
|
if (!url) {
|
|
1351
1372
|
throw new AnthropicCompatibilityError("Anthropic URL image content requires source.url.");
|
|
1352
1373
|
}
|
|
1353
|
-
return {
|
|
1374
|
+
return removeUndefined({
|
|
1375
|
+
cache_control: anthropicCacheControl(part.cache_control),
|
|
1354
1376
|
detail: "auto",
|
|
1355
1377
|
image_url: url,
|
|
1356
1378
|
type: "input_image"
|
|
1357
|
-
};
|
|
1379
|
+
});
|
|
1358
1380
|
}
|
|
1359
1381
|
throw new AnthropicCompatibilityError(
|
|
1360
1382
|
`Anthropic image source type "${sourceType || "unknown"}" is not supported.`
|
|
@@ -1375,15 +1397,42 @@ function anthropicToolResultOutput(content) {
|
|
|
1375
1397
|
}
|
|
1376
1398
|
return typeof content === "object" ? JSON.stringify(content) : String(content);
|
|
1377
1399
|
}
|
|
1378
|
-
function
|
|
1400
|
+
function anthropicSystemToResponses(system) {
|
|
1379
1401
|
if (typeof system === "string") {
|
|
1380
|
-
return system || void 0;
|
|
1402
|
+
return { input: [], instructions: system || void 0 };
|
|
1381
1403
|
}
|
|
1382
1404
|
if (!Array.isArray(system)) {
|
|
1405
|
+
return { input: [] };
|
|
1406
|
+
}
|
|
1407
|
+
const parts = system.map((part) => anthropicSystemPartToResponsesPart(part)).filter((part) => part !== void 0);
|
|
1408
|
+
if (parts.length === 0) {
|
|
1409
|
+
return { input: [] };
|
|
1410
|
+
}
|
|
1411
|
+
if (parts.some((part) => part.cache_control !== void 0)) {
|
|
1412
|
+
return {
|
|
1413
|
+
input: [
|
|
1414
|
+
{
|
|
1415
|
+
content: parts,
|
|
1416
|
+
role: "system",
|
|
1417
|
+
type: "message"
|
|
1418
|
+
}
|
|
1419
|
+
]
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
const text = parts.map((part) => textValue(part.text)).filter(Boolean).join("\n");
|
|
1423
|
+
return { input: [], instructions: text || void 0 };
|
|
1424
|
+
}
|
|
1425
|
+
function anthropicSystemPartToResponsesPart(part) {
|
|
1426
|
+
const record = asRecord(part);
|
|
1427
|
+
const text = textValue(record.text) || textValue(part);
|
|
1428
|
+
if (!text) {
|
|
1383
1429
|
return void 0;
|
|
1384
1430
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1431
|
+
return removeUndefined({
|
|
1432
|
+
cache_control: anthropicCacheControl(record.cache_control),
|
|
1433
|
+
text,
|
|
1434
|
+
type: "input_text"
|
|
1435
|
+
});
|
|
1387
1436
|
}
|
|
1388
1437
|
function anthropicTools(tools) {
|
|
1389
1438
|
if (!Array.isArray(tools)) {
|
|
@@ -1392,6 +1441,7 @@ function anthropicTools(tools) {
|
|
|
1392
1441
|
const converted = tools.map((tool) => {
|
|
1393
1442
|
const record = asRecord(tool);
|
|
1394
1443
|
return removeUndefined({
|
|
1444
|
+
cache_control: anthropicCacheControl(record.cache_control),
|
|
1395
1445
|
description: record.description,
|
|
1396
1446
|
name: record.name,
|
|
1397
1447
|
parameters: record.input_schema,
|
|
@@ -1401,6 +1451,55 @@ function anthropicTools(tools) {
|
|
|
1401
1451
|
});
|
|
1402
1452
|
return converted.length > 0 ? converted : void 0;
|
|
1403
1453
|
}
|
|
1454
|
+
function anthropicCacheControl(value) {
|
|
1455
|
+
if (value === void 0 || value === null) {
|
|
1456
|
+
return void 0;
|
|
1457
|
+
}
|
|
1458
|
+
const record = asRecord(value);
|
|
1459
|
+
const type = textValue(record.type);
|
|
1460
|
+
if (type !== "ephemeral") {
|
|
1461
|
+
throw new AnthropicCompatibilityError(
|
|
1462
|
+
`Anthropic cache_control type "${type || "unknown"}" is not supported.`
|
|
1463
|
+
);
|
|
1464
|
+
}
|
|
1465
|
+
const ttl = textValue(record.ttl);
|
|
1466
|
+
if (ttl && ttl !== "5m" && ttl !== "1h") {
|
|
1467
|
+
throw new AnthropicCompatibilityError(`Anthropic cache_control ttl "${ttl}" is not supported.`);
|
|
1468
|
+
}
|
|
1469
|
+
return removeUndefined({
|
|
1470
|
+
ttl: ttl || void 0,
|
|
1471
|
+
type
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
function applyCacheControlToLastBlock(request, cacheControl) {
|
|
1475
|
+
if (!cacheControl) {
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
const input = Array.isArray(request.input) ? request.input : [];
|
|
1479
|
+
for (let itemIndex = input.length - 1; itemIndex >= 0; itemIndex -= 1) {
|
|
1480
|
+
const item = asRecord(input[itemIndex]);
|
|
1481
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
1482
|
+
for (let partIndex = content.length - 1; partIndex >= 0; partIndex -= 1) {
|
|
1483
|
+
const part = asRecord(content[partIndex]);
|
|
1484
|
+
if (part.cache_control === void 0 && isCacheableResponsesPart(part)) {
|
|
1485
|
+
part.cache_control = cacheControl;
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
const tools = Array.isArray(request.tools) ? request.tools : [];
|
|
1491
|
+
for (let index = tools.length - 1; index >= 0; index -= 1) {
|
|
1492
|
+
const tool = asRecord(tools[index]);
|
|
1493
|
+
if (tool.cache_control === void 0) {
|
|
1494
|
+
tool.cache_control = cacheControl;
|
|
1495
|
+
return;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
function isCacheableResponsesPart(part) {
|
|
1500
|
+
const type = textValue(part.type);
|
|
1501
|
+
return type === "input_text" || type === "output_text" || type === "text" || type === "input_image";
|
|
1502
|
+
}
|
|
1404
1503
|
function anthropicToolChoice(toolChoice) {
|
|
1405
1504
|
if (toolChoice === void 0 || toolChoice === null) {
|
|
1406
1505
|
return void 0;
|