@prestyj/ai 4.2.56 → 4.2.75

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/README.md CHANGED
@@ -42,7 +42,7 @@ Tool parameters are Zod schemas. Converted to JSON Schema at the provider bounda
42
42
  |---|---|---|
43
43
  | `anthropic` | Claude Opus 4.6, Sonnet 4.6, Haiku 4.5 | Extended thinking, prompt caching, server-side compaction |
44
44
  | `openai` | GPT-4.1, o3, o4-mini | Supports OAuth (codex endpoint) and API keys |
45
- | `glm` | GLM-5, GLM-4.7 | Z.AI platform, OpenAI-compatible |
45
+ | `glm` | GLM-5.1, GLM-4.7 | Z.AI platform, OpenAI-compatible |
46
46
  | `moonshot` | Kimi K2.5 | Moonshot platform, OpenAI-compatible |
47
47
 
48
48
  ---
package/dist/index.cjs CHANGED
@@ -34,7 +34,12 @@ __export(index_exports, {
34
34
  EventStream: () => EventStream,
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);
@@ -501,12 +506,19 @@ async function runStream(options, result) {
501
506
  ]
502
507
  } : {},
503
508
  ...options.toolChoice && options.tools?.length ? { tool_choice: toAnthropicToolChoice(options.toolChoice) } : {},
504
- ...options.compaction ? { context_management: { edits: [{ type: "compact_20260112" }] } } : {},
509
+ ...(() => {
510
+ const contextEdits = [
511
+ ...options.compaction ? [{ type: "compact_20260112" }] : [],
512
+ ...options.clearToolUses ? [{ type: "clear_tool_uses_20250919" }] : []
513
+ ];
514
+ return contextEdits.length ? { context_management: { edits: contextEdits } } : {};
515
+ })(),
505
516
  stream: true
506
517
  };
507
518
  const betaHeaders = [
508
519
  ...isOAuth ? ["claude-code-20250219", "oauth-2025-04-20"] : [],
509
- ...options.compaction ? ["compact-2026-01-12"] : []
520
+ ...options.compaction ? ["compact-2026-01-12"] : [],
521
+ ...options.clearToolUses ? ["context-management-2025-06-27"] : []
510
522
  ];
511
523
  const stream2 = client.messages.stream(params, {
512
524
  signal: options.signal ?? void 0,
@@ -693,12 +705,12 @@ async function runStream2(options, result) {
693
705
  for await (const chunk of stream2) {
694
706
  const choice = chunk.choices?.[0];
695
707
  if (chunk.usage) {
696
- inputTokens = chunk.usage.prompt_tokens;
697
708
  outputTokens = chunk.usage.completion_tokens;
698
709
  const details = chunk.usage.prompt_tokens_details;
699
710
  if (details?.cached_tokens) {
700
711
  cacheRead = details.cached_tokens;
701
712
  }
713
+ inputTokens = chunk.usage.prompt_tokens - cacheRead;
702
714
  }
703
715
  if (!choice) continue;
704
716
  if (choice.finish_reason) {
@@ -1200,13 +1212,175 @@ async function runGLMWithFallback(options, result) {
1200
1212
  }
1201
1213
  }
1202
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
+ }
1203
1372
  // Annotate the CommonJS export names for ESM import in node:
1204
1373
  0 && (module.exports = {
1205
1374
  EZCoderAIError,
1206
1375
  EventStream,
1207
1376
  ProviderError,
1208
1377
  StreamResult,
1378
+ palsuAssistantMessage,
1379
+ palsuText,
1380
+ palsuThinking,
1381
+ palsuToolCall,
1209
1382
  providerRegistry,
1383
+ registerPalsuProvider,
1210
1384
  stream
1211
1385
  });
1212
1386
  //# sourceMappingURL=index.cjs.map