mirror of
https://github.com/mark3labs/kit.git
synced 2026-06-13 19:20:06 +00:00
e8e99b19a8
* Remove dead code: 5 unused symbols across internal packages
- internal/models: LoadModelSettingsFromConfig (zero refs)
- internal/prompts: PromptTemplate.ExpandWithArgs (zero refs)
- internal/app: NewMessageStore (tests migrated to NewMessageStoreWithMessages)
- internal/config: HasEnvVars (+ its test)
- internal/core: ContextWithSudoPassword (test migrated to context.WithValue)
* pkg/kit: use TreeManager alias in exported signatures
NewTreeManagerAdapter and InitTreeSession now spell their signatures with
the public kit.TreeManager alias instead of internal/session.TreeManager,
so go doc renders domain types rather than internal paths.
* Consolidate tool-kind classification into internal/extensions
coreToolKinds + toolKindFor were duplicated verbatim in
internal/extensions/wrapper.go and pkg/kit/events.go, risking silent
divergence between extension events and SDK events. Single source of
truth now lives in internal/extensions/toolkinds.go; pkg/kit re-exports
the constants.
* Consolidate Anthropic OAuth detection and usage-tracker refresh
The 'is the active Anthropic credential a stored OAuth token' check was
copy-pasted at 5 sites, all prefix-matching the magic string
'stored OAuth' produced in internal/auth. Now:
- internal/auth: new CredentialSourceOAuth constant + IsAnthropicOAuth()
- internal/ui: new UpdateUsageTrackerForModel(); CreateUsageTracker and
SetupCLI share lookupTrackableModel (SetupCLI no longer re-inlines the
tracker construction)
- cmd/root.go + cmd/extension_context.go: verbatim-duplicated tracker
refresh blocks replaced with ui.UpdateUsageTrackerForModel
- pkg/kit isAnthropicOAuth delegates to auth.IsAnthropicOAuth
- internal/models compares source against the constant
* pkg/kit: consolidate model-path helpers and argument tokenizer
- ExtractModelFromPath mis-parsed model IDs containing '/' (e.g.
'openrouter/meta/llama' -> 'meta'); it now delegates to
RemoveProviderFromModel and is deprecated alongside
ExtractProviderFromPath (-> GetCurrentProvider)
- parseFields delegated to prompts.ParseCommandArgs so extension argument
parsing and builtin prompt-template parsing share one quote/escape
grammar; ParseCommandArgs now also splits on tabs (superset of both
previous tokenizers)
* Unify the two {{variable}} template engines
internal/skills and pkg/kit/template_bridge each had their own grammar:
skills rejected '{{ name }}' (whitespace) but allowed digit-first names;
the bridge was the opposite. A template behaved differently depending on
whether it was loaded as a skill prompt or via the extension API.
internal/skills is now the single engine using the superset grammar
(\{\{\s*(\w+)\s*\}\}); pkg/kit ParseTemplate/RenderTemplate are thin
adapters over it. Expand is now regex-based so whitespace placeholders
expand consistently; missing variables are still left as-is.
* internal/ui: extract switchModel helper for model-switch flow
The model-selector handler (ModelSelectedMsg) and /model slash command
duplicated the full switch sequence (thinking-level fallback, setModel,
display-state update, preference persistence, ModelChange emit) and had
already drifted in ordering. Both now call a single switchModel method.
Display state is still updated directly (no prog.Send from Update).
* extbridge: extract shared BaseContext for extension wiring
cmd/extension_context.go and internal/acpserver/session.go each built a
giant extensions.Context literal, duplicating ~15 delegation closures
(GetContextStats, GetMessages, AppendEntry, options, SetModel core,
Complete, SpawnSubagent, ...) that had to be kept in sync by hand. New
data-access fields had to be wired in both places or ACP-mode extensions
silently got nil function fields.
extbridge.BaseContext now provides the headless half; both call sites
overlay only their UI-specific closures. As a side effect ACP mode gains
previously-missing APIs (state, tree navigation, skills, template
parsing, model resolution) that were nil before. The interactive TUI
keeps its exact SetModel/ReloadExtensions ordering via overrides.
* internal/tools: extract withOAuthRetry and marshalToolResult helpers
ExecuteTool repeated the OAuth-error/re-auth/retry stanza verbatim twice
(sync and task-augmented paths) and the marshal-and-wrap stanza four
times. Both are now single helpers with identical error strings, so a
fix to OAuth retry or error categorization applies everywhere at once.
* internal/ui: extract buildShareFile with defer-based cleanup
handleShareCommand repeated the close/remove/print/return cleanup chain
four times across its temp-file write error paths. File assembly now
lives in buildShareFile with a single deferred cleanup on error.
* cmd: extract flag validation, preference restore, and provider-URL routing from runNormalMode
runNormalMode opened with ~150 lines of policy logic (flag-combination
validation, persisted model/thinking-level preference restoration, and
two subtle --provider-url model-rewrite rules). These are now standalone
functions (validateModeFlags, restorePersistedPreferences,
applyProviderURLRouting) so the routing policy is independently readable
and testable. Behaviour unchanged; ordering preserved.
* fix: address review findings on SDK godoc and nil guard
- pkg/kit: remove internal package paths from exported godoc on
ParseTemplate and the ToolKind* constants (SDK doc surface must not
reference internal packages)
- internal/tools: guard marshalToolResult against a nil CallToolResult
(json.Marshal(nil) succeeds as 'null', then result.IsError panics if
a client returns nil result with nil error)
Skipped the TreeNode Children deep-copy suggestion: the slice already
comes from TreeManager.GetChildren which returns a fresh copy per call
into a throwaway intermediate, so no internal state is exposed.
222 lines
6.8 KiB
Go
222 lines
6.8 KiB
Go
package extensions
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"charm.land/fantasy"
|
|
)
|
|
|
|
// WrapToolsWithExtensions wraps each tool so that ToolCall and ToolResult
|
|
// events are emitted through the extension runner before and after execution.
|
|
|
|
// If the runner has no relevant handlers the original tools are returned
|
|
// unchanged (zero overhead).
|
|
func WrapToolsWithExtensions(tools []fantasy.AgentTool, runner *Runner) []fantasy.AgentTool {
|
|
if runner == nil {
|
|
return tools
|
|
}
|
|
// Always wrap tools through the runner so that SetActiveTools
|
|
// (disabled-tool checking) and event handlers both work. The
|
|
// overhead for disabled-tool checking is a single map lookup
|
|
// per tool call, which is negligible.
|
|
wrapped := make([]fantasy.AgentTool, len(tools))
|
|
for i, tool := range tools {
|
|
wrapped[i] = &wrappedTool{inner: tool, runner: runner}
|
|
}
|
|
return wrapped
|
|
}
|
|
|
|
// ExtensionToolsAsLLMTools converts ToolDef values registered by extensions
|
|
// into LLM agent tool implementations so the LLM can invoke them.
|
|
// The runner is optional; if provided, ToolContext.OnProgress routes
|
|
// progress messages through the runner's Print function.
|
|
func ExtensionToolsAsLLMTools(defs []ToolDef, runner *Runner) []fantasy.AgentTool {
|
|
tools := make([]fantasy.AgentTool, 0, len(defs))
|
|
for _, def := range defs {
|
|
tools = append(tools, &extensionTool{def: def, runner: runner})
|
|
}
|
|
return tools
|
|
}
|
|
|
|
// parseToolArgsJSON attempts to parse JSON-encoded tool args into a map.
|
|
// Returns nil on failure (non-fatal convenience parsing).
|
|
func parseToolArgsJSON(input string) map[string]any {
|
|
var parsed map[string]any
|
|
if json.Unmarshal([]byte(input), &parsed) == nil {
|
|
return parsed
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// wrappedTool — intercepts tool calls through the extension runner
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type wrappedTool struct {
|
|
inner fantasy.AgentTool
|
|
runner *Runner
|
|
}
|
|
|
|
func (w *wrappedTool) Info() fantasy.ToolInfo { return w.inner.Info() }
|
|
func (w *wrappedTool) ProviderOptions() fantasy.ProviderOptions { return w.inner.ProviderOptions() }
|
|
func (w *wrappedTool) SetProviderOptions(o fantasy.ProviderOptions) { w.inner.SetProviderOptions(o) }
|
|
|
|
func (w *wrappedTool) Run(ctx context.Context, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
toolName := w.inner.Info().Name
|
|
|
|
// 0. Check if tool is disabled via SetActiveTools.
|
|
if w.runner.IsToolDisabled(toolName) {
|
|
return fantasy.NewTextErrorResponse(
|
|
fmt.Sprintf("Error: tool %q is currently disabled", toolName)), nil
|
|
}
|
|
|
|
kind := ToolKindFor(toolName)
|
|
|
|
// 1. Emit ToolCall — extensions can block execution.
|
|
if w.runner.HasHandlers(ToolCall) {
|
|
result, _ := w.runner.Emit(ToolCallEvent{
|
|
ToolName: toolName,
|
|
ToolCallID: call.ID,
|
|
ToolKind: kind,
|
|
Input: call.Input,
|
|
ParsedArgs: parseToolArgsJSON(call.Input),
|
|
Source: "llm",
|
|
})
|
|
if r, ok := result.(ToolCallResult); ok && r.Block {
|
|
reason := r.Reason
|
|
if reason == "" {
|
|
reason = "blocked by extension"
|
|
}
|
|
return fantasy.NewTextErrorResponse(fmt.Sprintf("Error: %s", reason)), nil
|
|
}
|
|
}
|
|
|
|
// 2. Emit ToolExecutionStart.
|
|
if w.runner.HasHandlers(ToolExecutionStart) {
|
|
_, _ = w.runner.Emit(ToolExecutionStartEvent{ToolCallID: call.ID, ToolName: toolName, ToolKind: kind})
|
|
}
|
|
|
|
// 3. Execute the actual tool.
|
|
resp, err := w.inner.Run(ctx, call)
|
|
|
|
// 4. Emit ToolExecutionEnd.
|
|
if w.runner.HasHandlers(ToolExecutionEnd) {
|
|
_, _ = w.runner.Emit(ToolExecutionEndEvent{ToolCallID: call.ID, ToolName: toolName, ToolKind: kind})
|
|
}
|
|
|
|
// 5. Emit ToolResult — extensions can modify output.
|
|
if w.runner.HasHandlers(ToolResult) {
|
|
result, _ := w.runner.Emit(ToolResultEvent{
|
|
ToolCallID: call.ID,
|
|
ToolName: toolName,
|
|
ToolKind: kind,
|
|
Input: call.Input,
|
|
Content: resp.Content,
|
|
IsError: err != nil || resp.IsError,
|
|
Metadata: resp.Metadata,
|
|
})
|
|
if r, ok := result.(ToolResultResult); ok {
|
|
if r.Content != nil {
|
|
resp.Content = *r.Content
|
|
}
|
|
if r.IsError != nil {
|
|
resp.IsError = *r.IsError
|
|
}
|
|
}
|
|
}
|
|
|
|
return resp, err
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// extensionTool — wraps a ToolDef into an LLM agent tool
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type extensionTool struct {
|
|
def ToolDef
|
|
runner *Runner // optional; enables ToolContext.OnProgress
|
|
providerOptions fantasy.ProviderOptions
|
|
}
|
|
|
|
func (t *extensionTool) Info() fantasy.ToolInfo {
|
|
info := fantasy.ToolInfo{
|
|
Name: t.def.Name,
|
|
Description: t.def.Description,
|
|
}
|
|
|
|
// Parse the extension's JSON Schema and extract the properties map.
|
|
// Fantasy expects Parameters to contain property definitions directly
|
|
// (e.g. {"command": {"type":"string"}}) and wraps them into a full
|
|
// JSON Schema object internally. If the extension provides a full
|
|
// schema with "type":"object" and "properties", we extract just the
|
|
// properties. Required fields are also extracted if present.
|
|
if t.def.Parameters != "" {
|
|
var schema map[string]any
|
|
if err := json.Unmarshal([]byte(t.def.Parameters), &schema); err == nil {
|
|
if props, ok := schema["properties"].(map[string]any); ok {
|
|
info.Parameters = props
|
|
} else {
|
|
// Schema doesn't have "properties" — use as-is (may be
|
|
// a flat property map already matching the expected format).
|
|
info.Parameters = schema
|
|
}
|
|
// Extract required fields if present.
|
|
if req, ok := schema["required"].([]any); ok {
|
|
for _, r := range req {
|
|
if s, ok := r.(string); ok {
|
|
info.Required = append(info.Required, s)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ensure Parameters and Required are never nil — the OpenAI Responses API
|
|
// rejects tools where these fields serialize to JSON null instead of
|
|
// empty object/array.
|
|
if info.Parameters == nil {
|
|
info.Parameters = map[string]any{}
|
|
}
|
|
if info.Required == nil {
|
|
info.Required = []string{}
|
|
}
|
|
|
|
return info
|
|
}
|
|
|
|
func (t *extensionTool) ProviderOptions() fantasy.ProviderOptions { return t.providerOptions }
|
|
func (t *extensionTool) SetProviderOptions(o fantasy.ProviderOptions) { t.providerOptions = o }
|
|
|
|
func (t *extensionTool) Run(ctx context.Context, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
var result string
|
|
var err error
|
|
|
|
if t.def.ExecuteWithContext != nil {
|
|
tc := ToolContext{
|
|
IsCancelled: func() bool {
|
|
return ctx.Err() != nil
|
|
},
|
|
OnProgress: func(text string) {
|
|
if t.runner != nil {
|
|
t.runner.mu.RLock()
|
|
printFn := t.runner.ctx.Print
|
|
t.runner.mu.RUnlock()
|
|
if printFn != nil {
|
|
printFn(text)
|
|
}
|
|
}
|
|
},
|
|
}
|
|
result, err = t.def.ExecuteWithContext(call.Input, tc)
|
|
} else {
|
|
result, err = t.def.Execute(call.Input)
|
|
}
|
|
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
return fantasy.NewTextResponse(result), nil
|
|
}
|