@agenticmail/enterprise 0.5.453 → 0.5.455
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/CHANGELOG.md +7 -0
- package/dist/agent-heartbeat-GYWPCR3N.js +518 -0
- package/dist/agent-heartbeat-ZY5L5YAB.js +518 -0
- package/dist/agent-provisioner-Z7AUHO7M.js +231 -0
- package/dist/chunk-4Z2EHH32.js +7574 -0
- package/dist/chunk-EO6OJAZZ.js +7574 -0
- package/dist/chunk-HXBXNBDN.js +26343 -0
- package/dist/chunk-JPNHEYFZ.js +1728 -0
- package/dist/chunk-PSZU6FMQ.js +1151 -0
- package/dist/chunk-QBFO5H3V.js +26305 -0
- package/dist/chunk-SWARX2JK.js +1728 -0
- package/dist/chunk-TMDBV5Y4.js +5394 -0
- package/dist/chunk-TTKRFPYU.js +5394 -0
- package/dist/cli-agent-ICK7SJOB.js +2761 -0
- package/dist/cli-agent-URDHINSH.js +2761 -0
- package/dist/cli-serve-7OEDRFF4.js +322 -0
- package/dist/cli-serve-UWEDLJXU.js +322 -0
- package/dist/cli.js +3 -3
- package/dist/deployer-TXPCYQCJ.js +11 -0
- package/dist/index.js +5 -5
- package/dist/routes-4HXEEA34.js +94 -0
- package/dist/routes-L7JL6V3Y.js +94 -0
- package/dist/runtime-IDMHARB3.js +50 -0
- package/dist/runtime-OMQ7MRE5.js +50 -0
- package/dist/server-LSRSEFCF.js +36 -0
- package/dist/server-MWMBBTRJ.js +36 -0
- package/dist/setup-L4D42JIQ.js +20 -0
- package/dist/setup-VYFW7BC7.js +20 -0
- package/logs/cloudflared-error.log +70 -0
- package/package.json +1 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__esm
|
|
3
|
+
} from "./chunk-KFQGP6VL.js";
|
|
4
|
+
|
|
5
|
+
// src/engine/agent-provisioner.ts
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, statSync } from "fs";
|
|
7
|
+
import { resolve, dirname } from "path";
|
|
8
|
+
import { homedir } from "os";
|
|
9
|
+
import { execSync } from "child_process";
|
|
10
|
+
import { fileURLToPath } from "url";
|
|
11
|
+
function slugify(name) {
|
|
12
|
+
return name.split(/\s+/)[0].toLowerCase().replace(/[^a-z0-9-]/g, "");
|
|
13
|
+
}
|
|
14
|
+
function getPm2Name(config) {
|
|
15
|
+
const local = config.deployment?.config?.local;
|
|
16
|
+
if (local?.pm2Name) return local.pm2Name;
|
|
17
|
+
return `${slugify(config.name)}-agent`;
|
|
18
|
+
}
|
|
19
|
+
function findCliScript() {
|
|
20
|
+
try {
|
|
21
|
+
const thisFile = fileURLToPath(import.meta.url);
|
|
22
|
+
let dir = dirname(thisFile);
|
|
23
|
+
for (let i = 0; i < 5; i++) {
|
|
24
|
+
const candidate = resolve(dir, "dist", "cli.js");
|
|
25
|
+
if (existsSync(candidate)) return candidate;
|
|
26
|
+
const candidate2 = resolve(dir, "cli.js");
|
|
27
|
+
if (existsSync(candidate2) && dir.endsWith("dist")) return candidate2;
|
|
28
|
+
dir = dirname(dir);
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const result = execSync("npm root -g", { encoding: "utf8", timeout: 5e3 }).trim();
|
|
34
|
+
const candidate = resolve(result, "@agenticmail", "enterprise", "dist", "cli.js");
|
|
35
|
+
if (existsSync(candidate)) return candidate;
|
|
36
|
+
} catch {
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function provisionAgent(config) {
|
|
41
|
+
const slug = slugify(config.name);
|
|
42
|
+
const pm2Name = getPm2Name(config);
|
|
43
|
+
const amDir = resolve(homedir(), ".agenticmail");
|
|
44
|
+
const cwdEnvFile = resolve(process.cwd(), `.env.${slug}`);
|
|
45
|
+
const amDirEnvFile = resolve(amDir, `.env.${slug}`);
|
|
46
|
+
const envFile = existsSync(cwdEnvFile) ? cwdEnvFile : existsSync(amDirEnvFile) ? amDirEnvFile : cwdEnvFile;
|
|
47
|
+
const wrapperScript = resolve(amDir, `agent-${slug}.cjs`);
|
|
48
|
+
const port = config.deployment?.config?.local?.port || 3101;
|
|
49
|
+
try {
|
|
50
|
+
mkdirSync(amDir, { recursive: true });
|
|
51
|
+
} catch (e) {
|
|
52
|
+
return { success: false, error: `Cannot create ${amDir}: ${e.message}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
53
|
+
}
|
|
54
|
+
const envLines = [
|
|
55
|
+
`# Auto-generated for agent: ${config.displayName || config.name}`,
|
|
56
|
+
`# Created: ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
57
|
+
`AGENTICMAIL_AGENT_ID=${config.id}`,
|
|
58
|
+
`AGENTICMAIL_AGENT_NAME=${config.displayName || config.name}`,
|
|
59
|
+
`AGENTICMAIL_MODEL=${config.model?.provider || "anthropic"}/${config.model?.modelId || "claude-sonnet-4-20250514"}`,
|
|
60
|
+
`PORT=${port}`
|
|
61
|
+
];
|
|
62
|
+
if (config.model?.thinkingLevel) {
|
|
63
|
+
envLines.push(`AGENTICMAIL_THINKING=${config.model.thinkingLevel}`);
|
|
64
|
+
}
|
|
65
|
+
const envCandidates = [
|
|
66
|
+
resolve(amDir, ".env"),
|
|
67
|
+
resolve(process.cwd(), ".env")
|
|
68
|
+
];
|
|
69
|
+
const REQUIRED_INHERIT = ["DATABASE_URL", "JWT_SECRET"];
|
|
70
|
+
const OPTIONAL_INHERIT = ["AGENTICMAIL_VAULT_KEY", "ORG_ID", "ENTERPRISE_URL", "AGENTICMAIL_DOMAIN"];
|
|
71
|
+
let mainEnvContent = "";
|
|
72
|
+
for (const candidate of envCandidates) {
|
|
73
|
+
if (existsSync(candidate)) {
|
|
74
|
+
try {
|
|
75
|
+
mainEnvContent += "\n" + readFileSync(candidate, "utf8");
|
|
76
|
+
} catch {
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const inheritedKeys = /* @__PURE__ */ new Set();
|
|
81
|
+
for (const key of REQUIRED_INHERIT) {
|
|
82
|
+
const m = mainEnvContent.match(new RegExp(`^${key}=(.+)$`, "m"));
|
|
83
|
+
if (m) {
|
|
84
|
+
envLines.push(`${key}=${m[1]}`);
|
|
85
|
+
inheritedKeys.add(key);
|
|
86
|
+
} else if (process.env[key]) {
|
|
87
|
+
envLines.push(`${key}=${process.env[key]}`);
|
|
88
|
+
inheritedKeys.add(key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const missingKeys = REQUIRED_INHERIT.filter((k) => !inheritedKeys.has(k));
|
|
92
|
+
if (missingKeys.length > 0) {
|
|
93
|
+
return { success: false, error: `Missing required keys (checked .env files + process.env): ${missingKeys.join(", ")}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
94
|
+
}
|
|
95
|
+
for (const key of OPTIONAL_INHERIT) {
|
|
96
|
+
if (inheritedKeys.has(key)) continue;
|
|
97
|
+
const m = mainEnvContent.match(new RegExp(`^${key}=(.+)$`, "m"));
|
|
98
|
+
if (m) {
|
|
99
|
+
envLines.push(`${key}=${m[1]}`);
|
|
100
|
+
} else if (process.env[key]) {
|
|
101
|
+
envLines.push(`${key}=${process.env[key]}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!envLines.some((l) => l.startsWith("ENTERPRISE_URL="))) {
|
|
105
|
+
const portMatch = mainEnvContent.match(/^PORT=(\d+)$/m);
|
|
106
|
+
const serverPort = portMatch ? portMatch[1] : process.env.PORT || "8080";
|
|
107
|
+
envLines.push(`ENTERPRISE_URL=http://localhost:${serverPort}`);
|
|
108
|
+
}
|
|
109
|
+
let skipEnvWrite = false;
|
|
110
|
+
if (existsSync(envFile)) {
|
|
111
|
+
try {
|
|
112
|
+
const existing = readFileSync(envFile, "utf8");
|
|
113
|
+
const hasAgent = existing.includes(`AGENTICMAIL_AGENT_ID=${config.id}`);
|
|
114
|
+
const hasDb = existing.includes("DATABASE_URL=");
|
|
115
|
+
const hasJwt = existing.includes("JWT_SECRET=");
|
|
116
|
+
if (hasAgent && hasDb && hasJwt) {
|
|
117
|
+
skipEnvWrite = true;
|
|
118
|
+
}
|
|
119
|
+
} catch {
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (!skipEnvWrite) {
|
|
123
|
+
try {
|
|
124
|
+
writeFileSync(envFile, envLines.join("\n") + "\n");
|
|
125
|
+
} catch (e) {
|
|
126
|
+
return { success: false, error: `Cannot write ${envFile}: ${e.message}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const written = readFileSync(envFile, "utf8");
|
|
131
|
+
if (!written.includes(`AGENTICMAIL_AGENT_ID=${config.id}`)) {
|
|
132
|
+
return { success: false, error: `Env file validation failed: AGENTICMAIL_AGENT_ID not found in ${envFile}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
133
|
+
}
|
|
134
|
+
if (!written.includes("DATABASE_URL=")) {
|
|
135
|
+
return { success: false, error: `Env file validation failed: DATABASE_URL not found in ${envFile}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
136
|
+
}
|
|
137
|
+
} catch (e) {
|
|
138
|
+
return { success: false, error: `Cannot read back ${envFile}: ${e.message}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
139
|
+
}
|
|
140
|
+
const cliScript = findCliScript();
|
|
141
|
+
const wrapperLines = [
|
|
142
|
+
`#!/usr/bin/env node`,
|
|
143
|
+
`// Auto-generated agent wrapper for: ${config.displayName || config.name}`,
|
|
144
|
+
`// Agent ID: ${config.id}`,
|
|
145
|
+
`// Created: ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
146
|
+
`'use strict';`,
|
|
147
|
+
``,
|
|
148
|
+
`const { readFileSync } = require('fs');`,
|
|
149
|
+
``,
|
|
150
|
+
`// \u2500\u2500 Load agent env file \u2500\u2500`,
|
|
151
|
+
`const envFile = ${JSON.stringify(envFile)};`,
|
|
152
|
+
`try {`,
|
|
153
|
+
` const lines = readFileSync(envFile, 'utf8').split('\\n');`,
|
|
154
|
+
` for (const line of lines) {`,
|
|
155
|
+
` const m = line.match(/^([A-Z_][A-Z0-9_]*)=(.*)$/);`,
|
|
156
|
+
` if (m) process.env[m[1]] = m[2];`,
|
|
157
|
+
` }`,
|
|
158
|
+
`} catch (e) {`,
|
|
159
|
+
` console.error('[agent-wrapper] FATAL: Cannot load env file:', envFile, e.message);`,
|
|
160
|
+
` process.exit(1);`,
|
|
161
|
+
`}`,
|
|
162
|
+
``,
|
|
163
|
+
`// \u2500\u2500 Validate required env vars \u2500\u2500`,
|
|
164
|
+
`const required = ['AGENTICMAIL_AGENT_ID', 'DATABASE_URL', 'JWT_SECRET'];`,
|
|
165
|
+
`for (const key of required) {`,
|
|
166
|
+
` if (!process.env[key]) {`,
|
|
167
|
+
` console.error('[agent-wrapper] FATAL: Missing required env var:', key);`,
|
|
168
|
+
` process.exit(1);`,
|
|
169
|
+
` }`,
|
|
170
|
+
`}`,
|
|
171
|
+
`console.log('[agent-wrapper] Agent:', process.env.AGENTICMAIL_AGENT_NAME, '(' + process.env.AGENTICMAIL_AGENT_ID.slice(0, 8) + '...)');`,
|
|
172
|
+
``
|
|
173
|
+
];
|
|
174
|
+
if (cliScript) {
|
|
175
|
+
wrapperLines.push(
|
|
176
|
+
`// \u2500\u2500 Direct execution (found cli.js at build time) \u2500\u2500`,
|
|
177
|
+
`// Set argv so cli.js routes to 'agent' command`,
|
|
178
|
+
`process.argv = [process.execPath, __filename, 'agent'];`,
|
|
179
|
+
`try {`,
|
|
180
|
+
` require(${JSON.stringify(cliScript)});`,
|
|
181
|
+
`} catch (e) {`,
|
|
182
|
+
` // Fallback to npx if direct path is stale (e.g. npx cache cleared)`,
|
|
183
|
+
` console.warn('[agent-wrapper] Direct execution failed, falling back to npx:', e.message);`,
|
|
184
|
+
` const { spawnSync } = require('child_process');`,
|
|
185
|
+
` const r = spawnSync('npx', ['@agenticmail/enterprise', 'agent'], {`,
|
|
186
|
+
` stdio: 'inherit', env: process.env, shell: process.platform === 'win32'`,
|
|
187
|
+
` });`,
|
|
188
|
+
` process.exit(r.status || 0);`,
|
|
189
|
+
`}`
|
|
190
|
+
);
|
|
191
|
+
} else {
|
|
192
|
+
wrapperLines.push(
|
|
193
|
+
`// \u2500\u2500 npx execution (cli.js not found at provision time) \u2500\u2500`,
|
|
194
|
+
`const { spawnSync } = require('child_process');`,
|
|
195
|
+
`const r = spawnSync('npx', ['@agenticmail/enterprise', 'agent'], {`,
|
|
196
|
+
` stdio: 'inherit', env: process.env, shell: process.platform === 'win32'`,
|
|
197
|
+
`});`,
|
|
198
|
+
`process.exit(r.status || 0);`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
writeFileSync(wrapperScript, wrapperLines.join("\n") + "\n");
|
|
203
|
+
} catch (e) {
|
|
204
|
+
return { success: false, error: `Cannot write ${wrapperScript}: ${e.message}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const stat = statSync(wrapperScript);
|
|
208
|
+
if (stat.size < 100) {
|
|
209
|
+
return { success: false, error: `Wrapper script too small (${stat.size} bytes): ${wrapperScript}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
210
|
+
}
|
|
211
|
+
} catch (e) {
|
|
212
|
+
return { success: false, error: `Cannot stat wrapper: ${e.message}`, envFile, wrapperScript, slug, pm2Name, port };
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
success: true,
|
|
216
|
+
envFile,
|
|
217
|
+
wrapperScript,
|
|
218
|
+
cliScript: cliScript || void 0,
|
|
219
|
+
slug,
|
|
220
|
+
pm2Name,
|
|
221
|
+
port
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
var init_agent_provisioner = __esm({
|
|
225
|
+
"src/engine/agent-provisioner.ts"() {
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
init_agent_provisioner();
|
|
229
|
+
export {
|
|
230
|
+
provisionAgent
|
|
231
|
+
};
|