| 65 | |
| 66 | |
| 67 | async def run_input_file( |
| 68 | app_name: str, |
| 69 | user_id: str, |
| 70 | agent_or_app: Union[LlmAgent, App], |
| 71 | artifact_service: BaseArtifactService, |
| 72 | session_service: BaseSessionService, |
| 73 | credential_service: BaseCredentialService, |
| 74 | input_path: str, |
| 75 | memory_service: Optional[BaseMemoryService] = None, |
| 76 | ) -> Session: |
| 77 | app = _to_app(agent_or_app, app_name) |
| 78 | runner = Runner( |
| 79 | app=app, |
| 80 | artifact_service=artifact_service, |
| 81 | session_service=session_service, |
| 82 | memory_service=memory_service, |
| 83 | credential_service=credential_service, |
| 84 | ) |
| 85 | with open(input_path, 'r', encoding='utf-8') as f: |
| 86 | input_file = InputFile.model_validate_json(f.read()) |
| 87 | input_file.state['_time'] = datetime.now().isoformat() |
| 88 | |
| 89 | session = await session_service.create_session( |
| 90 | app_name=app_name, user_id=user_id, state=input_file.state |
| 91 | ) |
| 92 | for query in input_file.queries: |
| 93 | click.echo(f'[user]: {query}') |
| 94 | content = types.Content(role='user', parts=[types.Part(text=query)]) |
| 95 | async with Aclosing( |
| 96 | runner.run_async( |
| 97 | user_id=session.user_id, session_id=session.id, new_message=content |
| 98 | ) |
| 99 | ) as agen: |
| 100 | async for event in agen: |
| 101 | if event.content and event.content.parts: |
| 102 | if text := ''.join(part.text or '' for part in event.content.parts): |
| 103 | click.echo(f'[{event.author}]: {text}') |
| 104 | return session |
| 105 | |
| 106 | |
| 107 | _REQUEST_INPUT = 'adk_request_input' |