* Send messages to a model by name. Routes local models directly, remote models via fetchResponse.
(
modelName: string,
messages: Array<{ role: string; content: any }>,
token?: string,
enableStreaming: boolean = false,
onStreamChunk?: (chunk: string) => void,
onReasoningChunk?: (chunk: string) => void
)
| 785 | * Send messages to a model by name. Routes local models directly, remote models via fetchResponse. |
| 786 | */ |
| 787 | public async sendMessages( |
| 788 | modelName: string, |
| 789 | messages: Array<{ role: string; content: any }>, |
| 790 | token?: string, |
| 791 | enableStreaming: boolean = false, |
| 792 | onStreamChunk?: (chunk: string) => void, |
| 793 | onReasoningChunk?: (chunk: string) => void |
| 794 | ): Promise<string> { |
| 795 | let modelsResponse = this.listModels(); |
| 796 | let model = modelsResponse.models.find(m => m.name === modelName); |
| 797 | if (!model) { |
| 798 | modelsResponse = await this.fetchModels(); |
| 799 | model = modelsResponse.models.find(m => m.name === modelName); |
| 800 | } |
| 801 | // Ghost models (agent-creator-only) aren't returned by listModels but are valid on the |
| 802 | // Observer API — route them there when a token is present. |
| 803 | if (!model && !token) throw new Error(`Model '${modelName}' not found in available models`); |
| 804 | |
| 805 | const serverAddress = model ? model.server : 'https://api.observer-ai.com:443'; |
| 806 | |
| 807 | if (model?.decision) { |
| 808 | throw new Error(`'${modelName}' is a decision model: it answers agent questions and can't chat. Pick an LLM.`); |
| 809 | } |
| 810 | |
| 811 | if (this.isLocalModel(serverAddress)) { |
| 812 | if (!this.isLocalModelReady(serverAddress)) { |
| 813 | throw new Error('Local model not loaded. Please load it from the Add Model panel.'); |
| 814 | } |
| 815 | return this.generateWithLocalModel(serverAddress, messages, onStreamChunk, onReasoningChunk); |
| 816 | } |
| 817 | |
| 818 | if (serverAddress.includes('api.observer-ai.com') && token) { |
| 819 | this.optimisticUpdateQuota(); |
| 820 | } |
| 821 | const { fetchResponse } = await import('./sendApi'); |
| 822 | const params = { ...DEFAULT_INFERENCE_PARAMS, ...this.getModelParams(modelName) }; |
| 823 | return fetchResponse(serverAddress, messages, modelName, token, enableStreaming, onStreamChunk, params, onReasoningChunk); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Tools-aware send path. Returns the full AssistantResponse (content + tool_calls + |
no test coverage detected