@mate-academy/llm-gateway 8.1.0 → 8.2.0
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 +68 -4
- package/dist/LLMService.typedefs.d.ts +20 -0
- package/dist/LLMService.typedefs.js.map +1 -1
- package/dist/client/LLMAgent.d.ts +17 -4
- package/dist/client/LLMAgent.js +4 -0
- package/dist/client/LLMAgent.js.map +1 -1
- package/dist/client/LLMClientEngine.d.ts +81 -0
- package/dist/client/LLMClientEngine.js +350 -0
- package/dist/client/LLMClientEngine.js.map +1 -0
- package/dist/client/agentRun.typedefs.d.ts +18 -3
- package/dist/client/agentRun.typedefs.js +1 -0
- package/dist/client/agentRun.typedefs.js.map +1 -1
- package/dist/client/agentRunner.d.ts +38 -3
- package/dist/client/agentRunner.js +206 -80
- package/dist/client/agentRunner.js.map +1 -1
- package/dist/client/codegen/fetchPromptRecords.d.ts +1 -1
- package/dist/client/codegen/index.d.ts +1 -0
- package/dist/client/codegen/index.js +1 -0
- package/dist/client/codegen/index.js.map +1 -1
- package/dist/client/codegen/renderSnapshot.constants.d.ts +12 -0
- package/dist/client/codegen/renderSnapshot.constants.js +21 -0
- package/dist/client/codegen/renderSnapshot.constants.js.map +1 -0
- package/dist/client/codegen/renderSnapshot.d.ts +6 -18
- package/dist/client/codegen/renderSnapshot.js +152 -125
- package/dist/client/codegen/renderSnapshot.js.map +1 -1
- package/dist/client/codegen/renderSnapshot.typedefs.d.ts +20 -0
- package/dist/client/codegen/renderSnapshot.typedefs.js +3 -0
- package/dist/client/codegen/renderSnapshot.typedefs.js.map +1 -0
- package/dist/client/coerceToolResult.d.ts +10 -0
- package/dist/client/coerceToolResult.js +38 -0
- package/dist/client/coerceToolResult.js.map +1 -0
- package/dist/client/createLLMClient.d.ts +1 -40
- package/dist/client/createLLMClient.js +5 -329
- package/dist/client/createLLMClient.js.map +1 -1
- package/dist/client/index.d.ts +3 -1
- package/dist/client/index.js +3 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/llmClient.typedefs.d.ts +180 -0
- package/dist/client/{createLLMClient.typedefs.js → llmClient.typedefs.js} +1 -1
- package/dist/client/llmClient.typedefs.js.map +1 -0
- package/dist/client/plainMessages.d.ts +1 -1
- package/dist/client/promptVariables.d.ts +7 -0
- package/dist/client/promptVariables.js +13 -0
- package/dist/client/promptVariables.js.map +1 -0
- package/dist/client/snapshotRuntime.d.ts +17 -0
- package/dist/client/snapshotRuntime.js +42 -0
- package/dist/client/snapshotRuntime.js.map +1 -0
- package/dist/client/snapshotRuntime.typedefs.d.ts +13 -0
- package/dist/client/snapshotRuntime.typedefs.js +3 -0
- package/dist/client/snapshotRuntime.typedefs.js.map +1 -0
- package/dist/providers/LLMAPI/services/LLMAPIAssistance.service.d.ts +0 -2
- package/dist/providers/LLMAPI/services/LLMAPIAssistance.service.js +37 -25
- package/dist/providers/LLMAPI/services/LLMAPIAssistance.service.js.map +1 -1
- package/dist/providers/LLMAPI/services/LLMAPICompletion.service.d.ts +0 -2
- package/dist/providers/LLMAPI/services/LLMAPICompletion.service.js +15 -17
- package/dist/providers/LLMAPI/services/LLMAPICompletion.service.js.map +1 -1
- package/dist/providers/LLMAPI/utilities/index.d.ts +1 -1
- package/dist/providers/LLMAPI/utilities/index.js +3 -4
- package/dist/providers/LLMAPI/utilities/index.js.map +1 -1
- package/dist/providers/LLMAPI/utilities/validateStructuredResponse.d.ts +10 -0
- package/dist/providers/LLMAPI/utilities/validateStructuredResponse.js +21 -0
- package/dist/providers/LLMAPI/utilities/validateStructuredResponse.js.map +1 -0
- package/dist/services/LLMBaseService.abstract.js +2 -2
- package/dist/services/LLMBaseService.abstract.js.map +1 -1
- package/dist/utilities/index.d.ts +0 -1
- package/dist/utilities/index.js +0 -1
- package/dist/utilities/index.js.map +1 -1
- package/dist/utilities/llmTracing/llmTracing.typedefs.d.ts +8 -3
- package/dist/utilities/tools/LLMTool.d.ts +3 -12
- package/dist/utilities/tools/LLMTool.js.map +1 -1
- package/package.json +2 -1
- package/dist/client/createLLMClient.typedefs.d.ts +0 -91
- package/dist/client/createLLMClient.typedefs.js.map +0 -1
- package/dist/providers/LLMAPI/utilities/LLMAPIJsonFixer.d.ts +0 -24
- package/dist/providers/LLMAPI/utilities/LLMAPIJsonFixer.js +0 -237
- package/dist/providers/LLMAPI/utilities/LLMAPIJsonFixer.js.map +0 -1
- package/dist/utilities/traceIO.d.ts +0 -17
- package/dist/utilities/traceIO.js +0 -48
- package/dist/utilities/traceIO.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mate-academy/llm-gateway",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "A gateway package for LLM services.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -117,6 +117,7 @@
|
|
|
117
117
|
"mime-types": "^3.0.1",
|
|
118
118
|
"music-metadata": "^11.10.1",
|
|
119
119
|
"openai": "^6.27.0",
|
|
120
|
+
"ts-morph": "^28.0.0",
|
|
120
121
|
"uuid": "^9.0.1",
|
|
121
122
|
"zod": "^4.1.12"
|
|
122
123
|
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { type LLMProviders, type LLMInstanceOptions } from '../LLMService.typedefs';
|
|
2
|
-
import { type LLMGateway } from '../LLMGateway';
|
|
3
|
-
import { type LLMLoggerInterface } from '../utilities/logger';
|
|
4
|
-
import { type LLMReporterInterface } from '../utilities/reporter';
|
|
5
|
-
import { type InferSchema, type LLMSchemaInterface } from '../utilities/schema';
|
|
6
|
-
import { type LLMGatewayPromptClient } from '../client/promptClientPort.typedefs';
|
|
7
|
-
import { type LLMPromptBinding, type LLMPromptRegistry } from '../client/defineLLMPrompts';
|
|
8
|
-
import { type LLMPromptSnapshot } from '../client/promptSnapshot.typedefs';
|
|
9
|
-
import { type LLMVariableValue } from '../client/promptSnapshot.typedefs';
|
|
10
|
-
export type LLMClientMode = 'production' | 'experiment';
|
|
11
|
-
/**
|
|
12
|
-
* The explicit opt-out for the reporter pipeline; unlike tracing/prompts,
|
|
13
|
-
* reporting has no safe silent default, so tests must opt out loudly. Passed
|
|
14
|
-
* where `reporting` is expected to disable the Reporter/DWHSync wiring (tests,
|
|
15
|
-
* scripts).
|
|
16
|
-
*/
|
|
17
|
-
export declare const ReportingDisabled: unique symbol;
|
|
18
|
-
export type ReportingDisabledToken = typeof ReportingDisabled;
|
|
19
|
-
/**
|
|
20
|
-
* Resolves provider credentials for one call, scoped by provider + prompt +
|
|
21
|
-
* product, preserving the existing per-feature Secrets Manager layout.
|
|
22
|
-
*/
|
|
23
|
-
export type LLMCredentialsResolver = <Provider extends LLMProviders>(scope: {
|
|
24
|
-
provider: Provider;
|
|
25
|
-
promptName: string;
|
|
26
|
-
product?: string;
|
|
27
|
-
}) => Promise<LLMInstanceOptions[Provider]>;
|
|
28
|
-
export interface LLMClientStaticContext {
|
|
29
|
-
product?: string;
|
|
30
|
-
appEnvironment?: string;
|
|
31
|
-
[key: string]: string | undefined;
|
|
32
|
-
}
|
|
33
|
-
export interface LLMClientCallContext {
|
|
34
|
-
userId?: string | number | null;
|
|
35
|
-
[key: string]: string | number | boolean | null | undefined;
|
|
36
|
-
}
|
|
37
|
-
export interface LLMReportingConfig {
|
|
38
|
-
reporter: LLMReporterInterface<any>;
|
|
39
|
-
}
|
|
40
|
-
export interface CreateLLMClientConfig<Registry extends LLMPromptRegistry> {
|
|
41
|
-
gateway: LLMGateway;
|
|
42
|
-
registry: Registry;
|
|
43
|
-
snapshot: LLMPromptSnapshot;
|
|
44
|
-
credentials: LLMCredentialsResolver;
|
|
45
|
-
reporting: LLMReportingConfig | ReportingDisabledToken;
|
|
46
|
-
logger?: LLMLoggerInterface;
|
|
47
|
-
context?: LLMClientStaticContext;
|
|
48
|
-
mode?: LLMClientMode;
|
|
49
|
-
promptClient?: LLMGatewayPromptClient;
|
|
50
|
-
defaultLabel?: string;
|
|
51
|
-
cacheTtlSeconds?: number;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* The variables a prompt requires. Strict on required keys, tolerant of extras
|
|
55
|
-
* (`& Record<string, LLMVariableValue>`) so a Langfuse edit that drops a variable
|
|
56
|
-
* can never break a build — the stale extra is accepted, never rejected.
|
|
57
|
-
*/
|
|
58
|
-
export type LLMPromptVariables<RequiredKeys extends string> = Record<RequiredKeys, LLMVariableValue> & Record<string, LLMVariableValue>;
|
|
59
|
-
/**
|
|
60
|
-
* The return type of `generate` for one prompt: the bound schema's inferred type
|
|
61
|
-
* when a schema is present, else the raw completion string.
|
|
62
|
-
*/
|
|
63
|
-
export type LLMGenerateResult<Binding extends LLMPromptBinding> = Binding extends {
|
|
64
|
-
schema: infer Schema;
|
|
65
|
-
} ? Schema extends LLMSchemaInterface ? InferSchema<Schema> : string : string;
|
|
66
|
-
export interface LLMGenerateOptions<Variables extends Record<string, LLMVariableValue>, Schema extends LLMSchemaInterface = never> {
|
|
67
|
-
variables: Variables;
|
|
68
|
-
context?: LLMClientCallContext;
|
|
69
|
-
history?: LLMPlainMessage[];
|
|
70
|
-
abortSignal?: AbortSignal;
|
|
71
|
-
overrides?: LLMCallOverrides;
|
|
72
|
-
/**
|
|
73
|
-
* Per-call structured-output schema. Overrides the registry binding's `schema`
|
|
74
|
-
* for this call and drives the return type (`InferSchema<Schema>`). Use it when
|
|
75
|
-
* the output contract is dynamic per call (e.g. a schema whose shape depends on
|
|
76
|
-
* runtime input). The schema is the code-owned output contract, not a routing
|
|
77
|
-
* control, so it is honored in every mode.
|
|
78
|
-
*/
|
|
79
|
-
schema?: Schema;
|
|
80
|
-
}
|
|
81
|
-
export interface LLMCallOverrides {
|
|
82
|
-
model?: string;
|
|
83
|
-
provider?: LLMProviders;
|
|
84
|
-
promptVersion?: number;
|
|
85
|
-
promptLabel?: string;
|
|
86
|
-
}
|
|
87
|
-
export type LLMPlainMessageRole = 'user' | 'assistant';
|
|
88
|
-
export interface LLMPlainMessage {
|
|
89
|
-
role: LLMPlainMessageRole;
|
|
90
|
-
text: string;
|
|
91
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createLLMClient.typedefs.js","sourceRoot":"","sources":["../../src/client/createLLMClient.typedefs.ts"],"names":[],"mappings":";;;AAeA;;;;;GAKG;AACU,QAAA,iBAAiB,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { OpenAI } from 'openai';
|
|
2
|
-
import type { LLMLoggerInterface } from '../../../utilities/logger';
|
|
3
|
-
import type { LLMReporterInterface } from '../../../utilities/reporter';
|
|
4
|
-
import type { LLMSchemaInterface } from '../../../utilities/schema';
|
|
5
|
-
import type { LLMStructuredResult } from '../../../LLMService.typedefs';
|
|
6
|
-
import { type LLMModelUsage } from '../../../utilities';
|
|
7
|
-
interface ParseStructuredResponseOptions {
|
|
8
|
-
content: string;
|
|
9
|
-
schema: LLMSchemaInterface;
|
|
10
|
-
jsonSchema: any;
|
|
11
|
-
reporterContext?: any;
|
|
12
|
-
abortSignal?: AbortSignal;
|
|
13
|
-
primaryUsage: LLMModelUsage;
|
|
14
|
-
}
|
|
15
|
-
export declare class LLMAPIJsonFixer {
|
|
16
|
-
private readonly instance;
|
|
17
|
-
private readonly logger;
|
|
18
|
-
private readonly reporter;
|
|
19
|
-
constructor(instance: OpenAI, logger: LLMLoggerInterface | undefined, reporter: LLMReporterInterface<any> | undefined);
|
|
20
|
-
parseStructuredResponse(options: ParseStructuredResponseOptions): Promise<LLMStructuredResult>;
|
|
21
|
-
private fixAndParseJson;
|
|
22
|
-
}
|
|
23
|
-
export declare function sanitizeJsonResponse(response: string): string;
|
|
24
|
-
export {};
|
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LLMAPIJsonFixer = void 0;
|
|
4
|
-
exports.sanitizeJsonResponse = sanitizeJsonResponse;
|
|
5
|
-
const serializeError_1 = require("../../../utilities/serializeError");
|
|
6
|
-
const LLMAPI_typedefs_1 = require("../../../providers/LLMAPI/LLMAPI.typedefs");
|
|
7
|
-
const utilities_1 = require("../../../utilities");
|
|
8
|
-
class LLMAPIJsonFixer {
|
|
9
|
-
instance;
|
|
10
|
-
logger;
|
|
11
|
-
reporter;
|
|
12
|
-
constructor(instance, logger, reporter) {
|
|
13
|
-
this.instance = instance;
|
|
14
|
-
this.logger = logger;
|
|
15
|
-
this.reporter = reporter;
|
|
16
|
-
}
|
|
17
|
-
async parseStructuredResponse(options) {
|
|
18
|
-
const { content, schema, jsonSchema, reporterContext, abortSignal, primaryUsage, } = options;
|
|
19
|
-
const sanitized = sanitizeJsonResponse(content);
|
|
20
|
-
try {
|
|
21
|
-
const parsed = JSON.parse(sanitized);
|
|
22
|
-
const validation = schema._parse(parsed);
|
|
23
|
-
if (validation.error) {
|
|
24
|
-
this.logger
|
|
25
|
-
?.child('parseStructuredResponse')
|
|
26
|
-
.warn('Schema validation failed, attempting fix', { validationError: validation.error });
|
|
27
|
-
return this.fixAndParseJson({
|
|
28
|
-
rawResponse: sanitized,
|
|
29
|
-
parsingError: validation.error,
|
|
30
|
-
schema,
|
|
31
|
-
jsonSchema,
|
|
32
|
-
reporterContext,
|
|
33
|
-
abortSignal,
|
|
34
|
-
primaryUsage,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
text: sanitized,
|
|
39
|
-
data: validation.data,
|
|
40
|
-
usage: primaryUsage,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
catch (parseError) {
|
|
44
|
-
this.logger
|
|
45
|
-
?.child('parseStructuredResponse')
|
|
46
|
-
.warn('Failed to parse JSON response, attempting fix', {
|
|
47
|
-
content: sanitized,
|
|
48
|
-
parseError: parseError.message,
|
|
49
|
-
});
|
|
50
|
-
return this.fixAndParseJson({
|
|
51
|
-
rawResponse: sanitized,
|
|
52
|
-
parsingError: parseError.message,
|
|
53
|
-
schema,
|
|
54
|
-
jsonSchema,
|
|
55
|
-
reporterContext,
|
|
56
|
-
abortSignal,
|
|
57
|
-
primaryUsage,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
async fixAndParseJson(options) {
|
|
62
|
-
const { rawResponse, parsingError, schema, jsonSchema, reporterContext, abortSignal, primaryUsage, } = options;
|
|
63
|
-
const fixWriteMetrics = this.reporter?.initWriter(reporterContext);
|
|
64
|
-
try {
|
|
65
|
-
abortSignal?.throwIfAborted();
|
|
66
|
-
const fixCompletion = await this.instance.chat.completions.create({
|
|
67
|
-
model: LLMAPI_typedefs_1.LLMAPI_JSON_FIX_MODEL.name,
|
|
68
|
-
messages: [
|
|
69
|
-
{
|
|
70
|
-
role: 'system',
|
|
71
|
-
content: 'You are a JSON fixer.'
|
|
72
|
-
+ ' Fix the provided broken or invalid JSON'
|
|
73
|
-
+ ' so it matches the expected schema.'
|
|
74
|
-
+ ' Return ONLY the corrected JSON'
|
|
75
|
-
+ ' without any explanations or formatting.',
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
role: 'user',
|
|
79
|
-
content: buildFixJsonPrompt({
|
|
80
|
-
rawResponse,
|
|
81
|
-
parsingError,
|
|
82
|
-
jsonSchema,
|
|
83
|
-
}),
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
n: 1,
|
|
87
|
-
...LLMAPI_typedefs_1.LLMAPI_JSON_FIX_MODEL.config,
|
|
88
|
-
response_format: { type: 'json_object' },
|
|
89
|
-
}, {
|
|
90
|
-
signal: abortSignal,
|
|
91
|
-
});
|
|
92
|
-
const fixedContent = sanitizeJsonResponse(fixCompletion.choices[0]?.message.content ?? '');
|
|
93
|
-
const fixPromptTokens = fixCompletion.usage?.prompt_tokens ?? 0;
|
|
94
|
-
const fixCachedTokens = fixCompletion.usage?.prompt_tokens_details
|
|
95
|
-
?.cached_tokens ?? 0;
|
|
96
|
-
const fixOutputTokens = fixCompletion.usage?.completion_tokens ?? 0;
|
|
97
|
-
const fixUsage = {
|
|
98
|
-
inputTextTokens: fixPromptTokens - fixCachedTokens,
|
|
99
|
-
inputCachedTextTokens: fixCachedTokens,
|
|
100
|
-
outputTextTokens: fixOutputTokens,
|
|
101
|
-
};
|
|
102
|
-
await writeSuccessMetrics({
|
|
103
|
-
model: LLMAPI_typedefs_1.LLMAPI_JSON_FIX_MODEL,
|
|
104
|
-
usage: fixUsage,
|
|
105
|
-
writeMetrics: fixWriteMetrics,
|
|
106
|
-
});
|
|
107
|
-
const combinedUsage = (0, utilities_1.addUsage)(primaryUsage, fixUsage);
|
|
108
|
-
const parsed = JSON.parse(fixedContent);
|
|
109
|
-
const validation = schema._parse(parsed);
|
|
110
|
-
if (validation.error) {
|
|
111
|
-
this.logger
|
|
112
|
-
?.child('fixAndParseJson')
|
|
113
|
-
.error('JSON fix attempt failed schema validation', {
|
|
114
|
-
fixedContent,
|
|
115
|
-
validationError: validation.error,
|
|
116
|
-
});
|
|
117
|
-
return {
|
|
118
|
-
text: fixedContent,
|
|
119
|
-
parseError: validation.error,
|
|
120
|
-
usage: combinedUsage,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
this.logger
|
|
124
|
-
?.child('fixAndParseJson')
|
|
125
|
-
.info('JSON fix succeeded');
|
|
126
|
-
return {
|
|
127
|
-
text: fixedContent,
|
|
128
|
-
data: validation.data,
|
|
129
|
-
usage: combinedUsage,
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
catch (fixError) {
|
|
133
|
-
await writeErrorMetrics({
|
|
134
|
-
model: LLMAPI_typedefs_1.LLMAPI_JSON_FIX_MODEL,
|
|
135
|
-
isAborted: abortSignal?.aborted,
|
|
136
|
-
writeMetrics: fixWriteMetrics,
|
|
137
|
-
});
|
|
138
|
-
const fixLogger = this.logger?.child('fixAndParseJson');
|
|
139
|
-
const fixErrorPayload = { error: (0, serializeError_1.serializeError)(fixError) };
|
|
140
|
-
if (abortSignal?.aborted) {
|
|
141
|
-
fixLogger?.warn('JSON fix request failed', fixErrorPayload);
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
fixLogger?.error('JSON fix request failed', fixErrorPayload);
|
|
145
|
-
}
|
|
146
|
-
return {
|
|
147
|
-
text: rawResponse,
|
|
148
|
-
error: fixError,
|
|
149
|
-
parseError: 'Failed to fix structured response from LLMAPI',
|
|
150
|
-
usage: primaryUsage,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
exports.LLMAPIJsonFixer = LLMAPIJsonFixer;
|
|
156
|
-
function sanitizeJsonResponse(response) {
|
|
157
|
-
return response
|
|
158
|
-
.replace(/^```(?:json)?/, '')
|
|
159
|
-
.replace(/```$/, '')
|
|
160
|
-
.trim();
|
|
161
|
-
}
|
|
162
|
-
function buildFixJsonPrompt(options) {
|
|
163
|
-
const { rawResponse, parsingError, jsonSchema } = options;
|
|
164
|
-
return [
|
|
165
|
-
'Fix the following JSON response'
|
|
166
|
-
+ ' so it matches the expected schema.',
|
|
167
|
-
'',
|
|
168
|
-
'Expected JSON Schema:',
|
|
169
|
-
JSON.stringify(jsonSchema, null, 2),
|
|
170
|
-
'',
|
|
171
|
-
'Broken JSON response:',
|
|
172
|
-
rawResponse,
|
|
173
|
-
'',
|
|
174
|
-
'Parsing error:',
|
|
175
|
-
parsingError,
|
|
176
|
-
'',
|
|
177
|
-
'Common issues to fix:',
|
|
178
|
-
'- Missing or extra commas',
|
|
179
|
-
'- Unescaped special characters in strings',
|
|
180
|
-
'- Missing quotes around keys or values',
|
|
181
|
-
'- Wrong field names (rename to match schema)',
|
|
182
|
-
'- Wrong field types (convert to match schema)',
|
|
183
|
-
'- Truncated JSON (complete it logically)',
|
|
184
|
-
'',
|
|
185
|
-
'Return ONLY the corrected JSON.',
|
|
186
|
-
].join('\n');
|
|
187
|
-
}
|
|
188
|
-
async function writeSuccessMetrics(params) {
|
|
189
|
-
const { model, usage: { inputTextTokens = 0, outputTextTokens = 0, inputAudioTokens = 0, outputAudioTokens = 0, }, writeMetrics, } = params;
|
|
190
|
-
if (!writeMetrics) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
const costs = (0, utilities_1.calculateCosts)(model, {
|
|
194
|
-
inputTextTokens,
|
|
195
|
-
outputTextTokens,
|
|
196
|
-
inputAudioTokens,
|
|
197
|
-
outputAudioTokens,
|
|
198
|
-
});
|
|
199
|
-
const input = inputTextTokens + inputAudioTokens;
|
|
200
|
-
const output = outputTextTokens + outputAudioTokens;
|
|
201
|
-
return writeMetrics({
|
|
202
|
-
provider: 'LLMAPI',
|
|
203
|
-
purpose: 'Completion',
|
|
204
|
-
model: model.name,
|
|
205
|
-
modelConfig: model.config ?? null,
|
|
206
|
-
method: 'fixAndParseJson',
|
|
207
|
-
status: 'success',
|
|
208
|
-
tokens: {
|
|
209
|
-
input,
|
|
210
|
-
output,
|
|
211
|
-
total: input + output,
|
|
212
|
-
},
|
|
213
|
-
costs,
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
async function writeErrorMetrics(params) {
|
|
217
|
-
const { model, isAborted = false, writeMetrics } = params;
|
|
218
|
-
if (!writeMetrics) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
return writeMetrics({
|
|
222
|
-
provider: 'LLMAPI',
|
|
223
|
-
purpose: 'Completion',
|
|
224
|
-
model: model.name,
|
|
225
|
-
modelConfig: model.config ?? null,
|
|
226
|
-
method: 'fixAndParseJson',
|
|
227
|
-
status: isAborted ? 'cancelled' : 'error',
|
|
228
|
-
tokens: { input: 0, output: 0, total: 0 },
|
|
229
|
-
costs: {
|
|
230
|
-
total: 0,
|
|
231
|
-
input: 0,
|
|
232
|
-
output: 0,
|
|
233
|
-
currency: 'USD',
|
|
234
|
-
},
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
//# sourceMappingURL=LLMAPI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ACL,CAAC;AAED,KAAK,UAAU,iBAAiB,CAAC,MAIhC;IACC,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,KAAK,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;IAE1D,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,OAAO;IACT,CAAC;IAED,OAAO,YAAY,CAAC;QAClB,QAAQ,EAAE,QAAwB;QAClC,OAAO,EAAE,YAAmB;QAC5B,KAAK,EAAE,KAAK,CAAC,IAAI;QACjB,WAAW,EAAE,KAAK,CAAC,MAAM,IAAI,IAAI;QACjC,MAAM,EAAE,iBAAiB;QACzB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,OAAO;QACzC,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE;QACzC,KAAK,EAAE;YACL,KAAK,EAAE,CAAC;YACR,KAAK,EAAE,CAAC;YACR,MAAM,EAAE,CAAC;YACT,QAAQ,EAAE,KAAK;SAChB;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export interface TruncateTraceIOOptions {
|
|
2
|
-
maxChars?: number;
|
|
3
|
-
}
|
|
4
|
-
/**
|
|
5
|
-
* Caps a single trace/observation IO value so a large prompt or completion
|
|
6
|
-
* cannot inflate trace payloads (the PR1 carry-over: `withTrace` serialized full
|
|
7
|
-
* results). Strings over the limit are sliced with a notice; objects are
|
|
8
|
-
* serialized first, then capped; non-serializable values fall back to `String`.
|
|
9
|
-
* Returns the value unchanged when it is already within budget so small payloads
|
|
10
|
-
* keep their original type in the UI.
|
|
11
|
-
*
|
|
12
|
-
* Applied by `LLMBaseService` to every generation it records, so each tracing
|
|
13
|
-
* path (completion, assistance, speech-to-text) is capped at the single point
|
|
14
|
-
* that stamps the observation rather than per call site.
|
|
15
|
-
*/
|
|
16
|
-
export declare function truncateTraceIO(value: unknown, options?: TruncateTraceIOOptions): unknown;
|
|
17
|
-
export declare const TRACE_IO_MAX_CHARS = 16384;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TRACE_IO_MAX_CHARS = void 0;
|
|
4
|
-
exports.truncateTraceIO = truncateTraceIO;
|
|
5
|
-
const DEFAULT_MAX_TRACE_IO_CHARS = 16_384;
|
|
6
|
-
const TRUNCATION_NOTICE = '…[truncated]';
|
|
7
|
-
/**
|
|
8
|
-
* Caps a single trace/observation IO value so a large prompt or completion
|
|
9
|
-
* cannot inflate trace payloads (the PR1 carry-over: `withTrace` serialized full
|
|
10
|
-
* results). Strings over the limit are sliced with a notice; objects are
|
|
11
|
-
* serialized first, then capped; non-serializable values fall back to `String`.
|
|
12
|
-
* Returns the value unchanged when it is already within budget so small payloads
|
|
13
|
-
* keep their original type in the UI.
|
|
14
|
-
*
|
|
15
|
-
* Applied by `LLMBaseService` to every generation it records, so each tracing
|
|
16
|
-
* path (completion, assistance, speech-to-text) is capped at the single point
|
|
17
|
-
* that stamps the observation rather than per call site.
|
|
18
|
-
*/
|
|
19
|
-
function truncateTraceIO(value, options = {}) {
|
|
20
|
-
const maxChars = options.maxChars ?? DEFAULT_MAX_TRACE_IO_CHARS;
|
|
21
|
-
if (value === undefined || value === null) {
|
|
22
|
-
return value;
|
|
23
|
-
}
|
|
24
|
-
if (typeof value === 'string') {
|
|
25
|
-
return capString(value, maxChars);
|
|
26
|
-
}
|
|
27
|
-
const serialized = safeSerialize(value);
|
|
28
|
-
if (serialized.length <= maxChars) {
|
|
29
|
-
return value;
|
|
30
|
-
}
|
|
31
|
-
return capString(serialized, maxChars);
|
|
32
|
-
}
|
|
33
|
-
function capString(text, maxChars) {
|
|
34
|
-
if (text.length <= maxChars) {
|
|
35
|
-
return text;
|
|
36
|
-
}
|
|
37
|
-
return `${text.slice(0, maxChars)}${TRUNCATION_NOTICE}`;
|
|
38
|
-
}
|
|
39
|
-
function safeSerialize(value) {
|
|
40
|
-
try {
|
|
41
|
-
return JSON.stringify(value) ?? String(value);
|
|
42
|
-
}
|
|
43
|
-
catch {
|
|
44
|
-
return String(value);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.TRACE_IO_MAX_CHARS = DEFAULT_MAX_TRACE_IO_CHARS;
|
|
48
|
-
//# sourceMappingURL=traceIO.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"traceIO.js","sourceRoot":"","sources":["../../src/utilities/traceIO.ts"],"names":[],"mappings":";;;AAoBA,0CAqBC;AAzCD,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAE1C,MAAM,iBAAiB,GAAG,cAAc,CAAC;AAMzC;;;;;;;;;;;GAWG;AACH,SAAgB,eAAe,CAC7B,KAAc,EACd,UAAkC,EAAE;IAEpC,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,0BAA0B,CAAC;IAEhE,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAC1C,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,SAAS,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,UAAU,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAExC,IAAI,UAAU,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAClC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,SAAS,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,SAAS,CAAC,IAAY,EAAE,QAAgB;IAC/C,IAAI,IAAI,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC5B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,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