Skip to content

Commit 7f8bc0d

Browse files
committed
Load from ~/.agents as well!
1 parent 44b716b commit 7f8bc0d

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

npm-app/src/agents/agent-utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
3+
import * as os from 'os'
34

45
import { getProjectRoot } from '../project-files'
56

@@ -42,3 +43,10 @@ export function getAllTsFiles(dir: string): string[] {
4243
export function getAgentsDirectory(): string {
4344
return path.join(getProjectRoot(), ...agentTemplatesSubdir)
4445
}
46+
47+
/**
48+
* Get the user's home agents directory path
49+
*/
50+
export function getUserAgentsDirectory(): string {
51+
return path.join(os.homedir(), ...agentTemplatesSubdir)
52+
}

npm-app/src/agents/load-agents.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as fs from 'fs'
22

33
import { cyan, green } from 'picocolors'
44

5-
import { getAllTsFiles, getAgentsDirectory } from './agent-utils'
5+
import {
6+
getAllTsFiles,
7+
getAgentsDirectory,
8+
getUserAgentsDirectory,
9+
} from './agent-utils'
610

711
import type { CodebuffConfig } from '@codebuff/common/json-config/constants'
812

@@ -16,16 +20,24 @@ export async function loadLocalAgents({
1620
}): Promise<typeof loadedAgents> {
1721
loadedAgents = {}
1822

19-
const agentsDir = agentsPath ?? getAgentsDirectory()
23+
// Collect agents from both directories
24+
const agentsDirs = agentsPath
25+
? [agentsPath]
26+
: [getAgentsDirectory(), getUserAgentsDirectory()]
2027

21-
if (!fs.existsSync(agentsDir)) {
28+
const allTsFiles: string[] = []
29+
for (const dir of agentsDirs) {
30+
if (fs.existsSync(dir)) {
31+
allTsFiles.push(...getAllTsFiles(dir))
32+
}
33+
}
34+
35+
if (allTsFiles.length === 0) {
2236
return loadedAgents
2337
}
2438

2539
try {
26-
const tsFiles = getAllTsFiles(agentsDir)
27-
28-
for (const fullPath of tsFiles) {
40+
for (const fullPath of allTsFiles) {
2941
let agentDefinition: any
3042
let agentModule: any
3143
try {
@@ -111,14 +123,15 @@ export function displayLoadedAgents(codebuffConfig: CodebuffConfig) {
111123
const loadedAgentNames = Object.values(getLoadedAgentNames())
112124
// Calculate terminal width and format agents in columns
113125
const terminalWidth = process.stdout.columns || 80
114-
const columnWidth = Math.max(...loadedAgentNames.map(name => name.length)) + 2 // Column width based on longest name + padding
126+
const columnWidth =
127+
Math.max(...loadedAgentNames.map((name) => name.length)) + 2 // Column width based on longest name + padding
115128
const columnsPerRow = Math.max(1, Math.floor(terminalWidth / columnWidth))
116-
129+
117130
const formattedLines: string[] = []
118131
for (let i = 0; i < loadedAgentNames.length; i += columnsPerRow) {
119132
const rowAgents = loadedAgentNames.slice(i, i + columnsPerRow)
120133
const formattedRow = rowAgents
121-
.map(name => cyan(name.padEnd(columnWidth)))
134+
.map((name) => cyan(name.padEnd(columnWidth)))
122135
.join('')
123136
formattedLines.push(formattedRow)
124137
}

0 commit comments

Comments
 (0)