From 20705eac6d2ce6d27b04217e4c318fb274449763 Mon Sep 17 00:00:00 2001 From: zAlweNy26 Date: Sun, 7 Jul 2024 19:51:57 +0200 Subject: [PATCH] chore: small tweaks --- src/factory/embedder.ts | 2 +- src/factory/llm.ts | 2 +- src/looking_glass/cheshire-cat.ts | 21 ++++++++++++--------- src/looking_glass/stray-cat.ts | 3 +++ src/mad_hatter/mad-hatter.ts | 18 +++++++++--------- src/routes/plugins.ts | 8 ++++---- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/factory/embedder.ts b/src/factory/embedder.ts index d1bdb93..f6cd05a 100644 --- a/src/factory/embedder.ts +++ b/src/factory/embedder.ts @@ -18,7 +18,7 @@ export interface EmbedderSettings { description: string link?: string config: z.ZodEffects | z.AnyZodObject - getModel: (params: z.input) => Embeddings + getModel: (params: z.input) => Embeddings } const fakeEmbedderConfig: Readonly = Object.freeze({ diff --git a/src/factory/llm.ts b/src/factory/llm.ts index 3a8e27f..812da1e 100644 --- a/src/factory/llm.ts +++ b/src/factory/llm.ts @@ -18,7 +18,7 @@ export interface LLMSettings { description: string link?: string config: z.ZodEffects | z.AnyZodObject - getModel: (params: z.input) => BaseLanguageModel + getModel: (params: z.input) => BaseLanguageModel } const defaultLLMConfig: Readonly = Object.freeze({ diff --git a/src/looking_glass/cheshire-cat.ts b/src/looking_glass/cheshire-cat.ts index 4598fa3..b81d378 100644 --- a/src/looking_glass/cheshire-cat.ts +++ b/src/looking_glass/cheshire-cat.ts @@ -97,6 +97,11 @@ export class CheshireCat { return this.getStray(userId)! } + /** + * Removes a stray instance for the specified user from the collection. + * @param userId The ID of the user to remove. + * @returns True if the user was successfully removed, false otherwise. + */ removeStray(userId: string) { return this.strays.delete(userId) } @@ -124,13 +129,12 @@ export class CheshireCat { * @returns The found Embedder settings from db or the default LLM settings */ loadLanguageEmbedder() { - const selected = db.data.selectedEmbedder, embSettings = db.getEmbedderSettings(), llmSettings = db.getLLMSettings() + const selected = db.data.selectedEmbedder, settings = db.getEmbedderSettings() try { - if (!llmSettings) throw new Error('LLM settings not found') const embedder = getEmbedder(selected) if (!embedder) throw new Error('Embedder not found') - if (!embSettings && embedder.name !== 'FakeEmbedder') throw new Error('Embedder settings not found') - return embedder.getModel(embSettings ?? {}) + if (!settings) throw new Error('Embedder settings not found') + return embedder.getModel(settings) } catch (error) { log.error(`The selected Embedder "${selected}" does not exist. Falling back to the default Embedder.`) @@ -148,11 +152,10 @@ export class CheshireCat { log.error('Embedder size is 0') throw new Error('Embedder size is 0. Unable to proceed.') } - const vectorMemoryConfig = { + this.memory = await getVectorMemory({ embedderName: db.data.selectedEmbedder, embedderSize: this.embedderSize, - } - this.memory = await getVectorMemory(vectorMemoryConfig) + }) } private buildEmbeddedProceduresHashes(procedures: PointData[]) { @@ -160,8 +163,8 @@ export class CheshireCat { for (const proc of procedures) { const metadata = proc.payload?.metadata as Record const pageContent = (proc.payload?.pageContent as string).toLowerCase().replace(/\s/g, '_') - const hasDescription = metadata.trigger === 'description' ? '' : `.${pageContent ?? 'empty'}` - const hash = `${metadata.source ?? 'unknown'}.${metadata.trigger ?? 'unsupported'}${hasDescription}` + const description = metadata.trigger === 'description' ? '' : `.${pageContent ?? 'empty'}` + const hash = `${metadata.source ?? 'unknown'}.${metadata.trigger ?? 'unsupported'}${description}` hashes[hash] = proc.id.toString() } return hashes diff --git a/src/looking_glass/stray-cat.ts b/src/looking_glass/stray-cat.ts index 492a287..cbf7b3a 100644 --- a/src/looking_glass/stray-cat.ts +++ b/src/looking_glass/stray-cat.ts @@ -22,6 +22,9 @@ export type WS = ElysiaWS< any > +/** + * The stray cat goes around tools and hook, making troubles + */ export class StrayCat { private chatHistory: MemoryMessage[] = [] private userMessage!: Message diff --git a/src/mad_hatter/mad-hatter.ts b/src/mad_hatter/mad-hatter.ts index 68ea881..47ff5cf 100644 --- a/src/mad_hatter/mad-hatter.ts +++ b/src/mad_hatter/mad-hatter.ts @@ -15,7 +15,7 @@ export class MadHatter { private static instance: MadHatter private plugins = new Map() private activePlugins: Plugin['id'][] = [] - onPluginsSyncCallback?: () => void = undefined + onPluginsSyncCallback?: () => Promise = undefined hooks: Partial = {} tools: Tool[] = [] forms: Form[] = [] @@ -65,7 +65,7 @@ export class MadHatter { * Gets a copy of the installed plugins. */ get installedPlugins() { - return Array.from([...this.plugins.values()]) + return [...this.plugins.values()] } /** @@ -92,7 +92,7 @@ export class MadHatter { } } log.success('Active plugins:', this.activePlugins.join(', ')) - this.syncHooksAndProcedures() + await this.syncHooksAndProcedures() if (Object.keys(this.hooks).length > 0) { log.success('Added hooks:') log.table(Object.entries(this.hooks).map(([key, value]) => @@ -149,7 +149,7 @@ export class MadHatter { this.activePlugins = this.activePlugins.filter(p => p !== id) db.update(db => db.activePlugins = this.activePlugins) this.plugins.delete(id) - this.syncHooksAndProcedures() + await this.syncHooksAndProcedures() } } @@ -162,7 +162,7 @@ export class MadHatter { if (plugin && !plugin.reloading) { log.info(`Reloading plugin: ${plugin.id}`) await plugin.reload() - this.syncHooksAndProcedures() + await this.syncHooksAndProcedures() } } @@ -171,7 +171,7 @@ export class MadHatter { * @param id The ID of the plugin to toggle. * @param sync Whether to synchronize hooks and tools immediately. Default is true. */ - togglePlugin(id: string, sync = true) { + async togglePlugin(id: string, sync = true) { const plugin = this.plugins.get(id) if (plugin) { const active = this.activePlugins.includes(id) @@ -184,7 +184,7 @@ export class MadHatter { this.activePlugins.push(plugin.id) } db.update(db => db.activePlugins = this.activePlugins) - if (sync) this.syncHooksAndProcedures() + if (sync) await this.syncHooksAndProcedures() return plugin.active } return false @@ -194,7 +194,7 @@ export class MadHatter { * Synchronizes hooks, tools and forms. * It also sorts the hooks by priority. */ - syncHooksAndProcedures() { + async syncHooksAndProcedures() { log.silent('Synchronizing hooks, tools and forms...') this.tools = [] this.forms = [] @@ -214,7 +214,7 @@ export class MadHatter { Object.entries(this.hooks).forEach(([name, hooks]) => { this.hooks[name as HookNames] = hooks.sort((a, b) => b.priority - a.priority) }) - this.onPluginsSyncCallback?.() + await this.onPluginsSyncCallback?.() } } diff --git a/src/routes/plugins.ts b/src/routes/plugins.ts index 1911586..3802ae7 100644 --- a/src/routes/plugins.ts +++ b/src/routes/plugins.ts @@ -87,7 +87,7 @@ export function plugins(app: App) { } try { const plugin = await mh.installPlugin(tempFilePath) - mh.togglePlugin(plugin.id) + await mh.togglePlugin(plugin.id) } catch (error) { log.warn(error) @@ -104,7 +104,7 @@ export function plugins(app: App) { body: t.Object({ file: t.File(), }), - querystring: t.Object({ + query: t.Object({ async: t.BooleanString({ default: true }), }), response: { @@ -127,7 +127,7 @@ export function plugins(app: App) { body: t.Object({ url: t.String({ format: 'uri' }), }), - querystring: t.Object({ + query: t.Object({ async: t.BooleanString({ default: true }), }), response: { @@ -140,7 +140,7 @@ export function plugins(app: App) { const p = mh.getPlugin(id) if (!p) throw new NotFoundError('Plugin not found') const { active } = body - if (active) mh.togglePlugin(id) + if (active) await mh.togglePlugin(id) return { active: p.active, }