2024-06-12 02:35:07 -06:00
|
|
|
package api
|
2024-02-21 21:55:38 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
type ToolSpec struct {
|
2024-02-21 21:55:38 -07:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Parameters []ToolParameter
|
2024-06-12 02:35:07 -06:00
|
|
|
Impl func(*ToolSpec, map[string]interface{}) (string, error)
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type ToolParameter struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"` // "string", "integer", "boolean"
|
|
|
|
Required bool `json:"required"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Enum []string `json:"enum,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ToolCall struct {
|
2024-03-26 01:59:39 -06:00
|
|
|
ID string `json:"id" yaml:"-"`
|
|
|
|
Name string `json:"name" yaml:"tool"`
|
|
|
|
Parameters map[string]interface{} `json:"parameters" yaml:"parameters"`
|
2024-02-21 21:55:38 -07:00
|
|
|
}
|
|
|
|
|
2024-06-12 02:35:07 -06:00
|
|
|
type ToolResult struct {
|
|
|
|
ToolCallID string `json:"toolCallID" yaml:"-"`
|
|
|
|
ToolName string `json:"toolName,omitempty" yaml:"tool"`
|
|
|
|
Result string `json:"result,omitempty" yaml:"result"`
|
|
|
|
}
|
|
|
|
|
2024-02-21 21:55:38 -07:00
|
|
|
type ToolCalls []ToolCall
|
|
|
|
|
|
|
|
func (tc *ToolCalls) Scan(value any) (err error) {
|
|
|
|
s := value.(string)
|
|
|
|
if value == nil || s == "" {
|
|
|
|
*tc = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(s), tc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc ToolCalls) Value() (driver.Value, error) {
|
|
|
|
if len(tc) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.Marshal(tc)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Could not marshal ToolCalls to JSON: %v\n", err)
|
|
|
|
}
|
|
|
|
return string(jsonBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ToolResults []ToolResult
|
|
|
|
|
|
|
|
func (tr *ToolResults) Scan(value any) (err error) {
|
|
|
|
s := value.(string)
|
|
|
|
if value == nil || s == "" {
|
|
|
|
*tr = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(s), tr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr ToolResults) Value() (driver.Value, error) {
|
|
|
|
if len(tr) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
jsonBytes, err := json.Marshal([]ToolResult(tr))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Could not marshal ToolResults to JSON: %v\n", err)
|
|
|
|
}
|
|
|
|
return string(jsonBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CallResult struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Result any `json:"result,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r CallResult) ToJson() (string, error) {
|
|
|
|
if r.Message == "" {
|
|
|
|
// When message not supplied, assume success
|
|
|
|
r.Message = "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Could not marshal CallResult to JSON: %v\n", err)
|
|
|
|
}
|
|
|
|
return string(jsonBytes), nil
|
|
|
|
}
|