-
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
5 changed files
with
510 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,382 @@ | ||
import { ServiceDefinition } from '@grpc/grpc-js'; | ||
import { assistantService, messageService, runService, threadService } from '.'; | ||
import { ClientCallArgs, GeneratedServiceClientCtor, WrappedServiceClientType } from './types'; | ||
import { | ||
CreateThreadRequest, | ||
DeleteThreadRequest, | ||
GetThreadRequest, | ||
ListThreadsRequest, | ||
UpdateThreadRequest, | ||
} from './generated/yandex/cloud/ai/assistants/v1/threads/thread_service'; | ||
import { | ||
CreateAssistantRequest, | ||
DeleteAssistantRequest, | ||
GetAssistantRequest, | ||
ListAssistantsRequest, | ||
ListAssistantVersionsRequest, | ||
UpdateAssistantRequest, | ||
} from './generated/yandex/cloud/ai/assistants/v1/assistant_service'; | ||
import { | ||
CreateMessageRequest, | ||
GetMessageRequest, | ||
ListMessagesRequest, | ||
} from './generated/yandex/cloud/ai/assistants/v1/threads/message_service'; | ||
import { DeepPartial } from './generated/typeRegistry'; | ||
import { | ||
CreateRunRequest, | ||
GetLastRunByThreadRequest, | ||
GetRunRequest, | ||
ListenRunRequest, | ||
ListRunsRequest, | ||
} from './generated/yandex/cloud/ai/assistants/v1/runs/run_service'; | ||
import { CallOptions } from 'nice-grpc'; | ||
import { Thread } from './generated/yandex/cloud/ai/assistants/v1/threads/thread'; | ||
import { Assistant } from './generated/yandex/cloud/ai/assistants/v1/assistant'; | ||
import { Run } from './generated/yandex/cloud/ai/assistants/v1/runs/run'; | ||
|
||
type ClientType = <S extends ServiceDefinition>( | ||
clientClass: GeneratedServiceClientCtor<S>, | ||
customEndpoint?: string, | ||
) => WrappedServiceClientType<S>; | ||
|
||
type SessionArg = { client: ClientType }; | ||
|
||
type TypeFromProtoc< | ||
T extends { $type: string }, | ||
NotPartialKey extends keyof Omit<T, '$type'> = never, | ||
> = { | ||
[Key in NotPartialKey]: T[Key]; | ||
} & DeepPartial<T>; | ||
|
||
type SendMessageProps = TypeFromProtoc<CreateMessageRequest, 'threadId'>; | ||
|
||
type GetMessageProps = TypeFromProtoc<GetMessageRequest, 'threadId' | 'messageId'>; | ||
|
||
type ListMessagesProps = TypeFromProtoc<ListMessagesRequest, 'threadId'>; | ||
|
||
export const initMessageSdk = (session: SessionArg) => { | ||
const messageClient = session.client(messageService.MessageServiceClient); | ||
|
||
const send = (params: SendMessageProps, args?: ClientCallArgs) => { | ||
const p = messageClient.create( | ||
messageService.CreateMessageRequest.fromPartial(params), | ||
args, | ||
); | ||
|
||
return p; | ||
}; | ||
|
||
const get = (params: GetMessageProps, args?: ClientCallArgs) => { | ||
const p = messageClient.get(messageService.GetMessageRequest.fromPartial(params), args); | ||
return p; | ||
}; | ||
|
||
const list = (params: ListMessagesProps, args?: ClientCallArgs) => { | ||
const p = messageClient.list(messageService.ListMessagesRequest.fromPartial(params), args); | ||
return p; | ||
}; | ||
|
||
return { send, get, list }; | ||
}; | ||
|
||
type MessageSdk = ReturnType<typeof initMessageSdk>; | ||
|
||
type CreateThreadProps = TypeFromProtoc<CreateThreadRequest, 'folderId' | 'name'>; | ||
|
||
type GetThreadProps = TypeFromProtoc<GetThreadRequest, 'threadId'>; | ||
|
||
type ListThreadProps = TypeFromProtoc<ListThreadsRequest, 'folderId'>; | ||
|
||
type DeleteThreadProps = TypeFromProtoc<DeleteThreadRequest, 'threadId'>; | ||
|
||
type UpdateThreadProps = TypeFromProtoc<UpdateThreadRequest, 'threadId'>; | ||
|
||
export const threadWithSdk = <T extends Pick<Thread, 'id'>>( | ||
session: SessionArg, | ||
thread: T, | ||
{ | ||
threadSdk = initThreadSdk(session), | ||
messageSdk = initMessageSdk(session), | ||
}: { | ||
messageSdk?: MessageSdk; | ||
threadSdk?: ThreadSdk; | ||
}, | ||
) => { | ||
const sendMessage = (props: Omit<SendMessageProps, 'threadId'>, args?: ClientCallArgs) => { | ||
return messageSdk.send({ threadId: thread.id, ...props }, args); | ||
}; | ||
|
||
const listMessages = (props: Omit<ListMessagesProps, 'threadId'>, args?: ClientCallArgs) => { | ||
return messageSdk.list({ threadId: thread.id, ...props }, args); | ||
}; | ||
|
||
const getMessage = (props: Omit<GetMessageProps, 'threadId'>, args?: ClientCallArgs) => { | ||
return messageSdk.get({ threadId: thread.id, ...props }, args); | ||
}; | ||
|
||
const deleteInnerMethod = (args?: ClientCallArgs) => { | ||
return threadSdk.delete({ threadId: thread.id }, args); | ||
}; | ||
|
||
const updateMethod = (props: Omit<UpdateThreadProps, 'threadId'>, args?: ClientCallArgs) => { | ||
return threadSdk.update({ ...props, threadId: thread.id }, args); | ||
}; | ||
|
||
return Object.assign(thread, { | ||
sendMessage, | ||
listMessages, | ||
getMessage, | ||
delete: deleteInnerMethod, | ||
update: updateMethod, | ||
}); | ||
}; | ||
|
||
export const initThreadSdk = (session: SessionArg) => { | ||
const threadClient = session.client(threadService.ThreadServiceClient); | ||
|
||
const _connectWithSdk = (threadP: Promise<Thread>) => { | ||
const externalSession = session; | ||
|
||
const withSdk = async (session?: SessionArg | null, messageSdk?: MessageSdk) => { | ||
const thread = await threadP; | ||
return threadWithSdk<Thread>(session || externalSession, thread, { | ||
threadSdk, | ||
messageSdk, | ||
}); | ||
}; | ||
|
||
return { withSdk }; | ||
}; | ||
|
||
const get = (params: GetThreadProps, args?: ClientCallArgs) => { | ||
const p = threadClient.get(threadService.GetThreadRequest.fromPartial(params), args); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const list = (params: ListThreadProps, args?: ClientCallArgs) => { | ||
return threadClient.list(threadService.ListThreadsRequest.fromPartial(params), args); | ||
}; | ||
|
||
const deleteMethod = (params: DeleteThreadProps, args?: ClientCallArgs) => { | ||
return threadClient.delete(threadService.DeleteThreadRequest.fromPartial(params), args); | ||
}; | ||
|
||
const update = (params: UpdateThreadProps, args?: ClientCallArgs) => { | ||
const p = threadClient.update(threadService.UpdateThreadRequest.fromPartial(params), args); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const create = (params: CreateThreadProps, args?: ClientCallArgs) => { | ||
const p = threadClient.create(threadService.CreateThreadRequest.fromPartial(params), args); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const threadSdk = { create, delete: deleteMethod, get, list, update }; | ||
|
||
return threadSdk; | ||
}; | ||
|
||
type ThreadSdk = ReturnType<typeof initThreadSdk>; | ||
|
||
type CreateAssistantProps = Omit<TypeFromProtoc<CreateAssistantRequest, 'folderId'>, 'modelUri'> & { | ||
modelId: string; | ||
}; | ||
|
||
type GetAssistantProps = TypeFromProtoc<GetAssistantRequest, 'assistantId'>; | ||
|
||
type ListAssistantProps = TypeFromProtoc<ListAssistantsRequest, 'folderId'>; | ||
|
||
type DeleteAssistantProps = TypeFromProtoc<DeleteAssistantRequest, 'assistantId'>; | ||
|
||
type ListAssistantVersionsProps = TypeFromProtoc<ListAssistantVersionsRequest, 'assistantId'>; | ||
|
||
type UpdateAssistantProps = TypeFromProtoc<UpdateAssistantRequest, 'assistantId'>; | ||
|
||
export const assistantWithSdk = <T extends Pick<Assistant, 'id'>>( | ||
session: SessionArg, | ||
assistant: T, | ||
{ | ||
assistantSdk = initAssistantSdk(session), | ||
}: { | ||
assistantSdk?: AssistantSdk; | ||
}, | ||
) => { | ||
const deleteMethod = ( | ||
params: Omit<DeleteAssistantProps, 'assistantId'>, | ||
args?: ClientCallArgs, | ||
) => { | ||
return assistantSdk.delete({ ...params, assistantId: assistant.id }, args); | ||
}; | ||
|
||
const update = (params: Omit<UpdateAssistantProps, 'assistantId'>, args?: ClientCallArgs) => { | ||
return assistantSdk.update({ ...params, assistantId: assistant.id }, args); | ||
}; | ||
|
||
return Object.assign(assistant, { update, delete: deleteMethod }); | ||
}; | ||
|
||
export const initAssistantSdk = (session: SessionArg) => { | ||
const assistantClient = session.client(assistantService.AssistantServiceClient); | ||
|
||
const _connectWithSdk = (assistantP: Promise<Assistant>) => { | ||
const externalSession = session; | ||
|
||
const withSdk = async (session?: SessionArg | null) => { | ||
const thread = await assistantP; | ||
return assistantWithSdk<Assistant>(session || externalSession, thread, { | ||
assistantSdk, | ||
}); | ||
}; | ||
|
||
return { withSdk }; | ||
}; | ||
|
||
const create = async (params: CreateAssistantProps, args?: ClientCallArgs) => { | ||
const { modelId, ...restParams } = params; | ||
|
||
const p = assistantClient.create( | ||
assistantService.CreateAssistantRequest.fromPartial({ | ||
...restParams, | ||
modelUri: `gpt://${params.folderId}/${modelId}`, | ||
}), | ||
args, | ||
); | ||
|
||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const get = (params: GetAssistantProps, args?: ClientCallArgs) => { | ||
const p = assistantClient.get( | ||
assistantService.GetAssistantRequest.fromPartial(params), | ||
args, | ||
); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const list = (params: ListAssistantProps, args?: ClientCallArgs) => { | ||
const p = assistantClient.list( | ||
assistantService.ListAssistantsRequest.fromPartial(params), | ||
args, | ||
); | ||
|
||
return p; | ||
}; | ||
|
||
const deleteMethod = (params: DeleteAssistantProps, args?: ClientCallArgs) => { | ||
const p = assistantClient.delete( | ||
assistantService.DeleteAssistantRequest.fromPartial(params), | ||
args, | ||
); | ||
return p; | ||
}; | ||
|
||
const listVersions = (params: ListAssistantVersionsProps, args?: ClientCallArgs) => { | ||
const p = assistantClient.listVersions( | ||
assistantService.ListAssistantVersionsRequest.fromPartial(params), | ||
args, | ||
); | ||
return p; | ||
}; | ||
|
||
const update = (params: UpdateAssistantProps, args?: ClientCallArgs) => { | ||
const p = assistantClient.update( | ||
assistantService.UpdateAssistantRequest.fromPartial(params), | ||
args, | ||
); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const assistantSdk = { create, get, list, delete: deleteMethod, listVersions, update }; | ||
return assistantSdk; | ||
}; | ||
|
||
type AssistantSdk = ReturnType<typeof initAssistantSdk>; | ||
|
||
type GetRunProps = TypeFromProtoc<GetRunRequest, 'runId'>; | ||
|
||
type CreateRunProps = TypeFromProtoc<CreateRunRequest, 'threadId' | 'assistantId'>; | ||
|
||
type GetLastRunByThreadProps = TypeFromProtoc<GetLastRunByThreadRequest, 'threadId'>; | ||
|
||
type ListRunsProps = TypeFromProtoc<ListRunsRequest, 'folderId'>; | ||
|
||
type ListenRunProps = TypeFromProtoc<ListenRunRequest, 'runId' | 'eventsStartIdx'>; | ||
|
||
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 const initRunSdk = (session: SessionArg) => { | ||
const runClient = session.client(runService.RunServiceClient); | ||
|
||
const _connectWithSdk = (runP: Promise<Run>) => { | ||
const externalSession = session; | ||
|
||
const withSdk = async (session?: SessionArg | null) => { | ||
const run = await runP; | ||
return runWithSdk<Run>(session || externalSession, run, { | ||
runSdk, | ||
}); | ||
}; | ||
|
||
return { withSdk }; | ||
}; | ||
|
||
const create = (params: CreateRunProps, args?: ClientCallArgs) => { | ||
const p = runClient.create(runService.CreateRunRequest.fromPartial(params), args); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const get = (params: GetRunProps, args?: ClientCallArgs) => { | ||
const p = runClient.get(runService.GetRunRequest.fromPartial(params), args); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const getLastByThread = (params: GetLastRunByThreadProps, args?: ClientCallArgs) => { | ||
const p = runClient.getLastByThread( | ||
runService.GetLastRunByThreadRequest.fromPartial(params), | ||
args, | ||
); | ||
return Object.assign(p, _connectWithSdk(p)); | ||
}; | ||
|
||
const list = (params: ListRunsProps, args?: ClientCallArgs) => { | ||
const p = runClient.list(runService.ListRunsRequest.fromPartial(params), args); | ||
return p; | ||
}; | ||
|
||
const listen = (params: ListenRunProps, args?: ClientCallArgs & CallOptions) => { | ||
return runClient.listen(runService.ListenRunRequest.fromPartial(params), args); | ||
}; | ||
|
||
const runSdk = { create, get, getLastByThread, list, listen }; | ||
return runSdk; | ||
}; | ||
|
||
type RunSdk = ReturnType<typeof initRunSdk>; | ||
|
||
export const sdk = (session: SessionArg) => { | ||
const run = initRunSdk(session); | ||
const assistant = initAssistantSdk(session); | ||
const message = initMessageSdk(session); | ||
const thread = initThreadSdk(session); | ||
|
||
return { | ||
run, | ||
assistant, | ||
message, | ||
thread, | ||
}; | ||
}; |
Oops, something went wrong.