feat: extractFromLogs for AI response text, remove failed RPCs, summary fallback
This commit is contained in:
@@ -331,81 +331,38 @@ function activate(context) {
|
||||
if (isFirstPoll) {
|
||||
console.log(`Gravity Bridge: [LS] init ${key.substring(0, 8)} at step ${stepIdx} "${summary.substring(0, 30)}"`);
|
||||
}
|
||||
else if (stepIdx > 0) {
|
||||
// New conversation with AI response! Try to get actual text
|
||||
else if (stepIdx > 0 && summary) {
|
||||
// New conversation with AI response!
|
||||
console.log(`Gravity Bridge: [LS] NEW conversation ${key.substring(0, 8)} at step ${stepIdx} "${summary.substring(0, 40)}"`);
|
||||
// Try multiple methods to get actual AI response text
|
||||
const rpcAttempts = [
|
||||
{ m: 'LoadTrajectory', p: { trajectoryId: trajId } },
|
||||
{ m: 'LoadTrajectory', p: { googleAgentId: agentId } },
|
||||
{ m: 'GetCascadeTrajectory', p: { googleAgentId: agentId } },
|
||||
{ m: 'GetCascadeTrajectorySteps', p: { googleAgentId: agentId } },
|
||||
];
|
||||
let gotText = false;
|
||||
for (const a of rpcAttempts) {
|
||||
const res = await lsRPC(a.m, a.p);
|
||||
if (res && !res.code) {
|
||||
console.log(`Gravity Bridge: [LS] ✅ ${a.m}(${Object.keys(a.p)[0]}) → keys: ${Object.keys(res).join(', ')}`);
|
||||
console.log(`Gravity Bridge: [LS] sample: ${JSON.stringify(res).substring(0, 1000)}`);
|
||||
// Try to extract text
|
||||
const steps = res.steps || res.cortexSteps || [];
|
||||
if (Array.isArray(steps) && steps.length > 0) {
|
||||
extractAndRelaySteps(steps.slice(-3)); // last 3 steps
|
||||
gotText = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else {
|
||||
const err = res?.message || res?.code || 'empty';
|
||||
console.log(`Gravity Bridge: [LS] ❌ ${a.m}(${Object.keys(a.p)[0]}): ${typeof err === 'string' ? err.substring(0, 60) : err}`);
|
||||
}
|
||||
// Try to extract AI text from extensionLogs
|
||||
const aiText = extractFromLogs(parsed);
|
||||
if (aiText) {
|
||||
writeChatSnapshot(aiText);
|
||||
console.log(`Gravity Bridge: [LS] → relayed AI text (${aiText.length} chars) from logs`);
|
||||
}
|
||||
// Fallback: relay summary
|
||||
if (!gotText && summary) {
|
||||
else {
|
||||
writeChatSnapshot(`**${summary}**\n\n(새 대화, step ${stepIdx})`);
|
||||
lastStepIndex[key + '_summary'] = summary;
|
||||
console.log(`Gravity Bridge: [LS] → summary fallback to Discord`);
|
||||
}
|
||||
lastStepIndex[key + '_summary'] = summary;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (stepIdx > prev) {
|
||||
// New steps detected!
|
||||
// Existing conversation has new steps
|
||||
console.log(`Gravity Bridge: [LS] ${key.substring(0, 8)} steps: ${prev} → ${stepIdx} "${summary.substring(0, 40)}"`);
|
||||
// Try RPC to get full step text
|
||||
const attempts = [
|
||||
{ method: 'GetCascadeTrajectorySteps', params: { googleAgentId: agentId, trajectoryId: trajId, startStepIndex: prev } },
|
||||
{ method: 'GetCascadeTrajectory', params: { googleAgentId: agentId, trajectoryId: trajId } },
|
||||
];
|
||||
let stepsResult = null;
|
||||
for (const attempt of attempts) {
|
||||
const res = await lsRPC(attempt.method, attempt.params);
|
||||
if (res && !res.code && !res.message?.includes('not found')) {
|
||||
stepsResult = res;
|
||||
console.log(`Gravity Bridge: [LS] ✅ ${attempt.method} worked!`);
|
||||
console.log(`Gravity Bridge: [LS] keys: ${Object.keys(res).join(', ')}`);
|
||||
console.log(`Gravity Bridge: [LS] sample: ${JSON.stringify(res).substring(0, 800)}`);
|
||||
pollFailCount = 0;
|
||||
break;
|
||||
}
|
||||
// Try to extract AI text from extensionLogs
|
||||
const aiText = extractFromLogs(parsed);
|
||||
if (aiText) {
|
||||
writeChatSnapshot(aiText);
|
||||
console.log(`Gravity Bridge: [LS] → relayed AI text (${aiText.length} chars)`);
|
||||
}
|
||||
if (stepsResult) {
|
||||
const steps = stepsResult.steps || stepsResult.cortexSteps || stepsResult.cortex_steps || [];
|
||||
if (Array.isArray(steps) && steps.length > 0) {
|
||||
const newSteps = steps.slice(-(stepIdx - prev));
|
||||
extractAndRelaySteps(newSteps);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Fallback: relay summary text
|
||||
const summaryKey = key + '_summary';
|
||||
if (summary && summary !== lastStepIndex[summaryKey]) {
|
||||
writeChatSnapshot(`[AI 응답 감지]\n\n**${summary}**\n\n(step ${prev} → ${stepIdx})`);
|
||||
lastStepIndex[summaryKey] = summary;
|
||||
console.log(`Gravity Bridge: [LS] relayed summary: "${summary.substring(0, 80)}"`);
|
||||
}
|
||||
pollFailCount++;
|
||||
else if (summary && summary !== lastStepIndex[key + '_summary']) {
|
||||
// Summary changed = new topic
|
||||
writeChatSnapshot(`**${summary}**\n\n(step ${prev} → ${stepIdx})`);
|
||||
console.log(`Gravity Bridge: [LS] → summary change relayed`);
|
||||
}
|
||||
lastStepIndex[key + '_summary'] = summary;
|
||||
lastStepIndex[key] = stepIdx;
|
||||
}
|
||||
}
|
||||
@@ -450,6 +407,34 @@ function activate(context) {
|
||||
console.log(`Gravity Bridge: [LS] relayed ${messages.length} response(s) (${combined.length} chars) to Discord`);
|
||||
}
|
||||
}
|
||||
function extractFromLogs(diagData) {
|
||||
// Try to find AI response text in extension logs
|
||||
const logs = diagData.extensionLogs || diagData.extension_logs || '';
|
||||
if (!logs || typeof logs !== 'string' || logs.length < 10) {
|
||||
return null;
|
||||
}
|
||||
// Look for patterns that indicate AI response text
|
||||
// Pattern 1: notify_user Message content
|
||||
const notifyMatch = logs.match(/notify_user.*?"Message"\s*:\s*"([^"]{20,})"/s);
|
||||
if (notifyMatch) {
|
||||
return notifyMatch[1];
|
||||
}
|
||||
// Pattern 2: "content": "..." blocks from assistant role
|
||||
const contentMatches = logs.match(/"content"\s*:\s*"([^"]{50,})"/g);
|
||||
if (contentMatches && contentMatches.length > 0) {
|
||||
const lastContent = contentMatches[contentMatches.length - 1];
|
||||
const m = lastContent.match(/"content"\s*:\s*"(.+)"/);
|
||||
if (m) {
|
||||
return m[1].replace(/\\n/g, '\n').replace(/\\"/g, '"');
|
||||
}
|
||||
}
|
||||
// Pattern 3: Look for Korean text blocks (likely user-facing response)
|
||||
const koreanBlocks = logs.match(/[\uAC00-\uD7A3]{10,}[^"]{0,200}/g);
|
||||
if (koreanBlocks && koreanBlocks.length > 0) {
|
||||
return koreanBlocks[koreanBlocks.length - 1].substring(0, 500);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// Start LS bridge after a delay
|
||||
setTimeout(async () => {
|
||||
const found = await discoverLS();
|
||||
|
||||
Reference in New Issue
Block a user