Skip to content

Commit 658020f

Browse files
committed
fix stop button
1 parent 0542c12 commit 658020f

7 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/MaIN.Core/Hub/Contexts/ChatContext.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ public IChatConfigurationBuilder DisableCache()
198198
public async Task<ChatResult> CompleteAsync(
199199
bool translate = false, // Move to WithTranslate
200200
bool interactive = false, // Move to WithInteractive
201-
Func<LLMTokenValue?, Task>? changeOfValue = null)
201+
Func<LLMTokenValue?, Task>? changeOfValue = null,
202+
CancellationToken cancellationToken = default)
202203
{
203204
if (_chat.ModelInstance is null)
204205
{
@@ -219,7 +220,7 @@ public async Task<ChatResult> CompleteAsync(
219220
{
220221
await _chatService.Create(_chat);
221222
}
222-
var result = await _chatService.Completions(_chat, translate, interactive, changeOfValue);
223+
var result = await _chatService.Completions(_chat, translate, interactive, changeOfValue, cancellationToken);
223224
_files = [];
224225
return result;
225226
}

src/MaIN.Core/Hub/Contexts/Interfaces/ChatContext/IChatConfigurationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,5 @@ public interface IChatConfigurationBuilder : IChatActions
104104
/// <param name="interactive">A flag indicating whether the chat session should be interactive. Default is false.</param>
105105
/// <param name="changeOfValue">An optional callback invoked whenever a new token or update is received during streaming.</param>
106106
/// <returns>A <see cref="ChatResult"/> object containing the result of the completed chat session.</returns>
107-
Task<ChatResult> CompleteAsync(bool translate = false, bool interactive = false, Func<LLMTokenValue?, Task>? changeOfValue = null);
107+
Task<ChatResult> CompleteAsync(bool translate = false, bool interactive = false, Func<LLMTokenValue?, Task>? changeOfValue = null, CancellationToken cancellationToken = default);
108108
}

src/MaIN.InferPage/Components/Pages/Home.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
{
385385
await InvokeAsync(async () => await JS.InvokeVoidAsync("scrollManager.scrollToBottomSmooth", cancellationToken, _bottomElement));
386386
}
387-
});
387+
}, cancellationToken: cancellationToken);
388388

389389
await completionTask.WaitAsync(cancellationToken);
390390

src/MaIN.InferPage/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class Utils
88
public static BackendType BackendType { get; set; } = BackendType.Self;
99
public static bool HasApiKey { get; set; }
1010
public static bool IsLocal => BackendType == BackendType.Self || (BackendType == BackendType.Ollama && !HasApiKey);
11-
public static string? Model = "gemma3:4b";
11+
public static string? Model = "gemma3-4b";
1212
public static bool Reason { get; set; }
1313
public static bool Visual => VisualModels.Contains(Model);
1414
private static readonly string[] VisualModels = ["FLUX.1_Shnell", "FLUX.1", "dall-e-3", "dall-e", "imagen", "imagen-3"]; //user might type different names

src/MaIN.Services/Services/Abstract/IChatService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Task<ChatResult> Completions(
1111
Chat chat,
1212
bool translatePrompt = false,
1313
bool interactiveUpdates = false,
14-
Func<LLMTokenValue?, Task>? changeOfValue = null);
14+
Func<LLMTokenValue?, Task>? changeOfValue = null,
15+
CancellationToken cancellationToken = default);
1516
Task Delete(string id);
1617
Task<Chat> GetById(string id);
1718
Task<List<Chat>> GetAll();

src/MaIN.Services/Services/ChatService.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ public async Task<ChatResult> Completions(
3030
Chat chat,
3131
bool translate = false,
3232
bool interactiveUpdates = false,
33-
Func<LLMTokenValue?, Task>? changeOfValue = null)
33+
Func<LLMTokenValue?, Task>? changeOfValue = null,
34+
CancellationToken cancellationToken = default)
3435
{
3536
if (chat.ModelId == ImageGenService.LocalImageModels.FLUX)
3637
{
3738
chat.Visual = true; // TODO: add IImageGenModel interface and check for that instead
3839
}
39-
chat.Backend ??= chat.ModelInstance?.Backend ?? settings.BackendType;
40+
chat.Backend = settings.BackendType;
4041

4142
chat.Messages.Where(x => x.Type == MessageType.NotSet).ToList()
4243
.ForEach(x => x.Type = chat.Backend != BackendType.Self ? MessageType.CloudLLM : MessageType.LocalLLM);
@@ -59,13 +60,13 @@ public async Task<ChatResult> Completions(
5960
}))];
6061
}
6162

62-
var result = chat.Visual
63-
? await imageGenServiceFactory.CreateService(chat.Backend.Value)!.Send(chat)
63+
var result = chat.Visual
64+
? await imageGenServiceFactory.CreateService(chat.Backend.Value)!.Send(chat)
6465
: await llmServiceFactory.CreateService(chat.Backend.Value).Send(chat, new ChatRequestOptions()
6566
{
6667
InteractiveUpdates = interactiveUpdates,
6768
TokenCallback = changeOfValue
68-
});
69+
}, cancellationToken);
6970

7071
if (translate)
7172
{

src/MaIN.Services/Services/LLMService/OpenAiCompatibleService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ private async Task ProcessStreamingChatAsync(
626626

627627
while (!reader.EndOfStream)
628628
{
629+
cancellationToken.ThrowIfCancellationRequested();
630+
629631
var line = await reader.ReadLineAsync(cancellationToken);
630632
if (string.IsNullOrWhiteSpace(line))
631633
continue;

0 commit comments

Comments
 (0)