on main help sign in
add initial git support
commitbd2ec29b65e430f83f430db5fdc424c7d631989d authorKujtim Hoxha <kujtimii.h@gmail.com> dateSat 12 Apr 2025, 18:45 (2 years ago) parent0697dcc
19 files changed+791−176
.gitignore+1 README.md+13 cmd/diff/main.go+102 cmd/git/main.go+4 cmd/root.go+6 go.mod+16−4 go.sum+56−1 internal/assets/diff/themes/dark.json+73 internal/assets/embed.go+6 internal/assets/write.go+60 internal/git/diff.go+265 internal/llm/agent/agent.go+3 internal/llm/tools/bash.go+2−2 internal/llm/tools/edit.go+122−112 internal/llm/tools/edit_test.go−48 internal/llm/tools/file.go+10 internal/llm/tools/tools.go+15 internal/llm/tools/write.go+26−3 internal/tui/components/dialog/permission.go+11−6
.gitignore+1
@@ -43,3 +43,4 @@ debug.log
4343
4444.termai
4545
46+internal/assets/diff/index.mjs
README.md+13
@@ -102,6 +102,9 @@ TermAI is built with a modular architecture:
102102git clone https://github.com/kujtimiihoxha/termai.git
103103cd termai
104104
105+# Build the diff script first
106+go run cmd/diff/main.go
107+
105108# Build
106109go build -o termai
107110
@@ -109,6 +112,16 @@ go build -o termai
109112./termai
110113```
111114
115+### Important: Building the Diff Script
116+
117+Before building or running the application, you must first build the diff script by running:
118+
119+```bash
120+go run cmd/diff/main.go
121+```
122+
123+This command generates the necessary JavaScript file (`index.mjs`) used by the diff functionality in the application.
124+
112125## Acknowledgments
113126
114127TermAI builds upon the work of several open source projects and developers:
cmd/diff/main.gonew file+102
@@ -0,0 +1,102 @@
1+package main
2+
3+import (
4+ "fmt"
5+ "io"
6+ "os"
7+ "os/exec"
8+ "path/filepath"
9+)
10+
11+func main() {
12+ // Create a temporary directory
13+ tempDir, err := os.MkdirTemp("", "git-split-diffs")
14+ if err != nil {
15+ fmt.Printf("Error creating temp directory: %v\n", err)
16+ os.Exit(1)
17+ }
18+ defer func() {
19+ fmt.Printf("Cleaning up temporary directory: %s\n", tempDir)
20+ os.RemoveAll(tempDir)
21+ }()
22+ fmt.Printf("Created temporary directory: %s\n", tempDir)
23+
24+ // Clone the repository with minimum depth
25+ fmt.Println("Cloning git-split-diffs repository with minimum depth...")
26+ cmd := exec.Command("git", "clone", "--depth=1", "https://github.com/kujtimiihoxha/git-split-diffs", tempDir)
27+ cmd.Stdout = os.Stdout
28+ cmd.Stderr = os.Stderr
29+ if err := cmd.Run(); err != nil {
30+ fmt.Printf("Error cloning repository: %v\n", err)
31+ os.Exit(1)
32+ }
33+
34+ // Run npm install
35+ fmt.Println("Running npm install...")
36+ cmdNpmInstall := exec.Command("npm", "install")
37+ cmdNpmInstall.Dir = tempDir
38+ cmdNpmInstall.Stdout = os.Stdout
39+ cmdNpmInstall.Stderr = os.Stderr
40+ if err := cmdNpmInstall.Run(); err != nil {
41+ fmt.Printf("Error running npm install: %v\n", err)
42+ os.Exit(1)
43+ }
44+
45+ // Run npm run build
46+ fmt.Println("Running npm run build...")
47+ cmdNpmBuild := exec.Command("npm", "run", "build")
48+ cmdNpmBuild.Dir = tempDir
49+ cmdNpmBuild.Stdout = os.Stdout
50+ cmdNpmBuild.Stderr = os.Stderr
51+ if err := cmdNpmBuild.Run(); err != nil {
52+ fmt.Printf("Error running npm run build: %v\n", err)
53+ os.Exit(1)
54+ }
55+
56+ destDir := filepath.Join(".", "internal", "assets", "diff")
57+ destFile := filepath.Join(destDir, "index.mjs")
58+
59+ // Make sure the destination directory exists
60+ if err := os.MkdirAll(destDir, 0o755); err != nil {
61+ fmt.Printf("Error creating destination directory: %v\n", err)
62+ os.Exit(1)
63+ }
64+
65+ // Copy the file
66+ srcFile := filepath.Join(tempDir, "build", "index.mjs")
67+ fmt.Printf("Copying %s to %s\n", srcFile, destFile)
68+ if err := copyFile(srcFile, destFile); err != nil {
69+ fmt.Printf("Error copying file: %v\n", err)
70+ os.Exit(1)
71+ }
72+
73+ fmt.Println("Successfully completed the process!")
74+}
75+
76+// copyFile copies a file from src to dst
77+func copyFile(src, dst string) error {
78+ sourceFile, err := os.Open(src)
79+ if err != nil {
80+ return err
81+ }
82+ defer sourceFile.Close()
83+
84+ destFile, err := os.Create(dst)
85+ if err != nil {
86+ return err
87+ }
88+ defer destFile.Close()
89+
90+ _, err = io.Copy(destFile, sourceFile)
91+ if err != nil {
92+ return err
93+ }
94+
95+ // Make sure the file is written to disk
96+ err = destFile.Sync()
97+ if err != nil {
98+ return err
99+ }
100+
101+ return nil
102+}
cmd/git/main.gonew file+4
@@ -0,0 +1,4 @@
1+package main
2+
3+func main() {
4+}
cmd/root.go+6
@@ -8,6 +8,7 @@ import (
88
99 tea "github.com/charmbracelet/bubbletea"
1010 "github.com/kujtimiihoxha/termai/internal/app"
11+ "github.com/kujtimiihoxha/termai/internal/assets"
1112 "github.com/kujtimiihoxha/termai/internal/config"
1213 "github.com/kujtimiihoxha/termai/internal/db"
1314 "github.com/kujtimiihoxha/termai/internal/llm/agent"
@@ -28,6 +29,9 @@ var rootCmd = &cobra.Command{
2829 }
2930 debug, _ := cmd.Flags().GetBool("debug")
3031 err := config.Load(debug)
32+ if err != nil {
33+ return err
34+ }
3135 cfg := config.Get()
3236 defaultLevel := slog.LevelInfo
3337 if cfg.Debug {
@@ -38,9 +42,11 @@ var rootCmd = &cobra.Command{
3842 }))
3943 slog.SetDefault(logger)
4044
45+ err = assets.WriteAssets()
4146 if err != nil {
4247 return err
4348 }
49+
4450 conn, err := db.Connect()
4551 if err != nil {
4652 return err
go.mod+16−4
@@ -17,6 +17,7 @@ require (
1717 github.com/charmbracelet/lipgloss v1.1.0
1818 github.com/charmbracelet/x/ansi v0.8.0
1919 github.com/fsnotify/fsnotify v1.8.0
20+ github.com/go-git/go-git/v5 v5.15.0
2021 github.com/go-logfmt/logfmt v0.6.0
2122 github.com/golang-migrate/migrate/v4 v4.18.2
2223 github.com/google/generative-ai-go v0.19.0
@@ -45,6 +46,9 @@ require (
4546 cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
4647 cloud.google.com/go/compute/metadata v0.6.0 // indirect
4748 cloud.google.com/go/longrunning v0.5.7 // indirect
49+ dario.cat/mergo v1.0.0 // indirect
50+ github.com/Microsoft/go-winio v0.6.2 // indirect
51+ github.com/ProtonMail/go-crypto v1.1.6 // indirect
4852 github.com/alecthomas/chroma/v2 v2.15.0 // indirect
4953 github.com/andybalholm/cascadia v1.3.2 // indirect
5054 github.com/atotto/clipboard v0.1.4 // indirect
@@ -68,15 +72,20 @@ require (
6872 github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
6973 github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
7074 github.com/charmbracelet/x/term v0.2.1 // indirect
75+ github.com/cloudflare/circl v1.6.1 // indirect
76+ github.com/cyphar/filepath-securejoin v0.4.1 // indirect
7177 github.com/davecgh/go-spew v1.1.1 // indirect
7278 github.com/dlclark/regexp2 v1.11.4 // indirect
7379 github.com/dustin/go-humanize v1.0.1 // indirect
80+ github.com/emirpasic/gods v1.18.1 // indirect
7481 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
7582 github.com/felixge/httpsnoop v1.0.4 // indirect
83+ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
84+ github.com/go-git/go-billy/v5 v5.6.2 // indirect
7685 github.com/go-logr/logr v1.4.2 // indirect
7786 github.com/go-logr/stdr v1.2.2 // indirect
7887 github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
79− github.com/google/go-cmp v0.7.0 // indirect
88+ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
8089 github.com/google/s2a-go v0.1.8 // indirect
8190 github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
8291 github.com/googleapis/gax-go/v2 v2.14.1 // indirect
@@ -84,6 +93,8 @@ require (
8493 github.com/hashicorp/errwrap v1.1.0 // indirect
8594 github.com/hashicorp/go-multierror v1.1.1 // indirect
8695 github.com/inconshreveable/mousetrap v1.1.0 // indirect
96+ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
97+ github.com/kevinburke/ssh_config v1.2.0 // indirect
8798 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
8899 github.com/mattn/go-isatty v0.0.20 // indirect
89100 github.com/mattn/go-localereader v0.0.1 // indirect
@@ -91,11 +102,12 @@ require (
91102 github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
92103 github.com/muesli/cancelreader v0.2.2 // indirect
93104 github.com/pelletier/go-toml/v2 v2.2.3 // indirect
105+ github.com/pjbgf/sha1cd v0.3.2 // indirect
94106 github.com/pmezard/go-difflib v1.0.0 // indirect
95107 github.com/rivo/uniseg v0.4.7 // indirect
96− github.com/rogpeppe/go-internal v1.14.1 // indirect
97108 github.com/sagikazarmark/locafero v0.7.0 // indirect
98109 github.com/sahilm/fuzzy v0.1.1 // indirect
110+ github.com/skeema/knownhosts v1.3.1 // indirect
99111 github.com/sourcegraph/conc v0.3.0 // indirect
100112 github.com/spf13/afero v1.12.0 // indirect
101113 github.com/spf13/cast v1.7.1 // indirect
@@ -105,6 +117,7 @@ require (
105117 github.com/tidwall/match v1.1.1 // indirect
106118 github.com/tidwall/pretty v1.2.1 // indirect
107119 github.com/tidwall/sjson v1.2.5 // indirect
120+ github.com/xanzy/ssh-agent v0.3.3 // indirect
108121 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
109122 github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
110123 github.com/yuin/goldmark v1.7.8 // indirect
@@ -118,7 +131,6 @@ require (
118131 go.uber.org/multierr v1.9.0 // indirect
119132 golang.design/x/clipboard v0.7.0 // indirect
120133 golang.org/x/crypto v0.37.0 // indirect
121− golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
122134 golang.org/x/exp/shiny v0.0.0-20250305212735-054e65f0b394 // indirect
123135 golang.org/x/image v0.14.0 // indirect
124136 golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
@@ -132,6 +144,6 @@ require (
132144 google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
133145 google.golang.org/grpc v1.67.3 // indirect
134146 google.golang.org/protobuf v1.36.1 // indirect
135− gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
147+ gopkg.in/warnings.v0 v0.1.2 // indirect
136148 gopkg.in/yaml.v3 v3.0.1 // indirect
137149)
go.sum+56−1
@@ -10,10 +10,17 @@ cloud.google.com/go/compute/metadata v0.6.0 h1:A6hENjEsCDtC1k8byVsgwvVcioamEHvZ4
1010cloud.google.com/go/compute/metadata v0.6.0/go.mod h1:FjyFAW1MW0C203CEOMDTu3Dk1FlqW3Rga40jzHL4hfg=
1111cloud.google.com/go/longrunning v0.5.7 h1:WLbHekDbjK1fVFD3ibpFFVoyizlLRl73I7YKuAKilhU=
1212cloud.google.com/go/longrunning v0.5.7/go.mod h1:8GClkudohy1Fxm3owmBGid8W0pSgodEMwEAztp38Xng=
13+dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
14+dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
1315github.com/JohannesKaufmann/html-to-markdown v1.6.0 h1:04VXMiE50YYfCfLboJCLcgqF5x+rHJnb1ssNmqpLH/k=
1416github.com/JohannesKaufmann/html-to-markdown v1.6.0/go.mod h1:NUI78lGg/a7vpEJTz/0uOcYMaibytE4BUOQS8k78yPQ=
1517github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
1618github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
19+github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
20+github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
21+github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
22+github.com/ProtonMail/go-crypto v1.1.6 h1:ZcV+Ropw6Qn0AX9brlQLAUXfqLBc7Bl+f/DmNxpLfdw=
23+github.com/ProtonMail/go-crypto v1.1.6/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
1724github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
1825github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
1926github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
@@ -24,8 +31,12 @@ github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc
2431github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
2532github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
2633github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
34+github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
35+github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
2736github.com/anthropics/anthropic-sdk-go v0.2.0-beta.2 h1:h7qxtumNjKPWFv1QM/HJy60MteeW23iKeEtBoY7bYZk=
2837github.com/anthropics/anthropic-sdk-go v0.2.0-beta.2/go.mod h1:AapDW22irxK2PSumZiQXYUFvsdQgkwIWlpESweWZI/c=
38+github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
39+github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
2940github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
3041github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
3142github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
@@ -88,7 +99,11 @@ github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 h1:qko
8899github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ=
89100github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
90101github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
102+github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
103+github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
91104github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
105+github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
106+github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
92107github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
93108github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
94109github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -96,6 +111,10 @@ github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yA
96111github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
97112github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
98113github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
114+github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
115+github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
116+github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
117+github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
99118github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
100119github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
101120github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
@@ -104,6 +123,16 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
104123github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
105124github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
106125github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
126+github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
127+github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
128+github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
129+github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
130+github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM=
131+github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU=
132+github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
133+github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
134+github.com/go-git/go-git/v5 v5.15.0 h1:f5Qn0W0F7ry1iN0ZwIU5m/n7/BKB4hiZfc+zlZx7ly0=
135+github.com/go-git/go-git/v5 v5.15.0/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
107136github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
108137github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
109138github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -115,6 +144,8 @@ github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIx
115144github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
116145github.com/golang-migrate/migrate/v4 v4.18.2 h1:2VSCMz7x7mjyTXx3m2zPokOY82LTRgxK1yQYKo6wWQ8=
117146github.com/golang-migrate/migrate/v4 v4.18.2/go.mod h1:2CM6tJvn2kqPXwnXO/d3rAQYiyoIm180VsO8PRX6Rpk=
147+github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
148+github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
118149github.com/google/generative-ai-go v0.19.0 h1:R71szggh8wHMCUlEMsW2A/3T+5LdEIkiaHSYgSpUgdg=
119150github.com/google/generative-ai-go v0.19.0/go.mod h1:JYolL13VG7j79kM5BtHz4qwONHkeJQzOCkKXnpqtS/E=
120151github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
@@ -138,8 +169,11 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
138169github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
139170github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
140171github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
172+github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
173+github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
174+github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
175+github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
141176github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
142−github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
143177github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
144178github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
145179github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -179,11 +213,17 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
179213github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
180214github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
181215github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
216+github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
217+github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
182218github.com/openai/openai-go v0.1.0-beta.2 h1:Ra5nCFkbEl9w+UJwAciC4kqnIBUCcJazhmMA0/YN894=
183219github.com/openai/openai-go v0.1.0-beta.2/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=
184220github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
185221github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
222+github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
223+github.com/pjbgf/sha1cd v0.3.2/go.mod h1:zQWigSxVmsHEZow5qaLtPYxpcKMMQpa09ixqBxuCS6A=
186224github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
225+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
226+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
187227github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
188228github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
189229github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
@@ -203,6 +243,9 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
203243github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
204244github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
205245github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
246+github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
247+github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
248+github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
206249github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
207250github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
208251github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
@@ -216,6 +259,7 @@ github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
216259github.com/spf13/viper v1.20.0 h1:zrxIyR3RQIOsarIrgL8+sAvALXul9jeEPa06Y0Ph6vY=
217260github.com/spf13/viper v1.20.0/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
218261github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
262+github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
219263github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
220264github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
221265github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
@@ -232,6 +276,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
232276github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
233277github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
234278github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
279+github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
280+github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
235281github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
236282github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
237283github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
@@ -260,6 +306,7 @@ golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo=
260306golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E=
261307golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
262308golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
309+golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
263310golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
264311golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
265312golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
@@ -277,6 +324,7 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91
277324golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
278325golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
279326golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
327+golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
280328golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
281329golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
282330golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
@@ -294,10 +342,14 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
294342golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
295343golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
296344golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
345+golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
297346golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
347+golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
348+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
298349golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
299350golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
300351golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
352+golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
301353golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
302354golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
303355golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -320,6 +372,7 @@ golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
320372golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
321373golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
322374golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
375+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
323376golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
324377golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
325378golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
@@ -348,6 +401,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
348401gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
349402gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
350403gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
404+gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
405+gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
351406gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
352407gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
353408gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
internal/assets/diff/themes/dark.jsonnew file+73
@@ -0,0 +1,73 @@
1+{
2+ "SYNTAX_HIGHLIGHTING_THEME": "dark-plus",
3+ "DEFAULT_COLOR": {
4+ "color": "#ffffff",
5+ "backgroundColor": "#212121"
6+ },
7+ "COMMIT_HEADER_COLOR": {
8+ "color": "#cccccc"
9+ },
10+ "COMMIT_HEADER_LABEL_COLOR": {
11+ "color": "#00000022"
12+ },
13+ "COMMIT_SHA_COLOR": {
14+ "color": "#00eeaa"
15+ },
16+ "COMMIT_AUTHOR_COLOR": {
17+ "color": "#00aaee"
18+ },
19+ "COMMIT_DATE_COLOR": {
20+ "color": "#cccccc"
21+ },
22+ "COMMIT_MESSAGE_COLOR": {
23+ "color": "#cccccc"
24+ },
25+ "COMMIT_TITLE_COLOR": {
26+ "modifiers": [
27+ "bold"
28+ ]
29+ },
30+ "FILE_NAME_COLOR": {
31+ "color": "#ffdd99"
32+ },
33+ "BORDER_COLOR": {
34+ "color": "#ffdd9966",
35+ "modifiers": [
36+ "dim"
37+ ]
38+ },
39+ "HUNK_HEADER_COLOR": {
40+ "modifiers": [
41+ "dim"
42+ ]
43+ },
44+ "DELETED_WORD_COLOR": {
45+ "color": "#ffcccc",
46+ "backgroundColor": "#ff000033"
47+ },
48+ "INSERTED_WORD_COLOR": {
49+ "color": "#ccffcc",
50+ "backgroundColor": "#00ff0033"
51+ },
52+ "DELETED_LINE_NO_COLOR": {
53+ "color": "#00000022",
54+ "backgroundColor": "#00000022"
55+ },
56+ "INSERTED_LINE_NO_COLOR": {
57+ "color": "#00000022",
58+ "backgroundColor": "#00000022"
59+ },
60+ "UNMODIFIED_LINE_NO_COLOR": {
61+ "color": "#666666"
62+ },
63+ "DELETED_LINE_COLOR": {
64+ "color": "#cc6666",
65+ "backgroundColor": "#3a3030"
66+ },
67+ "INSERTED_LINE_COLOR": {
68+ "color": "#66cc66",
69+ "backgroundColor": "#303a30"
70+ },
71+ "UNMODIFIED_LINE_COLOR": {},
72+ "MISSING_LINE_COLOR": {}
73+}
internal/assets/embed.gonew file+6
@@ -0,0 +1,6 @@
1+package assets
2+
3+import "embed"
4+
5+//go:embed diff
6+var FS embed.FS
internal/assets/write.gonew file+60
@@ -0,0 +1,60 @@
1+package assets
2+
3+import (
4+ "os"
5+ "path/filepath"
6+
7+ "github.com/kujtimiihoxha/termai/internal/config"
8+)
9+
10+func WriteAssets() error {
11+ appCfg := config.Get()
12+ appWd := config.WorkingDirectory()
13+ scriptDir := filepath.Join(
14+ appWd,
15+ appCfg.Data.Directory,
16+ "diff",
17+ )
18+ scriptPath := filepath.Join(scriptDir, "index.mjs")
19+ // Before, run the script in cmd/diff/main.go to build this file
20+ if _, err := os.Stat(scriptPath); err != nil {
21+ scriptData, err := FS.ReadFile("diff/index.mjs")
22+ if err != nil {
23+ return err
24+ }
25+
26+ err = os.MkdirAll(scriptDir, 0o755)
27+ if err != nil {
28+ return err
29+ }
30+ err = os.WriteFile(scriptPath, scriptData, 0o755)
31+ if err != nil {
32+ return err
33+ }
34+ }
35+
36+ themeDir := filepath.Join(
37+ appWd,
38+ appCfg.Data.Directory,
39+ "themes",
40+ )
41+
42+ themePath := filepath.Join(themeDir, "dark.json")
43+
44+ if _, err := os.Stat(themePath); err != nil {
45+ themeData, err := FS.ReadFile("diff/themes/dark.json")
46+ if err != nil {
47+ return err
48+ }
49+
50+ err = os.MkdirAll(themeDir, 0o755)
51+ if err != nil {
52+ return err
53+ }
54+ err = os.WriteFile(themePath, themeData, 0o755)
55+ if err != nil {
56+ return err
57+ }
58+ }
59+ return nil
60+}
internal/git/diff.gonew file+265
@@ -0,0 +1,265 @@
1+package git
2+
3+import (
4+ "bytes"
5+ "fmt"
6+ "os"
7+ "os/exec"
8+ "path/filepath"
9+ "strings"
10+ "time"
11+
12+ "github.com/go-git/go-git/v5"
13+ "github.com/go-git/go-git/v5/plumbing/object"
14+ "github.com/kujtimiihoxha/termai/internal/config"
15+)
16+
17+type DiffStats struct {
18+ Additions int
19+ Removals int
20+}
21+
22+func GenerateGitDiff(filePath string, contentBefore string, contentAfter string) (string, error) {
23+ tempDir, err := os.MkdirTemp("", "git-diff-temp")
24+ if err != nil {
25+ return "", fmt.Errorf("failed to create temp dir: %w", err)
26+ }
27+ defer os.RemoveAll(tempDir)
28+
29+ repo, err := git.PlainInit(tempDir, false)
30+ if err != nil {
31+ return "", fmt.Errorf("failed to initialize git repo: %w", err)
32+ }
33+
34+ wt, err := repo.Worktree()
35+ if err != nil {
36+ return "", fmt.Errorf("failed to get worktree: %w", err)
37+ }
38+
39+ fullPath := filepath.Join(tempDir, filePath)
40+ if err = os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
41+ return "", fmt.Errorf("failed to create directories: %w", err)
42+ }
43+ if err = os.WriteFile(fullPath, []byte(contentBefore), 0o644); err != nil {
44+ return "", fmt.Errorf("failed to write 'before' content: %w", err)
45+ }
46+
47+ _, err = wt.Add(filePath)
48+ if err != nil {
49+ return "", fmt.Errorf("failed to add file to git: %w", err)
50+ }
51+
52+ beforeCommit, err := wt.Commit("Before", &git.CommitOptions{
53+ Author: &object.Signature{
54+ Name: "OpenCode",
55+ Email: "coder@opencode.ai",
56+ When: time.Now(),
57+ },
58+ })
59+ if err != nil {
60+ return "", fmt.Errorf("failed to commit 'before' version: %w", err)
61+ }
62+
63+ if err = os.WriteFile(fullPath, []byte(contentAfter), 0o644); err != nil {
64+ return "", fmt.Errorf("failed to write 'after' content: %w", err)
65+ }
66+
67+ _, err = wt.Add(filePath)
68+ if err != nil {
69+ return "", fmt.Errorf("failed to add updated file to git: %w", err)
70+ }
71+
72+ afterCommit, err := wt.Commit("After", &git.CommitOptions{
73+ Author: &object.Signature{
74+ Name: "OpenCode",
75+ Email: "coder@opencode.ai",
76+ When: time.Now(),
77+ },
78+ })
79+ if err != nil {
80+ return "", fmt.Errorf("failed to commit 'after' version: %w", err)
81+ }
82+
83+ beforeCommitObj, err := repo.CommitObject(beforeCommit)
84+ if err != nil {
85+ return "", fmt.Errorf("failed to get 'before' commit: %w", err)
86+ }
87+
88+ afterCommitObj, err := repo.CommitObject(afterCommit)
89+ if err != nil {
90+ return "", fmt.Errorf("failed to get 'after' commit: %w", err)
91+ }
92+
93+ patch, err := beforeCommitObj.Patch(afterCommitObj)
94+ if err != nil {
95+ return "", fmt.Errorf("failed to generate patch: %w", err)
96+ }
97+
98+ return patch.String(), nil
99+}
100+
101+func GenerateGitDiffWithStats(filePath string, contentBefore string, contentAfter string) (string, DiffStats, error) {
102+ tempDir, err := os.MkdirTemp("", "git-diff-temp")
103+ if err != nil {
104+ return "", DiffStats{}, fmt.Errorf("failed to create temp dir: %w", err)
105+ }
106+ defer os.RemoveAll(tempDir)
107+
108+ repo, err := git.PlainInit(tempDir, false)
109+ if err != nil {
110+ return "", DiffStats{}, fmt.Errorf("failed to initialize git repo: %w", err)
111+ }
112+
113+ wt, err := repo.Worktree()
114+ if err != nil {
115+ return "", DiffStats{}, fmt.Errorf("failed to get worktree: %w", err)
116+ }
117+
118+ fullPath := filepath.Join(tempDir, filePath)
119+ if err = os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
120+ return "", DiffStats{}, fmt.Errorf("failed to create directories: %w", err)
121+ }
122+ if err = os.WriteFile(fullPath, []byte(contentBefore), 0o644); err != nil {
123+ return "", DiffStats{}, fmt.Errorf("failed to write 'before' content: %w", err)
124+ }
125+
126+ _, err = wt.Add(filePath)
127+ if err != nil {
128+ return "", DiffStats{}, fmt.Errorf("failed to add file to git: %w", err)
129+ }
130+
131+ beforeCommit, err := wt.Commit("Before", &git.CommitOptions{
132+ Author: &object.Signature{
133+ Name: "OpenCode",
134+ Email: "coder@opencode.ai",
135+ When: time.Now(),
136+ },
137+ })
138+ if err != nil {
139+ return "", DiffStats{}, fmt.Errorf("failed to commit 'before' version: %w", err)
140+ }
141+
142+ if err = os.WriteFile(fullPath, []byte(contentAfter), 0o644); err != nil {
143+ return "", DiffStats{}, fmt.Errorf("failed to write 'after' content: %w", err)
144+ }
145+
146+ _, err = wt.Add(filePath)
147+ if err != nil {
148+ return "", DiffStats{}, fmt.Errorf("failed to add updated file to git: %w", err)
149+ }
150+
151+ afterCommit, err := wt.Commit("After", &git.CommitOptions{
152+ Author: &object.Signature{
153+ Name: "OpenCode",
154+ Email: "coder@opencode.ai",
155+ When: time.Now(),
156+ },
157+ })
158+ if err != nil {
159+ return "", DiffStats{}, fmt.Errorf("failed to commit 'after' version: %w", err)
160+ }
161+
162+ beforeCommitObj, err := repo.CommitObject(beforeCommit)
163+ if err != nil {
164+ return "", DiffStats{}, fmt.Errorf("failed to get 'before' commit: %w", err)
165+ }
166+
167+ afterCommitObj, err := repo.CommitObject(afterCommit)
168+ if err != nil {
169+ return "", DiffStats{}, fmt.Errorf("failed to get 'after' commit: %w", err)
170+ }
171+
172+ patch, err := beforeCommitObj.Patch(afterCommitObj)
173+ if err != nil {
174+ return "", DiffStats{}, fmt.Errorf("failed to generate patch: %w", err)
175+ }
176+
177+ stats := DiffStats{}
178+ for _, fileStat := range patch.Stats() {
179+ stats.Additions += fileStat.Addition
180+ stats.Removals += fileStat.Deletion
181+ }
182+
183+ return patch.String(), stats, nil
184+}
185+
186+func FormatDiff(diffText string, width int) (string, error) {
187+ if isSplitDiffsAvailable() {
188+ return formatWithSplitDiffs(diffText, width)
189+ }
190+
191+ return formatSimple(diffText), nil
192+}
193+
194+func isSplitDiffsAvailable() bool {
195+ _, err := exec.LookPath("node")
196+ return err == nil
197+}
198+
199+func formatWithSplitDiffs(diffText string, width int) (string, error) {
200+ var cmd *exec.Cmd
201+
202+ appCfg := config.Get()
203+ appWd := config.WorkingDirectory()
204+ script := filepath.Join(
205+ appWd,
206+ appCfg.Data.Directory,
207+ "diff",
208+ "index.mjs",
209+ )
210+
211+ cmd = exec.Command("node", script, "--color")
212+
213+ cmd.Env = append(os.Environ(), fmt.Sprintf("COLUMNS=%d", width))
214+
215+ cmd.Stdin = strings.NewReader(diffText)
216+
217+ var out bytes.Buffer
218+ cmd.Stdout = &out
219+
220+ var stderr bytes.Buffer
221+ cmd.Stderr = &stderr
222+
223+ err := cmd.Run()
224+ if err != nil {
225+ return "", fmt.Errorf("git-split-diffs error: %v, stderr: %s", err, stderr.String())
226+ }
227+
228+ return out.String(), nil
229+}
230+
231+func formatSimple(diffText string) string {
232+ lines := strings.Split(diffText, "\n")
233+ var result strings.Builder
234+
235+ for _, line := range lines {
236+ if len(line) == 0 {
237+ result.WriteString("\n")
238+ continue
239+ }
240+
241+ switch line[0] {
242+ case '+':
243+ result.WriteString("\033[32m" + line + "\033[0m\n")
244+ case '-':
245+ result.WriteString("\033[31m" + line + "\033[0m\n")
246+ case '@':
247+ result.WriteString("\033[36m" + line + "\033[0m\n")
248+ case 'd':
249+ if strings.HasPrefix(line, "diff --git") {
250+ result.WriteString("\033[1m" + line + "\033[0m\n")
251+ } else {
252+ result.WriteString(line + "\n")
253+ }
254+ default:
255+ result.WriteString(line + "\n")
256+ }
257+ }
258+
259+ if !strings.HasSuffix(diffText, "\n") {
260+ output := result.String()
261+ return output[:len(output)-1]
262+ }
263+
264+ return result.String()
265+}
internal/llm/agent/agent.go+3
@@ -246,6 +246,7 @@ func (c *agent) handleToolExecution(
246246}
247247
248248func (c *agent) generate(ctx context.Context, sessionID string, content string) error {
249+ ctx = context.WithValue(ctx, tools.SessionIDContextKey, sessionID)
249250 messages, err := c.Messages.List(sessionID)
250251 if err != nil {
251252 return err
@@ -310,6 +311,8 @@ func (c *agent) generate(ctx context.Context, sessionID string, content string)
310311 if err != nil {
311312 return err
312313 }
314+
315+ ctx = context.WithValue(ctx, tools.MessageIDContextKey, assistantMsg.ID)
313316 for event := range eventChan {
314317 err = c.processEvent(sessionID, &assistantMsg, event)
315318 if err != nil {
internal/llm/tools/bash.go+2−2
@@ -22,7 +22,7 @@ type BashPermissionsParams struct {
2222 Timeout int `json:"timeout"`
2323}
2424
25−type BashToolResponseMetadata struct {
25+type BashResponseMetadata struct {
2626 Took int64 `json:"took"`
2727}
2828type bashTool struct {
@@ -310,7 +310,7 @@ func (b *bashTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
310310 stdout += "\n" + errorMessage
311311 }
312312
313− metadata := BashToolResponseMetadata{
313+ metadata := BashResponseMetadata{
314314 Took: took,
315315 }
316316 if stdout == "" {
internal/llm/tools/edit.go+122−112
@@ -10,9 +10,9 @@ import (
1010 "time"
1111
1212 "github.com/kujtimiihoxha/termai/internal/config"
13+ "github.com/kujtimiihoxha/termai/internal/git"
1314 "github.com/kujtimiihoxha/termai/internal/lsp"
1415 "github.com/kujtimiihoxha/termai/internal/permission"
15− "github.com/sergi/go-diff/diffmatchpatch"
1616)
1717
1818type EditParams struct {
@@ -22,10 +22,13 @@ type EditParams struct {
2222}
2323
2424type EditPermissionsParams struct {
25− FilePath string `json:"file_path"`
26− OldString string `json:"old_string"`
27− NewString string `json:"new_string"`
28− Diff string `json:"diff"`
25+ FilePath string `json:"file_path"`
26+ Diff string `json:"diff"`
27+}
28+
29+type EditResponseMetadata struct {
30+ Additions int `json:"additions"`
31+ Removals int `json:"removals"`
2932}
3033
3134type editTool struct {
@@ -129,48 +132,77 @@ func (e *editTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error)
129132 }
130133
131134 if params.OldString == "" {
132− result, err := e.createNewFile(params.FilePath, params.NewString)
135+ result, err := e.createNewFile(ctx, params.FilePath, params.NewString)
133136 if err != nil {
134137 return NewTextErrorResponse(fmt.Sprintf("error creating file: %s", err)), nil
135138 }
136− return NewTextResponse(result), nil
139+ return WithResponseMetadata(NewTextResponse(result.text), EditResponseMetadata{
140+ Additions: result.additions,
141+ Removals: result.removals,
142+ }), nil
137143 }
138144
139145 if params.NewString == "" {
140− result, err := e.deleteContent(params.FilePath, params.OldString)
146+ result, err := e.deleteContent(ctx, params.FilePath, params.OldString)
141147 if err != nil {
142148 return NewTextErrorResponse(fmt.Sprintf("error deleting content: %s", err)), nil
143149 }
144− return NewTextResponse(result), nil
150+ return WithResponseMetadata(NewTextResponse(result.text), EditResponseMetadata{
151+ Additions: result.additions,
152+ Removals: result.removals,
153+ }), nil
145154 }
146155
147− result, err := e.replaceContent(params.FilePath, params.OldString, params.NewString)
156+ result, err := e.replaceContent(ctx, params.FilePath, params.OldString, params.NewString)
148157 if err != nil {
149158 return NewTextErrorResponse(fmt.Sprintf("error replacing content: %s", err)), nil
150159 }
151160
152161 waitForLspDiagnostics(ctx, params.FilePath, e.lspClients)
153− result = fmt.Sprintf("<result>\n%s\n</result>\n", result)
154− result += appendDiagnostics(params.FilePath, e.lspClients)
155− return NewTextResponse(result), nil
162+ text := fmt.Sprintf("<result>\n%s\n</result>\n", result.text)
163+ text += appendDiagnostics(params.FilePath, e.lspClients)
164+ return WithResponseMetadata(NewTextResponse(text), EditResponseMetadata{
165+ Additions: result.additions,
166+ Removals: result.removals,
167+ }), nil
168+}
169+
170+type editResponse struct {
171+ text string
172+ additions int
173+ removals int
156174}
157175
158−func (e *editTool) createNewFile(filePath, content string) (string, error) {
176+func (e *editTool) createNewFile(ctx context.Context, filePath, content string) (editResponse, error) {
177+ er := editResponse{}
159178 fileInfo, err := os.Stat(filePath)
160179 if err == nil {
161180 if fileInfo.IsDir() {
162− return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
181+ return er, fmt.Errorf("path is a directory, not a file: %s", filePath)
163182 }
164− return "", fmt.Errorf("file already exists: %s. Use the Replace tool to overwrite an existing file", filePath)
183+ return er, fmt.Errorf("file already exists: %s. Use the Replace tool to overwrite an existing file", filePath)
165184 } else if !os.IsNotExist(err) {
166− return "", fmt.Errorf("failed to access file: %w", err)
185+ return er, fmt.Errorf("failed to access file: %w", err)
167186 }
168187
169188 dir := filepath.Dir(filePath)
170189 if err = os.MkdirAll(dir, 0o755); err != nil {
171− return "", fmt.Errorf("failed to create parent directories: %w", err)
190+ return er, fmt.Errorf("failed to create parent directories: %w", err)
172191 }
173192
193+ sessionID, messageID := getContextValues(ctx)
194+ if sessionID == "" || messageID == "" {
195+ return er, fmt.Errorf("session ID and message ID are required for creating a new file")
196+ }
197+
198+ diff, stats, err := git.GenerateGitDiffWithStats(
199+ removeWorkingDirectoryPrefix(filePath),
200+ "",
201+ content,
202+ )
203+ if err != nil {
204+ return er, fmt.Errorf("failed to get file diff: %w", err)
205+ }
174206 p := e.permissions.Request(
175207 permission.CreatePermissionRequest{
176208 Path: filepath.Dir(filePath),
@@ -178,71 +210,88 @@ func (e *editTool) createNewFile(filePath, content string) (string, error) {
178210 Action: "create",
179211 Description: fmt.Sprintf("Create file %s", filePath),
180212 Params: EditPermissionsParams{
181− FilePath: filePath,
182− OldString: "",
183− NewString: content,
184− Diff: GenerateDiff("", content),
213+ FilePath: filePath,
214+ Diff: diff,
185215 },
186216 },
187217 )
188218 if !p {
189− return "", fmt.Errorf("permission denied")
219+ return er, fmt.Errorf("permission denied")
190220 }
191221
192222 err = os.WriteFile(filePath, []byte(content), 0o644)
193223 if err != nil {
194− return "", fmt.Errorf("failed to write file: %w", err)
224+ return er, fmt.Errorf("failed to write file: %w", err)
195225 }
196226
197227 recordFileWrite(filePath)
198228 recordFileRead(filePath)
199229
200− return "File created: " + filePath, nil
230+ er.text = "File created: " + filePath
231+ er.additions = stats.Additions
232+ er.removals = stats.Removals
233+ return er, nil
201234}
202235
203−func (e *editTool) deleteContent(filePath, oldString string) (string, error) {
236+func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string) (editResponse, error) {
237+ er := editResponse{}
204238 fileInfo, err := os.Stat(filePath)
205239 if err != nil {
206240 if os.IsNotExist(err) {
207− return "", fmt.Errorf("file not found: %s", filePath)
241+ return er, fmt.Errorf("file not found: %s", filePath)
208242 }
209− return "", fmt.Errorf("failed to access file: %w", err)
243+ return er, fmt.Errorf("failed to access file: %w", err)
210244 }
211245
212246 if fileInfo.IsDir() {
213− return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
247+ return er, fmt.Errorf("path is a directory, not a file: %s", filePath)
214248 }
215249
216250 if getLastReadTime(filePath).IsZero() {
217− return "", fmt.Errorf("you must read the file before editing it. Use the View tool first")
251+ return er, fmt.Errorf("you must read the file before editing it. Use the View tool first")
218252 }
219253
220254 modTime := fileInfo.ModTime()
221255 lastRead := getLastReadTime(filePath)
222256 if modTime.After(lastRead) {
223− return "", fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
257+ return er, fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
224258 filePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339))
225259 }
226260
227261 content, err := os.ReadFile(filePath)
228262 if err != nil {
229− return "", fmt.Errorf("failed to read file: %w", err)
263+ return er, fmt.Errorf("failed to read file: %w", err)
230264 }
231265
232266 oldContent := string(content)
233267
234268 index := strings.Index(oldContent, oldString)
235269 if index == -1 {
236− return "", fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
270+ return er, fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
237271 }
238272
239273 lastIndex := strings.LastIndex(oldContent, oldString)
240274 if index != lastIndex {
241− return "", fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
275+ return er, fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
242276 }
243277
244278 newContent := oldContent[:index] + oldContent[index+len(oldString):]
245279
280+ sessionID, messageID := getContextValues(ctx)
281+
282+ if sessionID == "" || messageID == "" {
283+ return er, fmt.Errorf("session ID and message ID are required for creating a new file")
284+ }
285+
286+ diff, stats, err := git.GenerateGitDiffWithStats(
287+ removeWorkingDirectoryPrefix(filePath),
288+ oldContent,
289+ newContent,
290+ )
291+ if err != nil {
292+ return er, fmt.Errorf("failed to get file diff: %w", err)
293+ }
294+
246295 p := e.permissions.Request(
247296 permission.CreatePermissionRequest{
248297 Path: filepath.Dir(filePath),
@@ -250,76 +299,85 @@ func (e *editTool) deleteContent(filePath, oldString string) (string, error) {
250299 Action: "delete",
251300 Description: fmt.Sprintf("Delete content from file %s", filePath),
252301 Params: EditPermissionsParams{
253− FilePath: filePath,
254− OldString: oldString,
255− NewString: "",
256− Diff: GenerateDiff(oldContent, newContent),
302+ FilePath: filePath,
303+ Diff: diff,
257304 },
258305 },
259306 )
260307 if !p {
261− return "", fmt.Errorf("permission denied")
308+ return er, fmt.Errorf("permission denied")
262309 }
263310
264311 err = os.WriteFile(filePath, []byte(newContent), 0o644)
265312 if err != nil {
266− return "", fmt.Errorf("failed to write file: %w", err)
313+ return er, fmt.Errorf("failed to write file: %w", err)
267314 }
268−
269315 recordFileWrite(filePath)
270316 recordFileRead(filePath)
271317
272− return "Content deleted from file: " + filePath, nil
318+ er.text = "Content deleted from file: " + filePath
319+ er.additions = stats.Additions
320+ er.removals = stats.Removals
321+ return er, nil
273322}
274323
275−func (e *editTool) replaceContent(filePath, oldString, newString string) (string, error) {
324+func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newString string) (editResponse, error) {
325+ er := editResponse{}
276326 fileInfo, err := os.Stat(filePath)
277327 if err != nil {
278328 if os.IsNotExist(err) {
279− return "", fmt.Errorf("file not found: %s", filePath)
329+ return er, fmt.Errorf("file not found: %s", filePath)
280330 }
281− return "", fmt.Errorf("failed to access file: %w", err)
331+ return er, fmt.Errorf("failed to access file: %w", err)
282332 }
283333
284334 if fileInfo.IsDir() {
285− return "", fmt.Errorf("path is a directory, not a file: %s", filePath)
335+ return er, fmt.Errorf("path is a directory, not a file: %s", filePath)
286336 }
287337
288338 if getLastReadTime(filePath).IsZero() {
289− return "", fmt.Errorf("you must read the file before editing it. Use the View tool first")
339+ return er, fmt.Errorf("you must read the file before editing it. Use the View tool first")
290340 }
291341
292342 modTime := fileInfo.ModTime()
293343 lastRead := getLastReadTime(filePath)
294344 if modTime.After(lastRead) {
295− return "", fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
345+ return er, fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
296346 filePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339))
297347 }
298348
299349 content, err := os.ReadFile(filePath)
300350 if err != nil {
301− return "", fmt.Errorf("failed to read file: %w", err)
351+ return er, fmt.Errorf("failed to read file: %w", err)
302352 }
303353
304354 oldContent := string(content)
305355
306356 index := strings.Index(oldContent, oldString)
307357 if index == -1 {
308− return "", fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
358+ return er, fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
309359 }
310360
311361 lastIndex := strings.LastIndex(oldContent, oldString)
312362 if index != lastIndex {
313− return "", fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
363+ return er, fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
314364 }
315365
316366 newContent := oldContent[:index] + newString + oldContent[index+len(oldString):]
317367
318− startIndex := max(0, index-3)
319− oldEndIndex := min(len(oldContent), index+len(oldString)+3)
320− newEndIndex := min(len(newContent), index+len(newString)+3)
368+ sessionID, messageID := getContextValues(ctx)
321369
322− diff := GenerateDiff(oldContent[startIndex:oldEndIndex], newContent[startIndex:newEndIndex])
370+ if sessionID == "" || messageID == "" {
371+ return er, fmt.Errorf("session ID and message ID are required for creating a new file")
372+ }
373+ diff, stats, err := git.GenerateGitDiffWithStats(
374+ removeWorkingDirectoryPrefix(filePath),
375+ oldContent,
376+ newContent,
377+ )
378+ if err != nil {
379+ return er, fmt.Errorf("failed to get file diff: %w", err)
380+ }
323381
324382 p := e.permissions.Request(
325383 permission.CreatePermissionRequest{
@@ -328,75 +386,27 @@ func (e *editTool) replaceContent(filePath, oldString, newString string) (string
328386 Action: "replace",
329387 Description: fmt.Sprintf("Replace content in file %s", filePath),
330388 Params: EditPermissionsParams{
331− FilePath: filePath,
332− OldString: oldString,
333− NewString: newString,
334− Diff: diff,
389+ FilePath: filePath,
390+
391+ Diff: diff,
335392 },
336393 },
337394 )
338395 if !p {
339− return "", fmt.Errorf("permission denied")
396+ return er, fmt.Errorf("permission denied")
340397 }
341398
342399 err = os.WriteFile(filePath, []byte(newContent), 0o644)
343400 if err != nil {
344− return "", fmt.Errorf("failed to write file: %w", err)
401+ return er, fmt.Errorf("failed to write file: %w", err)
345402 }
346403
347404 recordFileWrite(filePath)
348405 recordFileRead(filePath)
406+ er.text = "Content replaced in file: " + filePath
407+ er.additions = stats.Additions
408+ er.removals = stats.Removals
349409
350− return "Content replaced in file: " + filePath, nil
410+ return er, nil
351411}
352412
353−func GenerateDiff(oldContent, newContent string) string {
354− dmp := diffmatchpatch.New()
355− fileAdmp, fileBdmp, dmpStrings := dmp.DiffLinesToChars(oldContent, newContent)
356− diffs := dmp.DiffMain(fileAdmp, fileBdmp, false)
357− diffs = dmp.DiffCharsToLines(diffs, dmpStrings)
358− diffs = dmp.DiffCleanupSemantic(diffs)
359− buff := strings.Builder{}
360−
361− buff.WriteString("Changes:\n")
362−
363− for _, diff := range diffs {
364− text := diff.Text
365−
366− switch diff.Type {
367− case diffmatchpatch.DiffInsert:
368− for line := range strings.SplitSeq(text, "\n") {
369− if line == "" {
370− continue
371− }
372− _, _ = buff.WriteString("+ " + line + "\n")
373− }
374− case diffmatchpatch.DiffDelete:
375− for line := range strings.SplitSeq(text, "\n") {
376− if line == "" {
377− continue
378− }
379− _, _ = buff.WriteString("- " + line + "\n")
380− }
381− case diffmatchpatch.DiffEqual:
382− lines := strings.Split(text, "\n")
383− if len(lines) > 3 {
384− if lines[0] != "" {
385− _, _ = buff.WriteString(" " + lines[0] + "\n")
386− }
387− _, _ = buff.WriteString(" ...\n")
388− if lines[len(lines)-1] != "" {
389− _, _ = buff.WriteString(" " + lines[len(lines)-1] + "\n")
390− }
391− } else {
392− for _, line := range lines {
393− if line == "" {
394− continue
395− }
396− _, _ = buff.WriteString(" " + line + "\n")
397− }
398− }
399− }
400− }
401− return buff.String()
402−}
internal/llm/tools/edit_test.go−48
@@ -459,51 +459,3 @@ func TestEditTool_Run(t *testing.T) {
459459 assert.Equal(t, initialContent, string(fileContent))
460460 })
461461}
462−
463−func TestGenerateDiff(t *testing.T) {
464− testCases := []struct {
465− name string
466− oldContent string
467− newContent string
468− expectedDiff string
469− }{
470− {
471− name: "add content",
472− oldContent: "Line 1\nLine 2\n",
473− newContent: "Line 1\nLine 2\nLine 3\n",
474− expectedDiff: "Changes:\n Line 1\n Line 2\n+ Line 3\n",
475− },
476− {
477− name: "remove content",
478− oldContent: "Line 1\nLine 2\nLine 3\n",
479− newContent: "Line 1\nLine 3\n",
480− expectedDiff: "Changes:\n Line 1\n- Line 2\n Line 3\n",
481− },
482− {
483− name: "replace content",
484− oldContent: "Line 1\nLine 2\nLine 3\n",
485− newContent: "Line 1\nModified Line\nLine 3\n",
486− expectedDiff: "Changes:\n Line 1\n- Line 2\n+ Modified Line\n Line 3\n",
487− },
488− {
489− name: "empty to content",
490− oldContent: "",
491− newContent: "Line 1\nLine 2\n",
492− expectedDiff: "Changes:\n+ Line 1\n+ Line 2\n",
493− },
494− {
495− name: "content to empty",
496− oldContent: "Line 1\nLine 2\n",
497− newContent: "",
498− expectedDiff: "Changes:\n- Line 1\n- Line 2\n",
499− },
500− }
501−
502− for _, tc := range testCases {
503− t.Run(tc.name, func(t *testing.T) {
504− diff := GenerateDiff(tc.oldContent, tc.newContent)
505− assert.Contains(t, diff, tc.expectedDiff)
506− })
507− }
508−}
509−
internal/llm/tools/file.go+10
@@ -3,6 +3,8 @@ package tools
33import (
44 "sync"
55 "time"
6+
7+ "github.com/kujtimiihoxha/termai/internal/config"
68)
79
810// File record to track when files were read/written
@@ -17,6 +19,14 @@ var (
1719 fileRecordMutex sync.RWMutex
1820)
1921
22+func removeWorkingDirectoryPrefix(path string) string {
23+ wd := config.WorkingDirectory()
24+ if len(path) > len(wd) && path[:len(wd)] == wd {
25+ return path[len(wd)+1:]
26+ }
27+ return path
28+}
29+
2030func recordFileRead(path string) {
2131 fileRecordMutex.Lock()
2232 defer fileRecordMutex.Unlock()
internal/llm/tools/tools.go+15
@@ -17,6 +17,9 @@ type toolResponseType string
1717const (
1818 ToolResponseTypeText toolResponseType = "text"
1919 ToolResponseTypeImage toolResponseType = "image"
20+
21+ SessionIDContextKey = "session_id"
22+ MessageIDContextKey = "message_id"
2023)
2124
2225type ToolResponse struct {
@@ -62,3 +65,15 @@ type BaseTool interface {
6265 Info() ToolInfo
6366 Run(ctx context.Context, params ToolCall) (ToolResponse, error)
6467}
68+
69+func getContextValues(ctx context.Context) (string, string) {
70+ sessionID := ctx.Value(SessionIDContextKey)
71+ messageID := ctx.Value(MessageIDContextKey)
72+ if sessionID == nil {
73+ return "", ""
74+ }
75+ if messageID == nil {
76+ return sessionID.(string), ""
77+ }
78+ return sessionID.(string), messageID.(string)
79+}
internal/llm/tools/write.go+26−3
@@ -9,6 +9,7 @@ import (
99 "time"
1010
1111 "github.com/kujtimiihoxha/termai/internal/config"
12+ "github.com/kujtimiihoxha/termai/internal/git"
1213 "github.com/kujtimiihoxha/termai/internal/lsp"
1314 "github.com/kujtimiihoxha/termai/internal/permission"
1415)
@@ -20,7 +21,7 @@ type WriteParams struct {
2021
2122type WritePermissionsParams struct {
2223 FilePath string `json:"file_path"`
23− Content string `json:"content"`
24+ Diff string `json:"diff"`
2425}
2526
2627type writeTool struct {
@@ -28,6 +29,11 @@ type writeTool struct {
2829 permissions permission.Service
2930}
3031
32+type WriteResponseMetadata struct {
33+ Additions int `json:"additions"`
34+ Removals int `json:"removals"`
35+}
36+
3137const (
3238 WriteToolName = "write"
3339 writeDescription = `File writing tool that creates or updates files in the filesystem, allowing you to save or modify text content.
@@ -138,6 +144,18 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
138144 }
139145 }
140146
147+ sessionID, messageID := getContextValues(ctx)
148+ if sessionID == "" || messageID == "" {
149+ return NewTextErrorResponse("session ID or message ID is missing"), nil
150+ }
151+ diff, stats, err := git.GenerateGitDiffWithStats(
152+ removeWorkingDirectoryPrefix(filePath),
153+ oldContent,
154+ params.Content,
155+ )
156+ if err != nil {
157+ return NewTextErrorResponse(fmt.Sprintf("Failed to get file diff: %s", err)), nil
158+ }
141159 p := w.permissions.Request(
142160 permission.CreatePermissionRequest{
143161 Path: filePath,
@@ -146,7 +164,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
146164 Description: fmt.Sprintf("Create file %s", filePath),
147165 Params: WritePermissionsParams{
148166 FilePath: filePath,
149− Content: GenerateDiff(oldContent, params.Content),
167+ Diff: diff,
150168 },
151169 },
152170 )
@@ -166,5 +184,10 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
166184 result := fmt.Sprintf("File successfully written: %s", filePath)
167185 result = fmt.Sprintf("<result>\n%s\n</result>", result)
168186 result += appendDiagnostics(filePath, w.lspClients)
169− return NewTextResponse(result), nil
187+ return WithResponseMetadata(NewTextResponse(result),
188+ WriteResponseMetadata{
189+ Additions: stats.Additions,
190+ Removals: stats.Removals,
191+ },
192+ ), nil
170193}
internal/tui/components/dialog/permission.go+11−6
@@ -9,6 +9,7 @@ import (
99 tea "github.com/charmbracelet/bubbletea"
1010 "github.com/charmbracelet/glamour"
1111 "github.com/charmbracelet/lipgloss"
12+ "github.com/kujtimiihoxha/termai/internal/git"
1213 "github.com/kujtimiihoxha/termai/internal/llm/tools"
1314 "github.com/kujtimiihoxha/termai/internal/permission"
1415 "github.com/kujtimiihoxha/termai/internal/tui/components/core"
@@ -234,7 +235,6 @@ func (p *permissionDialogCmp) render() string {
234235 headerContent = lipgloss.NewStyle().Padding(0, 1).Render(lipgloss.JoinVertical(lipgloss.Left, headerParts...))
235236
236237 // Format the diff with colors
237− formattedDiff := formatDiff(pr.Diff)
238238
239239 // Set up viewport for the diff content
240240 p.contentViewPort.Width = p.width - 2 - 2
@@ -242,7 +242,11 @@ func (p *permissionDialogCmp) render() string {
242242 // Calculate content height dynamically based on window size
243243 maxContentHeight := p.height - lipgloss.Height(headerContent) - lipgloss.Height(form) - 2 - 2 - 1
244244 p.contentViewPort.Height = maxContentHeight
245− p.contentViewPort.SetContent(formattedDiff)
245+ diff, err := git.FormatDiff(pr.Diff, p.contentViewPort.Width)
246+ if err != nil {
247+ diff = fmt.Sprintf("Error formatting diff: %v", err)
248+ }
249+ p.contentViewPort.SetContent(diff)
246250
247251 // Style the viewport
248252 var contentBorder lipgloss.Border
@@ -281,16 +285,17 @@ func (p *permissionDialogCmp) render() string {
281285 // Recreate header content with the updated headerParts
282286 headerContent = lipgloss.NewStyle().Padding(0, 1).Render(lipgloss.JoinVertical(lipgloss.Left, headerParts...))
283287
284− // Format the diff with colors
285− formattedDiff := formatDiff(pr.Content)
286−
287288 // Set up viewport for the content
288289 p.contentViewPort.Width = p.width - 2 - 2
289290
290291 // Calculate content height dynamically based on window size
291292 maxContentHeight := p.height - lipgloss.Height(headerContent) - lipgloss.Height(form) - 2 - 2 - 1
292293 p.contentViewPort.Height = maxContentHeight
293− p.contentViewPort.SetContent(formattedDiff)
294+ diff, err := git.FormatDiff(pr.Diff, p.contentViewPort.Width)
295+ if err != nil {
296+ diff = fmt.Sprintf("Error formatting diff: %v", err)
297+ }
298+ p.contentViewPort.SetContent(diff)
294299
295300 // Style the viewport
296301 var contentBorder lipgloss.Border