elicit_bevy 0.11.1

Elicitation-enabled Bevy ECS tools for code generation and game development
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! `BevyAppPlugin` — descriptor-registry tools for Bevy app assembly.
//!
//! This plugin stores app descriptors server-side and emits a complete Bevy
//! `main.rs` scaffold on demand. It also exposes a few related stateless
//! skeleton emitters for plugin, plugin-group, and state-machine boilerplate.

use std::collections::HashMap;
use std::sync::{Arc, Mutex, MutexGuard};

use elicitation::{
    PluginContext, PluginToolRegistration, StatefulPlugin, ToolDescriptor, elicit_tool,
};
use rmcp::{
    ErrorData,
    model::{CallToolRequestParams, CallToolResult, Content, Tool},
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::instrument;
use uuid::Uuid;

/// Stored configuration for a generated Bevy app.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct BevyAppDescriptor {
    /// Human-readable app name.
    pub name: String,
    /// Optional DefaultPlugins configuration.
    pub default_plugins: Option<BevyDefaultPluginsDescriptor>,
    /// Additional plugin expressions registered on the app.
    pub plugins: Vec<String>,
    /// Custom schedules attached to the app.
    pub schedules: Vec<BevyScheduleDescriptor>,
    /// Optional runner expression passed to `app.set_runner(...)`.
    pub runner_expr: Option<String>,
}

/// Configuration for `DefaultPlugins`.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct BevyDefaultPluginsDescriptor {
    /// Optional `WindowPlugin { .. }` expression passed to `DefaultPlugins.set(...)`.
    pub window_plugin_expr: Option<String>,
}

/// Ordering metadata for a custom Bevy schedule.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct BevyScheduleDescriptor {
    /// Schedule label expression, e.g. `MySchedule`.
    pub label_expr: String,
    /// If true, order this schedule in the startup schedule list.
    #[serde(default)]
    pub startup: bool,
    /// Insert after this existing schedule label.
    pub after: Option<String>,
    /// Insert before this existing schedule label.
    pub before: Option<String>,
}

/// A state hook used by `bevy_app__state_machine`.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct BevyStateHook {
    /// State variant name, e.g. `Loading`.
    pub state: String,
    /// System expression added for this transition.
    pub system_expr: String,
}

/// Shared registry context for `bevy_app__*` tools.
#[derive(Debug)]
pub struct BevyAppCtx {
    items: Mutex<HashMap<Uuid, BevyAppDescriptor>>,
}

impl BevyAppCtx {
    fn new() -> Self {
        Self {
            items: Mutex::new(HashMap::new()),
        }
    }

    fn lock_items(&self) -> Result<MutexGuard<'_, HashMap<Uuid, BevyAppDescriptor>>, ErrorData> {
        self.items
            .lock()
            .map_err(|_| ErrorData::internal_error("bevy_app registry lock poisoned", None))
    }
}

impl PluginContext for BevyAppCtx {}

/// Parameters for `bevy_app__new`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppNewParams {
    /// Human-readable app name.
    pub name: String,
}

/// Parameters for `bevy_app__add_default_plugins`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppAddDefaultPluginsParams {
    /// UUID returned by `bevy_app__new`.
    pub config_id: String,
    /// Optional `WindowPlugin { .. }` expression passed to `DefaultPlugins.set(...)`.
    #[serde(default)]
    pub window_plugin_expr: Option<String>,
}

/// Parameters for `bevy_app__add_plugin`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppAddPluginParams {
    /// UUID returned by `bevy_app__new`.
    pub config_id: String,
    /// Plugin expression, e.g. `MyGameplayPlugin` or `FrameTimeDiagnosticsPlugin`.
    pub plugin_expr: String,
}

/// Parameters for `bevy_app__add_schedule`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppAddScheduleParams {
    /// UUID returned by `bevy_app__new`.
    pub config_id: String,
    /// Custom schedule label expression.
    pub label_expr: String,
    /// Whether this schedule belongs in startup ordering.
    #[serde(default)]
    pub startup: bool,
    /// Insert this schedule after an existing one.
    #[serde(default)]
    pub after: Option<String>,
    /// Insert this schedule before an existing one.
    #[serde(default)]
    pub before: Option<String>,
}

/// Parameters for `bevy_app__set_runner`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppSetRunnerParams {
    /// UUID returned by `bevy_app__new`.
    pub config_id: String,
    /// Runner expression, e.g. `ScheduleRunnerPlugin::run_loop(...)` wrapper or custom closure.
    pub runner_expr: String,
}

/// Parameters for `bevy_app__describe`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppDescribeParams {
    /// UUID returned by `bevy_app__new`.
    pub config_id: String,
}

/// Parameters for `bevy_app__emit`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyAppEmitParams {
    /// UUID returned by `bevy_app__new`.
    pub config_id: String,
}

/// Parameters for `bevy_app__plugin_struct`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyPluginStructParams {
    /// Plugin struct name.
    pub name: String,
    /// Optional body inserted into `fn build(&self, app: &mut App) { ... }`.
    #[serde(default)]
    pub body: Option<String>,
}

/// Parameters for `bevy_app__plugin_group`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyPluginGroupParams {
    /// Plugin-group struct name.
    pub name: String,
    /// Plugin expressions appended to the builder.
    #[serde(default)]
    pub plugins: Vec<String>,
}

/// Parameters for `bevy_app__state_machine`.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct BevyStateMachineParams {
    /// App-like receiver expression, defaults to `app`.
    #[serde(default = "default_app_var")]
    pub app_var: String,
    /// State enum name.
    pub enum_name: String,
    /// Optional visibility, e.g. `pub`.
    #[serde(default)]
    pub visibility: Option<String>,
    /// State variants in display order.
    pub variants: Vec<String>,
    /// Optional initial/default state variant.
    #[serde(default)]
    pub initial_state: Option<String>,
    /// Systems to register on `OnEnter`.
    #[serde(default)]
    pub on_enter: Vec<BevyStateHook>,
    /// Systems to register on `OnExit`.
    #[serde(default)]
    pub on_exit: Vec<BevyStateHook>,
}

/// Result returned by `bevy_app__new`.
#[derive(Debug, Serialize)]
pub struct BevyAppNewResult {
    /// UUID handle for the stored app descriptor.
    pub config_id: String,
}

fn default_app_var() -> String {
    "app".to_string()
}

fn json_result<T: Serialize>(value: &T) -> CallToolResult {
    match serde_json::to_string(value) {
        Ok(serialized) => CallToolResult::success(vec![Content::text(serialized)]),
        Err(error) => {
            CallToolResult::error(vec![Content::text(format!("serialize error: {error}"))])
        }
    }
}

fn ok_text(text: impl Into<String>) -> CallToolResult {
    CallToolResult::success(vec![Content::text(text.into())])
}

fn parse_id(id: &str) -> Result<Uuid, ErrorData> {
    id.parse()
        .map_err(|_| ErrorData::invalid_params(format!("invalid UUID: {id}"), None))
}

fn parse_ident(src: &str, context: &str) -> Result<syn::Ident, ErrorData> {
    syn::parse_str::<syn::Ident>(src).map_err(|error| {
        ErrorData::invalid_params(format!("invalid {context} `{src}`: {error}"), None)
    })
}

fn parse_visibility(src: &str) -> Result<syn::Visibility, ErrorData> {
    syn::parse_str::<syn::Visibility>(src).map_err(|error| {
        ErrorData::invalid_params(format!("invalid visibility `{src}`: {error}"), None)
    })
}

fn parse_expr(src: &str, context: &str) -> Result<(), ErrorData> {
    syn::parse_str::<syn::Expr>(src)
        .map(|_| ())
        .map_err(|error| {
            ErrorData::invalid_params(format!("invalid {context} `{src}`: {error}"), None)
        })
}

fn parse_tokens(src: &str, context: &str) -> Result<(), ErrorData> {
    src.parse::<proc_macro2::TokenStream>()
        .map(|_| ())
        .map_err(|error| {
            ErrorData::invalid_params(format!("invalid {context} `{src}`: {error}"), None)
        })
}

fn validate_schedule_params(params: &BevyAppAddScheduleParams) -> Result<(), ErrorData> {
    parse_expr(&params.label_expr, "schedule label")?;
    if let Some(after) = &params.after {
        parse_expr(after, "schedule after")?;
    }
    if let Some(before) = &params.before {
        parse_expr(before, "schedule before")?;
    }
    if params.after.is_some() && params.before.is_some() {
        return Err(ErrorData::invalid_params(
            "schedule ordering can use either `after` or `before`, not both".to_string(),
            None,
        ));
    }
    Ok(())
}

fn validate_state_machine_params(params: &BevyStateMachineParams) -> Result<(), ErrorData> {
    parse_expr(&params.app_var, "app receiver")?;
    parse_ident(&params.enum_name, "enum name")?;
    if let Some(visibility) = &params.visibility {
        let _ = parse_visibility(visibility)?;
    }
    if params.variants.is_empty() {
        return Err(ErrorData::invalid_params(
            "state variants must not be empty".to_string(),
            None,
        ));
    }
    for variant in &params.variants {
        parse_ident(variant, "state variant")?;
    }
    if let Some(initial) = &params.initial_state {
        parse_ident(initial, "initial state")?;
        if !params.variants.iter().any(|variant| variant == initial) {
            return Err(ErrorData::invalid_params(
                format!("initial_state `{initial}` is not in variants"),
                None,
            ));
        }
    }
    for hook in params.on_enter.iter().chain(params.on_exit.iter()) {
        parse_ident(&hook.state, "hook state")?;
        if !params.variants.iter().any(|variant| variant == &hook.state) {
            return Err(ErrorData::invalid_params(
                format!("hook state `{}` is not in variants", hook.state),
                None,
            ));
        }
        parse_expr(&hook.system_expr, "hook system expression")?;
    }
    Ok(())
}

fn emit_default_plugins(desc: &BevyDefaultPluginsDescriptor) -> String {
    match &desc.window_plugin_expr {
        Some(window) => format!("::bevy::DefaultPlugins.set({window})"),
        None => "::bevy::DefaultPlugins".to_string(),
    }
}

fn emit_schedule(desc: &BevyScheduleDescriptor) -> Vec<String> {
    let mut lines = vec![format!(
        "    app.add_schedule(::bevy::ecs::schedule::Schedule::new({}));",
        desc.label_expr
    )];
    match (&desc.after, &desc.before, desc.startup) {
        (Some(after), None, true) => {
            lines.push("    app.init_resource::<::bevy::app::MainScheduleOrder>();".to_string());
            lines.push(format!(
                "    app.world_mut().resource_mut::<::bevy::app::MainScheduleOrder>().insert_startup_after({after}, {});",
                desc.label_expr
            ));
        }
        (None, Some(before), true) => {
            lines.push("    app.init_resource::<::bevy::app::MainScheduleOrder>();".to_string());
            lines.push(format!(
                "    app.world_mut().resource_mut::<::bevy::app::MainScheduleOrder>().insert_startup_before({before}, {});",
                desc.label_expr
            ));
        }
        (Some(after), None, false) => {
            lines.push("    app.init_resource::<::bevy::app::MainScheduleOrder>();".to_string());
            lines.push(format!(
                "    app.world_mut().resource_mut::<::bevy::app::MainScheduleOrder>().insert_after({after}, {});",
                desc.label_expr
            ));
        }
        (None, Some(before), false) => {
            lines.push("    app.init_resource::<::bevy::app::MainScheduleOrder>();".to_string());
            lines.push(format!(
                "    app.world_mut().resource_mut::<::bevy::app::MainScheduleOrder>().insert_before({before}, {});",
                desc.label_expr
            ));
        }
        _ => {}
    }
    lines
}

fn emit_app(desc: &BevyAppDescriptor) -> String {
    let mut lines = vec![format!("// Generated Bevy app descriptor: {}", desc.name)];
    lines.push("fn main() {".to_string());
    lines.push("    let mut app = ::bevy::app::App::new();".to_string());
    if let Some(default_plugins) = &desc.default_plugins {
        lines.push(format!(
            "    app.add_plugins({});",
            emit_default_plugins(default_plugins)
        ));
    }
    for plugin in &desc.plugins {
        lines.push(format!("    app.add_plugins({plugin});"));
    }
    for schedule in &desc.schedules {
        lines.extend(emit_schedule(schedule));
    }
    if let Some(runner) = &desc.runner_expr {
        lines.push(format!("    app.set_runner({runner});"));
    }
    lines.push("    app.run();".to_string());
    lines.push("}".to_string());
    lines.join("\n")
}

fn emit_plugin_struct(name: &str, body: Option<&str>) -> String {
    let body = body.unwrap_or("// configure plugin systems and resources here");
    format!(
        "pub struct {name};\n\n\
         impl ::bevy::app::Plugin for {name} {{\n\
         \x20   fn build(&self, app: &mut ::bevy::app::App) {{\n\
         \x20       let _ = app;\n\
         \x20       {body}\n\
         \x20   }}\n\
         }}\n"
    )
}

fn emit_plugin_group(name: &str, plugins: &[String]) -> String {
    let builder = if plugins.is_empty() {
        "        ::bevy::app::PluginGroupBuilder::start::<Self>()".to_string()
    } else {
        let chain = plugins
            .iter()
            .map(|plugin| format!("            .add({plugin})"))
            .collect::<Vec<_>>()
            .join("\n");
        format!("        ::bevy::app::PluginGroupBuilder::start::<Self>()\n{chain}")
    };
    format!(
        "pub struct {name};\n\n\
         impl ::bevy::app::PluginGroup for {name} {{\n\
         \x20   fn build(self) -> ::bevy::app::PluginGroupBuilder {{\n\
         {builder}\n\
         \x20   }}\n\
         }}\n"
    )
}

fn emit_state_machine(params: &BevyStateMachineParams) -> String {
    let visibility = params.visibility.as_deref().unwrap_or("pub");
    let default_variant = params
        .initial_state
        .clone()
        .unwrap_or_else(|| params.variants[0].clone());
    let enum_body = params
        .variants
        .iter()
        .map(|variant| {
            if variant == &default_variant {
                format!("    #[default]\n    {variant},")
            } else {
                format!("    {variant},")
            }
        })
        .collect::<Vec<_>>()
        .join("\n");
    let mut wiring = vec![format!(
        "{app}.init_state::<{enum_name}>();",
        app = params.app_var,
        enum_name = params.enum_name
    )];
    for hook in &params.on_enter {
        wiring.push(format!(
            "{app}.add_systems(::bevy::prelude::OnEnter({enum_name}::{state}), {system});",
            app = params.app_var,
            enum_name = params.enum_name,
            state = hook.state,
            system = hook.system_expr
        ));
    }
    for hook in &params.on_exit {
        wiring.push(format!(
            "{app}.add_systems(::bevy::prelude::OnExit({enum_name}::{state}), {system});",
            app = params.app_var,
            enum_name = params.enum_name,
            state = hook.state,
            system = hook.system_expr
        ));
    }
    format!(
        "#[derive(::bevy::prelude::States, Debug, Clone, PartialEq, Eq, Hash, Default)]\n\
         {visibility} enum {enum_name} {{\n\
         {enum_body}\n\
         }}\n\n\
         {wiring}\n",
        visibility = visibility,
        enum_name = params.enum_name,
        enum_body = enum_body,
        wiring = wiring.join("\n")
    )
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "new",
    description = "Create a new Bevy app descriptor registry entry.",
    emit = None
)]
#[instrument(skip(ctx), fields(name = %p.name))]
async fn new_app(ctx: Arc<BevyAppCtx>, p: BevyAppNewParams) -> Result<CallToolResult, ErrorData> {
    let id = Uuid::new_v4();
    ctx.lock_items()?.insert(
        id,
        BevyAppDescriptor {
            name: p.name,
            default_plugins: None,
            plugins: vec![],
            schedules: vec![],
            runner_expr: None,
        },
    );
    Ok(json_result(&BevyAppNewResult {
        config_id: id.to_string(),
    }))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "add_default_plugins",
    description = "Attach DefaultPlugins to a stored Bevy app descriptor, optionally with a WindowPlugin expression.",
    emit = None
)]
#[instrument(skip(ctx))]
async fn add_default_plugins(
    ctx: Arc<BevyAppCtx>,
    p: BevyAppAddDefaultPluginsParams,
) -> Result<CallToolResult, ErrorData> {
    if let Some(window) = &p.window_plugin_expr {
        parse_expr(window, "window plugin expression")?;
    }
    let id = parse_id(&p.config_id)?;
    let mut items = ctx.lock_items()?;
    let desc = items.get_mut(&id).ok_or_else(|| {
        ErrorData::invalid_params(format!("unknown config_id: {}", p.config_id), None)
    })?;
    desc.default_plugins = Some(BevyDefaultPluginsDescriptor {
        window_plugin_expr: p.window_plugin_expr,
    });
    Ok(ok_text("default plugins configured"))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "add_plugin",
    description = "Register an additional plugin expression on a stored Bevy app descriptor.",
    emit = None
)]
#[instrument(skip(ctx), fields(plugin = %p.plugin_expr))]
async fn add_plugin(
    ctx: Arc<BevyAppCtx>,
    p: BevyAppAddPluginParams,
) -> Result<CallToolResult, ErrorData> {
    parse_expr(&p.plugin_expr, "plugin expression")?;
    let id = parse_id(&p.config_id)?;
    let mut items = ctx.lock_items()?;
    let desc = items.get_mut(&id).ok_or_else(|| {
        ErrorData::invalid_params(format!("unknown config_id: {}", p.config_id), None)
    })?;
    desc.plugins.push(p.plugin_expr);
    Ok(ok_text("plugin added"))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "add_schedule",
    description = "Attach a custom schedule plus optional MainScheduleOrder placement to a stored Bevy app descriptor.",
    emit = None
)]
#[instrument(skip(ctx))]
async fn add_schedule(
    ctx: Arc<BevyAppCtx>,
    p: BevyAppAddScheduleParams,
) -> Result<CallToolResult, ErrorData> {
    validate_schedule_params(&p)?;
    let id = parse_id(&p.config_id)?;
    let mut items = ctx.lock_items()?;
    let desc = items.get_mut(&id).ok_or_else(|| {
        ErrorData::invalid_params(format!("unknown config_id: {}", p.config_id), None)
    })?;
    desc.schedules.push(BevyScheduleDescriptor {
        label_expr: p.label_expr,
        startup: p.startup,
        after: p.after,
        before: p.before,
    });
    Ok(ok_text("schedule added"))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "set_runner",
    description = "Set the app runner expression on a stored Bevy app descriptor.",
    emit = None
)]
#[instrument(skip(ctx), fields(runner = %p.runner_expr))]
async fn set_runner(
    ctx: Arc<BevyAppCtx>,
    p: BevyAppSetRunnerParams,
) -> Result<CallToolResult, ErrorData> {
    parse_expr(&p.runner_expr, "runner expression")?;
    let id = parse_id(&p.config_id)?;
    let mut items = ctx.lock_items()?;
    let desc = items.get_mut(&id).ok_or_else(|| {
        ErrorData::invalid_params(format!("unknown config_id: {}", p.config_id), None)
    })?;
    desc.runner_expr = Some(p.runner_expr);
    Ok(ok_text("runner set"))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "describe",
    description = "Return the JSON descriptor for a stored Bevy app configuration.",
    emit = None
)]
#[instrument(skip(ctx))]
async fn describe(
    ctx: Arc<BevyAppCtx>,
    p: BevyAppDescribeParams,
) -> Result<CallToolResult, ErrorData> {
    let id = parse_id(&p.config_id)?;
    let items = ctx.lock_items()?;
    let desc = items.get(&id).ok_or_else(|| {
        ErrorData::invalid_params(format!("unknown config_id: {}", p.config_id), None)
    })?;
    Ok(json_result(desc))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "emit",
    description = "Emit a complete Bevy `fn main()` scaffold from a stored app descriptor.",
    emit = None
)]
#[instrument(skip(ctx))]
async fn emit(ctx: Arc<BevyAppCtx>, p: BevyAppEmitParams) -> Result<CallToolResult, ErrorData> {
    let id = parse_id(&p.config_id)?;
    let items = ctx.lock_items()?;
    let desc = items.get(&id).ok_or_else(|| {
        ErrorData::invalid_params(format!("unknown config_id: {}", p.config_id), None)
    })?;
    Ok(ok_text(emit_app(desc)))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "plugin_struct",
    description = "Emit a `struct MyPlugin; impl Plugin for MyPlugin { ... }` skeleton.",
    emit = None
)]
#[instrument(skip_all)]
async fn plugin_struct(p: BevyPluginStructParams) -> Result<CallToolResult, ErrorData> {
    parse_ident(&p.name, "plugin name")?;
    if let Some(body) = &p.body {
        parse_tokens(body, "plugin body")?;
    }
    Ok(ok_text(emit_plugin_struct(&p.name, p.body.as_deref())))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "plugin_group",
    description = "Emit a `struct MyGroup; impl PluginGroup for MyGroup { ... }` skeleton.",
    emit = None
)]
#[instrument(skip_all)]
async fn plugin_group(p: BevyPluginGroupParams) -> Result<CallToolResult, ErrorData> {
    parse_ident(&p.name, "plugin group name")?;
    for plugin in &p.plugins {
        parse_expr(plugin, "plugin expression")?;
    }
    Ok(ok_text(emit_plugin_group(&p.name, &p.plugins)))
}

#[elicit_tool(
    plugin = "bevy_app",
    name = "state_machine",
    description = "Emit a `States` enum plus `init_state`, `OnEnter`, and `OnExit` app wiring.",
    emit = None
)]
#[instrument(skip_all)]
async fn state_machine(p: BevyStateMachineParams) -> Result<CallToolResult, ErrorData> {
    validate_state_machine_params(&p)?;
    Ok(ok_text(emit_state_machine(&p)))
}

/// MCP plugin providing `bevy_app__*` tools for Bevy app descriptors.
#[derive(Debug)]
pub struct BevyAppPlugin(Arc<BevyAppCtx>);

impl BevyAppPlugin {
    /// Creates a new `BevyAppPlugin` with an empty registry.
    pub fn new() -> Self {
        Self(Arc::new(BevyAppCtx::new()))
    }

    /// Returns a shared reference to the underlying app registry context.
    pub fn ctx(&self) -> Arc<BevyAppCtx> {
        Arc::clone(&self.0)
    }

    /// Convenience helper for tests and direct integration.
    pub async fn invoke_tool(
        &self,
        name: &str,
        args: serde_json::Value,
    ) -> Result<CallToolResult, ErrorData> {
        let bare = name.strip_prefix("bevy_app__").unwrap_or(name).to_string();
        let params = if let Some(map) = args.as_object().cloned() {
            CallToolRequestParams::new(bare.clone()).with_arguments(map)
        } else {
            CallToolRequestParams::new(bare.clone())
        };
        let descriptor = elicitation::inventory::iter::<PluginToolRegistration>()
            .filter(|registration| registration.plugin == "bevy_app")
            .find(|registration| registration.name == bare)
            .map(|registration| (registration.constructor)())
            .ok_or_else(|| ErrorData::invalid_params(format!("unknown tool: {name}"), None))?;
        descriptor
            .dispatch(
                self.0.clone() as Arc<dyn std::any::Any + Send + Sync>,
                params,
            )
            .await
    }
}

impl Default for BevyAppPlugin {
    fn default() -> Self {
        Self::new()
    }
}

impl StatefulPlugin for BevyAppPlugin {
    type Context = BevyAppCtx;

    fn name(&self) -> &'static str {
        "bevy_app"
    }

    fn list_tools(&self) -> Vec<Tool> {
        elicitation::inventory::iter::<PluginToolRegistration>()
            .filter(|registration| registration.plugin == "bevy_app")
            .map(|registration| (registration.constructor)().as_tool())
            .collect()
    }

    fn tool_descriptors(&self) -> Vec<ToolDescriptor> {
        elicitation::inventory::iter::<PluginToolRegistration>()
            .filter(|registration| registration.plugin == "bevy_app")
            .map(|registration| (registration.constructor)())
            .collect()
    }

    fn context(&self) -> Arc<Self::Context> {
        self.0.clone()
    }
}