distributed_cli 4.0.0

The `distributed` CLI for Distributed applications: contracts check/accept, scaffold projects, describe manifests, compile clients, and render schema artifacts. Also a library so other CLIs (e.g. hops) can mount its commands.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use std::collections::{BTreeMap, BTreeSet};

use serde::Serialize;

use super::super::graphql::CompiledOperation;
use super::super::manifest::{canonical_json_value, ClientManifest};
use super::super::{
    ClientCompileError, GeneratedClientFile, GeneratedClientProject, GeneratedOperationSummary,
    GeneratedRoutePlan,
};
use super::commands::render_commands;
use super::common::json_string;
use super::operation::render_operation_module;

pub(crate) fn render_project(
    manifest: &ClientManifest,
    operations: Vec<CompiledOperation>,
) -> Result<GeneratedClientProject, ClientCompileError> {
    let mut files = Vec::new();
    let mut summaries = Vec::with_capacity(operations.len());
    let mut routes = Vec::new();

    for operation in &operations {
        files.push(GeneratedClientFile {
            path: operation.module_path.clone(),
            contents: render_operation_module(operation, manifest)?,
        });
        summaries.push(GeneratedOperationSummary {
            name: operation.name.clone(),
            source_path: operation.source_path.clone(),
            module_path: operation.module_path.clone(),
            export_name: operation.export_name.clone(),
            operation_hash: operation.query_hash.clone(),
            live_operation_hash: operation.live.as_ref().map(|live| live.hash.clone()),
        });
        if let Some(route) = &operation.route {
            routes.push(route.clone());
        }
    }
    routes.sort_by(|left, right| {
        left.route
            .cmp(&right.route)
            .then_with(|| left.operation.cmp(&right.operation))
    });
    files.push(GeneratedClientFile {
        path: "commands.ts".into(),
        contents: render_commands(manifest)?,
    });
    let pures = super::commands::render_pures(manifest)?;
    let has_pures = pures.is_some();
    if let Some(pures) = pures {
        files.push(GeneratedClientFile {
            path: "pures.ts".into(),
            contents: pures,
        });
    }
    files.push(GeneratedClientFile {
        path: "protocol.ts".into(),
        contents: render_protocol(manifest)?,
    });
    files.push(GeneratedClientFile {
        path: "routes.ts".into(),
        contents: render_routes(&routes, &operations)?,
    });
    files.push(GeneratedClientFile {
        path: "sveltekit.ts".into(),
        contents: render_sveltekit(manifest, &operations)?,
    });
    files.push(GeneratedClientFile {
        path: "index.ts".into(),
        contents: render_index(&operations, has_pures),
    });
    files.push(GeneratedClientFile {
        path: "manifest.json".into(),
        contents: render_compiler_manifest(manifest, &summaries, &routes)?,
    });
    files.sort_by(|left, right| left.path.cmp(&right.path));

    Ok(GeneratedClientProject {
        files,
        operations: summaries,
        routes,
        schema_fingerprint: manifest.schema_fingerprint.clone(),
        protocol_fingerprint: manifest.protocol_fingerprint.clone(),
    })
}
fn render_protocol(manifest: &ClientManifest) -> Result<String, ClientCompileError> {
    let operations =
        serde_json::to_string_pretty(&manifest.protocol_operations).map_err(|error| {
            ClientCompileError::manifest(
                "client.render.protocol",
                format!("failed to render protocol artifacts: {error}"),
            )
        })?;
    let trusted_presets =
        serde_json::to_string_pretty(&manifest.trusted_presets).map_err(|error| {
            ClientCompileError::manifest(
                "client.render.trusted_presets",
                format!("failed to render trusted-preset inventory: {error}"),
            )
        })?;
    let mut sections =
        vec!["/** GENERATED by distributed client. Exact framework-owned operation bytes. */".to_string()];
    if manifest.protocol_operations.command_status.is_some() {
        sections.push(
            "import type { ReplicaCommandStatusArtifact } from '@hops-ops/distributed/replica';"
                .into(),
        );
    }
    sections.push(format!(
        "export const CLIENT_PROTOCOL = {{\n\
         \tversion: 1,\n\
         \tserviceId: {},\n\
         \tschemaHash: {},\n\
         \tprotocolHash: {},\n\
         \tsurface: {},\n\
         \ttrustedPresets: {trusted_presets},\n\
         \toperations: {operations}\n\
         }} as const;",
        json_string(&manifest.service_id)?,
        json_string(&manifest.schema_fingerprint)?,
        json_string(&manifest.protocol_fingerprint)?,
        serde_json::to_string(&manifest.surface).map_err(|error| {
            ClientCompileError::manifest(
                "client.render.protocol",
                format!("failed to render client surface selector: {error}"),
            )
        })?,
    ));
    if let Some(status) = &manifest.protocol_operations.command_status {
        let artifact = canonical_json_value(serde_json::json!({
            "name": &status.name,
            "document": &status.operation,
            "operationHash": &status.operation_hash,
            "protocol": {
                "version": 1,
                "schemaHash": &manifest.schema_fingerprint,
                "protocolHash": &manifest.protocol_fingerprint,
                "surface": &manifest.surface,
                "operation": &status.operation_hash,
                "trustedPresets": &manifest.trusted_presets,
            }
        }));
        let artifact = serde_json::to_string_pretty(&artifact).map_err(|error| {
            ClientCompileError::manifest(
                "client.render.command_status",
                format!("failed to render command-status artifact: {error}"),
            )
        })?;
        sections.push(format!(
            "/** Exact compiler-owned operation used to recover ambiguous command outcomes. */\n\
             export const COMMAND_STATUS: ReplicaCommandStatusArtifact = {artifact};"
        ));
    }
    Ok(format!("{}\n", sections.join("\n\n")))
}

#[derive(Serialize)]
struct CompilerManifest<'a> {
    compiler_manifest_version: u32,
    distributed_manifest_version: u32,
    protocol_version: u32,
    service_id: &'a str,
    surface: &'a super::super::manifest::ManifestSurface,
    schema_fingerprint: &'a str,
    protocol_fingerprint: &'a str,
    scalar_codecs: &'a BTreeMap<String, String>,
    commands_requiring_revalidation: &'a BTreeSet<String>,
    operations: &'a [GeneratedOperationSummary],
    routes: &'a [GeneratedRoutePlan],
}

fn render_compiler_manifest(
    manifest: &ClientManifest,
    operations: &[GeneratedOperationSummary],
    routes: &[GeneratedRoutePlan],
) -> Result<String, ClientCompileError> {
    let provenance = CompilerManifest {
        compiler_manifest_version: 1,
        distributed_manifest_version: 2,
        protocol_version: 1,
        service_id: &manifest.service_id,
        surface: &manifest.surface,
        schema_fingerprint: &manifest.schema_fingerprint,
        protocol_fingerprint: &manifest.protocol_fingerprint,
        scalar_codecs: &manifest.scalar_codecs,
        commands_requiring_revalidation: &manifest.commands_requiring_revalidation,
        operations,
        routes,
    };
    serde_json::to_string_pretty(&provenance)
        .map(|rendered| format!("{rendered}\n"))
        .map_err(|error| {
            ClientCompileError::manifest(
                "client.render.manifest",
                format!("failed to render compiler provenance manifest: {error}"),
            )
        })
}

fn render_routes(
    routes: &[GeneratedRoutePlan],
    operations: &[CompiledOperation],
) -> Result<String, ClientCompileError> {
    let routes_json = serde_json::to_string_pretty(routes).map_err(|error| {
        ClientCompileError::manifest(
            "client.render.routes",
            format!("failed to render route plan: {error}"),
        )
    })?;
    let mut imports = Vec::new();
    let mut bindings = Vec::new();
    for (index, route) in routes.iter().enumerate() {
        let operation = operations
            .iter()
            .find(|operation| operation.name == route.operation)
            .ok_or_else(|| {
                ClientCompileError::manifest(
                    "client.render.routes",
                    format!(
                        "route `{}` references missing operation `{}`",
                        route.route, route.operation
                    ),
                )
            })?;
        let module = operation
            .module_path
            .strip_suffix(".ts")
            .expect("compiler module paths end in .ts");
        imports.push(format!(
            "import {{ {} }} from './{module}.js';",
            operation.export_name
        ));
        bindings.push(format!(
            "  {{ plan: DISTRIBUTED_ROUTES[{index}], artifact: {} }}",
            operation.export_name
        ));
    }
    let import_section = if imports.is_empty() {
        String::new()
    } else {
        format!("{}\n\n", imports.join("\n"))
    };
    let bindings = if bindings.is_empty() {
        "[]".to_string()
    } else {
        format!("[\n{}\n]", bindings.join(",\n"))
    };
    Ok(format!(
        "{import_section}\
         /** GENERATED framework-neutral `@load` ownership plan. */\n\
         export const DISTRIBUTED_ROUTES = {routes_json} as const;\n\
         \n\
         /** Static route-to-artifact bindings consumed by framework SSR adapters. */\n\
         export const DISTRIBUTED_ROUTE_OPERATIONS = {bindings} as const;\n\
         \n\
         export type DistributedRoutePlan = (typeof DISTRIBUTED_ROUTES)[number];\n\
         export type DistributedRouteOperation = (typeof DISTRIBUTED_ROUTE_OPERATIONS)[number];\n"
    ))
}

fn render_index(operations: &[CompiledOperation], has_pures: bool) -> String {
    let mut lines = vec![
        "/** GENERATED public entrypoint. */".to_string(),
        "export * from './commands.js';".into(),
        "export * from './protocol.js';".into(),
        "export * from './routes.js';".into(),
    ];
    if has_pures {
        lines.push("export * from './pures.js';".into());
    }
    for operation in operations {
        let module = operation
            .module_path
            .strip_suffix(".ts")
            .expect("compiler module paths end in .ts");
        lines.push(format!("export * from './{module}.js';"));
    }
    format!("{}\n", lines.join("\n"))
}

fn render_sveltekit(
    manifest: &ClientManifest,
    operations: &[CompiledOperation],
) -> Result<String, ClientCompileError> {
    if let Some(operation) = operations
        .iter()
        .find(|operation| !typescript_value_binding(&operation.name))
    {
        return Err(ClientCompileError::source(
            "client.operation.sveltekit_identifier",
            format!(
                "operation `{}` cannot be exported as a `$distributed` value because it is reserved in JavaScript/TypeScript; rename the operation",
                operation.name
            ),
            &operation.source_path,
            operation.source_line,
            operation.source_column,
        ));
    }

    let mut value_exports = BTreeSet::from([
        "COMMAND_ARTIFACTS".to_string(),
        "COMMANDS".to_string(),
        "DISTRIBUTED_ROUTES".to_string(),
        "DISTRIBUTED_ROUTE_OPERATIONS".to_string(),
        "PROJECTOR_ARTIFACTS".to_string(),
        "provideDistributed".to_string(),
        "useCommands".to_string(),
    ]);
    if !manifest.commands.is_empty() {
        value_exports.insert("createCommands".into());
    }
    for command in &manifest.commands {
        value_exports.insert(format!("Command_{}", command.mutation_field));
        value_exports.insert(format!("prepareCommand_{}", command.mutation_field));
    }
    for operation in operations {
        value_exports.insert(operation.export_name.clone());
        value_exports.insert(format!("{}Document", operation.export_name));
    }
    if let Some(operation) = operations
        .iter()
        .find(|operation| value_exports.contains(&operation.name))
    {
        return Err(ClientCompileError::source(
            "client.operation.sveltekit_export_collision",
            format!(
                "operation `{}` collides with the generated `$distributed` export namespace; rename the operation",
                operation.name
            ),
            &operation.source_path,
            operation.source_line,
            operation.source_column,
        ));
    }

    let mut sections = vec![
        "/** GENERATED by distributed client. Do not edit. */".to_string(),
        [
            "import {",
            "  createDistributedSvelteKit,",
            "  defineDistributedSvelteKitOperation,",
            "  provideDistributedSvelteKitClient,",
            "  useDistributedSvelteKitCommands",
            "} from '@hops-ops/distributed/sveltekit';",
            "",
            "import type {",
            "  CreateDistributedSvelteKitOptions,",
            "  DistributedSvelteKitClient",
            "} from '@hops-ops/distributed/sveltekit';",
        ]
        .join("\n"),
    ];

    if manifest.commands.is_empty() {
        sections
            .push("export type GeneratedCommands = Readonly<Record<never, never>>;".to_string());
    } else {
        sections.push(
            [
                "import {",
                "  createCommands as createGeneratedCommands,",
                "  type GeneratedCommands",
                "} from './commands.js';",
                "",
                "export type { GeneratedCommands } from './commands.js';",
            ]
            .join("\n"),
        );
    }

    for (index, operation) in operations.iter().enumerate() {
        let module = operation
            .module_path
            .strip_suffix(".ts")
            .expect("compiler module paths end in .ts");
        sections.push(format!(
            "import {{ {} as DistributedOperation_{index} }} from './{module}.js';",
            operation.export_name
        ));
    }

    sections.push(
        [
            "/** Inspectable framework-neutral artifacts remain available here. */",
            "export * from './index.js';",
        ]
        .join("\n"),
    );

    for (index, operation) in operations.iter().enumerate() {
        sections.push(format!(
            "/** Tree-local Svelte binding for the generated `{}` artifact. */\nexport const {} = defineDistributedSvelteKitOperation(DistributedOperation_{index});",
            operation.name, operation.name
        ));
    }

    let mut bindings = vec![
        "/**".to_string(),
        " * Create and install one component-tree/request-local generated client.".to_string(),
        " * No client or command proxy is retained by this module.".to_string(),
        " */".to_string(),
        "export function provideDistributed(".to_string(),
        "  options: Omit<CreateDistributedSvelteKitOptions<GeneratedCommands>, 'createCommands'>"
            .to_string(),
        "): DistributedSvelteKitClient<GeneratedCommands> {".to_string(),
        "  return provideDistributedSvelteKitClient(".to_string(),
        "    createDistributedSvelteKit<GeneratedCommands>({".to_string(),
    ];
    if manifest.commands.is_empty() {
        bindings.push("      ...options".to_string());
    } else {
        bindings.extend([
            "      ...options,".to_string(),
            "      createCommands: createGeneratedCommands".to_string(),
        ]);
    }
    bindings.extend(
        [
            "    })",
            "  );",
            "}",
            "",
            "/** Resolve the nearest generated command surface during component initialization. */",
            "export function useCommands(): GeneratedCommands {",
            "  return useDistributedSvelteKitCommands<GeneratedCommands>();",
            "}",
        ]
        .into_iter()
        .map(str::to_string),
    );
    sections.push(bindings.join("\n"));

    Ok(format!("{}\n", sections.join("\n\n")))
}

fn typescript_value_binding(name: &str) -> bool {
    // GraphQL already guarantees the identifier grammar. This list closes the
    // remaining JavaScript strict-mode and TypeScript keyword/contextual traps.
    !matches!(
        name,
        "abstract"
            | "any"
            | "arguments"
            | "as"
            | "asserts"
            | "async"
            | "await"
            | "bigint"
            | "boolean"
            | "break"
            | "case"
            | "catch"
            | "class"
            | "const"
            | "constructor"
            | "continue"
            | "debugger"
            | "declare"
            | "default"
            | "delete"
            | "do"
            | "else"
            | "enum"
            | "eval"
            | "export"
            | "extends"
            | "false"
            | "finally"
            | "for"
            | "from"
            | "function"
            | "get"
            | "global"
            | "if"
            | "implements"
            | "import"
            | "in"
            | "infer"
            | "instanceof"
            | "interface"
            | "intrinsic"
            | "is"
            | "keyof"
            | "let"
            | "module"
            | "namespace"
            | "never"
            | "new"
            | "null"
            | "number"
            | "object"
            | "of"
            | "out"
            | "override"
            | "package"
            | "private"
            | "protected"
            | "public"
            | "readonly"
            | "require"
            | "return"
            | "satisfies"
            | "set"
            | "static"
            | "string"
            | "super"
            | "switch"
            | "symbol"
            | "this"
            | "throw"
            | "true"
            | "try"
            | "type"
            | "typeof"
            | "undefined"
            | "unique"
            | "unknown"
            | "using"
            | "var"
            | "void"
            | "while"
            | "with"
            | "yield"
    )
}