ducklm/duck_core/context_builder.py

33 lines
989 B
Python

from duck_core.tasks.state import TaskState
class ContextBuilder:
def build_basic_messages(
self,
task: TaskState,
history_messages: list[dict[str, str]] | None = None,
memory_records: list[dict[str, str]] | None = None,
) -> list[dict[str, str]]:
memory_messages = []
if memory_records:
lines = [
f"- {record.get('scope', 'memory')}: {record.get('text', '')}"
for record in memory_records
if record.get("text")
]
if lines:
memory_messages.append(
{
"role": "system",
"content": "Relevant memory:\n" + "\n".join(lines),
}
)
return [
*memory_messages,
*(history_messages or []),
{
"role": "user",
"content": task.user_message,
},
]