Merge pull request #877 from trheyi/main
Add Assets method to assistant for retrieving and processing asset files
This commit is contained in:
commit
e536d6ebcc
3 changed files with 80 additions and 8 deletions
|
|
@ -242,6 +242,7 @@ func (next *NextAction) Execute(c *gin.Context, ctx chatctx.Context, contents *c
|
||||||
|
|
||||||
// Create a new Text
|
// Create a new Text
|
||||||
// Send loading message and mark as new
|
// Send loading message and mark as new
|
||||||
|
if !ctx.Silent {
|
||||||
msg := chatMessage.New().Map(map[string]interface{}{
|
msg := chatMessage.New().Map(map[string]interface{}{
|
||||||
"new": true,
|
"new": true,
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
|
|
@ -250,6 +251,7 @@ func (next *NextAction) Execute(c *gin.Context, ctx chatctx.Context, contents *c
|
||||||
})
|
})
|
||||||
msg.Assistant(assistant.ID, assistant.Name, assistant.Avatar)
|
msg.Assistant(assistant.ID, assistant.Name, assistant.Avatar)
|
||||||
msg.Write(c.Writer)
|
msg.Write(c.Writer)
|
||||||
|
}
|
||||||
newContents := chatMessage.NewContents()
|
newContents := chatMessage.NewContents()
|
||||||
|
|
||||||
// Update the context id
|
// Update the context id
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,15 @@ package assistant
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/yaoapp/gou/fs"
|
||||||
"github.com/yaoapp/gou/rag/driver"
|
"github.com/yaoapp/gou/rag/driver"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
|
sui "github.com/yaoapp/yao/sui/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Save save the assistant
|
// Save save the assistant
|
||||||
|
|
@ -156,6 +159,28 @@ func (ast *Assistant) Validate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Assets get the assets content
|
||||||
|
func (ast *Assistant) Assets(name string, data sui.Data) (string, error) {
|
||||||
|
|
||||||
|
app, err := fs.Get("app")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
root := path.Join(ast.Path, "assets", name)
|
||||||
|
raw, err := app.ReadFile(root)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if data != nil {
|
||||||
|
content, _ := data.Replace(string(raw))
|
||||||
|
return content, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(raw), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Clone creates a deep copy of the assistant
|
// Clone creates a deep copy of the assistant
|
||||||
func (ast *Assistant) Clone() *Assistant {
|
func (ast *Assistant) Clone() *Assistant {
|
||||||
if ast == nil {
|
if ast == nil {
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,51 @@ func (ast *Assistant) InitObject(v8ctx *v8.Context, c *gin.Context, context chat
|
||||||
v8ctx.WithFunction("Plan", jsPlan) // Create a new plan object
|
v8ctx.WithFunction("Plan", jsPlan) // Create a new plan object
|
||||||
v8ctx.WithFunction("Send", jsSend)
|
v8ctx.WithFunction("Send", jsSend)
|
||||||
v8ctx.WithFunction("Call", jsCall)
|
v8ctx.WithFunction("Call", jsCall)
|
||||||
|
v8ctx.WithFunction("Assets", jsAssets)
|
||||||
|
}
|
||||||
|
|
||||||
|
// jsAssets function, get the assets content
|
||||||
|
func jsAssets(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||||
|
|
||||||
|
global, err := global(info)
|
||||||
|
if err != nil {
|
||||||
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the message
|
||||||
|
args := info.Args()
|
||||||
|
if len(args) < 1 {
|
||||||
|
return bridge.JsException(info.Context(), "Assets requires at least one argument")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the name
|
||||||
|
name := args[0].String()
|
||||||
|
|
||||||
|
data := map[string]interface{}{}
|
||||||
|
if len(args) > 1 {
|
||||||
|
raw, err := bridge.GoValue(args[1], info.Context())
|
||||||
|
if err != nil {
|
||||||
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := raw.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return bridge.JsException(info.Context(), "Assets requires a map")
|
||||||
|
}
|
||||||
|
data = v
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := global.Assistant.Assets(name, data)
|
||||||
|
if err != nil {
|
||||||
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
jsContent, err := bridge.JsValue(info.Context(), content)
|
||||||
|
if err != nil {
|
||||||
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsContent
|
||||||
}
|
}
|
||||||
|
|
||||||
// jsSend function, send a message to the http stream connection
|
// jsSend function, send a message to the http stream connection
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue