Skip to content

Commit af728db

Browse files
committed
update prompt
1 parent c0a4458 commit af728db

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/Service/Agent/Intent/ActionIntent.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,23 @@ public function getMessage(): string
4646
$hint.= "\n";
4747
$hint.= '--' . "\n";
4848
$hint.= <<<PHP
49+
Action: [NAME]
4950
<?php
5051
5152
use Fusio\Worker;
5253
use Fusio\Engine;
5354
use Psr\Log\LoggerInterface;
5455
55-
/**
56-
* Action: [NAME]
57-
*/
5856
return function(Worker\ExecuteRequest \$request, Worker\ExecuteContext \$context, Engine\ConnectorInterface \$connector, Engine\Response\FactoryInterface \$response, Engine\DispatcherInterface \$dispatcher, LoggerInterface \$logger) {
5957
60-
// [INSERT_CODE_HERE]
58+
[CODE]
6159
6260
};
6361
6462
PHP;
6563
$hint.= '--' . "\n";
6664
$hint.= "\n";
67-
$hint.= 'Replace "// [INSERT_CODE_HERE]" with the code which you have generated.' . "\n";
65+
$hint.= 'Replace "[CODE]" with the code which you have generated.' . "\n";
6866
$hint.= 'Replace "[NAME]" with a short and precise name as lower case and separated by hyphens which summarizes the business logic of the user message.' . "\n";
6967
$hint.= "\n";
7068
$hint.= 'If the business logic wants to interact with an external service i.e. a database or remote HTTP endpoint, then you can use the getConnection method at the connector argument to access those external services.' . "\n";

src/Service/Agent/Serializer/ActionResultSerializer.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,54 @@ public function serialize(ResultInterface $result): AgentMessage
4343
throw new InvalidArgumentException('Expect a text result got: ' . $result::class);
4444
}
4545

46-
$content = trim($result->getContent(), ' -');
47-
48-
// try to extract the action name
49-
preg_match('/\* Action: ([A-Za-z0-9-]+)/im', $content, $matches);
50-
51-
$name = $matches[1] ?? null;
52-
if (empty($name)) {
53-
$name = 'Action-' . substr(sha1($content), 0, 8);
54-
}
46+
$content = trim($result->getContent());
47+
$name = $this->extractName($content);
48+
$code = $this->extractCode($content);
5549

5650
$object = new AgentMessageObject();
5751
$object->setType('object');
5852
$object->setPayload([
5953
'name' => $name,
6054
'class' => ClassName::serialize(WorkerPHPLocal::class),
6155
'config' => [
62-
'code' => $content,
56+
'code' => $code,
6357
],
6458
]);
6559

6660
return $object;
6761
}
62+
63+
private function extractName(string $content): string
64+
{
65+
preg_match('/\* Action: ([A-Za-z0-9-]+)/im', $content, $matches);
66+
67+
$name = $matches[1] ?? null;
68+
if (empty($name)) {
69+
$name = 'Action-' . substr(sha1($content), 0, 8);
70+
}
71+
72+
return $name;
73+
}
74+
75+
private function extractCode(string $content): string
76+
{
77+
$capture = false;
78+
$code = '';
79+
$lines = explode("\n", $content);
80+
foreach ($lines as $line) {
81+
if ($line === '<?php') {
82+
$capture = true;
83+
}
84+
85+
if ($capture) {
86+
$code.= $line . "\n";
87+
}
88+
89+
if ($line === '};') {
90+
break;
91+
}
92+
}
93+
94+
return $code;
95+
}
6896
}

0 commit comments

Comments
 (0)