Skip to content

Commit e248104

Browse files
committed
Reference: embed a ref summary
1 parent c10558f commit e248104

7 files changed

Lines changed: 36 additions & 18 deletions

File tree

src/apps/chat/components/message/fragments-attachment-image/ImageAttachmentFragments.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function ImageAttachmentFragments(props: {
116116
dataRefBytesSize={legacy.dataRef.bytesSize}
117117
imageWidth={legacy.width}
118118
imageHeight={legacy.height}
119-
imageAltText={legacy.altText || title}
119+
imageAltText={part.zRefSummary?.text || legacy.altText || title}
120120
disabled={props.disabled}
121121
onDeleteFragment={!props.onFragmentDelete ? undefined : () => props.onFragmentDelete?.(fId)}
122122
onViewImage={() => setViewingImageRefPart(legacy)}

src/apps/chat/components/message/fragments-content/ViewImageRefPartModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function ViewImageRefPartModal(props: {
8989
}
9090
}, [dataRef, altText]);
9191

92-
const title = props.imageRefPart.altText || 'Attachment Image';
92+
const title = altText || 'Attachment Image';
9393
return (
9494
<GoodModal
9595
open={true}

src/common/attachment-drafts/attachment.dblobs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export async function imageDataToImageAttachmentFragmentViaDBlob(
6969
return createZyncAssetReferenceAttachmentFragment(
7070
title, caption,
7171
nanoidToUuidV4(dblobAssetId, 'convert-dblob-to-dasset'),
72+
title || (source.media === 'file' ? source.refPath : source.media === 'url' ? source.refUrl : undefined), // use title if available, otherwise use the source refPath or refUrl
7273
'image',
7374
{
7475
pt: 'image_ref' as const,

src/common/stores/chat/chat.fragments.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { LiveFileId } from '~/common/livefile/liveFile.types';
22
import { agiId } from '~/common/util/idUtils';
3+
import { ellipsizeMiddle } from '~/common/util/textUtils';
34

45

56
/// Fragments - forward compatible ///
@@ -132,14 +133,15 @@ export type DMessageReferencePart =
132133
type _DMessageReferencePartBase<TRt extends string, TRefSpecificFields = {}> = {
133134
pt: 'reference';
134135
rt: TRt;
135-
// altText?: string;
136136
} & TRefSpecificFields;
137137

138138
type _DMessageZyncReferencePart<TZT extends ZYNC.Typename, TZTSpecificFields = {}> = _DMessageReferencePartBase<'zync', {
139139
zType: TZT;
140140
zUuid: ZYNC_Entity.UUID;
141141
// zRelationship: 'live', ...
142+
zRefSummary?: DMessageTextPart; // text summary of the reference for text-only models and accessibility
142143
} & TZTSpecificFields>;
144+
const MAX_ZYNC_REFERENCE_SUMMARY_LEN = 512; // max alt text length for Zync Asset Reference Parts
143145

144146
export type DMessageZyncAssetReferencePart = _DMessageZyncReferencePart<'asset', {
145147
// denorm fields for quick display
@@ -330,8 +332,8 @@ export function createErrorContentFragment(error: string): DMessageContentFragme
330332
return _createContentFragment(_create_Error_Part(error));
331333
}
332334

333-
export function createZyncAssetReferenceContentFragment(assetUuid: ZYNC_Entity.UUID, assetType: 'image' | 'audio', legacyImageRefPart?: DMessageZyncAssetReferencePart['_legacyImageRefPart']): DMessageContentFragment {
334-
return _createContentFragment(createDMessageZyncAssetReferencePart(assetUuid, assetType, legacyImageRefPart));
335+
export function createZyncAssetReferenceContentFragment(assetUuid: ZYNC_Entity.UUID, refSummary: string | undefined, assetType: 'image' | 'audio', legacyImageRefPart?: DMessageZyncAssetReferencePart['_legacyImageRefPart']): DMessageContentFragment {
336+
return _createContentFragment(createDMessageZyncAssetReferencePart(assetUuid, refSummary, assetType, legacyImageRefPart));
335337
}
336338

337339
export function create_FunctionCallInvocation_ContentFragment(id: string, functionName: string, args: string /*| null*/): DMessageContentFragment {
@@ -357,8 +359,8 @@ function _createContentFragment(part: DMessageContentFragment['part']): DMessage
357359

358360
/// Attachment Fragments - Creation & Duplication
359361

360-
export function createZyncAssetReferenceAttachmentFragment(title: string, caption: string, assetUuid: ZYNC_Entity.UUID, assetType: 'image' | 'audio', legacyImageRefPart?: DMessageZyncAssetReferencePart['_legacyImageRefPart']): DMessageAttachmentFragment {
361-
return _createAttachmentFragment(title, caption, createDMessageZyncAssetReferencePart(assetUuid, assetType, legacyImageRefPart), undefined);
362+
export function createZyncAssetReferenceAttachmentFragment(title: string, caption: string, assetUuid: ZYNC_Entity.UUID, refSummary: string | undefined, assetType: 'image' | 'audio', legacyImageRefPart?: DMessageZyncAssetReferencePart['_legacyImageRefPart']): DMessageAttachmentFragment {
363+
return _createAttachmentFragment(title, caption, createDMessageZyncAssetReferencePart(assetUuid, refSummary, assetType, legacyImageRefPart), undefined);
362364
}
363365

364366
export function createDocAttachmentFragment(l1Title: string, caption: string, vdt: DMessageDocMimeType, data: DMessageDataInline, ref: string, version: number, meta?: DMessageDocMeta, liveFileId?: LiveFileId): DMessageAttachmentFragment {
@@ -370,7 +372,7 @@ export function specialContentPartToDocAttachmentFragment(title: string, caption
370372
case isTextPart(contentPart):
371373
return createDocAttachmentFragment(title, caption, vdt, createDMessageDataInlineText(contentPart.text, 'text/plain'), ref, 2 /* As we attach our messages, we start from 2 */, docMeta);
372374
case isZyncAssetReferencePart(contentPart):
373-
return createZyncAssetReferenceAttachmentFragment(title, caption, contentPart.zUuid, contentPart.assetType, contentPart._legacyImageRefPart);
375+
return createZyncAssetReferenceAttachmentFragment(title, caption, contentPart.zUuid, contentPart.zRefSummary?.text, contentPart.assetType, contentPart._legacyImageRefPart);
374376
default:
375377
return createDocAttachmentFragment('Error', 'Content to Attachment', vdt, createDMessageDataInlineText(`Conversion of '${contentPart.pt}' is not supported yet.`, 'text/plain'), ref, 1 /* error has no version really */, docMeta);
376378
}
@@ -446,8 +448,16 @@ function _create_Error_Part(error: string): DMessageErrorPart {
446448
return { pt: 'error', error };
447449
}
448450

449-
export function createDMessageZyncAssetReferencePart(zUuid: ZYNC_Entity.UUID, assetType: 'image' | 'audio', legacyImageRefPart?: DMessageZyncAssetReferencePart['_legacyImageRefPart']): DMessageZyncAssetReferencePart {
450-
return { pt: 'reference', rt: 'zync', zType: 'asset', zUuid, assetType, ...(legacyImageRefPart && { _legacyImageRefPart: { ...legacyImageRefPart } }) };
451+
export function createDMessageZyncAssetReferencePart(zUuid: ZYNC_Entity.UUID, refSummary: string | undefined, assetType: 'image' | 'audio', legacyImageRefPart?: DMessageZyncAssetReferencePart['_legacyImageRefPart']): DMessageZyncAssetReferencePart {
452+
return {
453+
pt: 'reference',
454+
rt: 'zync',
455+
zType: 'asset',
456+
zUuid,
457+
...(refSummary && { zRefSummary: { pt: 'text', text: ellipsizeMiddle(refSummary, MAX_ZYNC_REFERENCE_SUMMARY_LEN) } }),
458+
assetType,
459+
...(legacyImageRefPart && { _legacyImageRefPart: { ...legacyImageRefPart } }),
460+
};
451461
}
452462

453463
function _create_Doc_Part(vdt: DMessageDocMimeType, data: DMessageDataInline, ref: string, l1Title: string, version: number, meta?: DMessageDocMeta): DMessageDocPart {
@@ -522,7 +532,7 @@ function _duplicate_Part<TPart extends (DMessageContentFragment | DMessageAttach
522532
switch (part.zType) {
523533
case 'asset':
524534
// Zync Asset Reference: new fragment, with the exact same reference (and fallback, if still in the migration period)
525-
return createDMessageZyncAssetReferencePart(part.zUuid, part.assetType, part._legacyImageRefPart ? { ...part._legacyImageRefPart } : undefined) as TPart;
535+
return createDMessageZyncAssetReferencePart(part.zUuid, part.zRefSummary?.text, part.assetType, part._legacyImageRefPart ? { ...part._legacyImageRefPart } : undefined) as TPart;
526536

527537
default:
528538
const _exhaustiveCheck: never = part.zType;

src/common/stores/chat/chats.converters.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,18 @@ export namespace V4ToHeadConverters {
7171
// [ASSET] [MIGRATION] Convert DBlob image references to Asset references - converts legacy image_ref parts with dblob references to the new reference system
7272
if (isContentOrAttachmentFragment(fragment) && isImageRefPart(fragment.part) && fragment.part.dataRef?.reftype === 'dblob') {
7373
const { dataRef, altText, width, height } = fragment.part;
74-
const newReferencePart = createDMessageZyncAssetReferencePart(nanoidToUuidV4(dataRef.dblobAssetId, 'convert-dblob-to-dasset'), 'image', {
75-
pt: 'image_ref' as const,
76-
dataRef: dataRef,
77-
...(altText ? { altText: altText } : {}),
78-
...(width ? { width: width } : {}),
79-
...(height ? { height: height } : {}),
80-
});
74+
const newReferencePart = createDMessageZyncAssetReferencePart(
75+
nanoidToUuidV4(dataRef.dblobAssetId, 'convert-dblob-to-dasset'),
76+
altText,
77+
'image',
78+
{
79+
pt: 'image_ref' as const,
80+
dataRef: dataRef,
81+
...(altText ? { altText: altText } : {}),
82+
...(width ? { width: width } : {}),
83+
...(height ? { height: height } : {}),
84+
}
85+
);
8186
m.fragments[i] = { ...fragment, part: newReferencePart };
8287
}
8388

src/modules/aix/client/ContentReassembler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ export class ContentReassembler {
516516
// Create a Zync Image Asset Reference *Content* fragment, as this is image content from the LLM
517517
const zyncImageAssetFragmentWithLegacy = createZyncAssetReferenceContentFragment(
518518
nanoidToUuidV4(dblobAssetId, 'convert-dblob-to-dasset'),
519+
prompt || safeLabel, // use prompt if available, otherwise use the label
519520
'image',
520521
{
521522
pt: 'image_ref' as const,

src/modules/t2i/t2i.client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export async function t2iGenerateImageContentFragments(
170170
// Create a Zync Image Asset Reference *Content* fragment, as this is image content from the LLM
171171
const zyncImageAssetFragmentWithLegacy = createZyncAssetReferenceContentFragment(
172172
nanoidToUuidV4(dblobAssetId, 'convert-dblob-to-dasset'),
173+
_i.altText || prompt, // use altText (revised prompt) if available, otherwise use the prompt
173174
'image',
174175
{
175176
pt: 'image_ref' as const,

0 commit comments

Comments
 (0)