mx 0.1.124

A Swiss army knife for Claude Code and multi-agent toolkits
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
use anyhow::{Context, Result, bail};

use crate::cli::*;
use crate::state;
use crate::tensor;

/// Handle emotional state tensor commands
pub(crate) fn handle_state(cmd: StateCommands) -> Result<()> {
    use std::io::{self, Read as IoRead};
    use std::path::PathBuf;

    // Helper to load tensor schema by ID or path
    let load_tensor_schema = |schema_arg: Option<String>| -> Result<tensor::TensorSchema> {
        match schema_arg {
            Some(s) if s.contains('/') || s.contains('.') => {
                // Looks like a path
                tensor::TensorSchema::load(&PathBuf::from(s))
            }
            Some(id) => tensor::TensorSchema::load_by_id(&id),
            None => tensor::TensorSchema::load_default(),
        }
    };

    // Helper to load legacy state schema
    let load_legacy_schema = |custom_path: Option<String>| -> Result<state::StateSchema> {
        match custom_path {
            Some(p) => state::load_schema(&PathBuf::from(p)),
            None => state::load_default_schema(),
        }
    };

    match cmd {
        // === NEW TENSOR-BASED COMMANDS ===
        StateCommands::Encode {
            values,
            dimensions,
            file,
            schema,
            guided,
            format,
            runes,
        } => {
            let schema = load_tensor_schema(schema)?;

            let tensor = if guided {
                // Interactive guided mode
                tensor::guided_capture(&schema)?
            } else if let Some(dims_str) = dimensions {
                // Parse named dimensions
                tensor::StateTensor::parse_named_dimensions(&schema, &dims_str)?
            } else if let Some(file_path) = file {
                // Read from file
                let content = std::fs::read_to_string(&file_path)
                    .with_context(|| format!("Failed to read file: {}", file_path))?;

                // Try pipe-separated first, then newline-separated
                let values_str = if content.contains('|') {
                    content.trim().to_string()
                } else {
                    content
                        .lines()
                        .map(|l| l.trim())
                        .filter(|l| !l.is_empty())
                        .collect::<Vec<_>>()
                        .join("|")
                };

                tensor::StateTensor::parse_values(&schema, &values_str)?
            } else if let Some(values_str) = values {
                // Parse from argument
                tensor::StateTensor::parse_values(&schema, &values_str)?
            } else {
                // Default tensor
                tensor::StateTensor::default_from_schema(&schema)
            };

            // Output in requested format
            match format.as_str() {
                "json" => println!("{}", serde_json::to_string_pretty(&tensor)?),
                "human" => {
                    println!("{}", tensor.describe(&schema));
                    if let Some((mood_name, mood, distance)) = tensor.nearest_mood(&schema) {
                        println!("\nNearest mood: {} (distance: {:.3})", mood_name, distance);
                        println!("  {}", mood.description);
                    }
                }
                "bootstrap" => {
                    // Self-documenting bootstrap format
                    println!("{}", tensor.format_bootstrap(&schema)?);
                }
                _ => {
                    // tensor format
                    if runes {
                        println!("{}", tensor.encode_with_runes(&schema));
                    } else {
                        println!("{}", tensor.encode());
                    }
                }
            }
        }

        StateCommands::Decode {
            input,
            schema,
            format,
        } => {
            // Get input from arg or stdin
            let input_str = match input {
                Some(s) => s,
                None => {
                    let mut buf = String::new();
                    io::stdin().read_to_string(&mut buf)?;
                    buf.trim().to_string()
                }
            };

            // Decode the tensor (schema ID is embedded in the string)
            let tensor = tensor::StateTensor::decode(&input_str)?;

            // Load schema (use argument if provided, otherwise use ID from tensor)
            let schema = match schema {
                Some(s) => load_tensor_schema(Some(s))?,
                None => tensor::TensorSchema::load_by_id(&tensor.schema_id)?,
            };

            match format.as_str() {
                "json" => println!("{}", serde_json::to_string_pretty(&tensor)?),
                "tensor" => println!("{}", tensor.encode()),
                "mood" => {
                    if let Some((mood_name, mood, distance)) = tensor.nearest_mood(&schema) {
                        println!("{}", mood_name);
                        println!("  Description: {}", mood.description);
                        println!("  Distance: {:.3}", distance);
                    } else {
                        println!("(unnamed region)");
                    }
                }
                _ => {
                    // human format
                    println!("{}", tensor.describe(&schema));
                    if let Some((mood_name, mood, distance)) = tensor.nearest_mood(&schema) {
                        println!("\nNearest mood: {} (distance: {:.3})", mood_name, distance);
                        println!("  {}", mood.description);
                    }
                }
            }
        }

        StateCommands::Schemas { json } => {
            let schemas = tensor::TensorSchema::list_available()?;

            if json {
                let schema_list: Vec<serde_json::Value> = schemas
                    .iter()
                    .filter_map(|schema_id| {
                        tensor::TensorSchema::load_by_id(schema_id).ok().map(|s| {
                            serde_json::json!({
                                "id": s.id,
                                "name": s.name,
                                "dimensions": s.dimensions.len(),
                                "moods": s.moods.len(),
                            })
                        })
                    })
                    .collect();
                println!("{}", serde_json::to_string_pretty(&schema_list)?);
            } else if schemas.is_empty() {
                println!("No schemas found (checked $MX_HOME/schemas/)");
                println!("\nCreate a schema file (YAML or JSON) to get started.");
            } else {
                println!("Available schemas:\n");
                for schema_id in schemas {
                    match tensor::TensorSchema::load_by_id(&schema_id) {
                        Ok(schema) => {
                            println!(
                                "  {} - {} ({} dimensions, {} moods)",
                                schema.id,
                                schema.name,
                                schema.dimensions.len(),
                                schema.moods.len()
                            );
                        }
                        Err(_) => {
                            println!("  {} - (failed to load)", schema_id);
                        }
                    }
                }
            }
        }

        StateCommands::Moods { schema, mood, json } => {
            let schema = load_tensor_schema(schema)?;

            if let Some(mood_name) = mood {
                // Show specific mood
                match schema.moods.get(&mood_name) {
                    Some(mood_def) => {
                        if json {
                            println!("{}", serde_json::to_string_pretty(&mood_def)?);
                        } else {
                            println!("Mood: {}", mood_name);
                            println!("Description: {}", mood_def.description);
                            println!("Tolerance: {:.2}", mood_def.tolerance);
                            println!("\nTensor values:");
                            for (i, value) in mood_def.tensor.iter().enumerate() {
                                let dim_name = schema
                                    .dimensions
                                    .get(i)
                                    .map(|d| d.name.as_str())
                                    .unwrap_or("?");
                                let weight = mood_def
                                    .weights
                                    .as_ref()
                                    .and_then(|w| w.get(i))
                                    .copied()
                                    .unwrap_or(1.0);
                                println!("  {}: {:.2} (weight: {:.2})", dim_name, value, weight);
                            }
                        }
                    }
                    None => {
                        bail!(
                            "Unknown mood '{}'. Available moods: {}",
                            mood_name,
                            schema.moods.keys().cloned().collect::<Vec<_>>().join(", ")
                        );
                    }
                }
            } else {
                // List all moods
                if json {
                    println!("{}", serde_json::to_string_pretty(&schema.moods)?);
                } else {
                    println!("Moods for schema '{}' ({}):\n", schema.id, schema.name);
                    for (name, mood_def) in &schema.moods {
                        let tensor_str: Vec<String> = mood_def
                            .tensor
                            .iter()
                            .map(|v| format!("{:.2}", v))
                            .collect();
                        println!("  {:12} [{}]", name, tensor_str.join("|"));
                        println!("               {}", mood_def.description);
                    }
                }
            }
        }

        StateCommands::Info { schema, json } => {
            let schema = load_tensor_schema(schema)?;

            if json {
                println!("{}", serde_json::to_string_pretty(&schema)?);
            } else {
                println!("Schema: {} ({})", schema.name, schema.id);
                println!("Version: {}", schema.version);
                println!();
                println!("Dimensions ({}):", schema.dimensions.len());
                for dim in &schema.dimensions {
                    let rune = dim
                        .rune
                        .as_ref()
                        .map(|r| format!(" {}", r))
                        .unwrap_or_default();
                    println!("  {}{}:", dim.name, rune);
                    println!("    Low:  {}", dim.anchors.low);
                    if let Some(mid) = &dim.anchors.mid {
                        println!("    Mid:  {}", mid);
                    }
                    println!("    High: {}", dim.anchors.high);
                    println!("    Default: {:.2}", dim.default);
                }
                println!();
                println!("Moods ({}):", schema.moods.len());
                for (name, mood) in &schema.moods {
                    println!(
                        "  {:12} - {} (tol: {:.2})",
                        name, mood.description, mood.tolerance
                    );
                }
            }
        }

        // === LEGACY COMMANDS (backward compatibility) ===
        StateCommands::LegacyEncode {
            mode,
            interactive,
            format,
            schema,
        } => {
            let schema = load_legacy_schema(schema)?;

            let dynamic_state = if interactive {
                state::DynamicState::interactive_capture(&schema)?
            } else if let Some(mode_name) = mode {
                state::DynamicState::from_mode(&mode_name, &schema)?
            } else {
                state::DynamicState::from_mode("default", &schema)?
            };

            match format.as_str() {
                "json" => println!("{}", serde_json::to_string_pretty(&dynamic_state)?),
                "human" => println!("{}", dynamic_state.describe(&schema)),
                _ => println!("{}", dynamic_state.encode_stele(&schema)),
            }
        }

        StateCommands::Parse {
            file,
            preference,
            format,
            schema,
        } => {
            // Strip leading markdown bold markers (**/__) so "**Wake State:** ..."
            // matches the same as plain "Wake State: ...".  Only used in the predicate;
            // the original line is kept for value extraction.
            fn strip_md_bold(line: &str) -> &str {
                line.trim_start()
                    .trim_start_matches("**")
                    .trim_start_matches("__")
                    .trim_start()
            }

            // Extract the @state:... fragment from a matched line, stripping any
            // leading label ("Wake State:", "**Wake State:**", etc.) and trailing
            // markdown bold close ("**") that pocket inserts around the label.
            fn extract_stele_fragment(line: &str) -> &str {
                // Find the @state token wherever it appears in the line
                if let Some(pos) = line.find("@state") {
                    line[pos..]
                        .trim_end_matches('*')
                        .trim_end_matches('_')
                        .trim()
                } else {
                    line.trim()
                }
            }

            let legacy_schema = load_legacy_schema(schema.clone())?;

            let raw_line = if let Some(pref) = preference {
                pref
            } else {
                let path = file.unwrap_or_else(|| {
                    crate::paths::swap_dir()
                        .join("session-bootstrap.md")
                        .to_string_lossy()
                        .to_string()
                });

                let content = std::fs::read_to_string(&path)
                    .with_context(|| format!("Failed to read file: {}", path))?;

                content
                    .lines()
                    .find(|line| {
                        let stripped = strip_md_bold(line);
                        stripped.starts_with("Wake Preference:")
                            || stripped.starts_with("Wake State:")
                            || stripped.starts_with(&legacy_schema.stele.header)
                    })
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| String::from("default"))
            };

            // Detect tensor format: @state:<namespace>|... (has colon after @state)
            let stele_fragment = extract_stele_fragment(&raw_line);
            let is_tensor_format =
                stele_fragment.starts_with("@state:") && stele_fragment.contains('|');

            if is_tensor_format {
                // Decode via tensor path which handles positional numeric values
                let tensor_schema = load_tensor_schema(schema)?;
                let tensor = tensor::StateTensor::decode(stele_fragment).with_context(|| {
                    format!("Failed to decode tensor stele: {}", stele_fragment)
                })?;

                match format.as_str() {
                    "json" => println!("{}", serde_json::to_string_pretty(&tensor.values)?),
                    "stele" => println!("{}", tensor.encode()),
                    _ => {
                        println!("Parsed: {}", stele_fragment);
                        println!();
                        println!("{}", tensor.describe(&tensor_schema));
                    }
                }
            } else {
                // Legacy path: mode names or rune-prefixed stele
                let dynamic_state =
                    state::parse_wake_preference_dynamic(&raw_line, &legacy_schema)?;

                match format.as_str() {
                    "json" => println!("{}", serde_json::to_string_pretty(&dynamic_state)?),
                    "stele" => println!("{}", dynamic_state.encode_stele(&legacy_schema)),
                    "mode" => {
                        println!("Mode calculation not yet implemented for DynamicState");
                    }
                    _ => {
                        println!("Parsed: {}", raw_line.trim());
                        println!();
                        println!("{}", dynamic_state.describe(&legacy_schema));
                    }
                }
            }
        }
    }

    Ok(())
}