AI-native design editor. Open-source Figma alternative.
refactor: look up a provider's ACP agent with acpAgentFor (#1728)
Danila Poyarkov57c955262e41
diff --git a/packages/core/src/ai/providers.ts b/packages/core/src/ai/providers.tsindex 6331a5cbf..2bd905cd3 100644--- a/packages/core/src/ai/providers.ts+++ b/packages/core/src/ai/providers.ts@@ -75,6 +75,11 @@ export const ACP_AGENTS: ACPAgentDef[] = [}]+/** The ACP agent a provider runs, or undefined for a provider that is not one. */+export function acpAgentFor(providerID: string): ACPAgentDef | undefined {+ return ACP_AGENTS.find((agent) => `acp:${agent.id}` === providerID)+}+export type AIProviderID =| 'openrouter'| 'anthropic'diff --git a/packages/core/tests/ai/providers.test.ts b/packages/core/tests/ai/providers.test.tsnew file mode 100644index 000000000..90b4c5ff2--- /dev/null+++ b/packages/core/tests/ai/providers.test.ts@@ -0,0 +1,15 @@+import { describe, expect, test } from 'bun:test'++import { acpAgentFor } from '#core/ai/providers'++describe('acpAgentFor', () => {+ test('finds the agent an acp: provider ID names', () => {+ expect(acpAgentFor('acp:codex')?.id).toBe('codex')+ expect(acpAgentFor('acp:claude-code')?.id).toBe('claude-code')+ })++ test('rejects anything but an exact acp: provider ID', () => {+ for (const providerID of ['codex', 'xacp:codex', 'acp:codex ', 'acp:', 'acp:unknown', 'openai'])+ expect(acpAgentFor(providerID)).toBeUndefined()+ })+})diff --git a/src/app/ai/agents/setup/use.ts b/src/app/ai/agents/setup/use.tsindex 83b0d2857..537a885a3 100644--- a/src/app/ai/agents/setup/use.ts+++ b/src/app/ai/agents/setup/use.ts@@ -1,6 +1,6 @@import { computed, ref, toValue, watch, type MaybeRefOrGetter } from 'vue'-import { ACP_AGENTS, type ACPAgentDef, type ACPAgentID } from '@open-pencil/core/constants'+import { acpAgentFor, type ACPAgentDef, type ACPAgentID } from '@open-pencil/core/constants'import { readPiAccount } from '@/app/ai/agents/pi/settings'@@ -22,7 +22,7 @@ export function useAgentSetup(const readingPi = ref(false)function agentID(providerID: string): ACPAgentID | null {- return ACP_AGENTS.find((agent) => `acp:${agent.id}` === providerID)?.id ?? null+ return acpAgentFor(providerID)?.id ?? null}function agentSetup(providerID: string): AgentSetupState {@@ -174,9 +174,7 @@ export function useAgentSetup(* outside the desktop app, where agents cannot run.*/export function useCodingAgentSetup(providerID: MaybeRefOrGetter<string>, setup = useAgentSetup()) {- const agent = computed(() =>- ACP_AGENTS.find((definition) => `acp:${definition.id}` === toValue(providerID))- )+ const agent = computed(() => acpAgentFor(toValue(providerID)))const state = computed(() => {if (!agent.value) return undefinedconst current = setup.agentSetup(toValue(providerID))diff --git a/src/app/ai/chat/session.ts b/src/app/ai/chat/session.tsindex d1d9ec9c4..72100b180 100644--- a/src/app/ai/chat/session.ts+++ b/src/app/ai/chat/session.ts@@ -3,8 +3,8 @@ import type { ChatTransport, FinishReason, UIMessage } from 'ai'import type { ComputedRef, Ref } from 'vue'import { ref } from 'vue'-import { ACP_AGENTS, AI_PROVIDERS } from '@open-pencil/core/constants'-import type { ACPAgentID, AIProviderID } from '@open-pencil/core/constants'+import { acpAgentFor, AI_PROVIDERS } from '@open-pencil/core/constants'+import type { AIProviderID } from '@open-pencil/core/constants'import { readPiAccount } from '@/app/ai/agents/pi/settings'import { agentWorkspace } from '@/app/ai/agents/setup/native'@@ -57,9 +57,8 @@ export async function createACPTransport(store?: EditorStore,onUsage?: (usage: AgentTurnUsage) => void) {- const agentId = providerID.replace('acp:', '') as ACPAgentID- const agentDef = ACP_AGENTS.find((a) => a.id === agentId)- if (!agentDef) throw new Error(`Unknown ACP agent: ${agentId}`)+ const agentDef = acpAgentFor(providerID)+ if (!agentDef) throw new Error(`Unknown ACP agent: ${providerID}`)// A folder of its own, not the home folder, whose project settings the agent would read.return new ACPChatTransport({ agentDef, cwd: await agentWorkspace(), store, onUsage })@@ -84,8 +83,7 @@ async function settleEndedReply(/** The agent or provider a chat runs on, as the composer names it. */function providerName(id: AIProviderID): string {- const agent = ACP_AGENTS.find((candidate) => `acp:${candidate.id}` === id)- return agent?.name ?? AI_PROVIDERS.find((provider) => provider.id === id)?.name ?? id+ return acpAgentFor(id)?.name ?? AI_PROVIDERS.find((provider) => provider.id === id)?.name ?? id}export function createChatSessionManager({diff --git a/src/app/ai/models/provider-name.ts b/src/app/ai/models/provider-name.tsindex 70a754a3d..537e19fd3 100644--- a/src/app/ai/models/provider-name.ts+++ b/src/app/ai/models/provider-name.ts@@ -1,11 +1,8 @@-import { ACP_AGENTS, AI_PROVIDERS, HARNESS_PROVIDER_ID } from '@open-pencil/core/constants'+import { acpAgentFor, AI_PROVIDERS, HARNESS_PROVIDER_ID } from '@open-pencil/core/constants'/** Display name for a provider, agent, or the Pi harness. */export function modelProviderName(providerID: string): string {if (providerID === HARNESS_PROVIDER_ID) return 'Pi'- if (providerID.startsWith('acp:')) {- const agentID = providerID.slice('acp:'.length)- return ACP_AGENTS.find((agent) => agent.id === agentID)?.name ?? providerID- }+ if (providerID.startsWith('acp:')) return acpAgentFor(providerID)?.name ?? providerIDreturn AI_PROVIDERS.find((provider) => provider.id === providerID)?.name ?? providerID}diff --git a/src/app/ai/models/settings/onboarding/use.ts b/src/app/ai/models/settings/onboarding/use.tsindex dda3cd10b..7ed6f2f69 100644--- a/src/app/ai/models/settings/onboarding/use.ts+++ b/src/app/ai/models/settings/onboarding/use.ts@@ -1,7 +1,7 @@import { uniq } from 'es-toolkit'import { computed, reactive, ref, watch } from 'vue'-import { ACP_AGENTS, IS_TAURI } from '@open-pencil/core/constants'+import { acpAgentFor, IS_TAURI } from '@open-pencil/core/constants'import {codingAgentDetection,@@ -99,7 +99,7 @@ export function useAIOnboarding({ agentsAvailable = IS_TAURI }: AIOnboardingOptiif (!agentsAvailable) return foundfor (const providerID of ONBOARDING_AGENTS) {const pi = agents.piSetup(providerID)- const agent = ACP_AGENTS.find((candidate) => `acp:${candidate.id}` === providerID)+ const agent = acpAgentFor(providerID)if (pi) found[providerID] = piDetection(pi)else if (agent) found[providerID] = codingAgentDetection(agent, agents.agentSetup(providerID))}diff --git a/src/app/ai/models/settings/profile-editor/selection.ts b/src/app/ai/models/settings/profile-editor/selection.tsindex c734970f0..d590482e9 100644--- a/src/app/ai/models/settings/profile-editor/selection.ts+++ b/src/app/ai/models/settings/profile-editor/selection.ts@@ -1,7 +1,7 @@import { computed, ref, watch, type Ref } from 'vue'import {- ACP_AGENTS,+ acpAgentFor,AI_PROVIDERS,type AIProviderID,type ModelOption@@ -42,9 +42,7 @@ export function useProfileModelSelection(const supportsThinking = computed(() => supportsThinkingLevel(draft.providerID))const providerDisplayName = computed(() => {if (!isACP.value) return providerDef.value.name-- const agentID = draft.providerID.slice('acp:'.length)- return ACP_AGENTS.find((agent) => agent.id === agentID)?.name ?? draft.providerID+ return acpAgentFor(draft.providerID)?.name ?? draft.providerID})const modelOptions = computed(() => {const options = modelPickerOptions(availableModels.value, providerDef.value.models, ai.value)diff --git a/src/app/ai/models/settings/status.ts b/src/app/ai/models/settings/status.tsindex 4ff778ce3..ec640f03d 100644--- a/src/app/ai/models/settings/status.ts+++ b/src/app/ai/models/settings/status.ts@@ -1,6 +1,6 @@import type { Ref } from 'vue'-import { ACP_AGENTS, HARNESS_PROVIDER_ID } from '@open-pencil/core/constants'+import { acpAgentFor, HARNESS_PROVIDER_ID } from '@open-pencil/core/constants'import {codingAgentDetection,@@ -93,7 +93,7 @@ export function useModelConnectionStates(const pi = setup.piSetup(providerID)return pi?.supported ? { agent: piDetection(pi), toolsUnreachable: false } : none}- const definition = ACP_AGENTS.find((agent) => `acp:${agent.id}` === providerID)+ const definition = acpAgentFor(providerID)const agent = definition && setup.agentSetup(providerID)if (!definition || !agent?.supported) return nonereturn {diff --git a/src/components/chat/ChatInput.vue b/src/components/chat/ChatInput.vueindex 6e2b12692..fc5193876 100644--- a/src/components/chat/ChatInput.vue+++ b/src/components/chat/ChatInput.vue@@ -1,7 +1,7 @@<script setup lang="ts">import { computed, useTemplateRef } from 'vue'-import { ACP_AGENTS } from '@open-pencil/core/constants'+import { acpAgentFor } from '@open-pencil/core/constants'import { useSelectionState } from '@open-pencil/vue/editor/selection-state'import { useI18n } from '@open-pencil/vue/i18n'@@ -106,8 +106,7 @@ const isAgentProvider = computed()const agentName = computed(() => {if (providerID.value === 'harness:pi') return 'Pi'- const agentId = providerID.value.replace('acp:', '')- return ACP_AGENTS.find((a) => a.id === agentId)?.name ?? agentId+ return acpAgentFor(providerID.value)?.name ?? providerID.value.replace('acp:', '')})const isCustomProvider = computed(() => providerID.value === 'openai-compatible' || providerID.value === 'anthropic-compatible'diff --git a/src/components/settings/ai-setup/AISetupConnection.vue b/src/components/settings/ai-setup/AISetupConnection.vueindex 1e5b6227b..7b922735e 100644--- a/src/components/settings/ai-setup/AISetupConnection.vue+++ b/src/components/settings/ai-setup/AISetupConnection.vue@@ -2,7 +2,7 @@import { tv } from 'tailwind-variants'import { computed } from 'vue'-import { ACP_AGENTS, AI_PROVIDERS } from '@open-pencil/core/constants'+import { acpAgentFor, AI_PROVIDERS } from '@open-pencil/core/constants'import { useI18n } from '@open-pencil/vue/i18n'import type { AgentSetupState, PiSetupState } from '@/app/ai/agents/setup/view'@@ -81,11 +81,7 @@ const name = computed(() =>? ai.value.aiSetupAccessServer: modelProviderName(providerID))-const agent = computed(() =>- isOnboardingAgent(providerID)- ? ACP_AGENTS.find((candidate) => `acp:${candidate.id}` === providerID)- : undefined-)+const agent = computed(() => (isOnboardingAgent(providerID) ? acpAgentFor(providerID) : undefined))const provider = computed(() => AI_PROVIDERS.find((candidate) => candidate.id === providerID))const server = computed(() => providerID === ONBOARDING_SERVER_PROVIDER)const signInProvider = computed(() => (isOnboardingSignInProvider(providerID) ? providerID : null))diff --git a/src/components/settings/provider-select/ProviderSelect.vue b/src/components/settings/provider-select/ProviderSelect.vueindex 89073617a..850a3e09e 100644--- a/src/components/settings/provider-select/ProviderSelect.vue+++ b/src/components/settings/provider-select/ProviderSelect.vue@@ -4,6 +4,7 @@ import { computed, onMounted, ref } from 'vue'import {ACP_AGENTS,+ acpAgentFor,AI_PROVIDERS,AUTOMATION_HTTP_PORT,IS_TAURI,@@ -69,10 +70,8 @@ const providerDef = computed(const displayName = computed(() => {if (providerID.value === 'harness:pi') return 'Pi'- if (providerID.value.startsWith('acp:')) {- const agentId = providerID.value.replace('acp:', '')- return ACP_AGENTS.find((agent) => agent.id === agentId)?.name ?? providerID.value- }+ if (providerID.value.startsWith('acp:'))+ return acpAgentFor(providerID.value)?.name ?? providerID.valuereturn providerDef.value.name})