1use crate::{
73 config::{ForgeConfig, RuntimeType, Environment},
74 debug::info,
75 runtime::{
76 adaptive::AdaptiveRuntimeSelector, actor_runtime::ForgeActorRuntime,
77 async_runtime::ForgeAsyncRuntime, runtime::ForgeRuntime,
78 runtime_trait::RuntimeTrait, system_detector::SystemResources,
79 },
80 types::{RuntimeOptions, Extensions, Content, EditorOptionsBuilder},
81 ForgeResult,
82};
83use std::sync::Arc;
84
85#[derive(Default)]
111pub struct ForgeRuntimeBuilder {
112 runtime_type: Option<RuntimeType>,
114 environment: Option<Environment>,
115
116 content: Option<Content>,
118 extensions: Vec<Extensions>,
119 history_limit: Option<usize>,
120 event_handlers: Vec<
121 Arc<dyn crate::event::EventHandler<crate::event::Event> + Send + Sync>,
122 >,
123
124 max_concurrent_tasks: Option<usize>,
126 queue_size: Option<usize>,
127 enable_monitoring: Option<bool>,
128 middleware_timeout_ms: Option<u64>,
129
130 schema_paths: Vec<String>,
132
133 full_config: Option<ForgeConfig>,
135}
136
137impl ForgeRuntimeBuilder {
138 pub fn new() -> Self {
145 Self::default()
146 }
147
148 pub fn runtime_type(
160 mut self,
161 runtime_type: RuntimeType,
162 ) -> Self {
163 self.runtime_type = Some(runtime_type);
164 self
165 }
166
167 pub fn environment(
177 mut self,
178 environment: Environment,
179 ) -> Self {
180 self.environment = Some(environment);
181 self
182 }
183
184 pub fn content(
194 mut self,
195 content: Content,
196 ) -> Self {
197 self.content = Some(content);
198 self
199 }
200
201 pub fn extension(
209 mut self,
210 extension: Extensions,
211 ) -> Self {
212 self.extensions.push(extension);
213 self
214 }
215
216 pub fn extensions(
224 mut self,
225 extensions: Vec<Extensions>,
226 ) -> Self {
227 self.extensions.extend(extensions);
228 self
229 }
230
231 pub fn schema_path(
239 mut self,
240 path: impl Into<String>,
241 ) -> Self {
242 self.schema_paths.push(path.into());
243 self
244 }
245
246 pub fn schema_paths(
254 mut self,
255 paths: Vec<String>,
256 ) -> Self {
257 self.schema_paths.extend(paths);
258 self
259 }
260
261 pub fn max_concurrent_tasks(
273 mut self,
274 count: usize,
275 ) -> Self {
276 self.max_concurrent_tasks = Some(count);
277 self
278 }
279
280 pub fn queue_size(
290 mut self,
291 size: usize,
292 ) -> Self {
293 self.queue_size = Some(size);
294 self
295 }
296
297 pub fn enable_monitoring(
305 mut self,
306 enable: bool,
307 ) -> Self {
308 self.enable_monitoring = Some(enable);
309 self
310 }
311
312 pub fn middleware_timeout_ms(
320 mut self,
321 timeout: u64,
322 ) -> Self {
323 self.middleware_timeout_ms = Some(timeout);
324 self
325 }
326
327 pub fn history_limit(
337 mut self,
338 limit: usize,
339 ) -> Self {
340 self.history_limit = Some(limit);
341 self
342 }
343
344 pub fn event_handler(
352 mut self,
353 handler: Arc<
354 dyn crate::event::EventHandler<crate::event::Event> + Send + Sync,
355 >,
356 ) -> Self {
357 self.event_handlers.push(handler);
358 self
359 }
360
361 pub fn with_config(
374 mut self,
375 config: ForgeConfig,
376 ) -> Self {
377 self.full_config = Some(config);
378 self
379 }
380
381 pub async fn from_config_file(path: &str) -> ForgeResult<Self> {
388 let content = tokio::fs::read_to_string(path).await.map_err(|e| {
389 crate::error::error_utils::storage_error(format!(
390 "Failed to read config file: {}",
391 e
392 ))
393 })?;
394
395 let config: ForgeConfig =
396 serde_json::from_str(&content).map_err(|e| {
397 crate::error::error_utils::config_error(format!(
398 "Failed to parse JSON config: {}",
399 e
400 ))
401 })?;
402
403 Ok(Self::new().with_config(config))
404 }
405
406 pub async fn build(self) -> ForgeResult<AnyRuntime> {
427 let (config, options) = self.build_config_and_options().await?;
429
430 let runtime_type = match config.runtime.runtime_type {
432 RuntimeType::Auto => {
433 let resources = SystemResources::detect();
434 let selected =
435 AdaptiveRuntimeSelector::select_runtime(&resources);
436
437 info!(
438 "🖥️ 系统资源: {} 核心 / {} 线程, {} GB 内存 ({})",
439 resources.cpu_cores,
440 resources.cpu_threads,
441 resources.total_memory_mb / 1024,
442 resources.tier_description()
443 );
444 info!("⚡ 自动选择运行时: {:?}", selected);
445
446 selected
447 },
448 rt => {
449 info!("⚡ 使用指定运行时: {:?}", rt);
450 rt
451 },
452 };
453
454 Self::create_runtime(runtime_type, options, config).await
456 }
457
458 async fn build_config_and_options(
460 self
461 ) -> ForgeResult<(ForgeConfig, RuntimeOptions)> {
462 let mut config = self.full_config.unwrap_or_else(|| {
464 match self.environment {
466 Some(env) => ForgeConfig::for_environment(env),
467 None => {
468 let resources = SystemResources::detect();
470 AdaptiveRuntimeSelector::generate_config(&resources)
471 },
472 }
473 });
474
475 if let Some(rt) = self.runtime_type {
477 config.runtime.runtime_type = rt;
478 }
479 if let Some(tasks) = self.max_concurrent_tasks {
480 config.processor.max_concurrent_tasks = tasks;
481 }
482 if let Some(size) = self.queue_size {
483 config.processor.max_queue_size = size;
484 }
485 if let Some(enable) = self.enable_monitoring {
486 config.performance.enable_monitoring = enable;
487 }
488 if let Some(timeout) = self.middleware_timeout_ms {
489 config.performance.middleware_timeout_ms = timeout;
490 }
491
492 if !self.schema_paths.is_empty() {
494 config.extension.xml_schema_paths = self.schema_paths;
495 }
496
497 let mut options_builder = EditorOptionsBuilder::new();
499
500 if let Some(content) = self.content {
501 options_builder = options_builder.content(content);
502 }
503
504 options_builder = options_builder.extensions(self.extensions);
505
506 if let Some(limit) = self.history_limit {
507 options_builder = options_builder.history_limit(limit);
508 }
509
510 options_builder = options_builder.event_handlers(self.event_handlers);
511
512 let options = options_builder.build();
513
514 Ok((config, options))
515 }
516
517 async fn create_runtime(
519 runtime_type: RuntimeType,
520 options: RuntimeOptions,
521 config: ForgeConfig,
522 ) -> ForgeResult<AnyRuntime> {
523 match runtime_type {
524 RuntimeType::Sync => {
525 let runtime =
526 ForgeRuntime::create_with_config(options, config).await?;
527 Ok(AnyRuntime::Sync(runtime))
528 },
529 RuntimeType::Async => {
530 let runtime =
531 ForgeAsyncRuntime::create_with_config(options, config)
532 .await?;
533 Ok(AnyRuntime::Async(runtime))
534 },
535 RuntimeType::Actor => {
536 let runtime =
537 ForgeActorRuntime::create_with_config(options, config)
538 .await?;
539 Ok(AnyRuntime::Actor(runtime))
540 },
541 RuntimeType::Auto => {
542 unreachable!("Auto should be resolved before this point")
543 },
544 }
545 }
546}
547
548pub enum AnyRuntime {
567 Sync(ForgeRuntime),
568 Async(ForgeAsyncRuntime),
569 Actor(ForgeActorRuntime),
570}
571
572impl AnyRuntime {
573 pub fn runtime_type(&self) -> RuntimeType {
575 match self {
576 Self::Sync(_) => RuntimeType::Sync,
577 Self::Async(_) => RuntimeType::Async,
578 Self::Actor(_) => RuntimeType::Actor,
579 }
580 }
581
582 pub fn as_sync(&self) -> Option<&ForgeRuntime> {
584 match self {
585 Self::Sync(rt) => Some(rt),
586 _ => None,
587 }
588 }
589
590 pub fn as_async(&self) -> Option<&ForgeAsyncRuntime> {
592 match self {
593 Self::Async(rt) => Some(rt),
594 _ => None,
595 }
596 }
597
598 pub fn as_actor(&self) -> Option<&ForgeActorRuntime> {
600 match self {
601 Self::Actor(rt) => Some(rt),
602 _ => None,
603 }
604 }
605
606 pub fn as_sync_mut(&mut self) -> Option<&mut ForgeRuntime> {
608 match self {
609 Self::Sync(rt) => Some(rt),
610 _ => None,
611 }
612 }
613
614 pub fn as_async_mut(&mut self) -> Option<&mut ForgeAsyncRuntime> {
616 match self {
617 Self::Async(rt) => Some(rt),
618 _ => None,
619 }
620 }
621
622 pub fn as_actor_mut(&mut self) -> Option<&mut ForgeActorRuntime> {
624 match self {
625 Self::Actor(rt) => Some(rt),
626 _ => None,
627 }
628 }
629}
630
631impl AnyRuntime {
633 pub async fn dispatch(
635 &mut self,
636 transaction: mf_state::Transaction,
637 ) -> ForgeResult<()> {
638 match self {
639 Self::Sync(rt) => rt.dispatch(transaction).await,
640 Self::Async(rt) => rt.dispatch(transaction).await,
641 Self::Actor(rt) => rt.dispatch(transaction).await,
642 }
643 }
644
645 pub async fn dispatch_with_meta(
647 &mut self,
648 transaction: mf_state::Transaction,
649 description: String,
650 meta: serde_json::Value,
651 ) -> ForgeResult<()> {
652 match self {
653 Self::Sync(rt) => {
654 rt.dispatch_with_meta(transaction, description, meta).await
655 },
656 Self::Async(rt) => {
657 rt.dispatch_with_meta(transaction, description, meta).await
658 },
659 Self::Actor(rt) => {
660 rt.dispatch_with_meta(transaction, description, meta).await
661 },
662 }
663 }
664
665 pub async fn command(
667 &mut self,
668 command: Arc<dyn mf_state::transaction::Command>,
669 ) -> ForgeResult<()> {
670 match self {
671 Self::Sync(rt) => rt.command(command).await,
672 Self::Async(rt) => rt.command(command).await,
673 Self::Actor(rt) => rt.command(command).await,
674 }
675 }
676
677 pub async fn command_with_meta(
679 &mut self,
680 command: Arc<dyn mf_state::transaction::Command>,
681 description: String,
682 meta: serde_json::Value,
683 ) -> ForgeResult<()> {
684 match self {
685 Self::Sync(rt) => {
686 rt.command_with_meta(command, description, meta).await
687 },
688 Self::Async(rt) => {
689 rt.command_with_meta(command, description, meta).await
690 },
691 Self::Actor(rt) => {
692 rt.command_with_meta(command, description, meta).await
693 },
694 }
695 }
696
697 pub async fn get_state(&self) -> ForgeResult<Arc<mf_state::State>> {
699 match self {
700 Self::Sync(rt) => Ok(Arc::clone(rt.get_state())),
701 Self::Async(rt) => rt.get_state().await,
702 Self::Actor(rt) => rt.get_state().await,
703 }
704 }
705
706 pub async fn get_tr(&self) -> ForgeResult<mf_state::Transaction> {
708 match self {
709 Self::Sync(rt) => Ok(rt.get_tr()),
710 Self::Async(rt) => rt.get_tr().await,
711 Self::Actor(rt) => rt.get_tr().await,
712 }
713 }
714
715 pub async fn doc(&self) -> ForgeResult<Arc<mf_model::NodePool>> {
717 match self {
718 Self::Sync(rt) => Ok(rt.doc()),
719 Self::Async(rt) => rt.doc().await,
720 Self::Actor(rt) => rt.doc().await,
721 }
722 }
723
724 pub async fn schema(&self) -> ForgeResult<Arc<mf_model::Schema>> {
726 match self {
727 Self::Sync(rt) => Ok(rt.get_schema()),
728 Self::Async(rt) => rt.get_schema().await,
729 Self::Actor(rt) => rt.get_schema().await,
730 }
731 }
732
733 pub async fn undo(&mut self) -> ForgeResult<()> {
735 match self {
736 Self::Sync(rt) => {
737 rt.undo();
738 Ok(())
739 },
740 Self::Async(rt) => rt.undo().await,
741 Self::Actor(rt) => rt.undo().await,
742 }
743 }
744
745 pub async fn redo(&mut self) -> ForgeResult<()> {
747 match self {
748 Self::Sync(rt) => {
749 rt.redo();
750 Ok(())
751 },
752 Self::Async(rt) => rt.redo().await,
753 Self::Actor(rt) => rt.redo().await,
754 }
755 }
756
757 pub async fn jump(
759 &mut self,
760 steps: isize,
761 ) -> ForgeResult<()> {
762 match self {
763 Self::Sync(rt) => {
764 rt.jump(steps);
765 Ok(())
766 },
767 Self::Async(rt) => rt.jump(steps).await,
768 Self::Actor(rt) => rt.jump(steps).await,
769 }
770 }
771
772 pub fn get_config(&self) -> &crate::config::ForgeConfig {
774 match self {
775 Self::Sync(rt) => rt.get_config(),
776 Self::Async(rt) => rt.get_config(),
777 Self::Actor(rt) => rt.get_config(),
778 }
779 }
780
781 pub fn update_config(
783 &mut self,
784 config: crate::config::ForgeConfig,
785 ) {
786 match self {
787 Self::Sync(rt) => rt.update_config(config),
788 Self::Async(rt) => rt.update_config(config),
789 Self::Actor(rt) => rt.update_config(config),
790 }
791 }
792
793 pub fn get_options(&self) -> crate::types::RuntimeOptions {
797 match self {
798 Self::Sync(rt) => rt.get_options().clone(),
799 Self::Async(rt) => rt.get_options().clone(),
800 Self::Actor(rt) => rt.get_options(),
801 }
802 }
803
804 pub async fn destroy(&mut self) -> ForgeResult<()> {
806 match self {
807 Self::Sync(rt) => rt.destroy().await,
808 Self::Async(rt) => rt.destroy().await,
809 Self::Actor(rt) => rt.destroy().await,
810 }
811 }
812}