@redis-iris/intent-router
v1.0.1
Published
TypeScript SDK for Redis Intent Router
Keywords
Readme
@redis-iris/intent-router
Developer-friendly & type-safe Typescript SDK specifically catered to leverage @redis-iris/intent-router API.
[!IMPORTANT] This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your workspace. Delete this section before > publishing to a package manager.
Summary
Redis Intent Router: Intent authoring and matching API for one Router. An intent is a named classification with example phrasings and an optional dispatch target; matching returns the intents closest to a query. Intents and examples are ordered lists: they are returned in the order they were created.
For more information about the API: Redis AI documentation
Table of Contents
SDK Installation
The SDK can be installed with either npm, pnpm, bun or yarn package managers.
NPM
npm add @redis-iris/intent-routerPNPM
pnpm add @redis-iris/intent-routerBun
bun add @redis-iris/intent-routerYarn
yarn add @redis-iris/intent-router[!NOTE] This package is published with CommonJS and ES Modules (ESM) support.
Requirements
For supported JavaScript runtimes, please consult RUNTIMES.md.
SDK Example Usage
Example
import { IntentRouter } from "@redis-iris/intent-router";
const intentRouter = new IntentRouter({
serverURL: "https://api.example.com",
});
async function run() {
const result = await intentRouter.getServiceLiveness();
console.log(result);
}
run();
Authentication
Per-Client Security Schemes
This SDK supports the following security scheme globally:
| Name | Type | Scheme | Environment Variable |
| -------- | ---- | ----------- | ----------------------- |
| apiKey | http | HTTP Bearer | INTENT_ROUTER_API_KEY |
To authenticate with the API the apiKey parameter must be set when initializing the SDK client instance. For example:
import { IntentRouter } from "@redis-iris/intent-router";
const intentRouter = new IntentRouter({
serverURL: "https://api.example.com",
apiKey: "<INTENT_ROUTER_API_KEY>",
});
async function run() {
const result = await intentRouter.getServiceLiveness();
console.log(result);
}
run();
Available Resources and Operations
IntentRouter SDK
- getServiceLiveness - Reports that this API process can answer requests, independently of downstream dependencies.
- getServiceReadiness - Evaluates the same serving dependencies as the operational readiness probe. Returns 503 without dependency details when the service cannot serve requests.
- clear - Removes everything the scope names. With INTENTS every intent and example of the Router is deleted and the Router itself stays. The scope is required: there is no default.
- getRouterHealth - Reports why a Router can or cannot serve: its state, the generation the data plane has applied and the stores it depends on.
- listIntents - Lists intents with their configuration and example counts, in the order they were created.
- addIntents - Adds several intents in the order given, after the ones the Router already has. An intent whose name already exists is replaced in its place; every other intent is left alone. Nothing is written if any intent is invalid.
- deleteIntent - Deletes one intent and its examples. Deleting a missing intent succeeds.
- getIntent - Returns one intent. Its examples are listed from the examples sub-resource.
- updateIntent - Changes an intent's description, target, minScore or metadata as a merge patch, without touching or re-embedding its examples. An omitted member is unchanged and a null member is cleared; a given metadata map replaces the whole map.
- upsertIntent - Creates or replaces one intent. Idempotent: the same request twice yields the same state. 201 when the intent is new, 200 when it replaced one, which keeps its place in the list. Existing phrasings absent from the request are removed after the new ones are written, so a concurrent match never sees an empty intent. Send
If-None-Match: *to create only: if the intent already exists the request is refused with 412 and nothing is written, and the check is atomic with the write, so of several concurrent creates exactly one succeeds. - listExamples - Lists an intent's examples in the order they were written.
- addExamples - Adds example phrasings to an existing intent, after the ones it already has. Repeating a phrasing is harmless: it keeps its place and is not embedded again.
- deleteExample - Deletes one example. Deleting a missing example succeeds.
- getExample - Returns one example. An identifier from another intent is not found.
- match - Returns the intents closest to the query, best first. No match is a 200 with an empty list.
Standalone functions
All the methods listed above are available as standalone functions. These functions are ideal for use in applications running in the browser, serverless runtimes or other environments where application bundle size is a primary concern. When using a bundler to build your application, all unused functionality will be either excluded from the final bundle or tree-shaken away.
To read more about standalone functions, check FUNCTIONS.md.
addExamples- Adds example phrasings to an existing intent, after the ones it already has. Repeating a phrasing is harmless: it keeps its place and is not embedded again.addIntents- Adds several intents in the order given, after the ones the Router already has. An intent whose name already exists is replaced in its place; every other intent is left alone. Nothing is written if any intent is invalid.clear- Removes everything the scope names. With INTENTS every intent and example of the Router is deleted and the Router itself stays. The scope is required: there is no default.deleteExample- Deletes one example. Deleting a missing example succeeds.deleteIntent- Deletes one intent and its examples. Deleting a missing intent succeeds.getExample- Returns one example. An identifier from another intent is not found.getIntent- Returns one intent. Its examples are listed from the examples sub-resource.getRouterHealth- Reports why a Router can or cannot serve: its state, the generation the data plane has applied and the stores it depends on.getServiceLiveness- Reports that this API process can answer requests, independently of downstream dependencies.getServiceReadiness- Evaluates the same serving dependencies as the operational readiness probe. Returns 503 without dependency details when the service cannot serve requests.listExamples- Lists an intent's examples in the order they were written.listIntents- Lists intents with their configuration and example counts, in the order they were created.match- Returns the intents closest to the query, best first. No match is a 200 with an empty list.updateIntent- Changes an intent's description, target, minScore or metadata as a merge patch, without touching or re-embedding its examples. An omitted member is unchanged and a null member is cleared; a given metadata map replaces the whole map.upsertIntent- Creates or replaces one intent. Idempotent: the same request twice yields the same state. 201 when the intent is new, 200 when it replaced one, which keeps its place in the list. Existing phrasings absent from the request are removed after the new ones are written, so a concurrent match never sees an empty intent. SendIf-None-Match: *to create only: if the intent already exists the request is refused with 412 and nothing is written, and the check is atomic with the write, so of several concurrent creates exactly one succeeds.
Global Parameters
A parameter is configured globally. This parameter may be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, This global value will be used as the default on the operations that use it. When such operations are called, there is a place in each to override the global value, if needed.
For example, you can set routerId to "<id>" at SDK initialization and then you do not have to pass the same value on calls to operations like clear. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.
Available Globals
The following global parameter is available. Global parameters can also be set via environment variable.
| Name | Type | Description | Environment | | -------- | ------ | ----------------------- | ----------------------- | | routerId | string | The routerId parameter. | INTENT_ROUTER_ROUTER_ID |
Example
import { IntentRouter } from "@redis-iris/intent-router";
const intentRouter = new IntentRouter({
serverURL: "https://api.example.com",
routerId: "<id>",
apiKey: "<INTENT_ROUTER_API_KEY>",
});
async function run() {
const result = await intentRouter.clear({
scope: "INTENTS",
});
console.log(result);
}
run();
Retries
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a retryConfig object to the call:
import { IntentRouter } from "@redis-iris/intent-router";
const intentRouter = new IntentRouter({
serverURL: "https://api.example.com",
});
async function run() {
const result = await intentRouter.getServiceLiveness({
retries: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
});
console.log(result);
}
run();
If you'd like to override the default retry strategy for all operations that support retries, you can provide a retryConfig at SDK initialization:
import { IntentRouter } from "@redis-iris/intent-router";
const intentRouter = new IntentRouter({
serverURL: "https://api.example.com",
retryConfig: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
});
async function run() {
const result = await intentRouter.getServiceLiveness();
console.log(result);
}
run();
Error Handling
IntentRouterError is the base class for all HTTP error responses. It has the following properties:
| Property | Type | Description |
| ------------------- | ---------- | --------------------------------------------------------------------------------------- |
| error.message | string | Error message |
| error.statusCode | number | HTTP response status code eg 404 |
| error.headers | Headers | HTTP response headers |
| error.body | string | HTTP body. Can be empty string if no body is returned. |
| error.rawResponse | Response | Raw HTTP response |
| error.data$ | | Optional. Some errors may contain structured data. See Error Classes. |
Example
import { IntentRouter } from "@redis-iris/intent-router";
import * as errors from "@redis-iris/intent-router/models/errors";
const intentRouter = new IntentRouter({
serverURL: "https://api.example.com",
});
async function run() {
try {
const result = await intentRouter.getServiceLiveness();
console.log(result);
} catch (error) {
// The base class for HTTP error responses
if (error instanceof errors.IntentRouterError) {
console.log(error.message);
console.log(error.statusCode);
console.log(error.body);
console.log(error.headers);
// Depending on the method different errors may be thrown
if (error instanceof errors.IntentBadRequestErrorResponseContent) {
console.log(error.data$.title); // string
console.log(error.data$.status); // number
console.log(error.data$.detail); // string
console.log(error.data$.instance); // string
console.log(error.data$.type); // models.IntentBadRequestErrorType
}
}
}
}
run();
Error Classes
Primary errors:
IntentRouterError: The base class for HTTP error responses.IntentBadRequestErrorResponseContent: The request is invalid. Status code400.AuthenticationErrorResponseContent: Authentication credentials are missing, malformed, or invalid. Status code401.ForbiddenErrorResponseContent: The caller is authenticated but not allowed to access the requested resource. Status code403.NotFoundErrorResponseContent: The requested resource does not exist. Status code404.TimeoutErrorResponseContent: The request timed out before the service could complete it. Status code408.IntentConflictErrorResponseContent: The request conflicts with the state of the Router. Status code409.PayloadTooLargeErrorResponseContent: The request payload exceeds the maximum supported size. Status code413.RouterSuspendedErrorResponseContent: The Router is suspended by an administrator and does not serve requests until it is resumed. Status code423.IntentFailedDependencyErrorResponseContent: A system the request depends on failed in a way that retrying the same request will not fix by itself. Status code424.UnexpectedErrorResponseContent: The service failed with an unexpected internal error. Status code500.IntentServiceUnavailableErrorResponseContent: The service cannot answer right now; retrying later is expected to work. Status code503.
Network errors:
ConnectionError: HTTP client was unable to make a request to a server.RequestTimeoutError: HTTP request timed out due to an AbortSignal signal.RequestAbortedError: HTTP request was aborted by the client.InvalidRequestError: Any input used to create a request is invalid.UnexpectedClientError: Unrecognised or unexpected error.
Inherit from IntentRouterError:
PreconditionFailedErrorResponseContent: The write named an expected revision (expectedRevision or If-Match) that is no longer current. Read the resource again and retry. Status code412. Applicable to 2 of 15 methods.*ResponseValidationError: Type mismatch between the data returned from the server and the structure expected by the SDK. Seeerror.rawValuefor the raw value anderror.pretty()for a nicely formatted multi-line string.
* Check the method documentation to see if the error is applicable.
Custom HTTP Client
The TypeScript SDK makes API calls using an HTTPClient that wraps the native
Fetch API. This
client is a thin wrapper around fetch and provides the ability to attach hooks
around the request lifecycle that can be used to modify the request or handle
errors and response.
The HTTPClient constructor takes an optional fetcher argument that can be
used to integrate a third-party HTTP client or when writing tests to mock out
the HTTP client and feed in fixtures.
The following example shows how to:
- route requests through a proxy server using undici's ProxyAgent
- use the
"beforeRequest"hook to add a custom header and a timeout to requests - use the
"requestError"hook to log errors
import { IntentRouter } from "@redis-iris/intent-router";
import { ProxyAgent } from "undici";
import { HTTPClient } from "@redis-iris/intent-router/lib/http";
const dispatcher = new ProxyAgent("http://proxy.example.com:8080");
const httpClient = new HTTPClient({
// 'fetcher' takes a function that has the same signature as native 'fetch'.
fetcher: (input, init) =>
// 'dispatcher' is specific to undici and not part of the standard Fetch API.
fetch(input, { ...init, dispatcher } as RequestInit),
});
httpClient.addHook("beforeRequest", (request) => {
const nextRequest = new Request(request, {
signal: request.signal || AbortSignal.timeout(5000)
});
nextRequest.headers.set("x-custom-header", "custom value");
return nextRequest;
});
httpClient.addHook("requestError", (error, request) => {
console.group("Request Error");
console.log("Reason:", `${error}`);
console.log("Endpoint:", `${request.method} ${request.url}`);
console.groupEnd();
});
const sdk = new IntentRouter({ httpClient: httpClient });Debugging
You can setup your SDK to emit debug logs for SDK requests and responses.
You can pass a logger that matches console's interface as an SDK option.
[!WARNING] Beware that debug logging will reveal secrets, like API tokens in headers, in log messages printed to a console or files. It's recommended to use this feature only during local development and not in production.
import { IntentRouter } from "@redis-iris/intent-router";
const sdk = new IntentRouter({ debugLogger: console });You can also enable a default debug logger by setting an environment variable INTENT_ROUTER_DEBUG to true.
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Contributions
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
