mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-06 21:05:21 +02:00
## 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
200 lines
5.0 KiB
JavaScript
200 lines
5.0 KiB
JavaScript
"use strict";
|
|
|
|
function stripAnsi(text) {
|
|
return text
|
|
.replace(/\u001B\][^\u0007]*(?:\u0007|\u001B\\)/g, "")
|
|
.replace(/\u001B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "");
|
|
}
|
|
|
|
const TOOL_OUTPUT_PREFIX = "\u250a";
|
|
|
|
function stripKaomoji(text) {
|
|
return text.replace(/[(][^()]{2,20}[)]\s*/gu, "").trim();
|
|
}
|
|
|
|
function isAssistantToolLine(stripped) {
|
|
return /^\u250a\s*\u{1f4ac}/u.test(stripped);
|
|
}
|
|
|
|
function extractAssistantText(line) {
|
|
return line.replace(/^[\s\u250a]*\u{1f4ac}\s*/u, "").trim();
|
|
}
|
|
|
|
function parseToolCompletionLine(line) {
|
|
let cleaned = line.trim().replace(/^\[done\]\s*/, "");
|
|
if (!cleaned.startsWith(TOOL_OUTPUT_PREFIX)) return null;
|
|
|
|
cleaned = cleaned.slice(TOOL_OUTPUT_PREFIX.length);
|
|
cleaned = stripKaomoji(cleaned).trim();
|
|
|
|
const durationMatch = cleaned.match(/([\d.]+s)\s*(?:\([\d.]+s\))?\s*$/);
|
|
const duration = durationMatch ? durationMatch[1] : "";
|
|
const verbAndDetail = durationMatch
|
|
? cleaned.slice(0, cleaned.lastIndexOf(durationMatch[0])).trim()
|
|
: cleaned;
|
|
const detailWithoutEmoji = verbAndDetail.replace(/^\p{Emoji_Presentation}\s*/u, "");
|
|
|
|
const hasError = /\[(?:exit \d+|error|full)\]/.test(detailWithoutEmoji) ||
|
|
/\[error\]\s*$/.test(cleaned);
|
|
|
|
const parts = detailWithoutEmoji.match(/^(\S+)\s+(.*)/);
|
|
if (!parts) {
|
|
return { name: "tool", detail: detailWithoutEmoji, duration, hasError };
|
|
}
|
|
|
|
const verb = parts[1];
|
|
const detail = parts[2].trim();
|
|
const nameMap = {
|
|
"$": "shell",
|
|
exec: "shell",
|
|
terminal: "shell",
|
|
search: "search",
|
|
fetch: "fetch",
|
|
crawl: "crawl",
|
|
navigate: "browser",
|
|
snapshot: "browser",
|
|
click: "browser",
|
|
type: "browser",
|
|
scroll: "browser",
|
|
back: "browser",
|
|
press: "browser",
|
|
close: "browser",
|
|
images: "browser",
|
|
vision: "browser",
|
|
read: "read",
|
|
write: "write",
|
|
patch: "patch",
|
|
grep: "search",
|
|
find: "search",
|
|
plan: "plan",
|
|
recall: "recall",
|
|
proc: "process",
|
|
delegate: "delegate",
|
|
todo: "todo",
|
|
memory: "memory",
|
|
clarify: "clarify",
|
|
session_search: "recall",
|
|
code: "execute",
|
|
execute: "execute",
|
|
web_search: "search",
|
|
web_extract: "fetch",
|
|
browser_navigate: "browser",
|
|
browser_click: "browser",
|
|
browser_type: "browser",
|
|
browser_snapshot: "browser",
|
|
browser_vision: "browser",
|
|
browser_scroll: "browser",
|
|
browser_press: "browser",
|
|
browser_back: "browser",
|
|
browser_close: "browser",
|
|
browser_get_images: "browser",
|
|
read_file: "read",
|
|
write_file: "write_file",
|
|
search_files: "search",
|
|
patch_file: "patch",
|
|
execute_code: "execute",
|
|
};
|
|
|
|
return {
|
|
name: nameMap[verb.toLowerCase()] || verb,
|
|
detail,
|
|
duration,
|
|
hasError,
|
|
};
|
|
}
|
|
|
|
let toolCallCounter = 0;
|
|
|
|
function syntheticToolUseId() {
|
|
toolCallCounter += 1;
|
|
return `hermes-tool-${toolCallCounter}`;
|
|
}
|
|
|
|
function isThinkingLine(line) {
|
|
return (
|
|
line.includes("\u{1f4ad}") ||
|
|
line.startsWith("<thinking>") ||
|
|
line.startsWith("</thinking>") ||
|
|
line.startsWith("Thinking:")
|
|
);
|
|
}
|
|
|
|
function parseStdoutLine(line, ts) {
|
|
const trimmed = stripAnsi(line).trim();
|
|
if (!trimmed) return [];
|
|
|
|
if (trimmed.startsWith("[hermes]") || trimmed.startsWith("[paperclip]")) {
|
|
return [{ kind: "system", ts, text: trimmed }];
|
|
}
|
|
|
|
if (trimmed.startsWith("[tool]")) {
|
|
return [];
|
|
}
|
|
|
|
if (/^\[\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
|
|
return [{ kind: "stderr", ts, text: trimmed }];
|
|
}
|
|
|
|
if (/^\p{Emoji_Presentation}\s*(Completed|Running|Error)?\s*$/u.test(trimmed)) {
|
|
return [];
|
|
}
|
|
|
|
if (trimmed.startsWith("session_id:")) {
|
|
return [{ kind: "system", ts, text: trimmed }];
|
|
}
|
|
|
|
if (trimmed.includes(TOOL_OUTPUT_PREFIX)) {
|
|
if (isAssistantToolLine(trimmed)) {
|
|
return [{ kind: "assistant", ts, text: extractAssistantText(trimmed) }];
|
|
}
|
|
|
|
const toolInfo = parseToolCompletionLine(trimmed);
|
|
if (toolInfo) {
|
|
const id = syntheticToolUseId();
|
|
const detailText = toolInfo.duration
|
|
? `${toolInfo.detail} ${toolInfo.duration}`
|
|
: toolInfo.detail;
|
|
|
|
return [
|
|
{
|
|
kind: "tool_call",
|
|
ts,
|
|
name: toolInfo.name,
|
|
input: { detail: toolInfo.detail },
|
|
toolUseId: id,
|
|
},
|
|
{
|
|
kind: "tool_result",
|
|
ts,
|
|
toolUseId: id,
|
|
content: detailText,
|
|
isError: toolInfo.hasError,
|
|
},
|
|
];
|
|
}
|
|
|
|
const escapedPrefix = TOOL_OUTPUT_PREFIX.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
const stripped = trimmed
|
|
.replace(/^\[done\]\s*/, "")
|
|
.replace(new RegExp(`^${escapedPrefix}\\s*`), "")
|
|
.trim();
|
|
return [{ kind: "stdout", ts, text: stripped }];
|
|
}
|
|
|
|
if (isThinkingLine(trimmed)) {
|
|
return [{ kind: "thinking", ts, text: trimmed.replace(/^\u{1f4ad}\s*/u, "") }];
|
|
}
|
|
|
|
if (
|
|
trimmed.startsWith("Error:") ||
|
|
trimmed.startsWith("ERROR:") ||
|
|
trimmed.startsWith("Traceback")
|
|
) {
|
|
return [{ kind: "stderr", ts, text: trimmed }];
|
|
}
|
|
|
|
return [{ kind: "assistant", ts, text: trimmed }];
|
|
}
|
|
|
|
module.exports = { parseStdoutLine };
|