- Add OpenClawProcessOwner class to manage the lifecycle of the OpenClaw process. - Introduce utility functions for managing OpenClaw runtime paths. - Update session store to normalize agent session keys and migrate existing keys. - Refactor main process to handle local provider API routing through a new dispatch function. - Enhance token usage writer to utilize a new session key parsing function. - Create agents management store to handle agent data and interactions. - Update chat store to integrate agent selection and session management. - Introduce AgentsSection component for displaying agent information in the UI. - Refactor HomePage to support agent selection and display current agent. - Update routing to reflect new agents page structure.
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import type { ProviderAccount } from '@runtime/lib/providers';
|
|
import {
|
|
DEFAULT_AGENT_ID,
|
|
DEFAULT_MAIN_SESSION_SUFFIX,
|
|
buildMainSessionKey,
|
|
normalizeAgentId,
|
|
type AgentSummary,
|
|
} from '@runtime/lib/agents';
|
|
import type { HostApiContext } from '../context';
|
|
import type { NormalizedHostApiRequest } from '../route-utils';
|
|
import { ok } from '../route-utils';
|
|
|
|
function formatModelDisplay(modelRef: string | null | undefined, fallbackLabel: string): string {
|
|
const trimmed = String(modelRef ?? '').trim();
|
|
if (!trimmed) return fallbackLabel;
|
|
|
|
const parts = trimmed.split('/');
|
|
return parts[parts.length - 1] || trimmed;
|
|
}
|
|
|
|
function buildMainAgent(defaultAccount: ProviderAccount | null): AgentSummary {
|
|
return {
|
|
id: DEFAULT_AGENT_ID,
|
|
name: 'Main Agent',
|
|
isDefault: true,
|
|
providerAccountId: defaultAccount?.id ?? null,
|
|
modelRef: defaultAccount?.model ?? null,
|
|
modelDisplay: formatModelDisplay(defaultAccount?.model, defaultAccount?.label || 'Unassigned'),
|
|
mainSessionKey: buildMainSessionKey(DEFAULT_AGENT_ID, DEFAULT_MAIN_SESSION_SUFFIX),
|
|
vendorId: defaultAccount?.vendorId ?? null,
|
|
source: 'synthetic-main',
|
|
};
|
|
}
|
|
|
|
function buildProviderBackedAgents(accounts: ProviderAccount[]): AgentSummary[] {
|
|
const seen = new Set<string>();
|
|
const summaries: AgentSummary[] = [];
|
|
|
|
for (const account of accounts) {
|
|
const agentId = normalizeAgentId(account.id);
|
|
if (seen.has(agentId) || agentId === DEFAULT_AGENT_ID) continue;
|
|
seen.add(agentId);
|
|
|
|
summaries.push({
|
|
id: agentId,
|
|
name: account.label || agentId,
|
|
isDefault: false,
|
|
providerAccountId: account.id,
|
|
modelRef: account.model ?? null,
|
|
modelDisplay: formatModelDisplay(account.model, account.label || agentId),
|
|
mainSessionKey: buildMainSessionKey(agentId, DEFAULT_MAIN_SESSION_SUFFIX),
|
|
vendorId: account.vendorId,
|
|
source: 'provider-account',
|
|
});
|
|
}
|
|
|
|
return summaries;
|
|
}
|
|
|
|
export async function handleAgentRoutes(
|
|
request: NormalizedHostApiRequest,
|
|
ctx: HostApiContext,
|
|
) {
|
|
const { pathname, method } = request;
|
|
if (pathname !== '/api/agents' || method !== 'GET') {
|
|
return null;
|
|
}
|
|
|
|
const accounts = ctx.providerApiService
|
|
.getAccounts()
|
|
.filter((account) => account.enabled !== false);
|
|
const defaultAccountId = ctx.providerApiService.getDefault().accountId;
|
|
const defaultAccount = accounts.find((account) => account.id === defaultAccountId) ?? accounts[0] ?? null;
|
|
|
|
const agents = [
|
|
buildMainAgent(defaultAccount),
|
|
...buildProviderBackedAgents(accounts),
|
|
];
|
|
|
|
return ok({
|
|
success: true,
|
|
agents,
|
|
defaultAgentId: DEFAULT_AGENT_ID,
|
|
defaultProviderAccountId: defaultAccount?.id ?? null,
|
|
defaultModelRef: defaultAccount?.model ?? null,
|
|
mainSessionSuffix: DEFAULT_MAIN_SESSION_SUFFIX,
|
|
configuredChannelTypes: [],
|
|
channelOwners: {},
|
|
channelAccountOwners: {},
|
|
});
|
|
}
|