Skip to content
This repository was archived by the owner on Sep 18, 2025. It is now read-only.

Commit 36f201d

Browse files
committed
fix logs and add cancellation
1 parent 0b007b9 commit 36f201d

File tree

23 files changed

+343
-283
lines changed

23 files changed

+343
-283
lines changed

cmd/root.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"log/slog"
56
"os"
67
"sync"
78

@@ -10,6 +11,7 @@ import (
1011
"github.com/kujtimiihoxha/termai/internal/config"
1112
"github.com/kujtimiihoxha/termai/internal/db"
1213
"github.com/kujtimiihoxha/termai/internal/llm/agent"
14+
"github.com/kujtimiihoxha/termai/internal/logging"
1315
"github.com/kujtimiihoxha/termai/internal/tui"
1416
zone "github.com/lrstanley/bubblezone"
1517
"github.com/spf13/cobra"
@@ -26,6 +28,16 @@ var rootCmd = &cobra.Command{
2628
}
2729
debug, _ := cmd.Flags().GetBool("debug")
2830
err := config.Load(debug)
31+
cfg := config.Get()
32+
defaultLevel := slog.LevelInfo
33+
if cfg.Debug {
34+
defaultLevel = slog.LevelDebug
35+
}
36+
logger := slog.New(slog.NewTextHandler(logging.NewWriter(), &slog.HandlerOptions{
37+
Level: defaultLevel,
38+
}))
39+
slog.SetDefault(logger)
40+
2941
if err != nil {
3042
return err
3143
}
@@ -37,14 +49,14 @@ var rootCmd = &cobra.Command{
3749

3850
app := app.New(ctx, conn)
3951
defer app.Close()
40-
app.Logger.Info("Starting termai...")
52+
logging.Info("Starting termai...")
4153
zone.NewGlobal()
4254
tui := tea.NewProgram(
4355
tui.New(app),
4456
tea.WithAltScreen(),
4557
tea.WithMouseCellMotion(),
4658
)
47-
app.Logger.Info("Setting up subscriptions...")
59+
logging.Info("Setting up subscriptions...")
4860
ch, unsub := setupSubscriptions(app)
4961
defer unsub()
5062

@@ -66,9 +78,8 @@ func setupSubscriptions(app *app.App) (chan tea.Msg, func()) {
6678
ch := make(chan tea.Msg)
6779
wg := sync.WaitGroup{}
6880
ctx, cancel := context.WithCancel(app.Context)
69-
7081
{
71-
sub := app.Logger.Subscribe(ctx)
82+
sub := logging.Subscribe(ctx)
7283
wg.Add(1)
7384
go func() {
7485
for ev := range sub {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/spf13/cobra v1.9.1
3434
github.com/spf13/viper v1.20.0
3535
github.com/stretchr/testify v1.10.0
36-
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
36+
golang.org/x/net v0.34.0
3737
google.golang.org/api v0.215.0
3838
)
3939

@@ -116,10 +116,10 @@ require (
116116
go.uber.org/multierr v1.9.0 // indirect
117117
golang.design/x/clipboard v0.7.0 // indirect
118118
golang.org/x/crypto v0.33.0 // indirect
119+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
119120
golang.org/x/exp/shiny v0.0.0-20250305212735-054e65f0b394 // indirect
120121
golang.org/x/image v0.14.0 // indirect
121122
golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
122-
golang.org/x/net v0.34.0 // indirect
123123
golang.org/x/oauth2 v0.25.0 // indirect
124124
golang.org/x/sync v0.12.0 // indirect
125125
golang.org/x/sys v0.31.0 // indirect

internal/app/services.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"context"
55
"database/sql"
6+
"log/slog"
67

78
"github.com/kujtimiihoxha/termai/internal/config"
89
"github.com/kujtimiihoxha/termai/internal/db"
@@ -23,16 +24,14 @@ type App struct {
2324

2425
LSPClients map[string]*lsp.Client
2526

26-
Logger logging.Interface
27-
2827
ceanups []func()
2928
}
3029

3130
func New(ctx context.Context, conn *sql.DB) *App {
3231
cfg := config.Get()
32+
logging.Info("Debug mode enabled")
33+
3334
q := db.New(conn)
34-
log := logging.Get()
35-
log.SetLevel(cfg.Log.Level)
3635
sessions := session.NewService(ctx, q)
3736
messages := message.NewService(ctx, q)
3837

@@ -41,7 +40,6 @@ func New(ctx context.Context, conn *sql.DB) *App {
4140
Sessions: sessions,
4241
Messages: messages,
4342
Permissions: permission.NewPermissionService(),
44-
Logger: log,
4543
LSPClients: make(map[string]*lsp.Client),
4644
}
4745

@@ -52,13 +50,13 @@ func New(ctx context.Context, conn *sql.DB) *App {
5250
})
5351
workspaceWatcher := watcher.NewWorkspaceWatcher(lspClient)
5452
if err != nil {
55-
log.Error("Failed to create LSP client for", name, err)
53+
logging.Error("Failed to create LSP client for", name, err)
5654
continue
5755
}
5856

5957
_, err = lspClient.InitializeLSPClient(ctx, config.WorkingDirectory())
6058
if err != nil {
61-
log.Error("Initialize failed", "error", err)
59+
logging.Error("Initialize failed", "error", err)
6260
continue
6361
}
6462
go workspaceWatcher.WatchWorkspace(ctx, config.WorkingDirectory())
@@ -74,5 +72,5 @@ func (a *App) Close() {
7472
for _, client := range a.LSPClients {
7573
client.Close()
7674
}
77-
a.Logger.Info("App closed")
75+
slog.Info("App closed")
7876
}

internal/db/connect.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/kujtimiihoxha/termai/internal/logging"
1717
)
1818

19-
var log = logging.Get()
20-
2119
func Connect() (*sql.DB, error) {
2220
dataDir := config.Get().Data.Directory
2321
if dataDir == "" {
@@ -50,43 +48,43 @@ func Connect() (*sql.DB, error) {
5048

5149
for _, pragma := range pragmas {
5250
if _, err = db.Exec(pragma); err != nil {
53-
log.Warn("Failed to set pragma", pragma, err)
51+
logging.Warn("Failed to set pragma", pragma, err)
5452
} else {
55-
log.Warn("Set pragma", "pragma", pragma)
53+
logging.Warn("Set pragma", "pragma", pragma)
5654
}
5755
}
5856

5957
// Initialize schema from embedded file
6058
d, err := iofs.New(FS, "migrations")
6159
if err != nil {
62-
log.Error("Failed to open embedded migrations", "error", err)
60+
logging.Error("Failed to open embedded migrations", "error", err)
6361
db.Close()
6462
return nil, fmt.Errorf("failed to open embedded migrations: %w", err)
6563
}
6664

6765
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
6866
if err != nil {
69-
log.Error("Failed to create SQLite driver", "error", err)
67+
logging.Error("Failed to create SQLite driver", "error", err)
7068
db.Close()
7169
return nil, fmt.Errorf("failed to create SQLite driver: %w", err)
7270
}
7371

7472
m, err := migrate.NewWithInstance("iofs", d, "ql", driver)
7573
if err != nil {
76-
log.Error("Failed to create migration instance", "error", err)
74+
logging.Error("Failed to create migration instance", "error", err)
7775
db.Close()
7876
return nil, fmt.Errorf("failed to create migration instance: %w", err)
7977
}
8078

8179
err = m.Up()
8280
if err != nil && err != migrate.ErrNoChange {
83-
log.Error("Migration failed", "error", err)
81+
logging.Error("Migration failed", "error", err)
8482
db.Close()
8583
return nil, fmt.Errorf("failed to apply schema: %w", err)
8684
} else if err == migrate.ErrNoChange {
87-
log.Info("No schema changes to apply")
85+
logging.Info("No schema changes to apply")
8886
} else {
89-
log.Info("Schema migration applied successfully")
87+
logging.Info("Schema migration applied successfully")
9088
}
9189

9290
return db, nil

internal/llm/agent/agent-tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (b *agentTool) Run(ctx context.Context, call tools.ToolCall) (tools.ToolRes
5656
return tools.NewTextErrorResponse(fmt.Sprintf("error creating session: %s", err)), nil
5757
}
5858

59-
err = agent.Generate(session.ID, params.Prompt)
59+
err = agent.Generate(ctx, session.ID, params.Prompt)
6060
if err != nil {
6161
return tools.NewTextErrorResponse(fmt.Sprintf("error generating agent: %s", err)), nil
6262
}

0 commit comments

Comments
 (0)