use PhpClaw\Claw;
use PhpClaw\Graph\Graph;
use PhpClaw\Graph\GraphState;
$writer = Claw::builder()
->systemPrompt('You write short product taglines.')
->build();
$reviewer = Claw::builder()
->systemPrompt('You are a strict marketing editor.')
->build();
$graph = (new Graph)
->addNode('write', function (GraphState $state) use ($writer) {
$draft = $writer->send(
"One tagline for {$state->get('product')}, tagline only."
)->text;
return $state
->set('draft', $draft)
->set('tries', $state->get('tries', 0) + 1);
})
->addNode('review', function (GraphState $state) use ($reviewer) {
$verdict = $reviewer->sendStructured(
"Approve if under 10 words: {$state->get('draft')}",
[
'type' => 'object',
'properties' => ['approved' => ['type' => 'boolean']],
'required' => ['approved'],
],
);
return $state->set('approved', $verdict->data['approved']);
})
->setEntry('write')
->addEdge('write', 'review')
->addConditionalEdge('review', fn (GraphState $state) =>
$state->get('approved') || $state->get('tries') >= 3
? Graph::END
: 'write'
);
$final = $graph->run(new GraphState(['product' => 'lavender soap']));
echo $final->get('draft');
use PhpClaw\Claw;
use PhpClaw\Exceptions\GraphInterruptedException;
use PhpClaw\Graph\Graph;
use PhpClaw\Graph\GraphState;
use PhpClaw\Memory\FileMemory;
$claw = Claw::builder()->build();
$graph = (new Graph)
->addNode('draft', fn (GraphState $state): GraphState => $state->set(
'post',
$claw->send('Write a one-line launch post for our lavender soap.')->text,
))
->addNode('publish', fn (GraphState $state): GraphState => $state->set('published', true))
->setEntry('draft')
->addEdge('draft', 'publish')
->addEdge('publish', Graph::END)
->setCheckpointer(new FileMemory(__DIR__.'/storage'))
->interruptBefore('publish');
try {
$graph->run(new GraphState, threadId: 'launch-post');
} catch (GraphInterruptedException $paused) {
echo $graph->getState('launch-post')->values['post'];
$final = $graph->resume('launch-post');
echo $final->get('published') ? 'Published' : 'Not published';
}