Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

2.7 KB
Raw Blame
import type { Hook, Register } from 'claude-code'

import { answer } from './command'
import { verdict } from './guard'
import { modeOf, readOptions, type ModOptions } from './options'
import { newStats, STATUS_PATH, statusText, type Stats } from './status'
import { findTool } from './tools'

const TEXT_CAP = 20_000

type Dollar = Parameters<Hook<'session.start'>>[0]

/** Everything one session of the mod keeps: its settings, its counters and the project root. */
type Session = { readonly opts: ModOptions; readonly stats: Stats; root?: string }

async function flush($: Dollar, s: Session): Promise<void> {
  if (s.root === undefined) return
  try {
    await $.fs.write(`${s.root}/${STATUS_PATH}`, statusText(s.stats, modeOf(s.opts), await $.clock.now()))
  } catch {
    /* the status file is a courtesy */
  }
}

/** A read-only call through an already-connected tool: its first text block, or undefined when absent or errored. */
async function peek($: Dollar, suffix: string, args: Record<string, unknown>): Promise<string | undefined> {
  const hit = findTool(await $.tool.list(), suffix)
  if (!hit) return undefined
  const res = await $.mcp.call(hit.server, hit.tool, args)
  return res.isError ? undefined : (res.content.find(b => b.type === 'text')?.text ?? '').slice(0, TEXT_CAP)
}

/**
 * Intelligence as a mod (ADR-445 pattern): a tighten-only guard on this plugin's own tools, `/intelligence-mod`, and a status file the console
 * reads. No network, no process spawning: only tools already connected.
 */
export const register: Register = (on, options) => {
  const s: Session = { opts: readOptions(options), stats: newStats() }

  on('session.start', async ($, e, next) => {
    const result = await next(e)
    s.root = (await $.session.root()) as string | undefined
    try {
      await $.command.register({ name: 'intelligence-mod', description: 'Intelligence mod: status, scan <text>, stats' })
    } catch {
      /* a name taken by another plugin must not stop the mod */
    }
    await flush($, s)
    return result
  })

  if (s.opts.guard) {
    on('tool.call', async ($, e, next) => {
      const reason = verdict(e.tool, e, s.opts)
      if (reason === undefined) return next(e)
      s.stats.blocked++
      s.stats.lastDenied = e.tool
      await flush($, s)
      return { deny: reason }
    })
  }

  /** `/intelligence-mod` is answered locally and takes no model turn. */
  on('command.run', { command: 'intelligence-mod' }, async ($, e) => {
    s.stats.commands++
    const text = await answer(typeof e.args === 'string' ? e.args : '', {
      opts: s.opts,
      stats: s.stats,
      peek: (suffix, args) => peek($, suffix, args),
      toolNames: async () => (await $.tool.list()).map(t => t.name),
    })
    await flush($, s)
    return { text }
  })
}