File size: 5,796 Bytes
12880e4
 
 
 
8db9296
 
12880e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db9296
 
12880e4
 
 
 
 
 
 
 
 
4ce05b9
 
 
 
12880e4
 
4ce05b9
12880e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ce05b9
 
 
 
12880e4
 
4ce05b9
12880e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db9296
 
 
 
 
 
 
 
 
 
 
 
12880e4
 
 
 
 
 
 
 
8db9296
12880e4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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<ImportTarget, TargetSpec> = {
  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 = "<!-- agentlens-import:start -->";
const MARK_END = "<!-- agentlens-import: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,
  };
}