callwire 2.0.1

High-performance bidirectional RPC over TCP with MessagePack framing — Go, Python, Rust interop
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
/// Callwire CLI — `cargo run --bin callwire -- init`
///
/// Scans the current directory for callwire workers across Go, Rust, Python,
/// and TypeScript, then generates a complete callwire.toml configuration.

use std::fmt::Write;
use std::fs;
use std::io;
use std::path::Path;

const EXCLUDED_DIRS: &[&str] = &[
    "node_modules", ".git", "target", "__pycache__", ".venv",
    "dist", "build", ".callwire",
];

fn should_skip(path: &Path) -> bool {
    for component in path.components() {
        if let Some(name) = component.as_os_str().to_str() {
            if EXCLUDED_DIRS.contains(&name) {
                return true;
            }
        }
    }
    false
}

fn is_sdk_dir(abs: &Path) -> bool {
    let go_mod_root = match find_go_mod_root() {
        Some(r) => r,
        None => return false,
    };
    let go_mod_path = std::path::PathBuf::from(&go_mod_root);
    // Two levels up: go/callwire -> go -> repo root
    let repo_root = match go_mod_path.parent().and_then(|p| p.parent()) {
        Some(r) => r,
        None => return false,
    };
    let sdk_roots = [
        repo_root.join("go").join("callwire"),
        repo_root.join("rust"),
        repo_root.join("ts"),
        repo_root.join("python").join("callwire"),
    ];
    let go_path = repo_root.join("go");
    let py_path = repo_root.join("python");
    for sdk in &sdk_roots {
        let canonical = if let Ok(c) = fs::canonicalize(sdk) { c } else { continue };
        if abs.starts_with(&canonical) && abs != go_path && abs != py_path {
            return true;
        }
    }
    false
}

/// Compute relative path from `base` to `target`, handling `../` when needed.
fn rel_path(base: &Path, target: &Path) -> String {
    let base_abs = if base.is_relative() {
        std::env::current_dir().ok().map(|c| c.join(base)).unwrap_or_else(|| base.to_path_buf())
    } else {
        base.to_path_buf()
    };
    let target_abs = if target.is_relative() {
        std::env::current_dir().ok().map(|c| c.join(target)).unwrap_or_else(|| target.to_path_buf())
    } else {
        target.to_path_buf()
    };
    let base_components: Vec<_> = base_abs.components().collect();
    let target_components: Vec<_> = target_abs.components().collect();
    let mut i = 0;
    while i < base_components.len() && i < target_components.len()
        && base_components[i] == target_components[i]
    {
        i += 1;
    }
    let mut result = std::path::PathBuf::new();
    for _ in i..base_components.len() {
        result.push("..");
    }
    for j in i..target_components.len() {
        result.push(target_components[j]);
    }
    result.to_string_lossy().to_string()
}

fn find_go_mod_root() -> Option<String> {
    let result = None;
    // Search from CWD and parent directories up to 3 levels
    let mut search_root = std::env::current_dir().ok()?;
    for _ in 0..4 {
        let mut files = vec![];
        let _ = walk_dir_at(&search_root, &mut |path: &Path| {
            if path.file_name().and_then(|n| n.to_str()) == Some("go.mod") {
                files.push(path.to_path_buf());
            }
            Ok(())
        });
        for path in &files {
            if should_skip(path) { continue; }
            if let Ok(content) = fs::read_to_string(path) {
                for line in content.lines() {
                    if line.starts_with("module ") && line.contains("callwire") {
                        if let Some(dir) = path.parent() {
                            if let Ok(abs) = fs::canonicalize(dir) {
                                return Some(abs.to_string_lossy().to_string());
                            }
                        }
                    }
                }
            }
        }
        // Try parent directory
        if let Some(parent) = search_root.parent() {
            search_root = parent.to_path_buf();
        } else {
            break;
        }
    }
    result
}

fn walk_dir_at<F>(root: &std::path::Path, f: &mut F) -> io::Result<()>
where
    F: FnMut(&Path) -> io::Result<()>,
{
    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        if should_skip(&dir) {
            continue;
        }
        for entry in fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if entry.file_type()?.is_dir() {
                stack.push(path);
            } else {
                f(&path)?;
            }
        }
    }
    Ok(())
}

fn service_name(stem: &str) -> String {
    let name = stem.replace('_', "-");
    if name.ends_with("-worker") {
        return name;
    }
    format!("{}-worker", name)
}

fn repo_root() -> std::path::PathBuf {
    if let Some(go_mod) = find_go_mod_root() {
        let p = std::path::PathBuf::from(&go_mod);
        if let Some(parent) = p.parent().and_then(|p| p.parent()) {
            return parent.to_path_buf();
        }
    }
    if let Ok(cwd) = std::env::current_dir() {
        return cwd;
    }
    std::path::PathBuf::from(".")
}

fn detect_go_workers() -> Vec<(String, String)> {
    let mut results = vec![];
    let go_mod_root = match find_go_mod_root() {
        Some(r) => r,
        None => return results,
    };
    let root = repo_root();

    let mut files = vec![];
    let _ = walk_dir_at(&root, &mut |path: &Path| {
        if path.extension().and_then(|e| e.to_str()) == Some("go") {
            files.push(path.to_path_buf());
        }
        Ok(())
    });

    for path in files {
        if should_skip(&path) {
            continue;
        }
        if let Ok(abs) = fs::canonicalize(&path) {
            if is_sdk_dir(&abs) { continue; }
        }
        let content = match fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => continue,
        };
        if !content.contains("package main") { continue; }
        if !content.contains("callwire.") { continue; }
        let has_export = content.contains("callwire.Export") || content.contains("callwire.MustExport");
        let has_init = content.contains("callwire.Init") || content.contains("callwire.Serve");
        if !(has_export && has_init) { continue; }

        let abs_path = if let Ok(a) = fs::canonicalize(&path) { a } else { continue };
        let rel = rel_path(std::path::Path::new(&go_mod_root), &abs_path);
        // Skip CLI tool directories
        if rel.starts_with("cmd/") { continue; }
        let fname = path.file_stem().and_then(|s| s.to_str()).unwrap_or("worker");
        let name = if fname == "main" {
            path.parent().and_then(|p| p.file_name()).and_then(|s| s.to_str()).unwrap_or("worker").replace('_', "-")
        } else {
            fname.replace('_', "-")
        };
        let mod_rel = rel_path(&root, std::path::Path::new(&go_mod_root));
        let cmd = format!("cd {} && go run {}", mod_rel, rel);
        results.push((service_name(&name), cmd));
    }
    results
}

fn detect_rust_workers() -> Vec<(String, String)> {
    let mut results = vec![];
    let root = repo_root();

    if let Ok(entries) = fs::read_dir(&root) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() || should_skip(&path) { continue; }
            let cargo_toml = path.join("Cargo.toml");
            if !cargo_toml.exists() { continue; }

            let content = match fs::read_to_string(&cargo_toml) {
                Ok(c) => c,
                Err(_) => continue,
            };
            if !content.contains("callwire") { continue; }

            let is_sdk = content.contains("name = \"callwire\"");
            let cargo_root = path;
            let mod_rel = cargo_root.strip_prefix(&root).unwrap_or(&cargo_root).to_string_lossy().to_string();

            // Check examples/
            let example_dir = cargo_root.join("examples");
            if example_dir.is_dir() {
                if let Ok(entries) = fs::read_dir(&example_dir) {
                    for e in entries.flatten() {
                        let ep = e.path();
                        if ep.extension().and_then(|s| s.to_str()) != Some("rs") { continue; }
                        let ec = match fs::read_to_string(&ep) { Ok(c) => c, Err(_) => continue };
                        if !ec.contains("callwire::") && !ec.contains("use callwire") { continue; }
                        let has_main = ec.contains("fn main") || ec.contains("#[tokio::main]");
                        let has_reg = ec.contains("callwire::register_unary") || ec.contains("callwire::export!");
                        let has_init = ec.contains("callwire::init()");
                        if !(has_main && has_reg && has_init) { continue; }
                        let name = ep.file_stem().and_then(|s| s.to_str()).unwrap_or("worker").replace('_', "-");
                        let cmd = format!("cd {} && cargo run --quiet --example {}", mod_rel, name);
                        results.push((service_name(&name), cmd));
                    }
                }
            }

            // Check src/bin/ (skip SDK's own binary targets)
            if !is_sdk {
                let bin_dir = cargo_root.join("src").join("bin");
                if bin_dir.is_dir() {
                    if let Ok(entries) = fs::read_dir(&bin_dir) {
                        for e in entries.flatten() {
                            let bp = e.path();
                            if bp.extension().and_then(|s| s.to_str()) != Some("rs") { continue; }
                            let bc = match fs::read_to_string(&bp) { Ok(c) => c, Err(_) => continue };
                            if !bc.contains("callwire::") && !bc.contains("use callwire") { continue; }
                            let has_main = bc.contains("fn main") || bc.contains("#[tokio::main]");
                            let has_reg = bc.contains("callwire::register_unary") || bc.contains("callwire::export!");
                            let has_init = bc.contains("callwire::init()");
                            if !(has_main && has_reg && has_init) { continue; }
                            let name = bp.file_stem().and_then(|s| s.to_str()).unwrap_or("worker").replace('_', "-");
                            let cmd = format!("cd {} && cargo run --bin {}", mod_rel, name);
                            results.push((service_name(&name), cmd));
                        }
                    }
                }
            }
        }
    }
    results
}


fn detect_ts_workers() -> Vec<(String, String)> {
    let mut results = vec![];
    let root = repo_root();

    let mut files = vec![];
    let _ = walk_dir_at(&root, &mut |path: &Path| {
        if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            if ext == "ts" || ext == "js" {
                if path.file_name().and_then(|s| s.to_str()).map_or(false, |s| !s.ends_with(".d.ts")) {
                    files.push(path.to_path_buf());
                }
            }
        }
        Ok(())
    });

    for path in files {
        if should_skip(&path) { continue; }
        if let Ok(abs) = fs::canonicalize(&path) {
            if is_sdk_dir(&abs) { continue; }
        }
        let content = match fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => continue,
        };
        if !content.contains("'callwire'") && !content.contains("\"callwire\"") { continue; }
        if !content.contains("new Server(") && !content.contains(".serve(") { continue; }

        let name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("worker").replace('_', "-");
        let cmd = format!("npx tsx {}", path.to_string_lossy());
        results.push((service_name(&name), cmd));
    }
    results
}

fn detect_py_workers() -> Vec<(String, String)> {
    let mut results = vec![];
    let root = repo_root();

    let mut files = vec![];
    let _ = walk_dir_at(&root, &mut |path: &Path| {
        if path.extension().and_then(|e| e.to_str()) == Some("py") {
            files.push(path.to_path_buf());
        }
        Ok(())
    });

    for path in files {
        if should_skip(&path) { continue; }
        if let Ok(abs) = fs::canonicalize(&path) {
            if is_sdk_dir(&abs) { continue; }
        }
        let fname = path.file_name().and_then(|s| s.to_str()).unwrap_or("");
        if fname.starts_with("test_") || fname == "__main__.py" { continue; }

        let content = match fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => continue,
        };
        let has_export = content.contains("@export") || content.contains("callwire.export");
        let has_serve = content.contains("callwire.serve(") || content.contains("callwire.init(");
        if !(has_export && has_serve) { continue; }

        let name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("worker").replace('_', "-");
        let cmd = format!("python {}", path.to_string_lossy());
        results.push((service_name(&name), cmd));
    }
    results
}

fn format_toml(services: &[(String, String)]) -> String {
    let pwd = repo_root().file_name().and_then(|s| s.to_str()).unwrap_or("project").to_string();

    let mut out = String::new();
    writeln!(out, "# callwire.toml").ok();
    writeln!(out, "# ────────────────────────────────────────────────────────────").ok();
    writeln!(out, "# Generated by `callwire init` (Rust CLI)").ok();
    writeln!(out, "# ────────────────────────────────────────────────────────────").ok();
    writeln!(out).ok();
    writeln!(out, "[project]").ok();
    writeln!(out, "name = \"{}-project\"", pwd).ok();
    writeln!(out, "version = \"1.0.0\"").ok();
    writeln!(out).ok();
    writeln!(out, "# ── Worker services ─────────────────────────────────────────").ok();
    writeln!(out).ok();

    for (name, cmd) in services {
        writeln!(out, "[services.{}]", name).ok();
        writeln!(out, "dev_cmd  = \"{}\"", cmd).ok();
        writeln!(out, "prod_cmd = \"./bin/{}\"", name).ok();
        writeln!(out).ok();
    }

    out
}

fn scan_and_generate() -> String {
    let mut services: Vec<(String, String)> = Vec::new();
    services.extend(detect_go_workers());
    services.extend(detect_rust_workers());
    services.extend(detect_ts_workers());
    services.extend(detect_py_workers());
    format_toml(&services)
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() < 2 || args[1] != "init" {
        eprintln!("Usage: cargo run --bin callwire -- init");
        std::process::exit(1);
    }

    let path = "callwire.toml";
    if std::path::Path::new(path).exists() {
        eprintln!("callwire: '{}' already exists — skipping.", path);
        eprintln!("callwire: delete it first, or edit it manually.");
        std::process::exit(1);
    }

    let content = scan_and_generate();
    match fs::write(path, &content) {
        Ok(_) => {
            let count = content.matches("[services.").count();
            println!("Created callwire.toml with {} service(s)", count);
            println!("{}", content);
        }
        Err(e) => {
            eprintln!("callwire: failed to write callwire.toml: {}", e);
            std::process::exit(1);
        }
    }
}