feat: enhance logging and error handling in backend and editor components

This commit is contained in:
2026-01-25 13:29:11 +08:00
parent bf7dec86ab
commit 5f00e71ceb
6 changed files with 179 additions and 87 deletions
+68 -15
View File
@@ -13,25 +13,40 @@ interface InlineSuggestionOptions {
function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
const apiUrl = options.apiUrl || 'http://localhost:8000/v1/completions';
console.log('[InlineSuggestion] Plugin initialized with API URL:', apiUrl);
return new Plugin({
key: INLINE_SUGGESTION_KEY,
state: {
init: () => ({ suggestion: '', visible: false }),
init: () => {
console.log('[InlineSuggestion] State initialized');
return { suggestion: '', visible: false };
},
apply: (tr, value) => {
if (!tr.docChanged) return value;
const { from, to } = tr.selection;
if (from === suggestionPos.from && to === suggestionPos.to) {
if (!tr.docChanged) {
console.log('[InlineSuggestion] No doc change in apply, returning same state');
return value;
}
return { suggestion: '', visible: false };
const { from, to } = tr.selection;
console.log('[InlineSuggestion] Apply called - selection changed:', { from, to }, 'current suggestionPos:', suggestionPos);
if (from === suggestionPos.from && to === suggestionPos.to) {
console.log('[InlineSuggestion] Selection matches suggestion position, keeping state');
return value;
}
const newState = { suggestion: '', visible: false };
console.log('[InlineSuggestion] Resetting suggestion state');
return newState;
},
},
props: {
handleKeyDown: (view: EditorView, event: KeyboardEvent) => {
if (event.key === 'Tab' && INLINE_SUGGESTION_KEY.getState(view.state).visible) {
const currentState = INLINE_SUGGESTION_KEY.getState(view.state);
console.log('[InlineSuggestion] Key pressed:', event.key, 'suggestion visible:', currentState.visible);
if (event.key === 'Tab' && currentState.visible) {
event.preventDefault();
const { suggestion } = INLINE_SUGGESTION_KEY.getState(view.state);
const { suggestion } = currentState;
console.log('[InlineSuggestion] Tab pressed - accepting suggestion:', suggestion.substring(0, 50));
if (suggestion) {
view.dispatch(view.state.tr.insertText(suggestion, view.state.selection.from));
currentSuggestion = '';
@@ -41,6 +56,7 @@ function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
if (event.key === 'Escape') {
const state = INLINE_SUGGESTION_KEY.getState(view.state);
if (state.visible) {
console.log('[InlineSuggestion] Escape pressed - dismissing suggestion');
view.dispatch(view.state.tr.setMeta(INLINE_SUGGESTION_KEY, { suggestion: '', visible: false }));
currentSuggestion = '';
return true;
@@ -51,7 +67,12 @@ function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
},
appendTransaction: (transactions, oldState, newState) => {
const lastTr = transactions[transactions.length - 1];
if (!lastTr || !lastTr.docChanged) return null;
if (!lastTr || !lastTr.docChanged) {
console.log('[InlineSuggestion] No document change in transaction');
return null;
}
console.log('[InlineSuggestion] Document changed, setting up debounce for', DEBOUNCE_MS, 'ms');
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
@@ -59,40 +80,72 @@ function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
const prefix = newState.doc.textBetween(0, from);
const suffix = newState.doc.textBetween(to, newState.doc.content.size);
console.log('[InlineSuggestion] Debounce fired - position:', { from, to });
console.log('[InlineSuggestion] Prefix length:', prefix.length, 'Suffix length:', suffix.length);
console.log('[InlineSuggestion] Prefix (last 100):', prefix.slice(-100));
console.log('[InlineSuggestion] Suffix (first 100):', suffix.slice(0, 100));
try {
console.log('[InlineSuggestion] Fetching from:', apiUrl);
const res = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prefix, suffix, languageId: 'markdown' }),
});
if (!res.ok) return;
console.log('[InlineSuggestion] Response status:', res.status);
if (!res.ok) {
const errorText = await res.text();
console.error('[InlineSuggestion] API error:', errorText);
return;
}
const reader = res.body?.getReader();
if (!reader) return;
if (!reader) {
console.error('[InlineSuggestion] No response body reader');
return;
}
let text = '';
let chunkCount = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunkCount++;
const chunk = new TextDecoder().decode(value);
console.log('[InlineSuggestion] Raw chunk', chunkCount, ':', chunk.substring(0, 200));
const lines = chunk.split('\n').filter(l => l.startsWith('data: '));
for (const line of lines) {
try {
const data = JSON.parse(line.slice(6));
if (data.content) text += data.content;
if (data.done) break;
} catch {}
if (data.content) {
text += data.content;
console.log('[InlineSuggestion] Accumulated suggestion:', text.substring(0, 100));
}
if (data.done) {
console.log('[InlineSuggestion] Stream done signal received');
break;
}
} catch (e) {
console.error('[InlineSuggestion] JSON parse error:', e);
}
}
}
console.log('[InlineSuggestion] Total chunks received:', chunkCount, 'Total text length:', text.length);
if (text && newState.selection.from === from) {
currentSuggestion = text;
suggestionPos = { from, to: from + text.length };
newState.apply(newState.tr.setMeta(INLINE_SUGGESTION_KEY, { suggestion: text, visible: true }));
const metaUpdate = { suggestion: text, visible: true };
console.log('[InlineSuggestion] Setting suggestion:', text.substring(0, 50), '...');
newState.apply(newState.tr.setMeta(INLINE_SUGGESTION_KEY, metaUpdate));
} else {
console.log('[InlineSuggestion] Suggestion not applied - empty text or cursor moved');
}
} catch (e) {
console.error('Inline suggestion error:', e);
console.error('[InlineSuggestion] Error:', e);
}
}, DEBOUNCE_MS);