addObservations appends new observations to existing entities. It returns the new observations that were actually added.
(observations []Observation)
| 235 | // addObservations appends new observations to existing entities. |
| 236 | // It returns the new observations that were actually added. |
| 237 | func (k knowledgeBase) addObservations(observations []Observation) ([]Observation, error) { |
| 238 | graph, err := k.loadGraph() |
| 239 | if err != nil { |
| 240 | return nil, err |
| 241 | } |
| 242 | |
| 243 | var results []Observation |
| 244 | |
| 245 | for _, obs := range observations { |
| 246 | entityIndex := slices.IndexFunc(graph.Entities, func(e Entity) bool { return e.Name == obs.EntityName }) |
| 247 | if entityIndex == -1 { |
| 248 | return nil, fmt.Errorf("entity with name %s not found", obs.EntityName) |
| 249 | } |
| 250 | |
| 251 | var newObservations []string |
| 252 | for _, content := range obs.Contents { |
| 253 | if !slices.Contains(graph.Entities[entityIndex].Observations, content) { |
| 254 | newObservations = append(newObservations, content) |
| 255 | graph.Entities[entityIndex].Observations = append(graph.Entities[entityIndex].Observations, content) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | results = append(results, Observation{ |
| 260 | EntityName: obs.EntityName, |
| 261 | Contents: newObservations, |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | if err := k.saveGraph(graph); err != nil { |
| 266 | return nil, err |
| 267 | } |
| 268 | |
| 269 | return results, nil |
| 270 | } |
| 271 | |
| 272 | // deleteEntities removes entities and their associated relations. |
| 273 | func (k knowledgeBase) deleteEntities(entityNames []string) error { |