Skip to content

Commit 0d581fc

Browse files
committed
优化:组件升级支持
1 parent 7ee474e commit 0d581fc

File tree

24 files changed

+548
-57
lines changed

24 files changed

+548
-57
lines changed

app/Constant/AppConstant.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class AppConstant
66
{
77
const APP = 'cms';
88
const APP_NAME = 'ModStartCMS';
9-
const VERSION = '9.2.0';
9+
const VERSION = '9.3.0';
1010
}
1111

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Module\AigcBase\Biz;
4+
5+
use ModStart\Core\Dao\ModelUtil;
6+
use ModStart\Core\Util\SerializeUtil;
7+
use Module\AigcBase\Job\AigcWorkProcessJob;
8+
use Module\AigcBase\Model\AigcWork;
9+
use Module\Vendor\Type\JobStatus;
10+
11+
abstract class AbstractAigcWorkBiz
12+
{
13+
abstract public function name();
14+
15+
abstract public function title();
16+
17+
abstract public function run($work, $param = []);
18+
19+
public static function submit($param)
20+
{
21+
$data = [];
22+
$data['biz'] = static::NAME;
23+
$data['status'] = JobStatus::QUEUE;
24+
$data['param'] = SerializeUtil::jsonEncode($param);
25+
$data = ModelUtil::insert(AigcWork::class, $data);
26+
AigcWorkProcessJob::create($data['id']);
27+
}
28+
29+
public static function runWork($workId)
30+
{
31+
$work = ModelUtil::get(AigcWork::class, $workId);
32+
ModelUtil::decodeRecordJson($work, ['param', 'result']);
33+
$bizer = AigcWorkBiz::getByName($work['biz']);
34+
$bizer->run($work);
35+
}
36+
}

module/AigcBase/Biz/AigcTaskBiz.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Module\Vendor\Provider\BizTrait;
66

77
/**
8+
* 异步任务,比如API调用
89
* @method static AbstractAigcTaskBiz[] listAll()
910
*/
1011
class AigcTaskBiz
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Module\AigcBase\Biz;
4+
5+
use Module\Vendor\Provider\BizTrait;
6+
7+
/**
8+
* 同步任务,比如大模型调用
9+
* @method static AbstractAigcWorkBiz[] listAll()
10+
*/
11+
class AigcWorkBiz
12+
{
13+
use BizTrait;
14+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Module\AigcBase\Job;
4+
5+
use ModStart\Core\Dao\ModelUtil;
6+
use ModStart\Core\Exception\BizException;
7+
use ModStart\Core\Input\Response;
8+
use ModStart\Core\Job\BaseJob;
9+
use ModStart\Core\Util\LogUtil;
10+
use ModStart\Core\Util\StrUtil;
11+
use Module\AigcBase\Biz\AigcWorkBiz;
12+
use Module\AigcBase\Model\AigcWork;
13+
use Module\AigcBase\Util\AigcWorkUtil;
14+
use Module\Vendor\Type\JobStatus;
15+
16+
class AigcWorkProcessJob extends BaseJob
17+
{
18+
public $workId;
19+
20+
public static function create($workId, $delay = 0)
21+
{
22+
$job = new static();
23+
$job->workId = $workId;
24+
if ($delay) {
25+
$job->delay($delay);
26+
}
27+
$job->onQueue('Aigc');
28+
app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job);
29+
}
30+
31+
private function markFail($msg)
32+
{
33+
LogUtil::info('AigcWorkProcessJob.fail - ' . $this->workId, $msg);
34+
ModelUtil::update(AigcWork::class, $this->workId, [
35+
'status' => JobStatus::FAIL,
36+
'statusRemark' => StrUtil::mbLimit($msg, 100),
37+
]);
38+
}
39+
40+
public function handle()
41+
{
42+
LogUtil::info('AigcWorkProcessJob.start - ' . $this->workId);
43+
$work = ModelUtil::get(AigcWork::class, $this->workId);
44+
if (empty($work)) {
45+
LogUtil::info('AigcWorkProcessJob.empty - ' . $this->workId);
46+
return;
47+
}
48+
if ($work['status'] != JobStatus::QUEUE) {
49+
LogUtil::info('AigcWorkProcessJob.done - ' . $this->workId);
50+
return;
51+
}
52+
ModelUtil::update(AigcWork::class, $this->workId, [
53+
'status' => JobStatus::PROCESS,
54+
'startTime' => date('Y-m-d H:i:s'),
55+
]);
56+
ModelUtil::transactionCommit();
57+
ModelUtil::decodeRecordJson($work, ['param', 'result']);
58+
$bizer = AigcWorkBiz::getByName($work['biz']);
59+
if (empty($bizer)) {
60+
$this->markFail('bizer.empty');
61+
return;
62+
}
63+
64+
try {
65+
$ret = $bizer->run($work, []);
66+
} catch (BizException $e) {
67+
$ret = Response::generateError($e->getMessage());
68+
} catch (\Exception $e) {
69+
throw $e;
70+
}
71+
LogUtil::info('AigcWorkProcessJob.result - ' . $this->workId, $ret);
72+
if (Response::isError($ret)) {
73+
$this->markFail('callQueue.error - ' . $ret['msg']);
74+
return;
75+
}
76+
if (empty($ret['data']['result'])) {
77+
$ret['data']['result'] = [];
78+
}
79+
ModelUtil::update(AigcWork::class, $this->workId, [
80+
'status' => JobStatus::SUCCESS,
81+
'cost' => time() - strtotime($work['startTime']),
82+
]);
83+
AigcWorkUtil::updateResult($this->workId, $ret['data']['result']);
84+
LogUtil::info('AigcWorkProcessJob.success - ' . $this->workId, $ret);
85+
}
86+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class AigcWorkCreate extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
16+
Schema::create('aigc_work', function (Blueprint $table) {
17+
18+
$table->bigIncrements('id');
19+
$table->timestamps();
20+
21+
$table->string('biz', 20)->nullable()->comment('');
22+
23+
/** @see \Module\Vendor\Type\JobStatus */
24+
$table->tinyInteger('status')->nullable()->comment('');
25+
$table->string('statusRemark', 100)->nullable()->comment('');
26+
27+
$table->dateTime('startTime')->nullable()->comment('');
28+
$table->integer('cost')->nullable()->comment('');
29+
30+
$table->string('param', 400)->nullable()->comment('');
31+
$table->string('result', 400)->nullable()->comment('');
32+
33+
$table->index(['biz']);
34+
});
35+
36+
}
37+
38+
/**
39+
* Reverse the migrations.
40+
*
41+
* @return void
42+
*/
43+
public function down()
44+
{
45+
}
46+
}

module/AigcBase/Model/AigcWork.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace Module\AigcBase\Model;
5+
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class AigcWork extends Model
10+
{
11+
protected $table = 'aigc_work';
12+
}

module/AigcBase/Provider/AbstractAigcChatProvider.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66

77
use ModStart\Core\Exception\BizException;
8+
use ModStart\Core\Input\Response;
9+
use ModStart\Core\Util\HtmlUtil;
810
use Module\AigcBase\Type\AigcProviderType;
11+
use Module\AigcBase\Util\AigcKeyPoolUtil;
12+
use Module\Vendor\Markdown\MarkdownUtil;
913

1014
abstract class AbstractAigcChatProvider extends AbstractAigcProvider
1115
{
@@ -27,15 +31,59 @@ public function streamSupport()
2731
return false;
2832
}
2933

30-
protected function normalMsg($msg)
34+
protected function chatGetContentOrFail($sessionId, $msg, $option)
35+
{
36+
if ($msg['type'] != 'text') {
37+
BizException::throws('机器人不能识别消息,请稍后再试');
38+
}
39+
$content = $msg['content'];
40+
if (!$option['markdown']) {
41+
$content = HtmlUtil::text($msg['content']);
42+
}
43+
BizException::throwsIfEmpty('消息内容为空', $content);
44+
return $content;
45+
}
46+
47+
protected function chatResponse($sessionId, $content, $option)
48+
{
49+
if (!$option['markdown']) {
50+
$content = MarkdownUtil::convertToHtml($content);
51+
}
52+
return Response::generateSuccessData([
53+
'msg' => [
54+
'type' => 'text',
55+
'content' => $content,
56+
]
57+
]);
58+
}
59+
60+
protected function chatResponseError($sessionId, $option)
61+
{
62+
return Response::generateSuccessData([
63+
'msg' => [
64+
'type' => 'text',
65+
'content' => '机器人太忙啦,请稍后再试'
66+
]
67+
]);
68+
}
69+
70+
protected function chatPrepare($sessionId, $msg, $option)
3171
{
3272
if (!is_array($msg)) {
3373
$msg = [
3474
'type' => 'text',
3575
'content' => $msg,
3676
];
3777
}
38-
return $msg;
78+
$option = array_merge([
79+
// 是否是 Markdown 返回,默认为 false
80+
'markdown' => false,
81+
], $option);;
82+
return [
83+
$sessionId,
84+
$msg,
85+
$option,
86+
];
3987
}
4088

4189
/**

module/AigcBase/Provider/AbstractAigcProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
abstract class AbstractAigcProvider
1313
{
14+
public $defaultModel;
15+
1416
abstract public function name();
1517

1618
abstract public function title();
@@ -86,8 +88,12 @@ public function paramDisplay($item, $param = [])
8688
return $result;
8789
}
8890

89-
protected function keyPoolGetOrFail($model)
91+
protected function keyPoolGetOrFail($model = null)
9092
{
93+
if (null === $model) {
94+
$model = $this->defaultModel;
95+
}
96+
BizException::throwsIfEmpty('AI模型没有选择', $model);
9197
$keyPool = AigcKeyPoolUtil::randomByType(static::NAME, $model);
9298
BizException::throwsIfEmpty('AI没有配置(name=' . static::NAME . ',model=' . $model . ')', $keyPool);
9399
return $keyPool;

module/AigcBase/Provider/AigcProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class AigcProvider
2525
public static function getByFullName($providerModel)
2626
{
2727
list($providerName, $modelName) = self::parseProviderModel($providerModel);
28-
return self::getByName($providerName);
28+
$provider = self::getByName($providerName);
29+
$provider->defaultModel = $modelName;
30+
return $provider;
2931
}
3032

3133
public static function parseProviderModel($providerModel)

0 commit comments

Comments
 (0)