@mastra/core
Version:
207 lines • 10.5 kB
TypeScript
import type { Experimental_DecisionModelV4 as DecisionModelV4, Experimental_DecisionModelV4CallOptions as DecisionModelV4CallOptions, Experimental_DecisionModelV4Input as DecisionModelV4Input, Experimental_DecisionModelV4Result as DecisionModelV4Result, Experimental_EvaluationModelV4 as EvaluationModelV4, SharedV4ProviderMetadata, SharedV4ProviderOptions, SharedV4Warning } from '../_types/@ai-sdk_provider@4.0.38/dist/index.js';
import { MastraBase } from '../base.js';
import type { Mastra } from '../mastra/index.js';
export type ClassifierState = DecisionModelV4Input;
export type DecisionModelResult = DecisionModelV4Result;
/** @deprecated Use `DecisionModelResult` instead. */
export type EvaluationModelResult = DecisionModelResult;
/**
* A model accepted by `Classifier`: an AI SDK decision model (`doDecide`), or a
* legacy evaluation model (`doEvaluate`) kept for backwards compatibility.
*/
export type ClassifierModel = DecisionModelV4 | EvaluationModelV4;
export interface MastraDecisionModelInterface {
readonly specificationVersion: 'v4';
readonly provider: string;
readonly modelId: string;
readonly supportedQuestionTypes: DecisionModelV4['supportedQuestionTypes'];
doDecide(options: DecisionModelV4CallOptions): Promise<DecisionModelResult>;
/** @deprecated Use `doDecide` instead. */
doEvaluate(options: DecisionModelV4CallOptions): Promise<DecisionModelResult>;
}
/** @deprecated Use `MastraDecisionModelInterface` instead. */
export interface MastraEvaluationModelInterface extends Omit<MastraDecisionModelInterface, 'doDecide'> {
}
export declare class MastraDecisionModel extends MastraBase implements MastraDecisionModelInterface {
#private;
readonly specificationVersion: 'v4';
readonly provider: string;
readonly modelId: string;
readonly supportedQuestionTypes: DecisionModelV4['supportedQuestionTypes'];
constructor(model: ClassifierModel);
doDecide(options: DecisionModelV4CallOptions): Promise<DecisionModelResult>;
/** @deprecated Use `doDecide` instead. */
doEvaluate(options: DecisionModelV4CallOptions): Promise<DecisionModelResult>;
protected transformResult(result: DecisionModelResult): DecisionModelResult;
}
/** @deprecated Use `MastraDecisionModel` instead. `MastraEvaluationModel` will be removed in a future release. */
export declare class MastraEvaluationModel extends MastraDecisionModel {
}
export type ChoiceQuestion<CRITERIA extends Readonly<Record<string, DecisionModelV4Input | null>> = Readonly<Record<string, DecisionModelV4Input | null>>> = {
readonly type: 'choice';
readonly instructions?: DecisionModelV4Input;
readonly criteria: CRITERIA;
};
export type ScoreQuestion<CRITERIA extends readonly (DecisionModelV4Input | null)[] = readonly (DecisionModelV4Input | null)[]> = {
readonly type: 'score';
readonly instructions?: DecisionModelV4Input;
readonly criteria: CRITERIA;
};
export type BooleanQuestion = {
readonly type: 'boolean';
readonly instructions?: DecisionModelV4Input;
readonly criteria?: {
readonly true?: DecisionModelV4Input | null;
readonly false?: DecisionModelV4Input | null;
};
};
export type ClassifierQuestion = ChoiceQuestion | ScoreQuestion | BooleanQuestion;
export type ClassifierQuestions = Readonly<Record<string, ClassifierQuestion>>;
export type ChoiceAnswer<OPTION extends string = string> = {
type: 'choice';
choice: OPTION;
probabilities?: Record<OPTION, number>;
};
export type ScoreAnswer = {
type: 'score';
score: number;
probabilities?: Record<string, number>;
};
export type BooleanAnswer = {
type: 'boolean';
probability: number;
};
/**
* The decision model declined to answer the question. Valid for any question type.
*
* The answer fields are typed as absent so equality checks such as
* `answers.route.choice === 'billing'` keep compiling and evaluate to `false` on a refusal.
*/
export type RefusalAnswer = {
type: 'refusal';
choice?: never;
score?: never;
probability?: never;
probabilities?: never;
};
export type ClassifierAnswer<QUESTION extends ClassifierQuestion> = QUESTION extends {
type: 'choice';
criteria: infer CRITERIA extends Readonly<Record<string, DecisionModelV4Input | null>>;
} ? ChoiceAnswer<Extract<keyof CRITERIA, string>> | RefusalAnswer : QUESTION extends {
type: 'score';
} ? ScoreAnswer | RefusalAnswer : QUESTION extends {
type: 'boolean';
} ? BooleanAnswer | RefusalAnswer : never;
export type ClassifierAnswers<QUESTIONS extends ClassifierQuestions> = {
-readonly [KEY in keyof QUESTIONS]: ClassifierAnswer<QUESTIONS[KEY]>;
};
export type ClassifierUsage = {
inputTokens?: number;
outputTokens?: number;
totalTokens: number;
};
export type ClassifierResult<QUESTIONS extends ClassifierQuestions> = {
answers: ClassifierAnswers<QUESTIONS>;
usage: ClassifierUsage;
warnings: SharedV4Warning[];
rounding?: {
probabilityDecimals?: number;
scoreDecimals?: number;
};
providerMetadata?: SharedV4ProviderMetadata;
response: {
id?: string;
timestamp: Date;
modelId: string;
headers?: Record<string, string | undefined>;
body?: unknown;
};
};
/** Ids of the run a classifier decision belongs to. Each one is set only when the caller knows it. */
export type ClassifierDecisionIds = {
runId?: string;
threadId?: string;
resourceId?: string;
messageId?: string;
traceId?: string;
spanId?: string;
};
/**
* A classifier call and its result, as passed to audit callbacks such as `ClassifierProcessor`'s `onDecision`.
* `result` is set when the classifier answered and `error` when the call failed.
*/
export type ClassifierDecision<QUESTIONS extends ClassifierQuestions> = {
classifierId: string;
/** The state sent to the classifier. */
state: ClassifierState;
/** Time spent in the classifier call, including retries. */
durationMs: number;
ids: ClassifierDecisionIds;
} & (ClassifierDecisionSuccess<QUESTIONS> | ClassifierDecisionError);
/** A successful classifier decision, containing the result and no error. */
type ClassifierDecisionSuccess<QUESTIONS extends ClassifierQuestions> = {
result: ClassifierResult<QUESTIONS>;
error?: never;
};
/** A failed classifier decision, containing the error and no result. */
type ClassifierDecisionError = {
result?: never;
error: unknown;
};
type CommonDecideOptions = {
state: ClassifierState;
abortSignal?: AbortSignal;
providerOptions?: SharedV4ProviderOptions;
maxRetries?: number;
};
export type ConfiguredClassifierDecideOptions = CommonDecideOptions & {
questions?: never;
};
export type PerCallClassifierDecideOptions<QUESTIONS extends ClassifierQuestions> = CommonDecideOptions & {
questions: QUESTIONS;
};
/** @deprecated Use `ConfiguredClassifierDecideOptions` instead. */
export type ConfiguredClassifierEvaluateOptions = ConfiguredClassifierDecideOptions;
/** @deprecated Use `PerCallClassifierDecideOptions` instead. */
export type PerCallClassifierEvaluateOptions<QUESTIONS extends ClassifierQuestions> = PerCallClassifierDecideOptions<QUESTIONS>;
export type ConfiguredClassifierOptions<QUESTIONS extends ClassifierQuestions> = {
id: string;
model: ClassifierModel | MastraDecisionModel;
questions: QUESTIONS;
};
export type PerCallClassifierOptions = {
id: string;
model: ClassifierModel | MastraDecisionModel;
questions?: never;
};
export interface ClassifierInterface<CONFIGURED_QUESTIONS extends ClassifierQuestions | undefined = undefined> {
readonly id: string;
readonly model: MastraDecisionModelInterface;
readonly questions: CONFIGURED_QUESTIONS;
decide(options: CONFIGURED_QUESTIONS extends ClassifierQuestions ? ConfiguredClassifierDecideOptions : never): Promise<CONFIGURED_QUESTIONS extends ClassifierQuestions ? ClassifierResult<CONFIGURED_QUESTIONS> : never>;
decide<const QUESTIONS extends ClassifierQuestions>(options: CONFIGURED_QUESTIONS extends undefined ? PerCallClassifierDecideOptions<QUESTIONS> : never): Promise<ClassifierResult<QUESTIONS>>;
/** @deprecated Use `decide` instead. */
evaluate(options: CONFIGURED_QUESTIONS extends ClassifierQuestions ? ConfiguredClassifierDecideOptions : never): Promise<CONFIGURED_QUESTIONS extends ClassifierQuestions ? ClassifierResult<CONFIGURED_QUESTIONS> : never>;
/** @deprecated Use `decide` instead. */
evaluate<const QUESTIONS extends ClassifierQuestions>(options: CONFIGURED_QUESTIONS extends undefined ? PerCallClassifierDecideOptions<QUESTIONS> : never): Promise<ClassifierResult<QUESTIONS>>;
}
export declare class Classifier<CONFIGURED_QUESTIONS extends ClassifierQuestions | undefined = undefined> extends MastraBase implements ClassifierInterface<CONFIGURED_QUESTIONS> {
#private;
readonly id: string;
readonly model: MastraDecisionModel;
readonly questions: CONFIGURED_QUESTIONS;
constructor(options: CONFIGURED_QUESTIONS extends ClassifierQuestions ? ConfiguredClassifierOptions<CONFIGURED_QUESTIONS> : PerCallClassifierOptions);
/**
* Internal: called by `Mastra.addClassifier()` so evaluations can start root spans
* through the registered observability instance when no span is active.
*/
__registerMastra(mastra: Mastra): void;
/** @deprecated Use `decide` instead. `evaluate` will be removed in a future release. */
evaluate(options: CONFIGURED_QUESTIONS extends ClassifierQuestions ? ConfiguredClassifierDecideOptions : never): Promise<CONFIGURED_QUESTIONS extends ClassifierQuestions ? ClassifierResult<CONFIGURED_QUESTIONS> : never>;
/** @deprecated Use `decide` instead. `evaluate` will be removed in a future release. */
evaluate<const QUESTIONS extends ClassifierQuestions>(options: CONFIGURED_QUESTIONS extends undefined ? PerCallClassifierDecideOptions<QUESTIONS> : never): Promise<ClassifierResult<QUESTIONS>>;
decide(options: CONFIGURED_QUESTIONS extends ClassifierQuestions ? ConfiguredClassifierDecideOptions : never): Promise<CONFIGURED_QUESTIONS extends ClassifierQuestions ? ClassifierResult<CONFIGURED_QUESTIONS> : never>;
decide<const QUESTIONS extends ClassifierQuestions>(options: CONFIGURED_QUESTIONS extends undefined ? PerCallClassifierDecideOptions<QUESTIONS> : never): Promise<ClassifierResult<QUESTIONS>>;
}
export {};
//# sourceMappingURL=index.d.ts.map