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

100% Statements 9/9
50% Branches 1/2
100% Functions 4/4
100% Lines 7/7

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                      2x 6x 6x               2x 2x                                       6x           2x  
import { ifDefined } from '../utils.ts';
import { capability } from './capability.ts';
 
/**
 * A factory function to make a task's `end` capability, which stores the first
 * invocation as the final result and ignores all subsequent invocations.
 *
 * @template Result - The expected type of the final result.
 * @returns A tuple containing the end capability, a function to check if the end capability was invoked, and a function to get the final result.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const makeEnd = <Result>() => {
  const result: { final?: Result; attachments?: Record<string, unknown> } = {};
  const end = capability(
    async ({
      final,
      attachments,
    }: {
      final: Result;
      attachments?: Record<string, unknown>;
    }): Promise<void> => {
      Eif (!Object.hasOwn(result, 'final')) {
        Object.assign(result, { final, ...ifDefined({ attachments }) });
      }
    },
    {
      description: 'Return a final response to the user.',
      args: {
        final: {
          required: true,
          type: 'string',
          description:
            'A concise final response that restates the requested information.',
        },
        attachments: {
          required: false,
          type: 'object',
          description: 'Attachments to the final response.',
        },
      },
    },
  );
  return [end, () => 'final' in result, () => result.final as Result] as const;
};
 
/**
 * A default `end` capability that does nothing.
 */
export const [end] = makeEnd();