ttc-ai-client
Version:
TypeScript client sdk for TTC AI services with decorators and schema validation.
123 lines • 4.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ttcAI = void 0;
const internal_1 = require("./internal");
const ttc_server_1 = require("./ttc_server");
const core_1 = require("./core");
class ttcAI {
// static conversation_id: string;
constructor() {
// url: string;
this.functions = {};
this.on_call = false;
this.init = async () => {
this.internal._setttcAI(this);
await this.fetchFunctions();
};
// ttcAI.conversation_id = conversation_id || '';
// this.url = server_url;
this.internal = new internal_1.TTCInternal();
}
async fetchFunctions() {
try {
const internal_functions = this.internal.get_internal_functions();
const result = await core_1.ttc.generateRPCMethodsList();
const functions = [...internal_functions, ...result];
// console.log('Fetched functions from server:', functions);
this.functions = functions.reduce((acc, func) => {
acc[func.name] = func;
return acc;
}, {});
}
catch (error) {
console.log(error);
console.log('Unable to fetch functions from server, confirm the server is running and the URL is correct and ask ttc to fetch functions again.');
}
}
async invokeFunctions(conversation_id, func) {
const isInternal = func.function.startsWith('TTCInternal.');
if (isInternal) {
return await this.internal.invokeInternal(conversation_id, func.function, func.arguments);
}
else {
await core_1.ttc.emit('function_call', {
id: conversation_id,
payload: `- ${func.function}: (${JSON.stringify(func.arguments)})`
});
return await this.invokeFunction(func);
}
}
async invokeFunctionWithPermission(conversation_id, func) {
// run the implementation to ask permission on terminal
const approved = await new Promise((resolve) => {
core_1.ttc.askPermission(conversation_id, func, (permitted) => {
resolve(permitted);
});
});
return {
approved,
p_response: approved ? await this.invokeFunctions(conversation_id, func) : { error: "Permission denied" }
};
}
async invokeFunction(call) {
try {
const payload = {
method: call.function,
params: Object.values(call.arguments)
};
const response = await core_1.ttc.invoke(payload.method, payload.params, {
socket: core_1.ttc.socket,
_scid: (0, ttc_server_1._scid)()
});
return response;
}
catch (error) {
console.error("Error calling API:", error);
throw error;
}
}
async processFunctionCall(conversation_id, functions) {
try {
const responses = [];
const image_urls = [];
for (const func of functions) {
let response = null;
// this is where to check for permissions or not
const needPermission = this.functions[func.function]?.permission === true;
if (needPermission) {
const { approved, p_response } = await this.invokeFunctionWithPermission(conversation_id, func);
if (!approved) {
responses.push({ function: func.function, response: { error: "Permission denied" } });
continue;
}
else {
response = p_response;
}
}
else {
response = await this.invokeFunctions(conversation_id, func);
}
if (func.function === 'TTCInternal.takeScreenshot') {
image_urls.push(response);
response = `Screenshot taken`;
}
if (response)
responses.push({ function: func.function, response });
else {
if (!func.function.includes('TTCInternal.')) {
responses.push({ function: func.function, response: `Function ${func.function} executed.` });
}
}
}
if (responses.length > 0)
await this.internal.sendFunctionResponse(conversation_id, responses, image_urls);
}
catch (error) {
console.log(error);
}
}
}
exports.ttcAI = ttcAI;
ttcAI.ttc_Uri = 'https://api.tentarclesai.com';
ttcAI.origins = {};
//# sourceMappingURL=ttcAI.js.map