@kenkaiiii/gg-ai 5.35.0 → 5.35.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
@@ -40,6 +40,7 @@ __export(index_exports, {
40
40
  environmentSecrets: () => environmentSecrets,
41
41
  formatError: () => formatError,
42
42
  formatErrorForDisplay: () => formatErrorForDisplay,
43
+ hasLoneSurrogate: () => hasLoneSurrogate,
43
44
  isHardBillingMessage: () => isHardBillingMessage,
44
45
  isUsageLimitError: () => isUsageLimitError,
45
46
  localWireModelId: () => localWireModelId,
@@ -52,10 +53,14 @@ __export(index_exports, {
52
53
  redactText: () => redactText,
53
54
  redactValue: () => redactValue,
54
55
  registerPalsuProvider: () => registerPalsuProvider,
56
+ sanitizeMessagesForWire: () => sanitizeMessagesForWire,
55
57
  setProviderDiagnostic: () => setProviderDiagnostic,
58
+ sliceHead: () => sliceHead,
59
+ sliceTail: () => sliceTail,
56
60
  stream: () => stream,
57
61
  toAnthropicMessages: () => toAnthropicMessages,
58
- toOpenAIMessages: () => toOpenAIMessages
62
+ toOpenAIMessages: () => toOpenAIMessages,
63
+ toWellFormedText: () => toWellFormedText
59
64
  });
60
65
  module.exports = __toCommonJS(index_exports);
61
66
 
@@ -3500,6 +3505,140 @@ var ProviderRegistryImpl = class {
3500
3505
  };
3501
3506
  var providerRegistry = new ProviderRegistryImpl();
3502
3507
 
3508
+ // src/utils/well-formed.ts
3509
+ var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
3510
+ var LONE_SURROGATE_GLOBAL = new RegExp(LONE_SURROGATE, "g");
3511
+ var REPLACEMENT = "\uFFFD";
3512
+ function hasLoneSurrogate(text) {
3513
+ const isWellFormed = text.isWellFormed;
3514
+ if (typeof isWellFormed === "function") return !isWellFormed.call(text);
3515
+ return LONE_SURROGATE.test(text);
3516
+ }
3517
+ function toWellFormedText(text) {
3518
+ if (!hasLoneSurrogate(text)) return text;
3519
+ const toWellFormed = text.toWellFormed;
3520
+ if (typeof toWellFormed === "function") return toWellFormed.call(text);
3521
+ return text.replace(LONE_SURROGATE_GLOBAL, REPLACEMENT);
3522
+ }
3523
+ function isHighSurrogate(code) {
3524
+ return code !== void 0 && code >= 55296 && code <= 56319;
3525
+ }
3526
+ function isLowSurrogate(code) {
3527
+ return code !== void 0 && code >= 56320 && code <= 57343;
3528
+ }
3529
+ function sliceHead(text, chars) {
3530
+ if (chars <= 0) return "";
3531
+ if (chars >= text.length) return text;
3532
+ const end = isHighSurrogate(text.charCodeAt(chars - 1)) ? chars - 1 : chars;
3533
+ return text.slice(0, end);
3534
+ }
3535
+ function sliceTail(text, chars) {
3536
+ if (chars <= 0) return "";
3537
+ if (chars >= text.length) return text;
3538
+ const start = text.length - chars;
3539
+ return text.slice(isLowSurrogate(text.charCodeAt(start)) ? start + 1 : start);
3540
+ }
3541
+ function sanitizeJsonValue(value) {
3542
+ if (typeof value === "string") return toWellFormedText(value);
3543
+ if (Array.isArray(value)) {
3544
+ let changed = false;
3545
+ const next = value.map((item) => {
3546
+ const sanitized = sanitizeJsonValue(item);
3547
+ if (sanitized !== item) changed = true;
3548
+ return sanitized;
3549
+ });
3550
+ return changed ? next : value;
3551
+ }
3552
+ if (value !== null && typeof value === "object") {
3553
+ let changed = false;
3554
+ const next = {};
3555
+ for (const [key, item] of Object.entries(value)) {
3556
+ const sanitizedKey = toWellFormedText(key);
3557
+ const sanitized = sanitizeJsonValue(item);
3558
+ if (sanitizedKey !== key || sanitized !== item) changed = true;
3559
+ next[sanitizedKey] = sanitized;
3560
+ }
3561
+ return changed ? next : value;
3562
+ }
3563
+ return value;
3564
+ }
3565
+ function sanitizeRecord(value) {
3566
+ return sanitizeJsonValue(value);
3567
+ }
3568
+ function sanitizePart(part) {
3569
+ switch (part.type) {
3570
+ case "text":
3571
+ case "thinking": {
3572
+ const text = toWellFormedText(part.text);
3573
+ return text === part.text ? part : { ...part, text };
3574
+ }
3575
+ case "tool_call": {
3576
+ const args = sanitizeRecord(part.args);
3577
+ return args === part.args ? part : { ...part, args };
3578
+ }
3579
+ case "server_tool_call": {
3580
+ const input = sanitizeJsonValue(part.input);
3581
+ return input === part.input ? part : { ...part, input };
3582
+ }
3583
+ case "server_tool_result": {
3584
+ const data = sanitizeJsonValue(part.data);
3585
+ return data === part.data ? part : { ...part, data };
3586
+ }
3587
+ case "raw": {
3588
+ const data = sanitizeRecord(part.data);
3589
+ return data === part.data ? part : { ...part, data };
3590
+ }
3591
+ default:
3592
+ return part;
3593
+ }
3594
+ }
3595
+ function sanitizeParts(parts) {
3596
+ let changed = false;
3597
+ const next = parts.map((part) => {
3598
+ const sanitized = sanitizePart(part);
3599
+ if (sanitized !== part) changed = true;
3600
+ return sanitized;
3601
+ });
3602
+ return changed ? next : parts;
3603
+ }
3604
+ function sanitizeToolResultContent(content) {
3605
+ if (typeof content === "string") return toWellFormedText(content);
3606
+ return sanitizeParts(content);
3607
+ }
3608
+ function sanitizeToolResults(results) {
3609
+ let changed = false;
3610
+ const next = results.map((result) => {
3611
+ const content = sanitizeToolResultContent(result.content);
3612
+ if (content === result.content) return result;
3613
+ changed = true;
3614
+ return { ...result, content };
3615
+ });
3616
+ return changed ? next : results;
3617
+ }
3618
+ function sanitizeMessage(message) {
3619
+ if (message.role === "tool") {
3620
+ const content2 = sanitizeToolResults(message.content);
3621
+ return content2 === message.content ? message : { ...message, content: content2 };
3622
+ }
3623
+ if (typeof message.content === "string") {
3624
+ const content2 = toWellFormedText(message.content);
3625
+ return content2 === message.content ? message : { ...message, content: content2 };
3626
+ }
3627
+ const content = sanitizeParts(message.content);
3628
+ return content === message.content ? message : { ...message, content };
3629
+ }
3630
+ function sanitizeMessagesForWire(messages) {
3631
+ let sanitized;
3632
+ for (let index = 0; index < messages.length; index++) {
3633
+ const message = messages[index];
3634
+ const next = sanitizeMessage(message);
3635
+ if (next === message) continue;
3636
+ sanitized ??= messages.slice();
3637
+ sanitized[index] = next;
3638
+ }
3639
+ return sanitized ?? messages;
3640
+ }
3641
+
3503
3642
  // src/stream.ts
3504
3643
  var GLM_CODING_BASE_URL = "https://api.z.ai/api/coding/paas/v4";
3505
3644
  var KIMI_CODE_USER_AGENT = `kimi-code-cli/${process.env.KIMI_CODE_VERSION ?? "1.0.11"}`;
@@ -3617,7 +3756,7 @@ function stream(options) {
3617
3756
  }
3618
3757
  const wireMessages = stripMessageProvenance(options.messages);
3619
3758
  const messages = clampProviderContextImages(
3620
- wireMessages,
3759
+ sanitizeMessagesForWire(wireMessages),
3621
3760
  options.provider,
3622
3761
  options.supportsImages
3623
3762
  );
@@ -4019,6 +4158,7 @@ function registerPalsuProvider(config) {
4019
4158
  environmentSecrets,
4020
4159
  formatError,
4021
4160
  formatErrorForDisplay,
4161
+ hasLoneSurrogate,
4022
4162
  isHardBillingMessage,
4023
4163
  isUsageLimitError,
4024
4164
  localWireModelId,
@@ -4031,9 +4171,13 @@ function registerPalsuProvider(config) {
4031
4171
  redactText,
4032
4172
  redactValue,
4033
4173
  registerPalsuProvider,
4174
+ sanitizeMessagesForWire,
4034
4175
  setProviderDiagnostic,
4176
+ sliceHead,
4177
+ sliceTail,
4035
4178
  stream,
4036
4179
  toAnthropicMessages,
4037
- toOpenAIMessages
4180
+ toOpenAIMessages,
4181
+ toWellFormedText
4038
4182
  });
4039
4183
  //# sourceMappingURL=index.cjs.map