Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
basis for file mapping and auto context
Dane Schneider28e437180de8
diff --git a/app/server/syntax/file_map.go b/app/server/syntax/file_map.gonew file mode 100644index 00000000..b2424c50--- /dev/null+++ b/app/server/syntax/file_map.go@@ -0,0 +1,464 @@+package syntax++import (+ "context"+ "fmt"+ "path/filepath"+ "strings"++ tree_sitter "github.com/smacker/go-tree-sitter"+)++// FileMap represents a file's important definitions+type FileMap struct {+ Definitions []Definition+}++type Definition struct {+ Type string // "function", "class", "key", "selector", "instruction" etc+ Signature string // The full signature/header without implementation+ Comments []string // Any comments that precede this definition+ TagAttrs []string // For xml style markup tags, the class and id attributes+ Line int // Line number where definition starts+ Children []Definition // For parent types that can contain nested definitions+}++func MapFile(ctx context.Context, filename string, content []byte) (*FileMap, error) {+ // Get appropriate parser+ ext := filepath.Ext(filename)+ parser, lang, _, _ := GetParserForExt(ext)+ if parser == nil {+ return nil, fmt.Errorf("unsupported file type: %s", ext)+ }++ // Parse file+ tree, err := parser.ParseCtx(ctx, nil, content)+ if err != nil {+ return nil, fmt.Errorf("failed to parse file: %v", err)+ }+ defer tree.Close()++ // Create map+ m := &FileMap{+ Definitions: mapNode(tree.RootNode(), content, lang),+ }+ return m, nil+}++func mapNode(node *tree_sitter.Node, content []byte, lang string) []Definition {+ switch lang {+ case "dockerfile":+ return mapDockerfile(node, content)+ case "yaml", "toml", "json", "cue", "hcl":+ return mapConfig(node, content)+ case "html", "svelte":+ return mapMarkup(node, content)+ case "css":+ return mapCSS(node, content)+ default:+ return mapTraditional(node, content)+ }+}++// For traditional programming languages+func mapTraditional(node *tree_sitter.Node, content []byte) []Definition {+ var defs []Definition+ cursor := tree_sitter.NewTreeCursor(node)+ defer cursor.Close()++ if cursor.GoToFirstChild() {+ for {+ node := cursor.CurrentNode()+ nodeType := node.Type()++ // Check if this is a definition node+ if isDefinitionNode(nodeType) {+ def := Definition{+ Type: nodeType,+ Line: int(node.StartPoint().Row) + 1,+ }++ if isAssignmentNode(nodeType) {+ // Try different field names for identifiers+ // fmt.Printf("assignment node: %s\n", nodeType)+ sig := ""+ for i := 0; i < int(node.ChildCount()); i++ {+ child := node.Child(i)+ // fmt.Printf("child: %s\n", child.Type())++ if strings.HasSuffix(child.Type(), "_spec") {+ for j := 0; j < int(child.ChildCount()); j++ {+ subChild := child.Child(j)+ // fmt.Printf("sub child: %s\n", subChild.Type())+ if subChild.Type() == "identifier" {+ sig += string(subChild.Content(content)) + " "+ }+ }+ break+ } else {+ sig += string(child.Content(content)) + " "+ }+ }++ def.Signature = sig+ } else {+ // Get signature (up to body)+ if body := findImplementationBoundary(node); body != nil {+ start := node.StartByte()+ end := body.StartByte()+ def.Signature = string(content[start:end])++ // If this is a parent type node, recurse into the body+ if isParentNode(nodeType) {+ def.Children = mapTraditional(body, content)+ }+ } else {+ def.Signature = string(node.Content(content))+ }+ }++ // Get preceding comments+ def.Comments = getPrecedingComments(node, content)++ defs = append(defs, def)+ }++ if !cursor.GoToNextSibling() {+ break+ }+ }+ }++ return defs+}++// Helper functions for definitions and parents+func isDefinitionNode(nodeType string) bool {+ // Ignore import/require/use/include and similar nodes+ if strings.HasPrefix(nodeType, "import_") ||+ strings.HasPrefix(nodeType, "require_") ||+ strings.HasPrefix(nodeType, "use_") ||+ strings.HasPrefix(nodeType, "using_") ||+ strings.HasPrefix(nodeType, "include_") ||+ strings.HasPrefix(nodeType, "package_") ||+ strings.HasPrefix(nodeType, "module_") {+ return false+ }++ if isAssignmentNode(nodeType) ||++ strings.HasSuffix(nodeType, "_definition") ||+ strings.HasSuffix(nodeType, "_declaration") ||+ strings.HasSuffix(nodeType, "_declarator") ||+ strings.HasSuffix(nodeType, "_spec") ||+ strings.HasSuffix(nodeType, "_binding") ||+ strings.HasSuffix(nodeType, "_signature") {++ return true+ }+ return false+}++var parentNodeTypes = map[string]bool{+ "namespace_definition": true, // C+++ "namespace_declaration": true, // C#+ "module_definition": true, // Ruby+ "class_definition": true, // Python/Ruby+ "class_declaration": true, // Java/C#/TypeScript+ "interface_declaration": true, // Java/TypeScript+ "trait_definition": true, // Rust+ "trait_declaration": true, // PHP+ "impl_block": true, // Rust implementations+}++func isParentNode(nodeType string) bool {+ return parentNodeTypes[nodeType]+}++var assignmentNodeTypes = map[string]bool{+ // Common patterns+ "assignment_expression": true,+ "declaration": true,+ "variable_declaration": true,++ // Go+ "var_declaration": true,+ "const_declaration": true,++ // JS/TS+ "lexical_declaration": true,++ // Python+ "assignment_statement": true,++ // Ruby+ "variable_assignment": true,+ "class_variable_assignment": true,++ // Rust+ "let_declaration": true,+ "const_item": true,++ // Java, C#+ "static_declaration": true,+ "member_declaration": true,+ "field_declaration": true,++ // Kotlin/Scala+ "property_declaration": true,++ // OCaml+ "let_binding": true,+ "value_binding": true,++ // Elixir+ "module_attribute": true,++ // PHP+ "global_declaration": true,+ "static_variable_declaration": true,+}++func isAssignmentNode(nodeType string) bool {+ return assignmentNodeTypes[nodeType]+}++// Implementation boundary detection+var bodyNodeTypes = map[string]bool{+ "block": true,+ "body": true,+ "suite": true,+ "statement_block": true,+ "compound_statement": true,+ "function_body": true,+ "do_block": true,+ "implementation": true,+ "field_declaration_list": true,+}++func findImplementationBoundary(node *tree_sitter.Node) *tree_sitter.Node {+ for i := 0; i < int(node.ChildCount()); i++ {+ child := node.Child(i)+ if bodyNodeTypes[child.Type()] {+ return child+ }+ }+ return nil+}++// Get preceding comments+func getPrecedingComments(node *tree_sitter.Node, content []byte) []string {+ var comments []string+ const maxCommentLength = 1000++ prevNode := node.PrevSibling()+ for prevNode != nil {+ if !strings.Contains(prevNode.Type(), "comment") {+ break+ }+ comment := string(prevNode.Content(content))+ if len(comment) > maxCommentLength {+ comment = comment[:maxCommentLength] + "..."+ }+ comments = append([]string{comment}, comments...)+ prevNode = prevNode.PrevSibling()+ }+ return comments+}++// Specialized mappers+func mapConfig(node *tree_sitter.Node, content []byte) []Definition {+ cursor := tree_sitter.NewTreeCursor(node)+ defer cursor.Close()++ var walkConfig func(*tree_sitter.Node) []Definition+ walkConfig = func(node *tree_sitter.Node) []Definition {+ var nodeDefs []Definition++ if node.Type() == "pair" || // YAML+ node.Type() == "key_value" || // TOML+ node.Type() == "field" { // CUE/HCL+ if keyNode := node.ChildByFieldName("key"); keyNode != nil {+ def := Definition{+ Type: "key",+ Signature: string(keyNode.Content(content)),+ Line: int(keyNode.StartPoint().Row) + 1,+ }++ // Check for block values to find nested keys+ if valNode := node.ChildByFieldName("value"); valNode != nil {+ if valNode.Type() == "block_mapping" || // YAML+ valNode.Type() == "block" { // TOML/HCL/CUE+ def.Children = walkConfig(valNode)+ }+ }++ nodeDefs = append(nodeDefs, def)+ }+ }++ // Walk siblings+ for i := 0; i < int(node.ChildCount()); i++ {+ nodeDefs = append(nodeDefs, walkConfig(node.Child(i))...)+ }++ return nodeDefs+ }++ return walkConfig(node)+}++func mapMarkup(node *tree_sitter.Node, content []byte) []Definition {+ cursor := tree_sitter.NewTreeCursor(node)+ defer cursor.Close()++ var walkMarkup func(*tree_sitter.Node) []Definition+ walkMarkup = func(node *tree_sitter.Node) []Definition {+ var nodeDefs []Definition++ if node.Type() == "start_tag" || node.Type() == "self_closing_tag" {+ // Get tag name+ nameNode := node.ChildByFieldName("name")+ if nameNode == nil {+ return nodeDefs+ }++ // Get class and id attributes only+ var attrs []string+ for i := 0; i < int(node.ChildCount()); i++ {+ child := node.Child(i)+ if child.Type() == "attribute" {+ attrName := child.ChildByFieldName("name")+ if attrName != nil {+ name := string(attrName.Content(content))+ if name == "class" || name == "id" {+ attrVal := child.ChildByFieldName("value")+ if attrVal != nil {+ attrs = append(attrs, fmt.Sprintf("%s=%q",+ name, attrVal.Content(content)))+ }+ }+ }+ }+ }++ def := Definition{+ Type: "tag",+ Line: int(node.StartPoint().Row) + 1,+ Signature: string(nameNode.Content(content)),+ TagAttrs: attrs,+ }++ // Recursively process children+ for i := 0; i < int(node.ChildCount()); i++ {+ child := node.Child(i)+ def.Children = append(def.Children, walkMarkup(child)...)+ }++ nodeDefs = append(nodeDefs, def)+ }++ return nodeDefs+ }++ return walkMarkup(node)+}++func mapCSS(node *tree_sitter.Node, content []byte) []Definition {+ var defs []Definition+ cursor := tree_sitter.NewTreeCursor(node)+ defer cursor.Close()++ if cursor.GoToFirstChild() {+ for {+ node := cursor.CurrentNode()+ if node.Type() == "selector" {+ defs = append(defs, Definition{+ Type: "selector",+ Signature: string(node.Content(content)),+ Line: int(node.StartPoint().Row) + 1,+ })+ }+ if !cursor.GoToNextSibling() {+ break+ }+ }+ }+ return defs+}++var dockerKeyInstructions = map[string]bool{+ "FROM": true,+ "ENTRYPOINT": true,+ "CMD": true,+ "EXPOSE": true,+ "COPY": true,+ "ENV": true,+}++func mapDockerfile(node *tree_sitter.Node, content []byte) []Definition {+ var defs []Definition+ cursor := tree_sitter.NewTreeCursor(node)+ defer cursor.Close()++ if cursor.GoToFirstChild() {+ for {+ node := cursor.CurrentNode()+ if node.Type() == "instruction" {+ instruction := string(node.Content(content))+ command := strings.Fields(instruction)[0]+ if dockerKeyInstructions[command] {+ defs = append(defs, Definition{+ Type: "instruction",+ Signature: instruction,+ Line: int(node.StartPoint().Row) + 1,+ })+ }+ }+ if !cursor.GoToNextSibling() {+ break+ }+ }+ }+ return defs+}++func (m *FileMap) String() string {+ var b strings.Builder++ var writeDefinition func(def *Definition, depth int)+ writeDefinition = func(def *Definition, depth int) {+ // Indent+ if depth > 0 {+ b.WriteString(strings.Repeat(" ", depth))+ b.WriteString("- ")+ }++ // Write signature (for tags, include attrs)+ if def.Type == "tag" {+ // Extract tag name from signature (it's the first word)+ tagName := strings.Fields(def.Signature)[0]+ // Build full representation with attrs+ if len(def.TagAttrs) > 0 {+ b.WriteString(fmt.Sprintf("%s %s", tagName, strings.Join(def.TagAttrs, " ")))+ } else {+ b.WriteString(tagName)+ }+ } else {+ b.WriteString(strings.TrimSpace(def.Signature))+ }+ b.WriteString("\n")++ // Write children with increased depth+ for _, child := range def.Children {+ writeDefinition(&child, depth+1)+ }+ }++ // Write all top-level definitions+ for _, def := range m.Definitions {+ writeDefinition(&def, 0)+ }++ return b.String()+}diff --git a/app/server/syntax/map_cli/.gitignore b/app/server/syntax/map_cli/.gitignorenew file mode 100644index 00000000..bc8b1768--- /dev/null+++ b/app/server/syntax/map_cli/.gitignore@@ -0,0 +1 @@+mapper\ No newline at end of filediff --git a/app/server/syntax/map_cli/go.mod b/app/server/syntax/map_cli/go.modnew file mode 100644index 00000000..5e52d987--- /dev/null+++ b/app/server/syntax/map_cli/go.mod@@ -0,0 +1,12 @@+module mapper++go 1.21.3++replace plandex-server => ../../++require plandex-server v0.0.0-00010101000000-000000000000++require (+ github.com/davecgh/go-spew v1.1.1 // indirect+ github.com/smacker/go-tree-sitter v0.0.0-20240625050157-a31a98a7c0f6 // indirect+)diff --git a/app/server/syntax/map_cli/go.sum b/app/server/syntax/map_cli/go.sumnew file mode 100644index 00000000..0447717d--- /dev/null+++ b/app/server/syntax/map_cli/go.sum@@ -0,0 +1,17 @@+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=+github.com/smacker/go-tree-sitter v0.0.0-20240625050157-a31a98a7c0f6 h1:mtD4ESyObQZnRVxHFcaYp2d7jMBDa4WJRXSB1Vszj+A=+github.com/smacker/go-tree-sitter v0.0.0-20240625050157-a31a98a7c0f6/go.mod h1:q99oHDsbP0xRwmn7Vmob8gbSMNyvJ83OauXPSuHQuKE=+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=+github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=+github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=+github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=diff --git a/app/server/syntax/map_cli/main.go b/app/server/syntax/map_cli/main.gonew file mode 100644index 00000000..439226aa--- /dev/null+++ b/app/server/syntax/map_cli/main.go@@ -0,0 +1,33 @@+package main++import (+ "context"+ "fmt"+ "os"+ "plandex-server/syntax"+)++func main() {+ ctx := context.Background()++ // file path is first arg+ if len(os.Args) < 2 {+ fmt.Println("usage: mapper <file>")+ os.Exit(1)+ }++ filename := os.Args[1]+ content, err := os.ReadFile(filename)+ if err != nil {+ fmt.Printf("error reading file: %v\n", err)+ os.Exit(1)+ }++ m, err := syntax.MapFile(ctx, filename, content)+ if err != nil {+ fmt.Printf("error mapping file: %v\n", err)+ os.Exit(1)+ }++ fmt.Println(m.String())+}diff --git a/app/server/syntax/map_test.go b/app/server/syntax/map_test.gonew file mode 100644index 00000000..8f988754--- /dev/null+++ b/app/server/syntax/map_test.go@@ -0,0 +1,62 @@+package syntax++import (+ "context"+ "testing"++ "github.com/stretchr/testify/assert"+)++func TestMapFile(t *testing.T) {+ tests := []struct {+ name string+ filename string+ content string+ want *FileMap+ }{+ {+ name: "simple go file",+ filename: "test.go",+ content: `package main++import "fmt"++// User represents a system user+type User struct {+ Name string+ Age int+}++// GetName returns the user's name+func (u *User) GetName() string {+ return u.Name+}`,+ want: &FileMap{+ Definitions: []Definition{+ {+ Type: "type_definition",+ Signature: "type User struct {\n Name string\n Age int\n}",+ Comments: []string{"// User represents a system user"},+ Line: 5,+ Children: []Definition{+ {+ Type: "method_definition",+ Signature: "func (u *User) GetName() string",+ Comments: []string{"// GetName returns the user's name"},+ Line: 11,+ },+ },+ },+ },+ },+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got, err := MapFile(context.Background(), tt.filename, []byte(tt.content))+ assert.NoError(t, err)+ assert.Equal(t, tt.want, got)+ })+ }+}diff --git a/app/shared/data_models.go b/app/shared/data_models.goindex d020be83..dfdc46c5 100644--- a/app/shared/data_models.go+++ b/app/shared/data_models.go@@ -310,9 +310,10 @@ func (p PlannerRoleConfig) Value() (driver.Value, error) {}type ModelPack struct {- Id string `json:"id"`- Name string `json:"name"`- Description string `json:"description"`+ Id string `json:"id"`+ Name string `json:"name"`+ Description string `json:"description"`+Planner PlannerRoleConfig `json:"planner"`PlanSummary ModelRoleConfig `json:"planSummary"`Builder ModelRoleConfig `json:"builder"`@@ -323,6 +324,7 @@ type ModelPack struct {// optional for backwards compatibilityVerifier *ModelRoleConfig `json:"verifier"`AutoFix *ModelRoleConfig `json:"autoFix"`+ Search *ModelRoleConfig `json:"search"`}func (m *ModelPack) GetVerifier() ModelRoleConfig {diff --git a/app/shared/plan_settings.go b/app/shared/plan_settings.goindex 752c0e5a..0850d69d 100644--- a/app/shared/plan_settings.go+++ b/app/shared/plan_settings.go@@ -37,8 +37,10 @@ const (ModelRoleName ModelRole = "names"ModelRoleCommitMsg ModelRole = "commit-messages"ModelRoleExecStatus ModelRole = "auto-continue"- ModelRoleVerifier ModelRole = "verifier"- ModelRoleAutoFix ModelRole = "auto-fix"+ ModelRoleSearch ModelRole = "search"++ ModelRoleVerifier ModelRole = "verifier"+ ModelRoleAutoFix ModelRole = "auto-fix")var AllModelRoles = []ModelRole{ModelRolePlanner, ModelRolePlanSummary, ModelRoleBuilder, ModelRoleName, ModelRoleCommitMsg, ModelRoleExecStatus, ModelRoleVerifier, ModelRoleAutoFix}