File size: 7,182 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';

const require = createRequire(import.meta.url);
const packageRoot = path.resolve(import.meta.dirname, '..');
const tempDir = path.join(packageRoot, '.tmp-api-extractor');
const dtsRoot = path.join(tempDir, 'dts');
const providerClientShimPath = path.join(dtsRoot, 'provider-clients.d.ts');
const tscBinPath = packageBinPath('typescript', 'bin/tsc');
const apiExtractorBinPath = packageBinPath('@microsoft/api-extractor', 'bin/api-extractor');

const packageDirs = new Set(['agent-core-v2', 'kaos', 'klient', 'kosong', 'node-sdk', 'oauth']);
const workspacePackages = new Map([
  ['@moonshot-ai/agent-core-v2', 'agent-core-v2'],
  ['@moonshot-ai/kaos', 'kaos'],
  ['@moonshot-ai/kimi-code-oauth', 'oauth'],
  ['@moonshot-ai/klient', 'klient'],
  ['@moonshot-ai/kosong', 'kosong'],
]);

try {
  await rm(tempDir, { recursive: true, force: true });
  await run('tsc', tscBinPath, ['-p', 'tsconfig.dts.json']);
  await writeProviderClientShim();
  await rewriteWorkspaceSpecifiers();
  await run('api-extractor', apiExtractorBinPath, ['run', '--local']);
} finally {
  await rm(tempDir, { recursive: true, force: true });
}

function packageBinPath(packageName, binPath) {
  return path.join(path.dirname(require.resolve(`${packageName}/package.json`)), binPath);
}

function run(command, binPath, args) {
  return new Promise((resolve, reject) => {
    const child = spawn(process.execPath, [binPath, ...args], {
      cwd: packageRoot,
      stdio: 'inherit',
    });

    child.once('error', reject);
    child.once('exit', (code, signal) => {
      if (code === 0) {
        resolve();
        return;
      }

      const detail = signal === null ? `exit code ${String(code)}` : `signal ${signal}`;
      reject(new Error(`${command} failed with ${detail}`));
    });
  });
}

async function writeProviderClientShim() {
  await mkdir(dtsRoot, { recursive: true });
  await writeFile(
    providerClientShimPath,
    [
      'export interface Anthropic {}',
      'export interface GoogleGenAI {}',
      'export interface OpenAI {}',
      'export namespace OpenAI {',
      '  export namespace Chat {',
      '    export type ChatCompletion = unknown;',
      '    export type ChatCompletionChunk = unknown;',
      '    export type ChatCompletionCreateParamsNonStreaming = unknown;',
      '  }',
      '}',
      '',
    ].join('\n'),
  );
}

async function rewriteWorkspaceSpecifiers() {
  const files = await findDtsFiles(dtsRoot);
  const emittedFiles = new Set(files.map((file) => path.resolve(file)));

  await Promise.all(
    files.map(async (file) => {
      const packageDir = packageDirForFile(file);
      if (packageDir === undefined) {
        return;
      }

      const text = await readFile(file, 'utf8');
      const providerClientSpecifier = relativeSpecifier(file, providerClientShimPath);
      const providerClientText = text
        .replaceAll(
          "import Anthropic from '@anthropic-ai/sdk';",
          `import { Anthropic } from '${providerClientSpecifier}';`,
        )
        .replaceAll(
          "import OpenAI from 'openai';",
          `import { OpenAI } from '${providerClientSpecifier}';`,
        )
        .replaceAll(
          "import type OpenAI from 'openai';",
          `import type { OpenAI } from '${providerClientSpecifier}';`,
        )
        .replaceAll(
          "import { GoogleGenAI as GenAIClient } from '@google/genai';",
          `import { GoogleGenAI as GenAIClient } from '${providerClientSpecifier}';`,
        );
      const updated = providerClientText.replaceAll(
        /(["'])(#\/[^"']+|@moonshot-ai\/(?:agent-core-v2|kaos|kimi-code-oauth|klient|kosong)(?:\/[^"']+)?)\1/g,
        (_match, quote, specifier) => {
          const resolved = resolveSpecifier({
            currentFile: file,
            emittedFiles,
            packageDir,
            specifier,
          });

          return `${quote}${relativeSpecifier(file, resolved)}${quote}`;
        },
      );

      if (updated !== text) {
        await writeFile(file, updated);
      }
    }),
  );
}

async function findDtsFiles(dir) {
  const entries = await readdir(dir, { withFileTypes: true });
  const files = await Promise.all(
    entries.map(async (entry) => {
      const entryPath = path.join(dir, entry.name);
      if (entry.isDirectory()) {
        return findDtsFiles(entryPath);
      }
      return entry.name.endsWith('.d.ts') ? [entryPath] : [];
    }),
  );

  return files.flat();
}

function packageDirForFile(file) {
  const parts = path.relative(dtsRoot, file).split(path.sep);
  const [packageDir, firstDir] = parts;

  if (packageDir === undefined || firstDir !== 'src' || !packageDirs.has(packageDir)) {
    return undefined;
  }

  return packageDir;
}

function resolveSpecifier({ currentFile, emittedFiles, packageDir, specifier }) {
  if (specifier.startsWith('#/')) {
    return resolvePackageSubpath({
      emittedFiles,
      srcRoot: srcRootForFile(currentFile, packageDir),
      subpath: specifier.slice(2),
      originalSpecifier: specifier,
    });
  }

  const workspacePackage = workspacePackageForSpecifier(specifier);
  if (workspacePackage === undefined) {
    throw new Error(`Unexpected workspace specifier in ${currentFile}: ${specifier}`);
  }

  return resolvePackageSubpath({
    emittedFiles,
    srcRoot: path.join(dtsRoot, workspacePackage.packageDir, 'src'),
    subpath: workspacePackage.subpath,
    originalSpecifier: specifier,
  });
}

function srcRootForFile(currentFile, packageDir) {
  const srcRoot = path.join(dtsRoot, packageDir, 'src');
  const humanRoot = path.join(srcRoot, 'human');
  const rel = path.relative(humanRoot, currentFile);
  return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel) ? humanRoot : srcRoot;
}

function workspacePackageForSpecifier(specifier) {
  for (const [packageName, packageDir] of workspacePackages) {
    if (specifier === packageName) {
      return { packageDir, subpath: 'index' };
    }

    const prefix = `${packageName}/`;
    if (specifier.startsWith(prefix)) {
      return { packageDir, subpath: specifier.slice(prefix.length) };
    }
  }

  return undefined;
}

function resolvePackageSubpath({ emittedFiles, srcRoot, subpath, originalSpecifier }) {
  const directFile = path.resolve(srcRoot, `${subpath}.d.ts`);
  if (emittedFiles.has(directFile) || existsSync(directFile)) {
    return directFile;
  }

  const indexFile = path.resolve(srcRoot, subpath, 'index.d.ts');
  if (emittedFiles.has(indexFile) || existsSync(indexFile)) {
    return indexFile;
  }

  throw new Error(`Unable to resolve ${originalSpecifier} in emitted declarations`);
}

function relativeSpecifier(fromFile, toFile) {
  const fromDir = path.dirname(fromFile);
  const withoutExtension = toFile.slice(0, -'.d.ts'.length);
  let relative = path.relative(fromDir, withoutExtension).replaceAll(path.sep, '/');

  if (!relative.startsWith('.')) {
    relative = `./${relative}`;
  }

  return relative;
}