This repository was archived by the owner on Sep 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: custom commands #133
Merged
kujtimiihoxha
merged 12 commits into
opencode-ai:main
from
mark3labs:implement-custom-commands
May 9, 2025
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
29a3cc7
Implement custom commands
ezynda3 540ea76
Add User: prefix
ezynda3 5257916
Reuse var
ezynda3 b4a6883
Merge branch 'opencode-ai:main' into implement-custom-commands
ezynda3 ab4aa9f
Check if the agent is busy and if so report a warning
ezynda3 0a46a89
Update README
ezynda3 db11b72
fix typo
ezynda3 4e489d7
Implement user and project scoped custom commands
ezynda3 93262a3
Allow for $ARGUMENTS
ezynda3 0585b4d
UI tweaks
ezynda3 0adbce9
Update internal/tui/components/dialog/arguments.go
ezynda3 0020a97
Also search in $HOME/.opencode/commands
ezynda3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package dialog | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/opencode-ai/opencode/internal/config" | ||
| "github.com/opencode-ai/opencode/internal/tui/util" | ||
| ) | ||
|
|
||
| // CustomCommandPrefix is the prefix used for custom commands loaded from files | ||
| const CustomCommandPrefix = "user:" | ||
|
|
||
| // LoadCustomCommands loads custom commands from the data directory | ||
| func LoadCustomCommands() ([]Command, error) { | ||
| cfg := config.Get() | ||
| if cfg == nil { | ||
| return nil, fmt.Errorf("config not loaded") | ||
| } | ||
|
|
||
| dataDir := cfg.Data.Directory | ||
| commandsDir := filepath.Join(dataDir, "commands") | ||
|
|
||
| // Check if the commands directory exists | ||
| if _, err := os.Stat(commandsDir); os.IsNotExist(err) { | ||
| // Create the commands directory if it doesn't exist | ||
| if err := os.MkdirAll(commandsDir, 0755); err != nil { | ||
| return nil, fmt.Errorf("failed to create commands directory: %w", err) | ||
| } | ||
| // Return empty list since we just created the directory | ||
| return []Command{}, nil | ||
| } | ||
|
|
||
| var commands []Command | ||
|
|
||
| // Walk through the commands directory and load all .md files | ||
| err := filepath.Walk(commandsDir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Skip directories | ||
| if info.IsDir() { | ||
| return nil | ||
| } | ||
|
|
||
| // Only process markdown files | ||
| if !strings.HasSuffix(strings.ToLower(info.Name()), ".md") { | ||
| return nil | ||
| } | ||
|
|
||
| // Read the file content | ||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read command file %s: %w", path, err) | ||
| } | ||
|
|
||
| // Get the command ID from the file name without the .md extension | ||
| commandID := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())) | ||
|
|
||
| // Get relative path from commands directory | ||
| relPath, err := filepath.Rel(commandsDir, path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get relative path for %s: %w", path, err) | ||
| } | ||
|
|
||
| // Create the command ID from the relative path | ||
| // Replace directory separators with colons | ||
| commandIDPath := strings.ReplaceAll(filepath.Dir(relPath), string(filepath.Separator), ":") | ||
| if commandIDPath != "." { | ||
| commandID = commandIDPath + ":" + commandID | ||
| } | ||
|
|
||
| // Create a command | ||
| command := Command{ | ||
| ID: CustomCommandPrefix + commandID, | ||
| Title: CustomCommandPrefix + commandID, | ||
| Description: fmt.Sprintf("Custom command from %s", relPath), | ||
| Handler: func(cmd Command) tea.Cmd { | ||
| return util.CmdHandler(CommandRunCustomMsg{ | ||
| Content: string(content), | ||
| }) | ||
| }, | ||
| } | ||
|
|
||
| commands = append(commands, command) | ||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load custom commands: %w", err) | ||
| } | ||
|
|
||
| return commands, nil | ||
| } | ||
|
|
||
| // CommandRunCustomMsg is sent when a custom command is executed | ||
| type CommandRunCustomMsg struct { | ||
| Content string | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to check if the agent is busy here before we send the message.
p.app.CoderAgent.IsBusy()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's busy what should the behavior be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess just returning a warning should be fine right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you already did what I wanted to suggest 😂 awesome.