Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix browser and cloudflare trace exports #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jun 10, 2025

Conversation

dkundel-openai
Copy link
Collaborator

@dkundel-openai dkundel-openai commented Jun 6, 2025

Both Cloudflare and the Browser should not run export loops by default.

This PR:

  1. Properly disables the export loop in Cloudflare Workers by default. In the browser it was already disabled.
  2. Provides a way to start the export loop with startTraceExportLoop and a way to forceFlush the traces (for Cloudflare)
  3. Adds some integration tests to test if the SDK runs in the browser

Copy link

changeset-bot bot commented Jun 6, 2025

🦋 Changeset detected

Latest commit: 08508ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@openai/agents-core Patch
@openai/agents-openai Patch
@openai/agents-realtime Patch
@openai/agents Patch
@openai/agents-extensions Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dkundel-openai dkundel-openai marked this pull request as draft June 6, 2025 21:25
@dkundel-openai
Copy link
Collaborator Author

This should fix #16 @threepointone if you want to take a look

This should also further fix #10 removing the warning log

@dkundel-openai dkundel-openai requested a review from rm-openai June 6, 2025 21:59
@dkundel-openai dkundel-openai marked this pull request as ready for review June 6, 2025 21:59
@threepointone
Copy link
Contributor

I'll have a look this weekend! thanks so much @dkundel-openai, legend.

@seratch seratch added the bug Something isn't working label Jun 10, 2025
@threepointone
Copy link
Contributor

Ok, I think I have a clean solution without mucking about ALS etc. tldr -

  • let's not run startTraceExportLoop(): this has the tendency to keep the worker running because there will always be a running timer
  • events start getting flushed anyway when the batch size is hit (because of BatchProcessor::#safeAddItem
  • cloudflare users will be expected to call forceFlush() manually (added as a method to TraceProvider
  • we (cloudflare/me) can probably have a better integration with out own Agent class that sets this up automatically / removes some boilerplate.
diff --git a/integration-tests/cloudflare-workers/worker/src/index.ts b/integration-tests/cloudflare-workers/worker/src/index.ts
index c1d5c72..c132ab8 100644
--- a/integration-tests/cloudflare-workers/worker/src/index.ts
+++ b/integration-tests/cloudflare-workers/worker/src/index.ts
@@ -31,7 +31,6 @@ export default {
     try {
       setDefaultOpenAIKey(env.OPENAI_API_KEY!);
       setTraceProcessors([new BatchTraceProcessor(new ConsoleSpanExporter())]);
-      startTraceExportLoop();
 
       const agent = new Agent({
         name: 'Test Agent',
@@ -40,13 +39,13 @@ export default {
       });
       const result = await run(agent, 'Hey there!');
 
-      // make sure you shut down tracing before exiting to flush any remaining traces
-      await getGlobalTraceProvider().shutdown();
-
       return new Response(`[RESPONSE]${result.finalOutput}[/RESPONSE]`);
     } catch (error) {
       console.error(error);
       return new Response(String(error), { status: 500 });
+    } finally {
+      // make sure to flush any remaining traces before exiting
+      ctx.waitUntil(getGlobalTraceProvider().forceFlush());
     }
   },
 } satisfies ExportedHandler<Env>;
diff --git a/packages/agents-core/src/tracing/provider.ts b/packages/agents-core/src/tracing/provider.ts
index ecbea0a..1b01c0b 100644
--- a/packages/agents-core/src/tracing/provider.ts
+++ b/packages/agents-core/src/tracing/provider.ts
@@ -204,6 +204,10 @@ export class TraceProvider {
       });
     }
   }
+
+  async forceFlush(): Promise<void> {
+    await this.#multiProcessor.forceFlush();
+  }
 }
 
 let GLOBAL_TRACE_PROVIDER: TraceProvider | undefined = undefined;

dkundel-openai and others added 11 commits June 10, 2025 09:15
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* docs: reset search buttons

* docs: fix title link

* docs: change line-height of code snippets

* docs: color typescript on landing page

* docs: change navbar icons to contain text

* docs: further design revisions

* docs: add github and npm link
* Continue agent execution when function calls are pending

* Add tests

* Add changeset for tool execution fix

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>

* Fix mock object

---------

Co-authored-by: Claude <[email protected]>
* feat(realtime): add changePeerConnection option

* feat(realtime): finalize changePeerConnection hook

* Update change-peer-connection.md
* feat(openai): pass through unknown hosted tools

* Create rich-buses-marry.md
@dkundel-openai dkundel-openai merged commit 36a401e into main Jun 10, 2025
5 checks passed
@dkundel-openai dkundel-openai deleted the dkundel/change-export-loop branch June 10, 2025 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package:agents-core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants