QuestionGeneration.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\QuestionBankService;
  4. use App\Services\KnowledgeGraphService;
  5. use BackedEnum;
  6. use Filament\Notifications\Notification;
  7. use Filament\Pages\Page;
  8. use UnitEnum;
  9. use Livewire\Attributes\Computed;
  10. class QuestionGeneration extends Page
  11. {
  12. protected static ?string $title = '题目生成';
  13. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-sparkles';
  14. protected static ?string $navigationLabel = '题目生成';
  15. protected static string|UnitEnum|null $navigationGroup = '题库系统';
  16. protected static ?int $navigationSort = 1;
  17. protected string $view = 'filament.pages.question-generation';
  18. public ?string $generateKpCode = null;
  19. public array $selectedSkills = [];
  20. public int $questionCount = 100;
  21. public ?string $generateDifficulty = null;
  22. public ?string $generateType = null;
  23. public ?string $promptTemplate = null;
  24. public bool $isGenerating = false;
  25. public ?string $currentTaskId = null;
  26. public int $currentTaskProgress = 0;
  27. public ?string $currentTaskMessage = null;
  28. #[Computed(cache: false)]
  29. public function knowledgePointOptions(): array
  30. {
  31. return app(\App\Services\QuestionServiceApi::class)->getKnowledgePointOptions();
  32. }
  33. #[Computed(cache: false)]
  34. public function skillsOptions(): array
  35. {
  36. if (!$this->generateKpCode) {
  37. return [];
  38. }
  39. $service = app(KnowledgeGraphService::class);
  40. return $service->getSkillsByKnowledgePoint($this->generateKpCode);
  41. }
  42. #[Computed(cache: false)]
  43. public function questionTypeOptions(): array
  44. {
  45. return [
  46. 'CHOICE' => '单选题',
  47. 'MULTIPLE_CHOICE' => '多选题',
  48. 'FILL_IN_THE_BLANK' => '填空题',
  49. 'CALCULATION' => '计算题',
  50. 'WORD_PROBLEM' => '应用题',
  51. 'PROOF' => '证明题',
  52. ];
  53. }
  54. public function updatedGenerateKpCode(): void
  55. {
  56. // 选择新知识点时清空技能选择
  57. $this->selectedSkills = [];
  58. }
  59. public function toggleAllSkills(): void
  60. {
  61. $skillsOptions = $this->skillsOptions;
  62. $skillsCount = count($skillsOptions);
  63. \Log::info('[ToggleAllSkills] Called', [
  64. 'skillsCount' => $skillsCount,
  65. 'selectedSkillsCount' => count($this->selectedSkills),
  66. 'skillsOptions' => array_column($skillsOptions, 'code'),
  67. 'selectedSkills' => $this->selectedSkills
  68. ]);
  69. if ($skillsCount === 0) {
  70. \Log::info('[ToggleAllSkills] No skills available, returning');
  71. return;
  72. }
  73. // 获取所有技能的编码列表
  74. $allSkillCodes = array_values(array_unique(array_column($skillsOptions, 'code')));
  75. // 检查当前选中的技能是否等于全部技能
  76. if (count($this->selectedSkills) === $skillsCount &&
  77. count(array_intersect($this->selectedSkills, $allSkillCodes)) === $skillsCount) {
  78. // 如果已全选,则清空
  79. $this->selectedSkills = [];
  80. \Log::info('[ToggleAllSkills] Deselecting all skills');
  81. } else {
  82. // 否则全选
  83. $this->selectedSkills = $allSkillCodes;
  84. \Log::info('[ToggleAllSkills] Selecting all skills', ['newSelection' => $this->selectedSkills]);
  85. }
  86. }
  87. public function executeGenerate(): void
  88. {
  89. if ($this->isGenerating) {
  90. return;
  91. }
  92. if (!$this->generateKpCode) {
  93. Notification::make()->title('请选择知识点')->danger()->send();
  94. return;
  95. }
  96. $skillsOptions = $this->skillsOptions;
  97. $skillsCount = count($skillsOptions);
  98. if ($skillsCount > 0 && empty($this->selectedSkills)) {
  99. Notification::make()->title('请选择至少一个技能')->danger()->send();
  100. return;
  101. }
  102. $this->isGenerating = true;
  103. $this->currentTaskId = null;
  104. try {
  105. $service = app(QuestionBankService::class);
  106. $callbackUrl = route('api.questions.callback');
  107. \Log::info("[QuestionGen] 开始生成,callback URL: " . $callbackUrl);
  108. $result = $service->generateIntelligentQuestions([
  109. 'kp_code' => $this->generateKpCode,
  110. 'skills' => $this->selectedSkills,
  111. 'count' => $this->questionCount,
  112. 'difficulty' => $this->generateDifficulty,
  113. 'type' => $this->generateType,
  114. 'prompt_template' => $this->promptTemplate ?? null
  115. ], $callbackUrl);
  116. if ($result['success'] ?? false) {
  117. $this->currentTaskId = $result['task_id'] ?? null;
  118. \Log::info("[QuestionGen] 任务已创建: {$this->currentTaskId},启动前端监控");
  119. Notification::make()
  120. ->title('正在生成题目')
  121. ->body("任务 ID: {$this->currentTaskId}\nAI正在后台生成,预计需要30-60秒...")
  122. ->info()
  123. ->persistent()
  124. ->send();
  125. $this->dispatch('start-async-task-monitoring');
  126. } else {
  127. $this->isGenerating = false;
  128. Notification::make()
  129. ->title('创建任务失败')
  130. ->body($result['message'] ?? '未知错误')
  131. ->danger()
  132. ->send();
  133. }
  134. } catch (\Exception $e) {
  135. $this->isGenerating = false;
  136. \Log::error("[QuestionGen] 生成异常: " . $e->getMessage());
  137. Notification::make()
  138. ->title('生成异常')
  139. ->body($e->getMessage())
  140. ->danger()
  141. ->send();
  142. }
  143. }
  144. public function forceCloseStatusBar(string $taskId): void
  145. {
  146. \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
  147. $this->isGenerating = false;
  148. $this->currentTaskId = null;
  149. Notification::make()
  150. ->title('⚠️ 任务超时')
  151. ->body('生成任务可能已完成,请刷新页面查看')
  152. ->warning()
  153. ->persistent()
  154. ->send();
  155. }
  156. }