History

Update environment configuration and streamline development scripts

- Consolidated environment variable management by centralizing all variables in the root .env file. - Updated development scripts across various packages to utilize the new environment loading mechanism. - Enhanced error handling for missing environment variables in the bot and server packages. - Adjusted Next.js configuration to map environment variables for client-side access.

Rhys Sullivan committed 9 months ago

Commit cd0d0db1 parent c24971b10 files · +60 −66Browse files

Unified · Split

10 files
  1. .env.example +17 −16
  2. assistant/packages/bot/package.json +3 −2
  3. assistant/packages/bot/src/index.ts +4 −1
  4. assistant/packages/server/package.json +3 −2
  5. assistant/packages/server/src/index.ts +4 −1
  6. dev.ts +11 −36
  7. executor/apps/web/next.config.ts +6 −0
  8. executor/apps/web/package.json +4 −3
  9. executor/apps/web/src/lib/convex-provider.tsx +4 −1
  10. package.json +4 −4
.env.example+17−16
@@ -1,24 +1,25 @@
1−# Required for the assistant to call Claude
2−ANTHROPIC_API_KEY=sk-ant-...
3−# Or use Claude Max OAuth token instead:
4−# ANTHROPIC_OAUTH_TOKEN=...
1+# All env vars live in this single root .env file.
2+# Sub-packages load it via `with-env` scripts (bun --env-file).
3+# Next.js vars are mapped from canonical names in next.config.ts.
54
6−# Required for the Discord bot
7−DISCORD_BOT_TOKEN=...
5+# ── Convex (required) ──
6+CONVEX_DEPLOYMENT=dev:your-deployment-slug
7+CONVEX_URL=https://your-deployment.convex.cloud
8+CONVEX_SITE_URL=https://your-deployment.convex.site
89
9−# Optional: executor URL (defaults to http://localhost:4001)
10−# EXECUTOR_URL=http://localhost:4001
10+# ── WorkOS (optional — enables auth, omit for guest-only mode) ──
11+# WORKOS_CLIENT_ID=client_...
12+# WORKOS_API_KEY=sk_test_...
13+# WORKOS_WEBHOOK_SECRET=...
1114
12−# Optional: assistant server URL for the bot (defaults to http://localhost:3000)
13−# ASSISTANT_SERVER_URL=http://localhost:3000
15+# ── Discord bot (optional — omit to skip bot startup) ──
16+# DISCORD_BOT_TOKEN=...
1417
15−# Optional: assistant server port (defaults to 3000)
16−# PORT=3000
18+# ── Assistant (optional) ──
19+# OPENASSISTANT_EXECUTOR_URL=http://localhost:3001
20+# OPENASSISTANT_CALLBACK_BASE_URL=http://localhost:3000
1721
18−# Optional: executor server port (defaults to 4001)
19−# Configured in executor/.env or as env var
20−
21−# Optional: tool source API keys
22+# ── Tool source API keys (optional) ──
2223 # POSTHOG_PERSONAL_API_KEY=...
2324 # POSTHOG_PROJECT_ID=...
2425 # OPENASSISTANT_GITHUB_TOKEN=...
assistant/packages/bot/package.json+3−2
@@ -3,8 +3,9 @@ · 2 unmodified lines
33 "private": true,
44 "type": "module",
55 "scripts": {
6− "dev": "bun --hot src/index.ts",
7− "start": "bun src/index.ts"
6+ "with-env": "bun run --env-file=../../../.env --",
7+ "dev": "bun with-env bun --hot src/index.ts",
8+ "start": "bun with-env bun src/index.ts"
89 },
910 "dependencies": {
1011 "@assistant/core": "workspace:*",
assistant/packages/bot/src/index.ts+4−1
@@ -27,7 +27,10 @@ if (!DISCORD_TOKEN) { · 26 unmodified lines
2727 }
2828
2929 const SERVER_URL = Bun.env.ASSISTANT_SERVER_URL ?? "http://localhost:3000";
30−const CONVEX_URL = Bun.env.CONVEX_URL ?? "http://127.0.0.1:3210";
30+const CONVEX_URL = Bun.env.CONVEX_URL;
31+if (!CONVEX_URL) {
32+ throw new Error("CONVEX_URL is required. Set it in your environment.");
33+}
3134
3235 // ---------------------------------------------------------------------------
3336 // Clients
assistant/packages/server/package.json+3−2
@@ -7,8 +7,9 @@ · 6 unmodified lines
77 "./client": "./src/client.ts"
88 },
99 "scripts": {
10− "dev": "bun --hot src/index.ts",
11− "start": "bun src/index.ts"
10+ "with-env": "bun run --env-file=../../../.env --",
11+ "dev": "bun with-env bun --hot src/index.ts",
12+ "start": "bun with-env bun src/index.ts"
1213 },
1314 "dependencies": {
1415 "@assistant/core": "workspace:*",
assistant/packages/server/src/index.ts+4−1
@@ -7,7 +7,10 @@ import { ConvexHttpClient } from "convex/browser"; · 6 unmodified lines
77 import { api } from "@executor/convex/_generated/api";
88
99 const PORT = Number(Bun.env.PORT ?? 3000);
10−const CONVEX_URL = Bun.env.CONVEX_URL ?? "http://127.0.0.1:3210";
10+const CONVEX_URL = Bun.env.CONVEX_URL;
11+if (!CONVEX_URL) {
12+ throw new Error("CONVEX_URL is required. Set it in your environment.");
13+}
1114 const EXECUTOR_URL = Bun.env.EXECUTOR_URL
1215 ?? Bun.env.CONVEX_SITE_URL
1316 ?? (CONVEX_URL.includes(".convex.cloud")
dev.ts+11−36
@@ -3,6 +3,8 @@ · 2 unmodified lines
33 *
44 * Usage: bun dev
55 *
6+ * Reads all configuration from the root .env file (auto-loaded by Bun).
7+ *
68 * Starts:
79 * 1. Convex cloud dev function push (once)
810 * 2. Convex cloud dev function watcher
@@ -36,39 +38,12 @@ function toSiteUrl(convexUrl: string): string { · 27 unmodified lines
3638 return convexUrl;
3739 }
3840
39−async function resolveExecutorUrls(): Promise<{ convexUrl: string; executorUrl: string }> {
40− if (Bun.env.CONVEX_URL) {
41− const convexUrl = Bun.env.CONVEX_URL;
42− const executorUrl = Bun.env.CONVEX_SITE_URL ?? toSiteUrl(convexUrl);
43− return { convexUrl, executorUrl };
44− }
45−
46− const envFile = Bun.file("./executor/.env.local");
47− if (!(await envFile.exists())) {
48− throw new Error("Missing executor/.env.local with CONVEX_URL");
49− }
50−
51− const content = await envFile.text();
52− const lines = content
53− .split("\n")
54− .map((entry) => entry.trim());
55−
56− const convexLine = lines.find((entry) => entry.startsWith("CONVEX_URL="));
57− const siteLine = lines.find((entry) => entry.startsWith("CONVEX_SITE_URL="));
58−
59− if (!convexLine) {
60− throw new Error("CONVEX_URL not found in executor/.env.local");
61− }
62−
63− const convexUrl = convexLine.slice("CONVEX_URL=".length).trim();
41+function resolveExecutorUrls(): { convexUrl: string; executorUrl: string } {
42+ const convexUrl = Bun.env.CONVEX_URL;
6443 if (!convexUrl) {
65− throw new Error("CONVEX_URL is empty in executor/.env.local");
44+ throw new Error("CONVEX_URL is not set. Add it to the root .env file.");
6645 }
67−
68− const executorUrl = siteLine
69− ? siteLine.slice("CONVEX_SITE_URL=".length).trim()
70− : toSiteUrl(convexUrl);
71−
46+ const executorUrl = Bun.env.CONVEX_SITE_URL ?? toSiteUrl(convexUrl);
7247 return { convexUrl, executorUrl };
7348 }
7449
@@ -161,7 +136,7 @@ if (!Bun.env.DISCORD_BOT_TOKEN) { · 86 unmodified lines
161136 // 1. Push functions (must complete before executor starts)
162137 await pushConvexFunctions();
163138
164−const urls = await resolveExecutorUrls();
139+const urls = resolveExecutorUrls();
165140 console.log(prefix("convex", `Using Convex URL: ${urls.convexUrl}`));
166141 console.log(prefix("convex", `Using executor HTTP URL: ${urls.executorUrl}`));
167142
@@ -181,8 +156,8 @@ spawnService("web", ["bun", "run", "dev", "--", "-p", "3002"], { · 13 unmodified lines
181156 // Small delay for web to be ready
182157 await Bun.sleep(1200);
183158
184−spawnService("assistant", ["bun", "run", "--cwd", "packages/server", "dev"], {
185− cwd: "./assistant",
159+spawnService("assistant", ["bun", "run", "dev"], {
160+ cwd: "./assistant/packages/server",
186161 env: {
187162 EXECUTOR_URL: urls.executorUrl,
188163 CONVEX_URL: urls.convexUrl,
@@ -193,8 +168,8 @@ spawnService("assistant", ["bun", "run", "--cwd", "packages/server", "dev"], { · 4 unmodified lines
193168
194169 if (Bun.env.DISCORD_BOT_TOKEN) {
195170 await Bun.sleep(1000);
196− spawnService("bot", ["bun", "run", "--cwd", "packages/bot", "dev"], {
197− cwd: "./assistant",
171+ spawnService("bot", ["bun", "run", "dev"], {
172+ cwd: "./assistant/packages/bot",
198173 env: {
199174 CONVEX_URL: urls.convexUrl,
200175 },
executor/apps/web/next.config.ts+6−0
@@ -1,6 +1,12 @@
11 import type { NextConfig } from "next";
22 const nextConfig: NextConfig = {
33 transpilePackages: ["@executor/contracts"],
4+ env: {
5+ // Map canonical env vars to NEXT_PUBLIC_ so they're available client-side.
6+ // This lets us keep a single root .env without NEXT_PUBLIC_ prefixes.
7+ NEXT_PUBLIC_CONVEX_URL: process.env.CONVEX_URL,
8+ NEXT_PUBLIC_WORKOS_CLIENT_ID: process.env.WORKOS_CLIENT_ID,
9+ },
410 };
511
612 export default nextConfig;
executor/apps/web/package.json+4−3
@@ -3,9 +3,10 @@ · 2 unmodified lines
33 "version": "0.1.0",
44 "private": true,
55 "scripts": {
6− "dev": "next dev",
7− "build": "next build",
8− "start": "next start",
6+ "with-env": "bun run --env-file=../../../.env --",
7+ "dev": "bun with-env next dev",
8+ "build": "bun with-env next build",
9+ "start": "bun with-env next start",
910 "lint": "eslint"
1011 },
1112 "dependencies": {
executor/apps/web/src/lib/convex-provider.tsx+4−1
@@ -10,7 +10,10 @@ import { ConvexProvider, ConvexReactClient } from "convex/react"; · 9 unmodified lines
1010 import type { ReactNode } from "react";
1111 import { workosEnabled } from "@/lib/auth-capabilities";
1212
13−const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL ?? "http://127.0.0.1:3210";
13+const convexUrl = process.env.NEXT_PUBLIC_CONVEX_URL;
14+if (!convexUrl) {
15+ throw new Error("CONVEX_URL is not set. Add it to the root .env file.");
16+}
1417 const convexClient = new ConvexReactClient(convexUrl, {
1518 unsavedChangesWarning: false,
1619 });
package.json+4−4
@@ -10,18 +10,18 @@ · 9 unmodified lines
1010 "assistant/packages/reacord"
1111 ],
1212 "scripts": {
13+ "with-env": "bun run --env-file=.env --",
1314 "dev": "bun run dev.ts",
14− "dev:executor": "bun run dev:executor:convex",
1515 "dev:executor:convex": "bunx --cwd executor convex dev",
1616 "dev:executor:web": "bun run --cwd executor/apps/web dev",
1717 "dev:executor:all": "bun run dev:executor:convex & bun run dev:executor:web",
18− "dev:assistant": "bun run --cwd assistant dev:server",
19− "dev:bot": "bun run --cwd assistant dev:bot",
18+ "dev:assistant": "bun run --cwd assistant/packages/server dev",
19+ "dev:bot": "bun run --cwd assistant/packages/bot dev",
2020 "test": "bun run test:executor && bun run test:assistant",
2121 "test:executor": "bun test --cwd executor",
2222 "test:assistant": "bun run --cwd assistant test",
2323 "typecheck:executor": "bunx tsc --noEmit -p executor/tsconfig.json",
24− "db:clear:executor": "bun run executor/wipe-convex-db.ts",
24+ "db:clear:executor": "bun with-env bun run executor/wipe-convex-db.ts",
2525 "convex:codegen": "bunx --cwd executor convex codegen"
2626 },
2727 "dependencies": {