Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions ex_app/lib/nc_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,39 @@ def _task_tool_calls(self, task: Task) -> tuple[list[dict[str, typing.Any]], lis

return default_tool_parser(raw_tool_calls)

def _task_to_message(self, task: Task) -> AIMessage:
async def _save_output_attachments(self, task: Task) -> list[dict[str, typing.Any]]:
"""
Save the task's output files to the user's Assistant folder and return them as file parts

Task output files live in appdata and cannot be used by tools, the saved copies in the user's folder can.
If saving fails (e.g. the Assistant app is missing or too old), the part keeps referencing the task output file.
"""
output_attachments = []
if isinstance(task.output, dict):
output_attachments = task.output.get('output_attachments') or []
parts = []
for file_id in output_attachments:
try:
saved = await self.nc.ocs(
"POST",
f"/ocs/v2.php/apps/assistant/api/v1/task/{task.id}/file/{int(file_id)}/save",
)
parts.append({"type": "file", "file_id": int(saved['fileId'])})
except Exception as e:
await log(self.nc, LogLvl.WARNING, f"Failed to save output file {file_id} of task {task.id} to the Assistant folder: {e}")
parts.append({"type": "file", "file_id": int(file_id), "ocp_task_id": task.id})
return parts

async def _task_to_message(self, task: Task) -> AIMessage:
if not isinstance(task.output, dict) or "output" not in task.output:
raise Exception('"output" key not found in Nextcloud TaskProcessing task result')

output_text = task.output['output']
output_attachments = task.output.get('output_attachments', [])
content = build_multimodal_content(output_text, output_attachments, task.id)
content: list[dict[str, typing.Any]] | str = await self._save_output_attachments(task)
if not content:
content = output_text
elif output_text:
content.append({"type": "text", "text": output_text})

tool_calls, invalid_tool_calls = self._task_tool_calls(task)
if len(tool_calls) > 0 or len(invalid_tool_calls) > 0:
Expand Down Expand Up @@ -356,7 +382,7 @@ async def _agenerate(
"The language model did not respond in time. Please try again.",
)

message = self._task_to_message(task)
message = await self._task_to_message(task)
return ChatResult(generations=[ChatGeneration(message=message)])

async def _astream(
Expand Down Expand Up @@ -421,15 +447,12 @@ async def _astream(
yield ChatGenerationChunk(message=AIMessageChunk(content=final_delta))

tool_calls, invalid_tool_calls = self._task_tool_calls(task)
output_attachments = []
if isinstance(task.output, dict):
output_attachments = task.output.get('output_attachments') or []
content = await self._save_output_attachments(task)

if output_attachments:
if content:
# Append file parts after streamed text so LangChain merges them
# into the final AIMessage content (attachments are not streamed).
yielded_chunk = True
content = [{"type": "file", "file_id": int(file_id), "ocp_task_id": task.id} for file_id in output_attachments]
yield ChatGenerationChunk(message=AIMessageChunk(
content=content,
tool_calls=tool_calls,
Expand Down
50 changes: 48 additions & 2 deletions tests/test_graph_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_build_task_input_and_task_to_message():
assert task_input["input"] == "and tomorrow?"
assert len(task_input["history"]) == 3

message = model._task_to_message(
message = asyncio.run(model._task_to_message(
Response.model_validate(
{
"task": {
Expand All @@ -78,12 +78,58 @@ def test_build_task_input_and_task_to_message():
}
}
).task
)
))
assert isinstance(message, AIMessage)
assert message.content == "it is sunny"
assert message.tool_calls[0]["name"] == "get_weather"


class _FakeNc:
"""Answers the Assistant app's save endpoint, failing for the given file ids."""

def __init__(self, failing_file_ids=()):
self.failing_file_ids = set(failing_file_ids)
self.calls = []

async def ocs(self, method, path, **kwargs):
self.calls.append((method, path))
file_id = int(path.split("/file/")[1].split("/")[0])
if file_id in self.failing_file_ids:
raise Exception("Not found")
return {"fileId": file_id + 1000, "path": f"/Assistant/{file_id}.png", "mime": "image/png"}


def _task_with_attachments(file_ids):
return Response.model_validate(
{"task": {"id": 7, "status": "STATUS_SUCCESSFUL", "output": {"output": "here", "output_attachments": file_ids}}}
).task


def test_task_to_message_saves_output_attachments_to_assistant_folder():
nc = _FakeNc()
model = ChatWithNextcloud.model_construct(nc=nc)
message = asyncio.run(model._task_to_message(_task_with_attachments([1, 2])))
assert nc.calls == [
("POST", "/ocs/v2.php/apps/assistant/api/v1/task/7/file/1/save"),
("POST", "/ocs/v2.php/apps/assistant/api/v1/task/7/file/2/save"),
]
assert message.content == [
{"type": "file", "file_id": 1001},
{"type": "file", "file_id": 1002},
{"type": "text", "text": "here"},
]


def test_task_to_message_keeps_task_reference_when_saving_fails():
model = ChatWithNextcloud.model_construct(nc=_FakeNc(failing_file_ids={2}))
message = asyncio.run(model._task_to_message(_task_with_attachments([1, 2])))
assert message.content == [
{"type": "file", "file_id": 1001},
{"type": "file", "file_id": 2, "ocp_task_id": 7},
{"type": "text", "text": "here"},
]


def test_serializer_roundtrip_token_shape():
serde = JsonPlusSerializer()
state = {
Expand Down
Loading