From 985906aa13d4423e0c9b092bd143e561033e8877 Mon Sep 17 00:00:00 2001 From: darkskygit Date: Thu, 27 Feb 2025 11:25:52 +0000 Subject: [PATCH] feat(server): copilot session prompt query (#10479) --- .../server/src/plugins/copilot/resolver.ts | 27 +++++++++++++++++-- .../server/src/plugins/copilot/session.ts | 16 ++++++++--- packages/backend/server/src/schema.gql | 10 ++++++- .../core/src/blocksuite/ai/actions/types.ts | 4 +-- .../src/blocksuite/ai/chat-panel/index.ts | 6 ++--- .../blocksuite/ai/provider/copilot-client.ts | 4 +-- .../blocksuite/ai/provider/setup-provider.tsx | 4 +-- .../src/graphql/copilot-sessions-get.gql | 5 +++- .../frontend/graphql/src/graphql/index.ts | 5 +++- packages/frontend/graphql/src/schema.ts | 19 ++++++++++--- 10 files changed, 79 insertions(+), 21 deletions(-) diff --git a/packages/backend/server/src/plugins/copilot/resolver.ts b/packages/backend/server/src/plugins/copilot/resolver.ts index 788da54ffa..879f31a103 100644 --- a/packages/backend/server/src/plugins/copilot/resolver.ts +++ b/packages/backend/server/src/plugins/copilot/resolver.ts @@ -275,6 +275,15 @@ class CopilotPromptType { messages!: CopilotPromptMessageType[]; } +@ObjectType() +class CopilotSessionType { + @Field(() => ID) + id!: string; + + @Field(() => String) + promptName!: string; +} + // ================== Resolver ================== @ObjectType('Copilot') @@ -306,18 +315,32 @@ export class CopilotResolver { } @ResolveField(() => [String], { - description: 'Get the session list in the workspace', + description: 'Get the session id list in the workspace', complexity: 2, + deprecationReason: 'Use `sessions` instead', }) async sessionIds( @Parent() copilot: CopilotType, @CurrentUser() user: CurrentUser, @Args('docId', { nullable: true }) docId?: string, @Args('options', { nullable: true }) options?: QueryChatSessionsInput + ) { + return await this.sessions(copilot, user, docId, options); + } + + @ResolveField(() => [CopilotSessionType], { + description: 'Get the session list in the workspace', + complexity: 2, + }) + async sessions( + @Parent() copilot: CopilotType, + @CurrentUser() user: CurrentUser, + @Args('docId', { nullable: true }) docId?: string, + @Args('options', { nullable: true }) options?: QueryChatSessionsInput ) { if (!copilot.workspaceId) return []; await this.permissions.checkCloudWorkspace(copilot.workspaceId, user.id); - return await this.chatSession.listSessionIds( + return await this.chatSession.listSessions( user.id, copilot.workspaceId, docId, diff --git a/packages/backend/server/src/plugins/copilot/session.ts b/packages/backend/server/src/plugins/copilot/session.ts index 79837d7fcf..abd58457bc 100644 --- a/packages/backend/server/src/plugins/copilot/session.ts +++ b/packages/backend/server/src/plugins/copilot/session.ts @@ -396,12 +396,12 @@ export class ChatSessionService { .reduce((prev, cost) => prev + cost, 0); } - async listSessionIds( + async listSessions( userId: string, workspaceId: string, docId?: string, options?: { action?: boolean } - ): Promise { + ): Promise> { return await this.db.aiSession .findMany({ where: { @@ -413,9 +413,17 @@ export class ChatSessionService { }, deletedAt: null, }, - select: { id: true }, + select: { + id: true, + promptName: true, + }, }) - .then(sessions => sessions.map(({ id }) => id)); + .then(sessions => + sessions.map(({ id, promptName }) => ({ + id, + promptName, + })) + ); } async listHistories( diff --git a/packages/backend/server/src/schema.gql b/packages/backend/server/src/schema.gql index 8e614e0b1b..20df8bf2a8 100644 --- a/packages/backend/server/src/schema.gql +++ b/packages/backend/server/src/schema.gql @@ -45,8 +45,11 @@ type Copilot { """Get the quota of the user in the workspace""" quota: CopilotQuota! + """Get the session id list in the workspace""" + sessionIds(docId: String, options: QueryChatSessionsInput): [String!]! @deprecated(reason: "Use `sessions` instead") + """Get the session list in the workspace""" - sessionIds(docId: String, options: QueryChatSessionsInput): [String!]! + sessions(docId: String, options: QueryChatSessionsInput): [CopilotSessionType!]! workspaceId: ID } @@ -192,6 +195,11 @@ type CopilotQuota { used: SafeInt! } +type CopilotSessionType { + id: ID! + promptName: String! +} + input CreateChatMessageInput { attachments: [String!] blobs: [Upload!] diff --git a/packages/frontend/core/src/blocksuite/ai/actions/types.ts b/packages/frontend/core/src/blocksuite/ai/actions/types.ts index 23966b56db..462bc13b94 100644 --- a/packages/frontend/core/src/blocksuite/ai/actions/types.ts +++ b/packages/frontend/core/src/blocksuite/ai/actions/types.ts @@ -301,11 +301,11 @@ declare global { docId: string, promptName?: string ) => Promise; - getSessionIds: ( + getSessions: ( workspaceId: string, docId?: string, options?: { action?: boolean } - ) => Promise; + ) => Promise<{ id: string; promptName: string }[] | undefined>; updateSession: (sessionId: string, promptName: string) => Promise; } diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts index 96778a3302..e112e4cbc5 100644 --- a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts +++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts @@ -300,12 +300,12 @@ export class ChatPanel extends WithDisposable(ShadowlessElement) { const userId = (await AIProvider.userInfo)?.id; if (!userId) return; - const sessionIds = await AIProvider.session?.getSessionIds( + const sessions = await AIProvider.session?.getSessions( this.doc.workspace.id, this.doc.id ); - if (sessionIds?.length) { - this._chatSessionId = sessionIds[0]; + if (sessions?.length) { + this._chatSessionId = sessions?.[0].id; await this._updateHistory(); } if (this._chatSessionId) { diff --git a/packages/frontend/core/src/blocksuite/ai/provider/copilot-client.ts b/packages/frontend/core/src/blocksuite/ai/provider/copilot-client.ts index c0b6f0d8c7..e6697e19a9 100644 --- a/packages/frontend/core/src/blocksuite/ai/provider/copilot-client.ts +++ b/packages/frontend/core/src/blocksuite/ai/provider/copilot-client.ts @@ -136,7 +136,7 @@ export class CopilotClient { } } - async getSessionIds( + async getSessions( workspaceId: string, docId?: string, options?: RequestOptions< @@ -152,7 +152,7 @@ export class CopilotClient { options, }, }); - return res.currentUser?.copilot?.sessionIds; + return res.currentUser?.copilot?.sessions; } catch (err) { throw resolveError(err); } diff --git a/packages/frontend/core/src/blocksuite/ai/provider/setup-provider.tsx b/packages/frontend/core/src/blocksuite/ai/provider/setup-provider.tsx index 96c50592ad..45c214eb31 100644 --- a/packages/frontend/core/src/blocksuite/ai/provider/setup-provider.tsx +++ b/packages/frontend/core/src/blocksuite/ai/provider/setup-provider.tsx @@ -408,12 +408,12 @@ Could you make a new website based on these notes and send back just the html fi promptName, }); }, - getSessionIds: async ( + getSessions: async ( workspaceId: string, docId?: string, options?: { action?: boolean } ) => { - return client.getSessionIds(workspaceId, docId, options); + return client.getSessions(workspaceId, docId, options); }, updateSession: async (sessionId: string, promptName: string) => { return client.updateSession({ diff --git a/packages/frontend/graphql/src/graphql/copilot-sessions-get.gql b/packages/frontend/graphql/src/graphql/copilot-sessions-get.gql index 7bf4083d37..05f5fb0a48 100644 --- a/packages/frontend/graphql/src/graphql/copilot-sessions-get.gql +++ b/packages/frontend/graphql/src/graphql/copilot-sessions-get.gql @@ -5,7 +5,10 @@ query getCopilotSessions( ) { currentUser { copilot(workspaceId: $workspaceId) { - sessionIds(docId: $docId, options: $options) + sessions(docId: $docId, options: $options) { + id + promptName + } } } } diff --git a/packages/frontend/graphql/src/graphql/index.ts b/packages/frontend/graphql/src/graphql/index.ts index a0a7685fea..2a7ff9bd3d 100644 --- a/packages/frontend/graphql/src/graphql/index.ts +++ b/packages/frontend/graphql/src/graphql/index.ts @@ -424,7 +424,10 @@ export const getCopilotSessionsQuery = { query getCopilotSessions($workspaceId: String!, $docId: String, $options: QueryChatSessionsInput) { currentUser { copilot(workspaceId: $workspaceId) { - sessionIds(docId: $docId, options: $options) + sessions(docId: $docId, options: $options) { + id + promptName + } } } }`, diff --git a/packages/frontend/graphql/src/schema.ts b/packages/frontend/graphql/src/schema.ts index b8052e050c..baf159fbd6 100644 --- a/packages/frontend/graphql/src/schema.ts +++ b/packages/frontend/graphql/src/schema.ts @@ -83,7 +83,7 @@ export interface Copilot { /** Get the quota of the user in the workspace */ quota: CopilotQuota; /** Get the session list in the workspace */ - sessionIds: Array; + sessions: Array; workspaceId: Maybe; } @@ -97,7 +97,7 @@ export interface CopilotHistoriesArgs { options?: InputMaybe; } -export interface CopilotSessionIdsArgs { +export interface CopilotSessionsArgs { docId?: InputMaybe; options?: InputMaybe; } @@ -259,6 +259,12 @@ export interface CopilotQuota { used: Scalars['SafeInt']['output']; } +export interface CopilotSessionType { + __typename?: 'CopilotSessionType'; + id: Scalars['ID']['output']; + promptName: Scalars['String']['output']; +} + export interface CreateChatMessageInput { attachments?: InputMaybe>; blobs?: InputMaybe>; @@ -2236,7 +2242,14 @@ export type GetCopilotSessionsQuery = { __typename?: 'Query'; currentUser: { __typename?: 'UserType'; - copilot: { __typename?: 'Copilot'; sessionIds: Array }; + copilot: { + __typename?: 'Copilot'; + sessions: Array<{ + __typename?: 'CopilotSessionType'; + id: string; + promptName: string; + }>; + }; } | null; };