mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-24 11:14:23 +01:00
initial agent setup
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/kujtimiihoxha/termai/internal/db"
|
||||
"github.com/kujtimiihoxha/termai/internal/llm"
|
||||
"github.com/kujtimiihoxha/termai/internal/logging"
|
||||
"github.com/kujtimiihoxha/termai/internal/message"
|
||||
"github.com/kujtimiihoxha/termai/internal/session"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -14,6 +16,8 @@ type App struct {
|
||||
Context context.Context
|
||||
|
||||
Sessions session.Service
|
||||
Messages message.Service
|
||||
LLM llm.Service
|
||||
|
||||
Logger logging.Interface
|
||||
}
|
||||
@@ -23,9 +27,15 @@ func New(ctx context.Context, conn *sql.DB) *App {
|
||||
log := logging.NewLogger(logging.Options{
|
||||
Level: viper.GetString("log.level"),
|
||||
})
|
||||
sessions := session.NewService(ctx, q)
|
||||
messages := message.NewService(ctx, q)
|
||||
llm := llm.NewService(ctx, log, sessions, messages)
|
||||
|
||||
return &App{
|
||||
Context: ctx,
|
||||
Sessions: session.NewService(ctx, q),
|
||||
Sessions: sessions,
|
||||
Messages: messages,
|
||||
LLM: llm,
|
||||
Logger: log,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,15 +24,30 @@ func New(db DBTX) *Queries {
|
||||
func Prepare(ctx context.Context, db DBTX) (*Queries, error) {
|
||||
q := Queries{db: db}
|
||||
var err error
|
||||
if q.createMessageStmt, err = db.PrepareContext(ctx, createMessage); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query CreateMessage: %w", err)
|
||||
}
|
||||
if q.createSessionStmt, err = db.PrepareContext(ctx, createSession); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query CreateSession: %w", err)
|
||||
}
|
||||
if q.deleteMessageStmt, err = db.PrepareContext(ctx, deleteMessage); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query DeleteMessage: %w", err)
|
||||
}
|
||||
if q.deleteSessionStmt, err = db.PrepareContext(ctx, deleteSession); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query DeleteSession: %w", err)
|
||||
}
|
||||
if q.deleteSessionMessagesStmt, err = db.PrepareContext(ctx, deleteSessionMessages); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query DeleteSessionMessages: %w", err)
|
||||
}
|
||||
if q.getMessageStmt, err = db.PrepareContext(ctx, getMessage); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query GetMessage: %w", err)
|
||||
}
|
||||
if q.getSessionByIDStmt, err = db.PrepareContext(ctx, getSessionByID); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query GetSessionByID: %w", err)
|
||||
}
|
||||
if q.listMessagesBySessionStmt, err = db.PrepareContext(ctx, listMessagesBySession); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query ListMessagesBySession: %w", err)
|
||||
}
|
||||
if q.listSessionsStmt, err = db.PrepareContext(ctx, listSessions); err != nil {
|
||||
return nil, fmt.Errorf("error preparing query ListSessions: %w", err)
|
||||
}
|
||||
@@ -44,21 +59,46 @@ func Prepare(ctx context.Context, db DBTX) (*Queries, error) {
|
||||
|
||||
func (q *Queries) Close() error {
|
||||
var err error
|
||||
if q.createMessageStmt != nil {
|
||||
if cerr := q.createMessageStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing createMessageStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.createSessionStmt != nil {
|
||||
if cerr := q.createSessionStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing createSessionStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.deleteMessageStmt != nil {
|
||||
if cerr := q.deleteMessageStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing deleteMessageStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.deleteSessionStmt != nil {
|
||||
if cerr := q.deleteSessionStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing deleteSessionStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.deleteSessionMessagesStmt != nil {
|
||||
if cerr := q.deleteSessionMessagesStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing deleteSessionMessagesStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.getMessageStmt != nil {
|
||||
if cerr := q.getMessageStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing getMessageStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.getSessionByIDStmt != nil {
|
||||
if cerr := q.getSessionByIDStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing getSessionByIDStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.listMessagesBySessionStmt != nil {
|
||||
if cerr := q.listMessagesBySessionStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing listMessagesBySessionStmt: %w", cerr)
|
||||
}
|
||||
}
|
||||
if q.listSessionsStmt != nil {
|
||||
if cerr := q.listSessionsStmt.Close(); cerr != nil {
|
||||
err = fmt.Errorf("error closing listSessionsStmt: %w", cerr)
|
||||
@@ -106,23 +146,33 @@ func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, ar
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
tx *sql.Tx
|
||||
createSessionStmt *sql.Stmt
|
||||
deleteSessionStmt *sql.Stmt
|
||||
getSessionByIDStmt *sql.Stmt
|
||||
listSessionsStmt *sql.Stmt
|
||||
updateSessionStmt *sql.Stmt
|
||||
db DBTX
|
||||
tx *sql.Tx
|
||||
createMessageStmt *sql.Stmt
|
||||
createSessionStmt *sql.Stmt
|
||||
deleteMessageStmt *sql.Stmt
|
||||
deleteSessionStmt *sql.Stmt
|
||||
deleteSessionMessagesStmt *sql.Stmt
|
||||
getMessageStmt *sql.Stmt
|
||||
getSessionByIDStmt *sql.Stmt
|
||||
listMessagesBySessionStmt *sql.Stmt
|
||||
listSessionsStmt *sql.Stmt
|
||||
updateSessionStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
tx: tx,
|
||||
createSessionStmt: q.createSessionStmt,
|
||||
deleteSessionStmt: q.deleteSessionStmt,
|
||||
getSessionByIDStmt: q.getSessionByIDStmt,
|
||||
listSessionsStmt: q.listSessionsStmt,
|
||||
updateSessionStmt: q.updateSessionStmt,
|
||||
db: tx,
|
||||
tx: tx,
|
||||
createMessageStmt: q.createMessageStmt,
|
||||
createSessionStmt: q.createSessionStmt,
|
||||
deleteMessageStmt: q.deleteMessageStmt,
|
||||
deleteSessionStmt: q.deleteSessionStmt,
|
||||
deleteSessionMessagesStmt: q.deleteSessionMessagesStmt,
|
||||
getMessageStmt: q.getMessageStmt,
|
||||
getSessionByIDStmt: q.getSessionByIDStmt,
|
||||
listMessagesBySessionStmt: q.listMessagesBySessionStmt,
|
||||
listSessionsStmt: q.listSessionsStmt,
|
||||
updateSessionStmt: q.updateSessionStmt,
|
||||
}
|
||||
}
|
||||
|
||||
117
internal/db/messages.sql.go
Normal file
117
internal/db/messages.sql.go
Normal file
@@ -0,0 +1,117 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// source: messages.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const createMessage = `-- name: CreateMessage :one
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
session_id,
|
||||
message_data,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
?, ?, ?, strftime('%s', 'now'), strftime('%s', 'now')
|
||||
)
|
||||
RETURNING id, session_id, message_data, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateMessageParams struct {
|
||||
ID string `json:"id"`
|
||||
SessionID string `json:"session_id"`
|
||||
MessageData string `json:"message_data"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
|
||||
row := q.queryRow(ctx, q.createMessageStmt, createMessage, arg.ID, arg.SessionID, arg.MessageData)
|
||||
var i Message
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.MessageData,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteMessage = `-- name: DeleteMessage :exec
|
||||
DELETE FROM messages
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteMessage(ctx context.Context, id string) error {
|
||||
_, err := q.exec(ctx, q.deleteMessageStmt, deleteMessage, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSessionMessages = `-- name: DeleteSessionMessages :exec
|
||||
DELETE FROM messages
|
||||
WHERE session_id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSessionMessages(ctx context.Context, sessionID string) error {
|
||||
_, err := q.exec(ctx, q.deleteSessionMessagesStmt, deleteSessionMessages, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
const getMessage = `-- name: GetMessage :one
|
||||
SELECT id, session_id, message_data, created_at, updated_at
|
||||
FROM messages
|
||||
WHERE id = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetMessage(ctx context.Context, id string) (Message, error) {
|
||||
row := q.queryRow(ctx, q.getMessageStmt, getMessage, id)
|
||||
var i Message
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.MessageData,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listMessagesBySession = `-- name: ListMessagesBySession :many
|
||||
SELECT id, session_id, message_data, created_at, updated_at
|
||||
FROM messages
|
||||
WHERE session_id = ?
|
||||
ORDER BY created_at ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListMessagesBySession(ctx context.Context, sessionID string) ([]Message, error) {
|
||||
rows, err := q.query(ctx, q.listMessagesBySessionStmt, listMessagesBySession, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Message{}
|
||||
for rows.Next() {
|
||||
var i Message
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.SessionID,
|
||||
&i.MessageData,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
-- sqlfluff:dialect:sqlite
|
||||
DROP TRIGGER IF EXISTS update_sessions_updated_at;
|
||||
DROP TRIGGER IF EXISTS update_messages_updated_at;
|
||||
|
||||
DROP TRIGGER IF EXISTS update_session_message_count_on_delete;
|
||||
DROP TRIGGER IF EXISTS update_session_message_count_on_insert;
|
||||
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
-- sqlfluff:dialect:sqlite
|
||||
-- Sessions
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
message_count INTEGER NOT NULL DEFAULT 0 CHECK (message_count >= 0),
|
||||
tokens INTEGER NOT NULL DEFAULT 0 CHECK (tokens >= 0),
|
||||
prompt_tokens INTEGER NOT NULL DEFAULT 0 CHECK (prompt_tokens >= 0),
|
||||
completion_tokens INTEGER NOT NULL DEFAULT 0 CHECK (completion_tokens>= 0),
|
||||
cost REAL NOT NULL DEFAULT 0.0 CHECK (cost >= 0.0),
|
||||
updated_at INTEGER NOT NULL, -- Unix timestamp in milliseconds
|
||||
created_at INTEGER NOT NULL -- Unix timestamp in milliseconds
|
||||
@@ -15,3 +16,38 @@ BEGIN
|
||||
UPDATE sessions SET updated_at = strftime('%s', 'now')
|
||||
WHERE id = new.id;
|
||||
END;
|
||||
|
||||
-- Messages
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id TEXT PRIMARY KEY,
|
||||
session_id TEXT NOT NULL,
|
||||
message_data TEXT NOT NULL, -- JSON string of message content
|
||||
created_at INTEGER NOT NULL, -- Unix timestamp in milliseconds
|
||||
updated_at INTEGER NOT NULL, -- Unix timestamp in milliseconds
|
||||
FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_session_id ON messages (session_id);
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS update_messages_updated_at
|
||||
AFTER UPDATE ON messages
|
||||
BEGIN
|
||||
UPDATE messages SET updated_at = strftime('%s', 'now')
|
||||
WHERE id = new.id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS update_session_message_count_on_insert
|
||||
AFTER INSERT ON messages
|
||||
BEGIN
|
||||
UPDATE sessions SET
|
||||
message_count = message_count + 1
|
||||
WHERE id = new.session_id;
|
||||
END;
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS update_session_message_count_on_delete
|
||||
AFTER DELETE ON messages
|
||||
BEGIN
|
||||
UPDATE sessions SET
|
||||
message_count = message_count - 1
|
||||
WHERE id = old.session_id;
|
||||
END;
|
||||
|
||||
@@ -4,12 +4,21 @@
|
||||
|
||||
package db
|
||||
|
||||
type Session struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MessageCount int64 `json:"message_count"`
|
||||
Tokens int64 `json:"tokens"`
|
||||
Cost float64 `json:"cost"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
SessionID string `json:"session_id"`
|
||||
MessageData string `json:"message_data"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MessageCount int64 `json:"message_count"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
Cost float64 `json:"cost"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -9,10 +9,14 @@ import (
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
// sqlfluff:dialect:sqlite
|
||||
CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error)
|
||||
CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)
|
||||
DeleteMessage(ctx context.Context, id string) error
|
||||
DeleteSession(ctx context.Context, id string) error
|
||||
DeleteSessionMessages(ctx context.Context, sessionID string) error
|
||||
GetMessage(ctx context.Context, id string) (Message, error)
|
||||
GetSessionByID(ctx context.Context, id string) (Session, error)
|
||||
ListMessagesBySession(ctx context.Context, sessionID string) ([]Message, error)
|
||||
ListSessions(ctx context.Context) ([]Session, error)
|
||||
UpdateSession(ctx context.Context, arg UpdateSessionParams) (Session, error)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ INSERT INTO sessions (
|
||||
id,
|
||||
title,
|
||||
message_count,
|
||||
tokens,
|
||||
prompt_tokens,
|
||||
completion_tokens,
|
||||
cost,
|
||||
updated_at,
|
||||
created_at
|
||||
@@ -24,26 +25,28 @@ INSERT INTO sessions (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
strftime('%s', 'now'),
|
||||
strftime('%s', 'now')
|
||||
) RETURNING id, title, message_count, tokens, cost, updated_at, created_at
|
||||
) RETURNING id, title, message_count, prompt_tokens, completion_tokens, cost, updated_at, created_at
|
||||
`
|
||||
|
||||
type CreateSessionParams struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MessageCount int64 `json:"message_count"`
|
||||
Tokens int64 `json:"tokens"`
|
||||
Cost float64 `json:"cost"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MessageCount int64 `json:"message_count"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
Cost float64 `json:"cost"`
|
||||
}
|
||||
|
||||
// sqlfluff:dialect:sqlite
|
||||
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error) {
|
||||
row := q.queryRow(ctx, q.createSessionStmt, createSession,
|
||||
arg.ID,
|
||||
arg.Title,
|
||||
arg.MessageCount,
|
||||
arg.Tokens,
|
||||
arg.PromptTokens,
|
||||
arg.CompletionTokens,
|
||||
arg.Cost,
|
||||
)
|
||||
var i Session
|
||||
@@ -51,7 +54,8 @@ func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (S
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.MessageCount,
|
||||
&i.Tokens,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.Cost,
|
||||
&i.UpdatedAt,
|
||||
&i.CreatedAt,
|
||||
@@ -70,7 +74,7 @@ func (q *Queries) DeleteSession(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
const getSessionByID = `-- name: GetSessionByID :one
|
||||
SELECT id, title, message_count, tokens, cost, updated_at, created_at
|
||||
SELECT id, title, message_count, prompt_tokens, completion_tokens, cost, updated_at, created_at
|
||||
FROM sessions
|
||||
WHERE id = ? LIMIT 1
|
||||
`
|
||||
@@ -82,7 +86,8 @@ func (q *Queries) GetSessionByID(ctx context.Context, id string) (Session, error
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.MessageCount,
|
||||
&i.Tokens,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.Cost,
|
||||
&i.UpdatedAt,
|
||||
&i.CreatedAt,
|
||||
@@ -91,7 +96,7 @@ func (q *Queries) GetSessionByID(ctx context.Context, id string) (Session, error
|
||||
}
|
||||
|
||||
const listSessions = `-- name: ListSessions :many
|
||||
SELECT id, title, message_count, tokens, cost, updated_at, created_at
|
||||
SELECT id, title, message_count, prompt_tokens, completion_tokens, cost, updated_at, created_at
|
||||
FROM sessions
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
@@ -109,7 +114,8 @@ func (q *Queries) ListSessions(ctx context.Context) ([]Session, error) {
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.MessageCount,
|
||||
&i.Tokens,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.Cost,
|
||||
&i.UpdatedAt,
|
||||
&i.CreatedAt,
|
||||
@@ -131,23 +137,26 @@ const updateSession = `-- name: UpdateSession :one
|
||||
UPDATE sessions
|
||||
SET
|
||||
title = ?,
|
||||
tokens = ?,
|
||||
prompt_tokens = ?,
|
||||
completion_tokens = ?,
|
||||
cost = ?
|
||||
WHERE id = ?
|
||||
RETURNING id, title, message_count, tokens, cost, updated_at, created_at
|
||||
RETURNING id, title, message_count, prompt_tokens, completion_tokens, cost, updated_at, created_at
|
||||
`
|
||||
|
||||
type UpdateSessionParams struct {
|
||||
Title string `json:"title"`
|
||||
Tokens int64 `json:"tokens"`
|
||||
Cost float64 `json:"cost"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
PromptTokens int64 `json:"prompt_tokens"`
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
Cost float64 `json:"cost"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSession(ctx context.Context, arg UpdateSessionParams) (Session, error) {
|
||||
row := q.queryRow(ctx, q.updateSessionStmt, updateSession,
|
||||
arg.Title,
|
||||
arg.Tokens,
|
||||
arg.PromptTokens,
|
||||
arg.CompletionTokens,
|
||||
arg.Cost,
|
||||
arg.ID,
|
||||
)
|
||||
@@ -156,7 +165,8 @@ func (q *Queries) UpdateSession(ctx context.Context, arg UpdateSessionParams) (S
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.MessageCount,
|
||||
&i.Tokens,
|
||||
&i.PromptTokens,
|
||||
&i.CompletionTokens,
|
||||
&i.Cost,
|
||||
&i.UpdatedAt,
|
||||
&i.CreatedAt,
|
||||
|
||||
30
internal/db/sql/messages.sql
Normal file
30
internal/db/sql/messages.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
-- name: GetMessage :one
|
||||
SELECT *
|
||||
FROM messages
|
||||
WHERE id = ? LIMIT 1;
|
||||
|
||||
-- name: ListMessagesBySession :many
|
||||
SELECT *
|
||||
FROM messages
|
||||
WHERE session_id = ?
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: CreateMessage :one
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
session_id,
|
||||
message_data,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
?, ?, ?, strftime('%s', 'now'), strftime('%s', 'now')
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteMessage :exec
|
||||
DELETE FROM messages
|
||||
WHERE id = ?;
|
||||
|
||||
-- name: DeleteSessionMessages :exec
|
||||
DELETE FROM messages
|
||||
WHERE session_id = ?;
|
||||
@@ -1,10 +1,10 @@
|
||||
-- sqlfluff:dialect:sqlite
|
||||
-- name: CreateSession :one
|
||||
INSERT INTO sessions (
|
||||
id,
|
||||
title,
|
||||
message_count,
|
||||
tokens,
|
||||
prompt_tokens,
|
||||
completion_tokens,
|
||||
cost,
|
||||
updated_at,
|
||||
created_at
|
||||
@@ -14,6 +14,7 @@ INSERT INTO sessions (
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
strftime('%s', 'now'),
|
||||
strftime('%s', 'now')
|
||||
) RETURNING *;
|
||||
@@ -32,7 +33,8 @@ ORDER BY created_at DESC;
|
||||
UPDATE sessions
|
||||
SET
|
||||
title = ?,
|
||||
tokens = ?,
|
||||
prompt_tokens = ?,
|
||||
completion_tokens = ?,
|
||||
cost = ?
|
||||
WHERE id = ?
|
||||
RETURNING *;
|
||||
|
||||
17
internal/llm/agent/agent.go
Normal file
17
internal/llm/agent/agent.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudwego/eino/flow/agent/react"
|
||||
)
|
||||
|
||||
func GetAgent(ctx context.Context, name string) (*react.Agent, string, error) {
|
||||
switch name {
|
||||
case "coder":
|
||||
agent, err := NewCoderAgent(ctx)
|
||||
return agent, CoderSystemPrompt(), err
|
||||
}
|
||||
return nil, "", fmt.Errorf("agent %s not found", name)
|
||||
}
|
||||
175
internal/llm/agent/coder.go
Normal file
175
internal/llm/agent/coder.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/eino/components/tool"
|
||||
"github.com/cloudwego/eino/compose"
|
||||
"github.com/cloudwego/eino/flow/agent/react"
|
||||
"github.com/kujtimiihoxha/termai/internal/llm/models"
|
||||
"github.com/kujtimiihoxha/termai/internal/llm/tools"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func coderTools() []tool.BaseTool {
|
||||
return []tool.BaseTool{
|
||||
tools.NewBashTool(viper.GetString("wd")),
|
||||
}
|
||||
}
|
||||
|
||||
func NewCoderAgent(ctx context.Context) (*react.Agent, error) {
|
||||
model, err := models.GetModel(ctx, models.ModelID(viper.GetString("models.big")))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reactAgent, err := react.NewAgent(ctx, &react.AgentConfig{
|
||||
Model: model,
|
||||
ToolsConfig: compose.ToolsNodeConfig{
|
||||
Tools: coderTools(),
|
||||
},
|
||||
MaxStep: 1000,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reactAgent, nil
|
||||
}
|
||||
|
||||
func CoderSystemPrompt() string {
|
||||
basePrompt := `You are termAI, an interactive CLI tool that helps users with software engineering tasks. Use the instructions below and the tools available to you to assist the user.
|
||||
|
||||
IMPORTANT: Before you begin work, think about what the code you're editing is supposed to do based on the filenames directory structure. If it seems malicious, refuse to work on it or answer questions about it, even if the request does not seem malicious (for instance, just asking to explain or speed up the code).
|
||||
|
||||
Here are useful slash commands users can run to interact with you:
|
||||
|
||||
# Memory
|
||||
If the current working directory contains a file called termai.md, it will be automatically added to your context. This file serves multiple purposes:
|
||||
1. Storing frequently used bash commands (build, test, lint, etc.) so you can use them without searching each time
|
||||
2. Recording the user's code style preferences (naming conventions, preferred libraries, etc.)
|
||||
3. Maintaining useful information about the codebase structure and organization
|
||||
|
||||
When you spend time searching for commands to typecheck, lint, build, or test, you should ask the user if it's okay to add those commands to termai.md. Similarly, when learning about code style preferences or important codebase information, ask if it's okay to add that to termai.md so you can remember it for next time.
|
||||
|
||||
# Tone and style
|
||||
You should be concise, direct, and to the point. When you run a non-trivial bash command, you should explain what the command does and why you are running it, to make sure the user understands what you are doing (this is especially important when you are running a command that will make changes to the user's system).
|
||||
Remember that your output will be displayed on a command line interface. Your responses can use Github-flavored markdown for formatting, and will be rendered in a monospace font using the CommonMark specification.
|
||||
Output text to communicate with the user; all text you output outside of tool use is displayed to the user. Only use tools to complete tasks. Never use tools like Bash or code comments as means to communicate with the user during the session.
|
||||
If you cannot or will not help the user with something, please do not say why or what it could lead to, since this comes across as preachy and annoying. Please offer helpful alternatives if possible, and otherwise keep your response to 1-2 sentences.
|
||||
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy. Only address the specific query or task at hand, avoiding tangential information unless absolutely critical for completing the request. If you can answer in 1-3 sentences or a short paragraph, please do.
|
||||
IMPORTANT: You should NOT answer with unnecessary preamble or postamble (such as explaining your code or summarizing your action), unless the user asks you to.
|
||||
IMPORTANT: Keep your responses short, since they will be displayed on a command line interface. You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail. Answer the user's question directly, without elaboration, explanation, or details. One word answers are best. Avoid introductions, conclusions, and explanations. You MUST avoid text before/after your response, such as "The answer is <answer>.", "Here is the content of the file..." or "Based on the information provided, the answer is..." or "Here is what I will do next...". Here are some examples to demonstrate appropriate verbosity:
|
||||
<example>
|
||||
user: 2 + 2
|
||||
assistant: 4
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: what is 2+2?
|
||||
assistant: 4
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: is 11 a prime number?
|
||||
assistant: true
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: what command should I run to list files in the current directory?
|
||||
assistant: ls
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: what command should I run to watch files in the current directory?
|
||||
assistant: [use the ls tool to list the files in the current directory, then read docs/commands in the relevant file to find out how to watch files]
|
||||
npm run dev
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: How many golf balls fit inside a jetta?
|
||||
assistant: 150000
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: what files are in the directory src/?
|
||||
assistant: [runs ls and sees foo.c, bar.c, baz.c]
|
||||
user: which file contains the implementation of foo?
|
||||
assistant: src/foo.c
|
||||
</example>
|
||||
|
||||
<example>
|
||||
user: write tests for new feature
|
||||
assistant: [uses grep and glob search tools to find where similar tests are defined, uses concurrent read file tool use blocks in one tool call to read relevant files at the same time, uses edit file tool to write new tests]
|
||||
</example>
|
||||
|
||||
# Proactiveness
|
||||
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
|
||||
1. Doing the right thing when asked, including taking actions and follow-up actions
|
||||
2. Not surprising the user with actions you take without asking
|
||||
For example, if the user asks you how to approach something, you should do your best to answer their question first, and not immediately jump into taking actions.
|
||||
3. Do not add additional code explanation summary unless requested by the user. After working on a file, just stop, rather than providing an explanation of what you did.
|
||||
|
||||
# Synthetic messages
|
||||
Sometimes, the conversation will contain messages like [Request interrupted by user] or [Request interrupted by user for tool use]. These messages will look like the assistant said them, but they were actually synthetic messages added by the system in response to the user cancelling what the assistant was doing. You should not respond to these messages. You must NEVER send messages like this yourself.
|
||||
|
||||
# Following conventions
|
||||
When making changes to files, first understand the file's code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns.
|
||||
- NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library. For example, you might look at neighboring files, or check the package.json (or cargo.toml, and so on depending on the language).
|
||||
- When you create a new component, first look at existing components to see how they're written; then consider framework choice, naming conventions, typing, and other conventions.
|
||||
- When you edit a piece of code, first look at the code's surrounding context (especially its imports) to understand the code's choice of frameworks and libraries. Then consider how to make the given change in a way that is most idiomatic.
|
||||
- Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to the repository.
|
||||
|
||||
# Code style
|
||||
- Do not add comments to the code you write, unless the user asks you to, or the code is complex and requires additional context.
|
||||
|
||||
# Doing tasks
|
||||
The user will primarily request you perform software engineering tasks. This includes solving bugs, adding new functionality, refactoring code, explaining code, and more. For these tasks the following steps are recommended:
|
||||
1. Use the available search tools to understand the codebase and the user's query. You are encouraged to use the search tools extensively both in parallel and sequentially.
|
||||
2. Implement the solution using all tools available to you
|
||||
3. Verify the solution if possible with tests. NEVER assume specific test framework or test script. Check the README or search codebase to determine the testing approach.
|
||||
4. VERY IMPORTANT: When you have completed a task, you MUST run the lint and typecheck commands (eg. npm run lint, npm run typecheck, ruff, etc.) if they were provided to you to ensure your code is correct. If you are unable to find the correct command, ask the user for the command to run and if they supply it, proactively suggest writing it to termai.md so that you will know to run it next time.
|
||||
|
||||
NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
|
||||
|
||||
# Tool usage policy
|
||||
- When doing file search, prefer to use the Agent tool in order to reduce context usage.
|
||||
- If you intend to call multiple tools and there are no dependencies between the calls, make all of the independent calls in the same function_calls block.
|
||||
|
||||
You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail.`
|
||||
|
||||
envInfo := getEnvironmentInfo()
|
||||
|
||||
return fmt.Sprintf("%s\n\n%s", basePrompt, envInfo)
|
||||
}
|
||||
|
||||
func getEnvironmentInfo() string {
|
||||
cwd := viper.GetString("wd")
|
||||
isGit := isGitRepo(cwd)
|
||||
platform := runtime.GOOS
|
||||
date := time.Now().Format("1/2/2006")
|
||||
|
||||
return fmt.Sprintf(`Here is useful information about the environment you are running in:
|
||||
<env>
|
||||
Working directory: %s
|
||||
Is directory a git repo: %s
|
||||
Platform: %s
|
||||
Today's date: %s
|
||||
</env>`, cwd, boolToYesNo(isGit), platform, date)
|
||||
}
|
||||
|
||||
func isGitRepo(dir string) bool {
|
||||
_, err := os.Stat(filepath.Join(dir, ".git"))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func boolToYesNo(b bool) string {
|
||||
if b {
|
||||
return "Yes"
|
||||
}
|
||||
return "No"
|
||||
}
|
||||
206
internal/llm/llm.go
Normal file
206
internal/llm/llm.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/eino/callbacks"
|
||||
"github.com/cloudwego/eino/compose"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/google/uuid"
|
||||
"github.com/kujtimiihoxha/termai/internal/llm/agent"
|
||||
"github.com/kujtimiihoxha/termai/internal/logging"
|
||||
"github.com/kujtimiihoxha/termai/internal/message"
|
||||
"github.com/kujtimiihoxha/termai/internal/pubsub"
|
||||
"github.com/kujtimiihoxha/termai/internal/session"
|
||||
|
||||
eModel "github.com/cloudwego/eino/components/model"
|
||||
enioAgent "github.com/cloudwego/eino/flow/agent"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
AgentRequestoEvent pubsub.EventType = "agent_request"
|
||||
AgentErrorEvent pubsub.EventType = "agent_error"
|
||||
AgentResponseEvent pubsub.EventType = "agent_response"
|
||||
)
|
||||
|
||||
type AgentMessageType int
|
||||
|
||||
const (
|
||||
AgentMessageTypeNewUserMessage AgentMessageType = iota
|
||||
AgentMessageTypeAgentResponse
|
||||
AgentMessageTypeError
|
||||
)
|
||||
|
||||
type agentID string
|
||||
|
||||
const (
|
||||
RootAgent agentID = "root"
|
||||
TaskAgent agentID = "task"
|
||||
)
|
||||
|
||||
type AgentEvent struct {
|
||||
ID string `json:"id"`
|
||||
Type AgentMessageType `json:"type"`
|
||||
AgentID agentID `json:"agent_id"`
|
||||
MessageID string `json:"message_id"`
|
||||
SessionID string `json:"session_id"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
pubsub.Suscriber[AgentEvent]
|
||||
|
||||
SendRequest(sessionID string, content string)
|
||||
}
|
||||
type service struct {
|
||||
*pubsub.Broker[AgentEvent]
|
||||
Requests sync.Map
|
||||
ctx context.Context
|
||||
activeRequests sync.Map
|
||||
messages message.Service
|
||||
sessions session.Service
|
||||
logger logging.Interface
|
||||
}
|
||||
|
||||
func (s *service) handleRequest(id string, sessionID string, content string) {
|
||||
cancel, ok := s.activeRequests.Load(id)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
defer cancel.(context.CancelFunc)()
|
||||
defer s.activeRequests.Delete(id)
|
||||
|
||||
history, err := s.messages.List(sessionID)
|
||||
if err != nil {
|
||||
s.Publish(AgentErrorEvent, AgentEvent{
|
||||
ID: id,
|
||||
Type: AgentMessageTypeError,
|
||||
AgentID: RootAgent,
|
||||
MessageID: "",
|
||||
SessionID: sessionID,
|
||||
Content: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Request: %s", content)
|
||||
agent, systemMessage, err := agent.GetAgent(s.ctx, viper.GetString("agents.default"))
|
||||
if err != nil {
|
||||
s.Publish(AgentErrorEvent, AgentEvent{
|
||||
ID: id,
|
||||
Type: AgentMessageTypeError,
|
||||
AgentID: RootAgent,
|
||||
MessageID: "",
|
||||
SessionID: sessionID,
|
||||
Content: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
messages := []*schema.Message{
|
||||
{
|
||||
Role: schema.System,
|
||||
Content: systemMessage,
|
||||
},
|
||||
}
|
||||
for _, m := range history {
|
||||
messages = append(messages, &m.MessageData)
|
||||
}
|
||||
builder := callbacks.NewHandlerBuilder()
|
||||
builder.OnStartFn(func(ctx context.Context, info *callbacks.RunInfo, input callbacks.CallbackInput) context.Context {
|
||||
i, ok := input.(*eModel.CallbackInput)
|
||||
if info.Component == "ChatModel" && ok {
|
||||
if len(messages) < len(i.Messages) {
|
||||
// find new messages
|
||||
newMessages := i.Messages[len(messages):]
|
||||
for _, m := range newMessages {
|
||||
_, err = s.messages.Create(sessionID, *m)
|
||||
if err != nil {
|
||||
s.Publish(AgentErrorEvent, AgentEvent{
|
||||
ID: id,
|
||||
Type: AgentMessageTypeError,
|
||||
AgentID: RootAgent,
|
||||
MessageID: "",
|
||||
SessionID: sessionID,
|
||||
Content: err.Error(),
|
||||
})
|
||||
}
|
||||
messages = append(messages, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx
|
||||
})
|
||||
builder.OnEndFn(func(ctx context.Context, info *callbacks.RunInfo, output callbacks.CallbackOutput) context.Context {
|
||||
return ctx
|
||||
})
|
||||
|
||||
out, err := agent.Generate(s.ctx, messages, enioAgent.WithComposeOptions(compose.WithCallbacks(builder.Build())))
|
||||
if err != nil {
|
||||
s.Publish(AgentErrorEvent, AgentEvent{
|
||||
ID: id,
|
||||
Type: AgentMessageTypeError,
|
||||
AgentID: RootAgent,
|
||||
MessageID: "",
|
||||
SessionID: sessionID,
|
||||
Content: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
usage := out.ResponseMeta.Usage
|
||||
if usage != nil {
|
||||
log.Printf("Prompt Tokens: %d, Completion Tokens: %d, Total Tokens: %d", usage.PromptTokens, usage.CompletionTokens, usage.TotalTokens)
|
||||
session, err := s.sessions.Get(sessionID)
|
||||
if err != nil {
|
||||
s.Publish(AgentErrorEvent, AgentEvent{
|
||||
ID: id,
|
||||
Type: AgentMessageTypeError,
|
||||
AgentID: RootAgent,
|
||||
MessageID: "",
|
||||
SessionID: sessionID,
|
||||
Content: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
session.PromptTokens += int64(usage.PromptTokens)
|
||||
session.CompletionTokens += int64(usage.CompletionTokens)
|
||||
// TODO: calculate cost
|
||||
_, err = s.sessions.Save(session)
|
||||
if err != nil {
|
||||
s.Publish(AgentErrorEvent, AgentEvent{
|
||||
ID: id,
|
||||
Type: AgentMessageTypeError,
|
||||
AgentID: RootAgent,
|
||||
MessageID: "",
|
||||
SessionID: sessionID,
|
||||
Content: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
s.messages.Create(sessionID, *out)
|
||||
}
|
||||
|
||||
func (s *service) SendRequest(sessionID string, content string) {
|
||||
id := uuid.New().String()
|
||||
|
||||
_, cancel := context.WithTimeout(s.ctx, 5*time.Minute)
|
||||
s.activeRequests.Store(id, cancel)
|
||||
log.Printf("Request: %s", content)
|
||||
go s.handleRequest(id, sessionID, content)
|
||||
}
|
||||
|
||||
func NewService(ctx context.Context, logger logging.Interface, sessions session.Service, messages message.Service) Service {
|
||||
return &service{
|
||||
Broker: pubsub.NewBroker[AgentEvent](),
|
||||
ctx: ctx,
|
||||
sessions: sessions,
|
||||
messages: messages,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
196
internal/llm/models/models.go
Normal file
196
internal/llm/models/models.go
Normal file
@@ -0,0 +1,196 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/cloudwego/eino-ext/components/model/claude"
|
||||
"github.com/cloudwego/eino-ext/components/model/openai"
|
||||
"github.com/cloudwego/eino/components/model"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type (
|
||||
ModelID string
|
||||
ModelProvider string
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
ID ModelID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Provider ModelProvider `json:"provider"`
|
||||
APIModel string `json:"api_model"` // Actual value used when calling the API
|
||||
}
|
||||
|
||||
const (
|
||||
DefaultBigModel = GPT4oMini
|
||||
DefaultLittleModel = GPT4oMini
|
||||
)
|
||||
|
||||
// Model IDs
|
||||
const (
|
||||
// OpenAI
|
||||
GPT4o ModelID = "gpt-4o"
|
||||
GPT4oMini ModelID = "gpt-4o-mini"
|
||||
GPT45 ModelID = "gpt-4.5"
|
||||
O1 ModelID = "o1"
|
||||
O1Mini ModelID = "o1-mini"
|
||||
// Anthropic
|
||||
Claude35Sonnet ModelID = "claude-3.5-sonnet"
|
||||
Claude3Haiku ModelID = "claude-3-haiku"
|
||||
Claude37Sonnet ModelID = "claude-3.7-sonnet"
|
||||
// Google
|
||||
Gemini20Pro ModelID = "gemini-2.0-pro"
|
||||
Gemini15Flash ModelID = "gemini-1.5-flash"
|
||||
Gemini20Flash ModelID = "gemini-2.0-flash"
|
||||
// xAI
|
||||
Grok3 ModelID = "grok-3"
|
||||
Grok2Mini ModelID = "grok-2-mini"
|
||||
// DeepSeek
|
||||
DeepSeekR1 ModelID = "deepseek-r1"
|
||||
DeepSeekCoder ModelID = "deepseek-coder"
|
||||
// Meta
|
||||
Llama3 ModelID = "llama-3"
|
||||
Llama270B ModelID = "llama-2-70b"
|
||||
)
|
||||
|
||||
const (
|
||||
ProviderOpenAI ModelProvider = "openai"
|
||||
ProviderAnthropic ModelProvider = "anthropic"
|
||||
ProviderGoogle ModelProvider = "google"
|
||||
ProviderXAI ModelProvider = "xai"
|
||||
ProviderDeepSeek ModelProvider = "deepseek"
|
||||
ProviderMeta ModelProvider = "meta"
|
||||
)
|
||||
|
||||
var SupportedModels = map[ModelID]Model{
|
||||
// OpenAI
|
||||
GPT4o: {
|
||||
ID: GPT4o,
|
||||
Name: "GPT-4o",
|
||||
Provider: ProviderOpenAI,
|
||||
APIModel: "gpt-4o",
|
||||
},
|
||||
GPT4oMini: {
|
||||
ID: GPT4oMini,
|
||||
Name: "GPT-4o Mini",
|
||||
Provider: ProviderOpenAI,
|
||||
APIModel: "gpt-4o-mini",
|
||||
},
|
||||
GPT45: {
|
||||
ID: GPT45,
|
||||
Name: "GPT-4.5",
|
||||
Provider: ProviderOpenAI,
|
||||
APIModel: "gpt-4.5",
|
||||
},
|
||||
O1: {
|
||||
ID: O1,
|
||||
Name: "o1",
|
||||
Provider: ProviderOpenAI,
|
||||
APIModel: "o1",
|
||||
},
|
||||
O1Mini: {
|
||||
ID: O1Mini,
|
||||
Name: "o1 Mini",
|
||||
Provider: ProviderOpenAI,
|
||||
APIModel: "o1-mini",
|
||||
},
|
||||
// Anthropic
|
||||
Claude35Sonnet: {
|
||||
ID: Claude35Sonnet,
|
||||
Name: "Claude 3.5 Sonnet",
|
||||
Provider: ProviderAnthropic,
|
||||
APIModel: "claude-3.5-sonnet",
|
||||
},
|
||||
Claude3Haiku: {
|
||||
ID: Claude3Haiku,
|
||||
Name: "Claude 3 Haiku",
|
||||
Provider: ProviderAnthropic,
|
||||
APIModel: "claude-3-haiku",
|
||||
},
|
||||
Claude37Sonnet: {
|
||||
ID: Claude37Sonnet,
|
||||
Name: "Claude 3.7 Sonnet",
|
||||
Provider: ProviderAnthropic,
|
||||
APIModel: "claude-3-7-sonnet-20250219",
|
||||
},
|
||||
// Google
|
||||
Gemini20Pro: {
|
||||
ID: Gemini20Pro,
|
||||
Name: "Gemini 2.0 Pro",
|
||||
Provider: ProviderGoogle,
|
||||
APIModel: "gemini-2.0-pro",
|
||||
},
|
||||
Gemini15Flash: {
|
||||
ID: Gemini15Flash,
|
||||
Name: "Gemini 1.5 Flash",
|
||||
Provider: ProviderGoogle,
|
||||
APIModel: "gemini-1.5-flash",
|
||||
},
|
||||
Gemini20Flash: {
|
||||
ID: Gemini20Flash,
|
||||
Name: "Gemini 2.0 Flash",
|
||||
Provider: ProviderGoogle,
|
||||
APIModel: "gemini-2.0-flash",
|
||||
},
|
||||
// xAI
|
||||
Grok3: {
|
||||
ID: Grok3,
|
||||
Name: "Grok 3",
|
||||
Provider: ProviderXAI,
|
||||
APIModel: "grok-3",
|
||||
},
|
||||
Grok2Mini: {
|
||||
ID: Grok2Mini,
|
||||
Name: "Grok 2 Mini",
|
||||
Provider: ProviderXAI,
|
||||
APIModel: "grok-2-mini",
|
||||
},
|
||||
// DeepSeek
|
||||
DeepSeekR1: {
|
||||
ID: DeepSeekR1,
|
||||
Name: "DeepSeek R1",
|
||||
Provider: ProviderDeepSeek,
|
||||
APIModel: "deepseek-r1",
|
||||
},
|
||||
DeepSeekCoder: {
|
||||
ID: DeepSeekCoder,
|
||||
Name: "DeepSeek Coder",
|
||||
Provider: ProviderDeepSeek,
|
||||
APIModel: "deepseek-coder",
|
||||
},
|
||||
// Meta
|
||||
Llama3: {
|
||||
ID: Llama3,
|
||||
Name: "LLaMA 3",
|
||||
Provider: ProviderMeta,
|
||||
APIModel: "llama-3",
|
||||
},
|
||||
Llama270B: {
|
||||
ID: Llama270B,
|
||||
Name: "LLaMA 2 70B",
|
||||
Provider: ProviderMeta,
|
||||
APIModel: "llama-2-70b",
|
||||
},
|
||||
}
|
||||
|
||||
func GetModel(ctx context.Context, model ModelID) (model.ChatModel, error) {
|
||||
provider := SupportedModels[model].Provider
|
||||
maxTokens := viper.GetInt("providers.common.max_tokens")
|
||||
switch provider {
|
||||
case ProviderOpenAI:
|
||||
return openai.NewChatModel(ctx, &openai.ChatModelConfig{
|
||||
APIKey: viper.GetString("providers.openai.key"),
|
||||
Model: string(SupportedModels[model].APIModel),
|
||||
MaxTokens: &maxTokens,
|
||||
})
|
||||
case ProviderAnthropic:
|
||||
return claude.NewChatModel(ctx, &claude.Config{
|
||||
APIKey: viper.GetString("providers.anthropic.key"),
|
||||
Model: string(SupportedModels[model].APIModel),
|
||||
MaxTokens: maxTokens,
|
||||
})
|
||||
|
||||
}
|
||||
return nil, errors.New("unsupported provider")
|
||||
}
|
||||
454
internal/llm/tools/bash.go
Normal file
454
internal/llm/tools/bash.go
Normal file
@@ -0,0 +1,454 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/cloudwego/eino/components/tool"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/kujtimiihoxha/termai/internal/llm/tools/shell"
|
||||
)
|
||||
|
||||
type bashTool struct {
|
||||
workingDir string
|
||||
}
|
||||
|
||||
const (
|
||||
BashToolName = "bash"
|
||||
|
||||
DefaultTimeout = 30 * 60 * 1000 // 30 minutes in milliseconds
|
||||
MaxTimeout = 10 * 60 * 1000 // 10 minutes in milliseconds
|
||||
MaxOutputLength = 30000
|
||||
)
|
||||
|
||||
type BashParams struct {
|
||||
Command string `json:"command"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
var BannedCommands = []string{
|
||||
"alias", "curl", "curlie", "wget", "axel", "aria2c",
|
||||
"nc", "telnet", "lynx", "w3m", "links", "httpie", "xh",
|
||||
"http-prompt", "chrome", "firefox", "safari",
|
||||
}
|
||||
|
||||
func (b *bashTool) Info(ctx context.Context) (*schema.ToolInfo, error) {
|
||||
return &schema.ToolInfo{
|
||||
Name: BashToolName,
|
||||
Desc: bashDescription(),
|
||||
ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{
|
||||
"command": {
|
||||
Type: "string",
|
||||
Desc: "The command to execute",
|
||||
Required: true,
|
||||
},
|
||||
"timeout": {
|
||||
Type: "number",
|
||||
Desc: "Optional timeout in milliseconds (max 600000)",
|
||||
},
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Handle implements Tool.
|
||||
func (b *bashTool) InvokableRun(ctx context.Context, args string, opts ...tool.Option) (string, error) {
|
||||
log.Printf("BashTool InvokableRun: %s", args)
|
||||
var params BashParams
|
||||
if err := json.Unmarshal([]byte(args), ¶ms); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if params.Timeout > MaxTimeout {
|
||||
params.Timeout = MaxTimeout
|
||||
} else if params.Timeout <= 0 {
|
||||
params.Timeout = DefaultTimeout
|
||||
}
|
||||
|
||||
if params.Command == "" {
|
||||
return "", errors.New("missing command")
|
||||
}
|
||||
|
||||
baseCmd := strings.Fields(params.Command)[0]
|
||||
for _, banned := range BannedCommands {
|
||||
if strings.EqualFold(baseCmd, banned) {
|
||||
return "", fmt.Errorf("command '%s' is not allowed", baseCmd)
|
||||
}
|
||||
}
|
||||
|
||||
// p := b.permission.Request(permission.CreatePermissionRequest{
|
||||
// Path: b.workingDir,
|
||||
// ToolName: BashToolName,
|
||||
// Action: "execute",
|
||||
// Description: fmt.Sprintf("Execute command: %s", params.Command),
|
||||
// Params: map[string]any{
|
||||
// "command": params.Command,
|
||||
// "timeout": params.Timeout,
|
||||
// },
|
||||
// })
|
||||
// if !p {
|
||||
// return "", errors.New("permission denied")
|
||||
// }
|
||||
|
||||
shell := shell.GetPersistentShell(b.workingDir)
|
||||
stdout, stderr, exitCode, interrupted, err := shell.Exec(ctx, params.Command, params.Timeout)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stdout = truncateOutput(stdout)
|
||||
stderr = truncateOutput(stderr)
|
||||
|
||||
errorMessage := stderr
|
||||
if interrupted {
|
||||
if errorMessage != "" {
|
||||
errorMessage += "\n"
|
||||
}
|
||||
errorMessage += "Command was aborted before completion"
|
||||
} else if exitCode != 0 {
|
||||
if errorMessage != "" {
|
||||
errorMessage += "\n"
|
||||
}
|
||||
errorMessage += fmt.Sprintf("Exit code %d", exitCode)
|
||||
}
|
||||
|
||||
hasBothOutputs := stdout != "" && stderr != ""
|
||||
|
||||
if hasBothOutputs {
|
||||
stdout += "\n"
|
||||
}
|
||||
|
||||
if errorMessage != "" {
|
||||
stdout += "\n" + errorMessage
|
||||
}
|
||||
|
||||
log.Printf("BashTool InvokableRun: stdout: %s, stderr: %s, exitCode: %d, interrupted: %t", stdout, stderr, exitCode, interrupted)
|
||||
|
||||
return stdout, nil
|
||||
}
|
||||
|
||||
func truncateOutput(content string) string {
|
||||
if len(content) <= MaxOutputLength {
|
||||
return content
|
||||
}
|
||||
|
||||
halfLength := MaxOutputLength / 2
|
||||
start := content[:halfLength]
|
||||
end := content[len(content)-halfLength:]
|
||||
|
||||
truncatedLinesCount := countLines(content[halfLength : len(content)-halfLength])
|
||||
return fmt.Sprintf("%s\n\n... [%d lines truncated] ...\n\n%s", start, truncatedLinesCount, end)
|
||||
}
|
||||
|
||||
func countLines(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
return len(strings.Split(s, "\n"))
|
||||
}
|
||||
|
||||
func bashDescriptionBCP() string {
|
||||
bannedCommandsStr := strings.Join(BannedCommands, ", ")
|
||||
return fmt.Sprintf(`Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
|
||||
|
||||
Before executing the command, please follow these steps:
|
||||
|
||||
1. Directory Verification:
|
||||
- If the command will create new directories or files, first use the LS tool to verify the parent directory exists and is the correct location
|
||||
- For example, before running "mkdir foo/bar", first use LS to check that "foo" exists and is the intended parent directory
|
||||
|
||||
2. Security Check:
|
||||
- For security and to limit the threat of a prompt injection attack, some commands are limited or banned. If you use a disallowed command, you will receive an error message explaining the restriction. Explain the error to the User.
|
||||
- Verify that the command is not one of the banned commands: %s.
|
||||
|
||||
3. Command Execution:
|
||||
- After ensuring proper quoting, execute the command.
|
||||
- Capture the output of the command.
|
||||
|
||||
4. Output Processing:
|
||||
- If the output exceeds %d characters, output will be truncated before being returned to you.
|
||||
- Prepare the output for display to the user.
|
||||
|
||||
5. Return Result:
|
||||
- Provide the processed output of the command.
|
||||
- If any errors occurred during execution, include those in the output.
|
||||
|
||||
Usage notes:
|
||||
- The command argument is required.
|
||||
- You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will timeout after 30 minutes.
|
||||
- VERY IMPORTANT: You MUST avoid using search commands like 'find' and 'grep'. Instead use Grep, Glob, or Agent tools to search. You MUST avoid read tools like 'cat', 'head', 'tail', and 'ls', and use FileRead and LS tools to read files.
|
||||
- When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
|
||||
- IMPORTANT: All commands share the same shell session. Shell state (environment variables, virtual environments, current directory, etc.) persist between commands. For example, if you set an environment variable as part of a command, the environment variable will persist for subsequent commands.
|
||||
- Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of 'cd'. You may use 'cd' if the User explicitly requests it.
|
||||
<good-example>
|
||||
pytest /foo/bar/tests
|
||||
</good-example>
|
||||
<bad-example>
|
||||
cd /foo/bar && pytest tests
|
||||
</bad-example>
|
||||
|
||||
# Committing changes with git
|
||||
|
||||
When the user asks you to create a new git commit, follow these steps carefully:
|
||||
|
||||
1. Start with a single message that contains exactly three tool_use blocks that do the following (it is VERY IMPORTANT that you send these tool_use blocks in a single message, otherwise it will feel slow to the user!):
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
||||
|
||||
2. Use the git context at the start of this conversation to determine which files are relevant to your commit. Add relevant untracked files to the staging area. Do not commit files that were already modified at the start of this conversation, if they are not relevant to your commit.
|
||||
|
||||
3. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
|
||||
|
||||
<commit_analysis>
|
||||
- List the files that have been changed or added
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
||||
- Brainstorm the purpose or motivation behind these changes
|
||||
- Do not use tools to explore code, beyond what is available in the git context
|
||||
- Assess the impact of these changes on the overall project
|
||||
- Check for any sensitive information that shouldn't be committed
|
||||
- Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
|
||||
- Ensure your language is clear, concise, and to the point
|
||||
- Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
||||
- Ensure the message is not generic (avoid words like "Update" or "Fix" without context)
|
||||
- Review the draft message to ensure it accurately reflects the changes and their purpose
|
||||
</commit_analysis>
|
||||
|
||||
4. Create the commit with a message ending with:
|
||||
🤖 Generated with termai
|
||||
Co-Authored-By: termai <noreply@termai.io>
|
||||
|
||||
- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
|
||||
<example>
|
||||
git commit -m "$(cat <<'EOF'
|
||||
Commit message here.
|
||||
|
||||
🤖 Generated with termai
|
||||
Co-Authored-By: termai <noreply@termai.io>
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
5. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
|
||||
|
||||
6. Finally, run git status to make sure the commit succeeded.
|
||||
|
||||
Important notes:
|
||||
- When possible, combine the "git add" and "git commit" commands into a single "git commit -am" command, to speed things up
|
||||
- However, be careful not to stage files (e.g. with 'git add .') for commits that aren't part of the change, they may have untracked files they want to keep around, but not commit.
|
||||
- NEVER update the git config
|
||||
- DO NOT push to the remote repository
|
||||
- IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
|
||||
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
||||
- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
|
||||
- Return an empty response - the user will see the git output directly
|
||||
|
||||
# Creating pull requests
|
||||
Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
|
||||
|
||||
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
||||
|
||||
1. Understand the current state of the branch. Remember to send a single message that contains multiple tool_use blocks (it is VERY IMPORTANT that you do this in a single message, otherwise it will feel slow to the user!):
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
|
||||
- Run a git log command and 'git diff main...HEAD' to understand the full commit history for the current branch (from the time it diverged from the 'main' branch.)
|
||||
|
||||
2. Create new branch if needed
|
||||
|
||||
3. Commit changes if needed
|
||||
|
||||
4. Push to remote with -u flag if needed
|
||||
|
||||
5. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (not just the latest commit, but all commits that will be included in the pull request!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
|
||||
|
||||
<pr_analysis>
|
||||
- List the commits since diverging from the main branch
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
||||
- Brainstorm the purpose or motivation behind these changes
|
||||
- Assess the impact of these changes on the overall project
|
||||
- Do not use tools to explore code, beyond what is available in the git context
|
||||
- Check for any sensitive information that shouldn't be committed
|
||||
- Draft a concise (1-2 bullet points) pull request summary that focuses on the "why" rather than the "what"
|
||||
- Ensure the summary accurately reflects all changes since diverging from the main branch
|
||||
- Ensure your language is clear, concise, and to the point
|
||||
- Ensure the summary accurately reflects the changes and their purpose (ie. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
||||
- Ensure the summary is not generic (avoid words like "Update" or "Fix" without context)
|
||||
- Review the draft summary to ensure it accurately reflects the changes and their purpose
|
||||
</pr_analysis>
|
||||
|
||||
6. Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
||||
<example>
|
||||
gh pr create --title "the pr title" --body "$(cat <<'EOF'
|
||||
## Summary
|
||||
<1-3 bullet points>
|
||||
|
||||
## Test plan
|
||||
[Checklist of TODOs for testing the pull request...]
|
||||
|
||||
🤖 Generated with termai
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
Important:
|
||||
- Return an empty response - the user will see the gh output directly
|
||||
- Never update git config`, bannedCommandsStr, MaxOutputLength)
|
||||
}
|
||||
|
||||
func bashDescription() string {
|
||||
bannedCommandsStr := strings.Join(BannedCommands, ", ")
|
||||
return fmt.Sprintf(`Executes a given bash command in a persistent shell session with optional timeout, ensuring proper handling and security measures.
|
||||
|
||||
Before executing the command, please follow these steps:
|
||||
|
||||
1. Directory Verification:
|
||||
- If the command will create new directories or files, first use the ls command to verify the parent directory exists and is the correct location
|
||||
- For example, before running "mkdir foo/bar", first use ls command to check that "foo" exists and is the intended parent directory
|
||||
|
||||
2. Security Check:
|
||||
- For security and to limit the threat of a prompt injection attack, some commands are limited or banned. If you use a disallowed command, you will receive an error message explaining the restriction. Explain the error to the User.
|
||||
- Verify that the command is not one of the banned commands: %s.
|
||||
|
||||
3. Command Execution:
|
||||
- After ensuring proper quoting, execute the command.
|
||||
- Capture the output of the command.
|
||||
|
||||
4. Output Processing:
|
||||
- If the output exceeds %d characters, output will be truncated before being returned to you.
|
||||
- Prepare the output for display to the user.
|
||||
|
||||
5. Return Result:
|
||||
- Provide the processed output of the command.
|
||||
- If any errors occurred during execution, include those in the output.
|
||||
|
||||
Usage notes:
|
||||
- The command argument is required.
|
||||
- You can specify an optional timeout in milliseconds (up to 600000ms / 10 minutes). If not specified, commands will timeout after 30 minutes.
|
||||
- When issuing multiple commands, use the ';' or '&&' operator to separate them. DO NOT use newlines (newlines are ok in quoted strings).
|
||||
- IMPORTANT: All commands share the same shell session. Shell state (environment variables, virtual environments, current directory, etc.) persist between commands. For example, if you set an environment variable as part of a command, the environment variable will persist for subsequent commands.
|
||||
- Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of 'cd'. You may use 'cd' if the User explicitly requests it.
|
||||
<good-example>
|
||||
pytest /foo/bar/tests
|
||||
</good-example>
|
||||
<bad-example>
|
||||
cd /foo/bar && pytest tests
|
||||
</bad-example>
|
||||
|
||||
# Committing changes with git
|
||||
|
||||
When the user asks you to create a new git commit, follow these steps carefully:
|
||||
|
||||
1. Start with a single message that contains exactly three tool_use blocks that do the following (it is VERY IMPORTANT that you send these tool_use blocks in a single message, otherwise it will feel slow to the user!):
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Run a git log command to see recent commit messages, so that you can follow this repository's commit message style.
|
||||
|
||||
2. Use the git context at the start of this conversation to determine which files are relevant to your commit. Add relevant untracked files to the staging area. Do not commit files that were already modified at the start of this conversation, if they are not relevant to your commit.
|
||||
|
||||
3. Analyze all staged changes (both previously staged and newly added) and draft a commit message. Wrap your analysis process in <commit_analysis> tags:
|
||||
|
||||
<commit_analysis>
|
||||
- List the files that have been changed or added
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
||||
- Brainstorm the purpose or motivation behind these changes
|
||||
- Do not use tools to explore code, beyond what is available in the git context
|
||||
- Assess the impact of these changes on the overall project
|
||||
- Check for any sensitive information that shouldn't be committed
|
||||
- Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
|
||||
- Ensure your language is clear, concise, and to the point
|
||||
- Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
||||
- Ensure the message is not generic (avoid words like "Update" or "Fix" without context)
|
||||
- Review the draft message to ensure it accurately reflects the changes and their purpose
|
||||
</commit_analysis>
|
||||
|
||||
4. Create the commit with a message ending with:
|
||||
🤖 Generated with termai
|
||||
Co-Authored-By: termai <noreply@termai.io>
|
||||
|
||||
- In order to ensure good formatting, ALWAYS pass the commit message via a HEREDOC, a la this example:
|
||||
<example>
|
||||
git commit -m "$(cat <<'EOF'
|
||||
Commit message here.
|
||||
|
||||
🤖 Generated with termai
|
||||
Co-Authored-By: termai <noreply@termai.io>
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
5. If the commit fails due to pre-commit hook changes, retry the commit ONCE to include these automated changes. If it fails again, it usually means a pre-commit hook is preventing the commit. If the commit succeeds but you notice that files were modified by the pre-commit hook, you MUST amend your commit to include them.
|
||||
|
||||
6. Finally, run git status to make sure the commit succeeded.
|
||||
|
||||
Important notes:
|
||||
- When possible, combine the "git add" and "git commit" commands into a single "git commit -am" command, to speed things up
|
||||
- However, be careful not to stage files (e.g. with 'git add .') for commits that aren't part of the change, they may have untracked files they want to keep around, but not commit.
|
||||
- NEVER update the git config
|
||||
- DO NOT push to the remote repository
|
||||
- IMPORTANT: Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported.
|
||||
- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
|
||||
- Ensure your commit message is meaningful and concise. It should explain the purpose of the changes, not just describe them.
|
||||
- Return an empty response - the user will see the git output directly
|
||||
|
||||
# Creating pull requests
|
||||
Use the gh command via the Bash tool for ALL GitHub-related tasks including working with issues, pull requests, checks, and releases. If given a Github URL use the gh command to get the information needed.
|
||||
|
||||
IMPORTANT: When the user asks you to create a pull request, follow these steps carefully:
|
||||
|
||||
1. Understand the current state of the branch. Remember to send a single message that contains multiple tool_use blocks (it is VERY IMPORTANT that you do this in a single message, otherwise it will feel slow to the user!):
|
||||
- Run a git status command to see all untracked files.
|
||||
- Run a git diff command to see both staged and unstaged changes that will be committed.
|
||||
- Check if the current branch tracks a remote branch and is up to date with the remote, so you know if you need to push to the remote
|
||||
- Run a git log command and 'git diff main...HEAD' to understand the full commit history for the current branch (from the time it diverged from the 'main' branch.)
|
||||
|
||||
2. Create new branch if needed
|
||||
|
||||
3. Commit changes if needed
|
||||
|
||||
4. Push to remote with -u flag if needed
|
||||
|
||||
5. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (not just the latest commit, but all commits that will be included in the pull request!), and draft a pull request summary. Wrap your analysis process in <pr_analysis> tags:
|
||||
|
||||
<pr_analysis>
|
||||
- List the commits since diverging from the main branch
|
||||
- Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
|
||||
- Brainstorm the purpose or motivation behind these changes
|
||||
- Assess the impact of these changes on the overall project
|
||||
- Do not use tools to explore code, beyond what is available in the git context
|
||||
- Check for any sensitive information that shouldn't be committed
|
||||
- Draft a concise (1-2 bullet points) pull request summary that focuses on the "why" rather than the "what"
|
||||
- Ensure the summary accurately reflects all changes since diverging from the main branch
|
||||
- Ensure your language is clear, concise, and to the point
|
||||
- Ensure the summary accurately reflects the changes and their purpose (ie. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
|
||||
- Ensure the summary is not generic (avoid words like "Update" or "Fix" without context)
|
||||
- Review the draft summary to ensure it accurately reflects the changes and their purpose
|
||||
</pr_analysis>
|
||||
|
||||
6. Create PR using gh pr create with the format below. Use a HEREDOC to pass the body to ensure correct formatting.
|
||||
<example>
|
||||
gh pr create --title "the pr title" --body "$(cat <<'EOF'
|
||||
## Summary
|
||||
<1-3 bullet points>
|
||||
|
||||
## Test plan
|
||||
[Checklist of TODOs for testing the pull request...]
|
||||
|
||||
🤖 Generated with termai
|
||||
EOF
|
||||
)"
|
||||
</example>
|
||||
|
||||
Important:
|
||||
- Return an empty response - the user will see the gh output directly
|
||||
- Never update git config`, bannedCommandsStr, MaxOutputLength)
|
||||
}
|
||||
|
||||
func NewBashTool(workingDir string) tool.InvokableTool {
|
||||
return &bashTool{
|
||||
workingDir: workingDir,
|
||||
}
|
||||
}
|
||||
294
internal/llm/tools/shell/shell.go
Normal file
294
internal/llm/tools/shell/shell.go
Normal file
@@ -0,0 +1,294 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PersistentShell struct {
|
||||
cmd *exec.Cmd
|
||||
stdin *os.File
|
||||
isAlive bool
|
||||
cwd string
|
||||
mu sync.Mutex
|
||||
commandQueue chan *commandExecution
|
||||
}
|
||||
|
||||
type commandExecution struct {
|
||||
command string
|
||||
timeout time.Duration
|
||||
resultChan chan commandResult
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type commandResult struct {
|
||||
stdout string
|
||||
stderr string
|
||||
exitCode int
|
||||
interrupted bool
|
||||
err error
|
||||
}
|
||||
|
||||
var (
|
||||
shellInstance *PersistentShell
|
||||
shellInstanceOnce sync.Once
|
||||
)
|
||||
|
||||
func GetPersistentShell(workingDir string) *PersistentShell {
|
||||
shellInstanceOnce.Do(func() {
|
||||
shellInstance = newPersistentShell(workingDir)
|
||||
})
|
||||
|
||||
if !shellInstance.isAlive {
|
||||
shellInstance = newPersistentShell(shellInstance.cwd)
|
||||
}
|
||||
|
||||
return shellInstance
|
||||
}
|
||||
|
||||
func newPersistentShell(cwd string) *PersistentShell {
|
||||
shellPath := os.Getenv("SHELL")
|
||||
if shellPath == "" {
|
||||
shellPath = "/bin/bash"
|
||||
}
|
||||
|
||||
cmd := exec.Command(shellPath, "-l")
|
||||
cmd.Dir = cwd
|
||||
|
||||
stdinPipe, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd.Env = append(os.Environ(), "GIT_EDITOR=true")
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
shell := &PersistentShell{
|
||||
cmd: cmd,
|
||||
stdin: stdinPipe.(*os.File),
|
||||
isAlive: true,
|
||||
cwd: cwd,
|
||||
commandQueue: make(chan *commandExecution, 10),
|
||||
}
|
||||
|
||||
go shell.processCommands()
|
||||
|
||||
go func() {
|
||||
err := cmd.Wait()
|
||||
if err != nil {
|
||||
}
|
||||
shell.isAlive = false
|
||||
close(shell.commandQueue)
|
||||
}()
|
||||
|
||||
return shell
|
||||
}
|
||||
|
||||
func (s *PersistentShell) processCommands() {
|
||||
for cmd := range s.commandQueue {
|
||||
result := s.execCommand(cmd.command, cmd.timeout, cmd.ctx)
|
||||
cmd.resultChan <- result
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PersistentShell) execCommand(command string, timeout time.Duration, ctx context.Context) commandResult {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if !s.isAlive {
|
||||
return commandResult{
|
||||
stderr: "Shell is not alive",
|
||||
exitCode: 1,
|
||||
err: errors.New("shell is not alive"),
|
||||
}
|
||||
}
|
||||
|
||||
tempDir := os.TempDir()
|
||||
stdoutFile := filepath.Join(tempDir, fmt.Sprintf("orbitowl-stdout-%d", time.Now().UnixNano()))
|
||||
stderrFile := filepath.Join(tempDir, fmt.Sprintf("orbitowl-stderr-%d", time.Now().UnixNano()))
|
||||
statusFile := filepath.Join(tempDir, fmt.Sprintf("orbitowl-status-%d", time.Now().UnixNano()))
|
||||
cwdFile := filepath.Join(tempDir, fmt.Sprintf("orbitowl-cwd-%d", time.Now().UnixNano()))
|
||||
|
||||
defer func() {
|
||||
os.Remove(stdoutFile)
|
||||
os.Remove(stderrFile)
|
||||
os.Remove(statusFile)
|
||||
os.Remove(cwdFile)
|
||||
}()
|
||||
|
||||
fullCommand := fmt.Sprintf(`
|
||||
eval %s < /dev/null > %s 2> %s
|
||||
EXEC_EXIT_CODE=$?
|
||||
pwd > %s
|
||||
echo $EXEC_EXIT_CODE > %s
|
||||
`,
|
||||
shellQuote(command),
|
||||
shellQuote(stdoutFile),
|
||||
shellQuote(stderrFile),
|
||||
shellQuote(cwdFile),
|
||||
shellQuote(statusFile),
|
||||
)
|
||||
|
||||
_, err := s.stdin.Write([]byte(fullCommand + "\n"))
|
||||
if err != nil {
|
||||
return commandResult{
|
||||
stderr: fmt.Sprintf("Failed to write command to shell: %v", err),
|
||||
exitCode: 1,
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
interrupted := false
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
s.killChildren()
|
||||
interrupted = true
|
||||
done <- true
|
||||
return
|
||||
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
if fileExists(statusFile) && fileSize(statusFile) > 0 {
|
||||
done <- true
|
||||
return
|
||||
}
|
||||
|
||||
if timeout > 0 {
|
||||
elapsed := time.Since(startTime)
|
||||
if elapsed > timeout {
|
||||
s.killChildren()
|
||||
interrupted = true
|
||||
done <- true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
<-done
|
||||
|
||||
stdout := readFileOrEmpty(stdoutFile)
|
||||
stderr := readFileOrEmpty(stderrFile)
|
||||
exitCodeStr := readFileOrEmpty(statusFile)
|
||||
newCwd := readFileOrEmpty(cwdFile)
|
||||
|
||||
exitCode := 0
|
||||
if exitCodeStr != "" {
|
||||
fmt.Sscanf(exitCodeStr, "%d", &exitCode)
|
||||
} else if interrupted {
|
||||
exitCode = 143
|
||||
stderr += "\nCommand execution timed out or was interrupted"
|
||||
}
|
||||
|
||||
if newCwd != "" {
|
||||
s.cwd = strings.TrimSpace(newCwd)
|
||||
}
|
||||
|
||||
return commandResult{
|
||||
stdout: stdout,
|
||||
stderr: stderr,
|
||||
exitCode: exitCode,
|
||||
interrupted: interrupted,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PersistentShell) killChildren() {
|
||||
if s.cmd == nil || s.cmd.Process == nil {
|
||||
return
|
||||
}
|
||||
|
||||
pgrepCmd := exec.Command("pgrep", "-P", fmt.Sprintf("%d", s.cmd.Process.Pid))
|
||||
output, err := pgrepCmd.Output()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, pidStr := range strings.Split(string(output), "\n") {
|
||||
if pidStr = strings.TrimSpace(pidStr); pidStr != "" {
|
||||
var pid int
|
||||
fmt.Sscanf(pidStr, "%d", &pid)
|
||||
if pid > 0 {
|
||||
proc, err := os.FindProcess(pid)
|
||||
if err == nil {
|
||||
proc.Signal(syscall.SIGTERM)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PersistentShell) Exec(ctx context.Context, command string, timeoutMs int) (string, string, int, bool, error) {
|
||||
if !s.isAlive {
|
||||
return "", "Shell is not alive", 1, false, errors.New("shell is not alive")
|
||||
}
|
||||
|
||||
timeout := time.Duration(timeoutMs) * time.Millisecond
|
||||
|
||||
resultChan := make(chan commandResult)
|
||||
s.commandQueue <- &commandExecution{
|
||||
command: command,
|
||||
timeout: timeout,
|
||||
resultChan: resultChan,
|
||||
ctx: ctx,
|
||||
}
|
||||
|
||||
result := <-resultChan
|
||||
return result.stdout, result.stderr, result.exitCode, result.interrupted, result.err
|
||||
}
|
||||
|
||||
func (s *PersistentShell) Close() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if !s.isAlive {
|
||||
return
|
||||
}
|
||||
|
||||
s.stdin.Write([]byte("exit\n"))
|
||||
|
||||
s.cmd.Process.Kill()
|
||||
s.isAlive = false
|
||||
}
|
||||
|
||||
func shellQuote(s string) string {
|
||||
return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'"
|
||||
}
|
||||
|
||||
func readFileOrEmpty(path string) string {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(content)
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func fileSize(path string) int64 {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return info.Size()
|
||||
}
|
||||
122
internal/message/message.go
Normal file
122
internal/message/message.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/google/uuid"
|
||||
"github.com/kujtimiihoxha/termai/internal/db"
|
||||
"github.com/kujtimiihoxha/termai/internal/pubsub"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
SessionID string
|
||||
MessageData schema.Message
|
||||
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
pubsub.Suscriber[Message]
|
||||
Create(sessionID string, messageData schema.Message) (Message, error)
|
||||
Get(id string) (Message, error)
|
||||
List(sessionID string) ([]Message, error)
|
||||
Delete(id string) error
|
||||
DeleteSessionMessages(sessionID string) error
|
||||
}
|
||||
|
||||
type service struct {
|
||||
*pubsub.Broker[Message]
|
||||
q db.Querier
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (s *service) Create(sessionID string, messageData schema.Message) (Message, error) {
|
||||
messageDataJSON, err := json.Marshal(messageData)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
dbMessage, err := s.q.CreateMessage(s.ctx, db.CreateMessageParams{
|
||||
ID: uuid.New().String(),
|
||||
SessionID: sessionID,
|
||||
MessageData: string(messageDataJSON),
|
||||
})
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message := s.fromDBItem(dbMessage)
|
||||
s.Publish(pubsub.CreatedEvent, message)
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(id string) error {
|
||||
message, err := s.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = s.q.DeleteMessage(s.ctx, message.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Publish(pubsub.DeletedEvent, message)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) DeleteSessionMessages(sessionID string) error {
|
||||
messages, err := s.List(sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, message := range messages {
|
||||
if message.SessionID == sessionID {
|
||||
err = s.Delete(message.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Get(id string) (Message, error) {
|
||||
dbMessage, err := s.q.GetMessage(s.ctx, id)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return s.fromDBItem(dbMessage), nil
|
||||
}
|
||||
|
||||
func (s *service) List(sessionID string) ([]Message, error) {
|
||||
dbMessages, err := s.q.ListMessagesBySession(s.ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
messages := make([]Message, len(dbMessages))
|
||||
for i, dbMessage := range dbMessages {
|
||||
messages[i] = s.fromDBItem(dbMessage)
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func (s *service) fromDBItem(item db.Message) Message {
|
||||
var messageData schema.Message
|
||||
json.Unmarshal([]byte(item.MessageData), &messageData)
|
||||
return Message{
|
||||
ID: item.ID,
|
||||
SessionID: item.SessionID,
|
||||
MessageData: messageData,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func NewService(ctx context.Context, q db.Querier) Service {
|
||||
return &service{
|
||||
Broker: pubsub.NewBroker[Message](),
|
||||
q: q,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,14 @@ import (
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
ID string
|
||||
Title string
|
||||
MessageCount int64
|
||||
Tokens int64
|
||||
Cost float64
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
ID string
|
||||
Title string
|
||||
MessageCount int64
|
||||
PromptTokens int64
|
||||
CompletionTokens int64
|
||||
Cost float64
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
@@ -69,10 +70,11 @@ func (s *service) Get(id string) (Session, error) {
|
||||
|
||||
func (s *service) Save(session Session) (Session, error) {
|
||||
dbSession, err := s.q.UpdateSession(s.ctx, db.UpdateSessionParams{
|
||||
ID: session.ID,
|
||||
Title: session.Title,
|
||||
Tokens: session.Tokens,
|
||||
Cost: session.Cost,
|
||||
ID: session.ID,
|
||||
Title: session.Title,
|
||||
PromptTokens: session.PromptTokens,
|
||||
CompletionTokens: session.CompletionTokens,
|
||||
Cost: session.Cost,
|
||||
})
|
||||
if err != nil {
|
||||
return Session{}, err
|
||||
@@ -96,13 +98,14 @@ func (s *service) List() ([]Session, error) {
|
||||
|
||||
func (s service) fromDBItem(item db.Session) Session {
|
||||
return Session{
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
MessageCount: item.MessageCount,
|
||||
Tokens: item.Tokens,
|
||||
Cost: item.Cost,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
MessageCount: item.MessageCount,
|
||||
PromptTokens: item.PromptTokens,
|
||||
CompletionTokens: item.CompletionTokens,
|
||||
Cost: item.Cost,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package repl
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/kujtimiihoxha/termai/internal/app"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/layout"
|
||||
"github.com/kujtimiihoxha/vimtea"
|
||||
@@ -13,6 +16,7 @@ type EditorCmp interface {
|
||||
layout.Focusable
|
||||
layout.Sizeable
|
||||
layout.Bordered
|
||||
layout.Bindings
|
||||
}
|
||||
|
||||
type editorCmp struct {
|
||||
@@ -25,12 +29,16 @@ type editorCmp struct {
|
||||
height int
|
||||
}
|
||||
|
||||
type localKeyMap struct {
|
||||
SendMessage key.Binding
|
||||
SendMessageI key.Binding
|
||||
type editorKeyMap struct {
|
||||
SendMessage key.Binding
|
||||
SendMessageI key.Binding
|
||||
InsertMode key.Binding
|
||||
NormaMode key.Binding
|
||||
VisualMode key.Binding
|
||||
VisualLineMode key.Binding
|
||||
}
|
||||
|
||||
var keyMap = localKeyMap{
|
||||
var editorKeyMapValue = editorKeyMap{
|
||||
SendMessage: key.NewBinding(
|
||||
key.WithKeys("enter"),
|
||||
key.WithHelp("enter", "send message normal mode"),
|
||||
@@ -39,6 +47,22 @@ var keyMap = localKeyMap{
|
||||
key.WithKeys("ctrl+s"),
|
||||
key.WithHelp("ctrl+s", "send message insert mode"),
|
||||
),
|
||||
InsertMode: key.NewBinding(
|
||||
key.WithKeys("i"),
|
||||
key.WithHelp("i", "insert mode"),
|
||||
),
|
||||
NormaMode: key.NewBinding(
|
||||
key.WithKeys("esc"),
|
||||
key.WithHelp("esc", "normal mode"),
|
||||
),
|
||||
VisualMode: key.NewBinding(
|
||||
key.WithKeys("v"),
|
||||
key.WithHelp("v", "visual mode"),
|
||||
),
|
||||
VisualLineMode: key.NewBinding(
|
||||
key.WithKeys("V"),
|
||||
key.WithHelp("V", "visual line mode"),
|
||||
),
|
||||
}
|
||||
|
||||
func (m *editorCmp) Init() tea.Cmd {
|
||||
@@ -58,11 +82,11 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch {
|
||||
case key.Matches(msg, keyMap.SendMessage):
|
||||
case key.Matches(msg, editorKeyMapValue.SendMessage):
|
||||
if m.editorMode == vimtea.ModeNormal {
|
||||
return m, m.Send()
|
||||
}
|
||||
case key.Matches(msg, keyMap.SendMessageI):
|
||||
case key.Matches(msg, editorKeyMapValue.SendMessageI):
|
||||
if m.editorMode == vimtea.ModeInsert {
|
||||
return m, m.Send()
|
||||
}
|
||||
@@ -75,36 +99,30 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Blur implements EditorCmp.
|
||||
func (m *editorCmp) Blur() tea.Cmd {
|
||||
m.focused = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// BorderText implements EditorCmp.
|
||||
func (m *editorCmp) BorderText() map[layout.BorderPosition]string {
|
||||
return map[layout.BorderPosition]string{
|
||||
layout.TopLeftBorder: "New Message",
|
||||
}
|
||||
}
|
||||
|
||||
// Focus implements EditorCmp.
|
||||
func (m *editorCmp) Focus() tea.Cmd {
|
||||
m.focused = true
|
||||
return m.editor.Tick()
|
||||
}
|
||||
|
||||
// GetSize implements EditorCmp.
|
||||
func (m *editorCmp) GetSize() (int, int) {
|
||||
return m.width, m.height
|
||||
}
|
||||
|
||||
// IsFocused implements EditorCmp.
|
||||
func (m *editorCmp) IsFocused() bool {
|
||||
return m.focused
|
||||
}
|
||||
|
||||
// SetSize implements EditorCmp.
|
||||
func (m *editorCmp) SetSize(width int, height int) {
|
||||
m.width = width
|
||||
m.height = height
|
||||
@@ -113,8 +131,10 @@ func (m *editorCmp) SetSize(width int, height int) {
|
||||
|
||||
func (m *editorCmp) Send() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
// TODO: Send message
|
||||
return nil
|
||||
content := strings.Join(m.editor.GetBuffer().Lines(), "\n")
|
||||
m.app.Messages.Create(m.sessionID, *schema.UserMessage(content))
|
||||
m.app.LLM.SendRequest(m.sessionID, content)
|
||||
return m.editor.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +142,10 @@ func (m *editorCmp) View() string {
|
||||
return m.editor.View()
|
||||
}
|
||||
|
||||
func (m *editorCmp) BindingKeys() []key.Binding {
|
||||
return layout.KeyMapToSlice(editorKeyMapValue)
|
||||
}
|
||||
|
||||
func NewEditorCmp(app *app.App) EditorCmp {
|
||||
return &editorCmp{
|
||||
app: app,
|
||||
|
||||
@@ -2,27 +2,47 @@ package repl
|
||||
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/kujtimiihoxha/termai/internal/app"
|
||||
"github.com/kujtimiihoxha/termai/internal/message"
|
||||
"github.com/kujtimiihoxha/termai/internal/pubsub"
|
||||
"github.com/kujtimiihoxha/termai/internal/session"
|
||||
)
|
||||
|
||||
type messagesCmp struct {
|
||||
app *app.App
|
||||
app *app.App
|
||||
messages []message.Message
|
||||
session session.Session
|
||||
}
|
||||
|
||||
func (i *messagesCmp) Init() tea.Cmd {
|
||||
func (m *messagesCmp) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *messagesCmp) Update(_ tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return i, nil
|
||||
func (m *messagesCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case pubsub.Event[message.Message]:
|
||||
if msg.Type == pubsub.CreatedEvent {
|
||||
m.messages = append(m.messages, msg.Payload)
|
||||
}
|
||||
case SelectedSessionMsg:
|
||||
m.session, _ = m.app.Sessions.Get(msg.SessionID)
|
||||
m.messages, _ = m.app.Messages.List(m.session.ID)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (i *messagesCmp) View() string {
|
||||
return "Messages"
|
||||
stringMessages := make([]string, len(i.messages))
|
||||
for idx, msg := range i.messages {
|
||||
stringMessages[idx] = msg.MessageData.Content
|
||||
}
|
||||
return lipgloss.JoinVertical(lipgloss.Top, stringMessages...)
|
||||
}
|
||||
|
||||
func NewMessagesCmp(app *app.App) tea.Model {
|
||||
return &messagesCmp{
|
||||
app,
|
||||
app: app,
|
||||
messages: []message.Message{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/charmbracelet/bubbles/list"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/kujtimiihoxha/termai/internal/app"
|
||||
"github.com/kujtimiihoxha/termai/internal/pubsub"
|
||||
"github.com/kujtimiihoxha/termai/internal/session"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/layout"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/styles"
|
||||
@@ -42,19 +43,30 @@ type SelectedSessionMsg struct {
|
||||
SessionID string
|
||||
}
|
||||
|
||||
type sessionsKeyMap struct {
|
||||
Select key.Binding
|
||||
}
|
||||
|
||||
var sessionKeyMapValue = sessionsKeyMap{
|
||||
Select: key.NewBinding(
|
||||
key.WithKeys("enter", " "),
|
||||
key.WithHelp("enter/space", "select session"),
|
||||
),
|
||||
}
|
||||
|
||||
func (i *sessionsCmp) Init() tea.Cmd {
|
||||
existing, err := i.app.Sessions.List()
|
||||
if err != nil {
|
||||
return util.ReportError(err)
|
||||
}
|
||||
if len(existing) == 0 || existing[0].MessageCount > 0 {
|
||||
session, err := i.app.Sessions.Create(
|
||||
newSession, err := i.app.Sessions.Create(
|
||||
"New Session",
|
||||
)
|
||||
if err != nil {
|
||||
return util.ReportError(err)
|
||||
}
|
||||
existing = append(existing, session)
|
||||
existing = append([]session.Session{newSession}, existing...)
|
||||
}
|
||||
return tea.Batch(
|
||||
util.CmdHandler(InsertSessionsMsg{existing}),
|
||||
@@ -70,10 +82,35 @@ func (i *sessionsCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
items[i] = listItem{
|
||||
id: s.ID,
|
||||
title: s.Title,
|
||||
desc: fmt.Sprintf("Tokens: %d, Cost: %.2f", s.Tokens, s.Cost),
|
||||
desc: fmt.Sprintf("Tokens: %d, Cost: %.2f", s.PromptTokens+s.CompletionTokens, s.Cost),
|
||||
}
|
||||
}
|
||||
return i, i.list.SetItems(items)
|
||||
case pubsub.Event[session.Session]:
|
||||
if msg.Type == pubsub.UpdatedEvent {
|
||||
// update the session in the list
|
||||
items := i.list.Items()
|
||||
for idx, item := range items {
|
||||
s := item.(listItem)
|
||||
if s.id == msg.Payload.ID {
|
||||
s.title = msg.Payload.Title
|
||||
s.desc = fmt.Sprintf("Tokens: %d, Cost: %.2f", msg.Payload.PromptTokens+msg.Payload.CompletionTokens, msg.Payload.Cost)
|
||||
items[idx] = s
|
||||
break
|
||||
}
|
||||
}
|
||||
return i, i.list.SetItems(items)
|
||||
}
|
||||
|
||||
case tea.KeyMsg:
|
||||
switch {
|
||||
case key.Matches(msg, sessionKeyMapValue.Select):
|
||||
selected := i.list.SelectedItem()
|
||||
if selected == nil {
|
||||
return i, nil
|
||||
}
|
||||
return i, util.CmdHandler(SelectedSessionMsg{selected.(listItem).id})
|
||||
}
|
||||
}
|
||||
if i.focused {
|
||||
u, cmd := i.list.Update(msg)
|
||||
@@ -129,7 +166,7 @@ func (i *sessionsCmp) BorderText() map[layout.BorderPosition]string {
|
||||
}
|
||||
|
||||
func (i *sessionsCmp) BindingKeys() []key.Binding {
|
||||
return layout.KeyMapToSlice(i.list.KeyMap)
|
||||
return append(layout.KeyMapToSlice(i.list.KeyMap), sessionKeyMapValue.Select)
|
||||
}
|
||||
|
||||
func NewSessionsCmp(app *app.App) SessionsCmp {
|
||||
|
||||
@@ -16,5 +16,6 @@ func NewReplPage(app *app.App) tea.Model {
|
||||
layout.BentoRightTopPane: repl.NewMessagesCmp(app),
|
||||
layout.BentoRightBottomPane: repl.NewEditorCmp(app),
|
||||
},
|
||||
layout.WithBentoLayoutCurrentPane(layout.BentoRightBottomPane),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/kujtimiihoxha/termai/internal/app"
|
||||
"github.com/kujtimiihoxha/termai/internal/llm"
|
||||
"github.com/kujtimiihoxha/termai/internal/pubsub"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/components/core"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/components/dialog"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/layout"
|
||||
@@ -66,6 +70,9 @@ func (a appModel) Init() tea.Cmd {
|
||||
|
||||
func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case pubsub.Event[llm.AgentEvent]:
|
||||
log.Println("Event received")
|
||||
log.Println(msg)
|
||||
case vimtea.EditorModeMsg:
|
||||
a.editorMode = msg.Mode
|
||||
case tea.WindowSizeMsg:
|
||||
|
||||
Reference in New Issue
Block a user