feat: enable S2S with native Viking memory
This commit is contained in:
@@ -27,6 +27,15 @@ type Service struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// NativeAccess contains only non-secret values that Volcano needs to query
|
||||
// this user's memory. The Viking API key is never returned to the device.
|
||||
type NativeAccess struct {
|
||||
SubjectID, UserID, CollectionName, AssistantID string
|
||||
MemoryTypes []string
|
||||
Limit int
|
||||
TransitionWords string
|
||||
}
|
||||
|
||||
func NewService(pool *pgxpool.Pool, client *Client) *Service {
|
||||
return &Service{pool: pool, client: client}
|
||||
}
|
||||
@@ -71,6 +80,33 @@ func (s *Service) PrepareForDevice(ctx context.Context, tenant, device string) (
|
||||
}
|
||||
return subject, strings.Join(parts, "\n"), nil
|
||||
}
|
||||
|
||||
func (s *Service) NativeAccessForDevice(ctx context.Context, tenant, device string) (*NativeAccess, error) {
|
||||
if strings.TrimSpace(s.client.options.CollectionName) == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var subject string
|
||||
err := s.pool.QueryRow(ctx, `SELECT b.subject_id::text FROM device_binding b JOIN device d ON d.id=b.device_id AND d.tenant_id=b.tenant_id WHERE b.device_id=$1::uuid AND b.tenant_id=$2::uuid AND b.unbound_at IS NULL AND d.status='registered'`, device, tenant).Scan(&subject)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ok, err := s.Eligible(ctx, tenant, subject)
|
||||
if err != nil || !ok {
|
||||
return nil, err
|
||||
}
|
||||
memoryTypes := []string{s.client.options.EventType}
|
||||
if s.client.options.ProfileType != "" && s.client.options.ProfileType != s.client.options.EventType {
|
||||
memoryTypes = append(memoryTypes, s.client.options.ProfileType)
|
||||
}
|
||||
return &NativeAccess{
|
||||
SubjectID: subject, UserID: UserID(tenant, subject), CollectionName: s.client.options.CollectionName,
|
||||
AssistantID: AssistantID, MemoryTypes: memoryTypes, Limit: 5,
|
||||
TransitionWords: "可参考的用户历史记忆如下,请自然承接;若与用户最新说法冲突,以最新说法为准:",
|
||||
}, nil
|
||||
}
|
||||
func (s *Service) Enqueue(ctx context.Context, event outbox.Event) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user