feat(agent-v1.1): 第四阶段 —— 幻觉检测 + 兼容降级 + 包导入导出界面 + 文档(v2.0.47)
按需求 v1.1 把最后三项补齐,然后发版。 ## 幻觉调用检测(需求 3.7) 模型很容易在"该记东西"的时候只在正文里说一句「我记住了」—— 用户当场看不出来(那句话说得太自然了),等到下次发现它完全不记得, 信任就没了。而"打算记"和"真的记了"在正文里看不出区别,只能由程序 比对「说了什么」和「做了什么」。 三个条件**同时**满足才算: ① 正文里有"我记住了"这类断言 ② 这一轮没有任何工具调用 ③ 工具是**可用的** 第③条最容易被忽略:Python 工具整个关掉时模型想调也调不了, 那时警告它"没调用工具"是冤枉它,而且警告会一直挂着,用户会以为坏了。 判宽比判窄更糟 —— 误伤会让用户看到莫名其妙的警告,几次之后他就不看 那个警示了(比没有还糟)。所以只收明确句式,`我记得 Python 的字典是有序的` 这种不报。界面在正文**下面**挂一条黄色警示(不塞进正文:那是系统批注, 不是 AI 说的话)。 ## 兼容降级(需求 3.3) 本地小模型、老网关很多不支持 `tools` 参数(轻则忽略、重则 400), 而记忆/情绪**全靠工具** —— 不降级的话用户换上这类模型会发现 "记忆功能突然全废了",还看不出原因。 做法:模型列表项加一个「不支持工具调用」开关,打开后不传 tools、 改成在提示词里约定正文内的调用格式(```doubao-call 代码块 + JSON), 内核解析 → 执行 → **从正文里剥掉**,执行记录照常发 tool_call/tool_result 事件 —— 所以界面上的工具痕迹、幻觉检测这些下游逻辑一行都不用改。 ★ 做成**用户手动打的标记**而不是自动探测:各家"不支持"的报错方式 五花八门,猜错的代价是把一个本来能用的模型降级掉,那比不降级更糟。 解析不了的块**原样留在正文里**:与其默默删掉(用户永远不知道它想干什么), 不如让他看见"它写了一段系统没认出来的东西" —— 那说明模型该换了。 ## 包导入导出界面(需求 2.5) - `AgentsScreen` 顶栏加「导入」(zip → base64 → 内核解包)。 不走 Kotlin 解压是因为安卓的 `ZipInputStream` 读不出这类 zip (Termux bootstrap 那个坑),而内核的 Python `zipfile` 没问题。 - 编辑界面底部加「导出这个智能体(.zip)」,并写明**包里只有** 名字/介绍/提示词/外观 —— 模型、世界观、限额不进包,更没有 API Key。 ## 文档 新增 `docs/智能体系统.md`:需求项 → 文件对照、数据分界(为什么一半 SQLite 一半 JSON)、三个容易踩的地方、包格式、接口一览、验证方法、 已知限制。README 加 §3.14 摘要。 ## 验证 - `scripts/probe-degraded.py`(新增):幻觉检测的**误伤**和**漏判**两头都钉 ("我记得 Python 的字典是有序的"不许报)、三个条件缺一不可、 降级调用的解析/执行/剥离/坏 JSON/未知工具/工具抛错/pinned 保护。 - `scripts/probe-agent-system.py`:12 节 70+ 项仍全绿。 - 装机回归:内核正常启动、选择界面四层叠加与默认回退完好。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import android.util.Base64
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
@@ -25,6 +25,7 @@ import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -102,6 +103,23 @@ fun AgentEditScreen(
|
||||
}.onFailure { vm.showHint("读不出这张图") }
|
||||
}
|
||||
|
||||
// 导出智能体包(需求 2.5):内核打包成 zip → base64 → 这里落盘。
|
||||
// ★ 包里**只有**名字/介绍/提示词/外观四件套 —— 模型引用、世界观绑定、限额
|
||||
// 一律不进包(需求 2.5 的"包的边界约定"),更不会有 API Key。
|
||||
var pendingZip by remember { mutableStateOf<String?>(null) }
|
||||
val saveZip = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.CreateDocument("application/zip"),
|
||||
) { uri ->
|
||||
val data = pendingZip
|
||||
pendingZip = null
|
||||
if (uri != null && data != null) {
|
||||
runCatching {
|
||||
ctx.contentResolver.openOutputStream(uri)
|
||||
?.use { it.write(Base64.decode(data, Base64.DEFAULT)) }
|
||||
}.onSuccess { vm.showHint("已导出") }.onFailure { vm.showHint("导出失败") }
|
||||
}
|
||||
}
|
||||
|
||||
FullScreen(
|
||||
title = if (isNew) "新建智能体" else "编辑智能体",
|
||||
subtitle = if (isNew) "保存后可以加外观图片" else draft.name,
|
||||
@@ -296,9 +314,31 @@ fun AgentEditScreen(
|
||||
)
|
||||
}
|
||||
|
||||
/* ---------------- 删除 ---------------- */
|
||||
if (!isNew && !draft.builtin) {
|
||||
/* ---------------- 导出 / 删除 ---------------- */
|
||||
if (!isNew) {
|
||||
Spacer(Modifier.height(10.dp))
|
||||
Column(
|
||||
Modifier.fillMaxWidth().padding(horizontal = 12.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
) {
|
||||
RowAction("导出这个智能体(.zip)") {
|
||||
vm.exportPackage("agents", draft.id) { content, _ -> pendingZip = content }
|
||||
}
|
||||
Text(
|
||||
"包里只有名字、介绍、提示词和外观图片;模型、世界观、限额不进包,更没有 API Key。",
|
||||
fontSize = 10.sp,
|
||||
lineHeight = 14.sp,
|
||||
color = doubaoTextSecondary(),
|
||||
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
|
||||
)
|
||||
}
|
||||
if (pendingZip != null) {
|
||||
LaunchedEffect(pendingZip) {
|
||||
saveZip.launch("${draft.name.ifBlank { "agent" }}.zip")
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isNew && !draft.builtin) {
|
||||
Box(Modifier.fillMaxWidth().padding(horizontal = 12.dp), contentAlignment = Alignment.Center) {
|
||||
RowAction("删除这个智能体", danger = true) {
|
||||
vm.deleteAgentFull(draft.id) { onDone() }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.background
|
||||
@@ -74,10 +74,30 @@ import com.doubao.lynxclone.data.VoiceOption
|
||||
fun AgentsScreen(vm: AppViewModel) {
|
||||
var confirmDelete by remember { mutableStateOf<Agent?>(null) }
|
||||
|
||||
// ★ 导入智能体包(需求 2.5):zip → base64 → 内核解包。
|
||||
// 不走 Kotlin 解压是因为安卓的 `ZipInputStream` **读不出**这类 zip
|
||||
// (Termux bootstrap 那个坑,见 TermuxInstaller 的注释),
|
||||
// 而内核的 Python `zipfile` 没这个问题 —— 少踩一次已知的坑。
|
||||
val ctx = androidx.compose.ui.platform.LocalContext.current
|
||||
val importPackage = androidx.activity.compose.rememberLauncherForActivityResult(
|
||||
androidx.activity.result.contract.ActivityResultContracts.OpenDocument(),
|
||||
) { uri ->
|
||||
if (uri != null) {
|
||||
runCatching {
|
||||
ctx.contentResolver.openInputStream(uri)?.use { it.readBytes() } ?: ByteArray(0)
|
||||
}.onSuccess { bytes ->
|
||||
vm.importPackage("agents", android.util.Base64.encodeToString(bytes, android.util.Base64.NO_WRAP), "")
|
||||
}.onFailure { vm.showHint("读不出这个文件") }
|
||||
}
|
||||
}
|
||||
|
||||
val activeId = vm.state.activeAgent?.id.orEmpty()
|
||||
|
||||
Column(Modifier.fillMaxSize()) {
|
||||
ScreenHeader("智能体", onBack = { vm.screen = Screen.Chat }) {
|
||||
TextButton(onClick = {
|
||||
importPackage.launch(arrayOf("application/zip", "application/octet-stream", "*/*"))
|
||||
}) { Text("导入", fontSize = 13.sp) }
|
||||
TextButton(onClick = { vm.openAgentEdit(null) }) { Text("新建", fontSize = 13.sp) }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.doubao.lynxclone.ui
|
||||
package com.doubao.lynxclone.ui
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
@@ -283,6 +283,7 @@ class AppViewModel(val client: KernelClient) : ViewModel() {
|
||||
apiKey = extra["apiKey"] as? String ?: "",
|
||||
provider = extra["provider"] as? String ?: "",
|
||||
pricePerK = (extra["pricePerK"] as? Double) ?: 0.0,
|
||||
noTools = (extra["noTools"] as? Boolean) ?: false,
|
||||
),
|
||||
id,
|
||||
)
|
||||
|
||||
@@ -627,6 +627,29 @@ private fun MessageItem(vm: AppViewModel, index: Int, msg: ChatMessage, speech:
|
||||
}
|
||||
}
|
||||
|
||||
// ★ 幻觉调用检测的警示(需求 3.7):它说了"我记住了"但这一轮没调工具。
|
||||
//
|
||||
// 放在正文**下面**而不是塞进正文:这是"系统对这条回复的批注",
|
||||
// 不是 AI 说的话 —— 混进正文里,用户会以为 AI 自己在自我怀疑。
|
||||
if (msg.hallucination) {
|
||||
Spacer(Modifier.height(6.dp))
|
||||
Row(
|
||||
Modifier
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.background(Color(0x1AF29900))
|
||||
.padding(horizontal = 8.dp, vertical = 5.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
"⚠ 它说记住了,但这一轮并没有真的调用记忆工具 —— 下次可能就不记得了。",
|
||||
fontSize = 10.sp,
|
||||
lineHeight = 14.sp,
|
||||
color = Color(0xFF8A6100),
|
||||
modifier = Modifier.widthIn(max = 280.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (!msg.pending && msg.content.isNotBlank() && !msg.error) {
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
|
||||
@@ -73,6 +73,8 @@ private data class Field(
|
||||
val multiline: Boolean = false,
|
||||
val password: Boolean = false,
|
||||
val number: Boolean = false,
|
||||
/** 开关:值的约定是 `"1"` = 开、`""` = 关(不弹键盘,点一下切换) */
|
||||
val toggle: Boolean = false,
|
||||
)
|
||||
|
||||
private val WORLD_FIELDS = listOf(
|
||||
@@ -100,6 +102,12 @@ private val MODEL_FIELDS = listOf(
|
||||
Field("apiKey", "API Key", "留空则用全局设置里那个", password = true),
|
||||
Field("provider", "提供商", "ark / openai / deepseek …(留空则跟随全局)"),
|
||||
Field("pricePerK", "单价(元/千 Token)", "留空 = 不显示费用估算", number = true),
|
||||
Field(
|
||||
"noTools", "不支持工具调用",
|
||||
"这个模型不会 function calling(本地小模型、老网关常见)。打开之后不再传 tools," +
|
||||
"改成让它在正文里写调用,内核执行完再剥掉 —— 记忆和情绪照常能用。",
|
||||
toggle = true,
|
||||
),
|
||||
)
|
||||
|
||||
/** 三张表:kind → (标题、字段、说明、是不是包) */
|
||||
@@ -322,6 +330,7 @@ private fun ResourceEditScreen(
|
||||
"name" to it.name, "apiBase" to it.apiBase, "model" to it.model,
|
||||
"apiKey" to "", "provider" to it.provider,
|
||||
"pricePerK" to if (it.pricePerK > 0) it.pricePerK.toString() else "",
|
||||
"noTools" to if (it.noTools) "1" else "",
|
||||
)
|
||||
} ?: emptyMap()
|
||||
}
|
||||
@@ -366,6 +375,7 @@ private fun ResourceEditScreen(
|
||||
extra["apiKey"] = values["apiKey"].orEmpty()
|
||||
extra["provider"] = values["provider"].orEmpty()
|
||||
extra["pricePerK"] = values["pricePerK"]?.toDoubleOrNull() ?: 0.0
|
||||
extra["noTools"] = values["noTools"] == "1"
|
||||
}
|
||||
if (kind.id == "worldviews") extra["image"] = worldImage
|
||||
vm.saveResource(kind.id, id, name, text, extra) { saving = false; onDone() }
|
||||
@@ -383,6 +393,42 @@ private fun ResourceEditScreen(
|
||||
.padding(14.dp),
|
||||
) {
|
||||
for (f in kind.fields) {
|
||||
// ---- 开关型字段:点一下切换,不弹键盘 ----
|
||||
if (f.toggle) {
|
||||
val on = values[f.key] == "1"
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
Text(f.label, fontSize = 13.sp, color = doubaoText())
|
||||
if (f.placeholder.isNotBlank()) {
|
||||
Text(
|
||||
f.placeholder,
|
||||
fontSize = 10.sp,
|
||||
lineHeight = 15.sp,
|
||||
color = doubaoTextSecondary(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.width(10.dp))
|
||||
Box(
|
||||
Modifier
|
||||
.size(width = 44.dp, height = 26.dp)
|
||||
.clip(RoundedCornerShape(13.dp))
|
||||
.background(if (on) Doubao.Blue else Doubao.Border)
|
||||
.clickable { values = values + (f.key to if (on) "" else "1") },
|
||||
contentAlignment = if (on) Alignment.CenterEnd else Alignment.CenterStart,
|
||||
) {
|
||||
Box(
|
||||
Modifier
|
||||
.padding(horizontal = 3.dp)
|
||||
.size(20.dp)
|
||||
.clip(RoundedCornerShape(10.dp))
|
||||
.background(Color.White),
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(Modifier.height(12.dp))
|
||||
continue
|
||||
}
|
||||
Text(f.label, fontSize = 13.sp, color = doubaoText())
|
||||
Spacer(Modifier.height(4.dp))
|
||||
OutlinedTextField(
|
||||
|
||||
Reference in New Issue
Block a user