@cordfuse/crosstalk 6.0.0-alpha.9 → 7.0.0-alpha.10
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 +97 -0
- package/bin/crosstalk.js +61 -74
- package/commands/channel.js +118 -0
- package/commands/chat.js +119 -0
- package/commands/down.js +40 -0
- package/commands/init.js +243 -0
- package/commands/logs.js +37 -0
- package/commands/pull.js +22 -0
- package/commands/replies.js +40 -0
- package/commands/restart.js +29 -0
- package/commands/rm.js +109 -0
- package/commands/run.js +115 -0
- package/commands/status.js +90 -0
- package/commands/up.js +135 -0
- package/commands/version.js +33 -0
- package/lib/api-client.js +141 -0
- package/lib/argv.js +30 -0
- package/lib/errors.js +19 -0
- package/lib/resolve.js +191 -0
- package/package.json +5 -21
- package/src/activation.ts +0 -104
- package/src/actor.ts +0 -131
- package/src/attach.ts +0 -118
- package/src/channel.ts +0 -49
- package/src/chat.ts +0 -142
- package/src/dispatch.ts +0 -531
- package/src/dlq.ts +0 -216
- package/src/filenames.ts +0 -28
- package/src/frontmatter.ts +0 -26
- package/src/init.ts +0 -138
- package/src/open.ts +0 -207
- package/src/replies.ts +0 -59
- package/src/send.ts +0 -122
- package/src/state.ts +0 -173
- package/src/status.ts +0 -75
- package/src/stop.ts +0 -37
- package/src/transport.ts +0 -213
- package/src/turnq.ts +0 -91
- package/src/upgrade.ts +0 -211
- package/src/wake.ts +0 -7
- package/template/CLAUDE.md +0 -12
- package/template/gitignore +0 -4
- package/template/upstream/CROSSTALK-VERSION +0 -1
- package/template/upstream/CROSSTALK.md +0 -298
- package/template/upstream/OPERATOR.md +0 -60
- package/template/upstream/PROTOCOL.md +0 -80
- package/template/upstream/actors/concierge.md +0 -36
package/commands/up.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// crosstalk up — bring up the engine container for a transport.
|
|
2
|
+
//
|
|
3
|
+
// alpha.6: no cwd magic. Resolves container by --containername flag
|
|
4
|
+
// (default 'crosstalk') and operates against <base>/<name>/.
|
|
5
|
+
//
|
|
6
|
+
// Three lifecycle cases:
|
|
7
|
+
// 1. Container already running → error (use chat/restart instead).
|
|
8
|
+
// 2. Storage exists, no container → resume; print one-line notice.
|
|
9
|
+
// 3. Storage doesn't exist → error pointing to `crosstalk init`.
|
|
10
|
+
//
|
|
11
|
+
// First-time up generates <base>/<name>/docker-compose.yml from a template.
|
|
12
|
+
// Subsequent ups respect operator edits — only regenerate if missing.
|
|
13
|
+
|
|
14
|
+
import { writeFileSync, existsSync, readFileSync } from 'fs';
|
|
15
|
+
import { spawnSync } from 'child_process';
|
|
16
|
+
import { has } from '../lib/argv.js';
|
|
17
|
+
import {
|
|
18
|
+
requireInitialized,
|
|
19
|
+
isRunning,
|
|
20
|
+
containerExists,
|
|
21
|
+
apiPortFor,
|
|
22
|
+
DEFAULT_CONTAINER_NAME,
|
|
23
|
+
CROSSTALK_LABEL,
|
|
24
|
+
} from '../lib/resolve.js';
|
|
25
|
+
|
|
26
|
+
const DEFAULT_IMAGE = process.env.CROSSTALK_IMAGE
|
|
27
|
+
?? 'ghcr.io/cordfuse/crosstalk-server:7.0.0-alpha.8';
|
|
28
|
+
|
|
29
|
+
function usage(exit = 0) {
|
|
30
|
+
const w = exit === 0 ? process.stdout : process.stderr;
|
|
31
|
+
w.write(
|
|
32
|
+
`Usage: crosstalk up [--containername <name>]
|
|
33
|
+
|
|
34
|
+
Starts the engine container for a transport that has already been
|
|
35
|
+
'crosstalk init'd. Default container is 'crosstalk'; pass --containername
|
|
36
|
+
to address a named one.
|
|
37
|
+
|
|
38
|
+
If the storage at <base>/<name>/ already exists (operator previously
|
|
39
|
+
brought it up then 'crosstalk down'd it), the container resumes against
|
|
40
|
+
that state — installed agent CLIs, OAuth tokens, dispatcher cursor all
|
|
41
|
+
carry over.
|
|
42
|
+
|
|
43
|
+
Environment:
|
|
44
|
+
CROSSTALK_IMAGE Override the engine image
|
|
45
|
+
CROSSTALK_ALIAS Override the engine's machine identity
|
|
46
|
+
(defaults to the container name)
|
|
47
|
+
`,
|
|
48
|
+
);
|
|
49
|
+
process.exit(exit);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function renderCompose({ name, image, apiPort, alias, uid, gid, paths }) {
|
|
53
|
+
return `# Generated by 'crosstalk up'. Machine-local — runtime-owned.
|
|
54
|
+
# Storage mode: ${paths.mode}
|
|
55
|
+
# transport (container /var/lib/crosstalk-transport) → ${paths.transportDir}
|
|
56
|
+
# crosstalk-root (container /crosstalk-root) → ${paths.crosstalkRoot}
|
|
57
|
+
# crosstalk-state (container /var/lib/crosstalk-state) → ${paths.crosstalkState}
|
|
58
|
+
|
|
59
|
+
services:
|
|
60
|
+
crosstalkd:
|
|
61
|
+
image: ${image}
|
|
62
|
+
container_name: ${name}
|
|
63
|
+
restart: unless-stopped
|
|
64
|
+
labels:
|
|
65
|
+
${CROSSTALK_LABEL.replace('=', ': "')}"
|
|
66
|
+
ports:
|
|
67
|
+
- "127.0.0.1:${apiPort}:7000"
|
|
68
|
+
volumes:
|
|
69
|
+
- ${paths.transportDir}:/var/lib/crosstalk-transport
|
|
70
|
+
- ${paths.crosstalkRoot}:/crosstalk-root
|
|
71
|
+
- ${paths.crosstalkState}:/var/lib/crosstalk-state
|
|
72
|
+
environment:
|
|
73
|
+
CROSSTALK_ALIAS: ${alias}
|
|
74
|
+
CROSSTALK_UID: "${uid}"
|
|
75
|
+
CROSSTALK_GID: "${gid}"
|
|
76
|
+
CROSSTALKD_API_PORT: "7000"
|
|
77
|
+
DISPATCH_JSON: "true"
|
|
78
|
+
DISPATCH_POLL_SECONDS: "30"
|
|
79
|
+
`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function run(argv) {
|
|
83
|
+
if (has(argv, '--help') || has(argv, '-h')) usage(0);
|
|
84
|
+
|
|
85
|
+
const { name, paths } = requireInitialized(argv);
|
|
86
|
+
|
|
87
|
+
// Case 1: already running → error, don't surprise the operator with
|
|
88
|
+
// a silent no-op or implicit restart.
|
|
89
|
+
if (isRunning(name)) {
|
|
90
|
+
process.stderr.write(
|
|
91
|
+
`crosstalk up: '${name}' is already running.\n` +
|
|
92
|
+
` Use 'crosstalk chat${name === DEFAULT_CONTAINER_NAME ? '' : ` --containername ${name}`}' to attach,\n` +
|
|
93
|
+
` 'crosstalk restart${name === DEFAULT_CONTAINER_NAME ? '' : ` --containername ${name}`}' to recreate,\n` +
|
|
94
|
+
` or 'crosstalk down${name === DEFAULT_CONTAINER_NAME ? '' : ` --containername ${name}`}' first.\n`,
|
|
95
|
+
);
|
|
96
|
+
return 1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Case "stopped but not removed" — `docker stop` without compose down.
|
|
100
|
+
// Silently remove the dead container so the recreate works.
|
|
101
|
+
if (containerExists(name)) {
|
|
102
|
+
spawnSync('docker', ['rm', '-f', name], { stdio: 'ignore' });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Compose file generation. If operator edited theirs, respect it.
|
|
106
|
+
const generated = !existsSync(paths.composeFile);
|
|
107
|
+
if (generated) {
|
|
108
|
+
const alias = process.env.CROSSTALK_ALIAS ?? name;
|
|
109
|
+
const uid = typeof process.getuid === 'function' ? process.getuid() : 1000;
|
|
110
|
+
const gid = typeof process.getgid === 'function' ? process.getgid() : 1000;
|
|
111
|
+
const apiPort = apiPortFor(name);
|
|
112
|
+
const image = DEFAULT_IMAGE;
|
|
113
|
+
const content = renderCompose({ name, image, apiPort, alias, uid, gid, paths });
|
|
114
|
+
writeFileSync(paths.composeFile, content);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Resume notice when storage is non-fresh (operator brought it up
|
|
118
|
+
// before, has installed CLIs / auth state in crosstalk-root/). Heuristic:
|
|
119
|
+
// crosstalk-root has more than just a .gitkeep / nothing.
|
|
120
|
+
if (!generated) {
|
|
121
|
+
process.stdout.write(`Resuming '${name}' (existing state at ${paths.storageRoot})\n`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const r = spawnSync('docker', ['compose', '-f', paths.composeFile, 'up', '-d'], {
|
|
125
|
+
stdio: 'inherit',
|
|
126
|
+
});
|
|
127
|
+
if (r.status !== 0) {
|
|
128
|
+
process.stderr.write(`crosstalk up: docker compose exited ${r.status}\n`);
|
|
129
|
+
return r.status ?? 1;
|
|
130
|
+
}
|
|
131
|
+
process.stdout.write(
|
|
132
|
+
`\n'${name}' starting. Check with 'crosstalk status${name === DEFAULT_CONTAINER_NAME ? '' : ` --containername ${name}`}' (give it ~5s).\n`,
|
|
133
|
+
);
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// crosstalk version — print client and (if reachable) engine versions.
|
|
2
|
+
//
|
|
3
|
+
// Doesn't fail if the engine isn't reachable; just prints "(engine
|
|
4
|
+
// unreachable)" since you might be running `version` before bringing
|
|
5
|
+
// the container up.
|
|
6
|
+
|
|
7
|
+
import { readFileSync } from 'fs';
|
|
8
|
+
import { dirname, join } from 'path';
|
|
9
|
+
import { fileURLToPath } from 'url';
|
|
10
|
+
import { apiFor, ConnectError } from '../lib/api-client.js';
|
|
11
|
+
|
|
12
|
+
const thisDir = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const pkgPath = join(thisDir, '..', 'package.json');
|
|
14
|
+
const clientPkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
15
|
+
|
|
16
|
+
export async function run(argv) {
|
|
17
|
+
process.stdout.write(`crosstalk client: ${clientPkg.version}\n`);
|
|
18
|
+
const api = apiFor(argv);
|
|
19
|
+
try {
|
|
20
|
+
// Skew detection happens passively in api-client.js via the
|
|
21
|
+
// X-Crosstalk-Engine-Version response header — no duplication here.
|
|
22
|
+
const v = await api.get('/version');
|
|
23
|
+
process.stdout.write(`crosstalkd engine: ${v.version} (alias: ${v.alias})\n`);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
if (err instanceof ConnectError) {
|
|
26
|
+
process.stdout.write(`crosstalkd engine: (unreachable on 127.0.0.1:${err.port})\n`);
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
process.stdout.write(`crosstalkd engine: (error: ${err.message})\n`);
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// api-client.js — HTTP client over loopback to crosstalkd's local API.
|
|
2
|
+
//
|
|
3
|
+
// All operator commands route through this. The engine binds 127.0.0.1
|
|
4
|
+
// inside the container; docker-compose maps that to the host loopback.
|
|
5
|
+
// The host port differs per container — default container 'crosstalk'
|
|
6
|
+
// gets 7000; named containers pick a free port at init time, recorded in
|
|
7
|
+
// `<base>/<name>/api-port`. apiPortFor() resolves name → port.
|
|
8
|
+
//
|
|
9
|
+
// Port resolution order:
|
|
10
|
+
// 1. opts.port (explicit override from caller)
|
|
11
|
+
// 2. CROSSTALK_API_PORT env var (test/dev override)
|
|
12
|
+
// 3. apiPortFor(name) — reads `<base>/<name>/api-port` file
|
|
13
|
+
// 4. DEFAULT_API_PORT (7000) as last resort
|
|
14
|
+
//
|
|
15
|
+
// No auth: engine binds 127.0.0.1 only, no token. Same model as ollama.
|
|
16
|
+
//
|
|
17
|
+
// Version skew: engine sets X-Crosstalk-Engine-Version on every response.
|
|
18
|
+
// We compare to client version on each call, emit one warning per process
|
|
19
|
+
// if mismatched. Pre-1.0 client and engine ship in lockstep.
|
|
20
|
+
|
|
21
|
+
import { readFileSync } from 'fs';
|
|
22
|
+
import { dirname, join } from 'path';
|
|
23
|
+
import { fileURLToPath } from 'url';
|
|
24
|
+
import { apiPortFor, DEFAULT_API_PORT, parseContainerName } from './resolve.js';
|
|
25
|
+
|
|
26
|
+
const CLIENT_VERSION = (() => {
|
|
27
|
+
try {
|
|
28
|
+
const thisDir = dirname(fileURLToPath(import.meta.url));
|
|
29
|
+
return JSON.parse(readFileSync(join(thisDir, '..', 'package.json'), 'utf-8')).version;
|
|
30
|
+
} catch {
|
|
31
|
+
return 'unknown';
|
|
32
|
+
}
|
|
33
|
+
})();
|
|
34
|
+
|
|
35
|
+
let skewWarned = false;
|
|
36
|
+
|
|
37
|
+
function checkSkew(engineVersion) {
|
|
38
|
+
if (skewWarned || !engineVersion || engineVersion === CLIENT_VERSION) return;
|
|
39
|
+
skewWarned = true;
|
|
40
|
+
process.stderr.write(
|
|
41
|
+
`[WARN] Version skew: client ${CLIENT_VERSION}, engine ${engineVersion}.\n` +
|
|
42
|
+
` Pre-1.0 client and engine ship in lockstep. Likely fix:\n` +
|
|
43
|
+
` crosstalk pull && crosstalk restart # if engine is behind\n` +
|
|
44
|
+
` npm install -g @cordfuse/crosstalk # if client is behind\n\n`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { DEFAULT_API_PORT };
|
|
49
|
+
|
|
50
|
+
export function resolvePort(opts = {}) {
|
|
51
|
+
if (typeof opts.port === 'number' && Number.isInteger(opts.port)) return opts.port;
|
|
52
|
+
const fromEnv = Number(process.env.CROSSTALK_API_PORT);
|
|
53
|
+
if (Number.isInteger(fromEnv) && fromEnv > 0) return fromEnv;
|
|
54
|
+
if (opts.name) return apiPortFor(opts.name);
|
|
55
|
+
return DEFAULT_API_PORT;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function apiUrl(path, port) {
|
|
59
|
+
return `http://127.0.0.1:${port}${path}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function parseJsonOrThrow(res) {
|
|
63
|
+
let body;
|
|
64
|
+
try {
|
|
65
|
+
body = await res.json();
|
|
66
|
+
} catch {
|
|
67
|
+
throw new ApiError(res.status, 'invalid JSON in response');
|
|
68
|
+
}
|
|
69
|
+
if (!res.ok) {
|
|
70
|
+
const msg = body && typeof body.error === 'string' ? body.error : `HTTP ${res.status}`;
|
|
71
|
+
throw new ApiError(res.status, msg);
|
|
72
|
+
}
|
|
73
|
+
return body;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class ApiError extends Error {
|
|
77
|
+
constructor(status, message) {
|
|
78
|
+
super(message);
|
|
79
|
+
this.name = 'ApiError';
|
|
80
|
+
this.status = status;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export class ConnectError extends Error {
|
|
85
|
+
constructor(port, cause) {
|
|
86
|
+
super(
|
|
87
|
+
`cannot reach crosstalkd on 127.0.0.1:${port} — is the container running? ` +
|
|
88
|
+
`Try 'crosstalk up' to start it. (underlying: ${cause})`,
|
|
89
|
+
);
|
|
90
|
+
this.name = 'ConnectError';
|
|
91
|
+
this.port = port;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function call(method, path, body, opts = {}) {
|
|
96
|
+
const port = resolvePort(opts);
|
|
97
|
+
const init = {
|
|
98
|
+
method,
|
|
99
|
+
headers: body ? { 'Content-Type': 'application/json' } : undefined,
|
|
100
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
101
|
+
};
|
|
102
|
+
let res;
|
|
103
|
+
try {
|
|
104
|
+
res = await fetch(apiUrl(path, port), init);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
throw new ConnectError(port, err.message || String(err));
|
|
107
|
+
}
|
|
108
|
+
checkSkew(res.headers.get('x-crosstalk-engine-version'));
|
|
109
|
+
return parseJsonOrThrow(res);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Bind an api client to a specific container name. All callers should
|
|
113
|
+
// either pass argv (auto-resolves name) or an explicit name. Internally
|
|
114
|
+
// each call resolves the port via the name's api-port file. Validation
|
|
115
|
+
// errors print cleanly (no stack trace) — bug C fix.
|
|
116
|
+
export function apiFor(argv) {
|
|
117
|
+
let name;
|
|
118
|
+
try {
|
|
119
|
+
name = parseContainerName(argv);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
process.stderr.write(`crosstalk: ${err.message}\n`);
|
|
122
|
+
process.exit(1);
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
name,
|
|
126
|
+
get: (path, opts = {}) => call('GET', path, undefined, { ...opts, name }),
|
|
127
|
+
post: (path, body, opts = {}) => call('POST', path, body, { ...opts, name }),
|
|
128
|
+
patch: (path, body, opts = {}) => call('PATCH', path, body, { ...opts, name }),
|
|
129
|
+
delete: (path, opts = {}) => call('DELETE', path, undefined, { ...opts, name }),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Legacy entry point — env-driven port (CROSSTALK_API_PORT or 7000),
|
|
134
|
+
// no name resolution. Kept for harness compatibility; new code should
|
|
135
|
+
// use apiFor(argv) instead.
|
|
136
|
+
export const api = {
|
|
137
|
+
get: (path, opts) => call('GET', path, undefined, opts),
|
|
138
|
+
post: (path, body, opts) => call('POST', path, body, opts),
|
|
139
|
+
patch: (path, body, opts) => call('PATCH', path, body, opts),
|
|
140
|
+
delete: (path, opts) => call('DELETE', path, undefined, opts),
|
|
141
|
+
};
|
package/lib/argv.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Tiny argv helpers shared across commands. No yargs / commander
|
|
2
|
+
// dependency — the surfaces are simple enough that hand-parsing is
|
|
3
|
+
// shorter than wiring a framework.
|
|
4
|
+
|
|
5
|
+
export function flag(argv, name) {
|
|
6
|
+
const i = argv.indexOf(name);
|
|
7
|
+
if (i === -1 || i === argv.length - 1) return undefined;
|
|
8
|
+
return argv[i + 1];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function has(argv, name) {
|
|
12
|
+
return argv.includes(name);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// All positional (non-flag) args, skipping flag values for the listed
|
|
16
|
+
// flags. Handles both --long and -short forms; flag names in
|
|
17
|
+
// flagsWithValue must match the form they appear in argv.
|
|
18
|
+
export function positionals(argv, flagsWithValue) {
|
|
19
|
+
const valueFlags = new Set(flagsWithValue);
|
|
20
|
+
const out = [];
|
|
21
|
+
for (let i = 0; i < argv.length; i++) {
|
|
22
|
+
const a = argv[i];
|
|
23
|
+
if (a.startsWith('-')) {
|
|
24
|
+
if (valueFlags.has(a)) i++;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
out.push(a);
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Shared error→stderr formatting for client commands.
|
|
2
|
+
//
|
|
3
|
+
// Pattern: catch known error types (ConnectError, ApiError) and print
|
|
4
|
+
// operator-readable messages with appropriate exit codes. Unknown errors
|
|
5
|
+
// rethrow so they show stack traces — those are real bugs to fix.
|
|
6
|
+
|
|
7
|
+
import { ApiError, ConnectError } from './api-client.js';
|
|
8
|
+
|
|
9
|
+
export function reportAndExit(err, contextHint) {
|
|
10
|
+
if (err instanceof ConnectError) {
|
|
11
|
+
process.stderr.write(`${contextHint}: ${err.message}\n`);
|
|
12
|
+
process.exit(4);
|
|
13
|
+
}
|
|
14
|
+
if (err instanceof ApiError) {
|
|
15
|
+
process.stderr.write(`${contextHint}: ${err.message} (HTTP ${err.status})\n`);
|
|
16
|
+
process.exit(err.status === 400 ? 1 : 3);
|
|
17
|
+
}
|
|
18
|
+
throw err;
|
|
19
|
+
}
|
package/lib/resolve.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// resolve.js — name-based resolution for the runtime-owned-transport
|
|
2
|
+
// model (alpha.6+).
|
|
3
|
+
//
|
|
4
|
+
// Operator addresses transports by container name, picked at `crosstalk
|
|
5
|
+
// init`. Default container is literally `crosstalk`; named containers
|
|
6
|
+
// are literally whatever the operator typed into --containername.
|
|
7
|
+
// Identity lives with the container; storage lives at <base>/<name>/.
|
|
8
|
+
//
|
|
9
|
+
// Replaces the cwd-walking transport.js from alpha.5 (which derived
|
|
10
|
+
// identity from the operator's filesystem position). The new model is
|
|
11
|
+
// position-independent — operator can be anywhere on disk.
|
|
12
|
+
|
|
13
|
+
import { existsSync, readFileSync, mkdirSync, readdirSync } from 'fs';
|
|
14
|
+
import { spawnSync } from 'child_process';
|
|
15
|
+
import { join } from 'path';
|
|
16
|
+
import { homedir } from 'os';
|
|
17
|
+
|
|
18
|
+
export const DEFAULT_CONTAINER_NAME = 'crosstalk';
|
|
19
|
+
export const DEFAULT_API_PORT = 7000;
|
|
20
|
+
export const CROSSTALK_LABEL = 'crosstalk.transport=true';
|
|
21
|
+
|
|
22
|
+
// Per-OS storage bases. User-mode (default) keeps everything under the
|
|
23
|
+
// operator's home — no sudo/UAC, no Docker Desktop file-sharing allowlist
|
|
24
|
+
// gymnastics. System-mode is for headless multi-operator deployments.
|
|
25
|
+
export function resolveBase(mode) {
|
|
26
|
+
const platform = process.platform;
|
|
27
|
+
const bases = (platform === 'darwin')
|
|
28
|
+
? {
|
|
29
|
+
user: join(homedir(), 'Library', 'Application Support', 'crosstalk'),
|
|
30
|
+
system: '/Library/Application Support/crosstalk',
|
|
31
|
+
}
|
|
32
|
+
: (platform === 'win32')
|
|
33
|
+
? {
|
|
34
|
+
user: process.env['LOCALAPPDATA']
|
|
35
|
+
? join(process.env['LOCALAPPDATA'], 'crosstalk')
|
|
36
|
+
: join(homedir(), 'AppData', 'Local', 'crosstalk'),
|
|
37
|
+
system: 'C:\\ProgramData\\crosstalk',
|
|
38
|
+
}
|
|
39
|
+
: {
|
|
40
|
+
user: join(process.env['XDG_DATA_HOME'] ?? join(homedir(), '.local', 'share'), 'crosstalk'),
|
|
41
|
+
system: '/var/lib/crosstalk',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
if (mode !== 'user' && mode !== 'system') {
|
|
45
|
+
throw new Error(`CROSSTALK_STORAGE_MODE='${mode}' invalid — must be 'user' or 'system'.`);
|
|
46
|
+
}
|
|
47
|
+
return bases[mode];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function storageMode() {
|
|
51
|
+
return (process.env.CROSSTALK_STORAGE_MODE ?? 'user').toLowerCase();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// All storage paths for a given container name, derived from the resolved
|
|
55
|
+
// base. None of these are validated for existence here — callers check
|
|
56
|
+
// based on context (init pre-creates, up verifies, etc).
|
|
57
|
+
export function storagePaths(name, mode = storageMode()) {
|
|
58
|
+
const base = resolveBase(mode);
|
|
59
|
+
const root = join(base, name);
|
|
60
|
+
return {
|
|
61
|
+
base,
|
|
62
|
+
mode,
|
|
63
|
+
storageRoot: root,
|
|
64
|
+
transportDir: join(root, 'transport'),
|
|
65
|
+
crosstalkRoot: join(root, 'crosstalk-root'),
|
|
66
|
+
crosstalkState: join(root, 'crosstalk-state'),
|
|
67
|
+
composeFile: join(root, 'docker-compose.yml'),
|
|
68
|
+
portFile: join(root, 'api-port'),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Container name from --containername flag, defaulting to `crosstalk`.
|
|
73
|
+
// Validates the name is a sane Docker container name + filesystem dir
|
|
74
|
+
// name: lowercase alphanumeric, `_.-`, no leading dot/hyphen.
|
|
75
|
+
const NAME_RE = /^[a-z0-9][a-z0-9_.-]*$/;
|
|
76
|
+
export function parseContainerName(argv) {
|
|
77
|
+
let name = DEFAULT_CONTAINER_NAME;
|
|
78
|
+
for (let i = 0; i < argv.length; i++) {
|
|
79
|
+
if (argv[i] === '--containername' || argv[i] === '-c') {
|
|
80
|
+
const val = argv[i + 1];
|
|
81
|
+
if (!val) throw new Error('--containername requires a value');
|
|
82
|
+
name = val;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!NAME_RE.test(name)) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`--containername '${name}' invalid — must be lowercase alphanumeric ` +
|
|
89
|
+
`with optional ._- (no leading . or -).`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return name;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Read the API port for a given container. Source-of-truth lookup:
|
|
96
|
+
// 1. `<base>/<name>/api-port` file (written at init time)
|
|
97
|
+
// 2. Fallback: DEFAULT_API_PORT
|
|
98
|
+
// We use a file rather than `docker inspect` because the resolver runs
|
|
99
|
+
// before the container exists (`init`) and after it's gone (`rm` cleanup).
|
|
100
|
+
// Docker is authoritative for what's running; the port file is
|
|
101
|
+
// authoritative for which port a given container WOULD use.
|
|
102
|
+
export function apiPortFor(name) {
|
|
103
|
+
const paths = storagePaths(name);
|
|
104
|
+
if (existsSync(paths.portFile)) {
|
|
105
|
+
const v = Number(readFileSync(paths.portFile, 'utf-8').trim());
|
|
106
|
+
if (Number.isInteger(v) && v > 0 && v < 65536) return v;
|
|
107
|
+
}
|
|
108
|
+
return DEFAULT_API_PORT;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Pick a free TCP port. Default container always gets DEFAULT_API_PORT
|
|
112
|
+
// (7000) so operators have a predictable target. Named containers search
|
|
113
|
+
// upward from 7001 until they find one that doesn't collide with already-
|
|
114
|
+
// allocated containers (any `<base>/*/api-port` file).
|
|
115
|
+
export function pickFreePort(name, mode = storageMode()) {
|
|
116
|
+
if (name === DEFAULT_CONTAINER_NAME) return DEFAULT_API_PORT;
|
|
117
|
+
const base = resolveBase(mode);
|
|
118
|
+
const taken = new Set([DEFAULT_API_PORT]);
|
|
119
|
+
try {
|
|
120
|
+
const dirs = readdirSync(base);
|
|
121
|
+
for (const d of dirs) {
|
|
122
|
+
const portFile = join(base, d, 'api-port');
|
|
123
|
+
if (existsSync(portFile)) {
|
|
124
|
+
const v = Number(readFileSync(portFile, 'utf-8').trim());
|
|
125
|
+
if (Number.isInteger(v)) taken.add(v);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
} catch { /* base may not exist yet */ }
|
|
129
|
+
for (let p = 7001; p < 8000; p++) {
|
|
130
|
+
if (!taken.has(p)) return p;
|
|
131
|
+
}
|
|
132
|
+
throw new Error('Could not find a free port between 7001 and 7999.');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// True if `<base>/<name>/transport/.git` exists. The cheapest signal that
|
|
136
|
+
// a transport has been initialized for this name.
|
|
137
|
+
export function isInitialized(name) {
|
|
138
|
+
const paths = storagePaths(name);
|
|
139
|
+
return existsSync(join(paths.transportDir, '.git'));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// True if a running container exists for this name. Uses Docker as the
|
|
143
|
+
// source of truth, filtered by our label to avoid confusing
|
|
144
|
+
// operator-named-`crosstalk` containers from other workloads.
|
|
145
|
+
export function isRunning(name) {
|
|
146
|
+
const r = spawnSync(
|
|
147
|
+
'docker',
|
|
148
|
+
['ps', '--filter', `label=${CROSSTALK_LABEL}`, '--filter', `name=^${name}$`, '--format', '{{.Names}}'],
|
|
149
|
+
{ encoding: 'utf-8' },
|
|
150
|
+
);
|
|
151
|
+
if (r.status !== 0) return false;
|
|
152
|
+
return r.stdout.trim().split('\n').includes(name);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// True if a stopped container exists (less common path; usually `down`
|
|
156
|
+
// removes the container). Checked separately so `up` can decide whether
|
|
157
|
+
// to `docker rm` and recreate.
|
|
158
|
+
export function containerExists(name) {
|
|
159
|
+
const r = spawnSync(
|
|
160
|
+
'docker',
|
|
161
|
+
['ps', '-a', '--filter', `label=${CROSSTALK_LABEL}`, '--filter', `name=^${name}$`, '--format', '{{.Names}}'],
|
|
162
|
+
{ encoding: 'utf-8' },
|
|
163
|
+
);
|
|
164
|
+
if (r.status !== 0) return false;
|
|
165
|
+
return r.stdout.trim().split('\n').includes(name);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Look up the resolved name+paths from argv, validate that init has run,
|
|
169
|
+
// and exit with a clear error if not. For verbs that operate on existing
|
|
170
|
+
// transports (up after init, status, chat, down, rm, etc).
|
|
171
|
+
//
|
|
172
|
+
// Validation errors from parseContainerName are caught here and printed
|
|
173
|
+
// cleanly — Mac's bug C from alpha.6 was a raw stack trace when an
|
|
174
|
+
// invalid --containername reached the resolver.
|
|
175
|
+
export function requireInitialized(argv) {
|
|
176
|
+
let name;
|
|
177
|
+
try {
|
|
178
|
+
name = parseContainerName(argv);
|
|
179
|
+
} catch (err) {
|
|
180
|
+
process.stderr.write(`crosstalk: ${err.message}\n`);
|
|
181
|
+
process.exit(1);
|
|
182
|
+
}
|
|
183
|
+
if (!isInitialized(name)) {
|
|
184
|
+
process.stderr.write(
|
|
185
|
+
`crosstalk: no transport '${name}' on this machine.\n` +
|
|
186
|
+
` Run 'crosstalk init${name === DEFAULT_CONTAINER_NAME ? '' : ` --containername ${name}`}' to set one up.\n`,
|
|
187
|
+
);
|
|
188
|
+
process.exit(2);
|
|
189
|
+
}
|
|
190
|
+
return { name, paths: storagePaths(name) };
|
|
191
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cordfuse/crosstalk",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Crosstalk
|
|
3
|
+
"version": "7.0.0-alpha.10",
|
|
4
|
+
"description": "Crosstalk client — host-side CLI that talks to the crosstalkd daemon running inside the crosstalk-server container.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/cordfuse/crosstalk",
|
|
@@ -14,26 +14,10 @@
|
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"bin/",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"lib/",
|
|
18
|
+
"commands/",
|
|
19
|
+
"README.md"
|
|
19
20
|
],
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsc --noEmit",
|
|
22
|
-
"lint": "tsc --noEmit",
|
|
23
|
-
"test": "bun test",
|
|
24
|
-
"prepack": "cp -r ../transport template",
|
|
25
|
-
"postpack": "rm -rf template"
|
|
26
|
-
},
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"@cordfuse/turnq": "^0.4.2",
|
|
29
|
-
"@lydell/node-pty": "^1.2.0-beta.12",
|
|
30
|
-
"tsx": "^4.20.0",
|
|
31
|
-
"yaml": "^2.8.0"
|
|
32
|
-
},
|
|
33
|
-
"devDependencies": {
|
|
34
|
-
"@types/node": "^22.10.0",
|
|
35
|
-
"typescript": "^5.7.0"
|
|
36
|
-
},
|
|
37
21
|
"engines": {
|
|
38
22
|
"node": ">=20"
|
|
39
23
|
},
|