Skip to content

Commit

Permalink
feat: ai-assistants-v1 sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Dec 16, 2024
1 parent 364e0c7 commit 43f3d4f
Show file tree
Hide file tree
Showing 9 changed files with 654 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"import/prefer-default-export": "off",
"comma-dangle": ["error", "always-multiline"],
"indent": "off",
"@typescript-eslint/indent": ["error", 4],
"max-len": ["error", 140],
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/prefer-optional-chain": "error",
Expand All @@ -51,6 +50,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);
};
108 changes: 108 additions & 0 deletions clients/ai-assistants-v1/sdk/runSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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 const runWithSdk = <T extends Pick<Run, 'id'>>(
session: SessionArg,
run: T,
{
runSdk = initRunSdk(session),
}: {
runSdk?: RunSdk;
},
) => {
const listen = (params: Omit<ListenRunProps, 'runId'>, args?: ClientCallArgs & CallOptions) => {
return runSdk.listen({ ...params, runId: run.id }, args);
};

return Object.assign(run, { listen });
};

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

0 comments on commit 43f3d4f

Please sign in to comment.