( request: ChatRequest, )
| 44 | } |
| 45 | |
| 46 | private async fetchStream( |
| 47 | request: ChatRequest, |
| 48 | ): Promise<ReadableStream<StreamResponse>> { |
| 49 | try { |
| 50 | console.log("\n=== ChatService Request Start ==="); |
| 51 | console.log("1. Request details:", { |
| 52 | language: get(languageStore), |
| 53 | pattern: get(selectedPatternName), |
| 54 | promptCount: request.prompts?.length, |
| 55 | messageCount: request.messages?.length, |
| 56 | }); |
| 57 | // NEW: Log the full payload before sending to backend |
| 58 | console.log( |
| 59 | "Final ChatRequest payload:", |
| 60 | JSON.stringify(request, null, 2), |
| 61 | ); |
| 62 | |
| 63 | const response = await fetch("/api/chat", { |
| 64 | method: "POST", |
| 65 | headers: { "Content-Type": "application/json" }, |
| 66 | body: JSON.stringify(request), |
| 67 | }); |
| 68 | |
| 69 | if (!response.ok) { |
| 70 | throw new ChatError( |
| 71 | `HTTP error! status: ${response.status}`, |
| 72 | "HTTP_ERROR", |
| 73 | { status: response.status }, |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | const reader = response.body?.getReader(); |
| 78 | if (!reader) { |
| 79 | throw new ChatError("Response body is null", "NULL_RESPONSE"); |
| 80 | } |
| 81 | |
| 82 | return this.createMessageStream(reader); |
| 83 | } catch (error) { |
| 84 | if (error instanceof ChatError) throw error; |
| 85 | throw new ChatError("Failed to fetch chat stream", "FETCH_ERROR", error); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Clean up pattern output for display. Should only be called on complete/accumulated content, |
no test coverage detected