feat: expand companion platform, device controls, and memoir workflows
ci / verify (push) Canceled after 0s
ci / verify (push) Canceled after 0s
Add customer and operator login, BLE provisioning, device fleet controls, call memory, memoir generation, and refreshed family and maintenance portals. Include API contracts, migrations, firmware sources, tests, assets, and deployment documentation.
This commit is contained in:
+73
-16
@@ -2,6 +2,7 @@ package saas
|
||||
|
||||
import (
|
||||
"context"
|
||||
"data-terminal-cloud/internal/devicefleet"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
"data-terminal-cloud/internal/audit"
|
||||
"data-terminal-cloud/internal/concurrency"
|
||||
"data-terminal-cloud/internal/database"
|
||||
"data-terminal-cloud/internal/elderagent"
|
||||
"data-terminal-cloud/internal/identity"
|
||||
"data-terminal-cloud/internal/outbox"
|
||||
"data-terminal-cloud/internal/pagination"
|
||||
@@ -63,17 +65,18 @@ type Household struct {
|
||||
}
|
||||
|
||||
type Subject struct {
|
||||
ID string `json:"id"`
|
||||
HouseholdID string `json:"household_id"`
|
||||
PreferredName string `json:"preferred_name"`
|
||||
Language string `json:"language"`
|
||||
Timezone string `json:"timezone"`
|
||||
ProfileData map[string]any `json:"profile_data"`
|
||||
ConversationPreferences map[string]any `json:"conversation_preferences"`
|
||||
Status string `json:"status"`
|
||||
Version int64 `json:"version"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
VoiceAgent *elderagent.Info `json:"voice_agent,omitempty"`
|
||||
ID string `json:"id"`
|
||||
HouseholdID string `json:"household_id"`
|
||||
PreferredName string `json:"preferred_name"`
|
||||
Language string `json:"language"`
|
||||
Timezone string `json:"timezone"`
|
||||
ProfileData map[string]any `json:"profile_data"`
|
||||
ConversationPreferences map[string]any `json:"conversation_preferences"`
|
||||
Status string `json:"status"`
|
||||
Version int64 `json:"version"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Consent struct {
|
||||
@@ -93,11 +96,12 @@ type Consent struct {
|
||||
}
|
||||
|
||||
type CreateSubjectInput struct {
|
||||
PreferredName string `json:"preferred_name"`
|
||||
Language string `json:"language"`
|
||||
Timezone string `json:"timezone"`
|
||||
ProfileData map[string]any `json:"profile_data"`
|
||||
ConversationPreferences map[string]any `json:"conversation_preferences"`
|
||||
InitialConsents InitialConsentSelection `json:"initial_consents,omitempty"`
|
||||
PreferredName string `json:"preferred_name"`
|
||||
Language string `json:"language"`
|
||||
Timezone string `json:"timezone"`
|
||||
ProfileData map[string]any `json:"profile_data"`
|
||||
ConversationPreferences map[string]any `json:"conversation_preferences"`
|
||||
}
|
||||
|
||||
type PatchSubjectInput struct {
|
||||
@@ -120,6 +124,7 @@ type GrantConsentInput struct {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Fleet *devicefleet.Service
|
||||
pool *pgxpool.Pool
|
||||
audit *audit.Repository
|
||||
redis *redis.Client
|
||||
@@ -129,6 +134,19 @@ type Service struct {
|
||||
firmwareStore firmwareObjectStore
|
||||
firmwareURLTTL time.Duration
|
||||
providerMemory bool
|
||||
voiceAgents *elderagent.Service
|
||||
}
|
||||
|
||||
func (s *Service) ConfigureVoiceAgents(agents *elderagent.Service) { s.voiceAgents = agents }
|
||||
|
||||
func (s *Service) EnsureSubjectVoiceAgent(ctx context.Context, principal identity.Principal, publicID string) (elderagent.Info, error) {
|
||||
if _, _, err := resolveSubject(ctx, s.pool, principal, publicID, identity.CapabilitySubjectWrite, false); err != nil {
|
||||
return elderagent.Info{}, err
|
||||
}
|
||||
if s.voiceAgents == nil {
|
||||
return elderagent.Info{}, errors.New("voice agents not configured")
|
||||
}
|
||||
return s.voiceAgents.Ensure(ctx, publicID)
|
||||
}
|
||||
|
||||
func (s *Service) ConfigureProviderMemory(enabled bool) {
|
||||
@@ -294,10 +312,23 @@ func (s *Service) ListSubjects(ctx context.Context, principal identity.Principal
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate subjects: %w", err)
|
||||
}
|
||||
rows.Close()
|
||||
if s.voiceAgents != nil {
|
||||
for n := range result {
|
||||
info, e := s.voiceAgents.Get(ctx, result[n].ID)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
result[n].VoiceAgent = &info
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateSubject(ctx context.Context, principal identity.Principal, householdPublicID, requestID string, input CreateSubjectInput) (Subject, error) {
|
||||
if err := ValidateInitialConsents(input.InitialConsents); err != nil {
|
||||
return Subject{}, err
|
||||
}
|
||||
var result Subject
|
||||
err := database.Transact(ctx, s.pool, func(tx pgx.Tx) error {
|
||||
access, err := resolveHouseholdAccess(ctx, tx, principal, householdPublicID, identity.CapabilitySubjectWrite, true)
|
||||
@@ -319,18 +350,35 @@ func (s *Service) CreateSubject(ctx context.Context, principal identity.Principa
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert subject: %w", err)
|
||||
}
|
||||
if err := InitializeSubjectMemory(ctx, tx, access.tenantID, result.ID, access.userID, requestID, input.InitialConsents); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.audit.Record(ctx, tx, audit.Event{
|
||||
TenantID: strPtr(access.tenantID), ActorType: "user", ActorPublicID: strPtr(access.userPublicID),
|
||||
Action: "subject.create", ResourceType: "subject", ResourceID: strPtr(result.ID),
|
||||
RequestID: requestID, Result: "success", Detail: map[string]any{"household_id": householdPublicID, "version": result.Version},
|
||||
})
|
||||
})
|
||||
if err == nil && s.voiceAgents != nil {
|
||||
info, provisionErr := s.voiceAgents.Ensure(ctx, result.ID)
|
||||
if provisionErr != nil && info.Status == "" {
|
||||
info = elderagent.Info{Status: "FAILED", Message: "老人已保存,智能体暂未就绪,请重试"}
|
||||
}
|
||||
result.VoiceAgent = &info
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *Service) GetSubject(ctx context.Context, principal identity.Principal, subjectPublicID string) (Subject, error) {
|
||||
access, result, err := resolveSubject(ctx, s.pool, principal, subjectPublicID, identity.CapabilitySubjectRead, false)
|
||||
_ = access
|
||||
if err == nil && s.voiceAgents != nil {
|
||||
info, e := s.voiceAgents.Get(ctx, subjectPublicID)
|
||||
if e != nil {
|
||||
return result, e
|
||||
}
|
||||
result.VoiceAgent = &info
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
@@ -385,6 +433,15 @@ func (s *Service) PatchSubject(ctx context.Context, principal identity.Principal
|
||||
RequestID: requestID, Result: "success", Detail: map[string]any{"from_version": expectedVersion, "to_version": result.Version},
|
||||
})
|
||||
})
|
||||
if err == nil && s.voiceAgents != nil {
|
||||
// The database trigger queues this atomically with the profile change.
|
||||
// A provider failure must not undo a successfully saved profile.
|
||||
info, syncErr := s.voiceAgents.Sync(ctx, result.ID)
|
||||
if syncErr != nil && info.Status == "" {
|
||||
info = elderagent.Info{Status: "PENDING", PersonaSyncStatus: "PENDING", Message: "资料已保存,等待同步"}
|
||||
}
|
||||
result.VoiceAgent = &info
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user