feat: prompt from station

This commit is contained in:
2026-04-06 17:00:12 +08:00
parent d19b770ef4
commit ad0add4500
12 changed files with 29 additions and 300 deletions

View File

@@ -164,7 +164,7 @@ class TerminalResource extends Resource
->schema([
Forms\Components\Grid::make(3)
->schema([
\AbdelhamidErrahmouni\FilamentMonacoEditor\MonacoEditor::make('prompt.prompt_template')
\AbdelhamidErrahmouni\FilamentMonacoEditor\MonacoEditor::make('prompt_template')
->label('提示词模板')
->language('markdown')
->fontSize('14px')

View File

@@ -18,26 +18,4 @@ class CreateTerminal extends CreateRecord
{
return '终端创建成功';
}
protected function mutateFormDataBeforeCreate(array $data): array
{
// 提取提示词数据,稍后单独处理
$this->promptData = $data['prompt'] ?? null;
unset($data['prompt']);
return $data;
}
protected function afterCreate(): void
{
// 创建终端后,创建或更新提示词
if (!empty($this->promptData['prompt_template'])) {
$this->record->prompt()->create([
'prompt_template' => $this->promptData['prompt_template'],
'variables' => $this->promptData['variables'] ?? [],
]);
}
}
private ?array $promptData = null;
}

View File

@@ -29,45 +29,4 @@ class EditTerminal extends EditRecord
{
return '终端更新成功';
}
protected function mutateFormDataBeforeFill(array $data): array
{
// 加载提示词数据到表单
if ($this->record->prompt) {
$data['prompt'] = [
'prompt_template' => $this->record->prompt->prompt_template,
'variables' => $this->record->prompt->variables,
];
}
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
// 提取提示词数据,稍后单独处理
$this->promptData = $data['prompt'] ?? null;
unset($data['prompt']);
return $data;
}
protected function afterSave(): void
{
// 更新或创建提示词
if (!empty($this->promptData['prompt_template'])) {
$this->record->prompt()->updateOrCreate(
['terminal_id' => $this->record->id],
[
'prompt_template' => $this->promptData['prompt_template'],
'variables' => $this->promptData['variables'] ?? [],
]
);
} elseif ($this->record->prompt) {
// 如果提示词模板为空,删除现有提示词
$this->record->prompt()->delete();
}
}
private ?array $promptData = null;
}

View File

@@ -62,7 +62,7 @@ class ViewTerminal extends ViewRecord
Infolists\Components\Section::make('AI提示词配置')
->schema([
Infolists\Components\TextEntry::make('prompt.prompt_template')
Infolists\Components\TextEntry::make('prompt_template')
->label('提示词模板')
->markdown()
->placeholder('未配置提示词'),

View File

@@ -23,10 +23,9 @@ class TerminalApiController extends Controller
public function config(Request $request): JsonResponse
{
$terminal = $request->attributes->get('terminal');
$terminal->load(['prompt', 'station']);
$terminal->load('station');
// 返回原始提示词模板
$systemPrompt = $terminal->prompt?->prompt_template ?? '';
$systemPrompt = $terminal->prompt_template ?? '';
// 获取终端所属线站的已发布指引数量(含全局指引)
$guideCount = $this->getTerminalGuides($terminal)->count();

View File

@@ -26,6 +26,7 @@ class Terminal extends Model
'diagram_urls',
'scada_data_url',
'scada_tags_url',
'prompt_template',
'voice_wakeup_enabled',
'voice_wakeup_word',
'is_online',
@@ -57,16 +58,6 @@ class Terminal extends Model
return $this->belongsTo(Station::class);
}
/**
* 获取终端的提示词配置
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function prompt()
{
return $this->hasOne(TerminalPrompt::class);
}
/**
* 配置活动日志选项
*

View File

@@ -1,57 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class TerminalPrompt extends Model
{
use LogsActivity;
/**
* 可批量赋值的属性
*
* @var array<string>
*/
protected $fillable = [
'terminal_id',
'prompt_template',
'variables',
];
/**
* 属性类型转换
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'variables' => 'array',
];
}
/**
* 获取提示词所属的终端
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function terminal()
{
return $this->belongsTo(Terminal::class);
}
/**
* 配置活动日志选项
*
* @return \Spatie\Activitylog\LogOptions
*/
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['terminal_id', 'prompt_template', 'variables'])
->logOnlyDirty()
->setDescriptionForEvent(fn(string $eventName) => "终端提示词已{$eventName}");
}
}

View File

@@ -30,6 +30,7 @@ class TerminalFactory extends Factory
'ip_address' => fake()->localIpv4(),
'station_id' => null,
'diagram_urls' => [['title' => '组态', 'url' => fake()->url()]],
'prompt_template' => null,
'voice_wakeup_enabled' => false,
'voice_wakeup_word' => null,
'is_online' => fake()->boolean(70), // 70%概率在线

View File

@@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('terminal_prompts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('terminal_id')->comment('终端ID');
$table->text('prompt_template')->comment('提示词模板');
$table->json('variables')->nullable()->comment('变量配置');
$table->timestamps();
// 添加外键约束
$table->foreign('terminal_id')
->references('id')
->on('terminals')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('terminal_prompts');
}
};

View File

@@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('terminals', function (Blueprint $table) {
$table->text('prompt_template')->nullable()->comment('AI提示词模板');
});
}
public function down(): void
{
Schema::table('terminals', function (Blueprint $table) {
$table->dropColumn('prompt_template');
});
}
};

View File

@@ -5,7 +5,6 @@ namespace Database\Seeders;
use App\Models\KnowledgeBase;
use App\Models\Station;
use App\Models\Terminal;
use App\Models\TerminalPrompt;
use Illuminate\Database\Seeder;
class TerminalSeeder extends Seeder
@@ -27,10 +26,6 @@ class TerminalSeeder extends Seeder
'BL16U1' => '192.168.1.39',
];
$defaultPrompt = <<<'PROMPT'
你是{station_name}光束线的AI助手终端: {terminal_name} / {terminal_code})。当前时间是{time}。请根据用户{user}的问题,提供准确的光束线操作指导、实验支持和技术咨询。你可以回答关于光束线参数、实验流程、设备状态、安全规范等方面的问题。
PROMPT;
// 创建通用知识库(全局,不关联线站)
$this->command->info('创建通用知识库...');
KnowledgeBase::create(['name' => '通用知识库', 'description' => '全站通用的规章制度和管理文档', 'status' => 'active']);
@@ -53,6 +48,7 @@ PROMPT;
'code' => "SCREEN-{$beamline}",
'ip_address' => $ipAddress,
'station_id' => $station->id,
'prompt_template' => "你是{$beamline}光束线的AI助手终端: {terminal_name} / {terminal_code})。当前时间是{time}。请根据用户{user}的问题,提供准确的光束线操作指导、实验支持和技术咨询。",
'diagram_urls' => [
['title' => 'BL16U1', 'url' => 'https://ssrf.9z.work/scada/BL16U1.svg'],
['title' => 'BL16U1前端布局图', 'url' => 'https://ssrf.9z.work/scada/BL16U1-1.svg']
@@ -62,12 +58,6 @@ PROMPT;
? now()
: now()->subHours(rand(1, 24)),
]);
TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => $defaultPrompt,
'variables' => [],
]);
}
$this->command->info('线站/知识库/终端创建完成!');

View File

@@ -1,118 +0,0 @@
<?php
namespace Tests\Feature;
use App\Models\Terminal;
use App\Models\TerminalPrompt;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TerminalPromptTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
/** @test */
public function terminal_can_have_prompt()
{
$terminal = Terminal::factory()->create();
$prompt = TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => '你是一个智能助手,当前用户是 {user}。',
'variables' => ['user', 'station', 'time'],
]);
$this->assertInstanceOf(TerminalPrompt::class, $terminal->prompt);
$this->assertEquals($prompt->id, $terminal->prompt->id);
$this->assertEquals('你是一个智能助手,当前用户是 {user}。', $terminal->prompt->prompt_template);
}
/** @test */
public function prompt_belongs_to_terminal()
{
$terminal = Terminal::factory()->create();
$prompt = TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => '测试提示词',
'variables' => [],
]);
$this->assertInstanceOf(Terminal::class, $prompt->terminal);
$this->assertEquals($terminal->id, $prompt->terminal->id);
}
/** @test */
public function prompt_variables_are_cast_to_array()
{
$terminal = Terminal::factory()->create();
$prompt = TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => '测试提示词',
'variables' => ['user', 'station', 'time'],
]);
$this->assertIsArray($prompt->variables);
$this->assertEquals(['user', 'station', 'time'], $prompt->variables);
}
/** @test */
public function deleting_terminal_deletes_prompt()
{
$terminal = Terminal::factory()->create();
$prompt = TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => '测试提示词',
'variables' => [],
]);
$promptId = $prompt->id;
// 使用forceDelete来真正删除记录触发级联删除
$terminal->forceDelete();
$this->assertDatabaseMissing('terminal_prompts', ['id' => $promptId]);
}
/** @test */
public function prompt_template_can_be_empty()
{
$terminal = Terminal::factory()->create();
$prompt = TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => '',
'variables' => [],
]);
$this->assertEquals('', $prompt->prompt_template);
}
/** @test */
public function prompt_logs_activity()
{
$terminal = Terminal::factory()->create();
$prompt = TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => '测试提示词',
'variables' => [],
]);
$this->assertDatabaseHas('activity_log', [
'subject_type' => TerminalPrompt::class,
'subject_id' => $prompt->id,
]);
}
}