diff --git a/gptscript/frame.py b/gptscript/frame.py index 06a314e..21aa459 100644 --- a/gptscript/frame.py +++ b/gptscript/frame.py @@ -194,13 +194,25 @@ def __init__(self, self.llmResponse = llmResponse +class PromptField: + def __init__(self, + name: str = "", + description: str = "", + sensitive: bool | None = None, + **kwargs, + ): + self.name = name + self.description = description + self.sensitive = sensitive + + class PromptFrame: def __init__(self, id: str = "", type: RunEventType = RunEventType.prompt, time: str = "", message: str = "", - fields: list[str] = None, + fields: list[PromptField] = None, metadata: dict[str, str] = None, sensitive: bool = False, **kwargs, @@ -209,6 +221,10 @@ def __init__(self, self.time = time self.message = message self.fields = fields + if self.fields is not None: + for i in range(len(self.fields)): + if isinstance(self.fields[i], dict): + self.fields[i] = PromptField(**self.fields[i]) self.metadata = metadata self.sensitive = sensitive self.type = type diff --git a/tests/test_gptscript.py b/tests/test_gptscript.py index 9378be1..05f747a 100644 --- a/tests/test_gptscript.py +++ b/tests/test_gptscript.py @@ -546,8 +546,11 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): event_output += output.content elif frame.type == RunEventType.callFinish: call_finish_seen = True + for output in frame.output: + event_output += output.content - run = gptscript.run(os.getcwd() + "/tests/fixtures/global-tools.gpt", + cwd = os.getcwd().removesuffix("/tests") + run = gptscript.run(cwd + "/tests/fixtures/global-tools.gpt", Options( disableCache=True, credentialOverrides=["github.com/gptscript-ai/gateway:OPENAI_API_KEY"], @@ -555,8 +558,9 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): event_handlers=[process_event], ) - assert "Hello!" in await run.text(), "Unexpected output from global tool test" - assert "Hello" in event_output, "Unexpected stream output from global tool test" + output = await run.text() + assert "Hello!" in output, "Unexpected output from global tool test: " + output + assert "Hello" in event_output, "Unexpected stream output from global tool test: " + event_output assert run_start_seen and call_start_seen and call_progress_seen and call_finish_seen and run_finish_seen, \ f"One of these is False: {run_start_seen}, {call_start_seen}, {call_progress_seen}, {call_finish_seen}, {run_finish_seen}" @@ -573,7 +577,7 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): confirm_event_found = True assert '"ls' in frame.input or '"dir' in frame.input, "Unexpected confirm input: " + frame.input await gptscript.confirm(AuthResponse(frame.id, True)) - elif frame.type == RunEventType.callProgress: + elif frame.type == RunEventType.callProgress or frame.type == RunEventType.callFinish: for output in frame.output: event_content += output.content @@ -610,7 +614,7 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): confirm_event_found = True assert '"ls"' in frame.input, "Unexpected confirm input: " + frame.input await gptscript.confirm(AuthResponse(frame.id, False, "I will not allow it!")) - elif frame.type == RunEventType.callProgress: + elif frame.type == RunEventType.callProgress or frame.type == RunEventType.callFinish: for output in frame.output: event_content += output.content @@ -637,9 +641,9 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): if frame.type == RunEventType.prompt: prompt_event_found = True assert len(frame.fields) == 1, "Unexpected number of fields: " + str(frame.fields) - assert "first name" in frame.fields[0], "Unexpected field: " + frame.fields[0] - await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0]: "Clicky"})) - elif frame.type == RunEventType.callProgress: + assert "first name" in frame.fields[0].name, "Unexpected field: " + frame.fields[0].name + await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0].name: "Clicky"})) + elif frame.type == RunEventType.callProgress or frame.type == RunEventType.callFinish: for output in frame.output: event_content += output.content @@ -667,10 +671,10 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): if frame.type == RunEventType.prompt: prompt_event_found = True assert len(frame.fields) == 1, "Unexpected number of fields: " + str(frame.fields) - assert "first name" in frame.fields[0], "Unexpected field: " + frame.fields[0] + assert "first name" in frame.fields[0].name, "Unexpected field: " + frame.fields[0].name assert "first_name" in frame.metadata, "Unexpected metadata: " + str(frame.metadata) assert frame.metadata["first_name"] == "Clicky", "Unexpected metadata: " + str(frame.metadata) - await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0]: "Clicky"})) + await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0].name: "Clicky"})) out = await gptscript.run( "sys.prompt", @@ -691,8 +695,8 @@ async def process_event(r: Run, frame: CallFrame | RunFrame | PromptFrame): if frame.type == RunEventType.prompt: prompt_event_found = True assert len(frame.fields) == 1, "Unexpected number of fields: " + str(frame.fields) - assert "first name" in frame.fields[0], "Unexpected field: " + frame.fields[0] - await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0]: "Clicky"})) + assert "first name" in frame.fields[0].name, "Unexpected field: " + frame.fields[0].name + await gptscript.prompt(PromptResponse(frame.id, {frame.fields[0].name: "Clicky"})) tool = ToolDef( tools=["sys.prompt"], @@ -727,7 +731,7 @@ async def test_run_file_with_metadata(gptscript): @pytest.mark.asyncio async def test_parse_with_metadata_then_run(gptscript): - cwd = os.getcwd().removesuffix("tests") + cwd = os.getcwd().removesuffix("/tests") tools = await gptscript.parse(cwd + "/tests/fixtures/parse-with-metadata.gpt") run = gptscript.evaluate(tools[0]) assert "200" == await run.text(), "Expect file to have correct output"