Class: Document
@providerprotocol/ai / @providerprotocol/ai / Document
Class: Document
Section titled “Class: Document”Defined in: src/core/media/document.ts:60
Represents a document that can be used in UPP messages.
Documents can be created from various sources (files, URLs, text, base64) and converted to content blocks for provider APIs. The class provides a unified interface regardless of the underlying source type.
Example
Section titled “Example”// Load PDF from fileconst pdfDoc = await Document.fromPath('./report.pdf');
// Reference PDF by URLconst urlDoc = Document.fromUrl('https://example.com/document.pdf');
// From plain textconst textDoc = Document.fromText('Document content here...');
// Use in a messageconst message = new UserMessage([document.toBlock()]);Properties
Section titled “Properties”mimeType
Section titled “mimeType”
readonlymimeType:string
Defined in: src/core/media/document.ts:64
MIME type of the document (‘application/pdf’ or ‘text/plain’)
source
Section titled “source”
readonlysource:DocumentSource
Defined in: src/core/media/document.ts:62
The underlying document source (base64, url, or text)
title?
Section titled “title?”
readonlyoptionaltitle:string
Defined in: src/core/media/document.ts:66
Optional document title (used for citations)
Accessors
Section titled “Accessors”hasData
Section titled “hasData”Get Signature
Section titled “Get Signature”get hasData():
boolean
Defined in: src/core/media/document.ts:83
Whether this document has data loaded in memory.
Returns false for URL-sourced documents that reference external resources.
Returns
Section titled “Returns”boolean
Get Signature
Section titled “Get Signature”get isPdf():
boolean
Defined in: src/core/media/document.ts:90
Whether this document is a PDF.
Returns
Section titled “Returns”boolean
isText
Section titled “isText”Get Signature
Section titled “Get Signature”get isText():
boolean
Defined in: src/core/media/document.ts:97
Whether this document is plain text.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”toBase64()
Section titled “toBase64()”toBase64():
string
Defined in: src/core/media/document.ts:107
Converts the document to a base64-encoded string.
Returns
Section titled “Returns”string
The document data as a base64 string
Throws
Section titled “Throws”When the source is a URL or plain text
toBlock()
Section titled “toBlock()”toBlock():
DocumentBlock
Defined in: src/core/media/document.ts:148
Converts this Document to a DocumentBlock for use in UPP messages.
Returns
Section titled “Returns”A DocumentBlock that can be included in message content arrays
toText()
Section titled “toText()”toText():
string
Defined in: src/core/media/document.ts:121
Gets the plain text content for text documents.
Returns
Section titled “Returns”string
The document text content
Throws
Section titled “Throws”When the source is not plain text
toUrl()
Section titled “toUrl()”toUrl():
string
Defined in: src/core/media/document.ts:135
Gets the URL for URL-sourced documents.
Returns
Section titled “Returns”string
The document URL
Throws
Section titled “Throws”When the source is not a URL
fromBase64()
Section titled “fromBase64()”
staticfromBase64(base64,mimeType,title?):Document
Defined in: src/core/media/document.ts:250
Creates a Document from base64-encoded data.
Parameters
Section titled “Parameters”base64
Section titled “base64”string
The base64-encoded document data
mimeType
Section titled “mimeType”string
The MIME type (‘application/pdf’ or ‘text/plain’)
title?
Section titled “title?”string
Optional document title
Returns
Section titled “Returns”Document
A Document containing the base64 data
Example
Section titled “Example”const doc = Document.fromBase64(pdfBase64, 'application/pdf', 'Contract');fromBlock()
Section titled “fromBlock()”
staticfromBlock(block):Document
Defined in: src/core/media/document.ts:287
Creates a Document from an existing DocumentBlock.
Useful for converting content blocks received from providers back into Document instances for further processing.
Parameters
Section titled “Parameters”A DocumentBlock from message content
Returns
Section titled “Returns”Document
A Document with the block’s source and metadata
fromPath()
Section titled “fromPath()”
staticfromPath(path,title?):Promise<Document>
Defined in: src/core/media/document.ts:173
Creates a Document by reading a file from disk.
The file is read into memory and base64-encoded. MIME type is automatically detected from the file extension.
Parameters
Section titled “Parameters”string
Path to the document file
title?
Section titled “title?”string
Optional document title
Returns
Section titled “Returns”Promise<Document>
Promise resolving to a Document with the file contents
Example
Section titled “Example”const doc = await Document.fromPath('./reports/annual.pdf');const docWithTitle = await Document.fromPath('./report.pdf', 'Annual Report 2024');fromText()
Section titled “fromText()”
staticfromText(text,title?):Document
Defined in: src/core/media/document.ts:270
Creates a Document from plain text content.
Parameters
Section titled “Parameters”string
The document text content
title?
Section titled “title?”string
Optional document title
Returns
Section titled “Returns”Document
A Document containing the text
Example
Section titled “Example”const doc = Document.fromText('This is the document content.', 'Notes');fromUrl()
Section titled “fromUrl()”
staticfromUrl(url,title?):Document
Defined in: src/core/media/document.ts:217
Creates a Document from a URL reference.
The URL is stored as a reference and not fetched. Providers will handle URL fetching if needed. Only PDF URLs are supported. URLs must use the http or https protocol.
Parameters
Section titled “Parameters”string
URL pointing to the PDF document
title?
Section titled “title?”string
Optional document title
Returns
Section titled “Returns”Document
A Document referencing the URL
Example
Section titled “Example”const doc = Document.fromUrl('https://example.com/report.pdf');