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
16 changes: 12 additions & 4 deletions src/agent/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export function detectInstalledAgents(): AgentType[] {
.map(([type]) => type as AgentType)
}

/** Detect the active agent from environment variables. */
export function detectEnvAgent(): AgentType | null {
for (const [type, target] of Object.entries(agents)) {
if (target.detectEnv())
return type as AgentType
}
return null
}

/**
* Detect the target agent (where skills are installed) from env vars and cwd.
* This is NOT the generator LLM β€” it determines the skills directory.
Expand All @@ -27,10 +36,9 @@ export function detectInstalledAgents(): AgentType[] {
* rather than silently picking the first match.
*/
export function detectTargetAgent(): AgentType | null {
for (const [type, target] of Object.entries(agents)) {
if (target.detectEnv())
return type as AgentType
}
const envAgent = detectEnvAgent()
if (envAgent)
return envAgent

const cwd = process.cwd()
const projectMatches: AgentType[] = []
Expand Down
2 changes: 1 addition & 1 deletion src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export {
// Import detection
export { detectImportedPackages } from './detect-imports.ts'
// Detection
export { detectInstalledAgents, detectProjectAgents, detectTargetAgent, getAgentVersion } from './detect.ts'
export { detectEnvAgent, detectInstalledAgents, detectProjectAgents, detectTargetAgent, getAgentVersion } from './detect.ts'
// Installation
export { computeSkillDirName, installSkillForAgents, linkSkillToAgents, sanitizeName, unlinkSkillFromAgents } from './install.ts'

Expand Down
14 changes: 8 additions & 6 deletions src/cli/agent-prompt.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import type { AgentType } from '../agent/index.ts'
import { styleText } from 'node:util'
import * as p from '@clack/prompts'
import { agents, detectInstalledAgents, detectProjectAgents, detectTargetAgent } from '../agent/index.ts'
import { agents, detectEnvAgent, detectInstalledAgents, detectProjectAgents } from '../agent/index.ts'
import { readConfig, updateConfig } from '../core/config.ts'
import { isInteractive } from './env.ts'

function detectProjectAgent(): AgentType | null {
const projectMatches = detectProjectAgents()
return projectMatches.length === 1 ? projectMatches[0]! : null
}

export function resolveAgent(agentFlag?: string): AgentType | 'none' | null {
if (process.env.SKILLD_NO_AGENT)
return null
return (agentFlag as AgentType | undefined)
?? detectTargetAgent()
?? detectEnvAgent()
?? (readConfig().agent as AgentType | undefined)
?? detectProjectAgent()
?? null
}

Expand All @@ -35,10 +41,6 @@ export function autoResolveAgent(agentFlag?: string): AgentType | null {
if (process.env.SKILLD_NO_AGENT)
return null

const projectMatches = detectProjectAgents()
if (projectMatches.length === 1)
return projectMatches[0]!

const installed = detectInstalledAgents()
if (installed.length === 1)
return installed[0]!
Expand Down
49 changes: 49 additions & 0 deletions test/unit/agent-prompt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { autoResolveAgent } from '../../src/cli/agent-prompt.ts'

const detection = vi.hoisted(() => ({
configAgent: undefined as string | undefined,
envAgent: null as string | null,
installedAgents: [] as string[],
projectAgents: [] as string[],
}))

vi.mock('../../src/agent/index.ts', () => ({
agents: {},
detectEnvAgent: () => detection.envAgent,
detectInstalledAgents: () => detection.installedAgents,
detectProjectAgents: () => detection.projectAgents,
detectTargetAgent: () => detection.envAgent
?? (detection.projectAgents.length === 1 ? detection.projectAgents[0] : null),
}))

vi.mock('../../src/core/config.ts', () => ({
readConfig: () => ({ agent: detection.configAgent }),
updateConfig: vi.fn(),
}))

describe('autoResolveAgent', () => {
const originalNoAgent = process.env.SKILLD_NO_AGENT

beforeEach(() => {
detection.configAgent = undefined
detection.envAgent = null
detection.installedAgents = []
detection.projectAgents = []
delete process.env.SKILLD_NO_AGENT
})

afterEach(() => {
if (originalNoAgent === undefined)
delete process.env.SKILLD_NO_AGENT
else
process.env.SKILLD_NO_AGENT = originalNoAgent
})

it('prefers the saved config over a single project marker', () => {
detection.configAgent = 'claude-code'
detection.projectAgents = ['cursor']

expect(autoResolveAgent()).toBe('claude-code')
})
})