@2en/clawly-plugins 1.15.0 → 1.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -33,6 +33,7 @@ import {registerCronHook} from './cron-hook'
33
33
  import {registerEmail} from './email'
34
34
  import {registerGateway} from './gateway'
35
35
  import {getGatewayConfig} from './gateway-fetch'
36
+ import {setupModelGateway} from './model-gateway-setup'
36
37
  import {registerOutboundHook, registerOutboundHttpRoute, registerOutboundMethods} from './outbound'
37
38
  import {registerTools} from './tools'
38
39
 
@@ -114,6 +115,7 @@ export default {
114
115
  registerClawlyCronChannel(api)
115
116
  registerCronHook(api)
116
117
  registerGateway(api)
118
+ setupModelGateway(api)
117
119
 
118
120
  // Email & calendar (optional — requires skillGatewayBaseUrl + skillGatewayToken in config)
119
121
  const gw = getGatewayConfig(api)
@@ -0,0 +1,101 @@
1
+ /**
2
+ * On plugin init, patches openclaw.json to add the `clawly-model-gateway`
3
+ * model provider entry. Credentials come from pluginConfig; the model list
4
+ * is derived from `agents.defaults.model` / `agents.defaults.imageModel`
5
+ * already present in the config.
6
+ *
7
+ * This runs synchronously during plugin registration (before gateway_start).
8
+ * OpenClaw loads the config file once at startup, so writing before the
9
+ * gateway fully starts ensures the provider is active on first boot.
10
+ */
11
+
12
+ import fs from 'node:fs'
13
+ import path from 'node:path'
14
+
15
+ import type {PluginApi} from './index'
16
+
17
+ const PROVIDER_NAME = 'clawly-model-gateway'
18
+
19
+ function resolveStateDir(api: PluginApi): string {
20
+ return api.runtime.state?.resolveStateDir?.(process.env) ?? process.env.OPENCLAW_STATE_DIR ?? ''
21
+ }
22
+
23
+ function readOpenclawConfig(configPath: string): Record<string, unknown> {
24
+ try {
25
+ return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
26
+ } catch {
27
+ return {}
28
+ }
29
+ }
30
+
31
+ function writeOpenclawConfig(configPath: string, config: Record<string, unknown>) {
32
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n')
33
+ }
34
+
35
+ export function setupModelGateway(api: PluginApi): void {
36
+ const cfg = api.pluginConfig as Record<string, unknown> | undefined
37
+ const baseUrl =
38
+ typeof cfg?.modelGatewayBaseUrl === 'string' ? cfg.modelGatewayBaseUrl.replace(/\/$/, '') : ''
39
+ const token = typeof cfg?.modelGatewayToken === 'string' ? cfg.modelGatewayToken : ''
40
+
41
+ if (!baseUrl || !token) {
42
+ api.logger.info('Model gateway not configured (missing baseUrl or token), skipping.')
43
+ return
44
+ }
45
+
46
+ const stateDir = resolveStateDir(api)
47
+ if (!stateDir) {
48
+ api.logger.warn('Cannot resolve state dir — model gateway setup skipped.')
49
+ return
50
+ }
51
+
52
+ const configPath = path.join(stateDir, 'openclaw.json')
53
+ const config = readOpenclawConfig(configPath)
54
+
55
+ // Already configured — skip
56
+ if (config.models?.providers?.[PROVIDER_NAME]) {
57
+ api.logger.info('Model gateway provider already configured.')
58
+ return
59
+ }
60
+
61
+ // Derive model IDs from agents.defaults
62
+ const defaultModelFull: string = (config.agents as any)?.defaults?.model?.primary ?? ''
63
+ const imageModelFull: string = (config.agents as any)?.defaults?.imageModel?.primary ?? ''
64
+
65
+ const prefix = `${PROVIDER_NAME}/`
66
+ const defaultModel = defaultModelFull.startsWith(prefix)
67
+ ? defaultModelFull.slice(prefix.length)
68
+ : defaultModelFull
69
+ const imageModel = imageModelFull.startsWith(prefix)
70
+ ? imageModelFull.slice(prefix.length)
71
+ : imageModelFull
72
+
73
+ if (!defaultModel) {
74
+ api.logger.warn('No default model found in agents.defaults — model gateway setup skipped.')
75
+ return
76
+ }
77
+
78
+ const models =
79
+ defaultModel === imageModel || !imageModel
80
+ ? [{id: defaultModel, name: defaultModel, input: ['text', 'image']}]
81
+ : [
82
+ {id: defaultModel, name: defaultModel, input: ['text']},
83
+ {id: imageModel, name: imageModel, input: ['text', 'image']},
84
+ ]
85
+
86
+ if (!config.models) config.models = {}
87
+ if (!(config.models as any).providers) (config.models as any).providers = {}
88
+ ;(config.models as any).providers[PROVIDER_NAME] = {
89
+ baseUrl,
90
+ apiKey: token,
91
+ api: 'openai-completions',
92
+ models,
93
+ }
94
+
95
+ try {
96
+ writeOpenclawConfig(configPath, config)
97
+ api.logger.info(`Model gateway provider configured: ${baseUrl} with ${models.length} model(s).`)
98
+ } catch (err) {
99
+ api.logger.error(`Failed to setup model gateway: ${(err as Error).message}`)
100
+ }
101
+ }
@@ -47,7 +47,9 @@
47
47
  "defaultTimeoutMs": { "type": "number", "minimum": 1000 },
48
48
  "configPath": { "type": "string" },
49
49
  "skillGatewayBaseUrl": { "type": "string" },
50
- "skillGatewayToken": { "type": "string" }
50
+ "skillGatewayToken": { "type": "string" },
51
+ "modelGatewayBaseUrl": { "type": "string" },
52
+ "modelGatewayToken": { "type": "string" }
51
53
  },
52
54
  "required": []
53
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@2en/clawly-plugins",
3
- "version": "1.15.0",
3
+ "version": "1.16.1",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "repository": {
@@ -23,6 +23,8 @@
23
23
  "email.ts",
24
24
  "gateway-fetch.ts",
25
25
  "outbound.ts",
26
+ "auto-pair.ts",
27
+ "model-gateway-setup.ts",
26
28
  "openclaw.plugin.json"
27
29
  ],
28
30
  "publishConfig": {