-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
654 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.