@openviking/openclaw-plugin 2026.6.2 → 2026.6.17-dev.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 (108) hide show
  1. package/INSTALL-AGENT.md +4 -4
  2. package/INSTALL-ZH.md +68 -8
  3. package/INSTALL.md +12 -8
  4. package/README.md +45 -35
  5. package/adapters/http-transport.ts +3 -0
  6. package/adapters/resource-packager.ts +141 -0
  7. package/auto-recall.ts +258 -27
  8. package/client.ts +201 -177
  9. package/commands/setup.ts +200 -33
  10. package/config/feature-gates.json +10 -0
  11. package/config.ts +428 -85
  12. package/context-engine.ts +71 -1402
  13. package/dist/adapters/http-transport.js +1 -0
  14. package/dist/adapters/resource-packager.js +114 -0
  15. package/dist/auto-recall.js +201 -25
  16. package/dist/client.js +125 -161
  17. package/dist/commands/setup.js +170 -32
  18. package/dist/config.js +285 -64
  19. package/dist/context-engine.js +62 -1173
  20. package/dist/index.js +209 -1489
  21. package/dist/memory-ranking.js +35 -12
  22. package/dist/plugin/command-registration.js +5 -0
  23. package/dist/plugin/openviking-archive-tools.js +210 -0
  24. package/dist/plugin/openviking-bypass-runtime.js +8 -0
  25. package/dist/plugin/openviking-client-runtime.js +28 -0
  26. package/dist/plugin/openviking-command-args.js +146 -0
  27. package/dist/plugin/openviking-command-definitions.js +206 -0
  28. package/dist/plugin/openviking-context-engine-ref.js +11 -0
  29. package/dist/plugin/openviking-context-engine-registration.js +23 -0
  30. package/dist/plugin/openviking-feature-gates.js +250 -0
  31. package/dist/plugin/openviking-import-runtime.js +50 -0
  32. package/dist/plugin/openviking-import-tools.js +86 -0
  33. package/dist/plugin/openviking-lifecycle-hooks.js +33 -0
  34. package/dist/plugin/openviking-memory-recall-tools.js +233 -0
  35. package/dist/plugin/openviking-memory-tools.js +199 -0
  36. package/dist/plugin/openviking-query-config-command.js +203 -0
  37. package/dist/plugin/openviking-query-runtime.js +367 -0
  38. package/dist/plugin/openviking-query-tools.js +91 -0
  39. package/dist/plugin/openviking-recall-trace-runtime.js +387 -0
  40. package/dist/plugin/openviking-recall-trace-tools.js +38 -0
  41. package/dist/plugin/openviking-runtime-state.js +26 -0
  42. package/dist/plugin/openviking-runtime-utils.js +62 -0
  43. package/dist/plugin/openviking-services.js +20 -0
  44. package/dist/plugin/openviking-session-routing-runtime.js +61 -0
  45. package/dist/plugin/openviking-tool-result-tools.js +268 -0
  46. package/dist/plugin/recall-trace-routes.js +77 -0
  47. package/dist/plugin/tool-registration.js +23 -0
  48. package/dist/process-manager.js +1 -1
  49. package/dist/query-config.js +336 -0
  50. package/dist/recall-trace.js +309 -0
  51. package/dist/registries/recall-resource-types.js +58 -0
  52. package/dist/routing/identity-routing.js +152 -0
  53. package/dist/routing/memory-uri.js +7 -0
  54. package/dist/services/context-lifecycle-service.js +847 -0
  55. package/dist/services/context-message-adapter.js +340 -0
  56. package/dist/session-transcript-repair.js +0 -1
  57. package/dist/text-utils.js +14 -31
  58. package/index.ts +271 -1873
  59. package/install-manifest.json +10 -1
  60. package/memory-ranking.ts +46 -10
  61. package/openclaw.plugin.json +198 -36
  62. package/package.json +14 -4
  63. package/plugin/command-registration.ts +34 -0
  64. package/plugin/openviking-archive-tools.ts +301 -0
  65. package/plugin/openviking-bypass-runtime.ts +26 -0
  66. package/plugin/openviking-client-runtime.ts +74 -0
  67. package/plugin/openviking-command-args.ts +187 -0
  68. package/plugin/openviking-command-definitions.ts +294 -0
  69. package/plugin/openviking-context-engine-ref.ts +16 -0
  70. package/plugin/openviking-context-engine-registration.ts +97 -0
  71. package/plugin/openviking-feature-gates.ts +335 -0
  72. package/plugin/openviking-import-runtime.ts +105 -0
  73. package/plugin/openviking-import-tools.ts +133 -0
  74. package/plugin/openviking-lifecycle-hooks.ts +65 -0
  75. package/plugin/openviking-memory-recall-tools.ts +368 -0
  76. package/plugin/openviking-memory-tools.ts +299 -0
  77. package/plugin/openviking-query-config-command.ts +230 -0
  78. package/plugin/openviking-query-formatters.ts +108 -0
  79. package/plugin/openviking-query-runtime.ts +476 -0
  80. package/plugin/openviking-query-tools.ts +161 -0
  81. package/plugin/openviking-recall-trace-runtime.ts +482 -0
  82. package/plugin/openviking-recall-trace-tools.ts +99 -0
  83. package/plugin/openviking-runtime-state.ts +52 -0
  84. package/plugin/openviking-runtime-utils.ts +67 -0
  85. package/plugin/openviking-services.ts +49 -0
  86. package/plugin/openviking-session-routing-runtime.ts +105 -0
  87. package/plugin/openviking-tool-result-tools.ts +397 -0
  88. package/plugin/recall-trace-routes.ts +133 -0
  89. package/plugin/tool-registration.ts +51 -0
  90. package/process-manager.ts +1 -1
  91. package/query-config.ts +400 -0
  92. package/recall-trace.ts +442 -0
  93. package/registries/openviking-tools.ts +84 -0
  94. package/registries/recall-resource-types.ts +74 -0
  95. package/routing/identity-routing.ts +210 -0
  96. package/routing/memory-uri.ts +8 -0
  97. package/services/context-lifecycle-service.ts +1209 -0
  98. package/services/context-message-adapter.ts +375 -0
  99. package/services/setup/config-writer.ts +125 -0
  100. package/services/setup/exit-utils.ts +5 -0
  101. package/services/setup/package-metadata.ts +56 -0
  102. package/services/setup/probe-service.ts +143 -0
  103. package/services/setup/setup-flow.ts +306 -0
  104. package/services/setup/version-compatibility.ts +41 -0
  105. package/session-transcript-repair.ts +0 -2
  106. package/skills/install-openviking-memory/SKILL.md +123 -50
  107. package/skills/openviking-context-database/SKILL.md +286 -0
  108. package/text-utils.ts +15 -34
package/context-engine.ts CHANGED
@@ -1,36 +1,24 @@
1
- import { createHash } from "node:crypto";
2
- import { DEFAULT_PHASE2_POLL_TIMEOUT_MS } from "./client.js";
3
- import type { OpenVikingClient, OVMessage } from "./client.js";
1
+ import type { OpenVikingClient } from "./client.js";
4
2
  import type { MemoryOpenVikingConfig } from "./config.js";
3
+ import type { RuntimeQueryConfigStore } from "./query-config.js";
5
4
  import {
6
5
  AUTO_RECALL_SOURCE_MARKER,
7
- buildAutoRecallContext,
8
- prepareRecallQuery,
9
6
  } from "./auto-recall.js";
10
7
  import {
11
8
  compileSessionPatterns,
12
9
  getCaptureDecision,
13
- extractNewTurnMessages,
14
10
  shouldBypassSession,
15
11
  } from "./text-utils.js";
12
+ import type { RecallTraceEntry } from "./recall-trace.js";
13
+ import { estimateAgentMessageTokens, estimateAgentMessagesTokens } from "./token-estimator.js";
14
+ import { openClawSessionToOvStorageId } from "./routing/identity-routing.js";
15
+ import type { AgentMessage } from "./services/context-message-adapter.js";
16
16
  import {
17
- trimForLog,
18
- toJsonLog,
19
- } from "./memory-ranking.js";
20
- import { sanitizeToolUseResultPairing } from "./session-transcript-repair.js";
21
- import {
22
- estimateAgentMessageTokens,
23
- estimateAgentMessagesTokens,
24
- estimateTextTokens,
25
- } from "./token-estimator.js";
26
-
27
- type AgentMessage = {
28
- role?: string;
29
- content?: unknown;
30
- timestamp?: unknown;
31
- };
32
-
33
- type ExtractedTurnMessage = ReturnType<typeof extractNewTurnMessages>["messages"][number];
17
+ assembleOpenVikingSession,
18
+ afterTurnOpenVikingSession,
19
+ compactOpenVikingSession,
20
+ commitOpenVikingSession,
21
+ } from "./services/context-lifecycle-service.js";
34
22
 
35
23
  type ContextEngineInfo = {
36
24
  id: string;
@@ -49,18 +37,6 @@ type IngestResult = {
49
37
  ingested: boolean;
50
38
  };
51
39
 
52
- export function toRoleId(senderId: string | undefined): string | undefined {
53
- if (!senderId) {
54
- return undefined;
55
- }
56
- const normalized = senderId
57
- .trim()
58
- .replace(/[^a-zA-Z0-9_-]+/g, "_")
59
- .replace(/^_+|_+$/g, "")
60
- .replace(/_+/g, "_");
61
- return normalized || undefined;
62
- }
63
-
64
40
  type IngestBatchResult = {
65
41
  ingestedCount: number;
66
42
  };
@@ -133,33 +109,6 @@ type Logger = {
133
109
  error: (msg: string) => void;
134
110
  };
135
111
 
136
- type ExtractedSender = {
137
- found: boolean;
138
- senderId?: string;
139
- };
140
-
141
- interface ContextBudgets {
142
- archiveMemory: number;
143
- sessionContext: number;
144
- reserved: number;
145
- }
146
-
147
- const BUDGET_UNLIMITED = -1;
148
- const ARCHIVE_BUDGET_RATIO = 0.15;
149
- const ARCHIVE_BUDGET_CAP = 8_000;
150
- const RESERVED_MIN = 20_000;
151
- const RESERVED_RATIO = 0.15;
152
- const ARCHIVE_INDEX_TRIM_LIMIT = 10;
153
-
154
- function allocateContextBudget(totalBudget: number, instructionTokens = 0): ContextBudgets {
155
- const reserveFloor = totalBudget >= RESERVED_MIN * 2 ? RESERVED_MIN : 0;
156
- const reserved = Math.min(totalBudget, Math.max(totalBudget * RESERVED_RATIO, reserveFloor));
157
- const usableBudget = Math.max(totalBudget - reserved - instructionTokens, 0);
158
- const archiveMemory = Math.min(usableBudget * ARCHIVE_BUDGET_RATIO, ARCHIVE_BUDGET_CAP);
159
- const sessionContext = Math.max(usableBudget - archiveMemory, 0);
160
- return { archiveMemory, sessionContext, reserved };
161
- }
162
-
163
112
  function roughEstimate(messages: AgentMessage[]): number {
164
113
  return estimateAgentMessagesTokens(messages);
165
114
  }
@@ -168,29 +117,6 @@ function msgTokenEstimate(msg: AgentMessage): number {
168
117
  return estimateAgentMessageTokens(msg);
169
118
  }
170
119
 
171
- function normalizeTimestamp(value: unknown): string | undefined {
172
- if (typeof value === "number" && Number.isFinite(value)) {
173
- const timestampMs = Math.abs(value) < 100_000_000_000 ? value * 1000 : value;
174
- return new Date(timestampMs).toISOString();
175
- }
176
- return undefined;
177
- }
178
-
179
- function pickLatestCreatedAt(messages: AgentMessage[]): string | undefined {
180
- for (let i = messages.length - 1; i >= 0; i -= 1) {
181
- const message = messages[i] as Record<string, unknown>;
182
- const role = typeof message.role === "string" ? message.role : "";
183
- if (!role || role === "system") {
184
- continue;
185
- }
186
- const normalized = normalizeTimestamp(message.timestamp);
187
- if (normalized) {
188
- return normalized;
189
- }
190
- }
191
- return undefined;
192
- }
193
-
194
120
  function messageDigest(messages: AgentMessage[], maxCharsPerMsg = 2000): Array<{role: string; content: string; tokens: number; truncated: boolean}> {
195
121
  return messages.map((msg) => {
196
122
  const m = msg as Record<string, unknown>;
@@ -298,13 +224,6 @@ function emitDiag(log: Logger, stage: string, sessionId: string, data: Record<st
298
224
  log.info(`openviking: diag ${JSON.stringify({ ts: Date.now(), stage, sessionId, data })}`);
299
225
  }
300
226
 
301
- function totalExtractedMemories(memories?: Record<string, number>): number {
302
- if (!memories || typeof memories !== "object") {
303
- return 0;
304
- }
305
- return Object.values(memories).reduce((sum, count) => sum + (count ?? 0), 0);
306
- }
307
-
308
227
  function validTokenBudget(raw: unknown): number | undefined {
309
228
  if (typeof raw === "number" && Number.isFinite(raw) && raw > 0) {
310
229
  return raw;
@@ -312,567 +231,6 @@ function validTokenBudget(raw: unknown): number | undefined {
312
231
  return undefined;
313
232
  }
314
233
 
315
- function extractRuntimeSenderId(
316
- runtimeContext: Record<string, unknown> | undefined,
317
- ): ExtractedSender {
318
- if (runtimeContext) {
319
- const senderId = runtimeContext.senderId;
320
- if (typeof senderId === "string") {
321
- const trimmed = senderId.trim();
322
- if (trimmed) {
323
- return {
324
- found: true,
325
- senderId: trimmed,
326
- };
327
- }
328
- }
329
- }
330
- return { found: false };
331
- }
332
-
333
- /** OpenClaw session UUID (path-safe on Windows). */
334
- const OPENVIKING_OV_SESSION_UUID =
335
- /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
336
-
337
- const WINDOWS_BAD_SESSION_SEGMENT = /[:<>"\\/|?\u0000-\u001f]/;
338
-
339
- /**
340
- * Map OpenClaw session identity to an OpenViking session_id that is safe as a single
341
- * AGFS path segment on Windows (no `:` etc.). Prefer UUID sessionId when present;
342
- * otherwise derive a stable sha256 from sessionKey.
343
- */
344
- export function openClawSessionToOvStorageId(
345
- sessionId: string | undefined,
346
- sessionKey: string | undefined,
347
- ): string {
348
- const sid = typeof sessionId === "string" ? sessionId.trim() : "";
349
- const key = typeof sessionKey === "string" ? sessionKey.trim() : "";
350
-
351
- if (sid && OPENVIKING_OV_SESSION_UUID.test(sid)) {
352
- return sid.toLowerCase();
353
- }
354
- if (key) {
355
- return createHash("sha256").update(key, "utf8").digest("hex");
356
- }
357
- if (sid) {
358
- if (WINDOWS_BAD_SESSION_SEGMENT.test(sid)) {
359
- return createHash("sha256").update(`openclaw-session:${sid}`, "utf8").digest("hex");
360
- }
361
- return sid;
362
- }
363
- throw new Error("openviking: need sessionId or sessionKey for OV session path");
364
- }
365
-
366
- /** Normalize a hook/tool session ref (uuid, sessionKey, or already-safe id) for OV storage. */
367
- export function openClawSessionRefToOvStorageId(ref: string): string {
368
- const t = ref.trim();
369
- if (!t) {
370
- throw new Error("openviking: empty session ref");
371
- }
372
- if (OPENVIKING_OV_SESSION_UUID.test(t)) {
373
- return t.toLowerCase();
374
- }
375
- if (WINDOWS_BAD_SESSION_SEGMENT.test(t)) {
376
- return createHash("sha256").update(t, "utf8").digest("hex");
377
- }
378
- return t;
379
- }
380
-
381
- /**
382
- * Convert an OpenViking stored message (parts-based format) into one or more
383
- * OpenClaw AgentMessages (content-blocks format).
384
- *
385
- * For assistant messages with ToolParts, this produces:
386
- * 1. The assistant message with canonical toolCall blocks in its content array
387
- * 2. A separate toolResult message per ToolPart (carrying tool_output)
388
- */
389
- export function convertToAgentMessages(msg: { role: string; parts: unknown[] }): AgentMessage[] {
390
- const parts = msg.parts ?? [];
391
- const contentBlocks: Record<string, unknown>[] = [];
392
- const toolCallBlocks: Record<string, unknown>[] = [];
393
- const toolResults: AgentMessage[] = [];
394
-
395
- for (const part of parts) {
396
- if (!part || typeof part !== "object") continue;
397
- const p = part as Record<string, unknown>;
398
-
399
- if (p.type === "text" && typeof p.text === "string") {
400
- contentBlocks.push({ type: "text", text: p.text });
401
- } else if (p.type === "context") {
402
- if (typeof p.abstract === "string" && p.abstract) {
403
- contentBlocks.push({ type: "text", text: p.abstract });
404
- }
405
- } else if (p.type === "tool") {
406
- const toolId = typeof p.tool_id === "string" ? p.tool_id : "";
407
- const toolName = typeof p.tool_name === "string" ? p.tool_name : undefined;
408
- const status = typeof p.tool_status === "string" ? p.tool_status : "unknown";
409
- const output = typeof p.tool_output === "string" ? p.tool_output : "";
410
- const ref = typeof p.tool_output_ref === "string" ? p.tool_output_ref : "";
411
- const originalChars = typeof p.tool_output_original_chars === "number"
412
- ? p.tool_output_original_chars
413
- : undefined;
414
-
415
- if (toolId) {
416
- // Structured path: emit canonical toolCall + toolResult pair (works for any role)
417
- toolCallBlocks.push({
418
- type: "toolCall",
419
- id: toolId,
420
- name: toolName ?? "unknown",
421
- arguments: p.tool_input ?? {},
422
- });
423
-
424
- let resultText = (status === "completed" || status === "error")
425
- ? (output || "(no output)")
426
- : "(interrupted — tool did not complete)";
427
- if (ref && !resultText.includes(ref)) {
428
- const suffix = originalChars !== undefined
429
- ? `\n[tool-result-ref] ${ref} original_chars=${originalChars}`
430
- : `\n[tool-result-ref] ${ref}`;
431
- resultText += suffix;
432
- }
433
- const resultPayload: Record<string, unknown> = {
434
- role: "toolResult",
435
- toolCallId: toolId,
436
- content: [{ type: "text", text: resultText }],
437
- isError: status === "error",
438
- };
439
- if (toolName) {
440
- resultPayload.toolName = toolName;
441
- }
442
- toolResults.push(resultPayload as unknown as AgentMessage);
443
- } else {
444
- // No tool_id: degrade to text block
445
- const fallbackName = toolName ?? "unknown";
446
- const segments = [`[${fallbackName}] (${status})`];
447
- if (p.tool_input) {
448
- try {
449
- segments.push(`Input: ${JSON.stringify(p.tool_input)}`);
450
- } catch {
451
- // non-serializable input, skip
452
- }
453
- }
454
- if (output) {
455
- segments.push(`Output: ${output}`);
456
- }
457
- contentBlocks.push({ type: "text", text: segments.join("\n") });
458
- }
459
- }
460
- }
461
-
462
- const result: AgentMessage[] = [];
463
-
464
- if (msg.role === "assistant") {
465
- // Assistant: text + toolCall in one message, then toolResults
466
- result.push({ role: "assistant", content: [...contentBlocks, ...toolCallBlocks] });
467
- result.push(...toolResults);
468
- } else {
469
- // Non-assistant: emit text as original role, then synthesize assistant(toolCall) + toolResult
470
- const texts = contentBlocks
471
- .filter((b) => b.type === "text")
472
- .map((b) => b.text as string);
473
- if (texts.length > 0) {
474
- result.push({ role: msg.role, content: texts.join("\n") });
475
- } else if (toolCallBlocks.length === 0) {
476
- result.push({ role: msg.role, content: "" });
477
- }
478
- if (toolCallBlocks.length > 0) {
479
- result.push({ role: "assistant", content: toolCallBlocks });
480
- result.push(...toolResults);
481
- }
482
- }
483
-
484
- return result;
485
- }
486
-
487
- function isToolOnlyMessage(msg: ExtractedTurnMessage): boolean {
488
- return msg.role === "user" && msg.parts.length > 0 && msg.parts.every((part) => part.type === "tool");
489
- }
490
-
491
- function coalesceConsecutiveToolMessages(messages: ExtractedTurnMessage[]): ExtractedTurnMessage[] {
492
- const result: ExtractedTurnMessage[] = [];
493
- let pendingTools: ExtractedTurnMessage | undefined;
494
-
495
- const flush = () => {
496
- if (pendingTools) {
497
- result.push(pendingTools);
498
- pendingTools = undefined;
499
- }
500
- };
501
-
502
- for (const msg of messages) {
503
- if (isToolOnlyMessage(msg)) {
504
- if (!pendingTools) {
505
- pendingTools = { role: "user", parts: [] };
506
- }
507
- pendingTools.parts.push(...msg.parts);
508
- continue;
509
- }
510
- flush();
511
- result.push(msg);
512
- }
513
- flush();
514
- return result;
515
- }
516
-
517
- function normalizeAssistantContent(messages: AgentMessage[]): void {
518
- for (let i = 0; i < messages.length; i++) {
519
- const msg = messages[i];
520
- if (msg?.role === "assistant" && typeof msg.content === "string") {
521
- messages[i] = {
522
- ...msg,
523
- content: [{ type: "text", text: msg.content }],
524
- };
525
- }
526
- }
527
- }
528
-
529
- function canonicalizeAssistantBlock(block: unknown): unknown {
530
- if (!block || typeof block !== "object") {
531
- return block;
532
- }
533
-
534
- const rec = block as Record<string, unknown>;
535
- const type = typeof rec.type === "string" ? rec.type : "";
536
- if (type === "toolCall") {
537
- if (rec.arguments !== undefined) {
538
- return rec;
539
- }
540
- return {
541
- ...rec,
542
- arguments: rec.input ?? rec.toolInput ?? {},
543
- };
544
- }
545
-
546
- if (type === "toolUse" || type === "functionCall" || type === "tool_call") {
547
- return {
548
- type: "toolCall",
549
- id: rec.id ?? rec.toolCallId ?? rec.toolUseId,
550
- name: rec.name,
551
- arguments: rec.arguments ?? rec.input ?? rec.toolInput ?? {},
552
- };
553
- }
554
-
555
- return rec;
556
- }
557
-
558
- function canonicalizeAgentMessages(messages: AgentMessage[]): AgentMessage[] {
559
- let changed = false;
560
- const next = messages.map((msg) => {
561
- if (!msg || typeof msg !== "object") {
562
- return msg;
563
- }
564
-
565
- if (msg.role === "assistant") {
566
- const content = Array.isArray(msg.content)
567
- ? msg.content.map((block) => canonicalizeAssistantBlock(block))
568
- : typeof msg.content === "string"
569
- ? [{ type: "text", text: msg.content }]
570
- : msg.content;
571
-
572
- if (content !== msg.content) {
573
- changed = true;
574
- return { ...msg, content };
575
- }
576
- return msg;
577
- }
578
-
579
- if (msg.role === "toolResult") {
580
- const raw = msg as Record<string, unknown>;
581
- const toolCallId =
582
- (typeof raw.toolCallId === "string" && raw.toolCallId) ||
583
- (typeof raw.toolUseId === "string" && raw.toolUseId) ||
584
- undefined;
585
- const toolName =
586
- typeof raw.toolName === "string" && raw.toolName.trim()
587
- ? raw.toolName.trim()
588
- : undefined;
589
-
590
- const nextMsg = {
591
- ...msg,
592
- ...(toolCallId ? { toolCallId } : {}),
593
- ...(toolName ? { toolName } : {}),
594
- } as AgentMessage;
595
-
596
- if (nextMsg !== msg) {
597
- changed = true;
598
- }
599
- return nextMsg;
600
- }
601
-
602
- return msg;
603
- });
604
-
605
- return changed ? next : messages;
606
- }
607
-
608
- export function formatMessageFaithful(msg: OVMessage): string {
609
- const roleTag = `[${msg.role}]`;
610
- if (!msg.parts || msg.parts.length === 0) {
611
- return `${roleTag}: (empty)`;
612
- }
613
-
614
- const sections: string[] = [];
615
- for (const part of msg.parts) {
616
- if (!part || typeof part !== "object") continue;
617
- switch (part.type) {
618
- case "text":
619
- if (part.text) sections.push(part.text);
620
- break;
621
- case "tool": {
622
- const status = part.tool_status ?? "unknown";
623
- const header = `[Tool: ${part.tool_name ?? "unknown"}] (${status})`;
624
- const inputStr = part.tool_input
625
- ? `Input: ${JSON.stringify(part.tool_input, null, 2)}`
626
- : "";
627
- const outputStr = part.tool_output ? `Output:\n${part.tool_output}` : "";
628
- sections.push([header, inputStr, outputStr].filter(Boolean).join("\n"));
629
- break;
630
- }
631
- case "context":
632
- sections.push(
633
- `[Context: ${part.uri ?? "?"}]${part.abstract ? ` ${part.abstract}` : ""}`,
634
- );
635
- break;
636
- default:
637
- sections.push(`[${part.type}]: ${JSON.stringify(part)}`);
638
- }
639
- }
640
-
641
- return `${roleTag}:\n${sections.join("\n\n")}`;
642
- }
643
-
644
- function buildSystemPromptAddition(): string {
645
- return [
646
- "## Session Context Guide",
647
- "",
648
- "Your conversation history includes two layers:",
649
- "",
650
- "1. **[Session History Summary]** — A compressed summary of all prior turns",
651
- " in this session. It is organized into structured sections (Key Facts,",
652
- " Timeline, People, etc.). Use it for background and continuity.",
653
- " The summary is lossy: specific details (exact dates, numbers, names,",
654
- " small events) may have been compressed away.",
655
- "",
656
- "2. **Active messages** — The most recent uncompressed turns.",
657
- "",
658
- "**Rules:**",
659
- "- When active messages conflict with the Summary, trust active messages",
660
- " as the newer source of truth.",
661
- "- Do not fabricate details the Summary does not state explicitly.",
662
- "- **CRITICAL: Before answering 'no information' or 'not mentioned',",
663
- " you MUST carefully re-read EVERY section of the [Session History Summary].",
664
- " The answer may be expressed with different wording than the question.",
665
- " Look for synonyms, related facts, and indirect references.**",
666
- "- If the Summary mentions a topic but lacks the specific detail asked,",
667
- " use the `ov_archive_search` tool to grep the original archived messages",
668
- " for the exact detail. Try 2-3 different keywords extracted from the question.",
669
- "- Only conclude information is unavailable AFTER both checking the Summary",
670
- " thoroughly AND searching the archives with at least 2 keyword variations.",
671
- ].join("\n");
672
- }
673
-
674
- function buildInstructionPrompt(): { text: string; tokens: number } {
675
- const text = buildSystemPromptAddition();
676
- return { text, tokens: estimateTextTokens(text) };
677
- }
678
-
679
- function buildArchiveMemory(
680
- archiveOverview: string | undefined,
681
- _preAbstracts: Array<{ archive_id: string; abstract: string }>,
682
- _budget: number,
683
- ): { messages: AgentMessage[]; tokens: number } {
684
- const messages: AgentMessage[] = [];
685
-
686
- if (archiveOverview) {
687
- messages.push({
688
- role: "user",
689
- content: `[Session History Summary]\n${archiveOverview}`,
690
- });
691
- }
692
-
693
- return { messages, tokens: roughEstimate(messages) };
694
- }
695
-
696
- /** Merge consecutive assistant messages by concatenating their content arrays. */
697
- export function mergeConsecutiveAssistants(messages: AgentMessage[]): AgentMessage[] {
698
- const result: AgentMessage[] = [];
699
- for (const msg of messages) {
700
- const prev = result[result.length - 1];
701
- if (msg.role === "assistant" && prev?.role === "assistant") {
702
- const prevContent = Array.isArray(prev.content) ? prev.content : [{ type: "text", text: prev.content }];
703
- const currContent = Array.isArray(msg.content) ? msg.content : [{ type: "text", text: msg.content }];
704
- prev.content = [...prevContent, ...currContent] as typeof prev.content;
705
- } else {
706
- result.push({ ...msg });
707
- }
708
- }
709
- return result;
710
- }
711
-
712
- /**
713
- * Hoist tool_result blocks to the front of a content array.
714
- *
715
- * The Anthropic / Bedrock / Gemini APIs require tool_result blocks to appear
716
- * at the START of a user message's content array (a tool_result must follow
717
- * the assistant tool_use that produced it). When mergeConsecutiveUsers
718
- * merges two user messages, the previous content's text blocks may end up
719
- * before tool_results from the second message — this function fixes the order.
720
- *
721
- * Same pattern as Claude Code's hoistToolResults in src/utils/messages.ts.
722
- */
723
- function hoistToolResults<T>(content: T[]): T[] {
724
- const toolResults: T[] = [];
725
- const others: T[] = [];
726
- for (const block of content) {
727
- if (
728
- block &&
729
- typeof block === "object" &&
730
- "type" in block &&
731
- (block as { type?: string }).type === "tool_result"
732
- ) {
733
- toolResults.push(block);
734
- } else {
735
- others.push(block);
736
- }
737
- }
738
- return [...toolResults, ...others];
739
- }
740
-
741
- /**
742
- * Merge consecutive user messages by concatenating their content arrays.
743
- *
744
- * Mirror of mergeConsecutiveAssistants. Required because Gemini and Anthropic
745
- * APIs reject consecutive same-role messages with stopReason=stop payloads=0
746
- * (empty response). Three independent sources can inject role: "user":
747
- *
748
- * 1. Archive commit: "[Session History Summary]" via buildArchiveMemory
749
- * 2. OpenClaw yield events: "[sessions_yield interrupt]" / "Turn yielded. ..."
750
- * 3. Audio/Telegram metadata: "[Audio] User text: [Telegram <name>...]"
751
- *
752
- * Without merging, these can stack into 2-5 consecutive user turns. The
753
- * 1P Anthropic API would merge server-side, but Bedrock/Gemini won't —
754
- * we merge client-side for wire-format consistency.
755
- *
756
- * Note: this MUST run AFTER sanitizeToolUseResultPairing because that pass
757
- * may strip orphaned tool_use / tool_result blocks and thereby create new
758
- * user-user adjacencies that didn't exist in the input.
759
- *
760
- * Tracks issue #1724.
761
- */
762
- export function mergeConsecutiveUsers(messages: AgentMessage[]): AgentMessage[] {
763
- const result: AgentMessage[] = [];
764
- for (const msg of messages) {
765
- const prev = result[result.length - 1];
766
- if (msg.role === "user" && prev?.role === "user") {
767
- const prevContent = Array.isArray(prev.content) ? prev.content : [{ type: "text", text: prev.content }];
768
- const currContent = Array.isArray(msg.content) ? msg.content : [{ type: "text", text: msg.content }];
769
- prev.content = hoistToolResults([...prevContent, ...currContent]) as typeof prev.content;
770
- } else {
771
- result.push({ ...msg });
772
- }
773
- }
774
- return result;
775
- }
776
-
777
- /**
778
- * Defensive role-alternation invariant check.
779
- *
780
- * After mergeConsecutiveUsers + mergeConsecutiveAssistants, the message stream
781
- * should already alternate user/assistant. But sanitizeToolUseResultPairing
782
- * can in rare cases strip a user_with_tool_result message that was the only
783
- * thing separating two assistant messages, leaving an assistant-assistant
784
- * adjacency that upstream merge passes can't fix.
785
- *
786
- * When detected, we insert a placeholder user message — matching Claude Code's
787
- * NO_CONTENT_MESSAGE pattern (see CC src/utils/messages.ts:5375-5388) — to
788
- * preserve the alternation contract that Gemini / Anthropic require.
789
- */
790
- export function ensureAlternation(messages: AgentMessage[]): AgentMessage[] {
791
- const result: AgentMessage[] = [];
792
- for (const msg of messages) {
793
- const prev = result[result.length - 1];
794
- if (prev && prev.role === "assistant" && msg.role === "assistant") {
795
- result.push({
796
- role: "user",
797
- content: "(no content)",
798
- });
799
- }
800
- result.push(msg);
801
- }
802
- return result;
803
- }
804
-
805
- function buildSessionContext(
806
- ovMessages: OVMessage[],
807
- budget: number,
808
- ): { messages: AgentMessage[]; tokens: number } {
809
- const raw = ovMessages.flatMap((m) => convertToAgentMessages(m));
810
- const messages = mergeConsecutiveAssistants(raw);
811
- const tokens = roughEstimate(messages);
812
- if (budget === BUDGET_UNLIMITED || tokens <= budget) {
813
- return { messages, tokens };
814
- }
815
- const trimmed = [...messages];
816
- while (trimmed.length > 0 && roughEstimate(trimmed) > budget) {
817
- trimmed.shift();
818
- }
819
- return { messages: trimmed, tokens: roughEstimate(trimmed) };
820
- }
821
-
822
- function sleep(ms: number): Promise<void> {
823
- return new Promise((resolve) => setTimeout(resolve, ms));
824
- }
825
-
826
- const PHASE2_POLL_INTERVAL_MS = 800;
827
- const PHASE2_POLL_MAX_MS = DEFAULT_PHASE2_POLL_TIMEOUT_MS;
828
-
829
- /**
830
- * After wait=false commit, Phase2 runs on the server. Poll task until completed/failed/timeout
831
- * so logs show memories_extracted (otherwise it looks like "nothing was saved").
832
- */
833
- async function pollPhase2ExtractionOutcome(
834
- getClient: () => Promise<OpenVikingClient>,
835
- taskId: string,
836
- agentId: string,
837
- logger: Logger,
838
- sessionLabel: string,
839
- ): Promise<void> {
840
- const deadline = Date.now() + PHASE2_POLL_MAX_MS;
841
- try {
842
- const client = await getClient();
843
- while (Date.now() < deadline) {
844
- await sleep(PHASE2_POLL_INTERVAL_MS);
845
- const task = await client.getTask(taskId, agentId).catch((e) => {
846
- logger.warn?.(`openviking: phase2 getTask failed task_id=${taskId}: ${String(e)}`);
847
- return null;
848
- });
849
- if (!task) {
850
- return;
851
- }
852
- const { status } = task;
853
- if (status === "completed") {
854
- logger.info(
855
- `openviking: phase2 completed task_id=${taskId} session=${sessionLabel} ` +
856
- `result=${toJsonLog(task.result ?? {})}`,
857
- );
858
- return;
859
- }
860
- if (status === "failed") {
861
- logger.warn?.(
862
- `openviking: phase2 failed task_id=${taskId} session=${sessionLabel} error=${task.error ?? "unknown"}`,
863
- );
864
- return;
865
- }
866
- }
867
- logger.warn?.(
868
- `openviking: phase2 poll timeout (${PHASE2_POLL_MAX_MS / 1000}s) task_id=${taskId} session=${sessionLabel} — ` +
869
- `check GET /api/v1/tasks/${taskId}`,
870
- );
871
- } catch (e) {
872
- logger.warn?.(`openviking: phase2 poll exception task_id=${taskId}: ${String(e)}`);
873
- }
874
- }
875
-
876
234
  export function createMemoryOpenVikingContextEngine(params: {
877
235
  id: string;
878
236
  name: string;
@@ -888,6 +246,8 @@ export function createMemoryOpenVikingContextEngine(params: {
888
246
  sessionKey?: string;
889
247
  ovSessionId?: string;
890
248
  }) => void;
249
+ queryConfigStore?: RuntimeQueryConfigStore;
250
+ traceRecorder?: { record(entry: RecallTraceEntry): void; recordAndFlush?: (entry: RecallTraceEntry) => Promise<unknown> };
891
251
  }): ContextEngineWithCommit {
892
252
  const {
893
253
  id,
@@ -898,6 +258,8 @@ export function createMemoryOpenVikingContextEngine(params: {
898
258
  getClient,
899
259
  resolveAgentId,
900
260
  rememberSessionAgentId,
261
+ queryConfigStore,
262
+ traceRecorder,
901
263
  } = params;
902
264
 
903
265
  const diagEnabled = cfg.emitStandardDiagnostics;
@@ -914,43 +276,16 @@ export function createMemoryOpenVikingContextEngine(params: {
914
276
  runtimeContext?: Record<string, unknown>;
915
277
  }): Promise<boolean> {
916
278
  const { sessionId } = params;
917
- const { sessionKey, ovSessionId: ovId } = resolveSessionIdentity(params);
918
- if (isBypassedSession({ sessionId, sessionKey })) {
919
- logger.warn?.(
920
- `openviking: commit skipped because session is bypassed (sessionId=${sessionId}, sessionKey=${sessionKey ?? "none"})`,
921
- );
922
- return false;
923
- }
924
- try {
925
- const client = await getClient();
926
- rememberSessionAgentId?.({
927
- sessionId,
928
- sessionKey,
929
- ovSessionId: ovId,
930
- });
931
- const agentId = resolveAgentId(sessionId, sessionKey, ovId);
932
- const commitResult = await client.commitSession(ovId, {
933
- wait: true,
934
- agentId,
935
- keepRecentCount: 0,
936
- });
937
- const memCount = totalExtractedMemories(commitResult.memories_extracted);
938
- if (commitResult.status === "failed") {
939
- logger.warn?.(`openviking: commit Phase 2 failed for session=${sessionId}: ${commitResult.error ?? "unknown"}`);
940
- return false;
941
- }
942
- if (commitResult.status === "timeout") {
943
- logger.warn?.(`openviking: commit Phase 2 timed out for session=${sessionId}, task_id=${commitResult.task_id ?? "none"}`);
944
- return false;
945
- }
946
- logger.info(
947
- `openviking: committed OV session=${sessionId} ovId=${ovId}, archived=${commitResult.archived ?? false}, memories=${memCount}, task_id=${commitResult.task_id ?? "none"}, trace_id=${commitResult.trace_id ?? "none"}`,
948
- );
949
- return true;
950
- } catch (err) {
951
- logger.warn?.(`openviking: commit failed for session=${sessionId}: ${String(err)}`);
952
- return false;
953
- }
279
+ const { sessionKey } = resolveSessionIdentity(params);
280
+ return commitOpenVikingSession({
281
+ sessionId,
282
+ sessionKey,
283
+ getClient,
284
+ logger,
285
+ rememberSessionAgentId,
286
+ resolveAgentId,
287
+ isBypassedSession,
288
+ });
954
289
  }
955
290
 
956
291
  function extractSessionKey(runtimeContext: Record<string, unknown> | undefined): string | undefined {
@@ -984,92 +319,6 @@ export function createMemoryOpenVikingContextEngine(params: {
984
319
  };
985
320
  }
986
321
 
987
- function extractRuntimeAgentId(
988
- runtimeContext: Record<string, unknown> | undefined,
989
- ): string | undefined {
990
- if (!runtimeContext) {
991
- return undefined;
992
- }
993
- const agentId = runtimeContext.agentId;
994
- return typeof agentId === "string" && agentId.trim() ? agentId.trim() : undefined;
995
- }
996
-
997
- function assemblePassthrough(
998
- ovSessionId: string,
999
- reason: string,
1000
- liveMessages: AgentMessage[],
1001
- originalTokens: number,
1002
- extra?: Record<string, unknown>,
1003
- ): AssembleResult {
1004
- diag("assemble_result", ovSessionId, {
1005
- passthrough: true,
1006
- reason,
1007
- outputMessagesCount: liveMessages.length,
1008
- inputTokenEstimate: originalTokens,
1009
- estimatedTokens: originalTokens,
1010
- tokensSaved: 0,
1011
- savingPct: 0,
1012
- ...extra,
1013
- });
1014
- return { messages: liveMessages, estimatedTokens: originalTokens };
1015
- }
1016
-
1017
- function buildAssembledContext(
1018
- overview: string | undefined,
1019
- preAbstracts: Array<{ archive_id: string; abstract: string }>,
1020
- ovMessages: OVMessage[],
1021
- tokenBudget: number,
1022
- ovSessionId: string,
1023
- ): {
1024
- sanitized: AgentMessage[];
1025
- archive: { messages: AgentMessage[]; tokens: number };
1026
- session: { messages: AgentMessage[]; tokens: number };
1027
- budgets: ContextBudgets;
1028
- instruction: { text: string; tokens: number };
1029
- } {
1030
- const hasArchives = Boolean(overview) || preAbstracts.length > 0;
1031
- const instruction = hasArchives ? buildInstructionPrompt() : { text: "", tokens: 0 };
1032
-
1033
- // 4-layer context partitioning:
1034
- // Instruction — system prompt guide (Archive Index / Session History usage)
1035
- // Archive — session history summary + per-archive one-line abstracts
1036
- // Session — active OV messages converted to AgentMessage format
1037
- // Reserved — headroom for model output (not consumed here)
1038
- const budgets = allocateContextBudget(tokenBudget, instruction.tokens);
1039
- const archive = buildArchiveMemory(overview, preAbstracts, budgets.archiveMemory);
1040
- const sessionBudget = Math.max(
1041
- tokenBudget - budgets.reserved - instruction.tokens - archive.tokens,
1042
- 0,
1043
- );
1044
- const session = buildSessionContext(ovMessages, sessionBudget);
1045
- const assembled = [...archive.messages, ...session.messages];
1046
-
1047
- logger.info(
1048
- `openviking: assemble entering session content for ${ovSessionId}: ` +
1049
- JSON.stringify(assembled.map((m) => ({
1050
- role: m.role,
1051
- content: typeof m.content === "string" ? m.content.substring(0, 100) : "[complex]",
1052
- })), null, 2),
1053
- );
1054
-
1055
- normalizeAssistantContent(assembled);
1056
- const canonical = canonicalizeAgentMessages(assembled);
1057
- // Defense in depth (issue #1724):
1058
- // 1) sanitizeToolUseResultPairing may strip orphaned tool_use/tool_result,
1059
- // potentially creating new user-user or assistant-assistant adjacencies.
1060
- // 2) mergeConsecutiveUsers fixes user-user (mirror of mergeConsecutiveAssistants
1061
- // already running inside buildSessionContext).
1062
- // 3) ensureAlternation is a final invariant check for the rare
1063
- // assistant-assistant case that the merges can't reach.
1064
- const sanitized = ensureAlternation(
1065
- mergeConsecutiveUsers(
1066
- sanitizeToolUseResultPairing(canonical as never[]) as AgentMessage[],
1067
- ),
1068
- );
1069
-
1070
- return { sanitized, archive, session, budgets, instruction };
1071
- }
1072
-
1073
322
  return {
1074
323
  info: {
1075
324
  id,
@@ -1091,649 +340,69 @@ export function createMemoryOpenVikingContextEngine(params: {
1091
340
  },
1092
341
 
1093
342
  async assemble(assembleParams): Promise<AssembleResult> {
1094
- const { messages } = assembleParams;
1095
343
  const tokenBudget = validTokenBudget(assembleParams.tokenBudget) ?? 128_000;
1096
- const { sessionKey, ovSessionId: OVSessionId } = resolveSessionIdentity(assembleParams);
1097
- const sender = extractRuntimeSenderId(assembleParams.runtimeContext);
1098
- const latestMessage = messages.at(-1);
1099
344
  const isMainAssemble =
1100
345
  Object.prototype.hasOwnProperty.call(assembleParams, "availableTools") ||
1101
346
  Object.prototype.hasOwnProperty.call(assembleParams, "citationsMode") ||
1102
347
  Object.prototype.hasOwnProperty.call(assembleParams, "prompt");
1103
- const isTransformContextAssemble = !isMainAssemble;
1104
-
1105
- const originalTokens = roughEstimate(messages);
1106
-
1107
- rememberSessionAgentId?.({
348
+ return assembleOpenVikingSession({
1108
349
  sessionId: assembleParams.sessionId,
1109
- sessionKey,
1110
- agentId: extractRuntimeAgentId(assembleParams.runtimeContext),
1111
- ovSessionId: OVSessionId,
1112
- });
1113
- diag("assemble_entry", OVSessionId, {
1114
- messagesCount: messages.length,
1115
- inputTokenEstimate: originalTokens,
350
+ sessionKey: resolveSessionKey(assembleParams),
351
+ messages: assembleParams.messages,
1116
352
  tokenBudget,
1117
- sessionKey: sessionKey ?? null,
1118
- senderIdFound: sender.found,
1119
- senderId: sender.senderId ?? null,
1120
- messages: messageDigest(messages),
353
+ runtimeContext: assembleParams.runtimeContext,
354
+ isMainAssemble,
355
+ cfg,
356
+ getClient,
357
+ logger,
358
+ resolveAgentId,
359
+ rememberSessionAgentId,
360
+ isBypassedSession,
361
+ queryConfigStore,
362
+ traceRecorder,
363
+ diag,
364
+ roughEstimate,
365
+ messageDigest,
366
+ extractAgentMessageText,
367
+ hasAutoRecallBlock,
368
+ prependRecallToLatestUserMessage,
1121
369
  });
1122
-
1123
- if (isBypassedSession({ sessionId: assembleParams.sessionId, sessionKey })) {
1124
- return assemblePassthrough(OVSessionId, "session_bypassed", messages, originalTokens);
1125
- }
1126
-
1127
- if (isTransformContextAssemble) {
1128
- if (latestMessage?.role !== "user") {
1129
- return assemblePassthrough(OVSessionId, "transform_context_non_user_tail", messages, originalTokens, {
1130
- latestRole: latestMessage?.role ?? null,
1131
- });
1132
- }
1133
- if (!cfg.autoRecall) {
1134
- return assemblePassthrough(OVSessionId, "transform_context_auto_recall_disabled", messages, originalTokens);
1135
- }
1136
- if (hasAutoRecallBlock(latestMessage)) {
1137
- return assemblePassthrough(OVSessionId, "transform_context_recall_already_injected", messages, originalTokens);
1138
- }
1139
-
1140
- const recallQuery = prepareRecallQuery(extractAgentMessageText(latestMessage));
1141
- if (!recallQuery.query || recallQuery.query.length < 5) {
1142
- return assemblePassthrough(OVSessionId, "transform_context_empty_recall_query", messages, originalTokens);
1143
- }
1144
- if (recallQuery.truncated) {
1145
- logger.info(
1146
- `openviking: recall query truncated (` +
1147
- `chars=${recallQuery.originalChars}->${recallQuery.finalChars})`,
1148
- );
1149
- }
1150
-
1151
- try {
1152
- const client = await getClient();
1153
- const routingRef = assembleParams.sessionId ?? sessionKey ?? OVSessionId;
1154
- const agentId = resolveAgentId(routingRef, sessionKey, OVSessionId);
1155
- const recall = await buildAutoRecallContext({
1156
- cfg,
1157
- client,
1158
- agentId,
1159
- queryText: recallQuery.query,
1160
- logger,
1161
- verbose: (message) => logger.info(message),
1162
- });
1163
-
1164
- if (!recall.block) {
1165
- return assemblePassthrough(OVSessionId, "transform_context_no_recall_hits", messages, originalTokens, {
1166
- memoryCount: recall.memoryCount,
1167
- });
1168
- }
1169
-
1170
- const withRecall = prependRecallToLatestUserMessage(messages, recall.block);
1171
- const estimatedTokens = roughEstimate(withRecall);
1172
- diag("assemble_result", OVSessionId, {
1173
- passthrough: false,
1174
- phase: "transform_context",
1175
- outputMessagesCount: withRecall.length,
1176
- inputTokenEstimate: originalTokens,
1177
- estimatedTokens,
1178
- autoRecallMemoryCount: recall.memoryCount,
1179
- autoRecallTokens: recall.estimatedTokens,
1180
- messages: messageDigest(withRecall),
1181
- });
1182
- return { messages: withRecall, estimatedTokens };
1183
- } catch (err) {
1184
- logger.warn?.(`openviking: auto-recall failed: ${String(err)}`);
1185
- return assemblePassthrough(OVSessionId, "transform_context_recall_failed", messages, originalTokens, {
1186
- error: String(err),
1187
- });
1188
- }
1189
- }
1190
-
1191
- try {
1192
- const client = await getClient();
1193
- const routingRef = assembleParams.sessionId ?? sessionKey ?? OVSessionId;
1194
- const agentId = resolveAgentId(routingRef, sessionKey, OVSessionId);
1195
- const ctx = await client.getSessionContext(OVSessionId, tokenBudget, agentId);
1196
-
1197
- const preAbstracts = ctx?.pre_archive_abstracts ?? [];
1198
- const hasArchives = !!ctx?.latest_archive_overview || preAbstracts.length > 0;
1199
- const activeCount = ctx?.messages?.length ?? 0;
1200
-
1201
- if (!ctx || (!hasArchives && activeCount === 0)) {
1202
- return assemblePassthrough(OVSessionId, "no_ov_data", messages, originalTokens, {
1203
- archiveCount: 0, activeCount: 0,
1204
- });
1205
- }
1206
- if (!hasArchives && ctx.messages.length < messages.length) {
1207
- return assemblePassthrough(OVSessionId, "ov_msgs_fewer_than_input", messages, originalTokens, {
1208
- archiveCount: 0, activeCount,
1209
- });
1210
- }
1211
-
1212
- const { sanitized, archive, session, budgets, instruction } = buildAssembledContext(
1213
- ctx.latest_archive_overview,
1214
- preAbstracts,
1215
- ctx.messages,
1216
- tokenBudget,
1217
- OVSessionId,
1218
- );
1219
-
1220
- if (sanitized.length === 0 && messages.length > 0) {
1221
- return assemblePassthrough(OVSessionId, "sanitized_empty", messages, originalTokens, {
1222
- archiveCount: preAbstracts.length, activeCount,
1223
- });
1224
- }
1225
-
1226
- const assembledTokens = roughEstimate(sanitized) + instruction.tokens;
1227
- const tokensSaved = originalTokens - assembledTokens;
1228
- const savingPct = originalTokens > 0 ? Math.round((tokensSaved / originalTokens) * 100) : 0;
1229
-
1230
- diag("assemble_result", OVSessionId, {
1231
- passthrough: false,
1232
- archiveCount: preAbstracts.length,
1233
- activeCount,
1234
- outputMessagesCount: sanitized.length,
1235
- inputTokenEstimate: originalTokens,
1236
- estimatedTokens: assembledTokens,
1237
- tokensSaved,
1238
- savingPct,
1239
- archiveTokens: archive.tokens,
1240
- archiveBudget: budgets.archiveMemory,
1241
- sessionTokens: session.tokens,
1242
- sessionBudget: budgets.sessionContext,
1243
- reservedBudget: budgets.reserved,
1244
- senderIdFound: sender.found,
1245
- senderId: sender.senderId ?? null,
1246
- messages: messageDigest(sanitized),
1247
- });
1248
-
1249
- return {
1250
- messages: sanitized,
1251
- estimatedTokens: assembledTokens,
1252
- ...(instruction.text ? { systemPromptAddition: instruction.text } : {}),
1253
- };
1254
- } catch (err) {
1255
- logger.warn?.(
1256
- `openviking: assemble failed for session=${OVSessionId}, ` +
1257
- `tokenBudget=${tokenBudget}, agentId=${resolveAgentId(OVSessionId)}: ${String(err)}`,
1258
- );
1259
- diag("assemble_error", OVSessionId, {
1260
- error: String(err),
1261
- tokenBudget,
1262
- agentId: resolveAgentId(OVSessionId),
1263
- senderIdFound: sender.found,
1264
- senderId: sender.senderId ?? null,
1265
- });
1266
- return { messages, estimatedTokens: roughEstimate(messages) };
1267
- }
1268
370
  },
1269
371
 
1270
372
  async afterTurn(afterTurnParams): Promise<void> {
1271
- if (!cfg.autoCapture) {
1272
- return;
1273
- }
1274
-
1275
- if (afterTurnParams.isHeartbeat) {
1276
- return;
1277
- }
1278
-
1279
- try {
1280
- const sender = extractRuntimeSenderId(afterTurnParams.runtimeContext);
1281
- const { sessionKey, ovSessionId: OVSessionId } = resolveSessionIdentity(afterTurnParams);
1282
- const runtimeAgentId = extractRuntimeAgentId(afterTurnParams.runtimeContext);
1283
- if (runtimeAgentId) {
1284
- rememberSessionAgentId?.({
1285
- agentId: runtimeAgentId,
1286
- sessionId: afterTurnParams.sessionId,
1287
- sessionKey,
1288
- ovSessionId: OVSessionId,
1289
- });
1290
- }
1291
- const routingRef =
1292
- afterTurnParams.sessionId ?? sessionKey ?? OVSessionId;
1293
- const agentId = resolveAgentId(routingRef, sessionKey, OVSessionId);
1294
-
1295
- if (isBypassedSession({ sessionId: afterTurnParams.sessionId, sessionKey })) {
1296
- diag("afterTurn_skip", OVSessionId, {
1297
- reason: "session_bypassed",
1298
- totalMessages: afterTurnParams.messages?.length ?? 0,
1299
- senderIdFound: sender.found,
1300
- senderId: sender.senderId ?? null,
1301
- });
1302
- return;
1303
- }
1304
-
1305
- const messages = afterTurnParams.messages ?? [];
1306
- if (messages.length === 0) {
1307
- diag("afterTurn_skip", OVSessionId, {
1308
- reason: "no_messages",
1309
- totalMessages: 0,
1310
- senderIdFound: sender.found,
1311
- senderId: sender.senderId ?? null,
1312
- });
1313
- return;
1314
- }
1315
-
1316
- const start =
1317
- typeof afterTurnParams.prePromptMessageCount === "number" &&
1318
- afterTurnParams.prePromptMessageCount >= 0
1319
- ? afterTurnParams.prePromptMessageCount
1320
- : 0;
1321
-
1322
- const { messages: extractedMessagesRaw, newCount } = extractNewTurnMessages(messages, start);
1323
- const extractedMessages = coalesceConsecutiveToolMessages(extractedMessagesRaw);
1324
-
1325
- if (extractedMessages.length === 0) {
1326
- diag("afterTurn_skip", OVSessionId, {
1327
- reason: "no_new_turn_messages",
1328
- totalMessages: messages.length,
1329
- prePromptMessageCount: start,
1330
- senderIdFound: sender.found,
1331
- senderId: sender.senderId ?? null,
1332
- });
1333
- return;
1334
- }
1335
-
1336
- const turnMessages = messages.slice(start) as AgentMessage[];
1337
- const newMessages = turnMessages.filter((m: any) => {
1338
- const r = (m as Record<string, unknown>).role as string;
1339
- return r === "user" || r === "assistant";
1340
- }) as AgentMessage[];
1341
- const newMsgFull = messageDigest(newMessages);
1342
- const newTurnTokens = newMsgFull.reduce((s, d) => s + d.tokens, 0);
1343
-
1344
- diag("afterTurn_entry", OVSessionId, {
1345
- totalMessages: messages.length,
1346
- newMessageCount: newCount,
1347
- prePromptMessageCount: start,
1348
- newTurnTokens,
1349
- senderIdFound: sender.found,
1350
- senderId: sender.senderId ?? null,
1351
- messages: newMsgFull,
1352
- });
1353
-
1354
- const client = await getClient();
1355
- const createdAt = pickLatestCreatedAt(turnMessages);
1356
- const senderRoleId = toRoleId(sender.senderId);
1357
- // 发送结构化消息:统一 role 为 user,通过 parts 区分类型
1358
- for (const msg of extractedMessages) {
1359
- const ovParts = msg.parts.map((part) => {
1360
- if (part.type === "text") {
1361
- // 清理 relevant-memories 块
1362
- const cleaned = part.text
1363
- .replace(/<relevant-memories>[\s\S]*?<\/relevant-memories>/gi, " ")
1364
- .replace(/\s+/g, " ")
1365
- .trim();
1366
- return { type: "text" as const, text: cleaned };
1367
- } else {
1368
- return {
1369
- type: "tool" as const,
1370
- tool_id: part.toolCallId,
1371
- tool_name: part.toolName,
1372
- tool_input: part.toolInput,
1373
- tool_output: part.toolOutput,
1374
- tool_status: part.toolStatus,
1375
- };
1376
- }
1377
- });
1378
-
1379
- if (ovParts.length > 0) {
1380
- await client.addSessionMessage(
1381
- OVSessionId,
1382
- msg.role, // 统一是 "user"
1383
- ovParts,
1384
- agentId,
1385
- createdAt,
1386
- msg.role === "user" ? senderRoleId : undefined,
1387
- );
1388
- }
1389
- }
1390
-
1391
- const session = await client.getSession(OVSessionId, agentId);
1392
- const pendingTokens = session.pending_tokens ?? 0;
1393
-
1394
- if (pendingTokens < cfg.commitTokenThreshold) {
1395
- diag("afterTurn_skip", OVSessionId, {
1396
- reason: "below_threshold",
1397
- pendingTokens,
1398
- commitTokenThreshold: cfg.commitTokenThreshold,
1399
- senderIdFound: sender.found,
1400
- senderId: sender.senderId ?? null,
1401
- });
1402
- return;
1403
- }
1404
-
1405
- const commitResult = await client.commitSession(OVSessionId, {
1406
- wait: false,
1407
- agentId,
1408
- keepRecentCount: cfg.commitKeepRecentCount,
1409
- });
1410
- logger.info(
1411
- `openviking: committed session=${OVSessionId}, ` +
1412
- `status=${commitResult.status}, archived=${commitResult.archived ?? false}, ` +
1413
- `task_id=${commitResult.task_id ?? "none"}, trace_id=${commitResult.trace_id ?? "none"}`,
1414
- );
1415
-
1416
- diag("afterTurn_commit", OVSessionId, {
1417
- pendingTokens,
1418
- commitTokenThreshold: cfg.commitTokenThreshold,
1419
- status: commitResult.status,
1420
- archived: commitResult.archived ?? false,
1421
- taskId: commitResult.task_id ?? null,
1422
- extractedMemories: totalExtractedMemories(commitResult.memories_extracted),
1423
- senderIdFound: sender.found,
1424
- senderId: sender.senderId ?? null,
1425
- });
1426
- if (commitResult.task_id && cfg.logFindRequests) {
1427
- logger.info(
1428
- `openviking: Phase2 memory extraction runs asynchronously on the server (task_id=${commitResult.task_id}). ` +
1429
- "memories_extracted appears only after that task completes — not in this immediate response.",
1430
- );
1431
- void pollPhase2ExtractionOutcome(
1432
- getClient,
1433
- commitResult.task_id,
1434
- agentId,
1435
- logger,
1436
- OVSessionId,
1437
- );
1438
- }
1439
- } catch (err) {
1440
- logger.warn?.(`openviking: afterTurn failed: ${String(err)}`);
1441
- const sender = extractRuntimeSenderId(afterTurnParams.runtimeContext);
1442
- diag("afterTurn_error", afterTurnParams.sessionId ?? "(unknown)", {
1443
- error: String(err),
1444
- senderIdFound: sender.found,
1445
- senderId: sender.senderId ?? null,
1446
- });
1447
- }
373
+ await afterTurnOpenVikingSession({
374
+ sessionId: afterTurnParams.sessionId,
375
+ sessionKey: resolveSessionKey(afterTurnParams),
376
+ messages: afterTurnParams.messages,
377
+ prePromptMessageCount: afterTurnParams.prePromptMessageCount,
378
+ isHeartbeat: afterTurnParams.isHeartbeat,
379
+ runtimeContext: afterTurnParams.runtimeContext,
380
+ cfg,
381
+ getClient,
382
+ logger,
383
+ resolveAgentId,
384
+ rememberSessionAgentId,
385
+ isBypassedSession,
386
+ diag,
387
+ });
1448
388
  },
1449
389
 
1450
390
  async compact(compactParams): Promise<CompactResult> {
1451
- const { sessionKey, ovSessionId: OVSessionId } = resolveSessionIdentity(compactParams);
1452
391
  const tokenBudget = validTokenBudget(compactParams.tokenBudget) ?? 128_000;
1453
- diag("compact_entry", OVSessionId, {
392
+ return compactOpenVikingSession({
393
+ sessionId: compactParams.sessionId,
394
+ sessionKey: resolveSessionKey(compactParams),
1454
395
  tokenBudget,
1455
- force: compactParams.force ?? false,
1456
- currentTokenCount: compactParams.currentTokenCount ?? null,
1457
- compactionTarget: compactParams.compactionTarget ?? null,
1458
- hasCustomInstructions: typeof compactParams.customInstructions === "string" &&
1459
- compactParams.customInstructions.trim().length > 0,
396
+ currentTokenCount: compactParams.currentTokenCount,
397
+ force: compactParams.force,
398
+ compactionTarget: compactParams.compactionTarget,
399
+ customInstructions: compactParams.customInstructions,
400
+ getClient,
401
+ logger,
402
+ resolveAgentId,
403
+ isBypassedSession,
404
+ diag,
1460
405
  });
1461
-
1462
- if (isBypassedSession({ sessionId: compactParams.sessionId, sessionKey })) {
1463
- diag("compact_result", OVSessionId, {
1464
- ok: true,
1465
- compacted: false,
1466
- reason: "session_bypassed",
1467
- });
1468
- return {
1469
- ok: true,
1470
- compacted: false,
1471
- reason: "session_bypassed",
1472
- };
1473
- }
1474
-
1475
- const client = await getClient();
1476
- const agentId = resolveAgentId(compactParams.sessionId, sessionKey, OVSessionId);
1477
- const tokensBeforeOriginal = validTokenBudget(compactParams.currentTokenCount);
1478
- let preCommitEstimatedTokens: number | undefined;
1479
- if (typeof tokensBeforeOriginal !== "number") {
1480
- try {
1481
- const preCtx = await client.getSessionContext(OVSessionId, tokenBudget, agentId);
1482
- if (
1483
- typeof preCtx.estimatedTokens === "number" &&
1484
- Number.isFinite(preCtx.estimatedTokens)
1485
- ) {
1486
- preCommitEstimatedTokens = preCtx.estimatedTokens;
1487
- }
1488
- } catch (preCtxErr) {
1489
- logger.info(
1490
- `openviking: compact pre-ctx fetch failed for session=${OVSessionId}, ` +
1491
- `tokenBudget=${tokenBudget}, agentId=${agentId}: ${String(preCtxErr)}`,
1492
- );
1493
- }
1494
- }
1495
-
1496
- const tokensBefore = tokensBeforeOriginal ?? preCommitEstimatedTokens ?? -1;
1497
-
1498
- try {
1499
- logger.info(
1500
- `openviking: compact committing session=${OVSessionId} (wait=true, tokenBudget=${tokenBudget})`,
1501
- );
1502
- const commitResult = await client.commitSession(OVSessionId, {
1503
- wait: true,
1504
- agentId,
1505
- keepRecentCount: 0,
1506
- });
1507
- const memCount = totalExtractedMemories(commitResult.memories_extracted);
1508
-
1509
- if (commitResult.status === "failed") {
1510
- logger.warn?.(
1511
- `openviking: compact commit Phase 2 failed for session=${OVSessionId}: ${commitResult.error ?? "unknown"}`,
1512
- );
1513
- diag("compact_result", OVSessionId, {
1514
- ok: false,
1515
- compacted: false,
1516
- reason: "commit_failed",
1517
- status: commitResult.status,
1518
- archived: commitResult.archived ?? false,
1519
- taskId: commitResult.task_id ?? null,
1520
- error: commitResult.error ?? null,
1521
- });
1522
- return {
1523
- ok: false,
1524
- compacted: false,
1525
- reason: "commit_failed",
1526
- result: {
1527
- summary: "",
1528
- firstKeptEntryId: "",
1529
- tokensBefore: tokensBefore,
1530
- tokensAfter: undefined,
1531
- details: {
1532
- commit: commitResult,
1533
- },
1534
- },
1535
- };
1536
- }
1537
-
1538
- if (commitResult.status === "timeout") {
1539
- logger.warn?.(
1540
- `openviking: compact commit Phase 2 timed out for session=${OVSessionId}, task_id=${commitResult.task_id ?? "none"}`,
1541
- );
1542
- diag("compact_result", OVSessionId, {
1543
- ok: false,
1544
- compacted: false,
1545
- reason: "commit_timeout",
1546
- status: commitResult.status,
1547
- archived: commitResult.archived ?? false,
1548
- taskId: commitResult.task_id ?? null,
1549
- });
1550
- return {
1551
- ok: false,
1552
- compacted: false,
1553
- reason: "commit_timeout",
1554
- result: {
1555
- summary: "",
1556
- firstKeptEntryId: "",
1557
- tokensBefore: tokensBefore,
1558
- tokensAfter: undefined,
1559
- details: {
1560
- commit: commitResult,
1561
- },
1562
- },
1563
- };
1564
- }
1565
-
1566
- logger.info(
1567
- `openviking: compact committed session=${OVSessionId}, archived=${commitResult.archived ?? false}, memories=${memCount}, task_id=${commitResult.task_id ?? "none"}, trace_id=${commitResult.trace_id ?? "none"}`,
1568
- );
1569
-
1570
- if (!commitResult.archived) {
1571
- logger.info(
1572
- `openviking: compact no archive for session=${OVSessionId}, ` +
1573
- `tokensBefore=${tokensBefore}, tokensAfter=${tokensBefore}`,
1574
- );
1575
- diag("compact_result", OVSessionId, {
1576
- ok: true,
1577
- compacted: false,
1578
- reason: "commit_no_archive",
1579
- status: commitResult.status,
1580
- archived: commitResult.archived ?? false,
1581
- taskId: commitResult.task_id ?? null,
1582
- memories: memCount,
1583
- tokensBefore: tokensBefore,
1584
- });
1585
- return {
1586
- ok: true,
1587
- compacted: false,
1588
- reason: "commit_no_archive",
1589
- result: {
1590
- summary: "",
1591
- tokensBefore: tokensBefore,
1592
- tokensAfter: tokensBefore >= 0 ? tokensBefore : undefined,
1593
- details: {
1594
- commit: commitResult,
1595
- },
1596
- },
1597
- };
1598
- }
1599
-
1600
- let summary = "";
1601
- let firstKeptEntryId = commitResult.archive_uri?.split("/").pop() ?? "";
1602
- let tokensAfter: number | undefined;
1603
- let contextFetchError: string | undefined;
1604
-
1605
- let ctx: Awaited<ReturnType<typeof client.getSessionContext>> | undefined;
1606
- try {
1607
- ctx = await client.getSessionContext(OVSessionId, tokenBudget, agentId);
1608
- // 打印完整的 getSessionContext 结果
1609
- logger.info(
1610
- `openviking: compact getSessionContext raw result for ${OVSessionId}: ` +
1611
- JSON.stringify(ctx, null, 2),
1612
- );
1613
- if (typeof ctx.latest_archive_overview === "string") {
1614
- summary = ctx.latest_archive_overview.trim();
1615
- }
1616
- if (
1617
- typeof ctx.estimatedTokens === "number" &&
1618
- Number.isFinite(ctx.estimatedTokens)
1619
- ) {
1620
- tokensAfter = ctx.estimatedTokens;
1621
- }
1622
- // 打印 compact 后重新写入 session 的完整内容
1623
- logger.info(
1624
- `openviking: compact restored session content for ${OVSessionId}: ` +
1625
- `messages=${ctx.messages?.length ?? 0}, ` +
1626
- `latestArchiveOverview=${summary.length > 0 ? "present" : "empty"} (${summary.length} chars), ` +
1627
- `preArchiveAbstracts=${ctx.pre_archive_abstracts?.length ?? 0}, ` +
1628
- `estimatedTokens=${ctx.estimatedTokens}`,
1629
- );
1630
- if (summary.length > 0) {
1631
- logger.info(
1632
- `openviking: compact latest_archive_overview for ${OVSessionId}: ${summary.substring(0, 200)}...`,
1633
- );
1634
- }
1635
- if (ctx.messages && ctx.messages.length > 0) {
1636
- // 打印所有消息的 role 和 content 摘要
1637
- const msgSummary = ctx.messages.map((m: { role?: string; content?: string; parts?: Array<{ type?: string; text?: string }> }) => {
1638
- const role = m.role ?? "unknown";
1639
- let textPreview = "";
1640
- if (m.content) {
1641
- textPreview = m.content.substring(0, 80);
1642
- } else if (m.parts && m.parts.length > 0) {
1643
- const textPart = m.parts.find((p: { type?: string }) => p.type === "text");
1644
- textPreview = textPart?.text?.substring(0, 80) ?? JSON.stringify(m.parts).substring(0, 80);
1645
- }
1646
- return { role, textPreview };
1647
- });
1648
- logger.info(
1649
- `openviking: compact restored messages for ${OVSessionId}: ` +
1650
- JSON.stringify(msgSummary),
1651
- );
1652
- }
1653
- } catch (ctxErr) {
1654
- contextFetchError = String(ctxErr);
1655
- logger.info(
1656
- `openviking: compact context fetch failed for session=${OVSessionId}, ` +
1657
- `tokenBudget=${tokenBudget}, agentId=${agentId}: ${contextFetchError}`,
1658
- );
1659
- }
1660
-
1661
- logger.info(
1662
- `openviking: compact tokens session=${OVSessionId}, ` +
1663
- `tokensBefore=${tokensBefore}, tokensAfter=${tokensAfter ?? "unknown"}, ` +
1664
- `latestArchiveId=${firstKeptEntryId || "none"}`,
1665
- );
1666
-
1667
- diag("compact_result", OVSessionId, {
1668
- ok: true,
1669
- compacted: true,
1670
- reason: "commit_completed",
1671
- status: commitResult.status,
1672
- archived: commitResult.archived ?? false,
1673
- taskId: commitResult.task_id ?? null,
1674
- memories: memCount,
1675
- tokensBefore: tokensBefore,
1676
- tokensAfter: tokensAfter ?? null,
1677
- latestArchiveId: firstKeptEntryId || null,
1678
- summaryPresent: summary.length > 0,
1679
- });
1680
- return {
1681
- ok: true,
1682
- compacted: true,
1683
- reason: "commit_completed",
1684
- result: {
1685
- summary,
1686
- firstKeptEntryId,
1687
- tokensBefore,
1688
- tokensAfter,
1689
- details: contextFetchError
1690
- ? {
1691
- commit: commitResult,
1692
- contextError: contextFetchError,
1693
- }
1694
- : {
1695
- commit: commitResult,
1696
- },
1697
- },
1698
- };
1699
- } catch (err) {
1700
- const errorMessage = String(err);
1701
- if (errorMessage.includes("[NOT_FOUND]") && errorMessage.includes("Session not found")) {
1702
- logger.info(
1703
- `openviking: compact skipped because OV session does not exist ` +
1704
- `(session=${OVSessionId}, agentId=${agentId})`,
1705
- );
1706
- diag("compact_result", OVSessionId, {
1707
- ok: true,
1708
- compacted: false,
1709
- reason: "session_not_found",
1710
- error: errorMessage,
1711
- });
1712
- return {
1713
- ok: true,
1714
- compacted: false,
1715
- reason: "session_not_found",
1716
- };
1717
- }
1718
- logger.warn?.(`openviking: compact commit failed for session=${OVSessionId}: ${errorMessage}`);
1719
- diag("compact_error", OVSessionId, {
1720
- error: errorMessage,
1721
- });
1722
- return {
1723
- ok: false,
1724
- compacted: false,
1725
- reason: "commit_error",
1726
- result: {
1727
- summary: "",
1728
- firstKeptEntryId: "",
1729
- tokensBefore: tokensBefore,
1730
- tokensAfter: undefined,
1731
- details: {
1732
- error: errorMessage,
1733
- },
1734
- },
1735
- };
1736
- }
1737
406
  },
1738
407
  };
1739
408
  }