File size: 4,021 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ์„ค๋ช… ๋ฌธ์„œ: docs/specs/openwork-cli-provider.md (v2.0.0)
// DLP ์‹คํŒจ ์œ„์น˜๋งŒ ๊ด€์ธกํ•œ๋‹ค. ์›๋ฌธยทpreviewยทํ•ด์‹œยท์‚ฌ์šฉ์ž ์ •์˜ ํ‚ค๋Š” ๋กœ๊ทธ์— ๋„ฃ์ง€ ์•Š๋Š”๋‹ค.
const scanner = require('../utils/secret-scanner');
const LIMIT = 32;
function findings(text, pointer) {
  if (typeof text !== 'string') return [];
  const result = scanner.scan(text), records = [];
  const category = (token, index) => /https?:$/.test(text.slice(Math.max(0, index - 6), index)) ? 'url'
    : token.startsWith('/') || /^[A-Za-z]:[\\/]/.test(token) ? 'path' : 'token';
  for (const finding of result.findings) {
    if (finding.type === 'high-entropy-token') {
      for (const match of text.matchAll(/[A-Za-z0-9+/_-]{24,}/g)) {
        if (scanner.shannonEntropy(match[0]) < 4) continue;
        records.push({ pointer, findingType: finding.type, length: match[0].length, category: category(match[0], match.index) });
        if (records.length >= LIMIT) return records;
      }
    } else {
      const signature = scanner.SIGNATURES.find(item => item.name === finding.type), match = signature?.re.exec(text);
      const token = match?.[1] || match?.[0];
      records.push({ pointer, findingType: finding.type, length: token ? token.length : null,
        category: token ? category(token, match.index) : 'token' });
    }
  }
  return records.slice(0, LIMIT);
}
function inspect(request, prompt, originalRequest = request) {
  if (scanner.scan(prompt).clean) return [];
  const result = [], add = (value, pointer) => {
    if (result.length < LIMIT) result.push(...findings(value, pointer).slice(0, LIMIT - result.length));
  };
  // ๊ฒฝ๋กœ๋Š” ์„œ๋ฒ„๊ฐ€ ์ •ํ•œ ๊ณ ์ • ๊ตฌ์กฐ์™€ ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. ์Šคํ‚ค๋งˆ ๋‚ด๋ถ€ ํ‚ค๋Š” ๊ณต๊ฐœํ•˜์ง€ ์•Š๋Š”๋‹ค.
  request.messages.forEach((message, index) => {
    const first = result.length;
    add(message.content, `/messages/${index}/content`);
    if (['system', 'developer'].includes(message.role) && result.length > first) {
      const original = originalRequest.messages[index]?.content;
      const tokens = new Set(String(message.content).match(/[A-Za-z0-9+/_-]{24,}/g) || []);
      const publicStaticIds = [['openwork-cloud_execute_capability', 'public_cloud_execute'], ['openwork-cloud_search_capabilities', 'public_cloud_search']]
        .filter(([token]) => tokens.has(token)).map(([, id]) => id);
      const sectionPresence = require('./cli-system-steering').inspectSteering(typeof original === 'string' ? original : '');
      for (let offset = first; offset < result.length; offset++) Object.assign(result[offset], { publicStaticIds, sectionPresence });
    }
    message.tool_calls?.forEach((call, callIndex) => {
      add(call.id, `/messages/${index}/tool_calls/${callIndex}/id`);
      add(call.function.name, `/messages/${index}/tool_calls/${callIndex}/function/name`);
      add(call.function.arguments, `/messages/${index}/tool_calls/${callIndex}/function/arguments`);
    });
    add(message.tool_call_id, `/messages/${index}/tool_call_id`);
  });
  request.tools.forEach((tool, index) => {
    add(tool.function.name, `/tools/${index}/function/name`);
    add(tool.function.description, `/tools/${index}/function/description`);
    add(JSON.stringify(tool.function.parameters), `/tools/${index}/function/parameters`);
  });
  add(JSON.stringify(request.pathAliases || []), '/pathAliases');
  // ์ง๋ ฌํ™” ๊ฒฝ๊ณ„/์ถœ๋ ฅ ๊ณ„์•ฝ ์ง€์นจ์—์„œ๋งŒ ์žฌํ˜„๋˜๋Š” ์‹คํŒจ๋„ ์‚ฌ์šฉ์ž ์›๋ฌธ ์—†์ด ๊ตฌ๋ถ„ํ•œ๋‹ค.
  if (!result.length) add(prompt, '/');
  return result;
}
function record(request, prompt, log = value => require('../utils/debug-log').writeDebugLog(value), originalRequest = request) {
  try {
    for (const detail of inspect(request, prompt, originalRequest)) {
      Promise.resolve(log('[OpenWork CLI DLP] ' + JSON.stringify({ stage: 'projection_prompt', ...detail }))).catch(() => {});
    }
  } catch { /* ์ง„๋‹จ ์‹คํŒจ๊ฐ€ ๊ธฐ์กด DLP ํŒ์ •์ด๋‚˜ ์ •์‚ฐ ํ๋ฆ„์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋Š”๋‹ค. */ }
}
module.exports = { inspect, record };