Skip to content

Commit 389bc33

Browse files
committed
Bar: added support for AI agents
1 parent 6a21ea5 commit 389bc33

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

src/Tracy/Bar/Bar.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function render(DeferredContent $defer): void
104104
require __DIR__ . '/dist/loader.phtml';
105105
}
106106
}
107+
108+
if (Helpers::isAgent() && Helpers::isHtmlMode() && !Helpers::isRedirect()) {
109+
$nonceAttr = ($nonce = Helpers::getNonce()) ? ' nonce="' . Helpers::escapeHtml($nonce) . '"' : '';
110+
echo '<script' . $nonceAttr . '>console.log(' . json_encode($this->renderAgent()) . ');</script>';
111+
}
107112
}
108113

109114

@@ -160,4 +165,22 @@ private function renderPanels(string $suffix = ''): array
160165
restore_error_handler();
161166
return $panels;
162167
}
168+
169+
170+
/**
171+
* Captures debug bar as plain text (markdown) for AI agents.
172+
*/
173+
public function renderAgent(): string
174+
{
175+
$time = number_format((microtime(true) - Debugger::$time) * 1000, 1);
176+
$memory = number_format(memory_get_peak_usage() / 1_000_000, 2);
177+
$parts = ["Tracy Bar | $time ms | $memory MB"];
178+
foreach ($this->panels as $panel) {
179+
if (method_exists($panel, 'getAgentInfo') && ($text = $panel->getAgentInfo()) !== null) {
180+
$parts[] = $text;
181+
}
182+
}
183+
184+
return implode("\n\n", $parts) . "\n";
185+
}
163186
}

src/Tracy/Bar/DefaultBarPanel.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ public function getPanel(): string
4242
}
4343
});
4444
}
45+
46+
47+
public function getAgentInfo(): ?string
48+
{
49+
return is_file(__DIR__ . "/dist/{$this->id}.agent.phtml")
50+
? Helpers::capture(function () {
51+
$data = $this->data;
52+
require __DIR__ . "/dist/{$this->id}.agent.phtml";
53+
})
54+
: null;
55+
}
4556
}

src/Tracy/Bar/IBarPanel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/**
1212
* Tracy Bar panel providing a tab label and optional panel content.
13+
* @method ?string getAgentInfo() Returns markdown summary for AI agents.
1314
*/
1415
interface IBarPanel
1516
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1); ?>
2+
<?php /** @var array<string, int|null> $data */ ?>
3+
<?php if (!$data) { return; } ?>
4+
## Warnings
5+
6+
<?php foreach ($data as $item => $count): ?>
7+
<?php [$file, $line, $message] = explode('|', $item, 3) ?>
8+
- <?= $message . ' in ' . $file . ':' . $line . ($count > 1 ? "" . $count . ')' : '') ?>
9+
10+
<?php endforeach ?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{varType array<string, int|null> $data}
2+
{exitIf !$data}
3+
## Warnings
4+
5+
{foreach $data as $item => $count}
6+
{do [$file, $line, $message] = explode('|', $item, 3)}
7+
- {=$message . ' in ' . $file . ':' . $line . ($count > 1 ? "" . $count . ')' : '')}
8+
{/foreach}

tests/Tracy/Bar.renderAgent().phpt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Test: Tracy\Bar::renderAgent()
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+
Assert::match(
20+
"Tracy Bar | %a% ms | %a% MB\n",
21+
$bar->renderAgent(),
22+
);
23+
});
24+
25+
26+
test('warnings rendering', function () {
27+
$bar = new Tracy\Bar;
28+
$panel = new Tracy\DefaultBarPanel('warnings');
29+
$panel->data = [
30+
'/app/foo.php|10|Notice: Undefined variable $x' => 3,
31+
'/app/bar.php|25|Deprecated: implode()' => 1,
32+
];
33+
$bar->addPanel($panel, 'Tracy:warnings');
34+
35+
Assert::match(
36+
"Tracy Bar | %a% ms | %a% MB\n\n## Warnings\n\n- Notice: Undefined variable \$x in /app/foo.php:10 (\u{00d7}3)\n- Deprecated: implode() in /app/bar.php:25\n\n",
37+
$bar->renderAgent(),
38+
);
39+
});
40+
41+
42+
test('no warnings section when empty', function () {
43+
$bar = new Tracy\Bar;
44+
$panel = new Tracy\DefaultBarPanel('warnings');
45+
$bar->addPanel($panel, 'Tracy:warnings');
46+
47+
Assert::match(
48+
"Tracy Bar | %a% ms | %a% MB\n",
49+
$bar->renderAgent(),
50+
);
51+
});

0 commit comments

Comments
 (0)