fix: manage direct chat output attachments

This commit is contained in:
0x-IHRR
2026-10-28 03:13:05 +08:00
parent 25bbc02a3b
commit 507ec78780
4 changed files with 57 additions and 15 deletions

View File

@@ -16,15 +16,23 @@ def _chat_exec_output(
) -> tuple[Path, WorkspaceItem]:
_, item = service.resolve_published_item(workspace_id, item_id)
parts = PurePosixPath(item.relative_path).parts
direct_chat_output = (
len(parts) == 5 and parts[:2] == ("outputs", "chat") and parts[3].startswith("turn_")
)
if (
not item.generated
or len(parts) < 10
or parts[:2] != ("outputs", "chat")
or not parts[3].startswith("turn_")
or parts[4] != "exec"
or parts[5] != "outputs"
or parts[6] != "chat"
or not parts[8].startswith("turn_")
or not (
direct_chat_output
or (
len(parts) >= 10
and parts[:2] == ("outputs", "chat")
and parts[3].startswith("turn_")
and parts[4] == "exec"
and parts[5] == "outputs"
and parts[6] == "chat"
and parts[8].startswith("turn_")
)
)
or any(part in {".", ".."} for part in parts)
):
raise WorkspaceError("Only generated chat tool outputs support file actions.")

View File

@@ -185,6 +185,28 @@ def test_chat_output_file_actions_preserve_the_attachment(chat_output_api):
assert client.get(item.url).content == b"<html>gap lock diagram</html>"
def test_direct_chat_output_file_actions_preserve_the_attachment(chat_output_api):
client, service, _, _ = chat_output_api
binding = service.current_binding()
relative = "outputs/chat/session/turn_run-123/showme-next-key-lock-range.html"
output = service.resolve(binding, relative, write=True)
output.parent.mkdir(parents=True)
output.write_text("<html>next-key-lock</html>")
item = service.publish(binding, [{"path": relative}])[0]
item = replace(item, url=urlsplit(item.url).path)
preview_response = client.get(item.url + "/file-storage")
assert preview_response.status_code == 200, preview_response.json()
preview = preview_response.json()
assert preview["file_path"] == str(output)
deleted = client.post(
item.url + "/remove-generated-output",
json={"fingerprint": preview["fingerprint"]},
)
assert deleted.status_code == 200, deleted.json()
assert not output.exists()
assert client.get(item.url).content == b"<html>next-key-lock</html>"
def test_chat_output_removal_refuses_changed_files(chat_output_api):
client, _, item, output = chat_output_api
preview = client.get(item.url + "/file-storage").json()

View File

@@ -21,19 +21,25 @@ interface FileStorage {
export function supportsGeneratedOutputActions(attachment: GeneratedOutputAttachment): boolean {
const parts = (attachment.relative_path || '').split('/')
const directChatOutput =
parts.length === 5 &&
parts[0] === 'outputs' &&
parts[1] === 'chat' &&
parts[3].startsWith('turn_')
return Boolean(
attachment.generated &&
/^\/files\/workspace-items\/ws_[0-9a-f]{32}\/wsi_[0-9a-f]{32}(?:\?[^#]*)?$/.test(
attachment.url || ''
) &&
parts.length >= 10 &&
parts[0] === 'outputs' &&
parts[1] === 'chat' &&
parts[3].startsWith('turn_') &&
parts[4] === 'exec' &&
parts[5] === 'outputs' &&
parts[6] === 'chat' &&
parts[8].startsWith('turn_')
(directChatOutput ||
(parts.length >= 10 &&
parts[0] === 'outputs' &&
parts[1] === 'chat' &&
parts[3].startsWith('turn_') &&
parts[4] === 'exec' &&
parts[5] === 'outputs' &&
parts[6] === 'chat' &&
parts[8].startsWith('turn_')))
)
}

View File

@@ -21,6 +21,11 @@ const attachment = {
relative_path:
'outputs/chat/session/turn_one/exec/outputs/chat/session/turn_one/gap-lock.html',
}
const directAttachment = {
generated: true,
url,
relative_path: 'outputs/chat/session/turn_run-123/showme-next-key-lock-range.html',
}
const storage = {
file_path: '/workspace/outputs/chat/session/turn/exec/outputs/gap-lock.html',
file_bytes: 6800,
@@ -33,6 +38,7 @@ beforeEach(() => mocks.fetch.mockReset())
it('offers actions only for generated chat exec outputs', () => {
expect(supportsGeneratedOutputActions(attachment)).toBe(true)
expect(supportsGeneratedOutputActions(directAttachment)).toBe(true)
expect(supportsGeneratedOutputActions({ ...attachment, generated: false })).toBe(false)
expect(
supportsGeneratedOutputActions({