okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffauffeat(api)!: migrate daemon and clients to protocol 361c43ca

main
4.8 KiB150 linesraw
1"""Scripted upstream events for real-daemon workflows. No daemon wire fixtures."""
2
3
4def chat_events(reply, arguments, call_id, chunk_size):
5    if reply.reasoning:
6        yield (
7            {
8                "id": "fixture",
9                "choices": [
10                    {
11                        "index": 0,
12                        "delta": {"reasoning_content": reply.reasoning},
13                        "finish_reason": None,
14                    }
15                ],
16            }
17        )
18    if reply.kind == "python":
19        for offset in range(0, len(arguments), chunk_size):
20            tool_call: dict[str, object] = {
21                "index": 0,
22                "function": {"arguments": arguments[offset : offset + chunk_size]},
23            }
24            if offset == 0:
25                tool_call.update(
26                    {
27                        "id": call_id,
28                        "type": "function",
29                        "function": {
30                            "name": reply.tool_name,
31                            "arguments": arguments[:chunk_size],
32                        },
33                    }
34                )
35            delta = {"tool_calls": [tool_call]}
36            yield (
37                {
38                    "id": "fixture",
39                    "choices": [{"index": 0, "delta": delta, "finish_reason": None}],
40                }
41            )
42        yield (
43            {
44                "id": "fixture",
45                "choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}],
46            }
47        )
48    else:
49        for offset in range(0, len(reply.value), chunk_size):
50            yield (
51                {
52                    "id": "fixture",
53                    "choices": [
54                        {
55                            "index": 0,
56                            "delta": {
57                                "content": reply.value[offset : offset + chunk_size]
58                            },
59                            "finish_reason": None,
60                        }
61                    ],
62                }
63            )
64        yield (
65            {
66                "id": "fixture",
67                "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
68            }
69        )
70
71
72def responses_events(reply, arguments, call_id, chunk_size):
73    yield ({"type": "response.created", "response": {"id": "fixture"}})
74    output = []
75    if reply.reasoning:
76        yield (
77            {
78                "type": "response.reasoning_summary_text.delta",
79                "output_index": 0,
80                "summary_index": 0,
81                "delta": reply.reasoning,
82            }
83        )
84        output.append(
85            {
86                "id": "reasoning",
87                "type": "reasoning",
88                "summary": [{"type": "summary_text", "text": reply.reasoning}],
89            }
90        )
91    if reply.kind == "python":
92        yield (
93            {
94                "type": "response.output_item.added",
95                "output_index": 0,
96                "item": {
97                    "id": "call",
98                    "type": "function_call",
99                    "call_id": call_id,
100                    "name": reply.tool_name,
101                },
102            }
103        )
104        for offset in range(0, len(arguments), chunk_size):
105            yield (
106                {
107                    "type": "response.function_call_arguments.delta",
108                    "output_index": 0,
109                    "delta": arguments[offset : offset + chunk_size],
110                }
111            )
112        output.append(
113            {
114                "id": "call",
115                "type": "function_call",
116                "call_id": call_id,
117                "name": reply.tool_name,
118                "arguments": arguments,
119                "status": "completed",
120            }
121        )
122    else:
123        for offset in range(0, len(reply.value), chunk_size):
124            yield (
125                {
126                    "type": "response.output_text.delta",
127                    "output_index": 0,
128                    "content_index": 0,
129                    "delta": reply.value[offset : offset + chunk_size],
130                }
131            )
132        output.append(
133            {
134                "id": "message",
135                "type": "message",
136                "role": "assistant",
137                "status": "completed",
138                "content": [
139                    {
140                        "type": "output_text",
141                        "text": reply.value,
142                        "annotations": [],
143                    }
144                ],
145            }
146        )
147    completed = {"id": "fixture", "status": "completed", "output": output}
148    if reply.usage is not None:
149        completed["usage"] = reply.usage
150    yield ({"type": "response.completed", "response": completed})