Skip to content

Commit

Permalink
chore: Move utils (#107)
Browse files Browse the repository at this point in the history
* carve out the config and deployment resolution

* Update packages/gen-ai-hub/src/index.ts

* fix some tests and types

* carve out the config and deployment resolution

* Update packages/gen-ai-hub/src/index.ts

* fix some tests and types

* fix transpilation

* Update API and fix tests

* fix type tests

* fix public api + naming

* fix api check

* fix mocking

* move utils

* Update packages/gen-ai-hub/src/client/openai/index.ts

Co-authored-by: Tom Frenken <[email protected]>

* “fix” tests

* fix internal files

* fix index.js

* “fix” cjs check

* aaaargh

* re

* add moved changes

* Update packages/ai-core/src/utils/deployment-resolver.test.ts

* Update packages/ai-core/tsconfig.cjs.json

* Update packages/gen-ai-hub/src/orchestration/orchestration-client.ts

* fix api check

---------

Co-authored-by: Tom Frenken <[email protected]>
Co-authored-by: Tom Frenken <[email protected]>
  • Loading branch information
3 people authored Sep 5, 2024
1 parent 01ae301 commit 03f42b9
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 34 deletions.
2 changes: 2 additions & 0 deletions packages/ai-core/internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './dist/internal.js';
//# sourceMappingURL=internal.d.ts.map
2 changes: 2 additions & 0 deletions packages/ai-core/internal.js

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

7 changes: 5 additions & 2 deletions packages/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"dist/**/*.js",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/*.d.ts.map"
"dist/**/*.d.ts.map",
"internal.js",
"internal.d.ts"
],
"scripts": {
"compile": "tsc",
Expand All @@ -23,7 +25,8 @@
"lint": "eslint . && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
"lint:fix": "eslint . --fix && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error",
"generate": "openapi-generator --generateESM --clearOutputDir -i ./src/spec/AI_CORE_API.yaml -o ./src/client && pnpm update-imports && pnpm lint:fix",
"update-imports": "node --no-warnings --loader ts-node/esm ../../scripts/update-imports.ts ./src/client/AI_CORE_API"
"update-imports": "node --no-warnings --loader ts-node/esm ../../scripts/update-imports.ts ./src/client/AI_CORE_API",
"check:public-api": "node --loader ts-node/esm ../../scripts/check-public-api-cli.ts"
},
"dependencies": {
"@sap-ai-sdk/core": "workspace:^"
Expand Down
7 changes: 7 additions & 0 deletions packages/ai-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export * from './client/AI_CORE_API/index.js';

export type {
ModelDeployment,
DeploymentIdConfiguration,
ModelConfiguration,
ResourceGroupConfiguration
} from './utils/index.js';
1 change: 1 addition & 0 deletions packages/ai-core/src/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './utils/index.js';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AiDeployment } from '@sap-ai-sdk/ai-core';
import { type AiDeployment } from '../client/AI_CORE_API/index.js';
import { deploymentCache } from './deployment-cache.js';
import { FoundationModel } from './model.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cache } from '@sap-cloud-sdk/connectivity/internal.js';
import { type AiDeployment } from '@sap-ai-sdk/ai-core';
import { type AiDeployment } from '../client/AI_CORE_API/index.js';
import { type DeploymentResolutionOptions } from './deployment-resolver.js';
import { extractModel, type FoundationModel } from './model.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import nock from 'nock';
import { AiDeployment } from '@sap-ai-sdk/ai-core';
import {
mockClientCredentialsGrantCall,
aiCoreDestination,
mockDeploymentsList
} from '../../../../test-util/mock-http.js';
import { type AiDeployment } from '../client/AI_CORE_API/index.js';
import { resolveDeploymentId } from './deployment-resolver.js';
import { deploymentCache } from './deployment-cache.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DeploymentApi, type AiDeployment } from '@sap-ai-sdk/ai-core';
import {
type AiDeployment,
DeploymentApi
} from '../client/AI_CORE_API/index.js';
import { deploymentCache } from './deployment-cache.js';
import { extractModel, type FoundationModel } from './model.js';

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AiDeployment } from '@sap-ai-sdk/ai-core';
import { AiDeployment } from '../client/AI_CORE_API/index.js';

/**
* A foundation model is identified by its name and optionally a version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type CustomRequestConfig, executeRequest } from '@sap-ai-sdk/core';
import {
getDeploymentId,
type ModelDeployment
} from '../../utils/deployment-resolver.js';
} from '@sap-ai-sdk/ai-core/internal.js';
import type {
OpenAiChatCompletionParameters,
OpenAiChatCompletionOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type CustomRequestConfig, executeRequest } from '@sap-ai-sdk/core';
import {
getDeploymentId,
type ModelDeployment
} from '../../utils/deployment-resolver.js';
} from '@sap-ai-sdk/ai-core/internal.js';
import type {
OpenAiEmbeddingParameters,
OpenAiEmbeddingOutput,
Expand Down
7 changes: 0 additions & 7 deletions packages/gen-ai-hub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export type {
} from './client/index.js';
export { OpenAiChatClient, OpenAiEmbeddingClient } from './client/index.js';

export type {
ModelDeployment,
DeploymentIdConfiguration,
ModelConfiguration,
ResourceGroupConfiguration
} from './utils/index.js';

export type {
OrchestrationModuleConfig,
CompletionPostResponse,
Expand Down
1 change: 0 additions & 1 deletion packages/gen-ai-hub/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './client/index.js';
export * from './orchestration/index.js';
export * from './utils/index.js';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { executeRequest, CustomRequestConfig } from '@sap-ai-sdk/core';
import {
resolveDeploymentId,
ResourceGroupConfiguration
} from '../utils/deployment-resolver.js';
} from '@sap-ai-sdk/ai-core/internal.js';
import {
CompletionPostRequest,
CompletionPostResponse
Expand Down
46 changes: 31 additions & 15 deletions scripts/check-public-api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable jsdoc/require-jsdoc */

import { join, resolve, parse, basename, dirname } from 'path';
import path from 'path';
import path, { join, resolve, parse, basename, dirname } from 'path';
import { fileURLToPath } from 'url';
import { promises, existsSync } from 'fs';
import { glob } from 'glob';
import { createLogger, flatten, unixEOL } from '@sap-cloud-sdk/util';
import mock from 'mock-fs';
import { CompilerOptions } from 'typescript';
/* eslint-disable-next-line no-restricted-imports */
import {
readCompilerOptions,
readIncludeExcludeWithDefaults,
Expand Down Expand Up @@ -49,7 +47,7 @@ function paths(pathToPackage: string): {
pathToSource: join(pathToPackage, 'src'),
pathToTsConfig: join(pathToPackage, 'tsconfig.json'),
pathToNodeModules: join(pathToPackage, 'node_modules'),
pathCompiled: 'dist',
pathCompiled: 'dist'
};
}

Expand Down Expand Up @@ -129,15 +127,19 @@ export async function checkApiOfPackage(pathToPackage: string): Promise<void> {
mockFileSystem(pathToPackage);
const opts = await getCompilerOptions(pathToPackage);
const includeExclude = await readIncludeExcludeWithDefaults(pathToTsConfig);
await transpileDirectory(pathToSource, {
compilerOptions: opts,
// We have things in our sources like `#!/usr/bin/env node` in CLI `.js` files which is not working with parser of prettier.
createFileOptions: {
overwrite: true,
prettierOptions: defaultPrettierConfig,
usePrettier: false
}
}, { exclude: includeExclude?.exclude!, include: ['**/*.ts'] });
await transpileDirectory(
pathToSource,
{
compilerOptions: opts,
// We have things in our sources like `#!/usr/bin/env node` in CLI `.js` files which is not working with parser of prettier.
createFileOptions: {
overwrite: true,
prettierOptions: defaultPrettierConfig,
usePrettier: false
}
},
{ exclude: includeExclude?.exclude!, include: ['**/*.ts'] }
);
// await checkBarrelRecursive(pathToSource);

const indexFilePath = join(pathToSource, 'index.ts');
Expand Down Expand Up @@ -250,6 +252,17 @@ export function parseBarrelFile(fileContent: string, regex: RegExp): string[] {
return flatten(groups.map(group => group.split(',')));
}

// TODO: currently this is called in one place to fix a very specific issue for the AI Core API
// For some reason parsing the barrel file looks at "{ SOMETHING } from '." only, which is not enough and even wrong because it does also look at imports, not only exports
export function parseOtherExports(fileContent: string): string[] {
const normalized = fileContent.replace(/\s+/g, ' ');
const groups = [
...captureGroupsFromGlobalRegex(/export const (\w+)/g, normalized),
...captureGroupsFromGlobalRegex(/export type (\w+)/g, normalized)
];
return flatten(groups.map(group => group.split(',')));
}

function checkInternalReExports(fileContent: string, filePath: string): void {
const internalReExports = parseBarrelFile(
fileContent,
Expand All @@ -268,9 +281,12 @@ export async function parseIndexFile(filePath: string): Promise<string[]> {
const cwd = dirname(filePath);
const fileContent = await readFile(filePath, 'utf-8');
checkInternalReExports(fileContent, filePath);
const localExports = parseBarrelFile(fileContent, regexExportedIndex);
const localExports = [
...parseBarrelFile(fileContent, regexExportedIndex),
...parseOtherExports(fileContent)
];
const starFiles = captureGroupsFromGlobalRegex(
/export \* from '([\w\/.-]+)'/g,
/export \* from '([\w/.-]+)'/g,
fileContent
);
const starFileExports = await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion test-util/mock-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { type EndpointOptions } from '@sap-ai-sdk/core';
import {
type FoundationModel,
type DeploymentResolutionOptions
} from '@sap-ai-sdk/gen-ai-hub/internal.js';
} from '@sap-ai-sdk/ai-core/internal.js';
import { dummyToken } from './mock-jwt.js';

// Get the directory of this file
Expand Down

0 comments on commit 03f42b9

Please sign in to comment.