@miruzero/zero-agent 0.3.5 → 0.3.7
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/README.md +31 -22
- package/package.json +6 -3
- package/src/agent.js +193 -158
- package/src/args.js +1 -3
- package/src/autostart.js +44 -68
- package/src/capabilities.js +98 -104
- package/src/chatgpt/auth/index.js +114 -0
- package/src/chatgpt/config/paths.js +25 -0
- package/src/chatgpt/index.js +5 -0
- package/src/chatgpt/session/direct-transport.js +68 -0
- package/src/chatgpt/session/exchange.js +17 -0
- package/src/chatgpt/session/index.js +102 -0
- package/src/chatgpt/session/local-storage.js +106 -0
- package/src/chatgpt/transport/direct/http-client.js +191 -0
- package/src/chatgpt-auth.js +27 -0
- package/src/chatgpt-runtime.js +8 -0
- package/src/chatgpt-session-exchange.js +8 -0
- package/src/chatgpt-session.js +26 -0
- package/src/cli.js +108 -71
- package/src/command/exec.js +34 -0
- package/src/command/filesystem/append-file.js +7 -12
- package/src/command/filesystem/copy-directory.js +8 -12
- package/src/command/filesystem/copy-file.js +8 -13
- package/src/command/filesystem/create-directory.js +7 -11
- package/src/command/filesystem/create-file.js +8 -13
- package/src/command/filesystem/delete-directory.js +7 -11
- package/src/command/filesystem/delete-file.js +8 -12
- package/src/command/filesystem/exists.js +6 -10
- package/src/command/filesystem/get-file-info.js +7 -12
- package/src/command/filesystem/glob-files.js +8 -13
- package/src/command/filesystem/grep-files.js +9 -15
- package/src/command/filesystem/hash-file.js +6 -11
- package/src/command/filesystem/index.js +33 -34
- package/src/command/filesystem/list-directory.js +6 -10
- package/src/command/filesystem/move-directory.js +9 -14
- package/src/command/filesystem/move-file.js +8 -13
- package/src/command/filesystem/path-policy.js +33 -16
- package/src/command/filesystem/read-file.js +7 -11
- package/src/command/filesystem/read-files.js +5 -8
- package/src/command/filesystem/rename-file.js +5 -8
- package/src/command/filesystem/replace-file.js +7 -12
- package/src/command/filesystem/text.js +5 -7
- package/src/command/filesystem/touch-file.js +8 -13
- package/src/command/filesystem/truncate-file.js +6 -10
- package/src/command/filesystem/walk.js +36 -39
- package/src/command/filesystem/write-file.js +8 -13
- package/src/command/index.js +19 -24
- package/src/command/path/index.js +46 -53
- package/src/config.js +21 -33
- package/src/device-paths.js +53 -0
- package/src/enrollment-client.js +19 -0
- package/src/http.js +27 -2
- package/src/identity-state.js +27 -0
- package/src/index.js +2 -4
- package/src/instance.js +85 -0
- package/src/local-server.js +174 -0
- package/src/local-tools.js +205 -205
- package/src/shared-zero/catalog.js +33 -0
- package/src/shared-zero/contract.js +18 -0
- package/src/shared-zero/descriptor.js +25 -0
- package/src/shared-zero/errors.js +12 -0
- package/src/shared-zero/index.js +9 -0
- package/src/shared-zero/runtime.js +14 -0
- package/src/shared-zero/target.js +19 -0
- package/src/shared-zero/tool-id.js +26 -0
- package/src/storage-roots.js +21 -0
- package/src/zero-runtime.js +1 -11
- package/src/zero.js +78 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const zero = require('./index');
|
|
4
|
+
const fail = (code, message) => Object.assign(new Error(message), { code });
|
|
5
|
+
|
|
6
|
+
exports.create = (items = []) => {
|
|
7
|
+
const canonical = new Map();
|
|
8
|
+
const aliases = new Map();
|
|
9
|
+
for (const item of items) {
|
|
10
|
+
const normalized = zero.descriptor.validate(item);
|
|
11
|
+
if (canonical.has(normalized.name)) throw fail('DUPLICATE_TOOL_ID', 'duplicate tool id: ' + normalized.name);
|
|
12
|
+
canonical.set(normalized.name, normalized);
|
|
13
|
+
}
|
|
14
|
+
for (const item of canonical.values()) {
|
|
15
|
+
for (const alias of item.aliases) {
|
|
16
|
+
if (canonical.has(alias) || aliases.has(alias)) throw fail('DUPLICATE_TOOL_ALIAS', 'duplicate tool alias: ' + alias);
|
|
17
|
+
aliases.set(alias, item.name);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const resolve = (name) => {
|
|
21
|
+
const canonicalName = canonical.has(name) ? name : aliases.get(name);
|
|
22
|
+
if (!canonicalName) throw fail('UNKNOWN_TOOL', 'unknown Zero tool: ' + name);
|
|
23
|
+
return canonical.get(canonicalName);
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
resolve,
|
|
27
|
+
get: (name) => canonical.get(name) || null,
|
|
28
|
+
list: ({ provider, category } = {}) => [...canonical.values()]
|
|
29
|
+
.filter((item) => !provider || item.provider === provider)
|
|
30
|
+
.filter((item) => !category || item.category === category)
|
|
31
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const VERSION = '1.0.0';
|
|
4
|
+
const versionMajor = (value) => {
|
|
5
|
+
const match = /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(String(value || ''));
|
|
6
|
+
return match ? Number(match[1]) : null;
|
|
7
|
+
};
|
|
8
|
+
const mismatch = (actual) => Object.assign(
|
|
9
|
+
new Error(`Zero contract major mismatch: expected ${VERSION}, got ${actual}`),
|
|
10
|
+
{ code: 'CONTRACT_VERSION_MISMATCH', expected: VERSION, actual }
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
exports.version = VERSION;
|
|
14
|
+
exports.isCompatible = (other) => versionMajor(other) === versionMajor(VERSION);
|
|
15
|
+
exports.assertCompatible = (other) => {
|
|
16
|
+
if (!exports.isCompatible(other)) throw mismatch(other);
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const zero = require('./index');
|
|
4
|
+
const EXECUTION = new Set(['core', 'target', 'either']);
|
|
5
|
+
const bad = (code, message) => Object.assign(new Error(message), { code });
|
|
6
|
+
|
|
7
|
+
exports.validate = (input) => {
|
|
8
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
9
|
+
throw bad('INVALID_TOOL_DESCRIPTOR', 'tool descriptor must be an object');
|
|
10
|
+
}
|
|
11
|
+
const identity = zero.toolId.parse(input.name);
|
|
12
|
+
if (input.execution && !EXECUTION.has(input.execution)) {
|
|
13
|
+
throw bad('INVALID_TOOL_DESCRIPTOR', 'invalid execution mode: ' + input.execution);
|
|
14
|
+
}
|
|
15
|
+
const aliases = input.aliases === undefined ? [] : input.aliases;
|
|
16
|
+
if (!Array.isArray(aliases) || aliases.some((item) => typeof item !== 'string' || !item.trim())) {
|
|
17
|
+
throw bad('INVALID_TOOL_DESCRIPTOR', 'aliases must be non-empty strings');
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
...input,
|
|
21
|
+
...identity,
|
|
22
|
+
aliases: [...new Set(aliases)],
|
|
23
|
+
inputSchema: input.inputSchema || { type: 'object', properties: {}, additionalProperties: false }
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
exports.normalize = (error, context = {}) => ({
|
|
4
|
+
ok: false,
|
|
5
|
+
error: {
|
|
6
|
+
code: error?.code || 'ZERO_ERROR',
|
|
7
|
+
message: error?.message || String(error || 'Zero error'),
|
|
8
|
+
...(Number.isInteger(error?.status) ? { status: error.status } : {}),
|
|
9
|
+
retryable: error?.retryable === true,
|
|
10
|
+
...(context.target !== undefined ? { target: context.target } : {})
|
|
11
|
+
}
|
|
12
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
exports.contract = require('./contract');
|
|
4
|
+
exports.toolId = require('./tool-id');
|
|
5
|
+
exports.descriptor = require('./descriptor');
|
|
6
|
+
exports.catalog = require('./catalog');
|
|
7
|
+
exports.errors = require('./errors');
|
|
8
|
+
exports.runtime = require('./runtime');
|
|
9
|
+
exports.target = require('./target');
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const bad = (message) => Object.assign(new Error(message), { code: 'INVALID_RUNTIME' });
|
|
4
|
+
|
|
5
|
+
exports.create = ({ catalog, invoke } = {}) => {
|
|
6
|
+
if (!catalog || typeof catalog.list !== 'function' || typeof catalog.resolve !== 'function') {
|
|
7
|
+
throw bad('runtime requires a catalog');
|
|
8
|
+
}
|
|
9
|
+
if (typeof invoke !== 'function') throw bad('runtime requires invoke(descriptor,args,context)');
|
|
10
|
+
return {
|
|
11
|
+
list: (filter = {}) => catalog.list(filter),
|
|
12
|
+
call: (name, args = {}, context = {}) => invoke(catalog.resolve(name), args, context)
|
|
13
|
+
};
|
|
14
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const invalid = () => Object.assign(new Error('invalid Zero target'), { code: 'INVALID_TARGET' });
|
|
4
|
+
|
|
5
|
+
exports.normalize = (value) => {
|
|
6
|
+
if (value === undefined || value === null || value === '' || value === 'local' || value === 'core') {
|
|
7
|
+
return { kind: 'local', name: 'local' };
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === 'string') return { kind: 'device', name: value };
|
|
10
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) throw invalid();
|
|
11
|
+
if (value.kind === 'local') return { kind: 'local', name: 'local' };
|
|
12
|
+
if (value.device_id) return { kind: 'device', device_id: String(value.device_id) };
|
|
13
|
+
if (value.name) {
|
|
14
|
+
if (value.name === 'local' || value.name === 'core') return { kind: 'local', name: 'local' };
|
|
15
|
+
return { kind: 'device', name: String(value.name) };
|
|
16
|
+
}
|
|
17
|
+
throw invalid();
|
|
18
|
+
};
|
|
19
|
+
exports.isRemote = (value) => exports.normalize(value).kind === 'device';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SEGMENT = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
4
|
+
const invalid = (value) => Object.assign(
|
|
5
|
+
new Error('invalid Zero tool id: ' + String(value)),
|
|
6
|
+
{ code: 'INVALID_TOOL_ID' }
|
|
7
|
+
);
|
|
8
|
+
const assertSegment = (value) => {
|
|
9
|
+
if (!SEGMENT.test(String(value || ''))) throw invalid(value);
|
|
10
|
+
return String(value);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
exports.format = ({ provider, category, function: functionName } = {}) => {
|
|
14
|
+
const p = assertSegment(provider);
|
|
15
|
+
const c = assertSegment(category);
|
|
16
|
+
const f = assertSegment(functionName);
|
|
17
|
+
return `zero.${p}.${c}.${f}`;
|
|
18
|
+
};
|
|
19
|
+
exports.parse = (name) => {
|
|
20
|
+
const parts = String(name || '').split('.');
|
|
21
|
+
if (parts.length !== 4 || parts[0] !== 'zero') throw invalid(name);
|
|
22
|
+
const provider = assertSegment(parts[1]);
|
|
23
|
+
const category = assertSegment(parts[2]);
|
|
24
|
+
const functionName = assertSegment(parts[3]);
|
|
25
|
+
return { provider, category, function: functionName, name: `zero.${provider}.${category}.${functionName}` };
|
|
26
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const zero = require('./zero');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
exports.discover = (system = {}) => {
|
|
7
|
+
const platform = system.platform || process.platform;
|
|
8
|
+
const exists = system.exists || zero.fs.existsSync;
|
|
9
|
+
const candidates = platform === 'win32'
|
|
10
|
+
? Array.from({ length: 26 }, (_, index) => String.fromCharCode(65 + index) + ':\\')
|
|
11
|
+
: ['/'];
|
|
12
|
+
|
|
13
|
+
return candidates.filter((item) => {
|
|
14
|
+
try {
|
|
15
|
+
return exists(item);
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
package/src/zero-runtime.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
try {
|
|
5
|
-
return require('@miruzero/zero');
|
|
6
|
-
} catch (error) {
|
|
7
|
-
if (error?.code !== 'MODULE_NOT_FOUND' || !String(error.message).includes('@miruzero/zero')) throw error;
|
|
8
|
-
return require('../../../packages/zero');
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const zero = load();
|
|
13
|
-
Object.assign(exports, zero);
|
|
3
|
+
module.exports = require('./zero');
|
package/src/zero.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const loadShared = () => require('./shared-zero');
|
|
4
|
+
|
|
5
|
+
const mount = (name, loader) => Object.defineProperty(exports, name, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
get() {
|
|
9
|
+
const value = loader();
|
|
10
|
+
Object.defineProperty(exports, name, { enumerable: true, configurable: false, value });
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const shared = loadShared();
|
|
16
|
+
exports.contract = shared.contract;
|
|
17
|
+
exports.toolId = shared.toolId;
|
|
18
|
+
exports.descriptor = shared.descriptor;
|
|
19
|
+
exports.catalog = shared.catalog;
|
|
20
|
+
exports.errors = shared.errors;
|
|
21
|
+
exports.runtime = shared.runtime;
|
|
22
|
+
exports.target = shared.target;
|
|
23
|
+
exports.fs = require('node:fs');
|
|
24
|
+
exports.path = require('node:path');
|
|
25
|
+
exports.os = require('node:os');
|
|
26
|
+
exports.crypto = require('node:crypto');
|
|
27
|
+
exports.net = require('node:net');
|
|
28
|
+
exports.nodeHttp = require('node:http');
|
|
29
|
+
exports.childProcess = require('node:child_process');
|
|
30
|
+
exports.pkg = require('../package.json');
|
|
31
|
+
|
|
32
|
+
mount('classicLevel', () => require('classic-level'));
|
|
33
|
+
mount('args', () => require('./args'));
|
|
34
|
+
mount('config', () => require('./config'));
|
|
35
|
+
mount('autostart', () => require('./autostart'));
|
|
36
|
+
mount('capabilities', () => require('./capabilities'));
|
|
37
|
+
mount('agent', () => require('./agent'));
|
|
38
|
+
mount('instance', () => require('./instance'));
|
|
39
|
+
mount('localServer', () => require('./local-server'));
|
|
40
|
+
mount('http', () => require('./http'));
|
|
41
|
+
mount('localTools', () => require('./local-tools'));
|
|
42
|
+
mount('command', () => require('./command'));
|
|
43
|
+
mount('chatgptAuth', () => require('./chatgpt-auth'));
|
|
44
|
+
mount('chatgptSession', () => require('./chatgpt-session'));
|
|
45
|
+
mount('chatgptSessionExchange', () => require('./chatgpt-session-exchange'));
|
|
46
|
+
mount('devicePaths', () => require('./device-paths'));
|
|
47
|
+
mount('identityState', () => require('./identity-state'));
|
|
48
|
+
mount('storageRoots', () => require('./storage-roots'));
|
|
49
|
+
mount('enrollmentClient', () => require('./enrollment-client'));
|
|
50
|
+
mount('commandFilesystem', () => require('./command/filesystem'));
|
|
51
|
+
mount('commandPath', () => require('./command/path'));
|
|
52
|
+
mount('commandExec', () => require('./command/exec'));
|
|
53
|
+
mount('pathPolicy', () => require('./command/filesystem/path-policy'));
|
|
54
|
+
mount('commandText', () => require('./command/filesystem/text'));
|
|
55
|
+
mount('commandWalk', () => require('./command/filesystem/walk'));
|
|
56
|
+
mount('readFileTool', () => require('./command/filesystem/read-file'));
|
|
57
|
+
mount('moveFileTool', () => require('./command/filesystem/move-file'));
|
|
58
|
+
|
|
59
|
+
mount('appendFileTool', () => require('./command/filesystem/append-file'));
|
|
60
|
+
mount('copyDirectoryTool', () => require('./command/filesystem/copy-directory'));
|
|
61
|
+
mount('copyFileTool', () => require('./command/filesystem/copy-file'));
|
|
62
|
+
mount('createDirectoryTool', () => require('./command/filesystem/create-directory'));
|
|
63
|
+
mount('createFileTool', () => require('./command/filesystem/create-file'));
|
|
64
|
+
mount('deleteDirectoryTool', () => require('./command/filesystem/delete-directory'));
|
|
65
|
+
mount('deleteFileTool', () => require('./command/filesystem/delete-file'));
|
|
66
|
+
mount('existsTool', () => require('./command/filesystem/exists'));
|
|
67
|
+
mount('getFileInfoTool', () => require('./command/filesystem/get-file-info'));
|
|
68
|
+
mount('globFilesTool', () => require('./command/filesystem/glob-files'));
|
|
69
|
+
mount('grepFilesTool', () => require('./command/filesystem/grep-files'));
|
|
70
|
+
mount('hashFileTool', () => require('./command/filesystem/hash-file'));
|
|
71
|
+
mount('listDirectoryTool', () => require('./command/filesystem/list-directory'));
|
|
72
|
+
mount('moveDirectoryTool', () => require('./command/filesystem/move-directory'));
|
|
73
|
+
mount('readFilesTool', () => require('./command/filesystem/read-files'));
|
|
74
|
+
mount('renameFileTool', () => require('./command/filesystem/rename-file'));
|
|
75
|
+
mount('replaceFileTool', () => require('./command/filesystem/replace-file'));
|
|
76
|
+
mount('touchFileTool', () => require('./command/filesystem/touch-file'));
|
|
77
|
+
mount('truncateFileTool', () => require('./command/filesystem/truncate-file'));
|
|
78
|
+
mount('writeFileTool', () => require('./command/filesystem/write-file'));
|