Skip to content

copilot_client

GitHub Copilot SDK client wrapper for feature engineering.

Provides a simplified interface to the Copilot SDK specifically designed for feature engineering tasks.

CopilotConfig

Bases: BaseModel

Configuration for Copilot client.

Source code in featcopilot/llm/copilot_client.py
class CopilotConfig(BaseModel):
    """Configuration for Copilot client."""

    model: str = Field(default="gpt-5.2", description="Model to use")
    temperature: float = Field(default=0.3, ge=0, le=1, description="Temperature for generation")
    max_tokens: int = Field(default=4096, description="Maximum tokens in response")
    timeout: float = Field(default=60.0, description="Timeout in seconds")
    streaming: bool = Field(default=False, description="Enable streaming responses")

CopilotFeatureClient

GitHub Copilot SDK client wrapper for feature engineering.

Provides high-level methods for: - Generating feature suggestions - Explaining features - Generating feature code - Validating features

Parameters:

Name Type Description Default
config CopilotConfig

Configuration for the client

None
model str

Model to use for generation

'gpt-5.2'

Examples:

>>> client = CopilotFeatureClient(model='gpt-5.2')
>>> await client.start()
>>> suggestions = await client.suggest_features(
...     column_info={'age': 'int', 'income': 'float'},
...     task='predict churn'
... )
>>> await client.stop()
Source code in featcopilot/llm/copilot_client.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
class CopilotFeatureClient:
    """
    GitHub Copilot SDK client wrapper for feature engineering.

    Provides high-level methods for:
    - Generating feature suggestions
    - Explaining features
    - Generating feature code
    - Validating features

    Parameters
    ----------
    config : CopilotConfig, optional
        Configuration for the client
    model : str, default='gpt-5.2'
        Model to use for generation

    Examples
    --------
    >>> client = CopilotFeatureClient(model='gpt-5.2')
    >>> await client.start()
    >>> suggestions = await client.suggest_features(
    ...     column_info={'age': 'int', 'income': 'float'},
    ...     task='predict churn'
    ... )
    >>> await client.stop()
    """

    def __init__(self, config: Optional[CopilotConfig] = None, model: str = "gpt-5.2", **kwargs):
        self.config = config or CopilotConfig(model=model, **kwargs)
        self._client = None
        self._session = None
        self._is_started = False
        self._copilot_available = False

    async def start(self) -> "CopilotFeatureClient":
        """
        Start the Copilot client.

        Returns
        -------
        self : CopilotFeatureClient
        """
        try:
            from copilot import CopilotClient

            self._client = CopilotClient()
            await self._client.start()
            self._session = await self._client.create_session(
                {
                    "model": self.config.model,
                    "streaming": self.config.streaming,
                }
            )
            self._is_started = True
            self._copilot_available = True

        except ImportError:
            # Copilot SDK not installed - use mock mode
            self._copilot_available = False
            self._is_started = True
            logger.warning("copilot-sdk not installed. Using mock LLM responses.")

        except Exception as e:
            # Copilot not available - use mock mode
            self._copilot_available = False
            self._is_started = True
            logger.warning(f"Could not connect to Copilot: {e}. Using mock LLM responses.")

        return self

    async def stop(self) -> None:
        """Stop the Copilot client."""
        if self._session and self._copilot_available:
            await self._session.destroy()
        if self._client and self._copilot_available:
            await self._client.stop()
        self._is_started = False

    async def send_prompt(self, prompt: str) -> str:
        """
        Send a prompt and get a response.

        Parameters
        ----------
        prompt : str
            The prompt to send

        Returns
        -------
        response : str
            The model's response
        """
        if not self._is_started:
            await self.start()

        if not self._copilot_available:
            return self._mock_response(prompt)

        # Use asyncio.Event to wait for completion
        done = asyncio.Event()
        response_content = []

        def on_event(event):
            if event.type.value == "assistant.message":
                response_content.append(event.data.content)
            elif event.type.value == "session.idle":
                done.set()

        self._session.on(on_event)
        await self._session.send({"prompt": prompt})

        # Wait with timeout
        try:
            await asyncio.wait_for(done.wait(), timeout=self.config.timeout)
        except asyncio.TimeoutError:
            return "Error: Request timed out"

        return response_content[-1] if response_content else ""

    def _mock_response(self, prompt: str) -> str:
        """Generate mock response when Copilot is unavailable."""
        # Extract column names from prompt if available
        import re

        columns = re.findall(r"- (\w+) \(", prompt)

        if ("suggest" in prompt.lower() or "feature" in prompt.lower()) and columns:
            # Generate context-aware mock features based on actual columns
            features = []
            if len(columns) >= 2:
                col1, col2 = columns[0], columns[1]
                features.append(
                    {
                        "name": f"{col1}_{col2}_ratio",
                        "code": f"result = df['{col1}'] / (df['{col2}'] + 1e-8)",
                        "explanation": f"Ratio of {col1} to {col2}, captures relative relationship",
                        "source_columns": [col1, col2],
                    }
                )
                features.append(
                    {
                        "name": f"{col1}_{col2}_product",
                        "code": f"result = df['{col1}'] * df['{col2}']",
                        "explanation": f"Interaction between {col1} and {col2}",
                        "source_columns": [col1, col2],
                    }
                )
            if len(columns) >= 3:
                col3 = columns[2]
                features.append(
                    {
                        "name": f"{col1}_normalized_by_{col3}",
                        "code": f"result = (df['{col1}'] - df['{col1}'].mean()) / (df['{col3}'] + 1e-8)",
                        "explanation": f"Normalized {col1} adjusted by {col3}",
                        "source_columns": [col1, col3],
                    }
                )
            if len(columns) >= 1:
                features.append(
                    {
                        "name": f"{columns[0]}_zscore",
                        "code": f"result = (df['{columns[0]}'] - df['{columns[0]}'].mean()) / (df['{columns[0]}'].std() + 1e-8)",
                        "explanation": f"Z-score normalization of {columns[0]}",
                        "source_columns": [columns[0]],
                    }
                )
            return json.dumps({"features": features})
        elif "suggest" in prompt.lower() or "feature" in prompt.lower():
            return json.dumps(
                {
                    "features": [
                        {
                            "name": "feature_interaction",
                            "code": "result = df.iloc[:, 0] * df.iloc[:, 1]",
                            "explanation": "Interaction between first two features",
                        }
                    ]
                }
            )
        elif "explain" in prompt.lower():
            return "This feature captures the relationship between the input variables."
        elif "code" in prompt.lower():
            return "result = df.iloc[:, 0] * df.iloc[:, 1]"
        else:
            return "Mock response for: " + prompt[:100]

    async def suggest_features(
        self,
        column_info: dict[str, str],
        task_description: str,
        column_descriptions: Optional[dict[str, str]] = None,
        domain: Optional[str] = None,
        max_suggestions: int = 10,
    ) -> list[dict[str, Any]]:
        """
        Get LLM suggestions for new features.

        Parameters
        ----------
        column_info : dict
            Dictionary mapping column names to data types
        task_description : str
            Description of the ML task
        column_descriptions : dict, optional
            Human-readable descriptions of columns
        domain : str, optional
            Domain context (e.g., 'healthcare', 'finance')
        max_suggestions : int, default=10
            Maximum number of feature suggestions

        Returns
        -------
        suggestions : list
            List of feature suggestions with code and explanations
        """
        prompt = self._build_suggestion_prompt(
            column_info, task_description, column_descriptions, domain, max_suggestions
        )

        response = await self.send_prompt(prompt)
        return self._parse_suggestions(response)

    def _build_suggestion_prompt(
        self,
        column_info: dict[str, str],
        task_description: str,
        column_descriptions: Optional[dict[str, str]] = None,
        domain: Optional[str] = None,
        max_suggestions: int = 10,
    ) -> str:
        """Build the prompt for feature suggestions."""
        prompt = f"""You are an expert data scientist specializing in feature engineering.

TASK: Suggest {max_suggestions} new features for the following machine learning task.

## ML Task
{task_description}

## Available Columns
"""
        for col, dtype in column_info.items():
            desc = column_descriptions.get(col, "") if column_descriptions else ""
            prompt += f"- {col} ({dtype}): {desc}\n"

        if domain:
            prompt += f"\n## Domain Context\nThis is a {domain} problem.\n"

        prompt += """
## Requirements
1. Suggest features that would be predictive for this task
2. Provide Python code using pandas (assume df is the DataFrame)
3. Explain why each feature might be useful
4. Consider interactions, ratios, and domain-specific transformations

## Output Format
Return a JSON object with a "features" array, each element having:
- "name": feature name (snake_case)
- "code": Python code to compute the feature (single line, result assigned to variable)
- "explanation": why this feature might be predictive
- "source_columns": list of column names used

Example:
{
  "features": [
    {
      "name": "age_income_ratio",
      "code": "result = df['age'] / (df['income'] + 1)",
      "explanation": "Ratio of age to income may indicate life stage and financial maturity",
      "source_columns": ["age", "income"]
    }
  ]
}

Return ONLY the JSON object, no other text.
"""
        return prompt

    def _parse_suggestions(self, response: str) -> list[dict[str, Any]]:
        """Parse feature suggestions from LLM response."""
        try:
            # Try to extract JSON from response
            response = response.strip()
            if response.startswith("```"):
                # Remove markdown code blocks
                lines = response.split("\n")
                response = "\n".join(lines[1:-1])

            data = json.loads(response)
            return data.get("features", [])

        except json.JSONDecodeError:
            # Try to extract JSON substring
            import re

            json_match = re.search(r"\{.*\}", response, re.DOTALL)
            if json_match:
                try:
                    data = json.loads(json_match.group())
                    return data.get("features", [])
                except json.JSONDecodeError:
                    pass

            return []

    async def explain_feature(
        self,
        feature_name: str,
        feature_code: str,
        column_descriptions: Optional[dict[str, str]] = None,
        task_description: Optional[str] = None,
    ) -> str:
        """
        Get a human-readable explanation of a feature.

        Parameters
        ----------
        feature_name : str
            Name of the feature
        feature_code : str
            Code that generates the feature
        column_descriptions : dict, optional
            Descriptions of source columns
        task_description : str, optional
            Description of the ML task

        Returns
        -------
        explanation : str
            Human-readable explanation
        """
        prompt = f"""Explain this feature in simple terms for a business stakeholder:

Feature Name: {feature_name}
Code: {feature_code}
"""
        if column_descriptions:
            prompt += "\nColumn Descriptions:\n"
            for col, desc in column_descriptions.items():
                prompt += f"- {col}: {desc}\n"

        if task_description:
            prompt += f"\nML Task: {task_description}\n"

        prompt += """
Provide a 2-3 sentence explanation of:
1. What this feature represents
2. Why it might be predictive for the task
"""
        return await self.send_prompt(prompt)

    async def generate_feature_code(
        self, description: str, column_info: dict[str, str], constraints: Optional[list[str]] = None
    ) -> str:
        """
        Generate Python code for a described feature.

        Parameters
        ----------
        description : str
            Natural language description of desired feature
        column_info : dict
            Available columns and their types
        constraints : list, optional
            Constraints on the generated code

        Returns
        -------
        code : str
            Python code to generate the feature
        """
        prompt = f"""Generate Python code to create this feature:

Description: {description}

Available Columns:
"""
        for col, dtype in column_info.items():
            prompt += f"- {col} ({dtype})\n"

        if constraints:
            prompt += "\nConstraints:\n"
            for c in constraints:
                prompt += f"- {c}\n"

        prompt += """
Requirements:
1. Use pandas operations (assume df is the DataFrame)
2. Assign the result to a variable called 'result'
3. Handle edge cases (division by zero, missing values)
4. Return ONLY the code, no explanations

Example output:
result = df['col1'] / (df['col2'] + 1e-8)
"""
        response = await self.send_prompt(prompt)

        # Extract code from response
        code = response.strip()
        if "```" in code:
            lines = code.split("\n")
            code_lines = []
            in_code_block = False
            for line in lines:
                if line.startswith("```"):
                    in_code_block = not in_code_block
                elif in_code_block:
                    code_lines.append(line)
            code = "\n".join(code_lines)

        return code

    async def validate_feature_code(self, code: str, sample_data: Optional[dict[str, list]] = None) -> dict[str, Any]:
        """
        Validate generated feature code.

        Parameters
        ----------
        code : str
            Feature code to validate
        sample_data : dict, optional
            Sample data for testing

        Returns
        -------
        result : dict
            Validation result with 'valid', 'error', and 'warnings' keys
        """
        import numpy as np
        import pandas as pd

        result = {"valid": True, "error": None, "warnings": []}

        # Syntax check
        try:
            compile(code, "<string>", "exec")
        except SyntaxError as e:
            result["valid"] = False
            result["error"] = f"Syntax error: {e}"
            return result

        # Runtime check with sample data
        if sample_data:
            try:
                df = pd.DataFrame(sample_data)
                local_vars = {"df": df, "np": np, "pd": pd}
                exec(
                    code,
                    {
                        "__builtins__": {
                            "len": len,
                            "sum": sum,
                            "max": max,
                            "min": min,
                            "int": int,
                            "float": float,
                            "str": str,
                            "bool": bool,
                            "abs": abs,
                            "round": round,
                            "pow": pow,
                            "range": range,
                            "list": list,
                            "dict": dict,
                            "set": set,
                            "tuple": tuple,
                            "sorted": sorted,
                            "reversed": reversed,
                            "enumerate": enumerate,
                            "zip": zip,
                            "any": any,
                            "all": all,
                            "map": map,
                            "filter": filter,
                            "isinstance": isinstance,
                            "hasattr": hasattr,
                            "getattr": getattr,
                        }
                    },
                    local_vars,
                )

                if "result" not in local_vars:
                    result["warnings"].append("Code does not assign to 'result' variable")

            except Exception as e:
                result["valid"] = False
                result["error"] = f"Runtime error: {e}"

        return result

explain_feature(feature_name, feature_code, column_descriptions=None, task_description=None) async

Get a human-readable explanation of a feature.

Parameters:

Name Type Description Default
feature_name str

Name of the feature

required
feature_code str

Code that generates the feature

required
column_descriptions dict

Descriptions of source columns

None
task_description str

Description of the ML task

None

Returns:

Name Type Description
explanation str

Human-readable explanation

Source code in featcopilot/llm/copilot_client.py
    async def explain_feature(
        self,
        feature_name: str,
        feature_code: str,
        column_descriptions: Optional[dict[str, str]] = None,
        task_description: Optional[str] = None,
    ) -> str:
        """
        Get a human-readable explanation of a feature.

        Parameters
        ----------
        feature_name : str
            Name of the feature
        feature_code : str
            Code that generates the feature
        column_descriptions : dict, optional
            Descriptions of source columns
        task_description : str, optional
            Description of the ML task

        Returns
        -------
        explanation : str
            Human-readable explanation
        """
        prompt = f"""Explain this feature in simple terms for a business stakeholder:

Feature Name: {feature_name}
Code: {feature_code}
"""
        if column_descriptions:
            prompt += "\nColumn Descriptions:\n"
            for col, desc in column_descriptions.items():
                prompt += f"- {col}: {desc}\n"

        if task_description:
            prompt += f"\nML Task: {task_description}\n"

        prompt += """
Provide a 2-3 sentence explanation of:
1. What this feature represents
2. Why it might be predictive for the task
"""
        return await self.send_prompt(prompt)

generate_feature_code(description, column_info, constraints=None) async

Generate Python code for a described feature.

Parameters:

Name Type Description Default
description str

Natural language description of desired feature

required
column_info dict

Available columns and their types

required
constraints list

Constraints on the generated code

None

Returns:

Name Type Description
code str

Python code to generate the feature

Source code in featcopilot/llm/copilot_client.py
    async def generate_feature_code(
        self, description: str, column_info: dict[str, str], constraints: Optional[list[str]] = None
    ) -> str:
        """
        Generate Python code for a described feature.

        Parameters
        ----------
        description : str
            Natural language description of desired feature
        column_info : dict
            Available columns and their types
        constraints : list, optional
            Constraints on the generated code

        Returns
        -------
        code : str
            Python code to generate the feature
        """
        prompt = f"""Generate Python code to create this feature:

Description: {description}

Available Columns:
"""
        for col, dtype in column_info.items():
            prompt += f"- {col} ({dtype})\n"

        if constraints:
            prompt += "\nConstraints:\n"
            for c in constraints:
                prompt += f"- {c}\n"

        prompt += """
Requirements:
1. Use pandas operations (assume df is the DataFrame)
2. Assign the result to a variable called 'result'
3. Handle edge cases (division by zero, missing values)
4. Return ONLY the code, no explanations

Example output:
result = df['col1'] / (df['col2'] + 1e-8)
"""
        response = await self.send_prompt(prompt)

        # Extract code from response
        code = response.strip()
        if "```" in code:
            lines = code.split("\n")
            code_lines = []
            in_code_block = False
            for line in lines:
                if line.startswith("```"):
                    in_code_block = not in_code_block
                elif in_code_block:
                    code_lines.append(line)
            code = "\n".join(code_lines)

        return code

send_prompt(prompt) async

Send a prompt and get a response.

Parameters:

Name Type Description Default
prompt str

The prompt to send

required

Returns:

Name Type Description
response str

The model's response

Source code in featcopilot/llm/copilot_client.py
async def send_prompt(self, prompt: str) -> str:
    """
    Send a prompt and get a response.

    Parameters
    ----------
    prompt : str
        The prompt to send

    Returns
    -------
    response : str
        The model's response
    """
    if not self._is_started:
        await self.start()

    if not self._copilot_available:
        return self._mock_response(prompt)

    # Use asyncio.Event to wait for completion
    done = asyncio.Event()
    response_content = []

    def on_event(event):
        if event.type.value == "assistant.message":
            response_content.append(event.data.content)
        elif event.type.value == "session.idle":
            done.set()

    self._session.on(on_event)
    await self._session.send({"prompt": prompt})

    # Wait with timeout
    try:
        await asyncio.wait_for(done.wait(), timeout=self.config.timeout)
    except asyncio.TimeoutError:
        return "Error: Request timed out"

    return response_content[-1] if response_content else ""

start() async

Start the Copilot client.

Returns:

Name Type Description
self CopilotFeatureClient
Source code in featcopilot/llm/copilot_client.py
async def start(self) -> "CopilotFeatureClient":
    """
    Start the Copilot client.

    Returns
    -------
    self : CopilotFeatureClient
    """
    try:
        from copilot import CopilotClient

        self._client = CopilotClient()
        await self._client.start()
        self._session = await self._client.create_session(
            {
                "model": self.config.model,
                "streaming": self.config.streaming,
            }
        )
        self._is_started = True
        self._copilot_available = True

    except ImportError:
        # Copilot SDK not installed - use mock mode
        self._copilot_available = False
        self._is_started = True
        logger.warning("copilot-sdk not installed. Using mock LLM responses.")

    except Exception as e:
        # Copilot not available - use mock mode
        self._copilot_available = False
        self._is_started = True
        logger.warning(f"Could not connect to Copilot: {e}. Using mock LLM responses.")

    return self

stop() async

Stop the Copilot client.

Source code in featcopilot/llm/copilot_client.py
async def stop(self) -> None:
    """Stop the Copilot client."""
    if self._session and self._copilot_available:
        await self._session.destroy()
    if self._client and self._copilot_available:
        await self._client.stop()
    self._is_started = False

suggest_features(column_info, task_description, column_descriptions=None, domain=None, max_suggestions=10) async

Get LLM suggestions for new features.

Parameters:

Name Type Description Default
column_info dict

Dictionary mapping column names to data types

required
task_description str

Description of the ML task

required
column_descriptions dict

Human-readable descriptions of columns

None
domain str

Domain context (e.g., 'healthcare', 'finance')

None
max_suggestions int

Maximum number of feature suggestions

10

Returns:

Name Type Description
suggestions list

List of feature suggestions with code and explanations

Source code in featcopilot/llm/copilot_client.py
async def suggest_features(
    self,
    column_info: dict[str, str],
    task_description: str,
    column_descriptions: Optional[dict[str, str]] = None,
    domain: Optional[str] = None,
    max_suggestions: int = 10,
) -> list[dict[str, Any]]:
    """
    Get LLM suggestions for new features.

    Parameters
    ----------
    column_info : dict
        Dictionary mapping column names to data types
    task_description : str
        Description of the ML task
    column_descriptions : dict, optional
        Human-readable descriptions of columns
    domain : str, optional
        Domain context (e.g., 'healthcare', 'finance')
    max_suggestions : int, default=10
        Maximum number of feature suggestions

    Returns
    -------
    suggestions : list
        List of feature suggestions with code and explanations
    """
    prompt = self._build_suggestion_prompt(
        column_info, task_description, column_descriptions, domain, max_suggestions
    )

    response = await self.send_prompt(prompt)
    return self._parse_suggestions(response)

validate_feature_code(code, sample_data=None) async

Validate generated feature code.

Parameters:

Name Type Description Default
code str

Feature code to validate

required
sample_data dict

Sample data for testing

None

Returns:

Name Type Description
result dict

Validation result with 'valid', 'error', and 'warnings' keys

Source code in featcopilot/llm/copilot_client.py
async def validate_feature_code(self, code: str, sample_data: Optional[dict[str, list]] = None) -> dict[str, Any]:
    """
    Validate generated feature code.

    Parameters
    ----------
    code : str
        Feature code to validate
    sample_data : dict, optional
        Sample data for testing

    Returns
    -------
    result : dict
        Validation result with 'valid', 'error', and 'warnings' keys
    """
    import numpy as np
    import pandas as pd

    result = {"valid": True, "error": None, "warnings": []}

    # Syntax check
    try:
        compile(code, "<string>", "exec")
    except SyntaxError as e:
        result["valid"] = False
        result["error"] = f"Syntax error: {e}"
        return result

    # Runtime check with sample data
    if sample_data:
        try:
            df = pd.DataFrame(sample_data)
            local_vars = {"df": df, "np": np, "pd": pd}
            exec(
                code,
                {
                    "__builtins__": {
                        "len": len,
                        "sum": sum,
                        "max": max,
                        "min": min,
                        "int": int,
                        "float": float,
                        "str": str,
                        "bool": bool,
                        "abs": abs,
                        "round": round,
                        "pow": pow,
                        "range": range,
                        "list": list,
                        "dict": dict,
                        "set": set,
                        "tuple": tuple,
                        "sorted": sorted,
                        "reversed": reversed,
                        "enumerate": enumerate,
                        "zip": zip,
                        "any": any,
                        "all": all,
                        "map": map,
                        "filter": filter,
                        "isinstance": isinstance,
                        "hasattr": hasattr,
                        "getattr": getattr,
                    }
                },
                local_vars,
            )

            if "result" not in local_vars:
                result["warnings"].append("Code does not assign to 'result' variable")

        except Exception as e:
            result["valid"] = False
            result["error"] = f"Runtime error: {e}"

    return result

SyncCopilotFeatureClient

Synchronous wrapper for CopilotFeatureClient.

Source code in featcopilot/llm/copilot_client.py
class SyncCopilotFeatureClient:
    """Synchronous wrapper for CopilotFeatureClient."""

    def __init__(self, **kwargs):
        self._async_client = CopilotFeatureClient(**kwargs)
        self._loop = None

    def _get_or_create_loop(self):
        """Get or create a persistent event loop for this client."""
        if self._loop is None or self._loop.is_closed():
            self._loop = asyncio.new_event_loop()
            asyncio.set_event_loop(self._loop)
        return self._loop

    def _run_async(self, coro):
        """Run an async coroutine, handling various event loop scenarios."""
        try:
            # First, try to get the running loop
            try:
                loop = asyncio.get_running_loop()
                # We're in a running loop - use nest_asyncio if available
                try:
                    import nest_asyncio

                    nest_asyncio.apply()
                    return loop.run_until_complete(coro)
                except ImportError:
                    # nest_asyncio not available, use thread pool
                    import concurrent.futures

                    with concurrent.futures.ThreadPoolExecutor() as executor:
                        future = executor.submit(self._run_in_new_loop, coro)
                        return future.result(timeout=120)
            except RuntimeError:
                # No running event loop - use our persistent loop
                loop = self._get_or_create_loop()
                return loop.run_until_complete(coro)
        except Exception as e:
            # Last resort - create a completely fresh loop
            try:
                return self._run_in_new_loop(coro)
            except Exception:
                raise e from None

    def _run_in_new_loop(self, coro):
        """Run coroutine in a fresh event loop."""
        loop = asyncio.new_event_loop()
        try:
            asyncio.set_event_loop(loop)
            return loop.run_until_complete(coro)
        finally:
            loop.close()

    def start(self):
        return self._run_async(self._async_client.start())

    def stop(self):
        result = self._run_async(self._async_client.stop())
        # Close our loop if it exists
        if self._loop is not None and not self._loop.is_closed():
            self._loop.close()
            self._loop = None
        return result

    def suggest_features(self, **kwargs):
        return self._run_async(self._async_client.suggest_features(**kwargs))

    def send_prompt(self, prompt: str):
        return self._run_async(self._async_client.send_prompt(prompt))

    def explain_feature(self, **kwargs):
        return self._run_async(self._async_client.explain_feature(**kwargs))

    def generate_feature_code(self, **kwargs):
        return self._run_async(self._async_client.generate_feature_code(**kwargs))

    def validate_feature_code(self, code: str, sample_data=None):
        return self._run_async(self._async_client.validate_feature_code(code=code, sample_data=sample_data))