-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcode_formatter.py
More file actions
68 lines (56 loc) · 2.31 KB
/
code_formatter.py
File metadata and controls
68 lines (56 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Format Gemini code execution responses for Telegram."""
def format_code_execution_response(response) -> tuple:
"""Extract executable_code and code_execution_result parts from a Gemini response.
Returns (text_parts, code_blocks) where:
- text_parts: the normal text content
- code_blocks: list of dicts with 'code', 'output', 'outcome' keys
"""
text_parts = []
code_blocks = []
candidates = getattr(response, 'candidates', None)
if not candidates:
return "", []
for candidate in candidates:
parts = getattr(candidate, 'content', None)
if not parts:
continue
parts = getattr(parts, 'parts', [])
pending_code = None
for part in parts:
if hasattr(part, 'executable_code') and part.executable_code:
pending_code = getattr(part.executable_code, 'code', '')
elif hasattr(part, 'code_execution_result') and part.code_execution_result:
result = part.code_execution_result
output = getattr(result, 'output', '')
outcome = getattr(result, 'outcome', 'OUTCOME_OK')
code_blocks.append({
'code': pending_code or '',
'output': output,
'outcome': str(outcome),
})
pending_code = None
elif hasattr(part, 'text') and part.text:
text_parts.append(part.text)
# If there was code without a result block
if pending_code:
code_blocks.append({
'code': pending_code,
'output': '',
'outcome': 'PENDING',
})
return "\n".join(text_parts), code_blocks
def format_code_blocks_for_telegram(code_blocks: list) -> str:
"""Format code execution blocks for Telegram display."""
if not code_blocks:
return ""
parts = []
for block in code_blocks:
if block.get('code'):
parts.append(f"```python\n{block['code']}\n```")
if block.get('output'):
outcome = block.get('outcome', '')
if 'ERROR' in outcome.upper():
parts.append(f"*Error:*\n```\n{block['output']}\n```")
else:
parts.append(f"*Output:*\n```\n{block['output']}\n```")
return "\n".join(parts)