Skip to content

feat(client): make response union's AsAny method type safe #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion audiotranscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ type TranscriptionStreamEventUnion struct {
} `json:"-"`
}

// anyTranscriptionStreamEvent is implemented by each variant of
// [TranscriptionStreamEventUnion] to add type safety for the return type of
// [TranscriptionStreamEventUnion.AsAny]
type anyTranscriptionStreamEvent interface {
implTranscriptionStreamEventUnion()
}

func (TranscriptionTextDeltaEvent) implTranscriptionStreamEventUnion() {}
func (TranscriptionTextDoneEvent) implTranscriptionStreamEventUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := TranscriptionStreamEventUnion.AsAny().(type) {
Expand All @@ -148,7 +158,7 @@ type TranscriptionStreamEventUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u TranscriptionStreamEventUnion) AsAny() any {
func (u TranscriptionStreamEventUnion) AsAny() anyTranscriptionStreamEvent {
switch u.Type {
case "transcript.text.delta":
return u.AsTranscriptTextDelta()
Expand Down
46 changes: 44 additions & 2 deletions betaassistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,38 @@ type AssistantStreamEventUnion struct {
} `json:"-"`
}

// anyAssistantStreamEvent is implemented by each variant of
// [AssistantStreamEventUnion] to add type safety for the return type of
// [AssistantStreamEventUnion.AsAny]
type anyAssistantStreamEvent interface {
implAssistantStreamEventUnion()
}

func (AssistantStreamEventThreadCreated) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunCreated) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunQueued) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunInProgress) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunRequiresAction) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunCompleted) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunIncomplete) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunFailed) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunCancelling) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunCancelled) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunExpired) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepCreated) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepInProgress) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepDelta) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepCompleted) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepFailed) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepCancelled) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadRunStepExpired) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadMessageCreated) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadMessageInProgress) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadMessageDelta) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadMessageCompleted) implAssistantStreamEventUnion() {}
func (AssistantStreamEventThreadMessageIncomplete) implAssistantStreamEventUnion() {}
func (AssistantStreamEventErrorEvent) implAssistantStreamEventUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := AssistantStreamEventUnion.AsAny().(type) {
Expand Down Expand Up @@ -374,7 +406,7 @@ type AssistantStreamEventUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u AssistantStreamEventUnion) AsAny() any {
func (u AssistantStreamEventUnion) AsAny() anyAssistantStreamEvent {
switch u.Event {
case "thread.created":
return u.AsThreadCreated()
Expand Down Expand Up @@ -1347,6 +1379,16 @@ type AssistantToolUnion struct {
} `json:"-"`
}

// anyAssistantTool is implemented by each variant of [AssistantToolUnion] to add
// type safety for the return type of [AssistantToolUnion.AsAny]
type anyAssistantTool interface {
implAssistantToolUnion()
}

func (CodeInterpreterTool) implAssistantToolUnion() {}
func (FileSearchTool) implAssistantToolUnion() {}
func (FunctionTool) implAssistantToolUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := AssistantToolUnion.AsAny().(type) {
Expand All @@ -1356,7 +1398,7 @@ type AssistantToolUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u AssistantToolUnion) AsAny() any {
func (u AssistantToolUnion) AsAny() anyAssistantTool {
switch u.Type {
case "code_interpreter":
return u.AsCodeInterpreter()
Expand Down
49 changes: 45 additions & 4 deletions betathreadmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ type AnnotationUnion struct {
} `json:"-"`
}

// anyAnnotation is implemented by each variant of [AnnotationUnion] to add type
// safety for the return type of [AnnotationUnion.AsAny]
type anyAnnotation interface {
implAnnotationUnion()
}

func (FileCitationAnnotation) implAnnotationUnion() {}
func (FilePathAnnotation) implAnnotationUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := AnnotationUnion.AsAny().(type) {
Expand All @@ -168,7 +177,7 @@ type AnnotationUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u AnnotationUnion) AsAny() any {
func (u AnnotationUnion) AsAny() anyAnnotation {
switch u.Type {
case "file_citation":
return u.AsFileCitation()
Expand Down Expand Up @@ -224,6 +233,15 @@ type AnnotationDeltaUnion struct {
} `json:"-"`
}

// anyAnnotationDelta is implemented by each variant of [AnnotationDeltaUnion] to
// add type safety for the return type of [AnnotationDeltaUnion.AsAny]
type anyAnnotationDelta interface {
implAnnotationDeltaUnion()
}

func (FileCitationDeltaAnnotation) implAnnotationDeltaUnion() {}
func (FilePathDeltaAnnotation) implAnnotationDeltaUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := AnnotationDeltaUnion.AsAny().(type) {
Expand All @@ -232,7 +250,7 @@ type AnnotationDeltaUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u AnnotationDeltaUnion) AsAny() any {
func (u AnnotationDeltaUnion) AsAny() anyAnnotationDelta {
switch u.Type {
case "file_citation":
return u.AsFileCitation()
Expand Down Expand Up @@ -1022,6 +1040,17 @@ type MessageContentUnion struct {
} `json:"-"`
}

// anyMessageContent is implemented by each variant of [MessageContentUnion] to add
// type safety for the return type of [MessageContentUnion.AsAny]
type anyMessageContent interface {
implMessageContentUnion()
}

func (ImageFileContentBlock) implMessageContentUnion() {}
func (ImageURLContentBlock) implMessageContentUnion() {}
func (TextContentBlock) implMessageContentUnion() {}
func (RefusalContentBlock) implMessageContentUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := MessageContentUnion.AsAny().(type) {
Expand All @@ -1032,7 +1061,7 @@ type MessageContentUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u MessageContentUnion) AsAny() any {
func (u MessageContentUnion) AsAny() anyMessageContent {
switch u.Type {
case "image_file":
return u.AsImageFile()
Expand Down Expand Up @@ -1103,6 +1132,18 @@ type MessageContentDeltaUnion struct {
} `json:"-"`
}

// anyMessageContentDelta is implemented by each variant of
// [MessageContentDeltaUnion] to add type safety for the return type of
// [MessageContentDeltaUnion.AsAny]
type anyMessageContentDelta interface {
implMessageContentDeltaUnion()
}

func (ImageFileDeltaBlock) implMessageContentDeltaUnion() {}
func (TextDeltaBlock) implMessageContentDeltaUnion() {}
func (RefusalDeltaBlock) implMessageContentDeltaUnion() {}
func (ImageURLDeltaBlock) implMessageContentDeltaUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := MessageContentDeltaUnion.AsAny().(type) {
Expand All @@ -1113,7 +1154,7 @@ type MessageContentDeltaUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u MessageContentDeltaUnion) AsAny() any {
func (u MessageContentDeltaUnion) AsAny() anyMessageContentDelta {
switch u.Type {
case "image_file":
return u.AsImageFile()
Expand Down
75 changes: 69 additions & 6 deletions betathreadrunstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ type CodeInterpreterToolCallCodeInterpreterOutputUnion struct {
} `json:"-"`
}

// anyCodeInterpreterToolCallCodeInterpreterOutput is implemented by each variant
// of [CodeInterpreterToolCallCodeInterpreterOutputUnion] to add type safety for
// the return type of [CodeInterpreterToolCallCodeInterpreterOutputUnion.AsAny]
type anyCodeInterpreterToolCallCodeInterpreterOutput interface {
implCodeInterpreterToolCallCodeInterpreterOutputUnion()
}

func (CodeInterpreterToolCallCodeInterpreterOutputLogs) implCodeInterpreterToolCallCodeInterpreterOutputUnion() {
}
func (CodeInterpreterToolCallCodeInterpreterOutputImage) implCodeInterpreterToolCallCodeInterpreterOutputUnion() {
}

// Use the following switch statement to find the correct variant
//
// switch variant := CodeInterpreterToolCallCodeInterpreterOutputUnion.AsAny().(type) {
Expand All @@ -240,7 +252,7 @@ type CodeInterpreterToolCallCodeInterpreterOutputUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u CodeInterpreterToolCallCodeInterpreterOutputUnion) AsAny() any {
func (u CodeInterpreterToolCallCodeInterpreterOutputUnion) AsAny() anyCodeInterpreterToolCallCodeInterpreterOutput {
switch u.Type {
case "logs":
return u.AsLogs()
Expand Down Expand Up @@ -405,6 +417,17 @@ type CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion struct {
} `json:"-"`
}

// anyCodeInterpreterToolCallDeltaCodeInterpreterOutput is implemented by each
// variant of [CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion] to add type
// safety for the return type of
// [CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion.AsAny]
type anyCodeInterpreterToolCallDeltaCodeInterpreterOutput interface {
implCodeInterpreterToolCallDeltaCodeInterpreterOutputUnion()
}

func (CodeInterpreterLogs) implCodeInterpreterToolCallDeltaCodeInterpreterOutputUnion() {}
func (CodeInterpreterOutputImage) implCodeInterpreterToolCallDeltaCodeInterpreterOutputUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion.AsAny().(type) {
Expand All @@ -413,7 +436,7 @@ type CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion) AsAny() any {
func (u CodeInterpreterToolCallDeltaCodeInterpreterOutputUnion) AsAny() anyCodeInterpreterToolCallDeltaCodeInterpreterOutput {
switch u.Type {
case "logs":
return u.AsLogs()
Expand Down Expand Up @@ -879,6 +902,16 @@ type RunStepStepDetailsUnion struct {
} `json:"-"`
}

// anyRunStepStepDetails is implemented by each variant of
// [RunStepStepDetailsUnion] to add type safety for the return type of
// [RunStepStepDetailsUnion.AsAny]
type anyRunStepStepDetails interface {
implRunStepStepDetailsUnion()
}

func (MessageCreationStepDetails) implRunStepStepDetailsUnion() {}
func (ToolCallsStepDetails) implRunStepStepDetailsUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := RunStepStepDetailsUnion.AsAny().(type) {
Expand All @@ -887,7 +920,7 @@ type RunStepStepDetailsUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u RunStepStepDetailsUnion) AsAny() any {
func (u RunStepStepDetailsUnion) AsAny() anyRunStepStepDetails {
switch u.Type {
case "message_creation":
return u.AsMessageCreation()
Expand Down Expand Up @@ -988,6 +1021,16 @@ type RunStepDeltaStepDetailsUnion struct {
} `json:"-"`
}

// anyRunStepDeltaStepDetails is implemented by each variant of
// [RunStepDeltaStepDetailsUnion] to add type safety for the return type of
// [RunStepDeltaStepDetailsUnion.AsAny]
type anyRunStepDeltaStepDetails interface {
implRunStepDeltaStepDetailsUnion()
}

func (RunStepDeltaMessageDelta) implRunStepDeltaStepDetailsUnion() {}
func (ToolCallDeltaObject) implRunStepDeltaStepDetailsUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := RunStepDeltaStepDetailsUnion.AsAny().(type) {
Expand All @@ -996,7 +1039,7 @@ type RunStepDeltaStepDetailsUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u RunStepDeltaStepDetailsUnion) AsAny() any {
func (u RunStepDeltaStepDetailsUnion) AsAny() anyRunStepDeltaStepDetails {
switch u.Type {
case "message_creation":
return u.AsMessageCreation()
Expand Down Expand Up @@ -1120,6 +1163,16 @@ type ToolCallUnion struct {
} `json:"-"`
}

// anyToolCall is implemented by each variant of [ToolCallUnion] to add type safety
// for the return type of [ToolCallUnion.AsAny]
type anyToolCall interface {
implToolCallUnion()
}

func (CodeInterpreterToolCall) implToolCallUnion() {}
func (FileSearchToolCall) implToolCallUnion() {}
func (FunctionToolCall) implToolCallUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := ToolCallUnion.AsAny().(type) {
Expand All @@ -1129,7 +1182,7 @@ type ToolCallUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u ToolCallUnion) AsAny() any {
func (u ToolCallUnion) AsAny() anyToolCall {
switch u.Type {
case "code_interpreter":
return u.AsCodeInterpreter()
Expand Down Expand Up @@ -1192,6 +1245,16 @@ type ToolCallDeltaUnion struct {
} `json:"-"`
}

// anyToolCallDelta is implemented by each variant of [ToolCallDeltaUnion] to add
// type safety for the return type of [ToolCallDeltaUnion.AsAny]
type anyToolCallDelta interface {
implToolCallDeltaUnion()
}

func (CodeInterpreterToolCallDelta) implToolCallDeltaUnion() {}
func (FileSearchToolCallDelta) implToolCallDeltaUnion() {}
func (FunctionToolCallDelta) implToolCallDeltaUnion() {}

// Use the following switch statement to find the correct variant
//
// switch variant := ToolCallDeltaUnion.AsAny().(type) {
Expand All @@ -1201,7 +1264,7 @@ type ToolCallDeltaUnion struct {
// default:
// fmt.Errorf("no variant present")
// }
func (u ToolCallDeltaUnion) AsAny() any {
func (u ToolCallDeltaUnion) AsAny() anyToolCallDelta {
switch u.Type {
case "code_interpreter":
return u.AsCodeInterpreter()
Expand Down
Loading