import type { AxAIService, AxAIServiceOptions, AxChatRequest, AxChatResponse, AxModelConfig, } from '../ai/types.js'; import type { AxAIMemory } from '../mem/types.js'; import type { AxAssertion, AxStreamingAssertion } from './asserts.js'; import type { AxInputFunctionType } from './functions.js'; import type { AxGen } from './generate.js'; import type { AxOptimizedProgram } from './optimizer.js'; import type { AxPromptTemplate } from './prompt.js'; import type { AxSignature, AxSignatureBuilder } from './sig.js'; import type { ParseSignature } from './sigtypes.js'; export type AxFieldValue = | string | string[] | number | boolean | object | null | undefined | { mimeType: string; data: string } | { mimeType: string; data: string }[] | { format?: 'wav'; data: string } | { format?: 'wav'; data: string }[]; export type AxGenIn = { [key: string]: AxFieldValue }; export type AxGenOut = { [key: string]: AxFieldValue }; /** * @deprecated AxMessage will be updated to a new design within this major version. * The current structure will be replaced in v15.0.0. * * Migration timeline: * - v14.0.0+: Deprecation warnings (current) * - v14.x: New message design introduced alongside existing * - v15.0.0: Complete replacement with new design */ export type AxMessage = | { role: 'user'; values: IN } | { role: 'assistant'; values: IN }; export type AxProgramTrace = { trace: OUT & Partial; programId: string; }; export type AxProgramDemos = { traces: (OUT & Partial)[]; programId: string; }; export type AxProgramExamples = | AxProgramDemos | AxProgramDemos['traces']; export type AxResultPickerFunctionFieldResults = { type: 'fields'; results: readonly { index: number; sample: Partial }[]; }; export type AxResultPickerFunctionFunctionResults = { type: 'function'; results: readonly { index: number; functionName: string; functionId: string; args: string | object; result: string; isError?: boolean; }[]; }; export type AxResultPickerFunction = ( data: | AxResultPickerFunctionFieldResults | AxResultPickerFunctionFunctionResults ) => number | Promise; export type AxProgramForwardOptions = AxAIServiceOptions & { // Execution control maxRetries?: number; maxSteps?: number; mem?: AxAIMemory; // AI service and model configuration ai?: AxAIService; modelConfig?: AxModelConfig; model?: MODEL; // Streaming and output sampleCount?: number; resultPicker?: AxResultPickerFunction; // Functions and calls functions?: AxInputFunctionType; functionCall?: AxChatRequest['functionCall']; stopFunction?: string | string[]; functionResultFormatter?: (result: unknown) => string; // Behavior control fastFail?: boolean; showThoughts?: boolean; functionCallMode?: 'auto' | 'native' | 'prompt'; cacheSystemPrompt?: boolean; // Memory tag cleanup control disableMemoryCleanup?: boolean; // Tracing and logging traceLabel?: string; // AxGen-specific options (previously in AxGenOptions) description?: string; thoughtFieldName?: string; promptTemplate?: typeof AxPromptTemplate; asserts?: AxAssertion[]; streamingAsserts?: AxStreamingAssertion[]; excludeContentFromTrace?: boolean; // Field prefix is required for single output field programs strictMode?: boolean; }; export type AxAIServiceActionOptions< TModel = unknown, TEmbedModel = unknown, TModelKey = string, > = AxAIServiceOptions & { ai?: Readonly>; functionResultFormatter?: (result: unknown) => string; }; export type AxProgramStreamingForwardOptions = Omit< AxProgramForwardOptions, 'stream' >; // Helper type to extract model type union from AxAIService (both TModel and TModelKey) export type AxAIServiceModelType< T extends Readonly>, > = T extends Readonly> ? TModel extends unknown ? TModelKey // For AxAI wrapper services, only use TModelKey since TModel is unknown : TModel | TModelKey // For direct services, use both TModel and TModelKey : never; // Clean forward options type that includes both TModel and model keys export type AxProgramForwardOptionsWithModels< T extends Readonly>, > = AxProgramForwardOptions>; // Clean streaming forward options type that includes both TModel and model keys export type AxProgramStreamingForwardOptionsWithModels< T extends Readonly>, > = AxProgramStreamingForwardOptions>; export type AxGenDeltaOut = { version: number; index: number; delta: Partial; }; export type AxGenStreamingOut = AsyncGenerator< AxGenDeltaOut, void, unknown >; export type DeltaOut = Omit, 'version'>; export type AsyncGenDeltaOut = AsyncGenerator< DeltaOut, void, unknown >; export type GenDeltaOut = Generator, void, unknown>; // eslint-disable-next-line @typescript-eslint/no-empty-object-type export type AxSetExamplesOptions = { // No options needed - all fields can be missing in examples }; export interface AxForwardable { forward( ai: Readonly, values: IN | AxMessage[], options?: Readonly> ): Promise; streamingForward( ai: Readonly, values: IN | AxMessage[], options?: Readonly> ): AxGenStreamingOut; } export interface AxTunable { setExamples: ( examples: Readonly>, options?: Readonly ) => void; setId: (id: string) => void; setParentId: (parentId: string) => void; getTraces: () => AxProgramTrace[]; setDemos: (demos: readonly AxProgramDemos[]) => void; applyOptimization: (optimizedProgram: AxOptimizedProgram) => void; } export interface AxUsable { getUsage: () => AxProgramUsage[]; resetUsage: () => void; } export interface AxProgrammable extends AxForwardable, AxTunable, AxUsable { getSignature: () => AxSignature; } export type AxProgramUsage = AxChatResponse['modelUsage'] & { ai: string; model: string; }; export interface AxProgramOptions { description?: string; traceLabel?: string; } // === Signature Parsing Types === // Type system moved to sigtypes.ts for better organization and features export type { ParseSignature } from './sigtypes.js'; // === Examples Type Utility === // Derives the example item type (OUT & Partial) from: // - An AxSignature instance // - An AxSignatureBuilder instance (from f()) // - A string signature (parsed via ParseSignature) export type AxExamples = T extends AxSignature ? OUT & Partial : T extends AxSignatureBuilder ? OUT2 & Partial : T extends AxGen ? OUT4 & Partial : T extends string ? ParseSignature extends { inputs: infer IN3; outputs: infer OUT3; } ? OUT3 & Partial : never : never; export type ExExamples = ReadonlyArray>; // === AxGen Helper Types === // Similar to AxExamples, these extract input/output types from AxGen signatures export type AxGenInput = T extends AxGen ? IN : T extends AxSignature ? IN2 : T extends AxSignatureBuilder ? IN3 : T extends string ? ParseSignature extends { inputs: infer IN4; outputs: any; } ? IN4 : never : never; export type AxGenOutput = T extends AxGen ? OUT : T extends AxSignature ? OUT2 : T extends AxSignatureBuilder ? OUT3 : T extends string ? ParseSignature extends { inputs: any; outputs: infer OUT4; } ? OUT4 : never : never; // ========================= // Optimizer shared type defs // ========================= // Shared optimizer-related types are exported exclusively from `common_types.ts` // to avoid duplicate type exports when generating the package index.