yao/neo/neo.go
Max 96d2f22e77 Update dependencies in go.mod and go.sum; refactor file handling in neo package
- Added the jsonrepair package to go.mod for enhanced JSON handling capabilities.
- Removed unused dependencies from go.mod and updated existing ones for better performance.
- Refactored file upload and download methods in the neo package to return nil values, indicating a temporary placeholder for future implementation.
- Updated the Message struct to use the new attachment package for better organization of file attachments.
2025-05-31 10:07:19 +08:00

102 lines
2.2 KiB
Go

package neo
import (
"fmt"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/yaoapp/yao/neo/assistant"
chatctx "github.com/yaoapp/yao/neo/context"
)
// Answer reply the message
func (neo *DSL) Answer(ctx chatctx.Context, question string, c *gin.Context) error {
var err error
var ast assistant.API = Neo.Assistant
if ctx.AssistantID != "" {
ast, err = neo.Select(ctx.AssistantID)
if err != nil {
return err
}
}
_, err = ast.Execute(c, ctx, question, nil)
return err
}
// Select select an assistant
func (neo *DSL) Select(id string) (assistant.API, error) {
if id == "" {
return Neo.Assistant, nil
}
return assistant.Get(id)
}
// Upload upload a file
func (neo *DSL) Upload(ctx chatctx.Context, c *gin.Context) (*assistant.File, error) {
// Get the file
tmpfile, err := c.FormFile("file")
if err != nil {
return nil, err
}
reader, err := tmpfile.Open()
if err != nil {
return nil, err
}
defer func() {
reader.Close()
os.Remove(tmpfile.Filename)
}()
// Get option from form data option_xxx
option := map[string]interface{}{}
for key := range c.Request.Form {
if strings.HasPrefix(key, "option_") {
option[strings.TrimPrefix(key, "option_")] = c.PostForm(key)
}
}
// Default use the assistant in context
ast := neo.Assistant
if ctx.ChatID == "" {
if ctx.AssistantID == "" {
return nil, fmt.Errorf("assistant_id is required")
}
ast, err = neo.Select(ctx.AssistantID)
if err != nil {
return nil, err
}
}
fmt.Println(ast)
return nil, nil
// return ast.Upload(ctx, tmpfile, reader, option)
}
// Download downloads a file
func (neo *DSL) Download(ctx chatctx.Context, c *gin.Context) (*assistant.FileResponse, error) {
// Get file_id from query string
fileID := c.Query("file_id")
if fileID == "" {
return nil, fmt.Errorf("file_id is required")
}
// Get assistant_id from context or query
// res, err := neo.HookCreate(ctx, []map[string]interface{}{}, c)
// if err != nil {
// return nil, err
// }
// Select Assistant
ast, err := neo.Select(neo.Use.Default)
if err != nil {
return nil, err
}
// Download file using the assistant
// return ast.Download(ctx.Context, fileID)
fmt.Println(ast)
return nil, nil
}