• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gregreindel / llm-exe / 14940659507

10 May 2025 01:47AM UTC coverage: 99.262%. First build
14940659507

Pull #61

github

web-flow
Merge e0149bd7c into a88aa8a8a
Pull Request #61: Update defaults

927 of 937 branches covered (98.93%)

Branch coverage included in aggregate %.

41 of 42 new or added lines in 16 files covered. (97.62%)

2167 of 2180 relevant lines covered (99.4%)

29.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.08
/src/executor/llm.ts
1
import {
2
  BaseLlm,
3
  PromptInput,
4
  ParserOutput,
5
  CoreExecutorExecuteOptions,
6
  ExecutorWithLlmOptions,
7
  ExecutorExecutionMetadata,
8
  LlmExecutorHooks,
9
  LlmExecutorExecuteOptions,
10
  BaseLlCall,
11
} from "@/types";
12
import { BaseParser, JsonParser, StringParser } from "@/parser";
8✔
13
import { BasePrompt } from "@/prompt";
14
import { BaseState } from "@/state";
15
import { BaseExecutor } from "./_base";
8✔
16
import { isPromise } from "@/utils/modules/isPromise";
8✔
17

18
/**
19
 * Core Executor With LLM
20
 */
21
export class LlmExecutor<
8✔
22
  Llm extends BaseLlm<any>,
23
  Prompt extends BasePrompt<Record<string, any>>,
24
  Parser extends BaseParser,
25
  State extends BaseState
26
> extends BaseExecutor<
27
  PromptInput<Prompt>,
28
  ParserOutput<Parser>,
29
  LlmExecutorHooks
30
> {
31
  public llm;
17✔
32
  public prompt;
17✔
33
  public promptFn: any;
17✔
34
  public parser;
17✔
35

36
  constructor(
37
    llmConfiguration: ExecutorWithLlmOptions<Llm, Prompt, Parser, State>,
38
    options?: CoreExecutorExecuteOptions<LlmExecutorHooks>
39
  ) {
40
    super(
17✔
41
      llmConfiguration.name || "anonymous-llm-executor",
34✔
42
      "llm-executor",
43
      options
44
    );
45

46
    this.llm = llmConfiguration.llm;
17✔
47
    this.parser = llmConfiguration?.parser || new StringParser();
17✔
48

49
    if (typeof llmConfiguration.prompt === "function") {
17✔
50
      this.prompt = undefined;
1✔
51
      this.promptFn = llmConfiguration.prompt;
1✔
52
    } else {
53
      this.prompt = llmConfiguration.prompt;
16✔
54
      this.promptFn = null;
16✔
55
    }
56
  }
57

58
  async execute(
59
    _input: PromptInput<Prompt> = {} as PromptInput<Prompt>,
×
60
    _options?: LlmExecutorExecuteOptions
61
  ): Promise<ParserOutput<Parser>> {
62
    if (this?.parser instanceof JsonParser && this.parser.schema) {
14✔
63
      _options = Object.assign(_options || {}, {
2✔
64
        jsonSchema: this.parser.schema,
65
      });
66
    }
67
    return super.execute(_input, _options);
14✔
68
  }
69

70
  async handler(_input: PromptInput<Prompt>, ..._args: any[]) {
71
    const call = await this.llm.call(_input, ..._args);
13✔
72
    return call;
13✔
73
  }
74

75
  async getHandlerInput(_input: PromptInput<Prompt>): Promise<any> {
76
    if (this.prompt) {
14✔
77
      if(isPromise(this.prompt.formatAsync)){
12✔
78
        return await this.prompt.formatAsync(_input);
11✔
79
      }else {
80
        return this.prompt.format(_input);
1✔
81
      }
82
    }
83

84
    if (this.promptFn) {
2✔
85
      const prompt = this.promptFn(_input);
1✔
86
      if(isPromise(prompt.formatAsync)){
1!
87
        return await prompt.formatAsync(_input);
1✔
88
      }else {
NEW
89
        return prompt.format(_input);
×
90
      }
91
    }
92
    throw new Error("Missing prompt");
1✔
93
  }
94

95
  getHandlerOutput(
96
    out: BaseLlCall,
97
    _metadata: ExecutorExecutionMetadata<
98
      PromptInput<Prompt>,
99
      ParserOutput<Parser>
100
    >
101
  ): ParserOutput<Parser> {
102
    // depending on out parser type, and result obj (out)
103
    // we should use different methods here
104
    if (this.parser.target === "function_call") {
13✔
105
      const outToStr = out.getResultContent();
2✔
106
      return this.parser.parse(outToStr, _metadata);
2✔
107
    } else {
108
      const outToStr = out.getResultText();
11✔
109
      return this.parser.parse(outToStr, _metadata);
11✔
110
    }
111
  }
112

113
  metadata() {
114
    return {
18✔
115
      llm: this.llm.getMetadata(),
116
    };
117
  }
118
  getTraceId() {
119
    if (this.traceId) {
20✔
120
      return this.traceId;
1✔
121
    }
122
    return this.llm.getTraceId();
19✔
123
  }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc