Merge branch 'pr-12'
This commit is contained in:
commit
ac279b2df3
@ -1,5 +1,7 @@
|
|||||||
"""Context builder for assembling agent prompts."""
|
"""Context builder for assembling agent prompts."""
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import mimetypes
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -114,7 +116,8 @@ When remembering something, write to {workspace_path}/memory/MEMORY.md"""
|
|||||||
self,
|
self,
|
||||||
history: list[dict[str, Any]],
|
history: list[dict[str, Any]],
|
||||||
current_message: str,
|
current_message: str,
|
||||||
skill_names: list[str] | None = None
|
skill_names: list[str] | None = None,
|
||||||
|
media: list[str] | None = None,
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Build the complete message list for an LLM call.
|
Build the complete message list for an LLM call.
|
||||||
@ -123,6 +126,7 @@ When remembering something, write to {workspace_path}/memory/MEMORY.md"""
|
|||||||
history: Previous conversation messages.
|
history: Previous conversation messages.
|
||||||
current_message: The new user message.
|
current_message: The new user message.
|
||||||
skill_names: Optional skills to include.
|
skill_names: Optional skills to include.
|
||||||
|
media: Optional list of local file paths for images/media.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of messages including system prompt.
|
List of messages including system prompt.
|
||||||
@ -136,11 +140,30 @@ When remembering something, write to {workspace_path}/memory/MEMORY.md"""
|
|||||||
# History
|
# History
|
||||||
messages.extend(history)
|
messages.extend(history)
|
||||||
|
|
||||||
# Current message
|
# Current message (with optional image attachments)
|
||||||
messages.append({"role": "user", "content": current_message})
|
user_content = self._build_user_content(current_message, media)
|
||||||
|
messages.append({"role": "user", "content": user_content})
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
def _build_user_content(self, text: str, media: list[str] | None) -> str | list[dict[str, Any]]:
|
||||||
|
"""Build user message content with optional base64-encoded images."""
|
||||||
|
if not media:
|
||||||
|
return text
|
||||||
|
|
||||||
|
images = []
|
||||||
|
for path in media:
|
||||||
|
p = Path(path)
|
||||||
|
mime, _ = mimetypes.guess_type(path)
|
||||||
|
if not p.is_file() or not mime or not mime.startswith("image/"):
|
||||||
|
continue
|
||||||
|
b64 = base64.b64encode(p.read_bytes()).decode()
|
||||||
|
images.append({"type": "image_url", "image_url": {"url": f"data:{mime};base64,{b64}"}})
|
||||||
|
|
||||||
|
if not images:
|
||||||
|
return text
|
||||||
|
return images + [{"type": "text", "text": text}]
|
||||||
|
|
||||||
def add_tool_result(
|
def add_tool_result(
|
||||||
self,
|
self,
|
||||||
messages: list[dict[str, Any]],
|
messages: list[dict[str, Any]],
|
||||||
|
|||||||
@ -152,7 +152,8 @@ class AgentLoop:
|
|||||||
# Build initial messages (use get_history for LLM-formatted messages)
|
# Build initial messages (use get_history for LLM-formatted messages)
|
||||||
messages = self.context.build_messages(
|
messages = self.context.build_messages(
|
||||||
history=session.get_history(),
|
history=session.get_history(),
|
||||||
current_message=msg.content
|
current_message=msg.content,
|
||||||
|
media=msg.media if msg.media else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Agent loop
|
# Agent loop
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user