Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.7 KB
Raw Blame
import fs from 'node:fs'
import { execFileSync } from 'node:child_process'
import { dirname, resolve } from 'node:path'
import assert from 'node:assert/strict'
import { build } from 'esbuild'
const file = 'src/shared/native-chat-tool-summary.ts'
const currentFile = 'src/shared/native-chat/tools/native-chat-tool-summary.ts'
const baseline = process.argv[2] ?? '20ab9950654'
const beforeSource = execFileSync('git', ['show', `${baseline}:${file}`], {
  encoding: 'utf8',
  windowsHide: true
})
const afterSource = fs.readFileSync(currentFile, 'utf8')
async function load(contents, sourceFile = file) {
  const { outputFiles } = await build({
    stdin: { contents, loader: 'ts', resolveDir: dirname(resolve(sourceFile)) },
    bundle: true,
    platform: 'node',
    format: 'esm',
    write: false,
    plugins: [
      {
        name: 'relocated-current-preview-dependencies',
        setup(builder) {
          builder.onResolve({ filter: /^\./ }, (args) => {
            const target = resolve(args.resolveDir, args.path)
            for (const name of ['native-chat-tool-preview-prefix', 'native-chat-tool-identity']) {
              const previous = resolve('src/shared', name)
              if (target === previous || target === `${previous}.ts`) {
                return { path: resolve('src/shared/native-chat/tools', `${name}.ts`) }
              }
            }
            return undefined
          })
        }
      }
    ]
  })
  return import(
    `data:text/javascript;base64,${Buffer.from(outputFiles[0].text).toString('base64')}`
  )
}
const before = await load(beforeSource),
  after = await load(afterSource, currentFile)
const display = (m, input) => {
  const d = m.createToolInputDisplay(input)
  return { ...d, formatDetail: d.formatDetail() }
}
let seed = 8121
const random = (max) => {
  seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0
  return Math.floor((seed / 4294967296) * max)
}
const pieces = [
  'a',
  'b',
  '…',
  '😀',
  '\ud800',
  '\udc00',
  '\0',
  '\t',
  '\r',
  '\n',
  ' ',
  '\v',
  '\f',
  '\u00a0',
  '\u1680',
  '\u2000',
  '\u200a',
  '\u2028',
  '\u2029',
  '\u202f',
  '\u205f',
  '\u3000',
  '\ufeff',
  '\u0085',
  '\u200b'
]
for (let i = 0; i < 3000; i++) {
  const input = Array.from({ length: random(500) }, () => pieces[random(pieces.length)]).join('')
  assert.equal(after.summarizeToolInput(input), before.summarizeToolInput(input))
  assert.deepEqual(display(after, input), display(before, input))
}
for (const input of [
  `${'a'.repeat(79)}…`,
  'a'.repeat(80) + ' '.repeat(500),
  ' '.repeat(100000),
  `${'\n'.repeat(100000)}x`,
  { command: 'a '.repeat(50000) },
  { file_path: 'a '.repeat(500) },
  { x: 'a '.repeat(500) },
  JSON.stringify({ command: 'a '.repeat(50000) })
]) {
  assert.deepEqual(display(after, input), display(before, input))
}
for (const [caseName, input] of [
  ['tiny', 'ls -la'],
  ['100KB', 'a   b\n\t'.repeat(15000)],
  ['1MB', 'a   b\n\t'.repeat(150000)],
  ['all-space', ' '.repeat(1000000)],
  ['leading', `${' '.repeat(1000000)}x`],
  ['trailing', `x${' '.repeat(1000000)}`],
  ['long-word', 'x'.repeat(1000000)]
]) {
  const samples = { before: [], after: [] }
  for (let i = 0; i < 20; i++) {
    before.createToolInputDisplay(input)
    after.createToolInputDisplay(input)
  }
  for (let r = 0; r < 8; r++) {
    for (const [label, m] of r % 2
      ? [
          ['after', after],
          ['before', before]
        ]
      : [
          ['before', before],
          ['after', after]
        ]) {
      global.gc()
      const start = process.cpuUsage()
      for (let i = 0; i < 20; i++) {
        m.createToolInputDisplay(input)
      }
      const cpu = process.cpuUsage(start)
      samples[label].push((cpu.user + cpu.system) / 20000)
    }
  }
  console.log(JSON.stringify({ caseName, samples }))
}