@raindrop-ai/openai-managed-agents 0.0.1 → 0.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.
package/README.md CHANGED
@@ -68,15 +68,19 @@ For a runner that reconnects streams, use the public typed handler with an
68
68
  unwrapped native client. Keep one Raindrop client for the runner's lifetime:
69
69
 
70
70
  ```ts
71
+ import type { Turn } from "openai/resources/beta/agents/sessions/turns";
72
+
71
73
  raindrop.handler.onSession(session);
72
74
  raindrop.handler.onInput(session.id, userInput); // after successful submission
73
- // Recover missed item events before completing recovered turns.
75
+ // Snapshot turn statuses before recovering their items.
76
+ const recoveredTurns: Turn[] = [];
77
+ for await (const turn of client.beta.agents.sessions.turns.list(session.id, { order: "asc" })) {
78
+ recoveredTurns.push(turn);
79
+ }
74
80
  for await (const item of client.beta.agents.sessions.items.list(session.id, { order: "asc" })) {
75
81
  raindrop.handler.onItem(session.id, item);
76
82
  }
77
- for await (const turn of client.beta.agents.sessions.turns.list(session.id, { order: "asc" })) {
78
- raindrop.handler.onTurn(turn);
79
- }
83
+ for (const turn of recoveredTurns) raindrop.handler.onTurn(turn);
80
84
  for await (const event of events) raindrop.handler.onEvent(event);
81
85
  await raindrop.flush();
82
86
  // After all streams/recovery work are stopped:
@@ -84,8 +88,10 @@ raindrop.handler.forgetSession(session.id);
84
88
  await raindrop.shutdown();
85
89
  ```
86
90
 
87
- Open the stream before recovery so it can buffer live updates. Recover stored
88
- items, then stored turns, before consuming buffered events.
91
+ Open the stream before recovery so it can buffer live updates. Read and buffer
92
+ turn statuses first, recover items next, then pass the buffered turns to the
93
+ handler. A turn completed after the snapshot closes from its buffered live
94
+ terminal event. Process callbacks serially during recovery and consumption.
89
95
 
90
96
  `onInputEvents(sessionId, events)` records accepted message/tool-result submissions.
91
97
  Repeated terminal turns and tool receipts are deduplicated within this client.
@@ -93,11 +99,11 @@ Turn deduplication retains the latest 10,000 completed turns. Across process
93
99
  restarts, provide a stable `eventId: (turnId) => ...` and replay items before turns.
94
100
  No session or item listing is intercepted automatically.
95
101
 
96
- The wrapper closes unfinished captures in the iterator's `finally`, including
97
- early exit and transport failure. This marks capture interruption; it does not
102
+ The wrapper closes unfinished captures when the last iterator for a session
103
+ exits, including early exit and transport failure. This marks capture interruption; it does not
98
104
  cancel the backend turn. Session model and context remain available for later
99
105
  streams; the previous input is cleared. Metadata retains the latest 10,000 idle
100
- sessions plus sessions with active turns. Call `forgetSession` when
106
+ sessions plus sessions with active turns or streams. Call `forgetSession` when
101
107
  the session is no longer needed. Use the callback path when an unfinished turn
102
108
  must survive a reconnection.
103
109
 
@@ -133,5 +139,5 @@ configuration. Subagent model and inherited coordinator input stay absent unless
133
139
  their own input is recorded.
134
140
 
135
141
  See [RELEASING.md](RELEASING.md) for first-publication and verification steps.
136
- See the [integration docs](https://docs.raindrop.ai/integrations/openai-managed-agents)
142
+ See the [integration docs](https://www.raindrop.ai/docs/integrations/openai-managed-agents)
137
143
  for configuration details.
package/dist/index.js CHANGED
@@ -2369,13 +2369,13 @@ var RaindropManagedAgentsHandler = class {
2369
2369
  sessionCapture(sessionId) {
2370
2370
  const existing = this.sessions.get(sessionId);
2371
2371
  if (existing) return existing;
2372
- const session = { context: this.opts, turns: /* @__PURE__ */ new Map() };
2372
+ const session = { context: this.opts, turns: /* @__PURE__ */ new Map(), activeStreams: 0 };
2373
2373
  this.sessions.set(sessionId, session);
2374
2374
  return session;
2375
2375
  }
2376
2376
  cacheSession(sessionId, session) {
2377
2377
  this.idleSessions.delete(sessionId);
2378
- if (session.turns.size > 0) return;
2378
+ if (session.turns.size > 0 || session.activeStreams > 0) return;
2379
2379
  this.idleSessions.add(sessionId);
2380
2380
  if (this.idleSessions.size <= 1e4) return;
2381
2381
  const oldest = this.idleSessions.values().next().value;
@@ -2388,7 +2388,16 @@ var RaindropManagedAgentsHandler = class {
2388
2388
  try {
2389
2389
  const capture = this.sessionCapture(session.id);
2390
2390
  capture.model = session.agent.model;
2391
- capture.context = { ...this.opts, ...capture.context, ...context };
2391
+ capture.context = {
2392
+ ...this.opts,
2393
+ ...capture.context,
2394
+ ...context,
2395
+ properties: {
2396
+ ...this.opts.properties,
2397
+ ...capture.context.properties,
2398
+ ...context.properties
2399
+ }
2400
+ };
2392
2401
  this.cacheSession(session.id, capture);
2393
2402
  } catch (e) {
2394
2403
  }
@@ -2412,7 +2421,12 @@ var RaindropManagedAgentsHandler = class {
2412
2421
  const existing = session.turns.get(turnId);
2413
2422
  if (existing) return existing;
2414
2423
  const context = session.context;
2415
- const eventId = (_c = (_b = (_a = this.opts).eventId) == null ? void 0 : _b.call(_a, turnId)) != null ? _c : randomUUID();
2424
+ let eventId;
2425
+ try {
2426
+ eventId = (_c = (_b = (_a = this.opts).eventId) == null ? void 0 : _b.call(_a, turnId)) == null ? void 0 : _c.trim();
2427
+ } catch (e) {
2428
+ }
2429
+ eventId || (eventId = randomUUID());
2416
2430
  const patch = {
2417
2431
  userId: context.userId,
2418
2432
  convoId: (_d = context.convoId) != null ? _d : sessionId,
@@ -2682,6 +2696,25 @@ var RaindropManagedAgentsHandler = class {
2682
2696
  }
2683
2697
  this.cacheSession(sessionId, session);
2684
2698
  }
2699
+ /** @internal */
2700
+ onStreamStart(sessionId) {
2701
+ try {
2702
+ const session = this.sessionCapture(sessionId);
2703
+ session.activeStreams++;
2704
+ this.cacheSession(sessionId, session);
2705
+ } catch (e) {
2706
+ }
2707
+ }
2708
+ /** @internal */
2709
+ onStreamEnd(sessionId) {
2710
+ try {
2711
+ const session = this.sessions.get(sessionId);
2712
+ if (!session) return;
2713
+ session.activeStreams = Math.max(0, session.activeStreams - 1);
2714
+ if (session.activeStreams === 0) this.closeSessionTurns(sessionId);
2715
+ } catch (e) {
2716
+ }
2717
+ }
2685
2718
  closeSessionTurns(sessionId, error) {
2686
2719
  try {
2687
2720
  const session = this.sessions.get(sessionId);
@@ -2714,15 +2747,15 @@ var RaindropManagedAgentsHandler = class {
2714
2747
  // package.json
2715
2748
  var package_default = {
2716
2749
  name: "@raindrop-ai/openai-managed-agents",
2717
- version: "0.0.1",
2750
+ version: "0.0.2",
2718
2751
  description: "Raindrop tracing for the OpenAI Agents API",
2719
2752
  main: "dist/index.js",
2720
2753
  module: "dist/index.mjs",
2721
2754
  types: "dist/index.d.ts",
2722
2755
  license: "MIT",
2723
- homepage: "https://docs.raindrop.ai/integrations/openai-managed-agents",
2756
+ homepage: "https://www.raindrop.ai/docs/integrations/openai-managed-agents",
2724
2757
  bugs: {
2725
- url: "https://docs.raindrop.ai/support"
2758
+ url: "https://www.raindrop.ai/docs/support"
2726
2759
  },
2727
2760
  exports: {
2728
2761
  ".": {
@@ -2793,14 +2826,18 @@ var libraryVersion = package_default.version;
2793
2826
  // src/wrap.ts
2794
2827
  var import_streaming = require("openai/core/streaming");
2795
2828
  async function* observe(events, handler, sessionId) {
2829
+ if (sessionId) handler.onStreamStart(sessionId);
2796
2830
  try {
2797
2831
  for await (const event of events) {
2798
- if (event.type === "agent.session.created") sessionId = event.session.id;
2832
+ if (!sessionId && event.type === "agent.session.created") {
2833
+ sessionId = event.session.id;
2834
+ handler.onStreamStart(sessionId);
2835
+ }
2799
2836
  handler.onEvent(event);
2800
2837
  yield event;
2801
2838
  }
2802
2839
  } finally {
2803
- if (sessionId) handler.closeSessionTurns(sessionId);
2840
+ if (sessionId) handler.onStreamEnd(sessionId);
2804
2841
  }
2805
2842
  }
2806
2843
  function wrapManagedAgents(client, handler, context) {
@@ -2828,19 +2865,12 @@ function wrapManagedAgents(client, handler, context) {
2828
2865
  if (result instanceof import_streaming.Stream) {
2829
2866
  const source = result;
2830
2867
  async function* initialEvents() {
2831
- let sessionId;
2832
- try {
2833
- for await (const event of source) {
2834
- if (event.type === "agent.session.created") {
2835
- sessionId = event.session.id;
2836
- handler.onSession(event.session, context);
2837
- handler.onInput(event.session.id, params.input);
2838
- }
2839
- handler.onEvent(event);
2840
- yield event;
2868
+ for await (const event of observe(source, handler)) {
2869
+ if (event.type === "agent.session.created") {
2870
+ handler.onSession(event.session, context);
2871
+ handler.onInput(event.session.id, params.input);
2841
2872
  }
2842
- } finally {
2843
- if (sessionId) handler.closeSessionTurns(sessionId);
2873
+ yield event;
2844
2874
  }
2845
2875
  }
2846
2876
  return new import_streaming.Stream(initialEvents, result.controller);
@@ -2855,20 +2885,7 @@ function wrapManagedAgents(client, handler, context) {
2855
2885
  return session;
2856
2886
  });
2857
2887
  if (prop === "stream")
2858
- return (sessionId, params, options) => {
2859
- const stream = target.stream(sessionId, params, options);
2860
- return new Proxy(stream, {
2861
- get(obj, key) {
2862
- if (key === Symbol.asyncIterator)
2863
- return () => {
2864
- handler.onInput(sessionId, params.input);
2865
- return observe(stream, handler, sessionId);
2866
- };
2867
- const value2 = Reflect.get(obj, key, obj);
2868
- return typeof value2 === "function" ? value2.bind(obj) : value2;
2869
- }
2870
- });
2871
- };
2888
+ return (sessionId, params, options) => target.stream.call(wrappedSessions, sessionId, params, options);
2872
2889
  const value = Reflect.get(target, prop, target);
2873
2890
  return typeof value === "function" ? value.bind(target) : value;
2874
2891
  }
package/dist/index.mjs CHANGED
@@ -2342,13 +2342,13 @@ var RaindropManagedAgentsHandler = class {
2342
2342
  sessionCapture(sessionId) {
2343
2343
  const existing = this.sessions.get(sessionId);
2344
2344
  if (existing) return existing;
2345
- const session = { context: this.opts, turns: /* @__PURE__ */ new Map() };
2345
+ const session = { context: this.opts, turns: /* @__PURE__ */ new Map(), activeStreams: 0 };
2346
2346
  this.sessions.set(sessionId, session);
2347
2347
  return session;
2348
2348
  }
2349
2349
  cacheSession(sessionId, session) {
2350
2350
  this.idleSessions.delete(sessionId);
2351
- if (session.turns.size > 0) return;
2351
+ if (session.turns.size > 0 || session.activeStreams > 0) return;
2352
2352
  this.idleSessions.add(sessionId);
2353
2353
  if (this.idleSessions.size <= 1e4) return;
2354
2354
  const oldest = this.idleSessions.values().next().value;
@@ -2361,7 +2361,16 @@ var RaindropManagedAgentsHandler = class {
2361
2361
  try {
2362
2362
  const capture = this.sessionCapture(session.id);
2363
2363
  capture.model = session.agent.model;
2364
- capture.context = { ...this.opts, ...capture.context, ...context };
2364
+ capture.context = {
2365
+ ...this.opts,
2366
+ ...capture.context,
2367
+ ...context,
2368
+ properties: {
2369
+ ...this.opts.properties,
2370
+ ...capture.context.properties,
2371
+ ...context.properties
2372
+ }
2373
+ };
2365
2374
  this.cacheSession(session.id, capture);
2366
2375
  } catch (e) {
2367
2376
  }
@@ -2385,7 +2394,12 @@ var RaindropManagedAgentsHandler = class {
2385
2394
  const existing = session.turns.get(turnId);
2386
2395
  if (existing) return existing;
2387
2396
  const context = session.context;
2388
- const eventId = (_c = (_b = (_a = this.opts).eventId) == null ? void 0 : _b.call(_a, turnId)) != null ? _c : randomUUID();
2397
+ let eventId;
2398
+ try {
2399
+ eventId = (_c = (_b = (_a = this.opts).eventId) == null ? void 0 : _b.call(_a, turnId)) == null ? void 0 : _c.trim();
2400
+ } catch (e) {
2401
+ }
2402
+ eventId || (eventId = randomUUID());
2389
2403
  const patch = {
2390
2404
  userId: context.userId,
2391
2405
  convoId: (_d = context.convoId) != null ? _d : sessionId,
@@ -2655,6 +2669,25 @@ var RaindropManagedAgentsHandler = class {
2655
2669
  }
2656
2670
  this.cacheSession(sessionId, session);
2657
2671
  }
2672
+ /** @internal */
2673
+ onStreamStart(sessionId) {
2674
+ try {
2675
+ const session = this.sessionCapture(sessionId);
2676
+ session.activeStreams++;
2677
+ this.cacheSession(sessionId, session);
2678
+ } catch (e) {
2679
+ }
2680
+ }
2681
+ /** @internal */
2682
+ onStreamEnd(sessionId) {
2683
+ try {
2684
+ const session = this.sessions.get(sessionId);
2685
+ if (!session) return;
2686
+ session.activeStreams = Math.max(0, session.activeStreams - 1);
2687
+ if (session.activeStreams === 0) this.closeSessionTurns(sessionId);
2688
+ } catch (e) {
2689
+ }
2690
+ }
2658
2691
  closeSessionTurns(sessionId, error) {
2659
2692
  try {
2660
2693
  const session = this.sessions.get(sessionId);
@@ -2687,15 +2720,15 @@ var RaindropManagedAgentsHandler = class {
2687
2720
  // package.json
2688
2721
  var package_default = {
2689
2722
  name: "@raindrop-ai/openai-managed-agents",
2690
- version: "0.0.1",
2723
+ version: "0.0.2",
2691
2724
  description: "Raindrop tracing for the OpenAI Agents API",
2692
2725
  main: "dist/index.js",
2693
2726
  module: "dist/index.mjs",
2694
2727
  types: "dist/index.d.ts",
2695
2728
  license: "MIT",
2696
- homepage: "https://docs.raindrop.ai/integrations/openai-managed-agents",
2729
+ homepage: "https://www.raindrop.ai/docs/integrations/openai-managed-agents",
2697
2730
  bugs: {
2698
- url: "https://docs.raindrop.ai/support"
2731
+ url: "https://www.raindrop.ai/docs/support"
2699
2732
  },
2700
2733
  exports: {
2701
2734
  ".": {
@@ -2766,14 +2799,18 @@ var libraryVersion = package_default.version;
2766
2799
  // src/wrap.ts
2767
2800
  import { Stream } from "openai/core/streaming";
2768
2801
  async function* observe(events, handler, sessionId) {
2802
+ if (sessionId) handler.onStreamStart(sessionId);
2769
2803
  try {
2770
2804
  for await (const event of events) {
2771
- if (event.type === "agent.session.created") sessionId = event.session.id;
2805
+ if (!sessionId && event.type === "agent.session.created") {
2806
+ sessionId = event.session.id;
2807
+ handler.onStreamStart(sessionId);
2808
+ }
2772
2809
  handler.onEvent(event);
2773
2810
  yield event;
2774
2811
  }
2775
2812
  } finally {
2776
- if (sessionId) handler.closeSessionTurns(sessionId);
2813
+ if (sessionId) handler.onStreamEnd(sessionId);
2777
2814
  }
2778
2815
  }
2779
2816
  function wrapManagedAgents(client, handler, context) {
@@ -2801,19 +2838,12 @@ function wrapManagedAgents(client, handler, context) {
2801
2838
  if (result instanceof Stream) {
2802
2839
  const source = result;
2803
2840
  async function* initialEvents() {
2804
- let sessionId;
2805
- try {
2806
- for await (const event of source) {
2807
- if (event.type === "agent.session.created") {
2808
- sessionId = event.session.id;
2809
- handler.onSession(event.session, context);
2810
- handler.onInput(event.session.id, params.input);
2811
- }
2812
- handler.onEvent(event);
2813
- yield event;
2841
+ for await (const event of observe(source, handler)) {
2842
+ if (event.type === "agent.session.created") {
2843
+ handler.onSession(event.session, context);
2844
+ handler.onInput(event.session.id, params.input);
2814
2845
  }
2815
- } finally {
2816
- if (sessionId) handler.closeSessionTurns(sessionId);
2846
+ yield event;
2817
2847
  }
2818
2848
  }
2819
2849
  return new Stream(initialEvents, result.controller);
@@ -2828,20 +2858,7 @@ function wrapManagedAgents(client, handler, context) {
2828
2858
  return session;
2829
2859
  });
2830
2860
  if (prop === "stream")
2831
- return (sessionId, params, options) => {
2832
- const stream = target.stream(sessionId, params, options);
2833
- return new Proxy(stream, {
2834
- get(obj, key) {
2835
- if (key === Symbol.asyncIterator)
2836
- return () => {
2837
- handler.onInput(sessionId, params.input);
2838
- return observe(stream, handler, sessionId);
2839
- };
2840
- const value2 = Reflect.get(obj, key, obj);
2841
- return typeof value2 === "function" ? value2.bind(obj) : value2;
2842
- }
2843
- });
2844
- };
2861
+ return (sessionId, params, options) => target.stream.call(wrappedSessions, sessionId, params, options);
2845
2862
  const value = Reflect.get(target, prop, target);
2846
2863
  return typeof value === "function" ? value.bind(target) : value;
2847
2864
  }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@raindrop-ai/openai-managed-agents",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Raindrop tracing for the OpenAI Agents API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "license": "MIT",
9
- "homepage": "https://docs.raindrop.ai/integrations/openai-managed-agents",
9
+ "homepage": "https://www.raindrop.ai/docs/integrations/openai-managed-agents",
10
10
  "bugs": {
11
- "url": "https://docs.raindrop.ai/support"
11
+ "url": "https://www.raindrop.ai/docs/support"
12
12
  },
13
13
  "exports": {
14
14
  ".": {