Skip to content

Commit 226bc25

Browse files
committed
Add asTexts
1 parent 8b5445e commit 226bc25

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Tool/Sources/ChatBasic/ChatAgent.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,27 @@ public protocol ChatAgent {
4242
func send(_ request: Request) async -> AsyncThrowingStream<Response, any Error>
4343
}
4444

45+
public extension AsyncThrowingStream<ChatAgentResponse, any Error> {
46+
func asTexts() async throws -> [String] {
47+
var result = [String]()
48+
var text = ""
49+
for try await response in self {
50+
switch response {
51+
case let .content(.text(content)):
52+
text += content
53+
case .startNewMessage:
54+
if !text.isEmpty {
55+
result.append(text)
56+
text = ""
57+
}
58+
default:
59+
break
60+
}
61+
}
62+
if !text.isEmpty {
63+
result.append(text)
64+
}
65+
return result
66+
}
67+
}
68+

0 commit comments

Comments
 (0)