bone-agent 1.3.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/bin/bone.js +39 -0
  2. package/package.json +25 -39
  3. package/LICENSE +0 -21
  4. package/README.md +0 -184
  5. package/bin/npm-wrapper.js +0 -235
  6. package/bin/rg +0 -0
  7. package/bin/rg.exe +0 -0
  8. package/config.yaml.example +0 -141
  9. package/prompts/main/ask_questions.md +0 -31
  10. package/prompts/main/batch_independent_calls.md +0 -5
  11. package/prompts/main/casual_interactions.md +0 -11
  12. package/prompts/main/code_references.md +0 -8
  13. package/prompts/main/communication_style.md +0 -12
  14. package/prompts/main/context_reliability.md +0 -12
  15. package/prompts/main/conversational_tool_calling.md +0 -15
  16. package/prompts/main/dream.md +0 -36
  17. package/prompts/main/editing_pattern.md +0 -13
  18. package/prompts/main/error_handling.md +0 -6
  19. package/prompts/main/exploration_pattern.md +0 -21
  20. package/prompts/main/intro.md +0 -1
  21. package/prompts/main/obsidian.md +0 -16
  22. package/prompts/main/obsidian_project.md +0 -79
  23. package/prompts/main/professional_objectivity.md +0 -3
  24. package/prompts/main/targeted_searching.md +0 -10
  25. package/prompts/main/task_lists_pattern.md +0 -8
  26. package/prompts/main/temp_folder.md +0 -9
  27. package/prompts/main/think_before_acting.md +0 -10
  28. package/prompts/main/tone_and_style.md +0 -4
  29. package/prompts/main/tool_preferences.md +0 -24
  30. package/prompts/main/trust_subagent_context.md +0 -21
  31. package/prompts/main/when_to_use_sub_agent.md +0 -7
  32. package/prompts/micro/ask_questions.md +0 -1
  33. package/prompts/micro/batch_independent_calls.md +0 -1
  34. package/prompts/micro/casual_interactions.md +0 -1
  35. package/prompts/micro/code_references.md +0 -1
  36. package/prompts/micro/communication_style.md +0 -1
  37. package/prompts/micro/context_reliability.md +0 -1
  38. package/prompts/micro/conversational_tool_calling.md +0 -1
  39. package/prompts/micro/editing_pattern.md +0 -1
  40. package/prompts/micro/error_handling.md +0 -1
  41. package/prompts/micro/exploration_pattern.md +0 -1
  42. package/prompts/micro/intro.md +0 -1
  43. package/prompts/micro/obsidian.md +0 -4
  44. package/prompts/micro/obsidian_project.md +0 -5
  45. package/prompts/micro/professional_objectivity.md +0 -1
  46. package/prompts/micro/targeted_searching.md +0 -1
  47. package/prompts/micro/task_lists_pattern.md +0 -1
  48. package/prompts/micro/temp_folder.md +0 -1
  49. package/prompts/micro/think_before_acting.md +0 -5
  50. package/prompts/micro/tone_and_style.md +0 -1
  51. package/prompts/micro/tool_preferences.md +0 -1
  52. package/prompts/micro/trust_subagent_context.md +0 -1
  53. package/prompts/micro/when_to_use_sub_agent.md +0 -1
  54. package/requirements.txt +0 -9
  55. package/src/__init__.py +0 -11
  56. package/src/core/__init__.py +0 -1
  57. package/src/core/agentic.py +0 -985
  58. package/src/core/chat_manager.py +0 -1564
  59. package/src/core/config_manager.py +0 -253
  60. package/src/core/cron.py +0 -582
  61. package/src/core/cron_allowlist.py +0 -118
  62. package/src/core/memory.py +0 -145
  63. package/src/core/retry.py +0 -71
  64. package/src/core/sub_agent.py +0 -326
  65. package/src/core/tool_approval.py +0 -220
  66. package/src/core/tool_feedback.py +0 -778
  67. package/src/exceptions.py +0 -79
  68. package/src/llm/__init__.py +0 -1
  69. package/src/llm/client.py +0 -171
  70. package/src/llm/config.py +0 -492
  71. package/src/llm/prompts.py +0 -489
  72. package/src/llm/providers.py +0 -436
  73. package/src/llm/streaming.py +0 -163
  74. package/src/llm/token_tracker.py +0 -384
  75. package/src/tools/__init__.py +0 -212
  76. package/src/tools/constants.py +0 -59
  77. package/src/tools/create_file.py +0 -136
  78. package/src/tools/directory.py +0 -389
  79. package/src/tools/edit.py +0 -545
  80. package/src/tools/file_reader.py +0 -322
  81. package/src/tools/helpers/__init__.py +0 -105
  82. package/src/tools/helpers/base.py +0 -550
  83. package/src/tools/helpers/converters.py +0 -44
  84. package/src/tools/helpers/file_helpers.py +0 -189
  85. package/src/tools/helpers/formatters.py +0 -411
  86. package/src/tools/helpers/loader.py +0 -231
  87. package/src/tools/helpers/parallel_executor.py +0 -231
  88. package/src/tools/helpers/path_resolver.py +0 -232
  89. package/src/tools/helpers/plugin_manifest.py +0 -156
  90. package/src/tools/obsidian.py +0 -96
  91. package/src/tools/review_sub_agent.py +0 -189
  92. package/src/tools/rg_search.py +0 -460
  93. package/src/tools/search_plugins.py +0 -109
  94. package/src/tools/select_option.py +0 -600
  95. package/src/tools/shell.py +0 -302
  96. package/src/tools/sub_agent.py +0 -139
  97. package/src/tools/task_list.py +0 -269
  98. package/src/tools/web_search.py +0 -61
  99. package/src/ui/__init__.py +0 -1
  100. package/src/ui/banner.py +0 -87
  101. package/src/ui/commands.py +0 -2809
  102. package/src/ui/displays.py +0 -214
  103. package/src/ui/loader.py +0 -284
  104. package/src/ui/main.py +0 -647
  105. package/src/ui/prompt_utils.py +0 -113
  106. package/src/ui/setting_selector.py +0 -590
  107. package/src/ui/setup_wizard.py +0 -294
  108. package/src/ui/sub_agent_panel.py +0 -234
  109. package/src/ui/tool_confirmation.py +0 -215
  110. package/src/utils/__init__.py +0 -1
  111. package/src/utils/citation_parser.py +0 -199
  112. package/src/utils/editor.py +0 -158
  113. package/src/utils/gitignore_filter.py +0 -149
  114. package/src/utils/logger.py +0 -254
  115. package/src/utils/paths.py +0 -30
  116. package/src/utils/result_parsers.py +0 -108
  117. package/src/utils/safe_commands.py +0 -243
  118. package/src/utils/settings.py +0 -191
  119. package/src/utils/user_message_logger.py +0 -120
  120. package/src/utils/validation.py +0 -191
  121. package/src/utils/web_search.py +0 -173
@@ -1,436 +0,0 @@
1
- """Provider-specific request/response handlers.
2
-
3
- This module isolates provider-specific API quirks into handler classes.
4
- """
5
-
6
- import json
7
- from typing import Optional, Dict, Any, Iterator
8
- import requests
9
-
10
- from exceptions import LLMResponseError
11
-
12
-
13
- class OpenAIHandler:
14
- """Handler for OpenAI-compatible providers.
15
-
16
- Supports: OpenAI, OpenRouter, GLM, Gemini, Kimi, MiniMax
17
- """
18
-
19
- def build_headers(self, config: Dict[str, Any]) -> Dict[str, str]:
20
- """Build request headers."""
21
- headers = {"Content-Type": "application/json"}
22
- if config.get("type") == "api" and config.get("api_key"):
23
- headers["Authorization"] = f"Bearer {config['api_key']}"
24
- if "headers_extra" in config:
25
- headers.update(config["headers_extra"])
26
- return headers
27
-
28
- def build_payload(self, config: Dict[str, Any], messages: list,
29
- tools: Optional[list] = None, stream: bool = True) -> Dict[str, Any]:
30
- """Build request payload."""
31
- payload = {**config.get("payload", {}), "messages": messages, "stream": stream}
32
-
33
- # Ensure model is set from config if not in payload
34
- if "model" not in payload:
35
- model_name = config.get("api_model") or config.get("model")
36
- if model_name:
37
- payload["model"] = model_name
38
-
39
- # Add tools if provided (OpenAI format)
40
- if tools:
41
- payload["tools"] = tools
42
-
43
- # Set default parameters if not in config
44
- if "temperature" not in payload and config.get("allow_temperature", True):
45
- payload["temperature"] = config.get("default_temperature", 0.1)
46
- if "top_p" not in payload and config.get("allow_top_p", True):
47
- payload["top_p"] = config.get("default_top_p", 0.9)
48
-
49
- return payload
50
-
51
- def parse_response(self, response_json: Dict[str, Any]) -> Dict[str, Any]:
52
- """Parse non-streaming response (already in OpenAI format)."""
53
- return response_json
54
-
55
- def parse_stream(self, response: requests.Response) -> Iterator[Dict[str, Any]]:
56
- """Parse streaming response.
57
-
58
- Yields text chunks, and finally yields a dict with __usage__ key.
59
- """
60
- usage_data = None
61
-
62
- for line in response.iter_lines():
63
- if line:
64
- line = line.decode('utf-8')
65
-
66
- # Skip OpenRouter comments (start with ':')
67
- if line.startswith(':'):
68
- continue
69
-
70
- if line.startswith('data: '):
71
- data_str = line[6:]
72
- if data_str.strip() == '[DONE]':
73
- break
74
-
75
- try:
76
- data = json.loads(data_str)
77
-
78
- # Check for mid-stream errors
79
- if 'error' in data:
80
- error_msg = data.get('error', {}).get('message', 'Unknown streaming error')
81
- raise LLMResponseError(
82
- f"Streaming error: {error_msg}",
83
- details={"error_data": data.get('error')}
84
- )
85
-
86
- # Capture usage data if present (usually in final chunk)
87
- if 'usage' in data:
88
- usage_data = dict(data['usage'])
89
- # Promote top-level cost into usage dict (OpenRouter places it here)
90
- if 'cost' in data:
91
- usage_data['cost'] = data['cost']
92
-
93
- choices = data.get('choices', [])
94
- if choices:
95
- delta = choices[0].get('delta', {})
96
- content = delta.get('content')
97
- if content is not None:
98
- yield content
99
-
100
- except json.JSONDecodeError as e:
101
- raise LLMResponseError(
102
- f"Failed to decode streaming response",
103
- details={"original_error": str(e)}
104
- )
105
-
106
- # Yield usage data as final item if captured
107
- if usage_data:
108
- yield {'__usage__': usage_data}
109
-
110
-
111
- class AnthropicHandler:
112
- """Handler for Anthropic API.
113
-
114
- Anthropic has significant differences from OpenAI:
115
- - Different endpoint (/messages vs /chat/completions)
116
- - Different message format (content arrays vs strings)
117
- - Different tool format (flat vs nested)
118
- - Different streaming (SSE with event types vs data: lines)
119
- - Different headers (x-api-key vs Authorization: Bearer)
120
- - Different parameters (requires max_tokens, forbids top_p with temperature)
121
- """
122
-
123
- def build_headers(self, config: Dict[str, Any]) -> Dict[str, str]:
124
- """Build request headers (Anthropic uses x-api-key)."""
125
- headers = {"Content-Type": "application/json"}
126
- if config.get("type") == "api" and config.get("api_key"):
127
- headers["x-api-key"] = config['api_key']
128
- if "headers_extra" in config:
129
- headers.update(config["headers_extra"])
130
- return headers
131
-
132
- def build_payload(self, config: Dict[str, Any], messages: list,
133
- tools: Optional[list] = None, stream: bool = True) -> Dict[str, Any]:
134
- """Build request payload (Anthropic format)."""
135
- # Extract system messages to top-level parameter
136
- system_messages = [msg["content"] for msg in messages if msg.get("role") == "system"]
137
- system_content = "\n".join(system_messages) if system_messages else None
138
- non_system_messages = [msg for msg in messages if msg.get("role") != "system"]
139
-
140
- # Convert messages and tools to Anthropic format
141
- anthropic_messages = self._convert_messages_to_anthropic(non_system_messages)
142
- anthropic_tools = self._convert_tools_to_anthropic(tools) if tools else None
143
-
144
- payload = {**config.get("payload", {}), "messages": anthropic_messages, "stream": stream}
145
-
146
- # Ensure model is set from config if not in payload
147
- if "model" not in payload:
148
- model_name = config.get("api_model") or config.get("model")
149
- if model_name:
150
- payload["model"] = model_name
151
-
152
- if system_content:
153
- payload["system"] = system_content
154
- if anthropic_tools:
155
- payload["tools"] = anthropic_tools
156
-
157
- # Set default parameters (Anthropic requires max_tokens)
158
- if "temperature" not in payload and config.get("allow_temperature", True):
159
- payload["temperature"] = config.get("default_temperature", 0.1)
160
- if "max_tokens" not in payload:
161
- payload["max_tokens"] = config.get("max_tokens", 4096)
162
-
163
- # Anthropic doesn't allow both temperature and top_p
164
- # Only set top_p if temperature is not set
165
- if "temperature" not in payload and "top_p" not in payload:
166
- payload["top_p"] = config.get("default_top_p", 0.9)
167
-
168
- return payload
169
-
170
- def parse_response(self, response_json: Dict[str, Any]) -> Dict[str, Any]:
171
- """Convert Anthropic response format to OpenAI-style format."""
172
- # Anthropic format: {"content": [{"type": "text", "text": "..."}], "usage": {...}}
173
- # OpenAI format: {"choices": [{"message": {"content": "..."}}], "usage": {...}}
174
-
175
- # Convert Anthropic usage format (input_tokens/output_tokens) to OpenAI format (prompt_tokens/completion_tokens)
176
- # Anthropic's input_tokens does NOT include cache tokens; total input =
177
- # input_tokens + cache_read_input_tokens + cache_creation_input_tokens
178
- anthropic_usage = response_json.get("usage", {})
179
- cache_read = anthropic_usage.get('cache_read_input_tokens', 0)
180
- cache_creation = anthropic_usage.get('cache_creation_input_tokens', 0)
181
- prompt_tokens = anthropic_usage.get('input_tokens', 0) + cache_read + cache_creation
182
- completion_tokens = anthropic_usage.get('output_tokens', 0)
183
- openai_format_usage = {
184
- 'prompt_tokens': prompt_tokens,
185
- 'completion_tokens': completion_tokens,
186
- 'total_tokens': prompt_tokens + completion_tokens,
187
- }
188
- # Preserve Anthropic cache token fields for the token tracker
189
- if 'cache_read_input_tokens' in anthropic_usage:
190
- openai_format_usage['cache_read_input_tokens'] = anthropic_usage['cache_read_input_tokens']
191
- if 'cache_creation_input_tokens' in anthropic_usage:
192
- openai_format_usage['cache_creation_input_tokens'] = anthropic_usage['cache_creation_input_tokens']
193
- # Preserve non-cache input count so cost estimation can bill only the
194
- # non-cache portion without relying on fragile prompt_tokens subtraction.
195
- if 'input_tokens' in anthropic_usage:
196
- openai_format_usage['input_tokens'] = anthropic_usage['input_tokens']
197
-
198
- result = {
199
- "choices": [],
200
- "usage": openai_format_usage
201
- }
202
-
203
- # Extract content from Anthropic's content array
204
- content_blocks = response_json.get("content", [])
205
- text_parts = []
206
- tool_calls = []
207
-
208
- for block in content_blocks:
209
- if block.get("type") == "text":
210
- text_parts.append(block.get("text", ""))
211
- elif block.get("type") == "tool_use":
212
- # Convert Anthropic tool_use to OpenAI tool_calls format
213
- tool_calls.append({
214
- "id": block.get("id"),
215
- "type": "function",
216
- "function": {
217
- "name": block.get("name"),
218
- "arguments": json.dumps(block.get("input", {}))
219
- }
220
- })
221
-
222
- # Build OpenAI-style message
223
- message = {"role": "assistant"}
224
-
225
- # Include either text content or tool calls
226
- if tool_calls:
227
- message["content"] = None
228
- message["tool_calls"] = tool_calls
229
- else:
230
- message["content"] = "".join(text_parts)
231
-
232
- result["choices"].append({"message": message})
233
-
234
- return result
235
-
236
- def parse_stream(self, response: requests.Response) -> Iterator[Dict[str, Any]]:
237
- """Parse Anthropic's SSE-based streaming response.
238
-
239
- Yields text chunks, and finally yields a dict with __usage__ key.
240
-
241
- Anthropic splits usage across two events:
242
- - message_start: contains input_tokens
243
- - message_delta: contains output_tokens
244
- We merge both and convert to OpenAI format (prompt_tokens/completion_tokens).
245
- """
246
- usage_data = {}
247
-
248
- for line in response.iter_lines():
249
- if line:
250
- line = line.decode('utf-8')
251
-
252
- # Anthropic uses SSE format: "event: <type>" followed by "data: <json>"
253
- if line.startswith('data: '):
254
- data_str = line[6:]
255
- try:
256
- data = json.loads(data_str)
257
-
258
- # Check for errors
259
- if data.get('type') == 'error':
260
- error_msg = data.get('error', {}).get('message', 'Unknown error')
261
- raise LLMResponseError(
262
- f"Anthropic streaming error: {error_msg}",
263
- details={"error_data": data.get('error')}
264
- )
265
-
266
- # Capture input_tokens from message_start events
267
- if data.get('type') == 'message_start':
268
- message_usage = data.get('message', {}).get('usage', {})
269
- if message_usage:
270
- usage_data.update(message_usage)
271
-
272
- # Capture output_tokens from message_delta events
273
- if data.get('type') == 'message_delta' and 'usage' in data:
274
- usage_data.update(data['usage'])
275
-
276
- # Extract text from content_block_delta events
277
- if data.get('type') == 'content_block_delta':
278
- delta = data.get('delta', {})
279
- if delta.get('type') == 'text_delta':
280
- text = delta.get('text', '')
281
- if text:
282
- yield text
283
-
284
- except json.JSONDecodeError as e:
285
- raise LLMResponseError(
286
- f"Failed to decode Anthropic streaming response",
287
- details={"original_error": str(e)}
288
- )
289
-
290
- # Yield usage data as final item if captured
291
- # Convert Anthropic format (input_tokens/output_tokens) to OpenAI format (prompt_tokens/completion_tokens)
292
- # Anthropic's input_tokens does NOT include cache tokens; total input =
293
- # input_tokens + cache_read_input_tokens + cache_creation_input_tokens
294
- if usage_data:
295
- cache_read = usage_data.get('cache_read_input_tokens', 0)
296
- cache_creation = usage_data.get('cache_creation_input_tokens', 0)
297
- prompt_tokens = usage_data.get('input_tokens', 0) + cache_read + cache_creation
298
- completion_tokens = usage_data.get('output_tokens', 0)
299
- openai_format_usage = {
300
- 'prompt_tokens': prompt_tokens,
301
- 'completion_tokens': completion_tokens,
302
- 'total_tokens': prompt_tokens + completion_tokens,
303
- }
304
- # Preserve Anthropic cache token fields for the token tracker
305
- if 'cache_read_input_tokens' in usage_data:
306
- openai_format_usage['cache_read_input_tokens'] = usage_data['cache_read_input_tokens']
307
- if 'cache_creation_input_tokens' in usage_data:
308
- openai_format_usage['cache_creation_input_tokens'] = usage_data['cache_creation_input_tokens']
309
- # Preserve non-cache input count for accurate cost estimation
310
- if 'input_tokens' in usage_data:
311
- openai_format_usage['input_tokens'] = usage_data['input_tokens']
312
- yield {'__usage__': openai_format_usage}
313
-
314
- @staticmethod
315
- def _convert_tools_to_anthropic(openai_tools: list) -> list:
316
- """Convert OpenAI-style tool definitions to Anthropic format.
317
-
318
- OpenAI format: {"type": "function", "function": {"name": "...", "parameters": {...}}}
319
- Anthropic format: {"name": "...", "description": "...", "input_schema": {...}}
320
- """
321
- anthropic_tools = []
322
-
323
- for openai_tool in openai_tools:
324
- if openai_tool.get("type") == "function":
325
- func = openai_tool.get("function", {})
326
- anthropic_tool = {
327
- "name": func.get("name"),
328
- "description": func.get("description", ""),
329
- "input_schema": func.get("parameters", {"type": "object", "properties": {}})
330
- }
331
- anthropic_tools.append(anthropic_tool)
332
-
333
- return anthropic_tools
334
-
335
- @staticmethod
336
- def _convert_messages_to_anthropic(openai_messages: list) -> list:
337
- """Convert OpenAI-style messages to Anthropic format.
338
-
339
- Anthropic requires all content to be an array, not a string.
340
-
341
- OpenAI format:
342
- {"role": "user", "content": "text"}
343
- {"role": "tool", "content": "...", "tool_call_id": "..."}
344
-
345
- Anthropic format:
346
- {"role": "user", "content": [{"type": "text", "text": "..."}]}
347
- {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "...", "content": "..."}]}
348
- """
349
- anthropic_messages = []
350
-
351
- for msg in openai_messages:
352
- # Handle tool result messages
353
- if msg.get("role") == "tool":
354
- anthropic_msg = {
355
- "role": "user",
356
- "content": [
357
- {
358
- "type": "tool_result",
359
- "tool_use_id": msg.get("tool_call_id"),
360
- "content": msg.get("content", "")
361
- }
362
- ]
363
- }
364
- anthropic_messages.append(anthropic_msg)
365
- # Handle user and assistant messages - convert string content to array
366
- elif msg.get("role") in ("user", "assistant"):
367
- content = msg.get("content", "")
368
- tool_calls = msg.get("tool_calls")
369
-
370
- # Build content blocks array
371
- content_blocks = []
372
-
373
- # Add text content if present
374
- if isinstance(content, str) and content.strip():
375
- content_blocks.append({
376
- "type": "text",
377
- "text": content
378
- })
379
- elif isinstance(content, list):
380
- # Already an array (Anthropic format), use as-is
381
- anthropic_messages.append(msg)
382
- continue
383
-
384
- # Add tool_use blocks if present (for assistant messages with tool calls)
385
- if tool_calls:
386
- for tool_call in tool_calls:
387
- content_blocks.append({
388
- "type": "tool_use",
389
- "id": tool_call.get("id"),
390
- "name": tool_call.get("function", {}).get("name"),
391
- "input": json.loads(tool_call.get("function", {}).get("arguments", "{}"))
392
- })
393
-
394
- # Only add message if we have content blocks (text or tool_use)
395
- if content_blocks:
396
- anthropic_msg = {
397
- "role": msg.get("role"),
398
- "content": content_blocks
399
- }
400
- anthropic_messages.append(anthropic_msg)
401
- else:
402
- # Other message types, pass through
403
- anthropic_messages.append(msg)
404
-
405
- return anthropic_messages
406
-
407
-
408
- # Handler registry - maps provider names to handler classes
409
- HANDLER_REGISTRY = {
410
- "openai": OpenAIHandler,
411
- "openrouter": OpenAIHandler,
412
- "glm": OpenAIHandler,
413
- "glm_plan": OpenAIHandler,
414
- "gemini": OpenAIHandler,
415
- "minimax": AnthropicHandler,
416
- "minimax_plan": AnthropicHandler,
417
- "kimi": OpenAIHandler,
418
- "anthropic": AnthropicHandler,
419
- "local": OpenAIHandler,
420
- }
421
-
422
-
423
- def get_handler(provider_name: str):
424
- """Get handler instance for the given provider.
425
-
426
- Args:
427
- provider_name: Name of the provider
428
-
429
- Returns:
430
- Handler instance for the provider
431
- """
432
- handler_class = HANDLER_REGISTRY.get(provider_name.lower(), OpenAIHandler)
433
- return handler_class()
434
-
435
-
436
- __all__ = ['OpenAIHandler', 'AnthropicHandler', 'get_handler']
@@ -1,163 +0,0 @@
1
- """Streaming response assembler for agentic mode.
2
-
3
- Consumes a StreamWrapper yielding mixed delta dicts and assembles them into
4
- a complete message dict (content + tool_calls), matching the format that
5
- non-streaming responses already produce.
6
-
7
- Usage:
8
- stream = client.chat_completion(messages, stream=True, tools=tools)
9
- assembler = StreamingResponse(stream, console, debug_mode=False)
10
- message = assembler.consume() # iterates stream, prints text, assembles tool_calls
11
- tool_calls = message.get("tool_calls")
12
- usage = assembler.usage
13
- """
14
-
15
- import json
16
- import sys
17
- from typing import Any, Dict, List, Optional
18
-
19
- from rich.text import Text
20
-
21
-
22
- class StreamingResponse:
23
- """Assemble streaming deltas into a complete message dict.
24
-
25
- Text deltas are printed to stderr immediately (raw, no formatting).
26
- Tool call deltas are buffered and reassembled across chunks.
27
- """
28
-
29
- def __init__(self, stream, console=None, debug_mode: bool = False,
30
- on_text=None, live=None):
31
- """
32
- Args:
33
- stream: StreamWrapper (or any iterable yielding deltas / __usage__ dicts).
34
- console: Rich Console instance (used for debug logging only).
35
- debug_mode: If True, log assembly details.
36
- on_text: Optional callback(str) invoked for each text token.
37
- Defaults to printing to stderr.
38
- live: Optional Rich Live context. When set, streaming text is
39
- rendered through Live (raw during streaming, swappable to
40
- Markdown on completion) instead of raw stderr.
41
- """
42
- self._stream = stream
43
- self._console = console
44
- self._debug = debug_mode
45
- self._on_text = on_text
46
- self._live = live
47
-
48
- # Accumulated state
49
- self._text_parts: List[str] = []
50
- self._tool_calls: Dict[int, Dict[str, Any]] = {} # index -> partial tool call
51
- self._usage: Optional[Dict[str, Any]] = None
52
-
53
- def consume(self) -> Dict[str, Any]:
54
- """Iterate the stream, print text tokens, assemble tool calls.
55
-
56
- Returns:
57
- A message dict with 'role', 'content', and optionally 'tool_calls'
58
- — same shape as a non-streaming response["choices"][0]["message"].
59
- """
60
- for item in self._stream:
61
- if isinstance(item, dict) and '__usage__' in item:
62
- self._usage = item['__usage__']
63
- continue
64
-
65
- # OpenAI-style delta: {"content": "...", "tool_calls": [...]}
66
- if isinstance(item, dict):
67
- self._process_delta(item)
68
- elif isinstance(item, str):
69
- # Fallback: plain text string (legacy parse_stream behavior)
70
- self._print(item)
71
- self._text_parts.append(item)
72
-
73
- return self._build_message()
74
-
75
- @property
76
- def usage(self) -> Optional[Dict[str, Any]]:
77
- """Usage data captured from the stream's final chunk."""
78
- return self._usage
79
-
80
- def _process_delta(self, delta: Dict[str, Any]):
81
- """Process a single streaming delta dict.
82
-
83
- Expected shapes (OpenAI format):
84
- {"content": "some text"}
85
- {"tool_calls": [{"index": 0, "id": "call_xxx", "function": {"name": "f"}}]}
86
- {"tool_calls": [{"index": 0, "function": {"arguments": "{..."}}]}
87
- {"content": "text", "tool_calls": [...]}
88
- """
89
- # Handle text content
90
- content = delta.get("content")
91
- if content is not None:
92
- self._print(content)
93
- self._text_parts.append(content)
94
-
95
- # Handle tool call fragments
96
- tool_calls = delta.get("tool_calls")
97
- if tool_calls:
98
- for tc_delta in tool_calls:
99
- idx = tc_delta.get("index", 0)
100
- if idx not in self._tool_calls:
101
- self._tool_calls[idx] = {
102
- "id": "",
103
- "type": "function",
104
- "function": {"name": "", "arguments": ""},
105
- }
106
-
107
- entry = self._tool_calls[idx]
108
-
109
- # Tool call id (sent once at the start)
110
- if tc_delta.get("id"):
111
- entry["id"] = tc_delta["id"]
112
-
113
- # Function name (sent once at the start)
114
- func = tc_delta.get("function", {})
115
- if func.get("name"):
116
- entry["function"]["name"] = func["name"]
117
-
118
- # Arguments (sent incrementally, concatenated)
119
- if func.get("arguments"):
120
- entry["function"]["arguments"] += func["arguments"]
121
-
122
- def _build_message(self) -> Dict[str, Any]:
123
- """Build the final message dict from assembled parts."""
124
- message: Dict[str, Any] = {"role": "assistant"}
125
-
126
- # Collect assembled tool calls in index order
127
- assembled_tool_calls = []
128
- if self._tool_calls:
129
- for idx in sorted(self._tool_calls.keys()):
130
- assembled_tool_calls.append(self._tool_calls[idx])
131
-
132
- if assembled_tool_calls:
133
- message["tool_calls"] = assembled_tool_calls
134
- # Content may be None or a string alongside tool calls
135
- text = "".join(self._text_parts).strip()
136
- message["content"] = text if text else None
137
- else:
138
- message["content"] = "".join(self._text_parts)
139
-
140
- return message
141
-
142
- def _print(self, text: str):
143
- """Output text token via the configured callback (default: stderr).
144
-
145
- When a Rich Live context is provided, text is rendered through Live
146
- for atomic screen updates (raw text during streaming, swappable to
147
- Markdown on completion).
148
- """
149
- if self._live is not None:
150
- # Render through Rich Live — update with accumulated text so far
151
- self._live.update(Text("".join(self._text_parts) + text))
152
- elif self._on_text is None:
153
- # Default: print to stderr
154
- sys.stderr.write(text)
155
- sys.stderr.flush()
156
- elif callable(self._on_text):
157
- self._on_text(text)
158
- # If on_text is False, silently drop output (subagent mode)
159
-
160
- def close(self):
161
- """Close the underlying stream."""
162
- if hasattr(self._stream, 'close'):
163
- self._stream.close()