Improvements to Code companion

This commit is contained in:
Chris
2025-05-12 09:04:40 +01:00
parent d97fdc798f
commit 4975f4c459
3 changed files with 111 additions and 4 deletions

View File

@@ -29,7 +29,7 @@ TODO: Neovim configurations I want to add:
- [ ] Add LSP symbols for Pest tests - [ ] Add LSP symbols for Pest tests
- [ ] More prompts - [ ] More prompts
- [ ] Give AI better context and tools for working with Hylark - [ ] Give AI better context and tools for working with Hylark
- [ ] Figure out workspaces - [ ] Figure out AI workspaces
- [ ] Figure out agentic workflow - [ ] Figure out agentic workflow
- [ ] Figure out a better way to add relevant buffers to AI - [ ] Figure out a better way to add relevant buffers to AI
- [ ] Better inline AI assistant - [ ] Better inline AI assistant

View File

@@ -22,7 +22,7 @@ return {
return require('codecompanion.adapters').extend('copilot', { return require('codecompanion.adapters').extend('copilot', {
schema = { schema = {
model = { model = {
default = 'gemini-2.0-flash', default = 'gemini-2.0-flash-001',
}, },
max_tokens = { max_tokens = {
default = 1000000, default = 1000000,
@@ -141,6 +141,7 @@ You must:
- Use actual line breaks in your responses; only use "\n" when you want a literal backslash followed by 'n'. - Use actual line breaks in your responses; only use "\n" when you want a literal backslash followed by 'n'.
- All non-code text responses must be written in the %s language indicated. - All non-code text responses must be written in the %s language indicated.
- Multiple, different tools can be called as part of the same response. - Multiple, different tools can be called as part of the same response.
- Use full path names when using tools to read and modify files.
When given a task: When given a task:
1. Think step-by-step and, unless the user requests otherwise or the task is very simple, describe your plan in detailed pseudocode. 1. Think step-by-step and, unless the user requests otherwise or the task is very simple, describe your plan in detailed pseudocode.
@@ -252,6 +253,111 @@ When given a task:
}, },
}, },
}, },
['PHPStan Fixer'] = {
strategy = 'workflow',
description = 'Use a workflow to fix PHPStan errors until there are none left.',
opts = {
short_name = 'phpstan',
},
prompts = {
{
{
name = 'Run PHPStan',
role = 'user',
opts = { auto_submit = false },
content = function()
-- Enable turbo mode!!!
vim.g.codecompanion_auto_tool_mode = true
return [[PHPStan is a static analysis tool for PHP. It is currently reporting errors in the code. Your task is to fix these errors and run PHPStan again until there are no errors left.
First of all use the @cmd_runner tool to run the `composer type-check` command. This will run PHPStan and output type errors in the code.]]
end,
},
},
{
{
name = 'Fetch files',
role = 'user',
opts = { auto_submit = false },
content = function()
-- Enable turbo mode!!!
vim.g.codecompanion_auto_tool_mode = true
return 'PHPStan has reported errors. Look at the output and see where the files are reported. Use the @mcp tool to read all the offending files so you can implement the fixes.'
end,
},
},
{
{
name = 'Fix errors and run',
role = 'user',
opts = { auto_submit = false },
content = function()
-- Enable turbo mode!!!
vim.g.codecompanion_auto_tool_mode = true
return [[### Instructions
Now you have the errors and the appropriate context you can fix the errors.
### Steps to Follow
You are required to analyse the PHPStan errors and write code to fix them.
Reason through the errors and write out the possible causes and fixes for each.
1. Then use the @mcp tool to update the files with the fixes. When editing files use the full path name including the project name /Users/chris/Code/Sites/hylark/
2. After editing the files use the @cmd_runner tool again to run the `composer type-check` command to see if any errors remain.
We'll repeat this cycle until there are no errors. Ensure no deviations from these steps.
### Tips for fixing PHPStan errors
- Do not ignore the errors
- Use Webmozart Assert library to narrow types
- Most errors are due to missing docblocks or calling methods/parameters on types that PHPStan cannot infer
- Use fully qualified namespaces for classes in doc blocks
- The code is working so fixes should not change the behaviour of the code
- There are some custom types in this project that help define types
- closure<T> creates T|Closure(): T, you can add a second argument to specify the depth so closure<T, 2> creates T|Closure(): T|Closure(): (Closure(): T). This is useful because GraphQL queries can return closures.
- promise<T> creates T|SyncPromise<T>, as with closure, you can add a second argument for depth. You can also add a third argument if T is an array to allow adding promises to each value, so promise<array{a: int}, 1, 1> will create array{a: int}|SyncPromise<array{a: int}>|array{a: SyncPromise<int>}|SyncPromise<array{a: SyncPromise<int>}>. This is useful because GraphQL queries can return promises and arrays of promises.
- GraphQL schema types (these should only be used in the Query classes)
- GArgs<T> will look at the GraphQL schema and create an array type for the arguments of a field on one of the root types (Query or Mutation)
- GArgs<TType, TField> will create a type for the arguments of a field on a type
- GType<T> will create an type for a specific GraphQL type, input, interface, or enum
- GVal<T> will create a type for the return value of a field on one of the root types (Query or Mutation)
- GVal<TType, TField> will create a type for the return value of a field on a type
- pick<T, TKeys> will create an array from T with only the keys in TKeys (e.g. pick<array{a: int, b: int, c: int}, 'a'|'b'> will create array{a: int, b: int})
- except<T, TKeys> works like pick only it excludes the keys in TKeys (e.g. except<array{a: int, b: int, c: int}, 'a'|'b'> will create array{c: int})
- union<T, U> creates a union array of T and U (e.g. union<array{a: int}, array{b: int}> will create array{a: int, b: int})]]
end,
},
},
{
{
name = 'Repeat On Failure',
role = 'user',
opts = { auto_submit = false },
-- Scope this prompt to the cmd_runner tool
condition = function()
return _G.codecompanion_current_tool == 'cmd_runner'
end,
-- Repeat until the tests pass, as indicated by the testing flag
-- which the cmd_runner tool sets on the chat buffer
repeat_until = function(chat)
-- Check if the last message in the chat buffer contains "[ERROR] found"
local messages = chat.messages
local last_message = messages[#messages]
if last_message and last_message.role == 'assistant' then
local content = last_message.content
return not content:find '[ERROR] found'
end
return true
end,
content = 'PHPStan is still reporting errors. Edit the code to fix the errors and run PHPStan again.',
},
},
},
},
} }
end, end,
dependencies = { dependencies = {

View File

@@ -198,7 +198,8 @@ return {
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/ -- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
local mason_registry = require 'mason-registry' local mason_registry = require 'mason-registry'
local vue_language_server_path = mason_registry.get_package('vue-language-server'):get_install_path() .. '/node_modules/@vue/language-server' local vue_language_server_path = vim.fn.expand '$MASON/packages/vue-language-server/node_modules/@vue/language-server'
print('Vue Language Server Path: ' .. vue_language_server_path)
local servers = { local servers = {
-- clangd = {}, -- clangd = {},
-- gopls = {}, -- gopls = {},
@@ -266,7 +267,7 @@ return {
['language_server_phpstan.enabled'] = false, ['language_server_phpstan.enabled'] = false,
['language_server_psalm.enabled'] = false, ['language_server_psalm.enabled'] = false,
['language_server_php_cs_fixer.enabled'] = true, ['language_server_php_cs_fixer.enabled'] = true,
['language_server_php_cs_fixer.bin'] = '~/.composer/vendor/bin/php-cs-fixer', ['language_server_php_cs_fixer.bin'] = '~/.local/share/composer/vendor/bin/php-cs-fixer',
}, },
}, },
} }