Skip to content

fix: make the web build bundle and run in a browser - #614

Merged
AmaadMartin merged 5 commits into
mainfrom
fix/web-build-parseable-and-node-free
Sep 10, 2026
Merged

AmaadMartin merged 5 commits into
mainfrom
fix/web-build-parseable-and-node-free

Conversation

@AmaadMartin

@AmaadMartin AmaadMartin commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable):

Closes: #608
Closes: #609
Closes: #611
Closes: #612
Related: #607

  1. Or, if no issue exists, describe the change:

Problem: @google/adk advertises browser support, but core/dist/web cannot be consumed by a browser bundler. Bundling dist/web/index_web.js with --platform=browser produced 346 errors, one of which was a syntax error in a shipped file, so no bundler configuration could work around it. Four separate defects:

  1. models/apigee_llm.js did not parse. The browser target named chrome58/safari11, which predate native async generators, so esbuild downlevelled yield* super.generateContentAsync(...) into an __asyncGenerator closure — where super is not valid. Unavoidable via LlmAgent → models/registry → apigee_llm.
  2. Every file in dist/web carried the Node createRequire banner, because it was gated on format === 'esm' and the web build is also ESM. 187 files imported module; none called require().
  3. winston and node:async_hooks were imported by the web build. A shim for node:async_hooks already existed but was gated behind --bundle, which npm run build does not pass, so it never reached a published artifact.
  4. common.ts had accumulated exports reaching node:fs, node:dns, adm-zip, google-auth-library and @google-cloud/vertexai, and index_web.ts re-exports it wholesale.

Solution:

  1. Raise the browser target to chrome63/safari12, the first versions with native async generators. Verified each target individually: chrome58 and safari11 downlevel and break, chrome63 and safari12 do not.
  2. Gate the createRequire banner on platform === 'node' as well as format. dist/esm keeps it, dist/web does not.
  3. Bundle the web target always. esbuild rejects alias without bundling, so this is what makes the shim mechanism work at all. The emitted filename follows the entry, so "browser": "./dist/web/index_web.js" still resolves. Adds utils/winston_shim.ts, implementing only the surface utils/logger.ts uses and writing to the console — that is what a browser log transport does.
  4. Move the Node-only exports from common.ts to index.ts. This follows the layout already in place: index.ts holds the Node-only exports plus export * from './common.js', and index_web.ts is common.js alone, so common.ts is already intended to be the browser-safe surface. The moved set was derived, not chosen — all 199 source modules were bundled for the browser and the ones reaching a Node-only dependency recorded. list_skills_tool, load_skill_tool and search_skills_tool were checked individually and are browser-safe, so they stay.

Result: dist/web/index_web.js bundles for --platform=browser with zero errors, down from 346, and the bundle runs.

bundled for --platform=browser before after
dist/web/index_web.js 346 errors 0
Unexpected "super" 1 0
Could not resolve "module" 147 0

The Node public API is unchanged. dist/esm/index.js exports the same 206 symbols before and after, verified by diffing the resolved export lists from a build of origin/main against this branch.

Testing Plan

Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

tests/integration/build_setup/web_build_test.ts is new; the build output had no test coverage at all, which is how all four defects shipped. Eight tests:

  • dist/web contains no createRequire banner, and dist/esm still does — the second guards against "fixing" the browser build by deleting the banner outright
  • every emitted file parses (esbuild.transform)
  • the path in the browser field exists
  • the entire web entry bundles for --platform=browser, then is imported and driven: LlmAgent, SequentialAgent, ParallelAgent, LoopAgent, FunctionTool, Runner, InMemorySessionService and BaseLlm are all present; an LlmAgent is constructed with a FunctionTool, a session is created, and the tool is executed
  • the Node-only surface (loadAllSkillsInDir, OpenAPIToolset, UnsafeLocalCodeExecutor, GcsArtifactService, VertexAiMemoryBankService, loadWebPage) is absent from the browser entry

Ran locally:

unit:core + unit:dev    2579 passed, 1 failed
                        (dev/test/cli/cli_create_test.ts — reads real gcloud
                        config, fails on main too, unrelated)
integration (new file)  8 passed
npm run docs:check      clean (typedoc --treatWarningsAsErrors)
prettier --check        clean
eslint                  clean

Mutation check (proving the tests can fail): reverting only the common.ts hunk and rebuilding makes the bundle step fail with 173 unresolved Node imports — node:dns/promises, node:net, node:fs/promises, node:path, and fs via @google-cloud/vertexai. vitest exits 1 and reports Test Files 1 failed. Reverting only the build.js hunk fails 6 of 8, naming models/apigee_llm.js as unparseable and listing all 198 bannered files.

Manual End-to-End (E2E) Tests:

npm ci && npm run build
npx esbuild core/dist/web/index_web.js --bundle --platform=browser \
  --format=esm --target=chrome138 --outfile=/tmp/adk.mjs   # 0 errors

node --input-type=module -e "
const adk = await import('/tmp/adk.mjs');
const tool = new adk.FunctionTool({name:'add', description:'adds',
  parameters:{type:'object',properties:{a:{type:'number'},b:{type:'number'}},required:['a','b']},
  execute: async ({a,b}) => ({sum:a+b})});
const agent = new adk.LlmAgent({name:'browser_agent', model:'gemini-2.0-flash',
  instruction:'You are a calculator.', tools:[tool]});
console.log(agent.name, agent.tools.length,
  JSON.stringify(await tool.runAsync({args:{a:2,b:3}, context:undefined})));
"
# browser_agent 1 {"sum":5}

Emitted bundle is 166 KB.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Notes for the reviewer

Comment thread core/build.js Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make bundle always true for web?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question — I tried it, and the short answer is that it doesn't get us there on its own and it costs us three things.

What bundle: true for web fixes. It makes alias usable. esbuild rejects alias outright without bundling (Cannot use "alias" without "bundle"), which is why the existing node:async_hooks shim is gated on bundle and therefore never reaches published output. With bundling on, that shim applies, and adding a winston alias clears winston too.

What it doesn't fix. packages: 'external' keeps node_modules external, so after bundling and both shims, dist/web/index.js still imports:

node:fs/promises, node:path, node:net, node:dns/promises,
adm-zip, google-auth-library

Those come from the barrel pulling skills/loader (#612) and from node-only packages. So bundling alone doesn't produce a browser-usable artifact.

What it costs.

  1. dist/web/index_web.js stops existing — output becomes dist/web/index.js — so "browser": "./dist/web/index_web.js" in package.json points at a missing file.
  2. Deep imports die. dist/web/agents/llm_agent.js and friends disappear, and importing deep modules is currently the only way to consume the web build at all, precisely because the barrel is unbundleable. It would remove today's workaround before the replacement works.
  3. minify: bundle and sourcemap: bundle are tied to the same flag, so the web build silently becomes minified.

So: I think bundling is likely part of the eventual answer for #611, but as a package it's a bigger change with its own regressions and it needs #612 resolved to actually pay off. I'd rather not fold it into this PR, which is deliberately scoped to the two mechanical defects (unparseable file, Node banner) — that keeps this one easy to reason about and revert.

I've written the four options up on #611, including this one with the measurements above. Happy to take whichever you prefer as a follow-up. My own preference is dropping the winston dependency instead: utils/logger.ts uses it for a level filter and a printf, which is about 40 lines of console code, and that removes the problem at the source for every platform without changing the build shape at all.

One correction I owe you while we're here: my original writeup on #611 claimed the && bundle gate was an oversight and proposed removing it. That was wrong — it's load-bearing, for the reason above. I've posted a retraction on the issue.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — dist/web/index_web.js now bundles for --platform=browser with zero errors, down from 346, and the test drives the whole thing.

You were right about bundling, and my earlier reply was wrong on the substance. I said it "doesn't get us there on its own and costs three things". Two of those three evaporate once the barrel is fixed:

  • the missing index_web.js was self-inflicted — the outfile was hardcoded to index.js; making it follow the entry keeps the browser field resolving
  • losing deep imports only mattered because the barrel was unbundleable; once it bundles, nobody needs them

So the web target is now always bundled, which is what makes alias usable and lets the node:async_hooks shim actually reach a published artifact for the first time.

What it took beyond that:

  • a winston shim (utils/winston_shim.ts) — utils/logger.ts drags winston into every module that logs, and winston needs os/fs/util/zlib/http
  • moving the Node-only exports out of common.ts

On the second one: I didn't hand-pick that list. I bundled all 199 source modules for the browser and recorded which reach a Node-only dependency, then moved exactly those. It turned out to be a small set — skills/loader, skills/gcp_skill_registry, tools/openapi_tool/*, tools/skill/{load_skill_resource,skill_toolset}, tools/load_web_page, and memory/vertex_ai_memory_bank_service. I checked list_skills_tool, load_skill_tool and search_skills_tool individually — they're browser-safe, so they stayed.

This also turned out not to be a new structure. index.ts already holds the Node-only exports plus export * from './common.js', and index_web.ts is common.js alone — so common.ts was already meant to be the browser-safe surface, and things had just leaked into it. The moved exports land next to the ones already in index.ts.

The Node public API is unchanged: 206 exports from dist/esm/index.js before and after, verified by diffing resolved export lists against a build of origin/main.

The test now covers the entire bundle, per your comment on the other thread — it bundles index_web.js, imports it, and constructs an LlmAgent with a FunctionTool, creates a session and executes the tool. Full detail in the PR description.

Two things I'd flag rather than bury:

  1. dist/web is now a single minified index_web.js instead of a file tree, so @google/adk/dist/web/agents/llm_agent.js no longer resolves. The exports map doesn't expose those paths anyway (package.json exports has no browser condition, so bundlers resolve the Node build and the browser field is ignored #610), and they were only a workaround for this exact bug, but it is a real change.
  2. The browser entry no longer exports the Node-only surface. Nothing that previously worked is lost — none of it could run in a browser — but it is a narrower public surface for the web build specifically.

Happy to split the barrel move into its own commit or PR if you'd rather review it separately; it's already an isolated commit (a6192c9) so it lifts out cleanly.

@kalenkevich kalenkevich Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create another test that will spin up real html page that will include compiled ADK with some simple test to see that everything works and nothing is really broken?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and I've pushed a version of this in d84b1ce.

The new web build output is usable by a browser bundler block takes entry points from dist/web, bundles them with --platform=browser, imports the result, and exercises it for real:

const tool = new FunctionTool({name: 'add', ..., execute: async ({a, b}) => ({sum: a + b})});
expect(isFunctionTool(tool)).toBe(true);
expect(tool._getDeclaration()?.parameters).toEqual({...});   // what a model actually receives
expect(await tool.runAsync({args: {a: 2, b: 3}, context: undefined})).toEqual({sum: 5});

So it's compiled browser output being constructed and executed, not just parsed. I verified all three new tests fail against an unpatched build.

Two things I want to flag rather than quietly decide:

1. The entry-point list is short, and that's the real finding. Right now only events/event.js and tools/function_tool.js bundle with zero errors. Here's the current state with this PR applied:

entry bundle errors
events/event.js 0
tools/function_tool.js 0
models/base_llm.js 1
sessions/in_memory_session_service.js 20
agents/llm_agent.js 21
runner/runner.js 21
index_web.js 198

The 20–21 are winston + node:async_hooks (#611); the 198 adds the barrel's skills/loader (#612). So a test that loads everything can't pass until those land — I've left a comment in the file saying to add agents/llm_agent.js and runner/runner.js as they become bundleable, since each is one dependency away.

2. It's not literally an HTML page in a browser, and that was a deliberate call. The repo has no browser test tooling today — no puppeteer, playwright or jsdom, and every vitest project is environment: 'node'. A true headless-browser test means adding a browser dependency that runs on all three CI OSes, which felt like your call rather than something to slip into a bugfix PR.

What's committed gets most of the value with no new dependency: it proves the compiled artifact bundles, imports and behaves. What it can't catch is anything genuinely browser-specific — a real window/DOM dependency, or code that only breaks under a browser's module loader.

If you'd like the real thing, I'm happy to add it. My suggestion would be to do it once #611 and #612 land, so the page can load the actual barrel and assert something meaningful end to end, and to open it as its own PR given it's a new devDependency. Say the word and I'll file the issue.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood — done. The test now bundles the entire web entry rather than a subset.

I'd scoped it to two modules because that was all that bundled cleanly at the time. That was treating the symptom, so I fixed the remaining two blockers instead (winston + node:async_hooks, and the Node-only exports leaking into common.ts) — details on the other thread and in the PR description.

tests/integration/build_setup/web_build_test.ts now does:

await esbuild.build({
  entryPoints: [WEB_ENTRY],          // core/dist/web/index_web.js — the whole bundle
  bundle: true, format: 'esm', platform: 'browser', target: 'chrome138',
});

then imports the result and drives the public API:

  • LlmAgent, SequentialAgent, ParallelAgent, LoopAgent, FunctionTool, Runner, InMemorySessionService, BaseLlm are all present and callable
  • an LlmAgent is constructed with a FunctionTool, a session is created, and the tool is executed — {sum: 5}
  • the Node-only surface (loadAllSkillsInDir, OpenAPIToolset, UnsafeLocalCodeExecutor, GcsArtifactService, VertexAiMemoryBankService, loadWebPage) is asserted absent from the browser entry, so the barrel can't silently re-acquire a node:fs dependency

The bundle step is in beforeAll, so if the web build ever stops bundling the whole file fails rather than skipping — I checked that specifically: reverting the common.ts hunk gives 173 unresolved Node imports, vitest exits 1, Test Files 1 failed.

On the "real html page" part specifically: this stops just short of a browser engine. It proves the bundle builds, imports and behaves, but it can't catch something genuinely browser-only — a real window/DOM dependency, or a failure specific to a browser's module loader. Closing that gap means a headless-browser devDependency (there's no puppeteer/playwright/jsdom in the repo today, and every vitest project is environment: 'node'), which felt like your call rather than mine to make inside a bugfix PR.

Now that the bundle actually works, that test would be straightforward — load index_web.js in a page and run the same agent construction. Happy to add it here if you want it, or file it as a follow-up. Just say which.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, and this is now built.

tests/integration/build_setup/web_app/ is a real page — message list, prompt input, send button — that imports core/dist/web/index_web.js directly with no build step of its own. web_app_test.ts drives it in headless Chromium via Playwright:

✓ loads the shipped bundle without a module or runtime error
✓ shows the user message and the agent reply after clicking send
✓ runs a second turn on the same session

It types into the input, clicks send, waits for li[data-author="agent"], and asserts the rendered list is exactly:

[{author: 'user',  text: 'hello agent'},
 {author: 'agent', text: 'Hello from the agent'}]

The model is a BaseLlm subclass returning a canned response — the same shape as the mock in tests/integration/build_setup/ts_esm/agent.ts. pageerror and console errors are collected and asserted empty, so a DOM reference or a module-loading failure fails the test rather than passing quietly.

You were right about the synthetic-ness, and I'd add a specific reason my previous test could not have caught real issues: it imported browser-targeted code into Node. Anything that only breaks under a browser's module loader, or touches window/document, would have passed it. That gap was the whole point of the test.

One thing this surfaced that I'd missed. dist/web/index_web.js was bundled but still had packages: 'external', so it emitted bare specifiers (@google/genai, zod, lodash-es) that a browser cannot resolve — the page could not have loaded it. The web target now bundles its dependencies and is genuinely self-contained at 972 KB. Node keeps packages: 'external' unchanged. That is also what makes your other comment satisfiable: the test no longer bundles anything.

Agreed on expanding from here — streaming, tool calls and multi-agent are the obvious next ones, and the fixture is set up so each is a new page interaction rather than new plumbing.

* and `runner/runner.js` are the two worth having next, and both are one
* dependency away.
*/
describe('web build output is usable by a browser bundler', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we need to test entire ADK TS bundle for web.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed — the test now bundles and runs the entire dist/web/index_web.js, not a subset. See the thread above for what it took (the two remaining blockers had to be fixed, which they now are) and the PR description for the full testing plan.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — and you were right that it was circular. dist/web is the bundle; re-bundling it in the test only exercised my esbuild invocation.

Removed. The browser test now loads core/dist/web/index_web.js straight from a <script type="module"> with no build step.

That only actually works because of a related fix: the web output had packages: 'external', so it still emitted bare specifiers a browser cannot resolve. It is now self-contained, so "already bundled" is true in the sense you meant it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — moved to tests/integration/build_setup/node_build_test.ts, which covers the ESM and CJS builds:

  • the ESM build still emits the createRequire banner
  • dist/cjs/package.json declares "type": "commonjs"
  • the Node builds still import winston (see below)

web_build_test.ts is now dist/web only.

@AmaadMartin AmaadMartin changed the title Fix: make dist/web parseable and free of the Node createRequire banner Fix: make the web build bundle and run in a browser Aug 4, 2026
) as {browser?: string};
expect(pkg.browser).toBeDefined();
const target = path.join(process.cwd(), 'core', pkg.browser!);
await expect(fs.access(target)).resolves.toBeUndefined();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a bug. Should it be defined instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — it was confusing even though it happened to pass. fs.access resolves to undefined on success, so .resolves.toBeUndefined() was technically correct, but it reads like an inverted assertion and would silently keep passing if the call were swapped for something else.

Replaced with something that says what it means:

const stat = await fs.stat(target);
expect(stat.isFile()).toBe(true);
expect(stat.size).toBeGreaterThan(0);

That also asserts more than the old version did: the path exists, is a file, and is non-empty.


it('still emits the createRequire banner for the Node ESM build', async () => {
// Guards against fixing the browser build by dropping the banner outright.
const nodeFiles = await collectJsFiles(NODE_ESM_DIST);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it make sense to create a separate test that will test it against the ESM and CJS builds. So please drop this test from the web test

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — moved to tests/integration/build_setup/node_build_test.ts, which covers the ESM and CJS builds:

  • the ESM build still emits the createRequire banner
  • dist/cjs/package.json declares "type": "commonjs"
  • the Node builds still import winston (see below)

web_build_test.ts is now dist/web only.

Comment on lines +117 to +125
await esbuild.build({
entryPoints: [WEB_ENTRY],
outfile: bundlePath,
bundle: true,
format: 'esm',
platform: 'browser',
target: 'chrome138',
logLevel: 'silent',
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should already test agains the bundled ADK TS. IT does not make sense to bundle it in the test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — and you were right that it was circular. dist/web is the bundle; re-bundling it in the test only exercised my esbuild invocation.

Removed. The browser test now loads core/dist/web/index_web.js straight from a <script type="module"> with no build step.

That only actually works because of a related fix: the web output had packages: 'external', so it still emitted bare specifiers a browser cannot resolve. It is now self-contained, so "already bundled" is true in the sense you meant it.

Comment on lines +134 to +206
it('bundles the entire web entry point for the browser', async () => {
const stat = await fs.stat(bundlePath);
expect(stat.size).toBeGreaterThan(0);
});

it('exposes the core primitives once bundled', async () => {
const adk = await import(pathToFileURL(bundlePath).href);
for (const symbol of [
'LlmAgent',
'SequentialAgent',
'ParallelAgent',
'LoopAgent',
'FunctionTool',
'Runner',
'InMemorySessionService',
'BaseLlm',
]) {
expect(typeof adk[symbol], symbol).toBe('function');
}
});

it('excludes the Node-only surface from the browser entry point', async () => {
const adk = await import(pathToFileURL(bundlePath).href);
// These reach node:fs, child_process, google-auth-library or
// @google-cloud/vertexai, and are exported from index.ts for Node only.
for (const symbol of [
'loadAllSkillsInDir',
'OpenAPIToolset',
'UnsafeLocalCodeExecutor',
'GcsArtifactService',
'VertexAiMemoryBankService',
'loadWebPage',
]) {
expect(adk[symbol], symbol).toBeUndefined();
}
});

it('builds a working agent and tool from the bundle', async () => {
const adk = await import(pathToFileURL(bundlePath).href);

const tool = new adk.FunctionTool({
name: 'add',
description: 'adds two numbers',
parameters: {
type: 'object',
properties: {a: {type: 'number'}, b: {type: 'number'}},
required: ['a', 'b'],
},
execute: async ({a, b}: {a: number; b: number}) => ({sum: a + b}),
});

const agent = new adk.LlmAgent({
name: 'browser_agent',
model: 'gemini-2.0-flash',
instruction: 'You are a calculator.',
tools: [tool],
});
expect(agent.name).toBe('browser_agent');
expect(agent.tools).toHaveLength(1);

const sessions = new adk.InMemorySessionService();
const session = await sessions.createSession({
appName: 'browser_app',
userId: 'user',
});
expect(session.id).toBeTruthy();

const result = await tool.runAsync({
args: {a: 2, b: 3},
context: undefined,
});
expect(result).toEqual({sum: 5});
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not what we really want to have. This kind of tests are very synthetic and might not catch real issues.

What I propose is to create a real HTML page that will include complied (and bundled) ADK TS from dist and RUN some REAL browser (headless) test.

For example the HTML page where messages, prompt input and send button. When user puts something in the input and clicks send button, their message should appear in the message list and agent should respond with some message. Test should verify that it is actually working.
The real model response can be mocked with predefined result (in the same way we do that for our integration tests).

So such kind of test will ensure that ADK TS for web compiled and bundled. It can be imported and used in the real web app and it will provide some basic functionality.

Once this is done, we can expand this testcases to test more ADK TS features on web.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, and this is now built.

tests/integration/build_setup/web_app/ is a real page — message list, prompt input, send button — that imports core/dist/web/index_web.js directly with no build step of its own. web_app_test.ts drives it in headless Chromium via Playwright:

✓ loads the shipped bundle without a module or runtime error
✓ shows the user message and the agent reply after clicking send
✓ runs a second turn on the same session

It types into the input, clicks send, waits for li[data-author="agent"], and asserts the rendered list is exactly:

[{author: 'user',  text: 'hello agent'},
 {author: 'agent', text: 'Hello from the agent'}]

The model is a BaseLlm subclass returning a canned response — the same shape as the mock in tests/integration/build_setup/ts_esm/agent.ts. pageerror and console errors are collected and asserted empty, so a DOM reference or a module-loading failure fails the test rather than passing quietly.

You were right about the synthetic-ness, and I'd add a specific reason my previous test could not have caught real issues: it imported browser-targeted code into Node. Anything that only breaks under a browser's module loader, or touches window/document, would have passed it. That gap was the whole point of the test.

One thing this surfaced that I'd missed. dist/web/index_web.js was bundled but still had packages: 'external', so it emitted bare specifiers (@google/genai, zod, lodash-es) that a browser cannot resolve — the page could not have loaded it. The web target now bundles its dependencies and is genuinely self-contained at 972 KB. Node keeps packages: 'external' unchanged. That is also what makes your other comment satisfiable: the test no longer bundles anything.

Agreed on expanding from here — streaming, tool calls and multi-agent are the obvious next ones, and the fixture is set up so each is a new page interaction rather than new plumbing.

@ScottMansfield

Copy link
Copy Markdown
Member

Please rebase and fix conflicts. let's get this landed so we can close ~5 issues.

@AmaadMartin AmaadMartin changed the title Fix: make the web build bundle and run in a browser fix: make the web build bundle and run in a browser Aug 21, 2026

@ScottMansfield ScottMansfield left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. I verified it end to end rather than reading for it.

The headline claim holds, and the bug is as bad as #607 says. On origin/main, the shipped browser entry cannot even be loaded:

$ node -e "import('core/dist/web/index_web.js')"
SyntaxError: 'super' keyword unexpected here

That is #608 reproducing exactly. On this branch the same entry loads, and LlmAgent / FunctionTool / Runner are all live functions.

What I confirmed on a clean build of ca68ecb:

Claim Result
dist/web bundles for --platform=browser 0 errors
createRequire banner 0 files in dist/web, 248 still in dist/esm
node:* / winston / module left in the web bundle none
Node public API unchanged 257 exports, identical — zero lost, zero gained
build_setup suite 39 passed, incl. real headless Chromium driving two agent turns
ts:check / lint / format:check / docs:check all clean

I also mutation-tested the part I trusted least. Re-adding any of the four moved Node-only exports to common.ts — OpenAPIToolset, loadWebPage, loadAllSkillsInDir, VertexAiMemoryBankService — fails the build outright (2–6 unresolved-import errors each). So the common.ts pruning is guarded by something stronger than a test: you cannot produce the artifact if it regresses. And the two moves I spot-checked are justified on the merits — SkillToolset imports node:fs/promises, node:os and node:path; LoadSkillResourceTool imports node:path and SkillToolset.

The "browser entry no longer exports the Node-only surface" caveat in your notes is, I think, weaker than you're giving yourself credit for: the browser entry on main doesn't parse, so nothing that worked is lost. You can't regress a surface that never loaded.

The Playwright test is the right call. Loading the actual shipped artifact in a real browser is what would have caught all four defects, and it's the piece that makes this durable rather than a one-time cleanup.


Two things I'd want fixed before this lands

1. Browser TypeScript consumers now type-check against a surface the runtime doesn't have. The exports map serves ./dist/types/index.d.ts — the Node types — under every condition, including browser. Verified with customConditions: ["browser"]: importing OpenAPIToolset, loadWebPage and VertexAiMemoryBankService compiles with exit 0, and at runtime all three are undefined in the browser bundle. This PR introduces the divergence — before it, common.ts had those symbols so types and runtime agreed. One-line fix, details inline on common.ts.

2. ./dist/web/* now resolves to files that don't exist. #618 added that subpath eight days ago, specifically so consumers could deep-import from dist/web. Collapsing the tree into one bundle means @google/adk/dist/web/agents/llm_agent.js still resolves through the exports map and then 404s — a hard bundler error. Your note says "the exports map does not expose them anyway (#610)", which was true when you wrote it and stopped being true when you merged main. Details inline.

Three worth a decision, not necessarily a change

3. The bundle is 1,062 KB, not 166 KB. That's a 7x gap in a number reviewers are weighing the trade-off on. packages: 'bundle' inlines zod (395 KB) and @google/genai (338 KB) — 69% of the artifact is third-party; ADK's own code is 241 KB. The same build with packages: 'external' is 242 KB, which is close enough to your 166 KB that I suspect the figure predates the packages change. Inline comment argues both sides.

4. dist/web now ships a 4.9 MB sourcemap it never shipped before, because sourcemap: bundle fires now that the web target is always bundled. dist/web goes from a map-less tree to 5.9 MB, all of it inside files: ["dist"]. Landing right after a5cdc38's package-weight work, worth confirming it's intended.

5. @google/adk-integrations is still browser-broken. Untouched here; integrations/build.js:66 has the byte-identical if (format === 'esm') banner gate you fixed in core. All 3 files in integrations/dist/web carry the banner and its web entry bundles with 2 errors. #618 gave that package a browser export condition too, so it currently advertises browser support and ships an unbundleable artifact — the same defect class as #607, one package over. Fine as a follow-up, but it shouldn't be silent.

Housekeeping

6. The Testing Plan no longer matches the code. It describes eight tests in web_build_test.ts, including a --platform=browser bundle step and a "Node-only surface is absent from the browser entry" assertion. At head, web_build_test.ts has three tests and no test in the repo bundles for the browser. The reorganisation across b6eb7917 was an improvement, but the mutation-check paragraph now describes a suite that isn't there. Since a reviewer's confidence in the riskiest hunk rests on that paragraph, it should say what the code actually does — the build-level guard is a better story anyway.


None of the above changes my read that this is the right fix, correctly derived, and that the browser test is what makes it stick. Items 1 and 2 are small and mechanical; the rest are judgement calls I'd just like recorded.

Automated review · CloudCode session ses_e5fd9d69397ffe38frdSpP16Ue

Comment thread core/src/common.ts
Comment on lines 328 to 333

export * from './artifacts/base_artifact_service.js';
export * from './features/feature_registry.js';
export * from './memory/base_memory_service.js';
export * from './sessions/base_session_service.js';
export * from './tools/base_tool.js';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these from the browser runtime creates a type/runtime divergence, because the browser condition has no types of its own.

core/package.json (not touched by this PR, but this is what makes it bite):

".": {
  "types": "./dist/types/index.d.ts",
  "browser": "./dist/web/index_web.js",
  "import": "./dist/esm/index.js",
  ...
}

types resolves the same for every condition, so a browser consumer gets the Node declarations. Verified against a clean build of this branch with moduleResolution: "bundler" and customConditions: ["browser"]:

import {OpenAPIToolset, loadWebPage, VertexAiMemoryBankService} from '@google/adk';
tsc --traceResolution
  Using 'exports' subpath '.' with target './dist/types/index.d.ts'.
TSC EXIT=0

and at runtime, against dist/web/index_web.js:

LlmAgent                     function
FunctionTool                 function
Runner                       function
OpenAPIToolset               undefined
loadWebPage                  undefined
VertexAiMemoryBankService    undefined
loadAllSkillsInDir           undefined
GcsArtifactService           undefined

Clean compile, undefined is not a constructor at runtime — the worst shape for this kind of bug, because the type system is the only thing a browser consumer has to tell them the symbol is unavailable, and it actively says the opposite.

This is introduced by this PR. Before it, common.ts exported all of these, so index.d.ts and the web runtime agreed (both wrong, but agreeing). tsc already emits the correct dist/types/index_web.d.ts — it's just never served.

Fix is a one-liner in the exports map, with types nested inside the browser condition so it wins for that resolver:

".": {
  "browser": {
    "types": "./dist/types/index_web.d.ts",
    "default": "./dist/web/index_web.js"
  },
  "types": "./dist/types/index.d.ts",
  "import": "./dist/esm/index.js",
  "require": "./dist/cjs/index.js",
  "default": "./dist/esm/index.js"
}

Ordering matters: condition objects are matched top-down, so browser has to precede the bare types, and types has to be first inside it. Worth an assertion in package_resolution_test.ts too — it already drives Node's real resolver with --conditions=browser, so it's a natural home.

Comment thread core/build.js
Comment on lines +125 to +132
// The web target is always bundled. Node built-ins can only be swapped
// for browser shims through esbuild's `alias`, which requires bundling.
build({
targetDir: 'web',
platform: 'browser',
format: 'esm',
entry: 'index_web.ts',
bundle,
bundle: true,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This collapses dist/web to a single file, which breaks the ./dist/web/* subpath that #618 added eight days ago.

Your reviewer note says deep imports "were only ever a workaround for the barrel being unbundleable, and the exports map does not expose them anyway (#610)". That was accurate when you wrote it. Then #610 was fixed by your own #618 (0f9f73d, Aug 13), which added exactly this:

"./dist/web/*": {
  "types": "./dist/types/*",
  "default": "./dist/web/*"
}

and ca68ecb merged it in. So the map now does expose them. Measured on this branch:

entry : @google/adk/dist/web/index_web.js        resolves
deep  : @google/adk/dist/web/agents/llm_agent.js  resolves-but-MISSING
deep  : @google/adk/dist/web/tools/function_tool.js  resolves-but-MISSING
deep  : @google/adk/dist/web/runner/runner.js    resolves-but-MISSING

resolves-but-MISSING is the bad case: the pattern matches, so the resolver hands the bundler a path, and the bundler then hard-errors on a missing file. A consumer who adopted the #618 workaround gets a build break with no deprecation.

In practice the blast radius is small — the workaround existed for about a week, and the whole point of this PR is that the barrel now works so nobody needs it. But the subpath should stop advertising a tree that no longer exists. Either drop ./dist/web/* from the exports map in this PR, or keep it and say so in the release notes. Dropping it is cleaner, and package_resolution_test.ts's exposes the web build under the ./dist/web/* subpath case only exercises index_web.js, so it keeps passing either way — which is itself worth knowing, since that test won't catch this.

Same question applies to "types": "./dist/types/*" in that block: for index_web.js it maps to ./dist/types/index_web.js, which isn't a .d.ts and doesn't exist. Pre-existing from #618, but now is a natural time.

Comment thread core/build.js
packages: 'external',
// The web target ships a self-contained bundle so a browser can load it
// directly; bare specifiers like '@google/genai' are not resolvable there.
packages: platform === 'browser' ? 'bundle' : 'external',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth stating this trade-off explicitly — the emitted bundle is 1,062 KB, not the 166 KB in the PR body.

Measured on a clean build of this branch: core/dist/web/index_web.js is 1,134,820 bytes on disk, 1,061,810 minified through esbuild. Attribution from a metafile:

  395,115  zod
  338,059  @google/genai
  241,248  adk source
   25,580  jsonpath-plus
   20,856  zod-to-json-schema
   18,237  lodash-es
   11,321  @opentelemetry/api

69% of the artifact is third-party. The same build with --packages=external is 242 KB — 4.3x smaller, and close enough to your 166 KB that I suspect the figure was measured before this line changed.

The argument for 'bundle' is in your comment and it's a real one: a bare @google/genai specifier can't be loaded by a browser directly. But that only matters for <script type="module"> against the raw file, which is the rarer case — a browser consumer almost always has a bundler, and for them inlining is actively worse:

  • zod can't be deduped against the copy they already have, so it's 395 KB twice
  • tree-shaking is defeated; importing one agent pulls all of @google/genai
  • the inlined zod version is frozen at ADK's, so a consumer can't resolve a conflict

packages: 'external' still works with the alias mechanism — alias needs bundle: true, not packages: 'bundle' — so you could keep every shim and ship 242 KB, at the cost of the direct-<script> story.

I don't think either choice is wrong. But 166 KB vs 1,062 KB is the difference between "obviously fine" and "worth a conversation", so the number in the body should be the real one. If 'bundle' is deliberate, the comment on this line is the right place to say why it beats external for the expected consumer.

Comment thread core/build.js
@@ -51,14 +56,19 @@ function build({
// mangling, so keep the original names in the bundle.
keepNames: true,
sourcemap: bundle,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side effect worth confirming is intended: sourcemap: bundle was previously false for the web target, because npm run build doesn't pass --bundle. Now that the web build is unconditionally bundled, this fires, and dist/web ships a 4.9 MB index_web.js.map.

1,134,820  core/dist/web/index_web.js
4,949,625  core/dist/web/index_web.js.map
         → dist/web total 5.9 MB, up from a map-less file tree

files: ["dist"] means that all goes into the published tarball. Landing directly after a5cdc38 ("591 → 172 packages on install"), a ~5 MB increase in package size seems worth a deliberate yes rather than a side effect of the bundle: true flip.

If you want the map for debuggability, sourcemap: 'linked' is already what you get; 'external' would at least keep it out of the default fetch path, and dropping it for the web target only is a one-word change. Whichever way, worth a line in the PR body since it's a user-visible packaging change.

Comment thread core/build.js Outdated
// Node-only. `module` has no browser equivalent, and the web build never
// calls require(), so emitting this into dist/web produced an unresolvable
// import in all ~187 of its files.
if (format === 'esm' && platform === 'node') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is correct and I verified both halves — 0 bannered files in dist/web, 248 still in dist/esm, with node_build_test.ts guarding the second so nobody "fixes" this by deleting the banner outright. Nice pairing.

Flagging that integrations/build.js:66 has the byte-identical gate and was not touched:

// integrations/build.js:66
if (format === 'esm') {
  buildOptions.banner = { ... createRequire ... }

Consequence, measured on this branch after a full npm run build:

integrations/dist/web: 3 js files, 3 of them bannered
$ npx esbuild integrations/dist/web/index_web.js --bundle --platform=browser
✘ [ERROR] Could not resolve "module"   integrations/dist/web/index_web.js:1:53
✘ [ERROR] Could not resolve "module"   integrations/dist/web/version.js:1:53
2 errors

That matters a bit more than "a second package has the same bug", because #618 also gave @google/adk-integrations a browser export condition. So it now points browser bundlers at an artifact that cannot be bundled — the exact shape of #607, one package over, and newly reachable because of the resolution fix.

Happy for it to be a follow-up rather than scope creep here (it's the same one-line change plus the target bump). But #607 is a tracking issue for "the package cannot be bundled for the browser", so it'd be worth either doing it here or filing it before this closes.

run: npm run ts:check:samples

- name: Install Playwright browser
run: npx playwright install --with-deps chromium

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two notes on putting this in the main validation job.

--with-deps installs OS packages and needs root — it fails outright without it, which I hit locally:

Switching to root user to install dependencies...
sudo: a terminal is required to read the password
Failed to install browsers

Fine on GitHub-hosted runners, but it makes the job non-reproducible on any self-hosted or container runner without passwordless sudo, and the failure mode is opaque. Plain npx playwright install chromium worked for me and is what the test actually needs, since it only launches headless Chromium.

Second, this now runs on every validation job, including doc-only and single-file PRs, and an uncached chromium download is a meaningful chunk of wall time. actions/cache on ~/.cache/ms-playwright keyed by the playwright version is the usual fix and is a few lines.

Neither blocks — the browser test is worth the cost, and I'd keep it. Just don't want the whole repo's CI to get slower by accident.

return out;
}

describe('web build output', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three static guards are well-scoped and the doc comment correctly points at web_app_test.ts for the behavioural half. No change requested here — this is about the PR body drifting from the code.

The Testing Plan still describes the pre-b6eb7917 layout: "Eight tests" in this file, including "the entire web entry bundles for --platform=browser" and "the Node-only surface (loadAllSkillsInDir, OpenAPIToolset, UnsafeLocalCodeExecutor, GcsArtifactService, VertexAiMemoryBankService, loadWebPage) is absent from the browser entry". At head:

web_build_test.ts   3 tests
node_build_test.ts  3 tests
web_app_test.ts     3 tests

grep for those six symbol names across tests/integration/build_setup/ → no matches
grep for a --platform=browser bundle step                            → no matches

So neither assertion exists any more, and the mutation-check paragraph ("reverting only the common.ts hunk … fails with 173 unresolved Node imports, vitest exits 1") no longer describes anything that runs.

I checked whether that actually leaves the riskiest hunk unguarded, and it mostly doesn't — re-adding each moved export to common.ts and rebuilding:

OpenAPIToolset             -> BUILD FAILS (6 errors)
LOAD_WEB_PAGE/loadWebPage  -> BUILD FAILS (2 errors)
loadAllSkillsInDir         -> BUILD FAILS (6 errors)
VertexAiMemoryBankService  -> BUILD FAILS (6 errors)

A regression can't produce an artifact at all, which is a better guarantee than a test asserting on one. So the substance is fine — please just update the body to describe it, because as written a reviewer is being pointed at evidence that isn't there.

One assertion would still earn its place, since the build-level guard only catches unresolvable imports and not a module that's pure JS but browser-hostile: import the built dist/web/index_web.js and assert those six names are undefined while LlmAgent/Runner are functions. It's ~6 lines here and pins the export surface directly.

Amaad Martin added 5 commits September 10, 2026 14:26
Two defects in the browser build output, both invisible to the existing
suite because it runs against src rather than the built artifacts.

1. `models/apigee_llm.js` did not parse. The browser target named
   chrome58/safari11, which predate native async generators, so esbuild
   downlevelled `yield* super.generateContentAsync(...)` into an
   __asyncGenerator closure and emitted `super` inside it. Raising the
   target to chrome63/safari12 keeps async generators native. This was a
   hard blocker: LlmAgent -> models/registry -> apigee_llm, so no browser
   consumer could avoid it, and no bundler configuration can parse invalid
   syntax.

2. Every file in dist/web carried the Node `createRequire` banner, because
   the banner was gated on `format === 'esm'` and the web build is also
   ESM. 187 files imported `module`; none ever called require().

Bundling dist/web/agents/llm_agent.js for the browser goes from 86 errors
to 21, and dist/web/index_web.js from 346 to 198. The remainder is winston
and node:async_hooks, tracked separately.

Adds a regression test over the build output, which is currently unguarded.
Verified it fails without this change, reporting all 198 bannered files and
naming models/apigee_llm.js as unparseable.

Fixes #608
Fixes #609
Part of #607
Addresses review feedback asking for a test that loads compiled ADK and
checks it actually works.

Parsing is necessary but not sufficient: it would not catch output that
bundles and then throws on import, or a primitive that is broken once
compiled for the browser. This bundles entry points from dist/web with
--platform=browser and runs them — constructing a FunctionTool, checking
the declaration a model would receive, and executing it.

The entry-point list is short because most of dist/web still cannot be
bundled: the barrel pulls skills/loader (node:fs, node:path) and the
logger pulls winston. agents/llm_agent.js and runner/runner.js are the
two worth adding next and are one dependency away each.

Verified all three new tests fail against an unpatched build.
Follows review feedback asking that the entire ADK TS web bundle be
tested, not a subset. Getting there needed the remaining two defects
fixed rather than deferred.

Bundle the web target always. esbuild rejects `alias` unless bundling,
so the existing node:async_hooks shim never reached published output --
`npm run build` does not pass --bundle. Bundling the web target makes
the alias mechanism work, and the emitted filename now follows the entry
so package.json's "browser" field still resolves.

Add a browser stand-in for winston. utils/logger.ts pulls winston into
every module that logs, and winston needs os/fs/util/zlib/http. The shim
implements only the surface logger.ts uses and writes to the console,
which is what a browser transport does.

Keep Node-only modules out of the web barrel. index.ts already holds the
Node-only exports and index_web.ts is common.ts alone, so common.ts is
meant to be the browser-safe surface -- but exports reaching node:fs,
node:dns, adm-zip, google-auth-library and @google-cloud/vertexai had
leaked into it. Those move to index.ts alongside the ones already there.
The set was derived by bundling all 199 source modules for the browser
and recording which reach a Node-only dependency, not chosen by hand.

core/dist/web/index_web.js now bundles for --platform=browser with zero
errors, down from 346. The bundle imports and drives the public API:
LlmAgent, ParallelAgent, SequentialAgent, LoopAgent, FunctionTool,
Runner, InMemorySessionService and BaseLlm are all present and working.

The Node public API is unchanged -- 206 exports from dist/esm/index.js
before and after, verified by diffing the resolved export lists.

Mutation check: reverting only the common.ts hunk makes the bundle step
fail with 173 unresolved Node imports, vitest exits 1 and reports the
file as failed.
…eb bundle

Replaces the synthetic bundle test with the real thing, per review.

tests/integration/build_setup/web_app/ is a small chat page -- message
list, prompt input, send button -- that imports core/dist/web/index_web.js
directly, with no build step of its own. web_app_test.ts drives it in
headless Chromium with Playwright: types a message, clicks send, and
asserts the user message and the agent reply both render, then runs a
second turn on the same session. The model is a BaseLlm subclass
returning a canned response, matching the mock in the existing
build_setup fixtures. Page errors and console errors fail the test, so a
DOM or module-loading failure surfaces instead of passing silently.

Making that possible meant the web target had to become genuinely
self-contained. It was bundled but kept `packages: 'external'`, so the
output still imported '@google/genai', 'zod' and friends as bare
specifiers, which a browser cannot resolve. The web target now bundles
its dependencies; Node keeps packages external as before. 972 KB.

Splits the build-output tests as requested: web_build_test.ts covers
dist/web only, and the Node ESM/CJS assertions move to node_build_test.ts.
Drops the assertions that re-bundled dist/web inside the test -- the
shipped artifact is already a bundle, and the browser test now exercises
it directly.

Adds playwright-chromium and a CI step to install the browser.
… platform

Rebasing onto main brought in #813, which keeps winston out of the
browser build by splitting the logger into logger_node (winston) and
logger_browser (console). The browser entry installs logger_browser and
never imports winston, so this PR's winston->shim alias and
winston_shim.ts are dead code and are removed.

node_build_test now reads utils/logger_node.js, where winston lives after
the split, instead of utils/logger.js.
@AmaadMartin
AmaadMartin force-pushed the fix/web-build-parseable-and-node-free branch from 0c89cc9 to ef6c1e4 Compare September 10, 2026 21:38
@AmaadMartin

Copy link
Copy Markdown
Collaborator Author

Rebased onto main; conflicts resolved at ef6c1e4. Two overlapping PRs had landed since: #837 (browser build target, createRequire banner fix, node:crypto/node:async_hooks shims) and #813 (logger split by platform). I kept this PR's unique parts — the always-bundled self-contained web target, moving the Node-only exports out of the browser barrel (common.ts -> index.ts, including VertexAiRagMemoryService, which main had newly added to the browser barrel), and the browser/build tests. I dropped the winston shim, because #813's logger_browser already keeps winston out of the browser build, so the shim was dead code. Build, ts:check, lint, format:check, docs:check and the three build_setup tests pass locally.

@AmaadMartin
AmaadMartin merged commit fbd7f5a into main Sep 10, 2026
14 of 15 checks passed
@AmaadMartin
AmaadMartin deleted the fix/web-build-parseable-and-node-free branch September 10, 2026 22:47
@kalenkevich kalenkevich mentioned this pull request Sep 10, 2026
AmaadMartin pushed a commit to AmaadMartin/adk-js that referenced this pull request Sep 11, 2026
…Node-free

The web target is now always bundled (google#614), and it must stay
free of Node built-ins. Registering OCIGenAILlm in models/registry.ts pulled
the OCI SDK into that bundle, because the browser entry reaches registry.ts
through common.ts and esbuild follows the SDK's dynamic imports. Register the
Node-only provider from index.ts instead, matching how every other Node-only
subsystem is exported only from the Node entry point.
AmaadMartin pushed a commit to AmaadMartin/adk-js that referenced this pull request Sep 19, 2026
…Node-free

The web target is now always bundled (google#614), and it must stay
free of Node built-ins. Registering OCIGenAILlm in models/registry.ts pulled
the OCI SDK into that bundle, because the browser entry reaches registry.ts
through common.ts and esbuild follows the SDK's dynamic imports. Register the
Node-only provider from index.ts instead, matching how every other Node-only
subsystem is exported only from the Node entry point.
AmaadMartin pushed a commit to AmaadMartin/adk-js that referenced this pull request Sep 25, 2026
…Node-free

The web target is now always bundled (google#614), and it must stay
free of Node built-ins. Registering OCIGenAILlm in models/registry.ts pulled
the OCI SDK into that bundle, because the browser entry reaches registry.ts
through common.ts and esbuild follows the SDK's dynamic imports. Register the
Node-only provider from index.ts instead, matching how every other Node-only
subsystem is exported only from the Node entry point.
prasanna8585 pushed a commit to prasanna8585/adk-js that referenced this pull request Nov 10, 2026
* Fix: make dist/web parseable and free of the Node createRequire banner

Two defects in the browser build output, both invisible to the existing
suite because it runs against src rather than the built artifacts.

1. `models/apigee_llm.js` did not parse. The browser target named
   chrome58/safari11, which predate native async generators, so esbuild
   downlevelled `yield* super.generateContentAsync(...)` into an
   __asyncGenerator closure and emitted `super` inside it. Raising the
   target to chrome63/safari12 keeps async generators native. This was a
   hard blocker: LlmAgent -> models/registry -> apigee_llm, so no browser
   consumer could avoid it, and no bundler configuration can parse invalid
   syntax.

2. Every file in dist/web carried the Node `createRequire` banner, because
   the banner was gated on `format === 'esm'` and the web build is also
   ESM. 187 files imported `module`; none ever called require().

Bundling dist/web/agents/llm_agent.js for the browser goes from 86 errors
to 21, and dist/web/index_web.js from 346 to 198. The remainder is winston
and node:async_hooks, tracked separately.

Adds a regression test over the build output, which is currently unguarded.
Verified it fails without this change, reporting all 198 bannered files and
naming models/apigee_llm.js as unparseable.

Fixes google#608
Fixes google#609
Part of google#607

* test: bundle and execute the compiled web output, not just parse it

Addresses review feedback asking for a test that loads compiled ADK and
checks it actually works.

Parsing is necessary but not sufficient: it would not catch output that
bundles and then throws on import, or a primitive that is broken once
compiled for the browser. This bundles entry points from dist/web with
--platform=browser and runs them — constructing a FunctionTool, checking
the declaration a model would receive, and executing it.

The entry-point list is short because most of dist/web still cannot be
bundled: the barrel pulls skills/loader (node:fs, node:path) and the
logger pulls winston. agents/llm_agent.js and runner/runner.js are the
two worth adding next and are one dependency away each.

Verified all three new tests fail against an unpatched build.

* Fix: make the whole web build bundle and run in a browser

Follows review feedback asking that the entire ADK TS web bundle be
tested, not a subset. Getting there needed the remaining two defects
fixed rather than deferred.

Bundle the web target always. esbuild rejects `alias` unless bundling,
so the existing node:async_hooks shim never reached published output --
`npm run build` does not pass --bundle. Bundling the web target makes
the alias mechanism work, and the emitted filename now follows the entry
so package.json's "browser" field still resolves.

Add a browser stand-in for winston. utils/logger.ts pulls winston into
every module that logs, and winston needs os/fs/util/zlib/http. The shim
implements only the surface logger.ts uses and writes to the console,
which is what a browser transport does.

Keep Node-only modules out of the web barrel. index.ts already holds the
Node-only exports and index_web.ts is common.ts alone, so common.ts is
meant to be the browser-safe surface -- but exports reaching node:fs,
node:dns, adm-zip, google-auth-library and @google-cloud/vertexai had
leaked into it. Those move to index.ts alongside the ones already there.
The set was derived by bundling all 199 source modules for the browser
and recording which reach a Node-only dependency, not chosen by hand.

core/dist/web/index_web.js now bundles for --platform=browser with zero
errors, down from 346. The bundle imports and drives the public API:
LlmAgent, ParallelAgent, SequentialAgent, LoopAgent, FunctionTool,
Runner, InMemorySessionService and BaseLlm are all present and working.

The Node public API is unchanged -- 206 exports from dist/esm/index.js
before and after, verified by diffing the resolved export lists.

Mutation check: reverting only the common.ts hunk makes the bundle step
fail with 173 unresolved Node imports, vitest exits 1 and reports the
file as failed.

* test: run ADK in a real headless browser, and ship a self-contained web bundle

Replaces the synthetic bundle test with the real thing, per review.

tests/integration/build_setup/web_app/ is a small chat page -- message
list, prompt input, send button -- that imports core/dist/web/index_web.js
directly, with no build step of its own. web_app_test.ts drives it in
headless Chromium with Playwright: types a message, clicks send, and
asserts the user message and the agent reply both render, then runs a
second turn on the same session. The model is a BaseLlm subclass
returning a canned response, matching the mock in the existing
build_setup fixtures. Page errors and console errors fail the test, so a
DOM or module-loading failure surfaces instead of passing silently.

Making that possible meant the web target had to become genuinely
self-contained. It was bundled but kept `packages: 'external'`, so the
output still imported '@google/genai', 'zod' and friends as bare
specifiers, which a browser cannot resolve. The web target now bundles
its dependencies; Node keeps packages external as before. 972 KB.

Splits the build-output tests as requested: web_build_test.ts covers
dist/web only, and the Node ESM/CJS assertions move to node_build_test.ts.
Drops the assertions that re-bundled dist/web inside the test -- the
shipped artifact is already a bundle, and the browser test now exercises
it directly.

Adds playwright-chromium and a CI step to install the browser.

* fix: drop the winston browser shim now that main splits the logger by platform

Rebasing onto main brought in google#813, which keeps winston out of the
browser build by splitting the logger into logger_node (winston) and
logger_browser (console). The browser entry installs logger_browser and
never imports winston, so this PR's winston->shim alias and
winston_shim.ts are dead code and are removed.

node_build_test now reads utils/logger_node.js, where winston lives after
the split, instead of utils/logger.js.

---------

Co-authored-by: Amaad Martin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment