import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { exportDsh } from "../export/dsh.js"; import { collectSessionTranscripts } from "../export/sessions.js"; import { importCodexThreads } from "./codex-threads.js"; export type ImportTarget = "codex" | "claude" | "zcode" | "workbuddy" | "mimo"; interface TargetSpec { label: string; instructionsFile: string; skillsDir?: string; } const HOME = homedir(); const TARGETS: Record = { codex: { label: "Codex", instructionsFile: join(HOME, ".codex", "AGENTS.md"), skillsDir: join(HOME, ".codex", "skills") }, claude: { label: "Claude Code", instructionsFile: join(HOME, ".claude", "CLAUDE.md"), skillsDir: join(HOME, ".claude", "skills") }, zcode: { label: "ZCode", instructionsFile: join(HOME, ".zcode", "AGENTS.md"), skillsDir: join(HOME, ".zcode", "skills") }, workbuddy: { label: "WorkBuddy", instructionsFile: join(HOME, ".workbuddy", "AGENTS.md"), skillsDir: join(HOME, ".workbuddy", "skills") }, mimo: { label: "MiMo Desktop", instructionsFile: join(HOME, ".agents", "AGENTS.md"), skillsDir: join(HOME, ".agents", "skills") }, }; const MARK_START = ""; const MARK_END = ""; export interface DshImportOptions { target: ImportTarget; dataDir?: string; withAttachments?: boolean; } export interface DshImportResult { target: ImportTarget; dataDir: string; instructionsFile: string; skillFile?: string; personas: number; conversations: number; materialsBytes: number; /** Codex only: threads written into the app sidebar. */ threads?: { imported: number; failures: number; codexWasRunning: boolean }; } function sectionFor(dataDir: string): string { return [ MARK_START, "## DSH 导入资料(AgentLens)", "", "以下资料从 DeepSeek Harness 一键导入,回答前先阅读后再作答:", "", `- 人设档案:\`${join(dataDir, "persona.md")}\``, `- 模型配置:\`${join(dataDir, "models.md")}\``, `- 文件资料:\`${join(dataDir, "files", "workspace")}\``, `- 记忆快照:\`${join(dataDir, "files", "memory")}\``, `- 对话记录:\`${join(dataDir, "conversations")}\``, "", "使用规则:涉及角色设定时先读 persona.md;模型偏好见 models.md;需要资料时查 files/;需要历史上下文时检索 conversations/。", MARK_END, ].join("\n"); } export function mergeInstructions(file: string, dataDir: string): void { let existing = ""; if (existsSync(file)) existing = readFileSync(file, "utf8"); const section = sectionFor(dataDir); const startIdx = existing.indexOf(MARK_START); const endIdx = existing.indexOf(MARK_END); let merged: string; if (startIdx !== -1 && endIdx !== -1) { merged = existing.slice(0, startIdx) + section + existing.slice(endIdx + MARK_END.length); } else { merged = existing.trimEnd() + "\n\n" + section + "\n"; } mkdirSync(dirname(file), { recursive: true }); writeFileSync(file, merged, "utf8"); } function writeSkill(skillsDir: string | undefined, dataDir: string): string | undefined { if (!skillsDir) return undefined; const dir = join(skillsDir, "dsh-import"); mkdirSync(dir, { recursive: true }); const file = join(dir, "SKILL.md"); const content = [ "---", "name: dsh-import", "description: DSH 导入资料(人物设定、工作区文件、历史对话)。当用户提到你的角色设定、以前做过什么、之前的工作文件、或需要参考历史对话时使用。", "---", "", "# DSH 导入资料", "", `从 DeepSeek Harness 导入的完整资料,位于 \`${dataDir}\`:`, "", "| 内容 | 路径 | 什么时候用 |", "|---|---|---|", `| 人设档案 | \`${join(dataDir, "persona.md")}\` | 用户问"你是谁""按我的设定来"时 |`, `| 模型配置 | \`${join(dataDir, "models.md")}\` | 用户问用的什么模型时 |`, `| 文件资料 | \`${join(dataDir, "files", "workspace")}\` | 需要之前的文件资料时 |`, `| 记忆快照 | \`${join(dataDir, "files", "memory")}\` | 需要历史决策/结论时 |`, `| 对话记录 | \`${join(dataDir, "conversations")}\` | 需要之前聊过什么时 |`, "", "阅读顺序:先 persona.md 了解人设,models.md 看模型,再按需检索 files / conversations。", "", ].join("\n"); writeFileSync(file, content, "utf8"); return file; } export function importDsh(options: DshImportOptions): DshImportResult { const spec = TARGETS[options.target]; if (!spec) throw new Error(`不支持的目标: ${options.target}`); const dataDir = options.dataDir && options.dataDir.length > 0 ? options.dataDir : join(HOME, `${options.target}-import`); const result = exportDsh({ outDir: dataDir, withAttachments: options.withAttachments === true, withMaterials: true, withConversations: true, }); mergeInstructions(spec.instructionsFile, dataDir); const skillFile = writeSkill(spec.skillsDir, dataDir); // Codex additionally gets real thread rows so the conversations show up in // the app's sidebar, not just as files. let threads; if (options.target === "codex") { try { const { transcripts } = collectSessionTranscripts(); threads = importCodexThreads(transcripts); } catch { threads = { imported: 0, failures: 0, codexWasRunning: false }; } } return { target: options.target, dataDir, instructionsFile: spec.instructionsFile, skillFile, personas: result.personas, conversations: result.conversationLines, materialsBytes: result.materialsBytes, threads, }; }