@stage-labs/metro 0.1.0-beta.117 → 0.1.0-beta.118
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/package.json +1 -1
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +13 -2
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +18 -8
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +8 -3
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +20 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +11 -3
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +3 -3
- package/runtime/runtime.json +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stage-labs/metro",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.118",
|
|
4
4
|
"description": "The metro command line. Sign in once per machine, then hand your MCP connector list to Claude Code without the credentials touching disk, argv or shell history.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metro",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.118",
|
|
4
4
|
"description": "Metro for Claude Code, installed by the metro daemon on this machine: every connector the agent holds as its own MCP server relayed through the daemon, the orchestrator-only guard on the main thread, the standing rules loaded at each session start, and a refresh skill for when the daemon could not keep the server list current. No vendor credential ever sits in a config file.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Bonustrack",
|
|
@@ -33,9 +33,17 @@ export interface RestoredAgent {
|
|
|
33
33
|
connectors: number;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
export type ImportMode = 'append' | 'overwrite';
|
|
37
|
+
|
|
36
38
|
export interface BundleApiDeps {
|
|
37
39
|
bundle: (subject: string, agentId: string) => Promise<AgentBundle>;
|
|
38
|
-
restore: (subject: string, bundle: AgentBundle) => Promise<RestoredAgent>;
|
|
40
|
+
restore: (subject: string, bundle: AgentBundle, mode: ImportMode) => Promise<RestoredAgent>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function parseMode(raw: unknown): ImportMode {
|
|
44
|
+
const mode = isRecord(raw) && typeof raw.mode === 'string' ? raw.mode : 'overwrite';
|
|
45
|
+
if (mode !== 'append' && mode !== 'overwrite') throw bad("mode is 'append' or 'overwrite'");
|
|
46
|
+
return mode;
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
const bad = (what: string): ApiError => new ApiError(`bundle: ${what}`, 400);
|
|
@@ -73,7 +81,10 @@ export function parseBundle(raw: unknown): AgentBundle {
|
|
|
73
81
|
}
|
|
74
82
|
|
|
75
83
|
async function answer(req: IncomingMessage, deps: BundleApiDeps, subject: string, path: string): Promise<unknown> {
|
|
76
|
-
if (path === RESTORE_PATH)
|
|
84
|
+
if (path === RESTORE_PATH) {
|
|
85
|
+
const body: unknown = await readJsonBody(req, BUNDLE_MAX);
|
|
86
|
+
return deps.restore(subject, parseBundle(body), parseMode(body));
|
|
87
|
+
}
|
|
77
88
|
const id = parseId(path.slice(AGENTS.length + 1).split('/')[0] ?? '');
|
|
78
89
|
if (id === null) throw new ApiError('no such agent', 404);
|
|
79
90
|
return deps.bundle(subject, id);
|
|
@@ -206,15 +206,18 @@ function importTarget(dir: string, agent: LoadedAgent & { key: string }): Import
|
|
|
206
206
|
return { path: same?.path ?? fresh, previous: same?.file };
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
+
export type ImportMode = 'append' | 'overwrite';
|
|
210
|
+
|
|
209
211
|
export async function localImportAgent(
|
|
210
212
|
subject: string,
|
|
211
213
|
agent: LoadedAgent,
|
|
212
214
|
dir = agentsDir(),
|
|
215
|
+
mode: ImportMode = 'overwrite',
|
|
213
216
|
): Promise<{ id: string; name: string; key: string; stations: number }> {
|
|
214
217
|
assertLocalOwner(subject, dir);
|
|
215
218
|
assertImportable(agent);
|
|
216
219
|
const { path, previous } = importTarget(dir, agent);
|
|
217
|
-
const file = fileFor(agent, localOwner(dir), path, previous);
|
|
220
|
+
const file = fileFor(agent, localOwner(dir), path, previous, mode);
|
|
218
221
|
ensureSecureDir(join(path, '..'));
|
|
219
222
|
save({ path, file });
|
|
220
223
|
if (previous !== undefined && previous.key !== agent.key) unregisterAgentKey(agent.id);
|
|
@@ -227,11 +230,17 @@ export async function localImportAgent(
|
|
|
227
230
|
});
|
|
228
231
|
}
|
|
229
232
|
|
|
230
|
-
function mergedStations(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
233
|
+
function mergedStations(
|
|
234
|
+
agent: LoadedAgent,
|
|
235
|
+
previous: AgentFile | undefined,
|
|
236
|
+
mode: ImportMode,
|
|
237
|
+
): AgentFile['stations'] {
|
|
238
|
+
const before = previous?.stations ?? [];
|
|
239
|
+
const here = (station: string, id: string): boolean => before.some((s) => s.station === station && s.id === id);
|
|
240
|
+
const fromMetro = agent.accounts
|
|
241
|
+
.map((a) => ({ ...a, allowlist: a.allowlist ?? ['*'] }))
|
|
242
|
+
.filter((m) => mode === 'overwrite' || !here(m.station, m.id));
|
|
243
|
+
const kept = before.filter((s) => !fromMetro.some((m) => m.station === s.station && m.id === s.id));
|
|
235
244
|
return [...fromMetro, ...kept];
|
|
236
245
|
}
|
|
237
246
|
|
|
@@ -239,7 +248,8 @@ function fileFor(
|
|
|
239
248
|
agent: LoadedAgent,
|
|
240
249
|
owner: string | null,
|
|
241
250
|
path: string,
|
|
242
|
-
previous
|
|
251
|
+
previous: AgentFile | undefined,
|
|
252
|
+
mode: ImportMode,
|
|
243
253
|
): AgentFile {
|
|
244
254
|
try {
|
|
245
255
|
return parseAgentFile(
|
|
@@ -249,7 +259,7 @@ function fileFor(
|
|
|
249
259
|
name: previous?.name ?? agent.name,
|
|
250
260
|
key: agent.key,
|
|
251
261
|
owner,
|
|
252
|
-
stations: mergedStations(agent, previous),
|
|
262
|
+
stations: mergedStations(agent, previous, mode),
|
|
253
263
|
connectors: previous?.connectors ?? [],
|
|
254
264
|
}),
|
|
255
265
|
path,
|
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
listMemory,
|
|
40
40
|
readMemoryFile,
|
|
41
41
|
readTranscript,
|
|
42
|
+
writeMemoryFile,
|
|
42
43
|
} from './files.js';
|
|
43
44
|
|
|
44
45
|
const PREFIX = '/api/claude';
|
|
@@ -94,12 +95,16 @@ const parts = (path: string): string[] => path.slice(PREFIX.length + 1).split('/
|
|
|
94
95
|
const seenIn = (body: Record<string, unknown>): string | null | undefined =>
|
|
95
96
|
'seenAt' in body ? (typeof body.seenAt === 'string' ? body.seenAt : null) : undefined;
|
|
96
97
|
|
|
97
|
-
|
|
98
|
+
const WRITE_HEADS = new Set(['settings', 'skills', 'memory']);
|
|
99
|
+
|
|
100
|
+
async function writeAnswer(req: IncomingMessage, path: string, search: string, dir: string): Promise<unknown> {
|
|
98
101
|
const [head = '', item = ''] = parts(path);
|
|
99
|
-
if ((head
|
|
102
|
+
if (!WRITE_HEADS.has(head) || item === '') throw new ApiError('method not allowed', 405);
|
|
100
103
|
const body = await readJsonBody(req, BODY_MAX);
|
|
101
104
|
if (!isRecord(body) || typeof body.text !== 'string') throw new ApiError('text is required', 400);
|
|
102
105
|
if (head === 'skills') return writeClaudeSkill(decodeURIComponent(item), body.text, seenIn(body), dir);
|
|
106
|
+
if (head === 'memory')
|
|
107
|
+
return writeMemoryFile(projectOf(new URLSearchParams(search)), decodeURIComponent(item), body.text, dir);
|
|
103
108
|
return writeClaudeSettings(item, body.text, seenIn(body), dir);
|
|
104
109
|
}
|
|
105
110
|
|
|
@@ -217,7 +222,7 @@ function routed(req: IncomingMessage, path: string, search: string, deps: Claude
|
|
|
217
222
|
if (head === LOGIN) return loginAnswer(req, item, deps);
|
|
218
223
|
const single = item === '' ? SINGLETONS[head] : undefined;
|
|
219
224
|
if (single !== undefined) return single(req, deps);
|
|
220
|
-
if (req.method === 'PUT') return writeAnswer(req, path, dir);
|
|
225
|
+
if (req.method === 'PUT') return writeAnswer(req, path, search, dir);
|
|
221
226
|
if (req.method === 'POST') return created(req, path, dir);
|
|
222
227
|
return answer(req.method ?? 'GET', path, new URLSearchParams(search), dir);
|
|
223
228
|
}
|
|
@@ -2,11 +2,14 @@ import {
|
|
|
2
2
|
closeSync,
|
|
3
3
|
createReadStream,
|
|
4
4
|
existsSync,
|
|
5
|
+
mkdirSync,
|
|
5
6
|
openSync,
|
|
6
7
|
readdirSync,
|
|
7
8
|
readSync,
|
|
9
|
+
renameSync,
|
|
8
10
|
rmSync,
|
|
9
11
|
statSync,
|
|
12
|
+
writeFileSync,
|
|
10
13
|
} from 'node:fs';
|
|
11
14
|
import { homedir } from 'node:os';
|
|
12
15
|
import { join } from 'node:path';
|
|
@@ -22,6 +25,8 @@ const TEXT_CAP = 20_000;
|
|
|
22
25
|
const RESULT_CAP = 8_000;
|
|
23
26
|
const INPUT_CAP = 4_000;
|
|
24
27
|
const FILE_CAP = 1_000_000;
|
|
28
|
+
const MEMORY_MAX = 256 * 1024;
|
|
29
|
+
const MEMORY_MODE = 0o644;
|
|
25
30
|
const NOISE = ['<task-notification>', '<system-reminder>', '<local-command', '<command-name>'];
|
|
26
31
|
|
|
27
32
|
export function claudeDir(): string {
|
|
@@ -322,6 +327,21 @@ export function listMemory(project: string, dir = claudeDir()): MemoryListing {
|
|
|
322
327
|
return { files, index: existsSync(index) ? readCapped(index) : null };
|
|
323
328
|
}
|
|
324
329
|
|
|
330
|
+
export function writeMemoryFile(project: string, name: string, text: string, dir = claudeDir()): MemoryFile {
|
|
331
|
+
if (text.trim() === '') throw new ApiError('a memory file cannot be empty', 400);
|
|
332
|
+
if (Buffer.byteLength(text, 'utf8') > MEMORY_MAX)
|
|
333
|
+
throw new ApiError(`a memory file is at most ${String(MEMORY_MAX)} bytes`, 400);
|
|
334
|
+
const folder = memoryDir(project, dir);
|
|
335
|
+
const file = safeName(name, MEMORY_RE, 'memory file name');
|
|
336
|
+
mkdirSync(folder, { recursive: true });
|
|
337
|
+
const path = join(folder, file);
|
|
338
|
+
const tmp = `${path}.metro-${String(process.pid)}`;
|
|
339
|
+
writeFileSync(tmp, text, { mode: MEMORY_MODE });
|
|
340
|
+
renameSync(tmp, path);
|
|
341
|
+
const stat = statSync(path);
|
|
342
|
+
return { name: file, bytes: stat.size, modifiedAt: stat.mtime.toISOString() };
|
|
343
|
+
}
|
|
344
|
+
|
|
325
345
|
export function readMemoryFile(project: string, name: string, dir = claudeDir()): string {
|
|
326
346
|
const path = join(memoryDir(project, dir), safeName(name, MEMORY_RE, 'memory file name'));
|
|
327
347
|
if (!existsSync(path)) throw new ApiError('no such memory file', 404);
|
|
@@ -104,14 +104,22 @@ async function oauthCapable(url: URL, auth: ConnectorAuth): Promise<boolean> {
|
|
|
104
104
|
return advertisesOAuth(url);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
export function localImportConnectors(
|
|
107
|
+
export function localImportConnectors(
|
|
108
|
+
rows: LoadedConnector[],
|
|
109
|
+
dir = agentsDir(),
|
|
110
|
+
mode: 'append' | 'overwrite' = 'overwrite',
|
|
111
|
+
): number {
|
|
108
112
|
const current = readLocalConnectors(dir);
|
|
109
|
-
|
|
113
|
+
const fresh =
|
|
114
|
+
mode === 'overwrite'
|
|
115
|
+
? rows
|
|
116
|
+
: rows.filter((row) => !current.some((r) => r.id === row.id || r.name === row.name));
|
|
117
|
+
for (const row of fresh) {
|
|
110
118
|
const other = current.find((r) => r.id !== row.id && r.name === row.name);
|
|
111
119
|
if (other !== undefined)
|
|
112
120
|
throw new ConnectorError(`a connector named '${row.name}' already exists on this daemon; rename it first`, 409);
|
|
113
121
|
}
|
|
114
|
-
const imported: LocalConnectorRow[] =
|
|
122
|
+
const imported: LocalConnectorRow[] = fresh.map((row) => ({
|
|
115
123
|
id: row.id,
|
|
116
124
|
name: row.name,
|
|
117
125
|
url: row.url,
|
|
@@ -164,10 +164,10 @@ function bundleApi(deps: LocalModeDeps): BundleApiDeps {
|
|
|
164
164
|
if (bundle.agent.key === '') throw new ApiError(`agent '${agent.name}' has no key to bundle`, 400);
|
|
165
165
|
return bundle;
|
|
166
166
|
},
|
|
167
|
-
restore: async (subject, bundle) => {
|
|
167
|
+
restore: async (subject, bundle, mode) => {
|
|
168
168
|
assertLocalOwner(subject);
|
|
169
|
-
const made = await localImportAgent(subject, loadedAgentOf(bundle));
|
|
170
|
-
const connectors = localImportConnectors(bundle.connectors);
|
|
169
|
+
const made = await localImportAgent(subject, loadedAgentOf(bundle), undefined, mode);
|
|
170
|
+
const connectors = localImportConnectors(bundle.connectors, undefined, mode);
|
|
171
171
|
for (const station of new Set(bundle.agent.stations.map((a) => a.station)))
|
|
172
172
|
await deps.syncStations(station).catch((err: unknown) => {
|
|
173
173
|
log.warn({ station, err: errMsg(err) }, 'restore: station reload failed, the change lands at the next boot');
|
package/runtime/runtime.json
CHANGED