From af3ae16d7576db3687159022e8d53239ddcb39cf Mon Sep 17 00:00:00 2001 From: GermanVor Date: Wed, 1 Jan 2025 20:23:29 +0100 Subject: [PATCH] chore: decoders --- .../ai-assistants-v1/sdk/searchIndexFileSdk.ts | 17 ++++++++++++----- clients/ai-assistants-v1/sdk/searchIndexSdk.ts | 12 +++++++----- clients/ai-assistants-v1/sdk/types.ts | 6 ++++++ .../sdk/imageGenerationSdk.ts | 18 +++++++++++++----- .../sdk/textGenerationSdk.ts | 17 ++++++++++------- clients/ai-foundation_models-v1/sdk/types.ts | 6 ++++++ clients/operation/sdk/operationSdk.ts | 17 +++++++++++++---- examples/assistant-with-search-index.ts | 5 +---- examples/generate-image.ts | 2 -- 9 files changed, 68 insertions(+), 32 deletions(-) diff --git a/clients/ai-assistants-v1/sdk/searchIndexFileSdk.ts b/clients/ai-assistants-v1/sdk/searchIndexFileSdk.ts index 529c2aae..6fc44e77 100644 --- a/clients/ai-assistants-v1/sdk/searchIndexFileSdk.ts +++ b/clients/ai-assistants-v1/sdk/searchIndexFileSdk.ts @@ -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, @@ -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>((operation) => { + return Object.assign(operation, { + decoder: BatchCreateSearchIndexFileResponse.decode, + }); + }); } get(params: GetSearchIndexFileProps, args?: ClientCallArgs) { diff --git a/clients/ai-assistants-v1/sdk/searchIndexSdk.ts b/clients/ai-assistants-v1/sdk/searchIndexSdk.ts index 559ffc45..b3d51245 100644 --- a/clients/ai-assistants-v1/sdk/searchIndexSdk.ts +++ b/clients/ai-assistants-v1/sdk/searchIndexSdk.ts @@ -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, @@ -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, @@ -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>((operation) => { + return Object.assign(operation, { decoder: SearchIndex.decode }); + }); } get(params: GetSearchIndexProps, args?: ClientCallArgs) { diff --git a/clients/ai-assistants-v1/sdk/types.ts b/clients/ai-assistants-v1/sdk/types.ts index 38e1bb12..ec70de96 100644 --- a/clients/ai-assistants-v1/sdk/types.ts +++ b/clients/ai-assistants-v1/sdk/types.ts @@ -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 = { /** @@ -136,3 +138,7 @@ export type TypeFromProtoc< > = { [Key in NotPartialKey]: T[Key]; } & DeepPartial; + +export type OperationWithDecoder = Operation & { + decoder: (input: Reader | Uint8Array, length?: number) => DecoderT; +}; diff --git a/clients/ai-foundation_models-v1/sdk/imageGenerationSdk.ts b/clients/ai-foundation_models-v1/sdk/imageGenerationSdk.ts index cf917795..8ecfe5be 100644 --- a/clients/ai-foundation_models-v1/sdk/imageGenerationSdk.ts +++ b/clients/ai-foundation_models-v1/sdk/imageGenerationSdk.ts @@ -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, @@ -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>((operation) => { + return Object.assign(operation, { decoder: ImageGenerationResponse.decode }); + }); } } diff --git a/clients/ai-foundation_models-v1/sdk/textGenerationSdk.ts b/clients/ai-foundation_models-v1/sdk/textGenerationSdk.ts index 8066b9df..dce634b6 100644 --- a/clients/ai-foundation_models-v1/sdk/textGenerationSdk.ts +++ b/clients/ai-foundation_models-v1/sdk/textGenerationSdk.ts @@ -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, @@ -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>((operation) => { + return Object.assign(operation, { decoder: CompletionResponse.decode }); + }); } } diff --git a/clients/ai-foundation_models-v1/sdk/types.ts b/clients/ai-foundation_models-v1/sdk/types.ts index 38e1bb12..ec70de96 100644 --- a/clients/ai-foundation_models-v1/sdk/types.ts +++ b/clients/ai-foundation_models-v1/sdk/types.ts @@ -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 = { /** @@ -136,3 +138,7 @@ export type TypeFromProtoc< > = { [Key in NotPartialKey]: T[Key]; } & DeepPartial; + +export type OperationWithDecoder = Operation & { + decoder: (input: Reader | Uint8Array, length?: number) => DecoderT; +}; diff --git a/clients/operation/sdk/operationSdk.ts b/clients/operation/sdk/operationSdk.ts index 0f3e40ec..4f952f5a 100644 --- a/clients/operation/sdk/operationSdk.ts +++ b/clients/operation/sdk/operationSdk.ts @@ -15,9 +15,15 @@ export type GetOperationProps = TypeFromProtoc; +type DecoderFuncType = (input: Reader | Uint8Array, length?: number) => DecoderT; + +type OperationWithDecoder = Operation & { + decoder?: DecoderFuncType; +}; + type PollArgs = { operationCallback?: (operation: Operation) => void; - decoder?: (input: Reader | Uint8Array, length?: number) => DecoderT; + decoder?: DecoderFuncType; }; interface CancellablePromise extends Promise { @@ -49,7 +55,7 @@ export class OperationSdk { static PollOperationEmptyResponseForDecoder = PollOperationEmptyResponseForDecoder; public pollOperation( - operation: Operation | string, + operation: string | OperationWithDecoder, intervalMs: number, args?: PollArgs, ): CancellablePromise { @@ -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; diff --git a/examples/assistant-with-search-index.ts b/examples/assistant-with-search-index.ts index f4f46923..c2297a71 100644 --- a/examples/assistant-with-search-index.ts +++ b/examples/assistant-with-search-index.ts @@ -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'; @@ -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; }; diff --git a/examples/generate-image.ts b/examples/generate-image.ts index 8333d51d..9a91e2f4 100644 --- a/examples/generate-image.ts +++ b/examples/generate-image.ts @@ -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') }); @@ -53,7 +52,6 @@ const folderId = getEnv('YC_FOLDER_ID'); 1_000, { operationCallback: console.log, - decoder: ImageGenerationResponse.decode, }, );