feat: 添加i18n,美化界面
This commit is contained in:
94
packages/codemirror/src/editor/modules/ai-completions.ts
Normal file
94
packages/codemirror/src/editor/modules/ai-completions.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { CompletionResult, CompletionSource } from '@codemirror/autocomplete';
|
||||
import { Transaction } from '@codemirror/state';
|
||||
|
||||
export async function fetchAICompletions(context) {
|
||||
const userInput = context.matchBefore(/\w*/);
|
||||
if (!userInput) return null;
|
||||
|
||||
const docText = context.state.doc.toString(); // 获取完整文档内容
|
||||
const cursorPos = context.pos; // 获取光标位置
|
||||
|
||||
// 添加提示词
|
||||
const promptText = `请根据以下代码和光标位置提供代码补全建议:
|
||||
|
||||
代码:
|
||||
${docText}
|
||||
|
||||
光标位置:${cursorPos}
|
||||
|
||||
请提供适当的代码补全。`;
|
||||
|
||||
const aiResponse = await fetch('http://192.168.31.220:11434/api/generate', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
prompt: promptText,
|
||||
// 如果 Ollama 支持,可以传递其他参数,如模型名称
|
||||
// model: 'your-model-name',
|
||||
model: 'qwen2.5-coder:14b',
|
||||
// model: 'qwen2.5:14b',
|
||||
stream: false,
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await aiResponse.json();
|
||||
const data = result;
|
||||
// 提取字段
|
||||
const createdAt = new Date(data.created_at); // 转换为日期对象
|
||||
const isDone = data.done;
|
||||
const doneReason = data.done_reason;
|
||||
const modelName = data.model;
|
||||
const responseText = data.response;
|
||||
const matchCodeBlock = responseText.match(/```javascript\n([\s\S]*?)\n```/);
|
||||
let codeBlock = '';
|
||||
if (matchCodeBlock) {
|
||||
const codeBlock = matchCodeBlock[1]; // 提取代码块
|
||||
console.log('补全代码:', codeBlock);
|
||||
}
|
||||
|
||||
const suggestions = [
|
||||
{
|
||||
label: codeBlock,
|
||||
type: 'text',
|
||||
},
|
||||
];
|
||||
return {
|
||||
from: userInput.from,
|
||||
options: suggestions,
|
||||
validFor: /^\w*$/,
|
||||
};
|
||||
}
|
||||
|
||||
// type AiCompletion = Readonly<CompletionSource>;
|
||||
export const testAiCompletion = async (context: any): Promise<CompletionResult | null> => {
|
||||
const userInput = context.matchBefore(/\w*/);
|
||||
if (!userInput) return null;
|
||||
const docText = context.state.doc.toString(); // 获取完整文档内容
|
||||
const cursorPos = context.pos; // 获取光标位置
|
||||
console.log('testAiCompletion', userInput, docText, cursorPos);
|
||||
|
||||
return {
|
||||
from: userInput.from,
|
||||
options: [
|
||||
{
|
||||
label: '123 测试 skflskdf ',
|
||||
type: 'text',
|
||||
apply: (state, completion, from, to) => {
|
||||
console.log('apply', state, completion, from, to);
|
||||
const newText = '123 测试 skflskdf ';
|
||||
state.dispatch({
|
||||
changes: { from, to, insert: newText },
|
||||
});
|
||||
},
|
||||
info: '这是一个长文本上的反馈是冷酷的父母离开时的父母 这是一个长文本上的反馈是冷酷的父母离开时的父母 这是一个长文本上的反馈是冷酷的父母离开时的父母这是一个长文本上的反馈是冷酷的父母离开时的父母 这是一个长文本上的反馈是冷酷的父母离开时的父母这是一个长文本上的反馈是冷酷的父母离开时的父母 这是一个长文本上的反馈是冷酷的父母离开时的父母',
|
||||
},
|
||||
{
|
||||
label: '456',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
validFor: /^\w*$/,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user