jorel
Version:
A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.
119 lines (118 loc) • 3.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskExecutionThread = void 0;
const task_execution_1 = require("./task-execution");
/**
* Represents an execution thread (messages along with the responsible agent) within a task execution
*/
class TaskExecutionThread {
/**
* Create a new task execution thread
* @param data
* @param jorEl
*/
constructor(data, jorEl) {
this.id = data.id;
this.agentId = data.agentId;
this.messages = data.messages;
this.parentThreadId = data.parentThreadId;
this.parentToolCallId = data.parentToolCallId;
this.events = data.events;
this.modified = data.modified;
this.jorEl = jorEl;
if (this.messages.length === 0) {
throw new task_execution_1.TaskExecutionError("Messages cannot be an empty array", this.id);
}
}
/**
* Whether this thread is the main thread
*/
get isMain() {
return this.id === task_execution_1.__mainTaskExecutionThreadId;
}
/**
* Get the agent instance for this thread
*/
get agent() {
return this.jorEl.getAgent(this.agentId);
}
/**
* Get the last message in this thread
*/
get latestMessage() {
if (this.messages.length === 0) {
throw new task_execution_1.TaskExecutionError("No messages in thread", this.id);
}
return this.messages[this.messages.length - 1];
}
/**
* Get the definition of this task execution thread
*/
get definition() {
return {
id: this.id,
agentId: this.agentId,
messages: this.messages.slice(),
parentThreadId: this.parentThreadId,
parentToolCallId: this.parentToolCallId,
events: this.events,
modified: this.modified,
};
}
/**
* Create a new instance of this thread - e.g. to avoid modifying the original
*/
get copy() {
return new TaskExecutionThread(this.definition, this.jorEl);
}
/**
* Get the pending approvals for this thread
*/
get toolCallsWithPendingApprovals() {
const toolCalls = [];
for (const message of this.messages) {
if (message.role === "assistant_with_tools") {
toolCalls.push(...message.toolCalls.map((toolCall) => ({ ...toolCall, messageId: message.id, threadId: this.id })));
}
}
return toolCalls;
}
/**
* Approve or reject tool calls
* @param messageId
* @param toolCallIds
* @param approvalState
*/
approveOrRejectToolCalls(messageId, toolCallIds, approvalState) {
let modified = false;
this.messages.forEach((message) => {
if (message.role === "assistant_with_tools" && message.id === messageId) {
message.toolCalls.forEach((toolCall) => {
if (toolCallIds.includes(toolCall.id)) {
toolCall.approvalState = approvalState;
modified = true;
}
});
}
});
if (modified) {
this.modified = true;
}
}
/**
* Add an event to this thread’s event list.
*/
addEvent(event) {
this.events.push(event);
this.modified = true;
}
/**
* Add a message to this thread
* @param message
*/
addMessage(message) {
this.messages.push(message);
this.modified = true;
}
}
exports.TaskExecutionThread = TaskExecutionThread;