moi-computer 0.1.4 → 0.2.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 +26 -52
- package/client/api/workspaces.ts +3 -5
- package/client/components/Scratchpad.tsx +118 -9
- package/client/components/settings/SettingsPages.tsx +8 -60
- package/client/components/tool-group/fixtures.ts +60 -66
- package/client/hooks/useEnvVars.ts +20 -0
- package/client/hooks/useMeiEvents.ts +2 -0
- package/dist/chunk-90bceh8y.js +672 -0
- package/dist/chunk-9mezmjpz.css +1 -0
- package/dist/index.html +1 -1
- package/lib/scratchpad-skew.ts +64 -0
- package/lib/types.ts +5 -7
- package/package.json +3 -3
- package/server/api.ts +15 -54
- package/server/cc-session.ts +1 -1
- package/server/cli-env.ts +238 -0
- package/server/cli.ts +172 -0
- package/server/control.ts +11 -0
- package/server/data-dir.ts +9 -0
- package/server/env-apply.ts +16 -0
- package/server/functions-worker.ts +2 -0
- package/server/functions.ts +101 -3
- package/server/registry.ts +1 -2
- package/server/required-env.ts +19 -0
- package/server/scratchpad-executor.ts +39 -6
- package/server/scratchpad.ts +44 -3
- package/server/status.ts +54 -3
- package/server/thread-config.ts +2 -3
- package/server/vendor.ts +15 -8
- package/server/workspace-env.ts +69 -80
- package/workspace/.claude/skills/moi-workspace/SKILL.md +39 -6
- package/dist/chunk-kc6e9nd4.css +0 -1
- package/dist/chunk-yf3p7qg7.js +0 -673
package/server/workspace-env.ts
CHANGED
|
@@ -4,23 +4,23 @@
|
|
|
4
4
|
// Two sources feed a workspace's effective env:
|
|
5
5
|
// 1. Discovered `.env` files in the workspace root (`.env`, then `.env.local`,
|
|
6
6
|
// local winning). Parsed with node:util's built-in `parseEnv` — no dep.
|
|
7
|
-
// `.env` flows to BOTH sinks (it's the workspace's own file).
|
|
8
7
|
// 2. UI-managed custom secrets. These never touch the repo. Secret VALUES are
|
|
9
8
|
// stored via the OS keychain (`Bun.secrets`) when available, else a 0600
|
|
10
|
-
// file fallback — see SecretStore.
|
|
11
|
-
// (widgets / agent / both) so a secret can be kept out of the agent's
|
|
12
|
-
// bypass-permissions Bash. Custom wins over `.env`.
|
|
9
|
+
// file fallback — see SecretStore. Custom wins over `.env`.
|
|
13
10
|
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
|
|
11
|
+
// There is ONE effective env per workspace — every key flows to every sink
|
|
12
|
+
// (widgets, agent, `moi env exec`).
|
|
13
|
+
//
|
|
14
|
+
// Non-secret metadata (the `inheritDotenv` mode flag) lives in a small JSON
|
|
15
|
+
// file in the OS data dir. Secret values live in the SecretStore. The two are
|
|
16
|
+
// keyed by the absolute workspace path.
|
|
19
17
|
import { chmod, mkdir, rename } from 'node:fs/promises'
|
|
20
18
|
import { dirname, join, resolve } from 'node:path'
|
|
21
19
|
import { parseEnv } from 'node:util'
|
|
22
20
|
|
|
23
|
-
import type {
|
|
21
|
+
import type { WorkspaceEnvView } from '@/lib/types'
|
|
22
|
+
|
|
23
|
+
import { DATA_DIR } from './data-dir'
|
|
24
24
|
|
|
25
25
|
// Dotenv files scanned in the workspace root, low → high precedence. A later
|
|
26
26
|
// file's keys override an earlier file's (base `.env`, machine-local `.env.local`).
|
|
@@ -28,21 +28,11 @@ const DOTENV_FILES = ['.env', '.env.local'] as const
|
|
|
28
28
|
|
|
29
29
|
// Valid POSIX-ish env var name. Used to reject junk keys from the API.
|
|
30
30
|
const ENV_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
31
|
-
const SCOPES: readonly EnvScope[] = ['widgets', 'agent', 'both']
|
|
32
31
|
|
|
33
32
|
export function isValidEnvKey(key: string): boolean {
|
|
34
33
|
return ENV_KEY_RE.test(key)
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
export function isValidScope(scope: unknown): scope is EnvScope {
|
|
38
|
-
return typeof scope === 'string' && (SCOPES as readonly string[]).includes(scope)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Where a key may flow, given its sink. `agent`/`widgets` only match their own
|
|
42
|
-
// sink; `both` matches either.
|
|
43
|
-
type Sink = 'widgets' | 'agent'
|
|
44
|
-
|
|
45
|
-
const DATA_DIR = envPaths('moi', { suffix: false }).data
|
|
46
36
|
const DEFAULT_META_PATH = join(DATA_DIR, 'workspace-env.json')
|
|
47
37
|
const DEFAULT_SECRET_PATH = join(DATA_DIR, 'workspace-secrets.json')
|
|
48
38
|
// Keychain namespace for Bun.secrets entries.
|
|
@@ -103,7 +93,7 @@ export function setWorkspaceEnvStorePath(metaPath: string, secretPath?: string)
|
|
|
103
93
|
export function resetWorkspaceEnvForTest() {
|
|
104
94
|
_metaPath = DEFAULT_META_PATH
|
|
105
95
|
_secretFilePath = DEFAULT_SECRET_PATH
|
|
106
|
-
setSecretStoreBackend(
|
|
96
|
+
setSecretStoreBackend(DEFAULT_BACKEND)
|
|
107
97
|
}
|
|
108
98
|
|
|
109
99
|
// Serialize writes per workspace path so two concurrent PUTs can't lose an
|
|
@@ -192,7 +182,11 @@ async function keychainAvailable(): Promise<boolean> {
|
|
|
192
182
|
}
|
|
193
183
|
}
|
|
194
184
|
|
|
195
|
-
|
|
185
|
+
// MOI_SECRET_BACKEND=file is the out-of-process form of setSecretStoreBackend:
|
|
186
|
+
// subprocesses (CLI spawned from tests, headless setups) pin the file store so
|
|
187
|
+
// they can never write to the real OS keychain.
|
|
188
|
+
const DEFAULT_BACKEND = process.env.MOI_SECRET_BACKEND === 'file' ? 'file' : 'auto'
|
|
189
|
+
let _backend: 'auto' | 'file' | 'keychain' = DEFAULT_BACKEND
|
|
196
190
|
let _storePromise: Promise<SecretStore> | null = null
|
|
197
191
|
|
|
198
192
|
// Force a backend (tests pin 'file' so they never touch the real keychain).
|
|
@@ -204,13 +198,19 @@ export function setSecretStoreBackend(backend: 'auto' | 'file' | 'keychain') {
|
|
|
204
198
|
async function createSecretStore(): Promise<SecretStore> {
|
|
205
199
|
if (_backend === 'file') return new FileSecretStore()
|
|
206
200
|
if (_backend === 'keychain') return new KeychainSecretStore()
|
|
201
|
+
// Announce the backend only in the server process (MOI_SERVER=1) — every CLI
|
|
202
|
+
// invocation re-probes, and `moi env` already reports the backend in its
|
|
203
|
+
// output, so logging here would just be per-command noise for the agent.
|
|
204
|
+
const announce = process.env.MOI_SERVER === '1'
|
|
207
205
|
if (await keychainAvailable()) {
|
|
208
|
-
console.log('[env] workspace secrets: OS keychain')
|
|
206
|
+
if (announce) console.log('[env] workspace secrets: OS keychain')
|
|
209
207
|
return new KeychainSecretStore()
|
|
210
208
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
209
|
+
if (announce) {
|
|
210
|
+
console.warn(
|
|
211
|
+
`[env] OS keychain unavailable — storing workspace secrets in ${_secretFilePath} (0600).`
|
|
212
|
+
)
|
|
213
|
+
}
|
|
214
214
|
return new FileSecretStore()
|
|
215
215
|
}
|
|
216
216
|
|
|
@@ -220,19 +220,18 @@ function secretStore(): Promise<SecretStore> {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
// ---------------------------------------------------------------------------
|
|
223
|
-
// Metadata: inheritDotenv
|
|
223
|
+
// Metadata: inheritDotenv (NOT secret)
|
|
224
224
|
// ---------------------------------------------------------------------------
|
|
225
225
|
|
|
226
|
-
|
|
226
|
+
// Older versions stored a per-key `scopes` map here too; stale entries in the
|
|
227
|
+
// file are simply ignored (and dropped the next time inheritDotenv is written).
|
|
228
|
+
type EnvMeta = { inheritDotenv: boolean }
|
|
227
229
|
type MetaStore = Record<string, EnvMeta>
|
|
228
230
|
|
|
229
231
|
async function getMeta(workspacePath: string): Promise<EnvMeta> {
|
|
230
232
|
const e = (await readJsonObjectFile<MetaStore>(_metaPath))[resolve(workspacePath)]
|
|
231
|
-
if (!e || typeof e !== 'object') return { inheritDotenv: true
|
|
232
|
-
return {
|
|
233
|
-
inheritDotenv: e.inheritDotenv !== false,
|
|
234
|
-
scopes: e.scopes && typeof e.scopes === 'object' ? e.scopes : {}
|
|
235
|
-
}
|
|
233
|
+
if (!e || typeof e !== 'object') return { inheritDotenv: true }
|
|
234
|
+
return { inheritDotenv: e.inheritDotenv !== false }
|
|
236
235
|
}
|
|
237
236
|
|
|
238
237
|
async function writeMeta(workspacePath: string, meta: EnvMeta): Promise<void> {
|
|
@@ -241,11 +240,6 @@ async function writeMeta(workspacePath: string, meta: EnvMeta): Promise<void> {
|
|
|
241
240
|
await writeJsonAtomic(_metaPath, store)
|
|
242
241
|
}
|
|
243
242
|
|
|
244
|
-
// The scope of a custom key, defaulting to 'both' when unset.
|
|
245
|
-
function scopeOf(meta: EnvMeta, key: string): EnvScope {
|
|
246
|
-
return meta.scopes[key] ?? 'both'
|
|
247
|
-
}
|
|
248
|
-
|
|
249
243
|
// ---------------------------------------------------------------------------
|
|
250
244
|
// Dotenv discovery
|
|
251
245
|
// ---------------------------------------------------------------------------
|
|
@@ -303,21 +297,26 @@ async function loadWorkspaceEnvState(workspacePath: string): Promise<WorkspaceEn
|
|
|
303
297
|
// Public API
|
|
304
298
|
// ---------------------------------------------------------------------------
|
|
305
299
|
|
|
306
|
-
// The effective env
|
|
307
|
-
//
|
|
308
|
-
//
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
300
|
+
// The effective env: discovered `.env` (when inherited) overlaid with the
|
|
301
|
+
// custom secrets. Injected at spawn — env is frozen once the process starts,
|
|
302
|
+
// so changes require a restart (see functions.ts / cc-session.ts).
|
|
303
|
+
export async function resolveWorkspaceEnv(workspacePath: string): Promise<Record<string, string>> {
|
|
304
|
+
const { secrets, dotenv } = await loadWorkspaceEnvState(workspacePath)
|
|
305
|
+
return { ...dotenv, ...secrets }
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Every key declared in the workspace's `.env` files, regardless of the
|
|
309
|
+
// inherit toggle. `moi env exec` uses this to scrub Bun's auto-loaded dotenv
|
|
310
|
+
// values out of the inherited process env, so the resolution above stays
|
|
311
|
+
// authoritative for workspace keys.
|
|
312
|
+
export async function discoverDotenvKeys(workspacePath: string): Promise<string[]> {
|
|
313
|
+
return Object.keys(mergeDotenv(await discoverDotenv(workspacePath)))
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Which store custom secrets land in — surfaced by the CLI after a write so a
|
|
317
|
+
// plaintext-file fallback never goes unnoticed.
|
|
318
|
+
export async function secretBackend(): Promise<'keychain' | 'file'> {
|
|
319
|
+
return (await secretStore()).backend
|
|
321
320
|
}
|
|
322
321
|
|
|
323
322
|
export type EnvUpdate = {
|
|
@@ -325,8 +324,6 @@ export type EnvUpdate = {
|
|
|
325
324
|
set?: Record<string, string>
|
|
326
325
|
// Delete these custom keys.
|
|
327
326
|
remove?: string[]
|
|
328
|
-
// Set the sink scope for existing custom keys.
|
|
329
|
-
scopes?: Record<string, EnvScope>
|
|
330
327
|
// Toggle whether `.env` files feed the workspace.
|
|
331
328
|
inheritDotenv?: boolean
|
|
332
329
|
}
|
|
@@ -349,30 +346,28 @@ export function updateWorkspaceEnv(workspacePath: string, patch: EnvUpdate): Pro
|
|
|
349
346
|
}
|
|
350
347
|
await store.set(workspacePath, secrets)
|
|
351
348
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
if (patch.scopes) {
|
|
357
|
-
for (const [k, s] of Object.entries(patch.scopes)) {
|
|
358
|
-
if (k in scopes && isValidScope(s)) scopes[k] = s
|
|
359
|
-
}
|
|
349
|
+
// Meta holds only inheritDotenv now, so a secrets-only patch has nothing
|
|
350
|
+
// to write (this is also the write that drops legacy `scopes` entries).
|
|
351
|
+
if (typeof patch.inheritDotenv === 'boolean') {
|
|
352
|
+
await writeMeta(workspacePath, { inheritDotenv: patch.inheritDotenv })
|
|
360
353
|
}
|
|
361
|
-
const inheritDotenv =
|
|
362
|
-
typeof patch.inheritDotenv === 'boolean' ? patch.inheritDotenv : meta.inheritDotenv
|
|
363
|
-
await writeMeta(workspacePath, { inheritDotenv, scopes })
|
|
364
354
|
})
|
|
365
355
|
}
|
|
366
356
|
|
|
367
|
-
// The env view for the settings UI
|
|
368
|
-
//
|
|
369
|
-
// the secret backend, and declared-required keys with a satisfied flag.
|
|
370
|
-
// `required` maps a key to the widgets that declared it (collected by caller)
|
|
357
|
+
// The env view for the settings UI and `moi env`: every effective key with its
|
|
358
|
+
// source, the discovered files (key counts only — values masked), the mode
|
|
359
|
+
// flag, the secret backend, and declared-required keys with a satisfied flag.
|
|
360
|
+
// `required` maps a key to the widgets that declared it (collected by caller);
|
|
361
|
+
// it may be passed as a promise — manifest reads are independent of the env
|
|
362
|
+
// stores, so both load in parallel here.
|
|
371
363
|
export async function getWorkspaceEnvView(
|
|
372
364
|
workspacePath: string,
|
|
373
|
-
|
|
365
|
+
requiredInput: Record<string, string[]> | Promise<Record<string, string[]>> = {}
|
|
374
366
|
): Promise<WorkspaceEnvView> {
|
|
375
|
-
const { meta, files, secrets, dotenv, backend } = await
|
|
367
|
+
const [{ meta, files, secrets, dotenv, backend }, required] = await Promise.all([
|
|
368
|
+
loadWorkspaceEnvState(workspacePath),
|
|
369
|
+
requiredInput
|
|
370
|
+
])
|
|
376
371
|
|
|
377
372
|
const keys = new Set([...Object.keys(dotenv), ...Object.keys(secrets)])
|
|
378
373
|
const vars = [...keys].sort().map(key => {
|
|
@@ -382,20 +377,14 @@ export async function getWorkspaceEnvView(
|
|
|
382
377
|
return {
|
|
383
378
|
key,
|
|
384
379
|
source,
|
|
385
|
-
...(inCustom ? { scope: scopeOf(meta, key) } : {}),
|
|
386
380
|
...(fromDotenv ? { files: files.filter(f => key in f.vars).map(f => f.file) } : {})
|
|
387
381
|
}
|
|
388
382
|
})
|
|
389
383
|
|
|
390
|
-
// A
|
|
391
|
-
// a `.env` value (if inherited) or a custom key scoped widgets/both.
|
|
392
|
-
const widgetVisible = new Set(Object.keys(dotenv))
|
|
393
|
-
for (const k of Object.keys(secrets)) {
|
|
394
|
-
if (scopeOf(meta, k) !== 'agent') widgetVisible.add(k)
|
|
395
|
-
}
|
|
384
|
+
// A declared-required key is "satisfied" when it's in the effective env.
|
|
396
385
|
const requiredView = Object.entries(required)
|
|
397
386
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
398
|
-
.map(([key, widgets]) => ({ key, satisfied:
|
|
387
|
+
.map(([key, widgets]) => ({ key, satisfied: keys.has(key), widgets }))
|
|
399
388
|
|
|
400
389
|
return {
|
|
401
390
|
vars,
|
|
@@ -112,6 +112,8 @@ never from inside `.moi/` itself. You don't pass paths; moi resolves the workspa
|
|
|
112
112
|
- `moi theme --font=<key>` — change font theme (omit `--font` to list options)
|
|
113
113
|
- `moi theme --color=<key>` — change color preset (omit `--color` to list options)
|
|
114
114
|
- `moi config` — set the workspace name & icon (`moi config --help` for usage)
|
|
115
|
+
- `moi env` — list available env keys and where they come from (never values);
|
|
116
|
+
`moi env exec -- <cmd>` runs a command with the workspace env (see Environment & secrets)
|
|
115
117
|
- `moi skill` — show installed vs bundled skill versions; `moi skill update` to refresh
|
|
116
118
|
|
|
117
119
|
For more options, commands, use `moi help`.
|
|
@@ -193,14 +195,45 @@ media → `.server.ts` returns the **path**, render with `fileUrl()`.
|
|
|
193
195
|
|
|
194
196
|
## Environment & secrets
|
|
195
197
|
|
|
198
|
+
Each workspace has an effective env: keys from the project's `.env` / `.env.local` (when
|
|
199
|
+
inheritance is enabled in settings) plus **custom secrets** the user manages in the workspace env
|
|
200
|
+
settings. moi injects this env into:
|
|
201
|
+
|
|
202
|
+
- applet server functions — read it as `process.env` inside `.server.ts`
|
|
203
|
+
- any command run via `moi env exec -- <cmd>`
|
|
204
|
+
- your own shell (Bash tool) — but only in some harnesses (e.g. Claude Code). Don't assume it:
|
|
205
|
+
verify the key is visible first, or just use `moi env exec`, which works everywhere.
|
|
206
|
+
|
|
207
|
+
Rules:
|
|
208
|
+
- **Check before you assume.** When a task needs a key or token — an API pull, a widget calling a
|
|
209
|
+
service — run `moi env` first. It lists key names with their source (`.env` / custom) and flags
|
|
210
|
+
declared `requiredEnv` keys that are missing. Values are never shown.
|
|
211
|
+
- **Key present** → say which key you'll use and where it's from ("using `NOTION_TOKEN` from
|
|
212
|
+
`.env`") and proceed. To run a script or one-off command with the workspace env, use
|
|
213
|
+
`moi env exec -- bun script.ts` — it also picks up values changed after your session started.
|
|
214
|
+
- **Key missing** → never invent or hardcode a value, and don't edit `.env` yourself. Tell the user
|
|
215
|
+
the exact key name to add in the workspace env settings. Still build and wire the applet: declare
|
|
216
|
+
the key in `config.requiredEnv` and handle its absence, so it works the moment the user sets it.
|
|
217
|
+
If the user pastes a value in chat, store it with `moi env set KEY=value`
|
|
218
|
+
(`moi env unset KEY` removes it).
|
|
219
|
+
- **Never print secret values** — not in chat, not in logs. Refer to keys by name only.
|
|
220
|
+
|
|
196
221
|
`process.env` is readable **only** inside `.server.ts` (the `.tsx` runs in the browser) — keep API
|
|
197
|
-
keys there.
|
|
198
|
-
|
|
199
|
-
|
|
222
|
+
keys there. Either source may be absent, so always handle a missing key. List expected keys in
|
|
223
|
+
`config.requiredEnv` — advisory only (it surfaces a hint in the UI and `moi env`; it's never
|
|
224
|
+
enforced).
|
|
200
225
|
|
|
201
226
|
```ts
|
|
202
|
-
|
|
203
|
-
|
|
227
|
+
// forecast.server.ts
|
|
228
|
+
export async function getForecast(city: string) {
|
|
229
|
+
const key = process.env.WEATHER_API_KEY // always current — env changes respawn the worker
|
|
230
|
+
if (!key) return { error: 'Add WEATHER_API_KEY to your env' }
|
|
231
|
+
const res = await fetch(`https://api.example.com/forecast?city=${encodeURIComponent(city)}`, {
|
|
232
|
+
headers: { Authorization: `Bearer ${key}` }
|
|
233
|
+
})
|
|
234
|
+
if (!res.ok) return { error: `Weather API error ${res.status}` }
|
|
235
|
+
return { data: await res.json() }
|
|
236
|
+
}
|
|
204
237
|
```
|
|
205
238
|
|
|
206
239
|
# Widgets
|
|
@@ -249,4 +282,4 @@ This skill is installed with moi (via the CLI or the UI) and can fall behind whe
|
|
|
249
282
|
- **Then** — if you updated, mention it.
|
|
250
283
|
|
|
251
284
|
<!-- moi skill version marker — read by `moi skill` to detect drift; do not edit by hand -->
|
|
252
|
-
<moi-skill version="0.
|
|
285
|
+
<moi-skill version="0.4.0" />
|