Skip to content

Commit 6c5fcb4

Browse files
committed
gmini and other stuff
1 parent 2e43a1c commit 6c5fcb4

26 files changed

Lines changed: 614 additions & 11 deletions

Directory.Packages.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<PackageVersion Include="ManagedCode.ClaudeCodeSharpSDK.Extensions.AI" Version="1.1.0" />
1616
<PackageVersion Include="ManagedCode.CodexSharpSDK.Extensions.AgentFramework" Version="1.1.0-rc4" />
1717
<PackageVersion Include="ManagedCode.CodexSharpSDK.Extensions.AI" Version="1.1.0" />
18+
<PackageVersion Include="ManagedCode.GeminiSharpSDK.Extensions.AgentFramework" Version="0.0.1-rc4" />
19+
<PackageVersion Include="ManagedCode.GeminiSharpSDK.Extensions.AI" Version="0.0.1" />
1820
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
1921
<PackageVersion Include="Microsoft.Agents.AI" Version="1.0.0-rc4" />
2022
<PackageVersion Include="Microsoft.Agents.AI.GitHub.Copilot" Version="1.0.0-preview.260311.1" />
@@ -35,4 +37,4 @@
3537
<PackageVersion Include="Uno.UITest.Helpers" Version="1.1.0-dev.70" />
3638
<PackageVersion Include="Xamarin.UITest" Version="4.4.2" />
3739
</ItemGroup>
38-
</Project>
40+
</Project>

DotPilot.Core/ChatSessions/Diagnostics/AgentSessionRuntimeLog.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ public static partial void LegacyAgentProfileNormalized(
131131
Message = "Workspace snapshot load failed.")]
132132
public static partial void WorkspaceLoadFailed(ILogger logger, Exception exception);
133133

134+
[LoggerMessage(
135+
EventId = 1227,
136+
Level = LogLevel.Information,
137+
Message = "Resetting local workspace data.")]
138+
public static partial void WorkspaceResetStarted(ILogger logger);
139+
140+
[LoggerMessage(
141+
EventId = 1228,
142+
Level = LogLevel.Information,
143+
Message = "Reset local workspace data and restored first-run defaults.")]
144+
public static partial void WorkspaceResetCompleted(ILogger logger);
145+
146+
[LoggerMessage(
147+
EventId = 1229,
148+
Level = LogLevel.Warning,
149+
Message = "Workspace reset blocked because active sessions are still running. ActiveSessionCount={ActiveSessionCount}.")]
150+
public static partial void WorkspaceResetBlocked(ILogger logger, int activeSessionCount);
151+
152+
[LoggerMessage(
153+
EventId = 1230,
154+
Level = LogLevel.Error,
155+
Message = "Workspace reset failed.")]
156+
public static partial void WorkspaceResetFailed(ILogger logger, Exception exception);
157+
134158
[LoggerMessage(
135159
EventId = 1203,
136160
Level = LogLevel.Information,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using ManagedCode.Communication;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace DotPilot.Core.ChatSessions;
5+
6+
internal sealed partial class AgentSessionService
7+
{
8+
private const string WorkspaceResetBlockedCode = "WorkspaceResetBlocked";
9+
private const string WorkspaceResetBlockedMessage = "Finish active session output before deleting local data.";
10+
11+
public async ValueTask<Result<AgentWorkspaceSnapshot>> ResetWorkspaceAsync(CancellationToken cancellationToken)
12+
{
13+
if (sessionActivityMonitor.Current.HasActiveSessions)
14+
{
15+
AgentSessionServiceLog.WorkspaceResetBlocked(
16+
logger,
17+
sessionActivityMonitor.Current.ActiveSessionCount);
18+
return Result<AgentWorkspaceSnapshot>.Fail(WorkspaceResetBlockedCode, WorkspaceResetBlockedMessage);
19+
}
20+
21+
try
22+
{
23+
await _initializationGate.WaitAsync(cancellationToken);
24+
try
25+
{
26+
AgentSessionServiceLog.WorkspaceResetStarted(logger);
27+
await ResetWorkspaceCoreAsync(cancellationToken);
28+
AgentSessionServiceLog.WorkspaceResetCompleted(logger);
29+
}
30+
finally
31+
{
32+
_initializationGate.Release();
33+
}
34+
35+
return await LoadWorkspaceAsync(forceRefreshProviders: true, cancellationToken);
36+
}
37+
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
38+
{
39+
throw;
40+
}
41+
catch (Exception exception)
42+
{
43+
AgentSessionServiceLog.WorkspaceResetFailed(logger, exception);
44+
return Result<AgentWorkspaceSnapshot>.Fail(exception);
45+
}
46+
}
47+
48+
private async Task ResetWorkspaceCoreAsync(CancellationToken cancellationToken)
49+
{
50+
await using var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
51+
await dbContext.Database.EnsureCreatedAsync(cancellationToken);
52+
await AgentProfileSchemaCompatibilityEnsurer.EnsureAsync(dbContext, cancellationToken);
53+
await ClearWorkspaceTablesAsync(dbContext, cancellationToken);
54+
await ClearRuntimeArtifactsAsync(cancellationToken);
55+
InvalidateProviderStatusSnapshot();
56+
_initialized = false;
57+
await EnsureDefaultProviderAndAgentAsync(dbContext, cancellationToken);
58+
_initialized = true;
59+
}
60+
61+
private static async Task ClearWorkspaceTablesAsync(
62+
LocalAgentSessionDbContext dbContext,
63+
CancellationToken cancellationToken)
64+
{
65+
dbContext.SessionEntries.RemoveRange(await dbContext.SessionEntries.ToListAsync(cancellationToken));
66+
dbContext.Sessions.RemoveRange(await dbContext.Sessions.ToListAsync(cancellationToken));
67+
dbContext.AgentProfiles.RemoveRange(await dbContext.AgentProfiles.ToListAsync(cancellationToken));
68+
dbContext.ProviderPreferences.RemoveRange(await dbContext.ProviderPreferences.ToListAsync(cancellationToken));
69+
await dbContext.SaveChangesAsync(cancellationToken);
70+
dbContext.ChangeTracker.Clear();
71+
}
72+
73+
private async Task ClearRuntimeArtifactsAsync(CancellationToken cancellationToken)
74+
{
75+
await sessionStateStore.ClearAsync(cancellationToken);
76+
await chatHistoryStore.ClearAsync(cancellationToken);
77+
if (storageOptions.UseInMemoryDatabase || OperatingSystem.IsBrowser())
78+
{
79+
return;
80+
}
81+
82+
await LocalStorageDeletion.DeleteDirectoryIfExistsAsync(
83+
AgentSessionStoragePaths.ResolvePlaygroundRootDirectory(storageOptions),
84+
cancellationToken);
85+
}
86+
}

DotPilot.Core/ChatSessions/Execution/AgentSessionService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
namespace DotPilot.Core.ChatSessions;
88

9-
internal sealed class AgentSessionService(
9+
internal sealed partial class AgentSessionService(
1010
IDbContextFactory<LocalAgentSessionDbContext> dbContextFactory,
1111
AgentExecutionLoggingMiddleware executionLoggingMiddleware,
1212
ISessionActivityMonitor sessionActivityMonitor,
1313
IAgentProviderStatusReader providerStatusReader,
1414
AgentRuntimeConversationFactory runtimeConversationFactory,
15+
LocalAgentSessionStateStore sessionStateStore,
16+
LocalAgentChatHistoryStore chatHistoryStore,
17+
AgentSessionStorageOptions storageOptions,
1518
TimeProvider timeProvider,
1619
ILogger<AgentSessionService> logger)
1720
: IAgentSessionService, IDisposable

DotPilot.Core/ChatSessions/Interfaces/IAgentSessionService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public interface IAgentSessionService
88

99
ValueTask<Result<AgentWorkspaceSnapshot>> RefreshWorkspaceAsync(CancellationToken cancellationToken);
1010

11+
ValueTask<Result<AgentWorkspaceSnapshot>> ResetWorkspaceAsync(CancellationToken cancellationToken);
12+
1113
ValueTask<Result<SessionTranscriptSnapshot>> GetSessionAsync(SessionId sessionId, CancellationToken cancellationToken);
1214

1315
ValueTask<Result<AgentProfileSummary>> CreateAgentAsync(

DotPilot.Core/ChatSessions/Persistence/Services/LocalAgentChatHistoryStore.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ public async ValueTask AppendAsync(
5252
await WriteAsync(path, combined, cancellationToken);
5353
}
5454

55+
public async ValueTask ClearAsync(CancellationToken cancellationToken)
56+
{
57+
_memoryHistory.Clear();
58+
if (UseTransientStore())
59+
{
60+
return;
61+
}
62+
63+
await LocalStorageDeletion.DeleteDirectoryIfExistsAsync(
64+
AgentSessionStoragePaths.ResolveChatHistoryDirectory(storageOptions),
65+
cancellationToken);
66+
}
67+
5568
private string GetPath(string storageKey)
5669
{
5770
ArgumentException.ThrowIfNullOrWhiteSpace(storageKey);

DotPilot.Core/ChatSessions/Persistence/Services/LocalAgentSessionStateStore.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ public async ValueTask SaveAsync(
6969
await WriteTextAsync(path, payload, cancellationToken);
7070
}
7171

72+
public async ValueTask ClearAsync(CancellationToken cancellationToken)
73+
{
74+
_memorySessions.Clear();
75+
if (UseTransientStore())
76+
{
77+
return;
78+
}
79+
80+
await LocalStorageDeletion.DeleteDirectoryIfExistsAsync(
81+
AgentSessionStoragePaths.ResolveRuntimeSessionDirectory(storageOptions),
82+
cancellationToken);
83+
}
84+
7285
private string GetPath(SessionId sessionId)
7386
{
7487
var directory = AgentSessionStoragePaths.ResolveRuntimeSessionDirectory(storageOptions);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace DotPilot.Core.ChatSessions;
2+
3+
internal static class LocalStorageDeletion
4+
{
5+
private const int DeleteRetryCount = 5;
6+
private static readonly TimeSpan DeleteRetryDelay = TimeSpan.FromMilliseconds(100);
7+
8+
public static async Task DeleteDirectoryIfExistsAsync(string path, CancellationToken cancellationToken)
9+
{
10+
ArgumentException.ThrowIfNullOrWhiteSpace(path);
11+
12+
if (!Directory.Exists(path))
13+
{
14+
return;
15+
}
16+
17+
for (var attempt = 0; attempt < DeleteRetryCount; attempt++)
18+
{
19+
cancellationToken.ThrowIfCancellationRequested();
20+
21+
try
22+
{
23+
Directory.Delete(path, recursive: true);
24+
return;
25+
}
26+
catch (IOException) when (attempt < DeleteRetryCount - 1)
27+
{
28+
await Task.Delay(DeleteRetryDelay, cancellationToken);
29+
}
30+
catch (UnauthorizedAccessException) when (attempt < DeleteRetryCount - 1)
31+
{
32+
await Task.Delay(DeleteRetryDelay, cancellationToken);
33+
}
34+
}
35+
}
36+
}

DotPilot.Core/DotPilot.Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<PackageReference Include="ManagedCode.CodexSharpSDK.Extensions.AI" />
1616
<PackageReference Include="ManagedCode.Communication" />
1717
<PackageReference Include="GitHub.Copilot.SDK" />
18+
<PackageReference Include="ManagedCode.GeminiSharpSDK.Extensions.AgentFramework" />
19+
<PackageReference Include="ManagedCode.GeminiSharpSDK.Extensions.AI" />
1820
<PackageReference Include="Microsoft.Agents.AI" />
1921
<PackageReference Include="Microsoft.Agents.AI.GitHub.Copilot" />
2022
<PackageReference Include="Microsoft.Agents.AI.Workflows" />

DotPilot.Core/Workspace/Interfaces/IAgentWorkspaceState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public interface IAgentWorkspaceState
88

99
ValueTask<Result<AgentWorkspaceSnapshot>> RefreshWorkspaceAsync(CancellationToken cancellationToken);
1010

11+
ValueTask<Result<AgentWorkspaceSnapshot>> ResetWorkspaceAsync(CancellationToken cancellationToken);
12+
1113
ValueTask<Result<SessionTranscriptSnapshot>> GetSessionAsync(SessionId sessionId, CancellationToken cancellationToken);
1214

1315
ValueTask<Result<AgentProfileSummary>> CreateAgentAsync(

0 commit comments

Comments
 (0)