feat(server): copilot session prompt query (#10479)

This commit is contained in:
darkskygit
2025-02-27 11:25:52 +00:00
parent d50860eee2
commit 985906aa13
10 changed files with 79 additions and 21 deletions

View File

@@ -275,6 +275,15 @@ class CopilotPromptType {
messages!: CopilotPromptMessageType[]; messages!: CopilotPromptMessageType[];
} }
@ObjectType()
class CopilotSessionType {
@Field(() => ID)
id!: string;
@Field(() => String)
promptName!: string;
}
// ================== Resolver ================== // ================== Resolver ==================
@ObjectType('Copilot') @ObjectType('Copilot')
@@ -306,18 +315,32 @@ export class CopilotResolver {
} }
@ResolveField(() => [String], { @ResolveField(() => [String], {
description: 'Get the session list in the workspace', description: 'Get the session id list in the workspace',
complexity: 2, complexity: 2,
deprecationReason: 'Use `sessions` instead',
}) })
async sessionIds( async sessionIds(
@Parent() copilot: CopilotType, @Parent() copilot: CopilotType,
@CurrentUser() user: CurrentUser, @CurrentUser() user: CurrentUser,
@Args('docId', { nullable: true }) docId?: string, @Args('docId', { nullable: true }) docId?: string,
@Args('options', { nullable: true }) options?: QueryChatSessionsInput @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 []; if (!copilot.workspaceId) return [];
await this.permissions.checkCloudWorkspace(copilot.workspaceId, user.id); await this.permissions.checkCloudWorkspace(copilot.workspaceId, user.id);
return await this.chatSession.listSessionIds( return await this.chatSession.listSessions(
user.id, user.id,
copilot.workspaceId, copilot.workspaceId,
docId, docId,

View File

@@ -396,12 +396,12 @@ export class ChatSessionService {
.reduce((prev, cost) => prev + cost, 0); .reduce((prev, cost) => prev + cost, 0);
} }
async listSessionIds( async listSessions(
userId: string, userId: string,
workspaceId: string, workspaceId: string,
docId?: string, docId?: string,
options?: { action?: boolean } options?: { action?: boolean }
): Promise<string[]> { ): Promise<Array<{ id: string; promptName: string }>> {
return await this.db.aiSession return await this.db.aiSession
.findMany({ .findMany({
where: { where: {
@@ -413,9 +413,17 @@ export class ChatSessionService {
}, },
deletedAt: null, 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( async listHistories(

View File

@@ -45,8 +45,11 @@ type Copilot {
"""Get the quota of the user in the workspace""" """Get the quota of the user in the workspace"""
quota: CopilotQuota! 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""" """Get the session list in the workspace"""
sessionIds(docId: String, options: QueryChatSessionsInput): [String!]! sessions(docId: String, options: QueryChatSessionsInput): [CopilotSessionType!]!
workspaceId: ID workspaceId: ID
} }
@@ -192,6 +195,11 @@ type CopilotQuota {
used: SafeInt! used: SafeInt!
} }
type CopilotSessionType {
id: ID!
promptName: String!
}
input CreateChatMessageInput { input CreateChatMessageInput {
attachments: [String!] attachments: [String!]
blobs: [Upload!] blobs: [Upload!]

View File

@@ -301,11 +301,11 @@ declare global {
docId: string, docId: string,
promptName?: string promptName?: string
) => Promise<string>; ) => Promise<string>;
getSessionIds: ( getSessions: (
workspaceId: string, workspaceId: string,
docId?: string, docId?: string,
options?: { action?: boolean } options?: { action?: boolean }
) => Promise<string[] | undefined>; ) => Promise<{ id: string; promptName: string }[] | undefined>;
updateSession: (sessionId: string, promptName: string) => Promise<string>; updateSession: (sessionId: string, promptName: string) => Promise<string>;
} }

View File

@@ -300,12 +300,12 @@ export class ChatPanel extends WithDisposable(ShadowlessElement) {
const userId = (await AIProvider.userInfo)?.id; const userId = (await AIProvider.userInfo)?.id;
if (!userId) return; if (!userId) return;
const sessionIds = await AIProvider.session?.getSessionIds( const sessions = await AIProvider.session?.getSessions(
this.doc.workspace.id, this.doc.workspace.id,
this.doc.id this.doc.id
); );
if (sessionIds?.length) { if (sessions?.length) {
this._chatSessionId = sessionIds[0]; this._chatSessionId = sessions?.[0].id;
await this._updateHistory(); await this._updateHistory();
} }
if (this._chatSessionId) { if (this._chatSessionId) {

View File

@@ -136,7 +136,7 @@ export class CopilotClient {
} }
} }
async getSessionIds( async getSessions(
workspaceId: string, workspaceId: string,
docId?: string, docId?: string,
options?: RequestOptions< options?: RequestOptions<
@@ -152,7 +152,7 @@ export class CopilotClient {
options, options,
}, },
}); });
return res.currentUser?.copilot?.sessionIds; return res.currentUser?.copilot?.sessions;
} catch (err) { } catch (err) {
throw resolveError(err); throw resolveError(err);
} }

View File

@@ -408,12 +408,12 @@ Could you make a new website based on these notes and send back just the html fi
promptName, promptName,
}); });
}, },
getSessionIds: async ( getSessions: async (
workspaceId: string, workspaceId: string,
docId?: string, docId?: string,
options?: { action?: boolean } options?: { action?: boolean }
) => { ) => {
return client.getSessionIds(workspaceId, docId, options); return client.getSessions(workspaceId, docId, options);
}, },
updateSession: async (sessionId: string, promptName: string) => { updateSession: async (sessionId: string, promptName: string) => {
return client.updateSession({ return client.updateSession({

View File

@@ -5,7 +5,10 @@ query getCopilotSessions(
) { ) {
currentUser { currentUser {
copilot(workspaceId: $workspaceId) { copilot(workspaceId: $workspaceId) {
sessionIds(docId: $docId, options: $options) sessions(docId: $docId, options: $options) {
id
promptName
}
} }
} }
} }

View File

@@ -424,7 +424,10 @@ export const getCopilotSessionsQuery = {
query getCopilotSessions($workspaceId: String!, $docId: String, $options: QueryChatSessionsInput) { query getCopilotSessions($workspaceId: String!, $docId: String, $options: QueryChatSessionsInput) {
currentUser { currentUser {
copilot(workspaceId: $workspaceId) { copilot(workspaceId: $workspaceId) {
sessionIds(docId: $docId, options: $options) sessions(docId: $docId, options: $options) {
id
promptName
}
} }
} }
}`, }`,

View File

@@ -83,7 +83,7 @@ export interface Copilot {
/** Get the quota of the user in the workspace */ /** Get the quota of the user in the workspace */
quota: CopilotQuota; quota: CopilotQuota;
/** Get the session list in the workspace */ /** Get the session list in the workspace */
sessionIds: Array<Scalars['String']['output']>; sessions: Array<CopilotSessionType>;
workspaceId: Maybe<Scalars['ID']['output']>; workspaceId: Maybe<Scalars['ID']['output']>;
} }
@@ -97,7 +97,7 @@ export interface CopilotHistoriesArgs {
options?: InputMaybe<QueryChatHistoriesInput>; options?: InputMaybe<QueryChatHistoriesInput>;
} }
export interface CopilotSessionIdsArgs { export interface CopilotSessionsArgs {
docId?: InputMaybe<Scalars['String']['input']>; docId?: InputMaybe<Scalars['String']['input']>;
options?: InputMaybe<QueryChatSessionsInput>; options?: InputMaybe<QueryChatSessionsInput>;
} }
@@ -259,6 +259,12 @@ export interface CopilotQuota {
used: Scalars['SafeInt']['output']; used: Scalars['SafeInt']['output'];
} }
export interface CopilotSessionType {
__typename?: 'CopilotSessionType';
id: Scalars['ID']['output'];
promptName: Scalars['String']['output'];
}
export interface CreateChatMessageInput { export interface CreateChatMessageInput {
attachments?: InputMaybe<Array<Scalars['String']['input']>>; attachments?: InputMaybe<Array<Scalars['String']['input']>>;
blobs?: InputMaybe<Array<Scalars['Upload']['input']>>; blobs?: InputMaybe<Array<Scalars['Upload']['input']>>;
@@ -2236,7 +2242,14 @@ export type GetCopilotSessionsQuery = {
__typename?: 'Query'; __typename?: 'Query';
currentUser: { currentUser: {
__typename?: 'UserType'; __typename?: 'UserType';
copilot: { __typename?: 'Copilot'; sessionIds: Array<string> }; copilot: {
__typename?: 'Copilot';
sessions: Array<{
__typename?: 'CopilotSessionType';
id: string;
promptName: string;
}>;
};
} | null; } | null;
}; };