Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for TranslateText #67

Merged
merged 2 commits into from
May 21, 2024
Merged
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
57 changes: 33 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ require('cmp').setup({
| `Fitten refactor_code` | Refactor code |
| `Fitten guess_programming_language` | Guess programming language |
| `Fitten analyze_data` | Analyze data |
| `Fitten translate_text` | Translate text |
| `Fitten translate_text_to_chinese` | Translate text to Chinese |
| `Fitten translate_text_to_english` | Translate text to English |
| `Fitten start_chat` | Start chat |

### Default Mappings
Expand Down Expand Up @@ -227,34 +230,40 @@ vim.log = {

---@class ImplementFeaturesOptions : ActionOptions
---@field feature_type string

---@class TranslateTextOptions : ActionOptions
---@field target_language string
```

### List of APIs

| API Prototype | Description |
|------------------------------------------------|-----------------------------------------------------------------|
| `login(username, password)` | Login to Fitten Code AI |
| `logout()` | Logout from Fitten Code AI |
| `register()` | Register to Fitten Code AI |
| `set_log_level(level)` | Set the log level |
| `get_current_status()` | Get the current status of the `InlineEngine` and `ActionEngine` |
| `triggering_completion()` | Manually triggering completion |
| `has_suggestion()` | Check if there is a suggestion |
| `accept_all_suggestions()` | Accept all suggestions |
| `accept_line()` | Accept line |
| `accept_word()` | Accept word |
| `document_code(ActionOptions)` | Document code |
| `edit_code(ActionOptions)` | Edit code |
| `explain_code(ActionOptions)` | Explain code |
| `find_bugs(ActionOptions)` | Find bugs |
| `generate_unit_test(GenerateUnitTestOptions)` | Generate unit test |
| `implement_features(ImplementFeaturesOptions)` | Implement features |
| `improve_code(ActionOptions)` | Improve code |
| `refactor_code(ActionOptions)` | Refactor code |
| `guess_programming_language(ActionOptions)` | Guess programming language |
| `analyze_data(ActionOptions)` | Analyze data |
| `start_chat(ActionOptions)` | Start chat |
| `stop_eval()` | Stop the evaluation |
| API Prototype | Description |
|---------------------------------------------------|-----------------------------------------------------------------|
| `login(username, password)` | Login to Fitten Code AI |
| `logout()` | Logout from Fitten Code AI |
| `register()` | Register to Fitten Code AI |
| `set_log_level(level)` | Set the log level |
| `get_current_status()` | Get the current status of the `InlineEngine` and `ActionEngine` |
| `triggering_completion()` | Manually triggering completion |
| `has_suggestion()` | Check if there is a suggestion |
| `accept_all_suggestions()` | Accept all suggestions |
| `accept_line()` | Accept line |
| `accept_word()` | Accept word |
| `document_code(ActionOptions)` | Document code |
| `edit_code(ActionOptions)` | Edit code |
| `explain_code(ActionOptions)` | Explain code |
| `find_bugs(ActionOptions)` | Find bugs |
| `generate_unit_test(GenerateUnitTestOptions)` | Generate unit test |
| `implement_features(ImplementFeaturesOptions)` | Implement features |
| `improve_code(ActionOptions)` | Improve code |
| `refactor_code(ActionOptions)` | Refactor code |
| `guess_programming_language(ActionOptions)` | Guess programming language |
| `analyze_data(ActionOptions)` | Analyze data |
| `translate_text(TranslateTextOptions)` | Translate text |
| `translate_text_to_chinese(TranslateTextOptions)` | Translate text to Chinese |
| `translate_text_to_english(TranslateTextOptions)` | Translate text to English |
| `start_chat(ActionOptions)` | Start chat |
| `stop_eval()` | Stop the evaluation |

## 🎉 Special Thanks

Expand Down
14 changes: 14 additions & 0 deletions lua/fittencode/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ M.api = {
analyze_data = function(opts)
return ActionsEngine.analyze_data(opts)
end,
---@param opts? TranslateTextOptions
translate_text = function(opts)
return ActionsEngine.translate_text(opts)
end,
---@param opts? TranslateTextOptions
translate_text_to_chinese = function(opts)
opts.target_language = 'Chinese'
return ActionsEngine.translate_text(opts)
end,
---@param opts? TranslateTextOptions
translate_text_to_english = function(opts)
opts.target_language = 'English'
return ActionsEngine.translate_text(opts)
end,
---@param opts? ActionOptions
start_chat = function(opts)
return ActionsEngine.start_chat(opts)
Expand Down
24 changes: 24 additions & 0 deletions lua/fittencode/bindings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ local function _analyze_data(...)
return _action_apis_wrap_content(API.analyze_data, ...)
end

local function _translate_text(...)
local args = { ... }
---@type TranslateTextOptions
local opts = {
target_language = args[1],
content = args[2],
}
return API.translate_text(opts)
end

local function _translate_text_to_chinese(...)
return _action_apis_wrap_content(API.translate_text_to_chinese, ...)
end

local function _translate_text_to_english(...)
return _action_apis_wrap_content(API.translate_text_to_english, ...)
end

function M.setup_commands()
---@type FittenCommands
local commands = {
Expand Down Expand Up @@ -201,6 +219,12 @@ function M.setup_commands()
guess_programming_language = _guess_programming_language,
-- Arguments: data
analyze_data = _analyze_data,
-- Arguments: traget_language, text
translate_text = _translate_text,
-- Arguments: text
translate_text_to_chinese = _translate_text_to_chinese,
-- Arguments: text
translate_text_to_english = _translate_text_to_english,
-- Arguments: language
start_chat = _start_chat,
-- Arguments: Nop
Expand Down
18 changes: 18 additions & 0 deletions lua/fittencode/engines/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ local SC = Status.C
---@field implement_features function
---@field improve_code function
---@field refactor_code function
---@field guess_programming_language function
---@field analyze_data function
---@field translate_text function
local ActionsEngine = {}

local ACTIONS = {
Expand All @@ -48,6 +51,7 @@ local ACTIONS = {
RefactorCode = 8,
GuessProgrammingLanguage = 9,
AnalyzeData = 10,
TranslateText = 11,
}

local current_eval = 1
Expand Down Expand Up @@ -81,6 +85,9 @@ local status = nil
---@class ImplementFeaturesOptions : ActionOptions
---@field feature_type string

---@class TranslateTextOptions : ActionOptions
---@field target_language string

---@param action integer
local function get_action_name(action)
return Base.tbl_key_by_value(ACTIONS, action)
Expand Down Expand Up @@ -485,6 +492,17 @@ function ActionsEngine.analyze_data(opts)
ActionsEngine.start_action(ACTIONS.AnalyzeData, merged)
end

---@param opts? TranslateTextOptions
function ActionsEngine.translate_text(opts)
local defaults = {}
local merged = vim.tbl_deep_extend('force', defaults, opts or {})
if merged.target_language == nil or #merged.target_language == 0 then
return
end
assert(merged.target_language)
ActionsEngine.start_action(ACTIONS.TranslateText, merged)
end

-- API: ActionOptions.content
---@param opts? ActionOptions
function ActionsEngine.start_chat(opts)
Expand Down
7 changes: 6 additions & 1 deletion lua/fittencode/prompt_providers/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local function make_range_content(buffer, range)
return table.concat(lines, '\n')
end

local NO_LANG_ACTIONS = { 'StartChat', 'GuessProgrammingLanguage', 'AnalyzeData' }
local NO_LANG_ACTIONS = { 'StartChat', 'GuessProgrammingLanguage', 'AnalyzeData', 'TranslateText' }

local MAP_ACTION_PROMPTS = {
StartChat = 'Answer the question above',
Expand All @@ -82,6 +82,11 @@ local MAP_ACTION_PROMPTS = {
RefactorCode = 'Refactor the code above',
GuessProgrammingLanguage = 'Guess the programming language of the code above',
AnalyzeData = 'Analyze the data above',
TranslateText = function(ctx)
assert(ctx.action_opts)
assert(ctx.action_opts.target_language)
return 'TranslateText the text above' .. ' to ' .. ctx.action_opts.target_language
end
}

local function make_language(ctx)
Expand Down