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
4 changes: 2 additions & 2 deletions Botticelli.AI.YaGpt/Message/YaGpt/YaGptInputMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Botticelli.AI.YaGpt.Message.YaGpt;

public class YaGptInputMessage
{
[JsonPropertyName("modelUri")]
[JsonPropertyName("model_uri")]
public string ModelUri { get; set; }

[JsonPropertyName("completionOptions")]
Expand All @@ -22,7 +22,7 @@ public class CompletionOptions
[JsonPropertyName("temperature")]
public double Temperature { get; set; }

[JsonPropertyName("maxTokens")]
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; set; }
}

Expand Down
5 changes: 2 additions & 3 deletions Botticelli.AI.YaGpt/Provider/YaGptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,10 @@ protected override async Task<HttpResponseMessage> GetGptResponse(AiMessage mess
Role = SystemRole,
Text = Settings.Value.Instruction
},

new()
{
Role = UserRole,
Text = message.Body
Text = message.Body ?? string.Empty
}
],
CompletionOptions = new CompletionOptions
Expand All @@ -114,7 +113,7 @@ protected override async Task<HttpResponseMessage> GetGptResponse(AiMessage mess
yaGptMessage.Messages.AddRange(message.AdditionalMessages?.Select(m => new YaGptMessage
{
Role = UserRole,
Text = m.Body
Text = m.Body ?? string.Empty,
}) ??
new List<YaGptMessage>());

Expand Down
1 change: 0 additions & 1 deletion Botticelli.AI.YaGpt/Settings/YaGptSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Botticelli.AI.YaGpt.Settings;

public class YaGptSettings : AiSettings
{
public string ApiKey { get; set; }
public string Model { get; set; }
public double Temperature { get; set; }
public string Instruction { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Botticelli.AI/AIProvider/ChatGptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private HttpClient GetClient()

client.BaseAddress = new Uri(Settings.Value.Url);
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Settings.Value.ApiKey);
new AuthenticationHeaderValue(Settings.Value.AuthMethod, Settings.Value.ApiKey);

return client;
}
Expand Down
13 changes: 11 additions & 2 deletions Botticelli.AI/Message/AIMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ public AiMessage(string uid) : base(uid)
{
}

public string Instruction { get; set; }
public string Instruction { get; set; } = string.Empty;

public List<AiMessage> AdditionalMessages { get; set; }
public List<AiMessage> AdditionalMessages { get; set; } = new List<AiMessage>();

public override Shared.ValueObjects.Message Copy()
{
var newMessage = (AiMessage)(base.Copy());
newMessage.Instruction = Instruction;
newMessage.AdditionalMessages = AdditionalMessages;

return newMessage;
}
}
5 changes: 3 additions & 2 deletions Botticelli.AI/Settings/AISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
public class AiSettings : ProviderSettings
{
public string? Url { get; set; }
public string AiName { get; set; }
public required string AiName { get; set; }
public bool StreamGeneration { get; set; }
public string ApiKey { get; set; }
public string AuthMethod { get; set; } = "Bearer";
public string? ApiKey { get; set; }
}

This file was deleted.

12 changes: 8 additions & 4 deletions Botticelli.Bus/Agent/PassAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,24 @@ public Task SendResponseAsync(SendMessageResponse response,

public Task StartAsync(CancellationToken token)
{
return Task.Run(async () => await InnerProcess(_handler, token));
Task.Run(() => InnerProcess(_handler, token), token);

return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

private async Task InnerProcess(THandler handler, CancellationToken token)
private void InnerProcess(THandler handler, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
if (NoneBus.SendMessageRequests.TryDequeue(out var request)) await handler.Handle(request, token);
Thread.Sleep(5);
if (NoneBus.SendMessageRequests.TryDequeue(out var request))
handler.Handle(request, token).Wait(token);

Task.Delay(5, token).Wait(token);
}
}
}
4 changes: 2 additions & 2 deletions Botticelli.Controls.Layouts/Inlines/InlineButtonMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class InlineButtonMenu : ILayout

public InlineButtonMenu(int rows, int columns)
{
if (rows < 1) throw new InvalidDataException("rows count should be > 1!");
if (columns < 1) throw new InvalidDataException("columns count should be > 1!");
if (rows < 1) throw new InvalidDataException("rows count should be > 0!");
if (columns < 1) throw new InvalidDataException("columns count should be > 0!");

_rows = rows;
_columns = columns;
Expand Down
9 changes: 9 additions & 0 deletions Botticelli.Controls/BasicControls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

public class Button : IControl
{
public Button()
{
}

public Button(string? content)
{
Content = content;
}

public string? Image { get; set; }
public string? Content { get; set; }

Expand Down
9 changes: 9 additions & 0 deletions Botticelli.Controls/Layouts/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ namespace Botticelli.Controls.Layouts;

public class Item
{
public Item()
{
}

public Item(IControl? control)
{
Control = control;
}

public IControl? Control { get; set; }

public ItemParams? Params { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion Botticelli.Controls/Parsers/JsonLayoutParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ private static void ResolveControlType(JsonElement itemElement, Item item)
{
if (itemElement.TryGetProperty("Button", out var buttonElement))
{
var hasCallback = buttonElement.TryGetProperty("Callback", out var callbackElement);
var button = new Button
{
Content = buttonElement.GetProperty("Content").GetString()
Content = buttonElement.GetProperty("Content")
.GetString(),
CallbackData = hasCallback ? callbackElement.GetString() : null
};

item.Control = button;
Expand Down
19 changes: 17 additions & 2 deletions Botticelli.Framework.Telegram/Builders/TelegramBotBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Configuration;
using Botticelli.Bot.Data;
using Botticelli.Bot.Data.Repositories;
using Botticelli.Bot.Data.Settings;
Expand All @@ -18,6 +19,7 @@
using Botticelli.Framework.Telegram.Layout;
using Botticelli.Framework.Telegram.Options;
using Botticelli.Framework.Telegram.Utils;
using Botticelli.Interfaces;
using Botticelli.Shared.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -171,8 +173,8 @@ public TelegramBotBuilder<TBot, TBotBuilder> Prepare()
#region Data

Services.AddDbContext<BotInfoContext>(o =>
o.UseSqlite($"Data source={BotDataAccessSettingsBuilder!.Build().ConnectionString}"));
Services.AddScoped<IBotDataAccess, BotDataAccess>();
o.UseSqlite($"Data source={BotDataAccessSettingsBuilder!.Build().ConnectionString}"), ServiceLifetime.Singleton);
Services.AddSingleton<IBotDataAccess, BotDataAccess>();

#endregion

Expand All @@ -197,6 +199,19 @@ public TelegramBotBuilder<TBot, TBotBuilder> Prepare()
.AddBotticelliFramework()
.AddSingleton<IBotUpdateHandler, BotUpdateHandler>()
.AddSingleton(client);

if (_isStandalone)
{
Services.AddHttpClient<BotStandaloneService>()
.AddServerCertificates(BotSettings);

if (BotData == null) throw new ConfigurationErrorsException("BotData is null!");

Services.AddHostedService<BotStandaloneService>()
.AddSingleton(BotData);

Services.AddSingleton<IBot>(sp => this .Build(sp)!);
}

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,13 @@ public TelegramStandaloneBotBuilder<TBot> AddBotData(BotDataSettingsBuilder<BotD

AddToken(BotData.BotKey);

Services.AddSingleton(BotData);

return this;
}

protected override TBot? InnerBuild(IServiceProvider serviceProvider)
{
Services.AddHttpClient<BotStandaloneService>()
.AddServerCertificates(BotSettings);

if (BotData == null) throw new ConfigurationErrorsException("BotData is null!");

Services.AddHostedService<BotStandaloneService>()
.AddSingleton(BotData);

return base.InnerBuild(serviceProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public static IServiceCollection AddTelegramLayoutsSupport(this IServiceCollecti
services.AddSingleton<ILayoutParser, JsonLayoutParser>()
.AddSingleton<ILayoutSupplier<ReplyKeyboardMarkup>, ReplyTelegramLayoutSupplier>()
.AddSingleton<ILayoutSupplier<InlineKeyboardMarkup>, InlineTelegramLayoutSupplier>()
.AddSingleton<IReplyTelegramLayoutSupplier, ReplyTelegramLayoutSupplier>()
.AddSingleton<IInlineTelegramLayoutSupplier, InlineTelegramLayoutSupplier>()
.AddSingleton<ILayoutLoader<ReplyKeyboardMarkup>, LayoutLoader<ILayoutParser,
ILayoutSupplier<ReplyKeyboardMarkup>, ReplyKeyboardMarkup>>()
.AddSingleton<ILayoutLoader<InlineKeyboardMarkup>, LayoutLoader<ILayoutParser,
Expand Down
2 changes: 2 additions & 0 deletions Botticelli.Framework.Telegram/TelegramBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ protected override async Task<SendMessageResponse> InnerSendMessageAsync<TSendOp
{
if (optionsBuilder.Build() is ReplyMarkup markup)
replyMarkup = markup;
else if (optionsBuilder.Build() is InlineKeyboardMarkup keyboardMarkup)
replyMarkup = keyboardMarkup;
else
replyMarkup = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ public CommandChainFirstElementProcessor(ILogger<CommandChainProcessor<TInputCom

public override async Task ProcessAsync(Message message, CancellationToken token)
{
Logger.LogDebug(Next == null ?
$"{nameof(CommandChainProcessor<TInputCommand>)} : no next step, returning" :
$"{nameof(CommandChainProcessor<TInputCommand>)} : next step is '{Next?.GetType().Name}'");
Logger.LogDebug(Next == null
? $"{nameof(CommandChainProcessor<TInputCommand>)} : no next step, returning"
: $"{nameof(CommandChainProcessor<TInputCommand>)} : next step is '{Next?.GetType().Name}'");

if (Next != null) await Next.ProcessAsync(message, token)!;
if (_bot != null)
{
if (Next != null)
{
Next.SetBot(_bot);
await Next.ProcessAsync(message, token)!;
}
}
}

protected override Task InnerProcess(Message message, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ public CommandChainProcessorBuilder(IServiceCollection services)

_typesChain.Add(typeof(CommandChainFirstElementProcessor<TInputCommand>));
_services.AddSingleton<CommandChainFirstElementProcessor<TInputCommand>>();

ProcessorFactoryBuilder.AddProcessor<CommandChainFirstElementProcessor<TInputCommand>>(_services);
}

public CommandChainProcessorBuilder<TInputCommand> AddNext<TNextProcessor>()
public CommandChainProcessorBuilder<TInputCommand> AddNext<TNextProcessor>(ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
where TNextProcessor : class, ICommandChainProcessor<TInputCommand>
{
_typesChain.Add(typeof(TNextProcessor));
_services.AddSingleton<TNextProcessor>();
switch (serviceLifetime)
{
case ServiceLifetime.Singleton:
_services.AddSingleton<TNextProcessor>();
break;
case ServiceLifetime.Scoped:
_services.AddScoped<TNextProcessor>();
break;
case ServiceLifetime.Transient:
_services.AddTransient<TNextProcessor>();
break;
default:
throw new ArgumentOutOfRangeException(nameof(serviceLifetime), serviceLifetime, null);
}

ProcessorFactoryBuilder.AddProcessor<TNextProcessor>(_services);

return this;
Expand All @@ -31,6 +46,7 @@ public CommandChainProcessorBuilder<TInputCommand> AddNext<TNextProcessor>()
public ICommandChainProcessor<TInputCommand>? Build(IServiceProvider sp)
{
if (_typesChain.Count == 0) return null;
var scope = sp.CreateScope();

// initializing chain processors...

Expand All @@ -41,7 +57,7 @@ public CommandChainProcessorBuilder<TInputCommand> AddNext<TNextProcessor>()

foreach (var type in _typesChain.Skip(1))
{
var proc = sp.GetRequiredService(type) as ICommandChainProcessor<TInputCommand>;
var proc = scope.ServiceProvider.GetRequiredService(type) as ICommandChainProcessor<TInputCommand>;

if (prev != null) prev.Next = proc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class CommandProcessor<TCommand> : ICommandProcessor
private readonly IValidator<Message> _messageValidator;
private readonly MetricsProcessor? _metricsProcessor;
protected readonly ILogger Logger;
private IBot? _bot;
protected IBot? _bot;

protected CommandProcessor(ILogger logger,
ICommandValidator<TCommand> commandValidator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace Botticelli.Framework.Commands.Processors;
public abstract class FluentCommandProcessor<TCommand>(
ILogger logger,
MetricsProcessor metricsProcessor,
ICommandValidator<TCommand> commandValidator,
IBot bot)
ICommandValidator<TCommand> commandValidator)
: ICommandProcessor
where TCommand : class, IFluentCommand
{
protected IBot Bot = bot;
protected IBot? Bot;

public string CommandText { get; init; }

public async Task ProcessAsync(Message message, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public override async Task ProcessAsync(Message message, CancellationToken token
if (Next != null)
{
Next.ChainIds.Add(message.ChainId.Value);
await Next.ProcessAsync(message, token);
if (_bot != null)
{
Next.SetBot(_bot);
await Next.ProcessAsync(message, token);
}
}
else
{
Expand Down
Loading
Loading