chore: migrate to oxlint and oxfmt
Co-authored-by: Christoph Nakazawa <christoph.pojer@gmail.com>
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type {
|
||||
ContextEvent,
|
||||
ExtensionAPI,
|
||||
ExtensionContext,
|
||||
} from "@mariozechner/pi-coding-agent";
|
||||
import type { ContextEvent, ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
|
||||
|
||||
import { pruneContextMessages } from "./pruner.js";
|
||||
import { getContextPruningRuntime } from "./runtime.js";
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import type { AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type {
|
||||
ImageContent,
|
||||
TextContent,
|
||||
ToolResultMessage,
|
||||
} from "@mariozechner/pi-ai";
|
||||
import type { ImageContent, TextContent, ToolResultMessage } from "@mariozechner/pi-ai";
|
||||
import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
|
||||
|
||||
import type { EffectiveContextPruningSettings } from "./settings.js";
|
||||
@@ -18,9 +14,7 @@ function asText(text: string): TextContent {
|
||||
return { type: "text", text };
|
||||
}
|
||||
|
||||
function collectTextSegments(
|
||||
content: ReadonlyArray<TextContent | ImageContent>,
|
||||
): string[] {
|
||||
function collectTextSegments(content: ReadonlyArray<TextContent | ImageContent>): string[] {
|
||||
const parts: string[] = [];
|
||||
for (const block of content) {
|
||||
if (block.type === "text") parts.push(block.text);
|
||||
@@ -82,9 +76,7 @@ function takeTailFromJoinedText(parts: string[], maxChars: number): string {
|
||||
return out.join("");
|
||||
}
|
||||
|
||||
function hasImageBlocks(
|
||||
content: ReadonlyArray<TextContent | ImageContent>,
|
||||
): boolean {
|
||||
function hasImageBlocks(content: ReadonlyArray<TextContent | ImageContent>): boolean {
|
||||
for (const block of content) {
|
||||
if (block.type === "image") return true;
|
||||
}
|
||||
@@ -208,21 +200,16 @@ export function pruneContextMessages(params: {
|
||||
const charWindow = contextWindowTokens * CHARS_PER_TOKEN_ESTIMATE;
|
||||
if (charWindow <= 0) return messages;
|
||||
|
||||
const cutoffIndex = findAssistantCutoffIndex(
|
||||
messages,
|
||||
settings.keepLastAssistants,
|
||||
);
|
||||
const cutoffIndex = findAssistantCutoffIndex(messages, settings.keepLastAssistants);
|
||||
if (cutoffIndex === null) return messages;
|
||||
|
||||
// Bootstrap safety: never prune anything before the first user message. This protects initial
|
||||
// "identity" reads (SOUL.md, USER.md, etc.) which typically happen before the first inbound user
|
||||
// message exists in the session transcript.
|
||||
const firstUserIndex = findFirstUserIndex(messages);
|
||||
const pruneStartIndex =
|
||||
firstUserIndex === null ? messages.length : firstUserIndex;
|
||||
const pruneStartIndex = firstUserIndex === null ? messages.length : firstUserIndex;
|
||||
|
||||
const isToolPrunable =
|
||||
params.isToolPrunable ?? makeToolPrunablePredicate(settings.tools);
|
||||
const isToolPrunable = params.isToolPrunable ?? makeToolPrunablePredicate(settings.tools);
|
||||
|
||||
if (settings.mode === "aggressive") {
|
||||
let next: AgentMessage[] | null = null;
|
||||
|
||||
@@ -41,81 +41,55 @@ export type EffectiveContextPruningSettings = {
|
||||
};
|
||||
};
|
||||
|
||||
export const DEFAULT_CONTEXT_PRUNING_SETTINGS: EffectiveContextPruningSettings =
|
||||
{
|
||||
mode: "adaptive",
|
||||
keepLastAssistants: 3,
|
||||
softTrimRatio: 0.3,
|
||||
hardClearRatio: 0.5,
|
||||
minPrunableToolChars: 50_000,
|
||||
tools: {},
|
||||
softTrim: {
|
||||
maxChars: 4_000,
|
||||
headChars: 1_500,
|
||||
tailChars: 1_500,
|
||||
},
|
||||
hardClear: {
|
||||
enabled: true,
|
||||
placeholder: "[Old tool result content cleared]",
|
||||
},
|
||||
};
|
||||
export const DEFAULT_CONTEXT_PRUNING_SETTINGS: EffectiveContextPruningSettings = {
|
||||
mode: "adaptive",
|
||||
keepLastAssistants: 3,
|
||||
softTrimRatio: 0.3,
|
||||
hardClearRatio: 0.5,
|
||||
minPrunableToolChars: 50_000,
|
||||
tools: {},
|
||||
softTrim: {
|
||||
maxChars: 4_000,
|
||||
headChars: 1_500,
|
||||
tailChars: 1_500,
|
||||
},
|
||||
hardClear: {
|
||||
enabled: true,
|
||||
placeholder: "[Old tool result content cleared]",
|
||||
},
|
||||
};
|
||||
|
||||
export function computeEffectiveSettings(
|
||||
raw: unknown,
|
||||
): EffectiveContextPruningSettings | null {
|
||||
export function computeEffectiveSettings(raw: unknown): EffectiveContextPruningSettings | null {
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const cfg = raw as ContextPruningConfig;
|
||||
if (cfg.mode !== "adaptive" && cfg.mode !== "aggressive") return null;
|
||||
|
||||
const s: EffectiveContextPruningSettings = structuredClone(
|
||||
DEFAULT_CONTEXT_PRUNING_SETTINGS,
|
||||
);
|
||||
const s: EffectiveContextPruningSettings = structuredClone(DEFAULT_CONTEXT_PRUNING_SETTINGS);
|
||||
s.mode = cfg.mode;
|
||||
|
||||
if (
|
||||
typeof cfg.keepLastAssistants === "number" &&
|
||||
Number.isFinite(cfg.keepLastAssistants)
|
||||
) {
|
||||
if (typeof cfg.keepLastAssistants === "number" && Number.isFinite(cfg.keepLastAssistants)) {
|
||||
s.keepLastAssistants = Math.max(0, Math.floor(cfg.keepLastAssistants));
|
||||
}
|
||||
if (
|
||||
typeof cfg.softTrimRatio === "number" &&
|
||||
Number.isFinite(cfg.softTrimRatio)
|
||||
) {
|
||||
if (typeof cfg.softTrimRatio === "number" && Number.isFinite(cfg.softTrimRatio)) {
|
||||
s.softTrimRatio = Math.min(1, Math.max(0, cfg.softTrimRatio));
|
||||
}
|
||||
if (
|
||||
typeof cfg.hardClearRatio === "number" &&
|
||||
Number.isFinite(cfg.hardClearRatio)
|
||||
) {
|
||||
if (typeof cfg.hardClearRatio === "number" && Number.isFinite(cfg.hardClearRatio)) {
|
||||
s.hardClearRatio = Math.min(1, Math.max(0, cfg.hardClearRatio));
|
||||
}
|
||||
if (
|
||||
typeof cfg.minPrunableToolChars === "number" &&
|
||||
Number.isFinite(cfg.minPrunableToolChars)
|
||||
) {
|
||||
if (typeof cfg.minPrunableToolChars === "number" && Number.isFinite(cfg.minPrunableToolChars)) {
|
||||
s.minPrunableToolChars = Math.max(0, Math.floor(cfg.minPrunableToolChars));
|
||||
}
|
||||
if (cfg.tools) {
|
||||
s.tools = cfg.tools;
|
||||
}
|
||||
if (cfg.softTrim) {
|
||||
if (
|
||||
typeof cfg.softTrim.maxChars === "number" &&
|
||||
Number.isFinite(cfg.softTrim.maxChars)
|
||||
) {
|
||||
if (typeof cfg.softTrim.maxChars === "number" && Number.isFinite(cfg.softTrim.maxChars)) {
|
||||
s.softTrim.maxChars = Math.max(0, Math.floor(cfg.softTrim.maxChars));
|
||||
}
|
||||
if (
|
||||
typeof cfg.softTrim.headChars === "number" &&
|
||||
Number.isFinite(cfg.softTrim.headChars)
|
||||
) {
|
||||
if (typeof cfg.softTrim.headChars === "number" && Number.isFinite(cfg.softTrim.headChars)) {
|
||||
s.softTrim.headChars = Math.max(0, Math.floor(cfg.softTrim.headChars));
|
||||
}
|
||||
if (
|
||||
typeof cfg.softTrim.tailChars === "number" &&
|
||||
Number.isFinite(cfg.softTrim.tailChars)
|
||||
) {
|
||||
if (typeof cfg.softTrim.tailChars === "number" && Number.isFinite(cfg.softTrim.tailChars)) {
|
||||
s.softTrim.tailChars = Math.max(0, Math.floor(cfg.softTrim.tailChars));
|
||||
}
|
||||
}
|
||||
@@ -123,10 +97,7 @@ export function computeEffectiveSettings(
|
||||
if (s.mode === "adaptive" && typeof cfg.hardClear.enabled === "boolean") {
|
||||
s.hardClear.enabled = cfg.hardClear.enabled;
|
||||
}
|
||||
if (
|
||||
typeof cfg.hardClear.placeholder === "string" &&
|
||||
cfg.hardClear.placeholder.trim()
|
||||
) {
|
||||
if (typeof cfg.hardClear.placeholder === "string" && cfg.hardClear.placeholder.trim()) {
|
||||
s.hardClear.placeholder = cfg.hardClear.placeholder.trim();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user