All files / kernel-agents/src/capabilities capability.ts

75% Statements 6/8
100% Branches 0/0
60% Functions 3/5
75% Lines 6/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                              6x     18x                 6x           5x   8x                     6x                      
import type { ExtractRecordKeys } from '../types/capability.ts';
import type {
  CapabilityRecord,
  CapabilitySpec,
  CapabilitySchema,
  Capability,
} from '../types.ts';
 
/**
 * Create a capability specification.
 *
 * @param func - The function to create a capability specification for
 * @param schema - The schema for the capability
 * @returns A capability specification
 */
export const capability = <Args extends Record<string, unknown>, Return = null>(
  func: Capability<Args, Return>,
  schema: CapabilitySchema<ExtractRecordKeys<Args>>,
): CapabilitySpec<Args, Return> => ({ func, schema });
 
type SchemaEntry = [string, { schema: CapabilitySchema<string> }];
/**
 * Extract only the serializable schemas from the capabilities
 *
 * @param capabilities - The capabilities to extract the schemas from
 * @returns A record mapping capability names to their schemas
 */
export const extractCapabilitySchemas = (
  capabilities: CapabilityRecord,
): Record<
  keyof typeof capabilities,
  (typeof capabilities)[keyof typeof capabilities]['schema']
> =>
  Object.fromEntries(
    (Object.entries(capabilities) as unknown as SchemaEntry[]).map(
      ([name, { schema }]) => [name, schema],
    ),
  );
 
type CapabilityEntry = [string, CapabilitySpec<never, unknown>];
/**
 * Extract only the functions from the capabilities
 *
 * @param capabilities - The capabilities to extract the functions from
 * @returns A record mapping capability names to their functions
 */
export const extractCapabilities = (
  capabilities: CapabilityRecord,
): Record<
  keyof typeof capabilities,
  (typeof capabilities)[keyof typeof capabilities]['func']
> =>
  Object.fromEntries(
    (Object.entries(capabilities) as unknown as CapabilityEntry[]).map(
      ([name, { func }]) => [name, func],
    ),
  );