**feat: enhance WebStreams API compatibility and refactor BrainyData integration**

### Changes:
- Introduced environment-independent WebStreams API compatibility:
  - Added `initializeStreamClasses` for dynamic detection of WebStreams API support in browsers and Node.js.
  - Implemented fallback mechanism with detailed error messages for unsupported environments.
  - Ensured asynchronous initialization of stream classes without blocking module execution.
- Updated `SequentialPipeline`:
  - Added `ensureStreamClassesInitialized` method to streamline WebStreams setup across operations.
  - Enhanced `initialize` to parallelize stream classes and `BrainyData` initialization.
  - Refactored WebSocket handlers (`createWebSocketHandler` and `createWebSocketStreams`) to support async stream initialization.
  - Improved TypeScript typings for streams (`TransformStreamDefaultController`, `PipelineResult<unknown>`).
- Adjusted `ServerSearchConduitAugmentation`:
  - Introduced dependency on `BrainyDataInterface` instead of direct `BrainyData` usage.
  - Ensured `localDb` is set prior to initialization with an appropriate error message.
  - Updated methods (`setLocalDb`, `getLocalDb`, and vector operations) to rely on `BrainyDataInterface`.
- Enhanced error handling and logging throughout streaming and augmentation processes.

### Purpose:
Improved cross-environment compatibility of WebStreams API, ensuring seamless usage in both browsers and Node.js. Refactored `ServerSearchConduitAugmentation` to decouple `BrainyData` dependency,
This commit is contained in:
David Snelling 2025-06-19 15:03:17 -07:00
parent 5f0d9f4b0a
commit 25f7ab73b4
2 changed files with 69 additions and 20 deletions

View file

@ -15,7 +15,7 @@ import {
} from '../types/augmentations.js'
import { WebSocketConduitAugmentation } from './conduitAugmentations.js'
import { v4 as uuidv4 } from 'uuid'
import { BrainyData } from '../brainyData.js'
import { BrainyDataInterface } from '../types/brainyDataInterface.js'
/**
* ServerSearchConduitAugmentation
@ -24,7 +24,7 @@ import { BrainyData } from '../brainyData.js'
* a server-hosted Brainy instance and storing results locally.
*/
export class ServerSearchConduitAugmentation extends WebSocketConduitAugmentation {
private localDb: BrainyData | null = null
private localDb: BrainyDataInterface | null = null
constructor(name: string = 'server-search-conduit') {
super(name)
@ -43,10 +43,9 @@ export class ServerSearchConduitAugmentation extends WebSocketConduitAugmentatio
// Initialize the base conduit
await super.initialize()
// Initialize local Brainy instance if not provided
// Local DB must be set before initialization
if (!this.localDb) {
this.localDb = new BrainyData()
await this.localDb.init()
throw new Error('Local database not set. Call setLocalDb before initializing.')
}
this.isInitialized = true
@ -60,7 +59,7 @@ export class ServerSearchConduitAugmentation extends WebSocketConduitAugmentatio
* Set the local Brainy instance
* @param db The Brainy instance to use for local storage
*/
setLocalDb(db: BrainyData): void {
setLocalDb(db: BrainyDataInterface): void {
this.localDb = db
}
@ -68,7 +67,7 @@ export class ServerSearchConduitAugmentation extends WebSocketConduitAugmentatio
* Get the local Brainy instance
* @returns The local Brainy instance
*/
getLocalDb(): BrainyData | null {
getLocalDb(): BrainyDataInterface | null {
return this.localDb
}
@ -265,7 +264,7 @@ export class ServerSearchConduitAugmentation extends WebSocketConduitAugmentatio
const id = await this.localDb.add(data, metadata)
// Get the vector and metadata
const noun = await this.localDb.get(id)
const noun = await this.localDb.get(id) as import('../coreTypes.js').VectorDocument<unknown>
if (!noun) {
return {
@ -620,7 +619,7 @@ export async function createServerSearchAugmentations(
conduitName?: string,
activationName?: string,
protocols?: string | string[],
localDb?: BrainyData
localDb?: BrainyDataInterface
} = {}
): Promise<{
conduit: ServerSearchConduitAugmentation,