mirror of
https://github.com/stablyai/orca.git
synced 2026-10-06 16:02:25 +00:00
src/cli/index.ts was the only eager value-import of RuntimeClient, and five other eager modules imported just RuntimeClientError / RuntimeRpcFailureError from the runtime-client barrel -- dragging in client -> pairing -> zod -> ws -> e2ee on every invocation. Those error classes live in runtime/types.ts, which has zero children, so the five imports now point there and the client loads through the existing (already lazy by design) ctx.client getter. Eager modules 199 -> 46, with node_modules dropping 94 -> 0. `orca --help` 2.04x (59.6 -> 29.2 ms); the same for help, no-args, and both error paths, which return before constructing a client. Commands that DO construct one still gain 1.10-1.12x from not eagerly parsing the transport the local path never uses. Correction to an earlier note: websocket-transport alone is ~24 modules / ~8 ms, not the 107 / 28 ms once recorded -- that figure wrongly charged it for zod, which enters through shared/pairing on a different edge. Marginal cost, never isolated cost. Co-authored-by: Orca <help@stably.ai>
118 lines
3.3 KiB
TypeScript
118 lines
3.3 KiB
TypeScript
import { RuntimeClientError } from './runtime/types'
|
|
import { REPEATED_FLAG_SEPARATOR } from './args'
|
|
|
|
export function getRequiredStringFlag(flags: Map<string, string | boolean>, name: string): string {
|
|
const value = flags.get(name)
|
|
if (typeof value === 'string' && value.length > 0) {
|
|
return value
|
|
}
|
|
throw new RuntimeClientError('invalid_argument', `Missing required --${name}`)
|
|
}
|
|
|
|
export function getRequiredStringFlagAllowingEmpty(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): string {
|
|
const value = flags.get(name)
|
|
if (typeof value === 'string') {
|
|
return value
|
|
}
|
|
throw new RuntimeClientError('invalid_argument', `Missing required --${name}`)
|
|
}
|
|
|
|
export function getOptionalStringFlag(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): string | undefined {
|
|
const value = flags.get(name)
|
|
return typeof value === 'string' && value.length > 0 ? value : undefined
|
|
}
|
|
|
|
export function getRepeatedStringFlag(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): string[] {
|
|
const value = getOptionalStringFlag(flags, name)
|
|
return value === undefined
|
|
? []
|
|
: value.split(REPEATED_FLAG_SEPARATOR).filter((entry) => entry.length > 0)
|
|
}
|
|
|
|
export function getOptionalNumberFlag(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): number | undefined {
|
|
const value = flags.get(name)
|
|
if (typeof value !== 'string' || value.length === 0) {
|
|
return undefined
|
|
}
|
|
const parsed = Number(value)
|
|
if (!Number.isFinite(parsed)) {
|
|
throw new RuntimeClientError('invalid_argument', `Invalid numeric value for --${name}`)
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
export function getOptionalPositiveIntegerFlag(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): number | undefined {
|
|
const value = getOptionalNumberFlag(flags, name)
|
|
if (value === undefined) {
|
|
return undefined
|
|
}
|
|
if (!Number.isInteger(value) || value <= 0) {
|
|
throw new RuntimeClientError('invalid_argument', `Invalid positive integer for --${name}`)
|
|
}
|
|
return value
|
|
}
|
|
|
|
export function getOptionalNonNegativeIntegerFlag(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): number | undefined {
|
|
const value = getOptionalNumberFlag(flags, name)
|
|
if (value === undefined) {
|
|
return undefined
|
|
}
|
|
if (!Number.isInteger(value) || value < 0) {
|
|
throw new RuntimeClientError('invalid_argument', `Invalid non-negative integer for --${name}`)
|
|
}
|
|
return value
|
|
}
|
|
|
|
export function getRequiredPositiveNumber(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): number {
|
|
const raw = getRequiredStringFlag(flags, name)
|
|
const value = Number(raw)
|
|
if (!Number.isFinite(value) || value <= 0) {
|
|
throw new RuntimeClientError('invalid_argument', `--${name} must be a positive number`)
|
|
}
|
|
return value
|
|
}
|
|
|
|
export function getRequiredFiniteNumber(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): number {
|
|
const raw = getRequiredStringFlag(flags, name)
|
|
const value = Number(raw)
|
|
if (!Number.isFinite(value)) {
|
|
throw new RuntimeClientError('invalid_argument', `--${name} must be a valid number`)
|
|
}
|
|
return value
|
|
}
|
|
|
|
export function getOptionalNullableNumberFlag(
|
|
flags: Map<string, string | boolean>,
|
|
name: string
|
|
): number | null | undefined {
|
|
const value = flags.get(name)
|
|
if (value === 'null') {
|
|
return null
|
|
}
|
|
return getOptionalNumberFlag(flags, name)
|
|
}
|