Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 35 additions & 3 deletions typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ Install TypeChat:
npm install typechat
```

TypeChat supports both native ECMAScript modules (ESM) and CommonJS. Use `import`
in an ESM application:

```js
import { createJsonTranslator } from "typechat";
import { createTypeScriptJsonValidator } from "typechat/ts";
```

Or use `require` in a CommonJS application:

```js
const { createJsonTranslator } = require("typechat");
const { createTypeScriptJsonValidator } = require("typechat/ts");
```

The `typechat`, `typechat/ts`, `typechat/zod`, and `typechat/interactive` entry points
select the matching JavaScript build and TypeScript declarations automatically.
Both builds target ES2021 and require Node.js 24.22.1 or newer.

For compatibility with earlier CommonJS-only releases, native ESM default imports
(`import typechat from "typechat"`) and dynamic imports' `.default` properties remain
supported for all four entry points. Default imports also preserve the exported
TypeScript types, such as `typechat.Result<T>`.

You can also build TypeChat from source:

```sh
Expand All @@ -36,9 +60,14 @@ npm install
npm run build
```

Run the test suite with `npm test`. It builds the library, compiles TypeScript tests,
and recursively discovers compiled `*.test.js` suites under `out/` and runtime
`*.test.mjs` suites under `tests/`. Add TypeScript tests under
The build emits CommonJS JavaScript and declarations in `dist`, and native ESM
JavaScript and declarations in `dist/esm`.

Run the test suite with `npm test`. Package tests verify named and default imports,
both module formats, and their declaration resolution using only the files included
in the npm package.
The test command recursively discovers compiled `*.test.js` suites under `out/`
and runtime `*.test.mjs` suites under `tests/`. Add TypeScript tests under
[test/](https://github.com/microsoft/TypeChat/tree/main/typescript/test) or runtime
tests under [tests/](https://github.com/microsoft/TypeChat/tree/main/typescript/tests);
no manual suite-list update is needed.
Expand All @@ -59,6 +88,9 @@ validation and repair flow. Zod and custom validators without schema-only checks
their existing instance-validation behavior. The `TypeChatJsonValidator` contract is
unchanged; custom validators can opt in through the exported `typeChatSchemaValidation`
symbol, so existing string-named `validateSchema` methods are not invoked as preflight.
The symbol's runtime value and canonical declaration are shared across the ESM and
CommonJS builds, so validators remain type-compatible and schema preflight also runs
when a validator and translator come from different module formats.

To see TypeChat in action, we recommend exploring the [TypeChat example projects](https://github.com/microsoft/TypeChat/tree/main/typescript/examples). You can try them on your local machine or in a GitHub Codespace.

Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/calendar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/coffeeShop-zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"zod": "^4.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/coffeeShop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/crossword/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/drawing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/healthData/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.1.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/multiSchema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "MIT",
"dependencies": {
"dotenv": "^16.3.1",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"find-config": "^1.0.0",
"music": "^0.0.1",
"typescript": "^5.3.3"
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/music/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"find-config": "^1.0.0",
"open": "^7.0.4",
"sqlite3": "^6.0.1",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/restaurant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/sentiment-zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0"
"typechat": "^0.2.0"
},
"devDependencies": {
"@types/find-config": "1.0.4",
Expand Down
2 changes: 1 addition & 1 deletion typescript/examples/sentiment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"find-config": "^1.0.0",
"typechat": "^0.1.0",
"typechat": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
Expand Down
28 changes: 14 additions & 14 deletions typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 50 additions & 6 deletions typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typechat",
"author": "Microsoft",
"version": "0.1.4",
"version": "0.2.0",
"license": "MIT",
"description": "TypeChat is an experimental library that makes it easy to build natural language interfaces using types.",
"keywords": [
Expand All @@ -20,7 +20,9 @@
"url": "https://github.com/microsoft/TypeChat/issues"
},
"scripts": {
"build": "tsc -p src",
"build": "npm run build:cjs && npm run build:esm",
"build:cjs": "tsc -p src",
"build:esm": "tsc -p src/tsconfig.esm.json && node -e \"const fs = require('fs'); fs.writeFileSync('dist/esm/package.json', JSON.stringify({ type: 'module' })); fs.copyFileSync('dist/esm/schemaValidation.d.ts', 'dist/schemaValidation.d.ts'); fs.writeFileSync('dist/esm/schemaValidation.d.ts', 'export { typeChatSchemaValidation } from \\'../schemaValidation.js\\';\\n')\"",
"test": "npm run build && tsc -p test && node --test \"out/**/*.test.js\" \"tests/**/*.test.mjs\"",
"build-all": "npm run build && npm run build --workspaces",
"prepare": "npm run build-all",
Expand All @@ -29,13 +31,55 @@
"publish-package": "npm publish",
"publish-package:dry-run": "npm publish --dry-run"
},
"type": "commonjs",
"main": "./dist/index.js",
"module": "./dist/esm/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./ts": "./dist/ts/index.js",
"./zod": "./dist/zod/index.js",
"./interactive": "./dist/interactive/index.js"
".": {
"import": {
"types": "./dist/esm/index.d.mts",
Comment thread
robgruen marked this conversation as resolved.
Resolved
"default": "./dist/esm/index.mjs"
Comment thread
robgruen marked this conversation as resolved.
Resolved
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"default": "./dist/index.js"
},
"./ts": {
"import": {
"types": "./dist/esm/ts/index.d.mts",
"default": "./dist/esm/ts/index.mjs"
},
"require": {
"types": "./dist/ts/index.d.ts",
"default": "./dist/ts/index.js"
},
"default": "./dist/ts/index.js"
},
"./zod": {
"import": {
"types": "./dist/esm/zod/index.d.mts",
"default": "./dist/esm/zod/index.mjs"
},
"require": {
"types": "./dist/zod/index.d.ts",
"default": "./dist/zod/index.js"
},
"default": "./dist/zod/index.js"
},
"./interactive": {
"import": {
"types": "./dist/esm/interactive/index.d.mts",
"default": "./dist/esm/interactive/index.mjs"
},
"require": {
"types": "./dist/interactive/index.d.ts",
"default": "./dist/interactive/index.js"
},
"default": "./dist/interactive/index.js"
}
},
"engines": {
"node": ">=24.22.1"
Expand Down
2 changes: 2 additions & 0 deletions typescript/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './index.js';
export * as default from './index.js';
6 changes: 3 additions & 3 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './result';
export * from './model';
export * from './typechat';
export * from './result.js';
export * from './model.js';
export * from './typechat.js';
2 changes: 2 additions & 0 deletions typescript/src/interactive/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './index.js';
export * as default from './index.js';
2 changes: 1 addition & 1 deletion typescript/src/interactive/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './interactive';
export * from './interactive.js';
2 changes: 1 addition & 1 deletion typescript/src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Result, success, error, withResponseMetadata, TypeChatResponseMetadata, TypeChatTokenUsage } from "./result";
import { Result, success, error, withResponseMetadata, TypeChatResponseMetadata, TypeChatTokenUsage } from "./result.js";

/**
* Represents a section of an LLM prompt with an associated role. TypeChat uses the "user" role for
Expand Down
4 changes: 4 additions & 0 deletions typescript/src/schemaValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Optional capability for validators that can check schema configuration without an instance.
*/
export const typeChatSchemaValidation: unique symbol = Symbol.for("typechat.schemaValidation");
2 changes: 2 additions & 0 deletions typescript/src/ts/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './index.js';
export * as default from './index.js';
Loading
Loading