f1f6516056
- Removed export leakage by demoting many helper and const symbols to module-local scope. - Renamed underscore-prefixed internals and cache fields, then updated related references and `satisfies never` checks. - Deleted obsolete logic branches and helpers, including harmony-stream interruption flow and unused benchmark runtime helpers. - Updated Biome config and manifests by broadening lint coverage and removing an unused `@napi-rs/cli` dev dependency. - Adjusted tests and utilities to use renamed test helpers and remove redundant private test-only helpers/locals.
118 lines
3.5 KiB
TypeScript
118 lines
3.5 KiB
TypeScript
/**
|
|
* Print mode (single-shot): Send prompts, output result, exit.
|
|
*
|
|
* Used for:
|
|
* - `omp -p "prompt"` - text output
|
|
* - `omp --mode json "prompt"` - JSON event stream
|
|
*/
|
|
import type { AssistantMessage, ImageContent } from "@oh-my-pi/pi-ai";
|
|
import { sanitizeText } from "@oh-my-pi/pi-natives";
|
|
import type { AgentSession } from "../session/agent-session";
|
|
import { initializeExtensions } from "./runtime-init";
|
|
|
|
/**
|
|
* Options for print mode.
|
|
*/
|
|
export interface PrintModeOptions {
|
|
/** Output mode: "text" for final response only, "json" for all events */
|
|
mode: "text" | "json";
|
|
/** Array of additional prompts to send after initialMessage */
|
|
messages?: string[];
|
|
/** First message to send (may contain @file content) */
|
|
initialMessage?: string;
|
|
/** Images to attach to the initial message */
|
|
initialImages?: ImageContent[];
|
|
}
|
|
|
|
/**
|
|
* Run in print (single-shot) mode.
|
|
* Sends prompts to the agent and outputs the result.
|
|
*/
|
|
export async function runPrintMode(session: AgentSession, options: PrintModeOptions): Promise<void> {
|
|
const { mode, messages = [], initialMessage, initialImages } = options;
|
|
|
|
// Emit session header for JSON mode
|
|
if (mode === "json") {
|
|
const header = session.sessionManager.getHeader();
|
|
if (header) {
|
|
process.stdout.write(`${JSON.stringify(header)}\n`);
|
|
}
|
|
}
|
|
// Set up extensions for print mode (no UI, no command context)
|
|
await initializeExtensions(session, {
|
|
reportSendError: (action, err) => {
|
|
process.stderr.write(
|
|
`Extension ${action === "extension_send" ? "sendMessage" : "sendUserMessage"} failed: ${err.message}\n`,
|
|
);
|
|
},
|
|
reportRuntimeError: err => {
|
|
process.stderr.write(`Extension error (${err.extensionPath}): ${err.error}\n`);
|
|
},
|
|
});
|
|
|
|
// Always subscribe to enable session persistence via _handleAgentEvent
|
|
session.subscribe(event => {
|
|
// In JSON mode, output all events
|
|
if (mode === "json") {
|
|
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
}
|
|
});
|
|
|
|
// Send initial message with attachments
|
|
if (initialMessage !== undefined) {
|
|
await session.prompt(initialMessage, { images: initialImages });
|
|
}
|
|
|
|
// Send remaining messages
|
|
for (const message of messages) {
|
|
await session.prompt(message);
|
|
}
|
|
|
|
// In text mode, output final response
|
|
if (mode === "text") {
|
|
const state = session.state;
|
|
const lastMessage = state.messages[state.messages.length - 1];
|
|
|
|
if (lastMessage?.role === "assistant") {
|
|
const assistantMsg = lastMessage as AssistantMessage;
|
|
|
|
// Check for error/aborted
|
|
if (assistantMsg.stopReason === "error" || assistantMsg.stopReason === "aborted") {
|
|
const errorLine = sanitizeText(assistantMsg.errorMessage || `Request ${assistantMsg.stopReason}`);
|
|
const flushed = process.stderr.write(`${errorLine}\n`);
|
|
if (flushed) {
|
|
process.exit(1);
|
|
} else {
|
|
process.stderr.once("drain", () => process.exit(1));
|
|
}
|
|
}
|
|
|
|
if (
|
|
assistantMsg.errorMessage &&
|
|
assistantMsg.stopReason !== "error" &&
|
|
assistantMsg.stopReason !== "aborted"
|
|
) {
|
|
process.stderr.write(`${sanitizeText(assistantMsg.errorMessage)}\n`);
|
|
}
|
|
|
|
// Output text content
|
|
for (const content of assistantMsg.content) {
|
|
if (content.type === "text") {
|
|
process.stdout.write(`${sanitizeText(content.text)}\n`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure stdout is fully flushed before returning
|
|
// This prevents race conditions where the process exits before all output is written
|
|
await new Promise<void>((resolve, reject) => {
|
|
process.stdout.write("", err => {
|
|
if (err) reject(err);
|
|
else resolve();
|
|
});
|
|
});
|
|
|
|
await session.dispose();
|
|
}
|