diff --git a/app/src/main/assets/packages/extended_file_tools.js b/app/src/main/assets/packages/extended_file_tools.js index f883aad423..ea017c2bf1 100644 --- a/app/src/main/assets/packages/extended_file_tools.js +++ b/app/src/main/assets/packages/extended_file_tools.js @@ -18,7 +18,7 @@ "description": { "zh": "检查文件或目录是否存在。", "en": "Check if a file or directory exists." }, "parameters": [ { "name": "path", "description": { "zh": "目标路径", "en": "Target path" }, "type": "string", "required": true }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false } + { "name": "environment", "description": { "zh": "可选:android/linux/repo:<仓库名>", "en": "Optional: android/linux/repo:" }, "type": "string", "required": false } ] }, { @@ -27,7 +27,7 @@ "parameters": [ { "name": "source", "description": { "zh": "源路径", "en": "Source path" }, "type": "string", "required": true }, { "name": "destination", "description": { "zh": "目标路径", "en": "Destination path" }, "type": "string", "required": true }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false } + { "name": "environment", "description": { "zh": "可选:android/linux(移动不支持 repo)", "en": "Optional: android/linux (repo not supported for move)" }, "type": "string", "required": false } ] }, { @@ -36,8 +36,8 @@ "parameters": [ { "name": "source", "description": { "zh": "源路径", "en": "Source path" }, "type": "string", "required": true }, { "name": "destination", "description": { "zh": "目标路径", "en": "Destination path" }, "type": "string", "required": true }, - { "name": "recursive", "description": { "zh": "可选:是否递归,默认 false", "en": "Optional: recursive (default: false)" }, "type": "boolean", "required": false }, - { "name": "source_environment", "description": { "zh": "可选:源环境 android/linux", "en": "Optional: source environment android/linux" }, "type": "string", "required": false }, + { "name": "recursive", "description": { "zh": "可选:递归复制目录,默认 false;复制目录必须传 true", "en": "Optional: recursive copy for directories, default false; required true when copying a directory" }, "type": "boolean", "required": false }, + { "name": "source_environment", "description": { "zh": "可选:源环境 android/linux(复制不支持 repo)", "en": "Optional: source environment android/linux (repo not supported for copy)" }, "type": "string", "required": false }, { "name": "dest_environment", "description": { "zh": "可选:目标环境 android/linux", "en": "Optional: destination environment android/linux" }, "type": "string", "required": false } ] }, @@ -46,7 +46,7 @@ "description": { "zh": "获取文件/目录信息。", "en": "Get file/directory info." }, "parameters": [ { "name": "path", "description": { "zh": "目标路径", "en": "Target path" }, "type": "string", "required": true }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false } + { "name": "environment", "description": { "zh": "可选:android/linux/repo:<仓库名>", "en": "Optional: android/linux/repo:" }, "type": "string", "required": false } ] }, { @@ -55,7 +55,7 @@ "parameters": [ { "name": "source", "description": { "zh": "源路径", "en": "Source path" }, "type": "string", "required": true }, { "name": "destination", "description": { "zh": "输出 zip 文件路径", "en": "Destination zip path" }, "type": "string", "required": true }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false }, + { "name": "environment", "description": { "zh": "可选:android/linux(linux 下路径在 proot 根文件系统内解析;不支持 repo)", "en": "Optional: android/linux (linux paths resolved inside the proot rootfs; repo not supported)" }, "type": "string", "required": false }, { "name": "include_root_directory", "description": { "zh": "压缩目录时是否保留源目录名作为顶层目录,默认 true", "en": "When zipping a directory, keep the source directory name as the top-level folder, default true" }, "type": "boolean", "required": false } ] }, @@ -65,15 +65,15 @@ "parameters": [ { "name": "source", "description": { "zh": "zip 文件路径", "en": "Zip file path" }, "type": "string", "required": true }, { "name": "destination", "description": { "zh": "解压目录", "en": "Destination directory" }, "type": "string", "required": true }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false } + { "name": "environment", "description": { "zh": "可选:android/linux(linux 下路径在 proot 根文件系统内解析;不支持 repo)", "en": "Optional: android/linux (linux paths resolved inside the proot rootfs; repo not supported)" }, "type": "string", "required": false } ] }, { "name": "open_file", "description": { "zh": "用系统默认应用打开文件。", "en": "Open a file with system default app." }, "parameters": [ - { "name": "path", "description": { "zh": "文件路径", "en": "File path" }, "type": "string", "required": true }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false } + { "name": "path", "description": { "zh": "Android 文件系统上的文件路径", "en": "File path on the Android file system" }, "type": "string", "required": true }, + { "name": "environment", "description": { "zh": "可选,仅 android(linux/repo 不支持)", "en": "Optional, android only (linux/repo not supported)" }, "type": "string", "required": false } ] }, { @@ -82,15 +82,22 @@ "parameters": [ { "name": "path", "description": { "zh": "文件路径", "en": "File path" }, "type": "string", "required": true }, { "name": "title", "description": { "zh": "可选:分享标题", "en": "Optional: share title" }, "type": "string", "required": false }, - { "name": "environment", "description": { "zh": "可选:android/linux", "en": "Optional: android/linux" }, "type": "string", "required": false } + { "name": "environment", "description": { "zh": "可选,仅 android(linux/repo 不支持)", "en": "Optional, android only (linux/repo not supported)" }, "type": "string", "required": false } ] } ] }*/ const ExtendedFileTools = (function () { async function file_exists(params) { + // 修改意图:原先 success 误用 result.exists,导致"文件不存在"这一正常查询结果 + // 被包装成失败;success 只表示查询本身是否成功,存在性结论放进 message/data const result = await Tools.Files.exists(params.path, params.environment); - return { success: !!result && (result.exists ?? true), message: '检查完成', data: result }; + const exists = !!result && (result.exists ?? false); + return { + success: !!result, + message: exists ? '文件或目录存在' : '文件或目录不存在(这是正常的查询结果,不是错误)', + data: result, + }; } async function move_file(params) { const result = await Tools.Files.move(params.source, params.destination, params.environment); diff --git a/app/src/main/java/com/ai/assistance/operit/api/chat/EnhancedAIService.kt b/app/src/main/java/com/ai/assistance/operit/api/chat/EnhancedAIService.kt index a9cf6b6165..377dc59826 100644 --- a/app/src/main/java/com/ai/assistance/operit/api/chat/EnhancedAIService.kt +++ b/app/src/main/java/com/ai/assistance/operit/api/chat/EnhancedAIService.kt @@ -13,6 +13,7 @@ import com.ai.assistance.operit.api.chat.enhance.FileBindingService import com.ai.assistance.operit.api.chat.enhance.MultiServiceManager import com.ai.assistance.operit.api.chat.enhance.ToolExecutionManager import com.ai.assistance.operit.api.chat.enhance.SubtaskExecutionState +import com.ai.assistance.operit.core.tools.HighRiskToolCatalog import com.ai.assistance.operit.api.chat.llmprovider.AIService import com.ai.assistance.operit.core.chat.logMessageTiming import com.ai.assistance.operit.core.chat.messageTimingNow @@ -269,32 +270,15 @@ class EnhancedAIService private constructor(private val context: Context) { * 处理文件绑定操作(非实例化方式) * @param context 应用上下文 * @param originalContent 原始文件内容 - * @param aiGeneratedCode AI生成的代码(包含"//existing code"标记) - * @return 混合后的文件内容 + * @param operations 结构化编辑操作列表 + * @return FileEditApplyResult */ - suspend fun applyFileBinding( - context: Context, - originalContent: String, - aiGeneratedCode: String, - onProgress: ((Float, String) -> Unit)? = null - ): Pair { - // 获取EnhancedAIService实例 - val instance = getInstance(context) - - // 委托给FileBindingService处理 - return instance.fileBindingService.processFileBinding( - originalContent, - aiGeneratedCode, - onProgress - ) - } - suspend fun applyFileBindingOperations( context: Context, originalContent: String, operations: List, onProgress: ((Float, String) -> Unit)? = null - ): Pair { + ): FileBindingService.FileEditApplyResult { val instance = getInstance(context) return instance.fileBindingService.processFileBindingOperations( originalContent = originalContent, @@ -3104,17 +3088,38 @@ class EnhancedAIService private constructor(private val context: Context) { return null } + // 修改意图:function-calling 路径为可审查工具补挂 call_purpose 调用说明参数文档 + // (文本提示词路径由 SystemToolPrompts.buildToolPromptCategories 注入) + val toolsWithCallPurpose = withCallPurposeParamDoc(hookedTools) + AppLogger.d( TAG, - "Tool Call已启用,提供 ${hookedTools.size} 个工具 (base=${selectedTools.size}, enableTools=$enableTools, visibleToolOverrides=${toolPromptVisibility.size}, roleCardCustomTools=${roleCardToolAccess.customEnabled})" + "Tool Call已启用,提供 ${toolsWithCallPurpose.size} 个工具 (base=${selectedTools.size}, enableTools=$enableTools, visibleToolOverrides=${toolPromptVisibility.size}, roleCardCustomTools=${roleCardToolAccess.customEnabled})" ) - hookedTools + toolsWithCallPurpose } catch (e: Exception) { AppLogger.e(TAG, "获取工具列表失败", e) null } } + /** 修改意图:为可审查工具(高危工具 + 文件工具族)的 function-calling schema 补挂 call_purpose 参数 */ + private fun withCallPurposeParamDoc(tools: List): List = + tools.map { tool -> + if (!HighRiskToolCatalog.isReviewableTool(tool.name)) { + tool + } else { + val structured = tool.parametersStructured ?: emptyList() + if (structured.any { it.name == HighRiskToolCatalog.CALL_PURPOSE_PARAM }) { + tool + } else { + tool.copy( + parametersStructured = structured + HighRiskToolCatalog.callPurposeParamSchema() + ) + } + } + } + // --- Service Lifecycle Management --- /** 启动或更新前台服务为“AI 正在运行”状态,以保持应用活跃 */ @@ -3216,22 +3221,6 @@ class EnhancedAIService private constructor(private val context: Context) { ActivityLifecycleManager.checkAndApplyKeepScreenOn(false) } - /** - * 处理文件绑定操作(实例方法) - * @param originalContent 原始文件内容 - * @param aiGeneratedCode AI生成的代码(包含"//existing code"标记) - * @return 混合后的文件内容 - */ - suspend fun applyFileBinding( - originalContent: String, - aiGeneratedCode: String - ): Pair { - return fileBindingService.processFileBinding( - originalContent, - aiGeneratedCode - ) - } - /** * 翻译文本功能 * @param text 要翻译的文本 diff --git a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileBindingService.kt b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileBindingService.kt index 8c9dffe5ba..7c722e3f4e 100644 --- a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileBindingService.kt +++ b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileBindingService.kt @@ -2,7 +2,6 @@ package com.ai.assistance.operit.api.chat.enhance import android.content.Context import com.ai.assistance.operit.util.AppLogger -import com.ai.assistance.operit.data.model.FunctionType import com.github.difflib.DiffUtils import com.github.difflib.UnifiedDiffUtils import java.util.concurrent.* @@ -12,11 +11,14 @@ class FileBindingService(context: Context) { companion object { private const val TAG = "FileBindingService" - private val EDIT_BLOCK_REGEX = - """\[START-(REPLACE|DELETE)\]\s*\n(.*?)\[END-\1\]""".toRegex( - RegexOption.DOT_MATCHES_ALL - ) - private const val PARALLEL_MIN_ITERATIONS = 2000 + + // 修改意图:read_file/read_file_part 的输出带 "N| " 行号前缀,模型常把整段输出粘贴进 + // old;匹配引擎的空白归一化不去除数字与竖线,前缀会同时破坏精确匹配与 n-gram 相似度。 + // 在编辑入口按行剥离该前缀(仅作用于 old;new 是模型新写的内容,保持原样)。 + private val LINE_NUMBER_PREFIX_REGEX = Regex("(?m)^\\s*\\d+\\| ") + + /** 模糊匹配接受阈值 */ + private const val FUZZY_ACCEPT_THRESHOLD = 0.9 } private enum class EditAction { @@ -29,16 +31,34 @@ class FileBindingService(context: Context) { DELETE } + /** + * @param occurrence 显式指定的第 N 处匹配(1-based);null 表示未指定—— + * 多候选时未指定 occurrence 一律拒绝,绝不静默选第一处 + */ data class StructuredEditOperation( val action: StructuredEditAction, val oldContent: String, - val newContent: String = "" + val newContent: String = "", + val occurrence: Int? = null + ) + + /** + * 编辑应用结果。 + * 失败约定沿用既有契约:diff 以 "Error" 开头、mergedContent 为原始内容, + * 调用方(StandardFileSystemTools.applyFile)据此识别失败。 + */ + data class FileEditApplyResult( + val mergedContent: String, + val diff: String, + /** 每个 op 的实际匹配位置(1-based 行号区间),成功时非空 */ + val appliedRanges: List ) private data class EditOperation( val action: EditAction, val oldContent: String, - val newContent: String + val newContent: String, + val occurrence: Int? ) private data class MatchSearchResult( @@ -48,101 +68,73 @@ class FileBindingService(context: Context) { val sizeDiff: Int, val lengthDiff: Int, val windows: Int, - val lcsCalculations: Int + val lcsCalculations: Int, + /** 本线程收集到的全部高相似窗口(start, end, score),供跨线程聚类 */ + val highScoreWindows: List> ) + /** 一个独立的候选位置(1 行号区间 0-based inclusive) */ + private data class MatchCandidate(val start: Int, val end: Int, val score: Double) + + private class MatchOutcome( + /** 最佳命中(0-based inclusive);未命中为 -1 */ + val start: Int, + val end: Int, + val score: Double, + /** 命中来自精确匹配(归一化后完全一致) */ + val exact: Boolean, + /** 独立候选位置,按得分降序;精确匹配时为全部出现位置(均 1.0) */ + val candidates: List, + /** 未命中时的参考信息:最高相似度与其位置 */ + val bestRejectedScore: Double, + val bestRejectedRange: Pair, + ) { + val matched: Boolean get() = start >= 0 + } + /** - * Processes file binding by applying structured edit blocks. - * This new approach abandons line numbers and sub-agents in favor of fuzzy content matching. - * - * 1. Parses `[START-REPLACE]` or `[START-DELETE]` blocks from the AI-generated code. - * 2. For each block, it uses the content of the `[OLD]` section as a search pattern. - * 3. It performs a fuzzy match of the `[OLD]` content against the `originalContent` to find the - * exact lines to be modified, ignoring whitespace and newlines. - * 4. Once the correct range is identified, it applies the `REPLACE` or `DELETE` operation. - * 5. If no structured blocks are found, it defaults to a full file replacement. + * 对一组结构化编辑操作执行"精确优先、模糊兜底"的匹配并应用。 * - * @param originalContent The original content of the file. - * @param aiGeneratedCode The AI-generated code, containing either edit blocks or full content. - * @return A Pair containing the final merged content and a diff string. + * @return [FileEditApplyResult]:成功时 mergedContent 为编辑后内容、diff 为统一 diff、 + * appliedRanges 为各 op 的匹配行号;失败时 mergedContent 为原内容、diff 以 "Error" 开头。 */ - suspend fun processFileBinding( - originalContent: String, - aiGeneratedCode: String, - onProgress: ((Float, String) -> Unit)? = null - ): Pair { - if (originalContent.isNotEmpty() && !aiGeneratedCode.contains("[START-")) { - val errorMsg = - "If you want to rewrite an entire existing file: please delete_file first then use apply_file with type=create (do not overwrite directly)." + - "If you want to modify a file: please use apply_file with type=replace/delete and provide old/new (or old)." - AppLogger.w(TAG, "Refusing full overwrite for existing content without structured edit blocks. $errorMsg") - return Pair(originalContent, errorMsg) - } - - if (aiGeneratedCode.contains("[START-")) { - onProgress?.invoke(0f, "Parsing patch...") - AppLogger.d(TAG, "Structured edit blocks detected. Attempting fuzzy patch.") - try { - val (success, resultString) = applyFuzzyPatch(originalContent, aiGeneratedCode, onProgress) - if (success) { - onProgress?.invoke(1f, "Patch applied") - AppLogger.d(TAG, "Fuzzy patch succeeded.") - val diffString = generateDiff(originalContent.replace("\r\n", "\n"), resultString) - return Pair(resultString, diffString) - } else { - AppLogger.w(TAG, "Fuzzy patch application failed. Reason: $resultString") - return Pair(originalContent, "Error: Could not apply patch. Reason: $resultString") - } - } catch (e: Exception) { - AppLogger.e(TAG, "Error during fuzzy patch process.", e) - return Pair(originalContent, "Error: An unexpected exception occurred during the patching process: ${e.message}") - } - } - - // Default to full file replacement if no special instructions are found - AppLogger.d(TAG, "No structured blocks found. Assuming full file replacement.") - val normalizedOriginalContent = originalContent.replace("\r\n", "\n") - val normalizedAiGeneratedCode = aiGeneratedCode.replace("\r\n", "\n").trim() - val diffString = generateDiff(normalizedOriginalContent, normalizedAiGeneratedCode) - return Pair(normalizedAiGeneratedCode, diffString) - } - suspend fun processFileBindingOperations( originalContent: String, operations: List, onProgress: ((Float, String) -> Unit)? = null - ): Pair { + ): FileEditApplyResult { if (operations.isEmpty()) { - return Pair(originalContent, "Error: No valid edit operations provided") + return FileEditApplyResult(originalContent, "Error: No valid edit operations provided", emptyList()) } val internalOps = operations.mapNotNull { op -> - val old = op.oldContent + // 修改意图:剥离模型从 read_file/read_file_part 输出整段粘贴带来的 "N| " 行号前缀 + val old = op.oldContent.replace(LINE_NUMBER_PREFIX_REGEX, "") if (old.isBlank()) return@mapNotNull null when (op.action) { StructuredEditAction.REPLACE -> { val newContent = op.newContent if (newContent.isBlank()) return@mapNotNull null - EditOperation(EditAction.REPLACE, old, newContent) + EditOperation(EditAction.REPLACE, old, newContent, op.occurrence) } StructuredEditAction.DELETE -> { - EditOperation(EditAction.DELETE, old, "") + EditOperation(EditAction.DELETE, old, "", op.occurrence) } } } if (internalOps.isEmpty()) { - return Pair(originalContent, "Error: No valid edit operations provided") + return FileEditApplyResult(originalContent, "Error: No valid edit operations provided", emptyList()) } onProgress?.invoke(0f, "Searching match...") - val (success, resultString) = applyFuzzyOperations(originalContent, internalOps, onProgress) + val (success, mergedContent, message, appliedRanges) = applyFuzzyOperations(originalContent, internalOps, onProgress) if (success) { onProgress?.invoke(1f, "Patch applied") - val diffString = generateDiff(originalContent.replace("\r\n", "\n"), resultString) - return Pair(resultString, diffString) + val diffString = generateDiff(originalContent.replace("\r\n", "\n"), mergedContent) + return FileEditApplyResult(mergedContent, diffString, appliedRanges) } - return Pair(originalContent, "Error: Could not apply patch. Reason: $resultString") + return FileEditApplyResult(originalContent, "Error: Could not apply patch. Reason: $message", emptyList()) } private fun generateDiff(original: String, modified: String): String { @@ -182,7 +174,6 @@ class FileBindingService(context: Context) { } } sb.appendLine("Changes: +$additions -$deletions lines") - // sb.appendLine() // Generate a standard unified diff to process val unifiedDiffLines = UnifiedDiffUtils.generateUnifiedDiff( @@ -228,60 +219,83 @@ class FileBindingService(context: Context) { return sb.toString() } - /** - * Applies a patch based on fuzzy matching of the `[OLD]` content block. - * This method is the core of the new, more robust patching mechanism. - * - * @return A Pair of (Boolean, String) indicating success and the modified content, or failure - * and a detailed error message. - */ - private fun applyFuzzyPatch( - originalContent: String, - aiPatchCode: String, - onProgress: ((Float, String) -> Unit)? = null - ): Pair { - try { - val operations = parseEditOperations(aiPatchCode) - if (operations.isEmpty()) { - return Pair(false, "No valid edit operations found in the patch code.") - } - - return applyFuzzyOperations(originalContent, operations, onProgress) - } catch (e: Exception) { - AppLogger.e(TAG, "Failed to apply fuzzy patch", e) - return Pair(false, "Failed to apply fuzzy patch due to an exception: ${e.message}") - } - } + private data class FuzzyApplyOutcome( + val success: Boolean, + val mergedContent: String, + val message: String, + val appliedRanges: List + ) private fun applyFuzzyOperations( originalContent: String, operations: List, onProgress: ((Float, String) -> Unit)? = null - ): Pair { + ): FuzzyApplyOutcome { onProgress?.invoke(0f, "Searching match...") val originalLines = originalContent.lines().toMutableList() val enrichedOps = mutableListOf>() + val appliedRanges = mutableListOf() val totalOps = operations.size.coerceAtLeast(1) val matchPhaseWeight = 0.8f val applyPhaseWeight = 0.2f for ((index, op) in operations.withIndex()) { - val (start, end) = findBestMatchRange(originalLines, op.oldContent) { p, msg -> + val outcome = findBestMatchRange(originalLines, op.oldContent) { p, msg -> val overall = (matchPhaseWeight * ((index.toFloat() + p) / totalOps.toFloat())) .coerceIn(0f, 0.99f) onProgress?.invoke(overall, "Matching ${index + 1}/$totalOps: $msg") } - if (start == -1) { + + if (!outcome.matched) { AppLogger.w(TAG, "Could not find a suitable match for OLD block: ${op.oldContent.take(100)}...") - return Pair(false, "Could not find a match for an OLD block. The file may have changed too much.") - } - if (hasMultiplePerfectMatches(originalContent, op.oldContent)) { - AppLogger.w(TAG, "Multiple perfect matches found for OLD block; aborting to avoid ambiguous replacement.") - return Pair(false, "Found multiple perfect matches for an OLD block in the target file. Please refine the patch so it only matches a single location.") + val bestHint = + if (outcome.bestRejectedScore > 0.0) { + val (bs, be) = outcome.bestRejectedRange + " (best candidate: lines ${bs + 1}-${be + 1}, similarity ${(outcome.bestRejectedScore * 100).toInt()}%, below the ${(FUZZY_ACCEPT_THRESHOLD * 100).toInt()}% threshold)" + } else { + "" + } + return FuzzyApplyOutcome( + false, + originalContent, + "Could not find a match for an OLD block$bestHint. Re-read the target lines with read_file_part, refine the old block, then retry." + + " Do NOT fall back to shell commands to edit files.", + emptyList() + ) } + + val candidates = outcome.candidates + val chosen = + if (candidates.size == 1) { + candidates[0] + } else if (op.occurrence != null && op.occurrence in 1..candidates.size) { + candidates[op.occurrence - 1] + } else { + // 修改意图:多候选时绝不静默选第一处(旧实现只拒绝"完美重复",0.9-0.99 + // 相似度的近似重复会被静默替换为最优窗,存在改错位置的风险)。 + // 未指定 occurrence 一律拒绝并列出各候选行号,模型可补上下文或指定 occurrence + val listing = candidates.joinToString("; ") { c -> + "lines ${c.start + 1}-${c.end + 1} (${(c.score * 100).toInt()}%)" + } + AppLogger.w(TAG, "Multiple match locations for OLD block; aborting to avoid ambiguous replacement.") + return FuzzyApplyOutcome( + false, + originalContent, + "The OLD block matches ${candidates.size} distinct locations: $listing. " + + "Add more surrounding context to make it unique, or pass occurrence=1..${candidates.size} to target the Nth match.", + emptyList() + ) + } + + val start = chosen.start + val end = chosen.end enrichedOps.add(Triple(op, start, end)) + appliedRanges.add( + "${op.action.name.lowercase()} → lines ${start + 1}-${end + 1} (${(chosen.score * 100).toInt()}%" + + (if (outcome.exact) ", exact" else ", fuzzy") + ")" + ) } // Sort operations by start line in descending order to apply from the bottom up @@ -332,7 +346,7 @@ class FileBindingService(context: Context) { onProgress?.invoke(overall, "Applying ${applied}/$totalOps") } - return Pair(true, originalLines.joinToString("\n")) + return FuzzyApplyOutcome(true, originalLines.joinToString("\n"), "", appliedRanges) } private fun tryApplyBoundaryPreservingEdit( @@ -382,100 +396,37 @@ class FileBindingService(context: Context) { return firstIndex } - private fun parseEditOperations(patchCode: String): List { - val operations = mutableListOf() - val lines = patchCode.lines() - var i = 0 - - while (i < lines.size) { - val line = lines[i].trim() - if (line.startsWith("[START-")) { - val header = line - val actionStr = header.substringAfter("[START-").substringBefore("]") - - val action = try { - EditAction.valueOf(actionStr) - } catch (e: IllegalArgumentException) { - i++ - continue // Skip invalid action - } - - var oldContent = "" - var newContent = "" - var inBlock: String? = null - i++ // Move to content - - while (i < lines.size && !lines[i].trim().startsWith("[END-$actionStr]")) { - val currentLine = lines[i] - val trimmedLine = currentLine.trim() - - if (trimmedLine.startsWith("[OLD]")) { - inBlock = "OLD" - val inline = currentLine.substringAfter("[OLD]", "") - if (inline.isNotEmpty()) { - oldContent += inline + "\n" - } - } else if (trimmedLine.startsWith("[NEW]")) { - inBlock = "NEW" - val inline = currentLine.substringAfter("[NEW]", "") - if (inline.isNotEmpty()) { - newContent += inline + "\n" - } - } else if (trimmedLine.startsWith("[/OLD]")) inBlock = null - else if (trimmedLine.startsWith("[/NEW]")) inBlock = null - else { - when (inBlock) { - "OLD" -> oldContent += currentLine + "\n" - "NEW" -> newContent += currentLine + "\n" - } - } - i++ - } - - val normalizedOld = oldContent.removeSuffix("\n").removeSuffix("\r") - val normalizedNew = newContent.removeSuffix("\n").removeSuffix("\r") - - // Basic validation - if ((action == EditAction.REPLACE || action == EditAction.DELETE) && normalizedOld.isBlank()) { - i++ - continue // Skip invalid operation - } - if (action == EditAction.REPLACE && normalizedNew.isBlank()) { - i++ - continue // Skip invalid operation - } - - operations.add(EditOperation(action, normalizedOld, normalizedNew)) - } - i++ - } - return operations - } - + /** + * 查找 old 块的匹配位置。 + * + * 修改意图(三处结构性修正): + * 1. 精确匹配快速路径——归一化后 indexOf 唯一命中直接返回,跳过滑动窗模糊搜索; + * 旧实现即使完美匹配也要跑完整并行扫描。 + * 2. 多候选拒绝——模糊搜索收集全部 >0.9 的窗口并按行区间重叠聚类为"独立位置", + * 独立位置 ≥2 时交由调用方(applyFuzzyOperations)按 occurrence 语义处理; + * 旧实现只拒绝"完美重复",近似重复会被静默替换为最优窗(可能改错位置)。 + * 3. 失败可诊断——未命中时回传最高相似度与其行号,供错误信息指导模型重试。 + */ private fun findBestMatchRange( originalLines: List, oldContent: String, onProgress: ((Float, String) -> Unit)? = null - ): Pair { + ): MatchOutcome { val oldContentLines = oldContent.lines() val numOldLines = oldContentLines.size - if (numOldLines == 0) return -1 to -1 - if (originalLines.isEmpty()) return -1 to -1 + if (numOldLines == 0 || originalLines.isEmpty()) { + return MatchOutcome(-1, -1, 0.0, false, emptyList(), 0.0, -1 to -1) + } AppLogger.d(TAG, "开始查找最佳匹配范围,原始文件行数: ${originalLines.size}, 目标块行数: $numOldLines") val startTime = System.currentTimeMillis() var totalWindows = 0 var lcsCalculations = 0 - // --- 优化1:预计算与规范化 --- - AppLogger.d(TAG, "开始预计算与规范化...") + // --- 预计算与规范化 --- val normalizedOldContent = oldContent.replace(Regex("\\s+"), "") val normalizedOldLength = normalizedOldContent.length val baseNgrams = buildNgrams(normalizedOldContent) - if (baseNgrams.isEmpty()) { - AppLogger.w(TAG, "OLD 块在去空白后过短,无法构建 n-gram,放弃匹配。") - return -1 to -1 - } val lineStartIndices = mutableListOf() val normalizedOriginalContent = buildString { @@ -488,9 +439,54 @@ class FileBindingService(context: Context) { } lineStartIndices.add(length) // 添加一个末尾索引,方便计算最后一行 } - AppLogger.d(TAG, "预计算完成,规范化后字符数: ${normalizedOriginalContent.length}") - // --- 阶段一:计算目标窗口尺寸范围 --- + /** 字符索引 → 行索引(lineStartIndices 升序,二分找最后一个 <= charIndex 的行起点) */ + fun lineOfChar(charIndex: Int): Int { + var lo = 0 + var hi = lineStartIndices.size - 1 + while (lo < hi) { + val mid = (lo + hi + 1) / 2 + if (lineStartIndices[mid] <= charIndex) lo = mid else hi = mid - 1 + } + return lo + } + + // --- 精确匹配快速路径 --- + if (normalizedOldLength > 0) { + val exactPositions = mutableListOf() + var scanIndex = normalizedOriginalContent.indexOf(normalizedOldContent) + while (scanIndex >= 0) { + exactPositions.add(scanIndex) + scanIndex = normalizedOriginalContent.indexOf(normalizedOldContent, scanIndex + normalizedOldLength) + } + if (exactPositions.isNotEmpty()) { + val candidates = exactPositions.map { charStart -> + val charEnd = charStart + normalizedOldLength + val startLine = lineOfChar(charStart) + val endLine = lineOfChar(charEnd - 1).coerceAtLeast(startLine) + MatchCandidate(startLine, endLine, 1.0) + } + AppLogger.d(TAG, "精确匹配命中 ${candidates.size} 处(归一化后完全一致)。") + return MatchOutcome( + start = candidates[0].start, + end = candidates[0].end, + score = 1.0, + exact = true, + candidates = candidates, + bestRejectedScore = 0.0, + bestRejectedRange = -1 to -1, + ) + } + // 精确未命中:old 去空白后过短则连 n-gram 都无法构建,直接失败 + if (baseNgrams.isEmpty()) { + AppLogger.w(TAG, "OLD 块在去空白后过短,无法构建 n-gram 且无精确匹配,放弃匹配。") + return MatchOutcome(-1, -1, 0.0, false, emptyList(), 0.0, -1 to -1) + } + } else { + return MatchOutcome(-1, -1, 0.0, false, emptyList(), 0.0, -1 to -1) + } + + // --- 模糊路径:滑动窗口尺寸范围 --- val delta = (numOldLines * 0.2).toInt() + 2 // 扩大到20%的容错范围,并确保至少有2行的浮动 val targetSizes = (maxOf(1, numOldLines - delta))..(numOldLines + delta) @@ -499,9 +495,9 @@ class FileBindingService(context: Context) { var bestMatchSizeDiff = Int.MAX_VALUE var bestMatchLengthDiff = Int.MAX_VALUE - // --- 阶段二:并行滑动窗口搜索 --- + // --- 并行滑动窗口搜索 --- val totalIterations = originalLines.size.toLong() * targetSizes.count().toLong() - AppLogger.d(TAG, "开始滑动窗口匹配(并行),总迭代次数: $totalIterations") + AppLogger.d(TAG, "精确未命中,开始滑动窗口匹配(并行),总迭代次数: $totalIterations") val processedWindows = java.util.concurrent.atomic.AtomicLong(0L) val lastProgressEmitMs = java.util.concurrent.atomic.AtomicLong(0L) @@ -520,9 +516,9 @@ class FileBindingService(context: Context) { val availableCores = Runtime.getRuntime().availableProcessors().coerceAtLeast(2) val threadCount = minOf(availableCores, originalLines.size) val segmentSize = (originalLines.size + threadCount - 1) / threadCount - val foundPerfectMatch = java.util.concurrent.atomic.AtomicBoolean(false) val executor = java.util.concurrent.Executors.newFixedThreadPool(threadCount) + val allHighScoreWindows = mutableListOf>() try { val tasks = mutableListOf>() @@ -540,17 +536,12 @@ class FileBindingService(context: Context) { var localWindows = 0 var localLcs = 0 var localChunk = 0L + // 修改意图:收集本线程所有 >0.9 的窗口(上限防退化), + // 合并后按行区间重叠聚类出"独立匹配位置" + val localHighScoreWindows = mutableListOf>() for (i in startLine until endExclusive) { - if (foundPerfectMatch.get()) { - break - } - for (size in targetSizes) { - if (foundPerfectMatch.get()) { - break - } - val endLine = i + size if (endLine > originalLines.size) { break @@ -575,6 +566,10 @@ class FileBindingService(context: Context) { localLcs++ val score = ngramSimilarity(baseNgrams, normalizedWindow) + if (score > FUZZY_ACCEPT_THRESHOLD && localHighScoreWindows.size < 5000) { + localHighScoreWindows.add(Triple(i, endLine - 1, score)) + } + val isBetter = (score > localBestScore) || (score == localBestScore && @@ -595,20 +590,6 @@ class FileBindingService(context: Context) { TAG, "并行块[$threadIndex] 发现更佳匹配: 行 ${i + 1}-$endLine, 相似度: $matchPercentage%" ) - - if (localBestScore == 1.0 && localBestSizeDiff == 0 && localBestLengthDiff == 0) { - foundPerfectMatch.set(true) - AppLogger.d(TAG, "并行块[$threadIndex] 已找到100%匹配,提前结束该块搜索。") - return@Callable MatchSearchResult( - localBestScore, - localBestStart, - localBestEnd, - localBestSizeDiff, - localBestLengthDiff, - localWindows, - localLcs - ) - } } } } @@ -625,7 +606,8 @@ class FileBindingService(context: Context) { localBestSizeDiff, localBestLengthDiff, localWindows, - localLcs + localLcs, + localHighScoreWindows ) } @@ -637,6 +619,7 @@ class FileBindingService(context: Context) { val result = future.get() totalWindows += result.windows lcsCalculations += result.lcsCalculations + allHighScoreWindows.addAll(result.highScoreWindows) val isBetter = (result.bestScore > bestMatchScore) || @@ -663,24 +646,66 @@ class FileBindingService(context: Context) { // 记录最终结果 val totalTime = (System.currentTimeMillis() - startTime) / 1000.0 - val result = if (bestMatchScore > 0.9) { - - val (start, end) = bestMatchRange - AppLogger.d( - TAG, - "匹配完成! 最佳匹配: 行 ${start + 1}-${end + 1}, 相似度: ${(bestMatchScore * 100).toInt()}%, " + - "总耗时: ${String.format("%.2f", totalTime)}s, " + - "总窗口数: $totalWindows, 总LCS计算: $lcsCalculations" + AppLogger.d( + TAG, + "匹配完成! 最佳相似度: ${(bestMatchScore * 100).toInt()}%, " + + "总耗时: ${String.format("%.2f", totalTime)}s, " + + "总窗口数: $totalWindows, 总LCS计算: $lcsCalculations" + ) + onProgress?.invoke(1f, "Search done") + + if (bestMatchScore <= FUZZY_ACCEPT_THRESHOLD) { + AppLogger.w(TAG, "未找到足够好的匹配 (最高相似度: ${(bestMatchScore * 100).toInt()}% < ${(FUZZY_ACCEPT_THRESHOLD * 100).toInt()}%)") + return MatchOutcome( + -1, -1, bestMatchScore, false, emptyList(), + bestRejectedScore = bestMatchScore, + bestRejectedRange = bestMatchRange, ) - bestMatchRange - } else { - AppLogger.w(TAG, "未找到足够好的匹配 (最高相似度: ${(bestMatchScore * 100).toInt()}% < 90%)") - -1 to -1 } - onProgress?.invoke(1f, "Search done") + // 高相似窗口按行区间重叠聚类为"独立匹配位置",每簇取最优窗 + val candidates = clusterOverlappingWindows(allHighScoreWindows) + val top = candidates.first() + AppLogger.d(TAG, "模糊匹配命中 ${candidates.size} 个独立位置,最优: 行 ${top.start + 1}-${top.end + 1}") + return MatchOutcome( + start = top.start, + end = top.end, + score = top.score, + exact = false, + candidates = candidates, + bestRejectedScore = 0.0, + bestRejectedRange = -1 to -1, + ) + } + + /** 将高相似窗口按行区间重叠聚类为独立位置,每簇保留得分最高的窗口,按得分降序返回 */ + private fun clusterOverlappingWindows( + windows: List> + ): List { + if (windows.isEmpty()) return emptyList() + val sorted = windows.sortedWith(compareBy({ it.first }, { it.second })) + val candidates = mutableListOf() + + var clusterBest = sorted[0] + var clusterEnd = sorted[0].second + fun flush() { + candidates.add(MatchCandidate(clusterBest.first, clusterBest.second, clusterBest.third)) + } + + for (window in sorted.drop(1)) { + if (window.first <= clusterEnd) { + // 与当前簇重叠,归入同簇(传递闭包) + if (window.third > clusterBest.third) clusterBest = window + clusterEnd = maxOf(clusterEnd, window.second) + } else { + flush() + clusterBest = window + clusterEnd = window.second + } + } + flush() - return result + return candidates.sortedByDescending { it.score } } private fun buildNgrams(s: String, n: Int = 3): Set { @@ -700,23 +725,4 @@ class FileBindingService(context: Context) { return if (union == 0) 0.0 else intersection.toDouble() / union.toDouble() } - - private fun hasMultiplePerfectMatches(originalContent: String, oldContent: String): Boolean { - val normalizedOld = oldContent.replace(Regex("\\s+"), "") - if (normalizedOld.isEmpty()) return false - - val normalizedOriginal = originalContent.replace(Regex("\\s+"), "") - var count = 0 - var index = normalizedOriginal.indexOf(normalizedOld) - while (index >= 0) { - count++ - if (count > 1) { - return true - } - index = normalizedOriginal.indexOf(normalizedOld, index + normalizedOld.length) - } - return false - } - - private fun String.trimTrailingNewline(): String = this.trimEnd('\n', '\r') } diff --git a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileDistillService.kt b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileDistillService.kt new file mode 100644 index 0000000000..a267c1661d --- /dev/null +++ b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/FileDistillService.kt @@ -0,0 +1,622 @@ +package com.ai.assistance.operit.api.chat.enhance + +import com.ai.assistance.operit.api.chat.EnhancedAIService +import com.ai.assistance.operit.core.chat.hooks.PromptTurn +import com.ai.assistance.operit.core.chat.hooks.PromptTurnKind +import com.ai.assistance.operit.core.config.FunctionalPrompts +import com.ai.assistance.operit.core.tools.FileContentData +import com.ai.assistance.operit.core.tools.StringResultData +import com.ai.assistance.operit.core.tools.ToolExecutionLimits +import com.ai.assistance.operit.core.tools.defaultTool.PathValidator +import com.ai.assistance.operit.core.tools.defaultTool.standard.StandardFileSystemTools +import com.ai.assistance.operit.data.model.AITool +import com.ai.assistance.operit.data.model.FunctionType +import com.ai.assistance.operit.data.model.ToolResult +import com.ai.assistance.operit.data.preferences.ApiPreferences +import com.ai.assistance.operit.util.AppLogger +import com.ai.assistance.operit.util.FileUtils +import java.io.BufferedReader +import java.io.File +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.TimeoutCancellationException +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.withTimeout +import org.json.JSONObject + +/** + * 修改意图:文件读取 AI 提炼拦截层(docs/TODO/chat_file_tool_hardening/08-AI读取提炼.md)。 + * read_file / read_file_part / read_file_full 默认经功能模型(FunctionType.FILE_DISTILL, + * 未配置时经 FunctionalConfigManager 既有链路默认主模型)提炼出与 focus 相关的带行号摘录, + * 降低长任务中文件读取的上下文占用。 + * + * 透传边界(以下情况一律执行原始读取、行为与现状一致,均属功能范围定义而非降级): + * - 开关关闭(ApiPreferences.FILE_DISTILL_ENABLED) + * - 调用参数 raw=true + * - repo:/linux 环境(提炼扫描基于 java.io.File 直读,仅覆盖默认 Android 本地环境) + * - 特殊类型(图片/PDF/文档/音视频,SPECIAL_FILE_EXTENSIONS 抽取管线一律透传) + * - 文件 ≤ FILE_DISTILL_MIN_BYTES(提炼收益不足以抵消一次模型调用) + * + * 失败路径全部显式:模型超时/异常/JSON 不可解析 → 失败 ToolResult 并指引 raw=true 重读, + * 不做任何回退;focus 缺失 → 失败并要求补传(显式契约)。 + */ +object FileDistillService { + private const val TAG = "FileDistillService" + + /** raw 参数名:true 时直接返回原始内容 */ + private const val PARAM_RAW = "raw" + + /** focus 参数名:AI 提炼模式下描述"想读什么",必填 */ + private const val PARAM_FOCUS = "focus" + + /** 各环境实现统一的截断标记(read_file 32K 截断时追加),用于向提炼模型声明截断状态 */ + private const val TRUNCATION_MARKER = "(file content truncated)" + + /** 提炼结果头部说明,声明行号语义(可与 edit_file 直接配合) */ + private const val DISTILL_HEADER_PREFIX = "[AI distilled read" + + /** 单份摘录的输出上限;超出按字符截断并注明(资源上限,非语义处理) */ + private const val EXCERPT_BUDGET_CHARS = 24_000 + + /** 提炼失败时统一附加的补救指引 */ + private const val RAW_RETRY_HINT = " Set raw=true to read the original content instead." + + /** 拦截的读取工具变体,决定提炼路径 */ + enum class Variant { READ_FILE, READ_FULL, READ_PART } + + // ---------- 对外入口 ---------- + + /** + * read 族工具统一拦截入口。透传判定通过后按变体进入提炼流程; + * [rawRead] 为原始工具执行(含全部既有校验与报错语义),透传与错误场景直接复用 + */ + suspend fun maybeDistillRead( + context: android.content.Context, + tool: AITool, + variant: Variant, + rawRead: suspend () -> ToolResult, + ): ToolResult { + val rawParam = tool.parameters.find { it.name == PARAM_RAW }?.value?.lowercase() + if (rawParam == "true" || rawParam == "1") { + return rawRead() + } + + val enabled = ApiPreferences.getInstance(context).fileDistillEnabledFlow.first() + if (!enabled) { + return rawRead() + } + + // 修改意图:提炼扫描基于 java.io.File 直读,仅覆盖默认 Android 本地环境; + // repo:/linux 环境的文件系统不经应用进程可见,保持原始读取 + val environment = tool.parameters.find { it.name == "environment" }?.value + if (environment != null && + (environment.startsWith("repo:", ignoreCase = true) || + environment.equals("linux", ignoreCase = true)) + ) { + return rawRead() + } + + return when (variant) { + Variant.READ_FILE -> distillReadFile(context, tool, rawRead) + Variant.READ_FULL -> distillReadFull(context, tool, rawRead) + Variant.READ_PART -> distillReadPart(context, tool, rawRead) + } + } + + // ---------- read_file:先原始读取(含特殊类型抽取),再对文本结果提炼 ---------- + + private suspend fun distillReadFile( + context: android.content.Context, + tool: AITool, + rawRead: suspend () -> ToolResult, + ): ToolResult { + val path = tool.parameters.find { it.name == "path" }?.value ?: "" + val result = rawRead() + // 原始读取失败(路径/大小/二进制门禁等)原样透传,错误语义不变 + if (!result.success) return result + val data = result.result as? FileContentData ?: return result + + val file = File(path) + if (path.isBlank() || !file.isFile) return result + // 特殊类型(图片 OCR/PDF 抽取等)结果一律透传,相关行为不改动 + if (StandardFileSystemTools.isSpecialFileExtension(file.extension)) return result + if (file.length() <= ToolExecutionLimits.FILE_DISTILL_MIN_BYTES) return result + + val focus = focusParam(tool) ?: return missingFocusResult(tool.name) + + val truncatedNote = + if (data.content.contains(TRUNCATION_MARKER)) { + "Note: the content below is a truncated prefix of a larger file." + } else { + "The content below is the complete file." + } + + // 修改意图:单次提炼的模型调用/解析异常统一转显式失败结果并指引 raw=true 重读, + // 不冒泡到执行框架(与 scanFile 的失败路径同构) + val verdict = + try { + distillSingle(context, path, focus, data.content, truncatedNote) + } catch (e: TimeoutCancellationException) { + AppLogger.w(TAG, "文件提炼超时: ${tool.name}, $path") + return distillFailedResult(tool.name, "AI distill timed out.") + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + AppLogger.w(TAG, "文件提炼失败: ${tool.name}, ${e.message}") + return distillFailedResult(tool.name, "AI distill failed: ${e.message ?: e.javaClass.simpleName}.") + } + return when (verdict) { + is SingleVerdict.Unrelated -> notFoundResult(tool.name, path, verdict.fileSummary) + // 修改意图:≥一半内容相关时提炼不省 token,按原样返回原始读取结果(含行号) + is SingleVerdict.NotWorth -> result + is SingleVerdict.Excerpt -> + distilledResult(tool.name, path, focus, verdict.excerpt, scanNote = null) + } + } + + // ---------- read_file_full:纯文本自读(带行号),小文件单次提炼、大文件扫描 ---------- + + private suspend fun distillReadFull( + context: android.content.Context, + tool: AITool, + rawRead: suspend () -> ToolResult, + ): ToolResult { + val path = tool.parameters.find { it.name == "path" }?.value ?: "" + if (path.isBlank()) return rawRead() + + val file = File(path) + // 存在性/类型/文本门禁交由原始读取给出与现状一致的报错 + if (!file.exists() || !file.isFile) return rawRead() + // 特殊类型(图片/PDF/文档/音视频)抽取管线一律透传,相关行为不改动 + if (StandardFileSystemTools.isSpecialFileExtension(file.extension)) return rawRead() + if (!FileUtils.isTextLike(file)) return rawRead() + if (file.length() <= ToolExecutionLimits.FILE_DISTILL_MIN_BYTES) return rawRead() + + val focus = focusParam(tool) ?: return missingFocusResult(tool.name) + + if (file.length() <= ToolExecutionLimits.FILE_DISTILL_SINGLE_INPUT_BYTES) { + val numbered = + file.bufferedReader().use { reader -> + readNumberedChunk( + reader, + startLine = 1, + endLineCap = null, + charBudget = ToolExecutionLimits.FILE_DISTILL_SINGLE_INPUT_BYTES, + ) + } + // 修改意图:单次提炼的模型调用/解析异常统一转显式失败结果(与 scanFile 同构) + val verdict = + try { + distillSingle( + context, + path, + focus, + numbered.text, + truncatedNote = "The content below is the complete file.", + ) + } catch (e: TimeoutCancellationException) { + AppLogger.w(TAG, "文件提炼超时: ${tool.name}, $path") + return distillFailedResult(tool.name, "AI distill timed out.") + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + AppLogger.w(TAG, "文件提炼失败: ${tool.name}, ${e.message}") + return distillFailedResult(tool.name, "AI distill failed: ${e.message ?: e.javaClass.simpleName}.") + } + return when (verdict) { + is SingleVerdict.Unrelated -> notFoundResult(tool.name, path, verdict.fileSummary) + // 修改意图:≥一半内容相关时直接返回完整原文(带行号),提炼不省 token + is SingleVerdict.NotWorth -> + ToolResult( + toolName = tool.name, + success = true, + result = + FileContentData( + path = path, + content = numbered.text, + size = file.length() + ), + error = "" + ) + is SingleVerdict.Excerpt -> + distilledResult(tool.name, path, focus, verdict.excerpt, scanNote = null) + } + } + + return scanFile(context, tool, path, file, focus, startLine = 1) + } + + // ---------- read_file_part:扫描协议,start_line/end_line 可选限定扫描范围 ---------- + + private suspend fun distillReadPart( + context: android.content.Context, + tool: AITool, + rawRead: suspend () -> ToolResult, + ): ToolResult { + val path = tool.parameters.find { it.name == "path" }?.value ?: "" + if (path.isBlank()) return rawRead() + PathValidator.validateAndroidPath(path, tool.name)?.let { return it } + + val file = File(path) + // 存在性/类型/文本门禁交由原始读取给出与现状一致的报错 + if (!file.exists() || !file.isFile) return rawRead() + if (!FileUtils.isTextLike(file)) return rawRead() + if (file.length() <= ToolExecutionLimits.FILE_DISTILL_MIN_BYTES) return rawRead() + + val focus = focusParam(tool) ?: return missingFocusResult(tool.name) + val startLine = + tool.parameters.find { it.name == "start_line" }?.value?.toIntOrNull()?.takeIf { it > 0 } + ?: 1 + val endLineCap = tool.parameters.find { it.name == "end_line" }?.value?.toIntOrNull() + // 修改意图:start > end 的非法范围交由原始读取给出与现状一致的校验报错 + if (endLineCap != null && endLineCap < startLine) return rawRead() + + return scanFile(context, tool, path, file, focus, startLine, endLineCap) + } + + // ---------- 扫描协议 ---------- + + /** + * 逐块滚动扫描:每块约 FILE_DISTILL_SCAN_CHUNK_CHARS 字符(带行号)送提炼模型, + * 每轮收集摘录与 continue_scan;结束后若摘录占比过高(大部分内容都相关)则返回 + * 已扫描区域原文。轮数与扫描总量受 ToolExecutionLimits 上限约束,触顶在结果中明示 + * "扫描未到文件尾",不静默截断 + */ + private suspend fun scanFile( + context: android.content.Context, + tool: AITool, + path: String, + file: File, + focus: String, + startLine: Int, + endLineCap: Int? = null, + ): ToolResult { + val excerpt = StringBuilder() + val scannedRaw = StringBuilder() + var scannedChars = 0 + var excerptChars = 0 + var round = 0 + var continueScan = true + var reachedEnd = false + var fileSummary = "" + + try { + file.bufferedReader().use { reader -> + // 跳到 startLine(AI 模式下 start_line 缺省为 1,即全文扫描) + var lineNo = 1 + while (lineNo < startLine) { + reader.readLine() ?: break + lineNo++ + } + + while (round < ToolExecutionLimits.FILE_DISTILL_SCAN_MAX_ROUNDS && + continueScan && + !reachedEnd + ) { + val chunk = + readNumberedChunk( + reader, + startLine = lineNo, + endLineCap = endLineCap, + charBudget = ToolExecutionLimits.FILE_DISTILL_SCAN_CHUNK_CHARS, + ) + val chunkText = chunk.text + if (chunkText.isBlank()) { + reachedEnd = chunk.endReached + break + } + round++ + scannedChars += chunkText.length + scannedRaw.append(chunkText) + // 修改意图:扫描总量受单条工具结果预算约束;触顶后停止后续轮次, + // reachedEnd 保持 false 使结果明示"扫描未到文件尾",不静默截断 + if (scannedChars >= ToolExecutionLimits.MAX_SINGLE_TOOL_RESULT_MESSAGE_CHARS) { + continueScan = false + } + + val response = + withTimeout(ToolExecutionLimits.FILE_DISTILL_SCAN_ROUND_TIMEOUT_MS) { + EnhancedAIService.getInstance(context).callFunctionModel( + functionType = FunctionType.FILE_DISTILL, + turns = + listOf( + PromptTurn( + kind = PromptTurnKind.USER, + content = + FunctionalPrompts.fileDistillScanChunkPrompt( + path = path, + focus = focus, + round = round, + startLine = chunk.firstLine, + endLine = chunk.lastLine, + isLastChunk = chunk.endReached, + hasPriorExcerpt = excerptChars > 0, + chunk = chunkText, + ), + ) + ), + recordTokenUsage = true, + ) + } + + val parsed = parseScanResponse(response) + if (parsed.fileSummary.isNotBlank()) { + fileSummary = parsed.fileSummary + } + if (parsed.related && parsed.excerpt.isNotBlank()) { + val piece = capExcerpt(parsed.excerpt) + excerpt.append(piece).append('\n') + excerptChars += piece.length + } + continueScan = parsed.continueScan && !chunk.endReached + reachedEnd = chunk.endReached + lineNo = chunk.lastLine + 1 + } + } + } catch (e: TimeoutCancellationException) { + AppLogger.w(TAG, "文件提炼扫描超时: ${tool.name}, $path") + return distillFailedResult(tool.name, "AI distill scan timed out.") + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + AppLogger.w(TAG, "文件提炼扫描失败: ${tool.name}, ${e.message}") + return distillFailedResult(tool.name, "AI distill scan failed: ${e.message ?: e.javaClass.simpleName}.") + } + + if (excerptChars == 0) { + return notFoundResult(tool.name, path, fileSummary) + } + + // 修改意图:摘录占比过高说明大部分内容都相关,提炼不省 token, + // 按用户规则直接返回相关原文(此处为已扫描区域原文,保留行号) + if (excerptChars > scannedChars * ToolExecutionLimits.FILE_DISTILL_EXCERPT_RATIO_LIMIT) { + val original = capTail(scannedRaw.toString(), keepChars = 56_000) + val note = + "\n\n(AI distilled read: most of the scanned content matched the focus, " + + "so the original scanned region is returned as-is." + + if (reachedEnd) ")" else " Scan stopped before the end of the file.)" + return ToolResult( + toolName = tool.name, + success = true, + result = + FileContentData( + path = path, + content = original + note, + size = (original.length + note.length).toLong() + ), + error = "" + ) + } + + val scanNote = + if (!reachedEnd) { + "Scan stopped before the end of the file (round/chunk limits); " + + "more relevant content may exist further down." + } else { + null + } + return distilledResult(tool.name, path, focus, excerpt.toString(), scanNote) + } + + // ---------- 单次协议 ---------- + + private sealed class SingleVerdict { + data class Unrelated(val fileSummary: String) : SingleVerdict() + + /** 内容大部分相关,提炼不省 token:调用方返回原文 */ + object NotWorth : SingleVerdict() + + data class Excerpt(val excerpt: String) : SingleVerdict() + } + + private class ScanVerdict( + val related: Boolean, + val excerpt: String, + val continueScan: Boolean, + val fileSummary: String, + ) + + private suspend fun distillSingle( + context: android.content.Context, + path: String, + focus: String, + content: String, + truncatedNote: String, + ): SingleVerdict { + val response = + withTimeout(ToolExecutionLimits.FILE_DISTILL_SINGLE_TIMEOUT_MS) { + EnhancedAIService.getInstance(context).callFunctionModel( + functionType = FunctionType.FILE_DISTILL, + turns = + listOf( + PromptTurn( + kind = PromptTurnKind.USER, + content = + FunctionalPrompts.fileDistillSinglePrompt( + path = path, + focus = focus, + content = content, + truncatedNote = truncatedNote, + ), + ) + ), + recordTokenUsage = true, + ) + } + return parseSingleResponse(response) + } + + // ---------- 结果构造 ---------- + + /** AI 提炼成功结果:头部声明行号语义与 focus,附扫描未完成提示(如有) */ + private fun distilledResult( + toolName: String, + path: String, + focus: String, + excerpt: String, + scanNote: String?, + ): ToolResult { + val header = + "$DISTILL_HEADER_PREFIX of $path; focus: $focus]\n" + + "[Line numbers refer to the original file and can be used with edit_file.]\n" + + "---\n" + val body = header + excerpt + val content = + if (scanNote != null) "$body\n\n($scanNote)" else body + return ToolResult( + toolName = toolName, + success = true, + result = + FileContentData( + path = path, + content = content, + size = content.length.toLong() + ), + error = "" + ) + } + + /** 未找到相关内容:读取本身成功,返回文件概要供主模型决定下一步 */ + private fun notFoundResult(toolName: String, path: String, fileSummary: String): ToolResult { + val content = + "[AI distilled read of $path]\n" + + "No content matching the focus was found in this file.\n" + + "File summary: ${fileSummary.ifBlank { "n/a" }}" + return ToolResult( + toolName = toolName, + success = true, + result = + FileContentData( + path = path, + content = content, + size = content.length.toLong() + ), + error = "" + ) + } + + private fun missingFocusResult(toolName: String): ToolResult = + ToolResult( + toolName = toolName, + success = false, + result = StringResultData(""), + error = + "The focus parameter is required for AI distilled read. " + + "Provide focus describing what you want from the file," + RAW_RETRY_HINT + ) + + private fun distillFailedResult(toolName: String, message: String): ToolResult = + ToolResult( + toolName = toolName, + success = false, + result = StringResultData(""), + error = message + RAW_RETRY_HINT + ) + + // ---------- 参数与解析 ---------- + + private fun focusParam(tool: AITool): String? { + val focus = + tool.parameters.find { it.name == PARAM_FOCUS }?.value?.trim().orEmpty() + return focus.ifBlank { null } + } + + /** 一块带行号的扫描文本:firstLine/lastLine 为起止行号,endReached 表示已读到范围末尾 */ + private class NumberedChunk( + val text: String, + val firstLine: Int, + val lastLine: Int, + val endReached: Boolean, + ) + + /** + * 从 [reader] 读取一块文本并加 "N| " 行号前缀(格式与实现层 addLineNumbers 一致, + * 分段间可直接衔接)。行数达到 [charBudget] 字符、越过 [endLineCap](含)或读至 + * EOF 时停止;endReached 仅在后两种情形为 true + */ + private fun readNumberedChunk( + reader: BufferedReader, + startLine: Int, + endLineCap: Int?, + charBudget: Int, + ): NumberedChunk { + val chunk = StringBuilder() + var lineNo = startLine - 1 + var endReached = false + while (chunk.length < charBudget) { + if (endLineCap != null && lineNo >= endLineCap) { + endReached = true + break + } + val line = reader.readLine() + if (line == null) { + endReached = true + break + } + lineNo++ + chunk.append(lineNo).append("| ").append(line).append('\n') + } + return NumberedChunk( + text = chunk.toString(), + firstLine = startLine, + lastLine = lineNo, + endReached = endReached, + ) + } + + /** 单份摘录超预算时按字符截断并注明(资源上限,非语义处理) */ + private fun capExcerpt(excerpt: String): String = + if (excerpt.length > EXCERPT_BUDGET_CHARS) { + excerpt.take(EXCERPT_BUDGET_CHARS) + "\n... (excerpt truncated at budget) ..." + } else { + excerpt + } + + /** 保留尾部 keepChars 字符;被裁掉的前缀以省略标记明示 */ + private fun capTail(text: String, keepChars: Int): String = + if (text.length > keepChars) { + "(earlier scanned lines omitted)\n" + text.takeLast(keepChars) + } else { + text + } + + /** 宽松解析单次协议 JSON;无法解析视为提炼失败(抛出,由调用方统一转为失败结果) */ + private fun parseSingleResponse(response: String): SingleVerdict { + val jsonText = + response.substringBeforeLast("}") + .substringAfter("{", missingDelimiterValue = "") + .takeIf { it.isNotEmpty() } + ?.let { "{$it}" } + if (jsonText.isNullOrBlank()) { + throw IllegalStateException("AI distill returned unparseable output.") + } + val json = JSONObject(jsonText) + val related = json.optBoolean("related", false) + if (!related) { + return SingleVerdict.Unrelated(json.optString("file_summary", "").trim()) + } + return if (json.optBoolean("worth", false)) { + SingleVerdict.Excerpt(json.optString("excerpt", "").trim()) + } else { + SingleVerdict.NotWorth + } + } + + /** 宽松解析扫描单轮 JSON;无法解析视为提炼失败(抛出,由调用方统一转为失败结果) */ + private fun parseScanResponse(response: String): ScanVerdict { + val jsonText = + response.substringBeforeLast("}") + .substringAfter("{", missingDelimiterValue = "") + .takeIf { it.isNotEmpty() } + ?.let { "{$it}" } + if (jsonText.isNullOrBlank()) { + throw IllegalStateException("AI distill scan returned unparseable output.") + } + val json = JSONObject(jsonText) + return ScanVerdict( + related = json.optBoolean("related", false), + excerpt = json.optString("excerpt", "").trim(), + continueScan = json.optBoolean("continue_scan", false), + fileSummary = json.optString("file_summary", "").trim(), + ) + } +} diff --git a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolExecutionManager.kt b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolExecutionManager.kt index 6d3a465077..a911f28a0d 100644 --- a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolExecutionManager.kt +++ b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolExecutionManager.kt @@ -6,15 +6,21 @@ import com.ai.assistance.operit.util.AppLogger import com.ai.assistance.operit.util.ChatTrace import com.ai.assistance.operit.core.tools.AIToolHandler import com.ai.assistance.operit.core.tools.AIToolHookDecision +import com.ai.assistance.operit.core.tools.HighRiskToolCatalog import com.ai.assistance.operit.core.tools.StringResultData import com.ai.assistance.operit.core.tools.ToolExecutor +import com.ai.assistance.operit.core.tools.WorkspacePathChecker +import com.ai.assistance.operit.core.tools.ToolProgressBus import com.ai.assistance.operit.core.tools.climode.CliToolModeSupport import com.ai.assistance.operit.core.tools.climode.ToolExposureMode import com.ai.assistance.operit.data.model.ToolInvocation import com.ai.assistance.operit.data.model.ToolResult import com.ai.assistance.operit.core.tools.packTool.PackageManager import com.ai.assistance.operit.util.stream.StreamCollector +import com.ai.assistance.operit.data.preferences.ApiPreferences import com.ai.assistance.operit.data.preferences.CharacterCardToolAccessResolver +import com.ai.assistance.operit.data.repository.ChatHistoryManager +import com.ai.assistance.operit.ui.permissions.PermissionLevel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.CancellationException import kotlinx.coroutines.asContextElement @@ -48,12 +54,28 @@ object ToolExecutionManager { private const val TOOL_SESSION_ID_PARAM = "__operit_tool_session_id" private val toolRuntimeContextThreadLocal = ThreadLocal() - data class ToolRuntimeContext( + // 修改意图:嵌套调用(JS 扩展包 toolCall → 内置文件工具)绕过权限弹窗链路, + // 由该 Hook 兜底做工作区判定;注册一次即可,ThreadLocal 判空保证主链路不受影响 + @Volatile + private var workspaceHookRegistered = false + + /** + * 修改意图:改为普通类并携带工作区绑定信息。字段说明: + * - chatWorkspaceRoot/chatWorkspaceEnv:会话绑定的工作区(以 ChatHistory 为准), + * 供嵌套调用的 AIToolHook 兜底判定使用(ThreadLocal 上下文随执行作用域传递) + * - approvedOutsidePaths:本轮调用中已由用户在"区外强制弹窗"确认过的路径(规范化后), + * 嵌套重入(扩展包 toolCall → 内置文件工具)不重复弹窗也不误拦截 + */ + class ToolRuntimeContext( val callerCardId: String? = null, val toolExposureMode: ToolExposureMode = ToolExposureMode.FULL, val callerChatId: String? = null, val toolSessionScopeId: String? = null, val subtaskExecution: SubtaskExecutionState? = null, + val chatWorkspaceRoot: String? = null, + val chatWorkspaceEnv: String? = null, + val approvedOutsidePaths: MutableSet = + java.util.Collections.newSetFromMap(java.util.concurrent.ConcurrentHashMap()), ) internal sealed class PackageContextInjection { @@ -602,7 +624,11 @@ object ToolExecutionManager { androidContext: Context, toolHandler: AIToolHandler, invocation: ToolInvocation, - toolExposureMode: ToolExposureMode = ToolExposureMode.FULL + toolExposureMode: ToolExposureMode = ToolExposureMode.FULL, + chatWorkspaceRoot: String? = null, + chatWorkspaceEnv: String? = null, + runtimeContext: ToolRuntimeContext? = null, + callerChatId: String? = null ): Pair { val resolvedTarget = resolveToolTarget(invocation.tool) val permissionTool = @@ -626,6 +652,220 @@ object ToolExecutionManager { return Pair(true, null) } + // 修改意图:文件工具工作区防护 + 大模型审查。判定先于 deny_tool 标记执行—— + // 安全边界不因模型侧文本标记而绕过。扩展包文件工具(如 extended_file_tools:copy_file) + // 与内置文件工具同族,同样纳入防护。 + // 行为矩阵(均为需求确认项): + // FORBID:区内走标准拒绝;区外(含未绑定工作区)直接拒绝并说明 + // ASK:走标准弹窗(不跑审查;审查开关仅在允许模式显示) + // ALLOW + 审查开:合规 → 直接执行(区外也不弹窗);不合规 → 弹窗展示审查意见由用户决定; + // 审查失败 → 弹窗提示错误信息由用户决定 + // ALLOW + 审查关:区内直行;区外强制弹窗 + if (WorkspacePathChecker.isWorkspaceProtectedTool(permissionTool.name)) { + val toolPermissionSystem = toolHandler.getToolPermissionSystem() + val level = toolPermissionSystem.getToolPermission(permissionTool.name) + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = chatWorkspaceRoot, + chatWorkspaceEnv = chatWorkspaceEnv, + tool = permissionTool, + ) + val extractedPaths = WorkspacePathChecker.extractPaths(permissionTool) + + if (level == PermissionLevel.FORBID && zone == WorkspacePathChecker.Zone.OUTSIDE) { + val errorMessage = + androidContext.getString( + R.string.tool_workspace_outside_forbidden, + resolvedTarget.displayName + ) + val errorResult = + ToolResult( + toolName = resolvedTarget.displayName, + success = false, + result = StringResultData(""), + error = errorMessage + ) + toolHandler.notifyToolPermissionChecked( + permissionTool, + granted = false, + reason = errorMessage + ) + return Pair(false, errorResult) + } + + if (level == PermissionLevel.ALLOW) { + val reviewEnabled = + if (zone == WorkspacePathChecker.Zone.INSIDE) { + ApiPreferences.getInstance(androidContext) + .toolReviewInsideWorkspaceFlow.first() + } else { + ApiPreferences.getInstance(androidContext) + .toolReviewOutsideWorkspaceFlow.first() + } + + if (reviewEnabled) { + // 大模型合规审查 + ToolProgressBus.update(permissionTool.name, 0.05f, "AI reviewing tool call...") + val outcome = + ToolReviewService.review( + androidContext, + permissionTool, + scopeLabel = + when (zone) { + WorkspacePathChecker.Zone.INSIDE -> "inside the workspace" + WorkspacePathChecker.Zone.OUTSIDE -> + "OUTSIDE the workspace (or no workspace bound)" + }, + chatWorkspaceRoot = chatWorkspaceRoot, + callerChatId = callerChatId, + ) + when (outcome) { + is ToolReviewOutcome.Compliant -> { + toolHandler.notifyToolPermissionChecked(permissionTool, granted = true) + if (zone == WorkspacePathChecker.Zone.OUTSIDE) { + // 审查通过:区外也直接放行,不弹窗(需求确认项) + recordApprovedOutsidePaths(runtimeContext, extractedPaths) + } + return Pair(true, null) + } + is ToolReviewOutcome.NonCompliant -> { + val confirmationReason = + androidContext.getString( + R.string.tool_review_noncompliant_confirmation, + outcome.reason + ) + return confirmAfterReview( + androidContext = androidContext, + toolHandler = toolHandler, + permissionTool = permissionTool, + resolvedDisplayName = resolvedTarget.displayName, + confirmationReason = confirmationReason, + deniedErrorPrefix = + "Tool call flagged non-compliant by AI review: ${outcome.reason}.", + runtimeContext = runtimeContext, + extractedPaths = extractedPaths + ) + } + is ToolReviewOutcome.Failed -> { + // 审查失败:弹出原询问窗口并提示审查错误信息,由用户决定 + val confirmationReason = + androidContext.getString( + R.string.tool_review_failed_confirmation, + outcome.message + ) + return confirmAfterReview( + androidContext = androidContext, + toolHandler = toolHandler, + permissionTool = permissionTool, + resolvedDisplayName = resolvedTarget.displayName, + confirmationReason = confirmationReason, + deniedErrorPrefix = outcome.message, + runtimeContext = runtimeContext, + extractedPaths = extractedPaths + ) + } + } + } else if (zone == WorkspacePathChecker.Zone.OUTSIDE) { + // 审查关闭:区外强制弹窗,原因中列出目标路径,供用户判断 + val confirmationReason = + androidContext.getString( + R.string.tool_workspace_outside_confirmation, + extractedPaths.joinToString(", ") + ) + val permissionResult = + toolPermissionSystem.requestConfirmation(permissionTool, confirmationReason) + + if (!permissionResult.isGranted) { + val errorMessage = + androidContext.getString(requireNotNull(permissionResult.errorMessageResId)) + val errorResult = + ToolResult( + toolName = resolvedTarget.displayName, + success = false, + result = StringResultData(""), + error = errorMessage + ) + toolHandler.notifyToolPermissionChecked( + permissionTool, + granted = false, + reason = errorMessage + ) + return Pair(false, errorResult) + } + + recordApprovedOutsidePaths(runtimeContext, extractedPaths) + toolHandler.notifyToolPermissionChecked(permissionTool, granted = true) + return Pair(true, null) + } + // ALLOW + 区内 + 审查关:落到标准流程(直接放行) + } + // ASK(区内外均走标准弹窗)与 FORBID(区内标准拒绝):落到标准流程 + } else if (HighRiskToolCatalog.isHighRiskTool(permissionTool.name)) { + // 修改意图:高危工具调用大模型审查(三开关之「自动审查高危工具」)。 + // 高危工具没有工作区路径概念,不分区,由独立开关管辖;行为矩阵与文件工具审查一致: + // ALLOW + 开关开:合规 → 直接执行;不合规/审查失败 → 弹窗由用户决定 + // ALLOW + 开关关 / ASK / FORBID:落到标准流程(工具自身权限语义不变) + val toolPermissionSystem = toolHandler.getToolPermissionSystem() + val level = toolPermissionSystem.getToolPermission(permissionTool.name) + if (level == PermissionLevel.ALLOW) { + val reviewEnabled = + ApiPreferences.getInstance(androidContext) + .toolReviewHighRiskToolsFlow.first() + if (reviewEnabled) { + ToolProgressBus.update(permissionTool.name, 0.05f, "AI reviewing tool call...") + val outcome = + ToolReviewService.review( + androidContext, + permissionTool, + scopeLabel = "high-risk tool call (not workspace-scoped)", + chatWorkspaceRoot = chatWorkspaceRoot, + callerChatId = callerChatId, + ) + when (outcome) { + is ToolReviewOutcome.Compliant -> { + toolHandler.notifyToolPermissionChecked(permissionTool, granted = true) + return Pair(true, null) + } + is ToolReviewOutcome.NonCompliant -> { + val confirmationReason = + androidContext.getString( + R.string.tool_review_noncompliant_confirmation, + outcome.reason + ) + return confirmAfterReview( + androidContext = androidContext, + toolHandler = toolHandler, + permissionTool = permissionTool, + resolvedDisplayName = resolvedTarget.displayName, + confirmationReason = confirmationReason, + deniedErrorPrefix = + "Tool call flagged non-compliant by AI review: ${outcome.reason}.", + runtimeContext = runtimeContext, + extractedPaths = emptyList() + ) + } + is ToolReviewOutcome.Failed -> { + val confirmationReason = + androidContext.getString( + R.string.tool_review_failed_confirmation, + outcome.message + ) + return confirmAfterReview( + androidContext = androidContext, + toolHandler = toolHandler, + permissionTool = permissionTool, + resolvedDisplayName = resolvedTarget.displayName, + confirmationReason = confirmationReason, + deniedErrorPrefix = outcome.message, + runtimeContext = runtimeContext, + extractedPaths = emptyList() + ) + } + } + } + } + } + // 检查是否强制拒绝权限(deny_tool标记) val hasPromptForPermission = !invocation.rawText.contains("deny_tool") @@ -664,6 +904,73 @@ object ToolExecutionManager { return Pair(true, null) } + /** + * 修改意图:执行前剥离 call_purpose(审查声明参数)。该参数仅供审查流读取, + * 若流入执行器,JS 包工具与代理解析(parseProxyInvocation 白名单)会因未知参数报错。 + * rawText 保留原始 XML,聊天记录与回传模型的调用文本不受影响。 + */ + private fun ToolInvocation.withoutCallPurposeParam(): ToolInvocation = + copy( + tool = + tool.copy( + parameters = + tool.parameters.filterNot { it.name == HighRiskToolCatalog.CALL_PURPOSE_PARAM } + ) + ) + + /** + * 修改意图:审查不合规/审查失败后的统一确认流(文件工具与高危工具审查共用)。 + * 弹窗由用户决定:拒绝 → notify 失败并返回错误结果;放行 → 记录已确认的 + * 区外路径(非文件工具传空列表即无副作用)并 notify 放行。 + * + * @param deniedErrorPrefix 拒绝时错误信息的审查侧前缀(文件/高危分支各自组装) + */ + private suspend fun confirmAfterReview( + androidContext: Context, + toolHandler: AIToolHandler, + permissionTool: AITool, + resolvedDisplayName: String, + confirmationReason: String, + deniedErrorPrefix: String, + runtimeContext: ToolRuntimeContext?, + extractedPaths: List + ): Pair { + val toolPermissionSystem = toolHandler.getToolPermissionSystem() + val permissionResult = + toolPermissionSystem.requestConfirmation(permissionTool, confirmationReason) + if (!permissionResult.isGranted) { + val errorMessage = + androidContext.getString(requireNotNull(permissionResult.errorMessageResId)) + toolHandler.notifyToolPermissionChecked(permissionTool, granted = false, reason = errorMessage) + return Pair( + false, + ToolResult( + toolName = resolvedDisplayName, + success = false, + result = StringResultData(""), + error = "$deniedErrorPrefix $errorMessage" + ) + ) + } + if (extractedPaths.isNotEmpty()) { + recordApprovedOutsidePaths(runtimeContext, extractedPaths) + } + toolHandler.notifyToolPermissionChecked(permissionTool, granted = true) + return Pair(true, null) + } + + /** 修改意图:记录用户/AI 审查放行的区外路径(规范化后),供嵌套重入的 Hook 放行 */ + private fun recordApprovedOutsidePaths( + runtimeContext: ToolRuntimeContext?, + paths: List + ) { + runtimeContext ?: return + paths.forEach { path -> + val canonical = runCatching { java.io.File(path).canonicalPath }.getOrDefault(path) + runtimeContext.approvedOutsidePaths.add(canonical) + } + } + /** * * 执行工具调用,包括权限检查、并行/串行执行和结果聚合。 @@ -709,6 +1016,17 @@ object ToolExecutionManager { toolHandler.registerDefaultTools() } + // 修改意图:嵌套调用的工作区兜底 Hook 幂等注册(主链路的弹窗防护在 + // checkToolPermission 的工作区阶段,二者互不重叠) + if (!workspaceHookRegistered) { + synchronized(this) { + if (!workspaceHookRegistered) { + toolHandler.addToolHook(WorkspaceFileToolHook()) + workspaceHookRegistered = true + } + } + } + val roleCardToolAccess = if (callerCardId.isNullOrBlank()) { null } else { @@ -721,6 +1039,17 @@ object ToolExecutionManager { if (subtaskExecution != null) throw error }.getOrNull() } + // 修改意图:工作区防护以会话绑定(ChatHistory)为准;未绑定工作区 → root 为 null, + // 所有文件工具路径按区外处理(需求确认项)。绑定解析失败同样按未绑定处理 + val chatWorkspaceBinding = callerChatId?.let { chatId -> + runCatching { + ChatHistoryManager.getInstance(context).getChatWorkspaceBinding(chatId) + }.onFailure { error -> + AppLogger.e(TAG, "工作区绑定解析失败: chatId=$callerChatId", error) + }.getOrNull() + } + val chatWorkspaceRoot = chatWorkspaceBinding?.first + val chatWorkspaceEnv = chatWorkspaceBinding?.second val toolRuntimeContext = ToolRuntimeContext( callerCardId = callerCardId, @@ -728,6 +1057,8 @@ object ToolExecutionManager { callerChatId = callerChatId, toolSessionScopeId = subtaskExecution?.toolSessionScopeId ?: callerChatId, subtaskExecution = subtaskExecution, + chatWorkspaceRoot = chatWorkspaceRoot, + chatWorkspaceEnv = chatWorkspaceEnv ) // 1. 顶层工具暴露模式拦截 @@ -819,9 +1150,20 @@ object ToolExecutionManager { when (val interception = toolHandler.checkToolInterception(interceptionTool)) { AIToolHookDecision.Allow -> { val (hasPermission, errorResult) = - checkToolPermission(context, toolHandler, invocation, toolExposureMode) + checkToolPermission( + context, + toolHandler, + invocation, + toolExposureMode, + chatWorkspaceRoot = chatWorkspaceRoot, + chatWorkspaceEnv = chatWorkspaceEnv, + runtimeContext = toolRuntimeContext, + callerChatId = callerChatId + ) if (hasPermission) { - permittedInvocations.add(IndexedValue(index, invocation)) + // 修改意图:call_purpose 为审查声明参数,仅供审查流读取; + // 执行前从工具参数剥离,工具执行器(含 JS 包与代理解析)不应看到该参数 + permittedInvocations.add(IndexedValue(index, invocation.withoutCallPurposeParam())) } else { completeUnexecuted(index, invocation, checkNotNull(errorResult), SubtaskExecutionState.Reason.PERMISSION_DENIED) } @@ -1070,8 +1412,13 @@ object ToolExecutionManager { // 包已激活但工具不存在 "Tool '$toolNamePart' does not exist in tool package '$packName'. Please use the 'use_package' tool and specify package name '$packName' to list all available tools in this package." } else { - // 包未激活 - "Tool package '$packName' is not activated. Auto-activation was attempted but failed, or tool '$toolNamePart' does not exist. Please use 'use_package' with package name '$packName' to check available tools." + // 修改意图:包未激活时回传 usePackage 的真实原因(如"包被禁用"), + // 不再笼统地报 "Auto-activation failed" + val activationReason = toolHandler.getLastPackageActivationMessage() + "Tool package '$packName' is not activated: " + + (activationReason?.takeIf { it.isNotBlank() } + ?: "auto-activation failed, or tool '$toolNamePart' does not exist") + + ". Please use 'use_package' with package name '$packName' to check available tools." } } } diff --git a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolReviewService.kt b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolReviewService.kt new file mode 100644 index 0000000000..e7e33c5285 --- /dev/null +++ b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/ToolReviewService.kt @@ -0,0 +1,204 @@ +package com.ai.assistance.operit.api.chat.enhance + +import com.ai.assistance.operit.api.chat.EnhancedAIService +import com.ai.assistance.operit.core.chat.hooks.PromptTurn +import com.ai.assistance.operit.core.chat.hooks.PromptTurnKind +import com.ai.assistance.operit.core.config.FunctionalPrompts +import com.ai.assistance.operit.core.tools.HighRiskToolCatalog +import com.ai.assistance.operit.core.tools.WorkspacePathChecker +import com.ai.assistance.operit.data.model.AITool +import com.ai.assistance.operit.data.model.FunctionType +import com.ai.assistance.operit.data.repository.ChatHistoryManager +import com.ai.assistance.operit.util.AppLogger +import com.ai.assistance.operit.util.ChatMarkupRegex +import kotlinx.coroutines.TimeoutCancellationException +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.withTimeout +import org.json.JSONObject + +/** 工具调用(文件工具 + 高危工具)的大模型合规审查结果 */ +sealed class ToolReviewOutcome { + /** 审查通过:调用方直接放行(不弹窗) */ + data class Compliant(val reason: String) : ToolReviewOutcome() + + /** 审查不合规:调用方弹窗展示 reason,由用户决定是否放行 */ + data class NonCompliant(val reason: String) : ToolReviewOutcome() + + /** 审查失败(未配置/报错/超时):调用方弹窗原询问窗口并提示错误信息,由用户决定 */ + data class Failed(val message: String) : ToolReviewOutcome() +} + +/** + * 修改意图:工具模式为"允许"时的工具调用合规初筛(审查范围从文件工具族 + * 泛化为文件工具 + HighRiskToolCatalog 高危工具调用,开关分区见调用方)。 + * 复用功能模型调用范式(FunctionType.TOOL_REVIEW → callFunctionModel), + * 未配置时 FunctionalConfigManager 自动回落主模型配置;审查失败不静默放行, + * 统一转为弹窗人工确认(需求确认项)。 + */ +object ToolReviewService { + private const val TAG = "ToolReviewService" + + /** 审查模型调用超时;超时语义与调用失败一致(弹窗人工确认) */ + private const val REVIEW_TIMEOUT_MS = 15_000L + + /** 审查 prompt 参数摘要的截断上限(单值与总量共用) */ + private const val CONTENT_SUMMARY_LIMIT = 500 + + /** 调用点前上下文:取最近 10 条大模型消息(含其工具调用),单条截断上限 */ + private const val RECENT_MESSAGE_COUNT = 10 + private const val RECENT_ENTRY_LIMIT = 300 + + /** 最近一条用户指令的截断上限 */ + private const val USER_INSTRUCTION_LIMIT = 800 + + /** 调用说明(call_purpose)的截断上限 */ + private const val CALL_PURPOSE_LIMIT = 200 + + suspend fun review( + context: android.content.Context, + tool: AITool, + scopeLabel: String, + chatWorkspaceRoot: String?, + callerChatId: String?, + ): ToolReviewOutcome { + // 修改意图:参数摘要从 content 类单参数扩展为全参数 name=value 汇总, + // 覆盖 command/url/action/package_name/tool_name 等高危工具参数; + // 单值与总量都截断,避免大内容挤爆审查上下文 + val contentSummary = tool.parameters + .joinToString("; ") { param -> "${param.name}=${param.value.take(CONTENT_SUMMARY_LIMIT)}" } + .take(CONTENT_SUMMARY_LIMIT) + + // 调用说明:可审查工具的可选声明参数,与调用点上下文做一致性比对 + val callPurpose = tool.parameters + .find { it.name == HighRiskToolCatalog.CALL_PURPOSE_PARAM } + ?.value + ?.replace(Regex("\\s+"), " ") + ?.trim() + ?.take(CALL_PURPOSE_LIMIT) + .orEmpty() + + val (recentContext, userInstruction) = buildReviewContext(context, callerChatId) + + // 高危工具走目录摘要;文件工具与未知名称沿用既有文件族映射 + val operationSummary = HighRiskToolCatalog.operationSummary(tool.name) + ?: fileOperationSummary(tool.name) + + val targetPaths = WorkspacePathChecker.extractPaths(tool).joinToString(", ") + + return try { + val response = + withTimeout(REVIEW_TIMEOUT_MS) { + EnhancedAIService.getInstance(context).callFunctionModel( + functionType = FunctionType.TOOL_REVIEW, + turns = + listOf( + PromptTurn( + kind = PromptTurnKind.USER, + content = + FunctionalPrompts.toolReviewPrompt( + toolName = tool.name, + operationSummary = operationSummary, + targetPaths = targetPaths, + scope = scopeLabel, + workspaceRoot = chatWorkspaceRoot ?: "", + contentSummary = contentSummary, + callPurpose = callPurpose, + userInstruction = userInstruction, + recentContext = recentContext, + ), + ) + ), + recordTokenUsage = false, + ) + } + + parseVerdict(response) + } catch (e: TimeoutCancellationException) { + AppLogger.w(TAG, "工具审查超时: ${tool.name}") + ToolReviewOutcome.Failed("Tool review timed out after ${REVIEW_TIMEOUT_MS / 1000} seconds.") + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + AppLogger.w(TAG, "工具审查调用失败: ${tool.name}, ${e.message}") + ToolReviewOutcome.Failed("Tool review failed: ${e.message ?: e.javaClass.simpleName}") + } + } + + /** + * 修改意图:审查上下文直读数据库全量历史(ChatHistoryManager.loadChatMessages), + * 不经运行态压缩摘要——最近一条用户指令必须是真实指令,不受上下文压缩影响。 + * 调用点前上下文取最近 10 条大模型消息(其内容含该轮的工具调用), + * 剔除 tool_result 结果块避免结果刷屏,仅保留模型文本与调用声明。 + * 无 callerChatId(悬浮球等无会话来源的调用)时返回空串,审查按无上下文判定。 + */ + private suspend fun buildReviewContext( + context: android.content.Context, + callerChatId: String?, + ): Pair { + if (callerChatId.isNullOrBlank()) { + return "" to "" + } + val messages = + ChatHistoryManager.getInstance(context).loadChatMessages(callerChatId) + val userInstruction = messages + .lastOrNull { it.sender == "user" } + ?.content + ?.replace(Regex("\\s+"), " ") + ?.trim() + ?.take(USER_INSTRUCTION_LIMIT) + .orEmpty() + val recentContext = messages + .filter { it.sender == "ai" } + .takeLast(RECENT_MESSAGE_COUNT) + .mapIndexed { index, message -> + val cleaned = + message.content + .replace(ChatMarkupRegex.toolResultAnyPattern, " ") + .replace(Regex("\\s+"), " ") + .trim() + .take(RECENT_ENTRY_LIMIT) + "${index + 1}. $cleaned" + } + .joinToString("\n") + return recentContext to userInstruction + } + + /** 文件工具族的英文操作摘要;高危目录未覆盖的名称给出通用描述 */ + private fun fileOperationSummary(toolName: String): String = + when (toolName.substringAfter(':')) { + "delete_file" -> "delete a file/directory" + "write_file", "write_file_binary", "create_file" -> "write/create a file" + "edit_file", "apply_file" -> "edit a file" + "move_file" -> "move/rename" + "copy_file" -> "copy" + "zip_files", "unzip_files" -> "archive operation" + "read_file", "read_file_part", "read_file_full", "read_file_binary", + "list_files", "file_exists", "file_info", "find_files", "grep_code", + "grep_context", -> "read/search (non-destructive)" + else -> "file operation" + } + + /** 宽松解析 {"compliant": bool, "reason": string};无法解析视为审查失败 */ + private fun parseVerdict(response: String): ToolReviewOutcome { + val jsonText = + response.substringBeforeLast("}") + .substringAfter("{", missingDelimiterValue = "") + .takeIf { it.isNotEmpty() } + ?.let { "{$it}" } + if (jsonText.isNullOrBlank()) { + return ToolReviewOutcome.Failed("Tool review returned unparseable output.") + } + return runCatching { + val json = JSONObject(jsonText) + val compliant = json.optBoolean("compliant", false) + val reason = json.optString("reason", "").trim() + if (compliant) { + ToolReviewOutcome.Compliant(reason.ifBlank { "review passed" }) + } else { + ToolReviewOutcome.NonCompliant(reason.ifBlank { "review flagged this call as non-compliant" }) + } + }.getOrElse { + ToolReviewOutcome.Failed("Tool review returned unparseable output.") + } + } +} diff --git a/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/WorkspaceFileToolHook.kt b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/WorkspaceFileToolHook.kt new file mode 100644 index 0000000000..c7f1781c7e --- /dev/null +++ b/app/src/main/java/com/ai/assistance/operit/api/chat/enhance/WorkspaceFileToolHook.kt @@ -0,0 +1,63 @@ +package com.ai.assistance.operit.api.chat.enhance + +import com.ai.assistance.operit.core.tools.AIToolHook +import com.ai.assistance.operit.core.tools.AIToolHookDecision +import com.ai.assistance.operit.core.tools.WorkspacePathChecker +import com.ai.assistance.operit.data.model.AITool +import java.io.File + +/** + * 修改意图:JS 扩展包经 toolCall → AIToolHandler.executeTool 的嵌套调用绕过了 + * ToolExecutionManager 的权限弹窗链路,工作区防护需要一个兜底拦截点。 + * + * 判定依赖 ToolExecutionManager 的 ToolRuntimeContext ThreadLocal——它只在 + * executeTool 的执行作用域内可见,因此: + * - 主链路(模型发起)的 checkToolInterception 发生在 ThreadLocal 设置之前, + * 本 Hook 直接放行,由 checkToolPermission 的工作区阶段负责弹窗/拒绝 + * - 嵌套调用(扩展包 toolCall)发生在 ThreadLocal 作用域内,本 Hook 生效 + * + * Hook 无 UI 能力:区外调用一律拒绝并回传理由;本轮已由用户确认过的区外路径 + * (approvedOutsidePaths)不重复拦截。 + */ +internal class WorkspaceFileToolHook : AIToolHook { + + override fun onToolCallIntercept(tool: AITool): AIToolHookDecision { + if (!WorkspacePathChecker.isWorkspaceProtectedTool(tool.name)) { + return AIToolHookDecision.Allow + } + + val runtimeContext = ToolExecutionManager.currentToolRuntimeContext() ?: return AIToolHookDecision.Allow + + val paths = WorkspacePathChecker.extractPaths(tool) + if (paths.isEmpty()) { + return AIToolHookDecision.Allow + } + + // 本轮已由用户确认过的区外路径直接放行 + val allConfirmed = + paths.all { path -> + val canonical = runCatching { File(path).canonicalPath }.getOrDefault(path) + canonical in runtimeContext.approvedOutsidePaths + } + if (allConfirmed) { + return AIToolHookDecision.Allow + } + + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = runtimeContext.chatWorkspaceRoot, + chatWorkspaceEnv = runtimeContext.chatWorkspaceEnv, + tool = tool, + ) + if (zone == WorkspacePathChecker.Zone.INSIDE) { + return AIToolHookDecision.Allow + } + + return AIToolHookDecision.Block( + reason = + "File tool '${tool.name}' targets paths outside the chat workspace " + + "(or the chat has no workspace bound): ${paths.joinToString(", ")}. " + + "Nested/package calls cannot be confirmed by the user and are denied." + ) + } +} diff --git a/app/src/main/java/com/ai/assistance/operit/core/config/FunctionalPrompts.kt b/app/src/main/java/com/ai/assistance/operit/core/config/FunctionalPrompts.kt index ecead95e96..31af394820 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/config/FunctionalPrompts.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/config/FunctionalPrompts.kt @@ -1349,4 +1349,123 @@ Rules: } } + /** + * 修改意图:文件工具调用的大模型合规审查 prompt。审查器只做合规初筛, + * 输出严格 JSON(compliant + reason),调用方对解析失败有明确语义(弹窗人工确认)。 + */ + fun toolReviewPrompt( + toolName: String, + operationSummary: String, + targetPaths: String, + scope: String, + workspaceRoot: String, + contentSummary: String, + callPurpose: String, + userInstruction: String, + recentContext: String, + ): String { + return """ +You are a tool-call compliance reviewer for an AI assistant. Review the following tool call and decide whether it is a reasonable, safe and non-destructive operation in the current context. + +Tool: $toolName +Operation: $operationSummary +Target paths (file tools only): ${targetPaths.ifBlank { "(none)" }} +Scope: $scope +Workspace root: ${workspaceRoot.ifBlank { "(no workspace bound)" }} +Declared call purpose (model-provided, optional): ${callPurpose.ifBlank { "(not provided)" }} +Latest user instruction (taken from full history, unaffected by context compression): ${userInstruction.ifBlank { "(unavailable)" }} +Recent context - up to 10 earlier model messages including their tool calls, oldest first: +${recentContext.ifBlank { "(unavailable)" }} +Parameter summary (truncated): $contentSummary + +Judge against these rules: +1) File operations: the paths should be consistent with a normal assistant task (no obvious attempts to destroy unrelated data, no mass deletion of root/system directories). Writes/edits should target the declared workspace or user-relevant locations; reads are generally acceptable. Recursively deleting or overwriting large/unknown directories outside the workspace is non-compliant. +2) Command/code execution (shell, terminal, sandbox scripts): the command must serve the declared assistant task; destructive or irreversible commands (mass deletion, formatting, system tampering, killing system processes) are non-compliant. +3) App and system management (install/uninstall/stop app, modify system settings): only compliant when clearly required by the task; changing unrelated or security-sensitive settings is non-compliant. +4) Network requests: sending user data or workspace content to unexpected endpoints, or fetching/executing content unrelated to the task (potential data exfiltration), is non-compliant. +5) Proxy/orchestration calls (use_package, package_proxy, proxy, trigger_workflow): judge by the target tool/params when visible; delegating to tools that would violate these rules is non-compliant. +6) Consistency: the call must be a coherent continuation of the latest user instruction and the recent context. When the model declared a call purpose, compare it against them - a declared purpose that contradicts the context, or a call unrelated to the user's request, is non-compliant. A missing declared purpose is NOT a violation by itself. +7) If the intent cannot be determined, prefer marking non-compliant with a short explanation. + +Output STRICT JSON only, no other text: {"compliant": true, "reason": ""} +""".trimIndent() + } + + /** + * 修改意图:AI 读取提炼(FileDistillService)单次协议 prompt——一次调用同时完成 + * related/worth 判断与带行号摘录。行号前缀与 read_file 输出格式一致(" 12| "), + * 摘录行号可直接用于 edit_file + */ + fun fileDistillSinglePrompt( + path: String, + focus: String, + content: String, + truncatedNote: String, + ): String { + return """ +You are a file-content extractor for an AI assistant. The assistant has read a file and wants only the parts relevant to its goal, so the reading cost stays small. + +File: $path +What the assistant wants from this file (focus): $focus +$truncatedNote +File content with line numbers ("N| " prefix; line numbers refer to the original file): +$content + +Judge and extract: +1) related: whether the content contains anything relevant to the focus. +2) worth: true only when LESS THAN HALF of the content is relevant to the focus (only then does distilling save tokens). If most of the content is relevant, set worth=false. +3) excerpt: required when worth=true. Copy the relevant original lines VERBATIM, keeping their exact "N| " line-number prefixes. Include one or two neighbouring lines around each relevant region so nothing is cut off mid-way. Never paraphrase, never add commentary. +4) file_summary: one short sentence describing what this file is. Used when related=false. + +Output STRICT JSON only, no other text: {"related": true, "worth": true, "excerpt": " 12| ...", "file_summary": "..."} +""".trimIndent() + } + + /** + * 修改意图:AI 读取提炼扫描协议单轮 prompt——逐块滚动,每轮输出该块相关摘录与 + * continue_scan 决策;块边界处可能截断相关段落,规则中要求模型据此决定继续扫描 + */ + fun fileDistillScanChunkPrompt( + path: String, + focus: String, + round: Int, + startLine: Int, + endLine: Int, + isLastChunk: Boolean, + hasPriorExcerpt: Boolean, + chunk: String, + ): String { + val priorNote = + if (hasPriorExcerpt) { + "Relevant lines from earlier chunks were already collected; do not repeat them." + } else { + "No relevant lines were found in earlier chunks." + } + val eofNote = + if (isLastChunk) { + "This is the LAST chunk of the file, continue_scan must be false." + } else { + "More chunks may follow." + } + return """ +You are scanning a large text file chunk by chunk for an AI assistant, to collect only the lines relevant to its goal. + +File: $path +What the assistant wants from this file (focus): $focus +Current chunk: $round (lines $startLine-$endLine). $eofNote +$priorNote +Chunk content with line numbers ("N| " prefix; line numbers refer to the original file): +$chunk + +Rules: +1) related: whether THIS chunk contains anything relevant to the focus (not whether earlier chunks did). +2) excerpt: copy the relevant original lines of THIS chunk VERBATIM, keeping their exact "N| " prefixes. Include one or two neighbouring lines around each relevant region. If a relevant section is cut off at the end of this chunk, still include what is visible and continue scanning in the next chunk. Empty string when nothing in this chunk is relevant. Never paraphrase, never add commentary. +3) continue_scan: true when later chunks are likely to contain more relevant content, or when this chunk ends in the middle of a relevant section; false when everything relevant has been found and the rest is clearly unrelated${ + if (isLastChunk) " (and always false for the last chunk)" else "" + }. +4) file_summary: one short sentence describing the whole file; only meaningful in the first chunk, empty string afterwards. + +Output STRICT JSON only, no other text: {"related": true, "excerpt": " 210| ...", "continue_scan": true, "file_summary": "..."} +""".trimIndent() + } } diff --git a/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPrompts.kt b/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPrompts.kt index ba5582c573..9e9585c52b 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPrompts.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPrompts.kt @@ -2,6 +2,7 @@ package com.ai.assistance.operit.core.config import com.ai.assistance.operit.core.chat.hooks.PromptHookContext import com.ai.assistance.operit.core.chat.hooks.PromptHookRegistry +import com.ai.assistance.operit.core.tools.HighRiskToolCatalog import com.ai.assistance.operit.data.model.SystemToolPromptCategory import com.ai.assistance.operit.data.model.ToolPrompt import com.ai.assistance.operit.data.model.ToolParameterSchema @@ -94,11 +95,16 @@ object SystemToolPrompts { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "e.g. \"/sdcard/Download\"", required = true), ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false) - ) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported (values defined at read_file). + |On success: "[android] Directory listing for :" followed by one line per entry with type, size, date and name. + |On failure: error states the cause (directory does not exist / invalid repository path) - fix the path and retry. + """.trimMargin() ), ToolPrompt( name = "read_file", - description = "Read the content of a file. For image files (jpg, jpeg, png, gif, bmp), it automatically extracts text using OCR.", + description = "Read the content of a file. For image files (jpg, jpeg, png, gif, bmp), it automatically extracts text using OCR. Files over 2048 bytes are AI-distilled by default: pass focus describing what you want and only the relevant lines (with original line numbers) come back.", parametersStructured = listOf( ToolParameterSchema( name = "path", @@ -106,6 +112,18 @@ object SystemToolPrompts { description = "file path", required = true ), + ToolParameterSchema( + name = "focus", + type = "string", + description = "what you want from this file, e.g. \"the function that parses config\"; required for the default AI-distilled read, ignored when raw=true", + required = false + ), + ToolParameterSchema( + name = "raw", + type = "boolean", + description = "optional, default false: AI-distilled read returning only focus-relevant lines; set true only when you are sure you need the raw content", + required = false + ), ToolParameterSchema( name = "environment", type = "string", @@ -136,36 +154,70 @@ object SystemToolPrompts { description = "optional, when true: return an tag for models that support video", required = false ) - ) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. + |AI distill (default, raw=false): files over 2048 bytes return only the lines relevant to focus (verbatim, with original "N| " line numbers usable by edit_file) preceded by an "[AI distilled read ...]" header; "no content matching the focus" plus a file summary when nothing matches. Unless you are sure you need the raw content, keep the default and state focus precisely - it saves context. raw=true returns the full original content. + |Images/PDF/doc/audio/video and files ≤ 2048 bytes always return the full extracted content; linux/repo: environments are not distilled. + |On AI-distill failure: the error says so - retry with raw=true to read the original content. + |Output: content with line-number prefixes "N| " for display only - NEVER copy the prefixes into edit_file/apply_file old. + |Limits: raw output truncated at 32000 characters with a "... (file content truncated) ..." marker; files over 32 MB are rejected. Use read_file_part (start_line/end_line) to read large files in segments. + |Special types (pdf/docx/images via OCR/audio/video) are extracted to text automatically; extraction failures return success=false. + |On failure: error names the cause with the fix - absolute path required / file not found / not a text file (use read_file_binary for binary files) / exceeds 32 MB read limit (use read_file_part). + """.trimMargin() ), ToolPrompt( name = "read_file_part", - description = "Read file content by line range.", + description = "Read file content. When you are NOT sure which line numbers you need, use the default AI-distilled read: pass focus and it scans the whole file, returning only the relevant lines with real line numbers. With raw=true it reads an exact line range (start_line/end_line) for segmented reading of large files.", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "file path", required = true), + ToolParameterSchema(name = "focus", type = "string", description = "what you want from this file; required for the default AI-distilled read (when raw is not true), ignored when raw=true", required = false), + ToolParameterSchema(name = "raw", type = "boolean", description = "optional, default false: AI-distilled scan returning only focus-relevant lines; set true to read the exact line range given by start_line/end_line", required = false), ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), - ToolParameterSchema(name = "start_line", type = "integer", description = "starting line number, 1-indexed", required = false, default = "1"), - ToolParameterSchema(name = "end_line", type = "integer", description = "ending line number, 1-indexed, inclusive, optional", required = false, default = "start_line + 99") - ) + ToolParameterSchema(name = "start_line", type = "integer", description = "AI-distilled read: optional, first line to scan from (default 1). raw=true: starting line number, 1-indexed", required = false, default = "1"), + ToolParameterSchema(name = "end_line", type = "integer", description = "AI-distilled read: optional, last line to scan to. raw=true: ending line number, 1-indexed, inclusive, optional; omitted = read to end of file", required = false, default = "raw=true: start_line + 199 (200 lines)") + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported (linux/repo: are not distilled). + |AI distill (default, raw=false): scans the file (optionally bounded by start_line/end_line) and returns only the lines relevant to focus - verbatim, with real "N| " line numbers usable by edit_file, preceded by an "[AI distilled read ...]" header. It keeps reading past a match until the relevant section ends, so excerpts are complete. If most of the scanned content matches the focus, the original scanned region is returned as-is. Unless you already know the exact lines, prefer the default and state focus precisely. + |raw=true: reads the exact start_line/end_line range (start_line defaults to 1, end_line omitted reads to end of file) - use for segmented reading of very large files. + |On AI-distill failure: the error says so - retry with raw=true to read the original content. + |Output: line-number prefixes "N| " (the N is the real file line number, display only - do NOT copy prefixes into edit_file/apply_file old). Output capped at 32000 characters. + |On success (raw=true): shows the start line it returned from, so you can chain the next segment (start_line = last returned line + 1). + |On failure: error names the cause (path invalid / start_line beyond end of file / not a text file). + """.trimMargin() ), ToolPrompt( name = "create_file", - description = "Create a new file by delegating to apply_file with type=create.", + description = "Create a new file with the given content. Fails if the file already exists.", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "file path", required = true), ToolParameterSchema(name = "new", type = "string", description = "full file content for the new file", required = true), ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false) - ) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported. + |Parent directories are created automatically. + |On success: "[android] Successfully created new file: ". + |On failure: "file already exists" - to replace an existing file entirely, call delete_file first, then write_file (create_file never overwrites). + """.trimMargin() ), ToolPrompt( name = "edit_file", - description = "Edit an existing file by delegating to apply_file with type=replace.", + description = "Edit an existing file: replace a unique snippet (old) with new content. Matching is exact-first, whitespace-tolerant fuzzy second.", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "file path", required = true), - ToolParameterSchema(name = "old", type = "string", description = "the exact content to be matched and replaced", required = true), - ToolParameterSchema(name = "new", type = "string", description = "the new content to insert", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false) - ) + ToolParameterSchema(name = "old", type = "string", description = "the snippet to be replaced - must be UNIQUE in the file; include enough surrounding context to disambiguate; do not include read_file line-number prefixes", required = true), + ToolParameterSchema(name = "new", type = "string", description = "the new content to insert (empty string with delete semantics is allowed via apply_file, not here)", required = true), + ToolParameterSchema(name = "occurrence", type = "integer", description = "optional, 1-based index of the match to replace when old matches multiple locations (the error lists the candidates); omitted occurrence means the edit is rejected, not defaulted to 1", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) | \"linux\" - repo: is not supported", required = false) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported. + |Matching: 1) exact whitespace-tolerant match; 2) fuzzy similarity > 0.9. If the snippet matches MULTIPLE locations, the edit is REJECTED with the candidate line numbers - add more context to old, or retry with the occurrence parameter to target the Nth match. + |On success: "[android] Successfully applied AI code to file: " plus a unified diff and the replaced line range. + |On failure: "Could not find a match" (re-read the target lines with read_file_part, then retry) or "multiple perfect matches" (make old unique). NEVER fall back to shell commands to edit files - keep using this tool. + """.trimMargin() ), ToolPrompt( name = "delete_file", @@ -173,8 +225,14 @@ object SystemToolPrompts { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "target path", required = true), ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), - ToolParameterSchema(name = "recursive", type = "boolean", description = "boolean", required = false, default = "false") - ) + ToolParameterSchema(name = "recursive", type = "boolean", description = "true = delete non-empty directories recursively; false (default) = only empty directories and single files", required = false, default = "false") + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported (repo: ignores recursive and deletes via the provider). + |Hard rule: recursive deletion of system roots (/ , /storage/emulated/0 , /sdcard , /data ...) is always rejected. + |On success: "[android] Successfully deleted: ". + |On failure: "Directory is not empty and recursive flag is not set" - pass recursive=true, or delete children first. + """.trimMargin() ), ToolPrompt( name = "make_directory", @@ -182,8 +240,13 @@ object SystemToolPrompts { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "directory path", required = true), ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), - ToolParameterSchema(name = "create_parents", type = "boolean", description = "boolean", required = false, default = "false") - ) + ToolParameterSchema(name = "create_parents", type = "boolean", description = "true = create missing parent directories too (mkdirs); false (default) = parent must already exist", required = false, default = "false") + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. + |On success: "[android] Directory created: "; creating an existing directory succeeds with "Directory already exists". + |On failure: "parent directory does not exist" - pass create_parents=true, or create parents first. + """.trimMargin() ), ToolPrompt( name = "find_files", @@ -193,33 +256,50 @@ object SystemToolPrompts { ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), ToolParameterSchema(name = "pattern", type = "string", description = "search pattern, e.g. \"*.jpg\"", required = true), ToolParameterSchema(name = "max_depth", type = "integer", description = "optional, controls depth of subdirectory search, -1=unlimited", required = false), - ToolParameterSchema(name = "use_path_pattern", type = "boolean", description = "boolean", required = false, default = "false"), - ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "boolean", required = false, default = "false") - ) + ToolParameterSchema(name = "use_path_pattern", type = "boolean", description = "true = match the pattern against the full path instead of the file name only", required = false, default = "false"), + ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "true = case-insensitive pattern matching", required = false, default = "false") + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. + |On success: "Found N files:" with up to 10 paths listed - if more were found the tail is abbreviated ("... and N other files"); narrow the search (deeper path, tighter pattern) to see all. + |On failure: "search path does not exist" - fix the path. + """.trimMargin() ), ToolPrompt( name = "grep_code", description = "Search code content matching a regex pattern in files. Returns matches with surrounding context lines.", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "search path", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" - repo: is not supported for content search", required = false), ToolParameterSchema(name = "pattern", type = "string", description = "regex pattern", required = true), ToolParameterSchema(name = "file_pattern", type = "string", description = "file filter", required = false, default = "\"*\""), - ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "boolean", required = false, default = "false"), + ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "true = case-insensitive matching", required = false, default = "false"), ToolParameterSchema(name = "context_lines", type = "integer", description = "lines of context before/after match", required = false, default = "3"), ToolParameterSchema(name = "max_results", type = "integer", description = "max matches", required = false, default = "100") - ) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported - the call is rejected with a clear error. + |Respects .gitignore rules; binary files are skipped automatically. + |On success: matches grouped per file (first 20 files listed; total match count is reported so you can narrow the pattern if needed). + |On failure: "Error performing native grep search" - check the path exists and the regex is valid. + """.trimMargin() ), ToolPrompt( name = "grep_context", description = "Search for relevant content based on intent/context understanding. Supports two modes: 1) Directory mode: when path is a directory, finds most relevant files. 2) File mode: when path is a file, finds most relevant code segments within that file. Uses semantic relevance scoring.", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "directory or file path", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" - repo: is not supported", required = false), ToolParameterSchema(name = "intent", type = "string", description = "intent or context description string", required = true), ToolParameterSchema(name = "file_pattern", type = "string", description = "file filter for directory mode", required = false, default = "\"*\""), ToolParameterSchema(name = "max_results", type = "integer", description = "maximum items to return", required = false, default = "10") - ) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported - the call is rejected with a clear error. + |Runs several model-assisted refine rounds; takes longer than grep_code - prefer grep_code when you already know the pattern. + |On success: ranked file paths or code segments matching the intent. + |On failure: "Error performing context search" - rephrase the intent more specifically, or fall back to grep_code with a concrete regex. + """.trimMargin() ), ToolPrompt( name = "download_file", @@ -229,10 +309,15 @@ object SystemToolPrompts { ToolParameterSchema(name = "visit_key", type = "string", description = "optional, visitKey from a previous visit_web result", required = false), ToolParameterSchema(name = "link_number", type = "integer", description = "optional, 1-based link index from Results (use with visit_key)", required = false), ToolParameterSchema(name = "image_number", type = "integer", description = "optional, 1-based image index from Images (use with visit_key)", required = false), - ToolParameterSchema(name = "destination", type = "string", description = "save path", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), + ToolParameterSchema(name = "destination", type = "string", description = "absolute save path on the target file system", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" - repo: is not supported", required = false), ToolParameterSchema(name = "headers", type = "string", description = "optional HTTP headers as JSON object string, e.g. {\"Referer\":\"...\"}", required = false) - ) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported. + |On success: "[android] File downloaded successfully to: " with file size. + |On failure: HTTP status errors (403/404 - check URL or add headers), write permission errors, timeout. Malformed headers JSON is rejected up front. + """.trimMargin() ) ) ) @@ -246,7 +331,12 @@ object SystemToolPrompts { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "例如\"/sdcard/Download\"", required = true), ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false) - ) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持(取值见 read_file)。 + |成功时返回:"[android] Directory listing for <路径>:" 后每行一条,含类型、大小、日期、文件名。 + |失败时返回:错误注明原因(目录不存在 / 无效的仓库路径),修正路径后重试。 + """.trimMargin() ), ToolPrompt( name = "read_file", @@ -283,36 +373,62 @@ object SystemToolPrompts { description = "可选,为true时:返回标签供支持视频的模型直接处理", required = false ) - ) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |输出:内容带 "N| " 行号前缀,仅供定位——严禁把前缀复制进 edit_file/apply_file 的 old。 + |限制:输出超 32000 字符会被截断并附 "... (file content truncated) ..." 标记;文件超过 32MB 直接拒绝。大文件请用 read_file_part(start_line/end_line)分段读取。 + |特殊类型(pdf/docx/图片OCR/音视频)自动提取文本;提取失败返回 success=false。 + |失败时返回:错误注明原因与修法——必须绝对路径 / 文件不存在 / 非文本文件(改用 read_file_binary)/ 超过 32MB 读取上限(改用 read_file_part)。 + """.trimMargin() ), ToolPrompt( name = "read_file_part", - description = "按行号范围读取文件内容。", + description = "按行号范围读取文件内容。大文件请用本工具分段读取。", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "文件路径", required = true), ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), ToolParameterSchema(name = "start_line", type = "integer", description = "起始行号,从1开始", required = false, default = "1"), - ToolParameterSchema(name = "end_line", type = "integer", description = "结束行号,从1开始,包括该行,可选", required = false, default = "start_line + 99") - ) + ToolParameterSchema(name = "end_line", type = "integer", description = "结束行号,从1开始,包括该行,可选;不传则读到文件末尾", required = false, default = "start_line + 199(200 行)") + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |输出:所请求的行,带 "N| " 行号前缀(N 是文件中的真实行号,仅供展示——严禁复制进 edit_file/apply_file 的 old)。输出上限 32000 字符。 + |成功时返回:会标明本次从第几行开始返回,据此衔接下一段(start_line = 上次末行 + 1)。 + |失败时返回:错误注明原因(路径无效 / start_line 超出文件末尾 / 非文本文件)。 + """.trimMargin() ), ToolPrompt( name = "create_file", - description = "通过委托给 apply_file 且 type=create 来创建新文件。", + description = "用给定内容创建新文件。文件已存在时会失败。", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "文件路径", required = true), ToolParameterSchema(name = "new", type = "string", description = "新文件的完整内容", required = true), ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false) - ) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"。 + |父目录会自动创建。 + |成功时返回:"[android] Successfully created new file: <路径>"。 + |失败时返回:"file already exists"——要整体替换已有文件,先 delete_file 再 write_file(create_file 绝不覆盖)。 + """.trimMargin() ), ToolPrompt( name = "edit_file", - description = "通过委托给 apply_file 且 type=replace 来编辑已存在文件。", + description = "编辑已有文件:将唯一的代码片段(old)替换为 new 内容。匹配先精确、后模糊(空白容忍)。", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "文件路径", required = true), - ToolParameterSchema(name = "old", type = "string", description = "用于匹配并替换的原始内容", required = true), + ToolParameterSchema(name = "old", type = "string", description = "被替换的片段——必须在文件中唯一;带上足够的上下文以消歧;不要带 read_file 的行号前缀", required = true), ToolParameterSchema(name = "new", type = "string", description = "要插入的新内容", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false) - ) + ToolParameterSchema(name = "occurrence", type = "integer", description = "可选,old 命中多处时定位第 N 处(报错会列出候选位置);不传 occurrence 会被拒绝,不会默认取第 1 处", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)| \"linux\"——不支持 repo:<名称>", required = false) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"。 + |匹配规则:1) 空白容忍的精确匹配;2) 相似度 > 0.9 的模糊匹配。片段命中多处时编辑会被拒绝并列出候选行号——给 old 补充上下文,或用 occurrence 参数定位第 N 处。 + |成功时返回:"[android] Successfully applied AI code to file: <路径>",附统一 diff 与被替换的行号范围。 + |失败时返回:"Could not find a match"(用 read_file_part 精读目标行后重试)或 "multiple perfect matches"(让 old 更唯一)。严禁改用 shell 命令编辑文件——继续使用本工具。 + """.trimMargin() ), ToolPrompt( name = "delete_file", @@ -320,8 +436,14 @@ object SystemToolPrompts { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "目标路径", required = true), ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), - ToolParameterSchema(name = "recursive", type = "boolean", description = "布尔值", required = false, default = "false") - ) + ToolParameterSchema(name = "recursive", type = "boolean", description = "true=递归删除非空目录;false(默认)=仅空目录和单个文件", required = false, default = "false") + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持(repo: 忽略 recursive,由提供端整树删除)。 + |硬性规则:对系统根目录(/ 、/storage/emulated/0 、/sdcard 、/data 等)的递归删除一律拒绝。 + |成功时返回:"[android] Successfully deleted: <路径>"。 + |失败时返回:"Directory is not empty and recursive flag is not set"——传 recursive=true,或先删子项。 + """.trimMargin() ), ToolPrompt( name = "make_directory", @@ -329,8 +451,13 @@ object SystemToolPrompts { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "目录路径", required = true), ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), - ToolParameterSchema(name = "create_parents", type = "boolean", description = "布尔值", required = false, default = "false") - ) + ToolParameterSchema(name = "create_parents", type = "boolean", description = "true=一并创建缺失的父目录(mkdirs);false(默认)=父目录必须已存在", required = false, default = "false") + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |成功时返回:"[android] Directory created: <路径>";目录已存在时返回成功 "Directory already exists"。 + |失败时返回:"parent directory does not exist"——传 create_parents=true,或先创建父目录。 + """.trimMargin() ), ToolPrompt( name = "find_files", @@ -340,33 +467,50 @@ object SystemToolPrompts { ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), ToolParameterSchema(name = "pattern", type = "string", description = "搜索模式,例如\"*.jpg\"", required = true), ToolParameterSchema(name = "max_depth", type = "integer", description = "可选,控制子目录搜索深度,-1=无限", required = false), - ToolParameterSchema(name = "use_path_pattern", type = "boolean", description = "布尔值", required = false, default = "false"), - ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "布尔值", required = false, default = "false") - ) + ToolParameterSchema(name = "use_path_pattern", type = "boolean", description = "true=对完整路径匹配模式,而非仅文件名", required = false, default = "false"), + ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "true=大小写不敏感匹配", required = false, default = "false") + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |成功时返回:"Found N files:",最多列出 10 条路径——命中更多时尾部缩略("... and N other files"),需要看全请缩小搜索范围(更深的路径、更紧的模式)。 + |失败时返回:"search path does not exist"——修正路径。 + """.trimMargin() ), ToolPrompt( name = "grep_code", description = "在文件中搜索匹配正则表达式的代码内容,返回带上下文的匹配结果。", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "搜索路径", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)或 \"linux\"——内容搜索不支持 repo:<名称>", required = false), ToolParameterSchema(name = "pattern", type = "string", description = "正则表达式模式", required = true), ToolParameterSchema(name = "file_pattern", type = "string", description = "文件过滤", required = false, default = "\"*\""), - ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "布尔值", required = false, default = "false"), + ToolParameterSchema(name = "case_insensitive", type = "boolean", description = "true=大小写不敏感", required = false, default = "false"), ToolParameterSchema(name = "context_lines", type = "integer", description = "匹配行前后的上下文行数", required = false, default = "3"), ToolParameterSchema(name = "max_results", type = "integer", description = "最大匹配数", required = false, default = "100") - ) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"——调用会被拒绝并给出明确错误。 + |遵循 .gitignore 规则;二进制文件自动跳过。 + |成功时返回:按文件分组的匹配(最多列 20 个文件;同时报告总匹配数,必要时收窄 pattern)。 + |失败时返回:"Error performing native grep search"——检查路径是否存在、正则是否合法。 + """.trimMargin() ), ToolPrompt( name = "grep_context", description = "基于意图/上下文理解搜索相关内容。支持两种模式:1) 目录模式:当path是目录时,找出最相关的文件。2) 文件模式:当path是文件时,找出该文件内最相关的代码段。使用语义相关性评分。", parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "目录或文件路径", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)或 \"linux\"——不支持 repo:<名称>", required = false), ToolParameterSchema(name = "intent", type = "string", description = "意图或上下文描述字符串", required = true), ToolParameterSchema(name = "file_pattern", type = "string", description = "目录模式下的文件过滤", required = false, default = "\"*\""), ToolParameterSchema(name = "max_results", type = "integer", description = "返回的最大项数", required = false, default = "10") - ) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"——调用会被拒绝并给出明确错误。 + |内部需要多轮模型辅助,耗时高于 grep_code——已知具体模式时优先用 grep_code。 + |成功时返回:与意图最相关的文件路径或代码段(按相关度排序)。 + |失败时返回:"Error performing context search"——把 intent 写得更具体,或退回 grep_code 用具体正则。 + """.trimMargin() ), ToolPrompt( name = "download_file", @@ -376,10 +520,15 @@ object SystemToolPrompts { ToolParameterSchema(name = "visit_key", type = "string", description = "可选, 上一次 visit_web 返回的 visitKey", required = false), ToolParameterSchema(name = "link_number", type = "integer", description = "可选, 整数, Results 中的链接编号(从1开始,需要配合 visit_key)", required = false), ToolParameterSchema(name = "image_number", type = "integer", description = "可选, 整数, Images 中的图片编号(从1开始,需要配合 visit_key)", required = false), - ToolParameterSchema(name = "destination", type = "string", description = "保存路径", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment", required = false), + ToolParameterSchema(name = "destination", type = "string", description = "目标文件系统上的绝对保存路径", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)或 \"linux\"——不支持 repo:<名称>", required = false), ToolParameterSchema(name = "headers", type = "string", description = "可选:HTTP请求头,JSON对象字符串,例如{\"Referer\":\"...\"}", required = false) - ) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"。 + |成功时返回:"[android] File downloaded successfully to: ",附文件大小。 + |失败时返回:HTTP 状态错误(403/404——检查 URL 或补 headers)、写权限错误、超时。headers JSON 格式非法会在执行前被拒绝。 + """.trimMargin() ) ) ) @@ -780,13 +929,15 @@ object SystemToolPrompts { ) } category.tools.add( - ToolPrompt( - name = toolName, - description = description, - parameters = item["parameters"] as? String ?: "", - parametersStructured = parseToolParameterSchemas(item["parametersStructured"]), - details = item["details"] as? String ?: "", - notes = item["notes"] as? String ?: "" + withCallPurposeParamDoc( + ToolPrompt( + name = toolName, + description = description, + parameters = item["parameters"] as? String ?: "", + parametersStructured = parseToolParameterSchemas(item["parametersStructured"]), + details = item["details"] as? String ?: "", + notes = item["notes"] as? String ?: "" + ) ) ) } @@ -800,6 +951,25 @@ object SystemToolPrompts { } } + /** + * 修改意图:为可审查工具(HighRiskToolCatalog 高危工具 + 文件工具族)补挂 + * call_purpose(调用说明)参数文档。工具提示词 hook 管线基于 Map 往返重建 ToolPrompt, + * 上游注入会被往返丢弃,故在重建点(buildToolPromptCategories)统一注入, + * 覆盖 generateToolsPromptEn/Cn 的全部文本渲染路径。 + */ + private fun withCallPurposeParamDoc(tool: ToolPrompt): ToolPrompt { + if (!HighRiskToolCatalog.isReviewableTool(tool.name)) { + return tool + } + val structured = tool.parametersStructured ?: emptyList() + if (structured.any { it.name == HighRiskToolCatalog.CALL_PURPOSE_PARAM }) { + return tool + } + return tool.copy( + parametersStructured = structured + HighRiskToolCatalog.callPurposeParamSchema() + ) + } + private fun parseToolParameterSchemas(value: Any?): List { val items = value as? List<*> ?: return emptyList() return items.mapNotNull { item -> diff --git a/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPromptsInternal.kt b/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPromptsInternal.kt index 3206b09edd..45dfd51ec7 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPromptsInternal.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/config/SystemToolPromptsInternal.kt @@ -31,15 +31,17 @@ object SystemToolPromptsInternal { parametersStructured = listOf( ToolParameterSchema(name = "path", type = "string", description = "file path", required = true), - ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment", required = false), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" - repo: is not supported", required = false), ToolParameterSchema(name = "type", type = "string", description = "operation type: replace | delete | create", required = true), - ToolParameterSchema(name = "old", type = "string", description = "the exact content to be matched and replaced/deleted (required for replace/delete)", required = false), - ToolParameterSchema(name = "new", type = "string", description = "the new content to insert (required for replace/create)", required = false) + ToolParameterSchema(name = "old", type = "string", description = "the content block to be matched and replaced/deleted (required for replace/delete); must be UNIQUE in the file; do not include read_file line-number prefixes", required = false), + ToolParameterSchema(name = "new", type = "string", description = "the new content to insert (required for replace/create)", required = false), + ToolParameterSchema(name = "occurrence", type = "integer", description = "optional, 1-based index of the match to operate on when old matches multiple locations (the error lists the candidates); omitted occurrence means the operation is rejected, not defaulted to 1", required = false) ), details = """ - **How it works**: - - The tool finds the best fuzzy match of `old` in the current file content (not by line numbers) and applies the requested operation. + - Matching strategy: 1) whitespace-tolerant exact match; 2) fuzzy similarity > 0.9. If `old` matches MULTIPLE locations, the operation is REJECTED with candidate line numbers - add more surrounding context to `old`, or pass `occurrence` to target the Nth match. - You can call this tool multiple times to apply multiple independent edits. + - Do NOT strip line numbers with shell tools; read_file/read_file_part output prefixes "N| " are removed automatically before matching. - **Parameters**: - `type`: @@ -49,9 +51,14 @@ object SystemToolPromptsInternal { - `old`: required for `replace` / `delete` - `new`: required for `replace` / `create` + - **Result format**: + - On success: "[android] Successfully applied AI code to file: " plus a unified diff and the replaced line range ("Replaced at lines X-Y"). + - On failure: "Could not find a match for an OLD block" (re-read the target lines with read_file_part, refine `old`, retry) / "Found multiple perfect matches" / "Found N similar matches" (make `old` unique or pass `occurrence`). + - **CRITICAL RULES**: - 1. **If you need to rewrite a whole existing file**: do **NOT** use apply_file to overwrite it. Instead, call `delete_file` first, then use `apply_file` with `type=create`. + 1. **If you need to rewrite a whole existing file**: do **NOT** use apply_file to overwrite it. Instead, call `delete_file` first, then `write_file` with the full content. 2. **If you need to modify an existing file**: you **MUST** use `type=replace` (or `type=delete`) and provide `old` / `new`. Do **NOT** delete the whole file and rewrite it. + 3. **Do NOT fall back to shell commands** (sed/awk/echo) to edit files - keep using this tool or edit_file. """ ), ToolPrompt( @@ -747,16 +754,26 @@ object SystemToolPromptsInternal { name = "file_exists", description = "Check if a file or directory exists.", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "target path", required = true) - ) + ToolParameterSchema(name = "path", type = "string", description = "target path", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment (android | linux | repo:)", required = false) + ), + details = """ + |On success: "[android] File exists at path: " or "[android] No file or directory exists at path: ". A "does not exist" answer is a SUCCESSFUL query, not an error. + """.trimMargin() ), ToolPrompt( name = "move_file", description = "Move or rename a file or directory.", parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "source path", required = true), - ToolParameterSchema(name = "destination", type = "string", description = "destination path", required = true) - ) + ToolParameterSchema(name = "destination", type = "string", description = "destination path", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" - repo: is not supported for move", required = false) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported - the call is rejected with a clear error. + |On success: "[android] Successfully moved to ". + |On failure: permission or destination-exists errors; fix per the message. + """.trimMargin() ), ToolPrompt( name = "copy_file", @@ -764,48 +781,83 @@ object SystemToolPromptsInternal { parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "source path", required = true), ToolParameterSchema(name = "destination", type = "string", description = "destination path", required = true), - ToolParameterSchema(name = "recursive", type = "boolean", description = "boolean", required = false, default = "false"), + ToolParameterSchema(name = "recursive", type = "boolean", description = "true = copy directories recursively; false (default) = single file / empty dir only", required = false, default = "false"), ToolParameterSchema(name = "source_environment", type = "string", description = "optional, \"android\" or \"linux\"", required = false, default = "\"android\""), ToolParameterSchema(name = "dest_environment", type = "string", description = "optional, \"android\" or \"linux\". For cross-environment copy (e.g., Android → Linux or Linux → Android), specify both source_environment and dest_environment", required = false, default = "\"android\"") - ) + ), + details = """ + |Environment: "android" | "linux" (also per-side via source_environment/dest_environment). "repo:" is not supported for copy. + |Directories need recursive=true, otherwise the call fails with a clear error. + |On success: "[android] Successfully copied to ". + |On failure: "Failed to read source file" / destination errors - verify both paths exist in their stated environments. + """.trimMargin() ), ToolPrompt( name = "file_info", description = "Get detailed information about a file or directory including type, size, permissions, owner, group, and last modified time.", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "target path", required = true) - ) + ToolParameterSchema(name = "path", type = "string", description = "target path", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "optional, same as read_file environment (android | linux | repo:)", required = false) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. + |On success: a stat block (type/size/permissions/owner/modified). Permission strings on repo: are provider-level approximations. + |On failure: "File does not exist" - fix the path. + """.trimMargin() ), ToolPrompt( name = "zip_files", description = "Compress files or directories.", parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "path to compress", required = true), - ToolParameterSchema(name = "destination", type = "string", description = "output zip file", required = true) - ) + ToolParameterSchema(name = "destination", type = "string", description = "output zip file", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" (paths resolved inside the proot rootfs) - repo: is not supported", required = false) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported - the call is rejected with a clear error. + |On success: "[android] Successfully created zip archive: ". + |On failure: source missing / destination unwritable - verify both paths in the stated environment. + """.trimMargin() ), ToolPrompt( name = "unzip_files", description = "Extract a zip file.", parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "zip file path", required = true), - ToolParameterSchema(name = "destination", type = "string", description = "extract path", required = true) - ) + ToolParameterSchema(name = "destination", type = "string", description = "extract path", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "optional, \"android\" (default) or \"linux\" (paths resolved inside the proot rootfs) - repo: is not supported", required = false) + ), + details = """ + |Environment: "android" | "linux". "repo:" is not supported - the call is rejected with a clear error. + |Zip-slip entries are rejected automatically. + |On success: "[android] Successfully extracted zip archive to: ". + |On failure: source zip missing / destination unwritable. + """.trimMargin() ), ToolPrompt( name = "open_file", description = "Open a file using the system's default application.", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "file path", required = true) - ) + ToolParameterSchema(name = "path", type = "string", description = "file path on the Android file system", required = true) + ), + details = """ + |Environment: "android" only. "linux" returns "not supported in Linux environment"; "repo:" is rejected with a clear error. + |On success: the share/open intent was dispatched (does not guarantee the app rendered the file). + |On failure: "File does not exist" or no app can handle the type (ActivityNotFoundException). + """.trimMargin() ), ToolPrompt( name = "share_file", description = "Share a file with other applications.", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "file path", required = true), + ToolParameterSchema(name = "path", type = "string", description = "file path on the Android file system", required = true), ToolParameterSchema(name = "title", type = "string", description = "optional share title", required = false, default = "\"Share File\"") - ) + ), + details = """ + |Environment: "android" only. "linux" returns "not supported in Linux environment"; "repo:" is rejected with a clear error. + |On success: the share intent was dispatched to the system chooser. + |On failure: "File does not exist" - verify the Android path. + """.trimMargin() ) ) ), @@ -1393,7 +1445,7 @@ object SystemToolPromptsInternal { listOf( ToolPrompt( name = "read_file_full", - description = "Read the full content of a file without enforcing size limit.", + description = "Read the full content of a text file without the 32000-character truncation of read_file. Files over 32 MB are rejected - use read_file_part for those. Text files over 2048 bytes are AI-distilled by default: pass focus and only the relevant lines (with original line numbers) come back.", parametersStructured = listOf( ToolParameterSchema( @@ -1402,24 +1454,44 @@ object SystemToolPromptsInternal { description = "file path", required = true ), + ToolParameterSchema( + name = "focus", + type = "string", + description = "what you want from this file; required for the default AI-distilled read, ignored when raw=true", + required = false + ), + ToolParameterSchema( + name = "raw", + type = "boolean", + description = "optional, default false: AI-distilled read returning only focus-relevant lines; set true only when you are sure you need the raw full content", + required = false + ), ToolParameterSchema( name = "environment", type = "string", - description = "optional, \"android\" (default) or \"linux\"", + description = "optional, \"android\" (default) | \"linux\" | \"repo:\"", required = false ), ToolParameterSchema( name = "text_only", type = "boolean", - description = "optional", + description = "optional, when true the tool is restricted to text-like files and skips non-text files with an explicit error", required = false, default = "false" ) - ) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported (linux/repo: are not distilled). + |AI distill (default, raw=false): text files over 2048 bytes return only the lines relevant to focus - verbatim, with original "N| " line numbers usable by edit_file, preceded by an "[AI distilled read ...]" header; "no content matching the focus" plus a file summary when nothing matches. Small files are distilled in one pass; larger ones are scanned chunk by chunk, and the result notes when the scan stopped before the end of the file. Unless you are sure you need the raw content, keep the default and state focus precisely. + |raw=true: returns the full original content. Images/PDF/doc/audio/video always go through the extraction pipeline untouched. + |On AI-distill failure: the error says so - retry with raw=true to read the original content. + |raw=true Output: raw text WITHOUT line-number prefixes (apply_file matching works directly on this). + |On failure: "File does not appear to be a text file" (use read_file_binary) / "exceeds the 32 MB read limit" (use read_file_part) / file not found. + """.trimMargin() ), ToolPrompt( name = "read_file_binary", - description = "Read binary file and return base64 content.", + description = "Read a binary file and return its base64 content (the base64 is included in the result text).", parametersStructured = listOf( ToolParameterSchema( @@ -1431,14 +1503,19 @@ object SystemToolPromptsInternal { ToolParameterSchema( name = "environment", type = "string", - description = "optional, \"android\" (default) or \"linux\"", + description = "optional, \"android\" (default) | \"linux\" | \"repo:\"", required = false ) - ) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. + |Pairs with write_file_binary (param name there is base64Content) for binary copy round-trips. + |On failure: "exceeds the 32 MB read limit" - do not try to read large binaries; operate on them by path instead. + """.trimMargin() ), ToolPrompt( name = "write_file", - description = "Write content to a file.", + description = "Write text content to a file. OVERWRITES an existing file by default; set append=true to append.", parametersStructured = listOf( ToolParameterSchema( @@ -1450,27 +1527,34 @@ object SystemToolPromptsInternal { ToolParameterSchema( name = "content", type = "string", - description = "file content", + description = "full file content to write", required = true ), ToolParameterSchema( name = "append", type = "boolean", - description = "optional", + description = "optional, true = append to the existing file; false (default) = overwrite the whole file", required = false, default = "false" ), ToolParameterSchema( name = "environment", type = "string", - description = "optional, \"android\" (default) or \"linux\"", + description = "optional, \"android\" (default) | \"linux\" | \"repo:\"", required = false ) - ) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. Parent directories are created automatically. + |This is the tool for whole-file rewrites (after delete_file) and for creating files when create_file refuses to overwrite. + |For partial edits prefer edit_file/apply_file - they are cheaper than rewriting the whole file. + |On success: "[android] Successfully wrote to file: " (append variant says so). + |On failure: permission errors with hints; an empty written file is reported explicitly. + """.trimMargin() ), ToolPrompt( name = "write_file_binary", - description = "Write base64 content to a binary file.", + description = "Write base64 content to a binary file (overwrites; parent directories are created automatically).", parametersStructured = listOf( ToolParameterSchema( @@ -1482,16 +1566,21 @@ object SystemToolPromptsInternal { ToolParameterSchema( name = "base64Content", type = "string", - description = "base64 encoded content", + description = "base64 encoded content (note: this is the exact parameter name, camelCase)", required = true ), ToolParameterSchema( name = "environment", type = "string", - description = "optional, \"android\" (default) or \"linux\"", + description = "optional, \"android\" (default) | \"linux\" | \"repo:\"", required = false ) - ) + ), + details = """ + |Environment: "android" | "linux" | "repo:" - all supported. + |On success: "[android] Successfully wrote binary file: " with decoded byte size. + |On failure: invalid base64 / permission errors. + """.trimMargin() ) ) ), @@ -3745,16 +3834,26 @@ object SystemToolPromptsInternal { name = "file_exists", description = "检查文件或目录是否存在。", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "目标路径", required = true) - ) + ToolParameterSchema(name = "path", type = "string", description = "目标路径", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment(android | linux | repo:<名称>)", required = false) + ), + details = """ + |成功时返回:"[android] File exists at path: <路径>" 或 "[android] No file or directory exists at path: <路径>"。"不存在"是正常的查询结果,不是错误。 + """.trimMargin() ), ToolPrompt( name = "move_file", description = "移动或重命名文件或目录。", parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "源路径", required = true), - ToolParameterSchema(name = "destination", type = "string", description = "目标路径", required = true) - ) + ToolParameterSchema(name = "destination", type = "string", description = "目标路径", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)或 \"linux\"——移动不支持 repo:<名称>", required = false) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"——调用会被拒绝并给出明确错误。 + |成功时返回:"[android] Successfully moved <源> 到 <目标>"。 + |失败时返回:权限或目标已存在错误,按错误信息修正。 + """.trimMargin() ), ToolPrompt( name = "copy_file", @@ -3762,48 +3861,83 @@ object SystemToolPromptsInternal { parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "源路径", required = true), ToolParameterSchema(name = "destination", type = "string", description = "目标路径", required = true), - ToolParameterSchema(name = "recursive", type = "boolean", description = "布尔值", required = false, default = "false"), + ToolParameterSchema(name = "recursive", type = "boolean", description = "true=递归复制目录;false(默认)=仅单文件/空目录", required = false, default = "false"), ToolParameterSchema(name = "source_environment", type = "string", description = "可选,\"android\"或\"linux\"", required = false, default = "\"android\""), ToolParameterSchema(name = "dest_environment", type = "string", description = "可选,\"android\"或\"linux\"。跨环境复制(如Android → Linux或Linux → Android)时,需指定source_environment和dest_environment", required = false, default = "\"android\"") - ) + ), + details = """ + |Environment:"android" | "linux"(也可用 source_environment/dest_environment 分别指定)。复制不支持 "repo:<名称>"。 + |复制目录需要 recursive=true,否则失败并给出明确错误。 + |成功时返回:"[android] Successfully copied <源> 到 <目标>"。 + |失败时返回:"Failed to read source file" / 目标错误——确认两个路径在各自环境中存在。 + """.trimMargin() ), ToolPrompt( name = "file_info", description = "获取文件或目录的详细信息,包括类型、大小、权限、所有者、组和最后修改时间。", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "目标路径", required = true) - ) + ToolParameterSchema(name = "path", type = "string", description = "目标路径", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "可选,同 read_file 的 environment(android | linux | repo:<名称>)", required = false) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |成功时返回:stat 信息块(类型/大小/权限/所有者/修改时间)。repo: 的权限串是提供端近似值。 + |失败时返回:"File does not exist"——修正路径。 + """.trimMargin() ), ToolPrompt( name = "zip_files", description = "压缩文件或目录。", parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "要压缩的路径", required = true), - ToolParameterSchema(name = "destination", type = "string", description = "输出zip文件", required = true) - ) + ToolParameterSchema(name = "destination", type = "string", description = "输出zip文件", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)或 \"linux\"(路径在proot根文件系统内解析)——不支持 repo:<名称>", required = false) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"——调用会被拒绝并给出明确错误。 + |成功时返回:"[android] Successfully created zip archive: <目标>"。 + |失败时返回:源不存在 / 目标不可写——确认两个路径在对应环境中有效。 + """.trimMargin() ), ToolPrompt( name = "unzip_files", description = "解压zip文件。", parametersStructured = listOf( ToolParameterSchema(name = "source", type = "string", description = "zip文件路径", required = true), - ToolParameterSchema(name = "destination", type = "string", description = "解压路径", required = true) - ) + ToolParameterSchema(name = "destination", type = "string", description = "解压路径", required = true), + ToolParameterSchema(name = "environment", type = "string", description = "可选,\"android\"(默认)或 \"linux\"(路径在proot根文件系统内解析)——不支持 repo:<名称>", required = false) + ), + details = """ + |Environment:"android" | "linux"。不支持 "repo:<名称>"——调用会被拒绝并给出明确错误。 + |Zip-slip(路径穿越)条目会被自动拒绝。 + |成功时返回:"[android] Successfully extracted zip archive to: <目标>"。 + |失败时返回:源 zip 不存在 / 目标不可写。 + """.trimMargin() ), ToolPrompt( name = "open_file", description = "使用系统默认应用程序打开文件。", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "文件路径", required = true) - ) + ToolParameterSchema(name = "path", type = "string", description = "Android 文件系统上的文件路径", required = true) + ), + details = """ + |Environment:仅 "android"。"linux" 返回 "not supported in Linux environment";"repo:<名称>" 会被拒绝并给出明确错误。 + |成功时返回:系统打开意图已发出(不保证目标应用成功渲染文件)。 + |失败时返回:"File does not exist" 或无应用可处理该类型(ActivityNotFoundException)。 + """.trimMargin() ), ToolPrompt( name = "share_file", description = "与其他应用程序共享文件。", parametersStructured = listOf( - ToolParameterSchema(name = "path", type = "string", description = "文件路径", required = true), + ToolParameterSchema(name = "path", type = "string", description = "Android 文件系统上的文件路径", required = true), ToolParameterSchema(name = "title", type = "string", description = "可选的共享标题", required = false, default = "\"Share File\"") - ) + ), + details = """ + |Environment:仅 "android"。"linux" 返回 "not supported in Linux environment";"repo:<名称>" 会被拒绝并给出明确错误。 + |成功时返回:共享意图已发送到系统分享面板。 + |失败时返回:"File does not exist"——确认 Android 路径。 + """.trimMargin() ) ) ), @@ -4391,7 +4525,7 @@ object SystemToolPromptsInternal { listOf( ToolPrompt( name = "read_file_full", - description = "读取完整文件内容(不限制大小)。", + description = "读取完整文本文件内容,没有 read_file 的 32000 字符截断。超过 32MB 的文件会被拒绝——请改用 read_file_part。超过 2048 字节的文本文件默认走 AI 提炼:传入 focus,只返回相关行(带原文行号)。", parametersStructured = listOf( ToolParameterSchema( @@ -4400,24 +4534,44 @@ object SystemToolPromptsInternal { description = "文件路径", required = true ), + ToolParameterSchema( + name = "focus", + type = "string", + description = "你想从该文件读取什么;默认 AI 提炼读取必填,raw=true 时忽略", + required = false + ), + ToolParameterSchema( + name = "raw", + type = "boolean", + description = "可选,默认 false:AI 提炼读取只返回与 focus 相关的行;仅确认需要完整原文时设为 true", + required = false + ), ToolParameterSchema( name = "environment", type = "string", - description = "可选,\"android\"(默认)或 \"linux\"", + description = "可选,\"android\"(默认)| \"linux\" | \"repo:<名称>\"", required = false ), ToolParameterSchema( name = "text_only", type = "boolean", - description = "可选", + description = "可选,为 true 时仅限文本类文件,非文本文件明确报错跳过", required = false, default = "false" ) - ) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持(linux/repo: 不做提炼)。 + |AI 提炼(默认,raw=false):超过 2048 字节的文本文件只返回与 focus 相关的行——原文摘录、保留可直接用于 edit_file 的 "N| " 行号、头部带 "[AI distilled read ...]" 声明;无相关内容时返回"未找到"+文件概要。小文件单次提炼,更大的文件逐块扫描,扫描未到文件尾时结果中会明示。除非确认需要完整原文,保持默认并精确描述 focus。 + |raw=true:返回完整原文。图片/PDF/文档/音视频始终走原抽取管线,不受影响。 + |提炼失败时:报错中会说明——用 raw=true 重试即可读取原文。 + |raw=true 输出:原始文本,不带 "N| " 行号前缀(可直接用于 apply_file 匹配)。 + |失败时返回:"File does not appear to be a text file"(改用 read_file_binary)/ "exceeds the 32 MB read limit"(改用 read_file_part)/ 文件不存在。 + """.trimMargin() ), ToolPrompt( name = "read_file_binary", - description = "读取二进制文件并返回 Base64 内容。", + description = "读取二进制文件并返回 Base64 内容(base64 会包含在结果文本中)。", parametersStructured = listOf( ToolParameterSchema( @@ -4429,14 +4583,19 @@ object SystemToolPromptsInternal { ToolParameterSchema( name = "environment", type = "string", - description = "可选,\"android\"(默认)或 \"linux\"", + description = "可选,\"android\"(默认)| \"linux\" | \"repo:<名称>\"", required = false ) - ) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |与 write_file_binary(其参数名为 base64Content)配合可完成二进制复制往返。 + |失败时返回:"exceeds the 32 MB read limit"——不要读取大体积二进制,改为按路径直接操作。 + """.trimMargin() ), ToolPrompt( name = "write_file", - description = "写入文件内容。", + description = "写入文本文件。默认覆盖已有文件;append=true 时为追加。", parametersStructured = listOf( ToolParameterSchema( @@ -4448,27 +4607,34 @@ object SystemToolPromptsInternal { ToolParameterSchema( name = "content", type = "string", - description = "文件内容", + description = "要写入的完整文件内容", required = true ), ToolParameterSchema( name = "append", type = "boolean", - description = "可选", + description = "可选,true=追加到已有文件;false(默认)=整体覆盖", required = false, default = "false" ), ToolParameterSchema( name = "environment", type = "string", - description = "可选,\"android\"(默认)或 \"linux\"", + description = "可选,\"android\"(默认)| \"linux\" | \"repo:<名称>\"", required = false ) - ) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。父目录自动创建。 + |整体重写文件(delete_file 之后)用它;create_file 拒绝覆盖时也用它。 + |局部修改优先用 edit_file/apply_file——比整文件重写更省。 + |成功时返回:"[android] Successfully wrote to file: <路径>"(追加会注明)。 + |失败时返回:权限错误(含修法提示);写出的文件为空会被明确报告。 + """.trimMargin() ), ToolPrompt( name = "write_file_binary", - description = "将 Base64 内容写入二进制文件。", + description = "将 Base64 内容写入二进制文件(覆盖写;父目录自动创建)。", parametersStructured = listOf( ToolParameterSchema( @@ -4480,16 +4646,21 @@ object SystemToolPromptsInternal { ToolParameterSchema( name = "base64Content", type = "string", - description = "Base64 编码内容", + description = "Base64 编码内容(注意:参数名就是 base64Content,驼峰命名)", required = true ), ToolParameterSchema( name = "environment", type = "string", - description = "可选,\"android\"(默认)或 \"linux\"", + description = "可选,\"android\"(默认)| \"linux\" | \"repo:<名称>\"", required = false ) - ) + ), + details = """ + |Environment:"android" | "linux" | "repo:<名称>"——全部支持。 + |成功时返回:"[android] Successfully wrote binary file: <路径>",附解码后字节数。 + |失败时返回:base64 非法 / 权限错误。 + """.trimMargin() ) ) ), diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/AIToolHandler.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/AIToolHandler.kt index b91528ad4a..405c2e18d9 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/AIToolHandler.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/AIToolHandler.kt @@ -51,10 +51,18 @@ class AIToolHandler private constructor(private val context: Context) { // Tool permission system private val toolPermissionSystem = ToolPermissionSystem.getInstance(context) + // 修改意图:记录最近一次 usePackage 的返回消息(含"包未启用"等真实原因)。 + // 此前该返回值被丢弃,包禁用时模型只能看到笼统的 "Tool not found",无法自纠 + @Volatile + private var lastPackageActivationMessage: String? = null + /** Get the tool permission system for UI use */ fun getToolPermissionSystem(): ToolPermissionSystem { return toolPermissionSystem } + + /** 最近一次包自动激活失败的原因(来自 PackageManager.usePackage 的返回),无则为 null */ + fun getLastPackageActivationMessage(): String? = lastPackageActivationMessage fun unregisterTool(toolName: String) { availableTools.remove(toolName) @@ -321,7 +329,9 @@ class AIToolHandler private constructor(private val context: Context) { val isMcpAvailable = packageManager.getAvailableServerPackages().containsKey(packageName) if (isPackageAvailable || isMcpAvailable) { AppLogger.d(TAG, "Auto-activating package '$packageName' for tool $toolName") - packageManager.usePackage(packageName) + // 修改意图:捕获 usePackage 的返回消息(含"包未启用"真实原因), + // 供工具不可用错误信息回传给模型 + lastPackageActivationMessage = packageManager.usePackage(packageName) executor = availableTools[toolName] } } catch (e: Exception) { diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/HighRiskToolCatalog.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/HighRiskToolCatalog.kt new file mode 100644 index 0000000000..45307ab166 --- /dev/null +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/HighRiskToolCatalog.kt @@ -0,0 +1,108 @@ +package com.ai.assistance.operit.core.tools + +import com.ai.assistance.operit.data.model.ToolParameterSchema + +/** + * 高危工具调用大模型审查的纯函数判定器(三开关之「高危工具」开关的适用范围)。 + * + * 与 [WorkspacePathChecker] 的文件工具族并列:文件工具按工作区路径判区审查, + * 本清单内的工具没有工作区路径概念,由独立的「自动审查高危工具」开关管辖。 + * + * 入选标准(需求确认的核心破坏集):任意命令/代码执行、跨应用动作、 + * 应用与系统管理、网络外发、可代理执行任意工具的入口、工作流编排。 + */ +object HighRiskToolCatalog { + + /** + * 调用说明参数名(审查声明):可审查工具调用时可附带一句"为什么发起本次调用", + * 供审查模型与调用点上下文做一致性比对。参数可选,模型未提供不构成违规; + * 该参数仅供审查流读取,执行前由 ToolExecutionManager 从工具参数剥离。 + */ + const val CALL_PURPOSE_PARAM = "call_purpose" + + /** 高危工具族(审查适用范围,共 18 个) */ + val HIGH_RISK_TOOLS: Set = setOf( + // 命令/代码执行 + "execute_shell", + "execute_in_terminal_session", + "execute_in_terminal_session_streaming", + "execute_hidden_terminal_command", + "input_in_terminal_session", + "execute_sandbox_script_direct", + // Intent/广播 + "execute_intent", + "send_broadcast", + // 应用与系统管理 + "install_app", + "uninstall_app", + "stop_app", + "modify_system_setting", + // 网络外发 + "http_request", + "multipart_request", + // 代理(可执行任意工具的入口) + "use_package", + "package_proxy", + "proxy", + // 编排 + "trigger_workflow", + ) + + fun isHighRiskTool(toolName: String): Boolean { + if (toolName in HIGH_RISK_TOOLS) return true + // 修改意图:扩展包工具(如 somepkg:http_request)与内置高危工具同族, + // 按限定名后缀纳入同一套审查,与 isWorkspaceProtectedTool 的匹配模式一致 + val localName = toolName.substringAfter(':', "") + return localName.isNotEmpty() && localName in HIGH_RISK_TOOLS + } + + /** 可审查工具本地名全集(高危工具 + 文件工具族):审查判定与调用说明参数文档注入的统一口径 */ + val REVIEWABLE_TOOL_LOCAL_NAMES: Set = + HIGH_RISK_TOOLS + WorkspacePathChecker.WORKSPACE_PROTECTED_FILE_TOOLS + + /** 是否为可审查工具(含限定名后缀匹配,与审查门控同一匹配模式) */ + fun isReviewableTool(toolName: String): Boolean = + REVIEWABLE_TOOL_LOCAL_NAMES.contains(toolName.substringAfter(':')) + + /** + * 调用说明参数的文档 schema(required=false,模型可省略)。 + * 文本提示词与 function-calling schema 两条注入路径共用,保证口径一致。 + */ + fun callPurposeParamSchema(): ToolParameterSchema = + ToolParameterSchema( + name = CALL_PURPOSE_PARAM, + type = "string", + description = + "optional, one short sentence declaring WHY this call is needed (purpose and " + + "expected effect), used for AI compliance review; " + + "可选:一句话说明本次调用的目的与预期效果,用于 AI 合规审查", + required = false + ) + + /** 高危工具的英文操作摘要,供审查 prompt 描述本次调用;非高危工具返回 null */ + fun operationSummary(toolName: String): String? { + val localName = toolName.substringAfter(':', "") + return when (localName) { + "execute_shell" -> "execute an arbitrary shell command" + "execute_in_terminal_session", "execute_in_terminal_session_streaming" -> + "execute a command in a terminal session" + "execute_hidden_terminal_command" -> + "execute a hidden terminal command without visible UI" + "input_in_terminal_session" -> + "send input into a running terminal session (may drive an interactive shell/REPL)" + "execute_sandbox_script_direct" -> "execute a sandbox script (arbitrary code)" + "execute_intent" -> "fire an Android Intent to start an activity/service across apps" + "send_broadcast" -> "send a system or app broadcast" + "install_app" -> "install an application" + "uninstall_app" -> "uninstall an application" + "stop_app" -> "force-stop an application" + "modify_system_setting" -> "modify system settings" + "http_request" -> "perform an arbitrary HTTP request (potential data exfiltration)" + "multipart_request" -> "perform a multipart HTTP upload (potential data exfiltration)" + "use_package" -> "invoke an arbitrary package tool (proxy execution)" + "package_proxy", "proxy" -> "proxy-execute another tool by name" + "trigger_workflow" -> "trigger a saved workflow that runs a chain of tools" + else -> null + } + } +} diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/ToolExecutionLimits.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/ToolExecutionLimits.kt index 2fc2512a6c..500d56f470 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/ToolExecutionLimits.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/ToolExecutionLimits.kt @@ -4,6 +4,41 @@ object ToolExecutionLimits { const val MAX_FILE_READ_BYTES = 32_000 const val DEFAULT_FILE_READ_PART_LINES = 200 const val MAX_TEXT_RESULT_LENGTH = 5_000 + + /** + * 整文件读取类工具(read_file / read_file_full / read_file_binary 及特殊文件抽取路径)的 + * 文件大小硬上限。超过该大小的文件统一拒绝并引导模型改用 read_file_part 按行分段读取, + * 避免无界 readText/readBytes 造成 OOM。 + */ + const val MAX_FILE_READ_SIZE_BYTES: Long = 32L * 1024 * 1024 + /** Maximum serialized size of one tool result sent back to the model. */ const val MAX_SINGLE_TOOL_RESULT_MESSAGE_CHARS = MAX_FILE_READ_BYTES * 2 + + /** + * AI 读取提炼的最小文件字节数:低于该阈值的文件直接返回原文, + * 提炼节省的上下文不足以抵消一次功能模型调用的延迟与费用。 + */ + const val FILE_DISTILL_MIN_BYTES = 2048 + + /** + * 单次提炼协议的输入上限(文件字节数):read_file_full 纯文本在此以下整读进一次 + * 提炼调用,以上进入扫描协议逐块提炼 + */ + const val FILE_DISTILL_SINGLE_INPUT_BYTES = 64_000 + + /** 扫描协议(read_file_part / 大文件 full 提炼)每轮送入提炼模型的文本块字符数 */ + const val FILE_DISTILL_SCAN_CHUNK_CHARS = 24_000 + + /** 扫描协议最大轮数;触顶显式返回"已找到内容 + 扫描未到文件尾",不静默截断 */ + const val FILE_DISTILL_SCAN_MAX_ROUNDS = 8 + + /** 扫描结束时,累计摘录超过已扫原文该比例则视为"大部分内容都相关",直接返回原文 */ + const val FILE_DISTILL_EXCERPT_RATIO_LIMIT = 0.6 + + /** 单次提炼协议调用的总超时(毫秒) */ + const val FILE_DISTILL_SINGLE_TIMEOUT_MS = 30_000L + + /** 扫描提炼协议单轮调用的超时(毫秒) */ + const val FILE_DISTILL_SCAN_ROUND_TIMEOUT_MS = 20_000L } diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/ToolRegistration.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/ToolRegistration.kt index f90cc5829e..311f001893 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/ToolRegistration.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/ToolRegistration.kt @@ -2,6 +2,7 @@ package com.ai.assistance.operit.core.tools import android.content.Context import com.ai.assistance.operit.R +import com.ai.assistance.operit.api.chat.enhance.FileDistillService import com.ai.assistance.operit.api.chat.enhance.ToolExecutionManager import com.ai.assistance.operit.core.tools.climode.CliToolModeSupport import com.ai.assistance.operit.core.tools.climode.ToolExposureMode @@ -1794,7 +1795,17 @@ fun registerAllTools(handler: AIToolHandler, context: Context) { val envInfo = formatEnvInfo(environment) s(R.string.toolreg_read_file_desc, path, envInfo) }, - executor = { tool -> runBlocking(Dispatchers.IO) { fileSystemTools.readFile(tool) } } + // 修改意图:read_file 默认走 AI 提炼(raw=true/特殊类型抽取/≤2048B/开关关/ + // repo:·linux 环境透传原始读取),透传与失败语义见 FileDistillService + executor = { tool -> + runBlocking(Dispatchers.IO) { + FileDistillService.maybeDistillRead( + context, + tool, + FileDistillService.Variant.READ_FILE, + ) { fileSystemTools.readFile(tool) } + } + } ) // 按行号范围读取文件内容 @@ -1814,8 +1825,16 @@ fun registerAllTools(handler: AIToolHandler, context: Context) { } s(R.string.toolreg_read_file_part_desc, rangeInfo, path, envInfo) }, + // 修改意图:read_file_part 默认走 AI 扫描提炼(focus 遍寻全文自动定位, + // start_line/end_line 转为可选的扫描范围限定;raw=true/门禁/开关/环境透传原始读取) executor = { tool -> - runBlocking(Dispatchers.IO) { fileSystemTools.readFilePart(tool) } + runBlocking(Dispatchers.IO) { + FileDistillService.maybeDistillRead( + context, + tool, + FileDistillService.Variant.READ_PART, + ) { fileSystemTools.readFilePart(tool) } + } } ) @@ -1828,7 +1847,17 @@ fun registerAllTools(handler: AIToolHandler, context: Context) { val envInfo = formatEnvInfo(environment) s(R.string.toolreg_read_file_full_desc, path, envInfo) }, - executor = { tool -> runBlocking(Dispatchers.IO) { fileSystemTools.readFileFull(tool) } } + // 修改意图:read_file_full 纯文本默认走 AI 提炼(≤64KB 单次、更大扫描); + // 特殊类型(图片/PDF/文档/音视频)抽取管线与 raw=true/门禁/开关/环境一律透传 + executor = { tool -> + runBlocking(Dispatchers.IO) { + FileDistillService.maybeDistillRead( + context, + tool, + FileDistillService.Variant.READ_FULL, + ) { fileSystemTools.readFileFull(tool) } + } + } ) // 读取二进制文件内容(Base64编码) diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/ToolResultDataClasses.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/ToolResultDataClasses.kt index 3cd51b0f09..474096d136 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/ToolResultDataClasses.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/ToolResultDataClasses.kt @@ -238,8 +238,14 @@ data class FilePartContentData( val env: String = "android" ) : ToolResultData() { override fun toString(): String { + // 修改意图:readFilePart 流式化后 totalLines 可能为 -1(未读到文件尾、总数未知), + // 此时明确告诉模型文件还有后续内容、可从 endLine+1 继续分段读取 val partInfo = - "Part ${partIndex + 1} of $totalParts (Lines ${startLine + 1}-$endLine of $totalLines)" + if (totalLines >= 0) { + "Part ${partIndex + 1} of $totalParts (Lines ${startLine + 1}-$endLine of $totalLines)" + } else { + "Part ${partIndex + 1} of $totalParts (Lines ${startLine + 1}-$endLine; total lines unknown - file continues, chain the next segment with start_line=${endLine + 1})" + } return "[$env] $partInfo\n\n$content" } } @@ -469,7 +475,9 @@ data class BinaryFileContentData( val env: String = "android" ) : ToolResultData() { override fun toString(): String { - return "[$env] Binary content of $path (${size} bytes, base64 length=${contentBase64.length})" + // 修改意图:base64 此前只回摘要,内容永远到不了模型,read_file_binary 实际不可用; + // 现在把 base64 放进模型可见文本(调用方已通过预算检查确保不超单条消息上限) + return "[$env] Binary content of $path (${size} bytes, base64 length=${contentBase64.length}):\n$contentBase64" } } @@ -548,7 +556,9 @@ data class FileOperationData( data class FileApplyResultData( val operation: FileOperationData, val aiDiffInstructions: String, - val diffContent: String? = null + val diffContent: String? = null, + /** 修改意图:各编辑操作的实际匹配位置(1-based 行号区间),成功时回传给模型核对编辑落点 */ + val matchedRanges: List = emptyList() ) : ToolResultData() { private fun buildRequestContent(): String { val sections = mutableListOf() @@ -572,21 +582,31 @@ data class FileApplyResultData( } } + /** + * 评审 P3:CDATA 段内的 `]]>` 会提前闭合段并破坏 XML 结构(含 `]]>` 的代码 diff + * 直接截断后续内容)。按 XML 规范把该序列拆成两段 CDATA——`]]` 变为 + * `]]]]>`——拼接后语义不变。 + */ + private fun escapeCdataEnd(content: String): String = + content.replace("]]>", "]]]]>") + override fun toString(): String { val sb = StringBuilder() sb.appendLine(operation.toString()) // If diffContent is available, embed it in a custom XML-like tag for the renderer. if (diffContent != null) { - val encodedDiff = diffContent.replace("&", "&").replace("<", "<").replace(">", ">") + // 修改意图:原"转义"链是恒等替换(&→&、<→<、>→>),实际什么都没做; + // diff 内容进入 CDATA 段,无需按普通 XML 转义,仅需处理 `]]>` 序列 + val encodedDiff = diffContent sb.append("") - sb.append("") + sb.append("") sb.append("") } val requestContent = buildRequestContent() if (requestContent.isNotBlank()) { - sb.append("") + sb.append("") } if (aiDiffInstructions.isNotEmpty() && !aiDiffInstructions.startsWith("Error")) { diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/WorkspacePathChecker.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/WorkspacePathChecker.kt new file mode 100644 index 0000000000..26dcd35728 --- /dev/null +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/WorkspacePathChecker.kt @@ -0,0 +1,198 @@ +package com.ai.assistance.operit.core.tools + +import com.ai.assistance.operit.data.model.AITool +import java.io.File + +/** + * 文件工具工作区防护的纯函数判定器。 + * + * 判定规则(与需求确认一致): + * - 会话未绑定工作区(root == null)→ 所有路径一律视为工作区外(不存在可信区) + * - android 路径:canonicalPath 前缀比较(root 或 root/子路径 才算区内) + * - linux / repo: 路径无法可靠映射回宿主目录做前缀比较——当工具环境与会话绑定环境 + * 完全一致时视为区内(该环境整体即会话授权的工作区),否则视为区外 + * + * delete_file 的危险根目录硬底线独立于本判定器,见 [WorkspacePathChecker.DANGEROUS_DELETE_ROOTS]。 + */ +object WorkspacePathChecker { + + enum class Zone { INSIDE, OUTSIDE } + + /** 受工作区防护的文件工具族(防护与审查适用范围,共 24 个) */ + val WORKSPACE_PROTECTED_FILE_TOOLS: Set = setOf( + "list_files", + "read_file", + "read_file_part", + "read_file_full", + "read_file_binary", + "write_file", + "write_file_binary", + "create_file", + "edit_file", + "apply_file", + "delete_file", + "make_directory", + "find_files", + "grep_code", + "grep_context", + "download_file", + "file_exists", + "move_file", + "copy_file", + "file_info", + "zip_files", + "unzip_files", + "open_file", + "share_file", + ) + + /** 工具参数中承载路径的参数名 */ + private val PATH_PARAM_NAMES = setOf("path", "source", "destination") + + /** 递归删除的绝对禁区(精确匹配规范化后的路径) */ + val DANGEROUS_DELETE_ROOTS: Set = setOf( + "/", + "/storage", + "/storage/emulated", + "/storage/emulated/0", + "/sdcard", + "/data", + "/data/data", + "/system", + "/vendor", + "/etc", + "/mnt", + "/root", + "/home", + "/usr", + "/var", + ) + + /** 修改意图:递归删除危险根目录的硬底线。命中即拒绝,独立于工作区判定与权限模式 */ + fun isDangerousRecursiveDeleteTarget(path: String): Boolean { + // 评审 P1:模型传入的拼写变体(/sdcard/.、//sdcard、/storage/emulated/0/.)与 + // 集合的精确字符串匹配不符但指向同一目录,deleteRecursively 会真的清空。 + // 词法规范化覆盖 ./..//重复斜杠(纯字符串运算,测试可跨平台断言); + // canonical 再覆盖符号链接与文件系统层差异(路径不存在时 canonicalPath 不抛错, + // 仍按词法规范化返回)。两道比较任一命中即拒绝。 + val normalized = lexicallyNormalizePath(path) + if (normalized.isEmpty()) return false + if (normalized in DANGEROUS_DELETE_ROOTS) return true + val canonical = runCatching { File(normalized).canonicalPath }.getOrNull() ?: return false + return canonical in DANGEROUS_DELETE_ROOTS + } + + /** + * 纯词法路径规范化:折叠重复斜杠、消解 "." 与 ".."(绝对路径在根处的 ".." 停留在根)、 + * 去首尾空白与尾部斜杠。不做文件系统访问,桌面 JVM 与设备行为一致,供单测断言。 + */ + internal fun lexicallyNormalizePath(path: String): String { + val trimmed = path.trim() + if (trimmed.isEmpty()) return "" + val absolute = trimmed.startsWith('/') + val parts = trimmed.split('/').filter { it.isNotEmpty() && it != "." } + val stack = ArrayDeque() + for (part in parts) { + if (part == "..") { + val canPop = stack.isNotEmpty() && stack.last() != ".." + if (canPop) { + stack.removeLast() + } else if (!absolute) { + stack.addLast(part) + } + } else { + stack.addLast(part) + } + } + val joined = stack.joinToString("/") + return if (absolute) "/$joined" else joined + } + + fun isWorkspaceProtectedTool(toolName: String): Boolean { + if (toolName in WORKSPACE_PROTECTED_FILE_TOOLS) return true + // 修改意图:扩展包文件工具(如 extended_file_tools:copy_file)与内置文件工具同族, + // 按限定名后缀纳入同一套工作区防护 + val localName = toolName.substringAfter(':', "") + return localName.isNotEmpty() && localName in WORKSPACE_PROTECTED_FILE_TOOLS + } + + /** 从工具参数中提取全部路径(path / source / destination) */ + fun extractPaths(tool: AITool): List = + tool.parameters + .filter { it.name in PATH_PARAM_NAMES } + .map { it.value } + .filter { it.isNotBlank() } + + /** + * 判定一次文件工具调用的目标整体是否在工作区内。 + * + * @param chatWorkspaceRoot 会话绑定的工作区根(ChatHistory.workspace;null = 未绑定) + * @param chatWorkspaceEnv 会话绑定的环境(ChatHistory.workspaceEnv;null 视为 android) + * @return 多路径时(move/copy)所有路径都在区内才算 INSIDE;任一路径在区外即 OUTSIDE + */ + fun resolveZone( + chatWorkspaceRoot: String?, + chatWorkspaceEnv: String?, + tool: AITool, + ): Zone { + val paths = extractPaths(tool) + if (paths.isEmpty()) { + // 修改意图:文件工具没有可提取的路径参数属于异常调用,按区外保守处理 + return Zone.OUTSIDE + } + val environment = tool.parameters.find { it.name == "environment" }?.value + val zones = paths.map { path -> + resolvePathZone( + chatWorkspaceRoot = chatWorkspaceRoot, + chatWorkspaceEnv = chatWorkspaceEnv, + path = path, + pathEnvironment = environment, + ) + } + return if (zones.all { it == Zone.INSIDE }) Zone.INSIDE else Zone.OUTSIDE + } + + /** + * 单路径判定。move/copy 的 source/destination 可各自带 *_environment 参数; + * 这里只按主 environment 参数解析(跨环境复制的精确判定由 copy_file 自身的校验承担)。 + */ + private fun resolvePathZone( + chatWorkspaceRoot: String?, + chatWorkspaceEnv: String?, + path: String, + pathEnvironment: String?, + ): Zone { + // 需求确认:未绑定工作区时不存在可信区,所有路径按区外 + if (chatWorkspaceRoot.isNullOrBlank()) return Zone.OUTSIDE + + val effectiveToolEnv = pathEnvironment?.trim()?.takeIf { it.isNotBlank() } ?: "android" + val effectiveWorkspaceEnv = chatWorkspaceEnv?.trim()?.takeIf { it.isNotBlank() } ?: "android" + + return when { + // android 路径 vs android 工作区:canonicalPath 前缀比较 + effectiveToolEnv == "android" && effectiveWorkspaceEnv == "android" -> + if (isAndroidPathInside(chatWorkspaceRoot, path)) Zone.INSIDE else Zone.OUTSIDE + + // linux / repo: 无法做宿主目录前缀比较——环境完全一致即视为区内 + effectiveToolEnv == effectiveWorkspaceEnv && + (effectiveToolEnv == "linux" || effectiveToolEnv.startsWith("repo:")) -> Zone.INSIDE + + else -> Zone.OUTSIDE + } + } + + private fun isAndroidPathInside(workspaceRoot: String, path: String): Boolean { + // 评审补记:canonicalPath 的分隔符随平台变化(Windows 桌面 JVM 为 '\',设备为 '/'), + // 前缀比较前必须归一到 '/',否则纯 JVM 单测在 Windows 上全部误判 OUTSIDE。 + // Android 设备上 canonicalPath 本就用 '/',该归一是 no-op。 + val root = canonicalizeForComparison(workspaceRoot).trimEnd('/') + val target = canonicalizeForComparison(path) + if (root.isEmpty()) return false + return target == root || target.startsWith("$root/") + } + + private fun canonicalizeForComparison(path: String): String = + runCatching { File(path).canonicalPath } + .getOrDefault(path) + .let { if (File.separatorChar == '\\') it.replace('\\', '/') else it } +} diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/LinuxFileSystemTools.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/LinuxFileSystemTools.kt index 95e4580c2f..1ff5639f20 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/LinuxFileSystemTools.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/LinuxFileSystemTools.kt @@ -14,10 +14,12 @@ import com.ai.assistance.operit.core.tools.GrepResultData import com.ai.assistance.operit.core.tools.StringResultData import com.ai.assistance.operit.core.tools.ToolProgressBus import com.ai.assistance.operit.core.tools.ToolExecutionLimits +import com.ai.assistance.operit.core.tools.WorkspacePathChecker import com.ai.assistance.operit.data.model.AITool import com.ai.assistance.operit.data.model.ToolParameter import com.ai.assistance.operit.data.model.ToolResult import com.ai.assistance.operit.util.FileUtils +import com.ai.assistance.operit.util.PathMapper import com.ai.assistance.operit.core.tools.defaultTool.PathValidator import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex @@ -144,8 +146,20 @@ class LinuxFileSystemTools(context: Context) : StandardFileSystemTools(context) ) } + // 修改意图:与 Android 环境一致,read_file_full 整文件读取受 32MB 硬上限约束 + val sizeBytes = fs.getFileSize(path) + if (sizeBytes > ToolExecutionLimits.MAX_FILE_READ_SIZE_BYTES) { + val sizeMb = sizeBytes / (1024.0 * 1024.0) + return ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "File exceeds the 32 MB read limit (actual %.1f MB): %s. Use read_file_part with start_line/end_line to read it in segments.".format(sizeMb, path) + ) + } + val fileExt = path.substringAfterLast('.', "").lowercase() - + // 特殊文件类型处理(图片、PDF等)暂时不支持在Linux环境 // 因为这些需要Android本地文件访问 if (fileExt in listOf("doc", "docx", "pdf", "jpg", "jpeg", "png", "gif", "bmp")) { @@ -236,6 +250,19 @@ class LinuxFileSystemTools(context: Context) : StandardFileSystemTools(context) ) } + // 修改意图:与 Android 环境一致,read_file_binary 整文件读取受 32MB 硬上限约束; + // Linux 环境无单条消息预算校验的必要(结果经同一 aggregate 汇聚),仅防 OOM + val binarySizeBytes = fs.getFileSize(path) + if (binarySizeBytes > ToolExecutionLimits.MAX_FILE_READ_SIZE_BYTES) { + val sizeMb = binarySizeBytes / (1024.0 * 1024.0) + return ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "File exceeds the 32 MB read limit (actual %.1f MB): %s. Operate on it by path instead.".format(sizeMb, path) + ) + } + val bytes = fs.readFileBytes(path) if (bytes == null) { return ToolResult( @@ -652,6 +679,23 @@ class LinuxFileSystemTools(context: Context) : StandardFileSystemTools(context) val recursive = tool.parameters.find { it.name == "recursive" }?.value?.toBoolean() ?: false PathValidator.validateLinuxPath(path, tool.name)?.let { return it } + // 修改意图:与 Android 环境一致,递归删除危险根目录(proot 根文件系统的 / 等) + // 由硬底线拦截,防止整棵 rootfs 被清空 + if (recursive && WorkspacePathChecker.isDangerousRecursiveDeleteTarget(path)) { + return ToolResult( + toolName = tool.name, + success = false, + result = FileOperationData( + operation = "delete", + env = "linux", + path = path, + successful = false, + details = "Refused to recursively delete a protected root directory: $path" + ), + error = "Refused to recursively delete a protected root directory: $path" + ) + } + if (path.isBlank()) { return ToolResult( toolName = tool.name, @@ -1173,15 +1217,55 @@ class LinuxFileSystemTools(context: Context) : StandardFileSystemTools(context) ) } - return grepCodeWithNativeRipgrep( - toolName = tool.name, - path = path, - pattern = pattern, - filePattern = filePattern, - caseInsensitive = caseInsensitive, - contextLines = contextLines, - maxResults = maxResults, - envLabel = "linux" + // 修改意图:native ripgrep 运行在宿主文件系统上,直接传 linux/proot 逻辑路径 + // (/home/... 等)会扫不到目标;先映射到宿主 rootfs 真实路径再扫描, + // 结果路径再反向映射回 linux 逻辑路径回传给模型。 + // 评审 P1:映射含 containment 校验,.. 逃出 rootfs 的路径(含 /../..)返回 null, + // 这里必须显式拒绝,不得回落原始路径交给 ripgrep。 + val mappedPath = + PathMapper.resolvePath(context, path, "linux") + ?: return ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "Path escapes the linux root filesystem and was rejected: path=\"$path\". " + + "Use a path inside the linux environment root." + ) + val grepResult = + grepCodeWithNativeRipgrep( + toolName = tool.name, + path = mappedPath, + pattern = pattern, + filePattern = filePattern, + caseInsensitive = caseInsensitive, + contextLines = contextLines, + maxResults = maxResults, + envLabel = "linux" + ) + + val resultData = grepResult.result as? GrepResultData + if (!grepResult.success || resultData == null) { + return grepResult + } + + // 评审 P3:反向映射失败说明该结果在 rootfs 之外(宿主内部真实路径),把宿主路径 + // 回传给模型既泄露内部结构、模型也无法把它当 linux 路径复用——丢弃该条目并记日志 + val remappedMatches = + resultData.matches.mapNotNull { fileMatch -> + val linuxFilePath = PathMapper.mapAndroidPathToLinux(context, fileMatch.filePath) + if (linuxFilePath != null) { + fileMatch.copy(filePath = linuxFilePath) + } else { + AppLogger.w( + TAG, + "grep result outside rootfs dropped: ${fileMatch.filePath}" + ) + null + } + } + return grepResult.copy( + result = + resultData.copy(searchPath = path, matches = remappedMatches) ) } diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/SafFileSystemTools.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/SafFileSystemTools.kt index 5531e667f0..045c21a61a 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/SafFileSystemTools.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/SafFileSystemTools.kt @@ -144,6 +144,16 @@ class SafFileSystemTools( return null } + // 修改意图:SAF 创建文件的白名单。此前白名单外的扩展名抛出的错误不列出任何支持项, + // 模型无从自纠;抽成常量供错误信息直接引用 + private val SAF_SUPPORTED_CREATE_EXTENSIONS: List = + listOf( + "md", "markdown", "txt", "log", "json", "xml", "html", "htm", "css", "csv", + "js", "mjs", "cjs", "ts", "tsx", "kt", "kts", "java", "py", "sh", "bash", + "yaml", "yml", "toml", "ini", "conf", "properties", "svg", "png", "jpg", + "jpeg", "webp", "gif", "pdf", "zip", "gz", "apk", + ) + private fun requireSafCreateMimeType(fileName: String): String { val normalizedName = fileName.lowercase(Locale.ROOT) val extension = fileName.substringAfterLast('.', "").lowercase(Locale.ROOT) @@ -176,7 +186,13 @@ class SafFileSystemTools( extension == "zip" -> "application/zip" extension == "gz" -> "application/gzip" extension == "apk" -> "application/vnd.android.package-archive" - else -> throw IllegalArgumentException("Unsupported file extension for SAF create: $fileName") + else -> + throw IllegalArgumentException( + "Unsupported file extension for SAF create: $fileName. " + + "Supported extensions: ${SAF_SUPPORTED_CREATE_EXTENSIONS.joinToString(", ")}, " + + "plus well-known files (README, LICENSE, .gitignore, ...). " + + "For other file types, operate outside the SAF environment." + ) } } @@ -1538,6 +1554,14 @@ class SafFileSystemTools( return withContext(Dispatchers.IO) { try { + // 修改意图:与 Android/Linux 环境一致,read_file_full 整文件读取受 32MB 硬上限 + // 约束(SAF 通过 ContentResolver 查询大小,超限拒绝并引导 read_file_part) + querySize(uri)?.let { sizeBytes -> + if (sizeBytes > ToolExecutionLimits.MAX_FILE_READ_SIZE_BYTES) { + val sizeMb = sizeBytes / (1024.0 * 1024.0) + return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "File exceeds the 32 MB read limit (actual %.1f MB): %s. Use read_file_part with start_line/end_line to read it in segments.".format(sizeMb, path)) + } + } if (textOnly) { val sample = readUpToBytes(uri, 512).first if (!FileUtils.isTextLike(sample)) { @@ -1548,7 +1572,7 @@ class SafFileSystemTools( ?: return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "Failed to open uri: $path") val bytes = input.use { it.readBytes() } if (bytes.isNotEmpty() && !FileUtils.isTextLike(bytes.take(1024).toByteArray())) { - return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "File does not appear to be a text file. Use specialized tools for binary files.") + return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "File does not appear to be a text file. Use read_file_binary for binary files (returns base64).") } val content = bytes.toString(Charsets.UTF_8) ToolResult(toolName = tool.name, success = true, result = FileContentData(path = path, content = content, size = querySize(uri) ?: bytes.size.toLong(), env = envLabel), error = "") @@ -1571,7 +1595,8 @@ class SafFileSystemTools( try { val (bytes, truncated) = readUpToBytes(uri, maxFileSizeBytes) if (bytes.isNotEmpty() && !FileUtils.isTextLike(bytes.take(512).toByteArray())) { - return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "File does not appear to be a text file. Use readFileFull tool for special file types.") + // 修改意图:与 Android 环境一致,报错指向真实存在的工具 + return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "File does not appear to be a text file. Use read_file_binary for binary files (returns base64), or read_file for documents/images (auto-extracts text).") } val content = bytes.toString(Charsets.UTF_8) var contentWithLineNumbers = addLineNumbers(content) @@ -1660,6 +1685,13 @@ class SafFileSystemTools( return withContext(Dispatchers.IO) { try { + // 修改意图:与 Android/Linux 环境一致,read_file_binary 整文件读取受 32MB 硬上限约束 + querySize(uri)?.let { sizeBytes -> + if (sizeBytes > ToolExecutionLimits.MAX_FILE_READ_SIZE_BYTES) { + val sizeMb = sizeBytes / (1024.0 * 1024.0) + return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "File exceeds the 32 MB read limit (actual %.1f MB): %s. Operate on it by path instead.".format(sizeMb, path)) + } + } val input = contentResolver.openInputStream(uri) ?: return@withContext ToolResult(toolName = tool.name, success = false, result = StringResultData(""), error = "Failed to open uri: $path") val bytes = input.use { it.readBytes() } diff --git a/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/StandardFileSystemTools.kt b/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/StandardFileSystemTools.kt index d4d11f63d4..4133798283 100644 --- a/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/StandardFileSystemTools.kt +++ b/app/src/main/java/com/ai/assistance/operit/core/tools/defaultTool/standard/StandardFileSystemTools.kt @@ -11,6 +11,7 @@ import com.ai.assistance.operit.core.tools.FileApplyResultData import com.ai.assistance.operit.core.tools.BinaryFileContentData import com.ai.assistance.operit.core.tools.FileExistsData import com.ai.assistance.operit.core.tools.FileInfoData +import com.ai.assistance.operit.core.tools.WorkspacePathChecker import com.ai.assistance.operit.core.tools.FileOperationData import com.ai.assistance.operit.core.tools.FilePartContentData import com.ai.assistance.operit.core.tools.FindFilesResultData @@ -79,7 +80,9 @@ open class StandardFileSystemTools(protected val context: Context) { companion object { protected const val TAG = "FileSystemTools" // 特殊文件类型扩展名列表(需要特殊处理提取文本的文件类型) - protected val SPECIAL_FILE_EXTENSIONS = listOf( + // 修改意图:可见性从 protected 放宽为 public——AI 读取提炼拦截层(FileDistillService) + // 需要用同一份清单判断"特殊类型抽取管线一律透传",复用而非复制清单避免两处漂移 + val SPECIAL_FILE_EXTENSIONS = listOf( "doc", "docx", // Word documents "pdf", // PDF documents "jpg", "jpeg", // Image files @@ -87,6 +90,10 @@ open class StandardFileSystemTools(protected val context: Context) { "mp3", "wav", "m4a", "aac", "flac", "ogg", "opus", "mp4", "mkv", "mov", "webm", "avi", "m4v" ) + + /** 特殊类型(图片/PDF/文档/音视频)扩展名判定;命中者走抽取管线,不进 AI 提炼 */ + fun isSpecialFileExtension(extension: String): Boolean = + extension.lowercase() in SPECIAL_FILE_EXTENSIONS } // ApiPreferences 实例,用于动态获取配置 @@ -793,8 +800,11 @@ open class StandardFileSystemTools(protected val context: Context) { protected fun addLineNumbers(content: String, startLine: Int, totalLines: Int): String { val lines = content.lines() if (lines.isEmpty()) return "" - val maxDigits = - if (totalLines > 0) totalLines.toString().length else lines.size.toString().length + // 修改意图:行号列宽按"实际展示的最大行号"计算。readFilePart 流式化后 totalLines + // 可能为 -1(未读到文件尾、总数未知),此时以本段展示的最大行号定宽,保证分段间对齐 + val maxDisplayedNumber = + maxOf(startLine + lines.size, if (totalLines > 0) totalLines else 0) + val maxDigits = maxDisplayedNumber.toString().length return lines.mapIndexed { index, line -> "${(startLine + index + 1).toString().padStart(maxDigits, ' ')}| $line" }.joinToString("\n") @@ -805,35 +815,98 @@ open class StandardFileSystemTools(protected val context: Context) { return fileExtension.lowercase() in SPECIAL_FILE_EXTENSIONS } - /** 统计文件总行数 */ - protected fun countFileLines(file: File): Int { - var totalLines = 0 - file.bufferedReader().use { reader -> - while (reader.readLine() != null) { - totalLines++ - } + /** + * 修改意图:整文件读取类工具(read_file / read_file_full / read_file_binary 及特殊文件 + * 抽取路径)的 32MB 硬上限。此前 read_file_full/read_file_binary 对文件大小无任何限制, + * 大文件 readText/readBytes 直接 OOM 且报错为 "error: null";统一在此拦截并引导模型 + * 改用 read_file_part 按行分段读取。 + */ + protected fun checkFileSizeLimit(file: File, toolName: String): ToolResult? { + val sizeBytes = file.length() + if (sizeBytes > ToolExecutionLimits.MAX_FILE_READ_SIZE_BYTES) { + val sizeMb = sizeBytes / (1024.0 * 1024.0) + return ToolResult( + toolName = toolName, + success = false, + result = StringResultData(""), + error = "File exceeds the 32 MB read limit (actual %.1f MB): %s. Use read_file_part with start_line/end_line to read it in segments.".format(sizeMb, file.path) + ) } - return totalLines + return null } - /** 从文件中读取指定范围的行 */ - private fun readLinesFromFile(file: File, startLine: Int, endLine: Int): String { - val partContent = StringBuilder() - var currentLine = 0 - file.bufferedReader().useLines { lines -> - lines.forEach { line -> - if (currentLine >= endLine) return@useLines - if (currentLine >= startLine) { - partContent.append(line).append('\n') - } - currentLine++ + /** readLinesStreaming 的结果载体 */ + private class StreamedLines( + val content: String, + /** 实际读到的最后一行(1-based);一行都没读到时为 0 */ + val lastLineRead: Int, + /** start_line 超出文件末尾 */ + val startBeyondEof: Boolean, + /** 命中文件尾时的实际总行数;因行数/字符预算提前停止时为 -1(总数未知) */ + val totalLinesIfKnown: Int, + ) + + /** + * 修改意图:readFilePart 的分段读取改为单次流式扫描——跳过 startIndex 之前的行后顺序 + * 收集到 requestedCount/字符预算/文件尾为止。旧实现先 countFileLines 全量扫描再 + * readLinesFromFile 二次扫描,大文件双倍 IO;流式版本在 end_line 缺省时无需预知总行数, + * 真正的超大文件也能低开销分段读取。 + */ + private fun readLinesStreaming( + file: File, + startIndex: Int, + requestedCount: Int, + charBudget: Int, + ): StreamedLines { + val sb = StringBuilder() + var collected = 0 + var index = 0 + var result: StreamedLines? = null + file.bufferedReader().useLines { sequence -> + val iterator = sequence.iterator() + while (index < startIndex && iterator.hasNext()) { + iterator.next() + index++ } + if (index < startIndex && !iterator.hasNext()) { + // 起始行超出文件末尾:index 即文件实际总行数(0-based 计数等于行数) + result = StreamedLines( + content = "", + lastLineRead = 0, + startBeyondEof = true, + totalLinesIfKnown = index, + ) + return@useLines + } + while (iterator.hasNext() && + collected < requestedCount && + sb.length <= charBudget) { + val line = iterator.next() + if (collected > 0) sb.append('\n') + sb.append(line) + collected++ + } + val lastLineRead = startIndex + collected + result = + if (iterator.hasNext()) { + // 因 requestedCount 或字符预算提前停止,文件还有后续内容,总行数未知 + StreamedLines( + content = sb.toString(), + lastLineRead = lastLineRead, + startBeyondEof = false, + totalLinesIfKnown = -1, + ) + } else { + // 已到文件尾:lastLineRead 即实际总行数 + StreamedLines( + content = sb.toString(), + lastLineRead = lastLineRead, + startBeyondEof = false, + totalLinesIfKnown = lastLineRead, + ) + } } - // Remove last newline if content is not empty - if (partContent.isNotEmpty()) { - partContent.setLength(partContent.length - 1) - } - return partContent.toString() + return result ?: StreamedLines("", 0, true, 0) } /** List files in a directory */ @@ -1170,37 +1243,23 @@ open class StandardFileSystemTools(protected val context: Context) { TAG, "Failed to decode image file, returning error message" ) + // 修改意图:解码失败从"错误文本当内容 + success=true"改为显式失败, + // 让模型能从结果判断提取确实失败而非"图片没有文字" ToolResult( toolName = tool.name, - success = true, - result = - FileContentData( - path = path, - content = - "Failed to decode image file.", - size = - "Failed to decode image file." - .length - .toLong() - ), - error = "" + success = false, + result = StringResultData(""), + error = "Failed to decode image file. The file may be corrupted or not a real image." ) } } catch (e: Exception) { AppLogger.e(TAG, "Error during OCR text extraction", e) + // 修改意图:OCR 异常从"错误文本当内容 + success=true"改为显式失败 ToolResult( toolName = tool.name, - success = true, - result = - FileContentData( - path = path, - content = - "Error extracting text from image: ${e.message}", - size = - "Error extracting text from image: ${e.message}" - .length.toLong() - ), - error = "" + success = false, + result = StringResultData(""), + error = "Error extracting text from image: ${e.message}" ) } } @@ -1432,8 +1491,12 @@ open class StandardFileSystemTools(protected val context: Context) { ) } + // 修改意图:read_file_full 此前对文件大小无限制,大文件 readText 会 OOM; + // 统一加 32MB 硬上限并引导改用 read_file_part 分段读取 + checkFileSizeLimit(file, tool.name)?.let { return it } + val fileExt = file.extension.lowercase() - + // 如果启用了 text_only 模式,检查文件是否为文本 if (textOnly) { // 读取文件前 512 字节进行判断 @@ -1474,7 +1537,7 @@ open class StandardFileSystemTools(protected val context: Context) { toolName = tool.name, success = false, result = StringResultData(""), - error = "File does not appear to be a text file. Use specialized tools for binary files." + error = "File does not appear to be a text file. Use read_file_binary for binary files (returns base64)." ) } } catch (e: Exception) { @@ -1524,9 +1587,26 @@ open class StandardFileSystemTools(protected val context: Context) { ) } + // 修改意图:read_file_binary 此前无大小限制,readBytes 无界会 OOM; + // 加 32MB 硬上限并引导改用 read_file_part 或按路径操作 + checkFileSizeLimit(file, tool.name)?.let { return@withContext it } + val bytes = file.readBytes() val base64 = android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP) + // 修改意图:base64 只有进入模型可见的最终消息才有意义(此前 toString 只回摘要, + // 内容永远到不了模型)。单条工具结果消息上限约 64k 字符,超出预算的 base64 + // 即便放进结果也会被静默截断成无效数据,因此显式拒绝并给出替代路径 + val base64Budget = ToolExecutionLimits.MAX_SINGLE_TOOL_RESULT_MESSAGE_CHARS - 2_000 + if (base64.length > base64Budget) { + return@withContext ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "Binary file is too large to inline as base64 (%d bytes, base64 length %d > budget %d). Operate on it by path instead (copy_file / write_file_binary with base64 you already have), or process it inside a terminal session.".format(bytes.size, base64.length, base64Budget) + ) + } + ToolResult( toolName = tool.name, success = true, @@ -1587,6 +1667,10 @@ open class StandardFileSystemTools(protected val context: Context) { ) } + // 修改意图:read_file 的特殊文件抽取路径(OCR/PDF 等)会整文件加载, + // 与其它整文件读取工具一样受 32MB 硬上限约束并引导分段读取 + checkFileSizeLimit(file, tool.name)?.let { return it } + val fileExt = file.extension.lowercase() // For special types, full read then truncate text is the only way. @@ -1621,24 +1705,30 @@ open class StandardFileSystemTools(protected val context: Context) { // For text-based files, read only the beginning. // Check if file is text-like by analyzing content if (!FileUtils.isTextLike(file)) { + // 修改意图:原报错指向不存在的 "readFileFull" 工具名,且对二进制文件给出错误补救; + // 指向真实存在的 read_file_binary(二进制)与 read_file(文档/图片自动提取文本) return ToolResult( toolName = tool.name, success = false, result = StringResultData(""), error = - "File does not appear to be a text file. Use readFileFull tool for special file types." + "File does not appear to be a text file. Use read_file_binary for binary files (returns base64), or read_file for documents/images (auto-extracts text)." ) } - val content = + val readOutcome = file.bufferedReader().use { val buffer = CharArray(maxFileSizeBytes) val charsRead = it.read(buffer, 0, maxFileSizeBytes) - if (charsRead > 0) String(buffer, 0, charsRead) else "" + // 修改意图:截断判定改为"缓冲区读满且还读得到下一个字符"。旧判定 + // file.length() > maxFileSizeBytes 拿字节数与字符预算比较, + // 多字节编码(中文等)下完整读出的文件会被误标为已截断 + val truncated = charsRead == maxFileSizeBytes && it.read() != -1 + Pair(if (charsRead > 0) String(buffer, 0, charsRead) else "", truncated) } - val truncated = file.length() > maxFileSizeBytes - var finalContent = addLineNumbers(content) + val (rawContent, truncated) = readOutcome + var finalContent = addLineNumbers(rawContent) if (truncated) { finalContent += "\n\n... (file content truncated) ..." } @@ -1672,6 +1762,21 @@ open class StandardFileSystemTools(protected val context: Context) { val startLineParam = tool.parameters.find { it.name == "start_line" }?.value?.toIntOrNull() ?: 1 val endLineParam = tool.parameters.find { it.name == "end_line" }?.value?.toIntOrNull() + // 评审 P2:显式传入 end_line < start_line 属于非法范围。旧实现会把 end_line 钳到 + // 有效范围返回实际内容,流式实现则 coerceAtLeast(0) 后静默返回空成功——模型传错 + // 范围都得不到纠错信号。在 android/linux/saf 三分支委托前统一显式失败。 + if (endLineParam != null && endLineParam < startLineParam) { + return ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "Invalid range: end_line (%d) must be >= start_line (%d). Re-check the line numbers with read_file_part or read_file first.".format( + endLineParam, + startLineParam + ) + ) + } + // 如果是Linux环境,委托给LinuxFileSystemTools if (isLinuxEnvironment(environment)) { return linuxTools.readFilePart(tool) @@ -1705,59 +1810,111 @@ open class StandardFileSystemTools(protected val context: Context) { ) } - // 1. 准备数据源 & 获取总行数 - // 如果是特殊文件,先提取所有文本到内存;如果是普通文件,只统计行数 - val inMemoryLines: List? - val totalLines: Int - + // 1. 准备数据源 if (isSpecialFileType(file.extension)) { + // 特殊文件:整文件提取到内存后按行切片(抽取路径已受 32MB 上限约束) val specialResult = handleSpecialFileRead(tool, path, file.extension.lowercase()) if (specialResult != null && !specialResult.success) return@withContext specialResult - if (specialResult != null) { - val content = (specialResult.result as FileContentData).content - inMemoryLines = content.lines() - totalLines = inMemoryLines.size - } else { - // Fallback if handleSpecialFileRead returns null (shouldn't happen given check) - inMemoryLines = null - totalLines = 0 + if (specialResult == null) { + // 修改意图:特殊类型但抽取器未处理时明确失败, + // 不允许静默落穿到纯文本流式路径读出乱码 + return@withContext ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "Special file type '.${file.extension}' could not be extracted. Try read_file or read_file_full." + ) } - } else { - inMemoryLines = null - totalLines = countFileLines(file) + + val inMemoryLines = (specialResult.result as FileContentData).content.lines() + val totalLines = inMemoryLines.size + val startLine = maxOf(1, startLineParam).coerceIn(1, maxOf(1, totalLines)) + val endLine = + (endLineParam + ?: (startLine + ToolExecutionLimits.DEFAULT_FILE_READ_PART_LINES - 1)) + .coerceIn(startLine, maxOf(1, totalLines)) + val startIndex = startLine - 1 + + val partContent = + if (totalLines > 0 && startIndex < totalLines) { + inMemoryLines.subList(startIndex, minOf(endLine, totalLines)).joinToString("\n") + } else "" + + var truncatedPartContent = partContent + val isTruncated = truncatedPartContent.length > maxFileSizeBytes + if (isTruncated) { + truncatedPartContent = truncatedPartContent.substring(0, maxFileSizeBytes) + } + + var contentWithLineNumbers = + addLineNumbers(truncatedPartContent, startIndex, totalLines) + if (isTruncated) { + contentWithLineNumbers += "\n\n... (file content truncated) ..." + } + + return@withContext ToolResult( + toolName = tool.name, + success = true, + result = FilePartContentData( + path = path, + content = contentWithLineNumbers, + partIndex = 0, // 保留兼容性,但不再使用 + totalParts = 1, // 保留兼容性,但不再使用 + startLine = startIndex, + endLine = minOf(endLine, totalLines), + totalLines = totalLines + ), + error = "" + ) + } + + // 修改意图:普通文件补上 isTextLike 门禁。旧实现不做文本判断, + // 读 .so/图片等二进制会返回 U+FFFD 乱码却报 success=true + if (!FileUtils.isTextLike(file)) { + return@withContext ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "File does not appear to be a text file. Use read_file_binary for binary files (returns base64)." + ) } - // 2. 计算实际的行号范围(行号从1开始,转换为0-based索引) - val startLine = maxOf(1, startLineParam).coerceIn(1, maxOf(1, totalLines)) - val endLine = - (endLineParam - ?: (startLine + ToolExecutionLimits.DEFAULT_FILE_READ_PART_LINES - 1)) - .coerceIn(startLine, maxOf(1, totalLines)) - - // 转换为0-based索引 + // 2. 普通文件:单次流式扫描,不再预统计总行数(旧实现 countFileLines 全量 + // 扫描 + readLinesFromFile 二次扫描,大文件双倍 IO);end_line 缺省时 + // 读到"起始行 + 200 行"或字符预算为止,不需要知道文件总长 + val startLine = maxOf(1, startLineParam) val startIndex = startLine - 1 - val endIndex = endLine // endLine 本身就是最后一行的1-based行号,转成exclusive的end需要不减1 - - // 3. 获取分段内容 - val partContent = if (inMemoryLines != null) { - // 内存模式:直接切片 - if (totalLines > 0 && startIndex < totalLines) { - inMemoryLines.subList(startIndex, minOf(endIndex, totalLines)).joinToString("\n") - } else "" - } else { - // 磁盘流模式:读取指定行 - if (totalLines > 0) readLinesFromFile(file, startIndex, endIndex) else "" + val requestedCount = + (endLineParam ?: (startLine + ToolExecutionLimits.DEFAULT_FILE_READ_PART_LINES - 1)) - + startLine + 1 + // end_line 缺省窗口为 200 行;显式传入时按传入行数收集 + .coerceAtLeast(0) + + val streamed = + readLinesStreaming(file, startIndex, requestedCount, maxFileSizeBytes) + + if (streamed.startBeyondEof) { + // 修改意图:start_line 超界从"静默返回空内容"改为明确报错并告知总行数, + // 模型可以据此把 start_line 拉回有效范围 + return@withContext ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "start_line %d is beyond the end of the file (total %d lines). Use a smaller start_line.".format(startLine, streamed.totalLinesIfKnown) + ) } - // 4. 封装返回结果 - var truncatedPartContent = partContent + var truncatedPartContent = streamed.content val isTruncated = truncatedPartContent.length > maxFileSizeBytes if (isTruncated) { truncatedPartContent = truncatedPartContent.substring(0, maxFileSizeBytes) } - var contentWithLineNumbers = addLineNumbers(truncatedPartContent, startIndex, totalLines) + // totalLinesIfKnown 为 -1 表示因行数/字符预算提前停止、总行数未知; + // addLineNumbers 会按本段展示的最大行号定宽 + var contentWithLineNumbers = + addLineNumbers(truncatedPartContent, startIndex, streamed.totalLinesIfKnown) if (isTruncated) { contentWithLineNumbers += "\n\n... (file content truncated) ..." } @@ -1771,8 +1928,8 @@ open class StandardFileSystemTools(protected val context: Context) { partIndex = 0, // 保留兼容性,但不再使用 totalParts = 1, // 保留兼容性,但不再使用 startLine = startIndex, - endLine = minOf(endIndex, totalLines), - totalLines = totalLines + endLine = streamed.lastLineRead, + totalLines = streamed.totalLinesIfKnown ), error = "" ) @@ -2058,6 +2215,26 @@ open class StandardFileSystemTools(protected val context: Context) { } PathValidator.validateAndroidPath(path, tool.name)?.let { return it } + // 修改意图:递归删除危险根目录的硬底线。此前 recursive=true 对任意绝对路径 + // (包括 / 与 /storage/emulated/0)直接 deleteRecursively,配合存储权限会造成 + // 大规模数据破坏;此处与环境无关地拦截(linux 环境由 LinuxFileSystemTools 同规则拦截) + if (recursive && WorkspacePathChecker.isDangerousRecursiveDeleteTarget(path)) { + return ToolResult( + toolName = tool.name, + success = false, + result = + FileOperationData( + operation = "delete", + path = path, + successful = false, + details = + "Refused to recursively delete a protected root directory: $path" + ), + error = + "Refused to recursively delete a protected root directory: $path" + ) + } + if (path.isBlank()) { return ToolResult( toolName = tool.name, @@ -2737,8 +2914,10 @@ open class StandardFileSystemTools(protected val context: Context) { val sourceEnvironment = tool.parameters.find { it.name == "source_environment" }?.value val destEnvironment = tool.parameters.find { it.name == "dest_environment" }?.value val environment = tool.parameters.find { it.name == "environment" }?.value + // 修改意图:recursive 缺省从 true 改为 false,与文档(默认 false)和扩展包 JS 对齐; + // 目录复制漏传参数时明确失败并提示,避免"以为只复制一个文件却复制了整棵树" val recursive = - tool.parameters.find { it.name == "recursive" }?.value?.toBoolean() ?: true + tool.parameters.find { it.name == "recursive" }?.value?.toBoolean() ?: false if (sourcePath.isBlank() || destPath.isBlank()) { return ToolResult( @@ -2880,10 +3059,10 @@ open class StandardFileSystemTools(protected val context: Context) { path = sourcePath, successful = false, details = - "Cannot copy directory without recursive flag" + "Cannot copy a directory without recursive=true. Pass recursive=true to copy the whole directory tree." ), error = - "Cannot copy directory without recursive flag" + "Cannot copy a directory without recursive=true. Pass recursive=true to copy the whole directory tree." ) } } else { @@ -3493,6 +3672,19 @@ open class StandardFileSystemTools(protected val context: Context) { } /** Zip files or directories */ + /** + * 评审 P1:linux 环境路径逃出 proot rootfs(PathMapper.resolvePath 返回 null)时 + * 的统一拒绝结果。不得回落原路径继续执行——那会把宿主文件系统暴露给工具。 + */ + private fun rootfsEscapeResult(tool: AITool, path: String, paramName: String): ToolResult = + ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "Path escapes the linux root filesystem and was rejected: $paramName=\"$path\". " + + "Use a path inside the linux environment root." + ) + open suspend fun zipFiles(tool: AITool): ToolResult { val sourcePath = tool.parameters.find { it.name == "source" }?.value ?: "" val zipPath = tool.parameters.find { it.name == "destination" }?.value ?: "" @@ -3510,11 +3702,24 @@ open class StandardFileSystemTools(protected val context: Context) { error = "include_root_directory must be true or false" ) } + // 修改意图:repo: 环境不支持 zip(旧实现静默落到 Android FS 报"不存在"), + // 按 environment 矩阵给明确拒绝,模型可据此改用 android/linux 环境 + if (isSafEnvironment(environment)) { + return ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "zip_files is not supported for SAF/repo environment. Use android or linux environment." + ) + } PathValidator.validateAndroidPath(sourcePath, tool.name, "source")?.let { return it } PathValidator.validateAndroidPath(zipPath, tool.name, "destination")?.let { return it } + // 评审 P1:linux 环境映射后必须校验 containment,.. 逃出 rootfs 的路径一律拒绝 val actualSourcePath = PathMapper.resolvePath(context, sourcePath, environment) + ?: return rootfsEscapeResult(tool, sourcePath, "source") val actualZipPath = PathMapper.resolvePath(context, zipPath, environment) + ?: return rootfsEscapeResult(tool, zipPath, "destination") if (sourcePath.isBlank() || zipPath.isBlank()) { return ToolResult( @@ -3667,11 +3872,23 @@ open class StandardFileSystemTools(protected val context: Context) { val zipPath = tool.parameters.find { it.name == "source" }?.value ?: "" val destPath = tool.parameters.find { it.name == "destination" }?.value ?: "" val environment = tool.parameters.find { it.name == "environment" }?.value + // 修改意图:repo: 环境不支持 unzip,按 environment 矩阵给明确拒绝 + if (isSafEnvironment(environment)) { + return ToolResult( + toolName = tool.name, + success = false, + result = StringResultData(""), + error = "unzip_files is not supported for SAF/repo environment. Use android or linux environment." + ) + } PathValidator.validateAndroidPath(zipPath, tool.name, "source")?.let { return it } PathValidator.validateAndroidPath(destPath, tool.name, "destination")?.let { return it } + // 评审 P1:linux 环境映射后必须校验 containment,.. 逃出 rootfs 的路径一律拒绝 val actualZipPath = PathMapper.resolvePath(context, zipPath, environment) + ?: return rootfsEscapeResult(tool, zipPath, "source") val actualDestPath = PathMapper.resolvePath(context, destPath, environment) + ?: return rootfsEscapeResult(tool, destPath, "destination") if (zipPath.isBlank() || destPath.isBlank()) { return ToolResult( @@ -3830,6 +4047,8 @@ open class StandardFileSystemTools(protected val context: Context) { val environment = tool.parameters.find { it.name == "environment" }?.value val oldContent = tool.parameters.find { it.name == "old" }?.value ?: "" val newContent = tool.parameters.find { it.name == "new" }?.value ?: "" + // 修改意图:edit_file 同样支持 occurrence(多候选定位),透传给 apply_file + val occurrenceParam = tool.parameters.find { it.name == "occurrence" }?.value val applyResult = applyFile( AITool( @@ -3839,6 +4058,7 @@ open class StandardFileSystemTools(protected val context: Context) { ToolParameter("type", "replace"), ToolParameter("old", oldContent), ToolParameter("new", newContent), + ToolParameter("occurrence", occurrenceParam ?: ""), environment?.takeIf { it.isNotBlank() }?.let { ToolParameter("environment", it) } ) ) @@ -3860,11 +4080,43 @@ open class StandardFileSystemTools(protected val context: Context) { val typeParam = tool.parameters.find { it.name == "type" }?.value val oldParam = tool.parameters.find { it.name == "old" }?.value val newParam = tool.parameters.find { it.name == "new" }?.value + // 修改意图:occurrence 用于 old 命中多处时按第 N 处定位;未传时保持 null, + // 多候选一律拒绝并列出候选行号,绝不静默选第一处 + val occurrenceParam = + tool.parameters.find { it.name == "occurrence" }?.value?.trim()?.toIntOrNull() ToolProgressBus.update(tool.name, 0f, "Preparing...") try { - PathValidator.validateAndroidPath(path, tool.name)?.let { - emit(it) + // 修改意图:路径校验按环境路由——linux 支持 ~ 前缀(旧实现统一走 + // validateAndroidPath,linux 的 ~/x 路径在 apply 上必失败而 write_file 成功); + // repo: 环境不支持结构化编辑,按 environment 矩阵给明确拒绝 + if (isLinuxEnvironment(environment)) { + PathValidator.validateLinuxPath(path, tool.name)?.let { + emit(it) + return@flow + } + } else if (isSafEnvironment(environment)) { + emit( + ToolResult( + toolName = tool.name, + success = false, + result = + FileOperationData( + operation = "apply", + path = path, + successful = false, + details = + "apply_file is not supported for SAF/repo environment. Use android or linux environment." + ), + error = + "apply_file is not supported for SAF/repo environment. Use android or linux environment." + ) + ) return@flow + } else { + PathValidator.validateAndroidPath(path, tool.name)?.let { + emit(it) + return@flow + } } if (path.isBlank()) { @@ -4096,7 +4348,8 @@ open class StandardFileSystemTools(protected val context: Context) { FileBindingService.StructuredEditOperation( action = FileBindingService.StructuredEditAction.REPLACE, oldContent = oldContent, - newContent = newContent + newContent = newContent, + occurrence = occurrenceParam ) ) } @@ -4123,7 +4376,8 @@ open class StandardFileSystemTools(protected val context: Context) { listOf( FileBindingService.StructuredEditOperation( action = FileBindingService.StructuredEditAction.DELETE, - oldContent = oldContent + oldContent = oldContent, + occurrence = occurrenceParam ) ) } @@ -4159,8 +4413,11 @@ open class StandardFileSystemTools(protected val context: Context) { val mapped = (0.2f + 0.6f * p).coerceIn(0f, 0.99f) ToolProgressBus.update(tool.name, mapped, msg) } - val mergedContent = bindingResult.first - val aiInstructions = bindingResult.second + // 修改意图:bindingResult 已携带各 op 的匹配行号(appliedRanges), + // 成功消息中回传给模型,便于其核对编辑落点 + val mergedContent = bindingResult.mergedContent + val aiInstructions = bindingResult.diff + val appliedRanges = bindingResult.appliedRanges if (aiInstructions.startsWith("Error", ignoreCase = true)) { AppLogger.e(TAG, "File binding failed: $aiInstructions") @@ -4223,7 +4480,9 @@ open class StandardFileSystemTools(protected val context: Context) { env = environment.orEmpty().trim().ifBlank { "android" }, path = path, successful = true, - details = "Successfully applied AI code to file: $path" + details = + "Successfully applied AI code to file: $path" + + (if (appliedRanges.isNotEmpty()) " Applied at: ${appliedRanges.joinToString("; ")}" else "") ) ToolProgressBus.update(tool.name, 0.92f, "Generating diff...") @@ -4239,7 +4498,8 @@ open class StandardFileSystemTools(protected val context: Context) { FileApplyResultData( operation = operationData, aiDiffInstructions = aiInstructions, - diffContent = diffContent + diffContent = diffContent, + matchedRanges = appliedRanges ), error = "" ) @@ -4280,7 +4540,9 @@ open class StandardFileSystemTools(protected val context: Context) { val environment = tool.parameters.find { it.name == "environment" }?.value PathValidator.validateAndroidPath(destPath, tool.name, "destination")?.let { return it } + // 评审 P1:linux 环境映射后必须校验 containment,.. 逃出 rootfs 的路径一律拒绝 val actualDestPath = PathMapper.resolvePath(context, destPath, environment) + ?: return rootfsEscapeResult(tool, destPath, "destination") fun parseHeaders(headersJson: String?): Map { if (headersJson.isNullOrBlank()) return emptyMap() @@ -4492,6 +4754,21 @@ open class StandardFileSystemTools(protected val context: Context) { if (isLinuxEnvironment(environment)) { return linuxTools.openFile(tool) } + // 修改意图:repo: 环境不支持 open(旧实现静默落 Android FS 报"不存在"),给明确拒绝 + if (isSafEnvironment(environment)) { + return ToolResult( + toolName = tool.name, + success = false, + result = + FileOperationData( + operation = "open", + path = path, + successful = false, + details = "open_file is not supported for SAF/repo environment." + ), + error = "open_file is not supported for SAF/repo environment." + ) + } PathValidator.validateAndroidPath(path, tool.name)?.let { return it } if (path.isBlank()) { @@ -4756,6 +5033,21 @@ open class StandardFileSystemTools(protected val context: Context) { if (isLinuxEnvironment(environment)) { return linuxTools.shareFile(tool) } + // 修改意图:repo: 环境不支持 share(旧实现静默落 Android FS 报"不存在"),给明确拒绝 + if (isSafEnvironment(environment)) { + return ToolResult( + toolName = tool.name, + success = false, + result = + FileOperationData( + operation = "share", + path = path, + successful = false, + details = "share_file is not supported for SAF/repo environment." + ), + error = "share_file is not supported for SAF/repo environment." + ) + } PathValidator.validateAndroidPath(path, tool.name)?.let { return it } if (path.isBlank()) { diff --git a/app/src/main/java/com/ai/assistance/operit/data/model/FunctionType.kt b/app/src/main/java/com/ai/assistance/operit/data/model/FunctionType.kt index 89f71e2349..3a29e1c319 100644 --- a/app/src/main/java/com/ai/assistance/operit/data/model/FunctionType.kt +++ b/app/src/main/java/com/ai/assistance/operit/data/model/FunctionType.kt @@ -12,5 +12,7 @@ enum class FunctionType { ROLE_RESPONSE_PLANNER, // 角色回答顺序规划 IMAGE_RECOGNITION, // 图像识别 AUDIO_RECOGNITION, // 音频识别 - VIDEO_RECOGNITION // 视频识别 + VIDEO_RECOGNITION, // 视频识别 + TOOL_REVIEW, // 工具调用合规审查 + FILE_DISTILL // 文件读取提炼(read 族工具的 AI 摘录提取) } diff --git a/app/src/main/java/com/ai/assistance/operit/data/preferences/ApiPreferences.kt b/app/src/main/java/com/ai/assistance/operit/data/preferences/ApiPreferences.kt index 3282ae123e..2029d791da 100644 --- a/app/src/main/java/com/ai/assistance/operit/data/preferences/ApiPreferences.kt +++ b/app/src/main/java/com/ai/assistance/operit/data/preferences/ApiPreferences.kt @@ -123,6 +123,16 @@ class ApiPreferences private constructor(private val context: Context) { // Key for Tools Enable/Disable val ENABLE_TOOLS = booleanPreferencesKey("enable_tools") + // 修改意图:工具调用的大模型自动审查开关(三开关:文件工具区内/区外 + 高危工具)。 + // 仅在工具模式为"允许"时生效与显示 + val TOOL_REVIEW_INSIDE_WORKSPACE = booleanPreferencesKey("tool_review_inside_workspace") + val TOOL_REVIEW_OUTSIDE_WORKSPACE = booleanPreferencesKey("tool_review_outside_workspace") + val TOOL_REVIEW_HIGH_RISK_TOOLS = booleanPreferencesKey("tool_review_high_risk_tools") + + // 修改意图:文件读取 AI 提炼开关(read_file/read_file_part/read_file_full 默认走功能模型提炼)。 + // 默认开启;关闭时三工具回到原始内容返回行为 + val FILE_DISTILL_ENABLED = booleanPreferencesKey("file_distill_enabled") + // Key for per-tool prompt visibility val TOOL_PROMPT_VISIBILITY_JSON = stringPreferencesKey("tool_prompt_visibility_json") @@ -159,6 +169,14 @@ class ApiPreferences private constructor(private val context: Context) { // Default value for Tools Enable/Disable const val DEFAULT_ENABLE_TOOLS = true + // 修改意图:自动审查默认关闭(三开关一致) + const val DEFAULT_TOOL_REVIEW_INSIDE_WORKSPACE = false + const val DEFAULT_TOOL_REVIEW_OUTSIDE_WORKSPACE = false + const val DEFAULT_TOOL_REVIEW_HIGH_RISK_TOOLS = false + + // 文件读取 AI 提炼默认开启 + const val DEFAULT_FILE_DISTILL_ENABLED = true + // Default value for Disable Stream Output (default false, meaning stream is enabled by default) const val DEFAULT_DISABLE_STREAM_OUTPUT = false @@ -275,6 +293,28 @@ class ApiPreferences private constructor(private val context: Context) { preferences[ENABLE_TOOLS] ?: DEFAULT_ENABLE_TOOLS } + // 修改意图:工具调用自动审查开关(区内/区外 + 高危工具) + val toolReviewInsideWorkspaceFlow: Flow = + context.apiDataStore.data.map { preferences -> + preferences[TOOL_REVIEW_INSIDE_WORKSPACE] ?: DEFAULT_TOOL_REVIEW_INSIDE_WORKSPACE + } + + val toolReviewOutsideWorkspaceFlow: Flow = + context.apiDataStore.data.map { preferences -> + preferences[TOOL_REVIEW_OUTSIDE_WORKSPACE] ?: DEFAULT_TOOL_REVIEW_OUTSIDE_WORKSPACE + } + + val toolReviewHighRiskToolsFlow: Flow = + context.apiDataStore.data.map { preferences -> + preferences[TOOL_REVIEW_HIGH_RISK_TOOLS] ?: DEFAULT_TOOL_REVIEW_HIGH_RISK_TOOLS + } + + // 修改意图:文件读取 AI 提炼开关 Flow + val fileDistillEnabledFlow: Flow = + context.apiDataStore.data.map { preferences -> + preferences[FILE_DISTILL_ENABLED] ?: DEFAULT_FILE_DISTILL_ENABLED + } + // Flow for per-tool prompt visibility val toolPromptVisibilityFlow: Flow> = context.apiDataStore.data.map { preferences -> @@ -388,6 +428,32 @@ class ApiPreferences private constructor(private val context: Context) { context.apiDataStore.edit { preferences -> preferences[ENABLE_TOOLS] = isEnabled } } + // 修改意图:保存工具调用自动审查开关(区内/区外 + 高危工具) + suspend fun saveToolReviewInsideWorkspace(isEnabled: Boolean) { + context.apiDataStore.edit { preferences -> + preferences[TOOL_REVIEW_INSIDE_WORKSPACE] = isEnabled + } + } + + suspend fun saveToolReviewOutsideWorkspace(isEnabled: Boolean) { + context.apiDataStore.edit { preferences -> + preferences[TOOL_REVIEW_OUTSIDE_WORKSPACE] = isEnabled + } + } + + suspend fun saveToolReviewHighRiskTools(isEnabled: Boolean) { + context.apiDataStore.edit { preferences -> + preferences[TOOL_REVIEW_HIGH_RISK_TOOLS] = isEnabled + } + } + + // 修改意图:保存文件读取 AI 提炼开关 + suspend fun saveFileDistillEnabled(isEnabled: Boolean) { + context.apiDataStore.edit { preferences -> + preferences[FILE_DISTILL_ENABLED] = isEnabled + } + } + // Save prompt visibility for a single tool suspend fun saveToolPromptVisibility(toolName: String, isVisible: Boolean) { context.apiDataStore.edit { preferences -> diff --git a/app/src/main/java/com/ai/assistance/operit/data/repository/ChatHistoryManager.kt b/app/src/main/java/com/ai/assistance/operit/data/repository/ChatHistoryManager.kt index 193b07d40a..276df34113 100644 --- a/app/src/main/java/com/ai/assistance/operit/data/repository/ChatHistoryManager.kt +++ b/app/src/main/java/com/ai/assistance/operit/data/repository/ChatHistoryManager.kt @@ -1646,6 +1646,22 @@ class ChatHistoryManager private constructor(private val context: Context) { } } + /** + * 修改意图:工具执行层的工作区防护需要按 chatId 解析会话绑定的工作区根与环境。 + * 返回 (workspace, workspaceEnv),未绑定或查询失败时均为 null(视为未绑定工作区)。 + */ + suspend fun getChatWorkspaceBinding(chatId: String): Pair { + return kotlinx.coroutines.withContext(Dispatchers.IO) { + try { + val chat = chatDao.getChatById(chatId) + Pair(chat?.workspace, chat?.workspaceEnv) + } catch (e: Exception) { + AppLogger.e(TAG, "Failed to get chat workspace binding for chat $chatId", e) + Pair(null, null) + } + } + } + // 直接加载聊天消息 suspend fun loadChatMessages(chatId: String): List { return kotlinx.coroutines.withContext(Dispatchers.IO) { diff --git a/app/src/main/java/com/ai/assistance/operit/services/core/ApiConfigDelegate.kt b/app/src/main/java/com/ai/assistance/operit/services/core/ApiConfigDelegate.kt index 4404cce630..dd772c8a07 100644 --- a/app/src/main/java/com/ai/assistance/operit/services/core/ApiConfigDelegate.kt +++ b/app/src/main/java/com/ai/assistance/operit/services/core/ApiConfigDelegate.kt @@ -126,6 +126,23 @@ class ApiConfigDelegate( private val _enableTools = MutableStateFlow(ApiPreferences.DEFAULT_ENABLE_TOOLS) val enableTools: StateFlow = _enableTools.asStateFlow() + // 修改意图:工具调用大模型自动审查开关(区内/区外 + 高危工具),仅工具模式为允许时生效与显示 + private val _toolReviewInsideWorkspace = + MutableStateFlow(ApiPreferences.DEFAULT_TOOL_REVIEW_INSIDE_WORKSPACE) + val toolReviewInsideWorkspace: StateFlow = _toolReviewInsideWorkspace.asStateFlow() + + private val _toolReviewOutsideWorkspace = + MutableStateFlow(ApiPreferences.DEFAULT_TOOL_REVIEW_OUTSIDE_WORKSPACE) + val toolReviewOutsideWorkspace: StateFlow = _toolReviewOutsideWorkspace.asStateFlow() + + private val _toolReviewHighRiskTools = + MutableStateFlow(ApiPreferences.DEFAULT_TOOL_REVIEW_HIGH_RISK_TOOLS) + val toolReviewHighRiskTools: StateFlow = _toolReviewHighRiskTools.asStateFlow() + + // 修改意图:文件读取 AI 提炼开关,默认开启;关闭时 read 族工具回到原始内容返回行为 + private val _fileDistillEnabled = MutableStateFlow(ApiPreferences.DEFAULT_FILE_DISTILL_ENABLED) + val fileDistillEnabled: StateFlow = _fileDistillEnabled.asStateFlow() + private val _toolPromptVisibility = MutableStateFlow>(emptyMap()) val toolPromptVisibility: StateFlow> = _toolPromptVisibility.asStateFlow() @@ -460,6 +477,30 @@ class ApiConfigDelegate( } } + // 修改意图:收集文件工具自动审查开关 + configScope.launch { + apiPreferences.toolReviewInsideWorkspaceFlow.collect { enabled -> + _toolReviewInsideWorkspace.value = enabled + } + } + configScope.launch { + apiPreferences.toolReviewOutsideWorkspaceFlow.collect { enabled -> + _toolReviewOutsideWorkspace.value = enabled + } + } + // 修改意图:收集高危工具自动审查开关 + configScope.launch { + apiPreferences.toolReviewHighRiskToolsFlow.collect { enabled -> + _toolReviewHighRiskTools.value = enabled + } + } + // 修改意图:收集文件读取 AI 提炼开关 + configScope.launch { + apiPreferences.fileDistillEnabledFlow.collect { enabled -> + _fileDistillEnabled.value = enabled + } + } + // Collect tool prompt visibility setting configScope.launch { apiPreferences.toolPromptVisibilityFlow.collect { visibility -> @@ -783,6 +824,36 @@ class ApiConfigDelegate( } } + // 修改意图:设置工具调用自动审查开关(区内/区外 + 高危工具) + fun saveToolReviewInsideWorkspace(isEnabled: Boolean) { + configScope.launch { + apiPreferences.saveToolReviewInsideWorkspace(isEnabled) + _toolReviewInsideWorkspace.value = isEnabled + } + } + + fun saveToolReviewOutsideWorkspace(isEnabled: Boolean) { + configScope.launch { + apiPreferences.saveToolReviewOutsideWorkspace(isEnabled) + _toolReviewOutsideWorkspace.value = isEnabled + } + } + + fun saveToolReviewHighRiskTools(isEnabled: Boolean) { + configScope.launch { + apiPreferences.saveToolReviewHighRiskTools(isEnabled) + _toolReviewHighRiskTools.value = isEnabled + } + } + + // 修改意图:保存文件读取 AI 提炼开关 + fun saveFileDistillEnabled(isEnabled: Boolean) { + configScope.launch { + apiPreferences.saveFileDistillEnabled(isEnabled) + _fileDistillEnabled.value = isEnabled + } + } + fun saveToolPromptVisibility(toolName: String, isVisible: Boolean) { configScope.launch { apiPreferences.saveToolPromptVisibility(toolName, isVisible) diff --git a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/agent/AgentChatInputSection.kt b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/agent/AgentChatInputSection.kt index b9484c9410..f6ecf3a915 100644 --- a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/agent/AgentChatInputSection.kt +++ b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/agent/AgentChatInputSection.kt @@ -206,6 +206,17 @@ fun AgentChatInputSection( inputMenuRuntime: String = "main", permissionLevel: PermissionLevel = PermissionLevel.ASK, onSetPermissionLevel: (PermissionLevel) -> Unit = {}, + // 修改意图:工具调用大模型自动审查开关(区内/区外 + 高危工具)。仅在工具模式为允许时展示, + // 经 AgentExtraSettingsPopup 透传至 AgentToolsPermissionGroupItem + toolReviewInsideWorkspace: Boolean = false, + toolReviewOutsideWorkspace: Boolean = false, + toolReviewHighRiskTools: Boolean = false, + onSaveToolReviewInsideWorkspace: (Boolean) -> Unit = {}, + onSaveToolReviewOutsideWorkspace: (Boolean) -> Unit = {}, + onSaveToolReviewHighRiskTools: (Boolean) -> Unit = {}, + // 修改意图:文件读取 AI 提炼开关,审查三开关之下展示 + fileDistillEnabled: Boolean = true, + onSaveFileDistillEnabled: (Boolean) -> Unit = {}, enableMemoryAutoUpdate: Boolean = false, onToggleMemoryAutoUpdate: () -> Unit = {}, isAutoReadEnabled: Boolean = false, @@ -1434,6 +1445,14 @@ fun AgentChatInputSection( onToggleAutoRead = onToggleAutoRead, permissionLevel = permissionLevel, onSetPermissionLevel = onSetPermissionLevel, + toolReviewInsideWorkspace = toolReviewInsideWorkspace, + toolReviewOutsideWorkspace = toolReviewOutsideWorkspace, + toolReviewHighRiskTools = toolReviewHighRiskTools, + onSaveToolReviewInsideWorkspace = onSaveToolReviewInsideWorkspace, + onSaveToolReviewOutsideWorkspace = onSaveToolReviewOutsideWorkspace, + onSaveToolReviewHighRiskTools = onSaveToolReviewHighRiskTools, + fileDistillEnabled = fileDistillEnabled, + onSaveFileDistillEnabled = onSaveFileDistillEnabled, enableTools = enableTools, onToggleTools = onToggleTools, disableStreamOutput = disableStreamOutput, @@ -2233,6 +2252,17 @@ private fun AgentExtraSettingsPopup( onToggleAutoRead: () -> Unit, permissionLevel: PermissionLevel, onSetPermissionLevel: (PermissionLevel) -> Unit, + // 修改意图:工具调用大模型自动审查开关(区内/区外 + 高危工具)。仅在工具模式为允许时展示, + // 开启后 ToolExecutionManager 在放行前调用 TOOL_REVIEW 功能模型审查 + toolReviewInsideWorkspace: Boolean = false, + toolReviewOutsideWorkspace: Boolean = false, + toolReviewHighRiskTools: Boolean = false, + onSaveToolReviewInsideWorkspace: (Boolean) -> Unit = {}, + onSaveToolReviewOutsideWorkspace: (Boolean) -> Unit = {}, + onSaveToolReviewHighRiskTools: (Boolean) -> Unit = {}, + // 修改意图:文件读取 AI 提炼开关,审查三开关之下展示 + fileDistillEnabled: Boolean = true, + onSaveFileDistillEnabled: (Boolean) -> Unit = {}, enableTools: Boolean, onToggleTools: () -> Unit, disableStreamOutput: Boolean, @@ -2394,6 +2424,14 @@ private fun AgentExtraSettingsPopup( onSetPermissionLevel(level) } }, + toolReviewInsideWorkspace = toolReviewInsideWorkspace, + toolReviewOutsideWorkspace = toolReviewOutsideWorkspace, + toolReviewHighRiskTools = toolReviewHighRiskTools, + onSaveToolReviewInsideWorkspace = onSaveToolReviewInsideWorkspace, + onSaveToolReviewOutsideWorkspace = onSaveToolReviewOutsideWorkspace, + onSaveToolReviewHighRiskTools = onSaveToolReviewHighRiskTools, + fileDistillEnabled = fileDistillEnabled, + onSaveFileDistillEnabled = onSaveFileDistillEnabled, toolSlotToggles = inputMenuTogglesBySlot[InputMenuToggleSlots.TOOLS].orEmpty(), onToggleInfoClick = { title, description -> infoPopupContent = title to description }, onManageTools = { showToolPromptManagerDialog = true }, @@ -2678,6 +2716,14 @@ private fun AgentToolsPermissionGroupItem( expanded: Boolean, onExpandedChange: (Boolean) -> Unit, onSelectPermissionLevel: (PermissionLevel) -> Unit, + toolReviewInsideWorkspace: Boolean, + toolReviewOutsideWorkspace: Boolean, + toolReviewHighRiskTools: Boolean, + onSaveToolReviewInsideWorkspace: (Boolean) -> Unit, + onSaveToolReviewOutsideWorkspace: (Boolean) -> Unit, + onSaveToolReviewHighRiskTools: (Boolean) -> Unit, + fileDistillEnabled: Boolean, + onSaveFileDistillEnabled: (Boolean) -> Unit, toolSlotToggles: List, onToggleInfoClick: (String, String) -> Unit, onManageTools: () -> Unit, @@ -2712,6 +2758,37 @@ private fun AgentToolsPermissionGroupItem( selectedLevel = effectiveLevel, onSelectPermissionLevel = onSelectPermissionLevel, ) + // 修改意图:工具调用大模型自动审查三开关。仅在工具模式为允许时显示(需求确认项); + // 区内/区外管文件工具(按工作区路径分区),高危工具管 HighRiskToolCatalog 高危工具族 + if (effectiveLevel == PermissionLevel.ALLOW) { + Text( + text = stringResource(R.string.agent_menu_tool_review_section), + fontSize = 12.sp, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(top = 4.dp), + ) + AgentReviewToggleRow( + label = stringResource(R.string.agent_menu_tool_review_inside), + checked = toolReviewInsideWorkspace, + onCheckedChange = onSaveToolReviewInsideWorkspace, + ) + AgentReviewToggleRow( + label = stringResource(R.string.agent_menu_tool_review_outside), + checked = toolReviewOutsideWorkspace, + onCheckedChange = onSaveToolReviewOutsideWorkspace, + ) + AgentReviewToggleRow( + label = stringResource(R.string.agent_menu_tool_review_high_risk), + checked = toolReviewHighRiskTools, + onCheckedChange = onSaveToolReviewHighRiskTools, + ) + // 修改意图:文件读取 AI 提炼开关,位于审查三开关之下(需求确认项) + AgentReviewToggleRow( + label = stringResource(R.string.agent_menu_file_distill), + checked = fileDistillEnabled, + onCheckedChange = onSaveFileDistillEnabled, + ) + } toolSlotToggles.forEach { toggle -> AgentInputMenuToggleSettingItem( toggle = toggle, @@ -2736,6 +2813,36 @@ private fun AgentToolsPermissionGroupItem( } } +@Composable +private fun AgentReviewToggleRow( + label: String, + checked: Boolean, + onCheckedChange: (Boolean) -> Unit, +) { + Row( + modifier = + Modifier + .fillMaxWidth() + .heightIn(min = 36.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Text( + text = label, + color = MaterialTheme.colorScheme.onSurfaceVariant, + fontSize = 12.sp, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + modifier = Modifier.weight(1f), + ) + Spacer(modifier = Modifier.width(8.dp)) + Switch( + checked = checked, + onCheckedChange = onCheckedChange, + modifier = Modifier.height(28.dp).width(48.dp), + ) + } +} + @Composable private fun AgentPermissionSegmentedControl( selectedLevel: PermissionLevel, diff --git a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/classic/ClassicChatSettingsBar.kt b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/classic/ClassicChatSettingsBar.kt index 1bc0267f46..408f1ed428 100644 --- a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/classic/ClassicChatSettingsBar.kt +++ b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/components/style/input/classic/ClassicChatSettingsBar.kt @@ -96,6 +96,16 @@ fun ClassicChatSettingsBar( inputMenuRuntime: String = "main", permissionLevel: PermissionLevel, onSetPermissionLevel: (PermissionLevel) -> Unit, + // 修改意图:工具调用大模型自动审查开关(区内/区外 + 高危工具),与 Agent 风格菜单一致,仅允许模式展示 + toolReviewInsideWorkspace: Boolean = false, + toolReviewOutsideWorkspace: Boolean = false, + toolReviewHighRiskTools: Boolean = false, + onSaveToolReviewInsideWorkspace: (Boolean) -> Unit = {}, + onSaveToolReviewOutsideWorkspace: (Boolean) -> Unit = {}, + onSaveToolReviewHighRiskTools: (Boolean) -> Unit = {}, + // 修改意图:文件读取 AI 提炼开关,审查三开关之下展示 + fileDistillEnabled: Boolean = true, + onSaveFileDistillEnabled: (Boolean) -> Unit = {}, enableThinkingMode: Boolean, onToggleThinkingMode: () -> Unit, thinkingOptionId: String, @@ -661,6 +671,36 @@ fun ClassicChatSettingsBar( showMenu = false } ) + // 修改意图:工具调用自动审查三开关,仅允许模式展示,与 Agent 风格一致 + if (enableTools && permissionLevel == PermissionLevel.ALLOW) { + Text( + text = stringResource(R.string.agent_menu_tool_review_section), + fontSize = 12.sp, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp) + ) + ClassicReviewToggleRow( + label = stringResource(R.string.agent_menu_tool_review_inside), + checked = toolReviewInsideWorkspace, + onCheckedChange = onSaveToolReviewInsideWorkspace, + ) + ClassicReviewToggleRow( + label = stringResource(R.string.agent_menu_tool_review_outside), + checked = toolReviewOutsideWorkspace, + onCheckedChange = onSaveToolReviewOutsideWorkspace, + ) + ClassicReviewToggleRow( + label = stringResource(R.string.agent_menu_tool_review_high_risk), + checked = toolReviewHighRiskTools, + onCheckedChange = onSaveToolReviewHighRiskTools, + ) + // 修改意图:文件读取 AI 提炼开关,位于审查三开关之下(需求确认项) + ClassicReviewToggleRow( + label = stringResource(R.string.agent_menu_file_distill), + checked = fileDistillEnabled, + onCheckedChange = onSaveFileDistillEnabled, + ) + } inputMenuTogglesBySlot[InputMenuToggleSlots.TOOLS].orEmpty().forEach { toggle -> InputMenuToggleSettingItem( toggle = toggle, @@ -1807,3 +1847,33 @@ private fun ActionSettingItem( ) } } + +/** 修改意图:Classic 风格设置条的工具调用自动审查开关行,与 Agent 风格的 AgentReviewToggleRow 对齐 */ +@Composable +private fun ClassicReviewToggleRow( + label: String, + checked: Boolean, + onCheckedChange: (Boolean) -> Unit +) { + Row( + modifier = Modifier + .fillMaxWidth() + .heightIn(min = 36.dp) + .padding(horizontal = 12.dp, vertical = 4.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Text( + text = label, + fontSize = 13.sp, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 2, + overflow = TextOverflow.Ellipsis, + modifier = Modifier.weight(1f) + ) + Spacer(modifier = Modifier.width(8.dp)) + Switch( + checked = checked, + onCheckedChange = onCheckedChange + ) + } +} diff --git a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/screens/AIChatScreen.kt b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/screens/AIChatScreen.kt index 6c470d1139..f4ed3a5d95 100644 --- a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/screens/AIChatScreen.kt +++ b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/screens/AIChatScreen.kt @@ -1011,6 +1011,26 @@ val actualViewModel: ChatViewModel = viewModel ?: viewModel { ChatViewModel(cont .collectAsState() .value, onSetPermissionLevel = actualViewModel::setMasterPermissionLevel, + toolReviewInsideWorkspace = + actualViewModel.toolReviewInsideWorkspace + .collectAsState() + .value, + toolReviewOutsideWorkspace = + actualViewModel.toolReviewOutsideWorkspace + .collectAsState() + .value, + toolReviewHighRiskTools = + actualViewModel.toolReviewHighRiskTools + .collectAsState() + .value, + fileDistillEnabled = + actualViewModel.fileDistillEnabled + .collectAsState() + .value, + onSaveToolReviewInsideWorkspace = actualViewModel::saveToolReviewInsideWorkspace, + onSaveToolReviewOutsideWorkspace = actualViewModel::saveToolReviewOutsideWorkspace, + onSaveToolReviewHighRiskTools = actualViewModel::saveToolReviewHighRiskTools, + onSaveFileDistillEnabled = actualViewModel::saveFileDistillEnabled, enableThinkingMode = enableThinkingMode, onToggleThinkingMode = { actualViewModel.toggleThinkingMode() }, thinkingOptionId = thinkingOptionId, @@ -1528,6 +1548,12 @@ private fun ChatInputBottomBar( val attachmentPanelState by actualViewModel.attachmentPanelState.collectAsState() val replyToMessage by actualViewModel.replyToMessage.collectAsState() val permissionLevel by actualViewModel.masterPermissionLevel.collectAsState() + // 修改意图:文件工具自动审查开关(区内/区外),工具菜单在允许模式下展示 + val toolReviewInsideWorkspace by actualViewModel.toolReviewInsideWorkspace.collectAsState() + val toolReviewOutsideWorkspace by actualViewModel.toolReviewOutsideWorkspace.collectAsState() + val toolReviewHighRiskTools by actualViewModel.toolReviewHighRiskTools.collectAsState() + // 修改意图:文件读取 AI 提炼开关(工具菜单允许模式下展示) + val fileDistillEnabled by actualViewModel.fileDistillEnabled.collectAsState() val isSummarizing by actualViewModel.isSummarizing.collectAsState() val isSendTriggeredSummarizing by actualViewModel.isSendTriggeredSummarizing.collectAsState() val isWaifuModeEnabled by waifuPreferences.enableWaifuModeFlow.collectAsState(initial = false) @@ -1916,6 +1942,14 @@ private fun ChatInputBottomBar( inputMenuRuntime = inputMenuRuntime, permissionLevel = permissionLevel, onSetPermissionLevel = actualViewModel::setMasterPermissionLevel, + toolReviewInsideWorkspace = toolReviewInsideWorkspace, + toolReviewOutsideWorkspace = toolReviewOutsideWorkspace, + toolReviewHighRiskTools = toolReviewHighRiskTools, + fileDistillEnabled = fileDistillEnabled, + onSaveToolReviewInsideWorkspace = actualViewModel::saveToolReviewInsideWorkspace, + onSaveToolReviewOutsideWorkspace = actualViewModel::saveToolReviewOutsideWorkspace, + onSaveToolReviewHighRiskTools = actualViewModel::saveToolReviewHighRiskTools, + onSaveFileDistillEnabled = actualViewModel::saveFileDistillEnabled, enableMemoryAutoUpdate = enableMemoryAutoUpdate, onToggleMemoryAutoUpdate = actualViewModel::toggleMemoryAutoUpdate, isAutoReadEnabled = isAutoReadEnabled, diff --git a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/viewmodel/ChatViewModel.kt b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/viewmodel/ChatViewModel.kt index a718eae272..94ac91147a 100644 --- a/app/src/main/java/com/ai/assistance/operit/ui/features/chat/viewmodel/ChatViewModel.kt +++ b/app/src/main/java/com/ai/assistance/operit/ui/features/chat/viewmodel/ChatViewModel.kt @@ -218,6 +218,20 @@ class ChatViewModel(private val context: Context) : ViewModel() { val thinkingOptionId: StateFlow by lazy { apiConfigDelegate.thinkingOptionId } val enableMemoryAutoUpdate: StateFlow by lazy { apiConfigDelegate.enableMemoryAutoUpdate } val enableTools: StateFlow by lazy { apiConfigDelegate.enableTools } + // 修改意图:工具调用自动审查开关(区内/区外 + 高危工具),会话工具菜单仅在允许模式下展示 + val toolReviewInsideWorkspace: StateFlow by lazy { + apiConfigDelegate.toolReviewInsideWorkspace + } + val toolReviewOutsideWorkspace: StateFlow by lazy { + apiConfigDelegate.toolReviewOutsideWorkspace + } + val toolReviewHighRiskTools: StateFlow by lazy { + apiConfigDelegate.toolReviewHighRiskTools + } + // 修改意图:文件读取 AI 提炼开关,会话工具菜单在允许模式下展示 + val fileDistillEnabled: StateFlow by lazy { + apiConfigDelegate.fileDistillEnabled + } val toolPromptVisibility: StateFlow> by lazy { apiConfigDelegate.toolPromptVisibility } val disableStreamOutput: StateFlow by lazy { apiConfigDelegate.disableStreamOutput } val disableUserPreferenceDescription: StateFlow by lazy { @@ -635,6 +649,24 @@ class ChatViewModel(private val context: Context) : ViewModel() { apiConfigDelegate.toggleTools() } + // 修改意图:设置工具调用自动审查开关(区内/区外 + 高危工具) + fun saveToolReviewInsideWorkspace(isEnabled: Boolean) { + apiConfigDelegate.saveToolReviewInsideWorkspace(isEnabled) + } + + fun saveToolReviewOutsideWorkspace(isEnabled: Boolean) { + apiConfigDelegate.saveToolReviewOutsideWorkspace(isEnabled) + } + + fun saveToolReviewHighRiskTools(isEnabled: Boolean) { + apiConfigDelegate.saveToolReviewHighRiskTools(isEnabled) + } + + // 修改意图:保存文件读取 AI 提炼开关 + fun saveFileDistillEnabled(isEnabled: Boolean) { + apiConfigDelegate.saveFileDistillEnabled(isEnabled) + } + fun saveToolPromptVisibility(toolName: String, isVisible: Boolean) { apiConfigDelegate.saveToolPromptVisibility(toolName, isVisible) } diff --git a/app/src/main/java/com/ai/assistance/operit/ui/features/settings/screens/FunctionalConfigScreen.kt b/app/src/main/java/com/ai/assistance/operit/ui/features/settings/screens/FunctionalConfigScreen.kt index 4ee8ab2cda..a4af21a6ed 100644 --- a/app/src/main/java/com/ai/assistance/operit/ui/features/settings/screens/FunctionalConfigScreen.kt +++ b/app/src/main/java/com/ai/assistance/operit/ui/features/settings/screens/FunctionalConfigScreen.kt @@ -728,6 +728,50 @@ fun FunctionConfigCard( .collect { chunk -> buffer.append(chunk) } buffer.toString() } + // 修改意图:新增 TOOL_REVIEW 枚举值的连接测试分支, + // 与 CHAT 同款简单问答即可验证连通性(when 表达式非穷尽会编译失败) + FunctionType.TOOL_REVIEW -> { + val parameters = + modelConfigManager.getModelParametersForConfig(configWithSelectedModel.id) + val buffer = StringBuilder() + service.sendMessage( + context, + listOf( + PromptTurn( + kind = PromptTurnKind.USER, + content = "Connection test: reply with {\"compliant\": true, \"reason\": \"ok\"} only." + ) + ), + parameters, + stream = false, + enableRetry = false, + recordTokenUsage = false, + ) + .collect { chunk -> buffer.append(chunk) } + buffer.toString() + } + // 修改意图:新增 FILE_DISTILL 枚举值的连接测试分支, + // 简单 JSON 问答验证连通性(when 表达式非穷尽会编译失败) + FunctionType.FILE_DISTILL -> { + val parameters = + modelConfigManager.getModelParametersForConfig(configWithSelectedModel.id) + val buffer = StringBuilder() + service.sendMessage( + context, + listOf( + PromptTurn( + kind = PromptTurnKind.USER, + content = "Connection test: reply with {\"related\": true, \"worth\": false, \"excerpt\": \"\", \"file_summary\": \"ok\"} only." + ) + ), + parameters, + stream = false, + enableRetry = false, + recordTokenUsage = false, + ) + .collect { chunk -> buffer.append(chunk) } + buffer.toString() + } } testResult = FunctionTestDisplay.fromResponse( @@ -984,6 +1028,8 @@ fun getFunctionDisplayName(functionType: FunctionType): String { FunctionType.IMAGE_RECOGNITION -> stringResource(id = R.string.function_type_image_recognition) FunctionType.AUDIO_RECOGNITION -> stringResource(id = R.string.function_type_audio_recognition) FunctionType.VIDEO_RECOGNITION -> stringResource(id = R.string.function_type_video_recognition) + FunctionType.TOOL_REVIEW -> stringResource(id = R.string.function_type_tool_review) + FunctionType.FILE_DISTILL -> stringResource(id = R.string.function_type_file_distill) } } @@ -1002,6 +1048,8 @@ fun getFunctionDescription(functionType: FunctionType): String { FunctionType.IMAGE_RECOGNITION -> stringResource(id = R.string.function_desc_image_recognition) FunctionType.AUDIO_RECOGNITION -> stringResource(id = R.string.function_desc_audio_recognition) FunctionType.VIDEO_RECOGNITION -> stringResource(id = R.string.function_desc_video_recognition) + FunctionType.TOOL_REVIEW -> stringResource(id = R.string.function_desc_tool_review) + FunctionType.FILE_DISTILL -> stringResource(id = R.string.function_desc_file_distill) } } diff --git a/app/src/main/java/com/ai/assistance/operit/ui/permissions/ToolPermissionSystem.kt b/app/src/main/java/com/ai/assistance/operit/ui/permissions/ToolPermissionSystem.kt index aa524e1789..f013f3a9a1 100644 --- a/app/src/main/java/com/ai/assistance/operit/ui/permissions/ToolPermissionSystem.kt +++ b/app/src/main/java/com/ai/assistance/operit/ui/permissions/ToolPermissionSystem.kt @@ -191,28 +191,46 @@ class ToolPermissionSystem private constructor(private val context: Context) { */ suspend fun checkToolPermission(tool: AITool): ToolPermissionCheckResult { AppLogger.d(TAG, "Starting permission check: ${tool.name}") - + val preferences = context.toolPermissionsDataStore.data.first() val masterSwitch = PermissionLevel.fromString(preferences[MASTER_SWITCH] ?: DEFAULT_MASTER_SWITCH) val key = toolPermissionKey(tool.name) val overrideLevel = preferences[key]?.let { PermissionLevel.fromString(it) } - + val permissionLevel = overrideLevel ?: masterSwitch - + return when (permissionLevel) { PermissionLevel.ALLOW -> ToolPermissionCheckResult.GRANTED PermissionLevel.ASK -> requestPermission(tool) PermissionLevel.FORBID -> ToolPermissionCheckResult.DENIED } } - + + /** + * 修改意图:工作区防护需要"无视允许模式强制询问"的能力。与 checkToolPermission 不同, + * 本方法不做级别判断,直接复用 requestPermission 的悬浮窗链路弹窗确认; + * reason 会附加在操作描述之后展示给用户(区外路径说明、AI 审查意见等)。 + */ + suspend fun requestConfirmation(tool: AITool, reason: String): ToolPermissionCheckResult { + return requestPermission(tool, reason) + } + /** * Request permission from the user to execute a tool */ - private suspend fun requestPermission(tool: AITool): ToolPermissionCheckResult { - // Get operation description - val operationDescription = getOperationDescription(tool) - + private suspend fun requestPermission( + tool: AITool, + reasonSuffix: String? = null + ): ToolPermissionCheckResult { + // Get operation description;附加原因(区外提示/审查意见)一并展示 + val operationDescription = buildString { + append(getOperationDescription(tool)) + if (!reasonSuffix.isNullOrBlank()) { + append("\n\n") + append(reasonSuffix) + } + } + AppLogger.d(TAG, "Requesting permission: ${tool.name}") // Clear existing request diff --git a/app/src/main/java/com/ai/assistance/operit/util/PathMapper.kt b/app/src/main/java/com/ai/assistance/operit/util/PathMapper.kt index 86483c3b35..80ec6ba6c0 100644 --- a/app/src/main/java/com/ai/assistance/operit/util/PathMapper.kt +++ b/app/src/main/java/com/ai/assistance/operit/util/PathMapper.kt @@ -11,48 +11,85 @@ object PathMapper { /** * 将Linux路径转换为Android文件系统中的实际路径 - * + * + * 评审 P1:`..` 可以逃出 proot rootfs(旧实现 `File(ubuntuRoot, relativePath).absolutePath` + * 不 canonicalize、不做 containment 校验,`grep_code(path=/../../../../data/...)` 会扫描 + * 宿主文件系统)。拼接结果 canonical 化并强制 containment:逃出 rootfs 一律返回 null, + * 调用方必须显式拒绝,不得回落原路径。 + * * @param context Android上下文 * @param linuxPath Linux格式的路径,例如 "/home/user/test.txt" 或 "/etc/hosts" - * @return Android文件系统中的实际绝对路径 + * @return 宿主文件系统上的 canonical 绝对路径;逃出 rootfs 时为 null */ - fun mapLinuxPath(context: Context, linuxPath: String): String { + fun mapLinuxPath(context: Context, linuxPath: String): String? { // Ubuntu根目录位于 {filesDir}/usr/var/lib/proot-distro/installed-rootfs/ubuntu val ubuntuRoot = File( context.filesDir, "usr/var/lib/proot-distro/installed-rootfs/ubuntu" - ) - + ).canonicalFile + // 移除路径开头的/,然后拼接到Ubuntu根目录 - val relativePath = linuxPath.trimStart('/') - + val relativePath = linuxPath.trim().trimStart('/') + // 如果输入路径为空或只有/,返回Ubuntu根目录 if (relativePath.isEmpty()) { return ubuntuRoot.absolutePath } - - return File(ubuntuRoot, relativePath).absolutePath + + val rootPath = ubuntuRoot.absolutePath.trimEnd('/') + val canonical = + runCatching { File(ubuntuRoot, relativePath).canonicalFile }.getOrNull() ?: return null + val canonicalPath = canonical.absolutePath + if (canonicalPath == rootPath) { + return rootPath + } + if (!canonicalPath.startsWith("$rootPath/")) { + return null + } + return canonicalPath } - + + /** + * 修改意图:反向映射——把宿主文件系统上的 proot rootfs 路径还原为 Linux 逻辑路径。 + * 用于 grep 等在宿主路径上执行、但结果需要以 linux 路径回传给模型的场景。 + * 不在 rootfs 内的路径返回 null(调用方必须丢弃该结果,不得把宿主真实路径回传给模型)。 + */ + fun mapAndroidPathToLinux(context: Context, androidPath: String): String? { + val ubuntuRoot = + File(context.filesDir, "usr/var/lib/proot-distro/installed-rootfs/ubuntu") + .canonicalFile + val target = + runCatching { File(androidPath).canonicalFile }.getOrNull() ?: return null + val rootPath = ubuntuRoot.absolutePath.trimEnd('/') + val targetPath = target.absolutePath + if (targetPath == rootPath) { + return "/" + } + if (targetPath.startsWith("$rootPath/")) { + return "/" + targetPath.removePrefix("$rootPath/") + } + return null + } + /** * 判断是否为Linux环境 - * + * * @param environment 环境参数值 * @return 如果是Linux环境返回true,否则返回false */ fun isLinuxEnvironment(environment: String?): Boolean { return environment?.lowercase() == "linux" } - + /** * 根据environment参数转换路径 - * + * * @param context Android上下文 * @param path 原始路径 * @param environment 环境参数("android" 或 "linux") - * @return 转换后的实际路径 + * @return 转换后的实际路径;linux 环境下路径逃出 rootfs 时为 null,调用方必须显式拒绝 */ - fun resolvePath(context: Context, path: String, environment: String?): String { + fun resolvePath(context: Context, path: String, environment: String?): String? { return if (isLinuxEnvironment(environment)) { mapLinuxPath(context, path) } else { diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index e3623a7bdd..412cacb099 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -3056,6 +3056,10 @@ Process intelligent binding and mixing of file content, providing more precise code processing capabilities. Recommend using fast, inexpensive models like Gemini Flash, not recommended to use DeepSeek R1 Used for automatic traversal and operation of UI interfaces. The corresponding UI controller model is only activated when the user explicitly requests to use an "agent" for control, for example by choosing dedicated models such as autoglm-phone-9b. Used for grep_context planning and result filtering (multi-round grep_code) + Tool Call Review + Auto-reviews file tool calls for compliance when tool mode is Allow (per-zone toggles) + File Read Distillation + AI-distills file reads to relevant excerpts to save context; uses the main model when unconfigured Dynamically plans speaking order and skip decisions in groups, supports multi-turn discussions @@ -5834,11 +5838,11 @@ List all character cards Get chat messages: %1$s%2$s%3$s List directory: %1$s%2$s - Read file: %1$s%2$s + Read file (AI-distilled to relevant lines by default, raw=true for raw content): %1$s%2$s lines %1$s-%2$s from line %1$s - Read file (%1$s): %2$s%3$s - Read full file: %1$s%2$s + Read file (prefer the default AI-distilled read when unsure of line numbers; raw=true for exact ranges) (%1$s): %2$s%3$s + Read full file (text AI-distilled by default, raw=true for raw content): %1$s%2$s Read binary file: %1$s%2$s Append to file Write to file @@ -8576,4 +8580,15 @@ e.g. high Write path e.g. high + + + Tool %1$s is set to forbidden and the target path is outside the current session workspace (or the session has no workspace bound); this call is rejected. + ⚠ Outside-workspace operation confirmation: the following target paths are outside the current session workspace (or the session has no workspace bound). Review the paths carefully before allowing:\n%1$s + 🤖 AI review flagged this call as non-compliant: %1$s\nConfirm whether to still allow the execution. + ⚠ AI review could not complete: %1$s\nConfirm manually whether this call may run. + Auto-review tool calls + Auto-review file tools (inside workspace) + Auto-review file tools (outside workspace) + Auto-review high-risk tools + AI distillation for file reads diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 7cb223dfae..79d7fcb122 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -3302,6 +3302,8 @@ Ahora puede usar el modo AutoGLM en la interfaz de conversación. Reconocimiento de video Proporciona funciones de traducción multilingüe, admite la conversión de idioma del contenido de texto. Se recomienda usar un modelo que admita varios idiomas. Planificación de búsqueda y filtrado de resultados para grep_context (grep_code en múltiples rondas) + Revisión de llamadas de herramientas + Revisa automáticamente el cumplimiento de las llamadas a herramientas de archivos cuando el modo es Permitir (interruptores por zona) Preparando para reenviar el mensaje Reversión realizada, el contenido del mensaje se ha colocado en el campo de entrada @@ -7821,4 +7823,17 @@ Ahora puede usar el modo AutoGLM en la interfaz de conversación. Análisis de tendencias Configuración de estadísticas Aceptar + + + Destilado por IA de lecturas de archivos + Al leer archivos, la IA destila el contenido relevante para ahorrar contexto; si no está configurado, usa el modelo principal + La herramienta %1$s está configurada como prohibida y la ruta de destino está fuera del espacio de trabajo de la sesión actual (o la sesión no tiene espacio de trabajo vinculado); la llamada se rechaza. + ⚠ Confirmación de operación fuera del espacio de trabajo: las siguientes rutas de destino están fuera del espacio de trabajo de la sesión actual (o la sesión no tiene espacio de trabajo vinculado). Revisa las rutas con cuidado antes de permitir:\n%1$s + 🤖 La revisión de IA considera que esta llamada no cumple las normas: %1$s\nConfirma si aún deseas permitir la ejecución. + ⚠ La revisión de IA no pudo completarse: %1$s\nConfirma manualmente si esta llamada puede ejecutarse. + Revisión automática de llamadas a herramientas + Revisión automática de herramientas de archivo (dentro del espacio de trabajo) + Revisión automática de herramientas de archivo (fuera del espacio de trabajo) + Revisión automática de herramientas de alto riesgo + Destilado por IA para lecturas de archivos diff --git a/app/src/main/res/values-id/strings.xml b/app/src/main/res/values-id/strings.xml index 27b68538c4..f1772bbb28 100644 --- a/app/src/main/res/values-id/strings.xml +++ b/app/src/main/res/values-id/strings.xml @@ -2836,6 +2836,8 @@ Sekarang Anda dapat menggunakan mode AutoGLM di antarmuka percakapan. Digunakan untuk navigasi dan operasi otomatis antarmuka UI, model pengontrol UI terkait hanya akan diaktifkan ketika pengguna secara eksplisit meminta penggunaan kontrol "Agen", misalnya dapat memilih model khusus seperti autoglm. Menyediakan fungsi terjemahan multibahasa, mendukung konversi bahasa untuk konten teks. Disarankan menggunakan model yang mendukung multibahasa. Digunakan untuk perencanaan pencarian dan penyaringan hasil grep_context (grep_code multi-ronde) + Tinjauan panggilan alat + Meninjau kepatuhan panggilan alat file secara otomatis saat mode Diizinkan (saklar per zona) Digunakan untuk menentukan urutan giliran bicara dan strategi skip secara dinamis dalam grup, mendukung diskusi multi-ronde Digunakan untuk pengenalan dan analisis konten gambar, mendukung parameter intent untuk pemahaman mendalam saat memanggil alat read_file untuk membaca gambar. Disarankan menggunakan model multimodal yang mendukung visi. Digunakan untuk pengenalan dan analisis konten audio, dapat menggunakan parameter intent untuk pemahaman mendalam (seperti transkripsi/ringkasan/pengenalan konten pembicaraan) saat memanggil alat read_file untuk membaca audio. Disarankan menggunakan model yang mendukung input audio atau transkripsi berkualitas tinggi. @@ -7672,4 +7674,17 @@ Sekarang Anda dapat menggunakan mode AutoGLM di antarmuka percakapan. Ketuk grafik untuk melihat data detail Total Token Token puncak + + + Distilasi AI bacaan file + Saat membaca file, AI mendestilasi konten relevan untuk menghemat konteks; jika tidak dikonfigurasi, memakai model utama + Alat %1$s disetel ke terlarang dan jalur tujuan berada di luar workspace sesi saat ini (atau sesi tidak mengikat workspace); panggilan ini ditolak. + ⚠ Konfirmasi operasi di luar workspace: jalur tujuan berikut berada di luar workspace sesi saat ini (atau sesi tidak mengikat workspace). Periksa jalur dengan cermat sebelum mengizinkan:\n%1$s + 🤖 Tinjauan AI menilai panggilan ini tidak sesuai: %1$s\nKonfirmasi apakah tetap mengizinkan eksekusi. + ⚠ Tinjauan AI tidak dapat diselesaikan: %1$s\nKonfirmasi secara manual apakah panggilan ini boleh dijalankan. + Tinjauan otomatis panggilan alat + Tinjauan otomatis alat file (dalam workspace) + Tinjauan otomatis alat file (luar workspace) + Tinjauan otomatis alat berisiko tinggi + Distilasi AI untuk bacaan file diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 608f750f0a..8a6882b3a8 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -295,4 +295,17 @@ 接続できましたが、確認できませんでした 展開 折りたたむ + + + ファイル読み取りAI抽出 + ファイル読み取り時にAIが関連内容を抽出してコンテキストを節約します。未設定の場合はメインモデルを使用します + ツール %1$s は禁止に設定されており、対象パスは現在のセッションのワークスペース外(またはセッションにワークスペースが未バインド)のため、この呼び出しは拒否されます。 + ⚠ ワークスペース外操作の確認: 以下の対象パスは現在のセッションのワークスペース外(またはセッションにワークスペースが未バインド)です。許可する前にパスを慎重に確認してください:\n%1$s + 🤖 AIレビューでこの呼び出しは不適合と判断されました: %1$s\nそれでも実行を許可するか確認してください。 + ⚠ AIレビューを完了できませんでした: %1$s\nこの呼び出しの実行可否を手動で確認してください。 + ツール呼び出しの自動レビュー + ファイルツールの自動レビュー(ワークスペース内) + ファイルツールの自動レビュー(ワークスペース外) + 高リスクツールの自動レビュー + ファイル読み取りAI抽出 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 8a224c420e..fd6d8d48dc 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -2658,6 +2658,8 @@ UI 인터페이스의 자동 순회 및 작동에 사용됩니다. 해당 UI 컨트롤러 모델은 사용자가 제어를 위해 "에이전트"를 사용하도록 명시적으로 요청하는 경우에만 활성화됩니다(예: autoglm-phone-9b와 같은 전용 모델 선택). 다국어 번역 기능을 제공하여 텍스트 콘텐츠의 언어 변환을 지원합니다. 다국어를 지원하는 모델을 사용하는 것이 좋습니다. grep_context 계획 및 결과 필터링에 사용됩니다(다중 grep_code). + 도구 호출 검토 + 도구 모드가 허용일 때 파일 도구 호출의 규정 준수를 자동 검토합니다(구역별 스위치) 그룹 내 말하기 순서 및 건너뛰기 결정을 동적으로 계획하고 다단계 토론을 지원합니다. 이미지 내용 인식 및 분석에 사용되며, 깊은 이해를 위해 read_file 도구 호출 시 intent 매개변수를 지원합니다. 비전 기능을 갖춘 다중 모드 모델을 사용하는 것이 좋습니다. 오디오 콘텐츠 인식 및 분석에 사용됩니다. 오디오에 대해 read_file을 호출할 때 더 깊은 이해를 위해 intent(예: 전사, 요약, 화자별 내용)를 전달할 수 있습니다. 오디오 입력 또는 고품질 전사를 지원하는 모델 사용을 권장합니다. @@ -7519,4 +7521,17 @@ 차트를 탭하여 자세한 데이터 보기 누적 토큰 최대 토큰 + + + 파일 읽기 AI 추출 + 파일을 읽을 때 AI가 관련 내용을 추출해 컨텍스트를 절약합니다. 미설정 시 기본 모델을 사용합니다 + 도구 %1$s(이)가 금지로 설정되어 있고 대상 경로가 현재 세션 작업 공간 밖(또는 세션에 작업 공간이 바인딩되지 않음)이므로 이 호출은 거부됩니다. + ⚠ 작업 공간 외 작업 확인: 다음 대상 경로는 현재 세션 작업 공간 밖(또는 세션에 작업 공간이 바인딩되지 않음)에 있습니다. 허용하기 전에 경로를 신중히 확인하세요:\n%1$s + 🤖 AI 검토에서 이 호출이 규정 미준수로 판단되었습니다: %1$s\n그래도 실행을 허용할지 확인해 주세요. + ⚠ AI 검토를 완료하지 못했습니다: %1$s\n이 호출의 실행 여부를 직접 확인해 주세요. + 도구 호출 자동 검토 + 파일 도구 자동 검토(작업 공간 내) + 파일 도구 자동 검토(작업 공간 외) + 고위험 도구 자동 검토 + 파일 읽기 AI 추출 diff --git a/app/src/main/res/values-ms/strings.xml b/app/src/main/res/values-ms/strings.xml index 27b41708bc..37884276b5 100644 --- a/app/src/main/res/values-ms/strings.xml +++ b/app/src/main/res/values-ms/strings.xml @@ -2957,6 +2957,8 @@ Kini anda boleh menggunakan mod AutoGLM di antara muka perbualan. Digunakan untuk penerokaan dan pengendalian automatik antara muka UI, model pengawal UI yang sepadan hanya akan diaktifkan apabila pengguna secara eksplisit meminta penggunaan kawalan "agen", contohnya boleh memilih model khusus seperti autoglm. Menyediakan fungsi terjemahan pelbagai bahasa, menyokong penukaran bahasa kandungan teks. Disyorkan menggunakan model yang menyokong pelbagai bahasa. Digunakan untuk perancangan pencarian grep_context dan penapisan hasil (grep_code berbilang pusingan) + Semakan panggilan alat + Menyemak pematuhan panggilan alat fail secara automatik apabila mod Benarkan (suis mengikut zon) Digunakan untuk menentukan urutan bercakap dan strategi langkauan secara dinamik dalam kumpulan, menyokong perbincangan berbilang pusingan Digunakan untuk pengenalan dan analisis kandungan imej, memanggil alat read_file untuk membaca gambar dengan menyokong parameter intent untuk pemahaman mendalam. Disyorkan menggunakan model multimodal yang menyokong penglihatan. Digunakan untuk pengenalan dan analisis kandungan audio, memanggil alat read_file untuk membaca audio dengan menggunakan parameter intent untuk pemahaman mendalam (seperti transkripsi/ringkasan/pengenalan kandungan pertuturan). Disyorkan menggunakan model yang menyokong input audio atau transkripsi berkualiti tinggi. @@ -7750,4 +7752,17 @@ Kini anda boleh menggunakan mod AutoGLM di antara muka perbualan. Analisis trend Tetapan statistik OK + + + Penyulingan AI bacaan fail + Semasa membaca fail, AI menyuling kandungan berkaitan untuk menjimatkan konteks; jika tidak dikonfigurasi, gunakan model utama + Alat %1$s ditetapkan kepada terlarang dan laluan sasaran berada di luar ruang kerja sesi semasa (atau sesi tidak mengikat ruang kerja); panggilan ini ditolak. + ⚠ Pengesahan operasi luar ruang kerja: laluan sasaran berikut berada di luar ruang kerja sesi semasa (atau sesi tidak mengikat ruang kerja). Semak laluan dengan teliti sebelum membenarkan:\n%1$s + 🤖 Semakan AI mendapati panggilan ini tidak patuh: %1$s\nSahkan sama ada masih dibenarkan dilaksanakan. + ⚠ Semakan AI tidak dapat diselesaikan: %1$s\nSahkan secara manual sama ada panggilan ini dibenarkan dijalankan. + Semakan automatik panggilan alat + Semakan automatik alat fail (dalam ruang kerja) + Semakan automatik alat fail (luar ruang kerja) + Semakan automatik alat berisiko tinggi + Penyulingan AI untuk bacaan fail diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index e3334bc553..655b0c3b72 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -2561,6 +2561,8 @@ Agora é possível usar o modo AutoGLM na interface de diálogo. Usado para passagem e operação automática da interface UI. O modelo de controlador de UI correspondente só será habilitado quando o usuário solicitar explicitamente o uso do controle "inteligente". Por exemplo, modelos especiais como o autoglm podem ser selecionados. Fornece função de tradução multilíngue e suporta conversão de idioma de conteúdo de texto. Recomenda-se usar um modelo que suporte vários idiomas. Planejamento de pesquisa e filtragem de resultados para grep_context (várias rodadas de grep_code) + Revisão de chamadas de ferramentas + Revisa automaticamente a conformidade das chamadas de ferramentas de arquivos quando o modo é Permitir (chaves por zona) Usado para determinar dinamicamente a ordem de fala e a estratégia de pular desta rodada no grupo, apoiando múltiplas rodadas de discussões Usado para reconhecimento e análise de conteúdo de imagem. Ao chamar a ferramenta read_file para ler imagens, ela suporta parâmetros de intenção para uma compreensão aprofundada. Recomenda-se a utilização de modelos multimodais que apoiem a visão. Usado para reconhecimento e análise de conteúdo de áudio. Ao chamar a ferramenta read_file para ler áudio, você pode usar o parâmetro intent para compreensão aprofundada (como transliteração/resumo/identificação de conteúdo falado). Recomenda-se usar um modelo que suporte entrada de áudio ou transcrição de alta qualidade. @@ -7510,4 +7512,17 @@ Agora é possível usar o modo AutoGLM na interface de diálogo. Toque no gráfico para ver dados detalhados Tokens acumulados Pico de tokens + + + Destilação por IA de leitura de arquivos + Ao ler arquivos, a IA destila o conteúdo relevante para economizar contexto; sem configuração, usa o modelo principal + A ferramenta %1$s está definida como proibida e o caminho de destino está fora do workspace da sessão atual (ou a sessão não tem workspace vinculado); a chamada é recusada. + ⚠ Confirmação de operação fora do workspace: os seguintes caminhos de destino estão fora do workspace da sessão atual (ou a sessão não tem workspace vinculado). Revise os caminhos com cuidado antes de permitir:\n%1$s + 🤖 A revisão por IA considerou esta chamada fora de conformidade: %1$s\nConfirme se deseja permitir a execução mesmo assim. + ⚠ A revisão por IA não pôde ser concluída: %1$s\nConfirme manualmente se esta chamada pode ser executada. + Revisão automática de chamadas de ferramentas + Revisão automática de ferramentas de arquivo (dentro do workspace) + Revisão automática de ferramentas de arquivo (fora do workspace) + Revisão automática de ferramentas de alto risco + Destilação por IA para leitura de arquivos diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 8e7d0c9434..e231be734c 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -2968,6 +2968,8 @@ Acum poate fi utilizat în interfața de dialog AutoGLM A devenit un model.utilizat pentruUIParcurgerea și operațiunile automate ale interfeței, corespunzătoare UI Modelul de controlor este activat numai atunci când utilizatorul solicită în mod explicit utilizarea controlului de tip „agent inteligent”, de exemplu, opțional autoglm și alte modele specializate. Oferă funcții de traducere multilingvă, permițând conversia limbii conținutului text. Se recomandă utilizarea unui model care acceptă mai multe limbi. utilizat pentrugrep_contextPlanificarea căutării și filtrarea rezultatelor (în mai multe etape)grep_code) + Revizuire apeluri de instrumente + Revizuiește automat conformitatea apelurilor de instrumente pentru fișiere când modul este Permitere (comutatoare pe zonă) Se utilizează pentru a stabili în mod dinamic, în cadrul unui grup, ordinea de intervenție și strategia de omitere pentru runda curentă, fiind compatibil cu discuțiile pe mai multe runde Utilizat pentru recunoașterea și analiza conținutului imaginilor, apelareread_fileInstrumentul acceptă citirea imaginilorintentÎnțelegerea aprofundată a parametrilor. Se recomandă utilizarea unor modele multimodale care integrează informații vizuale. Utilizat pentru recunoașterea și analiza conținutului audio, apelread_fileInstrumentul poate fi utilizat atunci când se citește un fișier audiointentÎnțelegerea aprofundată a parametrilor (de exemplu, transcrierea/Concluzii/(Recunoașterea conținutului vorbirii). Se recomandă utilizarea unui model care acceptă intrare audio sau care oferă transcriere de înaltă calitate. @@ -8348,4 +8350,17 @@ Acum poate fi utilizat în interfața de dialog AutoGLM A devenit un model.Analiza tendințelor Setări statistici OK + + + Distilare AI a citirii fișierelor + La citirea fișierelor, AI-ul distilează conținutul relevant pentru a economisi contextul; dacă nu este configurat, se folosește modelul principal + Instrumentul %1$s este setat ca interzis, iar calea destinație este în afara spațiului de lucru al sesiunii curente (sau sesiunea nu are un spațiu de lucru legat); apelul este respins. + ⚠ Confirmare operațiune în afara spațiului de lucru: următoarele căi de destinație sunt în afara spațiului de lucru al sesiunii curente (sau sesiunea nu are un spațiu de lucru legat). Verificați căile cu atenție înainte de a permite:\n%1$s + 🤖 Recenzia AI consideră acest apel non-conform: %1$s\nConfirmați dacă permiteți în continuare executarea. + ⚠ Recenzia AI nu a putut fi finalizată: %1$s\nConfirmați manual dacă acest apel poate fi executat. + Recenzie automată a apelurilor de instrumente + Recenzie automată a instrumentelor de fișiere (în interiorul spațiului de lucru) + Recenzie automată a instrumentelor de fișiere (în afara spațiului de lucru) + Recenzie automată a instrumentelor cu risc ridicat + Distilare AI pentru citirea fișierelor diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 65bf4c9f56..3ee2ec792d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3142,6 +3142,10 @@ 用于UI界面的自动遍历与操作,对应的 UI 控制器模型仅在用户明确要求使用“智能体”控制时才会启用,例如可选择 autoglm 等专用模型。 提供多语言翻译功能,支持文本内容的语言转换。建议使用支持多语言的模型。 用于grep_context的检索规划与结果筛选(多轮grep_code) + 工具调用审查 + 允许模式下自动审查文件工具调用是否合规(工作区内/外可分别开关) + 文件读取提炼 + 读取文件时 AI 提炼相关内容以节省上下文;未配置时使用主模型 用于在群组中动态决定本轮发言顺序与跳过策略,支持多轮讨论 用于图像内容识别和分析,调用read_file工具读取图片时支持intent参数进行深度理解。建议使用支持视觉的多模态模型。 用于音频内容识别和分析,调用read_file工具读取音频时可使用intent参数进行深度理解(如转写/总结/识别说话内容)。建议使用支持音频输入或高质量转写的模型。 @@ -6312,11 +6316,11 @@ 列出所有角色卡 获取对话消息: %1$s%2$s%3$s 列出目录内容: %1$s%2$s - 读取文件: %1$s%2$s + 读取文件(默认 AI 提炼相关内容,需原文时 raw=true): %1$s%2$s 行 %1$s-%2$s 从行 %1$s 开始 - 读取文件 (%1$s): %2$s%3$s - 读取完整文件内容: %1$s%2$s + 读取文件(不确认行号时建议默认 AI 提炼读取,raw=true 按行段读)(%1$s): %2$s%3$s + 读取完整文件内容(文本默认 AI 提炼,raw=true 读原文): %1$s%2$s 读取二进制文件内容: %1$s%2$s 追加内容到文件 写入内容到文件 @@ -7946,6 +7950,19 @@ 需要悬浮窗权限才能确认工具执行。 工具执行确认超时。 + + 工具 %1$s 已被设置为禁止,且目标路径位于当前会话工作区之外(或会话未绑定工作区),本次调用直接拒绝。 + ⚠ 工作区外操作确认:以下目标路径位于当前会话工作区之外(或会话未绑定工作区),请仔细核对路径后决定是否允许:\n%1$s + + + 🤖 AI 审查认为本次调用不合规:%1$s\n请确认是否仍要允许执行。 + ⚠ AI 审查未能完成:%1$s\n请人工确认本次调用是否允许执行。 + 自动审查工具调用 + 自动审查文件工具(工作区内) + 自动审查文件工具(工作区外) + 自动审查高危工具 + 文件读取 AI 提炼 + 图片捕获失败: %1$s diff --git a/app/src/test/java/com/ai/assistance/operit/api/chat/enhance/FileBindingServiceTest.kt b/app/src/test/java/com/ai/assistance/operit/api/chat/enhance/FileBindingServiceTest.kt new file mode 100644 index 0000000000..82c68f5a55 --- /dev/null +++ b/app/src/test/java/com/ai/assistance/operit/api/chat/enhance/FileBindingServiceTest.kt @@ -0,0 +1,183 @@ +package com.ai.assistance.operit.api.chat.enhance + +import com.ai.assistance.operit.util.AppLogger +import kotlinx.coroutines.runBlocking +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.mockito.kotlin.mock + +/** + * FileBindingService 编辑匹配引擎的纯 JVM 单测。 + * 覆盖:精确快路径、行号前缀剥离、多候选拒绝、occurrence 定位、模糊匹配、无匹配诊断、delete。 + */ +class FileBindingServiceTest { + + private lateinit var service: FileBindingService + + @Before + fun setUp() { + // 修改意图:AppLogger 默认走 android.util.Log(JVM 单测中是会抛异常的 stub), + // 关闭系统日志与文件日志,使匹配器可纯 JVM 运行 + AppLogger.enableSystemLog = false + AppLogger.enableFileLogging = false + service = FileBindingService(mock()) + } + + private fun op( + old: String, + new: String = "", + action: FileBindingService.StructuredEditAction = + FileBindingService.StructuredEditAction.REPLACE, + occurrence: Int? = null, + ): FileBindingService.StructuredEditOperation = + FileBindingService.StructuredEditOperation( + action = action, + oldContent = old, + newContent = new, + occurrence = occurrence + ) + + @Test + fun `exact unique match replaces content and reports exact line range`() = runBlocking { + val original = "line one\nline two\nline three\n" + val result = service.processFileBindingOperations( + original, + listOf(op(old = "line two", new = "line TWO edited")) + ) + + assertFalse(result.diff.startsWith("Error")) + assertEquals("line one\nline TWO edited\nline three\n", result.mergedContent) + val range = result.appliedRanges.single() + assertTrue("range=$range", range.contains("lines 2-2")) + assertTrue("range=$range", range.contains("exact")) + } + + @Test + fun `line number prefixes pasted from read_file output are stripped before matching`() = + runBlocking { + val original = "fun a() {\n val x = 1\n}\n" + // 模拟模型把 read_file 输出整段粘贴进 old(行号宽度 1) + val oldWithPrefixes = "1| fun a() {\n2| val x = 1\n3| }" + + val result = service.processFileBindingOperations( + original, + listOf(op(old = oldWithPrefixes, new = "fun a() {\n val x = 2\n}")) + ) + + assertFalse(result.diff.startsWith("Error")) + assertEquals("fun a() {\n val x = 2\n}\n", result.mergedContent) + } + + @Test + fun `multiple match locations without occurrence are rejected with line numbers`() = + runBlocking { + val original = "aaa\nbbb\naaa\nbbb\n" + val result = service.processFileBindingOperations( + original, + listOf(op(old = "aaa", new = "AAA")) + ) + + assertTrue(result.diff.startsWith("Error")) + assertEquals(original, result.mergedContent) + assertTrue("diff=${result.diff}", result.diff.contains("2 distinct locations")) + assertTrue("diff=${result.diff}", result.diff.contains("lines 1-1")) + assertTrue("diff=${result.diff}", result.diff.contains("lines 3-3")) + assertTrue("diff=${result.diff}", result.diff.contains("occurrence")) + } + + @Test + fun `occurrence selects the Nth match location`() = runBlocking { + val original = "aaa\nbbb\naaa\nbbb\n" + val result = service.processFileBindingOperations( + original, + listOf(op(old = "aaa", new = "AAA", occurrence = 2)) + ) + + assertFalse(result.diff.startsWith("Error")) + assertEquals("aaa\nbbb\nAAA\nbbb\n", result.mergedContent) + assertTrue( + "range=${result.appliedRanges}", + result.appliedRanges.single().contains("lines 3-3") + ) + } + + @Test + fun `occurrence beyond candidate count is rejected`() = runBlocking { + val original = "aaa\nbbb\naaa\n" + val result = service.processFileBindingOperations( + original, + listOf(op(old = "aaa", new = "AAA", occurrence = 5)) + ) + + assertTrue(result.diff.startsWith("Error")) + assertEquals(original, result.mergedContent) + } + + @Test + fun `fuzzy match tolerates small content drift and reports fuzzy range`() = runBlocking { + // 修改意图:长块(20 行)中单字符漂移只影响极少数 3-gram,相似度能稳过 0.9 阈值; + // 短块单字符漂移的 Jaccard 会跌到阈值以下,不适合做模糊命中用例。 + // 评审补记:lambda 必须显式命名参数 i——隐式 it 与字符串模板里的 $i 对不上, + // 该文件此前从未通过编译(compileDebugKotlin 不编测试源) + val originalLines = (1..20).map { i -> " val line$i = $i" } + val original = "fun a() {\n" + originalLines.joinToString("\n") + "\n}\n" + + val oldLines = (1..20).map { i -> " val line$i = ${if (i == 7) "8" else "$i"}" } + val old = "fun a() {\n" + oldLines.joinToString("\n") + "\n}" + + val newLines = (1..20).map { i -> " val line$i = ${if (i == 7) "9" else "$i"}" } + val newContent = "fun a() {\n" + newLines.joinToString("\n") + "\n}" + + val result = service.processFileBindingOperations(original, listOf(op(old = old, new = newContent))) + + assertFalse("diff=${result.diff}", result.diff.startsWith("Error")) + // 多行替换(start != end)不做缩进沿用,new 内容按原样插入; + // 原文件的尾行换行符保留,故结果比 newContent 多一个结尾 "\n" + assertEquals(newContent + "\n", result.mergedContent) + assertTrue( + "range=${result.appliedRanges}", + result.appliedRanges.single().contains("fuzzy") + ) + } + + @Test + fun `unmatchable old block fails with diagnostic message`() = runBlocking { + val original = "alpha beta gamma\ndelta epsilon zeta\neta theta iota\n" + // 评审补记:new 必须非空——空 new 的 REPLACE 在校验阶段就被丢弃 + // ("No valid edit operations provided"),走不到无匹配诊断路径 + val result = service.processFileBindingOperations( + original, + listOf( + op( + old = "completely unrelated content that does not exist anywhere here", + new = "brand new content that replaces nothing real" + ) + ) + ) + + assertTrue(result.diff.startsWith("Error")) + assertEquals(original, result.mergedContent) + assertTrue("diff=${result.diff}", result.diff.contains("Could not find a match")) + assertTrue("diff=${result.diff}", result.diff.contains("read_file_part")) + } + + @Test + fun `delete action removes the matched block`() = runBlocking { + val original = "a\nb\nc\n" + val result = service.processFileBindingOperations( + original, + listOf( + op( + old = "b", + action = FileBindingService.StructuredEditAction.DELETE + ) + ) + ) + + assertFalse(result.diff.startsWith("Error")) + assertEquals("a\nc\n", result.mergedContent) + } +} diff --git a/app/src/test/java/com/ai/assistance/operit/core/tools/WorkspacePathCheckerTest.kt b/app/src/test/java/com/ai/assistance/operit/core/tools/WorkspacePathCheckerTest.kt new file mode 100644 index 0000000000..6f11e249a6 --- /dev/null +++ b/app/src/test/java/com/ai/assistance/operit/core/tools/WorkspacePathCheckerTest.kt @@ -0,0 +1,187 @@ +package com.ai.assistance.operit.core.tools + +import com.ai.assistance.operit.data.model.AITool +import com.ai.assistance.operit.data.model.ToolParameter +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** WorkspacePathChecker 工作区判定与危险路径硬底线的纯 JVM 单测 */ +class WorkspacePathCheckerTest { + + private fun tool(vararg params: Pair): AITool = + AITool(name = "write_file", parameters = params.map { ToolParameter(it.first, it.second) }) + + @Test + fun `chat without workspace binding treats every path as outside`() { + // 需求确认项:未绑定工作区时不存在可信区,所有路径按区外 + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = null, + chatWorkspaceEnv = null, + tool = tool("path" to "/data/ws/inner/file.txt"), + ) + assertEquals(WorkspacePathChecker.Zone.OUTSIDE, zone) + } + + @Test + fun `android path inside workspace root is inside`() { + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/user/0/app/files/workspace/chat-1", + chatWorkspaceEnv = null, + tool = tool("path" to "/data/user/0/app/files/workspace/chat-1/sub/file.txt"), + ) + assertEquals(WorkspacePathChecker.Zone.INSIDE, zone) + } + + @Test + fun `android path outside workspace root is outside`() { + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/user/0/app/files/workspace/chat-1", + chatWorkspaceEnv = null, + tool = tool("path" to "/data/user/0/app/files/workspace/chat-2/file.txt"), + ) + assertEquals(WorkspacePathChecker.Zone.OUTSIDE, zone) + } + + @Test + fun `sibling prefix that merely shares a path prefix is outside`() { + // /ws2 与 /ws 共享字符串前缀但不是目录子路径,必须判区外 + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = null, + tool = tool("path" to "/data/ws2/file.txt"), + ) + assertEquals(WorkspacePathChecker.Zone.OUTSIDE, zone) + } + + @Test + fun `linux tool environment matching workspace env is inside`() { + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = "linux", + tool = tool("path" to "/home/user/x.txt", "environment" to "linux"), + ) + assertEquals(WorkspacePathChecker.Zone.INSIDE, zone) + } + + @Test + fun `linux tool environment with android workspace env is outside`() { + val zone = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = null, + tool = tool("path" to "/home/user/x.txt", "environment" to "linux"), + ) + assertEquals(WorkspacePathChecker.Zone.OUTSIDE, zone) + } + + @Test + fun `same repo bookmark is inside and different bookmark is outside`() { + val same = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = "repo:docs", + tool = tool("path" to "/docs/a.txt", "environment" to "repo:docs"), + ) + assertEquals(WorkspacePathChecker.Zone.INSIDE, same) + + val different = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = "repo:docs", + tool = tool("path" to "/other/a.txt", "environment" to "repo:other"), + ) + assertEquals(WorkspacePathChecker.Zone.OUTSIDE, different) + } + + @Test + fun `multi path call is inside only when every path is inside`() { + val bothInside = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = null, + tool = tool("source" to "/data/ws/a.txt", "destination" to "/data/ws/b.txt"), + ) + assertEquals(WorkspacePathChecker.Zone.INSIDE, bothInside) + + val mixed = + WorkspacePathChecker.resolveZone( + chatWorkspaceRoot = "/data/ws", + chatWorkspaceEnv = null, + tool = tool("source" to "/data/ws/a.txt", "destination" to "/data/other/b.txt"), + ) + assertEquals(WorkspacePathChecker.Zone.OUTSIDE, mixed) + } + + @Test + fun `qualified package tool names share the protected tool family`() { + assertTrue(WorkspacePathChecker.isWorkspaceProtectedTool("copy_file")) + assertTrue(WorkspacePathChecker.isWorkspaceProtectedTool("extended_file_tools:copy_file")) + assertFalse(WorkspacePathChecker.isWorkspaceProtectedTool("github:get_file_content")) + assertFalse(WorkspacePathChecker.isWorkspaceProtectedTool("package_proxy")) + } + + @Test + fun `dangerous recursive delete roots are refused and normal paths are not`() { + listOf( + "/", + "/storage", + "/storage/emulated/0", + "/sdcard", + "/data", + "/home", + ).forEach { path -> + assertTrue("root=$path", WorkspacePathChecker.isDangerousRecursiveDeleteTarget(path)) + } + + listOf( + "/data/ws/file.txt", + "/storage/emulated/0/Download/a.zip", + "/home/user/project", + "/data/ws", + ).forEach { path -> + assertFalse("path=$path", WorkspacePathChecker.isDangerousRecursiveDeleteTarget(path)) + } + } + + @Test + fun `dangerous root spelling variants are refused`() { + // 评审 P1:这些变体与根集合的精确匹配不符,但指向同一目录, + // deleteRecursively 会真的清空——词法规范化必须全部命中。 + listOf( + "/sdcard/.", + "/sdcard//", + "//sdcard", + "/storage/emulated/0/.", + "/sdcard/..", + "/data/../data", + "/sdcard/sub/..", + "/storage/emulated/0/..", + "/etc/../etc", + ).forEach { path -> + assertTrue( + "variant=$path normalized=${WorkspacePathChecker.lexicallyNormalizePath(path)}", + WorkspacePathChecker.isDangerousRecursiveDeleteTarget(path), + ) + } + } + + @Test + fun `lexical normalization collapses dot and duplicate segments`() { + assertEquals("/", WorkspacePathChecker.lexicallyNormalizePath("/")) + assertEquals("/sdcard", WorkspacePathChecker.lexicallyNormalizePath("/sdcard/.")) + assertEquals("/sdcard", WorkspacePathChecker.lexicallyNormalizePath("//sdcard//")) + assertEquals("/", WorkspacePathChecker.lexicallyNormalizePath("/sdcard/..")) + assertEquals("/data", WorkspacePathChecker.lexicallyNormalizePath("/data/../data")) + assertEquals("/data/ws", WorkspacePathChecker.lexicallyNormalizePath("/data/ws/./x/..")) + assertEquals("", WorkspacePathChecker.lexicallyNormalizePath(" ")) + // 相对路径在根之外的 ".." 保留词法形态,不逃逸到上级目录 + assertEquals("../..", WorkspacePathChecker.lexicallyNormalizePath("a/../../a/../..")) + } +} diff --git a/app/src/test/java/com/ai/assistance/operit/data/model/FunctionTypeTest.kt b/app/src/test/java/com/ai/assistance/operit/data/model/FunctionTypeTest.kt index 45498caacf..79134796c4 100644 --- a/app/src/test/java/com/ai/assistance/operit/data/model/FunctionTypeTest.kt +++ b/app/src/test/java/com/ai/assistance/operit/data/model/FunctionTypeTest.kt @@ -23,9 +23,10 @@ class FunctionTypeTest { FunctionType.MEMORY, FunctionType.UI_CONTROLLER, FunctionType.TRANSLATION, FunctionType.GREP, FunctionType.ROLE_RESPONSE_PLANNER, FunctionType.IMAGE_RECOGNITION, FunctionType.AUDIO_RECOGNITION, - FunctionType.VIDEO_RECOGNITION, + FunctionType.VIDEO_RECOGNITION, FunctionType.TOOL_REVIEW, + FunctionType.FILE_DISTILL, ) - assertEquals(11, FunctionType.values().size) + assertEquals(13, FunctionType.values().size) for (type in expected) { assertTrue("Missing $type", FunctionType.values().contains(type)) } @@ -43,5 +44,7 @@ class FunctionTypeTest { assertEquals("IMAGE_RECOGNITION", FunctionType.IMAGE_RECOGNITION.name) assertEquals("AUDIO_RECOGNITION", FunctionType.AUDIO_RECOGNITION.name) assertEquals("VIDEO_RECOGNITION", FunctionType.VIDEO_RECOGNITION.name) + assertEquals("TOOL_REVIEW", FunctionType.TOOL_REVIEW.name) + assertEquals("FILE_DISTILL", FunctionType.FILE_DISTILL.name) } } diff --git a/avator/dragonbones/consumer-rules.pro b/avator/dragonbones/consumer-rules.pro new file mode 100644 index 0000000000..e69de29bb2 diff --git "a/docs/TODO/chat_file_tool_hardening/01-\346\217\217\350\277\260\350\247\204\350\214\203\345\214\226.md" "b/docs/TODO/chat_file_tool_hardening/01-\346\217\217\350\277\260\350\247\204\350\214\203\345\214\226.md" new file mode 100644 index 0000000000..b721ac76b6 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/01-\346\217\217\350\277\260\350\247\204\350\214\203\345\214\226.md" @@ -0,0 +1,25 @@ +# 01 描述规范化 + +## 旧实现 + +- 文件工具描述缺成功/失败结果格式说明;environment 描述与实现矩阵不符(read/write 系文档漏 repo:,grep 系虚报 repo:) +- read_file 截断未文档化、报错指向不存在的 `readFileFull`(SFS:1642);read_file_part 默认窗口文档 100 行 vs 实现 200 行(SystemToolPrompts.kt:148 / ToolExecutionLimits.kt:5) +- write_file 未写明默认覆盖与自动建父目录;整文件重写配方两处矛盾(apply_file 描述 delete→apply(create) vs 运行时报错 delete→write_file) +- create_file/edit_file 引用标准聊天不可见的 apply_file;copy_file recursive 文档 false / 实现 true +- extended_file_tools.js file_exists 把 exists=false 包装成失败(:93) + +## 意图修正 + +每个文件工具 details 增加标准小节:environment 真实矩阵 / 成功时返回(格式示例)/ 失败时返回(类别+建议动作)/ 与相邻工具分工(含"改文件不要走 shell"约定)。同步修正上述所有具体错误。 + +## 期待的新实现 + +模型从描述即可正确选择工具与参数、预知结果格式、失败后可按建议自纠错。 + +## 实施记录 + +- 已完成(commit 1c61b33e):EN/CN 可见 11 工具 + 内部 apply_file/内部文件工具四件套/扩展文件工具 8 项全部补 details(environment 真实矩阵、成功/失败结果格式、工具分工) +- 修正幽灵工具名 readFileFull→read_file_binary(SFS 与 SAF 双处);read_file_part 默认窗口文档 100→200(EN/CN/files.d.ts) +- copy_file recursive 缺省 true→false 与文档/JS 对齐,目录复制漏参明确报错 +- extended_file_tools.js 修复 file_exists 误报 + environment 描述对齐最终矩阵 +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/02-\350\257\273\345\217\226\344\270\212\351\231\220\344\270\216\345\201\245\345\243\256\346\200\247.md" "b/docs/TODO/chat_file_tool_hardening/02-\350\257\273\345\217\226\344\270\212\351\231\220\344\270\216\345\201\245\345\243\256\346\200\247.md" new file mode 100644 index 0000000000..610bf93ff2 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/02-\350\257\273\345\217\226\344\270\212\351\231\220\344\270\216\345\201\245\345\243\256\346\200\247.md" @@ -0,0 +1,30 @@ +# 02 读取上限与健壮性 + +## 旧实现 + +- read_file_full / read_file_binary 无大小上限(OOM 后报 error: null);read_file 32KB 字符截断未文档化 +- read_file_part 无 countFileLines 全量读(大文件全量 IO);无 isTextLike 门禁(二进制返回乱码当成功) +- 特殊文件 OCR/解码失败返回 success=true(错误文本当内容) +- read_file 截断标记按字节判断按字符读取(SFS:1653 误判) +- read_file_binary 的 base64 被 toString() 摘要化,内容到不了模型 +- FileApplyResultData.toString() 转义链为无效恒等替换(ToolResultDataClasses.kt:579) + +## 意图修正 + +- ToolExecutionLimits 新增 MAX_FILE_READ_SIZE_BYTES = 32MB;read_file / read_file_full / read_file_binary 三环境实现入口检查(含特殊文件抽取路径),超限失败并引导 "Use read_file_part with start_line/end_line to read it in segments." +- read_file_part 改流式按行读取(去 countFileLines,end_line 缺省读到文件尾),保留 32k 字符输出截断 +- read_file_part 补 isTextLike 门禁;特殊文件提取失败改 success=false +- 修 bytes-vs-chars 误判;read_file_binary base64 进入模型可见结果(超 64k 单条上限时明确报错并给替代路径) +- 修无效转义链 + +## 期待的新实现 + +大文件可安全分段读取;二进制/损坏文件得到明确失败;二进制读写可完成往返。 + +## 实施记录 + +- 已完成(commit a87e006f):MAX_FILE_READ_SIZE_BYTES=32MB 在 android/linux/saf 三环境 read_file/read_file_full/read_file_binary 入口统一拦截,超限报错引导 read_file_part 分段读取 +- readFilePart 改单次流式扫描(删除 countFileLines/readLinesFromFile 双次全量 IO);start_line 超界明确报错并告知总行数;补 isTextLike 门禁;totalLines 未知时结果明示续读方式 +- read_file 截断判定改为实际多读一字符判定(修复多字节编码误标);图片 OCR 解码失败/异常改 success=false +- read_file_binary base64 进入模型可见结果;超单条消息预算显式拒绝并给替代路径;FileApplyResultData 无效转义清理 +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/03-\347\274\226\350\276\221\345\274\225\346\223\216\351\207\215\346\236\204.md" "b/docs/TODO/chat_file_tool_hardening/03-\347\274\226\350\276\221\345\274\225\346\223\216\351\207\215\346\236\204.md" new file mode 100644 index 0000000000..26e3bd641b --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/03-\347\274\226\350\276\221\345\274\225\346\223\216\351\207\215\346\236\204.md" @@ -0,0 +1,26 @@ +# 03 编辑引擎重构 + +## 旧实现 + +FileBindingService.kt:无精确匹配快路径(唯一"完美"判定也在滑动窗内);0.9 阈值多候选不拒绝、静默取最优窗替换(仅完美重复才中止,:280-283/:666);read_file 的 `N| ` 行号前缀污染匹配;每 op 匹配行号算出即弃(:284);[START-REPLACE] 块解析为死代码(无生产调用者)。 + +## 意图修正 + +1. hasMultiplePerfectMatches 重构为 (count, positions) 扫描器;findBestMatchRange 顶部归一化精确 indexOf,唯一命中直接定位 +2. 并行搜索统计 >0.9 窗口,非完美多候选 ≥2 → 失败并报各候选拿的行号+相似度,提示加上下文或用 occurrence +3. applyFile 入口对 old 剥离 `^\s*\d+\| `(MULTILINE) +4. 新增 occurrence 可选参数(默认 1);ToolRegistration 同步 +5. 成功结果含 "Replaced at lines X-Y";失败附最佳候选拿的行号+相似度 +6. 描述写明匹配语义与重试策略;删除死代码解析路径 +7. 新增 JUnit 五组用例 + +## 期待的新实现 + +edit_file 首次成功率显著提升;失败报错可指导模型精读目标行后重试,不再退化为终端脚本。 + +## 实施记录 + +- 已完成(commit d86ecfb9):FileBindingService 整体重构——归一化精确匹配快速路径、>0.9 窗口按行区间重叠聚类为独立位置、多候选未指定 occurrence 一律拒绝并列出候选行号+相似度、occurrence 参数(apply_file/edit_file 双入口)、old 入口剥离 N| 行号前缀、成功回传匹配行号(Applied at: lines X-Y / matchedRanges)、失败附最佳候选诊断并明确提示勿退化为 shell +- 删除死代码:[START-REPLACE] 解析链(processFileBinding/applyFuzzyPatch/parseEditOperations/EDIT_BLOCK_REGEX)、hasMultiplePerfectMatches +- 新增 FileBindingServiceTest 八个用例(精确/行号剥离/多候选拒绝/occurrence 定位与越界/模糊长块/无匹配诊断/delete) +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/04-\345\267\245\344\275\234\345\214\272\351\230\262\346\212\244.md" "b/docs/TODO/chat_file_tool_hardening/04-\345\267\245\344\275\234\345\214\272\351\230\262\346\212\244.md" new file mode 100644 index 0000000000..7acbe3a1f6 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/04-\345\267\245\344\275\234\345\214\272\351\230\262\346\212\244.md" @@ -0,0 +1,28 @@ +# 04 工作区防护 + +## 旧实现 + +文件工具对任意绝对路径直接执行(PathValidator 只查绝对前缀);权限检查按工具名,无路径维度;JS 包嵌套调用绕过 ToolExecutionManager;delete_file recursive 对 / 等根目录零防护。 + +## 意图修正 + +- 根解析:SendMessageOptions.workspacePath 传入 executeInvocations + 按 chatId 查 ChatHistory.workspace 为准;未绑定工作区 → 所有路径按区外 +- 新 WorkspacePathChecker(纯函数可测):canonicalPath 前缀比较;工具 env 与 chat workspaceEnv 错配 → 区外;linux 经 PathMapper;repo: 按 bookmark +- 行为矩阵(checkToolPermission 判定 stage):区内 ALLOW 直执行 / ASK 弹窗 / FORBID 拒绝;区外 ALLOW 强制弹窗 / ASK 弹窗 / FORBID 直接拒绝 +- 嵌套兜底:ToolRuntimeContext 扩展携带 chatId/workspaceRoot;AIToolHook 对 JS 包/package_proxy 嵌套文件工具同样判定,区外拒绝回传理由(hook 无 UI) +- delete_file 危险路径硬底线:拒绝 /、/storage/emulated/0、/sdcard、/data 等根级目录 recursive 删除 + +## 期待的新实现 + +工作区内流畅、区外必经用户把关;未绑定工作区默认全防护;危险根目录删除被硬拦截。 + +## 实施记录 + +- 已完成(commit 6e1d11f4):新增 WorkspacePathChecker(24 工具族清单 + 判定器 + 危险根目录清单);未绑定工作区 → 全部按区外(需求确认项) +- checkToolPermission 工作区阶段:区外 ALLOW/ASK 强制弹窗(原因列路径)、FORBID 直接拒绝;判定先于 deny_tool,不可被绕过;扩展包限定名(pkg:copy_file)同族纳管 +- 执行前按 chatId 查 ChatHistory.workspace/workspaceEnv(ChatHistoryManager.getChatWorkspaceBinding) +- ToolPermissionSystem.requestConfirmation(reason):无视允许模式强制弹窗入口 +- 新增 WorkspaceFileToolHook:JS 包 toolCall → executeTool 嵌套调用经 ToolRuntimeContext ThreadLocal 判定,区外拒绝回传理由;用户确认过的区外路径记录于 approvedOutsidePaths,嵌套重入不误拦 +- delete_file:protected roots 递归删除在 android/linux 双环境硬拦截 +- 新增 WorkspacePathCheckerTest 九个用例(含无工作区、兄弟前缀、多路径、限定名、危险根) +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/05-\345\244\247\346\250\241\345\236\213\345\256\241\346\237\245.md" "b/docs/TODO/chat_file_tool_hardening/05-\345\244\247\346\250\241\345\236\213\345\256\241\346\237\245.md" new file mode 100644 index 0000000000..5745afc134 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/05-\345\244\247\346\250\241\345\236\213\345\256\241\346\237\245.md" @@ -0,0 +1,30 @@ +# 05 大模型审查 + +## 旧实现 + +无工具审查机制;功能模型体系已有 FunctionType(GREP 等)+ FunctionalConfigManager 映射 + 功能模型配置界面(卡片按枚举自动生成)+ callFunctionModel 调用范式;会话工具菜单只有 ALLOW/ASK/FORBID 分段控件,无审查开关。 + +## 意图修正 + +1. FunctionType.TOOL_REVIEW + FunctionTypeTest(11→12)+ strings(values 与 8 个 locale)+ FunctionalConfigScreen 显示名/描述两个 when 分支(卡片自动生成);缺省映射回落主模型 +2. ApiPreferences 加 tool_review_inside_workspace / tool_review_outside_workspace(默认 false)+ ApiConfigDelegate StateFlow/toggle +3. 开关 UI:AgentChatInputSection 工具组展开区加两行开关,仅 permissionLevel==ALLOW 显示;ClassicChatSettingsBar 镜像;AIChatScreen 接线 +4. 审查执行(checkToolPermission,仅文件工具族 + ALLOW + 对应区开关开):callFunctionModel(TOOL_REVIEW) withTimeout 15s;FunctionalPrompts.toolReviewPrompt(工具名/操作类别/路径/区内外/工作区根/内容参数摘要 500 字符);lenient 解析 {compliant, reason} + - 合规 → 直接执行(不弹窗) + - 不合规 → 弹窗,"AI 审查意见"区块显示 reason,用户允许则执行、拒绝则 deny + - 审查失败/超时 → 弹窗原询问窗口并提示审查错误信息,用户决定 + - 审查阶段 ToolProgressBus 显示"AI 审查中…" +5. ASK/FORBID 不跑审查;开关不显示 + +## 期待的新实现 + +允许模式下文件工具调用按区经大模型合规初筛;用户只看到被标记可疑的调用。 + +## 实施记录 + +- 已完成(commit d589d610):FunctionType.TOOL_REVIEW + 测试(11→12)+ 8 locale 文案 + 功能模型配置卡片自动生成;缺省回落主模型 +- ApiPreferences 双开关键 + Flow + 保存;ApiConfigDelegate/ChatViewModel 透出 +- FunctionalPrompts.toolReviewPrompt + ToolReviewService(callFunctionModel + 15s 超时 + 宽松 JSON 解析) +- checkToolPermission 审查流(文件工具族 + 允许模式 + 对应区开关):合规 → 直接执行不弹窗(区外亦放行,需求确认项);不合规 → 弹窗展示理由由用户决定;失败/超时 → 弹窗提示错误由用户决定;审查阶段 ToolProgressBus 显示"AI reviewing" +- UI:Agent/Classic 两种输入栏工具组新增区内/区外开关行,仅允许模式显示 +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/06-\347\216\257\345\242\203\347\237\251\351\230\265\344\270\216\346\211\251\345\261\225\351\200\232\351\201\223.md" "b/docs/TODO/chat_file_tool_hardening/06-\347\216\257\345\242\203\347\237\251\351\230\265\344\270\216\346\211\251\345\261\225\351\200\232\351\201\223.md" new file mode 100644 index 0000000000..04cd454572 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/06-\347\216\257\345\242\203\347\237\251\351\230\265\344\270\216\346\211\251\345\261\225\351\200\232\351\201\223.md" @@ -0,0 +1,25 @@ +# 06 环境矩阵与扩展通道 + +## 旧实现 + +repo: 环境下 zip/unzip/open/share/apply/create/edit/move/copy 无 guard,静默落 Android FS 报"不存在";linux 环境无结构化编辑;grep 的 linux/SSH 路径直接喂 Android FS 上的 native ripgrep;SAF 写白名单报错不列扩展名;禁用包调用报 "Tool not found" 而非"未启用"。 + +## 意图修正 + +- repo: 环境 clean guard:zip/unzip/open/share/apply/create/edit/move/copy 入口返回 "X is not supported for repo environment" +- linux 环境编辑实装:linuxTools.readFileFull → FileBindingService → linuxTools.writeFile +- grep_code/grep_context linux 路径经 PathMapper 映射后跑 native ripgrep +- SAF 白名单报错列出支持的扩展名 +- AIToolHandler.getToolExecutorOrActivate 回传"包未启用"真实原因 +- 与 01 阶段的 environment 文档矩阵保持一致 + +## 期待的新实现 + +跨环境行为可预测:不支持即明确报错(可自纠),支持则行为与文档一致。 + +## 实施记录 + +- 已完成(commit a52327cb):repo: 环境 zip/unzip/open/share/apply 五项 clean guard;applyFile 路径校验按环境路由(linux 支持 ~);edit_file 补 occurrence 透传与 schema +- grep_code linux 环境经 PathMapper 映射到宿主 rootfs 扫描、结果反向映射回 linux 路径;PathMapper 新增 mapAndroidPathToLinux +- SAF 写白名单报错列出全部支持扩展名;禁用包调用回传 usePackage 真实原因(不再笼统 Tool not found) +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/07-\351\253\230\345\215\261\345\267\245\345\205\267\345\256\241\346\237\245.md" "b/docs/TODO/chat_file_tool_hardening/07-\351\253\230\345\215\261\345\267\245\345\205\267\345\256\241\346\237\245.md" new file mode 100644 index 0000000000..45dd5a73c0 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/07-\351\253\230\345\215\261\345\267\245\345\205\267\345\256\241\346\237\245.md" @@ -0,0 +1,50 @@ +# 07 高危工具审查(工具调用审查泛化) + +## 旧实现 + +05 的 TOOL_REVIEW 审查只覆盖 24 个文件工具(`WorkspacePathChecker.WORKSPACE_PROTECTED_FILE_TOOLS`),开关只有区内/区外两个分区;shell/终端、Intent/广播、安装卸载、HTTP 请求、代理类等高危工具调用在允许模式下不经任何模型初筛直接执行。审查 prompt 亦为文件专用("FILE tool call"),参数摘要只取 content/new/base64Content。该功能未随任何发布版本发出,本步骤按"未发布直接改"处理,不做旧语义兼容。 + +## 意图修正 + +1. 新增 `core/tools/HighRiskToolCatalog.kt`:18 个核心破坏集高危工具 + 每工具英文操作摘要 + 限定名后缀匹配(与 `isWorkspaceProtectedTool` 同模式,扩展包同名工具一并纳入) +2. `ToolExecutionManager.checkToolPermission` 在工作区块之后并列新增高危分支(`else if`):高危工具与文件工具两集合不相交,`ALLOW` + 「自动审查高危工具」开关开 → 跑审查;开关关 / ASK / FORBID 落到标准流程 +3. 审查确认流提取共用 `confirmAfterReview`(文件路径 NonCompliant/Failed 两分支与高危分支共用):拒绝 → notify+错误结果;放行 → recordApprovedOutsidePaths(非文件工具传空列表无副作用)+ notify 放行 +4. `ToolReviewService` 泛化:`zone` 参数改为调用方传入 scope 标签(文件=区内/区外文案,高危="high-risk tool call (not workspace-scoped)");operationSummary 先查 HighRiskToolCatalog 再落文件族映射;参数摘要从 content 类单参数扩展为全参数 name=value 汇总(单值/总量各截 500) +5. `FunctionalPrompts.toolReviewPrompt` 泛化:标题 "tool call"、Scope 字段、规则扩充(命令/代码不得破坏系统、应用与系统管理需任务明确需要、网络不得外发用户数据、代理调用按目标工具判定) +6. ApiPreferences 第三开关键 `tool_review_high_risk_tools`(默认 false)+ Flow + 保存;区内/区外两 key 原样保留 +7. UI 三开关:Agent/Classic 工具组展开区加小标题「自动审查工具调用」+ 三行开关(区内/区外/高危工具),仅允许模式显示;strings 仅加 values/(与既有两条一致);ApiConfigDelegate/ChatViewModel/AIChatScreen 管线镜像透传 + +## 高危工具清单(18 个,用户确认的核心破坏集) + +| 类别 | 工具 | +|---|---| +| 命令/代码执行 | execute_shell、execute_in_terminal_session、execute_in_terminal_session_streaming、execute_hidden_terminal_command、input_in_terminal_session(stdin 可驱动 REPL)、execute_sandbox_script_direct | +| Intent/广播 | execute_intent、send_broadcast | +| 应用与系统 | install_app、uninstall_app、stop_app、modify_system_setting | +| 网络外发 | http_request、multipart_request | +| 代理(任意工具执行入口) | use_package、package_proxy、proxy | +| 编排 | trigger_workflow | + +## 行为决策(用户确认) + +- 该 TOOL_REVIEW 功能未发布 → 直接改,不做向前兼容 +- 开关分区 → 三开关:区内文件 / 区外文件 / 高危工具(互相独立) +- 高危范围 → 核心破坏集 18 个(不含 UI 自动化、隐私读取、记忆/角色卡/模型配置写、浏览器操作等,后续可再扩) + +## 追加:调用说明参数与审查上下文一致性(同日第二轮) + +1. **call_purpose 调用说明参数(可选)**:可审查工具(`HighRiskToolCatalog.REVIEWABLE_TOOL_LOCAL_NAMES` = 18 高危 + 24 文件族)可附带一句话声明"为什么发起本次调用" + - 文档注入两路径:文本提示词在 `SystemToolPrompts.buildToolPromptCategories`(hook 管线 Map 往返会丢上游注入,须在重建点注入);function-calling schema 在 `EnhancedAIService.getAvailableToolsForFunction` 尾部。共用 `HighRiskToolCatalog.callPurposeParamSchema()`(required=false,中英双语描述) + - 参数仅供审查流读取;执行前 `ToolExecutionManager.withoutCallPurposeParam()` 从 `invocation.tool` 剥离(JS 包与代理解析会拒绝未知参数;rawText 保留原始 XML 供聊天记录与历史回传) +2. **审查上下文**:`ToolReviewService.buildReviewContext` 直读 `ChatHistoryManager.loadChatMessages`(数据库全量历史,不经运行态压缩摘要) + - 最近一条用户指令:全量历史中最后一条 `sender=="user"` 的消息(截 800 字符)——真实指令,不受上下文压缩影响 + - 调用点前 10 条:最近 10 条 `sender=="ai"` 消息(其内容含该轮工具调用),剔除 `tool_result` 结果块、压缩空白、单条截 300 字符,旧→新编号 + - 无 callerChatId(悬浮球等无会话来源)时传空串,审查按无上下文判定 +3. **审查 prompt**:新增 Declared call purpose / Latest user instruction / Recent context 三段 + 一致性规则(调用须是最近用户指令与前文上下文的连贯延续;声明目的与上下文矛盾 → 不合规;未提供 call_purpose 本身不构成违规) +4. `checkToolPermission` 增加 `callerChatId` 参数(唯一调用方 executeInvocations 传入),透传至两处 review 调用 + +## 实施记录 + +- 已完成:HighRiskToolCatalog 新建;ToolExecutionManager 高危分支 + confirmAfterReview 提取;ToolReviewService/FunctionalPrompts 泛化;ApiPreferences/ApiConfigDelegate/ChatViewModel/AIChatScreen/AgentChatInputSection/ClassicChatSettingsBar 三开关管线;strings 两条新增;上述各文件"文件工具自动审查"注释同步更新为"工具调用自动审查"语义 +- 已完成(第二轮):call_purpose 文档注入两路径 + 执行前剥离;审查上下文(前 10 条模型消息 + 最近用户指令)与一致性规则;checkToolPermission 透传 callerChatId +- 状态:待编译验证 diff --git "a/docs/TODO/chat_file_tool_hardening/08-AI\350\257\273\345\217\226\346\217\220\347\202\274.md" "b/docs/TODO/chat_file_tool_hardening/08-AI\350\257\273\345\217\226\346\217\220\347\202\274.md" new file mode 100644 index 0000000000..fc37a355b5 --- /dev/null +++ "b/docs/TODO/chat_file_tool_hardening/08-AI\350\257\273\345\217\226\346\217\220\347\202\274.md" @@ -0,0 +1,40 @@ +# 08 AI 读取提炼 + +## 旧实现 + +- read_file / read_file_part / read_file_full 原样返回文件内容(带行号),read_file 单次 32KB 字符截断;长文件读取占用大量上下文 +- read_file_part 需模型自备行号,行号未知时只能先读再猜,二跳成本高 +- 功能模型范式(FunctionType + FunctionalConfigScreen 枚举驱动 + 未配置默认主模型)已由 TOOL_REVIEW 建立并验证 + +## 意图修正 + +- FunctionType 新增 FILE_DISTILL;FunctionalConfigScreen 补三处穷尽 when(显示名/描述/连接测试)+ 新字符串,未配置时经既有 getConfigMappingForFunction 返回 DEFAULT_CONFIG_ID,即默认跟随主模型 +- ApiPreferences 新增 FILE_DISTILL_ENABLED 开关(默认开),UI 置于工具审查三开关同区域;开关关闭时三工具行为与现状完全一致 +- 新建 FileDistillService:单次协议(一次调用完成 related/worth 判断 + 带行号摘录)与扫描协议(read_file_part / 大文件 full 逐块滚动,每轮 related + 摘录 + continue_scan,块约 24KB,上限 8 轮);worth=false(≥50% 相关)直接返回原文;扫描结束摘录占比过高同样返回原文 +- ToolRegistration 在 read_file / read_file_part / read_file_full 的 executor 外包提炼拦截(不动 5 个权限子类实现):开关关 → 透传;isSpecialFileType(图片/PDF/文档/音视频 OCR 抽取管线)→ 透传;raw=true → 透传;文件 ≤2048 字节 → 透传原文;其余进提炼 +- 新参数:raw(默认 false,true 读原始内容)、focus(AI 模式必填,缺失报错要求补传);失败(超时/异常/JSON 不可解析)一律显式失败 ToolResult,error 指引 raw=true 重读,不降级 +- 三工具描述注明:除非确认要读原始内容,否则使用默认 AI 提炼;read_file_part 额外注明不确认行号时省略行号参数用 AI 读取自动定位 +- 工具描述与 UI 字符串多语言同步 + +## 期待的新实现 + +默认读取返回 AI 提炼后的带行号相关摘录,上下文占用大幅下降;主模型可继续用摘录中的行号做 edit_file;需要精确原文时 raw=true 一次到位。 + +## 实施记录 + +- 已完成(待编译验证): + - `FunctionType.FILE_DISTILL` 新枚举;`FunctionalConfigScreen` 连接测试/显示名/描述三处 when 补齐(编译器强制),`FunctionTestDisplay.fromResponse` 走既有 else 分支无需改 + - `ApiPreferences.FILE_DISTILL_ENABLED`(默认 true)+ Flow/存取;`ApiConfigDelegate` StateFlow/collect/save;`ChatViewModel` 透传;`AIChatScreen` 两处输入调用点 + 顶层 collect;`AgentChatInputSection` 顶层与 ExtraSettingsPopup 两层签名 + `AgentToolsPermissionGroupItem` 渲染行(审查三开关之下,允许模式显示;`agent_menu_file_distill` 仅中英——`agent_menu_tool_review_*` 系列本就只有中文,英文 fallback 属既有状态) + - `ToolExecutionLimits` 新常量:MIN_BYTES=2048 / SINGLE_INPUT_BYTES=64000 / SCAN_CHUNK_CHARS=24000 / SCAN_MAX_ROUNDS=8 / EXCERPT_RATIO_LIMIT=0.6 / 单次 30s / 每轮 20s + - `FunctionalPrompts` 新增单次协议(related/worth/excerpt/file_summary 一次输出)与扫描单轮(related/excerpt/continue_scan)两个 prompt;行号格式与实现层 "N| " 一致 + - 新建 `api/chat/enhance/FileDistillService`:三变体(READ_FILE 先原始读取后提炼 / READ_FULL 纯文本自读 ≤64KB 单次否则扫描 / READ_PART 全文扫描 start_line/end_line 转可选范围限定);透传边界(开关/raw/repo:·linux/特殊类型/≤2048B/门禁错误复用 rawRead 报错);单次与扫描共用 readNumberedChunk;worth=false 或摘录占比 >0.6 返回原文;失败(超时/异常/解析)一律显式失败 + raw=true 指引;摘录预算 24K/份、扫描总量受 64K 单条结果预算约束 + - `ToolRegistration` 三工具 executor 接入拦截(5 个权限子类零改动);`StandardFileSystemTools.SPECIAL_FILE_EXTENSIONS` 可见性 protected→public + isSpecialFileExtension 助手(提炼层复用同一清单) + - `SystemToolPrompts`(read_file/read_file_part)与 `SystemToolPromptsInternal`(read_file_full 中英两份)补 raw/focus 参数与 AI 提炼 details;`values`/`values-en` 三个 toolreg 短描述 + function_type/desc_file_distill 新 key +- 行为保持不变的路径:图片/PDF/文档/音视频读取(三工具特殊类型一律透传)、read_file_binary、raw=true、≤2048 字节文件、linux/repo: 环境 +- 决策记录:开关 UI 置于允许模式分支内(字面服从"审查开关之下");悬浮窗等其余 AgentChatInputSection 调用点靠默认参数编译通过,菜单不显示开关但功能默认开 +- 静态冒烟修正(2026-11-08 首轮编译,5 报错 3 根因): + 1. `fileDistillSinglePrompt` 内 `$contentTruncationNote` 笔误 → `$truncatedNote`(字符串模板吞标识符报 Unresolved) + 2. `rawRead` lambda 类型 `() -> ToolResult` 包不住 suspend 的 readFile 调用 → 4 处签名改 `suspend () -> ToolResult` + 3. classic 风格的 `ClassicChatSettingsBar` 是审查开关的平行链路(AIChatScreen 992 调用点),签名 + 渲染行补齐——教训:开关链有 Agent(三层透传)与 Classic(独立组件)两条平行路径,改开关参数两边都要补 +- 状态:待编译验证 + diff --git a/docs/TODO/chat_file_tool_hardening/index.md b/docs/TODO/chat_file_tool_hardening/index.md new file mode 100644 index 0000000000..99ec3e91e1 --- /dev/null +++ b/docs/TODO/chat_file_tool_hardening/index.md @@ -0,0 +1,67 @@ +--- +title: 文件工具强化(描述规范化/32MB 上限/编辑引擎重构/工作区防护/大模型审查/AI 读取提炼) +repo: https://github.com/luojiaping/Operit +upstream: https://github.com/AAswordman/Operit +status: impl-complete-pending-compile +--- + +# 文件工具强化 + +## 原本状况 + +静态审计确认的失败机制:提示词与实现不对齐(32KB 截断未文档化、行号前缀污染编辑匹配、environment 支持矩阵全面失实);edit_file/apply_file 模糊匹配缺陷(无精确快路径、0.9 相似度多候选不拒绝而静默取最优、报错指向不存在的工具名),失败率高导致模型改用终端脚本改文件;read_file_binary 内容到不了模型、read_file_part 无二进制门禁、特殊文件提取失败报成功;工作区零防护(任意绝对路径直接执行,删除无危险路径底线);无工具审查机制。 + +## 意图 + +- A 描述规范化:每个文件工具补充 environment 真实矩阵 / 成功返回格式 / 失败类别与建议动作 / 工具分工;修正误导性文案与报错 +- B 读取上限:32MB 硬上限(超出引导 read_file_part 分段),read_file_part 流式化,读取类健壮性修复 +- C 编辑引擎:精确匹配快路径 + 多候选拒绝 + 行号前缀剥离 + occurrence 参数 + 匹配行号回传 +- D 工作区防护:区内按既有权限模式;区外 ALLOW 强制弹窗、FORBID 直接拒绝;未绑定工作区一律按区外;delete_file 危险路径硬底线;JS 包嵌套调用 AIToolHook 兜底 +- E 大模型审查:FunctionType.TOOL_REVIEW + 功能模型配置 + 会话工具菜单自动审查三开关(区内/区外文件 + 高危工具,仅允许模式显示)+ 审查流(合规直接执行 / 不合规弹窗用户决定 / 审查失败弹窗提示错误) +- H AI 读取提炼:read_file/read_file_part/read_file_full 默认经功能模型提炼返回带行号相关摘录(特殊类型抽取管线透传、≤2048 字节透传、raw=true 透传),ApiPreferences 开关默认开,FILE_DISTILL 功能模型未配置默认主模型 +- G 高危工具审查:审查范围从文件工具族泛化为 HighRiskToolCatalog 高危工具调用(核心破坏集 18 个),三开关之「自动审查高危工具」独立管辖 +- F 环境矩阵:repo: 环境 clean guard、linux 编辑实装、grep linux 路径映射、SAF 白名单报错、禁用包报错修正 + +## 期待结果 + +- 文件工具首次成功率与自纠错率显著提升(描述可自解释、报错可操作) +- 工作区外/未绑定工作区的文件操作全部经用户确认或审查把关,禁止模式直接拒绝 +- edit_file 不再需要模型改用终端脚本兜底 + +## 作用域 + +- `core/config/SystemToolPrompts.kt`、`core/config/SystemToolPromptsInternal.kt` +- `core/tools/ToolExecutionLimits.kt`、`core/tools/ToolResultDataClasses.kt`、`core/tools/ToolRegistration.kt`、`core/tools/AIToolHandler.kt` +- `core/tools/defaultTool/standard/StandardFileSystemTools.kt`、`SafFileSystemTools.kt`、`LinuxFileSystemTools.kt`、`defaultTool/PathValidator.kt` +- `api/chat/enhance/FileBindingService.kt`、`api/chat/enhance/ToolExecutionManager.kt`、`api/chat/enhance/ConversationMarkupManager.kt`、`api/chat/EnhancedAIService.kt` +- `data/model/FunctionType.kt`、`data/preferences/ApiPreferences.kt`、`services/core/ApiConfigDelegate.kt` +- `ui/features/settings/screens/FunctionalConfigScreen.kt`、`ui/permissions/ToolPermissionSystem.kt`、`ui/permissions/PermissionRequestOverlay.kt` +- `ui/features/chat/components/style/input/agent/AgentChatInputSection.kt`、`.../classic/ClassicChatSettingsBar.kt`、`ui/features/chat/screens/AIChatScreen.kt` +- `assets/packages/extended_file_tools.js`、`examples/types/files.d.ts` +- 新增:`core/tools/WorkspacePathChecker.kt`、`core/tools/WorkspaceFileToolHook`(AIToolHook 实现)、`core/config/FunctionalPrompts.kt` 审查 prompt、FileBindingService 单测、WorkspacePathChecker 单测 + +## 行为决策(用户确认) + +- 审查不合规 → 弹窗展示审查理由,用户决定放行与否 +- 审查调用失败 → 弹出原询问窗口并在窗口提示错误信息 +- 审查通过 → 直接执行不弹窗(区内区外一致) +- 未绑定工作区 → 所有路径按工作区外对待 + +## 步骤 + +- [01-描述规范化.md](01-描述规范化.md) +- [02-读取上限与健壮性.md](02-读取上限与健壮性.md) +- [03-编辑引擎重构.md](03-编辑引擎重构.md) +- [04-工作区防护.md](04-工作区防护.md) +- [05-大模型审查.md](05-大模型审查.md) +- [06-环境矩阵与扩展通道.md](06-环境矩阵与扩展通道.md) +- [07-高危工具审查.md](07-高危工具审查.md) +- [08-AI读取提炼.md](08-AI读取提炼.md) + +## 验证 + +- 用户运行 `:app:compileDebugKotlin` 迭代修复 +- 新增 JUnit:FileBindingService 匹配器五组用例、WorkspacePathChecker(含无工作区用例) +- 手工回归矩阵:区内/区外/无工作区 × 允许/询问/禁止 × 审查开/关 × 合规/不合规/失败 +- 手工回归矩阵(高危工具):execute_shell 等核心破坏集 × 允许 × 高危开关开/关 × 合规/不合规/失败;三开关互相独立(仅开高危开关不影响文件工具区外强制弹窗) +- 手工用例:32MB 大文件读取、read_file_part 分页、edit_file 行号粘贴/多候选/occurrence、扩展包 file_exists、禁用包报错、repo: guard diff --git a/examples/types/files.d.ts b/examples/types/files.d.ts index 5b218f53d5..6c64fcc4d9 100644 --- a/examples/types/files.d.ts +++ b/examples/types/files.d.ts @@ -47,7 +47,7 @@ export namespace Files { * Read file content by line range * @param path - Path to file * @param startLine - Starting line number (1-indexed, default 1) - * @param endLine - Ending line number (1-indexed, inclusive, optional, default startLine + 99) + * @param endLine - Ending line number (1-indexed, inclusive, optional, default startLine + 199 (200 lines)) * @param environment - Execution environment ("android" or "linux"), default "android" */ function readPart(path: string, startLine?: number, endLine?: number, environment?: FileEnvironment): Promise;