zing-cli 0.0.2

CLI tool + MCP server for paid semantic search on Zing
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
use clap::{Parser, Subcommand};

mod api;
mod config;
mod error;
mod keystore;
mod mcp;
mod models;
mod sui;

#[derive(Parser)]
#[command(
    name = "zing",
    about = "Zing platform CLI",
    version = env!("CARGO_PKG_VERSION"),
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Show the CLI version
    Version,

    /// Search across all indexed wikis
    Search {
        /// Search query
        q: String,
        /// Filter to a specific owner (omit for global search)
        #[arg(long)]
        owner: Option<String>,
        /// Max results (default: 20, max: 50)
        #[arg(long, default_value = "20")]
        limit: u32,
        /// Override API base URL
        #[arg(long)]
        api: Option<String>,
        /// Override Sui RPC URL
        #[arg(long)]
        rpc: Option<String>,
        /// Output as JSON for agent consumption
        #[arg(long)]
        json: bool,
    },
    /// Retrieve semantic chunks
    Chunks {
        /// Search query
        q: String,
        /// Filter to a specific owner (omit for global search)
        #[arg(long)]
        owner: Option<String>,
        /// Max results (default: 20, max: 50)
        #[arg(long, default_value = "20")]
        limit: u32,
        /// Return full untruncated text (no truncation)
        #[arg(long)]
        expand: bool,
        /// Override API base URL
        #[arg(long)]
        api: Option<String>,
        /// Override Sui RPC URL
        #[arg(long)]
        rpc: Option<String>,
        /// Output as JSON for agent consumption
        #[arg(long)]
        json: bool,
    },
    /// Expand truncated chunks — get full untruncated text
    Expand {
        /// Chunk IDs to expand (max 20)
        chunk_ids: Vec<u64>,
        /// Override API base URL
        #[arg(long)]
        api: Option<String>,
        /// Override Sui RPC URL
        #[arg(long)]
        rpc: Option<String>,
        /// Output as JSON for agent consumption
        #[arg(long)]
        json: bool,
    },
    /// Sui wallet queries
    Client {
        #[command(subcommand)]
        action: ClientAction,
    },
    /// MCP server commands
    Mcp {
        #[command(subcommand)]
        action: McpAction,
    },
}

#[derive(Subcommand)]
enum ClientAction {
    /// Show the active Sui address
    ActiveAddress,
    /// Show SUI and USDC balances
    Balance,
}

#[derive(Subcommand)]
enum McpAction {
    /// Start the MCP server on stdio
    Serve {
        /// Override API base URL
        #[arg(long)]
        api: Option<String>,
    },
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "info".into()),
        )
        .init();

    let cli = Cli::parse();

    match cli.command {
        Command::Version => {
            println!("zing {}", env!("CARGO_PKG_VERSION"));
        }
        Command::Search { q, owner, limit, api, rpc, json } => {
            run_search(q, owner, limit, api, rpc, json).await?;
        }
        Command::Chunks { q, owner, limit, expand, api, rpc, json } => {
            run_chunks(q, owner, limit, expand, api, rpc, json).await?;
        }
        Command::Expand { chunk_ids, api, rpc, json } => {
            run_expand(chunk_ids, api, rpc, json).await?;
        }
        Command::Client { action } => {
            run_client(action).await?;
        }
        Command::Mcp { action } => {
            match action {
                McpAction::Serve { api } => {
                    let server = mcp::ZingMcpServer::new(api).await?;
                    server.serve().await?;
                }
            }
        }
    }

    Ok(())
}

async fn run_expand(
    chunk_ids: Vec<u64>,
    api_override: Option<String>,
    rpc_override: Option<String>,
    json: bool,
) -> anyhow::Result<()> {
    let cfg = config::load_config()?;
    let rpc_url = rpc_override.unwrap_or(cfg.rpc_url);
    let api_base_url = api_override.unwrap_or(cfg.api_base_url);

    // Convert Vec<u64> to Vec<i64> (BCS expects i64 in ExpandAccessMessage)
    let chunk_ids_i64: Vec<i64> = chunk_ids.iter().map(|&id| id as i64).collect();

    let keypair = keystore::load_keypair(&cfg.keystore_path, &cfg.active_address)?;

    let response = api::expand_chunks(
        &rpc_url,
        &api_base_url,
        &keypair,
        &cfg.active_address,
        &cfg.platform_usdc_address,
        &chunk_ids_i64,
    )
    .await?;

    if json {
        let agent_chunks: Vec<models::AgentExpandedChunk> = response.chunks.iter().map(|c| {
            models::AgentExpandedChunk {
                chunk_id: c.chunk_id,
                article_id: c.article_id.clone(),
                heading_path: c.heading_path.clone(),
                chunk_text: c.chunk_text.clone(),
                content_type: c.content_type.clone(),
                token_count: c.token_count,
                truncated: c.truncated.clone(),
            }
        }).collect();

        let agent_response = models::AgentExpandResponse {
            chunks: agent_chunks,
            budget: models::AgentBudget {
                paid_usdc: response.budget.paid_usdc,
                consumed_usdc: response.budget.consumed_usdc,
                remaining_usdc: response.budget.remaining_usdc,
            },
        };

        println!("{}", serde_json::to_string_pretty(&agent_response)?);
        return Ok(());
    }

    println!("Expand: {} chunks returned", response.chunks.len());

    for (i, chunk) in response.chunks.iter().enumerate() {
        println!();
        println!("{}. Chunk {}{} ({} tokens)", i + 1, chunk.chunk_id, chunk.content_type, chunk.token_count);
        if !chunk.heading_path.is_empty() {
            println!("   heading: {}", chunk.heading_path.join(" > "));
        }
        println!("   article: {}", chunk.article_id);
        println!("   text:");
        for line in chunk.chunk_text.lines() {
            println!("   {}", line);
        }
        if let Some(ref t) = chunk.truncated {
            match t.content_type.as_str() {
                "table" => {
                    if let (Some(total), Some(shown)) = (t.table_rows_total, t.table_rows_shown) {
                        println!("   (table: {} total rows, {} shown)", total, shown);
                    }
                }
                "code" => {
                    if let (Some(total), Some(shown)) = (t.code_lines_total, t.code_lines_shown) {
                        println!("   (code: {} total lines, {} shown)", total, shown);
                    }
                }
                "prose" => {
                    if let (Some(total), Some(shown)) = (t.prose_chars_total, t.prose_chars_shown) {
                        println!("   (prose: {} total chars, {} shown)", total, shown);
                    }
                }
                _ => {}
            }
        } else {
            println!("   (full text)");
        }
    }
    println!(
        "\nBudget: paid={}, consumed={}, remaining={}",
        response.budget.paid_usdc,
        response.budget.consumed_usdc,
        response.budget.remaining_usdc,
    );

    Ok(())
}

async fn run_client(action: ClientAction) -> anyhow::Result<()> {
    let cfg = config::load_config()?;
    let rpc_url = &cfg.rpc_url;

    match action {
        ClientAction::ActiveAddress => {
            println!("Active address: {}", cfg.active_address);
        }
        ClientAction::Balance => {
            let (addr_bal, coin_bal) =
                sui::get_usdc_balance(rpc_url, &cfg.active_address).await?;
            let total = addr_bal + coin_bal;

            println!("Active address: {}", cfg.active_address);
            println!(
                "USDC balance: {}.{:06} USDC",
                total / 1_000_000,
                total % 1_000_000
            );
        }
    }
    Ok(())
}

async fn run_search(
    q: String,
    owner: Option<String>,
    limit: u32,
    api_override: Option<String>,
    rpc_override: Option<String>,
    json: bool,
) -> anyhow::Result<()> {
    let cfg = config::load_config()?;
    let rpc_url = rpc_override.unwrap_or(cfg.rpc_url);
    let api_base_url = api_override.unwrap_or(cfg.api_base_url);

    let keypair = keystore::load_keypair(&cfg.keystore_path, &cfg.active_address)?;

    let wiki = owner.as_deref().unwrap_or("global").to_string();
    let owner_param = if owner.is_some() { owner.as_deref() } else { None };

    let response = api::search(
        &rpc_url,
        &api_base_url,
        &keypair,
        &cfg.active_address,
        &cfg.platform_usdc_address,
        &q,
        &wiki,
        owner_param,
        limit.min(50),
    )
    .await?;

    if json {
        let agent_results: Vec<models::AgentSearchResult> = response.results.iter().map(|r| {
            let excerpt = r.best_match.as_ref().map(|m| m.excerpt.clone());
            let heading_path = r.best_match.as_ref().map(|m| m.heading_path.clone()).unwrap_or_default();
            models::AgentSearchResult {
                article_id: r.article_id.clone(),
                title: r.title.clone().unwrap_or_else(|| "Untitled".into()),
                excerpt,
                heading_path,
                score: r.signals.relevance_score,
                article_token_count: r.signals.article_token_count,
                recency_days: r.signals.recency_days,
                tags: r.tags.clone(),
            }
        }).collect();

        let agent_response = models::AgentSearchResponse {
            results: agent_results,
            budget: models::AgentBudget {
                paid_usdc: response.budget.paid_usdc,
                consumed_usdc: response.budget.consumed_usdc,
                remaining_usdc: response.budget.remaining_usdc,
            },
        };

        println!("{}", serde_json::to_string_pretty(&agent_response)?);
        return Ok(());
    }

    println!("Search: \"{}\"{} results", q, response.results.len());

    for (i, result) in response.results.iter().enumerate() {
        let title = result.title.as_deref().unwrap_or("Untitled");
        let score = result.signals.relevance_score;
        let recency = result.signals.recency_days;
        let tags = result.tags.join(" · ");
        let excerpt = result.best_match.as_ref().map(|m| m.excerpt.as_str()).unwrap_or("");
        let heading_path = result.best_match.as_ref().map(|m| m.heading_path.join(" > ")).unwrap_or_default();

        println!();
        println!("{}. {:<40} score: {:.2}  {}d ago", i + 1, title, score, recency);
        if !tags.is_empty() {
            println!("   tags: {}", tags);
        }
        if !excerpt.is_empty() {
            println!("   \"{}\"", excerpt);
        }
        if !heading_path.is_empty() {
            println!("   heading: {}", heading_path);
        }
        println!("   article: {}", result.article_id);
    }
    println!(
        "\nBudget: paid={}, consumed={}, remaining={}",
        response.budget.paid_usdc,
        response.budget.consumed_usdc,
        response.budget.remaining_usdc,
    );

    Ok(())
}

async fn run_chunks(
    q: String,
    owner: Option<String>,
    limit: u32,
    expand: bool,
    api_override: Option<String>,
    rpc_override: Option<String>,
    json: bool,
) -> anyhow::Result<()> {
    let cfg = config::load_config()?;
    let rpc_url = rpc_override.unwrap_or(cfg.rpc_url);
    let api_base_url = api_override.unwrap_or(cfg.api_base_url);

    let keypair = keystore::load_keypair(&cfg.keystore_path, &cfg.active_address)?;

    let wiki = owner.as_deref().unwrap_or("global").to_string();
    let owner_param = if owner.is_some() { owner.as_deref() } else { None };

    let response = api::chunks(
        &rpc_url,
        &api_base_url,
        &keypair,
        &cfg.active_address,
        &cfg.platform_usdc_address,
        &q,
        &wiki,
        owner_param,
        limit.min(50),
        if expand { Some(true) } else { None },
        None,
    )
    .await?;

    if json {
        let agent_chunks: Vec<models::AgentChunkResult> = response.chunks.iter().map(|c| {
            models::AgentChunkResult {
                chunk_id: c.chunk_id,
                article_id: c.article_id.clone(),
                title: c.title.clone(),
                text: c.text.clone(),
                score: c.scores.blended,
                chunk_token_count: c.chunk_token_count,
                heading_path: c.heading_path.clone(),
                content_type: c.content_type.clone(),
                language: c.language.clone(),
                truncated: c.truncated.clone(),
            }
        }).collect();

        let agent_response = models::AgentChunksResponse {
            chunks: agent_chunks,
            budget: models::AgentBudget {
                paid_usdc: response.budget.paid_usdc,
                consumed_usdc: response.budget.consumed_usdc,
                remaining_usdc: response.budget.remaining_usdc,
            },
        };

        println!("{}", serde_json::to_string_pretty(&agent_response)?);
        return Ok(());
    }

    println!("Chunks: \"{}\"{} results", q, response.chunks.len());

    for (i, chunk) in response.chunks.iter().enumerate() {
        let text_preview = &chunk.text;

        println!();
        println!("{}. {:<40} score: {:.2}  {} tokens", i + 1, chunk.title, chunk.scores.blended, chunk.chunk_token_count);
        println!("   \"{}\"", text_preview);
        if !chunk.heading_path.is_empty() {
            println!("   heading: {}", chunk.heading_path.join(" > "));
        }
        println!("   chunk_id: {}  article: {}", chunk.chunk_id, chunk.article_id);
    }
    println!(
        "\nBudget: paid={}, consumed={}, remaining={}",
        response.budget.paid_usdc,
        response.budget.consumed_usdc,
        response.budget.remaining_usdc,
    );

    Ok(())
}