Improve config lookup and OpenAI streaming decode
This commit is contained in:
29
README.md
29
README.md
@@ -5,8 +5,10 @@ Goaichat is a terminal-based AI chat client written in Go. It connects to OpenAI
|
|||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Config-driven startup**: Loads defaults from `config.yaml` with environment overrides.
|
- **Config-driven startup**: Loads defaults from `config.yaml` with environment overrides.
|
||||||
- **Interactive chat loop**: (In progress) command-driven conversation with persistent history.
|
- **Interactive chat loop**: Command-driven conversation with OpenAI-compatible models.
|
||||||
- **Extensible architecture**: Structured packages under `internal/` for config, app wiring, chat, storage, OpenAI client, UI, and telemetry.
|
- **Persistent history**: Sessions and messages are stored in SQLite, allowing `/open` and `/rename` commands.
|
||||||
|
- **Session naming assistance**: The assistant suggests descriptive session names that you can adopt.
|
||||||
|
- **Extensible architecture**: Structured packages under `internal/` for configuration, chat orchestration, storage, OpenAI client integration, and CLI wiring.
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
@@ -31,7 +33,7 @@ go build ./cmd/goaichat
|
|||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
Create `config.yaml` in the project root or pass `--config` to specify a path. A minimal configuration looks like:
|
Create `config.yaml` in the project root or pass `--config` to specify a path. When no path is provided, Goaichat searches the current working directory first and then `$HOME/.config/goaichat/config.yaml`. A minimal configuration looks like:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
api:
|
api:
|
||||||
@@ -58,7 +60,24 @@ Environment variables override several fields:
|
|||||||
go run ./cmd/goaichat --config path/to/config.yaml
|
go run ./cmd/goaichat --config path/to/config.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
The current build logs startup/shutdown messages while core services are implemented. Upcoming milestones include the chat loop, persistence, OpenAI integration, and Bubble Tea UI.
|
### Usage
|
||||||
|
|
||||||
|
After launching, type messages at the prompt. The following commands are available:
|
||||||
|
|
||||||
|
- `/help` – display available commands.
|
||||||
|
- `/reset` – clear the current in-memory conversation.
|
||||||
|
- `/list` – list saved sessions with summaries.
|
||||||
|
- `/open <name>` – load a previously saved session by name.
|
||||||
|
- `/rename <name>` – assign a new name to the current session (use suggestions or provide your own).
|
||||||
|
- `/exit` – quit the application.
|
||||||
|
|
||||||
|
The assistant proposes session name suggestions after the first reply in a session. Use `/rename <suggested-name>` to accept them quickly.
|
||||||
|
|
||||||
|
### Persistence
|
||||||
|
|
||||||
|
Goaichat uses SQLite for storage. On first run, the application applies migrations from `internal/storage/migrations/`. Sessions are automatically created when you begin chatting, and every message is stored for later retrieval.
|
||||||
|
|
||||||
|
By default the database is stored at `$HOME/.local/share/goaichat/goaichat.db`. You can override the location via the `storage.path` or `storage.base_dir` settings in `config.yaml`.
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
@@ -67,4 +86,4 @@ The current build logs startup/shutdown messages while core services are impleme
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
TBD
|
This project is licensed under the [MIT License](LICENSE).
|
||||||
|
23
dev-plan.md
23
dev-plan.md
@@ -8,7 +8,7 @@
|
|||||||
## Functional Requirements
|
## Functional Requirements
|
||||||
- **Interactive chat loop** Allow continuous message exchange with the AI until the user exits.
|
- **Interactive chat loop** Allow continuous message exchange with the AI until the user exits.
|
||||||
- **Config-driven startup** Parse `config.yaml` for `api.url`, `api.key`, model defaults, and future options (e.g., system prompts, temperature, streaming flag).
|
- **Config-driven startup** Parse `config.yaml` for `api.url`, `api.key`, model defaults, and future options (e.g., system prompts, temperature, streaming flag).
|
||||||
- **Persistent history** Store chats in a SQLite database located at `/var/log/goaichat/<username>/goaichat.db`, capturing session metadata and full message transcripts.
|
- **Persistent history** Store chats in a SQLite database located at `$HOME/.local/share/goaichat/goaichat.db` (configurable), capturing session metadata and full message transcripts.
|
||||||
- **History commands** Provide `/list` to enumerate saved chats and allow selecting or reopening them within the session.
|
- **History commands** Provide `/list` to enumerate saved chats and allow selecting or reopening them within the session.
|
||||||
- **Model management** Support `/models` command to display configured/available models, persist the user’s choice, and use it for subsequent requests.
|
- **Model management** Support `/models` command to display configured/available models, persist the user’s choice, and use it for subsequent requests.
|
||||||
- **Graceful exit controls** Support commands like `/exit`, `/reset`, `/help`.
|
- **Graceful exit controls** Support commands like `/exit`, `/reset`, `/help`.
|
||||||
@@ -44,20 +44,17 @@ dflow LR
|
|||||||
ChatCore --> UI
|
ChatCore --> UI
|
||||||
Config[(config.yaml + env)] --> ConfigPkg[Configuration]
|
Config[(config.yaml + env)] --> ConfigPkg[Configuration]
|
||||||
ConfigPkg --> AppCore[Application core]
|
ConfigPkg --> AppCore[Application core]
|
||||||
AppCore --> UI
|
|
||||||
AppCore --> ChatCore
|
AppCore --> ChatCore
|
||||||
AppCore --> OpenAIClient
|
AppCore --> OpenAIClient
|
||||||
AppCore --> Storage
|
AppCore --> Storage
|
||||||
```
|
##- **Persistence & Data Storage (`internal/storage`)
|
||||||
|
- **Data directory** Resolve user-specific path `$HOME/.local/share/goaichat/` (configurable override) and ensure directories exist with secure permissions.
|
||||||
## Persistence & Data Storage (`internal/storage`)
|
- **Database** SQLite file `goaichat.db` managed via `modernc.org/sqlite` (pure Go) for portability; allow build tag to switch to `mattn/go-sqlite3` if desired.
|
||||||
- **Data directory** Resolve user-specific path `/var/log/goaichat/<username>/` (configurable override) and ensure directories exist with secure permissions.
|
- **Schema**
|
||||||
- **Database** SQLite file `goaichat.db` managed via `modernc.org/sqlite` (pure Go) for portability; allow build tag to switch to `mattn/go-sqlite3` if desired.
|
- **`sessions`** (`id`, `name`, `created_at`, `updated_at`, `model_name`, `summary`).
|
||||||
- **Schema**
|
- **`messages`** (`id`, `session_id`, `role`, `content`, `token_count`, `created_at`).
|
||||||
- **`sessions`** (`id`, `name`, `created_at`, `updated_at`, `model_name`, `summary`).
|
- **`models`** (`id`, `name`, `display_name`, `provider`, `is_default`, `last_used_at`).
|
||||||
- **`messages`** (`id`, `session_id`, `role`, `content`, `token_count`, `created_at`).
|
{{ ... }}
|
||||||
- **`models`** (`id`, `name`, `display_name`, `provider`, `is_default`, `last_used_at`).
|
|
||||||
- **Migrations** Ship SQL migration files applied on startup; version using goose/atlas or simple in-app migration runner.
|
|
||||||
- **Data access** Provide repository interfaces for chat service to create sessions, append messages, list chats, update selected model, and fetch transcripts.
|
- **Data access** Provide repository interfaces for chat service to create sessions, append messages, list chats, update selected model, and fetch transcripts.
|
||||||
- **Performance** Enable write-ahead logging, tune connection settings, and guard concurrent access with a request queue or mutex.
|
- **Performance** Enable write-ahead logging, tune connection settings, and guard concurrent access with a request queue or mutex.
|
||||||
|
|
||||||
@@ -127,7 +124,7 @@ logging:
|
|||||||
- **Milestone 4: Persistence foundation**
|
- **Milestone 4: Persistence foundation**
|
||||||
- **Design** SQLite schema and migrations within `internal/storage`.
|
- **Design** SQLite schema and migrations within `internal/storage`.
|
||||||
- **Implement** repositories for sessions, messages, and models.
|
- **Implement** repositories for sessions, messages, and models.
|
||||||
- **Ensure** path resolution to `/var/log/goaichat/<username>/` with permission checks and tests.
|
- **Ensure** path resolution to `$HOME/.local/share/goaichat/` with permission checks and tests.
|
||||||
|
|
||||||
- **Milestone 5: Chat loop MVP**
|
- **Milestone 5: Chat loop MVP**
|
||||||
- **Implement** CLI loop that reads user input, calls chat client, prints responses.
|
- **Implement** CLI loop that reads user input, calls chat client, prints responses.
|
||||||
|
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -63,8 +64,19 @@ func Load(path string) (*Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := loadFile("config.yaml", &cfg); err != nil && !errors.Is(err, os.ErrNotExist) {
|
candidates := []string{"config.yaml"}
|
||||||
return nil, err
|
if home, err := os.UserHomeDir(); err == nil && strings.TrimSpace(home) != "" {
|
||||||
|
candidates = append(candidates, filepath.Join(home, ".config", "goaichat", "config.yaml"))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, candidate := range candidates {
|
||||||
|
if err := loadFile(candidate, &cfg); err != nil {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package openai
|
package openai
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -86,6 +87,9 @@ func (c *Client) CreateChatCompletion(ctx context.Context, req ChatCompletionReq
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||||
|
if req.Stream {
|
||||||
|
return decodeStream(resp.Body)
|
||||||
|
}
|
||||||
return decodeSuccess(resp.Body)
|
return decodeSuccess(resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,13 +97,121 @@ func (c *Client) CreateChatCompletion(ctx context.Context, req ChatCompletionReq
|
|||||||
}
|
}
|
||||||
|
|
||||||
func decodeSuccess(r io.Reader) (*ChatCompletionResponse, error) {
|
func decodeSuccess(r io.Reader) (*ChatCompletionResponse, error) {
|
||||||
var response ChatCompletionResponse
|
data, err := io.ReadAll(r)
|
||||||
if err := json.NewDecoder(r).Decode(&response); err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("decode response: %w", err)
|
return nil, fmt.Errorf("read response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var response ChatCompletionResponse
|
||||||
|
if err := json.Unmarshal(data, &response); err != nil {
|
||||||
|
trimmed := bytes.TrimSpace(data)
|
||||||
|
if len(trimmed) == 0 {
|
||||||
|
return nil, fmt.Errorf("decode response: %w", err)
|
||||||
|
}
|
||||||
|
return &ChatCompletionResponse{
|
||||||
|
Choices: []ChatCompletionChoice{{
|
||||||
|
Message: ChatMessage{Role: "assistant", Content: string(trimmed)},
|
||||||
|
}},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
return &response, nil
|
return &response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func decodeStream(r io.Reader) (*ChatCompletionResponse, error) {
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
var payloads []json.RawMessage
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.HasPrefix(line, "data: ") {
|
||||||
|
payload := strings.TrimPrefix(line, "data: ")
|
||||||
|
if payload == "[DONE]" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
payloads = append(payloads, json.RawMessage(payload))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("read stream: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(payloads) == 0 {
|
||||||
|
return nil, errors.New("empty stream response")
|
||||||
|
}
|
||||||
|
|
||||||
|
type streamChunk struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Object string `json:"object"`
|
||||||
|
Choices []struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
Message ChatMessage `json:"message"`
|
||||||
|
Delta ChatMessage `json:"delta"`
|
||||||
|
FinishReason string `json:"finish_reason"`
|
||||||
|
} `json:"choices"`
|
||||||
|
Usage Usage `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var aggregated ChatCompletionResponse
|
||||||
|
var builder strings.Builder
|
||||||
|
role := "assistant"
|
||||||
|
finish := ""
|
||||||
|
|
||||||
|
for _, payload := range payloads {
|
||||||
|
var chunk streamChunk
|
||||||
|
if err := json.Unmarshal(payload, &chunk); err != nil {
|
||||||
|
return nil, fmt.Errorf("decode stream response: %w", err)
|
||||||
|
}
|
||||||
|
if aggregated.ID == "" {
|
||||||
|
aggregated.ID = chunk.ID
|
||||||
|
}
|
||||||
|
if aggregated.Object == "" {
|
||||||
|
aggregated.Object = chunk.Object
|
||||||
|
}
|
||||||
|
aggregated.Usage.PromptTokens += chunk.Usage.PromptTokens
|
||||||
|
aggregated.Usage.CompletionTokens += chunk.Usage.CompletionTokens
|
||||||
|
aggregated.Usage.TotalTokens += chunk.Usage.TotalTokens
|
||||||
|
|
||||||
|
if len(chunk.Choices) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
choice := chunk.Choices[0]
|
||||||
|
if choice.Message.Role != "" {
|
||||||
|
role = choice.Message.Role
|
||||||
|
}
|
||||||
|
if choice.Delta.Role != "" {
|
||||||
|
role = choice.Delta.Role
|
||||||
|
}
|
||||||
|
if choice.Message.Content != "" {
|
||||||
|
builder.WriteString(choice.Message.Content)
|
||||||
|
}
|
||||||
|
if choice.Delta.Content != "" {
|
||||||
|
builder.WriteString(choice.Delta.Content)
|
||||||
|
}
|
||||||
|
if choice.FinishReason != "" {
|
||||||
|
finish = choice.FinishReason
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
content := strings.TrimSpace(builder.String())
|
||||||
|
if content == "" {
|
||||||
|
return nil, errors.New("stream response contained no content")
|
||||||
|
}
|
||||||
|
|
||||||
|
aggregated.Choices = []ChatCompletionChoice{{
|
||||||
|
Index: 0,
|
||||||
|
FinishReason: finish,
|
||||||
|
Message: ChatMessage{
|
||||||
|
Role: role,
|
||||||
|
Content: content,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
|
return &aggregated, nil
|
||||||
|
}
|
||||||
|
|
||||||
func decodeError(r io.Reader, status int) error {
|
func decodeError(r io.Reader, status int) error {
|
||||||
var apiErr ErrorResponse
|
var apiErr ErrorResponse
|
||||||
if err := json.NewDecoder(r).Decode(&apiErr); err != nil {
|
if err := json.NewDecoder(r).Decode(&apiErr); err != nil {
|
||||||
|
@@ -87,20 +87,26 @@ func resolvePath(cfg config.StorageConfig) (string, error) {
|
|||||||
return cfg.Path, nil
|
return cfg.Path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
username := cfg.Username
|
baseDir := strings.TrimSpace(cfg.BaseDir)
|
||||||
if username == "" {
|
if baseDir == "" {
|
||||||
username = os.Getenv("USER")
|
home, err := os.UserHomeDir()
|
||||||
if username == "" {
|
if err == nil && strings.TrimSpace(home) != "" {
|
||||||
username = "default"
|
baseDir = filepath.Join(home, ".local", "share", "goaichat")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
base := filepath.Join("/var/log/goaichat", username)
|
if baseDir == "" {
|
||||||
if cfg.BaseDir != "" {
|
username := strings.TrimSpace(cfg.Username)
|
||||||
base = cfg.BaseDir
|
if username == "" {
|
||||||
|
username = strings.TrimSpace(os.Getenv("USER"))
|
||||||
|
if username == "" {
|
||||||
|
username = "default"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
baseDir = filepath.Join(os.TempDir(), "goaichat", username)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(base, "goaichat.db"), nil
|
return filepath.Join(baseDir, "goaichat.db"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureDir(dir string) error {
|
func ensureDir(dir string) error {
|
||||||
|
Reference in New Issue
Block a user