import { invoke } from "@tauri-apps/api/core"; import type { Action, AgenticRunRequest, AgenticRunResult, AnalyzeReport, ApplyTxResult, BatchEvent, GenerateActionsResult, PreviewResult, ProjectProfile, Session, TrendsResult, UndoStatus, VerifyResult, } from "./types"; export interface UndoRedoState { undo_available: boolean; redo_available: boolean; } export interface RunBatchPayload { paths: string[]; confirm_apply: boolean; auto_check: boolean; selected_actions?: Action[]; user_confirmed?: boolean; attached_files?: string[]; } export interface ApplyActionsTxOptions { auto_check: boolean; user_confirmed: boolean; protocol_version_override?: number | null; fallback_attempted?: boolean; } export interface ProjectItem { id: string; path: string; } export interface AddProjectResult { id: string; } export interface UndoLastResult { ok: boolean; error_code?: string; error?: string; } export async function getUndoRedoState(): Promise { return invoke("get_undo_redo_state_cmd"); } export async function getUndoStatus(): Promise { return invoke("undo_status").catch(() => ({ available: false } as UndoStatus)); } export async function getFolderLinks(): Promise<{ paths: string[] }> { return invoke<{ paths: string[] }>("get_folder_links"); } export async function setFolderLinks(paths: string[]): Promise { return invoke("set_folder_links", { links: { paths } }); } export async function getProjectProfile(path: string): Promise { return invoke("get_project_profile", { path }); } export async function runBatchCmd(payload: RunBatchPayload): Promise { return invoke("run_batch_cmd", { payload }); } /** Предпросмотр diff для actions (CREATE/UPDATE/DELETE) без записи на диск. */ export async function previewActions(rootPath: string, actions: Action[]): Promise { return invoke("preview_actions_cmd", { payload: { root_path: rootPath, actions, auto_check: null, label: null, user_confirmed: false, }, }); } export async function applyActionsTx( path: string, actions: Action[], options: ApplyActionsTxOptions | boolean ): Promise { const opts: ApplyActionsTxOptions = typeof options === "boolean" ? { auto_check: options, user_confirmed: true } : options; return invoke("apply_actions_tx", { path, actions, options: opts, }); } export async function generateActionsFromReport( path: string, report: AnalyzeReport, mode: string ): Promise { return invoke("generate_actions_from_report", { path, report, mode, }); } export async function agenticRun(payload: AgenticRunRequest): Promise { return invoke("agentic_run", { payload }); } export async function listProjects(): Promise { return invoke("list_projects"); } export async function addProject(path: string, name: string | null): Promise { return invoke("add_project", { path, name }); } export async function listSessions(projectId?: string): Promise { return invoke("list_sessions", { projectId: projectId ?? null }); } export async function appendSessionEvent( projectId: string, kind: string, role: string, text: string ): Promise { return invoke("append_session_event", { project_id: projectId, kind, role, text, }); } export interface AgentPlanResult { ok: boolean; summary: string; actions: Action[]; error?: string; error_code?: string; plan_json?: string; plan_context?: string; protocol_version_used?: number | null; online_fallback_suggested?: string | null; online_context_used?: boolean | null; } export async function proposeActions( path: string, reportJson: string, userGoal: string, designStyle?: string | null, trendsContext?: string | null, lastPlanJson?: string | null, lastContext?: string | null, applyErrorCode?: string | null, applyErrorValidatedJson?: string | null, applyRepairAttempt?: number | null, applyErrorStage?: string | null, onlineFallbackAttempted?: boolean | null, onlineContextMd?: string | null, onlineContextSources?: string[] | null, onlineFallbackExecuted?: boolean | null, onlineFallbackReason?: string | null ): Promise { return invoke("propose_actions", { path, reportJson, userGoal, designStyle: designStyle ?? null, trendsContext: trendsContext ?? null, lastPlanJson: lastPlanJson ?? null, lastContext: lastContext ?? null, applyErrorCode: applyErrorCode ?? null, applyErrorValidatedJson: applyErrorValidatedJson ?? null, applyRepairAttempt: applyRepairAttempt ?? null, applyErrorStage: applyErrorStage ?? null, onlineFallbackAttempted: onlineFallbackAttempted ?? null, onlineContextMd: onlineContextMd ?? null, onlineContextSources: onlineContextSources ?? null, onlineFallbackExecuted: onlineFallbackExecuted ?? null, onlineFallbackReason: onlineFallbackReason ?? null, }); } export async function undoLastTx(path: string): Promise { return invoke("undo_last_tx", { path }); } export async function undoLast(): Promise { return invoke("undo_last"); } export async function redoLast(): Promise { return invoke("redo_last"); } /** Проверка целостности проекта (типы, сборка, тесты). Вызывается автоматически после применений или вручную. */ export async function verifyProject(path: string): Promise { return invoke("verify_project", { path }); } /** Тренды и рекомендации: последнее обновление и список. should_update === true если прошло >= 30 дней. */ export async function getTrendsRecommendations(): Promise { return invoke("get_trends_recommendations"); } /** Обновить тренды и рекомендации (запрос к внешним ресурсам по allowlist). */ export async function fetchTrendsRecommendations(): Promise { return invoke("fetch_trends_recommendations"); } // Settings export/import export interface ImportResult { projects_imported: number; profiles_imported: number; sessions_imported: number; folder_links_imported: number; } /** Export all settings as JSON string */ export async function exportSettings(): Promise { return invoke("export_settings"); } /** Import settings from JSON string */ export async function importSettings(json: string, mode?: "replace" | "merge"): Promise { return invoke("import_settings", { json, mode: mode ?? "merge" }); } /** Еженедельный отчёт: агрегация трасс и генерация через LLM */ export async function analyzeWeeklyReports( projectPath: string, from?: string | null, to?: string | null ): Promise { return invoke("analyze_weekly_reports_cmd", { projectPath, from: from ?? null, to: to ?? null, }); } /** Сохранить отчёт в docs/reports/weekly_YYYY-MM-DD.md */ export async function saveReport(projectPath: string, reportMd: string, date?: string | null): Promise { return invoke("save_report_cmd", { projectPath, reportMd, date: date ?? null }); } /** Online research: поиск Tavily + fetch + LLM summarize. Требует PAPAYU_ONLINE_RESEARCH=1, PAPAYU_TAVILY_API_KEY. */ export async function researchAnswer(query: string): Promise { return invoke("research_answer_cmd", { query }); }