@mastra/openai 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@mastra/openai",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "module": "dist/mylib.esm.js",
7
+ "typings": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "husky": {
13
+ "hooks": {
14
+ "pre-commit": "dts lint"
15
+ }
16
+ },
17
+ "engines": {
18
+ "node": ">=20 <22"
19
+ },
20
+ "size-limit": [
21
+ {
22
+ "path": "dist/mylib.cjs.production.min.js",
23
+ "limit": "10 KB"
24
+ },
25
+ {
26
+ "path": "dist/mylib.esm.js",
27
+ "limit": "10 KB"
28
+ }
29
+ ],
30
+ "devDependencies": {
31
+ "@jest/globals": "^29.7.0",
32
+ "@rollup/plugin-image": "^3.0.3",
33
+ "@size-limit/preset-small-lib": "^11.1.4",
34
+ "@tsconfig/recommended": "^1.0.7",
35
+ "@types/jest": "^29.5.12",
36
+ "@types/lodash": "^4.17.7",
37
+ "@types/node": "^22.1.0",
38
+ "dts-cli": "^2.0.5",
39
+ "husky": "^9.1.4",
40
+ "jest": "^29.7.0",
41
+ "size-limit": "^11.1.4",
42
+ "ts-jest": "^29.2.4",
43
+ "tslib": "^2.6.3",
44
+ "typescript": "^5.5.4"
45
+ },
46
+ "keywords": [],
47
+ "author": "",
48
+ "license": "ISC",
49
+ "dependencies": {
50
+ "fets": "*",
51
+ "zod": "^3.23.8",
52
+ "@mastra/core": "0.1.0"
53
+ },
54
+ "scripts": {
55
+ "analyze": "size-limit --why",
56
+ "build": "dts build",
57
+ "build:dev": "dts watch",
58
+ "lint": "dts lint",
59
+ "size": "size-limit",
60
+ "start": "dts watch",
61
+ "test": "jest"
62
+ }
63
+ }
@@ -0,0 +1,59 @@
1
+ import {
2
+ describe,
3
+ it,
4
+ beforeAll,
5
+ afterAll, //expect
6
+ } from '@jest/globals';
7
+ import { Framework } from '@mastra/core';
8
+
9
+ import { OpenaiIntegration } from '.';
10
+
11
+ const API_KEY = process.env.API_KEY!;
12
+ const dbUri = process.env.DB_URL!;
13
+ const connectionId = process.env.CONNECTION_ID!;
14
+
15
+ const integrationName = 'OPENAI';
16
+
17
+ const integrationFramework = Framework.init({
18
+ name: 'TestFramework',
19
+ integrations: [new OpenaiIntegration()],
20
+ systemApis: [],
21
+ systemEvents: {},
22
+ db: {
23
+ provider: 'postgres',
24
+ uri: dbUri,
25
+ },
26
+ systemHostURL: 'http://localhost:3000',
27
+ routeRegistrationPath: '/api/mastra',
28
+ blueprintDirPath: '',
29
+ });
30
+
31
+ //const integration = integrationFramework.getIntegration(integrationName) as OpenaiIntegration
32
+
33
+ describe('openai', () => {
34
+ beforeAll(async () => {
35
+ await integrationFramework.connectIntegrationByCredential({
36
+ name: integrationName,
37
+ connectionId,
38
+ credential: {
39
+ value: {
40
+ API_KEY,
41
+ },
42
+ type: 'API_KEY',
43
+ },
44
+ });
45
+ });
46
+
47
+ it('should 200 on some apis', async () => {
48
+ //const client = await integration.getApiClient({ connectionId });
49
+ //const response = await client['/2010-04-01/Accounts.json'].get();
50
+ //expect(response.status).toBe(200);
51
+ });
52
+
53
+ afterAll(async () => {
54
+ await integrationFramework.disconnectIntegration({
55
+ name: integrationName,
56
+ connectionId,
57
+ });
58
+ });
59
+ });
Binary file
@@ -0,0 +1,37 @@
1
+
2
+ <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
3
+ viewBox="0 0 297.5 297.5" xml:space="preserve">
4
+ <g id="XMLID_40_">
5
+ <g>
6
+ <path style="fill:#ACBFC7;" d="M277.71,158.52v85.7H19.79v-85.7h6.53v40.54c0,16.98,13.81,30.78,30.78,30.78
7
+ s30.78-13.8,30.78-30.78v-40.54h30.09v40.54c0,16.98,13.81,30.78,30.78,30.78c16.98,0,30.78-13.8,30.78-30.78v-40.54h30.1v40.54
8
+ c0,16.98,13.8,30.78,30.78,30.78c16.97,0,30.78-13.8,30.78-30.78v-40.54H277.71z"/>
9
+ <rect x="218.66" y="53.28" style="fill:#CDD9DD;" width="43.49" height="10.53"/>
10
+ <rect x="229.17" y="83.35" style="fill:#CDD9DD;" width="22.48" height="23.92"/>
11
+ <rect x="137.51" y="83.35" style="fill:#CDD9DD;" width="22.48" height="23.92"/>
12
+ <rect x="127.01" y="53.28" style="fill:#CDD9DD;" width="43.49" height="10.53"/>
13
+ <rect x="35.35" y="53.28" style="fill:#CDD9DD;" width="43.49" height="10.53"/>
14
+ <rect x="45.86" y="83.35" style="fill:#CDD9DD;" width="22.48" height="23.92"/>
15
+ <path style="fill:#FF4855;" d="M251.65,126.81v72.25c0,6.2-5.05,11.24-11.24,11.24c-6.2,0-11.24-5.04-11.24-11.24v-72.25H251.65z"
16
+ />
17
+ <path style="fill:#D61616;" d="M68.34,126.81v72.25c0,6.2-5.04,11.24-11.24,11.24s-11.24-5.04-11.24-11.24v-72.25H68.34z"/>
18
+ <path style="fill:#FFD63F;" d="M159.99,126.81v72.25c0,6.2-5.04,11.24-11.24,11.24s-11.24-5.04-11.24-11.24v-72.25H159.99z"/>
19
+ <path d="M297.25,148.75v105.24c0,5.4-4.37,9.77-9.77,9.77H10.02c-5.39,0-9.77-4.37-9.77-9.77V148.75c0-5.4,4.38-9.77,9.77-9.77
20
+ h16.3V83.35h-0.74c-5.39,0-9.77-4.38-9.77-9.77V43.51c0-5.4,4.38-9.77,9.77-9.77h63.03c5.4,0,9.77,4.37,9.77,9.77v30.07
21
+ c0,5.39-4.37,9.77-9.77,9.77h-0.73v55.63h30.09V83.35h-0.73c-5.4,0-9.77-4.38-9.77-9.77V43.51c0-5.4,4.37-9.77,9.77-9.77h63.03
22
+ c5.39,0,9.77,4.37,9.77,9.77v30.07c0,5.39-4.38,9.77-9.77,9.77h-0.74v55.63h30.1V83.35h-0.74c-5.39,0-9.77-4.38-9.77-9.77V43.51
23
+ c0-5.4,4.38-9.77,9.77-9.77h63.03c5.4,0,9.77,4.37,9.77,9.77v30.07c0,5.39-4.37,9.77-9.77,9.77h-0.73v55.63h16.29
24
+ C292.88,138.98,297.25,143.35,297.25,148.75z M277.71,244.22v-85.7h-6.52v40.54c0,16.98-13.81,30.78-30.78,30.78
25
+ c-16.98,0-30.78-13.8-30.78-30.78v-40.54h-30.1v40.54c0,16.98-13.8,30.78-30.78,30.78c-16.97,0-30.78-13.8-30.78-30.78v-40.54
26
+ H87.88v40.54c0,16.98-13.81,30.78-30.78,30.78s-30.78-13.8-30.78-30.78v-40.54h-6.53v85.7H277.71z M262.15,63.81V53.28h-43.49
27
+ v10.53H262.15z M251.65,199.06v-72.25h-22.48v72.25c0,6.2,5.04,11.24,11.24,11.24C246.6,210.3,251.65,205.26,251.65,199.06z
28
+ M251.65,107.27V83.35h-22.48v23.92H251.65z M170.5,63.81V53.28h-43.49v10.53H170.5z M159.99,199.06v-72.25h-22.48v72.25
29
+ c0,6.2,5.04,11.24,11.24,11.24S159.99,205.26,159.99,199.06z M159.99,107.27V83.35h-22.48v23.92H159.99z M78.84,63.81V53.28H35.35
30
+ v10.53H78.84z M68.34,199.06v-72.25H45.86v72.25c0,6.2,5.04,11.24,11.24,11.24S68.34,205.26,68.34,199.06z M68.34,107.27V83.35
31
+ H45.86v23.92H68.34z"/>
32
+ </g>
33
+ <g>
34
+ </g>
35
+ </g>
36
+ </svg>
37
+
package/src/index.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { Integration, OpenAPI, IntegrationCredentialType, IntegrationAuth } from '@mastra/core';
2
+ import { createClient, type OASClient, type NormalizeOAS } from 'fets';
3
+ import { z } from 'zod';
4
+
5
+ // @ts-ignore
6
+ import OpenaiLogo from './assets/openai.png';
7
+ import { openapi } from './openapi';
8
+ import { components } from './openapi-components';
9
+ import { paths } from './openapi-paths';
10
+
11
+ export class OpenaiIntegration extends Integration {
12
+ constructor() {
13
+ super({
14
+ authType: IntegrationCredentialType.API_KEY,
15
+ name: 'OPENAI',
16
+ logoUrl: OpenaiLogo,
17
+ authConnectionOptions: z.object({
18
+ API_KEY: z.string(),
19
+ }),
20
+ });
21
+ }
22
+
23
+ getOpenApiSpec() {
24
+ return { paths, components } as unknown as OpenAPI;
25
+ }
26
+
27
+ getApiClient = async ({ connectionId }: { connectionId: string }): Promise<OASClient<NormalizeOAS<openapi>>> => {
28
+ const connection = await this.dataLayer?.getConnection({ name: this.name, connectionId });
29
+
30
+ if (!connection) {
31
+ throw new Error(`Connection not found for connectionId: ${connectionId}`);
32
+ }
33
+
34
+ const credential = await this.dataLayer?.getCredentialsByConnection(connection.id);
35
+ const value = credential?.value as Record<string, string>;
36
+
37
+ const client = createClient<NormalizeOAS<openapi>>({
38
+ endpoint: `https://api.openai.com/v1`,
39
+ globalParams: {
40
+ headers: {
41
+ Authorization: `Bearer ${value?.['API_KEY']}`,
42
+ },
43
+ },
44
+ });
45
+
46
+ return client as any;
47
+ };
48
+
49
+ registerEvents() {
50
+ this.events = {};
51
+ return this.events;
52
+ }
53
+
54
+ getAuthenticator() {
55
+ return new IntegrationAuth({
56
+ dataAccess: this.dataLayer!,
57
+ // @ts-ignore
58
+ onConnectionCreated: () => {
59
+ // TODO
60
+ },
61
+ config: {
62
+ INTEGRATION_NAME: this.name,
63
+ AUTH_TYPE: this.config.authType,
64
+ },
65
+ });
66
+ }
67
+ }