@sublang/spex 4.1.0 → 5.0.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 +438 -54
- package/dist/cli.js +81 -31
- package/dist/commands/add.d.ts +12 -0
- package/dist/commands/add.js +138 -0
- package/dist/commands/auth-target.d.ts +11 -0
- package/dist/commands/auth-target.js +50 -0
- package/dist/commands/environment.d.ts +123 -0
- package/dist/commands/environment.js +411 -0
- package/dist/commands/export.d.ts +21 -0
- package/dist/commands/export.js +663 -0
- package/dist/commands/home.d.ts +56 -0
- package/dist/commands/home.js +215 -0
- package/dist/commands/install.d.ts +2 -0
- package/dist/commands/install.js +48 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +87 -0
- package/dist/commands/login.d.ts +2 -0
- package/dist/commands/login.js +211 -0
- package/dist/commands/logout.d.ts +2 -0
- package/dist/commands/logout.js +74 -0
- package/dist/commands/publish.d.ts +2 -0
- package/dist/commands/publish.js +120 -0
- package/dist/commands/remove.d.ts +2 -0
- package/dist/commands/remove.js +34 -0
- package/dist/commands/run.d.ts +50 -0
- package/dist/commands/run.js +309 -0
- package/dist/commands/search.d.ts +2 -0
- package/dist/commands/search.js +34 -0
- package/dist/commands/settle.d.ts +124 -0
- package/dist/commands/settle.js +475 -0
- package/dist/commands/update.d.ts +2 -0
- package/dist/commands/update.js +37 -0
- package/dist/commands/whoami.d.ts +2 -0
- package/dist/commands/whoami.js +75 -0
- package/dist/lib/environment/agent-folders.d.ts +14 -0
- package/dist/lib/environment/agent-folders.js +11 -0
- package/dist/lib/environment/archive.d.ts +26 -0
- package/dist/lib/environment/archive.js +91 -0
- package/dist/lib/environment/builtins.d.ts +112 -0
- package/dist/lib/environment/builtins.js +539 -0
- package/dist/lib/environment/citations.d.ts +75 -0
- package/dist/lib/environment/citations.js +267 -0
- package/dist/lib/environment/exports.d.ts +190 -0
- package/dist/lib/environment/exports.js +1029 -0
- package/dist/lib/environment/format.d.ts +133 -0
- package/dist/lib/environment/format.js +651 -0
- package/dist/lib/environment/git-source.d.ts +41 -0
- package/dist/lib/environment/git-source.js +159 -0
- package/dist/lib/environment/index.d.ts +20 -0
- package/dist/lib/environment/index.js +27 -0
- package/dist/lib/environment/install.d.ts +140 -0
- package/dist/lib/environment/install.js +694 -0
- package/dist/lib/environment/layout.d.ts +15 -0
- package/dist/lib/environment/layout.js +50 -0
- package/dist/lib/environment/lock.d.ts +87 -0
- package/dist/lib/environment/lock.js +305 -0
- package/dist/lib/environment/marker.d.ts +15 -0
- package/dist/lib/environment/marker.js +31 -0
- package/dist/lib/environment/own-paths.d.ts +59 -0
- package/dist/lib/environment/own-paths.js +174 -0
- package/dist/lib/environment/placed-record.d.ts +32 -0
- package/dist/lib/environment/placed-record.js +72 -0
- package/dist/lib/environment/registries.d.ts +25 -0
- package/dist/lib/environment/registries.js +75 -0
- package/dist/lib/environment/registry.d.ts +173 -0
- package/dist/lib/environment/registry.js +390 -0
- package/dist/lib/environment/requests.d.ts +108 -0
- package/dist/lib/environment/requests.js +403 -0
- package/dist/lib/environment/resolve.d.ts +70 -0
- package/dist/lib/environment/resolve.js +451 -0
- package/dist/lib/environment/semver.d.ts +30 -0
- package/dist/lib/environment/semver.js +130 -0
- package/dist/lib/environment/store.d.ts +23 -0
- package/dist/lib/environment/store.js +123 -0
- package/dist/lib/environment/tar.d.ts +27 -0
- package/dist/lib/environment/tar.js +219 -0
- package/dist/lib/files.d.ts +66 -0
- package/dist/lib/files.js +233 -0
- package/dist/lib/git-host.d.ts +579 -0
- package/dist/lib/git-host.js +1363 -0
- package/dist/lib/home-file.d.ts +124 -0
- package/dist/lib/home-file.js +288 -0
- package/dist/lib/instance.d.ts +63 -0
- package/dist/lib/instance.js +130 -0
- package/dist/lib/words.d.ts +32 -0
- package/dist/lib/words.js +27 -0
- package/dist/lint.js +8 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/standin-registry.d.ts +94 -0
- package/dist/testing/standin-registry.js +449 -0
- package/package.json +37 -3
- package/dist/attached-cli.d.ts +0 -13
- package/dist/attached-cli.js +0 -247
- package/dist/attached-client.d.ts +0 -36
- package/dist/attached-client.js +0 -189
- package/dist/generated-protocol.d.ts +0 -2
- package/dist/generated-protocol.js +0 -6
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { GitHostClient, type CredentialStore, type AuthScope } from "../lib/git-host.js";
|
|
2
|
+
import { type HomeFile, type HomeInstance } from "../lib/home-file.js";
|
|
3
|
+
import { OriginRefused } from "../lib/instance.js";
|
|
4
|
+
import { Refusal } from "./run.js";
|
|
5
|
+
/** The home's folder: a nonempty `SPEX_HOME`, else `~/.spex`. */
|
|
6
|
+
export declare function homeDir(env?: NodeJS.ProcessEnv): string;
|
|
7
|
+
/** `home.yaml` as it stands, null where none stands; one that will not
|
|
8
|
+
* read refuses the command naming the file and the library's reason. */
|
|
9
|
+
export declare function readHome(dir: string): HomeFile | null;
|
|
10
|
+
/** An address as a reader is shown it: any user info it carries, which
|
|
11
|
+
* may hold a secret, stands as `…`. */
|
|
12
|
+
export declare function shownAddress(value: string): string;
|
|
13
|
+
/** An address the origin rule refuses, for `flag`, as one phrase ending
|
|
14
|
+
* in what to do (cli-13). */
|
|
15
|
+
export declare function addressRefusal(flag: string, value: string, error: OriginRefused, example?: string): Refusal;
|
|
16
|
+
/** The canonical origin `--space` names; one the origin rule refuses
|
|
17
|
+
* is refused saying why. */
|
|
18
|
+
export declare function spaceOption(value: string): string;
|
|
19
|
+
/** The home's default instance (storage-2): the first listed, else the
|
|
20
|
+
* origin it was created with; for a home not yet made, the origin a new
|
|
21
|
+
* one would record. */
|
|
22
|
+
export declare function defaultInstance(home: HomeFile | null, env?: NodeJS.ProcessEnv): HomeInstance;
|
|
23
|
+
/** The instance a command names: `--space`, else the default. */
|
|
24
|
+
export declare function namedInstance(home: HomeFile | null, space: string | undefined, env?: NodeJS.ProcessEnv): HomeInstance;
|
|
25
|
+
/** Whether a home lists an instance at all. */
|
|
26
|
+
export declare function listed(home: HomeFile | null, origin: string): boolean;
|
|
27
|
+
/** Whether an entry reads signed in: an account, not marked signed out. */
|
|
28
|
+
export declare function signedIn(entry: HomeInstance | undefined): boolean;
|
|
29
|
+
/** An origin's host, with its port where it is not the scheme's default. */
|
|
30
|
+
export declare function hostOf(origin: string): string;
|
|
31
|
+
/** An instance as the reader knows it: its display name where the home
|
|
32
|
+
* holds one, else its host. */
|
|
33
|
+
export declare function nameOf(instance: HomeInstance): string;
|
|
34
|
+
/** The credential store of one instance under the home (storage-19). */
|
|
35
|
+
export declare function credentialsOf(dir: string, origin: string, scope?: AuthScope): CredentialStore;
|
|
36
|
+
/** The client of one instance, signing in as this device (git-host-3). */
|
|
37
|
+
export declare function clientOf(origin: string, credentials: CredentialStore, onSignedOut?: () => void, scope?: AuthScope): GitHostClient;
|
|
38
|
+
/** A host's answer in the CLI's words (git-host-11, cli-13): what the
|
|
39
|
+
* Space or registry said, its own words as the detail, and what to do —
|
|
40
|
+
* `again`, the command that tries once more — never a secret. An answer
|
|
41
|
+
* of no known kind, such as a page that is not there, comes from an
|
|
42
|
+
* address that is no Space or registry. */
|
|
43
|
+
export declare function hostCause(error: unknown, name: string, again?: string): string;
|
|
44
|
+
/** Cache a registry's display name as its descriptor gave it. A cache
|
|
45
|
+
* never makes the home: where it is missing, nothing is written unless
|
|
46
|
+
* `create`; a write that fails leaves the name uncached. */
|
|
47
|
+
export declare function cacheRegistryName(dir: string, origin: string, displayName: string, create?: boolean): void;
|
|
48
|
+
/** The display name a registry's public descriptor gives
|
|
49
|
+
* (environments-39), read with no credential; null where it does not
|
|
50
|
+
* answer in time or names none. */
|
|
51
|
+
export declare function describedRegistryName(origin: string): Promise<string | null>;
|
|
52
|
+
/** A registry as the reader knows it (cli-22): the display name its
|
|
53
|
+
* descriptor gives — read at login and kept in the home's cache, read
|
|
54
|
+
* again where none is cached or the cached one is a day old — else its
|
|
55
|
+
* origin's host; never the name of a Space entry at its origin. */
|
|
56
|
+
export declare function registryName(dir: string, origin: string): Promise<string>;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
// The home the package commands act on (cli-1, storage-1, storage-2):
|
|
4
|
+
// `SPEX_HOME` where set, else `~/.spex`. The CLI reads `home.yaml` with
|
|
5
|
+
// the library, checking no pairing — the app checks its own — and
|
|
6
|
+
// writes it only where a command records something; it never reads or
|
|
7
|
+
// writes `workspace/`, a clone, the lease, a session or a record.
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
9
|
+
import { hostname, homedir } from "node:os";
|
|
10
|
+
import { dirname, join, resolve } from "node:path";
|
|
11
|
+
import { RegistryClient } from "../lib/environment/registry.js";
|
|
12
|
+
import { writeApplicationBytes } from "../lib/files.js";
|
|
13
|
+
import { fileCredentialStore, GitHostClient, HostError, retryAfterSeconds } from "../lib/git-host.js";
|
|
14
|
+
import { defaultInstanceOf, homeFilePath, instanceAt, readHomeFile } from "../lib/home-file.js";
|
|
15
|
+
import { canonicalOrigin, hostUrlFor, instanceFolder, OriginRefused, parseOrigin } from "../lib/instance.js";
|
|
16
|
+
import { Refusal } from "./run.js";
|
|
17
|
+
/** The home's folder: a nonempty `SPEX_HOME`, else `~/.spex`. */
|
|
18
|
+
export function homeDir(env = process.env) {
|
|
19
|
+
const named = env.SPEX_HOME?.trim();
|
|
20
|
+
return named ? resolve(named) : join(homedir(), ".spex");
|
|
21
|
+
}
|
|
22
|
+
/** `home.yaml` as it stands, null where none stands; one that will not
|
|
23
|
+
* read refuses the command naming the file and the library's reason. */
|
|
24
|
+
export function readHome(dir) {
|
|
25
|
+
try {
|
|
26
|
+
return readHomeFile(dir);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
30
|
+
const file = homeFilePath(dir);
|
|
31
|
+
// A layout this Spex does not read is a newer Spex's: update Spex.
|
|
32
|
+
const next = /unsupported format/.test(reason) ? "update Spex" : "fix the file, then retry";
|
|
33
|
+
throw new Refusal(`${reason.startsWith(file) ? `${file} cannot be read${reason.slice(file.length)}` : `${file} cannot be read: ${reason}`} — ${next}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Why the origin rule refuses an address, in the reader's words. */
|
|
37
|
+
const ADDRESS_RULES = {
|
|
38
|
+
invalid: "it is not a URL",
|
|
39
|
+
scheme: "it is neither http nor https",
|
|
40
|
+
user: "it carries a user or a password",
|
|
41
|
+
path: "it names a path",
|
|
42
|
+
query: "it carries a query",
|
|
43
|
+
fragment: "it carries a fragment",
|
|
44
|
+
host: "its host is not a plain domain name",
|
|
45
|
+
port: "its port is not from 1 to 65535",
|
|
46
|
+
insecure: "http works only on this computer",
|
|
47
|
+
};
|
|
48
|
+
/** An address as a reader is shown it: any user info it carries, which
|
|
49
|
+
* may hold a secret, stands as `…`. */
|
|
50
|
+
export function shownAddress(value) {
|
|
51
|
+
return value.trim().replace(/^([a-z][a-z0-9+.-]*:\/\/)[^/?#]*@/i, "$1…@");
|
|
52
|
+
}
|
|
53
|
+
/** An address the origin rule refuses, for `flag`, as one phrase ending
|
|
54
|
+
* in what to do (cli-13). */
|
|
55
|
+
export function addressRefusal(flag, value, error, example = "https://spex.sublang.ai") {
|
|
56
|
+
return new Refusal(`${flag} ${shownAddress(value)}: ${ADDRESS_RULES[error.reason]} — enter just the address, like ${example}`);
|
|
57
|
+
}
|
|
58
|
+
/** The canonical origin `--space` names; one the origin rule refuses
|
|
59
|
+
* is refused saying why. */
|
|
60
|
+
export function spaceOption(value) {
|
|
61
|
+
try {
|
|
62
|
+
return canonicalOrigin(value);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
if (!(error instanceof OriginRefused))
|
|
66
|
+
throw error;
|
|
67
|
+
throw addressRefusal("--space", value, error);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** The home's default instance (storage-2): the first listed, else the
|
|
71
|
+
* origin it was created with; for a home not yet made, the origin a new
|
|
72
|
+
* one would record. */
|
|
73
|
+
export function defaultInstance(home, env = process.env) {
|
|
74
|
+
if (home)
|
|
75
|
+
return defaultInstanceOf(home);
|
|
76
|
+
let origin;
|
|
77
|
+
try {
|
|
78
|
+
origin = hostUrlFor(env);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (!(error instanceof OriginRefused))
|
|
82
|
+
throw error;
|
|
83
|
+
throw addressRefusal("SPEX_HOST_URL", env.SPEX_HOST_URL?.trim() ?? "", error);
|
|
84
|
+
}
|
|
85
|
+
return { origin, clientId: "spex" };
|
|
86
|
+
}
|
|
87
|
+
/** The instance a command names: `--space`, else the default. */
|
|
88
|
+
export function namedInstance(home, space, env = process.env) {
|
|
89
|
+
if (space === undefined)
|
|
90
|
+
return defaultInstance(home, env);
|
|
91
|
+
const origin = spaceOption(space);
|
|
92
|
+
return (home && instanceAt(home, origin)) ?? { origin, clientId: "spex" };
|
|
93
|
+
}
|
|
94
|
+
/** Whether a home lists an instance at all. */
|
|
95
|
+
export function listed(home, origin) {
|
|
96
|
+
return home !== null && instanceAt(home, origin) !== undefined;
|
|
97
|
+
}
|
|
98
|
+
/** Whether an entry reads signed in: an account, not marked signed out. */
|
|
99
|
+
export function signedIn(entry) {
|
|
100
|
+
return entry?.account !== undefined && entry.signedOut !== true;
|
|
101
|
+
}
|
|
102
|
+
/** An origin's host, with its port where it is not the scheme's default. */
|
|
103
|
+
export function hostOf(origin) {
|
|
104
|
+
const { host, port } = parseOrigin(origin);
|
|
105
|
+
return port === null ? host : `${host}:${port}`;
|
|
106
|
+
}
|
|
107
|
+
/** An instance as the reader knows it: its display name where the home
|
|
108
|
+
* holds one, else its host. */
|
|
109
|
+
export function nameOf(instance) {
|
|
110
|
+
return instance.displayName ?? hostOf(instance.origin);
|
|
111
|
+
}
|
|
112
|
+
/** The credential store of one instance under the home (storage-19). */
|
|
113
|
+
export function credentialsOf(dir, origin, scope = "space") {
|
|
114
|
+
return fileCredentialStore(dir, origin, scope);
|
|
115
|
+
}
|
|
116
|
+
/** The client of one instance, signing in as this device (git-host-3). */
|
|
117
|
+
export function clientOf(origin, credentials, onSignedOut, scope = "space") {
|
|
118
|
+
let device = "this device";
|
|
119
|
+
try {
|
|
120
|
+
device = hostname() || device;
|
|
121
|
+
}
|
|
122
|
+
catch { /* the label's fallback */ }
|
|
123
|
+
return new GitHostClient({ url: origin, scope, label: `spex on ${device}`, credentials, ...(onSignedOut ? { onSignedOut } : {}) });
|
|
124
|
+
}
|
|
125
|
+
/** A host's answer in the CLI's words (git-host-11, cli-13): what the
|
|
126
|
+
* Space or registry said, its own words as the detail, and what to do —
|
|
127
|
+
* `again`, the command that tries once more — never a secret. An answer
|
|
128
|
+
* of no known kind, such as a page that is not there, comes from an
|
|
129
|
+
* address that is no Space or registry. */
|
|
130
|
+
export function hostCause(error, name, again = "retry") {
|
|
131
|
+
if (!(error instanceof HostError)) {
|
|
132
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
133
|
+
return / — /.test(message) ? message : `${message} — ${again}`;
|
|
134
|
+
}
|
|
135
|
+
switch (error.kind) {
|
|
136
|
+
case "unreachable": return `${name} could not be reached — check the connection, then retry`;
|
|
137
|
+
case "rate_limited": {
|
|
138
|
+
const seconds = retryAfterSeconds(error.retryAfter);
|
|
139
|
+
return `${name} asked to wait${seconds === null ? "" : ` ${seconds} seconds`} — retry then`;
|
|
140
|
+
}
|
|
141
|
+
case "reauth": return `sign-in expired at ${name} — spex login`;
|
|
142
|
+
case "host_refused": return `${name} refused (${error.words ?? error.code ?? "no reason given"}) — ${again}`;
|
|
143
|
+
default: return `${name} gave no answer Spex reads (${error.code ?? error.kind}${error.words ? `: ${error.words}` : ""}) — check the address, then retry`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// -- a registry's name (cli-22) -----------------------------------------------
|
|
147
|
+
/** How long a registry's display name read from its descriptor stands
|
|
148
|
+
* before a command naming it reads the descriptor again. */
|
|
149
|
+
const REGISTRY_NAME_TTL_MS = 24 * 60 * 60_000;
|
|
150
|
+
/** Where the home keeps a registry's descriptor as fetched metadata,
|
|
151
|
+
* under `cache/`, which may be deleted at any time (environments-7). */
|
|
152
|
+
function registryNameFile(dir, origin) {
|
|
153
|
+
return join(dir, "cache", "registries", `${instanceFolder(origin)}.json`);
|
|
154
|
+
}
|
|
155
|
+
/** The display name cached for a registry, with when it was read; null
|
|
156
|
+
* where none is, or it does not read. */
|
|
157
|
+
function cachedRegistryName(dir, origin) {
|
|
158
|
+
try {
|
|
159
|
+
const value = JSON.parse(readFileSync(registryNameFile(dir, origin), "utf8"));
|
|
160
|
+
if (value.origin !== origin || typeof value.displayName !== "string" || value.displayName.trim() === "" || typeof value.readAt !== "number")
|
|
161
|
+
return null;
|
|
162
|
+
return { displayName: value.displayName, readAt: value.readAt };
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Cache a registry's display name as its descriptor gave it. A cache
|
|
169
|
+
* never makes the home: where it is missing, nothing is written unless
|
|
170
|
+
* `create`; a write that fails leaves the name uncached. */
|
|
171
|
+
export function cacheRegistryName(dir, origin, displayName, create = false) {
|
|
172
|
+
if (!create && !existsSync(dir))
|
|
173
|
+
return;
|
|
174
|
+
try {
|
|
175
|
+
const file = registryNameFile(dir, origin);
|
|
176
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
177
|
+
writeApplicationBytes(file, JSON.stringify({ origin, displayName, readAt: Date.now() }));
|
|
178
|
+
}
|
|
179
|
+
catch { /* the name stays uncached */ }
|
|
180
|
+
}
|
|
181
|
+
/** The display name a registry's public descriptor gives
|
|
182
|
+
* (environments-39), read with no credential; null where it does not
|
|
183
|
+
* answer in time or names none. */
|
|
184
|
+
export async function describedRegistryName(origin) {
|
|
185
|
+
try {
|
|
186
|
+
const name = (await new RegistryClient({ url: origin }).describe()).displayName.trim();
|
|
187
|
+
return name === "" ? null : name;
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** A registry as the reader knows it (cli-22): the display name its
|
|
194
|
+
* descriptor gives — read at login and kept in the home's cache, read
|
|
195
|
+
* again where none is cached or the cached one is a day old — else its
|
|
196
|
+
* origin's host; never the name of a Space entry at its origin. */
|
|
197
|
+
export async function registryName(dir, origin) {
|
|
198
|
+
let at;
|
|
199
|
+
try {
|
|
200
|
+
at = canonicalOrigin(origin);
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return origin;
|
|
204
|
+
}
|
|
205
|
+
const cached = cachedRegistryName(dir, at);
|
|
206
|
+
if (cached && Date.now() - cached.readAt < REGISTRY_NAME_TTL_MS)
|
|
207
|
+
return cached.displayName;
|
|
208
|
+
const described = await describedRegistryName(at);
|
|
209
|
+
if (described !== null) {
|
|
210
|
+
cacheRegistryName(dir, at, described);
|
|
211
|
+
return described;
|
|
212
|
+
}
|
|
213
|
+
return cached?.displayName ?? hostOf(at);
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=home.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
// `spex install`, or `spex i` (cli-4, environments-7): install what
|
|
4
|
+
// `spex.lock` records and place the skills for the agents and the specs
|
|
5
|
+
// in `specs/`, writing the lock first where it is missing or `spex.yaml`
|
|
6
|
+
// changed, and print each package installed with its version, where its
|
|
7
|
+
// spec stands — an edited copy left as the reader's — and where each of
|
|
8
|
+
// its skills went. A lock installing as it stands rewrites no file. The
|
|
9
|
+
// app's built-in package that no seed and no registry serves is named as
|
|
10
|
+
// the Spex app's to install.
|
|
11
|
+
import { isPathSource } from "../lib/environment/index.js";
|
|
12
|
+
import { Environment } from "./environment.js";
|
|
13
|
+
import { leftToAppLine, packageStates, settle, skillLines, specLines, versionOf } from "./settle.js";
|
|
14
|
+
import { parsed, Refusal } from "./run.js";
|
|
15
|
+
export const install = async (args) => {
|
|
16
|
+
parsed("install", args, {}, { min: 0, max: 0 });
|
|
17
|
+
const env = new Environment();
|
|
18
|
+
if (env.readRequests() === null)
|
|
19
|
+
throw new Refusal(`no spex.yaml in ${env.root} — spex add <org>/<pkg>`);
|
|
20
|
+
const settled = await settle(env, "install");
|
|
21
|
+
if (settled === null)
|
|
22
|
+
return;
|
|
23
|
+
const { lock, install: report } = settled;
|
|
24
|
+
const lines = [];
|
|
25
|
+
if (settled.lockWritten)
|
|
26
|
+
lines.push(settled.staleBefore === null ? "Wrote spex.lock" : `Updated spex.lock: ${settled.staleBefore.join("; ")}`);
|
|
27
|
+
const names = Object.keys(lock.packages).sort();
|
|
28
|
+
const states = packageStates(env.root, lock);
|
|
29
|
+
for (const name of names) {
|
|
30
|
+
const resolution = lock.packages[name];
|
|
31
|
+
const version = versionOf(env.root, name, resolution);
|
|
32
|
+
if (isPathSource(resolution.source)) {
|
|
33
|
+
lines.push(report.missingPaths.includes(name) ? `Files missing for ${name} ${version}: no folder at ${resolution.source.path}` : `Using ${name} ${version} from folder ${resolution.source.path}`);
|
|
34
|
+
}
|
|
35
|
+
else if (report.left?.includes(name)) {
|
|
36
|
+
lines.push(leftToAppLine(name, version));
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
lines.push(report.installed.includes(name) ? `Installed ${name} ${version}` : `Already installed ${name} ${version}`);
|
|
41
|
+
}
|
|
42
|
+
lines.push(...specLines(settled.exported.specs, name), ...skillLines(env.root, lock, name, states.get(name)));
|
|
43
|
+
}
|
|
44
|
+
if (names.length === 0)
|
|
45
|
+
lines.push("Nothing added yet — spex add <org>/<pkg>");
|
|
46
|
+
console.log(lines.join("\n"));
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=install.js.map
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
// `spex list`, or `spex ls` (cli-6, environments-14): the packages of
|
|
4
|
+
// this folder as its files stand, in a project's Packages tab's words
|
|
5
|
+
// (DR-172) — the folder and its registry by name; each package added,
|
|
6
|
+
// with its version or folder; each package installed, with its version,
|
|
7
|
+
// where it comes from, the packages it came for and whether its files
|
|
8
|
+
// stand, beneath one providing a spec where that spec stands in
|
|
9
|
+
// `specs/` and the packages its links wait for, read from the files,
|
|
10
|
+
// their marker lines and the install record as an install would find
|
|
11
|
+
// them, with no placement run (environments-41); each
|
|
12
|
+
// skill with the agent folders it is placed in; and, where `spex.lock`
|
|
13
|
+
// is missing or stale, or a package is not installed, the command to
|
|
14
|
+
// run: `spex update` for a stale lock, as the Packages tab's chip offers
|
|
15
|
+
// Update, else `spex install`.
|
|
16
|
+
import { BUILTIN_PACKAGE_NAME, isGitRequest, isPathRequest, isPathSource, readSpecPlacements } from "../lib/environment/index.js";
|
|
17
|
+
import { Environment } from "./environment.js";
|
|
18
|
+
import { gitLabel, gitLocators, packageStates, placeText, skillPlaces, sourceLabel, specLines, staleReasons, table, unnamedText, versionOf } from "./settle.js";
|
|
19
|
+
import { parsed } from "./run.js";
|
|
20
|
+
/** A package as `spex.yaml` adds it: its version, its folder, or its Git
|
|
21
|
+
* repository at a branch, tag or commit. */
|
|
22
|
+
function addedLabel(request) {
|
|
23
|
+
if (isPathRequest(request))
|
|
24
|
+
return `folder ${request.path}`;
|
|
25
|
+
if (isGitRequest(request))
|
|
26
|
+
return `${gitLabel(request.git)} at ${request.rev}${request.path ? `, folder ${request.path}` : ""}`;
|
|
27
|
+
return request.version;
|
|
28
|
+
}
|
|
29
|
+
/** The last line of a stale lock: what changed, and what to do — a
|
|
30
|
+
* folder package's `meta.yaml` that cannot be read fixed first. */
|
|
31
|
+
function staleLine(reasons) {
|
|
32
|
+
if (reasons.includes("spex.yaml is missing"))
|
|
33
|
+
return "spex.yaml is missing — spex add <org>/<pkg>";
|
|
34
|
+
const unreadable = reasons.map((reason) => / changed in (.+): its meta\.yaml cannot be read: /.exec(reason)?.[1]).filter((path) => path !== undefined);
|
|
35
|
+
if (unreadable.length > 0)
|
|
36
|
+
return `${reasons.join("; ")} — fix ${unreadable.map((path) => `${path}/meta.yaml`).join(", ")}, then spex update`;
|
|
37
|
+
return `${reasons.join("; ")} — spex update`;
|
|
38
|
+
}
|
|
39
|
+
export const list = async (args) => {
|
|
40
|
+
parsed("list", args, {}, { min: 0, max: 0 });
|
|
41
|
+
const env = new Environment();
|
|
42
|
+
const read = env.readRequests();
|
|
43
|
+
const locked = env.readLock();
|
|
44
|
+
if (read === null && locked === null) {
|
|
45
|
+
console.log(`No packages in ${env.root} yet — spex add <org>/<pkg>`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const registry = await env.registryName(env.registryOf(read?.requests ?? null).origin);
|
|
49
|
+
const lines = [`Folder ${env.root}`, `Registry ${registry}`];
|
|
50
|
+
const locators = gitLocators(read?.requests ?? null, locked?.lock ?? null);
|
|
51
|
+
const added = Object.entries(read?.requests.packages ?? {}).sort(([a], [b]) => a.localeCompare(b));
|
|
52
|
+
if (added.length > 0)
|
|
53
|
+
lines.push("Added", ...table(added.map(([name, request]) => [name, addedLabel(request)])));
|
|
54
|
+
else if (read !== null)
|
|
55
|
+
lines.push("Nothing added yet — spex add <org>/<pkg>");
|
|
56
|
+
if (locked === null) {
|
|
57
|
+
if (added.length > 0)
|
|
58
|
+
lines.push("Not installed yet — spex install");
|
|
59
|
+
console.log(lines.join("\n"));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const { lock } = locked;
|
|
63
|
+
const names = Object.keys(lock.packages).sort();
|
|
64
|
+
const states = packageStates(env.root, lock);
|
|
65
|
+
if (names.length > 0) {
|
|
66
|
+
const rows = table(names.map((name) => {
|
|
67
|
+
const resolution = lock.packages[name];
|
|
68
|
+
const requiredBy = resolution.requiredBy.length > 0 ? `for ${resolution.requiredBy.join(", ")}` : "";
|
|
69
|
+
return [name, versionOf(env.root, name, resolution), sourceLabel(name, resolution), requiredBy, states.get(name)];
|
|
70
|
+
}));
|
|
71
|
+
const specs = readSpecPlacements(env.root, lock);
|
|
72
|
+
lines.push("Installed", ...names.flatMap((name, at) => [rows[at], ...specLines(specs, name, " ")]));
|
|
73
|
+
}
|
|
74
|
+
const skills = names.flatMap((name) => skillPlaces(env.root, lock, name, states.get(name)).map((place) => [place.skill, placeText(place)]));
|
|
75
|
+
if (skills.length > 0)
|
|
76
|
+
lines.push("Skills for agents", ...table(skills));
|
|
77
|
+
const stale = staleReasons(env.root, lock, read?.text ?? null);
|
|
78
|
+
if (stale !== null)
|
|
79
|
+
lines.push(staleLine(stale));
|
|
80
|
+
// A lock that stands with files an install places not in place, as
|
|
81
|
+
// after a clone; the built-in waits for the Spex app (cli-4), and a
|
|
82
|
+
// folder package's files are its folder's.
|
|
83
|
+
else if (names.some((name) => name !== BUILTIN_PACKAGE_NAME && !isPathSource(lock.packages[name].source) && states.get(name) !== "installed"))
|
|
84
|
+
lines.push("Not installed yet — spex install");
|
|
85
|
+
console.log(unnamedText(lines.join("\n"), locators));
|
|
86
|
+
};
|
|
87
|
+
//# sourceMappingURL=list.js.map
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
// `spex login [--registry <url> | --space <url>] [--force]` (cli-10,
|
|
4
|
+
// git-host-3, DR-168): sign this computer in by the device flow at this
|
|
5
|
+
// folder's registry for its packages, or at the registry or Space named.
|
|
6
|
+
// A registry sign-in keeps its app token alone and caches the registry's
|
|
7
|
+
// display name (cli-22); a Space sign-in records the Space in
|
|
8
|
+
// `home.yaml` with its account — the first one recorded becoming the
|
|
9
|
+
// default — while the library's client keeps its app token in
|
|
10
|
+
// `local/credentials.yaml` (storage-19).
|
|
11
|
+
import { anotherAccountError, HostError, } from "../lib/git-host.js";
|
|
12
|
+
import { homeFilePath, instanceAt, readHomeFile, updateHomeFile, withDisplayName, withSignIn } from "../lib/home-file.js";
|
|
13
|
+
import { authTarget } from "./auth-target.js";
|
|
14
|
+
import { instanceFolder } from "../lib/instance.js";
|
|
15
|
+
import { cacheRegistryName, clientOf, credentialsOf, describedRegistryName, homeDir, hostCause, hostOf, listed, nameOf, readHome, registryName, signedIn } from "./home.js";
|
|
16
|
+
import { Interrupted, options, Refusal } from "./run.js";
|
|
17
|
+
/** A sign-in that ended without an account, as one phrase ending in
|
|
18
|
+
* what to do (cli-13). */
|
|
19
|
+
function signInCause(error, name) {
|
|
20
|
+
if (!(error instanceof HostError) || error.cause === undefined)
|
|
21
|
+
return `not signed in: ${hostCause(error, name, "spex login to try again")}`;
|
|
22
|
+
switch (error.cause) {
|
|
23
|
+
case "denied": return `not signed in: the code was denied at ${name} — spex login to try again`;
|
|
24
|
+
case "expired": return `not signed in: the code expired at ${name} — spex login to try again`;
|
|
25
|
+
case "account": return `not signed in: this computer is signed in at ${name} as @${error.words} — sign in there as @${error.words}`;
|
|
26
|
+
case "stopped": return `not signed in: your sign-in at ${name} changed meanwhile — spex login to try again`;
|
|
27
|
+
case "unreachable": return `not signed in: ${name} could not be reached — check the connection, then retry`;
|
|
28
|
+
default: return `not signed in: ${name} refused the sign-in${error.words ? ` (${error.words})` : error.code ? ` (${error.code})` : ""} — spex login to try again`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** A store holding one pair in memory alone, for a device revoked
|
|
32
|
+
* through it without writing anything anywhere. */
|
|
33
|
+
function heldOnly(pair) {
|
|
34
|
+
let held = pair;
|
|
35
|
+
return {
|
|
36
|
+
read: async () => held,
|
|
37
|
+
replace: (expected, next) => {
|
|
38
|
+
if (expected !== held && JSON.stringify(expected) !== JSON.stringify(held))
|
|
39
|
+
return false;
|
|
40
|
+
held = next;
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export const login = async (args, interrupt) => {
|
|
46
|
+
const values = options("login", args, { space: { type: "string" }, registry: { type: "string" }, force: { type: "boolean" } });
|
|
47
|
+
const dir = homeDir();
|
|
48
|
+
const home = readHome(dir);
|
|
49
|
+
const { target, scope } = authTarget(home, values);
|
|
50
|
+
const origin = target.origin;
|
|
51
|
+
// A registry is named by its own descriptor, a Space by its home entry
|
|
52
|
+
// (cli-22).
|
|
53
|
+
let name = scope === "registry" ? await registryName(dir, origin) : nameOf(target);
|
|
54
|
+
// No two Spaces at one folder (storage-2): refused before the flow.
|
|
55
|
+
if (scope === "space" && home && !listed(home, origin)) {
|
|
56
|
+
const folder = instanceFolder(origin);
|
|
57
|
+
const clash = home.instances.find((entry) => instanceFolder(entry.origin) === folder);
|
|
58
|
+
if (clash)
|
|
59
|
+
throw new Refusal(`${hostOf(origin)} and ${hostOf(clash.origin)} would share one folder on this computer — sign in at ${clash.origin} instead`);
|
|
60
|
+
}
|
|
61
|
+
const credentials = credentialsOf(dir, origin, scope);
|
|
62
|
+
let held;
|
|
63
|
+
try {
|
|
64
|
+
held = await credentials.read();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new Refusal(`your sign-in at ${name} cannot be read: ${error.message} — spex logout, then spex login`);
|
|
68
|
+
}
|
|
69
|
+
const entry = home ? instanceAt(home, origin) : undefined;
|
|
70
|
+
if (held && (scope === "registry" || signedIn(entry)) && values.force !== true) {
|
|
71
|
+
let login = entry?.account?.login;
|
|
72
|
+
if (scope === "registry") {
|
|
73
|
+
try {
|
|
74
|
+
login = (await clientOf(origin, credentials, undefined, scope).me()).login;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
// A sign-in the registry no longer accepts is gone: sign in anew.
|
|
78
|
+
if ((await credentials.read().catch(() => held)) !== null)
|
|
79
|
+
throw new Refusal(hostCause(error, name));
|
|
80
|
+
login = undefined;
|
|
81
|
+
held = null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (held) {
|
|
85
|
+
// The command that signs in again here, the target named as given.
|
|
86
|
+
const target = values.space !== undefined ? ` --space ${origin}` : values.registry !== undefined ? ` --registry ${origin}` : "";
|
|
87
|
+
console.log(`Already signed in at ${name} as @${login} — spex login${target} --force signs in again`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const client = clientOf(origin, credentials, undefined, scope);
|
|
92
|
+
// The registry's or Space's display name, read while the person
|
|
93
|
+
// approves; a sign-in goes on where it does not answer.
|
|
94
|
+
const described = scope === "registry" ? describedRegistryName(origin) : client.describe().then((description) => description.displayName || null, () => null);
|
|
95
|
+
// At a listed instance, only its entry's account (git-host-2): read in
|
|
96
|
+
// the turn that would store the token, so another stores nothing.
|
|
97
|
+
const admit = (account) => {
|
|
98
|
+
if (scope === "registry")
|
|
99
|
+
return null;
|
|
100
|
+
let recorded;
|
|
101
|
+
try {
|
|
102
|
+
recorded = readHomeFile(dir);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const listedAccount = recorded ? instanceAt(recorded, origin)?.account : undefined;
|
|
108
|
+
return listedAccount && listedAccount.id !== account.id ? anotherAccountError(listedAccount.login) : null;
|
|
109
|
+
};
|
|
110
|
+
let flow;
|
|
111
|
+
let stopped = false;
|
|
112
|
+
// An interrupt ends the wait for the flow's start at once: no token
|
|
113
|
+
// is stored before the flow ends, and a start answered late is
|
|
114
|
+
// cancelled.
|
|
115
|
+
let stop;
|
|
116
|
+
const stopping = new Promise((_, reject) => { stop = () => reject(new Error("stopped")); });
|
|
117
|
+
stopping.catch(() => undefined);
|
|
118
|
+
const release = interrupt.hold(() => {
|
|
119
|
+
stopped = true;
|
|
120
|
+
flow?.cancel();
|
|
121
|
+
stop();
|
|
122
|
+
});
|
|
123
|
+
let account;
|
|
124
|
+
try {
|
|
125
|
+
const starting = client.startDeviceSignIn({ admit });
|
|
126
|
+
try {
|
|
127
|
+
flow = await Promise.race([starting, stopping]);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (stopped) {
|
|
131
|
+
starting.then((late) => late.cancel(), () => undefined);
|
|
132
|
+
throw new Interrupted("interrupted; nothing recorded");
|
|
133
|
+
}
|
|
134
|
+
throw new Refusal(signInCause(error, name));
|
|
135
|
+
}
|
|
136
|
+
if (stopped) {
|
|
137
|
+
flow.cancel();
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
console.log(`Sign in at ${name} with this code in your browser:`);
|
|
141
|
+
console.log(` URL: ${flow.verificationUri}`);
|
|
142
|
+
console.log(` Code: ${flow.userCode}`);
|
|
143
|
+
console.log(`Waiting for ${name} to confirm (Ctrl-C to stop)…`);
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
account = await flow.done;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
if (stopped)
|
|
150
|
+
throw new Interrupted("interrupted; nothing recorded");
|
|
151
|
+
throw new Refusal(signInCause(error, name));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
release();
|
|
156
|
+
}
|
|
157
|
+
// The token is stored; an interrupt now stops the wait for the
|
|
158
|
+
// display name, and the record is made before the command ends.
|
|
159
|
+
let skipName;
|
|
160
|
+
const nameSkipped = new Promise((resolve) => { skipName = () => resolve(null); });
|
|
161
|
+
const deferred = interrupt.hold(() => skipName());
|
|
162
|
+
try {
|
|
163
|
+
const displayName = await Promise.race([described, nameSkipped]);
|
|
164
|
+
if (scope === "registry") {
|
|
165
|
+
if (held)
|
|
166
|
+
await clientOf(origin, heldOnly(held), undefined, scope).signOut().catch(() => undefined);
|
|
167
|
+
// The registry's own name, kept for every command naming it (cli-22).
|
|
168
|
+
if (displayName !== null) {
|
|
169
|
+
cacheRegistryName(dir, origin, displayName, true);
|
|
170
|
+
name = displayName;
|
|
171
|
+
}
|
|
172
|
+
console.log(`Signed in at ${name} as @${account.login}.`);
|
|
173
|
+
if (interrupt.requested)
|
|
174
|
+
throw new Interrupted(`interrupted once signed in at ${name} as @${account.login}`);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
let recorded;
|
|
178
|
+
try {
|
|
179
|
+
recorded = updateHomeFile(dir, (current) => {
|
|
180
|
+
const next = withSignIn(current, origin, account, homeFilePath(dir));
|
|
181
|
+
return displayName === null ? next : withDisplayName(next, origin, displayName) ?? next;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
// Nothing recorded, so the pair the sign-in replaced is put back
|
|
186
|
+
// and the device just signed in is revoked from memory alone.
|
|
187
|
+
const issued = await credentials.read().catch(() => null);
|
|
188
|
+
if (issued && credentials.replace(issued, held)) {
|
|
189
|
+
await clientOf(origin, heldOnly(issued)).signOut().catch(() => undefined);
|
|
190
|
+
}
|
|
191
|
+
// The home file's own words name it and why; a layout from a
|
|
192
|
+
// newer Spex needs that Spex.
|
|
193
|
+
const reason = error.message;
|
|
194
|
+
const next = /unsupported format/.test(reason) ? "update Spex, then spex login again" : "fix the file, then spex login again";
|
|
195
|
+
throw new Refusal(`not signed in, nothing recorded: ${reason.includes(homeFilePath(dir)) ? reason : `${homeFilePath(dir)}: ${reason}`} — ${next}`);
|
|
196
|
+
}
|
|
197
|
+
// A forced sign-in replaced the device's former token: that device
|
|
198
|
+
// is revoked at the instance, so it lists this device once.
|
|
199
|
+
if (held)
|
|
200
|
+
await clientOf(origin, heldOnly(held)).signOut().catch(() => undefined);
|
|
201
|
+
const shown = displayName ?? name;
|
|
202
|
+
const isDefault = recorded.instances[0]?.origin === origin;
|
|
203
|
+
console.log(`Signed in at ${shown} as @${account.login}${isDefault ? " (default)" : ""}.`);
|
|
204
|
+
if (interrupt.requested)
|
|
205
|
+
throw new Interrupted(`interrupted once signed in at ${shown} as @${account.login}`);
|
|
206
|
+
}
|
|
207
|
+
finally {
|
|
208
|
+
deferred();
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
//# sourceMappingURL=login.js.map
|