72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package domain
|
|
|
|
import "fmt"
|
|
|
|
var protocolServiceTypes = map[string][]ServiceType{
|
|
"openai": {ServiceChat},
|
|
"replicate": {ServiceImageGeneration},
|
|
}
|
|
|
|
var serviceTypeProtocol = map[ServiceType]string{
|
|
ServiceChat: "openai",
|
|
ServiceImageGeneration: "replicate",
|
|
}
|
|
|
|
func DefaultProtocol(serviceType ServiceType) string {
|
|
if p, ok := serviceTypeProtocol[serviceType]; ok {
|
|
return p
|
|
}
|
|
return "openai"
|
|
}
|
|
|
|
func DefaultServiceType(protocol string) ServiceType {
|
|
types := ServiceTypesForProtocol(protocol)
|
|
if len(types) == 0 {
|
|
return ServiceChat
|
|
}
|
|
return types[0]
|
|
}
|
|
|
|
func ServiceTypesForProtocol(protocol string) []ServiceType {
|
|
return append([]ServiceType(nil), protocolServiceTypes[protocol]...)
|
|
}
|
|
|
|
func ProtocolForServiceType(serviceType ServiceType) string {
|
|
return DefaultProtocol(serviceType)
|
|
}
|
|
|
|
func ValidateProviderBinding(protocol, serviceType string) error {
|
|
types := ServiceTypesForProtocol(protocol)
|
|
if len(types) == 0 {
|
|
return fmt.Errorf("unsupported protocol: %s", protocol)
|
|
}
|
|
st := ServiceType(serviceType)
|
|
for _, allowed := range types {
|
|
if allowed == st {
|
|
return nil
|
|
}
|
|
}
|
|
// Allow legacy provider rows until migrated.
|
|
if protocol == "replicate" && (st == ServiceTextToImage || st == ServiceImageToImage) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("protocol %s does not support service_type %s", protocol, serviceType)
|
|
}
|
|
|
|
func ImageProviderServiceTypes() []string {
|
|
return []string{
|
|
string(ServiceImageGeneration),
|
|
string(ServiceTextToImage),
|
|
string(ServiceImageToImage),
|
|
}
|
|
}
|
|
|
|
func IsImageProviderServiceType(serviceType string) bool {
|
|
switch ServiceType(serviceType) {
|
|
case ServiceImageGeneration, ServiceTextToImage, ServiceImageToImage:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|