Skip to content

feat: add prompt callback URL #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builtin

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -774,7 +775,37 @@ func SysDownload(ctx context.Context, env []string, input string) (_ string, err
return params.Location, nil
}

func SysPrompt(_ context.Context, _ []string, input string) (_ string, err error) {
func sysPromptHTTP(ctx context.Context, url, message string, fields []string, sensitive bool) (_ string, err error) {
data, err := json.Marshal(map[string]any{
"message": message,
"fields": fields,
"sensitive": sensitive,
})
if err != nil {
return "", err
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(data))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
resp.Body.Close()

if resp.StatusCode != 200 {
return "", fmt.Errorf("invalid status code [%d], expected 200", resp.StatusCode)
}

data, err = io.ReadAll(resp.Body)
return string(data), err
}

func SysPrompt(ctx context.Context, envs []string, input string) (_ string, err error) {
var params struct {
Message string `json:"message,omitempty"`
Fields string `json:"fields,omitempty"`
Expand All @@ -784,6 +815,12 @@ func SysPrompt(_ context.Context, _ []string, input string) (_ string, err error
return "", err
}

for _, env := range envs {
if url, ok := strings.CutPrefix(env, "GPTSCRIPT_PROMPT_URL="); ok {
return sysPromptHTTP(ctx, url, params.Message, strings.Split(params.Fields, ","), params.Sensitive == "true")
}
}

if params.Message != "" {
_, _ = fmt.Fprintln(os.Stderr, params.Message)
}
Expand Down