Skip to content

Commit

Permalink
dynamically import process and fs
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Mar 22, 2024
1 parent b663083 commit 71bf654
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bgent",
"version": "0.1.9",
"version": "0.1.10",
"private": false,
"description": "bgent. because agent was taken.",
"type": "module",
Expand Down
35 changes: 19 additions & 16 deletions src/lib/adapters/sqlite/sqlite_vss.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { statSync } from "fs";
import { join } from "path";
import { arch, platform } from "process";

// TypeScript definitions
export interface Database {
loadExtension(file: string, entrypoint?: string | undefined): void;
Expand All @@ -28,7 +24,9 @@ function platformPackageName(platform: string, arch: string): string {
return `sqlite-vss-${os}-${arch}`;
}

function loadablePathResolver(name: string): string {
async function loadablePathResolver(name: string): Promise<string> {
const { platform, arch } = await import("process");

if (!validPlatform(platform, arch)) {
throw new Error(
`Unsupported platform for sqlite-vss, on a ${platform}-${arch} machine, but not in supported platforms (${supportedPlatforms
Expand All @@ -37,6 +35,9 @@ function loadablePathResolver(name: string): string {
);
}

const { join } = await import("path");
const { statSync } = await import("fs");

const packageName = platformPackageName(platform, arch);
let loadablePath = join(
__dirname,
Expand All @@ -50,7 +51,7 @@ function loadablePathResolver(name: string): string {
`${name}.${extensionSuffix(platform)}`,
);

// if loadable path doesnt exist, check path2
// if loadable path doesn't exist, check path2
if (!statSync(loadablePath, { throwIfNoEntry: false })) {
loadablePath = join(
__dirname,
Expand All @@ -72,23 +73,25 @@ function loadablePathResolver(name: string): string {
return loadablePath;
}

export function getVectorLoadablePath(): string {
export async function getVectorLoadablePath(): Promise<string> {
return loadablePathResolver("vector0");
}

export function getVssLoadablePath(): string {
export async function getVssLoadablePath(): Promise<string> {
return loadablePathResolver("vss0");
}

export function loadVector(db: Database): void {
db.loadExtension(getVectorLoadablePath());
export async function loadVector(db: Database): Promise<void> {
const loadablePath = await getVectorLoadablePath();
db.loadExtension(loadablePath);
}

export function loadVss(db: Database): void {
db.loadExtension(getVssLoadablePath());
export async function loadVss(db: Database): Promise<void> {
const loadablePath = await getVssLoadablePath();
db.loadExtension(loadablePath);
}

export function load(db: Database): void {
loadVector(db);
loadVss(db);
}
export async function load(db: Database): Promise<void> {
await loadVector(db);
await loadVss(db);
}
2 changes: 1 addition & 1 deletion src/test/createRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function createRuntime({
adapter = new SqliteDatabaseAdapter(new Database(":memory:"));

// Load sqlite-vss
load((adapter as SqliteDatabaseAdapter).db);
await load((adapter as SqliteDatabaseAdapter).db);
// Create a test user and session
user = {
id: zeroUuid,
Expand Down

0 comments on commit 71bf654

Please sign in to comment.