Skip to content

Commit

Permalink
chore: decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Jan 1, 2025
1 parent 60af6d7 commit af3ae16
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 32 deletions.
17 changes: 12 additions & 5 deletions clients/ai-assistants-v1/sdk/searchIndexFileSdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Client } from 'nice-grpc';
import { searchIndexFileService } from '..';

import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';
import { ClientCallArgs, OperationWithDecoder, SessionArg, TypeFromProtoc } from './types';
import {
BatchCreateSearchIndexFileRequest,
BatchCreateSearchIndexFileResponse,
GetSearchIndexFileRequest,
ListSearchIndexFilesRequest,
SearchIndexFileServiceService,
Expand Down Expand Up @@ -34,10 +35,16 @@ export class SearchIndexFileSdk {
}

batchCreate(params: CreateSearchIndexFileProps, args?: ClientCallArgs) {
return this.searchIndexFileClient.batchCreate(
searchIndexFileService.BatchCreateSearchIndexFileRequest.fromPartial(params),
args,
);
return this.searchIndexFileClient
.batchCreate(
searchIndexFileService.BatchCreateSearchIndexFileRequest.fromPartial(params),
args,
)
.then<OperationWithDecoder<BatchCreateSearchIndexFileResponse>>((operation) => {
return Object.assign(operation, {
decoder: BatchCreateSearchIndexFileResponse.decode,
});
});
}

get(params: GetSearchIndexFileProps, args?: ClientCallArgs) {
Expand Down
12 changes: 7 additions & 5 deletions clients/ai-assistants-v1/sdk/searchIndexSdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client } from 'nice-grpc';
import { searchIndexService } from '..';

import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';
import { ClientCallArgs, OperationWithDecoder, SessionArg, TypeFromProtoc } from './types';
import {
CreateSearchIndexRequest,
DeleteSearchIndexRequest,
Expand All @@ -10,6 +10,7 @@ import {
SearchIndexServiceService,
UpdateSearchIndexRequest,
} from '../generated/yandex/cloud/ai/assistants/v1/searchindex/search_index_service';
import { SearchIndex } from '../generated/yandex/cloud/ai/assistants/v1/searchindex/search_index';

export type CreateSearchIndexProps = TypeFromProtoc<
CreateSearchIndexRequest,
Expand Down Expand Up @@ -40,10 +41,11 @@ export class SearchIndexSdk {
}

create(params: CreateSearchIndexProps, args?: ClientCallArgs) {
return this.searchIndexClient.create(
searchIndexService.CreateSearchIndexRequest.fromPartial(params),
args,
);
return this.searchIndexClient
.create(searchIndexService.CreateSearchIndexRequest.fromPartial(params), args)
.then<OperationWithDecoder<SearchIndex>>((operation) => {
return Object.assign(operation, { decoder: SearchIndex.decode });
});
}

get(params: GetSearchIndexProps, args?: ClientCallArgs) {
Expand Down
6 changes: 6 additions & 0 deletions clients/ai-assistants-v1/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DeadlineOptions } from 'nice-grpc-client-middleware-deadline';
import { NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';

import { DeepPartial } from '../generated/typeRegistry';
import { Operation } from '../generated/yandex/cloud/operation/operation';
import { Reader } from 'protobufjs';

type RetryOptions = {
/**
Expand Down Expand Up @@ -136,3 +138,7 @@ export type TypeFromProtoc<
> = {
[Key in NotPartialKey]: T[Key];
} & DeepPartial<T>;

export type OperationWithDecoder<DecoderT> = Operation & {
decoder: (input: Reader | Uint8Array, length?: number) => DecoderT;
};
18 changes: 13 additions & 5 deletions clients/ai-foundation_models-v1/sdk/imageGenerationSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { imageGenerationService } from '..';
import {
ImageGenerationAsyncServiceService,
ImageGenerationRequest,
ImageGenerationResponse,
} from '../generated/yandex/cloud/ai/foundation_models/v1/image_generation/image_generation_service';
import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';
import { ClientCallArgs, OperationWithDecoder, SessionArg, TypeFromProtoc } from './types';

export type GenerateImageProps = Omit<
TypeFromProtoc<ImageGenerationRequest, 'messages'>,
Expand Down Expand Up @@ -33,10 +34,17 @@ export class ImageGenerationSdk {
const { modelId, folderId, ...restParams } = params;
const modelUri = `art://${folderId}/${modelId}`;

return this.imageGenerationClient.generate(
imageGenerationService.ImageGenerationRequest.fromPartial({ ...restParams, modelUri }),
args,
);
return this.imageGenerationClient
.generate(
imageGenerationService.ImageGenerationRequest.fromPartial({
...restParams,
modelUri,
}),
args,
)
.then<OperationWithDecoder<ImageGenerationResponse>>((operation) => {
return Object.assign(operation, { decoder: ImageGenerationResponse.decode });
});
}
}

Expand Down
17 changes: 10 additions & 7 deletions clients/ai-foundation_models-v1/sdk/textGenerationSdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Client } from 'nice-grpc';
import { textGenerationService } from '..';

import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';
import { ClientCallArgs, OperationWithDecoder, SessionArg, TypeFromProtoc } from './types';
import {
CompletionRequest,
CompletionResponse,
TextGenerationAsyncServiceService,
TextGenerationServiceService,
TokenizeRequest,
Expand Down Expand Up @@ -81,12 +82,14 @@ export class TextGenerationSdk {
const { modelId, folderId, ...restParams } = params;
const modelUri = `gpt://${folderId}/${modelId}`;

const operationP = this.textGenerationAsyncClient.completion(
textGenerationService.CompletionRequest.fromPartial({ ...restParams, modelUri }),
args,
);

return operationP;
return this.textGenerationAsyncClient
.completion(
textGenerationService.CompletionRequest.fromPartial({ ...restParams, modelUri }),
args,
)
.then<OperationWithDecoder<CompletionResponse>>((operation) => {
return Object.assign(operation, { decoder: CompletionResponse.decode });
});
}
}

Expand Down
6 changes: 6 additions & 0 deletions clients/ai-foundation_models-v1/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DeadlineOptions } from 'nice-grpc-client-middleware-deadline';
import { NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';

import { DeepPartial } from '../generated/typeRegistry';
import { Operation } from '../generated/yandex/cloud/operation/operation';
import { Reader } from 'protobufjs';

type RetryOptions = {
/**
Expand Down Expand Up @@ -136,3 +138,7 @@ export type TypeFromProtoc<
> = {
[Key in NotPartialKey]: T[Key];
} & DeepPartial<T>;

export type OperationWithDecoder<DecoderT> = Operation & {
decoder: (input: Reader | Uint8Array, length?: number) => DecoderT;
};
17 changes: 13 additions & 4 deletions clients/operation/sdk/operationSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ export type GetOperationProps = TypeFromProtoc<GetOperationRequest, 'operationId

export type CancelOperationProps = TypeFromProtoc<CancelOperationRequest, 'operationId'>;

type DecoderFuncType<DecoderT> = (input: Reader | Uint8Array, length?: number) => DecoderT;

type OperationWithDecoder<DecoderT> = Operation & {
decoder?: DecoderFuncType<DecoderT>;
};

type PollArgs<DecoderT> = {
operationCallback?: (operation: Operation) => void;
decoder?: (input: Reader | Uint8Array, length?: number) => DecoderT;
decoder?: DecoderFuncType<DecoderT>;
};

interface CancellablePromise<T> extends Promise<T> {
Expand Down Expand Up @@ -49,7 +55,7 @@ export class OperationSdk {
static PollOperationEmptyResponseForDecoder = PollOperationEmptyResponseForDecoder;

public pollOperation<DecoderT = Operation>(
operation: Operation | string,
operation: string | OperationWithDecoder<DecoderT>,
intervalMs: number,
args?: PollArgs<DecoderT>,
): CancellablePromise<DecoderT> {
Expand All @@ -65,13 +71,16 @@ export class OperationSdk {
return false;
};

const decoderFromOperation = isObject(operation) ? operation.decoder : null;
const operationDecoderHandler = (operation: Operation): DecoderT => {
if (args?.decoder) {
const decoder = args?.decoder ?? decoderFromOperation;

if (decoder) {
if (operation.response === undefined) {
throw new PollOperationEmptyResponseForDecoder(operation);
}

return args.decoder(operation.response.value);
return decoder(operation.response.value);
}

return operation as DecoderT;
Expand Down
5 changes: 1 addition & 4 deletions examples/assistant-with-search-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
initThreadSdk,
MessageSdk,
} from '@yandex-cloud/nodejs-sdk/ai-assistants-v1/sdk';
import { SearchIndex } from '@yandex-cloud/nodejs-sdk/ai-assistants-v1/generated/yandex/cloud/ai/assistants/v1/searchindex/search_index';

import { initOperationSdk } from '@yandex-cloud/nodejs-sdk/operation/sdk';

Expand Down Expand Up @@ -70,9 +69,7 @@ const createSearchIndex = async (fileId: string) => {

const operationSdk = initOperationSdk(session);

const searchIndex = await operationSdk.pollOperation(createSearchIndexOperation, 500, {
decoder: SearchIndex.decode,
});
const searchIndex = await operationSdk.pollOperation(createSearchIndexOperation, 500);

return searchIndex;
};
Expand Down
2 changes: 0 additions & 2 deletions examples/generate-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
initImageGenerationSdk,
imageGeneration,
} from '@yandex-cloud/nodejs-sdk/ai-foundation_models-v1/sdk';
import { ImageGenerationResponse } from '@yandex-cloud/nodejs-sdk/ai-foundation_models-v1/generated/yandex/cloud/ai/foundation_models/v1/image_generation/image_generation_service';
import { writeFile } from 'fs';

dotenv.config({ path: path.resolve(__dirname, '.env') });
Expand Down Expand Up @@ -53,7 +52,6 @@ const folderId = getEnv('YC_FOLDER_ID');
1_000,
{
operationCallback: console.log,
decoder: ImageGenerationResponse.decode,
},
);

Expand Down

0 comments on commit af3ae16

Please sign in to comment.