🐛 Fixes the AI agent get posts ability

This commit is contained in:
2025-10-26 11:51:51 +08:00
parent 29c5971554
commit cf6e534d02
2 changed files with 78 additions and 37 deletions

View File

@@ -47,12 +47,11 @@ public class ThoughtProvider
builder.AddOllamaChatCompletion(ModelDefault!, new Uri(endpoint ?? "http://localhost:11434/api")); builder.AddOllamaChatCompletion(ModelDefault!, new Uri(endpoint ?? "http://localhost:11434/api"));
break; break;
case "deepseek": case "deepseek":
builder.AddOpenAIChatCompletion(ModelDefault!, var client = new OpenAIClient(
new OpenAIClient( new ApiKeyCredential(apiKey!),
new ApiKeyCredential(apiKey!), new OpenAIClientOptions { Endpoint = new Uri(endpoint ?? "https://api.deepseek.com/v1") }
new OpenAIClientOptions { Endpoint = new Uri(endpoint ?? "https://api.deepseek.com/v1") }
)
); );
builder.AddOpenAIChatCompletion(ModelDefault!, client);
break; break;
default: default:
throw new IndexOutOfRangeException("Unknown thinking provider: " + ModelProviderType); throw new IndexOutOfRangeException("Unknown thinking provider: " + ModelProviderType);
@@ -70,7 +69,7 @@ public class ThoughtProvider
var request = new GetAccountRequest { Id = userId }; var request = new GetAccountRequest { Id = userId };
var response = await _accountClient.GetAccountAsync(request); var response = await _accountClient.GetAccountAsync(request);
return JsonSerializer.Serialize(response, GrpcTypeHelper.SerializerOptions); return JsonSerializer.Serialize(response, GrpcTypeHelper.SerializerOptions);
}, "get_user_profile", "Get a user profile from the Solar Network."), }, "get_user", "Get a user profile from the Solar Network."),
KernelFunctionFactory.CreateFromMethod(async (string postId) => KernelFunctionFactory.CreateFromMethod(async (string postId) =>
{ {
var request = new GetPostRequest { Id = postId }; var request = new GetPostRequest { Id = postId };
@@ -83,36 +82,86 @@ public class ThoughtProvider
var response = await _postClient.SearchPostsAsync(request); var response = await _postClient.SearchPostsAsync(request);
return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions); return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions);
}, "search_posts", "Search posts by query from the Solar Network."), }, "search_posts", "Search posts by query from the Solar Network."),
KernelFunctionFactory.CreateFromMethod(async () => KernelFunctionFactory.CreateFromMethod(async (
string? publisherId = null,
string? realmId = null,
int pageSize = 10,
string? pageToken = null,
string? orderBy = null,
List<string>? categories = null,
List<string>? tags = null,
string? query = null,
List<int>? types = null,
string? afterIso = null,
string? beforeIso = null,
bool includeReplies = false,
int? pinned = null,
bool onlyMedia = false,
bool shuffle = false
) =>
{ {
var request = new ListPostsRequest { PageSize = 10 }; var request = new ListPostsRequest
{
PublisherId = publisherId,
RealmId = realmId,
PageSize = pageSize,
PageToken = pageToken,
OrderBy = orderBy,
Query = query,
IncludeReplies = includeReplies,
Pinned = pinned.HasValue ? (PostPinMode)pinned : default,
OnlyMedia = onlyMedia,
Shuffle = shuffle
};
if (!string.IsNullOrEmpty(afterIso))
{
request.After =
Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(DateTimeOffset.Parse(afterIso)
.ToUniversalTime());
}
if (!string.IsNullOrEmpty(beforeIso))
{
request.Before =
Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(DateTimeOffset.Parse(beforeIso)
.ToUniversalTime());
}
if (categories != null) request.Categories.AddRange(categories);
if (tags != null) request.Tags.AddRange(tags);
if (types != null) request.Types_.AddRange(types.Select(t => (PostType)t));
var response = await _postClient.ListPostsAsync(request); var response = await _postClient.ListPostsAsync(request);
return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions); return JsonSerializer.Serialize(response.Posts, GrpcTypeHelper.SerializerOptions);
}, "get_recent_posts", "Get recent posts from the Solar Network.") }, "list_posts", "Get posts from the Solar Network with customizable filters.")
]); ]);
} }
public PromptExecutionSettings CreatePromptExecutionSettings() public PromptExecutionSettings CreatePromptExecutionSettings()
{ {
return ModelProviderType switch switch (ModelProviderType)
{ {
"ollama" => new OllamaPromptExecutionSettings case "ollama":
{ return new OllamaPromptExecutionSettings
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto( {
options: new FunctionChoiceBehaviorOptions FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(
{ options: new FunctionChoiceBehaviorOptions
AllowParallelCalls = true, AllowConcurrentInvocation = true {
}) AllowParallelCalls = true,
}, AllowConcurrentInvocation = true
"deepseek" => new OpenAIPromptExecutionSettings })
{ };
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto( case "deepseek":
options: new FunctionChoiceBehaviorOptions return new OpenAIPromptExecutionSettings
{ {
AllowParallelCalls = true, AllowConcurrentInvocation = true FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(
}) options: new FunctionChoiceBehaviorOptions
}, {
_ => throw new InvalidOperationException("Unknown provider: " + ModelProviderType) AllowParallelCalls = true,
}; AllowConcurrentInvocation = true
})
};
default:
throw new InvalidOperationException("Unknown provider: " + ModelProviderType);
}
} }
} }

View File

@@ -69,7 +69,6 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
.Include(p => p.ForwardedPost) .Include(p => p.ForwardedPost)
.Include(p => p.Awards) .Include(p => p.Awards)
.Include(p => p.FeaturedRecords) .Include(p => p.FeaturedRecords)
.Where(p => p.DeletedAt == null) // Only active posts
.AsQueryable(); .AsQueryable();
if (!string.IsNullOrWhiteSpace(request.Query)) if (!string.IsNullOrWhiteSpace(request.Query))
@@ -82,14 +81,10 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
} }
if (!string.IsNullOrWhiteSpace(request.PublisherId) && Guid.TryParse(request.PublisherId, out var pid)) if (!string.IsNullOrWhiteSpace(request.PublisherId) && Guid.TryParse(request.PublisherId, out var pid))
{
query = query.Where(p => p.PublisherId == pid); query = query.Where(p => p.PublisherId == pid);
}
if (!string.IsNullOrWhiteSpace(request.RealmId) && Guid.TryParse(request.RealmId, out var rid)) if (!string.IsNullOrWhiteSpace(request.RealmId) && Guid.TryParse(request.RealmId, out var rid))
{
query = query.Where(p => p.RealmId == rid); query = query.Where(p => p.RealmId == rid);
}
query = query.FilterWithVisibility(null, [], []); query = query.FilterWithVisibility(null, [], []);
@@ -128,7 +123,6 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
.Include(p => p.ForwardedPost) .Include(p => p.ForwardedPost)
.Include(p => p.Awards) .Include(p => p.Awards)
.Include(p => p.FeaturedRecords) .Include(p => p.FeaturedRecords)
.Where(p => p.DeletedAt == null)
.AsQueryable(); .AsQueryable();
query = request.Shuffle query = request.Shuffle
@@ -154,9 +148,7 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
} }
if (request.OnlyMedia) if (request.OnlyMedia)
{
query = query.Where(e => e.Attachments.Count > 0); query = query.Where(e => e.Attachments.Count > 0);
}
query = request.Pinned switch query = request.Pinned switch
{ {
@@ -166,7 +158,7 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
Shared.Proto.PostPinMode.PublisherPage when !string.IsNullOrWhiteSpace(request.PublisherId) => Shared.Proto.PostPinMode.PublisherPage when !string.IsNullOrWhiteSpace(request.PublisherId) =>
query.Where(p => p.PinMode == Shared.Models.PostPinMode.PublisherPage), query.Where(p => p.PinMode == Shared.Models.PostPinMode.PublisherPage),
Shared.Proto.PostPinMode.ReplyPage => query.Where(p => p.PinMode == Shared.Models.PostPinMode.ReplyPage), Shared.Proto.PostPinMode.ReplyPage => query.Where(p => p.PinMode == Shared.Models.PostPinMode.ReplyPage),
_ => query.Where(p => p.PinMode == (Shared.Models.PostPinMode)request.Pinned) _ => query
}; };
// Include/exclude replies // Include/exclude replies