Update asset metadata and enhance Agent class functionality in SUI

- Updated modification times for various asset files in bindata.go to reflect recent changes.
- Enhanced the Agent class in libsui/agent.ts by adding context management and a cancel method for better resource handling.
- Improved type definitions and event handling in the Agent class to streamline message processing and enhance user interactions.
This commit is contained in:
Max 2025-05-07 15:07:52 +08:00
parent 83bdab3499
commit 54e291b804
2 changed files with 105 additions and 100 deletions

File diff suppressed because one or more lines are too long

View file

@ -57,6 +57,8 @@ class Agent {
private events: EventHandlers; private events: EventHandlers;
private assistant_id: string; private assistant_id: string;
private chat_id?: string; private chat_id?: string;
private es: EventSource | null;
private context: Record<string, any>;
/** /**
* Agent constructor * Agent constructor
@ -68,6 +70,8 @@ class Agent {
this.events = {}; this.events = {};
this.assistant_id = assistant_id; this.assistant_id = assistant_id;
this.chat_id = option.chat_id; this.chat_id = option.chat_id;
this.es = null;
this.context = option.context || {};
} }
/** /**
@ -98,6 +102,16 @@ class Agent {
return this; return this;
} }
/**
* Cancel the agent
*/
Cancel() {
if (this.es) {
this.es.close();
this.es = null;
}
}
/** /**
* Call the AI Agent * Call the AI Agent
* @param input Text message or input object with text and optional attachments * @param input Text message or input object with text and optional attachments
@ -105,7 +119,6 @@ class Agent {
*/ */
async Call(input: AgentInput, ...args: any[]): Promise<any> { async Call(input: AgentInput, ...args: any[]): Promise<any> {
const messages: AgentMessage[] = []; const messages: AgentMessage[] = [];
let currentContent = "";
let lastAssistant = { let lastAssistant = {
assistant_id: null as string | null, assistant_id: null as string | null,
assistant_name: null as string | null, assistant_name: null as string | null,
@ -134,8 +147,10 @@ class Agent {
} }
} }
// Add context to the content
const context = { ...this.context, args };
const contentRaw = encodeURIComponent(JSON.stringify(content)); const contentRaw = encodeURIComponent(JSON.stringify(content));
const contextRaw = encodeURIComponent(JSON.stringify(args)); const contextRaw = encodeURIComponent(JSON.stringify(context));
const token = this.token; const token = this.token;
const chatId = this.chat_id || this.makeChatID(); const chatId = this.chat_id || this.makeChatID();
const assistantParam = `&assistant_id=${this.assistant_id}`; const assistantParam = `&assistant_id=${this.assistant_id}`;
@ -196,21 +211,10 @@ class Agent {
}; };
try { try {
const es = new EventSource(endpoint, { const es = new EventSource(endpoint, { withCredentials: true });
withCredentials: true, this.es = es;
});
es.onopen = () => {
const messageHandler = this.events["message"] as MessageHandler;
if (messageHandler) {
messageHandler({
text: "",
is_neo: true,
new: true,
});
}
};
es.onopen = () => {};
es.onmessage = ({ data }: { data: string }) => { es.onmessage = ({ data }: { data: string }) => {
try { try {
const formated_data = JSON.parse(data); const formated_data = JSON.parse(data);
@ -438,4 +442,5 @@ interface AgentOption {
host?: string; host?: string;
token: string; token: string;
chat_id?: string; chat_id?: string;
context?: Record<string, any>;
} }