-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrender_utils.py
More file actions
47 lines (37 loc) · 1.6 KB
/
render_utils.py
File metadata and controls
47 lines (37 loc) · 1.6 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
# SPDX-FileCopyrightText: GitHub, Inc.
# SPDX-License-Identifier: MIT
"""Utilities for rendering and buffering streamed model output."""
import asyncio
import logging
from .path_utils import log_file_name
__all__ = ["flush_async_output", "render_model_output"]
async_output = {}
async_output_lock = asyncio.Lock()
render_logger = logging.getLogger("render")
file_handler = logging.FileHandler(log_file_name("render_stdout.log"))
file_handler.terminator = ""
render_logger.addHandler(file_handler)
render_logger.propagate = False
async def flush_async_output(task_id: str) -> None:
"""Flush buffered async output for *task_id* to the console."""
async with async_output_lock:
if task_id not in async_output:
# No buffered output (agent may have failed before producing any).
return
data = async_output.pop(task_id)
await render_model_output(f"** 🤖✏️ Output for async task: {task_id}\n\n")
await render_model_output(data)
async def render_model_output(data: str, *, log: bool = True, async_task: bool = False, task_id: str | None = None) -> None:
"""Print model output to the console, optionally buffering for async tasks."""
async with async_output_lock:
if async_task and task_id:
if task_id in async_output:
async_output[task_id] += data
data = ""
else:
async_output[task_id] = data
data = "** 🤖✏️ Gathering output from async task ... please hold\n"
if data:
if log:
render_logger.info(data)
print(data, end="", flush=True)