@kenkaiiii/gg-ai 5.68.0 → 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)) {
@@ -3908,6 +3915,41 @@ function sanitizeMessagesForWire(messages) {
3908
3915
  return sanitized ?? messages;
3909
3916
  }
3910
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
+
3911
3953
  // src/stream.ts
3912
3954
  var GLM_CODING_BASE_URL = "https://api.z.ai/api/coding/paas/v4";
3913
3955
  var KIMI_CODE_USER_AGENT = `kimi-code-cli/${process.env.KIMI_CODE_VERSION ?? "1.0.11"}`;
@@ -4055,6 +4097,14 @@ function stream(options) {
4055
4097
  options.provider,
4056
4098
  options.supportsImages
4057
4099
  );
4100
+ if (options.onContextPrepared) {
4101
+ try {
4102
+ options.onContextPrepared(
4103
+ observePreparedContext(wireMessages, messages, options.tools ?? [])
4104
+ );
4105
+ } catch {
4106
+ }
4107
+ }
4058
4108
  return entry.stream(messages === options.messages ? options : { ...options, messages });
4059
4109
  }
4060
4110
  function stripMessageProvenance(messages) {