@super_studio/ecforce-ai-agent-server 0.1.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/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +471 -0
- package/dist/index.mjs +471 -0
- package/dist/lib/constants.d.ts +2 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/sdk/__generated__/index.d.ts +536 -0
- package/dist/sdk/__generated__/index.d.ts.map +1 -0
- package/dist/sdk/generate.d.ts +2 -0
- package/dist/sdk/generate.d.ts.map +1 -0
- package/dist/sdk/index.d.ts +18 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/package.json +36 -0
- package/src/.tmp/test-script.ts +18 -0
- package/src/index.ts +1 -0
- package/src/lib/constants.ts +1 -0
- package/src/sdk/__generated__/index.ts +982 -0
- package/src/sdk/generate.ts +66 -0
- package/src/sdk/index.ts +46 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { generateApi } from "swagger-typescript-api";
|
|
4
|
+
import { API_ENDPOINT } from "@/lib/constants";
|
|
5
|
+
|
|
6
|
+
const SCRIPT_DIR = path.dirname(new URL(import.meta.url).pathname);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* こちらのスクリプトは以下のコマンドで実行してください
|
|
10
|
+
*
|
|
11
|
+
* `pnpm --filter web ma:sdk:generate`
|
|
12
|
+
*/
|
|
13
|
+
void (async () => {
|
|
14
|
+
await updateApiSchema();
|
|
15
|
+
|
|
16
|
+
await generateApi({
|
|
17
|
+
fileName: "index.ts",
|
|
18
|
+
input: path.resolve(SCRIPT_DIR, "./schema/openapi.json"),
|
|
19
|
+
output: path.resolve(SCRIPT_DIR, "./__generated__"),
|
|
20
|
+
moduleNameIndex: 1,
|
|
21
|
+
unwrapResponseData: true,
|
|
22
|
+
hooks: {
|
|
23
|
+
onFormatRouteName(routeInfo) {
|
|
24
|
+
const { method, route, summary } = routeInfo;
|
|
25
|
+
|
|
26
|
+
const isValidSummary = !!summary && /^[a-zA-Z]+$/.test(summary);
|
|
27
|
+
|
|
28
|
+
const methodName = isValidSummary
|
|
29
|
+
? summary
|
|
30
|
+
: getFallbackMethodName(method, route);
|
|
31
|
+
|
|
32
|
+
return methodName;
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
})();
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* `get: /cx/app-test/prisma-test` -> `getPrismaTest`
|
|
40
|
+
*/
|
|
41
|
+
function getFallbackMethodName(method: string, route: string) {
|
|
42
|
+
const [, , , ...pathParts] = route.split("/");
|
|
43
|
+
const methodName = pathParts
|
|
44
|
+
.map((part) => {
|
|
45
|
+
// ハイフンを削除してキャメルケースに変換
|
|
46
|
+
return part
|
|
47
|
+
.split("-")
|
|
48
|
+
.map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1))
|
|
49
|
+
.join("");
|
|
50
|
+
})
|
|
51
|
+
.join("");
|
|
52
|
+
return `${method}${methodName}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function updateApiSchema() {
|
|
56
|
+
// スキーマを取得
|
|
57
|
+
const res = await fetch(`${API_ENDPOINT}/api/openapi.json`);
|
|
58
|
+
const json = (await res.json()) as unknown;
|
|
59
|
+
// フォルダが存在しない場合は作成
|
|
60
|
+
fs.mkdirSync(path.resolve(SCRIPT_DIR, "./schema"), { recursive: true });
|
|
61
|
+
// スキーマを保存
|
|
62
|
+
fs.writeFileSync(
|
|
63
|
+
path.resolve(SCRIPT_DIR, "./schema/openapi.json"),
|
|
64
|
+
JSON.stringify(json, null, 2),
|
|
65
|
+
);
|
|
66
|
+
}
|
package/src/sdk/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { API_ENDPOINT } from "@/lib/constants";
|
|
2
|
+
import { Api } from "./__generated__";
|
|
3
|
+
|
|
4
|
+
export * from "./__generated__";
|
|
5
|
+
|
|
6
|
+
export type CreateClientParams = {
|
|
7
|
+
/**
|
|
8
|
+
* 管理画面から作られたAPIキー。
|
|
9
|
+
* `AI_AGENT_API_KEY`の環境変数からも設定可能。
|
|
10
|
+
*/
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
/**
|
|
13
|
+
* エージェントのAPIエンドポイント。
|
|
14
|
+
* `AI_AGENT_API_ENDPOINT`の環境変数からも設定可能。
|
|
15
|
+
* デフォルトは本番のAPIエンドポイント。
|
|
16
|
+
* 基本的には変更不要。
|
|
17
|
+
*/
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function createClient(params?: CreateClientParams) {
|
|
22
|
+
const baseUrl =
|
|
23
|
+
params?.baseUrl ?? process.env.AI_AGENT_API_ENDPOINT ?? API_ENDPOINT;
|
|
24
|
+
const apiKey = params?.apiKey ?? process.env.AI_AGENT_API_KEY;
|
|
25
|
+
|
|
26
|
+
if (!apiKey) {
|
|
27
|
+
throw new Error("APIキーが設定されていません。");
|
|
28
|
+
}
|
|
29
|
+
if (!baseUrl) {
|
|
30
|
+
throw new Error("APIエンドポイントが設定されていません。");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return new Api({
|
|
34
|
+
baseUrl,
|
|
35
|
+
baseApiParams: {
|
|
36
|
+
secure: true,
|
|
37
|
+
},
|
|
38
|
+
async securityWorker() {
|
|
39
|
+
return {
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Bearer ${apiKey}`,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|