@open1s/jsbos 1.0.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.
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/brainos.js +515 -0
- package/index.d.ts +238 -0
- package/index.js +598 -0
- package/jsbos.darwin-arm64.node +0 -0
- package/package.json +119 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 N-API for Rust
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# `@napi-rs/package-template`
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
> Template project for writing node packages with napi-rs.
|
|
6
|
+
|
|
7
|
+
# Usage
|
|
8
|
+
|
|
9
|
+
1. Click **Use this template**.
|
|
10
|
+
2. **Clone** your project.
|
|
11
|
+
3. Run `yarn install` to install dependencies.
|
|
12
|
+
4. Run `yarn napi rename -n [@your-scope/package-name] -b [binary-name]` command under the project folder to rename your package.
|
|
13
|
+
|
|
14
|
+
## Install this test package
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
yarn add @napi-rs/package-template
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Ability
|
|
21
|
+
|
|
22
|
+
### Build
|
|
23
|
+
|
|
24
|
+
After `yarn build/npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).
|
|
25
|
+
|
|
26
|
+
### Test
|
|
27
|
+
|
|
28
|
+
With [ava](https://github.com/avajs/ava), run `yarn test/npm run test` to testing native addon. You can also switch to another testing framework if you want.
|
|
29
|
+
|
|
30
|
+
### CI
|
|
31
|
+
|
|
32
|
+
With GitHub Actions, each commit and pull request will be built and tested automatically in [`node@20`, `@node22`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.
|
|
33
|
+
|
|
34
|
+
### Release
|
|
35
|
+
|
|
36
|
+
Release native package is very difficult in old days. Native packages may ask developers who use it to install `build toolchain` like `gcc/llvm`, `node-gyp` or something more.
|
|
37
|
+
|
|
38
|
+
With `GitHub actions`, we can easily prebuild a `binary` for major platforms. And with `N-API`, we should never be afraid of **ABI Compatible**.
|
|
39
|
+
|
|
40
|
+
The other problem is how to deliver prebuild `binary` to users. Downloading it in `postinstall` script is a common way that most packages do it right now. The problem with this solution is it introduced many other packages to download binary that has not been used by `runtime codes`. The other problem is some users may not easily download the binary from `GitHub/CDN` if they are behind a private network (But in most cases, they have a private NPM mirror).
|
|
41
|
+
|
|
42
|
+
In this package, we choose a better way to solve this problem. We release different `npm packages` for different platforms. And add it to `optionalDependencies` before releasing the `Major` package to npm.
|
|
43
|
+
|
|
44
|
+
`NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/package-template` to see how it works.
|
|
45
|
+
|
|
46
|
+
## Develop requirements
|
|
47
|
+
|
|
48
|
+
- Install the latest `Rust`
|
|
49
|
+
- Install `Node.js@10+` which fully supported `Node-API`
|
|
50
|
+
- Install `yarn@1.x`
|
|
51
|
+
|
|
52
|
+
## Test in local
|
|
53
|
+
|
|
54
|
+
- yarn
|
|
55
|
+
- yarn build
|
|
56
|
+
- yarn test
|
|
57
|
+
|
|
58
|
+
And you will see:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
$ ava --verbose
|
|
62
|
+
|
|
63
|
+
✔ sync function from native code
|
|
64
|
+
✔ sleep function from native code (201ms)
|
|
65
|
+
─
|
|
66
|
+
|
|
67
|
+
2 tests passed
|
|
68
|
+
✨ Done in 1.12s.
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Release package
|
|
72
|
+
|
|
73
|
+
Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting.
|
|
74
|
+
|
|
75
|
+
In `Settings -> Secrets`, add **NPM_TOKEN** into it.
|
|
76
|
+
|
|
77
|
+
When you want to release the package:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
|
|
81
|
+
|
|
82
|
+
git push
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
GitHub actions will do the rest job for you.
|
|
86
|
+
|
|
87
|
+
> WARN: Don't run `npm publish` manually.
|
package/brainos.js
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* brainos-js — Elegant JavaScript API for BrainOS Agent Framework
|
|
3
|
+
*
|
|
4
|
+
* A high-level wrapper around jsbos that provides:
|
|
5
|
+
* - Async context manager for lifecycle management
|
|
6
|
+
* - @tool() decorator for simple tool registration
|
|
7
|
+
* - Fluent agent creation with chainable config
|
|
8
|
+
* - Minimal boilerplate
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* import { BrainOS, tool } from './brainos.js';
|
|
12
|
+
*
|
|
13
|
+
* const brain = new BrainOS();
|
|
14
|
+
* await brain.start();
|
|
15
|
+
* const agent = brain.agent('assistant');
|
|
16
|
+
* const result = await agent.ask('What is 42 + 58?');
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
Bus,
|
|
21
|
+
ConfigLoader,
|
|
22
|
+
Query,
|
|
23
|
+
Queryable,
|
|
24
|
+
Caller,
|
|
25
|
+
Callable,
|
|
26
|
+
Publisher,
|
|
27
|
+
Subscriber,
|
|
28
|
+
McpClient,
|
|
29
|
+
version: getVersion,
|
|
30
|
+
initTracing,
|
|
31
|
+
logTestMessage,
|
|
32
|
+
HookEvent,
|
|
33
|
+
HookDecision,
|
|
34
|
+
HookContextData,
|
|
35
|
+
HookRegistry,
|
|
36
|
+
} = require('./index.js');
|
|
37
|
+
|
|
38
|
+
class ToolDef {
|
|
39
|
+
constructor(name, description, callback, parameters = {}, schema = {}) {
|
|
40
|
+
this.name = name;
|
|
41
|
+
this.description = description;
|
|
42
|
+
this.callback = callback;
|
|
43
|
+
this.parameters = parameters;
|
|
44
|
+
this.schema = schema;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function tool(description, options = {}) {
|
|
49
|
+
return function (target, propertyKey, descriptor) {
|
|
50
|
+
const originalMethod = descriptor.value;
|
|
51
|
+
const toolName = options.name || propertyKey;
|
|
52
|
+
|
|
53
|
+
const schema = options.schema || {};
|
|
54
|
+
const properties = schema.properties || {};
|
|
55
|
+
|
|
56
|
+
const wrapper = function (args) {
|
|
57
|
+
const params = {};
|
|
58
|
+
for (const [key, spec] of Object.entries(properties)) {
|
|
59
|
+
params[key] = args[key] !== undefined ? args[key] : spec.default;
|
|
60
|
+
}
|
|
61
|
+
return originalMethod(params);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const toolDef = new ToolDef(
|
|
65
|
+
toolName,
|
|
66
|
+
description,
|
|
67
|
+
wrapper,
|
|
68
|
+
properties,
|
|
69
|
+
schema
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
descriptor.value.toolDef = toolDef;
|
|
73
|
+
descriptor.value.toolName = toolName;
|
|
74
|
+
|
|
75
|
+
return descriptor;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class Agent {
|
|
80
|
+
constructor(bus, options = {}) {
|
|
81
|
+
this._bus = bus;
|
|
82
|
+
this._inner = null;
|
|
83
|
+
this._tools = [];
|
|
84
|
+
this._hooks = [];
|
|
85
|
+
this._config = {
|
|
86
|
+
name: options.name || 'assistant',
|
|
87
|
+
model: options.model || 'nvidia/meta/llama-3.1-8b-instruct',
|
|
88
|
+
baseUrl: options.baseUrl || 'https://integrate.api.nvidia.com/v1',
|
|
89
|
+
apiKey: options.apiKey,
|
|
90
|
+
systemPrompt: options.systemPrompt || 'You are a helpful assistant.',
|
|
91
|
+
temperature: options.temperature ?? 0.7,
|
|
92
|
+
timeoutSecs: options.timeoutSecs || 120,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
withModel(model) {
|
|
97
|
+
this._config.model = model;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
withPrompt(prompt) {
|
|
102
|
+
this._config.systemPrompt = prompt;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
withTemperature(temp) {
|
|
107
|
+
this._config.temperature = temp;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
withTimeout(secs) {
|
|
112
|
+
this._config.timeoutSecs = secs;
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
withTools(...tools) {
|
|
117
|
+
for (const t of tools) {
|
|
118
|
+
this._tools.push(t);
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
withBashTool(name = 'bash', workspaceRoot = null) {
|
|
124
|
+
this._config._bashTool = { name, workspaceRoot };
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async start() {
|
|
129
|
+
const { Agent: RawAgent } = require('./jsbos.js');
|
|
130
|
+
this._inner = await RawAgent.create(this._config);
|
|
131
|
+
for (const t of this._tools) {
|
|
132
|
+
const schema = JSON.stringify(t.schema);
|
|
133
|
+
const params = JSON.stringify(t.parameters);
|
|
134
|
+
await this._inner.addTool(
|
|
135
|
+
t.name,
|
|
136
|
+
t.description,
|
|
137
|
+
params,
|
|
138
|
+
schema,
|
|
139
|
+
(err, args) => t.callback(args)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
if (this._config._bashTool) {
|
|
143
|
+
await this._inner.addBashTool(
|
|
144
|
+
this._config._bashTool.name,
|
|
145
|
+
this._config._bashTool.workspaceRoot
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
for (const h of this._hooks) {
|
|
149
|
+
this._inner.registerHook(h.event, h.callback);
|
|
150
|
+
}
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async ask(question) {
|
|
155
|
+
if (!this._inner) await this.start();
|
|
156
|
+
return this._inner.runSimple(question);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async chat(message) {
|
|
160
|
+
return this.ask(message);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async runSimple(message) {
|
|
164
|
+
if (!this._inner) await this.start();
|
|
165
|
+
return this._inner.runSimple(message);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async react(task) {
|
|
169
|
+
if (!this._inner) await this.start();
|
|
170
|
+
return this._inner.react(task);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
stream(task, onToken) {
|
|
174
|
+
if (!this._inner) throw new Error('Agent not started. Call start() first.');
|
|
175
|
+
return this._inner.stream(task, (err, token) => {
|
|
176
|
+
if (err) {
|
|
177
|
+
onToken({ type: 'Error', error: err.message || String(err) });
|
|
178
|
+
} else {
|
|
179
|
+
onToken(token);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async streamCollect(task) {
|
|
185
|
+
const tokens = [];
|
|
186
|
+
await new Promise((resolve, reject) => {
|
|
187
|
+
this.stream(task, (token) => {
|
|
188
|
+
tokens.push(token);
|
|
189
|
+
if (token.type === 'Done' || token.type === 'Error') {
|
|
190
|
+
if (token.type === 'Error') reject(new Error(token.error));
|
|
191
|
+
else resolve();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
return tokens;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
get tools() {
|
|
199
|
+
if (!this._inner) return this._tools.map(t => t.name);
|
|
200
|
+
return this._inner.listTools();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
get config() {
|
|
204
|
+
return { ...this._config };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
class BusManager {
|
|
209
|
+
constructor(options = {}) {
|
|
210
|
+
this._mode = options.mode || 'peer';
|
|
211
|
+
this._connect = options.connect;
|
|
212
|
+
this._listen = options.listen;
|
|
213
|
+
this._peer = options.peer;
|
|
214
|
+
this._bus = null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
static async create(options = {}) {
|
|
218
|
+
const manager = new BusManager(options);
|
|
219
|
+
return manager.start();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async start() {
|
|
223
|
+
const { Bus: RawBus } = require('./jsbos.js');
|
|
224
|
+
const cfg = {
|
|
225
|
+
mode: this._mode,
|
|
226
|
+
connect: this._connect,
|
|
227
|
+
listen: this._listen,
|
|
228
|
+
peer: this._peer,
|
|
229
|
+
};
|
|
230
|
+
this._bus = await RawBus.create(cfg);
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async stop() {
|
|
235
|
+
this._bus = null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async publishText(topic, payload) {
|
|
239
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
240
|
+
await this._bus.publishText(topic, payload);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async publishJson(topic, data) {
|
|
244
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
245
|
+
await this._bus.publishJson(topic, data);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async createPublisher(topic) {
|
|
249
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
250
|
+
const raw = await this._bus.createPublisher(topic);
|
|
251
|
+
return new PublisherWrapper(raw);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async createSubscriber(topic) {
|
|
255
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
256
|
+
const raw = await this._bus.createSubscriber(topic);
|
|
257
|
+
return new SubscriberWrapper(raw);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async createQuery(topic) {
|
|
261
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
262
|
+
const raw = await this._bus.createQuery(topic);
|
|
263
|
+
return new QueryClient(raw);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async createQueryable(topic, handler = null) {
|
|
267
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
268
|
+
const raw = await this._bus.createQueryable(topic);
|
|
269
|
+
if (handler) raw.setHandler(handler);
|
|
270
|
+
return new QueryableServer(raw);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async createCaller(name) {
|
|
274
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
275
|
+
const raw = await this._bus.createCaller(name);
|
|
276
|
+
return new CallerClient(raw);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async createCallable(uri, handler = null) {
|
|
280
|
+
if (!this._bus) throw new Error('Bus not started');
|
|
281
|
+
const raw = await this._bus.createCallable(uri);
|
|
282
|
+
if (handler) raw.setHandler(handler);
|
|
283
|
+
return new CallableServer(raw);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
get bus() {
|
|
287
|
+
if (!this._bus) throw new Error('Bus not started. Call start() first.');
|
|
288
|
+
return this._bus;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
class PublisherWrapper {
|
|
293
|
+
constructor(inner) {
|
|
294
|
+
this._inner = inner;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
get topic() {
|
|
298
|
+
return this._inner.topic;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async publishText(payload) {
|
|
302
|
+
await this._inner.publishText(payload);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async publishJson(data) {
|
|
306
|
+
await this._inner.publishJson(data);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
class SubscriberWrapper {
|
|
311
|
+
constructor(inner) {
|
|
312
|
+
this._inner = inner;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
get topic() {
|
|
316
|
+
return this._inner.topic;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async recv() {
|
|
320
|
+
return this._inner.recv();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async recvWithTimeoutMs(timeoutMs) {
|
|
324
|
+
return this._inner.recvWithTimeoutMs(timeoutMs);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async recvJsonWithTimeoutMs(timeoutMs) {
|
|
328
|
+
return this._inner.recvJsonWithTimeoutMs(timeoutMs);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async run(callback) {
|
|
332
|
+
await this._inner.run(callback);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async runJson(callback) {
|
|
336
|
+
await this._inner.runJson(callback);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
[Symbol.asyncIterator]() {
|
|
340
|
+
return this;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async next() {
|
|
344
|
+
const msg = await this.recv();
|
|
345
|
+
if (msg === null) return { done: true };
|
|
346
|
+
return { done: false, value: msg };
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
class QueryClient {
|
|
351
|
+
constructor(inner) {
|
|
352
|
+
this._inner = inner;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
get topic() {
|
|
356
|
+
return this._inner.topic;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async queryText(payload) {
|
|
360
|
+
return this._inner.queryText(payload);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async queryTextTimeoutMs(payload, timeoutMs) {
|
|
364
|
+
return this._inner.queryTextTimeoutMs(payload, timeoutMs);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
class QueryableServer {
|
|
369
|
+
constructor(inner) {
|
|
370
|
+
this._inner = inner;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
setHandler(handler) {
|
|
374
|
+
this._inner.setHandler(handler);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async start() {
|
|
378
|
+
await this._inner.start();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async run(handler) {
|
|
382
|
+
await this._inner.run(handler);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async runJson(handler) {
|
|
386
|
+
await this._inner.runJson(handler);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
class CallerClient {
|
|
391
|
+
constructor(inner) {
|
|
392
|
+
this._inner = inner;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async callText(payload) {
|
|
396
|
+
return this._inner.callText(payload);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
class CallableServer {
|
|
401
|
+
constructor(inner) {
|
|
402
|
+
this._inner = inner;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
setHandler(handler) {
|
|
406
|
+
this._inner.setHandler(handler);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
get isStarted() {
|
|
410
|
+
return this._inner.isStarted();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async start() {
|
|
414
|
+
await this._inner.start();
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async run(handler) {
|
|
418
|
+
await this._inner.run(handler);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
async runJson(handler) {
|
|
422
|
+
await this._inner.runJson(handler);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
class BrainOS {
|
|
427
|
+
constructor(options = {}) {
|
|
428
|
+
this._options = options;
|
|
429
|
+
this._bus = null;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async start() {
|
|
433
|
+
const loader = new ConfigLoader();
|
|
434
|
+
loader.discover();
|
|
435
|
+
const config = JSON.parse(loader.loadSync());
|
|
436
|
+
|
|
437
|
+
const globalModel = config.global_model || {};
|
|
438
|
+
this._apiKey = this._options.apiKey || globalModel.api_key;
|
|
439
|
+
this._baseUrl = this._options.baseUrl || globalModel.base_url || 'https://integrate.api.nvidia.com/v1';
|
|
440
|
+
this._model = this._options.model || globalModel.model || 'nvidia/meta/llama-3.1-8b-instruct';
|
|
441
|
+
|
|
442
|
+
this._bus = await Bus.create();
|
|
443
|
+
return this;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async stop() {
|
|
447
|
+
this._bus = null;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
agent(name = 'assistant', options = {}) {
|
|
451
|
+
if (!this._bus) {
|
|
452
|
+
throw new Error('BrainOS not started. Call start() first.');
|
|
453
|
+
}
|
|
454
|
+
return new Agent(this._bus, {
|
|
455
|
+
name,
|
|
456
|
+
model: options.model || this._model,
|
|
457
|
+
baseUrl: options.baseUrl || this._baseUrl,
|
|
458
|
+
apiKey: options.apiKey || this._apiKey,
|
|
459
|
+
systemPrompt: options.systemPrompt || 'You are a helpful assistant.',
|
|
460
|
+
temperature: options.temperature ?? 0.7,
|
|
461
|
+
timeoutSecs: options.timeoutSecs || 120,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
get bus() {
|
|
466
|
+
if (!this._bus) {
|
|
467
|
+
throw new Error('BrainOS not started. Call start() first.');
|
|
468
|
+
}
|
|
469
|
+
return this._bus;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
module.exports = {
|
|
474
|
+
BrainOS,
|
|
475
|
+
Agent,
|
|
476
|
+
tool,
|
|
477
|
+
ToolDef,
|
|
478
|
+
BusManager,
|
|
479
|
+
Publisher: PublisherWrapper,
|
|
480
|
+
Subscriber: SubscriberWrapper,
|
|
481
|
+
Query: QueryClient,
|
|
482
|
+
Queryable: QueryableServer,
|
|
483
|
+
Caller: CallerClient,
|
|
484
|
+
Callable: CallableServer,
|
|
485
|
+
McpClient,
|
|
486
|
+
version: getVersion,
|
|
487
|
+
initTracing,
|
|
488
|
+
logTestMessage,
|
|
489
|
+
HookEvent,
|
|
490
|
+
HookDecision,
|
|
491
|
+
HookContextData,
|
|
492
|
+
HookRegistry,
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
export {
|
|
496
|
+
BrainOS,
|
|
497
|
+
Agent,
|
|
498
|
+
tool,
|
|
499
|
+
ToolDef,
|
|
500
|
+
BusManager,
|
|
501
|
+
PublisherWrapper as Publisher,
|
|
502
|
+
SubscriberWrapper as Subscriber,
|
|
503
|
+
QueryClient as Query,
|
|
504
|
+
QueryableServer as Queryable,
|
|
505
|
+
CallerClient as Caller,
|
|
506
|
+
CallableServer as Callable,
|
|
507
|
+
McpClient,
|
|
508
|
+
getVersion as version,
|
|
509
|
+
initTracing,
|
|
510
|
+
logTestMessage,
|
|
511
|
+
HookEvent,
|
|
512
|
+
HookDecision,
|
|
513
|
+
HookContextData,
|
|
514
|
+
HookRegistry,
|
|
515
|
+
};
|