chore: remove example files and release configuration
Deleted augmentation pipeline examples and `.releaserc.json`, streamlining the repository by eliminating redundant example implementations and release automation files.
This commit is contained in:
parent
a22fd7de77
commit
5566ddfb09
14 changed files with 169 additions and 1474 deletions
|
|
@ -235,7 +235,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.ISenseAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.ISenseAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.ISenseAugmentation, M, R>(
|
||||
this.registry.sense,
|
||||
|
|
@ -260,7 +260,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.IConduitAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.IConduitAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.IConduitAugmentation, M, R>(
|
||||
this.registry.conduit,
|
||||
|
|
@ -285,7 +285,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.ICognitionAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.ICognitionAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.ICognitionAugmentation, M, R>(
|
||||
this.registry.cognition,
|
||||
|
|
@ -310,7 +310,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.IMemoryAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.IMemoryAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.IMemoryAugmentation, M, R>(
|
||||
this.registry.memory,
|
||||
|
|
@ -335,7 +335,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.IPerceptionAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.IPerceptionAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.IPerceptionAugmentation, M, R>(
|
||||
this.registry.perception,
|
||||
|
|
@ -360,7 +360,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.IDialogAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.IDialogAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.IDialogAugmentation, M, R>(
|
||||
this.registry.dialog,
|
||||
|
|
@ -385,7 +385,7 @@ export class AugmentationPipeline {
|
|||
method: M & (BrainyAugmentations.IActivationAugmentation[M] extends (...args: any[]) => any ? M : never),
|
||||
args: Parameters<Extract<BrainyAugmentations.IActivationAugmentation[M], (...args: any[]) => any>>,
|
||||
options: PipelineOptions = {}
|
||||
): Promise<AugmentationResponse<R>[]> {
|
||||
): Promise<Promise<{ success: boolean; data: R; error?: string }>[]> {
|
||||
const opts = { ...DEFAULT_PIPELINE_OPTIONS, ...options }
|
||||
return this.executeTypedPipeline<BrainyAugmentations.IActivationAugmentation, M, R>(
|
||||
this.registry.activation,
|
||||
|
|
@ -538,10 +538,10 @@ export class AugmentationPipeline {
|
|||
// Create a timeout promise if a timeout is specified
|
||||
const timeoutPromise = options.timeout
|
||||
? new Promise<{
|
||||
success: boolean
|
||||
data: R
|
||||
error?: string
|
||||
}>((_, reject) => {
|
||||
success: boolean
|
||||
data: R
|
||||
error?: string
|
||||
}>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error(`Timeout executing ${String(method)} on ${augmentation.name}`))
|
||||
}, options.timeout)
|
||||
|
|
|
|||
|
|
@ -535,6 +535,47 @@ export class BrainyData<T = any> {
|
|||
return this.index.size()
|
||||
}
|
||||
|
||||
/**
|
||||
* Embed text or data into a vector using the same embedding function used by this instance
|
||||
* This allows clients to use the same TensorFlow Universal Sentence Encoder throughout their application
|
||||
*
|
||||
* @param data Text or data to embed
|
||||
* @returns A promise that resolves to the embedded vector
|
||||
*/
|
||||
public async embed(data: string | string[]): Promise<Vector> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
try {
|
||||
return await this.embeddingFunction(data)
|
||||
} catch (error) {
|
||||
console.error('Failed to embed data:', error)
|
||||
throw new Error(`Failed to embed data: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for similar documents using a text query
|
||||
* This is a convenience method that embeds the query text and performs a search
|
||||
*
|
||||
* @param query Text query to search for
|
||||
* @param k Number of results to return
|
||||
* @returns Array of search results
|
||||
*/
|
||||
public async searchText(query: string, k: number = 10): Promise<SearchResult<T>[]> {
|
||||
await this.ensureInitialized()
|
||||
|
||||
try {
|
||||
// Embed the query text
|
||||
const queryVector = await this.embed(query)
|
||||
|
||||
// Search using the embedded vector
|
||||
return await this.search(queryVector, k)
|
||||
} catch (error) {
|
||||
console.error('Failed to search with text query:', error)
|
||||
throw new Error(`Failed to search with text query: ${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the database is initialized
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,266 +0,0 @@
|
|||
/**
|
||||
* Augmentation Pipeline Example
|
||||
*
|
||||
* This example demonstrates how to use the augmentation pipeline to register
|
||||
* and execute multiple augmentations of each type.
|
||||
*/
|
||||
|
||||
import {
|
||||
augmentationPipeline,
|
||||
ExecutionMode,
|
||||
PipelineOptions,
|
||||
IAugmentation,
|
||||
AugmentationResponse,
|
||||
BrainyAugmentations
|
||||
} from '../index.js'
|
||||
|
||||
/**
|
||||
* Example Cognition Augmentation
|
||||
*/
|
||||
class SimpleCognitionAugmentation implements BrainyAugmentations.ICognitionAugmentation {
|
||||
readonly name = 'simple-cognition'
|
||||
readonly description = 'A simple cognition augmentation for demonstration'
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing SimpleCognitionAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down SimpleCognitionAugmentation')
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
async reason(
|
||||
query: string,
|
||||
context?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<{ inference: string; confidence: number }>> {
|
||||
console.log(`SimpleCognitionAugmentation reasoning about: ${query}`)
|
||||
console.log('Context:', context)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
inference: `Simple inference about: ${query}`,
|
||||
confidence: 0.7
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async infer(
|
||||
dataSubset: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<Record<string, unknown>>> {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
result: `Inferred from data: ${JSON.stringify(dataSubset)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async executeLogic(
|
||||
ruleId: string,
|
||||
input: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Another Example Cognition Augmentation
|
||||
*/
|
||||
class AdvancedCognitionAugmentation implements BrainyAugmentations.ICognitionAugmentation {
|
||||
readonly name = 'advanced-cognition'
|
||||
readonly description = 'A more advanced cognition augmentation for demonstration'
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing AdvancedCognitionAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down AdvancedCognitionAugmentation')
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
async reason(
|
||||
query: string,
|
||||
context?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<{ inference: string; confidence: number }>> {
|
||||
console.log(`AdvancedCognitionAugmentation reasoning about: ${query}`)
|
||||
console.log('Context:', context)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
inference: `Advanced inference about: ${query} with detailed analysis`,
|
||||
confidence: 0.9
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async infer(
|
||||
dataSubset: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<Record<string, unknown>>> {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
result: `Advanced inference from data: ${JSON.stringify(dataSubset)}`,
|
||||
additionalInsights: ['insight1', 'insight2']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async executeLogic(
|
||||
ruleId: string,
|
||||
input: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example Sense Augmentation
|
||||
*/
|
||||
class SimpleSenseAugmentation implements BrainyAugmentations.ISenseAugmentation {
|
||||
readonly name = 'simple-sense'
|
||||
readonly description = 'A simple sense augmentation for demonstration'
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing SimpleSenseAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down SimpleSenseAugmentation')
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
async processRawData(
|
||||
rawData: Buffer | string,
|
||||
dataType: string
|
||||
): Promise<AugmentationResponse<{ nouns: string[]; verbs: string[] }>> {
|
||||
console.log(`SimpleSenseAugmentation processing ${dataType} data:`,
|
||||
typeof rawData === 'string' ? rawData : 'Buffer data')
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
nouns: ['example', 'data', 'processing'],
|
||||
verbs: ['process', 'analyze', 'extract']
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async listenToFeed(
|
||||
feedUrl: string,
|
||||
callback: (data: { nouns: string[]; verbs: string[] }) => void
|
||||
): Promise<void> {
|
||||
console.log(`SimpleSenseAugmentation listening to feed: ${feedUrl}`)
|
||||
// In a real implementation, this would set up a listener
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function to demonstrate the augmentation pipeline
|
||||
*/
|
||||
async function main() {
|
||||
try {
|
||||
console.log('=== Augmentation Pipeline Example ===')
|
||||
|
||||
// Create augmentation instances
|
||||
const simpleCognition = new SimpleCognitionAugmentation()
|
||||
const advancedCognition = new AdvancedCognitionAugmentation()
|
||||
const simpleSense = new SimpleSenseAugmentation()
|
||||
|
||||
// Register augmentations with the pipeline
|
||||
augmentationPipeline
|
||||
.register(simpleCognition)
|
||||
.register(advancedCognition)
|
||||
.register(simpleSense)
|
||||
|
||||
// Initialize all registered augmentations
|
||||
console.log('\n=== Initializing Augmentations ===')
|
||||
await augmentationPipeline.initialize()
|
||||
|
||||
// Execute a cognition pipeline in sequential mode (default)
|
||||
console.log('\n=== Executing Cognition Pipeline (Sequential) ===')
|
||||
const reasoningResults = await augmentationPipeline.executeCognitionPipeline(
|
||||
'reason',
|
||||
['What is the capital of France?', { additionalContext: 'geography' }]
|
||||
)
|
||||
|
||||
console.log('\nReasoning Results:')
|
||||
reasoningResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (result.success) {
|
||||
console.log(` Inference: ${result.data.inference}`)
|
||||
console.log(` Confidence: ${result.data.confidence}`)
|
||||
} else {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Execute a cognition pipeline in parallel mode
|
||||
console.log('\n=== Executing Cognition Pipeline (Parallel) ===')
|
||||
const inferResults = await augmentationPipeline.executeCognitionPipeline(
|
||||
'infer',
|
||||
[{ topic: 'climate change', data: [1, 2, 3] }],
|
||||
{ mode: ExecutionMode.PARALLEL }
|
||||
)
|
||||
|
||||
console.log('\nInference Results:')
|
||||
inferResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (result.success) {
|
||||
console.log(` Data: ${JSON.stringify(result.data)}`)
|
||||
} else {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Execute a sense pipeline
|
||||
console.log('\n=== Executing Sense Pipeline ===')
|
||||
const processingResults = await augmentationPipeline.executeSensePipeline(
|
||||
'processRawData',
|
||||
['This is some example text to process', 'text']
|
||||
)
|
||||
|
||||
console.log('\nProcessing Results:')
|
||||
processingResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (result.success) {
|
||||
console.log(` Nouns: ${result.data.nouns.join(', ')}`)
|
||||
console.log(` Verbs: ${result.data.verbs.join(', ')}`)
|
||||
} else {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Shut down all registered augmentations
|
||||
console.log('\n=== Shutting Down Augmentations ===')
|
||||
await augmentationPipeline.shutDown()
|
||||
|
||||
console.log('\n=== Example Complete ===')
|
||||
} catch (error) {
|
||||
console.error('Error in augmentation pipeline example:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// Run the example
|
||||
main()
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
/**
|
||||
* Example demonstrating how to use the FileSystemStorage adapter with a custom directory
|
||||
*/
|
||||
|
||||
import { BrainyData } from '../brainyData.js'
|
||||
import { FileSystemStorage } from '../storage/fileSystemStorage.js'
|
||||
import path from 'path'
|
||||
|
||||
// Example data - word embeddings
|
||||
const wordEmbeddings = {
|
||||
'cat': [0.2, 0.3, 0.4, 0.1],
|
||||
'dog': [0.3, 0.2, 0.4, 0.2],
|
||||
'fish': [0.1, 0.1, 0.8, 0.2]
|
||||
}
|
||||
|
||||
// Example metadata
|
||||
const metadata: {
|
||||
[key: string]: { type: string; [key]: boolean } | undefined | null
|
||||
} = {
|
||||
'cat': { type: 'mammal', domesticated: true },
|
||||
'dog': { type: 'mammal', domesticated: true },
|
||||
'fish': { type: 'fish', domesticated: false }
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the example
|
||||
*/
|
||||
async function runExample() {
|
||||
console.log('Initializing vector database with custom storage location...')
|
||||
|
||||
// Create a custom storage adapter with a specific directory
|
||||
// This will store data in ./custom-data directory relative to the current working directory
|
||||
const customStoragePath = path.join(process.cwd(), 'custom-data')
|
||||
const storageAdapter = new FileSystemStorage(customStoragePath)
|
||||
|
||||
// Create a new vector database with the custom storage adapter
|
||||
const db = new BrainyData({
|
||||
storageAdapter
|
||||
})
|
||||
|
||||
await db.init()
|
||||
|
||||
console.log(`Using custom storage location: ${customStoragePath}`)
|
||||
console.log('Adding vectors to the database...')
|
||||
|
||||
// Add vectors to the database
|
||||
const ids: Record<string, string> = {}
|
||||
for (const [word, vector] of Object.entries(wordEmbeddings)) {
|
||||
ids[word] = await db.add(vector, metadata[word])
|
||||
console.log(`Added "${word}" with ID: ${ids[word]}`)
|
||||
}
|
||||
|
||||
console.log('\nDatabase size:', db.size())
|
||||
|
||||
// Search for similar vectors
|
||||
console.log('\nSearching for vectors similar to "cat"...')
|
||||
const catResults = await db.search(wordEmbeddings['cat'], 2)
|
||||
console.log('Results:')
|
||||
for (const result of catResults) {
|
||||
const word = Object.entries(ids).find(([_, id]) => id === result.id)?.[0] || 'unknown'
|
||||
console.log(`- ${word} (score: ${result.score.toFixed(4)}, metadata:`, result.metadata, ')')
|
||||
}
|
||||
|
||||
console.log('\nExample completed successfully!')
|
||||
console.log(`Data has been stored in: ${customStoragePath}`)
|
||||
console.log('You can restart this example to verify that data persists between runs.')
|
||||
}
|
||||
|
||||
// Only run in Node.js environment
|
||||
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
|
||||
runExample().catch(error => {
|
||||
console.error('Error running example:', error)
|
||||
})
|
||||
} else {
|
||||
console.error('This example is designed to run in Node.js environments only.')
|
||||
}
|
||||
|
|
@ -1,755 +0,0 @@
|
|||
/**
|
||||
* Memory Augmentation Example
|
||||
*
|
||||
* This example demonstrates how to create and register memory augmentations
|
||||
* for storing data in different formats (e.g., fileSystem, in memory, or firestore).
|
||||
*/
|
||||
|
||||
import {
|
||||
augmentationPipeline,
|
||||
IAugmentation,
|
||||
IWebSocketSupport,
|
||||
IWebSocketMemoryAugmentation,
|
||||
AugmentationResponse,
|
||||
BrainyAugmentations,
|
||||
ExecutionMode
|
||||
} from '../index.js'
|
||||
|
||||
/**
|
||||
* Example In-Memory Augmentation
|
||||
*
|
||||
* This augmentation stores data in memory using a JavaScript Map.
|
||||
*/
|
||||
class InMemoryAugmentation implements BrainyAugmentations.IMemoryAugmentation {
|
||||
readonly name = 'in-memory-storage'
|
||||
readonly description = 'A simple in-memory storage augmentation'
|
||||
|
||||
private storage: Map<string, unknown> = new Map()
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing InMemoryAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down InMemoryAugmentation')
|
||||
this.storage.clear()
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
async storeData(
|
||||
key: string,
|
||||
data: unknown,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`Storing data with key: ${key}`)
|
||||
try {
|
||||
this.storage.set(key, data)
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async retrieveData(
|
||||
key: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<unknown>> {
|
||||
console.log(`Retrieving data with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
error: `Key not found: ${key}`
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: this.storage.get(key)
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateData(
|
||||
key: string,
|
||||
data: unknown,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`Updating data with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: `Key not found: ${key}`
|
||||
}
|
||||
}
|
||||
|
||||
this.storage.set(key, data)
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteData(
|
||||
key: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`Deleting data with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: `Key not found: ${key}`
|
||||
}
|
||||
}
|
||||
|
||||
this.storage.delete(key)
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async listDataKeys(
|
||||
pattern?: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<string[]>> {
|
||||
console.log(`Listing data keys with pattern: ${pattern || 'all'}`)
|
||||
try {
|
||||
const keys = Array.from(this.storage.keys())
|
||||
|
||||
// Filter keys by pattern if provided
|
||||
const filteredKeys = pattern
|
||||
? keys.filter(key => key.includes(pattern))
|
||||
: keys
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: filteredKeys
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: [],
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example File System Memory Augmentation
|
||||
*
|
||||
* This augmentation simulates storing data in the file system.
|
||||
*/
|
||||
class FileSystemMemoryAugmentation implements BrainyAugmentations.IMemoryAugmentation {
|
||||
readonly name = 'file-system-storage'
|
||||
readonly description = 'A file system storage augmentation'
|
||||
|
||||
private storage: Map<string, unknown> = new Map()
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing FileSystemMemoryAugmentation')
|
||||
console.log('Simulating file system initialization...')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down FileSystemMemoryAugmentation')
|
||||
console.log('Simulating file system cleanup...')
|
||||
this.storage.clear()
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
async storeData(
|
||||
key: string,
|
||||
data: unknown,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`Storing data in file system with key: ${key}`)
|
||||
try {
|
||||
// Simulate file system operations
|
||||
console.log(`Writing to file: ${key}.json`)
|
||||
this.storage.set(key, data)
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async retrieveData(
|
||||
key: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<unknown>> {
|
||||
console.log(`Retrieving data from file system with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
error: `File not found: ${key}.json`
|
||||
}
|
||||
}
|
||||
|
||||
// Simulate file system operations
|
||||
console.log(`Reading from file: ${key}.json`)
|
||||
return {
|
||||
success: true,
|
||||
data: this.storage.get(key)
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateData(
|
||||
key: string,
|
||||
data: unknown,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`Updating data in file system with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: `File not found: ${key}.json`
|
||||
}
|
||||
}
|
||||
|
||||
// Simulate file system operations
|
||||
console.log(`Updating file: ${key}.json`)
|
||||
this.storage.set(key, data)
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteData(
|
||||
key: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`Deleting data from file system with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: `File not found: ${key}.json`
|
||||
}
|
||||
}
|
||||
|
||||
// Simulate file system operations
|
||||
console.log(`Deleting file: ${key}.json`)
|
||||
this.storage.delete(key)
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async listDataKeys(
|
||||
pattern?: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<string[]>> {
|
||||
console.log(`Listing data keys from file system with pattern: ${pattern || 'all'}`)
|
||||
try {
|
||||
// Simulate file system operations
|
||||
console.log('Reading directory contents...')
|
||||
const keys = Array.from(this.storage.keys())
|
||||
|
||||
// Filter keys by pattern if provided
|
||||
const filteredKeys = pattern
|
||||
? keys.filter(key => key.includes(pattern))
|
||||
: keys
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: filteredKeys
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: [],
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example Combined WebSocket and Memory Augmentation
|
||||
*
|
||||
* This augmentation implements both WebSocket and Memory interfaces.
|
||||
*/
|
||||
class WebSocketMemoryAugmentation implements IWebSocketMemoryAugmentation {
|
||||
readonly name = 'websocket-memory-storage'
|
||||
readonly description = 'A combined WebSocket and Memory storage augmentation'
|
||||
|
||||
private storage: Map<string, unknown> = new Map()
|
||||
private connections: Map<string, { url: string, status: 'connected' | 'disconnected' | 'error' }> = new Map()
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing WebSocketMemoryAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down WebSocketMemoryAugmentation')
|
||||
this.storage.clear()
|
||||
this.connections.clear()
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
// WebSocket methods
|
||||
async connectWebSocket(url: string, protocols?: string | string[]): Promise<{
|
||||
connectionId: string;
|
||||
url: string;
|
||||
status: 'connected' | 'disconnected' | 'error'
|
||||
}> {
|
||||
console.log(`WebSocketMemory connecting to WebSocket at ${url}`)
|
||||
const connectionId = `ws-memory-${Date.now()}`
|
||||
this.connections.set(connectionId, { url, status: 'connected' })
|
||||
return {
|
||||
connectionId,
|
||||
url,
|
||||
status: 'connected'
|
||||
}
|
||||
}
|
||||
|
||||
async sendWebSocketMessage(connectionId: string, data: unknown): Promise<void> {
|
||||
console.log(`WebSocketMemory sending message to WebSocket ${connectionId}:`, data)
|
||||
if (!this.connections.has(connectionId)) {
|
||||
throw new Error(`Connection not found: ${connectionId}`)
|
||||
}
|
||||
}
|
||||
|
||||
async onWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void> {
|
||||
console.log(`WebSocketMemory registering callback for WebSocket ${connectionId}`)
|
||||
if (!this.connections.has(connectionId)) {
|
||||
throw new Error(`Connection not found: ${connectionId}`)
|
||||
}
|
||||
}
|
||||
|
||||
async closeWebSocket(connectionId: string, code?: number, reason?: string): Promise<void> {
|
||||
console.log(`WebSocketMemory closing WebSocket ${connectionId}`)
|
||||
if (!this.connections.has(connectionId)) {
|
||||
throw new Error(`Connection not found: ${connectionId}`)
|
||||
}
|
||||
this.connections.delete(connectionId)
|
||||
}
|
||||
|
||||
// Memory methods
|
||||
async storeData(
|
||||
key: string,
|
||||
data: unknown,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`WebSocketMemory storing data with key: ${key}`)
|
||||
try {
|
||||
this.storage.set(key, data)
|
||||
|
||||
// If a WebSocket connection is specified in options, send the data through it
|
||||
const connectionId = options?.connectionId as string
|
||||
if (connectionId && this.connections.has(connectionId)) {
|
||||
await this.sendWebSocketMessage(connectionId, {
|
||||
action: 'store',
|
||||
key,
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async retrieveData(
|
||||
key: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<unknown>> {
|
||||
console.log(`WebSocketMemory retrieving data with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
error: `Key not found: ${key}`
|
||||
}
|
||||
}
|
||||
|
||||
const data = this.storage.get(key)
|
||||
|
||||
// If a WebSocket connection is specified in options, send the request through it
|
||||
const connectionId = options?.connectionId as string
|
||||
if (connectionId && this.connections.has(connectionId)) {
|
||||
await this.sendWebSocketMessage(connectionId, {
|
||||
action: 'retrieve',
|
||||
key
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateData(
|
||||
key: string,
|
||||
data: unknown,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`WebSocketMemory updating data with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: `Key not found: ${key}`
|
||||
}
|
||||
}
|
||||
|
||||
this.storage.set(key, data)
|
||||
|
||||
// If a WebSocket connection is specified in options, send the update through it
|
||||
const connectionId = options?.connectionId as string
|
||||
if (connectionId && this.connections.has(connectionId)) {
|
||||
await this.sendWebSocketMessage(connectionId, {
|
||||
action: 'update',
|
||||
key,
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteData(
|
||||
key: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
console.log(`WebSocketMemory deleting data with key: ${key}`)
|
||||
try {
|
||||
if (!this.storage.has(key)) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: `Key not found: ${key}`
|
||||
}
|
||||
}
|
||||
|
||||
this.storage.delete(key)
|
||||
|
||||
// If a WebSocket connection is specified in options, send the delete request through it
|
||||
const connectionId = options?.connectionId as string
|
||||
if (connectionId && this.connections.has(connectionId)) {
|
||||
await this.sendWebSocketMessage(connectionId, {
|
||||
action: 'delete',
|
||||
key
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async listDataKeys(
|
||||
pattern?: string,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<string[]>> {
|
||||
console.log(`WebSocketMemory listing data keys with pattern: ${pattern || 'all'}`)
|
||||
try {
|
||||
const keys = Array.from(this.storage.keys())
|
||||
|
||||
// Filter keys by pattern if provided
|
||||
const filteredKeys = pattern
|
||||
? keys.filter(key => key.includes(pattern))
|
||||
: keys
|
||||
|
||||
// If a WebSocket connection is specified in options, send the list request through it
|
||||
const connectionId = options?.connectionId as string
|
||||
if (connectionId && this.connections.has(connectionId)) {
|
||||
await this.sendWebSocketMessage(connectionId, {
|
||||
action: 'list',
|
||||
pattern
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: filteredKeys
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
data: [],
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function to demonstrate the memory augmentation
|
||||
*/
|
||||
async function main() {
|
||||
try {
|
||||
console.log('=== Memory Augmentation Example ===')
|
||||
|
||||
// Create augmentation instances
|
||||
const inMemoryStorage = new InMemoryAugmentation()
|
||||
const fileSystemStorage = new FileSystemMemoryAugmentation()
|
||||
const webSocketMemory = new WebSocketMemoryAugmentation()
|
||||
|
||||
// Register augmentations with the pipeline
|
||||
augmentationPipeline
|
||||
.register(inMemoryStorage)
|
||||
.register(fileSystemStorage)
|
||||
.register(webSocketMemory)
|
||||
|
||||
// Initialize all registered augmentations
|
||||
console.log('\n=== Initializing Augmentations ===')
|
||||
await augmentationPipeline.initialize()
|
||||
|
||||
// Store data in all memory augmentations
|
||||
console.log('\n=== Storing Data ===')
|
||||
const storeResults = await augmentationPipeline.executeMemoryPipeline(
|
||||
'storeData',
|
||||
['user123', { name: 'John Doe', email: 'john@example.com' }]
|
||||
)
|
||||
|
||||
console.log('\nStore Results:')
|
||||
storeResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (!result.success) {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Retrieve data from all memory augmentations
|
||||
console.log('\n=== Retrieving Data ===')
|
||||
const retrieveResults = await augmentationPipeline.executeMemoryPipeline(
|
||||
'retrieveData',
|
||||
['user123']
|
||||
)
|
||||
|
||||
console.log('\nRetrieve Results:')
|
||||
retrieveResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (result.success) {
|
||||
console.log(` Data: ${JSON.stringify(result.data)}`)
|
||||
} else {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Update data in all memory augmentations
|
||||
console.log('\n=== Updating Data ===')
|
||||
const updateResults = await augmentationPipeline.executeMemoryPipeline(
|
||||
'updateData',
|
||||
['user123', { name: 'John Doe', email: 'john.updated@example.com' }]
|
||||
)
|
||||
|
||||
console.log('\nUpdate Results:')
|
||||
updateResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (!result.success) {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Retrieve updated data from all memory augmentations
|
||||
console.log('\n=== Retrieving Updated Data ===')
|
||||
const retrieveUpdatedResults = await augmentationPipeline.executeMemoryPipeline(
|
||||
'retrieveData',
|
||||
['user123']
|
||||
)
|
||||
|
||||
console.log('\nRetrieve Updated Results:')
|
||||
retrieveUpdatedResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (result.success) {
|
||||
console.log(` Data: ${JSON.stringify(result.data)}`)
|
||||
} else {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Store additional data
|
||||
console.log('\n=== Storing Additional Data ===')
|
||||
await augmentationPipeline.executeMemoryPipeline(
|
||||
'storeData',
|
||||
['user456', { name: 'Jane Smith', email: 'jane@example.com' }]
|
||||
)
|
||||
|
||||
// List all keys
|
||||
console.log('\n=== Listing All Keys ===')
|
||||
const listResults = await augmentationPipeline.executeMemoryPipeline(
|
||||
'listDataKeys',
|
||||
[]
|
||||
)
|
||||
|
||||
console.log('\nList Results:')
|
||||
listResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (result.success) {
|
||||
console.log(` Keys: ${result.data.join(', ')}`)
|
||||
} else {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Use the WebSocket-Memory augmentation directly
|
||||
console.log('\n=== Using WebSocket-Memory Augmentation ===')
|
||||
const connection = await webSocketMemory.connectWebSocket('wss://example.com/storage')
|
||||
console.log('Connection established:', connection)
|
||||
|
||||
// Store data through WebSocket
|
||||
console.log('\n=== Storing Data Through WebSocket ===')
|
||||
const wsStoreResult = await webSocketMemory.storeData(
|
||||
'wsUser123',
|
||||
{ name: 'WebSocket User', email: 'ws@example.com' },
|
||||
{ connectionId: connection.connectionId }
|
||||
)
|
||||
console.log('WebSocket Store Result:', wsStoreResult)
|
||||
|
||||
// Retrieve data through WebSocket
|
||||
console.log('\n=== Retrieving Data Through WebSocket ===')
|
||||
const wsRetrieveResult = await webSocketMemory.retrieveData(
|
||||
'wsUser123',
|
||||
{ connectionId: connection.connectionId }
|
||||
)
|
||||
console.log('WebSocket Retrieve Result:', wsRetrieveResult)
|
||||
|
||||
// Delete data from all memory augmentations
|
||||
console.log('\n=== Deleting Data ===')
|
||||
const deleteResults = await augmentationPipeline.executeMemoryPipeline(
|
||||
'deleteData',
|
||||
['user123']
|
||||
)
|
||||
|
||||
console.log('\nDelete Results:')
|
||||
deleteResults.forEach((result, index) => {
|
||||
console.log(`Result ${index + 1}:`)
|
||||
console.log(` Success: ${result.success}`)
|
||||
if (!result.success) {
|
||||
console.log(` Error: ${result.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
// Shut down all registered augmentations
|
||||
console.log('\n=== Shutting Down Augmentations ===')
|
||||
await augmentationPipeline.shutDown()
|
||||
|
||||
console.log('\n=== Example Complete ===')
|
||||
} catch (error) {
|
||||
console.error('Error in memory augmentation example:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// Run the example
|
||||
main()
|
||||
|
|
@ -1,210 +0,0 @@
|
|||
/**
|
||||
* WebSocket Augmentation Example
|
||||
*
|
||||
* This example demonstrates how to create and register a WebSocket-supporting augmentation.
|
||||
*/
|
||||
|
||||
import {
|
||||
augmentationPipeline,
|
||||
IAugmentation,
|
||||
IWebSocketSupport,
|
||||
IWebSocketCognitionAugmentation,
|
||||
AugmentationResponse,
|
||||
BrainyAugmentations
|
||||
} from '../index.js'
|
||||
|
||||
/**
|
||||
* Example WebSocket Augmentation
|
||||
*/
|
||||
class SimpleWebSocketAugmentation implements IWebSocketSupport {
|
||||
readonly name = 'simple-websocket'
|
||||
readonly description = 'A simple WebSocket augmentation for demonstration'
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing SimpleWebSocketAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down SimpleWebSocketAugmentation')
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
async connectWebSocket(url: string, protocols?: string | string[]): Promise<{
|
||||
connectionId: string;
|
||||
url: string;
|
||||
status: 'connected' | 'disconnected' | 'error'
|
||||
}> {
|
||||
console.log(`Connecting to WebSocket at ${url}`)
|
||||
return {
|
||||
connectionId: 'ws-1',
|
||||
url,
|
||||
status: 'connected'
|
||||
}
|
||||
}
|
||||
|
||||
async sendWebSocketMessage(connectionId: string, data: unknown): Promise<void> {
|
||||
console.log(`Sending message to WebSocket ${connectionId}:`, data)
|
||||
}
|
||||
|
||||
async onWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void> {
|
||||
console.log(`Registering callback for WebSocket ${connectionId}`)
|
||||
// In a real implementation, this would set up a listener
|
||||
}
|
||||
|
||||
async closeWebSocket(connectionId: string, code?: number, reason?: string): Promise<void> {
|
||||
console.log(`Closing WebSocket ${connectionId}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example Combined WebSocket and Cognition Augmentation
|
||||
*/
|
||||
class WebSocketCognitionAugmentation implements IWebSocketCognitionAugmentation {
|
||||
readonly name = 'websocket-cognition'
|
||||
readonly description = 'A combined WebSocket and Cognition augmentation for demonstration'
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
console.log('Initializing WebSocketCognitionAugmentation')
|
||||
}
|
||||
|
||||
async shutDown(): Promise<void> {
|
||||
console.log('Shutting down WebSocketCognitionAugmentation')
|
||||
}
|
||||
|
||||
async getStatus(): Promise<'active' | 'inactive' | 'error'> {
|
||||
return 'active'
|
||||
}
|
||||
|
||||
// WebSocket methods
|
||||
async connectWebSocket(url: string, protocols?: string | string[]): Promise<{
|
||||
connectionId: string;
|
||||
url: string;
|
||||
status: 'connected' | 'disconnected' | 'error'
|
||||
}> {
|
||||
console.log(`WebSocketCognition connecting to WebSocket at ${url}`)
|
||||
return {
|
||||
connectionId: 'ws-cognition-1',
|
||||
url,
|
||||
status: 'connected'
|
||||
}
|
||||
}
|
||||
|
||||
async sendWebSocketMessage(connectionId: string, data: unknown): Promise<void> {
|
||||
console.log(`WebSocketCognition sending message to WebSocket ${connectionId}:`, data)
|
||||
}
|
||||
|
||||
async onWebSocketMessage(connectionId: string, callback: (data: unknown) => void): Promise<void> {
|
||||
console.log(`WebSocketCognition registering callback for WebSocket ${connectionId}`)
|
||||
}
|
||||
|
||||
async closeWebSocket(connectionId: string, code?: number, reason?: string): Promise<void> {
|
||||
console.log(`WebSocketCognition closing WebSocket ${connectionId}`)
|
||||
}
|
||||
|
||||
// Cognition methods
|
||||
async reason(
|
||||
query: string,
|
||||
context?: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<{ inference: string; confidence: number }>> {
|
||||
console.log(`WebSocketCognition reasoning about: ${query}`)
|
||||
console.log('Context:', context)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
inference: `WebSocket-enabled inference about: ${query}`,
|
||||
confidence: 0.85
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async infer(
|
||||
dataSubset: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<Record<string, unknown>>> {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
result: `WebSocket-enabled inference from data: ${JSON.stringify(dataSubset)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async executeLogic(
|
||||
ruleId: string,
|
||||
input: Record<string, unknown>
|
||||
): Promise<AugmentationResponse<boolean>> {
|
||||
return {
|
||||
success: true,
|
||||
data: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function to demonstrate the WebSocket augmentation
|
||||
*/
|
||||
async function main() {
|
||||
try {
|
||||
console.log('=== WebSocket Augmentation Example ===')
|
||||
|
||||
// Create augmentation instances
|
||||
const simpleWebSocket = new SimpleWebSocketAugmentation()
|
||||
const webSocketCognition = new WebSocketCognitionAugmentation()
|
||||
|
||||
// Register augmentations with the pipeline
|
||||
augmentationPipeline
|
||||
.register(simpleWebSocket)
|
||||
.register(webSocketCognition)
|
||||
|
||||
// Initialize all registered augmentations
|
||||
console.log('\n=== Initializing Augmentations ===')
|
||||
await augmentationPipeline.initialize()
|
||||
|
||||
// Get all WebSocket-supporting augmentations
|
||||
console.log('\n=== WebSocket Augmentations ===')
|
||||
const webSocketAugmentations = augmentationPipeline.getWebSocketAugmentations()
|
||||
console.log(`Found ${webSocketAugmentations.length} WebSocket augmentations:`)
|
||||
webSocketAugmentations.forEach(aug => {
|
||||
console.log(`- ${aug.name}: ${aug.description}`)
|
||||
})
|
||||
|
||||
// Use the simple WebSocket augmentation
|
||||
console.log('\n=== Using Simple WebSocket Augmentation ===')
|
||||
const connection = await simpleWebSocket.connectWebSocket('wss://example.com')
|
||||
console.log('Connection established:', connection)
|
||||
await simpleWebSocket.sendWebSocketMessage(connection.connectionId, { message: 'Hello, WebSocket!' })
|
||||
|
||||
// Use the combined WebSocket and Cognition augmentation
|
||||
console.log('\n=== Using Combined WebSocket-Cognition Augmentation ===')
|
||||
const wsConnection = await webSocketCognition.connectWebSocket('wss://example.com/cognition')
|
||||
console.log('Connection established:', wsConnection)
|
||||
await webSocketCognition.sendWebSocketMessage(wsConnection.connectionId, { message: 'Hello from cognition!' })
|
||||
|
||||
// Execute a cognition method on the combined augmentation
|
||||
console.log('\n=== Executing Cognition Method on Combined Augmentation ===')
|
||||
const reasoningResult = await webSocketCognition.reason('What is the meaning of life?', { context: 'philosophical' })
|
||||
console.log('Reasoning result:', reasoningResult)
|
||||
|
||||
// Use the cognition pipeline with the combined augmentation
|
||||
console.log('\n=== Executing Cognition Pipeline ===')
|
||||
const pipelineResults = await augmentationPipeline.executeCognitionPipeline(
|
||||
'reason',
|
||||
['What is the capital of France?', { additionalContext: 'geography' }]
|
||||
)
|
||||
console.log('Pipeline results:', pipelineResults)
|
||||
|
||||
// Shut down all registered augmentations
|
||||
console.log('\n=== Shutting Down Augmentations ===')
|
||||
await augmentationPipeline.shutDown()
|
||||
|
||||
console.log('\n=== Example Complete ===')
|
||||
} catch (error) {
|
||||
console.error('Error in WebSocket augmentation example:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// Run the example
|
||||
main()
|
||||
14
src/index.ts
14
src/index.ts
|
|
@ -22,6 +22,20 @@ export {
|
|||
dotProductDistance
|
||||
}
|
||||
|
||||
// Export embedding functionality
|
||||
import {
|
||||
SimpleEmbedding,
|
||||
UniversalSentenceEncoder,
|
||||
createEmbeddingFunction,
|
||||
defaultEmbeddingFunction
|
||||
} from './utils/embedding.js'
|
||||
export {
|
||||
SimpleEmbedding,
|
||||
UniversalSentenceEncoder,
|
||||
createEmbeddingFunction,
|
||||
defaultEmbeddingFunction
|
||||
}
|
||||
|
||||
// Export storage adapters
|
||||
import {
|
||||
OPFSStorage,
|
||||
|
|
|
|||
|
|
@ -20,11 +20,11 @@ type WebSocketConnection = {
|
|||
}
|
||||
|
||||
type DataCallback<T> = (data: T) => void
|
||||
export type AugmentationResponse<T> = Promise<{
|
||||
export type AugmentationResponse<T> = {
|
||||
success: boolean
|
||||
data: T
|
||||
error?: string
|
||||
}>
|
||||
}
|
||||
|
||||
/**
|
||||
* Base interface for all Brainy augmentations.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue