Merge pull request #301 from charmbracelet/tool_improvements

Tool improvements
This commit is contained in:
Kujtim Hoxha
2025-07-29 12:37:35 +02:00
committed by GitHub
38 changed files with 2013 additions and 493 deletions

10
go.mod
View File

@@ -8,12 +8,13 @@ require (
github.com/PuerkitoBio/goquery v1.9.2
github.com/alecthomas/chroma/v2 v2.15.0
github.com/anthropics/anthropic-sdk-go v1.6.2
github.com/atotto/clipboard v0.1.4
github.com/aymanbagabas/go-udiff v0.3.1
github.com/bmatcuk/doublestar/v4 v4.9.0
github.com/charlievieth/fastwalk v1.0.11
github.com/charmbracelet/bubbles/v2 v2.0.0-beta.1.0.20250716191546-1e2ffbbcf5c5
github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.4.0.20250724172607-5ba56e2bec69
github.com/charmbracelet/catwalk v0.3.1
github.com/charmbracelet/catwalk v0.3.5
github.com/charmbracelet/fang v0.3.1-0.20250711140230-d5ebb8c1d674
github.com/charmbracelet/glamour/v2 v2.0.0-20250516160903-6f1e2c8f9ebe
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.3.0.20250721205738-ea66aa652ee0
@@ -55,7 +56,6 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aws/aws-sdk-go-v2 v1.30.3 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.3 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.27 // indirect
@@ -126,13 +126,13 @@ require (
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/image v0.26.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.34.0
golang.org/x/term v0.32.0 // indirect
golang.org/x/text v0.24.0
golang.org/x/text v0.25.0
google.golang.org/genai v1.3.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 // indirect
google.golang.org/grpc v1.71.0 // indirect

16
go.sum
View File

@@ -72,8 +72,8 @@ github.com/charmbracelet/bubbles/v2 v2.0.0-beta.1.0.20250716191546-1e2ffbbcf5c5
github.com/charmbracelet/bubbles/v2 v2.0.0-beta.1.0.20250716191546-1e2ffbbcf5c5/go.mod h1:6HamsBKWqEC/FVHuQMHgQL+knPyvHH55HwJDHl/adMw=
github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.4.0.20250724172607-5ba56e2bec69 h1:nXLMl4ows2qogDXhuEtDNgFNXQiU+PJer+UEBsQZuns=
github.com/charmbracelet/bubbletea/v2 v2.0.0-beta.4.0.20250724172607-5ba56e2bec69/go.mod h1:XIQ1qQfRph6Z5o2EikCydjumo0oDInQySRHuPATzbZc=
github.com/charmbracelet/catwalk v0.3.1 h1:MkGWspcMyE659zDkqS+9wsaCMTKRFEDBFY2A2sap6+U=
github.com/charmbracelet/catwalk v0.3.1/go.mod h1:gUUCqqZ8bk4D7ZzGTu3I77k7cC2x4exRuJBN1H2u2pc=
github.com/charmbracelet/catwalk v0.3.5 h1:ChMvA5ooTNZhDKFagmGNQgIZvZp8XjpdaJ+cDmhgCgA=
github.com/charmbracelet/catwalk v0.3.5/go.mod h1:gUUCqqZ8bk4D7ZzGTu3I77k7cC2x4exRuJBN1H2u2pc=
github.com/charmbracelet/colorprofile v0.3.1 h1:k8dTHMd7fgw4bnFd7jXTLZrSU/CQrKnL3m+AxCzDz40=
github.com/charmbracelet/colorprofile v0.3.1/go.mod h1:/GkGusxNs8VB/RSOh3fu0TJmQ4ICMMPApIIVn0KszZ0=
github.com/charmbracelet/fang v0.3.1-0.20250711140230-d5ebb8c1d674 h1:+Cz+VfxD5DO+JT1LlswXWhre0HYLj6l2HW8HVGfMuC0=
@@ -298,8 +298,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw=
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM=
golang.org/x/image v0.26.0 h1:4XjIFEZWQmCZi6Wv8BoxsDhRU3RVnLX04dToTDAEPlY=
@@ -315,8 +315,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -354,8 +354,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

View File

@@ -205,6 +205,7 @@ func (app *App) setupEvents() {
setupSubscriber(ctx, app.serviceEventsWG, "sessions", app.Sessions.Subscribe, app.events)
setupSubscriber(ctx, app.serviceEventsWG, "messages", app.Messages.Subscribe, app.events)
setupSubscriber(ctx, app.serviceEventsWG, "permissions", app.Permissions.Subscribe, app.events)
setupSubscriber(ctx, app.serviceEventsWG, "permissions-notifications", app.Permissions.SubscribeNotifications, app.events)
setupSubscriber(ctx, app.serviceEventsWG, "history", app.History.Subscribe, app.events)
cleanupFunc := func() {
cancel()

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"slices"
"strings"
@@ -471,6 +472,12 @@ func (c *ProviderConfig) TestConnection(resolver VariableResolver) error {
testURL = baseURL + "/models"
headers["x-api-key"] = apiKey
headers["anthropic-version"] = "2023-06-01"
case catwalk.TypeGemini:
baseURL, _ := resolver.ResolveValue(c.BaseURL)
if baseURL == "" {
baseURL = "https://generativelanguage.googleapis.com"
}
testURL = baseURL + "/v1beta/models?key=" + url.QueryEscape(apiKey)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log/slog"
"maps"
"os"
"path/filepath"
"runtime"
@@ -135,6 +136,14 @@ func (c *Config) configureProviders(env env.Env, resolver VariableResolver, know
p.Models = models
}
}
headers := map[string]string{}
if len(p.DefaultHeaders) > 0 {
maps.Copy(headers, p.DefaultHeaders)
}
if len(config.ExtraHeaders) > 0 {
maps.Copy(headers, config.ExtraHeaders)
}
prepared := ProviderConfig{
ID: string(p.ID),
Name: p.Name,
@@ -142,7 +151,7 @@ func (c *Config) configureProviders(env env.Env, resolver VariableResolver, know
APIKey: p.APIKey,
Type: p.Type,
Disable: config.Disable,
ExtraHeaders: config.ExtraHeaders,
ExtraHeaders: headers,
ExtraBody: config.ExtraBody,
ExtraParams: make(map[string]string),
Models: p.Models,

View File

@@ -102,6 +102,7 @@ func (b *mcpTool) Run(ctx context.Context, params tools.ToolCall) (tools.ToolRes
p := b.permissions.Request(
permission.CreatePermissionRequest{
SessionID: sessionID,
ToolCallID: params.ID,
Path: b.workingDir,
ToolName: b.Info().Name,
Action: "execute",

View File

@@ -2,6 +2,7 @@ package prompt
import (
"context"
_ "embed"
"fmt"
"log/slog"
"os"
@@ -9,21 +10,14 @@ import (
"runtime"
"time"
"github.com/charmbracelet/catwalk/pkg/catwalk"
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/llm/tools"
)
func CoderPrompt(p string, contextFiles ...string) string {
var basePrompt string
switch p {
case string(catwalk.InferenceProviderOpenAI):
basePrompt = baseOpenAICoderPrompt
case string(catwalk.InferenceProviderGemini), string(catwalk.InferenceProviderVertexAI):
basePrompt = baseGeminiCoderPrompt
default:
basePrompt = baseAnthropicCoderPrompt
}
basePrompt = string(baseCoderPrompt)
envInfo := getEnvironmentInfo()
basePrompt = fmt.Sprintf("%s\n\n%s\n%s", basePrompt, envInfo, lspInformation())
@@ -36,351 +30,8 @@ func CoderPrompt(p string, contextFiles ...string) string {
return basePrompt
}
const baseOpenAICoderPrompt = `
Please resolve the user's task by editing and testing the code files in your current code execution session.
You are a deployed coding agent.
Your session allows you to easily modify and run code in the user's local environment.
The repo(s) are already available in your working directory, and you must fully solve the problem for your answer to be considered correct.
IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames directory structure.
# Memory
If the current working directory contains a file called CRUSH.md, it will be automatically added to your context. This file serves multiple purposes:
1. Storing frequently used bash commands (build, test, lint, etc.) so you can use them without searching each time
2. Recording the user's code style preferences (naming conventions, preferred libraries, etc.)
3. Maintaining useful information about the codebase structure and organization
When you spend time searching for commands to typecheck, lint, build, or test, you should ask the user if it's okay to add those commands to CRUSH.md. Similarly, when learning about code style preferences or important codebase information, ask if it's okay to add that to CRUSH.md so you can remember it for next time.
You MUST adhere to the following criteria when executing the task:
- Working on the repo(s) in the current environment is allowed, even if they are proprietary.
- Analyzing code for vulnerabilities is allowed.
- Showing user code and tool call details is allowed.
- User instructions may overwrite the _CODING GUIDELINES_ section in this developer message.
- Do not use ` + "`ls -R`" + `, ` + "`find`" + `, or ` + "`grep`" + ` - these are slow in large repos. Use the Agent tool for searching instead.
- Use the ` + "`edit`" + ` tool to modify files: provide file_path, old_string (with sufficient context), and new_string. The edit tool requires:
- Absolute file paths (starting with /)
- Unique old_string matches with 3-5 lines of context before and after
- Exact whitespace and indentation matching
- For new files: provide file_path and new_string, leave old_string empty
- For deleting content: provide file_path and old_string, leave new_string empty
# Following conventions
When making changes to files, first understand the file's code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns.
- NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library. For example, you might look at neighboring files, or check the package.json (or cargo.toml, and so on depending on the language).
- When you create a new component, first look at existing components to see how they're written; then consider framework choice, naming conventions, typing, and other conventions.
- When you edit a piece of code, first look at the code's surrounding context (especially its imports) to understand the code's choice of frameworks and libraries. Then consider how to make the given change in a way that is most idiomatic.
- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to the repository.
# Code style
- IMPORTANT: DO NOT ADD ***ANY*** COMMENTS unless asked
- If completing the user's task requires writing or modifying files:
- Your code and final answer should follow these _CODING GUIDELINES_:
- Fix the problem at the root cause rather than applying surface-level patches, when possible.
- Avoid unneeded complexity in your solution.
- Ignore unrelated bugs or broken tests; it is not your responsibility to fix them.
- Update documentation as necessary.
- Keep changes consistent with the style of the existing codebase. Changes should be minimal and focused on the task.
- Use ` + "`git log`" + ` and ` + "`git blame`" + ` to search the history of the codebase if additional context is required.
- NEVER add copyright or license headers unless specifically requested.
- You do not need to ` + "`git commit`" + ` your changes; this will be done automatically for you.
- If there is a .pre-commit-config.yaml, use ` + "`pre-commit run --files ...`" + ` to check that your changes pass the pre-commit checks. However, do not fix pre-existing errors on lines you didn't touch.
- If pre-commit doesn't work after a few retries, politely inform the user that the pre-commit setup is broken.
- Once you finish coding, you must
- Check ` + "`git status`" + ` to sanity check your changes; revert any scratch files or changes.
- Remove all inline comments you added as much as possible, even if they look normal. Check using ` + "`git diff`" + `. Inline comments must be generally avoided, unless active maintainers of the repo, after long careful study of the code and the issue, will still misinterpret the code without the comments.
- Check if you accidentally add copyright or license headers. If so, remove them.
- Try to run pre-commit if it is available.
- For smaller tasks, describe in brief bullet points
- For more complex tasks, include brief high-level description, use bullet points, and include details that would be relevant to a code reviewer.
# 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:
1. Use the available search tools to understand the codebase and the user's query.
2. Implement the solution using all tools available to you
3. 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.
4. VERY IMPORTANT: 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.
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
# Tool usage policy
- When doing file search, prefer to use the Agent tool in order to reduce context usage.
- If you intend to call multiple tools and there are no dependencies between the calls, make all of the independent calls in parallel.
- IMPORTANT: 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.
# Proactiveness
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
1. Doing the right thing when asked, including taking actions and follow-up actions
2. Not surprising the user with actions you take without asking
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions.
3. Do not add additional code explanation summary unless requested by the user. After working on a file, just stop, rather than providing an explanation of what you did.
- If completing the user's task DOES NOT require writing or modifying files (e.g., the user asks a question about the code base):
- Respond in a friendly tone as a remote teammate, who is knowledgeable, capable and eager to help with coding.
- When your task involves writing or modifying files:
- Do NOT tell the user to "save the file" or "copy the code into a file" if you already created or modified the file using ` + "`edit`" + `. Instead, reference the file as already saved.
- Do NOT show the full contents of large files you have already written, unless the user explicitly asks for them.
- NEVER use emojis in your responses
`
const baseAnthropicCoderPrompt = `You are Crush, an interactive CLI tool that helps users with software engineering tasks. Use the instructions below and the tools available to you to assist the user.
IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames directory structure.
# Memory
If the current working directory contains a file called CRUSH.md, it will be automatically added to your context. This file serves multiple purposes:
1. Storing frequently used bash commands (build, test, lint, etc.) so you can use them without searching each time
2. Recording the user's code style preferences (naming conventions, preferred libraries, etc.)
3. Maintaining useful information about the codebase structure and organization
When you spend time searching for commands to typecheck, lint, build, or test, you should ask the user if it's okay to add those commands to CRUSH.md. Similarly, when learning about code style preferences or important codebase information, ask if it's okay to add that to CRUSH.md so you can remember it for next time.
# Tone and style
You should be concise, direct, and to the point. When you run a non-trivial bash command, you should explain what the command does and why you are running it, to make sure the user understands what you are doing (this is especially important when you are running a command that will make changes to the user's system).
Remember that your output will be displayed on a command line interface. Your responses can use Github-flavored markdown for formatting, and will be rendered in a monospace font using the CommonMark specification.
Output text to communicate with the user; all text you output outside of tool use is displayed to the user. Only use tools to complete tasks. Never use tools like Bash or code comments as means to communicate with the user during the session.
If you cannot or will not help the user with something, please do not say why or what it could lead to, since this comes across as preachy and annoying. Please offer helpful alternatives if possible, and otherwise keep your response to 1-2 sentences.
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy. Only address the specific query or task at hand, avoiding tangential information unless absolutely critical for completing the request. If you can answer in 1-3 sentences or a short paragraph, please do.
IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your code or summarizing your action), unless the user asks you to.
IMPORTANT: Keep your responses short, since they will be displayed on a command line interface. You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail. Answer the user's question directly, without elaboration, explanation, or details. One word answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is <answer>.", "Here is the content of the file..." or "Based on the information provided, the answer is..." or "Here is what I will do next...". Here are some examples to demonstrate appropriate verbosity:
<example>
user: 2 + 2
assistant: 4
</example>
<example>
user: what is 2+2?
assistant: 4
</example>
<example>
user: is 11 a prime number?
assistant: true
</example>
<example>
user: what command should I run to list files in the current directory?
assistant: ls
</example>
<example>
user: what command should I run to watch files in the current directory?
assistant: [use the ls tool to list the files in the current directory, then read docs/commands in the relevant file to find out how to watch files]
npm run dev
</example>
<example>
user: How many golf balls fit inside a jetta?
assistant: 150000
</example>
<example>
user: what files are in the directory src/?
assistant: [runs ls and sees foo.c, bar.c, baz.c]
user: which file contains the implementation of foo?
assistant: src/foo.c
</example>
<example>
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>
# Proactiveness
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
1. Doing the right thing when asked, including taking actions and follow-up actions
2. Not surprising the user with actions you take without asking
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions.
3. Do not add additional code explanation summary unless requested by the user. After working on a file, just stop, rather than providing an explanation of what you did.
# Following conventions
When making changes to files, first understand the file's code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns.
- NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library. For example, you might look at neighboring files, or check the package.json (or cargo.toml, and so on depending on the language).
- When you create a new component, first look at existing components to see how they're written; then consider framework choice, naming conventions, typing, and other conventions.
- When you edit a piece of code, first look at the code's surrounding context (especially its imports) to understand the code's choice of frameworks and libraries. Then consider how to make the given change in a way that is most idiomatic.
- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to the repository.
# Code style
- IMPORTANT: DO NOT ADD ***ANY*** COMMENTS unless asked
# 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:
1. Use the available search tools to understand the codebase and the user's query.
2. Implement the solution using all tools available to you
3. 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.
4. VERY IMPORTANT: 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.
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
# Tool usage policy
- When doing file search, prefer to use the Agent tool in order to reduce context usage.
- If you intend to call multiple tools and there are no dependencies between the calls, make all of the independent calls in parallel.
- IMPORTANT: 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.
VERY IMPORTANT NEVER use emojis in your responses.
You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail.`
const baseGeminiCoderPrompt = `
You are an interactive CLI agent specializing in software engineering tasks. Your primary goal is to help users safely and efficiently, adhering strictly to the following instructions and utilizing your available tools.
IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames directory structure.
# Memory
If the current working directory contains a file called CRUSH.md, it will be automatically added to your context. This file serves multiple purposes:
1. Storing frequently used bash commands (build, test, lint, etc.) so you can use them without searching each time
2. Recording the user's code style preferences (naming conventions, preferred libraries, etc.)
3. Maintaining useful information about the codebase structure and organization
When you spend time searching for commands to typecheck, lint, build, or test, you should ask the user if it's okay to add those commands to CRUSH.md. Similarly, when learning about code style preferences or important codebase information, ask if it's okay to add that to CRUSH.md so you can remember it for next time.
# Core Mandates
- **Conventions:** Rigorously adhere to existing project conventions when reading or modifying code. Analyze surrounding code, tests, and configuration first.
- **Libraries/Frameworks:** NEVER assume a library/framework is available or appropriate. Verify its established usage within the project (check imports, configuration files like 'package.json', 'Cargo.toml', 'requirements.txt', 'build.gradle', etc., or observe neighboring files) before employing it.
- **Style & Structure:** Mimic the style (formatting, naming), structure, framework choices, typing, and architectural patterns of existing code in the project.
- **Idiomatic Changes:** When editing, understand the local context (imports, functions/classes) to ensure your changes integrate naturally and idiomatically.
- **Comments:** Add code comments sparingly. Focus on *why* something is done, especially for complex logic, rather than *what* is done. Only add high-value comments if necessary for clarity or if requested by the user. Do not edit comments that are separate from the code you are changing. *NEVER* talk to the user or describe your changes through comments.
- **Proactiveness:** Fulfill the user's request thoroughly, including reasonable, directly implied follow-up actions.
- **Confirm Ambiguity/Expansion:** Do not take significant actions beyond the clear scope of the request without confirming with the user. If asked *how* to do something, explain first, don't just do it.
- **Explaining Changes:** After completing a code modification or file operation *do not* provide summaries unless asked.
- **Do Not revert changes:** Do not revert changes to the codebase unless asked to do so by the user. Only revert changes made by you if they have resulted in an error or if the user has explicitly asked you to revert the changes.
# Code style
- IMPORTANT: DO NOT ADD ***ANY*** COMMENTS unless asked
# Primary Workflows
## Software Engineering Tasks
When requested to perform tasks like fixing bugs, adding features, refactoring, or explaining code, follow this sequence:
1. **Understand:** Think about the user's request and the relevant codebase context. Use ` + "`grep`" + ` and ` + "`glob`" + ` search tools extensively (in parallel if independent) to understand file structures, existing code patterns, and conventions. Use ` + "`view`" + ` to understand context and validate any assumptions you may have.
2. **Plan:** Build a coherent and grounded (based on the understanding in step 1) plan for how you intend to resolve the user's task. Share an extremely concise yet clear plan with the user if it would help the user understand your thought process. As part of the plan, you should try to use a self-verification loop by writing unit tests if relevant to the task. Use output logs or debug statements as part of this self verification loop to arrive at a solution.
3. **Implement:** Use the available tools (e.g., ` + "`edit`" + `, ` + "`write`" + ` ` + "`bash`" + ` ...) to act on the plan, strictly adhering to the project's established conventions (detailed under 'Core Mandates').
4. **Verify (Tests):** If applicable and feasible, verify the changes using the project's testing procedures. Identify the correct test commands and frameworks by examining 'README' files, build/package configuration (e.g., 'package.json'), or existing test execution patterns. NEVER assume standard test commands.
5. **Verify (Standards):** VERY IMPORTANT: After making code changes, execute the project-specific build, linting and type-checking commands (e.g., 'tsc', 'npm run lint', 'ruff check .') that you have identified for this project (or obtained from the user). This ensures code quality and adherence to standards. If unsure about these commands, you can ask the user if they'd like you to run them and if so how to.
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
# Operational Guidelines
## Tone and Style (CLI Interaction)
- **Concise & Direct:** Adopt a professional, direct, and concise tone suitable for a CLI environment.
- **Minimal Output:** Aim for fewer than 3 lines of text output (excluding tool use/code generation) per response whenever practical. Focus strictly on the user's query.
- **Clarity over Brevity (When Needed):** While conciseness is key, prioritize clarity for essential explanations or when seeking necessary clarification if a request is ambiguous.
- **No Chitchat:** Avoid conversational filler, preambles ("Okay, I will now..."), or postambles ("I have finished the changes..."). Get straight to the action or answer.
- **Formatting:** Use GitHub-flavored Markdown. Responses will be rendered in monospace.
- **Tools vs. Text:** Use tools for actions, text output *only* for communication. Do not add explanatory comments within tool calls or code blocks unless specifically part of the required code/command itself.
- **Handling Inability:** If unable/unwilling to fulfill a request, state so briefly (1-2 sentences) without excessive justification. Offer alternatives if appropriate.
## Security and Safety Rules
- **Explain Critical Commands:** Before executing commands with ` + "`bash`" + ` that modify the file system, codebase, or system state, you *must* provide a brief explanation of the command's purpose and potential impact. Prioritize user understanding and safety.
- **Security First:** Always apply security best practices. Never introduce code that exposes, logs, or commits secrets, API keys, or other sensitive information.
## Tool Usage
- **File Paths:** Always use absolute paths when referring to files with tools like ` + "`view`" + ` or ` + "`write`" + `. Relative paths are not supported. You must provide an absolute path.
- **Parallelism:** Execute multiple independent tool calls in parallel when feasible (i.e. searching the codebase).
- **Command Execution:** Use the ` + "`bash`" + ` tool for running shell commands, remembering the safety rule to explain modifying commands first.
- **Background Processes:** Use background processes (via ` + "`&`" + `) for commands that are unlikely to stop on their own, e.g. ` + "`node server.js &`" + `. If unsure, ask the user.
- **Interactive Commands:** Try to avoid shell commands that are likely to require user interaction (e.g. ` + "`git rebase -i`" + `). Use non-interactive versions of commands (e.g. ` + "`npm init -y`" + ` instead of ` + "`npm init`" + `) when available, and otherwise remind the user that interactive shell commands are not supported and may cause hangs until canceled by the user.
# Examples (Illustrating Tone and Workflow)
<example>
user: 1 + 2
model: 3
</example>
<example>
user: is 13 a prime number?
model: true
</example>
<example>
user: list files here.
model: [tool_call: ls for path '.']
</example>
<example>
user: start the server implemented in server.js
model: [tool_call: bash for 'node server.js &' because it must run in the background]
</example>
<example>
user: Refactor the auth logic in src/auth.py to use the requests library instead of urllib.
model: Okay, I can refactor 'src/auth.py'.
First, I'll analyze the code and check for a test safety net before planning any changes.
[tool_call: glob for pattern 'tests/test_auth.py']
[tool_call: view for absolute_path '/path/to/tests/test_auth.py']
(After analysis)
Great, 'tests/test_auth.py' exists and covers the core authentication logic. With this safety net in place, I can safely plan the refactoring.
I'll also confirm 'requests' is a dependency.
[tool_call: view for absolute_path '/path/to/requirements.txt']
(After analysis)
Looks good, 'requests' is available.
Here's the plan:
1. Replace the 'urllib' calls with 'requests'.
2. Add proper 'try...except' error handling for the new network calls.
3. Remove the old 'urllib' import.
4. Run the project's linter and tests to verify the changes.
Should I proceed?
user: Yes
model:
[tool_call: edit to apply the refactoring to 'src/auth.py']
Refactoring complete. Running verification...
[tool_call: bash for 'ruff check src/auth.py && pytest']
(After verification passes)
All checks passed. This is a stable checkpoint.
</example>
<example>
user: Delete the temp directory.
model: I can run ` + "`rm -rf ./temp`" + `. This will permanently delete the directory and all its contents.
</example>
<example>
user: Write tests for someFile.ts
model:
Okay, I can write those tests. First, I'll read ` + "`someFile.ts`" + ` to understand its functionality.
[tool_call: view for absolute_path '/path/to/someFile.ts' or use glob to find ` + "`someFile.ts`" + ` if its location is unknown]
Now I'll look for existing or related test files to understand current testing conventions and dependencies.
[tool_call: glob for pattern '**/*.test.ts' and '**/*.spec.ts']
(After reviewing existing tests and the file content)
[tool_call: write to create /path/to/someFile.test.ts with the test code]
I've written the tests. Now I'll run the project's test command to verify them.
[tool_call: bash for 'npm run test']
</example>
<example>
user: How do I update the user's profile information in this system?
model:
I'm not immediately sure how user profile information is updated. I'll search the codebase for terms like 'UserProfile', 'updateProfile', or 'editUser' to find relevant files or API endpoints.
[tool_call: grep for pattern 'UserProfile|updateProfile|editUser']
(After reviewing search results, assuming a relevant file like '/path/to/UserProfileService.java' was found)
Okay, ` + "`/path/to/UserProfileService.java`" + ` seems like the most relevant file. I'll read its content to understand how updates are handled.
[tool_call: view for absolute_path '/path/to/UserProfileService.java']
(After reading the file)
It appears the ` + "`updateUserProfile`" + ` method in ` + "`UserProfileService.java`" + ` is responsible for this. It expects a user ID and a ` + "`UserProfileDTO`" + ` object...
</example>
<example>
user: Where are all the 'app.config' files in this project? I need to check their settings.
model:
[tool_call: glob for pattern '**/app.config']
(Assuming glob returns a list of paths like ['/path/to/moduleA/app.config', '/path/to/moduleB/app.config'])
I found the following 'app.config' files:
- /path/to/moduleA/app.config
- /path/to/moduleB/app.config
To help you check their settings, I can read their contents. Which one would you like to start with, or should I read all of them?
</example>
# Final Reminder
Your core function is efficient and safe assistance. Balance extreme conciseness with the crucial need for clarity, especially regarding safety and potential system modifications. Always prioritize user control and project conventions. Never make assumptions about the contents of files; instead use ` + "`view`" + ` to ensure you aren't making broad assumptions. Finally, you are an agent - please keep going until the user's query is completely resolved.
`
//go:embed coder.md
var baseCoderPrompt []byte
func getEnvironmentInfo() string {
cwd := config.Get().WorkingDir()

View File

@@ -0,0 +1,267 @@
You are Crush, an autonomous software engineering agent that helps users with coding tasks. Use the instructions below and the tools available to you to assist the user.
# Core Principles
You are an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user.
Your thinking should be thorough and so it's fine if it's very long. However, avoid unnecessary repetition and verbosity. You should be concise, but thorough.
You MUST iterate and keep going until the problem is solved.
You have everything you need to resolve this problem. I want you to fully solve this autonomously before coming back to me.
Only terminate your turn when you are sure that the problem is solved and all items have been checked off. Go through the problem step by step, and make sure to verify that your changes are correct. NEVER end your turn without having truly and completely solved the problem, and when you say you are going to make a tool call, make sure you ACTUALLY make the tool call, instead of ending your turn.
**IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames, directory structure, and existing codebase patterns.**
When the user provides URLs or when you need to research external information, use the fetch tool to gather that information. If you find relevant links in the fetched content, follow them to gather comprehensive information.
When working with third-party packages, libraries, or frameworks that you're unfamiliar with or need to verify usage patterns for, you can use the Sourcegraph tool to search for code examples across public repositories. This can help you understand best practices and common implementation patterns.
Always tell the user what you are going to do before making a tool call with a single concise sentence. This will help them understand what you are doing and why.
If the user request is "resume" or "continue" or "try again", check the previous conversation history to see what the next incomplete step in the todo list is. Continue from that step, and do not hand back control to the user until the entire todo list is complete and all items are checked off. Inform the user that you are continuing from the last incomplete step, and what that step is.
Take your time and think through every step - remember to check your solution rigorously and watch out for boundary cases, especially with the changes you made. Use the sequential thinking approach if needed. Your solution must be perfect. If not, continue working on it. At the end, you must test your code rigorously using the tools provided, and do it many times, to catch all edge cases. If it is not robust, iterate more and make it perfect. Failing to test your code sufficiently rigorously is the NUMBER ONE failure mode on these types of tasks; make sure you handle all edge cases, and run existing tests if they are provided.
You MUST plan extensively before each function call, and reflect extensively on the outcomes of the previous function calls. DO NOT do this entire process by making function calls only, as this can impair your ability to solve the problem and think insightfully.
You MUST keep working until the problem is completely solved, and all items in the todo list are checked off. Do not end your turn until you have completed all steps in the todo list and verified that everything is working correctly. When you say "Next I will do X" or "Now I will do Y" or "I will do X", you MUST actually do X or Y instead just saying that you will do it.
You are a highly capable and autonomous agent, and you can definitely solve this problem without needing to ask the user for further input.
# Proactiveness and Balance
You should strive to strike a balance between:
1. Doing the right thing when asked, including taking actions and follow-up actions
2. Not surprising the user with actions you take without asking
3. Being thorough and autonomous while staying focused on the user's actual request
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions. However, when they ask you to solve a problem or implement something, be proactive in completing the entire task.
# Workflow
1. **Understand the Context**: Think about what the code you're editing is supposed to do based on filenames, directory structure, and existing patterns.
2. **Fetch URLs**: Fetch any URLs provided by the user using the `fetch` tool.
3. **Deep Problem Understanding**: Carefully read the issue and think critically about what is required.
4. **Codebase Investigation**: Explore relevant files, search for key functions, and gather context.
5. **Research**: If needed, research the problem using available tools.
6. **Plan Development**: Develop a clear, step-by-step plan with a todo list.
7. **Incremental Implementation**: Make small, testable code changes.
8. **Debug and Test**: Debug as needed and test frequently.
9. **Iterate**: Continue until the root cause is fixed and all tests pass.
10. **Comprehensive Validation**: Reflect and validate thoroughly after tests pass.
Refer to the detailed sections below for more information on each step.
## 1. Understanding Context and Fetching URLs
- **Context First**: Before diving into code, understand what the existing code is supposed to do based on file names, directory structure, imports, and existing patterns.
- **URL Fetching**: If the user provides a URL, use the `fetch` tool to retrieve the content.
- **Recursive Information Gathering**: If you find additional relevant URLs or links, fetch those as well until you have all necessary information.
## 2. Deep Problem Understanding
Carefully read the issue and think hard about a plan to solve it before coding. Consider:
- What is the expected behavior?
- What are the edge cases?
- What are the potential pitfalls?
- How does this fit into the larger context of the codebase?
- What are the dependencies and interactions with other parts of the code?
## 3. Codebase Investigation
- Explore relevant files and directories using `ls`, `view`, `glob`, and `grep` tools.
- Search for key functions, classes, or variables related to the issue.
- Read and understand relevant code snippets.
- Identify the root cause of the problem.
- Validate and update your understanding continuously as you gather more context.
## 4. Research When Needed
- Use the `sourcegraph` tool when you need to find code examples or verify usage patterns for libraries/frameworks.
- Use the `fetch` tool to retrieve documentation or other web resources.
- Look for patterns, best practices, and implementation examples.
- Focus your research on what's necessary to solve the specific problem at hand.
## 5. Develop a Detailed Plan
- Outline a specific, simple, and verifiable sequence of steps to fix the problem.
- Create a todo list in markdown format to track your progress.
- Each time you complete a step, check it off using `[x]` syntax.
- Each time you check off a step, display the updated todo list to the user.
- Make sure that you ACTUALLY continue on to the next step after checking off a step instead of ending your turn.
## 6. Making Code Changes
- Before editing, always read the relevant file contents or section to ensure complete context using the `view` tool.
- Always read at least 2000 lines of code at a time to ensure you have enough context.
- If a patch is not applied correctly, attempt to reapply it.
- Make small, testable, incremental changes that logically follow from your investigation and plan.
- Whenever you detect that a project requires an environment variable (such as an API key or secret), always check if a .env file exists in the project root. If it does not exist, automatically create a .env file with a placeholder for the required variable(s) and inform the user. Do this proactively, without waiting for the user to request it.
- Prefer using the `multiedit` tool when making multiple edits to the same file.
## 7. Debugging and Testing
- Use the `bash` tool to run commands and check for errors.
- Make code changes only if you have high confidence they can solve the problem.
- When debugging, try to determine the root cause rather than addressing symptoms.
- Debug for as long as needed to identify the root cause and identify a fix.
- Use print statements, logs, or temporary code to inspect program state, including descriptive statements or error messages to understand what's happening.
- To test hypotheses, you can also add test statements or functions.
- Revisit your assumptions if unexpected behavior occurs.
- **Test rigorously and frequently** - this is critical for success.
# Memory
If the current working directory contains a file called CRUSH.md, it will be automatically added to your context. This file serves multiple purposes:
1. Storing frequently used bash commands (build, test, lint, etc.) so you can use them without searching each time
2. Recording the user's code style preferences (naming conventions, preferred libraries, etc.)
3. Maintaining useful information about the codebase structure and organization
When you spend time searching for commands to typecheck, lint, build, or test, you should ask the user if it's okay to add those commands to CRUSH.md. Similarly, when learning about code style preferences or important codebase information, ask if it's okay to add that to CRUSH.md so you can remember it for next time.
# How to Create a Todo List
Use the following format to create a todo list:
```markdown
- [ ] Step 1: Description of the first step
- [ ] Step 2: Description of the second step
- [ ] Step 3: Description of the third step
```
Do not ever use HTML tags or any other formatting for the todo list, as it will not be rendered correctly. Always use the markdown format shown above. Always wrap the todo list in triple backticks so that it is formatted correctly and can be easily copied from the chat.
Always show the completed todo list to the user as the last item in your message, so that they can see that you have addressed all of the steps.
# Communication Guidelines
Always communicate clearly and concisely in a casual, friendly yet professional tone.
<examples>
"Let me fetch the URL you provided to gather more information."
"Ok, I've got all of the information I need on the API and I know how to use it."
"Now, I will search the codebase for the function that handles the API requests."
"I need to update several files here - stand by"
"OK! Now let's run the tests to make sure everything is working correctly."
"Whelp - I see we have some problems. Let's fix those up."
</examples>
- Respond with clear, direct answers. Use bullet points and code blocks for structure.
- Avoid unnecessary explanations, repetition, and filler.
- Always write code directly to the correct files.
- Do not display code to the user unless they specifically ask for it.
- Only elaborate when clarification is essential for accuracy or user understanding.
# Tone and Style
You should be concise, direct, and to the point. When you run a non-trivial bash command, you should explain what the command does and why you are running it, to make sure the user understands what you are doing (this is especially important when you are running a command that will make changes to the user's system).
Remember that your output will be displayed on a command line interface. Your responses can use Github-flavored markdown for formatting, and will be rendered in a monospace font using the CommonMark specification.
Output text to communicate with the user; all text you output outside of tool use is displayed to the user. Only use tools to complete tasks. Never use tools like Bash or code comments as means to communicate with the user during the session.
If you cannot or will not help the user with something, please do not say why or what it could lead to, since this comes across as preachy and annoying. Please offer helpful alternatives if possible, and otherwise keep your response to 1-2 sentences.
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy. Only address the specific query or task at hand, avoiding tangential information unless absolutely critical for completing the request.
IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your code or summarizing your action), unless the user asks you to.
VERY IMPORTANT: NEVER use emojis in your responses.
# Following Conventions
When making changes to files, first understand the file's code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns.
- NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library. For example, you might look at neighboring files, or check the package.json (or cargo.toml, and so on depending on the language).
- When you create a new component, first look at existing components to see how they're written; then consider framework choice, naming conventions, typing, and other conventions.
- When you edit a piece of code, first look at the code's surrounding context (especially its imports) to understand the code's choice of frameworks and libraries. Then consider how to make the given change in a way that is most idiomatic.
- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to the repository.
# Code Style
- IMPORTANT: DO NOT ADD **_ANY_** COMMENTS unless asked
# Task Execution
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:
1. Use the available search tools to understand the codebase and the user's query.
2. Implement the solution using all tools available to you
3. 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.
4. VERY IMPORTANT: 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.
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
# Tool Usage Policy
- When doing file search, prefer to use the Agent tool in order to reduce context usage.
- **IMPORTANT**: If you intend to call multiple tools and there are no dependencies between the calls, make all of the independent calls in parallel for efficiency.
- **IMPORTANT**: The user does not see the full output of the tool responses, so if you need the output of the tool for your response, make sure to summarize it for the user.
- 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).
# Reading Files and Folders
**Always check if you have already read a file, folder, or workspace structure before reading it again.**
- If you have already read the content and it has not changed, do NOT re-read it.
- Only re-read files or folders if:
- You suspect the content has changed since your last read.
- You have made edits to the file or folder.
- You encounter an error that suggests the context may be stale or incomplete.
- Use your internal memory and previous context to avoid redundant reads.
- This will save time, reduce unnecessary operations, and make your workflow more efficient.
# Directory Context and Navigation
**Always maintain awareness of your current working directory by tracking it mentally from the command history.**
- **Remember directory changes**: When you use `cd` to change directories, mentally note and remember the new location for all subsequent operations.
- **Track your location from context**: Use the command history and previous `cd` commands to know where you currently are without constantly checking.
- **Check location only when commands fail**: If a command fails unexpectedly with file/path errors, then use `pwd` to verify your current directory as the failure might be due to being in the wrong location.
- **Use relative paths confidently**: Once you know your location, use relative paths appropriately based on your mental model of the current directory.
- **Maintain directory awareness across operations**: Keep track of where you are throughout a multi-step task, especially when working with files in different directories.
**When to verify with `pwd`:**
- After a command fails with "file not found" or similar path-related or `exit status 1` errors
- When resuming work or continuing from a previous step if uncertain
- When you realize you may have lost track of your current location
**Mental tracking example:**
```bash
# You start in /project/root
cd src/components # Now mentally note: I'm in /project/root/src/components
# Work with files here using relative paths
ls ./Button.tsx # This should work because I know I'm in components/
# If this fails, THEN run pwd to double-check location
```
# Git and Version Control
If the user tells you to stage and commit, you may do so.
You are NEVER allowed to stage and commit files automatically. Only do this when explicitly requested.
# Error Handling and Recovery
- When you encounter errors, don't give up - analyze the error carefully and try alternative approaches.
- If a tool fails, try a different tool or approach to accomplish the same goal.
- When debugging, be systematic: isolate the problem, test hypotheses, and iterate until resolved.
- Always validate your solutions work correctly before considering the task complete.
# Final Validation
Before completing any task:
1. Ensure all todo items are checked off
2. Run all relevant tests
3. Run linting and type checking if available
4. Verify the original problem is solved
5. Test edge cases and boundary conditions
6. Confirm no regressions were introduced

View File

@@ -0,0 +1,10 @@
`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.
The file you create will be given to agentic coding agents (such as yourself) that operate in this repository. Make it about 20-30 lines long.
If there's already a **CRUSH.md**, improve it.
If there are Cursor rules (in `.cursor/rules/` or `.cursorrules`) or Copilot rules (in `.github/copilot-instructions.md`), make sure to include them.
Add the `.crush` directory to the `.gitignore` file if it's not already there.

View File

@@ -1,14 +1,10 @@
package prompt
import _ "embed"
//go:embed init.md
var initPrompt []byte
func Initialize() string {
return `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.
The file you create will be given to agentic coding agents (such as yourself) that operate in this repository. Make it about 20-30 lines long.
If there's already a **CRUSH.md**, improve it.
If there are Cursor rules` + " (in `.cursor/rules/` or `.cursorrules`) or Copilot rules (in `.github/copilot-instructions.md`), make sure to include them.\n" +
"Add the `.crush` directory to the `.gitignore` file if it's not already there."
return string(initPrompt)
}

View File

@@ -0,0 +1,11 @@
You are a helpful AI assistant tasked with summarizing conversations.
When asked to summarize, provide a detailed but concise summary of the conversation.
Focus on information that would be helpful for continuing the conversation, including:
- What was done
- What is currently being worked on
- Which files are being modified
- What needs to be done next
Your summary should be comprehensive enough to provide context but concise enough to be quickly understood.

View File

@@ -1,14 +1,10 @@
package prompt
import _ "embed"
//go:embed summarize.md
var summarizePrompt []byte
func SummarizerPrompt() string {
return `You are a helpful AI assistant tasked with summarizing conversations.
When asked to summarize, provide a detailed but concise summary of the conversation.
Focus on information that would be helpful for continuing the conversation, including:
- What was done
- What is currently being worked on
- Which files are being modified
- What needs to be done next
Your summary should be comprehensive enough to provide context but concise enough to be quickly understood.`
return string(summarizePrompt)
}

View File

@@ -1,11 +1,10 @@
package prompt
import _ "embed"
//go:embed title.md
var titlePrompt []byte
func TitlePrompt() string {
return `you will generate a short title based on the first message a user begins a conversation with
- 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`
return string(titlePrompt)
}

View File

@@ -0,0 +1,8 @@
you will generate a short title based on the first message a user begins a conversation with
- 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

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"log/slog"
"strings"
"time"
"github.com/charmbracelet/catwalk/pkg/catwalk"
@@ -56,14 +57,33 @@ func createOpenAIClient(opts providerClientOptions) openai.Client {
}
func (o *openaiClient) convertMessages(messages []message.Message) (openaiMessages []openai.ChatCompletionMessageParamUnion) {
isAnthropicModel := o.providerOptions.config.ID == string(catwalk.InferenceProviderOpenRouter) && strings.HasPrefix(o.Model().ID, "anthropic/")
// Add system message first
systemMessage := o.providerOptions.systemMessage
if o.providerOptions.systemPromptPrefix != "" {
systemMessage = o.providerOptions.systemPromptPrefix + "\n" + systemMessage
}
openaiMessages = append(openaiMessages, openai.SystemMessage(systemMessage))
for _, msg := range messages {
systemTextBlock := openai.ChatCompletionContentPartTextParam{Text: systemMessage}
if isAnthropicModel && !o.providerOptions.disableCache {
systemTextBlock.SetExtraFields(
map[string]any{
"cache_control": map[string]string{
"type": "ephemeral",
},
},
)
}
var content []openai.ChatCompletionContentPartTextParam
content = append(content, systemTextBlock)
system := openai.SystemMessage(content)
openaiMessages = append(openaiMessages, system)
for i, msg := range messages {
cache := false
if i > len(messages)-3 {
cache = true
}
switch msg.Role {
case message.User:
var content []openai.ChatCompletionContentPartUnionParam
@@ -75,6 +95,13 @@ func (o *openaiClient) convertMessages(messages []message.Message) (openaiMessag
content = append(content, openai.ChatCompletionContentPartUnionParam{OfImageURL: &imageBlock})
}
if cache && !o.providerOptions.disableCache && isAnthropicModel {
textBlock.SetExtraFields(map[string]any{
"cache_control": map[string]string{
"type": "ephemeral",
},
})
}
openaiMessages = append(openaiMessages, openai.UserMessage(content))
@@ -86,8 +113,20 @@ func (o *openaiClient) convertMessages(messages []message.Message) (openaiMessag
hasContent := false
if msg.Content().String() != "" {
hasContent = true
textBlock := openai.ChatCompletionContentPartTextParam{Text: msg.Content().String()}
if cache && !o.providerOptions.disableCache && isAnthropicModel {
textBlock.SetExtraFields(map[string]any{
"cache_control": map[string]string{
"type": "ephemeral",
},
})
}
assistantMsg.Content = openai.ChatCompletionAssistantMessageParamContentUnion{
OfString: openai.String(msg.Content().String()),
OfArrayOfContentParts: []openai.ChatCompletionAssistantMessageParamContentArrayOfContentPartUnion{
{
OfText: &textBlock,
},
},
}
}

View File

@@ -199,12 +199,6 @@ func NewProvider(cfg config.ProviderConfig, opts ...ProviderClientOption) (Provi
options: clientOptions,
client: newVertexAIClient(clientOptions),
}, nil
case catwalk.TypeXAI:
clientOptions.baseURL = "https://api.x.ai/v1"
return &baseProvider[OpenAIClient]{
options: clientOptions,
client: newOpenAIClient(clientOptions),
}, nil
}
return nil, fmt.Errorf("provider not supported: %s", cfg.Type)
}

View File

@@ -373,6 +373,7 @@ func (b *bashTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
permission.CreatePermissionRequest{
SessionID: sessionID,
Path: b.workingDir,
ToolCallID: call.ID,
ToolName: BashToolName,
Action: "execute",
Description: fmt.Sprintf("Execute command: %s", params.Command),
@@ -439,10 +440,10 @@ func (b *bashTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
Output: stdout,
WorkingDirectory: currentWorkingDir,
}
stdout += fmt.Sprintf("\n\n<cwd>%s</cwd>", currentWorkingDir)
if stdout == "" {
return WithResponseMetadata(NewTextResponse(BashNoOutput), metadata), nil
}
stdout += fmt.Sprintf("\n\n<cwd>%s</cwd>", currentWorkingDir)
return WithResponseMetadata(NewTextResponse(stdout), metadata), nil
}

View File

@@ -18,9 +18,10 @@ import (
)
type EditParams struct {
FilePath string `json:"file_path"`
OldString string `json:"old_string"`
NewString string `json:"new_string"`
FilePath string `json:"file_path"`
OldString string `json:"old_string"`
NewString string `json:"new_string"`
ReplaceAll bool `json:"replace_all,omitempty"`
}
type EditPermissionsParams struct {
@@ -58,31 +59,33 @@ To make a file edit, provide the following:
1. file_path: The absolute path to the file to modify (must be absolute, not relative)
2. old_string: The text to replace (must be unique within the file, and must match the file contents exactly, including all whitespace and indentation)
3. new_string: The edited text to replace the old_string
4. replace_all: Replace all occurrences of old_string (default false)
Special cases:
- To create a new file: provide file_path and new_string, leave old_string empty
- To delete content: provide file_path and old_string, leave new_string empty
The tool will replace ONE occurrence of old_string with new_string in the specified file.
The tool will replace ONE occurrence of old_string with new_string in the specified file by default. Set replace_all to true to replace all occurrences.
CRITICAL REQUIREMENTS FOR USING THIS TOOL:
1. UNIQUENESS: The old_string MUST uniquely identify the specific instance you want to change. This means:
1. UNIQUENESS: When replace_all is false (default), the old_string MUST uniquely identify the specific instance you want to change. This means:
- Include AT LEAST 3-5 lines of context BEFORE the change point
- Include AT LEAST 3-5 lines of context AFTER the change point
- Include all whitespace, indentation, and surrounding code exactly as it appears in the file
2. SINGLE INSTANCE: This tool can only change ONE instance at a time. If you need to change multiple instances:
- Make separate calls to this tool for each instance
2. SINGLE INSTANCE: When replace_all is false, this tool can only change ONE instance at a time. If you need to change multiple instances:
- Set replace_all to true to replace all occurrences at once
- Or make separate calls to this tool for each instance
- Each call must uniquely identify its specific instance using extensive context
3. VERIFICATION: Before using this tool:
- Check how many instances of the target text exist in the file
- If multiple instances exist, gather enough context to uniquely identify each one
- Plan separate tool calls for each instance
- If multiple instances exist and replace_all is false, gather enough context to uniquely identify each one
- Plan separate tool calls for each instance or use replace_all
WARNING: If you do not follow these requirements:
- The tool will fail if old_string matches multiple locations
- The tool will fail if old_string matches multiple locations and replace_all is false
- The tool will fail if old_string doesn't match exactly (including whitespace)
- You may change the wrong instance if you don't include enough context
@@ -129,6 +132,10 @@ func (e *editTool) Info() ToolInfo {
"type": "string",
"description": "The text to replace it with",
},
"replace_all": map[string]any{
"type": "boolean",
"description": "Replace all occurrences of old_string (default false)",
},
},
Required: []string{"file_path", "old_string", "new_string"},
}
@@ -152,20 +159,20 @@ func (e *editTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
var err error
if params.OldString == "" {
response, err = e.createNewFile(ctx, params.FilePath, params.NewString)
response, err = e.createNewFile(ctx, params.FilePath, params.NewString, call)
if err != nil {
return response, err
}
}
if params.NewString == "" {
response, err = e.deleteContent(ctx, params.FilePath, params.OldString)
response, err = e.deleteContent(ctx, params.FilePath, params.OldString, params.ReplaceAll, call)
if err != nil {
return response, err
}
}
response, err = e.replaceContent(ctx, params.FilePath, params.OldString, params.NewString)
response, err = e.replaceContent(ctx, params.FilePath, params.OldString, params.NewString, params.ReplaceAll, call)
if err != nil {
return response, err
}
@@ -182,7 +189,7 @@ func (e *editTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
return response, nil
}
func (e *editTool) createNewFile(ctx context.Context, filePath, content string) (ToolResponse, error) {
func (e *editTool) createNewFile(ctx context.Context, filePath, content string, call ToolCall) (ToolResponse, error) {
fileInfo, err := os.Stat(filePath)
if err == nil {
if fileInfo.IsDir() {
@@ -217,6 +224,7 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
permission.CreatePermissionRequest{
SessionID: sessionID,
Path: permissionPath,
ToolCallID: call.ID,
ToolName: EditToolName,
Action: "write",
Description: fmt.Sprintf("Create file %s", filePath),
@@ -264,7 +272,7 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
), nil
}
func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string) (ToolResponse, error) {
func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string, replaceAll bool, call ToolCall) (ToolResponse, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
@@ -297,17 +305,29 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
oldContent := string(content)
index := strings.Index(oldContent, oldString)
if index == -1 {
return NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
}
var newContent string
var deletionCount int
lastIndex := strings.LastIndex(oldContent, oldString)
if index != lastIndex {
return NewTextErrorResponse("old_string appears multiple times in the file. Please provide more context to ensure a unique match"), nil
}
if replaceAll {
newContent = strings.ReplaceAll(oldContent, oldString, "")
deletionCount = strings.Count(oldContent, oldString)
if deletionCount == 0 {
return NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
}
} else {
index := strings.Index(oldContent, oldString)
if index == -1 {
return NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
}
newContent := oldContent[:index] + oldContent[index+len(oldString):]
lastIndex := strings.LastIndex(oldContent, oldString)
if index != lastIndex {
return NewTextErrorResponse("old_string appears multiple times in the file. Please provide more context to ensure a unique match, or set replace_all to true"), nil
}
newContent = oldContent[:index] + oldContent[index+len(oldString):]
deletionCount = 1
}
sessionID, messageID := GetContextValues(ctx)
@@ -330,6 +350,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
permission.CreatePermissionRequest{
SessionID: sessionID,
Path: permissionPath,
ToolCallID: call.ID,
ToolName: EditToolName,
Action: "write",
Description: fmt.Sprintf("Delete content from file %s", filePath),
@@ -385,7 +406,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
), nil
}
func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newString string) (ToolResponse, error) {
func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newString string, replaceAll bool, call ToolCall) (ToolResponse, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
@@ -418,17 +439,29 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
oldContent := string(content)
index := strings.Index(oldContent, oldString)
if index == -1 {
return NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
}
var newContent string
var replacementCount int
lastIndex := strings.LastIndex(oldContent, oldString)
if index != lastIndex {
return NewTextErrorResponse("old_string appears multiple times in the file. Please provide more context to ensure a unique match"), nil
}
if replaceAll {
newContent = strings.ReplaceAll(oldContent, oldString, newString)
replacementCount = strings.Count(oldContent, oldString)
if replacementCount == 0 {
return NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
}
} else {
index := strings.Index(oldContent, oldString)
if index == -1 {
return NewTextErrorResponse("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks"), nil
}
newContent := oldContent[:index] + newString + oldContent[index+len(oldString):]
lastIndex := strings.LastIndex(oldContent, oldString)
if index != lastIndex {
return NewTextErrorResponse("old_string appears multiple times in the file. Please provide more context to ensure a unique match, or set replace_all to true"), nil
}
newContent = oldContent[:index] + newString + oldContent[index+len(oldString):]
replacementCount = 1
}
if oldContent == newContent {
return NewTextErrorResponse("new content is the same as old content. No changes made."), nil
@@ -452,6 +485,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
permission.CreatePermissionRequest{
SessionID: sessionID,
Path: permissionPath,
ToolCallID: call.ID,
ToolName: EditToolName,
Action: "write",
Description: fmt.Sprintf("Replace content in file %s", filePath),

View File

@@ -136,6 +136,7 @@ func (t *fetchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
permission.CreatePermissionRequest{
SessionID: sessionID,
Path: t.workingDir,
ToolCallID: call.ID,
ToolName: FetchToolName,
Action: "fetch",
Description: fmt.Sprintf("Fetch content from URL: %s", params.URL),

View File

@@ -0,0 +1,467 @@
package tools
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"github.com/charmbracelet/crush/internal/diff"
"github.com/charmbracelet/crush/internal/history"
"github.com/charmbracelet/crush/internal/lsp"
"github.com/charmbracelet/crush/internal/permission"
)
type MultiEditOperation struct {
OldString string `json:"old_string"`
NewString string `json:"new_string"`
ReplaceAll bool `json:"replace_all,omitempty"`
}
type MultiEditParams struct {
FilePath string `json:"file_path"`
Edits []MultiEditOperation `json:"edits"`
}
type MultiEditPermissionsParams struct {
FilePath string `json:"file_path"`
OldContent string `json:"old_content,omitempty"`
NewContent string `json:"new_content,omitempty"`
}
type MultiEditResponseMetadata struct {
Additions int `json:"additions"`
Removals int `json:"removals"`
OldContent string `json:"old_content,omitempty"`
NewContent string `json:"new_content,omitempty"`
EditsApplied int `json:"edits_applied"`
}
type multiEditTool struct {
lspClients map[string]*lsp.Client
permissions permission.Service
files history.Service
workingDir string
}
const (
MultiEditToolName = "multiedit"
multiEditDescription = `This is a tool for making multiple edits to a single file in one operation. It is built on top of the Edit tool and allows you to perform multiple find-and-replace operations efficiently. Prefer this tool over the Edit tool when you need to make multiple edits to the same file.
Before using this tool:
1. Use the Read tool to understand the file's contents and context
2. Verify the directory path is correct
To make multiple file edits, provide the following:
1. file_path: The absolute path to the file to modify (must be absolute, not relative)
2. edits: An array of edit operations to perform, where each edit contains:
- old_string: The text to replace (must match the file contents exactly, including all whitespace and indentation)
- new_string: The edited text to replace the old_string
- replace_all: Replace all occurrences of old_string. This parameter is optional and defaults to false.
IMPORTANT:
- All edits are applied in sequence, in the order they are provided
- Each edit operates on the result of the previous edit
- All edits must be valid for the operation to succeed - if any edit fails, none will be applied
- This tool is ideal when you need to make several changes to different parts of the same file
CRITICAL REQUIREMENTS:
1. All edits follow the same requirements as the single Edit tool
2. The edits are atomic - either all succeed or none are applied
3. Plan your edits carefully to avoid conflicts between sequential operations
WARNING:
- The tool will fail if edits.old_string doesn't match the file contents exactly (including whitespace)
- The tool will fail if edits.old_string and edits.new_string are the same
- Since edits are applied in sequence, ensure that earlier edits don't affect the text that later edits are trying to find
When making edits:
- Ensure all edits result in idiomatic, correct code
- Do not leave the code in a broken state
- Always use absolute file paths (starting with /)
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
- Use replace_all for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.
If you want to create a new file, use:
- A new file path, including dir name if needed
- First edit: empty old_string and the new file's contents as new_string
- Subsequent edits: normal edit operations on the created content`
)
func NewMultiEditTool(lspClients map[string]*lsp.Client, permissions permission.Service, files history.Service, workingDir string) BaseTool {
return &multiEditTool{
lspClients: lspClients,
permissions: permissions,
files: files,
workingDir: workingDir,
}
}
func (m *multiEditTool) Name() string {
return MultiEditToolName
}
func (m *multiEditTool) Info() ToolInfo {
return ToolInfo{
Name: MultiEditToolName,
Description: multiEditDescription,
Parameters: map[string]any{
"file_path": map[string]any{
"type": "string",
"description": "The absolute path to the file to modify",
},
"edits": map[string]any{
"type": "array",
"items": map[string]any{
"type": "object",
"properties": map[string]any{
"old_string": map[string]any{
"type": "string",
"description": "The text to replace",
},
"new_string": map[string]any{
"type": "string",
"description": "The text to replace it with",
},
"replace_all": map[string]any{
"type": "boolean",
"default": false,
"description": "Replace all occurrences of old_string (default false).",
},
},
"required": []string{"old_string", "new_string"},
"additionalProperties": false,
},
"minItems": 1,
"description": "Array of edit operations to perform sequentially on the file",
},
},
Required: []string{"file_path", "edits"},
}
}
func (m *multiEditTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
var params MultiEditParams
if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
return NewTextErrorResponse("invalid parameters"), nil
}
if params.FilePath == "" {
return NewTextErrorResponse("file_path is required"), nil
}
if len(params.Edits) == 0 {
return NewTextErrorResponse("at least one edit operation is required"), nil
}
if !filepath.IsAbs(params.FilePath) {
params.FilePath = filepath.Join(m.workingDir, params.FilePath)
}
// Validate all edits before applying any
if err := m.validateEdits(params.Edits); err != nil {
return NewTextErrorResponse(err.Error()), nil
}
var response ToolResponse
var err error
// Handle file creation case (first edit has empty old_string)
if len(params.Edits) > 0 && params.Edits[0].OldString == "" {
response, err = m.processMultiEditWithCreation(ctx, params, call)
} else {
response, err = m.processMultiEditExistingFile(ctx, params, call)
}
if err != nil {
return response, err
}
if response.IsError {
return response, nil
}
// Wait for LSP diagnostics and add them to the response
waitForLspDiagnostics(ctx, params.FilePath, m.lspClients)
text := fmt.Sprintf("<result>\n%s\n</result>\n", response.Content)
text += getDiagnostics(params.FilePath, m.lspClients)
response.Content = text
return response, nil
}
func (m *multiEditTool) validateEdits(edits []MultiEditOperation) error {
for i, edit := range edits {
if edit.OldString == edit.NewString {
return fmt.Errorf("edit %d: old_string and new_string are identical", i+1)
}
// Only the first edit can have empty old_string (for file creation)
if i > 0 && edit.OldString == "" {
return fmt.Errorf("edit %d: only the first edit can have empty old_string (for file creation)", i+1)
}
}
return nil
}
func (m *multiEditTool) processMultiEditWithCreation(ctx context.Context, params MultiEditParams, call ToolCall) (ToolResponse, error) {
// First edit creates the file
firstEdit := params.Edits[0]
if firstEdit.OldString != "" {
return NewTextErrorResponse("first edit must have empty old_string for file creation"), nil
}
// Check if file already exists
if _, err := os.Stat(params.FilePath); err == nil {
return NewTextErrorResponse(fmt.Sprintf("file already exists: %s", params.FilePath)), nil
} else if !os.IsNotExist(err) {
return ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
}
// Create parent directories
dir := filepath.Dir(params.FilePath)
if err := os.MkdirAll(dir, 0o755); err != nil {
return ToolResponse{}, fmt.Errorf("failed to create parent directories: %w", err)
}
// Start with the content from the first edit
currentContent := firstEdit.NewString
// Apply remaining edits to the content
for i := 1; i < len(params.Edits); i++ {
edit := params.Edits[i]
newContent, err := m.applyEditToContent(currentContent, edit)
if err != nil {
return NewTextErrorResponse(fmt.Sprintf("edit %d failed: %s", i+1, err.Error())), nil
}
currentContent = newContent
}
// Get session and message IDs
sessionID, messageID := GetContextValues(ctx)
if sessionID == "" || messageID == "" {
return ToolResponse{}, fmt.Errorf("session ID and message ID are required for creating a new file")
}
// Check permissions
_, additions, removals := diff.GenerateDiff("", currentContent, strings.TrimPrefix(params.FilePath, m.workingDir))
rootDir := m.workingDir
permissionPath := filepath.Dir(params.FilePath)
if strings.HasPrefix(params.FilePath, rootDir) {
permissionPath = rootDir
}
p := m.permissions.Request(permission.CreatePermissionRequest{
SessionID: sessionID,
Path: permissionPath,
ToolCallID: call.ID,
ToolName: MultiEditToolName,
Action: "write",
Description: fmt.Sprintf("Create file %s with %d edits", params.FilePath, len(params.Edits)),
Params: MultiEditPermissionsParams{
FilePath: params.FilePath,
OldContent: "",
NewContent: currentContent,
},
})
if !p {
return ToolResponse{}, permission.ErrorPermissionDenied
}
// Write the file
err := os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
if err != nil {
return ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
}
// Update file history
_, err = m.files.Create(ctx, sessionID, params.FilePath, "")
if err != nil {
return ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
}
_, err = m.files.CreateVersion(ctx, sessionID, params.FilePath, currentContent)
if err != nil {
slog.Debug("Error creating file history version", "error", err)
}
recordFileWrite(params.FilePath)
recordFileRead(params.FilePath)
return WithResponseMetadata(
NewTextResponse(fmt.Sprintf("File created with %d edits: %s", len(params.Edits), params.FilePath)),
MultiEditResponseMetadata{
OldContent: "",
NewContent: currentContent,
Additions: additions,
Removals: removals,
EditsApplied: len(params.Edits),
},
), nil
}
func (m *multiEditTool) processMultiEditExistingFile(ctx context.Context, params MultiEditParams, call ToolCall) (ToolResponse, error) {
// Validate file exists and is readable
fileInfo, err := os.Stat(params.FilePath)
if err != nil {
if os.IsNotExist(err) {
return NewTextErrorResponse(fmt.Sprintf("file not found: %s", params.FilePath)), nil
}
return ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
}
if fileInfo.IsDir() {
return NewTextErrorResponse(fmt.Sprintf("path is a directory, not a file: %s", params.FilePath)), nil
}
// Check if file was read before editing
if getLastReadTime(params.FilePath).IsZero() {
return NewTextErrorResponse("you must read the file before editing it. Use the View tool first"), nil
}
// Check if file was modified since last read
modTime := fileInfo.ModTime()
lastRead := getLastReadTime(params.FilePath)
if modTime.After(lastRead) {
return NewTextErrorResponse(
fmt.Sprintf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
params.FilePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339),
)), nil
}
// Read current file content
content, err := os.ReadFile(params.FilePath)
if err != nil {
return ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
}
oldContent := string(content)
currentContent := oldContent
// Apply all edits sequentially
for i, edit := range params.Edits {
newContent, err := m.applyEditToContent(currentContent, edit)
if err != nil {
return NewTextErrorResponse(fmt.Sprintf("edit %d failed: %s", i+1, err.Error())), nil
}
currentContent = newContent
}
// Check if content actually changed
if oldContent == currentContent {
return NewTextErrorResponse("no changes made - all edits resulted in identical content"), nil
}
// Get session and message IDs
sessionID, messageID := GetContextValues(ctx)
if sessionID == "" || messageID == "" {
return ToolResponse{}, fmt.Errorf("session ID and message ID are required for editing file")
}
// Generate diff and check permissions
_, additions, removals := diff.GenerateDiff(oldContent, currentContent, strings.TrimPrefix(params.FilePath, m.workingDir))
rootDir := m.workingDir
permissionPath := filepath.Dir(params.FilePath)
if strings.HasPrefix(params.FilePath, rootDir) {
permissionPath = rootDir
}
p := m.permissions.Request(permission.CreatePermissionRequest{
SessionID: sessionID,
Path: permissionPath,
ToolCallID: call.ID,
ToolName: MultiEditToolName,
Action: "write",
Description: fmt.Sprintf("Apply %d edits to file %s", len(params.Edits), params.FilePath),
Params: MultiEditPermissionsParams{
FilePath: params.FilePath,
OldContent: oldContent,
NewContent: currentContent,
},
})
if !p {
return ToolResponse{}, permission.ErrorPermissionDenied
}
// Write the updated content
err = os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
if err != nil {
return ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
}
// Update file history
file, err := m.files.GetByPathAndSession(ctx, params.FilePath, sessionID)
if err != nil {
_, err = m.files.Create(ctx, sessionID, params.FilePath, oldContent)
if err != nil {
return ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
}
}
if file.Content != oldContent {
// User manually changed the content, store an intermediate version
_, err = m.files.CreateVersion(ctx, sessionID, params.FilePath, oldContent)
if err != nil {
slog.Debug("Error creating file history version", "error", err)
}
}
// Store the new version
_, err = m.files.CreateVersion(ctx, sessionID, params.FilePath, currentContent)
if err != nil {
slog.Debug("Error creating file history version", "error", err)
}
recordFileWrite(params.FilePath)
recordFileRead(params.FilePath)
return WithResponseMetadata(
NewTextResponse(fmt.Sprintf("Applied %d edits to file: %s", len(params.Edits), params.FilePath)),
MultiEditResponseMetadata{
OldContent: oldContent,
NewContent: currentContent,
Additions: additions,
Removals: removals,
EditsApplied: len(params.Edits),
},
), nil
}
func (m *multiEditTool) applyEditToContent(content string, edit MultiEditOperation) (string, error) {
if edit.OldString == "" && edit.NewString == "" {
return content, nil
}
if edit.OldString == "" {
return "", fmt.Errorf("old_string cannot be empty for content replacement")
}
var newContent string
var replacementCount int
if edit.ReplaceAll {
newContent = strings.ReplaceAll(content, edit.OldString, edit.NewString)
replacementCount = strings.Count(content, edit.OldString)
if replacementCount == 0 {
return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
}
} else {
index := strings.Index(content, edit.OldString)
if index == -1 {
return "", fmt.Errorf("old_string not found in content. Make sure it matches exactly, including whitespace and line breaks")
}
lastIndex := strings.LastIndex(content, edit.OldString)
if index != lastIndex {
return "", fmt.Errorf("old_string appears multiple times in the content. Please provide more context to ensure a unique match, or set replace_all to true")
}
newContent = content[:index] + edit.NewString + content[index+len(edit.OldString):]
replacementCount = 1
}
return newContent, nil
}

View File

@@ -181,6 +181,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
permission.CreatePermissionRequest{
SessionID: sessionID,
Path: permissionPath,
ToolCallID: call.ID,
ToolName: WriteToolName,
Action: "write",
Description: fmt.Sprintf("Create file %s", filePath),

View File

@@ -1,6 +1,7 @@
package permission
import (
"context"
"errors"
"path/filepath"
"slices"
@@ -15,6 +16,7 @@ var ErrorPermissionDenied = errors.New("permission denied")
type CreatePermissionRequest struct {
SessionID string `json:"session_id"`
ToolCallID string `json:"tool_call_id"`
ToolName string `json:"tool_name"`
Description string `json:"description"`
Action string `json:"action"`
@@ -22,9 +24,16 @@ type CreatePermissionRequest struct {
Path string `json:"path"`
}
type PermissionNotification struct {
ToolCallID string `json:"tool_call_id"`
Granted bool `json:"granted"`
Denied bool `json:"denied"`
}
type PermissionRequest struct {
ID string `json:"id"`
SessionID string `json:"session_id"`
ToolCallID string `json:"tool_call_id"`
ToolName string `json:"tool_name"`
Description string `json:"description"`
Action string `json:"action"`
@@ -39,22 +48,32 @@ type Service interface {
Deny(permission PermissionRequest)
Request(opts CreatePermissionRequest) bool
AutoApproveSession(sessionID string)
SubscribeNotifications(ctx context.Context) <-chan pubsub.Event[PermissionNotification]
}
type permissionService struct {
*pubsub.Broker[PermissionRequest]
notificationBroker *pubsub.Broker[PermissionNotification]
workingDir string
sessionPermissions []PermissionRequest
sessionPermissionsMu sync.RWMutex
pendingRequests *csync.Map[string, chan bool]
autoApproveSessions []string
autoApproveSessions map[string]bool
autoApproveSessionsMu sync.RWMutex
skip bool
allowedTools []string
// used to make sure we only process one request at a time
requestMu sync.Mutex
activeRequest *PermissionRequest
}
func (s *permissionService) GrantPersistent(permission PermissionRequest) {
s.notificationBroker.Publish(pubsub.CreatedEvent, PermissionNotification{
ToolCallID: permission.ToolCallID,
Granted: true,
})
respCh, ok := s.pendingRequests.Get(permission.ID)
if ok {
respCh <- true
@@ -63,20 +82,41 @@ func (s *permissionService) GrantPersistent(permission PermissionRequest) {
s.sessionPermissionsMu.Lock()
s.sessionPermissions = append(s.sessionPermissions, permission)
s.sessionPermissionsMu.Unlock()
if s.activeRequest != nil && s.activeRequest.ID == permission.ID {
s.activeRequest = nil
}
}
func (s *permissionService) Grant(permission PermissionRequest) {
s.notificationBroker.Publish(pubsub.CreatedEvent, PermissionNotification{
ToolCallID: permission.ToolCallID,
Granted: true,
})
respCh, ok := s.pendingRequests.Get(permission.ID)
if ok {
respCh <- true
}
if s.activeRequest != nil && s.activeRequest.ID == permission.ID {
s.activeRequest = nil
}
}
func (s *permissionService) Deny(permission PermissionRequest) {
s.notificationBroker.Publish(pubsub.CreatedEvent, PermissionNotification{
ToolCallID: permission.ToolCallID,
Granted: false,
Denied: true,
})
respCh, ok := s.pendingRequests.Get(permission.ID)
if ok {
respCh <- false
}
if s.activeRequest != nil && s.activeRequest.ID == permission.ID {
s.activeRequest = nil
}
}
func (s *permissionService) Request(opts CreatePermissionRequest) bool {
@@ -84,6 +124,13 @@ func (s *permissionService) Request(opts CreatePermissionRequest) bool {
return true
}
// tell the UI that a permission was requested
s.notificationBroker.Publish(pubsub.CreatedEvent, PermissionNotification{
ToolCallID: opts.ToolCallID,
})
s.requestMu.Lock()
defer s.requestMu.Unlock()
// Check if the tool/action combination is in the allowlist
commandKey := opts.ToolName + ":" + opts.Action
if slices.Contains(s.allowedTools, commandKey) || slices.Contains(s.allowedTools, opts.ToolName) {
@@ -91,7 +138,7 @@ func (s *permissionService) Request(opts CreatePermissionRequest) bool {
}
s.autoApproveSessionsMu.RLock()
autoApprove := slices.Contains(s.autoApproveSessions, opts.SessionID)
autoApprove := s.autoApproveSessions[opts.SessionID]
s.autoApproveSessionsMu.RUnlock()
if autoApprove {
@@ -106,6 +153,7 @@ func (s *permissionService) Request(opts CreatePermissionRequest) bool {
ID: uuid.New().String(),
Path: dir,
SessionID: opts.SessionID,
ToolCallID: opts.ToolCallID,
ToolName: opts.ToolName,
Description: opts.Description,
Action: opts.Action,
@@ -121,30 +169,46 @@ func (s *permissionService) Request(opts CreatePermissionRequest) bool {
}
s.sessionPermissionsMu.RUnlock()
respCh := make(chan bool, 1)
s.sessionPermissionsMu.RLock()
for _, p := range s.sessionPermissions {
if p.ToolName == permission.ToolName && p.Action == permission.Action && p.SessionID == permission.SessionID && p.Path == permission.Path {
s.sessionPermissionsMu.RUnlock()
return true
}
}
s.sessionPermissionsMu.RUnlock()
s.activeRequest = &permission
respCh := make(chan bool, 1)
s.pendingRequests.Set(permission.ID, respCh)
defer s.pendingRequests.Del(permission.ID)
// Publish the request
s.Publish(pubsub.CreatedEvent, permission)
// Wait for the response indefinitely
return <-respCh
}
func (s *permissionService) AutoApproveSession(sessionID string) {
s.autoApproveSessionsMu.Lock()
s.autoApproveSessions = append(s.autoApproveSessions, sessionID)
s.autoApproveSessions[sessionID] = true
s.autoApproveSessionsMu.Unlock()
}
func (s *permissionService) SubscribeNotifications(ctx context.Context) <-chan pubsub.Event[PermissionNotification] {
return s.notificationBroker.Subscribe(ctx)
}
func NewPermissionService(workingDir string, skip bool, allowedTools []string) Service {
return &permissionService{
Broker: pubsub.NewBroker[PermissionRequest](),
workingDir: workingDir,
sessionPermissions: make([]PermissionRequest, 0),
skip: skip,
allowedTools: allowedTools,
pendingRequests: csync.NewMap[string, chan bool](),
Broker: pubsub.NewBroker[PermissionRequest](),
notificationBroker: pubsub.NewBroker[PermissionNotification](),
workingDir: workingDir,
sessionPermissions: make([]PermissionRequest, 0),
autoApproveSessions: make(map[string]bool),
skip: skip,
allowedTools: allowedTools,
pendingRequests: csync.NewMap[string, chan bool](),
}
}

View File

@@ -1,7 +1,10 @@
package permission
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPermissionService_AllowedCommands(t *testing.T) {
@@ -90,3 +93,159 @@ func TestPermissionService_SkipMode(t *testing.T) {
t.Error("expected permission to be granted in skip mode")
}
}
func TestPermissionService_SequentialProperties(t *testing.T) {
t.Run("Sequential permission requests with persistent grants", func(t *testing.T) {
service := NewPermissionService("/tmp", false, []string{})
req1 := CreatePermissionRequest{
SessionID: "session1",
ToolName: "file_tool",
Description: "Read file",
Action: "read",
Params: map[string]string{"file": "test.txt"},
Path: "/tmp/test.txt",
}
var result1 bool
var wg sync.WaitGroup
wg.Add(1)
events := service.Subscribe(t.Context())
go func() {
defer wg.Done()
result1 = service.Request(req1)
}()
var permissionReq PermissionRequest
event := <-events
permissionReq = event.Payload
service.GrantPersistent(permissionReq)
wg.Wait()
assert.True(t, result1, "First request should be granted")
// Second identical request should be automatically approved due to persistent permission
req2 := CreatePermissionRequest{
SessionID: "session1",
ToolName: "file_tool",
Description: "Read file again",
Action: "read",
Params: map[string]string{"file": "test.txt"},
Path: "/tmp/test.txt",
}
result2 := service.Request(req2)
assert.True(t, result2, "Second request should be auto-approved")
})
t.Run("Sequential requests with temporary grants", func(t *testing.T) {
service := NewPermissionService("/tmp", false, []string{})
req := CreatePermissionRequest{
SessionID: "session2",
ToolName: "file_tool",
Description: "Write file",
Action: "write",
Params: map[string]string{"file": "test.txt"},
Path: "/tmp/test.txt",
}
events := service.Subscribe(t.Context())
var result1 bool
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
result1 = service.Request(req)
}()
var permissionReq PermissionRequest
event := <-events
permissionReq = event.Payload
service.Grant(permissionReq)
wg.Wait()
assert.True(t, result1, "First request should be granted")
var result2 bool
wg.Add(1)
go func() {
defer wg.Done()
result2 = service.Request(req)
}()
event = <-events
permissionReq = event.Payload
service.Deny(permissionReq)
wg.Wait()
assert.False(t, result2, "Second request should be denied")
})
t.Run("Concurrent requests with different outcomes", func(t *testing.T) {
service := NewPermissionService("/tmp", false, []string{})
events := service.Subscribe(t.Context())
var wg sync.WaitGroup
results := make([]bool, 0)
requests := []CreatePermissionRequest{
{
SessionID: "concurrent1",
ToolName: "tool1",
Action: "action1",
Path: "/tmp/file1.txt",
Description: "First concurrent request",
},
{
SessionID: "concurrent2",
ToolName: "tool2",
Action: "action2",
Path: "/tmp/file2.txt",
Description: "Second concurrent request",
},
{
SessionID: "concurrent3",
ToolName: "tool3",
Action: "action3",
Path: "/tmp/file3.txt",
Description: "Third concurrent request",
},
}
for i, req := range requests {
wg.Add(1)
go func(index int, request CreatePermissionRequest) {
defer wg.Done()
results = append(results, service.Request(request))
}(i, req)
}
for range 3 {
event := <-events
switch event.Payload.ToolName {
case "tool1":
service.Grant(event.Payload)
case "tool2":
service.GrantPersistent(event.Payload)
case "tool3":
service.Deny(event.Payload)
}
}
wg.Wait()
grantedCount := 0
for _, result := range results {
if result {
grantedCount++
}
}
assert.Equal(t, 2, grantedCount, "Should have 2 granted and 1 denied")
secondReq := requests[1]
secondReq.Description = "Repeat of second request"
result := service.Request(secondReq)
assert.True(t, result, "Repeated request should be auto-approved due to persistent permission")
})
}

View File

@@ -2,6 +2,7 @@ package chat
import (
"context"
"log/slog"
"time"
"github.com/charmbracelet/bubbles/v2/key"
@@ -9,6 +10,7 @@ import (
"github.com/charmbracelet/crush/internal/app"
"github.com/charmbracelet/crush/internal/llm/agent"
"github.com/charmbracelet/crush/internal/message"
"github.com/charmbracelet/crush/internal/permission"
"github.com/charmbracelet/crush/internal/pubsub"
"github.com/charmbracelet/crush/internal/session"
"github.com/charmbracelet/crush/internal/tui/components/chat/messages"
@@ -85,6 +87,8 @@ func (m *messageListCmp) Init() tea.Cmd {
// Update handles incoming messages and updates the component state.
func (m *messageListCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case pubsub.Event[permission.PermissionNotification]:
return m, m.handlePermissionRequest(msg.Payload)
case SessionSelectedMsg:
if msg.ID != m.session.ID {
cmd := m.SetSession(msg)
@@ -124,6 +128,20 @@ func (m *messageListCmp) View() string {
)
}
func (m *messageListCmp) handlePermissionRequest(permission permission.PermissionNotification) tea.Cmd {
items := m.listCmp.Items()
slog.Info("Handling permission request", "tool_call_id", permission.ToolCallID, "granted", permission.Granted)
if toolCallIndex := m.findToolCallByID(items, permission.ToolCallID); toolCallIndex != NotFound {
toolCall := items[toolCallIndex].(messages.ToolCallCmp)
toolCall.SetPermissionRequested()
if permission.Granted {
toolCall.SetPermissionGranted()
}
m.listCmp.UpdateItem(toolCall.ID(), toolCall)
}
return nil
}
// handleChildSession handles messages from child sessions (agent tools).
func (m *messageListCmp) handleChildSession(event pubsub.Event[message.Message]) tea.Cmd {
var cmds []tea.Cmd
@@ -158,6 +176,7 @@ func (m *messageListCmp) handleChildSession(event pubsub.Event[message.Message])
nestedCall := messages.NewToolCallCmp(
event.Payload.ID,
tc,
m.app.Permissions,
messages.WithToolCallNested(true),
)
cmds = append(cmds, nestedCall.Init())
@@ -199,7 +218,12 @@ func (m *messageListCmp) handleMessageEvent(event pubsub.Event[message.Message])
if event.Payload.SessionID != m.session.ID {
return m.handleChildSession(event)
}
return m.handleUpdateAssistantMessage(event.Payload)
switch event.Payload.Role {
case message.Assistant:
return m.handleUpdateAssistantMessage(event.Payload)
case message.Tool:
return m.handleToolMessage(event.Payload)
}
}
return nil
}
@@ -371,7 +395,7 @@ func (m *messageListCmp) updateOrAddToolCall(msg message.Message, tc message.Too
}
// Add new tool call if not found
return m.listCmp.AppendItem(messages.NewToolCallCmp(msg.ID, tc))
return m.listCmp.AppendItem(messages.NewToolCallCmp(msg.ID, tc, m.app.Permissions))
}
// handleNewAssistantMessage processes new assistant messages and their tool calls.
@@ -390,7 +414,7 @@ func (m *messageListCmp) handleNewAssistantMessage(msg message.Message) tea.Cmd
// Add tool calls
for _, tc := range msg.ToolCalls() {
cmd := m.listCmp.AppendItem(messages.NewToolCallCmp(msg.ID, tc))
cmd := m.listCmp.AppendItem(messages.NewToolCallCmp(msg.ID, tc, m.app.Permissions))
cmds = append(cmds, cmd)
}
@@ -473,11 +497,12 @@ func (m *messageListCmp) convertAssistantMessage(msg message.Message, toolResult
// Add tool calls with their results and status
for _, tc := range msg.ToolCalls() {
options := m.buildToolCallOptions(tc, msg, toolResultMap)
uiMessages = append(uiMessages, messages.NewToolCallCmp(msg.ID, tc, options...))
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)
nestedUIMessages := m.convertMessagesToUI(nestedMessages, make(map[string]message.ToolResult))
nestedToolResultMap := m.buildToolResultMap(nestedMessages)
nestedUIMessages := m.convertMessagesToUI(nestedMessages, nestedToolResultMap)
nestedToolCalls := make([]messages.ToolCallCmp, 0, len(nestedUIMessages))
for _, nestedMsg := range nestedUIMessages {
if toolCall, ok := nestedMsg.(messages.ToolCallCmp); ok {

View File

@@ -2,8 +2,10 @@ package editor
import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
@@ -80,7 +82,7 @@ const (
maxAttachments = 5
)
type openEditorMsg struct {
type OpenEditorMsg struct {
Text string
}
@@ -119,7 +121,7 @@ func (m *editorCmp) openEditor(value string) tea.Cmd {
return util.ReportWarn("Message is empty")
}
os.Remove(tmpfile.Name())
return openEditorMsg{
return OpenEditorMsg{
Text: strings.TrimSpace(string(content)),
}
})
@@ -204,9 +206,47 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.completionsStartIndex = 0
}
}
case openEditorMsg:
case OpenEditorMsg:
m.textarea.SetValue(msg.Text)
m.textarea.MoveToEnd()
case tea.PasteMsg:
path := strings.ReplaceAll(string(msg), "\\ ", " ")
// try to get an image
path, err := filepath.Abs(path)
if err != nil {
m.textarea, cmd = m.textarea.Update(msg)
return m, cmd
}
isAllowedType := false
for _, ext := range filepicker.AllowedTypes {
if strings.HasSuffix(path, ext) {
isAllowedType = true
break
}
}
if !isAllowedType {
m.textarea, cmd = m.textarea.Update(msg)
return m, cmd
}
tooBig, _ := filepicker.IsFileTooBig(path, filepicker.MaxAttachmentSize)
if tooBig {
m.textarea, cmd = m.textarea.Update(msg)
return m, cmd
}
content, err := os.ReadFile(path)
if err != nil {
m.textarea, cmd = m.textarea.Update(msg)
return m, cmd
}
mimeBufferSize := min(512, len(content))
mimeType := http.DetectContentType(content[:mimeBufferSize])
fileName := filepath.Base(path)
attachment := message.Attachment{FilePath: path, FileName: fileName, MimeType: mimeType, Content: content}
return m, util.CmdHandler(filepicker.FilePickedMsg{
Attachment: attachment,
})
case tea.KeyPressMsg:
cur := m.textarea.Cursor()
curIdx := m.textarea.Width()*cur.Y + cur.X

View File

@@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/charmbracelet/bubbles/v2/key"
"github.com/charmbracelet/bubbles/v2/viewport"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/catwalk/pkg/catwalk"
@@ -13,6 +14,7 @@ import (
"github.com/charmbracelet/x/ansi"
"github.com/google/uuid"
"github.com/atotto/clipboard"
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/message"
"github.com/charmbracelet/crush/internal/tui/components/anim"
@@ -23,6 +25,8 @@ import (
"github.com/charmbracelet/crush/internal/tui/util"
)
var copyKey = key.NewBinding(key.WithKeys("c", "y", "C", "Y"), key.WithHelp("c/y", "copy"))
// MessageCmp defines the interface for message components in the chat interface.
// It combines standard UI model interfaces with message-specific functionality.
type MessageCmp interface {
@@ -94,6 +98,14 @@ func (m *messageCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.anim = u.(*anim.Anim)
return m, cmd
}
case tea.KeyPressMsg:
if key.Matches(msg, copyKey) {
err := clipboard.WriteAll(m.message.Content().Text)
if err != nil {
return m, util.ReportError(fmt.Errorf("failed to copy message content to clipboard: %w", err))
}
return m, util.ReportInfo("Message copied to clipboard")
}
}
return m, nil
}

View File

@@ -166,6 +166,7 @@ func init() {
registry.register(tools.DownloadToolName, func() renderer { return downloadRenderer{} })
registry.register(tools.ViewToolName, func() renderer { return viewRenderer{} })
registry.register(tools.EditToolName, func() renderer { return editRenderer{} })
registry.register(tools.MultiEditToolName, func() renderer { return multiEditRenderer{} })
registry.register(tools.WriteToolName, func() renderer { return writeRenderer{} })
registry.register(tools.FetchToolName, func() renderer { return fetchRenderer{} })
registry.register(tools.GlobToolName, func() renderer { return globRenderer{} })
@@ -294,6 +295,57 @@ func (er editRenderer) Render(v *toolCallCmp) string {
return renderPlainContent(v, v.result.Content)
}
formatter := core.DiffFormatter().
Before(fsext.PrettyPath(params.FilePath), meta.OldContent).
After(fsext.PrettyPath(params.FilePath), meta.NewContent).
Width(v.textWidth() - 2) // -2 for padding
if v.textWidth() > 120 {
formatter = formatter.Split()
}
// add a message to the bottom if the content was truncated
formatted := formatter.String()
if lipgloss.Height(formatted) > responseContextHeight {
contentLines := strings.Split(formatted, "\n")
truncateMessage := t.S().Muted.
Background(t.BgBaseLighter).
PaddingLeft(2).
Width(v.textWidth() - 2).
Render(fmt.Sprintf("… (%d lines)", len(contentLines)-responseContextHeight))
formatted = strings.Join(contentLines[:responseContextHeight], "\n") + "\n" + truncateMessage
}
return formatted
})
}
// -----------------------------------------------------------------------------
// Multi-Edit renderer
// -----------------------------------------------------------------------------
// multiEditRenderer handles multiple file edits with diff visualization
type multiEditRenderer struct {
baseRenderer
}
// Render displays the multi-edited file with a formatted diff of changes
func (mer multiEditRenderer) Render(v *toolCallCmp) string {
t := styles.CurrentTheme()
var params tools.MultiEditParams
var args []string
if err := mer.unmarshalParams(v.call.Input, &params); err == nil {
file := fsext.PrettyPath(params.FilePath)
editsCount := len(params.Edits)
args = newParamBuilder().
addMain(file).
addKeyValue("edits", fmt.Sprintf("%d", editsCount)).
build()
}
return mer.renderWithParams(v, "Multi-Edit", args, func() string {
var meta tools.MultiEditResponseMetadata
if err := mer.unmarshalParams(v.result.Metadata, &meta); err != nil {
return renderPlainContent(v, v.result.Content)
}
formatter := core.DiffFormatter().
Before(fsext.PrettyPath(params.FilePath), meta.OldContent).
After(fsext.PrettyPath(params.FilePath), meta.NewContent).
@@ -672,7 +724,11 @@ func earlyState(header string, v *toolCallCmp) (string, bool) {
case v.cancelled:
message = t.S().Base.Foreground(t.FgSubtle).Render("Canceled.")
case v.result.ToolCallID == "":
message = t.S().Base.Foreground(t.FgSubtle).Render("Waiting for tool to start...")
if v.permissionRequested && !v.permissionGranted {
message = t.S().Base.Foreground(t.FgSubtle).Render("Requesting for permission...")
} else {
message = t.S().Base.Foreground(t.FgSubtle).Render("Waiting for tool response...")
}
default:
return "", false
}
@@ -799,6 +855,8 @@ func prettifyToolName(name string) string {
return "Download"
case tools.EditToolName:
return "Edit"
case tools.MultiEditToolName:
return "Multi-Edit"
case tools.FetchToolName:
return "Fetch"
case tools.GlobToolName:

View File

@@ -1,10 +1,21 @@
package messages
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/charmbracelet/bubbles/v2/key"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/crush/internal/diff"
"github.com/charmbracelet/crush/internal/fsext"
"github.com/charmbracelet/crush/internal/llm/agent"
"github.com/charmbracelet/crush/internal/llm/tools"
"github.com/charmbracelet/crush/internal/message"
"github.com/charmbracelet/crush/internal/permission"
"github.com/charmbracelet/crush/internal/tui/components/anim"
"github.com/charmbracelet/crush/internal/tui/components/core/layout"
"github.com/charmbracelet/crush/internal/tui/styles"
@@ -30,6 +41,8 @@ type ToolCallCmp interface {
SetNestedToolCalls([]ToolCallCmp) // Set nested tool calls
SetIsNested(bool) // Set whether this tool call is nested
ID() string
SetPermissionRequested() // Mark permission request
SetPermissionGranted() // Mark permission granted
}
// toolCallCmp implements the ToolCallCmp interface for displaying tool calls.
@@ -40,10 +53,12 @@ type toolCallCmp struct {
isNested bool // Whether this tool call is nested within another
// Tool call data and state
parentMessageID string // ID of the message that initiated this tool call
call message.ToolCall // The tool call being executed
result message.ToolResult // The result of the tool execution
cancelled bool // Whether the tool call was cancelled
parentMessageID string // ID of the message that initiated this tool call
call message.ToolCall // The tool call being executed
result message.ToolResult // The result of the tool execution
cancelled bool // Whether the tool call was cancelled
permissionRequested bool
permissionGranted bool
// Animation state for pending tool calls
spinning bool // Whether to show loading animation
@@ -81,9 +96,21 @@ func WithToolCallNestedCalls(calls []ToolCallCmp) ToolCallOption {
}
}
func WithToolPermissionRequested() ToolCallOption {
return func(m *toolCallCmp) {
m.permissionRequested = true
}
}
func WithToolPermissionGranted() ToolCallOption {
return func(m *toolCallCmp) {
m.permissionGranted = true
}
}
// NewToolCallCmp creates a new tool call component with the given parent message ID,
// tool call, and optional configuration
func NewToolCallCmp(parentMessageID string, tc message.ToolCall, opts ...ToolCallOption) ToolCallCmp {
func NewToolCallCmp(parentMessageID string, tc message.ToolCall, permissions permission.Service, opts ...ToolCallOption) ToolCallCmp {
m := &toolCallCmp{
call: tc,
parentMessageID: parentMessageID,
@@ -137,6 +164,10 @@ func (m *toolCallCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
case tea.KeyPressMsg:
if key.Matches(msg, copyKey) {
return m, m.copyTool()
}
}
return m, nil
}
@@ -165,6 +196,456 @@ func (m *toolCallCmp) SetCancelled() {
m.cancelled = true
}
func (m *toolCallCmp) copyTool() tea.Cmd {
content := m.formatToolForCopy()
err := clipboard.WriteAll(content)
if err != nil {
return util.ReportError(fmt.Errorf("failed to copy tool content to clipboard: %w", err))
}
return util.ReportInfo("Tool content copied to clipboard")
}
func (m *toolCallCmp) formatToolForCopy() string {
var parts []string
toolName := prettifyToolName(m.call.Name)
parts = append(parts, fmt.Sprintf("## %s Tool Call", toolName))
if m.call.Input != "" {
params := m.formatParametersForCopy()
if params != "" {
parts = append(parts, "### Parameters:")
parts = append(parts, params)
}
}
if m.result.ToolCallID != "" {
if m.result.IsError {
parts = append(parts, "### Error:")
parts = append(parts, m.result.Content)
} else {
parts = append(parts, "### Result:")
content := m.formatResultForCopy()
if content != "" {
parts = append(parts, content)
}
}
} else if m.cancelled {
parts = append(parts, "### Status:")
parts = append(parts, "Cancelled")
} else {
parts = append(parts, "### Status:")
parts = append(parts, "Pending...")
}
return strings.Join(parts, "\n\n")
}
func (m *toolCallCmp) formatParametersForCopy() string {
switch m.call.Name {
case tools.BashToolName:
var params tools.BashParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
cmd := strings.ReplaceAll(params.Command, "\n", " ")
cmd = strings.ReplaceAll(cmd, "\t", " ")
return fmt.Sprintf("**Command:** %s", cmd)
}
case tools.ViewToolName:
var params tools.ViewParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**File:** %s", fsext.PrettyPath(params.FilePath)))
if params.Limit > 0 {
parts = append(parts, fmt.Sprintf("**Limit:** %d", params.Limit))
}
if params.Offset > 0 {
parts = append(parts, fmt.Sprintf("**Offset:** %d", params.Offset))
}
return strings.Join(parts, "\n")
}
case tools.EditToolName:
var params tools.EditParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
return fmt.Sprintf("**File:** %s", fsext.PrettyPath(params.FilePath))
}
case tools.MultiEditToolName:
var params tools.MultiEditParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**File:** %s", fsext.PrettyPath(params.FilePath)))
parts = append(parts, fmt.Sprintf("**Edits:** %d", len(params.Edits)))
return strings.Join(parts, "\n")
}
case tools.WriteToolName:
var params tools.WriteParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
return fmt.Sprintf("**File:** %s", fsext.PrettyPath(params.FilePath))
}
case tools.FetchToolName:
var params tools.FetchParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**URL:** %s", params.URL))
if params.Format != "" {
parts = append(parts, fmt.Sprintf("**Format:** %s", params.Format))
}
if params.Timeout > 0 {
parts = append(parts, fmt.Sprintf("**Timeout:** %s", (time.Duration(params.Timeout)*time.Second).String()))
}
return strings.Join(parts, "\n")
}
case tools.GrepToolName:
var params tools.GrepParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**Pattern:** %s", params.Pattern))
if params.Path != "" {
parts = append(parts, fmt.Sprintf("**Path:** %s", params.Path))
}
if params.Include != "" {
parts = append(parts, fmt.Sprintf("**Include:** %s", params.Include))
}
if params.LiteralText {
parts = append(parts, "**Literal:** true")
}
return strings.Join(parts, "\n")
}
case tools.GlobToolName:
var params tools.GlobParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**Pattern:** %s", params.Pattern))
if params.Path != "" {
parts = append(parts, fmt.Sprintf("**Path:** %s", params.Path))
}
return strings.Join(parts, "\n")
}
case tools.LSToolName:
var params tools.LSParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
path := params.Path
if path == "" {
path = "."
}
return fmt.Sprintf("**Path:** %s", fsext.PrettyPath(path))
}
case tools.DownloadToolName:
var params tools.DownloadParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**URL:** %s", params.URL))
parts = append(parts, fmt.Sprintf("**File Path:** %s", fsext.PrettyPath(params.FilePath)))
if params.Timeout > 0 {
parts = append(parts, fmt.Sprintf("**Timeout:** %s", (time.Duration(params.Timeout)*time.Second).String()))
}
return strings.Join(parts, "\n")
}
case tools.SourcegraphToolName:
var params tools.SourcegraphParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
parts = append(parts, fmt.Sprintf("**Query:** %s", params.Query))
if params.Count > 0 {
parts = append(parts, fmt.Sprintf("**Count:** %d", params.Count))
}
if params.ContextWindow > 0 {
parts = append(parts, fmt.Sprintf("**Context:** %d", params.ContextWindow))
}
return strings.Join(parts, "\n")
}
case tools.DiagnosticsToolName:
return "**Project:** diagnostics"
case agent.AgentToolName:
var params agent.AgentParams
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
return fmt.Sprintf("**Task:**\n%s", params.Prompt)
}
}
var params map[string]any
if json.Unmarshal([]byte(m.call.Input), &params) == nil {
var parts []string
for key, value := range params {
displayKey := strings.ReplaceAll(key, "_", " ")
if len(displayKey) > 0 {
displayKey = strings.ToUpper(displayKey[:1]) + displayKey[1:]
}
parts = append(parts, fmt.Sprintf("**%s:** %v", displayKey, value))
}
return strings.Join(parts, "\n")
}
return ""
}
func (m *toolCallCmp) formatResultForCopy() string {
switch m.call.Name {
case tools.BashToolName:
return m.formatBashResultForCopy()
case tools.ViewToolName:
return m.formatViewResultForCopy()
case tools.EditToolName:
return m.formatEditResultForCopy()
case tools.MultiEditToolName:
return m.formatMultiEditResultForCopy()
case tools.WriteToolName:
return m.formatWriteResultForCopy()
case tools.FetchToolName:
return m.formatFetchResultForCopy()
case agent.AgentToolName:
return m.formatAgentResultForCopy()
case tools.DownloadToolName, tools.GrepToolName, tools.GlobToolName, tools.LSToolName, tools.SourcegraphToolName, tools.DiagnosticsToolName:
return fmt.Sprintf("```\n%s\n```", m.result.Content)
default:
return m.result.Content
}
}
func (m *toolCallCmp) formatBashResultForCopy() string {
var meta tools.BashResponseMetadata
if m.result.Metadata != "" {
json.Unmarshal([]byte(m.result.Metadata), &meta)
}
output := meta.Output
if output == "" && m.result.Content != tools.BashNoOutput {
output = m.result.Content
}
if output == "" {
return ""
}
return fmt.Sprintf("```bash\n%s\n```", output)
}
func (m *toolCallCmp) formatViewResultForCopy() string {
var meta tools.ViewResponseMetadata
if m.result.Metadata != "" {
json.Unmarshal([]byte(m.result.Metadata), &meta)
}
if meta.Content == "" {
return m.result.Content
}
lang := ""
if meta.FilePath != "" {
ext := strings.ToLower(filepath.Ext(meta.FilePath))
switch ext {
case ".go":
lang = "go"
case ".js", ".mjs":
lang = "javascript"
case ".ts":
lang = "typescript"
case ".py":
lang = "python"
case ".rs":
lang = "rust"
case ".java":
lang = "java"
case ".c":
lang = "c"
case ".cpp", ".cc", ".cxx":
lang = "cpp"
case ".sh", ".bash":
lang = "bash"
case ".json":
lang = "json"
case ".yaml", ".yml":
lang = "yaml"
case ".xml":
lang = "xml"
case ".html":
lang = "html"
case ".css":
lang = "css"
case ".md":
lang = "markdown"
}
}
var result strings.Builder
if lang != "" {
result.WriteString(fmt.Sprintf("```%s\n", lang))
} else {
result.WriteString("```\n")
}
result.WriteString(meta.Content)
result.WriteString("\n```")
return result.String()
}
func (m *toolCallCmp) formatEditResultForCopy() string {
var meta tools.EditResponseMetadata
if m.result.Metadata == "" {
return m.result.Content
}
if json.Unmarshal([]byte(m.result.Metadata), &meta) != nil {
return m.result.Content
}
var params tools.EditParams
json.Unmarshal([]byte(m.call.Input), &params)
var result strings.Builder
if meta.OldContent != "" || meta.NewContent != "" {
fileName := params.FilePath
if fileName != "" {
fileName = fsext.PrettyPath(fileName)
}
diffContent, additions, removals := diff.GenerateDiff(meta.OldContent, meta.NewContent, fileName)
result.WriteString(fmt.Sprintf("Changes: +%d -%d\n", additions, removals))
result.WriteString("```diff\n")
result.WriteString(diffContent)
result.WriteString("\n```")
}
return result.String()
}
func (m *toolCallCmp) formatMultiEditResultForCopy() string {
var meta tools.MultiEditResponseMetadata
if m.result.Metadata == "" {
return m.result.Content
}
if json.Unmarshal([]byte(m.result.Metadata), &meta) != nil {
return m.result.Content
}
var params tools.MultiEditParams
json.Unmarshal([]byte(m.call.Input), &params)
var result strings.Builder
if meta.OldContent != "" || meta.NewContent != "" {
fileName := params.FilePath
if fileName != "" {
fileName = fsext.PrettyPath(fileName)
}
diffContent, additions, removals := diff.GenerateDiff(meta.OldContent, meta.NewContent, fileName)
result.WriteString(fmt.Sprintf("Changes: +%d -%d\n", additions, removals))
result.WriteString("```diff\n")
result.WriteString(diffContent)
result.WriteString("\n```")
}
return result.String()
}
func (m *toolCallCmp) formatWriteResultForCopy() string {
var params tools.WriteParams
if json.Unmarshal([]byte(m.call.Input), &params) != nil {
return m.result.Content
}
lang := ""
if params.FilePath != "" {
ext := strings.ToLower(filepath.Ext(params.FilePath))
switch ext {
case ".go":
lang = "go"
case ".js", ".mjs":
lang = "javascript"
case ".ts":
lang = "typescript"
case ".py":
lang = "python"
case ".rs":
lang = "rust"
case ".java":
lang = "java"
case ".c":
lang = "c"
case ".cpp", ".cc", ".cxx":
lang = "cpp"
case ".sh", ".bash":
lang = "bash"
case ".json":
lang = "json"
case ".yaml", ".yml":
lang = "yaml"
case ".xml":
lang = "xml"
case ".html":
lang = "html"
case ".css":
lang = "css"
case ".md":
lang = "markdown"
}
}
var result strings.Builder
result.WriteString(fmt.Sprintf("File: %s\n", fsext.PrettyPath(params.FilePath)))
if lang != "" {
result.WriteString(fmt.Sprintf("```%s\n", lang))
} else {
result.WriteString("```\n")
}
result.WriteString(params.Content)
result.WriteString("\n```")
return result.String()
}
func (m *toolCallCmp) formatFetchResultForCopy() string {
var params tools.FetchParams
if json.Unmarshal([]byte(m.call.Input), &params) != nil {
return m.result.Content
}
var result strings.Builder
if params.URL != "" {
result.WriteString(fmt.Sprintf("URL: %s\n", params.URL))
}
switch params.Format {
case "html":
result.WriteString("```html\n")
case "text":
result.WriteString("```\n")
default: // markdown
result.WriteString("```markdown\n")
}
result.WriteString(m.result.Content)
result.WriteString("\n```")
return result.String()
}
func (m *toolCallCmp) formatAgentResultForCopy() string {
var result strings.Builder
if len(m.nestedToolCalls) > 0 {
result.WriteString("### Nested Tool Calls:\n")
for i, nestedCall := range m.nestedToolCalls {
nestedContent := nestedCall.(*toolCallCmp).formatToolForCopy()
indentedContent := strings.ReplaceAll(nestedContent, "\n", "\n ")
result.WriteString(fmt.Sprintf("%d. %s\n", i+1, indentedContent))
if i < len(m.nestedToolCalls)-1 {
result.WriteString("\n")
}
}
if m.result.Content != "" {
result.WriteString("\n### Final Result:\n")
}
}
if m.result.Content != "" {
result.WriteString(fmt.Sprintf("```markdown\n%s\n```", m.result.Content))
}
return result.String()
}
// SetToolCall updates the tool call data and stops spinning if finished
func (m *toolCallCmp) SetToolCall(call message.ToolCall) {
m.call = call
@@ -316,3 +797,13 @@ func (m *toolCallCmp) Spinning() bool {
func (m *toolCallCmp) ID() string {
return m.call.ID
}
// SetPermissionRequested marks that a permission request was made for this tool call
func (m *toolCallCmp) SetPermissionRequested() {
m.permissionRequested = true
}
// SetPermissionGranted marks that permission was granted for this tool call
func (m *toolCallCmp) SetPermissionGranted() {
m.permissionGranted = true
}

View File

@@ -562,6 +562,8 @@ func (s *splashCmp) infoSection() string {
lipgloss.Left,
s.cwd(),
"",
s.currentModelBlock(),
"",
lipgloss.JoinHorizontal(lipgloss.Left, s.lspBlock(), s.mcpBlock()),
"",
),
@@ -740,6 +742,24 @@ func (s *splashCmp) mcpBlock() string {
)
}
func (s *splashCmp) currentModelBlock() string {
cfg := config.Get()
agentCfg := cfg.Agents["coder"]
model := config.Get().GetModelByType(agentCfg.Model)
t := styles.CurrentTheme()
modelIcon := t.S().Base.Foreground(t.FgSubtle).Render(styles.ModelIcon)
modelName := t.S().Text.Render(model.Name)
modelInfo := fmt.Sprintf("%s %s", modelIcon, modelName)
parts := []string{
modelInfo,
}
return lipgloss.JoinVertical(
lipgloss.Left,
parts...,
)
}
func (s *splashCmp) IsShowingAPIKey() bool {
return s.needsAPIKey
}

View File

@@ -7,6 +7,7 @@ import (
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/crush/internal/tui/styles"
"github.com/charmbracelet/crush/internal/tui/util"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
)
@@ -72,13 +73,19 @@ func (m *statusCmp) infoMsg() string {
switch m.info.Type {
case util.InfoTypeError:
infoType = t.S().Base.Background(t.Red).Padding(0, 1).Render("ERROR")
message = t.S().Base.Background(t.Error).Width(m.width).Foreground(t.White).Padding(0, 1).Render(m.info.Msg)
widthLeft := m.width - (lipgloss.Width(infoType) + 2)
info := ansi.Truncate(m.info.Msg, widthLeft, "…")
message = t.S().Base.Background(t.Error).Width(widthLeft+2).Foreground(t.White).Padding(0, 1).Render(info)
case util.InfoTypeWarn:
infoType = t.S().Base.Foreground(t.BgOverlay).Background(t.Yellow).Padding(0, 1).Render("WARNING")
message = t.S().Base.Foreground(t.BgOverlay).Width(m.width).Background(t.Warning).Padding(0, 1).Render(m.info.Msg)
widthLeft := m.width - (lipgloss.Width(infoType) + 2)
info := ansi.Truncate(m.info.Msg, widthLeft, "…")
message = t.S().Base.Foreground(t.BgOverlay).Width(widthLeft+2).Background(t.Warning).Padding(0, 1).Render(info)
default:
infoType = t.S().Base.Foreground(t.BgOverlay).Background(t.Green).Padding(0, 1).Render("OKAY!")
message = t.S().Base.Background(t.Success).Width(m.width).Foreground(t.White).Padding(0, 1).Render(m.info.Msg)
widthLeft := m.width - (lipgloss.Width(infoType) + 2)
info := ansi.Truncate(m.info.Msg, widthLeft, "…")
message = t.S().Base.Background(t.Success).Width(widthLeft+2).Foreground(t.White).Padding(0, 1).Render(info)
}
return ansi.Truncate(infoType+message, m.width, "…")
}

View File

@@ -59,7 +59,11 @@ type commandDialogCmp struct {
type (
SwitchSessionsMsg struct{}
NewSessionsMsg struct{}
SwitchModelMsg struct{}
QuitMsg struct{}
OpenFilePickerMsg struct{}
ToggleHelpMsg struct{}
ToggleCompactModeMsg struct{}
ToggleThinkingMsg struct{}
CompactMsg struct {
@@ -248,13 +252,29 @@ func (c *commandDialogCmp) Position() (int, int) {
func (c *commandDialogCmp) defaultCommands() []Command {
commands := []Command{
{
ID: "init",
Title: "Initialize Project",
Description: "Create/Update the CRUSH.md memory file",
ID: "new_session",
Title: "New Session",
Description: "start a new session",
Shortcut: "ctrl+n",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(chat.SendMsg{
Text: prompt.Initialize(),
})
return util.CmdHandler(NewSessionsMsg{})
},
},
{
ID: "switch_session",
Title: "Switch Session",
Description: "Switch to a different session",
Shortcut: "ctrl+s",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(SwitchSessionsMsg{})
},
},
{
ID: "switch_model",
Title: "Switch Model",
Description: "Switch to a different model",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(SwitchModelMsg{})
},
},
}
@@ -307,23 +327,49 @@ func (c *commandDialogCmp) defaultCommands() []Command {
},
})
}
if c.sessionID != "" {
agentCfg := config.Get().Agents["coder"]
model := config.Get().GetModelByType(agentCfg.Model)
if model.SupportsImages {
commands = append(commands, Command{
ID: "file_picker",
Title: "Open File Picker",
Shortcut: "ctrl+f",
Description: "Open file picker",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(OpenFilePickerMsg{})
},
})
}
}
return append(commands, []Command{
{
ID: "switch_session",
Title: "Switch Session",
Description: "Switch to a different session",
Shortcut: "ctrl+s",
ID: "toggle_help",
Title: "Toggle Help",
Shortcut: "ctrl+g",
Description: "Toggle help",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(SwitchSessionsMsg{})
return util.CmdHandler(ToggleHelpMsg{})
},
},
{
ID: "switch_model",
Title: "Switch Model",
Description: "Switch to a different model",
ID: "init",
Title: "Initialize Project",
Description: "Create/Update the CRUSH.md memory file",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(SwitchModelMsg{})
return util.CmdHandler(chat.SendMsg{
Text: prompt.Initialize(),
})
},
},
{
ID: "quit",
Title: "Quit",
Description: "Quit",
Shortcut: "ctrl+c",
Handler: func(cmd Command) tea.Cmd {
return util.CmdHandler(QuitMsg{})
},
},
}...)

View File

@@ -21,7 +21,7 @@ import (
)
const (
maxAttachmentSize = int64(5 * 1024 * 1024) // 5MB
MaxAttachmentSize = int64(5 * 1024 * 1024) // 5MB
FilePickerID = "filepicker"
fileSelectionHight = 10
)
@@ -45,10 +45,12 @@ type model struct {
help help.Model
}
var AllowedTypes = []string{".jpg", ".jpeg", ".png"}
func NewFilePickerCmp(workingDir string) FilePicker {
t := styles.CurrentTheme()
fp := filepicker.New()
fp.AllowedTypes = []string{".jpg", ".jpeg", ".png"}
fp.AllowedTypes = AllowedTypes
if workingDir != "" {
fp.CurrentDirectory = workingDir
@@ -127,7 +129,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Sequence(
util.CmdHandler(dialogs.CloseDialogMsg{}),
func() tea.Msg {
isFileLarge, err := ValidateFileSize(path, maxAttachmentSize)
isFileLarge, err := IsFileTooBig(path, MaxAttachmentSize)
if err != nil {
return util.ReportError(fmt.Errorf("unable to read the image: %w", err))
}
@@ -222,7 +224,7 @@ func (m *model) Position() (int, int) {
return row, col
}
func ValidateFileSize(filePath string, sizeLimit int64) (bool, error) {
func IsFileTooBig(filePath string, sizeLimit int64) (bool, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
return false, fmt.Errorf("error getting file info: %w", err)

View File

@@ -84,7 +84,7 @@ func (p *permissionDialogCmp) Init() tea.Cmd {
}
func (p *permissionDialogCmp) supportsDiffView() bool {
return p.permission.ToolName == tools.EditToolName || p.permission.ToolName == tools.WriteToolName
return p.permission.ToolName == tools.EditToolName || p.permission.ToolName == tools.WriteToolName || p.permission.ToolName == tools.MultiEditToolName
}
func (p *permissionDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -305,6 +305,20 @@ func (p *permissionDialogCmp) renderHeader() string {
),
baseStyle.Render(strings.Repeat(" ", p.width)),
)
case tools.MultiEditToolName:
params := p.permission.Params.(tools.MultiEditPermissionsParams)
fileKey := t.S().Muted.Render("File")
filePath := t.S().Text.
Width(p.width - lipgloss.Width(fileKey)).
Render(fmt.Sprintf(" %s", fsext.PrettyPath(params.FilePath)))
headerParts = append(headerParts,
lipgloss.JoinHorizontal(
lipgloss.Left,
fileKey,
filePath,
),
baseStyle.Render(strings.Repeat(" ", p.width)),
)
case tools.FetchToolName:
headerParts = append(headerParts, t.S().Muted.Width(p.width).Bold(true).Render("URL"))
}
@@ -329,6 +343,8 @@ func (p *permissionDialogCmp) getOrGenerateContent() string {
content = p.generateEditContent()
case tools.WriteToolName:
content = p.generateWriteContent()
case tools.MultiEditToolName:
content = p.generateMultiEditContent()
case tools.FetchToolName:
content = p.generateFetchContent()
default:
@@ -435,6 +451,28 @@ func (p *permissionDialogCmp) generateDownloadContent() string {
return ""
}
func (p *permissionDialogCmp) generateMultiEditContent() string {
if pr, ok := p.permission.Params.(tools.MultiEditPermissionsParams); ok {
// Use the cache for diff rendering
formatter := core.DiffFormatter().
Before(fsext.PrettyPath(pr.FilePath), pr.OldContent).
After(fsext.PrettyPath(pr.FilePath), pr.NewContent).
Height(p.contentViewPort.Height()).
Width(p.contentViewPort.Width()).
XOffset(p.diffXOffset).
YOffset(p.diffYOffset)
if p.useDiffSplitMode() {
formatter = formatter.Split()
} else {
formatter = formatter.Unified()
}
diff := formatter.String()
return diff
}
return ""
}
func (p *permissionDialogCmp) generateFetchContent() string {
t := styles.CurrentTheme()
baseStyle := t.S().Base.Background(t.BgSubtle)
@@ -579,6 +617,9 @@ func (p *permissionDialogCmp) SetSize() tea.Cmd {
case tools.WriteToolName:
p.width = int(float64(p.wWidth) * 0.8)
p.height = int(float64(p.wHeight) * 0.8)
case tools.MultiEditToolName:
p.width = int(float64(p.wWidth) * 0.8)
p.height = int(float64(p.wHeight) * 0.8)
case tools.FetchToolName:
p.width = int(float64(p.wWidth) * 0.8)
p.height = int(float64(p.wHeight) * 0.3)

View File

@@ -94,7 +94,7 @@ func NewFilterableList[T FilterableItem](items []T, opts ...filterableListOption
for _, opt := range opts {
opt(f.filterableOptions)
}
f.list = New[T](items, f.listOptions...).(*list[T])
f.list = New(items, f.listOptions...).(*list[T])
f.updateKeyMaps()
f.items = slices.Collect(f.list.items.Seq())

View File

@@ -236,6 +236,18 @@ func (l *list[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, l.keyMap.Home):
return l, l.GoToTop()
}
s := l.SelectedItem()
if s == nil {
return l, nil
}
item := *s
var cmds []tea.Cmd
updated, cmd := item.Update(msg)
cmds = append(cmds, cmd)
if u, ok := updated.(T); ok {
cmds = append(cmds, l.UpdateItem(u.ID(), u))
}
return l, tea.Batch(cmds...)
}
}
return l, nil

View File

@@ -8,6 +8,7 @@ type KeyMap struct {
Quit key.Binding
Help key.Binding
Commands key.Binding
Suspend key.Binding
Sessions key.Binding
pageBindings []key.Binding
@@ -27,6 +28,10 @@ func DefaultKeyMap() KeyMap {
key.WithKeys("ctrl+p"),
key.WithHelp("ctrl+p", "commands"),
),
Suspend: key.NewBinding(
key.WithKeys("ctrl+z"),
key.WithHelp("ctrl+z", "suspend"),
),
Sessions: key.NewBinding(
key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "sessions"),

View File

@@ -12,6 +12,7 @@ import (
"github.com/charmbracelet/crush/internal/config"
"github.com/charmbracelet/crush/internal/history"
"github.com/charmbracelet/crush/internal/message"
"github.com/charmbracelet/crush/internal/permission"
"github.com/charmbracelet/crush/internal/pubsub"
"github.com/charmbracelet/crush/internal/session"
"github.com/charmbracelet/crush/internal/tui/components/anim"
@@ -36,8 +37,7 @@ import (
var ChatPageID page.PageID = "chat"
type (
OpenFilePickerMsg struct{}
ChatFocusedMsg struct {
ChatFocusedMsg struct {
Focused bool
}
CancelTimerExpiredMsg struct{}
@@ -178,6 +178,10 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case CancelTimerExpiredMsg:
p.isCanceling = false
return p, nil
case editor.OpenEditorMsg:
u, cmd := p.editor.Update(msg)
p.editor = u.(editor.Editor)
return p, cmd
case chat.SendMsg:
return p, p.sendMessage(msg.Text, msg.Attachments)
case chat.SessionSelectedMsg:
@@ -253,6 +257,11 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
p.sidebar = u.(sidebar.Sidebar)
cmds = append(cmds, cmd)
return p, tea.Batch(cmds...)
case pubsub.Event[permission.PermissionNotification]:
u, cmd := p.chat.Update(msg)
p.chat = u.(chat.MessageListCmp)
cmds = append(cmds, cmd)
return p, tea.Batch(cmds...)
case commands.CommandRunCustomMsg:
if p.app.CoderAgent.IsBusy() {
@@ -278,15 +287,23 @@ func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
p.isProjectInit = false
p.focusedPane = PanelTypeEditor
return p, p.SetSize(p.width, p.height)
case commands.NewSessionsMsg:
if p.app.CoderAgent.IsBusy() {
return p, util.ReportWarn("Agent is busy, please wait before starting a new session...")
}
return p, p.newSession()
case tea.KeyPressMsg:
switch {
case key.Matches(msg, p.keyMap.NewSession):
if p.app.CoderAgent.IsBusy() {
return p, util.ReportWarn("Agent is busy, please wait before starting a new session...")
}
return p, p.newSession()
case key.Matches(msg, p.keyMap.AddAttachment):
agentCfg := config.Get().Agents["coder"]
model := config.Get().GetModelByType(agentCfg.Model)
if model.SupportsImages {
return p, util.CmdHandler(OpenFilePickerMsg{})
return p, util.CmdHandler(commands.OpenFilePickerMsg{})
} else {
return p, util.ReportWarn("File attachments are not supported by the current model: " + model.Name)
}
@@ -812,6 +829,10 @@ func (p *chatPage) Help() help.KeyMap {
key.WithKeys("up", "down"),
key.WithHelp("↑↓", "scroll"),
),
key.NewBinding(
key.WithKeys("c", "y"),
key.WithHelp("c/y", "copy"),
),
)
fullList = append(fullList,
[]key.Binding{

View File

@@ -170,7 +170,14 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, util.CmdHandler(dialogs.OpenDialogMsg{
Model: compact.NewCompactDialogCmp(a.app.CoderAgent, msg.SessionID, true),
})
case commands.QuitMsg:
return a, util.CmdHandler(dialogs.OpenDialogMsg{
Model: quit.NewQuitDialog(),
})
case commands.ToggleHelpMsg:
a.status.ToggleFullHelp()
a.showingFullHelp = !a.showingFullHelp
return a, a.handleWindowResize(a.wWidth, a.wHeight)
// Model Switch
case models.ModelSelectedMsg:
config.Get().UpdatePreferredModel(msg.ModelType, msg.Model)
@@ -187,7 +194,7 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, util.ReportInfo(fmt.Sprintf("%s model changed to %s", modelTypeName, msg.Model.Model))
// File Picker
case chat.OpenFilePickerMsg:
case commands.OpenFilePickerMsg:
if a.dialog.ActiveDialogID() == filepicker.FilePickerID {
// If the commands dialog is already open, close it
return a, util.CmdHandler(dialogs.CloseDialogMsg{})
@@ -196,6 +203,11 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Model: filepicker.NewFilePickerCmp(a.app.Config().WorkingDir()),
})
// Permissions
case pubsub.Event[permission.PermissionNotification]:
// forward to page
updated, cmd := a.pages[a.currentPage].Update(msg)
a.pages[a.currentPage] = updated.(util.Model)
return a, cmd
case pubsub.Event[permission.PermissionRequest]:
return a, util.CmdHandler(dialogs.OpenDialogMsg{
Model: permissions.NewPermissionDialogCmp(msg.Payload),
@@ -248,6 +260,13 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyPressMsg:
return a, a.handleKeyPressMsg(msg)
case tea.MouseWheelMsg:
if !a.dialog.HasDialogs() {
updated, pageCmd := a.pages[a.currentPage].Update(msg)
a.pages[a.currentPage] = updated.(util.Model)
cmds = append(cmds, pageCmd)
}
return a, tea.Batch(cmds...)
case tea.PasteMsg:
if a.dialog.HasDialogs() {
u, dialogCmd := a.dialog.Update(msg)
@@ -370,6 +389,11 @@ func (a *appModel) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
},
)
return tea.Sequence(cmds...)
case key.Matches(msg, a.keyMap.Suspend):
if a.app.CoderAgent.IsBusy() {
return util.ReportWarn("Agent is busy, please wait...")
}
return tea.Suspend
default:
if a.dialog.HasDialogs() {
u, dialogCmd := a.dialog.Update(msg)