@aliwey/bmo 2.0.8 → 2.0.9

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.
@@ -76,19 +76,23 @@ class OpenCodeBotClient:
76
76
  self.is_connected = False
77
77
  return self.is_connected
78
78
 
79
- async def create_session(self, provider_id: Optional[str] = None, model_id: Optional[str] = None, env: Optional[dict] = None) -> Optional[str]:
79
+ async def create_session(self, provider_id: Optional[str] = None, model_id: Optional[str] = None, env: Optional[dict] = None, agent: str = "build") -> Optional[str]:
80
80
  """Create a new opencode session and return its ID."""
81
81
  try:
82
82
  # Force defaults if None/Empty
83
83
  p_id = provider_id if (provider_id and str(provider_id) != "None") else "opencode"
84
84
  m_id = model_id if (model_id and str(model_id) != "None") else "big-pickle"
85
85
 
86
- # The server expects a nested 'model' object
86
+ # Map 'default' to 'build' to prevent server-side agent switching errors
87
+ agent_to_use = "build" if (not agent or agent == "default") else agent
88
+
89
+ # The server expects a nested 'model' object and optional 'agent'
87
90
  payload = {
88
91
  "model": {
89
92
  "id": m_id,
90
93
  "providerID": p_id
91
- }
94
+ },
95
+ "agent": agent_to_use
92
96
  }
93
97
 
94
98
  if env:
@@ -104,7 +108,7 @@ class OpenCodeBotClient:
104
108
  session_id = data.get("id")
105
109
 
106
110
  actual_model = data.get("model", {}).get("id", "Unknown")
107
- logger.info("--- OPENCODE SESSION CREATED: %s (Model: %s) ---", session_id, actual_model)
111
+ logger.info("--- OPENCODE SESSION CREATED: %s (Model: %s, Agent: %s) ---", session_id, actual_model, agent_to_use)
108
112
  return session_id
109
113
  except Exception as e:
110
114
  logger.error("Failed to create session: %s", e)
@@ -282,7 +286,7 @@ class OpenCodeBotClient:
282
286
 
283
287
  if not session_id:
284
288
  # Inject keys if provided
285
- session_id = await self.create_session(provider_id, model_id, env=provider_env)
289
+ session_id = await self.create_session(provider_id, model_id, env=provider_env, agent=active_agent)
286
290
  if not session_id:
287
291
  return "Error: Could not create OpenCode session."
288
292
 
@@ -411,7 +415,7 @@ class OpenCodeBotClient:
411
415
  # Automatic recovery: if session not found, clear it and try once more
412
416
  if r is not None and r.status_code == 404:
413
417
  logger.warning("Session %s not found on server. Creating new session...", session_id)
414
- session_id = await self.create_session(provider_id, model_id, env=provider_env)
418
+ session_id = await self.create_session(provider_id, model_id, env=provider_env, agent=active_agent)
415
419
  if not session_id:
416
420
  return "Error: Session lost and could not create a new one."
417
421
  self.last_session_id = session_id
@@ -576,7 +580,7 @@ class OpenCodeBotClient:
576
580
  return "❌ Error: Could not connect to OpenCode backend. Is the server running?"
577
581
 
578
582
  if not session_id:
579
- session_id = await self.create_session(provider_id, model_id, env=provider_env)
583
+ session_id = await self.create_session(provider_id, model_id, env=provider_env, agent=active_agent)
580
584
  if not session_id:
581
585
  return "Error: Could not create OpenCode session."
582
586
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliwey/bmo",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "BMO — AI coding assistant with Telegram, CLI & Web sync. One command, all frontends.",
5
5
  "keywords": ["ai", "coding-assistant", "telegram-bot", "cli", "opencode", "bfp"],
6
6
  "homepage": "https://github.com/aliwey/bmo",
package/webchat/server.js CHANGED
@@ -327,7 +327,10 @@ app.post('/api/switch-model', express.json(), async (req, res) => {
327
327
  const parts = (model || 'big-pickle').split('/');
328
328
  const bareModelId = parts.pop();
329
329
  const providerId = provider || parts.pop() || 'opencode';
330
- const payload = { model: { id: bareModelId, providerID: providerId } };
330
+ const payload = {
331
+ model: { id: bareModelId, providerID: providerId },
332
+ agent: 'build'
333
+ };
331
334
  const r = await fetch(`${OPENCODE_URL}/session`, {
332
335
  method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload)
333
336
  });
@@ -353,9 +356,10 @@ function generateTitle(firstMessage) {
353
356
  return words.slice(0, 6).join(' ') + '…';
354
357
  }
355
358
 
356
- async function createOpenCodeSession(modelId = 'qwen3.6-plus-free', providerId = 'opencode') {
359
+ async function createOpenCodeSession(modelId = 'qwen3.6-plus-free', providerId = 'opencode', agent = 'build') {
357
360
  const payload = {
358
- model: { id: modelId, providerID: providerId }
361
+ model: { id: modelId, providerID: providerId },
362
+ agent: agent === 'default' ? 'build' : agent
359
363
  };
360
364
  const controller = new AbortController();
361
365
  const timeout = setTimeout(() => controller.abort(), 10000);
@@ -371,7 +375,7 @@ async function createOpenCodeSession(modelId = 'qwen3.6-plus-free', providerId =
371
375
  }
372
376
  const data = await r.json();
373
377
  const sessionId = data.id;
374
- console.log('[OpenCode] Created session:', sessionId, 'model:', modelId);
378
+ console.log('[OpenCode] Created session:', sessionId, 'model:', modelId, 'agent:', payload.agent);
375
379
  return sessionId;
376
380
  } catch (e) {
377
381
  clearTimeout(timeout);
@@ -412,14 +416,14 @@ async function callOpenCode(text, sessionId, mode = 'execute', agent = 'default'
412
416
 
413
417
  if (r && r.status === 404) {
414
418
  console.log('[OpenCode] Session not found, creating new one...');
415
- sessionId = await createOpenCodeSession(modelId || 'qwen3.6-plus-free', providerId || 'opencode');
419
+ sessionId = await createOpenCodeSession(modelId || 'qwen3.6-plus-free', providerId || 'opencode', agent);
416
420
  r = await doSend(sessionId);
417
421
  }
418
422
 
419
423
  // 500 with a recoverable model → create new session and retry
420
424
  if (r && r.status === 500 && modelId) {
421
425
  console.log('[OpenCode] Session 500, creating new session with model:', modelId);
422
- sessionId = await createOpenCodeSession(modelId, providerId);
426
+ sessionId = await createOpenCodeSession(modelId, providerId, agent);
423
427
  r = await doSend(sessionId, 30000);
424
428
  }
425
429