trypost/app/Services/Automation/ExpressionResolver.php
Paulo Castellano 448ae73389 Add expression autocomplete, side-panel editor, and richer HTTP fetch
Automations editor:
- {{ }} expression autocomplete in CodeMirror, scoped to the braces and
  graph-aware (suggests only what upstream nodes provide + variables + now);
  migrate the Generate prompt to CodeMirror so it shares the same completions
- Expandable editors: an expand button slides out a side-by-side panel
  (matching the sidebar card), with a minimize control; the inline field
  collapses to a hint while editing in the panel
- Hover-revealed editor toolbar (expand/copy) with styled tooltips so the
  buttons no longer obscure the text while reading
- Beta badge on the Automations sidebar item
- Delete a single connection with Backspace/Delete (edge selection)
- Re-key node config so switching between same-type nodes refreshes the form

HTTP fetch node — cover every JSON response shape:
- Top-level array, object map (items_path=*), array of primitives, and NDJSON
- Key-based dedup via item_key_path (seen-set, FIFO-capped) for feeds without
  dates; first poll records a baseline and emits nothing (date path too)

Fan-out test visibility:
- root_run_id links every forked branch back to the run that started a test,
  so the test panel aggregates all branches instead of one

Fix a few pre-existing type issues (ScheduleData import, padded minute,
optional created_at).
2026-06-12 11:31:58 -03:00

63 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Automation;
use Illuminate\Support\Carbon;
class ExpressionResolver
{
public function resolve(string $template, array $context): string
{
return preg_replace_callback(
'/\{\{\s*([a-zA-Z0-9_.]+)\s*\}\}/',
fn ($matches) => $this->resolveVariable($matches[1], $context),
$template,
) ?? $template;
}
/**
* Resolves `{{ ... }}` placeholders inside an already-decoded JSON structure
* (arrays/strings), resolving only string leaves. The caller json_encodes the
* result, so values are never string-interpolated into raw JSON — quotes,
* `&`, newlines etc. in the data can't corrupt the payload.
*
* @param array<string, mixed> $context
*/
public function resolveStructured(mixed $value, array $context): mixed
{
if (is_string($value)) {
return $this->resolve($value, $context);
}
if (is_array($value)) {
return array_map(fn ($item) => $this->resolveStructured($item, $context), $value);
}
return $value;
}
private function resolveVariable(string $path, array $context): string
{
if ($path === 'now') {
return Carbon::now()->toIso8601String();
}
if ($path === 'today') {
return Carbon::today()->toDateString();
}
$value = data_get($context, $path);
if ($value === null) {
return '';
}
if (is_scalar($value)) {
return (string) $value;
}
return json_encode($value);
}
}