Skip to content

Commit 194c038

Browse files
authored
Use the relatives records on the IA chat (#4473)
2 parents 7f7498f + 53fc984 commit 194c038

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

model/contact/contacts.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"strings"
88

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

258259
// CreateMyself creates the myself contact document from the instance settings.
259-
func CreateMyself(db prefixer.Prefixer, settings *couchdb.JSONDoc) (*Contact, error) {
260+
func CreateMyself(inst *instance.Instance, settings *couchdb.JSONDoc) (*Contact, error) {
260261
doc := New()
261262
doc.JSONDoc.M["me"] = true
262263
if name, ok := settings.M["public_name"]; ok {
@@ -267,7 +268,10 @@ func CreateMyself(db prefixer.Prefixer, settings *couchdb.JSONDoc) (*Contact, er
267268
{"address": email, "primary": true},
268269
}
269270
}
270-
if err := couchdb.CreateDoc(db, doc); err != nil {
271+
doc.JSONDoc.M["cozy"] = []map[string]interface{}{
272+
{"url": inst.PageURL("", nil), "primary": true},
273+
}
274+
if err := couchdb.CreateDoc(inst, doc); err != nil {
271275
return nil, err
272276
}
273277
return doc, nil

model/rag/chat.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) er
128128
return err
129129
}
130130
myself, _ := contact.GetMyself(inst)
131+
relatives, _ := getRelatives(inst, myself)
131132
payload := map[string]interface{}{
132-
"messages": chat.Messages,
133-
"myself": myself,
134-
"stream": true,
133+
"messages": chat.Messages,
134+
"myself": myself,
135+
"relatives": relatives,
136+
"stream": true,
135137
}
136138

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

183+
func getRelatives(inst *instance.Instance, myself *contact.Contact) ([]*contact.Contact, error) {
184+
if myself == nil {
185+
return nil, errors.New("no myself contact")
186+
}
187+
var ids []string
188+
rels, _ := myself.Get("relationships").(map[string]interface{})
189+
if len(rels) == 0 {
190+
return nil, nil
191+
}
192+
related, _ := rels["related"].(map[string]interface{})
193+
if len(related) == 0 {
194+
return nil, nil
195+
}
196+
data, _ := related["data"].([]interface{})
197+
for _, item := range data {
198+
item, _ := item.(map[string]interface{})
199+
if len(item) > 0 {
200+
id, _ := item["_id"].(string)
201+
if len(id) > 0 {
202+
ids = append(ids, id)
203+
}
204+
}
205+
}
206+
if len(ids) == 0 {
207+
return nil, nil
208+
}
209+
var relatives []*contact.Contact
210+
req := &couchdb.AllDocsRequest{Keys: ids}
211+
if err := couchdb.GetAllDocs(inst, consts.Contacts, req, &relatives); err != nil {
212+
return nil, err
213+
}
214+
return relatives, nil
215+
}
216+
181217
func callRAGQuery(inst *instance.Instance, payload map[string]interface{}) (*http.Response, error) {
182218
ragServer := inst.RAGServer()
183219
if ragServer.URL == "" {

0 commit comments

Comments
 (0)