48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { config } from "../config.js";
|
|
import { type RuntimeSessionContext } from "./sessionContext.js";
|
|
import { stageConversationText } from "./conversationWorkspace.js";
|
|
|
|
export type ToolDataFilePayload = {
|
|
bytes: number;
|
|
content_type: string;
|
|
file_path: string;
|
|
};
|
|
|
|
export const stageLargeToolOutput = async (
|
|
context: RuntimeSessionContext,
|
|
content: string,
|
|
options: {
|
|
contentType: string;
|
|
extension: string;
|
|
force?: boolean;
|
|
prefix: string;
|
|
},
|
|
): Promise<ToolDataFilePayload | null> => {
|
|
if (!options.force && Buffer.byteLength(content) <= config.MAX_INLINE_RESULT_BYTES) {
|
|
return null;
|
|
}
|
|
if (!context.workspaceDirectory) {
|
|
throw new Error(
|
|
"large tool output requires a conversation workspace; create a new conversation",
|
|
);
|
|
}
|
|
const staged = await stageConversationText(
|
|
context.workspaceDirectory,
|
|
config.RESULT_REF_IMPORT_DIR,
|
|
content,
|
|
options,
|
|
);
|
|
return {
|
|
bytes: staged.bytes,
|
|
content_type: staged.contentType,
|
|
file_path: staged.filePath,
|
|
};
|
|
};
|
|
|
|
export const buildLargeCliResult = (dataFile: ToolDataFilePayload) => ({
|
|
ok: true,
|
|
schema_version: "tjwater-cli/v1",
|
|
summary: "CLI 结果已保存到当前对话工作区",
|
|
data_file: dataFile,
|
|
});
|