ai-refactor(flow): 调整抽象流程节点实现和依赖路径

- 修改 AbstractProcessNode 中 ProcessContext 的命名空间引用为 app\flow\context\ProcessContext
- 引入 app\flow\vo\CanHandleResult 用于节点处理结果表示
- 更新前置策略执行后对成功状态的判断,改为调用 isSuccess() 方法
- 增加日志记录细节,便于调试策略执行中断时的错误信息
- 优化代码注释,提升代码可读性和维护性
This commit is contained in:
zimoyin
2026-03-11 00:48:10 +08:00
parent d303f3501f
commit e040fccba6
45 changed files with 819 additions and 2718 deletions
+12 -14
View File
@@ -4,7 +4,7 @@ namespace app\flow\strategies;
use app\flow\config\VoiceTemplatesConfig;
use app\flow\exception\IllegalUsageException;
use app\flow\ProcessContext;
use app\flow\context\ProcessContext;
use app\flow\nodes\ProcessNodeInterface;
use app\utils\Logger;
@@ -31,22 +31,21 @@ class VoiceGenerationStrategy extends AbstractStrategy
protected function doExecute(ProcessContext $context, ProcessNodeInterface $node): ProcessContext
{
// 如果存在流程中自定义语音,就直接输出
if (!empty($context->voiceMessage)) {
if (!empty($context->getVoice()->message)) {
Logger::warn(
"流程中存在自定义语音或存在多次设置语音,语音应该在 VoiceGenerationStrategy 策略中配置,否则不能拦截与自定义配置",
new IllegalUsageException("In the existing process, there is custom voice, which should be configured in the VoiceGenerationStrategy strategy; otherwise, it cannot be intercepted and customized")
);
$context->setVoice($context->voiceMessage);
return $context;
return $context->builder()->voiceMessage($context->getVoice()->message)->build();
}
// 如果已经有错误,生成错误语音
// 否则生成正常流程语音
$voice = !$context->success ? $this->generateErrorVoice($context) : $this->generateNormalVoice($context, $node);
$voice = !$context->isSuccess() ? $this->generateErrorVoice($context) : $this->generateNormalVoice($context, $node);
// 应用语音模板
foreach ($context->voiceTemplateParams as $key => $val) {
foreach ($context->getVoice()->templateParams as $key => $val) {
$replaceVal = match (true) {
is_array($val) => implode(',', $val),
is_bool($val) => $val ? 'true' : 'false',
@@ -56,9 +55,8 @@ class VoiceGenerationStrategy extends AbstractStrategy
$voice = str_replace('{' . $key . '}', $replaceVal, $voice);
}
Logger::debug("应用语音模板后的内容: {$voice}");
$context->setVoice($voice);
return $context;
return $context->builder()->voiceMessage($voice)->build();
}
/**
@@ -68,7 +66,7 @@ class VoiceGenerationStrategy extends AbstractStrategy
{
$stepCode = $node->getCode();
$stepName = $node->getName();
$processType = $context->processType;
$processType = $context->getProcessType();
// 根据流程类型选择模板
$templateKey = $this->getTemplateKey($processType, $stepCode, $context);
@@ -94,12 +92,12 @@ class VoiceGenerationStrategy extends AbstractStrategy
*/
protected function generateErrorVoice(ProcessContext $context): string
{
$errorMessage = $context->errorMessage;
$errorMessage = $context->getVoice()->errorMessage;
$errorTemplates = $this->voiceTemplatesConfig->voiceMessage;
$errorMsg = $errorTemplates[$errorMessage->name] ?? $errorMessage->value;
if (empty($errorMsg)) {
$errorMsg = $context->voiceMessage;
$errorMsg = $context->getVoice()->message;
Logger::debug("错误信息配置未命中,使用自定义语音: {$errorMsg}");
} else {
Logger::debug("错误信息配置命中: {$errorMsg}");
@@ -129,7 +127,7 @@ class VoiceGenerationStrategy extends AbstractStrategy
protected function getTemplateKey(string $processType, string $stepCode, ProcessContext $context): string
{
// 晨洗流程中的清洗/机洗步骤
if ($context->needMorningWash) {
if ($context->getMorningWash()->needMorningWash) {
return 'morning_wash';
}
@@ -144,11 +142,11 @@ class VoiceGenerationStrategy extends AbstractStrategy
{
$reminds = [];
if ($context->needLeakTestRemind) {
if ($context->needLeakTestRemind()) {
$reminds[] = ',清洗开始前,请测漏';
}
if ($context->needStorageRemind) {
if ($context->needStorageRemind()) {
$reminds[] = ',未登记取出';
}