@2en/clawly-plugins 1.17.3 → 1.17.4
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/gateway/config-repair.ts +119 -0
- package/gateway/index.ts +2 -0
- package/model-gateway-setup.ts +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config repair RPC: detects and fixes missing/incomplete clawly-model-gateway
|
|
3
|
+
* provider in openclaw.json.
|
|
4
|
+
*
|
|
5
|
+
* Methods:
|
|
6
|
+
* - clawly.config.repair({ dryRun? }) → { ok/repaired, detail }
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
|
|
11
|
+
import type {PluginApi} from '../index'
|
|
12
|
+
import {
|
|
13
|
+
PROVIDER_NAME,
|
|
14
|
+
readOpenclawConfig,
|
|
15
|
+
resolveStateDir,
|
|
16
|
+
writeOpenclawConfig,
|
|
17
|
+
} from '../model-gateway-setup'
|
|
18
|
+
|
|
19
|
+
export function registerConfigRepair(api: PluginApi) {
|
|
20
|
+
api.registerGatewayMethod('clawly.config.repair', async ({params, respond}) => {
|
|
21
|
+
const dryRun = params.dryRun === true
|
|
22
|
+
|
|
23
|
+
const cfg = api.pluginConfig as Record<string, unknown> | undefined
|
|
24
|
+
const baseUrl =
|
|
25
|
+
typeof cfg?.modelGatewayBaseUrl === 'string' ? cfg.modelGatewayBaseUrl.replace(/\/$/, '') : ''
|
|
26
|
+
const token = typeof cfg?.modelGatewayToken === 'string' ? cfg.modelGatewayToken : ''
|
|
27
|
+
|
|
28
|
+
if (!baseUrl || !token) {
|
|
29
|
+
respond(true, {
|
|
30
|
+
...(dryRun ? {ok: false} : {repaired: false}),
|
|
31
|
+
detail: 'Plugin config missing modelGatewayBaseUrl or modelGatewayToken — cannot repair',
|
|
32
|
+
})
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const stateDir = resolveStateDir(api)
|
|
37
|
+
if (!stateDir) {
|
|
38
|
+
respond(true, {
|
|
39
|
+
...(dryRun ? {ok: false} : {repaired: false}),
|
|
40
|
+
detail: 'Cannot resolve state dir',
|
|
41
|
+
})
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const configPath = path.join(stateDir, 'openclaw.json')
|
|
46
|
+
const config = readOpenclawConfig(configPath)
|
|
47
|
+
|
|
48
|
+
// Check current provider state
|
|
49
|
+
const providers = (config.models as any)?.providers as Record<string, any> | undefined
|
|
50
|
+
const provider = providers?.[PROVIDER_NAME] as Record<string, unknown> | undefined
|
|
51
|
+
const currentBaseUrl = typeof provider?.baseUrl === 'string' ? provider.baseUrl : ''
|
|
52
|
+
const currentApiKey = typeof provider?.apiKey === 'string' ? provider.apiKey : ''
|
|
53
|
+
|
|
54
|
+
const needsRepair = !provider || !currentBaseUrl || !currentApiKey
|
|
55
|
+
|
|
56
|
+
if (!needsRepair) {
|
|
57
|
+
respond(true, {
|
|
58
|
+
...(dryRun ? {ok: true} : {repaired: false}),
|
|
59
|
+
detail: 'Provider credentials intact',
|
|
60
|
+
})
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Dry-run: report status without mutating
|
|
65
|
+
if (dryRun) {
|
|
66
|
+
const missing: string[] = []
|
|
67
|
+
if (!provider) missing.push('provider entry')
|
|
68
|
+
else {
|
|
69
|
+
if (!currentBaseUrl) missing.push('baseUrl')
|
|
70
|
+
if (!currentApiKey) missing.push('apiKey')
|
|
71
|
+
}
|
|
72
|
+
respond(true, {ok: false, detail: `Missing: ${missing.join(', ')}`})
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Repair: derive models from agents.defaults (same logic as model-gateway-setup)
|
|
77
|
+
const defaultModelFull: string = (config.agents as any)?.defaults?.model?.primary ?? ''
|
|
78
|
+
const imageModelFull: string = (config.agents as any)?.defaults?.imageModel?.primary ?? ''
|
|
79
|
+
|
|
80
|
+
const prefix = `${PROVIDER_NAME}/`
|
|
81
|
+
const defaultModel = defaultModelFull.startsWith(prefix)
|
|
82
|
+
? defaultModelFull.slice(prefix.length)
|
|
83
|
+
: defaultModelFull
|
|
84
|
+
const imageModel = imageModelFull.startsWith(prefix)
|
|
85
|
+
? imageModelFull.slice(prefix.length)
|
|
86
|
+
: imageModelFull
|
|
87
|
+
|
|
88
|
+
const models = !defaultModel
|
|
89
|
+
? (provider?.models ?? [])
|
|
90
|
+
: defaultModel === imageModel || !imageModel
|
|
91
|
+
? [{id: defaultModel, name: defaultModel, input: ['text', 'image']}]
|
|
92
|
+
: [
|
|
93
|
+
{id: defaultModel, name: defaultModel, input: ['text']},
|
|
94
|
+
{id: imageModel, name: imageModel, input: ['text', 'image']},
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
if (!config.models) config.models = {}
|
|
98
|
+
if (!(config.models as any).providers) (config.models as any).providers = {}
|
|
99
|
+
;(config.models as any).providers[PROVIDER_NAME] = {
|
|
100
|
+
...(provider ?? {}),
|
|
101
|
+
baseUrl,
|
|
102
|
+
apiKey: token,
|
|
103
|
+
api: 'openai-completions',
|
|
104
|
+
models,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
writeOpenclawConfig(configPath, config)
|
|
109
|
+
api.logger.info(`config.repair: restored provider credentials (baseUrl + apiKey)`)
|
|
110
|
+
respond(true, {repaired: true, detail: 'Provider credentials restored'})
|
|
111
|
+
} catch (err) {
|
|
112
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
113
|
+
api.logger.error(`config.repair: write failed — ${msg}`)
|
|
114
|
+
respond(true, {repaired: false, detail: `Write failed: ${msg}`})
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
api.logger.info('config-repair: registered clawly.config.repair')
|
|
119
|
+
}
|
package/gateway/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {PluginApi} from '../index'
|
|
|
2
2
|
import {registerAgentSend} from './agent'
|
|
3
3
|
import {registerChannelsConfigure} from './channels-configure'
|
|
4
4
|
import {registerClawhub2gateway} from './clawhub2gateway'
|
|
5
|
+
import {registerConfigRepair} from './config-repair'
|
|
5
6
|
import {registerMemoryBrowser} from './memory'
|
|
6
7
|
import {registerNotification} from './notification'
|
|
7
8
|
import {registerOfflinePush} from './offline-push'
|
|
@@ -17,4 +18,5 @@ export function registerGateway(api: PluginApi) {
|
|
|
17
18
|
registerPlugins(api)
|
|
18
19
|
registerChannelsConfigure(api)
|
|
19
20
|
registerOfflinePush(api)
|
|
21
|
+
registerConfigRepair(api)
|
|
20
22
|
}
|
package/model-gateway-setup.ts
CHANGED
|
@@ -14,13 +14,13 @@ import path from 'node:path'
|
|
|
14
14
|
|
|
15
15
|
import type {PluginApi} from './index'
|
|
16
16
|
|
|
17
|
-
const PROVIDER_NAME = 'clawly-model-gateway'
|
|
17
|
+
export const PROVIDER_NAME = 'clawly-model-gateway'
|
|
18
18
|
|
|
19
|
-
function resolveStateDir(api: PluginApi): string {
|
|
19
|
+
export function resolveStateDir(api: PluginApi): string {
|
|
20
20
|
return api.runtime.state?.resolveStateDir?.(process.env) ?? process.env.OPENCLAW_STATE_DIR ?? ''
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
function readOpenclawConfig(configPath: string): Record<string, unknown> {
|
|
23
|
+
export function readOpenclawConfig(configPath: string): Record<string, unknown> {
|
|
24
24
|
try {
|
|
25
25
|
return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
|
|
26
26
|
} catch {
|
|
@@ -28,7 +28,7 @@ function readOpenclawConfig(configPath: string): Record<string, unknown> {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
function writeOpenclawConfig(configPath: string, config: Record<string, unknown>) {
|
|
31
|
+
export function writeOpenclawConfig(configPath: string, config: Record<string, unknown>) {
|
|
32
32
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n')
|
|
33
33
|
}
|
|
34
34
|
|