| 1 |
package openaiwire |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/clnkr-ai/clnkr" |
| 8 |
"github.com/clnkr-ai/clnkr/internal/providers/actwire" |
| 9 |
) |
| 10 |
|
| 11 |
func RequestSchema() map[string]any { |
| 12 |
return actwire.RequestSchema() |
| 13 |
} |
| 14 |
|
| 15 |
// UnattendedRequestSchema accepts act and done turns, but not clarify. |
| 16 |
func UnattendedRequestSchema() map[string]any { |
| 17 |
return actwire.UnattendedRequestSchema() |
| 18 |
} |
| 19 |
|
| 20 |
func FinalTurnSchema() map[string]any { |
| 21 |
return actwire.FinalTurnSchema() |
| 22 |
} |
| 23 |
|
| 24 |
func DoneOnlySchema() map[string]any { |
| 25 |
return actwire.DoneOnlySchema() |
| 26 |
} |
| 27 |
|
| 28 |
func NormalizeMessagesForProvider(messages []clnkr.Message) []clnkr.Message { |
| 29 |
return actwire.NormalizeMessagesForProvider(messages) |
| 30 |
} |
| 31 |
|
| 32 |
func ParseProviderTurn(raw string) (clnkr.Turn, error) { |
| 33 |
return actwire.ParseProviderTurn(raw) |
| 34 |
} |
| 35 |
|
| 36 |
func ParseChatCompletionTurn(raw string) (clnkr.Turn, error) { |
| 37 |
turn, err := actwire.ParseProviderTurn(raw) |
| 38 |
if err == nil { |
| 39 |
return turn, nil |
| 40 |
} |
| 41 |
if canonical, canonicalErr := clnkr.ParseTurn(raw); canonicalErr == nil { |
| 42 |
return canonical, nil |
| 43 |
} |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
func FormatErrorMessage(code json.RawMessage, message string) string { |
| 48 |
codeString := errorCodeText(code) |
| 49 |
switch { |
| 50 |
case codeString != "" && message != "": |
| 51 |
return codeString + ": " + message |
| 52 |
case codeString != "": |
| 53 |
return codeString |
| 54 |
case message != "": |
| 55 |
return message |
| 56 |
default: |
| 57 |
return "" |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func errorCodeText(code json.RawMessage) string { |
| 62 |
var text string |
| 63 |
if err := json.Unmarshal(code, &text); err == nil { |
| 64 |
return text |
| 65 |
} |
| 66 |
return "" |
| 67 |
} |
| 68 |
|
| 69 |
func IsContextLengthErrorText(message string) bool { |
| 70 |
normalized := strings.ToLower(message) |
| 71 |
if strings.Contains(normalized, "context_length_exceeded") { |
| 72 |
return true |
| 73 |
} |
| 74 |
if strings.Contains(normalized, "context window") && |
| 75 |
(strings.Contains(normalized, "exceed") || strings.Contains(normalized, "exceeds")) { |
| 76 |
return true |
| 77 |
} |
| 78 |
if strings.Contains(normalized, "maximum context length") { |
| 79 |
return true |
| 80 |
} |
| 81 |
return strings.Contains(normalized, "context length") && |
| 82 |
(strings.Contains(normalized, "exceed") || strings.Contains(normalized, "resulted in")) |
| 83 |
} |