From 58a7fe5c057f5f2e0b3d6fee61ae13464bee14f8 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Thu, 8 Aug 2024 15:26:48 +0200 Subject: [PATCH 01/19] restructure --- package.json | 2 +- packages/core/README.md | 0 packages/core/jest.config.mjs | 7 ++++ packages/core/package.json | 33 +++++++++++++++++++ .../src/core => core/src}/context.test.ts | 2 +- .../src/core => core/src}/context.ts | 2 +- .../src/core => core/src}/http-client.test.ts | 4 +-- .../src/core => core/src}/http-client.ts | 0 packages/core/src/index.ts | 2 ++ packages/core/tsconfig.json | 11 +++++++ packages/gen-ai-hub/jest.config.mjs | 4 ++- packages/gen-ai-hub/package.json | 1 + packages/gen-ai-hub/src/client/interface.ts | 2 +- .../src/client/openai/openai-client.test.ts | 12 +++---- .../src/client/openai/openai-client.ts | 2 +- .../src/client/openai/openai-types.ts | 2 +- packages/gen-ai-hub/src/core/index.ts | 2 -- packages/gen-ai-hub/src/index.ts | 2 -- .../orchestration-client.test.ts | 8 ++--- .../src/orchestration/orchestration-client.ts | 2 +- .../src/orchestration/orchestration-types.ts | 2 +- ...enai-chat-completion-success-response.json | 0 .../openai-embeddings-success-response.json | 0 .../openai/openai-error-response.json | 0 ...aihub-chat-completion-message-history.json | 0 ...ihub-chat-completion-success-response.json | 0 packages/gen-ai-hub/tsconfig.json | 5 ++- pnpm-lock.yaml | 28 ++++++++++++++-- pnpm-workspace.yaml | 1 + .../test-util => test-util}/mock-context.ts | 3 ++ .../src/test-util => test-util}/mock-http.ts | 9 +++-- tests/e2e-tests/package.json | 3 +- tests/e2e-tests/src/ai-core.test.ts | 2 +- 33 files changed, 119 insertions(+), 34 deletions(-) create mode 100644 packages/core/README.md create mode 100644 packages/core/jest.config.mjs create mode 100644 packages/core/package.json rename packages/{gen-ai-hub/src/core => core/src}/context.test.ts (96%) rename packages/{gen-ai-hub/src/core => core/src}/context.ts (98%) rename packages/{gen-ai-hub/src/core => core/src}/http-client.test.ts (90%) rename packages/{gen-ai-hub/src/core => core/src}/http-client.ts (100%) create mode 100644 packages/core/src/index.ts create mode 100644 packages/core/tsconfig.json delete mode 100644 packages/gen-ai-hub/src/core/index.ts rename packages/gen-ai-hub/{src/test-util/mock-data => test}/openai/openai-chat-completion-success-response.json (100%) rename packages/gen-ai-hub/{src/test-util/mock-data => test}/openai/openai-embeddings-success-response.json (100%) rename packages/gen-ai-hub/{src/test-util/mock-data => test}/openai/openai-error-response.json (100%) rename packages/gen-ai-hub/{src/test-util/mock-data => test}/orchestration/genaihub-chat-completion-message-history.json (100%) rename packages/gen-ai-hub/{src/test-util/mock-data => test}/orchestration/genaihub-chat-completion-success-response.json (100%) rename {packages/gen-ai-hub/src/test-util => test-util}/mock-context.ts (98%) rename {packages/gen-ai-hub/src/test-util => test-util}/mock-http.ts (90%) diff --git a/package.json b/package.json index e56d33f7..0d832eb8 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@changesets/cli": "^2.27.7", "@sap-cloud-sdk/eslint-config": "^3.18.0", "@sap-cloud-sdk/connectivity": "^3.18.0", - "@sap-cloud-sdk/http-client": "^3.18.0", + "@sap-ai-sdk/core": "workspace:^", "@types/jest": "^29.5.12", "@types/node": "^20.14.14", "@jest/globals": "^29.5.12", diff --git a/packages/core/README.md b/packages/core/README.md new file mode 100644 index 00000000..e69de29b diff --git a/packages/core/jest.config.mjs b/packages/core/jest.config.mjs new file mode 100644 index 00000000..0570d2af --- /dev/null +++ b/packages/core/jest.config.mjs @@ -0,0 +1,7 @@ +import config from '../../jest.config.mjs'; +const coreConfig = { + ...config, + displayName: 'core', +}; + +export default coreConfig; diff --git a/packages/core/package.json b/packages/core/package.json new file mode 100644 index 00000000..6f9e378a --- /dev/null +++ b/packages/core/package.json @@ -0,0 +1,33 @@ +{ + "name": "@sap-ai-sdk/core", + "version": "0.0.0", + "description": "", + "license": "Apache-2.0", + "keywords": [ + "sap-ai-sdk", + "ai-core" + ], + "type": "module", + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "files": [ + "dist/**/*.js", + "dist/**/*.js.map", + "dist/**/*.d.ts", + "dist/**/*.d.ts.map" + ], + "scripts": { + "compile": "tsc", + "test": "NODE_OPTIONS=--experimental-vm-modules jest", + "lint": "eslint . && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c", + "lint:fix": "eslint . --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error" + }, + "dependencies": { + "@sap-cloud-sdk/http-client": "^3.18.0", + "@sap-cloud-sdk/connectivity": "^3.18.0", + "@sap-cloud-sdk/util": "^3.18.0" + }, + "devDependencies": { + "typescript": "^5.5.4" + } +} diff --git a/packages/gen-ai-hub/src/core/context.test.ts b/packages/core/src/context.test.ts similarity index 96% rename from packages/gen-ai-hub/src/core/context.test.ts rename to packages/core/src/context.test.ts index 6a5c7c9a..4769c1c9 100644 --- a/packages/gen-ai-hub/src/core/context.test.ts +++ b/packages/core/src/context.test.ts @@ -1,7 +1,7 @@ import { mockAiCoreEnvVariable, mockClientCredentialsGrantCall -} from '../test-util/mock-context.js'; +} from '../../../test-util/mock-context.js'; import { getAiCoreDestination } from './context.js'; describe('context', () => { diff --git a/packages/gen-ai-hub/src/core/context.ts b/packages/core/src/context.ts similarity index 98% rename from packages/gen-ai-hub/src/core/context.ts rename to packages/core/src/context.ts index 42dc3281..7b8ac8e5 100644 --- a/packages/gen-ai-hub/src/core/context.ts +++ b/packages/core/src/context.ts @@ -7,7 +7,7 @@ import { } from '@sap-cloud-sdk/connectivity'; const logger = createLogger({ - package: 'gen-ai-hub', + package: 'core', messageContext: 'context' }); diff --git a/packages/gen-ai-hub/src/core/http-client.test.ts b/packages/core/src/http-client.test.ts similarity index 90% rename from packages/gen-ai-hub/src/core/http-client.test.ts rename to packages/core/src/http-client.test.ts index 5dd5cfb0..8ee15dc2 100644 --- a/packages/gen-ai-hub/src/core/http-client.test.ts +++ b/packages/core/src/http-client.test.ts @@ -1,7 +1,7 @@ import { jest } from '@jest/globals'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; -import { mockGetAiCoreDestination } from '../test-util/mock-context.js'; -import { mockInference } from '../test-util/mock-http.js'; +import { mockGetAiCoreDestination } from '../../../test-util/mock-context.js'; +import { mockInference } from '../../../test-util/mock-http.js'; jest.unstable_mockModule('./context.js', () => ({ getAiCoreDestination: jest.fn(() => diff --git a/packages/gen-ai-hub/src/core/http-client.ts b/packages/core/src/http-client.ts similarity index 100% rename from packages/gen-ai-hub/src/core/http-client.ts rename to packages/core/src/http-client.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts new file mode 100644 index 00000000..f2ddf7eb --- /dev/null +++ b/packages/core/src/index.ts @@ -0,0 +1,2 @@ +export { executeRequest, BaseLlmParametersWithDeploymentId, BaseLlmParameters, CustomRequestConfig } from './http-client.js'; +export { getAiCoreDestination } from './context.js'; \ No newline at end of file diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json new file mode 100644 index 00000000..d7b4b5b2 --- /dev/null +++ b/packages/core/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "./src", + "outDir": "./dist", + "tsBuildInfoFile": "./dist/.tsbuildinfo", + "composite": true + }, + "include": ["src/**/*.ts"], + "exclude": ["dist/**/*", "**/*.test.ts", "node_modules/**/*"] +} diff --git a/packages/gen-ai-hub/jest.config.mjs b/packages/gen-ai-hub/jest.config.mjs index e5b85b14..bfba9699 100644 --- a/packages/gen-ai-hub/jest.config.mjs +++ b/packages/gen-ai-hub/jest.config.mjs @@ -1,5 +1,7 @@ import config from '../../jest.config.mjs'; -export default { +const genAiConfig = { ...config, displayName: 'gen-ai-hub', }; + +export default genAiConfig; diff --git a/packages/gen-ai-hub/package.json b/packages/gen-ai-hub/package.json index 6596f5e6..b760f7db 100644 --- a/packages/gen-ai-hub/package.json +++ b/packages/gen-ai-hub/package.json @@ -26,6 +26,7 @@ "generate": "openapi-generator -i ./src/orchestration/spec/ -o ./src/orchestration/client" }, "dependencies": { + "@sap-ai-sdk/core": "workspace:^", "@sap-cloud-sdk/http-client": "^3.18.0", "@sap-cloud-sdk/connectivity": "^3.18.0", "@sap-cloud-sdk/util": "^3.18.0" diff --git a/packages/gen-ai-hub/src/client/interface.ts b/packages/gen-ai-hub/src/client/interface.ts index 75c30350..d67e82de 100644 --- a/packages/gen-ai-hub/src/client/interface.ts +++ b/packages/gen-ai-hub/src/client/interface.ts @@ -1,4 +1,4 @@ -import { BaseLlmParameters, CustomRequestConfig } from '../core/http-client.js'; +import { BaseLlmParameters, CustomRequestConfig } from '@sap-ai-sdk/core'; import { BaseLlmOutput } from './types.js'; /** diff --git a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts index 3d179dda..06e657df 100644 --- a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts +++ b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts @@ -1,12 +1,12 @@ import nock from 'nock'; import { jest } from '@jest/globals'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; -import { mockGetAiCoreDestination } from '../../test-util/mock-context.js'; +import { mockGetAiCoreDestination } from '../../../../../test-util/mock-context.js'; import { - BaseLlmParametersWithDeploymentId, - EndpointOptions -} from '../../core/http-client.js'; -import { mockInference, parseMockResponse } from '../../test-util/mock-http.js'; + BaseLlmParametersWithDeploymentId +} from '@sap-ai-sdk/core'; +import { EndpointOptions } from '@sap-ai-sdk/core/src/http-client.js'; +import { mockInference, parseMockResponse } from '../../../../../test-util/mock-http.js'; import { OpenAiChatCompletionOutput, OpenAiChatCompletionParameters, @@ -14,7 +14,7 @@ import { OpenAiEmbeddingOutput, OpenAiEmbeddingParameters } from './openai-types.js'; -jest.unstable_mockModule('../../core/context.js', () => ({ +jest.unstable_mockModule('@sap-ai-sdk/core', () => ({ getAiCoreDestination: jest.fn(() => Promise.resolve(mockGetAiCoreDestination()) ) diff --git a/packages/gen-ai-hub/src/client/openai/openai-client.ts b/packages/gen-ai-hub/src/client/openai/openai-client.ts index 8cae3ce7..c1eb9180 100644 --- a/packages/gen-ai-hub/src/client/openai/openai-client.ts +++ b/packages/gen-ai-hub/src/client/openai/openai-client.ts @@ -2,7 +2,7 @@ import { BaseLlmParameters, CustomRequestConfig, executeRequest -} from '../../core/index.js'; +} from '@sap-ai-sdk/core'; import { BaseClient } from '../interface.js'; import { OpenAiChatCompletionParameters, diff --git a/packages/gen-ai-hub/src/client/openai/openai-types.ts b/packages/gen-ai-hub/src/client/openai/openai-types.ts index dbbbcd15..06e04b0c 100644 --- a/packages/gen-ai-hub/src/client/openai/openai-types.ts +++ b/packages/gen-ai-hub/src/client/openai/openai-types.ts @@ -1,4 +1,4 @@ -import { BaseLlmParameters } from '../../core/http-client.js'; +import { BaseLlmParameters } from '@sap-ai-sdk/core'; /** * OpenAI system message. diff --git a/packages/gen-ai-hub/src/core/index.ts b/packages/gen-ai-hub/src/core/index.ts deleted file mode 100644 index daa75097..00000000 --- a/packages/gen-ai-hub/src/core/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './http-client.js'; -export * from './context.js'; diff --git a/packages/gen-ai-hub/src/index.ts b/packages/gen-ai-hub/src/index.ts index 96aed6c6..a66ba39a 100644 --- a/packages/gen-ai-hub/src/index.ts +++ b/packages/gen-ai-hub/src/index.ts @@ -5,7 +5,6 @@ export { OpenAiEmbeddingOutput, OpenAiChatCompletionOutput } from './client/index.js'; -export { CustomRequestConfig, BaseLlmParameters } from './core/index.js'; export { GenAiHubClient, GenAiHubCompletionParameters, @@ -14,4 +13,3 @@ export { LlmConfig, ChatMessages } from './orchestration/index.js'; -export { getAiCoreDestination } from './core/index.js'; diff --git a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts index 17af17f6..dfb95eb7 100644 --- a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts +++ b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts @@ -1,12 +1,12 @@ import nock from 'nock'; import { jest } from '@jest/globals'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; -import { mockGetAiCoreDestination } from '../test-util/mock-context.js'; -import { mockInference, parseMockResponse } from '../test-util/mock-http.js'; -import { BaseLlmParametersWithDeploymentId } from '../core/index.js'; +import { mockGetAiCoreDestination } from '../../../../test-util/mock-context.js'; +import { mockInference, parseMockResponse } from '../../../../test-util/mock-http.js'; +import { BaseLlmParametersWithDeploymentId } from '@sap-ai-sdk/core'; import { CompletionPostResponse } from './api/index.js'; import { GenAiHubCompletionParameters } from './orchestration-types.js'; -jest.unstable_mockModule('../core/context.js', () => ({ +jest.unstable_mockModule('@sap-ai-sdk/core', () => ({ getAiCoreDestination: jest.fn(() => Promise.resolve(mockGetAiCoreDestination()) ) diff --git a/packages/gen-ai-hub/src/orchestration/orchestration-client.ts b/packages/gen-ai-hub/src/orchestration/orchestration-client.ts index e660420f..c540750d 100644 --- a/packages/gen-ai-hub/src/orchestration/orchestration-client.ts +++ b/packages/gen-ai-hub/src/orchestration/orchestration-client.ts @@ -1,4 +1,4 @@ -import { executeRequest, CustomRequestConfig } from '../core/index.js'; +import { executeRequest, CustomRequestConfig } from '@sap-ai-sdk/core'; import { CompletionPostRequest } from './api/schema/index.js'; import { GenAiHubCompletionParameters, diff --git a/packages/gen-ai-hub/src/orchestration/orchestration-types.ts b/packages/gen-ai-hub/src/orchestration/orchestration-types.ts index 2ca7d3e4..c11cc12e 100644 --- a/packages/gen-ai-hub/src/orchestration/orchestration-types.ts +++ b/packages/gen-ai-hub/src/orchestration/orchestration-types.ts @@ -1,4 +1,4 @@ -import { BaseLlmParameters } from '../core/index.js'; +import { BaseLlmParameters } from '@sap-ai-sdk/core'; import { ChatMessages, CompletionPostResponse, diff --git a/packages/gen-ai-hub/src/test-util/mock-data/openai/openai-chat-completion-success-response.json b/packages/gen-ai-hub/test/openai/openai-chat-completion-success-response.json similarity index 100% rename from packages/gen-ai-hub/src/test-util/mock-data/openai/openai-chat-completion-success-response.json rename to packages/gen-ai-hub/test/openai/openai-chat-completion-success-response.json diff --git a/packages/gen-ai-hub/src/test-util/mock-data/openai/openai-embeddings-success-response.json b/packages/gen-ai-hub/test/openai/openai-embeddings-success-response.json similarity index 100% rename from packages/gen-ai-hub/src/test-util/mock-data/openai/openai-embeddings-success-response.json rename to packages/gen-ai-hub/test/openai/openai-embeddings-success-response.json diff --git a/packages/gen-ai-hub/src/test-util/mock-data/openai/openai-error-response.json b/packages/gen-ai-hub/test/openai/openai-error-response.json similarity index 100% rename from packages/gen-ai-hub/src/test-util/mock-data/openai/openai-error-response.json rename to packages/gen-ai-hub/test/openai/openai-error-response.json diff --git a/packages/gen-ai-hub/src/test-util/mock-data/orchestration/genaihub-chat-completion-message-history.json b/packages/gen-ai-hub/test/orchestration/genaihub-chat-completion-message-history.json similarity index 100% rename from packages/gen-ai-hub/src/test-util/mock-data/orchestration/genaihub-chat-completion-message-history.json rename to packages/gen-ai-hub/test/orchestration/genaihub-chat-completion-message-history.json diff --git a/packages/gen-ai-hub/src/test-util/mock-data/orchestration/genaihub-chat-completion-success-response.json b/packages/gen-ai-hub/test/orchestration/genaihub-chat-completion-success-response.json similarity index 100% rename from packages/gen-ai-hub/src/test-util/mock-data/orchestration/genaihub-chat-completion-success-response.json rename to packages/gen-ai-hub/test/orchestration/genaihub-chat-completion-success-response.json diff --git a/packages/gen-ai-hub/tsconfig.json b/packages/gen-ai-hub/tsconfig.json index d7b4b5b2..ba13e06d 100644 --- a/packages/gen-ai-hub/tsconfig.json +++ b/packages/gen-ai-hub/tsconfig.json @@ -7,5 +7,8 @@ "composite": true }, "include": ["src/**/*.ts"], - "exclude": ["dist/**/*", "**/*.test.ts", "node_modules/**/*"] + "exclude": ["dist/**/*", "test/**/*", "**/*.test.ts", "node_modules/**/*"], + "references": [ + { "path": "../core" } + ] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 816ed8d8..d7cacc3d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,15 +14,15 @@ importers: '@jest/globals': specifier: ^29.5.12 version: 29.7.0 + '@sap-ai-sdk/core': + specifier: workspace:^ + version: link:packages/core '@sap-cloud-sdk/connectivity': specifier: ^3.18.0 version: 3.18.0 '@sap-cloud-sdk/eslint-config': specifier: ^3.18.0 version: 3.18.0(@types/eslint@8.56.10)(@typescript-eslint/parser@7.18.0(eslint@9.8.0)(typescript@5.5.4))(eslint@9.8.0)(prettier@3.3.3)(typescript@5.5.4) - '@sap-cloud-sdk/http-client': - specifier: ^3.18.0 - version: 3.18.0 '@types/jest': specifier: ^29.5.12 version: 29.5.12 @@ -64,8 +64,27 @@ importers: specifier: ^5.5.4 version: 5.5.4 + packages/core: + dependencies: + '@sap-cloud-sdk/connectivity': + specifier: ^3.18.0 + version: 3.18.0 + '@sap-cloud-sdk/http-client': + specifier: ^3.18.0 + version: 3.18.0 + '@sap-cloud-sdk/util': + specifier: ^3.18.0 + version: 3.18.0 + devDependencies: + typescript: + specifier: ^5.5.4 + version: 5.5.4 + packages/gen-ai-hub: dependencies: + '@sap-ai-sdk/core': + specifier: workspace:^ + version: link:../core '@sap-cloud-sdk/connectivity': specifier: ^3.18.0 version: 3.18.0 @@ -103,6 +122,9 @@ importers: '@sap-ai-sdk/ai-core': specifier: workspace:^ version: link:../../packages/ai-core + '@sap-ai-sdk/core': + specifier: workspace:^ + version: link:../../packages/core '@sap-ai-sdk/gen-ai-hub': specifier: workspace:^ version: link:../../packages/gen-ai-hub diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 4787eced..b8b5aa24 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -2,6 +2,7 @@ packages: # all packages in direct subdirs of packages/ - 'packages/ai-core' - 'packages/gen-ai-hub' + - 'packages/core' # sample code - 'sample-code' # e2e tests diff --git a/packages/gen-ai-hub/src/test-util/mock-context.ts b/test-util/mock-context.ts similarity index 98% rename from packages/gen-ai-hub/src/test-util/mock-context.ts rename to test-util/mock-context.ts index 6148aebe..994abd24 100644 --- a/packages/gen-ai-hub/src/test-util/mock-context.ts +++ b/test-util/mock-context.ts @@ -45,6 +45,9 @@ export function mockAiCoreEnvVariable(): void { process.env['aicore'] = JSON.stringify(aiCoreServiceBinding.credentials); } +/** + * @internal + */ export function mockGetAiCoreDestination( destination = aiCoreDestination ): HttpDestination { diff --git a/packages/gen-ai-hub/src/test-util/mock-http.ts b/test-util/mock-http.ts similarity index 90% rename from packages/gen-ai-hub/src/test-util/mock-http.ts rename to test-util/mock-http.ts index 847f9618..c11d10bb 100644 --- a/packages/gen-ai-hub/src/test-util/mock-http.ts +++ b/test-util/mock-http.ts @@ -4,9 +4,9 @@ import { HttpDestination } from '@sap-cloud-sdk/connectivity'; import nock from 'nock'; import { BaseLlmParameters, - CustomRequestConfig, - EndpointOptions -} from '../core/http-client.js'; + CustomRequestConfig +} from '@sap-ai-sdk/core'; +import { EndpointOptions } from '@sap-ai-sdk/core/src/http-client.js'; const mockEndpoint: EndpointOptions = { url: 'mock-endpoint', @@ -42,6 +42,9 @@ export function mockInference( .reply(response.status, response.data); } +/** + * @internal + */ export function parseMockResponse(client: string, fileName: string): T { const fileContent = fs.readFileSync( path.join('src', 'test-util', 'mock-data', client, fileName), diff --git a/tests/e2e-tests/package.json b/tests/e2e-tests/package.json index 0b6ca9c4..c10279fc 100644 --- a/tests/e2e-tests/package.json +++ b/tests/e2e-tests/package.json @@ -14,7 +14,8 @@ "dependencies": { "@sap-ai-sdk/sample-code": "workspace:^", "@sap-ai-sdk/gen-ai-hub": "workspace:^", - "@sap-ai-sdk/ai-core": "workspace:^" + "@sap-ai-sdk/ai-core": "workspace:^", + "@sap-ai-sdk/core": "workspace:^" }, "scripts": { "compile": "tsc", diff --git a/tests/e2e-tests/src/ai-core.test.ts b/tests/e2e-tests/src/ai-core.test.ts index 640ee719..7d2b3107 100644 --- a/tests/e2e-tests/src/ai-core.test.ts +++ b/tests/e2e-tests/src/ai-core.test.ts @@ -1,5 +1,5 @@ import { DeploymentApi } from '@sap-ai-sdk/ai-core'; -import { getAiCoreDestination } from '@sap-ai-sdk/gen-ai-hub'; +import { getAiCoreDestination } from '@sap-ai-sdk/core'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; import 'dotenv/config'; From 52d42a13c351c6d79daecd47e655bd769f8e6ed1 Mon Sep 17 00:00:00 2001 From: Tom Frenken Date: Fri, 9 Aug 2024 11:47:41 +0200 Subject: [PATCH 02/19] lint --- packages/core/src/index.ts | 9 +++++++-- .../gen-ai-hub/src/client/openai/openai-client.test.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f2ddf7eb..99fb69b0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,2 +1,7 @@ -export { executeRequest, BaseLlmParametersWithDeploymentId, BaseLlmParameters, CustomRequestConfig } from './http-client.js'; -export { getAiCoreDestination } from './context.js'; \ No newline at end of file +export { + executeRequest, + BaseLlmParametersWithDeploymentId, + BaseLlmParameters, + CustomRequestConfig +} from './http-client.js'; +export { getAiCoreDestination } from './context.js'; diff --git a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts index 06e657df..7272f1bc 100644 --- a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts +++ b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts @@ -1,11 +1,11 @@ import nock from 'nock'; import { jest } from '@jest/globals'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; -import { mockGetAiCoreDestination } from '../../../../../test-util/mock-context.js'; import { BaseLlmParametersWithDeploymentId } from '@sap-ai-sdk/core'; import { EndpointOptions } from '@sap-ai-sdk/core/src/http-client.js'; +import { mockGetAiCoreDestination } from '../../../../../test-util/mock-context.js'; import { mockInference, parseMockResponse } from '../../../../../test-util/mock-http.js'; import { OpenAiChatCompletionOutput, From dd8f4800916bd11ad122acbd3cec5514ee1ed45a Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Fri, 9 Aug 2024 12:11:27 +0200 Subject: [PATCH 03/19] test restructure+workflows --- .github/workflows/build.yml | 2 +- .github/workflows/e2e-test.yaml | 4 +--- package.json | 16 +++++++-------- packages/gen-ai-hub/src/index.ts | 3 ++- pnpm-lock.yaml | 18 ++++++++++++++--- pnpm-workspace.yaml | 4 +++- tests/e2e-tests/package.json | 2 +- tests/type-tests/package.json | 20 +++++++++++++++++++ tests/type-tests/{ => test}/context.test-d.ts | 2 +- .../{ => test}/http-client.test-d.ts | 2 +- tests/type-tests/{ => test}/openai.test-d.ts | 4 ++-- .../{ => test}/orchestration.test-d.ts | 3 +-- 12 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 tests/type-tests/package.json rename tests/type-tests/{ => test}/context.test-d.ts (63%) rename tests/type-tests/{ => test}/http-client.test-d.ts (88%) rename tests/type-tests/{ => test}/openai.test-d.ts (82%) rename tests/type-tests/{ => test}/orchestration.test-d.ts (92%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6c666762..9d07827f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,7 +25,7 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'pnpm' - run: pnpm i --frozen-lockfile - - run: pnpm test + - run: pnpm test:unit - run: pnpm test:type checks: diff --git a/.github/workflows/e2e-test.yaml b/.github/workflows/e2e-test.yaml index ca59053c..26555bfe 100644 --- a/.github/workflows/e2e-test.yaml +++ b/.github/workflows/e2e-test.yaml @@ -51,9 +51,7 @@ jobs: done wget -qO- -S --content-on-error localhost:8080 - name: "Execute E2E Tests" - working-directory: ./tests/e2e-tests - run: | - pnpm run e2e-test + run: pnpm test:e2e - name: "Slack Notification" if: failure() uses: slackapi/slack-github-action@v1.26.0 diff --git a/package.json b/package.json index 1d5ac3ca..e52c6280 100644 --- a/package.json +++ b/package.json @@ -7,23 +7,24 @@ "repository": "github:SAP/ai-sdk-js", "private": true, "type": "module", - "types": "tests/type-tests", - "tsd": { - "directory": "tests/type-tests" - }, "scripts": { "postinstall": "pnpm compile", "compile": "pnpm -r -w=false run compile", - "test": "NODE_OPTIONS=--experimental-vm-modules pnpm -r run test", - "test:type": "tsd", + "test:unit": "NODE_OPTIONS=--experimental-vm-modules pnpm -r -F=./packages/** test", + "test:type": "pnpm -F=@sap-ai-sdk/type-tests test", + "test:e2e": "pnpm -F=@sap-ai-sdk/e2e-tests test", "lint": "pnpm -r run lint", "lint:fix": "pnpm -r run lint:fix", - "generate": "pnpm -r run generate" + "generate": "pnpm -r run generate", + "ai-core": "pnpm -F=@sap-ai-sdk/ai-core", + "gen-ai-hub": "pnpm -F=@sap-ai-sdk/gen-ai-hub", + "core": "pnpm -F=@sap-ai-sdk/core" }, "devDependencies": { "@changesets/cli": "^2.27.7", "@sap-cloud-sdk/eslint-config": "^3.18.0", "@sap-cloud-sdk/connectivity": "^3.18.0", + "@sap-cloud-sdk/http-client": "^3.18.0", "@sap-ai-sdk/core": "workspace:^", "@types/jest": "^29.5.12", "@types/node": "^20.14.14", @@ -34,7 +35,6 @@ "prettier": "^3.3.3", "ts-jest": "^29.2.4", "ts-node": "^10.9.2", - "tsd": "^0.31.0", "typescript": "^5.5.4" } } diff --git a/packages/gen-ai-hub/src/index.ts b/packages/gen-ai-hub/src/index.ts index a66ba39a..73e14135 100644 --- a/packages/gen-ai-hub/src/index.ts +++ b/packages/gen-ai-hub/src/index.ts @@ -11,5 +11,6 @@ export { GenAiHubCompletionResponse, PromptConfig, LlmConfig, - ChatMessages + ChatMessages, + CompletionPostResponse } from './orchestration/index.js'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 804eeea5..3d77bd09 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: '@sap-cloud-sdk/eslint-config': specifier: ^3.18.0 version: 3.18.0(@types/eslint@8.56.10)(@typescript-eslint/parser@7.18.0(eslint@9.8.0)(typescript@5.5.4))(eslint@9.8.0)(prettier@3.3.3)(typescript@5.5.4) + '@sap-cloud-sdk/http-client': + specifier: ^3.18.0 + version: 3.18.0 '@types/jest': specifier: ^29.5.12 version: 29.5.12 @@ -47,9 +50,6 @@ importers: ts-node: specifier: ^10.9.2 version: 10.9.2(@types/node@20.14.14)(typescript@5.5.4) - tsd: - specifier: ^0.31.0 - version: 0.31.1 typescript: specifier: ^5.5.4 version: 5.5.4 @@ -142,6 +142,18 @@ importers: specifier: ^16.4.5 version: 16.4.5 + tests/type-tests: + devDependencies: + '@sap-ai-sdk/core': + specifier: workspace:^ + version: link:../../packages/core + '@sap-ai-sdk/gen-ai-hub': + specifier: workspace:^ + version: link:../../packages/gen-ai-hub + tsd: + specifier: ^0.31.0 + version: 0.31.1 + packages: '@ampproject/remapping@2.3.0': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index b8b5aa24..7d9348b8 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -7,5 +7,7 @@ packages: - 'sample-code' # e2e tests - 'tests/e2e-tests' + # type tests + - 'tests/type-tests' # exclude packages that are inside test directories - - '!tests/type-tests' + diff --git a/tests/e2e-tests/package.json b/tests/e2e-tests/package.json index c10279fc..743c79c3 100644 --- a/tests/e2e-tests/package.json +++ b/tests/e2e-tests/package.json @@ -19,7 +19,7 @@ }, "scripts": { "compile": "tsc", - "e2e-test": "NODE_OPTIONS=--experimental-vm-modules jest", + "test": "NODE_OPTIONS=--experimental-vm-modules jest", "lint": "prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c", "lint:fix": "prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error" }, diff --git a/tests/type-tests/package.json b/tests/type-tests/package.json new file mode 100644 index 00000000..666860d0 --- /dev/null +++ b/tests/type-tests/package.json @@ -0,0 +1,20 @@ +{ + "name": "@sap-ai-sdk/type-tests", + "version": "0.0.0", + "description": "Tests to ensure correct types in the SAP AI SDK for JavaScript.", + "type": "module", + "private": true, + "types": "test", + "scripts": { + "test": "pnpm tsd", + "lint:fix": "prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error" + }, + "tsd": { + "directory": "test" + }, + "devDependencies": { + "@sap-ai-sdk/gen-ai-hub": "workspace:^", + "@sap-ai-sdk/core": "workspace:^", + "tsd": "^0.31.0" + } +} \ No newline at end of file diff --git a/tests/type-tests/context.test-d.ts b/tests/type-tests/test/context.test-d.ts similarity index 63% rename from tests/type-tests/context.test-d.ts rename to tests/type-tests/test/context.test-d.ts index 98029060..aa967764 100644 --- a/tests/type-tests/context.test-d.ts +++ b/tests/type-tests/test/context.test-d.ts @@ -1,5 +1,5 @@ import { Destination } from '@sap-cloud-sdk/connectivity'; import { expectType } from 'tsd'; -import { getAiCoreDestination } from '../../packages/gen-ai-hub/src/core/context.js'; +import { getAiCoreDestination } from '@sap-ai-sdk/core'; expectType>(getAiCoreDestination()); diff --git a/tests/type-tests/http-client.test-d.ts b/tests/type-tests/test/http-client.test-d.ts similarity index 88% rename from tests/type-tests/http-client.test-d.ts rename to tests/type-tests/test/http-client.test-d.ts index 8b85cdc0..918a9f65 100644 --- a/tests/type-tests/http-client.test-d.ts +++ b/tests/type-tests/test/http-client.test-d.ts @@ -1,6 +1,6 @@ import { HttpResponse } from '@sap-cloud-sdk/http-client'; import { expectError, expectType } from 'tsd'; -import { executeRequest } from '../../packages/gen-ai-hub/src/core/http-client.js'; +import { executeRequest } from '@sap-ai-sdk/core'; expectType>( executeRequest( diff --git a/tests/type-tests/openai.test-d.ts b/tests/type-tests/test/openai.test-d.ts similarity index 82% rename from tests/type-tests/openai.test-d.ts rename to tests/type-tests/test/openai.test-d.ts index 53050c22..592e9451 100644 --- a/tests/type-tests/openai.test-d.ts +++ b/tests/type-tests/test/openai.test-d.ts @@ -1,9 +1,9 @@ import { expectError, expectType } from 'tsd'; -import { OpenAiClient } from '../../packages/gen-ai-hub/src/client/openai/openai-client.js'; import { + OpenAiClient, OpenAiChatCompletionOutput, OpenAiEmbeddingOutput -} from '../../packages/gen-ai-hub/src/client/openai/openai-types.js' +} from '@sap-ai-sdk/gen-ai-hub' const client = new OpenAiClient(); expectType(client ); diff --git a/tests/type-tests/orchestration.test-d.ts b/tests/type-tests/test/orchestration.test-d.ts similarity index 92% rename from tests/type-tests/orchestration.test-d.ts rename to tests/type-tests/test/orchestration.test-d.ts index a2eab494..82cb0dd1 100644 --- a/tests/type-tests/orchestration.test-d.ts +++ b/tests/type-tests/test/orchestration.test-d.ts @@ -1,6 +1,5 @@ import { expectError, expectType } from 'tsd'; -import { GenAiHubClient } from '../../packages/gen-ai-hub/src/orchestration/orchestration-client.js'; -import { CompletionPostResponse } from '../../packages/gen-ai-hub/src/orchestration/index.js'; +import { GenAiHubClient, CompletionPostResponse } from '@sap-ai-sdk/gen-ai-hub'; const client = new GenAiHubClient(); expectType(client); From aa7c72fa5d6f730cbe478ef6736e878d2492f212 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Fri, 9 Aug 2024 16:36:26 +0200 Subject: [PATCH 04/19] lint --- eslint.config.js | 5 +++ .../src/client/openai/openai-client.test.ts | 9 +++-- .../orchestration-client.test.ts | 5 ++- packages/gen-ai-hub/tsconfig.json | 4 +- pnpm-lock.yaml | 10 +---- sample-code/src/aiservice.ts | 1 - tests/type-tests/package.json | 38 +++++++++---------- tests/type-tests/test/openai.test-d.ts | 4 +- 8 files changed, 37 insertions(+), 39 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index ccd444fd..a5371107 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -2,6 +2,11 @@ import flatConfig from '@sap-cloud-sdk/eslint-config/flat-config.js'; export default [ ...flatConfig, + { + // Estlint flat config is not supported by eslint-plugin-import. + // https://github.com/import-js/eslint-plugin-import/issues/2556 + rules: { 'import/namespace': 'off'} + }, { ignores: ['**/dist/**/*', '**/coverage/**/*', 'packages/ai-core/src/client/**/*'], }, diff --git a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts index 7272f1bc..bbd4398c 100644 --- a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts +++ b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts @@ -1,12 +1,13 @@ import nock from 'nock'; import { jest } from '@jest/globals'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; -import { - BaseLlmParametersWithDeploymentId -} from '@sap-ai-sdk/core'; +import { BaseLlmParametersWithDeploymentId } from '@sap-ai-sdk/core'; import { EndpointOptions } from '@sap-ai-sdk/core/src/http-client.js'; import { mockGetAiCoreDestination } from '../../../../../test-util/mock-context.js'; -import { mockInference, parseMockResponse } from '../../../../../test-util/mock-http.js'; +import { + mockInference, + parseMockResponse +} from '../../../../../test-util/mock-http.js'; import { OpenAiChatCompletionOutput, OpenAiChatCompletionParameters, diff --git a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts index b27233c7..4e402184 100644 --- a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts +++ b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts @@ -3,7 +3,10 @@ import { jest } from '@jest/globals'; import { HttpDestination } from '@sap-cloud-sdk/connectivity'; import { BaseLlmParametersWithDeploymentId } from '@sap-ai-sdk/core'; import { mockGetAiCoreDestination } from '../../../../test-util/mock-context.js'; -import { mockInference, parseMockResponse } from '../../../../test-util/mock-http.js'; +import { + mockInference, + parseMockResponse +} from '../../../../test-util/mock-http.js'; import { CompletionPostResponse } from './client/api/index.js'; import { GenAiHubCompletionParameters } from './orchestration-types.js'; jest.unstable_mockModule('@sap-ai-sdk/core', () => ({ diff --git a/packages/gen-ai-hub/tsconfig.json b/packages/gen-ai-hub/tsconfig.json index ba13e06d..5c6b23ab 100644 --- a/packages/gen-ai-hub/tsconfig.json +++ b/packages/gen-ai-hub/tsconfig.json @@ -8,7 +8,5 @@ }, "include": ["src/**/*.ts"], "exclude": ["dist/**/*", "test/**/*", "**/*.test.ts", "node_modules/**/*"], - "references": [ - { "path": "../core" } - ] + "references": [{ "path": "../core" }] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d77bd09..fff2b013 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1526,10 +1526,6 @@ packages: engines: {node: '>=4'} hasBin: true - esquery@1.5.0: - resolution: {integrity: sha512-YQLXUplAwJgCydQ78IMJywZCceoqk1oH01OERdSAJc/7U2AylwjhSCLDEtqwg811idIS/9fIU5GjG73IgjKMVg==} - engines: {node: '>=0.10'} - esquery@1.6.0: resolution: {integrity: sha512-ca9pw9fomFcKPvFLXhBKUK90ZvGibiGOvRJNbjljY7s7uq/5YO4BOzcYtJqExdx99rF6aAcnRxHmcUHcz6sQsg==} engines: {node: '>=0.10'} @@ -5220,7 +5216,7 @@ snapshots: eslint-scope: 8.0.2 eslint-visitor-keys: 4.0.0 espree: 10.1.0 - esquery: 1.5.0 + esquery: 1.6.0 esutils: 2.0.3 fast-deep-equal: 3.1.3 file-entry-cache: 8.0.0 @@ -5249,10 +5245,6 @@ snapshots: esprima@4.0.1: {} - esquery@1.5.0: - dependencies: - estraverse: 5.3.0 - esquery@1.6.0: dependencies: estraverse: 5.3.0 diff --git a/sample-code/src/aiservice.ts b/sample-code/src/aiservice.ts index 7b79492a..36f0543f 100644 --- a/sample-code/src/aiservice.ts +++ b/sample-code/src/aiservice.ts @@ -1,4 +1,3 @@ -// eslint-disable-next-line import/namespace import { OpenAiClient } from '@sap-ai-sdk/gen-ai-hub'; const openAiClient = new OpenAiClient(); diff --git a/tests/type-tests/package.json b/tests/type-tests/package.json index 666860d0..a2fa84d7 100644 --- a/tests/type-tests/package.json +++ b/tests/type-tests/package.json @@ -1,20 +1,20 @@ { - "name": "@sap-ai-sdk/type-tests", - "version": "0.0.0", - "description": "Tests to ensure correct types in the SAP AI SDK for JavaScript.", - "type": "module", - "private": true, - "types": "test", - "scripts": { - "test": "pnpm tsd", - "lint:fix": "prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error" - }, - "tsd": { - "directory": "test" - }, - "devDependencies": { - "@sap-ai-sdk/gen-ai-hub": "workspace:^", - "@sap-ai-sdk/core": "workspace:^", - "tsd": "^0.31.0" - } -} \ No newline at end of file + "name": "@sap-ai-sdk/type-tests", + "version": "0.0.0", + "description": "Tests to ensure correct types in the SAP AI SDK for JavaScript.", + "type": "module", + "private": true, + "types": "test", + "scripts": { + "test": "pnpm tsd", + "lint:fix": "prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error" + }, + "tsd": { + "directory": "test" + }, + "devDependencies": { + "@sap-ai-sdk/gen-ai-hub": "workspace:^", + "@sap-ai-sdk/core": "workspace:^", + "tsd": "^0.31.0" + } +} diff --git a/tests/type-tests/test/openai.test-d.ts b/tests/type-tests/test/openai.test-d.ts index 592e9451..443662e6 100644 --- a/tests/type-tests/test/openai.test-d.ts +++ b/tests/type-tests/test/openai.test-d.ts @@ -3,10 +3,10 @@ import { OpenAiClient, OpenAiChatCompletionOutput, OpenAiEmbeddingOutput -} from '@sap-ai-sdk/gen-ai-hub' +} from '@sap-ai-sdk/gen-ai-hub'; const client = new OpenAiClient(); -expectType(client ); +expectType(client); /** * Chat Completion. From 93f948c056c23e4ff4ae8a135270483714f98754 Mon Sep 17 00:00:00 2001 From: Tom Frenken Date: Mon, 12 Aug 2024 11:10:24 +0200 Subject: [PATCH 05/19] add temporary workaround --- log.txt | 141 +++++++++++++++++++++++++ packages/ai-core/jest.config.mjs | 9 ++ packages/ai-core/tsconfig.test.json | 9 ++ packages/core/jest.config.mjs | 9 ++ packages/core/tsconfig.test.json | 9 ++ packages/gen-ai-hub/jest.config.mjs | 9 ++ packages/gen-ai-hub/tsconfig.test.json | 9 ++ 7 files changed, 195 insertions(+) create mode 100644 log.txt create mode 100644 packages/ai-core/tsconfig.test.json create mode 100644 packages/core/tsconfig.test.json create mode 100644 packages/gen-ai-hub/tsconfig.test.json diff --git a/log.txt b/log.txt new file mode 100644 index 00000000..165959e3 --- /dev/null +++ b/log.txt @@ -0,0 +1,141 @@ + +> sap-ai-sdk@0.0.0 test:unit /Users/I519840/Code/ai-core/ai-sdk-js +> NODE_OPTIONS=--experimental-vm-modules pnpm -r -F=./packages/** test + +Scope: 3 of 7 workspace projects +packages/ai-core test$ NODE_OPTIONS=--experimental-vm-modules jest +packages/core test$ NODE_OPTIONS=--experimental-vm-modules jest +packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/core test: (node:78069) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/ai-core test: (node:78070) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/ai-core test: (node:78071) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/ai-core test: (node:78068) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/ai-core test: (node:78067) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/ai-core test: (node:78065) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/core test: (node:78066) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/core test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/core test: PASS core src/http-client.test.ts +packages/ai-core test: PASS ai-core src/client/AI_CORE_API/scenario-api.test.ts +packages/core test: PASS core src/context.test.ts +packages/core test: ● Console +packages/core test: console.log +packages/core test: [2024-08-12T09:05:11.266Z] WARN (environment-accessor): Could not find service binding of type 'aicore'. This might cause errors in other parts of the application. +packages/core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/core test: console.log +packages/core test: [2024-08-12T09:05:11.282Z] INFO (context): Found a service key in environment variable "aicore". Using a service key is recommended for local testing only. Bind the AI Core service to the application for productive usage. +packages/core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/core test: console.log +packages/core test: [2024-08-12T09:05:11.282Z] WARN (client-credentials-token-cache): Cannot create cache key for client credentials token cache. The given tenant ID is undefined. +packages/core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/core test: Test Suites: 2 passed, 2 total +packages/core test: Tests: 4 passed, 4 total +packages/core test: Snapshots: 1 passed, 1 total +packages/core test: Time: 3.683 s +packages/core test: Ran all test suites. +packages/ai-core test: PASS ai-core src/client/AI_CORE_API/artifact-api.test.ts +packages/ai-core test: ● Console +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.335Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/artifacts/. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.349Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/artifacts. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: PASS ai-core src/client/AI_CORE_API/configuration-api.test.ts +packages/ai-core test: ● Console +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.335Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/configurations/. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.345Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/configurations. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: PASS ai-core src/client/AI_CORE_API/execution-api.test.ts +packages/ai-core test: ● Console +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.347Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/executions/. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.359Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/executions. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/core test: Done +packages/ai-core test: PASS ai-core src/client/AI_CORE_API/deployment-api.test.ts +packages/ai-core test: ● Console +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.350Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.361Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.367Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h/. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.369Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.375Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h/. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: console.log +packages/ai-core test: [2024-08-12T09:05:11.376Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h. +packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) +packages/ai-core test: Test Suites: 5 passed, 5 total +packages/ai-core test: Tests: 11 passed, 11 total +packages/ai-core test: Snapshots: 0 total +packages/ai-core test: Time: 3.791 s +packages/ai-core test: Ran all test suites. +packages/ai-core test: Done +packages/gen-ai-hub test$ NODE_OPTIONS=--experimental-vm-modules jest +packages/gen-ai-hub test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/gen-ai-hub test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/gen-ai-hub test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. +packages/gen-ai-hub test: FAIL gen-ai-hub src/orchestration/orchestration-client.test.ts +packages/gen-ai-hub test: ● Test suite failed to run +packages/gen-ai-hub test: SyntaxError: The requested module '@sap-ai-sdk/core' does not provide an export named 'executeRequest' +packages/gen-ai-hub test: 16 | })); +packages/gen-ai-hub test: 17 | +packages/gen-ai-hub test: > 18 | const { GenAiHubClient, constructCompletionPostRequest } = await import( +packages/gen-ai-hub test: | ^ +packages/gen-ai-hub test: 19 | './orchestration-client.js' +packages/gen-ai-hub test: 20 | ); +packages/gen-ai-hub test: 21 | describe('GenAiHubClient', () => { +packages/gen-ai-hub test: at Runtime.linkAndEvaluateModule (../../node_modules/.pnpm/jest-runtime@30.0.0-alpha.6/node_modules/jest-runtime/build/index.js:685:5) +packages/gen-ai-hub test: at src/orchestration/orchestration-client.test.ts:18:60 +packages/gen-ai-hub test: (node:78164) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/gen-ai-hub test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/gen-ai-hub test: FAIL gen-ai-hub src/client/openai/openai-client.test.ts +packages/gen-ai-hub test: ● Test suite failed to run +packages/gen-ai-hub test: SyntaxError: The requested module '@sap-ai-sdk/core' does not provide an export named 'executeRequest' +packages/gen-ai-hub test: 21 | ) +packages/gen-ai-hub test: 22 | })); +packages/gen-ai-hub test: > 23 | const { OpenAiClient } = await import('./openai-client.js'); +packages/gen-ai-hub test: | ^ +packages/gen-ai-hub test: 24 | +packages/gen-ai-hub test: 25 | describe('openai client', () => { +packages/gen-ai-hub test: 26 | let destination: HttpDestination; +packages/gen-ai-hub test: at Runtime.linkAndEvaluateModule (../../node_modules/.pnpm/jest-runtime@30.0.0-alpha.6/node_modules/jest-runtime/build/index.js:685:5) +packages/gen-ai-hub test: at src/client/openai/openai-client.test.ts:23:26 +packages/gen-ai-hub test: (node:78163) ExperimentalWarning: VM Modules is an experimental feature and might change at any time +packages/gen-ai-hub test: (Use `node --trace-warnings ...` to show where the warning was created) +packages/gen-ai-hub test: Test Suites: 2 failed, 2 total +packages/gen-ai-hub test: Tests: 0 total +packages/gen-ai-hub test: Snapshots: 0 total +packages/gen-ai-hub test: Time: 2.143 s +packages/gen-ai-hub test: Ran all test suites. +packages/gen-ai-hub test: Failed +/Users/I519840/Code/ai-core/ai-sdk-js/packages/gen-ai-hub: + ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  @sap-ai-sdk/gen-ai-hub@0.0.0 test: `NODE_OPTIONS=--experimental-vm-modules jest` +Exit status 1 + ELIFECYCLE  Command failed with exit code 1. diff --git a/packages/ai-core/jest.config.mjs b/packages/ai-core/jest.config.mjs index 3d5aa2fa..1b314717 100644 --- a/packages/ai-core/jest.config.mjs +++ b/packages/ai-core/jest.config.mjs @@ -2,6 +2,15 @@ import config from '../../jest.config.mjs'; const aiCoreConfig = { ...config, displayName: 'ai-core', + transform: { + '^.+\\.ts$': [ + 'ts-jest', + { + tsconfig: 'tsconfig.test.json', + useESM: true + } + ] + }, }; export default aiCoreConfig; diff --git a/packages/ai-core/tsconfig.test.json b/packages/ai-core/tsconfig.test.json new file mode 100644 index 00000000..e2272c3e --- /dev/null +++ b/packages/ai-core/tsconfig.test.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "../..", + "noEmit": true + }, + "include": ["src/**/*.ts", "**/*.test.ts", "../../test-util/**/*.ts"], + "exclude": ["dist/**/*", "node_modules/**/*"] +} \ No newline at end of file diff --git a/packages/core/jest.config.mjs b/packages/core/jest.config.mjs index 0570d2af..a278348d 100644 --- a/packages/core/jest.config.mjs +++ b/packages/core/jest.config.mjs @@ -2,6 +2,15 @@ import config from '../../jest.config.mjs'; const coreConfig = { ...config, displayName: 'core', + transform: { + '^.+\\.ts$': [ + 'ts-jest', + { + tsconfig: 'tsconfig.test.json', + useESM: true + } + ] + }, }; export default coreConfig; diff --git a/packages/core/tsconfig.test.json b/packages/core/tsconfig.test.json new file mode 100644 index 00000000..e2272c3e --- /dev/null +++ b/packages/core/tsconfig.test.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "../..", + "noEmit": true + }, + "include": ["src/**/*.ts", "**/*.test.ts", "../../test-util/**/*.ts"], + "exclude": ["dist/**/*", "node_modules/**/*"] +} \ No newline at end of file diff --git a/packages/gen-ai-hub/jest.config.mjs b/packages/gen-ai-hub/jest.config.mjs index bfba9699..a33e1a16 100644 --- a/packages/gen-ai-hub/jest.config.mjs +++ b/packages/gen-ai-hub/jest.config.mjs @@ -2,6 +2,15 @@ import config from '../../jest.config.mjs'; const genAiConfig = { ...config, displayName: 'gen-ai-hub', + transform: { + '^.+\\.ts$': [ + 'ts-jest', + { + tsconfig: 'tsconfig.test.json', + useESM: true + } + ] + }, }; export default genAiConfig; diff --git a/packages/gen-ai-hub/tsconfig.test.json b/packages/gen-ai-hub/tsconfig.test.json new file mode 100644 index 00000000..e2272c3e --- /dev/null +++ b/packages/gen-ai-hub/tsconfig.test.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "../..", + "noEmit": true + }, + "include": ["src/**/*.ts", "**/*.test.ts", "../../test-util/**/*.ts"], + "exclude": ["dist/**/*", "node_modules/**/*"] +} \ No newline at end of file From a1ccbd9f1554bd780cc8a3f825c5106bf3a629ce Mon Sep 17 00:00:00 2001 From: Tom Frenken Date: Mon, 12 Aug 2024 11:10:36 +0200 Subject: [PATCH 06/19] remove log --- log.txt | 141 -------------------------------------------------------- 1 file changed, 141 deletions(-) delete mode 100644 log.txt diff --git a/log.txt b/log.txt deleted file mode 100644 index 165959e3..00000000 --- a/log.txt +++ /dev/null @@ -1,141 +0,0 @@ - -> sap-ai-sdk@0.0.0 test:unit /Users/I519840/Code/ai-core/ai-sdk-js -> NODE_OPTIONS=--experimental-vm-modules pnpm -r -F=./packages/** test - -Scope: 3 of 7 workspace projects -packages/ai-core test$ NODE_OPTIONS=--experimental-vm-modules jest -packages/core test$ NODE_OPTIONS=--experimental-vm-modules jest -packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/ai-core test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/core test: (node:78069) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/ai-core test: (node:78070) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/ai-core test: (node:78071) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/ai-core test: (node:78068) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/ai-core test: (node:78067) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/ai-core test: (node:78065) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/ai-core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/core test: (node:78066) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/core test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/core test: PASS core src/http-client.test.ts -packages/ai-core test: PASS ai-core src/client/AI_CORE_API/scenario-api.test.ts -packages/core test: PASS core src/context.test.ts -packages/core test: ● Console -packages/core test: console.log -packages/core test: [2024-08-12T09:05:11.266Z] WARN (environment-accessor): Could not find service binding of type 'aicore'. This might cause errors in other parts of the application. -packages/core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/core test: console.log -packages/core test: [2024-08-12T09:05:11.282Z] INFO (context): Found a service key in environment variable "aicore". Using a service key is recommended for local testing only. Bind the AI Core service to the application for productive usage. -packages/core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/core test: console.log -packages/core test: [2024-08-12T09:05:11.282Z] WARN (client-credentials-token-cache): Cannot create cache key for client credentials token cache. The given tenant ID is undefined. -packages/core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/core test: Test Suites: 2 passed, 2 total -packages/core test: Tests: 4 passed, 4 total -packages/core test: Snapshots: 1 passed, 1 total -packages/core test: Time: 3.683 s -packages/core test: Ran all test suites. -packages/ai-core test: PASS ai-core src/client/AI_CORE_API/artifact-api.test.ts -packages/ai-core test: ● Console -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.335Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/artifacts/. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.349Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/artifacts. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: PASS ai-core src/client/AI_CORE_API/configuration-api.test.ts -packages/ai-core test: ● Console -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.335Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/configurations/. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.345Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/configurations. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: PASS ai-core src/client/AI_CORE_API/execution-api.test.ts -packages/ai-core test: ● Console -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.347Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/executions/. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.359Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/executions. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/core test: Done -packages/ai-core test: PASS ai-core src/client/AI_CORE_API/deployment-api.test.ts -packages/ai-core test: ● Console -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.350Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.361Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.367Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h/. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.369Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.375Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h/. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: console.log -packages/ai-core test: [2024-08-12T09:05:11.376Z] WARN (csrf-middleware): Failed to get CSRF token from URL: /lm/deployments/4e5f6g7h. -packages/ai-core test: at Console.log (../../node_modules/.pnpm/winston@3.13.1/node_modules/winston/lib/winston/transports/console.js:79:23) -packages/ai-core test: Test Suites: 5 passed, 5 total -packages/ai-core test: Tests: 11 passed, 11 total -packages/ai-core test: Snapshots: 0 total -packages/ai-core test: Time: 3.791 s -packages/ai-core test: Ran all test suites. -packages/ai-core test: Done -packages/gen-ai-hub test$ NODE_OPTIONS=--experimental-vm-modules jest -packages/gen-ai-hub test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/gen-ai-hub test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/gen-ai-hub test: ts-jest[versions] (WARN) Version 30.0.0-alpha.6 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=29.0.0 <30.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions. -packages/gen-ai-hub test: FAIL gen-ai-hub src/orchestration/orchestration-client.test.ts -packages/gen-ai-hub test: ● Test suite failed to run -packages/gen-ai-hub test: SyntaxError: The requested module '@sap-ai-sdk/core' does not provide an export named 'executeRequest' -packages/gen-ai-hub test: 16 | })); -packages/gen-ai-hub test: 17 | -packages/gen-ai-hub test: > 18 | const { GenAiHubClient, constructCompletionPostRequest } = await import( -packages/gen-ai-hub test: | ^ -packages/gen-ai-hub test: 19 | './orchestration-client.js' -packages/gen-ai-hub test: 20 | ); -packages/gen-ai-hub test: 21 | describe('GenAiHubClient', () => { -packages/gen-ai-hub test: at Runtime.linkAndEvaluateModule (../../node_modules/.pnpm/jest-runtime@30.0.0-alpha.6/node_modules/jest-runtime/build/index.js:685:5) -packages/gen-ai-hub test: at src/orchestration/orchestration-client.test.ts:18:60 -packages/gen-ai-hub test: (node:78164) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/gen-ai-hub test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/gen-ai-hub test: FAIL gen-ai-hub src/client/openai/openai-client.test.ts -packages/gen-ai-hub test: ● Test suite failed to run -packages/gen-ai-hub test: SyntaxError: The requested module '@sap-ai-sdk/core' does not provide an export named 'executeRequest' -packages/gen-ai-hub test: 21 | ) -packages/gen-ai-hub test: 22 | })); -packages/gen-ai-hub test: > 23 | const { OpenAiClient } = await import('./openai-client.js'); -packages/gen-ai-hub test: | ^ -packages/gen-ai-hub test: 24 | -packages/gen-ai-hub test: 25 | describe('openai client', () => { -packages/gen-ai-hub test: 26 | let destination: HttpDestination; -packages/gen-ai-hub test: at Runtime.linkAndEvaluateModule (../../node_modules/.pnpm/jest-runtime@30.0.0-alpha.6/node_modules/jest-runtime/build/index.js:685:5) -packages/gen-ai-hub test: at src/client/openai/openai-client.test.ts:23:26 -packages/gen-ai-hub test: (node:78163) ExperimentalWarning: VM Modules is an experimental feature and might change at any time -packages/gen-ai-hub test: (Use `node --trace-warnings ...` to show where the warning was created) -packages/gen-ai-hub test: Test Suites: 2 failed, 2 total -packages/gen-ai-hub test: Tests: 0 total -packages/gen-ai-hub test: Snapshots: 0 total -packages/gen-ai-hub test: Time: 2.143 s -packages/gen-ai-hub test: Ran all test suites. -packages/gen-ai-hub test: Failed -/Users/I519840/Code/ai-core/ai-sdk-js/packages/gen-ai-hub: - ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  @sap-ai-sdk/gen-ai-hub@0.0.0 test: `NODE_OPTIONS=--experimental-vm-modules jest` -Exit status 1 - ELIFECYCLE  Command failed with exit code 1. From c37aa3101574a946da0d9478fc2fe502cb70821f Mon Sep 17 00:00:00 2001 From: Tom Frenken Date: Mon, 12 Aug 2024 14:34:55 +0200 Subject: [PATCH 07/19] change tsconfig setup --- packages/ai-core/jest.config.mjs | 2 +- packages/core/jest.config.mjs | 2 +- packages/core/tsconfig.test.json | 9 --------- packages/gen-ai-hub/jest.config.mjs | 2 +- packages/gen-ai-hub/tsconfig.test.json | 9 --------- .../ai-core/tsconfig.test.json => tsconfig.test.json | 4 ++-- 6 files changed, 5 insertions(+), 23 deletions(-) delete mode 100644 packages/core/tsconfig.test.json delete mode 100644 packages/gen-ai-hub/tsconfig.test.json rename packages/ai-core/tsconfig.test.json => tsconfig.test.json (57%) diff --git a/packages/ai-core/jest.config.mjs b/packages/ai-core/jest.config.mjs index 1b314717..90936ce2 100644 --- a/packages/ai-core/jest.config.mjs +++ b/packages/ai-core/jest.config.mjs @@ -6,7 +6,7 @@ const aiCoreConfig = { '^.+\\.ts$': [ 'ts-jest', { - tsconfig: 'tsconfig.test.json', + tsconfig: '../../tsconfig.test.json', useESM: true } ] diff --git a/packages/core/jest.config.mjs b/packages/core/jest.config.mjs index a278348d..eef585d7 100644 --- a/packages/core/jest.config.mjs +++ b/packages/core/jest.config.mjs @@ -6,7 +6,7 @@ const coreConfig = { '^.+\\.ts$': [ 'ts-jest', { - tsconfig: 'tsconfig.test.json', + tsconfig: '../../tsconfig.test.json', useESM: true } ] diff --git a/packages/core/tsconfig.test.json b/packages/core/tsconfig.test.json deleted file mode 100644 index e2272c3e..00000000 --- a/packages/core/tsconfig.test.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "rootDir": "../..", - "noEmit": true - }, - "include": ["src/**/*.ts", "**/*.test.ts", "../../test-util/**/*.ts"], - "exclude": ["dist/**/*", "node_modules/**/*"] -} \ No newline at end of file diff --git a/packages/gen-ai-hub/jest.config.mjs b/packages/gen-ai-hub/jest.config.mjs index a33e1a16..9f828100 100644 --- a/packages/gen-ai-hub/jest.config.mjs +++ b/packages/gen-ai-hub/jest.config.mjs @@ -6,7 +6,7 @@ const genAiConfig = { '^.+\\.ts$': [ 'ts-jest', { - tsconfig: 'tsconfig.test.json', + tsconfig: '../../tsconfig.test.json', useESM: true } ] diff --git a/packages/gen-ai-hub/tsconfig.test.json b/packages/gen-ai-hub/tsconfig.test.json deleted file mode 100644 index e2272c3e..00000000 --- a/packages/gen-ai-hub/tsconfig.test.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "rootDir": "../..", - "noEmit": true - }, - "include": ["src/**/*.ts", "**/*.test.ts", "../../test-util/**/*.ts"], - "exclude": ["dist/**/*", "node_modules/**/*"] -} \ No newline at end of file diff --git a/packages/ai-core/tsconfig.test.json b/tsconfig.test.json similarity index 57% rename from packages/ai-core/tsconfig.test.json rename to tsconfig.test.json index e2272c3e..fed84345 100644 --- a/packages/ai-core/tsconfig.test.json +++ b/tsconfig.test.json @@ -1,9 +1,9 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "rootDir": "../..", + "rootDir": ".", "noEmit": true }, - "include": ["src/**/*.ts", "**/*.test.ts", "../../test-util/**/*.ts"], + "include": ["packages/**/*.ts", "test-util/**/*.ts"], "exclude": ["dist/**/*", "node_modules/**/*"] } \ No newline at end of file From 8b1bc66eedb929c7b762879db41e7c7cddc8b8f1 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 12 Aug 2024 20:58:40 +0200 Subject: [PATCH 08/19] mock env in global setup --- global-test-setup.ts | 11 +++ global-test-teardown.ts | 3 + jest.config.mjs | 4 +- package.json | 4 +- packages/ai-core/jest.config.mjs | 6 +- packages/core/jest.config.mjs | 6 +- packages/core/src/context.test.ts | 24 +----- packages/core/src/context.ts | 8 +- packages/core/src/http-client.test.ts | 31 +++---- packages/gen-ai-hub/jest.config.mjs | 6 +- .../src/client/openai/openai-client.test.ts | 65 +++++---------- .../orchestration-client.test.ts | 35 +++----- pnpm-lock.yaml | 20 +++-- test-util/mock-context.ts | 82 ------------------- test-util/mock-http.ts | 81 ++++++++++++++++-- test-util/mock-jwt.ts | 23 ++++++ 16 files changed, 193 insertions(+), 216 deletions(-) create mode 100644 global-test-setup.ts create mode 100644 global-test-teardown.ts delete mode 100644 test-util/mock-context.ts create mode 100644 test-util/mock-jwt.ts diff --git a/global-test-setup.ts b/global-test-setup.ts new file mode 100644 index 00000000..d5e2167b --- /dev/null +++ b/global-test-setup.ts @@ -0,0 +1,11 @@ +export default async function mockAiCoreEnvVariable() { + const aiCoreServiceCredentials = { + clientid: 'clientid', + clientsecret: 'clientsecret', + url: 'https://example.authentication.eu12.hana.ondemand.com', + serviceurls: { + AI_API_URL: 'https://api.ai.ml.hana.ondemand.com' + } + } + process.env['aicore'] = JSON.stringify(aiCoreServiceCredentials); +} diff --git a/global-test-teardown.ts b/global-test-teardown.ts new file mode 100644 index 00000000..23a0d973 --- /dev/null +++ b/global-test-teardown.ts @@ -0,0 +1,3 @@ +export default async function tearDown() { + delete process.env.aicore; +} \ No newline at end of file diff --git a/jest.config.mjs b/jest.config.mjs index 2bca7c35..9ed597f4 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -54,10 +54,10 @@ const config = { // forceCoverageMatch: [], // A path to a module which exports an async function that is triggered once before all test suites - // globalSetup: undefined, + globalSetup: '../../global-test-setup.ts', // A path to a module which exports an async function that is triggered once after all test suites - // globalTeardown: undefined, + globalTeardown: '../../global-test-teardown.ts', // A set of global variables that need to be available in all test environments // globals: {}, diff --git a/package.json b/package.json index 399005d3..ea05a686 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ "prettier": "^3.3.3", "ts-jest": "^29.2.4", "ts-node": "^10.9.2", - "typescript": "^5.5.4" + "typescript": "^5.5.4", + "jsonwebtoken": "^9.0.2", + "@types/jsonwebtoken": "^9.0.2" } } diff --git a/packages/ai-core/jest.config.mjs b/packages/ai-core/jest.config.mjs index 90936ce2..f0a79077 100644 --- a/packages/ai-core/jest.config.mjs +++ b/packages/ai-core/jest.config.mjs @@ -7,9 +7,9 @@ const aiCoreConfig = { 'ts-jest', { tsconfig: '../../tsconfig.test.json', - useESM: true - } - ] + useESM: true, + }, + ], }, }; diff --git a/packages/core/jest.config.mjs b/packages/core/jest.config.mjs index eef585d7..338ee630 100644 --- a/packages/core/jest.config.mjs +++ b/packages/core/jest.config.mjs @@ -7,9 +7,9 @@ const coreConfig = { 'ts-jest', { tsconfig: '../../tsconfig.test.json', - useESM: true - } - ] + useESM: true, + }, + ], }, }; diff --git a/packages/core/src/context.test.ts b/packages/core/src/context.test.ts index 4769c1c9..9640d554 100644 --- a/packages/core/src/context.test.ts +++ b/packages/core/src/context.test.ts @@ -1,28 +1,12 @@ -import { - mockAiCoreEnvVariable, - mockClientCredentialsGrantCall -} from '../../../test-util/mock-context.js'; +import nock from 'nock'; +import { mockClientCredentialsGrantCall } from '../../../test-util/mock-http.js'; import { getAiCoreDestination } from './context.js'; describe('context', () => { - it('should throw if ai-core binding is not found', async () => { - await expect(getAiCoreDestination()).rejects.toThrow( - 'Could not find service credentials for AI Core. Please check the service binding.' - ); - }); - - it('should throw for ill formatted JSON', async () => { - process.env.aicore = 'Improper JSON string'; - - await expect(getAiCoreDestination()).rejects - .toThrowErrorMatchingInlineSnapshot(` - "Error in parsing service key from the "aicore" environment variable. - Cause: Unexpected token 'I', "Improper JSON string" is not valid JSON" - `); + afterEach(() => { + nock.cleanAll(); }); - it('should throw if client credentials are not fetched', async () => { - mockAiCoreEnvVariable(); mockClientCredentialsGrantCall( { error: 'unauthorized', diff --git a/packages/core/src/context.ts b/packages/core/src/context.ts index 7b8ac8e5..35b3740a 100644 --- a/packages/core/src/context.ts +++ b/packages/core/src/context.ts @@ -2,6 +2,7 @@ import { createLogger } from '@sap-cloud-sdk/util'; import { Destination, Service, + ServiceCredentials, getServiceBinding, transformServiceBindingToDestination } from '@sap-cloud-sdk/connectivity'; @@ -31,7 +32,8 @@ export async function getAiCoreDestination(): Promise { const aiCoreDestination = await transformServiceBindingToDestination( aiCoreServiceBinding, { - useCache: true + useCache: true, + jwt: { zid: 'dummy-tenant' } } ); return aiCoreDestination; @@ -52,7 +54,9 @@ function getAiCoreServiceKeyFromEnv(): Service | undefined { } } -function parseServiceKeyFromEnv(aiCoreEnv: string | undefined) { +function parseServiceKeyFromEnv( + aiCoreEnv: string | undefined +): ServiceCredentials | undefined { if (aiCoreEnv) { try { return JSON.parse(aiCoreEnv); diff --git a/packages/core/src/http-client.test.ts b/packages/core/src/http-client.test.ts index 8ee15dc2..bf608740 100644 --- a/packages/core/src/http-client.test.ts +++ b/packages/core/src/http-client.test.ts @@ -1,26 +1,18 @@ -import { jest } from '@jest/globals'; -import { HttpDestination } from '@sap-cloud-sdk/connectivity'; -import { mockGetAiCoreDestination } from '../../../test-util/mock-context.js'; -import { mockInference } from '../../../test-util/mock-http.js'; - -jest.unstable_mockModule('./context.js', () => ({ - getAiCoreDestination: jest.fn(() => - Promise.resolve(mockGetAiCoreDestination()) - ) -})); -const { executeRequest } = await import('./http-client.js'); +import nock from 'nock'; +import { + mockClientCredentialsGrantCall, + mockInference +} from '../../../test-util/mock-http.js'; +import { dummyToken } from '../../../test-util/mock-jwt.js'; +import { executeRequest } from './http-client.js'; describe('http-client', () => { - let destination: HttpDestination; - beforeAll(() => { - destination = mockGetAiCoreDestination(); + mockClientCredentialsGrantCall({ access_token: dummyToken }, 200); }); - - afterAll(() => { - jest.restoreAllMocks(); + afterEach(() => { + nock.cleanAll(); }); - it('should execute a request to the AI Core service', async () => { const mockPrompt = { prompt: 'some test prompt' }; const mockPromptResponse = { completion: 'some test completion' }; @@ -35,8 +27,7 @@ describe('http-client', () => { { data: mockPromptResponse, status: 200 - }, - destination + } ); const res = await executeRequest( { url: '/mock-endpoint', apiVersion: 'mock-api-version' }, diff --git a/packages/gen-ai-hub/jest.config.mjs b/packages/gen-ai-hub/jest.config.mjs index 9f828100..b7e23360 100644 --- a/packages/gen-ai-hub/jest.config.mjs +++ b/packages/gen-ai-hub/jest.config.mjs @@ -7,9 +7,9 @@ const genAiConfig = { 'ts-jest', { tsconfig: '../../tsconfig.test.json', - useESM: true - } - ] + useESM: true, + }, + ], }, }; diff --git a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts index bbd4398c..89d9faad 100644 --- a/packages/gen-ai-hub/src/client/openai/openai-client.test.ts +++ b/packages/gen-ai-hub/src/client/openai/openai-client.test.ts @@ -1,13 +1,11 @@ import nock from 'nock'; -import { jest } from '@jest/globals'; -import { HttpDestination } from '@sap-cloud-sdk/connectivity'; import { BaseLlmParametersWithDeploymentId } from '@sap-ai-sdk/core'; -import { EndpointOptions } from '@sap-ai-sdk/core/src/http-client.js'; -import { mockGetAiCoreDestination } from '../../../../../test-util/mock-context.js'; import { + mockClientCredentialsGrantCall, mockInference, parseMockResponse } from '../../../../../test-util/mock-http.js'; +import { dummyToken } from '../../../../../test-util/mock-jwt.js'; import { OpenAiChatCompletionOutput, OpenAiChatCompletionParameters, @@ -15,41 +13,29 @@ import { OpenAiEmbeddingOutput, OpenAiEmbeddingParameters } from './openai-types.js'; -jest.unstable_mockModule('@sap-ai-sdk/core', () => ({ - getAiCoreDestination: jest.fn(() => - Promise.resolve(mockGetAiCoreDestination()) - ) -})); -const { OpenAiClient } = await import('./openai-client.js'); +import { OpenAiClient } from './openai-client.js'; describe('openai client', () => { - let destination: HttpDestination; const deploymentConfiguration: BaseLlmParametersWithDeploymentId = { deploymentId: 'deployment-id' }; - let chatCompletionEndpoint: EndpointOptions; - let embeddingsEndpoint: EndpointOptions; - - beforeAll(() => { - destination = mockGetAiCoreDestination(); - - chatCompletionEndpoint = { - url: 'chat/completions', - apiVersion: '2024-02-01' - }; + const chatCompletionEndpoint = { + url: 'chat/completions', + apiVersion: '2024-02-01' + }; + const embeddingsEndpoint = { + url: 'embeddings', + apiVersion: '2024-02-01' + }; - embeddingsEndpoint = { - url: 'embeddings', - apiVersion: '2024-02-01' - }; - }); + const client = new OpenAiClient(); - afterEach(() => { - nock.cleanAll(); + beforeAll(() => { + mockClientCredentialsGrantCall({ access_token: dummyToken }, 200); }); afterAll(() => { - jest.restoreAllMocks(); + nock.cleanAll(); }); describe('chatCompletion', () => { @@ -79,13 +65,11 @@ describe('openai client', () => { data: mockResponse, status: 200 }, - destination, chatCompletionEndpoint ); - expect(new OpenAiClient().chatCompletion(request)).resolves.toEqual( - mockResponse - ); + const response = await client.chatCompletion(request); + expect(response).toEqual(mockResponse); }); it('throws on bad request', async () => { @@ -107,13 +91,10 @@ describe('openai client', () => { data: mockResponse, status: 400 }, - destination, chatCompletionEndpoint ); - await expect( - new OpenAiClient().chatCompletion(request) - ).rejects.toThrow(); + expect(client.chatCompletion(request)).rejects.toThrow(); }); }); @@ -137,13 +118,10 @@ describe('openai client', () => { data: mockResponse, status: 200 }, - destination, embeddingsEndpoint ); - - expect(new OpenAiClient().embeddings(request)).resolves.toEqual( - mockResponse - ); + const response = await client.embeddings(request); + expect(response).toEqual(mockResponse); }); it('throws on bad request', async () => { @@ -165,11 +143,10 @@ describe('openai client', () => { data: mockResponse, status: 400 }, - destination, embeddingsEndpoint ); - expect(new OpenAiClient().embeddings(request)).rejects.toThrow(); + expect(client.embeddings(request)).rejects.toThrow(); }); }); }); diff --git a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts index 4e402184..5104c8f7 100644 --- a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts +++ b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts @@ -1,40 +1,30 @@ import nock from 'nock'; -import { jest } from '@jest/globals'; -import { HttpDestination } from '@sap-cloud-sdk/connectivity'; import { BaseLlmParametersWithDeploymentId } from '@sap-ai-sdk/core'; -import { mockGetAiCoreDestination } from '../../../../test-util/mock-context.js'; import { + mockClientCredentialsGrantCall, mockInference, parseMockResponse } from '../../../../test-util/mock-http.js'; +import { dummyToken } from '../../../../test-util/mock-jwt.js'; import { CompletionPostResponse } from './client/api/index.js'; import { GenAiHubCompletionParameters } from './orchestration-types.js'; -jest.unstable_mockModule('@sap-ai-sdk/core', () => ({ - getAiCoreDestination: jest.fn(() => - Promise.resolve(mockGetAiCoreDestination()) - ) -})); +import { + GenAiHubClient, + constructCompletionPostRequest +} from './orchestration-client.js'; -const { GenAiHubClient, constructCompletionPostRequest } = await import( - './orchestration-client.js' -); describe('GenAiHubClient', () => { - let destination: HttpDestination; const client = new GenAiHubClient(); const deploymentConfiguration: BaseLlmParametersWithDeploymentId = { deploymentId: 'deployment-id' }; beforeAll(() => { - destination = mockGetAiCoreDestination(); - }); - - afterEach(() => { - nock.cleanAll(); + mockClientCredentialsGrantCall({ access_token: dummyToken }, 200); }); afterAll(() => { - jest.restoreAllMocks(); + nock.cleanAll(); }); it('calls chatCompletion with minimum configuration and parses response', async () => { @@ -65,12 +55,12 @@ describe('GenAiHubClient', () => { data: mockResponse, status: 200 }, - destination, { url: 'completion' } ); - expect(client.chatCompletion(request)).resolves.toEqual(mockResponse); + const response = await client.chatCompletion(request); + expect(response).toEqual(mockResponse); }); it('sends message history together with templating config', async () => { @@ -115,12 +105,11 @@ describe('GenAiHubClient', () => { data: mockResponse, status: 200 }, - destination, { url: 'completion' } ); - - expect(client.chatCompletion(request)).resolves.toEqual(mockResponse); + const response = await client.chatCompletion(request); + expect(response).toEqual(mockResponse); }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 206bacc0..6949ac77 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,9 @@ importers: '@types/jest': specifier: ^29.5.12 version: 29.5.12 + '@types/jsonwebtoken': + specifier: ^9.0.2 + version: 9.0.6 '@types/node': specifier: ^20.14.15 version: 20.14.15 @@ -38,6 +41,9 @@ importers: jest: specifier: ^30.0.0-alpha.6 version: 30.0.0-alpha.6(@types/node@20.14.15)(ts-node@10.9.2(@types/node@20.14.15)(typescript@5.5.4)) + jsonwebtoken: + specifier: ^9.0.2 + version: 9.0.2 nock: specifier: ^13.5.4 version: 13.5.4 @@ -50,9 +56,6 @@ importers: ts-node: specifier: ^10.9.2 version: 10.9.2(@types/node@20.14.15)(typescript@5.5.4) - tsd: - specifier: ^0.31.0 - version: 0.31.1 typescript: specifier: ^5.5.4 version: 5.5.4 @@ -764,6 +767,9 @@ packages: '@types/json5@0.0.29': resolution: {integrity: sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==} + '@types/jsonwebtoken@9.0.6': + resolution: {integrity: sha512-/5hndP5dCjloafCXns6SZyESp3Ldq7YjH3zwzwczYnjxIT0Fqzk5ROSYVGfFyczIue7IUEj8hkvLbPoLQ18vQw==} + '@types/mime@1.3.5': resolution: {integrity: sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==} @@ -4339,7 +4345,7 @@ snapshots: '@types/connect@3.4.38': dependencies: - '@types/node': 20.14.14 + '@types/node': 20.14.15 '@types/eslint@7.29.0': dependencies: @@ -4393,6 +4399,10 @@ snapshots: '@types/json5@0.0.29': {} + '@types/jsonwebtoken@9.0.6': + dependencies: + '@types/node': 20.14.15 + '@types/mime@1.3.5': {} '@types/minimist@1.2.5': {} @@ -4418,7 +4428,7 @@ snapshots: '@types/send@0.17.4': dependencies: '@types/mime': 1.3.5 - '@types/node': 20.14.14 + '@types/node': 20.14.15 '@types/serve-static@1.15.7': dependencies: diff --git a/test-util/mock-context.ts b/test-util/mock-context.ts deleted file mode 100644 index 994abd24..00000000 --- a/test-util/mock-context.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { - DestinationAuthToken, - HttpDestination, - ServiceCredentials -} from '@sap-cloud-sdk/connectivity'; -import nock from 'nock'; - -export const aiCoreServiceBinding = { - label: 'aicore', - credentials: { - clientid: 'clientid', - clientsecret: 'clientsecret', - url: 'https://example.authentication.eu12.hana.ondemand.com', - identityzone: 'examplezone', - identityzoneid: 'examplezoneid', - appname: 'appname', - serviceurls: { - AI_API_URL: 'https://api.ai.ml.hana.ondemand.com' - } - } -}; - -export const aiCoreDestination = { - url: 'https://api.ai.ml.hana.ondemand.com' -}; - -export function createDestinationTokens( - token: string = 'mock-token', - expiresIn?: string -): { authTokens: DestinationAuthToken[] } { - return { - authTokens: [ - { - value: token, - type: 'bearer', - expiresIn, - http_header: { key: 'Authorization', value: `Bearer ${token}` }, - error: null - } - ] - }; -} - -export function mockAiCoreEnvVariable(): void { - process.env['aicore'] = JSON.stringify(aiCoreServiceBinding.credentials); -} - -/** - * @internal - */ -export function mockGetAiCoreDestination( - destination = aiCoreDestination -): HttpDestination { - const mockDestination: HttpDestination = { - ...destination, - authentication: 'OAuth2ClientCredentials', - ...createDestinationTokens() - }; - return mockDestination; -} - -export function mockClientCredentialsGrantCall( - response: any, - responseCode: number, - serviceCredentials: ServiceCredentials = aiCoreServiceBinding.credentials, - delay = 0 -): nock.Scope { - return nock(serviceCredentials.url, { - reqheaders: { - 'Content-Type': 'application/x-www-form-urlencoded', - Accept: 'application/json' - } - }) - .post('/oauth/token', { - grant_type: 'client_credentials', - client_id: serviceCredentials.clientid, - client_secret: serviceCredentials.clientsecret, - response_type: 'token' - }) - .delay(delay) - .reply(responseCode, response); -} diff --git a/test-util/mock-http.ts b/test-util/mock-http.ts index c010e1e4..9aab1202 100644 --- a/test-util/mock-http.ts +++ b/test-util/mock-http.ts @@ -1,23 +1,89 @@ import fs from 'fs'; import path from 'path'; -import { fileURLToPath } from 'url'; -import { HttpDestination } from '@sap-cloud-sdk/connectivity'; +import { DestinationAuthToken, HttpDestination, ServiceCredentials } from '@sap-cloud-sdk/connectivity'; import nock from 'nock'; import { BaseLlmParameters, CustomRequestConfig } from '@sap-ai-sdk/core'; import { EndpointOptions } from '@sap-ai-sdk/core/src/http-client.js'; +import { dummyToken } from './mock-jwt.js'; -// Get the directory of this file -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); +export const aiCoreDestination = { + url: 'https://api.ai.ml.hana.ondemand.com' +}; + +export const aiCoreServiceBinding = { + label: 'aicore', + credentials: { + clientid: 'clientid', + clientsecret: 'clientsecret', + url: 'https://example.authentication.eu12.hana.ondemand.com', + identityzone: 'examplezone', + identityzoneid: 'examplezoneid', + appname: 'appname', + serviceurls: { + AI_API_URL: aiCoreDestination.url + } + } +}; const mockEndpoint: EndpointOptions = { url: 'mock-endpoint', apiVersion: 'mock-api-version' }; +export function mockAiCoreEnvVariable(): void { + process.env['aicore'] = JSON.stringify(aiCoreServiceBinding.credentials); +} + +export function createDestinationTokens( + token: string = dummyToken, + expiresIn?: string +): { authTokens: DestinationAuthToken[] } { + return { + authTokens: [ + { + value: token, + type: 'bearer', + expiresIn, + http_header: { key: 'Authorization', value: `Bearer ${token}` }, + error: null + } + ] + }; +} + +/** + * @internal + */ +export function getMockedAiCoreDestination( + destination = aiCoreDestination +): HttpDestination { + const mockDestination: HttpDestination = { + ...destination, + authentication: 'OAuth2ClientCredentials', + ...createDestinationTokens() + }; + return mockDestination; +} + +export function mockClientCredentialsGrantCall( + response: any, + responseCode: number, + serviceCredentials: ServiceCredentials = aiCoreServiceBinding.credentials, + delay = 0 +): nock.Scope { + return nock(serviceCredentials.url) + .post('/oauth/token', { + grant_type: 'client_credentials', + client_id: serviceCredentials.clientid, + client_secret: serviceCredentials.clientsecret + }) + .delay(delay) + .reply(responseCode, response); +} + export function mockInference( request: { data: D; @@ -27,12 +93,11 @@ export function mockInference( data: any; status?: number; }, - destination: HttpDestination, endpoint: EndpointOptions = mockEndpoint ): nock.Scope { const { deploymentConfiguration, ...body } = request.data; const { url, apiVersion } = endpoint; - + const destination = getMockedAiCoreDestination(); return nock(destination.url, { reqheaders: { 'ai-resource-group': 'default', @@ -52,7 +117,7 @@ export function mockInference( */ export function parseMockResponse(client: string, fileName: string): T { const fileContent = fs.readFileSync( - path.join(__dirname, 'mock-data', client, fileName), + path.join('test', client, fileName), 'utf-8' ); diff --git a/test-util/mock-jwt.ts b/test-util/mock-jwt.ts new file mode 100644 index 00000000..909857fb --- /dev/null +++ b/test-util/mock-jwt.ts @@ -0,0 +1,23 @@ +import jwt from 'jsonwebtoken'; +import { generateKeyPairSync } from 'node:crypto'; + +export const { publicKey, privateKey } = generateKeyPairSync('rsa', { + modulusLength: 4096, + publicKeyEncoding: { + type: 'pkcs1', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs1', + format: 'pem' + } + }); + + export const dummyToken = jwt.sign( + { dummy: 'content' }, + privateKey, + { + algorithm: 'RS512' + } + ); + \ No newline at end of file From 76febedfa7e786ff6f1a0c3c62ec09c5d884b134 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Tue, 13 Aug 2024 10:48:48 +0200 Subject: [PATCH 09/19] fix tsconfig.test --- jest.config.mjs | 1 + packages/ai-core/jest.config.mjs | 9 --------- packages/core/jest.config.mjs | 9 --------- packages/gen-ai-hub/jest.config.mjs | 9 --------- 4 files changed, 1 insertion(+), 27 deletions(-) diff --git a/jest.config.mjs b/jest.config.mjs index 9ed597f4..6fa0d469 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -174,6 +174,7 @@ const config = { 'ts-jest', { useESM: true, + tsconfig: '../../tsconfig.test.json', }, ], }, diff --git a/packages/ai-core/jest.config.mjs b/packages/ai-core/jest.config.mjs index f0a79077..3d5aa2fa 100644 --- a/packages/ai-core/jest.config.mjs +++ b/packages/ai-core/jest.config.mjs @@ -2,15 +2,6 @@ import config from '../../jest.config.mjs'; const aiCoreConfig = { ...config, displayName: 'ai-core', - transform: { - '^.+\\.ts$': [ - 'ts-jest', - { - tsconfig: '../../tsconfig.test.json', - useESM: true, - }, - ], - }, }; export default aiCoreConfig; diff --git a/packages/core/jest.config.mjs b/packages/core/jest.config.mjs index 338ee630..0570d2af 100644 --- a/packages/core/jest.config.mjs +++ b/packages/core/jest.config.mjs @@ -2,15 +2,6 @@ import config from '../../jest.config.mjs'; const coreConfig = { ...config, displayName: 'core', - transform: { - '^.+\\.ts$': [ - 'ts-jest', - { - tsconfig: '../../tsconfig.test.json', - useESM: true, - }, - ], - }, }; export default coreConfig; diff --git a/packages/gen-ai-hub/jest.config.mjs b/packages/gen-ai-hub/jest.config.mjs index b7e23360..bfba9699 100644 --- a/packages/gen-ai-hub/jest.config.mjs +++ b/packages/gen-ai-hub/jest.config.mjs @@ -2,15 +2,6 @@ import config from '../../jest.config.mjs'; const genAiConfig = { ...config, displayName: 'gen-ai-hub', - transform: { - '^.+\\.ts$': [ - 'ts-jest', - { - tsconfig: '../../tsconfig.test.json', - useESM: true, - }, - ], - }, }; export default genAiConfig; From d85ae6d7d23a5c2be9c968599c8f166da2925944 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Tue, 13 Aug 2024 11:02:41 +0200 Subject: [PATCH 10/19] fix test --- .../gen-ai-hub/src/orchestration/orchestration-client.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts index c05a9d53..adbc9c63 100644 --- a/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts +++ b/packages/gen-ai-hub/src/orchestration/orchestration-client.test.ts @@ -97,7 +97,6 @@ describe('GenAiHubClient', () => { data: mockResponse, status: 200 }, - destination, { url: 'completion' } @@ -159,7 +158,6 @@ describe('GenAiHubClient', () => { data: mockResponse, status: 200 }, - destination, { url: 'completion' } From 8b02377cd9099c161e8c9f2323dfde7620664d19 Mon Sep 17 00:00:00 2001 From: Marika Marszalkowski <868536+marikaner@users.noreply.github.com> Date: Tue, 13 Aug 2024 09:21:28 +0000 Subject: [PATCH 11/19] add core to canary release --- .github/workflows/publish-canary.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/publish-canary.yml b/.github/workflows/publish-canary.yml index 20ab2bcf..3e9896d1 100644 --- a/.github/workflows/publish-canary.yml +++ b/.github/workflows/publish-canary.yml @@ -35,6 +35,7 @@ jobs: cat <> .changeset/canary-release-changeset.md --- '@sap-ai-sdk/ai-core': patch + '@sap-ai-sdk/core': patch '@sap-ai-sdk/gen-ai-hub': patch --- From b092f0649a0c607ed1e2de4cb85f00e88c36c9fd Mon Sep 17 00:00:00 2001 From: Marika Marszalkowski <868536+marikaner@users.noreply.github.com> Date: Tue, 13 Aug 2024 10:56:33 +0000 Subject: [PATCH 12/19] add ignored packages to config.json --- .changeset/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/config.json b/.changeset/config.json index 31ec3812..6b628ff7 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -7,5 +7,5 @@ "access": "public", "baseBranch": "main", "updateInternalDependencies": "patch", - "ignore": [] + "ignore": ["@sap-ai-sdk/sample-code", "@sap-ai-sdk/e2e-tests", "@sap-ai-sdk/type-tests"] } From 7bbfb96359f2f527b3886180707be1eedf24fae0 Mon Sep 17 00:00:00 2001 From: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:08:39 +0200 Subject: [PATCH 13/19] Update test-util/mock-http.ts Co-authored-by: Marika Marszalkowski <868536+marikaner@users.noreply.github.com> --- test-util/mock-http.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-util/mock-http.ts b/test-util/mock-http.ts index 9aab1202..bd1efc1b 100644 --- a/test-util/mock-http.ts +++ b/test-util/mock-http.ts @@ -47,7 +47,7 @@ export function createDestinationTokens( value: token, type: 'bearer', expiresIn, - http_header: { key: 'Authorization', value: `Bearer ${token}` }, + http_header: { key: 'authorization', value: `Bearer ${token}` }, error: null } ] From 629557618598b8d75a8e9cd22a0a17954013a9ac Mon Sep 17 00:00:00 2001 From: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:08:52 +0200 Subject: [PATCH 14/19] Update packages/core/src/context.test.ts Co-authored-by: Marika Marszalkowski <868536+marikaner@users.noreply.github.com> --- packages/core/src/context.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/context.test.ts b/packages/core/src/context.test.ts index 9640d554..00ceb833 100644 --- a/packages/core/src/context.test.ts +++ b/packages/core/src/context.test.ts @@ -6,6 +6,7 @@ describe('context', () => { afterEach(() => { nock.cleanAll(); }); + it('should throw if client credentials are not fetched', async () => { mockClientCredentialsGrantCall( { From f33ef61f7164c33f1f96e81baaa1b895a7f267d6 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Tue, 13 Aug 2024 13:12:22 +0200 Subject: [PATCH 15/19] review --- package.json | 2 +- packages/ai-core/jest.config.mjs | 6 ++---- packages/core/jest.config.mjs | 4 +--- packages/gen-ai-hub/jest.config.mjs | 4 +--- 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index ea05a686..24d97603 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "scripts": { "postinstall": "pnpm compile", "compile": "pnpm -r -w=false run compile", - "test:unit": "NODE_OPTIONS=--experimental-vm-modules pnpm -r -F=./packages/** test", + "test:unit": "pnpm -r -F=./packages/** test", "test:type": "pnpm -F=@sap-ai-sdk/type-tests test", "test:e2e": "pnpm -F=@sap-ai-sdk/e2e-tests test", "lint": "pnpm -r run lint", diff --git a/packages/ai-core/jest.config.mjs b/packages/ai-core/jest.config.mjs index 3d5aa2fa..b1d7ba7a 100644 --- a/packages/ai-core/jest.config.mjs +++ b/packages/ai-core/jest.config.mjs @@ -1,7 +1,5 @@ import config from '../../jest.config.mjs'; -const aiCoreConfig = { +export default { ...config, displayName: 'ai-core', -}; - -export default aiCoreConfig; +}; \ No newline at end of file diff --git a/packages/core/jest.config.mjs b/packages/core/jest.config.mjs index 0570d2af..daae3e2f 100644 --- a/packages/core/jest.config.mjs +++ b/packages/core/jest.config.mjs @@ -1,7 +1,5 @@ import config from '../../jest.config.mjs'; -const coreConfig = { +export default { ...config, displayName: 'core', }; - -export default coreConfig; diff --git a/packages/gen-ai-hub/jest.config.mjs b/packages/gen-ai-hub/jest.config.mjs index bfba9699..e5b85b14 100644 --- a/packages/gen-ai-hub/jest.config.mjs +++ b/packages/gen-ai-hub/jest.config.mjs @@ -1,7 +1,5 @@ import config from '../../jest.config.mjs'; -const genAiConfig = { +export default { ...config, displayName: 'gen-ai-hub', }; - -export default genAiConfig; From 176a916eed6daced5448e91f220e297d0522d157 Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Tue, 13 Aug 2024 11:13:06 +0000 Subject: [PATCH 16/19] fix: Changes from lint --- packages/ai-core/jest.config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ai-core/jest.config.mjs b/packages/ai-core/jest.config.mjs index b1d7ba7a..596d4a8d 100644 --- a/packages/ai-core/jest.config.mjs +++ b/packages/ai-core/jest.config.mjs @@ -2,4 +2,4 @@ import config from '../../jest.config.mjs'; export default { ...config, displayName: 'ai-core', -}; \ No newline at end of file +}; From 88f08014025028f6ebd826881b83ddcc7d97540a Mon Sep 17 00:00:00 2001 From: Tom Frenken Date: Tue, 13 Aug 2024 14:25:06 +0200 Subject: [PATCH 17/19] update docs --- global-test-setup.ts | 7 +++++-- global-test-teardown.ts | 7 +++++-- tsconfig.test.json | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/global-test-setup.ts b/global-test-setup.ts index d5e2167b..8c14b538 100644 --- a/global-test-setup.ts +++ b/global-test-setup.ts @@ -1,4 +1,7 @@ -export default async function mockAiCoreEnvVariable() { +/** + * This file is used to mock the environment variables that are required for the tests. + */ +export default async function mockAiCoreEnvVariable(): Promise { const aiCoreServiceCredentials = { clientid: 'clientid', clientsecret: 'clientsecret', @@ -6,6 +9,6 @@ export default async function mockAiCoreEnvVariable() { serviceurls: { AI_API_URL: 'https://api.ai.ml.hana.ondemand.com' } - } + }; process.env['aicore'] = JSON.stringify(aiCoreServiceCredentials); } diff --git a/global-test-teardown.ts b/global-test-teardown.ts index 23a0d973..42e8c459 100644 --- a/global-test-teardown.ts +++ b/global-test-teardown.ts @@ -1,3 +1,6 @@ -export default async function tearDown() { +/** + * This file is used to run code after all tests have been run. + */ +export default async function tearDown(): Promise { delete process.env.aicore; -} \ No newline at end of file +} diff --git a/tsconfig.test.json b/tsconfig.test.json index fed84345..24573e6a 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -6,4 +6,4 @@ }, "include": ["packages/**/*.ts", "test-util/**/*.ts"], "exclude": ["dist/**/*", "node_modules/**/*"] -} \ No newline at end of file +} From 8208482679ec8c5807343b5f5f8db5a5d855f153 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Tue, 13 Aug 2024 14:42:14 +0200 Subject: [PATCH 18/19] docs --- packages/core/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/core/README.md b/packages/core/README.md index e69de29b..e756986c 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -0,0 +1,18 @@ +# @sap-ai-sdk/core + +This package contains core utility functions that we reuse in the SDK to set the context and execute HTTP requests. +They are primarily designed for internal usage. + +### Installation + +``` +$ npm install @sap-ai-sdk/core +``` + +### Usage + +The core package is not intended for direct usage. + +## License + +The SAP AI SDK is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/) \ No newline at end of file From ea92d772e96b0944dd76c4e8ea66c3878eccfea8 Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Tue, 13 Aug 2024 12:42:58 +0000 Subject: [PATCH 19/19] fix: Changes from lint --- packages/core/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/README.md b/packages/core/README.md index e756986c..8ed23e4b 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -15,4 +15,4 @@ The core package is not intended for direct usage. ## License -The SAP AI SDK is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/) \ No newline at end of file +The SAP AI SDK is released under the [Apache License Version 2.0.](http://www.apache.org/licenses/)