|
| 1 | +"""Fake Vertex AI Client implementations for testing.""" |
| 2 | + |
| 3 | +import copy |
| 4 | + |
| 5 | +from google.genai import errors as genai_errors |
| 6 | +from vertexai import types as vertexai_types |
| 7 | + |
| 8 | + |
| 9 | +class FakeAgentEnginesA2aTasksEventsClient: |
| 10 | + def __init__(self, parent_client): |
| 11 | + self.parent_client = parent_client |
| 12 | + |
| 13 | + async def append( |
| 14 | + self, name: str, task_events: list[vertexai_types.TaskEvent] |
| 15 | + ) -> None: |
| 16 | + task = self.parent_client.tasks.get(name) |
| 17 | + if not task: |
| 18 | + raise genai_errors.APIError( |
| 19 | + code=404, |
| 20 | + response_json={ |
| 21 | + 'error': { |
| 22 | + 'status': 'NOT_FOUND', |
| 23 | + 'message': 'Task not found', |
| 24 | + } |
| 25 | + }, |
| 26 | + ) |
| 27 | + |
| 28 | + task = copy.deepcopy(task) |
| 29 | + if ( |
| 30 | + not hasattr(task, 'next_event_sequence_number') |
| 31 | + or not task.next_event_sequence_number |
| 32 | + ): |
| 33 | + task.next_event_sequence_number = 0 |
| 34 | + |
| 35 | + for event in task_events: |
| 36 | + data = event.event_data |
| 37 | + if getattr(data, 'state_change', None): |
| 38 | + task.state = getattr(data.state_change, 'new_state', task.state) |
| 39 | + if getattr(data, 'metadata_change', None): |
| 40 | + task.metadata = getattr( |
| 41 | + data.metadata_change, 'new_metadata', task.metadata |
| 42 | + ) |
| 43 | + if getattr(data, 'output_change', None): |
| 44 | + change = getattr( |
| 45 | + data.output_change, 'task_artifact_change', None |
| 46 | + ) |
| 47 | + if not change: |
| 48 | + continue |
| 49 | + if not getattr(task, 'output', None): |
| 50 | + task.output = vertexai_types.TaskOutput() |
| 51 | + |
| 52 | + current_artifacts = ( |
| 53 | + list(task.output.artifacts) |
| 54 | + if getattr(task.output, 'artifacts', None) |
| 55 | + else [] |
| 56 | + ) |
| 57 | + |
| 58 | + deleted_ids = getattr(change, 'deleted_artifact_ids', []) or [] |
| 59 | + if deleted_ids: |
| 60 | + current_artifacts = [ |
| 61 | + a |
| 62 | + for a in current_artifacts |
| 63 | + if a.artifact_id not in deleted_ids |
| 64 | + ] |
| 65 | + |
| 66 | + added = getattr(change, 'added_artifacts', []) or [] |
| 67 | + if added: |
| 68 | + current_artifacts.extend(added) |
| 69 | + |
| 70 | + updated = getattr(change, 'updated_artifacts', []) or [] |
| 71 | + if updated: |
| 72 | + updated_map = {a.artifact_id: a for a in updated} |
| 73 | + current_artifacts = [ |
| 74 | + updated_map.get(a.artifact_id, a) |
| 75 | + for a in current_artifacts |
| 76 | + ] |
| 77 | + |
| 78 | + try: |
| 79 | + del task.output.artifacts[:] |
| 80 | + task.output.artifacts.extend(current_artifacts) |
| 81 | + except Exception: |
| 82 | + task.output.artifacts = current_artifacts |
| 83 | + task.next_event_sequence_number += 1 |
| 84 | + |
| 85 | + self.parent_client.tasks[name] = task |
| 86 | + |
| 87 | + |
| 88 | +class FakeAgentEnginesA2aTasksClient: |
| 89 | + def __init__(self): |
| 90 | + self.tasks: dict[str, vertexai_types.A2aTask] = {} |
| 91 | + self.events = FakeAgentEnginesA2aTasksEventsClient(self) |
| 92 | + |
| 93 | + async def create( |
| 94 | + self, |
| 95 | + name: str, |
| 96 | + a2a_task_id: str, |
| 97 | + config: vertexai_types.CreateAgentEngineTaskConfig, |
| 98 | + ) -> vertexai_types.A2aTask: |
| 99 | + full_name = f'{name}/a2aTasks/{a2a_task_id}' |
| 100 | + task = vertexai_types.A2aTask( |
| 101 | + name=full_name, |
| 102 | + context_id=config.context_id, |
| 103 | + metadata=config.metadata, |
| 104 | + output=config.output, |
| 105 | + state=vertexai_types.State.SUBMITTED, |
| 106 | + ) |
| 107 | + task.next_event_sequence_number = 1 |
| 108 | + self.tasks[full_name] = task |
| 109 | + return task |
| 110 | + |
| 111 | + async def get(self, name: str) -> vertexai_types.A2aTask: |
| 112 | + if name not in self.tasks: |
| 113 | + raise genai_errors.APIError( |
| 114 | + code=404, |
| 115 | + response_json={ |
| 116 | + 'error': { |
| 117 | + 'status': 'NOT_FOUND', |
| 118 | + 'message': 'Task not found', |
| 119 | + } |
| 120 | + }, |
| 121 | + ) |
| 122 | + return copy.deepcopy(self.tasks[name]) |
| 123 | + |
| 124 | + |
| 125 | +class FakeAgentEnginesClient: |
| 126 | + def __init__(self): |
| 127 | + self.a2a_tasks = FakeAgentEnginesA2aTasksClient() |
| 128 | + |
| 129 | + |
| 130 | +class FakeAioClient: |
| 131 | + def __init__(self): |
| 132 | + self.agent_engines = FakeAgentEnginesClient() |
| 133 | + |
| 134 | + |
| 135 | +class FakeVertexClient: |
| 136 | + def __init__(self): |
| 137 | + self.aio = FakeAioClient() |
0 commit comments