backprompter-server-sdk 0.1.3 → 0.1.4

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 +35 -0
  2. package/index.js +44 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -28,6 +28,41 @@ const result = await bp.run({
28
28
  console.log(result.response);
29
29
  ```
30
30
 
31
+ ## Continuing a combo agent
32
+
33
+ When a combo agent reaches its limit of sub-agent calls before the task is
34
+ done, the result has `unfinished: true` and a `checkpoint`. Pass it back to
35
+ carry on from where it stopped (no `userPrompt` needed):
36
+
37
+ ```js
38
+ if (result.unfinished) {
39
+ const next = await bp.run({ agentId: 'researcher', continueFrom: result.checkpoint });
40
+ }
41
+ ```
42
+
43
+ The checkpoint is a short run id (`bpr_` + 22 characters). The run is saved
44
+ on Backprompter's servers for 7 days, works only for the app it came from, and
45
+ can be retried if a request fails. Continuing counts as a new message.
46
+ `autoContinue: n` continues up to n times (max 20) before returning;
47
+ `result.continued` says how many.
48
+
49
+ ## Code agents
50
+
51
+ Backprompter's servers never run code. With `clientRunsCode: true`, a combo
52
+ whose code sub-agent writes code pauses with `pending_code: [{ id, agent, code }]`.
53
+ Run it wherever you choose and send the results back:
54
+
55
+ ```js
56
+ let result = await bp.run({ agentId: 'analyst-team', userPrompt: 'Total sales by region', clientRunsCode: true, dataSchemas });
57
+ while (result.pending_code) {
58
+ const codeResults = result.pending_code.map(({ id, code }) => ({ id, output: runInYourSandbox(code) }));
59
+ result = await bp.run({ agentId: 'analyst-team', continueFrom: result.checkpoint, codeResults, clientRunsCode: true, dataSchemas });
60
+ }
61
+ ```
62
+
63
+ Send `{ id, error }` for code that failed. Code steps aren't new messages; one
64
+ reply runs code at most 10 times.
65
+
31
66
  ## Attachments
32
67
 
33
68
  `run()` takes `fileInputs` and `urlInputs`, and which key you use inside
package/index.js CHANGED
@@ -70,17 +70,33 @@ export default class BackprompterServer {
70
70
  // callbacks see the answer as it is written. See _streamRun.
71
71
  stream,
72
72
  onDelta,
73
- onStatus
73
+ onStatus,
74
+ // A combo agent's `checkpoint` (a saved run id, "bpr_…", kept for 7 days)
75
+ // from an earlier result with `unfinished: true` or `pending_code`:
76
+ // carries on from where it stopped. No userPrompt needed.
77
+ continueFrom,
78
+ // Continue an unfinished combo answer up to this many times (max 20)
79
+ // before returning. The result says how many in `continued`.
80
+ autoContinue,
81
+ // Code agents. With clientRunsCode, a combo pauses when a code sub-agent
82
+ // writes code and returns `pending_code: [{ id, agent, code }]`. Run the
83
+ // code wherever you choose (Backprompter's servers never run it), then call
84
+ // run({ continueFrom: result.checkpoint, codeResults: [{ id, output } |
85
+ // { id, error }] }). dataSchemas: your datasets' schemas, so the code is
86
+ // written for your columns.
87
+ clientRunsCode,
88
+ codeResults,
89
+ dataSchemas
74
90
  } = {}) {
75
91
  const resolvedApiKey = String(apiKey || this._apiKey || '').trim();
76
92
  if (!resolvedApiKey) throw new Error('BackprompterServer: apiKey is required.');
77
- if (!userPrompt) throw new Error('BackprompterServer: userPrompt is required.');
93
+ if (!userPrompt && !continueFrom) throw new Error('BackprompterServer: userPrompt is required.');
78
94
 
79
95
  const resolvedTenantId = tenantId || this._tenantId || undefined;
80
96
  const body = {
81
97
  tenant_id: resolvedTenantId,
82
98
  agent_id: agentId,
83
- user_prompt: userPrompt,
99
+ user_prompt: userPrompt || '',
84
100
  conversation_history: Array.isArray(conversationHistory) ? conversationHistory : [],
85
101
  profileString: String(profileString || ''),
86
102
  file_inputs: Array.isArray(fileInputs) ? fileInputs : [],
@@ -92,12 +108,31 @@ export default class BackprompterServer {
92
108
  if (genDiff !== undefined) body.gen_diff = genDiff;
93
109
  if (baseText !== undefined) body.base_text = baseText;
94
110
  if (ragCharBudget !== undefined) body.rag_char_budget = ragCharBudget;
95
-
96
- if (stream) return this._streamRun(resolvedApiKey, body, { onDelta, onStatus });
97
- return this._apiFetchWithApiKey('/public/run', resolvedApiKey, {
98
- method: 'POST',
99
- body: JSON.stringify(body)
100
- });
111
+ if (continueFrom) body.continue_from = continueFrom;
112
+ if (clientRunsCode) body.client_runs_code = true;
113
+ if (Array.isArray(codeResults)) body.code_results = codeResults;
114
+ if (Array.isArray(dataSchemas)) body.data_schemas = dataSchemas;
115
+
116
+ const send = (b) => (stream
117
+ ? this._streamRun(resolvedApiKey, b, { onDelta, onStatus })
118
+ : this._apiFetchWithApiKey('/public/run', resolvedApiKey, { method: 'POST', body: JSON.stringify(b) }));
119
+
120
+ let res = await send(body);
121
+ const limit = Math.min(Math.max(Math.floor(Number(autoContinue) || 0), 0), 20);
122
+ let continued = 0;
123
+ while (res && res.unfinished && res.checkpoint && !res.pending_code && continued < limit) {
124
+ continued += 1;
125
+ if (typeof onStatus === 'function') onStatus({ type: 'status', stage: 'continue', count: continued });
126
+ res = await send({
127
+ tenant_id: resolvedTenantId,
128
+ user_prompt: '',
129
+ continue_from: res.checkpoint,
130
+ ...(clientRunsCode ? { client_runs_code: true } : {}),
131
+ ...(Array.isArray(dataSchemas) ? { data_schemas: dataSchemas } : {})
132
+ });
133
+ }
134
+ if (res && continued) res.continued = continued;
135
+ return res;
101
136
  }
102
137
 
103
138
  // POST /public/run with "stream": true and read the server-sent events.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backprompter-server-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Backprompter server-side SDK for Node.js backends",
5
5
  "type": "module",
6
6
  "main": "index.js",