ttc-ai-client
Version:
TypeScript client sdk for TTC AI services with decorators and schema validation.
227 lines • 11.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TTCInternal = void 0;
const core_1 = require("./core");
class TTCInternal {
constructor() {
this.function_response_cache = {};
this._setttcAI = (ai) => {
this.ai = ai;
};
this.getFunctionDetails = (functionName) => {
return this.ai?.functions[functionName];
};
this.takeNote = async (conversation_id, note) => {
const response = await core_1.ttc.server.ttcCore.takeNote(conversation_id, note);
// return 'Note taken: ' + response.data;
};
this.searchChatHistory = async (conversation_id, startIndex, endIndex, query) => {
const response = await core_1.ttc.server.ttcCore.searchChatHistory(conversation_id, startIndex, endIndex, query);
return response.data;
};
this.takeScreenshot = async (conversation_id, x = 0, y = 0, width, height) => {
try {
// Request screen capture permission
const stream = await navigator.mediaDevices.getDisplayMedia({
video: true
});
const video = document.createElement('video');
video.srcObject = stream;
await new Promise(resolve => video.onloadedmetadata = resolve);
video.play();
// Create canvas to capture the screenshot
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new Error('Could not get canvas context');
}
// Set canvas size to video dimensions or specified dimensions
const videoWidth = video.videoWidth;
const videoHeight = video.videoHeight;
canvas.width = width || videoWidth;
canvas.height = height || videoHeight;
// Draw the video frame to canvas
ctx.drawImage(video, x, y, width || videoWidth, height || videoHeight, 0, 0, width || videoWidth, height || videoHeight);
// Stop the stream
stream.getTracks().forEach(track => track.stop());
// Convert to base64 data URL
return new Promise((resolve, reject) => {
canvas.toBlob(async (blob) => {
if (blob) {
try {
const response = await core_1.ttc.server.Media.uploadImage(core_1.ttc.app_id, conversation_id, blob);
if (response.status === 'success' && response.data?.url) {
console.log('Screenshot taken and uploaded:', response.data.url);
resolve(response.data.url);
}
else {
reject(new Error('Upload failed or no URL returned'));
}
}
catch (uploadError) {
reject(uploadError);
}
}
else {
reject(new Error('Could not convert canvas to blob'));
}
}, 'image/png');
});
}
catch (error) {
console.error('Screenshot failed:', error);
throw new Error(`Screenshot failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
};
this.getEnvironment = () => {
if (typeof window !== 'undefined') {
// Browser environment
return {
current_url: window.location.href,
userAgent: navigator.userAgent,
language: navigator.language
};
}
else {
// Node.js environment
return {
nodeVersion: process.version,
platform: process.platform,
arch: process.arch
};
}
};
this.searchLongtermMemory = async (conversation_id, startIndex, endIndex, query) => {
const response = await core_1.ttc.server.ttcCore.searchLongtermMemory(conversation_id, startIndex, endIndex, query);
return response.data;
};
this.storeLongtermMemory = async (conversation_id, note) => {
const response = await core_1.ttc.server.ttcCore.storeLongtermMemory(conversation_id, note);
return response.data;
};
this.invokeTCC = async (conversation_id, command) => {
const response = await core_1.ttc.server.TCC.invoke(command, conversation_id);
return response.data;
};
this.changeToModel = async (conversation_id, model_id, type) => {
const response = await core_1.ttc.server.TCC.change_to_model(conversation_id, model_id, type);
return response.data;
};
this.sendMessage = (conversation_id, message) => {
core_1.ttc.server.ttcCore.chatAI(conversation_id || '', message);
};
this.sendFunctionResponse = async (conversation_id, response, image_urls) => {
if (this.ai.on_call) {
this.function_response_cache = { response, image_urls };
}
else {
await core_1.ttc.server.ttcCore.functionResponse(conversation_id || '', response, image_urls);
}
};
this.speakToUser = async (conversation_id, message) => {
await core_1.ttc.emit('message', {
id: conversation_id,
payload: message
});
};
this.speakToAssistant = async (conversational_id, sender_conversation_id, message) => {
await core_1.ttc.server.ttcCore.chatAssistant(sender_conversation_id, message, conversational_id);
};
this.invokeInternal = async (conversation_id, functionName, args) => {
if (functionName !== 'TTCInternal.speakToUser')
console.log(`- ${functionName}: (${JSON.stringify(args)})`);
switch (functionName) {
case 'TTCInternal.speakToUser':
return await this.speakToUser(conversation_id, args.message);
case 'TTCInternal.speakToAssistant':
return await this.speakToAssistant(conversation_id, args.assistant_conversation_id, args.message);
case 'TTCInternal.getEnvironment':
return await this.getEnvironment();
case 'TTCInternal.fetchAppFunctions':
return await this.fetchAppFunctions(conversation_id);
case 'TTCInternal.getFunctionDetails':
return await this.getFunctionDetails(args.functionName);
case 'TTCInternal.takeNote':
return await this.takeNote(conversation_id, args.note);
case 'TTCInternal.searchChatHistory':
return await this.searchChatHistory(conversation_id, args.startIndex, args.endIndex, args.query);
case 'TTCInternal.takeScreenshot':
return await this.takeScreenshot(conversation_id, args.x, args.y, args.width, args.height);
case 'TTCInternal.storeLongtermMemory':
return await this.storeLongtermMemory(conversation_id, args.note);
case 'TTCInternal.searchLongtermMemory':
return await this.searchLongtermMemory(conversation_id, args.startIndex, args.endIndex, args.query);
case 'TTCInternal.invokeTCC':
return await this.invokeTCC(conversation_id, args.command);
case 'TTCInternal.changeToModel':
return await this.changeToModel(conversation_id, args.model_id, args.type);
default:
throw new Error(`Unknown function: ${functionName}`);
}
};
this.token = "";
}
async fetchAppFunctions(conversation_id) {
await this.ai?.fetchFunctions();
await core_1.ttc.server.ttcCore.setAppFunctions(conversation_id, this.ai?.functions);
return 'App functions have been added to your system message';
}
get_internal_functions() {
// return for saveToken
// fetchToken
// fetchFunctions
// invokeCLI
return [
{
name: 'TTCInternal.speakToAssistant',
description: 'Send a message to another assistant',
input_schema: `{ assistant_conversation_id: string, message: string }`,
output_schema: 'any'
},
{
name: 'TTCInternal.storeLongtermMemory',
description: 'Store a note in longterm memory',
input_schema: `{ "note": "string" }`,
output_schema: 'any'
},
{
name: 'TTCInternal.searchLongtermMemory',
description: 'Search longterm memory between startIndex and endIndex for a query string',
input_schema: `{ startIndex: number, endIndex: number, query: string }`,
output_schema: `{ results: [ { index: number, message: string } ] }`
},
{
name: 'TTCInternal.dispatchSentinel',
description: 'clones a temporary assistant to handle a specific task and submit report',
input_schema: `{ instruction: string }`,
output_schema: 'any'
},
{
name: 'TTCInternal.getEnvironment',
description: 'Get information about the current browser session',
input_schema: `{}`,
output_schema: `{ current_url: string, userAgent: string, language: string }`
},
{
name: 'TTCInternal.takeScreenshot',
description: 'Take a screenshot of a portion of the screen. Parameters: x, y (coordinates), width, height (optional - defaults to full screen)',
input_schema: `{ x: number, y: number, width: number?, height: number? }`,
output_schema: 'string'
},
{
name: 'TTCInternal.invokeTCC',
description: 'Invoke a Tentarcles Critical Command. Available commands: get_assistants (get all available assistants), summarize (summarize conversation), get_models (get available AI models), get_active_sentinels (get active sentinel assistants)',
input_schema: `{ command: "get_assistants" | "summarize" | "get_models" | "get_active_sentinels" }`,
output_schema: 'any'
},
{
name: 'TTCInternal.changeToModel',
description: 'Change the AI model for the current conversation. Use with caution as it may discontinue chat if model is not compatible',
input_schema: `{ model_id: string, type: string }`,
output_schema: 'any'
},
];
}
}
exports.TTCInternal = TTCInternal;
//# sourceMappingURL=internal.js.map