"""Scripted upstream events for real-daemon workflows. No daemon wire fixtures.""" from __future__ import annotations from collections.abc import Iterator import json from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from fake_provider import Reply def chat_events(reply, arguments, call_id, chunk_size): if reply.reasoning: yield ( { "id": "fixture", "choices": [ { "index": 0, "delta": {"reasoning_content": reply.reasoning}, "finish_reason": None, } ], } ) if reply.kind == "python": for offset in range(0, len(arguments), chunk_size): tool_call: dict[str, object] = { "index": 0, "function": {"arguments": arguments[offset : offset + chunk_size]}, } if offset == 0: tool_call.update( { "id": call_id, "type": "function", "function": { "name": reply.tool_name, "arguments": arguments[:chunk_size], }, } ) delta = {"tool_calls": [tool_call]} yield ( { "id": "fixture", "choices": [{"index": 0, "delta": delta, "finish_reason": None}], } ) yield ( { "id": "fixture", "choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}], } ) else: for offset in range(0, len(reply.value), chunk_size): yield ( { "id": "fixture", "choices": [ { "index": 0, "delta": { "content": reply.value[offset : offset + chunk_size] }, "finish_reason": None, } ], } ) yield ( { "id": "fixture", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}], } ) def responses_events(reply, arguments, call_id, chunk_size): yield ({"type": "response.created", "response": {"id": "fixture"}}) output = [] if reply.reasoning: yield ( { "type": "response.reasoning_summary_text.delta", "output_index": 0, "summary_index": 0, "delta": reply.reasoning, } ) output.append( { "id": "reasoning", "type": "reasoning", "summary": [{"type": "summary_text", "text": reply.reasoning}], } ) if reply.kind == "python": yield ( { "type": "response.output_item.added", "output_index": 0, "item": { "id": "call", "type": "function_call", "call_id": call_id, "name": reply.tool_name, }, } ) for offset in range(0, len(arguments), chunk_size): yield ( { "type": "response.function_call_arguments.delta", "output_index": 0, "delta": arguments[offset : offset + chunk_size], } ) output.append( { "id": "call", "type": "function_call", "call_id": call_id, "name": reply.tool_name, "arguments": arguments, "status": "completed", } ) else: for offset in range(0, len(reply.value), chunk_size): yield ( { "type": "response.output_text.delta", "output_index": 0, "content_index": 0, "delta": reply.value[offset : offset + chunk_size], } ) output.append( { "id": "message", "type": "message", "role": "assistant", "status": "completed", "content": [ { "type": "output_text", "text": reply.value, "annotations": [], } ], } ) completed = {"id": "fixture", "status": "completed", "output": output} if reply.usage is not None: completed["usage"] = reply.usage yield ({"type": "response.completed", "response": completed}) def messages_events(reply, arguments, call_id, chunk_size): """Emit one Messages reply, including incremental tool arguments. Output usage starts at 1 and is updated at message_delta, so tests can detect a reducer that misses the final count. """ def block(index, start, deltas): yield {"type": "content_block_start", "index": index, "content_block": start} for delta in deltas: yield {"type": "content_block_delta", "index": index, "delta": delta} yield {"type": "content_block_stop", "index": index} message = { "id": "msg_fixture", "model": "fixture-model", "stop_reason": None, "stop_sequence": None, "type": "message", "role": "assistant", "content": [], } if reply.usage is not None: message["usage"] = { "input_tokens": reply.usage.get("input_tokens", 0), "output_tokens": 1, } yield {"type": "message_start", "message": message} index = 0 if reply.reasoning: yield from block( index, {"type": "thinking", "thinking": "", "signature": ""}, [ {"type": "thinking_delta", "thinking": reply.reasoning}, {"type": "signature_delta", "signature": "fixture-signature"}, ], ) index += 1 if reply.kind == "python": yield from block( index, {"type": "tool_use", "id": call_id, "name": reply.tool_name, "input": {}}, [ { "type": "input_json_delta", "partial_json": arguments[offset : offset + chunk_size], } for offset in range(0, len(arguments), chunk_size) ], ) else: yield from block( index, {"type": "text", "text": ""}, [ { "type": "text_delta", "text": reply.value[offset : offset + chunk_size], } for offset in range(0, len(reply.value), chunk_size) ], ) stop = { "type": "message_delta", "delta": { "stop_reason": "tool_use" if reply.kind == "python" else "end_turn", "stop_sequence": None, }, } if reply.usage is not None: stop["usage"] = {"output_tokens": reply.usage.get("output_tokens", 0)} yield stop yield {"type": "message_stop"} def gemini_events(reply, arguments, call_id, chunk_size): """Emit one Gemini reply with whole tool arguments and final usage. Omit generated call ids to exercise the reducer's id allocation. An explicit reply.call_id is preserved. """ def chunk(part): content = {"role": "model", "parts": [part]} return {"candidates": [{"content": content, "index": 0}]} if reply.reasoning: yield chunk({"text": reply.reasoning, "thought": True}) if reply.kind == "python": call = { "name": reply.tool_name, "args": json.loads(arguments) if arguments else {}, } if reply.call_id: call["id"] = reply.call_id yield chunk( {"functionCall": call, "thoughtSignature": "Zml4dHVyZS1zaWduYXR1cmU="} ) else: for offset in range(0, len(reply.value), chunk_size): yield chunk({"text": reply.value[offset : offset + chunk_size]}) last = {"candidates": [{"finishReason": "STOP", "index": 0}]} if reply.usage is not None: prompt = reply.usage.get("input_tokens", 0) answer = reply.usage.get("output_tokens", 0) last["usageMetadata"] = { "promptTokenCount": prompt, "candidatesTokenCount": answer, "totalTokenCount": prompt + answer, } yield last def antigravity_events( reply: Reply, arguments: str, call_id: str, chunk_size: int ) -> Iterator[dict[str, Any]]: """Cloud Code Assist wraps Gemini chunks and carries service identity.""" for chunk in gemini_events(reply, arguments, call_id, chunk_size): yield { "response": { **chunk, "responseId": "fixture-response", "modelVersion": "fixture-served", }, "traceId": "fixture-trace", }