using Stardust.Data.Configs;
using Stardust.Data.Deployment;
using Stardust.Mcp;
using Stardust.Server.Services;
using Stardust.Web.Services;
using Stardust.Data.Platform;
namespace Stardust.Web.Services;
/// <summary>MCP 写操作适配器。复用宿主已注册的配置、部署、流水线服务和命令通道。</summary>
public class McpOperations : IMcpOperations
{
private readonly ConfigService _config;
private readonly DeployService _deploy;
private readonly PipelineService _pipeline;
private readonly StarFactory _star;
public McpOperations(ConfigService config, DeployService deploy, PipelineService pipeline, StarFactory star)
{
_config = config;
_deploy = deploy;
_pipeline = pipeline;
_star = star;
}
public Task<Int32> SetConfigAsync(Int32 configId, IDictionary<String, Object> configs)
{
var cfg = ProjectScopeRules.FilterVisible(AppConfig.FindById(configId)) ?? throw new InvalidOperationException("Configuration does not exist.");
return Task.FromResult(_config.SetConfigs(cfg, configs));
}
public Task<Int32> PublishConfigAsync(Int32 configId) => _config.Publish(configId);
public Task<ConfigRollbackResult> RollbackConfigAsync(Int32 historyId) => _config.Rollback(historyId);
public Task<String> UseVersionAsync(Int32 versionId, String ip, Boolean publishEnabledNodes = true)
=> _deploy.UseVersion(versionId, ip, publishEnabledNodes);
public async Task<Int32> ControlDeployAsync(Int32 deployId, Int32 deployNodeId, String action, String ip)
{
var app = ProjectScopeRules.FilterVisible(AppDeploy.FindById(deployId));
var node = ProjectScopeRules.FilterVisible(AppDeployNode.FindById(deployNodeId));
return await _deploy.Control(app, node, action, ip, 0, 0);
}
public async Task<Object> TriggerPipelineAsync(Int32 pipelineId, String ip)
{
var run = await _pipeline.Trigger(pipelineId, ip);
return new { run_id = run.Id, status = run.Status.ToString() };
}
public Task<Boolean> CancelPipelineAsync(Int32 runId, String ip) => Task.FromResult(_pipeline.Cancel(runId, ip));
public async Task<Object> SendNodeCommandAsync(String nodeCode, String command, String args)
{
var reply = await _star.SendNodeCommandAsync(nodeCode, command, args, 0, 3600, 5);
return new { status = reply?.Status.ToString(), data = reply?.Data };
}
public async Task<Object> SendAppCommandAsync(String appName, String clientId, String command, String args)
{
var reply = await _star.SendAppCommandAsync(appName, clientId, command, args, 0, 3600, 5);
return new { status = reply?.Status.ToString(), data = reply?.Data };
}
public async Task<Object> ReadDeployLogFilesAsync(Int32 deployNodeId)
{
var dn = ProjectScopeRules.FilterVisible(AppDeployNode.FindById(deployNodeId)) ?? throw new InvalidOperationException($"Deploy node [{deployNodeId}] does not exist");
var rs = await _deploy.ListLogFilesAsync(dn);
return new { files = rs.Files.Select(f => new { name = f.Name, size = f.Size, last_write_utc = f.LastWriteUtc }).ToArray() };
}
public async Task<Object> ReadDeployLogTailAsync(Int32 deployNodeId, String file, Int64 offset, Int32 maxBytes)
{
var dn = ProjectScopeRules.FilterVisible(AppDeployNode.FindById(deployNodeId)) ?? throw new InvalidOperationException($"Deploy node [{deployNodeId}] does not exist");
var rs = await _deploy.TailLogFileAsync(dn, file, offset, maxBytes);
return new { content = rs.Content, offset = rs.Offset, file_size = rs.FileSize, truncated = rs.Truncated };
}
}
|