Type instantiation is excessively deep and possibly infinite while creating custom tool in Node.JS environment

### Description

**Description:**

When attempting to integrate a custom tool with the AI SDK in a Node.js backend environment (specifically with Fastify (also tested with nest.js)), a persistent "Type instantiation is excessively deep and possibly infinite" error occurs. This error seems to stem from a recursive type inference issue within the `tool` function provided by the `ai` package. The problem manifests when defining a tool, particularly when specifying its schema or parameters, and it prevents the TypeScript compiler from successfully building the project.

**Steps to Reproduce:**

1.  **Project Setup:**
    *   Initialize a new Node.js project with TypeScript and Fastify.
    *   Install the necessary dependencies, including `ai`, `zod`, and `@ai-sdk/openai`.
    *   Configure the `tsconfig.json` for a modern Node.js environment (e.g., `"module": "esnext"`, `"target": "es2022"`).

2.  **Tool Definition:**
    *   Create a new file for the tool definition (e.g., `src/tools/weather.tool.ts`).
    *   Import the `tool` function from the `ai` package and `zod` for schema definition.
    *   Define a simple tool using the `tool` function, providing a `description`, `inputSchema` (using `zod`), and an `execute` or `run` function.

    ```typescript
    import { tool } from "ai";
    import { z } from "zod";

    export const weatherTool = tool({
        description: 'Get the weather in a location',
        inputSchema: z.object({
          location: z.string(),
        }),
        execute: async ({ location }: { location: string }) => ({
          location,
          temperature: 72 + Math.floor(Math.random() * 21) - 10,
        }),
      });
    ```

3.  **Tool Integration:**
    *   In a service file (e.g., `src/services/ai.service.ts`), import the `weatherTool`.
    *   Use the `generateText` function from the `ai` package and pass the `weatherTool` in the `tools` array.

    ```typescript
    import { openai } from '@ai-sdk/openai';
    import { generateText } from 'ai';
    import { weatherTool } from '../tools/weather.tool';

    // ...

    const { text } = await generateText({
      model: this.model,
      messages,
      tools: { weatherTool },
    });
    ```

4.  **Build the Project:**
    *   Run the TypeScript compiler (`tsc` or `npm run build`).

**Expected Behavior:**

The project should compile successfully without any type errors, allowing the tool to be used with the AI model.

**Actual Behavior:**

The TypeScript compiler fails with a "Type instantiation is excessively deep and possibly infinite" error, pointing to the tool definition file. This error prevents the project from being built and run.

**Error Logs:**

```
src/tools/weather.tool.ts(4,28): error TS2589: Type instantiation is excessively deep and possibly infinite.
src/tools/weather.tool.ts(6,18): error TS2589: Type instantiation is excessively deep and possibly infinite.
```

**Analysis:**

The root cause of this issue appears to be a problem with how the `tool` function's type definitions are handled by the TypeScript compiler. The recursive nature of the type inference, especially when combined with `zod` schemas, leads to an infinite loop that exceeds the compiler's depth limit.

Several attempts were made to work around this issue, including:

*   Using `parameters` or `schema` instead of `inputSchema`.
*   Simplifying the tool definition by removing the `zod` schema and using a plain JavaScript object.
*   Removing the `tool` wrapper function and defining the tool as a simple object.

None of these workarounds were successful, indicating that the problem lies deep within the type definitions of the `ai` SDK.

**Environment:**

*   **Node.js Version:** v22.17.1
*   **TypeScript Version:** 5.9.2
*   **`ai` SDK Version:** 5.0.15
*   **`@ai-sdk/openai` Version:** 2.0.15
*   **`zod` Version:** 3.25.76
*   **Operating System:** macOS

**Suggested Solution:**

The type definitions for the `tool` function in the `ai` SDK need to be reviewed and revised to prevent this recursive type inference issue. This may involve simplifying the generic types, providing explicit type annotations, or using a different approach to schema definition that is more compatible with the TypeScript compiler.

### AI SDK Version

**Environment:**

*   **Node.js Version:** v22.17.1
*   **TypeScript Version:** 5.9.2
*   **`ai` SDK Version:** 5.0.15
*   **`@ai-sdk/openai` Version:** 2.0.15
*   **`zod` Version:** 3.25.76
*   **Operating System:** macOS

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

Labels: ai/provider, support, provider/openai
State: closed