@dreamlake/dreamlake-cli 0.100.0 → 0.102.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.
@@ -29,6 +29,8 @@ export declare function hostsApi(t: Transport, namespace: string): {
29
29
  durationSeconds: number;
30
30
  }) => Promise<unknown>;
31
31
  launch: (id: string) => Promise<unknown>;
32
+ movePreview(hostId: string, destinationNamespace: string): Promise<import("./host-moves.js").HostMovePreview>;
33
+ move(hostId: string, request: import("./host-moves.js").HostMoveRequest): Promise<import("./host-moves.js").HostMoveReceipt>;
32
34
  };
33
35
  export declare function serviceAccountsApi(t: Transport, namespace: string): {
34
36
  list: (query?: {
@@ -1,8 +1,10 @@
1
+ import { hostMoveApi } from "./host-moves.js";
1
2
  import { seg } from "./client.js";
2
3
  import { call, namespacePath } from "./common.js";
3
4
  export function hostsApi(t, namespace) {
4
5
  const base = namespacePath(namespace) + "/hosts", p = (id) => base + "/" + seg(id);
5
6
  return {
7
+ ...hostMoveApi(t, namespace),
6
8
  list: (query = {}) => call(t, "GET", base, undefined, query),
7
9
  status: (id) => call(t, "GET", p(id)),
8
10
  enroll: (input) => call(t, "POST", base + "/enrollments", input),
@@ -7,6 +7,9 @@ export type TurnCapabilities = {
7
7
  layoutActions?: 1;
8
8
  sessionControl?: 1;
9
9
  notesWrite?: 1;
10
+ hostMove?: 1;
11
+ toolsWrite?: 1;
12
+ memoryWrite?: 1;
10
13
  };
11
14
  export type AgentEvent = {
12
15
  type: "context";
@@ -64,6 +67,11 @@ export interface ConversationSummary {
64
67
  /** Parse `data: {...}` server-sent events; a frame that is not JSON is dropped, never guessed. */
65
68
  export declare function readAgentEvents(body: ReadableStream<Uint8Array>): AsyncGenerator<AgentEvent>;
66
69
  export declare function agentApi(t: Transport, options: Pick<ClientOptions, "remote" | "token" | "fetch">): {
70
+ namespaceScope: (namespace: string) => Promise<{
71
+ apiVersion: 1;
72
+ namespace: string;
73
+ allowed: true;
74
+ }>;
67
75
  browserActions: (conversationId: string) => Promise<{
68
76
  actions: unknown[];
69
77
  }>;
@@ -79,6 +87,7 @@ export declare function agentApi(t: Transport, options: Pick<ClientOptions, "rem
79
87
  restore: (conversationId: string) => Promise<{
80
88
  sessionId: string;
81
89
  status: string;
90
+ actingNamespace?: string;
82
91
  messages: {
83
92
  role: "user" | "assistant";
84
93
  content: string;
@@ -96,6 +105,7 @@ export declare function agentApi(t: Transport, options: Pick<ClientOptions, "rem
96
105
  chat(input: {
97
106
  message: string;
98
107
  sessionId?: string;
108
+ actingNamespace?: string;
99
109
  capabilities?: TurnCapabilities;
100
110
  page?: {
101
111
  pathname: string;
package/dist/api/agent.js CHANGED
@@ -38,6 +38,14 @@ export async function* readAgentEvents(body) {
38
38
  export function agentApi(t, options) {
39
39
  return {
40
40
  ...projectToolsApi(t),
41
+ namespaceScope: async (namespace) => {
42
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(namespace))
43
+ throw new Error('invalid agent namespace');
44
+ const result = await t.request('GET', '/agent/namespace-scope', { query: { namespace } });
45
+ if (result?.apiVersion !== 1 || result.namespace !== namespace || result.allowed !== true)
46
+ throw new Error('Agent namespace scope could not be verified');
47
+ return result;
48
+ },
41
49
  browserActions: (conversationId) => {
42
50
  if (!isObjectId(conversationId))
43
51
  throw new Error("expected a 24-hex conversation id");
@@ -58,6 +66,8 @@ export function agentApi(t, options) {
58
66
  },
59
67
  /** One turn as a stream of events. `page` is the browser context; a CLI passes a pathname only. */
60
68
  async *chat(input) {
69
+ if (input.actingNamespace !== undefined && !/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(input.actingNamespace))
70
+ throw new Error('invalid agent namespace');
61
71
  const doFetch = options.fetch ?? fetch;
62
72
  const response = await doFetch(options.remote.replace(/\/+$/, "") + "/agent/chat", {
63
73
  method: "POST",
@@ -71,6 +81,7 @@ export function agentApi(t, options) {
71
81
  body: JSON.stringify({
72
82
  message: input.message,
73
83
  ...(input.sessionId ? { sessionId: input.sessionId } : {}),
84
+ ...(input.actingNamespace !== undefined ? { actingNamespace: input.actingNamespace } : {}),
74
85
  ...(input.capabilities ? { capabilities: input.capabilities } : {}),
75
86
  page: input.page ?? { pathname: "/", layouts: [] },
76
87
  }),
@@ -0,0 +1,54 @@
1
+ import { type Transport } from './client.js';
2
+ /** Saved sessions stay at their original addresses; existing sharing is removed. */
3
+ export type HostMoveRetainedSessions = {
4
+ count: number;
5
+ policy: 'retain_original_namespace_make_private';
6
+ accessChanges: {
7
+ public: number;
8
+ shared: number;
9
+ };
10
+ };
11
+ /** Run history and acknowledged executions retain their original namespace and identities. */
12
+ export type HostMoveRetainedRuns = {
13
+ count: number;
14
+ terminal: number;
15
+ active: number;
16
+ policy: 'retain_original_namespace_acknowledged_runs';
17
+ };
18
+ export type HostMovePreview = {
19
+ apiVersion: 1;
20
+ hostId: string;
21
+ sourceNamespaceId: string;
22
+ destinationNamespaceId: string;
23
+ destinationNamespace: string;
24
+ name: string;
25
+ ownerUserId: string;
26
+ visibility: 'private';
27
+ revision: number;
28
+ previewHash: string;
29
+ blockers: string[];
30
+ executable: boolean;
31
+ sessions?: HostMoveRetainedSessions;
32
+ runs?: HostMoveRetainedRuns;
33
+ };
34
+ export type HostMoveRequest = {
35
+ destinationNamespace: string;
36
+ requestId: string;
37
+ expectedRevision: number;
38
+ previewHash: string;
39
+ };
40
+ export type HostMoveReceipt = Omit<HostMovePreview, 'previewHash' | 'blockers' | 'executable'> & {
41
+ operationId: string;
42
+ requestId: string;
43
+ recordedAt: string;
44
+ replayed: boolean;
45
+ };
46
+ export declare function validateHostMoveSessions(value: unknown): void;
47
+ export declare function validateHostMoveRuns(value: unknown): void;
48
+ export declare function validateHostMovePreview(value: unknown, hostId: string, destinationNamespace: string): HostMovePreview;
49
+ export declare function validateHostMoveReceipt(value: unknown, hostId: string, request: HostMoveRequest): HostMoveReceipt;
50
+ export declare function hostMoveApi(t: Transport, namespace: string): {
51
+ movePreview(hostId: string, destinationNamespace: string): Promise<HostMovePreview>;
52
+ /** After uncertainty replay exactly this request; no new key, preview or automatic retry. */
53
+ move(hostId: string, request: HostMoveRequest): Promise<HostMoveReceipt>;
54
+ };
@@ -0,0 +1,66 @@
1
+ import { seg } from './client.js';
2
+ const nonempty = (v) => typeof v === 'string' && v.length > 0 && v.length <= 256 && !/[\x00-\x1f\x7f]/.test(v);
3
+ const invalid = () => { throw new Error('Host move response could not be verified. Keep the same request and reconcile before retrying.'); };
4
+ const base = (namespace, hostId) => {
5
+ if (!nonempty(namespace) || !/^[a-f0-9]{24}$/.test(hostId))
6
+ invalid();
7
+ return `/namespaces/${seg(namespace)}/hosts/${hostId}/move`;
8
+ };
9
+ export function validateHostMoveSessions(value) {
10
+ if (value === undefined)
11
+ return;
12
+ const v = value;
13
+ if (!v || v.policy !== 'retain_original_namespace_make_private' || !Number.isSafeInteger(v.count) || v.count < 0 ||
14
+ !v.accessChanges || !['public', 'shared'].every(key => { const n = v.accessChanges[key]; return Number.isSafeInteger(n) && n >= 0 && n <= v.count; }))
15
+ invalid();
16
+ }
17
+ export function validateHostMoveRuns(value) {
18
+ if (value === undefined)
19
+ return;
20
+ const v = value;
21
+ if (!v || v.policy !== 'retain_original_namespace_acknowledged_runs' ||
22
+ ![v.count, v.terminal, v.active].every(n => Number.isSafeInteger(n) && n >= 0) || v.terminal + v.active !== v.count)
23
+ invalid();
24
+ }
25
+ function common(v, hostId, destinationNamespace) {
26
+ if (!v || v.apiVersion !== 1 || v.hostId !== hostId || v.destinationNamespace !== destinationNamespace ||
27
+ ![v.sourceNamespaceId, v.destinationNamespaceId, v.ownerUserId, v.name].every(nonempty) ||
28
+ v.visibility !== 'private' || !Number.isSafeInteger(v.revision) || v.revision < 0)
29
+ invalid();
30
+ validateHostMoveSessions(v.sessions);
31
+ validateHostMoveRuns(v.runs);
32
+ }
33
+ export function validateHostMovePreview(value, hostId, destinationNamespace) {
34
+ const v = value;
35
+ common(v, hostId, destinationNamespace);
36
+ if (!/^[a-f0-9]{64}$/.test(v.previewHash) || !Array.isArray(v.blockers) ||
37
+ v.blockers.some(b => !nonempty(b)) || typeof v.executable !== 'boolean' || v.executable !== (v.blockers.length === 0))
38
+ invalid();
39
+ return v;
40
+ }
41
+ export function validateHostMoveReceipt(value, hostId, request) {
42
+ const v = value;
43
+ common(v, hostId, request.destinationNamespace);
44
+ if (v.requestId !== request.requestId || !nonempty(v.operationId) || v.revision !== request.expectedRevision + 1 ||
45
+ !nonempty(v.recordedAt) || !Number.isFinite(Date.parse(v.recordedAt)) || typeof v.replayed !== 'boolean')
46
+ invalid();
47
+ return v;
48
+ }
49
+ export function hostMoveApi(t, namespace) {
50
+ return {
51
+ async movePreview(hostId, destinationNamespace) {
52
+ if (!nonempty(destinationNamespace) || !/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(destinationNamespace))
53
+ invalid();
54
+ const result = await t.request('POST', `${base(namespace, hostId)}/preview`, { json: { destinationNamespace } });
55
+ return validateHostMovePreview(result, hostId, destinationNamespace);
56
+ },
57
+ /** After uncertainty replay exactly this request; no new key, preview or automatic retry. */
58
+ async move(hostId, request) {
59
+ if (!request || !nonempty(request.destinationNamespace) || !/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(request.destinationNamespace) || !nonempty(request.requestId) || !/^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$/.test(request.requestId) ||
60
+ !Number.isSafeInteger(request.expectedRevision) || request.expectedRevision < 0 || request.expectedRevision >= 2147483647 || !/^[a-f0-9]{64}$/.test(request.previewHash) ||
61
+ Object.keys(request).some(k => !['destinationNamespace', 'requestId', 'expectedRevision', 'previewHash'].includes(k)))
62
+ invalid();
63
+ return validateHostMoveReceipt(await t.request('POST', base(namespace, hostId), { json: request }), hostId, request);
64
+ },
65
+ };
66
+ }
@@ -374,6 +374,8 @@ export declare function createApi(transport: Transport): {
374
374
  durationSeconds: number;
375
375
  }) => Promise<unknown>;
376
376
  launch: (id: string) => Promise<unknown>;
377
+ movePreview(hostId: string, destinationNamespace: string): Promise<import("./host-moves.js").HostMovePreview>;
378
+ move(hostId: string, request: import("./host-moves.js").HostMoveRequest): Promise<import("./host-moves.js").HostMoveReceipt>;
377
379
  };
378
380
  serviceAccounts: (namespace: string) => {
379
381
  list: (query?: {
@@ -1033,6 +1035,11 @@ export declare function createApi(transport: Transport): {
1033
1035
  export type DreamLakeClient = ReturnType<typeof createClient>;
1034
1036
  export declare function createClient(options: ClientOptions): {
1035
1037
  agent: {
1038
+ namespaceScope: (namespace: string) => Promise<{
1039
+ apiVersion: 1;
1040
+ namespace: string;
1041
+ allowed: true;
1042
+ }>;
1036
1043
  browserActions: (conversationId: string) => Promise<{
1037
1044
  actions: unknown[];
1038
1045
  }>;
@@ -1048,6 +1055,7 @@ export declare function createClient(options: ClientOptions): {
1048
1055
  restore: (conversationId: string) => Promise<{
1049
1056
  sessionId: string;
1050
1057
  status: string;
1058
+ actingNamespace?: string;
1051
1059
  messages: {
1052
1060
  role: "user" | "assistant";
1053
1061
  content: string;
@@ -1064,6 +1072,7 @@ export declare function createClient(options: ClientOptions): {
1064
1072
  chat(input: {
1065
1073
  message: string;
1066
1074
  sessionId?: string;
1075
+ actingNamespace?: string;
1067
1076
  capabilities?: import("./agent.js").TurnCapabilities;
1068
1077
  page?: {
1069
1078
  pathname: string;
@@ -1442,6 +1451,8 @@ export declare function createClient(options: ClientOptions): {
1442
1451
  durationSeconds: number;
1443
1452
  }) => Promise<unknown>;
1444
1453
  launch: (id: string) => Promise<unknown>;
1454
+ movePreview(hostId: string, destinationNamespace: string): Promise<import("./host-moves.js").HostMovePreview>;
1455
+ move(hostId: string, request: import("./host-moves.js").HostMoveRequest): Promise<import("./host-moves.js").HostMoveReceipt>;
1445
1456
  };
1446
1457
  serviceAccounts: (namespace: string) => {
1447
1458
  list: (query?: {
@@ -2111,3 +2122,4 @@ export { documentEdits, documentInsertion } from "./note-edits.js";
2111
2122
  export * from "./note-facade.js";
2112
2123
  export type { NoteReadingOptions, NoteReadingPage, NoteFocusedRead } from "./note-reading.js";
2113
2124
  export { lineRange, focusedRead, InvalidLineRange } from "./note-reading.js";
2125
+ export type { HostMovePreview, HostMoveRequest, HostMoveReceipt, HostMoveRetainedSessions, HostMoveRetainedRuns } from "./host-moves.js";
@@ -11,6 +11,8 @@ export declare function detectKind(file: string, override?: string): string | nu
11
11
  export interface ArtifactPushArgs extends DreamlakeOpts {
12
12
  file: string;
13
13
  entry?: string;
14
+ site?: boolean;
15
+ spa?: boolean;
14
16
  title?: string;
15
17
  kind?: string;
16
18
  artifactId?: string;
@@ -11,6 +11,7 @@ import { readFileSync, statSync } from "node:fs";
11
11
  import path from "node:path";
12
12
  import { uploadIncremental, MAX_SIZE } from "./incremental.js";
13
13
  import { packPages } from "./pages.js";
14
+ import { collectSite, uploadSite } from "./site.js";
14
15
  import { metaError, putArtifactCover, readCoverFile, registerArtifactMetaCommand } from "./meta.js";
15
16
  // ../prompt.js, NOT @inquirer/prompts directly. This file used to import the
16
17
  // inquirer prompt straight, which bypassed prompt.ts's `!process.stdin.isTTY`
@@ -86,10 +87,15 @@ function shellQuote(value) {
86
87
  export async function runDreamlakeArtifactPush(args, deps = {}) {
87
88
  const incremental = !args.full && args.incremental !== false;
88
89
  let content;
90
+ let site;
89
91
  let directory = false;
90
92
  try {
91
93
  const stat = statSync(args.file);
92
94
  directory = stat.isDirectory();
95
+ if (args.site && !directory)
96
+ throw new Error('--site requires a built static directory');
97
+ if (args.spa && !args.site)
98
+ throw new Error('--spa requires --site');
93
99
  if (!directory && !stat.isFile())
94
100
  throw new Error("not a file or directory");
95
101
  if (!directory && args.entry !== undefined)
@@ -100,7 +106,8 @@ export async function runDreamlakeArtifactPush(args, deps = {}) {
100
106
  fail("incremental artifacts are limited to 512 MiB");
101
107
  return 1;
102
108
  }
103
- content = directory ? packPages(args.file, args.entry) : readFileSync(args.file);
109
+ site = args.site ? collectSite(args.file, args.entry, args.spa) : undefined;
110
+ content = site ? site.content : directory ? packPages(args.file, args.entry) : readFileSync(args.file);
104
111
  }
105
112
  catch (error) {
106
113
  const missing = error.code === 'ENOENT';
@@ -141,6 +148,13 @@ export async function runDreamlakeArtifactPush(args, deps = {}) {
141
148
  const namespace = await requireNamespace(ctx, args.namespace);
142
149
  if (!namespace)
143
150
  return 1;
151
+ if (site) {
152
+ const capability = await jsonRequest('GET', `${ctx.remote}/artifacts/site-capabilities`, token);
153
+ if (capability.status !== 200 || capability.body?.schema !== 'dreamlake-artifact-files/1') {
154
+ fail('Static artifact hosting is unavailable on this server; no files were uploaded.');
155
+ return 1;
156
+ }
157
+ }
144
158
  // A title the user set once must survive every later push. Without
145
159
  // --title, ask the catalog first: an existing row keeps its title (the
146
160
  // registration below then carries none), a new one gets the file stem.
@@ -195,10 +209,14 @@ export async function runDreamlakeArtifactPush(args, deps = {}) {
195
209
  process.stdout.write(`Uploading ${path.basename(args.file)} (${kind})\n`);
196
210
  process.stdout.write(` namespace: ${namespace}\n`);
197
211
  process.stdout.write(` artifact: ${artifactId} v${version}\n`);
198
- process.stdout.write(` size: ${content.length} bytes\n`);
212
+ process.stdout.write(` size: ${site?.bytes ?? content.length} bytes\n`);
199
213
  try {
200
214
  let storedContent = content;
201
- if (incremental) {
215
+ if (site) {
216
+ const result = await uploadSite(site, backend);
217
+ process.stdout.write(` site: ${site.manifest.files.length} files, ${result.uploadedFiles} new files, ${result.uploadedBytes} bytes uploaded\n`);
218
+ }
219
+ else if (incremental) {
202
220
  const result = await uploadIncremental(content, backend);
203
221
  storedContent = result.content;
204
222
  process.stdout.write(` incremental: ${result.uploadedChunks} new chunks, ${result.uploadedBytes} compressed bytes uploaded, ${result.reusedBytes} source bytes reused\n`);
@@ -374,6 +392,8 @@ export function registerArtifactCommand(program) {
374
392
  .description("push a renderable artifact — a new version of --id (written straight to storage)")
375
393
  .argument("<file>", "file or self-contained HTML page directory to push")
376
394
  .option("--entry <path>", "entry page for a directory (default: index.html)")
395
+ .option("--site", "upload a static build with files loaded on demand (requires site hosting)")
396
+ .option("--spa", "serve the site entry for extensionless client routes (requires --site)")
377
397
  .option("--title <title>", "human title (default: file stem)")
378
398
  .option("--kind <kind>", "html, react, markdown, svg, code, mermaid (default: auto)")
379
399
  .option("--id <artifact-id>", "stable artifact id for versioning (default: slug of title)")
@@ -0,0 +1,16 @@
1
+ import { type DreamdbBackend, type UploadFile } from '../compat/s3-backend.js';
2
+ import { type SiteManifest } from './siteFormat.js';
3
+ /** Snapshot the manifest before any network writes; retain file metadata, not media buffers. */
4
+ export declare function collectSite(directory: string, entry?: string, spa?: boolean): {
5
+ manifest: SiteManifest;
6
+ objects: Map<string, UploadFile>;
7
+ json: Buffer<ArrayBuffer>;
8
+ hash: string;
9
+ content: Buffer<ArrayBuffer>;
10
+ bytes: number;
11
+ };
12
+ /** Conditional immutable writes; publish the manifest only after every file exists. */
13
+ export declare function uploadSite(site: ReturnType<typeof collectSite>, backend: DreamdbBackend): Promise<{
14
+ uploadedBytes: number;
15
+ uploadedFiles: number;
16
+ }>;
@@ -0,0 +1,125 @@
1
+ import { closeSync, constants, fstatSync, lstatSync, openSync, readdirSync, readSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { createHash } from 'node:crypto';
4
+ import { S3_SINGLE_PUT_MAX_BYTES } from '../compat/s3-backend.js';
5
+ import { encodeSiteDescriptor, SITE_MAX_FILES, SITE_MANIFEST_MAX_BYTES, siteFileKey, siteManifestKey, validSitePath, validateSiteManifest } from './siteFormat.js';
6
+ const sha = (bytes) => createHash('sha256').update(bytes).digest('hex');
7
+ /** Hash with bounded memory; a signed upload rejects source changes before publication. */
8
+ function inspectFile(file, html) {
9
+ const fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW);
10
+ try {
11
+ const before = fstatSync(fd);
12
+ if (!before.isFile() || !Number.isSafeInteger(before.size) || before.size > S3_SINGLE_PUT_MAX_BYTES)
13
+ throw new Error('A site file exceeds the 5 GiB single-PUT transport limit');
14
+ if (html && before.size > 32 * 1024 * 1024)
15
+ throw new Error('A site HTML page exceeds 32 MiB; move large assets into separate files');
16
+ const hash = createHash('sha256'), buffer = Buffer.allocUnsafe(1024 * 1024);
17
+ const decoder = html ? new TextDecoder('utf-8', { fatal: true }) : undefined;
18
+ let size = 0, count;
19
+ while ((count = readSync(fd, buffer, 0, buffer.length, null)) > 0) {
20
+ size += count;
21
+ if (size > before.size)
22
+ throw new Error('Site file changed while hashing; retry the push');
23
+ hash.update(buffer.subarray(0, count));
24
+ decoder?.decode(buffer.subarray(0, count), { stream: true });
25
+ }
26
+ decoder?.decode();
27
+ const after = fstatSync(fd);
28
+ if (size !== before.size || after.mtimeMs !== before.mtimeMs || after.ctimeMs !== before.ctimeMs)
29
+ throw new Error('Site file changed while hashing; retry the push');
30
+ return { path: file, size, sha256: hash.digest('hex'), mtimeMs: after.mtimeMs, ctimeMs: after.ctimeMs,
31
+ dev: after.dev, ino: after.ino };
32
+ }
33
+ finally {
34
+ closeSync(fd);
35
+ }
36
+ }
37
+ /** Snapshot the manifest before any network writes; retain file metadata, not media buffers. */
38
+ export function collectSite(directory, entry = 'index.html', spa = false) {
39
+ if (!lstatSync(directory).isDirectory())
40
+ throw new Error('Site must be a directory, not a symlink');
41
+ const manifest = { schema: 'dreamlake-artifact-files/1', entry, spa, files: [] };
42
+ const objects = new Map();
43
+ let bytes = 0;
44
+ let visited = 0;
45
+ function walk(relative, depth) {
46
+ if (depth > 32)
47
+ throw new Error('Artifact site nesting exceeds 32 directories');
48
+ for (const name of readdirSync(path.join(directory, relative)).sort()) {
49
+ if (++visited > SITE_MAX_FILES * 2)
50
+ throw new Error('Too many site directory entries');
51
+ const file = path.join(directory, relative, name);
52
+ const stat = lstatSync(file);
53
+ if (stat.isSymbolicLink())
54
+ throw new Error(`Symlinks are not supported: ${file}`);
55
+ if (name.startsWith('.'))
56
+ continue;
57
+ const local = relative ? `${relative}/${name}` : name;
58
+ if (!validSitePath(local))
59
+ throw new Error(`Invalid artifact path: ${local}`);
60
+ if (stat.isDirectory()) {
61
+ walk(local, depth + 1);
62
+ continue;
63
+ }
64
+ if (!stat.isFile())
65
+ throw new Error(`Not a regular file: ${local}`);
66
+ if (manifest.files.length >= SITE_MAX_FILES)
67
+ throw new Error('Artifact site exceeds 4096 files');
68
+ const data = inspectFile(file, /\.html?$/i.test(local));
69
+ bytes += data.size;
70
+ if (!Number.isSafeInteger(bytes))
71
+ throw new Error('Artifact site size exceeds safe integer accounting');
72
+ objects.set(data.sha256, data);
73
+ manifest.files.push({ path: local, sha256: data.sha256, size: data.size });
74
+ }
75
+ }
76
+ walk('', 0);
77
+ validateSiteManifest(manifest);
78
+ const json = Buffer.from(JSON.stringify(manifest));
79
+ if (json.length > SITE_MANIFEST_MAX_BYTES)
80
+ throw new Error('Artifact manifest exceeds 2 MiB');
81
+ const hash = sha(json);
82
+ return { manifest, objects, json, hash, content: Buffer.from(encodeSiteDescriptor(hash)), bytes };
83
+ }
84
+ /** Conditional immutable writes; publish the manifest only after every file exists. */
85
+ export async function uploadSite(site, backend) {
86
+ if (!backend.putFile)
87
+ throw new Error('Artifact site uploads require a streaming file backend');
88
+ let uploadedBytes = 0;
89
+ let uploadedFiles = 0;
90
+ let next = 0;
91
+ const entries = [...site.objects];
92
+ let failure;
93
+ async function put(key, bytes) {
94
+ if ((await backend.head(key)).exists)
95
+ return false;
96
+ const result = await backend.put(key, bytes, { ifNoneMatchStar: true });
97
+ if (result.status !== 'created' && !(await backend.head(key)).exists)
98
+ throw new Error('Artifact file upload conflict; retry the push');
99
+ return result.status === 'created';
100
+ }
101
+ await Promise.all(Array.from({ length: Math.min(8, entries.length) }, async () => {
102
+ while (next < entries.length && !failure) {
103
+ const [hash, data] = entries[next++];
104
+ try {
105
+ const key = siteFileKey(hash);
106
+ if ((await backend.head(key)).exists)
107
+ continue;
108
+ const result = await backend.putFile(key, data, { ifNoneMatchStar: true });
109
+ if (result.status !== 'created' && !(await backend.head(key)).exists)
110
+ throw new Error('Artifact file upload conflict; retry the push');
111
+ if (result.status === 'created') {
112
+ uploadedBytes += data.size;
113
+ uploadedFiles++;
114
+ }
115
+ }
116
+ catch (error) {
117
+ failure = error;
118
+ }
119
+ }
120
+ }));
121
+ if (failure)
122
+ throw failure;
123
+ await put(siteManifestKey(site.hash), site.json);
124
+ return { uploadedBytes, uploadedFiles };
125
+ }
@@ -0,0 +1,25 @@
1
+ export declare const SITE_PREFIX = "<!--DREAMLAKE-ARTIFACT-FILES/1-->\n<script type=\"application/json\" id=\"dreamlake-artifact-files\">";
2
+ export declare const SITE_SUFFIX = "</script>\n<p>This artifact requires a viewer with static-site support.</p>";
3
+ export declare const SITE_MAX_FILES = 4096;
4
+ export declare const SITE_MANIFEST_MAX_BYTES: number;
5
+ export interface SiteFile {
6
+ path: string;
7
+ sha256: string;
8
+ size: number;
9
+ }
10
+ export interface SiteManifest {
11
+ schema: 'dreamlake-artifact-files/1';
12
+ entry: string;
13
+ spa: boolean;
14
+ files: SiteFile[];
15
+ }
16
+ export interface SiteDescriptor {
17
+ manifest: string;
18
+ }
19
+ export declare const isHash: (s: unknown) => s is string;
20
+ export declare function validSitePath(s: unknown): s is string;
21
+ export declare function validateSiteManifest(value: unknown): SiteManifest;
22
+ export declare function encodeSiteDescriptor(manifest: string): string;
23
+ export declare function decodeSiteDescriptor(content: string): SiteDescriptor | null;
24
+ export declare const siteManifestKey: (hash: string) => string;
25
+ export declare const siteFileKey: (hash: string) => string;
@@ -0,0 +1,53 @@
1
+ // Wire contract shared with dreamlake-server/src/services/artifactSiteFormat.ts
2
+ // and dreamlake-ai/frame/siteFormat.ts. Never change v1 in place.
3
+ export const SITE_PREFIX = '<!--DREAMLAKE-ARTIFACT-FILES/1-->\n<script type="application/json" id="dreamlake-artifact-files">';
4
+ export const SITE_SUFFIX = '</script>\n<p>This artifact requires a viewer with static-site support.</p>';
5
+ export const SITE_MAX_FILES = 4096;
6
+ export const SITE_MANIFEST_MAX_BYTES = 2 * 1024 * 1024;
7
+ export const isHash = (s) => typeof s === 'string' && /^[a-f0-9]{64}$/.test(s);
8
+ export function validSitePath(s) {
9
+ return typeof s === 'string' && s.length > 0 && s.length <= 512 && s === s.normalize('NFC') &&
10
+ !/[\\%?#:\u0000-\u0020\u007f]/.test(s) &&
11
+ !s.startsWith('_dreamlake/') && s !== '_dreamlake' &&
12
+ s.split('/').every(p => !!p && !p.startsWith('.'));
13
+ }
14
+ export function validateSiteManifest(value) {
15
+ const m = value;
16
+ if (!m || m.schema !== 'dreamlake-artifact-files/1' || !validSitePath(m.entry) ||
17
+ !/\.html?$/i.test(m.entry) || typeof m.spa !== 'boolean' || !Array.isArray(m.files) ||
18
+ !m.files.length || m.files.length > SITE_MAX_FILES)
19
+ throw new Error('Invalid artifact site manifest');
20
+ const paths = new Set();
21
+ let total = 0;
22
+ for (const f of m.files) {
23
+ if (!f || !validSitePath(f.path) || !isHash(f.sha256) || !Number.isSafeInteger(f.size) || f.size < 0 ||
24
+ paths.has(f.path.toLowerCase()))
25
+ throw new Error('Invalid or duplicate artifact file');
26
+ paths.add(f.path.toLowerCase());
27
+ total += f.size;
28
+ if (!Number.isSafeInteger(total))
29
+ throw new Error('Artifact site size exceeds safe integer accounting');
30
+ if (/\.html?$/i.test(f.path) && f.size > 32 * 1024 * 1024)
31
+ throw new Error('A site HTML page exceeds 32 MiB; move large assets into separate files');
32
+ }
33
+ if (!m.files.some(f => f.path === m.entry))
34
+ throw new Error('Artifact entry page is missing');
35
+ return m;
36
+ }
37
+ export function encodeSiteDescriptor(manifest) {
38
+ if (!isHash(manifest))
39
+ throw new Error('Invalid artifact manifest hash');
40
+ return SITE_PREFIX + JSON.stringify({ manifest }) + SITE_SUFFIX;
41
+ }
42
+ export function decodeSiteDescriptor(content) {
43
+ if (!content.startsWith('<!--DREAMLAKE-ARTIFACT-FILES/'))
44
+ return null;
45
+ if (!content.startsWith(SITE_PREFIX) || !content.endsWith(SITE_SUFFIX) || content.length > 4096)
46
+ throw new Error('Unsupported artifact site format');
47
+ const value = JSON.parse(content.slice(SITE_PREFIX.length, -SITE_SUFFIX.length));
48
+ if (!isHash(value?.manifest))
49
+ throw new Error('Invalid artifact manifest hash');
50
+ return { manifest: value.manifest };
51
+ }
52
+ export const siteManifestKey = (hash) => `artifact-files/v1/manifests/${hash}.json`;
53
+ export const siteFileKey = (hash) => `artifact-files/v1/objects/${hash}`;
@@ -10,6 +10,12 @@ export interface MaterializedConfig {
10
10
  remote_url?: string;
11
11
  /** Cached namespace slug for the materialized (remote, token) pair. */
12
12
  namespace?: string;
13
+ /** Local CLI default, bound to the validated connection without storing another bearer. */
14
+ default_namespace?: {
15
+ namespace: string;
16
+ remote: string;
17
+ tokenSha256: string;
18
+ };
13
19
  device_secret?: string;
14
20
  [key: string]: unknown;
15
21
  }
@@ -91,6 +91,7 @@ export function projectEnv(env, dir) {
91
91
  updateConfig({
92
92
  remote_url: env.server.replace(/\/+$/, ""),
93
93
  namespace: env.namespace || undefined,
94
+ default_namespace: undefined,
94
95
  }, dir);
95
96
  }
96
97
  /** Remove the connection (logout with no env left). Keeps device_secret. */
@@ -98,5 +99,5 @@ export function clearProjection(dir) {
98
99
  const tokens = readJson(tokensPath(dir));
99
100
  delete tokens[TOKEN_SLOT];
100
101
  writeJson(tokensPath(dir), tokens);
101
- updateConfig({ remote_url: undefined, namespace: undefined }, dir);
102
+ updateConfig({ remote_url: undefined, namespace: undefined, default_namespace: undefined }, dir);
102
103
  }
@@ -1,3 +1,13 @@
1
+ export declare const S3_SINGLE_PUT_MAX_BYTES: number;
2
+ export interface UploadFile {
3
+ path: string;
4
+ size: number;
5
+ sha256: string;
6
+ mtimeMs: number;
7
+ ctimeMs: number;
8
+ dev: number;
9
+ ino: number;
10
+ }
1
11
  /** Result of a conditional PUT, as DreamDB's connector expects it. */
2
12
  export type PutStatus = "created" | "exists" | "casFailed";
3
13
  /**
@@ -25,6 +35,12 @@ export interface DreamdbBackend {
25
35
  status: PutStatus;
26
36
  etag?: string;
27
37
  }>;
38
+ putFile?(key: string, file: UploadFile, opts?: {
39
+ ifNoneMatchStar?: boolean;
40
+ }): Promise<{
41
+ status: PutStatus;
42
+ etag?: string;
43
+ }>;
28
44
  list(prefix: string): Promise<string[]>;
29
45
  }
30
46
  export interface S3Credentials {
@@ -68,6 +84,16 @@ export declare class S3Backend {
68
84
  status: PutStatus;
69
85
  etag?: string;
70
86
  }>;
87
+ /** Stream a pre-hashed file, binding its expected bytes into the S3 signature/checksum. */
88
+ putFile(key: string, file: UploadFile, opts?: {
89
+ ifNoneMatchStar?: boolean;
90
+ }): Promise<{
91
+ status: "exists" | "casFailed";
92
+ etag?: undefined;
93
+ } | {
94
+ status: "created";
95
+ etag: string | undefined;
96
+ }>;
71
97
  /** Keys under `prefix`, relative to the dataset base (ListObjectsV2). */
72
98
  list(prefix: string): Promise<string[]>;
73
99
  }