All files / src/flow/nodes split_by_llm.ts

98.62% Statements 143/145
78.37% Branches 29/37
100% Functions 4/4
98.62% Lines 143/145

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146132x 132x 132x 2x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 8x 8x 8x 8x   8x 8x 8x 8x 8x   8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 8x 1x 1x 1x 8x 8x 8x 8x 8x 8x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 14x 14x 14x 14x 14x 14x 14x 14x 13x 13x 14x 14x 14x 14x 13x 14x 14x 13x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 133x 132x 132x 132x 132x 132x 136x 136x 136x 136x 136x 136x 136x 132x 132x 132x 132x 132x 132x 132x 132x  
import { ACTION_GROUPS, FormData, NodeConfig, FlowTypes } from '../types';
import { CallLLM, Node } from '../../store/flow-definition';
import { generateUUID, createSuccessFailureRouter } from '../../utils';
import { html } from 'lit';
import {
  renderClamped,
  renderHighlightedText,
  renderLineItem,
  getLlmIcon
} from '../utils';
import { LLMModel, hasLLMRole } from '../flow-utils';
 
export const split_by_llm: NodeConfig = {
  type: 'split_by_llm',
  name: 'Call AI',
  group: ACTION_GROUPS.services,
  flowTypes: [FlowTypes.VOICE, FlowTypes.MESSAGE, FlowTypes.BACKGROUND],
  showAsAction: true,
  render: (node: Node) => {
    const callLlmAction = node.actions?.find(
      (action) => action.type === 'call_llm'
    ) as CallLLM;
    const instructions =
      callLlmAction?.instructions || 'Configure AI instructions';
    const llmName = callLlmAction?.llm?.name;
    return html`
      ${llmName
        ? html`<div class="body" style="padding-bottom:0;">
            ${renderLineItem(llmName, getLlmIcon(llmName))}
          </div>`
        : null}
      <div class="body" style="margin-bottom:10px;">
        ${renderClamped(
          renderHighlightedText(instructions, true),
          instructions
        )}
      </div>
    `;
  },
 
  form: {
    llm: {
      type: 'select',
      label: 'LLM',
      required: true,
      options: [],
      endpoint: '/api/internal/llms.json',
      searchable: true,
      valueKey: 'uuid',
      nameKey: 'name',
      placeholder: 'Select an LLM...',
      shouldExclude: (option: LLMModel) => !hasLLMRole(option, 'engine')
    },
    input: {
      type: 'text',
      label: 'Input',
      helpText: 'The input the AI will process',
      required: true,
      evaluated: true,
      maxLength: 10000,
      placeholder: '@input'
    },
    instructions: {
      type: 'textarea',
      label: 'Instructions',
      helpText:
        'Tell the AI what to do with the input. The result can be referenced as **`@locals._llm_output`**',
      required: true,
      evaluated: true,
      maxLength: 10000,
      placeholder: 'Enter instructions for the AI model...',
      minHeight: 130
    }
  },
  layout: ['llm', 'input', 'instructions'],
  toFormData: (node: Node) => {
    // Extract data from the existing node structure
    const callLlmAction = node.actions?.find(
      (action) => action.type === 'call_llm'
    ) as CallLLM;
 
    return {
      uuid: node.uuid,
      llm: callLlmAction?.llm ? [callLlmAction.llm] : [],
      input: callLlmAction?.input || '@input',
      instructions: callLlmAction?.instructions || ''
    };
  },
  fromFormData: (formData: FormData, originalNode: Node): Node => {
    // Get LLM selection
    const llmSelection =
      Array.isArray(formData.llm) && formData.llm.length > 0
        ? formData.llm[0]
        : null;
 
    // Find existing call_llm action to preserve its UUID
    const existingCallLlmAction = originalNode.actions?.find(
      (action) => action.type === 'call_llm'
    );
    const callLlmUuid = existingCallLlmAction?.uuid || generateUUID();
 
    // Create call_llm action
    const callLlmAction: CallLLM = {
      type: 'call_llm',
      uuid: callLlmUuid,
      llm: llmSelection
        ? {
            uuid: llmSelection.uuid || llmSelection.value,
            name: llmSelection.name
          }
        : { uuid: '', name: '' },
      input: formData.input || '@input',
      instructions: formData.instructions || '',
      output_local: '_llm_output'
    };
 
    // Create categories and exits for Success and Failure
    const existingCategories = originalNode.router?.categories || [];
    const existingExits = originalNode.exits || [];
    const existingCases = originalNode.router?.cases || [];
 
    const { router, exits } = createSuccessFailureRouter(
      '@locals._llm_output',
      {
        type: 'has_text',
        arguments: []
      },
      existingCategories,
      existingExits,
      existingCases
    );
 
    // Return the complete node
    return {
      uuid: originalNode.uuid,
      actions: [callLlmAction],
      router: router,
      exits: exits
    };
  },
 
  // Localization support for categories
  localizable: 'categories',
  nonTranslatableCategories: 'all'
};