Add TypeScript compiler integration - core implementation

Co-authored-by: qaiu <29825328+qaiu@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-07 04:43:36 +00:00
parent a97268c702
commit f2c9c34324
8 changed files with 653 additions and 2 deletions

View File

@@ -422,6 +422,132 @@ public class PlaygroundApi {
return dbService.getPlaygroundParserById(id);
}
/**
* 保存TypeScript代码及其编译结果
*/
@RouteMapping(value = "/typescript", method = RouteMethod.POST)
public Future<JsonObject> saveTypeScriptCode(RoutingContext ctx) {
Promise<JsonObject> promise = Promise.promise();
try {
JsonObject body = ctx.body().asJsonObject();
Long parserId = body.getLong("parserId");
String tsCode = body.getString("tsCode");
String es5Code = body.getString("es5Code");
String compileErrors = body.getString("compileErrors");
String compilerVersion = body.getString("compilerVersion");
String compileOptions = body.getString("compileOptions");
Boolean isValid = body.getBoolean("isValid", true);
if (parserId == null) {
promise.complete(JsonResult.error("解析器ID不能为空").toJsonObject());
return promise.future();
}
if (StringUtils.isBlank(tsCode)) {
promise.complete(JsonResult.error("TypeScript代码不能为空").toJsonObject());
return promise.future();
}
if (StringUtils.isBlank(es5Code)) {
promise.complete(JsonResult.error("编译后的ES5代码不能为空").toJsonObject());
return promise.future();
}
// 代码长度验证
if (tsCode.length() > MAX_CODE_LENGTH || es5Code.length() > MAX_CODE_LENGTH) {
promise.complete(JsonResult.error("代码长度超过限制最大128KB").toJsonObject());
return promise.future();
}
JsonObject tsCodeInfo = new JsonObject();
tsCodeInfo.put("parserId", parserId);
tsCodeInfo.put("tsCode", tsCode);
tsCodeInfo.put("es5Code", es5Code);
tsCodeInfo.put("compileErrors", compileErrors);
tsCodeInfo.put("compilerVersion", compilerVersion);
tsCodeInfo.put("compileOptions", compileOptions);
tsCodeInfo.put("isValid", isValid);
tsCodeInfo.put("ip", getClientIp(ctx.request()));
dbService.saveTypeScriptCode(tsCodeInfo).onSuccess(result -> {
promise.complete(result);
}).onFailure(e -> {
log.error("保存TypeScript代码失败", e);
promise.complete(JsonResult.error("保存失败: " + e.getMessage()).toJsonObject());
});
} catch (Exception e) {
log.error("解析请求参数失败", e);
promise.complete(JsonResult.error("解析请求参数失败: " + e.getMessage()).toJsonObject());
}
return promise.future();
}
/**
* 根据parserId获取TypeScript代码
*/
@RouteMapping(value = "/typescript/:parserId", method = RouteMethod.GET)
public Future<JsonObject> getTypeScriptCode(Long parserId) {
return dbService.getTypeScriptCodeByParserId(parserId);
}
/**
* 更新TypeScript代码
*/
@RouteMapping(value = "/typescript/:parserId", method = RouteMethod.PUT)
public Future<JsonObject> updateTypeScriptCode(RoutingContext ctx, Long parserId) {
Promise<JsonObject> promise = Promise.promise();
try {
JsonObject body = ctx.body().asJsonObject();
String tsCode = body.getString("tsCode");
String es5Code = body.getString("es5Code");
String compileErrors = body.getString("compileErrors");
String compilerVersion = body.getString("compilerVersion");
String compileOptions = body.getString("compileOptions");
Boolean isValid = body.getBoolean("isValid", true);
if (StringUtils.isBlank(tsCode)) {
promise.complete(JsonResult.error("TypeScript代码不能为空").toJsonObject());
return promise.future();
}
if (StringUtils.isBlank(es5Code)) {
promise.complete(JsonResult.error("编译后的ES5代码不能为空").toJsonObject());
return promise.future();
}
// 代码长度验证
if (tsCode.length() > MAX_CODE_LENGTH || es5Code.length() > MAX_CODE_LENGTH) {
promise.complete(JsonResult.error("代码长度超过限制最大128KB").toJsonObject());
return promise.future();
}
JsonObject tsCodeInfo = new JsonObject();
tsCodeInfo.put("tsCode", tsCode);
tsCodeInfo.put("es5Code", es5Code);
tsCodeInfo.put("compileErrors", compileErrors);
tsCodeInfo.put("compilerVersion", compilerVersion);
tsCodeInfo.put("compileOptions", compileOptions);
tsCodeInfo.put("isValid", isValid);
dbService.updateTypeScriptCode(parserId, tsCodeInfo).onSuccess(result -> {
promise.complete(result);
}).onFailure(e -> {
log.error("更新TypeScript代码失败", e);
promise.complete(JsonResult.error("更新失败: " + e.getMessage()).toJsonObject());
});
} catch (Exception e) {
log.error("解析请求参数失败", e);
promise.complete(JsonResult.error("解析请求参数失败: " + e.getMessage()).toJsonObject());
}
return promise.future();
}
/**
* 获取客户端IP
*/

View File

@@ -0,0 +1,55 @@
package cn.qaiu.lz.web.model;
import cn.qaiu.db.ddl.Constraint;
import cn.qaiu.db.ddl.Length;
import cn.qaiu.db.ddl.Table;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* 演练场TypeScript代码实体
* 用于保存用户编写的TypeScript源代码
* 与PlaygroundParser关联存储原始TS代码和编译后的ES5代码
*/
@Data
@Table("playground_typescript_code")
public class PlaygroundTypeScriptCode {
private static final long serialVersionUID = 1L;
@Constraint(autoIncrement = true, notNull = true)
private Long id;
@Constraint(notNull = true)
private Long parserId; // 关联的解析器ID外键
@Length(varcharSize = 65535)
@Constraint(notNull = true)
private String tsCode; // TypeScript原始代码
@Length(varcharSize = 65535)
@Constraint(notNull = true)
private String es5Code; // 编译后的ES5代码
@Length(varcharSize = 2000)
private String compileErrors; // 编译错误信息(如果有)
@Length(varcharSize = 32)
private String compilerVersion; // 编译器版本
@Length(varcharSize = 1000)
private String compileOptions; // 编译选项JSON格式
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime = new Date(); // 创建时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime; // 更新时间
private Boolean isValid = true; // 编译是否成功
@Length(varcharSize = 64)
private String ip; // 创建者IP
}

View File

@@ -50,4 +50,19 @@ public interface DbService extends BaseAsyncService {
*/
Future<JsonObject> getPlaygroundParserById(Long id);
/**
* 保存TypeScript代码及其编译结果
*/
Future<JsonObject> saveTypeScriptCode(JsonObject tsCodeInfo);
/**
* 根据parserId获取TypeScript代码
*/
Future<JsonObject> getTypeScriptCodeByParserId(Long parserId);
/**
* 更新TypeScript代码
*/
Future<JsonObject> updateTypeScriptCode(Long parserId, JsonObject tsCodeInfo);
}

View File

@@ -265,4 +265,114 @@ public class DbServiceImpl implements DbService {
return promise.future();
}
@Override
public Future<JsonObject> saveTypeScriptCode(JsonObject tsCodeInfo) {
JDBCPool client = JDBCPoolInit.instance().getPool();
Promise<JsonObject> promise = Promise.promise();
String sql = """
INSERT INTO playground_typescript_code
(parser_id, ts_code, es5_code, compile_errors, compiler_version,
compile_options, create_time, is_valid, ip)
VALUES (?, ?, ?, ?, ?, ?, NOW(), ?, ?)
""";
client.preparedQuery(sql)
.execute(Tuple.of(
tsCodeInfo.getLong("parserId"),
tsCodeInfo.getString("tsCode"),
tsCodeInfo.getString("es5Code"),
tsCodeInfo.getString("compileErrors"),
tsCodeInfo.getString("compilerVersion"),
tsCodeInfo.getString("compileOptions"),
tsCodeInfo.getBoolean("isValid", true),
tsCodeInfo.getString("ip")
))
.onSuccess(res -> {
promise.complete(JsonResult.success("保存TypeScript代码成功").toJsonObject());
})
.onFailure(e -> {
log.error("saveTypeScriptCode failed", e);
promise.fail(e);
});
return promise.future();
}
@Override
public Future<JsonObject> getTypeScriptCodeByParserId(Long parserId) {
JDBCPool client = JDBCPoolInit.instance().getPool();
Promise<JsonObject> promise = Promise.promise();
String sql = "SELECT * FROM playground_typescript_code WHERE parser_id = ? ORDER BY create_time DESC LIMIT 1";
client.preparedQuery(sql)
.execute(Tuple.of(parserId))
.onSuccess(rows -> {
if (rows.size() > 0) {
Row row = rows.iterator().next();
JsonObject tsCode = new JsonObject();
tsCode.put("id", row.getLong("id"));
tsCode.put("parserId", row.getLong("parser_id"));
tsCode.put("tsCode", row.getString("ts_code"));
tsCode.put("es5Code", row.getString("es5_code"));
tsCode.put("compileErrors", row.getString("compile_errors"));
tsCode.put("compilerVersion", row.getString("compiler_version"));
tsCode.put("compileOptions", row.getString("compile_options"));
var createTime = row.getLocalDateTime("create_time");
if (createTime != null) {
tsCode.put("createTime", createTime.toString().replace("T", " "));
}
var updateTime = row.getLocalDateTime("update_time");
if (updateTime != null) {
tsCode.put("updateTime", updateTime.toString().replace("T", " "));
}
tsCode.put("isValid", row.getBoolean("is_valid"));
tsCode.put("ip", row.getString("ip"));
promise.complete(JsonResult.data(tsCode).toJsonObject());
} else {
promise.complete(JsonResult.data(null).toJsonObject());
}
})
.onFailure(e -> {
log.error("getTypeScriptCodeByParserId failed", e);
promise.fail(e);
});
return promise.future();
}
@Override
public Future<JsonObject> updateTypeScriptCode(Long parserId, JsonObject tsCodeInfo) {
JDBCPool client = JDBCPoolInit.instance().getPool();
Promise<JsonObject> promise = Promise.promise();
String sql = """
UPDATE playground_typescript_code
SET ts_code = ?, es5_code = ?, compile_errors = ?, compiler_version = ?,
compile_options = ?, update_time = NOW(), is_valid = ?
WHERE parser_id = ?
""";
client.preparedQuery(sql)
.execute(Tuple.of(
tsCodeInfo.getString("tsCode"),
tsCodeInfo.getString("es5Code"),
tsCodeInfo.getString("compileErrors"),
tsCodeInfo.getString("compilerVersion"),
tsCodeInfo.getString("compileOptions"),
tsCodeInfo.getBoolean("isValid", true),
parserId
))
.onSuccess(res -> {
promise.complete(JsonResult.success("更新TypeScript代码成功").toJsonObject());
})
.onFailure(e -> {
log.error("updateTypeScriptCode failed", e);
promise.fail(e);
});
return promise.future();
}
}