mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 07:57:28 +00:00
refactor(config): share byte-size parsing for memory flush
This commit is contained in:
@@ -2,7 +2,7 @@ import { lookupContextTokens } from "../../agents/context.js";
|
|||||||
import { resolveCronStyleNow } from "../../agents/current-time.js";
|
import { resolveCronStyleNow } from "../../agents/current-time.js";
|
||||||
import { DEFAULT_CONTEXT_TOKENS } from "../../agents/defaults.js";
|
import { DEFAULT_CONTEXT_TOKENS } from "../../agents/defaults.js";
|
||||||
import { DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR } from "../../agents/pi-settings.js";
|
import { DEFAULT_PI_COMPACTION_RESERVE_TOKENS_FLOOR } from "../../agents/pi-settings.js";
|
||||||
import { parseByteSize } from "../../cli/parse-bytes.js";
|
import { parseNonNegativeByteSize } from "../../config/byte-size.js";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import { resolveFreshSessionTotalTokens, type SessionEntry } from "../../config/sessions.js";
|
import { resolveFreshSessionTotalTokens, type SessionEntry } from "../../config/sessions.js";
|
||||||
import { SILENT_REPLY_TOKEN } from "../tokens.js";
|
import { SILENT_REPLY_TOKEN } from "../tokens.js";
|
||||||
@@ -78,26 +78,6 @@ const normalizeNonNegativeInt = (value: unknown): number | null => {
|
|||||||
return int >= 0 ? int : null;
|
return int >= 0 ? int : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const normalizeOptionalByteSize = (value: unknown): number | null => {
|
|
||||||
if (typeof value === "number" && Number.isFinite(value)) {
|
|
||||||
const int = Math.floor(value);
|
|
||||||
return int >= 0 ? int : null;
|
|
||||||
}
|
|
||||||
if (typeof value === "string") {
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (!trimmed) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const bytes = parseByteSize(trimmed, { defaultUnit: "b" });
|
|
||||||
return bytes >= 0 ? bytes : null;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function resolveMemoryFlushSettings(cfg?: OpenClawConfig): MemoryFlushSettings | null {
|
export function resolveMemoryFlushSettings(cfg?: OpenClawConfig): MemoryFlushSettings | null {
|
||||||
const defaults = cfg?.agents?.defaults?.compaction?.memoryFlush;
|
const defaults = cfg?.agents?.defaults?.compaction?.memoryFlush;
|
||||||
const enabled = defaults?.enabled ?? true;
|
const enabled = defaults?.enabled ?? true;
|
||||||
@@ -107,7 +87,7 @@ export function resolveMemoryFlushSettings(cfg?: OpenClawConfig): MemoryFlushSet
|
|||||||
const softThresholdTokens =
|
const softThresholdTokens =
|
||||||
normalizeNonNegativeInt(defaults?.softThresholdTokens) ?? DEFAULT_MEMORY_FLUSH_SOFT_TOKENS;
|
normalizeNonNegativeInt(defaults?.softThresholdTokens) ?? DEFAULT_MEMORY_FLUSH_SOFT_TOKENS;
|
||||||
const forceFlushTranscriptBytes =
|
const forceFlushTranscriptBytes =
|
||||||
normalizeOptionalByteSize(defaults?.forceFlushTranscriptBytes) ??
|
parseNonNegativeByteSize(defaults?.forceFlushTranscriptBytes) ??
|
||||||
DEFAULT_MEMORY_FLUSH_FORCE_TRANSCRIPT_BYTES;
|
DEFAULT_MEMORY_FLUSH_FORCE_TRANSCRIPT_BYTES;
|
||||||
const prompt = defaults?.prompt?.trim() || DEFAULT_MEMORY_FLUSH_PROMPT;
|
const prompt = defaults?.prompt?.trim() || DEFAULT_MEMORY_FLUSH_PROMPT;
|
||||||
const systemPrompt = defaults?.systemPrompt?.trim() || DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT;
|
const systemPrompt = defaults?.systemPrompt?.trim() || DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT;
|
||||||
|
|||||||
29
src/config/byte-size.ts
Normal file
29
src/config/byte-size.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { parseByteSize } from "../cli/parse-bytes.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse an optional byte-size value from config.
|
||||||
|
* Accepts non-negative numbers or strings like "2mb".
|
||||||
|
*/
|
||||||
|
export function parseNonNegativeByteSize(value: unknown): number | null {
|
||||||
|
if (typeof value === "number" && Number.isFinite(value)) {
|
||||||
|
const int = Math.floor(value);
|
||||||
|
return int >= 0 ? int : null;
|
||||||
|
}
|
||||||
|
if (typeof value === "string") {
|
||||||
|
const trimmed = value.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const bytes = parseByteSize(trimmed, { defaultUnit: "b" });
|
||||||
|
return bytes >= 0 ? bytes : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isValidNonNegativeByteSizeString(value: string): boolean {
|
||||||
|
return parseNonNegativeByteSize(value) !== null;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { parseByteSize } from "../cli/parse-bytes.js";
|
import { isValidNonNegativeByteSizeString } from "./byte-size.js";
|
||||||
import {
|
import {
|
||||||
HeartbeatSchema,
|
HeartbeatSchema,
|
||||||
AgentSandboxSchema,
|
AgentSandboxSchema,
|
||||||
@@ -96,14 +96,9 @@ export const AgentDefaultsSchema = z
|
|||||||
forceFlushTranscriptBytes: z
|
forceFlushTranscriptBytes: z
|
||||||
.union([
|
.union([
|
||||||
z.number().int().nonnegative(),
|
z.number().int().nonnegative(),
|
||||||
z.string().refine((value) => {
|
z
|
||||||
try {
|
.string()
|
||||||
parseByteSize(value.trim(), { defaultUnit: "b" });
|
.refine(isValidNonNegativeByteSizeString, "Expected byte size string like 2mb"),
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}, "Expected byte size string like 2mb"),
|
|
||||||
])
|
])
|
||||||
.optional(),
|
.optional(),
|
||||||
prompt: z.string().optional(),
|
prompt: z.string().optional(),
|
||||||
|
|||||||
Reference in New Issue
Block a user