@promptev/client 0.1.0 → 0.2.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 +46 -63
- package/dist/esm/index.d.ts +36 -15
- package/dist/esm/index.js +46 -63
- package/package.json +6 -7
- package/readme.md +117 -104
package/dist/cjs/index.cjs
CHANGED
|
@@ -165,80 +165,61 @@ class PromptevClient {
|
|
|
165
165
|
reader.releaseLock();
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
|
-
// ──
|
|
168
|
+
// ── Agent API ───────────────────────────────────────
|
|
169
169
|
/**
|
|
170
|
-
*
|
|
170
|
+
* Trigger an agent in a single blocking call — the primary way to use the SDK.
|
|
171
|
+
*
|
|
172
|
+
* Creates (or resumes) a session AND runs the agent, returning the final
|
|
173
|
+
* response as JSON. Any tools the agent has attached run server-side.
|
|
174
|
+
*
|
|
175
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
176
|
+
* @param message - The user message to send to the agent.
|
|
177
|
+
* @param opts.sessionId - Pass the `sessionId` from a previous call to keep
|
|
178
|
+
* conversation memory. Omit it for a fresh, stateless run.
|
|
179
|
+
* @param opts.variables - Object filling the agent's `{{...}}` prompt variables.
|
|
180
|
+
* @returns `{ response, sessionId }` — pass `sessionId` back on the next call
|
|
181
|
+
* to continue the same conversation.
|
|
171
182
|
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
183
|
+
* @example
|
|
184
|
+
* const { response, sessionId } = await client.triggerAgent(
|
|
185
|
+
* "your-agent-id",
|
|
186
|
+
* "What is the refund policy?",
|
|
187
|
+
* { variables: { company: "Acme Corp" } }
|
|
188
|
+
* );
|
|
189
|
+
* // Follow-up keeps memory:
|
|
190
|
+
* await client.triggerAgent("your-agent-id", "And for digital goods?", { sessionId });
|
|
175
191
|
*/
|
|
176
|
-
async
|
|
177
|
-
const url = `${this.baseUrl}/api/sdk/v1/
|
|
178
|
-
const
|
|
192
|
+
async triggerAgent(agentId, message, opts) {
|
|
193
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${agentId}/trigger`;
|
|
194
|
+
const payload = { message };
|
|
195
|
+
if (opts?.sessionId != null)
|
|
196
|
+
payload.session_id = opts.sessionId;
|
|
197
|
+
if (opts?.variables != null)
|
|
198
|
+
payload.variables = opts.variables;
|
|
199
|
+
// Optional caller/end-user id stored on the chat session; defaults server-side
|
|
200
|
+
// to a generated webhook_<id> when omitted.
|
|
201
|
+
if (opts?.visitor != null)
|
|
202
|
+
payload.visitor = opts.visitor;
|
|
179
203
|
const resp = await this.fetchWithRetry(url, {
|
|
180
204
|
method: "POST",
|
|
181
205
|
headers: this.headers,
|
|
182
|
-
body: JSON.stringify(
|
|
206
|
+
body: JSON.stringify(payload),
|
|
183
207
|
});
|
|
184
208
|
const text = await resp.text();
|
|
185
209
|
PromptevClient.raiseForStatus(resp.status, text);
|
|
186
210
|
const data = JSON.parse(text);
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
211
|
+
return {
|
|
212
|
+
response: typeof data.response === "string" ? data.response : "",
|
|
213
|
+
sessionId: data.session_id,
|
|
214
|
+
};
|
|
191
215
|
}
|
|
192
216
|
/**
|
|
193
|
-
*
|
|
217
|
+
* Start a new agent chat session (streaming flow).
|
|
194
218
|
*
|
|
195
|
-
*
|
|
196
|
-
* output. Returns the same event types as agent streaming.
|
|
197
|
-
*/
|
|
198
|
-
async *streamPrompt(promptKey, query, variables) {
|
|
199
|
-
const url = `${this.baseUrl}/api/sdk/v1/prompt/client/${this.projectKey}/${promptKey}?stream=true`;
|
|
200
|
-
const merged = { query, ...(variables ?? {}) };
|
|
201
|
-
const controller = new AbortController();
|
|
202
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
203
|
-
let resp;
|
|
204
|
-
try {
|
|
205
|
-
resp = await fetch(url, {
|
|
206
|
-
method: "POST",
|
|
207
|
-
headers: this.headers,
|
|
208
|
-
body: JSON.stringify({ variables: merged }),
|
|
209
|
-
signal: controller.signal,
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
catch (err) {
|
|
213
|
-
clearTimeout(timeoutId);
|
|
214
|
-
if (err.name === "AbortError")
|
|
215
|
-
throw new NetworkError("Request timed out");
|
|
216
|
-
throw new NetworkError(`Network error: ${err.message}`);
|
|
217
|
-
}
|
|
218
|
-
clearTimeout(timeoutId);
|
|
219
|
-
if (resp.status >= 400) {
|
|
220
|
-
const text = await resp.text();
|
|
221
|
-
PromptevClient.raiseForStatus(resp.status, text);
|
|
222
|
-
}
|
|
223
|
-
const contentType = resp.headers.get("content-type") || "";
|
|
224
|
-
// Non-streaming fallback: prompt has no model, backend returns JSON
|
|
225
|
-
if (!contentType.includes("text/event-stream")) {
|
|
226
|
-
const data = await resp.json();
|
|
227
|
-
yield {
|
|
228
|
-
type: "done",
|
|
229
|
-
output: data.prompt || "",
|
|
230
|
-
raw: data,
|
|
231
|
-
};
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
yield* this.parseSSE(resp);
|
|
235
|
-
}
|
|
236
|
-
// ── Agent API ───────────────────────────────────────
|
|
237
|
-
/**
|
|
238
|
-
* Start a new agent chat session.
|
|
219
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
239
220
|
*/
|
|
240
|
-
async startAgent(
|
|
241
|
-
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${
|
|
221
|
+
async startAgent(agentId, options) {
|
|
222
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${agentId}/start`;
|
|
242
223
|
const payload = {};
|
|
243
224
|
if (options?.visitor != null)
|
|
244
225
|
payload.visitor = options.visitor;
|
|
@@ -254,7 +235,7 @@ class PromptevClient {
|
|
|
254
235
|
const data = JSON.parse(text);
|
|
255
236
|
return {
|
|
256
237
|
sessionToken: data.session_token,
|
|
257
|
-
|
|
238
|
+
agentId: data.chatbot_id,
|
|
258
239
|
name: data.name,
|
|
259
240
|
memoryEnabled: data.memory_enabled ?? false,
|
|
260
241
|
messages: data.messages ?? [],
|
|
@@ -262,9 +243,11 @@ class PromptevClient {
|
|
|
262
243
|
}
|
|
263
244
|
/**
|
|
264
245
|
* Stream an agent response as SSE events.
|
|
246
|
+
*
|
|
247
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
265
248
|
*/
|
|
266
|
-
async *streamAgent(
|
|
267
|
-
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${
|
|
249
|
+
async *streamAgent(agentId, options) {
|
|
250
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${agentId}/stream?session_token=${encodeURIComponent(options.sessionToken)}`;
|
|
268
251
|
const controller = new AbortController();
|
|
269
252
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
270
253
|
let resp;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -7,11 +7,15 @@ export interface PromptevClientConfig {
|
|
|
7
7
|
}
|
|
8
8
|
export interface AgentSession {
|
|
9
9
|
sessionToken: string;
|
|
10
|
-
|
|
10
|
+
agentId: string;
|
|
11
11
|
name: string;
|
|
12
12
|
memoryEnabled: boolean;
|
|
13
13
|
messages: Record<string, any>[];
|
|
14
14
|
}
|
|
15
|
+
export interface TriggerResult {
|
|
16
|
+
response: string;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
}
|
|
15
19
|
export interface AgentEvent {
|
|
16
20
|
type: string;
|
|
17
21
|
output: string;
|
|
@@ -51,31 +55,48 @@ export declare class PromptevClient {
|
|
|
51
55
|
private fetchWithRetry;
|
|
52
56
|
private parseSSE;
|
|
53
57
|
/**
|
|
54
|
-
*
|
|
58
|
+
* Trigger an agent in a single blocking call — the primary way to use the SDK.
|
|
55
59
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
*
|
|
60
|
+
* Creates (or resumes) a session AND runs the agent, returning the final
|
|
61
|
+
* response as JSON. Any tools the agent has attached run server-side.
|
|
62
|
+
*
|
|
63
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
64
|
+
* @param message - The user message to send to the agent.
|
|
65
|
+
* @param opts.sessionId - Pass the `sessionId` from a previous call to keep
|
|
66
|
+
* conversation memory. Omit it for a fresh, stateless run.
|
|
67
|
+
* @param opts.variables - Object filling the agent's `{{...}}` prompt variables.
|
|
68
|
+
* @returns `{ response, sessionId }` — pass `sessionId` back on the next call
|
|
69
|
+
* to continue the same conversation.
|
|
63
70
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
71
|
+
* @example
|
|
72
|
+
* const { response, sessionId } = await client.triggerAgent(
|
|
73
|
+
* "your-agent-id",
|
|
74
|
+
* "What is the refund policy?",
|
|
75
|
+
* { variables: { company: "Acme Corp" } }
|
|
76
|
+
* );
|
|
77
|
+
* // Follow-up keeps memory:
|
|
78
|
+
* await client.triggerAgent("your-agent-id", "And for digital goods?", { sessionId });
|
|
66
79
|
*/
|
|
67
|
-
|
|
80
|
+
triggerAgent(agentId: string, message: string, opts?: {
|
|
81
|
+
sessionId?: string;
|
|
82
|
+
variables?: Record<string, any>;
|
|
83
|
+
visitor?: string;
|
|
84
|
+
}): Promise<TriggerResult>;
|
|
68
85
|
/**
|
|
69
|
-
* Start a new agent chat session.
|
|
86
|
+
* Start a new agent chat session (streaming flow).
|
|
87
|
+
*
|
|
88
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
70
89
|
*/
|
|
71
|
-
startAgent(
|
|
90
|
+
startAgent(agentId: string, options?: {
|
|
72
91
|
visitor?: string;
|
|
73
92
|
platform?: string;
|
|
74
93
|
}): Promise<AgentSession>;
|
|
75
94
|
/**
|
|
76
95
|
* Stream an agent response as SSE events.
|
|
96
|
+
*
|
|
97
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
77
98
|
*/
|
|
78
|
-
streamAgent(
|
|
99
|
+
streamAgent(agentId: string, options: {
|
|
79
100
|
sessionToken: string;
|
|
80
101
|
query: string;
|
|
81
102
|
}): AsyncGenerator<AgentEvent>;
|
package/dist/esm/index.js
CHANGED
|
@@ -155,80 +155,61 @@ export class PromptevClient {
|
|
|
155
155
|
reader.releaseLock();
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
|
-
// ──
|
|
158
|
+
// ── Agent API ───────────────────────────────────────
|
|
159
159
|
/**
|
|
160
|
-
*
|
|
160
|
+
* Trigger an agent in a single blocking call — the primary way to use the SDK.
|
|
161
|
+
*
|
|
162
|
+
* Creates (or resumes) a session AND runs the agent, returning the final
|
|
163
|
+
* response as JSON. Any tools the agent has attached run server-side.
|
|
164
|
+
*
|
|
165
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
166
|
+
* @param message - The user message to send to the agent.
|
|
167
|
+
* @param opts.sessionId - Pass the `sessionId` from a previous call to keep
|
|
168
|
+
* conversation memory. Omit it for a fresh, stateless run.
|
|
169
|
+
* @param opts.variables - Object filling the agent's `{{...}}` prompt variables.
|
|
170
|
+
* @returns `{ response, sessionId }` — pass `sessionId` back on the next call
|
|
171
|
+
* to continue the same conversation.
|
|
161
172
|
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
173
|
+
* @example
|
|
174
|
+
* const { response, sessionId } = await client.triggerAgent(
|
|
175
|
+
* "your-agent-id",
|
|
176
|
+
* "What is the refund policy?",
|
|
177
|
+
* { variables: { company: "Acme Corp" } }
|
|
178
|
+
* );
|
|
179
|
+
* // Follow-up keeps memory:
|
|
180
|
+
* await client.triggerAgent("your-agent-id", "And for digital goods?", { sessionId });
|
|
165
181
|
*/
|
|
166
|
-
async
|
|
167
|
-
const url = `${this.baseUrl}/api/sdk/v1/
|
|
168
|
-
const
|
|
182
|
+
async triggerAgent(agentId, message, opts) {
|
|
183
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${agentId}/trigger`;
|
|
184
|
+
const payload = { message };
|
|
185
|
+
if (opts?.sessionId != null)
|
|
186
|
+
payload.session_id = opts.sessionId;
|
|
187
|
+
if (opts?.variables != null)
|
|
188
|
+
payload.variables = opts.variables;
|
|
189
|
+
// Optional caller/end-user id stored on the chat session; defaults server-side
|
|
190
|
+
// to a generated webhook_<id> when omitted.
|
|
191
|
+
if (opts?.visitor != null)
|
|
192
|
+
payload.visitor = opts.visitor;
|
|
169
193
|
const resp = await this.fetchWithRetry(url, {
|
|
170
194
|
method: "POST",
|
|
171
195
|
headers: this.headers,
|
|
172
|
-
body: JSON.stringify(
|
|
196
|
+
body: JSON.stringify(payload),
|
|
173
197
|
});
|
|
174
198
|
const text = await resp.text();
|
|
175
199
|
PromptevClient.raiseForStatus(resp.status, text);
|
|
176
200
|
const data = JSON.parse(text);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
201
|
+
return {
|
|
202
|
+
response: typeof data.response === "string" ? data.response : "",
|
|
203
|
+
sessionId: data.session_id,
|
|
204
|
+
};
|
|
181
205
|
}
|
|
182
206
|
/**
|
|
183
|
-
*
|
|
207
|
+
* Start a new agent chat session (streaming flow).
|
|
184
208
|
*
|
|
185
|
-
*
|
|
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);
|
|
225
|
-
}
|
|
226
|
-
// ── Agent API ───────────────────────────────────────
|
|
227
|
-
/**
|
|
228
|
-
* Start a new agent chat session.
|
|
209
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
229
210
|
*/
|
|
230
|
-
async startAgent(
|
|
231
|
-
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${
|
|
211
|
+
async startAgent(agentId, options) {
|
|
212
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${agentId}/start`;
|
|
232
213
|
const payload = {};
|
|
233
214
|
if (options?.visitor != null)
|
|
234
215
|
payload.visitor = options.visitor;
|
|
@@ -244,7 +225,7 @@ export class PromptevClient {
|
|
|
244
225
|
const data = JSON.parse(text);
|
|
245
226
|
return {
|
|
246
227
|
sessionToken: data.session_token,
|
|
247
|
-
|
|
228
|
+
agentId: data.chatbot_id,
|
|
248
229
|
name: data.name,
|
|
249
230
|
memoryEnabled: data.memory_enabled ?? false,
|
|
250
231
|
messages: data.messages ?? [],
|
|
@@ -252,9 +233,11 @@ export class PromptevClient {
|
|
|
252
233
|
}
|
|
253
234
|
/**
|
|
254
235
|
* Stream an agent response as SSE events.
|
|
236
|
+
*
|
|
237
|
+
* @param agentId - Your agent's id (from the Deploy tab).
|
|
255
238
|
*/
|
|
256
|
-
async *streamAgent(
|
|
257
|
-
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${
|
|
239
|
+
async *streamAgent(agentId, options) {
|
|
240
|
+
const url = `${this.baseUrl}/api/sdk/v1/agent/${this.projectKey}/${agentId}/stream?session_token=${encodeURIComponent(options.sessionToken)}`;
|
|
258
241
|
const controller = new AbortController();
|
|
259
242
|
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
260
243
|
let resp;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@promptev/client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "JavaScript/TypeScript SDK for Promptev —
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "JavaScript/TypeScript SDK for Promptev — trigger and stream your AI agents programmatically",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.cjs",
|
|
7
7
|
"module": "./dist/esm/index.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"types": "./dist/esm/index.d.ts"
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
|
-
"license": "
|
|
16
|
+
"license": "SEE LICENSE IN LICENSE.txt",
|
|
17
17
|
"author": "Promptev Inc. <support@promptev.ai>",
|
|
18
18
|
"private": false,
|
|
19
19
|
"publishConfig": {
|
|
@@ -27,13 +27,12 @@
|
|
|
27
27
|
],
|
|
28
28
|
"keywords": [
|
|
29
29
|
"promptev",
|
|
30
|
-
"
|
|
30
|
+
"agents",
|
|
31
|
+
"agent-sdk",
|
|
32
|
+
"ai",
|
|
31
33
|
"api",
|
|
32
34
|
"client",
|
|
33
35
|
"sdk",
|
|
34
|
-
"ai",
|
|
35
|
-
"agents",
|
|
36
|
-
"agent-sdk",
|
|
37
36
|
"context-engineering",
|
|
38
37
|
"typescript",
|
|
39
38
|
"javascript"
|
package/readme.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Promptev JavaScript SDK
|
|
2
2
|
|
|
3
|
-
The official JavaScript/TypeScript SDK for [Promptev.ai](https://promptev.ai) —
|
|
3
|
+
The official JavaScript/TypeScript SDK for [Promptev.ai](https://promptev.ai) — trigger and stream your AI **agents** programmatically using your project API key.
|
|
4
|
+
|
|
5
|
+
> In Promptev, an **agent** is your deployed AI assistant: instructions + model + tools (Jira, Slack, GitHub, …) + context packs, all configured in the dashboard. This SDK runs those agents. Tools execute server-side, memory is built in.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -41,123 +43,114 @@ const { PromptevClient } = require("@promptev/client");
|
|
|
41
43
|
|
|
42
44
|
## Quick Start
|
|
43
45
|
|
|
46
|
+
The fastest way to use an agent is `triggerAgent` — one blocking call that runs the agent and returns its final answer. Pass your agent's id (from the **Deploy** tab) and a message:
|
|
47
|
+
|
|
44
48
|
```ts
|
|
45
49
|
import { PromptevClient } from "@promptev/client";
|
|
46
50
|
|
|
47
51
|
const client = new PromptevClient({ projectKey: "pv_sk_your_key_here" });
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
const { response, sessionId } = await client.triggerAgent(
|
|
54
|
+
"your-agent-id",
|
|
51
55
|
"What is the refund policy?",
|
|
52
|
-
{ company: "Acme Corp" }
|
|
56
|
+
{ variables: { company: "Acme Corp" } }
|
|
53
57
|
);
|
|
54
|
-
console.log(
|
|
58
|
+
console.log(response);
|
|
55
59
|
|
|
56
|
-
//
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
for
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (event.type === "done") console.log(event.output);
|
|
64
|
-
}
|
|
60
|
+
// Keep conversation memory by passing the same sessionId back:
|
|
61
|
+
const followUp = await client.triggerAgent(
|
|
62
|
+
"your-agent-id",
|
|
63
|
+
"And for digital goods?",
|
|
64
|
+
{ sessionId }
|
|
65
|
+
);
|
|
66
|
+
console.log(followUp.response);
|
|
65
67
|
```
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
That's it. The agent's tools, context packs, and model all run server-side — you just get the answer.
|
|
68
70
|
|
|
69
|
-
|
|
71
|
+
## `triggerAgent` — run an agent in one call
|
|
70
72
|
|
|
71
|
-
|
|
73
|
+
`triggerAgent(agentId, message, opts?)` creates **or** resumes a session **and** runs the agent, returning the final response as JSON.
|
|
72
74
|
|
|
73
75
|
```ts
|
|
74
|
-
const result = await client.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
);
|
|
76
|
+
const result = await client.triggerAgent("your-agent-id", "Summarize our Q4 sales report");
|
|
77
|
+
console.log(result.response); // the agent's answer
|
|
78
|
+
console.log(result.sessionId); // pass this back to continue the conversation
|
|
78
79
|
```
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
| Parameter | Type | Description |
|
|
82
|
+
|---|---|---|
|
|
83
|
+
| `agentId` | `string` | Your agent's id (from the Deploy tab) |
|
|
84
|
+
| `message` | `string` | The user message to send to the agent |
|
|
85
|
+
| `opts.sessionId` | `string?` | Pass the `sessionId` from a previous call to keep memory. Omit for a fresh, stateless run. |
|
|
86
|
+
| `opts.variables` | `object?` | Fills the agent's `{{...}}` prompt variables |
|
|
87
|
+
| `opts.visitor` | `string?` | Attribute the run to an end-user id (stored on the chat session for analytics/audit). Defaults to a generated `webhook_<id>`. Set it on the first call of a conversation; a resumed session keeps its original visitor. |
|
|
88
|
+
|
|
89
|
+
Returns `{ response: string; sessionId: string }`.
|
|
81
90
|
|
|
82
91
|
```ts
|
|
83
|
-
|
|
84
|
-
|
|
92
|
+
// Attribute the run to a specific end-user:
|
|
93
|
+
const { response } = await client.triggerAgent(
|
|
94
|
+
"your-agent-id",
|
|
95
|
+
"How do I reset my password?",
|
|
96
|
+
{ visitor: "user_12345" }
|
|
85
97
|
);
|
|
86
98
|
```
|
|
87
99
|
|
|
88
|
-
###
|
|
100
|
+
### Stateful (with memory)
|
|
89
101
|
|
|
90
|
-
|
|
102
|
+
Pass the returned `sessionId` back on each follow-up to keep context:
|
|
91
103
|
|
|
92
104
|
```ts
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
);
|
|
97
|
-
console.log(
|
|
105
|
+
let { response, sessionId } = await client.triggerAgent("your-agent-id", "Summarize Q4 sales");
|
|
106
|
+
console.log(response);
|
|
107
|
+
|
|
108
|
+
({ response } = await client.triggerAgent("your-agent-id", "Compare that with Q3", { sessionId }));
|
|
109
|
+
console.log(response);
|
|
98
110
|
```
|
|
99
111
|
|
|
100
|
-
###
|
|
112
|
+
### Stateless (no memory)
|
|
101
113
|
|
|
102
|
-
|
|
114
|
+
Omit `sessionId` for an independent, one-shot run each time:
|
|
103
115
|
|
|
104
116
|
```ts
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const systemPrompt = await client.runPrompt("support-agent",
|
|
111
|
-
"How do I reset my password?",
|
|
112
|
-
{ company: "Acme Corp", tone: "professional" }
|
|
117
|
+
const { response } = await client.triggerAgent(
|
|
118
|
+
"your-agent-id",
|
|
119
|
+
"What is the refund policy?",
|
|
120
|
+
{ variables: { company: "Acme Corp" } }
|
|
113
121
|
);
|
|
114
|
-
|
|
115
|
-
const response = await openai.chat.completions.create({
|
|
116
|
-
model: "gpt-4",
|
|
117
|
-
messages: [
|
|
118
|
-
{ role: "system", content: systemPrompt },
|
|
119
|
-
{ role: "user", content: "How do I reset my password?" },
|
|
120
|
-
],
|
|
121
|
-
});
|
|
122
|
-
console.log(response.choices[0].message.content);
|
|
123
122
|
```
|
|
124
123
|
|
|
125
|
-
###
|
|
124
|
+
### With prompt variables
|
|
126
125
|
|
|
127
|
-
|
|
126
|
+
If your agent's instructions contain `{{...}}` placeholders, fill them via `variables`:
|
|
128
127
|
|
|
129
128
|
```ts
|
|
130
|
-
|
|
131
|
-
"
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
else if (event.type === "processing") console.log(`Running: ${event.output}`);
|
|
136
|
-
else if (event.type === "done") console.log(event.output);
|
|
137
|
-
}
|
|
129
|
+
const { response } = await client.triggerAgent(
|
|
130
|
+
"your-agent-id",
|
|
131
|
+
"How do I reset my password?",
|
|
132
|
+
{ variables: { company: "Acme Corp", tone: "professional" } }
|
|
133
|
+
);
|
|
138
134
|
```
|
|
139
135
|
|
|
140
|
-
|
|
141
|
-
> - `runPrompt()` — simple prompt execution, no tools, returns a string
|
|
142
|
-
> - `streamPrompt()` — prompts with tools, RAG-heavy queries, or when you want real-time output
|
|
143
|
-
|
|
144
|
-
## Agents
|
|
136
|
+
## Streaming an agent
|
|
145
137
|
|
|
146
|
-
|
|
138
|
+
When you want real-time output — agent reasoning, tool-execution status, and the final answer as they happen — use the streaming flow: `startAgent` to open a session, then `streamAgent` to receive [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).
|
|
147
139
|
|
|
148
140
|
### Start a session
|
|
149
141
|
|
|
150
142
|
```ts
|
|
151
143
|
const session = await client.startAgent("your-agent-id", { visitor: "John" });
|
|
152
144
|
|
|
153
|
-
console.log(session.sessionToken); //
|
|
154
|
-
console.log(session.
|
|
155
|
-
console.log(session.
|
|
145
|
+
console.log(session.sessionToken); // use this for all subsequent stream calls
|
|
146
|
+
console.log(session.agentId); // the agent id, echoed back
|
|
147
|
+
console.log(session.name); // agent display name
|
|
148
|
+
console.log(session.memoryEnabled); // whether the agent retains conversation context
|
|
156
149
|
```
|
|
157
150
|
|
|
158
151
|
### Stream a response
|
|
159
152
|
|
|
160
|
-
|
|
153
|
+
Each event has a `type` and `output`:
|
|
161
154
|
|
|
162
155
|
| Event Type | Description |
|
|
163
156
|
|---|---|
|
|
@@ -168,7 +161,7 @@ The agent responds via Server-Sent Events (SSE). Each event has a `type` and `ou
|
|
|
168
161
|
| `error` | Something went wrong |
|
|
169
162
|
|
|
170
163
|
```ts
|
|
171
|
-
for await (const event of client.streamAgent(session.
|
|
164
|
+
for await (const event of client.streamAgent(session.agentId, {
|
|
172
165
|
sessionToken: session.sessionToken,
|
|
173
166
|
query: "What are the open P1 bugs in our backlog?",
|
|
174
167
|
})) {
|
|
@@ -187,7 +180,7 @@ The session token maintains conversation context across messages:
|
|
|
187
180
|
const session = await client.startAgent("your-agent-id", { visitor: "Sarah" });
|
|
188
181
|
|
|
189
182
|
// First message
|
|
190
|
-
for await (const event of client.streamAgent(session.
|
|
183
|
+
for await (const event of client.streamAgent(session.agentId, {
|
|
191
184
|
sessionToken: session.sessionToken,
|
|
192
185
|
query: "Summarize our Q4 sales report",
|
|
193
186
|
})) {
|
|
@@ -195,7 +188,7 @@ for await (const event of client.streamAgent(session.chatbotId, {
|
|
|
195
188
|
}
|
|
196
189
|
|
|
197
190
|
// Follow-up — agent remembers the previous context
|
|
198
|
-
for await (const event of client.streamAgent(session.
|
|
191
|
+
for await (const event of client.streamAgent(session.agentId, {
|
|
199
192
|
sessionToken: session.sessionToken,
|
|
200
193
|
query: "Compare that with Q3",
|
|
201
194
|
})) {
|
|
@@ -207,7 +200,7 @@ for await (const event of client.streamAgent(session.chatbotId, {
|
|
|
207
200
|
|
|
208
201
|
```ts
|
|
209
202
|
async function askAgent(client, session, query) {
|
|
210
|
-
for await (const event of client.streamAgent(session.
|
|
203
|
+
for await (const event of client.streamAgent(session.agentId, {
|
|
211
204
|
sessionToken: session.sessionToken,
|
|
212
205
|
query,
|
|
213
206
|
})) {
|
|
@@ -219,6 +212,10 @@ async function askAgent(client, session, query) {
|
|
|
219
212
|
const answer = await askAgent(client, session, "What's our monthly churn rate?");
|
|
220
213
|
```
|
|
221
214
|
|
|
215
|
+
> **When to use which:**
|
|
216
|
+
> - `triggerAgent()` — the default. One call, blocking, returns the final answer. Best for APIs, jobs, and server-to-server use.
|
|
217
|
+
> - `startAgent()` + `streamAgent()` — when you want to render reasoning and tool progress live (e.g., a chat UI).
|
|
218
|
+
|
|
222
219
|
## Error Handling
|
|
223
220
|
|
|
224
221
|
The SDK raises typed errors for each failure scenario:
|
|
@@ -237,13 +234,14 @@ import {
|
|
|
237
234
|
const client = new PromptevClient({ projectKey: "pv_sk_..." });
|
|
238
235
|
|
|
239
236
|
try {
|
|
240
|
-
const
|
|
237
|
+
const { response } = await client.triggerAgent("your-agent-id", "Hello");
|
|
238
|
+
console.log(response);
|
|
241
239
|
} catch (err) {
|
|
242
240
|
if (err instanceof ValidationError) {
|
|
243
241
|
// 400 — missing variables, bad input
|
|
244
242
|
console.log(`Invalid request: ${err.message}`);
|
|
245
243
|
} else if (err instanceof NotFoundError) {
|
|
246
|
-
// 404 —
|
|
244
|
+
// 404 — agent or project not found
|
|
247
245
|
console.log(`Not found: ${err.message}`);
|
|
248
246
|
} else if (err instanceof AuthenticationError) {
|
|
249
247
|
// 401/403 — invalid API key or agent not active
|
|
@@ -265,6 +263,16 @@ All errors extend `PromptevError` and include:
|
|
|
265
263
|
- `err.statusCode` — HTTP status code (if applicable)
|
|
266
264
|
- `err.responseText` — Raw response body (for debugging)
|
|
267
265
|
|
|
266
|
+
| Error | HTTP Status | When |
|
|
267
|
+
|---|---|---|
|
|
268
|
+
| `ValidationError` | 400 | Missing required variables, bad input |
|
|
269
|
+
| `AuthenticationError` | 401, 403 | Invalid API key, agent not active |
|
|
270
|
+
| `NotFoundError` | 404 | Project or agent not found |
|
|
271
|
+
| `RateLimitError` | 429 | API usage quota exceeded |
|
|
272
|
+
| `ServerError` | 5xx | Server error (after retries exhausted) |
|
|
273
|
+
| `NetworkError` | — | Connection failed, timeout, DNS error |
|
|
274
|
+
| `PromptevError` | any | Base class for all above errors |
|
|
275
|
+
|
|
268
276
|
## Configuration
|
|
269
277
|
|
|
270
278
|
```ts
|
|
@@ -279,7 +287,7 @@ const client = new PromptevClient({
|
|
|
279
287
|
|
|
280
288
|
| Parameter | Default | Description |
|
|
281
289
|
|---|---|---|
|
|
282
|
-
| `projectKey` | *required* | Your Promptev project API key |
|
|
290
|
+
| `projectKey` | *required* | Your Promptev project API key (`pv_sk_...`) |
|
|
283
291
|
| `baseUrl` | `https://api.promptev.ai` | API base URL |
|
|
284
292
|
| `timeout` | `30000` | Request timeout in milliseconds |
|
|
285
293
|
| `maxRetries` | `2` | Automatic retries for transient server errors (502, 503, 504) |
|
|
@@ -291,19 +299,25 @@ const client = new PromptevClient({
|
|
|
291
299
|
|
|
292
300
|
| Method | Description | Returns |
|
|
293
301
|
|---|---|---|
|
|
294
|
-
| `
|
|
295
|
-
| `
|
|
296
|
-
| `
|
|
297
|
-
|
|
302
|
+
| `triggerAgent(agentId, message, opts?)` | Run an agent in one blocking call | `Promise<TriggerResult>` |
|
|
303
|
+
| `startAgent(agentId, options?)` | Start a streaming agent session | `Promise<AgentSession>` |
|
|
304
|
+
| `streamAgent(agentId, options)` | Stream an agent response (SSE) | `AsyncGenerator<AgentEvent>` |
|
|
305
|
+
|
|
306
|
+
### `TriggerResult`
|
|
307
|
+
|
|
308
|
+
| Field | Type | Description |
|
|
309
|
+
|---|---|---|
|
|
310
|
+
| `response` | `string` | The agent's final answer |
|
|
311
|
+
| `sessionId` | `string` | Pass back as `opts.sessionId` to continue the conversation |
|
|
298
312
|
|
|
299
313
|
### `AgentSession`
|
|
300
314
|
|
|
301
315
|
| Field | Type | Description |
|
|
302
316
|
|---|---|---|
|
|
303
317
|
| `sessionToken` | `string` | Token for subsequent stream calls |
|
|
304
|
-
| `
|
|
318
|
+
| `agentId` | `string` | Agent identifier |
|
|
305
319
|
| `name` | `string` | Agent display name |
|
|
306
|
-
| `memoryEnabled` | `boolean` | Whether agent retains conversation context |
|
|
320
|
+
| `memoryEnabled` | `boolean` | Whether the agent retains conversation context |
|
|
307
321
|
| `messages` | `array` | Previous messages (populated when resuming a session) |
|
|
308
322
|
|
|
309
323
|
### `AgentEvent`
|
|
@@ -314,18 +328,6 @@ const client = new PromptevClient({
|
|
|
314
328
|
| `output` | `string` | Event content text |
|
|
315
329
|
| `raw` | `object` | Full parsed SSE event data |
|
|
316
330
|
|
|
317
|
-
### Exceptions
|
|
318
|
-
|
|
319
|
-
| Error | HTTP Status | When |
|
|
320
|
-
|---|---|---|
|
|
321
|
-
| `ValidationError` | 400 | Missing required variables, bad input |
|
|
322
|
-
| `AuthenticationError` | 401, 403 | Invalid API key, agent not active |
|
|
323
|
-
| `NotFoundError` | 404 | Project, prompt, or agent not found |
|
|
324
|
-
| `RateLimitError` | 429 | API usage quota exceeded |
|
|
325
|
-
| `ServerError` | 5xx | Server error (after retries exhausted) |
|
|
326
|
-
| `NetworkError` | — | Connection failed, timeout, DNS error |
|
|
327
|
-
| `PromptevError` | any | Base class for all above errors |
|
|
328
|
-
|
|
329
331
|
## Framework Examples
|
|
330
332
|
|
|
331
333
|
### React
|
|
@@ -336,15 +338,15 @@ import { PromptevClient } from "@promptev/client";
|
|
|
336
338
|
|
|
337
339
|
const client = new PromptevClient({ projectKey: "pv_sk_..." });
|
|
338
340
|
|
|
339
|
-
export function
|
|
341
|
+
export function AskAgent() {
|
|
340
342
|
const [answer, setAnswer] = useState("");
|
|
341
343
|
const [loading, setLoading] = useState(false);
|
|
342
344
|
|
|
343
345
|
async function handleAsk() {
|
|
344
346
|
setLoading(true);
|
|
345
347
|
try {
|
|
346
|
-
const
|
|
347
|
-
setAnswer(
|
|
348
|
+
const { response } = await client.triggerAgent("your-agent-id", "What is your refund policy?");
|
|
349
|
+
setAnswer(response);
|
|
348
350
|
} finally {
|
|
349
351
|
setLoading(false);
|
|
350
352
|
}
|
|
@@ -352,7 +354,7 @@ export function AskAI() {
|
|
|
352
354
|
|
|
353
355
|
return (
|
|
354
356
|
<div>
|
|
355
|
-
<button onClick={handleAsk} disabled={loading}>Ask
|
|
357
|
+
<button onClick={handleAsk} disabled={loading}>Ask Agent</button>
|
|
356
358
|
{answer && <p>{answer}</p>}
|
|
357
359
|
</div>
|
|
358
360
|
);
|
|
@@ -383,7 +385,7 @@ export function AgentChat() {
|
|
|
383
385
|
const query = input;
|
|
384
386
|
setInput("");
|
|
385
387
|
|
|
386
|
-
for await (const event of client.streamAgent(sessionRef.current.
|
|
388
|
+
for await (const event of client.streamAgent(sessionRef.current.agentId, {
|
|
387
389
|
sessionToken: sessionRef.current.sessionToken,
|
|
388
390
|
query,
|
|
389
391
|
})) {
|
|
@@ -413,8 +415,12 @@ const app = express();
|
|
|
413
415
|
const client = new PromptevClient({ projectKey: "pv_sk_..." });
|
|
414
416
|
|
|
415
417
|
app.post("/api/ask", express.json(), async (req, res) => {
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
+
const { response, sessionId } = await client.triggerAgent(
|
|
419
|
+
"your-agent-id",
|
|
420
|
+
req.body.question,
|
|
421
|
+
{ sessionId: req.body.sessionId } // pass back to keep memory across turns
|
|
422
|
+
);
|
|
423
|
+
res.json({ answer: response, sessionId });
|
|
418
424
|
});
|
|
419
425
|
|
|
420
426
|
app.listen(3000);
|
|
@@ -429,12 +435,19 @@ app.listen(3000);
|
|
|
429
435
|
const client = new PromptevClient({ projectKey: "pv_sk_..." });
|
|
430
436
|
|
|
431
437
|
document.getElementById("ask-btn").addEventListener("click", async () => {
|
|
432
|
-
const
|
|
433
|
-
document.getElementById("output").textContent =
|
|
438
|
+
const { response } = await client.triggerAgent("your-agent-id", "What is the refund policy?");
|
|
439
|
+
document.getElementById("output").textContent = response;
|
|
434
440
|
});
|
|
435
441
|
</script>
|
|
436
442
|
```
|
|
437
443
|
|
|
444
|
+
## Building from source
|
|
445
|
+
|
|
446
|
+
```bash
|
|
447
|
+
npm install
|
|
448
|
+
npm run build # builds both ESM (dist/esm) and CommonJS (dist/cjs)
|
|
449
|
+
```
|
|
450
|
+
|
|
438
451
|
## License
|
|
439
452
|
|
|
440
453
|
This SDK is commercial software by [Promptev Inc](https://promptev.ai).
|