@cubicecho/agent-core 2.24.0 → 2.25.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 (71) hide show
  1. package/README.md +10 -7
  2. package/dist/agent-loop.d.ts +84 -205
  3. package/dist/agent-loop.js +288 -597
  4. package/dist/calibration.d.ts +14 -10
  5. package/dist/calibration.js +30 -23
  6. package/dist/capabilities.d.ts +111 -42
  7. package/dist/capabilities.js +281 -138
  8. package/dist/client.d.ts +104 -35
  9. package/dist/client.js +205 -74
  10. package/dist/compaction.d.ts +103 -60
  11. package/dist/compaction.js +160 -86
  12. package/dist/config.d.ts +48 -12
  13. package/dist/config.js +20 -1
  14. package/dist/continuation.d.ts +24 -16
  15. package/dist/continuation.js +70 -44
  16. package/dist/digest.d.ts +7 -0
  17. package/dist/digest.js +18 -0
  18. package/dist/errors.d.ts +24 -15
  19. package/dist/errors.js +23 -15
  20. package/dist/events.d.ts +100 -63
  21. package/dist/events.js +250 -120
  22. package/dist/guards.d.ts +35 -4
  23. package/dist/guards.js +38 -5
  24. package/dist/hook-events.d.ts +21 -5
  25. package/dist/hook-events.js +23 -9
  26. package/dist/hooks.d.ts +96 -73
  27. package/dist/hooks.js +202 -116
  28. package/dist/index.d.ts +30 -26
  29. package/dist/index.js +29 -24
  30. package/dist/ledger.d.ts +62 -30
  31. package/dist/ledger.js +163 -56
  32. package/dist/platform.d.ts +13 -0
  33. package/dist/platform.js +13 -0
  34. package/dist/preselect.d.ts +222 -0
  35. package/dist/preselect.js +290 -0
  36. package/dist/request-body.d.ts +32 -0
  37. package/dist/request-body.js +47 -0
  38. package/dist/reset.d.ts +1 -0
  39. package/dist/reset.js +7 -6
  40. package/dist/retry.d.ts +29 -138
  41. package/dist/retry.js +69 -282
  42. package/dist/run-calls.d.ts +123 -0
  43. package/dist/run-calls.js +351 -0
  44. package/dist/run-turn.d.ts +36 -15
  45. package/dist/run-turn.js +74 -37
  46. package/dist/runtime.d.ts +23 -17
  47. package/dist/runtime.js +34 -24
  48. package/dist/schema-compat.d.ts +35 -18
  49. package/dist/schema-compat.js +340 -175
  50. package/dist/scope.d.ts +28 -5
  51. package/dist/scope.js +37 -10
  52. package/dist/side-task.d.ts +123 -40
  53. package/dist/side-task.js +181 -63
  54. package/dist/snapshot.d.ts +10 -5
  55. package/dist/snapshot.js +73 -30
  56. package/dist/spec.d.ts +56 -25
  57. package/dist/spec.js +494 -215
  58. package/dist/stream.d.ts +66 -16
  59. package/dist/stream.js +220 -97
  60. package/dist/thinking.d.ts +23 -8
  61. package/dist/thinking.js +92 -44
  62. package/dist/tokens.d.ts +164 -6
  63. package/dist/tokens.js +319 -6
  64. package/dist/tool-calls.d.ts +82 -17
  65. package/dist/tool-calls.js +198 -83
  66. package/dist/tool-loading.d.ts +101 -189
  67. package/dist/tool-loading.js +277 -371
  68. package/dist/wire.d.ts +81 -0
  69. package/dist/wire.js +73 -0
  70. package/llms.txt +76 -39
  71. package/package.json +4 -4
package/dist/runtime.d.ts CHANGED
@@ -1,15 +1,16 @@
1
- import { preselect, runAgentLoop } from "./agent-loop.ts";
2
- import { calibrate, charsPerTokenFor, resetCalibration } from "./calibration.ts";
3
- import { capabilitiesFor, expireCapabilities, resetCapabilities } from "./capabilities.ts";
4
- import { type ClientPoolOptions, configureClients, contextLimitFor, getClient, listModels, resetClients, servedWindow } from "./client.ts";
5
- import { compactTranscript, runCompaction, summariser } from "./compaction.ts";
6
- import { continueTurn } from "./continuation.ts";
7
- import { configureEvents, type EventBusOptions, emit, endRun, history, resetEvents, watch } from "./events.ts";
8
- import { assembleContext, configureHooks, consult, gather, type HookOptions, notify, resetHooks, withContext } from "./hooks.ts";
9
- import { resetAll } from "./reset.ts";
10
- import { runTurn } from "./run-turn.ts";
11
- import { ask, askJson, resetHints, tryAsk } from "./side-task.ts";
12
- import { exportCapabilities, importCapabilities } from "./snapshot.ts";
1
+ import { runAgentLoop } from './agent-loop.ts';
2
+ import { calibrate, charsPerTokenFor, resetCalibration } from './calibration.ts';
3
+ import { capabilitiesFor, expireCapabilities, resetCapabilities } from './capabilities.ts';
4
+ import { type ClientPoolOptions, configureClients, contextLimitFor, getClient, listModels, resetClients, servedWindow } from './client.ts';
5
+ import { compactTranscript, runCompaction, summariser } from './compaction.ts';
6
+ import { continueTurn } from './continuation.ts';
7
+ import { configureEvents, type EventBusOptions, emit, endRun, history, resetEvents, watch } from './events.ts';
8
+ import { assembleContext, configureHooks, consult, gather, type HookOptions, notify, resetHooks, withContext } from './hooks.ts';
9
+ import { preselect } from './preselect.ts';
10
+ import { resetAll } from './reset.ts';
11
+ import { runTurn } from './run-turn.ts';
12
+ import { ask, askJson, resetHints, tryAsk } from './side-task.ts';
13
+ import { exportCapabilities, importCapabilities } from './snapshot.ts';
13
14
  /**
14
15
  * Every exported function that reads or writes what a runtime remembers, itself or through what
15
16
  * it calls. The rest of the package is pure, and is the same function whichever runtime it is
@@ -68,6 +69,7 @@ export interface RuntimeOptions {
68
69
  /**
69
70
  * One set of the package's caches, with the functions that use them as methods.
70
71
  *
72
+ * @remarks
71
73
  * Each method is the top-level function of the same name, with the same signature and doc
72
74
  * comment, run against this runtime's state instead of the process's: its pooled clients and
73
75
  * model listings, latched capabilities, measured characters per token, no-thinking hints, event
@@ -79,10 +81,11 @@ export type Runtime = typeof STATEFUL & {
79
81
  * an `await`, or from a timer it sets — uses this runtime's state. What the methods do, for
80
82
  * code that calls the top-level functions itself.
81
83
  *
84
+ * @param fn - What to run. Its return value, or what it throws, is handed straight back.
85
+ *
86
+ * @remarks
82
87
  * It does not reach a callback that outlives `fn` and is called from elsewhere — a generator
83
88
  * resumed later, a function handed back and called by someone else. Call a method there.
84
- *
85
- * @param fn What to run. Its return value, or what it throws, is handed straight back.
86
89
  */
87
90
  run<T>(fn: () => T): T;
88
91
  };
@@ -90,6 +93,11 @@ export type Runtime = typeof STATEFUL & {
90
93
  * A runtime of its own: separate clients, listings, capability latches, hints, event bus and hook
91
94
  * settings from the process's and from every other runtime's.
92
95
  *
96
+ * @param [options] - What to configure it with before first use. A part left out keeps the
97
+ * defaults — not the default runtime's settings, which a new runtime does not inherit.
98
+ * @returns A runtime whose state starts empty, with whatever `options` gave already applied.
99
+ *
100
+ * @remarks
93
101
  * For a host that must not share them — one that mints a key per tenant, where a shared pool has
94
102
  * tenants evicting each other's clients and a shared bus has one tenant's run id readable by
95
103
  * another — and for tests, where a runtime per case needs no `resetAll` between them and can run
@@ -104,14 +112,12 @@ export type Runtime = typeof STATEFUL & {
104
112
  *
105
113
  * Nothing needs closing. A runtime is garbage once nothing holds it; its one timer, the event
106
114
  * bus's sweep, is unreferenced and stops rescheduling when its last run has been swept.
107
- *
108
- * @param options What to configure it with before first use. A part left out keeps the
109
- * defaults — not the default runtime's settings, which a new runtime does not inherit.
110
115
  */
111
116
  export declare function createRuntime(options?: RuntimeOptions): Runtime;
112
117
  /**
113
118
  * The runtime the top-level functions use when no other is running.
114
119
  *
120
+ * @remarks
115
121
  * `defaultRuntime.getClient(endpoint)` and `getClient(endpoint)` are the same call outside any
116
122
  * runtime. Inside one they differ: the top-level function follows the runtime that is running,
117
123
  * and this always reaches the process-wide state.
package/dist/runtime.js CHANGED
@@ -1,16 +1,17 @@
1
- import { preselect, runAgentLoop } from "./agent-loop.js";
2
- import { calibrate, charsPerTokenFor, resetCalibration } from "./calibration.js";
3
- import { capabilitiesFor, expireCapabilities, resetCapabilities } from "./capabilities.js";
4
- import { configureClients, contextLimitFor, getClient, listModels, resetClients, servedWindow, } from "./client.js";
5
- import { compactTranscript, runCompaction, summariser } from "./compaction.js";
6
- import { continueTurn } from "./continuation.js";
7
- import { configureEvents, emit, endRun, history, resetEvents, watch, } from "./events.js";
8
- import { assembleContext, configureHooks, consult, gather, notify, resetHooks, withContext, } from "./hooks.js";
9
- import { resetAll } from "./reset.js";
10
- import { runTurn } from "./run-turn.js";
11
- import { inScope, rootScope } from "./scope.js";
12
- import { ask, askJson, resetHints, tryAsk } from "./side-task.js";
13
- import { exportCapabilities, importCapabilities } from "./snapshot.js";
1
+ import { runAgentLoop } from './agent-loop.js';
2
+ import { calibrate, charsPerTokenFor, resetCalibration } from './calibration.js';
3
+ import { capabilitiesFor, expireCapabilities, resetCapabilities } from './capabilities.js';
4
+ import { configureClients, contextLimitFor, getClient, listModels, resetClients, servedWindow, } from './client.js';
5
+ import { compactTranscript, runCompaction, summariser } from './compaction.js';
6
+ import { continueTurn } from './continuation.js';
7
+ import { configureEvents, emit, endRun, history, resetEvents, watch } from './events.js';
8
+ import { assembleContext, configureHooks, consult, gather, notify, resetHooks, withContext, } from './hooks.js';
9
+ import { preselect } from './preselect.js';
10
+ import { resetAll } from './reset.js';
11
+ import { runTurn } from './run-turn.js';
12
+ import { inScope, rootScope } from './scope.js';
13
+ import { ask, askJson, resetHints, tryAsk } from './side-task.js';
14
+ import { exportCapabilities, importCapabilities } from './snapshot.js';
14
15
  /**
15
16
  * Every exported function that reads or writes what a runtime remembers, itself or through what
16
17
  * it calls. The rest of the package is pure, and is the same function whichever runtime it is
@@ -57,13 +58,16 @@ const STATEFUL = {
57
58
  resetHooks,
58
59
  resetAll,
59
60
  };
60
- /** Makes the methods of the runtime whose state is `scope`. */
61
+ /**
62
+ * Makes the methods of the runtime whose state is `scope`.
63
+ *
64
+ * @param scope - The state the methods run against. Held, not copied.
65
+ * @returns Every function in `STATEFUL` wrapped to run in `scope`, and `run`. What `summariser`
66
+ * hands back is wrapped as well, since it is called after the method has returned.
67
+ */
61
68
  function bind(scope) {
62
69
  const within = (fn) => (...args) => inScope(scope, () => fn(...args));
63
- const methods = Object.fromEntries(Object.entries(STATEFUL).map(([name, fn]) => [
64
- name,
65
- within(fn),
66
- ]));
70
+ const methods = Object.fromEntries(Object.entries(STATEFUL).map(([name, fn]) => [name, within(fn)]));
67
71
  return {
68
72
  ...methods,
69
73
  // What it hands back is called later, by `compactTranscript` or by the host, and asks then.
@@ -75,6 +79,11 @@ function bind(scope) {
75
79
  * A runtime of its own: separate clients, listings, capability latches, hints, event bus and hook
76
80
  * settings from the process's and from every other runtime's.
77
81
  *
82
+ * @param [options] - What to configure it with before first use. A part left out keeps the
83
+ * defaults — not the default runtime's settings, which a new runtime does not inherit.
84
+ * @returns A runtime whose state starts empty, with whatever `options` gave already applied.
85
+ *
86
+ * @remarks
78
87
  * For a host that must not share them — one that mints a key per tenant, where a shared pool has
79
88
  * tenants evicting each other's clients and a shared bus has one tenant's run id readable by
80
89
  * another — and for tests, where a runtime per case needs no `resetAll` between them and can run
@@ -89,23 +98,24 @@ function bind(scope) {
89
98
  *
90
99
  * Nothing needs closing. A runtime is garbage once nothing holds it; its one timer, the event
91
100
  * bus's sweep, is unreferenced and stops rescheduling when its last run has been swept.
92
- *
93
- * @param options What to configure it with before first use. A part left out keeps the
94
- * defaults — not the default runtime's settings, which a new runtime does not inherit.
95
101
  */
96
102
  export function createRuntime(options = {}) {
97
103
  const runtime = bind(new Map());
98
- if (options.clients)
104
+ if (options.clients) {
99
105
  runtime.configureClients(options.clients);
100
- if (options.events)
106
+ }
107
+ if (options.events) {
101
108
  runtime.configureEvents(options.events);
102
- if (options.hooks)
109
+ }
110
+ if (options.hooks) {
103
111
  runtime.configureHooks(options.hooks);
112
+ }
104
113
  return runtime;
105
114
  }
106
115
  /**
107
116
  * The runtime the top-level functions use when no other is running.
108
117
  *
118
+ * @remarks
109
119
  * `defaultRuntime.getClient(endpoint)` and `getClient(endpoint)` are the same call outside any
110
120
  * runtime. Inside one they differ: the top-level function follows the runtime that is running,
111
121
  * and this always reaches the process-wide state.
@@ -1,53 +1,70 @@
1
- import type OpenAI from "openai";
1
+ import type OpenAI from 'openai';
2
2
  /**
3
3
  * One JSON Schema as a strict server will accept it, for a schema that is not a tool's parameters.
4
4
  *
5
+ * @param schema - The schema as written. Never mutated. Its root is held to an object, as a tool's
6
+ * parameters are, and anything that is not a schema at all comes back as an object with no
7
+ * properties.
8
+ * @returns A new object: `type: "object"` with a `properties` map, no combinator at the root, no
9
+ * `required` name without a property, and no definition that nothing points at.
10
+ *
11
+ * @remarks
5
12
  * What `sanitizeTools` does to each tool, without the tool: a `response_format` schema goes through
6
13
  * the same grammar converter a tool's parameters do, and a caller holding only the schema had to
7
14
  * wrap it in a definition to get here. Nothing is remembered — the cache is keyed on a tool
8
15
  * definition's identity, and a bare schema has no object that stands for it across requests.
9
- *
10
- * @param schema The schema as written. Never mutated. Its root is held to an object, as a tool's
11
- * parameters are, and anything that is not a schema at all comes back as an object with no
12
- * properties.
13
16
  */
14
17
  export declare function sanitizeSchema(schema: unknown): Record<string, unknown>;
15
18
  /**
16
19
  * Tool definitions a strict server will accept, remembered per definition object.
17
20
  *
21
+ * @param tools - The definitions as the pool hands them over. Never mutated — where a schema
22
+ * changed, a new definition is returned in its place.
23
+ * @returns A new array in the same order. Each entry is the definition remembered for its tool, so
24
+ * the same object on every call; a tool that is not a function tool is its own entry.
25
+ *
26
+ * @remarks
18
27
  * The first call on a connection's tools does the work and every later one is a lookup, so
19
28
  * calling this per request costs nothing.
20
- *
21
- * @param tools The definitions as the pool hands them over. Never mutated — where a schema
22
- * changed, a new definition is returned in its place.
23
29
  */
24
30
  export declare const sanitizeTools: (tools: OpenAI.ChatCompletionTool[]) => OpenAI.ChatCompletionTool[];
25
31
  /**
26
32
  * One schema without its `pattern` and `format` keywords, for a schema that is not a tool's.
27
33
  *
34
+ * @param schema - Already sanitised. Relaxing is the retry, not a substitute for `sanitizeSchema`.
35
+ * Never mutated; anything that is not a schema comes back as an object with no properties.
36
+ * @returns A new object, the keywords gone from every schema position in it.
37
+ *
38
+ * @remarks
28
39
  * The retry `relaxTools` makes, on a bare schema. Only the keywords go: a property that happens to
29
40
  * be called `format` is an argument name and stays.
30
- *
31
- * @param schema Already sanitised. Relaxing is the retry, not a substitute for `sanitizeSchema`.
32
- * Never mutated; anything that is not a schema comes back as an object with no properties.
33
41
  */
34
42
  export declare function relaxSchema(schema: unknown): Record<string, unknown>;
35
43
  /**
36
- * The retry shape: llama.cpp's converter rejects regex escape classes (`\d`, `\w`, `\s`) in
37
- * `pattern` and most `format` values, both of which only ever narrowed a string the tool
38
- * re-validates anyway.
44
+ * The tool array with every schema's `pattern` and `format` keywords gone — the shape a turn is
45
+ * retried with.
46
+ *
47
+ * @param tools - Already sanitised. Relaxing is the retry, not a substitute for `sanitizeTools`.
48
+ * Never mutated.
49
+ * @returns A new array in the same order. Each entry is the relaxed definition remembered for its
50
+ * tool, so the same object on every call; a tool that is not a function tool is its own entry.
39
51
  *
40
- * @param tools Already sanitised. Relaxing is the retry, not a substitute for `sanitizeTools`.
52
+ * @remarks
53
+ * llama.cpp's converter rejects regex escape classes (`\d`, `\w`, `\s`) in `pattern` and most
54
+ * `format` values, both of which only ever narrowed a string the tool re-validates anyway.
41
55
  */
42
56
  export declare const relaxTools: (tools: OpenAI.ChatCompletionTool[]) => OpenAI.ChatCompletionTool[];
43
57
  /**
44
- * Does this failure look like the server could not build a grammar from our tool schemas?
58
+ * Whether a failure looks like the server could not build a grammar from our tool schemas.
45
59
  *
60
+ * @param message - The server's error text. Matched case-insensitively.
61
+ * @returns `true` for a grammar or schema-conversion failure. `false` for anything else, and
62
+ * always for a chat template's "no user query found", whatever it is wrapped in.
63
+ *
64
+ * @remarks
46
65
  * Every server words this differently — llama-server says "error parsing grammar", Lemonade
47
66
  * says "Failed to initialize samplers: failed to parse grammar", others surface the converter
48
67
  * by name. Since a grammar is only ever involved in constrained decoding, treat any mention of
49
68
  * one as ours; the retry is cheap and latches after a single request.
50
- *
51
- * @param message The server's error text. Matched case-insensitively.
52
69
  */
53
70
  export declare function isGrammarError(message: string): boolean;