Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
feat(console): offer a slug instead of rejecting an agent name
raullopez-sandbox88b1562d2c5b
diff --git a/connectors/claude-code-plugin/skills/configure/SKILL.md b/connectors/claude-code-plugin/skills/configure/SKILL.mdindex 195175291..021affb6b 100644--- a/connectors/claude-code-plugin/skills/configure/SKILL.md+++ b/connectors/claude-code-plugin/skills/configure/SKILL.md@@ -265,7 +265,9 @@ collides and nobody can tell which human is behind which agent:from `$USER` or `whoami`.To slugify: lowercase, replace anything outside `[a-z0-9._-]` with `-`, collapse-repeats, strip leading/trailing `-`.+runs of `-`, strip leading/trailing `.`, `_` and `-`. Strip all three, not just `-`:+the pattern requires a letter or digit first, so a surviving leading `.` or `_`+is rejected all over again.Confirm the name or accept a custom one; if it fails the regex, explain why andask again. If it carries no user identifier, flag the collision risk once anddiff --git a/connectors/codex-plugin/skills/configure/SKILL.md b/connectors/codex-plugin/skills/configure/SKILL.mdindex 6efd0c645..009e5ec63 100644--- a/connectors/codex-plugin/skills/configure/SKILL.md+++ b/connectors/codex-plugin/skills/configure/SKILL.md@@ -193,7 +193,9 @@ and nobody can tell which human is behind which agent:from `$USER` or `whoami`.To slugify: lowercase, replace anything outside `[a-z0-9._-]` with `-`, collapse-repeats, strip leading/trailing `-`.+runs of `-`, strip leading/trailing `.`, `_` and `-`. Strip all three, not just `-`:+the pattern requires a letter or digit first, so a surviving leading `.` or `_`+is rejected all over again.Confirm the name or accept a custom one; if it fails the regex, explain why andask again. If it carries no user identifier, flag the collision risk once anddiff --git a/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.test.ts b/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.test.tsindex 866612f9e..17a451ca2 100644--- a/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.test.ts+++ b/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.test.ts@@ -1,29 +1,12 @@import { describe, expect, it } from 'vitest';-import { slugifyAgentNamePart, suggestAgentDefaults } from './agent-defaults';--describe('slugifyAgentNamePart', () => {- it('lowercases and keeps the allowed charset', () => {- expect(slugifyAgentNamePart('My-Repo.v2_x')).toBe('my-repo.v2_x');- });-- it('replaces disallowed characters with a single hyphen', () => {- expect(slugifyAgentNamePart('foo bar//baz')).toBe('foo-bar-baz');- });-- it('strips leading and trailing hyphens', () => {- expect(slugifyAgentNamePart(' @weird@ ')).toBe('weird');- });-- it('returns empty for an all-disallowed string', () => {- expect(slugifyAgentNamePart('@@@')).toBe('');- });-});+import { AGENT_NAME_PATTERN } from '@shared/core/agents/agent-slug';+import { suggestAgentDefaults } from './agent-defaults';describe('suggestAgentDefaults', () => {it('builds a claude-code.<repo>.<user> name and matches the agent-name pattern', () => {const { name, description } = suggestAgentDefaults('/Users/someone/My Repo', 'claude');expect(name.startsWith('claude-code.my-repo')).toBe(true);- expect(name).toMatch(/^[a-z0-9][a-z0-9._-]*$/);+ expect(name).toMatch(AGENT_NAME_PATTERN);expect(description).toBe('Claude Code running in My Repo');});@@ -31,7 +14,13 @@ describe('suggestAgentDefaults', () => {const { name, description } = suggestAgentDefaults('/Users/someone/My Repo', 'codex');expect(name.startsWith('codex.my-repo')).toBe(true);expect(name).not.toContain('claude-code');- expect(name).toMatch(/^[a-z0-9][a-z0-9._-]*$/);+ expect(name).toMatch(AGENT_NAME_PATTERN);expect(description).toBe('Codex running in My Repo');});++ it('slugifies a dotfile-style directory into a valid name part', () => {+ const { name } = suggestAgentDefaults('/Users/someone/.Hidden Repo', 'codex');+ expect(name.startsWith('codex.hidden-repo')).toBe(true);+ expect(name).toMatch(AGENT_NAME_PATTERN);+ });});diff --git a/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.ts b/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.tsindex e194dd865..c6d9f622f 100644--- a/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.ts+++ b/console/apps/switch-console-desktop/src/main/core/agents/agent-defaults.ts@@ -1,22 +1,9 @@import os from 'node:os';import path from 'node:path';+import { slugifyAgentNamePart } from '@shared/core/agents/agent-slug';import { getProvider, type AgentProviderId } from '@shared/core/providers/agent-provider-registry';import type { AgentDefaults } from '@shared/core/switch-servers/switch-servers';-/**- * Slugify a string into the Switch agent-name charset: lowercase letters,- * digits, `.`, `-`, `_`. Any other character becomes `-`; runs of `-` collapse- * and leading/trailing `-` are stripped. Mirrors the `configure` skill's slug- * rule so desktop-registered agents are named the same way.- */-export function slugifyAgentNamePart(value: string): string {- return value- .toLowerCase()- .replace(/[^a-z0-9._-]+/g, '-')- .replace(/-+/g, '-')- .replace(/^-+|-+$/g, '');-}-/*** Suggest a default name and description for a new agent of `providerId` in* `dir`. The name is `<provider-slug>.<repo-slug>.<user-slug>`, where the prefixdiff --git a/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/configure-agent-panel.tsx b/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/configure-agent-panel.tsxindex 9ecf96744..b2e56532f 100644--- a/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/configure-agent-panel.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/configure-agent-panel.tsx@@ -235,6 +235,8 @@ export function AgentIdentityFields({ form }: { form: ConfigureAgentFormState })const nameId = useId();const descriptionId = useId();const instructionsId = useId();+ const nameRef = useRef<HTMLInputElement>(null);+return (<FieldGroup>{/* Above the name, because it is the first thing the finished agent is@@ -255,16 +257,35 @@ export function AgentIdentityFields({ form }: { form: ConfigureAgentFormState })<FieldLabel htmlFor={nameId}>Name</FieldLabel><Inputid={nameId}+ ref={nameRef}placeholder="Name this agent"value={form.agentName}onChange={(e) => form.setAgentName(e.target.value)}- aria-invalid={form.agentName.length > 0 && !form.nameIsValid}+ aria-invalid={form.nameIsRejected}/>- {form.agentName.length > 0 && !form.nameIsValid ? (- <span className="text-destructive text-xs">- Use lowercase letters, digits, <span className="font-mono">. - _</span>, starting with a- letter or digit. No spaces or uppercase.- </span>+ {form.nameIsRejected ? (+ <div className="flex flex-col items-start gap-1.5">+ <span className="text-destructive text-xs">+ Use lowercase letters, digits, <span className="font-mono">. - _</span>, starting with+ a letter or digit. No spaces or uppercase.+ </span>+ {form.suggestedName && (+ <Button+ variant="outline"+ size="xs"+ className="max-w-full"+ // Accepting the offer makes the name valid, which unmounts this+ // button; without moving focus first it falls to the body and+ // the tab order restarts.+ onClick={() => {+ form.setAgentName(form.suggestedName);+ nameRef.current?.focus();+ }}+ >+ Use <span className="truncate font-mono">{form.suggestedName}</span>+ </Button>+ )}+ </div>) : form.agentName.length > 0 ? (// Once there is a name, show the handle it produces rather than// repeating the advice: the advice is about choosing a name, and itdiff --git a/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/modes.ts b/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/modes.tsindex 811b6c8e8..e890aa067 100644--- a/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/modes.ts+++ b/console/apps/switch-console-desktop/src/renderer/features/locations/components/add-agent-modal/modes.ts@@ -1,13 +1,10 @@import { useCallback, useState } from 'react';import { randomAgentAvatarUrl } from '@shared/core/agents/agent-avatar';+import { AGENT_NAME_PATTERN, slugifyAgentNamePart } from '@shared/core/agents/agent-slug';import type { AgentProviderId } from '@shared/core/providers/agent-provider-registry';import { ownerOnlyPolicy } from '@shared/core/switch-servers/owner-policy';import type { AddressingPolicy } from '@shared/core/switch-servers/switch-servers';-/** Switch agent-name charset, enforced server-side too: lowercase letters,- * digits, `.`, `-`, `_`, starting with a letter or digit. */-export const AGENT_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;-export function usePickMode() {const [path, setPath] = useState('');const [serverId, setServerId] = useState<string | null>(null);@@ -85,12 +82,19 @@ export function useConfigureAgentForm() {);const nameIsValid = AGENT_NAME_PATTERN.test(agentName);+ const nameIsRejected = agentName.length > 0 && !nameIsValid;+ // A rejected name the app can repair itself is an offer, not a dead end. Only+ // an offer: rewriting as the user types moves text out from under them. Empty+ // when nothing of the name survives slugging, and there is nothing to offer.+ const suggestedName = nameIsRejected ? slugifyAgentNamePart(agentName) : '';const isValid = nameIsValid && description.trim().length > 0;return {agentName,setAgentName,nameIsValid,+ nameIsRejected,+ suggestedName,description,setDescription,instructions,diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/new-agent-name-suggestion.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/new-agent-name-suggestion.test.tsxnew file mode 100644index 000000000..dcea7b18b--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/new-agent-name-suggestion.test.tsx@@ -0,0 +1,127 @@+/**+ * The offer made to someone who typed a name Switch will not accept.+ *+ * A display name — "Switch Dev" — is the obvious thing to type and the one+ * thing the charset rejects, so the field offers the slug instead of leaving+ * the reader to work out the rule. The offer is a button, never an edit: text+ * rewritten under the cursor is worse than an error. Pinned here because the+ * slugifier's own tests say nothing about whether anything on screen uses it.+ */+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, describe, expect, it, vi } from 'vitest';++vi.mock('@renderer/lib/ipc', () => ({+ events: { on: vi.fn() },+ rpc: {+ switchServers: {+ listRemoteRooms: () => Promise.resolve([]),+ listRemoteRoomGroups: () => Promise.resolve([]),+ listRemoteExternalUsers: () => Promise.resolve([]),+ listRemoteAgents: () => Promise.resolve([]),+ listRemoteBridges: () => Promise.resolve([]),+ listMyIdentities: () => Promise.resolve([]),+ },+ },+}));++import { AgentIdentityFields } from '@renderer/features/locations/components/add-agent-modal/configure-agent-panel';+import { useConfigureAgentForm } from '@renderer/features/locations/components/add-agent-modal/modes';++let container: HTMLDivElement | null = null;+let root: Root | null = null;+let form: ReturnType<typeof useConfigureAgentForm> | null = null;++afterEach(async () => {+ if (root) await act(async () => root!.unmount());+ container?.remove();+ container = null;+ root = null;+ form = null;+});++function Fields() {+ form = useConfigureAgentForm();+ return <AgentIdentityFields form={form} />;+}++async function renderFields(): Promise<HTMLDivElement> {+ container = document.createElement('div');+ document.body.appendChild(container);+ root = createRoot(container);+ await act(async () => root!.render(<Fields />));+ return container;+}++/** Type into the controlled Name input the way React will notice. */+async function typeName(el: HTMLElement, value: string) {+ const field = el.querySelector<HTMLInputElement>('input[placeholder="Name this agent"]');+ expect(field, 'no Name input').not.toBeNull();+ const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!;+ await act(async () => {+ setter.call(field!, value);+ field!.dispatchEvent(new Event('input', { bubbles: true }));+ });+}++function suggestionButton(el: HTMLElement): HTMLButtonElement | undefined {+ return [...el.querySelectorAll<HTMLButtonElement>('button')].find((b) =>+ /^Use\b/.test(b.textContent?.trim() ?? '')+ );+}++const REJECTION = 'Use lowercase letters, digits';++describe('a rejected agent name', () => {+ it('offers the slug of what was typed', async () => {+ const el = await renderFields();+ await typeName(el, 'Switch Dev');++ expect(el.textContent).toContain(REJECTION);+ const button = suggestionButton(el);+ expect(button?.textContent?.trim()).toBe('Use switch-dev');+ // Speech input says what it reads, so the accessible name has to be the+ // visible one rather than a paraphrase of it (WCAG 2.5.3). With no+ // aria-label the text content above is the accessible name.+ expect(button?.getAttribute('aria-label')).toBeNull();+ });++ it('fills the field in when the offer is accepted', async () => {+ const el = await renderFields();+ await typeName(el, 'Switch Dev');+ await act(async () => suggestionButton(el)!.click());++ expect(form?.agentName).toBe('switch-dev');+ expect(form?.nameIsValid).toBe(true);+ expect(el.textContent).not.toContain(REJECTION);+ expect(suggestionButton(el)).toBeUndefined();+ });++ it('keeps focus in the field it repaired', async () => {+ // The offer unmounts on acceptance; without moving focus first it falls to+ // the body and the tab order restarts.+ const el = await renderFields();+ await typeName(el, 'Switch Dev');+ await act(async () => suggestionButton(el)!.click());++ expect(document.activeElement).toBe(+ el.querySelector<HTMLInputElement>('input[placeholder="Name this agent"]')+ );+ });++ it('offers nothing when nothing of the name survives slugging', async () => {+ const el = await renderFields();+ await typeName(el, '@@@');++ expect(el.textContent).toContain(REJECTION);+ expect(suggestionButton(el)).toBeUndefined();+ });++ it('says nothing at all about a name that is already valid', async () => {+ const el = await renderFields();+ await typeName(el, 'switch-dev');++ expect(el.textContent).not.toContain(REJECTION);+ expect(suggestionButton(el)).toBeUndefined();+ });+});diff --git a/console/apps/switch-console-desktop/src/shared/core/agents/agent-slug.test.ts b/console/apps/switch-console-desktop/src/shared/core/agents/agent-slug.test.tsnew file mode 100644index 000000000..dc84519fb--- /dev/null+++ b/console/apps/switch-console-desktop/src/shared/core/agents/agent-slug.test.ts@@ -0,0 +1,63 @@+import { describe, expect, it } from 'vitest';+import { AGENT_NAME_PATTERN, slugifyAgentNamePart } from './agent-slug';++const INPUTS = [+ '',+ 'Switch Dev',+ 'Buddy',+ ' ',+ '@@@',+ '.Foo',+ '_bar',+ 'foo--bar',+ 'Ünïcødé',+ 'UPPER.Case_9',+ 'x'.repeat(4096),+ // Separators are in the charset, so a run of them reaches the trailing strip+ // uncollapsed — the shape that made an earlier form of it quadratic.+ `a${'.'.repeat(50_000)}a`,+ '_'.repeat(50_000),+];++describe('slugifyAgentNamePart', () => {+ // The invariant the suggestion chip rests on: a slug is either offerable or+ // absent, never a name the validator would reject all over again.+ it.each(INPUTS)('returns an empty string or a valid name for %j', (input) => {+ const slug = slugifyAgentNamePart(input);+ expect(slug === '' || AGENT_NAME_PATTERN.test(slug)).toBe(true);+ });++ it('lowercases and keeps the allowed charset', () => {+ expect(slugifyAgentNamePart('My-Repo.v2_x')).toBe('my-repo.v2_x');+ });++ it('replaces disallowed characters with a single hyphen', () => {+ expect(slugifyAgentNamePart('foo bar//baz')).toBe('foo-bar-baz');+ });++ it('slugifies a spaced display name into a hyphenated one', () => {+ expect(slugifyAgentNamePart('Switch Dev')).toBe('switch-dev');+ });++ it('strips leading and trailing separators, not only hyphens', () => {+ expect(slugifyAgentNamePart('.Foo')).toBe('foo');+ expect(slugifyAgentNamePart('_bar')).toBe('bar');+ expect(slugifyAgentNamePart('baz_')).toBe('baz');+ expect(slugifyAgentNamePart(' @weird@ ')).toBe('weird');+ });++ it('returns empty when nothing survives', () => {+ expect(slugifyAgentNamePart('@@@')).toBe('');+ expect(slugifyAgentNamePart(' ')).toBe('');+ expect(slugifyAgentNamePart('')).toBe('');+ });++ it('offers nothing rather than a shortened name for an over-long one', () => {+ expect(slugifyAgentNamePart('a'.repeat(128))).toBe('a'.repeat(128));+ expect(slugifyAgentNamePart('a'.repeat(129))).toBe('');+ });++ it('leaves an already-valid name alone', () => {+ expect(slugifyAgentNamePart('buddy')).toBe('buddy');+ });+});diff --git a/console/apps/switch-console-desktop/src/shared/core/agents/agent-slug.ts b/console/apps/switch-console-desktop/src/shared/core/agents/agent-slug.tsnew file mode 100644index 000000000..4d4106061--- /dev/null+++ b/console/apps/switch-console-desktop/src/shared/core/agents/agent-slug.ts@@ -0,0 +1,31 @@+/** Switch agent-name charset, enforced server-side too by `_VALID_NAME_RE` in+ * `core/switch_core/bridges/agent/protocol/service.py`: lowercase letters,+ * digits, `.`, `-`, `_`, starting with a letter or digit. */+export const AGENT_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;++/** Past this there is nothing to suggest. The trailing-separator strip below+ * backtracks quadratically over a long run of separators, and an agent name is+ * short — so a longer string is refused rather than bounded, which would offer+ * back a quietly shortened name. */+const MAX_SLUG_INPUT = 128;++/**+ * Slugify a string into the agent-name charset, so that what comes back either+ * matches `AGENT_NAME_PATTERN` or is empty. That is the invariant the two are+ * kept in one module for: the name field offers a slug to repair a name the+ * pattern rejected, so a slug the pattern also rejects would re-offer the same+ * error.+ *+ * Leading and trailing `.` and `_` go the way of `-`, since the pattern wants a+ * letter or digit first and a name ending in a separator reads as truncated.+ *+ * Each connector plugin's `configure` skill documents this rule for agents that+ * register without Switch Console; keep them in step.+ */+export function slugifyAgentNamePart(value: string): string {+ if (value.length > MAX_SLUG_INPUT) return '';+ return value+ .toLowerCase()+ .replace(/[^a-z0-9._]+/g, '-')+ .replace(/^[._-]+|[._-]+$/g, '');+}