Skip to content

🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.

13.8 KB
Raw Blame
// anymodel bot ⇄ DSH live control (MIT, part of anymodel bot).
//
// The npm 0.2.1-rc.4 SDK protocol only offers initialize / session/prompt / session/resume / shutdown. This
// plugin gives the host a command channel into the running child: it long-polls the host over the loopback
// bridge (POST /dsh/control/poll with AMB_CONTROL_TOKEN, a per-launch id; the bridge token is only ever sent to
// AMB_BRIDGE_URL) and answers each command with POST /dsh/control/result. Commands:
//
//   select    {sessionId, provider, model, effort?}  per-conversation model: every Agent's prompt assembly and request
//                                            config read this map (as DSH's installModelSelection does)
//   providers {providers}                   replace llm-pi-ai's (volatile) provider profiles, so a model the
//                                            launch patch did not list becomes routable without a restart
//   steer     {sessionId, messageId, text, images} deliver a user message into the running turn (agent.steer:
//                                            the model sees it at its next step) under the host's message id (the
//                                            session's user/message carries it; the command's own id is the
//                                            envelope's); refused when the agent is idle
//   cancel    {sessionId}                   cancel just this session's turn (agent.cancel), its subagents
//                                            and the shell jobs the turn started
//   idle      {}                            how many agents are busy (not idle, or input queued) and how many
//                                            shell jobs run: the host recycles the child only when both are 0
//
// Ops this plugin does not know go to the handlers other plugins put on globalThis[Symbol.for('amb.control.handlers')]
// (amb-tasks: task-quiet, task-stop), so there is one command channel per child.
//
// Long shell commands move to background jobs (the host patches the shell timeouts); when one ends,
// tool-jobs wakes the idle session with its result and the host follows that turn like any other.
import { boundContextSummary, createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm'
import { admitEncodedImages } from '@deepseek-ai/dsh-attachment'

export const name = 'amb-control'

/**
 * Couple a session to the host's model selection, like DSH's installModelSelection (dsh-agent): prompt
 * assembly snapshots the selection ({{model}}), the request config takes its provider/model, and a change of
 * model adds a user-role notice to the next request. Unlike DSH's, a provider-only change (the same model moved
 * to another wire API after an upstream rejection) adds no notice. No selection: the Agent's own options.
 */
function installSelection(agentCtx, current) {
  let assembled
  const offAssembly = agentCtx.on('system-prompt/assemble', async (_assembly, _context, next) => {
    const selected = current()
    const result = await next()
    assembled = selected
    if (!selected) return result
    return { ...result, variables: { ...result.variables, provider: selected.provider, model: selected.model } }
  })
  const offRequest = agentCtx.on('agent/request', async (_payload, next) => {
    const resolved = await next()
    if (!assembled) return resolved
    // The inherited effort belongs to the session's first model; the host's choice (if any) replaces it.
    const { reasoningEffort: _inherited, ...rest } = resolved
    return { ...rest, provider: assembled.provider, model: assembled.model, ...(assembled.effort ? { reasoningEffort: assembled.effort } : {}) }
  })
  const offNotice = agentCtx.on('agent/pre-step', async ({ agent, messages, signal, step }, next) => {
    const decision = await next()
    if (decision.kind === 'reject' || signal.aborted) return decision
    if (decision.messages.length === 0 && (step === 1 || messages.length > 0)) return decision
    const previous = agent.session.requestHeader()?.config
    if (!assembled || !previous || previous.model === assembled.model) return decision
    const from = previous.model, to = assembled.model
    const notice = createUserMessage({
      content: [{ type: 'text', text: `[model changed: assistant turns above this point were generated by ${from}; the session continues with ${to}]` }],
      source: { kind: 'model-selection', form: 'notice', summary: boundContextSummary(`${from} → ${to}`) },
    })
    return { ...decision, messages: [...decision.messages, notice] }
  }, { prepend: true })
  return () => { offAssembly(); offRequest(); offNotice() }
}

/** How long a /stop waits for the turn's killed jobs to settle (so their end wakes no one). */
const KILL_WAIT_MS = 5_000

const sleep = (ms, signal) => new Promise((resolve) => {
  const t = setTimeout(resolve, ms)
  signal?.addEventListener('abort', () => { clearTimeout(t); resolve() }, { once: true })
})
const live = (status) => status === 'running' || status === 'stopping'

function bridge() {
  const url = process.env.AMB_BRIDGE_URL
  const token = process.env.AMB_BRIDGE_TOKEN
  if (!url || !token) return undefined
  return async (path, body, signal) => {
    const res = await fetch(url + path, {
      method: 'POST',
      headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
      body: JSON.stringify(body),
      signal,
    })
    if (!res.ok) throw new Error(`bridge ${path} ${res.status}`)
    return res.json()
  }
}

export function apply(ctx) {
  /** sessionId → { provider, model, effort? } chosen by the host for that conversation */
  const selections = new Map()
  /** agent id → ids of jobs its shell moved to the background (the model wanted their result) */
  const promoted = new Map()
  /** agent id → when its current activity started (jobs started since then belong to the turn) */
  const startedAt = new Map()
  /** sessions being cancelled: no steering into them */
  const cancelling = new Set()
  /** steered message ids (idempotent retries) */
  const delivered = new Set()

  const agents = () => ctx.get('agents')
  const jobs = () => ctx.get('jobs')

  // ---------- per-conversation model ----------
  ctx.on('agent/created', ({ agent }) => {
    const id = String(agent.id)
    installSelection(agent.ctx, () => selections.get(id))
  })

  ctx.on('agent/status', ({ agent, status }) => {
    const id = String(agent?.id ?? '')
    if (status === 'running') startedAt.set(id, Date.now())
    else cancelling.delete(id)
  })

  ctx.on('agent/disposed', ({ agent }) => {
    const id = String(agent?.id ?? '')
    promoted.delete(id)
    startedAt.delete(id)
    cancelling.delete(id)
  })

  // ---------- auto-backgrounded commands ----------
  ctx.on('tools/execute', async (exec, next) => {
    const result = await next()
    const value = result?.value
    if (value && value.kind === 'promoted' && value.jobId && exec?.agent) {
      const id = String(exec.agent.id)
      if (!promoted.has(id)) promoted.set(id, new Set())
      promoted.get(id).add(String(value.jobId))
    }
    return result
  })

  try {
    ctx.inject?.(['systemPrompt'], (c) => {
      c.systemPrompt.section({
        name: 'amb:background',
        order: c.systemPrompt.getSectionOrder('TOOL_JOBS'),
        text: 'A shell command still running after its timeout moves to a background job by itself. You may end your turn while it runs: tell the user it is still running. When it finishes you are woken with its result, and your reply reaches the user as a follow-up message, so do not poll or sleep on it. Start servers and other processes that should keep running with run_in_background: true.',
      })
    })
  } catch {}

  // ---------- commands ----------
  const steerable = (agent) => agent.status === 'running' && !cancelling.has(String(agent.id)) && !agent.phase?.abort?.signal?.aborted

  const pendingIds = (agent) => {
    try {
      return new Set([...(agent.inbox?.nextTurn ?? []), ...(agent.inbox?.nextStep ?? [])].map((m) => m.id))
    } catch {
      return new Set()
    }
  }

  async function updateProviders(providers) {
    const loader = ctx.get('loader')
    const entry = loader && [...loader.entries()].find((e) => e.options?.id === 'llm-pi-ai')
    if (!entry) throw new Error('llm-pi-ai entry not found')
    await entry.update({ config: { ...(entry.options.config ?? {}), providers } })
    // a refused volatile update is only logged by the loader: check that every route resolves now
    const llm = ctx.get('llm')
    for (const [provider, profile] of Object.entries(providers)) {
      for (const m of profile.models ?? []) await llm.resolveCallConfig({ provider, model: m.id })
    }
  }

  /** jobs owned by the session that this turn started or auto-backgrounded, still running */
  function turnJobs(agent) {
    const registry = jobs()
    if (!registry) return []
    const id = String(agent.id)
    const since = startedAt.get(id) ?? Infinity
    const mine = promoted.get(id) ?? new Set()
    return registry.list(id).filter((j) => j.owner === id && live(j.status) && (j.startedAt >= since || mine.has(j.id)))
  }

  async function killTurnJobs(agent) {
    const registry = jobs()
    const id = String(agent.id)
    const list = turnJobs(agent)
    // waits first, so each end counts as collected and tool-jobs injects no notice
    const waits = list.map((j) => registry.wait(j.id, KILL_WAIT_MS, id).catch(() => undefined))
    for (const j of list) try { registry.kill(j.id, id, 'stopped by the user') } catch {}
    await Promise.all(waits)
  }

  function descendants(agent) {
    const registry = agents()
    const out = []
    const walk = (parent) => {
      for (const a of registry.list()) {
        if (a !== parent && registry.isOwnedBy?.(a.id, parent)) {
          out.push(a)
          walk(a)
        }
      }
    }
    walk(agent)
    return out
  }

  const handlers = {
    async ping() {
      return {}
    },
    async select({ sessionId, provider, model, effort }) {
      if (provider && model) selections.set(String(sessionId), { provider: String(provider), model: String(model), ...(effort ? { effort: String(effort) } : {}) })
      return {}
    },
    async providers({ providers }) {
      await updateProviders(providers)
      return {}
    },
    async steer({ sessionId, messageId, text, images }) {
      const id = String(messageId ?? '')
      if (!id) return { accepted: false, reason: 'no message id' }
      const agent = agents()?.get(String(sessionId))
      if (!agent) return { accepted: false, reason: 'no agent' }
      if (delivered.has(id) || pendingIds(agent).has(id)) return { accepted: true, duplicate: true }
      if (!steerable(agent)) return { accepted: false, reason: 'idle' }
      const content = [{ type: 'text', text: String(text ?? '') }]
      if (Array.isArray(images) && images.length) {
        const attachments = ctx.get('attachments')
        if (attachments) {
          const refs = await admitEncodedImages(attachments, images.map((i) => ({ data: i.data, mediaType: i.mimeType })))
          for (const ref of refs) content.push({ type: 'image', attachment: ref })
        }
      }
      // re-check after the await: steering into an idle agent would start a turn nobody listens to
      if (agents()?.get(String(sessionId)) !== agent || !steerable(agent)) return { accepted: false, reason: 'idle' }
      agent.steer(freezeMessage({ id: String(id), role: 'user', content, source: { kind: 'user' } }))
      delivered.add(id)
      if (delivered.size > 1000) delivered.delete(delivered.values().next().value)
      return { accepted: true }
    },
    // the host recycles an idle child (the SDK cannot close a session, so each one stays until the child
    // restarts): is anything still going on in it? Agents that are not idle or have queued input, and
    // running shell jobs, whoever owns them.
    async idle() {
      const list = agents()?.list?.() ?? []
      const registry = jobs()
      const running = new Set()
      for (const a of list) {
        try {
          for (const j of registry?.list?.(String(a.id)) ?? []) if (live(j.status)) running.add(String(j.id))
        } catch {}
      }
      const busy = list.filter((a) => a.status !== 'idle' || pendingIds(a).size > 0).length
      return { agents: list.length, busy, jobs: running.size }
    },
    async cancel({ sessionId }) {
      const agent = agents()?.get(String(sessionId))
      if (!agent) return { running: false }
      const running = agent.status === 'running'
      if (running) cancelling.add(String(agent.id))
      for (const child of descendants(agent)) try { child.cancel({ kind: 'parent' }) } catch {}
      agent.cancel({ kind: 'user' }, { keepInbox: false })
      void killTurnJobs(agent).catch(() => {})
      return { running }
    },
  }

  // ---------- long poll ----------
  const call = bridge()
  const controlId = process.env.AMB_CONTROL_TOKEN
  if (!call || !controlId) return
  const botId = process.env.AMB_BOT_ID ?? ''
  const stop = new AbortController()
  try {
    ctx.on('dispose', () => stop.abort())
  } catch {}

  const run = async (cmd) => {
    let out
    try {
      const fn = handlers[cmd.op] ?? globalThis[Symbol.for('amb.control.handlers')]?.[cmd.op]
      if (!fn) throw new Error(`unknown command ${cmd.op}`)
      out = { ok: true, result: await fn(cmd) }
    } catch (e) {
      out = { ok: false, error: String(e?.message ?? e).slice(0, 500) }
    }
    await call('/dsh/control/result', { botId, controlId, id: cmd.id, ...out }).catch(() => {})
  }

  void (async () => {
    let backoff = 250
    while (!stop.signal.aborted) {
      let res
      try {
        res = await call('/dsh/control/poll', { botId, controlId }, stop.signal)
        backoff = 250
      } catch {
        if (stop.signal.aborted) return
        await sleep(backoff, stop.signal)
        backoff = Math.min(backoff * 2, 5_000)
        continue
      }
      if (res?.stop) return
      for (const cmd of res?.commands ?? []) void run(cmd)
    }
  })()
}