@prometheus-ai/agent-core 0.5.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 (61) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +473 -0
  3. package/dist/types/agent-loop.d.ts +55 -0
  4. package/dist/types/agent.d.ts +331 -0
  5. package/dist/types/append-only-context.d.ts +113 -0
  6. package/dist/types/compaction/branch-summarization.d.ts +94 -0
  7. package/dist/types/compaction/compaction.d.ts +183 -0
  8. package/dist/types/compaction/entries.d.ts +103 -0
  9. package/dist/types/compaction/errors.d.ts +26 -0
  10. package/dist/types/compaction/index.d.ts +12 -0
  11. package/dist/types/compaction/messages.d.ts +61 -0
  12. package/dist/types/compaction/openai.d.ts +58 -0
  13. package/dist/types/compaction/pruning.d.ts +19 -0
  14. package/dist/types/compaction/shake.d.ts +82 -0
  15. package/dist/types/compaction/tool-protection.d.ts +17 -0
  16. package/dist/types/compaction/utils.d.ts +32 -0
  17. package/dist/types/compaction.d.ts +1 -0
  18. package/dist/types/harmony-leak.d.ts +118 -0
  19. package/dist/types/index.d.ts +11 -0
  20. package/dist/types/proxy.d.ts +84 -0
  21. package/dist/types/run-collector.d.ts +196 -0
  22. package/dist/types/telemetry.d.ts +588 -0
  23. package/dist/types/thinking.d.ts +17 -0
  24. package/dist/types/types.d.ts +443 -0
  25. package/dist/types/utils/yield.d.ts +52 -0
  26. package/package.json +75 -0
  27. package/src/agent-loop.ts +1418 -0
  28. package/src/agent.ts +1236 -0
  29. package/src/append-only-context.ts +297 -0
  30. package/src/compaction/branch-summarization.ts +339 -0
  31. package/src/compaction/compaction.ts +1155 -0
  32. package/src/compaction/entries.ts +133 -0
  33. package/src/compaction/errors.ts +31 -0
  34. package/src/compaction/index.ts +13 -0
  35. package/src/compaction/messages.ts +212 -0
  36. package/src/compaction/openai.ts +552 -0
  37. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  38. package/src/compaction/prompts/branch-summary-context.md +5 -0
  39. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  40. package/src/compaction/prompts/branch-summary.md +30 -0
  41. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  42. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  43. package/src/compaction/prompts/compaction-summary.md +38 -0
  44. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  45. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  46. package/src/compaction/prompts/file-operations.md +10 -0
  47. package/src/compaction/prompts/handoff-document.md +49 -0
  48. package/src/compaction/prompts/summarization-system.md +3 -0
  49. package/src/compaction/pruning.ts +99 -0
  50. package/src/compaction/shake.ts +406 -0
  51. package/src/compaction/tool-protection.ts +55 -0
  52. package/src/compaction/utils.ts +185 -0
  53. package/src/compaction.ts +1 -0
  54. package/src/harmony-leak.ts +456 -0
  55. package/src/index.ts +21 -0
  56. package/src/proxy.ts +326 -0
  57. package/src/run-collector.ts +631 -0
  58. package/src/telemetry.ts +2020 -0
  59. package/src/thinking.ts +19 -0
  60. package/src/types.ts +505 -0
  61. package/src/utils/yield.ts +146 -0
@@ -0,0 +1,443 @@
1
+ import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Effort, ImageContent, Message, Model, SimpleStreamOptions, Static, streamSimple, TextContent, Tool, ToolChoice, ToolResultMessage, TSchema } from "@prometheus-ai/ai";
2
+ import type { AppendOnlyContextManager } from "./append-only-context";
3
+ import type { HarmonyAuditEvent } from "./harmony-leak";
4
+ import type { AgentRunCoverage, AgentRunSummary } from "./run-collector";
5
+ import type { AgentTelemetryConfig } from "./telemetry";
6
+ /** Stream function - can return sync or Promise for async config lookup */
7
+ export type StreamFn = (...args: Parameters<typeof streamSimple>) => AssistantMessageEventStream | Promise<AssistantMessageEventStream>;
8
+ /**
9
+ * Configuration for the agent loop.
10
+ */
11
+ export interface AgentLoopConfig extends SimpleStreamOptions {
12
+ model: Model;
13
+ /**
14
+ * When to interrupt tool execution for steering messages.
15
+ * - "immediate" = check after each tool call (default)
16
+ * - "wait" = defer steering until the current turn completes
17
+ */
18
+ interruptMode?: "immediate" | "wait";
19
+ /**
20
+ * Maximum completed tool calls to accept from one streamed assistant turn before
21
+ * cutting the provider stream and executing that batch. The cap is enforced on
22
+ * `toolcall_end` so every executed call has complete arguments. Undefined disables
23
+ * batching.
24
+ */
25
+ maxToolCallsPerTurn?: number;
26
+ /**
27
+ * Optional session identifier forwarded to LLM providers.
28
+ * Used by providers that support session-based caching (e.g., OpenAI Codex).
29
+ */
30
+ sessionId?: string;
31
+ /**
32
+ * Optional resolver called per LLM request to produce request metadata.
33
+ * When set, the agent loop evaluates it **after** `getApiKey` resolves the
34
+ * session-sticky credential, ensuring the metadata's `account_uuid` reflects
35
+ * the credential actually used for the request (not the credential that was
36
+ * current when `AgentLoopConfig` was first constructed). Overrides the static
37
+ * `metadata` field when present.
38
+ */
39
+ metadataResolver?: (provider: string) => Record<string, unknown> | undefined;
40
+ /**
41
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
42
+ *
43
+ * Each AgentMessage must be converted to a UserMessage, AssistantMessage, or ToolResultMessage
44
+ * that the LLM can understand. AgentMessages that cannot be converted (e.g., UI-only notifications,
45
+ * status messages) should be filtered out.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * convertToLlm: (messages) => messages.flatMap(m => {
50
+ * if (m.role === "custom") {
51
+ * // Convert custom message to user message
52
+ * return [{ role: "user", content: m.content, timestamp: m.timestamp }];
53
+ * }
54
+ * if (m.role === "notification") {
55
+ * // Filter out UI-only messages
56
+ * return [];
57
+ * }
58
+ * // Pass through standard LLM messages
59
+ * return [m];
60
+ * })
61
+ * ```
62
+ */
63
+ convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
64
+ /**
65
+ * Optional transform applied to the context before `convertToLlm`.
66
+ *
67
+ * Use this for operations that work at the AgentMessage level:
68
+ * - Context window management (pruning old messages)
69
+ * - Injecting context from external sources
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * transformContext: async (messages) => {
74
+ * if (estimateTokens(messages) > MAX_TOKENS) {
75
+ * return pruneOldMessages(messages);
76
+ * }
77
+ * return messages;
78
+ * }
79
+ * ```
80
+ */
81
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal) => Promise<AgentMessage[]>;
82
+ /**
83
+ * Resolves an API key dynamically for each LLM call.
84
+ *
85
+ * Useful for short-lived OAuth tokens (e.g., GitHub Copilot) that may expire
86
+ * during long-running tool execution phases.
87
+ */
88
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
89
+ /**
90
+ * Returns steering messages to inject into the conversation mid-run.
91
+ *
92
+ * Called after each tool execution to check for user interruptions unless interruptMode is "wait".
93
+ * If messages are returned, remaining tool calls are skipped and
94
+ * these messages are added to the context before the next LLM call.
95
+ */
96
+ getSteeringMessages?: () => Promise<AgentMessage[]>;
97
+ /**
98
+ * Returns follow-up messages to process after the agent would otherwise stop.
99
+ *
100
+ * Called when the agent has no more tool calls and no steering messages.
101
+ * If messages are returned, they're added to the context and the agent
102
+ * continues with another turn.
103
+ */
104
+ getFollowUpMessages?: () => Promise<AgentMessage[]>;
105
+ /**
106
+ * Hook fired right before the loop would exit.
107
+ *
108
+ * Called when the agent has no more tool calls and no steering messages,
109
+ * immediately before polling follow-up messages.
110
+ */
111
+ onBeforeYield?: () => Promise<void> | void;
112
+ /**
113
+ * Provides tool execution context, resolved per tool call.
114
+ * Use for late-bound UI or session state access.
115
+ */
116
+ getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
117
+ /**
118
+ * Refreshes prompt/tool context from live session state before each model call.
119
+ * Use this when tool availability or the system prompt can change mid-turn.
120
+ */
121
+ syncContextBeforeModelCall?: (context: AgentContext) => void | Promise<void>;
122
+ /**
123
+ * Optional transform applied to tool call arguments before execution.
124
+ * Use for deobfuscating secrets or rewriting arguments.
125
+ */
126
+ transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
127
+ /**
128
+ * Enable intent tracing for tool calls.
129
+ * When enabled, the harness injects a `string` field into tool schemas sent to the model,
130
+ * then strips from arguments before executing tools.
131
+ */
132
+ intentTracing?: boolean;
133
+ /**
134
+ * Append-only context mode — stabilizes system prompt + tool spec bytes
135
+ * across turns so provider prefix caches hit at maximum rate.
136
+ *
137
+ * When set, the loop reads messages from the append-only log (stable
138
+ * byte prefix) and caches system prompt + tools. Tools exclude per-turn
139
+ * `_i` intent fields.
140
+ */
141
+ appendOnlyContext?: AppendOnlyContextManager;
142
+ /**
143
+ * Inspect assistant streaming events before they are published to the outer agent event stream.
144
+ * Callers may abort synchronously to stop consuming buffered provider events.
145
+ */
146
+ onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
147
+ /**
148
+ * Called when GPT-5 Harmony protocol leakage is detected and mitigated.
149
+ */
150
+ onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
151
+ /**
152
+ * Dynamic tool choice override, resolved per LLM call.
153
+ * When set and returns a value, overrides the static `toolChoice`.
154
+ */
155
+ getToolChoice?: () => ToolChoice | undefined;
156
+ /**
157
+ * Dynamic reasoning effort override, resolved per LLM call.
158
+ * When set and returns a value, overrides the static `reasoning` captured
159
+ * at run-loop start. Use this so mid-run thinking-level changes apply on
160
+ * the next model call instead of waiting for the next prompt.
161
+ */
162
+ getReasoning?: () => Effort | undefined;
163
+ /**
164
+ * Called after a tool call has been validated and is about to execute.
165
+ *
166
+ * Return `{ block: true }` to prevent execution. The loop emits an error tool
167
+ * result instead (using `reason` as the error text, or a default if omitted).
168
+ *
169
+ * Mutating `context.args` in place changes the arguments passed to `tool.execute`
170
+ * — the loop does **not** re-validate after this hook runs.
171
+ *
172
+ * The hook receives the tool abort signal (`signal`) and is responsible for
173
+ * honoring it. Throwing surfaces as a tool-error result and does not abort the
174
+ * rest of the batch.
175
+ */
176
+ beforeToolCall?: (context: BeforeToolCallContext, signal?: AbortSignal) => Promise<BeforeToolCallResult | undefined> | BeforeToolCallResult | undefined;
177
+ /**
178
+ * Called after a tool finishes executing, before `tool_execution_end` and the
179
+ * tool-result message are emitted.
180
+ *
181
+ * Return an `AfterToolCallResult` to override individual fields of the executed
182
+ * tool result. Omitted fields keep their original values; there is no deep merge.
183
+ *
184
+ * Throwing surfaces as a tool-error result and does not abort the rest of the batch.
185
+ */
186
+ afterToolCall?: (context: AfterToolCallContext, signal?: AbortSignal) => Promise<AfterToolCallResult | undefined> | AfterToolCallResult | undefined;
187
+ /**
188
+ * Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
189
+ * GenAI-semantic-convention spans (`invoke_agent`, `chat`, `execute_tool`)
190
+ * using the global tracer provider. Leaving this field undefined disables
191
+ * the instrumentation entirely — the loop performs zero tracer lookups.
192
+ *
193
+ * See {@link AgentTelemetryConfig} for the full surface (hooks, content
194
+ * capture, cost estimator, agent identity).
195
+ */
196
+ telemetry?: AgentTelemetryConfig;
197
+ }
198
+ /**
199
+ * Batch/sequencing metadata for the tool call currently being processed.
200
+ */
201
+ export interface ToolCallContext {
202
+ batchId: string;
203
+ index: number;
204
+ total: number;
205
+ toolCalls: Array<{
206
+ id: string;
207
+ name: string;
208
+ }>;
209
+ }
210
+ /** A single tool-call content block emitted by an assistant message. */
211
+ export type AgentToolCall = Extract<AssistantMessage["content"][number], {
212
+ type: "toolCall";
213
+ }>;
214
+ /**
215
+ * Result returned from `beforeToolCall`.
216
+ *
217
+ * Set `block: true` to prevent the tool from executing. The loop emits an error tool
218
+ * result instead, using `reason` as the error text (or a default if omitted).
219
+ *
220
+ * Mutating the `args` reference passed in `BeforeToolCallContext` is supported and
221
+ * survives into execution — the loop does **not** re-validate after this hook runs.
222
+ */
223
+ export interface BeforeToolCallResult {
224
+ block?: boolean;
225
+ reason?: string;
226
+ }
227
+ /**
228
+ * Partial override returned from `afterToolCall`.
229
+ *
230
+ * Merge semantics are field-by-field; omitted fields keep the executed values.
231
+ * No deep merge is performed.
232
+ */
233
+ export interface AfterToolCallResult {
234
+ /** If provided, replaces the tool result content array in full. */
235
+ content?: (TextContent | ImageContent)[];
236
+ /** If provided, replaces the tool result details payload in full. */
237
+ details?: unknown;
238
+ /** If provided, replaces the error flag carried with the tool result. */
239
+ isError?: boolean;
240
+ }
241
+ /** Context passed to `beforeToolCall`. */
242
+ export interface BeforeToolCallContext {
243
+ /** The assistant message that requested the tool call. */
244
+ assistantMessage: AssistantMessage;
245
+ /** The raw tool call block from `assistantMessage.content`. */
246
+ toolCall: AgentToolCall;
247
+ /**
248
+ * Validated tool arguments. The same reference is forwarded to `tool.execute`
249
+ * (after any `transformToolCallArguments` pass), so in-place mutations stick.
250
+ */
251
+ args: Record<string, unknown>;
252
+ /** Current agent context at the time the tool call is prepared. */
253
+ context: AgentContext;
254
+ }
255
+ /** Context passed to `afterToolCall`. */
256
+ export interface AfterToolCallContext {
257
+ /** The assistant message that requested the tool call. */
258
+ assistantMessage: AssistantMessage;
259
+ /** The raw tool call block from `assistantMessage.content`. */
260
+ toolCall: AgentToolCall;
261
+ /** Validated tool arguments used for execution (post `beforeToolCall` mutations). */
262
+ args: Record<string, unknown>;
263
+ /** The executed tool result before any `afterToolCall` overrides are applied. */
264
+ result: AgentToolResult<any>;
265
+ /** Whether the executed tool result is currently treated as an error. */
266
+ isError: boolean;
267
+ /** Current agent context at the time the tool call is finalized. */
268
+ context: AgentContext;
269
+ }
270
+ /**
271
+ * Extensible interface for custom app messages.
272
+ * Apps can extend via declaration merging:
273
+ *
274
+ * @example
275
+ * ```typescript
276
+ * declare module "@prometheus-ai/agent-core" {
277
+ * interface CustomAgentMessages {
278
+ * artifact: ArtifactMessage;
279
+ * notification: NotificationMessage;
280
+ * }
281
+ * }
282
+ * ```
283
+ */
284
+ export interface CustomAgentMessages {
285
+ }
286
+ /**
287
+ * AgentMessage: Union of LLM messages + custom messages.
288
+ * This abstraction allows apps to add custom message types while maintaining
289
+ * type safety and compatibility with the base LLM messages.
290
+ */
291
+ export type AgentMessage = Message | CustomAgentMessages[keyof CustomAgentMessages];
292
+ /**
293
+ * Agent state containing all configuration and conversation data.
294
+ */
295
+ export interface AgentState {
296
+ systemPrompt: string[];
297
+ model: Model;
298
+ thinkingLevel?: Effort;
299
+ tools: AgentTool<any>[];
300
+ messages: AgentMessage[];
301
+ isStreaming: boolean;
302
+ streamMessage: AgentMessage | null;
303
+ pendingToolCalls: Set<string>;
304
+ error?: string;
305
+ }
306
+ export interface AgentToolResult<T = any, _TInput = unknown> {
307
+ content: (TextContent | ImageContent)[];
308
+ details?: T;
309
+ isError?: boolean;
310
+ }
311
+ export type AgentToolUpdateCallback<T = any, TInput = unknown> = (partialResult: AgentToolResult<T, TInput>) => void;
312
+ /** Options passed to renderResult */
313
+ export interface RenderResultOptions {
314
+ /** Whether the result view is expanded */
315
+ expanded: boolean;
316
+ /** Whether this is a partial/streaming result */
317
+ isPartial: boolean;
318
+ /** Current spinner frame index for animated elements (optional) */
319
+ spinnerFrame?: number;
320
+ }
321
+ /** Capability tier a tool exercises. Determines which approval modes auto-approve it. */
322
+ export type ToolTier = "read" | "write" | "exec";
323
+ /**
324
+ * Per-tool approval declaration.
325
+ * - bare tier ("read" / "write" / "exec") — static classification.
326
+ * - object form — adds a `reason` (shown in the prompt) and/or `override: true`
327
+ * (force-prompt even in modes that would otherwise auto-approve this tier).
328
+ * - function — dynamic, given parsed args. Returns either form above.
329
+ *
330
+ * Omitted approvals are treated as "exec" by callers that enforce approvals.
331
+ */
332
+ export type ToolApprovalDecision = ToolTier | {
333
+ tier: ToolTier;
334
+ reason?: string;
335
+ override?: boolean;
336
+ };
337
+ export type ToolApproval = ToolApprovalDecision | ((args: unknown) => ToolApprovalDecision);
338
+ /**
339
+ * Context passed to tool execution.
340
+ * Apps can extend via declaration merging.
341
+ */
342
+ export interface AgentToolContext {
343
+ }
344
+ export type AgentToolExecFn<TParameters extends TSchema = TSchema, TDetails = any, TTheme = unknown> = (this: AgentTool<TParameters, TDetails, TTheme>, toolCallId: string, params: Static<TParameters>, signal?: AbortSignal, onUpdate?: AgentToolUpdateCallback<TDetails, TParameters>, context?: AgentToolContext) => Promise<AgentToolResult<TDetails, TParameters>>;
345
+ export interface AgentTool<TParameters extends TSchema = TSchema, TDetails = any, TTheme = unknown> extends Tool<TParameters> {
346
+ label: string;
347
+ /** If true, tool is excluded unless explicitly listed in --tools or agent's tools field */
348
+ hidden?: boolean;
349
+ /** If true, tool can stage a pending action that requires explicit resolution via the resolve tool. */
350
+ deferrable?: boolean;
351
+ /** Built-in tool loading behavior. "essential" loads initially; "discoverable" can be activated by tool search. */
352
+ loadMode?: "essential" | "discoverable";
353
+ /** Short one-line summary used for tool discovery indexes. */
354
+ summary?: string;
355
+ /** If true, tool execution ignores abort signals (runs to completion) */
356
+ nonAbortable?: boolean;
357
+ /**
358
+ * Concurrency mode for tool scheduling when multiple calls are in one turn.
359
+ * - "shared": can run alongside other shared tools (default)
360
+ * - "exclusive": runs alone; other tools wait until it finishes
361
+ */
362
+ concurrency?: "shared" | "exclusive";
363
+ /** If true, argument validation errors are non-fatal: raw args are passed to execute() instead of returning an error to the LLM. */
364
+ lenientArgValidation?: boolean;
365
+ /**
366
+ * Controls how the INTENT_FIELD (`_i`) is handled for this tool.
367
+ * - `"require"` (default): `_i` is injected and required in the parameter schema.
368
+ * - `"optional"`: `_i` is injected as an optional/nullable field.
369
+ * - `"omit"`: `_i` is NOT injected. Use for tools where intent is obvious (yield, resolve, todo, …).
370
+ * - function: `_i` is NOT injected; intent is derived dynamically from (potentially partial / streaming) args.
371
+ */
372
+ intent?: "omit" | "optional" | "require" | ((args: Partial<Static<TParameters>>) => string | undefined);
373
+ /**
374
+ * Normalize (potentially partial) streamed arguments into the plain text that
375
+ * stream-content matchers (e.g. TTSR rules) should inspect — the real content
376
+ * the call introduces, without wire grammar such as patch prefixes or JSON
377
+ * string escaping. Return `undefined` to fall back to raw argument-delta
378
+ * matching.
379
+ */
380
+ matcherDigest?: (args: unknown) => string | undefined;
381
+ /** Capability tier declaration used by approval gates. Omitted means "exec". */
382
+ approval?: ToolApproval;
383
+ /** Lines appended after the standard approval prompt header. */
384
+ formatApprovalDetails?: (args: unknown) => string | string[] | undefined;
385
+ /** The main execution callback for this tool. */
386
+ execute: AgentToolExecFn<TParameters, TDetails, TTheme>;
387
+ /** Optional custom rendering for tool call display (returns UI component) */
388
+ renderCall?: (args: Static<TParameters>, options: RenderResultOptions, theme: TTheme) => unknown;
389
+ /** Optional custom rendering for tool result display (returns UI component) */
390
+ renderResult?: (result: AgentToolResult<TDetails, TParameters>, options: RenderResultOptions, theme: TTheme) => unknown;
391
+ }
392
+ export interface AgentContext {
393
+ systemPrompt: string[];
394
+ messages: AgentMessage[];
395
+ tools?: AgentTool<any>[];
396
+ }
397
+ /**
398
+ * Events emitted by the Agent for UI updates.
399
+ * These events provide fine-grained lifecycle information for messages, turns, and tool executions.
400
+ */
401
+ export type AgentEvent = {
402
+ type: "agent_start";
403
+ } | {
404
+ type: "agent_end";
405
+ messages: AgentMessage[];
406
+ /** Present iff `AgentTelemetryConfig` was supplied on this run. */
407
+ telemetry?: AgentRunSummary;
408
+ coverage?: AgentRunCoverage;
409
+ } | {
410
+ type: "turn_start";
411
+ } | {
412
+ type: "turn_end";
413
+ message: AgentMessage;
414
+ toolResults: ToolResultMessage[];
415
+ } | {
416
+ type: "message_start";
417
+ message: AgentMessage;
418
+ } | {
419
+ type: "message_update";
420
+ message: AgentMessage;
421
+ assistantMessageEvent: AssistantMessageEvent;
422
+ } | {
423
+ type: "message_end";
424
+ message: AgentMessage;
425
+ } | {
426
+ type: "tool_execution_start";
427
+ toolCallId: string;
428
+ toolName: string;
429
+ args: any;
430
+ intent?: string;
431
+ } | {
432
+ type: "tool_execution_update";
433
+ toolCallId: string;
434
+ toolName: string;
435
+ args: any;
436
+ partialResult: any;
437
+ } | {
438
+ type: "tool_execution_end";
439
+ toolCallId: string;
440
+ toolName: string;
441
+ result: any;
442
+ isError?: boolean;
443
+ };
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Cooperative yield utility for preventing Bun event-loop busy-wait.
3
+ *
4
+ * ## Root Cause
5
+ *
6
+ * Bun 1.3.x (JavaScriptCore) event loop busy-waits (spins in userspace)
7
+ * when the only pending work is an unresolved Promise — even if there are
8
+ * active I/O watchers (stdin, child process pipes, etc.). The event loop
9
+ * continuously polls for microtask resolution instead of blocking in
10
+ * `epoll_wait`, consuming ~100% of a CPU core.
11
+ *
12
+ * This affects any `await` on a never-resolved Promise, including:
13
+ * - `Promise.withResolvers()` used for user input callbacks
14
+ * - `await proc.exited` for long-running child processes
15
+ * - Agent loop iterations waiting for the next tool call
16
+ *
17
+ * ## Fix
18
+ *
19
+ * A recurring `setInterval` keeps the event loop sleeping in `epoll_wait`.
20
+ * The `EventLoopKeepalive` class and `keepaliveWhile()` wrapper provide a
21
+ * clean way to install and clean up this keepalive timer.
22
+ *
23
+ * The older `yieldIfDue()` and `ExponentialYield` approaches (compensated
24
+ * sleep loops) are retained for the agent-loop hot-path where Promises
25
+ * resolve frequently and the keepalive alone is insufficient.
26
+ */
27
+ export declare class EventLoopKeepalive {
28
+ #private;
29
+ [Symbol.dispose](): void;
30
+ }
31
+ /**
32
+ * Yield to the Bun event loop, sleeping for at least 20 ms — but at most
33
+ * once every {@link YIELD_INTERVAL_MS}. Callers in hot paths can invoke
34
+ * this freely; only the slow path actually sleeps.
35
+ */
36
+ export declare function yieldIfDue(): Promise<void>;
37
+ export declare class ExponentialYield {
38
+ #private;
39
+ constructor(opts?: {
40
+ minMs?: number;
41
+ maxMs?: number;
42
+ multiplier?: number;
43
+ });
44
+ notifyActivity(): void;
45
+ sleep(signal?: AbortSignal): Promise<number>;
46
+ /**
47
+ * Race `racers` against an exponentially-backed-off cooperative yield.
48
+ * The losing sleep is cancelled as soon as a racer settles, so no stray
49
+ * timers keep the event loop alive past the racer's resolution.
50
+ */
51
+ race<T>(racers: Array<Promise<T>>): Promise<T>;
52
+ }
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "type": "module",
3
+ "name": "@prometheus-ai/agent-core",
4
+ "version": "0.5.0",
5
+ "description": "General-purpose agent with transport abstraction, state management, and attachment support",
6
+ "homepage": "https://prometheus.trivlab.com",
7
+ "author": "Uttam Trivedi",
8
+ "contributors": [
9
+ "Mario Zechner"
10
+ ],
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/uttamtrivedi/Prometheus.git",
15
+ "directory": "packages/agent"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/uttamtrivedi/Prometheus/issues"
19
+ },
20
+ "keywords": [
21
+ "ai",
22
+ "agent",
23
+ "llm",
24
+ "transport",
25
+ "state-management"
26
+ ],
27
+ "main": "./src/index.ts",
28
+ "types": "./dist/types/index.d.ts",
29
+ "scripts": {
30
+ "check": "biome check . && bun run check:types",
31
+ "check:types": "tsgo -p tsconfig.json --noEmit",
32
+ "lint": "biome lint .",
33
+ "test": "bun test --parallel",
34
+ "fix": "biome check --write --unsafe .",
35
+ "fmt": "biome format --write ."
36
+ },
37
+ "dependencies": {
38
+ "@prometheus-ai/ai": "0.5.0",
39
+ "@prometheus-ai/natives": "0.5.0",
40
+ "@prometheus-ai/utils": "0.5.0",
41
+ "@opentelemetry/api": "^1.9.1"
42
+ },
43
+ "devDependencies": {
44
+ "@opentelemetry/context-async-hooks": "^2.7.1",
45
+ "@opentelemetry/sdk-trace-base": "^2.7.1",
46
+ "@types/bun": "^1.3.14"
47
+ },
48
+ "engines": {
49
+ "bun": ">=1.3.14"
50
+ },
51
+ "files": [
52
+ "src",
53
+ "README.md",
54
+ "CHANGELOG.md",
55
+ "dist/types"
56
+ ],
57
+ "exports": {
58
+ ".": {
59
+ "types": "./dist/types/index.d.ts",
60
+ "import": "./src/index.ts"
61
+ },
62
+ "./compaction": {
63
+ "types": "./dist/types/compaction.d.ts",
64
+ "import": "./src/compaction.ts"
65
+ },
66
+ "./compaction/*": {
67
+ "types": "./dist/types/compaction/*.d.ts",
68
+ "import": "./src/compaction/*.ts"
69
+ },
70
+ "./*": {
71
+ "types": "./dist/types/*.d.ts",
72
+ "import": "./src/*.ts"
73
+ }
74
+ }
75
+ }