@langgraph-js/pure-graph 1.0.0 → 1.0.2

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.
@@ -42,7 +42,6 @@ api.post('/threads/:thread_id/runs/stream', zValidator('param', z.object({ threa
42
42
  for await (const { event, data } of client.runs.stream(thread_id, payload.assistant_id, payload)) {
43
43
  await stream.writeSSE({ data: serialiseAsDict(data), event });
44
44
  }
45
- // await stream.sleep(500); // 不知为何要等
46
45
  });
47
46
  });
48
47
  api.get('/threads/:thread_id/runs', zValidator('param', z.object({ thread_id: z.string().uuid() })), zValidator('query', z.object({
@@ -61,8 +61,7 @@ export const createEndpoint = (threads) => {
61
61
  },
62
62
  };
63
63
  }
64
- const run = threads.createRun(threadId, assistantId, payload);
65
- for await (const data of streamState(threads, run, payload, {
64
+ for await (const data of streamState(threads, threads.createRun(threadId, assistantId, payload), payload, {
66
65
  attempt: 0,
67
66
  getGraph,
68
67
  })) {
@@ -180,7 +180,7 @@ export async function* streamState(threads, run, payload, options) {
180
180
  // throw error;
181
181
  }
182
182
  finally {
183
- // 在完成后立即清理队列,因为消费者已经完成
183
+ // 在完成后清理队列
184
184
  await threads.set(threadId, { status: 'idle' });
185
185
  globalMessageQueue.removeQueue(queueId);
186
186
  }
@@ -14,7 +14,7 @@ export class CancelEventMessage extends EventMessage {
14
14
  }
15
15
  export class StreamEndEventMessage extends EventMessage {
16
16
  constructor() {
17
- super('__stream_end__');
17
+ super('__stream_end__', 'stream end');
18
18
  }
19
19
  }
20
20
  export class StreamErrorEventMessage extends EventMessage {
@@ -18,7 +18,7 @@ export class MemoryStreamQueue extends BaseStreamQueue {
18
18
  * 异步生成器:支持 for await...of 方式消费队列数据
19
19
  */
20
20
  async *onDataReceive() {
21
- const queue = [];
21
+ let queue = [];
22
22
  let pendingResolve = null;
23
23
  let isStreamEnded = false;
24
24
  const handleData = async (item) => {
@@ -28,7 +28,13 @@ export class MemoryStreamQueue extends BaseStreamQueue {
28
28
  if (data.event === '__stream_end__' ||
29
29
  data.event === '__stream_error__' ||
30
30
  data.event === '__stream_cancel__') {
31
- isStreamEnded = true;
31
+ setTimeout(() => {
32
+ isStreamEnded = true;
33
+ if (pendingResolve) {
34
+ pendingResolve();
35
+ pendingResolve = null;
36
+ }
37
+ }, 300);
32
38
  if (data.event === '__stream_cancel__') {
33
39
  this.cancel();
34
40
  }
@@ -38,12 +44,15 @@ export class MemoryStreamQueue extends BaseStreamQueue {
38
44
  pendingResolve = null;
39
45
  }
40
46
  };
47
+ // todo 这个框架的事件监听的数据返回顺序有误
41
48
  this.on('dataChange', handleData);
42
49
  try {
43
50
  while (!isStreamEnded) {
44
51
  if (queue.length > 0) {
45
- const item = queue.shift();
46
- yield item;
52
+ for (const item of queue) {
53
+ yield item;
54
+ }
55
+ queue = [];
47
56
  }
48
57
  else {
49
58
  await new Promise((resolve) => {
package/package.json CHANGED
@@ -1,16 +1,8 @@
1
1
  {
2
2
  "name": "@langgraph-js/pure-graph",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
- "exports": {
7
- ".": {
8
- "import": "./dist/index.js"
9
- },
10
- "/hono": {
11
- "import": "./dist/adapter/hono/index.js"
12
- }
13
- },
14
6
  "keywords": [],
15
7
  "author": "",
16
8
  "license": "MIT",
@@ -34,6 +26,7 @@
34
26
  },
35
27
  "scripts": {
36
28
  "dev": "bun run test/hono.ts",
37
- "build": "tsc"
29
+ "build": "rm -r dist && tsc",
30
+ "prepublish": "pnpm build"
38
31
  }
39
32
  }
@@ -53,7 +53,6 @@ api.post(
53
53
  for await (const { event, data } of client.runs.stream(thread_id, payload.assistant_id, payload)) {
54
54
  await stream.writeSSE({ data: serialiseAsDict(data), event });
55
55
  }
56
- // await stream.sleep(500); // 不知为何要等
57
56
  });
58
57
  },
59
58
  );
@@ -78,12 +78,15 @@ export const createEndpoint = (threads: BaseThreadsManager): ILangGraphClient =>
78
78
  };
79
79
  }
80
80
 
81
- const run = threads.createRun(threadId, assistantId, payload);
82
-
83
- for await (const data of streamState(threads, run, payload, {
84
- attempt: 0,
85
- getGraph,
86
- })) {
81
+ for await (const data of streamState(
82
+ threads,
83
+ threads.createRun(threadId, assistantId, payload),
84
+ payload,
85
+ {
86
+ attempt: 0,
87
+ getGraph,
88
+ },
89
+ )) {
87
90
  yield data;
88
91
  }
89
92
  },
@@ -246,7 +246,7 @@ export async function* streamState(
246
246
  await threads.set(threadId, { status: 'error' });
247
247
  // throw error;
248
248
  } finally {
249
- // 在完成后立即清理队列,因为消费者已经完成
249
+ // 在完成后清理队列
250
250
  await threads.set(threadId, { status: 'idle' });
251
251
  globalMessageQueue.removeQueue(queueId);
252
252
  }
@@ -16,7 +16,7 @@ export class CancelEventMessage extends EventMessage {
16
16
 
17
17
  export class StreamEndEventMessage extends EventMessage {
18
18
  constructor() {
19
- super('__stream_end__');
19
+ super('__stream_end__', 'stream end');
20
20
  }
21
21
  }
22
22
 
@@ -21,20 +21,26 @@ export class MemoryStreamQueue extends BaseStreamQueue implements BaseStreamQueu
21
21
  * 异步生成器:支持 for await...of 方式消费队列数据
22
22
  */
23
23
  async *onDataReceive(): AsyncGenerator<EventMessage, void, unknown> {
24
- const queue: EventMessage[] = [];
24
+ let queue: EventMessage[] = [];
25
25
  let pendingResolve: (() => void) | null = null;
26
26
  let isStreamEnded = false;
27
27
  const handleData = async (item: EventMessage) => {
28
28
  const data = this.compressMessages ? ((await this.decodeData(item as any)) as EventMessage) : item;
29
29
  queue.push(data);
30
-
31
30
  // 检查是否为流结束或错误信号
32
31
  if (
33
32
  data.event === '__stream_end__' ||
34
33
  data.event === '__stream_error__' ||
35
34
  data.event === '__stream_cancel__'
36
35
  ) {
37
- isStreamEnded = true;
36
+ setTimeout(() => {
37
+ isStreamEnded = true;
38
+ if (pendingResolve) {
39
+ pendingResolve();
40
+ pendingResolve = null;
41
+ }
42
+ }, 300);
43
+
38
44
  if (data.event === '__stream_cancel__') {
39
45
  this.cancel();
40
46
  }
@@ -45,14 +51,16 @@ export class MemoryStreamQueue extends BaseStreamQueue implements BaseStreamQueu
45
51
  pendingResolve = null;
46
52
  }
47
53
  };
48
-
54
+ // todo 这个框架的事件监听的数据返回顺序有误
49
55
  this.on('dataChange', handleData as any);
50
56
 
51
57
  try {
52
58
  while (!isStreamEnded) {
53
59
  if (queue.length > 0) {
54
- const item = queue.shift() as EventMessage;
55
- yield item;
60
+ for (const item of queue) {
61
+ yield item;
62
+ }
63
+ queue = [];
56
64
  } else {
57
65
  await new Promise((resolve) => {
58
66
  pendingResolve = resolve as () => void;
package/test/test.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { registerGraph } from '../src/createEndpoint';
2
+ import { graph } from '/Users/konghayao/code/ai/code-graph/agents/code/graph';
3
+ import { Hono } from 'hono';
4
+ import LangGraphApp from '../src/adapter/hono/index';
5
+ registerGraph('code', graph);
6
+
7
+ const app = new Hono();
8
+ app.route('/', LangGraphApp);
9
+
10
+ export default app;