File size: 1,700 Bytes
dd480ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Intent Interpreter Agent — UPGRADED
 * Deeply understands user's workflow request using provider-agnostic LLM Gateway
 * NO direct OpenAI dependency
 */
import { type LLMGateway } from '@wfo/integrations/llm-providers/index';
import type { WorkflowIntent } from '../types/workflow';
import { INTENT_INTERPRETER_PROMPT } from '../prompts/intentInterpreter';

export class IntentInterpreterAgent {
  private llm: LLMGateway;

  constructor(llm: LLMGateway) {
    this.llm = llm;
  }

  async interpret(userRequest: string): Promise<WorkflowIntent> {
    const raw = await this.llm.completeJSON<Partial<WorkflowIntent>>([
      { role: 'system', content: INTENT_INTERPRETER_PROMPT },
      {
        role: 'user',
        content: `Analyse this workflow request and return structured JSON:\n\n${userRequest}`,
      },
    ], {
      temperature: 0.1,
      retries: 3,
    });

    return this.validateAndNormalize(raw);
  }

  private validateAndNormalize(raw: Partial<WorkflowIntent>): WorkflowIntent {
    return {
      workflowType: raw.workflowType ?? 'general',
      requiresAI: raw.requiresAI ?? false,
      integrations: Array.isArray(raw.integrations) ? raw.integrations : [],
      riskLevel: raw.riskLevel ?? 'medium',
      requiresHumanApproval: raw.requiresHumanApproval ?? true,
      syncVsAsync: raw.syncVsAsync ?? 'sync',
      scalingRequirements: raw.scalingRequirements ?? 'low',
      identifiedRisks: Array.isArray(raw.identifiedRisks) ? raw.identifiedRisks : [],
      estimatedComplexity: raw.estimatedComplexity ?? 'moderate',
      domain: raw.domain ?? 'general',
      triggerType: raw.triggerType ?? 'webhook',
      triggerHint: raw.triggerHint,
    };
  }
}