import { Injectable } from '@nestjs/common';
import type {
AiTool,
AiToolContext,
AiToolDefinition,
} from '../domain/ai-tool';
import { CostingReadModelService } from '../../costing/read-models/costing-read-model.service';
export interface RecipeCostInput {
recipeId: string;
includeHistory?: boolean;
}
@Injectable()
export class RecipeCostAiTool implements AiTool<RecipeCostInput, unknown> {
readonly definition: AiToolDefinition = {
name: 'costing.recipe',
category: 'costing',
description:
'Read the current recipe cost projection and optionally its cost history for a location.',
readOnly: true,
requiredPermissions: ['costing.read'],
};
constructor(
private readonly costingReadModel: CostingReadModelService,
) {}
async execute(
context: AiToolContext,
input: RecipeCostInput,
): Promise<unknown> {
if (!context.locationId) {
throw new Error('ai_tool_location_required:costing.recipe');
}
if (!input.recipeId) {
throw new Error('ai_tool_recipe_required:costing.recipe');
}
const current = await this.costingReadModel.recipeCost({
organizationId: context.organizationId,
locationId: context.locationId,
recipeId: input.recipeId,
});
const history = input.includeHistory
? await this.costingReadModel.recipeHistory({
organizationId: context.organizationId,
locationId: context.locationId,
recipeId: input.recipeId,
})
: undefined;
return {
current,
...(input.includeHistory ? { history } : {}),
};
}
}
