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 };
|