-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathSuperpowersQuickActionCardHelper.cs
More file actions
237 lines (217 loc) · 7.86 KB
/
SuperpowersQuickActionCardHelper.cs
File metadata and controls
237 lines (217 loc) · 7.86 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using WebCodeCli.Domain.Domain.Model;
using WebCodeCli.Domain.Domain.Model.Channels;
using WebCodeCli.Domain.Domain.Service;
namespace WebCodeCli.Domain.Domain.Service.Channels;
internal static class SuperpowersQuickActionCardHelper
{
private const string ExecutionControlRow = "execution_control_row";
private const string PlanActionRow = "plan_action_row";
private const string CapabilityActionRow = "capability_action_row";
private const string SessionConfirmActionRow = "session_confirm_action_row";
public static bool ShouldShowPlanActions(
IEnumerable<string?>? sessionContents,
bool hasPlanFiles)
{
return hasPlanFiles
&& SessionContainsSuperpowers(sessionContents);
}
public static FeishuStreamingCardBottomPrompt? CreateBottomPrompt(
string sessionId,
string chatKey,
string? toolId,
SuperpowersCapabilitySnapshot? capabilityState = null)
{
if (capabilityState?.State == SuperpowersCapabilityState.Unavailable)
{
return null;
}
return new FeishuStreamingCardBottomPrompt
{
InputName = SuperpowersQuickActionDefaults.QuickInputFieldName,
InputLabel = SuperpowersQuickActionDefaults.InstructionText,
Placeholder = SuperpowersQuickActionDefaults.QuickInputPlaceholder,
DefaultValue = string.Empty,
ButtonText = SuperpowersQuickActionDefaults.QuickSubmitButtonText,
ButtonType = "primary",
Value = BuildActionValue(
FeishuHelpCardAction.SubmitSuperpowersQuickInputAction,
sessionId,
chatKey,
toolId)
};
}
public static IReadOnlyList<FeishuStreamingCardBottomAction> CreateBottomActions(
string sessionId,
string chatKey,
string? toolId,
bool showPlanActions,
SuperpowersCapabilitySnapshot? capabilityState = null,
bool showStopAction = false)
{
var actions = new List<FeishuStreamingCardBottomAction>();
if (capabilityState?.State == SuperpowersCapabilityState.Unavailable)
{
actions.Add(new FeishuStreamingCardBottomAction
{
Text = SuperpowersQuickActionDefaults.CapabilityRetryButtonText,
Type = "default",
RowKey = CapabilityActionRow,
Value = BuildActionValue(
FeishuHelpCardAction.RetrySuperpowersCapabilityDetectionAction,
sessionId,
chatKey,
toolId)
});
if (showStopAction)
{
actions.Add(CreateStopAction(sessionId, chatKey, toolId));
}
return actions;
}
actions.Add(new FeishuStreamingCardBottomAction
{
Text = SuperpowersQuickActionDefaults.ContinueButtonText,
Type = "default",
RowKey = ExecutionControlRow,
Value = BuildActionValue(
FeishuHelpCardAction.ContinueSuperpowersAction,
sessionId,
chatKey,
toolId)
});
if (showStopAction)
{
actions.Add(CreateStopAction(sessionId, chatKey, toolId));
}
if (showPlanActions)
{
actions.AddRange(
[
new FeishuStreamingCardBottomAction
{
Text = SuperpowersQuickActionDefaults.ExecutePlanButtonText,
Type = "default",
RowKey = PlanActionRow,
Value = BuildActionValue(
FeishuHelpCardAction.ExecuteSuperpowersPlanAction,
sessionId,
chatKey,
toolId)
},
new FeishuStreamingCardBottomAction
{
Text = SuperpowersQuickActionDefaults.ExecuteSubagentPlanButtonText,
Type = "primary",
RowKey = PlanActionRow,
Value = BuildActionValue(
FeishuHelpCardAction.ExecuteSuperpowersSubagentPlanAction,
sessionId,
chatKey,
toolId)
}
]);
}
return actions;
}
public static string? MergeCapabilityStatusMarkdown(
string? statusMarkdown,
SuperpowersCapabilitySnapshot? capabilityState)
{
if (capabilityState?.State != SuperpowersCapabilityState.Unavailable
|| string.IsNullOrWhiteSpace(capabilityState.Message))
{
return statusMarkdown;
}
var capabilityMessage = $"⚠️ {capabilityState.Message},请点击“{SuperpowersQuickActionDefaults.CapabilityRetryButtonText}”";
return string.IsNullOrWhiteSpace(statusMarkdown)
? capabilityMessage
: $"{statusMarkdown}\n{capabilityMessage}";
}
public static string BuildSessionMismatchConfirmationMarkdown(
string boundSessionId,
string currentSessionId)
{
return
"⚠️ 当前激活会话已经变化,已暂停直接执行。\n\n" +
$"卡片绑定会话:`{boundSessionId}`\n" +
$"当前激活会话:`{currentSessionId}`\n\n" +
"请选择接下来要对哪个会话执行这次 Superpowers 操作。";
}
public static IReadOnlyList<FeishuStreamingCardBottomAction> CreateSessionMismatchConfirmActions(
string boundSessionId,
string currentSessionId,
string chatKey,
string? toolId,
string command)
{
return
[
new FeishuStreamingCardBottomAction
{
Text = "继续原会话",
Type = "default",
RowKey = SessionConfirmActionRow,
Value = new
{
action = FeishuHelpCardAction.ConfirmBoundSuperpowersAction,
session_id = boundSessionId,
chat_key = chatKey,
tool_id = toolId,
command
}
},
new FeishuStreamingCardBottomAction
{
Text = "改为当前会话",
Type = "primary",
RowKey = SessionConfirmActionRow,
Value = new
{
action = FeishuHelpCardAction.ConfirmCurrentSuperpowersAction,
session_id = currentSessionId,
chat_key = chatKey,
tool_id = toolId,
command
}
}
];
}
private static object BuildActionValue(
string action,
string sessionId,
string chatKey,
string? toolId)
{
return new
{
action,
session_id = sessionId,
chat_key = chatKey,
tool_id = toolId
};
}
private static FeishuStreamingCardBottomAction CreateStopAction(
string sessionId,
string chatKey,
string? toolId)
{
return new FeishuStreamingCardBottomAction
{
Text = SuperpowersQuickActionDefaults.StopButtonText,
Type = "default",
RowKey = ExecutionControlRow,
Value = BuildActionValue(
FeishuHelpCardAction.StopStreamingExecutionAction,
sessionId,
chatKey,
toolId)
};
}
private static bool SessionContainsSuperpowers(IEnumerable<string?>? sessionContents)
{
return sessionContents != null
&& sessionContents.Any(content =>
!string.IsNullOrWhiteSpace(content)
&& content.Contains("superpowers", StringComparison.OrdinalIgnoreCase));
}
}