File size: 3,033 Bytes
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
"""A fixed, isolated Java health service for requirement-driven browser apps.

The application itself is a self-contained HTML document supplied by the
model. The Java service is platform-owned and never chosen by that model.
"""
from src.modules.execution.contracts import HttpCase, SandboxConfig
from src.modules.skill.domain.models import SkillError


PROFILE = 'java17-browser-app'
SKILL = 'webapp-java-development'
PAGE = 'website/index.html'
JAVA = 'src/main/java/demo/App.java'

JAVA_SOURCE = '''package demo;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public final class App {
  private static void reply(HttpExchange exchange, int status, String json) throws IOException {
    byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
    exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
    exchange.sendResponseHeaders(status, bytes.length);
    try (var body = exchange.getResponseBody()) { body.write(bytes); }
  }
  public static void main(String[] args) throws IOException {
    HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 8080), 0);
    server.createContext("/", exchange -> {
      if (!"/health".equals(exchange.getRequestURI().getPath())) {
        reply(exchange, 404, "{\\"error\\":\\"NOT_FOUND\\"}");
      } else if (!"GET".equals(exchange.getRequestMethod())) {
        reply(exchange, 405, "{\\"error\\":\\"METHOD_NOT_ALLOWED\\"}");
      } else {
        reply(exchange, 200, "{\\"status\\":\\"ok\\"}");
      }
    });
    server.start();
  }
}
'''

CONTRACT = (
    'The platform supplies a fixed Java17 /health service and independently '
    'tests it inside a networkless VM. The model supplies only website/index.html: '
    'one complete self-contained HTML document with inline CSS and JavaScript. '
    'Implement the CURRENT user requirement as an interactive browser app. '
    'Do not reuse the calculator business unless the current requirement asks '
    'for a calculator. Do not depend on external resources or network requests.'
)


def prepare_patch(patch):
    if not isinstance(patch, dict) or not isinstance(patch.get('files'), dict) or set(patch['files']) != {PAGE}:
        raise SkillError('WEBAPP_PATCH_INVALID', '网页应用只接受模型生成的 website/index.html。', 422)
    return {'files': {JAVA: JAVA_SOURCE, PAGE: patch['files'][PAGE]}}


def scenario(image):
    health = HttpCase(path='/health', status=200, expected_json={'status': 'ok'})
    wrong_method = HttpCase(path='/health', method='POST', status=405,
                            expected_json={'error': 'METHOD_NOT_ALLOWED'})
    unknown = HttpCase(path='/unknown', status=404, expected_json={'error': 'NOT_FOUND'})
    return SandboxConfig(image=image, skill_ids=[SKILL], api_contract=CONTRACT,
                         command_seconds=120, tests=[health, wrong_method, unknown], health=health)