Refactor assistant loading and initialization in Neo API
- Simplified the assistant loading process by removing the asynchronous query for the assistant list and replacing it with a direct call to retrieve the default assistant. - Introduced a new method, defaultAssistant, to streamline the retrieval of the default assistant based on the current configuration. - Enhanced the LoadStore function to support loading assistants from a specified path, improving flexibility in assistant management. - Updated the overall structure for better readability and maintainability, ensuring a more efficient assistant initialization process.
This commit is contained in:
parent
8d320f0e41
commit
fd5d701a23
4 changed files with 68 additions and 29 deletions
38
neo/assistant/api.go
Normal file
38
neo/assistant/api.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package assistant
|
||||
|
||||
// Get get the assistant by id
|
||||
func Get(id string) (*Assistant, error) {
|
||||
return LoadStore(id)
|
||||
}
|
||||
|
||||
// GetByConnector get the assistant by connector
|
||||
func GetByConnector(connector string, name string) (*Assistant, error) {
|
||||
id := "connector:" + connector
|
||||
|
||||
assistant, exists := loaded.Get(id)
|
||||
if exists {
|
||||
return assistant, nil
|
||||
}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"assistant_id": id,
|
||||
"connector": connector,
|
||||
"description": "Default assistant for " + connector,
|
||||
"name": name,
|
||||
"type": "assistant",
|
||||
}
|
||||
|
||||
assistant, err := loadMap(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
}
|
||||
loaded.Put(assistant)
|
||||
return assistant, nil
|
||||
}
|
||||
|
||||
// Init init the assistant
|
||||
// Choose the connector and initialize the assistant
|
||||
func (ast *Assistant) initialize() error {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -115,6 +115,17 @@ func LoadStore(id string) (*Assistant, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Load from path
|
||||
if data["path"] != nil {
|
||||
assistant, err = LoadPath(data["path"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
loaded.Put(assistant)
|
||||
return assistant, nil
|
||||
}
|
||||
|
||||
// Load from store
|
||||
assistant, err = loadMap(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
34
neo/load.go
34
neo/load.go
|
|
@ -1,10 +1,7 @@
|
|||
package neo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/yaoapp/gou/application"
|
||||
"github.com/yaoapp/yao/config"
|
||||
|
|
@ -58,32 +55,11 @@ func Load(cfg config.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Query Assistant List
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
listDone := make(chan error, 1)
|
||||
go func() {
|
||||
list, err := Neo.HookAssistants(ctx, assistant.QueryParam{Limit: 100})
|
||||
Neo.updateAssistantList(list)
|
||||
listDone <- err
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-listDone:
|
||||
if err != nil {
|
||||
return fmt.Errorf("Neo assistant list failed: %w", err)
|
||||
}
|
||||
|
||||
// Create Default Assistant
|
||||
Neo.Assistant, err = Neo.createDefaultAssistant()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("Neo assistant list timeout: %w", ctx.Err())
|
||||
defaultAssistant, err := Neo.defaultAssistant()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Neo.Assistant = defaultAssistant.API
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
14
neo/neo.go
14
neo/neo.go
|
|
@ -346,6 +346,20 @@ func (neo *DSL) chat(ast assistant.API, ctx Context, messages []map[string]inter
|
|||
}
|
||||
}
|
||||
|
||||
// defaultAssistant get the default assistant
|
||||
func (neo *DSL) defaultAssistant() (*assistant.Assistant, error) {
|
||||
if neo.Use != "" {
|
||||
return assistant.Get(neo.Use)
|
||||
}
|
||||
|
||||
name := neo.Name
|
||||
if name == "" {
|
||||
name = "Neo"
|
||||
}
|
||||
|
||||
return assistant.GetByConnector(neo.Connector, name)
|
||||
}
|
||||
|
||||
// updateAssistantList update the assistant list
|
||||
func (neo *DSL) updateAssistantList(list []assistant.Assistant) {
|
||||
lock.Lock()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue