@kenkaiiii/gg-ai 4.2.70 → 4.2.72
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 +167 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +71 -2
- package/dist/index.d.ts +71 -2
- package/dist/index.js +162 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -34,7 +34,12 @@ __export(index_exports, {
|
|
|
34
34
|
GGAIError: () => GGAIError,
|
|
35
35
|
ProviderError: () => ProviderError,
|
|
36
36
|
StreamResult: () => StreamResult,
|
|
37
|
+
palsuAssistantMessage: () => palsuAssistantMessage,
|
|
38
|
+
palsuText: () => palsuText,
|
|
39
|
+
palsuThinking: () => palsuThinking,
|
|
40
|
+
palsuToolCall: () => palsuToolCall,
|
|
37
41
|
providerRegistry: () => providerRegistry,
|
|
42
|
+
registerPalsuProvider: () => registerPalsuProvider,
|
|
38
43
|
stream: () => stream
|
|
39
44
|
});
|
|
40
45
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -1207,13 +1212,175 @@ async function runGLMWithFallback(options, result) {
|
|
|
1207
1212
|
}
|
|
1208
1213
|
}
|
|
1209
1214
|
}
|
|
1215
|
+
|
|
1216
|
+
// src/providers/palsu.ts
|
|
1217
|
+
function palsuText(text) {
|
|
1218
|
+
return { role: "assistant", content: text ? [{ type: "text", text }] : [] };
|
|
1219
|
+
}
|
|
1220
|
+
function palsuThinking(thinking, text) {
|
|
1221
|
+
const content = [{ type: "thinking", text: thinking }];
|
|
1222
|
+
if (text) content.push({ type: "text", text });
|
|
1223
|
+
return { role: "assistant", content };
|
|
1224
|
+
}
|
|
1225
|
+
function palsuToolCall(name, args, id) {
|
|
1226
|
+
return {
|
|
1227
|
+
role: "assistant",
|
|
1228
|
+
content: [{ type: "tool_call", id: id ?? `palsu_${name}_${Date.now()}`, name, args }]
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
function palsuAssistantMessage(content, options) {
|
|
1232
|
+
return { role: "assistant", content, _stopReason: options?.stopReason };
|
|
1233
|
+
}
|
|
1234
|
+
var DEFAULT_CHUNK_SIZE = 20;
|
|
1235
|
+
function chunkText(text, size) {
|
|
1236
|
+
const chunks = [];
|
|
1237
|
+
for (let i = 0; i < text.length; i += size) {
|
|
1238
|
+
chunks.push(text.slice(i, i + size));
|
|
1239
|
+
}
|
|
1240
|
+
return chunks.length > 0 ? chunks : [""];
|
|
1241
|
+
}
|
|
1242
|
+
function simulateStream(message, stopReason, result, signal, cacheUsage) {
|
|
1243
|
+
if (signal?.aborted) {
|
|
1244
|
+
result.abort(new Error("aborted"));
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
const content = typeof message.content === "string" ? message.content ? [{ type: "text", text: message.content }] : [] : message.content;
|
|
1248
|
+
let outputChars = 0;
|
|
1249
|
+
for (const part of content) {
|
|
1250
|
+
if (signal?.aborted) {
|
|
1251
|
+
result.abort(new Error("aborted"));
|
|
1252
|
+
return;
|
|
1253
|
+
}
|
|
1254
|
+
if (part.type === "text") {
|
|
1255
|
+
const chunks = chunkText(part.text, DEFAULT_CHUNK_SIZE);
|
|
1256
|
+
for (const chunk of chunks) {
|
|
1257
|
+
result.push({ type: "text_delta", text: chunk });
|
|
1258
|
+
outputChars += chunk.length;
|
|
1259
|
+
}
|
|
1260
|
+
} else if (part.type === "thinking") {
|
|
1261
|
+
result.push({ type: "thinking_delta", text: part.text });
|
|
1262
|
+
outputChars += part.text.length;
|
|
1263
|
+
} else if (part.type === "tool_call") {
|
|
1264
|
+
const argsJson = JSON.stringify(part.args);
|
|
1265
|
+
result.push({ type: "toolcall_delta", id: part.id, name: part.name, argsJson });
|
|
1266
|
+
result.push({ type: "toolcall_done", id: part.id, name: part.name, args: part.args });
|
|
1267
|
+
outputChars += argsJson.length;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
const outputTokens = Math.max(1, Math.ceil(outputChars / 4));
|
|
1271
|
+
const usage = {
|
|
1272
|
+
inputTokens: 100,
|
|
1273
|
+
outputTokens,
|
|
1274
|
+
...cacheUsage?.cacheRead ? { cacheRead: cacheUsage.cacheRead } : {},
|
|
1275
|
+
...cacheUsage?.cacheWrite ? { cacheWrite: cacheUsage.cacheWrite } : {}
|
|
1276
|
+
};
|
|
1277
|
+
result.push({ type: "done", stopReason });
|
|
1278
|
+
result.complete({ message, stopReason, usage });
|
|
1279
|
+
}
|
|
1280
|
+
function computeCacheUsage(current, previous) {
|
|
1281
|
+
if (!previous) {
|
|
1282
|
+
return { cacheRead: 0, cacheWrite: Math.ceil(current.length / 4) };
|
|
1283
|
+
}
|
|
1284
|
+
const maxLen = Math.min(current.length, previous.length);
|
|
1285
|
+
let commonLen = 0;
|
|
1286
|
+
for (let i = 0; i < maxLen; i++) {
|
|
1287
|
+
if (current[i] !== previous[i]) break;
|
|
1288
|
+
commonLen++;
|
|
1289
|
+
}
|
|
1290
|
+
return {
|
|
1291
|
+
cacheRead: Math.ceil(commonLen / 4),
|
|
1292
|
+
cacheWrite: Math.ceil((current.length - commonLen) / 4)
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
function registerPalsuProvider(config) {
|
|
1296
|
+
const name = config?.name ?? "palsu";
|
|
1297
|
+
const responses = [];
|
|
1298
|
+
const state = { callCount: 0 };
|
|
1299
|
+
const defaultResponse = config?.defaultResponse ?? palsuText("");
|
|
1300
|
+
const enableCache = config?.promptCache ?? false;
|
|
1301
|
+
let lastMessagesSerialized = null;
|
|
1302
|
+
const modelStates = /* @__PURE__ */ new Map();
|
|
1303
|
+
if (config?.models) {
|
|
1304
|
+
for (const [modelName, modelConfig] of Object.entries(config.models)) {
|
|
1305
|
+
modelStates.set(modelName, {
|
|
1306
|
+
responses: [],
|
|
1307
|
+
defaultResponse: modelConfig.defaultResponse
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
const handle = {
|
|
1312
|
+
setResponses(r) {
|
|
1313
|
+
responses.length = 0;
|
|
1314
|
+
responses.push(...r);
|
|
1315
|
+
},
|
|
1316
|
+
appendResponses(...r) {
|
|
1317
|
+
responses.push(...r);
|
|
1318
|
+
},
|
|
1319
|
+
getPendingResponseCount() {
|
|
1320
|
+
return responses.length;
|
|
1321
|
+
},
|
|
1322
|
+
state,
|
|
1323
|
+
getModel(modelName) {
|
|
1324
|
+
if (!modelStates.has(modelName)) {
|
|
1325
|
+
modelStates.set(modelName, { responses: [] });
|
|
1326
|
+
}
|
|
1327
|
+
const ms = modelStates.get(modelName);
|
|
1328
|
+
return {
|
|
1329
|
+
setResponses(r) {
|
|
1330
|
+
ms.responses.length = 0;
|
|
1331
|
+
ms.responses.push(...r);
|
|
1332
|
+
},
|
|
1333
|
+
appendResponses(...r) {
|
|
1334
|
+
ms.responses.push(...r);
|
|
1335
|
+
},
|
|
1336
|
+
getPendingResponseCount() {
|
|
1337
|
+
return ms.responses.length;
|
|
1338
|
+
}
|
|
1339
|
+
};
|
|
1340
|
+
},
|
|
1341
|
+
unregister() {
|
|
1342
|
+
providerRegistry.unregister(name);
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
providerRegistry.register(name, {
|
|
1346
|
+
stream(options) {
|
|
1347
|
+
state.callCount++;
|
|
1348
|
+
const ms = modelStates.get(options.model);
|
|
1349
|
+
const responseDef = (ms && ms.responses.length > 0 ? ms.responses.shift() : void 0) ?? (responses.length > 0 ? responses.shift() : void 0) ?? ms?.defaultResponse ?? defaultResponse;
|
|
1350
|
+
const result = new StreamResult();
|
|
1351
|
+
let cacheUsage;
|
|
1352
|
+
if (enableCache) {
|
|
1353
|
+
const serialized = JSON.stringify(options.messages);
|
|
1354
|
+
cacheUsage = computeCacheUsage(serialized, lastMessagesSerialized);
|
|
1355
|
+
lastMessagesSerialized = serialized;
|
|
1356
|
+
}
|
|
1357
|
+
const rawMessage = typeof responseDef === "function" ? responseDef(options.messages, options, state) : responseDef;
|
|
1358
|
+
Promise.resolve(rawMessage).then(
|
|
1359
|
+
(message) => {
|
|
1360
|
+
const hasToolCalls = Array.isArray(message.content) && message.content.some((p) => p.type === "tool_call");
|
|
1361
|
+
const explicitStop = message._stopReason;
|
|
1362
|
+
const stopReason = explicitStop ?? (hasToolCalls ? "tool_use" : "end_turn");
|
|
1363
|
+
simulateStream(message, stopReason, result, options.signal, cacheUsage);
|
|
1364
|
+
},
|
|
1365
|
+
(err) => result.abort(err instanceof Error ? err : new Error(String(err)))
|
|
1366
|
+
);
|
|
1367
|
+
return result;
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
1370
|
+
return handle;
|
|
1371
|
+
}
|
|
1210
1372
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1211
1373
|
0 && (module.exports = {
|
|
1212
1374
|
EventStream,
|
|
1213
1375
|
GGAIError,
|
|
1214
1376
|
ProviderError,
|
|
1215
1377
|
StreamResult,
|
|
1378
|
+
palsuAssistantMessage,
|
|
1379
|
+
palsuText,
|
|
1380
|
+
palsuThinking,
|
|
1381
|
+
palsuToolCall,
|
|
1216
1382
|
providerRegistry,
|
|
1383
|
+
registerPalsuProvider,
|
|
1217
1384
|
stream
|
|
1218
1385
|
});
|
|
1219
1386
|
//# sourceMappingURL=index.cjs.map
|