mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
6acdd191c7
commit bcdb026ae43e4f95d0b2c4f9bd440a2df9d6b514 Author: Ran <16112591+chen-ran@users.noreply.github.com> Date: Thu Feb 12 17:10:32 2026 +0800 chore: update DEVELOPMENT.md commit30281742efMerge:ca5c6a15b05f13Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 15:49:17 2026 +0800 merge(github/main): integrate fx dependency injection framework Merge upstream fx refactor and adapt all services to use go.uber.org/fx for dependency injection. Resolve conflicts in main.go, server.go, and service constructors while preserving our domain model changes. - Fix telegram adapter panic on shutdown (double close channel) - Fix feishu adapter processing messages after stop - Increase directory lookup timeout from 2s to 5s commitca5c6a1866Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 15:33:09 2026 +0800 refactor(core): restructure conversation, channel and message domains - Rename chat module to conversation with flow-based architecture - Move channelidentities into channel/identities subpackage - Add channel/route for routing logic - Add message service with event hub - Add MCP providers: container, directory, schedule - Refactor Feishu/Telegram adapters with directory and stream support - Add platform management page and channel badges in web UI - Update database schema for conversations, messages and channel routes - Add @memoh/shared package for cross-package type definitions commit75e2ef0467Merge:d99ba3801cb6c8Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 14:45:49 2026 +0800 merge(github): merge github/main, resolve index.ts URL conflict Keep our defensive absolute-URL check in createAuthFetcher. commitd99ba38b7dMerge:860e20f35ce7d1Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 05:20:18 2026 +0800 merge(github): merge github/main, keep our code and docs/spec commit860e20fe70Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 22:13:27 2026 +0800 docs(docs): add concepts and style guides for VitePress site - Add concepts: identity-and-binding, index (en/zh) - Add style: terminology (en/zh) - Update index and zh/index - Update .vitepress/config.ts commita75fdb8040Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 17:37:16 2026 +0800 refactor(mcp): standardize unified tool gateway on go-sdk Split business executors from federation sources and migrate unified tool/federation transports to the official go-sdk for stricter MCP compliance and safer session lifecycle handling. Add targeted regression tests for accept compatibility, initialization retries, pending cleanup, and include updated swagger artifacts. commit02b33c8e85Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 15:42:21 2026 +0800 refactor(core): finalize user-centric identity and policy cleanup Unify auth and chat identity semantics around user_id, enforce personal-bot owner-only authorization, and remove legacy compatibility branches in integration tests. commit06e8619a37Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 14:47:03 2026 +0800 refactor(core): migrate channel identity and binding across app Align channel identity and bind flow across backend and app-facing layers, including generated swagger artifacts and package lock updates while excluding docs content changes.
338 lines
7.7 KiB
Go
338 lines
7.7 KiB
Go
package subagent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/memohai/memoh/internal/db"
|
|
"github.com/memohai/memoh/internal/db/sqlc"
|
|
)
|
|
|
|
type Service struct {
|
|
queries *sqlc.Queries
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func NewService(log *slog.Logger, queries *sqlc.Queries) *Service {
|
|
return &Service{
|
|
queries: queries,
|
|
logger: log.With(slog.String("service", "subagent")),
|
|
}
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, botID string, req CreateRequest) (Subagent, error) {
|
|
if s.queries == nil {
|
|
return Subagent{}, fmt.Errorf("subagent queries not configured")
|
|
}
|
|
name := strings.TrimSpace(req.Name)
|
|
if name == "" {
|
|
return Subagent{}, fmt.Errorf("name is required")
|
|
}
|
|
description := strings.TrimSpace(req.Description)
|
|
if description == "" {
|
|
return Subagent{}, fmt.Errorf("description is required")
|
|
}
|
|
pgBotID, err := db.ParseUUID(botID)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
messagesPayload, err := marshalMessages(req.Messages)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
metadataPayload, err := marshalMetadata(req.Metadata)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
skillsPayload, err := marshalSkills(req.Skills)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
row, err := s.queries.CreateSubagent(ctx, sqlc.CreateSubagentParams{
|
|
Name: name,
|
|
Description: description,
|
|
BotID: pgBotID,
|
|
Messages: messagesPayload,
|
|
Metadata: metadataPayload,
|
|
Skills: skillsPayload,
|
|
})
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
return toSubagent(row)
|
|
}
|
|
|
|
func (s *Service) Get(ctx context.Context, id string) (Subagent, error) {
|
|
pgID, err := db.ParseUUID(id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
row, err := s.queries.GetSubagentByID(ctx, pgID)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return Subagent{}, fmt.Errorf("subagent not found")
|
|
}
|
|
return Subagent{}, err
|
|
}
|
|
return toSubagent(row)
|
|
}
|
|
|
|
func (s *Service) List(ctx context.Context, botID string) ([]Subagent, error) {
|
|
pgBotID, err := db.ParseUUID(botID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := s.queries.ListSubagentsByBot(ctx, pgBotID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]Subagent, 0, len(rows))
|
|
for _, row := range rows {
|
|
item, err := toSubagent(row)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, id string, req UpdateRequest) (Subagent, error) {
|
|
existing, err := s.Get(ctx, id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
name := existing.Name
|
|
if req.Name != nil {
|
|
name = strings.TrimSpace(*req.Name)
|
|
if name == "" {
|
|
return Subagent{}, fmt.Errorf("name is required")
|
|
}
|
|
}
|
|
description := existing.Description
|
|
if req.Description != nil {
|
|
description = strings.TrimSpace(*req.Description)
|
|
if description == "" {
|
|
return Subagent{}, fmt.Errorf("description is required")
|
|
}
|
|
}
|
|
metadata := existing.Metadata
|
|
if req.Metadata != nil {
|
|
metadata = req.Metadata
|
|
}
|
|
metadataPayload, err := marshalMetadata(metadata)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
pgID, err := db.ParseUUID(id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
row, err := s.queries.UpdateSubagent(ctx, sqlc.UpdateSubagentParams{
|
|
ID: pgID,
|
|
Name: name,
|
|
Description: description,
|
|
Metadata: metadataPayload,
|
|
})
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
return toSubagent(row)
|
|
}
|
|
|
|
func (s *Service) UpdateContext(ctx context.Context, id string, req UpdateContextRequest) (Subagent, error) {
|
|
messagesPayload, err := marshalMessages(req.Messages)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
pgID, err := db.ParseUUID(id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
row, err := s.queries.UpdateSubagentMessages(ctx, sqlc.UpdateSubagentMessagesParams{
|
|
ID: pgID,
|
|
Messages: messagesPayload,
|
|
})
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
return toSubagent(row)
|
|
}
|
|
|
|
func (s *Service) UpdateSkills(ctx context.Context, id string, req UpdateSkillsRequest) (Subagent, error) {
|
|
skillsPayload, err := marshalSkills(req.Skills)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
pgID, err := db.ParseUUID(id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
row, err := s.queries.UpdateSubagentSkills(ctx, sqlc.UpdateSubagentSkillsParams{
|
|
ID: pgID,
|
|
Skills: skillsPayload,
|
|
})
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
return toSubagent(row)
|
|
}
|
|
|
|
func (s *Service) AddSkills(ctx context.Context, id string, req AddSkillsRequest) (Subagent, error) {
|
|
existing, err := s.Get(ctx, id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
merged := mergeSkills(existing.Skills, req.Skills)
|
|
payload, err := marshalSkills(merged)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
pgID, err := db.ParseUUID(id)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
row, err := s.queries.UpdateSubagentSkills(ctx, sqlc.UpdateSubagentSkillsParams{
|
|
ID: pgID,
|
|
Skills: payload,
|
|
})
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
return toSubagent(row)
|
|
}
|
|
|
|
func (s *Service) Delete(ctx context.Context, id string) error {
|
|
pgID, err := db.ParseUUID(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.queries.SoftDeleteSubagent(ctx, pgID)
|
|
}
|
|
|
|
func toSubagent(row sqlc.Subagent) (Subagent, error) {
|
|
messages, err := unmarshalMessages(row.Messages)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
metadata, err := unmarshalMetadata(row.Metadata)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
skills, err := unmarshalSkills(row.Skills)
|
|
if err != nil {
|
|
return Subagent{}, err
|
|
}
|
|
item := Subagent{
|
|
ID: row.ID.String(),
|
|
Name: row.Name,
|
|
Description: row.Description,
|
|
BotID: row.BotID.String(),
|
|
Messages: messages,
|
|
Metadata: metadata,
|
|
Skills: skills,
|
|
Deleted: row.Deleted,
|
|
}
|
|
if row.CreatedAt.Valid {
|
|
item.CreatedAt = row.CreatedAt.Time
|
|
}
|
|
if row.UpdatedAt.Valid {
|
|
item.UpdatedAt = row.UpdatedAt.Time
|
|
}
|
|
if row.DeletedAt.Valid {
|
|
deletedAt := row.DeletedAt.Time
|
|
item.DeletedAt = &deletedAt
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
func marshalMessages(messages []map[string]any) ([]byte, error) {
|
|
if messages == nil {
|
|
messages = []map[string]any{}
|
|
}
|
|
return json.Marshal(messages)
|
|
}
|
|
|
|
func unmarshalMessages(payload []byte) ([]map[string]any, error) {
|
|
if len(payload) == 0 {
|
|
return []map[string]any{}, nil
|
|
}
|
|
var messages []map[string]any
|
|
if err := json.Unmarshal(payload, &messages); err != nil {
|
|
return nil, err
|
|
}
|
|
if messages == nil {
|
|
messages = []map[string]any{}
|
|
}
|
|
return messages, nil
|
|
}
|
|
|
|
func marshalMetadata(metadata map[string]any) ([]byte, error) {
|
|
if metadata == nil {
|
|
metadata = map[string]any{}
|
|
}
|
|
return json.Marshal(metadata)
|
|
}
|
|
|
|
func unmarshalMetadata(payload []byte) (map[string]any, error) {
|
|
if len(payload) == 0 {
|
|
return map[string]any{}, nil
|
|
}
|
|
var metadata map[string]any
|
|
if err := json.Unmarshal(payload, &metadata); err != nil {
|
|
return nil, err
|
|
}
|
|
if metadata == nil {
|
|
metadata = map[string]any{}
|
|
}
|
|
return metadata, nil
|
|
}
|
|
|
|
func marshalSkills(skills []string) ([]byte, error) {
|
|
return json.Marshal(normalizeSkills(skills))
|
|
}
|
|
|
|
func unmarshalSkills(payload []byte) ([]string, error) {
|
|
if len(payload) == 0 {
|
|
return []string{}, nil
|
|
}
|
|
var skills []string
|
|
if err := json.Unmarshal(payload, &skills); err != nil {
|
|
return nil, err
|
|
}
|
|
if skills == nil {
|
|
skills = []string{}
|
|
}
|
|
return skills, nil
|
|
}
|
|
|
|
func normalizeSkills(skills []string) []string {
|
|
seen := map[string]struct{}{}
|
|
normalized := make([]string, 0, len(skills))
|
|
for _, skill := range skills {
|
|
trimmed := strings.TrimSpace(skill)
|
|
if trimmed == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[trimmed]; ok {
|
|
continue
|
|
}
|
|
seen[trimmed] = struct{}{}
|
|
normalized = append(normalized, trimmed)
|
|
}
|
|
return normalized
|
|
}
|
|
|
|
func mergeSkills(existing []string, incoming []string) []string {
|
|
merged := append([]string{}, existing...)
|
|
merged = append(merged, incoming...)
|
|
return normalizeSkills(merged)
|
|
}
|
|
|