diff --git a/gptscript/__init__.py b/gptscript/__init__.py index e69de29..ef3adbe 100644 --- a/gptscript/__init__.py +++ b/gptscript/__init__.py @@ -0,0 +1,9 @@ +from gptscript.gptscript import GPTScript +from gptscript.confirm import AuthResponse +from gptscript.frame import RunFrame, CallFrame, PromptFrame +from gptscript.opts import GlobalOptions +from gptscript.prompt import PromptResponse +from gptscript.run import Run, RunBasicCommand, Options +from gptscript.text import Text +from gptscript.tool import ToolDef, Tool +from gptscript.exec_utils import get_env diff --git a/gptscript/exec_utils.py b/gptscript/exec_utils.py index 22fd463..98e4132 100644 --- a/gptscript/exec_utils.py +++ b/gptscript/exec_utils.py @@ -1,6 +1,9 @@ +import json import os import subprocess import sys +import base64 +import gzip if sys.platform == "win32": import msvcrt @@ -84,3 +87,15 @@ def _stream_cmd(cmd, args=[], input=None, fds=tuple(), close_fds=True): return process except Exception as e: raise e + + +def get_env(key, default=''): + v = os.environ.get(key, '') + if v == '': + return default + if v.startswith('{"_gz":"') and v.endswith('"}'): + try: + return gzip.decompress(base64.b64decode(v[8:-2])).decode('utf-8') + except Exception as e: + pass + return v diff --git a/tests/test_gptscript.py b/tests/test_gptscript.py index 3ea280d..84ff00e 100644 --- a/tests/test_gptscript.py +++ b/tests/test_gptscript.py @@ -1,3 +1,6 @@ +import base64 +import gzip +import json import os import platform import subprocess @@ -13,6 +16,7 @@ from gptscript.run import Run from gptscript.text import Text from gptscript.tool import ToolDef, ArgumentSchema, Property, Tool +from gptscript.exec_utils import get_env # Ensure the OPENAI_API_KEY is set for testing @@ -512,3 +516,10 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): assert not prompt_event_found, "Prompt event occurred" assert "prompt event occurred" in out, "Unexpected output: " + out + + +def test_get_env(): + os.environ['TEST_ENV'] = json.dumps({ + '_gz': base64.b64encode(gzip.compress(b'test value')).decode('utf-8'), + }).replace(' ', '') + assert 'test value' == get_env('TEST_ENV')