Skip to content

Commit 0cfa66d

Browse files
committed
feat: openai datasource
1 parent 169520f commit 0cfa66d

File tree

11 files changed

+5952
-7
lines changed

11 files changed

+5952
-7
lines changed

server/node-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"express": "^4.18.2",
3737
"express-async-errors": "^3.1.1",
3838
"firebase-admin": "^11.5.0",
39+
"formdata-node": "4",
3940
"jsonpath": "^1.1.1",
4041
"lodash": "^4.17.21",
4142
"loglevel": "^1.8.1",

server/node-service/src/common/util.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ export function toJsonValue(value: any): any {
5252
return {};
5353
}
5454
}
55+
56+
export function toStringOrJson(value: any): any {
57+
if (typeof value !== "string") {
58+
return value;
59+
}
60+
try {
61+
const json = relaxedJSONToJSON(value, true);
62+
return JSON.parse(json);
63+
} catch (e) {
64+
if (typeof value === "string") {
65+
return value;
66+
}
67+
console.info("invalid json input:", value);
68+
return {};
69+
}
70+
}

server/node-service/src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dynamoDBPlugin from "./dynamodb";
77
import firebasePlugin from "./firebase";
88
import couchdbPlugin from "./couchdb";
99
import woocommercePlugin from "./woocommerce";
10+
import openAiPlugin from "./openAi";
1011

1112
const plugins: (DataSourcePlugin | DataSourcePluginFactory)[] = [
1213
// helloWorldPlugin,
@@ -17,6 +18,7 @@ const plugins: (DataSourcePlugin | DataSourcePluginFactory)[] = [
1718
firebasePlugin,
1819
couchdbPlugin,
1920
woocommercePlugin,
21+
openAiPlugin,
2022
];
2123

2224
export default plugins;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import _ from "lodash";
2+
import { OpenAPIV3, OpenAPI } from "openapi-types";
3+
import { ConfigToType, DataSourcePlugin } from "openblocks-sdk/dataSource";
4+
import { runOpenApi } from "../openApi";
5+
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
6+
import spec from "./openai-spec.json";
7+
8+
const dataSourceConfig = {
9+
type: "dataSource",
10+
params: [
11+
{
12+
key: "ApiKey.value",
13+
type: "password",
14+
label: "API Key",
15+
rules: [{ required: true }],
16+
placeholder: "<Your API KEY>",
17+
tooltip:
18+
"[Document](https://platform.openai.com/account/api-keys) on which you can create your api key",
19+
},
20+
],
21+
} as const;
22+
23+
const parseOptions: ParseOpenApiOptions = {
24+
actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => {
25+
return _.upperFirst(operation.operationId || "");
26+
},
27+
};
28+
29+
type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>;
30+
31+
const openAiPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
32+
id: "OpenAI",
33+
name: "OpenAI",
34+
icon: "openAI.svg",
35+
category: "api",
36+
dataSourceConfig,
37+
queryConfig: async () => {
38+
const { actions } = await parseOpenApi(spec, parseOptions);
39+
return {
40+
type: "query",
41+
label: "Action",
42+
actions,
43+
};
44+
},
45+
run: function (actionData, dataSourceConfig): Promise<any> {
46+
const runApiDsConfig = {
47+
url: "",
48+
serverURL: "",
49+
dynamicParamsConfig: dataSourceConfig,
50+
};
51+
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document);
52+
},
53+
};
54+
55+
export default openAiPlugin;

0 commit comments

Comments
 (0)