@kenkaiiii/gg-ai 5.35.0 → 5.36.0

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.d.cts CHANGED
@@ -505,6 +505,21 @@ declare function redactText(text: string, options?: RedactionOptions): string;
505
505
  */
506
506
  declare function redactValue<T>(value: T, options?: RedactionOptions): T;
507
507
 
508
+ /** True when the string contains at least one unpaired surrogate. */
509
+ declare function hasLoneSurrogate(text: string): boolean;
510
+ /** Replace unpaired surrogates with U+FFFD; returns the input when already valid. */
511
+ declare function toWellFormedText(text: string): string;
512
+ /** `text.slice(0, chars)` that never cuts an astral character in half. */
513
+ declare function sliceHead(text: string, chars: number): string;
514
+ /** `text.slice(-chars)` that never cuts an astral character in half. */
515
+ declare function sliceTail(text: string, chars: number): string;
516
+ /**
517
+ * Strip unpaired surrogates from everything headed for the wire. Returns the
518
+ * same array (and same message objects) when the history is already valid, so
519
+ * the clean path stays allocation-free.
520
+ */
521
+ declare function sanitizeMessagesForWire(messages: Message[]): Message[];
522
+
508
523
  /**
509
524
  * Provider-level diagnostic hook. Mirrors the pattern used by gg-agent's
510
525
  * setStreamDiagnostic — the host app wires a callback (typically writing to
@@ -627,4 +642,4 @@ interface PalsuProviderConfig {
627
642
  */
628
643
  declare function registerPalsuProvider(config?: PalsuProviderConfig): PalsuProviderHandle;
629
644
 
630
- export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type MessageProvenance, type MessageProvenanceKind, type MessageProvenanceSource, type MessageProvenanceVisibility, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, REDACTED as REDACTION_MARKER, type RawContent, type RedactionOptions, type ServerToolCall, type ServerToolCallEvent, type ServerToolDefinition, type ServerToolResult, type ServerToolResultEvent, type StopReason, type StreamEvent, type StreamOptions, type StreamResponse, StreamResult, type SystemMessage, type TextContent, type TextDeltaEvent, type ThinkingContent, type ThinkingDeltaEvent, type ThinkingLevel, type Tool, type ToolCall, type ToolCallDeltaEvent, type ToolCallDoneEvent, type ToolChoice, type ToolResult, type ToolResultContent, type ToolResultMessage, type Usage, type UserMessage, type VideoContent, clampProviderContextImages, classifyProviderError, environmentSecrets, formatError, formatErrorForDisplay, isHardBillingMessage, isUsageLimitError, localWireModelId, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, prewarmAnthropicCache, providerRegistry, redactText, redactValue, registerPalsuProvider, setProviderDiagnostic, stream, toAnthropicMessages, toOpenAIMessages };
645
+ export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type MessageProvenance, type MessageProvenanceKind, type MessageProvenanceSource, type MessageProvenanceVisibility, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, REDACTED as REDACTION_MARKER, type RawContent, type RedactionOptions, type ServerToolCall, type ServerToolCallEvent, type ServerToolDefinition, type ServerToolResult, type ServerToolResultEvent, type StopReason, type StreamEvent, type StreamOptions, type StreamResponse, StreamResult, type SystemMessage, type TextContent, type TextDeltaEvent, type ThinkingContent, type ThinkingDeltaEvent, type ThinkingLevel, type Tool, type ToolCall, type ToolCallDeltaEvent, type ToolCallDoneEvent, type ToolChoice, type ToolResult, type ToolResultContent, type ToolResultMessage, type Usage, type UserMessage, type VideoContent, clampProviderContextImages, classifyProviderError, environmentSecrets, formatError, formatErrorForDisplay, hasLoneSurrogate, isHardBillingMessage, isUsageLimitError, localWireModelId, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, prewarmAnthropicCache, providerRegistry, redactText, redactValue, registerPalsuProvider, sanitizeMessagesForWire, setProviderDiagnostic, sliceHead, sliceTail, stream, toAnthropicMessages, toOpenAIMessages, toWellFormedText };
package/dist/index.d.ts CHANGED
@@ -505,6 +505,21 @@ declare function redactText(text: string, options?: RedactionOptions): string;
505
505
  */
506
506
  declare function redactValue<T>(value: T, options?: RedactionOptions): T;
507
507
 
508
+ /** True when the string contains at least one unpaired surrogate. */
509
+ declare function hasLoneSurrogate(text: string): boolean;
510
+ /** Replace unpaired surrogates with U+FFFD; returns the input when already valid. */
511
+ declare function toWellFormedText(text: string): string;
512
+ /** `text.slice(0, chars)` that never cuts an astral character in half. */
513
+ declare function sliceHead(text: string, chars: number): string;
514
+ /** `text.slice(-chars)` that never cuts an astral character in half. */
515
+ declare function sliceTail(text: string, chars: number): string;
516
+ /**
517
+ * Strip unpaired surrogates from everything headed for the wire. Returns the
518
+ * same array (and same message objects) when the history is already valid, so
519
+ * the clean path stays allocation-free.
520
+ */
521
+ declare function sanitizeMessagesForWire(messages: Message[]): Message[];
522
+
508
523
  /**
509
524
  * Provider-level diagnostic hook. Mirrors the pattern used by gg-agent's
510
525
  * setStreamDiagnostic — the host app wires a callback (typically writing to
@@ -627,4 +642,4 @@ interface PalsuProviderConfig {
627
642
  */
628
643
  declare function registerPalsuProvider(config?: PalsuProviderConfig): PalsuProviderHandle;
629
644
 
630
- export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type MessageProvenance, type MessageProvenanceKind, type MessageProvenanceSource, type MessageProvenanceVisibility, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, REDACTED as REDACTION_MARKER, type RawContent, type RedactionOptions, type ServerToolCall, type ServerToolCallEvent, type ServerToolDefinition, type ServerToolResult, type ServerToolResultEvent, type StopReason, type StreamEvent, type StreamOptions, type StreamResponse, StreamResult, type SystemMessage, type TextContent, type TextDeltaEvent, type ThinkingContent, type ThinkingDeltaEvent, type ThinkingLevel, type Tool, type ToolCall, type ToolCallDeltaEvent, type ToolCallDoneEvent, type ToolChoice, type ToolResult, type ToolResultContent, type ToolResultMessage, type Usage, type UserMessage, type VideoContent, clampProviderContextImages, classifyProviderError, environmentSecrets, formatError, formatErrorForDisplay, isHardBillingMessage, isUsageLimitError, localWireModelId, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, prewarmAnthropicCache, providerRegistry, redactText, redactValue, registerPalsuProvider, setProviderDiagnostic, stream, toAnthropicMessages, toOpenAIMessages };
645
+ export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type MessageProvenance, type MessageProvenanceKind, type MessageProvenanceSource, type MessageProvenanceVisibility, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, REDACTED as REDACTION_MARKER, type RawContent, type RedactionOptions, type ServerToolCall, type ServerToolCallEvent, type ServerToolDefinition, type ServerToolResult, type ServerToolResultEvent, type StopReason, type StreamEvent, type StreamOptions, type StreamResponse, StreamResult, type SystemMessage, type TextContent, type TextDeltaEvent, type ThinkingContent, type ThinkingDeltaEvent, type ThinkingLevel, type Tool, type ToolCall, type ToolCallDeltaEvent, type ToolCallDoneEvent, type ToolChoice, type ToolResult, type ToolResultContent, type ToolResultMessage, type Usage, type UserMessage, type VideoContent, clampProviderContextImages, classifyProviderError, environmentSecrets, formatError, formatErrorForDisplay, hasLoneSurrogate, isHardBillingMessage, isUsageLimitError, localWireModelId, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, prewarmAnthropicCache, providerRegistry, redactText, redactValue, registerPalsuProvider, sanitizeMessagesForWire, setProviderDiagnostic, sliceHead, sliceTail, stream, toAnthropicMessages, toOpenAIMessages, toWellFormedText };
package/dist/index.js CHANGED
@@ -3439,6 +3439,140 @@ var ProviderRegistryImpl = class {
3439
3439
  };
3440
3440
  var providerRegistry = new ProviderRegistryImpl();
3441
3441
 
3442
+ // src/utils/well-formed.ts
3443
+ var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
3444
+ var LONE_SURROGATE_GLOBAL = new RegExp(LONE_SURROGATE, "g");
3445
+ var REPLACEMENT = "\uFFFD";
3446
+ function hasLoneSurrogate(text) {
3447
+ const isWellFormed = text.isWellFormed;
3448
+ if (typeof isWellFormed === "function") return !isWellFormed.call(text);
3449
+ return LONE_SURROGATE.test(text);
3450
+ }
3451
+ function toWellFormedText(text) {
3452
+ if (!hasLoneSurrogate(text)) return text;
3453
+ const toWellFormed = text.toWellFormed;
3454
+ if (typeof toWellFormed === "function") return toWellFormed.call(text);
3455
+ return text.replace(LONE_SURROGATE_GLOBAL, REPLACEMENT);
3456
+ }
3457
+ function isHighSurrogate(code) {
3458
+ return code !== void 0 && code >= 55296 && code <= 56319;
3459
+ }
3460
+ function isLowSurrogate(code) {
3461
+ return code !== void 0 && code >= 56320 && code <= 57343;
3462
+ }
3463
+ function sliceHead(text, chars) {
3464
+ if (chars <= 0) return "";
3465
+ if (chars >= text.length) return text;
3466
+ const end = isHighSurrogate(text.charCodeAt(chars - 1)) ? chars - 1 : chars;
3467
+ return text.slice(0, end);
3468
+ }
3469
+ function sliceTail(text, chars) {
3470
+ if (chars <= 0) return "";
3471
+ if (chars >= text.length) return text;
3472
+ const start = text.length - chars;
3473
+ return text.slice(isLowSurrogate(text.charCodeAt(start)) ? start + 1 : start);
3474
+ }
3475
+ function sanitizeJsonValue(value) {
3476
+ if (typeof value === "string") return toWellFormedText(value);
3477
+ if (Array.isArray(value)) {
3478
+ let changed = false;
3479
+ const next = value.map((item) => {
3480
+ const sanitized = sanitizeJsonValue(item);
3481
+ if (sanitized !== item) changed = true;
3482
+ return sanitized;
3483
+ });
3484
+ return changed ? next : value;
3485
+ }
3486
+ if (value !== null && typeof value === "object") {
3487
+ let changed = false;
3488
+ const next = {};
3489
+ for (const [key, item] of Object.entries(value)) {
3490
+ const sanitizedKey = toWellFormedText(key);
3491
+ const sanitized = sanitizeJsonValue(item);
3492
+ if (sanitizedKey !== key || sanitized !== item) changed = true;
3493
+ next[sanitizedKey] = sanitized;
3494
+ }
3495
+ return changed ? next : value;
3496
+ }
3497
+ return value;
3498
+ }
3499
+ function sanitizeRecord(value) {
3500
+ return sanitizeJsonValue(value);
3501
+ }
3502
+ function sanitizePart(part) {
3503
+ switch (part.type) {
3504
+ case "text":
3505
+ case "thinking": {
3506
+ const text = toWellFormedText(part.text);
3507
+ return text === part.text ? part : { ...part, text };
3508
+ }
3509
+ case "tool_call": {
3510
+ const args = sanitizeRecord(part.args);
3511
+ return args === part.args ? part : { ...part, args };
3512
+ }
3513
+ case "server_tool_call": {
3514
+ const input = sanitizeJsonValue(part.input);
3515
+ return input === part.input ? part : { ...part, input };
3516
+ }
3517
+ case "server_tool_result": {
3518
+ const data = sanitizeJsonValue(part.data);
3519
+ return data === part.data ? part : { ...part, data };
3520
+ }
3521
+ case "raw": {
3522
+ const data = sanitizeRecord(part.data);
3523
+ return data === part.data ? part : { ...part, data };
3524
+ }
3525
+ default:
3526
+ return part;
3527
+ }
3528
+ }
3529
+ function sanitizeParts(parts) {
3530
+ let changed = false;
3531
+ const next = parts.map((part) => {
3532
+ const sanitized = sanitizePart(part);
3533
+ if (sanitized !== part) changed = true;
3534
+ return sanitized;
3535
+ });
3536
+ return changed ? next : parts;
3537
+ }
3538
+ function sanitizeToolResultContent(content) {
3539
+ if (typeof content === "string") return toWellFormedText(content);
3540
+ return sanitizeParts(content);
3541
+ }
3542
+ function sanitizeToolResults(results) {
3543
+ let changed = false;
3544
+ const next = results.map((result) => {
3545
+ const content = sanitizeToolResultContent(result.content);
3546
+ if (content === result.content) return result;
3547
+ changed = true;
3548
+ return { ...result, content };
3549
+ });
3550
+ return changed ? next : results;
3551
+ }
3552
+ function sanitizeMessage(message) {
3553
+ if (message.role === "tool") {
3554
+ const content2 = sanitizeToolResults(message.content);
3555
+ return content2 === message.content ? message : { ...message, content: content2 };
3556
+ }
3557
+ if (typeof message.content === "string") {
3558
+ const content2 = toWellFormedText(message.content);
3559
+ return content2 === message.content ? message : { ...message, content: content2 };
3560
+ }
3561
+ const content = sanitizeParts(message.content);
3562
+ return content === message.content ? message : { ...message, content };
3563
+ }
3564
+ function sanitizeMessagesForWire(messages) {
3565
+ let sanitized;
3566
+ for (let index = 0; index < messages.length; index++) {
3567
+ const message = messages[index];
3568
+ const next = sanitizeMessage(message);
3569
+ if (next === message) continue;
3570
+ sanitized ??= messages.slice();
3571
+ sanitized[index] = next;
3572
+ }
3573
+ return sanitized ?? messages;
3574
+ }
3575
+
3442
3576
  // src/stream.ts
3443
3577
  var GLM_CODING_BASE_URL = "https://api.z.ai/api/coding/paas/v4";
3444
3578
  var KIMI_CODE_USER_AGENT = `kimi-code-cli/${process.env.KIMI_CODE_VERSION ?? "1.0.11"}`;
@@ -3556,7 +3690,7 @@ function stream(options) {
3556
3690
  }
3557
3691
  const wireMessages = stripMessageProvenance(options.messages);
3558
3692
  const messages = clampProviderContextImages(
3559
- wireMessages,
3693
+ sanitizeMessagesForWire(wireMessages),
3560
3694
  options.provider,
3561
3695
  options.supportsImages
3562
3696
  );
@@ -3957,6 +4091,7 @@ export {
3957
4091
  environmentSecrets,
3958
4092
  formatError,
3959
4093
  formatErrorForDisplay,
4094
+ hasLoneSurrogate,
3960
4095
  isHardBillingMessage,
3961
4096
  isUsageLimitError,
3962
4097
  localWireModelId,
@@ -3969,9 +4104,13 @@ export {
3969
4104
  redactText,
3970
4105
  redactValue,
3971
4106
  registerPalsuProvider,
4107
+ sanitizeMessagesForWire,
3972
4108
  setProviderDiagnostic,
4109
+ sliceHead,
4110
+ sliceTail,
3973
4111
  stream,
3974
4112
  toAnthropicMessages,
3975
- toOpenAIMessages
4113
+ toOpenAIMessages,
4114
+ toWellFormedText
3976
4115
  };
3977
4116
  //# sourceMappingURL=index.js.map