@spec2tools/sdk 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/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/lib.d.ts +29 -0
- package/dist/lib.js +97 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @spec2tools/sdk
|
|
2
|
+
|
|
3
|
+
Create AI SDK tools from OpenAPI specifications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @spec2tools/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createTools } from '@spec2tools/sdk';
|
|
15
|
+
import { generateText, stepCountIs } from 'ai';
|
|
16
|
+
import { openai } from '@ai-sdk/openai';
|
|
17
|
+
|
|
18
|
+
const tools = await createTools({ spec: './openapi.yaml' });
|
|
19
|
+
|
|
20
|
+
const result = await generateText({
|
|
21
|
+
model: openai('gpt-4o'),
|
|
22
|
+
tools,
|
|
23
|
+
prompt: 'List all users',
|
|
24
|
+
stopWhen: stepCountIs(3)
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
console.log(result.text);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
### `createTools(options: Spec2ToolsOptions): Promise<ToolSet>`
|
|
33
|
+
|
|
34
|
+
Creates AI SDK-compatible tools from an OpenAPI specification.
|
|
35
|
+
|
|
36
|
+
#### Options
|
|
37
|
+
|
|
38
|
+
- `spec` (string, required): Path or URL to the OpenAPI specification file (JSON or YAML)
|
|
39
|
+
|
|
40
|
+
#### Returns
|
|
41
|
+
|
|
42
|
+
A `Promise` that resolves to an object of AI SDK tools.
|
|
43
|
+
|
|
44
|
+
#### Throws
|
|
45
|
+
|
|
46
|
+
- `Error` if the API requires authentication. For authenticated APIs, use the `@spec2tools/cli` package instead.
|
|
47
|
+
|
|
48
|
+
## Notes
|
|
49
|
+
|
|
50
|
+
- `createTools()` only works with APIs that don't require authentication
|
|
51
|
+
- For authenticated APIs, use the CLI: `npx @spec2tools/cli start --spec ./openapi.yaml`
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { generateText } from 'ai';
|
|
2
|
+
type ToolSet = NonNullable<Parameters<typeof generateText>[0]['tools']>;
|
|
3
|
+
export interface Spec2ToolsOptions {
|
|
4
|
+
/** Path or URL to OpenAPI specification */
|
|
5
|
+
spec: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Create AI SDK tools from an OpenAPI specification.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { createTools } from '@spec2tools/sdk';
|
|
13
|
+
* import { generateText } from 'ai';
|
|
14
|
+
* import { openai } from '@ai-sdk/openai';
|
|
15
|
+
*
|
|
16
|
+
* const tools = await createTools({ spec: './openapi.yaml' });
|
|
17
|
+
*
|
|
18
|
+
* const result = await generateText({
|
|
19
|
+
* model: openai('gpt-4o'),
|
|
20
|
+
* tools,
|
|
21
|
+
* prompt: 'List all users',
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @throws Error if the API requires authentication
|
|
26
|
+
*/
|
|
27
|
+
export declare function createTools(options: Spec2ToolsOptions): Promise<ToolSet>;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=lib.d.ts.map
|
package/dist/lib.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { tool } from 'ai';
|
|
2
|
+
import { loadOpenAPISpec, extractBaseUrl, extractAuthConfig, parseOperations, AuthManager, ToolExecutionError, } from '@spec2tools/core';
|
|
3
|
+
/**
|
|
4
|
+
* Create AI SDK tools from an OpenAPI specification.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { createTools } from '@spec2tools/sdk';
|
|
9
|
+
* import { generateText } from 'ai';
|
|
10
|
+
* import { openai } from '@ai-sdk/openai';
|
|
11
|
+
*
|
|
12
|
+
* const tools = await createTools({ spec: './openapi.yaml' });
|
|
13
|
+
*
|
|
14
|
+
* const result = await generateText({
|
|
15
|
+
* model: openai('gpt-4o'),
|
|
16
|
+
* tools,
|
|
17
|
+
* prompt: 'List all users',
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @throws Error if the API requires authentication
|
|
22
|
+
*/
|
|
23
|
+
export async function createTools(options) {
|
|
24
|
+
const spec = await loadOpenAPISpec(options.spec);
|
|
25
|
+
const baseUrl = extractBaseUrl(spec);
|
|
26
|
+
const authConfig = extractAuthConfig(spec);
|
|
27
|
+
// Check if auth is required
|
|
28
|
+
if (authConfig.type !== 'none') {
|
|
29
|
+
throw new Error(`This API requires authentication (${authConfig.type}). ` +
|
|
30
|
+
`The createTools() function only supports APIs without authentication. ` +
|
|
31
|
+
`Use the CLI for authenticated APIs: npx @spec2tools/cli start --spec ${options.spec}`);
|
|
32
|
+
}
|
|
33
|
+
const toolDefs = parseOperations(spec);
|
|
34
|
+
const authManager = new AuthManager(authConfig);
|
|
35
|
+
// Build AI SDK tools
|
|
36
|
+
const tools = {};
|
|
37
|
+
for (const toolDef of toolDefs) {
|
|
38
|
+
const { name, description, parameters, httpMethod, path } = toolDef;
|
|
39
|
+
tools[name] = tool({
|
|
40
|
+
description,
|
|
41
|
+
inputSchema: parameters,
|
|
42
|
+
execute: async (params) => {
|
|
43
|
+
// Build URL with path parameters
|
|
44
|
+
let url = `${baseUrl}${path}`;
|
|
45
|
+
const queryParams = {};
|
|
46
|
+
const bodyParams = {};
|
|
47
|
+
for (const [key, value] of Object.entries(params)) {
|
|
48
|
+
if (value === undefined)
|
|
49
|
+
continue;
|
|
50
|
+
if (url.includes(`{${key}}`)) {
|
|
51
|
+
// Path parameter
|
|
52
|
+
url = url.replace(`{${key}}`, encodeURIComponent(String(value)));
|
|
53
|
+
}
|
|
54
|
+
else if (httpMethod === 'GET' || httpMethod === 'DELETE') {
|
|
55
|
+
// Query parameter for GET/DELETE
|
|
56
|
+
queryParams[key] = String(value);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// Body parameter for POST/PUT/PATCH
|
|
60
|
+
bodyParams[key] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Add query parameters
|
|
64
|
+
const queryString = new URLSearchParams(queryParams).toString();
|
|
65
|
+
if (queryString) {
|
|
66
|
+
url += `?${queryString}`;
|
|
67
|
+
}
|
|
68
|
+
// Build request options
|
|
69
|
+
const fetchOptions = {
|
|
70
|
+
method: httpMethod,
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'application/json',
|
|
73
|
+
...authManager.getAuthHeaders(),
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
// Add body for non-GET/DELETE requests
|
|
77
|
+
if (Object.keys(bodyParams).length > 0 &&
|
|
78
|
+
httpMethod !== 'GET' &&
|
|
79
|
+
httpMethod !== 'DELETE') {
|
|
80
|
+
fetchOptions.body = JSON.stringify(bodyParams);
|
|
81
|
+
}
|
|
82
|
+
const response = await fetch(url, fetchOptions);
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
const errorText = await response.text();
|
|
85
|
+
throw new ToolExecutionError(name, new Error(`HTTP ${response.status}: ${errorText}`));
|
|
86
|
+
}
|
|
87
|
+
const contentType = response.headers.get('content-type');
|
|
88
|
+
if (contentType?.includes('application/json')) {
|
|
89
|
+
return await response.json();
|
|
90
|
+
}
|
|
91
|
+
return await response.text();
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return tools;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=lib.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spec2tools/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Create AI SDK tools from OpenAPI specifications",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/**/*.js",
|
|
16
|
+
"dist/**/*.d.ts"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"openapi",
|
|
20
|
+
"ai",
|
|
21
|
+
"agent",
|
|
22
|
+
"tools",
|
|
23
|
+
"ai-sdk",
|
|
24
|
+
"vercel-ai"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/CahidArda/spec2tools.git",
|
|
30
|
+
"directory": "packages/sdk"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"ai": "^6.0.68",
|
|
34
|
+
"zod": "^3.24.0",
|
|
35
|
+
"@spec2tools/core": "0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"zod": "^3.24.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.0.0",
|
|
42
|
+
"typescript": "^5.6.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc",
|
|
49
|
+
"dev": "tsc --watch",
|
|
50
|
+
"typecheck": "tsc --noEmit"
|
|
51
|
+
}
|
|
52
|
+
}
|