@aliyunrds/ctxdb 1.0.9 → 1.0.10-beta.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.
@@ -1,24 +1,26 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  captureParsedMessages
4
- } from "../chunk-2ROMQSPA.js";
5
- import "../chunk-7TPYIT63.js";
4
+ } from "../chunk-LASAMQQY.js";
5
+ import "../chunk-XWJAZAMN.js";
6
6
  import {
7
7
  HttpClient,
8
8
  agentFromArgvWithFallback,
9
9
  debug,
10
10
  load,
11
11
  resolveDataApiCredential,
12
- setDebug
13
- } from "../chunk-2U7G6UXE.js";
12
+ sessionBinding,
13
+ setDebug,
14
+ skillStopRows
15
+ } from "../chunk-TZFDXKLJ.js";
16
+ import "../chunk-5ITT5GSP.js";
14
17
  import {
15
18
  shouldSkipHooks
16
19
  } from "../chunk-5ML4VSXN.js";
17
- import "../chunk-5ITT5GSP.js";
18
20
  import "../chunk-PI2SUS3M.js";
19
21
 
20
22
  // src/hooks/hermes-post-llm-call.ts
21
- import { pathToFileURL } from "url";
23
+ import { pathToFileURL } from "node:url";
22
24
  function asRecord(value) {
23
25
  return value && typeof value === "object" && !Array.isArray(value) ? value : {};
24
26
  }
@@ -170,6 +172,23 @@ async function main() {
170
172
  );
171
173
  }
172
174
  const input = extractHermesPostLlmCall(event);
175
+ if (agent === "hermes" && input.sessionId) {
176
+ try {
177
+ const extra = asRecord(event.extra);
178
+ const history = extra.conversation_history ?? event.conversation_history;
179
+ const answer = extra.assistant_response ?? event.assistant_response;
180
+ if (Array.isArray(history) && typeof answer === "string") {
181
+ await skillStopRows(
182
+ sessionBinding(input.sessionId, void 0, "hermes"),
183
+ history,
184
+ { last_assistant_message: answer },
185
+ null
186
+ );
187
+ }
188
+ } catch {
189
+ process.stderr.write("ctxdb skill: Hermes checkpoint unavailable\n");
190
+ }
191
+ }
173
192
  const cfg = load({ agent });
174
193
  const credential = resolveDataApiCredential(cfg);
175
194
  setDebug(cfg.debug);
@@ -1,34 +1,39 @@
1
1
  #!/usr/bin/env node
2
+ import {
3
+ composeUserPromptSubmit
4
+ } from "../chunk-5HPYYOPH.js";
2
5
  import {
3
6
  HOOK_TIMEOUT_MS,
4
7
  composeSessionStart
5
- } from "../chunk-FD3IPF6R.js";
6
- import {
7
- composeUserPromptSubmit
8
- } from "../chunk-U4DRH4NZ.js";
8
+ } from "../chunk-SG4LQTOF.js";
9
9
  import {
10
10
  fetchKbCatalogBlock
11
- } from "../chunk-V7IZ2UYJ.js";
12
- import "../chunk-T5ZJEPR4.js";
11
+ } from "../chunk-VMBCSFGF.js";
12
+ import "../chunk-SZ3AZDMU.js";
13
13
  import {
14
14
  isCircuitOpen
15
- } from "../chunk-7TPYIT63.js";
15
+ } from "../chunk-XWJAZAMN.js";
16
16
  import {
17
17
  HttpClient,
18
18
  agentFromArgvWithFallback,
19
+ catalogContext,
19
20
  debug,
20
21
  load,
21
22
  resolveDataApiCredential,
22
- setDebug
23
- } from "../chunk-2U7G6UXE.js";
23
+ sessionBinding,
24
+ setDebug,
25
+ skillDeferredFeedback,
26
+ skillPromptContext,
27
+ skillSessionStart
28
+ } from "../chunk-TZFDXKLJ.js";
29
+ import "../chunk-5ITT5GSP.js";
24
30
  import {
25
31
  shouldSkipHooks
26
32
  } from "../chunk-5ML4VSXN.js";
27
- import "../chunk-5ITT5GSP.js";
28
33
  import "../chunk-PI2SUS3M.js";
29
34
 
30
35
  // src/hooks/hermes-pre-llm-call.ts
31
- import { pathToFileURL } from "url";
36
+ import { pathToFileURL } from "node:url";
32
37
  function asRecord(v) {
33
38
  return v && typeof v === "object" && !Array.isArray(v) ? v : {};
34
39
  }
@@ -97,6 +102,7 @@ async function readStdinJson() {
97
102
  }
98
103
  }
99
104
  async function main() {
105
+ let skillContext = "", memoryContext = "", skillSession = null;
100
106
  try {
101
107
  if (shouldSkipHooks()) return 0;
102
108
  const event = await readStdinJson();
@@ -107,8 +113,24 @@ async function main() {
107
113
  `
108
114
  );
109
115
  }
110
- const input = extractHermesPreLlmCall(event);
111
116
  const cfg = load({ agent });
117
+ if (isCircuitOpen(agent, cfg.baseUrl)) return 0;
118
+ const input = extractHermesPreLlmCall(event);
119
+ if (agent === "hermes" && input.sessionId) {
120
+ skillSession = input.sessionId;
121
+ try {
122
+ if (input.isFirstTurn) skillContext = await skillSessionStart({ session_id: input.sessionId, source: "startup" }, void 0, "hermes");
123
+ else {
124
+ try {
125
+ skillContext = catalogContext(sessionBinding(input.sessionId, void 0, "hermes"));
126
+ } catch {
127
+ skillContext = await skillSessionStart({ session_id: input.sessionId, source: "startup" }, void 0, "hermes");
128
+ }
129
+ }
130
+ await skillPromptContext({ session_id: input.sessionId, prompt: input.prompt }, void 0, "hermes");
131
+ } catch {
132
+ }
133
+ }
112
134
  const credential = resolveDataApiCredential(cfg);
113
135
  setDebug(cfg.debug);
114
136
  debug("hermes.pre_llm_call", "start", {
@@ -150,12 +172,23 @@ async function main() {
150
172
  }
151
173
  if (!composed.ctx) return 0;
152
174
  debug("hermes.pre_llm_call", `ok, context length=${composed.ctx.length}`);
153
- process.stdout.write(formatHermesPreLlmCallStdout(composed.ctx));
175
+ memoryContext = composed.ctx;
154
176
  return 0;
155
177
  } catch (err) {
156
178
  process.stderr.write(`ctxdb hermes pre_llm_call: unexpected error: ${err?.message ?? err}
157
179
  `);
158
180
  return 0;
181
+ } finally {
182
+ const context = [skillContext, memoryContext].filter(Boolean).join("\n\n");
183
+ let handedOff = false;
184
+ try {
185
+ if (skillSession) await skillDeferredFeedback(sessionBinding(skillSession, void 0, "hermes"), async (feedback) => {
186
+ handedOff = true;
187
+ await new Promise((resolve, reject) => process.stdout.write(formatHermesPreLlmCallStdout([context, feedback].filter(Boolean).join("\n\n")), (error) => error ? reject(error) : resolve()));
188
+ });
189
+ } catch {
190
+ }
191
+ if (!handedOff && context) process.stdout.write(formatHermesPreLlmCallStdout(context));
159
192
  }
160
193
  }
161
194
  if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ agentFromArgvWithFallback,
4
+ receiveEnd,
5
+ sessionBinding
6
+ } from "../chunk-TZFDXKLJ.js";
7
+ import "../chunk-5ITT5GSP.js";
8
+ import {
9
+ shouldSkipHooks
10
+ } from "../chunk-5ML4VSXN.js";
11
+ import "../chunk-PI2SUS3M.js";
12
+
13
+ // src/hooks/session-end.ts
14
+ async function main() {
15
+ const { agent } = agentFromArgvWithFallback();
16
+ if (shouldSkipHooks() || agent === "default") return;
17
+ try {
18
+ let raw = "";
19
+ for await (const chunk of process.stdin) {
20
+ raw += chunk;
21
+ if (Buffer.byteLength(raw) > 65536) return;
22
+ }
23
+ const event = JSON.parse(raw);
24
+ if (event.hook_event_name !== "SessionEnd" || typeof event.session_id !== "string") return;
25
+ const binding = sessionBinding(event.session_id, void 0, agent);
26
+ await receiveEnd(binding);
27
+ } catch {
28
+ process.stderr.write("ctxdb skill: session state unavailable\n");
29
+ }
30
+ }
31
+ main().then(() => {
32
+ process.exitCode = 0;
33
+ }).catch(() => {
34
+ process.exitCode = 0;
35
+ });
@@ -3,28 +3,29 @@ import {
3
3
  HOOK_TIMEOUT_MS,
4
4
  composeSessionStart,
5
5
  formatSessionStartStdout
6
- } from "../chunk-FD3IPF6R.js";
7
- import "../chunk-V7IZ2UYJ.js";
8
- import "../chunk-T5ZJEPR4.js";
6
+ } from "../chunk-SG4LQTOF.js";
7
+ import "../chunk-VMBCSFGF.js";
8
+ import "../chunk-SZ3AZDMU.js";
9
9
  import {
10
10
  isCircuitOpen
11
- } from "../chunk-7TPYIT63.js";
11
+ } from "../chunk-XWJAZAMN.js";
12
12
  import {
13
13
  HttpClient,
14
14
  agentFromArgvWithFallback,
15
15
  debug,
16
16
  load,
17
17
  resolveDataApiCredential,
18
- setDebug
19
- } from "../chunk-2U7G6UXE.js";
18
+ setDebug,
19
+ skillSessionStart
20
+ } from "../chunk-TZFDXKLJ.js";
21
+ import "../chunk-5ITT5GSP.js";
20
22
  import {
21
23
  shouldSkipHooks
22
24
  } from "../chunk-5ML4VSXN.js";
23
- import "../chunk-5ITT5GSP.js";
24
25
  import "../chunk-PI2SUS3M.js";
25
26
 
26
27
  // src/hooks/session-start.ts
27
- import { pathToFileURL } from "url";
28
+ import { pathToFileURL } from "node:url";
28
29
  async function readStdinJson() {
29
30
  let raw = "";
30
31
  for await (const chunk of process.stdin) raw += chunk;
@@ -37,10 +38,17 @@ async function readStdinJson() {
37
38
  }
38
39
  }
39
40
  async function main() {
41
+ let skillContext = "";
42
+ let memoryContext = "";
43
+ let outputAgent;
40
44
  try {
41
45
  if (shouldSkipHooks()) return 0;
42
46
  const event = await readStdinJson();
43
47
  const { agent, fellBack } = agentFromArgvWithFallback();
48
+ outputAgent = agent;
49
+ const cfg = load({ agent });
50
+ if (isCircuitOpen(agent, cfg.baseUrl)) return 0;
51
+ if (agent !== "default") skillContext = await skillSessionStart(event, void 0, agent);
44
52
  if (fellBack) {
45
53
  process.stderr.write(
46
54
  `ctxdb warmup: agent unspecified, defaulting to ${agent}
@@ -50,7 +58,6 @@ async function main() {
50
58
  const cwd = typeof event.cwd === "string" ? event.cwd : "";
51
59
  const sessionId = typeof event.session_id === "string" ? event.session_id : typeof event.sessionId === "string" ? event.sessionId : null;
52
60
  if (!cwd) return 0;
53
- const cfg = load({ agent });
54
61
  const credential = resolveDataApiCredential(cfg);
55
62
  setDebug(cfg.debug);
56
63
  debug("warmup", "start", { cwd, userId: cfg.userId });
@@ -89,12 +96,15 @@ async function main() {
89
96
  `ctxdb warmup: ok (${memoryCount} memories, ${kbChunkCount} kb chunks, kb_catalog=${kbCatalogLines} lines)
90
97
  `
91
98
  );
92
- process.stdout.write(formatSessionStartStdout(agent, ctx));
99
+ memoryContext = ctx;
93
100
  return 0;
94
101
  } catch (err) {
95
102
  process.stderr.write(`ctxdb warmup: unexpected error: ${err?.message ?? err}
96
103
  `);
97
104
  return 0;
105
+ } finally {
106
+ const context = [skillContext, memoryContext].filter(Boolean).join("\n\n");
107
+ if (outputAgent && context) process.stdout.write(formatSessionStartStdout(outputAgent, context));
98
108
  }
99
109
  }
100
110
  if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
@@ -1,20 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  captureTurn
4
- } from "../chunk-2ROMQSPA.js";
5
- import "../chunk-7TPYIT63.js";
4
+ } from "../chunk-LASAMQQY.js";
5
+ import "../chunk-XWJAZAMN.js";
6
6
  import {
7
7
  HttpClient,
8
8
  agentFromArgvWithFallback,
9
9
  debug,
10
10
  load,
11
11
  resolveDataApiCredential,
12
- setDebug
13
- } from "../chunk-2U7G6UXE.js";
12
+ sessionBinding,
13
+ setDebug,
14
+ skillStop
15
+ } from "../chunk-TZFDXKLJ.js";
16
+ import "../chunk-5ITT5GSP.js";
14
17
  import {
15
18
  shouldSkipHooks
16
19
  } from "../chunk-5ML4VSXN.js";
17
- import "../chunk-5ITT5GSP.js";
18
20
  import "../chunk-PI2SUS3M.js";
19
21
 
20
22
  // src/hooks/stop.ts
@@ -47,6 +49,14 @@ async function main() {
47
49
  const transcriptPath = event.transcript_path;
48
50
  if (typeof transcriptPath !== "string" || !transcriptPath) return 0;
49
51
  const sessionId = extractHookSessionId(event);
52
+ if (agent !== "default" && sessionId) {
53
+ try {
54
+ const binding = sessionBinding(sessionId, void 0, agent);
55
+ await skillStop(binding, event, null);
56
+ } catch {
57
+ process.stderr.write("ctxdb skill: evidence capture unavailable\n");
58
+ }
59
+ }
50
60
  const cfg = load({ agent });
51
61
  const credential = resolveDataApiCredential(cfg);
52
62
  setDebug(cfg.debug);
@@ -2,28 +2,29 @@
2
2
  import {
3
3
  composeUserPromptSubmit,
4
4
  formatUserPromptSubmitStdout
5
- } from "../chunk-U4DRH4NZ.js";
6
- import "../chunk-V7IZ2UYJ.js";
7
- import "../chunk-T5ZJEPR4.js";
5
+ } from "../chunk-5HPYYOPH.js";
6
+ import "../chunk-VMBCSFGF.js";
7
+ import "../chunk-SZ3AZDMU.js";
8
8
  import {
9
9
  isCircuitOpen
10
- } from "../chunk-7TPYIT63.js";
10
+ } from "../chunk-XWJAZAMN.js";
11
11
  import {
12
12
  HttpClient,
13
13
  agentFromArgvWithFallback,
14
14
  debug,
15
15
  load,
16
16
  resolveDataApiCredential,
17
- setDebug
18
- } from "../chunk-2U7G6UXE.js";
17
+ setDebug,
18
+ skillPromptContext
19
+ } from "../chunk-TZFDXKLJ.js";
20
+ import "../chunk-5ITT5GSP.js";
19
21
  import {
20
22
  shouldSkipHooks
21
23
  } from "../chunk-5ML4VSXN.js";
22
- import "../chunk-5ITT5GSP.js";
23
24
  import "../chunk-PI2SUS3M.js";
24
25
 
25
26
  // src/hooks/user-prompt-submit.ts
26
- import { pathToFileURL } from "url";
27
+ import { pathToFileURL } from "node:url";
27
28
  async function readStdinJson() {
28
29
  let raw = "";
29
30
  for await (const chunk of process.stdin) raw += chunk;
@@ -36,10 +37,17 @@ async function readStdinJson() {
36
37
  }
37
38
  }
38
39
  async function main() {
40
+ let skillContext = "";
41
+ let memoryContext = "";
42
+ let outputAgent;
39
43
  try {
40
44
  if (shouldSkipHooks()) return 0;
41
45
  const event = await readStdinJson();
42
46
  const { agent, fellBack } = agentFromArgvWithFallback();
47
+ outputAgent = agent;
48
+ if (agent !== "default") {
49
+ skillContext = await skillPromptContext(event, void 0, agent);
50
+ }
43
51
  if (fellBack) {
44
52
  process.stderr.write(
45
53
  `ctxdb recall: agent unspecified, defaulting to ${agent}
@@ -82,12 +90,15 @@ async function main() {
82
90
  }
83
91
  if (!ctx) return 0;
84
92
  debug("recall", `ok, additionalContext length=${ctx.length}`);
85
- process.stdout.write(formatUserPromptSubmitStdout(agent, ctx));
93
+ memoryContext = ctx;
86
94
  return 0;
87
95
  } catch (err) {
88
96
  process.stderr.write(`ctxdb recall: unexpected error: ${err?.message ?? err}
89
97
  `);
90
98
  return 0;
99
+ } finally {
100
+ const context = [memoryContext, skillContext].filter(Boolean).join("\n\n");
101
+ if (outputAgent && context) process.stdout.write(formatUserPromptSubmitStdout(outputAgent, context));
91
102
  }
92
103
  }
93
104
  if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {