mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-06-06 02:31:13 +02:00
fix: correct API endpoint and add debug logging for AI chat
- Fix endpoint from /chat/completions to /v1/chat/completions (OpenAI-compatible) - Add console logging for request URL, model, message count - Add logging for stream chunks and completion status - Better error messages with HTTP status codes
This commit is contained in:
@@ -242,7 +242,12 @@ export const aiService = {
|
|||||||
messages: { role: string; content: string }[],
|
messages: { role: string; content: string }[],
|
||||||
model: string,
|
model: string,
|
||||||
): AsyncGenerator<string, void, unknown> {
|
): AsyncGenerator<string, void, unknown> {
|
||||||
const response = await fetch(`${provider.baseUrl}/chat/completions`, {
|
const url = `${provider.baseUrl}/v1/chat/completions`;
|
||||||
|
console.log("[AI] Streaming chat request to:", url);
|
||||||
|
console.log("[AI] Model:", model);
|
||||||
|
console.log("[AI] Messages:", messages.length);
|
||||||
|
|
||||||
|
const response = await fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -257,13 +262,16 @@ export const aiService = {
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.text();
|
const error = await response.text();
|
||||||
throw new Error(`Chat completion failed: ${error}`);
|
console.error("[AI] Chat completion failed:", response.status, error);
|
||||||
|
throw new Error(`Chat completion failed (${response.status}): ${error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.body) {
|
if (!response.body) {
|
||||||
throw new Error("No response body");
|
throw new Error("No response body");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("[AI] Response received, starting stream...");
|
||||||
|
|
||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let buffer = "";
|
let buffer = "";
|
||||||
@@ -282,7 +290,10 @@ export const aiService = {
|
|||||||
if (!trimmed || !trimmed.startsWith("data: ")) continue;
|
if (!trimmed || !trimmed.startsWith("data: ")) continue;
|
||||||
|
|
||||||
const data = trimmed.slice(6);
|
const data = trimmed.slice(6);
|
||||||
if (data === "[DONE]") return;
|
if (data === "[DONE]") {
|
||||||
|
console.log("[AI] Stream complete");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(data);
|
const parsed = JSON.parse(data);
|
||||||
@@ -290,8 +301,8 @@ export const aiService = {
|
|||||||
if (content) {
|
if (content) {
|
||||||
yield content;
|
yield content;
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (e) {
|
||||||
// Skip invalid JSON lines
|
console.warn("[AI] Failed to parse SSE data:", data, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -309,6 +320,8 @@ export const aiService = {
|
|||||||
content: string,
|
content: string,
|
||||||
onChunk?: (chunk: string, fullContent: string) => void,
|
onChunk?: (chunk: string, fullContent: string) => void,
|
||||||
): Promise<AIMessage> {
|
): Promise<AIMessage> {
|
||||||
|
console.log("[AI] sendMessage called for conversation:", conversationId);
|
||||||
|
|
||||||
// Get conversation and provider
|
// Get conversation and provider
|
||||||
const conversation = await this.getConversation(conversationId);
|
const conversation = await this.getConversation(conversationId);
|
||||||
if (!conversation) {
|
if (!conversation) {
|
||||||
@@ -320,8 +333,12 @@ export const aiService = {
|
|||||||
throw new Error("Provider not found");
|
throw new Error("Provider not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("[AI] Provider:", provider.name, provider.baseUrl);
|
||||||
|
console.log("[AI] Model:", conversation.model);
|
||||||
|
|
||||||
// Add user message
|
// Add user message
|
||||||
await this.addMessage(conversationId, "user", content);
|
await this.addMessage(conversationId, "user", content);
|
||||||
|
console.log("[AI] User message added");
|
||||||
|
|
||||||
// Get all messages for context
|
// Get all messages for context
|
||||||
const messages = await this.getMessages(conversationId);
|
const messages = await this.getMessages(conversationId);
|
||||||
@@ -340,12 +357,15 @@ export const aiService = {
|
|||||||
|
|
||||||
// Stream the response
|
// Stream the response
|
||||||
let fullContent = "";
|
let fullContent = "";
|
||||||
|
let chunkCount = 0;
|
||||||
try {
|
try {
|
||||||
|
console.log("[AI] Starting to stream response...");
|
||||||
for await (const chunk of this.streamChat(
|
for await (const chunk of this.streamChat(
|
||||||
provider,
|
provider,
|
||||||
chatMessages,
|
chatMessages,
|
||||||
conversation.model,
|
conversation.model,
|
||||||
)) {
|
)) {
|
||||||
|
chunkCount++;
|
||||||
fullContent += chunk;
|
fullContent += chunk;
|
||||||
await this.updateMessage(assistantMessage.id, {
|
await this.updateMessage(assistantMessage.id, {
|
||||||
content: fullContent,
|
content: fullContent,
|
||||||
@@ -353,6 +373,13 @@ export const aiService = {
|
|||||||
onChunk?.(chunk, fullContent);
|
onChunk?.(chunk, fullContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"[AI] Stream finished. Total chunks:",
|
||||||
|
chunkCount,
|
||||||
|
"Total chars:",
|
||||||
|
fullContent.length,
|
||||||
|
);
|
||||||
|
|
||||||
// Mark as complete
|
// Mark as complete
|
||||||
await this.updateMessage(assistantMessage.id, {
|
await this.updateMessage(assistantMessage.id, {
|
||||||
isStreaming: false,
|
isStreaming: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user