65 lines
4.5 KiB
Python
65 lines
4.5 KiB
Python
from __future__ import annotations
|
|
|
|
from model_router.router import ModelRequestContract, ModelResponseContract
|
|
|
|
|
|
class DeterministicCodingProvider:
|
|
"""Local deterministic provider used by M2 tests before real Qwen wiring."""
|
|
|
|
provider_name = "deterministic"
|
|
|
|
def complete(self, request: ModelRequestContract) -> ModelResponseContract:
|
|
prompt = request.prompt.lower()
|
|
if "inspection phase" in prompt:
|
|
operations = [
|
|
{"type": "list_directory", "path": "."},
|
|
{"type": "git_status"},
|
|
]
|
|
if "migration" in prompt:
|
|
operations.append({"type": "list_directory", "path": "items/migrations"})
|
|
if "health" in prompt or "endpoint" in prompt:
|
|
operations.extend([
|
|
{"type": "read_file", "path": "app/urls.py"},
|
|
{"type": "list_directory", "path": "tests"},
|
|
])
|
|
return ModelResponseContract("qwen-deterministic", "Inspected worktree.", {"inspect_operations": operations})
|
|
if "force_bad_implementation" in prompt:
|
|
operations = [{"type": "write_text", "path": "bad.txt", "content": "not enough\n"}]
|
|
return ModelResponseContract("qwen-deterministic", "Wrote intentionally insufficient change.", {"operations": operations})
|
|
if "/health" in prompt or "health endpoint" in prompt:
|
|
urls = '''from django.http import JsonResponse\nfrom django.urls import path\n\n\ndef health(request):\n return JsonResponse({"status": "ok"})\n\n\nurlpatterns = [\n path("health", health, name="health"),\n]\n'''
|
|
tests = '''from django.test import TestCase\n\n\nclass HealthEndpointTests(TestCase):\n def test_health_endpoint(self):\n response = self.client.get("/health")\n\n self.assertEqual(response.status_code, 200)\n self.assertEqual(response.json(), {"status": "ok"})\n'''
|
|
operations = [
|
|
{"type": "write_text", "path": "app/urls.py", "content": urls},
|
|
{"type": "write_text", "path": "tests/test_health.py", "content": tests},
|
|
]
|
|
return ModelResponseContract("qwen-deterministic", "Implemented health endpoint and tests.", {"operations": operations})
|
|
if "description field" in prompt:
|
|
models = '''from django.db import models\n\n\nclass Item(models.Model):\n name = models.CharField(max_length=100)\n description = models.TextField(blank=True)\n\n def __str__(self):\n return self.name\n'''
|
|
admin = '''from django.contrib import admin\n\nfrom items.models import Item\n\n\n@admin.register(Item)\nclass ItemAdmin(admin.ModelAdmin):\n list_display = ("name", "description")\n search_fields = ("name", "description")\n'''
|
|
tests = '''from django.test import TestCase\n\nfrom items.models import Item\n\n\nclass ItemDescriptionTests(TestCase):\n def test_item_description_field(self):\n item = Item.objects.create(name="Widget", description="Useful")\n\n self.assertEqual(item.description, "Useful")\n'''
|
|
migration = '''# Generated by Artifex deterministic M2 coder\nfrom django.db import migrations, models\n\n\nclass Migration(migrations.Migration):\n dependencies = [\n ("items", "0001_initial"),\n ]\n\n operations = [\n migrations.AddField(\n model_name="item",\n name="description",\n field=models.TextField(blank=True),\n ),\n ]\n'''
|
|
operations = [
|
|
{"type": "write_text", "path": "items/models.py", "content": models},
|
|
{"type": "write_text", "path": "items/admin.py", "content": admin},
|
|
{"type": "write_text", "path": "items/migrations/0002_item_description.py", "content": migration},
|
|
{"type": "write_text", "path": "tests/test_item_description.py", "content": tests},
|
|
]
|
|
return ModelResponseContract("qwen-deterministic", "Added description field, migration, admin, and tests.", {"operations": operations})
|
|
return ModelResponseContract("qwen-deterministic", "No operation matched.", {"operations": []})
|
|
|
|
def health(self) -> str:
|
|
return "AVAILABLE"
|
|
|
|
|
|
class DeterministicSolProvider:
|
|
provider_name = "deterministic_sol"
|
|
|
|
def __init__(self, content: str) -> None:
|
|
self.content = content
|
|
|
|
def complete(self, request):
|
|
return ModelResponseContract("sol-deterministic", self.content, {"usage": {}})
|
|
|
|
def health(self) -> str:
|
|
return "AVAILABLE"
|