Skip to content

The custom segment — your own message content (generative UI)

Custom segment - for framework-specific views (Onboarding, Tools, etc.)

A segment is data, not an element — it has no tag and dispatches nothing. This one is AparteCustomSegment, and it is the object you push into a bubble; something else draws it.

Width Open in a tab
Core's own renderer, drawing the example printed below inside a real viewport. A segment is data — this is what the library makes of it, and it looks the same in every framework.
// With no renderer registered for `subType`, core draws `fallback` — which is why the
// field exists: an unknown view degrades to a sentence instead of to nothing.
{
id: 's1',
type: 'custom',
subType: 'weather-widget',
data: { city: 'Lille', celsius: 11 },
fallback: 'Lille — 11°C, overcast.',
}

Discriminated by type: 'custom'.

FieldTypeRequiredDescription
subTypestringyesUnique string structure to identify the view (e.g. ‘onboarding’, ‘webcontainer’, ‘weather-widget’)
dataunknownArbitrary data payload for the component
fallbackstringOptional fallback text representation

From AparteSegmentBase, which is also what a segment type of your own extends.

FieldTypeRequiredDescription
idstringyesUnique segment identifier
typestringyesSegment type discriminator
isStreamingbooleanWhether segment is currently being streamed
messageIdstringId of the message this segment belongs to. Stamped on insertion.
indexnumberPosition in the owning message’s segments[], maintained across insertions and removals.
metaRecord<string, unknown> & { aparte?: AparteSegmentTiming }Extras the producer of the segment knows — token counts, cost, compute device — plus aparte, the one sub-object core writes. More

Extras the producer of the segment knows — token counts, cost, compute device — plus aparte, the one sub-object core writes.

Everything except meta.aparte is yours: fill it with updateSegment(segmentId, { meta }), which MERGES rather than replaces. Mirrors AparteMessage.metadata.

An app pushes one with addSegment() on the viewport, or a parser produces it while a reply streams. Core stamps its identity and timing on insert, so an emitted segment does not have to carry them.

Nothing in core draws this one — it is the escape hatch, and the renderer is yours. Without one registered, core falls back: it draws the segment’s fallback string when there is one, and otherwise a [Unknown segment type: custom] placeholder with a console warning naming the type.

import { registerSegmentRenderer } from '@aparte/core';
import type { AparteCustomSegment } from '@aparte/core';
registerSegmentRenderer({
type: 'custom',
render: (segment: AparteCustomSegment) => {
const el = document.createElement('div');
el.textContent = JSON.stringify(segment);
return el;
},
});

See Customization for the whole renderer seam, and bring your own loop for emitting segments yourself.