mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 10:06:17 +08:00
feat: text to image impl (#6437)
fix CLOUD-18 fix CLOUD-28 fix CLOUD-29
This commit is contained in:
@@ -3,43 +3,26 @@ import { randomUUID } from 'node:crypto';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
import { ChatMessageCache } from './message';
|
||||
import { ChatPrompt, PromptService } from './prompt';
|
||||
import {
|
||||
AvailableModel,
|
||||
ChatHistory,
|
||||
ChatMessage,
|
||||
ChatMessageSchema,
|
||||
ChatSessionOptions,
|
||||
ChatSessionState,
|
||||
getTokenEncoder,
|
||||
ListHistoriesOptions,
|
||||
PromptMessage,
|
||||
PromptMessageSchema,
|
||||
PromptParams,
|
||||
SubmittedMessage,
|
||||
} from './types';
|
||||
|
||||
export interface ChatSessionOptions {
|
||||
userId: string;
|
||||
workspaceId: string;
|
||||
docId: string;
|
||||
promptName: string;
|
||||
}
|
||||
|
||||
export interface ChatSessionState
|
||||
extends Omit<ChatSessionOptions, 'promptName'> {
|
||||
// connect ids
|
||||
sessionId: string;
|
||||
// states
|
||||
prompt: ChatPrompt;
|
||||
messages: ChatMessage[];
|
||||
}
|
||||
|
||||
export type ListHistoriesOptions = {
|
||||
action: boolean | undefined;
|
||||
limit: number | undefined;
|
||||
skip: number | undefined;
|
||||
sessionId: string | undefined;
|
||||
};
|
||||
|
||||
export class ChatSession implements AsyncDisposable {
|
||||
constructor(
|
||||
private readonly messageCache: ChatMessageCache,
|
||||
private readonly state: ChatSessionState,
|
||||
private readonly dispose?: (state: ChatSessionState) => Promise<void>,
|
||||
private readonly maxTokenSize = 3840
|
||||
@@ -60,6 +43,29 @@ export class ChatSession implements AsyncDisposable {
|
||||
this.state.messages.push(message);
|
||||
}
|
||||
|
||||
async getMessageById(messageId: string) {
|
||||
const message = await this.messageCache.get(messageId);
|
||||
if (!message || message.sessionId !== this.state.sessionId) {
|
||||
throw new Error(`Message not found: ${messageId}`);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
async pushByMessageId(messageId: string) {
|
||||
const message = await this.messageCache.get(messageId);
|
||||
if (!message || message.sessionId !== this.state.sessionId) {
|
||||
throw new Error(`Message not found: ${messageId}`);
|
||||
}
|
||||
|
||||
this.push({
|
||||
role: 'user',
|
||||
content: message.content,
|
||||
attachments: message.attachments,
|
||||
params: message.params,
|
||||
createdAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
pop() {
|
||||
this.state.messages.pop();
|
||||
}
|
||||
@@ -109,6 +115,7 @@ export class ChatSessionService {
|
||||
|
||||
constructor(
|
||||
private readonly db: PrismaClient,
|
||||
private readonly messageCache: ChatMessageCache,
|
||||
private readonly prompt: PromptService
|
||||
) {}
|
||||
|
||||
@@ -326,6 +333,10 @@ export class ChatSessionService {
|
||||
});
|
||||
}
|
||||
|
||||
async createMessage(message: SubmittedMessage): Promise<string | undefined> {
|
||||
return await this.messageCache.set(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* usage:
|
||||
* ``` typescript
|
||||
@@ -342,7 +353,7 @@ export class ChatSessionService {
|
||||
async get(sessionId: string): Promise<ChatSession | null> {
|
||||
const state = await this.getSession(sessionId);
|
||||
if (state) {
|
||||
return new ChatSession(state, async state => {
|
||||
return new ChatSession(this.messageCache, state, async state => {
|
||||
await this.setSession(state);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user