@olane/os 0.7.12-alpha.9 → 0.7.13-alpha.0

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.
Files changed (66) hide show
  1. package/dist/src/o-olane-os/config/default.config.js +1 -1
  2. package/dist/src/o-olane-os/o-os.d.ts +1 -1
  3. package/dist/src/o-olane-os/o-os.d.ts.map +1 -1
  4. package/dist/src/o-olane-os/o-os.js +7 -5
  5. package/dist/src/utils/config.d.ts.map +1 -1
  6. package/dist/src/utils/config.js +2 -2
  7. package/dist/test/basic/basic-usage.spec.d.ts +0 -1
  8. package/dist/test/basic/basic-usage.spec.js +137 -107
  9. package/dist/test/basic/playground.spec.d.ts +2 -0
  10. package/dist/test/basic/playground.spec.d.ts.map +1 -0
  11. package/dist/test/basic/playground.spec.js +110 -0
  12. package/dist/test/capabilities/base-capability.spec.d.ts +2 -0
  13. package/dist/test/capabilities/base-capability.spec.d.ts.map +1 -0
  14. package/dist/test/capabilities/base-capability.spec.js +174 -0
  15. package/dist/test/capabilities/capability-errors.spec.d.ts +2 -0
  16. package/dist/test/capabilities/capability-errors.spec.d.ts.map +1 -0
  17. package/dist/test/capabilities/capability-errors.spec.js +340 -0
  18. package/dist/test/capabilities/capability-integration.spec.d.ts +2 -0
  19. package/dist/test/capabilities/capability-integration.spec.d.ts.map +1 -0
  20. package/dist/test/capabilities/capability-integration.spec.js +463 -0
  21. package/dist/test/capabilities/capability-registry.spec.d.ts +2 -0
  22. package/dist/test/capabilities/capability-registry.spec.d.ts.map +1 -0
  23. package/dist/test/capabilities/capability-registry.spec.js +261 -0
  24. package/dist/test/capabilities/configure-capability.spec.d.ts +2 -0
  25. package/dist/test/capabilities/configure-capability.spec.d.ts.map +1 -0
  26. package/dist/test/capabilities/configure-capability.spec.js +366 -0
  27. package/dist/test/capabilities/evaluate-capability.spec.d.ts +2 -0
  28. package/dist/test/capabilities/evaluate-capability.spec.d.ts.map +1 -0
  29. package/dist/test/capabilities/evaluate-capability.spec.js +323 -0
  30. package/dist/test/capabilities/intelligence-capability.spec.d.ts +2 -0
  31. package/dist/test/capabilities/intelligence-capability.spec.d.ts.map +1 -0
  32. package/dist/test/capabilities/intelligence-capability.spec.js +171 -0
  33. package/dist/test/capabilities/multiple-step-capability.spec.d.ts +2 -0
  34. package/dist/test/capabilities/multiple-step-capability.spec.d.ts.map +1 -0
  35. package/dist/test/capabilities/multiple-step-capability.spec.js +441 -0
  36. package/dist/test/capabilities/search-capability.spec.d.ts +2 -0
  37. package/dist/test/capabilities/search-capability.spec.d.ts.map +1 -0
  38. package/dist/test/capabilities/search-capability.spec.js +337 -0
  39. package/dist/test/capabilities/task-capability.spec.d.ts +2 -0
  40. package/dist/test/capabilities/task-capability.spec.d.ts.map +1 -0
  41. package/dist/test/capabilities/task-capability.spec.js +335 -0
  42. package/dist/test/capabilities/utils/capability-test-utils.d.ts +68 -0
  43. package/dist/test/capabilities/utils/capability-test-utils.d.ts.map +1 -0
  44. package/dist/test/capabilities/utils/capability-test-utils.js +161 -0
  45. package/dist/test/utils/tmp.node.d.ts +3 -0
  46. package/dist/test/utils/tmp.node.d.ts.map +1 -0
  47. package/dist/test/utils/tmp.node.js +8 -0
  48. package/package.json +15 -12
  49. package/dist/src/network/config/default.config.d.ts +0 -3
  50. package/dist/src/network/config/default.config.d.ts.map +0 -1
  51. package/dist/src/network/config/default.config.js +0 -32
  52. package/dist/src/network/index.d.ts +0 -5
  53. package/dist/src/network/index.d.ts.map +0 -1
  54. package/dist/src/network/index.js +0 -4
  55. package/dist/src/network/interfaces/network-status.enum.d.ts +0 -7
  56. package/dist/src/network/interfaces/network-status.enum.d.ts.map +0 -1
  57. package/dist/src/network/interfaces/network-status.enum.js +0 -7
  58. package/dist/src/network/interfaces/network.interface.d.ts +0 -18
  59. package/dist/src/network/interfaces/network.interface.d.ts.map +0 -1
  60. package/dist/src/network/interfaces/network.interface.js +0 -1
  61. package/dist/src/network/o-network.d.ts +0 -34
  62. package/dist/src/network/o-network.d.ts.map +0 -1
  63. package/dist/src/network/o-network.js +0 -247
  64. package/dist/test/utils/default.network.d.ts +0 -3
  65. package/dist/test/utils/default.network.d.ts.map +0 -1
  66. package/dist/test/utils/default.network.js +0 -33
@@ -1,247 +0,0 @@
1
- import { NetworkStatus } from './interfaces/network-status.enum.js';
2
- import touch from 'touch';
3
- import { readFile } from 'fs/promises';
4
- import { oLeaderNode } from '@olane/o-leader';
5
- import { Logger, oAddress } from '@olane/o-core';
6
- import { NodeType } from '@olane/o-core';
7
- import { initCommonTools } from '@olane/o-tools-common';
8
- import { initRegistryTools } from '@olane/o-tool-registry';
9
- import { ConfigManager } from '../utils/config.js';
10
- import { oLaneTool } from '@olane/o-lane';
11
- import { oNodeAddress } from '@olane/o-node';
12
- export class oNetwork {
13
- constructor(config) {
14
- this.leaders = []; // clones of leader for scale
15
- this.nodes = []; // clones of node for scale
16
- this.rootLeader = null; // the root leader node
17
- this.roundRobinIndex = 0;
18
- this.inFlightRequests = [];
19
- this.logger = new Logger('oNetwork');
20
- this.config = config;
21
- }
22
- entryNode() {
23
- const node = this.nodes[this.roundRobinIndex];
24
- this.roundRobinIndex = (this.roundRobinIndex + 1) % this.nodes.length;
25
- return node;
26
- }
27
- addLeader(leader) {
28
- this.leaders.push(leader);
29
- if (!this.rootLeader) {
30
- this.rootLeader = leader;
31
- }
32
- }
33
- async addNode(node) {
34
- if (this.status !== NetworkStatus.RUNNING) {
35
- throw new Error('Network is not running, cannot add node');
36
- }
37
- // TODO: how do we handle multiple leaders?
38
- // TODO: how do we handle multiple nodes?
39
- if (this.nodes.length === 0) {
40
- throw new Error('No non-leader nodes found, cannot add node');
41
- }
42
- const selection = this.entryNode();
43
- selection.addChildNode(node);
44
- await selection.start();
45
- const parent = this.entryNode();
46
- parent.addChildNode(node);
47
- }
48
- async loadConfig() {
49
- // TODO make this a promise based function
50
- if (this.config.configFilePath) {
51
- try {
52
- await touch(this.config.configFilePath);
53
- this.logger.debug('Config file path: ' + this.config.configFilePath);
54
- // let's load the config
55
- const config = await readFile(this.config.configFilePath, 'utf8');
56
- this.logger.debug('Config file contents: ' + config);
57
- if (config?.length === 0) {
58
- // no contents, let's create a new config
59
- throw new Error('No config file found, cannot start network');
60
- }
61
- const json = JSON.parse(config);
62
- this.logger.debug('Config file parsed: ' + json);
63
- const networkConfig = json.oNetworkConfig;
64
- this.config = {
65
- ...networkConfig,
66
- nodes: (networkConfig.nodes || []).map((node) => ({
67
- ...node,
68
- address: new oNodeAddress(node.address.value),
69
- })),
70
- network: {
71
- ...networkConfig.network,
72
- port: networkConfig.network?.port || 4999,
73
- },
74
- configFilePath: this.config.configFilePath, // persist the config file path
75
- };
76
- }
77
- catch (error) {
78
- this.logger.error('Failed to initialize config folder', error);
79
- throw new Error('Failed to initialize config folder');
80
- }
81
- }
82
- else {
83
- this.logger.warn('No config file path provided, using default config');
84
- }
85
- }
86
- async getNetworkName() {
87
- if (this.config.configFilePath) {
88
- const networkConfig = await ConfigManager.getNetworkConfigFromPath(this.config.configFilePath);
89
- return networkConfig?.name;
90
- }
91
- }
92
- async startNodes(type) {
93
- this.logger.debug('Starting nodes');
94
- if (!this.config.nodes || this.config.nodes?.length === 0) {
95
- throw new Error('No nodes found in config, cannot start network');
96
- }
97
- const filtered = this.config.nodes.filter((node) => node.type === type);
98
- const networkName = await this.getNetworkName();
99
- // check the config for any leaders
100
- for (const node of filtered) {
101
- if (node.type === NodeType.LEADER) {
102
- this.logger.debug('Starting leader: ' + node.address.toString());
103
- const leaderNode = new oLeaderNode({
104
- ...node,
105
- networkName: networkName,
106
- });
107
- if (!this.rootLeader) {
108
- this.rootLeader = leaderNode;
109
- }
110
- await leaderNode.start();
111
- await initCommonTools(leaderNode);
112
- this.leaders.push(leaderNode);
113
- }
114
- else {
115
- this.logger.debug('Starting non-leader node: ' + node.address.toString());
116
- const commonNode = new oLaneTool({
117
- ...node,
118
- address: node.address,
119
- leader: this.rootLeader?.address || null,
120
- parent: this.rootLeader?.address || null,
121
- });
122
- await commonNode.start();
123
- this.rootLeader?.addChildNode(commonNode);
124
- await initCommonTools(commonNode);
125
- await initRegistryTools(commonNode);
126
- this.nodes.push(commonNode);
127
- }
128
- }
129
- }
130
- async runSavedPlans() {
131
- const plans = Array.from(new Set(this.config?.lanes || []));
132
- for (const plan of plans) {
133
- this.logger.debug('Running saved plan: ' + plan);
134
- await this.use(new oAddress(plan), {
135
- method: 'use',
136
- });
137
- }
138
- }
139
- async use(oAddress, params) {
140
- const entryNode = this.entryNode();
141
- if (!entryNode) {
142
- throw new Error('Entry node not found');
143
- }
144
- this.logger.debug('Using address: ' + oAddress.toString());
145
- return entryNode.use(oAddress, params);
146
- // TODO: experiment with this (the wrong way to enter the network)
147
- // const leader = this.leaders.find(
148
- // (leader) => leader.type === NodeType.LEADER,
149
- // );
150
- // if (!leader) {
151
- // throw new Error('Leader not found');
152
- // }
153
- // return leader.use(oAddress, params);
154
- }
155
- async start() {
156
- this.logger.debug('Starting o-network');
157
- this.status = NetworkStatus.STARTING;
158
- // initialize config folder
159
- await this.loadConfig();
160
- this.logger.debug('o-network config loaded');
161
- // start leaders (and consequentially, the rest of the network)
162
- await this.startNodes(NodeType.LEADER);
163
- this.logger.debug('Leaders started...');
164
- await this.startNodes(NodeType.NODE);
165
- this.logger.debug('Nodes started...');
166
- await this.runSavedPlans();
167
- this.logger.debug('Saved plans run...');
168
- this.logger.debug('o-network started...');
169
- // index the network
170
- if (!this.config.noIndexNetwork) {
171
- await this.use(oAddress.leader(), {
172
- method: 'index_network',
173
- params: {},
174
- });
175
- }
176
- this.status = NetworkStatus.RUNNING;
177
- return {
178
- peerId: this.rootLeader?.peerId.toString() || '',
179
- transports: this.rootLeader?.transports || [],
180
- };
181
- }
182
- async stop() {
183
- this.logger.debug('Stopping o-network');
184
- this.status = NetworkStatus.STOPPING;
185
- const stopPromises = [];
186
- try {
187
- // Stop all common nodes first
188
- if (this.nodes.length > 0) {
189
- stopPromises.push(Promise.allSettled(this.nodes.map(async (node) => {
190
- try {
191
- await node.stop();
192
- this.logger.debug(`Stopped node: ${node.address.toString()}`);
193
- }
194
- catch (error) {
195
- this.logger.error(`Error stopping node ${node.address.toString()}:`, error);
196
- }
197
- })).then(() => {
198
- this.nodes = [];
199
- this.logger.debug('All common nodes stopped');
200
- }));
201
- }
202
- // Stop all leader nodes
203
- if (this.leaders.length > 0) {
204
- stopPromises.push(Promise.allSettled(this.leaders.map(async (leader) => {
205
- try {
206
- await leader.stop();
207
- this.logger.debug(`Stopped leader: ${leader.address.toString()}`);
208
- }
209
- catch (error) {
210
- this.logger.error(`Error stopping leader ${leader.address.toString()}:`, error);
211
- }
212
- })).then(() => {
213
- this.leaders = [];
214
- this.logger.debug('All leader nodes stopped');
215
- }));
216
- }
217
- // Stop root leader last
218
- if (this.rootLeader) {
219
- this.logger.debug('Stopping root leader...');
220
- stopPromises.push(this.rootLeader
221
- .stop()
222
- .then(() => {
223
- this.logger.debug('Root leader stopped');
224
- this.rootLeader = null;
225
- })
226
- .catch((error) => {
227
- this.logger.error('Error stopping root leader:', error);
228
- }));
229
- }
230
- // Wait for all stop operations to complete
231
- await Promise.all(stopPromises);
232
- this.logger.debug('o-network stopped successfully');
233
- }
234
- catch (error) {
235
- this.logger.error('Error while stopping o-network:', error);
236
- }
237
- finally {
238
- this.status = NetworkStatus.STOPPED;
239
- this.roundRobinIndex = 0;
240
- }
241
- }
242
- async restart() {
243
- this.logger.debug('Restarting o-network');
244
- await this.stop();
245
- await this.start();
246
- }
247
- }
@@ -1,3 +0,0 @@
1
- import { oNetwork } from '../../src/index.js';
2
- export declare const defaultNetwork: oNetwork;
3
- //# sourceMappingURL=default.network.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"default.network.d.ts","sourceRoot":"","sources":["../../../test/utils/default.network.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,eAAO,MAAM,cAAc,UAoBzB,CAAC"}
@@ -1,33 +0,0 @@
1
- import { NodeType, oAddress, setupGracefulShutdown } from '@olane/o-core';
2
- import { oNetwork } from '../../src/index.js';
3
- export const defaultNetwork = new oNetwork({
4
- // configFilePath: path.join(os.homedir(), '.olane', 'config.json'),
5
- nodes: [
6
- {
7
- type: NodeType.LEADER,
8
- address: new oAddress('o://leader'),
9
- description: 'Leader for testing',
10
- leader: null,
11
- parent: null,
12
- },
13
- {
14
- type: NodeType.NODE,
15
- address: new oAddress('o://node'),
16
- description: 'Node for testing',
17
- leader: null,
18
- parent: null,
19
- },
20
- ],
21
- lanes: [],
22
- // noIndexNetwork: true,
23
- });
24
- setupGracefulShutdown(async () => {
25
- console.log('Stopping o-network...');
26
- await defaultNetwork.stop();
27
- console.log('o-network stopped successfully');
28
- }, {
29
- timeout: 30000, // 30 seconds timeout
30
- onTimeout: () => {
31
- console.error('Shutdown timeout reached, forcing exit');
32
- },
33
- });