Skip to content

Commit 780e07e

Browse files
authored
feat: add stacked credential contexts (#849)
Signed-off-by: Grant Linville <[email protected]>
1 parent 45d444f commit 780e07e

21 files changed

+283
-81
lines changed

docs/docs/03-tools/04-credential-tools.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,55 @@ import os
222222
223223
print("myCred expires at " + os.getenv("GPTSCRIPT_CREDENTIAL_EXPIRATION", ""))
224224
```
225+
226+
## Stacked Credential Contexts (Advanced)
227+
228+
When setting the `--credential-context` argument in GPTScript, you can specify multiple contexts separated by commas.
229+
We refer to this as "stacked credential contexts", or just stacked contexts for short. This allows you to specify an order
230+
of priority for credential contexts. This is best explained by example.
231+
232+
### Example: stacked contexts when running a script that uses a credential
233+
234+
Let's say you have two contexts, `one` and `two`, and you specify them like this:
235+
236+
```bash
237+
gptscript --credential-context one,two my-script.gpt
238+
```
239+
240+
```
241+
Credential: my-credential-tool.gpt as myCred
242+
243+
<tool stuff here>
244+
```
245+
246+
When GPTScript runs, it will first look for a credential called `myCred` in the `one` context.
247+
If it doesn't find it there, it will look for it in the `two` context. If it also doesn't find it there,
248+
it will run the `my-credential-tool.gpt` tool to get the credential. It will then store the new credential into the `one`
249+
context, since that has the highest priority.
250+
251+
### Example: stacked contexts when listing credentials
252+
253+
```bash
254+
gptscript --credential-context one,two credentials
255+
```
256+
257+
When you list credentials like this, GPTScript will print out the information for all credentials in contexts one and two,
258+
with one exception. If there is a credential name that exists in both contexts, GPTScript will only print the information
259+
for the credential in the context with the highest priority, which in this case is `one`.
260+
261+
(To see all credentials in all contexts, you can still use the `--all-contexts` flag, and it will show all credentials,
262+
regardless of whether the same name appears in another context.)
263+
264+
### Example: stacked contexts when showing credentials
265+
266+
```bash
267+
gptscript --credential-context one,two credential show myCred
268+
```
269+
270+
When you show a credential like this, GPTScript will first look for `myCred` in the `one` context. If it doesn't find it
271+
there, it will look for it in the `two` context. If it doesn't find it in either context, it will print an error message.
272+
273+
:::note
274+
You cannot specify stacked contexts when doing `gptscript credential delete`. GPTScript will return an error if
275+
more than one context is specified for this command.
276+
:::

docs/docs/04-command-line-reference/gptscript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ gptscript [flags] PROGRAM_FILE [INPUT...]
1818
--color Use color in output (default true) ($GPTSCRIPT_COLOR)
1919
--config string Path to GPTScript config file ($GPTSCRIPT_CONFIG)
2020
--confirm Prompt before running potentially dangerous commands ($GPTSCRIPT_CONFIRM)
21-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
21+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2222
--credential-override strings Credentials to override (ex: --credential-override github.com/example/cred-tool:API_TOKEN=1234) ($GPTSCRIPT_CREDENTIAL_OVERRIDE)
2323
--debug Enable debug logging ($GPTSCRIPT_DEBUG)
2424
--debug-messages Enable logging of chat completion calls ($GPTSCRIPT_DEBUG_MESSAGES)

docs/docs/04-command-line-reference/gptscript_credential.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gptscript credential [flags]
2020
### Options inherited from parent commands
2121

2222
```
23-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
23+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2424
```
2525

2626
### SEE ALSO

docs/docs/04-command-line-reference/gptscript_credential_delete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ gptscript credential delete <credential name> [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
21+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2222
```
2323

2424
### SEE ALSO

docs/docs/04-command-line-reference/gptscript_credential_show.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ gptscript credential show <credential name> [flags]
1818
### Options inherited from parent commands
1919

2020
```
21-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
21+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2222
```
2323

2424
### SEE ALSO

docs/docs/04-command-line-reference/gptscript_eval.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ gptscript eval [flags]
3030
--color Use color in output (default true) ($GPTSCRIPT_COLOR)
3131
--config string Path to GPTScript config file ($GPTSCRIPT_CONFIG)
3232
--confirm Prompt before running potentially dangerous commands ($GPTSCRIPT_CONFIRM)
33-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
33+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
3434
--credential-override strings Credentials to override (ex: --credential-override github.com/example/cred-tool:API_TOKEN=1234) ($GPTSCRIPT_CREDENTIAL_OVERRIDE)
3535
--debug Enable debug logging ($GPTSCRIPT_DEBUG)
3636
--debug-messages Enable logging of chat completion calls ($GPTSCRIPT_DEBUG_MESSAGES)

docs/docs/04-command-line-reference/gptscript_fmt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ gptscript fmt [flags]
2424
--color Use color in output (default true) ($GPTSCRIPT_COLOR)
2525
--config string Path to GPTScript config file ($GPTSCRIPT_CONFIG)
2626
--confirm Prompt before running potentially dangerous commands ($GPTSCRIPT_CONFIRM)
27-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
27+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2828
--credential-override strings Credentials to override (ex: --credential-override github.com/example/cred-tool:API_TOKEN=1234) ($GPTSCRIPT_CREDENTIAL_OVERRIDE)
2929
--debug Enable debug logging ($GPTSCRIPT_DEBUG)
3030
--debug-messages Enable logging of chat completion calls ($GPTSCRIPT_DEBUG_MESSAGES)

docs/docs/04-command-line-reference/gptscript_getenv.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ gptscript getenv [flags] KEY [DEFAULT]
2323
--color Use color in output (default true) ($GPTSCRIPT_COLOR)
2424
--config string Path to GPTScript config file ($GPTSCRIPT_CONFIG)
2525
--confirm Prompt before running potentially dangerous commands ($GPTSCRIPT_CONFIRM)
26-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
26+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2727
--credential-override strings Credentials to override (ex: --credential-override github.com/example/cred-tool:API_TOKEN=1234) ($GPTSCRIPT_CREDENTIAL_OVERRIDE)
2828
--debug Enable debug logging ($GPTSCRIPT_DEBUG)
2929
--debug-messages Enable logging of chat completion calls ($GPTSCRIPT_DEBUG_MESSAGES)

docs/docs/04-command-line-reference/gptscript_parse.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ gptscript parse [flags]
2424
--color Use color in output (default true) ($GPTSCRIPT_COLOR)
2525
--config string Path to GPTScript config file ($GPTSCRIPT_CONFIG)
2626
--confirm Prompt before running potentially dangerous commands ($GPTSCRIPT_CONFIRM)
27-
--credential-context string Context name in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT) (default "default")
27+
--credential-context strings Context name(s) in which to store credentials ($GPTSCRIPT_CREDENTIAL_CONTEXT)
2828
--credential-override strings Credentials to override (ex: --credential-override github.com/example/cred-tool:API_TOKEN=1234) ($GPTSCRIPT_CREDENTIAL_OVERRIDE)
2929
--debug Enable debug logging ($GPTSCRIPT_DEBUG)
3030
--debug-messages Enable logging of chat completion calls ($GPTSCRIPT_DEBUG_MESSAGES)

integration/cred_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,57 @@ func TestCredentialExpirationEnv(t *testing.T) {
4545
}
4646
}
4747
}
48+
49+
// TestStackedCredentialContexts tests creating, using, listing, showing, and deleting credentials when there are multiple contexts.
50+
func TestStackedCredentialContexts(t *testing.T) {
51+
// First, test credential creation. We will create a credential called testcred in two different contexts called one and two.
52+
_, err := RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_one", "--credential-context", "one,two")
53+
require.NoError(t, err)
54+
55+
_, err = RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_two", "--credential-context", "two")
56+
require.NoError(t, err)
57+
58+
// Next, we try running the testcred_one tool. It should print the value of "testcred" in whichever context it finds the cred first.
59+
out, err := RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_one", "--credential-context", "one,two")
60+
require.NoError(t, err)
61+
require.Contains(t, out, "one")
62+
require.NotContains(t, out, "two")
63+
64+
out, err = RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_one", "--credential-context", "two,one")
65+
require.NoError(t, err)
66+
require.Contains(t, out, "two")
67+
require.NotContains(t, out, "one")
68+
69+
// Next, list credentials and specify both contexts. We should get the credential from the first specified context.
70+
out, err = GPTScriptExec("--credential-context", "one,two", "cred")
71+
require.NoError(t, err)
72+
require.Contains(t, out, "one")
73+
require.NotContains(t, out, "two")
74+
75+
out, err = GPTScriptExec("--credential-context", "two,one", "cred")
76+
require.NoError(t, err)
77+
require.Contains(t, out, "two")
78+
require.NotContains(t, out, "one")
79+
80+
// Next, try showing the credentials.
81+
out, err = GPTScriptExec("--credential-context", "one,two", "cred", "show", "testcred")
82+
require.NoError(t, err)
83+
require.Contains(t, out, "one")
84+
require.NotContains(t, out, "two")
85+
86+
out, err = GPTScriptExec("--credential-context", "two,one", "cred", "show", "testcred")
87+
require.NoError(t, err)
88+
require.Contains(t, out, "two")
89+
require.NotContains(t, out, "one")
90+
91+
// Make sure we get an error if we try to delete a credential with multiple contexts specified.
92+
_, err = GPTScriptExec("--credential-context", "one,two", "cred", "delete", "testcred")
93+
require.Error(t, err)
94+
95+
// Now actually delete the credentials.
96+
_, err = GPTScriptExec("--credential-context", "one", "cred", "delete", "testcred")
97+
require.NoError(t, err)
98+
99+
_, err = GPTScriptExec("--credential-context", "two", "cred", "delete", "testcred")
100+
require.NoError(t, err)
101+
}

0 commit comments

Comments
 (0)