Skip to content

refactor: improve type safety with openapi-fetch and simplify API with server functions#18

Merged
MrOrz merged 6 commits into
masterfrom
refactor-chat-typing
Mar 15, 2026
Merged

refactor: improve type safety with openapi-fetch and simplify API with server functions#18
MrOrz merged 6 commits into
masterfrom
refactor-chat-typing

Conversation

@MrOrz
Copy link
Copy Markdown
Member

@MrOrz MrOrz commented Mar 12, 2026

This PR is stacked on top of upgrade-adk. It focuses on improving type safety across the application and simplifying the API layer.

Major Changes

1. Robust Type Safety with openapi-fetch

  • Switched from manual fetch calls and hardcoded interfaces to openapi-fetch.
  • Replaced custom interfaces in src/lib/adk.ts with types generated directly from the ADK OpenAPI spec.
  • Breaking Change Alignment: Updated MessageRole from agent to model and adjusted property casing (e.g., grounding_metadata to groundingMetadata) to match the latest ADK definitions.

2. API Simplification with TanStack Server Functions

  • Complete removal of /api directory: Deleted the last remaining proxy route /api/chat. All backend communication is now handled via TanStack Server Functions.
  • Server-side Streaming with Async Generators: Refactored the chat stream to use an async generator in the runChat server function. SSE parsing logic is now handled on the server, allowing the client to consume typed AdkEvent objects via a simple for await loop.
  • Internalized System Parameters: Encapsulated appName and userId within server functions. The client no longer needs to pass or maintain these internal identifiers, improving security and API cleanliness.
  • Updated session management to use server functions, reducing boilerplate and providing a better developer experience.

3. OpenAPI Type Generation Improvements

  • Adjusted the type generation script to replace unknown with {} in generated types, resolving TypeScript issues where unknown was too restrictive for TanStack Start's expectations.

Implementation Details

  • Updated src/lib/chatCache.ts to use the new runChat server function and handle camelCase property names (appName, userId, sessionId, newMessage).
  • Refactored src/hooks/useChat.ts and other components to align with the new server function signatures and camelCase naming conventions.
  • Standardized the API layer by removing src/routes/api/* files.

Reviewer Notes

Please pay close attention to the MessageRole rename and the removal of the src/routes/api directory. Ensure no client-side manual fetch calls remain (TypeScript should help verify this).

MrOrz and others added 2 commits March 12, 2026 23:13
Fixes the TanStack Start createServerFn return serialization type error by post-processing the openapi-typescript generated types. The generated 'unknown' types deep within nested events and dictionaries like 'state' are strictly rejected by the server payload bounds, but replacing them with '{}' satisfies the compiler natively without complex local mapped types or resorting to 'any'.

Co-authored-by: Antigravity <antigravity@google.com>
Updated MessageRole from 'agent' to 'model' to match the server-defined roles.
Removed manual role mapping logic in applyEventToState.

Co-authored-by: Antigravity <antigravity@google.com>
@github-actions github-actions Bot temporarily deployed to pr-18 March 12, 2026 16:11 Destroyed
@MrOrz MrOrz changed the title refactor: align MessageRole and fix OpenAPI unknown types refactor: improve type safety with openapi-fetch and simplify API with server functions Mar 12, 2026
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the application's data fetching and API interaction layers. By adopting openapi-fetch and TanStack Server Functions, the codebase achieves greater type safety and a more streamlined API architecture. This change removes the need for several client-side API proxy routes, simplifying the overall application structure and improving developer experience through better type inference and reduced boilerplate.

Highlights

  • Enhanced Type Safety with openapi-fetch: Integrated openapi-fetch to replace manual fetch calls, leveraging types generated directly from the ADK OpenAPI specification. This provides end-to-end type safety for API interactions.
  • API Simplification with TanStack Server Functions: Refactored session management and chat tracking to utilize TanStack Server Functions, eliminating several redundant API proxy route files (src/routes/api/sessions.ts, src/routes/api/sessions.$sessionId.ts) and reducing boilerplate.
  • ADK Type Alignment and Breaking Changes: Updated MessageRole from agent to model and adjusted property casing (e.g., grounding_metadata to groundingMetadata) in src/lib/adk.ts to align with the latest ADK OpenAPI definitions.
  • Improved OpenAPI Type Generation: Modified the type generation script to automatically replace unknown with {} in generated types, resolving compatibility issues with TanStack Start's expectations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • package.json
    • Added openapi-fetch and openapi-typescript dependencies.
    • Introduced a typegen script to generate OpenAPI types and replace unknown with {}.
  • pnpm-lock.yaml
    • Updated lockfile to include new openapi-fetch and openapi-typescript dependencies and their transitive dependencies.
  • src/hooks/useChat.ts
    • Updated getSession import to use the new server function from src/lib/sessions.functions.ts.
    • Adjusted getSession call to match the createServerFn input signature.
  • src/hooks/useSessions.ts
    • Updated listSessions import to use the new server function from src/lib/sessions.functions.ts.
  • src/lib/adk.ts
    • Removed manual ADK interface definitions.
    • Imported and re-exported types directly from the generated adk-types.d.ts.
    • Changed MessageRole from agent to model.
    • Updated property access for grounding_metadata to groundingMetadata.
  • src/lib/adkClient.ts
    • Added a new file to initialize and export the openapi-fetch client for ADK interactions.
  • src/lib/api.ts
    • Removed the file, as its functionality is replaced by TanStack Server Functions.
  • src/lib/apiTypes.ts
    • Removed the file, as its types are now generated directly from the OpenAPI spec.
  • src/lib/chatCache.ts
    • Updated MessageRole from agent to model in several places for consistency.
    • Added nullish coalescing operators (?? '', ?? {}) for functionCall properties to improve robustness.
    • Updated grounding_metadata access to groundingMetadata.
    • Added nullish coalescing operator (?? []) for session.events to handle potentially undefined event arrays.
  • src/lib/sessions.functions.ts
    • Added a new file containing createServerFn implementations for listSessions, getSession, and createSession using adkClient.
  • src/routeTree.gen.ts
    • Removed route definitions for /api/sessions and /api/sessions/$sessionId.
  • src/routes/_app.tsx
    • Removed the toggleDrawer callback, indicating a potential UI simplification or removal of a drawer feature.
  • src/routes/_app/index.tsx
    • Updated createSession call to match the createServerFn input signature.
  • src/routes/api/chat.ts
    • Modified the POST handler to use adkClient.POST with parseAs: 'stream' instead of a raw fetch call for streaming ADK responses.
  • src/routes/api/sessions.$sessionId.ts
    • Removed the file, as its functionality is replaced by TanStack Server Functions.
  • src/routes/api/sessions.ts
    • Removed the file, as its functionality is replaced by TanStack Server Functions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist
Copy link
Copy Markdown

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

MrOrz and others added 2 commits March 13, 2026 02:18
- Add `runChat` server function with async generator streaming
- Parse SSE on server side in `runChat`
- Simplified `chatCache.ts` to use `runChat` instead of manual SSE parsing
- Update `StartStreamOptions` and `useChat.ts` to use camelCase properties
- Delete `/api/chat` route

Co-authored-by: Antigravity <antigravity@google.com>
- Define `ChatInput` to hide `appName`, `userId`, and `streaming` from the client
- `runChat` now prepends `ADK_APP_NAME` and `ADK_USER_ID` server-side
- Update `chatCache.ts` to stop passing these internal details

Co-authored-by: Antigravity <antigravity@google.com>
@github-actions github-actions Bot temporarily deployed to pr-18 March 12, 2026 18:22 Destroyed
@MrOrz MrOrz self-assigned this Mar 12, 2026
@MrOrz MrOrz requested review from andyy0216, bil4444 and nonumpa March 12, 2026 18:29
Base automatically changed from upgrade-adk to master March 13, 2026 15:17
This was referenced Mar 14, 2026
@MrOrz
Copy link
Copy Markdown
Member Author

MrOrz commented Mar 14, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a significant and well-executed refactoring that improves type safety and simplifies the API layer. By replacing manual fetch calls with openapi-fetch and generated types, the frontend is now tightly coupled with the ADK's OpenAPI specification, reducing the chance of integration errors. The move to TanStack Server Functions is a major improvement, centralizing API logic, removing the need for a separate /api proxy layer, and simplifying client-side streaming logic.

My review includes a couple of suggestions to enhance portability and error handling for even greater robustness.

Comment thread package.json
Comment thread src/lib/sessions.functions.ts Outdated
Comment thread src/lib/sessions.functions.ts Outdated
@MrOrz MrOrz mentioned this pull request Mar 14, 2026
Comment thread src/lib/adk.ts Outdated
@MrOrz MrOrz added the jules Include this to trigger Jules label Mar 15, 2026
Comment thread src/lib/adk.ts Outdated
@MrOrz
Copy link
Copy Markdown
Member Author

MrOrz commented Mar 15, 2026

Google Jules, please help with the review comment I have tagged you.

- Create handleAdkError and handleAdkResponseError in src/lib/server-utils.ts
- In development, errors include detailed FastAPI messages
- In production, full errors are logged to the server console while client receives a generic message
- Remove duplicate constants and obsolete SSE logic from src/lib/adk.ts
- Update sessions.functions.ts to use new error handlers

Co-authored-by: Gemini CLI <gemini-cli@google.com>
@github-actions github-actions Bot temporarily deployed to pr-18 March 15, 2026 06:20 Destroyed
@MrOrz MrOrz requested a review from nonumpa March 15, 2026 06:21
- Renamed server-utils.ts to adk.server.ts following TanStack Start convention
- Updated sessions.functions.ts imports
- Added header comments to distinguish shared vs server-only ADK files

Co-authored-by: Gemini CLI <gemini-cli@google.com>
@github-actions github-actions Bot temporarily deployed to pr-18 March 15, 2026 06:37 Destroyed
Copy link
Copy Markdown
Member

@nonumpa nonumpa left a comment

Choose a reason for hiding this comment

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

LGTM!

@MrOrz MrOrz merged commit d7abd08 into master Mar 15, 2026
2 checks passed
@MrOrz MrOrz deleted the refactor-chat-typing branch March 15, 2026 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jules Include this to trigger Jules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants