aegiscode 6.7.7 → 6.8.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/README.md +35 -51
- package/bin/aegiscode.js +24 -0
- package/package.json +1 -1
- package/scripts/predist.mjs +11 -7
- package/src/app.js +7 -46
- package/src/commands.js +40 -159
- package/src/config.js +0 -10
- package/src/deps.js +5 -30
- package/src/engine.js +18 -83
- package/src/sharedpaths.js +50 -0
- package/vendor/client/env-file.js +182 -0
- package/vendor/desktop/lib/local/engine.js +225 -128
- package/vendor/desktop/lib/local/local.js +433 -0
- package/vendor/desktop/lib/local/tools.js +7 -56
- package/vendor/desktop/lib/settings.js +33 -0
- package/vendor/desktop/renderer/usage.js +4 -4
- package/src/custommodels.js +0 -198
- package/vendor/desktop/lib/local/providers.js +0 -679
package/src/deps.js
CHANGED
|
@@ -18,30 +18,12 @@
|
|
|
18
18
|
* hosts would drift.
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
const fs = require('node:fs');
|
|
22
21
|
const path = require('node:path');
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
path.join(srcDir, '..', '..'), // repo root (in-repo layout)
|
|
29
|
-
path.join(cliDir, 'vendor'), // staged tree (published layout)
|
|
30
|
-
];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Resolve a repo-relative module path against whichever root has it. */
|
|
34
|
-
function resolveShared(relPath) {
|
|
35
|
-
for (const root of roots()) {
|
|
36
|
-
const candidate = path.join(root, relPath);
|
|
37
|
-
if (fs.existsSync(candidate)) return candidate;
|
|
38
|
-
}
|
|
39
|
-
throw new Error(
|
|
40
|
-
`aegiscode: cannot find ${relPath}. Expected it beside cli/ (in the repo) or ` +
|
|
41
|
-
'under cli/vendor/ (installed package). Reinstall the package, or run ' +
|
|
42
|
-
'`node scripts/predist.mjs` from cli/ if this is a source checkout.'
|
|
43
|
-
);
|
|
44
|
-
}
|
|
23
|
+
// Where the shared modules live, WITHOUT loading any of them. Kept in its own
|
|
24
|
+
// module because this file eagerly requires everything it resolves, which
|
|
25
|
+
// makes it unusable from inside the engine's own dependency graph.
|
|
26
|
+
const { roots, resolveShared } = require('./sharedpaths.js');
|
|
45
27
|
|
|
46
28
|
const clientPath = resolveShared(path.join('client', 'aegis.js'));
|
|
47
29
|
const toolsPath = resolveShared(path.join('mcp', 'tools.js'));
|
|
@@ -81,17 +63,11 @@ const updatePath = resolveShared(path.join('client', 'update.js'));
|
|
|
81
63
|
// must work the SAME queue with the SAME rules (scoped commits included).
|
|
82
64
|
const queuePath = resolveShared(path.join('desktop', 'lib', 'local', 'queue.js'));
|
|
83
65
|
const autonomousPath = resolveShared(path.join('desktop', 'lib', 'local', 'autonomous.js'));
|
|
84
|
-
// The direct-provider transport (openaiCompatible + anthropicMessages). The
|
|
85
|
-
// desktop injects it into createLocalEngine for its custom-endpoint classes;
|
|
86
|
-
// the CLI injects the same real module for its `custom` class (the /model add
|
|
87
|
-
// catalog) rather than the throwing stub it shipped before.
|
|
88
|
-
const providersPath = resolveShared(path.join('desktop', 'lib', 'local', 'providers.js'));
|
|
89
66
|
|
|
90
67
|
const { createClient } = require(clientPath);
|
|
91
68
|
const { createTools } = require(toolsPath);
|
|
92
69
|
const { usageTokens } = require(usagePath);
|
|
93
70
|
const { createLocalEngine } = require(enginePath);
|
|
94
|
-
const providers = require(providersPath);
|
|
95
71
|
const { createSettingsStore, isReservedNamespace } = require(settingsPath);
|
|
96
72
|
const { agentRoles, agentRoleLabel } = require(agentsPath);
|
|
97
73
|
const { buildSystemPrompt } = require(promptPath);
|
|
@@ -105,7 +81,6 @@ module.exports = {
|
|
|
105
81
|
createTools,
|
|
106
82
|
usageTokens,
|
|
107
83
|
createLocalEngine,
|
|
108
|
-
providers,
|
|
109
84
|
createSettingsStore,
|
|
110
85
|
isReservedNamespace,
|
|
111
86
|
agentRoles,
|
|
@@ -114,6 +89,6 @@ module.exports = {
|
|
|
114
89
|
updater,
|
|
115
90
|
queue,
|
|
116
91
|
autonomous,
|
|
117
|
-
paths: { client: clientPath, tools: toolsPath, usage: usagePath, engine: enginePath,
|
|
92
|
+
paths: { client: clientPath, tools: toolsPath, usage: usagePath, engine: enginePath, settings: settingsPath, agents: agentsPath, prompt: promptPath, update: updatePath, queue: queuePath, autonomous: autonomousPath },
|
|
118
93
|
roots,
|
|
119
94
|
};
|
package/src/engine.js
CHANGED
|
@@ -18,11 +18,9 @@
|
|
|
18
18
|
* free ride — and why the key must never be handed to a direct provider
|
|
19
19
|
* call from here.
|
|
20
20
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* and a stub is proof that a code path meant for another host never silently
|
|
25
|
-
* runs here.
|
|
21
|
+
* Ollama stays a stub that throws if the engine ever calls it: it needs an
|
|
22
|
+
* endpoint surface this host does not offer, and a stub is proof that a code
|
|
23
|
+
* path meant for another host never silently runs here.
|
|
26
24
|
*
|
|
27
25
|
* ── the key store ───────────────────────────────────────────────────────────
|
|
28
26
|
*
|
|
@@ -37,20 +35,19 @@
|
|
|
37
35
|
*/
|
|
38
36
|
|
|
39
37
|
const os = require('node:os');
|
|
40
|
-
const { createLocalEngine, createSettingsStore
|
|
38
|
+
const { createLocalEngine, createSettingsStore } = require('./deps.js');
|
|
41
39
|
const { credentials } = require('./shared.js');
|
|
42
|
-
const custom = require('./custommodels.js');
|
|
43
40
|
const VERSION = require('../package.json').version;
|
|
44
41
|
|
|
45
42
|
/**
|
|
46
43
|
* The classes this host can actually run (see the docstring above).
|
|
47
|
-
* aegis — the pooled route (account key pays, pool picks the provider).
|
|
44
|
+
* aegis — the pooled route (account key pays, pool picks the provider; bills).
|
|
48
45
|
* byok — the BYOK relay (your provider key, AEGIS bills a handling fee).
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
46
|
+
* local — a model on hardware you own, over loopback. Bills nobody because
|
|
47
|
+
* there is no vendor; refused outright if the base URL is not local
|
|
48
|
+
* (desktop/lib/local/local.js remoteRefusal, fail-closed).
|
|
52
49
|
*/
|
|
53
|
-
const HOST_CLASSES = Object.freeze(['aegis', 'byok', '
|
|
50
|
+
const HOST_CLASSES = Object.freeze(['aegis', 'byok', 'local']);
|
|
54
51
|
|
|
55
52
|
/** The class a turn runs on when nothing else is said. */
|
|
56
53
|
const DEFAULT_CLASS = 'aegis';
|
|
@@ -59,15 +56,9 @@ const DEFAULT_CLASS = 'aegis';
|
|
|
59
56
|
const CLASS_LABELS = Object.freeze({
|
|
60
57
|
aegis: 'AEGIS Cloud (pooled)',
|
|
61
58
|
byok: 'Bring your own key (relayed, billed)',
|
|
62
|
-
|
|
59
|
+
local: 'Local model (your own machine, free)',
|
|
63
60
|
});
|
|
64
61
|
|
|
65
|
-
function unsupported(label) {
|
|
66
|
-
return async () => {
|
|
67
|
-
throw new Error(`aegiscode: ${label} is not available — this client only runs the aegis and byok classes`);
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
62
|
/**
|
|
72
63
|
* @param {object} client A client from client/aegis.js (createClient()).
|
|
73
64
|
* @param {() => boolean} getConfirmMode Whether mutating tools (exec,
|
|
@@ -89,20 +80,12 @@ function createEngine({ client, getConfirmMode, getClass, settings: injectedSett
|
|
|
89
80
|
const local = createLocalEngine({
|
|
90
81
|
aegis: client,
|
|
91
82
|
settings,
|
|
92
|
-
ollama:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
// `custom` class (the /model add catalog) rewrites a turn to the
|
|
99
|
-
// 'openai-compat' / 'anthropic' class the desktop engine already drives
|
|
100
|
-
// through exactly these two functions, so a user's own endpoint gets the
|
|
101
|
-
// same tool loop the pooled class does.
|
|
102
|
-
providers: {
|
|
103
|
-
anthropicMessages: providers.anthropicMessages,
|
|
104
|
-
openaiCompatible: providers.openaiCompatible,
|
|
105
|
-
},
|
|
83
|
+
// No `ollama` or `providers` injection: both were stubs for a transport
|
|
84
|
+
// this host never runs. The local class is served by the engine's own
|
|
85
|
+
// `localTransport`, which defaults to ./local.js — vendored alongside this
|
|
86
|
+
// engine by cli/scripts/predist.mjs — so a local model on the user's own
|
|
87
|
+
// machine works here with no extra wiring. `byok` never touches a local
|
|
88
|
+
// transport at all: it is relayed through AEGIS.
|
|
106
89
|
env: {
|
|
107
90
|
platform: process.platform,
|
|
108
91
|
arch: process.arch,
|
|
@@ -119,44 +102,8 @@ function createEngine({ client, getConfirmMode, getClass, settings: injectedSett
|
|
|
119
102
|
return HOST_CLASSES.includes(asked) ? asked : DEFAULT_CLASS;
|
|
120
103
|
}
|
|
121
104
|
|
|
122
|
-
/**
|
|
123
|
-
* Rewrite a `custom`-class turn to the concrete transport class the desktop
|
|
124
|
-
* engine already drives. The catalog entry is the source of truth for the
|
|
125
|
-
* endpoint; its base URL + key are written just-in-time into the wire-class
|
|
126
|
-
* settings row (settings.get(cls).baseURL / rawKey(cls) is what the engine's
|
|
127
|
-
* dispatch reads), and the payload's class + model are set to that wire class
|
|
128
|
-
* and the entry's real model string. Throws a clear, in-process error rather
|
|
129
|
-
* than shipping `model: undefined` or an empty base URL upstream.
|
|
130
|
-
*/
|
|
131
|
-
function prepareCustom(payload) {
|
|
132
|
-
const modelId = payload && payload.model;
|
|
133
|
-
const resolved = modelId ? custom.resolveCustom(modelId, settings) : null;
|
|
134
|
-
if (!resolved) {
|
|
135
|
-
const err = new Error(
|
|
136
|
-
modelId
|
|
137
|
-
? `custom: no model "${modelId}" in the catalog — /model add <id> <name> <model> <baseURL>, or /models to list.`
|
|
138
|
-
: 'custom: no model pinned — pin one with /model <id> (/models lists your custom endpoints).'
|
|
139
|
-
);
|
|
140
|
-
err.status = 400;
|
|
141
|
-
throw err;
|
|
142
|
-
}
|
|
143
|
-
if (resolved.wire === 'anthropic' && !resolved.key) {
|
|
144
|
-
const err = new Error(
|
|
145
|
-
`custom: "${resolved.id}" is an Anthropic-style endpoint and needs a key — /model key ${resolved.id}.`
|
|
146
|
-
);
|
|
147
|
-
err.status = 400;
|
|
148
|
-
throw err;
|
|
149
|
-
}
|
|
150
|
-
// Just-in-time scratch write: the wire-class row is only ever this session's
|
|
151
|
-
// active custom endpoint, re-set on every custom turn, so two custom models
|
|
152
|
-
// sharing a wire never collide.
|
|
153
|
-
settings.set(resolved.wireClass, { baseURL: resolved.baseURL, key: resolved.key || null });
|
|
154
|
-
return { ...payload, class: resolved.wireClass, model: resolved.model };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
105
|
function chat(payload, onDelta) {
|
|
158
106
|
const cls = currentClass();
|
|
159
|
-
if (cls === 'custom') return local.chat(prepareCustom(payload), onDelta);
|
|
160
107
|
// `class` is stamped here rather than sent by every caller, so the class a
|
|
161
108
|
// turn runs on has exactly one source of truth (the live session state) and
|
|
162
109
|
// a caller cannot accidentally pin a stale one.
|
|
@@ -170,23 +117,11 @@ function createEngine({ client, getConfirmMode, getClass, settings: injectedSett
|
|
|
170
117
|
clearSessionApprovals: local.clearSessionApprovals,
|
|
171
118
|
// The engine's own class table, narrowed to the ones this host runs — so a
|
|
172
119
|
// UI built from listClasses() can never offer a class that would throw.
|
|
173
|
-
// `custom` is not in the desktop engine's own CLASSES (it is a CLI concept
|
|
174
|
-
// that rewrites to openai-compat/anthropic), so it is appended here.
|
|
175
120
|
// Async, like the local engine's: listClasses() probes Ollama (a live
|
|
176
121
|
// HTTP round-trip) before it can answer, so returning the raw promise made
|
|
177
122
|
// `/class` throw "filter is not a function". The caller awaits it.
|
|
178
|
-
listClasses: async () =>
|
|
179
|
-
|
|
180
|
-
const configured = custom.listCustomModels(settings).length > 0;
|
|
181
|
-
return [...base, { class: 'custom', label: CLASS_LABELS.custom, kind: 'custom', configured }];
|
|
182
|
-
},
|
|
183
|
-
// The `custom` class enumerates the catalog (the desktop engine has no such
|
|
184
|
-
// class, so delegating would throw); every other class delegates.
|
|
185
|
-
listModels: (cls) => {
|
|
186
|
-
const want = cls || currentClass();
|
|
187
|
-
if (want === 'custom') return { class: 'custom', models: custom.listCustomModels(settings) };
|
|
188
|
-
return local.listModels(want);
|
|
189
|
-
},
|
|
123
|
+
listClasses: async () => (await local.listClasses()).filter((c) => HOST_CLASSES.includes(c.class)),
|
|
124
|
+
listModels: (cls) => local.listModels(cls || currentClass()),
|
|
190
125
|
classLabel: (cls) => CLASS_LABELS[cls] || CLASS_LABELS[DEFAULT_CLASS],
|
|
191
126
|
getClass: currentClass,
|
|
192
127
|
settings,
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* WHERE the CLI's shared modules are, with no side effects.
|
|
5
|
+
*
|
|
6
|
+
* This is `deps.js`'s path resolution split into its own module, and the split
|
|
7
|
+
* is load-bearing rather than cosmetic. `deps.js` eagerly requires everything
|
|
8
|
+
* it resolves — the engine, the tool registry, the client — so a module that
|
|
9
|
+
* only needs to *locate* one shared file cannot require deps.js: doing so
|
|
10
|
+
* from inside something deps.js itself requires would be a require cycle,
|
|
11
|
+
* handing one side a half-built exports object at load time.
|
|
12
|
+
*
|
|
13
|
+
* So: paths here, code in deps.js.
|
|
14
|
+
*
|
|
15
|
+
* Two layouts must work, exactly as deps.js documents:
|
|
16
|
+
*
|
|
17
|
+
* in-repo <repo>/cli/src/sharedpaths.js -> <repo>/desktop/lib/local/...
|
|
18
|
+
* npm <pkg>/src/sharedpaths.js -> <pkg>/vendor/desktop/lib/...
|
|
19
|
+
*
|
|
20
|
+
* Resolution is by existence, and a missing module is a loud error rather than
|
|
21
|
+
* a silent fallback to a second copy: duplicate implementations are how the
|
|
22
|
+
* desktop and the terminal would drift apart.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const fs = require('node:fs');
|
|
26
|
+
const path = require('node:path');
|
|
27
|
+
|
|
28
|
+
function roots() {
|
|
29
|
+
const srcDir = __dirname; // <root>/cli/src
|
|
30
|
+
const cliDir = path.join(srcDir, '..');
|
|
31
|
+
return [
|
|
32
|
+
path.join(srcDir, '..', '..'), // repo root (in-repo layout)
|
|
33
|
+
path.join(cliDir, 'vendor'), // staged tree (published layout)
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Resolve a repo-relative module path against whichever root has it. */
|
|
38
|
+
function resolveShared(relPath) {
|
|
39
|
+
for (const root of roots()) {
|
|
40
|
+
const candidate = path.join(root, relPath);
|
|
41
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
42
|
+
}
|
|
43
|
+
throw new Error(
|
|
44
|
+
`aegiscode: cannot find ${relPath}. Expected it beside cli/ (in the repo) or ` +
|
|
45
|
+
'under cli/vendor/ (installed package). Reinstall the package, or run ' +
|
|
46
|
+
'`node scripts/predist.mjs` from cli/ if this is a source checkout.'
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = { roots, resolveShared };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The shared `~/.aegiscode/.env` loader.
|
|
5
|
+
*
|
|
6
|
+
* One file holding every key this product family can use, for every host:
|
|
7
|
+
*
|
|
8
|
+
* AEGIS_API_KEY=aegis_…
|
|
9
|
+
* OPENAI_API_KEY=sk-…
|
|
10
|
+
* DEEPSEEK_API_KEY=sk-…
|
|
11
|
+
*
|
|
12
|
+
* Before this module each host read `process.env` only, which meant a key was
|
|
13
|
+
* configured if — and only if — the user had exported it in the shell that
|
|
14
|
+
* happened to launch the app. That is why the byok flow needed a separate
|
|
15
|
+
* `/byok-key <provider>` step and an encrypted per-provider store: the file was
|
|
16
|
+
* never read. Reading it into `process.env` at startup makes the instruction
|
|
17
|
+
* the same in the CLI and the desktop — "put it in ~/.aegiscode/.env" — and it
|
|
18
|
+
* needs no other change in either, because `AEGIS_API_KEY` is already the FIRST
|
|
19
|
+
* entry in `client/credentials.js`'s documented resolution order.
|
|
20
|
+
*
|
|
21
|
+
* Two rules this module deliberately obeys:
|
|
22
|
+
*
|
|
23
|
+
* 1. A variable already present in the environment is never overwritten. The
|
|
24
|
+
* file is a convenience for the common case, not a way for a stale file to
|
|
25
|
+
* shadow an explicit `AEGIS_API_KEY=… aegiscode` in CI.
|
|
26
|
+
* 2. It reads only. Nothing here writes a secret, so the 0600 store remains
|
|
27
|
+
* the one thing this repo creates; if the file the user made is readable
|
|
28
|
+
* by other accounts we say so rather than fixing it silently.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const fs = require('node:fs');
|
|
32
|
+
const path = require('node:path');
|
|
33
|
+
const credentials = require('./credentials.js');
|
|
34
|
+
|
|
35
|
+
const ENV_FILE = '.env';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* provider id -> the env var a BYOK key is conventionally exported as, for the
|
|
39
|
+
* ids where the generic rule below would spell the wrong name. The generic rule
|
|
40
|
+
* is `ID_API_KEY` in upper snake case, which already covers openai, anthropic,
|
|
41
|
+
* deepseek, groq, mistral, openrouter, together and most of the catalogue — so
|
|
42
|
+
* this table is only the exceptions, kept short on purpose.
|
|
43
|
+
*/
|
|
44
|
+
const ALIASES = Object.freeze({
|
|
45
|
+
google: 'GEMINI_API_KEY',
|
|
46
|
+
gemini: 'GEMINI_API_KEY',
|
|
47
|
+
'google-ai': 'GEMINI_API_KEY',
|
|
48
|
+
xai: 'XAI_API_KEY',
|
|
49
|
+
'x-ai': 'XAI_API_KEY',
|
|
50
|
+
grok: 'XAI_API_KEY',
|
|
51
|
+
huggingface: 'HF_TOKEN',
|
|
52
|
+
together: 'TOGETHER_API_KEY',
|
|
53
|
+
'together-ai': 'TOGETHER_API_KEY',
|
|
54
|
+
fireworks: 'FIREWORKS_API_KEY',
|
|
55
|
+
'azure-openai': 'AZURE_OPENAI_API_KEY',
|
|
56
|
+
'voyage-ai': 'VOYAGE_API_KEY',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/** The file this host would read, honouring `$AEGISCODE_HOME`. */
|
|
60
|
+
function envFileFor(dir) {
|
|
61
|
+
return path.join(dir || credentials.aegisHome(), ENV_FILE);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The env var one BYOK provider's key is read from.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} providerId e.g. 'openai', 'deepseek', 'Google'
|
|
68
|
+
* @returns {string} e.g. 'OPENAI_API_KEY' — '' when there is no usable id
|
|
69
|
+
*/
|
|
70
|
+
function envVarFor(providerId) {
|
|
71
|
+
const id = String(providerId || '').trim().toLowerCase();
|
|
72
|
+
if (!id) return '';
|
|
73
|
+
// A `byok:` prefixed row is the same provider — accept it so callers can pass
|
|
74
|
+
// whatever spelling they hold without stripping it first.
|
|
75
|
+
const bare = id.startsWith('byok:') ? id.slice('byok:'.length).trim() : id;
|
|
76
|
+
if (!bare) return '';
|
|
77
|
+
if (ALIASES[bare]) return ALIASES[bare];
|
|
78
|
+
const name = bare.toUpperCase().replace(/[^A-Z0-9]+/g, '_').replace(/^_+|_+$/g, '');
|
|
79
|
+
return name ? `${name}_API_KEY` : '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Parse dotenv text. Deliberately small and total: an unparseable line is
|
|
84
|
+
* skipped rather than thrown, so one bad line cannot stop the rest of the file
|
|
85
|
+
* (and a stray `#` comment or a blank line is normal, not an error).
|
|
86
|
+
*
|
|
87
|
+
* Handles `export `, `KEY=value`, `KEY="value"`, `KEY='value'` and a trailing
|
|
88
|
+
* ` # comment` on an unquoted value.
|
|
89
|
+
*/
|
|
90
|
+
function parseEnvText(text) {
|
|
91
|
+
const out = {};
|
|
92
|
+
for (const raw of String(text == null ? '' : text).split(/\r?\n/)) {
|
|
93
|
+
const line = raw.trim();
|
|
94
|
+
if (!line || line.startsWith('#')) continue;
|
|
95
|
+
const eq = line.indexOf('=');
|
|
96
|
+
if (eq <= 0) continue;
|
|
97
|
+
let name = line.slice(0, eq).trim();
|
|
98
|
+
if (name.startsWith('export ')) name = name.slice('export '.length).trim();
|
|
99
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) continue;
|
|
100
|
+
let value = line.slice(eq + 1).trim();
|
|
101
|
+
const quoted = (value.startsWith('"') && value.endsWith('"') && value.length >= 2)
|
|
102
|
+
|| (value.startsWith("'") && value.endsWith("'") && value.length >= 2);
|
|
103
|
+
if (quoted) {
|
|
104
|
+
value = value.slice(1, -1);
|
|
105
|
+
} else {
|
|
106
|
+
const hash = value.indexOf(' #');
|
|
107
|
+
if (hash !== -1) value = value.slice(0, hash).trim();
|
|
108
|
+
}
|
|
109
|
+
out[name] = value;
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Read the env file into `process.env` (or `o.env`), without overwriting
|
|
116
|
+
* anything already set.
|
|
117
|
+
*
|
|
118
|
+
* @param {object} [o]
|
|
119
|
+
* @param {object} [o.env] defaults to process.env
|
|
120
|
+
* @param {string} [o.dir] defaults to aegisHome()
|
|
121
|
+
* @param {string} [o.file] an explicit path, for tests
|
|
122
|
+
* @returns {{ok:boolean, file:string, loaded:string[], kept:string[],
|
|
123
|
+
* reason:string, loose:boolean, mode:number}}
|
|
124
|
+
* `loaded` — names taken from the file. `kept` — names the environment
|
|
125
|
+
* already had, left alone (a real export always wins).
|
|
126
|
+
*/
|
|
127
|
+
function loadEnvFile(o = {}) {
|
|
128
|
+
const env = o.env || process.env;
|
|
129
|
+
const file = o.file || envFileFor(o.dir);
|
|
130
|
+
const result = { ok: false, file, loaded: [], kept: [], reason: '', loose: false, mode: 0 };
|
|
131
|
+
|
|
132
|
+
let text;
|
|
133
|
+
try {
|
|
134
|
+
text = fs.readFileSync(file, 'utf8');
|
|
135
|
+
} catch (e) {
|
|
136
|
+
result.reason = e && e.code === 'ENOENT' ? 'absent' : 'unreadable';
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
const st = fs.statSync(file);
|
|
142
|
+
result.mode = st.mode & 0o777;
|
|
143
|
+
// Group- or world-readable: worth one line to the user, never a rewrite of
|
|
144
|
+
// a file we did not create.
|
|
145
|
+
result.loose = (st.mode & 0o077) !== 0;
|
|
146
|
+
} catch { /* stat is advisory only */ }
|
|
147
|
+
|
|
148
|
+
const parsed = parseEnvText(text);
|
|
149
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
150
|
+
if (env[name] === undefined || env[name] === '') {
|
|
151
|
+
env[name] = value;
|
|
152
|
+
result.loaded.push(name);
|
|
153
|
+
} else {
|
|
154
|
+
result.kept.push(name);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
result.ok = true;
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The BYOK key for a provider out of the environment, if the env file (or the
|
|
163
|
+
* shell) supplies one. Returns '' when it does not — the encrypted store then
|
|
164
|
+
* remains the only source, exactly as before.
|
|
165
|
+
*/
|
|
166
|
+
function providerKeyFromEnv(providerId, env) {
|
|
167
|
+
const name = envVarFor(providerId);
|
|
168
|
+
if (!name) return { key: '', env: '' };
|
|
169
|
+
const raw = (env || process.env)[name];
|
|
170
|
+
const key = typeof raw === 'string' ? raw.trim() : '';
|
|
171
|
+
return { key, env: name };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
module.exports = {
|
|
175
|
+
ENV_FILE,
|
|
176
|
+
ALIASES,
|
|
177
|
+
envFileFor,
|
|
178
|
+
envVarFor,
|
|
179
|
+
parseEnvText,
|
|
180
|
+
loadEnvFile,
|
|
181
|
+
providerKeyFromEnv,
|
|
182
|
+
};
|