refine chat stream code

This commit is contained in:
Jicheng Lu 2025-07-08 00:16:21 -05:00
parent 9f7aa8f4d9
commit da097de053
3 changed files with 14 additions and 18 deletions

View file

@ -62,6 +62,7 @@ public class ChatStreamMiddleware
var hub = services.GetRequiredService<IRealtimeHub>();
var conn = hub.SetHubConnection(conversationId);
conn.CurrentAgentId = agentId;
InitEvents(conn);
// load conversation and state
var convService = services.GetRequiredService<IConversationService>();
@ -128,6 +129,11 @@ public class ChatStreamMiddleware
break;
}
return (response.Event, data);
}
private void InitEvents(RealtimeHubConnection conn)
{
conn.OnModelMessageReceived = message =>
JsonSerializer.Serialize(new
{
@ -147,7 +153,5 @@ public class ChatStreamMiddleware
{
@event = "clear"
});
return (response.Event, data);
}
}

View file

@ -329,17 +329,13 @@ public class GoogleRealTimeProvider : IRealTimeCompletion
var words = new List<string>();
HookEmitter.Emit<IRealtimeHook>(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)), agent.Id);
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x =>
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x => new FunctionDef
{
var fn = new FunctionDef
{
Name = x.Name ?? string.Empty,
Description = x.Description ?? string.Empty,
Parameters = x.Parameters != null
Name = x.Name ?? string.Empty,
Description = x.Description ?? string.Empty,
Parameters = x.Parameters != null
? JsonSerializer.Deserialize<FunctionParametersDef>(JsonSerializer.Serialize(x.Parameters))
: null
};
return fn;
}).ToArray();
await HookEmitter.Emit<IContentGeneratingHook>(_services,

View file

@ -326,15 +326,11 @@ public class RealTimeCompletionProvider : IRealTimeCompletion
var (prompt, messages, options) = PrepareOptions(agent, []);
var instruction = messages.FirstOrDefault()?.Content.FirstOrDefault()?.Text ?? agent?.Description ?? string.Empty;
var functions = options.Tools.Select(x =>
var functions = options.Tools.Select(x => new FunctionDef
{
var fn = new FunctionDef
{
Name = x.FunctionName,
Description = x.FunctionDescription
};
fn.Parameters = JsonSerializer.Deserialize<FunctionParametersDef>(x.FunctionParameters);
return fn;
Name = x.FunctionName,
Description = x.FunctionDescription,
Parameters = JsonSerializer.Deserialize<FunctionParametersDef>(x.FunctionParameters)
}).ToArray();
var realtimeModelSettings = _services.GetRequiredService<RealtimeModelSettings>();