feat: switch from OpenAI API to native Ollama Python client
This commit refactors the LLM integration to use Ollama's native Python client instead of OpenAI-compatible API, while fixing critical template syntax errors and improving project structure. Key changes: - Replace openai package with ollama package in backend requirements - Rewrite llm.py to use ollama.AsyncClient for direct Ollama API calls - Update main.py to use non-streaming Ollama responses with thinking extraction - Fix template syntax error in MilkdownEditor.vue (GhostTextOverlay component tags) - Fix string截取错误 by using slice() instead of substring() - Add src/utils/api.js and src/utils/config.js for shared configuration - Add CORS middleware to FastAPI backend - Update prompt.py with clearer instructions for continuation generation - Add comprehensive README.md documentation BREAKING CHANGE: Environment variables OLLAMA_BASE_URL changed to OLLAMA_HOST (remove /v1/ suffix)
This commit is contained in:
@@ -1,64 +1,58 @@
|
||||
import { Plugin, PluginKey } from '@milkdown/prose/state';
|
||||
import { EditorView } from '@milkdown/prose/view';
|
||||
import { fetchSuggestion } from '../utils/api.js';
|
||||
import { DEBUG, API_URL } from '../utils/config.js';
|
||||
|
||||
const INLINE_SUGGESTION_KEY = new PluginKey('inline-suggestion');
|
||||
const DEBOUNCE_MS = 150;
|
||||
let debounceTimer = null;
|
||||
let currentSuggestion = '';
|
||||
let suggestionPos = { from: 0, to: 0 };
|
||||
|
||||
interface InlineSuggestionOptions {
|
||||
apiUrl?: string;
|
||||
}
|
||||
|
||||
function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
|
||||
const apiUrl = options.apiUrl || 'http://localhost:8000/v1/completions';
|
||||
console.log('[InlineSuggestion] Plugin initialized with API URL:', apiUrl);
|
||||
interface InlineSuggestionState {
|
||||
suggestion: string;
|
||||
visible: boolean;
|
||||
debounceTimer: ReturnType<typeof setTimeout> | null;
|
||||
currentSuggestion: string;
|
||||
suggestionPos: { from: number; to: number };
|
||||
}
|
||||
|
||||
return new Plugin({
|
||||
function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
|
||||
const apiUrl = options.apiUrl || API_URL;
|
||||
|
||||
return new Plugin<InlineSuggestionState>({
|
||||
key: INLINE_SUGGESTION_KEY,
|
||||
state: {
|
||||
init: () => {
|
||||
console.log('[InlineSuggestion] State initialized');
|
||||
return { suggestion: '', visible: false };
|
||||
},
|
||||
init: () => ({
|
||||
suggestion: '',
|
||||
visible: false,
|
||||
debounceTimer: null,
|
||||
currentSuggestion: '',
|
||||
suggestionPos: { from: 0, to: 0 }
|
||||
}),
|
||||
apply: (tr, value) => {
|
||||
if (!tr.docChanged) {
|
||||
console.log('[InlineSuggestion] No doc change in apply, returning same state');
|
||||
return value;
|
||||
}
|
||||
if (!tr.docChanged) return value;
|
||||
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');
|
||||
if (from === value.suggestionPos.from && to === value.suggestionPos.to) {
|
||||
return value;
|
||||
}
|
||||
const newState = { suggestion: '', visible: false };
|
||||
console.log('[InlineSuggestion] Resetting suggestion state');
|
||||
return newState;
|
||||
return { ...value, suggestion: '', visible: false };
|
||||
},
|
||||
},
|
||||
props: {
|
||||
handleKeyDown: (view: EditorView, event: KeyboardEvent) => {
|
||||
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) {
|
||||
const state = INLINE_SUGGESTION_KEY.getState(view.state);
|
||||
if (event.key === 'Tab' && state.visible) {
|
||||
event.preventDefault();
|
||||
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 = '';
|
||||
if (state.suggestion) {
|
||||
view.dispatch(view.state.tr.insertText(state.suggestion, view.state.selection.from));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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 = '';
|
||||
view.dispatch(view.state.tr.setMeta(INLINE_SUGGESTION_KEY, { ...state, suggestion: '', visible: false }));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -67,85 +61,30 @@ function createInlineSuggestionPlugin(options: InlineSuggestionOptions = {}) {
|
||||
},
|
||||
appendTransaction: (transactions, oldState, newState) => {
|
||||
const lastTr = transactions[transactions.length - 1];
|
||||
if (!lastTr || !lastTr.docChanged) {
|
||||
console.log('[InlineSuggestion] No document change in transaction');
|
||||
return null;
|
||||
}
|
||||
if (!lastTr || !lastTr.docChanged) return null;
|
||||
|
||||
console.log('[InlineSuggestion] Document changed, setting up debounce for', DEBOUNCE_MS, 'ms');
|
||||
|
||||
clearTimeout(debounceTimer);
|
||||
debounceTimer = setTimeout(async () => {
|
||||
const currentState = INLINE_SUGGESTION_KEY.getState(newState);
|
||||
|
||||
clearTimeout(currentState.debounceTimer);
|
||||
currentState.debounceTimer = setTimeout(async () => {
|
||||
const { from, to } = newState.selection;
|
||||
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' }),
|
||||
});
|
||||
|
||||
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) {
|
||||
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;
|
||||
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);
|
||||
const text = await fetchSuggestion(prefix, suffix, apiUrl);
|
||||
|
||||
if (text && newState.selection.from === from) {
|
||||
currentSuggestion = text;
|
||||
suggestionPos = { from, to: from + text.length };
|
||||
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');
|
||||
newState.apply(newState.tr.setMeta(INLINE_SUGGESTION_KEY, {
|
||||
...currentState,
|
||||
currentSuggestion: text,
|
||||
suggestionPos: { from, to: from + text.length },
|
||||
suggestion: text,
|
||||
visible: true
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[InlineSuggestion] Error:', e);
|
||||
if (DEBUG) console.error('Inline suggestion error:', e);
|
||||
}
|
||||
}, DEBOUNCE_MS);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user