@memberjunction/ai-prompts 6.2.0-edge.1 → 6.2.0-edge.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/AIModelRunner.d.ts +9 -88
- package/dist/AIModelRunner.d.ts.map +1 -1
- package/dist/AIModelRunner.js +11 -283
- package/dist/AIModelRunner.js.map +1 -1
- package/dist/BaseModelRunner.d.ts +36 -1
- package/dist/BaseModelRunner.d.ts.map +1 -1
- package/dist/BaseModelRunner.js +82 -3
- package/dist/BaseModelRunner.js.map +1 -1
- package/dist/audio/AISpeechToTextRunner.d.ts +46 -0
- package/dist/audio/AISpeechToTextRunner.d.ts.map +1 -0
- package/dist/audio/AISpeechToTextRunner.js +93 -0
- package/dist/audio/AISpeechToTextRunner.js.map +1 -0
- package/dist/audio/AITextToSpeechRunner.d.ts +43 -0
- package/dist/audio/AITextToSpeechRunner.d.ts.map +1 -0
- package/dist/audio/AITextToSpeechRunner.js +80 -0
- package/dist/audio/AITextToSpeechRunner.js.map +1 -0
- package/dist/audio/audio-description.d.ts +22 -0
- package/dist/audio/audio-description.d.ts.map +1 -0
- package/dist/audio/audio-description.js +52 -0
- package/dist/audio/audio-description.js.map +1 -0
- package/dist/audio/audio-runner.types.d.ts +46 -0
- package/dist/audio/audio-runner.types.d.ts.map +1 -0
- package/dist/audio/audio-runner.types.js +7 -0
- package/dist/audio/audio-runner.types.js.map +1 -0
- package/dist/decision/AIDecisionRunner.d.ts +134 -0
- package/dist/decision/AIDecisionRunner.d.ts.map +1 -0
- package/dist/decision/AIDecisionRunner.js +498 -0
- package/dist/decision/AIDecisionRunner.js.map +1 -0
- package/dist/decision/LLMDecision.d.ts +158 -0
- package/dist/decision/LLMDecision.d.ts.map +1 -0
- package/dist/decision/LLMDecision.js +493 -0
- package/dist/decision/LLMDecision.js.map +1 -0
- package/dist/decision/decision-questions.d.ts +28 -0
- package/dist/decision/decision-questions.d.ts.map +1 -0
- package/dist/decision/decision-questions.js +123 -0
- package/dist/decision/decision-questions.js.map +1 -0
- package/dist/decision/decision-runner.types.d.ts +29 -0
- package/dist/decision/decision-runner.types.d.ts.map +1 -0
- package/dist/decision/decision-runner.types.js +14 -0
- package/dist/decision/decision-runner.types.js.map +1 -0
- package/dist/embedding/AIEmbeddingRunner.d.ts +115 -0
- package/dist/embedding/AIEmbeddingRunner.d.ts.map +1 -0
- package/dist/embedding/AIEmbeddingRunner.js +462 -0
- package/dist/embedding/AIEmbeddingRunner.js.map +1 -0
- package/dist/embedding/embedding-runner.types.d.ts +72 -0
- package/dist/embedding/embedding-runner.types.d.ts.map +1 -0
- package/dist/embedding/embedding-runner.types.js +2 -0
- package/dist/embedding/embedding-runner.types.js.map +1 -0
- package/dist/image/AIImageGenerationRunner.d.ts +101 -0
- package/dist/image/AIImageGenerationRunner.d.ts.map +1 -0
- package/dist/image/AIImageGenerationRunner.js +355 -0
- package/dist/image/AIImageGenerationRunner.js.map +1 -0
- package/dist/image/image-runner.types.d.ts +90 -0
- package/dist/image/image-runner.types.d.ts.map +1 -0
- package/dist/image/image-runner.types.js +7 -0
- package/dist/image/image-runner.types.js.map +1 -0
- package/dist/index.d.ts +16 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -1
- package/dist/media/BaseMediaRunner.d.ts +143 -0
- package/dist/media/BaseMediaRunner.d.ts.map +1 -0
- package/dist/media/BaseMediaRunner.js +448 -0
- package/dist/media/BaseMediaRunner.js.map +1 -0
- package/dist/media/media-runner.types.d.ts +128 -0
- package/dist/media/media-runner.types.d.ts.map +1 -0
- package/dist/media/media-runner.types.js +8 -0
- package/dist/media/media-runner.types.js.map +1 -0
- package/dist/media/media-usage.d.ts +13 -0
- package/dist/media/media-usage.d.ts.map +1 -0
- package/dist/media/media-usage.js +18 -0
- package/dist/media/media-usage.js.map +1 -0
- package/dist/video/AIVideoRunner.d.ts +46 -0
- package/dist/video/AIVideoRunner.d.ts.map +1 -0
- package/dist/video/AIVideoRunner.js +88 -0
- package/dist/video/AIVideoRunner.js.map +1 -0
- package/dist/video/video-runner.types.d.ts +29 -0
- package/dist/video/video-runner.types.d.ts.map +1 -0
- package/dist/video/video-runner.types.js +7 -0
- package/dist/video/video-runner.types.js.map +1 -0
- package/package.json +12 -12
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Runner for text-to-speech on `TTS` models.
|
|
3
|
+
*
|
|
4
|
+
* @module @memberjunction/ai-prompts
|
|
5
|
+
*/
|
|
6
|
+
import { BaseTextToSpeech, ModelUsage } from '@memberjunction/ai';
|
|
7
|
+
import { BaseMediaRunner } from '../media/BaseMediaRunner.js';
|
|
8
|
+
import { Base64ByteLength, CountCharacters, DetectAudioFormat } from './audio-description.js';
|
|
9
|
+
import { SecondsIn } from '../media/media-usage.js';
|
|
10
|
+
/**
|
|
11
|
+
* Runs text-to-speech on `TTS` models (`BaseTextToSpeech` drivers). It does for a speech call what
|
|
12
|
+
* `AIPromptRunner` does for a chat call: selects a model from the carrier prompt's bindings (or the
|
|
13
|
+
* pinned `ModelID`), resolves credentials, fails over, and writes an `MJ: AI Prompt Runs` row.
|
|
14
|
+
*
|
|
15
|
+
* The row never holds audio. `Messages` records the text, the voice and the character count, and
|
|
16
|
+
* `Result` the audio's format, size and, when the driver reported it, duration. Usage is the
|
|
17
|
+
* driver's when it reports a quantity; otherwise the characters sent are counted in the
|
|
18
|
+
* `Characters` measure. No price unit type prices `Characters` yet, so such a run is left uncosted,
|
|
19
|
+
* with a logged reason, rather than priced wrong. The runner never sets a cost itself.
|
|
20
|
+
*/
|
|
21
|
+
export class AITextToSpeechRunner extends BaseMediaRunner {
|
|
22
|
+
/** The carrier prompt used when the caller names none. */
|
|
23
|
+
static { this.DEFAULT_PROMPT_NAME = 'Default Text To Speech'; }
|
|
24
|
+
/** Speech calls run only on `TTS` models. */
|
|
25
|
+
get RequiredModelType() {
|
|
26
|
+
return 'TTS';
|
|
27
|
+
}
|
|
28
|
+
get DefaultLogCategory() {
|
|
29
|
+
return 'AITextToSpeechRunner';
|
|
30
|
+
}
|
|
31
|
+
get DefaultPromptName() {
|
|
32
|
+
return AITextToSpeechRunner.DEFAULT_PROMPT_NAME;
|
|
33
|
+
}
|
|
34
|
+
get DriverBaseClass() {
|
|
35
|
+
return BaseTextToSpeech;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Speaks `params.text` (`BaseTextToSpeech.CreateSpeech`). Never throws: every failure is a result
|
|
39
|
+
* with `Success: false` and an `ErrorMessage`.
|
|
40
|
+
*/
|
|
41
|
+
async RunTextToSpeech(params) {
|
|
42
|
+
const outcome = await this.RunMediaOperation(params, {
|
|
43
|
+
Name: 'CreateSpeech',
|
|
44
|
+
Validate: p => (p.text && p.text.trim().length > 0 ? undefined : 'Text to speak is required (params.text)'),
|
|
45
|
+
Invoke: (driver, apiName) => driver.CreateSpeech({ ...this.DriverParams(params), model_id: apiName }),
|
|
46
|
+
DescribeRequest: p => this.describeRequest(p),
|
|
47
|
+
DescribeOutput: output => this.describeAudio(output, params),
|
|
48
|
+
CountUsage: p => ModelUsage.ForMedia('Characters', CountCharacters(p.text)),
|
|
49
|
+
});
|
|
50
|
+
return this.toResult(outcome);
|
|
51
|
+
}
|
|
52
|
+
/** The run row's `Messages`: the text, the voice, the requested format and the character count. */
|
|
53
|
+
describeRequest(params) {
|
|
54
|
+
return JSON.stringify({
|
|
55
|
+
Operation: 'CreateSpeech',
|
|
56
|
+
Text: params.text,
|
|
57
|
+
Voice: params.voice,
|
|
58
|
+
OutputFormat: params.output_format,
|
|
59
|
+
Characters: CountCharacters(params.text),
|
|
60
|
+
AgentRunID: params.AgentRunID,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The run row's `Result`: the audio's format (read from its leading bytes, else the format asked
|
|
65
|
+
* for), its size, and its duration when the driver reported one. Never the audio.
|
|
66
|
+
*/
|
|
67
|
+
describeAudio(output, params) {
|
|
68
|
+
return JSON.stringify({
|
|
69
|
+
Format: DetectAudioFormat(output.data) ?? params.output_format ?? null,
|
|
70
|
+
Bytes: output.data?.byteLength ?? Base64ByteLength(output.content),
|
|
71
|
+
DurationSeconds: SecondsIn(output.usage, 'output') ?? null,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** The caller's result: the shared fields, with the driver's result as `SpeechResult`. */
|
|
75
|
+
toResult(outcome) {
|
|
76
|
+
const { Output, ...result } = outcome;
|
|
77
|
+
return { ...result, SpeechResult: Output };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=AITextToSpeechRunner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AITextToSpeechRunner.js","sourceRoot":"","sources":["../../src/audio/AITextToSpeechRunner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAoC,MAAM,oBAAoB,CAAC;AACpG,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAG3D,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAC3F,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD;;;;;;;;;;GAUG;AACH,MAAM,OAAO,oBAAqB,SAAQ,eAAwE;IAChH,0DAA0D;aACnC,wBAAmB,GAAG,wBAAwB,CAAC;IAEtE,6CAA6C;IAC7C,IAAoB,iBAAiB;QACnC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAuB,kBAAkB;QACvC,OAAO,sBAAsB,CAAC;IAChC,CAAC;IAED,IAAuB,iBAAiB;QACtC,OAAO,oBAAoB,CAAC,mBAAmB,CAAC;IAClD,CAAC;IAED,IAAuB,eAAe;QACpC,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,eAAe,CAAC,M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AAI,CAAC,SAAS,CAAC;YACpB,MAAM,EAAE,iBAAiB,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,aAAa,IAAI,IAAI;YACtE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,IAAI,gBAAgB,CAAC,MAAM,CAAC,OAAO,CAAC;YAClE,eAAe,EAAE,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,IAAI,IAAI;SAC3D,CAAC,CAAC;IACL,CAAC;IAED,0FAA0F;IAClF,QAAQ,CAAC,OAAwC;QACvD,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC;QACtC,OAAO,EAAE,GAAG,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,CAAC;IAC7C,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Helpers the audio runners use to describe audio on a run row without storing it.
|
|
3
|
+
* Internal to the package.
|
|
4
|
+
*
|
|
5
|
+
* @module @memberjunction/ai-prompts
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The number of characters in text as a speech vendor counts them: Unicode code points, so an emoji
|
|
9
|
+
* or an accented letter written as one code point counts once, not as two UTF-16 units.
|
|
10
|
+
*/
|
|
11
|
+
export declare function CountCharacters(text: string): number;
|
|
12
|
+
/**
|
|
13
|
+
* The size in bytes of the data a base 64 string encodes, without decoding it. Padding (`=`) and
|
|
14
|
+
* whitespace are not data.
|
|
15
|
+
*/
|
|
16
|
+
export declare function Base64ByteLength(base64: string | undefined): number;
|
|
17
|
+
/**
|
|
18
|
+
* The container format of audio bytes, read from their leading bytes, or undefined when they are
|
|
19
|
+
* not a recognized container (raw PCM, for one, has no header).
|
|
20
|
+
*/
|
|
21
|
+
export declare function DetectAudioFormat(bytes: Buffer | undefined): string | undefined;
|
|
22
|
+
//# sourceMappingURL=audio-description.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-description.d.ts","sourceRoot":"","sources":["../../src/audio/audio-description.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAOnE;AAeD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAK/E"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Helpers the audio runners use to describe audio on a run row without storing it.
|
|
3
|
+
* Internal to the package.
|
|
4
|
+
*
|
|
5
|
+
* @module @memberjunction/ai-prompts
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The number of characters in text as a speech vendor counts them: Unicode code points, so an emoji
|
|
9
|
+
* or an accented letter written as one code point counts once, not as two UTF-16 units.
|
|
10
|
+
*/
|
|
11
|
+
export function CountCharacters(text) {
|
|
12
|
+
return Array.from(text).length;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The size in bytes of the data a base 64 string encodes, without decoding it. Padding (`=`) and
|
|
16
|
+
* whitespace are not data.
|
|
17
|
+
*/
|
|
18
|
+
export function Base64ByteLength(base64) {
|
|
19
|
+
if (!base64) {
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
const compact = base64.replace(/\s/g, '');
|
|
23
|
+
const padding = compact.endsWith('==') ? 2 : compact.endsWith('=') ? 1 : 0;
|
|
24
|
+
return Math.max(0, Math.floor((compact.length * 3) / 4) - padding);
|
|
25
|
+
}
|
|
26
|
+
/** Leading bytes that identify an audio container, checked in order. */
|
|
27
|
+
const AUDIO_SIGNATURES = [
|
|
28
|
+
{ Format: 'wav', Matches: b => ascii(b, 0, 4) === 'RIFF' && ascii(b, 8, 12) === 'WAVE' },
|
|
29
|
+
{ Format: 'ogg', Matches: b => ascii(b, 0, 4) === 'OggS' },
|
|
30
|
+
{ Format: 'flac', Matches: b => ascii(b, 0, 4) === 'fLaC' },
|
|
31
|
+
{ Format: 'webm', Matches: b => b.length >= 4 && b[0] === 0x1a && b[1] === 0x45 && b[2] === 0xdf && b[3] === 0xa3 },
|
|
32
|
+
{ Format: 'mp4', Matches: b => ascii(b, 4, 8) === 'ftyp' },
|
|
33
|
+
{ Format: 'mp3', Matches: b => ascii(b, 0, 3) === 'ID3' },
|
|
34
|
+
// ADTS AAC and MPEG audio both open with a sync word; the layer bits (00 for ADTS) tell them apart.
|
|
35
|
+
{ Format: 'aac', Matches: b => b.length >= 2 && b[0] === 0xff && (b[1] & 0xf6) === 0xf0 },
|
|
36
|
+
{ Format: 'mp3', Matches: b => b.length >= 2 && b[0] === 0xff && (b[1] & 0xe0) === 0xe0 },
|
|
37
|
+
];
|
|
38
|
+
/**
|
|
39
|
+
* The container format of audio bytes, read from their leading bytes, or undefined when they are
|
|
40
|
+
* not a recognized container (raw PCM, for one, has no header).
|
|
41
|
+
*/
|
|
42
|
+
export function DetectAudioFormat(bytes) {
|
|
43
|
+
if (!bytes || bytes.length < 4) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return AUDIO_SIGNATURES.find(signature => signature.Matches(bytes))?.Format;
|
|
47
|
+
}
|
|
48
|
+
/** The bytes in [start, end) as ASCII, or an empty string when the buffer is too short. */
|
|
49
|
+
function ascii(bytes, start, end) {
|
|
50
|
+
return bytes.length >= end ? bytes.toString('latin1', start, end) : '';
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=audio-description.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-description.js","sourceRoot":"","sources":["../../src/audio/audio-description.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY;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AAC;AAC9E,CAAC;AAED,2FAA2F;AAC3F,SAAS,KAAK,CAAC,KAAa,EAAE,KAAa,EAAE,GAAW;IACtD,OAAO,KAAK,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACzE,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Parameter and result types for the audio runners.
|
|
3
|
+
*
|
|
4
|
+
* @module @memberjunction/ai-prompts
|
|
5
|
+
*/
|
|
6
|
+
import type { SpeechResult, SpeechToTextParams, TextToSpeechParams } from '@memberjunction/ai';
|
|
7
|
+
import type { AIMediaRunOptions, AIMediaRunResult } from '../media/media-runner.types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Parameters for `AITextToSpeechRunner.RunTextToSpeech`: the driver's own `TextToSpeechParams`
|
|
10
|
+
* (text, voice, voice settings and the rest), plus the runner's options.
|
|
11
|
+
*
|
|
12
|
+
* The runner sets the driver's `model_id` per candidate, to the candidate's API name; a model with
|
|
13
|
+
* none leaves the driver's default. Voices belong to a vendor, so pin `ModelID` when the voice
|
|
14
|
+
* matters.
|
|
15
|
+
*/
|
|
16
|
+
export interface AITextToSpeechRunParams extends TextToSpeechParams, AIMediaRunOptions {
|
|
17
|
+
}
|
|
18
|
+
/** The result of `AITextToSpeechRunner.RunTextToSpeech`. */
|
|
19
|
+
export interface AITextToSpeechRunResult extends AIMediaRunResult {
|
|
20
|
+
/**
|
|
21
|
+
* The driver's result: the audio in `data`, and base 64 encoded in `content`. Absent when the call
|
|
22
|
+
* never reached a model. The run row describes the audio but never holds it.
|
|
23
|
+
*/
|
|
24
|
+
SpeechResult?: SpeechResult;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parameters for `AISpeechToTextRunner.RunSpeechToText`: the audio, as `audioData` bytes or a base 64
|
|
28
|
+
* `audioFile` (one is required), and the driver's optional hints, plus the runner's options.
|
|
29
|
+
*
|
|
30
|
+
* The driver's `model` is set by the runner, per candidate, to the candidate's API name.
|
|
31
|
+
*/
|
|
32
|
+
export interface AISpeechToTextRunParams extends Pick<SpeechToTextParams, 'audioData' | 'fileName' | 'language' | 'prompt' | 'temperature'>, Partial<Pick<SpeechToTextParams, 'audioFile'>>, AIMediaRunOptions {
|
|
33
|
+
}
|
|
34
|
+
/** The result of `AISpeechToTextRunner.RunSpeechToText`. */
|
|
35
|
+
export interface AISpeechToTextRunResult extends AIMediaRunResult {
|
|
36
|
+
/** The transcript, when the call succeeded. */
|
|
37
|
+
Transcript?: string;
|
|
38
|
+
/**
|
|
39
|
+
* The seconds of audio transcribed, when the driver reported them. Undefined otherwise; the runner
|
|
40
|
+
* never estimates it.
|
|
41
|
+
*/
|
|
42
|
+
DurationSeconds?: number;
|
|
43
|
+
/** The driver's result. Absent when the call never reached a model. */
|
|
44
|
+
SpeechResult?: SpeechResult;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=audio-runner.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-runner.types.d.ts","sourceRoot":"","sources":["../../src/audio/audio-runner.types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC/F,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEvF;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAwB,SAAQ,kBAAkB,EAAE,iBAAiB;CAAG;AAEzF,4DAA4D;AAC5D,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D;;;OAGG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAED;;;;;GAKG;AACH,MAAM,WAAW,uBACf,SAAQ,IAAI,CAAC,kBAAkB,EAAE,WAAW,GAAG,UAAU,GAAG,UAAU,GAAG,QAAQ,GAAG,aAAa,CAAC,EAChG,OAAO,CAAC,IAAI,CAAC,kBAAkB,EAAE,WAAW,CAAC,CAAC,EAC9C,iBAAiB;CAAG;AAExB,4DAA4D;AAC5D,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,uEAAuE;IACvE,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio-runner.types.js","sourceRoot":"","sources":["../../src/audio/audio-runner.types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { DecisionResult } from '@memberjunction/ai';
|
|
2
|
+
import { AIPromptParams } from '@memberjunction/ai-core-plus';
|
|
3
|
+
import { BaseModelRunner, FailoverAttempt } from '../BaseModelRunner.js';
|
|
4
|
+
import { AIDecisionParams, AIDecisionRunResult } from './decision-runner.types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Runs typed decisions (Likelihood, Choice, Score) on `Decision`-type models. It does for a decision
|
|
7
|
+
* what `AIPromptRunner` does for a chat call: selects a model from the decision prompt's bindings,
|
|
8
|
+
* resolves credentials, bounds each call with the caller's cancellation and `params.timeoutMS` (when
|
|
9
|
+
* set; there is no default timeout, as for chat prompts), fails over, and writes an
|
|
10
|
+
* `MJ: AI Prompt Runs` row with tokens and cost.
|
|
11
|
+
*
|
|
12
|
+
* It owns no parsing: drivers return typed answers and `BaseDecision` validates them. The
|
|
13
|
+
* configuration carrier is an `MJ: AI Prompts` row whose template renders the state when the caller
|
|
14
|
+
* does not pass one.
|
|
15
|
+
*/
|
|
16
|
+
export declare class AIDecisionRunner extends BaseModelRunner {
|
|
17
|
+
/** Decisions run only on `Decision`-type models. */
|
|
18
|
+
get RequiredModelType(): string;
|
|
19
|
+
protected get DefaultLogCategory(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Whether a driver needs its own API key. `LLMDecision` does not: it runs an MJ prompt whose chat
|
|
22
|
+
* models resolve their own credentials, as `LLMReranker` does.
|
|
23
|
+
*/
|
|
24
|
+
protected DriverRequiresCredentials(driverClass: string): boolean;
|
|
25
|
+
/** Treats a driver that needs no credentials as always available. */
|
|
26
|
+
protected HasCredentialsAvailable(driverClass: string, promptId: string | undefined, modelId: string | undefined, vendorId: string | undefined, params?: AIPromptParams): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Makes an `LLMDecision`'s chat run a child of this decision's run, and returns whether it did. The
|
|
29
|
+
* attempt's cost is booked as the decision run's descendant cost only when this returns true, so
|
|
30
|
+
* linking and booking cannot disagree.
|
|
31
|
+
*
|
|
32
|
+
* A driver that is not an `LLMDecision` calls its model directly: there is nothing to link. The
|
|
33
|
+
* child's INSERT names this run as its `ParentID`, so this run's queued INSERT must land first.
|
|
34
|
+
* When that INSERT failed there is no row to point at: the child would then fail its foreign key
|
|
35
|
+
* and go unrecorded too, so the chat run is left unlinked, a root run of its own.
|
|
36
|
+
*/
|
|
37
|
+
private linkChildRun;
|
|
38
|
+
/**
|
|
39
|
+
* Answers the questions in `params.Questions` about the state. Never throws: every failure is a
|
|
40
|
+
* result with `success: false` and an `errorMessage`.
|
|
41
|
+
*/
|
|
42
|
+
ExecuteDecision(params: AIDecisionParams): Promise<AIDecisionRunResult>;
|
|
43
|
+
/** Finalizes a run row as failed, so an exception after it was created never leaves it 'Running'. */
|
|
44
|
+
private finalizeFailedRun;
|
|
45
|
+
/** Returns an error message when the request cannot run, otherwise undefined. */
|
|
46
|
+
private validateParams;
|
|
47
|
+
/** Builds the candidates, or returns the reason there are none. The type floor throws here. */
|
|
48
|
+
private buildCandidates;
|
|
49
|
+
/** Candidates already warned about for lacking credentials, so each warning appears once per process. */
|
|
50
|
+
private static readonly warnedMissingCredentials;
|
|
51
|
+
/** Selects the first candidate with credentials, or returns an error listing every candidate. */
|
|
52
|
+
private selectCandidate;
|
|
53
|
+
/**
|
|
54
|
+
* Warns, once per process per model, when a higher-priority candidate is skipped for lack of a
|
|
55
|
+
* credential. Otherwise a host without, say, Jev's key would silently send every decision to the
|
|
56
|
+
* fallback model.
|
|
57
|
+
*/
|
|
58
|
+
private warnSkippedCandidates;
|
|
59
|
+
/** The caller's explicit state, or the prompt's template rendered with the caller's data. */
|
|
60
|
+
private resolveState;
|
|
61
|
+
/**
|
|
62
|
+
* Runs the selected candidate alone when failover is off, otherwise the base failover loop. Tracks
|
|
63
|
+
* the candidate that produced the result, so the caller reports the model that actually answered,
|
|
64
|
+
* and adds up what every attempt's linked child run cost, so none of those costs is lost when a
|
|
65
|
+
* later attempt answers.
|
|
66
|
+
*/
|
|
67
|
+
private runDecision;
|
|
68
|
+
/** Adds an attempt's cost to a running total. An attempt that reported no cost adds nothing. */
|
|
69
|
+
private addCost;
|
|
70
|
+
/**
|
|
71
|
+
* Makes the decision on one candidate: builds its driver, links the driver's child run when it
|
|
72
|
+
* makes one, and calls it.
|
|
73
|
+
*/
|
|
74
|
+
private executeOnCandidate;
|
|
75
|
+
/**
|
|
76
|
+
* The candidate's driver, after checking its limits and resolving its credential, or a failed
|
|
77
|
+
* result that allows failover and says why there is none.
|
|
78
|
+
*/
|
|
79
|
+
private driverForCandidate;
|
|
80
|
+
/**
|
|
81
|
+
* Checks the request against the model's declared `Decision` limits. A breach is an error that
|
|
82
|
+
* allows failover, because another model may accept the request. `MaxStateTokens` is not checked
|
|
83
|
+
* yet: the runner has no token counter.
|
|
84
|
+
*/
|
|
85
|
+
private checkModelLimits;
|
|
86
|
+
/**
|
|
87
|
+
* Builds the candidate's driver through the ClassFactory. `LLMDecision` takes the ID of the chat
|
|
88
|
+
* prompt named by the model-vendor row's `APIName`; every other driver takes only its API key.
|
|
89
|
+
* Returns an error message when the driver cannot be built.
|
|
90
|
+
*/
|
|
91
|
+
private createDriver;
|
|
92
|
+
/** Calls the driver, bounded by the caller's cancellation and `params.timeoutMS` when set. */
|
|
93
|
+
private callDriver;
|
|
94
|
+
/**
|
|
95
|
+
* A failed decision that allows failover to the next candidate. The severity must not be 'Fatal':
|
|
96
|
+
* the failover loop stops on any Fatal error before it reads `canFailover`.
|
|
97
|
+
*/
|
|
98
|
+
private failedDecision;
|
|
99
|
+
/** The result returned when every failover candidate has failed. */
|
|
100
|
+
protected createFailoverErrorResult(lastError: Error | null, failoverAttempts: FailoverAttempt[]): DecisionResult;
|
|
101
|
+
/**
|
|
102
|
+
* Finalizes the run row: every answer with its full distribution in `Result`, the resolved model
|
|
103
|
+
* version in `ModelSpecificResponseDetails`, and tokens and cost from the driver's usage.
|
|
104
|
+
*/
|
|
105
|
+
private finalizeDecisionRun;
|
|
106
|
+
/**
|
|
107
|
+
* Records the answering attempt's tokens on the run, and the cost by where it was incurred:
|
|
108
|
+
* - a model call the answering driver made directly is this run's own `Cost`;
|
|
109
|
+
* - a model call made through a child run linked to this run (`LLMDecision`'s chat prompt) is that
|
|
110
|
+
* child's cost, so it is recorded as `DescendantCost` and in `TotalCost`, never as `Cost`: a
|
|
111
|
+
* report summing `Cost` over every run would otherwise count the chat call twice.
|
|
112
|
+
*
|
|
113
|
+
* `DescendantCost` sums every attempt's linked child run, not only the answering one's: each is a
|
|
114
|
+
* child of this run, and the server's rollup sums them all, so recording less would overwrite
|
|
115
|
+
* that rollup with a smaller number. An attempt whose chat run could not be linked is not a
|
|
116
|
+
* descendant, so when it answers, its cost is this run's own.
|
|
117
|
+
*/
|
|
118
|
+
private applyUsage;
|
|
119
|
+
/** Builds the caller's result from the driver's, naming the candidate that answered. */
|
|
120
|
+
private buildRunResult;
|
|
121
|
+
/** A failed result for a request that never reached, or never finished, a model call. */
|
|
122
|
+
private failedRunResult;
|
|
123
|
+
private modelInfoFor;
|
|
124
|
+
/** The key format BaseModelRunner's failover credential cache uses. */
|
|
125
|
+
private candidateKey;
|
|
126
|
+
/** Records which candidate was chosen and why, for the run row. */
|
|
127
|
+
private buildSelectionInfo;
|
|
128
|
+
/**
|
|
129
|
+
* Renders the prompt's template into the state, with the same data layering as the chat runner:
|
|
130
|
+
* system placeholders, then `params.data`, then `params.templateData`.
|
|
131
|
+
*/
|
|
132
|
+
private renderStateFromTemplate;
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=AIDecisionRunner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AIDecisionRunner.d.ts","sourceRoot":"","sources":["../../src/decision/AIDecisionRunner.ts"],"names":[],"mappings":"AAIA,OAAO,EAKL,cAAc,EAGf,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAKL,cAAc,EACf,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,eAAe,EAEf,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,yBAAyB,CAAC;AAwCjC;;;;;;;;;;GAUG;AACH,qBAAa,gBAAiB,SAAQ,eAAe;IACnD,oDAAoD;IACpD,IAAoB,iBAAiB,IAAI,MAAM,CAE9C;IAED,cAAuB,kBAAkB,IAAI,MAAM,CAElD;IAED;;;OAGG;IACH,SAAS,CAAC,yBAAyB,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO;IAIjE,qEAAqE;cAClD,uBAAuB,CACxC,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,MAAM,CAAC,EAAE,cAAc,GACtB,OAAO;IAOV;;;;;;;;;OASG;YACW,YAAY;IAY1B;;;OAGG;IACU,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IA0CpF,qGAAqG;YACvF,iBAAiB;IAO/B,iFAAiF;IACjF,OAAO,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