From adfa6bd78389e01e2b5a6a6d71a718a2c9f10298 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Mon, 24 Nov 2025 18:30:46 +0300 Subject: Feat: tool model for or --- llm.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'llm.go') diff --git a/llm.go b/llm.go index 38b484f..55051bd 100644 --- a/llm.go +++ b/llm.go @@ -76,7 +76,6 @@ type OpenRouterChat struct { Model string } - func (lcp LlamaCPPeer) GetToken() string { return "" } @@ -484,16 +483,9 @@ func (or OpenRouterChat) GetToken() string { func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, error) { logger.Debug("formmsg open router completion", "link", cfg.CurrentAPI) - // Capture the image attachment path at the beginning to avoid race conditions // with API rotation that might clear the global variable localImageAttachmentPath := imageAttachmentPath - - if cfg.ToolUse && !resume { - // prompt += "\n" + cfg.ToolRole + ":\n" + toolSysMsg - // add to chat body - chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg}) - } if msg != "" { // otherwise let the bot continue var newMsg models.RoleMsg // Check if we have an image to add to this message @@ -536,7 +528,6 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro Model: chatBody.Model, Stream: chatBody.Stream, } - for i, msg := range chatBody.Messages { bodyCopy.Messages[i] = msg // Standardize role if it's a user role @@ -544,8 +535,10 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro bodyCopy.Messages[i].Role = "user" } } - orBody := models.NewOpenRouterChatReq(*bodyCopy, defaultLCPProps) + if cfg.ToolUse && !resume && role != cfg.ToolRole { + orBody.Tools = baseTools // set tools to use + } data, err := json.Marshal(orBody) if err != nil { logger.Error("failed to form a msg", "error", err) -- cgit v1.2.3 From fc963f86c94f8d96224414e409fda757f3b2d11d Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Mon, 24 Nov 2025 19:08:34 +0300 Subject: Feat: parse func call from or --- llm.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'llm.go') diff --git a/llm.go b/llm.go index 55051bd..47a1d46 100644 --- a/llm.go +++ b/llm.go @@ -468,6 +468,19 @@ func (or OpenRouterChat) ParseChunk(data []byte) (*models.TextChunk, error) { resp := &models.TextChunk{ Chunk: llmchunk.Choices[len(llmchunk.Choices)-1].Delta.Content, } + + // Handle tool calls similar to OpenAIer + if len(llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls) > 0 { + resp.ToolChunk = llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Arguments + fname := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Name + if fname != "" { + resp.FuncName = fname + } + } + if resp.ToolChunk != "" { + resp.ToolResp = true + } + if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" { if resp.Chunk != "" { logger.Error("text inside of finish llmchunk", "chunk", llmchunk) -- cgit v1.2.3 From 01da37b3971a6aa1c3c051c34666672b339ae2b6 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Tue, 25 Nov 2025 11:40:37 +0300 Subject: Fix: openrouter func ctx resp --- llm.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'llm.go') diff --git a/llm.go b/llm.go index 47a1d46..e7245ce 100644 --- a/llm.go +++ b/llm.go @@ -160,11 +160,14 @@ func (op OpenAIer) ParseChunk(data []byte) (*models.TextChunk, error) { Chunk: llmchunk.Choices[len(llmchunk.Choices)-1].Delta.Content, } if len(llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls) > 0 { - resp.ToolChunk = llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Arguments - fname := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Name + toolCall := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0] + resp.ToolChunk = toolCall.Function.Arguments + fname := toolCall.Function.Name if fname != "" { resp.FuncName = fname } + // Capture the tool call ID if available + resp.ToolID = toolCall.ID } if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" { if resp.Chunk != "" { @@ -471,11 +474,14 @@ func (or OpenRouterChat) ParseChunk(data []byte) (*models.TextChunk, error) { // Handle tool calls similar to OpenAIer if len(llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls) > 0 { - resp.ToolChunk = llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Arguments - fname := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Name + toolCall := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0] + resp.ToolChunk = toolCall.Function.Arguments + fname := toolCall.Function.Name if fname != "" { resp.FuncName = fname } + // Capture the tool call ID if available + resp.ToolID = toolCall.ID } if resp.ToolChunk != "" { resp.ToolResp = true -- cgit v1.2.3