@langgraph-js/pure-graph 1.0.1 → 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,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/pure-graph",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [],
@@ -26,6 +26,7 @@
26
26
  },
27
27
  "scripts": {
28
28
  "dev": "bun run test/hono.ts",
29
- "build": "tsc"
29
+ "build": "rm -r dist && tsc",
30
+ "prepublish": "pnpm build"
30
31
  }
31
32
  }