ContentPartStream
Model for content part event helpers.
A content part is a single piece of content within a message — text,
audio, an image, or a transcript. Use the type-check properties
(isText, isMarkdown, isHtml, isAudio, isImage, isTranscript)
to determine the content type and handle it accordingly.
Examples¶
message.onContentPartStart((part) => {
if (part.isMarkdown) {
part.onChunk((chunk) => {
process.stdout.write(chunk.data ?? '');
});
}
});
message.onContentPartStart((part) => {
if (part.isText) {
part.onChunk((chunk) => showPlainText(chunk.data ?? ''));
} else if (part.isMarkdown) {
part.onChunk((chunk) => renderMarkdown(chunk.data ?? ''));
} else if (part.isHtml) {
part.onChunk((chunk) => renderHtml(chunk.data ?? ''));
} else if (part.isAudio) {
part.onChunk((chunk) => audioPlayer.enqueue(chunk.data ?? ''));
} else if (part.isImage) {
part.onChunk((chunk) => imageBuffer.append(chunk.data ?? ''));
} else if (part.isTranscript) {
part.onChunk((chunk) => showTranscript(chunk.data ?? ''));
}
});
message.onContentPartStart((part) => {
part.onCompleted((completed) => {
console.log(`Full text: ${completed.data}`);
// Access citations — each has offset, length, and sources
for (const citation of completed.citations) {
const citedText = completed.data.substring(
citation.offset,
citation.offset + citation.length
);
console.log(`"${citedText}" cited from:`, citation.sources);
}
});
});
message.onContentPartCompleted((completed) => {
console.log(`[${completed.mimeType}] ${completed.data}`);
});
Properties¶
Methods¶
onChunk()¶
onChunk(
cb: (chunk:ContentPartChunkEvent) =>void): () =>void
Registers a handler for content part chunks
Chunks are the fundamental unit of streaming data. Each chunk contains a piece of the content (text, audio data, etc.).
Parameters¶
| Parameter | Type | Description |
|---|---|---|
cb |
(chunk: ContentPartChunkEvent) => void |
Callback receiving each chunk |
Returns¶
Cleanup function to remove the handler
():
void
Returns¶
void
Example¶
onCompleted()¶
onCompleted(
cb: (completedContentPart:CompletedContentPart) =>void):void
Registers a handler called when this content part finishes
The handler receives the aggregated content part data including all buffered text, citations, and any citation errors.
Parameters¶
| Parameter | Type | Description |
|---|---|---|
cb |
(completedContentPart: CompletedContentPart) => void |
Callback receiving the completed content part data |
Returns¶
void
Example¶
part.onCompleted((completed) => {
console.log(`Content type: ${completed.mimeType}`);
console.log(`Full text: ${completed.data}`);
// Citations provide offset/length into the text and source references
for (const citation of completed.citations) {
const citedText = completed.data.substring(
citation.offset,
citation.offset + citation.length
);
console.log(`"${citedText}" — sources:`, citation.sources);
}
// Citation errors indicate malformed citation ranges
if (completed.citationErrors.length > 0) {
console.warn('Citation errors:', completed.citationErrors);
}
});
onContentPartEnd()¶
onContentPartEnd(
cb: (endContentPart:ContentPartEndEvent) =>void): () =>void
Registers a handler for content part end events
Parameters¶
| Parameter | Type | Description |
|---|---|---|
cb |
(endContentPart: ContentPartEndEvent) => void |
Callback receiving the end event |
Returns¶
Cleanup function to remove the handler
():
void
Returns¶
void
Example¶
onErrorEnd()¶
onErrorEnd(
cb: (error: {errorId:string; } &ErrorEndEvent) =>void): () =>void
Registers a handler for error end events
Parameters¶
| Parameter | Type | Description |
|---|---|---|
cb |
(error: { errorId: string; } & ErrorEndEvent) => void |
Callback receiving the error end event |
Returns¶
Cleanup function to remove the handler
():
void
Returns¶
void
onErrorStart()¶
onErrorStart(
cb: (error: {errorId:string; } &ErrorStartEvent) =>void): () =>void
Registers a handler for error start events
Parameters¶
| Parameter | Type | Description |
|---|---|---|
cb |
(error: { errorId: string; } & ErrorStartEvent) => void |
Callback receiving the error event |
Returns¶
Cleanup function to remove the handler
():
void
Returns¶
void
Example¶
sendChunk()¶
sendChunk(
chunk:ContentPartChunkEvent):void
Sends a content part chunk
Parameters¶
| Parameter | Type | Description |
|---|---|---|
chunk |
ContentPartChunkEvent |
Chunk data to send |
Returns¶
void
Example¶
sendContentPartEnd()¶
sendContentPartEnd(
endContentPart?:ContentPartEndEvent):void
Ends the content part stream
Parameters¶
| Parameter | Type | Description |
|---|---|---|
endContentPart? |
ContentPartEndEvent |
Optional end event data |
Returns¶
void