plexus-macros 0.5.2

Procedural macros for Plexus RPC activations
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# Complete Pipeline: Rust → JSON Schema → Synapse IR → Client Libraries

Understanding the full type extraction and code generation pipeline.

---

## The Complete Flow

```
Rust Backend (#[hub_methods])
  ↓ [schemars::schema_for!]
JSON Schema (PluginSchema + MethodSchema)
  ↓ [WebSocket RPC fetch via synapse]
Synapse IR (deduplicated, compiler-ready)
  ↓ [hub-codegen Rust binary]
Generated Code (TypeScript/Rust)
  ↓ [synapse-cc orchestrator]
Client Libraries (type-safe, deployed)
```

---

## Stage 1: Rust Macros → JSON Schema

### Input: Rust Code with Macros

```rust
use plexus::prelude::*;

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(tag = "event", rename_all = "snake_case")]
enum BashEvent {
    Stdout { data: String },
    Stderr { data: String },
    ExitCode { code: i32 },
}

#[hub_methods(namespace = "bash", version = "1.0.0")]
impl BashActivation {
    /// Execute a bash command
    #[hub_method(streaming)]
    async fn execute(&self, command: String, timeout: u64)
        -> impl Stream<Item = BashEvent>
    {
        // implementation
    }
}
```

### Output: JSON Schema (in PluginSchema)

```json
{
  "namespace": "bash",
  "version": "1.0.0",
  "methods": [{
    "name": "execute",
    "description": "Execute a bash command",
    "params": {
      "type": "object",
      "required": ["command", "timeout"],
      "properties": {
        "command": { "type": "string" },
        "timeout": { "type": "integer", "format": "uint64" }
      },
      "$defs": {}
    },
    "returns": {
      "oneOf": [
        {
          "type": "object",
          "required": ["event", "data"],
          "properties": {
            "event": { "const": "stdout" },
            "data": { "type": "string" }
          }
        },
        {
          "type": "object",
          "required": ["event", "data"],
          "properties": {
            "event": { "const": "stderr" },
            "data": { "type": "string" }
          }
        },
        {
          "type": "object",
          "required": ["event", "code"],
          "properties": {
            "event": { "const": "exit_code" },
            "code": { "type": "integer", "format": "int32" }
          }
        }
      ],
      "$defs": {
        "BashEvent": { /* full enum schema */ }
      }
    },
    "streaming": true,
    "bidirectional": false,
    "http_method": "Post"
  }]
}
```

**What gets preserved:**
- ✅ Field names and types
- ✅ Required vs optional fields
- ✅ Type references via `$ref`
- ✅ Format hints (uint64, int32, uuid, date-time)
- ✅ Enum discriminators (tag field)
- ✅ Enum variants with fields
- ✅ Streaming flag
- ✅ Descriptions from doc comments

**What gets lost:**
- ❌ Rust-specific types (Cow, Arc, Box)
- ❌ Lifetime annotations
- ❌ Impl trait details
- ❌ Generic type parameters (resolved to concrete types)

---

## Stage 2: JSON Schema → Synapse IR

### Input: PluginSchema (from WebSocket RPC)

Synapse connects to the live backend and calls:
```
ws://localhost:4444/rpc
→ bash.schema → PluginSchema { ... }
→ (recursively walk children)
```

### Conversion Logic (Haskell)

```haskell
-- synapse/src/Synapse/IR/Builder.hs

schemaToTypeRef :: Text -> Value -> TypeRef
schemaToTypeRef namespace schema = case schema of
  Object o -> case (lookupType o, lookupRef o, lookupArray o, lookupNullable o) of
    -- Named type reference
    (_, Just ref, _, _) -> RefNamed (extractQualifiedName namespace ref)

    -- Array type
    (_, _, Just items, _) -> RefArray (schemaToTypeRef namespace items)

    -- Nullable type (anyOf with null)
    (_, _, _, Just innerSchema) -> RefOptional (schemaToTypeRef namespace innerSchema)

    -- Primitive type with format
    (Just primType, _, _, _) -> RefPrimitive primType (lookupFormat o)

  -- Intentionally dynamic (schema: true)
  Bool True -> RefAny

  -- Schema gap/error
  _ -> RefUnknown

-- Extract enum definition
extractEnumDef :: Value -> TypeDef
extractEnumDef schema = case findOneOf schema of
  Just variants -> TypeDef
    { tdKind = KindEnum
        { keDiscriminator = findDiscriminator schema  -- e.g., "event"
        , keVariants = map extractVariant variants
        }
    }
```

### Output: Synapse IR (JSON)

```json
{
  "irVersion": "2.0",
  "irBackend": "substrate",
  "irHash": "a1b2c3d4e5f6a7b8",
  "irTypes": {
    "bash.BashEvent": {
      "tdName": "BashEvent",
      "tdNamespace": "bash",
      "tdKind": {
        "tag": "KindEnum",
        "keDiscriminator": "event",
        "keVariants": [
          {
            "vdName": "stdout",
            "vdFields": [
              {
                "fdName": "data",
                "fdType": { "tag": "RefPrimitive", "contents": ["string", null] },
                "fdRequired": true
              }
            ]
          },
          {
            "vdName": "stderr",
            "vdFields": [
              {
                "fdName": "data",
                "fdType": { "tag": "RefPrimitive", "contents": ["string", null] },
                "fdRequired": true
              }
            ]
          },
          {
            "vdName": "exit_code",
            "vdFields": [
              {
                "fdName": "code",
                "fdType": { "tag": "RefPrimitive", "contents": ["integer", "int32"] },
                "fdRequired": true
              }
            ]
          }
        ]
      }
    }
  },
  "irMethods": {
    "bash.execute": {
      "mdName": "execute",
      "mdFullPath": "bash.execute",
      "mdNamespace": "bash",
      "mdStreaming": true,
      "mdParams": [
        {
          "pdName": "command",
          "pdType": { "tag": "RefPrimitive", "contents": ["string", null] },
          "pdRequired": true
        },
        {
          "pdName": "timeout",
          "pdType": { "tag": "RefPrimitive", "contents": ["integer", "uint64"] },
          "pdRequired": true
        }
      ],
      "mdReturns": { "tag": "RefNamed", "contents": ["bash", "BashEvent"] },
      "mdBidirType": null
    }
  },
  "irPlugins": {
    "bash": ["execute"]
  }
}
```

**What gets preserved:**
- ✅ Enum discriminators explicit ("event" field)
- ✅ Variant names and fields
- ✅ Format hints (int32, uint64, uuid, etc.)
- ✅ Qualified type references (namespace.TypeName)
- ✅ Optional vs required fields
- ✅ Streaming flag
- ✅ Bidirectional channel types (if present)

**What gets lost:**
- ❌ JSON Schema `$defs` structure (flattened to global irTypes map)
- ❌ Original oneOf encoding (normalized to discriminated enum)

**Key improvement:** IR is **deduplicated** and **compiler-ready** (no $ref resolution needed).

---

## Stage 3: Synapse IR → Generated Code (hub-codegen)

### Input: IR JSON

hub-codegen reads the IR and generates language-specific code.

### TypeScript Generation

```rust
// hub-codegen/src/generator/typescript/types.rs

fn generate_enum(enum_def: &EnumDef) -> String {
    let discriminator = &enum_def.keDiscriminator;

    // Generate interface per variant
    let variant_interfaces = variants.iter().map(|variant| {
        let variant_name = to_pascal_case(&variant.vdName);
        let interface_name = format!("{}{}", type_name, variant_name);

        let fields = variant.vdFields.iter().map(|field| {
            format!("  {}: {};", field.fdName, type_ref_to_ts(&field.fdType))
        }).collect::<Vec<_>>().join("\n");

        format!(
            "export interface {} {{\n  {}: '{}';\n{}\n}}",
            interface_name, discriminator, variant.vdName, fields
        )
    }).collect::<Vec<_>>().join("\n\n");

    // Generate union type
    let union_type = format!(
        "export type {} = {};",
        type_name,
        variant_names.join(" | ")
    );

    // Generate type guards
    let type_guards = variants.iter().map(|variant| {
        format!(
            "export function is{}(e: {}): e is {} {{\n  return e.{} === '{}';\n}}",
            variant_name, type_name, interface_name, discriminator, variant.vdName
        )
    }).collect::<Vec<_>>().join("\n\n");

    format!("{}\n\n{}\n\n{}", variant_interfaces, union_type, type_guards)
}
```

### Output: TypeScript Client

```typescript
// bash/types.ts
export interface BashEventStdout {
  event: 'stdout';
  data: string;
}

export interface BashEventStderr {
  event: 'stderr';
  data: string;
}

export interface BashEventExitCode {
  event: 'exit_code';
  code: number;
}

export type BashEvent =
  | BashEventStdout
  | BashEventStderr
  | BashEventExitCode;

export function isBashEventStdout(e: BashEvent): e is BashEventStdout {
  return e.event === 'stdout';
}

export function isBashEventStderr(e: BashEvent): e is BashEventStderr {
  return e.event === 'stderr';
}

export function isBashEventExitCode(e: BashEvent): e is BashEventExitCode {
  return e.event === 'exit_code';
}

// bash/client.ts
export interface BashClient {
  execute(command: string, timeout: number): AsyncGenerator<BashEvent>;
}

export class BashClientImpl implements BashClient {
  constructor(private rpc: RpcClient) {}

  async *execute(command: string, timeout: number): AsyncGenerator<BashEvent> {
    const stream = await this.rpc.call<BashEvent>(
      'bash.execute',
      { command, timeout }
    );

    for await (const item of stream) {
      if (item.type === 'data') {
        yield item.content;
      } else if (item.type === 'error') {
        throw new Error(item.error);
      }
      // handle other PlexusStreamItem types
    }
  }
}
```

**What gets preserved:**
- ✅ Full discriminated union types
- ✅ Type guards for runtime discrimination
- ✅ Streaming → AsyncGenerator
- ✅ Non-streaming → Promise
- ✅ All field types and names
- ✅ Required vs optional (? suffix)

**What gets lost:**
- ❌ Format hints (uint64 → number, no runtime validation)
- ❌ Doc comments (not in IR yet)

---

## Stage 4: synapse-cc Integration

### What synapse-cc Adds

```
hub-codegen output (JSON mode)
  ↓
synapse-cc:
  ├─ Three-way merge (baseline ← current, new)
  ├─ Write files to output directory
  ├─ Detect package manager (bun/npm/yarn/pnpm)
  ├─ Install dependencies (npm add @types/node ws)
  ├─ Write tsconfig.json (if standalone)
  ├─ Run tsc --noEmit (if standalone)
  ├─ Run tests (if standalone && not --no-tests)
  └─ Write cache manifests
```

**Result:** Fully integrated, type-safe client library ready to use.

---

## What Type Information is Critical?

### Must Preserve (Currently Working)

1. **Enum discriminators** - Which field distinguishes variants
2. **Variant names and fields** - Full structure of each enum case
3. **Format hints** - uuid, int32, uint64, date-time
4. **Required vs optional** - Which fields can be missing
5. **Type references** - Cross-namespace imports
6. **Streaming flag** - AsyncGenerator vs Promise
7. **Bidirectional types** - Request/response channel types

### Should Preserve (Partially Working)

1. **Doc comments** - Currently not in IR (❌)
2. **Validation constraints** - min/max/pattern (❌)
3. **Example values** - For docs/tests (❌)
4. **Deprecation** - Warn about old methods (❌)
5. **Default values** - For optional params (❌)

### Could Preserve (Future)

1. **Newtype semantics** - UserId vs PostId distinction
2. **Branded types** - Runtime type tagging
3. **Custom validators** - Client-side validation
4. **Security hints** - Mark sensitive fields (#[sensitive])

---

## Current Pain Points

### 1. **Doc Comments Don't Flow Through**

```rust
/// Execute a bash command  // ← Lost!
#[hub_method]
async fn execute(&self, command: String) -> BashEvent { }
```

**Solution:** Add `description` field to IR MethodDef and TypeDef.

### 2. **No Parameter Descriptions**

```rust
#[hub_method(params(command = "Shell command to execute"))]
```

Currently awkward. With new macro:
```rust
#[method]
async fn execute(
    &self,
    /// Shell command to execute  // ← Should be in IR!
    command: String,
) { }
```

**Solution:** Extract param descriptions into IR ParamDef.

### 3. **Validation Constraints Lost**

```rust
#[derive(Validate, JsonSchema)]
struct CreateUser {
    #[validate(email)]
    email: String,  // ← Validation not in schema/IR!
}
```

**Solution:**
- Add schemars validation attributes
- Synapse extracts to IR
- hub-codegen emits validation in generated code

### 4. **No Example Values**

```rust
#[method]
async fn execute(
    &self,
    #[example("ls -la")] command: String,  // ← Not in IR
) { }
```

**Solution:** Add `examples` field to ParamDef and FieldDef in IR.

---

## Recommendations for Macro v2

### Add to PluginSchema (plexus-core)

```rust
pub struct MethodSchema {
    pub name: String,
    pub description: String,           // Already have ✅
    pub long_description: Option<String>,  // Add ⭐
    pub params: Option<Schema>,
    pub param_descriptions: HashMap<String, String>,  // Add ⭐
    pub param_examples: HashMap<String, Value>,       // Add ⭐
    pub returns: Option<Schema>,
    pub examples: Vec<String>,         // Add ⭐ (JSON examples)
    pub streaming: bool,
    pub http_method: HttpMethod,
    pub deprecated: Option<String>,    // Add ⭐
    pub tags: Vec<String>,             // Add ⭐
}
```

### Add to Synapse IR

```haskell
data ParamDef = ParamDef
  { pdName        :: Text
  , pdType        :: TypeRef
  , pdRequired    :: Bool
  , pdDescription :: Maybe Text      -- Add ⭐
  , pdExample     :: Maybe Value     -- Add ⭐
  , pdDefault     :: Maybe Value     -- Add ⭐
  }

data MethodDef = MethodDef
  { mdName            :: Text
  , mdDescription     :: Maybe Text      -- Add ⭐
  , mdLongDescription :: Maybe Text      -- Add ⭐
  , mdExamples        :: [Text]          -- Add ⭐ (JSON strings)
  , mdDeprecated      :: Maybe Text      -- Add ⭐
  , mdTags            :: [Text]          -- Add ⭐
  , -- ... existing fields
  }

data TypeDef = TypeDef
  { tdDescription :: Maybe Text          -- Add ⭐
  , -- ... existing fields
  }

data FieldDef = FieldDef
  { fdDescription :: Maybe Text          -- Add ⭐
  , fdExample     :: Maybe Value         -- Add ⭐
  , -- ... existing fields
  }
```

### Update hub-codegen Output

```typescript
// With full docs:

/**
 * Execute a bash command and stream output.
 *
 * This runs the command in a bash shell and streams stdout/stderr
 * as it becomes available.
 *
 * @example
 * ```typescript
 * for await (const event of client.bash.execute("ls -la", 30)) {
 *   if (isBashEventStdout(event)) {
 *     console.log(event.data);
 *   }
 * }
 * ```
 *
 * @param command - Shell command to execute
 * @param timeout - Maximum execution time in seconds
 * @returns Stream of bash events
 * @deprecated Use executeV2 instead
 */
execute(command: string, timeout: number): AsyncGenerator<BashEvent>;
```

---

## Summary: Complete Type Flow

```
Rust Source
  → [schemars] →
JSON Schema (with $defs, oneOf, format hints)
  → [synapse schemaToTypeRef] →
Synapse IR (TypeRef, MethodDef, deduplicated)
  → [hub-codegen] →
TypeScript (discriminated unions, type guards, AsyncGenerator)
  → [synapse-cc] →
Client Library (type-safe, documented, validated)
```

**Every stage must preserve:**
1. Type structure (enums, structs, primitives)
2. Discriminators (which field is the tag)
3. Format hints (uuid, int32, etc.)
4. Required vs optional
5. Documentation (descriptions, examples)
6. Validation rules (min, max, pattern)
7. Streaming semantics
8. Deprecation warnings

**The macros are the SOURCE OF TRUTH.** Everything downstream depends on what they expose in PluginSchema!