mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-31 09:09:54 +08:00
feat: ai create an image (#6538)
Related to https://github.com/toeverything/blocksuite/pull/6746
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
import { assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import { AIProvider } from '@blocksuite/presets';
|
import { AIProvider } from '@blocksuite/presets';
|
||||||
|
|
||||||
import { createChatSession, listHistories, textToText } from './request';
|
import {
|
||||||
|
createChatSession,
|
||||||
|
listHistories,
|
||||||
|
textToText,
|
||||||
|
toImage,
|
||||||
|
} from './request';
|
||||||
|
|
||||||
export function setupAIProvider() {
|
export function setupAIProvider() {
|
||||||
// a single workspace should have only a single chat session
|
// a single workspace should have only a single chat session
|
||||||
@@ -235,4 +240,14 @@ export function setupAIProvider() {
|
|||||||
return (await listHistories(workspaceId, docId)) ?? [];
|
return (await listHistories(workspaceId, docId)) ?? [];
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AIProvider.provide('createImage', options => {
|
||||||
|
// const promptName = 'debug:action:fal-sd15';
|
||||||
|
const promptName = 'debug:action:dalle3';
|
||||||
|
return toImage({
|
||||||
|
...options,
|
||||||
|
promptName,
|
||||||
|
forceToImage: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ export type TextToTextOptions = {
|
|||||||
params?: Record<string, string>;
|
params?: Record<string, string>;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
|
forceToImage?: boolean; // force to image
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createChatSession({
|
export function createChatSession({
|
||||||
@@ -56,33 +57,43 @@ async function createSessionMessage({
|
|||||||
sessionId: providedSessionId,
|
sessionId: providedSessionId,
|
||||||
attachments,
|
attachments,
|
||||||
params,
|
params,
|
||||||
|
forceToImage,
|
||||||
}: TextToTextOptions) {
|
}: TextToTextOptions) {
|
||||||
const hasAttachments = attachments && attachments.length > 0;
|
|
||||||
if (!promptName && !providedSessionId) {
|
if (!promptName && !providedSessionId) {
|
||||||
throw new Error('promptName or sessionId is required');
|
throw new Error('promptName or sessionId is required');
|
||||||
}
|
}
|
||||||
|
const hasAttachments = attachments && attachments.length > 0;
|
||||||
const sessionId = await (providedSessionId ??
|
const sessionId = await (providedSessionId ??
|
||||||
client.createSession({
|
client.createSession({
|
||||||
workspaceId,
|
workspaceId,
|
||||||
docId,
|
docId,
|
||||||
promptName: promptName as string,
|
promptName: promptName as string,
|
||||||
}));
|
}));
|
||||||
if (hasAttachments) {
|
|
||||||
const normalizedAttachments = await Promise.all(
|
if (forceToImage || hasAttachments) {
|
||||||
attachments.map(async attachment => {
|
const options = {
|
||||||
if (typeof attachment === 'string') {
|
sessionId,
|
||||||
return attachment;
|
|
||||||
}
|
|
||||||
const url = await readBlobAsURL(attachment);
|
|
||||||
return url;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
const messageId = await client.createMessage({
|
|
||||||
sessionId: sessionId,
|
|
||||||
content,
|
content,
|
||||||
attachments: normalizedAttachments,
|
|
||||||
params,
|
params,
|
||||||
});
|
} as {
|
||||||
|
sessionId: string;
|
||||||
|
content?: string;
|
||||||
|
params?: Record<string, string>;
|
||||||
|
attachments?: string[];
|
||||||
|
};
|
||||||
|
if (hasAttachments) {
|
||||||
|
const normalizedAttachments = await Promise.all(
|
||||||
|
attachments.map(async attachment => {
|
||||||
|
if (typeof attachment === 'string') {
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
const url = await readBlobAsURL(attachment);
|
||||||
|
return url;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
options.attachments = normalizedAttachments;
|
||||||
|
}
|
||||||
|
const messageId = await client.createMessage(options);
|
||||||
return {
|
return {
|
||||||
messageId,
|
messageId,
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -126,7 +137,7 @@ export function textToText({
|
|||||||
messageId: message.messageId,
|
messageId: message.messageId,
|
||||||
message: message.message,
|
message: message.message,
|
||||||
});
|
});
|
||||||
yield* toTextStream(eventSource, { timeout: timeout });
|
yield* toTextStream(eventSource, { timeout });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
@@ -158,3 +169,36 @@ export function textToText({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const listHistories = client.getHistories;
|
export const listHistories = client.getHistories;
|
||||||
|
|
||||||
|
// Only one image is currently being processed
|
||||||
|
export function toImage({
|
||||||
|
docId,
|
||||||
|
workspaceId,
|
||||||
|
promptName,
|
||||||
|
content,
|
||||||
|
attachments,
|
||||||
|
params,
|
||||||
|
forceToImage,
|
||||||
|
timeout = TIMEOUT,
|
||||||
|
}: TextToTextOptions) {
|
||||||
|
return {
|
||||||
|
[Symbol.asyncIterator]: async function* () {
|
||||||
|
const { messageId, sessionId } = await createSessionMessage({
|
||||||
|
docId,
|
||||||
|
workspaceId,
|
||||||
|
promptName,
|
||||||
|
content,
|
||||||
|
attachments,
|
||||||
|
params,
|
||||||
|
forceToImage,
|
||||||
|
});
|
||||||
|
|
||||||
|
const eventSource = client.imagesStream(
|
||||||
|
// @ts-expect-error: messageId should exist
|
||||||
|
messageId,
|
||||||
|
sessionId
|
||||||
|
);
|
||||||
|
yield* toTextStream(eventSource, { timeout, type: 'attachment' });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user