Skip to content

Commit f41f368

Browse files
authored
Merge pull request #178 from yandex-cloud/assistant-sdk
Assistant, Files, Foundation Models, Operation sdk and update for mono generated
2 parents 364e0c7 + 35e877e commit f41f368

File tree

725 files changed

+705402
-752446
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

725 files changed

+705402
-752446
lines changed

.eslintrc.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"import/prefer-default-export": "off",
2727
"comma-dangle": ["error", "always-multiline"],
2828
"indent": "off",
29-
"@typescript-eslint/indent": ["error", 4],
3029
"max-len": ["error", 140],
30+
"@typescript-eslint/indent": ["error", 4],
3131
"@typescript-eslint/ban-ts-comment": "off",
3232
"@typescript-eslint/prefer-optional-chain": "error",
3333
"prefer-arrow-functions/prefer-arrow-functions": ["error"],
@@ -51,6 +51,7 @@
5151
"devDependencies": true
5252
}
5353
],
54-
"import/no-cycle": "off"
54+
"import/no-cycle": "off",
55+
"@typescript-eslint/no-explicit-any": 1
5556
}
5657
}

clients/operation/sdk/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './operationSdk';
2+
export * from '..';

clients/operation/sdk/operationSdk.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { isObject, noop } from 'lodash';
2+
import { operationService } from '..';
3+
import {
4+
CancelOperationRequest,
5+
GetOperationRequest,
6+
OperationServiceService,
7+
} from '../generated/yandex/cloud/operation/operation_service';
8+
9+
import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';
10+
import { Client } from 'nice-grpc';
11+
import { Operation } from '../generated/yandex/cloud/operation/operation';
12+
import { Reader } from 'protobufjs';
13+
14+
export type GetOperationProps = TypeFromProtoc<GetOperationRequest, 'operationId'>;
15+
16+
export type CancelOperationProps = TypeFromProtoc<CancelOperationRequest, 'operationId'>;
17+
18+
type DecoderFuncType<DecoderT> = (input: Reader | Uint8Array, length?: number) => DecoderT;
19+
20+
type OperationWithDecoder<DecoderT> = Operation & {
21+
decoder?: DecoderFuncType<DecoderT>;
22+
};
23+
24+
type PollArgs<DecoderT> = {
25+
operationCallback?: (operation: Operation) => void;
26+
decoder?: DecoderFuncType<DecoderT>;
27+
};
28+
29+
interface CancellablePromise<T> extends Promise<T> {
30+
cancelPolling?: () => void;
31+
}
32+
33+
export class PollOperationWasCanceled {
34+
operation?: Operation;
35+
constructor(operation?: Operation) {
36+
this.operation = operation;
37+
}
38+
}
39+
40+
export class PollOperationEmptyResponseForDecoder {
41+
operation?: Operation;
42+
constructor(operation?: Operation) {
43+
this.operation = operation;
44+
}
45+
}
46+
47+
export class OperationSdk {
48+
private operationClient: Client<typeof OperationServiceService, ClientCallArgs>;
49+
50+
constructor(session: SessionArg) {
51+
this.operationClient = session.client(operationService.OperationServiceClient);
52+
}
53+
54+
static PollOperationWasCanceled = PollOperationWasCanceled;
55+
static PollOperationEmptyResponseForDecoder = PollOperationEmptyResponseForDecoder;
56+
57+
public pollOperation<DecoderT = Operation>(
58+
operation: string | OperationWithDecoder<DecoderT>,
59+
intervalMs: number,
60+
args?: PollArgs<DecoderT>,
61+
): CancellablePromise<DecoderT> {
62+
let outputReject: (reason?: unknown) => void = noop;
63+
let timeoutId: NodeJS.Timeout | undefined;
64+
65+
const operationStatusHandler = (operation: Operation) => {
66+
if (operation.done || operation.error) {
67+
if (timeoutId !== undefined) clearTimeout(timeoutId);
68+
return true;
69+
}
70+
71+
return false;
72+
};
73+
74+
const decoderFromOperation = isObject(operation) ? operation.decoder : null;
75+
const operationDecoderHandler = (operation: Operation): DecoderT => {
76+
const decoder = args?.decoder ?? decoderFromOperation;
77+
78+
if (decoder) {
79+
if (operation.response === undefined) {
80+
throw new PollOperationEmptyResponseForDecoder(operation);
81+
}
82+
83+
return decoder(operation.response.value);
84+
}
85+
86+
return operation as DecoderT;
87+
};
88+
89+
if (isObject(operation) && operationStatusHandler(operation)) {
90+
return Promise.resolve(operation).then(operationDecoderHandler);
91+
}
92+
93+
const p = new Promise<Operation>((resolver, reject) => {
94+
outputReject = reject;
95+
const operationId = isObject(operation) ? operation.id : operation;
96+
97+
const f = () => {
98+
this.get({ operationId }).then((operation) => {
99+
args?.operationCallback?.(operation);
100+
101+
if (operationStatusHandler(operation)) {
102+
timeoutId = undefined;
103+
resolver(operation);
104+
return;
105+
}
106+
107+
timeoutId = setTimeout(() => f(), intervalMs);
108+
});
109+
};
110+
111+
f();
112+
});
113+
114+
(p as CancellablePromise<Operation>).cancelPolling = () => {
115+
outputReject?.(
116+
new PollOperationWasCanceled(isObject(operation) ? operation : undefined),
117+
);
118+
119+
if (timeoutId !== undefined) clearTimeout(timeoutId);
120+
};
121+
122+
return p.then(operationDecoderHandler);
123+
}
124+
125+
public get(params: GetOperationProps, args?: ClientCallArgs) {
126+
return this.operationClient.get(
127+
operationService.GetOperationRequest.fromPartial(params),
128+
args,
129+
);
130+
}
131+
132+
public cancel(params: CancelOperationProps, args?: ClientCallArgs) {
133+
return this.operationClient.cancel(
134+
operationService.CancelOperationRequest.fromPartial(params),
135+
args,
136+
);
137+
}
138+
}
139+
140+
export const initOperationSdk = (session: SessionArg) => {
141+
return new OperationSdk(session);
142+
};

clients/operation/sdk/types.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { ChannelCredentials, ChannelOptions, Client, ServiceDefinition } from '@grpc/grpc-js';
2+
import { ClientError, RawClient, Status } from 'nice-grpc';
3+
import { DeadlineOptions } from 'nice-grpc-client-middleware-deadline';
4+
import { NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';
5+
6+
import { DeepPartial } from '../generated/typeRegistry';
7+
8+
type RetryOptions = {
9+
/**
10+
* Boolean indicating whether retries are enabled.
11+
*
12+
* If the method is marked as idempotent in Protobuf, i.e. has
13+
*
14+
* option idempotency_level = IDEMPOTENT;
15+
*
16+
* then the default is `true`. Otherwise, the default is `false`.
17+
*
18+
* Method options currently work only when compiling with `ts-proto`.
19+
*/
20+
retry?: boolean;
21+
/**
22+
* Base delay between retry attempts in milliseconds.
23+
*
24+
* Defaults to 1000.
25+
*
26+
* Example: if `retryBaseDelayMs` is 100, then retries will be attempted in
27+
* 100ms, 200ms, 400ms etc. (not counting jitter).
28+
*/
29+
retryBaseDelayMs?: number;
30+
/**
31+
* Maximum delay between attempts in milliseconds.
32+
*
33+
* Defaults to 15 seconds.
34+
*
35+
* Example: if `retryBaseDelayMs` is 1000 and `retryMaxDelayMs` is 3000, then
36+
* retries will be attempted in 1000ms, 2000ms, 3000ms, 3000ms etc (not
37+
* counting jitter).
38+
*/
39+
retryMaxDelayMs?: number;
40+
/**
41+
* Maximum for the total number of attempts. `Infinity` is supported.
42+
*
43+
* Defaults to 1, i.e. a single retry will be attempted.
44+
*/
45+
retryMaxAttempts?: number;
46+
/**
47+
* Array of retryable status codes.
48+
*
49+
* Default is `[UNKNOWN, RESOURCE_EXHAUSTED, INTERNAL, UNAVAILABLE]`.
50+
*/
51+
retryableStatuses?: Status[];
52+
/**
53+
* Called after receiving error with retryable status code before setting
54+
* backoff delay timer.
55+
*
56+
* If the error code is not retryable, or the maximum attempts exceeded, this
57+
* function will not be called and the error will be thrown from the client
58+
* method.
59+
*/
60+
onRetryableError?(error: ClientError, attempt: number, delayMs: number): void;
61+
};
62+
63+
export interface TokenService {
64+
getToken: () => Promise<string>;
65+
}
66+
67+
export interface GeneratedServiceClientCtor<T extends ServiceDefinition> {
68+
service: T;
69+
70+
new (
71+
address: string,
72+
credentials: ChannelCredentials,
73+
options?: Partial<ChannelOptions>,
74+
): Client;
75+
}
76+
77+
export interface IIAmCredentials {
78+
serviceAccountId: string;
79+
accessKeyId: string;
80+
privateKey: Buffer | string;
81+
}
82+
83+
export interface ISslCredentials {
84+
rootCertificates?: Buffer;
85+
clientPrivateKey?: Buffer;
86+
clientCertChain?: Buffer;
87+
}
88+
89+
export interface ChannelSslOptions {
90+
rootCerts?: Buffer;
91+
privateKey?: Buffer;
92+
certChain?: Buffer;
93+
}
94+
95+
export interface GenericCredentialsConfig {
96+
pollInterval?: number;
97+
ssl?: ChannelSslOptions;
98+
headers?: Record<string, string>;
99+
}
100+
101+
export interface OAuthCredentialsConfig extends GenericCredentialsConfig {
102+
oauthToken: string;
103+
}
104+
105+
export interface IamTokenCredentialsConfig extends GenericCredentialsConfig {
106+
iamToken: string;
107+
}
108+
109+
export interface ServiceAccountCredentialsConfig extends GenericCredentialsConfig {
110+
serviceAccountJson: IIAmCredentials;
111+
}
112+
113+
export type SessionConfig =
114+
| OAuthCredentialsConfig
115+
| IamTokenCredentialsConfig
116+
| ServiceAccountCredentialsConfig
117+
| GenericCredentialsConfig;
118+
119+
export type ClientCallArgs = DeadlineOptions & RetryOptions;
120+
121+
export type WrappedServiceClientType<S extends ServiceDefinition> = RawClient<
122+
NormalizedServiceDefinition<S>,
123+
ClientCallArgs
124+
>;
125+
126+
export type ClientType = <S extends ServiceDefinition>(
127+
clientClass: GeneratedServiceClientCtor<S>,
128+
customEndpoint?: string,
129+
) => WrappedServiceClientType<S>;
130+
131+
export type SessionArg = { client: ClientType };
132+
133+
export type TypeFromProtoc<
134+
T extends { $type: string },
135+
NotPartialKey extends keyof Omit<T, '$type'> = never,
136+
> = {
137+
[Key in NotPartialKey]: T[Key];
138+
} & DeepPartial<T>;

0 commit comments

Comments
 (0)