npm.io
13.1.19 • Published 3d ago

@typia/utils

Licence
MIT
Version
13.1.19
Deps
1
Size
2.5 MB
Vulns
0
Weekly
0
Stars
5.9K

@typia/utils

Typia Logo

GitHub license NPM Version NPM Downloads Build Status Guide Documents Gurubase Discord Badge

Utility functions and converters for the typia ecosystem.

Automatically installed as a dependency of typia.

Key Exports

Module Description
HttpLlm Create LLM controllers from OpenAPI documents and execute function calls
HttpMigration HTTP migration utilities
HttpError HTTP error type
OpenApiConverter Convert between OpenAPI versions
OpenApiTypeChecker Type checker for OpenAPI documents
LlmSchemaConverter Convert JSON Schema to LLM schema
LlmTypeChecker Type checker for LLM schemas
LlmJson Parse lenient JSON and format validation errors for LLM-friendly feedback

LlmJson

Parse lenient JSON and format validation errors for LLM-friendly feedback:

import { IValidation } from "@typia/interface";
import { LlmJson } from "@typia/utils";

const validation: IValidation<unknown> = func.validate(llmArguments);
if (validation.success === false) {
  console.log(LlmJson.stringify(validation));
}

Output:

{
  "x": "not a number", // ❌ expected: number
  "y": 5
}

HttpLlm

Create LLM controllers from OpenAPI documents:

import { IHttpLlmController } from "@typia/interface";
import { HttpLlm } from "@typia/utils";

// any OpenAPI version (Swagger 2.0, OpenAPI 3.0/3.1) is accepted
const controller: IHttpLlmController = HttpLlm.controller({
  name: "shopping",
  document: await fetch("https://shopping.example.com/swagger.json").then(r => r.json()),
  connection: {
    host: "https://shopping.example.com",
    headers: { Authorization: "Bearer ..." },
  },
});

// use with @typia/langchain or @typia/vercel
toLangChainTools({ controllers: [controller] });
toVercelTools({ controllers: [controller] });

OpenApiConverter is used internally to normalize any OpenAPI version into a unified format. You can also use it directly:

import { OpenApi, OpenApiV3 } from "@typia/interface";
import { OpenApiConverter } from "@typia/utils";

const emended: OpenApi.IDocument = OpenApiConverter.upgradeDocument(swagger2doc); // Swagger 2.0 → OpenApi
const downgraded: OpenApiV3.IDocument = OpenApiConverter.downgradeDocument(emended, "3.0"); // → OpenAPI 3.0

Keywords