From a8c8c7919f692314d6ef3feb1f525d4e1fba3079 Mon Sep 17 00:00:00 2001 From: Kibin Shin Date: Fri, 7 Jun 2024 11:21:55 +0900 Subject: [PATCH] Add break tools --- pkg/engine/control.go | 20 ++++++++++++++++++++ pkg/engine/engine.go | 2 ++ pkg/types/tool.go | 5 +++++ 3 files changed, 27 insertions(+) create mode 100644 pkg/engine/control.go diff --git a/pkg/engine/control.go b/pkg/engine/control.go new file mode 100644 index 000000000..7bb45c0a5 --- /dev/null +++ b/pkg/engine/control.go @@ -0,0 +1,20 @@ +package engine + +import ( + "encoding/json" + "fmt" + + "github.com/gptscript-ai/gptscript/pkg/types" +) + +func (e *Engine) runBreak(tool types.Tool, input string) (cmdOut *Return, cmdErr error) { + info, err := json.Marshal(tool) + if err != nil { + return nil, err + } + var dict map[string]interface{} + json.Unmarshal(info, &dict) + dict["input"] = input + info, err = json.Marshal(dict) + return nil, fmt.Errorf("TOOL_BREAK: %s", info) +} diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index c5008b4fe..cf64ef6c2 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -279,6 +279,8 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) { return e.runOpenAPI(tool, input) } else if tool.IsEcho() { return e.runEcho(tool) + } else if tool.IsBreak() { + return e.runBreak(tool, input) } s, err := e.runCommand(ctx, tool, input, ctx.ToolCategory) if err != nil { diff --git a/pkg/types/tool.go b/pkg/types/tool.go index dd89c4711..4de0fde6f 100644 --- a/pkg/types/tool.go +++ b/pkg/types/tool.go @@ -19,6 +19,7 @@ const ( DaemonPrefix = "#!sys.daemon" OpenAPIPrefix = "#!sys.openapi" EchoPrefix = "#!sys.echo" + BreakPrefix = "#!sys.break" CommandPrefix = "#!" ) @@ -732,6 +733,10 @@ func (t Tool) IsEcho() bool { return strings.HasPrefix(t.Instructions, EchoPrefix) } +func (t Tool) IsBreak() bool { + return strings.HasPrefix(t.Instructions, BreakPrefix) +} + func (t Tool) IsHTTP() bool { return strings.HasPrefix(t.Instructions, "#!http://") || strings.HasPrefix(t.Instructions, "#!https://")