@openpalm/lib 0.9.8 → 0.10.1
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 -71
- package/package.json +1 -1
- package/src/control-plane/audit.ts +4 -4
- package/src/control-plane/backup.ts +31 -0
- package/src/control-plane/channels.ts +88 -156
- package/src/control-plane/cleanup-guardrails.test.ts +289 -0
- package/src/control-plane/compose-args.test.ts +170 -0
- package/src/control-plane/compose-args.ts +57 -0
- package/src/control-plane/config-persistence.ts +270 -0
- package/src/control-plane/core-assets.ts +58 -234
- package/src/control-plane/crypto.ts +14 -0
- package/src/control-plane/docker.ts +94 -204
- package/src/control-plane/env-schema-validation.test.ts +118 -0
- package/src/control-plane/extends-support.test.ts +105 -0
- package/src/control-plane/home.ts +133 -0
- package/src/control-plane/install-edge-cases.test.ts +314 -717
- package/src/control-plane/lifecycle.ts +215 -233
- package/src/control-plane/lock.test.ts +194 -0
- package/src/control-plane/lock.ts +176 -0
- package/src/control-plane/memory-config.ts +34 -160
- package/src/control-plane/opencode-client.test.ts +154 -0
- package/src/control-plane/opencode-client.ts +113 -0
- package/src/control-plane/provider-config.ts +34 -0
- package/src/control-plane/redact-schema.ts +50 -0
- package/src/control-plane/registry-components.test.ts +313 -0
- package/src/control-plane/registry.test.ts +414 -0
- package/src/control-plane/registry.ts +418 -0
- package/src/control-plane/rollback.ts +128 -0
- package/src/control-plane/scheduler.ts +18 -190
- package/src/control-plane/secret-backend.test.ts +359 -0
- package/src/control-plane/secret-backend.ts +322 -0
- package/src/control-plane/secret-mappings.ts +185 -0
- package/src/control-plane/secrets.ts +186 -112
- package/src/control-plane/setup-config.schema.json +306 -0
- package/src/control-plane/setup-status.ts +15 -8
- package/src/control-plane/setup-validation.ts +90 -0
- package/src/control-plane/setup.test.ts +336 -929
- package/src/control-plane/setup.ts +159 -849
- package/src/control-plane/spec-to-env.test.ts +100 -0
- package/src/control-plane/spec-to-env.ts +195 -0
- package/src/control-plane/spec-validator.ts +159 -0
- package/src/control-plane/stack-spec.test.ts +150 -0
- package/src/control-plane/stack-spec.ts +101 -22
- package/src/control-plane/types.ts +6 -99
- package/src/control-plane/validate.ts +107 -0
- package/src/index.ts +101 -159
- package/src/provider-constants.ts +2 -31
- package/src/control-plane/connection-mapping.ts +0 -191
- package/src/control-plane/connection-migration-flags.ts +0 -40
- package/src/control-plane/connection-profiles.ts +0 -317
- package/src/control-plane/core-asset-provider.ts +0 -21
- package/src/control-plane/fs-asset-provider.ts +0 -65
- package/src/control-plane/fs-registry-provider.ts +0 -46
- package/src/control-plane/paths.ts +0 -77
- package/src/control-plane/registry-provider.ts +0 -19
- package/src/control-plane/staging.ts +0 -399
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Shared setup orchestration for the OpenPalm control plane.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* so that both the CLI setup wizard and the admin UI can call `performSetup()`.
|
|
6
|
-
*
|
|
4
|
+
* Both the CLI setup wizard and the admin UI call `performSetup()`.
|
|
7
5
|
* This module does NOT include Docker operations (compose up, image pull, etc.)
|
|
8
6
|
* — those happen separately in the caller after setup completes.
|
|
9
7
|
*/
|
|
10
8
|
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
|
|
9
|
+
import { randomBytes } from "node:crypto";
|
|
11
10
|
import { createLogger } from "../logger.js";
|
|
12
11
|
import {
|
|
13
12
|
PROVIDER_KEY_MAP,
|
|
@@ -15,34 +14,26 @@ import {
|
|
|
15
14
|
OLLAMA_INSTACK_URL,
|
|
16
15
|
} from "../provider-constants.js";
|
|
17
16
|
import { mergeEnvContent } from "./env.js";
|
|
18
|
-
import {
|
|
17
|
+
import { ensureHomeDirs } from "./home.js";
|
|
19
18
|
import {
|
|
20
19
|
ensureSecrets,
|
|
21
20
|
updateSecretsEnv,
|
|
21
|
+
updateSystemSecretsEnv,
|
|
22
22
|
ensureOpenCodeConfig,
|
|
23
|
+
readStackEnv,
|
|
23
24
|
} from "./secrets.js";
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import { writeMemoryConfig } from "./memory-config.js";
|
|
27
|
-
import { ensureOpenCodeSystemConfig, ensureAdminOpenCodeConfig, ensureMemoryDir } from "./core-assets.js";
|
|
28
|
-
import { applyInstall, createState, writeSetupTokenFile } from "./lifecycle.js";
|
|
25
|
+
import { ensureOpenCodeSystemConfig, ensureMemoryDir } from "./core-assets.js";
|
|
26
|
+
import { createState, writeSetupTokenFile } from "./lifecycle.js";
|
|
29
27
|
import { writeStackSpec } from "./stack-spec.js";
|
|
30
|
-
import type { StackSpec } from "./stack-spec.js";
|
|
31
|
-
import {
|
|
32
|
-
import type {
|
|
33
|
-
import
|
|
34
|
-
import
|
|
28
|
+
import type { StackSpec, StackSpecCapabilities } from "./stack-spec.js";
|
|
29
|
+
import { writeCapabilityVars } from "./spec-to-env.js";
|
|
30
|
+
import type { ControlPlaneState } from "./types.js";
|
|
31
|
+
import { validateSetupSpec } from "./setup-validation.js";
|
|
32
|
+
import { listEnabledAddonIds } from "./registry.js";
|
|
33
|
+
export { validateSetupSpec } from "./setup-validation.js";
|
|
35
34
|
|
|
36
35
|
const logger = createLogger("setup");
|
|
37
36
|
|
|
38
|
-
/** Apply Ollama in-stack URL override to connections when Ollama is enabled. */
|
|
39
|
-
function resolveOllamaUrls(connections: SetupConnection[], ollamaEnabled: boolean): SetupConnection[] {
|
|
40
|
-
if (!ollamaEnabled) return connections;
|
|
41
|
-
return connections.map((c) =>
|
|
42
|
-
c.provider === "ollama" ? { ...c, baseUrl: OLLAMA_INSTACK_URL } : c
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
37
|
// ── Types ────────────────────────────────────────────────────────────────
|
|
47
38
|
|
|
48
39
|
export type SetupConnection = {
|
|
@@ -53,606 +44,112 @@ export type SetupConnection = {
|
|
|
53
44
|
apiKey: string;
|
|
54
45
|
};
|
|
55
46
|
|
|
56
|
-
export type SetupAssignments = {
|
|
57
|
-
llm: { connectionId: string; model: string; smallModel?: string };
|
|
58
|
-
embeddings: { connectionId: string; model: string; embeddingDims?: number };
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type SetupInput = {
|
|
62
|
-
adminToken: string;
|
|
63
|
-
ownerName?: string;
|
|
64
|
-
ownerEmail?: string;
|
|
65
|
-
memoryUserId: string;
|
|
66
|
-
ollamaEnabled: boolean;
|
|
67
|
-
connections: SetupConnection[];
|
|
68
|
-
assignments: SetupAssignments;
|
|
69
|
-
voice?: {
|
|
70
|
-
tts?: string; // e.g. 'kokoro', 'piper', 'openai-tts', 'browser-tts', null
|
|
71
|
-
stt?: string; // e.g. 'whisper-local', 'openai-stt', 'browser-stt', null
|
|
72
|
-
};
|
|
73
|
-
channels?: string[]; // e.g. ['chat', 'discord', 'api']
|
|
74
|
-
services?: {
|
|
75
|
-
admin?: boolean;
|
|
76
|
-
openviking?: boolean;
|
|
77
|
-
ollama?: boolean;
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
|
|
81
47
|
export type SetupResult = {
|
|
82
48
|
ok: boolean;
|
|
83
49
|
error?: string;
|
|
84
|
-
/** Services that should be started after setup. */
|
|
85
50
|
started?: string[];
|
|
86
51
|
};
|
|
87
52
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
version: 1;
|
|
92
|
-
owner?: { name?: string; email?: string };
|
|
53
|
+
export type SetupSpec = {
|
|
54
|
+
version: 2;
|
|
55
|
+
capabilities: StackSpecCapabilities;
|
|
93
56
|
security: { adminToken: string };
|
|
57
|
+
owner?: { name?: string; email?: string };
|
|
94
58
|
connections: SetupConnection[];
|
|
95
|
-
|
|
96
|
-
memory?: { userId?: string };
|
|
97
|
-
channels?: Record<string, boolean | ChannelCredentials>;
|
|
98
|
-
services?: Record<string, boolean | ServiceConfig>;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export type SetupConfigAssignments = {
|
|
102
|
-
llm: { connectionId: string; model: string; smallModel?: string };
|
|
103
|
-
embeddings: { connectionId: string; model: string; embeddingDims?: number };
|
|
104
|
-
tts?: { engine: string; connectionId?: string; model?: string } | string | null;
|
|
105
|
-
stt?: { engine: string; connectionId?: string; model?: string } | string | null;
|
|
59
|
+
channelCredentials?: Record<string, Record<string, string>>;
|
|
106
60
|
};
|
|
107
61
|
|
|
108
|
-
export type ChannelCredentials = {
|
|
109
|
-
enabled?: boolean;
|
|
110
|
-
botToken?: string;
|
|
111
|
-
applicationId?: string;
|
|
112
|
-
registerCommands?: boolean;
|
|
113
|
-
allowedGuilds?: string;
|
|
114
|
-
allowedRoles?: string;
|
|
115
|
-
allowedUsers?: string;
|
|
116
|
-
blockedUsers?: string;
|
|
117
|
-
slackBotToken?: string;
|
|
118
|
-
slackAppToken?: string;
|
|
119
|
-
allowedChannels?: string;
|
|
120
|
-
[key: string]: unknown;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export type ServiceConfig = {
|
|
124
|
-
enabled: boolean;
|
|
125
|
-
[key: string]: unknown;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
export type DetectedProvider = {
|
|
129
|
-
provider: string;
|
|
130
|
-
url: string;
|
|
131
|
-
available: boolean;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
// ── Validation ───────────────────────────────────────────────────────────
|
|
135
|
-
|
|
136
|
-
/** Safe env var key pattern: uppercase alphanumeric + underscores, starting with a letter. */
|
|
137
|
-
const SAFE_ENV_KEY_RE = /^[A-Z][A-Z0-9_]*$/;
|
|
138
|
-
|
|
139
|
-
/** Valid connection ID pattern: starts with letter or digit, allows A-Z, a-z, 0-9, _, -. */
|
|
140
|
-
const CONNECTION_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
141
|
-
|
|
142
|
-
/** Providers that are valid for setup wizard connections. */
|
|
143
|
-
const WIZARD_PROVIDERS = new Set([
|
|
144
|
-
"openai", "anthropic", "ollama", "groq", "together",
|
|
145
|
-
"mistral", "deepseek", "xai", "lmstudio", "model-runner",
|
|
146
|
-
"ollama-instack", "google", "huggingface",
|
|
147
|
-
]);
|
|
148
|
-
|
|
149
|
-
// ── Shared Validation Helpers ────────────────────────────────────────────
|
|
150
|
-
|
|
151
|
-
/** Validate a connections array. Pushes errors to the provided array. */
|
|
152
|
-
function validateConnectionsArray(
|
|
153
|
-
connections: unknown,
|
|
154
|
-
errors: string[]
|
|
155
|
-
): void {
|
|
156
|
-
if (!Array.isArray(connections) || connections.length === 0) {
|
|
157
|
-
errors.push("connections array is required and must be non-empty");
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
const seenIds = new Set<string>();
|
|
161
|
-
for (let i = 0; i < connections.length; i++) {
|
|
162
|
-
const c = connections[i];
|
|
163
|
-
if (typeof c !== "object" || c === null) {
|
|
164
|
-
errors.push(`connections[${i}] must be an object`);
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
const conn = c as Record<string, unknown>;
|
|
168
|
-
const id = typeof conn.id === "string" ? conn.id.trim() : "";
|
|
169
|
-
const provider = typeof conn.provider === "string" ? conn.provider.trim() : "";
|
|
170
|
-
|
|
171
|
-
if (!id) {
|
|
172
|
-
errors.push(`connections[${i}].id is required`);
|
|
173
|
-
} else if (!CONNECTION_ID_RE.test(id)) {
|
|
174
|
-
errors.push(`connections[${i}].id must start with a letter or digit (allowed: A-Z, a-z, 0-9, _, -)`);
|
|
175
|
-
} else if (seenIds.has(id)) {
|
|
176
|
-
errors.push(`Duplicate connection ID: ${id}`);
|
|
177
|
-
} else {
|
|
178
|
-
seenIds.add(id);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const name = typeof conn.name === "string" ? conn.name.trim() : "";
|
|
182
|
-
if (!name) {
|
|
183
|
-
errors.push(`connections[${i}].name is required`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (!provider) {
|
|
187
|
-
errors.push(`connections[${i}].provider is required`);
|
|
188
|
-
} else if (!WIZARD_PROVIDERS.has(provider)) {
|
|
189
|
-
errors.push(`connections[${i}].provider "${provider}" is outside wizard scope`);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Validate assignments block (llm + embeddings) and cross-validate
|
|
196
|
-
* connectionIds against the connections array. Pushes errors to the
|
|
197
|
-
* provided array.
|
|
198
|
-
*/
|
|
199
|
-
function validateAssignmentsBlock(
|
|
200
|
-
assignments: unknown,
|
|
201
|
-
connections: unknown,
|
|
202
|
-
errors: string[]
|
|
203
|
-
): void {
|
|
204
|
-
if (typeof assignments !== "object" || assignments === null) {
|
|
205
|
-
errors.push("assignments object is required");
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const assignmentsObj = assignments as Record<string, unknown>;
|
|
210
|
-
const llm = assignmentsObj.llm;
|
|
211
|
-
const embeddings = assignmentsObj.embeddings;
|
|
212
|
-
const preAssignmentLength = errors.length;
|
|
213
|
-
|
|
214
|
-
if (typeof llm !== "object" || llm === null) {
|
|
215
|
-
errors.push("assignments.llm is required");
|
|
216
|
-
} else {
|
|
217
|
-
const llmObj = llm as Record<string, unknown>;
|
|
218
|
-
if (!llmObj.connectionId || typeof llmObj.connectionId !== "string") {
|
|
219
|
-
errors.push("assignments.llm.connectionId is required");
|
|
220
|
-
}
|
|
221
|
-
if (!llmObj.model || typeof llmObj.model !== "string") {
|
|
222
|
-
errors.push("assignments.llm.model is required");
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if (typeof embeddings !== "object" || embeddings === null) {
|
|
227
|
-
errors.push("assignments.embeddings is required");
|
|
228
|
-
} else {
|
|
229
|
-
const embObj = embeddings as Record<string, unknown>;
|
|
230
|
-
if (!embObj.connectionId || typeof embObj.connectionId !== "string") {
|
|
231
|
-
errors.push("assignments.embeddings.connectionId is required");
|
|
232
|
-
}
|
|
233
|
-
if (!embObj.model || typeof embObj.model !== "string") {
|
|
234
|
-
errors.push("assignments.embeddings.model is required");
|
|
235
|
-
}
|
|
236
|
-
if (
|
|
237
|
-
embObj.embeddingDims !== undefined &&
|
|
238
|
-
(typeof embObj.embeddingDims !== "number" ||
|
|
239
|
-
!Number.isInteger(embObj.embeddingDims) ||
|
|
240
|
-
embObj.embeddingDims < 1)
|
|
241
|
-
) {
|
|
242
|
-
errors.push("assignments.embeddings.embeddingDims must be a positive integer");
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Cross-validate: assignment connectionIds must reference a connection.
|
|
247
|
-
// Only run when no new assignment errors were added above.
|
|
248
|
-
if (Array.isArray(connections) && errors.length === preAssignmentLength) {
|
|
249
|
-
const connectionIds = new Set(
|
|
250
|
-
(connections as Array<Record<string, unknown>>).map(
|
|
251
|
-
(c) => typeof c.id === "string" ? c.id.trim() : ""
|
|
252
|
-
)
|
|
253
|
-
);
|
|
254
|
-
const llmConnId =
|
|
255
|
-
typeof (llm as Record<string, unknown>)?.connectionId === "string"
|
|
256
|
-
? ((llm as Record<string, unknown>).connectionId as string)
|
|
257
|
-
: "";
|
|
258
|
-
const embConnId =
|
|
259
|
-
typeof (embeddings as Record<string, unknown>)?.connectionId === "string"
|
|
260
|
-
? ((embeddings as Record<string, unknown>).connectionId as string)
|
|
261
|
-
: "";
|
|
262
|
-
|
|
263
|
-
if (llmConnId && !connectionIds.has(llmConnId)) {
|
|
264
|
-
errors.push(`assignments.llm.connectionId "${llmConnId}" does not match any connection`);
|
|
265
|
-
}
|
|
266
|
-
if (embConnId && !connectionIds.has(embConnId)) {
|
|
267
|
-
errors.push(`assignments.embeddings.connectionId "${embConnId}" does not match any connection`);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
export function validateSetupInput(input: unknown): { valid: boolean; errors: string[] } {
|
|
273
|
-
const errors: string[] = [];
|
|
274
|
-
|
|
275
|
-
if (typeof input !== "object" || input === null) {
|
|
276
|
-
return { valid: false, errors: ["Input must be a non-null object"] };
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
const body = input as Record<string, unknown>;
|
|
280
|
-
|
|
281
|
-
// adminToken
|
|
282
|
-
if (typeof body.adminToken !== "string" || !body.adminToken) {
|
|
283
|
-
errors.push("adminToken is required and must be a non-empty string");
|
|
284
|
-
} else if (body.adminToken.length < 8) {
|
|
285
|
-
errors.push("adminToken must be at least 8 characters");
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// ownerName and ownerEmail are optional strings
|
|
289
|
-
if (body.ownerName !== undefined && typeof body.ownerName !== "string") {
|
|
290
|
-
errors.push("ownerName must be a string if provided");
|
|
291
|
-
}
|
|
292
|
-
if (body.ownerEmail !== undefined && typeof body.ownerEmail !== "string") {
|
|
293
|
-
errors.push("ownerEmail must be a string if provided");
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// memoryUserId
|
|
297
|
-
if (body.memoryUserId !== undefined && typeof body.memoryUserId !== "string") {
|
|
298
|
-
errors.push("memoryUserId must be a string");
|
|
299
|
-
}
|
|
300
|
-
if (typeof body.memoryUserId === "string" && !/^[A-Za-z0-9_]+$/.test(body.memoryUserId)) {
|
|
301
|
-
errors.push("memoryUserId contains invalid characters (alphanumeric and underscores only)");
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// ollamaEnabled
|
|
305
|
-
if (body.ollamaEnabled !== undefined && typeof body.ollamaEnabled !== "boolean") {
|
|
306
|
-
errors.push("ollamaEnabled must be a boolean");
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// voice (optional)
|
|
310
|
-
if (body.voice !== undefined) {
|
|
311
|
-
if (typeof body.voice !== "object" || body.voice === null) {
|
|
312
|
-
errors.push("voice must be an object if provided");
|
|
313
|
-
} else {
|
|
314
|
-
const voice = body.voice as Record<string, unknown>;
|
|
315
|
-
if (voice.tts !== undefined && voice.tts !== null && typeof voice.tts !== "string") {
|
|
316
|
-
errors.push("voice.tts must be a string or null if provided");
|
|
317
|
-
}
|
|
318
|
-
if (voice.stt !== undefined && voice.stt !== null && typeof voice.stt !== "string") {
|
|
319
|
-
errors.push("voice.stt must be a string or null if provided");
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// channels (optional)
|
|
325
|
-
if (body.channels !== undefined) {
|
|
326
|
-
if (!Array.isArray(body.channels)) {
|
|
327
|
-
errors.push("channels must be an array if provided");
|
|
328
|
-
} else {
|
|
329
|
-
for (let i = 0; i < body.channels.length; i++) {
|
|
330
|
-
if (typeof body.channels[i] !== "string") {
|
|
331
|
-
errors.push(`channels[${i}] must be a string`);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// services (optional)
|
|
338
|
-
if (body.services !== undefined) {
|
|
339
|
-
if (typeof body.services !== "object" || body.services === null) {
|
|
340
|
-
errors.push("services must be an object if provided");
|
|
341
|
-
} else {
|
|
342
|
-
const services = body.services as Record<string, unknown>;
|
|
343
|
-
for (const [key, val] of Object.entries(services)) {
|
|
344
|
-
if (typeof val !== "boolean") {
|
|
345
|
-
errors.push(`services.${key} must be a boolean`);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// connections
|
|
352
|
-
validateConnectionsArray(body.connections, errors);
|
|
353
|
-
|
|
354
|
-
// assignments
|
|
355
|
-
validateAssignmentsBlock(body.assignments, body.connections, errors);
|
|
356
|
-
|
|
357
|
-
return { valid: errors.length === 0, errors };
|
|
358
|
-
}
|
|
359
|
-
|
|
360
62
|
// ── Secrets Builder ──────────────────────────────────────────────────────
|
|
361
63
|
|
|
362
64
|
/**
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
* Returns a Record<string, string> of secrets.env updates that should be
|
|
366
|
-
* written during setup.
|
|
65
|
+
* Map provider id → env var for a custom base URL override.
|
|
66
|
+
* Allows writeCapabilityVars to resolve non-default endpoints.
|
|
367
67
|
*/
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
68
|
+
const PROVIDER_BASE_URL_ENV: Record<string, string> = {
|
|
69
|
+
openai: "OPENAI_BASE_URL",
|
|
70
|
+
anthropic: "ANTHROPIC_BASE_URL",
|
|
71
|
+
groq: "GROQ_BASE_URL",
|
|
72
|
+
mistral: "MISTRAL_BASE_URL",
|
|
73
|
+
together: "TOGETHER_BASE_URL",
|
|
74
|
+
deepseek: "DEEPSEEK_BASE_URL",
|
|
75
|
+
xai: "XAI_BASE_URL",
|
|
76
|
+
google: "GOOGLE_BASE_URL",
|
|
77
|
+
huggingface: "HF_BASE_URL",
|
|
78
|
+
ollama: "OLLAMA_BASE_URL",
|
|
79
|
+
lmstudio: "LMSTUDIO_BASE_URL",
|
|
80
|
+
"model-runner": "MODEL_RUNNER_BASE_URL",
|
|
81
|
+
"openai-compatible": "OPENAI_COMPATIBLE_BASE_URL",
|
|
82
|
+
};
|
|
374
83
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
84
|
+
export function buildSecretsFromSetup(
|
|
85
|
+
connections: SetupConnection[],
|
|
86
|
+
owner?: { name?: string; email?: string },
|
|
87
|
+
): Record<string, string> {
|
|
88
|
+
const updates: Record<string, string> = {};
|
|
89
|
+
const ownerName = (owner?.name?.trim() ?? "").replace(/[\r\n\0]/g, "").slice(0, 200);
|
|
90
|
+
const ownerEmail = (owner?.email?.trim() ?? "").replace(/[\r\n\0]/g, "").slice(0, 200);
|
|
378
91
|
if (ownerName) updates.OWNER_NAME = ownerName;
|
|
379
92
|
if (ownerEmail) updates.OWNER_EMAIL = ownerEmail;
|
|
380
93
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
// System LLM vars
|
|
395
|
-
const llmConnection = effectiveConnections.find((c) => c.id === input.assignments.llm.connectionId);
|
|
396
|
-
if (llmConnection) {
|
|
397
|
-
updates.SYSTEM_LLM_PROVIDER = llmConnection.provider;
|
|
398
|
-
updates.SYSTEM_LLM_MODEL = input.assignments.llm.model;
|
|
399
|
-
if (llmConnection.baseUrl) {
|
|
400
|
-
updates.SYSTEM_LLM_BASE_URL = llmConnection.baseUrl;
|
|
401
|
-
const normalizedUrl = llmConnection.baseUrl.replace(/\/+$/, "");
|
|
402
|
-
updates.OPENAI_BASE_URL = normalizedUrl.endsWith("/v1") ? normalizedUrl : `${normalizedUrl}/v1`;
|
|
94
|
+
for (const cap of connections) {
|
|
95
|
+
// API key: spec value takes precedence, then fall back to environment
|
|
96
|
+
const envVar = PROVIDER_KEY_MAP[cap.provider];
|
|
97
|
+
if (envVar) {
|
|
98
|
+
const key = cap.apiKey || process.env[envVar] || "";
|
|
99
|
+
if (key) updates[envVar] = key;
|
|
100
|
+
}
|
|
101
|
+
// Persist user-configured base URL for any provider so writeCapabilityVars can resolve it
|
|
102
|
+
if (cap.baseUrl) {
|
|
103
|
+
const urlEnv = PROVIDER_BASE_URL_ENV[cap.provider];
|
|
104
|
+
if (urlEnv) updates[urlEnv] = cap.baseUrl;
|
|
403
105
|
}
|
|
404
106
|
}
|
|
405
|
-
|
|
406
|
-
// Memory user ID
|
|
407
|
-
updates.MEMORY_USER_ID = input.memoryUserId || "default_user";
|
|
408
|
-
|
|
409
107
|
return updates;
|
|
410
108
|
}
|
|
411
109
|
|
|
412
|
-
// ── Connection Env Var Map Builder ───────────────────────────────────────
|
|
413
|
-
|
|
414
110
|
/**
|
|
415
|
-
*
|
|
416
|
-
*
|
|
111
|
+
* Read auth.json and extract API keys for OAuth-authenticated providers.
|
|
112
|
+
* This fills the gap where OAuth auth writes tokens to auth.json but
|
|
113
|
+
* not to stack.env — the memory service needs them as env vars.
|
|
417
114
|
*/
|
|
418
|
-
export function
|
|
419
|
-
|
|
420
|
-
)
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
115
|
+
export function extractAuthJsonKeys(vaultDir: string): Record<string, string> {
|
|
116
|
+
const authJsonPath = `${vaultDir}/stack/auth.json`;
|
|
117
|
+
if (!existsSync(authJsonPath)) return {};
|
|
118
|
+
try {
|
|
119
|
+
const raw = readFileSync(authJsonPath, "utf-8").trim();
|
|
120
|
+
if (!raw || raw === "{}") return {};
|
|
121
|
+
const auth = JSON.parse(raw) as Record<string, unknown>;
|
|
122
|
+
const updates: Record<string, string> = {};
|
|
123
|
+
for (const [provider, entry] of Object.entries(auth)) {
|
|
124
|
+
if (!entry || typeof entry !== "object") continue;
|
|
125
|
+
const record = entry as Record<string, unknown>;
|
|
126
|
+
// OpenCode stores API keys as { token: "..." } or { apiKey: "..." }
|
|
127
|
+
const token = (record.token ?? record.apiKey ?? record.api_key ?? record.key) as string | undefined;
|
|
128
|
+
if (token && typeof token === "string") {
|
|
129
|
+
const envVar = PROVIDER_KEY_MAP[provider];
|
|
130
|
+
if (envVar) updates[envVar] = token;
|
|
131
|
+
}
|
|
433
132
|
}
|
|
434
|
-
|
|
435
|
-
|
|
133
|
+
return updates;
|
|
134
|
+
} catch {
|
|
135
|
+
return {};
|
|
436
136
|
}
|
|
437
|
-
|
|
438
|
-
return connEnvVarMap;
|
|
439
137
|
}
|
|
440
138
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
* 3. Update secrets.env with API keys and system config
|
|
450
|
-
* 4. Build and write memory config via buildMem0Mapping()
|
|
451
|
-
* 5. Write connection profiles
|
|
452
|
-
* 6. Ensure OpenCode configs
|
|
453
|
-
* 7. Apply install via applyInstall()
|
|
454
|
-
*
|
|
455
|
-
* Does NOT include Docker operations (compose up, pull, etc.) — the caller
|
|
456
|
-
* handles those separately after setup completes.
|
|
457
|
-
*/
|
|
458
|
-
export async function performSetup(
|
|
459
|
-
input: SetupInput,
|
|
460
|
-
assetProvider: CoreAssetProvider,
|
|
461
|
-
opts?: { state?: ControlPlaneState }
|
|
462
|
-
): Promise<SetupResult> {
|
|
463
|
-
// ── Validate ─────────────────────────────────────────────────────────
|
|
464
|
-
const validation = validateSetupInput(input);
|
|
465
|
-
if (!validation.valid) {
|
|
466
|
-
return { ok: false, error: validation.errors.join("; ") };
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
logger.info("performing setup", {
|
|
470
|
-
connectionCount: input.connections.length,
|
|
471
|
-
ollamaEnabled: input.ollamaEnabled,
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
// ── Resolve state ────────────────────────────────────────────────────
|
|
475
|
-
const state = opts?.state ?? createState(input.adminToken);
|
|
476
|
-
|
|
477
|
-
// ── Resolve effective connections (Ollama in-stack override) ──────────
|
|
478
|
-
const effectiveConnections = resolveOllamaUrls(input.connections, input.ollamaEnabled);
|
|
479
|
-
|
|
480
|
-
// ── Build connection env var map ─────────────────────────────────────
|
|
481
|
-
const connEnvVarMap = buildConnectionEnvVarMap(effectiveConnections);
|
|
482
|
-
|
|
483
|
-
// ── Build secrets.env updates ────────────────────────────────────────
|
|
484
|
-
// Pass already-resolved connections to avoid a second resolveOllamaUrls call
|
|
485
|
-
const updates = buildSecretsFromSetup({ ...input, connections: effectiveConnections });
|
|
486
|
-
|
|
487
|
-
// ── Persist secrets.env ──────────────────────────────────────────────
|
|
488
|
-
try {
|
|
489
|
-
ensureXdgDirs();
|
|
490
|
-
ensureSecrets(state);
|
|
491
|
-
ensureConnectionProfilesStore(state.configDir);
|
|
492
|
-
updateSecretsEnv(state, updates);
|
|
493
|
-
} catch (err) {
|
|
494
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
495
|
-
logger.error("failed to update secrets.env", { error: message });
|
|
496
|
-
return { ok: false, error: `Failed to update secrets.env: ${message}` };
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// Update state with new admin token
|
|
500
|
-
state.adminToken = input.adminToken;
|
|
501
|
-
writeSetupTokenFile(state);
|
|
502
|
-
|
|
503
|
-
// ── Build and persist Memory config ──────────────────────────────────
|
|
504
|
-
const llmConnectionId = input.assignments.llm.connectionId;
|
|
505
|
-
const embConnectionId = input.assignments.embeddings.connectionId;
|
|
506
|
-
const llmModel = input.assignments.llm.model;
|
|
507
|
-
const llmSmallModel = input.assignments.llm.smallModel || "";
|
|
508
|
-
const embModel = input.assignments.embeddings.model;
|
|
509
|
-
const embDims = input.assignments.embeddings.embeddingDims || 0;
|
|
510
|
-
|
|
511
|
-
const llmConnection = effectiveConnections.find((c) => c.id === llmConnectionId);
|
|
512
|
-
if (!llmConnection) {
|
|
513
|
-
return { ok: false, error: `LLM connection "${llmConnectionId}" not found in connections list` };
|
|
514
|
-
}
|
|
515
|
-
const embConnection = effectiveConnections.find((c) => c.id === embConnectionId);
|
|
516
|
-
if (!embConnection) {
|
|
517
|
-
return { ok: false, error: `Embeddings connection "${embConnectionId}" not found in connections list` };
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
const memoryModel = llmSmallModel || llmModel;
|
|
521
|
-
|
|
522
|
-
const llmEnvVar = connEnvVarMap.get(llmConnection.id);
|
|
523
|
-
if (!llmEnvVar) {
|
|
524
|
-
return { ok: false, error: `No env var mapping found for LLM connection "${llmConnection.id}"` };
|
|
525
|
-
}
|
|
526
|
-
const llmApiKeyEnvRef = llmConnection.apiKey ? `env:${llmEnvVar}` : "not-needed";
|
|
527
|
-
|
|
528
|
-
const embEnvVar = connEnvVarMap.get(embConnection.id);
|
|
529
|
-
if (!embEnvVar) {
|
|
530
|
-
return { ok: false, error: `No env var mapping found for embeddings connection "${embConnection.id}"` };
|
|
531
|
-
}
|
|
532
|
-
const embApiKeyEnvRef = embConnection.apiKey ? `env:${embEnvVar}` : "not-needed";
|
|
533
|
-
|
|
534
|
-
const embLookupKey = `${embConnection.provider}/${embModel}`;
|
|
535
|
-
const resolvedDims = embDims || EMBEDDING_DIMS[embLookupKey] || 1536;
|
|
536
|
-
|
|
537
|
-
const omConfig = buildMem0Mapping({
|
|
538
|
-
llm: {
|
|
539
|
-
provider: llmConnection.provider,
|
|
540
|
-
baseUrl: llmConnection.baseUrl,
|
|
541
|
-
model: memoryModel,
|
|
542
|
-
apiKeyRef: llmApiKeyEnvRef,
|
|
543
|
-
},
|
|
544
|
-
embedder: {
|
|
545
|
-
provider: embConnection.provider,
|
|
546
|
-
baseUrl: embConnection.baseUrl,
|
|
547
|
-
model: embModel || "text-embedding-3-small",
|
|
548
|
-
apiKeyRef: embApiKeyEnvRef,
|
|
549
|
-
},
|
|
550
|
-
embeddingDims: resolvedDims,
|
|
551
|
-
customInstructions: "",
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
writeMemoryConfig(state.dataDir, omConfig);
|
|
555
|
-
|
|
556
|
-
// ── Write connection profiles ────────────────────────────────────────
|
|
557
|
-
const profilesInput = effectiveConnections.map((conn) => {
|
|
558
|
-
const apiKeyEnvVar = connEnvVarMap.get(conn.id);
|
|
559
|
-
return {
|
|
560
|
-
id: conn.id,
|
|
561
|
-
name: conn.name,
|
|
562
|
-
provider: conn.provider,
|
|
563
|
-
baseUrl: conn.baseUrl,
|
|
564
|
-
hasApiKey: Boolean(conn.apiKey) && Boolean(apiKeyEnvVar),
|
|
565
|
-
apiKeyEnvVar: apiKeyEnvVar ?? "",
|
|
566
|
-
};
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
writeConnectionsDocument(state.configDir, {
|
|
570
|
-
profiles: profilesInput,
|
|
571
|
-
assignments: {
|
|
572
|
-
llm: input.assignments.llm,
|
|
573
|
-
embeddings: {
|
|
574
|
-
connectionId: input.assignments.embeddings.connectionId,
|
|
575
|
-
model: input.assignments.embeddings.model,
|
|
576
|
-
embeddingDims: resolvedDims,
|
|
577
|
-
},
|
|
578
|
-
} as CapabilityAssignments,
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
// ── Ensure OpenCode configs ──────────────────────────────────────────
|
|
582
|
-
ensureOpenCodeConfig();
|
|
583
|
-
ensureOpenCodeSystemConfig(assetProvider);
|
|
584
|
-
ensureAdminOpenCodeConfig(assetProvider);
|
|
585
|
-
ensureMemoryDir();
|
|
586
|
-
|
|
587
|
-
// ── Write stack spec (openpalm.yaml) ─────────────────────────────────
|
|
588
|
-
const stackSpec: StackSpec = {
|
|
589
|
-
version: 3,
|
|
590
|
-
connections: effectiveConnections.map((c) => ({
|
|
591
|
-
id: c.id,
|
|
592
|
-
name: c.name,
|
|
593
|
-
provider: c.provider,
|
|
594
|
-
baseUrl: c.baseUrl,
|
|
595
|
-
})),
|
|
596
|
-
assignments: {
|
|
597
|
-
llm: input.assignments.llm,
|
|
598
|
-
embeddings: {
|
|
599
|
-
connectionId: input.assignments.embeddings.connectionId,
|
|
600
|
-
model: input.assignments.embeddings.model,
|
|
601
|
-
embeddingDims: resolvedDims,
|
|
602
|
-
},
|
|
603
|
-
},
|
|
604
|
-
ollamaEnabled: input.ollamaEnabled,
|
|
605
|
-
...(input.voice ? { voice: input.voice } : {}),
|
|
606
|
-
...(input.channels ? { channels: input.channels } : {}),
|
|
607
|
-
...(input.services ? { services: input.services } : {}),
|
|
139
|
+
export function buildSystemSecretsFromSetup(
|
|
140
|
+
adminToken: string,
|
|
141
|
+
existingSystemEnv: Record<string, string> = {}
|
|
142
|
+
): Record<string, string> {
|
|
143
|
+
return {
|
|
144
|
+
OP_ADMIN_TOKEN: adminToken,
|
|
145
|
+
OP_ASSISTANT_TOKEN: existingSystemEnv.OP_ASSISTANT_TOKEN || randomBytes(32).toString("hex"),
|
|
146
|
+
OP_MEMORY_TOKEN: existingSystemEnv.OP_MEMORY_TOKEN || randomBytes(32).toString("hex"),
|
|
608
147
|
};
|
|
609
|
-
writeStackSpec(state.configDir, stackSpec);
|
|
610
|
-
|
|
611
|
-
// ── Mark setup complete in DATA_HOME stack.env before staging ────────
|
|
612
|
-
const dataStackEnv = `${state.dataDir}/stack.env`;
|
|
613
|
-
mkdirSync(state.dataDir, { recursive: true });
|
|
614
|
-
const stackBase = existsSync(dataStackEnv) ? readFileSync(dataStackEnv, "utf-8") : "";
|
|
615
|
-
writeFileSync(
|
|
616
|
-
dataStackEnv,
|
|
617
|
-
mergeEnvContent(stackBase, {
|
|
618
|
-
OPENPALM_SETUP_COMPLETE: "true",
|
|
619
|
-
OPENPALM_OLLAMA_ENABLED: input.ollamaEnabled ? "true" : "false",
|
|
620
|
-
OPENPALM_ADMIN_ENABLED: input.services?.admin ? "true" : "false",
|
|
621
|
-
})
|
|
622
|
-
);
|
|
623
|
-
|
|
624
|
-
// ── Apply install (stages artifacts, no Docker) ──────────────────────
|
|
625
|
-
applyInstall(state, assetProvider);
|
|
626
|
-
|
|
627
|
-
logger.info("setup complete", {
|
|
628
|
-
connectionCount: input.connections.length,
|
|
629
|
-
llmProvider: llmConnection.provider,
|
|
630
|
-
llmModel,
|
|
631
|
-
embModel,
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
return { ok: true };
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
// ── Provider Detection ───────────────────────────────────────────────────
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Detect available local providers in a setup-friendly format.
|
|
641
|
-
* Wraps detectLocalProviders() from model-runner.ts.
|
|
642
|
-
*/
|
|
643
|
-
export async function detectProviders(): Promise<DetectedProvider[]> {
|
|
644
|
-
const raw = await detectLocalProviders();
|
|
645
|
-
return raw.map((r: LocalProviderDetection) => ({
|
|
646
|
-
provider: r.provider,
|
|
647
|
-
url: r.url,
|
|
648
|
-
available: r.available,
|
|
649
|
-
}));
|
|
650
148
|
}
|
|
651
149
|
|
|
652
150
|
// ── Channel Credential Env Var Mapping ───────────────────────────────────
|
|
653
151
|
|
|
654
|
-
|
|
655
|
-
export const CHANNEL_CREDENTIAL_ENV_MAP: Record<string, Record<string, string>> = {
|
|
152
|
+
const CHANNEL_CREDENTIAL_ENV_MAP: Record<string, Record<string, string>> = {
|
|
656
153
|
discord: {
|
|
657
154
|
botToken: "DISCORD_BOT_TOKEN",
|
|
658
155
|
applicationId: "DISCORD_APPLICATION_ID",
|
|
@@ -671,286 +168,99 @@ export const CHANNEL_CREDENTIAL_ENV_MAP: Record<string, Record<string, string>>
|
|
|
671
168
|
},
|
|
672
169
|
};
|
|
673
170
|
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
return { valid: false, errors: ["Config must be a non-null object"] };
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
const body = config as Record<string, unknown>;
|
|
688
|
-
|
|
689
|
-
// version
|
|
690
|
-
if (body.version !== 1) {
|
|
691
|
-
errors.push("version must be 1");
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
// owner (optional)
|
|
695
|
-
if (body.owner !== undefined) {
|
|
696
|
-
if (typeof body.owner !== "object" || body.owner === null) {
|
|
697
|
-
errors.push("owner must be an object if provided");
|
|
698
|
-
} else {
|
|
699
|
-
const owner = body.owner as Record<string, unknown>;
|
|
700
|
-
if (owner.name !== undefined && typeof owner.name !== "string") {
|
|
701
|
-
errors.push("owner.name must be a string if provided");
|
|
702
|
-
}
|
|
703
|
-
if (owner.email !== undefined && typeof owner.email !== "string") {
|
|
704
|
-
errors.push("owner.email must be a string if provided");
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
// security
|
|
710
|
-
if (typeof body.security !== "object" || body.security === null) {
|
|
711
|
-
errors.push("security object is required");
|
|
712
|
-
} else {
|
|
713
|
-
const security = body.security as Record<string, unknown>;
|
|
714
|
-
if (typeof security.adminToken !== "string" || !security.adminToken) {
|
|
715
|
-
errors.push("security.adminToken is required and must be a non-empty string");
|
|
716
|
-
} else if (security.adminToken.length < 8) {
|
|
717
|
-
errors.push("security.adminToken must be at least 8 characters");
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
// connections
|
|
722
|
-
validateConnectionsArray(body.connections, errors);
|
|
723
|
-
|
|
724
|
-
// assignments
|
|
725
|
-
validateAssignmentsBlock(body.assignments, body.connections, errors);
|
|
726
|
-
|
|
727
|
-
// channels (optional)
|
|
728
|
-
if (body.channels !== undefined) {
|
|
729
|
-
if (typeof body.channels !== "object" || body.channels === null) {
|
|
730
|
-
errors.push("channels must be an object if provided");
|
|
731
|
-
} else {
|
|
732
|
-
const channels = body.channels as Record<string, unknown>;
|
|
733
|
-
for (const [channelId, value] of Object.entries(channels)) {
|
|
734
|
-
if (typeof value === "boolean") continue;
|
|
735
|
-
if (typeof value !== "object" || value === null) {
|
|
736
|
-
errors.push(`channels.${channelId} must be a boolean or object`);
|
|
737
|
-
continue;
|
|
738
|
-
}
|
|
739
|
-
// Channel-specific credential validation
|
|
740
|
-
if (channelId === "discord") {
|
|
741
|
-
const creds = value as Record<string, unknown>;
|
|
742
|
-
if (creds.enabled !== false && !creds.botToken) {
|
|
743
|
-
errors.push("channels.discord.botToken is required when discord is enabled");
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
if (channelId === "slack") {
|
|
747
|
-
const creds = value as Record<string, unknown>;
|
|
748
|
-
if (creds.enabled !== false) {
|
|
749
|
-
if (!creds.slackBotToken) {
|
|
750
|
-
errors.push("channels.slack.slackBotToken is required when slack is enabled");
|
|
751
|
-
}
|
|
752
|
-
if (!creds.slackAppToken) {
|
|
753
|
-
errors.push("channels.slack.slackAppToken is required when slack is enabled");
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
// services (optional)
|
|
762
|
-
if (body.services !== undefined) {
|
|
763
|
-
if (typeof body.services !== "object" || body.services === null) {
|
|
764
|
-
errors.push("services must be an object if provided");
|
|
765
|
-
} else {
|
|
766
|
-
const services = body.services as Record<string, unknown>;
|
|
767
|
-
for (const [key, val] of Object.entries(services)) {
|
|
768
|
-
if (typeof val === "boolean") continue;
|
|
769
|
-
if (typeof val !== "object" || val === null) {
|
|
770
|
-
errors.push(`services.${key} must be a boolean or object`);
|
|
771
|
-
} else if (typeof (val as Record<string, unknown>).enabled !== "boolean") {
|
|
772
|
-
errors.push(`services.${key}.enabled must be a boolean`);
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
// memory (optional)
|
|
779
|
-
if (body.memory !== undefined) {
|
|
780
|
-
if (typeof body.memory !== "object" || body.memory === null) {
|
|
781
|
-
errors.push("memory must be an object if provided");
|
|
782
|
-
} else {
|
|
783
|
-
const memory = body.memory as Record<string, unknown>;
|
|
784
|
-
if (memory.userId !== undefined && typeof memory.userId !== "string") {
|
|
785
|
-
errors.push("memory.userId must be a string if provided");
|
|
786
|
-
}
|
|
787
|
-
if (typeof memory.userId === "string" && !/^[A-Za-z0-9_]+$/.test(memory.userId)) {
|
|
788
|
-
errors.push("memoryUserId contains invalid characters (alphanumeric and underscores only)");
|
|
789
|
-
}
|
|
171
|
+
function buildChannelCredentialEnvVars(
|
|
172
|
+
channelCredentials: Record<string, Record<string, string>>
|
|
173
|
+
): Record<string, string> {
|
|
174
|
+
const envVars: Record<string, string> = {};
|
|
175
|
+
for (const [channelId, creds] of Object.entries(channelCredentials)) {
|
|
176
|
+
const mapping = CHANNEL_CREDENTIAL_ENV_MAP[channelId];
|
|
177
|
+
if (!mapping) continue;
|
|
178
|
+
for (const [field, envKey] of Object.entries(mapping)) {
|
|
179
|
+
const val = creds[field];
|
|
180
|
+
if (typeof val === "string" && val) envVars[envKey] = val;
|
|
790
181
|
}
|
|
791
182
|
}
|
|
792
|
-
|
|
793
|
-
return { valid: errors.length === 0, errors };
|
|
183
|
+
return envVars;
|
|
794
184
|
}
|
|
795
185
|
|
|
796
|
-
// ──
|
|
186
|
+
// ── Core Setup Orchestration ─────────────────────────────────────────────
|
|
797
187
|
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
let tts: string | undefined;
|
|
805
|
-
let stt: string | undefined;
|
|
188
|
+
export async function performSetup(
|
|
189
|
+
input: SetupSpec,
|
|
190
|
+
opts?: { state?: ControlPlaneState }
|
|
191
|
+
): Promise<SetupResult> {
|
|
192
|
+
const validation = validateSetupSpec(input);
|
|
193
|
+
if (!validation.valid) return { ok: false, error: validation.errors.join("; ") };
|
|
806
194
|
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
// them, the caller should write the original SetupConfig to
|
|
811
|
-
// openpalm.yaml separately.
|
|
812
|
-
tts = typeof config.assignments.tts === "string"
|
|
813
|
-
? config.assignments.tts
|
|
814
|
-
: config.assignments.tts.engine;
|
|
815
|
-
}
|
|
195
|
+
const { capabilities, security, owner, connections, channelCredentials } = input;
|
|
196
|
+
const state = opts?.state ?? createState(security.adminToken);
|
|
197
|
+
const ollamaEnabled = listEnabledAddonIds(state.homeDir).includes("ollama");
|
|
816
198
|
|
|
817
|
-
|
|
818
|
-
// Same as tts above — connectionId and model are dropped during
|
|
819
|
-
// normalization and not written to the stack spec by performSetup().
|
|
820
|
-
stt = typeof config.assignments.stt === "string"
|
|
821
|
-
? config.assignments.stt
|
|
822
|
-
: config.assignments.stt.engine;
|
|
823
|
-
}
|
|
199
|
+
logger.info("performing setup", { capabilityCount: connections.length, ollamaEnabled });
|
|
824
200
|
|
|
825
|
-
//
|
|
826
|
-
const
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
}
|
|
201
|
+
// Apply Ollama in-stack URL override when addon is enabled
|
|
202
|
+
const effectiveConnections = ollamaEnabled
|
|
203
|
+
? connections.map((c) => c.provider === "ollama" ? { ...c, baseUrl: OLLAMA_INSTACK_URL } : c)
|
|
204
|
+
: connections;
|
|
205
|
+
const updates = buildSecretsFromSetup(effectiveConnections, owner);
|
|
206
|
+
|
|
207
|
+
// Merge OAuth-authenticated provider keys from auth.json
|
|
208
|
+
// (OAuth flows store tokens in auth.json, not in the setup payload)
|
|
209
|
+
const oauthKeys = extractAuthJsonKeys(state.vaultDir);
|
|
210
|
+
for (const [key, value] of Object.entries(oauthKeys)) {
|
|
211
|
+
// Only fill in keys that weren't already provided via API key entry
|
|
212
|
+
if (!updates[key]) updates[key] = value;
|
|
838
213
|
}
|
|
839
214
|
|
|
840
|
-
//
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
215
|
+
// Persist vault env files
|
|
216
|
+
try {
|
|
217
|
+
ensureHomeDirs();
|
|
218
|
+
ensureSecrets(state);
|
|
219
|
+
const existingSystemEnv = readStackEnv(state.vaultDir);
|
|
220
|
+
if (channelCredentials) Object.assign(updates, buildChannelCredentialEnvVars(channelCredentials));
|
|
221
|
+
// Pick up channel credential env vars not already provided in the spec
|
|
222
|
+
for (const mapping of Object.values(CHANNEL_CREDENTIAL_ENV_MAP)) {
|
|
223
|
+
for (const envKey of Object.values(mapping)) {
|
|
224
|
+
if (!updates[envKey] && process.env[envKey]) updates[envKey] = process.env[envKey];
|
|
848
225
|
}
|
|
849
226
|
}
|
|
227
|
+
updateSecretsEnv(state, updates);
|
|
228
|
+
updateSystemSecretsEnv(state, buildSystemSecretsFromSetup(security.adminToken, existingSystemEnv));
|
|
229
|
+
} catch (err) {
|
|
230
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
231
|
+
logger.error("failed to update vault env files", { error: message });
|
|
232
|
+
return { ok: false, error: `Failed to update vault env files: ${message}` };
|
|
850
233
|
}
|
|
851
234
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
return {
|
|
856
|
-
adminToken: config.security.adminToken,
|
|
857
|
-
ownerName: config.owner?.name,
|
|
858
|
-
ownerEmail: config.owner?.email,
|
|
859
|
-
memoryUserId: config.memory?.userId || "default_user",
|
|
860
|
-
ollamaEnabled,
|
|
861
|
-
connections: config.connections,
|
|
862
|
-
assignments: {
|
|
863
|
-
llm: config.assignments.llm,
|
|
864
|
-
embeddings: config.assignments.embeddings,
|
|
865
|
-
},
|
|
866
|
-
...(tts !== undefined || stt !== undefined ? { voice: { tts, stt } } : {}),
|
|
867
|
-
...(enabledChannels.length > 0 ? { channels: enabledChannels } : {}),
|
|
868
|
-
...(Object.keys(enabledServices).length > 0 ? { services: enabledServices as SetupInput["services"] } : {}),
|
|
869
|
-
};
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
// ── Channel Credential Env Var Builder ───────────────────────────────────
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* Extract credential fields from typed channel configs using
|
|
876
|
-
* CHANNEL_CREDENTIAL_ENV_MAP and return a Record<string, string> suitable
|
|
877
|
-
* for writing to secrets.env.
|
|
878
|
-
*
|
|
879
|
-
* Unknown channels (not in CHANNEL_CREDENTIAL_ENV_MAP) are skipped.
|
|
880
|
-
* Boolean values are converted to strings.
|
|
881
|
-
*/
|
|
882
|
-
export function buildChannelCredentialEnvVars(
|
|
883
|
-
channels: Record<string, boolean | ChannelCredentials> | undefined
|
|
884
|
-
): Record<string, string> {
|
|
885
|
-
const envVars: Record<string, string> = {};
|
|
886
|
-
if (!channels) return envVars;
|
|
235
|
+
state.adminToken = security.adminToken;
|
|
236
|
+
state.assistantToken = readStackEnv(state.vaultDir).OP_ASSISTANT_TOKEN ?? state.assistantToken;
|
|
237
|
+
writeSetupTokenFile(state);
|
|
887
238
|
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
if (typeof value !== "object" || value === null) continue;
|
|
239
|
+
// Write stack.yml and OP_CAP_* capability vars to stack.env
|
|
240
|
+
writeMemoryAndStackConfigs({ version: 2, capabilities }, state);
|
|
891
241
|
|
|
892
|
-
|
|
893
|
-
|
|
242
|
+
ensureOpenCodeConfig();
|
|
243
|
+
ensureOpenCodeSystemConfig();
|
|
244
|
+
ensureMemoryDir();
|
|
894
245
|
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
if (typeof fieldValue === "boolean") {
|
|
900
|
-
envVars[envKey] = String(fieldValue);
|
|
901
|
-
} else if (typeof fieldValue === "string" && fieldValue) {
|
|
902
|
-
envVars[envKey] = fieldValue;
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
}
|
|
246
|
+
// Mark setup complete in vault/stack/stack.env (where isSetupComplete reads it)
|
|
247
|
+
const systemEnvPath = `${state.vaultDir}/stack/stack.env`;
|
|
248
|
+
const systemBase = existsSync(systemEnvPath) ? readFileSync(systemEnvPath, "utf-8") : "";
|
|
249
|
+
writeFileSync(systemEnvPath, mergeEnvContent(systemBase, { OP_SETUP_COMPLETE: "true" }), { mode: 0o600 });
|
|
906
250
|
|
|
907
|
-
|
|
251
|
+
logger.info("setup complete", { capabilityCount: connections.length });
|
|
252
|
+
return { ok: true };
|
|
908
253
|
}
|
|
909
254
|
|
|
910
|
-
|
|
255
|
+
/** Write stack.yml and OP_CAP_* capability vars to stack.env from the spec's capabilities. */
|
|
256
|
+
function writeMemoryAndStackConfigs(spec: StackSpec, state: ControlPlaneState): void {
|
|
257
|
+
const { provider: embProvider, model: embModel } = spec.capabilities.embeddings;
|
|
258
|
+
const resolvedDims = spec.capabilities.embeddings.dims || EMBEDDING_DIMS[`${embProvider}/${embModel}`] || 1536;
|
|
911
259
|
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
* Channel credentials are written BEFORE performSetup() so that when
|
|
919
|
-
* performSetup() stages secrets.env from CONFIG_HOME to STATE_HOME
|
|
920
|
-
* (via applyInstall), the staged copy already contains channel creds.
|
|
921
|
-
*/
|
|
922
|
-
export async function performSetupFromConfig(
|
|
923
|
-
config: SetupConfig,
|
|
924
|
-
assetProvider: CoreAssetProvider,
|
|
925
|
-
opts?: { state?: ControlPlaneState }
|
|
926
|
-
): Promise<SetupResult> {
|
|
927
|
-
// ── Validate ─────────────────────────────────────────────────────────
|
|
928
|
-
const validation = validateSetupConfig(config);
|
|
929
|
-
if (!validation.valid) {
|
|
930
|
-
return { ok: false, error: validation.errors.join("; ") };
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
// ── Write channel credentials to CONFIG_HOME/secrets.env FIRST ─────
|
|
934
|
-
// This must happen before performSetup() which stages secrets.env
|
|
935
|
-
// from CONFIG_HOME to STATE_HOME via applyInstall().
|
|
936
|
-
const input = normalizeToSetupInput(config);
|
|
937
|
-
const state = opts?.state ?? createState(config.security.adminToken);
|
|
938
|
-
const channelEnvVars = buildChannelCredentialEnvVars(config.channels);
|
|
939
|
-
if (Object.keys(channelEnvVars).length > 0) {
|
|
940
|
-
try {
|
|
941
|
-
ensureXdgDirs();
|
|
942
|
-
ensureSecrets(state);
|
|
943
|
-
updateSecretsEnv(state, channelEnvVars);
|
|
944
|
-
} catch (err) {
|
|
945
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
946
|
-
logger.error("failed to write channel credentials to secrets.env", { error: message });
|
|
947
|
-
return { ok: false, error: `Failed to write channel credentials: ${message}` };
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
// ── Normalize and delegate to performSetup ─────────────────────────
|
|
952
|
-
// performSetup() writes its own secrets (admin token, API keys, etc.)
|
|
953
|
-
// and then stages the now-complete secrets.env to STATE_HOME.
|
|
954
|
-
const result = await performSetup(input, assetProvider, { ...opts, state });
|
|
955
|
-
return result;
|
|
260
|
+
const specToWrite: StackSpec = {
|
|
261
|
+
...spec,
|
|
262
|
+
capabilities: { ...spec.capabilities, embeddings: { ...spec.capabilities.embeddings, dims: resolvedDims } },
|
|
263
|
+
};
|
|
264
|
+
writeStackSpec(state.configDir, specToWrite);
|
|
265
|
+
writeCapabilityVars(specToWrite, state.vaultDir);
|
|
956
266
|
}
|