(api: OpenClawPluginApi)
| 67 | } |
| 68 | |
| 69 | private trySetLLMContextFromPluginConfig(api: OpenClawPluginApi): boolean { |
| 70 | const pluginConfig = api.pluginConfig as Record<string, unknown> | undefined; |
| 71 | const llm = (pluginConfig?.llm ?? null) as Record<string, unknown> | null; |
| 72 | if (!llm || typeof llm !== "object") return false; |
| 73 | |
| 74 | const provider = SessionState.readString(llm.provider); |
| 75 | const modelId = SessionState.readString(llm.model); |
| 76 | const apiType = SessionState.readString(llm.api); |
| 77 | const baseUrl = SessionState.readString(llm.baseUrl); |
| 78 | const apiKeyEnv = SessionState.readString(llm.apiKeyEnv) || "AGENT_WARD_API_KEY"; |
| 79 | const apiKey = SessionState.readString(process.env[apiKeyEnv]); |
| 80 | |
| 81 | if (!provider || !modelId || !apiType || !baseUrl || !apiKey) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | const model: Model<Api> = { |
| 86 | id: modelId, |
| 87 | name: modelId, |
| 88 | provider, |
| 89 | api: apiType as Api, |
| 90 | baseUrl, |
| 91 | headers: (llm.headers as Record<string, string> | undefined), |
| 92 | input: ["text"], |
| 93 | } as Model<Api>; |
| 94 | |
| 95 | this.defaultModelRef = `${provider}/${modelId}`; |
| 96 | this.llmContext = { |
| 97 | model, |
| 98 | options: { |
| 99 | apiKey, |
| 100 | }, |
| 101 | apiKey, |
| 102 | modelRef: this.defaultModelRef, |
| 103 | }; |
| 104 | |
| 105 | getLogger().info(`[SessionState] LLM context initialized from plugin config using env ${apiKeyEnv}`); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | async setLLMContext(api: OpenClawPluginApi): Promise<void> { |
| 110 | if (this.trySetLLMContextFromPluginConfig(api)) { |
no test coverage detected