@nextclaw/ncp-react 0.4.11 → 0.4.13

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 (2) hide show
  1. package/dist/index.js +64 -1
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -7,6 +7,65 @@ import { DefaultNcpAgentConversationStateManager } from "@nextclaw/ncp-toolkit";
7
7
  import {
8
8
  NcpEventType
9
9
  } from "@nextclaw/ncp";
10
+ var EVENT_BATCH_DELAY_MS = 16;
11
+ var NcpEventDispatchBatcher = class {
12
+ constructor(dispatchBatch) {
13
+ this.dispatchBatch = dispatchBatch;
14
+ this.queue = [];
15
+ this.flushTimerId = null;
16
+ this.isFlushing = false;
17
+ this.isDisposed = false;
18
+ this.enqueue = (event) => {
19
+ if (this.isDisposed) {
20
+ return;
21
+ }
22
+ this.queue.push(event);
23
+ this.scheduleFlush();
24
+ };
25
+ this.dispose = () => {
26
+ this.isDisposed = true;
27
+ if (this.flushTimerId !== null) {
28
+ window.clearTimeout(this.flushTimerId);
29
+ this.flushTimerId = null;
30
+ }
31
+ this.queue.length = 0;
32
+ };
33
+ this.scheduleFlush = () => {
34
+ if (this.flushTimerId !== null || this.isFlushing || this.queue.length === 0) {
35
+ return;
36
+ }
37
+ this.flushTimerId = window.setTimeout(() => {
38
+ this.flushTimerId = null;
39
+ void this.flush();
40
+ }, EVENT_BATCH_DELAY_MS);
41
+ };
42
+ this.flush = async () => {
43
+ if (this.isDisposed || this.isFlushing || this.queue.length === 0) {
44
+ return;
45
+ }
46
+ this.isFlushing = true;
47
+ try {
48
+ while (this.queue.length > 0) {
49
+ const batch = this.queue.splice(0);
50
+ await this.dispatchBatch(batch);
51
+ }
52
+ } finally {
53
+ this.isFlushing = false;
54
+ this.scheduleFlush();
55
+ }
56
+ };
57
+ }
58
+ };
59
+ function dispatchEventsToManager(manager, events) {
60
+ const batchDispatch = manager.dispatchBatch;
61
+ if (typeof batchDispatch === "function") {
62
+ return batchDispatch.call(manager, events);
63
+ }
64
+ return events.reduce(
65
+ (chain, event) => chain.then(() => manager.dispatch(event)),
66
+ Promise.resolve()
67
+ );
68
+ }
10
69
  function shouldDispatchEventToSession(event, sessionId) {
11
70
  const payload = "payload" in event ? event.payload : null;
12
71
  if (!payload || typeof payload !== "object") {
@@ -80,14 +139,18 @@ function useNcpAgentRuntime({
80
139
  setIsSending(false);
81
140
  }, [sessionId]);
82
141
  useEffect(() => {
142
+ const eventBatcher = new NcpEventDispatchBatcher(
143
+ (events) => dispatchEventsToManager(manager, events)
144
+ );
83
145
  const unsubscribeClient = client.subscribe((event) => {
84
146
  if (!shouldDispatchEventToSession(event, sessionId)) {
85
147
  return;
86
148
  }
87
- void manager.dispatch(event);
149
+ eventBatcher.enqueue(event);
88
150
  });
89
151
  return () => {
90
152
  unsubscribeClient();
153
+ eventBatcher.dispose();
91
154
  void client.stop();
92
155
  };
93
156
  }, [client, manager, sessionId]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextclaw/ncp-react",
3
- "version": "0.4.11",
3
+ "version": "0.4.13",
4
4
  "private": false,
5
5
  "description": "React bindings for building NCP-based agent applications.",
6
6
  "type": "module",
@@ -15,8 +15,8 @@
15
15
  "dist"
16
16
  ],
17
17
  "dependencies": {
18
- "@nextclaw/ncp": "0.4.5",
19
- "@nextclaw/ncp-toolkit": "0.4.14"
18
+ "@nextclaw/ncp": "0.4.6",
19
+ "@nextclaw/ncp-toolkit": "0.4.16"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": "^18.0.0 || ^19.0.0"