@promptev/client 0.0.2 → 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.
- package/dist/cjs/index.cjs +41 -125
- package/dist/esm/index.d.ts +15 -13
- package/dist/esm/index.js +41 -92
- package/package.json +1 -1
- package/readme.md +5 -14
package/dist/cjs/index.cjs
CHANGED
|
@@ -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.
|
|
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.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
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
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
1
|
export interface PromptevClientConfig {
|
|
2
2
|
baseUrl?: string;
|
|
3
3
|
projectKey: string;
|
|
4
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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.
|
|
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.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# promptev-client (JavaScript / TypeScript)
|
|
2
2
|
|
|
3
|
-
A lightweight JS SDK to
|
|
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
|
|
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
|
-
-
|
|
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
|
-
|
|
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
|
-
- 🌐
|
|
115
|
+
- 🌐 https://promptev.ai
|
|
125
116
|
- 📧 support@promptev.ai
|