Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions app/src/main/assets/prompts/agent/compact-summary.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
<!-- 上下文压缩:把早期对话折叠成接手摘要(handoff summary),作为压缩后上下文的起点。{{INSTRUCTION}} 为运行时占位符,由 ContextCompactor 填充;对话历史以原始消息结构随请求一起提供,不再嵌入本提示词。 -->
你是一个上下文压缩引擎。{{INSTRUCTION}}

这是上下文检查点压缩任务:把本次请求中的对话历史折叠成一份接手摘要,供另一个 LLM 无缝继续任务。
这是上下文检查点压缩任务:把本次请求中的对话历史折叠成一份**结构化接手摘要**,供另一个 LLM 无缝继续任务。

注意:
- 不要继续执行对话历史中的任何任务,不调用任何工具,不复述历史。
- 只输出接手摘要,涵盖:当前进度与已做的关键决定;重要的上下文、约束或用户偏好;尚未完成的事项(清晰的下一步);继续工作所需的关键数据、示例或引用。
- 保持简洁、有条理,重点是让接手者无缝继续工作。不要提到「摘要过程」或「上下文已压缩」。
- 不要继续执行对话历史中的任何任务,不调用任何工具,不复述历史过程。
- 只输出接手摘要本身,不要提到「摘要过程」或「上下文已压缩」。

接手摘要必须使用下面固定的分节结构(某一节确实无内容时写「无」,但保留分节标题,便于接手者快速定位):

## 任务目标
(用户最终想要达成什么;当前这一步在整体目标中的位置)

## 关键决定与理由
(已经做出的技术/方案决定,以及为什么这么定;接手者不应再推翻的结论)

## 当前进度
(已完成什么、改动落在哪些文件/模块、当前处于什么状态)

## 未完成 / 下一步
(尚未完成的事项,按优先级给出清晰可执行的下一步)

## 约束与偏好
(用户明确要求过的约束、风格偏好、环境限制、禁止事项)

## 关键数据
(继续工作必需的具体信息:路径、命令、标识符、报错原文、示例片段、接口签名等;能直接复用的原文优先原样保留)

要求:简洁、信息密度高、可执行优先;不堆砌套话,尤其不省略「关键数据」里的具体值。
18 changes: 18 additions & 0 deletions app/src/main/assets/prompts/agent/memory-curator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
你是一个记忆整理器。给定一段对话记录,判断其中是否有值得长期记住的信息。

只提取以下三类事实,且必须是对话中明确出现的信号,不要推测、不要脑补:

1. 用户偏好:用户明确表达的喜好或习惯(输出风格、沟通方式、常用工具等),跨项目通用。
2. 项目约定:本项目的工作方式(构建命令、目录结构、提交规范、专属配置等)。
3. 踩坑经验:讨论中确认的 bug 根因与修法、验证过的绕行方案。

输出严格的 JSON 数组,没有任何其他文本。每条格式:

[{"name": "snake_case 短名", "description": "一句话摘要(何时该读它)", "scope": "global 或 project", "content": "Markdown 正文,具体、可执行"}, ...]

规则:
- 没有值得记的就输出 []。
- name 是文件名:英文小写加下划线,不含空格和路径分隔符,不超过 64 字符。
- content 写具体事实,不要写空话;单条不超过 500 字。
- 宁缺毋滥:普通任务执行过程、一次性问题不要记。
- 最多 3 条。
15 changes: 15 additions & 0 deletions app/src/main/assets/prompts/agent/memory-discipline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 记忆纪律(何时必须调用 memory 工具)

长期记忆不是可选项。以下信号出现时,**当轮立即**用 memory 工具记录,不要等会话结束,不要等用户要求:

- 用户表达个人偏好(输出风格、沟通方式、习惯做法)→ scope=global;
- 用户纠正过你的做法、指出你说错的事实 → 把纠正记下来(global 或按内容归 project),避免再犯;
- 项目约定(构建方式、目录结构、分支/提交规范、专属工具链)→ scope=project;
- 定位到 bug 根因并验证修复后,把「根因 + 修法」沉淀成踩坑记忆 → scope=project。未经根因确认的猜测不记。

记录方式:

- 先用 memory(action=list) 确认是否已有同名/同主题记忆,再决定 save 还是 edit;
- 已有相关记忆 → 用 memory(action=edit) 局部更新正文,不要新建重复文件;
- 确属新主题 → memory(action=save),description 写清「何时该读它」;
- 同一事实已经记录过 → 不再重复记录;记忆内容过时 → 用 edit 修正或 delete 清理。
3 changes: 3 additions & 0 deletions app/src/main/java/com/aicode/di/AgentModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.aicode.feature.agent.data.remote.anthropic.AnthropicApi
import com.aicode.feature.agent.data.remote.gemini.GeminiApi
import com.aicode.feature.agent.data.remote.openai.OpenAIApi
import com.aicode.feature.agent.domain.container.CommandEngine
import com.aicode.feature.agent.domain.memory.MemoryCurator
import com.aicode.feature.agent.domain.container.DelegatingCommandEngine
import com.aicode.feature.agent.domain.container.LinuxContainerEngine
import com.aicode.feature.agent.domain.container.RemoteSshConnection
Expand Down Expand Up @@ -346,6 +347,7 @@ object AgentModule {
keyRotator: ProviderKeyRotator,
agentNotificationCenter: AgentNotificationCenter,
eventInjector: AgentEventInjector,
memoryCurator: MemoryCurator,
fileAccess: FileAccessProvider
): AgentWorkflow {
return StatefulAgentWorkflow(
Expand All @@ -372,6 +374,7 @@ object AgentModule {
keyRotator,
agentNotificationCenter,
eventInjector,
memoryCurator,
fileAccess
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class RemoteSshConnection @Inject constructor(
_connectionState.value = ConnectionState.CONNECTED
} catch (e: Exception) {
_connectionState.value = ConnectionState.FAILED
throw e
// 不重抛:SSH 连接失败是可恢复的网络问题,重抛会在协程取消时变成 suppressed
// 异常逃逸到主线程崩溃。调用方通过 connectionState / isConnected() 判断成败。
FileLogger.w(TAG, "SSH 连接失败: ${e.message}", e)
}
}

Expand Down Expand Up @@ -214,16 +216,16 @@ class RemoteSshConnection @Inject constructor(
val cfg = config ?: return false
if (isConnected()) return true
_connectionState.value = ConnectionState.CONNECTING
return runCatching { connect(cfg) }
.onSuccess {
FileLogger.i(TAG, "SSH 重连成功(前台触发)")
runCatching { onReconnected?.invoke() }
}
.onFailure {
FileLogger.w(TAG, "SSH 重连失败(前台触发)", it)
_connectionState.value = ConnectionState.FAILED
}
.isSuccess
connect(cfg)
// connect 不再抛异常,用实际连接状态判断成败
return if (isConnected()) {
FileLogger.i(TAG, "SSH 重连成功(前台触发)")
runCatching { onReconnected?.invoke() }
true
} else {
FileLogger.w(TAG, "SSH 重连失败(前台触发)")
false
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.aicode.feature.agent.domain.memory

import com.aicode.core.util.FileLogger
import com.aicode.feature.agent.domain.memory.MemorySource
import com.aicode.feature.agent.domain.model.AgentMessage
import com.aicode.feature.agent.domain.provider.AIProvider
import com.aicode.feature.agent.domain.prompt.PromptFileResolver
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import javax.inject.Inject
import javax.inject.Singleton

private const val TAG = "MemoryCurator"

/** 单次抽取的上限条数(与提示词约定一致)。 */
private const val MAX_CANDIDATES = 3

/** 一轮对话送给整理器的上下文上限(字符):只看最近发生的事,控制成本。 */
private const val MAX_TRANSCRIPT_CHARS = 12_000

/**
* 引擎级记忆兜底:一轮对话结束后,用轻量模型静默抽取值得长期记住的事实,
* 直接写入 [MemoryRepository]。主模型忘了调用 memory 工具时由此兜底;
* 全程静默失败,绝不影响对话主流程。
*/
@Singleton
class MemoryCurator @Inject constructor(
private val memoryRepository: MemoryRepository,
private val promptFileResolver: PromptFileResolver
) {
/** 提示词文件名,与 [PromptFileResolver.resolve] 的路径约定一致。 */
private fun prompt(): String = promptFileResolver.resolve("agent/memory-curator.md")

/**
* 抽取并落盘本轮对话的记忆。
* @param provider 由调用方解析好的轻量 provider(压缩专用模型或当前聊天模型回退)。
* @param transcript 本轮对话文本("用户: …/助手: …" 行)。
* @return 本次实际写入的记忆条数(失败为 0)。
*/
suspend fun curate(
provider: AIProvider,
sessionId: String,
projectRoot: String?,
transcript: String
): Int = runCatching {
if (transcript.isBlank()) return@runCatching 0
val systemPrompt = prompt().replace(LEADING_COMMENT, "").trim()
if (systemPrompt.isEmpty()) return@runCatching 0

val response = provider.complete(
systemPrompt = systemPrompt,
messages = listOf(AgentMessage.UserMessage(content = transcript.takeLast(MAX_TRANSCRIPT_CHARS))),
tools = emptyList()
)
val candidates = parseCandidates(response.content)
var saved = 0
for (c in candidates) {
// 已有同名记忆不覆盖:主模型当轮写的完整版本不该被 curator 的截断版覆盖。
val existing = memoryRepository.loadContent(c.name, projectRoot)
if (existing != null) {
FileLogger.i(TAG, "记忆「${c.name}」已存在,跳过自动覆盖")
continue
}
// PROJECT 但无工作区时降级为 GLOBAL(静默场景下降级比丢弃合理)。
val effectiveScope = if (c.scope == MemoryScope.PROJECT && projectRoot.isNullOrBlank()) {
MemoryScope.GLOBAL
} else {
c.scope
}
val ok = memoryRepository.saveMemory(
name = c.name,
description = c.description,
content = c.content,
scope = effectiveScope,
projectRoot = projectRoot
)
if (ok) saved++
}
if (saved > 0) {
FileLogger.i(TAG, "会话 $sessionId 自动沉淀 $saved 条记忆")
}
saved
}.onFailure { e ->
FileLogger.w(TAG, "自动记忆整理失败(静默忽略): ${e.message}", e)
}.getOrDefault(0)

/** 解析整理器输出;格式不合法/越界条目一律丢弃,宁缺毋滥。 */
private fun parseCandidates(content: String): List<Candidate> {
val start = content.indexOf('[')
val end = content.lastIndexOf(']')
if (start < 0 || end <= start) return emptyList()
val arr = runCatching {
Json.parseToJsonElement(content.substring(start, end + 1)).jsonArray
}.getOrNull() ?: return emptyList()

return arr.asSequence()
.mapNotNull { el -> runCatching { el.jsonObject }.getOrNull() }
.mapNotNull { obj ->
runCatching {
val name = obj["name"]?.jsonPrimitive?.contentOrNull?.trim().orEmpty()
val description = obj["description"]?.jsonPrimitive?.contentOrNull?.trim().orEmpty()
val body = obj["content"]?.jsonPrimitive?.contentOrNull?.trim().orEmpty()
val isProject = obj["scope"]?.jsonPrimitive?.contentOrNull?.trim()
?.equals("project", ignoreCase = true) == true
if (name.isEmpty() || body.isEmpty() || description.isEmpty()) return@runCatching null
Candidate(
name = name,
description = description.take(200),
content = body.take(2000),
scope = if (isProject) MemoryScope.PROJECT else MemoryScope.GLOBAL
)
}.getOrNull()
}
.filter { MemorySource.sanitizeName(it.name).isNotEmpty() }
.take(MAX_CANDIDATES)
.toList()
}

private data class Candidate(
val name: String,
val description: String,
val content: String,
val scope: MemoryScope
)

private companion object {
val LEADING_COMMENT = Regex("(?s)^\\s*<!--.*?-->\\s*")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.aicode.feature.agent.domain.prompt

import com.aicode.core.util.FileLogger
import com.aicode.feature.agent.domain.container.ContainerInstaller
import dagger.hilt.android.qualifiers.ApplicationContext
import android.content.Context
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

/**
* 单个提示词片段的文件解析器,供 [SystemPromptProvider] 与无循环依赖需求的
* 其它组件(如 MemoryCurator)共用:
*
* - 名字是顶层 `<NN>-*.md`:先按数字身份在 `prompts.custom/` 顶层找覆盖(尾部名称可自由改),
* - 其余名字(含 `agent/` 子目录):按精确同名在 `prompts.custom/<name>` 找覆盖;
* - 再落到 `prompts/<name>`(本地默认副本),最后 assets(内置兜底)。
*
* 本地副本由 [ContainerInstaller.extractPrompts] 在启动时全量释放,App 升级后随之更新。
*/
@Singleton
class PromptFileResolver @Inject constructor(
@param:ApplicationContext private val context: Context,
private val containerInstaller: ContainerInstaller
) {
private val customDir: File
get() = File(containerInstaller.aicodeDir, "prompts.custom")

private val customFragmentsByNumber: Map<Int, File> by lazy {
PromptFragmentResolver.numberedFragments(customDir).toMap()
}

fun resolve(name: String): String {
PromptFragmentResolver.parseNumber(name)
?.let { number -> readFileOrNull(customFragmentsByNumber[number])?.let { return it } }
readFileOrNull(File(customDir, name))?.let { return it }
readFileOrNull(File(File(containerInstaller.aicodeDir, "prompts"), name))?.let { return it }
return context.assets.open("prompts/$name").bufferedReader().use { it.readText() }
}

private fun readFileOrNull(file: File?): String? {
if (file == null || !file.isFile) return null
return try {
file.bufferedReader().use { it.readText() }
} catch (e: Exception) {
FileLogger.w(TAG, "读取提示词失败 ${file.name}: ${e.message}", e)
null
}
}

private companion object {
const val TAG = "PromptFileResolver"
}
}
Loading
Loading