-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aggregate parser logic into single object
- Loading branch information
1 parent
c7991ab
commit 9c0c1e6
Showing
2 changed files
with
61 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Sources/AnthropicSwiftSDK/Network/StreamingParser/AnthropicStreamingParser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// AnthropicStreamingParser.swift | ||
// | ||
// | ||
// Created by 伊藤史 on 2024/03/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum AnthropicStreamingParser { | ||
public static func parse<T: AsyncSequence>(stream: T) async throws -> AsyncThrowingStream<StreamingResponse, Error> where T.Element == String { | ||
return AsyncThrowingStream.init { continuation in | ||
let task = Task { | ||
var currentEvent: StreamingEvent? | ||
for try await line in stream { | ||
do { | ||
let lineType = try StreamingResponseParser.parse(line: line) | ||
switch lineType { | ||
case .empty: | ||
break | ||
case .event: | ||
currentEvent = try StreamingEventLineParser.parse(eventLine: line) | ||
case .data: | ||
guard let currentEvent = currentEvent else { | ||
break | ||
} | ||
|
||
switch currentEvent { | ||
case .ping: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingPingResponse) | ||
case .messageStart: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingMessageStartResponse) | ||
case .messageDelta: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingMessageDeltaResponse) | ||
case .messageStop: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingMessageStopResponse) | ||
case .contentBlockStart: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingContentBlockStartResponse) | ||
case .contentBlockDelta: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingContentBlockDeltaResponse) | ||
case .contentBlockStop: | ||
continuation.yield(try StreamingDataLineParser.parse(dataLine: line) as StreamingContentBlockStopResponse) | ||
case .error: | ||
let data = try StreamingDataLineParser.parse(dataLine: line) as StreamingErrorResponse | ||
continuation.finish(throwing: data.error.type) | ||
} | ||
} | ||
} catch let error { | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
|
||
continuation.finish() | ||
} | ||
continuation.onTermination = { @Sendable _ in | ||
task.cancel() | ||
} | ||
} | ||
} | ||
} |