-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(harness): file attachment support with filename preservation and text file handling #13574
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
Changes from all commits
7b451d2
f5118bd
81274b8
434fb31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| '@mastra/core': patch | ||
| 'mastracode': patch | ||
| --- | ||
|
|
||
| **`sendMessage` now accepts `files` instead of `images`**, supporting any file type with optional `filename`. | ||
|
|
||
| **Breaking change:** Rename `images` to `files` when calling `harness.sendMessage()`: | ||
|
|
||
| ```ts | ||
| // Before | ||
| await harness.sendMessage({ | ||
| content: "Analyze this", | ||
| images: [{ data: base64Data, mimeType: "image/png" }], | ||
| }); | ||
|
|
||
| // After | ||
| await harness.sendMessage({ | ||
| content: "Analyze this", | ||
| files: [{ data: base64Data, mediaType: "image/png", filename: "screenshot.png" }], | ||
| }); | ||
| ``` | ||
|
|
||
| - `files` accepts `{ data, mediaType, filename? }` — filenames are now preserved through storage and message history | ||
| - Text-based files (`text/*`, `application/json`) are automatically decoded to readable text content instead of being sent as binary, which models could not process | ||
| - `HarnessMessageContent` now includes a `file` type, so file parts round-trip correctly through message history |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1059,12 +1059,12 @@ export class Harness<TState extends HarnessStateSchema = HarnessStateSchema> { | |||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||
| async sendMessage({ | ||||||||||||||||||||||||||||||||||||||||||||||
| content, | ||||||||||||||||||||||||||||||||||||||||||||||
| images, | ||||||||||||||||||||||||||||||||||||||||||||||
| files, | ||||||||||||||||||||||||||||||||||||||||||||||
| tracingContext, | ||||||||||||||||||||||||||||||||||||||||||||||
| tracingOptions, | ||||||||||||||||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||||||||||||||||
| content: string; | ||||||||||||||||||||||||||||||||||||||||||||||
| images?: Array<{ data: string; mimeType: string }>; | ||||||||||||||||||||||||||||||||||||||||||||||
| files?: Array<{ data: string; mediaType: string; filename?: string }>; | ||||||||||||||||||||||||||||||||||||||||||||||
| tracingContext?: TracingContext; | ||||||||||||||||||||||||||||||||||||||||||||||
| tracingOptions?: TracingOptions; | ||||||||||||||||||||||||||||||||||||||||||||||
| }): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1098,17 +1098,33 @@ export class Harness<TState extends HarnessStateSchema = HarnessStateSchema> { | |||||||||||||||||||||||||||||||||||||||||||||
| streamOptions.toolsets = await this.buildToolsets(requestContext); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| let messageInput: string | Record<string, unknown> = content; | ||||||||||||||||||||||||||||||||||||||||||||||
| if (images?.length) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (files?.length) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const fileParts = files.map(f => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const isText = f.mediaType.startsWith('text/') || f.mediaType === 'application/json'; | ||||||||||||||||||||||||||||||||||||||||||||||
| if (isText) { | ||||||||||||||||||||||||||||||||||||||||||||||
| let textContent = f.data; | ||||||||||||||||||||||||||||||||||||||||||||||
| // Decode data URI to plain text | ||||||||||||||||||||||||||||||||||||||||||||||
| const base64Match = f.data.match(/^data:[^;]*;base64,(.*)$/); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (base64Match) { | ||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||
| textContent = Buffer.from(base64Match[1]!, 'base64').toString('utf-8'); | ||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||
| // Fall through with raw data | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1106
to
+1114
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decode non-base64 data URIs for text attachments. Line 1100 only decodes 💡 Proposed fix- // Decode data URI to plain text
- const base64Match = f.data.match(/^data:[^;]*;base64,(.*)$/);
- if (base64Match) {
- try {
- textContent = Buffer.from(base64Match[1]!, 'base64').toString('utf-8');
- } catch {
- // Fall through with raw data
- }
- }
+ // Decode data URI to plain text (supports base64 and url-encoded forms)
+ const dataUriMatch = f.data.match(/^data:([^,]*),(.*)$/s);
+ if (dataUriMatch) {
+ const meta = dataUriMatch[1] ?? '';
+ const payload = dataUriMatch[2] ?? '';
+ try {
+ textContent = /;base64/i.test(meta)
+ ? Buffer.from(payload, 'base64').toString('utf-8')
+ : decodeURIComponent(payload);
+ } catch {
+ // Fall through with raw data
+ }
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
| const label = f.filename ? `[File: ${f.filename}]` : '[Attached file]'; | ||||||||||||||||||||||||||||||||||||||||||||||
| return { type: 'text' as const, text: `${label}\n\`\`\`\n${textContent}\n\`\`\`` }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| type: 'file' as const, | ||||||||||||||||||||||||||||||||||||||||||||||
| data: f.data, | ||||||||||||||||||||||||||||||||||||||||||||||
| mimeType: f.mediaType, | ||||||||||||||||||||||||||||||||||||||||||||||
| filename: f.filename, | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| messageInput = { | ||||||||||||||||||||||||||||||||||||||||||||||
| role: 'user', | ||||||||||||||||||||||||||||||||||||||||||||||
| content: [ | ||||||||||||||||||||||||||||||||||||||||||||||
| { type: 'text', text: content }, | ||||||||||||||||||||||||||||||||||||||||||||||
| ...images.map((img: { data: string; mimeType: string }) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||
| type: 'file', | ||||||||||||||||||||||||||||||||||||||||||||||
| data: img.data, | ||||||||||||||||||||||||||||||||||||||||||||||
| mediaType: img.mimeType, | ||||||||||||||||||||||||||||||||||||||||||||||
| })), | ||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||
| content: [{ type: 'text', text: content }, ...fileParts], | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1297,6 +1313,32 @@ export class Harness<TState extends HarnessStateSchema = HarnessStateSchema> { | |||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| case 'file': | ||||||||||||||||||||||||||||||||||||||||||||||
| content.push({ | ||||||||||||||||||||||||||||||||||||||||||||||
| type: 'file', | ||||||||||||||||||||||||||||||||||||||||||||||
| data: typeof part.data === 'string' ? part.data : '', | ||||||||||||||||||||||||||||||||||||||||||||||
| mediaType: | ||||||||||||||||||||||||||||||||||||||||||||||
| (part as { mediaType?: string }).mediaType ?? | ||||||||||||||||||||||||||||||||||||||||||||||
| (part as { mimeType?: string }).mimeType ?? | ||||||||||||||||||||||||||||||||||||||||||||||
| 'application/octet-stream', | ||||||||||||||||||||||||||||||||||||||||||||||
| ...((part as { filename?: string }).filename ? { filename: (part as { filename?: string }).filename } : {}), | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||
| case 'image': { | ||||||||||||||||||||||||||||||||||||||||||||||
| const imgData = | ||||||||||||||||||||||||||||||||||||||||||||||
| typeof part.data === 'string' | ||||||||||||||||||||||||||||||||||||||||||||||
| ? part.data | ||||||||||||||||||||||||||||||||||||||||||||||
| : typeof (part as { image?: string }).image === 'string' | ||||||||||||||||||||||||||||||||||||||||||||||
| ? (part as { image?: string }).image! | ||||||||||||||||||||||||||||||||||||||||||||||
| : ''; | ||||||||||||||||||||||||||||||||||||||||||||||
| content.push({ | ||||||||||||||||||||||||||||||||||||||||||||||
| type: 'file', | ||||||||||||||||||||||||||||||||||||||||||||||
| data: imgData, | ||||||||||||||||||||||||||||||||||||||||||||||
| mediaType: | ||||||||||||||||||||||||||||||||||||||||||||||
| (part as { mimeType?: string }).mimeType ?? (part as { mediaType?: string }).mediaType ?? 'image/png', | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1334
to
+1340
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve filename when normalizing This branch converts 💡 Suggested fix content.push({
type: 'file',
data: imgData,
mediaType:
(part as { mimeType?: string }).mimeType ?? (part as { mediaType?: string }).mediaType ?? 'image/png',
+ ...((part as { filename?: string }).filename ? { filename: (part as { filename?: string }).filename } : {}),
});🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| // Skip other part types (step-start, data-om-status, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.