MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / askModel

Function askModel

graph-ui/src/chat/chat-client.ts:178–229  ·  view source on GitHub ↗
(request: ChatRequest)

Source from the content-addressed store, hash-verified

176
177/** Ask the sidecar once. Rejects with a {@link ChatError} and never with a string. */
178export async function askModel(request: ChatRequest): Promise<ChatReply> {
179 const started = Date.now();
180 let response: Response;
181 try {
182 response = await request.fetch(`${request.origin}${SIDECAR_CHAT_PATH}`, {
183 method: 'POST',
184 headers: { 'Content-Type': 'application/json' },
185 body: JSON.stringify(chatBody(request)),
186 ...(request.signal === undefined ? {} : { signal: request.signal }),
187 });
188 } catch (error) {
189 throw new ChatError(
190 'unreachable',
191 `the sidecar did not answer: ${error instanceof Error ? error.message : String(error)}`,
192 );
193 }
194 const text = await response.text();
195 if (!response.ok) {
196 throw new ChatError('http', `the sidecar answered with HTTP ${response.status}: ${text.slice(0, 200)}`);
197 }
198 let payload: unknown;
199 try {
200 payload = JSON.parse(text);
201 } catch {
202 throw new ChatError('malformed', `the sidecar answered with something that is not JSON: ${text.slice(0, 200)}`);
203 }
204 const record = asRecord(payload);
205 const choices = record['choices'];
206 const first = Array.isArray(choices) ? asRecord(choices[0]) : {};
207 const message = asRecord(first['message']);
208 const content = asText(message['content']).trim();
209 const reasoning = asText(message['reasoning_content']).trim();
210 const usage = asRecord(record['usage']);
211 const finishReason = asText(first['finish_reason']);
212 const reply: ChatReply = {
213 content,
214 reasoning,
215 thoughtOnly: content.length === 0 && reasoning.length > 0,
216 truncated: finishReason === 'length',
217 finishReason,
218 durationMs: Date.now() - started,
219 };
220 const promptTokens = asCount(usage['prompt_tokens']);
221 if (promptTokens !== undefined) {
222 reply.promptTokens = promptTokens;
223 }
224 const completionTokens = asCount(usage['completion_tokens']);
225 if (completionTokens !== undefined) {
226 reply.completionTokens = completionTokens;
227 }
228 return reply;
229}

Callers 5

TwinPanelFunction · 0.90
askAiFunction · 0.90
askAtlasFunction · 0.90
askAiFunction · 0.90

Calls 5

chatBodyFunction · 0.85
asTextFunction · 0.85
asCountFunction · 0.85
asRecordFunction · 0.70
textMethod · 0.45

Tested by

no test coverage detected