Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.6 KB
Raw Blame
import type { PrismaClient } from '@prisma/client'
import type { AuthUser } from '../../src/lib/ssoAuth.js'
import { superTabRoutes } from '../../src/routes/superTabs.js'
import { SuperTabStore } from '../../src/superTabs/store.js'
import type { CommandResult, Reader, Snapshot, SwarmDriver } from './driver.js'
import { benchApp, caller, type Call } from './http.js'

const base = '/api/super-tabs'

/** The Super Tab routes on one store, in process. Drivers that share them share the store and its in-process state. */
export interface StoreRoutes { call: Call; close(): Promise<void> }

export async function storeRoutes(db: PrismaClient, users: () => readonly AuthUser[]): Promise<StoreRoutes> {
  const store = new SuperTabStore(db)
  // No GitHub App: the bench never reaches GitHub.
  const app = await benchApp(users, async scope => superTabRoutes(scope, { store, github: null }))
  return { call: caller(app), close: () => app.close() }
}

/** Today's store: one `SuperTab` document per Swarm, read in full by every poll (store.ts, routes/superTabs.ts). On the
 *  store by parts its full reads answer 426, so `partsDriver` keeps only its writes. */
export async function legacyDriver(db: PrismaClient, users: () => readonly AuthUser[], routes?: StoreRoutes): Promise<SwarmDriver> {
  const { call, close } = routes ?? await storeRoutes(db, users)
  const reader = (label: string, user: AuthUser, url: string): Reader => ({ label, poll: () => call(user, 'GET', url) })
  return {
    name: 'legacy',
    close,
    createSwarm: (owner, operationId, name) => call(owner, 'POST', base, { operationId, name }),
    execute: (tabId, user, operationId, command, asAgentId) =>
      call<CommandResult>(user, 'POST', `${base}/${tabId}/commands`, { operationId, command, ...(asAgentId ? { asAgentId } : {}) }),
    async snapshot(tabId, user) {
      const reply = await call<{ state: Snapshot }>(user, 'GET', `${base}/${tabId}`)
      if (!reply.data) throw new Error(`Snapshot failed: ${reply.code}`)
      return reply.data.state
    },
    async machineGeneration(tabId, host, machineId) {
      const reply = await call<Array<{ tabId: string; machine: { generation: string } }>>(host, 'GET', `${base}/hosts/${machineId}/pools`)
      const row = reply.data?.find(r => r.tabId === tabId)
      if (!row) throw new Error(`No contribution of ${machineId}: ${reply.code}`)
      return row.machine.generation
    },
    observeMachine: (tabId, host, machineId, observation) => call(host, 'POST', `${base}/${tabId}/machines/${machineId}`, observation),
    openNative: (tabId, opener, input) => call(opener, 'POST', `${base}/${tabId}/native`, input),
    appendNative: (tabId, host, contextId, event) => call(host, 'POST', `${base}/${tabId}/native/${contextId}`, event),
    deliveries: (tabId, host, agentId) => call(host, 'GET', `${base}/${tabId}/agents/${agentId}/deliveries`),
    delivery: (tabId, host, agentId, deliveryId, action) => call(host, 'POST', `${base}/${tabId}/agents/${agentId}/deliveries/${deliveryId}`, action),
    desktopReader: (tabId, person) => reader(`desktop:${person.email}`, person, `${base}/${tabId}`),
    hostReader: (host, machineId) => reader(`host:${machineId}`, host, `${base}/hosts/${machineId}`),
    async stateSize(tabId) {
      const result = await db.$runCommandRaw({ aggregate: 'SuperTab', cursor: {},
        pipeline: [{ $match: { _id: tabId } }, { $project: { size: { $bsonSize: '$$ROOT' } } }] }) as { cursor: { firstBatch: Array<{ size: number }> } }
      const rows = result.cursor.firstBatch
      return { bytes: rows.reduce((sum, r) => sum + r.size, 0), documents: rows.length }
    },
  }
}