@trillet-ai/web-sdk
Version:
Trillet Web SDK for real-time audio communication with AI agents
64 lines (63 loc) • 2.32 kB
JavaScript
export class TranscriptManager {
constructor() {
this.transcripts = [];
this.currentTranscript = {};
}
getTranscripts() {
return this.transcripts;
}
getCurrentTranscript() {
return this.currentTranscript;
}
addToolStarted(toolCall) {
this.transcripts.push({
role: 'assistant',
text: `Using tool: ${toolCall.name}`,
isFinal: true,
timestamp: new Date(),
toolUsed: { ...toolCall, status: 'pending' },
});
}
addToolCompleted(toolResult) {
const toolTranscriptIndex = this.transcripts.map((t) => { var _a, _b; return ((_a = t.toolUsed) === null || _a === void 0 ? void 0 : _a.name) === toolResult.name && ((_b = t.toolUsed) === null || _b === void 0 ? void 0 : _b.status) === 'pending'; }).lastIndexOf(true);
if (toolTranscriptIndex > -1) {
const toolTranscript = this.transcripts[toolTranscriptIndex];
if (toolTranscript && toolTranscript.toolUsed) {
toolTranscript.toolUsed.status = toolResult.isError ? 'error' : 'success';
}
}
}
handleTranscript(participantId, isAgent, transcriptData) {
if (transcriptData.isFinal) {
delete this.currentTranscript[participantId];
const text = transcriptData.text.trim();
if (!text)
return false;
// Check for duplicates
const lastMessage = this.transcripts[this.transcripts.length - 1];
if (lastMessage && lastMessage.text === text && lastMessage.participantId === participantId && Date.now() - lastMessage.timestamp.getTime() < 1000) {
return false;
}
this.transcripts.push({
role: isAgent ? 'assistant' : 'user',
text,
isFinal: true,
timestamp: new Date(),
participantId,
});
}
else {
this.currentTranscript[participantId] = transcriptData.text;
}
return true;
}
addMessage(role, text, participantId) {
this.transcripts.push({
role,
text,
isFinal: true,
timestamp: new Date(),
participantId,
});
}
}