(api: OpenClawPluginApi)
| 107 | } |
| 108 | |
| 109 | async setLLMContext(api: OpenClawPluginApi): Promise<void> { |
| 110 | if (this.trySetLLMContextFromPluginConfig(api)) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | const config: OpenClawConfig = api.config; |
| 115 | if (!config ) { |
| 116 | getLogger().error(`[SessionState] No config available, cannot initialize LLM context`); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | const modelConfig = config.agents?.defaults?.model; |
| 121 | this.defaultModelRef = typeof modelConfig === 'string' |
| 122 | ? modelConfig |
| 123 | : modelConfig?.primary; |
| 124 | |
| 125 | if (!this.defaultModelRef) { |
| 126 | getLogger().error(`[SessionState] No model reference found in config or session, cannot initialize LLM context`); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | const idx = this.defaultModelRef.indexOf("/"); |
| 131 | const provider = this.defaultModelRef.substring(0, idx); |
| 132 | const modelId = this.defaultModelRef.substring(idx + 1); |
| 133 | |
| 134 | try { |
| 135 | const authInfo = await api.runtime.modelAuth.resolveApiKeyForProvider({ |
| 136 | provider, |
| 137 | cfg: config, |
| 138 | }); |
| 139 | |
| 140 | if (!authInfo.apiKey) { |
| 141 | getLogger().warn(`[SessionState] No API key found for provider ${provider}`); |
| 142 | return; |
| 143 | } |
| 144 | let model: Model<Api> = null as any; |
| 145 | |
| 146 | const providerConfig = config.models?.providers?.[provider]; |
| 147 | if (providerConfig) { |
| 148 | for (const m of providerConfig.models) { |
| 149 | if (m.id === modelId) { |
| 150 | model = m as Model<Api>; |
| 151 | model.baseUrl = providerConfig.baseUrl; |
| 152 | model.api = providerConfig.api; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | model = model || { |
| 158 | id: modelId, |
| 159 | name: modelId, |
| 160 | provider: provider, |
| 161 | baseUrl: config.models?.providers?.[provider]?.baseUrl || PROVIDER_BASE_URLS[provider], |
| 162 | api: config.models?.providers?.[provider]?.api || PROVIDER_API_TYPES[provider], |
| 163 | } as Model<Api>; |
| 164 | |
| 165 | model = { |
| 166 | ...model, |
no test coverage detected