




















@@ -3,11 +3,26 @@ import path from "node:path";
33import { sanitizeDiagnosticPayload } from "../agents/payload-redaction.js";
44import { getQueuedFileWriter, type QueuedFileWriter } from "../agents/queued-file-writer.js";
55import type { OpenClawConfig } from "../config/types.openclaw.js";
6-import { resolveUserPath } from "../utils.js";
76import { parseBooleanValue } from "../utils/boolean.js";
87import { safeJsonStringify } from "../utils/safe-json.js";
8+import {
9+TRAJECTORY_RUNTIME_EVENT_MAX_BYTES,
10+TRAJECTORY_RUNTIME_FILE_MAX_BYTES,
11+resolveTrajectoryFilePath,
12+resolveTrajectoryPointerFilePath,
13+resolveTrajectoryPointerOpenFlags,
14+} from "./paths.js";
915import type { TrajectoryEvent, TrajectoryToolDefinition } from "./types.js";
101617+export {
18+TRAJECTORY_RUNTIME_EVENT_MAX_BYTES,
19+TRAJECTORY_RUNTIME_FILE_MAX_BYTES,
20+resolveTrajectoryFilePath,
21+resolveTrajectoryPointerFilePath,
22+resolveTrajectoryPointerOpenFlags,
23+safeTrajectorySessionFileName,
24+} from "./paths.js";
25+1126type TrajectoryRuntimeInit = {
1227cfg?: OpenClawConfig;
1328env?: NodeJS.ProcessEnv;
@@ -30,73 +45,8 @@ type TrajectoryRuntimeRecorder = {
3045};
31463247const writers = new Map<string, QueuedFileWriter>();
33-export const TRAJECTORY_RUNTIME_FILE_MAX_BYTES = 50 * 1024 * 1024;
34-export const TRAJECTORY_RUNTIME_EVENT_MAX_BYTES = 256 * 1024;
3548const MAX_TRAJECTORY_WRITERS = 100;
364937-type TrajectoryPointerOpenFlagConstants = Pick<
38-typeof fs.constants,
39-"O_CREAT" | "O_TRUNC" | "O_WRONLY"
40-> &
41-Partial<Pick<typeof fs.constants, "O_NOFOLLOW">>;
42-43-export function safeTrajectorySessionFileName(sessionId: string): string {
44-const safe = sessionId.replaceAll(/[^A-Za-z0-9_-]/g, "_").slice(0, 120);
45-return /[A-Za-z0-9]/u.test(safe) ? safe : "session";
46-}
47-48-export function resolveTrajectoryPointerOpenFlags(
49-constants: TrajectoryPointerOpenFlagConstants = fs.constants,
50-): number {
51-const noFollow = constants.O_NOFOLLOW;
52-return (
53-constants.O_CREAT |
54-constants.O_TRUNC |
55-constants.O_WRONLY |
56-(typeof noFollow === "number" ? noFollow : 0)
57-);
58-}
59-60-function resolveContainedPath(baseDir: string, fileName: string): string {
61-const resolvedBase = path.resolve(baseDir);
62-const resolvedFile = path.resolve(resolvedBase, fileName);
63-const relative = path.relative(resolvedBase, resolvedFile);
64-if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
65-throw new Error("Trajectory file path escaped its configured directory");
66-}
67-return resolvedFile;
68-}
69-70-export function resolveTrajectoryFilePath(params: {
71-env?: NodeJS.ProcessEnv;
72-sessionFile?: string;
73-sessionId: string;
74-}): string {
75-const env = params.env ?? process.env;
76-const dirOverride = env.OPENCLAW_TRAJECTORY_DIR?.trim();
77-if (dirOverride) {
78-return resolveContainedPath(
79-resolveUserPath(dirOverride),
80-`${safeTrajectorySessionFileName(params.sessionId)}.jsonl`,
81-);
82-}
83-if (!params.sessionFile) {
84-return path.join(
85-process.cwd(),
86-`${safeTrajectorySessionFileName(params.sessionId)}.trajectory.jsonl`,
87-);
88-}
89-return params.sessionFile.endsWith(".jsonl")
90- ? `${params.sessionFile.slice(0, -".jsonl".length)}.trajectory.jsonl`
91- : `${params.sessionFile}.trajectory.jsonl`;
92-}
93-94-export function resolveTrajectoryPointerFilePath(sessionFile: string): string {
95-return sessionFile.endsWith(".jsonl")
96- ? `${sessionFile.slice(0, -".jsonl".length)}.trajectory-path.json`
97- : `${sessionFile}.trajectory-path.json`;
98-}
99-10050function writeTrajectoryPointerBestEffort(params: {
10151filePath: string;
10252sessionFile?: string;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。