-
Notifications
You must be signed in to change notification settings - Fork 116
feat(run): enable inline text document attachments via chat APIs #2821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ac073fd
feat(run): add inline text document attachments to chat APIs
mike-inkeep 3aff3eb
Review feedback and merge main
mike-inkeep 6a81bf4
Merge remote-tracking branch 'origin/main' into feat/txt_attachments
mike-inkeep 885b936
Merge remote-tracking branch 'origin/main' into feat/txt_attachments
mike-inkeep a338dbc
PR feedback
mike-inkeep 10473b9
Minor fix
mike-inkeep ed38ae7
Allow .json text file attachments
mike-inkeep 74253ec
Fix unrelated tracing timestamp bug
mike-inkeep 02c7740
Merge remote-tracking branch 'origin/main' into feat/txt_attachments
mike-inkeep 134d609
Tighten up route validation
mike-inkeep 012f81d
Merge branch 'main' into feat/txt_attachments
mike-inkeep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'agents-api': patch | ||
| --- | ||
|
|
||
| Add inline text document attachments to the run chat APIs for `text/plain`, `text/markdown`, `text/html`, `text/csv`, `text/x-log`, and `application/json` while keeping remote URLs limited to PDFs. Persist text attachments as blob-backed file parts and replay them into model input as XML-tagged text blocks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
agents-api/src/__tests__/run/agents/conversation-history.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const { downloadMock } = vi.hoisted(() => ({ downloadMock: vi.fn() })); | ||
|
|
||
| vi.mock('../../../../logger', () => ({ | ||
| getLogger: () => ({ warn: vi.fn(), info: vi.fn(), debug: vi.fn(), error: vi.fn() }), | ||
| })); | ||
|
|
||
| vi.mock('../../../domains/run/services/blob-storage', async (importOriginal) => { | ||
| const actual = | ||
| await importOriginal<typeof import('../../../domains/run/services/blob-storage')>(); | ||
| return { ...actual, getBlobStorageProvider: () => ({ download: downloadMock }) }; | ||
| }); | ||
|
|
||
| import { buildUserMessageContent } from '../../../domains/run/agents/generation/conversation-history'; | ||
|
|
||
| describe('buildUserMessageContent', () => { | ||
| it('injects inline text attachments as XML attachment blocks', async () => { | ||
| const content = await buildUserMessageContent('Please summarize this', [ | ||
| { | ||
| kind: 'file', | ||
| file: { | ||
| bytes: Buffer.from('# Title\r\n\r\nHello world', 'utf8').toString('base64'), | ||
| mimeType: 'text/markdown', | ||
| }, | ||
| metadata: { | ||
| filename: 'notes.md', | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(content).toEqual([ | ||
| { type: 'text', text: 'Please summarize this' }, | ||
| { | ||
| type: 'text', | ||
| text: [ | ||
| '<attached_file filename="notes.md" media_type="text/markdown">', | ||
| '# Title', | ||
| '', | ||
| 'Hello world', | ||
| '</attached_file>', | ||
| ].join('\n'), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('downloads and injects blob-backed text attachments', async () => { | ||
| downloadMock.mockResolvedValue({ | ||
| data: Buffer.from('# Title\n\nHello from blob', 'utf8'), | ||
| mimeType: 'text/markdown', | ||
| }); | ||
|
|
||
| const content = await buildUserMessageContent('Summarize this', [ | ||
| { | ||
| kind: 'file', | ||
| file: { | ||
| uri: 'blob://v1/t_tenant/media/p_proj/conv/c_conv/m_msg/sha256-abc123', | ||
| mimeType: 'text/markdown', | ||
| }, | ||
| metadata: { filename: 'notes.md' }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(downloadMock).toHaveBeenCalledWith( | ||
| 'v1/t_tenant/media/p_proj/conv/c_conv/m_msg/sha256-abc123' | ||
| ); | ||
| expect(content).toEqual([ | ||
| { type: 'text', text: 'Summarize this' }, | ||
| { | ||
| type: 'text', | ||
| text: [ | ||
| '<attached_file filename="notes.md" media_type="text/markdown">', | ||
| '# Title', | ||
| '', | ||
| 'Hello from blob', | ||
| '</attached_file>', | ||
| ].join('\n'), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('injects inline JSON attachments as XML attachment blocks', async () => { | ||
| const content = await buildUserMessageContent('Summarize this payload', [ | ||
| { | ||
| kind: 'file', | ||
| file: { | ||
| bytes: Buffer.from('{"items":[1,2,3]}\n', 'utf8').toString('base64'), | ||
| mimeType: 'application/json', | ||
| }, | ||
| metadata: { | ||
| filename: 'payload.json', | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(content).toEqual([ | ||
| { type: 'text', text: 'Summarize this payload' }, | ||
| { | ||
| type: 'text', | ||
| text: [ | ||
| '<attached_file filename="payload.json" media_type="application/json">', | ||
| '{"items":[1,2,3]}', | ||
| '', | ||
| '</attached_file>', | ||
| ].join('\n'), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns unavailable block when blob download fails', async () => { | ||
| downloadMock.mockRejectedValue(new Error('Blob not found')); | ||
|
|
||
| const content = await buildUserMessageContent('Summarize this', [ | ||
| { | ||
| kind: 'file', | ||
| file: { | ||
| uri: 'blob://v1/t_tenant/media/p_proj/conv/c_conv/m_msg/sha256-abc123', | ||
| mimeType: 'text/markdown', | ||
| }, | ||
| metadata: { filename: 'notes.md' }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(content).toEqual([ | ||
| { type: 'text', text: 'Summarize this' }, | ||
| { | ||
| type: 'text', | ||
| text: expect.stringContaining('[Attachment unavailable]'), | ||
| }, | ||
| ]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 MAJOR: Missing test for blob-backed text attachment replay
Issue: This test only covers the inline bytes path (
file.bytespresent). The blob URI download path (file.uriwithblob://scheme) at lines 134-136 of conversation-history.ts has no test coverage.Why: If the blob download integration is broken (wrong URI parsing, missing await, mock not configured), text attachments will fail during conversation history replay. Users would see errors like "Blob not found" on subsequent turns after uploading text files.
Fix: Add a test for blob-backed attachments:
Refs: