@mastra/core
Version:
The core foundation of the Mastra framework, providing essential components and interfaces for building AI-powered applications.
1 lines • 16.9 kB
Source Map (JSON)
{"version":3,"sources":["../../src/scores/types.ts","../../src/scores/base.ts","../../src/scores/code.ts","../../src/scores/llm.ts"],"names":["z","randomUUID","createStep","createWorkflow","Agent"],"mappings":";;;;;;;AAsCO,IAAM,8BAAA,GAAiCA,KAAA,CAAE,MAAA,CAAOA,KAAA,CAAE,MAAA,IAAUA,KAAA,CAAE,GAAA,EAAK,CAAA,CAAE,QAAA;AAIrE,IAAM,kBAAA,GAAqBA,MAAE,MAAA;AAE7B,IAAM,iBAAA,GAAoBA,MAAE,MAAA,CAAO;AAAA,EACxC,MAAA,EAAQA,KAAA,CAAE,MAAA,CAAOA,KAAA,CAAE,MAAA,IAAUA,KAAA,CAAE,GAAA,EAAK,CAAA,CAAE,QAAA,EAAS;AAAA,EAC/C,KAAA,EAAO,kBAAA;AAAA,EACP,MAAA,EAAQA,KAAA,CAAE,MAAA,EAAO,CAAE,QAAA;AACrB,CAAC;AClCM,IAAM,eAAN,MAAmB;AAAA,EACxB,IAAA;AAAA,EACA,WAAA;AAAA,EACA,OAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,WAAA;AAAA,EAEA,YAAY,IAAA,EAAqB;AAC/B,IAAA,IAAA,CAAK,OAAO,IAAA,CAAK,IAAA;AACjB,IAAA,IAAA,CAAK,cAAc,IAAA,CAAK,WAAA;AACxB,IAAA,IAAA,CAAK,UAAU,IAAA,CAAK,OAAA;AACpB,IAAA,IAAA,CAAK,UAAU,IAAA,CAAK,OAAA;AACpB,IAAA,IAAA,CAAK,SAAS,IAAA,CAAK,MAAA;AACnB,IAAA,IAAA,CAAK,WAAW,EAAC;AACjB,IAAA,IAAA,CAAK,cAAc,IAAA,CAAK,WAAA;AAExB,IAAA,IAAI,KAAK,QAAA,EAAU;AACjB,MAAA,IAAA,CAAK,WAAW,IAAA,CAAK,QAAA;AAA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AAAA,QACxB,GAAG,GAAA;AAAA,QACH,mBAAmB,GAAA,CAAI,iBAAA;AAAA,QACvB,mBAAmB,GAAA,CAAI,iBAAA;AAAA;AAAA,QACvB,KAAA,EAAO,IAAI,KAAA,IAAS;AAAA,OACtB;AAEA,MAAA,MAAM,SAAS,IAAA,CAAK,MAAA,EAAQ,aAAa,EAAE,GAAA,EAAK,mBAAmB,CAAA;AAEnE,MAAA,MAAM,YAAA,GAAe,MAAM,GAAA,CAAI,QAAA,CAAS,MAAA,EAAQ;AAAA,QAC9C,MAAA,EAAQJ,MAAE,MAAA,CAAO;AAAA,UACf,MAAA,EAAQA,MAAE,MAAA;AAAO,SAClB;AAAA,OACF,CAAA;AAED,MAAA,OAAO;AAAA,QACL,MAAA,EAAQ,aAAa,MAAA,CAAO,MAAA;AAAA,QAC5B,YAAA,EAAc;AAAA,OAChB;AAAA,IACF,CAAA,GACA;AAAA,GACL,CAAA;AAED,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["import { z } from 'zod';\n\nexport type ScoringSamplingConfig = { type: 'none' } | { type: 'ratio'; rate: number };\n\nexport type ScoringSource = 'LIVE' | 'TEST';\n\nexport type ScoringEntityType = 'AGENT' | 'WORKFLOW';\n\nexport type ScoringPrompts = {\n description: string;\n prompt: string;\n};\n\nexport type ScoringInput = {\n runId?: string;\n input?: Record<string, any>[];\n output: Record<string, any>;\n additionalContext?: Record<string, any>;\n runtimeContext?: Record<string, any>;\n};\n\nexport type ScoringHookInput = {\n runId?: string;\n scorer: Record<string, any>;\n input: Record<string, any>[];\n output: Record<string, any>;\n metadata?: Record<string, any>;\n additionalContext?: Record<string, any>;\n source: ScoringSource;\n entity: Record<string, any>;\n entityType: ScoringEntityType;\n runtimeContext?: Record<string, any>;\n structuredOutput?: boolean;\n traceId?: string;\n resourceId?: string;\n threadId?: string;\n};\n\nexport const scoringExtractStepResultSchema = z.record(z.string(), z.any()).optional();\n\nexport type ScoringExtractStepResult = z.infer<typeof scoringExtractStepResultSchema>;\n\nexport const scoringValueSchema = z.number();\n\nexport const scoreResultSchema = z.object({\n result: z.record(z.string(), z.any()).optional(),\n score: scoringValueSchema,\n prompt: z.string().optional(),\n});\n\nexport type ScoringAnalyzeStepResult = z.infer<typeof scoreResultSchema>;\n\nexport type ScoringInputWithExtractStepResult<TExtract = any> = ScoringInput & {\n runId: string;\n extractStepResult?: TExtract;\n extractPrompt?: string;\n};\n\nexport type ScoringInputWithExtractStepResultAndAnalyzeStepResult<\n TExtract = any,\n TScore = any,\n> = ScoringInputWithExtractStepResult<TExtract> & {\n score: number;\n analyzeStepResult?: TScore;\n analyzePrompt?: string;\n};\n\nexport type ScoringInputWithExtractStepResultAndScoreAndReason =\n ScoringInputWithExtractStepResultAndAnalyzeStepResult & {\n reason?: string;\n reasonPrompt?: string;\n };\n\nexport type ScoreRowData = ScoringInputWithExtractStepResultAndScoreAndReason &\n ScoringHookInput & {\n id: string;\n entityId: string;\n scorerId: string;\n createdAt: Date;\n updatedAt: Date;\n };\n\nexport type ExtractionStepFn = (input: ScoringInput) => Promise<Record<string, any>>;\n\nexport type AnalyzeStepFn = (input: ScoringInputWithExtractStepResult) => Promise<ScoringAnalyzeStepResult>;\n\nexport type ReasonStepFn = (\n input: ScoringInputWithExtractStepResultAndAnalyzeStepResult,\n) => Promise<{ reason: string; reasonPrompt?: string } | null>;\n\nexport type ScorerOptions = {\n name: string;\n description: string;\n extract?: ExtractionStepFn;\n analyze: AnalyzeStepFn;\n reason?: ReasonStepFn;\n metadata?: Record<string, any>;\n isLLMScorer?: boolean;\n};\n","import { randomUUID } from 'crypto';\nimport { z } from 'zod';\nimport { createStep, createWorkflow } from '../workflows';\nimport { scoreResultSchema, scoringExtractStepResultSchema } from './types';\nimport type {\n ExtractionStepFn,\n ReasonStepFn,\n ScorerOptions,\n AnalyzeStepFn,\n ScoringInput,\n ScoringInputWithExtractStepResultAndScoreAndReason,\n ScoringSamplingConfig,\n} from './types';\n\nexport class MastraScorer {\n name: string;\n description: string;\n extract?: ExtractionStepFn;\n analyze: AnalyzeStepFn;\n reason?: ReasonStepFn;\n metadata?: Record<string, any>;\n isLLMScorer?: boolean;\n\n constructor(opts: ScorerOptions) {\n this.name = opts.name;\n this.description = opts.description;\n this.extract = opts.extract;\n this.analyze = opts.analyze;\n this.reason = opts.reason;\n this.metadata = {};\n this.isLLMScorer = opts.isLLMScorer;\n\n if (opts.metadata) {\n this.metadata = opts.metadata;\n }\n }\n\n async run(input: ScoringInput): Promise<ScoringInputWithExtractStepResultAndScoreAndReason> {\n let runId = input.runId;\n if (!runId) {\n runId = randomUUID();\n }\n\n const extractStep = createStep({\n id: 'extract',\n description: 'Extract relevant element from the run',\n inputSchema: z.any(),\n outputSchema: scoringExtractStepResultSchema,\n execute: async ({ inputData }) => {\n if (!this.extract) {\n return;\n }\n\n const extractStepResult = await this.extract(inputData);\n\n return extractStepResult;\n },\n });\n\n const analyzeStep = createStep({\n id: 'analyze',\n description: 'Score the extracted element',\n inputSchema: scoringExtractStepResultSchema,\n outputSchema: scoreResultSchema,\n execute: async ({ inputData }) => {\n const analyzeStepResult = await this.analyze({ ...input, runId, extractStepResult: inputData?.result });\n\n return analyzeStepResult;\n },\n });\n\n const reasonStep = createStep({\n id: 'reason',\n description: 'Reason about the score',\n inputSchema: scoreResultSchema,\n outputSchema: z.any(),\n execute: async ({ getStepResult }) => {\n const analyzeStepRes = getStepResult(analyzeStep);\n const extractStepResult = getStepResult(extractStep);\n\n if (!this.reason) {\n return {\n extractStepResult: extractStepResult?.result,\n analyzeStepResult: analyzeStepRes?.result,\n analyzePrompt: analyzeStepRes?.prompt,\n extractPrompt: extractStepResult?.prompt,\n score: analyzeStepRes?.score,\n };\n }\n\n const reasonResult = await this.reason({\n ...input,\n analyzeStepResult: analyzeStepRes.result,\n score: analyzeStepRes.score,\n runId,\n });\n\n return {\n extractStepResult: extractStepResult?.result,\n analyzeStepResult: analyzeStepRes?.result,\n analyzePrompt: analyzeStepRes?.prompt,\n extractPrompt: extractStepResult?.prompt,\n score: analyzeStepRes?.score,\n ...reasonResult,\n };\n },\n });\n\n const scoringPipeline = createWorkflow({\n id: `scoring-pipeline-${this.name}`,\n inputSchema: z.any(),\n outputSchema: z.any(),\n steps: [extractStep, analyzeStep],\n })\n .then(extractStep)\n .then(analyzeStep)\n .then(reasonStep)\n .commit();\n\n const workflowRun = await scoringPipeline.createRunAsync();\n\n const execution = await workflowRun.start({\n inputData: input,\n });\n\n if (execution.status !== 'success') {\n throw new Error(\n `Scoring pipeline failed: ${execution.status}`,\n execution.status === 'failed' ? execution.error : undefined,\n );\n }\n\n return { runId, ...execution.result };\n }\n}\nexport type MastraScorerEntry = {\n scorer: MastraScorer;\n sampling?: ScoringSamplingConfig;\n};\n\nexport type MastraScorers = Record<string, MastraScorerEntry>;\n","import { MastraScorer } from './base';\nimport type { ScorerOptions } from './types';\n\nexport function createScorer(opts: ScorerOptions) {\n const scorer = new MastraScorer({\n name: opts.name,\n description: opts.description,\n extract: opts.extract,\n analyze: opts.analyze,\n reason: opts.reason,\n });\n\n return scorer;\n}\n","import { z } from 'zod';\nimport { Agent } from '../agent';\nimport type { MastraLanguageModel } from '../agent/types';\nimport { MastraScorer } from './base';\nimport type {\n ScoringInput,\n ScoringInputWithExtractStepResult,\n ScoringInputWithExtractStepResultAndAnalyzeStepResult,\n} from './types';\n\ntype LLMJudge = {\n model: MastraLanguageModel;\n instructions: string;\n};\n\nexport type LLMScorerOptions<TExtractOutput extends Record<string, any> = any, TScoreOutput = any> = {\n name: string;\n description: string;\n judge: LLMJudge;\n extract?: {\n description: string;\n judge?: LLMJudge;\n outputSchema: z.ZodType<TExtractOutput>;\n createPrompt: ({ run }: { run: ScoringInput }) => string;\n };\n analyze: {\n description: string;\n judge?: LLMJudge;\n outputSchema: z.ZodType<TScoreOutput>;\n createPrompt: ({ run }: { run: ScoringInput & { extractStepResult: TExtractOutput } }) => string;\n };\n reason?: {\n description: string;\n judge?: LLMJudge;\n createPrompt: ({\n run,\n }: {\n run: ScoringInputWithExtractStepResult & { analyzeStepResult: TScoreOutput; score: number };\n }) => string;\n };\n calculateScore: ({ run }: { run: ScoringInputWithExtractStepResult & { analyzeStepResult: TScoreOutput } }) => number;\n};\n\nexport function createLLMScorer<TExtractOutput extends Record<string, any> = any, TScoreOutput = any>(\n opts: LLMScorerOptions<TExtractOutput, TScoreOutput>,\n) {\n const model = opts.judge.model;\n\n const llm = new Agent({\n name: opts.name,\n instructions: opts.judge.instructions,\n model: model,\n });\n\n const scorer = new MastraScorer({\n name: opts.name,\n description: opts.description,\n metadata: opts,\n isLLMScorer: true,\n ...(opts.extract && {\n extract: async run => {\n const prompt = opts.extract!.createPrompt({ run });\n const extractResult = await llm.generate(prompt, {\n output: opts.extract!.outputSchema,\n });\n\n return {\n result: extractResult.object as Record<string, any>,\n prompt,\n };\n },\n }),\n analyze: async run => {\n const runWithExtractResult = {\n ...run,\n extractStepResult: run.extractStepResult,\n };\n\n const prompt = opts.analyze.createPrompt({ run: runWithExtractResult });\n\n const analyzeResult = await llm.generate(prompt, {\n output: opts.analyze.outputSchema,\n });\n\n let score = 0;\n\n const runWithScoreResult = {\n ...runWithExtractResult,\n analyzeStepResult: analyzeResult.object,\n };\n\n if (opts.calculateScore) {\n score = opts.calculateScore({ run: runWithScoreResult });\n }\n\n (runWithScoreResult as ScoringInputWithExtractStepResultAndAnalyzeStepResult).score = score;\n\n return {\n result: analyzeResult.object!,\n score: score,\n prompt,\n };\n },\n reason: opts.reason\n ? async run => {\n // Prepare run with both extract and score results\n const runWithAllResults = {\n ...run,\n extractStepResult: run.extractStepResult,\n analyzeStepResult: run.analyzeStepResult, // Use results as fallback\n score: run.score || 0,\n };\n\n const prompt = opts.reason?.createPrompt({ run: runWithAllResults })!;\n\n const reasonResult = await llm.generate(prompt, {\n output: z.object({\n reason: z.string(),\n }),\n });\n\n return {\n reason: reasonResult.object.reason,\n reasonPrompt: prompt,\n };\n }\n : undefined,\n });\n\n return scorer;\n}\n"]}