Attempts to normalized Agent output when using OpenSSH Server

This commit is contained in:
Raven Scott
2026-04-22 17:11:42 -04:00
parent 50f90320f7
commit bc22a0d63e
11 changed files with 105 additions and 30 deletions
+30 -8
View File
@@ -4225,19 +4225,36 @@ function bareAgentErr(ctx, s) {
}
/**
* SSH PTYs expect CRLF for line breaks (same as `bare-openssh-pty-console.js` / `man` via console).
* @param {Record<string, unknown> | undefined} ctx
* @param {string} s
*/
function bareAgentNormalizeStreamNewlines(ctx, s) {
if (ctx && ctx.bareOsPtyStdoutCrlf)
return String(s).replace(/\r?\n/g, '\r\n')
return String(s)
}
/**
* @param {Record<string, unknown> | undefined} ctx
* @param {import('stream').Writable | undefined} out
* @param {string} s
*/
function bareAgentWriteOut(out, s) {
function bareAgentWriteOut(ctx, out, s) {
const text = bareAgentNormalizeStreamNewlines(ctx, s)
if (out && typeof out.write === 'function') {
try {
out.write(s)
out.write(text)
} catch {
/* ignore */
}
} else {
try {
process.stdout.write(s)
if (typeof bareOsEmitRaw === 'function') {
bareOsEmitRaw(ctx, text)
} else if (typeof process !== 'undefined' && process.stdout?.write) {
process.stdout.write(text)
}
} catch {
/* ignore */
}
@@ -4325,7 +4342,7 @@ async function bareAgentPromptSetupLine(ctx, prompt) {
if (!stdin || typeof stdin.on !== 'function') {
throw new Error('setup: stdin stream unavailable')
}
bareAgentWriteOut(stdout, '\x1b[?25h\x1b[0m' + prompt)
bareAgentWriteOut(ctx, stdout, '\x1b[?25h\x1b[0m' + prompt)
return bareAgentReadStreamLineOnce(stdin)
}
@@ -4463,6 +4480,7 @@ async function bareAgentInteractiveSetup(ctx, argv0, paths, config, opts) {
}
const stdout = ctx.replStdout || ctx.stdout
bareAgentWriteOut(
ctx,
stdout,
'\n=== ' +
argv0 +
@@ -4740,6 +4758,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
const fn = () => {
masterAbort.abort()
bareAgentWriteOut(
ctx,
stdout,
bareEditSgr('dim', useColor) + '^C' + EDIT_ANSI_RESET + '\n'
)
@@ -4809,7 +4828,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
if (e.type === 'delta_content') {
const chunk = typeof e.content === 'string' ? e.content : ''
assistantContent += chunk
bareAgentWriteOut(stdout, chunk)
bareAgentWriteOut(ctx, stdout, chunk)
} else if (e.type === 'delta_tool_calls') {
const arr = e.tool_calls
if (Array.isArray(arr)) {
@@ -4850,6 +4869,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
const pt = u.prompt_tokens
const ct = u.completion_tokens
bareAgentWriteOut(
ctx,
stdout,
'\n' +
bareEditSgr('dim', useColor) +
@@ -4864,7 +4884,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
if (!hasTools) {
await bareAgentSaveHistory(ctx, paths.history, messages)
bareAgentWriteOut(stdout, '\n')
bareAgentWriteOut(ctx, stdout, '\n')
break
}
@@ -4885,6 +4905,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
const name = fn?.name || ''
const argsStr = fn?.arguments || '{}'
bareAgentWriteOut(
ctx,
stdout,
'\n' +
bareEditSgr('keyword', useColor) +
@@ -4895,7 +4916,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
)
const spinner = bareEditSgr('dim', useColor) + '… running ' + name + EDIT_ANSI_RESET
bareAgentWriteOut(stdout, spinner + '\r')
bareAgentWriteOut(ctx, stdout, spinner + '\r')
const resultStr = await bareAgentDispatchTool({
ctx,
@@ -4910,7 +4931,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
onTaskComplete
})
bareAgentWriteOut(stdout, '\x1b[K')
bareAgentWriteOut(ctx, stdout, '\x1b[K')
messages.push({
role: 'tool',
@@ -4924,6 +4945,7 @@ async function bareOsRunAgentSession(ctx, argv0, task, runOpts) {
await bareAgentSaveHistory(ctx, paths.history, messages)
if (completed) {
bareAgentWriteOut(
ctx,
stdout,
bareEditSgr('dim', useColor) +
'\nDone: ' +