Skip to content

Commit 29bc8a1

Browse files
committed
refactoring and consolidation
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
1 parent 1a1fce4 commit 29bc8a1

13 files changed

Lines changed: 159 additions & 158 deletions

File tree

core/http/endpoints/anthropic/messages.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ func handleAnthropicNonStream(c echo.Context, id string, input *schema.Anthropic
243243
if shouldUseFn && len(toolCalls) > 0 {
244244
stopReason = "tool_use"
245245
for _, tc := range toolCalls {
246-
var inputArgs map[string]interface{}
246+
var inputArgs map[string]any
247247
if err := json.Unmarshal([]byte(tc.Arguments), &inputArgs); err != nil {
248248
xlog.Warn("Failed to parse tool call arguments as JSON", "error", err, "args", tc.Arguments)
249-
inputArgs = map[string]interface{}{"raw": tc.Arguments}
249+
inputArgs = map[string]any{"raw": tc.Arguments}
250250
}
251251
contentBlocks = append(contentBlocks, schema.AnthropicContentBlock{
252252
Type: "tool_use",
@@ -269,9 +269,9 @@ func handleAnthropicNonStream(c echo.Context, id string, input *schema.Anthropic
269269
contentBlocks = append(contentBlocks, schema.AnthropicContentBlock{Type: "text", Text: stripped})
270270
}
271271
for i, fc := range parsed {
272-
var inputArgs map[string]interface{}
272+
var inputArgs map[string]any
273273
if err := json.Unmarshal([]byte(fc.Arguments), &inputArgs); err != nil {
274-
inputArgs = map[string]interface{}{"raw": fc.Arguments}
274+
inputArgs = map[string]any{"raw": fc.Arguments}
275275
}
276276
toolCallID := fc.ID
277277
if toolCallID == "" {
@@ -638,15 +638,15 @@ func convertAnthropicToOpenAIMessages(input *schema.AnthropicRequest) []schema.M
638638
case string:
639639
openAIMsg.StringContent = content
640640
openAIMsg.Content = content
641-
case []interface{}:
641+
case []any:
642642
// Handle array of content blocks
643643
var textContent string
644644
var stringImages []string
645645
var toolCalls []schema.ToolCall
646646
toolCallIndex := 0
647647

648648
for _, block := range content {
649-
if blockMap, ok := block.(map[string]interface{}); ok {
649+
if blockMap, ok := block.(map[string]any); ok {
650650
blockType, _ := blockMap["type"].(string)
651651
switch blockType {
652652
case "text":
@@ -655,7 +655,7 @@ func convertAnthropicToOpenAIMessages(input *schema.AnthropicRequest) []schema.M
655655
}
656656
case "image":
657657
// Handle image content
658-
if source, ok := blockMap["source"].(map[string]interface{}); ok {
658+
if source, ok := blockMap["source"].(map[string]any); ok {
659659
if sourceType, ok := source["type"].(string); ok && sourceType == "base64" {
660660
if data, ok := source["data"].(string); ok {
661661
mediaType, _ := source["media_type"].(string)
@@ -703,10 +703,10 @@ func convertAnthropicToOpenAIMessages(input *schema.AnthropicRequest) []schema.M
703703
switch rc := resultContent.(type) {
704704
case string:
705705
resultText = rc
706-
case []interface{}:
706+
case []any:
707707
// Array of content blocks
708708
for _, cb := range rc {
709-
if cbMap, ok := cb.(map[string]interface{}); ok {
709+
if cbMap, ok := cb.(map[string]any); ok {
710710
if cbMap["type"] == "text" {
711711
if text, ok := cbMap["text"].(string); ok {
712712
resultText += text
@@ -775,7 +775,7 @@ func convertAnthropicTools(input *schema.AnthropicRequest, cfg *config.ModelConf
775775
return nil, false
776776
}
777777
// "auto" is the default - let model decide
778-
case map[string]interface{}:
778+
case map[string]any:
779779
// Specific tool selection: {"type": "tool", "name": "tool_name"}
780780
if tcType, ok := tc["type"].(string); ok && tcType == "tool" {
781781
if name, ok := tc["name"].(string); ok {

core/http/endpoints/localai/agent_jobs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ func ExecuteTaskByNameEndpoint(app *application.Application) echo.HandlerFunc {
275275

276276
if c.Request().ContentLength > 0 {
277277
if err := c.Bind(&params); err != nil {
278-
body := make(map[string]interface{})
278+
body := make(map[string]any)
279279
if err := c.Bind(&body); err == nil {
280280
params = make(map[string]string)
281281
for k, v := range body {

core/http/endpoints/localai/import_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func ImportModelURIEndpoint(cl *config.ModelConfigLoader, appConfig *config.Appl
8585

8686
galleryService.ModelGalleryChannel <- galleryop.ManagementOp[gallery.GalleryModel, gallery.ModelConfig]{
8787
Req: gallery.GalleryModel{
88-
Overrides: map[string]interface{}{},
88+
Overrides: map[string]any{},
8989
},
9090
ID: uuid.String(),
9191
GalleryElementName: galleryID,

core/http/endpoints/localai/mcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type MCPReasoningEvent struct {
2323
type MCPToolCallEvent struct {
2424
Type string `json:"type"`
2525
Name string `json:"name"`
26-
Arguments map[string]interface{} `json:"arguments"`
26+
Arguments map[string]any `json:"arguments"`
2727
Reasoning string `json:"reasoning"`
2828
}
2929

core/http/endpoints/localai/welcome.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
4242
// Get model statuses to display in the UI the operation in progress
4343
processingModels, taskTypes := opcache.GetStatus()
4444

45-
summary := map[string]interface{}{
45+
summary := map[string]any{
4646
"Title": "LocalAI API - " + internal.PrintableVersion(),
4747
"Version": internal.PrintableVersion(),
4848
"BaseURL": middleware.BaseURL(c),

core/http/endpoints/mcp/tools.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,11 @@ func DiscoverMCPTools(ctx context.Context, sessions []NamedSession) ([]MCPToolIn
324324
Description: tool.Description,
325325
}
326326

327-
// Convert InputSchema to map[string]interface{} for functions.Function
327+
// Convert InputSchema to map[string]any for functions.Function
328328
if tool.InputSchema != nil {
329329
schemaBytes, err := json.Marshal(tool.InputSchema)
330330
if err == nil {
331-
var params map[string]interface{}
331+
var params map[string]any
332332
if err := json.Unmarshal(schemaBytes, &params); err == nil {
333333
f.Parameters = params
334334
} else {
@@ -337,9 +337,9 @@ func DiscoverMCPTools(ctx context.Context, sessions []NamedSession) ([]MCPToolIn
337337
}
338338
}
339339
if f.Parameters == nil {
340-
f.Parameters = map[string]interface{}{
340+
f.Parameters = map[string]any{
341341
"type": "object",
342-
"properties": map[string]interface{}{},
342+
"properties": map[string]any{},
343343
}
344344
}
345345

core/http/endpoints/openai/chat.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,9 @@ func ChatEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator
579579
noActionGrammar := functions.Function{
580580
Name: noActionName,
581581
Description: noActionDescription,
582-
Parameters: map[string]interface{}{
583-
"properties": map[string]interface{}{
584-
"message": map[string]interface{}{
582+
Parameters: map[string]any{
583+
"properties": map[string]any{
584+
"message": map[string]any{
585585
"type": "string",
586586
"description": "The message to reply the user with",
587587
}},
@@ -1042,7 +1042,7 @@ func ChatEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator
10421042
message := &schema.Message{
10431043
Role: "assistant",
10441044
Content: &textContentToReturn,
1045-
FunctionCall: map[string]interface{}{
1045+
FunctionCall: map[string]any{
10461046
"name": name,
10471047
"arguments": args,
10481048
},
@@ -1216,7 +1216,7 @@ func handleQuestion(config *config.ModelConfig, funcResults []functions.FuncCall
12161216
arg = funcResults[0].Arguments
12171217
}
12181218
// If there is a message that the LLM already sends as part of the JSON reply, use it
1219-
arguments := map[string]interface{}{}
1219+
arguments := map[string]any{}
12201220
if err := json.Unmarshal([]byte(arg), &arguments); err != nil {
12211221
xlog.Debug("handleQuestion: function result did not contain a valid JSON object")
12221222
}

core/http/endpoints/openresponses/responses.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ func ResponsesEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, eval
192192
noActionGrammar := functions.Function{
193193
Name: noActionName,
194194
Description: noActionDescription,
195-
Parameters: map[string]interface{}{
196-
"properties": map[string]interface{}{
197-
"message": map[string]interface{}{
195+
Parameters: map[string]any{
196+
"properties": map[string]any{
197+
"message": map[string]any{
198198
"type": "string",
199199
"description": "The message to reply the user with",
200200
},
@@ -292,17 +292,17 @@ func ResponsesEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, eval
292292
}
293293

294294
// convertORInputToMessages converts Open Responses input to internal Messages
295-
func convertORInputToMessages(input interface{}, cfg *config.ModelConfig) ([]schema.Message, error) {
295+
func convertORInputToMessages(input any, cfg *config.ModelConfig) ([]schema.Message, error) {
296296
var messages []schema.Message
297297

298298
switch v := input.(type) {
299299
case string:
300300
// Simple string = user message
301301
return []schema.Message{{Role: "user", StringContent: v}}, nil
302-
case []interface{}:
302+
case []any:
303303
// Array of items
304304
for _, itemRaw := range v {
305-
itemMap, ok := itemRaw.(map[string]interface{})
305+
itemMap, ok := itemRaw.(map[string]any)
306306
if !ok {
307307
continue
308308
}
@@ -378,14 +378,14 @@ func convertORInputToMessages(input interface{}, cfg *config.ModelConfig) ([]sch
378378
}
379379

380380
// convertORReasoningItemToMessage converts an Open Responses reasoning item to an assistant Message fragment (for merging).
381-
func convertORReasoningItemToMessage(itemMap map[string]interface{}) (schema.Message, error) {
381+
func convertORReasoningItemToMessage(itemMap map[string]any) (schema.Message, error) {
382382
var reasoning string
383383
if content := itemMap["content"]; content != nil {
384384
if s, ok := content.(string); ok {
385385
reasoning = s
386-
} else if parts, ok := content.([]interface{}); ok {
386+
} else if parts, ok := content.([]any); ok {
387387
for _, p := range parts {
388-
if partMap, ok := p.(map[string]interface{}); ok {
388+
if partMap, ok := p.(map[string]any); ok {
389389
if t, _ := partMap["type"].(string); (t == "output_text" || t == "input_text") && partMap["text"] != nil {
390390
if tStr, ok := partMap["text"].(string); ok {
391391
reasoning += tStr
@@ -399,7 +399,7 @@ func convertORReasoningItemToMessage(itemMap map[string]interface{}) (schema.Mes
399399
}
400400

401401
// convertORFunctionCallItemToMessage converts an Open Responses function_call item to an assistant Message fragment (for merging).
402-
func convertORFunctionCallItemToMessage(itemMap map[string]interface{}) (schema.Message, error) {
402+
func convertORFunctionCallItemToMessage(itemMap map[string]any) (schema.Message, error) {
403403
callID, _ := itemMap["call_id"].(string)
404404
name, _ := itemMap["name"].(string)
405405
arguments, _ := itemMap["arguments"].(string)
@@ -627,7 +627,7 @@ func flushAssistantAccumulator(out *[]schema.Message, acc **schema.Message) {
627627
}
628628

629629
// convertORMessageItem converts an Open Responses message item to internal Message
630-
func convertORMessageItem(itemMap map[string]interface{}, cfg *config.ModelConfig) (schema.Message, error) {
630+
func convertORMessageItem(itemMap map[string]any, cfg *config.ModelConfig) (schema.Message, error) {
631631
role, _ := itemMap["role"].(string)
632632
msg := schema.Message{Role: role}
633633

@@ -636,15 +636,15 @@ func convertORMessageItem(itemMap map[string]interface{}, cfg *config.ModelConfi
636636
case string:
637637
msg.StringContent = contentVal
638638
msg.Content = contentVal
639-
case []interface{}:
639+
case []any:
640640
// Array of content parts
641641
var textContent string
642642
var stringImages []string
643643
var stringVideos []string
644644
var stringAudios []string
645645

646646
for _, partRaw := range contentVal {
647-
partMap, ok := partRaw.(map[string]interface{})
647+
partMap, ok := partRaw.(map[string]any)
648648
if !ok {
649649
continue
650650
}
@@ -767,7 +767,7 @@ func convertORToolsToFunctions(input *schema.OpenResponsesRequest, cfg *config.M
767767
// "auto" is the default - let model decide whether to use tools
768768
// Tools are available but not forced
769769
}
770-
case map[string]interface{}:
770+
case map[string]any:
771771
if tcType, ok := tc["type"].(string); ok && tcType == "function" {
772772
if name, ok := tc["name"].(string); ok {
773773
cfg.SetFunctionCallString(name)
@@ -780,20 +780,20 @@ func convertORToolsToFunctions(input *schema.OpenResponsesRequest, cfg *config.M
780780
}
781781

782782
// convertTextFormatToResponseFormat converts Open Responses text_format to OpenAI response_format
783-
func convertTextFormatToResponseFormat(textFormat interface{}) interface{} {
783+
func convertTextFormatToResponseFormat(textFormat any) any {
784784
switch tf := textFormat.(type) {
785-
case map[string]interface{}:
785+
case map[string]any:
786786
if tfType, ok := tf["type"].(string); ok {
787787
if tfType == "json_schema" {
788-
return map[string]interface{}{
788+
return map[string]any{
789789
"type": "json_schema",
790790
"json_schema": tf,
791791
}
792792
}
793-
return map[string]interface{}{"type": tfType}
793+
return map[string]any{"type": tfType}
794794
}
795795
case string:
796-
return map[string]interface{}{"type": tf}
796+
return map[string]any{"type": tf}
797797
}
798798
return nil
799799
}
@@ -866,7 +866,7 @@ func handleBackgroundNonStream(ctx context.Context, store *ResponseStore, respon
866866
for i, fc := range funcCallResults {
867867
if fc.Name == noActionName {
868868
if fc.Arguments != "" {
869-
var args map[string]interface{}
869+
var args map[string]any
870870
if err := json.Unmarshal([]byte(fc.Arguments), &args); err == nil {
871871
if msg, ok := args["message"].(string); ok && msg != "" {
872872
textContent = msg
@@ -1391,7 +1391,7 @@ func handleOpenResponsesNonStream(c echo.Context, responseID string, createdAt i
13911391
// This is a text response, not a tool call
13921392
// Try to extract the message from the arguments
13931393
if fc.Arguments != "" {
1394-
var args map[string]interface{}
1394+
var args map[string]any
13951395
if err := json.Unmarshal([]byte(fc.Arguments), &args); err == nil {
13961396
if msg, ok := args["message"].(string); ok && msg != "" {
13971397
textContent = msg
@@ -2050,7 +2050,7 @@ func handleOpenResponsesStream(c echo.Context, responseID string, createdAt int6
20502050
if fc.Name == noActionName {
20512051
// This is a text response, not a tool call
20522052
if fc.Arguments != "" {
2053-
var args map[string]interface{}
2053+
var args map[string]any
20542054
if err := json.Unmarshal([]byte(fc.Arguments), &args); err == nil {
20552055
if msg, ok := args["message"].(string); ok && msg != "" {
20562056
textContent = msg
@@ -2806,7 +2806,7 @@ func buildORResponse(responseID string, createdAt int64, completedAt *int64, sta
28062806
}
28072807

28082808
// Default tool_choice: "auto" if tools are present, "none" otherwise
2809-
var toolChoice interface{}
2809+
var toolChoice any
28102810
if input.ToolChoice != nil {
28112811
toolChoice = input.ToolChoice
28122812
} else if len(tools) > 0 {
@@ -2899,14 +2899,14 @@ func buildORResponse(responseID string, createdAt int64, completedAt *int64, sta
28992899

29002900
// sendOpenResponsesError sends an error response
29012901
func sendOpenResponsesError(c echo.Context, statusCode int, errorType, message, param string) error {
2902-
errorResp := map[string]interface{}{
2903-
"error": map[string]interface{}{
2902+
errorResp := map[string]any{
2903+
"error": map[string]any{
29042904
"type": errorType,
29052905
"message": message,
29062906
},
29072907
}
29082908
if param != "" {
2909-
errorResp["error"].(map[string]interface{})["param"] = param
2909+
errorResp["error"].(map[string]any)["param"] = param
29102910
}
29112911
return c.JSON(statusCode, errorResp)
29122912
}
@@ -2937,8 +2937,8 @@ func convertORToolsToOpenAIFormat(orTools []schema.ORFunctionTool) []functions.T
29372937
// @Param stream query string false "Set to 'true' to resume streaming"
29382938
// @Param starting_after query int false "Sequence number to resume from (for streaming)"
29392939
// @Success 200 {object} schema.ORResponseResource "Response"
2940-
// @Failure 400 {object} map[string]interface{} "Bad Request"
2941-
// @Failure 404 {object} map[string]interface{} "Not Found"
2940+
// @Failure 400 {object} map[string]any "Bad Request"
2941+
// @Failure 404 {object} map[string]any "Not Found"
29422942
// @Router /v1/responses/{id} [get]
29432943
func GetResponseEndpoint() func(c echo.Context) error {
29442944
return func(c echo.Context) error {
@@ -3076,8 +3076,8 @@ func handleStreamResume(c echo.Context, store *ResponseStore, responseID string,
30763076
// @Description Cancel a background response if it's still in progress
30773077
// @Param id path string true "Response ID"
30783078
// @Success 200 {object} schema.ORResponseResource "Response"
3079-
// @Failure 400 {object} map[string]interface{} "Bad Request"
3080-
// @Failure 404 {object} map[string]interface{} "Not Found"
3079+
// @Failure 400 {object} map[string]any "Bad Request"
3080+
// @Failure 404 {object} map[string]any "Not Found"
30813081
// @Router /v1/responses/{id}/cancel [post]
30823082
func CancelResponseEndpoint() func(c echo.Context) error {
30833083
return func(c echo.Context) error {

0 commit comments

Comments
 (0)