-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: Rewrite lines #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mudream4869
wants to merge
9
commits into
staging
Choose a base branch
from
rewrite_prompt
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d1c2cb
✨ feat(rewrite): add rewrite prompt template and parsing functionality
mudream4869 88a44bc
✨ feat(rewrite): add rewrite command for AI-based line rewriting in c…
mudream4869 642e2a1
✨ feat(rewrite): implement rewrite command with confirmation modal an…
mudream4869 856267d
✨ feat(rewrite): add prompt goal functionality for rewrite command an…
mudream4869 88a4c92
✨ feat(tests): add comprehensive tests for rewrite command validation…
mudream4869 584e8ad
🧹 refactor(cmd_rewrite): remove min/max
mudream4869 b5aba81
🧹 refactor(tests): remove unused context extraction tests and clean u…
mudream4869 994ecfd
🔧 refactor(render): remove project parameter from RenderRewrite function
mudream4869 41cab33
📝 docs(cmd_rewrite): update command description to clarify file updat…
mudream4869 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
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,257 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/voilelab/gonovelmaker/internal/config" | ||
| "github.com/voilelab/gonovelmaker/internal/llmbackend" | ||
| "github.com/voilelab/gonovelmaker/internal/nmutil" | ||
| "github.com/voilelab/gonovelmaker/internal/obsidian" | ||
| "github.com/voilelab/gonovelmaker/novelmaker" | ||
| ) | ||
|
|
||
| type RewriteCmd struct { | ||
| json bool | ||
| filepath string | ||
| promptGoal string | ||
| lineStart int | ||
| lineEnd int | ||
| contextPrev int | ||
| contextNext int | ||
| backend string | ||
| model string | ||
| timeout int | ||
|
|
||
| llmBackendMaker llmbackend.LLMBackendMaker | ||
|
|
||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func NewRewriteCmd(llmBackendMaker llmbackend.LLMBackendMaker) *RewriteCmd { | ||
| r := &RewriteCmd{ | ||
| llmBackendMaker: llmBackendMaker, | ||
| } | ||
|
|
||
| r.cmd = &cobra.Command{ | ||
| Use: "rewrite", | ||
| Short: "Rewrite a specific range of lines in a file", | ||
| Long: `Rewrite a specific range of lines in a file using AI. | ||
|
|
||
| This command reads the specified file, extracts the target lines and context, | ||
| sends them to the AI model for rewriting, and updates the file with the new content. | ||
| The filepath should be relative to the vault root (e.g., "Story/001_ch1.md"). | ||
|
|
||
| Example: | ||
| novelmaker-obs rewrite --filepath Story/001_ch1.md --line-start 10 --line-end 15 --context-prev 5 --context-next 5 | ||
| novelmaker-obs rewrite -f Story/001_ch1.md -s 10 -e 15 -p 5 -n 5 --model gpt-4`, | ||
| RunE: r.run, | ||
| } | ||
|
|
||
| r.cmd.Flags().BoolVarP(&r.json, "json", "j", false, "Output in JSON format") | ||
|
|
||
| r.cmd.Flags().StringVarP(&r.filepath, "filepath", "f", "", "Path to the file relative to vault root (required)") | ||
| r.cmd.Flags().StringVarP(&r.promptGoal, "prompt", "g", "去除 AI 味的慣用詞與過度評述語氣", "Rewrite goal/instruction (optional, default: remove AI-like phrases)") | ||
| r.cmd.Flags().IntVarP(&r.lineStart, "line-start", "s", 0, "Starting line number to rewrite (required, 1-indexed)") | ||
| r.cmd.Flags().IntVarP(&r.lineEnd, "line-end", "e", 0, "Ending line number to rewrite (required, 1-indexed, inclusive)") | ||
| r.cmd.Flags().IntVarP(&r.contextPrev, "context-prev", "p", 3, "Number of lines before the target as context (default 3)") | ||
| r.cmd.Flags().IntVarP(&r.contextNext, "context-next", "n", 3, "Number of lines after the target as context (default 3)") | ||
|
|
||
| r.cmd.MarkFlagRequired("filepath") | ||
| r.cmd.MarkFlagRequired("line-start") | ||
| r.cmd.MarkFlagRequired("line-end") | ||
|
|
||
| // Allow overriding config values per-command | ||
| r.cmd.Flags().StringVar(&r.backend, "backend", "", "LLM backend to use (optional, uses default if not specified)") | ||
| r.cmd.Flags().StringVar(&r.model, "model", "", "Model to use, overrides config (optional)") | ||
| r.cmd.Flags().IntVar(&r.timeout, "timeout", 0, "Timeout in seconds for the API request (optional)") | ||
|
|
||
| return r | ||
| } | ||
|
|
||
| func (r *RewriteCmd) run(cmd *cobra.Command, args []string) error { | ||
| // Validate line numbers | ||
| if r.lineStart < 1 { | ||
| return fmt.Errorf("line-start must be >= 1, got %d", r.lineStart) | ||
| } | ||
| if r.lineEnd < r.lineStart { | ||
| return fmt.Errorf("line-end (%d) must be >= line-start (%d)", r.lineEnd, r.lineStart) | ||
| } | ||
| if r.contextPrev < 0 { | ||
| return fmt.Errorf("context-prev must be >= 0, got %d", r.contextPrev) | ||
| } | ||
| if r.contextNext < 0 { | ||
| return fmt.Errorf("context-next must be >= 0, got %d", r.contextNext) | ||
| } | ||
|
|
||
| // Load config | ||
| cfg, err := config.Load() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load config: %w", err) | ||
| } | ||
|
|
||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get current directory: %w", err) | ||
| } | ||
|
|
||
| // Create vault | ||
| vault, err := obsidian.NewVault(cwd) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open vault: %w", err) | ||
| } | ||
| defer vault.Close() | ||
|
|
||
| // Load project | ||
| project, err := vault.LoadProject() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load project: %w", err) | ||
| } | ||
|
|
||
| // Get backend configuration | ||
| backend := cfg.GetBackend(r.backend) | ||
| if backend == nil { | ||
| if r.backend != "" { | ||
| return fmt.Errorf("backend '%s' not found in config", r.backend) | ||
| } | ||
| return fmt.Errorf("no LLM backend configured. Please configure at least one backend in ~/.novelmaker/config.toml") | ||
| } | ||
|
|
||
| // Determine effective API settings (flags override config) | ||
| effectiveModel := nmutil.FirstNonEmptyString(r.model, backend.Model) | ||
| effectiveTimeout := time.Duration(nmutil.FirstNonZero(r.timeout, backend.Timeout)) * time.Second | ||
|
|
||
| if !r.json { | ||
| fmt.Println("Rewriting lines with AI...") | ||
| fmt.Printf(" Project: %s\n", project.Name) | ||
| fmt.Printf(" Model: %s\n", effectiveModel) | ||
| fmt.Printf(" File: %s\n", r.filepath) | ||
| fmt.Printf(" Lines: %d-%d\n", r.lineStart, r.lineEnd) | ||
| fmt.Printf(" Context: %d lines before, %d lines after\n", r.contextPrev, r.contextNext) | ||
| } | ||
|
|
||
| // Read the file and extract lines | ||
| fileLines, err := r.readFileLines(r.filepath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read file: %w", err) | ||
| } | ||
|
|
||
| totalLines := len(fileLines) | ||
| if r.lineStart > totalLines { | ||
| return fmt.Errorf("line-start (%d) exceeds total lines in file (%d)", r.lineStart, totalLines) | ||
| } | ||
| if r.lineEnd > totalLines { | ||
| return fmt.Errorf("line-end (%d) exceeds total lines in file (%d)", r.lineEnd, totalLines) | ||
| } | ||
|
|
||
| // Extract context and target (convert to 0-indexed) | ||
| contextStartIdx := max(0, r.lineStart-1-r.contextPrev) | ||
| targetStartIdx := r.lineStart - 1 | ||
| targetEndIdx := r.lineEnd // inclusive, so no -1 | ||
| contextEndIdx := min(totalLines, r.lineEnd+r.contextNext) | ||
|
|
||
| contextBefore := strings.Join(fileLines[contextStartIdx:targetStartIdx], "\n") | ||
| targetSentence := strings.Join(fileLines[targetStartIdx:targetEndIdx], "\n") | ||
| contextAfter := strings.Join(fileLines[targetEndIdx:contextEndIdx], "\n") | ||
mudream4869 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Load rewrite prompt template from vault | ||
| rewritePrompt, err := vault.LoadRewritePrompt() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load rewrite prompt from vault: %w", err) | ||
| } | ||
|
|
||
| llmBackend := r.llmBackendMaker( | ||
| backend.APIKey, | ||
| backend.BaseURL, | ||
| effectiveModel, | ||
| ) | ||
|
|
||
| renderer := novelmaker.NewRenderer( | ||
| llmBackend, | ||
| effectiveTimeout, | ||
| ) | ||
|
|
||
| slog.Info("Rewriting text", | ||
| "project", project.Name, | ||
| "model", effectiveModel, | ||
| "file", r.filepath, | ||
| "line_range", fmt.Sprintf("%d-%d", r.lineStart, r.lineEnd), | ||
| ) | ||
|
|
||
| // Call AI API | ||
| rewrittenContent, usage, err := renderer.RenderRewrite( | ||
| project, rewritePrompt, r.promptGoal, contextBefore, targetSentence, contextAfter) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to rewrite text: %w", err) | ||
| } | ||
|
|
||
| // Replace the target lines with rewritten content | ||
| rewrittenLines := strings.Split(strings.TrimSpace(rewrittenContent), "\n") | ||
|
|
||
| if !r.json { | ||
| // In non-JSON mode, write to file immediately | ||
| newFileLines := make([]string, 0, len(fileLines)) | ||
| newFileLines = append(newFileLines, fileLines[:targetStartIdx]...) | ||
| newFileLines = append(newFileLines, rewrittenLines...) | ||
| newFileLines = append(newFileLines, fileLines[targetEndIdx:]...) | ||
|
|
||
| newContent := strings.Join(newFileLines, "\n") | ||
| if err := os.WriteFile(r.filepath, []byte(newContent), 0644); err != nil { | ||
| return fmt.Errorf("failed to write file: %w", err) | ||
| } | ||
|
|
||
| fmt.Printf("\n✓ Successfully rewrote lines %d-%d!\n", r.lineStart, r.lineEnd) | ||
| fmt.Printf(" Original lines: %d\n", r.lineEnd-r.lineStart+1) | ||
| fmt.Printf(" New lines: %d\n", len(rewrittenLines)) | ||
| fmt.Printf("\nToken Usage:\n") | ||
| fmt.Printf(" Input tokens: %d\n", usage.InputTokens) | ||
| fmt.Printf(" Output tokens: %d\n", usage.OutputTokens) | ||
| fmt.Printf(" Total tokens: %d\n", usage.TotalTokens) | ||
| fmt.Printf("\nRewritten content:\n") | ||
| fmt.Printf(" %s\n", strings.ReplaceAll(rewrittenContent, "\n", "\n ")) | ||
| } else { | ||
| // In JSON mode, only return the result without modifying the file | ||
| output := map[string]any{ | ||
| "filepath": r.filepath, | ||
| "original_line_count": r.lineEnd - r.lineStart + 1, | ||
| "new_line_count": len(rewrittenLines), | ||
| "input_tokens": usage.InputTokens, | ||
| "output_tokens": usage.OutputTokens, | ||
| "total_tokens": usage.TotalTokens, | ||
| "rewritten_content": rewrittenContent, | ||
| } | ||
| encoder := json.NewEncoder(os.Stdout) | ||
| encoder.SetIndent("", " ") | ||
| if err := encoder.Encode(output); err != nil { | ||
| return fmt.Errorf("failed to encode JSON output: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (r *RewriteCmd) readFileLines(filepath string) ([]string, error) { | ||
| file, err := os.Open(filepath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer file.Close() | ||
|
|
||
| var lines []string | ||
| scanner := bufio.NewScanner(file) | ||
| for scanner.Scan() { | ||
| lines = append(lines, scanner.Text()) | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return lines, nil | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.