Skip to content

Commit 6d59c53

Browse files
committed
enhance package resolver to support C packages and update input schema
1 parent 2f04eac commit 6d59c53

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/codegraphcontext/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ def _init_tools(self):
154154
"inputSchema": {
155155
"type": "object",
156156
"properties": {
157-
"package_name": {"type": "string", "description": "Name of the package to add (e.g., 'requests', 'express')."},
158-
"language": {"type": "string", "description": "The programming language of the package.", "enum": ["python", "javascript"]},
157+
"package_name": {"type": "string", "description": "Name of the package to add (e.g., 'requests', 'express', 'iniparser')."},
158+
"language": {"type": "string", "description": "The programming language of the package.", "enum": ["python", "javascript", "c"]},
159159
"is_dependency": {"type": "boolean", "description": "Mark as a dependency.", "default": True}
160160
},
161161
"required": ["package_name", "language"]

src/codegraphcontext/tools/package_resolver.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,73 @@ def _get_npm_package_path(package_name: str) -> Optional[str]:
5555
debug_log(f"Error getting npm package path for {package_name}: {e}")
5656
return None
5757

58+
def _get_c_package_path(package_name: str) -> Optional[str]:
59+
"""
60+
Finds the local installation path of a C package.
61+
"""
62+
try:
63+
debug_log(f"Getting local path for C package: {package_name}")
64+
65+
# Try using pkg-config to find the package
66+
try:
67+
result = subprocess.run(
68+
["pkg-config", "--variable=includedir", package_name],
69+
capture_output=True,
70+
text=True,
71+
timeout=5
72+
)
73+
if result.returncode == 0 and result.stdout.strip():
74+
include_dir = Path(result.stdout.strip())
75+
package_path = include_dir / package_name
76+
if package_path.exists():
77+
return str(package_path.resolve())
78+
if include_dir.exists():
79+
return str(include_dir.resolve())
80+
except (subprocess.TimeoutExpired, FileNotFoundError):
81+
debug_log(f"pkg-config not available or timed out for {package_name}")
82+
83+
# Search in standard system include directories
84+
common_include_paths = [
85+
"/usr/include",
86+
"/usr/local/include",
87+
"/opt/homebrew/include",
88+
"/opt/local/include",
89+
Path.home() / ".local" / "include",
90+
]
91+
92+
for base_path in common_include_paths:
93+
base_path = Path(base_path)
94+
if not base_path.exists():
95+
continue
96+
97+
# Check if package exists as a directory
98+
package_dir = base_path / package_name
99+
if package_dir.exists() and package_dir.is_dir():
100+
return str(package_dir.resolve())
101+
102+
# Check for header files with package name
103+
header_file = base_path / f"{package_name}.h"
104+
if header_file.exists():
105+
return str(header_file.resolve())
106+
107+
# Check current directory for local installations
108+
local_package = Path(f"./{package_name}")
109+
if local_package.exists():
110+
return str(local_package.resolve())
111+
112+
return None
113+
except Exception as e:
114+
debug_log(f"Error getting C package path for {package_name}: {e}")
115+
return None
116+
58117
def get_local_package_path(package_name: str, language: str) -> Optional[str]:
59118
"""
60119
Dispatches to the correct package path finder based on the language.
61120
"""
62121
finders = {
63122
"python": _get_python_package_path,
64123
"javascript": _get_npm_package_path,
124+
"c": _get_c_package_path,
65125
}
66126
finder = finders.get(language)
67127
if finder:

0 commit comments

Comments
 (0)