Skip to content

Commit 37c8dc9

Browse files
feat(client): make response union's AsAny method type safe (#352)
1 parent 8830750 commit 37c8dc9

7 files changed

+371
-26
lines changed

audiotranscription.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ type TranscriptionStreamEventUnion struct {
140140
} `json:"-"`
141141
}
142142

143+
// anyTranscriptionStreamEvent is implemented by each variant of
144+
// [TranscriptionStreamEventUnion] to add type safety for the return type of
145+
// [TranscriptionStreamEventUnion.AsAny]
146+
type anyTranscriptionStreamEvent interface {
147+
implTranscriptionStreamEventUnion()
148+
}
149+
150+
func (TranscriptionTextDeltaEvent) implTranscriptionStreamEventUnion() {}
151+
func (TranscriptionTextDoneEvent) implTranscriptionStreamEventUnion() {}
152+
143153
// Use the following switch statement to find the correct variant
144154
//
145155
// switch variant := TranscriptionStreamEventUnion.AsAny().(type) {
@@ -148,7 +158,7 @@ type TranscriptionStreamEventUnion struct {
148158
// default:
149159
// fmt.Errorf("no variant present")
150160
// }
151-
func (u TranscriptionStreamEventUnion) AsAny() any {
161+
func (u TranscriptionStreamEventUnion) AsAny() anyTranscriptionStreamEvent {
152162
switch u.Type {
153163
case "transcript.text.delta":
154164
return u.AsTranscriptTextDelta()

betaassistant.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,38 @@ type AssistantStreamEventUnion struct {
344344
} `json:"-"`
345345
}
346346

347+
// anyAssistantStreamEvent is implemented by each variant of
348+
// [AssistantStreamEventUnion] to add type safety for the return type of
349+
// [AssistantStreamEventUnion.AsAny]
350+
type anyAssistantStreamEvent interface {
351+
implAssistantStreamEventUnion()
352+
}
353+
354+
func (AssistantStreamEventThreadCreated) implAssistantStreamEventUnion() {}
355+
func (AssistantStreamEventThreadRunCreated) implAssistantStreamEventUnion() {}
356+
func (AssistantStreamEventThreadRunQueued) implAssistantStreamEventUnion() {}
357+
func (AssistantStreamEventThreadRunInProgress) implAssistantStreamEventUnion() {}
358+
func (AssistantStreamEventThreadRunRequiresAction) implAssistantStreamEventUnion() {}
359+
func (AssistantStreamEventThreadRunCompleted) implAssistantStreamEventUnion() {}
360+
func (AssistantStreamEventThreadRunIncomplete) implAssistantStreamEventUnion() {}
361+
func (AssistantStreamEventThreadRunFailed) implAssistantStreamEventUnion() {}
362+
func (AssistantStreamEventThreadRunCancelling) implAssistantStreamEventUnion() {}
363+
func (AssistantStreamEventThreadRunCancelled) implAssistantStreamEventUnion() {}
364+
func (AssistantStreamEventThreadRunExpired) implAssistantStreamEventUnion() {}
365+
func (AssistantStreamEventThreadRunStepCreated) implAssistantStreamEventUnion() {}
366+
func (AssistantStreamEventThreadRunStepInProgress) implAssistantStreamEventUnion() {}
367+
func (AssistantStreamEventThreadRunStepDelta) implAssistantStreamEventUnion() {}
368+
func (AssistantStreamEventThreadRunStepCompleted) implAssistantStreamEventUnion() {}
369+
func (AssistantStreamEventThreadRunStepFailed) implAssistantStreamEventUnion() {}
370+
func (AssistantStreamEventThreadRunStepCancelled) implAssistantStreamEventUnion() {}
371+
func (AssistantStreamEventThreadRunStepExpired) implAssistantStreamEventUnion() {}
372+
func (AssistantStreamEventThreadMessageCreated) implAssistantStreamEventUnion() {}
373+
func (AssistantStreamEventThreadMessageInProgress) implAssistantStreamEventUnion() {}
374+
func (AssistantStreamEventThreadMessageDelta) implAssistantStreamEventUnion() {}
375+
func (AssistantStreamEventThreadMessageCompleted) implAssistantStreamEventUnion() {}
376+
func (AssistantStreamEventThreadMessageIncomplete) implAssistantStreamEventUnion() {}
377+
func (AssistantStreamEventErrorEvent) implAssistantStreamEventUnion() {}
378+
347379
// Use the following switch statement to find the correct variant
348380
//
349381
// switch variant := AssistantStreamEventUnion.AsAny().(type) {
@@ -374,7 +406,7 @@ type AssistantStreamEventUnion struct {
374406
// default:
375407
// fmt.Errorf("no variant present")
376408
// }
377-
func (u AssistantStreamEventUnion) AsAny() any {
409+
func (u AssistantStreamEventUnion) AsAny() anyAssistantStreamEvent {
378410
switch u.Event {
379411
case "thread.created":
380412
return u.AsThreadCreated()
@@ -1347,6 +1379,16 @@ type AssistantToolUnion struct {
13471379
} `json:"-"`
13481380
}
13491381

1382+
// anyAssistantTool is implemented by each variant of [AssistantToolUnion] to add
1383+
// type safety for the return type of [AssistantToolUnion.AsAny]
1384+
type anyAssistantTool interface {
1385+
implAssistantToolUnion()
1386+
}
1387+
1388+
func (CodeInterpreterTool) implAssistantToolUnion() {}
1389+
func (FileSearchTool) implAssistantToolUnion() {}
1390+
func (FunctionTool) implAssistantToolUnion() {}
1391+
13501392
// Use the following switch statement to find the correct variant
13511393
//
13521394
// switch variant := AssistantToolUnion.AsAny().(type) {
@@ -1356,7 +1398,7 @@ type AssistantToolUnion struct {
13561398
// default:
13571399
// fmt.Errorf("no variant present")
13581400
// }
1359-
func (u AssistantToolUnion) AsAny() any {
1401+
func (u AssistantToolUnion) AsAny() anyAssistantTool {
13601402
switch u.Type {
13611403
case "code_interpreter":
13621404
return u.AsCodeInterpreter()

betathreadmessage.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ type AnnotationUnion struct {
160160
} `json:"-"`
161161
}
162162

163+
// anyAnnotation is implemented by each variant of [AnnotationUnion] to add type
164+
// safety for the return type of [AnnotationUnion.AsAny]
165+
type anyAnnotation interface {
166+
implAnnotationUnion()
167+
}
168+
169+
func (FileCitationAnnotation) implAnnotationUnion() {}
170+
func (FilePathAnnotation) implAnnotationUnion() {}
171+
163172
// Use the following switch statement to find the correct variant
164173
//
165174
// switch variant := AnnotationUnion.AsAny().(type) {
@@ -168,7 +177,7 @@ type AnnotationUnion struct {
168177
// default:
169178
// fmt.Errorf("no variant present")
170179
// }
171-
func (u AnnotationUnion) AsAny() any {
180+
func (u AnnotationUnion) AsAny() anyAnnotation {
172181
switch u.Type {
173182
case "file_citation":
174183
return u.AsFileCitation()
@@ -224,6 +233,15 @@ type AnnotationDeltaUnion struct {
224233
} `json:"-"`
225234
}
226235

236+
// anyAnnotationDelta is implemented by each variant of [AnnotationDeltaUnion] to
237+
// add type safety for the return type of [AnnotationDeltaUnion.AsAny]
238+
type anyAnnotationDelta interface {
239+
implAnnotationDeltaUnion()
240+
}
241+
242+
func (FileCitationDeltaAnnotation) implAnnotationDeltaUnion() {}
243+
func (FilePathDeltaAnnotation) implAnnotationDeltaUnion() {}
244+
227245
// Use the following switch statement to find the correct variant
228246
//
229247
// switch variant := AnnotationDeltaUnion.AsAny().(type) {
@@ -232,7 +250,7 @@ type AnnotationDeltaUnion struct {
232250
// default:
233251
// fmt.Errorf("no variant present")
234252
// }
235-
func (u AnnotationDeltaUnion) AsAny() any {
253+
func (u AnnotationDeltaUnion) AsAny() anyAnnotationDelta {
236254
switch u.Type {
237255
case "file_citation":
238256
return u.AsFileCitation()
@@ -1022,6 +1040,17 @@ type MessageContentUnion struct {
10221040
} `json:"-"`
10231041
}
10241042

1043+
// anyMessageContent is implemented by each variant of [MessageContentUnion] to add
1044+
// type safety for the return type of [MessageContentUnion.AsAny]
1045+
type anyMessageContent interface {
1046+
implMessageContentUnion()
1047+
}
1048+
1049+
func (ImageFileContentBlock) implMessageContentUnion() {}
1050+
func (ImageURLContentBlock) implMessageContentUnion() {}
1051+
func (TextContentBlock) implMessageContentUnion() {}
1052+
func (RefusalContentBlock) implMessageContentUnion() {}
1053+
10251054
// Use the following switch statement to find the correct variant
10261055
//
10271056
// switch variant := MessageContentUnion.AsAny().(type) {
@@ -1032,7 +1061,7 @@ type MessageContentUnion struct {
10321061
// default:
10331062
// fmt.Errorf("no variant present")
10341063
// }
1035-
func (u MessageContentUnion) AsAny() any {
1064+
func (u MessageContentUnion) AsAny() anyMessageContent {
10361065
switch u.Type {
10371066
case "image_file":
10381067
return u.AsImageFile()
@@ -1103,6 +1132,18 @@ type MessageContentDeltaUnion struct {
11031132
} `json:"-"`
11041133
}
11051134

1135+
// anyMessageContentDelta is implemented by each variant of
1136+
// [MessageContentDeltaUnion] to add type safety for the return type of
1137+
// [MessageContentDeltaUnion.AsAny]
1138+
type anyMessageContentDelta interface {
1139+
implMessageContentDeltaUnion()
1140+
}
1141+
1142+
func (ImageFileDeltaBlock) implMessageContentDeltaUnion() {}
1143+
func (TextDeltaBlock) implMessageContentDeltaUnion() {}
1144+
func (RefusalDeltaBlock) implMessageContentDeltaUnion() {}
1145+
func (ImageURLDeltaBlock) implMessageContentDeltaUnion() {}
1146+
11061147
// Use the following switch statement to find the correct variant
11071148
//
11081149
// switch variant := MessageContentDeltaUnion.AsAny().(type) {
@@ -1113,7 +1154,7 @@ type MessageContentDeltaUnion struct {
11131154
// default:
11141155
// fmt.Errorf("no variant present")
11151156
// }
1116-
func (u MessageContentDeltaUnion) AsAny() any {
1157+
func (u MessageContentDeltaUnion) AsAny() anyMessageContentDelta {
11171158
switch u.Type {
11181159
case "image_file":
11191160
return u.AsImageFile()

betathreadrunstep.go

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ type CodeInterpreterToolCallCodeInterpreterOutputUnion struct {
232232
} `json:"-"`
233233
}
234234

235+
// anyCodeInterpreterToolCallCodeInterpreterOutput is implemented by each variant
236+
// of [CodeInterpreterToolCallCodeInterpreterOutputUnion] to add type safety for
237+
// the return type of [CodeInterpreterToolCallCodeInterpreterOutputUnion.AsAny]
238+
type anyCodeInterpreterToolCallCodeInterpreterOutput interface {
239+
implCodeInterpreterToolCallCodeInterpreterOutputUnion()
240+
}
241+
242+
func (CodeInterpreterToolCallCodeInterpreterOutputLogs) implCodeInterpreterToolCallCodeInterpreterOutputUnion() {
243+
}
244+
func (CodeInterpreterToolCallCodeInterpreterOutputImage) implCodeInterpreterToolCallCodeInterpreterOutputUnion() {
245+
}
246+
235247
// Use the following switch statement to find the correct variant
236248
//
237249
// switch variant := CodeInterpreterToolCallCodeInterpreterOutputUnion.AsAny().(type) {
@@ -240,7 +252,7 @@ type CodeInterpreterToolCallCodeInterpreterOutputUnion struct {
240252
// default:
241253
// fmt.Errorf("no variant present")
242254
// }
243-
func (u CodeInterpreterToolCallCodeInterpreterOutputUnion) AsAny() any {
255+
func (u CodeInterpreterToolCallCodeInterpreterOutputUnion) AsAny() anyCodeInterpreterToolCallCodeInterpreterOutput {
244256
switch u.Type {
245257
case "logs":
246258
return u.AsLogs()
@@ -405,6 +417,17 @@ type CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion struct {
405417
} `json:"-"`
406418
}
407419

420+
// anyCodeInterpreterToolCallDeltaCodeInterpreterOutput is implemented by each
421+
// variant of [CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion] to add type
422+
// safety for the return type of
423+
// [CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion.AsAny]
424+
type anyCodeInterpreterToolCallDeltaCodeInterpreterOutput interface {
425+
implCodeInterpreterToolCallDeltaCodeInterpreterOutputUnion()
426+
}
427+
428+
func (CodeInterpreterLogs) implCodeInterpreterToolCallDeltaCodeInterpreterOutputUnion() {}
429+
func (CodeInterpreterOutputImage) implCodeInterpreterToolCallDeltaCodeInterpreterOutputUnion() {}
430+
408431
// Use the following switch statement to find the correct variant
409432
//
410433
// switch variant := CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion.AsAny().(type) {
@@ -413,7 +436,7 @@ type CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion struct {
413436
// default:
414437
// fmt.Errorf("no variant present")
415438
// }
416-
func (u CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion) AsAny() any {
439+
func (u CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion) AsAny() anyCodeInterpreterToolCallDeltaCodeInterpreterOutput {
417440
switch u.Type {
418441
case "logs":
419442
return u.AsLogs()
@@ -879,6 +902,16 @@ type RunStepStepDetailsUnion struct {
879902
} `json:"-"`
880903
}
881904

905+
// anyRunStepStepDetails is implemented by each variant of
906+
// [RunStepStepDetailsUnion] to add type safety for the return type of
907+
// [RunStepStepDetailsUnion.AsAny]
908+
type anyRunStepStepDetails interface {
909+
implRunStepStepDetailsUnion()
910+
}
911+
912+
func (MessageCreationStepDetails) implRunStepStepDetailsUnion() {}
913+
func (ToolCallsStepDetails) implRunStepStepDetailsUnion() {}
914+
882915
// Use the following switch statement to find the correct variant
883916
//
884917
// switch variant := RunStepStepDetailsUnion.AsAny().(type) {
@@ -887,7 +920,7 @@ type RunStepStepDetailsUnion struct {
887920
// default:
888921
// fmt.Errorf("no variant present")
889922
// }
890-
func (u RunStepStepDetailsUnion) AsAny() any {
923+
func (u RunStepStepDetailsUnion) AsAny() anyRunStepStepDetails {
891924
switch u.Type {
892925
case "message_creation":
893926
return u.AsMessageCreation()
@@ -988,6 +1021,16 @@ type RunStepDeltaStepDetailsUnion struct {
9881021
} `json:"-"`
9891022
}
9901023

1024+
// anyRunStepDeltaStepDetails is implemented by each variant of
1025+
// [RunStepDeltaStepDetailsUnion] to add type safety for the return type of
1026+
// [RunStepDeltaStepDetailsUnion.AsAny]
1027+
type anyRunStepDeltaStepDetails interface {
1028+
implRunStepDeltaStepDetailsUnion()
1029+
}
1030+
1031+
func (RunStepDeltaMessageDelta) implRunStepDeltaStepDetailsUnion() {}
1032+
func (ToolCallDeltaObject) implRunStepDeltaStepDetailsUnion() {}
1033+
9911034
// Use the following switch statement to find the correct variant
9921035
//
9931036
// switch variant := RunStepDeltaStepDetailsUnion.AsAny().(type) {
@@ -996,7 +1039,7 @@ type RunStepDeltaStepDetailsUnion struct {
9961039
// default:
9971040
// fmt.Errorf("no variant present")
9981041
// }
999-
func (u RunStepDeltaStepDetailsUnion) AsAny() any {
1042+
func (u RunStepDeltaStepDetailsUnion) AsAny() anyRunStepDeltaStepDetails {
10001043
switch u.Type {
10011044
case "message_creation":
10021045
return u.AsMessageCreation()
@@ -1120,6 +1163,16 @@ type ToolCallUnion struct {
11201163
} `json:"-"`
11211164
}
11221165

1166+
// anyToolCall is implemented by each variant of [ToolCallUnion] to add type safety
1167+
// for the return type of [ToolCallUnion.AsAny]
1168+
type anyToolCall interface {
1169+
implToolCallUnion()
1170+
}
1171+
1172+
func (CodeInterpreterToolCall) implToolCallUnion() {}
1173+
func (FileSearchToolCall) implToolCallUnion() {}
1174+
func (FunctionToolCall) implToolCallUnion() {}
1175+
11231176
// Use the following switch statement to find the correct variant
11241177
//
11251178
// switch variant := ToolCallUnion.AsAny().(type) {
@@ -1129,7 +1182,7 @@ type ToolCallUnion struct {
11291182
// default:
11301183
// fmt.Errorf("no variant present")
11311184
// }
1132-
func (u ToolCallUnion) AsAny() any {
1185+
func (u ToolCallUnion) AsAny() anyToolCall {
11331186
switch u.Type {
11341187
case "code_interpreter":
11351188
return u.AsCodeInterpreter()
@@ -1192,6 +1245,16 @@ type ToolCallDeltaUnion struct {
11921245
} `json:"-"`
11931246
}
11941247

1248+
// anyToolCallDelta is implemented by each variant of [ToolCallDeltaUnion] to add
1249+
// type safety for the return type of [ToolCallDeltaUnion.AsAny]
1250+
type anyToolCallDelta interface {
1251+
implToolCallDeltaUnion()
1252+
}
1253+
1254+
func (CodeInterpreterToolCallDelta) implToolCallDeltaUnion() {}
1255+
func (FileSearchToolCallDelta) implToolCallDeltaUnion() {}
1256+
func (FunctionToolCallDelta) implToolCallDeltaUnion() {}
1257+
11951258
// Use the following switch statement to find the correct variant
11961259
//
11971260
// switch variant := ToolCallDeltaUnion.AsAny().(type) {
@@ -1201,7 +1264,7 @@ type ToolCallDeltaUnion struct {
12011264
// default:
12021265
// fmt.Errorf("no variant present")
12031266
// }
1204-
func (u ToolCallDeltaUnion) AsAny() any {
1267+
func (u ToolCallDeltaUnion) AsAny() anyToolCallDelta {
12051268
switch u.Type {
12061269
case "code_interpreter":
12071270
return u.AsCodeInterpreter()

0 commit comments

Comments
 (0)