Skip to content

Commit 764b0bd

Browse files
committed
feat: implement command-line flag handling and completion support
- Remove unused `debug` import and commented out debug trace statement. - Add `flags` map to `CommandExecutor` for handling command-line flags. - Implement flag parsing logic in `CommandExecutor` to filter and process command arguments. - Add flag completion support for `--verbose` and `--help` in the command completer. - Update the system prompt to emphasize architectural analysis and code quality evaluation. - Add `FlagVerbose` constant for verbose mode handling. - Add `RenderChatMessages` function to render and copy chat messages to the clipboard. Signed-off-by: codiing-hui <wecoding@yeah.net>
1 parent c20cd0d commit 764b0bd

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

internal/convo/factory.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/coding-hui/ai-terminal/internal/errbook"
99
"github.com/coding-hui/ai-terminal/internal/options"
10-
"github.com/coding-hui/ai-terminal/internal/util/debug"
1110
)
1211

1312
var (
@@ -60,7 +59,7 @@ func GetConversationStore(cfg *options.Config) (Store, error) {
6059
} else {
6160
// No conversations exist, generate new ID
6261
cfg.ConversationID = NewConversationID()
63-
debug.Trace("conversation id not provided, generating new id `%s`", cfg.ConversationID)
62+
//debug.Trace("conversation id not provided, generating new id `%s`", cfg.ConversationID)
6463
}
6564
}
6665

internal/ui/coders/commands.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func getSupportedCommands() []string {
4040
type CommandExecutor struct {
4141
coder *AutoCoder
4242
editor *EditBlockCoder
43+
flags map[string]bool
4344
}
4445

4546
func NewCommandExecutor(coder *AutoCoder) *CommandExecutor {
@@ -95,8 +96,12 @@ func (c *CommandExecutor) Executor(input string) {
9596
return
9697
}
9798

99+
// Parse command arguments
100+
filteredArgs := c.parseFlags(args)
101+
userQuestion := strings.Join(filteredArgs, " ")
102+
98103
// Execute the recognized command
99-
if err := fn(context.Background(), args...); err != nil {
104+
if err := fn(context.Background(), userQuestion); err != nil {
100105
console.RenderError(err, "Failed to execute command %s", cmd)
101106
}
102107
}
@@ -108,6 +113,10 @@ func (c *CommandExecutor) ask(ctx context.Context, args ...string) error {
108113
return errbook.Wrap("Failed to prepare ask completion messages", err)
109114
}
110115

116+
if c.flags[FlagVerbose] {
117+
return console.RenderChatMessages(messages)
118+
}
119+
111120
chatModel := chat.NewChat(c.coder.cfg,
112121
chat.WithContext(ctx),
113122
chat.WithMessages(messages),
@@ -317,6 +326,10 @@ func (c *CommandExecutor) coding(ctx context.Context, args ...string) error {
317326
return err
318327
}
319328

329+
if c.flags[FlagVerbose] {
330+
return console.RenderChatMessages(messages)
331+
}
332+
320333
err = c.editor.Execute(ctx, messages)
321334
if err != nil {
322335
return err
@@ -517,6 +530,20 @@ func (c *CommandExecutor) formatFileContent(filePath string) (string, error) {
517530
return fmt.Sprintf("\n%s%s", relPath, wrapFenceWithType(content, filePath)), nil
518531
}
519532

533+
func (c *CommandExecutor) parseFlags(args []string) (filteredArgs []string) {
534+
c.flags = make(map[string]bool)
535+
536+
for _, arg := range args {
537+
if strings.HasPrefix(arg, "--") {
538+
c.flags[arg[2:]] = true
539+
continue
540+
}
541+
filteredArgs = append(filteredArgs, arg)
542+
}
543+
544+
return
545+
}
546+
520547
func extractCmdArgs(input string) (string, []string) {
521548
input = strings.TrimPrefix(input, "!")
522549
input = strings.TrimPrefix(input, "/")

internal/ui/coders/completer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,14 @@ func (c CommandCompleter) Complete(d prompt.Document) []prompt.Suggest {
4444
return prompt.FilterFuzzy(completions, t, true)
4545
}
4646

47+
// if the input starts with "--", then we use the flag completer
48+
if strings.HasPrefix(t, "--") {
49+
completions := []prompt.Suggest{
50+
{Text: "--verbose"},
51+
{Text: "--help"},
52+
}
53+
return prompt.FilterContains(completions, t, true)
54+
}
55+
4756
return []prompt.Suggest{}
4857
}

internal/ui/coders/prompts.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,28 @@ ONLY EVER RETURN CODE IN A *SEARCH/REPLACE BLOCK*!
5656
var (
5757
promptAsk = prompts.NewChatPromptTemplate([]prompts.MessageFormatter{
5858
prompts.NewSystemMessagePromptTemplate(
59-
`You are a professional software engineer!
60-
Take requests for review to the supplied code.
61-
If the request is ambiguous, ask questions.
62-
63-
Always reply in the same language as the user's question!!!
64-
59+
`You are a senior software architect and coding expert! Your role is to:
60+
1. Analyze implementation details and design patterns
61+
2. Evaluate code quality and architecture
62+
3. Suggest optimization strategies
63+
4. Identify potential edge cases
64+
5. Propose maintainability improvements
65+
66+
Avoid direct code changes unless explicitly requested.
67+
Provide detailed rationale for each recommendation.
68+
Use diagrams/examples when explaining complex concepts.
69+
Highlight any anti-patterns detected.
6570
`,
6671
nil,
6772
),
6873
prompts.NewHumanMessagePromptTemplate(
69-
`I have *added these files to the chat* so you can go ahead and review them.
70-
74+
`Codebase Context:
7175
{{ .added_files }}
7276
`,
7377
[]string{addedFilesKey},
7478
),
7579
prompts.NewAIMessagePromptTemplate(
76-
"Ok, I will review the above code carefully to see if there are any bugs or performance optimization issues.",
80+
"Understood. Let me break this down systematically...",
7781
nil,
7882
),
7983
prompts.NewHumanMessagePromptTemplate(

internal/ui/coders/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package coders
22

3+
const (
4+
FlagVerbose = "verbose"
5+
)
6+
37
// PromptMode represents the mode of the prompt.
48
type PromptMode int
59

internal/ui/console/render.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package console
22

33
import (
44
"fmt"
5+
"html"
56
"os"
67
"sync"
78

9+
"github.com/atotto/clipboard"
810
"github.com/charmbracelet/lipgloss"
911
"github.com/muesli/termenv"
12+
13+
"github.com/coding-hui/wecoding-sdk-go/services/ai/llms"
1014
)
1115

1216
var StdoutRenderer = sync.OnceValue(func() *lipgloss.Renderer {
@@ -52,3 +56,16 @@ func RenderAppName(appName string, suffix string, args ...interface{}) {
5256
appName = MakeGradientText(StdoutStyles().AppName, appName)
5357
fmt.Print(appName + " " + fmt.Sprintf(suffix, args...))
5458
}
59+
60+
func RenderChatMessages(messages []llms.ChatMessage) error {
61+
content, err := llms.GetBufferString(messages, "human", "ai")
62+
if err != nil {
63+
return err
64+
}
65+
content = html.UnescapeString(content)
66+
Render(content)
67+
_ = clipboard.WriteAll(content)
68+
termenv.Copy(content)
69+
PrintConfirmation("COPIED", "Ask prompt content copied to clipboard!")
70+
return nil
71+
}

0 commit comments

Comments
 (0)