Skip to content

Commit

Permalink
chore: Add README for Val Town adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nadeesha committed Dec 31, 2024
1 parent 357f63d commit b233bc4
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 3 deletions.
131 changes: 131 additions & 0 deletions adapters/valtown-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<p align="center">
<img src="https://a.inferable.ai/logo-hex.png" width="200" style="border-radius: 10px" />
</p>

# Val Town Adapter

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Documentation](https://img.shields.io/badge/docs-inferable.ai-brightgreen)](https://docs.inferable.ai/)

This is the official Inferable AI adapter for Val Town. It allows you to expose your Val Town functions as tools for Inferable cluster. Inferable is an [open-source developer platform for building AI agents](https://www.inferable.ai) and [Vals are small JavaScript or TypeScript snippets of code](https://www.val.town/), written in the browser and runs on a managed val.town server.

<video src="https://a.inferable.ai/valtown.mp4" width="500" />

## Quick Start

```typescript
import { InferableService } from "jsr:@inferable/valtown-adapter";

const service = new InferableService({
description: "My functions",
token: "sk-inf-val-xxxx", // Optional: Add a token to secure your endpoint.
});

service.registerFunction({
name: "sum",
description: "Sum two numbers",
handler: (input: { a: number; b: number }) => Promise.resolve(input.a + input.b),
input: {
type: "object",
properties: {
a: { type: "number" },
b: { type: "number" },
},
required: ["a", "b"],
},
});

const server = service.getServer();

export default server;
```

And then you can install this val in your cluster via Cluster > Integrations > Val.town.

## API Reference

### `InferableService`

The main class for creating an Inferable service that exposes Val Town functions.

#### Constructor Options

```typescript
new InferableService({
description: string; // A description of your service
token?: string; // Optional: A token to authenticate requests
})
```

#### Methods

##### `registerFunction(options)`

Register a function to be exposed through the Inferable service.

Parameters:
- `options`: Object with the following properties:
- `name`: string - The name of the function
- `description`: string - A description of what the function does
- `handler`: (input: any) => Promise<any> - The function implementation
- `input`: Object - JSON Schema describing the input parameters
- `type`: "object" (literal)
- `properties`: Record of parameter names to their types
- `required`: string[] - List of required parameter names

##### `getServer()`

Returns a request handler function compatible with Val Town's serverless environment.

Returns: `(request: Request) => Promise<Response>`

## Endpoints

The adapter exposes two main endpoints:

### GET /meta

Returns metadata about the service, including:
- Service description
- List of available functions and their schemas

### POST /exec/functions/:functionName

Executes a registered function.
- Request body should match the function's input schema
- Returns the function result in the response

## Security

To secure your endpoint, you can provide a token in the constructor:

```typescript
const service = new InferableService({
description: "My secure functions",
token: "sk-inf-val-xxxx",
});
```

When a token is set, all requests must include a Bearer token in the Authorization header:
```
Authorization: Bearer sk-inf-val-xxxx
```

To get a token, install the Val.town integration in your cluster. Upon providing the URL for the val, you'll be provided with a token.

## Documentation

- [Inferable documentation](https://docs.inferable.ai/) contains all the information you need to get started with Inferable.
- [Val Town documentation](https://www.val.town/docs) for more information about Val Town.

## Support

For support or questions, please [create an issue in the repository](https://github.com/inferablehq/inferable/issues).

## Contributing

Contributions to the Inferable Val Town Adapter are welcome. Please ensure that your code adheres to the existing style and includes appropriate tests.

## License

This project is licensed under the MIT License.
11 changes: 8 additions & 3 deletions adapters/valtown-adapter/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ export class InferableService {

constructor(
private options: {
/**
* A description of the service.
*/
description: string;

/**
* A token to authenticate requests to the service. If not provided, authentication is disabled.
*/
token?: string;
},
) {
// Remove public key formatting logic as it's no longer needed
}
) {}

registerFunction(options: {
name: string;
Expand Down

0 comments on commit b233bc4

Please sign in to comment.