File size: 1,199 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { isTruthyEnvValue } from "../infra/env.js";
import { redactToolPayloadText } from "./redact.js";
import type { SubsystemLogger } from "./subsystem.js";

const MAX_NAMES = 24;
const MAX_NAME_LENGTH = 80;

type CodeModeDiagnosticFields = Record<
  string,
  boolean | number | string | readonly string[] | undefined
>;

export function isCodeModeDiagnosticEnabled(): boolean {
  return isTruthyEnvValue(process.env.OPENCLAW_DEBUG_CODE_MODE);
}

export function logCodeModeDiagnostic(
  log: Pick<SubsystemLogger, "info">,
  boundary: string,
  fields: CodeModeDiagnosticFields,
): void {
  if (!isCodeModeDiagnosticEnabled()) {
    return;
  }
  const bounded = Object.fromEntries(
    Object.entries(fields).map(([key, value]) => {
      if (Array.isArray(value)) {
        const names = [...new Set(value.map((name) => name.slice(0, MAX_NAME_LENGTH)))];
        names.sort((left, right) => left.localeCompare(right));
        return [key, names.slice(0, MAX_NAMES)];
      }
      return [key, typeof value === "string" ? value.slice(0, MAX_NAME_LENGTH) : value];
    }),
  );
  log.info(
    redactToolPayloadText(`code-mode diagnostic ${JSON.stringify({ boundary, ...bounded })}`),
  );
}