ifixkart-admin/services/api/simulation.ts

127 lines
3.7 KiB
TypeScript

import { apiFetch } from './client';
export interface ScenarioMeta {
title: string;
description: string;
default_params: Record<string, any>;
}
export interface ScenariosResponse {
available_scenarios: string[];
scenarios: Record<string, ScenarioMeta>;
max_concurrent_simulations: number;
job_timeout_seconds: number;
}
export interface SimulationJobSummary {
job_id: string;
scenario_type: string;
status: string;
store_id: number;
created_at: string;
execution_time_ms?: number;
total_cost_usd?: number;
}
export interface HistoricalBaseline {
historical_sample_size: number;
historical_mean: number;
historical_std: number;
empirical_95_ci_lower: number;
empirical_95_ci_upper: number;
baseline_source: string;
sampling_notice: string;
}
export interface SimulationDispersion {
agent_sample_count: number;
simulation_mean: number;
simulation_std: number;
agent_disagreement_index: number;
hypothesis_spread_label: string;
}
export interface CalibratedProjection {
point_estimate: number;
credible_range_lower: number;
credible_range_upper: number;
historical_weight: number;
simulation_weight: number;
epistemic_notice: string;
}
export interface AgentLog {
agent_id: string;
archetype: string;
device?: string;
price_sensitivity?: number;
simulated_purchase_prob?: number;
decision?: string;
reasoning?: string;
[key: string]: any;
}
export interface SimulationJobDetail {
id: string;
store_id: number;
user_id: number;
scenario_type: string;
status: 'QUEUED' | 'CLAIMED' | 'RUNNING' | 'COMPLETED' | 'FAILED' | 'FAILED_INTERRUPTED' | 'TIMED_OUT' | 'CANCELLED';
input_params: Record<string, any>;
historical_baseline?: HistoricalBaseline;
simulation_dispersion?: SimulationDispersion;
calibrated_projection?: CalibratedProjection;
summary_metrics?: Record<string, any>;
report_markdown?: string;
agent_logs?: AgentLog[];
error_message?: string;
execution_time_ms?: number;
prompt_tokens?: number;
completion_tokens?: number;
total_cost_usd?: number;
last_heartbeat_at?: string;
created_at: string;
started_at?: string;
completed_at?: string;
}
export interface PersonaInterviewResponse {
agent_id: string;
question: string;
response: string;
latency_ms: number;
}
export const simulationService = {
async getScenarios(): Promise<ScenariosResponse> {
return apiFetch<ScenariosResponse>('/api/v1/simulation/scenarios');
},
async runSimulation(scenario_type: string, input_params: Record<string, any>, store_id: number = 1): Promise<{ message: string; job_id: string; status: string; poll_url: string }> {
return apiFetch('/api/v1/simulation/scenarios/run', {
method: 'POST',
body: JSON.stringify({ scenario_type, input_params, store_id }),
});
},
async getJobDetail(job_id: string): Promise<SimulationJobDetail> {
return apiFetch<SimulationJobDetail>(`/api/v1/simulation/jobs/${job_id}`);
},
async listJobs(limit: number = 20): Promise<SimulationJobSummary[]> {
return apiFetch<SimulationJobSummary[]>(`/api/v1/simulation/jobs?limit=${limit}`);
},
async cancelJob(job_id: string): Promise<{ message: string; job_id: string; cancelled: boolean }> {
return apiFetch(`/api/v1/simulation/jobs/${job_id}/cancel`, {
method: 'POST',
});
},
async interviewPersona(job_id: string, agent_id: string, question: string): Promise<PersonaInterviewResponse> {
return apiFetch<PersonaInterviewResponse>(`/api/v1/simulation/jobs/${job_id}/interview`, {
method: 'POST',
body: JSON.stringify({ agent_id, question }),
});
},
};