Skip to content

Commit 134c772

Browse files
authored
all plugins expect Guid for Id. author now returns structured response (#118)
1 parent 263d7bc commit 134c772

File tree

8 files changed

+52
-14
lines changed

8 files changed

+52
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Goodtocode.SemanticKernel.Core.Application.Abstractions;
2+
3+
public interface IAuthorResponse
4+
{
5+
Guid AuthorId { get; }
6+
string? Name { get; }
7+
string Status { get; }
8+
string? Message { get; }
9+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Plugins;
1+
namespace Goodtocode.SemanticKernel.Core.Application.Abstractions;
22

33
public interface IAuthorsPlugin
44
{
5-
Task<string> GetAuthorInfoAsync(string authorId, CancellationToken cancellationToken);
5+
Task<IAuthorResponse> GetAuthorNameAsync(Guid authorId, CancellationToken cancellationToken);
66
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
namespace Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Plugins;
1+
namespace Goodtocode.SemanticKernel.Core.Application.Abstractions;
22

33
public interface IChatMessagesPlugin
44
{
55
Task<IEnumerable<string>> ListRecentMessagesAsync(DateTime? startDate, DateTime? endDate, CancellationToken cancellationToken);
6-
Task<IEnumerable<string>> GetChatMessagesAsync(string sessionId, CancellationToken cancellationToken);
6+
Task<IEnumerable<string>> GetChatMessagesAsync(Guid sessionId, CancellationToken cancellationToken);
77
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
namespace Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Plugins;
1+
namespace Goodtocode.SemanticKernel.Core.Application.Abstractions;
22

33
public interface IChatSessionsPlugin
44
{
55
Task<IEnumerable<string>> ListRecentSessionsAsync(DateTime? startDate, DateTime? endDate, CancellationToken cancellationToken);
6-
Task<string> UpdateChatSessionTitleAsync(string sessionId, string newTitle, CancellationToken cancellationToken);
6+
Task<string> UpdateChatSessionTitleAsync(Guid sessionId, string newTitle, CancellationToken cancellationToken);
77
}

src/Infrastructure.SemanticKernel/ConfigureServices.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Options;
1+
using Goodtocode.SemanticKernel.Core.Application.Abstractions;
2+
using Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Options;
23
using Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Plugins;
34
using Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Services;
45
using Microsoft.Extensions.Configuration;

src/Infrastructure.SemanticKernel/Plugins/AuthorsPlugin.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,45 @@
55

66
namespace Goodtocode.SemanticKernel.Infrastructure.SemanticKernel.Plugins;
77

8+
public class AuthorResponse : IAuthorResponse
9+
{
10+
public Guid AuthorId { get; set; }
11+
public string? Name { get; set; }
12+
public string? Status { get; set; }
13+
public string? Message { get; set; }
14+
}
15+
816
public sealed class AuthorsPlugin(IServiceProvider serviceProvider) : IAuthorsPlugin
917
{
1018
private readonly IServiceProvider _serviceProvider = serviceProvider;
1119

1220
[KernelFunction("get_author")]
13-
[Description("Returns the author's name for the specified author ID, or 'Author not found' if no match exists.")]
14-
public async Task<string> GetAuthorInfoAsync(string authorId, CancellationToken cancellationToken)
21+
[Description("Returns structured author info including name, status, and explanation.")]
22+
async Task<IAuthorResponse> IAuthorsPlugin.GetAuthorNameAsync(Guid authorId, CancellationToken cancellationToken)
1523
{
1624
using var scope = _serviceProvider.CreateScope();
1725
var context = scope.ServiceProvider.GetRequiredService<ISemanticKernelContext>();
1826
var author = await context.Authors.FindAsync([authorId, cancellationToken], cancellationToken: cancellationToken);
19-
return author?.Name ?? "Author not found";
27+
28+
if (author == null)
29+
{
30+
return new AuthorResponse
31+
{
32+
AuthorId = authorId,
33+
Name = null,
34+
Status = "NotFound",
35+
Message = "No author found with the specified ID."
36+
};
37+
}
38+
39+
return new AuthorResponse
40+
{
41+
AuthorId = authorId,
42+
Name = author.Name,
43+
Status = string.IsNullOrWhiteSpace(author.Name) ? "Partial" : "Found",
44+
Message = string.IsNullOrWhiteSpace(author.Name)
45+
? "Author exists but name is not yet linked to Entra External ID."
46+
: "Author found."
47+
};
2048
}
2149
}

src/Infrastructure.SemanticKernel/Plugins/ChatMessagesPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public async Task<IEnumerable<string>> ListRecentMessagesAsync(DateTime? startDa
3535

3636
[KernelFunction("get_messages")]
3737
[Description("Retrieves all messages from a specific chat session.")]
38-
public async Task<IEnumerable<string>> GetChatMessagesAsync(string sessionId,
38+
public async Task<IEnumerable<string>> GetChatMessagesAsync(Guid sessionId,
3939
CancellationToken cancellationToken = default)
4040
{
4141
// Get ISemanticKernelContext directly instead of constructor DI to allow this plugin to be registered via AddSingleton() and not scoped due to EF.
4242
using var scope = _serviceProvider.CreateScope();
4343
var context = scope.ServiceProvider.GetRequiredService<ISemanticKernelContext>();
4444

4545
var messages = await context.ChatMessages
46-
.Where(x => x.ChatSessionId.ToString() == sessionId)
46+
.Where(x => x.ChatSessionId == sessionId)
4747
.ToListAsync(cancellationToken);
4848

4949
return messages.Select(m => $"{m.ChatSessionId}: {m.Timestamp} - {m.Role}: {m.Content}");

src/Infrastructure.SemanticKernel/Plugins/ChatSessionsPlugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public async Task<IEnumerable<string>> ListRecentSessionsAsync(
3737

3838
[KernelFunction("change_title")]
3939
[Description("Changes the title on this chat session.")]
40-
public async Task<string> UpdateChatSessionTitleAsync(string sessionId, string newTitle,
40+
public async Task<string> UpdateChatSessionTitleAsync(Guid sessionId, string newTitle,
4141
CancellationToken cancellationToken = default)
4242
{
4343
// Get ISemanticKernelContext directly instead of constructor DI to allow this plugin to be registered via AddSingleton() and not scoped due to EF.
4444
using var scope = _serviceProvider.CreateScope();
4545
var context = scope.ServiceProvider.GetRequiredService<ISemanticKernelContext>();
4646

4747
var chatSession = await context.ChatSessions
48-
.FirstOrDefaultAsync(x => x.Id.ToString() == sessionId, cancellationToken: cancellationToken);
48+
.FirstOrDefaultAsync(x => x.Id == sessionId, cancellationToken: cancellationToken);
4949
chatSession!.Title = newTitle;
5050
context.ChatSessions.Update(chatSession);
5151
await context.SaveChangesAsync(cancellationToken);

0 commit comments

Comments
 (0)