feature: channels and skills (#2)

Co-authored-by: paisley <8197966+su8su@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Felix
2026-02-06 18:26:06 +08:00
committed by GitHub
Unverified
parent f9845023c3
commit fa6c23b82a
23 changed files with 4315 additions and 802 deletions

View File

@@ -34,10 +34,107 @@ export const useChannelsStore = create<ChannelsState>((set, get) => ({
error: null,
fetchChannels: async () => {
// channels.status returns a complex nested object, not a simple array.
// Channel management is deferred to Settings > Channels page.
// For now, just use empty list - channels will be added later.
set({ channels: [], loading: false });
set({ loading: true, error: null });
try {
const result = await window.electron.ipcRenderer.invoke(
'gateway:rpc',
'channels.status',
{ probe: true }
) as {
success: boolean;
result?: {
channelOrder?: string[];
channels?: Record<string, unknown>;
channelAccounts?: Record<string, Array<{
accountId?: string;
configured?: boolean;
connected?: boolean;
running?: boolean;
lastError?: string;
name?: string;
linked?: boolean;
lastConnectedAt?: number | null;
lastInboundAt?: number | null;
lastOutboundAt?: number | null;
}>>;
channelDefaultAccountId?: Record<string, string>;
};
error?: string;
};
if (result.success && result.result) {
const data = result.result;
const channels: Channel[] = [];
// Parse the complex channels.status response into simple Channel objects
const channelOrder = data.channelOrder || Object.keys(data.channels || {});
for (const channelId of channelOrder) {
const summary = (data.channels as Record<string, unknown> | undefined)?.[channelId] as Record<string, unknown> | undefined;
const configured =
typeof summary?.configured === 'boolean'
? summary.configured
: typeof (summary as { running?: boolean })?.running === 'boolean'
? true
: false;
if (!configured) continue;
const accounts = data.channelAccounts?.[channelId] || [];
const defaultAccountId = data.channelDefaultAccountId?.[channelId];
const primaryAccount =
(defaultAccountId ? accounts.find((a) => a.accountId === defaultAccountId) : undefined) ||
accounts.find((a) => a.connected === true || a.linked === true) ||
accounts[0];
// Map gateway status to our status format
let status: Channel['status'] = 'disconnected';
const now = Date.now();
const RECENT_MS = 10 * 60 * 1000;
const hasRecentActivity = (a: { lastInboundAt?: number | null; lastOutboundAt?: number | null; lastConnectedAt?: number | null }) =>
(typeof a.lastInboundAt === 'number' && now - a.lastInboundAt < RECENT_MS) ||
(typeof a.lastOutboundAt === 'number' && now - a.lastOutboundAt < RECENT_MS) ||
(typeof a.lastConnectedAt === 'number' && now - a.lastConnectedAt < RECENT_MS);
const anyConnected = accounts.some((a) => a.connected === true || a.linked === true || hasRecentActivity(a));
const anyRunning = accounts.some((a) => a.running === true);
const summaryError =
typeof (summary as { error?: string })?.error === 'string'
? (summary as { error?: string }).error
: typeof (summary as { lastError?: string })?.lastError === 'string'
? (summary as { lastError?: string }).lastError
: undefined;
const anyError =
accounts.some((a) => typeof a.lastError === 'string' && a.lastError) || Boolean(summaryError);
if (anyConnected) {
status = 'connected';
} else if (anyRunning && !anyError) {
status = 'connected';
} else if (anyError) {
status = 'error';
} else if (anyRunning) {
status = 'connecting';
}
channels.push({
id: `${channelId}-${primaryAccount?.accountId || 'default'}`,
type: channelId as ChannelType,
name: primaryAccount?.name || channelId,
status,
accountId: primaryAccount?.accountId,
error:
(typeof primaryAccount?.lastError === 'string' ? primaryAccount.lastError : undefined) ||
(typeof summaryError === 'string' ? summaryError : undefined),
});
}
set({ channels, loading: false });
} else {
// Gateway not available - try to show channels from local config
set({ channels: [], loading: false });
}
} catch {
// Gateway not connected, show empty
set({ channels: [], loading: false });
}
},
addChannel: async (params) => {