98 lines
2.2 KiB
TypeScript
98 lines
2.2 KiB
TypeScript
|
|
import { apiFetch, type PaginatedResponse } from './api';
|
||
|
|
|
||
|
|
export interface ActionItem {
|
||
|
|
type: string;
|
||
|
|
params: Record<string, unknown>;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ChatResponse {
|
||
|
|
response: string;
|
||
|
|
actions: ActionItem[];
|
||
|
|
session_id: string;
|
||
|
|
message_id: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ActionResponse {
|
||
|
|
action: string;
|
||
|
|
result: unknown;
|
||
|
|
success: boolean;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ChatMessage {
|
||
|
|
id: string;
|
||
|
|
session_id: string;
|
||
|
|
role: 'user' | 'assistant';
|
||
|
|
content: string;
|
||
|
|
actions?: ActionItem[] | null;
|
||
|
|
created_at?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface VoiceResponse {
|
||
|
|
transcription: string;
|
||
|
|
response: string;
|
||
|
|
actions: ActionItem[];
|
||
|
|
session_id: string;
|
||
|
|
message_id: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function sendChatMessage(
|
||
|
|
message: string,
|
||
|
|
sessionId?: string
|
||
|
|
): Promise<ChatResponse> {
|
||
|
|
return apiFetch<ChatResponse>('/copilot/chat', {
|
||
|
|
method: 'POST',
|
||
|
|
body: JSON.stringify({ message, session_id: sessionId || undefined }),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function executeAction(
|
||
|
|
action: string,
|
||
|
|
params: Record<string, unknown>,
|
||
|
|
sessionId?: string
|
||
|
|
): Promise<ActionResponse> {
|
||
|
|
return apiFetch<ActionResponse>('/copilot/action', {
|
||
|
|
method: 'POST',
|
||
|
|
body: JSON.stringify({ action, params, session_id: sessionId || undefined }),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getChatHistory(
|
||
|
|
page = 1,
|
||
|
|
pageSize = 20,
|
||
|
|
sessionId?: string
|
||
|
|
): Promise<PaginatedResponse<ChatMessage>> {
|
||
|
|
let path = `/copilot/history?page=${page}&page_size=${pageSize}`;
|
||
|
|
if (sessionId) {
|
||
|
|
path += `&session_id=${sessionId}`;
|
||
|
|
}
|
||
|
|
return apiFetch<PaginatedResponse<ChatMessage>>(path);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function sendVoiceMessage(
|
||
|
|
audioBase64: string,
|
||
|
|
mimeType = 'audio/webm',
|
||
|
|
sessionId?: string
|
||
|
|
): Promise<VoiceResponse> {
|
||
|
|
return apiFetch<VoiceResponse>('/copilot/voice', {
|
||
|
|
method: 'POST',
|
||
|
|
body: JSON.stringify({
|
||
|
|
audio: audioBase64,
|
||
|
|
mime_type: mimeType,
|
||
|
|
session_id: sessionId || undefined,
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function fileToBase64(file: Blob): Promise<string> {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const reader = new FileReader();
|
||
|
|
reader.onload = () => {
|
||
|
|
const result = reader.result as string;
|
||
|
|
const base64 = result.split(',')[1] || result;
|
||
|
|
resolve(base64);
|
||
|
|
};
|
||
|
|
reader.onerror = reject;
|
||
|
|
reader.readAsDataURL(file);
|
||
|
|
});
|
||
|
|
}
|