Skip to content

Commit

Permalink
Use the relatives records on the IA chat (#4473)
Browse files Browse the repository at this point in the history
nono authored Oct 9, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 7f7498f + 53fc984 commit 194c038
Showing 2 changed files with 45 additions and 5 deletions.
8 changes: 6 additions & 2 deletions model/contact/contacts.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import (
"encoding/json"
"strings"

"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/couchdb/mango"
@@ -256,7 +257,7 @@ func FindByEmail(db prefixer.Prefixer, email string) (*Contact, error) {
}

// CreateMyself creates the myself contact document from the instance settings.
func CreateMyself(db prefixer.Prefixer, settings *couchdb.JSONDoc) (*Contact, error) {
func CreateMyself(inst *instance.Instance, settings *couchdb.JSONDoc) (*Contact, error) {
doc := New()
doc.JSONDoc.M["me"] = true
if name, ok := settings.M["public_name"]; ok {
@@ -267,7 +268,10 @@ func CreateMyself(db prefixer.Prefixer, settings *couchdb.JSONDoc) (*Contact, er
{"address": email, "primary": true},
}
}
if err := couchdb.CreateDoc(db, doc); err != nil {
doc.JSONDoc.M["cozy"] = []map[string]interface{}{
{"url": inst.PageURL("", nil), "primary": true},
}
if err := couchdb.CreateDoc(inst, doc); err != nil {
return nil, err
}
return doc, nil
42 changes: 39 additions & 3 deletions model/rag/chat.go
Original file line number Diff line number Diff line change
@@ -128,10 +128,12 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
return err
}
myself, _ := contact.GetMyself(inst)
relatives, _ := getRelatives(inst, myself)
payload := map[string]interface{}{
"messages": chat.Messages,
"myself": myself,
"stream": true,
"messages": chat.Messages,
"myself": myself,
"relatives": relatives,
"stream": true,
}

res, err := callRAGQuery(inst, payload)
@@ -178,6 +180,40 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
return couchdb.UpdateDoc(inst, &chat)
}

func getRelatives(inst *instance.Instance, myself *contact.Contact) ([]*contact.Contact, error) {
if myself == nil {
return nil, errors.New("no myself contact")
}
var ids []string
rels, _ := myself.Get("relationships").(map[string]interface{})
if len(rels) == 0 {
return nil, nil
}
related, _ := rels["related"].(map[string]interface{})
if len(related) == 0 {
return nil, nil
}
data, _ := related["data"].([]interface{})
for _, item := range data {
item, _ := item.(map[string]interface{})
if len(item) > 0 {
id, _ := item["_id"].(string)
if len(id) > 0 {
ids = append(ids, id)
}
}
}
if len(ids) == 0 {
return nil, nil
}
var relatives []*contact.Contact
req := &couchdb.AllDocsRequest{Keys: ids}
if err := couchdb.GetAllDocs(inst, consts.Contacts, req, &relatives); err != nil {
return nil, err
}
return relatives, nil
}

func callRAGQuery(inst *instance.Instance, payload map[string]interface{}) (*http.Response, error) {
ragServer := inst.RAGServer()
if ragServer.URL == "" {

0 comments on commit 194c038

Please sign in to comment.