Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import {
EDITOR_TARGETS,
type EditorId,
type EditorMcpTarget,
type McpInstallOptions,
} from '../commands/editors.ts';
import { type McpDeclineReason, writeEditorMcpConfig } from '../commands/init.ts';
import { writeProjectSkill, writeProjectSkillToHub } from './write-project-skill.ts';
type IntegrationId = 'mcp-config' | 'project-skill';
export interface IntegrationWriteOutcome {
readonly integration: IntegrationId;
readonly editorId: EditorId;
readonly action:
| 'written'
| 'overwritten'
| 'skipped-unsupported'
| 'skipped-prerequisite'
| 'declined'
| 'failed';
readonly path?: string;
readonly error?: string;
readonly reason?: McpDeclineReason;
}
export interface ProjectIntegrationWriter {
readonly id: IntegrationId;
write(
target: EditorMcpTarget,
projectDir: string,
options: McpInstallOptions,
): IntegrationWriteOutcome;
}
export const mcpConfigWriter: ProjectIntegrationWriter = {
id: 'mcp-config',
write(target, projectDir, options) {
const projectPath = target.projectConfigPath?.(projectDir);
if (!projectPath) {
return {
integration: 'mcp-config',
editorId: target.id,
action: 'skipped-unsupported',
};
}
try {
const result = writeEditorMcpConfig(target, projectDir, options, undefined, projectPath);
if (result.action === 'written' || result.action === 'overwritten') {
return {
integration: 'mcp-config',
editorId: target.id,
action: result.action,
path: result.configPath,
};
}
if (result.action === 'failed') {
return {
integration: 'mcp-config',
editorId: target.id,
action: 'failed',
path: result.configPath,
error: result.error ?? 'unknown failure',
};
}
if (result.action === 'declined') {
return {
integration: 'mcp-config',
editorId: target.id,
action: 'declined',
path: result.configPath,
...(result.declineReason !== undefined ? { reason: result.declineReason } : {}),
};
}
return {
integration: 'mcp-config',
editorId: target.id,
action: 'failed',
path: result.configPath,
error: `unexpected project-scope action: ${result.action}`,
};
} catch (err) {
return {
integration: 'mcp-config',
editorId: target.id,
action: 'failed',
path: projectPath,
error: err instanceof Error ? err.message : String(err),
};
}
},
};
export const projectSkillWriter: ProjectIntegrationWriter = {
id: 'project-skill',
write(target, projectDir, options) {
try {
const result = writeProjectSkill(target, projectDir, { home: options.home });
return {
integration: 'project-skill',
editorId: target.id,
action: result.action,
...(result.path ? { path: result.path } : {}),
...(result.error ? { error: result.error } : {}),
};
} catch (err) {
return {
integration: 'project-skill',
editorId: target.id,
action: 'failed',
error: err instanceof Error ? err.message : String(err),
};
}
},
};
export const DEFAULT_PROJECT_INTEGRATIONS = [mcpConfigWriter, projectSkillWriter] as const;
export function applyProjectIntegrations(
projectDir: string,
editorIds: readonly EditorId[],
options: McpInstallOptions = {},
writers: readonly ProjectIntegrationWriter[] = DEFAULT_PROJECT_INTEGRATIONS,
): IntegrationWriteOutcome[] {
const outcomes: IntegrationWriteOutcome[] = [];
for (const editorId of editorIds) {
const target = EDITOR_TARGETS[editorId];
for (const writer of writers) {
outcomes.push(writer.write(target, projectDir, options));
}
}
const hub = writeProjectSkillToHub(projectDir, editorIds);
if (hub) {
outcomes.push({
integration: 'project-skill',
editorId: hub.editorId,
action: hub.action,
...(hub.path ? { path: hub.path } : {}),
...(hub.error ? { error: hub.error } : {}),
});
}
return outcomes;
}