> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getbindu.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 3.7 TypeScript OpenAI Agent

> TypeScript agent using OpenAI SDK with Bindu

TypeScript agent using OpenAI SDK with Bindu.

## Code

```typescript theme={null}
/**
 * TypeScript OpenAI Agent — Bindufied
 *
 * Demonstrates using the Bindu TypeScript SDK with the OpenAI SDK.
 * Uses GPT-4o to answer questions and assist users.
 *
 * Usage:
 *   1. Set OPENAI_API_KEY in .env
 *   2. npx tsx index.ts
 */

import { bindufy, ChatMessage } from "@bindu/sdk";
import OpenAI from "openai";
import * as dotenv from "dotenv";

dotenv.config();

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

// bindufy — one call, full microservice
bindufy(
  {
    author: "opnai-sample-ts@getbindu.com",
    name: "openai-assistant-agent",
    description:
      "An assistant built with the OpenAI SDK and Bindu. Powered by GPT-4o.",
    version: "1.0.0",
    deployment: {
      url: "http://localhost:3773",
      expose: true,
      cors_origins: ["http://localhost:5173"],
    },
    skills: ["skills/typescript-openai-skill"],
  },
  async (messages: ChatMessage[]) => {
    // Call OpenAI GPT-4o
    const response = await openai.chat.completions.create({
      model: process.env.OPENAI_MODEL || "gpt-4o",
      messages: messages.map((m) => ({
        role: m.role as "user" | "assistant" | "system",
        content: m.content,
      })),
    });

    return response.choices[0].message.content || "";
  }
);
```

## Skill Configuration

Create `skills/typescript-openai-skill/skill.yaml`:

```yaml theme={null}
# TypeScript OpenAI Agent Skill
# TypeScript-based AI agent using OpenAI SDK

id: typescript-openai-skill
name: TypeScript OpenAI Agent Skill
version: 1.0.0
author: typescript-developer@example.com

description: |
  TypeScript agent that demonstrates Bindu SDK integration with OpenAI SDK.
  Built with modern TypeScript practices and direct OpenAI API integration.

  Features:
  - Direct OpenAI SDK integration
  - TypeScript type safety and modern ES features
  - GPT-4o model support
  - Environment-based configuration
  - CORS-enabled for frontend integration

tags:
  - typescript
  - openai
  - gpt-4o
  - bindu-sdk
  - agent-development
  - es2022
  - nodejs

input_modes:
  - application/json
output_modes:
  - application/json

examples:
  - "What are the key features of GPT-4o for TypeScript development?"
  - "How do I implement error handling in OpenAI API calls?"
  - "Create a TypeScript function that analyzes sentiment using OpenAI"
  - "What are the best practices for TypeScript OpenAI integration?"
  - "How do I optimize OpenAI API responses in TypeScript?"

capabilities_detail:
  openai_integration:
    supported: true
    description: "Direct integration with OpenAI SDK"
    features:
      - gpt_4o_support
      - chat_completions
      - error_handling

  typescript_support:
    supported: true
    description: "Full TypeScript support with type safety"
    features:
      - es2022_target
      - strict_typing
      - modern_syntax

  bindu_sdk:
    supported: true
    description: "Full Bindu SDK integration for protocol compliance"
    features:
      - did_support
      - x402_payments
      - a2a_protocol
      - cors_enabled

requirements:
  packages:
    - "@bindu/sdk"
    - "openai"
    - "dotenv"
    - "@types/node"
    - "tsx"
  system:
    - node_18_or_higher
    - typescript_5_or_higher
  api_keys:
    - OPENAI_API_KEY

performance:
  avg_response_time_ms: 2500
  max_concurrent_requests: 8
  memory_usage_mb: 256
  scalability: vertical

assessment:
  keywords:
    - typescript
    - openai
    - gpt-4o
    - agent
    - bindu
    - nodejs
    - es2022

  specializations:
    - domain: typescript-development
      confidence_boost: 0.4
    - domain: openai-integration
      confidence_boost: 0.6
    - domain: api-development
      confidence_boost: 0.3

  anti_patterns:
    - "blocking operations"
    - "callback hell"
    - "type_any_usage"
    - "legacy_syntax"

  complexity_indicators:
    simple:
      - "what are"
      - "how do I"
      - "explain"
    medium:
      - "implement"
      - "best practices for"
    complex:
      - "create function that"
      - "optimize responses"
      - "comprehensive guide"
```

## How It Works

**TypeScript SDK Integration**

* `@bindu/sdk`: TypeScript Bindu SDK for agent development
* Automatic microservice generation with DID and auth
* gRPC communication with Bindu Python core
* A2A protocol compliance handled by SDK

**OpenAI SDK Integration**

* `OpenAI`: Direct OpenAI SDK integration
* GPT-4o model for question answering
* Chat completions API with message history
* Configurable model via environment variable

**Message Processing**

* `ChatMessage[]`: Bindu message format with role and content
* Role mapping (user/assistant/system) for OpenAI compatibility
* Direct API calls to OpenAI chat completions
* Response extraction and content return

**Configuration**

* Environment-based API key management
* Standard Bindu deployment configuration
* Skills declaration for capabilities
* Model configuration flexibility

## Dependencies

```bash theme={null}
npm install @bindu/sdk openai dotenv
npm install -D @types/node tsx
```

## Environment Setup

Create `.env` file:

```bash theme={null}
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_MODEL=gpt-4o
```

## Run

```bash theme={null}
npx tsx index.ts
```

## Example API Calls

<AccordionGroup>
  <Accordion title="Message Send Request">
    ```json theme={null}
    {
      "jsonrpc": "2.0",
      "method": "message/send",
      "params": {
        "message": {
          "role": "user",
          "kind": "message",
          "messageId": "9f11c870-5616-49ad-b187-d93cbb100001",
          "contextId": "9f11c870-5616-49ad-b187-d93cbb100002",
          "taskId": "9f11c870-5616-49ad-b187-d93cbb100003",
          "parts": [
            {
              "kind": "text",
              "text": "What are the key features of GPT-4o for TypeScript development?"
            }
          ]
        },
         "skillId": "typescript-openai-skill",
        "configuration": {
          "acceptedOutputModes": ["application/json"]
        }
      },
      "id": "9f11c870-5616-49ad-b187-d93cbb100003"
    }
    ```
  </Accordion>

  <Accordion title="Task get Request">
    ```json theme={null}
    {
      "jsonrpc": "2.0",
      "method": "tasks/get",
      "params": {
        "taskId": "9f11c870-5616-49ad-b187-d93cbb100003"
      },
      "id": "9f11c870-5616-49ad-b187-d93cbb100004"
    }
    ```
  </Accordion>
</AccordionGroup>

## Frontend Setup

```bash theme={null}
# Clone the Bindu repository
git clone https://github.com/GetBindu/Bindu

# Navigate to frontend directory
cd frontend

# Install dependencies
npm install

# Start frontend development server
npm run dev
```

Open [http://localhost:5173](http://localhost:5173) and try to chat with the TypeScript OpenAI agent
