Skip to content

Commit 01bedd6

Browse files
committed
feat(agent): add state reset and message preprocessing #259
- Add `resetState` method to `AgentStateService` to reset agent state. - Rename `compressHistory` to `preprocessMessages` and update its logic. - Integrate `AgentStateService` into `CustomLLMProvider` for message preprocessing.
1 parent 02b5774 commit 01bedd6

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

core/src/main/kotlin/cc/unitmesh/devti/llms/custom/CustomLLMProvider.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cc.unitmesh.devti.gui.chat.message.ChatRole
66
import cc.unitmesh.devti.llm2.model.LlmConfig
77
import cc.unitmesh.devti.llm2.model.ModelType
88
import cc.unitmesh.devti.llms.LLMProvider
9+
import cc.unitmesh.devti.observer.agent.AgentStateService
910
import cc.unitmesh.devti.prompting.optimizer.PromptOptimizer
1011
import cc.unitmesh.devti.settings.coder.coderSetting
1112
import com.intellij.openapi.diagnostic.logger
@@ -32,6 +33,8 @@ class CustomLLMProvider(val project: Project, var llmConfig: LlmConfig = LlmConf
3233
private val messages: MutableList<Message> = mutableListOf()
3334
private val logger = logger<CustomLLMProvider>()
3435

36+
private val agentService = project.getService(AgentStateService::class.java)
37+
3538
override fun clearMessage() = messages.clear()
3639

3740
override fun getAllMessages(): List<Message> {
@@ -81,8 +84,9 @@ class CustomLLMProvider(val project: Project, var llmConfig: LlmConfig = LlmConf
8184
}
8285

8386
messages += Message("user", prompt)
87+
val finalMsgs = agentService.preprocessMessages(messages)
8488

85-
val customRequest = CustomRequest(messages)
89+
val customRequest = CustomRequest(finalMsgs)
8690
val requestContent = customRequest.updateCustomFormat(requestFormat)
8791

8892
val body = RequestBody.create("application/json".toMediaTypeOrNull(), requestContent.toByteArray())

core/src/main/kotlin/cc/unitmesh/devti/observer/agent/AgentState.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@ import cc.unitmesh.devti.agent.tool.AgentTool
44
import cc.unitmesh.devti.llms.custom.Message
55
import com.intellij.openapi.components.Service
66
import com.intellij.util.diff.Diff.Change
7-
import kotlinx.serialization.Contextual
8-
import kotlinx.serialization.Serializable
97
import java.util.UUID
108

119
data class AgentState(
12-
val conversationId: String = UUID.randomUUID().toString(),
13-
val changeList: List<Change> = emptyList(),
14-
val messages: List<Message> = emptyList(),
15-
val usedTools: List<AgentTool> = emptyList(),
10+
var conversationId: String = UUID.randomUUID().toString(),
11+
var changeList: List<Change> = emptyList(),
12+
var messages: List<Message> = emptyList(),
13+
var usedTools: List<AgentTool> = emptyList(),
1614
)
1715

18-
19-
@Service
16+
@Service(Service.Level.PROJECT)
2017
class AgentStateService {
2118
var state: AgentState = AgentState()
2219

20+
fun resetState() {
21+
state = AgentState()
22+
}
23+
24+
fun addTools(tools: List<AgentTool>) {
25+
26+
}
27+
28+
fun addChanges(val fileName: String) {
29+
// todo changeList.add()
30+
}
31+
2332
/**
2433
* Call some LLM to compress it or use some other method to compress the history
2534
*/
26-
fun compressHistory(messages: List<Message>): List<Message> {
35+
fun preprocessMessages(messages: List<Message>): List<Message> {
36+
state.messages = messages
2737
return messages
2838
}
2939
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchInputListener.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ open class SketchInputListener(
7676
logger<SketchInputListener>().debug("Start compiling: $input")
7777
ProgressManager.getInstance().runProcessWithProgressSynchronously({
7878
val devInProcessor = LanguageProcessor.devin()
79-
val compiledInput = runReadAction { devInProcessor?.compile(project, input) } ?: input
79+
val compiledInput = runReadAction {
80+
devInProcessor?.compile(project, input)
81+
} ?: input
8082

8183
toolWindow.beforeRun()
8284
toolWindow.updateHistoryPanel()

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import cc.unitmesh.devti.gui.toolbar.NewSketchAction
1010
import cc.unitmesh.devti.inline.AutoDevInlineChatService
1111
import cc.unitmesh.devti.inline.fullHeight
1212
import cc.unitmesh.devti.inline.fullWidth
13+
import cc.unitmesh.devti.observer.agent.AgentStateService
1314
import cc.unitmesh.devti.sketch.ui.ExtensionLangSketch
1415
import cc.unitmesh.devti.sketch.ui.LangSketch
1516
import cc.unitmesh.devti.sketch.ui.LanguageSketchProvider
@@ -403,6 +404,8 @@ open class SketchToolWindow(
403404
myList.removeAll()
404405
historyPanel.removeAll()
405406
initializePreAllocatedBlocks(project)
407+
408+
project.getService(AgentStateService::class.java).resetState()
406409
}
407410

408411
fun printThinking(string: String) {

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/DevInsCompiler.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class DevInsCompiler(
6262

6363
output.append(it.text)
6464
}
65-
6665
DevInTypes.USED -> processUsed(it as DevInUsed)
6766
DevInTypes.COMMENTS -> {
6867
if (it.text.startsWith("[flow]:")) {
@@ -78,7 +77,6 @@ class DevInsCompiler(
7877
}
7978
}
8079
}
81-
8280
else -> {
8381
output.append(it.text)
8482
logger.warn("Unknown element type: ${it.elementType}")

0 commit comments

Comments
 (0)