| import type { AppContext } from "@api/ai/agents/config/shared"; |
| import { format, startOfYear, subMonths, subYears } from "date-fns"; |
|
|
| type PeriodOption = |
| | "3-months" |
| | "6-months" |
| | "this-year" |
| | "1-year" |
| | "2-years" |
| | "5-years"; |
|
|
| |
| const VALID_PERIOD_OPTIONS: ReadonlySet<string> = new Set([ |
| "3-months", |
| "6-months", |
| "this-year", |
| "1-year", |
| "2-years", |
| "5-years", |
| ]); |
|
|
| |
| function isValidPeriodOption(value: unknown): value is PeriodOption { |
| return typeof value === "string" && VALID_PERIOD_OPTIONS.has(value); |
| } |
|
|
| |
| function getPeriodDates(period: PeriodOption): { from: string; to: string } { |
| const now = new Date(); |
| const to = format(now, "yyyy-MM-dd"); |
|
|
| switch (period) { |
| case "3-months": |
| return { from: format(subMonths(now, 3), "yyyy-MM-dd"), to }; |
| case "6-months": |
| return { from: format(subMonths(now, 6), "yyyy-MM-dd"), to }; |
| case "this-year": |
| return { from: format(startOfYear(now), "yyyy-MM-dd"), to }; |
| case "1-year": |
| return { from: format(subYears(now, 1), "yyyy-MM-dd"), to }; |
| case "2-years": |
| return { from: format(subYears(now, 2), "yyyy-MM-dd"), to }; |
| case "5-years": |
| return { from: format(subYears(now, 5), "yyyy-MM-dd"), to }; |
| default: |
| return { from: format(subYears(now, 1), "yyyy-MM-dd"), to }; |
| } |
| } |
|
|
| export interface ResolvedToolParams { |
| from: string; |
| to: string; |
| currency?: string; |
| revenueType?: "gross" | "net"; |
| [key: string]: unknown; |
| } |
|
|
| export interface ResolveToolParamsOptions { |
| toolName: string; |
| appContext: AppContext; |
| aiParams: { |
| period?: string; |
| dateRange?: string; |
| from?: string; |
| to?: string; |
| currency?: string | null; |
| revenueType?: string; |
| [key: string]: unknown; |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function resolveToolParams( |
| options: ResolveToolParamsOptions, |
| ): ResolvedToolParams { |
| const { toolName, appContext, aiParams } = options; |
| const { forcedToolCall, metricsFilter, baseCurrency } = appContext; |
|
|
| |
| if (forcedToolCall?.toolName === toolName && forcedToolCall.toolParams) { |
| const forced = forcedToolCall.toolParams; |
| return { |
| ...forced, |
| from: |
| (forced.from as string) ?? |
| metricsFilter?.from ?? |
| getPeriodDates("1-year").from, |
| to: |
| (forced.to as string) ?? |
| metricsFilter?.to ?? |
| getPeriodDates("1-year").to, |
| currency: |
| (forced.currency as string) ?? metricsFilter?.currency ?? baseCurrency, |
| revenueType: |
| (forced.revenueType as "gross" | "net") ?? |
| metricsFilter?.revenueType ?? |
| "net", |
| }; |
| } |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| const historicalPeriod = isValidPeriodOption(aiParams.dateRange) |
| ? aiParams.dateRange |
| : isValidPeriodOption(aiParams.period) |
| ? aiParams.period |
| : undefined; |
|
|
| let from: string; |
| let to: string; |
|
|
| if (historicalPeriod) { |
| |
| const dates = getPeriodDates(historicalPeriod); |
| from = dates.from; |
| to = dates.to; |
| } else if (aiParams.from && aiParams.to) { |
| |
| from = aiParams.from; |
| to = aiParams.to; |
| } else if (metricsFilter?.from && metricsFilter?.to) { |
| |
| from = metricsFilter.from; |
| to = metricsFilter.to; |
| } else { |
| |
| const dates = getPeriodDates("1-year"); |
| from = dates.from; |
| to = dates.to; |
| } |
|
|
| |
| const currency = |
| (aiParams.currency !== null && aiParams.currency !== undefined |
| ? aiParams.currency |
| : undefined) ?? |
| metricsFilter?.currency ?? |
| baseCurrency; |
|
|
| |
| const revenueType = |
| (aiParams.revenueType as "gross" | "net") ?? |
| metricsFilter?.revenueType ?? |
| "net"; |
|
|
| |
| return { |
| ...aiParams, |
| from, |
| to, |
| currency, |
| revenueType, |
| }; |
| } |
|
|