Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Restructure packages #61

Merged
merged 24 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,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/[email protected]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
cat <<EOT >> .changeset/canary-release-changeset.md
---
'@sap-ai-sdk/ai-core': patch
'@sap-ai-sdk/core': patch
'@sap-ai-sdk/gen-ai-hub': patch
---

Expand Down
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*'],
},
Expand Down
14 changes: 14 additions & 0 deletions global-test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This file is used to mock the environment variables that are required for the tests.
*/
export default async function mockAiCoreEnvVariable(): Promise<void> {
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);
}
6 changes: 6 additions & 0 deletions global-test-teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This file is used to run code after all tests have been run.
*/
export default async function tearDown(): Promise<void> {
delete process.env.aicore;
}
5 changes: 3 additions & 2 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down Expand Up @@ -174,6 +174,7 @@ const config = {
'ts-jest',
{
useESM: true,
tsconfig: '../../tsconfig.test.json',
},
],
},
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
"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": "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",
deekshas8 marked this conversation as resolved.
Show resolved Hide resolved
"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.15",
"@jest/globals": "^29.5.12",
Expand All @@ -34,7 +35,8 @@
"prettier": "^3.3.3",
"ts-jest": "^29.2.4",
"ts-node": "^10.9.2",
"tsd": "^0.31.0",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"jsonwebtoken": "^9.0.2",
"@types/jsonwebtoken": "^9.0.2"
}
}
4 changes: 1 addition & 3 deletions packages/ai-core/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import config from '../../jest.config.mjs';
const aiCoreConfig = {
export default {
...config,
displayName: 'ai-core',
};

export default aiCoreConfig;
18 changes: 18 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -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/)
5 changes: 5 additions & 0 deletions packages/core/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import config from '../../jest.config.mjs';
export default {
...config,
displayName: 'core',
};
33 changes: 33 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
22 changes: 22 additions & 0 deletions packages/core/src/context.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import nock from 'nock';
import { mockClientCredentialsGrantCall } from '../../../test-util/mock-http.js';
import { getAiCoreDestination } from './context.js';

describe('context', () => {
afterEach(() => {
nock.cleanAll();
});

it('should throw if client credentials are not fetched', async () => {
deekshas8 marked this conversation as resolved.
Show resolved Hide resolved
mockClientCredentialsGrantCall(
{
error: 'unauthorized',
error_description: 'Bad credentials'
},
401
);
await expect(getAiCoreDestination()).rejects.toThrow(
/Could not fetch client credentials token for service of type "aicore"/
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { createLogger } from '@sap-cloud-sdk/util';
import {
Destination,
Service,
ServiceCredentials,
getServiceBinding,
transformServiceBindingToDestination
} from '@sap-cloud-sdk/connectivity';

const logger = createLogger({
package: 'gen-ai-hub',
package: 'core',
messageContext: 'context'
});

Expand All @@ -31,7 +32,8 @@ export async function getAiCoreDestination(): Promise<Destination> {
const aiCoreDestination = await transformServiceBindingToDestination(
aiCoreServiceBinding,
{
useCache: true
useCache: true,
jwt: { zid: 'dummy-tenant' }
}
);
return aiCoreDestination;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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' };
Expand All @@ -35,8 +27,7 @@ describe('http-client', () => {
{
data: mockPromptResponse,
status: 200
},
destination
}
);
const res = await executeRequest(
{ url: '/mock-endpoint', apiVersion: 'mock-api-version' },
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {
executeRequest,
BaseLlmParametersWithDeploymentId,
BaseLlmParameters,
CustomRequestConfig
} from './http-client.js';
export { getAiCoreDestination } from './context.js';
11 changes: 11 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**/*"]
}
1 change: 1 addition & 0 deletions packages/gen-ai-hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"generate:orchestration": "openapi-generator --overwrite --generateESM -i ./src/orchestration/spec/api.yaml -o ./src/orchestration/client && pnpm lint:fix"
},
"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",
Expand Down
2 changes: 1 addition & 1 deletion packages/gen-ai-hub/src/client/interface.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down
Loading
Loading