@@ -1,15 +1,21 @@
|
||||
package calculator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
agents "aichat/agenttool"
|
||||
|
||||
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -17,11 +23,95 @@ const (
|
||||
ActivationPrompt = "执行简单、确定性的数学四则运算。当用户询问加减乘除、括号表达式、小数运算或需要准确计算表达式结果时,应直接调用此工具;不用于代数推导、方程求解、统计分析或复杂数学证明。"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Enabled bool `yaml:"enabled" json:"enabled"`
|
||||
ActivationPrompt string `yaml:"activation_prompt" json:"activation_prompt"`
|
||||
}
|
||||
|
||||
type ToolArgs struct {
|
||||
Expression string `json:"expression"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type LoadedTool struct {
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
func NewLoadedTool(cfg *Config) *LoadedTool {
|
||||
if cfg == nil {
|
||||
defaultCfg := defaultConfig()
|
||||
cfg = &defaultCfg
|
||||
}
|
||||
return &LoadedTool{cfg: cfg}
|
||||
}
|
||||
|
||||
func init() {
|
||||
agents.Register(agents.Descriptor{Name: ToolName, Load: func(path string, options agents.LoadOptions) (agents.LoadedTool, error) {
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewLoadedTool(cfg), nil
|
||||
}})
|
||||
}
|
||||
|
||||
func defaultConfig() Config {
|
||||
return Config{Enabled: true, ActivationPrompt: ActivationPrompt}
|
||||
}
|
||||
|
||||
func LoadConfig(path string) (*Config, error) {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("检查计算器工具配置失败: %w", err)
|
||||
}
|
||||
cfg := defaultConfig()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
return nil, fmt.Errorf("创建计算器工具配置目录失败: %w", err)
|
||||
}
|
||||
data, err := yaml.Marshal(&cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成计算器工具配置失败: %w", err)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0644); err != nil {
|
||||
return nil, fmt.Errorf("写入计算器工具配置失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取计算器工具配置失败: %w", err)
|
||||
}
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("解析计算器工具配置失败: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(cfg.ActivationPrompt) == "" {
|
||||
cfg.ActivationPrompt = ActivationPrompt
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (t *LoadedTool) Name() string { return ToolName }
|
||||
|
||||
func (t *LoadedTool) Enabled() bool { return t != nil && t.cfg != nil && t.cfg.Enabled }
|
||||
|
||||
func (t *LoadedTool) ToolDefinition(description string) *model.Tool {
|
||||
if strings.TrimSpace(description) == "" && t != nil && t.cfg != nil {
|
||||
description = t.cfg.ActivationPrompt
|
||||
}
|
||||
return ToolDefinition(description)
|
||||
}
|
||||
|
||||
func (t *LoadedTool) Execute(ctx context.Context, args string, runtime agents.Runtime) (string, error) {
|
||||
result, err := ExecuteTool(args)
|
||||
if err == nil && runtime.Emit != nil {
|
||||
runtime.Emit(agents.Frame{Type: "trace", Tool: ToolName, Stage: "calculate", Status: "success", Message: "四则运算完成"})
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (t *LoadedTool) RawState() any { return nil }
|
||||
|
||||
func ToolDefinition(description string) *model.Tool {
|
||||
description = strings.TrimSpace(description)
|
||||
if description == "" {
|
||||
|
||||
Reference in New Issue
Block a user