Skip to content

Commit 69519e0

Browse files
committed
update for stacked contexts
Signed-off-by: Grant Linville <[email protected]>
1 parent e5ed884 commit 69519e0

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

gptscript/credentials.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def is_timezone_aware(dt: datetime):
99

1010

1111
class CredentialType(Enum):
12-
Tool = "tool",
13-
ModelProvider = "modelProvider"
12+
tool = "tool",
13+
modelProvider = "modelProvider"
1414

1515

1616
class Credential:
1717
def __init__(self,
1818
context: str = "default",
1919
toolName: str = "",
20-
type: CredentialType = CredentialType.Tool,
20+
type: CredentialType = CredentialType.tool,
2121
env: dict[str, str] = None,
2222
ephemeral: bool = False,
2323
expiresAt: datetime = None,
@@ -76,3 +76,20 @@ def __init__(self,
7676
self.allContexts = allContexts
7777
self.contexts = contexts
7878
self.name = name
79+
80+
def to_credential(c) -> Credential:
81+
expiresAt = c["expiresAt"]
82+
if expiresAt is not None:
83+
if expiresAt.endswith("Z"):
84+
expiresAt = expiresAt.replace("Z", "+00:00")
85+
expiresAt = datetime.fromisoformat(expiresAt)
86+
87+
return Credential(
88+
context=c["context"],
89+
toolName=c["toolName"],
90+
type=CredentialType[c["type"]],
91+
env=c["env"],
92+
ephemeral=c.get("ephemeral", False),
93+
expiresAt=expiresAt,
94+
refreshToken=c["refreshToken"],
95+
)

gptscript/gptscript.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import requests
1010

1111
from gptscript.confirm import AuthResponse
12-
from gptscript.credentials import Credential
12+
from gptscript.credentials import Credential, to_credential
1313
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
1414
from gptscript.opts import GlobalOptions
1515
from gptscript.prompt import PromptResponse
@@ -195,7 +195,7 @@ async def list_credentials(self, contexts: List[str] = None, all_contexts: bool
195195
if res.startswith("an error occurred:"):
196196
return res
197197

198-
return [Credential(**c) for c in json.loads(res)]
198+
return [to_credential(cred) for cred in json.loads(res)]
199199

200200
async def create_credential(self, cred: Credential) -> str:
201201
return await self._run_basic_command(
@@ -214,7 +214,7 @@ async def reveal_credential(self, contexts: List[str] = None, name: str = "") ->
214214
if res.startswith("an error occurred:"):
215215
return res
216216

217-
return Credential(**json.loads(res))
217+
return to_credential(json.loads(res))
218218

219219
async def delete_credential(self, context: str = "default", name: str = "") -> str:
220220
return await self._run_basic_command(

tests/test_gptscript.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import os
55
import platform
66
import subprocess
7+
from datetime import datetime, timedelta, timezone
8+
from time import sleep
79

810
import pytest
911

@@ -688,12 +690,18 @@ async def test_parse_with_metadata_then_run(gptscript):
688690
@pytest.mark.asyncio
689691
async def test_credentials(gptscript):
690692
name = "test-" + str(os.urandom(4).hex())
691-
res = await gptscript.create_credential(Credential(toolName=name, env={"TEST": "test"}))
693+
now = datetime.now()
694+
res = await gptscript.create_credential(Credential(toolName=name, env={"TEST": "test"}, expiresAt=now + timedelta(seconds=5)))
692695
assert not res.startswith("an error occurred"), "Unexpected error creating credential: " + res
693696

697+
sleep(5)
698+
694699
res = await gptscript.list_credentials()
695700
assert not str(res).startswith("an error occurred"), "Unexpected error listing credentials: " + str(res)
696701
assert len(res) > 0, "Expected at least one credential"
702+
for cred in res:
703+
if cred.toolName == name:
704+
assert cred.expiresAt < datetime.now(timezone.utc), "Expected credential to have expired"
697705

698706
res = await gptscript.reveal_credential(name=name)
699707
assert not str(res).startswith("an error occurred"), "Unexpected error revealing credential: " + res

0 commit comments

Comments
 (0)