juglans 0.2.16

Compiler and runtime for Juglans Workflow Language
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
610
611
// src/wasm/language.rs — LSP intelligence for WASM (Monaco Editor integration)
//
// Pure computation versions of diagnostics, completions, hover, goto-definition.
// No tower-lsp dependency — returns simple Serialize structs for JS consumption.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::core::graph::NodeType;
use crate::core::parser::GraphParser;
use crate::core::validator::{ValidationSeverity, WorkflowValidator};

// ================================================================
// WASM-native types (replaces tower-lsp types)
// ================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Position {
    pub line: u32,
    pub character: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Range {
    pub start: Position,
    pub end: Position,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Diagnostic {
    pub range: Range,
    pub severity: String,
    pub code: String,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionItem {
    pub label: String,
    pub kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub insert_text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub insert_text_format: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoverResult {
    pub contents: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocationResult {
    pub range: Range,
}

// ================================================================
// Diagnostics
// ================================================================

pub fn compute_diagnostics(content: &str) -> Vec<Diagnostic> {
    let mut diagnostics = Vec::new();

    let graph = match GraphParser::parse(content) {
        Ok(g) => g,
        Err(e) => {
            let (line, col) = extract_pest_position(&e.to_string());
            diagnostics.push(Diagnostic {
                range: Range {
                    start: Position {
                        line,
                        character: col,
                    },
                    end: Position {
                        line,
                        character: col + 1,
                    },
                },
                severity: "error".into(),
                code: "parse".into(),
                message: e.to_string(),
            });
            return diagnostics;
        }
    };

    let result = WorkflowValidator::validate(&graph);

    for issue in result.errors.iter().chain(result.warnings.iter()) {
        let severity = match issue.severity {
            ValidationSeverity::Error => "error",
            ValidationSeverity::Warning => "warning",
        };

        let range = if let Some(ref node_id) = issue.node_id {
            find_node_range(content, node_id)
        } else {
            Range {
                start: Position {
                    line: 0,
                    character: 0,
                },
                end: Position {
                    line: 0,
                    character: 0,
                },
            }
        };

        diagnostics.push(Diagnostic {
            range,
            severity: severity.into(),
            code: issue.code.clone(),
            message: issue.message.clone(),
        });
    }

    diagnostics
}

fn find_node_range(content: &str, node_id: &str) -> Range {
    let def_pattern = format!("[{}]", node_id);
    for (line_num, line) in content.lines().enumerate() {
        if let Some(col) = line.find(&def_pattern) {
            return Range {
                start: Position {
                    line: line_num as u32,
                    character: col as u32,
                },
                end: Position {
                    line: line_num as u32,
                    character: (col + def_pattern.len()) as u32,
                },
            };
        }
    }
    Range {
        start: Position {
            line: 0,
            character: 0,
        },
        end: Position {
            line: 0,
            character: 0,
        },
    }
}

fn extract_pest_position(msg: &str) -> (u32, u32) {
    if let Some(pos) = msg.find(" --> ") {
        let after = &msg[pos + 5..];
        let parts: Vec<&str> = after.splitn(3, ':').collect();
        if parts.len() >= 2 {
            let line = parts[0].trim().parse::<u32>().unwrap_or(1);
            let col = parts[1]
                .split(|c: char| !c.is_ascii_digit())
                .next()
                .and_then(|s| s.parse::<u32>().ok())
                .unwrap_or(1);
            return (line.saturating_sub(1), col.saturating_sub(1));
        }
    }
    (0, 0)
}

// ================================================================
// Completions
// ================================================================

const METADATA_KEYS: &[(&str, &str)] = &[
    ("slug", "Unique identifier"),
    ("name", "Display name"),
    ("version", "Semantic version"),
    ("source", "Source URL or path"),
    ("author", "Author name"),
    ("description", "Workflow description"),
    ("entry", "Entry node ID"),
    ("exit", "Exit node ID(s)"),
    ("libs", "Library imports"),
    ("flows", "Flow imports"),
    ("prompts", "Prompt file patterns"),
    ("agents", "Agent file patterns"),
    ("tools", "Tool file patterns"),
    ("python", "Python module imports"),
    ("is_public", "Public visibility flag"),
];

const BUILTIN_TOOLS: &[(&str, &str)] = &[
    ("chat", "AI chat completion"),
    ("p", "Render prompt template"),
    ("fetch", "HTTP request"),
    ("fetch_url", "Fetch URL content"),
    ("notify", "Send notification"),
    ("print", "Print to output"),
    ("reply", "Send reply message"),
    ("return", "Return value from function"),
    ("timer", "Delay execution"),
    ("serve", "HTTP server entry point"),
    ("response", "HTTP response builder"),
    ("assert", "Test assertion"),
    ("config", "Test configuration"),
    ("read_file", "Read file contents"),
    ("write_file", "Write file contents"),
    ("edit_file", "Edit file contents"),
    ("glob", "File pattern matching"),
    ("grep", "Search file contents"),
    ("bash", "Execute shell command"),
    ("sh", "Execute shell command (alias)"),
    ("execute_workflow", "Run sub-workflow"),
    ("memory_search", "Search memory store"),
    ("history", "Chat history"),
    ("vector_create_space", "Create vector space"),
    ("vector_upsert", "Upsert vectors"),
    ("vector_search", "Search vectors"),
    ("vector_list_spaces", "List vector spaces"),
    ("vector_delete_space", "Delete vector space"),
    ("vector_delete", "Delete vectors by ID"),
    ("feishu_webhook", "Send Feishu webhook"),
    ("db_connect", "Connect to database"),
    ("db_disconnect", "Disconnect database"),
    ("db_query", "Raw SQL query"),
    ("db_exec", "Raw SQL execution"),
    ("db_find", "Find records"),
    ("db_find_one", "Find single record"),
    ("db_create", "Create record"),
    ("db_create_many", "Create multiple records"),
    ("db_upsert", "Upsert record"),
    ("db_update", "Update records"),
    ("db_delete", "Delete records"),
    ("db_count", "Count records"),
    ("db_aggregate", "Aggregate query"),
    ("db_begin", "Begin transaction"),
    ("db_commit", "Commit transaction"),
    ("db_rollback", "Rollback transaction"),
    ("db_create_table", "Create table"),
    ("db_drop_table", "Drop table"),
    ("db_alter_table", "Alter table"),
    ("db_tables", "List tables"),
    ("db_columns", "List table columns"),
];

pub fn completions(source: &str, line: u32, col: u32) -> Vec<CompletionItem> {
    let line_text = source.lines().nth(line as usize).unwrap_or("");
    let col = (col as usize).min(line_text.len());
    let prefix = &line_text[..col];
    let trimmed = prefix.trim_start();

    let mut items = Vec::new();

    // Try to parse for graph info (functions, nodes)
    let graph = GraphParser::parse(source).ok();

    // 1. Metadata keys
    if (trimmed.is_empty() || trimmed.chars().all(|c| c.is_alphanumeric() || c == '_'))
        && !line_text.contains('[')
        && is_metadata_region(source, line)
    {
        for (key, desc) in METADATA_KEYS {
            items.push(CompletionItem {
                label: format!("{}: ", key),
                kind: "property".into(),
                detail: Some(desc.to_string()),
                insert_text: Some(format!("{}: ", key)),
                insert_text_format: None,
            });
        }
    }

    // 2. Tool names — after [node]:
    if trimmed.contains("]:") {
        let after_colon = trimmed
            .split_once("]:")
            .map(|x| x.1)
            .unwrap_or("")
            .trim_start();
        if after_colon.is_empty() || (!after_colon.contains('(') && !after_colon.contains("->")) {
            for (tool, desc) in BUILTIN_TOOLS {
                items.push(CompletionItem {
                    label: tool.to_string(),
                    kind: "function".into(),
                    detail: Some(desc.to_string()),
                    insert_text: Some(format!("{}($1)", tool)),
                    insert_text_format: Some("snippet".into()),
                });
            }
            if let Some(ref g) = graph {
                for fname in g.functions.keys() {
                    items.push(CompletionItem {
                        label: fname.clone(),
                        kind: "function".into(),
                        detail: Some("User function".into()),
                        insert_text: None,
                        insert_text_format: None,
                    });
                }
            }
        }
    }

    // 3. Node references
    if prefix.ends_with("-> [") || (trimmed.starts_with('[') && !trimmed.contains("]:")) {
        if let Some(ref g) = graph {
            for node_id in g.node_map.keys() {
                items.push(CompletionItem {
                    label: node_id.clone(),
                    kind: "reference".into(),
                    detail: Some("Node".into()),
                    insert_text: None,
                    insert_text_format: None,
                });
            }
        }
    }

    // 4. Variables
    if prefix.contains('$') {
        let base_vars = [
            ("$input", "Input data"),
            ("$output", "Previous node output"),
            ("$ctx", "Workflow context"),
            ("$reply", "Agent reply metadata"),
            ("$error", "Error information"),
        ];
        for (var, desc) in base_vars {
            items.push(CompletionItem {
                label: var.to_string(),
                kind: "variable".into(),
                detail: Some(desc.to_string()),
                insert_text: None,
                insert_text_format: None,
            });
        }
    }

    // 5. Keywords + snippets
    if trimmed.is_empty() {
        let keywords = [
            ("foreach", "Iterate over collection"),
            ("while", "Loop with condition"),
            ("switch", "Multi-branch routing"),
            ("class", "Class definition"),
            ("struct", "Struct definition"),
        ];
        for (kw, desc) in keywords {
            items.push(CompletionItem {
                label: kw.to_string(),
                kind: "keyword".into(),
                detail: Some(desc.to_string()),
                insert_text: None,
                insert_text_format: None,
            });
        }
        items.push(CompletionItem {
            label: "node".into(),
            kind: "snippet".into(),
            detail: Some("Node definition".into()),
            insert_text: Some("[${1:name}]: ${2:tool}(${3:params})".into()),
            insert_text_format: Some("snippet".into()),
        });
        items.push(CompletionItem {
            label: "edge".into(),
            kind: "snippet".into(),
            detail: Some("Edge definition".into()),
            insert_text: Some("[${1:from}] -> [${2:to}]".into()),
            insert_text_format: Some("snippet".into()),
        });
    }

    items
}

fn is_metadata_region(content: &str, line: u32) -> bool {
    for (i, l) in content.lines().enumerate() {
        if i >= line as usize {
            return true;
        }
        let t = l.trim();
        if t.starts_with('[') && t.contains("]:") {
            return false;
        }
    }
    true
}

// ================================================================
// Hover
// ================================================================

pub fn hover(source: &str, line: u32, col: u32) -> Option<HoverResult> {
    let line_text = source.lines().nth(line as usize)?;
    let col = col as usize;
    let graph = GraphParser::parse(source).ok();

    // Hover on [node_id]
    if let Some(node_id) = extract_bracket_id(line_text, col) {
        if let Some(ref g) = graph {
            if let Some(idx) = g.node_map.get(&node_id) {
                let node = &g.graph[*idx];
                let type_str = match &node.node_type {
                    NodeType::Task(action) => format!("**Task**: `{}`", action.name),
                    NodeType::Foreach { .. } => "**Foreach** loop".into(),
                    NodeType::Loop { .. } => "**While** loop".into(),
                    NodeType::Literal(v) => format!("**Literal**: `{}`", v),
                    _ => "Node".into(),
                };
                return Some(HoverResult {
                    contents: format!("### [{}]\n{}", node_id, type_str),
                });
            }
            if let Some(func) = g.functions.get(&node_id) {
                let params = func.params.join(", ");
                return Some(HoverResult {
                    contents: format!("### Function `{}`\nParams: `({})`", node_id, params),
                });
            }
        }
    }

    // Hover on $variable
    if let Some(var) = extract_variable(line_text, col) {
        let desc = match var.as_str() {
            "$input" => "Input data passed to the workflow",
            "$output" => "Output from the previous node",
            "$ctx" => "Workflow context variables",
            "$reply" => "Agent reply metadata (output, status)",
            "$error" => "Error information from on_error edge",
            _ => "Variable",
        };
        return Some(HoverResult {
            contents: format!("`{}`\n\n{}", var, desc),
        });
    }

    // Hover on tool name
    if let Some(tool_name) = extract_tool_name(line_text, col) {
        if let Some(d) = tool_description(&tool_name) {
            return Some(HoverResult {
                contents: format!("### `{}`\n{}", tool_name, d),
            });
        }
    }

    None
}

// ================================================================
// Go-to-Definition
// ================================================================

pub fn goto_definition(source: &str, line: u32, col: u32) -> Option<LocationResult> {
    let line_text = source.lines().nth(line as usize)?;
    let col = col as usize;
    let target = extract_bracket_id(line_text, col)?;

    for (line_num, src_line) in source.lines().enumerate() {
        let trimmed = src_line.trim();
        // Match [target]: ... (node definition)
        let def_pat = format!("[{}]:", target);
        if let Some(c) = trimmed.find(&def_pat) {
            let abs_col = src_line.find(&def_pat).unwrap_or(c);
            return Some(LocationResult {
                range: Range {
                    start: Position {
                        line: line_num as u32,
                        character: abs_col as u32,
                    },
                    end: Position {
                        line: line_num as u32,
                        character: (abs_col + def_pat.len()) as u32,
                    },
                },
            });
        }
        // Match function def: [target(params)]:
        let fn_prefix = format!("[{}(", target);
        if let Some(c) = trimmed.find(&fn_prefix) {
            let abs_col = src_line.find(&fn_prefix).unwrap_or(c);
            return Some(LocationResult {
                range: Range {
                    start: Position {
                        line: line_num as u32,
                        character: abs_col as u32,
                    },
                    end: Position {
                        line: line_num as u32,
                        character: (abs_col + fn_prefix.len()) as u32,
                    },
                },
            });
        }
    }

    None
}

// ================================================================
// Pure text helpers (ported from lsp/navigation.rs)
// ================================================================

fn extract_bracket_id(line: &str, col: usize) -> Option<String> {
    let bytes = line.as_bytes();
    if col >= bytes.len() {
        return None;
    }
    let mut start = col;
    while start > 0 {
        if bytes[start - 1] == b'[' {
            break;
        }
        if bytes[start - 1] == b']' {
            return None;
        }
        start -= 1;
    }
    if start == 0 && bytes.first() != Some(&b'[') {
        if bytes[0] != b'[' {
            return None;
        }
    }
    let mut end = col;
    while end < bytes.len() {
        if bytes[end] == b']' {
            break;
        }
        if bytes[end] == b'[' && end != start.saturating_sub(1) {
            return None;
        }
        end += 1;
    }
    if end >= bytes.len() {
        return None;
    }
    let content = &line[start..end];
    let id = content.split('(').next().unwrap_or(content).trim();
    if id.is_empty() {
        return None;
    }
    Some(id.to_string())
}

fn extract_variable(line: &str, col: usize) -> Option<String> {
    let bytes = line.as_bytes();
    if col >= bytes.len() {
        return None;
    }
    let mut start = col;
    while start > 0 {
        if bytes[start - 1] == b'$' {
            start -= 1;
            break;
        }
        if !bytes[start - 1].is_ascii_alphanumeric()
            && bytes[start - 1] != b'_'
            && bytes[start - 1] != b'.'
        {
            return None;
        }
        start -= 1;
    }
    if bytes[start] != b'$' {
        return None;
    }
    let mut end = col + 1;
    while end < bytes.len()
        && (bytes[end].is_ascii_alphanumeric() || bytes[end] == b'_' || bytes[end] == b'.')
    {
        end += 1;
    }
    let var = &line[start..end];
    let base = var.split('.').next().unwrap_or(var);
    Some(base.to_string())
}

fn extract_tool_name(line: &str, col: usize) -> Option<String> {
    let trimmed = line.trim();
    let after = if let Some(idx) = trimmed.find("]:") {
        trimmed[idx + 2..].trim_start()
    } else {
        return None;
    };
    if let Some(paren) = after.find('(') {
        let name = after[..paren].trim();
        if !name.is_empty() && col >= line.find(name).unwrap_or(usize::MAX) {
            return Some(name.to_string());
        }
    }
    None
}

fn tool_description(name: &str) -> Option<&'static str> {
    match name {
        "chat" => Some("AI chat completion.\n\nParams: `model`, `prompt`, `system`, `state`, `agent`, `tools`, `temperature`"),
        "p" => Some("Render a prompt template.\n\nParams: `prompt` (slug or path)"),
        "fetch" => Some("HTTP request.\n\nParams: `url`, `method`, `headers`, `body`"),
        "notify" => Some("Send notification.\n\nParams: `message`"),
        "print" => Some("Print value to output.\n\nParams: (value expression)"),
        "reply" => Some("Send reply to client.\n\nParams: `message`, `type`"),
        "set" => Some("Set a context variable.\n\nParams: `key`, `value`"),
        "serve" => Some("Mark node as HTTP entry point.\n\nParams: `method`, `path`"),
        "response" => Some("Build HTTP response.\n\nParams: `status`, `body`, `headers`"),
        "assert" => Some("Test assertion.\n\nParams: `contains`, `eq`, `true`"),
        "bash" | "sh" => Some("Execute shell command.\n\nParams: `command`"),
        "read_file" => Some("Read file contents.\n\nParams: `path`"),
        "write_file" => Some("Write file contents.\n\nParams: `path`, `content`"),
        _ if name.starts_with("db_") => Some("Database ORM operation"),
        _ if name.starts_with("vector_") => Some("Vector store operation"),
        _ => None,
    }
}