|
1 | 1 | package ai.devchat.devchat |
2 | 2 |
|
3 | 3 | import ai.devchat.devchat.handler.* |
4 | | -import java.lang.reflect.InvocationTargetException |
| 4 | +import com.alibaba.fastjson.JSONObject |
5 | 5 | import kotlin.reflect.KClass |
6 | 6 | import kotlin.reflect.full.primaryConstructor |
7 | 7 |
|
8 | 8 | class ActionHandlerFactory { |
9 | | - private val actionHandlerMap: Map<String, KClass<out ActionHandler>> = |
10 | | - object : HashMap<String, KClass<out ActionHandler>>() { |
11 | | - init { |
12 | | - put(DevChatActions.SEND_MESSAGE_REQUEST, SendMessageRequestHandler::class) |
13 | | - put(DevChatActions.SET_OR_UPDATE_KEY_REQUEST, SetOrUpdateKeyRequestHandler::class) |
14 | | - put(DevChatActions.LIST_COMMANDS_REQUEST, ListCommandsRequestHandler::class) |
15 | | - put(DevChatActions.LOAD_CONVERSATIONS_REQUEST, LoadConversationRequestHandler::class) |
16 | | - put(DevChatActions.LOAD_HISTORY_MESSAGES_REQUEST, LoadHistoryMessagesRequestHandler::class) |
17 | | - put(DevChatActions.LIST_TOPICS_REQUEST, ListTopicsRequestHandler::class) |
18 | | - put(DevChatActions.INSERT_CODE_REQUEST, InsertCodeRequestHandler::class) |
19 | | - put(DevChatActions.REPLACE_FILE_CONTENT_REQUEST, ReplaceFileContentHandler::class) |
20 | | - put(DevChatActions.VIEW_DIFF_REQUEST, ViewDiffRequestHandler::class) |
21 | | - put(DevChatActions.LIST_CONTEXTS_REQUEST, ListContextsRequestHandler::class) |
22 | | - put(DevChatActions.LIST_MODELS_REQUEST, ListModelsRequestHandler::class) |
23 | | - put(DevChatActions.ADD_CONTEXT_REQUEST, AddContextRequestHandler::class) |
24 | | - put(DevChatActions.GET_KEY_REQUEST, GetKeyRequestHandler::class) |
25 | | - put(DevChatActions.COMMIT_CODE_REQUEST, CommitCodeRequestHandler::class) |
26 | | - put(DevChatActions.GET_SETTING_REQUEST, GetSettingRequestHandler::class) |
27 | | - put(DevChatActions.UPDATE_SETTING_REQUEST, UpdateSettingRequestHandler::class) |
28 | | - put(DevChatActions.SHOW_SETTING_DIALOG_REQUEST, ShowSettingDialogRequestHandler::class) |
29 | | - put(DevChatActions.DELETE_LAST_CONVERSATION_REQUEST, DeleteLastConversationRequestHandler::class) |
30 | | - put(DevChatActions.DELETE_TOPIC_REQUEST, DeleteTopicRequestHandler::class) |
31 | | - } |
32 | | - } |
| 9 | + private val actionHandlerMap: Map<String, KClass<out ActionHandler>> = mapOf( |
| 10 | + DevChatActions.SEND_MESSAGE_REQUEST to SendMessageRequestHandler::class, |
| 11 | + DevChatActions.SET_OR_UPDATE_KEY_REQUEST to SetOrUpdateKeyRequestHandler::class, |
| 12 | + DevChatActions.LIST_COMMANDS_REQUEST to ListCommandsRequestHandler::class, |
| 13 | + DevChatActions.LOAD_CONVERSATIONS_REQUEST to LoadConversationRequestHandler::class, |
| 14 | + DevChatActions.LOAD_HISTORY_MESSAGES_REQUEST to LoadHistoryMessagesRequestHandler::class, |
| 15 | + DevChatActions.OPEN_LINK_REQUEST to OpenLinkRequestHandler::class, |
| 16 | + DevChatActions.LIST_TOPICS_REQUEST to ListTopicsRequestHandler::class, |
| 17 | + DevChatActions.INSERT_CODE_REQUEST to InsertCodeRequestHandler::class, |
| 18 | + DevChatActions.REPLACE_FILE_CONTENT_REQUEST to ReplaceFileContentHandler::class, |
| 19 | + DevChatActions.VIEW_DIFF_REQUEST to ViewDiffRequestHandler::class, |
| 20 | + DevChatActions.LIST_CONTEXTS_REQUEST to ListContextsRequestHandler::class, |
| 21 | + DevChatActions.LIST_MODELS_REQUEST to ListModelsRequestHandler::class, |
| 22 | + DevChatActions.ADD_CONTEXT_REQUEST to AddContextRequestHandler::class, |
| 23 | + DevChatActions.GET_KEY_REQUEST to GetKeyRequestHandler::class, |
| 24 | + DevChatActions.COMMIT_CODE_REQUEST to CommitCodeRequestHandler::class, |
| 25 | + DevChatActions.GET_SETTING_REQUEST to GetSettingRequestHandler::class, |
| 26 | + DevChatActions.UPDATE_SETTING_REQUEST to UpdateSettingRequestHandler::class, |
| 27 | + DevChatActions.SHOW_SETTING_DIALOG_REQUEST to ShowSettingDialogRequestHandler::class, |
| 28 | + DevChatActions.DELETE_LAST_CONVERSATION_REQUEST to DeleteLastConversationRequestHandler::class, |
| 29 | + DevChatActions.DELETE_TOPIC_REQUEST to DeleteTopicRequestHandler::class, |
| 30 | + ) |
33 | 31 |
|
34 | | - fun createActionHandler(action: String): ActionHandler { |
35 | | - val handlerClass = actionHandlerMap[action] |
36 | | - return if (handlerClass != null) { |
37 | | - try { |
38 | | - handlerClass.primaryConstructor!!.call(DevChatActionHandler.instance) |
39 | | - } catch (e: NoSuchMethodException) { |
40 | | - throw RuntimeException("Failed to instantiate action handler for: $action", e) |
41 | | - } catch (e: InstantiationException) { |
42 | | - throw RuntimeException("Failed to instantiate action handler for: $action", e) |
43 | | - } catch (e: IllegalAccessException) { |
44 | | - throw RuntimeException("Failed to instantiate action handler for: $action", e) |
45 | | - } catch (e: InvocationTargetException) { |
46 | | - throw RuntimeException("Failed to instantiate action handler for: $action", e) |
47 | | - } |
48 | | - } else { |
49 | | - throw RuntimeException("Action handler not found: $action") |
| 32 | + fun createActionHandler(action: String, metadata: JSONObject, payload: JSONObject): ActionHandler { |
| 33 | + val handlerClass = actionHandlerMap[action] ?: throw RuntimeException("Action handler not found: $action") |
| 34 | + return try { |
| 35 | + handlerClass.primaryConstructor!!.call(metadata, payload) |
| 36 | + } catch (e: Exception) { |
| 37 | + // Catch any exception since the handling is the same |
| 38 | + throw RuntimeException("Failed to instantiate action handler for: $action", e) |
50 | 39 | } |
51 | 40 | } |
52 | 41 | } |
0 commit comments