|
1
|
// Typed wrappers around the Siti on-device chat backend (Rust/Tauri commands |
|
2
|
// and events). Keeping the IPC surface in one place means components never |
|
3
|
// touch raw command-name strings. |
|
4
|
|
|
5
|
import { invoke } from "@tauri-apps/api/core"; |
|
6
|
import { listen, type UnlistenFn } from "@tauri-apps/api/event"; |
|
7
|
import { getVersion } from "@tauri-apps/api/app"; |
|
8
|
|
|
9
|
export type ChatStatus = |
|
10
|
| "unloaded" |
|
11
|
| "loading" |
|
12
|
| "ready" |
|
13
|
| "generating" |
|
14
|
| "error"; |
|
15
|
|
|
16
|
export interface ModelInfo { |
|
17
|
id: string; |
|
18
|
name: string; |
|
19
|
org: string; |
|
20
|
description: string; |
|
21
|
approx_memory: string; |
|
22
|
size_bytes: number | null; |
|
23
|
is_downloaded: boolean; |
|
24
|
is_selected: boolean; |
|
25
|
} |
|
26
|
|
|
27
|
export interface ChatStatusResponse { |
|
28
|
status: ChatStatus; |
|
29
|
model_name: string | null; |
|
30
|
approx_memory: string | null; |
|
31
|
history_length: number; |
|
32
|
} |
|
33
|
|
|
34
|
export interface ChatStatusPayload { |
|
35
|
status: ChatStatus; |
|
36
|
model_name: string | null; |
|
37
|
error: string | null; |
|
38
|
} |
|
39
|
|
|
40
|
export interface ChatReplyPayload { |
|
41
|
reply: string | null; |
|
42
|
duration: string | null; |
|
43
|
error: string | null; |
|
44
|
} |
|
45
|
|
|
46
|
export interface ChatMessagePayload { |
|
47
|
role: "user" | "assistant"; |
|
48
|
content: string; |
|
49
|
} |
|
50
|
|
|
51
|
// ── Commands ──────────────────────────────────────────────────────────────── |
|
52
|
|
|
53
|
export const listModels = () => invoke<ModelInfo[]>("chat_list_models"); |
|
54
|
|
|
55
|
export const setModel = (modelId: string) => |
|
56
|
invoke<void>("chat_set_model", { modelId }); |
|
57
|
|
|
58
|
export const removeModel = (modelId: string) => |
|
59
|
invoke<string>("chat_remove_model", { modelId }); |
|
60
|
|
|
61
|
export const sendMessage = (message: string) => |
|
62
|
invoke<void>("chat_send_message", { message }); |
|
63
|
|
|
64
|
export const getStatus = () => invoke<ChatStatusResponse>("chat_get_status"); |
|
65
|
|
|
66
|
export const getHistory = () => |
|
67
|
invoke<ChatMessagePayload[]>("chat_get_history"); |
|
68
|
|
|
69
|
export const clearHistory = () => invoke<void>("chat_clear_history"); |
|
70
|
|
|
71
|
// ── App metadata ────────────────────────────────────────────────────────── |
|
72
|
|
|
73
|
/** Marketing version (CFBundleShortVersionString) from tauri.conf.json. */ |
|
74
|
export const getAppVersion = () => getVersion(); |
|
75
|
|
|
76
|
/** Native build number (Apple CFBundleVersion); `null` when unavailable. */ |
|
77
|
export const getBuildVersion = () => |
|
78
|
invoke<string | null>("app_build_version"); |
|
79
|
|
|
80
|
// ── Events ────────────────────────────────────────────────────────────────── |
|
81
|
|
|
82
|
export const onStatusChanged = (cb: (p: ChatStatusPayload) => void): Promise<UnlistenFn> => |
|
83
|
listen<ChatStatusPayload>("chat_status_changed", (e) => cb(e.payload)); |
|
84
|
|
|
85
|
export const onReply = (cb: (p: ChatReplyPayload) => void): Promise<UnlistenFn> => |
|
86
|
listen<ChatReplyPayload>("chat_reply", (e) => cb(e.payload)); |
|
87
|
|
|
88
|
// ── Helpers ─────────────────────────────────────────────────────────────── |
|
89
|
|
|
90
|
/** Format a byte count as a compact "1.9 GB" / "941 MB" string. */ |
|
91
|
export function formatSize(bytes: number | null): string { |
|
92
|
if (!bytes) return ""; |
|
93
|
const gb = bytes / 1e9; |
|
94
|
if (gb >= 1) return `${gb.toFixed(gb >= 10 ? 0 : 1)} GB`; |
|
95
|
return `${Math.round(bytes / 1e6)} MB`; |
|
96
|
} |