@noves/intent-typescript-sdk
Version:
Noves Intent Typescript SDK
135 lines (134 loc) • 4.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerSideIntentSDK = void 0;
const base_1 = require("../shared/base");
const grpcClient_1 = require("./grpcClient");
const types_1 = require("../shared/types");
const errors_1 = require("../shared/errors");
const types_2 = require("../shared/types");
class ServerSideIntentSDK extends base_1.IntentSDKBase {
activeGrpcStreams = new Map();
grpcClient;
constructor() {
super({
apiUrl: types_1.IntentEndpoints.API_URL,
grpcUrl: types_1.IntentEndpoints.GRPC_URL
});
this.grpcClient = new grpcClient_1.GrpcServerClient(types_1.IntentEndpoints.GRPC_URL);
}
async loadIntents() {
try {
const response = await fetch(`${types_1.IntentEndpoints.API_URL}/intents`);
if (!response.ok) {
throw new Error(`Failed to fetch intents: ${response.statusText}`);
}
this.intents = await response.json();
}
catch (error) {
console.error('Failed to load intents:', error);
this.intents = [];
}
}
async getValue(intentId, params = {}, pageSize) {
await this.ensureInitialized();
const intent = this.intents.find(i => i.id === intentId);
if (!intent) {
throw new errors_1.IntentSDKError(`Intent ${intentId} not found`);
}
if (!(0, types_2.isValueIntent)(intent)) {
throw new errors_1.IntentSDKError(`Intent ${intentId} is not a value intent`);
}
try {
const response = await fetch(`${types_1.IntentEndpoints.API_URL}/intents`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: intentId,
params,
pageSize
}),
});
if (!response.ok) {
throw new errors_1.IntentSDKError(`Failed to fetch intent data: ${response.statusText}`);
}
return await response.json();
}
catch (error) {
if (error instanceof Error) {
throw new errors_1.IntentSDKError(`Failed to execute intent: ${error.message}`);
}
throw new errors_1.IntentSDKError('Failed to execute intent');
}
}
async getStream(intentId, params, onData, onError, onComplete) {
const intent = this.intents.find(i => i.id === intentId);
if (!intent) {
onError?.(new errors_1.IntentSDKError(`Intent ${intentId} not found`));
return;
}
const formattedParams = this.grpcClient.stringifyParams(params);
const request = {
id: intentId,
params: formattedParams,
cusRateLimit: 0.1,
};
return new Promise((resolve, reject) => {
try {
const stream = this.grpcClient.genericIntent(request);
stream.on('data', (response) => {
try {
onData(response);
}
catch (error) {
onError?.(error);
}
});
stream.on('error', (error) => {
onError?.(error);
onComplete?.();
reject(error);
});
stream.on('end', () => {
onComplete?.();
resolve();
});
this.activeGrpcStreams.set(intentId, stream);
}
catch (error) {
onError?.(error);
reject(error);
}
});
}
stopStream(intentId) {
const stream = this.activeGrpcStreams.get(intentId);
if (stream) {
stream.cancel();
this.activeGrpcStreams.delete(intentId);
}
}
async getIntents() {
await this.ensureInitialized();
return this.intents;
}
async executeIntent(intentId, params) {
await this.ensureInitialized();
const intent = this.intents.find(i => i.id === intentId);
if (!intent) {
throw new Error("Intent not found");
}
if (intent.mode.includes('value')) {
const data = await this.getValue(intentId, params);
return data;
}
else if (intent.mode.includes('stream')) {
return new Promise((resolve, reject) => {
this.getStream(intentId, params, (data) => resolve(data), (error) => reject(error));
});
}
throw new Error("Invalid intent mode");
}
}
exports.ServerSideIntentSDK = ServerSideIntentSDK;