LibreChat/packages/api/src/code/paths.ts
Danny Avila ab086a6616
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
🧭 fix: Explain Missing Workspace Paths to the Model (#16700)
Attached workspace file tools now append a short, specific explanation when
the worker rejects a path with 422:

- NOT_FOUND from a current worker, or INVALID_PATH carrying its
  "Workspace path does not exist" message through an older Code API, says
  the path does not exist: wait for a command still writing it, or list the
  directory to find the right path. For create_file it names the missing
  parent directory.
- An older worker's bare INVALID_PATH on read_file, create_file or
  edit_file, which it also returns for a missing file, lists the possible
  causes: not written yet, a directory or symlink, or a missing parent
  directory for create_file.

Other rejections are unchanged.
2026-10-02 23:13:59 -04:00

73 lines
3.3 KiB
TypeScript

import type { WorkspaceToolHttpError } from './workspace';
/** The attached file tools whose path rejections LibreChat explains to the model. */
export type WorkspacePathOperation = 'read' | 'write' | 'edit' | 'search' | 'list';
/**
* `missing`: the worker reports the path absent, as `NOT_FOUND` or, through a Code API that
* predates that code, as `INVALID_PATH` with the same message. `unusable`: an older worker's
* `INVALID_PATH`, which it also returns for a file that does not exist.
*/
type WorkspacePathRejection = 'missing' | 'unusable';
const WORKSPACE_PATH_MISSING_MESSAGE = 'Workspace path does not exist';
/** Older workers cannot tell a missing file from an unusable path, so only these get that hint. */
const LEGACY_REJECTION_OPERATIONS: ReadonlySet<WorkspacePathOperation> = new Set([
'read',
'write',
'edit',
]);
function getWorkspacePathRejection(
error: WorkspaceToolHttpError,
): WorkspacePathRejection | undefined {
if (error.reason !== 'rejected' || error.upstreamStatus !== 422 || !error.upstreamBody) {
return undefined;
}
try {
const parsed: { code?: unknown; error?: unknown } | null = JSON.parse(error.upstreamBody);
if (parsed?.code === 'NOT_FOUND') return 'missing';
if (parsed?.code !== 'INVALID_PATH') return undefined;
return parsed.error === WORKSPACE_PATH_MISSING_MESSAGE ? 'missing' : 'unusable';
} catch {
return undefined;
}
}
function describeMissingPath(operation: WorkspacePathOperation, displayPath: string): string {
const quoted = JSON.stringify(displayPath);
if (operation === 'write') {
return `A parent directory of ${quoted} does not exist. Create it first (for example with mkdir -p), then retry.`;
}
if (operation === 'search' || operation === 'list') {
return `${quoted} does not exist. List a parent directory to find the right path.`;
}
return `${quoted} does not exist. If a command is still writing it, wait for that command to finish before reading it again; otherwise list its directory to find the right path.`;
}
function describeUnusablePath(operation: WorkspacePathOperation, displayPath: string): string {
const quoted = JSON.stringify(displayPath);
if (operation === 'write') {
return `The worker could not write ${quoted}. Its parent directory may not exist yet, so create it first (for example with mkdir -p) and retry; otherwise the path is a symlink or passes through one.`;
}
return `The worker could not open ${quoted}. The file may not exist yet (a command may still be writing it), or the path is a directory or a symlink, or passes through one. List its directory to check before retrying.`;
}
/**
* Appends a short explanation to a worker's path rejection so the model can tell a file that
* does not exist yet from a malformed path. Other rejections are returned unchanged.
*/
export function explainWorkspacePathRejection(
error: WorkspaceToolHttpError,
operation: WorkspacePathOperation,
displayPath: string,
): WorkspaceToolHttpError {
const rejection = getWorkspacePathRejection(error);
if (rejection === 'missing') {
error.message += `. ${describeMissingPath(operation, displayPath)}`;
} else if (rejection === 'unusable' && LEGACY_REJECTION_OPERATIONS.has(operation)) {
error.message += `. ${describeUnusablePath(operation, displayPath)}`;
}
return error;
}