-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLlmAgentContracts.cs
More file actions
311 lines (281 loc) · 14.8 KB
/
Copy pathLlmAgentContracts.cs
File metadata and controls
311 lines (281 loc) · 14.8 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Collections.Generic;
namespace TelegramSearchBot.Model.AI {
public enum AgentTaskKind {
Message = 0,
Continuation = 1
}
public enum AgentTaskStatus {
Pending = 0,
Running = 1,
Completed = 2,
Failed = 3,
Recovering = 4,
Cancelled = 5
}
public enum AgentChunkType {
Snapshot = 0,
Done = 1,
Error = 2,
IterationLimitReached = 3
}
public enum CodingAgentJobStatus {
Pending = 0,
Running = 1,
Completed = 2,
Failed = 3,
Cancelling = 4,
Cancelled = 5,
TimedOut = 6
}
public sealed class AgentUserSnapshot {
public long UserId { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
public bool? IsPremium { get; set; }
public bool? IsBot { get; set; }
}
public sealed class AgentMessageExtensionSnapshot {
public string Name { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
}
public sealed class AgentHistoryMessage {
public long DataId { get; set; }
public DateTime DateTime { get; set; }
public long GroupId { get; set; }
public long MessageId { get; set; }
public long FromUserId { get; set; }
public long ReplyToUserId { get; set; }
public long ReplyToMessageId { get; set; }
public string Content { get; set; } = string.Empty;
public AgentUserSnapshot User { get; set; } = new AgentUserSnapshot();
public List<AgentMessageExtensionSnapshot> Extensions { get; set; } = [];
}
public sealed class AgentModelCapability {
public string Name { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
public sealed class AgentChannelConfig {
public int ChannelId { get; set; }
public string Name { get; set; } = string.Empty;
public string Gateway { get; set; } = string.Empty;
public string ApiKey { get; set; } = string.Empty;
public LLMProvider Provider { get; set; }
public int Parallel { get; set; }
public int Priority { get; set; }
public string ModelName { get; set; } = string.Empty;
public List<AgentModelCapability> Capabilities { get; set; } = [];
// Phase 2:解析出的 binding 快照(旧二进制/legacy 行全部为 null,走 Provider/Gateway)。
public int? BindingId { get; set; }
public string BindingEndpoint { get; set; } = string.Empty;
public LlmProtocol? BindingProtocol { get; set; }
public LlmAuthProfile? BindingAuthProfile { get; set; }
}
public sealed class AgentExecutionTask {
public string TaskId { get; set; } = Guid.NewGuid().ToString("N");
public AgentTaskKind Kind { get; set; } = AgentTaskKind.Message;
public long ChatId { get; set; }
public long UserId { get; set; }
public long MessageId { get; set; }
public long BotUserId { get; set; }
public string BotName { get; set; } = string.Empty;
public string InputMessage { get; set; } = string.Empty;
public string ModelName { get; set; } = string.Empty;
public int MaxToolCycles { get; set; }
public AgentChannelConfig Channel { get; set; } = new AgentChannelConfig();
public List<AgentHistoryMessage> History { get; set; } = [];
public LlmContinuationSnapshot? ContinuationSnapshot { get; set; }
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
public int RecoveryAttempt { get; set; }
}
public sealed class CodingAgentJobRequest {
public string JobId { get; set; } = Guid.NewGuid().ToString("N");
public long ChatId { get; set; }
public long UserId { get; set; }
public long MessageId { get; set; }
public string Prompt { get; set; } = string.Empty;
public string WorkingDirectory { get; set; } = string.Empty;
public string AgentsJson { get; set; } = string.Empty;
public int TimeoutMinutes { get; set; }
public string Provider { get; set; } = string.Empty;
public string Model { get; set; } = string.Empty;
public string Tools { get; set; } = string.Empty;
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class CodingAgentJobReport {
public string JobId { get; set; } = string.Empty;
public CodingAgentJobStatus Status { get; set; } = CodingAgentJobStatus.Pending;
public long ChatId { get; set; }
public long UserId { get; set; }
public long MessageId { get; set; }
public string Prompt { get; set; } = string.Empty;
public string WorkingDirectory { get; set; } = string.Empty;
public string Summary { get; set; } = string.Empty;
public string Output { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
public string LogPath { get; set; } = string.Empty;
public DateTime StartedAtUtc { get; set; } = DateTime.MinValue;
public DateTime CompletedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class CodingAgentControlCommand {
public string JobId { get; set; } = string.Empty;
public string Action { get; set; } = string.Empty;
public string Reason { get; set; } = string.Empty;
public DateTime RequestedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class AgentStreamChunk {
public string TaskId { get; set; } = string.Empty;
public AgentChunkType Type { get; set; } = AgentChunkType.Snapshot;
public int Sequence { get; set; }
public string Content { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
public LlmContinuationSnapshot? ContinuationSnapshot { get; set; }
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class TelegramAgentToolTask {
public string RequestId { get; set; } = Guid.NewGuid().ToString("N");
public string ToolName { get; set; } = string.Empty;
public Dictionary<string, string> Arguments { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public long ChatId { get; set; }
public long UserId { get; set; }
public long MessageId { get; set; }
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class TelegramAgentToolResult {
public string RequestId { get; set; } = string.Empty;
public bool Success { get; set; }
public string Result { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
public long TelegramMessageId { get; set; }
public DateTime CompletedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class SandboxToolTask {
public string RequestId { get; set; } = Guid.NewGuid().ToString("N");
public string ToolName { get; set; } = string.Empty;
public Dictionary<string, string> Arguments { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public long ChatId { get; set; }
public long UserId { get; set; }
public long MessageId { get; set; }
public string BoxName { get; set; } = string.Empty;
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class SandboxToolResult {
public string RequestId { get; set; } = string.Empty;
public bool Success { get; set; }
public string Result { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
public DateTime CompletedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class AgentSessionInfo {
public long ChatId { get; set; }
public int ProcessId { get; set; }
public int Port { get; set; }
public string Status { get; set; } = "starting";
public string CurrentTaskId { get; set; } = string.Empty;
public DateTime StartedAtUtc { get; set; } = DateTime.UtcNow;
public DateTime LastHeartbeatUtc { get; set; } = DateTime.UtcNow;
public DateTime LastActiveAtUtc { get; set; } = DateTime.UtcNow;
public DateTime ShutdownRequestedAtUtc { get; set; } = DateTime.MinValue;
public string ErrorMessage { get; set; } = string.Empty;
}
public sealed class AgentControlCommand {
public long ChatId { get; set; }
public string Action { get; set; } = string.Empty;
public string Reason { get; set; } = string.Empty;
public DateTime RequestedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class AgentDeadLetterEntry {
public string TaskId { get; set; } = string.Empty;
public long ChatId { get; set; }
public string Reason { get; set; } = string.Empty;
public int RecoveryAttempt { get; set; }
public string Payload { get; set; } = string.Empty;
public string LastContent { get; set; } = string.Empty;
public DateTime FailedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class SubAgentTaskEnvelope {
public string RequestId { get; set; } = Guid.NewGuid().ToString("N");
public string Type { get; set; } = "echo";
public string Payload { get; set; } = string.Empty;
public SubAgentMcpExecuteRequest? McpExecute { get; set; }
public SubAgentBackgroundTaskRequest? BackgroundTask { get; set; }
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
}
public sealed class SubAgentMcpExecuteRequest {
public string ServerName { get; set; } = "subagent";
public string Command { get; set; } = string.Empty;
public List<string> Args { get; set; } = [];
public Dictionary<string, string> Env { get; set; } = new(StringComparer.OrdinalIgnoreCase);
public int TimeoutSeconds { get; set; } = 30;
public string ToolName { get; set; } = string.Empty;
public Dictionary<string, object?> Arguments { get; set; } = new(StringComparer.OrdinalIgnoreCase);
}
public sealed class SubAgentBackgroundTaskRequest {
public string Command { get; set; } = string.Empty;
public List<string> Args { get; set; } = [];
public Dictionary<string, string> Env { get; set; } = new(StringComparer.OrdinalIgnoreCase);
public string WorkingDirectory { get; set; } = string.Empty;
public int TimeoutSeconds { get; set; } = 30;
}
public sealed class SubAgentTaskResult {
public string RequestId { get; set; } = string.Empty;
public bool Success { get; set; }
public string Result { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
public int ExitCode { get; set; }
public DateTime CompletedAtUtc { get; set; } = DateTime.UtcNow;
}
/// <summary>
/// Serializable tool definition for exporting main-process tool descriptions to Redis,
/// so that agent processes can register them as proxy tools.
/// </summary>
public sealed class ProxyToolDefinition {
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<ProxyToolParameter> Parameters { get; set; } = [];
}
public sealed class ProxyToolParameter {
public string Name { get; set; } = string.Empty;
public string Type { get; set; } = "string";
public string Description { get; set; } = string.Empty;
public bool Required { get; set; }
}
public static class LlmAgentRedisKeys {
public const string AgentTaskQueue = "AGENT_TASKS";
public const string AgentTaskDeadLetterQueue = "AGENT_TASKS:DEAD";
public const string TelegramTaskQueue = "TELEGRAM_TASKS";
public const string ActiveTaskSet = "AGENT_ACTIVE_TASKS";
public const string SubAgentTaskQueue = "SUBAGENT_TASKS";
public const string AgentToolDefs = "AGENT_TOOL_DEFS";
public const string AgentChatBatchDueSet = "AGENT_CHAT_BATCH_DUE";
public const string CodingAgentJobQueue = "CODING_AGENT_JOBS";
public const string CodingAgentReportQueue = "CODING_AGENT_REPORTS";
public const string CodingAgentActiveJobSet = "CODING_AGENT_ACTIVE_JOBS";
public static string AgentTaskState(string taskId) => $"AGENT_TASK:{taskId}";
public static string AgentSnapshot(string taskId) => $"AGENT_SNAPSHOT:{taskId}";
public static string AgentTerminal(string taskId) => $"AGENT_TERMINAL:{taskId}";
[Obsolete("Use AgentSnapshot/AgentTerminal instead")]
public static string AgentChunks(string taskId) => $"AGENT_CHUNKS:{taskId}";
[Obsolete("Use AgentSnapshot/AgentTerminal instead")]
public static string AgentChunkIndex(string taskId) => $"AGENT_CHUNK_INDEX:{taskId}";
public static string AgentSession(long chatId) => $"AGENT_SESSION:{chatId}";
public static string AgentControl(long chatId) => $"AGENT_CONTROL:{chatId}";
public static string TelegramResult(string requestId) => $"TELEGRAM_RESULT:{requestId}";
public static string SubAgentResult(string requestId) => $"SUBAGENT_RESULT:{requestId}";
public static string SandboxToolQueue(long chatId) => $"SANDBOX_TOOL_TASKS:{chatId}";
public static string SandboxToolHeartbeat(long chatId) => $"SANDBOX_TOOL_HEARTBEAT:{chatId}";
public static string SandboxToolResult(string requestId) => $"SANDBOX_TOOL_RESULT:{requestId}";
public static string ModelSelectState(long chatId) => $"modelselect:{chatId}:state";
public static string ModelSelectModels(long chatId) => $"modelselect:{chatId}:models";
public static string ImageGenerationModelSelection(long chatId, long userId) => $"image_generation:model_select:{chatId}:{userId}";
public static string MusicGenerationModelSelection(long chatId, long userId) => $"music_generation:model_select:{chatId}:{userId}";
public static string AgentChatBatchList(long chatId) => $"AGENT_CHAT_BATCH:{chatId}:MESSAGES";
public static string AgentChatBatchMeta(long chatId) => $"AGENT_CHAT_BATCH:{chatId}:META";
public static string AgentChatBatchLock(long chatId) => $"AGENT_CHAT_BATCH:{chatId}:LOCK";
public static string AgentChatConfigWarning(long chatId, string warningType) => $"AGENT_CHAT_WARNING:{chatId}:{warningType}";
public static string CodingAgentJobState(string jobId) => $"CODING_AGENT_JOB:{jobId}";
public static string CodingAgentControl(string jobId) => $"CODING_AGENT_CONTROL:{jobId}";
}
}