Files
my-pi/pi-tool-search/extensions/config.ts
T
云服务部-叶林立andDevin <158243242+devin-ai-integration[bot]@users.noreply.github.com> 2a6bf8c929 feat(pi-overflow): 新增统一的新工具结果展示截断扩展
此前截断分散在多层且互相冲突:Pi bash 保留尾部 50KB,RTK 兜底截断又只留
开头 12000 字符(砍掉报错与完整输出路径),pi-chrome 截开头 30K,web_fetch
各 provider 不一致。新扩展统一负责"新结果给模型看多少":
- capture.ts 最先加载暂存原文,index.ts 最后加载(system-prompt 之前)
- 超过 24K 字符时原文落盘 <agentDir>/my-pi/pi-overflow/(700/600、原子写),
  优先取 bash fullOutputPath 完整文件;模型看到 头 + 中段信号行 + 尾 的有界
  视图,行号基于落盘原文、结尾永远可见
- overflow_query:按 id 正则搜索、行区间、jsonPath 只读查询,id 校验防穿越
- 收编:RTK truncate 默认关闭、删除 pi-chrome truncateText、web_fetch 默认
  每页 20000(上限 100000)、web_search max_results 限定 1–20
- pi-compaction 遮蔽带 details.overflow.id 的结果时在占位符提示 overflow_query
- 登记 tools.jsonc / routing / 权限 allow / tool-search 常驻与分组

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 20:25:44 -07:00

118 lines
4.1 KiB
TypeScript

import { readFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
const TOOL_SEARCH_DIR = join("my-pi", "tool-search");
/**
* Bundle-level `config/` is the single source of truth: the extension reads
* `config/tool-search.json` from inside the installed bundle (resolved
* relative to this source file), never deployed to or migrated from the
* agent directory.
*/
export function getToolSearchConfigPath(): string {
const override = process.env.PI_TOOL_SEARCH_CONFIG_PATH;
if (override) return override;
return fileURLToPath(new URL("../../config/tool-search.json", import.meta.url));
}
/** Disposable catalog cache under the agent dir's `my-pi/tool-search/` tree. */
export function getToolSearchCatalogPath(agentDir: string): string {
return join(agentDir, TOOL_SEARCH_DIR, "catalog-v1.json");
}
export const BUNDLE_TOOL_SEARCH_DEFAULTS = {
alwaysEnabled: [
"codegraph_explore",
"lsp_diagnostics",
"ctx_execute",
"ctx_execute_file",
"ctx_batch_execute",
"memory_search",
"session_search",
"memory_add",
"memory_replace",
"memory_remove",
"skill_manage",
"context_tree_query",
"overflow_query",
],
showToolSearchFooterStatus: false,
maxActiveGroups: 5,
maxToolsPerGroup: 8,
maxDynamicTools: 28,
groupOverrides: {},
} as const;
export interface ToolSearchConfig {
alwaysEnabled: string[];
showToolSearchFooterStatus: boolean;
maxActiveGroups: number;
maxToolsPerGroup: number;
maxDynamicTools: number;
groupOverrides: Record<string, string[]>;
}
function isObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function positiveInteger(value: unknown, fallback: number): number {
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : fallback;
}
function stringList(value: unknown): string[] {
if (!Array.isArray(value)) return [];
return [...new Set(value.filter((item): item is string => typeof item === "string" && item.length > 0))];
}
function readGroupOverrides(value: unknown): Record<string, string[]> {
if (!isObject(value)) return {};
const overrides: Record<string, string[]> = {};
for (const [groupId, names] of Object.entries(value)) {
const tools = stringList(names);
if (groupId.trim() && tools.length > 0) overrides[groupId] = tools;
}
return overrides;
}
export function bundleFallbackConfig(): ToolSearchConfig {
return {
alwaysEnabled: [...BUNDLE_TOOL_SEARCH_DEFAULTS.alwaysEnabled],
showToolSearchFooterStatus: BUNDLE_TOOL_SEARCH_DEFAULTS.showToolSearchFooterStatus,
maxActiveGroups: BUNDLE_TOOL_SEARCH_DEFAULTS.maxActiveGroups,
maxToolsPerGroup: BUNDLE_TOOL_SEARCH_DEFAULTS.maxToolsPerGroup,
maxDynamicTools: BUNDLE_TOOL_SEARCH_DEFAULTS.maxDynamicTools,
groupOverrides: {},
};
}
/**
* Read the Tool Search config. Defaults to the bundle-level
* `config/tool-search.json`; tests inject an explicit temp path. A missing,
* unreadable, or non-object file falls back to the bundle defaults.
*/
export function readToolSearchConfig(
configPath: string = getToolSearchConfigPath(),
): ToolSearchConfig {
const fallback = bundleFallbackConfig();
try {
const parsed: unknown = JSON.parse(readFileSync(configPath, "utf8"));
if (!isObject(parsed)) return fallback;
const config = parsed;
return {
alwaysEnabled: Array.isArray(config.alwaysEnabled) ? stringList(config.alwaysEnabled) : fallback.alwaysEnabled,
showToolSearchFooterStatus:
typeof config.showToolSearchFooterStatus === "boolean"
? config.showToolSearchFooterStatus
: fallback.showToolSearchFooterStatus,
maxActiveGroups: positiveInteger(config.maxActiveGroups, fallback.maxActiveGroups),
maxToolsPerGroup: positiveInteger(config.maxToolsPerGroup, fallback.maxToolsPerGroup),
maxDynamicTools: positiveInteger(config.maxDynamicTools, fallback.maxDynamicTools),
groupOverrides: readGroupOverrides(config.groupOverrides),
};
} catch {
return fallback;
}
}