Skip to content

Commit 8b240d1

Browse files
committed
feat: add prompt metadata field
Signed-off-by: Donnie Adams <[email protected]>
1 parent 20a19b9 commit 8b240d1

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"
@@ -148,9 +147,6 @@ func main() {
148147
return
149148
}
150149

151-
sigCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
152-
defer cancel()
153-
154150
state, err := generateString()
155151
if err != nil {
156152
fmt.Printf("failed to generate state: %v\n", err)
@@ -187,86 +183,96 @@ func main() {
187183
os.Exit(1)
188184
}
189185

190-
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()))
186+
metadata := map[string]string{
187+
"toolContext": "credential",
188+
"toolDisplayName": fmt.Sprintf("%s%s Integration", strings.ToTitle(integration[:1]), integration[1:]),
189+
"authURL": u.String(),
190+
}
191+
192+
b, err := json.Marshal(metadata)
193+
if err != nil {
194+
fmt.Printf("failed to marshal metadata: %v\n", err)
195+
os.Exit(1)
196+
}
191197

192198
run, err := gs.Run(context.Background(), "sys.prompt", gptscript.Options{
193-
Input: sysPromptInput,
199+
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())),
194200
})
195201
if err != nil {
196202
fmt.Printf("failed to run sys.prompt: %v\n", err)
197203
os.Exit(1)
198204
}
199205

200-
if _, err = run.Text(); err != nil {
206+
out, err := run.Text()
207+
if err != nil {
201208
fmt.Printf("failed to get text: %v\n", err)
202209
os.Exit(1)
203210
}
204211

205-
// Open the user's browser so that they can authorize the app.
206-
_ = browser.OpenURL(u.String())
212+
var m map[string]string
213+
_ = json.Unmarshal([]byte(out), &m)
214+
215+
if m["handled"] != "true" {
216+
// Open the user's browser so that they can authorize the app.
217+
_ = browser.OpenURL(u.String())
218+
}
207219

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

249-
out := cred{
250-
Env: map[string]string{
251-
env: oauthResp.AccessToken,
252-
},
253-
RefreshToken: oauthResp.RefreshToken,
254-
}
255-
256-
if oauthResp.ExpiresIn > 0 {
257-
expiresAt := now.Add(time.Second * time.Duration(oauthResp.ExpiresIn))
258-
out.ExpiresAt = &expiresAt
259-
}
260-
261-
credJSON, err := json.Marshal(out)
262-
if err != nil {
263-
fmt.Printf("failed to marshal credential: %v\n", err)
264-
os.Exit(1)
265-
}
266-
267-
fmt.Print(string(credJSON))
268-
os.Exit(0)
256+
out := cred{
257+
Env: map[string]string{
258+
env: oauthResp.AccessToken,
259+
},
260+
RefreshToken: oauthResp.RefreshToken,
269261
}
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)
270276
}
271277
}
272278

0 commit comments

Comments
 (0)