Skip to content
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

feat: Inferable tRPC Adapter #272

Merged
merged 12 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions connectors/trpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Dependencies
node_modules/

# Build output
dist/

# Environment variables
.env
.env.*

# IDE files
.vscode/
.idea/

# Logs
*.log
npm-debug.log*

# Coverage directory
coverage/

# OS files
.DS_Store
103 changes: 103 additions & 0 deletions connectors/trpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<p align="center">
<img src="https://a.inferable.ai/logo-hex.png" width="200" style="border-radius: 10px" />
</p>

# tRPC Adapter for Inferable

![npm version](https://badge.fury.io/js/@inferable/trpc-connector.svg)
![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)

The Inferable tRPC Adapter allows you to expose your existing tRPC router endpoints as Inferable functions. This enables AI agents to interact with your tRPC API while preserving all your existing middleware and type safety.

## Installation

### npm

```bash
npm install @inferable/trpc-connector
```

### yarn

```bash
yarn add @inferable/trpc-connector
```

### pnpm

```bash
pnpm add @inferable/trpc-connector
```

## Quick Start

Create your tRPC router as normal:

```ts
const t = initTRPC.create();

const router = t.router;
const publicProcedure = t.procedure;

const appRouter = t.router({
userById: publicProcedure
.input(z.object({ id: z.string() }))
.meta({ inferable: true }) // <--- Mark this procedure for Inferable
.query(({ input }) => {
return users.find((user) => user.id === input.id);
}),
});
```

Create an Inferable service from your router:

```ts
import { createInferableService } from "@inferable/trpc-connector";
import { Inferable } from "inferable";

const client = new Inferable({
apiSecret: process.env.INFERABLE_API_SECRET,
});

const service = createInferableService({
router: appRouter,
createCaller: t.createCallerFactory(appRouter),
name: "userService",
client,
});

// Start the service
await service.start();
```

3. Your tRPC procedures are now available as Inferable functions!

```ts
const result = await client.run({
initialPrompt: "Get the user with id 1",
resultSchema: z.object({
id: z.string(),
name: z.string(),
email: z.string(),
}),
});
```

## Notes

- Preserve Middleware: All your existing tRPC middleware continues to work
- Type Safety: Maintains full type safety through your tRPC router
- Selective Exposure: Only procedures marked with meta({ inferable: true }) are exposed
- Custom Descriptions: Add descriptions to help guide the AI through meta({ description: "..." })

## Documentation

Inferable documentation contains all the information you need to get started with Inferable.

## Support

For support or questions, please create an issue in the repository.

## Contributing

Contributions to the Inferable tRPC Connector are welcome. Please ensure that your code adheres to the existing style and includes appropriate tests.
28 changes: 28 additions & 0 deletions connectors/trpc/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
]
}
}
7 changes: 7 additions & 0 deletions connectors/trpc/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["<rootDir>/src"],
testMatch: ["**/*.test.ts"],
collectCoverageFrom: ["src/**/*.ts", "!src/**/*.d.ts"],
};
Loading
Loading