refactor(core): get copilot sessions api (#10168)

Fix issue [BS-2575](https://linear.app/affine-design/issue/BS-2575).

### What Changed?
- Refactor `getCopilotSessions` api.
  - Add `docId` parameter.
  - Add `action` parameter.
This commit is contained in:
akumatus
2025-02-14 06:57:57 +00:00
parent f20e3f6d8f
commit 1bf1832211
10 changed files with 82 additions and 96 deletions

View File

@@ -129,6 +129,12 @@ enum ChatHistoryOrder {
registerEnumType(ChatHistoryOrder, { name: 'ChatHistoryOrder' });
@InputType()
class QueryChatSessionsInput {
@Field(() => Boolean, { nullable: true })
action: boolean | undefined;
}
@InputType()
class QueryChatHistoriesInput implements Partial<ListHistoriesOptions> {
@Field(() => Boolean, { nullable: true })
@@ -274,6 +280,9 @@ class CopilotPromptType {
export class CopilotType {
@Field(() => ID, { nullable: true })
workspaceId!: string | undefined;
@Field(() => ID, { nullable: true })
docId!: string | undefined;
}
@Throttle()
@@ -296,31 +305,23 @@ export class CopilotResolver {
}
@ResolveField(() => [String], {
description: 'Get the session list of chats in the workspace',
description: 'Get the session list in the workspace',
complexity: 2,
})
async chats(
async sessionIds(
@Parent() copilot: CopilotType,
@CurrentUser() user: CurrentUser
@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.listSessions(user.id, copilot.workspaceId);
}
@ResolveField(() => [String], {
description: 'Get the session list of actions in the workspace',
complexity: 2,
})
async actions(
@Parent() copilot: CopilotType,
@CurrentUser() user: CurrentUser
) {
if (!copilot.workspaceId) return [];
await this.permissions.checkCloudWorkspace(copilot.workspaceId, user.id);
return await this.chatSession.listSessions(user.id, copilot.workspaceId, {
action: true,
});
return await this.chatSession.listSessionIds(
user.id,
copilot.workspaceId,
docId,
options
);
}
@ResolveField(() => [CopilotHistoriesType], {})

View File

@@ -391,17 +391,18 @@ export class ChatSessionService {
.reduce((prev, cost) => prev + cost, 0);
}
async listSessions(
async listSessionIds(
userId: string,
workspaceId: string,
options?: { docId?: string; action?: boolean }
docId?: string,
options?: { action?: boolean }
): Promise<string[]> {
return await this.db.aiSession
.findMany({
where: {
userId,
workspaceId,
docId: workspaceId === options?.docId ? undefined : options?.docId,
docId,
prompt: {
action: options?.action ? { not: null } : null,
},

View File

@@ -37,18 +37,16 @@ enum ContextFileStatus {
}
type Copilot {
"""Get the session list of actions in the workspace"""
actions: [String!]!
"""Get the session list of chats in the workspace"""
chats: [String!]!
"""Get the context list of a session"""
contexts(contextId: String, sessionId: String!): [CopilotContext!]!
docId: ID
histories(docId: String, options: QueryChatHistoriesInput): [CopilotHistories!]!
"""Get the quota of the user in the workspace"""
quota: CopilotQuota!
"""Get the session list in the workspace"""
sessionIds(docId: String, options: QueryChatSessionsInput): [String!]!
workspaceId: ID
}
@@ -884,6 +882,10 @@ input QueryChatHistoriesInput {
withPrompt: Boolean
}
input QueryChatSessionsInput {
action: Boolean
}
type QueryTooLongDataType {
max: Int!
}