@@ -23,8 +23,8 @@ public final class TerminalChatPlugin: ChatPlugin {
2323 terminal = Terminal ( )
2424 }
2525
26- public func sendForTextResponse ( _ request: Request ) async
27- -> AsyncThrowingStream < String , any Error >
26+ public func getTextContent ( from request: Request ) async
27+ -> AsyncStream < String >
2828 {
2929 return . init { continuation in
3030 let task = Task {
@@ -78,11 +78,33 @@ public final class TerminalChatPlugin: ChatPlugin {
7878 }
7979 }
8080
81+ public func sendForTextResponse( _ request: Request ) async
82+ -> AsyncThrowingStream < String , any Error >
83+ {
84+ let stream = await getTextContent ( from: request)
85+ return . init { continuation in
86+ let task = Task {
87+ continuation. yield ( " Executing command: ` \( request. text) ` \n \n " )
88+ continuation. yield ( " ```console \n " )
89+ for await text in stream {
90+ try Task . checkCancellation ( )
91+ continuation. yield ( text)
92+ }
93+ continuation. yield ( " \n ``` \n " )
94+ continuation. finish ( )
95+ }
96+
97+ continuation. onTermination = { _ in
98+ task. cancel ( )
99+ }
100+ }
101+ }
102+
81103 public func formatContent( _ content: Response . Content ) -> Response . Content {
82104 switch content {
83105 case let . text( content) :
84106 return . text( """
85- ```sh
107+ ```console
86108 \( content)
87109 ```
88110 """ )
@@ -98,44 +120,40 @@ public final class TerminalChatPlugin: ChatPlugin {
98120
99121 continuation. yield ( . startAction( id: " run " , task: " Run ` \( request. text) ` " ) )
100122
101- let textStream = await sendForTextResponse ( request)
123+ let textStream = await getTextContent ( from : request)
102124 var previousOutput = " "
103125
104126 continuation. yield ( . finishAction(
105127 id: " run " ,
106128 result: . success( " Executed. " )
107129 ) )
108130
109- do {
110- for try await accumulatedOutput in textStream {
111- try Task . checkCancellation ( )
131+ for await accumulatedOutput in textStream {
132+ try Task . checkCancellation ( )
112133
113- let newContent = accumulatedOutput. dropFirst ( previousOutput. count)
114- previousOutput = accumulatedOutput
115-
116- if !newContent. isEmpty {
117- if Date ( ) . timeIntervalSince ( updateTime) > 60 * 2 {
118- continuation. yield ( . startNewMessage)
119- continuation. yield ( . startAction(
120- id: " run " ,
121- task: " Continue ` \( request. text) ` "
122- ) )
123- continuation. yield ( . finishAction(
124- id: " run " ,
125- result: . success( " Executed. " )
126- ) )
127- continuation. yield ( . content( . text( " [continue] \n " ) ) )
128- updateTime = Date ( )
129- }
130-
131- continuation. yield ( . content( . text( String ( newContent) ) ) )
134+ let newContent = accumulatedOutput. dropFirst ( previousOutput. count)
135+ previousOutput = accumulatedOutput
136+
137+ if !newContent. isEmpty {
138+ if Date ( ) . timeIntervalSince ( updateTime) > 60 * 2 {
139+ continuation. yield ( . startNewMessage)
140+ continuation. yield ( . startAction(
141+ id: " run " ,
142+ task: " Continue ` \( request. text) ` "
143+ ) )
144+ continuation. yield ( . finishAction(
145+ id: " run " ,
146+ result: . success( " Executed. " )
147+ ) )
148+ continuation. yield ( . content( . text( " [continue] \n " ) ) )
149+ updateTime = Date ( )
132150 }
133- }
134151
135- continuation. finish ( )
136- } catch {
137- continuation. finish ( throwing: error)
152+ continuation. yield ( . content( . text( String ( newContent) ) ) )
153+ }
138154 }
155+
156+ continuation. finish ( )
139157 }
140158
141159 continuation. onTermination = { _ in
0 commit comments