@anvia/core 0.16.0 → 0.18.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 (78) hide show
  1. package/dist/agent/index.d.ts +11 -11
  2. package/dist/agent/index.js +9 -7
  3. package/dist/{agent-BTiQ4RZn.d.ts → agent-CBz3CRxa.d.ts} +8 -7
  4. package/dist/audio-generation/index.d.ts +1 -1
  5. package/dist/{chunk-3PRP2DV4.js → chunk-3YJXLOGO.js} +5 -5
  6. package/dist/{chunk-PJCJ45RJ.js → chunk-7LCKIULT.js} +198 -16
  7. package/dist/chunk-7LCKIULT.js.map +1 -0
  8. package/dist/{chunk-A7AYX6BD.js → chunk-DPTZ2MK4.js} +12 -6
  9. package/dist/{chunk-A7AYX6BD.js.map → chunk-DPTZ2MK4.js.map} +1 -1
  10. package/dist/{chunk-K72JKDZ7.js → chunk-F56AMXMQ.js} +3 -132
  11. package/dist/chunk-F56AMXMQ.js.map +1 -0
  12. package/dist/{chunk-4PMAWIGM.js → chunk-JNJBKSLA.js} +2 -2
  13. package/dist/chunk-MTGNA4OS.js +138 -0
  14. package/dist/chunk-MTGNA4OS.js.map +1 -0
  15. package/dist/{chunk-R6QERZAB.js → chunk-PJDL5UT2.js} +52 -4
  16. package/dist/chunk-PJDL5UT2.js.map +1 -0
  17. package/dist/{chunk-3QG4SFH4.js → chunk-QABIBFKE.js} +4 -4
  18. package/dist/{chunk-IT426VXL.js → chunk-SWUH4G2I.js} +2 -2
  19. package/dist/chunk-TGLNXVII.js +220 -0
  20. package/dist/chunk-TGLNXVII.js.map +1 -0
  21. package/dist/chunk-XWUC7CIT.js +1 -0
  22. package/dist/chunk-XWUC7CIT.js.map +1 -0
  23. package/dist/chunk-ZNTIUOKL.js +33 -0
  24. package/dist/chunk-ZNTIUOKL.js.map +1 -0
  25. package/dist/completion/index.d.ts +3 -3
  26. package/dist/completion/index.js +7 -5
  27. package/dist/errors-Bf661V89.d.ts +17 -0
  28. package/dist/evals/index.d.ts +8 -8
  29. package/dist/evals/index.js +10 -8
  30. package/dist/evals/index.js.map +1 -1
  31. package/dist/extractor/index.d.ts +8 -8
  32. package/dist/extractor/index.js +10 -8
  33. package/dist/guardrails/index.d.ts +1 -1
  34. package/dist/hooks/index.d.ts +3 -3
  35. package/dist/image-generation/index.d.ts +1 -1
  36. package/dist/{index-Du1vCw6J.d.ts → index-XTUEMWhU.d.ts} +16 -8
  37. package/dist/index.d.ts +13 -12
  38. package/dist/index.js +20 -8
  39. package/dist/internal/agent.d.ts +8 -8
  40. package/dist/internal/agent.js +7 -5
  41. package/dist/{json-D_Ijo4lJ.d.ts → json-D9IvOgx_.d.ts} +1 -1
  42. package/dist/loaders/index.d.ts +1 -1
  43. package/dist/mcp/index.d.ts +4 -4
  44. package/dist/memory/index.d.ts +4 -3
  45. package/dist/memory/index.js +15 -1
  46. package/dist/{middleware-CsxdqqjJ.d.ts → middleware-Cp0tm6zE.d.ts} +3 -3
  47. package/dist/observability/index.d.ts +3 -3
  48. package/dist/observability/index.js.map +1 -1
  49. package/dist/pipeline/index.d.ts +8 -8
  50. package/dist/request/index.d.ts +8 -8
  51. package/dist/request/index.js +6 -4
  52. package/dist/skills/index.d.ts +4 -4
  53. package/dist/skills/index.js +2 -2
  54. package/dist/{think-tool-BTFFKn1m.d.ts → think-tool-CpCKo8Q8.d.ts} +1 -1
  55. package/dist/tool/index.d.ts +5 -5
  56. package/dist/tool/index.js +2 -2
  57. package/dist/{tool-sKozRXMI.d.ts → tool-Dhwg__1L.d.ts} +1 -1
  58. package/dist/transcription/index.d.ts +1 -1
  59. package/dist/{types-yf-Exga_.d.ts → types-BBEf34DP.d.ts} +2 -2
  60. package/dist/{types-CHFrSm-3.d.ts → types-BCA8p0sb.d.ts} +1 -1
  61. package/dist/types-DBHzPsbx.d.ts +119 -0
  62. package/dist/{types-BkTDw00d.d.ts → types-DMGvEFRf.d.ts} +1 -1
  63. package/dist/{types-nybevqL9.d.ts → types-XjIh933x.d.ts} +9 -1
  64. package/dist/{types-BT1n5oU4.d.ts → types-fBv8mhvP.d.ts} +1 -1
  65. package/dist/ui/index.d.ts +1 -1
  66. package/dist/ui/index.js +2 -2
  67. package/dist/vector-store/index.d.ts +4 -4
  68. package/package.json +1 -1
  69. package/dist/chunk-K72JKDZ7.js.map +0 -1
  70. package/dist/chunk-M6RPFHG7.js +0 -11
  71. package/dist/chunk-M6RPFHG7.js.map +0 -1
  72. package/dist/chunk-PJCJ45RJ.js.map +0 -1
  73. package/dist/chunk-R6QERZAB.js.map +0 -1
  74. package/dist/types-DMQ8JEf8.d.ts +0 -72
  75. /package/dist/{chunk-3PRP2DV4.js.map → chunk-3YJXLOGO.js.map} +0 -0
  76. /package/dist/{chunk-4PMAWIGM.js.map → chunk-JNJBKSLA.js.map} +0 -0
  77. /package/dist/{chunk-3QG4SFH4.js.map → chunk-QABIBFKE.js.map} +0 -0
  78. /package/dist/{chunk-IT426VXL.js.map → chunk-SWUH4G2I.js.map} +0 -0
@@ -1,16 +1,16 @@
1
- import { D as DynamicContextOptions, b as DynamicToolOptions, c as AgentEventStore, d as AgentEventStoreOptions, A as Agent } from '../agent-BTiQ4RZn.js';
2
- export { e as AgentEventAppendInput, f as AgentEventRecord, g as AgentEventStoreInclude, h as AgentSession, i as AgentToolOptions } from '../agent-BTiQ4RZn.js';
1
+ import { D as DynamicContextOptions, b as DynamicToolOptions, c as AgentEventStore, d as AgentEventStoreOptions, A as Agent } from '../agent-CBz3CRxa.js';
2
+ export { e as AgentEventAppendInput, f as AgentEventRecord, g as AgentEventStoreInclude, h as AgentSession, i as AgentToolOptions } from '../agent-CBz3CRxa.js';
3
3
  import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
4
- import { C as CompletionModel, P as ProviderTool, i as JsonValue, z as ToolChoice } from '../types-nybevqL9.js';
4
+ import { C as CompletionModel, P as ProviderTool, i as JsonValue, z as ToolChoice } from '../types-XjIh933x.js';
5
5
  import { GuardrailPolicyInput } from '../guardrails/index.js';
6
- import { P as PromptHook } from '../types-CHFrSm-3.js';
7
- import { a as McpServer } from '../types-yf-Exga_.js';
8
- import { e as MemoryStore, f as MemoryOptions } from '../types-DMQ8JEf8.js';
9
- import { i as AgentObserver, O as ObserveOptions } from '../index-Du1vCw6J.js';
10
- import { a as SkillSet } from '../types-BT1n5oU4.js';
11
- import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-CsxdqqjJ.js';
12
- import { A as AnyTool, e as ToolApprovalsOptions } from '../tool-sKozRXMI.js';
13
- import { a as VectorSearchIndex } from '../types-BkTDw00d.js';
6
+ import { P as PromptHook } from '../types-BCA8p0sb.js';
7
+ import { a as McpServer } from '../types-BBEf34DP.js';
8
+ import { m as MemoryStore, n as MemoryOptions } from '../types-DBHzPsbx.js';
9
+ import { i as AgentObserver, O as ObserveOptions } from '../index-XTUEMWhU.js';
10
+ import { a as SkillSet } from '../types-fBv8mhvP.js';
11
+ import { k as ToolSearchDocument, l as ToolSet, A as AgentMiddleware, e as ToolMiddleware } from '../middleware-Cp0tm6zE.js';
12
+ import { A as AnyTool, e as ToolApprovalsOptions } from '../tool-Dhwg__1L.js';
13
+ import { a as VectorSearchIndex } from '../types-DMGvEFRf.js';
14
14
  import 'zod';
15
15
  import '@modelcontextprotocol/sdk/client/sse.js';
16
16
  import '@modelcontextprotocol/sdk/client/stdio.js';
@@ -1,16 +1,18 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "../chunk-A7AYX6BD.js";
4
- import "../chunk-3QG4SFH4.js";
5
- import "../chunk-PJCJ45RJ.js";
3
+ } from "../chunk-DPTZ2MK4.js";
4
+ import "../chunk-QABIBFKE.js";
5
+ import "../chunk-7LCKIULT.js";
6
6
  import "../chunk-YK4WAAS4.js";
7
7
  import "../chunk-XUUY2L2D.js";
8
- import "../chunk-4PMAWIGM.js";
8
+ import "../chunk-JNJBKSLA.js";
9
9
  import "../chunk-I7TM7TKX.js";
10
10
  import "../chunk-2ODTMRHP.js";
11
- import "../chunk-M6RPFHG7.js";
12
- import "../chunk-K72JKDZ7.js";
13
- import "../chunk-R6QERZAB.js";
11
+ import "../chunk-ZNTIUOKL.js";
12
+ import "../chunk-TGLNXVII.js";
13
+ import "../chunk-MTGNA4OS.js";
14
+ import "../chunk-F56AMXMQ.js";
15
+ import "../chunk-PJDL5UT2.js";
14
16
  import "../chunk-WQKHFADH.js";
15
17
  import "../chunk-YBBD4HZ3.js";
16
18
  import "../chunk-OIMLU4SF.js";
@@ -1,11 +1,11 @@
1
- import { C as CompletionModel, M as Message, D as Document, i as JsonValue, P as ProviderTool, z as ToolChoice, J as JsonObject } from './types-nybevqL9.js';
1
+ import { C as CompletionModel, M as Message, D as Document, i as JsonValue, P as ProviderTool, z as ToolChoice, J as JsonObject } from './types-XjIh933x.js';
2
2
  import { GuardrailPolicyInput, GuardrailPolicy } from './guardrails/index.js';
3
- import { P as PromptHook } from './types-CHFrSm-3.js';
4
- import { h as MemoryContext, j as MemoryRegistration, S as SessionOptions } from './types-DMQ8JEf8.js';
5
- import { j as AgentTraceOptions, P as PromptResponse, d as AgentStreamEvent, f as AgentStreamEventWithoutToolCallDeltas, g as AgentStreamOptions, k as AgentObserverRegistration } from './index-Du1vCw6J.js';
6
- import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-sKozRXMI.js';
7
- import { A as AgentMiddleware, e as ToolMiddleware, k as ToolSearchDocument, l as ToolSet } from './middleware-CsxdqqjJ.js';
8
- import { V as VectorFilter, c as VectorSearchResult, a as VectorSearchIndex } from './types-BkTDw00d.js';
3
+ import { P as PromptHook } from './types-BCA8p0sb.js';
4
+ import { q as MemoryContext, s as MemoryRegistration, u as SessionOptions } from './types-DBHzPsbx.js';
5
+ import { j as AgentTraceOptions, P as PromptResponse, d as AgentStreamEvent, f as AgentStreamEventWithoutToolCallDeltas, g as AgentStreamOptions, k as AgentObserverRegistration } from './index-XTUEMWhU.js';
6
+ import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContext, N as NormalizedToolOutput } from './tool-Dhwg__1L.js';
7
+ import { A as AgentMiddleware, e as ToolMiddleware, k as ToolSearchDocument, l as ToolSet } from './middleware-Cp0tm6zE.js';
8
+ import { V as VectorFilter, c as VectorSearchResult, a as VectorSearchIndex } from './types-DMGvEFRf.js';
9
9
 
10
10
  type CompletionRetryContext = {
11
11
  error: unknown;
@@ -83,6 +83,7 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
83
83
  private executeToolCalls;
84
84
  private runOutputGuardrailsForResponse;
85
85
  private recordGuardrailDecision;
86
+ private recordMemoryCompaction;
86
87
  private guardrailRunContext;
87
88
  private startRunObservers;
88
89
  private recordAgentEvent;
@@ -1,4 +1,4 @@
1
- import { i as JsonValue } from '../types-nybevqL9.js';
1
+ import { i as JsonValue } from '../types-XjIh933x.js';
2
2
 
3
3
  type AudioGenerationRequest = {
4
4
  text: string;
@@ -1,20 +1,20 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "./chunk-A7AYX6BD.js";
3
+ } from "./chunk-DPTZ2MK4.js";
4
4
  import {
5
5
  extractRagText
6
- } from "./chunk-PJCJ45RJ.js";
6
+ } from "./chunk-7LCKIULT.js";
7
7
  import {
8
8
  createTool
9
9
  } from "./chunk-I7TM7TKX.js";
10
10
  import {
11
11
  CompletionRequestBuilder
12
- } from "./chunk-K72JKDZ7.js";
12
+ } from "./chunk-MTGNA4OS.js";
13
13
  import {
14
14
  CompletionCapabilityError,
15
15
  Message,
16
16
  Usage
17
- } from "./chunk-R6QERZAB.js";
17
+ } from "./chunk-PJDL5UT2.js";
18
18
 
19
19
  // src/extractor/extractor.ts
20
20
  var SUBMIT_TOOL_NAME = "submit";
@@ -138,4 +138,4 @@ export {
138
138
  Extractor,
139
139
  ExtractorBuilder
140
140
  };
141
- //# sourceMappingURL=chunk-3PRP2DV4.js.map
141
+ //# sourceMappingURL=chunk-3YJXLOGO.js.map
@@ -4,15 +4,23 @@ import {
4
4
  import {
5
5
  parseToolArgs,
6
6
  toolResultContentToText
7
- } from "./chunk-4PMAWIGM.js";
7
+ } from "./chunk-JNJBKSLA.js";
8
8
  import {
9
9
  runControl,
10
10
  toolCallControl
11
11
  } from "./chunk-2ODTMRHP.js";
12
12
  import {
13
- CompletionRequestBuilder,
13
+ MemoryCompactionConflictError,
14
+ MemoryCompactionError,
15
+ createMemoryCompactionSummary,
16
+ cumulativeCompactedMessageCount
17
+ } from "./chunk-TGLNXVII.js";
18
+ import {
19
+ CompletionRequestBuilder
20
+ } from "./chunk-MTGNA4OS.js";
21
+ import {
14
22
  isStreamingCompletionModel
15
- } from "./chunk-K72JKDZ7.js";
23
+ } from "./chunk-F56AMXMQ.js";
16
24
  import {
17
25
  AssistantContent,
18
26
  Message,
@@ -21,7 +29,7 @@ import {
21
29
  assertCompletionRequestSupported,
22
30
  getAssistantGenerationMetadata,
23
31
  textFromAssistantContent
24
- } from "./chunk-R6QERZAB.js";
32
+ } from "./chunk-PJDL5UT2.js";
25
33
  import {
26
34
  mapWithConcurrency
27
35
  } from "./chunk-OIMLU4SF.js";
@@ -151,9 +159,13 @@ var PromptRequestMemory = class {
151
159
  async prepareRun(runId, newMessages) {
152
160
  const memory = this.memory();
153
161
  if (memory === void 0 || this.memoryContext === void 0) {
154
- return this.initialHistory;
162
+ return {
163
+ history: this.initialHistory,
164
+ usage: Usage.empty()
165
+ };
155
166
  }
156
- const memoryHistory = await memory.store.load(this.memoryContext);
167
+ const preparation = await this.prepareStoredHistory(memory, runId, newMessages.length);
168
+ const memoryHistory = preparation.history;
157
169
  const chatHistory = [...memoryHistory, ...this.initialHistory];
158
170
  if (memory.options.savePolicy === "message") {
159
171
  await memory.store.append({
@@ -163,7 +175,10 @@ var PromptRequestMemory = class {
163
175
  messages: newMessages
164
176
  });
165
177
  }
166
- return chatHistory;
178
+ return {
179
+ ...preparation,
180
+ history: chatHistory
181
+ };
167
182
  }
168
183
  async commitMessages(runId, turn, messages, pendingTurnMessages) {
169
184
  const memory = this.memory();
@@ -222,7 +237,119 @@ var PromptRequestMemory = class {
222
237
  memory() {
223
238
  return this.memoryContext === void 0 ? void 0 : this.agent.memory;
224
239
  }
240
+ async prepareStoredHistory(memory, runId, incomingMessageCount) {
241
+ const context = this.memoryContext;
242
+ if (context === void 0) {
243
+ return { history: [], usage: Usage.empty() };
244
+ }
245
+ const options = memory.options.compaction;
246
+ const capability = memory.store.compaction;
247
+ if (options === void 0 || capability === void 0) {
248
+ return {
249
+ history: await memory.store.load(context),
250
+ usage: Usage.empty()
251
+ };
252
+ }
253
+ let usage = Usage.empty();
254
+ let conflictRetries = 0;
255
+ for (let attempt = 0; attempt <= options.conflictRetries; attempt += 1) {
256
+ const snapshot = await capability.load(context);
257
+ const compactedMessageCount = compactedPrefixLength(
258
+ snapshot.messages,
259
+ incomingMessageCount,
260
+ options.maxMessages,
261
+ options.keepRecentUserTurns
262
+ );
263
+ if (compactedMessageCount === 0) {
264
+ return { history: snapshot.messages, usage };
265
+ }
266
+ const prefix = snapshot.messages.slice(0, compactedMessageCount);
267
+ let result;
268
+ try {
269
+ result = await options.compactor({
270
+ context,
271
+ messages: prefix
272
+ });
273
+ } catch (error) {
274
+ throw remappedCompactorError(error, usage);
275
+ }
276
+ if (result.usage !== void 0) {
277
+ usage = Usage.add(usage, result.usage);
278
+ }
279
+ const summaryText = result.summary.trim();
280
+ if (summaryText.length === 0) {
281
+ throw new MemoryCompactionError("Memory compactor returned an empty summary.", {
282
+ usage
283
+ });
284
+ }
285
+ const summary = createMemoryCompactionSummary(
286
+ summaryText,
287
+ cumulativeCompactedMessageCount(prefix)
288
+ );
289
+ let commit;
290
+ try {
291
+ commit = await capability.commit({
292
+ context,
293
+ revision: snapshot.revision,
294
+ compactedMessageCount,
295
+ summary,
296
+ runId: `memory-compaction:${runId}:${attempt + 1}`
297
+ });
298
+ } catch (error) {
299
+ throw new MemoryCompactionError("Memory compaction store commit failed.", {
300
+ cause: error,
301
+ usage
302
+ });
303
+ }
304
+ if (commit === "committed") {
305
+ const retained = snapshot.messages.slice(compactedMessageCount);
306
+ return {
307
+ history: [summary, ...retained],
308
+ usage,
309
+ compaction: {
310
+ originalMessageCount: snapshot.messages.length,
311
+ compactedMessageCount,
312
+ retainedMessageCount: retained.length,
313
+ conflictRetries
314
+ }
315
+ };
316
+ }
317
+ conflictRetries += 1;
318
+ }
319
+ throw new MemoryCompactionConflictError(options.conflictRetries + 1, usage);
320
+ }
225
321
  };
322
+ function compactedPrefixLength(messages, incomingMessageCount, maxMessages, keepRecentUserTurns) {
323
+ if (messages.length + incomingMessageCount <= maxMessages) {
324
+ return 0;
325
+ }
326
+ const userMessageIndexes = messages.flatMap(
327
+ (message, index) => message.role === "user" ? [index] : []
328
+ );
329
+ if (userMessageIndexes.length <= keepRecentUserTurns) {
330
+ return 0;
331
+ }
332
+ return userMessageIndexes[userMessageIndexes.length - keepRecentUserTurns] ?? 0;
333
+ }
334
+ function remappedCompactorError(error, usage) {
335
+ if (error instanceof MemoryCompactionError) {
336
+ const mergedUsage = error.usage === void 0 ? usage : isEmptyUsage(usage) ? error.usage : Usage.add(usage, error.usage);
337
+ if (mergedUsage === error.usage || error.usage === void 0 && isEmptyUsage(usage)) {
338
+ return error;
339
+ }
340
+ return new MemoryCompactionError(error.message, {
341
+ cause: error,
342
+ usage: mergedUsage
343
+ });
344
+ }
345
+ return new MemoryCompactionError("Memory compactor failed.", {
346
+ cause: error,
347
+ usage
348
+ });
349
+ }
350
+ function isEmptyUsage(usage) {
351
+ return usage.inputTokens === 0 && usage.outputTokens === 0 && usage.totalTokens === 0 && usage.cachedInputTokens === 0 && usage.cacheCreationInputTokens === 0;
352
+ }
226
353
 
227
354
  // src/internal/prompt-runtime/retrieval.ts
228
355
  async function fetchDynamicContext(agent, ragText) {
@@ -1628,7 +1755,10 @@ var PromptRequest = class _PromptRequest {
1628
1755
  return result;
1629
1756
  }
1630
1757
  newMessages = [this.promptMessage];
1631
- this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
1758
+ const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
1759
+ this.chatHistory = memoryPreparation.history;
1760
+ usage = Usage.add(usage, memoryPreparation.usage);
1761
+ await this.recordMemoryCompaction(memoryPreparation, runObservers);
1632
1762
  const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
1633
1763
  await this.runRunStartHook(newMessages);
1634
1764
  while (currentTurns <= this.maxTurnCount + 1) {
@@ -1744,6 +1874,9 @@ var PromptRequest = class _PromptRequest {
1744
1874
  }
1745
1875
  throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
1746
1876
  } catch (error) {
1877
+ if (error instanceof MemoryCompactionError && error.usage !== void 0) {
1878
+ usage = Usage.add(usage, error.usage);
1879
+ }
1747
1880
  const finalError = await this.runRunErrorHook(error, usage, newMessages);
1748
1881
  this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
1749
1882
  await runObservers.error({ error: finalError, usage, messages: [...newMessages] });
@@ -1801,7 +1934,10 @@ var PromptRequest = class _PromptRequest {
1801
1934
  return;
1802
1935
  }
1803
1936
  newMessages = [this.promptMessage];
1804
- this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
1937
+ const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
1938
+ this.chatHistory = memoryPreparation.history;
1939
+ usage = Usage.add(usage, memoryPreparation.usage);
1940
+ await this.recordMemoryCompaction(memoryPreparation, runObservers);
1805
1941
  const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
1806
1942
  await this.runRunStartHook(newMessages);
1807
1943
  while (currentTurns <= this.maxTurnCount + 1) {
@@ -1830,11 +1966,20 @@ var PromptRequest = class _PromptRequest {
1830
1966
  );
1831
1967
  assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
1832
1968
  const providerRequest = this.providerTraceRequest(request, { stream: true });
1833
- const generationObservers = await runObservers.startGeneration(
1834
- this.generationStartArgs(currentTurns, request, providerRequest)
1969
+ const generationStartArgs = this.generationStartArgs(
1970
+ currentTurns,
1971
+ request,
1972
+ providerRequest
1835
1973
  );
1836
- const accumulator = new CompletionStreamAccumulator();
1974
+ const generationObservers = await runObservers.startGeneration(generationStartArgs);
1837
1975
  const generationStartedAt = Date.now();
1976
+ yield await emit({
1977
+ type: "generation_start",
1978
+ turn: currentTurns,
1979
+ request,
1980
+ modelInfo: generationStartArgs.modelInfo
1981
+ });
1982
+ const accumulator = new CompletionStreamAccumulator();
1838
1983
  let firstDeltaMs;
1839
1984
  const bufferResponseEvents = this.shouldBufferStreamResponseEvents();
1840
1985
  const emittedToolCallIds = /* @__PURE__ */ new Set();
@@ -1916,7 +2061,12 @@ var PromptRequest = class _PromptRequest {
1916
2061
  yield await emit(event);
1917
2062
  }
1918
2063
  }
1919
- yield await emit({ type: "turn_end", turn: currentTurns, response });
2064
+ yield await emit({
2065
+ type: "turn_end",
2066
+ turn: currentTurns,
2067
+ response,
2068
+ firstDeltaMs
2069
+ });
1920
2070
  emittedTurnEnd = true;
1921
2071
  }
1922
2072
  if (this.steeringMessages.length > 0) {
@@ -1960,7 +2110,12 @@ var PromptRequest = class _PromptRequest {
1960
2110
  }
1961
2111
  }
1962
2112
  if (!emittedTurnEnd) {
1963
- yield await emit({ type: "turn_end", turn: currentTurns, response });
2113
+ yield await emit({
2114
+ type: "turn_end",
2115
+ turn: currentTurns,
2116
+ response,
2117
+ firstDeltaMs
2118
+ });
1964
2119
  }
1965
2120
  const result = {
1966
2121
  output: guardedOutput.output,
@@ -2009,7 +2164,12 @@ var PromptRequest = class _PromptRequest {
2009
2164
  [assistantMessage],
2010
2165
  pendingTurnMessages
2011
2166
  );
2012
- yield await emit({ type: "turn_end", turn: currentTurns, response });
2167
+ yield await emit({
2168
+ type: "turn_end",
2169
+ turn: currentTurns,
2170
+ response,
2171
+ firstDeltaMs
2172
+ });
2013
2173
  const toolResultEvents = createAsyncQueue();
2014
2174
  const toolResultsPromise = this.executeToolCalls(
2015
2175
  runId,
@@ -2049,6 +2209,9 @@ var PromptRequest = class _PromptRequest {
2049
2209
  }
2050
2210
  throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
2051
2211
  } catch (error) {
2212
+ if (error instanceof MemoryCompactionError && error.usage !== void 0) {
2213
+ usage = Usage.add(usage, error.usage);
2214
+ }
2052
2215
  const finalError = await this.runRunErrorHook(error, usage, newMessages);
2053
2216
  this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
2054
2217
  const finalUsage = usage;
@@ -2220,6 +2383,24 @@ var PromptRequest = class _PromptRequest {
2220
2383
  attributes: guardrailDecisionAttributes(decision)
2221
2384
  });
2222
2385
  }
2386
+ async recordMemoryCompaction(preparation, runObservers) {
2387
+ const compaction = preparation.compaction;
2388
+ if (compaction === void 0) {
2389
+ return;
2390
+ }
2391
+ await runObservers.event({
2392
+ name: "memory.compaction",
2393
+ attributes: {
2394
+ originalMessageCount: compaction.originalMessageCount,
2395
+ compactedMessageCount: compaction.compactedMessageCount,
2396
+ retainedMessageCount: compaction.retainedMessageCount,
2397
+ conflictRetries: compaction.conflictRetries,
2398
+ inputTokens: preparation.usage.inputTokens,
2399
+ outputTokens: preparation.usage.outputTokens,
2400
+ totalTokens: preparation.usage.totalTokens
2401
+ }
2402
+ });
2403
+ }
2223
2404
  guardrailRunContext(runId) {
2224
2405
  const context = {
2225
2406
  agentId: this.agent.id,
@@ -2242,6 +2423,7 @@ var PromptRequest = class _PromptRequest {
2242
2423
  agentDescription: this.agent.description,
2243
2424
  instructions: this.agent.instructions,
2244
2425
  trace: this.traceOptions,
2426
+ promptRef: this.traceOptions?.promptRef,
2245
2427
  prompt: this.promptMessage,
2246
2428
  history: this.chatHistory,
2247
2429
  maxTurns: this.maxTurnCount
@@ -2568,4 +2750,4 @@ export {
2568
2750
  extractRagText,
2569
2751
  PromptRequest
2570
2752
  };
2571
- //# sourceMappingURL=chunk-PJCJ45RJ.js.map
2753
+ //# sourceMappingURL=chunk-7LCKIULT.js.map