Files
PaperClipAI/packages/adapters/hermes/gateway-ui-parser.cjs
T
neerajdad123-byte 70c86d2c73 fix(hermes): strip ANSI escape codes from terminal output in UI parsers (#8731)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Hermes adapter produces terminal output with ANSI color codes on
stdout
> - These escape sequences flow through the UI parsers untouched and
render as raw garbage text
> - This PR adds ANSI stripping at the entry point of all four Hermes
parse-stdout entry points
   > - The same regex is already proven in claude-local adapter
   > - The benefit is clean, readable terminal output for Hermes agents

   ## Linked Issues or Issue Description

   No existing issue. This is a bug report:

   **What happened**

Hermes terminal output displayed ANSI color codes as raw text in the
Paperclip UI, making agent output unreadable.

   **Expected behavior**

Terminal output in run transcripts should be clean text without
invisible control characters.

   **Steps to reproduce**

   1. Connect a Hermes agent to Paperclip
   2. Create and assign a task to the agent
   3. View the run transcript — ANSI escape codes appear as raw garbage

   **Paperclip version or commit**

   e6407b322 (upstream master)

   **Deployment mode**

   local_trusted

   ## What Changed

- Added `stripAnsi()` function using the same regex pattern from
claude-local adapter (quota.ts) — strips CSI and
 OSC sequences
- Applied at entry point of `parseHermesStdoutLine` in hermes_local (TS
+ CJS)
- Applied at entry point of `parseHermesGatewayStdoutLine` in
hermes_gateway (TS + CJS)
- CJS files keep the function inline since the dynamic parser sandbox
has no module loader
   - 5 files changed, +123/-8 lines

   ## Verification

- Smoke tested with real ANSI patterns from Hermes output — all samples
pass
- `pnpm --filter @paperclipai/hermes-paperclip-adapter exec vitest run
src/ui/parse-stdout.test.ts` — 9 passed
- TypeScript compiles clean for both hermes and hermes-gateway packages
- Adapter tests pass (5/6, 1 pre-existing Windows CI failure unrelated)
- Live tested on running Paperclip instance — ANSI codes no longer
appear in transcripts

   ## Risks

Low risk. Only affects Hermes parser output. Regex already proven in
claude-local adapter. No logic changes to parse
 behavior — only strips invisible control characters before parsing.

   ## Model Used

   DeepSeek V4 Pro — reasoning mode, tool use

   ## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue
 in-PR following the relevant issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` /
 `github.com/paperclipai/paperclip` URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id
 or instance-derived details
   - [x] I have run tests locally and they pass
   - [x] I have added or updated tests where applicable
   - [ ] I have updated relevant documentation to reflect my changes
   - [x] I have considered and documented any risks above
   - [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-07-06 12:07:55 -07:00

57 lines
1.7 KiB
JavaScript

"use strict";
function stripAnsi(text) {
return text
.replace(/\u001B\][^\u0007]*(?:\u0007|\u001B\\)/g, "")
.replace(/\u001B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "");
}
function safeJsonParse(text) {
try {
return JSON.parse(text);
} catch {
return null;
}
}
function asRecord(value) {
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
return value;
}
function asString(value) {
return typeof value === "string" ? value : "";
}
function parseStdoutLine(line, ts) {
const cleaned = stripAnsi(line);
const trimmed = cleaned.trim();
if (!trimmed) return [];
const eventMatch = trimmed.match(/^\[hermes-gateway:event\]\s+run=([^\s]+)\s+event=([^\s]+)\s+data=(.*)$/s);
if (eventMatch) {
const eventName = eventMatch[2];
const data = asRecord(safeJsonParse(eventMatch[3]));
if (eventName === "message.delta") {
const delta = asString(data && data.delta) || asString(data && data.text_delta);
return delta ? [{ kind: "assistant", ts, text: stripAnsi(delta), delta: true }] : [];
}
if (eventName === "run.failed" || eventName === "run.error") {
const message = asString(data && data.error) || asString(data && data.message) || "Hermes run failed";
return [{ kind: "stderr", ts, text: message }];
}
if (eventName === "reasoning.available") {
return [{ kind: "thinking", ts, text: "Hermes reasoning available" }];
}
return [{ kind: "system", ts, text: `Hermes event: ${eventName}` }];
}
if (trimmed.startsWith("[hermes-gateway]")) {
return [{ kind: "system", ts, text: trimmed.replace(/^\[hermes-gateway\]\s*/, "") }];
}
return [{ kind: "stdout", ts, text: cleaned }];
}
module.exports = { parseStdoutLine };