|
| 1 | +package opencode |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// getOpenCodeDBPath returns the path to OpenCode's SQLite database. |
| 13 | +// OpenCode always uses ~/.local/share/opencode/opencode.db (XDG default) |
| 14 | +// regardless of platform — it does NOT use ~/Library/Application Support on macOS. |
| 15 | +// |
| 16 | +// XDG_DATA_HOME overrides the default on all platforms. |
| 17 | +func getOpenCodeDBPath() (string, error) { |
| 18 | + dataDir := os.Getenv("XDG_DATA_HOME") |
| 19 | + if dataDir == "" { |
| 20 | + home, err := os.UserHomeDir() |
| 21 | + if err != nil { |
| 22 | + return "", fmt.Errorf("failed to get home directory: %w", err) |
| 23 | + } |
| 24 | + dataDir = filepath.Join(home, ".local", "share") |
| 25 | + } |
| 26 | + return filepath.Join(dataDir, "opencode", "opencode.db"), nil |
| 27 | +} |
| 28 | + |
| 29 | +// runSQLiteQuery executes a SQL query against OpenCode's SQLite database. |
| 30 | +// Returns the combined stdout/stderr output. |
| 31 | +func runSQLiteQuery(query string, timeout time.Duration) ([]byte, error) { |
| 32 | + dbPath, err := getOpenCodeDBPath() |
| 33 | + if err != nil { |
| 34 | + return nil, fmt.Errorf("failed to get OpenCode DB path: %w", err) |
| 35 | + } |
| 36 | + if _, err := os.Stat(dbPath); os.IsNotExist(err) { |
| 37 | + return nil, fmt.Errorf("OpenCode database not found: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 41 | + defer cancel() |
| 42 | + |
| 43 | + //nolint:gosec // G204: query is constructed from sanitized inputs (escapeSQLiteString) |
| 44 | + cmd := exec.CommandContext(ctx, "sqlite3", dbPath, query) |
| 45 | + output, err := cmd.CombinedOutput() |
| 46 | + if err != nil { |
| 47 | + return output, fmt.Errorf("sqlite3 query failed: %w", err) |
| 48 | + } |
| 49 | + return output, nil |
| 50 | +} |
| 51 | + |
| 52 | +// deleteMessagesFromSQLite removes all messages (and cascading parts) for a session. |
| 53 | +// This is used before reimporting a session during rewind so that `opencode import` |
| 54 | +// can insert the checkpoint-state messages (import uses ON CONFLICT DO NOTHING). |
| 55 | +func deleteMessagesFromSQLite(sessionID string) error { |
| 56 | + // Enable foreign keys so CASCADE deletes work (parts are deleted with messages). |
| 57 | + query := fmt.Sprintf( |
| 58 | + "PRAGMA foreign_keys = ON; DELETE FROM message WHERE session_id = '%s';", |
| 59 | + escapeSQLiteString(sessionID), |
| 60 | + ) |
| 61 | + if output, err := runSQLiteQuery(query, 5*time.Second); err != nil { |
| 62 | + return fmt.Errorf("failed to delete messages from OpenCode DB: %w (output: %s)", err, string(output)) |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// escapeSQLiteString escapes single quotes in a string for safe use in SQLite queries. |
| 68 | +func escapeSQLiteString(s string) string { |
| 69 | + result := make([]byte, 0, len(s)) |
| 70 | + for i := range len(s) { |
| 71 | + if s[i] == '\'' { |
| 72 | + result = append(result, '\'', '\'') |
| 73 | + } else { |
| 74 | + result = append(result, s[i]) |
| 75 | + } |
| 76 | + } |
| 77 | + return string(result) |
| 78 | +} |
0 commit comments