-
Notifications
You must be signed in to change notification settings - Fork 865
Improve Claude stream completion handling #1030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { getUserConfig } from '../../config/index.mjs' | ||
| import { pushRecord, setAbortController } from './shared.mjs' | ||
| import { fetchSSE } from '../../utils/fetch-sse.mjs' | ||
| import { FETCH_RESPONSE_STREAM_FAILED, fetchSSE } from '../../utils/fetch-sse.mjs' | ||
| import { isEmpty } from 'lodash-es' | ||
| import { getConversationPairs } from '../../utils/get-conversation-pairs.mjs' | ||
| import { getModelValue } from '../../utils/model-name-convert.mjs' | ||
|
|
@@ -49,7 +49,11 @@ export async function generateAnswersWithClaudeApi(port, question, session) { | |
| } | ||
|
|
||
| let answer = '' | ||
| await fetchSSE(`${apiUrl}/v1/messages`, { | ||
| let stopReason = '' | ||
| let completionError | ||
| let wasAborted = false | ||
| let completedSuccessfully = false | ||
| const streamError = await fetchSSE(`${apiUrl}/v1/messages`, { | ||
| method: 'POST', | ||
| signal: controller.signal, | ||
| headers: { | ||
|
|
@@ -69,10 +73,42 @@ export async function generateAnswersWithClaudeApi(port, question, session) { | |
| console.debug('json error', error) | ||
| return | ||
| } | ||
| if (data?.type === 'error') { | ||
| controller.abort() | ||
| const error = new Error(JSON.stringify(data)) | ||
| if (completedSuccessfully) error.code = FETCH_RESPONSE_STREAM_FAILED | ||
| throw error | ||
| } | ||
| if (completedSuccessfully) return | ||
| if (data?.type === 'message_delta') { | ||
| stopReason = data?.delta?.stop_reason || stopReason | ||
| return | ||
| } | ||
| if (data?.type === 'message_stop') { | ||
| if (stopReason === 'max_tokens') { | ||
| completionError = new Error( | ||
| 'Claude reached the response token limit. Increase Max Response Token Length and try again.', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Claude strings exceed 100 columns The new Claude error string and SSE fixtures contain physical lines longer than 100 characters. These additions violate the source line-length limit. Agent Prompt
|
||
| ) | ||
| } | ||
| if (stopReason === 'model_context_window_exceeded') { | ||
| completionError = new Error( | ||
| 'Claude reached the model context window limit. Clear the conversation and try again.', | ||
| ) | ||
| } | ||
| if (stopReason === 'refusal') { | ||
| completionError = new Error('Claude declined to respond to this request.') | ||
| } | ||
| if (!completionError && !['end_turn', 'stop_sequence'].includes(stopReason)) { | ||
| completionError = new Error('Claude response stream ended before completion.') | ||
| } | ||
| if (completionError) { | ||
| controller.abort() | ||
| throw completionError | ||
| } | ||
|
Comment on lines
+98
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Anthropic returns another non-final stop reason such as Useful? React with 👍 / 👎. |
||
| pushRecord(session, question, answer) | ||
| console.debug('conversation history', { content: session.conversationRecords }) | ||
| port.postMessage({ answer: null, done: true, session: session }) | ||
| completedSuccessfully = true | ||
| return | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
|
|
@@ -84,12 +120,10 @@ export async function generateAnswersWithClaudeApi(port, question, session) { | |
| }, | ||
| async onStart() {}, | ||
| async onEnd(aborted) { | ||
| wasAborted = aborted | ||
| try { | ||
| if (!aborted) { | ||
| port.postMessage({ done: true }) | ||
| } | ||
| } finally { | ||
| port.onMessage.removeListener(messageListener) | ||
| } finally { | ||
| port.onDisconnect.removeListener(disconnectListener) | ||
| } | ||
| }, | ||
|
|
@@ -100,5 +134,16 @@ export async function generateAnswersWithClaudeApi(port, question, session) { | |
| const error = await resp.json().catch(() => ({})) | ||
| throw new Error(!isEmpty(error) ? JSON.stringify(error) : `${resp.status} ${resp.statusText}`) | ||
| }, | ||
| }) | ||
| }).catch((error) => error) | ||
| if (wasAborted) return | ||
| if (completionError) throw completionError | ||
| if ( | ||
| streamError && | ||
| (!completedSuccessfully || streamError.code !== FETCH_RESPONSE_STREAM_FAILED) | ||
| ) { | ||
| throw streamError | ||
| } | ||
| if (!completedSuccessfully) { | ||
| throw new Error('Claude response stream ended before completion.') | ||
|
Comment on lines
+146
to
+147
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Anthropic accepts the request but later emits an SSE Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Locale json lines exceed 100
📘 Rule violation⚙ MaintainabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools