@2en/clawly-plugins 1.17.3 → 1.17.5

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.
@@ -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/gateway/memory.ts CHANGED
@@ -44,39 +44,57 @@ function resolveStateDir(api: PluginApi): string {
44
44
  return api.runtime.state?.resolveStateDir?.(process.env) ?? process.env.OPENCLAW_STATE_DIR ?? ''
45
45
  }
46
46
 
47
- /** Read the default agent's workspace path from openclaw.json (cached). */
48
- let _cachedAgentWorkspace: string | null | undefined
49
- function readAgentWorkspace(api: PluginApi): string | null {
50
- if (_cachedAgentWorkspace !== undefined) return _cachedAgentWorkspace
47
+ /** Read agents list from openclaw.json (cached). */
48
+ let _cachedAgentsList: Array<{id?: string; default?: boolean; workspace?: string}> | null | undefined
49
+ function readAgentsList(api: PluginApi): Array<{id?: string; default?: boolean; workspace?: string}> | null {
50
+ if (_cachedAgentsList !== undefined) return _cachedAgentsList
51
51
  try {
52
52
  const stateDir = resolveStateDir(api)
53
53
  if (!stateDir) {
54
- _cachedAgentWorkspace = null
54
+ _cachedAgentsList = null
55
55
  return null
56
56
  }
57
57
  const raw = fsSync.readFileSync(path.join(stateDir, 'openclaw.json'), 'utf-8')
58
58
  const config = JSON.parse(raw)
59
59
  const agents = config?.agents?.list
60
60
  if (!Array.isArray(agents)) {
61
- _cachedAgentWorkspace = null
61
+ _cachedAgentsList = null
62
62
  return null
63
63
  }
64
- const defaultAgent = agents.find((a: any) => a.default) ?? agents[0]
65
- const ws = typeof defaultAgent?.workspace === 'string' ? defaultAgent.workspace : null
66
- _cachedAgentWorkspace = ws
67
- return ws
64
+ _cachedAgentsList = agents
65
+ return agents
68
66
  } catch {
69
- _cachedAgentWorkspace = null
67
+ _cachedAgentsList = null
70
68
  return null
71
69
  }
72
70
  }
73
71
 
72
+ /** Read workspace path for a specific agent (by id) or the default agent. */
73
+ function readAgentWorkspace(api: PluginApi, agentId?: string): string | null {
74
+ const agents = readAgentsList(api)
75
+ if (!agents) return null
76
+ let agent: (typeof agents)[number] | undefined
77
+ if (agentId) {
78
+ agent = agents.find((a) => a.id === agentId)
79
+ }
80
+ if (!agent) {
81
+ agent = agents.find((a) => a.default) ?? agents[0]
82
+ }
83
+ return typeof agent?.workspace === 'string' ? agent.workspace : null
84
+ }
85
+
74
86
  /** Resolve the workspace root directory (without /memory suffix). */
75
87
  function resolveWorkspaceRoot(api: PluginApi, profile?: string): string {
76
88
  const cfg = coercePluginConfig(api)
77
89
  const configPath = configString(cfg, 'memoryDir')
78
90
  if (configPath) return path.dirname(configPath) // strip /memory if configured
79
91
 
92
+ // If profile is an agent id, look up that agent's workspace directly
93
+ if (profile && profile !== 'main') {
94
+ const agentWs = readAgentWorkspace(api, profile)
95
+ if (agentWs) return agentWs
96
+ }
97
+
80
98
  const stateDir = resolveStateDir(api)
81
99
  const baseDir =
82
100
  process.env.OPENCLAW_WORKSPACE ??
@@ -85,6 +103,7 @@ function resolveWorkspaceRoot(api: PluginApi, profile?: string): string {
85
103
  ? path.join(stateDir, 'workspace')
86
104
  : path.join(os.homedir(), '.openclaw', 'workspace'))
87
105
  if (profile && profile !== 'main') {
106
+ // Fallback: append profile suffix when agent not found in config
88
107
  const parentDir = path.dirname(baseDir)
89
108
  const baseName = path.basename(baseDir)
90
109
  return path.join(parentDir, `${baseName}-${profile}`)
@@ -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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@2en/clawly-plugins",
3
- "version": "1.17.3",
3
+ "version": "1.17.5",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "repository": {