|
|
|
|
|
|
|
|
import { outputMessage } from './output-message'; |
|
|
import { chatLogEditor } from './boot-app'; |
|
|
import { editorViewCtx, serializerCtx } from '@milkdown/core'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function handlePrompt({ promptMarkdown, workerConnection }) { |
|
|
|
|
|
let historyText = await chatLogEditor.action(async (ctx) => { |
|
|
const serializer = ctx.get(serializerCtx); |
|
|
const view = ctx.get(editorViewCtx); |
|
|
return serializer(view.state.doc); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const trimmed = (promptMarkdown || '').trim(); |
|
|
if (trimmed.startsWith('/') && trimmed.length > 1) { |
|
|
const modelId = trimmed.slice(1).trim(); |
|
|
outputMessage(`Loading model: ${modelId}...`); |
|
|
try { |
|
|
await workerConnection.loadModel(modelId); |
|
|
outputMessage(`Model ${modelId} loaded successfully!`); |
|
|
} catch (error) { |
|
|
outputMessage(`Error loading model ${modelId}: ${error.message}`); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
const formatted = `**Question:**\n> ${promptMarkdown.replaceAll('\n', '\n> ')}`; |
|
|
outputMessage(formatted); |
|
|
|
|
|
outputMessage('Processing your request...'); |
|
|
try { |
|
|
|
|
|
const combinedPrompt = promptMarkdown; |
|
|
const promptOutput = await workerConnection.runPrompt(combinedPrompt); |
|
|
outputMessage('**Reply:**\n' + promptOutput); |
|
|
} catch (error) { |
|
|
outputMessage('**Error:** ' + error.message); |
|
|
} |
|
|
} |