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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<PackageVersion Include="Cronos" Version="0.13.0" />
<PackageVersion Include="Netclaw.SkillClient" Version="0.4.1" />
<PackageVersion Include="ShellSyntaxTree" Version="0.2.0" />
<PackageVersion Include="Termina" Version="0.15.1" />
<PackageVersion Include="Termina" Version="0.16.1" />
</ItemGroup>
<!-- Serialization -->
<ItemGroup>
Expand Down
47 changes: 47 additions & 0 deletions src/Netclaw.Cli.Tests/Mcp/McpToolPermissionsViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,53 @@ public void ToggleServerAccess_GrantsAllDiscoveredTools()
Assert.True(vm.IsToolGranted(new ToolName(tool)));
}

[Fact]
public void ToggleServerAccess_FirstEnableReplacesAStoredPartialGrantSet()
{
File.WriteAllText(_paths.NetclawConfigPath,
"""
{
"configVersion": 1,
"Tools": {
"AudienceProfiles": {
"Team": {
"McpServersMode": "Allowlist",
"AllowedMcpServers": [],
"McpServerToolGrants": {
"notion": ["search"]
}
}
}
}
}
""");

var vm = CreateVm();
var tools = new[] { "create-pages", "search", "list-databases" };
vm.InitializeForTests(new McpServerName("notion"), tools);
vm.SetSelectedAudienceForTests(TrustAudience.Team);

Assert.False(vm.IsServerAllowedForSelectedAudience());

vm.ToggleServerAccess();

Assert.True(vm.IsServerAllowedForSelectedAudience());
foreach (var tool in tools)
Assert.True(vm.IsToolGranted(new ToolName(tool)));

vm.ToggleServerAccess();

Assert.False(vm.IsServerAllowedForSelectedAudience());
foreach (var tool in tools)
Assert.False(vm.IsToolGranted(new ToolName(tool)));

vm.ToggleServerAccess();

Assert.True(vm.IsServerAllowedForSelectedAudience());
foreach (var tool in tools)
Assert.True(vm.IsToolGranted(new ToolName(tool)));
}

[Fact]
public void ToggleServerAccess_DisablingClearsGrantedTools()
{
Expand Down
46 changes: 29 additions & 17 deletions src/Netclaw.Cli/Mcp/McpToolPermissionsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Netclaw.Cli.Mcp;
public sealed class McpToolPermissionsPage : ReactivePage<McpToolPermissionsViewModel>
{
private SelectionListNode<string>? _serverList;
private DynamicLayoutNode? _contentNode;
private KeyedDynamicLayoutNode<(ToolPermissionsState State, int Revision)>? _contentNode;
private DynamicLayoutNode? _footerNode;
private DynamicLayoutNode? _gridHeaderRowsNode;
private DynamicLayoutNode? _toolRowsNode;
Expand Down Expand Up @@ -68,23 +68,26 @@ private static LayoutNode BuildHeader()

private LayoutNode BuildContent()
{
_contentNode = new DynamicLayoutNode(() =>
{
_serverList = null;
_toolScrollNode = null;
_gridHeaderRowsNode = null;
_toolRowsNode = null;
_stepSubs.Clear();

return ViewModel.CurrentState.Value switch
_contentNode = new KeyedDynamicLayoutNode<(ToolPermissionsState State, int Revision)>(
GetContentKey,
_ =>
{
ToolPermissionsState.Loading => BuildLoading(),
ToolPermissionsState.ServerList => BuildServerList(),
ToolPermissionsState.ToolGrid => BuildToolGrid(),
ToolPermissionsState.Saving => BuildLoading(),
_ => Layouts.Empty()
};
});
_serverList = null;
_toolScrollNode = null;
_gridHeaderRowsNode = null;
_toolRowsNode = null;
_stepSubs.Clear();

return ViewModel.CurrentState.Value switch
{
ToolPermissionsState.Loading => BuildLoading(),
ToolPermissionsState.ServerList => BuildServerList(),
ToolPermissionsState.ToolGrid => BuildToolGrid(),
ToolPermissionsState.Saving => BuildLoading(),
_ => Layouts.Empty()
};
},
KeyedDynamicCachePolicy.EvictOnKeyChange);

// ToolGrid updates invalidate only the header and rows, so the scroll container persists and keeps its scroll position.
ViewModel.StateVersion
Expand All @@ -106,6 +109,15 @@ private LayoutNode BuildContent()
return _contentNode.Fill();
}

private (ToolPermissionsState State, int Revision) GetContentKey()
{
var state = ViewModel.CurrentState.Value;
var revision = state is ToolPermissionsState.Loading or ToolPermissionsState.Saving
? ViewModel.StateVersion.Value
: 0;
return (state, revision);
}

private ILayoutNode BuildLoading()
{
var msg = string.IsNullOrEmpty(ViewModel.StatusMessage.Value)
Expand Down
3 changes: 1 addition & 2 deletions src/Netclaw.Cli/Mcp/McpToolPermissionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ public void ToggleServerAccess()
_pendingGrants[SelectedServer] = serverGrants;
}

if (!serverGrants.ContainsKey(audienceName))
serverGrants[audienceName] = new HashSet<string>(DiscoveredTools, StringComparer.Ordinal);
serverGrants[audienceName] = new HashSet<string>(DiscoveredTools, StringComparer.Ordinal);
}
else if (_pendingGrants.TryGetValue(SelectedServer, out var existingGrants))
{
Expand Down
83 changes: 57 additions & 26 deletions src/Netclaw.Cli/Tui/ModelManagerPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class ModelManagerPage : ReactivePage<ModelManagerViewModel>

private IFocusable? _lastFocusedList;
private TextInputNode? _lastFocusedInput;
private DynamicLayoutNode? _contentNode;
private KeyedDynamicLayoutNode<ModelManagerContent>? _contentNode;
private readonly CompositeDisposable _stepSubs = [];

protected override void OnBound()
Expand Down Expand Up @@ -57,21 +57,24 @@ private ILayoutNode BuildInnerLayout()

private LayoutNode BuildContent()
{
_contentNode = new DynamicLayoutNode(() =>
{
_lastFocusedList = null;
_lastFocusedInput = null;
_stepSubs.Clear();

return ViewModel.CurrentState.Value switch
_contentNode = new KeyedDynamicLayoutNode<ModelManagerContent>(
GetContentKey,
_ =>
{
ModelManagerState.RoleOverview => BuildRoleOverview(),
ModelManagerState.SelectProvider => BuildProviderSelection(),
ModelManagerState.DiscoverModels => BuildDiscoverModels(),
ModelManagerState.ConfirmAssignment => BuildConfirmAssignment(),
_ => Layouts.Empty()
};
});
_lastFocusedList = null;
_lastFocusedInput = null;
_stepSubs.Clear();

return ViewModel.CurrentState.Value switch
{
ModelManagerState.RoleOverview => BuildRoleOverview(),
ModelManagerState.SelectProvider => BuildProviderSelection(),
ModelManagerState.DiscoverModels => BuildDiscoverModels(),
ModelManagerState.ConfirmAssignment => BuildConfirmAssignment(),
_ => Layouts.Empty()
};
},
KeyedDynamicCachePolicy.EvictOnKeyChange);

ViewModel.StateVersion
.Subscribe(_ => _contentNode.Invalidate())
Expand All @@ -80,6 +83,23 @@ private LayoutNode BuildContent()
return _contentNode;
}

private ModelManagerContent GetContentKey()
{
if (ViewModel.CurrentState.Value != ModelManagerState.DiscoverModels)
return (ModelManagerContent)ViewModel.CurrentState.Value;

if (ViewModel.IsProbing.Value)
return ModelManagerContent.DiscoverProbing;
if (ViewModel.ProbeResult.Value is { Success: false })
return ModelManagerContent.DiscoverFailed;
if (ViewModel.ManualModelEntry)
return ModelManagerContent.DiscoverManualEntry;
if (ViewModel.ProbeResult.Value is not null && ViewModel.DiscoveredModels.Count == 0)
return ModelManagerContent.DiscoverEmpty;

return ModelManagerContent.DiscoverList;
}

private LayoutNode BuildStatusBar()
{
return ViewModel.StatusMessage
Expand Down Expand Up @@ -118,14 +138,15 @@ private LayoutNode BuildKeyBindings()
private ILayoutNode BuildRoleOverview()
{
var models = ViewModel.Models;
var items = new List<string>
{
FormatRoleItem("Main", models?.Main),
FormatRoleItem("Fallback", models?.Fallback),
FormatRoleItem("Compaction", models?.Compaction)
};
var items = new[] { "Main", "Fallback", "Compaction" };

_roleList = Layouts.SelectionList(items)
_roleList = Layouts.SelectionList(items, role => FormatRoleItem(role, role switch
{
"Main" => ViewModel.Models?.Main,
"Fallback" => ViewModel.Models?.Fallback,
"Compaction" => ViewModel.Models?.Compaction,
_ => null
}))
.WithMode(SelectionMode.Single)
.WithHighlightColors(Color.Black, Color.Cyan);

Expand All @@ -137,8 +158,7 @@ private ILayoutNode BuildRoleOverview()
{
if (selected.Count > 0)
{
var role = selected[0].Split(' ', 2)[0].Trim();
ViewModel.StartAssignment(role);
ViewModel.StartAssignment(selected[0]);
}
})
.DisposeWith(_stepSubs);
Expand Down Expand Up @@ -384,8 +404,7 @@ private void HandleKeyPress(KeyPressed key)
var selectedItems = _roleList.SelectedItems;
if (selectedItems.Count > 0)
{
var role = selectedItems[0].Split(' ', 2)[0].Trim();
ViewModel.ClearRole(role);
ViewModel.ClearRole(selectedItems[0]);
}
}
return;
Expand All @@ -406,6 +425,18 @@ private void HandleKeyPress(KeyPressed key)
RouteInputToActiveComponent(keyInfo);
}

private enum ModelManagerContent
{
RoleOverview = ModelManagerState.RoleOverview,
SelectProvider = ModelManagerState.SelectProvider,
ConfirmAssignment = ModelManagerState.ConfirmAssignment,
DiscoverProbing,
DiscoverFailed,
DiscoverEmpty,
DiscoverManualEntry,
DiscoverList
}

private void RouteInputToActiveComponent(ConsoleKeyInfo keyInfo)
{
if (_lastFocusedInput is not null)
Expand Down
70 changes: 42 additions & 28 deletions src/Netclaw.Cli/Tui/ProviderManagerPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ProviderManagerPage(IClipboardService? clipboardService = null)

private IFocusable? _lastFocusedList;
private TextInputNode? _lastFocusedInput;
private DynamicLayoutNode? _contentNode;
private KeyedDynamicLayoutNode<(ProviderManagerState State, int Revision)>? _contentNode;
private readonly CompositeDisposable _stepSubs = [];

protected override void OnBound()
Expand Down Expand Up @@ -73,34 +73,37 @@ private ILayoutNode BuildInnerLayout()

private LayoutNode BuildContent()
{
_contentNode = new DynamicLayoutNode(() =>
{
_lastFocusedList = null;
_lastFocusedInput = null;
_stepSubs.Clear();

return ViewModel.CurrentState.Value switch
_contentNode = new KeyedDynamicLayoutNode<(ProviderManagerState State, int Revision)>(
GetContentKey,
_ =>
{
ProviderManagerState.Loading => BuildLoadingView(),
ProviderManagerState.List => BuildProviderListView(),
ProviderManagerState.AddSelectType => BuildAddSelectTypeView(),
ProviderManagerState.AddName => BuildAddNameView(),
ProviderManagerState.AddSelectAuth => BuildAddAuthView(),
ProviderManagerState.AddGitHubCopilotAuthHost => BuildGitHubCopilotAuthHostView(),
ProviderManagerState.AddGitHubCopilotEnterpriseHost => BuildGitHubCopilotEnterpriseHostView(),
ProviderManagerState.AddGitHubCopilotEnterpriseApiBase => BuildGitHubCopilotEnterpriseApiBaseView(),
ProviderManagerState.AddCredentials => BuildCredentialsView(),
ProviderManagerState.AddOAuthDeviceFlow => BuildOAuthDeviceFlowView(),
ProviderManagerState.AddBrowserOAuthFlow => BuildBrowserOAuthFlowView(),
ProviderManagerState.AddValidating => BuildValidatingView(),
ProviderManagerState.AddComplete => BuildAddCompleteView(),
ProviderManagerState.Details => BuildDetailsView(),
ProviderManagerState.RenameProvider => BuildRenameView(),
ProviderManagerState.FixCredentials => BuildFixCredentialsView(),
ProviderManagerState.RemoveConfirm => BuildRemoveConfirmView(),
_ => Layouts.Empty()
};
});
_lastFocusedList = null;
_lastFocusedInput = null;
_stepSubs.Clear();

return ViewModel.CurrentState.Value switch
{
ProviderManagerState.Loading => BuildLoadingView(),
ProviderManagerState.List => BuildProviderListView(),
ProviderManagerState.AddSelectType => BuildAddSelectTypeView(),
ProviderManagerState.AddName => BuildAddNameView(),
ProviderManagerState.AddSelectAuth => BuildAddAuthView(),
ProviderManagerState.AddGitHubCopilotAuthHost => BuildGitHubCopilotAuthHostView(),
ProviderManagerState.AddGitHubCopilotEnterpriseHost => BuildGitHubCopilotEnterpriseHostView(),
ProviderManagerState.AddGitHubCopilotEnterpriseApiBase => BuildGitHubCopilotEnterpriseApiBaseView(),
ProviderManagerState.AddCredentials => BuildCredentialsView(),
ProviderManagerState.AddOAuthDeviceFlow => BuildOAuthDeviceFlowView(),
ProviderManagerState.AddBrowserOAuthFlow => BuildBrowserOAuthFlowView(),
ProviderManagerState.AddValidating => BuildValidatingView(),
ProviderManagerState.AddComplete => BuildAddCompleteView(),
ProviderManagerState.Details => BuildDetailsView(),
ProviderManagerState.RenameProvider => BuildRenameView(),
ProviderManagerState.FixCredentials => BuildFixCredentialsView(),
ProviderManagerState.RemoveConfirm => BuildRemoveConfirmView(),
_ => Layouts.Empty()
};
},
KeyedDynamicCachePolicy.EvictOnKeyChange);

ViewModel.StateVersion
.Subscribe(_ => _contentNode.Invalidate())
Expand All @@ -113,6 +116,17 @@ private LayoutNode BuildContent()
return _contentNode;
}

private (ProviderManagerState State, int Revision) GetContentKey()
{
var state = ViewModel.CurrentState.Value;
var revision = state is ProviderManagerState.Loading
or ProviderManagerState.AddValidating
or ProviderManagerState.Details
? ViewModel.StateVersion.Value
: 0;
return (state, revision);
}

private LayoutNode BuildStatusBar()
{
// Combine StatusMessage (success/green) with ErrorMessage (error/red).
Expand Down
Loading