Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SkyflowError extends Error {

error?: ISkyflowError;

constructor(errorCode: ISkyflowError, args: any[] = []) {
constructor(errorCode: ISkyflowError, args: Array<string | number> = []) {
const formattedError = {
http_status: errorCode?.http_status || BAD_REQUEST,
details: errorCode?.details || [],
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import UpdateResponse from './vault/model/response/update';
import FileUploadResponse from './vault/model/response/file-upload';
import QueryResponse from './vault/model/response/query';
import InvokeConnectionResponse from './vault/model/response/invoke/invoke';
import { SkyflowConfig, TokenizeRequestType, DetokenizeData } from './vault/types';
import { SkyflowConfig, TokenizeRequestType, DetokenizeData, InsertResponseType, GetResponseData, QueryResponseType, IndexRange } from './vault/types';
import VaultConfig from './vault/config/vault';
import SkyflowError from './error';
import ConnectionConfig from './vault/config/connection';
Expand All @@ -46,6 +46,7 @@ import DeidentifyFileResponse from './vault/model/response/deidentify-file';
import GetDetectRunRequest from './vault/model/request/get-detect-run';
import { TokenType, MaskingMethod, DetectOutputTranscription } from './utils';
import { Bleep } from './vault/model/options/deidentify-file/bleep-audio';
import { SkyflowRecordError } from './utils/index';
export {
Env,
LogLevel,
Expand Down Expand Up @@ -113,5 +114,10 @@ export {
Bleep,
MaskingMethod,
DetectOutputTranscription,
GetDetectRunRequest
GetDetectRunRequest,
SkyflowRecordError,
InsertResponseType,
GetResponseData,
QueryResponseType,
IndexRange
};
26 changes: 17 additions & 9 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import process from "process";
import SKYFLOW_ERROR_CODE from "../error/codes";
import { isExpired } from "./jwt-utils";
import { isValidAPIKey } from "./validations";
import { StringKeyValueMapType } from "../vault/types";

dotenv.config();

Expand Down Expand Up @@ -216,6 +217,14 @@ export interface ISkyflowError {
details?: Array<string> | null,
}

export interface SkyflowRecordError {
error: string,
requestId: string | null,
httpCode?: string | number | null,
requestIndex?: number | null,
token?: string | null,
}

export interface AuthInfo {
key: string,
type: AuthType
Expand Down Expand Up @@ -345,12 +354,12 @@ export function getBaseUrl(url: string): string {
}

export function fillUrlWithPathAndQueryParams(url: string,
pathParams?: object,
queryParams?: object) {
pathParams?: StringKeyValueMapType,
queryParams?: StringKeyValueMapType) {
let filledUrl = url;
if (pathParams) {
Object.entries(pathParams).forEach(([key, value]) => {
filledUrl = url.replace(`{${key}}`, value);
filledUrl = url.replace(`{${key}}`, String(value));
});
}
if (queryParams) {
Expand Down Expand Up @@ -402,13 +411,12 @@ export const printLog = (message: string, messageType: MessageType, logLevel: Lo
}
};

export const parameterizedString = (...args: any[]) => {
const str = args[0];
const params = args.filter((arg, index) => index !== 0);
export const parameterizedString = (message: string, ...args: Array<string | number | number[] | LogLevel | LogLevel[] | string[]>) => {
const str = message;
if (!str) return '';
return str.replace(/%s[0-9]+/g, (matchedStr: any) => {
const variableIndex = matchedStr.replace('%s', '') - 1;
return params[variableIndex];
return str.replace(/%s[0-9]+/g, (matchedStr: string | number) => {
const variableIndex = parseInt((matchedStr as string).replace('%s', '')) - 1;
return args[variableIndex] as string;
});
};

Expand Down
6 changes: 3 additions & 3 deletions src/utils/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ export const validateInsertOptions = (insertOptions?: InsertOptions) => {
};

const validateTokensMapWithTokenStrict = (
data: object,
tokens: object
data: Record<string, unknown>,
tokens: Record<string, unknown>
) => {
const dataKeys = Object.keys(data);

Expand Down Expand Up @@ -1181,7 +1181,7 @@ export const validateDeidentifyFileOptions = (deidentifyFileOptions: DeidentifyF
}
};

function isStringKeyValueMap(obj: any): obj is StringKeyValueMapType {
function isStringKeyValueMap(obj: unknown): obj is StringKeyValueMapType {
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/vault/controller/connections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class ConnectionController {
const requestId = headers?.get(REQUEST_ID_KEY) || '';
const invokeConnectionResponse = new InvokeConnectionResponse({
data: body,
metadata: { requestId }
metadata: { requestId },
errors: null
});
resolve(invokeConnectionResponse);
}).catch((err) => {
Expand Down
65 changes: 33 additions & 32 deletions src/vault/controller/detect/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//imports

import path from "path";
import { DeidentifyTextRequest as DeidentifyTextRequest2,DeidentifyAudioRequest, DeidentifyAudioRequestFileDataFormat, DeidentifyDocumentRequest, DeidentifyDocumentRequestFileDataFormat, DeidentifyFileRequestFileDataFormat, DeidentifyImageRequest, DeidentifyImageRequestFileDataFormat, DeidentifyImageRequestMaskingMethod, DeidentifyPdfRequest, DeidentifyPresentationRequest, DeidentifyPresentationRequestFileDataFormat, DeidentifySpreadsheetRequest, DeidentifySpreadsheetRequestFileDataFormat, DeidentifyStructuredTextRequest, DeidentifyStructuredTextRequestFileDataFormat, DetectedEntity, EntityType, GetRunRequest, Transformations as GeneratedTransformations, TokenTypeWithoutVault } from "../../../ _generated_/rest/api";
import { DeidentifyTextRequest as DeidentifyTextRequest2,DeidentifyAudioRequest, DeidentifyAudioRequestFileDataFormat, DeidentifyDocumentRequest, DeidentifyDocumentRequestFileDataFormat, DeidentifyFileRequestFileDataFormat, DeidentifyImageRequest, DeidentifyImageRequestFileDataFormat, DeidentifyImageRequestMaskingMethod, DeidentifyPdfRequest, DeidentifyPresentationRequest, DeidentifyPresentationRequestFileDataFormat, DeidentifySpreadsheetRequest, DeidentifySpreadsheetRequestFileDataFormat, DeidentifyStructuredTextRequest, DeidentifyStructuredTextRequestFileDataFormat, DetectedEntity, EntityType, GetRunRequest, Transformations as GeneratedTransformations, TokenTypeWithoutVault, DeidentifyStringResponse, DeidentifyStatusResponse } from "../../../ _generated_/rest/api";
import { DeidentifyFileRequest as DeidentifyFileRequest2} from "../../../ _generated_/rest/api";

import { TokenType } from "../../../ _generated_/rest/api";
Expand All @@ -25,6 +25,7 @@ import SKYFLOW_ERROR_CODE from "../../../error/codes";
import GetDetectRunRequest from "../../model/request/get-detect-run";
import Transformations from "../../model/options/deidentify-text/transformations";
import { SkyflowAllError } from "../../types";
import { DeidentifyFileDetectRunResponse, DeidentifyFileOutput, DetectTextResponse, DetectFileResponse } from "../../types";

class DetectController {

Expand Down Expand Up @@ -267,15 +268,15 @@ class DetectController {
}
}

private processDeidentifyFileResponse(response: any, outputDirectory: string, fileBaseName: string) {
private processDeidentifyFileResponse(response: DeidentifyFileDetectRunResponse, outputDirectory: string, fileBaseName: string) {
try {
// Ensure the output directory exists
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory, { recursive: true });
}

// Iterate over the output array in the response
response.output.forEach((fileObject: any, index: number) => {
response.output.forEach((fileObject: DeidentifyFileOutput, index: number) => {
const { processedFile, processedFileExtension } = fileObject;

if (!processedFile || !processedFileExtension) {
Expand Down Expand Up @@ -334,7 +335,7 @@ class DetectController {

const poll = () => {
this.client.filesAPI.getRun(runId, req)
.then((response: any) => {
.then((response: DeidentifyStatusResponse) => {
if (response.status === 'IN_PROGRESS') {
if (currentWaitTime >= maxWaitTime) {
resolve({ runId }); // Resolve with runId if max wait time is exceeded
Expand All @@ -359,14 +360,14 @@ class DetectController {
reject(new SkyflowError(SKYFLOW_ERROR_CODE.INTERNAL_SERVER_ERROR, [response.message]));
}
})
.catch((error: any) => {
.catch((error) => {
reject(error);
});
};

poll(); // Start polling
}
private handleRequest(apiCall: Function, requestType: string): Promise<any> {
private handleRequest<T>(apiCall: Function, requestType: string): Promise<T> {
return new Promise((resolve, reject) => {
printLog(parameterizedString(logs.infoLogs.EMIT_REQUEST, TYPES[requestType]), MessageType.LOG, this.client.getLogLevel());
const sdkHeaders = this.createSdkHeaders();
Expand All @@ -381,7 +382,7 @@ class DetectController {
switch (requestType) {
case TYPES.DEIDENTIFY_TEXT:
case TYPES.REIDENTIFY_TEXT:
resolve({records: data, requestId})
resolve({records: data, requestId} as T)
break;
case TYPES.DEIDENTIFY_FILE:
const req: GetRunRequest = {
Expand All @@ -393,7 +394,7 @@ class DetectController {
this.pollForProcessedFile(data.run_id, req, maxWaitTime, resolve, reject); // Call the extracted polling function
break;
case TYPES.DETECT_RUN:
resolve({data, requestId})
resolve({data, requestId} as T)
break;

}
Expand Down Expand Up @@ -422,29 +423,29 @@ class DetectController {
};
}

private parseDeidentifyTextResponse(data: any) {
private parseDeidentifyTextResponse(records: DeidentifyStringResponse) {
return {
processedText: data.records.processed_text,
entities: data.records.entities.map((entity: DetectedEntity) => ({
token: entity.token,
value: entity.value,
processedText: records.processed_text,
entities: records.entities.map((entity: DetectedEntity) => ({
token: entity.token!,
value: entity.value!,
textIndex: {
start: entity.location?.start_index,
end: entity.location?.end_index,
start: entity.location?.start_index!,
end: entity.location?.end_index!,
},
processedIndex: {
start: entity.location?.start_index_processed,
end: entity.location?.end_index_processed,
start: entity.location?.start_index_processed!,
end: entity.location?.end_index_processed!,
},
entity: entity.entity_type,
scores: entity.entity_scores,
entity: entity.entity_type!,
scores: entity.entity_scores!,
})),
wordCount: data.records.word_count,
charCount: data.records.character_count,
wordCount: records.word_count,
charCount: records.character_count,
};
}

private parseDeidentifyFileResponse(data: any, runId?: string, status?: string): DeidentifyFileResponse {
private parseDeidentifyFileResponse(data: DeidentifyFileDetectRunResponse, runId?: string, status?: string): DeidentifyFileResponse {
return new DeidentifyFileResponse({
file: data.output?.[0]?.processedFile ?? '',
type: data.output?.[0]?.processedFileType ?? '',
Expand All @@ -456,12 +457,12 @@ class DetectController {
pageCount: data.pages ?? 0,
slideCount: data.slides ?? 0,
entities: (data.output || [])
.filter((fileObject: any) => fileObject.processedFileType === 'entities')
.map((fileObject: any) => ({
file: fileObject.processedFile,
extension: fileObject.processedFileExtension,
.filter((fileObject: DeidentifyFileOutput) => fileObject.processedFileType === 'entities')
.map((fileObject: DeidentifyFileOutput) => ({
file: fileObject.processedFile as string,
extension: fileObject.processedFileExtension as string,
})),
runId: data.runId ?? data.run_id ?? runId, // Handles both camelCase and snake_case
runId: data.runId ?? data.runId ?? runId,
status: status,
});
}
Expand All @@ -475,13 +476,13 @@ class DetectController {
validateDeIdentifyTextRequest(request, options, this.client.getLogLevel());

const requestBody = this.buildDeidentifyTextRequest(request, options);
this.handleRequest(
this.handleRequest<DetectTextResponse<DeidentifyStringResponse>>(
() => this.client.stringsAPI.deidentifyString(
requestBody
).withRawResponse(),
TYPES.DEIDENTIFY_TEXT
).then(data => {
const parsedResponse = new DeidentifyTextResponse(this.parseDeidentifyTextResponse(data))
const parsedResponse = new DeidentifyTextResponse(this.parseDeidentifyTextResponse(data.records))
resolve(parsedResponse);
}).catch(error => {
reject(error)
Expand Down Expand Up @@ -510,7 +511,7 @@ class DetectController {
plaintext: options?.getPlainTextEntities(),
}
};
this.handleRequest(
this.handleRequest<DetectTextResponse<Record<string, string>>>(
() => this.client.stringsAPI.reidentifyString(
requestBody
).withRawResponse(),
Expand Down Expand Up @@ -542,7 +543,7 @@ class DetectController {
vault_id: this.client.vaultId
}

this.handleRequest(
this.handleRequest<DetectFileResponse<DeidentifyFileDetectRunResponse>>(
() => this.client.filesAPI.getRun(
request.runId,
req
Expand Down Expand Up @@ -576,7 +577,7 @@ class DetectController {
this.waitTime = options?.getWaitTime() ?? this.waitTime;

var reqType : DeidenitfyFileRequestTypes = this.getReqType(fileExtension);
var promiseReq: Promise<any>;
var promiseReq: Promise<DeidentifyFileDetectRunResponse>;
switch (reqType){
case DeidenitfyFileRequestTypes.AUDIO:
promiseReq = this.buildAudioRequest(request, options, fileExtension)
Expand Down
Loading
Loading