Skip to content

Commit

Permalink
chore: Bun-related updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Jul 1, 2024
1 parent 4a31091 commit 8b04e99
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 24 deletions.
41 changes: 41 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "bun",
"request": "launch",
"name": "Debug Bun",
// The path to a JavaScript or TypeScript file to run.
"program": "${file}",
// The arguments to pass to the program, if any.
"args": [],
// The working directory of the program.
"cwd": "${workspaceFolder}",
// The environment variables to pass to the program.
"env": {},
// If the environment variables should not be inherited from the parent process.
"strictEnv": false,
// If the program should be run in watch mode.
// This is equivalent to passing `--watch` to the `bun` executable.
// You can also set this to "hot" to enable hot reloading using `--hot`.
"watchMode": false,
// If the debugger should stop on the first line of the program.
"stopOnEntry": false,
// If the debugger should be disabled. (for example, breakpoints will not be hit)
"noDebug": false,
// The path to the `bun` executable, defaults to your `PATH` environment variable.
"runtime": "bun",
// The arguments to pass to the `bun` executable, if any.
// Unlike `args`, these are passed to the executable itself, not the program.
"runtimeArgs": []
},
{
"type": "bun",
"request": "attach",
"name": "Attach to Bun",
// The URL of the WebSocket inspector to attach to.
// This value can be retrieved by using `bun --inspect`.
"url": "ws://localhost:6499/"
}
]
}
Binary file modified bun.lockb
Binary file not shown.
19 changes: 19 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
logLevel = "warn"
telemetry = true

[test]
root = "./test"

[install]
dev = true
peer = true
exact = true
auto = "auto"

[install.lockfile]
save = true

[run]
shell = "bun"
bun = true
silent = true
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
"@elysiajs/static": "^1.0.3",
"@elysiajs/stream": "^1.0.3",
"@elysiajs/swagger": "^1.0.5",
"@langchain/anthropic": "^0.2.2",
"@langchain/anthropic": "^0.2.3",
"@langchain/azure-openai": "^0.0.11",
"@langchain/cohere": "^0.1.0",
"@langchain/community": "^0.2.15",
"@langchain/community": "^0.2.16",
"@langchain/core": "^0.2.11",
"@langchain/google-genai": "^0.0.21",
"@langchain/mistralai": "^0.0.24",
"@langchain/mistralai": "^0.0.25",
"@langchain/openai": "^0.2.1",
"@mgcrea/pino-pretty-compact": "^1.3.0",
"@qdrant/js-client-rest": "^1.9.0",
"callsites": "^4.1.0",
"callsites": "^4.2.0",
"cheerio": "1.0.0-rc.12",
"chokidar": "^3.6.0",
"consola": "^3.2.3",
Expand All @@ -70,7 +70,7 @@
"defu": "^6.1.4",
"destr": "^2.0.3",
"dotenv": "^16.4.5",
"elysia": "^1.0.25",
"elysia": "^1.0.26",
"fastembed": "^1.14.1",
"get-port-please": "^3.1.2",
"html-to-text": "^9.0.5",
Expand Down
5 changes: 2 additions & 3 deletions src/mad_hatter/mad-hatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { mkdir, readdir, rm } from 'node:fs/promises'
import { basename, join, sep } from 'node:path'
import { execSync } from 'node:child_process'
import chokidar from 'chokidar'
import { catPaths, existsDir } from '@utils'
import { db } from '@db'
Expand Down Expand Up @@ -139,8 +138,8 @@ export class MadHatter {
const requirementsPath = join(plugin.path, 'requirements.txt')
if (existsDir(requirementsPath)) {
const requirements = await Bun.file(requirementsPath).text()
const pkgs = requirements.split('\n').map(req => req.trim().split('=')[0]).join(' ')
try { execSync(`pnpm remove ${pkgs}`, { cwd: plugin.path }) }
const pkgs = requirements.split('\n').map(req => req.trim().split('=')[0]).filter(p => p !== undefined)
try { Bun.spawnSync(['bun', 'remove', ...pkgs]) }
catch (error) { log.error(`Error removing requirements for ${plugin.id}: ${error}`) }
}
await rm(plugin.path, { recursive: true, force: true })
Expand Down
12 changes: 4 additions & 8 deletions src/mad_hatter/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { basename, dirname, extname, join, relative } from 'node:path'
import type { Dirent } from 'node:fs'
import { existsSync, statSync } from 'node:fs'
import { unlink } from 'node:fs/promises'
import { execSync } from 'node:child_process'
import { defu } from 'defu'
import { z } from 'zod'
import { destr } from 'destr'
Expand All @@ -12,6 +11,7 @@ import { log } from '@logger'
import { type Hook, isHook } from './hook.ts'
import { type Tool, isTool } from './tool.ts'
import { type Form, isForm } from './form.ts'
import pkg from '~/package.json'

const pluginManifestSchema = z.object({
name: z.string().min(1).trim(),
Expand Down Expand Up @@ -216,13 +216,9 @@ export class Plugin<
const requirementsPath = join(this.path, 'requirements.txt')
if (existsSync(requirementsPath)) {
const requirements = await Bun.file(requirementsPath).text()
const names = requirements.split('\n').map(req => req.trim().split('@')[0]!)
try {
execSync(`npm list ${names.join(' ')}`, { cwd: this.path }).toString()
}
catch (error) {
const pkgs = requirements.split('\n').join(' ')
try { execSync(`pnpm i ${pkgs}`, { cwd: this.path }) }
const pkgs = requirements.split('\n')
if (!Object.keys(pkg.dependencies).every(dep => pkgs.includes(dep))) {
try { Bun.spawnSync(['bun', 'i', ...pkgs]) }
catch (error) { log.error(`Error installing requirements for ${this.id}: ${error}`) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { serverTiming } from '@elysiajs/server-timing'
import { cors } from '@elysiajs/cors'
import { staticPlugin } from '@elysiajs/static'
import { madHatter } from '@mh/mad-hatter.ts'
import pkg from '../package.json' assert { type: 'json' }
import { db } from './database.ts'
import { logWelcome, parsedEnv } from './utils.ts'
import { apiModels, swaggerTags } from './context.ts'
import { log } from './logger.ts'
import { rabbitHole } from './rabbit-hole.ts'
import pkg from '~/package.json'

const app = new Elysia()
.trace(async ({ handle, context }) => {
Expand Down
10 changes: 5 additions & 5 deletions src/memory/vector-memory-collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'node:path'
import { lstatSync, mkdirSync, renameSync, writeFileSync } from 'node:fs'
import { lstat, mkdir, rename } from 'node:fs/promises'
import { randomUUID } from 'uncrypto'
import { ofetch } from 'ofetch'
import { parsedEnv } from '@utils'
Expand Down Expand Up @@ -90,11 +90,11 @@ export class VectorMemoryCollection {
return
}
log.warn(`Saving "${this.name}" collection dump...`)
const stats = lstatSync(folder)
const stats = await lstat(folder)
if (stats.isDirectory()) log.info('Directory dormouse exists')
else {
log.warn('Creating dormouse directory...')
mkdirSync(folder)
await mkdir(folder)
}
const snapshot = await vectorDb.createSnapshot(this.name)
if (!snapshot) {
Expand All @@ -106,9 +106,9 @@ export class VectorMemoryCollection {
const collectionAlias = await vectorDb.getCollectionAliases(this.name)
const aliasName = collectionAlias.aliases[0]?.alias_name ?? `${this.embedderName}_${this.name}`
const res = await ofetch<string>(remoteSnap)
writeFileSync(dumpDir, res)
await Bun.write(dumpDir, res)
const newName = join(folder, aliasName.replaceAll('/', '_').concat('.snapshot'))
renameSync(dumpDir, newName)
await rename(dumpDir, newName)
const snapshots = await vectorDb.listSnapshots(this.name)
snapshots.forEach(async snap => vectorDb.deleteSnapshot(this.name, snap.name))
log.info(`Dump ${newName} saved successfully`)
Expand Down
3 changes: 2 additions & 1 deletion src/rabbit-hole.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { basename, extname, resolve } from 'node:path'
import { File } from 'node:buffer'
import { PDFLoader } from '@langchain/community/document_loaders/fs/pdf'
import { CSVLoader } from '@langchain/community/document_loaders/fs/csv'
import { DocxLoader } from '@langchain/community/document_loaders/fs/docx'
Expand Down Expand Up @@ -142,7 +143,7 @@ export class RabbitHole {
throw new Error(`The file type "${file.type}" is not supported. Skipping ingestion...`)

log.info('Ingesting file...')
const loader = new this.fileHandlers[mime]!(file)
const loader = new this.fileHandlers[mime]!(file as unknown as Blob)
stray.send({ type: 'notification', content: 'Parsing the content. Big content could require some minutes...' })
const content = await loader.load()
stray.send({ type: 'notification', content: 'Parsing completed. Starting now the reading process...' })
Expand Down
2 changes: 1 addition & 1 deletion test/api/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'bun:test'
import { treaty } from '@elysiajs/eden'
import { parsedEnv } from '@utils'
import pkg from '../../package.json' assert { type: 'json' }
import pkg from '~/package.json'
import app from '@/main.ts'

const api = treaty(app)
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"module": "ESNext",
"moduleResolution": "bundler",
"paths": {
"~/*": ["./*"],
"@/*": ["./src/*"],
"@dto/*": ["./src/dtos/*"],
"@factory/*": ["./src/factory/*"],
Expand Down

0 comments on commit 8b04e99

Please sign in to comment.