From d362211d6579ea22475cab8d446455b9127fd24f Mon Sep 17 00:00:00 2001 From: hannesrudolph Date: Mon, 9 Jun 2025 12:10:11 -0600 Subject: [PATCH] refactor(bedrock): consolidate stream processing for better maintainability - Replace multiple handler methods with unified processStreamEvent() - Simplify main stream processing loop from ~30 lines to 1 line - Maintain all existing functionality and test coverage - Address PR reviewer feedback for improved code organization Addresses: Stream processing consolidation suggestion from PR review --- src/api/providers/bedrock.ts | 103 +++++++---------------------------- 1 file changed, 21 insertions(+), 82 deletions(-) diff --git a/src/api/providers/bedrock.ts b/src/api/providers/bedrock.ts index 2cf6b31acd..a111ba6367 100644 --- a/src/api/providers/bedrock.ts +++ b/src/api/providers/bedrock.ts @@ -165,63 +165,30 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH } /** - * Handles thinking content block events + * Unified stream event processor for better maintainability */ - private *handleThinkingContentBlock(contentBlock: any): Generator { - if (contentBlock?.type === "thinking" && contentBlock.thinking !== undefined) { - yield { - type: "reasoning", - text: contentBlock.thinking || "", + private *processStreamEvent(streamEvent: StreamEvent): Generator { + // Handle content blocks + if (streamEvent.contentBlockStart) { + const { contentBlock, start } = streamEvent.contentBlockStart + + if (contentBlock?.type === "thinking" && contentBlock.thinking !== undefined) { + yield { type: "reasoning", text: contentBlock.thinking || "" } + } else if (start?.text || contentBlock?.text) { + yield { type: "text", text: start?.text || contentBlock?.text || "" } } } - } - /** - * Handles text content block events - */ - private *handleTextContentBlock(start: any, contentBlock: any): Generator { - const text = start?.text || contentBlock?.text - if (text !== undefined) { - yield { - type: "text", - text: text || "", - } - } - } + // Handle content deltas + if (streamEvent.contentBlockDelta?.delta) { + const { delta } = streamEvent.contentBlockDelta - /** - * Handles thinking delta events - */ - private *handleThinkingDelta(delta: any): Generator { - if (delta.type === "thinking_delta" && delta.thinking) { - yield { - type: "reasoning", - text: delta.thinking, - } - } - } - - /** - * Handles signature delta events (part of thinking) - */ - private *handleSignatureDelta(delta: any): Generator { - if (delta.type === "signature_delta" && delta.signature) { - // Signature is part of the thinking process, treat it as reasoning - yield { - type: "reasoning", - text: delta.signature, - } - } - } - - /** - * Handles text delta events - */ - private *handleTextDelta(delta: any): Generator { - if (delta.text) { - yield { - type: "text", - text: delta.text, + if (delta.type === "thinking_delta" && delta.thinking) { + yield { type: "reasoning", text: delta.thinking } + } else if (delta.type === "signature_delta" && delta.signature) { + yield { type: "reasoning", text: delta.signature } + } else if (delta.text) { + yield { type: "text", text: delta.text } } } } @@ -582,36 +549,8 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH continue } - // Handle content blocks - if (streamEvent.contentBlockStart) { - const { contentBlock, start } = streamEvent.contentBlockStart - - // Handle thinking content blocks - if (contentBlock?.type === "thinking") { - yield* this.handleThinkingContentBlock(contentBlock) - continue - } - - // Handle regular text content blocks - if (start?.text || contentBlock?.text) { - yield* this.handleTextContentBlock(start, contentBlock) - continue - } - } - - // Handle content deltas - if (streamEvent.contentBlockDelta?.delta) { - const delta = streamEvent.contentBlockDelta.delta - - // Handle thinking deltas - yield* this.handleThinkingDelta(delta) - - // Handle signature deltas (part of thinking) - yield* this.handleSignatureDelta(delta) - - // Handle regular text deltas - yield* this.handleTextDelta(delta) - } + // Use unified stream event processor + yield* this.processStreamEvent(streamEvent) // Handle message stop if (streamEvent.messageStop) { continue