@kenkaiiii/gg-ai 5.67.1 → 5.68.1

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 CHANGED
@@ -810,10 +810,17 @@ function countContextImages(messages) {
810
810
  }
811
811
  return count;
812
812
  }
813
+ var IMAGE_DROP_BATCH = 30;
814
+ function providerImageDropCount(imageCount2, budget) {
815
+ const overflow = imageCount2 - budget;
816
+ if (overflow <= 0) return 0;
817
+ const batch = Math.max(1, Math.min(IMAGE_DROP_BATCH, Math.floor(budget / 3)));
818
+ return Math.min(imageCount2, Math.ceil(overflow / batch) * batch);
819
+ }
813
820
  function clampProviderContextImages(messages, provider, supportsImages) {
814
821
  if (supportsImages === false) return messages;
815
822
  const budget = PROVIDER_IMAGE_BUDGETS[provider] ?? 5;
816
- let remainingToRemove = countContextImages(messages) - budget;
823
+ let remainingToRemove = providerImageDropCount(countContextImages(messages), budget);
817
824
  if (remainingToRemove <= 0) return messages;
818
825
  return messages.map((message) => {
819
826
  if (message.role === "user" && Array.isArray(message.content)) {
@@ -1443,7 +1450,7 @@ function streamAnthropic(options) {
1443
1450
  async function* runStream(options) {
1444
1451
  const client = createClient(options);
1445
1452
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
1446
- const useStreaming = options.streaming !== false;
1453
+ const useStreaming = options.streaming !== false && !options.prewarm;
1447
1454
  const cacheControl = toAnthropicCacheControl(options.cacheRetention, options.baseUrl);
1448
1455
  const supportsFirstPartyToolExtras = !options.baseUrl || options.baseUrl.includes("api.anthropic.com");
1449
1456
  const downgradedImages = downgradeUnsupportedImages(options.messages, options.supportsImages);
@@ -1467,6 +1474,17 @@ async function* runStream(options) {
1467
1474
  outputConfig = t.outputConfig;
1468
1475
  }
1469
1476
  }
1477
+ if (options.prewarm) {
1478
+ const budget = thinking?.budget_tokens;
1479
+ if (budget != null && budget >= 1) {
1480
+ return {
1481
+ message: { role: "assistant", content: [] },
1482
+ stopReason: "end_turn",
1483
+ usage: { inputTokens: 0, outputTokens: 0 }
1484
+ };
1485
+ }
1486
+ maxTokens = 1;
1487
+ }
1470
1488
  const params = {
1471
1489
  model: options.model,
1472
1490
  max_tokens: maxTokens,
@@ -3897,6 +3915,41 @@ function sanitizeMessagesForWire(messages) {
3897
3915
  return sanitized ?? messages;
3898
3916
  }
3899
3917
 
3918
+ // src/utils/context-observation.ts
3919
+ function imageCount(message) {
3920
+ if (!message || !Array.isArray(message.content)) return 0;
3921
+ if (message.role === "user")
3922
+ return message.content.filter((part) => part.type === "image").length;
3923
+ if (message.role !== "tool") return 0;
3924
+ return message.content.reduce(
3925
+ (count, result) => count + (Array.isArray(result.content) ? result.content.filter((part) => part.type === "image").length : 0),
3926
+ 0
3927
+ );
3928
+ }
3929
+ function observePreparedContext(before, after, tools) {
3930
+ let imagesBefore = 0;
3931
+ let imagesAfter = 0;
3932
+ let firstImageDropMessage = null;
3933
+ for (let index = 0; index < before.length; index++) {
3934
+ const oldCount = imageCount(before[index]);
3935
+ const newCount = imageCount(after[index]);
3936
+ imagesBefore += oldCount;
3937
+ imagesAfter += newCount;
3938
+ if (firstImageDropMessage === null && newCount < oldCount) firstImageDropMessage = index;
3939
+ }
3940
+ return {
3941
+ messages: after,
3942
+ tools: tools.map((tool) => ({
3943
+ name: tool.name,
3944
+ description: tool.description,
3945
+ parameters: resolveToolSchema(tool)
3946
+ })),
3947
+ imagesBefore,
3948
+ imagesAfter,
3949
+ firstImageDropMessage
3950
+ };
3951
+ }
3952
+
3900
3953
  // src/stream.ts
3901
3954
  var GLM_CODING_BASE_URL = "https://api.z.ai/api/coding/paas/v4";
3902
3955
  var KIMI_CODE_USER_AGENT = `kimi-code-cli/${process.env.KIMI_CODE_VERSION ?? "1.0.11"}`;
@@ -4044,6 +4097,14 @@ function stream(options) {
4044
4097
  options.provider,
4045
4098
  options.supportsImages
4046
4099
  );
4100
+ if (options.onContextPrepared) {
4101
+ try {
4102
+ options.onContextPrepared(
4103
+ observePreparedContext(wireMessages, messages, options.tools ?? [])
4104
+ );
4105
+ } catch {
4106
+ }
4107
+ }
4047
4108
  return entry.stream(messages === options.messages ? options : { ...options, messages });
4048
4109
  }
4049
4110
  function stripMessageProvenance(messages) {