mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-05-09 00:20:09 +00:00
fix lint type error
This commit is contained in:
@@ -1,115 +1,117 @@
|
||||
import { App } from 'obsidian'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
import { ChatConversationMeta } from '../../../types/chat'
|
||||
import { AbstractJsonRepository } from '../base'
|
||||
import { CHAT_DIR, ROOT_DIR } from '../constants'
|
||||
import { EmptyChatTitleException } from '../exception'
|
||||
|
||||
import {
|
||||
CHAT_SCHEMA_VERSION,
|
||||
ChatConversation,
|
||||
ChatConversationMetadata,
|
||||
CHAT_SCHEMA_VERSION,
|
||||
ChatConversation
|
||||
} from './types'
|
||||
|
||||
|
||||
export class ChatManager extends AbstractJsonRepository<
|
||||
ChatConversation,
|
||||
ChatConversationMetadata
|
||||
ChatConversation,
|
||||
ChatConversationMeta
|
||||
> {
|
||||
constructor(app: App) {
|
||||
super(app, `${ROOT_DIR}/${CHAT_DIR}`)
|
||||
}
|
||||
constructor(app: App) {
|
||||
super(app, `${ROOT_DIR}/${CHAT_DIR}`)
|
||||
}
|
||||
|
||||
protected generateFileName(chat: ChatConversation): string {
|
||||
// Format: v{schemaVersion}_{title}_{updatedAt}_{id}.json
|
||||
const encodedTitle = encodeURIComponent(chat.title)
|
||||
return `v${chat.schemaVersion}_${encodedTitle}_${chat.updatedAt}_${chat.id}.json`
|
||||
}
|
||||
protected generateFileName(chat: ChatConversation): string {
|
||||
// Format: v{schemaVersion}_{title}_{updatedAt}_{id}.json
|
||||
const encodedTitle = encodeURIComponent(chat.title)
|
||||
return `v${chat.schemaVersion}_${encodedTitle}_${chat.updatedAt}_${chat.id}.json`
|
||||
}
|
||||
|
||||
protected parseFileName(fileName: string): ChatConversationMetadata | null {
|
||||
// Parse: v{schemaVersion}_{title}_{updatedAt}_{id}.json
|
||||
const regex = new RegExp(
|
||||
`^v${CHAT_SCHEMA_VERSION}_(.+)_(\\d+)_([0-9a-f-]+)\\.json$`,
|
||||
)
|
||||
const match = fileName.match(regex)
|
||||
if (!match) return null
|
||||
protected parseFileName(fileName: string): ChatConversationMeta | null {
|
||||
// Parse: v{schemaVersion}_{title}_{updatedAt}_{id}.json
|
||||
const regex = new RegExp(
|
||||
`^v${CHAT_SCHEMA_VERSION}_(.+)_(\\d+)_([0-9a-f-]+)\\.json$`,
|
||||
)
|
||||
const match = fileName.match(regex)
|
||||
if (!match) return null
|
||||
|
||||
const title = decodeURIComponent(match[1])
|
||||
const updatedAt = parseInt(match[2], 10)
|
||||
const id = match[3]
|
||||
const title = decodeURIComponent(match[1])
|
||||
const updatedAt = parseInt(match[2], 10)
|
||||
const id = match[3]
|
||||
|
||||
return {
|
||||
id,
|
||||
schemaVersion: CHAT_SCHEMA_VERSION,
|
||||
title,
|
||||
updatedAt,
|
||||
}
|
||||
}
|
||||
return {
|
||||
id,
|
||||
schemaVersion: CHAT_SCHEMA_VERSION,
|
||||
title,
|
||||
updatedAt,
|
||||
createdAt: 0,
|
||||
}
|
||||
}
|
||||
|
||||
public async createChat(
|
||||
initialData: Partial<ChatConversation>,
|
||||
): Promise<ChatConversation> {
|
||||
if (initialData.title && initialData.title.length === 0) {
|
||||
throw new EmptyChatTitleException()
|
||||
}
|
||||
public async createChat(
|
||||
initialData: Partial<ChatConversation>,
|
||||
): Promise<ChatConversation> {
|
||||
if (initialData.title && initialData.title.length === 0) {
|
||||
throw new EmptyChatTitleException()
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
const newChat: ChatConversation = {
|
||||
id: uuidv4(),
|
||||
title: 'New chat',
|
||||
messages: [],
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
schemaVersion: CHAT_SCHEMA_VERSION,
|
||||
...initialData,
|
||||
}
|
||||
const now = Date.now()
|
||||
const newChat: ChatConversation = {
|
||||
id: uuidv4(),
|
||||
title: 'New chat',
|
||||
messages: [],
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
schemaVersion: CHAT_SCHEMA_VERSION,
|
||||
...initialData,
|
||||
}
|
||||
|
||||
await this.create(newChat)
|
||||
return newChat
|
||||
}
|
||||
await this.create(newChat)
|
||||
return newChat
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<ChatConversation | null> {
|
||||
const allMetadata = await this.listMetadata()
|
||||
const targetMetadata = allMetadata.find((meta) => meta.id === id)
|
||||
public async findById(id: string): Promise<ChatConversation | null> {
|
||||
const allMetadata = await this.listMetadata()
|
||||
const targetMetadata = allMetadata.find((meta) => meta.id === id)
|
||||
|
||||
if (!targetMetadata) return null
|
||||
if (!targetMetadata) return null
|
||||
|
||||
return this.read(targetMetadata.fileName)
|
||||
}
|
||||
return this.read(targetMetadata.fileName)
|
||||
}
|
||||
|
||||
public async updateChat(
|
||||
id: string,
|
||||
updates: Partial<
|
||||
Omit<ChatConversation, 'id' | 'createdAt' | 'updatedAt' | 'schemaVersion'>
|
||||
>,
|
||||
): Promise<ChatConversation | null> {
|
||||
const chat = await this.findById(id)
|
||||
if (!chat) return null
|
||||
public async updateChat(
|
||||
id: string,
|
||||
updates: Partial<
|
||||
Omit<ChatConversation, 'id' | 'createdAt' | 'updatedAt' | 'schemaVersion'>
|
||||
>,
|
||||
): Promise<ChatConversation | null> {
|
||||
const chat = await this.findById(id)
|
||||
if (!chat) return null
|
||||
|
||||
if (updates.title !== undefined && updates.title.length === 0) {
|
||||
throw new EmptyChatTitleException()
|
||||
}
|
||||
if (updates.title !== undefined && updates.title.length === 0) {
|
||||
throw new EmptyChatTitleException()
|
||||
}
|
||||
|
||||
const updatedChat: ChatConversation = {
|
||||
...chat,
|
||||
...updates,
|
||||
updatedAt: Date.now(),
|
||||
}
|
||||
const updatedChat: ChatConversation = {
|
||||
...chat,
|
||||
...updates,
|
||||
updatedAt: Date.now(),
|
||||
}
|
||||
|
||||
await this.update(chat, updatedChat)
|
||||
return updatedChat
|
||||
}
|
||||
await this.update(chat, updatedChat)
|
||||
return updatedChat
|
||||
}
|
||||
|
||||
public async deleteChat(id: string): Promise<boolean> {
|
||||
const allMetadata = await this.listMetadata()
|
||||
const targetMetadata = allMetadata.find((meta) => meta.id === id)
|
||||
if (!targetMetadata) return false
|
||||
public async deleteChat(id: string): Promise<boolean> {
|
||||
const allMetadata = await this.listMetadata()
|
||||
const targetMetadata = allMetadata.find((meta) => meta.id === id)
|
||||
if (!targetMetadata) return false
|
||||
|
||||
await this.delete(targetMetadata.fileName)
|
||||
return true
|
||||
}
|
||||
await this.delete(targetMetadata.fileName)
|
||||
return true
|
||||
}
|
||||
|
||||
public async listChats(): Promise<ChatConversationMetadata[]> {
|
||||
const metadata = await this.listMetadata()
|
||||
return metadata.sort((a, b) => b.updatedAt - a.updatedAt)
|
||||
}
|
||||
public async listChats(): Promise<ChatConversationMeta[]> {
|
||||
const metadata = await this.listMetadata()
|
||||
return metadata.sort((a, b) => b.updatedAt - a.updatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user