PHP code example of timmcleod / agent-workflows

1. Go to this page and download the library: Download timmcleod/agent-workflows library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

timmcleod / agent-workflows example snippets


// app/AgentWorkflows/ContractReview.php
class ContractReview extends Workflow
{
    public function build(WorkflowDefinition $workflow): WorkflowDefinition
    {
        return $workflow
            ->step(
                ExtractClausesAgent::class, 
                'Extract the key clauses: {{ contract }}'
            )
            ->step(
                RiskAnalysisAgent::class, 
                'Assess the risk of: {{ output:ExtractClausesAgent }}'
            )
            ->when(
                fn (WorkflowState $state) => $state->output(RiskAnalysisAgent::class)?->structured('riskScore') >= 7,
                then: EscalationAgent::class,
                thenPrompt: 'Draft an escalation memo covering: {{ output:RiskAnalysisAgent }}'
            )
            ->awaitHuman(
                reason: 'Final sign-off 

$run = ContractReview::start(['contract' => $text]);
// Each step runs as a queued job and is checkpointed as it completes. The run
// parks at the sign-off gate, through deploys, restarts, and weekends.

$run->resume(['approved' => true], by: $request->user());  // the manager answers on Tuesday
$run->retry();                                             // a step failed? re-run that step only;
                                                           // earlier steps keep their results and their token bill

// app/AgentWorkflows/AcquisitionReview.php
return $workflow
    // any invokable class is a step
    ->step(FetchFilings::class)

    // an agent: one checkpointed agentic turn
    ->step(SummarizeFilingsAgent::class)

    // fan out the analysis, merge when all finish
    ->parallel([
        FinancialAnalysisAgent::class,
        [LegalAnalysisAgent::class, 'Review for legal exposure: {{ output:SummarizeFilingsAgent }}'],
    ])

    // argue the thesis; a judge rules each round
    ->debate(
        ['bull' => BullCaseAgent::class, 'bear' => BearCaseAgent::class],
        judge: VerdictAgent::class,
        as: 'thesis'
    )

    // no consensus? dig deeper, else skip ahead
    ->when(
        fn (WorkflowState $state) => ! $state->get('steps.thesis.satisfied'),
        then: DeepDiveAgent::class
    )

    // revise the memo until it is ready
    ->evaluate(
        DraftMemoAgent::class,
        as: 'memo',
        until: fn (WorkflowState $state) => $state->get('steps.memo.structured.score', 0) >= 8
    )

    // park for a person, for hours or weeks
    ->awaitHuman(reason: 'Partner sign-off