Skip to content

Commit e641b08

Browse files
🧩 Refactor(Workflow): 更新触发器类型处理逻辑,移除冗余代码
1 parent 7a88c62 commit e641b08

11 files changed

Lines changed: 26 additions & 101 deletions

File tree

KitX Clients/KitX Workflow/KitX.Workflow.Test/KcsCompileTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void RunKcsCompileTest(string kcsPath, bool withRoundTrip = false)
6060

6161
Console.WriteLine($"Name: {kcs.Name}");
6262
Console.WriteLine($"Id: {kcs.Id}");
63-
Console.WriteLine($"Trigger: {kcs.TriggerType}");
63+
Console.WriteLine($"Trigger: {kcs.TriggerConfig?.TriggerType ?? "Manual"}");
6464
Console.WriteLine($"UseBlockMode: {kcs.UseBlockMode}");
6565
Console.WriteLine($"Helpers: {(kcs.HelperFunctions?.Count ?? 0)}");
6666
Console.WriteLine();

KitX Clients/KitX Workflow/KitX.Workflow/BlockScripting/BlockScriptServiceImpl.cs

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,26 @@ namespace KitX.Workflow.BlockScripting;
1616
internal class BlockScriptServiceImpl : IBlockScriptService
1717
{
1818
private readonly WorkflowRuntimeState _state;
19-
private RealPluginManager? _realPluginManager;
19+
private readonly BlockScriptExecutor _executor;
2020

2121
/// <summary>
2222
/// Initializes a new instance of BlockScriptServiceImpl.
2323
/// </summary>
2424
/// <param name="state">Shared runtime state.</param>
25-
/// <param name="realPluginManager">RealPluginManager instance from DI container (optional).</param>
26-
internal BlockScriptServiceImpl(WorkflowRuntimeState state, RealPluginManager? realPluginManager = null)
25+
/// <param name="executor">
26+
/// The DI-registered, plugin-manager-wired <see cref="IBlockScriptExecutor"/> singleton.
27+
/// Must be the same instance registered in <c>AddKitXWorkflow</c> (where its
28+
/// <c>SetPluginManager</c> is called with the <see cref="RealPluginManager"/>), so that
29+
/// <c>G.PluginCall(...)</c> dispatches to live plugins during execution. Injecting it here
30+
/// (instead of lazily constructing a bare <c>new BlockScriptExecutor()</c>) eliminates the
31+
/// prior dual-instance bug where the trigger execution path ran on an unwired executor.
32+
/// </param>
33+
internal BlockScriptServiceImpl(WorkflowRuntimeState state, IBlockScriptExecutor executor)
2734
{
2835
_state = state;
29-
_realPluginManager = realPluginManager;
30-
TrySetPluginManager();
31-
}
32-
33-
/// <summary>
34-
/// Sets the RealPluginManager after initialization.
35-
/// This is needed when RealPluginManager is resolved after BlockScriptServiceImpl is created.
36-
/// </summary>
37-
internal void SetRealPluginManager(RealPluginManager? realPluginManager)
38-
{
39-
_realPluginManager = realPluginManager;
40-
TrySetPluginManager();
41-
}
42-
43-
/// <summary>
44-
/// Try to set the plugin manager on the BlockScriptExecutor if conditions are met.
45-
/// </summary>
46-
private void TrySetPluginManager()
47-
{
48-
if (_state.BlockScriptExecutor != null && _state.IsParserInitialized && _realPluginManager != null)
49-
{
50-
Log.Information("[BlockScriptServiceImpl] Setting RealPluginManager. HashCode: {HashCode}", _realPluginManager.GetHashCode());
51-
_state.BlockScriptExecutor.SetPluginManager(_realPluginManager);
52-
}
36+
// The contract guarantees a wired executor; cast once to the concrete type the
37+
// pipeline (ExecuteAsync / CompileForPersistence / PreloadFromDisk / Validate) needs.
38+
_executor = (BlockScriptExecutor)executor;
5339
}
5440

5541
/// <summary>
@@ -60,23 +46,9 @@ private void TrySetPluginManager()
6046
BuiltinFunctionRegistry.Discover(typeof(BuiltinFunctionRegistry).Assembly));
6147

6248
/// <summary>
63-
/// Gets the BlockScript executor, creating it if necessary.
64-
/// The executor is initialized with the plugin manager if the parser is initialized.
49+
/// Gets the DI-injected BlockScript executor (single, plugin-manager-wired instance).
6550
/// </summary>
66-
private BlockScriptExecutor BlockScriptExecutor
67-
{
68-
get
69-
{
70-
if (_state.BlockScriptExecutor == null)
71-
{
72-
Log.Information("[BlockScriptServiceImpl] Creating new BlockScriptExecutor, IsParserInitialized = {_IsParserInitialized}",
73-
_state.IsParserInitialized);
74-
_state.BlockScriptExecutor = new BlockScriptExecutor();
75-
TrySetPluginManager();
76-
}
77-
return _state.BlockScriptExecutor;
78-
}
79-
}
51+
private BlockScriptExecutor BlockScriptExecutor => _executor;
8052

8153
/// <inheritdoc />
8254
public BlockScriptParseResult ParseBlockScript(string sourceCode)

KitX Clients/KitX Workflow/KitX.Workflow/BuiltinFunctions/CreateWorkflowFunction.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public string CreateWorkflow(string name, string blockScriptSource)
7575
LastModifiedTime = DateTime.UtcNow,
7676
UseBlockMode = true,
7777
BlockScriptSource = blockScriptSource,
78-
TriggerType = "Manual",
7978
Author = "AI Assistant",
8079
Description = "Workflow generated by KitX AI Assistant"
8180
};

KitX Clients/KitX Workflow/KitX.Workflow/BuiltinFunctions/ListWorkflowsFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public string ListWorkflows()
6767
w.Id,
6868
w.Name,
6969
w.Description,
70-
w.TriggerType
70+
TriggerType = w.TriggerConfig?.TriggerType ?? "Manual"
7171
});
7272
return JsonSerializer.Serialize(info);
7373
}

KitX Clients/KitX Workflow/KitX.Workflow/Hosting/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ public static IServiceCollection AddKitXWorkflow(this IServiceCollection service
3232
{
3333
Log.Information("[AddKitXWorkflow] Registering workflow services...");
3434

35-
// IBlockScriptService is created via factory to inject RealPluginManager from DI.
35+
// IBlockScriptService is created via factory. It receives the same DI-registered,
36+
// plugin-manager-wired IBlockScriptExecutor singleton (registered below) so the trigger
37+
// execution path runs plugins through the wired executor, not a bare new instance.
3638
// WorkflowScriptService facade is kept as the backward-compat singleton graph.
3739
services.AddSingleton<IBlockScriptService>(provider =>
3840
{
3941
var state = WorkflowScriptService.RuntimeState;
4042
var rpm = provider.GetRequiredService<RealPluginManager>();
41-
var service = new BlockScriptServiceImpl(state, rpm);
42-
Log.Information("[DI] IBlockScriptService created with RealPluginManager. HashCode: {HashCode}", rpm.GetHashCode());
43+
var executor = provider.GetRequiredService<IBlockScriptExecutor>();
44+
var service = new BlockScriptServiceImpl(state, executor);
45+
Log.Information("[DI] IBlockScriptService created with RealPluginManager + wired executor. RPM HashCode: {HashCode}", rpm.GetHashCode());
4346
return service;
4447
});
4548
// IBlockScriptPipelineService was removed (zero interface-type consumers;

KitX Clients/KitX Workflow/KitX.Workflow/Services/WorkflowCase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,5 @@ public class WorkflowCase : IWorkflowCase
2727

2828
public DateTime LastModifiedTime { get; set; } = DateTime.UtcNow;
2929

30-
public string TriggerType { get; set; } = "Manual";
31-
3230
public TriggerConfig? TriggerConfig { get; set; }
3331
}

KitX Clients/KitX Workflow/KitX.Workflow/Services/WorkflowPluginService.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using KitX.Core.Contract.Workflow;
22
using KitX.Core.Contract.Plugin;
3-
using KitX.Workflow.Hosting;
43
using KitX.Shared.CSharp.Plugin;
54
using Microsoft.CodeAnalysis.CSharp;
65
using Serilog;
@@ -24,32 +23,6 @@ internal WorkflowPluginService(WorkflowRuntimeState state)
2423
_state = state;
2524
}
2625

27-
/// <inheritdoc />
28-
public void InitializePluginManager()
29-
{
30-
if (_state.IsParserInitialized) return;
31-
32-
try
33-
{
34-
if (!ServiceLocator.IsInitialized)
35-
{
36-
Log.Error("[WorkflowPluginService] Cannot initialize plugin manager: ServiceLocator not initialized");
37-
return;
38-
}
39-
40-
var pluginServer = ServiceLocator.GetRequiredService<IPluginServer>();
41-
var realPluginManager = new RealPluginManager(pluginServer);
42-
43-
_state.IsParserInitialized = true;
44-
Log.Information("[WorkflowPluginService] Real plugin manager initialized");
45-
}
46-
catch (Exception ex)
47-
{
48-
Log.Error($"[WorkflowPluginService] Failed to initialize real plugin manager: {ex.Message}");
49-
_state.IsParserInitialized = true;
50-
}
51-
}
52-
5326
/// <inheritdoc />
5427
public void UpdateAvailablePlugins(List<PluginInfo> plugins)
5528
{

KitX Clients/KitX Workflow/KitX.Workflow/Services/WorkflowRuntimeState.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ internal class WorkflowRuntimeState
2222
/// </summary>
2323
internal CSharpScriptEngine? Engine;
2424

25-
/// <summary>
26-
/// Whether the plugin manager has been initialized.
27-
/// </summary>
28-
internal bool IsParserInitialized;
29-
3025
/// <summary>
3126
/// Available plugins for workflow execution.
3227
/// </summary>
@@ -36,9 +31,4 @@ internal class WorkflowRuntimeState
3631
/// Block script parser instance.
3732
/// </summary>
3833
internal BlockScriptParser? BlockScriptParser;
39-
40-
/// <summary>
41-
/// Block script executor instance.
42-
/// </summary>
43-
internal BlockScriptExecutor? BlockScriptExecutor;
4434
}

KitX Clients/KitX Workflow/KitX.Workflow/Services/WorkflowStorageService.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public async Task<IWorkflowCase> CreateWorkflowAsync(string name, string? descri
6060
Author = string.Empty,
6161
CreatedTime = now,
6262
LastModifiedTime = now,
63-
TriggerType = "Manual",
6463
UseBlockMode = true,
6564
BlockScriptSource = GetDefaultBlockScriptTemplate(),
6665
MainProgram = string.Empty,
@@ -84,7 +83,6 @@ public async Task<IWorkflowCase> CreateWorkflowAsync(string name, string? descri
8483
ScriptPath = filePath,
8584
CreatedTime = now,
8685
LastModifiedTime = now,
87-
TriggerType = "Manual",
8886
};
8987
}
9088

@@ -169,11 +167,6 @@ public async Task<IReadOnlyList<IWorkflowCase>> DiscoverWorkflowsAsync()
169167
? File.GetCreationTimeUtc(file)
170168
: kcs.CreatedTime;
171169

172-
// Prefer TriggerConfig (structured) over legacy TriggerType (string)
173-
var triggerConfig = kcs.TriggerConfig;
174-
var triggerType = triggerConfig?.TriggerType
175-
?? (string.IsNullOrEmpty(kcs.TriggerType) ? "Manual" : kcs.TriggerType);
176-
177170
results.Add(new WorkflowCase
178171
{
179172
Id = id,
@@ -184,8 +177,7 @@ public async Task<IReadOnlyList<IWorkflowCase>> DiscoverWorkflowsAsync()
184177
ScriptPath = file,
185178
CreatedTime = createdTime,
186179
LastModifiedTime = kcs.LastModifiedTime == default ? File.GetLastWriteTimeUtc(file) : kcs.LastModifiedTime,
187-
TriggerType = triggerType,
188-
TriggerConfig = triggerConfig,
180+
TriggerConfig = kcs.TriggerConfig,
189181
});
190182
}
191183
catch (Exception ex)
@@ -298,8 +290,6 @@ private void EnsureDirectoryExists()
298290
result.Description = desc.GetString() ?? string.Empty;
299291
if (root.TryGetProperty(nameof(KcsFileFormat.Author), out var author))
300292
result.Author = author.GetString() ?? string.Empty;
301-
if (root.TryGetProperty(nameof(KcsFileFormat.TriggerType), out var triggerType))
302-
result.TriggerType = triggerType.GetString() ?? "Manual";
303293
if (root.TryGetProperty(nameof(KcsFileFormat.UseBlockMode), out var useBlockMode))
304294
result.UseBlockMode = useBlockMode.GetBoolean();
305295
if (root.TryGetProperty(nameof(KcsFileFormat.BlockScriptSource), out var bsSource))

0 commit comments

Comments
 (0)