Skip to content

Commit d362211

Browse files
committed
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
1 parent 69cfac0 commit d362211

File tree

1 file changed

+21
-82
lines changed

1 file changed

+21
-82
lines changed

src/api/providers/bedrock.ts

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -165,63 +165,30 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
165165
}
166166

167167
/**
168-
* Handles thinking content block events
168+
* Unified stream event processor for better maintainability
169169
*/
170-
private *handleThinkingContentBlock(contentBlock: any): Generator<ApiStreamChunk, void, unknown> {
171-
if (contentBlock?.type === "thinking" && contentBlock.thinking !== undefined) {
172-
yield {
173-
type: "reasoning",
174-
text: contentBlock.thinking || "",
170+
private *processStreamEvent(streamEvent: StreamEvent): Generator<ApiStreamChunk, void, unknown> {
171+
// Handle content blocks
172+
if (streamEvent.contentBlockStart) {
173+
const { contentBlock, start } = streamEvent.contentBlockStart
174+
175+
if (contentBlock?.type === "thinking" && contentBlock.thinking !== undefined) {
176+
yield { type: "reasoning", text: contentBlock.thinking || "" }
177+
} else if (start?.text || contentBlock?.text) {
178+
yield { type: "text", text: start?.text || contentBlock?.text || "" }
175179
}
176180
}
177-
}
178-
179-
/**
180-
* Handles text content block events
181-
*/
182-
private *handleTextContentBlock(start: any, contentBlock: any): Generator<ApiStreamChunk, void, unknown> {
183-
const text = start?.text || contentBlock?.text
184-
if (text !== undefined) {
185-
yield {
186-
type: "text",
187-
text: text || "",
188-
}
189-
}
190-
}
191-
192-
/**
193-
* Handles thinking delta events
194-
*/
195-
private *handleThinkingDelta(delta: any): Generator<ApiStreamChunk, void, unknown> {
196-
if (delta.type === "thinking_delta" && delta.thinking) {
197-
yield {
198-
type: "reasoning",
199-
text: delta.thinking,
200-
}
201-
}
202-
}
203181

204-
/**
205-
* Handles signature delta events (part of thinking)
206-
*/
207-
private *handleSignatureDelta(delta: any): Generator<ApiStreamChunk, void, unknown> {
208-
if (delta.type === "signature_delta" && delta.signature) {
209-
// Signature is part of the thinking process, treat it as reasoning
210-
yield {
211-
type: "reasoning",
212-
text: delta.signature,
213-
}
214-
}
215-
}
182+
// Handle content deltas
183+
if (streamEvent.contentBlockDelta?.delta) {
184+
const { delta } = streamEvent.contentBlockDelta
216185

217-
/**
218-
* Handles text delta events
219-
*/
220-
private *handleTextDelta(delta: any): Generator<ApiStreamChunk, void, unknown> {
221-
if (delta.text) {
222-
yield {
223-
type: "text",
224-
text: delta.text,
186+
if (delta.type === "thinking_delta" && delta.thinking) {
187+
yield { type: "reasoning", text: delta.thinking }
188+
} else if (delta.type === "signature_delta" && delta.signature) {
189+
yield { type: "reasoning", text: delta.signature }
190+
} else if (delta.text) {
191+
yield { type: "text", text: delta.text }
225192
}
226193
}
227194
}
@@ -582,36 +549,8 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
582549
continue
583550
}
584551

585-
// Handle content blocks
586-
if (streamEvent.contentBlockStart) {
587-
const { contentBlock, start } = streamEvent.contentBlockStart
588-
589-
// Handle thinking content blocks
590-
if (contentBlock?.type === "thinking") {
591-
yield* this.handleThinkingContentBlock(contentBlock)
592-
continue
593-
}
594-
595-
// Handle regular text content blocks
596-
if (start?.text || contentBlock?.text) {
597-
yield* this.handleTextContentBlock(start, contentBlock)
598-
continue
599-
}
600-
}
601-
602-
// Handle content deltas
603-
if (streamEvent.contentBlockDelta?.delta) {
604-
const delta = streamEvent.contentBlockDelta.delta
605-
606-
// Handle thinking deltas
607-
yield* this.handleThinkingDelta(delta)
608-
609-
// Handle signature deltas (part of thinking)
610-
yield* this.handleSignatureDelta(delta)
611-
612-
// Handle regular text deltas
613-
yield* this.handleTextDelta(delta)
614-
}
552+
// Use unified stream event processor
553+
yield* this.processStreamEvent(streamEvent)
615554
// Handle message stop
616555
if (streamEvent.messageStop) {
617556
continue

0 commit comments

Comments
 (0)