Class: Thread
@providerprotocol/ai / @providerprotocol/ai / Thread
Class: Thread
Section titled “Class: Thread”Defined in: src/types/thread.ts:58
Thread - A utility class for managing conversation history.
Provides methods for building, manipulating, and persisting
conversation message sequences. This class is optional; users
can also manage their own Message[] arrays directly.
Example
Section titled “Example”// Create a new thread and add messagesconst thread = new Thread();thread.user('Hello!');thread.assistant('Hi there! How can I help?');
// Use with LLM inferenceconst turn = await instance.generate(thread, 'What is 2+2?');thread.append(turn);
// Serialize for storageconst json = thread.toJSON();localStorage.setItem('chat', JSON.stringify(json));
// Restore from storageconst restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Thread(
messages?):Thread
Defined in: src/types/thread.ts:76
Creates a new thread instance.
Parameters
Section titled “Parameters”messages?
Section titled “messages?”Message[]
Optional initial messages to populate the thread
Returns
Section titled “Returns”Thread
Properties
Section titled “Properties”
readonlyid:string
Defined in: src/types/thread.ts:60
Unique thread identifier
Accessors
Section titled “Accessors”length
Section titled “length”Get Signature
Section titled “Get Signature”get length():
number
Defined in: src/types/thread.ts:93
Number of messages in the thread.
Returns
Section titled “Returns”number
messages
Section titled “messages”Get Signature
Section titled “Get Signature”get messages(): readonly
Message[]
Defined in: src/types/thread.ts:86
All messages in the thread (readonly).
Returns
Section titled “Returns”readonly Message[]
Methods
Section titled “Methods”[iterator]()
Section titled “[iterator]()”[iterator]():
Iterator<Message>
Defined in: src/types/thread.ts:279
Enables iteration over messages with for…of loops.
Returns
Section titled “Returns”Iterator<Message>
Iterator over the thread’s messages
Example
Section titled “Example”for (const message of thread) { console.log(message.text);}append()
Section titled “append()”append(
turn):this
Defined in: src/types/thread.ts:103
Appends all messages from a Turn to the thread.
Parameters
Section titled “Parameters”The Turn containing messages to append
Returns
Section titled “Returns”this
This thread instance for chaining
assistant()
Section titled “assistant()”assistant(
content):this
Defined in: src/types/thread.ts:153
Adds an assistant message to the thread.
Parameters
Section titled “Parameters”content
Section titled “content”String or array of content blocks
string | AssistantContent[]
Returns
Section titled “Returns”this
This thread instance for chaining
Example
Section titled “Example”thread.assistant('I can help with that!');clear()
Section titled “clear()”clear():
this
Defined in: src/types/thread.ts:211
Removes all messages from the thread.
Returns
Section titled “Returns”this
This thread instance for chaining
filter()
Section titled “filter()”filter(
type):Message[]
Defined in: src/types/thread.ts:171
Filters messages by type.
Parameters
Section titled “Parameters”The message type to filter by
Returns
Section titled “Returns”Message[]
Array of messages matching the type
Example
Section titled “Example”const userMessages = thread.filter('user');const assistantMessages = thread.filter('assistant');push()
Section titled “push()”push(…
messages):this
Defined in: src/types/thread.ts:115
Adds raw messages to the thread.
Parameters
Section titled “Parameters”messages
Section titled “messages”…Message[]
Messages to add
Returns
Section titled “Returns”this
This thread instance for chaining
slice()
Section titled “slice()”slice(
start?,end?):Thread
Defined in: src/types/thread.ts:202
Creates a new thread with a subset of messages.
Parameters
Section titled “Parameters”start?
Section titled “start?”number
Start index (inclusive)
number
End index (exclusive)
Returns
Section titled “Returns”Thread
New Thread containing the sliced messages
Example
Section titled “Example”const subset = thread.slice(0, 10);tail()
Section titled “tail()”tail(
count):Message[]
Defined in: src/types/thread.ts:186
Returns the last N messages from the thread.
Parameters
Section titled “Parameters”number
Number of messages to return
Returns
Section titled “Returns”Message[]
Array of the last N messages
Example
Section titled “Example”const recent = thread.tail(5);toJSON()
Section titled “toJSON()”toJSON():
ThreadJSON
Defined in: src/types/thread.ts:237
Serializes the thread to JSON format.
Returns
Section titled “Returns”JSON-serializable representation of the thread
Example
Section titled “Example”const json = thread.toJSON();localStorage.setItem('thread', JSON.stringify(json));toMessages()
Section titled “toMessages()”toMessages():
Message[]
Defined in: src/types/thread.ts:222
Converts the thread to a plain message array.
Returns
Section titled “Returns”Message[]
Copy of the internal message array
user()
Section titled “user()”user(
content):this
Defined in: src/types/thread.ts:136
Adds a user message to the thread.
Parameters
Section titled “Parameters”content
Section titled “content”String or array of content blocks
string | UserContent[]
Returns
Section titled “Returns”this
This thread instance for chaining
Example
Section titled “Example”thread.user('Hello, world!');thread.user([ { type: 'text', text: 'Describe this image:' }, { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }]);fromJSON()
Section titled “fromJSON()”
staticfromJSON(json):Thread
Defined in: src/types/thread.ts:258
Deserializes a thread from JSON format.
Parameters
Section titled “Parameters”The JSON representation to deserialize
Returns
Section titled “Returns”Thread
Reconstructed Thread instance
Example
Section titled “Example”const json = JSON.parse(localStorage.getItem('thread'));const thread = Thread.fromJSON(json);