MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / loadAgentsMdForRoots

Function loadAgentsMdForRoots

packages/agent-core/src/profile/context.ts:58–115  ·  view source on GitHub ↗
(
  kaos: Kaos,
  brandHome: string | undefined,
  workDirs: readonly string[],
)

Source from the content-addressed store, hash-verified

56}
57
58async function loadAgentsMdForRoots(
59 kaos: Kaos,
60 brandHome: string | undefined,
61 workDirs: readonly string[],
62): Promise<LoadedAgentsMd> {
63 const discovered: AgentFile[] = [];
64 const seen = new Set<string>();
65
66 const collect = async (path: string): Promise<boolean> => {
67 const file = await readAgentFile(kaos, path);
68 if (file === undefined) return false;
69 const key = kaos.normpath(file.path);
70 if (seen.has(key)) return false;
71 seen.add(key);
72 discovered.push(file);
73 return true;
74 };
75
76 // User-level files come first so any project-level AGENTS.md overrides them.
77 // The brand dir follows KIMI_CODE_HOME (default ~/.kimi-code); the generic
78 // .agents dir stays under the real OS home so it can be shared across tools.
79 const realHome = kaos.gethome();
80 const brandDir = brandHome ?? join(realHome, '.kimi-code');
81 await collect(join(brandDir, 'AGENTS.md'));
82
83 // Generic user-level dir (.agents) matches skill discovery.
84 const genericDirs = [join(realHome, '.agents')];
85 const genericFiles = genericDirs.flatMap((dir) =>
86 ['AGENTS.md', 'agents.md'].map((name) => join(dir, name)),
87 );
88 for (const file of genericFiles) {
89 if (await collect(file)) break;
90 }
91
92 for (const workDir of workDirs) {
93 const rootKaos = kaos.withCwd(workDir);
94 const rootWorkDir = rootKaos.getcwd();
95 const projectRoot = await findProjectRoot(rootKaos, rootWorkDir);
96 const dirs = dirsRootToLeaf(rootKaos, rootWorkDir, projectRoot);
97
98 for (const dir of dirs) {
99 await collect(join(dir, '.kimi-code', 'AGENTS.md'));
100 for (const fileName of ['AGENTS.md', 'agents.md']) {
101 if (await collect(join(dir, fileName))) break;
102 }
103 }
104 }
105
106 const content = renderAgentFiles(discovered);
107 const totalBytes = byteLength(content);
108 const warning =
109 totalBytes > AGENTS_MD_RECOMMENDED_MAX_BYTES
110 ? `AGENTS.md total ${formatKB(totalBytes)} KB exceeds the recommended ` +
111 `${formatKB(AGENTS_MD_RECOMMENDED_MAX_BYTES)} KB. Large instruction files ` +
112 `increase cost and may impact performance; consider trimming.`
113 : undefined;
114 return { content, warning };
115}

Callers 2

loadAgentsMdFunction · 0.85

Calls 9

dirsRootToLeafFunction · 0.85
renderAgentFilesFunction · 0.85
byteLengthFunction · 0.85
formatKBFunction · 0.85
collectFunction · 0.70
findProjectRootFunction · 0.70
gethomeMethod · 0.65
withCwdMethod · 0.65
getcwdMethod · 0.65

Tested by

no test coverage detected