File size: 5,660 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
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';

const args = process.argv.slice(2);
const realCli = process.env.CODEXMOBILE_REAL_LARK_CLI || 'lark-cli';
const stateDir = process.env.CODEXMOBILE_LARK_GUARD_STATE_DIR || process.cwd();
const turnId = process.env.CODEXMOBILE_TURN_ID || process.env.CODEXMOBILE_SESSION_ID || 'unknown-turn';

function argValue(flag) {
  const index = args.indexOf(flag);
  if (index < 0 || index + 1 >= args.length) {
    return '';
  }
  return String(args[index + 1] || '').trim();
}

function isSlidesCreate() {
  return args[0] === 'slides' && args[1] === '+create';
}

function stateKey() {
  const title = argValue('--title') || '(untitled)';
  return `${turnId}:${title.toLowerCase()}`;
}

function redacted(value) {
  return String(value || '')
    .replace(/"appSecret"\s*:\s*"[^"]+"/gi, '"appSecret":"****"')
    .replace(/"access[_-]?token"\s*:\s*"[^"]+"/gi, '"accessToken":"****"')
    .replace(/"refresh[_-]?token"\s*:\s*"[^"]+"/gi, '"refreshToken":"****"')
    .replace(/\b(u|ur|t)-[A-Za-z0-9._-]{20,}\b/g, '$1-[hidden]')
    .replace(/sk-[A-Za-z0-9._-]+/g, 'sk-[hidden]');
}

async function readState(filePath) {
  try {
    const raw = await fs.readFile(filePath, 'utf8');
    const parsed = JSON.parse(raw);
    return parsed && typeof parsed === 'object' ? parsed : {};
  } catch {
    return {};
  }
}

async function writeState(filePath, state) {
  await fs.mkdir(path.dirname(filePath), { recursive: true });
  await fs.writeFile(filePath, JSON.stringify(state, null, 2), 'utf8');
}

function isPreCreateValidationFailure(text) {
  return /--slides invalid json|invalid json, must be an array|unknown flag|required flag|auth|permission|forbidden|unauthorized|no user logged in|network|econn|timeout/i.test(text);
}

async function maybeBlockDuplicate(stateFile) {
  if (!isSlidesCreate()) {
    return false;
  }
  const state = await readState(stateFile);
  const key = stateKey();
  const existing = state[key];
  if (!existing?.attemptedAt) {
    return false;
  }

  const title = argValue('--title') || '(untitled)';
  const hint = {
    ok: false,
    error: {
      type: 'codexmobile_duplicate_slides_create_blocked',
      message: `This turn already ran slides +create for "${title}". Do not create another PPT. Reuse the previous xml_presentation_id and repair/append pages on the same presentation.`,
      previous: {
        title,
        attemptedAt: existing.attemptedAt,
        xmlPresentationId: existing.xmlPresentationId || '',
        output: existing.output || ''
      },
      nextStep: 'Run lark-cli slides xml_presentations get, then use xml_presentation.slide.create or +replace-slide on the existing PPT.'
    }
  };
  process.stderr.write(`${JSON.stringify(hint, null, 2)}\n`);
  process.exitCode = 24;
  return true;
}

function extractPresentationId(text) {
  const value = String(text || '');
  const jsonMatch = value.match(/"xml_presentation_id"\s*:\s*"([^"]+)"/i);
  if (jsonMatch) {
    return jsonMatch[1];
  }
  const urlMatch = value.match(/\/slides\/([A-Za-z0-9_-]+)/i);
  return urlMatch?.[1] || '';
}

async function recordCreateAttempt(stateFile, stdout, stderr, code) {
  if (!isSlidesCreate()) {
    return;
  }
  const combined = `${stdout}\n${stderr}`;
  const xmlPresentationId = extractPresentationId(combined);
  if (code !== 0 && !xmlPresentationId && isPreCreateValidationFailure(combined)) {
    return;
  }
  const state = await readState(stateFile);
  const key = stateKey();
  state[key] = {
    attemptedAt: new Date().toISOString(),
    title: argValue('--title') || '(untitled)',
    xmlPresentationId,
    output: redacted(combined).slice(-1600)
  };
  await writeState(stateFile, state);
}

function larkCliSpawnCommand() {
  if (process.platform === 'win32' && /\.cmd$|\.bat$/i.test(realCli)) {
    const commandLine = ['call', windowsCmdQuote(realCli), ...args.map(windowsCmdQuote)].join(' ');
    return {
      command: process.env.ComSpec || 'cmd.exe',
      args: ['/d', '/c', commandLine],
      windowsVerbatimArguments: true
    };
  }
  return {
    command: realCli,
    args,
    windowsVerbatimArguments: false
  };
}

function windowsCmdQuote(value) {
  return `"${String(value || '').replace(/"/g, '""').replace(/\r?\n/g, ' ')}"`;
}

async function main() {
  const stateFile = path.join(stateDir, 'lark-slides-create-guard.json');
  if (await maybeBlockDuplicate(stateFile)) {
    return;
  }

  const cli = larkCliSpawnCommand();
  const child = spawn(cli.command, cli.args, {
    cwd: process.cwd(),
    env: {
      ...process.env,
      CODEXMOBILE_LARK_GUARD_CHILD: '1'
    },
    shell: false,
    windowsHide: true,
    windowsVerbatimArguments: cli.windowsVerbatimArguments
  });

  let stdout = '';
  let stderr = '';
  child.stdout?.on('data', (chunk) => {
    const text = chunk.toString('utf8');
    stdout += text;
  });
  child.stderr?.on('data', (chunk) => {
    const text = chunk.toString('utf8');
    stderr += text;
  });
  child.on('error', (error) => {
    process.stderr.write(`${error.message}\n`);
    process.exitCode = 1;
  });
  child.on('close', async (code) => {
    if (stdout) {
      process.stdout.write(stdout);
    }
    if (stderr) {
      process.stderr.write(stderr);
    }
    await recordCreateAttempt(stateFile, stdout, stderr, code || 0).catch((error) => {
      process.stderr.write(`[codexmobile-lark-guard] failed to record state: ${error.message}\n`);
    });
    process.exitCode = code || 0;
  });
}

main().catch((error) => {
  process.stderr.write(`${error.message}\n`);
  process.exitCode = 1;
});