holosphere 1.1.19 → 2.0.0-alpha0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +36 -0
- package/.eslintrc.json +16 -0
- package/.prettierrc.json +7 -0
- package/README.md +476 -531
- package/bin/holosphere-activitypub.js +158 -0
- package/cleanup-test-data.js +204 -0
- package/examples/demo.html +1333 -0
- package/examples/example-bot.js +197 -0
- package/package.json +47 -87
- package/scripts/check-bundle-size.js +54 -0
- package/scripts/check-quest-ids.js +77 -0
- package/scripts/import-holons.js +578 -0
- package/scripts/publish-to-relay.js +101 -0
- package/scripts/read-example.js +186 -0
- package/scripts/relay-diagnostic.js +59 -0
- package/scripts/relay-example.js +179 -0
- package/scripts/resync-to-relay.js +245 -0
- package/scripts/revert-import.js +196 -0
- package/scripts/test-hybrid-mode.js +108 -0
- package/scripts/test-local-storage.js +63 -0
- package/scripts/test-nostr-direct.js +55 -0
- package/scripts/test-read-data.js +45 -0
- package/scripts/test-write-read.js +63 -0
- package/scripts/verify-import.js +95 -0
- package/scripts/verify-relay-data.js +139 -0
- package/src/ai/aggregation.js +319 -0
- package/src/ai/breakdown.js +511 -0
- package/src/ai/classifier.js +217 -0
- package/src/ai/council.js +228 -0
- package/src/ai/embeddings.js +279 -0
- package/src/ai/federation-ai.js +324 -0
- package/src/ai/h3-ai.js +955 -0
- package/src/ai/index.js +112 -0
- package/src/ai/json-ops.js +225 -0
- package/src/ai/llm-service.js +205 -0
- package/src/ai/nl-query.js +223 -0
- package/src/ai/relationships.js +353 -0
- package/src/ai/schema-extractor.js +218 -0
- package/src/ai/spatial.js +293 -0
- package/src/ai/tts.js +194 -0
- package/src/content/social-protocols.js +168 -0
- package/src/core/holosphere.js +273 -0
- package/src/crypto/secp256k1.js +259 -0
- package/src/federation/discovery.js +334 -0
- package/src/federation/hologram.js +1042 -0
- package/src/federation/registry.js +386 -0
- package/src/hierarchical/upcast.js +110 -0
- package/src/index.js +2669 -0
- package/src/schema/validator.js +91 -0
- package/src/spatial/h3-operations.js +110 -0
- package/src/storage/backend-factory.js +125 -0
- package/src/storage/backend-interface.js +142 -0
- package/src/storage/backends/activitypub/server.js +653 -0
- package/src/storage/backends/activitypub-backend.js +272 -0
- package/src/storage/backends/gundb-backend.js +233 -0
- package/src/storage/backends/nostr-backend.js +136 -0
- package/src/storage/filesystem-storage-browser.js +41 -0
- package/src/storage/filesystem-storage.js +138 -0
- package/src/storage/global-tables.js +81 -0
- package/src/storage/gun-async.js +281 -0
- package/src/storage/gun-wrapper.js +221 -0
- package/src/storage/indexeddb-storage.js +122 -0
- package/src/storage/key-storage-simple.js +76 -0
- package/src/storage/key-storage.js +136 -0
- package/src/storage/memory-storage.js +59 -0
- package/src/storage/migration.js +338 -0
- package/src/storage/nostr-async.js +811 -0
- package/src/storage/nostr-client.js +939 -0
- package/src/storage/nostr-wrapper.js +211 -0
- package/src/storage/outbox-queue.js +208 -0
- package/src/storage/persistent-storage.js +109 -0
- package/src/storage/sync-service.js +164 -0
- package/src/subscriptions/manager.js +142 -0
- package/test-ai-real-api.js +202 -0
- package/tests/unit/ai/aggregation.test.js +295 -0
- package/tests/unit/ai/breakdown.test.js +446 -0
- package/tests/unit/ai/classifier.test.js +294 -0
- package/tests/unit/ai/council.test.js +262 -0
- package/tests/unit/ai/embeddings.test.js +384 -0
- package/tests/unit/ai/federation-ai.test.js +344 -0
- package/tests/unit/ai/h3-ai.test.js +458 -0
- package/tests/unit/ai/index.test.js +304 -0
- package/tests/unit/ai/json-ops.test.js +307 -0
- package/tests/unit/ai/llm-service.test.js +390 -0
- package/tests/unit/ai/nl-query.test.js +383 -0
- package/tests/unit/ai/relationships.test.js +311 -0
- package/tests/unit/ai/schema-extractor.test.js +384 -0
- package/tests/unit/ai/spatial.test.js +279 -0
- package/tests/unit/ai/tts.test.js +279 -0
- package/tests/unit/content.test.js +332 -0
- package/tests/unit/contract/core.test.js +88 -0
- package/tests/unit/contract/crypto.test.js +198 -0
- package/tests/unit/contract/data.test.js +223 -0
- package/tests/unit/contract/federation.test.js +181 -0
- package/tests/unit/contract/hierarchical.test.js +113 -0
- package/tests/unit/contract/schema.test.js +114 -0
- package/tests/unit/contract/social.test.js +217 -0
- package/tests/unit/contract/spatial.test.js +110 -0
- package/tests/unit/contract/subscriptions.test.js +128 -0
- package/tests/unit/contract/utils.test.js +159 -0
- package/tests/unit/core.test.js +152 -0
- package/tests/unit/crypto.test.js +328 -0
- package/tests/unit/federation.test.js +234 -0
- package/tests/unit/gun-async.test.js +252 -0
- package/tests/unit/hierarchical.test.js +399 -0
- package/tests/unit/integration/scenario-01-geographic-storage.test.js +74 -0
- package/tests/unit/integration/scenario-02-federation.test.js +76 -0
- package/tests/unit/integration/scenario-03-subscriptions.test.js +102 -0
- package/tests/unit/integration/scenario-04-validation.test.js +129 -0
- package/tests/unit/integration/scenario-05-hierarchy.test.js +125 -0
- package/tests/unit/integration/scenario-06-social.test.js +135 -0
- package/tests/unit/integration/scenario-07-persistence.test.js +130 -0
- package/tests/unit/integration/scenario-08-authorization.test.js +161 -0
- package/tests/unit/integration/scenario-09-cross-dimensional.test.js +139 -0
- package/tests/unit/integration/scenario-10-cross-holosphere-capabilities.test.js +357 -0
- package/tests/unit/integration/scenario-11-cross-holosphere-federation.test.js +410 -0
- package/tests/unit/integration/scenario-12-capability-federated-read.test.js +719 -0
- package/tests/unit/performance/benchmark.test.js +85 -0
- package/tests/unit/schema.test.js +213 -0
- package/tests/unit/spatial.test.js +158 -0
- package/tests/unit/storage.test.js +195 -0
- package/tests/unit/subscriptions.test.js +328 -0
- package/tests/unit/test-data-permanence-debug.js +197 -0
- package/tests/unit/test-data-permanence.js +340 -0
- package/tests/unit/test-key-persistence-fixed.js +148 -0
- package/tests/unit/test-key-persistence.js +172 -0
- package/tests/unit/test-relay-permanence.js +376 -0
- package/tests/unit/test-second-node.js +95 -0
- package/tests/unit/test-simple-write.js +89 -0
- package/vite.config.js +49 -0
- package/vitest.config.js +20 -0
- package/FEDERATION.md +0 -213
- package/compute.js +0 -298
- package/content.js +0 -1022
- package/federation.js +0 -1234
- package/global.js +0 -736
- package/hexlib.js +0 -335
- package/hologram.js +0 -183
- package/holosphere-bundle.esm.js +0 -34549
- package/holosphere-bundle.js +0 -34580
- package/holosphere-bundle.min.js +0 -49
- package/holosphere.d.ts +0 -604
- package/holosphere.js +0 -739
- package/node.js +0 -246
- package/schema.js +0 -139
- package/utils.js +0 -302
package/FEDERATION.md
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
# HoloSphere Federation
|
|
2
|
-
|
|
3
|
-
HoloSphere's federation system allows different holons (spaces) to share and access data from each other. Federation creates a relationship between spaces that enables data propagation and cross-space access.
|
|
4
|
-
|
|
5
|
-
## Key Concepts
|
|
6
|
-
|
|
7
|
-
- **Federation Relationship**: A connection between two spaces that allows data to flow between them.
|
|
8
|
-
- **Soul References**: Lightweight references that point to data in its original location (single source of truth).
|
|
9
|
-
- **Notification Flow**: Data notifications flow from source spaces to target spaces that are in the notify list.
|
|
10
|
-
- **Source-Target Relationship**: Each federation sets up a source space (federation list) and a target space (notify list).
|
|
11
|
-
|
|
12
|
-
## Federation Data Flow
|
|
13
|
-
|
|
14
|
-
The HoloSphere federation system works with a clear source-target relationship:
|
|
15
|
-
|
|
16
|
-
1. **Federation List**: When space A federates with space B, space A adds B to its federation list.
|
|
17
|
-
2. **Notify List**: Space B adds space A to its notify list.
|
|
18
|
-
3. **Data Flow**: When space A changes, space B gets notified (but not vice versa unless bidirectional).
|
|
19
|
-
|
|
20
|
-
## Creating Federation
|
|
21
|
-
|
|
22
|
-
Create federation relationships between spaces:
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
// Create federation between space1 and space2
|
|
26
|
-
await holoSphere.federate('space1', 'space2');
|
|
27
|
-
|
|
28
|
-
// This sets up:
|
|
29
|
-
// - space1.federation includes space2
|
|
30
|
-
// - space2.notify includes space1
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
The bidirectional parameter is largely unused in the current implementation since the federation system naturally sets up the correct notification flow. The default relationship allows space2 to be notified of changes in space1.
|
|
34
|
-
|
|
35
|
-
## Storing and Propagating Data
|
|
36
|
-
|
|
37
|
-
Data must be explicitly propagated to federated spaces:
|
|
38
|
-
|
|
39
|
-
```javascript
|
|
40
|
-
const data = {
|
|
41
|
-
id: 'item1',
|
|
42
|
-
title: 'Federation Example',
|
|
43
|
-
value: 42
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// Store data in space1
|
|
47
|
-
await holoSphere.put('space1', 'items', data);
|
|
48
|
-
|
|
49
|
-
// Propagate to federated spaces
|
|
50
|
-
await holoSphere.propagate('space1', 'items', data);
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
You can also enable automatic propagation in the `put()` method:
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
// Store data and automatically propagate
|
|
57
|
-
await holoSphere.put('space1', 'items', data, null, {
|
|
58
|
-
autoPropagate: true
|
|
59
|
-
});
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
## Accessing Federated Data
|
|
63
|
-
|
|
64
|
-
### Direct Retrieval
|
|
65
|
-
|
|
66
|
-
You can access data directly from any space:
|
|
67
|
-
|
|
68
|
-
```javascript
|
|
69
|
-
// Retrieve data from space2 (will resolve hologram if it's a hologram)
|
|
70
|
-
const data = await holoSphere.get('space2', 'items', 'item1', null, {
|
|
71
|
-
resolveHolograms: true // Default is true
|
|
72
|
-
});
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
### Aggregate Federated Data
|
|
76
|
-
|
|
77
|
-
Use `getFederated()` to get data from multiple federated spaces:
|
|
78
|
-
|
|
79
|
-
```javascript
|
|
80
|
-
// Get combined data from the local space and all its federated spaces
|
|
81
|
-
const federatedData = await holoSphere.getFederated('space2', 'items', {
|
|
82
|
-
resolveHolograms: true, // Default: true
|
|
83
|
-
idField: 'id' // Field to use as the unique identifier
|
|
84
|
-
});
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Soul Holograms
|
|
88
|
-
|
|
89
|
-
HoloSphere uses a simplified hologram system based on soul paths:
|
|
90
|
-
|
|
91
|
-
1. A hologram contains only an `id` and a `soul` property
|
|
92
|
-
2. The soul path is in the format: `appname/holon/lens/key`
|
|
93
|
-
3. When resolving a hologram, HoloSphere follows the soul path to retrieve the original data
|
|
94
|
-
|
|
95
|
-
By default, federation propagation uses holograms instead of duplicating data. This can be controlled:
|
|
96
|
-
|
|
97
|
-
```javascript
|
|
98
|
-
// Propagate with full data copy instead of holograms
|
|
99
|
-
await holoSphere.propagate('space1', 'items', data, {
|
|
100
|
-
useHolograms: false
|
|
101
|
-
});
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## Removing Federation
|
|
105
|
-
|
|
106
|
-
```javascript
|
|
107
|
-
// Remove federation relationship
|
|
108
|
-
await holoSphere.unfederate('space1', 'space2');
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
## Complete Example
|
|
112
|
-
|
|
113
|
-
Here's a complete example showing the proper way to set up and use federation:
|
|
114
|
-
|
|
115
|
-
```javascript
|
|
116
|
-
import HoloSphere from './holosphere.js';
|
|
117
|
-
|
|
118
|
-
async function federationExample() {
|
|
119
|
-
const holoSphere = new HoloSphere('example-app');
|
|
120
|
-
|
|
121
|
-
try {
|
|
122
|
-
const space1 = 'public-space1';
|
|
123
|
-
const space2 = 'public-space2';
|
|
124
|
-
|
|
125
|
-
// Step 1: Create federation relationship
|
|
126
|
-
await holoSphere.federate(space1, space2);
|
|
127
|
-
|
|
128
|
-
// Step 2: Verify federation is set up properly
|
|
129
|
-
const fedInfo1 = await holoSphere.getFederation(space1);
|
|
130
|
-
const fedInfo2 = await holoSphere.getFederation(space2);
|
|
131
|
-
|
|
132
|
-
console.log(`Federation info for ${space1}:`, fedInfo1);
|
|
133
|
-
// Should include: federation: ['space2']
|
|
134
|
-
|
|
135
|
-
console.log(`Federation info for ${space2}:`, fedInfo2);
|
|
136
|
-
// Should include: notify: ['space1']
|
|
137
|
-
|
|
138
|
-
// Step 3: Store data in space1
|
|
139
|
-
const item = {
|
|
140
|
-
id: 'item1',
|
|
141
|
-
title: 'Federation Test',
|
|
142
|
-
value: 42
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
await holoSphere.put(space1, 'items', item);
|
|
146
|
-
|
|
147
|
-
// Step 4: Propagate data to federated spaces
|
|
148
|
-
await holoSphere.propagate(space1, 'items', item);
|
|
149
|
-
|
|
150
|
-
// Allow time for propagation
|
|
151
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
152
|
-
|
|
153
|
-
// Step 5: Access data from space2 (resolves hologram)
|
|
154
|
-
const itemFromSpace2 = await holoSphere.get(space2, 'items', 'item1');
|
|
155
|
-
console.log('Item from space2:', itemFromSpace2);
|
|
156
|
-
|
|
157
|
-
// Step 6: Update item in space1
|
|
158
|
-
const updatedItem = {
|
|
159
|
-
...item,
|
|
160
|
-
value: 100,
|
|
161
|
-
updated: true
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
await holoSphere.put(space1, 'items', updatedItem);
|
|
165
|
-
|
|
166
|
-
// Since we're using soul holograms, the update is immediately visible
|
|
167
|
-
// through the hologram without needing to propagate again
|
|
168
|
-
|
|
169
|
-
// Verify update is visible through the hologram
|
|
170
|
-
const updatedItemFromSpace2 = await holoSphere.get(space2, 'items', 'item1');
|
|
171
|
-
console.log('Updated item from space2:', updatedItemFromSpace2);
|
|
172
|
-
|
|
173
|
-
// Step 7: Clean up
|
|
174
|
-
await holoSphere.unfederate(space1, space2);
|
|
175
|
-
} finally {
|
|
176
|
-
// Always close the HoloSphere instance
|
|
177
|
-
await holoSphere.close();
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
federationExample().catch(console.error);
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
## Troubleshooting
|
|
185
|
-
|
|
186
|
-
### Common Issues
|
|
187
|
-
|
|
188
|
-
1. **Federation Relationship**: Make sure to check both the federation list and notify list to understand data flow.
|
|
189
|
-
|
|
190
|
-
2. **Data Propagation**: If data isn't appearing in federated spaces, check:
|
|
191
|
-
- The federation relationship was created correctly
|
|
192
|
-
- The data was propagated explicitly or `autoPropagate` was set to `true`
|
|
193
|
-
- The notify list includes the target space
|
|
194
|
-
|
|
195
|
-
3. **Reference Resolution**: If you're getting reference objects instead of the actual data:
|
|
196
|
-
- Make sure `resolveHolograms` is set to `true` (it's the default)
|
|
197
|
-
- Check that the original data still exists at the referenced location
|
|
198
|
-
|
|
199
|
-
4. **Timing Issues**: Data propagation is asynchronous. Add small delays (500-1000ms) between operations to allow propagation to complete.
|
|
200
|
-
|
|
201
|
-
### Best Practices
|
|
202
|
-
|
|
203
|
-
1. **Verify Federation Structure**: After creating a federation, check both spaces to ensure:
|
|
204
|
-
- Source space has the target in its federation list
|
|
205
|
-
- Target space has the source in its notify list
|
|
206
|
-
|
|
207
|
-
2. **Explicit Propagation**: Unless you're using `autoPropagate`, always call `propagate()` explicitly after storing data that should be shared.
|
|
208
|
-
|
|
209
|
-
3. **Choose the Right Propagation Method**:
|
|
210
|
-
- Use `useHolograms: true` (default) to keep a single source of truth
|
|
211
|
-
- Use `useHolograms: false` only when you need independent copies
|
|
212
|
-
|
|
213
|
-
4. **Cleanup**: Always close the HoloSphere instance when done to prevent resource leaks.
|
package/compute.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
// holo_compute.js
|
|
2
|
-
import * as h3 from 'h3-js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Computes operations across multiple layers up the hierarchy
|
|
6
|
-
* @param {HoloSphere} holoInstance - The HoloSphere instance.
|
|
7
|
-
* @param {string} holon - Starting holon identifier
|
|
8
|
-
* @param {string} lens - The lens to compute
|
|
9
|
-
* @param {object} options - Computation options
|
|
10
|
-
* @param {number} [maxLevels=15] - Maximum levels to compute up
|
|
11
|
-
* @param {string} [password] - Optional password for private holons
|
|
12
|
-
*/
|
|
13
|
-
export async function computeHierarchy(holoInstance, holon, lens, options, maxLevels = 15, password = null) {
|
|
14
|
-
let currentHolon = holon;
|
|
15
|
-
let currentRes = h3.getResolution(currentHolon);
|
|
16
|
-
const results = [];
|
|
17
|
-
|
|
18
|
-
while (currentRes > 0 && maxLevels > 0) {
|
|
19
|
-
try {
|
|
20
|
-
// Use the instance's compute method
|
|
21
|
-
const result = await holoInstance.compute(currentHolon, lens, options, password);
|
|
22
|
-
if (result) {
|
|
23
|
-
results.push(result);
|
|
24
|
-
}
|
|
25
|
-
currentHolon = h3.cellToParent(currentHolon, currentRes - 1);
|
|
26
|
-
currentRes--;
|
|
27
|
-
maxLevels--;
|
|
28
|
-
} catch (error) {
|
|
29
|
-
console.error('Error in compute hierarchy:', error);
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return results;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Computes operations on content within a holon and lens for one layer up.
|
|
39
|
-
* @param {HoloSphere} holoInstance - The HoloSphere instance.
|
|
40
|
-
* @param {string} holon - The holon identifier.
|
|
41
|
-
* @param {string} lens - The lens to compute.
|
|
42
|
-
* @param {object} options - Computation options
|
|
43
|
-
* @param {string} options.operation - The operation to perform ('summarize', 'aggregate', 'concatenate')
|
|
44
|
-
* @param {string[]} [options.fields] - Fields to perform operation on
|
|
45
|
-
* @param {string} [options.targetField] - Field to store the result in
|
|
46
|
-
* @param {string} [password] - Optional password for private holons
|
|
47
|
-
* @throws {Error} If parameters are invalid or missing
|
|
48
|
-
*/
|
|
49
|
-
export async function compute(holoInstance, holon, lens, options, password = null) {
|
|
50
|
-
// Validate required parameters
|
|
51
|
-
if (!holon || !lens) {
|
|
52
|
-
throw new Error('compute: Missing required parameters');
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Convert string operation to options object
|
|
56
|
-
if (typeof options === 'string') {
|
|
57
|
-
options = { operation: options };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!options?.operation) {
|
|
61
|
-
throw new Error('compute: Missing required parameters');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Validate holon format and resolution first
|
|
65
|
-
let res;
|
|
66
|
-
try {
|
|
67
|
-
res = h3.getResolution(holon);
|
|
68
|
-
} catch (error) {
|
|
69
|
-
throw new Error('compute: Invalid holon format');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (res < 1 || res > 15) {
|
|
73
|
-
throw new Error('compute: Invalid holon resolution (must be between 1 and 15)');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const {
|
|
77
|
-
operation,
|
|
78
|
-
fields = [],
|
|
79
|
-
targetField,
|
|
80
|
-
depth,
|
|
81
|
-
maxDepth
|
|
82
|
-
} = options;
|
|
83
|
-
|
|
84
|
-
// Validate depth parameters if provided
|
|
85
|
-
if (depth !== undefined && depth < 0) {
|
|
86
|
-
throw new Error('compute: Invalid depth parameter');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (maxDepth !== undefined && (maxDepth < 1 || maxDepth > 15)) {
|
|
90
|
-
throw new Error('compute: Invalid maxDepth parameter (must be between 1 and 15)');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Validate operation
|
|
94
|
-
const validOperations = ['summarize', 'aggregate', 'concatenate'];
|
|
95
|
-
if (!validOperations.includes(operation)) {
|
|
96
|
-
throw new Error(`compute: Invalid operation (must be one of ${validOperations.join(', ')})`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const parent = h3.cellToParent(holon, res - 1);
|
|
100
|
-
const siblings = h3.cellToChildren(parent, res);
|
|
101
|
-
|
|
102
|
-
// Collect all content from siblings using instance's getAll
|
|
103
|
-
const contents = await Promise.all(
|
|
104
|
-
siblings.map(sibling => holoInstance.getAll(sibling, lens, password))
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
const flatContents = contents.flat().filter(Boolean);
|
|
108
|
-
|
|
109
|
-
if (flatContents.length > 0) {
|
|
110
|
-
try {
|
|
111
|
-
let computed;
|
|
112
|
-
switch (operation) {
|
|
113
|
-
case 'summarize':
|
|
114
|
-
// For summarize, concatenate specified fields or use entire content
|
|
115
|
-
const textToSummarize = fields.length > 0
|
|
116
|
-
? flatContents.map(item => fields.map(field => item[field]).filter(Boolean).join('\n')).join('\n')
|
|
117
|
-
: JSON.stringify(flatContents);
|
|
118
|
-
computed = await holoInstance.summarize(textToSummarize); // Use instance's summarize
|
|
119
|
-
break;
|
|
120
|
-
|
|
121
|
-
case 'aggregate':
|
|
122
|
-
// For aggregate, sum numeric fields
|
|
123
|
-
computed = fields.reduce((acc, field) => {
|
|
124
|
-
acc[field] = flatContents.reduce((sum, item) => {
|
|
125
|
-
return sum + (Number(item[field]) || 0);
|
|
126
|
-
}, 0);
|
|
127
|
-
return acc;
|
|
128
|
-
}, {});
|
|
129
|
-
break;
|
|
130
|
-
|
|
131
|
-
case 'concatenate':
|
|
132
|
-
// For concatenate, combine arrays or strings
|
|
133
|
-
computed = fields.reduce((acc, field) => {
|
|
134
|
-
acc[field] = flatContents.reduce((combined, item) => {
|
|
135
|
-
const value = item[field];
|
|
136
|
-
if (Array.isArray(value)) {
|
|
137
|
-
return [...combined, ...value];
|
|
138
|
-
} else if (value) {
|
|
139
|
-
return [...combined, value];
|
|
140
|
-
}
|
|
141
|
-
return combined;
|
|
142
|
-
}, []);
|
|
143
|
-
// Remove duplicates if array
|
|
144
|
-
acc[field] = Array.from(new Set(acc[field]));
|
|
145
|
-
return acc;
|
|
146
|
-
}, {});
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (computed) {
|
|
151
|
-
const resultId = `${parent}_${operation}`;
|
|
152
|
-
const result = {
|
|
153
|
-
id: resultId,
|
|
154
|
-
timestamp: Date.now()
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
// Store result in targetField if specified, otherwise at root level
|
|
158
|
-
if (targetField) {
|
|
159
|
-
result[targetField] = computed;
|
|
160
|
-
} else if (typeof computed === 'object') {
|
|
161
|
-
Object.assign(result, computed);
|
|
162
|
-
} else {
|
|
163
|
-
result.value = computed;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
await holoInstance.put(parent, lens, result, password); // Use instance's put
|
|
167
|
-
return result;
|
|
168
|
-
}
|
|
169
|
-
} catch (error) {
|
|
170
|
-
console.warn('Error in compute operation:', error);
|
|
171
|
-
throw error;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Summarizes provided history text using OpenAI.
|
|
180
|
-
* @param {HoloSphere} holoInstance - The HoloSphere instance.
|
|
181
|
-
* @param {string} history - The history text to summarize.
|
|
182
|
-
* @returns {Promise<string>} - The summarized text.
|
|
183
|
-
*/
|
|
184
|
-
export async function summarize(holoInstance, history) {
|
|
185
|
-
if (!holoInstance.openai) {
|
|
186
|
-
return 'OpenAI not initialized, please specify the API key in the constructor.'
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
try {
|
|
190
|
-
const response = await holoInstance.openai.chat.completions.create({
|
|
191
|
-
model: "gpt-4",
|
|
192
|
-
messages: [
|
|
193
|
-
{
|
|
194
|
-
role: "system",
|
|
195
|
-
content: "You are a helpful assistant that summarizes text concisely while preserving key information. Keep summaries clear and focused."
|
|
196
|
-
},
|
|
197
|
-
{
|
|
198
|
-
role: "user",
|
|
199
|
-
content: history
|
|
200
|
-
}
|
|
201
|
-
],
|
|
202
|
-
temperature: 0.7,
|
|
203
|
-
max_tokens: 500
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
return response.choices[0].message.content.trim();
|
|
207
|
-
} catch (error) {
|
|
208
|
-
console.error('Error in summarize:', error);
|
|
209
|
-
throw new Error('Failed to generate summary');
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Upcasts content to parent holonagons recursively using holograms.
|
|
215
|
-
* @param {HoloSphere} holoInstance - The HoloSphere instance.
|
|
216
|
-
* @param {string} holon - The current holon identifier.
|
|
217
|
-
* @param {string} lens - The lens under which to upcast.
|
|
218
|
-
* @param {object} content - The content to upcast.
|
|
219
|
-
* @param {number} [maxLevels=15] - Maximum levels to upcast.
|
|
220
|
-
* @returns {Promise<object>} - The original content.
|
|
221
|
-
*/
|
|
222
|
-
export async function upcast(holoInstance, holon, lens, content, maxLevels = 15) {
|
|
223
|
-
// Store the actual content at the original resolution using instance's put
|
|
224
|
-
await holoInstance.put(holon, lens, content);
|
|
225
|
-
|
|
226
|
-
let res = h3.getResolution(holon);
|
|
227
|
-
|
|
228
|
-
// If already at the highest level (res 0) or reached max levels, we're done
|
|
229
|
-
if (res === 0 || maxLevels <= 0) {
|
|
230
|
-
return content;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Get the parent cell
|
|
234
|
-
let parent = h3.cellToParent(holon, res - 1);
|
|
235
|
-
|
|
236
|
-
// Create a hologram to store in the parent using instance's createHologram
|
|
237
|
-
const hologram = holoInstance.createHologram(holon, lens, content);
|
|
238
|
-
|
|
239
|
-
// Store the hologram in the parent cell using instance's put
|
|
240
|
-
await holoInstance.put(parent, lens, hologram, null, {
|
|
241
|
-
autoPropagate: false
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
// Continue upcasting with the parent using instance's upcast
|
|
245
|
-
if (res > 1 && maxLevels > 1) {
|
|
246
|
-
// Recursively call the instance's upcast method
|
|
247
|
-
// Pass the *hologram* to the next level, not the original content
|
|
248
|
-
return holoInstance.upcast(parent, lens, hologram, maxLevels - 1);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return content;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Updates the parent holon with a new report.
|
|
256
|
-
* Note: This function assumes the existence of `getCellInfo` and `db.put` on the holoInstance,
|
|
257
|
-
* which were not defined in the original class. Adjust as needed.
|
|
258
|
-
* @param {HoloSphere} holoInstance - The HoloSphere instance.
|
|
259
|
-
* @param {string} id - The child holon identifier.
|
|
260
|
-
* @param {string} report - The report to update.
|
|
261
|
-
* @returns {Promise<object>} - The updated parent information.
|
|
262
|
-
*/
|
|
263
|
-
export async function updateParent(holoInstance, id, report) {
|
|
264
|
-
// Check if required methods exist on the instance
|
|
265
|
-
if (typeof holoInstance.getCellInfo !== 'function' || !holoInstance.db || typeof holoInstance.db.put !== 'function') {
|
|
266
|
-
console.warn('updateParent requires getCellInfo and db.put methods on the HoloSphere instance.');
|
|
267
|
-
// Decide how to handle this: throw error, return null, etc.
|
|
268
|
-
// For now, let's proceed assuming they might exist but log a warning.
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
try {
|
|
272
|
-
let cellinfo = await holoInstance.getCellInfo(id)
|
|
273
|
-
let res = h3.getResolution(id)
|
|
274
|
-
let parent = h3.cellToParent(id, res - 1)
|
|
275
|
-
let parentInfo = await holoInstance.getCellInfo(parent)
|
|
276
|
-
parentInfo.wisdom[id] = report
|
|
277
|
-
//update summary using instance's summarize
|
|
278
|
-
let summary = await holoInstance.summarize(Object.values(parentInfo.wisdom).join('\n'))
|
|
279
|
-
parentInfo.summary = summary
|
|
280
|
-
|
|
281
|
-
// Assuming db is accessible like this
|
|
282
|
-
await holoInstance.db.put('cell', parentInfo)
|
|
283
|
-
return parentInfo
|
|
284
|
-
} catch (error) {
|
|
285
|
-
console.error("Error during updateParent:", error);
|
|
286
|
-
// Re-throw or handle error appropriately
|
|
287
|
-
throw error;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// Export all compute operations as default
|
|
292
|
-
export default {
|
|
293
|
-
computeHierarchy,
|
|
294
|
-
compute,
|
|
295
|
-
summarize,
|
|
296
|
-
upcast,
|
|
297
|
-
updateParent
|
|
298
|
-
};
|