mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-21 16:27:18 +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
294 lines
10 KiB
TypeScript
294 lines
10 KiB
TypeScript
/**
|
|
* Strip ANSI escape sequences (CSI, OSC) from terminal text.
|
|
* Same pattern used in claude-local adapter quota.ts.
|
|
*/
|
|
function stripAnsi(text: string): string {
|
|
return text
|
|
.replace(/\u001B\][^\u0007]*(?:\u0007|\u001B\\)/g, "")
|
|
.replace(/\u001B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "");
|
|
}
|
|
|
|
/**
|
|
* Parse Hermes Agent stdout into TranscriptEntry objects for the Paperclip UI.
|
|
*
|
|
* Hermes CLI quiet-mode output patterns:
|
|
* Assistant: " ┊ 💬 {text}"
|
|
* Tool (TTY): " ┊ {emoji} {verb:9} {detail} {duration}"
|
|
* Tool (pipe): " [done] ┊ {emoji} {verb:9} {detail} {duration} ({total})"
|
|
* System: "[hermes] ..."
|
|
*
|
|
* We emit structured tool_call/tool_result pairs so Paperclip renders proper
|
|
* tool cards (with status icons, expand/collapse) instead of raw stdout blocks.
|
|
*/
|
|
|
|
import type { TranscriptEntry } from "@paperclipai/adapter-utils";
|
|
|
|
import { TOOL_OUTPUT_PREFIX } from "../shared/constants.js";
|
|
|
|
// ── Kaomoji / noise stripping ──────────────────────────────────────────────
|
|
|
|
/**
|
|
* Strip kawaii faces and decorative emoji from a tool summary line.
|
|
* Leaves meaningful emoji (💻 for terminal, 🔍 for search, etc.) intact
|
|
* by only stripping parenthesized kaomoji like (。◕‿◕。).
|
|
*/
|
|
function stripKaomoji(text: string): string {
|
|
// Strip parenthesized kaomoji faces: (。◕‿◕。), (★ω★), etc.
|
|
return text.replace(/[(][^()]{2,20}[)]\s*/gu, "").trim();
|
|
}
|
|
|
|
// ── Line classification ────────────────────────────────────────────────────
|
|
|
|
/** Check if a ┊ line is an assistant message (┊ 💬 ...). */
|
|
function isAssistantToolLine(stripped: string): boolean {
|
|
return /^┊\s*💬/.test(stripped);
|
|
}
|
|
|
|
/** Extract assistant text from a ┊ 💬 line. */
|
|
function extractAssistantText(line: string): string {
|
|
return line.replace(/^[\s┊]*💬\s*/, "").trim();
|
|
}
|
|
|
|
/**
|
|
* Parse a tool completion line into structured data.
|
|
*
|
|
* Handles both TTY and pipe formats:
|
|
* TTY: ┊ 💻 $ curl -s "..." 0.1s
|
|
* Pipe: [done] ┊ 💻 $ curl -s "..." 0.1s (0.5s)
|
|
*/
|
|
function parseToolCompletionLine(
|
|
line: string,
|
|
): { name: string; detail: string; duration: string; hasError: boolean } | null {
|
|
// Strip leading whitespace and [done] prefix
|
|
let cleaned = line.trim().replace(/^\[done\]\s*/, "");
|
|
|
|
// Must start with ┊
|
|
if (!cleaned.startsWith(TOOL_OUTPUT_PREFIX)) return null;
|
|
|
|
// Remove ┊ prefix and any leading kaomoji face
|
|
cleaned = cleaned.slice(TOOL_OUTPUT_PREFIX.length);
|
|
cleaned = stripKaomoji(cleaned).trim();
|
|
|
|
// Now format is: "{emoji} {verb:9} {detail} {duration}" or "{emoji} {verb:9} {detail} {duration} ({total})"
|
|
// Example: "💻 $ curl -s ..." or "🔍 search pattern 0.1s"
|
|
// The verb+detail are separated by whitespace, duration is at the end
|
|
|
|
// Match: emoji + verb + detail + duration
|
|
// Duration pattern: N.Ns (possibly followed by (N.Ns))
|
|
const durationMatch = cleaned.match(/([\d.]+s)\s*(?:\([\d.]+s\))?\s*$/);
|
|
const duration = durationMatch ? durationMatch[1] : "";
|
|
|
|
// Remove duration from the end to get verb + detail
|
|
let verbAndDetail = durationMatch
|
|
? cleaned.slice(0, cleaned.lastIndexOf(durationMatch[0])).trim()
|
|
: cleaned;
|
|
verbAndDetail = verbAndDetail.replace(/^\p{Emoji_Presentation}\s*/u, "");
|
|
|
|
// Check for error suffixes
|
|
const hasError = /\[(?:exit \d+|error|full)\]/.test(verbAndDetail) ||
|
|
/\[error\]\s*$/.test(cleaned);
|
|
|
|
// The first token (after emoji) is the verb, rest is detail
|
|
// Verbs are always a single word or symbol ($ for terminal)
|
|
const parts = verbAndDetail.match(/^(\S+)\s+(.*)/);
|
|
if (!parts) {
|
|
return { name: "tool", detail: verbAndDetail, duration, hasError };
|
|
}
|
|
|
|
const verb = parts[1];
|
|
const detail = parts[2].trim();
|
|
|
|
// Map Hermes verbs to readable tool names
|
|
const nameMap: Record<string, string> = {
|
|
"$": "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",
|
|
};
|
|
|
|
const name = nameMap[verb.toLowerCase()] || verb;
|
|
|
|
return { name, detail, duration, hasError };
|
|
}
|
|
|
|
// ── Synthetic tool ID generation ────────────────────────────────────────────
|
|
|
|
let toolCallCounter = 0;
|
|
|
|
/**
|
|
* Generate a synthetic toolUseId for pairing tool_call with tool_result.
|
|
* Paperclip uses this to match them in normalizeTranscript.
|
|
*/
|
|
function syntheticToolUseId(): string {
|
|
return `hermes-tool-${++toolCallCounter}`;
|
|
}
|
|
|
|
// ── Thinking detection ─────────────────────────────────────────────────────
|
|
|
|
function isThinkingLine(line: string): boolean {
|
|
return (
|
|
line.includes("💭") ||
|
|
line.startsWith("<thinking>") ||
|
|
line.startsWith("</thinking>") ||
|
|
line.startsWith("Thinking:")
|
|
);
|
|
}
|
|
|
|
// ── Main parser ────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Parse a single line of Hermes stdout into transcript entries.
|
|
*
|
|
* Emits structured tool_call/tool_result pairs (with synthetic IDs) so
|
|
* Paperclip renders proper tool cards with status icons and expand/collapse.
|
|
*
|
|
* @param line Raw stdout line from Hermes CLI
|
|
* @param ts ISO timestamp for the entry
|
|
* @returns Array of TranscriptEntry objects (may be empty)
|
|
*/
|
|
export function parseHermesStdoutLine(
|
|
line: string,
|
|
ts: string,
|
|
): TranscriptEntry[] {
|
|
const trimmed = stripAnsi(line).trim();
|
|
if (!trimmed) return [];
|
|
|
|
// ── System/adapter messages ────────────────────────────────────────────
|
|
if (trimmed.startsWith("[hermes]") || trimmed.startsWith("[paperclip]")) {
|
|
return [{ kind: "system", ts, text: trimmed }];
|
|
}
|
|
|
|
// ── Non-quiet mode tool start lines: [tool] (kaomoji) emoji verb ... ──
|
|
// These are redundant — the tool_call/tool_result pair arrives later from
|
|
// the ┊ completion line. Skip them to avoid duplicate entries.
|
|
if (trimmed.startsWith("[tool]")) {
|
|
return [];
|
|
}
|
|
|
|
// ── MCP / server init noise reclassified from stderr by wrappedOnLog ──
|
|
// Pattern: [2026-03-25T10:40:53.941Z] INFO: ...
|
|
// Emit as stderr so Paperclip groups them into the amber accordion.
|
|
if (/^\[\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
|
|
return [{ kind: "stderr", ts, text: trimmed }];
|
|
}
|
|
|
|
// ── Standalone spinner remnants: "💻 Completed", "💻\nCompleted", etc. ─
|
|
// These are non-quiet mode spinner frame leftovers — skip them.
|
|
if (/^\p{Emoji_Presentation}\s*(Completed|Running|Error)?\s*$/u.test(trimmed)) {
|
|
return [];
|
|
}
|
|
|
|
// ── Session info line ────────────────────────────────────────────────
|
|
if (trimmed.startsWith("session_id:")) {
|
|
return [{ kind: "system", ts, text: trimmed }];
|
|
}
|
|
|
|
// ── Quiet-mode tool/message lines (prefixed with ┊) ────────────────────
|
|
if (trimmed.includes(TOOL_OUTPUT_PREFIX)) {
|
|
// Assistant message: ┊ 💬 {text}
|
|
if (isAssistantToolLine(trimmed)) {
|
|
return [{ kind: "assistant", ts, text: extractAssistantText(trimmed) }];
|
|
}
|
|
|
|
// Tool completion: ┊ {emoji} {verb} {detail} {duration}
|
|
const toolInfo = parseToolCompletionLine(trimmed);
|
|
if (toolInfo) {
|
|
const id = syntheticToolUseId();
|
|
const detailText = toolInfo.duration
|
|
? `${toolInfo.detail} ${toolInfo.duration}`
|
|
: toolInfo.detail;
|
|
|
|
return [
|
|
{
|
|
kind: "tool_call" as const,
|
|
ts,
|
|
name: toolInfo.name,
|
|
input: { detail: toolInfo.detail },
|
|
toolUseId: id,
|
|
},
|
|
{
|
|
kind: "tool_result" as const,
|
|
ts,
|
|
toolUseId: id,
|
|
content: detailText,
|
|
isError: toolInfo.hasError,
|
|
},
|
|
] as TranscriptEntry[];
|
|
}
|
|
|
|
// Fallback: raw ┊ line that doesn't match tool format
|
|
const stripped = trimmed
|
|
.replace(/^\[done\]\s*/, "")
|
|
.replace(new RegExp(`^${TOOL_OUTPUT_PREFIX.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*`), "")
|
|
.trim();
|
|
return [{ kind: "stdout", ts, text: stripped }];
|
|
}
|
|
|
|
// ── Thinking blocks ────────────────────────────────────────────────────
|
|
if (isThinkingLine(trimmed)) {
|
|
return [
|
|
{
|
|
kind: "thinking",
|
|
ts,
|
|
text: trimmed.replace(/^💭\s*/, ""),
|
|
},
|
|
];
|
|
}
|
|
|
|
// ── Error output ───────────────────────────────────────────────────────
|
|
if (
|
|
trimmed.startsWith("Error:") ||
|
|
trimmed.startsWith("ERROR:") ||
|
|
trimmed.startsWith("Traceback")
|
|
) {
|
|
return [{ kind: "stderr", ts, text: trimmed }];
|
|
}
|
|
|
|
// ── Regular assistant output ───────────────────────────────────────────
|
|
return [{ kind: "assistant", ts, text: trimmed }];
|
|
}
|