Remove local context from model requests
This commit is contained in:
110
src/utils/api.ts
110
src/utils/api.ts
@@ -438,39 +438,14 @@ export function appendSystemContext(
|
||||
systemPrompt: SystemPrompt,
|
||||
context: { [k: string]: string },
|
||||
): string[] {
|
||||
return [
|
||||
...systemPrompt,
|
||||
Object.entries(context)
|
||||
.map(([key, value]) => `${key}: ${value}`)
|
||||
.join('\n'),
|
||||
].filter(Boolean)
|
||||
return systemPrompt
|
||||
}
|
||||
|
||||
export function prependUserContext(
|
||||
messages: Message[],
|
||||
context: { [k: string]: string },
|
||||
): Message[] {
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
return messages
|
||||
}
|
||||
|
||||
if (Object.entries(context).length === 0) {
|
||||
return messages
|
||||
}
|
||||
|
||||
return [
|
||||
createUserMessage({
|
||||
content: `<system-reminder>\nAs you answer the user's questions, you can use the following context:\n${Object.entries(
|
||||
context,
|
||||
)
|
||||
.map(([key, value]) => `# ${key}\n${value}`)
|
||||
.join('\n')}
|
||||
|
||||
IMPORTANT: this context may or may not be relevant to your tasks. You should not respond to this context unless it is highly relevant to your task.\n</system-reminder>\n`,
|
||||
isMeta: true,
|
||||
}),
|
||||
...messages,
|
||||
]
|
||||
return messages
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -480,86 +455,7 @@ export async function logContextMetrics(
|
||||
mcpConfigs: Record<string, ScopedMcpServerConfig>,
|
||||
toolPermissionContext: ToolPermissionContext,
|
||||
): Promise<void> {
|
||||
// Early return if logging is disabled
|
||||
if (isAnalyticsDisabled()) {
|
||||
return
|
||||
}
|
||||
const [{ tools: mcpTools }, tools, userContext, systemContext] =
|
||||
await Promise.all([
|
||||
prefetchAllMcpResources(mcpConfigs),
|
||||
getTools(toolPermissionContext),
|
||||
getUserContext(),
|
||||
getSystemContext(),
|
||||
])
|
||||
// Extract individual context sizes and calculate total
|
||||
const gitStatusSize = systemContext.gitStatus?.length ?? 0
|
||||
const claudeMdSize = userContext.claudeMd?.length ?? 0
|
||||
|
||||
// Calculate total context size
|
||||
const totalContextSize = gitStatusSize + claudeMdSize
|
||||
|
||||
// Get file count using ripgrep (rounded to nearest power of 10 for privacy)
|
||||
const currentDir = getCwd()
|
||||
const ignorePatternsByRoot = getFileReadIgnorePatterns(toolPermissionContext)
|
||||
const normalizedIgnorePatterns = normalizePatternsToPath(
|
||||
ignorePatternsByRoot,
|
||||
currentDir,
|
||||
)
|
||||
const fileCount = await countFilesRoundedRg(
|
||||
currentDir,
|
||||
AbortSignal.timeout(1000),
|
||||
normalizedIgnorePatterns,
|
||||
)
|
||||
|
||||
// Calculate tool metrics
|
||||
let mcpToolsCount = 0
|
||||
let mcpServersCount = 0
|
||||
let mcpToolsTokens = 0
|
||||
let nonMcpToolsCount = 0
|
||||
let nonMcpToolsTokens = 0
|
||||
|
||||
const nonMcpTools = tools.filter(tool => !tool.isMcp)
|
||||
mcpToolsCount = mcpTools.length
|
||||
nonMcpToolsCount = nonMcpTools.length
|
||||
|
||||
// Extract unique server names from MCP tool names (format: mcp__servername__toolname)
|
||||
const serverNames = new Set<string>()
|
||||
for (const tool of mcpTools) {
|
||||
const parts = tool.name.split('__')
|
||||
if (parts.length >= 3 && parts[1]) {
|
||||
serverNames.add(parts[1])
|
||||
}
|
||||
}
|
||||
mcpServersCount = serverNames.size
|
||||
|
||||
// Estimate tool tokens locally for analytics (avoids N API calls per session)
|
||||
// Use inputJSONSchema (plain JSON Schema) when available, otherwise convert Zod schema
|
||||
for (const tool of mcpTools) {
|
||||
const schema =
|
||||
'inputJSONSchema' in tool && tool.inputJSONSchema
|
||||
? tool.inputJSONSchema
|
||||
: zodToJsonSchema(tool.inputSchema)
|
||||
mcpToolsTokens += roughTokenCountEstimation(jsonStringify(schema))
|
||||
}
|
||||
for (const tool of nonMcpTools) {
|
||||
const schema =
|
||||
'inputJSONSchema' in tool && tool.inputJSONSchema
|
||||
? tool.inputJSONSchema
|
||||
: zodToJsonSchema(tool.inputSchema)
|
||||
nonMcpToolsTokens += roughTokenCountEstimation(jsonStringify(schema))
|
||||
}
|
||||
|
||||
logEvent('tengu_context_size', {
|
||||
git_status_size: gitStatusSize,
|
||||
claude_md_size: claudeMdSize,
|
||||
total_context_size: totalContextSize,
|
||||
project_file_count_rounded: fileCount,
|
||||
mcp_tools_count: mcpToolsCount,
|
||||
mcp_servers_count: mcpServersCount,
|
||||
mcp_tools_tokens: mcpToolsTokens,
|
||||
non_mcp_tools_count: nonMcpToolsCount,
|
||||
non_mcp_tools_tokens: nonMcpToolsTokens,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Generalize this to all tools
|
||||
|
||||
Reference in New Issue
Block a user