Skip to content

Commit cb578fb

Browse files
committed
Copilot review.
1 parent a244e1f commit cb578fb

2 files changed

Lines changed: 27 additions & 28 deletions

File tree

‎samples/csharp/hosted-agents/agent-framework/browser-automation/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ authenticate model and Toolbox calls.
6666
Initialize from a directory outside the sample:
6767

6868
```bash
69-
azd ai agent init -t ./browser-automation/azure.yaml
69+
azd ai agent init -m ./browser-automation/azure.yaml
7070
azd provision
7171
```
7272

‎samples/csharp/hosted-agents/agent-framework/browser-automation/src/bat-csharp-maf/utils/Middlewares.cs‎

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@ public static class Middlewares
2222
CancellationToken cancellationToken)
2323
{
2424
var functionName = context.Function.Name;
25-
var arguments = context.CallContent?.Arguments;
26-
var safeArguments = arguments is null
27-
? ""
28-
: Redaction.Redact(
29-
string.Join(", ", arguments.Select(pair => $"{pair.Key}={pair.Value}")));
30-
Logger.LogInformation(
31-
"[tool] name={Name} arguments={Arguments}",
32-
functionName,
33-
safeArguments);
25+
Logger.LogInformation("[tool] name={Name}", functionName);
3426

3527
var result = await next(context, cancellationToken);
3628

3729
if (functionName.EndsWith(
3830
"close_browser_session",
3931
StringComparison.OrdinalIgnoreCase))
4032
{
41-
_liveViewUrl = null;
33+
Interlocked.Exchange(ref _liveViewUrl, null);
4234
return result;
4335
}
4436

@@ -67,7 +59,9 @@ public static class Middlewares
6759
return result;
6860
}
6961

70-
_liveViewUrl = liveViewUrl;
62+
if (!string.IsNullOrEmpty(liveViewUrl))
63+
Interlocked.Exchange(ref _liveViewUrl, liveViewUrl);
64+
7165
Logger.LogInformation(
7266
"[browser-session] id={BrowserSessionId} liveViewUrl={LiveViewUrl}",
7367
browserSessionId,
@@ -100,14 +94,16 @@ public static async Task<AgentResponse> LiveViewUrlMiddleware(
10094
session,
10195
options,
10296
cancellationToken);
103-
if (!string.IsNullOrEmpty(_liveViewUrl))
97+
98+
var liveViewUrl = Interlocked.Exchange(ref _liveViewUrl, null);
99+
if (!string.IsNullOrEmpty(liveViewUrl))
104100
{
105101
Logger.LogInformation(
106102
"[live-view] injecting non-streaming response url={LiveViewUrl}",
107-
_liveViewUrl);
103+
liveViewUrl);
108104
response.Messages.Add(new ChatMessage(
109105
ChatRole.Assistant,
110-
$"\n\n🔴 [Browser Live View]({_liveViewUrl})"));
106+
$"\n\n🔴 [Browser Live View]({liveViewUrl})"));
111107
}
112108
return response;
113109
}
@@ -120,38 +116,36 @@ public static async IAsyncEnumerable<AgentResponseUpdate>
120116
AIAgent innerAgent,
121117
[EnumeratorCancellation] CancellationToken cancellationToken)
122118
{
123-
var initialUrl = _liveViewUrl;
124-
var injected = !string.IsNullOrEmpty(initialUrl);
125-
126119
await foreach (var update in innerAgent.RunStreamingAsync(
127120
messages,
128121
session,
129122
options,
130123
cancellationToken))
131124
{
132-
if (!injected && !string.IsNullOrEmpty(_liveViewUrl))
125+
var liveViewUrl = Interlocked.Exchange(ref _liveViewUrl, null);
126+
if (!string.IsNullOrEmpty(liveViewUrl))
133127
{
134128
Logger.LogInformation(
135129
"[live-view] injecting streaming response url={LiveViewUrl}",
136-
_liveViewUrl);
130+
liveViewUrl);
137131
yield return new AgentResponseUpdate(
138132
ChatRole.Assistant,
139133
[new TextContent(
140-
$"🔴 Created Browser Session: [Live View]({_liveViewUrl})\n\n")]);
141-
injected = true;
134+
$"🔴 Created Browser Session: [Live View]({liveViewUrl})\n\n")]);
142135
}
143136
yield return update;
144137
}
145138

146-
if (!string.IsNullOrEmpty(_liveViewUrl))
139+
var finalLiveViewUrl = Interlocked.Exchange(ref _liveViewUrl, null);
140+
if (!string.IsNullOrEmpty(finalLiveViewUrl))
147141
{
148142
Logger.LogInformation(
149143
"[live-view] appending final streaming response url={LiveViewUrl}",
150-
_liveViewUrl);
144+
finalLiveViewUrl);
151145
yield return new AgentResponseUpdate(
152146
ChatRole.Assistant,
153147
[new TextContent(
154-
$"\n\n🔴 [Browser Live View]({_liveViewUrl})")])
148+
$"\n\n🔴 [Browser Live View]({finalLiveViewUrl})")])
155149
{
156150
FinishReason = ChatFinishReason.Stop,
157151
};
@@ -179,12 +173,16 @@ private static string SerializeResult(object? result)
179173

180174
private static bool TryExtractBrowserSession(
181175
JsonElement element,
182-
out string? browserSessionId,
176+
out string browserSessionId,
183177
out string? liveViewUrl)
184178
{
185-
browserSessionId = null;
179+
string? extractedBrowserSessionId = null;
186180
liveViewUrl = null;
187-
ExtractBrowserSessionValues(element, ref browserSessionId, ref liveViewUrl);
181+
ExtractBrowserSessionValues(
182+
element,
183+
ref extractedBrowserSessionId,
184+
ref liveViewUrl);
185+
browserSessionId = extractedBrowserSessionId ?? "";
188186
return !string.IsNullOrEmpty(browserSessionId);
189187
}
190188

@@ -250,4 +248,5 @@ private static void ExtractBrowserSessionValues(
250248
break;
251249
}
252250
}
251+
253252
}

0 commit comments

Comments
 (0)