MCPcopy Create free account
hub / github.com/PromtEngineer/localGPT / ChatAPI

Class ChatAPI

src/lib/api.ts:84–631  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82}
83
84class ChatAPI {
85 async checkHealth(): Promise<HealthResponse> {
86 try {
87 const response = await fetch(`${API_BASE_URL}/health`);
88 if (!response.ok) {
89 throw new Error(`Health check failed: ${response.status}`);
90 }
91 return await response.json();
92 } catch (error) {
93 console.error('Health check failed:', error);
94 throw error;
95 }
96 }
97
98 async sendMessage(request: ChatRequest): Promise<ChatResponse> {
99 try {
100 const response = await fetch(`${API_BASE_URL}/chat`, {
101 method: 'POST',
102 headers: {
103 'Content-Type': 'application/json',
104 },
105 body: JSON.stringify({
106 message: request.message,
107 model: request.model || 'llama3.2:latest',
108 conversation_history: request.conversation_history || [],
109 }),
110 });
111
112 if (!response.ok) {
113 const errorData = await response.json().catch(() => ({ error: 'Unknown error' }));
114 throw new Error(`Chat API error: ${errorData.error || response.statusText}`);
115 }
116
117 return await response.json();
118 } catch (error) {
119 console.error('Chat API failed:', error);
120 throw error;
121 }
122 }
123
124 // Convert ChatMessage array to conversation history format
125 messagesToHistory(messages: ChatMessage[]): Array<{ role: 'user' | 'assistant'; content: string }> {
126 return messages
127 .filter(msg => typeof msg.content === 'string' && msg.content.trim())
128 .map(msg => ({
129 role: msg.sender,
130 content: msg.content as string,
131 }));
132 }
133
134 // Session Management
135 async getSessions(): Promise<SessionResponse> {
136 try {
137 const response = await fetch(`${API_BASE_URL}/sessions`);
138 if (!response.ok) {
139 throw new Error(`Failed to get sessions: ${response.status}`);
140 }
141 return await response.json();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected