@codelia/core 0.1.75 → 0.1.76

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.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { JSONSchema7 } from 'json-schema';
2
+ import { ProviderName, AgentEvent, SessionStateSummary as SessionStateSummary$1, ModelReasoningLevel } from '@codelia/shared-types';
3
+ export { AgentEvent, FinalResponseEvent, LlmRetryEvent, ReasoningEvent, StepCompleteEvent, StepStartEvent, TextEvent, ToolCallEvent, ToolResultEvent } from '@codelia/shared-types';
2
4
  import { ZodSchema } from 'zod';
3
5
  import { Logger } from 'openai/client';
4
- import { AgentEvent, SessionStateSummary as SessionStateSummary$1, ModelReasoningLevel } from '@codelia/shared-types';
5
- export { AgentEvent, FinalResponseEvent, ReasoningEvent, StepCompleteEvent, StepStartEvent, TextEvent, ToolCallEvent, ToolResultEvent } from '@codelia/shared-types';
6
6
  import Anthropic from '@anthropic-ai/sdk';
7
7
  import { MessageCreateParamsNonStreaming, OutputConfig } from '@anthropic-ai/sdk/resources/messages/messages';
8
8
  import OpenAI, { ClientOptions } from 'openai';
@@ -68,16 +68,30 @@ type HostedSearchUserLocation = {
68
68
  region?: string;
69
69
  timezone?: string;
70
70
  };
71
- type HostedSearchToolDefinition = {
71
+ type HostedWebSearchToolDefinition = {
72
72
  type: "hosted_search";
73
+ search_kind?: "web";
73
74
  name: string;
74
- provider?: "openai" | "anthropic" | "openrouter" | "google";
75
+ provider?: "openai" | "anthropic" | "openrouter" | "google" | "xai";
75
76
  search_context_size?: "low" | "medium" | "high";
76
77
  allowed_domains?: string[];
77
78
  blocked_domains?: string[];
78
79
  max_uses?: number;
79
80
  user_location?: HostedSearchUserLocation;
80
81
  };
82
+ type HostedXSearchToolDefinition = {
83
+ type: "hosted_search";
84
+ search_kind: "x";
85
+ name: "x_search";
86
+ provider: "xai";
87
+ allowed_x_handles?: string[];
88
+ excluded_x_handles?: string[];
89
+ from_date?: string;
90
+ to_date?: string;
91
+ enable_image_understanding?: boolean;
92
+ enable_video_understanding?: boolean;
93
+ };
94
+ type HostedSearchToolDefinition = HostedWebSearchToolDefinition | HostedXSearchToolDefinition;
81
95
  type ToolDefinition = FunctionToolDefinition | HostedSearchToolDefinition;
82
96
  type ToolChoice = "auto" | "required" | "none" | string;
83
97
  type JsonValue = string | number | boolean | null | {
@@ -222,9 +236,36 @@ type AgentServices = {
222
236
  modelMetadata?: ModelMetadataService;
223
237
  toolOutputCacheStore?: ToolOutputCacheStore | null;
224
238
  monotonicNowMs?: () => number;
239
+ llmRetryNowMs?: () => number;
240
+ llmRetryRandom?: () => number;
241
+ llmRetrySleep?: (delayMs: number, signal?: AbortSignal) => Promise<void>;
225
242
  };
226
243
 
227
- type ProviderName = "openai" | "anthropic" | "openrouter" | "google" | "zai";
244
+ type ProviderFailureKind = "rate_limit" | "overloaded" | "timeout" | "network" | "hard_quota" | "auth" | "permission" | "validation" | "provider" | "cancelled";
245
+ type RetryDelaySource = "retry-after" | "reset-header" | "provider-body" | "backoff";
246
+ type ProviderFailure = {
247
+ provider: ProviderName;
248
+ kind: ProviderFailureKind;
249
+ retryable: boolean;
250
+ safeMessage: string;
251
+ status?: number;
252
+ retryAfterMs?: number;
253
+ delaySource?: Exclude<RetryDelaySource, "backoff">;
254
+ delivery?: "none" | "buffered" | "committed";
255
+ };
256
+ type ProviderFailureClassifier = (error: unknown) => ProviderFailure | null;
257
+ declare class ProviderFailureError extends Error {
258
+ readonly failure: ProviderFailure;
259
+ readonly attempts: number;
260
+ readonly maxAttempts: number;
261
+ constructor(failure: ProviderFailure, options?: {
262
+ attempts?: number;
263
+ maxAttempts?: number;
264
+ cause?: unknown;
265
+ });
266
+ }
267
+ declare const isProviderFailureError: (error: unknown) => error is ProviderFailureError;
268
+
228
269
  type ChatInvokeInput = {
229
270
  messages: BaseMessage[];
230
271
  model?: string;
@@ -241,6 +282,7 @@ interface BaseChatModel<P extends ProviderName = ProviderName, O = unknown> {
241
282
  ainvoke(input: ChatInvokeInput & {
242
283
  options?: O;
243
284
  }, context?: ChatInvokeContext): Promise<ChatInvokeCompletion>;
285
+ classifyFailure?: ProviderFailureClassifier;
244
286
  onHistoryCompacted?(context?: ChatInvokeContext): void | Promise<void>;
245
287
  }
246
288
 
@@ -502,6 +544,11 @@ type RunErrorRecord = {
502
544
  name: string;
503
545
  message: string;
504
546
  stack?: string;
547
+ provider?: ProviderName;
548
+ kind?: ProviderFailureKind;
549
+ status?: number;
550
+ attempts?: number;
551
+ max_attempts?: number;
505
552
  };
506
553
  meta?: Record<string, unknown>;
507
554
  };
@@ -571,7 +618,8 @@ type AgentOptions = {
571
618
  llmMaxRetries?: number;
572
619
  llmRetryBaseDelayMs?: number;
573
620
  llmRetryMaxDelayMs?: number;
574
- llmRetryableStatusCodes?: number[];
621
+ llmMaxRetryAfterMs?: number;
622
+ llmRetryWindowMs?: number;
575
623
  canExecuteTool?: ToolPermissionHook;
576
624
  };
577
625
  declare class Agent {
@@ -588,6 +636,8 @@ declare class Agent {
588
636
  private readonly modelRegistry;
589
637
  private readonly canExecuteTool?;
590
638
  private readonly monotonicNowMs;
639
+ private readonly llmRetryPolicy;
640
+ private readonly llmRetryDependencies;
591
641
  private history;
592
642
  private usageService;
593
643
  constructor(options: AgentOptions);
@@ -598,15 +648,14 @@ declare class Agent {
598
648
  private checkAndCompact;
599
649
  private trimToolOutputs;
600
650
  private processToolMessage;
601
- private buildToolContext;
602
651
  private recordLlmRequest;
603
652
  private resolveSessionKey;
604
653
  private buildInvokeContext;
605
654
  private recordLlmResponse;
655
+ private invokeModelWithRetry;
606
656
  run(message: string | ContentPart[], options?: AgentRunOptions): Promise<string>;
607
657
  runStream(message: string | ContentPart[], options?: AgentRunOptions): AsyncGenerator<AgentEvent>;
608
658
  private generateFinalResponse;
609
- private executeToolCall;
610
659
  }
611
660
 
612
661
  type StringifyContentMode = "display" | "log";
@@ -639,7 +688,7 @@ interface StoragePathService {
639
688
  resolvePaths(options?: ResolveStorageOptions): StoragePaths;
640
689
  }
641
690
 
642
- declare const PROVIDER_NAME$3: "anthropic";
691
+ declare const PROVIDER_NAME$5: "anthropic";
643
692
  type AnthropicOutputEffort = NonNullable<OutputConfig["effort"]> | "xhigh";
644
693
  type AnthropicOutputConfig = Omit<OutputConfig, "effort"> & {
645
694
  effort?: AnthropicOutputEffort | null;
@@ -662,9 +711,10 @@ type ChatAnthropicOptions = {
662
711
  reasoningFallbackApplied?: boolean;
663
712
  reasoningBudgetPreset?: string;
664
713
  };
665
- declare class ChatAnthropic implements BaseChatModel<typeof PROVIDER_NAME$3, AnthropicInvokeOptions> {
666
- readonly provider: typeof PROVIDER_NAME$3;
714
+ declare class ChatAnthropic implements BaseChatModel<typeof PROVIDER_NAME$5, AnthropicInvokeOptions> {
715
+ readonly provider: typeof PROVIDER_NAME$5;
667
716
  readonly model: string;
717
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
668
718
  private readonly client;
669
719
  private readonly defaultMaxTokens;
670
720
  private readonly defaultInvokeOptions;
@@ -681,6 +731,38 @@ declare class ChatAnthropic implements BaseChatModel<typeof PROVIDER_NAME$3, Ant
681
731
  private debugResponseIfEnabled;
682
732
  }
683
733
 
734
+ declare const PROVIDER_NAME$4: "moonshot";
735
+ type MoonshotInvokeOptions = {
736
+ max_completion_tokens?: number;
737
+ response_format?: unknown;
738
+ };
739
+ type ChatMoonshotOptions = {
740
+ apiKey?: string;
741
+ baseURL?: string;
742
+ fetch?: typeof fetch;
743
+ model?: string;
744
+ timeoutMs?: number;
745
+ maxRetries?: number;
746
+ reasoningLevelRequested?: ModelReasoningLevel;
747
+ };
748
+ declare class ChatMoonshot implements BaseChatModel<typeof PROVIDER_NAME$4, MoonshotInvokeOptions> {
749
+ readonly provider: typeof PROVIDER_NAME$4;
750
+ readonly model: string;
751
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
752
+ private readonly apiKey?;
753
+ private readonly clientOptions;
754
+ private readonly reasoningLevelRequested;
755
+ private debugInvokeSeq;
756
+ private lastDebugRequestPayload;
757
+ constructor(options?: ChatMoonshotOptions);
758
+ ainvoke(input: ChatInvokeInput & {
759
+ options?: MoonshotInvokeOptions;
760
+ }): Promise<ChatInvokeCompletion>;
761
+ private nextDebugInvokeSeq;
762
+ private debugRequestIfEnabled;
763
+ private debugResponseIfEnabled;
764
+ }
765
+
684
766
  /**
685
767
  * Responses API reasoning values supported by Codelia models. The generated
686
768
  * OpenAI SDK type can lag newly released provider values such as `max`.
@@ -699,7 +781,7 @@ type OpenAiResponsesWsLike = {
699
781
  }): void;
700
782
  };
701
783
 
702
- declare const PROVIDER_NAME$2: "openai";
784
+ declare const PROVIDER_NAME$3: "openai";
703
785
  type OpenAITextVerbosity = Exclude<ResponseTextConfig["verbosity"], null>;
704
786
  type OpenAIInvokeOptions = Omit<ResponseCreateParamsBase, "model" | "input" | "tools" | "tool_choice" | "stream"> & {
705
787
  reasoningEffort?: ResponsesReasoningEffort;
@@ -727,9 +809,10 @@ type ChatOpenAIOptions = {
727
809
  websocketConnectTimeoutMs?: number;
728
810
  websocketResponseIdleTimeoutMs?: number;
729
811
  };
730
- declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$2, OpenAIInvokeOptions> {
731
- readonly provider: typeof PROVIDER_NAME$2;
812
+ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$3, OpenAIInvokeOptions> {
813
+ readonly provider: typeof PROVIDER_NAME$3;
732
814
  readonly model: string;
815
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
733
816
  private readonly providerModel;
734
817
  private readonly client;
735
818
  private readonly defaultReasoningEffort?;
@@ -771,7 +854,7 @@ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$2, OpenAI
771
854
  private getHttpStreamDebugObserver;
772
855
  }
773
856
 
774
- declare const PROVIDER_NAME$1: "openrouter";
857
+ declare const PROVIDER_NAME$2: "openrouter";
775
858
  type OpenRouterTextVerbosity = Exclude<ResponseTextConfig["verbosity"], null>;
776
859
  type OpenRouterInvokeOptions = Omit<ResponseCreateParamsBase, "model" | "input" | "tools" | "tool_choice" | "stream"> & {
777
860
  reasoningEffort?: ResponsesReasoningEffort;
@@ -787,9 +870,10 @@ type ChatOpenRouterOptions = {
787
870
  reasoningFallbackApplied?: boolean;
788
871
  textVerbosity?: OpenRouterTextVerbosity;
789
872
  };
790
- declare class ChatOpenRouter implements BaseChatModel<typeof PROVIDER_NAME$1, OpenRouterInvokeOptions> {
791
- readonly provider: typeof PROVIDER_NAME$1;
873
+ declare class ChatOpenRouter implements BaseChatModel<typeof PROVIDER_NAME$2, OpenRouterInvokeOptions> {
874
+ readonly provider: typeof PROVIDER_NAME$2;
792
875
  readonly model: string;
876
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
793
877
  private readonly client;
794
878
  private readonly defaultReasoningEffort?;
795
879
  private readonly reasoningLevelMeta;
@@ -805,6 +889,38 @@ declare class ChatOpenRouter implements BaseChatModel<typeof PROVIDER_NAME$1, Op
805
889
  private debugResponseIfEnabled;
806
890
  }
807
891
 
892
+ declare const PROVIDER_NAME$1: "xai";
893
+ type XaiReasoningEffort = "low" | "medium" | "high";
894
+ type XaiInvokeOptions = Omit<ResponseCreateParamsBase, "model" | "input" | "tools" | "tool_choice" | "stream" | "reasoning"> & {
895
+ reasoningEffort?: XaiReasoningEffort;
896
+ };
897
+ type ChatXaiOptions = {
898
+ client?: OpenAI;
899
+ clientOptions?: ClientOptions;
900
+ model?: string;
901
+ reasoningEffort?: XaiReasoningEffort;
902
+ reasoningLevelRequested?: ModelReasoningLevel;
903
+ reasoningLevelApplied?: ModelReasoningLevel;
904
+ reasoningFallbackApplied?: boolean;
905
+ };
906
+ declare class ChatXai implements BaseChatModel<typeof PROVIDER_NAME$1, XaiInvokeOptions> {
907
+ readonly provider: typeof PROVIDER_NAME$1;
908
+ readonly model: string;
909
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
910
+ private readonly client;
911
+ private readonly defaultReasoningEffort;
912
+ private readonly reasoningLevelMeta;
913
+ private debugInvokeSeq;
914
+ private lastDebugRequestPayload;
915
+ constructor(options?: ChatXaiOptions);
916
+ ainvoke(input: ChatInvokeInput & {
917
+ options?: XaiInvokeOptions;
918
+ }, context?: ChatInvokeContext): Promise<ChatInvokeCompletion>;
919
+ private nextDebugInvokeSeq;
920
+ private debugRequestIfEnabled;
921
+ private debugResponseIfEnabled;
922
+ }
923
+
808
924
  type ZaiReasoningEffort = "high" | "max";
809
925
 
810
926
  declare const PROVIDER_NAME: "zai";
@@ -830,6 +946,7 @@ type ChatZaiOptions = {
830
946
  declare class ChatZai implements BaseChatModel<typeof PROVIDER_NAME, ZaiInvokeOptions> {
831
947
  readonly provider: typeof PROVIDER_NAME;
832
948
  readonly model: string;
949
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
833
950
  private readonly apiKey?;
834
951
  private readonly baseURL;
835
952
  private readonly fetchImpl;
@@ -853,10 +970,17 @@ declare const ANTHROPIC_MODELS: ModelSpec[];
853
970
 
854
971
  declare const GOOGLE_MODELS: ModelSpec[];
855
972
 
973
+ declare const MOONSHOT_DEFAULT_MODEL = "kimi-k3";
974
+ declare const MOONSHOT_MODELS: ModelSpec[];
975
+
856
976
  declare const OPENAI_DEFAULT_MODEL = "gpt-5.6";
857
977
  declare const OPENAI_DEFAULT_REASONING_EFFORT = "medium";
858
978
  declare const OPENAI_MODELS: ModelSpec[];
859
979
 
980
+ declare const XAI_DEFAULT_MODEL = "grok-4.5";
981
+ declare const XAI_DEFAULT_REASONING_EFFORT: "high";
982
+ declare const XAI_MODELS: ModelSpec[];
983
+
860
984
  declare const ZAI_DEFAULT_MODEL = "glm-5.2";
861
985
  declare const ZAI_REASONING_EFFORT_MODELS: Set<string>;
862
986
  declare const ZAI_MODELS: ModelSpec[];
@@ -872,4 +996,4 @@ declare class TaskComplete extends Error {
872
996
  constructor(finalMessage?: string);
873
997
  }
874
998
 
875
- export { ANTHROPIC_DEFAULT_MODEL, ANTHROPIC_MODELS, Agent, type AgentEventRecord, type AgentOptions, type AgentRunOptions, type AgentServices, type AgentSession, type BaseChatModel, type BaseMessage, ChatAnthropic, type ChatInvokeContext, type ChatInvokeInput, ChatOpenAI, ChatOpenRouter, ChatZai, type ContentPart, DEFAULT_MODEL_REGISTRY, type DependencyKey, GOOGLE_MODELS, type LlmRequestRecord, type LlmResponseRecord, type ModelCapabilities, type ModelCost, type ModelEntry, type ModelLimits, type ModelMetadataIndex, type ModelMetadataService, type ModelRegistry, type ModelSpec, OPENAI_DEFAULT_MODEL, OPENAI_DEFAULT_REASONING_EFFORT, OPENAI_MODELS, type ResolveStorageOptions, type RunContextRecord, type RunEndRecord, type RunErrorRecord, type RunEventStoreFactory, type RunEventStoreInit, type RunStartRecord, type RunStatusRecord, type SessionHeader, type SessionRecord, type SessionState, type SessionStateStore, type SessionStateSummary, type SessionStore, type StorageLayout, type StoragePathService, type StoragePaths, type StringifyContentMode, type StringifyContentOptions, TaskComplete, type Tool, type ToolChoice, type ToolContext, type ToolDefinition, type ToolExecution, type ToolOutputCacheConfig, type ToolOutputCacheReadOptions, type ToolOutputCacheRecord, type ToolOutputCacheSearchOptions, ToolOutputCacheService, type ToolOutputCacheStore, type ToolOutputRecord, type ToolOutputRef, type ToolPermissionDecision, type ToolPermissionHook, type ToolResult, ZAI_DEFAULT_MODEL, ZAI_MODELS, ZAI_REASONING_EFFORT_MODELS, applyModelMetadata, createModelRegistry, defineTool, getDefaultSystemPromptPath, isUsableModelSpec, listModels, registerModels, resolveModel, resolveProviderModelId, stringifyContent, stringifyContentParts, supportsFastMode };
999
+ export { ANTHROPIC_DEFAULT_MODEL, ANTHROPIC_MODELS, Agent, type AgentEventRecord, type AgentOptions, type AgentRunOptions, type AgentServices, type AgentSession, type BaseChatModel, type BaseMessage, ChatAnthropic, type ChatInvokeContext, type ChatInvokeInput, ChatMoonshot, ChatOpenAI, ChatOpenRouter, ChatXai, ChatZai, type ContentPart, DEFAULT_MODEL_REGISTRY, type DependencyKey, GOOGLE_MODELS, type LlmRequestRecord, type LlmResponseRecord, MOONSHOT_DEFAULT_MODEL, MOONSHOT_MODELS, type ModelCapabilities, type ModelCost, type ModelEntry, type ModelLimits, type ModelMetadataIndex, type ModelMetadataService, type ModelRegistry, type ModelSpec, OPENAI_DEFAULT_MODEL, OPENAI_DEFAULT_REASONING_EFFORT, OPENAI_MODELS, type ProviderFailure, type ProviderFailureClassifier, ProviderFailureError, type ProviderFailureKind, type ResolveStorageOptions, type RetryDelaySource, type RunContextRecord, type RunEndRecord, type RunErrorRecord, type RunEventStoreFactory, type RunEventStoreInit, type RunStartRecord, type RunStatusRecord, type SessionHeader, type SessionRecord, type SessionState, type SessionStateStore, type SessionStateSummary, type SessionStore, type StorageLayout, type StoragePathService, type StoragePaths, type StringifyContentMode, type StringifyContentOptions, TaskComplete, type Tool, type ToolChoice, type ToolContext, type ToolDefinition, type ToolExecution, type ToolOutputCacheConfig, type ToolOutputCacheReadOptions, type ToolOutputCacheRecord, type ToolOutputCacheSearchOptions, ToolOutputCacheService, type ToolOutputCacheStore, type ToolOutputRecord, type ToolOutputRef, type ToolPermissionDecision, type ToolPermissionHook, type ToolResult, XAI_DEFAULT_MODEL, XAI_DEFAULT_REASONING_EFFORT, XAI_MODELS, ZAI_DEFAULT_MODEL, ZAI_MODELS, ZAI_REASONING_EFFORT_MODELS, applyModelMetadata, createModelRegistry, defineTool, getDefaultSystemPromptPath, isProviderFailureError, isUsableModelSpec, listModels, registerModels, resolveModel, resolveProviderModelId, stringifyContent, stringifyContentParts, supportsFastMode };