Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: use more descriptive names for database tools #334

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ func main() {
// Run the requested command
var result string
switch command {
case "listTables":
result, err = cmd.ListTables(ctx, db)
case "exec":
result, err = cmd.Exec(ctx, db, os.Getenv("STATEMENT"))
case "listDatabaseTables":
result, err = cmd.ListDatabaseTables(ctx, db)
case "execDatabaseStatement":
result, err = cmd.ExecDatabaseStatement(ctx, db, os.Getenv("STATEMENT"))
if err == nil {
err = saveWorkspaceDB(ctx, g, dbWorkspacePath, dbFile, initialDBData)
}
case "query":
result, err = cmd.Query(ctx, db, os.Getenv("QUERY"))
case "context":
result, err = cmd.Context(ctx, db)
case "runDatabaseQuery":
result, err = cmd.RunDatabaseQuery(ctx, db, os.Getenv("QUERY"))
case "databaseContext":
result, err = cmd.DatabaseContext(ctx, db)
default:
err = fmt.Errorf("unknown command: %s", command)
}
Expand Down
9 changes: 5 additions & 4 deletions database/pkg/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"strings"
)

// Context returns the context text for the SQLite tools.
// DatabaseContext returns the context text for the SQLite tools.
// The resulting string contains all of schemas in the database.
func Context(ctx context.Context, db *sql.DB) (string, error) {
func DatabaseContext(ctx context.Context, db *sql.DB) (string, error) {
// Build the markdown output
var out strings.Builder
out.WriteString(`# START INSTRUCTIONS: Database Tools

You have access to tools for interacting with a SQLite database.
The Exec tool only accepts valid SQLite3 statements.
The Query tool only accepts valid SQLite3 queries.
The "List Database Tables" tool returns a list of tables in the database.
The "Exec Database Statement" tool only accepts valid SQLite3 statements.
The "Run Database Query" tool only accepts valid SQLite3 queries.
Display all results from these tools and their schemas in markdown format.
If the user refers to creating or modifying tables assume they mean a SQLite3 table and not writing a table
in a markdown file.
Expand Down
4 changes: 2 additions & 2 deletions database/pkg/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
)

// Exec executes a SQL statement (e.g., INSERT, UPDATE, DELETE, CREATE) and returns a status message.
func Exec(ctx context.Context, db *sql.DB, stmt string) (string, error) {
// ExecDatabaseStatement executes a SQL statement (e.g., INSERT, UPDATE, DELETE, CREATE) and returns a status message.
func ExecDatabaseStatement(ctx context.Context, db *sql.DB, stmt string) (string, error) {
_, err := db.ExecContext(ctx, stmt)
if err != nil {
return "", fmt.Errorf("error executing SQL: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions database/pkg/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type Output struct {
Rows []map[string]any `json:"rows"`
}

// Query executes a SQL query (e.g., SELECT) and returns the result formatted in JSON
func Query(ctx context.Context, db *sql.DB, query string) (string, error) {
// RunDatabaseQuery executes a SQL query (e.g. SELECT) and returns a JSON object containing the results.
func RunDatabaseQuery(ctx context.Context, db *sql.DB, query string) (string, error) {
if query == "" {
return "", fmt.Errorf("empty query")
}
Expand Down
3 changes: 2 additions & 1 deletion database/pkg/cmd/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"fmt"
)

func ListTables(ctx context.Context, db *sql.DB) (string, error) {
// ListDatabaseTables returns a JSON object containing the list of tables in the database.
func ListDatabaseTables(ctx context.Context, db *sql.DB) (string, error) {
tables, err := listTables(ctx, db)
if err != nil {
return "", fmt.Errorf("failed to list tables: %w", err)
Expand Down
20 changes: 10 additions & 10 deletions database/tool.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ Name: Database
Description: Tools for interacting with a database
Metadata: category: Capability
Metadata: icon: https://cdn.jsdelivr.net/npm/@phosphor-icons/core@2/assets/duotone/database-duotone.svg
Share Tools: Query, Exec
Share Tools: Run Database Query, Exec Database Statement

---
Name: Tables
Description: List all tables in the SQLite database and return the results in markdown format
Name: List Database Tables
Description: List all tables in the SQLite database and return a JSON object containing the results
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the results in markdown format"

Just to double check - you didnt change the return format right? You are just updating the description to match reality?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjellick Yes, I'm just updating it to match a change Darren made a while back. We were originally returning markdown formatted data. It's been JSON for a while at this point.


#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool listTables
#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool listDatabaseTables

---
Name: Query
Description: Run a SQL query against the SQLite database and return the results in markdown format
Name: Run Database Query
Description: Run a SQL query against the SQLite database and return a JSON object containing the results
Share Context: Database Context
Param: query: SQL query to run

#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool query
#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool runDatabaseQuery

---
Name: Exec
Name: Exec Database Statement
Description: Execute a SQL statement against the SQLite database
Share Context: Database Context
Param: statement: SQL statement to execute

#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool exec
#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool execDatabaseStatement

---
Name: Database Context
Type: context

#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool context
#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool databaseContext