44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"g.fc.run/ai/byre/internal/api"
|
|
)
|
|
|
|
func (s *Store) UsageCommands(ctx context.Context) (map[api.AgentSessionVendor]string, error) {
|
|
rows, err := s.db.QueryContext(ctx, `SELECT vendor, command FROM usage_commands`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
cmds := map[api.AgentSessionVendor]string{}
|
|
for rows.Next() {
|
|
vendor := ""
|
|
cmd := ""
|
|
err = rows.Scan(&vendor, &cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cmds[api.AgentSessionVendor(vendor)] = cmd
|
|
}
|
|
return cmds, rows.Err()
|
|
}
|
|
|
|
func (s *Store) SetUsageCommands(ctx context.Context, actor string, cmds map[api.AgentSessionVendor]string) error {
|
|
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
|
for vendor, cmd := range cmds {
|
|
_, err := tx.ExecContext(ctx, `
|
|
INSERT INTO usage_commands (vendor, command) VALUES (?, ?)
|
|
ON CONFLICT (vendor) DO UPDATE SET command = excluded.command
|
|
`, string(vendor), cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return emit(api.Event{Kind: "admin.usage_commands", Actor: actor, Payload: map[string]any{}})
|
|
})
|
|
}
|