-
Notifications
You must be signed in to change notification settings - Fork 14
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
[WIP] Assistant sdk #178
Open
GermanVor
wants to merge
7
commits into
alpha
Choose a base branch
from
assistant-sdk
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[WIP] Assistant sdk #178
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c0b16ac
feat: ai-assistants-v1 sdk
GermanVor 93cd9d8
chore: modifyPackageJSON
GermanVor 92c2b53
chore: export SDK
GermanVor 56ddf8c
chore: finally null
GermanVor 263d666
fix: exports
GermanVor ab8097f
chore: add example
GermanVor 6b0b997
chore: return @typescript-eslint/indent
GermanVor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -16,3 +16,5 @@ export * as thread from './generated/yandex/cloud/ai/assistants/v1/threads/threa | |
export * as threadService from './generated/yandex/cloud/ai/assistants/v1/threads/thread_service'; | ||
export * as user from './generated/yandex/cloud/ai/assistants/v1/users/user'; | ||
export * as userService from './generated/yandex/cloud/ai/assistants/v1/users/user_service'; | ||
|
||
export * as SDK from './sdk'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Че т я не очень понял, а откуда оно взялось? Я в proto не вижу каких-либо модулей There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ты про то, что оно появилось в // generated file ? |
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,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); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это правило prettier'ом хэндлится?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6b0b997
не знаю что это за правило, случайно удалил