feat: add memory and WebSocket augmentation examples with configuration files
This commit is contained in:
parent
022680a2f5
commit
a22fd7de77
9 changed files with 1031 additions and 0 deletions
11
.idea/brainy.iml
generated
Normal file
11
.idea/brainy.iml
generated
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/dist" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/node_modules" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
7
.idea/jsLibraryMappings.xml
generated
Normal file
7
.idea/jsLibraryMappings.xml
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptLibraryMappings">
|
||||
<includedPredefinedLibrary name="Node.js Core" />
|
||||
<includedPredefinedLibrary name="ECMAScript 6" />
|
||||
</component>
|
||||
</project>
|
||||
17
.idea/material_theme_project_new.xml
generated
Normal file
17
.idea/material_theme_project_new.xml
generated
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MaterialThemeProjectNewConfig">
|
||||
<option name="metadata">
|
||||
<MTProjectMetadataState>
|
||||
<option name="migrated" value="true" />
|
||||
<option name="pristineConfig" value="false" />
|
||||
<option name="userId" value="-1be7adca:196364ea35f:-7ffe" />
|
||||
</MTProjectMetadataState>
|
||||
</option>
|
||||
<option name="titleBarState">
|
||||
<MTProjectTitleBarConfigState>
|
||||
<option name="overrideColor" value="false" />
|
||||
</MTProjectTitleBarConfigState>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
9
.idea/misc.xml
generated
Normal file
9
.idea/misc.xml
generated
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/brainy.iml" filepath="$PROJECT_DIR$/.idea/brainy.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/typescript-compiler.xml
generated
Normal file
8
.idea/typescript-compiler.xml
generated
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="TypeScriptCompiler">
|
||||
<option name="recompileOnChanges" value="true" />
|
||||
<option name="typeScriptCompilerParams" value="--sourceMap true" />
|
||||
<option name="useConfig" value="true" />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
755
src/examples/memoryAugmentation.ts
Normal file
755
src/examples/memoryAugmentation.ts
Normal file
|
|
@ -0,0 +1,755 @@
|
|||
/**
|
||||
* 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()
|
||||
210
src/examples/webSocketAugmentation.ts
Normal file
210
src/examples/webSocketAugmentation.ts
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
/**
|
||||
* 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue