* Send a prompt through the appropriate backend for the given model. * - Local models (browser_local, llama_cpp_local): routed directly, no inference params * - Remote models: fetches per-model params and delegates to sendApi.fetchResponse
(
modelName: string,
preprocessResult: PreProcessorResult,
token?: string,
enableStreaming: boolean = false,
onStreamChunk?: (chunk: string) => void,
onReasoningChunk?: (chunk: string) => void
)
| 648 | * - Remote models: fetches per-model params and delegates to sendApi.fetchResponse |
| 649 | */ |
| 650 | public async sendPrompt( |
| 651 | modelName: string, |
| 652 | preprocessResult: PreProcessorResult, |
| 653 | token?: string, |
| 654 | enableStreaming: boolean = false, |
| 655 | onStreamChunk?: (chunk: string) => void, |
| 656 | onReasoningChunk?: (chunk: string) => void |
| 657 | ): Promise<string> { |
| 658 | // Resolve model → server |
| 659 | let modelsResponse = this.listModels(); |
| 660 | let model = modelsResponse.models.find(m => m.name === modelName); |
| 661 | if (!model) { |
| 662 | modelsResponse = await this.fetchModels(); |
| 663 | model = modelsResponse.models.find(m => m.name === modelName); |
| 664 | } |
| 665 | if (!model) throw new Error(`Model '${modelName}' not found in available models`); |
| 666 | |
| 667 | const serverAddress = model.server; |
| 668 | |
| 669 | // Build messages from preprocessResult |
| 670 | const hasImages = preprocessResult.images && preprocessResult.images.length > 0; |
| 671 | let content: any = preprocessResult.modifiedPrompt; |
| 672 | if (hasImages) { |
| 673 | content = [ |
| 674 | { type: 'text', text: preprocessResult.modifiedPrompt }, |
| 675 | ...preprocessResult.images!.map(img => ({ |
| 676 | type: 'image', |
| 677 | image: `data:image/png;base64,${img}`, |
| 678 | })), |
| 679 | ]; |
| 680 | } |
| 681 | const messages = [{ role: 'user', content }]; |
| 682 | |
| 683 | // Local models: route directly, they manage their own settings |
| 684 | if (this.isLocalModel(serverAddress)) { |
| 685 | if (!this.isLocalModelReady(serverAddress)) { |
| 686 | throw new Error('Local model not loaded. Please load it from the Add Model panel.'); |
| 687 | } |
| 688 | return this.generateWithLocalModel(serverAddress, messages, onStreamChunk, onReasoningChunk); |
| 689 | } |
| 690 | |
| 691 | // Remote models: attach per-model inference params |
| 692 | if (serverAddress.includes('api.observer-ai.com') && token) { |
| 693 | this.optimisticUpdateQuota(); |
| 694 | } |
| 695 | const { fetchResponse } = await import('./sendApi'); |
| 696 | const params = { ...DEFAULT_INFERENCE_PARAMS, ...this.getModelParams(modelName) }; |
| 697 | return fetchResponse(serverAddress, messages, modelName, token, enableStreaming, onStreamChunk, params, onReasoningChunk); |
| 698 | } |
| 699 | |
| 700 | // =========================================================================== |
| 701 | // Utilities |
no test coverage detected