feat: add event-driven architecture with scope resolution

- Add event queue system (queue.lua) with priority-based processing
- Add patch system (patch.lua) with staleness detection via changedtick
- Add confidence scoring (confidence.lua) with 5 weighted heuristics
- Add async worker wrapper (worker.lua) with timeout handling
- Add scheduler (scheduler.lua) with completion-aware injection
- Add Tree-sitter scope resolution (scope.lua) for functions/methods/classes
- Add intent detection (intent.lua) for complete/refactor/fix/add/etc
- Add tag precedence rules (first tag in scope wins)
- Update autocmds to emit events instead of direct processing
- Add scheduler config options (ollama_scout, escalation_threshold)
- Update prompts with scope-aware context
- Update README with emojis and new features
- Update documentation (llms.txt, CHANGELOG.md, doc/codetyper.txt)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-13 21:55:44 -05:00
parent 6268a57498
commit 8a3ee81c3f
28 changed files with 6055 additions and 2687 deletions

View File

@@ -0,0 +1,328 @@
---@mod codetyper.agent.confidence Response confidence scoring
---@brief [[
--- Scores LLM responses using heuristics to decide if escalation is needed.
--- Returns 0.0-1.0 where higher = more confident the response is good.
---@brief ]]
local M = {}
--- Heuristic weights (must sum to 1.0)
M.weights = {
length = 0.15, -- Response length relative to prompt
uncertainty = 0.30, -- Uncertainty phrases
syntax = 0.25, -- Syntax completeness
repetition = 0.15, -- Duplicate lines
truncation = 0.15, -- Incomplete ending
}
--- Uncertainty phrases that indicate low confidence
local uncertainty_phrases = {
-- English
"i'm not sure",
"i am not sure",
"maybe",
"perhaps",
"might work",
"could work",
"not certain",
"uncertain",
"i think",
"possibly",
"TODO",
"FIXME",
"XXX",
"placeholder",
"implement this",
"fill in",
"your code here",
"...", -- Ellipsis as placeholder
"# TODO",
"// TODO",
"-- TODO",
"/* TODO",
}
--- Score based on response length relative to prompt
---@param response string
---@param prompt string
---@return number 0.0-1.0
local function score_length(response, prompt)
local response_len = #response
local prompt_len = #prompt
-- Very short response to long prompt is suspicious
if prompt_len > 50 and response_len < 20 then
return 0.2
end
-- Response should generally be longer than prompt for code generation
local ratio = response_len / math.max(prompt_len, 1)
if ratio < 0.5 then
return 0.3
elseif ratio < 1.0 then
return 0.6
elseif ratio < 2.0 then
return 0.8
else
return 1.0
end
end
--- Score based on uncertainty phrases
---@param response string
---@return number 0.0-1.0
local function score_uncertainty(response)
local lower = response:lower()
local found = 0
for _, phrase in ipairs(uncertainty_phrases) do
if lower:find(phrase:lower(), 1, true) then
found = found + 1
end
end
-- More uncertainty phrases = lower score
if found == 0 then
return 1.0
elseif found == 1 then
return 0.7
elseif found == 2 then
return 0.5
else
return 0.2
end
end
--- Check bracket balance for common languages
---@param response string
---@return boolean balanced
local function check_brackets(response)
local pairs = {
["{"] = "}",
["["] = "]",
["("] = ")",
}
local stack = {}
for char in response:gmatch(".") do
if pairs[char] then
table.insert(stack, pairs[char])
elseif char == "}" or char == "]" or char == ")" then
if #stack == 0 or stack[#stack] ~= char then
return false
end
table.remove(stack)
end
end
return #stack == 0
end
--- Score based on syntax completeness
---@param response string
---@return number 0.0-1.0
local function score_syntax(response)
local score = 1.0
-- Check bracket balance
if not check_brackets(response) then
score = score - 0.4
end
-- Check for common incomplete patterns
-- Lua: unbalanced end/function
local function_count = select(2, response:gsub("function%s*%(", ""))
+ select(2, response:gsub("function%s+%w+%(", ""))
local end_count = select(2, response:gsub("%f[%w]end%f[%W]", ""))
if function_count > end_count + 2 then
score = score - 0.2
end
-- JavaScript/TypeScript: unclosed template literals
local backtick_count = select(2, response:gsub("`", ""))
if backtick_count % 2 ~= 0 then
score = score - 0.2
end
-- String quotes balance
local double_quotes = select(2, response:gsub('"', ""))
local single_quotes = select(2, response:gsub("'", ""))
-- Allow for escaped quotes by being lenient
if double_quotes % 2 ~= 0 and not response:find('\\"') then
score = score - 0.1
end
if single_quotes % 2 ~= 0 and not response:find("\\'") then
score = score - 0.1
end
return math.max(0, score)
end
--- Score based on line repetition
---@param response string
---@return number 0.0-1.0
local function score_repetition(response)
local lines = vim.split(response, "\n", { plain = true })
if #lines < 3 then
return 1.0
end
-- Count duplicate non-empty lines
local seen = {}
local duplicates = 0
for _, line in ipairs(lines) do
local trimmed = vim.trim(line)
if #trimmed > 10 then -- Only check substantial lines
if seen[trimmed] then
duplicates = duplicates + 1
end
seen[trimmed] = true
end
end
local dup_ratio = duplicates / #lines
if dup_ratio < 0.1 then
return 1.0
elseif dup_ratio < 0.2 then
return 0.8
elseif dup_ratio < 0.3 then
return 0.5
else
return 0.2 -- High repetition = degraded output
end
end
--- Score based on truncation indicators
---@param response string
---@return number 0.0-1.0
local function score_truncation(response)
local score = 1.0
-- Ends with ellipsis
if response:match("%.%.%.$") then
score = score - 0.5
end
-- Ends with incomplete comment
if response:match("/%*[^*/]*$") then -- Unclosed /* comment
score = score - 0.4
end
if response:match("<!%-%-[^>]*$") then -- Unclosed HTML comment
score = score - 0.4
end
-- Ends mid-statement (common patterns)
local trimmed = vim.trim(response)
local last_char = trimmed:sub(-1)
-- Suspicious endings
if last_char == "=" or last_char == "," or last_char == "(" then
score = score - 0.3
end
-- Very short last line after long response
local lines = vim.split(response, "\n", { plain = true })
if #lines > 5 then
local last_line = vim.trim(lines[#lines])
if #last_line < 5 and not last_line:match("^[%}%]%)%;end]") then
score = score - 0.2
end
end
return math.max(0, score)
end
---@class ConfidenceBreakdown
---@field length number
---@field uncertainty number
---@field syntax number
---@field repetition number
---@field truncation number
---@field weighted_total number
--- Calculate confidence score for response
---@param response string The LLM response
---@param prompt string The original prompt
---@param context table|nil Additional context (unused for now)
---@return number confidence 0.0-1.0
---@return ConfidenceBreakdown breakdown Individual scores
function M.score(response, prompt, context)
_ = context -- Reserved for future use
if not response or #response == 0 then
return 0, {
length = 0,
uncertainty = 0,
syntax = 0,
repetition = 0,
truncation = 0,
weighted_total = 0,
}
end
local scores = {
length = score_length(response, prompt or ""),
uncertainty = score_uncertainty(response),
syntax = score_syntax(response),
repetition = score_repetition(response),
truncation = score_truncation(response),
}
-- Calculate weighted total
local weighted = 0
for key, weight in pairs(M.weights) do
weighted = weighted + (scores[key] * weight)
end
scores.weighted_total = weighted
return weighted, scores
end
--- Check if response needs escalation
---@param confidence number
---@param threshold number|nil Default: 0.7
---@return boolean needs_escalation
function M.needs_escalation(confidence, threshold)
threshold = threshold or 0.7
return confidence < threshold
end
--- Get human-readable confidence level
---@param confidence number
---@return string
function M.level_name(confidence)
if confidence >= 0.9 then
return "excellent"
elseif confidence >= 0.8 then
return "good"
elseif confidence >= 0.7 then
return "acceptable"
elseif confidence >= 0.5 then
return "uncertain"
else
return "poor"
end
end
--- Format breakdown for logging
---@param breakdown ConfidenceBreakdown
---@return string
function M.format_breakdown(breakdown)
return string.format(
"len:%.2f unc:%.2f syn:%.2f rep:%.2f tru:%.2f = %.2f",
breakdown.length,
breakdown.uncertainty,
breakdown.syntax,
breakdown.repetition,
breakdown.truncation,
breakdown.weighted_total
)
end
return M

View File

@@ -0,0 +1,312 @@
---@mod codetyper.agent.intent Intent detection from prompts
---@brief [[
--- Parses prompt content to determine user intent and target scope.
--- Intents determine how the generated code should be applied.
---@brief ]]
local M = {}
---@class Intent
---@field type string "complete"|"refactor"|"add"|"fix"|"document"|"test"|"explain"|"optimize"
---@field scope_hint string|nil "function"|"class"|"block"|"file"|"selection"|nil
---@field confidence number 0.0-1.0 how confident we are about the intent
---@field action string "replace"|"insert"|"append"|"none"
---@field keywords string[] Keywords that triggered this intent
--- Intent patterns with associated metadata
local intent_patterns = {
-- Complete: fill in missing implementation
complete = {
patterns = {
"complete",
"finish",
"implement",
"fill in",
"fill out",
"stub",
"todo",
"fixme",
},
scope_hint = "function",
action = "replace",
priority = 1,
},
-- Refactor: rewrite existing code
refactor = {
patterns = {
"refactor",
"rewrite",
"restructure",
"reorganize",
"clean up",
"cleanup",
"simplify",
"improve",
},
scope_hint = "function",
action = "replace",
priority = 2,
},
-- Fix: repair bugs or issues
fix = {
patterns = {
"fix",
"repair",
"correct",
"debug",
"solve",
"resolve",
"patch",
"bug",
"error",
"issue",
},
scope_hint = "function",
action = "replace",
priority = 1,
},
-- Add: insert new code
add = {
patterns = {
"add",
"create",
"insert",
"include",
"append",
"new",
"generate",
"write",
},
scope_hint = nil, -- Could be anywhere
action = "insert",
priority = 3,
},
-- Document: add documentation
document = {
patterns = {
"document",
"comment",
"jsdoc",
"docstring",
"describe",
"annotate",
"type hint",
"typehint",
},
scope_hint = "function",
action = "replace", -- Replace with documented version
priority = 2,
},
-- Test: generate tests
test = {
patterns = {
"test",
"spec",
"unit test",
"integration test",
"coverage",
},
scope_hint = "file",
action = "append",
priority = 3,
},
-- Optimize: improve performance
optimize = {
patterns = {
"optimize",
"performance",
"faster",
"efficient",
"speed up",
"reduce",
"minimize",
},
scope_hint = "function",
action = "replace",
priority = 2,
},
-- Explain: provide explanation (no code change)
explain = {
patterns = {
"explain",
"what does",
"how does",
"why",
"describe",
"walk through",
"understand",
},
scope_hint = "function",
action = "none",
priority = 4,
},
}
--- Scope hint patterns
local scope_patterns = {
["this function"] = "function",
["this method"] = "function",
["the function"] = "function",
["the method"] = "function",
["this class"] = "class",
["the class"] = "class",
["this file"] = "file",
["the file"] = "file",
["this block"] = "block",
["the block"] = "block",
["this"] = nil, -- Use Tree-sitter to determine
["here"] = nil,
}
--- Detect intent from prompt content
---@param prompt string The prompt content
---@return Intent
function M.detect(prompt)
local lower = prompt:lower()
local best_match = nil
local best_priority = 999
local matched_keywords = {}
-- Check each intent type
for intent_type, config in pairs(intent_patterns) do
for _, pattern in ipairs(config.patterns) do
if lower:find(pattern, 1, true) then
if config.priority < best_priority then
best_match = intent_type
best_priority = config.priority
matched_keywords = { pattern }
elseif config.priority == best_priority and best_match == intent_type then
table.insert(matched_keywords, pattern)
end
end
end
end
-- Default to "add" if no clear intent
if not best_match then
best_match = "add"
matched_keywords = {}
end
local config = intent_patterns[best_match]
-- Detect scope hint from prompt
local scope_hint = config.scope_hint
for pattern, hint in pairs(scope_patterns) do
if lower:find(pattern, 1, true) then
scope_hint = hint or scope_hint
break
end
end
-- Calculate confidence based on keyword matches
local confidence = 0.5 + (#matched_keywords * 0.15)
confidence = math.min(confidence, 1.0)
return {
type = best_match,
scope_hint = scope_hint,
confidence = confidence,
action = config.action,
keywords = matched_keywords,
}
end
--- Check if intent requires code modification
---@param intent Intent
---@return boolean
function M.modifies_code(intent)
return intent.action ~= "none"
end
--- Check if intent should replace existing code
---@param intent Intent
---@return boolean
function M.is_replacement(intent)
return intent.action == "replace"
end
--- Check if intent adds new code
---@param intent Intent
---@return boolean
function M.is_insertion(intent)
return intent.action == "insert" or intent.action == "append"
end
--- Get system prompt modifier based on intent
---@param intent Intent
---@return string
function M.get_prompt_modifier(intent)
local modifiers = {
complete = [[
You are completing an incomplete function.
Return the complete function with all missing parts filled in.
Keep the existing signature unless changes are required.
Output only the code, no explanations.]],
refactor = [[
You are refactoring existing code.
Improve the code structure while maintaining the same behavior.
Keep the function signature unchanged.
Output only the refactored code, no explanations.]],
fix = [[
You are fixing a bug in the code.
Identify and correct the issue while minimizing changes.
Preserve the original intent of the code.
Output only the fixed code, no explanations.]],
add = [[
You are adding new code.
Follow the existing code style and conventions.
Output only the new code to be inserted, no explanations.]],
document = [[
You are adding documentation to the code.
Add appropriate comments/docstrings for the function.
Include parameter types, return types, and description.
Output the complete function with documentation.]],
test = [[
You are generating tests for the code.
Create comprehensive unit tests covering edge cases.
Follow the testing conventions of the project.
Output only the test code, no explanations.]],
optimize = [[
You are optimizing code for performance.
Improve efficiency while maintaining correctness.
Document any significant algorithmic changes.
Output only the optimized code, no explanations.]],
explain = [[
You are explaining code to a developer.
Provide a clear, concise explanation of what the code does.
Include information about the algorithm and any edge cases.
Do not output code, only explanation.]],
}
return modifiers[intent.type] or modifiers.add
end
--- Format intent for logging
---@param intent Intent
---@return string
function M.format(intent)
return string.format(
"%s (scope: %s, action: %s, confidence: %.2f)",
intent.type,
intent.scope_hint or "auto",
intent.action,
intent.confidence
)
end
return M

View File

@@ -0,0 +1,478 @@
---@mod codetyper.agent.patch Patch system with staleness detection
---@brief [[
--- Manages code patches with buffer snapshots for staleness detection.
--- Patches are queued for safe injection when completion popup is not visible.
---@brief ]]
local M = {}
---@class BufferSnapshot
---@field bufnr number Buffer number
---@field changedtick number vim.b.changedtick at snapshot time
---@field content_hash string Hash of buffer content in range
---@field range {start_line: number, end_line: number}|nil Range snapshotted
---@class PatchCandidate
---@field id string Unique patch ID
---@field event_id string Related PromptEvent ID
---@field target_bufnr number Target buffer for injection
---@field target_path string Target file path
---@field original_snapshot BufferSnapshot Snapshot at event creation
---@field generated_code string Code to inject
---@field injection_range {start_line: number, end_line: number}|nil
---@field injection_strategy string "append"|"replace"|"insert"
---@field confidence number Confidence score (0.0-1.0)
---@field status string "pending"|"applied"|"stale"|"rejected"
---@field created_at number Timestamp
---@field applied_at number|nil When applied
--- Patch storage
---@type PatchCandidate[]
local patches = {}
--- Patch ID counter
local patch_counter = 0
--- Generate unique patch ID
---@return string
function M.generate_id()
patch_counter = patch_counter + 1
return string.format("patch_%d_%d", os.time(), patch_counter)
end
--- Hash buffer content in range
---@param bufnr number
---@param start_line number|nil 1-indexed, nil for whole buffer
---@param end_line number|nil 1-indexed, nil for whole buffer
---@return string
local function hash_buffer_range(bufnr, start_line, end_line)
if not vim.api.nvim_buf_is_valid(bufnr) then
return ""
end
local lines
if start_line and end_line then
lines = vim.api.nvim_buf_get_lines(bufnr, start_line - 1, end_line, false)
else
lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
end
local content = table.concat(lines, "\n")
local hash = 0
for i = 1, #content do
hash = (hash * 31 + string.byte(content, i)) % 2147483647
end
return string.format("%x", hash)
end
--- Take a snapshot of buffer state
---@param bufnr number Buffer number
---@param range {start_line: number, end_line: number}|nil Optional range
---@return BufferSnapshot
function M.snapshot_buffer(bufnr, range)
local changedtick = 0
if vim.api.nvim_buf_is_valid(bufnr) then
changedtick = vim.api.nvim_buf_get_var(bufnr, "changedtick") or vim.b[bufnr].changedtick or 0
end
local content_hash
if range then
content_hash = hash_buffer_range(bufnr, range.start_line, range.end_line)
else
content_hash = hash_buffer_range(bufnr, nil, nil)
end
return {
bufnr = bufnr,
changedtick = changedtick,
content_hash = content_hash,
range = range,
}
end
--- Check if buffer changed since snapshot
---@param snapshot BufferSnapshot
---@return boolean is_stale
---@return string|nil reason
function M.is_snapshot_stale(snapshot)
if not vim.api.nvim_buf_is_valid(snapshot.bufnr) then
return true, "buffer_invalid"
end
-- Check changedtick first (fast path)
local current_tick = vim.api.nvim_buf_get_var(snapshot.bufnr, "changedtick")
or vim.b[snapshot.bufnr].changedtick or 0
if current_tick ~= snapshot.changedtick then
-- Changedtick differs, but might be just cursor movement
-- Verify with content hash
local current_hash
if snapshot.range then
current_hash = hash_buffer_range(
snapshot.bufnr,
snapshot.range.start_line,
snapshot.range.end_line
)
else
current_hash = hash_buffer_range(snapshot.bufnr, nil, nil)
end
if current_hash ~= snapshot.content_hash then
return true, "content_changed"
end
end
return false, nil
end
--- Check if a patch is stale
---@param patch PatchCandidate
---@return boolean
---@return string|nil reason
function M.is_stale(patch)
return M.is_snapshot_stale(patch.original_snapshot)
end
--- Queue a patch for deferred application
---@param patch PatchCandidate
---@return PatchCandidate
function M.queue_patch(patch)
patch.id = patch.id or M.generate_id()
patch.status = patch.status or "pending"
patch.created_at = patch.created_at or os.time()
table.insert(patches, patch)
-- Log patch creation
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "patch",
message = string.format(
"Patch queued: %s (confidence: %.2f)",
patch.id, patch.confidence or 0
),
data = {
patch_id = patch.id,
event_id = patch.event_id,
target_path = patch.target_path,
code_preview = patch.generated_code:sub(1, 50),
},
})
end)
return patch
end
--- Create patch from event and response
---@param event table PromptEvent
---@param generated_code string
---@param confidence number
---@param strategy string|nil Injection strategy (overrides intent-based)
---@return PatchCandidate
function M.create_from_event(event, generated_code, confidence, strategy)
-- Get target buffer
local target_bufnr = vim.fn.bufnr(event.target_path)
if target_bufnr == -1 then
-- Try to find by filename
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(buf)
if name == event.target_path then
target_bufnr = buf
break
end
end
end
-- Take snapshot of the scope range in target buffer (for staleness detection)
local snapshot_range = event.scope_range or event.range
local snapshot = M.snapshot_buffer(
target_bufnr ~= -1 and target_bufnr or event.bufnr,
snapshot_range
)
-- Determine injection strategy and range based on intent
local injection_strategy = strategy
local injection_range = nil
if not injection_strategy and event.intent then
local intent_mod = require("codetyper.agent.intent")
if intent_mod.is_replacement(event.intent) then
injection_strategy = "replace"
-- Use scope range for replacement
if event.scope_range then
injection_range = event.scope_range
end
elseif event.intent.action == "insert" then
injection_strategy = "insert"
-- Insert at prompt location
injection_range = { start_line = event.range.start_line, end_line = event.range.start_line }
elseif event.intent.action == "append" then
injection_strategy = "append"
-- Will append to end of file
else
injection_strategy = "append"
end
end
injection_strategy = injection_strategy or "append"
return {
id = M.generate_id(),
event_id = event.id,
target_bufnr = target_bufnr,
target_path = event.target_path,
original_snapshot = snapshot,
generated_code = generated_code,
injection_range = injection_range,
injection_strategy = injection_strategy,
confidence = confidence,
status = "pending",
created_at = os.time(),
intent = event.intent,
scope = event.scope,
}
end
--- Get all pending patches
---@return PatchCandidate[]
function M.get_pending()
local pending = {}
for _, patch in ipairs(patches) do
if patch.status == "pending" then
table.insert(pending, patch)
end
end
return pending
end
--- Get patch by ID
---@param id string
---@return PatchCandidate|nil
function M.get(id)
for _, patch in ipairs(patches) do
if patch.id == id then
return patch
end
end
return nil
end
--- Get patches for event
---@param event_id string
---@return PatchCandidate[]
function M.get_for_event(event_id)
local result = {}
for _, patch in ipairs(patches) do
if patch.event_id == event_id then
table.insert(result, patch)
end
end
return result
end
--- Mark patch as applied
---@param id string
---@return boolean
function M.mark_applied(id)
local patch = M.get(id)
if patch then
patch.status = "applied"
patch.applied_at = os.time()
return true
end
return false
end
--- Mark patch as stale
---@param id string
---@param reason string|nil
---@return boolean
function M.mark_stale(id, reason)
local patch = M.get(id)
if patch then
patch.status = "stale"
patch.stale_reason = reason
return true
end
return false
end
--- Mark patch as rejected
---@param id string
---@param reason string|nil
---@return boolean
function M.mark_rejected(id, reason)
local patch = M.get(id)
if patch then
patch.status = "rejected"
patch.reject_reason = reason
return true
end
return false
end
--- Apply a patch to the target buffer
---@param patch PatchCandidate
---@return boolean success
---@return string|nil error
function M.apply(patch)
-- Check staleness first
local is_stale, stale_reason = M.is_stale(patch)
if is_stale then
M.mark_stale(patch.id, stale_reason)
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "warning",
message = string.format("Patch %s is stale: %s", patch.id, stale_reason or "unknown"),
})
end)
return false, "patch_stale: " .. (stale_reason or "unknown")
end
-- Ensure target buffer is valid
local target_bufnr = patch.target_bufnr
if target_bufnr == -1 or not vim.api.nvim_buf_is_valid(target_bufnr) then
-- Try to load buffer from path
target_bufnr = vim.fn.bufadd(patch.target_path)
if target_bufnr == 0 then
M.mark_rejected(patch.id, "buffer_not_found")
return false, "target buffer not found"
end
vim.fn.bufload(target_bufnr)
patch.target_bufnr = target_bufnr
end
-- Prepare code lines
local code_lines = vim.split(patch.generated_code, "\n", { plain = true })
-- Apply based on strategy
local ok, err = pcall(function()
if patch.injection_strategy == "replace" and patch.injection_range then
-- Replace specific range
vim.api.nvim_buf_set_lines(
target_bufnr,
patch.injection_range.start_line - 1,
patch.injection_range.end_line,
false,
code_lines
)
elseif patch.injection_strategy == "insert" and patch.injection_range then
-- Insert at specific line
vim.api.nvim_buf_set_lines(
target_bufnr,
patch.injection_range.start_line - 1,
patch.injection_range.start_line - 1,
false,
code_lines
)
else
-- Default: append to end
local line_count = vim.api.nvim_buf_line_count(target_bufnr)
vim.api.nvim_buf_set_lines(target_bufnr, line_count, line_count, false, code_lines)
end
end)
if not ok then
M.mark_rejected(patch.id, err)
return false, err
end
M.mark_applied(patch.id)
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "success",
message = string.format("Patch %s applied successfully", patch.id),
data = {
target_path = patch.target_path,
lines_added = #code_lines,
},
})
end)
return true, nil
end
--- Flush all pending patches that are safe to apply
---@return number applied_count
---@return number stale_count
function M.flush_pending()
local applied = 0
local stale = 0
for _, patch in ipairs(patches) do
if patch.status == "pending" then
local success, _ = M.apply(patch)
if success then
applied = applied + 1
else
stale = stale + 1
end
end
end
return applied, stale
end
--- Cancel all pending patches for a buffer
---@param bufnr number
---@return number cancelled_count
function M.cancel_for_buffer(bufnr)
local cancelled = 0
for _, patch in ipairs(patches) do
if patch.status == "pending" and
(patch.target_bufnr == bufnr or patch.original_snapshot.bufnr == bufnr) then
patch.status = "cancelled"
cancelled = cancelled + 1
end
end
return cancelled
end
--- Cleanup old patches
---@param max_age number Max age in seconds (default: 300)
function M.cleanup(max_age)
max_age = max_age or 300
local now = os.time()
local i = 1
while i <= #patches do
local patch = patches[i]
if patch.status ~= "pending" and (now - patch.created_at) > max_age then
table.remove(patches, i)
else
i = i + 1
end
end
end
--- Get statistics
---@return table
function M.stats()
local stats = {
total = #patches,
pending = 0,
applied = 0,
stale = 0,
rejected = 0,
cancelled = 0,
}
for _, patch in ipairs(patches) do
local s = patch.status
if stats[s] then
stats[s] = stats[s] + 1
end
end
return stats
end
--- Clear all patches
function M.clear()
patches = {}
end
return M

View File

@@ -0,0 +1,438 @@
---@mod codetyper.agent.queue Event queue for prompt processing
---@brief [[
--- Priority queue system for PromptEvents with observer pattern.
--- Events are processed by priority (1=high, 2=normal, 3=low).
---@brief ]]
local M = {}
---@class PromptEvent
---@field id string Unique event ID
---@field bufnr number Source buffer number
---@field range {start_line: number, end_line: number} Line range of prompt tag
---@field timestamp number os.clock() timestamp
---@field changedtick number Buffer changedtick snapshot
---@field content_hash string Hash of prompt region
---@field prompt_content string Cleaned prompt text
---@field target_path string Target file for injection
---@field priority number Priority (1=high, 2=normal, 3=low)
---@field status string "pending"|"processing"|"completed"|"escalated"|"cancelled"
---@field attempt_count number Number of processing attempts
---@field worker_type string|nil LLM provider used ("ollama"|"claude"|etc)
---@field created_at number System time when created
---@field intent Intent|nil Detected intent from prompt
---@field scope ScopeInfo|nil Resolved scope (function/class/file)
---@field scope_text string|nil Text of the resolved scope
---@field scope_range {start_line: number, end_line: number}|nil Range of scope in target
--- Internal state
---@type PromptEvent[]
local queue = {}
--- Event listeners (observer pattern)
---@type function[]
local listeners = {}
--- Event ID counter
local event_counter = 0
--- Generate unique event ID
---@return string
function M.generate_id()
event_counter = event_counter + 1
return string.format("evt_%d_%d", os.time(), event_counter)
end
--- Simple hash function for content
---@param content string
---@return string
function M.hash_content(content)
local hash = 0
for i = 1, #content do
hash = (hash * 31 + string.byte(content, i)) % 2147483647
end
return string.format("%x", hash)
end
--- Notify all listeners of queue change
---@param event_type string "enqueue"|"dequeue"|"update"|"cancel"
---@param event PromptEvent|nil The affected event
local function notify_listeners(event_type, event)
for _, listener in ipairs(listeners) do
pcall(listener, event_type, event, #queue)
end
end
--- Add event listener
---@param callback function(event_type: string, event: PromptEvent|nil, queue_size: number)
---@return number Listener ID for removal
function M.add_listener(callback)
table.insert(listeners, callback)
return #listeners
end
--- Remove event listener
---@param listener_id number
function M.remove_listener(listener_id)
if listener_id > 0 and listener_id <= #listeners then
table.remove(listeners, listener_id)
end
end
--- Compare events for priority sorting
---@param a PromptEvent
---@param b PromptEvent
---@return boolean
local function compare_priority(a, b)
-- Lower priority number = higher priority
if a.priority ~= b.priority then
return a.priority < b.priority
end
-- Same priority: older events first (FIFO)
return a.timestamp < b.timestamp
end
--- Check if two events are in the same scope
---@param a PromptEvent
---@param b PromptEvent
---@return boolean
local function same_scope(a, b)
-- Same buffer
if a.target_path ~= b.target_path then
return false
end
-- Both have scope ranges
if a.scope_range and b.scope_range then
-- Check if ranges overlap
return a.scope_range.start_line <= b.scope_range.end_line
and b.scope_range.start_line <= a.scope_range.end_line
end
-- Fallback: check if prompt ranges are close (within 10 lines)
if a.range and b.range then
local distance = math.abs(a.range.start_line - b.range.start_line)
return distance < 10
end
return false
end
--- Find conflicting events in the same scope
---@param event PromptEvent
---@return PromptEvent[] Conflicting pending events
function M.find_conflicts(event)
local conflicts = {}
for _, existing in ipairs(queue) do
if existing.status == "pending" and existing.id ~= event.id then
if same_scope(event, existing) then
table.insert(conflicts, existing)
end
end
end
return conflicts
end
--- Check if an event should be skipped due to conflicts (first tag wins)
---@param event PromptEvent
---@return boolean should_skip
---@return string|nil reason
function M.check_precedence(event)
local conflicts = M.find_conflicts(event)
for _, conflict in ipairs(conflicts) do
-- First (older) tag wins
if conflict.timestamp < event.timestamp then
return true, string.format(
"Skipped: earlier tag in same scope (event %s)",
conflict.id
)
end
end
return false, nil
end
--- Insert event maintaining priority order
---@param event PromptEvent
local function insert_sorted(event)
local pos = #queue + 1
for i, existing in ipairs(queue) do
if compare_priority(event, existing) then
pos = i
break
end
end
table.insert(queue, pos, event)
end
--- Enqueue a new event
---@param event PromptEvent
---@return PromptEvent The enqueued event with generated ID if missing
function M.enqueue(event)
-- Ensure required fields
event.id = event.id or M.generate_id()
event.timestamp = event.timestamp or os.clock()
event.created_at = event.created_at or os.time()
event.status = event.status or "pending"
event.priority = event.priority or 2
event.attempt_count = event.attempt_count or 0
-- Generate content hash if not provided
if not event.content_hash and event.prompt_content then
event.content_hash = M.hash_content(event.prompt_content)
end
insert_sorted(event)
notify_listeners("enqueue", event)
-- Log to agent logs if available
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "queue",
message = string.format("Event queued: %s (priority: %d)", event.id, event.priority),
data = {
event_id = event.id,
bufnr = event.bufnr,
prompt_preview = event.prompt_content:sub(1, 50),
},
})
end)
return event
end
--- Dequeue highest priority pending event
---@return PromptEvent|nil
function M.dequeue()
for i, event in ipairs(queue) do
if event.status == "pending" then
event.status = "processing"
notify_listeners("dequeue", event)
return event
end
end
return nil
end
--- Peek at next pending event without removing
---@return PromptEvent|nil
function M.peek()
for _, event in ipairs(queue) do
if event.status == "pending" then
return event
end
end
return nil
end
--- Get event by ID
---@param id string
---@return PromptEvent|nil
function M.get(id)
for _, event in ipairs(queue) do
if event.id == id then
return event
end
end
return nil
end
--- Update event status
---@param id string
---@param status string
---@param extra table|nil Additional fields to update
---@return boolean Success
function M.update_status(id, status, extra)
for _, event in ipairs(queue) do
if event.id == id then
event.status = status
if extra then
for k, v in pairs(extra) do
event[k] = v
end
end
notify_listeners("update", event)
return true
end
end
return false
end
--- Mark event as completed
---@param id string
---@return boolean
function M.complete(id)
return M.update_status(id, "completed")
end
--- Mark event as escalated (needs remote LLM)
---@param id string
---@return boolean
function M.escalate(id)
local event = M.get(id)
if event then
event.status = "escalated"
event.attempt_count = event.attempt_count + 1
-- Re-queue as pending with same priority
event.status = "pending"
notify_listeners("update", event)
return true
end
return false
end
--- Cancel all events for a buffer
---@param bufnr number
---@return number Number of cancelled events
function M.cancel_for_buffer(bufnr)
local cancelled = 0
for _, event in ipairs(queue) do
if event.bufnr == bufnr and event.status == "pending" then
event.status = "cancelled"
cancelled = cancelled + 1
notify_listeners("cancel", event)
end
end
return cancelled
end
--- Cancel event by ID
---@param id string
---@return boolean
function M.cancel(id)
return M.update_status(id, "cancelled")
end
--- Get all pending events
---@return PromptEvent[]
function M.get_pending()
local pending = {}
for _, event in ipairs(queue) do
if event.status == "pending" then
table.insert(pending, event)
end
end
return pending
end
--- Get all processing events
---@return PromptEvent[]
function M.get_processing()
local processing = {}
for _, event in ipairs(queue) do
if event.status == "processing" then
table.insert(processing, event)
end
end
return processing
end
--- Get queue size (all events)
---@return number
function M.size()
return #queue
end
--- Get count of pending events
---@return number
function M.pending_count()
local count = 0
for _, event in ipairs(queue) do
if event.status == "pending" then
count = count + 1
end
end
return count
end
--- Get count of processing events
---@return number
function M.processing_count()
local count = 0
for _, event in ipairs(queue) do
if event.status == "processing" then
count = count + 1
end
end
return count
end
--- Check if queue is empty (no pending events)
---@return boolean
function M.is_empty()
return M.pending_count() == 0
end
--- Clear all events (optionally filter by status)
---@param status string|nil Status to clear, or nil for all
function M.clear(status)
if status then
local i = 1
while i <= #queue do
if queue[i].status == status then
table.remove(queue, i)
else
i = i + 1
end
end
else
queue = {}
end
notify_listeners("update", nil)
end
--- Cleanup completed/cancelled events older than max_age seconds
---@param max_age number Maximum age in seconds (default: 300)
function M.cleanup(max_age)
max_age = max_age or 300
local now = os.time()
local i = 1
while i <= #queue do
local event = queue[i]
if (event.status == "completed" or event.status == "cancelled")
and (now - event.created_at) > max_age then
table.remove(queue, i)
else
i = i + 1
end
end
end
--- Get queue statistics
---@return table
function M.stats()
local stats = {
total = #queue,
pending = 0,
processing = 0,
completed = 0,
cancelled = 0,
escalated = 0,
}
for _, event in ipairs(queue) do
local s = event.status
if stats[s] then
stats[s] = stats[s] + 1
end
end
return stats
end
--- Debug: dump queue contents
---@return string
function M.dump()
local lines = { "Queue contents:" }
for i, event in ipairs(queue) do
table.insert(lines, string.format(
" %d. [%s] %s (p:%d, status:%s, attempts:%d)",
i, event.id,
event.prompt_content:sub(1, 30):gsub("\n", " "),
event.priority, event.status, event.attempt_count
))
end
return table.concat(lines, "\n")
end
return M

View File

@@ -0,0 +1,488 @@
---@mod codetyper.agent.scheduler Event scheduler with completion-awareness
---@brief [[
--- Central orchestrator for the event-driven system.
--- Handles dispatch, escalation, and completion-safe injection.
---@brief ]]
local M = {}
local queue = require("codetyper.agent.queue")
local patch = require("codetyper.agent.patch")
local worker = require("codetyper.agent.worker")
local confidence_mod = require("codetyper.agent.confidence")
--- Scheduler state
local state = {
running = false,
timer = nil,
poll_interval = 100, -- ms
paused = false,
config = {
enabled = true,
ollama_scout = true,
escalation_threshold = 0.7,
max_concurrent = 2,
completion_delay_ms = 100,
remote_provider = "claude", -- Default fallback provider
},
}
--- Autocommand group for injection timing
local augroup = nil
--- Check if completion popup is visible
---@return boolean
function M.is_completion_visible()
-- Check native popup menu
if vim.fn.pumvisible() == 1 then
return true
end
-- Check nvim-cmp
local ok, cmp = pcall(require, "cmp")
if ok and cmp.visible and cmp.visible() then
return true
end
-- Check coq_nvim
local coq_ok, coq = pcall(require, "coq")
if coq_ok and coq and type(coq.visible) == "function" and coq.visible() then
return true
end
return false
end
--- Check if we're in insert mode
---@return boolean
function M.is_insert_mode()
local mode = vim.fn.mode()
return mode == "i" or mode == "ic" or mode == "ix"
end
--- Check if it's safe to inject code
---@return boolean
---@return string|nil reason if not safe
function M.is_safe_to_inject()
if M.is_completion_visible() then
return false, "completion_visible"
end
if M.is_insert_mode() then
return false, "insert_mode"
end
return true, nil
end
--- Get the provider for escalation
---@return string
local function get_remote_provider()
local ok, codetyper = pcall(require, "codetyper")
if ok then
local config = codetyper.get_config()
if config and config.llm and config.llm.provider then
-- If current provider is ollama, use configured remote
if config.llm.provider == "ollama" then
-- Check which remote provider is configured
if config.llm.claude and config.llm.claude.api_key then
return "claude"
elseif config.llm.openai and config.llm.openai.api_key then
return "openai"
elseif config.llm.gemini and config.llm.gemini.api_key then
return "gemini"
elseif config.llm.copilot then
return "copilot"
end
end
return config.llm.provider
end
end
return state.config.remote_provider
end
--- Get the primary provider (ollama if scout enabled, else configured)
---@return string
local function get_primary_provider()
if state.config.ollama_scout then
return "ollama"
end
local ok, codetyper = pcall(require, "codetyper")
if ok then
local config = codetyper.get_config()
if config and config.llm and config.llm.provider then
return config.llm.provider
end
end
return "claude"
end
--- Process worker result and decide next action
---@param event table PromptEvent
---@param result table WorkerResult
local function handle_worker_result(event, result)
if not result.success then
-- Failed - try escalation if this was ollama
if result.worker_type == "ollama" and event.attempt_count < 2 then
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "info",
message = string.format(
"Escalating event %s to remote provider (ollama failed)",
event.id
),
})
end)
event.attempt_count = event.attempt_count + 1
event.status = "pending"
event.worker_type = get_remote_provider()
return
end
-- Mark as failed
queue.update_status(event.id, "failed", { error = result.error })
return
end
-- Success - check confidence
local needs_escalation = confidence_mod.needs_escalation(
result.confidence,
state.config.escalation_threshold
)
if needs_escalation and result.worker_type == "ollama" and event.attempt_count < 2 then
-- Low confidence from ollama - escalate to remote
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "info",
message = string.format(
"Escalating event %s to remote provider (confidence: %.2f < %.2f)",
event.id, result.confidence, state.config.escalation_threshold
),
})
end)
event.attempt_count = event.attempt_count + 1
event.status = "pending"
event.worker_type = get_remote_provider()
return
end
-- Good enough or final attempt - create patch
local p = patch.create_from_event(event, result.response, result.confidence)
patch.queue_patch(p)
queue.complete(event.id)
-- Schedule patch application
M.schedule_patch_flush()
end
--- Dispatch next event from queue
local function dispatch_next()
if state.paused then
return
end
-- Check concurrent limit
if worker.active_count() >= state.config.max_concurrent then
return
end
-- Get next pending event
local event = queue.dequeue()
if not event then
return
end
-- Check for precedence conflicts (multiple tags in same scope)
local should_skip, skip_reason = queue.check_precedence(event)
if should_skip then
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "warning",
message = string.format("Event %s skipped: %s", event.id, skip_reason or "conflict"),
})
end)
queue.cancel(event.id)
-- Try next event
return dispatch_next()
end
-- Determine which provider to use
local provider = event.worker_type or get_primary_provider()
-- Log dispatch with intent/scope info
pcall(function()
local logs = require("codetyper.agent.logs")
local intent_info = event.intent and event.intent.type or "unknown"
local scope_info = event.scope and event.scope.type ~= "file"
and string.format("%s:%s", event.scope.type, event.scope.name or "anon")
or "file"
logs.add({
type = "info",
message = string.format(
"Dispatching %s [intent: %s, scope: %s, provider: %s]",
event.id, intent_info, scope_info, provider
),
})
end)
-- Create worker
worker.create(event, provider, function(result)
vim.schedule(function()
handle_worker_result(event, result)
end)
end)
end
--- Schedule patch flush after delay (completion safety)
function M.schedule_patch_flush()
vim.defer_fn(function()
local safe, reason = M.is_safe_to_inject()
if safe then
local applied, stale = patch.flush_pending()
if applied > 0 or stale > 0 then
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "info",
message = string.format("Patches flushed: %d applied, %d stale", applied, stale),
})
end)
end
else
-- Not safe yet, reschedule
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "debug",
message = string.format("Patch flush deferred: %s", reason or "unknown"),
})
end)
-- Will be retried on next InsertLeave or CursorHold
end
end, state.config.completion_delay_ms)
end
--- Main scheduler loop
local function scheduler_loop()
if not state.running then
return
end
dispatch_next()
-- Cleanup old items periodically
if math.random() < 0.01 then -- ~1% chance each tick
queue.cleanup(300)
patch.cleanup(300)
end
-- Schedule next tick
state.timer = vim.defer_fn(scheduler_loop, state.poll_interval)
end
--- Setup autocommands for injection timing
local function setup_autocmds()
if augroup then
pcall(vim.api.nvim_del_augroup_by_id, augroup)
end
augroup = vim.api.nvim_create_augroup("CodetypeScheduler", { clear = true })
-- Flush patches when leaving insert mode
vim.api.nvim_create_autocmd("InsertLeave", {
group = augroup,
callback = function()
vim.defer_fn(function()
if not M.is_completion_visible() then
patch.flush_pending()
end
end, state.config.completion_delay_ms)
end,
desc = "Flush pending patches on InsertLeave",
})
-- Flush patches on cursor hold
vim.api.nvim_create_autocmd("CursorHold", {
group = augroup,
callback = function()
if not M.is_insert_mode() and not M.is_completion_visible() then
patch.flush_pending()
end
end,
desc = "Flush pending patches on CursorHold",
})
-- Cancel patches when buffer changes significantly
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
callback = function(ev)
-- Mark relevant patches as potentially stale
-- They'll be checked on next flush attempt
end,
desc = "Check patch staleness on save",
})
-- Cleanup when buffer is deleted
vim.api.nvim_create_autocmd("BufDelete", {
group = augroup,
callback = function(ev)
queue.cancel_for_buffer(ev.buf)
patch.cancel_for_buffer(ev.buf)
worker.cancel_for_event(ev.buf)
end,
desc = "Cleanup on buffer delete",
})
end
--- Start the scheduler
---@param config table|nil Configuration overrides
function M.start(config)
if state.running then
return
end
-- Merge config
if config then
for k, v in pairs(config) do
state.config[k] = v
end
end
-- Load config from codetyper if available
pcall(function()
local codetyper = require("codetyper")
local ct_config = codetyper.get_config()
if ct_config and ct_config.scheduler then
for k, v in pairs(ct_config.scheduler) do
state.config[k] = v
end
end
end)
if not state.config.enabled then
return
end
state.running = true
state.paused = false
-- Setup autocmds
setup_autocmds()
-- Add queue listener
queue.add_listener(function(event_type, event, queue_size)
if event_type == "enqueue" and not state.paused then
-- New event - try to dispatch immediately
vim.schedule(dispatch_next)
end
end)
-- Start main loop
scheduler_loop()
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "info",
message = "Scheduler started",
data = {
ollama_scout = state.config.ollama_scout,
escalation_threshold = state.config.escalation_threshold,
max_concurrent = state.config.max_concurrent,
},
})
end)
end
--- Stop the scheduler
function M.stop()
state.running = false
if state.timer then
pcall(function()
if type(state.timer) == "userdata" and state.timer.stop then
state.timer:stop()
end
end)
state.timer = nil
end
if augroup then
pcall(vim.api.nvim_del_augroup_by_id, augroup)
augroup = nil
end
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "info",
message = "Scheduler stopped",
})
end)
end
--- Pause the scheduler (don't process new events)
function M.pause()
state.paused = true
end
--- Resume the scheduler
function M.resume()
state.paused = false
vim.schedule(dispatch_next)
end
--- Check if scheduler is running
---@return boolean
function M.is_running()
return state.running
end
--- Check if scheduler is paused
---@return boolean
function M.is_paused()
return state.paused
end
--- Get scheduler status
---@return table
function M.status()
return {
running = state.running,
paused = state.paused,
queue_stats = queue.stats(),
patch_stats = patch.stats(),
active_workers = worker.active_count(),
config = vim.deepcopy(state.config),
}
end
--- Manually trigger dispatch
function M.dispatch()
if state.running and not state.paused then
dispatch_next()
end
end
--- Force flush all pending patches (ignores completion check)
function M.force_flush()
return patch.flush_pending()
end
--- Update configuration
---@param config table
function M.configure(config)
for k, v in pairs(config) do
state.config[k] = v
end
end
return M

View File

@@ -0,0 +1,444 @@
---@mod codetyper.agent.scope Tree-sitter scope resolution
---@brief [[
--- Resolves semantic scope for prompts using Tree-sitter.
--- Finds the smallest enclosing function/method/block for a given position.
---@brief ]]
local M = {}
---@class ScopeInfo
---@field type string "function"|"method"|"class"|"block"|"file"|"unknown"
---@field node_type string Tree-sitter node type
---@field range {start_row: number, start_col: number, end_row: number, end_col: number}
---@field text string The full text of the scope
---@field name string|nil Name of the function/class if available
--- Node types that represent function-like scopes per language
local function_nodes = {
-- Lua
["function_declaration"] = "function",
["function_definition"] = "function",
["local_function"] = "function",
["function"] = "function",
-- JavaScript/TypeScript
["function_declaration"] = "function",
["function_expression"] = "function",
["arrow_function"] = "function",
["method_definition"] = "method",
["function"] = "function",
-- Python
["function_definition"] = "function",
["async_function_definition"] = "function",
-- Go
["function_declaration"] = "function",
["method_declaration"] = "method",
-- Rust
["function_item"] = "function",
["impl_item"] = "method",
-- Ruby
["method"] = "method",
["singleton_method"] = "method",
-- Java/C#
["method_declaration"] = "method",
["constructor_declaration"] = "method",
-- C/C++
["function_definition"] = "function",
}
--- Node types that represent class-like scopes
local class_nodes = {
["class_declaration"] = "class",
["class_definition"] = "class",
["class"] = "class",
["struct_item"] = "class",
["impl_item"] = "class",
["interface_declaration"] = "class",
["module"] = "class",
}
--- Node types that represent block scopes
local block_nodes = {
["block"] = "block",
["statement_block"] = "block",
["compound_statement"] = "block",
["do_block"] = "block",
}
--- Check if Tree-sitter is available for buffer
---@param bufnr number
---@return boolean
function M.has_treesitter(bufnr)
local ok, parsers = pcall(require, "nvim-treesitter.parsers")
if not ok then
return false
end
local lang = parsers.get_buf_lang(bufnr)
if not lang then
return false
end
return parsers.has_parser(lang)
end
--- Get Tree-sitter node at position
---@param bufnr number
---@param row number 0-indexed
---@param col number 0-indexed
---@return TSNode|nil
local function get_node_at_pos(bufnr, row, col)
local ok, ts_utils = pcall(require, "nvim-treesitter.ts_utils")
if not ok then
return nil
end
-- Try to get the node at the cursor position
local node = ts_utils.get_node_at_cursor()
if node then
return node
end
-- Fallback: get root and find node
local parser = vim.treesitter.get_parser(bufnr)
if not parser then
return nil
end
local tree = parser:parse()[1]
if not tree then
return nil
end
local root = tree:root()
return root:named_descendant_for_range(row, col, row, col)
end
--- Find enclosing scope node of specific types
---@param node TSNode
---@param node_types table<string, string>
---@return TSNode|nil, string|nil scope_type
local function find_enclosing_scope(node, node_types)
local current = node
while current do
local node_type = current:type()
if node_types[node_type] then
return current, node_types[node_type]
end
current = current:parent()
end
return nil, nil
end
--- Extract function/method name from node
---@param node TSNode
---@param bufnr number
---@return string|nil
local function get_scope_name(node, bufnr)
-- Try to find name child node
local name_node = node:field("name")[1]
if name_node then
return vim.treesitter.get_node_text(name_node, bufnr)
end
-- Try identifier child
for child in node:iter_children() do
if child:type() == "identifier" or child:type() == "property_identifier" then
return vim.treesitter.get_node_text(child, bufnr)
end
end
return nil
end
--- Resolve scope at position using Tree-sitter
---@param bufnr number Buffer number
---@param row number 1-indexed line number
---@param col number 1-indexed column number
---@return ScopeInfo
function M.resolve_scope(bufnr, row, col)
-- Default to file scope
local default_scope = {
type = "file",
node_type = "file",
range = {
start_row = 1,
start_col = 0,
end_row = vim.api.nvim_buf_line_count(bufnr),
end_col = 0,
},
text = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), "\n"),
name = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":t"),
}
-- Check if Tree-sitter is available
if not M.has_treesitter(bufnr) then
-- Fall back to heuristic-based scope resolution
return M.resolve_scope_heuristic(bufnr, row, col) or default_scope
end
-- Convert to 0-indexed for Tree-sitter
local ts_row = row - 1
local ts_col = col - 1
-- Get node at position
local node = get_node_at_pos(bufnr, ts_row, ts_col)
if not node then
return default_scope
end
-- Try to find function scope first
local scope_node, scope_type = find_enclosing_scope(node, function_nodes)
-- If no function, try class
if not scope_node then
scope_node, scope_type = find_enclosing_scope(node, class_nodes)
end
-- If no class, try block
if not scope_node then
scope_node, scope_type = find_enclosing_scope(node, block_nodes)
end
if not scope_node then
return default_scope
end
-- Get range (convert back to 1-indexed)
local start_row, start_col, end_row, end_col = scope_node:range()
-- Get text
local text = vim.treesitter.get_node_text(scope_node, bufnr)
-- Get name
local name = get_scope_name(scope_node, bufnr)
return {
type = scope_type,
node_type = scope_node:type(),
range = {
start_row = start_row + 1,
start_col = start_col,
end_row = end_row + 1,
end_col = end_col,
},
text = text,
name = name,
}
end
--- Heuristic fallback for scope resolution (no Tree-sitter)
---@param bufnr number
---@param row number 1-indexed
---@param col number 1-indexed
---@return ScopeInfo|nil
function M.resolve_scope_heuristic(bufnr, row, col)
_ = col -- unused in heuristic
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local filetype = vim.bo[bufnr].filetype
-- Language-specific function patterns
local patterns = {
lua = {
start = "^%s*local%s+function%s+",
start_alt = "^%s*function%s+",
ending = "^%s*end%s*$",
},
python = {
start = "^%s*def%s+",
start_alt = "^%s*async%s+def%s+",
ending = nil, -- Python uses indentation
},
javascript = {
start = "^%s*function%s+",
start_alt = "^%s*const%s+%w+%s*=%s*",
ending = "^%s*}%s*$",
},
typescript = {
start = "^%s*function%s+",
start_alt = "^%s*const%s+%w+%s*=%s*",
ending = "^%s*}%s*$",
},
}
local lang_patterns = patterns[filetype]
if not lang_patterns then
return nil
end
-- Find function start (search backwards)
local start_line = nil
for i = row, 1, -1 do
local line = lines[i]
if line:match(lang_patterns.start) or
(lang_patterns.start_alt and line:match(lang_patterns.start_alt)) then
start_line = i
break
end
end
if not start_line then
return nil
end
-- Find function end
local end_line = nil
if lang_patterns.ending then
-- Brace/end based languages
local depth = 0
for i = start_line, #lines do
local line = lines[i]
-- Count braces or end keywords
if filetype == "lua" then
if line:match("function") or line:match("if") or line:match("for") or line:match("while") then
depth = depth + 1
end
if line:match("^%s*end") then
depth = depth - 1
if depth <= 0 then
end_line = i
break
end
end
else
-- JavaScript/TypeScript brace counting
for _ in line:gmatch("{") do depth = depth + 1 end
for _ in line:gmatch("}") do depth = depth - 1 end
if depth <= 0 and i > start_line then
end_line = i
break
end
end
end
else
-- Python: use indentation
local base_indent = #(lines[start_line]:match("^%s*") or "")
for i = start_line + 1, #lines do
local line = lines[i]
if line:match("^%s*$") then
goto continue
end
local indent = #(line:match("^%s*") or "")
if indent <= base_indent then
end_line = i - 1
break
end
::continue::
end
end_line = end_line or #lines
end
if not end_line then
end_line = #lines
end
-- Extract text
local scope_lines = {}
for i = start_line, end_line do
table.insert(scope_lines, lines[i])
end
-- Try to extract function name
local name = nil
local first_line = lines[start_line]
name = first_line:match("function%s+([%w_]+)") or
first_line:match("def%s+([%w_]+)") or
first_line:match("const%s+([%w_]+)")
return {
type = "function",
node_type = "heuristic",
range = {
start_row = start_line,
start_col = 0,
end_row = end_line,
end_col = #lines[end_line],
},
text = table.concat(scope_lines, "\n"),
name = name,
}
end
--- Get scope for the current cursor position
---@return ScopeInfo
function M.resolve_scope_at_cursor()
local bufnr = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
return M.resolve_scope(bufnr, cursor[1], cursor[2] + 1)
end
--- Check if position is inside a function/method
---@param bufnr number
---@param row number 1-indexed
---@param col number 1-indexed
---@return boolean
function M.is_in_function(bufnr, row, col)
local scope = M.resolve_scope(bufnr, row, col)
return scope.type == "function" or scope.type == "method"
end
--- Get all functions in buffer
---@param bufnr number
---@return ScopeInfo[]
function M.get_all_functions(bufnr)
local functions = {}
if not M.has_treesitter(bufnr) then
return functions
end
local parser = vim.treesitter.get_parser(bufnr)
if not parser then
return functions
end
local tree = parser:parse()[1]
if not tree then
return functions
end
local root = tree:root()
-- Query for all function nodes
local lang = parser:lang()
local query_string = [[
(function_declaration) @func
(function_definition) @func
(method_definition) @func
(arrow_function) @func
]]
local ok, query = pcall(vim.treesitter.query.parse, lang, query_string)
if not ok then
return functions
end
for _, node in query:iter_captures(root, bufnr, 0, -1) do
local start_row, start_col, end_row, end_col = node:range()
local text = vim.treesitter.get_node_text(node, bufnr)
local name = get_scope_name(node, bufnr)
table.insert(functions, {
type = function_nodes[node:type()] or "function",
node_type = node:type(),
range = {
start_row = start_row + 1,
start_col = start_col,
end_row = end_row + 1,
end_col = end_col,
},
text = text,
name = name,
})
end
return functions
end
return M

View File

@@ -97,6 +97,23 @@ function M.to_claude_format()
return tools
end
--- Convert tool definitions to OpenAI API format
---@return table[] Tools in OpenAI's expected format
function M.to_openai_format()
local tools = {}
for _, tool in pairs(M.definitions) do
table.insert(tools, {
type = "function",
["function"] = {
name = tool.name,
description = tool.description,
parameters = tool.parameters,
},
})
end
return tools
end
--- Convert tool definitions to prompt format for Ollama
---@return string Formatted tool descriptions for system prompt
function M.to_prompt_format()

View File

@@ -0,0 +1,419 @@
---@mod codetyper.agent.worker Async LLM worker wrapper
---@brief [[
--- Wraps LLM clients with timeout handling and confidence scoring.
--- Provides unified interface for scheduler to dispatch work.
---@brief ]]
local M = {}
local confidence = require("codetyper.agent.confidence")
---@class WorkerResult
---@field success boolean Whether the request succeeded
---@field response string|nil The generated code
---@field error string|nil Error message if failed
---@field confidence number Confidence score (0.0-1.0)
---@field confidence_breakdown table Detailed confidence breakdown
---@field duration number Time taken in seconds
---@field worker_type string LLM provider used
---@field usage table|nil Token usage if available
---@class Worker
---@field id string Worker ID
---@field event table PromptEvent being processed
---@field worker_type string LLM provider type
---@field status string "pending"|"running"|"completed"|"failed"|"timeout"
---@field start_time number Start timestamp
---@field timeout_ms number Timeout in milliseconds
---@field timer any Timeout timer handle
---@field callback function Result callback
--- Worker ID counter
local worker_counter = 0
--- Active workers
---@type table<string, Worker>
local active_workers = {}
--- Default timeouts by provider type
local default_timeouts = {
ollama = 30000, -- 30s for local
claude = 60000, -- 60s for remote
openai = 60000,
gemini = 60000,
copilot = 60000,
}
--- Generate worker ID
---@return string
local function generate_id()
worker_counter = worker_counter + 1
return string.format("worker_%d_%d", os.time(), worker_counter)
end
--- Get LLM client by type
---@param worker_type string
---@return table|nil client
---@return string|nil error
local function get_client(worker_type)
local ok, client = pcall(require, "codetyper.llm." .. worker_type)
if ok and client then
return client, nil
end
return nil, "Unknown provider: " .. worker_type
end
--- Build prompt for code generation
---@param event table PromptEvent
---@return string prompt
---@return table context
local function build_prompt(event)
local intent_mod = require("codetyper.agent.intent")
-- Get target file content for context
local target_content = ""
if event.target_path then
local ok, lines = pcall(function()
return vim.fn.readfile(event.target_path)
end)
if ok and lines then
target_content = table.concat(lines, "\n")
end
end
local filetype = vim.fn.fnamemodify(event.target_path or "", ":e")
-- Build context with scope information
local context = {
target_path = event.target_path,
target_content = target_content,
filetype = filetype,
scope = event.scope,
scope_text = event.scope_text,
scope_range = event.scope_range,
intent = event.intent,
}
-- Build the actual prompt based on intent and scope
local system_prompt = ""
local user_prompt = event.prompt_content
if event.intent then
system_prompt = intent_mod.get_prompt_modifier(event.intent)
end
-- If we have a scope (function/method), include it in the prompt
if event.scope_text and event.scope and event.scope.type ~= "file" then
local scope_type = event.scope.type
local scope_name = event.scope.name or "anonymous"
-- For replacement intents, provide the full scope to transform
if event.intent and intent_mod.is_replacement(event.intent) then
user_prompt = string.format(
[[Here is a %s named "%s" in a %s file:
```%s
%s
```
User request: %s
Return the complete transformed %s. Output only code, no explanations.]],
scope_type,
scope_name,
filetype,
filetype,
event.scope_text,
event.prompt_content,
scope_type
)
else
-- For insertion intents, provide context
user_prompt = string.format(
[[Context - this code is inside a %s named "%s":
```%s
%s
```
User request: %s
Output only the code to insert, no explanations.]],
scope_type,
scope_name,
filetype,
event.scope_text,
event.prompt_content
)
end
else
-- No scope resolved, use full file context
user_prompt = string.format(
[[File: %s (%s)
```%s
%s
```
User request: %s
Output only code, no explanations.]],
vim.fn.fnamemodify(event.target_path or "", ":t"),
filetype,
filetype,
target_content:sub(1, 4000), -- Limit context size
event.prompt_content
)
end
context.system_prompt = system_prompt
context.formatted_prompt = user_prompt
return user_prompt, context
end
--- Create and start a worker
---@param event table PromptEvent
---@param worker_type string LLM provider type
---@param callback function(result: WorkerResult)
---@return Worker
function M.create(event, worker_type, callback)
local worker = {
id = generate_id(),
event = event,
worker_type = worker_type,
status = "pending",
start_time = os.clock(),
timeout_ms = default_timeouts[worker_type] or 60000,
callback = callback,
}
active_workers[worker.id] = worker
-- Log worker creation
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "worker",
message = string.format("Worker %s started (%s)", worker.id, worker_type),
data = {
worker_id = worker.id,
event_id = event.id,
provider = worker_type,
},
})
end)
-- Start the work
M.start(worker)
return worker
end
--- Start worker execution
---@param worker Worker
function M.start(worker)
worker.status = "running"
-- Set up timeout
worker.timer = vim.defer_fn(function()
if worker.status == "running" then
worker.status = "timeout"
active_workers[worker.id] = nil
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "warning",
message = string.format("Worker %s timed out after %dms", worker.id, worker.timeout_ms),
})
end)
worker.callback({
success = false,
response = nil,
error = "timeout",
confidence = 0,
confidence_breakdown = {},
duration = (os.clock() - worker.start_time),
worker_type = worker.worker_type,
})
end
end, worker.timeout_ms)
-- Get client and execute
local client, client_err = get_client(worker.worker_type)
if not client then
M.complete(worker, nil, client_err)
return
end
local prompt, context = build_prompt(worker.event)
-- Call the LLM
client.generate(prompt, context, function(response, err, usage)
-- Cancel timeout timer
if worker.timer then
pcall(function()
-- Timer might have already fired
if type(worker.timer) == "userdata" and worker.timer.stop then
worker.timer:stop()
end
end)
end
if worker.status ~= "running" then
return -- Already timed out or cancelled
end
M.complete(worker, response, err, usage)
end)
end
--- Complete worker execution
---@param worker Worker
---@param response string|nil
---@param error string|nil
---@param usage table|nil
function M.complete(worker, response, error, usage)
local duration = os.clock() - worker.start_time
if error then
worker.status = "failed"
active_workers[worker.id] = nil
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "error",
message = string.format("Worker %s failed: %s", worker.id, error),
})
end)
worker.callback({
success = false,
response = nil,
error = error,
confidence = 0,
confidence_breakdown = {},
duration = duration,
worker_type = worker.worker_type,
usage = usage,
})
return
end
-- Score confidence
local conf_score, breakdown = confidence.score(response, worker.event.prompt_content)
worker.status = "completed"
active_workers[worker.id] = nil
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "success",
message = string.format(
"Worker %s completed (%.2fs, confidence: %.2f - %s)",
worker.id, duration, conf_score, confidence.level_name(conf_score)
),
data = {
confidence_breakdown = confidence.format_breakdown(breakdown),
usage = usage,
},
})
end)
worker.callback({
success = true,
response = response,
error = nil,
confidence = conf_score,
confidence_breakdown = breakdown,
duration = duration,
worker_type = worker.worker_type,
usage = usage,
})
end
--- Cancel a worker
---@param worker_id string
---@return boolean
function M.cancel(worker_id)
local worker = active_workers[worker_id]
if not worker then
return false
end
if worker.timer then
pcall(function()
if type(worker.timer) == "userdata" and worker.timer.stop then
worker.timer:stop()
end
end)
end
worker.status = "cancelled"
active_workers[worker_id] = nil
pcall(function()
local logs = require("codetyper.agent.logs")
logs.add({
type = "info",
message = string.format("Worker %s cancelled", worker_id),
})
end)
return true
end
--- Get active worker count
---@return number
function M.active_count()
local count = 0
for _ in pairs(active_workers) do
count = count + 1
end
return count
end
--- Get all active workers
---@return Worker[]
function M.get_active()
local workers = {}
for _, worker in pairs(active_workers) do
table.insert(workers, worker)
end
return workers
end
--- Check if worker exists and is running
---@param worker_id string
---@return boolean
function M.is_running(worker_id)
local worker = active_workers[worker_id]
return worker ~= nil and worker.status == "running"
end
--- Cancel all workers for an event
---@param event_id string
---@return number cancelled_count
function M.cancel_for_event(event_id)
local cancelled = 0
for id, worker in pairs(active_workers) do
if worker.event.id == event_id then
M.cancel(id)
cancelled = cancelled + 1
end
end
return cancelled
end
--- Set timeout for worker type
---@param worker_type string
---@param timeout_ms number
function M.set_timeout(worker_type, timeout_ms)
default_timeouts[worker_type] = timeout_ms
end
return M