2
2
<?php
3
3
4
4
use Symfony \Component \Console \Command \Command ;
5
+ use Symfony \Component \Console \Helper \Table ;
5
6
use Symfony \Component \Console \Input \InputInterface ;
6
- use Symfony \Component \Console \Output \ConsoleSectionOutput ;
7
- use Symfony \Component \Console \Output \OutputInterface ;
7
+ use Symfony \Component \Console \Output \ConsoleOutput ;
8
8
use Symfony \Component \Console \SingleCommandApplication ;
9
9
use Symfony \Component \Console \Style \SymfonyStyle ;
10
10
use Symfony \Component \Finder \Finder ;
@@ -15,7 +15,7 @@ require_once __DIR__.'/vendor/autoload.php';
15
15
16
16
$ app = (new SingleCommandApplication ('LLM Chain Example Runner ' ))
17
17
->setDescription ('Runs all LLM Chain examples in folder examples/ ' )
18
- ->setCode (function (InputInterface $ input , OutputInterface $ output ) {
18
+ ->setCode (function (InputInterface $ input , ConsoleOutput $ output ) {
19
19
$ io = new SymfonyStyle ($ input , $ output );
20
20
$ io ->title ('LLM Chain Examples ' );
21
21
@@ -25,44 +25,52 @@ $app = (new SingleCommandApplication('LLM Chain Example Runner'))
25
25
->sortByName ()
26
26
->files ();
27
27
28
- /** @var array{example: SplFileInfo, section: ConsoleSectionOutput, process: Process, state: 'running'|'finished' } $exampleRuns */
28
+ /** @var array{example: SplFileInfo, process: Process} $exampleRuns */
29
29
$ exampleRuns = [];
30
30
foreach ($ examples as $ example ) {
31
31
$ exampleRuns [] = [
32
32
'example ' => $ example ,
33
- 'section ' => $ section = $ output ->section (),
34
33
'process ' => $ process = new Process (['php ' , $ example ->getRealPath ()]),
35
- 'state ' => 'running ' ,
36
34
];
37
-
38
35
$ process ->start ();
39
- $ section ->writeln (sprintf ('Example %s: <comment>Running</comment> ' , $ example ->getFilename ()));
40
36
}
41
37
42
38
$ examplesRunning = fn () => array_reduce ($ exampleRuns , fn ($ running , $ example ) => $ running || $ example ['process ' ]->isRunning (), false );
43
39
$ examplesSuccessful = fn () => array_reduce ($ exampleRuns , fn ($ successful , $ example ) => $ successful && $ example ['process ' ]->isSuccessful (), true );
44
40
45
- while ($ examplesRunning ()) {
46
- sleep (1 );
41
+ $ section = $ output ->section ();
42
+ $ renderTable = function () use ($ exampleRuns , $ section ) {
43
+ $ section ->clear ();
44
+ $ table = new Table ($ section );
45
+ $ table ->setHeaders (['Example ' , 'State ' , 'Output ' ]);
47
46
foreach ($ exampleRuns as $ run ) {
48
- if ('running ' === $ run ['state ' ] && !$ run ['process ' ]->isRunning ()) {
49
- $ emptyOutput = 0 === strlen (trim ($ run ['process ' ]->getOutput ()));
50
- $ success = $ run ['process ' ]->isSuccessful () && !$ emptyOutput ;
51
- $ result = $ success ? '<info>Finished</info> '
47
+ /** @var SplFileInfo $example */
48
+ /** @var Process $process */
49
+ ['example ' => $ example , 'process ' => $ process ] = $ run ;
50
+
51
+ $ output = str_replace (PHP_EOL , ' ' , $ process ->getOutput ());
52
+ $ output = strlen ($ output ) <= 100 ? $ output : substr ($ output , 0 , 100 ).'... ' ;
53
+ $ emptyOutput = 0 === strlen (trim ($ output ));
54
+
55
+ $ state = 'Running ' ;
56
+ if ($ process ->isTerminated ()) {
57
+ $ success = $ process ->isSuccessful () && !$ emptyOutput ;
58
+ $ state = $ success ? '<info>Finished</info> '
52
59
: (1 === $ run ['process ' ]->getExitCode () || $ emptyOutput ? '<error>Failed</error> ' : '<comment>Skipped</comment> ' );
53
- $ run ['section ' ]->overwrite (sprintf ('Example %s: %s ' , $ run ['example ' ]->getFilename (), $ result ));
54
- $ run ['state ' ] = 'finished ' ;
55
- if ($ output ->isVerbose ()) {
56
- $ exampleOutput = $ emptyOutput ? 'Output was empty ' : $ run ['process ' ]->getOutput ();
57
- $ exampleOutput = strlen ($ exampleOutput ) <= 100 ? $ exampleOutput : substr ($ exampleOutput , 0 , 100 ).'... ' ;
58
- $ run ['section ' ]->writeln (
59
- sprintf ('<%s>%s</> ' , $ success ? 'fg=#999999 ' : 'fg=red ' , trim ($ exampleOutput ))
60
- );
61
- }
62
60
}
61
+
62
+ $ table ->addRow ([$ example ->getFilename (), $ state , $ output ]);
63
63
}
64
+ $ table ->render ();
65
+ };
66
+
67
+ while ($ examplesRunning ()) {
68
+ $ renderTable ();
69
+ sleep (1 );
64
70
}
65
71
72
+ $ renderTable ();
73
+
66
74
$ io ->newLine ();
67
75
if (!$ examplesSuccessful ()) {
68
76
$ io ->error ('Some examples failed or were skipped! ' );
0 commit comments