Files
t3code/packages/shared/src/toolOutput.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

169 lines
5.6 KiB
TypeScript

import * as Predicate from "effect/Predicate";
const MAX_PARSED_BYTES = 16_384;
const MAX_METADATA_BYTES = 8_192;
const MAX_ID_LENGTH = 256;
const MAX_THREADS = 100;
const MAX_CONTENT_BLOCKS = 32;
const MAX_ENVELOPE_DEPTH = 4;
const MAX_ENVELOPE_NODES = 128;
const encoder = new TextEncoder();
interface ResultReadBudget {
remainingBytes: number;
remainingNodes: number;
exceeded: boolean;
}
interface ResultEnvelope {
data?: Record<PropertyKey, unknown>;
failed: boolean;
}
interface CompactToolOutput {
isError?: true;
threadId?: string;
messageId?: string;
taskId?: string;
scheduledTaskId?: string;
status?: "rolled_back";
thread?: { threadId: string };
threads?: Array<{ threadId?: string; status?: "rolled_back" }>;
}
function readResult(value: unknown, budget: ResultReadBudget, depth = 0): ResultEnvelope {
if (depth > MAX_ENVELOPE_DEPTH || budget.remainingNodes-- <= 0) {
budget.exceeded = true;
return { failed: false };
}
if (typeof value === "string") {
if (value.length > budget.remainingBytes) {
budget.exceeded = true;
return { failed: false };
}
const bytes = encoder.encode(value).byteLength;
if (bytes > budget.remainingBytes) {
budget.exceeded = true;
return { failed: false };
}
budget.remainingBytes -= bytes;
try {
return readResult(JSON.parse(value), budget, depth + 1);
} catch {
return { failed: false };
}
}
if (Array.isArray(value)) {
if (value.length > MAX_CONTENT_BLOCKS) {
budget.exceeded = true;
return { failed: false };
}
let data: ResultEnvelope["data"];
let failed = false;
for (const block of value) {
const text = Predicate.isObject(block) ? block.text : undefined;
const result = readResult(
Predicate.isObject(text) ? (text.text ?? text) : text,
budget,
depth + 1,
);
data ??= result.data;
failed ||= result.failed;
if (budget.exceeded) break;
}
return { ...(data === undefined ? {} : { data }), failed };
}
if (!Predicate.isObject(value)) return { failed: false };
const failed =
value.isError === true ||
value.is_error === true ||
value._tag === "OrchestratorMcpFailure" ||
value.error != null;
const content = value.structuredContent ?? value.content;
if (content !== undefined) {
const nested = readResult(content, budget, depth + 1);
return { ...nested, failed: failed || nested.failed };
}
return { data: value, failed };
}
function boundedId(value: unknown): string | undefined {
if (typeof value !== "string" || value.length > MAX_ID_LENGTH || value.trim() === "") {
return undefined;
}
return Array.from(value).join("");
}
/** Keeps only IDs and failure metadata used by T3's grouped tool summaries. */
export function compactDynamicToolOutput(value: unknown): CompactToolOutput | undefined {
const budget: ResultReadBudget = {
remainingBytes: MAX_PARSED_BYTES,
remainingNodes: MAX_ENVELOPE_NODES,
exceeded: false,
};
const result = readResult(value, budget);
const output: CompactToolOutput = result.failed ? { isError: true } : {};
const data = budget.exceeded ? undefined : result.data;
if (data !== undefined) {
for (const key of ["threadId", "messageId", "taskId", "scheduledTaskId"] as const) {
const id = boundedId(data[key]);
if (id !== undefined) output[key] = id;
}
if (data.status === "rolled_back") output.status = "rolled_back";
const nestedThreadId = Predicate.isObject(data.thread)
? boundedId(data.thread.threadId)
: undefined;
if (nestedThreadId !== undefined) output.thread = { threadId: nestedThreadId };
if (Array.isArray(data.threads)) {
let complete = data.threads.length <= MAX_THREADS;
const threads: NonNullable<CompactToolOutput["threads"]> = [];
if (complete) {
for (const entry of data.threads) {
if (!Predicate.isObject(entry)) {
complete = false;
break;
}
const threadId = boundedId(entry.threadId);
const rolledBack = entry.status === "rolled_back";
if (threadId === undefined && !rolledBack) {
complete = false;
break;
}
threads.push({
...(threadId === undefined ? {} : { threadId }),
...(rolledBack ? { status: "rolled_back" as const } : {}),
});
}
}
if (complete) output.threads = threads;
else {
// A partial batch or a top-level ID would turn an unknown creation
// count into a confidently wrong one in the existing summary parser.
delete output.threadId;
delete output.status;
}
}
}
if (encoder.encode(JSON.stringify(output)).byteLength > MAX_METADATA_BYTES) {
delete output.threads;
delete output.threadId;
delete output.status;
}
return Object.keys(output).length === 0 ? undefined : output;
}
/** Some providers report completion even when command output describes a failure. */
export function toolOutputIndicatesFailure(text: string): boolean {
return (
/file not found|no files found|enoent|no such file|commandnotfoundexception|command not found|is not recognized as the name of a cmdlet|a parameter cannot be found that matches parameter name/i.test(
text,
) ||
(/cannot find path/i.test(text) && /because it does not exist/i.test(text)) ||
(/is not recognized/i.test(text) && /the term '/i.test(text)) ||
/<exited with exit code\s+[1-9]\d*\s*>/i.test(text) ||
/exit(?:ed)? with exit code\s+[1-9]\d*/i.test(text) ||
/exit code\s*[:\s]\s*[1-9]\d*\b/i.test(text)
);
}