@kenkaiiii/gg-ai 4.3.230 → 4.3.231

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
@@ -1,4 +1,6 @@
1
1
  import { z } from 'zod';
2
+ import Anthropic from '@anthropic-ai/sdk';
3
+ import OpenAI from 'openai';
2
4
 
3
5
  type Provider = "anthropic" | "xiaomi" | "openai" | "gemini" | "glm" | "moonshot" | "minimax" | "deepseek" | "openrouter" | "palsu";
4
6
  type ThinkingLevel = "low" | "medium" | "high" | "xhigh" | "max";
@@ -396,6 +398,19 @@ type ProviderDiagnosticFn = (phase: string, data?: Record<string, unknown>) => v
396
398
  /** Register a diagnostic callback for provider-level tracing. */
397
399
  declare function setProviderDiagnostic(fn: ProviderDiagnosticFn | null): void;
398
400
 
401
+ declare function toAnthropicMessages(messages: Message[], cacheControl?: {
402
+ type: "ephemeral";
403
+ ttl?: "1h";
404
+ }): {
405
+ system: Anthropic.TextBlockParam[] | undefined;
406
+ messages: Anthropic.MessageParam[];
407
+ };
408
+ declare function toOpenAIMessages(messages: Message[], options?: {
409
+ provider?: string;
410
+ thinking?: boolean;
411
+ supportsImages?: boolean;
412
+ }): OpenAI.ChatCompletionMessageParam[];
413
+
399
414
  interface PalsuProviderState {
400
415
  callCount: number;
401
416
  }
@@ -465,4 +480,4 @@ interface PalsuProviderConfig {
465
480
  */
466
481
  declare function registerPalsuProvider(config?: PalsuProviderConfig): PalsuProviderHandle;
467
482
 
468
- export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, type RawContent, 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, formatError, formatErrorForDisplay, isUsageLimitError, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, providerRegistry, registerPalsuProvider, setProviderDiagnostic, stream };
483
+ export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, type RawContent, 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, formatError, formatErrorForDisplay, isUsageLimitError, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, providerRegistry, registerPalsuProvider, setProviderDiagnostic, stream, toAnthropicMessages, toOpenAIMessages };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { z } from 'zod';
2
+ import Anthropic from '@anthropic-ai/sdk';
3
+ import OpenAI from 'openai';
2
4
 
3
5
  type Provider = "anthropic" | "xiaomi" | "openai" | "gemini" | "glm" | "moonshot" | "minimax" | "deepseek" | "openrouter" | "palsu";
4
6
  type ThinkingLevel = "low" | "medium" | "high" | "xhigh" | "max";
@@ -396,6 +398,19 @@ type ProviderDiagnosticFn = (phase: string, data?: Record<string, unknown>) => v
396
398
  /** Register a diagnostic callback for provider-level tracing. */
397
399
  declare function setProviderDiagnostic(fn: ProviderDiagnosticFn | null): void;
398
400
 
401
+ declare function toAnthropicMessages(messages: Message[], cacheControl?: {
402
+ type: "ephemeral";
403
+ ttl?: "1h";
404
+ }): {
405
+ system: Anthropic.TextBlockParam[] | undefined;
406
+ messages: Anthropic.MessageParam[];
407
+ };
408
+ declare function toOpenAIMessages(messages: Message[], options?: {
409
+ provider?: string;
410
+ thinking?: boolean;
411
+ supportsImages?: boolean;
412
+ }): OpenAI.ChatCompletionMessageParam[];
413
+
399
414
  interface PalsuProviderState {
400
415
  callCount: number;
401
416
  }
@@ -465,4 +480,4 @@ interface PalsuProviderConfig {
465
480
  */
466
481
  declare function registerPalsuProvider(config?: PalsuProviderConfig): PalsuProviderHandle;
467
482
 
468
- export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, type RawContent, 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, formatError, formatErrorForDisplay, isUsageLimitError, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, providerRegistry, registerPalsuProvider, setProviderDiagnostic, stream };
483
+ export { type AssistantMessage, type CacheRetention, type ContentPart, type DoneEvent, type ErrorEvent, type ErrorSource, EventStream, type FormattedError, GGAIError, type ImageContent, type Message, type PalsuModelConfig, type PalsuModelHandle, type PalsuProviderConfig, type PalsuProviderHandle, type PalsuProviderState, type PalsuResponse, type PalsuResponseFactory, type Provider, type ProviderDiagnosticFn, type ProviderEntry, ProviderError, type ProviderStreamFn, type RawContent, 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, formatError, formatErrorForDisplay, isUsageLimitError, palsuAssistantMessage, palsuText, palsuThinking, palsuToolCall, providerRegistry, registerPalsuProvider, setProviderDiagnostic, stream, toAnthropicMessages, toOpenAIMessages };
package/dist/index.js CHANGED
@@ -356,13 +356,60 @@ function normalizeRootForAnthropic(schema) {
356
356
  }
357
357
 
358
358
  // src/providers/transform.ts
359
+ function hasValidThinkingSignature(part) {
360
+ return typeof part.signature === "string" && part.signature.trim().length > 0;
361
+ }
362
+ function isRawThinking(part) {
363
+ if (part.type !== "raw") return false;
364
+ const t = part.data.type;
365
+ return t === "thinking" || t === "redacted_thinking";
366
+ }
359
367
  function isPositionSensitiveThinking(part) {
360
- if (part.type === "thinking") return !!part.signature;
361
- if (part.type === "raw") {
362
- const t = part.data.type;
363
- return t === "thinking" || t === "redacted_thinking";
368
+ if (part.type === "thinking") return hasValidThinkingSignature(part);
369
+ return isRawThinking(part);
370
+ }
371
+ function toAnthropicAssistantPart(part, idMap) {
372
+ if (part.type === "text") return { type: "text", text: part.text };
373
+ if (part.type === "thinking") {
374
+ const sig = part.signature;
375
+ return sig && sig.trim().length > 0 ? { type: "thinking", thinking: part.text, signature: sig } : { type: "text", text: part.text };
364
376
  }
365
- return false;
377
+ if (part.type === "tool_call")
378
+ return {
379
+ type: "tool_use",
380
+ id: remapAnthropicToolCallId(part.id, idMap),
381
+ name: part.name,
382
+ input: part.args
383
+ };
384
+ if (part.type === "server_tool_call")
385
+ return {
386
+ type: "server_tool_use",
387
+ id: part.id,
388
+ name: part.name,
389
+ input: part.input
390
+ };
391
+ if (part.type === "server_tool_result")
392
+ return part.data;
393
+ if (part.type === "raw") return part.data;
394
+ return null;
395
+ }
396
+ function toAnthropicAssistantContent(content, isLatest, idMap) {
397
+ if (!isLatest) {
398
+ return content.filter((part) => {
399
+ if (part.type === "thinking" || isRawThinking(part)) return false;
400
+ if (part.type === "text" && !part.text) return false;
401
+ return true;
402
+ }).map((part) => toAnthropicAssistantPart(part, idMap)).filter((b) => b !== null);
403
+ }
404
+ const lastThinkingIdx = content.reduce(
405
+ (last, part, idx) => isPositionSensitiveThinking(part) ? idx : last,
406
+ -1
407
+ );
408
+ return content.filter((part, idx) => {
409
+ if (part.type === "thinking" && !hasValidThinkingSignature(part) && !part.text) return false;
410
+ if (part.type === "text" && !part.text && idx > lastThinkingIdx) return false;
411
+ return true;
412
+ }).map((part) => toAnthropicAssistantPart(part, idMap)).filter((b) => b !== null);
366
413
  }
367
414
  var NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
368
415
  var NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
@@ -440,7 +487,13 @@ function toAnthropicMessages(messages, cacheControl) {
440
487
  let systemText;
441
488
  const out = [];
442
489
  const idMap = /* @__PURE__ */ new Map();
490
+ const lastAssistantIdx = messages.reduce(
491
+ (last, m, i) => m.role === "assistant" ? i : last,
492
+ -1
493
+ );
494
+ let msgIdx = -1;
443
495
  for (const msg of messages) {
496
+ msgIdx++;
444
497
  if (msg.role === "system") {
445
498
  systemText = msg.content;
446
499
  continue;
@@ -463,38 +516,7 @@ function toAnthropicMessages(messages, cacheControl) {
463
516
  continue;
464
517
  }
465
518
  if (msg.role === "assistant") {
466
- const lastThinkingIdx = typeof msg.content === "string" ? -1 : msg.content.reduce(
467
- (last, part, idx) => isPositionSensitiveThinking(part) ? idx : last,
468
- -1
469
- );
470
- const content = typeof msg.content === "string" ? msg.content : msg.content.filter((part, idx) => {
471
- if (part.type === "thinking" && !part.signature && !part.text) return false;
472
- if (part.type === "text" && !part.text && idx > lastThinkingIdx) return false;
473
- return true;
474
- }).map((part) => {
475
- if (part.type === "text") return { type: "text", text: part.text };
476
- if (part.type === "thinking") {
477
- return part.signature ? { type: "thinking", thinking: part.text, signature: part.signature } : { type: "text", text: part.text };
478
- }
479
- if (part.type === "tool_call")
480
- return {
481
- type: "tool_use",
482
- id: remapAnthropicToolCallId(part.id, idMap),
483
- name: part.name,
484
- input: part.args
485
- };
486
- if (part.type === "server_tool_call")
487
- return {
488
- type: "server_tool_use",
489
- id: part.id,
490
- name: part.name,
491
- input: part.input
492
- };
493
- if (part.type === "server_tool_result")
494
- return part.data;
495
- if (part.type === "raw") return part.data;
496
- return null;
497
- }).filter(Boolean);
519
+ const content = typeof msg.content === "string" ? msg.content : toAnthropicAssistantContent(msg.content, msgIdx === lastAssistantIdx, idMap);
498
520
  if (Array.isArray(content) && content.length === 0) continue;
499
521
  out.push({ role: "assistant", content });
500
522
  continue;
@@ -2825,6 +2847,8 @@ export {
2825
2847
  providerRegistry,
2826
2848
  registerPalsuProvider,
2827
2849
  setProviderDiagnostic,
2828
- stream
2850
+ stream,
2851
+ toAnthropicMessages,
2852
+ toOpenAIMessages
2829
2853
  };
2830
2854
  //# sourceMappingURL=index.js.map