backprompter-server-sdk 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.
Files changed (3) hide show
  1. package/README.md +36 -0
  2. package/index.js +131 -0
  3. package/package.json +23 -0
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Backprompter Server SDK
2
+
3
+ Node.js SDK for server-to-server calls to Backprompter using API keys.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install backprompter-server-sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```js
14
+ import BackprompterServer from 'backprompter-server-sdk';
15
+
16
+ const bp = new BackprompterServer({
17
+ apiKey: process.env.BACKPROMPTER_API_KEY,
18
+ tenantId: process.env.BACKPROMPTER_APP_KEY,
19
+ backendUrl: process.env.BACKPROMPTER_BACKEND_URL
20
+ });
21
+
22
+ const result = await bp.run({
23
+ agentId: 'your-agent-name',
24
+ userPrompt: 'Summarize this support ticket.',
25
+ conversationHistory: [],
26
+ profileString: 'Enterprise customer'
27
+ });
28
+
29
+ console.log(result.response);
30
+ ```
31
+
32
+ ## Notes
33
+
34
+ - Requires Node.js 18+ (uses built-in fetch).
35
+ - Uses `x-bp-api-key` authentication.
36
+ - `tenantId` can be passed in constructor or per-call (`run({ tenantId })`).
package/index.js ADDED
@@ -0,0 +1,131 @@
1
+ const DEFAULT_BACKEND_URL = 'https://backprompter-us-628834831216.us-central1.run.app';
2
+ const LOCAL_BACKEND_URL = 'http://localhost:8080';
3
+
4
+ function normalizeBackendUrl(url) {
5
+ const raw = String(url || '').trim();
6
+ return raw ? raw.replace(/\/+$/, '') : '';
7
+ }
8
+
9
+ function assertFetchAvailable() {
10
+ if (typeof fetch !== 'function') {
11
+ throw new Error('BackprompterServer: fetch is not available. Use Node 18+ runtime.');
12
+ }
13
+ }
14
+
15
+ function getErrorMessage(status, payload) {
16
+ if (payload && typeof payload === 'object') {
17
+ if (typeof payload.error === 'string' && payload.error.trim()) return payload.error;
18
+ if (typeof payload.message === 'string' && payload.message.trim()) return payload.message;
19
+ }
20
+ return `HTTP ${status}`;
21
+ }
22
+
23
+ export default class BackprompterServer {
24
+ constructor({ apiKey, tenantId, backendUrl } = {}) {
25
+ this._apiKey = apiKey ? String(apiKey).trim() : '';
26
+ this._tenantId = tenantId ? String(tenantId).trim() : '';
27
+ this._backendUrl = normalizeBackendUrl(backendUrl || DEFAULT_BACKEND_URL);
28
+ if (!this._backendUrl) throw new Error('BackprompterServer: backendUrl cannot be empty.');
29
+ }
30
+
31
+ static get DEFAULT_BACKEND_URL() {
32
+ return DEFAULT_BACKEND_URL;
33
+ }
34
+
35
+ static get LOCAL_BACKEND_URL() {
36
+ return LOCAL_BACKEND_URL;
37
+ }
38
+
39
+ setApiKey(apiKey) {
40
+ this._apiKey = String(apiKey || '').trim();
41
+ return this._apiKey;
42
+ }
43
+
44
+ setTenantId(tenantId) {
45
+ this._tenantId = String(tenantId || '').trim();
46
+ return this._tenantId;
47
+ }
48
+
49
+ setBackendUrl(url) {
50
+ const normalized = normalizeBackendUrl(url);
51
+ if (!normalized) throw new Error('BackprompterServer: backendUrl cannot be empty.');
52
+ this._backendUrl = normalized;
53
+ return this._backendUrl;
54
+ }
55
+
56
+ setEnvironment(env) {
57
+ const normalizedEnv = String(env || '').trim().toLowerCase();
58
+ if (normalizedEnv === 'local') return this.setBackendUrl(LOCAL_BACKEND_URL);
59
+ if (normalizedEnv === 'production' || normalizedEnv === 'prod') return this.setBackendUrl(DEFAULT_BACKEND_URL);
60
+ throw new Error("BackprompterServer: setEnvironment expects 'local' or 'production'.");
61
+ }
62
+
63
+ getBackendUrl() {
64
+ return this._backendUrl;
65
+ }
66
+
67
+ async run({
68
+ userPrompt,
69
+ agentId,
70
+ conversationHistory = [],
71
+ profileString = '',
72
+ fileInputs = [],
73
+ urlInputs = [],
74
+ contextLimit,
75
+ maxTokens,
76
+ genDiff,
77
+ baseText,
78
+ ragCharBudget,
79
+ tenantId,
80
+ apiKey
81
+ } = {}) {
82
+ const resolvedApiKey = String(apiKey || this._apiKey || '').trim();
83
+ if (!resolvedApiKey) throw new Error('BackprompterServer: apiKey is required.');
84
+ if (!userPrompt) throw new Error('BackprompterServer: userPrompt is required.');
85
+
86
+ const resolvedTenantId = tenantId || this._tenantId || undefined;
87
+ const body = {
88
+ tenant_id: resolvedTenantId,
89
+ agent_id: agentId,
90
+ user_prompt: userPrompt,
91
+ conversation_history: Array.isArray(conversationHistory) ? conversationHistory : [],
92
+ profileString: String(profileString || ''),
93
+ file_inputs: Array.isArray(fileInputs) ? fileInputs : [],
94
+ url_inputs: Array.isArray(urlInputs) ? urlInputs : []
95
+ };
96
+
97
+ if (contextLimit !== undefined) body.context_limit = contextLimit;
98
+ if (maxTokens !== undefined) body.max_tokens = maxTokens;
99
+ if (genDiff !== undefined) body.gen_diff = genDiff;
100
+ if (baseText !== undefined) body.base_text = baseText;
101
+ if (ragCharBudget !== undefined) body.rag_char_budget = ragCharBudget;
102
+
103
+ return this._apiFetchWithApiKey('/public/run', resolvedApiKey, {
104
+ method: 'POST',
105
+ body: JSON.stringify(body)
106
+ });
107
+ }
108
+
109
+ async _apiFetchWithApiKey(path, apiKey, options = {}) {
110
+ assertFetchAvailable();
111
+
112
+ const base = this._backendUrl;
113
+ const requestPath = String(path || '').startsWith('/') ? String(path || '') : `/${String(path || '')}`;
114
+ const url = `${base}${requestPath}`;
115
+
116
+ const response = await fetch(url, {
117
+ ...options,
118
+ headers: {
119
+ 'Content-Type': 'application/json',
120
+ 'x-bp-api-key': apiKey,
121
+ ...(options.headers || {})
122
+ }
123
+ });
124
+
125
+ const payload = await response.json().catch(() => ({}));
126
+ if (!response.ok) {
127
+ throw new Error(getErrorMessage(response.status, payload));
128
+ }
129
+ return payload;
130
+ }
131
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "backprompter-server-sdk",
3
+ "version": "0.1.0",
4
+ "description": "Backprompter server-side SDK for Node.js backends",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "exports": {
8
+ ".": "./index.js"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "engines": {
14
+ "node": ">=18"
15
+ },
16
+ "keywords": [
17
+ "backprompter",
18
+ "sdk",
19
+ "server",
20
+ "node"
21
+ ],
22
+ "license": "MIT"
23
+ }