kimiflare 0.73.1 → 0.73.2
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 +36 -49
- package/dist/index.js.map +1 -1
- package/dist/sdk/index.js +36 -49
- package/dist/sdk/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1211,27 +1211,24 @@ async function* runKimi(opts2) {
|
|
|
1211
1211
|
}
|
|
1212
1212
|
const requestId = opts2.requestId ?? crypto.randomUUID();
|
|
1213
1213
|
const { url, headers: gatewayHeaders } = buildKimiRequestTarget(opts2);
|
|
1214
|
-
const
|
|
1214
|
+
const isCloudEndpoint = url.startsWith("https://api.kimiflare.com");
|
|
1215
1215
|
const entry = getModelOrInfer(opts2.model);
|
|
1216
1216
|
const supportsTemperature = entry.supports.temperature !== false;
|
|
1217
1217
|
const supportsReasoning = entry.supports.reasoning === true;
|
|
1218
|
+
const compatModel = entry.provider === "workers-ai" ? `workers-ai/${opts2.model}` : opts2.model;
|
|
1218
1219
|
const body = {
|
|
1219
1220
|
messages: sanitizeMessagesForApi(opts2.messages),
|
|
1220
1221
|
...opts2.tools && opts2.tools.length ? { tools: opts2.tools, tool_choice: "auto", parallel_tool_calls: true } : {},
|
|
1221
1222
|
stream: true,
|
|
1222
1223
|
...supportsTemperature ? { temperature: opts2.temperature ?? 0.2 } : {},
|
|
1223
1224
|
max_completion_tokens: opts2.maxCompletionTokens ?? 16384,
|
|
1224
|
-
|
|
1225
|
-
// Workers AI endpoints route by URL path and ignore body.model.
|
|
1226
|
-
...isUniversalEndpoint ? { model: opts2.model } : {},
|
|
1225
|
+
...isCloudEndpoint ? {} : { model: compatModel },
|
|
1227
1226
|
// OpenAI's streaming API omits `usage` by default — you have to explicitly
|
|
1228
1227
|
// opt in via stream_options. Without this, the status bar's token /
|
|
1229
|
-
// context-% / cost columns stay blank
|
|
1230
|
-
// the Universal Endpoint (verified empirically by curling /compat with and
|
|
1231
|
-
// without this flag — see PR #468 discussion). CF docs don't mention
|
|
1228
|
+
// context-% / cost columns stay blank. CF docs don't mention
|
|
1232
1229
|
// stream_options but accept it transparently and forward it upstream;
|
|
1233
1230
|
// providers that don't recognize the field ignore it.
|
|
1234
|
-
...
|
|
1231
|
+
...isCloudEndpoint ? {} : { stream_options: { include_usage: true } }
|
|
1235
1232
|
};
|
|
1236
1233
|
if (opts2.reasoningEffort && supportsReasoning) {
|
|
1237
1234
|
body.reasoning_effort = opts2.reasoningEffort;
|
|
@@ -1381,25 +1378,24 @@ function gatewayHeadersFor(opts2) {
|
|
|
1381
1378
|
function buildKimiRequestTarget(opts2) {
|
|
1382
1379
|
validateModelId(opts2.model);
|
|
1383
1380
|
if (opts2.cloudMode) {
|
|
1384
|
-
const
|
|
1385
|
-
if (opts2.cloudDeviceId)
|
|
1386
|
-
return { url: "https://api.kimiflare.com/v1/chat", headers };
|
|
1381
|
+
const headers2 = opts2.cloudToken ? { Authorization: `Bearer ${opts2.cloudToken}` } : {};
|
|
1382
|
+
if (opts2.cloudDeviceId) headers2["X-Device-ID"] = opts2.cloudDeviceId;
|
|
1383
|
+
return { url: "https://api.kimiflare.com/v1/chat", headers: headers2 };
|
|
1384
|
+
}
|
|
1385
|
+
if (!opts2.gateway?.id) {
|
|
1386
|
+
throw new KimiApiError(
|
|
1387
|
+
[
|
|
1388
|
+
`kimiflare: ${opts2.model} routes through Cloudflare AI Gateway, but no gateway is configured.`,
|
|
1389
|
+
``,
|
|
1390
|
+
`To fix: run /gateway <your-gateway-id> (create one at https://dash.cloudflare.com/?to=/:account/ai-gateway).`
|
|
1391
|
+
].join("\n"),
|
|
1392
|
+
void 0,
|
|
1393
|
+
400
|
|
1394
|
+
);
|
|
1387
1395
|
}
|
|
1388
1396
|
const entry = getModelOrInfer(opts2.model);
|
|
1397
|
+
const headers = gatewayHeadersFor(opts2);
|
|
1389
1398
|
if (entry.provider !== "workers-ai") {
|
|
1390
|
-
if (!opts2.gateway?.id) {
|
|
1391
|
-
throw new KimiApiError(
|
|
1392
|
-
[
|
|
1393
|
-
`kimiflare: ${opts2.model} (${entry.provider}) is routed through Cloudflare AI Gateway, but no gateway is configured.`,
|
|
1394
|
-
``,
|
|
1395
|
-
`To fix: run /gateway <your-gateway-id> (create one at https://dash.cloudflare.com/?to=/:account/ai-gateway),`,
|
|
1396
|
-
`or switch back to a Workers AI model: /model @cf/moonshotai/kimi-k2.6`
|
|
1397
|
-
].join("\n"),
|
|
1398
|
-
void 0,
|
|
1399
|
-
400
|
|
1400
|
-
);
|
|
1401
|
-
}
|
|
1402
|
-
const headers = gatewayHeadersFor(opts2);
|
|
1403
1399
|
const useUnified = !!opts2.unifiedBilling;
|
|
1404
1400
|
const alias = opts2.providerKeyAliases?.[entry.provider];
|
|
1405
1401
|
const providerKey = opts2.providerKeys?.[entry.provider];
|
|
@@ -1415,24 +1411,12 @@ function buildKimiRequestTarget(opts2) {
|
|
|
1415
1411
|
401
|
|
1416
1412
|
);
|
|
1417
1413
|
}
|
|
1418
|
-
return {
|
|
1419
|
-
url: `https://gateway.ai.cloudflare.com/v1/${encodeURIComponent(opts2.accountId)}/${encodeURIComponent(
|
|
1420
|
-
opts2.gateway.id
|
|
1421
|
-
)}/compat/chat/completions`,
|
|
1422
|
-
headers
|
|
1423
|
-
};
|
|
1424
|
-
}
|
|
1425
|
-
if (!opts2.gateway?.id) {
|
|
1426
|
-
return {
|
|
1427
|
-
url: `https://api.cloudflare.com/client/v4/accounts/${encodeURIComponent(opts2.accountId)}/ai/run/${opts2.model}`,
|
|
1428
|
-
headers: {}
|
|
1429
|
-
};
|
|
1430
1414
|
}
|
|
1431
1415
|
return {
|
|
1432
1416
|
url: `https://gateway.ai.cloudflare.com/v1/${encodeURIComponent(opts2.accountId)}/${encodeURIComponent(
|
|
1433
1417
|
opts2.gateway.id
|
|
1434
|
-
)}/
|
|
1435
|
-
headers
|
|
1418
|
+
)}/compat/chat/completions`,
|
|
1419
|
+
headers
|
|
1436
1420
|
};
|
|
1437
1421
|
}
|
|
1438
1422
|
function readGatewayMeta(headers) {
|
|
@@ -3206,20 +3190,23 @@ async function fetchEmbeddings(opts2) {
|
|
|
3206
3190
|
if (opts2.cloudToken) headers.Authorization = `Bearer ${opts2.cloudToken}`;
|
|
3207
3191
|
if (opts2.cloudDeviceId) headers["X-Device-ID"] = opts2.cloudDeviceId;
|
|
3208
3192
|
} else {
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
...opts2.gateway.metadata ?? {},
|
|
3214
|
-
feature: "embedding"
|
|
3215
|
-
};
|
|
3216
|
-
const entries = Object.entries(merged).slice(0, 5);
|
|
3217
|
-
headers["cf-aig-metadata"] = JSON.stringify(Object.fromEntries(entries));
|
|
3193
|
+
if (!opts2.gateway) {
|
|
3194
|
+
throw new Error(
|
|
3195
|
+
"embeddings require an AI Gateway to be configured (run /gateway <id> or set aiGatewayId in config)"
|
|
3196
|
+
);
|
|
3218
3197
|
}
|
|
3219
|
-
|
|
3198
|
+
url = `https://gateway.ai.cloudflare.com/v1/${opts2.accountId}/${opts2.gateway.id}/workers-ai/${model}`;
|
|
3199
|
+
headers.Authorization = `Bearer ${opts2.apiToken}`;
|
|
3200
|
+
const merged = {
|
|
3201
|
+
...opts2.gateway.metadata ?? {},
|
|
3202
|
+
feature: "embedding"
|
|
3203
|
+
};
|
|
3204
|
+
const entries = Object.entries(merged).slice(0, 5);
|
|
3205
|
+
headers["cf-aig-metadata"] = JSON.stringify(Object.fromEntries(entries));
|
|
3206
|
+
if (opts2.gateway.cacheTtl !== void 0) {
|
|
3220
3207
|
headers["cf-aig-cache-ttl"] = String(opts2.gateway.cacheTtl);
|
|
3221
3208
|
}
|
|
3222
|
-
if (opts2.gateway
|
|
3209
|
+
if (opts2.gateway.skipCache !== void 0) {
|
|
3223
3210
|
headers["cf-aig-skip-cache"] = String(opts2.gateway.skipCache);
|
|
3224
3211
|
}
|
|
3225
3212
|
}
|