

























@@ -1,3 +1,7 @@
1+/**
2+ * Shared helpers for CLI runner prompts, args, queueing, sessions, and image
3+ * payload preparation.
4+ */
15import crypto from "node:crypto";
26import fs from "node:fs/promises";
37import os from "node:os";
@@ -31,6 +35,7 @@ import { buildSystemPromptParams } from "../system-prompt-params.js";
3135import type { SilentReplyPromptMode } from "../system-prompt.types.js";
3236import { sanitizeImageBlocks } from "../tool-images.js";
3337import { formatTomlConfigOverride } from "./toml-inline.js";
38+/** Re-export CLI reliability helpers used by older runner call sites. */
3439export { buildCliSupervisorScopeKey, resolveCliNoOutputTimeoutMs } from "./reliability.js";
35403641const CLI_RUN_QUEUE = new KeyedAsyncQueue();
@@ -39,10 +44,12 @@ function isClaudeCliProvider(providerId: string): boolean {
3944return normalizeOptionalLowercaseString(providerId) === "claude-cli";
4045}
414647+/** Enqueues a CLI run under a backend/session key to prevent unsafe overlap. */
4248export function enqueueCliRun<T>(key: string, task: () => Promise<T>): Promise<T> {
4349return CLI_RUN_QUEUE.enqueue(key, task);
4450}
455152+/** Resolves the serialization key for a CLI backend run. */
4653export function resolveCliRunQueueKey(params: {
4754backendId: string;
4855serialize?: boolean;
@@ -66,6 +73,7 @@ export function resolveCliRunQueueKey(params: {
6673return params.backendId;
6774}
687576+/** Builds the system prompt sent to a CLI-backed agent runtime. */
6977export function buildCliAgentSystemPrompt(params: {
7078workspaceDir: string;
7179cwd?: string;
@@ -133,8 +141,10 @@ export function buildCliAgentSystemPrompt(params: {
133141});
134142}
135143144+/** Back-compat alias for the CLI system prompt builder. */
136145export const buildSystemPrompt = buildCliAgentSystemPrompt;
137146147+/** Applies backend model aliases to a requested CLI model id. */
138148export function normalizeCliModel(modelId: string, backend: CliBackendConfig): string {
139149const trimmed = modelId.trim();
140150if (!trimmed) {
@@ -152,6 +162,7 @@ export function normalizeCliModel(modelId: string, backend: CliBackendConfig): s
152162return trimmed;
153163}
154164165+/** Decides whether a system prompt should be sent for this CLI turn. */
155166export function resolveSystemPromptUsage(params: {
156167backend: CliBackendConfig;
157168isNewSession: boolean;
@@ -178,6 +189,7 @@ export function resolveSystemPromptUsage(params: {
178189return systemPrompt;
179190}
180191192+/** Resolves the CLI session id to send and whether the turn starts a new session. */
181193export function resolveSessionIdToSend(params: {
182194backend: CliBackendConfig;
183195cliSessionId?: string;
@@ -196,6 +208,7 @@ export function resolveSessionIdToSend(params: {
196208return { sessionId: crypto.randomUUID(), isNew: true };
197209}
198210211+/** Routes prompt text to argv or stdin based on backend input policy. */
199212export function resolvePromptInput(params: { backend: CliBackendConfig; prompt: string }): {
200213argsPrompt?: string;
201214stdin?: string;
@@ -237,6 +250,7 @@ function appendImagePathsToPrompt(prompt: string, paths: string[], prefix = ""):
237250return `${trimmed}${separator}${paths.map((entry) => `${prefix}${entry}`).join("\n")}`;
238251}
239252253+/** Loads and sanitizes image references found in prompt text. */
240254export async function loadPromptRefImages(params: {
241255prompt: string;
242256workspaceDir: string;
@@ -274,6 +288,7 @@ export async function loadPromptRefImages(params: {
274288return sanitizedImages;
275289}
276290291+/** Writes CLI image payloads to private paths and returns their file paths. */
277292export async function writeCliImages(params: {
278293backend: CliBackendConfig;
279294workspaceDir: string;
@@ -298,6 +313,7 @@ export async function writeCliImages(params: {
298313return { paths, cleanup };
299314}
300315316+/** Writes a temporary system prompt file when the backend needs file-based prompts. */
301317export async function writeCliSystemPromptFile(params: {
302318backend: CliBackendConfig;
303319systemPrompt: string;
@@ -322,6 +338,7 @@ export async function writeCliSystemPromptFile(params: {
322338};
323339}
324340341+/** Prepares prompt text and image paths for a CLI backend run. */
325342export async function prepareCliPromptImagePayload(params: {
326343backend: CliBackendConfig;
327344prompt: string;
@@ -364,6 +381,7 @@ export async function prepareCliPromptImagePayload(params: {
364381};
365382}
366383384+/** Builds final CLI argv from backend config and prepared prompt/session inputs. */
367385export function buildCliArgs(params: {
368386backend: CliBackendConfig;
369387baseArgs: string[];
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。