refactor(core): lazy create copilot session and context (#10170)

This commit is contained in:
akumatus
2025-02-14 14:48:46 +00:00
parent 5a42edf076
commit 631c8b8145
11 changed files with 179 additions and 170 deletions
@@ -30,30 +30,6 @@ export type ToImageOptions = TextToTextOptions & {
seed?: string;
};
export async function createChatSession({
client,
workspaceId,
docId,
promptName = 'Chat With AFFiNE AI',
}: {
client: CopilotClient;
workspaceId: string;
docId: string;
promptName?: string;
}) {
const sessionId = await client.createSession({
workspaceId,
docId,
promptName,
});
// always update the prompt name
await client.updateSession({
sessionId,
promptName,
});
return sessionId;
}
async function resizeImage(blob: Blob | File): Promise<Blob | null> {
let src = '';
try {
@@ -90,7 +66,7 @@ async function createSessionMessage({
client,
docId,
workspaceId,
promptName,
promptName = 'Chat With AFFiNE AI',
content,
sessionId: providedSessionId,
attachments,
@@ -102,11 +78,10 @@ async function createSessionMessage({
}
const hasAttachments = attachments && attachments.length > 0;
const sessionId = await (providedSessionId ??
createChatSession({
client,
client.createSession({
workspaceId,
docId,
promptName: promptName as string,
promptName,
}));
const options: Parameters<CopilotClient['createMessage']>[0] = {
@@ -11,7 +11,7 @@ import { z } from 'zod';
import type { CopilotClient } from './copilot-client';
import type { PromptKey } from './prompt';
import { createChatSession, textToText, toImage } from './request';
import { textToText, toImage } from './request';
import { setupTracker } from './tracker';
const filterStyleToPromptName = new Map(
@@ -37,13 +37,6 @@ export function setupAIProvider(
) {
//#region actions
AIProvider.provide('chat', options => {
const sessionId =
options.sessionId ??
createChatSession({
client,
workspaceId: options.workspaceId,
docId: options.docId,
});
const { input, docs, ...rest } = options;
const params = docs?.length
? {
@@ -58,7 +51,6 @@ export function setupAIProvider(
...rest,
client,
content: input,
sessionId,
params,
});
});
@@ -408,10 +400,9 @@ Could you make a new website based on these notes and send back just the html fi
createSession: async (
workspaceId: string,
docId: string,
promptName?: string
promptName = 'Chat With AFFiNE AI'
) => {
return createChatSession({
client,
return client.createSession({
workspaceId,
docId,
promptName,