Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assistant, Files, Foundation Models, Operation sdk and update for mono generated #178

Merged
merged 22 commits into from
Jan 10, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: decoders
GermanVor committed Jan 1, 2025
commit af3ae16d7576db3687159022e8d53239ddcb39cf
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,
@@ -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) {
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,
@@ -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<OperationWithDecoder<SearchIndex>>((operation) => {
return Object.assign(operation, { decoder: SearchIndex.decode });
});
}

get(params: GetSearchIndexProps, args?: ClientCallArgs) {
6 changes: 6 additions & 0 deletions clients/ai-assistants-v1/sdk/types.ts
Original file line number Diff line number Diff line change
@@ -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<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
@@ -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'>,
@@ -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 });
});
}
}

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,
@@ -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 });
});
}
}

6 changes: 6 additions & 0 deletions clients/ai-foundation_models-v1/sdk/types.ts
Original file line number Diff line number Diff line change
@@ -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<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
@@ -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> {
@@ -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> {
@@ -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;
5 changes: 1 addition & 4 deletions examples/assistant-with-search-index.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 0 additions & 2 deletions examples/generate-image.ts
Original file line number Diff line number Diff line change
@@ -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,
},
);