-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(protocol): preserve media attachment names #3548
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
base: main
Are you sure you want to change the base?
Changes from all commits
b4de632
c2060ab
5a0ddd7
fe96535
54d002d
670f9ba
0dedb84
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,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Preserve image and video filenames in session history. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,30 +87,42 @@ function isFsError(error: unknown): boolean { | |
| return error instanceof Error && typeof (error as NodeJS.ErrnoException).code === 'string'; | ||
| } | ||
|
|
||
| export async function assertPromptSessionMediaRefs( | ||
| export async function resolvePromptSessionMediaRefs( | ||
| content: WireContent, | ||
| store: ISessionMediaStore, | ||
| ): Promise<void> { | ||
| ): Promise<WireContent> { | ||
| const resolved: WireContent = []; | ||
| let changed = false; | ||
| for (const part of content) { | ||
| if ( | ||
| (part.type !== 'image' && part.type !== 'video') || | ||
| part.source.kind !== 'session_media' | ||
| ) continue; | ||
| ) { | ||
| resolved.push(part); | ||
| continue; | ||
| } | ||
| const file = await store.open(part.source.file_id); | ||
| if (file === undefined) throw fileNotFoundError(part.source.file_id); | ||
| if (part.name === undefined) { | ||
| resolved.push({ ...part, name: file.name }); | ||
| changed = true; | ||
| } else { | ||
| resolved.push(part); | ||
| } | ||
| } | ||
| return changed ? resolved : content; | ||
| } | ||
|
|
||
| export function contentToCoreParts(content: WireContent): ContentPart[] { | ||
| const parts: ContentPart[] = []; | ||
| for (const part of content) { | ||
| if (part.type === 'text') parts.push({ type: 'text', text: part.text }); | ||
| else if (part.type === 'image' && part.source.kind === 'url') parts.push({ type: 'image_url', imageUrl: { url: part.source.url, id: part.source.id } }); | ||
| else if (part.type === 'image' && part.source.kind === 'base64') parts.push({ type: 'image_url', imageUrl: { url: `data:${part.source.media_type};base64,${part.source.data}` } }); | ||
| else if (part.type === 'image' && part.source.kind === 'session_media') parts.push({ type: 'image_url', imageUrl: { url: buildDaemonFileUrl(part.source.file_id), id: part.source.file_id } }); | ||
| else if (part.type === 'video' && part.source.kind === 'url') parts.push({ type: 'video_url', videoUrl: { url: part.source.url, id: part.source.id } }); | ||
| else if (part.type === 'video' && part.source.kind === 'base64') parts.push({ type: 'video_url', videoUrl: { url: `data:${part.source.media_type};base64,${part.source.data}` } }); | ||
| else if (part.type === 'video' && part.source.kind === 'session_media') parts.push({ type: 'video_url', videoUrl: { url: buildDaemonFileUrl(part.source.file_id), id: part.source.file_id } }); | ||
| else if (part.type === 'image' && part.source.kind === 'url') parts.push({ type: 'image_url', imageUrl: { url: part.source.url, id: part.source.id, name: part.name } }); | ||
| else if (part.type === 'image' && part.source.kind === 'base64') parts.push({ type: 'image_url', imageUrl: { url: `data:${part.source.media_type};base64,${part.source.data}`, name: part.name } }); | ||
| else if (part.type === 'image' && part.source.kind === 'session_media') parts.push({ type: 'image_url', imageUrl: { url: buildDaemonFileUrl(part.source.file_id), id: part.source.file_id, name: part.name } }); | ||
| else if (part.type === 'video' && part.source.kind === 'url') parts.push({ type: 'video_url', videoUrl: { url: part.source.url, id: part.source.id, name: part.name } }); | ||
| else if (part.type === 'video' && part.source.kind === 'base64') parts.push({ type: 'video_url', videoUrl: { url: `data:${part.source.media_type};base64,${part.source.data}`, name: part.name } }); | ||
| else if (part.type === 'video' && part.source.kind === 'session_media') parts.push({ type: 'video_url', videoUrl: { url: buildDaemonFileUrl(part.source.file_id), id: part.source.file_id, name: part.name } }); | ||
| } | ||
| return parts; | ||
| } | ||
|
|
@@ -172,10 +184,10 @@ export async function resolvePromptMediaFiles( | |
| ); | ||
| if (!isModelAcceptedImageMime(effectiveMime)) { | ||
| const bytes = Buffer.from(part.source.data, 'base64'); | ||
| const name = `image.${imageExtensionForMime(effectiveMime)}`; | ||
| const name = part.name ?? `image.${imageExtensionForMime(effectiveMime)}`; | ||
| const persisted = await persistAttachmentBytes( | ||
| bytes, | ||
| `${createHash('sha256').update(bytes).digest('hex').slice(0, 32)}-${name}`, | ||
| `${createHash('sha256').update(bytes).digest('hex').slice(0, 32)}-${sanitizeAttachmentName(name)}`, | ||
| await resolveAttachmentsDir(), | ||
| ); | ||
| content.push({ | ||
|
|
@@ -223,6 +235,7 @@ export async function resolvePromptMediaFiles( | |
| content.push({ | ||
| type: 'image', | ||
| source: { kind: 'base64', media_type: compressed.mimeType, data: compressed.base64 }, | ||
| name: part.name, | ||
|
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.
When a named base64 image resolves to a MIME type unsupported by the model, the earlier branch returns before reaching this preserved-name path and constructs the resulting file attachment with the generic Useful? React with 👍 / 👎. |
||
| }); | ||
| changed = true; | ||
| } else { | ||
|
|
@@ -288,7 +301,7 @@ export async function resolvePromptMediaFiles( | |
| if (isFsError(error)) throw fileNotFoundError(sourcePath); | ||
| throw error; | ||
| }); | ||
| const name = basename(sourcePath); | ||
| const name = part.name ?? basename(sourcePath); | ||
| const declared = pathMediaMime(sourcePath, data, 'image'); | ||
| if (!declared.startsWith('image/')) { | ||
| throw new Error2('validation.failed', `${sourcePath} is ${declared}, not an image`); | ||
|
|
@@ -337,6 +350,7 @@ export async function resolvePromptMediaFiles( | |
| content.push({ | ||
| type: 'image', | ||
| source: { kind: 'url', url: buildDaemonFileUrl(saved.id) }, | ||
| name: part.name ?? name, | ||
| }); | ||
| changed = true; | ||
| continue; | ||
|
|
@@ -359,6 +373,7 @@ export async function resolvePromptMediaFiles( | |
| content.push({ | ||
| type: 'video', | ||
| source: { kind: 'url', url: buildDaemonFileUrl(saved.id) }, | ||
| name: part.name ?? basename(sourcePath), | ||
| }); | ||
| changed = true; | ||
| continue; | ||
|
|
@@ -376,20 +391,21 @@ export async function resolvePromptMediaFiles( | |
| let mediaType = file.meta.media_type; | ||
| mediaType = resolveEffectiveImageMime(mediaType, data); | ||
| if (!isModelAcceptedImageMime(mediaType)) { | ||
| const name = part.name ?? file.meta.name; | ||
| const persisted = await persistAttachmentBytes( | ||
| data, | ||
| `${file.meta.id}-${sanitizeAttachmentName(file.meta.name)}`, | ||
| `${file.meta.id}-${sanitizeAttachmentName(name)}`, | ||
| await resolveAttachmentsDir(), | ||
| ); | ||
| content.push({ | ||
| type: 'text', | ||
| text: persisted === null | ||
| ? buildUnsupportedImageNotice(mediaType, file.meta.name) | ||
| : buildAttachedFileNotice(file.meta.name, mediaType, file.meta.size, persisted), | ||
| ? buildUnsupportedImageNotice(mediaType, name) | ||
| : buildAttachedFileNotice(name, mediaType, file.meta.size, persisted), | ||
| }); | ||
| if (persisted !== null) { | ||
| attachments.push({ | ||
| name: file.meta.name, | ||
| name, | ||
| mediaType, | ||
| size: file.meta.size, | ||
| path: persisted, | ||
|
|
@@ -438,6 +454,7 @@ export async function resolvePromptMediaFiles( | |
| content.push({ | ||
| type: 'image', | ||
| source: { kind: 'url', url: buildDaemonFileUrl(finalFile.meta.id) }, | ||
| name: part.name ?? file.meta.name, | ||
|
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.
For file-backed images and videos, this now emits a non-empty AGENTS.md reference: AGENTS.md:L61-L61 Useful? React with 👍 / 👎. 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.
When a named Useful? React with 👍 / 👎. |
||
| }); | ||
| changed = true; | ||
| continue; | ||
|
|
@@ -446,6 +463,7 @@ export async function resolvePromptMediaFiles( | |
| content.push({ | ||
| type: 'video', | ||
| source: { kind: 'url', url: buildDaemonFileUrl(file.meta.id) }, | ||
| name: part.name ?? file.meta.name, | ||
| }); | ||
| changed = true; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,12 +47,14 @@ export type ImageSource = z.infer<typeof imageSourceSchema>; | |
| export const imageContentSchema = z.object({ | ||
| type: z.literal('image'), | ||
| source: imageSourceSchema, | ||
| name: z.string().min(1).optional(), | ||
|
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.
When a consumer parses prompt responses, message history, or Useful? React with 👍 / 👎. |
||
| }); | ||
| export type ImageContent = z.infer<typeof imageContentSchema>; | ||
|
|
||
| export const videoContentSchema = z.object({ | ||
| type: z.literal('video'), | ||
| source: imageSourceSchema, | ||
| name: z.string().min(1).optional(), | ||
| }); | ||
| export type VideoContent = z.infer<typeof videoContentSchema>; | ||
|
|
||
|
|
||
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.
When a client reuses an existing
session_mediaattachment without the new optionalnamefield—as existing clients and historical prompt payloads do—this conversion copiesundefinedeven thoughassertPromptSessionMediaRefsopens aSessionMediaFilewhose persisted metadata includes the original name. Consequently the core part,turn.startedattachment, and reconstructed transcript remain nameless; enrich session-media parts from the store metadata when the request omits the name, including the analogous video path.Useful? React with 👍 / 👎.