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 sdk #178

Open
wants to merge 9 commits into
base: alpha
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"import/prefer-default-export": "off",
"comma-dangle": ["error", "always-multiline"],
"indent": "off",
"@typescript-eslint/indent": ["error", 4],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это правило prettier'ом хэндлится?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6b0b997

не знаю что это за правило, случайно удалил

"max-len": ["error", 140],
"@typescript-eslint/indent": ["error", 4],
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/prefer-optional-chain": "error",
"prefer-arrow-functions/prefer-arrow-functions": ["error"],
Expand All @@ -51,6 +51,7 @@
"devDependencies": true
}
],
"import/no-cycle": "off"
"import/no-cycle": "off",
"@typescript-eslint/no-explicit-any": 1
}
}
139 changes: 139 additions & 0 deletions clients/ai-assistants-v1/sdk/assistantSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Client } from 'nice-grpc';
import { assistantService } from '..';
import { Assistant } from '../generated/yandex/cloud/ai/assistants/v1/assistant';
import {
AssistantServiceService,
CreateAssistantRequest,
DeleteAssistantRequest,
GetAssistantRequest,
ListAssistantsRequest,
ListAssistantVersionsRequest,
UpdateAssistantRequest,
} from '../generated/yandex/cloud/ai/assistants/v1/assistant_service';
import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';

export type CreateAssistantProps = Omit<
TypeFromProtoc<CreateAssistantRequest, 'folderId'>,
'modelUri'
> & {
modelId: string;
};

export type GetAssistantProps = TypeFromProtoc<GetAssistantRequest, 'assistantId'>;

export type ListAssistantProps = TypeFromProtoc<ListAssistantsRequest, 'folderId'>;

export type DeleteAssistantProps = TypeFromProtoc<DeleteAssistantRequest, 'assistantId'>;

export type ListAssistantVersionsProps = TypeFromProtoc<
ListAssistantVersionsRequest,
'assistantId'
>;

export type UpdateAssistantProps = TypeFromProtoc<UpdateAssistantRequest, 'assistantId'>;

export class AssistantWithSdk {
private assistantSdk: AssistantSdk;
private assistant: Assistant;

constructor(assistantSdk: AssistantSdk, assistant: Assistant) {
this.assistantSdk = assistantSdk;
this.assistant = assistant;
}

get data() {
return this.assistant;
}

private updateData(assistant: Assistant) {
this.assistant = assistant;
return this;
}

delete(params: Omit<DeleteAssistantProps, 'assistantId'>, args?: ClientCallArgs) {
const p = this.assistantSdk.delete({ ...params, assistantId: this.assistant.id }, args);
return p;
}

update(params: Omit<UpdateAssistantProps, 'assistantId'>, args?: ClientCallArgs) {
const p = this.assistantSdk.update({ ...params, assistantId: this.assistant.id }, args);
return p.then(this.updateData.bind(this));
}
}

export class AssistantSdk {
private assistantClient: Client<typeof AssistantServiceService, ClientCallArgs>;

constructor(session: SessionArg) {
this.assistantClient = session.client(assistantService.AssistantServiceClient);
}

private static _withSdk(this: AssistantSdk, assistantP: Promise<Assistant>) {
async function withSdk(this: AssistantSdk) {
const assistant = await assistantP;
return new AssistantWithSdk(this, assistant);
}

return Object.assign(assistantP, { withSdk: withSdk.bind(this) });
}

create(params: CreateAssistantProps, args?: ClientCallArgs) {
const { modelId, ...restParams } = params;

const p = this.assistantClient.create(
assistantService.CreateAssistantRequest.fromPartial({
...restParams,
modelUri: `gpt://${params.folderId}/${modelId}`,
}),
args,
);

return AssistantSdk._withSdk.call(this, p);
}

get(params: GetAssistantProps, args?: ClientCallArgs) {
const p = this.assistantClient.get(
assistantService.GetAssistantRequest.fromPartial(params),
args,
);
return AssistantSdk._withSdk.call(this, p);
}

list(params: ListAssistantProps, args?: ClientCallArgs) {
const p = this.assistantClient.list(
assistantService.ListAssistantsRequest.fromPartial(params),
args,
);

return p;
}

delete(params: DeleteAssistantProps, args?: ClientCallArgs) {
const p = this.assistantClient.delete(
assistantService.DeleteAssistantRequest.fromPartial(params),
args,
);
return p;
}

listVersions(params: ListAssistantVersionsProps, args?: ClientCallArgs) {
const p = this.assistantClient.listVersions(
assistantService.ListAssistantVersionsRequest.fromPartial(params),
args,
);
return p;
}

update(params: UpdateAssistantProps, args?: ClientCallArgs) {
const p = this.assistantClient.update(
assistantService.UpdateAssistantRequest.fromPartial(params),
args,
);

return AssistantSdk._withSdk.call(this, p);
}
}

export const initAssistantSdk = (session: SessionArg) => {
return new AssistantSdk(session);
};
4 changes: 4 additions & 0 deletions clients/ai-assistants-v1/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './assistantSdk';
export * from './messageSdk';
export * from './runSdk';
export * from './threadSdk';
71 changes: 71 additions & 0 deletions clients/ai-assistants-v1/sdk/messageSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { messageService } from '..';
import { MessageContent } from '../generated/yandex/cloud/ai/assistants/v1/threads/message';
import {
CreateMessageRequest,
GetMessageRequest,
ListMessagesRequest,
MessageServiceService,
} from '../generated/yandex/cloud/ai/assistants/v1/threads/message_service';
import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';
import { Client } from 'nice-grpc';

export type SendMessageProps = TypeFromProtoc<CreateMessageRequest, 'threadId'>;

export type GetMessageProps = TypeFromProtoc<GetMessageRequest, 'threadId' | 'messageId'>;

export type ListMessagesProps = TypeFromProtoc<ListMessagesRequest, 'threadId'>;

export class MessageSdk {
private messageClient: Client<typeof MessageServiceService, ClientCallArgs>;

constructor(session: SessionArg) {
this.messageClient = session.client(messageService.MessageServiceClient);
}

public static getMessageContent(...args: string[]): TypeFromProtoc<MessageContent> {
return { content: args.map((content) => ({ text: { content } })) };
}

public static messageContentToString(messageContent?: MessageContent): string {
return (
messageContent?.content.reduce((res, { text }) => {
if (text?.content) {
res += text.content;
}

return res;
}, '') ?? ''
);
}

public send(params: SendMessageProps, args?: ClientCallArgs) {
const p = this.messageClient.create(
messageService.CreateMessageRequest.fromPartial(params),
args,
);

return p;
}

get(params: GetMessageProps, args?: ClientCallArgs) {
const p = this.messageClient.get(
messageService.GetMessageRequest.fromPartial(params),
args,
);

return p;
}

list(params: ListMessagesProps, args?: ClientCallArgs) {
const p = this.messageClient.list(
messageService.ListMessagesRequest.fromPartial(params),
args,
);

return p;
}
}

export const initMessageSdk = (session: SessionArg) => {
return new MessageSdk(session);
};
92 changes: 92 additions & 0 deletions clients/ai-assistants-v1/sdk/runSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { ClientCallArgs, SessionArg, TypeFromProtoc } from './types';

import {
CreateRunRequest,
GetLastRunByThreadRequest,
GetRunRequest,
ListenRunRequest,
ListRunsRequest,
RunServiceService,
} from '../generated/yandex/cloud/ai/assistants/v1/runs/run_service';
import { Run } from '../generated/yandex/cloud/ai/assistants/v1/runs/run';
import { CallOptions } from '@grpc/grpc-js';
import { runService } from '..';
import { Client } from 'nice-grpc';

export type GetRunProps = TypeFromProtoc<GetRunRequest, 'runId'>;

export type CreateRunProps = TypeFromProtoc<CreateRunRequest, 'threadId' | 'assistantId'>;

export type GetLastRunByThreadProps = TypeFromProtoc<GetLastRunByThreadRequest, 'threadId'>;

export type ListRunsProps = TypeFromProtoc<ListRunsRequest, 'folderId'>;

export type ListenRunProps = TypeFromProtoc<ListenRunRequest, 'runId'>;

type RunClientType = Client<typeof RunServiceService, ClientCallArgs>;

export class RunWithSdk {
private runSdk: RunSdk;
private run: Run;

constructor(runSdk: RunSdk, run: Run) {
this.runSdk = runSdk;
this.run = run;
}

get data() {
return this.run;
}

listen(params: Omit<ListenRunProps, 'runId'>, args?: ClientCallArgs & CallOptions) {
return this.runSdk.listen({ ...params, runId: this.run.id }, args);
}
}

export class RunSdk {
private runClient: RunClientType;

constructor(session: SessionArg) {
this.runClient = session.client(runService.RunServiceClient);
}

private static _withSdk(this: RunSdk, runP: Promise<Run>) {
async function withSdk(this: RunSdk) {
const run = await runP;
return new RunWithSdk(this, run);
}

return Object.assign(runP, { withSdk: withSdk.bind(this) });
}

create(params: CreateRunProps, args?: ClientCallArgs) {
const p = this.runClient.create(runService.CreateRunRequest.fromPartial(params), args);
return RunSdk._withSdk.call(this, p);
}

get(params: GetRunProps, args?: ClientCallArgs) {
const p = this.runClient.get(runService.GetRunRequest.fromPartial(params), args);
return RunSdk._withSdk.call(this, p);
}

getLastByThread(params: GetLastRunByThreadProps, args?: ClientCallArgs) {
const p = this.runClient.getLastByThread(
runService.GetLastRunByThreadRequest.fromPartial(params),
args,
);
return RunSdk._withSdk.call(this, p);
}

list(params: ListRunsProps, args?: ClientCallArgs) {
const p = this.runClient.list(runService.ListRunsRequest.fromPartial(params), args);
return p;
}

listen(params: ListenRunProps, args?: ClientCallArgs & CallOptions) {
return this.runClient.listen(runService.ListenRunRequest.fromPartial(params), args);
}
}

export const initRunSdk = (session: SessionArg) => {
return new RunSdk(session);
};
Loading
Loading