Skip to content

Class: Thread

@providerprotocol/ai


@providerprotocol/ai / @providerprotocol/ai / 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.

// Create a new thread and add messages
const thread = new Thread();
thread.user('Hello!');
thread.assistant('Hi there! How can I help?');
// Use with LLM inference
const turn = await instance.generate(thread, 'What is 2+2?');
thread.append(turn);
// Serialize for storage
const json = thread.toJSON();
localStorage.setItem('chat', JSON.stringify(json));
// Restore from storage
const restored = Thread.fromJSON(JSON.parse(localStorage.getItem('chat')));

new Thread(messages?): Thread

Defined in: src/types/thread.ts:76

Creates a new thread instance.

Message[]

Optional initial messages to populate the thread

Thread

readonly id: string

Defined in: src/types/thread.ts:60

Unique thread identifier

get length(): number

Defined in: src/types/thread.ts:93

Number of messages in the thread.

number


get messages(): readonly Message[]

Defined in: src/types/thread.ts:86

All messages in the thread (readonly).

readonly Message[]

[iterator](): Iterator<Message>

Defined in: src/types/thread.ts:279

Enables iteration over messages with for…of loops.

Iterator<Message>

Iterator over the thread’s messages

for (const message of thread) {
console.log(message.text);
}

append(turn): this

Defined in: src/types/thread.ts:103

Appends all messages from a Turn to the thread.

Turn

The Turn containing messages to append

this

This thread instance for chaining


assistant(content): this

Defined in: src/types/thread.ts:153

Adds an assistant message to the thread.

String or array of content blocks

string | AssistantContent[]

this

This thread instance for chaining

thread.assistant('I can help with that!');

clear(): this

Defined in: src/types/thread.ts:211

Removes all messages from the thread.

this

This thread instance for chaining


filter(type): Message[]

Defined in: src/types/thread.ts:171

Filters messages by type.

MessageType

The message type to filter by

Message[]

Array of messages matching the type

const userMessages = thread.filter('user');
const assistantMessages = thread.filter('assistant');

push(…messages): this

Defined in: src/types/thread.ts:115

Adds raw messages to the thread.

…Message[]

Messages to add

this

This thread instance for chaining


slice(start?, end?): Thread

Defined in: src/types/thread.ts:202

Creates a new thread with a subset of messages.

number

Start index (inclusive)

number

End index (exclusive)

Thread

New Thread containing the sliced messages

const subset = thread.slice(0, 10);

tail(count): Message[]

Defined in: src/types/thread.ts:186

Returns the last N messages from the thread.

number

Number of messages to return

Message[]

Array of the last N messages

const recent = thread.tail(5);

toJSON(): ThreadJSON

Defined in: src/types/thread.ts:237

Serializes the thread to JSON format.

ThreadJSON

JSON-serializable representation of the thread

const json = thread.toJSON();
localStorage.setItem('thread', JSON.stringify(json));

toMessages(): Message[]

Defined in: src/types/thread.ts:222

Converts the thread to a plain message array.

Message[]

Copy of the internal message array


user(content): this

Defined in: src/types/thread.ts:136

Adds a user message to the thread.

String or array of content blocks

string | UserContent[]

this

This thread instance for chaining

thread.user('Hello, world!');
thread.user([
{ type: 'text', text: 'Describe this image:' },
{ type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }
]);

static fromJSON(json): Thread

Defined in: src/types/thread.ts:258

Deserializes a thread from JSON format.

ThreadJSON

The JSON representation to deserialize

Thread

Reconstructed Thread instance

const json = JSON.parse(localStorage.getItem('thread'));
const thread = Thread.fromJSON(json);