@launchdarkly/server-sdk-ai 0.1.0 → 0.1.1

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 (42) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +14 -4
  3. package/__tests__/LDAIClientImpl.test.ts +136 -0
  4. package/__tests__/LDAIConfigTrackerImpl.test.ts +270 -0
  5. package/__tests__/TokenUsage.test.ts +78 -0
  6. package/dist/LDAIClientImpl.d.ts.map +1 -1
  7. package/dist/LDAIClientImpl.js +4 -1
  8. package/dist/LDAIClientImpl.js.map +1 -1
  9. package/dist/api/LDAIClient.d.ts +27 -20
  10. package/dist/api/LDAIClient.d.ts.map +1 -1
  11. package/dist/api/metrics/index.d.ts +0 -1
  12. package/dist/api/metrics/index.d.ts.map +1 -1
  13. package/dist/api/metrics/index.js +0 -1
  14. package/dist/api/metrics/index.js.map +1 -1
  15. package/dist/index.d.ts +8 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js.map +1 -1
  18. package/docs/assets/navigation.js +1 -1
  19. package/docs/assets/search.js +1 -1
  20. package/docs/enums/LDFeedbackKind.html +4 -4
  21. package/docs/functions/createBedrockTokenUsage.html +1 -1
  22. package/docs/functions/initAi.html +2 -2
  23. package/docs/index.html +5 -3
  24. package/docs/interfaces/LDAIClient.html +15 -14
  25. package/docs/interfaces/LDAIConfig.html +5 -5
  26. package/docs/interfaces/LDAIConfigTracker.html +9 -9
  27. package/docs/interfaces/LDGenerationConfig.html +4 -4
  28. package/docs/interfaces/LDMessage.html +4 -4
  29. package/docs/interfaces/LDModelConfig.html +3 -3
  30. package/docs/interfaces/LDTokenUsage.html +5 -5
  31. package/jest.config.js +7 -0
  32. package/package.json +2 -2
  33. package/src/LDAIClientImpl.ts +4 -1
  34. package/src/api/LDAIClient.ts +27 -20
  35. package/src/api/metrics/index.ts +0 -1
  36. package/src/index.ts +8 -0
  37. package/dist/api/metrics/UnderScoreTokenUsage.d.ts +0 -3
  38. package/dist/api/metrics/UnderScoreTokenUsage.d.ts.map +0 -1
  39. package/dist/api/metrics/UnderScoreTokenUsage.js +0 -12
  40. package/dist/api/metrics/UnderScoreTokenUsage.js.map +0 -1
  41. package/docs/functions/createUnderscoreTokenUsage.html +0 -1
  42. package/src/api/metrics/UnderScoreTokenUsage.ts +0 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@launchdarkly/server-sdk-ai",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "LaunchDarkly AI SDK for Server-Side JavaScript",
5
5
  "homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/sdk/server-ai",
6
6
  "repository": {
@@ -16,7 +16,7 @@
16
16
  "prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
17
17
  "lint:fix": "yarn run lint --fix",
18
18
  "check": "yarn prettier && yarn lint && yarn build && yarn test",
19
- "test": "echo No tests added yet."
19
+ "test": "jest"
20
20
  },
21
21
  "keywords": [
22
22
  "launchdarkly",
@@ -41,7 +41,10 @@ export class LDAIClientImpl implements LDAIClient {
41
41
  const value: VariationContent = await this._ldClient.variation(key, context, defaultValue);
42
42
  // We are going to modify the contents before returning them, so we make a copy.
43
43
  // This isn't a deep copy and the application developer should not modify the returned content.
44
- const config: LDGenerationConfig = { ...value };
44
+ const config: LDGenerationConfig = {};
45
+ if (value.model) {
46
+ config.model = { ...value.model };
47
+ }
45
48
  const allVariables = { ...variables, ldctx: context };
46
49
 
47
50
  if (value.prompt) {
@@ -10,44 +10,50 @@ export interface LDAIClient {
10
10
  /**
11
11
  * Parses and interpolates a template string with the provided variables.
12
12
  *
13
- * @param template - The template string to be parsed and interpolated.
14
- * @param variables - An object containing the variables to be used for interpolation.
13
+ * @param template The template string to be parsed and interpolated.
14
+ * @param variables An object containing the variables to be used for interpolation.
15
15
  * @returns The interpolated string.
16
16
  */
17
17
  interpolateTemplate(template: string, variables: Record<string, unknown>): string;
18
18
 
19
19
  /**
20
- * Retrieves and processes a prompt template based on the provided key, LaunchDarkly context, and
21
- * variables.
20
+ * Retrieves and processes an AI configuration based on the provided key, LaunchDarkly context,
21
+ * and variables. This includes the model configuration and the processed prompts.
22
22
  *
23
- * @param key - A unique identifier for the prompt template. This key is used to fetch the correct
24
- * prompt from storage or configuration.
25
- * @param context - The LaunchDarkly context object that contains relevant information about the
26
- * current environment, user, or session. This context may influence how the prompt is processed
27
- * or personalized.
28
- * @param variables - A map of key-value pairs representing dynamic variables to be injected into
23
+ * @param key The key of the AI configuration.
24
+ * @param context The LaunchDarkly context object that contains relevant information about the
25
+ * current environment, user, or session. This context may influence how the configuration is
26
+ * processed or personalized.
27
+ * @param variables A map of key-value pairs representing dynamic variables to be injected into
29
28
  * the prompt template. The keys correspond to placeholders within the template, and the values
30
29
  * are the corresponding replacements.
31
- * @param defaultValue - A fallback value to be used if the prompt template associated with the
32
- * key is not found or if any errors occur during processing.
30
+ * @param defaultValue A fallback value containing model configuration and prompts. This will
31
+ * be used if the configurationuration is not available from launchdarkly.
33
32
  *
34
- * @returns The processed prompt after all variables have been substituted in the stored prompt
35
- * template. If the prompt cannot be retrieved or processed, the `defaultValue` is returned.
33
+ * @returns The AI configurationuration including a processed prompt after all variables have been
34
+ * substituted in the stored prompt template. This will also include a `tracker` used to track
35
+ * the state of the AI operation. If the configuration cannot be accessed from LaunchDarkly, then
36
+ * the return value will include information from the defaultValue.
36
37
  *
37
38
  * @example
38
39
  * ```
39
40
  * const key = "welcome_prompt";
40
41
  * const context = {...};
41
42
  * const variables = {username: 'john'};
42
- * const defaultValue = {};
43
+ * const defaultValue = {
44
+ * enabled: false,
45
+ * };
43
46
  *
44
47
  * const result = modelConfig(key, context, defaultValue, variables);
45
48
  * // Output:
46
49
  * {
47
- * modelId: "gpt-4o",
48
- * temperature: 0.2,
49
- * maxTokens: 4096,
50
- * userDefinedKey: "myValue",
50
+ * enabled: true,
51
+ * config: {
52
+ * modelId: "gpt-4o",
53
+ * temperature: 0.2,
54
+ * maxTokens: 4096,
55
+ * userDefinedKey: "myValue",
56
+ * },
51
57
  * prompt: [
52
58
  * {
53
59
  * role: "system",
@@ -57,7 +63,8 @@ export interface LDAIClient {
57
63
  * role: "user",
58
64
  * content: "Explain how you're an amazing GPT."
59
65
  * }
60
- * ]
66
+ * ],
67
+ * tracker: ...
61
68
  * }
62
69
  * ```
63
70
  */
@@ -1,4 +1,3 @@
1
1
  export * from './BedrockTokenUsage';
2
2
  export * from './LDFeedbackKind';
3
3
  export * from './LDTokenUsage';
4
- export * from './UnderScoreTokenUsage';
package/src/index.ts CHANGED
@@ -1,3 +1,11 @@
1
+ /**
2
+ * This is the API reference for the LaunchDarkly AI SDK for Server-Side JavaScript.
3
+ *
4
+ * In typical usage, you will call {@link initAi} once at startup time to obtain an instance of
5
+ * {@link LDAIClient}, which provides access to all of the SDK's functionality.
6
+ *
7
+ * @packageDocumentation
8
+ */
1
9
  import { LDAIClient } from './api/LDAIClient';
2
10
  import { LDAIClientImpl } from './LDAIClientImpl';
3
11
  import { LDClientMin } from './LDClientMin';
@@ -1,3 +0,0 @@
1
- import { LDTokenUsage } from './LDTokenUsage';
2
- export declare function createUnderscoreTokenUsage(data: any): LDTokenUsage;
3
- //# sourceMappingURL=UnderScoreTokenUsage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"UnderScoreTokenUsage.d.ts","sourceRoot":"","sources":["../../../src/api/metrics/UnderScoreTokenUsage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,GAAG,GAAG,YAAY,CAMlE"}
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createUnderscoreTokenUsage = void 0;
4
- function createUnderscoreTokenUsage(data) {
5
- return {
6
- total: data.total_tokens || 0,
7
- input: data.prompt_tokens || 0,
8
- output: data.completion_tokens || 0,
9
- };
10
- }
11
- exports.createUnderscoreTokenUsage = createUnderscoreTokenUsage;
12
- //# sourceMappingURL=UnderScoreTokenUsage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"UnderScoreTokenUsage.js","sourceRoot":"","sources":["../../../src/api/metrics/UnderScoreTokenUsage.ts"],"names":[],"mappings":";;;AAEA,SAAgB,0BAA0B,CAAC,IAAS;IAClD,OAAO;QACL,KAAK,EAAE,IAAI,CAAC,YAAY,IAAI,CAAC;QAC7B,KAAK,EAAE,IAAI,CAAC,aAAa,IAAI,CAAC;QAC9B,MAAM,EAAE,IAAI,CAAC,iBAAiB,IAAI,CAAC;KACpC,CAAC;AACJ,CAAC;AAND,gEAMC"}
@@ -1 +0,0 @@
1
- <!DOCTYPE html><html class="default" lang="en"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>createUnderscoreTokenUsage | @launchdarkly/server-sdk-ai - v0.1.0</title><meta name="description" content="Documentation for @launchdarkly/server-sdk-ai"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><div class="table-cell" id="tsd-search" data-base=".."><div class="field"><label for="tsd-search-field" class="tsd-widget tsd-toolbar-icon search no-caption"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="../assets/icons.svg#icon-search"></use></svg></label><input type="text" id="tsd-search-field" aria-label="Search"/></div><div class="field"><div id="tsd-toolbar-links"></div></div><ul class="results"><li class="state loading">Preparing search index...</li><li class="state failure">The search index is not available</li></ul><a href="../index.html" class="title">@launchdarkly/server-sdk-ai - v0.1.0</a></div><div class="table-cell" id="tsd-widgets"><a href="#" class="tsd-widget tsd-toolbar-icon menu no-caption" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb"><li><a href="../index.html">@launchdarkly/server-sdk-ai</a></li><li><a href="createUnderscoreTokenUsage.html">createUnderscoreTokenUsage</a></li></ul><h1>Function createUnderscoreTokenUsage</h1></div><section class="tsd-panel"><ul class="tsd-signatures"><li class="tsd-signature tsd-anchor-link"><a id="createUnderscoreTokenUsage" class="tsd-anchor"></a><span class="tsd-kind-call-signature">create<wbr/>Underscore<wbr/>Token<wbr/>Usage</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">data</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="../interfaces/LDTokenUsage.html" class="tsd-signature-type tsd-kind-interface">LDTokenUsage</a><a href="#createUnderscoreTokenUsage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></li><li class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">data</span>: <span class="tsd-signature-type">any</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <a href="../interfaces/LDTokenUsage.html" class="tsd-signature-type tsd-kind-interface">LDTokenUsage</a></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/launchdarkly/js-core/blob/cc35e9ed1c2a3fe9d6cacb7e9654b9160e90879c/packages/sdk/server-ai/src/api/metrics/UnderScoreTokenUsage.ts#L3">api/metrics/UnderScoreTokenUsage.ts:3</a></li></ul></aside></li></ul></section></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div></div><div class="site-menu"><nav class="tsd-navigation"><a href="../index.html"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="../assets/icons.svg#icon-1"></use></svg><span>@launchdarkly/server-sdk-ai - v0.1.0</span></a><ul class="tsd-small-nested-navigation" id="tsd-nav-container" data-base=".."><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
@@ -1,9 +0,0 @@
1
- import { LDTokenUsage } from './LDTokenUsage';
2
-
3
- export function createUnderscoreTokenUsage(data: any): LDTokenUsage {
4
- return {
5
- total: data.total_tokens || 0,
6
- input: data.prompt_tokens || 0,
7
- output: data.completion_tokens || 0,
8
- };
9
- }