@promptev/client 0.0.1 → 0.0.3

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.
@@ -1,37 +1,4 @@
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
36
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
4
  };
@@ -40,102 +7,51 @@ exports.PromptevClient = void 0;
40
7
  const axios_1 = __importDefault(require("axios"));
41
8
  class PromptevClient {
42
9
  constructor(config) {
43
- this.refreshInterval = null;
44
- this.baseUrl = config.baseUrl || "https://api.promptev.ai";
10
+ this.baseUrl = config.baseUrl ?? "https://api.promptev.ai";
45
11
  this.projectKey = config.projectKey;
46
- this.cacheRefreshIntervalMs = config.cacheRefreshIntervalMs || 30000;
47
- this.client = axios_1.default.create({ baseURL: this.baseUrl });
48
- const isNode = typeof window === "undefined";
49
- this.isReady = (async () => {
50
- if (isNode) {
51
- const { default: NodeCache } = await Promise.resolve().then(() => __importStar(require("node-cache")));
52
- this.promptCache = new NodeCache({
53
- stdTTL: 3600,
54
- checkperiod: 120,
55
- useClones: false,
56
- });
57
- }
58
- else {
59
- this.promptCache = new Map();
60
- }
61
- this.startCacheRefresh();
62
- })();
63
- }
64
- async ensureReady() {
65
- await this.isReady;
66
- }
67
- makeCacheKey(promptKey, variables) {
68
- const sorted = Object.keys(variables)
69
- .sort()
70
- .map(k => `${k}=${variables[k]}`)
71
- .join("&");
72
- return `${promptKey}:${btoa(sorted)}`;
73
- }
74
- async fetchPromptFromServer(promptKey) {
12
+ this.client = axios_1.default.create({
13
+ baseURL: this.baseUrl,
14
+ headers: {
15
+ "Content-Type": "application/json",
16
+ ...(config.headers ?? {}),
17
+ },
18
+ });
19
+ }
20
+ /**
21
+ * Compile a prompt by POSTing variables.
22
+ * Body shape (exact):
23
+ * {
24
+ * "variables": {
25
+ * "additionalProp1": "string",
26
+ * "additionalProp2": "string",
27
+ * "additionalProp3": "string"
28
+ * }
29
+ * }
30
+ */
31
+ async getPrompt(promptKey, variables = {}, opts) {
75
32
  const url = `/api/sdk/v1/prompt/client/${this.projectKey}/${promptKey}`;
76
- const response = await this.client.get(url);
77
- const rawPrompt = response.data.prompt;
78
- const rawVars = Array.isArray(response.data.variables)
79
- ? response.data.variables
80
- : typeof response.data.variables === "string"
81
- ? response.data.variables.split(",").map(v => v.trim())
82
- : [];
83
- return { prompt: rawPrompt, variables: rawVars };
84
- }
85
- async formatPrompt(template, requiredVars, values) {
86
- if (!requiredVars.length)
87
- return template;
88
- const missing = requiredVars.filter(v => !(v in values));
89
- if (missing.length)
90
- throw new Error(`Missing variables: ${missing.join(", ")}`);
91
- let formatted = template;
92
- for (const [key, val] of Object.entries(values)) {
93
- const pattern = new RegExp(`{{\\s*${key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*}}`, "g");
94
- formatted = formatted.replace(pattern, val);
95
- }
96
- return formatted;
97
- }
98
- startCacheRefresh() {
99
- if (this.refreshInterval)
100
- clearInterval(this.refreshInterval);
101
- this.refreshInterval = setInterval(async () => {
102
- await this.ensureReady();
103
- const keys = Array.from(this.promptCache instanceof Map ? this.promptCache.keys() : this.promptCache.keys());
104
- for (const cacheKey of keys) {
105
- const [promptKey, encoded] = cacheKey.split(":");
106
- const vars = JSON.parse(atob(encoded));
107
- try {
108
- await this.fetchAndCache(promptKey, vars);
109
- }
110
- catch { }
33
+ try {
34
+ const resp = await this.client.post(url, { variables }, // <-- exact body
35
+ { signal: opts?.signal });
36
+ // Success shape: { prompt: string }
37
+ const compiled = resp.data?.prompt;
38
+ if (typeof compiled !== "string") {
39
+ throw new Error("Unexpected response: missing 'prompt' field");
111
40
  }
112
- }, this.cacheRefreshIntervalMs);
113
- }
114
- async fetchAndCache(promptKey, variables) {
115
- const data = await this.fetchPromptFromServer(promptKey);
116
- const compiled = await this.formatPrompt(data.prompt, data.variables, variables);
117
- const cacheKey = this.makeCacheKey(promptKey, variables);
118
- this.promptCache.set(cacheKey, compiled);
119
- return compiled;
120
- }
121
- async getPrompt(promptKey, variables = {}) {
122
- await this.ensureReady();
123
- const cacheKey = this.makeCacheKey(promptKey, variables);
124
- const cached = this.promptCache.get(cacheKey);
125
- if (cached)
126
- return cached;
127
- return await this.fetchAndCache(promptKey, variables);
128
- }
129
- async dispose() {
130
- await this.ensureReady();
131
- if (this.refreshInterval)
132
- clearInterval(this.refreshInterval);
133
- if (this.promptCache instanceof Map) {
134
- this.promptCache.clear();
41
+ return compiled;
135
42
  }
136
- else {
137
- this.promptCache.flushAll();
138
- this.promptCache.close();
43
+ catch (err) {
44
+ const ax = err;
45
+ const status = ax.response?.status;
46
+ const body = ax.response?.data;
47
+ const detail = body?.detail ?? body;
48
+ // Your API returns detail as a plain string on 400
49
+ if (typeof detail === "string") {
50
+ throw new Error(detail);
51
+ }
52
+ // Fallback error
53
+ const msg = `Request failed${status ? ` (${status})` : ""}`;
54
+ throw new Error(msg);
139
55
  }
140
56
  }
141
57
  }
@@ -1,24 +1,26 @@
1
1
  export interface PromptevClientConfig {
2
2
  baseUrl?: string;
3
3
  projectKey: string;
4
- cacheRefreshIntervalMs?: number;
4
+ headers?: Record<string, string>;
5
5
  }
6
6
  export declare class PromptevClient {
7
7
  private client;
8
8
  private baseUrl;
9
9
  private projectKey;
10
- private promptCache;
11
- private refreshInterval;
12
- private cacheRefreshIntervalMs;
13
- private isReady;
14
10
  constructor(config: PromptevClientConfig);
15
- private ensureReady;
16
- private makeCacheKey;
17
- private fetchPromptFromServer;
18
- private formatPrompt;
19
- private startCacheRefresh;
20
- private fetchAndCache;
21
- getPrompt(promptKey: string, variables?: Record<string, string>): Promise<string>;
22
- dispose(): Promise<void>;
11
+ /**
12
+ * Compile a prompt by POSTing variables.
13
+ * Body shape (exact):
14
+ * {
15
+ * "variables": {
16
+ * "additionalProp1": "string",
17
+ * "additionalProp2": "string",
18
+ * "additionalProp3": "string"
19
+ * }
20
+ * }
21
+ */
22
+ getPrompt(promptKey: string, variables?: Record<string, string>, opts?: {
23
+ signal?: AbortSignal;
24
+ }): Promise<string>;
23
25
  }
24
26
  export default PromptevClient;
package/dist/esm/index.js CHANGED
@@ -1,102 +1,51 @@
1
1
  import axios from "axios";
2
2
  export class PromptevClient {
3
3
  constructor(config) {
4
- this.refreshInterval = null;
5
- this.baseUrl = config.baseUrl || "https://api.promptev.ai";
4
+ this.baseUrl = config.baseUrl ?? "https://api.promptev.ai";
6
5
  this.projectKey = config.projectKey;
7
- this.cacheRefreshIntervalMs = config.cacheRefreshIntervalMs || 30000;
8
- this.client = axios.create({ baseURL: this.baseUrl });
9
- const isNode = typeof window === "undefined";
10
- this.isReady = (async () => {
11
- if (isNode) {
12
- const { default: NodeCache } = await import("node-cache");
13
- this.promptCache = new NodeCache({
14
- stdTTL: 3600,
15
- checkperiod: 120,
16
- useClones: false,
17
- });
18
- }
19
- else {
20
- this.promptCache = new Map();
21
- }
22
- this.startCacheRefresh();
23
- })();
24
- }
25
- async ensureReady() {
26
- await this.isReady;
27
- }
28
- makeCacheKey(promptKey, variables) {
29
- const sorted = Object.keys(variables)
30
- .sort()
31
- .map(k => `${k}=${variables[k]}`)
32
- .join("&");
33
- return `${promptKey}:${btoa(sorted)}`;
34
- }
35
- async fetchPromptFromServer(promptKey) {
6
+ this.client = axios.create({
7
+ baseURL: this.baseUrl,
8
+ headers: {
9
+ "Content-Type": "application/json",
10
+ ...(config.headers ?? {}),
11
+ },
12
+ });
13
+ }
14
+ /**
15
+ * Compile a prompt by POSTing variables.
16
+ * Body shape (exact):
17
+ * {
18
+ * "variables": {
19
+ * "additionalProp1": "string",
20
+ * "additionalProp2": "string",
21
+ * "additionalProp3": "string"
22
+ * }
23
+ * }
24
+ */
25
+ async getPrompt(promptKey, variables = {}, opts) {
36
26
  const url = `/api/sdk/v1/prompt/client/${this.projectKey}/${promptKey}`;
37
- const response = await this.client.get(url);
38
- const rawPrompt = response.data.prompt;
39
- const rawVars = Array.isArray(response.data.variables)
40
- ? response.data.variables
41
- : typeof response.data.variables === "string"
42
- ? response.data.variables.split(",").map(v => v.trim())
43
- : [];
44
- return { prompt: rawPrompt, variables: rawVars };
45
- }
46
- async formatPrompt(template, requiredVars, values) {
47
- if (!requiredVars.length)
48
- return template;
49
- const missing = requiredVars.filter(v => !(v in values));
50
- if (missing.length)
51
- throw new Error(`Missing variables: ${missing.join(", ")}`);
52
- let formatted = template;
53
- for (const [key, val] of Object.entries(values)) {
54
- const pattern = new RegExp(`{{\\s*${key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*}}`, "g");
55
- formatted = formatted.replace(pattern, val);
56
- }
57
- return formatted;
58
- }
59
- startCacheRefresh() {
60
- if (this.refreshInterval)
61
- clearInterval(this.refreshInterval);
62
- this.refreshInterval = setInterval(async () => {
63
- await this.ensureReady();
64
- const keys = Array.from(this.promptCache instanceof Map ? this.promptCache.keys() : this.promptCache.keys());
65
- for (const cacheKey of keys) {
66
- const [promptKey, encoded] = cacheKey.split(":");
67
- const vars = JSON.parse(atob(encoded));
68
- try {
69
- await this.fetchAndCache(promptKey, vars);
70
- }
71
- catch { }
27
+ try {
28
+ const resp = await this.client.post(url, { variables }, // <-- exact body
29
+ { signal: opts?.signal });
30
+ // Success shape: { prompt: string }
31
+ const compiled = resp.data?.prompt;
32
+ if (typeof compiled !== "string") {
33
+ throw new Error("Unexpected response: missing 'prompt' field");
72
34
  }
73
- }, this.cacheRefreshIntervalMs);
74
- }
75
- async fetchAndCache(promptKey, variables) {
76
- const data = await this.fetchPromptFromServer(promptKey);
77
- const compiled = await this.formatPrompt(data.prompt, data.variables, variables);
78
- const cacheKey = this.makeCacheKey(promptKey, variables);
79
- this.promptCache.set(cacheKey, compiled);
80
- return compiled;
81
- }
82
- async getPrompt(promptKey, variables = {}) {
83
- await this.ensureReady();
84
- const cacheKey = this.makeCacheKey(promptKey, variables);
85
- const cached = this.promptCache.get(cacheKey);
86
- if (cached)
87
- return cached;
88
- return await this.fetchAndCache(promptKey, variables);
89
- }
90
- async dispose() {
91
- await this.ensureReady();
92
- if (this.refreshInterval)
93
- clearInterval(this.refreshInterval);
94
- if (this.promptCache instanceof Map) {
95
- this.promptCache.clear();
35
+ return compiled;
96
36
  }
97
- else {
98
- this.promptCache.flushAll();
99
- this.promptCache.close();
37
+ catch (err) {
38
+ const ax = err;
39
+ const status = ax.response?.status;
40
+ const body = ax.response?.data;
41
+ const detail = body?.detail ?? body;
42
+ // Your API returns detail as a plain string on 400
43
+ if (typeof detail === "string") {
44
+ throw new Error(detail);
45
+ }
46
+ // Fallback error
47
+ const msg = `Request failed${status ? ` (${status})` : ""}`;
48
+ throw new Error(msg);
100
49
  }
101
50
  }
102
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptev/client",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Client library for accessing Promptev API",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.cjs",
@@ -19,7 +19,7 @@
19
19
  "publishConfig": {
20
20
  "access": "public"
21
21
  },
22
- "homepage": "https://promptliy.ai",
22
+ "homepage": "https://promptev.ai",
23
23
  "files": [
24
24
  "dist",
25
25
  "readme.md",
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # promptev-client (JavaScript / TypeScript)
2
2
 
3
- A lightweight JS SDK to securely fetch, format, and cache prompts from [Promptev.ai](https://promptev.ai).
3
+ A lightweight JS SDK to fetch compiled prompts from [Promptev.ai](https://promptev.ai).
4
4
 
5
5
  ---
6
6
 
@@ -14,17 +14,11 @@ yarn install @promptev/client
14
14
  pnpm add @promptev/client
15
15
  ```
16
16
 
17
- > Optional for Node.js caching (recommended):
18
- ```bash
19
- npm install node-cache
20
- ```
21
-
22
17
  ---
23
18
 
24
19
  ## What is Promptev?
25
20
 
26
- [Promptev](https://promptev.ai) helps teams manage, version, and collaborate on production-grade prompts with live context, variables, versioning and integrations.
27
-
21
+ [Promptev](https://promptev.ai) helps teams manage, version, and collaborate on AI prompts at scale — with variables, live context packs, histories, cost estimation, and SDK access.
28
22
  ---
29
23
 
30
24
  ## Usage
@@ -91,10 +85,7 @@ console.log(response.choices[0].message.content);
91
85
  ## Features
92
86
 
93
87
  - ✅ Works with or without prompt variables
94
- - 🔁 In-memory caching (Map or NodeCache)
95
- - 🔄 Background refresh (default: 30s, configurable)
96
- - ⚡ Token-safe variable formatting
97
- - 🔐 Secure for frontend & server
88
+ - 🔐 Safe for frontend & server use
98
89
  - 🔌 Works with any LLM provider
99
90
 
100
91
  ---
@@ -105,7 +96,7 @@ console.log(response.choices[0].message.content);
105
96
  new PromptevClient({
106
97
  projectKey: "pv_sk_abc...",
107
98
  baseUrl: "https://api.promptev.ai", // optional
108
- cacheRefreshIntervalMs: 60000 // optional
99
+ headers: { "X-PTV-API-Key": "..." } // optional: extra headers
109
100
  });
110
101
  ```
111
102
 
@@ -121,5 +112,5 @@ By using this package, you agree to the terms in [`LICENSE.txt`](./LICENSE.txt).
121
112
 
122
113
  ## Contact
123
114
 
124
- - 🌐 [https://promptev.ai](https://promptev.ai)
115
+ - 🌐 https://promptev.ai
125
116
  - 📧 support@promptev.ai