Skip to content

Commit

Permalink
move out packages into seperate repos
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-schabel committed Jan 11, 2025
1 parent dfb4b23 commit 474deb1
Show file tree
Hide file tree
Showing 85 changed files with 479 additions and 5,415 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Brandon Schabel
Copyright (c) 2025 Brandon Schabel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Binary file modified bun.lockb
Binary file not shown.
192 changes: 0 additions & 192 deletions instructions.md

This file was deleted.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "octo-prompt",
"name": "octoprompt",
"module": "index.ts",
"type": "module",
"workspaces": [
"packages/*",
"packages/websocket-react-example/*"
"packages/*"
],
"devDependencies": {
"@types/bun": "latest"
Expand All @@ -13,7 +12,6 @@
"typescript": "^5.7.2"
},
"dependencies": {
"@bnk/router": "1.0.5",
"@bnk/websocket-manager-react": "workspace:*"
"@bnk/router": "1.0.5"
}
}
4 changes: 2 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"drizzle-orm": "^0.36.3"
},
"dependencies": {
"@bnk/ai": "^1.0.0",
"@bnk/router": "1.0.6",
"@types/archiver": "^6.0.3",
"archiver": "^7.0.1",
"better-sqlite3": "^11.7.0",
"openai": "^4.73.1",
"shared": "workspace:*",
"zod": "^3.23.8",
"@bnk/ai": "workspace:*"
"zod": "^3.23.8"
}
}
102 changes: 102 additions & 0 deletions packages/server/src/websocket/websocket-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// websocket-config.ts
import { globalStateSchema, createInitialGlobalState, type GlobalState } from "shared";
import { db } from "shared/database";
import { globalStateTable, eq } from "shared";
import { ZodError } from "zod";

// --------------------------------------------------
// 1. In-Memory Cache
// --------------------------------------------------
// This holds your state in memory for fast reads/writes.
let inMemoryGlobalState: GlobalState | null = null;

// --------------------------------------------------
// 2. Fetch from Database
// --------------------------------------------------
async function loadFromDb(): Promise<GlobalState> {
const row = await db
.select()
.from(globalStateTable)
.where(eq(globalStateTable.id, "main"))
.get();

if (!row) {
// No stored state in DB; create a fresh one and persist
const initialState = createInitialGlobalState();
await db
.insert(globalStateTable)
.values({ id: "main", state_json: JSON.stringify(initialState) })
.run();
return initialState;
}

// Validate or fall back if the schema fails
try {
const parsed = JSON.parse(row.state_json);
return globalStateSchema.parse(parsed);
} catch (err) {
if (err instanceof ZodError) {
// If DB state is invalid, reset to a known good state
const fallback = createInitialGlobalState();
await saveToDb(fallback);
return fallback;
}
throw err;
}
}

// --------------------------------------------------
// 3. Save to Database
// --------------------------------------------------
async function saveToDb(newState: GlobalState): Promise<void> {
const exists = await db
.select()
.from(globalStateTable)
.where(eq(globalStateTable.id, "main"))
.get();

if (!exists) {
await db
.insert(globalStateTable)
.values({ id: "main", state_json: JSON.stringify(newState) })
.run();
} else {
await db
.update(globalStateTable)
.set({ state_json: JSON.stringify(newState) })
.where(eq(globalStateTable.id, "main"))
.run();
}
}

// --------------------------------------------------
// 4. getState / setState for BNK manager
// --------------------------------------------------
export async function getState(): Promise<GlobalState> {
// If our in-memory copy is null, load from DB once
if (!inMemoryGlobalState) {
inMemoryGlobalState = await loadFromDb();
}
// Return a clone (structuredClone or deep copy) to avoid accidental mutations
return structuredClone(inMemoryGlobalState);
}

export async function setState(newState: GlobalState): Promise<void> {
// Update in-memory copy
inMemoryGlobalState = structuredClone(newState);

// Optionally, write to DB immediately.
// If you'd rather do periodic saves, comment this out or queue it in an interval.
await saveToDb(inMemoryGlobalState);
}

// --------------------------------------------------
// 5. Optional: Periodic DB Save Interval
// --------------------------------------------------
const SAVE_INTERVAL_MS = 10_000; // e.g. 10 seconds
setInterval(async () => {
if (inMemoryGlobalState) {
await saveToDb(inMemoryGlobalState);
// console.log("[PeriodicSync] State saved to DB");
}
}, SAVE_INTERVAL_MS);
21 changes: 0 additions & 21 deletions packages/streaming-engine/LICENSE

This file was deleted.

Loading

0 comments on commit 474deb1

Please sign in to comment.