bare-agent 0.11.0 → 0.12.1

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 (68) hide show
  1. package/README.md +1 -0
  2. package/bareagent.context.md +1149 -0
  3. package/bin/cli.d.ts +4 -0
  4. package/bin/cli.js +40 -10
  5. package/bin/test-provider.d.ts +2 -0
  6. package/bin/test-provider.js +5 -1
  7. package/index.d.ts +20 -0
  8. package/package.json +46 -10
  9. package/src/bareguard-adapter.d.ts +118 -0
  10. package/src/bareguard-adapter.js +75 -3
  11. package/src/checkpoint.d.ts +61 -0
  12. package/src/checkpoint.js +17 -8
  13. package/src/circuit-breaker.d.ts +70 -0
  14. package/src/circuit-breaker.js +20 -4
  15. package/src/errors.d.ts +106 -0
  16. package/src/errors.js +50 -1
  17. package/src/loop.d.ts +135 -0
  18. package/src/loop.js +73 -17
  19. package/src/mcp-bridge.d.ts +133 -0
  20. package/src/mcp-bridge.js +179 -27
  21. package/src/mcp.d.ts +4 -0
  22. package/src/memory.d.ts +50 -0
  23. package/src/memory.js +22 -2
  24. package/src/planner.d.ts +62 -0
  25. package/src/planner.js +26 -7
  26. package/src/provider-anthropic.d.ts +55 -0
  27. package/src/provider-anthropic.js +32 -11
  28. package/src/provider-clipipe.d.ts +86 -0
  29. package/src/provider-clipipe.js +28 -18
  30. package/src/provider-fallback.d.ts +44 -0
  31. package/src/provider-fallback.js +18 -8
  32. package/src/provider-ollama.d.ts +41 -0
  33. package/src/provider-ollama.js +27 -7
  34. package/src/provider-openai.d.ts +57 -0
  35. package/src/provider-openai.js +31 -16
  36. package/src/providers.d.ts +6 -0
  37. package/src/providers.js +8 -0
  38. package/src/retry.d.ts +44 -0
  39. package/src/retry.js +15 -1
  40. package/src/run-plan.d.ts +126 -0
  41. package/src/run-plan.js +46 -13
  42. package/src/scheduler.d.ts +102 -0
  43. package/src/scheduler.js +32 -4
  44. package/src/state.d.ts +45 -0
  45. package/src/state.js +18 -2
  46. package/src/store-jsonfile.d.ts +85 -0
  47. package/src/store-jsonfile.js +33 -8
  48. package/src/store-sqlite.d.ts +90 -0
  49. package/src/store-sqlite.js +31 -7
  50. package/src/stores.d.ts +3 -0
  51. package/src/stream.d.ts +79 -0
  52. package/src/stream.js +32 -0
  53. package/src/tools.d.ts +8 -0
  54. package/src/transport-jsonl.d.ts +30 -0
  55. package/src/transport-jsonl.js +13 -0
  56. package/src/transports.d.ts +2 -0
  57. package/tools/browse.d.ts +10 -0
  58. package/tools/browse.js +2 -0
  59. package/tools/defer.d.ts +33 -0
  60. package/tools/defer.js +12 -3
  61. package/tools/mobile.d.ts +34 -0
  62. package/tools/mobile.js +28 -15
  63. package/tools/shell.d.ts +31 -0
  64. package/tools/shell.js +55 -6
  65. package/tools/spawn.d.ts +107 -0
  66. package/tools/spawn.js +24 -5
  67. package/types/index.d.ts +66 -0
  68. package/types/shims.d.ts +16 -0
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Library-level: spawn a child and return a handle.
3
+ *
4
+ * Returns: {
5
+ * wait() — Promise<{ text, usage, cost, error, events }>
6
+ * onLine(fn) — subscribe to every JSONL event from child stdout
7
+ * kill(sig?) — terminate the child
8
+ * pid — child process id
9
+ * }
10
+ *
11
+ * Use this from library code; the LLM-callable tool below wraps it with blocking semantics.
12
+ */
13
+ export type ChildEvent = Record<string, any> & {
14
+ type?: string;
15
+ text?: string;
16
+ ts?: string;
17
+ data?: any;
18
+ };
19
+ /**
20
+ * Library-level: spawn a child and return a handle.
21
+ *
22
+ * Returns: {
23
+ * wait() — Promise<{ text, usage, cost, error, events }>
24
+ * onLine(fn) — subscribe to every JSONL event from child stdout
25
+ * kill(sig?) — terminate the child
26
+ * pid — child process id
27
+ * }
28
+ *
29
+ * Use this from library code; the LLM-callable tool below wraps it with blocking semantics.
30
+ */
31
+ export type SpawnChildOptions = {
32
+ /**
33
+ * - Path to a bareagent config JSON file.
34
+ */
35
+ config?: string | undefined;
36
+ /**
37
+ * - Optional JSON input passed to the child on stdin.
38
+ */
39
+ input?: any;
40
+ /**
41
+ * - Override the bareagent CLI path.
42
+ */
43
+ cliPath?: string | undefined;
44
+ /**
45
+ * - Force-kill child after this many ms.
46
+ */
47
+ timeoutMs?: number | undefined;
48
+ /**
49
+ * - bareagent Stream — child:stderr events get re-emitted here.
50
+ */
51
+ stream?: import("../src/stream").Stream | undefined;
52
+ };
53
+ export type Stream = import("../src/stream").Stream;
54
+ /**
55
+ * LLM-callable spawn tool. Blocks; returns the child's final result.
56
+ *
57
+ * @param {object} [options]
58
+ * @param {string} [options.cliPath] - Override the bareagent CLI path (default: ./bin/cli.js relative to this file).
59
+ * @param {number} [options.timeoutMs] - Force-kill child after this many ms (default 10 min).
60
+ * @param {Stream} [options.stream] - bareagent Stream instance — child:stderr events get re-emitted here.
61
+ * @returns {{tool: import('../types').ToolDef, spawnChild: typeof spawnChild}}
62
+ */
63
+ export function createSpawnTool(options?: {
64
+ cliPath?: string | undefined;
65
+ timeoutMs?: number | undefined;
66
+ stream?: import("../src/stream").Stream | undefined;
67
+ }): {
68
+ tool: import("../types").ToolDef;
69
+ spawnChild: typeof spawnChild;
70
+ };
71
+ /**
72
+ * Library-level: spawn a child and return a handle.
73
+ *
74
+ * Returns: {
75
+ * wait() — Promise<{ text, usage, cost, error, events }>
76
+ * onLine(fn) — subscribe to every JSONL event from child stdout
77
+ * kill(sig?) — terminate the child
78
+ * pid — child process id
79
+ * }
80
+ *
81
+ * Use this from library code; the LLM-callable tool below wraps it with blocking semantics.
82
+ *
83
+ * @typedef {Record<string, any> & {type?: string, text?: string, ts?: string, data?: any}} ChildEvent
84
+ *
85
+ * @typedef {object} SpawnChildOptions
86
+ * @property {string} [config] - Path to a bareagent config JSON file.
87
+ * @property {*} [input] - Optional JSON input passed to the child on stdin.
88
+ * @property {string} [cliPath] - Override the bareagent CLI path.
89
+ * @property {number} [timeoutMs] - Force-kill child after this many ms.
90
+ * @property {Stream} [stream] - bareagent Stream — child:stderr events get re-emitted here.
91
+ *
92
+ * @param {SpawnChildOptions} [opts]
93
+ */
94
+ export function spawnChild({ config, input, cliPath, timeoutMs, stream }?: SpawnChildOptions): {
95
+ wait: () => Promise<{
96
+ text: any;
97
+ usage: any;
98
+ cost: any;
99
+ error: any;
100
+ events: ChildEvent[];
101
+ exitCode: any;
102
+ signal: any;
103
+ }>;
104
+ onLine: (fn: (event: ChildEvent) => void) => () => void;
105
+ kill: (sig?: NodeJS.Signals) => void;
106
+ pid: number | undefined;
107
+ };
package/tools/spawn.js CHANGED
@@ -31,6 +31,8 @@
31
31
  * per-family.
32
32
  */
33
33
 
34
+ /** @typedef {import('../src/stream').Stream} Stream */
35
+
34
36
  const { spawn: cpSpawn } = require('node:child_process');
35
37
  const path = require('node:path');
36
38
  const readline = require('node:readline');
@@ -57,6 +59,17 @@ function resolveCliPath() {
57
59
  * }
58
60
  *
59
61
  * Use this from library code; the LLM-callable tool below wraps it with blocking semantics.
62
+ *
63
+ * @typedef {Record<string, any> & {type?: string, text?: string, ts?: string, data?: any}} ChildEvent
64
+ *
65
+ * @typedef {object} SpawnChildOptions
66
+ * @property {string} [config] - Path to a bareagent config JSON file.
67
+ * @property {*} [input] - Optional JSON input passed to the child on stdin.
68
+ * @property {string} [cliPath] - Override the bareagent CLI path.
69
+ * @property {number} [timeoutMs] - Force-kill child after this many ms.
70
+ * @property {Stream} [stream] - bareagent Stream — child:stderr events get re-emitted here.
71
+ *
72
+ * @param {SpawnChildOptions} [opts]
60
73
  */
61
74
  function spawnChild({ config, input, cliPath, timeoutMs, stream } = {}) {
62
75
  if (typeof config !== 'string' || !config) {
@@ -81,8 +94,11 @@ function spawnChild({ config, input, cliPath, timeoutMs, stream } = {}) {
81
94
  }
82
95
  child.stdin.end();
83
96
 
97
+ /** @type {ChildEvent[]} */
84
98
  const events = [];
99
+ /** @type {((event: ChildEvent) => void)[]} */
85
100
  const lineSubscribers = [];
101
+ /** @param {(event: ChildEvent) => void} fn */
86
102
  const onLine = (fn) => { lineSubscribers.push(fn); return () => {
87
103
  const i = lineSubscribers.indexOf(fn);
88
104
  if (i >= 0) lineSubscribers.splice(i, 1);
@@ -100,7 +116,7 @@ function spawnChild({ config, input, cliPath, timeoutMs, stream } = {}) {
100
116
  }
101
117
  events.push(event);
102
118
  for (const fn of lineSubscribers) {
103
- try { fn(event); } catch (err) {
119
+ try { fn(event); } catch (/** @type {any} */ err) {
104
120
  // never let a subscriber kill the read loop
105
121
  process.stderr.write(`[spawn] onLine subscriber threw: ${err.message}\n`);
106
122
  }
@@ -165,7 +181,9 @@ function spawnChild({ config, input, cliPath, timeoutMs, stream } = {}) {
165
181
  };
166
182
  }
167
183
  // Pluck the final loop:done event — that's the canonical child result.
168
- const done = events.findLast?.(e => e.type === 'loop:done')
184
+ // `findLast` is es2023; the lib target may not declare it, so reach it via
185
+ // `any` while keeping the runtime fallback for older Node versions.
186
+ const done = /** @type {any} */ (events).findLast?.((/** @type {ChildEvent} */ e) => e.type === 'loop:done')
169
187
  || [...events].reverse().find(e => e.type === 'loop:done');
170
188
  if (done) {
171
189
  return {
@@ -191,6 +209,7 @@ function spawnChild({ config, input, cliPath, timeoutMs, stream } = {}) {
191
209
  };
192
210
  }
193
211
 
212
+ /** @param {NodeJS.Signals} [sig] */
194
213
  function kill(sig = 'SIGTERM') {
195
214
  try { child.kill(sig); } catch { /* already dead */ }
196
215
  }
@@ -204,8 +223,8 @@ function spawnChild({ config, input, cliPath, timeoutMs, stream } = {}) {
204
223
  * @param {object} [options]
205
224
  * @param {string} [options.cliPath] - Override the bareagent CLI path (default: ./bin/cli.js relative to this file).
206
225
  * @param {number} [options.timeoutMs] - Force-kill child after this many ms (default 10 min).
207
- * @param {object} [options.stream] - bareagent Stream instance — child:stderr events get re-emitted here.
208
- * @returns {{tool: object, spawnChild: Function}}
226
+ * @param {Stream} [options.stream] - bareagent Stream instance — child:stderr events get re-emitted here.
227
+ * @returns {{tool: import('../types').ToolDef, spawnChild: typeof spawnChild}}
209
228
  */
210
229
  function createSpawnTool(options = {}) {
211
230
  const tool = {
@@ -225,7 +244,7 @@ function createSpawnTool(options = {}) {
225
244
  },
226
245
  required: ['config'],
227
246
  },
228
- execute: async ({ config, input }) => {
247
+ execute: async (/** @type {{config: string, input?: *}} */ { config, input }) => {
229
248
  const handle = spawnChild({
230
249
  config,
231
250
  input,
@@ -0,0 +1,66 @@
1
+ // Shared, cross-cutting type shapes for bare-agent, consumed from JSDoc via
2
+ // /** @typedef {import('../types').Provider} Provider */ (path is relative to the .js file)
3
+ // These describe the structural contracts that flow between components (provider
4
+ // <-> loop <-> tools). Per-file option bags live as local @typedef blocks in
5
+ // each module; only the genuinely shared shapes belong here.
6
+
7
+ /** Token accounting returned by a provider's generate(). */
8
+ export interface Usage {
9
+ inputTokens: number;
10
+ outputTokens: number;
11
+ }
12
+
13
+ /** A single tool invocation requested by the model. `arguments` is parsed JSON. */
14
+ export interface ToolCall {
15
+ id: string;
16
+ name: string;
17
+ arguments: any;
18
+ }
19
+
20
+ /** Normalized result every provider.generate() resolves to. */
21
+ export interface GenerateResult {
22
+ text: string;
23
+ toolCalls: ToolCall[];
24
+ usage: Usage;
25
+ }
26
+
27
+ /** A conversation message in OpenAI chat format. */
28
+ export interface Message {
29
+ role: string;
30
+ content?: string | null;
31
+ tool_calls?: any[];
32
+ tool_call_id?: string;
33
+ [key: string]: any;
34
+ }
35
+
36
+ /** A callable tool exposed to the loop/provider. */
37
+ export interface ToolDef {
38
+ name: string;
39
+ description?: string;
40
+ parameters?: Record<string, any>;
41
+ execute(args: any): any | Promise<any>;
42
+ }
43
+
44
+ /** Minimal LLM provider contract the Loop and Planner depend on. */
45
+ export interface Provider {
46
+ /** Model id, surfaced for cost estimation. */
47
+ model?: string | null;
48
+ /** Provider name, surfaced in onLlmResult. */
49
+ name?: string | null;
50
+ generate(
51
+ messages: Message[],
52
+ tools?: ToolDef[],
53
+ options?: Record<string, any>,
54
+ ): Promise<GenerateResult>;
55
+ }
56
+
57
+ /** Swappable persistence backend for Memory. */
58
+ export interface Store {
59
+ store(content: any, metadata?: Record<string, any>): any;
60
+ search(query: string, options?: Record<string, any>): any;
61
+ get(id: any): any;
62
+ delete(id: any): any;
63
+ }
64
+
65
+ /** Opaque per-run blob forwarded to policy/record callbacks. */
66
+ export type Ctx = any;
@@ -0,0 +1,16 @@
1
+ // Ambient module declarations for dependencies that ship no TypeScript types.
2
+ //
3
+ // `bareguard` is a required dependency but publishes plain JS with no .d.ts; the
4
+ // others are optional/peer deps that may not be installed in every environment
5
+ // (and aren't installed in CI). Declaring them here lets `tsc --checkJs` run
6
+ // without `npm i`-ing native modules, while keeping our own JSDoc fully checked.
7
+ // Each `require()` of these modules resolves to `any`, so our code is responsible
8
+ // for documenting the shapes it relies on via local @typedef/@param JSDoc.
9
+
10
+ declare module 'bareguard';
11
+ declare module 'better-sqlite3';
12
+ declare module 'barebrowse';
13
+ declare module 'barebrowse/bareagent';
14
+ declare module 'baremobile';
15
+ declare module 'baremobile/ios';
16
+ declare module 'cron-parser';