-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProviderBackedAgentKernel.cs
More file actions
348 lines (308 loc) · 15 KB
/
ProviderBackedAgentKernel.cs
File metadata and controls
348 lines (308 loc) · 15 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SharpClaw.Code.Agents.Configuration;
using SharpClaw.Code.Agents.Models;
using SharpClaw.Code.Protocol.Events;
using SharpClaw.Code.Protocol.Models;
using SharpClaw.Code.Providers.Abstractions;
using SharpClaw.Code.Providers.Models;
using SharpClaw.Code.Protocol.Enums;
using SharpClaw.Code.Telemetry.Diagnostics;
using SharpClaw.Code.Telemetry.Metrics;
using SharpClaw.Code.Tools.Models;
namespace SharpClaw.Code.Agents.Internal;
/// <summary>
/// Executes the provider-backed core of a SharpClaw agent run,
/// including a multi-iteration tool-calling loop.
/// </summary>
public sealed class ProviderBackedAgentKernel(
IProviderRequestPreflight providerRequestPreflight,
IModelProviderResolver providerResolver,
IAuthFlowService authFlowService,
ToolCallDispatcher toolCallDispatcher,
IOptions<AgentLoopOptions> loopOptions,
ILogger<ProviderBackedAgentKernel> logger)
{
internal async Task<ProviderInvocationResult> ExecuteAsync(
AgentFrameworkRequest request,
ToolExecutionContext? toolExecutionContext,
IReadOnlyList<ProviderToolDefinition>? availableTools,
CancellationToken cancellationToken)
{
var options = loopOptions.Value;
var requestedModel = request.Context.Model;
var requestedProvider = request.Context.Metadata is not null && request.Context.Metadata.TryGetValue("provider", out var metadataProvider)
? metadataProvider
: string.Empty;
var baseMetadata = request.Context.Metadata is null
? null
: new Dictionary<string, string>(request.Context.Metadata, StringComparer.Ordinal);
// Run a single preflight to resolve the effective provider name for auth/resolution
var resolvedRequest = providerRequestPreflight.Prepare(new ProviderRequest(
Id: $"provider-request-{Guid.NewGuid():N}",
SessionId: request.Context.SessionId,
TurnId: request.Context.TurnId,
ProviderName: requestedProvider ?? string.Empty,
Model: requestedModel,
Prompt: request.Context.Prompt,
SystemPrompt: request.Instructions,
OutputFormat: request.Context.OutputFormat,
Temperature: 0.1m,
Metadata: baseMetadata));
var resolvedProviderName = resolvedRequest.ProviderName;
try
{
// --- Auth check ---
AuthStatus authStatus;
try
{
authStatus = await authFlowService.GetStatusAsync(resolvedProviderName, cancellationToken).ConfigureAwait(false);
}
catch (InvalidOperationException)
{
throw CreateMissingProviderException(resolvedProviderName, requestedModel, "auth status lookup");
}
catch (Exception exception)
{
throw new ProviderExecutionException(
resolvedProviderName,
requestedModel,
ProviderFailureKind.AuthenticationUnavailable,
$"Provider '{resolvedProviderName}' authentication probe failed.",
exception);
}
if (!authStatus.IsAuthenticated)
{
logger.LogWarning(
"Provider {ProviderName} is not authenticated for session {SessionId}.",
resolvedProviderName,
request.Context.SessionId);
throw new ProviderExecutionException(
resolvedProviderName,
requestedModel,
ProviderFailureKind.AuthenticationUnavailable,
$"Provider '{resolvedProviderName}' is not authenticated.");
}
// --- Resolve provider ---
IModelProvider provider;
try
{
provider = providerResolver.Resolve(resolvedProviderName);
}
catch (InvalidOperationException)
{
throw CreateMissingProviderException(resolvedProviderName, requestedModel, "provider resolution");
}
// --- Build initial conversation messages ---
// Do not add request.Instructions as a shared "system" chat message here.
// Provider adapters apply system instructions via ProviderRequest.SystemPrompt
// so providers that do not support a native "system" role (e.g. Anthropic)
// do not receive duplicated or remapped instruction turns.
var messages = new List<ChatMessage>();
// Prepend prior-turn conversation history for multi-turn context.
if (request.Context.ConversationHistory is { Count: > 0 } history)
{
messages.AddRange(history);
}
messages.Add(new ChatMessage("user", [new ContentBlock(ContentBlockKind.Text, request.Context.Prompt, null, null, null, null)]));
// --- Tool-calling loop ---
var allProviderEvents = new List<ProviderEvent>();
var allToolResults = new List<ToolResult>();
var allToolEvents = new List<RuntimeEvent>();
var outputSegments = new List<string>();
UsageSnapshot? terminalUsage = null;
ProviderRequest? lastProviderRequest = null;
var iteration = 0;
for (; iteration < options.MaxToolIterations; iteration++)
{
UsageSnapshot? iterationUsage = null;
var providerRequest = providerRequestPreflight.Prepare(new ProviderRequest(
Id: $"provider-request-{Guid.NewGuid():N}",
SessionId: request.Context.SessionId,
TurnId: request.Context.TurnId,
ProviderName: resolvedProviderName,
Model: requestedModel,
Prompt: request.Context.Prompt,
SystemPrompt: request.Instructions,
OutputFormat: request.Context.OutputFormat,
Temperature: 0.1m,
Metadata: baseMetadata,
Messages: messages,
Tools: availableTools,
MaxTokens: options.MaxTokensPerRequest));
lastProviderRequest = providerRequest;
var iterationTextSegments = new List<string>();
var toolUseEvents = new List<ProviderEvent>();
using var providerScope = new ProviderActivityScope(resolvedProviderName, requestedModel, providerRequest.Id);
var providerSw = Stopwatch.StartNew();
try
{
var stream = await provider.StartStreamAsync(providerRequest, cancellationToken).ConfigureAwait(false);
await foreach (var providerEvent in stream.Events.WithCancellation(cancellationToken))
{
allProviderEvents.Add(providerEvent);
if (!providerEvent.IsTerminal && !string.IsNullOrWhiteSpace(providerEvent.Content))
{
iterationTextSegments.Add(providerEvent.Content);
}
if (!string.IsNullOrEmpty(providerEvent.ToolUseId) && !string.IsNullOrEmpty(providerEvent.ToolName))
{
toolUseEvents.Add(providerEvent);
}
if (providerEvent.IsTerminal && providerEvent.Usage is not null)
{
iterationUsage = providerEvent.Usage;
terminalUsage = providerEvent.Usage;
}
}
providerSw.Stop();
providerScope.SetCompleted(iterationUsage?.InputTokens, iterationUsage?.OutputTokens);
SharpClawMeterSource.ProviderDuration.Record(providerSw.Elapsed.TotalMilliseconds);
}
catch (Exception ex)
{
providerSw.Stop();
providerScope.SetError(ex.Message);
throw;
}
// If no tool-use events, accumulate text and break
if (toolUseEvents.Count == 0)
{
outputSegments.AddRange(iterationTextSegments);
break;
}
// Build assistant message with text + tool-use content blocks
var assistantBlocks = new List<ContentBlock>();
var iterationText = string.Concat(iterationTextSegments);
if (!string.IsNullOrEmpty(iterationText))
{
assistantBlocks.Add(new ContentBlock(ContentBlockKind.Text, iterationText, null, null, null, null));
}
foreach (var toolUseEvent in toolUseEvents)
{
assistantBlocks.Add(new ContentBlock(
ContentBlockKind.ToolUse,
null,
toolUseEvent.ToolUseId,
toolUseEvent.ToolName,
toolUseEvent.ToolInputJson,
null));
}
messages.Add(new ChatMessage("assistant", assistantBlocks));
// Dispatch each tool call and collect results
var toolResultBlocks = new List<ContentBlock>();
foreach (var toolUseEvent in toolUseEvents)
{
if (toolExecutionContext is null)
{
// No tool execution context means we cannot dispatch tools
toolResultBlocks.Add(new ContentBlock(
ContentBlockKind.ToolResult,
"Tool execution is not available in this context.",
toolUseEvent.ToolUseId,
null,
null,
true));
continue;
}
var (resultBlock, toolResult, events) = await toolCallDispatcher.DispatchAsync(
toolUseEvent,
toolExecutionContext,
cancellationToken).ConfigureAwait(false);
toolResultBlocks.Add(resultBlock);
allToolResults.Add(toolResult);
allToolEvents.AddRange(events);
}
messages.Add(new ChatMessage("user", toolResultBlocks));
// Accumulate partial text from tool-calling iterations
if (!string.IsNullOrEmpty(iterationText))
{
outputSegments.Add(iterationText);
}
}
// Detect if the loop was exhausted (provider kept requesting tools every iteration).
var toolLoopExhausted = iteration >= options.MaxToolIterations;
if (toolLoopExhausted)
{
logger.LogWarning(
"Tool-calling loop reached maximum iterations ({MaxIterations}) for session {SessionId}; output may be incomplete.",
options.MaxToolIterations,
request.Context.SessionId);
outputSegments.Add($"\n\n[Tool-calling loop reached the maximum of {options.MaxToolIterations} iterations. Output may be incomplete.]");
}
var output = string.Concat(outputSegments);
if (string.IsNullOrWhiteSpace(output))
{
logger.LogWarning(
"Provider {ProviderName} returned no stream content for session {SessionId}; returning placeholder response.",
resolvedProviderName,
request.Context.SessionId);
return CreatePlaceholderResult(request, requestedModel, $"Provider '{resolvedProviderName}' returned no content; using placeholder response.");
}
var usage = terminalUsage ?? new UsageSnapshot(
InputTokens: request.Context.Prompt.Length,
OutputTokens: output.Length,
CachedInputTokens: 0,
TotalTokens: request.Context.Prompt.Length + output.Length,
EstimatedCostUsd: null);
var summary = toolLoopExhausted
? $"Provider response from {resolvedProviderName}/{requestedModel} is incomplete because the tool-calling loop reached the maximum of {options.MaxToolIterations} iterations."
: $"Streamed provider response from {resolvedProviderName}/{requestedModel}.";
return new ProviderInvocationResult(
Output: output,
Usage: usage,
Summary: summary,
ProviderRequest: lastProviderRequest,
ProviderEvents: allProviderEvents,
ToolResults: allToolResults.Count > 0 ? allToolResults : null,
ToolEvents: allToolEvents.Count > 0 ? allToolEvents : null);
}
catch (OperationCanceledException)
{
logger.LogWarning(
"Provider execution was canceled for session {SessionId}, turn {TurnId}.",
request.Context.SessionId,
request.Context.TurnId);
throw;
}
catch (ProviderExecutionException)
{
throw;
}
catch (Exception exception)
{
throw new ProviderExecutionException(
resolvedProviderName,
requestedModel,
ProviderFailureKind.StreamFailed,
$"Provider '{resolvedProviderName}' failed during execution.",
exception);
}
}
/// <summary>
/// Backward-compatible overload for callers that do not need tool calling.
/// </summary>
internal Task<ProviderInvocationResult> ExecuteAsync(AgentFrameworkRequest request, CancellationToken cancellationToken)
=> ExecuteAsync(request, toolExecutionContext: null, availableTools: null, cancellationToken);
private static ProviderInvocationResult CreatePlaceholderResult(AgentFrameworkRequest request, string model, string summary)
{
var output = $"Session {request.Context.SessionId} turn {request.Context.TurnId}: placeholder response for '{request.Context.Prompt}' using model '{model}'.";
var usage = new UsageSnapshot(
InputTokens: request.Context.Prompt.Length,
OutputTokens: output.Length,
CachedInputTokens: 0,
TotalTokens: request.Context.Prompt.Length + output.Length,
EstimatedCostUsd: 0.0001m);
return new ProviderInvocationResult(output, usage, summary, null, null);
}
private static ProviderExecutionException CreateMissingProviderException(
string providerName,
string model,
string stage)
=> new(
providerName,
model,
ProviderFailureKind.MissingProvider,
$"No provider named '{providerName}' was registered during {stage}.");
}