Skip to content

Commit 3daabed

Browse files
committed
refactor(tokenizer): use TokenizerFactory for tokenizer creation
This change replaces the direct use of the `TokenizerImpl.INSTANCE` with the `TokenizerFactory.createTokenizer()` method to ensure consistent tokenizer creation across the application. It simplifies the process of obtaining a tokenizer instance and enhances the modularity of the codebase. Additionally, the `LLMCoroutineScope` now has a dedicated `coroutineExceptionHandler` for improved error handling.
1 parent 5b63ff6 commit 3daabed

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputSection.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import cc.unitmesh.devti.agent.configurable.customAgentSetting
66
import cc.unitmesh.devti.agent.model.CustomAgentConfig
77
import cc.unitmesh.devti.agent.model.CustomAgentState
88
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
9-
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
9+
import cc.unitmesh.devti.llms.tokenizer.TokenizerFactory
1010
import cc.unitmesh.devti.settings.AutoDevSettingsState
1111
import com.intellij.openapi.Disposable
1212
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -32,7 +32,6 @@ import com.intellij.util.ui.JBEmptyBorder
3232
import com.intellij.util.ui.JBUI
3333
import com.intellij.util.ui.UIUtil
3434
import com.intellij.util.ui.components.BorderLayoutPanel
35-
import kotlinx.serialization.json.Json
3635
import java.awt.CardLayout
3736
import java.awt.Color
3837
import java.awt.Dimension
@@ -44,7 +43,6 @@ import javax.swing.JComponent
4443
import javax.swing.JPanel
4544
import kotlin.math.max
4645
import kotlin.math.min
47-
import kotlinx.serialization.decodeFromString
4846

4947
/**
5048
*
@@ -65,7 +63,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
6563

6664
val editorListeners = EventDispatcher.create(AutoDevInputListener::class.java)
6765
private var tokenizer: Tokenizer? = try {
68-
lazy { TokenizerImpl.INSTANCE }.value
66+
lazy { TokenizerFactory.createTokenizer() }.value
6967
} catch (e: Exception) {
7068
logger.error("TokenizerImpl.INSTANCE is not available", e)
7169
null

src/main/kotlin/cc/unitmesh/devti/llms/tokenizer/TokenizerImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class TokenizerImpl : Tokenizer {
2121
override fun tokenize(chunk: String): IntArrayList? {
2222
return encoding.encode(chunk, maxTokenLength).tokens
2323
}
24+
}
2425

25-
companion object {
26-
val INSTANCE = ApplicationManager.getApplication().getService(TokenizerImpl::class.java)
27-
}
26+
object TokenizerFactory {
27+
fun createTokenizer(): Tokenizer = ApplicationManager.getApplication().getService(TokenizerImpl::class.java)
2828
}

src/main/kotlin/cc/unitmesh/devti/provider/PromptStrategy.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cc.unitmesh.devti.provider
22

33
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
4-
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
4+
import cc.unitmesh.devti.llms.tokenizer.TokenizerFactory
55
import cc.unitmesh.devti.prompting.CodePromptText
66
import cc.unitmesh.devti.settings.AutoDevSettingsState
77
import com.intellij.openapi.extensions.ExtensionPointName
@@ -26,7 +26,7 @@ abstract class PromptStrategy : LazyExtensionInstance<PromptStrategy>() {
2626
@Attribute("implementationClass")
2727
var implementationClass: String? = null
2828

29-
private val tokenizer: Tokenizer = TokenizerImpl.INSTANCE
29+
private val tokenizer: Tokenizer = TokenizerFactory.createTokenizer()
3030

3131
override fun getImplementationClassName(): String? = implementationClass
3232

src/main/kotlin/cc/unitmesh/devti/util/LLMCoroutineScope.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import kotlinx.coroutines.SupervisorJob
1010

1111
@Service(Service.Level.PROJECT)
1212
class LLMCoroutineScope {
13+
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
14+
Logger.getInstance(LLMCoroutineScope::class.java).error(throwable)
15+
}
16+
1317
val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + coroutineExceptionHandler)
1418

1519
companion object {
16-
val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
17-
Logger.getInstance(LLMCoroutineScope::class.java).error(throwable)
18-
}
19-
2020
fun scope(project: Project): CoroutineScope = project.service<LLMCoroutineScope>().coroutineScope
2121
}
2222
}

src/main/kotlin/com/intellij/temporary/error/ErrorMessageProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
22
package com.intellij.temporary.error
33

4+
import cc.unitmesh.devti.llms.tokenizer.TokenizerFactory
45
import cc.unitmesh.devti.util.isInProject
5-
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
66
import cc.unitmesh.devti.prompting.TextTemplatePrompt
77
import cc.unitmesh.devti.settings.AutoDevSettingsState
88
import com.intellij.execution.filters.FileHyperlinkInfo
@@ -80,7 +80,7 @@ object ErrorMessageProcessor {
8080
extractErrorPlaces(project, consoleLineFrom, consoleLineTo, consoleEditor)
8181

8282
val errorPromptBuilder =
83-
ErrorPromptBuilder(AutoDevSettingsState.maxTokenLength, TokenizerImpl.INSTANCE)
83+
ErrorPromptBuilder(AutoDevSettingsState.maxTokenLength, TokenizerFactory.createTokenizer())
8484

8585
return errorPromptBuilder.buildPrompt(extractedText, extractedErrorPlaces)
8686
}

0 commit comments

Comments
 (0)