All files / kernel-agents-repl/src/strategies/repl prepare-attempt.ts

0% Statements 0/16
0% Branches 0/5
0% Functions 0/2
0% Lines 0/16

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                               
import { mergeDisjointRecords } from '@metamask/kernel-utils';
import type { Logger } from '@metamask/logger';
import { extractCapabilitySchemas } from '@ocap/kernel-agents/capabilities/capability';
import { makeEnd } from '@ocap/kernel-agents/capabilities/end';
import type {
  PREP,
  Objective,
  Context,
  CapabilityRecord,
  Progress,
  PrepareAttempt,
} from '@ocap/kernel-agents/types';
import { ifDefined } from '@ocap/kernel-agents/utils';
 
import { makeEvaluator } from './evaluator.ts';
import type { State, Observation, Action } from './messages.ts';
import {
  InterjectionMessage,
  ImportMessage,
  ResultMessage,
} from './messages.ts';
import { makePrinter } from './printer.ts';
import { makePrompter } from './prompter.ts';
import { makeReader } from './reader.ts';
 
export const prepareAttempt: PrepareAttempt<State, Action, Observation> = <
  Result,
>({
  objective: { intent, judgment },
  context,
  options = {},
}: {
  objective: Objective<Result>;
  context: Context;
  options?: {
    seed?: number;
    tokenLength?: number;
    taskLogger?: Logger;
    printLogger?: Logger;
  };
}): [PREP<State, Action, Observation>, Progress<Result, State>] => {
  const { seed, tokenLength, taskLogger, printLogger } = options;
 
  const [end, didEnd, getEnd] = makeEnd();
 
  const capabilities = mergeDisjointRecords(context.capabilities, {
    end,
  }) as CapabilityRecord;
 
  const history = [
    new InterjectionMessage(intent),
    ImportMessage.fromNames(Object.keys(capabilities)),
    new ResultMessage({ value: extractCapabilitySchemas(capabilities) }),
  ];
 
  const progress: Progress<Result, State> = {
    history,
    isDone: () => {
      if (didEnd()) {
        const result = getEnd();
        if (!judgment(result)) {
          throw new Error(`Invalid result: ${result as string}`, {
            cause: result,
          });
        }
        Object.assign(progress, { result });
        return true;
      }
      return false;
    },
    // result: not defined until judgment is satisfied
  };
 
  const readLogger = taskLogger?.subLogger({ tags: ['read'] });
  const evalLogger = taskLogger?.subLogger({ tags: ['eval'] });
 
  return [
    [
      makePrompter(ifDefined({ seed, tokenLength })),
      makeReader(ifDefined({ logger: readLogger })),
      makeEvaluator(ifDefined({ capabilities, logger: evalLogger })),
      makePrinter({ history, ...ifDefined({ logger: printLogger }) }),
      // TODO: Fix these types
    ] as unknown as PREP<State, Action, Observation>,
    progress,
  ];
};