Skip to content

Commit

Permalink
chore: small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Jul 9, 2024
1 parent 760da42 commit 20705ea
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/factory/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface EmbedderSettings {
description: string
link?: string
config: z.ZodEffects<z.AnyZodObject> | z.AnyZodObject
getModel: (params: z.input<EmbedderSettings['config']>) => Embeddings
getModel: (params: z.input<this['config']>) => Embeddings
}

const fakeEmbedderConfig: Readonly<EmbedderSettings> = Object.freeze({
Expand Down
2 changes: 1 addition & 1 deletion src/factory/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface LLMSettings {
description: string
link?: string
config: z.ZodEffects<z.AnyZodObject> | z.AnyZodObject
getModel: (params: z.input<LLMSettings['config']>) => BaseLanguageModel
getModel: (params: z.input<this['config']>) => BaseLanguageModel
}

const defaultLLMConfig: Readonly<LLMSettings> = Object.freeze({
Expand Down
21 changes: 12 additions & 9 deletions src/looking_glass/cheshire-cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.`)
Expand All @@ -148,20 +152,19 @@ 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[]) {
const hashes: Record<string, string> = {}
for (const proc of procedures) {
const metadata = proc.payload?.metadata as Record<string, any>
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
Expand Down
3 changes: 3 additions & 0 deletions src/looking_glass/stray-cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/mad_hatter/mad-hatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class MadHatter {
private static instance: MadHatter
private plugins = new Map<string, Plugin>()
private activePlugins: Plugin['id'][] = []
onPluginsSyncCallback?: () => void = undefined
onPluginsSyncCallback?: () => Promise<void> = undefined
hooks: Partial<Hooks> = {}
tools: Tool[] = []
forms: Form[] = []
Expand Down Expand Up @@ -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()]
}

/**
Expand All @@ -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]) =>
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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()
}
}

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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 = []
Expand All @@ -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?.()
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/routes/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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,
}
Expand Down

0 comments on commit 20705ea

Please sign in to comment.