Details (original message): Implement the backend: store/bus/runmgr core, mock and LLM runners (agent-native client), HITL three-branch routing, subagent runs, builtin and declarative HTTP tools, ingest parsers (md/txt/docx/pdf-text), knowledge fulltext, layered settings, profiles, plugin host, REST+SSE API, single-binary entrypoint; e2e and unit tests.
126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package store
|
||
|
||
import (
|
||
"database/sql"
|
||
"errors"
|
||
|
||
core "agent-system-core"
|
||
)
|
||
|
||
// ---- 画像 ----
|
||
|
||
func (s *Store) SaveProfile(p core.AgentProfile) error {
|
||
_, err := s.db.Exec(`INSERT OR REPLACE INTO profiles(id,builtin,payload) VALUES(?,?,?)`,
|
||
p.ID, boolInt(p.Builtin), marshalJSON(p))
|
||
return err
|
||
}
|
||
|
||
func (s *Store) ListProfiles() ([]core.AgentProfile, error) {
|
||
rows, err := s.db.Query(`SELECT payload FROM profiles ORDER BY builtin DESC, id`)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer rows.Close()
|
||
var out []core.AgentProfile
|
||
for rows.Next() {
|
||
var payload string
|
||
if err := rows.Scan(&payload); err != nil {
|
||
return nil, err
|
||
}
|
||
p, _ := unmarshalJSON[core.AgentProfile](payload)
|
||
out = append(out, p)
|
||
}
|
||
return out, rows.Err()
|
||
}
|
||
|
||
func (s *Store) GetProfile(id string) (core.AgentProfile, error) {
|
||
var payload string
|
||
err := s.db.QueryRow(`SELECT payload FROM profiles WHERE id=?`, id).Scan(&payload)
|
||
if errors.Is(err, sql.ErrNoRows) {
|
||
return core.AgentProfile{}, ErrNotFound
|
||
}
|
||
if err != nil {
|
||
return core.AgentProfile{}, err
|
||
}
|
||
return unmarshalJSON[core.AgentProfile](payload)
|
||
}
|
||
|
||
func (s *Store) DeleteProfile(id string) error {
|
||
var builtin int
|
||
if err := s.db.QueryRow(`SELECT builtin FROM profiles WHERE id=?`, id).Scan(&builtin); err != nil {
|
||
return err
|
||
}
|
||
if builtin == 1 {
|
||
return errors.New("store: builtin profile cannot be deleted")
|
||
}
|
||
_, err := s.db.Exec(`DELETE FROM profiles WHERE id=?`, id)
|
||
return err
|
||
}
|
||
|
||
// ---- 设置(scope = user | project:<id>)----
|
||
|
||
func (s *Store) GetSettingsScope(scope string) (map[string]any, error) {
|
||
var payload string
|
||
err := s.db.QueryRow(`SELECT "values" FROM settings WHERE scope=?`, scope).Scan(&payload)
|
||
if errors.Is(err, sql.ErrNoRows) {
|
||
return map[string]any{}, nil
|
||
}
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
out, err := unmarshalJSON[map[string]any](payload)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if out == nil {
|
||
out = map[string]any{}
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func (s *Store) MergeSettingsScope(scope string, values map[string]any) error {
|
||
cur, err := s.GetSettingsScope(scope)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
merged := core.MergeSettings(cur, values)
|
||
_, err = s.db.Exec(`INSERT OR REPLACE INTO settings(scope,"values") VALUES(?,?)`, scope, marshalJSON(merged))
|
||
return err
|
||
}
|
||
|
||
func (s *Store) DeleteSettingsScope(scope string) error {
|
||
_, err := s.db.Exec(`DELETE FROM settings WHERE scope=?`, scope)
|
||
return err
|
||
}
|
||
|
||
// ---- 插件注册表(加载状态;能力注册在 pluginhost 内存态)----
|
||
|
||
func (s *Store) UpsertPlugin(id, dir string, m core.PluginManifest) error {
|
||
_, err := s.db.Exec(`INSERT OR REPLACE INTO plugins(id,dir,manifest,enabled,loaded_at) VALUES(?,?,?,?,?)`,
|
||
id, dir, marshalJSON(m), 1, nowISO())
|
||
return err
|
||
}
|
||
|
||
func (s *Store) ListPlugins() ([]core.PluginManifest, error) {
|
||
rows, err := s.db.Query(`SELECT manifest FROM plugins ORDER BY id`)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer rows.Close()
|
||
var out []core.PluginManifest
|
||
for rows.Next() {
|
||
var payload string
|
||
if err := rows.Scan(&payload); err != nil {
|
||
return nil, err
|
||
}
|
||
m, _ := unmarshalJSON[core.PluginManifest](payload)
|
||
out = append(out, m)
|
||
}
|
||
return out, rows.Err()
|
||
}
|
||
|
||
func (s *Store) RemovePlugin(id string) error {
|
||
_, err := s.db.Exec(`DELETE FROM plugins WHERE id=?`, id)
|
||
return err
|
||
}
|