QuestionGeneration.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 = 21;
  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. #[Computed(cache: false)]
  55. public function promptOptions(): array
  56. {
  57. $service = app(\App\Services\QuestionServiceApi::class);
  58. $prompts = $service->listPrompts(type: 'question_generation', active: 'yes');
  59. // 只展示激活的题目生成模板
  60. $options = [];
  61. foreach ($prompts as $prompt) {
  62. $label = $prompt['template_name'];
  63. if (!empty($prompt['description'])) {
  64. $label .= ' - ' . (is_string($prompt['description']) ? $prompt['description'] : json_encode($prompt['description']));
  65. }
  66. $options[$prompt['template_name']] = $label;
  67. }
  68. // 自动选择第一个激活模板
  69. if (!$this->promptTemplate && !empty($options)) {
  70. $this->promptTemplate = array_key_first($options);
  71. }
  72. return $options;
  73. }
  74. public function updatedGenerateKpCode(): void
  75. {
  76. // 选择新知识点时清空技能选择
  77. $this->selectedSkills = [];
  78. }
  79. public function toggleAllSkills(): void
  80. {
  81. $skillsOptions = $this->skillsOptions;
  82. $skillsCount = count($skillsOptions);
  83. \Log::info('[ToggleAllSkills] Called', [
  84. 'skillsCount' => $skillsCount,
  85. 'selectedSkillsCount' => count($this->selectedSkills),
  86. 'skillsOptions' => array_column($skillsOptions, 'code'),
  87. 'selectedSkills' => $this->selectedSkills
  88. ]);
  89. if ($skillsCount === 0) {
  90. \Log::info('[ToggleAllSkills] No skills available, returning');
  91. return;
  92. }
  93. // 获取所有技能的编码列表
  94. $allSkillCodes = array_values(array_unique(array_column($skillsOptions, 'code')));
  95. // 检查当前选中的技能是否等于全部技能
  96. if (count($this->selectedSkills) === $skillsCount &&
  97. count(array_intersect($this->selectedSkills, $allSkillCodes)) === $skillsCount) {
  98. // 如果已全选,则清空
  99. $this->selectedSkills = [];
  100. \Log::info('[ToggleAllSkills] Deselecting all skills');
  101. } else {
  102. // 否则全选
  103. $this->selectedSkills = $allSkillCodes;
  104. \Log::info('[ToggleAllSkills] Selecting all skills', ['newSelection' => $this->selectedSkills]);
  105. }
  106. }
  107. public function executeGenerate(): void
  108. {
  109. if ($this->isGenerating) {
  110. return;
  111. }
  112. if (!$this->generateKpCode) {
  113. Notification::make()->title('请选择知识点')->danger()->send();
  114. return;
  115. }
  116. $skillsOptions = $this->skillsOptions;
  117. $skillsCount = count($skillsOptions);
  118. if ($skillsCount > 0 && empty($this->selectedSkills)) {
  119. Notification::make()->title('请选择至少一个技能')->danger()->send();
  120. return;
  121. }
  122. $this->isGenerating = true;
  123. $this->currentTaskId = null;
  124. try {
  125. $service = app(QuestionBankService::class);
  126. $callbackUrl = route('api.questions.callback');
  127. \Log::info("[QuestionGen] 开始生成,callback URL: " . $callbackUrl);
  128. $result = $service->generateIntelligentQuestions([
  129. 'kp_code' => $this->generateKpCode,
  130. 'skills' => $this->selectedSkills,
  131. 'count' => $this->questionCount,
  132. 'difficulty' => $this->generateDifficulty,
  133. 'type' => $this->generateType,
  134. 'prompt_template' => $this->promptTemplate ?? null
  135. ], $callbackUrl);
  136. if ($result['success'] ?? false) {
  137. $this->currentTaskId = $result['task_id'] ?? null;
  138. \Log::info("[QuestionGen] 任务已创建: {$this->currentTaskId},启动前端监控");
  139. Notification::make()
  140. ->title('正在生成题目')
  141. ->body("任务 ID: {$this->currentTaskId}\nAI正在后台生成,预计需要30-60秒...")
  142. ->info()
  143. ->persistent()
  144. ->send();
  145. $this->dispatch('start-async-task-monitoring');
  146. } else {
  147. $this->isGenerating = false;
  148. Notification::make()
  149. ->title('创建任务失败')
  150. ->body($result['message'] ?? '未知错误')
  151. ->danger()
  152. ->send();
  153. }
  154. } catch (\Exception $e) {
  155. $this->isGenerating = false;
  156. \Log::error("[QuestionGen] 生成异常: " . $e->getMessage());
  157. Notification::make()
  158. ->title('生成异常')
  159. ->body($e->getMessage())
  160. ->danger()
  161. ->send();
  162. }
  163. }
  164. public function forceCloseStatusBar(string $taskId): void
  165. {
  166. \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
  167. $this->isGenerating = false;
  168. $this->currentTaskId = null;
  169. Notification::make()
  170. ->title('⚠️ 任务超时')
  171. ->body('生成任务可能已完成,请刷新页面查看')
  172. ->warning()
  173. ->persistent()
  174. ->send();
  175. }
  176. }