Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using Microsoft.Extensions.Logging;
using OpenAI;

// Secrets come from the .NET user-secrets store in development and from
// environment variables in CI/production (env vars win on key collisions).
// Secrets come from the .NET user-secrets store and from
// environment variables (env vars win on key collisions).
IConfiguration config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
Expand All @@ -36,8 +36,6 @@ string GetRequired(string key) =>
// UseFunctionInvocation() adds the middleware that actually *executes* the tool
// calls the model requests — without it, attaching the Tavily tool to the
// Researcher agent would let the model ask for a search but nothing would run it.
// Middleware is applied inner-to-outer, so function invocation wraps the raw
// OpenAI client.
//
// UseOpenTelemetry() emits a GenAI span per model round-trip (model name, token
// usage, tool calls). Its source is named "BlogWriter.ChatClient" so the
Expand Down
19 changes: 12 additions & 7 deletions ReviewerAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<string> InvokeAsync(ResearchState state)

if (revisionNum >= ResearchState.MaxRevisions)
{
return "Uh oh - Maximum revisions reached.";
return "APPROVED - Maximum revisions reached.";
}
Comment on lines 57 to 60

// Per-turn input only — the evaluation criteria are on the agent.
Expand All @@ -76,7 +76,7 @@ public async Task<string> InvokeAsync(ResearchState state)
});
AgentResponse response = await _agent.RunAsync(message, options: runOptions);
string content = response.Text;
return !string.IsNullOrEmpty(content) ? content : "APPROVED";
return !string.IsNullOrEmpty(content) ? content : ManageError("No review content returned from the agent.");
}
catch (TokenCapExceededException)
{
Expand All @@ -85,14 +85,19 @@ public async Task<string> InvokeAsync(ResearchState state)
}
catch (Exception e)
{
// Do NOT approve on failure — that would ship an unreviewed draft.
// Returning feedback (not "APPROVED") routes back to the author for
// another attempt; the revision cap still guarantees termination.
Console.WriteLine($"Review error: {e.Message}");
return "Review could not be completed due to a transient error. Please revise and resubmit the draft.";
return ManageError(e.Message);
}
}

private string ManageError(string errorMessage)
{
// Do NOT approve on failure — that would ship an unreviewed draft.
// Returning feedback (not "APPROVED") routes back to the author for
// another attempt; the revision cap still guarantees termination.
Console.WriteLine($"Review error: {errorMessage}");
return "Review could not be completed due to a transient error. Please revise and resubmit the draft.";
}

/// <summary>Node that reviews the draft.</summary>
public async Task<ResearchState> ReviewerNodeAsync(ResearchState state)
{
Expand Down
5 changes: 5 additions & 0 deletions TokenCapChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ private void Track(UsageDetails? usage)
return;
}

if (maxTotalTokens <= 0)
{
throw new InvalidOperationException("Token cap must be a positive number.");
}
Comment on lines +56 to +59

long total = Interlocked.Add(ref _totalTokens, used);
if (total > maxTotalTokens)
{
Expand Down