Skip to content

Commit 2d07420

Browse files
committed
chore: add a way to track reasoning only
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 23c7064 commit 2d07420

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

options.go

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ import (
1010
// Options contains all configuration options for the Cogito agent
1111
// It allows customization of behavior, tools, prompts, and execution parameters
1212
type Options struct {
13-
prompts prompt.PromptMap
14-
maxIterations int
15-
tools Tools
16-
deepContext bool
17-
toolReasoner bool
18-
toolReEvaluator bool
19-
autoPlan bool
20-
planReEvaluator bool
21-
statusCallback func(string)
22-
gaps []string
23-
context context.Context
24-
infiniteExecution bool
25-
maxAttempts int
26-
feedbackCallback func() *Fragment
27-
toolCallCallback func(*ToolChoice) bool
28-
toolCallResultCallback func(ToolStatus)
29-
strictGuidelines bool
30-
mcpSessions []*mcp.ClientSession
31-
guidelines Guidelines
32-
mcpPrompts bool
33-
mcpArgs map[string]string
34-
maxRetries int
35-
loopDetectionSteps int
36-
forceReasoning bool
13+
prompts prompt.PromptMap
14+
maxIterations int
15+
tools Tools
16+
deepContext bool
17+
toolReasoner bool
18+
toolReEvaluator bool
19+
autoPlan bool
20+
planReEvaluator bool
21+
statusCallback, reasoningCallback func(string)
22+
gaps []string
23+
context context.Context
24+
infiniteExecution bool
25+
maxAttempts int
26+
feedbackCallback func() *Fragment
27+
toolCallCallback func(*ToolChoice) bool
28+
toolCallResultCallback func(ToolStatus)
29+
strictGuidelines bool
30+
mcpSessions []*mcp.ClientSession
31+
guidelines Guidelines
32+
mcpPrompts bool
33+
mcpArgs map[string]string
34+
maxRetries int
35+
loopDetectionSteps int
36+
forceReasoning bool
3737
}
3838

3939
type Option func(*Options)
@@ -221,3 +221,10 @@ func WithForceReasoning() func(o *Options) {
221221
o.forceReasoning = true
222222
}
223223
}
224+
225+
// WithReasoningCallback sets a callback function to receive reasoning updates during execution
226+
func WithReasoningCallback(fn func(string)) func(o *Options) {
227+
return func(o *Options) {
228+
o.reasoningCallback = fn
229+
}
230+
}

tools.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,18 @@ func toolSelection(llm LLM, f Fragment, tools Tools, guidelines Guidelines, tool
632632
// No tool was selected, reasoning contains the response
633633
xlog.Debug("[toolSelection] No tool selected", "reasoning", reasoning)
634634
o.statusCallback(reasoning)
635+
o.reasoningCallback("No tool selected")
635636
// TODO: reasoning in this case would be the LLM's response to the user, not the tool selection
636637
// But, ExecuteTools doesn't return ther response, but just executes the tools and returns the result of the tools.
637638
// In this way, we are wasting computation as the user will ask again the LLM for computing the response
638639
// (again, while we could have used the reasoning as it is actually a response if no tools were selected)
639640
return f, nil, true, nil
640641
}
641642

643+
if reasoning != "" {
644+
o.reasoningCallback(reasoning)
645+
}
646+
642647
xlog.Debug("[toolSelection] Tool selected", "tool", selectedTool.Name, "reasoning", reasoning)
643648
o.statusCallback(fmt.Sprintf("Selected tool: %s", selectedTool.Name))
644649

@@ -701,22 +706,21 @@ func ExecuteTools(llm LLM, f Fragment, opts ...Option) (Fragment, error) {
701706
// If the tool reasoner is enabled, we first try to figure out if we need to call a tool or not
702707
// We ask to the LLM, and then we extract a boolean from the answer
703708
if o.toolReasoner {
704-
705709
// ToolReasoner will call guidelines and tools for the initial fragment
706710
toolReason, err := ToolReasoner(llm, f, opts...)
707711
if err != nil {
708712
return f, fmt.Errorf("failed to extract boolean: %w", err)
709713
}
710714

711-
o.statusCallback(f.LastMessage().Content)
712-
713715
boolean, err := ExtractBoolean(llm, toolReason, opts...)
714716
if err != nil {
715717
return f, fmt.Errorf("failed extracting boolean: %w", err)
716718
}
717719
xlog.Debug("Tool reasoning", "wants_tool", boolean.Boolean)
718720
if !boolean.Boolean {
719721
xlog.Debug("LLM decided to not use any tool")
722+
o.statusCallback("Ended reasoning without using any tool")
723+
o.reasoningCallback("Ended reasoning without using any tool")
720724
return f, ErrNoToolSelected
721725
}
722726
}

0 commit comments

Comments
 (0)