UNPKG

pipe-protocol

Version:

A protocol for large scale Interplanetary Intertool Agent Context

221 lines 6.9 kB
"use strict"; /** * @file Pipe Class Implementation * @version 1.0.0 * @status STABLE - DO NOT MODIFY WITHOUT TESTS * @lastModified 2024-02-04 * * Core class for managing tool wrapping and IPFS integration. * * IMPORTANT: * - All modifications must maintain test coverage * - Configuration must be immutable after initialization * - Hook system must maintain order of execution * - IPFS operations must handle errors gracefully * * Functionality: * - Tool wrapping with IPFS capabilities * - Hook system for extensibility * - Configuration management * - IPFS client integration * - Token counting and limiting * - Schema generation * - Scope management (public/private) * - Pin management */ Object.defineProperty(exports, "__esModule", { value: true }); exports.summaryHook = exports.PipeProtocol = exports.Pipe = void 0; const ipfsClient_1 = require("./services/ipfs/ipfsClient"); const toolWrapping_1 = require("./services/pipe/toolWrapping"); const pipeTool_1 = require("./services/pipe/pipeTool"); const utils_1 = require("./utils"); class Pipe { constructor(config) { this.hooks = []; // Initialize IPFS configuration const ipfsConfig = { endpoint: config?.ipfs?.endpoint || 'http://localhost:5001', timeout: config?.ipfs?.timeout || 30000, scope: config?.ipfs?.scope || 'private', pin: config?.ipfs?.pin ?? true }; // Initialize default configuration this.config = { ipfs: ipfsConfig, defaults: { maxTokens: config?.defaults?.maxTokens ?? 1000, storeResult: config?.defaults?.storeResult ?? true, generateSchema: config?.defaults?.generateSchema ?? true, scope: config?.defaults?.scope ?? 'private', pin: config?.defaults?.pin ?? true } }; this.ipfsClient = new ipfsClient_1.IPFSClient(ipfsConfig); } /** * Add hooks to the pipe */ addHooks(hooks) { this.hooks.push(...hooks); } /** * Remove a hook by name */ removeHook(name) { this.hooks = this.hooks.filter(hook => hook.name !== name); } /** * Execute hooks of a specific type */ async executeHooks(type, data) { let result = data; for (const hook of this.hooks.filter(h => h.type === type)) { result = await hook.handler(result); } return result; } /** * Store data in IPFS */ async store(data, options) { const processedData = await this.executeHooks('beforeStore', data); const cid = await this.ipfsClient.store(processedData, { pin: options?.pin ?? this.config.defaults.pin, scope: options?.scope ?? this.config.defaults.scope }); await this.executeHooks('afterStore', { data: processedData, cid }); return cid; } /** * Retrieve data from IPFS */ async retrieve(cid) { return this.ipfsClient.fetch(cid, this.config.defaults.scope || 'private'); } /** * Wrap tools with IPFS capabilities */ wrap(tools) { // Create the pipe tool const pipeTool = (0, pipeTool_1.createPipeTool)(this.ipfsClient); // Wrap the provided tools const wrappedTools = tools.map(tool => (0, toolWrapping_1.wrapTool)(tool, { ipfsClient: this.ipfsClient, maxTokens: this.config.defaults.maxTokens, storeResult: this.config.defaults.storeResult, generateSchema: this.config.defaults.generateSchema, scope: this.config.defaults.scope, pin: this.config.defaults.pin, hooks: { beforeStore: (data) => this.executeHooks('beforeStore', data), afterStore: (data) => this.executeHooks('afterStore', data) } })); // Append the pipe tool at the end return [...wrappedTools, pipeTool]; } /** * Publish a record to IPFS */ async publishRecord(record) { const processedRecord = await this.executeHooks('beforeStore', record); const cid = await this.ipfsClient.store(processedRecord, { pin: this.config.defaults.pin, scope: processedRecord.scope }); const publishedRecord = { ...processedRecord, cid }; await this.executeHooks('afterStore', publishedRecord); return publishedRecord; } /** * Fetch a record from IPFS */ async fetchRecord(cid, scope) { const result = await this.ipfsClient.fetch(cid, scope); return result; } /** * Pin a record in IPFS */ async pin(cid, scope) { await this.ipfsClient.pin(cid, scope); } /** * Unpin a record from IPFS */ async unpin(cid, scope) { await this.ipfsClient.unpin(cid, scope); } /** * Get pinned CIDs for a scope */ async getPinnedCids(scope) { return this.ipfsClient.getPinnedCids(scope); } /** * Get node status */ getStatus() { return this.ipfsClient.getStatus(); } /** * Get node info for a scope */ getNodeInfo(scope) { return this.ipfsClient.getNodeInfo(scope); } /** * Get storage metrics for a scope */ async getStorageMetrics(scope) { return this.ipfsClient.getStorageMetrics(scope); } /** * Publish a bundle to IPFS */ async publishBundle(bundle) { const processedBundle = await this.executeHooks('beforeStore', bundle); const schemaRecord = await this.publishRecord(processedBundle.schemaRecord); const dataRecord = await this.publishRecord(processedBundle.dataRecord); const publishedBundle = { ...processedBundle, schemaRecord, dataRecord }; await this.executeHooks('afterStore', publishedBundle); return publishedBundle; } /** * Replicate a record from one scope to another */ async replicate(cid, fromScope, toScope) { await this.ipfsClient.replicate(cid, fromScope, toScope); } /** * Get configuration for a scope */ async getConfiguration(scope) { return this.ipfsClient.getConfiguration(scope); } /** * Stop the pipe and cleanup resources */ async stop() { await this.ipfsClient.stop(); } } exports.Pipe = Pipe; exports.PipeProtocol = Pipe; /** * Example summary hook that generates a summary of data before storage */ exports.summaryHook = { name: 'summary', type: 'beforeStore', handler: async (data) => { const summary = await (0, utils_1.generateSummary)(data); return { data, metadata: { summary } }; } }; //# sourceMappingURL=pipe.js.map