diff --git a/apis/.gitignore b/apis/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/apis/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/apis/Makefile b/apis/Makefile index a09a566..8b53d5b 100644 --- a/apis/Makefile +++ b/apis/Makefile @@ -1,9 +1,15 @@ +LIB_FILES :=$(wildcard lib/*.jsonnet) + OPENAI_FILES := $(wildcard openai/lib/*.jsonnet) \ $(wildcard openai/fixture/*.json) SCHEMA_FILES := $(wildcard schemas/*.json) -ALL_FILES := $(OPENAI_FILES) $(SCHEMA_FILES) +ANTHROPIC_FILES := $(wildcard anthropic/lib/*.jsonnet) + +DOC_FILES := README.md changelog.md + +ALL_FILES := $(DOC_FILES) $(SCHEMA_FILES) $(LIB_FILES) $(OPENAI_FILES) $(ANTHROPIC_FILES) ALL_DIST_FILES := $(addprefix dist/, $(ALL_FILES)) dist: $(ALL_DIST_FILES) @@ -11,3 +17,21 @@ dist: $(ALL_DIST_FILES) ./dist/%: % mkdir -p $(dir $@) cp $< $@ + +# -- + +TEST_DIRS := $(dir $(wildcard */test)) + +TEST_TRIGGERS := $(addsuffix .test-trigger, $(TEST_DIRS)) + +test: $(TEST_TRIGGERS) + +%.test-trigger: + cd $(dir $@) && npm test + +DEPS_TRIGGERS := $(addsuffix .deps-trigger, $(TEST_DIRS)) + +update-deps: $(DEPS_TRIGGERS) + +%.deps-trigger: + cd $(dir $@) && npm install tplfa-jsonnet --update diff --git a/apis/README.md b/apis/README.md index 1b728af..134104d 100644 --- a/apis/README.md +++ b/apis/README.md @@ -15,6 +15,38 @@ npm ci # once to setup the test project npm test # run tests ``` +## Sample run + +Using [jsonnet](https://jsonnet.org/) command-line tool: + +``` +jsonnet --jpath ./lib ./openai/lib/request-tpl.jsonnet -V prompt=hello \ + -V secret1=TOKEN -V secret2=orgid +``` + +Output: + +``` +{ + "body": { + "messages": [ + { + "content": "hello", + "role": "user" + } + ], + "model": "gpt-3.5-turbo" + }, + "headers": { + "Authorization": "Bearer TOKEN", + "Content-type": "application/json", + "OpenAI-Organization": "orgid" + }, + "method": "POST", + "url": "https://api.openai.com/v1/chat/completions" +} +``` + ## Request templates Input: @@ -70,5 +102,5 @@ The document structure will be more rich in the future. It will be based on the # Using in NPM ``` -npm install https://github.com/olpa/templating-for-api/releases/download/apis-v1.0.0/apis-v1.0.0.tar.gz +npm install https://github.com/olpa/templating-for-api/releases/download/apis-v1.1.1/apis-v1.1.1.tar.gz ``` diff --git a/apis/anthropic/fixture/response.json b/apis/anthropic/fixture/response.json new file mode 100644 index 0000000..497a5cb --- /dev/null +++ b/apis/anthropic/fixture/response.json @@ -0,0 +1,18 @@ +{ + "id": "msg_015ZPFSNXEvVHRymsG7H6Pab", + "type": "message", + "role": "assistant", + "model": "claude-3-5-sonnet-20240620", + "content": [ + { + "type": "text", + "text": "Pong!" + } + ], + "stop_reason": "end_turn", + "stop_sequence": null, + "usage": { + "input_tokens": 17, + "output_tokens": 55 + } +} diff --git a/apis/anthropic/lib/document-tpl.jsonnet b/apis/anthropic/lib/document-tpl.jsonnet new file mode 100644 index 0000000..673a830 --- /dev/null +++ b/apis/anthropic/lib/document-tpl.jsonnet @@ -0,0 +1,16 @@ +local response = std.parseJson(std.extVar("response")); + +{ + doc: [ + { + type: 'markdown', + content: [ + { + type: 'text', + text: item.text + } + ] + } + for item in response.content + ] +} diff --git a/apis/anthropic/lib/request-tpl.jsonnet b/apis/anthropic/lib/request-tpl.jsonnet new file mode 100644 index 0000000..1c6cc89 --- /dev/null +++ b/apis/anthropic/lib/request-tpl.jsonnet @@ -0,0 +1,11 @@ +local openai = import "openai-request-tpl.jsonnet"; + +openai.run("https://api.anthropic.com/v1/messages", "key") + { + headers+: { + "Anthropic-version": "2023-06-01", + }, + body+: { + "max_tokens": 1024, + "model": "claude-3-5-sonnet-20240620", + } +} diff --git a/apis/anthropic/package-lock.json b/apis/anthropic/package-lock.json new file mode 100644 index 0000000..f1a0c25 --- /dev/null +++ b/apis/anthropic/package-lock.json @@ -0,0 +1,1160 @@ +{ + "name": "anthropic", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "anthropic", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "tplfa-jsonnet": "file:../../jsonnet-js-ts/dist", + "ts-node": "^10.9.2" + }, + "devDependencies": { + "@types/chai": "^4.3.16", + "@types/mocha": "^10.0.7", + "@types/node": "^20.12.3", + "chai": "^4.5.0", + "mocha": "^10.7.0" + } + }, + "../../jsonnet-js-ts/dist": { + "name": "tplfa-jsonnet", + "version": "1.1.0", + "license": "MIT" + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@tsconfig/node10": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", + "integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==" + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==" + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==" + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==" + }, + "node_modules/@types/chai": { + "version": "4.3.16", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.3.16.tgz", + "integrity": "sha512-PatH4iOdyh3MyWtmHVFXLWCCIhUbopaltqddG9BzB+gMIzee2MJrvd+jouii9Z3wzQJruGWAm7WOMjgfG8hQlQ==", + "dev": true + }, + "node_modules/@types/mocha": { + "version": "10.0.7", + "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.7.tgz", + "integrity": "sha512-GN8yJ1mNTcFcah/wKEFIJckJx9iJLoMSzWcfRRuxz/Jk+U6KQNnml+etbtxFK8lPjzOw3zp4Ha/kjSst9fsHYw==", + "dev": true + }, + "node_modules/@types/node": { + "version": "20.12.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.3.tgz", + "integrity": "sha512-sD+ia2ubTeWrOu+YMF+MTAB7E+O7qsMqAbMfW7DG3K1URwhZ5hN1pLlRVGbf4wDFzSfikL05M17EyorS86jShw==", + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/acorn": { + "version": "8.11.3", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", + "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-walk": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.2.tgz", + "integrity": "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/ansi-colors": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz", + "integrity": "sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/ansi-regex": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==" + }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, + "node_modules/assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", + "dev": true + }, + "node_modules/binary-extensions": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", + "dev": true, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, + "license": "MIT", + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, + "node_modules/camelcase": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", + "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/chai": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.5.0.tgz", + "integrity": "sha512-RITGBfijLkBddZvnn8jdqoTypxvqbOLYQkGGxXzeFjVHvudaPw0HNFD9x928/eUwYWd2dPCugVqspGALTZZQKw==", + "dev": true, + "dependencies": { + "assertion-error": "^1.1.0", + "check-error": "^1.0.3", + "deep-eql": "^4.1.3", + "get-func-name": "^2.0.2", + "loupe": "^2.3.6", + "pathval": "^1.1.1", + "type-detect": "^4.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/chalk/node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/check-error": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.3.tgz", + "integrity": "sha512-iKEoDYaRmd1mxM90a2OEfWhjsjPpYPuQ+lMYsoxB126+t8fw7ySEO48nmDg5COTjxDI65/Y2OWpeEHk3ZOe8zg==", + "dev": true, + "dependencies": { + "get-func-name": "^2.0.2" + }, + "engines": { + "node": "*" + } + }, + "node_modules/chokidar": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", + "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", + "dev": true, + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dev": true, + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dev": true, + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dev": true + }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==" + }, + "node_modules/debug": { + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz", + "integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==", + "dev": true, + "dependencies": { + "ms": "2.1.2" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/debug/node_modules/ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", + "dev": true + }, + "node_modules/decamelize": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", + "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/deep-eql": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-4.1.4.tgz", + "integrity": "sha512-SUwdGfqdKOwxCPeVYjwSyRpJ7Z+fhpwIAtmCUdZIWZ/YP5R9WAsyuSgpLVDi9bjWoN2LXHNss/dk3urXtdQxGg==", + "dev": true, + "dependencies": { + "type-detect": "^4.0.0" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dev": true + }, + "node_modules/escalade": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.2.tgz", + "integrity": "sha512-ErCHMCae19vR8vQGe50xIsVomy19rg6gFu3+r3jkEO46suLMWBksvVyoGgQV+jOfl84ZSOSlmv6Gxa89PmTGmA==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/escape-string-regexp": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", + "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, + "license": "MIT", + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/find-up": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", + "dev": true, + "dependencies": { + "locate-path": "^6.0.0", + "path-exists": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/flat": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", + "integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==", + "dev": true, + "bin": { + "flat": "cli.js" + } + }, + "node_modules/fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", + "dev": true + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/get-caller-file": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", + "dev": true, + "engines": { + "node": "6.* || 8.* || >= 10.*" + } + }, + "node_modules/get-func-name": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.2.tgz", + "integrity": "sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/glob": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/he": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", + "dev": true, + "bin": { + "he": "bin/he" + } + }, + "node_modules/inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", + "dev": true, + "dependencies": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/is-plain-obj": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", + "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dev": true, + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, + "node_modules/locate-path": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", + "dev": true, + "dependencies": { + "p-locate": "^5.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dev": true, + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/loupe": { + "version": "2.3.7", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-2.3.7.tgz", + "integrity": "sha512-zSMINGVYkdpYSOBmLi0D1Uo7JU9nVdQKrHxC8eYlV+9YKK9WePqAlL7lSlorG/U2Fw1w0hTBmaa/jrQ3UbPHtA==", + "dev": true, + "dependencies": { + "get-func-name": "^2.0.1" + } + }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" + }, + "node_modules/minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/mocha": { + "version": "10.7.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-10.7.0.tgz", + "integrity": "sha512-v8/rBWr2VO5YkspYINnvu81inSz2y3ODJrhO175/Exzor1RcEZZkizgE2A+w/CAXXoESS8Kys5E62dOHGHzULA==", + "dev": true, + "dependencies": { + "ansi-colors": "^4.1.3", + "browser-stdout": "^1.3.1", + "chokidar": "^3.5.3", + "debug": "^4.3.5", + "diff": "^5.2.0", + "escape-string-regexp": "^4.0.0", + "find-up": "^5.0.0", + "glob": "^8.1.0", + "he": "^1.2.0", + "js-yaml": "^4.1.0", + "log-symbols": "^4.1.0", + "minimatch": "^5.1.6", + "ms": "^2.1.3", + "serialize-javascript": "^6.0.2", + "strip-json-comments": "^3.1.1", + "supports-color": "^8.1.1", + "workerpool": "^6.5.1", + "yargs": "^16.2.0", + "yargs-parser": "^20.2.9", + "yargs-unparser": "^2.0.0" + }, + "bin": { + "_mocha": "bin/_mocha", + "mocha": "bin/mocha.js" + }, + "engines": { + "node": ">= 14.0.0" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true + }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dev": true, + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/p-limit": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", + "dev": true, + "dependencies": { + "yocto-queue": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/p-locate": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", + "dev": true, + "dependencies": { + "p-limit": "^3.0.2" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/path-exists": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", + "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/pathval": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz", + "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/randombytes": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", + "dev": true, + "dependencies": { + "safe-buffer": "^5.1.0" + } + }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, + "node_modules/require-directory": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/serialize-javascript": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz", + "integrity": "sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==", + "dev": true, + "dependencies": { + "randombytes": "^2.1.0" + } + }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dev": true, + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-ansi": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dev": true, + "dependencies": { + "ansi-regex": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/strip-json-comments": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", + "dev": true, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/supports-color": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, + "node_modules/tplfa-jsonnet": { + "resolved": "../../jsonnet-js-ts/dist", + "link": true + }, + "node_modules/ts-node": { + "version": "10.9.2", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", + "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/ts-node/node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/type-detect": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.1.0.tgz", + "integrity": "sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==", + "dev": true, + "engines": { + "node": ">=4" + } + }, + "node_modules/typescript": { + "version": "5.4.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.3.tgz", + "integrity": "sha512-KrPd3PKaCLr78MalgiwJnA25Nm8HAmdwN3mYUYZgG/wizIo9EainNVQI9/yDavtVFRN2h3k8uf3GLHuhDMgEHg==", + "peer": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" + }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==" + }, + "node_modules/workerpool": { + "version": "6.5.1", + "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.5.1.tgz", + "integrity": "sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==", + "dev": true + }, + "node_modules/wrap-ansi": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", + "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/wrap-ansi?sponsor=1" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "dev": true + }, + "node_modules/y18n": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", + "dev": true, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dev": true, + "dependencies": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs-parser": { + "version": "20.2.9", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", + "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", + "dev": true, + "engines": { + "node": ">=10" + } + }, + "node_modules/yargs-unparser": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz", + "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==", + "dev": true, + "dependencies": { + "camelcase": "^6.0.0", + "decamelize": "^4.0.0", + "flat": "^5.0.2", + "is-plain-obj": "^2.1.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "engines": { + "node": ">=6" + } + }, + "node_modules/yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + } + } +} diff --git a/apis/anthropic/package.json b/apis/anthropic/package.json new file mode 100644 index 0000000..f7e1254 --- /dev/null +++ b/apis/anthropic/package.json @@ -0,0 +1,23 @@ +{ + "name": "anthropic", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "mocha --require ts-node/register test/**/*.ts" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "tplfa-jsonnet": "file:../../jsonnet-js-ts/dist", + "ts-node": "^10.9.2" + }, + "devDependencies": { + "@types/chai": "^4.3.16", + "@types/mocha": "^10.0.7", + "@types/node": "^20.12.3", + "chai": "^4.5.0", + "mocha": "^10.7.0" + } +} diff --git a/apis/anthropic/test/claude-test.ts b/apis/anthropic/test/claude-test.ts new file mode 100644 index 0000000..31c2faa --- /dev/null +++ b/apis/anthropic/test/claude-test.ts @@ -0,0 +1,61 @@ +import fs from 'fs'; +import 'tplfa-jsonnet/wasm_exec.js'; +import { Jsonnet, getJsonnet } from 'tplfa-jsonnet/jsonnet'; +import chai from 'chai'; +import fixtureRequest from '../../openai/fixture/request.json'; +import fixtureResponse from '../fixture/response.json'; +import fixtureDocument from '../../openai/fixture/document.json'; +chai.config.truncateThreshold = 0; + +const url = 'https://api.anthropic.com/v1/messages'; + +describe('anthropic clause', () => { + let jsonnet: Jsonnet; + const libFileName = 'openai-request-tpl.jsonnet' + const libRequestTpl = fs.readFileSync(`${__dirname}/../../lib/${libFileName}`, 'utf-8'); + const requestTpl = fs.readFileSync(`${__dirname}/../lib/request-tpl.jsonnet`, 'utf-8'); + const documentTpl = fs.readFileSync(`${__dirname}/../lib/document-tpl.jsonnet`, 'utf-8'); + + before(async () => { + const jsonnetWasm = await fs.promises.readFile( + require.resolve('tplfa-jsonnet/libjsonnet.wasm') + ); + jsonnet = await getJsonnet(jsonnetWasm); + }); + + + const expectedInHappyPath = { + url, + method: 'POST', + headers: { + 'Content-type': 'application/json', + 'X-api-key': 'is a secret1', + 'Anthropic-version': '2023-06-01', + }, + body: { + ...fixtureRequest, + model: 'claude-3-5-sonnet-20240620', + max_tokens: 1024, + } + }; + + it('to request: happy path', async () => { + const backStr = await jsonnet.evaluate(requestTpl, { + prompt: 'Ping!', + secret1: 'is a secret1', + secret2: 'is a secret2', + }, { [libFileName]: libRequestTpl }); + + const back = JSON.parse(backStr); + chai.expect(back).to.eql(expectedInHappyPath); + }); + + it('response to document: happy path', async () => { + const doc = JSON.parse(await jsonnet.evaluate( + documentTpl, + { response: JSON.stringify(fixtureResponse)} + )); + + chai.expect(doc).to.eql(fixtureDocument); + }); +}); diff --git a/apis/anthropic/tsconfig.json b/apis/anthropic/tsconfig.json new file mode 100644 index 0000000..b2db2e5 --- /dev/null +++ b/apis/anthropic/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "es2016", + "module": "commonjs", + "outDir": "./dist", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "resolveJsonModule": true + } +} diff --git a/apis/changelog.md b/apis/changelog.md new file mode 100644 index 0000000..eba288d --- /dev/null +++ b/apis/changelog.md @@ -0,0 +1,13 @@ +# Changelog + +## [2024-10-02] + +- Add Anthropic Claude model +- Extract shared jsonnet code to a library +- Use the library in the Openai template +- Add a sample `jsonnet` run to `README` + + +## [2024-07-29] + +- First release diff --git a/apis/dist/README.md b/apis/dist/README.md new file mode 100644 index 0000000..134104d --- /dev/null +++ b/apis/dist/README.md @@ -0,0 +1,106 @@ +# API definitions + +## File layout, running a test + +For an api `foo`, the templates are: + +- `foo/lib/request-tpl.jsonnet` and +- `foo/lib/document-tpl.jsonnet`. + +There are also test files to check conversion. + +``` +cd foo +npm ci # once to setup the test project +npm test # run tests +``` + +## Sample run + +Using [jsonnet](https://jsonnet.org/) command-line tool: + +``` +jsonnet --jpath ./lib ./openai/lib/request-tpl.jsonnet -V prompt=hello \ + -V secret1=TOKEN -V secret2=orgid +``` + +Output: + +``` +{ + "body": { + "messages": [ + { + "content": "hello", + "role": "user" + } + ], + "model": "gpt-3.5-turbo" + }, + "headers": { + "Authorization": "Bearer TOKEN", + "Content-type": "application/json", + "OpenAI-Organization": "orgid" + }, + "method": "POST", + "url": "https://api.openai.com/v1/chat/completions" +} +``` + +## Request templates + +Input: + +- `secret1`: First secret, usually an API key +- `secret2`: Optionally second secret. In case of OpenAI API, it's an organization ID +- `prompt` +- `parent`: When using in a chain of templates, `parent` contains the output of the previous template. See `openai-patch-example` how to patch the transformation. + +Output: + +An object, ready to use for [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch). The expected fields: + +- `url` +- `method` +- `headers` +- `body`: Type is the `json object`, not `string` + +The header `Content-type: application/json` is often required by APIs, even if not mentioned in documentation. + +There is a JSON schema for validation: [./schemas/request.json](./schemas/request.json). + +## Document templates + +Input: + +- `response`: body payload + +Output: + +``` +{ + doc: [ + { + type: 'markdown', + content: [ + { + type: 'text', + text: ... whatever ... + } + ] + } + ] +} +``` + +There could be several `type: markdown' objects. + +There is a JSON schema for validation: [./schemas/document.json](./schemas/document.json). + +The document structure will be more rich in the future. It will be based on the [ProseMirror's document model](https://prosemirror.net/docs/guide/#doc). + +# Using in NPM + +``` +npm install https://github.com/olpa/templating-for-api/releases/download/apis-v1.1.1/apis-v1.1.1.tar.gz +``` diff --git a/apis/dist/anthropic/lib/document-tpl.jsonnet b/apis/dist/anthropic/lib/document-tpl.jsonnet new file mode 100644 index 0000000..673a830 --- /dev/null +++ b/apis/dist/anthropic/lib/document-tpl.jsonnet @@ -0,0 +1,16 @@ +local response = std.parseJson(std.extVar("response")); + +{ + doc: [ + { + type: 'markdown', + content: [ + { + type: 'text', + text: item.text + } + ] + } + for item in response.content + ] +} diff --git a/apis/dist/anthropic/lib/request-tpl.jsonnet b/apis/dist/anthropic/lib/request-tpl.jsonnet new file mode 100644 index 0000000..1c6cc89 --- /dev/null +++ b/apis/dist/anthropic/lib/request-tpl.jsonnet @@ -0,0 +1,11 @@ +local openai = import "openai-request-tpl.jsonnet"; + +openai.run("https://api.anthropic.com/v1/messages", "key") + { + headers+: { + "Anthropic-version": "2023-06-01", + }, + body+: { + "max_tokens": 1024, + "model": "claude-3-5-sonnet-20240620", + } +} diff --git a/apis/dist/changelog.md b/apis/dist/changelog.md new file mode 100644 index 0000000..eba288d --- /dev/null +++ b/apis/dist/changelog.md @@ -0,0 +1,13 @@ +# Changelog + +## [2024-10-02] + +- Add Anthropic Claude model +- Extract shared jsonnet code to a library +- Use the library in the Openai template +- Add a sample `jsonnet` run to `README` + + +## [2024-07-29] + +- First release diff --git a/apis/dist/openai/lib/document-tpl.jsonnet b/apis/dist/lib/openai-document-tpl.jsonnet similarity index 100% rename from apis/dist/openai/lib/document-tpl.jsonnet rename to apis/dist/lib/openai-document-tpl.jsonnet diff --git a/apis/dist/lib/openai-request-tpl.jsonnet b/apis/dist/lib/openai-request-tpl.jsonnet new file mode 100644 index 0000000..9589a1b --- /dev/null +++ b/apis/dist/lib/openai-request-tpl.jsonnet @@ -0,0 +1,29 @@ +local secret1 = std.extVar("secret1"); +local prompt = std.extVar("prompt"); + +local run(url, auth_loc) = { + "url": url, + "headers": { + "Content-type": "application/json", + } + ( + if auth_loc == "bearer" then + { "Authorization": "Bearer " + secret1 } + else if auth_loc == "key" then + { "X-api-key": secret1 } + else + error "Unknown auth_loc" + ) + , + "body": { + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "user", + "content": prompt + } + ] + }, + "method": "POST" +}; + +{ "run": run } diff --git a/apis/dist/openai/lib/request-tpl.jsonnet b/apis/dist/openai/lib/request-tpl.jsonnet index a2004d7..c4ee232 100644 --- a/apis/dist/openai/lib/request-tpl.jsonnet +++ b/apis/dist/openai/lib/request-tpl.jsonnet @@ -1,22 +1,11 @@ -local secret1 = std.extVar("secret1"); +local openai = import "openai-request-tpl.jsonnet"; local secret2 = std.extVar("secret2"); -local prompt = std.extVar("prompt"); -{ - "url": "https://api.openai.com/v1/chat/completions", - "headers": { - "Content-type": "application/json", - "Authorization": "Bearer " + secret1, - [if secret2 != '' then "OpenAI-Organization"]: secret2, +openai.run("https://api.openai.com/v1/chat/completions", "bearer") + { + headers+: { + [if secret2 != "" then "OpenAI-Organization"]: secret2, }, - "body": { - "model": "gpt-3.5-turbo", - "messages": [ - { - "role": "user", - "content": prompt - } - ] + body+: { + model: "gpt-3.5-turbo", }, - "method": "POST" } diff --git a/apis/dist/package.json b/apis/dist/package.json index a64877e..de02066 100644 --- a/apis/dist/package.json +++ b/apis/dist/package.json @@ -1,6 +1,6 @@ { "name": "tplfa-apis", - "version": "1.0.0", + "version": "1.1.1", "description": "APIs for API templating", "keywords": [], "author": "Oleg Parashchenko", diff --git a/apis/openai/lib/document-tpl.jsonnet b/apis/lib/openai-document-tpl.jsonnet similarity index 100% rename from apis/openai/lib/document-tpl.jsonnet rename to apis/lib/openai-document-tpl.jsonnet diff --git a/apis/lib/openai-request-tpl.jsonnet b/apis/lib/openai-request-tpl.jsonnet new file mode 100644 index 0000000..9589a1b --- /dev/null +++ b/apis/lib/openai-request-tpl.jsonnet @@ -0,0 +1,29 @@ +local secret1 = std.extVar("secret1"); +local prompt = std.extVar("prompt"); + +local run(url, auth_loc) = { + "url": url, + "headers": { + "Content-type": "application/json", + } + ( + if auth_loc == "bearer" then + { "Authorization": "Bearer " + secret1 } + else if auth_loc == "key" then + { "X-api-key": secret1 } + else + error "Unknown auth_loc" + ) + , + "body": { + "model": "gpt-3.5-turbo", + "messages": [ + { + "role": "user", + "content": prompt + } + ] + }, + "method": "POST" +}; + +{ "run": run } diff --git a/apis/openai/lib/request-tpl.jsonnet b/apis/openai/lib/request-tpl.jsonnet index a2004d7..c4ee232 100644 --- a/apis/openai/lib/request-tpl.jsonnet +++ b/apis/openai/lib/request-tpl.jsonnet @@ -1,22 +1,11 @@ -local secret1 = std.extVar("secret1"); +local openai = import "openai-request-tpl.jsonnet"; local secret2 = std.extVar("secret2"); -local prompt = std.extVar("prompt"); -{ - "url": "https://api.openai.com/v1/chat/completions", - "headers": { - "Content-type": "application/json", - "Authorization": "Bearer " + secret1, - [if secret2 != '' then "OpenAI-Organization"]: secret2, +openai.run("https://api.openai.com/v1/chat/completions", "bearer") + { + headers+: { + [if secret2 != "" then "OpenAI-Organization"]: secret2, }, - "body": { - "model": "gpt-3.5-turbo", - "messages": [ - { - "role": "user", - "content": prompt - } - ] + body+: { + model: "gpt-3.5-turbo", }, - "method": "POST" } diff --git a/apis/openai/package-lock.json b/apis/openai/package-lock.json index 62f93f5..04b0ea3 100644 --- a/apis/openai/package-lock.json +++ b/apis/openai/package-lock.json @@ -22,7 +22,7 @@ }, "../../jsonnet-js-ts/dist": { "name": "tplfa-jsonnet", - "version": "1.0.1", + "version": "1.1.0", "license": "MIT" }, "node_modules/@cspotcode/source-map-support": { @@ -211,12 +211,13 @@ } }, "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", "dev": true, + "license": "MIT", "dependencies": { - "fill-range": "^7.0.1" + "fill-range": "^7.1.1" }, "engines": { "node": ">=8" @@ -443,10 +444,11 @@ } }, "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", "dev": true, + "license": "MIT", "dependencies": { "to-regex-range": "^5.0.1" }, @@ -629,6 +631,7 @@ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true, + "license": "MIT", "engines": { "node": ">=0.12.0" } @@ -959,6 +962,7 @@ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dev": true, + "license": "MIT", "dependencies": { "is-number": "^7.0.0" }, diff --git a/apis/openai/test/openai-test.ts b/apis/openai/test/openai-test.ts index 7f0fa3e..51371c7 100644 --- a/apis/openai/test/openai-test.ts +++ b/apis/openai/test/openai-test.ts @@ -9,8 +9,13 @@ chai.config.truncateThreshold = 0; const url = 'https://api.openai.com/v1/chat/completions'; +const libDir = `${__dirname}/../../lib`; + describe('open ai', () => { let jsonnet: Jsonnet; + const libFiles = { + 'openai-request-tpl.jsonnet': fs.readFileSync(`${libDir}/openai-request-tpl.jsonnet`, 'utf-8'), + } before(async () => { const jsonnetWasm = await fs.promises.readFile( @@ -38,7 +43,7 @@ describe('open ai', () => { prompt: 'Ping!', secret1: 'is a secret1', secret2: 'is a secret2', - }); + }, libFiles); const back = JSON.parse(backStr); chai.expect(back).to.eql(expectedInHappyPath); @@ -49,7 +54,7 @@ describe('open ai', () => { prompt: 'Ping!', secret1: 'is a secret1', secret2: '', - }); + }, libFiles); const back = JSON.parse(backStr); chai.expect(back).to.eql({ @@ -69,7 +74,7 @@ describe('open ai', () => { const backStr = await jsonnet.evaluate(patchTpl, { parent: JSON.stringify(expectedInHappyPath), - }); + }, libFiles); const back = JSON.parse(backStr); chai.expect(back).to.eql({ @@ -85,7 +90,7 @@ describe('open ai', () => { }); describe('response to document', () => { - const documentTpl = fs.readFileSync(`${__dirname}/../lib/document-tpl.jsonnet`, 'utf-8'); + const documentTpl = fs.readFileSync(`${libDir}/openai-document-tpl.jsonnet`, 'utf-8'); it('happy path', async () => { const doc = JSON.parse(await jsonnet.evaluate( diff --git a/in-action/nodejs-jsonnet/package-lock.json b/in-action/nodejs-jsonnet/package-lock.json index 4be8eae..09903b3 100644 --- a/in-action/nodejs-jsonnet/package-lock.json +++ b/in-action/nodejs-jsonnet/package-lock.json @@ -13,7 +13,7 @@ }, "../../jsonnet-js-ts/dist": { "name": "tplfa-jsonnet", - "version": "1.0.1", + "version": "1.1.0", "license": "MIT" }, "node_modules/@cspotcode/source-map-support": { diff --git a/in-action/nodejs-tplfa/README.md b/in-action/nodejs-tplfa/README.md index e889625..860cf52 100644 --- a/in-action/nodejs-tplfa/README.md +++ b/in-action/nodejs-tplfa/README.md @@ -5,20 +5,28 @@ Sample run: ``` npm ci # run once to setup the project -export OPENAI_API_KEY=.... +export LLM_API_KEY=.... ./node_modules/.bin/ts-node src/index.ts \ --api ../../apis/openai/lib \ - --api ../../apis/openai-patch-example/lib \ - --prompt "Tell me a joke about OpenAI." + --prompt "Hi! Who are you and who created you?" ``` Options: - `--help` - `--debug` -- `--secret1`: default is the value of the environment variable `OPENAI_API_KEY` +- `--secret1`: default is the value of the environment variable `LLM_API_KEY` - `--secret2` - `--prompt` - `--api`: Path to the api dir with files `request-tpl.jsonnet` and `document-tpl.jsonnet`. Several `--api` options create a chain of templates: the output of the previous template is passed to the next template as the parameter `parent` The values of `secret1`, `secret2` and `prompt` are passed as the corresponding parameters to the request templates. See [../../apis/README.md](../../apis/README.md). + +Example how to run a patch template: + +``` +./node_modules/.bin/ts-node src/index.ts \ + --api ../../apis/openai/lib \ + --api ../../apis/openai-patch-example/lib \ + --prompt "Hi!" +``` diff --git a/in-action/nodejs-tplfa/package-lock.json b/in-action/nodejs-tplfa/package-lock.json index c604348..c918a4a 100644 --- a/in-action/nodejs-tplfa/package-lock.json +++ b/in-action/nodejs-tplfa/package-lock.json @@ -27,23 +27,23 @@ }, "../../apis/dist": { "name": "tplfa-apis", - "version": "1.0.0", + "version": "1.1.1", "license": "MIT" }, "../../jsonnet-js-ts/dist": { "name": "tplfa-jsonnet", - "version": "1.0.1", + "version": "1.1.0", "license": "MIT" }, "../../tplfa-js/dist": { "name": "tplfa", - "version": "1.0.0", + "version": "1.1.1", "license": "MIT", "dependencies": { "ajv": "^8.12.0", "node-fetch": "^2.6.1", - "tplfa-apis": "^1.0.0", - "tplfa-jsonnet": "^1.0.0" + "tplfa-apis": "^1.1.1", + "tplfa-jsonnet": "^1.1.0" }, "devDependencies": { "@types/ajv": "^1.0.0", diff --git a/in-action/nodejs-tplfa/src/index.ts b/in-action/nodejs-tplfa/src/index.ts index 1a9af49..2a6ff05 100644 --- a/in-action/nodejs-tplfa/src/index.ts +++ b/in-action/nodejs-tplfa/src/index.ts @@ -5,6 +5,7 @@ import { getJsonnet } from 'tplfa-jsonnet/jsonnet'; import { LoadedTemplate, TplfaResultOrError } from 'tplfa/tplfa-types'; import { TplfaValidator } from 'tplfa/tplfa-validator'; import { TemplatingForApi } from 'tplfa/templating-for-api'; +import { loadLibTemplates, loadTemplate } from 'tplfa/nodejs-loader'; import { ApiClient } from 'tplfa/api-client'; interface ToolArguments { @@ -33,7 +34,7 @@ async function parseCommandLine(): Promise< }) .option('secret1', { describe: 'Secret value 1', - default: process.env.OPENAI_API_KEY ?? '', + default: process.env.LLM_API_KEY ?? '', type: 'string', }) .option('secret2', { @@ -74,7 +75,8 @@ async function main(): Promise { ); const jsonnet = await getJsonnet(jsonnetWasm); const validator = new TplfaValidator(); - const tplfa = new TemplatingForApi(jsonnet, validator); + const libTemplates = await loadLibTemplates(); + const tplfa = new TemplatingForApi(jsonnet, validator, libTemplates); // // API template loader @@ -82,25 +84,14 @@ async function main(): Promise { async function templateLoader( templatePath: string ): Promise> { - function readFileNoExc(path: string): string | undefined { - try { - return fs.readFileSync(path, 'utf-8'); - } catch { - return undefined; - } - } + const tpl = await loadTemplate(templatePath, libTemplates); return { ok: true, result: { - requestTpl: readFileNoExc( - `${templatePath}/request-tpl.jsonnet` - ), - documentTpl: readFileNoExc( - `${templatePath}/document-tpl.jsonnet` - ), + ...tpl, hasDebugFlag: argv.debug, - }, - }; + } + } } // diff --git a/in-action/web-jsonnet/README.md b/in-action/web-jsonnet/README.md index 4c9e709..f1bd764 100644 --- a/in-action/web-jsonnet/README.md +++ b/in-action/web-jsonnet/README.md @@ -8,7 +8,7 @@ To start the server: ``` -python3 serve.py +python3 server.py ``` Then point a browser to . diff --git a/jsonnet-js-ts/README.md b/jsonnet-js-ts/README.md index f189c69..1656071 100644 --- a/jsonnet-js-ts/README.md +++ b/jsonnet-js-ts/README.md @@ -8,7 +8,7 @@ For sample usage, see Installation: ``` -npm install https://github.com/olpa/templating-for-api/releases/download/jsonnet-v1.0.1/jsonnet-v1.0.1.tar.gz +npm install https://github.com/olpa/templating-for-api/releases/download/jsonnet-v1.1.0/jsonnet-v1.1.0.tar.gz ``` In code: @@ -27,7 +27,7 @@ jsonnet = await getJsonnet(jsonnetWasm); The object `jsonnet` provides two functions: - `jsonnet_evaluate_snippet`: Wrapper for the corresponding jsonnet library function -- `evaluate`: Simplified interface. Only code and its external variables +- `evaluate`: Simplified interface. Only code, its external variables, and library files Compatibility note: diff --git a/jsonnet-js-ts/build-dist.sh b/jsonnet-js-ts/build-dist.sh index 70bd45d..5d51ad9 100755 --- a/jsonnet-js-ts/build-dist.sh +++ b/jsonnet-js-ts/build-dist.sh @@ -12,3 +12,5 @@ tsc /src/jsonnet.ts --outDir /dist/ --target es2017 --module commonjs --declarat cp /dist/jsonnet.js /dist/jsonnet-web.js && sed 's/^.*exports.*$//' --in-place /dist/jsonnet-web.js " + +cp README.md changelog.md ./dist/ diff --git a/jsonnet-js-ts/changelog.md b/jsonnet-js-ts/changelog.md new file mode 100644 index 0000000..95d5525 --- /dev/null +++ b/jsonnet-js-ts/changelog.md @@ -0,0 +1,10 @@ +# Changelog for jsonnet-js-ts + +## [1.1.0] - 2024-10-02 + +- Expose the argument for the library files + + +## [1.0.0] - 2024-07-24 + +- First release diff --git a/jsonnet-js-ts/dist/README.md b/jsonnet-js-ts/dist/README.md new file mode 100644 index 0000000..1656071 --- /dev/null +++ b/jsonnet-js-ts/dist/README.md @@ -0,0 +1,34 @@ +# jsonnet for TypeScript and JavaScript + +For sample usage, see + +- [../in-action/web-jsonnet/](../in-action/web-jsonnet/) +- [../in-action/nodejs-jsonnet/](../in-action/nodejs-jsonnet/) + +Installation: + +``` +npm install https://github.com/olpa/templating-for-api/releases/download/jsonnet-v1.1.0/jsonnet-v1.1.0.tar.gz +``` + +In code: + +``` +import 'tplfa-jsonnet/wasm_exec.js'; +import { Jsonnet, getJsonnet } from 'tplfa-jsonnet/jsonnet'; +... + +const jsonnetWasm = await fs.promises.readFile( + require.resolve('tplfa-jsonnet/libjsonnet.wasm') +); +jsonnet = await getJsonnet(jsonnetWasm); +``` + +The object `jsonnet` provides two functions: + +- `jsonnet_evaluate_snippet`: Wrapper for the corresponding jsonnet library function +- `evaluate`: Simplified interface. Only code, its external variables, and library files + +Compatibility note: + +For old versions of node you need to edit the file `./dist/wasm_exec.js`. After the line `use strict` add line `globalThis.crypto ??= require('crypto');`. Maybe more changes are needed: . diff --git a/jsonnet-js-ts/dist/changelog.md b/jsonnet-js-ts/dist/changelog.md new file mode 100644 index 0000000..95d5525 --- /dev/null +++ b/jsonnet-js-ts/dist/changelog.md @@ -0,0 +1,10 @@ +# Changelog for jsonnet-js-ts + +## [1.1.0] - 2024-10-02 + +- Expose the argument for the library files + + +## [1.0.0] - 2024-07-24 + +- First release diff --git a/jsonnet-js-ts/dist/jsonnet-web.js b/jsonnet-js-ts/dist/jsonnet-web.js index 978aba2..587640c 100644 --- a/jsonnet-js-ts/dist/jsonnet-web.js +++ b/jsonnet-js-ts/dist/jsonnet-web.js @@ -14,9 +14,9 @@ async function getJsonnet(jnWasm) { if (!jsonnet_evaluate_snippet) { throw new Error('libjsonnet: `jsonnet_evaluate_snippet` is missing'); } - async function evaluate(code, vars) { + async function evaluate(code, vars, files = {}) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return jsonnet_evaluate_snippet('anonymous', code, { anonymous: code }, vars !== null && vars !== void 0 ? vars : {}, {}, {}, {}); + return jsonnet_evaluate_snippet('anonymous', code, Object.assign(Object.assign({}, files), { anonymous: code }), vars !== null && vars !== void 0 ? vars : {}, {}, {}, {}); } jsonnet = { jsonnet_evaluate_snippet, diff --git a/jsonnet-js-ts/dist/jsonnet.d.ts b/jsonnet-js-ts/dist/jsonnet.d.ts index c7ab6f2..945656f 100644 --- a/jsonnet-js-ts/dist/jsonnet.d.ts +++ b/jsonnet-js-ts/dist/jsonnet.d.ts @@ -1,5 +1,5 @@ export type Jsonnet = { jsonnet_evaluate_snippet: (filename: string, code: string, files: Record, extrStrs: Record, extrCodes: Record, tlaStrs: Record, tlaCodes: Record) => Promise; - evaluate: (code: string, extrStrs?: Record) => Promise; + evaluate: (code: string, extrStrs?: Record, files?: Record) => Promise; }; export declare function getJsonnet(jnWasm: Promise | BufferSource): Promise; diff --git a/jsonnet-js-ts/dist/jsonnet.js b/jsonnet-js-ts/dist/jsonnet.js index b56bc52..9dc3a87 100644 --- a/jsonnet-js-ts/dist/jsonnet.js +++ b/jsonnet-js-ts/dist/jsonnet.js @@ -14,9 +14,9 @@ async function getJsonnet(jnWasm) { if (!jsonnet_evaluate_snippet) { throw new Error('libjsonnet: `jsonnet_evaluate_snippet` is missing'); } - async function evaluate(code, vars) { + async function evaluate(code, vars, files = {}) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return jsonnet_evaluate_snippet('anonymous', code, { anonymous: code }, vars !== null && vars !== void 0 ? vars : {}, {}, {}, {}); + return jsonnet_evaluate_snippet('anonymous', code, Object.assign(Object.assign({}, files), { anonymous: code }), vars !== null && vars !== void 0 ? vars : {}, {}, {}, {}); } jsonnet = { jsonnet_evaluate_snippet, diff --git a/jsonnet-js-ts/dist/package.json b/jsonnet-js-ts/dist/package.json index c6eccce..e7adc63 100644 --- a/jsonnet-js-ts/dist/package.json +++ b/jsonnet-js-ts/dist/package.json @@ -1,6 +1,6 @@ { "name": "tplfa-jsonnet", - "version": "1.0.1", + "version": "1.1.0", "description": "TS and JS bindings for Jsonnet", "keywords": [ "jsonnet" diff --git a/jsonnet-js-ts/src/jsonnet.ts b/jsonnet-js-ts/src/jsonnet.ts index e4834d6..9c20134 100644 --- a/jsonnet-js-ts/src/jsonnet.ts +++ b/jsonnet-js-ts/src/jsonnet.ts @@ -11,7 +11,8 @@ export type Jsonnet = { evaluate: ( code: string, - extrStrs?: Record + extrStrs?: Record, + files?: Record, ) => Promise; }; @@ -47,13 +48,14 @@ export async function getJsonnet( async function evaluate( code: string, - vars?: Record + vars?: Record, + files: Record = {}, ): Promise { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return jsonnet_evaluate_snippet!( 'anonymous', code, - { anonymous: code }, + { ...files, anonymous: code }, vars ?? {}, {}, {}, diff --git a/tplfa-js/README.md b/tplfa-js/README.md index 6e4ba27..b7be226 100644 --- a/tplfa-js/README.md +++ b/tplfa-js/README.md @@ -3,7 +3,7 @@ Installation: ``` -npm install https://github.com/olpa/templating-for-api/releases/download/tplfa-v1.0.0/tplfa-v1.0.0.tar.gz +npm install https://github.com/olpa/templating-for-api/releases/download/tplfa-v1.1.1/tplfa-v1.1.1.tar.gz ``` ## Client workflow @@ -19,6 +19,7 @@ Run a query-template: - Put the prompt and the secret to `TplfaReqVars` - If the template is a part of a chain, put the previous output to `parent` - Load the code of the request template to `requestTemplate` + - If the template is from `tpfa-apis` package, use `loadLibTemplates` followed by `loadTemplate` from `nodejs-loader.ts` - Call `TemplatingForApi.toTplfaRequest` - You get a validated output of type `TplfaRequest` diff --git a/tplfa-js/build.sh b/tplfa-js/build.sh index 890954a..3aa0830 100755 --- a/tplfa-js/build.sh +++ b/tplfa-js/build.sh @@ -1,3 +1,12 @@ tsc + +mv ./dist/src/* ./dist +rm ./dist/test/* +rmdir ./dist/src ./dist/test + cp package.json ./dist/ -sed 's/"file:.*"/"^1.0.0"/' --in-place ./dist/package.json + +version_apis=$(cat ./node_modules/tplfa-apis/package.json | jq -r .version) +version_jsonnet=$(cat ./node_modules/tplfa-jsonnet/package.json | jq -r .version) +sed 's/"file:.*apis.*"/"^'$version_apis'"/' --in-place ./dist/package.json +sed 's/"file:.*jsonnet.*"/"^'$version_jsonnet'"/' --in-place ./dist/package.json diff --git a/tplfa-js/changelog.md b/tplfa-js/changelog.md new file mode 100644 index 0000000..07272e7 --- /dev/null +++ b/tplfa-js/changelog.md @@ -0,0 +1,11 @@ +# Changelog + +## [1.1.1] - 2024-10-02 + +- Loader of "tplfa-api" library files and templates +- The constructor of "TemplatingForApi" requires a map with library files + + +## [1.0.0] - 2024-08-01 + +- First release diff --git a/tplfa-js/dist/README.md b/tplfa-js/dist/README.md new file mode 100644 index 0000000..b7be226 --- /dev/null +++ b/tplfa-js/dist/README.md @@ -0,0 +1,45 @@ +# JS/TS utilities + +Installation: + +``` +npm install https://github.com/olpa/templating-for-api/releases/download/tplfa-v1.1.1/tplfa-v1.1.1.tar.gz +``` + +## Client workflow + +For setup, create instances of + +- `jsonnet` from `tplfa-jsonnet` package +- `TplfaValidator` +- `TemplatingForApi` + +Run a query-template: + +- Put the prompt and the secret to `TplfaReqVars` + - If the template is a part of a chain, put the previous output to `parent` +- Load the code of the request template to `requestTemplate` + - If the template is from `tpfa-apis` package, use `loadLibTemplates` followed by `loadTemplate` from `nodejs-loader.ts` +- Call `TemplatingForApi.toTplfaRequest` + - You get a validated output of type `TplfaRequest` + +Query an API: + +- Call `TemplatingForApi.toRequest` to convert `Request` from `tplfa` type to `fetch` type +- Call `fetch` + +Run a document-template: + +Similar to running a query-template. + + +## Chain of templates + +Workflow: + +- Define a template loader +- Create an instance of `ApiClient` +- Create a chain of templates as `TplfaTransformationVars[]` +- `ApiClient.call` + +Complete example: [../in-action/nodejs-tplfa/](../in-action/nodejs-tplfa/). diff --git a/tplfa-js/dist/changelog.md b/tplfa-js/dist/changelog.md new file mode 100644 index 0000000..07272e7 --- /dev/null +++ b/tplfa-js/dist/changelog.md @@ -0,0 +1,11 @@ +# Changelog + +## [1.1.1] - 2024-10-02 + +- Loader of "tplfa-api" library files and templates +- The constructor of "TemplatingForApi" requires a map with library files + + +## [1.0.0] - 2024-08-01 + +- First release diff --git a/tplfa-js/dist/nodejs-loader.d.ts b/tplfa-js/dist/nodejs-loader.d.ts new file mode 100644 index 0000000..132e6ae --- /dev/null +++ b/tplfa-js/dist/nodejs-loader.d.ts @@ -0,0 +1,8 @@ +import { LibTemplates } from './tplfa-types'; +export declare function getApisDir(): string; +export declare function loadLibTemplates(): Promise; +export type DefinedLoadedTemplate = { + requestTpl: string; + documentTpl: string; +}; +export declare function loadTemplate(tplPath: string, libTemplates: LibTemplates): Promise; diff --git a/tplfa-js/dist/nodejs-loader.js b/tplfa-js/dist/nodejs-loader.js new file mode 100644 index 0000000..f184d23 --- /dev/null +++ b/tplfa-js/dist/nodejs-loader.js @@ -0,0 +1,51 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.getApisDir = getApisDir; +exports.loadLibTemplates = loadLibTemplates; +exports.loadTemplate = loadTemplate; +const path_1 = __importDefault(require("path")); +const fs_1 = require("fs"); +function getApisDir() { + const rootFile = require.resolve('tplfa-apis/package.json'); + return path_1.default.dirname(rootFile); +} +async function loadLibTemplates() { + const apisDir = getApisDir(); + const files = [ + 'openai-document-tpl.jsonnet', + 'openai-request-tpl.jsonnet', + ]; + const loaded = files.map(async (fname) => [ + fname, + await fs_1.promises.readFile(path_1.default.join(apisDir, 'lib', fname), 'utf-8'), + ]); + return Object.fromEntries(await Promise.all(loaded)); +} +async function loadTemplate(tplPath, libTemplates) { + const toLoad = [ + ['requestTpl', 'request-tpl.jsonnet', 'openai-request-tpl.jsonnet'], + [ + 'documentTpl', + 'document-tpl.jsonnet', + 'openai-document-tpl.jsonnet', + ], + ]; + const loaded = toLoad.map(async ([field, fnameBase, libField]) => { + const fname = path_1.default.join(tplPath, fnameBase); + try { + const code = await fs_1.promises.readFile(fname, 'utf-8'); + return [field, code]; + } + catch (err) { + const { code } = err; + if (code !== 'ENOENT') { + throw err; + } + return [field, libTemplates[libField]]; + } + }); + return Object.fromEntries(await Promise.all(loaded)); +} diff --git a/tplfa-js/dist/package.json b/tplfa-js/dist/package.json index f1d9b40..1e2b9a7 100644 --- a/tplfa-js/dist/package.json +++ b/tplfa-js/dist/package.json @@ -1,19 +1,19 @@ { "name": "tplfa", - "version": "1.0.0", + "version": "1.1.1", "description": "Templating for APIs", "scripts": { "test": "jest", "fix": "eslint --fix \"./src/**/*.ts\" \"./test/**/*.ts\" && prettier \"./src/**/*.ts\" \"./test/**/*.ts\" --write", - "build": "./build.sh" + "build": "./build.sh && cp README.md changelog.md ./dist" }, "author": "Oleg Parashchenko", "license": "MIT", "dependencies": { "ajv": "^8.12.0", "node-fetch": "^2.6.1", - "tplfa-apis": "^1.0.0", - "tplfa-jsonnet": "^1.0.0" + "tplfa-apis": "^1.1.1", + "tplfa-jsonnet": "^1.1.0" }, "devDependencies": { "@types/ajv": "^1.0.0", diff --git a/tplfa-js/dist/templating-for-api.d.ts b/tplfa-js/dist/templating-for-api.d.ts index e6491fc..2b548d2 100644 --- a/tplfa-js/dist/templating-for-api.d.ts +++ b/tplfa-js/dist/templating-for-api.d.ts @@ -8,7 +8,8 @@ export interface ITemplatingForApi { export declare class TemplatingForApi implements ITemplatingForApi { private readonly jsonnet; private readonly validator; - constructor(jsonnet: Jsonnet, validator: TplfaValidator); + private readonly libTemplates; + constructor(jsonnet: Jsonnet, validator: TplfaValidator, libTemplates: Record); toTplfaRequest(codeName: string, requestTemplate: string, reqVars: TplfaReqVars, debugLog?: (...msg: unknown[]) => void): Promise>; static toRequest(req: TplfaRequest): Request; toDocument(codeName: string, apiTemplate: string, docVars: TplfaDocVars, debugLog?: (...msg: unknown[]) => void): Promise>; diff --git a/tplfa-js/dist/templating-for-api.js b/tplfa-js/dist/templating-for-api.js index 5a64b51..6286971 100644 --- a/tplfa-js/dist/templating-for-api.js +++ b/tplfa-js/dist/templating-for-api.js @@ -2,9 +2,10 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplatingForApi = void 0; class TemplatingForApi { - constructor(jsonnet, validator) { + constructor(jsonnet, validator, libTemplates) { this.jsonnet = jsonnet; this.validator = validator; + this.libTemplates = libTemplates; } async toTplfaRequest(codeName, requestTemplate, reqVars, debugLog) { let error; @@ -19,7 +20,7 @@ class TemplatingForApi { } try { error = `${errorPrefix}: Failed to execute`; - const reqStr = await this.jsonnet.evaluate(requestTemplate, reqVars); + const reqStr = await this.jsonnet.evaluate(requestTemplate, reqVars, this.libTemplates); if (debugLog) { debugLog('tplfa toRequest output [name, output]', codeName, reqStr); } @@ -69,7 +70,7 @@ class TemplatingForApi { } try { error = `${errorPrefix}: Failed to execute`; - const transformed = await this.jsonnet.evaluate(apiTemplate, docVars); + const transformed = await this.jsonnet.evaluate(apiTemplate, docVars, this.libTemplates); if (debugLog) { debugLog('tplfa toDocument output [name, output]', codeName, transformed); } diff --git a/tplfa-js/dist/tplfa-types.d.ts b/tplfa-js/dist/tplfa-types.d.ts index cfa38a9..113aa11 100644 --- a/tplfa-js/dist/tplfa-types.d.ts +++ b/tplfa-js/dist/tplfa-types.d.ts @@ -47,3 +47,7 @@ export type LoadedTemplate = { documentTpl: string | undefined; hasDebugFlag?: boolean | undefined; }; +export type LibTemplates = { + 'openai-document-tpl.jsonnet': string; + 'openai-request-tpl.jsonnet': string; +}; diff --git a/tplfa-js/package-lock.json b/tplfa-js/package-lock.json index 74d1ff3..e31dc58 100644 --- a/tplfa-js/package-lock.json +++ b/tplfa-js/package-lock.json @@ -1,12 +1,12 @@ { "name": "tplfa", - "version": "1.0.0", + "version": "1.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "tplfa", - "version": "1.0.0", + "version": "1.1.1", "license": "MIT", "dependencies": { "ajv": "^8.12.0", @@ -32,12 +32,12 @@ }, "../apis/dist": { "name": "tplfa-apis", - "version": "1.0.0", + "version": "1.1.1", "license": "MIT" }, "../jsonnet-js-ts/dist": { "name": "tplfa-jsonnet", - "version": "1.0.1", + "version": "1.1.0", "license": "MIT" }, "node_modules/@ampproject/remapping": { diff --git a/tplfa-js/package.json b/tplfa-js/package.json index 1a53625..1995266 100644 --- a/tplfa-js/package.json +++ b/tplfa-js/package.json @@ -1,11 +1,11 @@ { "name": "tplfa", - "version": "1.0.0", + "version": "1.1.1", "description": "Templating for APIs", "scripts": { "test": "jest", "fix": "eslint --fix \"./src/**/*.ts\" \"./test/**/*.ts\" && prettier \"./src/**/*.ts\" \"./test/**/*.ts\" --write", - "build": "./build.sh" + "build": "./build.sh && cp README.md changelog.md ./dist" }, "author": "Oleg Parashchenko", "license": "MIT", diff --git a/tplfa-js/src/nodejs-loader.ts b/tplfa-js/src/nodejs-loader.ts new file mode 100644 index 0000000..e86fc61 --- /dev/null +++ b/tplfa-js/src/nodejs-loader.ts @@ -0,0 +1,57 @@ +import path from 'path'; +import { promises as fs } from 'fs'; +import { LoadedTemplate, LibTemplates } from './tplfa-types'; + +export function getApisDir(): string { + const rootFile = require.resolve('tplfa-apis/package.json'); + return path.dirname(rootFile); +} + +export async function loadLibTemplates(): Promise { + const apisDir = getApisDir(); + const files: Array = [ + 'openai-document-tpl.jsonnet', + 'openai-request-tpl.jsonnet', + ]; + const loaded = files.map(async (fname) => [ + fname, + await fs.readFile(path.join(apisDir, 'lib', fname), 'utf-8'), + ]); + return Object.fromEntries(await Promise.all(loaded)); +} + +// Like `LoadedTemplate`, but `string` instead of `string|undefined` +export type DefinedLoadedTemplate = { + requestTpl: string; + documentTpl: string; +}; + +export async function loadTemplate( + tplPath: string, + libTemplates: LibTemplates +): Promise { + const toLoad: Array< + [keyof LoadedTemplate, string, keyof LibTemplates] + > = [ + ['requestTpl', 'request-tpl.jsonnet', 'openai-request-tpl.jsonnet'], + [ + 'documentTpl', + 'document-tpl.jsonnet', + 'openai-document-tpl.jsonnet', + ], + ]; + const loaded = toLoad.map(async ([field, fnameBase, libField]) => { + const fname = path.join(tplPath, fnameBase); + try { + const code = await fs.readFile(fname, 'utf-8'); + return [field, code]; + } catch (err) { + const { code } = err as Error & { code: string }; + if (code !== 'ENOENT') { + throw err; + } + return [field, libTemplates[libField]]; + } + }); + return Object.fromEntries(await Promise.all(loaded)); +} diff --git a/tplfa-js/src/templating-for-api.ts b/tplfa-js/src/templating-for-api.ts index 65de775..1c4fe42 100644 --- a/tplfa-js/src/templating-for-api.ts +++ b/tplfa-js/src/templating-for-api.ts @@ -26,7 +26,8 @@ export interface ITemplatingForApi { export class TemplatingForApi implements ITemplatingForApi { constructor( private readonly jsonnet: Jsonnet, - private readonly validator: TplfaValidator + private readonly validator: TplfaValidator, + private readonly libTemplates: Record ) {} async toTplfaRequest( @@ -56,7 +57,8 @@ export class TemplatingForApi implements ITemplatingForApi { error = `${errorPrefix}: Failed to execute`; const reqStr = await this.jsonnet.evaluate( requestTemplate, - reqVars + reqVars, + this.libTemplates ); if (debugLog) { @@ -133,7 +135,8 @@ export class TemplatingForApi implements ITemplatingForApi { error = `${errorPrefix}: Failed to execute`; const transformed = await this.jsonnet.evaluate( apiTemplate, - docVars + docVars, + this.libTemplates ); if (debugLog) { diff --git a/tplfa-js/src/tplfa-types.ts b/tplfa-js/src/tplfa-types.ts index 8e8f2e4..86e7d0e 100644 --- a/tplfa-js/src/tplfa-types.ts +++ b/tplfa-js/src/tplfa-types.ts @@ -60,3 +60,8 @@ export type LoadedTemplate = { documentTpl: string | undefined; hasDebugFlag?: boolean | undefined; }; + +export type LibTemplates = { + 'openai-document-tpl.jsonnet': string; + 'openai-request-tpl.jsonnet': string; +}; diff --git a/tplfa-js/test/templating-for-api.test.ts b/tplfa-js/test/templating-for-api.test.ts index 71c46f1..5b4cc90 100644 --- a/tplfa-js/test/templating-for-api.test.ts +++ b/tplfa-js/test/templating-for-api.test.ts @@ -1,31 +1,44 @@ import fs from 'fs'; +import path from 'path'; import { getJsonnet, Jsonnet } from 'tplfa-jsonnet/jsonnet'; import sampleChatResponse from 'tplfa-apis/openai/fixture/response.json'; import sampleDocument from 'tplfa-apis/openai/fixture/document.json'; +import { TplfaValidator } from '../src/tplfa-validator'; import { TemplatingForApi } from '../src/templating-for-api'; -import { TplfaDocVars, TplfaReqVars } from '../src/tplfa-types'; +import { + TplfaDocVars, + TplfaReqVars, + LibTemplates, +} from '../src/tplfa-types'; +import { + getApisDir, + DefinedLoadedTemplate, + loadLibTemplates, + loadTemplate, +} from '../src/nodejs-loader'; require('tplfa-jsonnet/wasm_exec.js'); describe('templating-for-api', () => { let jsonnet: Jsonnet; let tplfa: TemplatingForApi; - - const openaiReqTemplate = fs.readFileSync( - require.resolve('tplfa-apis/openai/lib/request-tpl.jsonnet'), - 'utf-8' - ); - const openaiDocTemplate = fs.readFileSync( - require.resolve('tplfa-apis/openai/lib/document-tpl.jsonnet'), - 'utf-8' - ); + let template: DefinedLoadedTemplate; + let libTemplates: LibTemplates; beforeAll(async () => { + libTemplates = await loadLibTemplates(); + const openaiPath = path.join(getApisDir(), 'openai', 'lib'); + template = await loadTemplate(openaiPath, libTemplates); + const jnWasm = fs.readFileSync( require.resolve('tplfa-jsonnet/libjsonnet.wasm') ); jsonnet = await getJsonnet(jnWasm); - tplfa = new TemplatingForApi(jsonnet); + tplfa = new TemplatingForApi( + jsonnet, + new TplfaValidator(), + libTemplates + ); }); describe('toRequest', () => { @@ -39,7 +52,7 @@ describe('templating-for-api', () => { it('happy path', async () => { const tplfaRequest = await tplfa.toTplfaRequest( 'test code', - openaiReqTemplate, + template.requestTpl, vars ); @@ -56,7 +69,7 @@ describe('templating-for-api', () => { it('catch error in jsonnet', async () => { const request = await tplfa.toTplfaRequest( 'test code', - openaiReqTemplate, + template.requestTpl, {} as TplfaReqVars ); @@ -97,7 +110,7 @@ describe('templating-for-api', () => { const logMock = jest.fn(); await tplfa.toTplfaRequest( 'test code', - openaiReqTemplate, + template.requestTpl, vars, logMock ); @@ -108,7 +121,7 @@ describe('templating-for-api', () => { 'tplfa toRequest input [name, vars, code]', 'test code', vars, - openaiReqTemplate + template.requestTpl ); expect(logMock).toHaveBeenNthCalledWith( 2, @@ -129,7 +142,7 @@ describe('templating-for-api', () => { it('happy path', async () => { const doc = await tplfa.toDocument( 'test code', - openaiDocTemplate, + template.documentTpl, vars ); @@ -143,7 +156,7 @@ describe('templating-for-api', () => { it('catch error in jsonnet', async () => { const request = await tplfa.toDocument( 'test code', - openaiReqTemplate, + template.requestTpl, {} as TplfaDocVars ); @@ -186,7 +199,7 @@ describe('templating-for-api', () => { const logMock = jest.fn(); await tplfa.toDocument( 'test code', - openaiDocTemplate, + template.documentTpl, vars, logMock ); @@ -197,7 +210,7 @@ describe('templating-for-api', () => { 'tplfa toDocument input [name, vars, code]', 'test code', vars, - openaiDocTemplate + template.documentTpl ); expect(logMock).toHaveBeenNthCalledWith( 2, diff --git a/tplfa-js/tsconfig.json b/tplfa-js/tsconfig.json index e470cac..de309ce 100644 --- a/tplfa-js/tsconfig.json +++ b/tplfa-js/tsconfig.json @@ -11,6 +11,7 @@ "declaration": true }, "include": [ - "src/**/*.ts" + "src/**/*.ts", + "test/**/*.ts" ] }