|
5 | 5 |
|
6 | 6 | from pythinker_core.chat_provider import ( |
7 | 7 | APIEmptyResponseError, |
| 8 | + APIStreamProtocolError, |
8 | 9 | ChatProvider, |
9 | 10 | StreamedMessagePart, |
10 | 11 | TokenUsage, |
11 | 12 | ) |
12 | | -from pythinker_core.message import ContentPart, Message, TextPart, ThinkPart, ToolCall |
| 13 | +from pythinker_core.message import Message, TextPart, ThinkPart, ToolCall |
| 14 | +from pythinker_core.stream_message_assembler import StreamMessageAssembler |
13 | 15 | from pythinker_core.tooling import Tool |
14 | 16 | from pythinker_core.utils.aio import Callback, callback |
15 | 17 |
|
@@ -46,30 +48,43 @@ async def generate( |
46 | 48 | APIEmptyResponseError: If the API returns an empty response. |
47 | 49 | ChatProviderError: If any other recognized chat provider error occurs. |
48 | 50 | """ |
49 | | - message = Message(role="assistant", content=[]) |
50 | | - pending_part: StreamedMessagePart | None = None # message part that is currently incomplete |
| 51 | + assembler = StreamMessageAssembler() |
| 52 | + output_published = False |
51 | 53 |
|
52 | | - logger.trace("Generating with history: {history}", history=history) |
| 54 | + logger.trace( |
| 55 | + "Generating with {history_count} history messages and {tool_count} tools", |
| 56 | + history_count=len(history), |
| 57 | + tool_count=len(tools), |
| 58 | + ) |
53 | 59 | stream = await chat_provider.generate(system_prompt, tools, history) |
54 | 60 | async for part in stream: |
55 | | - logger.trace("Received part: {part}", part=part) |
| 61 | + logger.trace("Received stream part: {part_type}", part_type=type(part).__name__) |
56 | 62 | if on_message_part: |
57 | 63 | await callback(on_message_part, part.model_copy(deep=True)) |
| 64 | + output_published = True |
| 65 | + |
| 66 | + try: |
| 67 | + assembler.add(part) |
| 68 | + except APIStreamProtocolError as error: |
| 69 | + error.output_published = output_published |
| 70 | + if error.response_id is None: |
| 71 | + error.response_id = stream.id |
| 72 | + raise |
| 73 | + |
| 74 | + try: |
| 75 | + message = assembler.finish( |
| 76 | + response_id=stream.id, |
| 77 | + finish_reason=stream.finish_reason, |
| 78 | + ) |
| 79 | + except APIStreamProtocolError as error: |
| 80 | + error.output_published = output_published |
| 81 | + if error.response_id is None: |
| 82 | + error.response_id = stream.id |
| 83 | + raise |
58 | 84 |
|
59 | | - if pending_part is None: |
60 | | - pending_part = part |
61 | | - elif not pending_part.merge_in_place(part): # try merge into the pending part |
62 | | - # unmergeable part must push the pending part to the buffer |
63 | | - _message_append(message, pending_part) |
64 | | - if isinstance(pending_part, ToolCall) and on_tool_call: |
65 | | - await callback(on_tool_call, pending_part) |
66 | | - pending_part = part |
67 | | - |
68 | | - # end of message |
69 | | - if pending_part is not None: |
70 | | - _message_append(message, pending_part) |
71 | | - if isinstance(pending_part, ToolCall) and on_tool_call: |
72 | | - await callback(on_tool_call, pending_part) |
| 85 | + for tool_call in message.tool_calls or []: |
| 86 | + if on_tool_call: |
| 87 | + await callback(on_tool_call, tool_call) |
73 | 88 |
|
74 | 89 | if not message.content and not message.tool_calls: |
75 | 90 | raise APIEmptyResponseError("The API returned an empty response.") |
@@ -112,16 +127,3 @@ class GenerateResult: |
112 | 127 | """The token usage of the generated message.""" |
113 | 128 | truncated: bool = False |
114 | 129 | """True when the response was cut off by the output-token limit (finish_reason 'length').""" |
115 | | - |
116 | | - |
117 | | -def _message_append(message: Message, part: StreamedMessagePart) -> None: |
118 | | - match part: |
119 | | - case ContentPart(): |
120 | | - message.content.append(part) |
121 | | - case ToolCall(): |
122 | | - if message.tool_calls is None: |
123 | | - message.tool_calls = [] |
124 | | - message.tool_calls.append(part) |
125 | | - case _: |
126 | | - # may be an orphaned `ToolCallPart` |
127 | | - return |
|
0 commit comments