.gitignore 🔗
@@ -49,3 +49,4 @@ Thumbs.db
manpages/
completions/
!internal/tui/components/completions/
+.prettierignore
kujtimiihoxha created
.gitignore | 1
go.mod | 2
go.sum | 6 +++
internal/agent/agent.go | 11 +++--
internal/agent/agent_tool.go | 12 ++++-
internal/agent/common_test.go | 19 ---------
internal/agent/coordinator.go | 2 -
internal/agent/prompt/prompt.go | 10 ++--
internal/agent/prompts.go | 4 +-
internal/agent/templates/agent_tool.md | 7 +--
internal/agent/templates/coder.md.tpl | 20 ++++++----
internal/agent/templates/initialize.md | 2
internal/agent/templates/task.md.tpl | 0
internal/agent/templates/title.md | 2 +
internal/agent/tools/bash.go | 2
internal/agent/tools/bash.tpl | 2
internal/agent/tools/edit.md | 11 ++---
internal/agent/tools/glob.md | 6 +--
internal/agent/tools/grep.md | 11 ++---
internal/agent/tools/ls.md | 3 -
internal/agent/tools/multiedit.md | 9 +---
internal/agent/tools/sourcegraph.md | 6 +--
internal/agent/tools/tools.go | 13 ++++++
internal/agent/tools/view.md | 3 -
internal/agent/tools/write.md | 3 -
internal/session/session.go | 27 ++++++++++++++
internal/tui/components/chat/chat.go | 12 +++++-
internal/tui/components/chat/sidebar/sidebar.go | 14 +++---
internal/tui/components/dialogs/commands/commands.go | 2
internal/tui/page/chat/chat.go | 4 -
30 files changed, 127 insertions(+), 99 deletions(-)
@@ -49,3 +49,4 @@ Thumbs.db
manpages/
completions/
!internal/tui/components/completions/
+.prettierignore
@@ -68,7 +68,7 @@ require (
require (
github.com/anthropics/anthropic-sdk-go v1.12.0 // indirect
- github.com/charmbracelet/fantasy v0.0.0-20251002051643-c96822199d77
+ github.com/charmbracelet/fantasy v0.0.0-20251003071236-5d39f0348e5d
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
)
@@ -53,6 +53,12 @@ github.com/charmbracelet/fang v0.4.2 h1:nWr7Tb82/TTNNGMGG35aTZ1X68loAOQmpb0qxkKX
github.com/charmbracelet/fang v0.4.2/go.mod h1:wHJKQYO5ReYsxx+yZl+skDtrlKO/4LLEQ6EXsdHhRhg=
github.com/charmbracelet/fantasy v0.0.0-20251002051643-c96822199d77 h1:YHuUqaojkeu00YtQeXPqM/1RNJH/jqGNaQYFwa7JQTk=
github.com/charmbracelet/fantasy v0.0.0-20251002051643-c96822199d77/go.mod h1:RZotHpq44tKZDe6Vf0kk1iDqnUgH7Scx+K/7uJ9Qwnw=
+github.com/charmbracelet/fantasy v0.0.0-20251003054629-33bf06cef92c h1:+gCA3Sv7g1jnZ96Em7j9u61H2O/1SkBAZ1LM9yd2bM8=
+github.com/charmbracelet/fantasy v0.0.0-20251003054629-33bf06cef92c/go.mod h1:RZotHpq44tKZDe6Vf0kk1iDqnUgH7Scx+K/7uJ9Qwnw=
+github.com/charmbracelet/fantasy v0.0.0-20251003055851-3196e9fa7380 h1:UYWO3cutUTV9ZLOH4hrXFy9hg4E1pVbLZsZLA1OA3+g=
+github.com/charmbracelet/fantasy v0.0.0-20251003055851-3196e9fa7380/go.mod h1:RZotHpq44tKZDe6Vf0kk1iDqnUgH7Scx+K/7uJ9Qwnw=
+github.com/charmbracelet/fantasy v0.0.0-20251003071236-5d39f0348e5d h1:HS9qu7CgQGPnPcNoHIZXGWGFS8SaxaBXYytLdakm+jY=
+github.com/charmbracelet/fantasy v0.0.0-20251003071236-5d39f0348e5d/go.mod h1:RZotHpq44tKZDe6Vf0kk1iDqnUgH7Scx+K/7uJ9Qwnw=
github.com/charmbracelet/glamour/v2 v2.0.0-20250811143442-a27abb32f018 h1:PU4Zvpagsk5sgaDxn5W4sxHuLp9QRMBZB3bFSk40A4w=
github.com/charmbracelet/glamour/v2 v2.0.0-20250811143442-a27abb32f018/go.mod h1:Z/GLmp9fzaqX4ze3nXG7StgWez5uBM5XtlLHK8V/qSk=
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.3.0.20250917201909-41ff0bf215ea h1:g1HfUgSMvye8mgecMD1mPscpt+pzJoDEiSA+p2QXzdQ=
@@ -175,7 +175,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*ai.Agen
TopK: call.TopK,
FrequencyPenalty: call.FrequencyPenalty,
// Before each step create the new assistant message
- PrepareStep: func(options ai.PrepareStepFunctionOptions) (prepared ai.PrepareStepResult, err error) {
+ PrepareStep: func(callContext context.Context, options ai.PrepareStepFunctionOptions) (_ context.Context, prepared ai.PrepareStepResult, err error) {
var assistantMsg message.Message
assistantMsg, err = a.messages.Create(genCtx, call.SessionID, message.CreateMessageParams{
Role: message.Assistant,
@@ -184,9 +184,11 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*ai.Agen
Provider: a.largeModel.ModelCfg.Provider,
})
if err != nil {
- return prepared, err
+ return callContext, prepared, err
}
+ callContext = context.WithValue(ctx, tools.MessageIDContextKey, assistantMsg.ID)
+
currentAssistant = &assistantMsg
prepared.Messages = options.Messages
@@ -200,7 +202,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*ai.Agen
for _, queued := range queuedCalls {
userMessage, createErr := a.createUserMessage(genCtx, queued)
if createErr != nil {
- return prepared, createErr
+ return callContext, prepared, createErr
}
prepared.Messages = append(prepared.Messages, userMessage.ToAIMessage()...)
}
@@ -220,7 +222,7 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*ai.Agen
prepared.Messages[i].ProviderOptions = a.getCacheControlOptions()
}
}
- return prepared, err
+ return callContext, prepared, err
},
OnReasoningDelta: func(id string, text string) error {
currentAssistant.AppendReasoningContent(text)
@@ -307,7 +309,6 @@ func (a *sessionAgent) Run(ctx context.Context, call SessionAgentCall) (*ai.Agen
case ai.FinishReasonToolCalls:
finishReason = message.FinishReasonToolUse
}
- slog.Info("OnStepFinish", "reason", stepResult.FinishReason)
currentAssistant.AddFinish(finishReason, "", "")
a.updateSessionUsage(a.largeModel, ¤tSession, stepResult.Usage)
sessionLock.Lock()
@@ -14,7 +14,7 @@ import (
"github.com/charmbracelet/crush/internal/config"
)
-//go:embed templates/agentTool.md
+//go:embed templates/agent_tool.md
var agentToolDescription []byte
type AgentParams struct {
@@ -55,7 +55,13 @@ func (c *coordinator) agentTool() (ai.AgentTool, error) {
return ai.ToolResponse{}, errors.New("session id missing from context")
}
- session, err := c.sessions.CreateTaskSession(ctx, call.ID, sessionID, "New Agent Session")
+ agentMessageID := tools.GetMessageFromContext(ctx)
+ if agentMessageID == "" {
+ return ai.ToolResponse{}, errors.New("agent message id missing from context")
+ }
+
+ agentToolSessionID := c.sessions.CreateAgentToolSessionID(agentMessageID, call.ID)
+ session, err := c.sessions.CreateTaskSession(ctx, agentToolSessionID, sessionID, "New Agent Session")
if err != nil {
return ai.ToolResponse{}, fmt.Errorf("error creating session: %s", err)
}
@@ -65,7 +71,7 @@ func (c *coordinator) agentTool() (ai.AgentTool, error) {
maxTokens = model.ModelCfg.MaxTokens
}
result, err := agent.Run(ctx, SessionAgentCall{
- SessionID: sessionID,
+ SessionID: session.ID,
Prompt: params.Prompt,
MaxOutputTokens: maxTokens,
ProviderOptions: c.getProviderOptions(model),
@@ -1,7 +1,6 @@
package agent
import (
- "fmt"
"net/http"
"os"
"path/filepath"
@@ -69,18 +68,9 @@ func openaiBuilder(model string) builderFunc {
func openRouterBuilder(model string) builderFunc {
return func(t *testing.T, r *recorder.Recorder) (ai.LanguageModel, error) {
- tf := func() func() string {
- id := 0
- return func() string {
- id += 1
- return fmt.Sprintf("%s-%d", t.Name(), id)
- }
- }
provider := openrouter.New(
openrouter.WithAPIKey(os.Getenv("CRUSH_OPENROUTER_API_KEY")),
openrouter.WithHTTPClient(&http.Client{Transport: r}),
- openrouter.WithLanguageUniqueToolCallIds(),
- openrouter.WithLanguageModelGenerateIDFunc(tf()),
)
return provider.LanguageModel(model)
}
@@ -88,19 +78,10 @@ func openRouterBuilder(model string) builderFunc {
func zAIBuilder(model string) builderFunc {
return func(t *testing.T, r *recorder.Recorder) (ai.LanguageModel, error) {
- tf := func() func() string {
- id := 0
- return func() string {
- id += 1
- return fmt.Sprintf("%s-%d", t.Name(), id)
- }
- }
provider := openaicompat.New(
"https://api.z.ai/api/coding/paas/v4",
openaicompat.WithAPIKey(os.Getenv("CRUSH_ZAI_API_KEY")),
openaicompat.WithHTTPClient(&http.Client{Transport: r}),
- openaicompat.WithLanguageUniqueToolCallIds(),
- openaicompat.WithLanguageModelGenerateIDFunc(tf()),
)
return provider.LanguageModel(model)
}
@@ -356,7 +356,6 @@ func (c *coordinator) buildOpenaiProvider(baseURL, apiKey string, headers map[st
func (c *coordinator) buildOpenrouterProvider(_, apiKey string, headers map[string]string) ai.Provider {
opts := []openrouter.Option{
openrouter.WithAPIKey(apiKey),
- openrouter.WithLanguageUniqueToolCallIds(),
}
if c.cfg.Options.Debug {
httpClient := log.NewHTTPClient()
@@ -371,7 +370,6 @@ func (c *coordinator) buildOpenrouterProvider(_, apiKey string, headers map[stri
func (c *coordinator) buildOpenaiCompatProvider(baseURL, apiKey string, headers map[string]string) ai.Provider {
opts := []openaicompat.Option{
openaicompat.WithAPIKey(apiKey),
- openaicompat.WithLanguageUniqueToolCallIds(),
}
if c.cfg.Options.Debug {
httpClient := log.NewHTTPClient()
@@ -15,11 +15,11 @@ import (
// Prompt represents a template-based prompt generator.
type Prompt struct {
- name string
- template string
- now func() time.Time
- platform string
- workingDir string
+ name string
+ template string
+ now func() time.Time
+ platform string
+ workingDir string
}
type PromptDat struct {
@@ -6,10 +6,10 @@ import (
"github.com/charmbracelet/crush/internal/agent/prompt"
)
-//go:embed templates/coder.gotmpl
+//go:embed templates/coder.md.tpl
var coderPromptTmpl []byte
-//go:embed templates/task.gotmpl
+//go:embed templates/task.md.tpl
var taskPromptTmpl []byte
//go:embed templates/initialize.md
@@ -1,16 +1,15 @@
Launch a new agent that has access to the following tools: GlobTool, GrepTool, LS, View. When you are searching for a keyword or file and are not confident that you will find the right match on the first try, use the Agent tool to perform the search for you.
-<usage>
+<usage>
- If you are searching for a keyword like "config" or "logger", or for questions like "which file does X?", the Agent tool is strongly recommended
- If you want to read a specific file path, use the View or GlobTool tool instead of the Agent tool, to find the match more quickly
- If you are searching for a specific class definition like "class Foo", use the GlobTool tool instead, to find the match more quickly
- </usage>
+</usage>
<usage_notes>
-
1. Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
2. When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.
3. Each agent invocation is stateless. You will not be able to send additional messages to the agent, nor will the agent be able to communicate with you outside of its final report. Therefore, your prompt should contain a highly detailed task description for the agent to perform autonomously and you should specify exactly what information the agent should return back to you in its final and only message to you.
4. The agent's outputs should generally be trusted
5. IMPORTANT: The agent can not use Bash, Replace, Edit, so can not modify files. If you want to use these tools, use them directly instead of going through the agent.
- </usage_notes>
+</usage_notes>
@@ -2,18 +2,19 @@ You are Crush, a powerful AI Assistant that runs in the CLI.
Use the instructions below and the tools available to you to assist the user.
<memory_instructions>
-If the current working directory contains a file called CRUSH.md, it will be automatically added to your context.
+If the current working directory contains a file used for memory, they will be automatically added to your context.
-This file serves multiple purposes:
+These file serves multiple purposes:
- Storing frequently used bash commands (build, test, lint, etc.) so you can use them without searching each time
- Recording the user's code style preferences (naming conventions, preferred libraries, etc.)
- Maintaining useful information about the codebase structure and organization
-When you discover important information that could be useful for the future update/add the info to the CRUSH.md.
+When you discover important information that could be useful for the future update/add the info the appropriate memory file.
-Memory might be added to you during a task if there are nested memory files that relate to the work you are doing.
+Make sure to follow the memory files instructions while working.
</memory_instructions>
+
<communication_style>
- Be concise and direct
- Keep responses under 4 lines unless details requested
@@ -65,6 +66,7 @@ assistant: src/foo.c
user: write tests for new feature
assistant: [uses grep and glob search tools to find where similar tests are defined, uses concurrent read file tool use blocks in one tool call to read relevant files at the same time, uses edit file tool to write new tests]
</example>
+
</communication_style>
<proactiveness>
@@ -86,15 +88,17 @@ When making changes to files, first understand the file's code conventions. Mimi
</following_conversations>
<code_style>
-- Follow existing code style and patterns
-- Do not add any comments to code you write unless asked to do so
+- Follow existing code style and patterns.
+- Do not add any comments to code you write unless asked to do so.
+- Thrive to write only code that is necessary to solve the given issue (less code is always better).
+- Follow best practices for the language and framework used in the project.
</code_style>
<doing_tasks>
The user will primarily request you perform software engineering tasks. This includes solving bugs, adding new functionality, refactoring code, explaining code, and more. For these tasks the following steps are recommended:
- Use the available search tools to understand the codebase and the user's query.
-- Plan out the implementation
+- Plan out the implementation (create a todo list)
- Implement the solution using all tools available to you
- Verify the solution if possible with tests. NEVER assume specific test framework or test script. Check the README or search codebase to determine the testing approach.
- When you have completed a task, you MUST run the lint and typecheck commands (eg. npm run lint, npm run typecheck, ruff, etc.) if they were provided to you to ensure your code is correct. If you are unable to find the correct command, ask the user for the command to run and if they supply it, proactively suggest writing it to CRUSH.md so that you will know to run it next time.
@@ -103,7 +107,7 @@ NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTAN
</doing_tasks>
<tool_use>
-- When doing file search, prefer to use the Agent tool in order to reduce context usage.
+- When doing file search, prefer to use the Agent tool, give the agent detailed instructions on what to search for and response format details.
- All tools are executed in parallel when multiple tool calls are sent in a single message. Only send multiple tool calls when they are safe to run in parallel (no dependencies between them).
- The user does not see the full output of the tool responses, so if you need the output of the tool for the response make sure to summarize it for the user.
</tool_use>
@@ -1,4 +1,4 @@
-`Please analyze this codebase and create a **CRUSH.md** file containing:
+Please analyze this codebase and create a **CRUSH.md** file containing:
- Build/lint/test commands - especially for running a single test
- Code style guidelines including imports, formatting, types, naming conventions, error handling, etc.
@@ -1,8 +1,10 @@
you will generate a short title based on the first message a user begins a conversation with
+<rules>
- ensure it is not more than 50 characters long
- the title should be a summary of the user's message
- it should be one line long
- do not use quotes or colons
- the entire text you return will be used as the title
- never return anything that is more than one sentence (one line) long
+</rules>
@@ -44,7 +44,7 @@ const (
BashNoOutput = "no output"
)
-//go:embed bash.gotmpl
+//go:embed bash.tpl
var bashDescriptionTmpl []byte
var bashDescriptionTpl = template.Must(
@@ -108,7 +108,7 @@ Important:
- Return empty response - user sees gh output
- Never update git config
- </pull_requests>
+</pull_requests>
<examples>
Good: pytest /foo/bar/tests
@@ -13,10 +13,9 @@ Edits files by replacing text, creating new files, or deleting content. For movi
</parameters>
<special_cases>
-
- Create file: provide file_path + new_string, leave old_string empty
- Delete content: provide file_path + old_string, leave new_string empty
- </special_cases>
+</special_cases>
<critical_requirements>
UNIQUENESS (when replace_all=false): old_string MUST uniquely identify target instance
@@ -34,7 +33,7 @@ VERIFICATION: Before using
- Check how many instances of target text exist
- Gather sufficient context for unique identification
- Plan separate calls or use replace_all
- </critical_requirements>
+</critical_requirements>
<warnings>
Tool fails if:
@@ -44,17 +43,15 @@ Tool fails if:
</warnings>
<best_practices>
-
- Ensure edits result in correct, idiomatic code
- Don't leave code in broken state
- Use absolute file paths (starting with /)
- Use forward slashes (/) for cross-platform compatibility
- Multiple edits to same file: send all in single message with multiple tool calls
- </best_practices>
+</best_practices>
<windows_notes>
-
- Forward slashes work throughout (C:/path/file)
- File permissions handled automatically
- Line endings converted automatically (\n ↔ \r\n)
- </windows_notes>
+</windows_notes>
@@ -7,13 +7,12 @@ Fast file pattern matching tool that finds files by name/pattern, returning path
</usage>
<pattern_syntax>
-
- '\*' matches any sequence of non-separator characters
- '\*\*' matches any sequence including separators
- '?' matches any single non-separator character
- '[...]' matches any character in brackets
- '[!...]' matches any character not in brackets
- </pattern_syntax>
+</pattern_syntax>
<examples>
- '*.js' - JavaScript files in current directory
@@ -29,11 +28,10 @@ Fast file pattern matching tool that finds files by name/pattern, returning path
</limitations>
<cross_platform>
-
- Path separators handled automatically (/ and \ work)
- Uses ripgrep (rg) if available, otherwise Go implementation
- Patterns should use forward slashes (/) for compatibility
- </cross_platform>
+</cross_platform>
<tips>
- Combine with Grep: find files with Glob, search contents with Grep
@@ -14,14 +14,13 @@ When literal_text=false (supports standard regex):
- 'function' searches for literal text "function"
- 'log\..\*Error' finds text starting with "log." and ending with "Error"
- 'import\s+.\*\s+from' finds import statements in JavaScript/TypeScript
- </regex_syntax>
+</regex_syntax>
<include_patterns>
-
- '\*.js' - Only search JavaScript files
- '\*.{ts,tsx}' - Only search TypeScript files
- '\*.go' - Only search Go files
- </include_patterns>
+</include_patterns>
<limitations>
- Results limited to 100 files (newest first)
@@ -31,18 +30,16 @@ When literal_text=false (supports standard regex):
</limitations>
<ignore_support>
-
- Respects .gitignore patterns to skip ignored files/directories
- Respects .crushignore patterns for additional ignore rules
- Both ignore files auto-detected in search root directory
- </ignore_support>
+</ignore_support>
<cross_platform>
-
- Uses ripgrep (rg) if available for better performance
- Falls back to Go implementation if ripgrep unavailable
- File paths normalized automatically for compatibility
- </cross_platform>
+</cross_platform>
<tips>
- For faster searches: use Glob to find relevant files first, then Grep
@@ -21,12 +21,11 @@ Shows files and subdirectories in tree structure for exploring project organizat
</limitations>
<cross_platform>
-
- Hidden file detection uses Unix convention (files starting with '.')
- Windows hidden files (with hidden attribute) not auto-skipped
- Common Windows directories (System32, Program Files) not in default ignore
- Path separators handled automatically (/ and \ work)
- </cross_platform>
+</cross_platform>
<tips>
- Use Glob for finding files by name patterns instead of browsing
@@ -21,11 +21,10 @@ Makes multiple edits to a single file in one operation. Built on Edit tool for e
</operation>
<critical_requirements>
-
1. All edits follow same requirements as single Edit tool
2. Edits are atomic - either all succeed or none applied
3. Plan edits carefully to avoid conflicts between sequential operations
- </critical_requirements>
+</critical_requirements>
<warnings>
- Tool fails if old_string doesn't match file contents exactly (including whitespace)
@@ -34,17 +33,15 @@ Makes multiple edits to a single file in one operation. Built on Edit tool for e
</warnings>
<best_practices>
-
- Ensure all edits result in correct, idiomatic code
- Don't leave code in broken state
- Use absolute file paths (starting with /)
- Use replace_all for renaming variables across file
- Avoid adding emojis unless user explicitly requests
- </best_practices>
+</best_practices>
<new_file_creation>
-
- Provide new file path (including directory if needed)
- First edit: empty old_string, new file contents as new_string
- Subsequent edits: normal edit operations on created content
- </new_file_creation>
+</new_file_creation>
@@ -7,7 +7,6 @@ Search code across public repositories using Sourcegraph's GraphQL API.
</usage>
<basic_syntax>
-
- "fmt.Println" - exact matches
- "file:.go fmt.Println" - limit to Go files
- "repo:^github\.com/golang/go$ fmt.Println" - specific repos
@@ -16,7 +15,7 @@ Search code across public repositories using Sourcegraph's GraphQL API.
- "fmt\.(Print|Printf|Println)" - regex patterns
- "\"exact phrase\"" - exact phrase matching
- "-file:test" or "-repo:forks" - exclude matches
- </basic_syntax>
+</basic_syntax>
<key_filters>
Repository: repo:name, repo:^exact$, repo:org/repo@branch, -repo:exclude, fork:yes, archived:yes, visibility:public
@@ -35,12 +34,11 @@ Result: select:repo, select:file, select:content, count:100, timeout:30s
</examples>
<boolean_operators>
-
- "term1 AND term2" - both terms
- "term1 OR term2" - either term
- "term1 NOT term2" - term1 but not term2
- "term1 and (term2 or term3)" - grouping with parentheses
- </boolean_operators>
+</boolean_operators>
<limitations>
- Only searches public repositories
@@ -11,6 +11,7 @@ type (
const (
SessionIDContextKey sessionIDContextKey = "session_id"
+ MessageIDContextKey messageIDContextKey = "message_id"
)
func GetSessionFromContext(ctx context.Context) string {
@@ -24,3 +25,15 @@ func GetSessionFromContext(ctx context.Context) string {
}
return s
}
+
+func GetMessageFromContext(ctx context.Context) string {
+ messageID := ctx.Value(MessageIDContextKey)
+ if messageID == nil {
+ return ""
+ }
+ s, ok := messageID.(string)
+ if !ok {
+ return ""
+ }
+ return s
+}
@@ -23,11 +23,10 @@ Reads and displays file contents with line numbers for examining code, logs, or
</limitations>
<cross_platform>
-
- Handles Windows (CRLF) and Unix (LF) line endings
- Works with forward slashes (/) and backslashes (\)
- Auto-detects text encoding for common formats
- </cross_platform>
+</cross_platform>
<tips>
- Use with Glob to find files first
@@ -19,9 +19,8 @@ Creates or updates files in filesystem for saving/modifying text content.
</limitations>
<cross_platform>
-
- Use forward slashes (/) for compatibility
- </cross_platform>
+</cross_platform>
<tips>
- Use View tool first to examine existing files before modifying
@@ -3,6 +3,8 @@ package session
import (
"context"
"database/sql"
+ "fmt"
+ "strings"
"github.com/charmbracelet/crush/internal/db"
"github.com/charmbracelet/crush/internal/event"
@@ -32,6 +34,11 @@ type Service interface {
List(ctx context.Context) ([]Session, error)
Save(ctx context.Context, session Session) (Session, error)
Delete(ctx context.Context, id string) error
+
+ // Agent tool session management
+ CreateAgentToolSessionID(messageID, toolCallID string) string
+ ParseAgentToolSessionID(sessionID string) (messageID string, toolCallID string, ok bool)
+ IsAgentToolSession(sessionID string) bool
}
type service struct {
@@ -157,3 +164,23 @@ func NewService(q db.Querier) Service {
q,
}
}
+
+// CreateAgentToolSessionID creates a session ID for agent tool sessions using the format "messageID$$toolCallID"
+func (s *service) CreateAgentToolSessionID(messageID, toolCallID string) string {
+ return fmt.Sprintf("%s$$%s", messageID, toolCallID)
+}
+
+// ParseAgentToolSessionID parses an agent tool session ID into its components
+func (s *service) ParseAgentToolSessionID(sessionID string) (messageID string, toolCallID string, ok bool) {
+ parts := strings.Split(sessionID, "$$")
+ if len(parts) != 2 {
+ return "", "", false
+ }
+ return parts[0], parts[1], true
+}
+
+// IsAgentToolSession checks if a session ID follows the agent tool session format
+func (s *service) IsAgentToolSession(sessionID string) bool {
+ _, _, ok := s.ParseAgentToolSessionID(sessionID)
+ return ok
+}
@@ -261,12 +261,19 @@ func (m *messageListCmp) handleChildSession(event pubsub.Event[message.Message])
if len(event.Payload.ToolCalls()) == 0 && len(event.Payload.ToolResults()) == 0 {
return nil
}
+
+ // Check if this is an agent tool session and parse it
+ childSessionID := event.Payload.SessionID
+ parentMessageID, toolCallID, ok := m.app.Sessions.ParseAgentToolSessionID(childSessionID)
+ if !ok {
+ return nil
+ }
items := m.listCmp.Items()
toolCallInx := NotFound
var toolCall messages.ToolCallCmp
for i := len(items) - 1; i >= 0; i-- {
if msg, ok := items[i].(messages.ToolCallCmp); ok {
- if msg.GetToolCall().ID == event.Payload.SessionID {
+ if msg.ParentMessageID() == parentMessageID && msg.GetToolCall().ID == toolCallID {
toolCallInx = i
toolCall = msg
}
@@ -613,7 +620,8 @@ func (m *messageListCmp) convertAssistantMessage(msg message.Message, toolResult
uiMessages = append(uiMessages, messages.NewToolCallCmp(msg.ID, tc, m.app.Permissions, options...))
// If this tool call is the agent tool, fetch nested tool calls
if tc.Name == agent.AgentToolName {
- nestedMessages, _ := m.app.Messages.List(context.Background(), tc.ID)
+ agentToolSessionID := m.app.Sessions.CreateAgentToolSessionID(msg.ID, tc.ID)
+ nestedMessages, _ := m.app.Messages.List(context.Background(), agentToolSessionID)
nestedToolResultMap := m.buildToolResultMap(nestedMessages)
nestedUIMessages := m.convertMessagesToUI(nestedMessages, nestedToolResultMap)
nestedToolCalls := make([]messages.ToolCallCmp, 0, len(nestedUIMessages))
@@ -563,13 +563,6 @@ func (s *sidebarCmp) currentModelBlock() string {
if model.CanReason {
reasoningInfoStyle := t.S().Subtle.PaddingLeft(2)
switch modelProvider.Type {
- case catwalk.TypeOpenAI:
- reasoningEffort := model.DefaultReasoningEffort
- if selectedModel.ReasoningEffort != "" {
- reasoningEffort = selectedModel.ReasoningEffort
- }
- formatter := cases.Title(language.English, cases.NoLower)
- parts = append(parts, reasoningInfoStyle.Render(formatter.String(fmt.Sprintf("Reasoning %s", reasoningEffort))))
case catwalk.TypeAnthropic:
formatter := cases.Title(language.English, cases.NoLower)
if selectedModel.Think {
@@ -577,6 +570,13 @@ func (s *sidebarCmp) currentModelBlock() string {
} else {
parts = append(parts, reasoningInfoStyle.Render(formatter.String("Thinking off")))
}
+ default:
+ reasoningEffort := model.DefaultReasoningEffort
+ if selectedModel.ReasoningEffort != "" {
+ reasoningEffort = selectedModel.ReasoningEffort
+ }
+ formatter := cases.Title(language.English, cases.NoLower)
+ parts = append(parts, reasoningInfoStyle.Render(formatter.String(fmt.Sprintf("Reasoning %s", reasoningEffort))))
}
}
if s.session.ID != "" {
@@ -326,7 +326,7 @@ func (c *commandDialogCmp) defaultCommands() []Command {
}
// OpenAI models: reasoning effort dialog
- if providerCfg.Type == catwalk.TypeOpenAI && model.HasReasoningEffort {
+ if model.HasReasoningEffort {
commands = append(commands, Command{
ID: "select_reasoning_effort",
Title: "Select Reasoning Effort",
@@ -9,7 +9,6 @@ import (
"github.com/charmbracelet/bubbles/v2/key"
"github.com/charmbracelet/bubbles/v2/spinner"
tea "github.com/charmbracelet/bubbletea/v2"
- "github.com/charmbracelet/catwalk/pkg/catwalk"
"github.com/charmbracelet/crush/internal/app"
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/history"
@@ -563,8 +562,7 @@ func (p *chatPage) openReasoningDialog() tea.Cmd {
model := cfg.GetModelByType(agentCfg.Model)
providerCfg := cfg.GetProviderForModel(agentCfg.Model)
- if providerCfg != nil && model != nil &&
- providerCfg.Type == catwalk.TypeOpenAI && model.HasReasoningEffort {
+ if providerCfg != nil && model != nil && model.HasReasoningEffort {
// Return the OpenDialogMsg directly so it bubbles up to the main TUI
return dialogs.OpenDialogMsg{
Model: reasoning.NewReasoningDialog(),