File size: 8,400 Bytes
90f0300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { spawn } from 'node:child_process';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { buildFeishuSkillInstruction } from './feishu-skills.js';
import { AUTH_DOMAINS } from './lark-cli-definitions.js';
import { CODEXMOBILE_LARK_AGENT_DIR, CODEXMOBILE_LARK_GUARD_DIR, CODEXMOBILE_STATE_DIR, ROOT_DIR } from './runtime-paths.js';
import {
  LARK_CLI,
  larkCliEnvironment,
  larkCliSpawnOptions,
  larkError,
  prependPathEntry,
  redacted,
  resolveLarkCliCommand,
  runLarkCli
} from './lark-cli-runner.js';
import {
  envValue,
  getLarkDocsStatusState,
  larkCliVersion,
  larkConfigStatus,
  resetLarkDocsStatusCache
} from './lark-cli-status.js';

let authRun = null;
let agentConfigPreparedAt = 0;

export { larkCliEnvironment };

async function ensureAgentLarkConfigDir() {
  const sourceRoot = path.join(os.homedir(), '.lark-cli');
  const sourceProfile = path.join(sourceRoot, 'openclaw');
  const targetRoot = CODEXMOBILE_LARK_AGENT_DIR;
  const targetProfile = path.join(targetRoot, 'openclaw');
  const now = Date.now();

  if (now - agentConfigPreparedAt < 5000) {
    return targetRoot;
  }

  await fs.mkdir(targetProfile, { recursive: true });
  await fs.cp(sourceProfile, targetProfile, {
    recursive: true,
    force: true,
    filter: (source) => {
      const name = path.basename(source).toLowerCase();
      return !['locks', 'cache', 'logs'].includes(name);
    }
  });
  await Promise.all([
    fs.mkdir(path.join(targetProfile, 'locks'), { recursive: true }),
    fs.mkdir(path.join(targetProfile, 'cache'), { recursive: true }),
    fs.mkdir(path.join(targetProfile, 'logs'), { recursive: true })
  ]);
  agentConfigPreparedAt = now;
  return targetRoot;
}

async function ensureLarkCliGuardDir() {
  const guardDir = CODEXMOBILE_LARK_GUARD_DIR;
  const guardScript = path.join(ROOT_DIR, 'scripts', 'lark-cli-guard.mjs');
  const cmdPath = path.join(guardDir, 'lark-cli.cmd');
  const nodePath = process.execPath;
  await fs.mkdir(guardDir, { recursive: true });
  await fs.writeFile(
    cmdPath,
    [
      '@echo off',
      `"${nodePath}" "${guardScript}" %*`
    ].join('\r\n'),
    'utf8'
  );
  return guardDir;
}

function publicPendingAuth() {
  if (!authRun) {
    return null;
  }
  return {
    verificationUrl: authRun.verificationUrl,
    userCode: authRun.userCode,
    expiresAt: authRun.expiresAt,
    status: authRun.status,
    error: authRun.error || ''
  };
}

export async function getLarkDocsStatus(options = {}) {
  return getLarkDocsStatusState(options, publicPendingAuth);
}

async function ensureLarkConfigured() {
  const config = await larkConfigStatus();
  if (config.configReady) {
    return;
  }
  const appId = envValue('LARK_APP_ID', 'CODEXMOBILE_FEISHU_APP_ID');
  const appSecret = envValue('LARK_APP_SECRET', 'CODEXMOBILE_FEISHU_APP_SECRET');
  if (!appId || !appSecret) {
    throw larkError('缺少飞书 App ID 或 App Secret,请先配置 CODEXMOBILE_FEISHU_APP_ID / CODEXMOBILE_FEISHU_APP_SECRET。', {
      statusCode: 400
    });
  }

  const init = await runLarkCli(
    ['config', 'init', '--app-id', appId, '--app-secret-stdin', '--brand', 'feishu'],
    { input: `${appSecret}\n`, timeoutMs: 30000 }
  );
  if (!init.ok) {
    throw larkError(init.error || 'lark-cli 配置失败', { statusCode: 502 });
  }
}

async function setDefaultAsUser() {
  const result = await runLarkCli(['config', 'default-as', 'user'], { timeoutMs: 10000 });
  if (!result.ok) {
    console.warn('[lark-cli] failed to set default identity:', result.error || result.stderr || result.stdout);
  }
}

function extractUserCode(verificationUrl) {
  try {
    return new URL(verificationUrl).searchParams.get('user_code') || '';
  } catch {
    return '';
  }
}

async function startDevicePoll(deviceCode) {
  let child = null;
  try {
    const command = await resolveLarkCliCommand();
    const spawnOptions = larkCliSpawnOptions(command, ['auth', 'login', '--device-code', deviceCode]);
    child = spawn(spawnOptions.command, spawnOptions.args, {
      env: larkCliEnvironment(),
      windowsHide: true,
      windowsVerbatimArguments: spawnOptions.windowsVerbatimArguments
    });
  } catch (error) {
    authRun.status = 'failed';
    authRun.error = redacted(error.message);
    return;
  }
  authRun.process = child;
  authRun.status = 'polling';

  let stdout = '';
  let stderr = '';
  const finish = async (status, error = '') => {
    if (!authRun) {
      return;
    }
    authRun.status = status;
    authRun.error = error;
    authRun.process = null;
    resetLarkDocsStatusCache();
    if (status === 'connected') {
      await setDefaultAsUser();
    }
  };

  child.stdout?.on('data', (chunk) => {
    stdout += chunk.toString('utf8');
  });
  child.stderr?.on('data', (chunk) => {
    stderr += chunk.toString('utf8');
  });
  child.on('error', (error) => {
    finish('failed', redacted(error.message));
  });
  child.on('close', (code) => {
    if (code === 0) {
      finish('connected');
      return;
    }
    finish('failed', redacted(stderr || stdout || `lark-cli auth login exited with code ${code}`));
  });
}

export async function startLarkCliAuth() {
  const cli = await larkCliVersion();
  if (!cli.installed) {
    throw larkError('未安装 lark-cli,请先安装 @larksuite/cli。', { statusCode: 400 });
  }

  await ensureLarkConfigured();
  await setDefaultAsUser();

  if (authRun?.status === 'polling' && Date.now() < authRun.expiresAt) {
    return {
      verificationUrl: authRun.verificationUrl,
      userCode: authRun.userCode,
      expiresAt: authRun.expiresAt,
      status: authRun.status
    };
  }

  const args = ['auth', 'login', '--recommend', '--no-wait', '--json'];
  for (const domain of AUTH_DOMAINS) {
    args.push('--domain', domain);
  }
  const result = await runLarkCli(args, { timeoutMs: 30000 });
  if (!result.ok || !result.json?.device_code || !result.json?.verification_url) {
    throw larkError(result.error || '获取飞书授权地址失败', { statusCode: 502 });
  }

  authRun = {
    deviceCode: result.json.device_code,
    verificationUrl: result.json.verification_url,
    userCode: extractUserCode(result.json.verification_url),
    expiresAt: Date.now() + Math.max(0, Number(result.json.expires_in || 600)) * 1000,
    status: 'pending',
    error: '',
    process: null
  };
  await startDevicePoll(authRun.deviceCode);
  resetLarkDocsStatusCache();

  return {
    verificationUrl: authRun.verificationUrl,
    userCode: authRun.userCode,
    expiresAt: authRun.expiresAt,
    status: authRun.status
  };
}

export async function logoutLarkCli() {
  if (authRun?.process) {
    authRun.process.kill();
  }
  authRun = null;
  const result = await runLarkCli(['auth', 'logout'], { timeoutMs: 15000 });
  resetLarkDocsStatusCache();
  if (!result.ok && !/no logged-in users/i.test(result.stdout || result.stderr || result.error || '')) {
    throw larkError(result.error || '断开飞书授权失败', { statusCode: 502 });
  }
  return true;
}

export async function buildCodexLarkCliContext(message = '') {
  const status = await getLarkDocsStatus({ authenticated: true, force: false }).catch(() => null);
  const enabled = Boolean(status?.codexEnabled);
  const requestedInstruction = await buildFeishuSkillInstruction(message);
  const instruction = enabled
    ? requestedInstruction
    : requestedInstruction
      ? [
          'CodexMobile Feishu/Lark was requested, but the integration is not currently authorized.',
          'Do not run lark-cli commands. Reply in concise Chinese that Feishu authorization has expired or is unavailable, and ask the user to open the top-right Docs panel and reconnect Feishu.'
        ].join('\n')
      : '';
  const env = larkCliEnvironment();
  if (enabled) {
    const configRoot = await ensureAgentLarkConfigDir();
    const realCli = await resolveLarkCliCommand();
    env.LARKSUITE_CLI_CONFIG_DIR = configRoot;
    env.LARKSUITE_CLI_LOG_DIR = path.join(configRoot, 'openclaw', 'logs');
    env.LARKSUITE_CLI_NO_UPDATE_NOTIFIER = '1';
    if (realCli && realCli !== LARK_CLI) {
      const guardDir = await ensureLarkCliGuardDir();
      env.CODEXMOBILE_REAL_LARK_CLI = realCli;
      env.CODEXMOBILE_LARK_GUARD_STATE_DIR = CODEXMOBILE_STATE_DIR;
      prependPathEntry(env, guardDir);
    }
  }
  return {
    enabled,
    env,
    instruction
  };
}