mirror of
				https://github.com/MindWorkAI/AI-Studio.git
				synced 2025-11-04 10:00:21 +00:00 
			
		
		
		
	Added thread-safe random class
This commit is contained in:
		
							parent
							
								
									2cea53d2a9
								
							
						
					
					
						commit
						b74fd644ff
					
				@ -1,6 +1,7 @@
 | 
			
		||||
using AIStudio.Chat;
 | 
			
		||||
using AIStudio.Provider;
 | 
			
		||||
using AIStudio.Settings;
 | 
			
		||||
using AIStudio.Tools;
 | 
			
		||||
 | 
			
		||||
using Microsoft.AspNetCore.Components;
 | 
			
		||||
 | 
			
		||||
@ -15,7 +16,7 @@ public abstract partial class AssistantBase : ComponentBase
 | 
			
		||||
    protected IJSRuntime JsRuntime { get; init; } = null!;
 | 
			
		||||
 | 
			
		||||
    [Inject]
 | 
			
		||||
    protected Random RNG { get; set; } = null!;
 | 
			
		||||
    protected ThreadSafeRandom RNG { get; init; } = null!;
 | 
			
		||||
    
 | 
			
		||||
    protected abstract string Title { get; }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
@ -22,7 +22,7 @@ public partial class Workspaces : ComponentBase
 | 
			
		||||
    private IDialogService DialogService { get; set; } = null!;
 | 
			
		||||
    
 | 
			
		||||
    [Inject]
 | 
			
		||||
    public Random RNG { get; set; } = null!;
 | 
			
		||||
    private ThreadSafeRandom RNG { get; init; } = null!;
 | 
			
		||||
    
 | 
			
		||||
    [Parameter]
 | 
			
		||||
    public ChatThread? CurrentChatThread { get; set; }
 | 
			
		||||
 | 
			
		||||
@ -25,7 +25,7 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable
 | 
			
		||||
    public IJSRuntime JsRuntime { get; init; } = null!;
 | 
			
		||||
 | 
			
		||||
    [Inject]
 | 
			
		||||
    public Random RNG { get; set; } = null!;
 | 
			
		||||
    private ThreadSafeRandom RNG { get; init; } = null!;
 | 
			
		||||
    
 | 
			
		||||
    [Inject]
 | 
			
		||||
    public IDialogService DialogService { get; set; } = null!;
 | 
			
		||||
 | 
			
		||||
@ -29,7 +29,7 @@ builder.Services.AddSingleton(MessageBus.INSTANCE);
 | 
			
		||||
builder.Services.AddSingleton<Rust>();
 | 
			
		||||
builder.Services.AddMudMarkdownClipboardService<MarkdownClipboardService>();
 | 
			
		||||
builder.Services.AddSingleton<SettingsManager>();
 | 
			
		||||
builder.Services.AddSingleton<Random>();
 | 
			
		||||
builder.Services.AddSingleton<ThreadSafeRandom>();
 | 
			
		||||
builder.Services.AddHostedService<UpdateService>();
 | 
			
		||||
builder.Services.AddHostedService<TemporaryChatService>();
 | 
			
		||||
builder.Services.AddRazorComponents()
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										88
									
								
								app/MindWork AI Studio/Tools/ThreadSafeRandom.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								app/MindWork AI Studio/Tools/ThreadSafeRandom.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,88 @@
 | 
			
		||||
namespace AIStudio.Tools;
 | 
			
		||||
 | 
			
		||||
/// <inheritdoc />
 | 
			
		||||
public sealed class ThreadSafeRandom : Random
 | 
			
		||||
{
 | 
			
		||||
    private static readonly object LOCK = new();
 | 
			
		||||
 | 
			
		||||
    #region Overrides of Random
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override int Next()
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.Next();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override int Next(int maxValue)
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.Next(maxValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override int Next(int minValue, int maxValue)
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.Next(minValue, maxValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override void NextBytes(byte[] buffer)
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            base.NextBytes(buffer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override void NextBytes(Span<byte> buffer)
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            base.NextBytes(buffer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override double NextDouble()
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.NextDouble();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override long NextInt64()
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.NextInt64();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override long NextInt64(long maxValue)
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.NextInt64(maxValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override long NextInt64(long minValue, long maxValue)
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.NextInt64(minValue, maxValue);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public override float NextSingle()
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.NextSingle();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    protected override double Sample()
 | 
			
		||||
    {
 | 
			
		||||
        lock (LOCK)
 | 
			
		||||
            return base.Sample();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #endregion
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user