Skip to content

Commit cca9a9e

Browse files
Decoding run stream events (#46)
* Getting stream run events * typo
1 parent 3be54cb commit cca9a9e

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

Examples/SwiftOpenAIExample/SwiftOpenAIExample/Assistants/AssistantThreadConfigurationProvider.swift

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ import SwiftOpenAI
7979
case nil:
8080
print("PROVIDER: tool call nil")
8181
}
82+
case .threadRunCompleted(let runObject):
83+
print("PROVIDER: the run is completed - \(runObject)")
8284
default: break
8385
}
8486
}

Sources/OpenAI/Public/ResponseModels/Assistants/AssistantStreamEvent.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,35 @@ public enum AssistantStreamEvent {
2020

2121
/// Occurs when a run moves to a queued status.
2222
/// - data is a run
23-
case threadRunQueued
23+
case threadRunQueued(RunObject)
2424

2525
/// Occurs when a run moves to an in_progress status.
2626
/// - data is a run
27-
case threadRunInProgress
27+
case threadRunInProgress(RunObject)
2828

2929
/// Occurs when a run moves to a requires_action status.
3030
/// - data is a run
31-
case threadRunRequiresAction
31+
case threadRunRequiresAction(RunObject)
3232

3333
/// Occurs when a run is completed.
3434
/// - data is a run
35-
case threadRunCompleted
35+
case threadRunCompleted(RunObject)
3636

3737
/// Occurs when a run fails.
3838
/// - data is a run
39-
case threadRunFailed
39+
case threadRunFailed(RunObject)
4040

4141
/// Occurs when a run moves to a cancelling status.
4242
/// - data is a run
43-
case threadRunCancelling
43+
case threadRunCancelling(RunObject)
4444

4545
/// Occurs when a run is cancelled.
4646
/// - data is a run
47-
case threadRunCancelled
47+
case threadRunCancelled(RunObject)
4848

4949
/// Occurs when a run expires.
5050
/// - data is a run
51-
case threadRunExpired
51+
case threadRunExpired(RunObject)
5252

5353
/// Occurs when a run step is created.
5454
/// - data is a run step

Sources/OpenAI/Public/ResponseModels/Assistants/AssistantStreamEventObject.swift

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public enum AssistantStreamEventObject: String {
1515
/// - data is a [thread](https://platform.openai.com/docs/api-reference/threads/object)
1616
case threadCreated = "thread.created"
1717

18+
/// Occurs during the life cycle of a run.
19+
/// - data is a [run](https://platform.openai.com/docs/api-reference/runs/object)
20+
case threadRun = "thread.run"
21+
1822
/// Occurs when a new run is created.
1923
/// - data is a [run](https://platform.openai.com/docs/api-reference/runs/object)
2024
case threadRunCreated = "thread.run.created"

Sources/OpenAI/Public/Service/OpenAIService.swift

+30-1
Original file line numberDiff line numberDiff line change
@@ -1180,14 +1180,43 @@ extension OpenAIService {
11801180
case .threadRunStepDelta:
11811181
let decoded = try self.decoder.decode(RunStepDeltaObject.self, from: data)
11821182
continuation.yield(.threadRunStepDelta(decoded))
1183+
case .threadRun:
1184+
// We expect a object of type "thread.run.SOME_STATE" in the data object
1185+
// However what we get is a `thread.run` object but we can check the status
1186+
// of the decoded run to determine the stream event.
1187+
// If we check the event line instead, this will contain the expected "event: thread.run.step.completed" for example.
1188+
// Therefore the need to stream this event in the following way.
1189+
let decoded = try self.decoder.decode(RunObject.self, from: data)
1190+
switch RunObject.Status(rawValue: decoded.status) {
1191+
case .queued:
1192+
continuation.yield(.threadRunQueued(decoded))
1193+
case .inProgress:
1194+
continuation.yield(.threadRunInProgress(decoded))
1195+
case .requiresAction:
1196+
continuation.yield(.threadRunRequiresAction(decoded))
1197+
case .cancelling:
1198+
continuation.yield(.threadRunCancelling(decoded))
1199+
case .cancelled:
1200+
continuation.yield(.threadRunCancelled(decoded))
1201+
case .failed:
1202+
continuation.yield(.threadRunFailed(decoded))
1203+
case .completed:
1204+
continuation.yield(.threadRunCompleted(decoded))
1205+
case .expired:
1206+
continuation.yield(.threadRunExpired(decoded))
1207+
default:
1208+
#if DEBUG
1209+
print("DEBUG threadRun status not found = \(try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any])")
1210+
#endif
1211+
}
11831212
default:
11841213
#if DEBUG
11851214
print("DEBUG EVENT \(eventObject.rawValue) IGNORED = \(try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any])")
11861215
#endif
11871216
}
11881217
} else {
11891218
#if DEBUG
1190-
print("DEBUG EVENT DECODE IGNORED= \(try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any])")
1219+
print("DEBUG EVENT DECODE IGNORED = \(try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any])")
11911220
#endif
11921221
}
11931222
} catch let DecodingError.keyNotFound(key, context) {

0 commit comments

Comments
 (0)