Skip to content

Commit 13c4004

Browse files
committed
Bar: added support for AI agents [WIP]
1 parent 292e9e0 commit 13c4004

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

src/Tracy/Bar/Bar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ public function renderLoader(DeferredContent $defer): void
6363
}
6464

6565

66+
/**
67+
* Renders debug bar as plain text (markdown).
68+
*/
69+
public function renderAsText(): void
70+
{
71+
$time = microtime(true) - Debugger::$time;
72+
$memory = memory_get_peak_usage() / 1_000_000;
73+
$warningsPanel = $this->panels['Tracy:warnings'] ?? null;
74+
require __DIR__ . '/dist/markdown.phtml';
75+
}
76+
77+
6678
/**
6779
* Renders debug bar.
6880
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{*
2+
* Tracy Bar rendered as HTML comment (for AI agents).
3+
*
4+
* @var float $time
5+
* @var float $memory
6+
* @var ?Tracy\DefaultBarPanel $warningsPanel
7+
*}
8+
9+
<!-- tracy
10+
Tracy Bar | {=number_format($time * 1000, 1)} ms | {=number_format($memory, 2)} MB
11+
{if $warningsPanel instanceof \Tracy\DefaultBarPanel && $warningsPanel->data}
12+
13+
## Warnings
14+
15+
{foreach $warningsPanel->data as $key => $count}
16+
{do [$file, $line, $message] = explode('|', $key, 3)}
17+
- {=$message . ' in ' . $file . ':' . $line . ($count > 1 ? " (×" . $count . ')' : '')}
18+
{/foreach}
19+
{/if}
20+
-->

src/Tracy/Bar/dist/markdown.phtml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
?>
4+
<?php /**
5+
* Tracy Bar rendered as HTML comment (for AI agents).
6+
*
7+
* @var float $time
8+
* @var float $memory
9+
* @var ?Tracy\DefaultBarPanel $warningsPanel
10+
*/ ?>
11+
12+
<!-- tracy
13+
Tracy Bar | <?= number_format($time * 1000, 1) ?> ms | <?= number_format($memory, 2) ?> MB
14+
<?php if ($warningsPanel instanceof \Tracy\DefaultBarPanel && $warningsPanel->data): ?>
15+
16+
## Warnings
17+
18+
<?php foreach ($warningsPanel->data as $key => $count): ?>
19+
<?php [$file, $line, $message] = explode('|', $key, 3) ?>
20+
- <?= $message . ' in ' . $file . ':' . $line . ($count > 1 ? "" . $count . ')' : '') ?>
21+
22+
<?php endforeach ?>
23+
<?php endif ?>
24+
-->

src/Tracy/Debugger/DevelopmentStrategy.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ public function renderBar(): void
135135
ini_set('display_errors', '1');
136136
}
137137

138-
$this->bar->render($this->defer);
138+
if (Helpers::isAgent()) {
139+
$this->bar->renderAsText();
140+
} else {
141+
$this->bar->render($this->defer);
142+
}
139143
}
140144
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* Test: Tracy\Bar::renderAsText()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Tester\Assert;
10+
use Tracy\Debugger;
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
test('info line with time and memory', function () {
16+
Debugger::$productionMode = false;
17+
Debugger::$time = microtime(true) - 0.05; // simulate 50 ms
18+
$bar = Debugger::getBar();
19+
20+
ob_start();
21+
$bar->renderAsText();
22+
$output = ob_get_clean();
23+
24+
Assert::match('
25+
<!-- tracy
26+
Tracy Bar | %a% ms | %a% MB
27+
-->
28+
', $output);
29+
});
30+
31+
32+
test('warnings rendering', function () {
33+
$bar = new Tracy\Bar;
34+
$panel = new Tracy\DefaultBarPanel('warnings');
35+
$panel->data = [
36+
'/app/foo.php|10|Notice: Undefined variable $x' => 3,
37+
'/app/bar.php|25|Deprecated: implode()' => 1,
38+
];
39+
$bar->addPanel($panel, 'Tracy:warnings');
40+
41+
ob_start();
42+
$bar->renderAsText();
43+
$output = ob_get_clean();
44+
45+
Assert::match('
46+
<!-- tracy
47+
Tracy Bar | %a% ms | %a% MB
48+
49+
## Warnings
50+
51+
- Notice: Undefined variable $x in /app/foo.php:10 (' . "\u{00d7}" . '3)
52+
- Deprecated: implode() in /app/bar.php:25
53+
-->
54+
', $output);
55+
});
56+
57+
58+
test('no warnings section when empty', function () {
59+
$bar = new Tracy\Bar;
60+
$panel = new Tracy\DefaultBarPanel('warnings');
61+
$bar->addPanel($panel, 'Tracy:warnings');
62+
63+
ob_start();
64+
$bar->renderAsText();
65+
$output = ob_get_clean();
66+
67+
Assert::match('
68+
<!-- tracy
69+
Tracy Bar | %a% ms | %a% MB
70+
-->
71+
', $output);
72+
});

0 commit comments

Comments
 (0)