@promptev/client 0.0.2 → 0.1.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/dist/cjs/index.cjs +273 -122
- package/dist/esm/index.d.ts +75 -16
- package/dist/esm/index.js +265 -85
- package/package.json +6 -9
- package/readme.md +387 -62
- package/dist/cjs/index.d.ts +0 -30
package/dist/esm/index.js
CHANGED
|
@@ -1,103 +1,283 @@
|
|
|
1
|
-
|
|
1
|
+
// ── Types ───────────────────────────────────────────────
|
|
2
|
+
// ── Errors ──────────────────────────────────────────────
|
|
3
|
+
export class PromptevError extends Error {
|
|
4
|
+
constructor(message, statusCode, responseText) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "PromptevError";
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
this.responseText = responseText;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class ValidationError extends PromptevError {
|
|
12
|
+
constructor(message, statusCode, responseText) {
|
|
13
|
+
super(message, statusCode, responseText);
|
|
14
|
+
this.name = "ValidationError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class AuthenticationError extends PromptevError {
|
|
18
|
+
constructor(message, statusCode, responseText) {
|
|
19
|
+
super(message, statusCode, responseText);
|
|
20
|
+
this.name = "AuthenticationError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class NotFoundError extends PromptevError {
|
|
24
|
+
constructor(message, statusCode, responseText) {
|
|
25
|
+
super(message, statusCode, responseText);
|
|
26
|
+
this.name = "NotFoundError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class RateLimitError extends PromptevError {
|
|
30
|
+
constructor(message, statusCode, responseText) {
|
|
31
|
+
super(message, statusCode, responseText);
|
|
32
|
+
this.name = "RateLimitError";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class ServerError extends PromptevError {
|
|
36
|
+
constructor(message, statusCode, responseText) {
|
|
37
|
+
super(message, statusCode, responseText);
|
|
38
|
+
this.name = "ServerError";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class NetworkError extends PromptevError {
|
|
42
|
+
constructor(message, statusCode, responseText) {
|
|
43
|
+
super(message, statusCode, responseText);
|
|
44
|
+
this.name = "NetworkError";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// ── Client ──────────────────────────────────────────────
|
|
48
|
+
const RETRYABLE_STATUS = new Set([502, 503, 504]);
|
|
49
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
50
|
+
const DEFAULT_BACKOFF = 500; // ms
|
|
2
51
|
export class PromptevClient {
|
|
3
52
|
constructor(config) {
|
|
4
|
-
this.refreshInterval = null;
|
|
5
|
-
this.baseUrl = config.baseUrl || "https://api.promptev.ai";
|
|
6
53
|
this.projectKey = config.projectKey;
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
54
|
+
this.baseUrl = (config.baseUrl ?? "https://api.promptev.ai").replace(/\/+$/, "");
|
|
55
|
+
this.headers = {
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
...(config.headers ?? {}),
|
|
58
|
+
};
|
|
59
|
+
this.timeout = config.timeout ?? 30000;
|
|
60
|
+
this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
61
|
+
}
|
|
62
|
+
// ── Error handling ──────────────────────────────────
|
|
63
|
+
static raiseForStatus(status, text) {
|
|
64
|
+
if (status < 400)
|
|
65
|
+
return;
|
|
66
|
+
let detail;
|
|
67
|
+
try {
|
|
68
|
+
const body = JSON.parse(text);
|
|
69
|
+
detail =
|
|
70
|
+
typeof body.detail === "string" ? body.detail : text || "Unknown error";
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
detail = text || "Unknown error";
|
|
74
|
+
}
|
|
75
|
+
if (status === 400)
|
|
76
|
+
throw new ValidationError(detail, status, text);
|
|
77
|
+
if (status === 401)
|
|
78
|
+
throw new AuthenticationError(detail, status, text);
|
|
79
|
+
if (status === 403)
|
|
80
|
+
throw new AuthenticationError(detail, status, text);
|
|
81
|
+
if (status === 404)
|
|
82
|
+
throw new NotFoundError(detail, status, text);
|
|
83
|
+
if (status === 429)
|
|
84
|
+
throw new RateLimitError(detail, status, text);
|
|
85
|
+
if (status >= 500)
|
|
86
|
+
throw new ServerError(detail, status, text);
|
|
87
|
+
throw new PromptevError(detail, status, text);
|
|
88
|
+
}
|
|
89
|
+
// ── Retry helper ────────────────────────────────────
|
|
90
|
+
async fetchWithRetry(url, init) {
|
|
91
|
+
let lastError = null;
|
|
92
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
93
|
+
try {
|
|
94
|
+
const controller = new AbortController();
|
|
95
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
96
|
+
const resp = await fetch(url, {
|
|
97
|
+
...init,
|
|
98
|
+
signal: controller.signal,
|
|
17
99
|
});
|
|
100
|
+
clearTimeout(timeoutId);
|
|
101
|
+
if (!RETRYABLE_STATUS.has(resp.status) || attempt === this.maxRetries) {
|
|
102
|
+
return resp;
|
|
103
|
+
}
|
|
104
|
+
lastError = new ServerError(`Server error (${resp.status})`, resp.status);
|
|
18
105
|
}
|
|
19
|
-
|
|
20
|
-
|
|
106
|
+
catch (err) {
|
|
107
|
+
if (err.name === "AbortError") {
|
|
108
|
+
lastError = new NetworkError("Request timed out");
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
lastError = new NetworkError(`Network error: ${err.message}`);
|
|
112
|
+
}
|
|
113
|
+
if (attempt === this.maxRetries)
|
|
114
|
+
throw lastError;
|
|
21
115
|
}
|
|
22
|
-
|
|
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) {
|
|
36
|
-
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);
|
|
116
|
+
const delay = DEFAULT_BACKOFF * Math.pow(2, attempt);
|
|
117
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
56
118
|
}
|
|
57
|
-
|
|
119
|
+
throw lastError;
|
|
58
120
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
121
|
+
// ── SSE parser ──────────────────────────────────────
|
|
122
|
+
async *parseSSE(response) {
|
|
123
|
+
const reader = response.body.getReader();
|
|
124
|
+
const decoder = new TextDecoder();
|
|
125
|
+
let buffer = "";
|
|
126
|
+
try {
|
|
127
|
+
while (true) {
|
|
128
|
+
const { done, value } = await reader.read();
|
|
129
|
+
if (done)
|
|
130
|
+
break;
|
|
131
|
+
buffer += decoder.decode(value, { stream: true });
|
|
132
|
+
const lines = buffer.split("\n");
|
|
133
|
+
buffer = lines.pop() || "";
|
|
134
|
+
for (const line of lines) {
|
|
135
|
+
if (!line.startsWith("data: "))
|
|
136
|
+
continue;
|
|
137
|
+
try {
|
|
138
|
+
const data = JSON.parse(line.slice(6));
|
|
139
|
+
const event = {
|
|
140
|
+
type: data.type || "unknown",
|
|
141
|
+
output: data.output || "",
|
|
142
|
+
raw: data,
|
|
143
|
+
};
|
|
144
|
+
yield event;
|
|
145
|
+
if (event.type === "done" || event.type === "error")
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
70
151
|
}
|
|
71
|
-
catch { }
|
|
72
152
|
}
|
|
73
|
-
}
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
reader.releaseLock();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// ── Prompt API ──────────────────────────────────────
|
|
159
|
+
/**
|
|
160
|
+
* Compile and execute a prompt.
|
|
161
|
+
*
|
|
162
|
+
* If the prompt has a model configured, it compiles the template,
|
|
163
|
+
* sends it to the LLM, and returns the AI response. If no model
|
|
164
|
+
* is configured, it returns the compiled template string.
|
|
165
|
+
*/
|
|
166
|
+
async runPrompt(promptKey, query, variables) {
|
|
167
|
+
const url = `${this.baseUrl}/api/sdk/v1/prompt/client/${this.projectKey}/${promptKey}`;
|
|
168
|
+
const merged = { query, ...(variables ?? {}) };
|
|
169
|
+
const resp = await this.fetchWithRetry(url, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
headers: this.headers,
|
|
172
|
+
body: JSON.stringify({ variables: merged }),
|
|
173
|
+
});
|
|
174
|
+
const text = await resp.text();
|
|
175
|
+
PromptevClient.raiseForStatus(resp.status, text);
|
|
176
|
+
const data = JSON.parse(text);
|
|
177
|
+
if (typeof data.prompt !== "string") {
|
|
178
|
+
throw new PromptevError("Unexpected response: missing 'prompt' field");
|
|
179
|
+
}
|
|
180
|
+
return data.prompt;
|
|
74
181
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Compile and execute a prompt with streaming.
|
|
184
|
+
*
|
|
185
|
+
* Use this when the prompt has tools attached or you want real-time
|
|
186
|
+
* output. Returns the same event types as agent streaming.
|
|
187
|
+
*/
|
|
188
|
+
async *streamPrompt(promptKey, query, variables) {
|
|
189
|
+
const url = `${this.baseUrl}/api/sdk/v1/prompt/client/${this.projectKey}/${promptKey}?stream=true`;
|
|
190
|
+
const merged = { query, ...(variables ?? {}) };
|
|
191
|
+
const controller = new AbortController();
|
|
192
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
193
|
+
let resp;
|
|
194
|
+
try {
|
|
195
|
+
resp = await fetch(url, {
|
|
196
|
+
method: "POST",
|
|
197
|
+
headers: this.headers,
|
|
198
|
+
body: JSON.stringify({ variables: merged }),
|
|
199
|
+
signal: controller.signal,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
clearTimeout(timeoutId);
|
|
204
|
+
if (err.name === "AbortError")
|
|
205
|
+
throw new NetworkError("Request timed out");
|
|
206
|
+
throw new NetworkError(`Network error: ${err.message}`);
|
|
207
|
+
}
|
|
208
|
+
clearTimeout(timeoutId);
|
|
209
|
+
if (resp.status >= 400) {
|
|
210
|
+
const text = await resp.text();
|
|
211
|
+
PromptevClient.raiseForStatus(resp.status, text);
|
|
212
|
+
}
|
|
213
|
+
const contentType = resp.headers.get("content-type") || "";
|
|
214
|
+
// Non-streaming fallback: prompt has no model, backend returns JSON
|
|
215
|
+
if (!contentType.includes("text/event-stream")) {
|
|
216
|
+
const data = await resp.json();
|
|
217
|
+
yield {
|
|
218
|
+
type: "done",
|
|
219
|
+
output: data.prompt || "",
|
|
220
|
+
raw: data,
|
|
221
|
+
};
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
yield* this.parseSSE(resp);
|
|
81
225
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
226
|
+
// ── Agent API ───────────────────────────────────────
|
|
227
|
+
/**
|
|
228
|
+
* Start a new agent chat session.
|
|
229
|
+
*/
|
|
230
|
+
async startAgent(chatbotId, options) {
|
|
231
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${chatbotId}/start`;
|
|
232
|
+
const payload = {};
|
|
233
|
+
if (options?.visitor != null)
|
|
234
|
+
payload.visitor = options.visitor;
|
|
235
|
+
if (options?.platform && options.platform !== "sdk")
|
|
236
|
+
payload.platform = options.platform;
|
|
237
|
+
const resp = await this.fetchWithRetry(url, {
|
|
238
|
+
method: "POST",
|
|
239
|
+
headers: this.headers,
|
|
240
|
+
body: JSON.stringify(payload),
|
|
241
|
+
});
|
|
242
|
+
const text = await resp.text();
|
|
243
|
+
PromptevClient.raiseForStatus(resp.status, text);
|
|
244
|
+
const data = JSON.parse(text);
|
|
245
|
+
return {
|
|
246
|
+
sessionToken: data.session_token,
|
|
247
|
+
chatbotId: data.chatbot_id,
|
|
248
|
+
name: data.name,
|
|
249
|
+
memoryEnabled: data.memory_enabled ?? false,
|
|
250
|
+
messages: data.messages ?? [],
|
|
251
|
+
};
|
|
89
252
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
253
|
+
/**
|
|
254
|
+
* Stream an agent response as SSE events.
|
|
255
|
+
*/
|
|
256
|
+
async *streamAgent(chatbotId, options) {
|
|
257
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${chatbotId}/stream?session_token=${encodeURIComponent(options.sessionToken)}`;
|
|
258
|
+
const controller = new AbortController();
|
|
259
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
260
|
+
let resp;
|
|
261
|
+
try {
|
|
262
|
+
resp = await fetch(url, {
|
|
263
|
+
method: "POST",
|
|
264
|
+
headers: this.headers,
|
|
265
|
+
body: JSON.stringify({ query: options.query }),
|
|
266
|
+
signal: controller.signal,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
catch (err) {
|
|
270
|
+
clearTimeout(timeoutId);
|
|
271
|
+
if (err.name === "AbortError")
|
|
272
|
+
throw new NetworkError("Request timed out");
|
|
273
|
+
throw new NetworkError(`Network error: ${err.message}`);
|
|
96
274
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
275
|
+
clearTimeout(timeoutId);
|
|
276
|
+
if (resp.status >= 400) {
|
|
277
|
+
const text = await resp.text();
|
|
278
|
+
PromptevClient.raiseForStatus(resp.status, text);
|
|
100
279
|
}
|
|
280
|
+
yield* this.parseSSE(resp);
|
|
101
281
|
}
|
|
102
282
|
}
|
|
103
283
|
export default PromptevClient;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@promptev/client",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "JavaScript/TypeScript SDK for Promptev — run AI prompts and agents programmatically",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.cjs",
|
|
7
7
|
"module": "./dist/esm/index.js",
|
|
@@ -32,9 +32,9 @@
|
|
|
32
32
|
"client",
|
|
33
33
|
"sdk",
|
|
34
34
|
"ai",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
35
|
+
"agents",
|
|
36
|
+
"agent-sdk",
|
|
37
|
+
"context-engineering",
|
|
38
38
|
"typescript",
|
|
39
39
|
"javascript"
|
|
40
40
|
],
|
|
@@ -44,10 +44,7 @@
|
|
|
44
44
|
"build:cjs": "tsc -p tsconfig.cjs.json && node scripts/rename-cjs.cjs",
|
|
45
45
|
"build": "npm run build:esm && npm run build:cjs"
|
|
46
46
|
},
|
|
47
|
-
"dependencies": {
|
|
48
|
-
"axios": "^1.10.0",
|
|
49
|
-
"node-cache": "^5.1.2"
|
|
50
|
-
},
|
|
47
|
+
"dependencies": {},
|
|
51
48
|
"devDependencies": {
|
|
52
49
|
"@types/node": "^20.10.0",
|
|
53
50
|
"ts-node": "^10.9.2",
|