From 5df35b4a3df5127be41df17cfffc330079bf496d Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Wed, 26 Mar 2025 16:20:14 -0700 Subject: [PATCH] Enhance: Add ability to do inline prompting to choose oauth/pat Signed-off-by: Daishan Peng --- frame.go | 7 ++++--- gptscript_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/frame.go b/frame.go index 953572f..4b77860 100644 --- a/frame.go +++ b/frame.go @@ -124,9 +124,10 @@ type Prompt struct { } type Field struct { - Name string `json:"name,omitempty"` - Sensitive *bool `json:"sensitive,omitempty"` - Description string `json:"description,omitempty"` + Name string `json:"name,omitempty"` + Sensitive *bool `json:"sensitive,omitempty"` + Description string `json:"description,omitempty"` + Options []string `json:"options,omitempty"` } type Fields []Field diff --git a/gptscript_test.go b/gptscript_test.go index 476492d..213a7a6 100644 --- a/gptscript_test.go +++ b/gptscript_test.go @@ -160,7 +160,7 @@ func TestCancelRun(t *testing.T) { } func TestAbortChatCompletionRun(t *testing.T) { - tool := ToolDef{Instructions: "What is the capital of the united states?"} + tool := ToolDef{Instructions: "Generate a real long essay about the meaning of life."} run, err := g.Evaluate(context.Background(), Options{DisableCache: true, IncludeEvents: true}, tool) if err != nil { @@ -1404,6 +1404,53 @@ func TestPromptWithoutPromptAllowed(t *testing.T) { } } +func TestPromptWithOptions(t *testing.T) { + run, err := g.Run(context.Background(), "sys.prompt", Options{IncludeEvents: true, Prompt: true, Input: `{"fields":[{"name":"Authentication Method","description":"The authentication token for the user","options":["API Key","OAuth"]}]}`}) + if err != nil { + t.Errorf("Error executing tool: %v", err) + } + + // Wait for the prompt event + var promptFrame *PromptFrame + for e := range run.Events() { + if e.Prompt != nil { + if e.Prompt.Type == EventTypePrompt { + promptFrame = e.Prompt + break + } + } + } + + if promptFrame == nil { + t.Fatalf("No prompt call event") + return + } + + if len(promptFrame.Fields) != 1 { + t.Fatalf("Unexpected number of fields: %d", len(promptFrame.Fields)) + } + + if promptFrame.Fields[0].Name != "Authentication Method" { + t.Errorf("Unexpected field: %s", promptFrame.Fields[0].Name) + } + + if promptFrame.Fields[0].Description != "The authentication token for the user" { + t.Errorf("Unexpected description: %s", promptFrame.Fields[0].Description) + } + + if len(promptFrame.Fields[0].Options) != 2 { + t.Fatalf("Unexpected number of options: %d", len(promptFrame.Fields[0].Options)) + } + + if promptFrame.Fields[0].Options[0] != "API Key" { + t.Errorf("Unexpected option: %s", promptFrame.Fields[0].Options[0]) + } + + if promptFrame.Fields[0].Options[1] != "OAuth" { + t.Errorf("Unexpected option: %s", promptFrame.Fields[0].Options[1]) + } +} + func TestGetCommand(t *testing.T) { currentEnvVar := os.Getenv("GPTSCRIPT_BIN") t.Cleanup(func() {