@mate-academy/prompt-client 1.0.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.
Files changed (54) hide show
  1. package/README.md +251 -0
  2. package/dist/LLMPromptClient.constants.d.ts +2 -0
  3. package/dist/LLMPromptClient.constants.js +5 -0
  4. package/dist/LLMPromptClient.errors.d.ts +11 -0
  5. package/dist/LLMPromptClient.errors.js +26 -0
  6. package/dist/LLMPromptClient.typedefs.d.ts +22 -0
  7. package/dist/LLMPromptClient.typedefs.js +7 -0
  8. package/dist/LLMTracer.errors.d.ts +4 -0
  9. package/dist/LLMTracer.errors.js +11 -0
  10. package/dist/LLMTracer.typedefs.d.ts +45 -0
  11. package/dist/LLMTracer.typedefs.js +2 -0
  12. package/dist/PromptManagement.constants.d.ts +4 -0
  13. package/dist/PromptManagement.constants.js +10 -0
  14. package/dist/PromptManagement.factory.d.ts +2 -0
  15. package/dist/PromptManagement.factory.js +10 -0
  16. package/dist/PromptManagement.typedefs.d.ts +35 -0
  17. package/dist/PromptManagement.typedefs.js +8 -0
  18. package/dist/index.d.ts +14 -0
  19. package/dist/index.js +30 -0
  20. package/dist/providers/InMemory/InMemory.factory.d.ts +4 -0
  21. package/dist/providers/InMemory/InMemory.factory.js +19 -0
  22. package/dist/providers/InMemory/InMemory.typedefs.d.ts +26 -0
  23. package/dist/providers/InMemory/InMemory.typedefs.js +8 -0
  24. package/dist/providers/InMemory/InMemoryPrompt.client.d.ts +13 -0
  25. package/dist/providers/InMemory/InMemoryPrompt.client.js +61 -0
  26. package/dist/providers/InMemory/InMemoryTracer.client.d.ts +15 -0
  27. package/dist/providers/InMemory/InMemoryTracer.client.js +63 -0
  28. package/dist/providers/Langfuse/Langfuse.factory.d.ts +4 -0
  29. package/dist/providers/Langfuse/Langfuse.factory.js +23 -0
  30. package/dist/providers/Langfuse/Langfuse.helpers.d.ts +1 -0
  31. package/dist/providers/Langfuse/Langfuse.helpers.js +16 -0
  32. package/dist/providers/Langfuse/Langfuse.typedefs.d.ts +14 -0
  33. package/dist/providers/Langfuse/Langfuse.typedefs.js +2 -0
  34. package/dist/providers/Langfuse/LangfusePrompt.client.d.ts +16 -0
  35. package/dist/providers/Langfuse/LangfusePrompt.client.js +81 -0
  36. package/dist/providers/Langfuse/LangfusePrompt.d.ts +13 -0
  37. package/dist/providers/Langfuse/LangfusePrompt.js +29 -0
  38. package/dist/providers/Langfuse/LangfuseTracer.client.d.ts +13 -0
  39. package/dist/providers/Langfuse/LangfuseTracer.client.js +49 -0
  40. package/dist/utilities/index.d.ts +3 -0
  41. package/dist/utilities/index.js +19 -0
  42. package/dist/utilities/logger/PromptManagementLogger.d.ts +7 -0
  43. package/dist/utilities/logger/PromptManagementLogger.js +8 -0
  44. package/dist/utilities/logger/index.d.ts +1 -0
  45. package/dist/utilities/logger/index.js +17 -0
  46. package/dist/utilities/template/compileTemplateVariables.d.ts +1 -0
  47. package/dist/utilities/template/compileTemplateVariables.js +7 -0
  48. package/dist/utilities/template/index.d.ts +1 -0
  49. package/dist/utilities/template/index.js +17 -0
  50. package/dist/utilities/usageDetails/index.d.ts +1 -0
  51. package/dist/utilities/usageDetails/index.js +17 -0
  52. package/dist/utilities/usageDetails/usageDetails.helpers.d.ts +17 -0
  53. package/dist/utilities/usageDetails/usageDetails.helpers.js +33 -0
  54. package/package.json +69 -0
package/README.md ADDED
@@ -0,0 +1,251 @@
1
+ # @mate-academy/prompt-client
2
+
3
+ Provider-agnostic LLM prompt management and tracing client. Consumers code
4
+ against two stable interfaces — `LLMPromptClient` (fetch + compile prompts,
5
+ caching, fallbacks) and `LLMTracer` (traces + generations with usage/cost
6
+ details) — and pick a provider from `PromptManagementProviders`. The provider
7
+ can be swapped without touching call sites.
8
+
9
+ Providers:
10
+
11
+ - **`Langfuse`** — the real provider. One shared Langfuse SDK client backs both
12
+ the prompt client and the tracer.
13
+ - **`InMemory`** — a deterministic test double: seedable prompts and a
14
+ recording tracer for assertions. Use it in unit/integration tests instead of
15
+ hand-rolled mocks.
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ npm install @mate-academy/prompt-client
21
+ ```
22
+
23
+ ## Quick start
24
+
25
+ ```typescript
26
+ import {
27
+ createPromptManagement,
28
+ PromptManagementProviders,
29
+ } from '@mate-academy/prompt-client';
30
+
31
+ const promptManagement = createPromptManagement({
32
+ provider: PromptManagementProviders.Langfuse,
33
+ options: {
34
+ credentials: {
35
+ publicKey: process.env.LANGFUSE_PUBLIC_KEY ?? '',
36
+ secretKey: process.env.LANGFUSE_SECRET_KEY ?? '',
37
+ baseUrl: process.env.LANGFUSE_BASE_URL ?? '',
38
+ },
39
+ },
40
+ logger,
41
+ });
42
+
43
+ const prompt = await promptManagement.promptClient.getPrompt('my-prompt');
44
+ const instructions = prompt.compile({ leadName: 'Maria' });
45
+ ```
46
+
47
+ `createPromptManagement` returns a `PromptManagementBundle`:
48
+
49
+ | Member | Purpose |
50
+ |---|---|
51
+ | `promptClient` | `LLMPromptClient` — `getPrompt(name, options?)`, `shutdown()` |
52
+ | `tracer` | `LLMTracer` — `createTrace`, `createGeneration`, `flush`, `shutdown` |
53
+ | `flush()` | Flush pending trace/generation events without shutting down |
54
+ | `shutdown()` | Flush and stop the underlying client (whole bundle) |
55
+
56
+ `promptClient.shutdown()` and `tracer.shutdown()` stop the same shared client;
57
+ prefer `bundle.shutdown()`.
58
+
59
+ ## Prompts
60
+
61
+ ```typescript
62
+ const prompt = await promptClient.getPrompt('chatAgent.instructions', {
63
+ label: 'production', // default; mutually exclusive with version
64
+ version: 4, // pin an exact version (label is then omitted)
65
+ cacheTtlSeconds: 60, // default 60s, SDK-side cache + background refresh
66
+ fallback: FALLBACK_TEXT, // never throw: return this text on any failure
67
+ });
68
+
69
+ prompt.name; // 'chatAgent.instructions'
70
+ prompt.version; // Langfuse version, or 0 when the fallback was used
71
+ prompt.isFallback; // true when the fallback was served
72
+ prompt.config; // the config object stored on the Langfuse prompt
73
+ prompt.compile({ leadName: 'Maria' }); // mustache-style {{var}} substitution
74
+ ```
75
+
76
+ Error model (only when NO `fallback` is provided):
77
+
78
+ - `LLMPromptNotFoundError` — the prompt does not exist (safe to use as an
79
+ existence probe).
80
+ - `LLMPromptFetchError` — infrastructure failure (network/HTTP) or a non-text
81
+ prompt. Both extend `LLMPromptError` and carry `promptName` + `cause`.
82
+
83
+ With `fallback` set, `getPrompt` never rejects: on any failure it resolves to
84
+ the fallback text with `version: 0` and `isFallback: true`, and the logger
85
+ receives a warning.
86
+
87
+ ## Tracing
88
+
89
+ ```typescript
90
+ const trace = tracer.createTrace({
91
+ name: 'conversation-turn',
92
+ sessionId: chatId, // groups turns of one conversation in Langfuse
93
+ userId,
94
+ tags: ['sdr'],
95
+ input: inboundMessage,
96
+ });
97
+
98
+ const generation = tracer.createGeneration(trace, {
99
+ name: 'chat-completion',
100
+ model: modelName,
101
+ input: messages,
102
+ prompt, // links the generation to the Langfuse prompt version
103
+ });
104
+
105
+ generation.end({
106
+ output: completion,
107
+ usageDetails: usageToUsageDetails(response.usage),
108
+ costDetails: costToCostDetails(response.cost),
109
+ });
110
+
111
+ trace.update({ output: decision });
112
+ ```
113
+
114
+ `usageToUsageDetails` / `costToCostDetails` convert
115
+ `@mate-academy/llm-gateway` usage/cost results into the shape Langfuse expects;
116
+ their input types are structural, so no llm-gateway dependency is required.
117
+
118
+ ## Usage in a long-lived server (api)
119
+
120
+ A process can talk to several Langfuse projects. A project is a **runtime
121
+ argument, never baked into the client**: build one bundle per project (one
122
+ shared client each), memoize them behind a registry keyed by your own project
123
+ enum, and pass the project at the call site. Flush + stop every live bundle on
124
+ SIGTERM.
125
+
126
+ ```typescript
127
+ const bundles = new Map<LangfuseProject, PromptManagementBundle>();
128
+
129
+ const getPromptManagement = (
130
+ project: LangfuseProject,
131
+ ): PromptManagementBundle => {
132
+ const existing = bundles.get(project);
133
+
134
+ if (existing) {
135
+ return existing;
136
+ }
137
+
138
+ const bundle = createPromptManagement({
139
+ provider: PromptManagementProviders.Langfuse,
140
+ options: { credentials: appConfig.langfuse.projects[project] },
141
+ logger: rootLogger.child(`PromptManagement:${project}`),
142
+ });
143
+
144
+ bundles.set(project, bundle);
145
+
146
+ return bundle;
147
+ };
148
+
149
+ // at a call site — pick the project you need:
150
+ const { promptClient, tracer } = getPromptManagement(LangfuseProject.SalesQA);
151
+
152
+ // in graceful shutdown:
153
+ await Promise.all([...bundles.values()].map((bundle) => bundle.shutdown()));
154
+ ```
155
+
156
+ Memoizing per project is correctness, not caching: each bundle owns a live SDK
157
+ client with a background flush timer and buffered events, so the registry's
158
+ `Map` is the client's lifetime — one client per project per process.
159
+
160
+ Typed error handling keeps existing semantics: catch
161
+ `LLMPromptNotFoundError` for "is this conversation scorable?" probes and treat
162
+ `LLMPromptFetchError` as an infrastructure alert.
163
+
164
+ ## Usage in a Lambda
165
+
166
+ Create the bundle at module scope so it stays warm across invocations, use
167
+ per-prompt fallbacks so a Langfuse outage can never break a turn, and **flush
168
+ at the end of every invocation** — buffered trace events are lost when the
169
+ sandbox freezes:
170
+
171
+ ```typescript
172
+ const promptManagement = createPromptManagement({
173
+ provider: PromptManagementProviders.Langfuse,
174
+ options: {
175
+ credentials: {
176
+ publicKey: appConfig.langfusePublicKey,
177
+ secretKey: appConfig.langfuseSecretKey,
178
+ baseUrl: appConfig.langfuseBaseUrl,
179
+ },
180
+ flushAt: 1, // send events immediately; Lambdas have no idle time to batch
181
+ },
182
+ logger,
183
+ });
184
+
185
+ export const handler = async (event: SQSEvent): Promise<void> => {
186
+ try {
187
+ const prompt = await promptManagement.promptClient.getPrompt(
188
+ PromptKey.ChatAgentInstructions,
189
+ { fallback: FALLBACKS[PromptKey.ChatAgentInstructions] },
190
+ );
191
+ // ... run the turn, create trace/generations with sessionId: chatId
192
+ } finally {
193
+ await promptManagement.flush();
194
+ }
195
+ };
196
+ ```
197
+
198
+ ## Testing consumers with the InMemory provider
199
+
200
+ ```typescript
201
+ const promptManagement = createPromptManagement({
202
+ provider: PromptManagementProviders.InMemory,
203
+ options: {
204
+ prompts: {
205
+ 'chatAgent.instructions': { prompt: 'Reply to {{leadName}}', version: 3 },
206
+ },
207
+ // GenerateStub: unknown names resolve to `Mock prompt for <name>`
208
+ missingPromptBehavior: InMemoryMissingPromptBehaviors.GenerateStub,
209
+ },
210
+ });
211
+
212
+ // The InMemory bundle is typed with the concrete classes:
213
+ promptManagement.promptClient.setPrompt('closing', { prompt: 'Bye!' });
214
+ promptManagement.tracer.traces; // recorded traces + their updates
215
+ promptManagement.tracer.generations; // recorded generations + end payloads
216
+ promptManagement.tracer.flushCallCount; // flush()/shutdown() call counters
217
+ promptManagement.tracer.reset();
218
+ ```
219
+
220
+ ## Provider notes (Langfuse)
221
+
222
+ - `label` and `version` are mutually exclusive; when `version` is set the
223
+ label (including the `production` default) is omitted automatically.
224
+ - Fallback prompts have `version: 0` and `isFallback: true` (SDK semantics).
225
+ - Only `text` prompts are supported; a `chat` prompt is rejected with
226
+ `LLMPromptFetchError`. Chat support would arrive as an additive
227
+ `getChatPrompt` method.
228
+ - Pinned to the `langfuse` v3 SDK. Its only dynamic imports target Node
229
+ built-ins (`fs`, `crypto`) and are marked `webpackIgnore`, so the package is
230
+ safe to bundle with webpack/serverless-bundle for Lambdas.
231
+ - No SDK types leak through the public API; a future SDK swap stays inside
232
+ this package.
233
+
234
+ ## Development
235
+
236
+ ```bash
237
+ npm run lint
238
+ npm run type-check
239
+ npm test # unit tests, no network
240
+ npm run test:integration # requires .env.test (see .env.test.example)
241
+ npm run build
242
+ ```
243
+
244
+ ## Publishing
245
+
246
+ ```bash
247
+ npm run patch # or minor / major / canary
248
+ ```
249
+
250
+ First-ever publish of this scoped package must pass `--access public`:
251
+ `npm publish --access public`.
@@ -0,0 +1,2 @@
1
+ export declare const DEFAULT_PROMPT_LABEL = "production";
2
+ export declare const DEFAULT_PROMPT_CACHE_TTL_SECONDS = 60;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_PROMPT_CACHE_TTL_SECONDS = exports.DEFAULT_PROMPT_LABEL = void 0;
4
+ exports.DEFAULT_PROMPT_LABEL = 'production';
5
+ exports.DEFAULT_PROMPT_CACHE_TTL_SECONDS = 60;
@@ -0,0 +1,11 @@
1
+ export declare class LLMPromptError extends Error {
2
+ readonly promptName: string;
3
+ readonly cause?: unknown | undefined;
4
+ constructor(message: string, promptName: string, cause?: unknown | undefined);
5
+ }
6
+ export declare class LLMPromptNotFoundError extends LLMPromptError {
7
+ constructor(promptName: string, cause?: unknown);
8
+ }
9
+ export declare class LLMPromptFetchError extends LLMPromptError {
10
+ constructor(promptName: string, cause?: unknown);
11
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LLMPromptFetchError = exports.LLMPromptNotFoundError = exports.LLMPromptError = void 0;
4
+ class LLMPromptError extends Error {
5
+ constructor(message, promptName, cause) {
6
+ super(message);
7
+ this.promptName = promptName;
8
+ this.cause = cause;
9
+ this.name = 'LLMPromptError';
10
+ }
11
+ }
12
+ exports.LLMPromptError = LLMPromptError;
13
+ class LLMPromptNotFoundError extends LLMPromptError {
14
+ constructor(promptName, cause) {
15
+ super(`LLM prompt not found: ${promptName}`, promptName, cause);
16
+ this.name = 'LLMPromptNotFoundError';
17
+ }
18
+ }
19
+ exports.LLMPromptNotFoundError = LLMPromptNotFoundError;
20
+ class LLMPromptFetchError extends LLMPromptError {
21
+ constructor(promptName, cause) {
22
+ super(`Failed to fetch LLM prompt: ${promptName}`, promptName, cause);
23
+ this.name = 'LLMPromptFetchError';
24
+ }
25
+ }
26
+ exports.LLMPromptFetchError = LLMPromptFetchError;
@@ -0,0 +1,22 @@
1
+ export declare enum LLMPromptTypes {
2
+ Text = "text"
3
+ }
4
+ export interface LLMPrompt {
5
+ name: string;
6
+ version: number;
7
+ type: LLMPromptTypes;
8
+ prompt: string;
9
+ config: unknown;
10
+ isFallback: boolean;
11
+ compile(variables?: Record<string, string>): string;
12
+ }
13
+ export interface GetPromptOptions {
14
+ version?: number;
15
+ label?: string;
16
+ cacheTtlSeconds?: number;
17
+ fallback?: string;
18
+ }
19
+ export interface LLMPromptClient {
20
+ getPrompt(name: string, options?: GetPromptOptions): Promise<LLMPrompt>;
21
+ shutdown(): Promise<void>;
22
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LLMPromptTypes = void 0;
4
+ var LLMPromptTypes;
5
+ (function (LLMPromptTypes) {
6
+ LLMPromptTypes["Text"] = "text";
7
+ })(LLMPromptTypes || (exports.LLMPromptTypes = LLMPromptTypes = {}));
@@ -0,0 +1,4 @@
1
+ export declare class LLMTracerError extends Error {
2
+ readonly cause?: unknown | undefined;
3
+ constructor(message: string, cause?: unknown | undefined);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LLMTracerError = void 0;
4
+ class LLMTracerError extends Error {
5
+ constructor(message, cause) {
6
+ super(message);
7
+ this.cause = cause;
8
+ this.name = 'LLMTracerError';
9
+ }
10
+ }
11
+ exports.LLMTracerError = LLMTracerError;
@@ -0,0 +1,45 @@
1
+ import { type LLMPrompt } from './LLMPromptClient.typedefs';
2
+ export interface LLMTraceOptions {
3
+ name: string;
4
+ sessionId?: string;
5
+ userId?: string;
6
+ tags?: string[];
7
+ metadata?: Record<string, unknown>;
8
+ input?: unknown;
9
+ output?: unknown;
10
+ }
11
+ export interface LLMTraceUpdateOptions<Input = unknown, Output = unknown> {
12
+ input?: Input;
13
+ output?: Output;
14
+ metadata?: Record<string, unknown>;
15
+ }
16
+ export interface LLMTrace {
17
+ id: string;
18
+ getTraceUrl(): string;
19
+ update<Input = unknown, Output = unknown>(options: LLMTraceUpdateOptions<Input, Output>): void;
20
+ }
21
+ export interface LLMGenerationOptions {
22
+ name: string;
23
+ model?: string;
24
+ modelParameters?: Record<string, string | number | boolean | null>;
25
+ metadata?: Record<string, unknown>;
26
+ input?: unknown;
27
+ output?: unknown;
28
+ prompt?: LLMPrompt;
29
+ }
30
+ export interface LLMGenerationEndOptions {
31
+ output?: unknown;
32
+ metadata?: Record<string, unknown>;
33
+ usageDetails?: Record<string, number>;
34
+ costDetails?: Record<string, number>;
35
+ }
36
+ export interface LLMGeneration {
37
+ id: string;
38
+ end(options?: LLMGenerationEndOptions): void;
39
+ }
40
+ export interface LLMTracer {
41
+ createTrace(options: LLMTraceOptions): LLMTrace;
42
+ createGeneration(trace: LLMTrace, options: LLMGenerationOptions): LLMGeneration;
43
+ flush(): Promise<void>;
44
+ shutdown(): Promise<void>;
45
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export declare const PROMPT_MANAGEMENT_PROVIDER_FACTORIES: {
2
+ readonly Langfuse: (options: import(".").LangfuseProviderOptions, logger: import(".").PromptManagementLogger) => import("./PromptManagement.typedefs").PromptManagementBundle;
3
+ readonly InMemory: (options: import(".").InMemoryProviderOptions, _logger: import(".").PromptManagementLogger) => import("./PromptManagement.typedefs").InMemoryPromptManagementBundle;
4
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PROMPT_MANAGEMENT_PROVIDER_FACTORIES = void 0;
4
+ const PromptManagement_typedefs_1 = require("./PromptManagement.typedefs");
5
+ const InMemory_factory_1 = require("./providers/InMemory/InMemory.factory");
6
+ const Langfuse_factory_1 = require("./providers/Langfuse/Langfuse.factory");
7
+ exports.PROMPT_MANAGEMENT_PROVIDER_FACTORIES = {
8
+ [PromptManagement_typedefs_1.PromptManagementProviders.Langfuse]: Langfuse_factory_1.createLangfusePromptManagement,
9
+ [PromptManagement_typedefs_1.PromptManagementProviders.InMemory]: InMemory_factory_1.createInMemoryPromptManagement,
10
+ };
@@ -0,0 +1,2 @@
1
+ import { type PromptManagementBundles, type PromptManagementOptions, type PromptManagementProviders } from './PromptManagement.typedefs';
2
+ export declare const createPromptManagement: <Provider extends PromptManagementProviders>(options: PromptManagementOptions<Provider>) => PromptManagementBundles[Provider];
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPromptManagement = void 0;
4
+ const PromptManagement_constants_1 = require("./PromptManagement.constants");
5
+ const logger_1 = require("./utilities/logger");
6
+ const createPromptManagement = (options) => {
7
+ const providerFactory = PromptManagement_constants_1.PROMPT_MANAGEMENT_PROVIDER_FACTORIES[options.provider];
8
+ return providerFactory(options.options, options.logger ?? logger_1.NOOP_PROMPT_MANAGEMENT_LOGGER);
9
+ };
10
+ exports.createPromptManagement = createPromptManagement;
@@ -0,0 +1,35 @@
1
+ import { type LLMPromptClient } from './LLMPromptClient.typedefs';
2
+ import { type LLMTracer } from './LLMTracer.typedefs';
3
+ import { type InMemoryProviderOptions } from './providers/InMemory/InMemory.typedefs';
4
+ import { type InMemoryPromptClient } from './providers/InMemory/InMemoryPrompt.client';
5
+ import { type InMemoryTracer } from './providers/InMemory/InMemoryTracer.client';
6
+ import { type LangfuseProviderOptions } from './providers/Langfuse/Langfuse.typedefs';
7
+ import { type PromptManagementLogger } from './utilities/logger';
8
+ export declare enum PromptManagementProviders {
9
+ Langfuse = "Langfuse",
10
+ InMemory = "InMemory"
11
+ }
12
+ export interface PromptManagementProviderOptionsMap {
13
+ [PromptManagementProviders.Langfuse]: LangfuseProviderOptions;
14
+ [PromptManagementProviders.InMemory]: InMemoryProviderOptions;
15
+ }
16
+ export interface PromptManagementBundle {
17
+ promptClient: LLMPromptClient;
18
+ tracer: LLMTracer;
19
+ flush(): Promise<void>;
20
+ shutdown(): Promise<void>;
21
+ }
22
+ export interface InMemoryPromptManagementBundle extends PromptManagementBundle {
23
+ promptClient: InMemoryPromptClient;
24
+ tracer: InMemoryTracer;
25
+ }
26
+ export interface PromptManagementBundles {
27
+ [PromptManagementProviders.Langfuse]: PromptManagementBundle;
28
+ [PromptManagementProviders.InMemory]: InMemoryPromptManagementBundle;
29
+ }
30
+ export interface PromptManagementOptions<Provider extends PromptManagementProviders> {
31
+ provider: Provider;
32
+ options: PromptManagementProviderOptionsMap[Provider];
33
+ logger?: PromptManagementLogger;
34
+ }
35
+ export type PromptManagementProviderFactory<Provider extends PromptManagementProviders> = (options: PromptManagementProviderOptionsMap[Provider], logger: PromptManagementLogger) => PromptManagementBundles[Provider];
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PromptManagementProviders = void 0;
4
+ var PromptManagementProviders;
5
+ (function (PromptManagementProviders) {
6
+ PromptManagementProviders["Langfuse"] = "Langfuse";
7
+ PromptManagementProviders["InMemory"] = "InMemory";
8
+ })(PromptManagementProviders || (exports.PromptManagementProviders = PromptManagementProviders = {}));
@@ -0,0 +1,14 @@
1
+ export * from './LLMPromptClient.typedefs';
2
+ export * from './LLMPromptClient.constants';
3
+ export * from './LLMPromptClient.errors';
4
+ export * from './LLMTracer.typedefs';
5
+ export * from './LLMTracer.errors';
6
+ export * from './PromptManagement.typedefs';
7
+ export * from './PromptManagement.factory';
8
+ export * from './providers/Langfuse/Langfuse.typedefs';
9
+ export * from './providers/InMemory/InMemory.typedefs';
10
+ export * from './providers/InMemory/InMemoryPrompt.client';
11
+ export * from './providers/InMemory/InMemoryTracer.client';
12
+ export * from './utilities/logger';
13
+ export * from './utilities/template';
14
+ export * from './utilities/usageDetails';
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./LLMPromptClient.typedefs"), exports);
18
+ __exportStar(require("./LLMPromptClient.constants"), exports);
19
+ __exportStar(require("./LLMPromptClient.errors"), exports);
20
+ __exportStar(require("./LLMTracer.typedefs"), exports);
21
+ __exportStar(require("./LLMTracer.errors"), exports);
22
+ __exportStar(require("./PromptManagement.typedefs"), exports);
23
+ __exportStar(require("./PromptManagement.factory"), exports);
24
+ __exportStar(require("./providers/Langfuse/Langfuse.typedefs"), exports);
25
+ __exportStar(require("./providers/InMemory/InMemory.typedefs"), exports);
26
+ __exportStar(require("./providers/InMemory/InMemoryPrompt.client"), exports);
27
+ __exportStar(require("./providers/InMemory/InMemoryTracer.client"), exports);
28
+ __exportStar(require("./utilities/logger"), exports);
29
+ __exportStar(require("./utilities/template"), exports);
30
+ __exportStar(require("./utilities/usageDetails"), exports);
@@ -0,0 +1,4 @@
1
+ import { type InMemoryPromptManagementBundle } from '../../PromptManagement.typedefs';
2
+ import { type InMemoryProviderOptions } from '../../providers/InMemory/InMemory.typedefs';
3
+ import { type PromptManagementLogger } from '../../utilities/logger';
4
+ export declare const createInMemoryPromptManagement: (options: InMemoryProviderOptions, _logger: PromptManagementLogger) => InMemoryPromptManagementBundle;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createInMemoryPromptManagement = void 0;
4
+ const InMemoryPrompt_client_1 = require("../../providers/InMemory/InMemoryPrompt.client");
5
+ const InMemoryTracer_client_1 = require("../../providers/InMemory/InMemoryTracer.client");
6
+ const createInMemoryPromptManagement = (options, _logger) => {
7
+ const promptClient = new InMemoryPrompt_client_1.InMemoryPromptClient(options);
8
+ const tracer = new InMemoryTracer_client_1.InMemoryTracer();
9
+ return {
10
+ promptClient,
11
+ tracer,
12
+ flush: () => tracer.flush(),
13
+ shutdown: async () => {
14
+ await promptClient.shutdown();
15
+ await tracer.shutdown();
16
+ },
17
+ };
18
+ };
19
+ exports.createInMemoryPromptManagement = createInMemoryPromptManagement;
@@ -0,0 +1,26 @@
1
+ import { type LLMGenerationEndOptions, type LLMGenerationOptions, type LLMTraceOptions, type LLMTraceUpdateOptions } from '../../LLMTracer.typedefs';
2
+ export declare enum InMemoryMissingPromptBehaviors {
3
+ Throw = "Throw",
4
+ GenerateStub = "GenerateStub"
5
+ }
6
+ export interface InMemorySeedPrompt {
7
+ prompt: string;
8
+ version?: number;
9
+ config?: unknown;
10
+ }
11
+ export interface InMemoryProviderOptions {
12
+ prompts?: Record<string, InMemorySeedPrompt>;
13
+ missingPromptBehavior?: InMemoryMissingPromptBehaviors;
14
+ }
15
+ export interface InMemoryRecordedTrace {
16
+ id: string;
17
+ options: LLMTraceOptions;
18
+ updates: LLMTraceUpdateOptions[];
19
+ }
20
+ export interface InMemoryRecordedGeneration {
21
+ id: string;
22
+ traceId: string;
23
+ options: LLMGenerationOptions;
24
+ endOptions: LLMGenerationEndOptions | null;
25
+ isEnded: boolean;
26
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryMissingPromptBehaviors = void 0;
4
+ var InMemoryMissingPromptBehaviors;
5
+ (function (InMemoryMissingPromptBehaviors) {
6
+ InMemoryMissingPromptBehaviors["Throw"] = "Throw";
7
+ InMemoryMissingPromptBehaviors["GenerateStub"] = "GenerateStub";
8
+ })(InMemoryMissingPromptBehaviors || (exports.InMemoryMissingPromptBehaviors = InMemoryMissingPromptBehaviors = {}));
@@ -0,0 +1,13 @@
1
+ import { type GetPromptOptions, type LLMPrompt, type LLMPromptClient } from '../../LLMPromptClient.typedefs';
2
+ import { type InMemoryProviderOptions, type InMemorySeedPrompt } from '../../providers/InMemory/InMemory.typedefs';
3
+ export declare class InMemoryPromptClient implements LLMPromptClient {
4
+ private readonly prompts;
5
+ private readonly missingPromptBehavior;
6
+ constructor(options: InMemoryProviderOptions);
7
+ getPrompt(name: string, options?: GetPromptOptions): Promise<LLMPrompt>;
8
+ shutdown(): Promise<void>;
9
+ setPrompt(name: string, seedPrompt: InMemorySeedPrompt): void;
10
+ clear(): void;
11
+ private buildPrompt;
12
+ private buildFallbackPrompt;
13
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryPromptClient = void 0;
4
+ const LLMPromptClient_typedefs_1 = require("../../LLMPromptClient.typedefs");
5
+ const LLMPromptClient_errors_1 = require("../../LLMPromptClient.errors");
6
+ const InMemory_typedefs_1 = require("../../providers/InMemory/InMemory.typedefs");
7
+ const template_1 = require("../../utilities/template");
8
+ const DEFAULT_SEED_PROMPT_VERSION = 1;
9
+ const FALLBACK_PROMPT_VERSION = 0;
10
+ class InMemoryPromptClient {
11
+ constructor(options) {
12
+ this.prompts = new Map(Object.entries(options.prompts ?? {}));
13
+ this.missingPromptBehavior = options.missingPromptBehavior
14
+ ?? InMemory_typedefs_1.InMemoryMissingPromptBehaviors.Throw;
15
+ }
16
+ async getPrompt(name, options) {
17
+ const seedPrompt = this.prompts.get(name);
18
+ if (seedPrompt) {
19
+ return this.buildPrompt(name, seedPrompt);
20
+ }
21
+ if (options?.fallback !== undefined) {
22
+ return this.buildFallbackPrompt(name, options.fallback);
23
+ }
24
+ if (this.missingPromptBehavior === InMemory_typedefs_1.InMemoryMissingPromptBehaviors.GenerateStub) {
25
+ return this.buildPrompt(name, { prompt: `Mock prompt for ${name}` });
26
+ }
27
+ throw new LLMPromptClient_errors_1.LLMPromptNotFoundError(name);
28
+ }
29
+ shutdown() {
30
+ return Promise.resolve();
31
+ }
32
+ setPrompt(name, seedPrompt) {
33
+ this.prompts.set(name, seedPrompt);
34
+ }
35
+ clear() {
36
+ this.prompts.clear();
37
+ }
38
+ buildPrompt(name, seedPrompt) {
39
+ return {
40
+ name,
41
+ version: seedPrompt.version ?? DEFAULT_SEED_PROMPT_VERSION,
42
+ type: LLMPromptClient_typedefs_1.LLMPromptTypes.Text,
43
+ prompt: seedPrompt.prompt,
44
+ config: seedPrompt.config ?? {},
45
+ isFallback: false,
46
+ compile: (variables) => (0, template_1.compileTemplateVariables)(seedPrompt.prompt, variables),
47
+ };
48
+ }
49
+ buildFallbackPrompt(name, fallback) {
50
+ return {
51
+ name,
52
+ version: FALLBACK_PROMPT_VERSION,
53
+ type: LLMPromptClient_typedefs_1.LLMPromptTypes.Text,
54
+ prompt: fallback,
55
+ config: {},
56
+ isFallback: true,
57
+ compile: (variables) => (0, template_1.compileTemplateVariables)(fallback, variables),
58
+ };
59
+ }
60
+ }
61
+ exports.InMemoryPromptClient = InMemoryPromptClient;