Skip to content

Commit 2560982

Browse files
committed
in deletecredential, return whether the credential was found
Signed-off-by: Grant Linville <[email protected]>
1 parent ebc128c commit 2560982

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

gptscript.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOpt
161161
disableCache = disableCache || opt.DisableCache
162162
}
163163

164-
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
164+
out, _, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName, "disableCache": disableCache})
165165
if err != nil {
166166
return nil, err
167167
}
@@ -180,7 +180,7 @@ func (g *GPTScript) Parse(ctx context.Context, fileName string, opts ...ParseOpt
180180

181181
// ParseContent will parse the given string into a tool.
182182
func (g *GPTScript) ParseContent(ctx context.Context, toolDef string) ([]Node, error) {
183-
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
183+
out, _, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
184184
if err != nil {
185185
return nil, err
186186
}
@@ -203,7 +203,7 @@ func (g *GPTScript) Fmt(ctx context.Context, nodes []Node) (string, error) {
203203
node.TextNode.combine()
204204
}
205205

206-
out, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
206+
out, _, err := g.runBasicCommand(ctx, "fmt", Document{Nodes: nodes})
207207
if err != nil {
208208
return "", err
209209
}
@@ -241,7 +241,7 @@ func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...Lo
241241
}
242242
}
243243

244-
out, err := g.runBasicCommand(ctx, "load", payload)
244+
out, _, err := g.runBasicCommand(ctx, "load", payload)
245245
if err != nil {
246246
return nil, err
247247
}
@@ -260,7 +260,7 @@ func (g *GPTScript) load(ctx context.Context, payload map[string]any, opts ...Lo
260260

261261
// Version will return the output of `gptscript --version`
262262
func (g *GPTScript) Version(ctx context.Context) (string, error) {
263-
out, err := g.runBasicCommand(ctx, "version", nil)
263+
out, _, err := g.runBasicCommand(ctx, "version", nil)
264264
if err != nil {
265265
return "", err
266266
}
@@ -285,7 +285,7 @@ func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) (
285285
o.Providers = append(o.Providers, g.globalOpts.DefaultModelProvider)
286286
}
287287

288-
out, err := g.runBasicCommand(ctx, "list-models", map[string]any{
288+
out, _, err := g.runBasicCommand(ctx, "list-models", map[string]any{
289289
"providers": o.Providers,
290290
"env": g.globalOpts.Env,
291291
"credentialOverrides": o.CredentialOverrides,
@@ -298,12 +298,12 @@ func (g *GPTScript) ListModels(ctx context.Context, opts ...ListModelsOptions) (
298298
}
299299

300300
func (g *GPTScript) Confirm(ctx context.Context, resp AuthResponse) error {
301-
_, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
301+
_, _, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
302302
return err
303303
}
304304

305305
func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) error {
306-
_, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
306+
_, _, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
307307
return err
308308
}
309309

@@ -315,7 +315,7 @@ func (g *GPTScript) ListCredentials(ctx context.Context, credCtxs []string, allC
315315
req.Context = credCtxs
316316
}
317317

318-
out, err := g.runBasicCommand(ctx, "credentials", req)
318+
out, _, err := g.runBasicCommand(ctx, "credentials", req)
319319
if err != nil {
320320
return nil, err
321321
}
@@ -333,12 +333,12 @@ func (g *GPTScript) CreateCredential(ctx context.Context, cred Credential) error
333333
return fmt.Errorf("failed to marshal credential: %w", err)
334334
}
335335

336-
_, err = g.runBasicCommand(ctx, "credentials/create", CredentialRequest{Content: string(credJSON)})
336+
_, _, err = g.runBasicCommand(ctx, "credentials/create", CredentialRequest{Content: string(credJSON)})
337337
return err
338338
}
339339

340340
func (g *GPTScript) RevealCredential(ctx context.Context, credCtxs []string, name string) (Credential, error) {
341-
out, err := g.runBasicCommand(ctx, "credentials/reveal", CredentialRequest{
341+
out, _, err := g.runBasicCommand(ctx, "credentials/reveal", CredentialRequest{
342342
Context: credCtxs,
343343
Name: name,
344344
})
@@ -353,15 +353,25 @@ func (g *GPTScript) RevealCredential(ctx context.Context, credCtxs []string, nam
353353
return cred, nil
354354
}
355355

356-
func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string) error {
357-
_, err := g.runBasicCommand(ctx, "credentials/delete", CredentialRequest{
356+
// DeleteCredential will delete the credential with the given name in the given context.
357+
// A return value of false, nil indicates that the credential was not found.
358+
// false, non-nil error indicates a different error when trying to delete.
359+
// true, nil indicates a successful deletion.
360+
func (g *GPTScript) DeleteCredential(ctx context.Context, credCtx, name string) (bool, error) {
361+
_, code, err := g.runBasicCommand(ctx, "credentials/delete", CredentialRequest{
358362
Context: []string{credCtx}, // Only one context can be specified for delete operations
359363
Name: name,
360364
})
361-
return err
365+
if err != nil {
366+
if code == 404 {
367+
return false, nil
368+
}
369+
return false, err
370+
}
371+
return true, nil
362372
}
363373

364-
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
374+
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, int, error) {
365375
run := &Run{
366376
url: g.url,
367377
requestPath: requestPath,
@@ -370,18 +380,18 @@ func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, bod
370380
}
371381

372382
if err := run.request(ctx, body); err != nil {
373-
return "", err
383+
return "", run.responseCode, err
374384
}
375385

376386
out, err := run.Text()
377387
if err != nil {
378-
return "", err
388+
return "", run.responseCode, err
379389
}
380390
if run.err != nil {
381-
return run.ErrorOutput(), run.err
391+
return run.ErrorOutput(), run.responseCode, run.err
382392
}
383393

384-
return out, nil
394+
return out, run.responseCode, nil
385395
}
386396

387397
func getCommand() string {

gptscript_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,7 @@ func TestCredentials(t *testing.T) {
14821482
require.Equal(t, cred.RefreshToken, "my-refresh-token")
14831483

14841484
// Delete
1485-
err = g.DeleteCredential(context.Background(), "testing", name)
1485+
found, err := g.DeleteCredential(context.Background(), "testing", name)
14861486
require.NoError(t, err)
1487+
require.True(t, found)
14871488
}

run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Run struct {
3636
output, errput string
3737
events chan Frame
3838
lock sync.Mutex
39+
responseCode int
3940
}
4041

4142
// Text returns the text output of the gptscript. It blocks until the output is ready.
@@ -235,6 +236,7 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
235236
return r.err
236237
}
237238

239+
r.responseCode = resp.StatusCode
238240
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
239241
r.state = Error
240242
r.err = fmt.Errorf("run encountered an error")

0 commit comments

Comments
 (0)