coder

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

Package coder 提供从 opencode 迁移的编码类工具(bash / edit / view / write / patch / glob / grep / ls / fetch / sourcegraph / diagnostics)。

迁移约定:

  • 全部实现 gobrave 的 tool.Tool(Definition + Execute),可直接注册进 tool.Registry;
  • 工具的依赖(文件历史 / LSP 客户端 / 工作目录 / 会话 ID)在执行期从 ctx 读取, 因此工具本身无状态,可在注册表里单例复用;
  • 权限请求与 opencode internal/llm/tools 对齐:工具在真正读写 / 执行前自行调用 tool.RequirePermission,由 ctx 中的 PermissionRequester 桥接到 Runtime。

Index

Constants

View Source
const (
	BashToolName = "bash"

	DefaultTimeout  = 1 * 60 * 1000  // 1 minute in milliseconds
	MaxTimeout      = 10 * 60 * 1000 // 10 minutes in milliseconds
	MaxOutputLength = 30000
)
View Source
const (
	LSToolName = "ls"
	MaxLSFiles = 1000
)
View Source
const (
	ViewToolName     = "view"
	MaxReadSize      = 250 * 1024
	DefaultReadLimit = 2000
	MaxLineLength    = 2000
)
View Source
const (
	DiagnosticsToolName = "diagnostics"
)
View Source
const (
	EditToolName = "edit"
)
View Source
const (
	FetchToolName = "fetch"
)
View Source
const (
	GlobToolName = "glob"
)
View Source
const (
	GrepToolName = "grep"
)
View Source
const (
	PatchToolName = "patch"
)
View Source
const (
	SourcegraphToolName = "sourcegraph"
)
View Source
const (
	WriteToolName = "write"
)

Variables

This section is empty.

Functions

func All

func All(deps Deps) []tool.Tool

All 返回全部 coder 工具。新增工具时在此追加即可自动接入各 Provider。 All 返回全部 coder 工具。静态依赖(文件历史 / LSP)由 container 在构建期注入。

func NewBash

func NewBash(deps Deps) tool.Tool

NewBash 返回 bash 工具(持久 shell 会话中执行命令)。

实际执行位置由 container.bash_execution_mode 决定:host(默认)走宿主机持久 shell, container 走用户的容器模板终端(container.bash_container_template_id)。Agent 对此 无感知:工具入参与返回结构完全一致,不需要传入任何容器相关信息。

func NewDiagnostics

func NewDiagnostics(deps Deps) tool.Tool

NewDiagnostics 返回 diagnostics 工具(读取 LSP 诊断结果)。

func NewEdit

func NewEdit(deps Deps) tool.Tool

NewEdit 返回 edit 工具。

func NewFetch

func NewFetch(deps Deps) tool.Tool

NewFetch 返回 fetch 工具。

出站请求复用 config 中的代理配置(proxy.http_proxy / proxy.https_proxy / proxy.no_proxy);未配置时直连,不读取环境变量。

func NewGlob

func NewGlob() tool.Tool

NewGlob 返回 glob 工具。

func NewGrep

func NewGrep() tool.Tool

NewGrep 返回 grep 工具。

func NewLs

func NewLs() tool.Tool

NewLs 返回 ls 工具。

func NewPatch

func NewPatch(deps Deps) tool.Tool

NewPatch 返回 patch 工具。

func NewSourcegraph

func NewSourcegraph() tool.Tool

NewSourcegraph 返回 sourcegraph 工具。

func NewView

func NewView(deps Deps) tool.Tool

NewView 返回 view 工具(带行号读取文件并附带 LSP 诊断)。

func NewWrite

func NewWrite(deps Deps) tool.Tool

NewWrite 返回 write 工具。

Types

type BashOutput

type BashOutput struct {
	Output   string `json:"output"`
	Metadata struct {
		StartTime int64 `json:"start_time"`
		EndTime   int64 `json:"end_time"`
	} `json:"metadata"`
}

BashOutput 是 bash 工具的结果(回传给模型)。

type BashParams

type BashParams struct {
	Command string `json:"command"`
	Timeout int    `json:"timeout"`
}

BashParams 是 bash 工具的入参。

type BashPermissionsParams

type BashPermissionsParams struct {
	Command string `json:"command"`
	Timeout int    `json:"timeout"`
}

BashPermissionsParams 是 bash 工具授权请求携带的参数(对齐 opencode)。

type Deps

type Deps struct {
	// History 是会话内文件版本历史服务(edit / write / patch 记录版本)。
	History agentinterfaces.HistoryService
	// LSP 是 LSP 客户端提供者(diagnostics / view / edit / write / patch 读取诊断)。
	//
	// 以「提供者」而不是客户端 map 注入:Clients() 在每次执行时取当前快照,
	// 避免工具在构造期就把客户端集合固化下来。
	LSP LSPClientProvider
	// Config 是进程配置快照,供需要出站网络的工具(如 fetch)读取代理设置。
	// 为 nil 或未配置代理时直连(不读取环境变量)。
	Config *config.Config
	// ContainerShell 是容器终端的最小服务(bash 在 container 模式下使用)。
	//
	// 为 nil 时 bash 一律在宿主机执行:仓库级单元测试等没有容器运行时的场景
	// 不必装配它。bash 用哪种执行位置由 container.bash_execution_mode 决定。
	ContainerShell hostshell.ContainerShellService
}

Deps 是 coder 工具集的进程级静态依赖,由 container 在构建工具注册表时一次性装配。

与 builtin.Deps 一样,这些依赖通过构造函数注入,而不是每次执行时从 context 读取: 工具依赖对编译器可见、可单测,provider 侧也无需维护逐能力的注入清单。

type DiagnosticsParams

type DiagnosticsParams struct {
	FilePath string `json:"file_path"`
}

DiagnosticsParams 是 diagnostics 工具的入参。

type EditParams

type EditParams struct {
	FilePath  string `json:"file_path"`
	OldString string `json:"old_string"`
	NewString string `json:"new_string"`
}

type EditPermissionsParams

type EditPermissionsParams struct {
	FilePath string `json:"file_path"`
	Diff     string `json:"diff"`
}

type EditResponseMetadata

type EditResponseMetadata struct {
	Diff      string `json:"diff"`
	Additions int    `json:"additions"`
	Removals  int    `json:"removals"`
}

type FetchParams

type FetchParams struct {
	URL     string `json:"url"`
	Format  string `json:"format"`
	Timeout int    `json:"timeout,omitempty"`
}

type FileReadStatus

type FileReadStatus struct {
	// Read 表示本进程内是否读取过该文件(进程重启后为 false)。
	Read bool
	// Modified 表示读取之后文件内容被外部改动过。
	Modified bool
	// LastRead 是最后一次读取时间(未读过时为零值)。
	LastRead time.Time
	// ModTime 是文件当前的修改时间(mtime 可能因 NFS 服务端时钟而超前)。
	ModTime time.Time
	// Detail 是判定细节,便于日志 / 排障。
	Detail string
}

FileReadStatus 描述文件相对"最后一次读取"的状态。

func CheckFileRead

func CheckFileRead(path string, info os.FileInfo) FileReadStatus

CheckFileRead 判断 path 是否已读过且读后未被改动。

info 为调用方已有的 os.FileInfo,可为 nil(内部自行 Stat)。 判定优先使用内容快照(size + sha256),因此不受 NFS mtime 时钟偏差影响; 快照不可用(文件过大 / 读取失败)时退化为 size + 带容忍窗口的 mtime 比较。

type GlobParams

type GlobParams struct {
	Pattern string `json:"pattern"`
	Path    string `json:"path"`
}

type GlobResponseMetadata

type GlobResponseMetadata struct {
	NumberOfFiles int  `json:"number_of_files"`
	Truncated     bool `json:"truncated"`
}

type GrepParams

type GrepParams struct {
	Pattern     string `json:"pattern"`
	Path        string `json:"path"`
	Include     string `json:"include"`
	LiteralText bool   `json:"literal_text"`
}

type GrepResponseMetadata

type GrepResponseMetadata struct {
	NumberOfMatches int  `json:"number_of_matches"`
	Truncated       bool `json:"truncated"`
}

type LSPClientProvider

type LSPClientProvider interface {
	Clients() map[string]*lsp.Client
}

LSPClientProvider 抽象「提供当前 LSP 客户端集合」的能力。

*lspmanager.Manager 直接满足该接口,避免 coder 包依赖 manager 的具体类型。

type LSParams

type LSParams struct {
	Path   string   `json:"path"`
	Ignore []string `json:"ignore"`
}

type LSResponseMetadata

type LSResponseMetadata struct {
	NumberOfFiles int  `json:"number_of_files"`
	Truncated     bool `json:"truncated"`
}

type LineScanner

type LineScanner struct {
	// contains filtered or unexported fields
}

func NewLineScanner

func NewLineScanner(r io.Reader) *LineScanner

func (*LineScanner) Err

func (s *LineScanner) Err() error

func (*LineScanner) Scan

func (s *LineScanner) Scan() bool

func (*LineScanner) Text

func (s *LineScanner) Text() string

type PatchParams

type PatchParams struct {
	PatchText string `json:"patch_text"`
}

type PatchResponseMetadata

type PatchResponseMetadata struct {
	FilesChanged []string `json:"files_changed"`
	Additions    int      `json:"additions"`
	Removals     int      `json:"removals"`
}

type SourcegraphParams

type SourcegraphParams struct {
	Query         string `json:"query"`
	Count         int    `json:"count,omitempty"`
	ContextWindow int    `json:"context_window,omitempty"`
	Timeout       int    `json:"timeout,omitempty"`
}

type SourcegraphResponseMetadata

type SourcegraphResponseMetadata struct {
	NumberOfMatches int  `json:"number_of_matches"`
	Truncated       bool `json:"truncated"`
}

type ToolResponse

type ToolResponse struct {
	// Content 回传给模型的文本。
	Content string
	// IsError 表示这次调用失败(模型可据此纠正)。
	IsError bool
}

ToolResponse 是工具内部产生的文本结果。

func NewTextErrorResponse

func NewTextErrorResponse(content string) ToolResponse

NewTextErrorResponse 构造失败结果。

func NewTextResponse

func NewTextResponse(content string) ToolResponse

NewTextResponse 构造成功结果。

func WithResponseMetadata

func WithResponseMetadata(response ToolResponse, _ any) ToolResponse

WithResponseMetadata 兼容 opencode 的调用形式;元信息不参与模型回传,直接忽略。

type TreeNode

type TreeNode struct {
	Name     string      `json:"name"`
	Path     string      `json:"path"`
	Type     string      `json:"type"` // "file" or "directory"
	Children []*TreeNode `json:"children,omitempty"`
}

type ViewParams

type ViewParams struct {
	FilePath string `json:"file_path"`
	Offset   int    `json:"offset"`
	Limit    int    `json:"limit"`
}

ViewParams 是 view 工具的入参。

type WriteParams

type WriteParams struct {
	FilePath string `json:"file_path"`
	Content  string `json:"content"`
}

type WritePermissionsParams

type WritePermissionsParams struct {
	FilePath string `json:"file_path"`
	Diff     string `json:"diff"`
}

type WriteResponseMetadata

type WriteResponseMetadata struct {
	Diff      string `json:"diff"`
	Additions int    `json:"additions"`
	Removals  int    `json:"removals"`
}

Directories

Path Synopsis
Package diff 提供文件补丁解析与应用能力(从 opencode internal/diff 迁移)。
Package diff 提供文件补丁解析与应用能力(从 opencode internal/diff 迁移)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL