contextual-agent-sdk
Version:
SDK for building AI agents with seamless voice-text context switching
139 lines • 4.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextBridge = void 0;
class ContextBridge {
cache = new Map();
bridgeContext(context, sourceModality, targetModality) {
if (sourceModality === targetModality) {
return context;
}
if (sourceModality === 'voice' && targetModality === 'text') {
return this.voiceToTextBridge(context);
}
if (sourceModality === 'text' && targetModality === 'voice') {
return this.textToVoiceBridge(context);
}
return context;
}
voiceToTextBridge(context) {
let bridged = context
.replace(/um|uh|like|you know/gi, '')
.replace(/(\s)+/g, ' ')
.trim();
const topics = this.detectTopics(bridged);
if (topics.length > 1) {
bridged = topics.map(topic => `${topic.heading}:\n${topic.content}`).join('\n\n');
}
bridged = this.formatLists(bridged);
bridged = this.highlightKeyPoints(bridged);
return bridged;
}
textToVoiceBridge(context) {
let bridged = context
.replace(/therefore|however|moreover/gi, 'so')
.replace(/additionally/gi, 'also')
.replace(/regarding/gi, 'about');
bridged = this.splitLongSentences(bridged);
bridged = this.addConversationalMarkers(bridged);
return bridged;
}
detectTopics(text) {
const topics = [];
const sentences = text.split(/[.!?]+/);
let currentTopic = '';
let currentContent = [];
sentences.forEach(sentence => {
const keywords = this.extractKeywords(sentence);
if (this.isNewTopic(keywords, currentTopic)) {
if (currentTopic) {
topics.push({
heading: currentTopic,
content: currentContent.join('. ')
});
}
currentTopic = this.generateTopicHeading(keywords);
currentContent = [sentence];
}
else {
currentContent.push(sentence);
}
});
if (currentTopic) {
topics.push({
heading: currentTopic,
content: currentContent.join('. ')
});
}
return topics;
}
formatLists(text) {
return text.replace(/([.!?]+)\s*(\d+\.|[-•])/g, '$1\n\n$2');
}
highlightKeyPoints(text) {
const keywords = ['important', 'key', 'must', 'critical', 'essential'];
let highlighted = text;
keywords.forEach(keyword => {
const regex = new RegExp(`(${keyword})`, 'gi');
highlighted = highlighted.replace(regex, '**$1**');
});
return highlighted;
}
splitLongSentences(text) {
return text.replace(/([^.!?]+[.!?]+)/g, (sentence) => {
if (sentence.length > 100) {
const parts = sentence.split(/,|;|\band\b|\bor\b/);
return parts.join('.\n');
}
return sentence;
});
}
addConversationalMarkers(text) {
const markers = [
'you see',
'basically',
'actually',
'right',
'well'
];
return text.split('\n').map(line => {
if (line.length > 50) {
const marker = markers[Math.floor(Math.random() * markers.length)];
return `${marker}, ${line.charAt(0).toLowerCase() + line.slice(1)}`;
}
return line;
}).join('\n');
}
extractKeywords(text) {
return text
.toLowerCase()
.split(/\W+/)
.filter(word => word.length > 3)
.filter(word => !this.isStopWord(word));
}
isNewTopic(keywords, currentTopic) {
if (!currentTopic)
return true;
const topicKeywords = this.extractKeywords(currentTopic);
const overlap = keywords.filter(k => topicKeywords.includes(k));
return overlap.length === 0;
}
generateTopicHeading(keywords) {
return keywords
.slice(0, 3)
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
.join(' ');
}
isStopWord(word) {
const stopWords = new Set([
'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have',
'this', 'from', 'with', 'they', 'would', 'what', 'when',
'there', 'about'
]);
return stopWords.has(word);
}
clearCache() {
this.cache.clear();
}
}
exports.ContextBridge = ContextBridge;
//# sourceMappingURL=ContextBridge.js.map