@codelia/core 0.1.81 → 0.1.83

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,6 +1,7 @@
1
+ import * as _codelia_shared_types from '@codelia/shared-types';
2
+ import { LlmCallTiming, ProviderName, ModelFastMode, AgentEvent, SessionStateSummary as SessionStateSummary$1, ModelReasoningLevel } from '@codelia/shared-types';
3
+ export { AgentEvent, FinalResponseEvent, LlmRetryEvent, LlmStatusEvent, ReasoningEvent, StepCompleteEvent, StepStartEvent, TextEvent, ToolCallEvent, ToolResultEvent } from '@codelia/shared-types';
1
4
  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';
4
5
  import { ZodSchema } from 'zod';
5
6
  import { Logger } from 'openai/client';
6
7
  import Anthropic from '@anthropic-ai/sdk';
@@ -9,10 +10,16 @@ import OpenAI, { ClientOptions } from 'openai';
9
10
  import { ResponsesClientEvent, ResponseCreateParamsBase, ResponseTextConfig } from 'openai/resources/responses/responses';
10
11
  import { ResponsesWS } from 'openai/resources/responses/ws';
11
12
  import { ReasoningEffort } from 'openai/resources/shared';
13
+ import { ResponsesProviderConfig } from '@codelia/config';
12
14
 
13
15
  /**
14
16
  * Message Content Parts
15
17
  */
18
+ type ImageRefPart = {
19
+ type: "image_ref";
20
+ image_ref: _codelia_shared_types.ImageArtifactRef;
21
+ detail?: "auto" | "low" | "high";
22
+ };
16
23
  type TextPart = {
17
24
  type: "text";
18
25
  text: string;
@@ -38,7 +45,7 @@ type OtherPart = {
38
45
  kind: string;
39
46
  payload: unknown;
40
47
  };
41
- type ContentPart = TextPart | ImagePart | DocumentPart | OtherPart;
48
+ type ContentPart = TextPart | ImagePart | ImageRefPart | DocumentPart | OtherPart;
42
49
 
43
50
  /**
44
51
  * Tool Call
@@ -129,6 +136,8 @@ type AssistantMessage = {
129
136
  name?: string;
130
137
  tool_calls?: ToolCall[];
131
138
  refusal?: string | null;
139
+ /** Provider-owned response metadata, preserved for exact history replay. */
140
+ provider_meta?: unknown;
132
141
  };
133
142
  type ToolOutputRef = {
134
143
  id: string;
@@ -159,10 +168,12 @@ type ChatInvokeUsage = {
159
168
  input_cache_creation_tokens?: number | null;
160
169
  input_image_tokens?: number | null;
161
170
  output_tokens: number;
171
+ output_reasoning_tokens?: number | null;
162
172
  total_tokens: number;
163
173
  };
164
174
  type ChatInvokeCompletion = {
165
175
  messages: BaseMessage[];
176
+ timing?: LlmCallTiming;
166
177
  usage?: ChatInvokeUsage | null;
167
178
  stop_reason?: string | null;
168
179
  provider_meta?: unknown;
@@ -233,6 +244,7 @@ interface ModelMetadataService {
233
244
  }
234
245
 
235
246
  type AgentServices = {
247
+ resolveImage?: (ref: _codelia_shared_types.ImageArtifactRef, signal?: AbortSignal) => Promise<ImagePart>;
236
248
  modelMetadata?: ModelMetadataService;
237
249
  toolOutputCacheStore?: ToolOutputCacheStore | null;
238
250
  monotonicNowMs?: () => number;
@@ -249,6 +261,7 @@ type ProviderFailure = {
249
261
  retryable: boolean;
250
262
  safeMessage: string;
251
263
  status?: number;
264
+ code?: string;
252
265
  retryAfterMs?: number;
253
266
  delaySource?: Exclude<RetryDelaySource, "backoff">;
254
267
  delivery?: "none" | "buffered" | "committed";
@@ -266,6 +279,24 @@ declare class ProviderFailureError extends Error {
266
279
  }
267
280
  declare const isProviderFailureError: (error: unknown) => error is ProviderFailureError;
268
281
 
282
+ type LlmRetryPolicy = {
283
+ maxRetries: number;
284
+ baseDelayMs: number;
285
+ maxDelayMs: number;
286
+ maxRetryAfterMs: number;
287
+ retryWindowMs: number;
288
+ };
289
+
290
+ type ImageTokenInput = {
291
+ width?: number;
292
+ height?: number;
293
+ detail?: "auto" | "low" | "high";
294
+ };
295
+ type ImageTokenEstimate = {
296
+ tokens: number;
297
+ method: "patch" | "tile" | "fallback";
298
+ };
299
+
269
300
  type ChatInvokeInput = {
270
301
  messages: BaseMessage[];
271
302
  model?: string;
@@ -275,6 +306,9 @@ type ChatInvokeInput = {
275
306
  };
276
307
  type ChatInvokeContext = {
277
308
  sessionKey?: string;
309
+ /** One user turn; distinct from a durable session key. */
310
+ turnKey?: string;
311
+ onProviderStatus?: (event: _codelia_shared_types.LlmStatusEvent) => void;
278
312
  };
279
313
  interface BaseChatModel<P extends ProviderName = ProviderName, O = unknown> {
280
314
  readonly provider: P;
@@ -283,6 +317,10 @@ interface BaseChatModel<P extends ProviderName = ProviderName, O = unknown> {
283
317
  options?: O;
284
318
  }, context?: ChatInvokeContext): Promise<ChatInvokeCompletion>;
285
319
  classifyFailure?: ProviderFailureClassifier;
320
+ /** Endpoint-owned retry policy takes precedence over Agent defaults. */
321
+ readonly retryPolicy?: Partial<LlmRetryPolicy>;
322
+ /** Optional model-specific vision estimate; never a billing usage report. */
323
+ estimateImageTokens?(image: ImageTokenInput): ImageTokenEstimate;
286
324
  onHistoryCompacted?(context?: ChatInvokeContext): void | Promise<void>;
287
325
  }
288
326
 
@@ -302,7 +340,7 @@ type ModelSpec = {
302
340
  };
303
341
  type ModelRegistry = {
304
342
  modelsById: Record<string, ModelSpec>;
305
- aliasesByProvider: Record<ProviderName, Record<string, string>>;
343
+ aliasesByProvider: Record<string, Record<string, string>>;
306
344
  };
307
345
  declare function createModelRegistry(specs: ModelSpec[]): ModelRegistry;
308
346
  declare function registerModels(registry: ModelRegistry, specs: ModelSpec[]): void;
@@ -431,7 +469,7 @@ type SessionHeader = {
431
469
  provider?: string;
432
470
  name?: string;
433
471
  reasoning?: string;
434
- fast?: boolean;
472
+ fast?: ModelFastMode;
435
473
  source?: "config" | "session";
436
474
  };
437
475
  prompts?: {
@@ -459,17 +497,7 @@ type RunStartRecord = {
459
497
  text: string;
460
498
  } | {
461
499
  type: "parts";
462
- parts: Array<{
463
- type: "text";
464
- text: string;
465
- } | {
466
- type: "image_url";
467
- image_url: {
468
- url: string;
469
- detail?: "auto" | "low" | "high";
470
- media_type?: "image/png" | "image/jpeg" | "image/webp" | "image/gif";
471
- };
472
- }>;
500
+ parts: ContentPart[];
473
501
  };
474
502
  ui_context?: unknown;
475
503
  meta?: Record<string, unknown>;
@@ -509,7 +537,7 @@ type LlmRequestRecord = {
509
537
  provider?: string;
510
538
  name?: string;
511
539
  reasoning?: string;
512
- fast?: boolean;
540
+ fast?: ModelFastMode;
513
541
  };
514
542
  input: {
515
543
  messages: BaseMessage[];
@@ -527,6 +555,7 @@ type LlmResponseRecord = {
527
555
  seq: number;
528
556
  output: {
529
557
  messages: BaseMessage[];
558
+ timing?: LlmCallTiming;
530
559
  usage?: ChatInvokeUsage | null;
531
560
  stop_reason?: string | null;
532
561
  provider_meta?: unknown;
@@ -552,6 +581,7 @@ type RunErrorRecord = {
552
581
  provider?: ProviderName;
553
582
  kind?: ProviderFailureKind;
554
583
  status?: number;
584
+ code?: string;
555
585
  attempts?: number;
556
586
  max_attempts?: number;
557
587
  };
@@ -647,6 +677,7 @@ declare class Agent {
647
677
  private readonly llmRetryDependencies;
648
678
  private history;
649
679
  private usageService;
680
+ private imageUsageBaseline?;
650
681
  constructor(options: AgentOptions);
651
682
  getUsageSummary(): UsageSummary;
652
683
  getContextLeftPercent(): number | null;
@@ -751,7 +782,9 @@ type ChatMoonshotOptions = {
751
782
  timeoutMs?: number;
752
783
  maxRetries?: number;
753
784
  reasoningLevelRequested?: ModelReasoningLevel;
785
+ reasoningEffort?: MoonshotReasoningEffort;
754
786
  };
787
+ type MoonshotReasoningEffort = "low" | "high" | "max";
755
788
  declare class ChatMoonshot implements BaseChatModel<typeof PROVIDER_NAME$4, MoonshotInvokeOptions> {
756
789
  readonly provider: typeof PROVIDER_NAME$4;
757
790
  readonly model: string;
@@ -759,6 +792,7 @@ declare class ChatMoonshot implements BaseChatModel<typeof PROVIDER_NAME$4, Moon
759
792
  private readonly apiKey?;
760
793
  private readonly clientOptions;
761
794
  private readonly reasoningLevelRequested;
795
+ private readonly reasoningEffort;
762
796
  private debugInvokeSeq;
763
797
  private lastDebugRequestPayload;
764
798
  constructor(options?: ChatMoonshotOptions);
@@ -790,10 +824,12 @@ type OpenAiResponsesWsLike = {
790
824
 
791
825
  declare const PROVIDER_NAME$3: "openai";
792
826
  type OpenAITextVerbosity = Exclude<ResponseTextConfig["verbosity"], null>;
827
+ /** The SDK service-tier union can lag public OpenAI API additions. */
828
+ type OpenAiServiceTier = ResponseCreateParamsBase["service_tier"] | "ultrafast";
793
829
  type OpenAIInvokeOptions = Omit<ResponseCreateParamsBase, "model" | "input" | "tools" | "tool_choice" | "stream"> & {
794
830
  reasoningEffort?: ResponsesReasoningEffort;
795
831
  textVerbosity?: OpenAITextVerbosity;
796
- serviceTier?: ResponseCreateParamsBase["service_tier"];
832
+ serviceTier?: OpenAiServiceTier;
797
833
  };
798
834
  type OpenAIResponsesWsOptions = NonNullable<ConstructorParameters<typeof ResponsesWS>[1]> & {
799
835
  headers?: Record<string, string>;
@@ -809,7 +845,7 @@ type ChatOpenAIOptions = {
809
845
  reasoningLevelApplied?: ModelReasoningLevel;
810
846
  reasoningFallbackApplied?: boolean;
811
847
  textVerbosity?: OpenAITextVerbosity;
812
- serviceTier?: ResponseCreateParamsBase["service_tier"];
848
+ serviceTier?: OpenAiServiceTier;
813
849
  websocketMode?: OpenAiWebsocketMode;
814
850
  websocketApiVersion?: OpenAiWebsocketApiVersion;
815
851
  createResponsesWs?: (client: OpenAI, options?: OpenAIResponsesWsOptions) => OpenAiResponsesWsLike;
@@ -834,10 +870,13 @@ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$3, OpenAI
834
870
  private readonly wsStateBySessionKey;
835
871
  private readonly wsDisabledUntilBySessionKey;
836
872
  private wsReconnectCount;
873
+ private readonly codexTurns;
837
874
  constructor(options?: ChatOpenAIOptions);
838
875
  ainvoke(input: ChatInvokeInput & {
839
876
  options?: OpenAIInvokeOptions;
840
877
  }, context?: ChatInvokeContext): Promise<ChatInvokeCompletion>;
878
+ private createCodexMetadata;
879
+ private resolveCodexTurn;
841
880
  onHistoryCompacted(context?: ChatInvokeContext): void;
842
881
  private invokeWithTransport;
843
882
  private invokeHttpTransportWithDebug;
@@ -846,7 +885,6 @@ declare class ChatOpenAI implements BaseChatModel<typeof PROVIDER_NAME$3, OpenAI
846
885
  private invokeViaWs;
847
886
  private closeWsSafely;
848
887
  private closeWsWithoutMaskingPrimaryError;
849
- private isWsConnectionReuseExpired;
850
888
  private isWsReconnectRetryableError;
851
889
  private isWsConnectionReusable;
852
890
  private extractWsErrorCode;
@@ -897,7 +935,7 @@ declare class ChatOpenRouter implements BaseChatModel<typeof PROVIDER_NAME$2, Op
897
935
  }
898
936
 
899
937
  declare const PROVIDER_NAME$1: "xai";
900
- type XaiReasoningEffort = "low" | "medium" | "high";
938
+ type XaiReasoningEffort = "low" | "medium" | "high" | "xhigh";
901
939
  type XaiInvokeOptions = Omit<ResponseCreateParamsBase, "model" | "input" | "tools" | "tool_choice" | "stream" | "reasoning"> & {
902
940
  reasoningEffort?: XaiReasoningEffort;
903
941
  };
@@ -928,10 +966,10 @@ declare class ChatXai implements BaseChatModel<typeof PROVIDER_NAME$1, XaiInvoke
928
966
  private debugResponseIfEnabled;
929
967
  }
930
968
 
931
- type ZaiReasoningEffort = "high" | "max";
969
+ type ZaiReasoningEffort = "low" | "high" | "max";
932
970
 
933
971
  declare const PROVIDER_NAME: "zai";
934
- type ZaiAppliedReasoningLevel = Extract<ModelReasoningLevel, "high" | "xhigh" | "max">;
972
+ type ZaiAppliedReasoningLevel = Extract<ModelReasoningLevel, "low" | "high" | "xhigh" | "max">;
935
973
  type ZaiInvokeOptions = {
936
974
  max_tokens?: number;
937
975
  temperature?: number;
@@ -978,6 +1016,7 @@ declare const ANTHROPIC_MODELS: ModelSpec[];
978
1016
  declare const GOOGLE_MODELS: ModelSpec[];
979
1017
 
980
1018
  declare const MOONSHOT_DEFAULT_MODEL = "kimi-k3";
1019
+ declare const MOONSHOT_K3_1_MODELS: Set<string>;
981
1020
  declare const MOONSHOT_MODELS: ModelSpec[];
982
1021
 
983
1022
  declare const OPENAI_DEFAULT_MODEL = "gpt-5.6";
@@ -1003,4 +1042,36 @@ declare class TaskComplete extends Error {
1003
1042
  constructor(finalMessage?: string);
1004
1043
  }
1005
1044
 
1006
- 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, ToolPermissionDenied, 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 };
1045
+ type ResponsesInvokeOptions = {
1046
+ reasoningEffort?: ModelReasoningLevel;
1047
+ maxOutputTokens?: number;
1048
+ text?: ResponseCreateParamsBase["text"];
1049
+ };
1050
+ /** Stateless HTTP Responses transport, independently named and authenticated. */
1051
+ declare class ChatResponses implements BaseChatModel<string, ResponsesInvokeOptions> {
1052
+ readonly provider: string;
1053
+ readonly model: string;
1054
+ readonly retryPolicy: {
1055
+ baseDelayMs?: number | undefined;
1056
+ maxRetries: number;
1057
+ };
1058
+ private readonly client;
1059
+ private readonly endpoint;
1060
+ private readonly reasoningEffort?;
1061
+ private readonly textVerbosity?;
1062
+ constructor(options: {
1063
+ provider: string;
1064
+ model: string;
1065
+ endpoint: ResponsesProviderConfig;
1066
+ apiKey: string;
1067
+ reasoningEffort?: ModelReasoningLevel;
1068
+ textVerbosity?: "low" | "medium" | "high";
1069
+ fetch?: typeof fetch;
1070
+ });
1071
+ readonly classifyFailure: (error: unknown) => ProviderFailure;
1072
+ ainvoke(input: ChatInvokeInput & {
1073
+ options?: ResponsesInvokeOptions;
1074
+ }, context?: ChatInvokeContext): Promise<ChatInvokeCompletion>;
1075
+ }
1076
+
1077
+ 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, ChatResponses, ChatXai, ChatZai, type ContentPart, DEFAULT_MODEL_REGISTRY, type DependencyKey, GOOGLE_MODELS, type ImageTokenEstimate, type ImageTokenInput, type LlmRequestRecord, type LlmResponseRecord, MOONSHOT_DEFAULT_MODEL, MOONSHOT_K3_1_MODELS, 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 ResponsesInvokeOptions, 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, ToolPermissionDenied, 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 };