Skip to content

Commit bf48210

Browse files
authored
Merge pull request #3 from thedadams/metadata-testing
feat: add prompt metadata field
2 parents 8384832 + 8b240d1 commit bf48210

File tree

3 files changed

+77
-71
lines changed

3 files changed

+77
-71
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module gateway-oauth2
22

3-
go 1.22.4
3+
go 1.23.0
44

55
require (
66
github.com/adrg/xdg v0.4.0
7-
github.com/gptscript-ai/go-gptscript v0.9.2
7+
github.com/gptscript-ai/go-gptscript v0.9.5-rc3.0.20240820135702-14a861da41fa
88
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
99
)
1010

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicb
1111
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
1212
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
1313
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
14-
github.com/gptscript-ai/go-gptscript v0.9.2 h1:mrHCma/AeZqpRU0pvNHYU/TKm86NAi1F1tWe7Ys8zxk=
15-
github.com/gptscript-ai/go-gptscript v0.9.2/go.mod h1:Dh6vYRAiVcyC3ElZIGzTvNF1FxtYwA07BHfSiFKQY7s=
14+
github.com/gptscript-ai/go-gptscript v0.9.5-rc3.0.20240820135702-14a861da41fa h1:4UuWK7OYpThPoT8U9kFqmR26mmcvMc/UfH7O4MajPT8=
15+
github.com/gptscript-ai/go-gptscript v0.9.5-rc3.0.20240820135702-14a861da41fa/go.mod h1:hOfEt4oCiY/kiSHBm8ywnzFvEk/mWhv1h3yjZxui37U=
1616
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
1717
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
1818
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

main.go

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import (
1010
"net/http"
1111
"net/url"
1212
"os"
13-
"os/signal"
14-
"syscall"
13+
"strings"
1514
"time"
1615

1716
"github.com/adrg/xdg"
@@ -152,9 +151,6 @@ func main() {
152151
return
153152
}
154153

155-
sigCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
156-
defer cancel()
157-
158154
state, err := generateString()
159155
if err != nil {
160156
fmt.Printf("failed to generate state: %v\n", err)
@@ -194,86 +190,96 @@ func main() {
194190
os.Exit(1)
195191
}
196192

197-
sysPromptInput := fmt.Sprintf(`{"message":%q,"fields":""}`, fmt.Sprintf("Opening browser to %s. If there is an issue, paste this link into a browser manually.", u.String()))
193+
metadata := map[string]string{
194+
"toolContext": "credential",
195+
"toolDisplayName": fmt.Sprintf("%s%s Integration", strings.ToTitle(integration[:1]), integration[1:]),
196+
"authURL": u.String(),
197+
}
198+
199+
b, err := json.Marshal(metadata)
200+
if err != nil {
201+
fmt.Printf("failed to marshal metadata: %v\n", err)
202+
os.Exit(1)
203+
}
198204

199205
run, err := gs.Run(context.Background(), "sys.prompt", gptscript.Options{
200-
Input: sysPromptInput,
206+
Input: fmt.Sprintf(`{"metadata":%s,"message":%q}`, b, fmt.Sprintf("Opening browser to %s. If there is an issue, paste this link into a browser manually.", u.String())),
201207
})
202208
if err != nil {
203209
fmt.Printf("failed to run sys.prompt: %v\n", err)
204210
os.Exit(1)
205211
}
206212

207-
if _, err = run.Text(); err != nil {
213+
out, err := run.Text()
214+
if err != nil {
208215
fmt.Printf("failed to get text: %v\n", err)
209216
os.Exit(1)
210217
}
211218

212-
// Open the user's browser so that they can authorize the app.
213-
_ = browser.OpenURL(u.String())
219+
var m map[string]string
220+
_ = json.Unmarshal([]byte(out), &m)
221+
222+
if m["handled"] != "true" {
223+
// Open the user's browser so that they can authorize the app.
224+
_ = browser.OpenURL(u.String())
225+
}
214226

215227
t := time.NewTicker(2 * time.Second)
216-
for {
217-
select {
218-
case <-sigCtx.Done():
219-
fmt.Println("canceled")
228+
for range t.C {
229+
// Construct the request to get the token from the gateway.
230+
req, err := http.NewRequest("GET", tokenURL, nil)
231+
if err != nil {
232+
fmt.Printf("failed to create request: %v\n", err)
220233
os.Exit(1)
221-
case <-t.C:
222-
// Construct the request to get the token from the gateway.
223-
req, err := http.NewRequest("GET", tokenURL, nil)
224-
if err != nil {
225-
fmt.Printf("failed to create request: %v\n", err)
226-
os.Exit(1)
227-
}
228-
229-
q = req.URL.Query()
230-
q.Set("state", state)
231-
q.Set("verifier", verifier)
232-
req.URL.RawQuery = q.Encode()
233-
234-
// Send the request to the gateway.
235-
now := time.Now()
236-
resp, err := http.DefaultClient.Do(req)
237-
if err != nil {
238-
fmt.Fprintf(os.Stderr, "failed to send request: %v\n", err)
239-
continue
240-
}
241-
242-
if resp.StatusCode != http.StatusOK {
243-
fmt.Fprintf(os.Stderr, "unexpected status code: %d\n", resp.StatusCode)
244-
continue
245-
}
246-
247-
// Parse the response from the gateway.
248-
var oauthResp oauthResponse
249-
if err := json.NewDecoder(resp.Body).Decode(&oauthResp); err != nil {
250-
fmt.Printf("failed to decode JSON: %v\n", err)
251-
_ = resp.Body.Close()
252-
os.Exit(1)
253-
}
234+
}
235+
236+
q = req.URL.Query()
237+
q.Set("state", state)
238+
q.Set("verifier", verifier)
239+
req.URL.RawQuery = q.Encode()
240+
241+
// Send the request to the gateway.
242+
now := time.Now()
243+
resp, err := http.DefaultClient.Do(req)
244+
if err != nil {
245+
_, _ = fmt.Fprintf(os.Stderr, "failed to send request: %v\n", err)
246+
continue
247+
}
248+
249+
if resp.StatusCode != http.StatusOK {
250+
_, _ = fmt.Fprintf(os.Stderr, "unexpected status code: %d\n", resp.StatusCode)
251+
continue
252+
}
253+
254+
// Parse the response from the gateway.
255+
var oauthResp oauthResponse
256+
if err := json.NewDecoder(resp.Body).Decode(&oauthResp); err != nil {
257+
fmt.Printf("failed to decode JSON: %v\n", err)
254258
_ = resp.Body.Close()
259+
os.Exit(1)
260+
}
261+
_ = resp.Body.Close()
255262

256-
out := cred{
257-
Env: map[string]string{
258-
env: oauthResp.AccessToken,
259-
},
260-
RefreshToken: oauthResp.RefreshToken,
261-
}
262-
263-
if oauthResp.ExpiresIn > 0 {
264-
expiresAt := now.Add(time.Second * time.Duration(oauthResp.ExpiresIn))
265-
out.ExpiresAt = &expiresAt
266-
}
267-
268-
credJSON, err := json.Marshal(out)
269-
if err != nil {
270-
fmt.Printf("failed to marshal credential: %v\n", err)
271-
os.Exit(1)
272-
}
273-
274-
fmt.Print(string(credJSON))
275-
os.Exit(0)
263+
out := cred{
264+
Env: map[string]string{
265+
env: oauthResp.AccessToken,
266+
},
267+
RefreshToken: oauthResp.RefreshToken,
276268
}
269+
270+
if oauthResp.ExpiresIn > 0 {
271+
expiresAt := now.Add(time.Second * time.Duration(oauthResp.ExpiresIn))
272+
out.ExpiresAt = &expiresAt
273+
}
274+
275+
credJSON, err := json.Marshal(out)
276+
if err != nil {
277+
fmt.Printf("failed to marshal credential: %v\n", err)
278+
os.Exit(1)
279+
}
280+
281+
fmt.Print(string(credJSON))
282+
os.Exit(0)
277283
}
278284
}
279285

0 commit comments

Comments
 (0)