specli 0.0.19 → 0.0.20
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/README.md +3 -1
- package/dist/ai/tools.d.ts +6 -5
- package/dist/ai/tools.js +7 -20
- package/dist/ai/tools.test.js +7 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -321,7 +321,7 @@ import { generateText } from "ai";
|
|
|
321
321
|
const result = await generateText({
|
|
322
322
|
model: yourModel,
|
|
323
323
|
tools: {
|
|
324
|
-
api: specli({
|
|
324
|
+
api: await specli({
|
|
325
325
|
spec: "https://api.example.com/openapi.json",
|
|
326
326
|
bearerToken: process.env.API_TOKEN,
|
|
327
327
|
}),
|
|
@@ -330,6 +330,8 @@ const result = await generateText({
|
|
|
330
330
|
});
|
|
331
331
|
```
|
|
332
332
|
|
|
333
|
+
The `specli()` function is async and fetches the OpenAPI spec upfront, so the returned tool is ready to use immediately without any additional network requests.
|
|
334
|
+
|
|
333
335
|
The tool supports three commands:
|
|
334
336
|
- `list` - Show available resources and actions
|
|
335
337
|
- `help` - Get details about a specific action
|
package/dist/ai/tools.d.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* const result = await generateText({
|
|
12
12
|
* model: yourModel,
|
|
13
13
|
* tools: {
|
|
14
|
-
* api: specli({ spec: "https://api.example.com/openapi.json" }),
|
|
14
|
+
* api: await specli({ spec: "https://api.example.com/openapi.json" }),
|
|
15
15
|
* },
|
|
16
16
|
* prompt: "List all users",
|
|
17
17
|
* });
|
|
@@ -38,8 +38,11 @@ export type SpecliToolOptions = {
|
|
|
38
38
|
};
|
|
39
39
|
/**
|
|
40
40
|
* Create an AI SDK tool for interacting with an OpenAPI spec.
|
|
41
|
+
*
|
|
42
|
+
* The spec is fetched once when this function is called, so the returned
|
|
43
|
+
* tool already has the spec loaded and ready to use.
|
|
41
44
|
*/
|
|
42
|
-
export declare function specli(options: SpecliToolOptions): import("ai").Tool<{
|
|
45
|
+
export declare function specli(options: SpecliToolOptions): Promise<import("ai").Tool<{
|
|
43
46
|
command: "list" | "exec" | "help";
|
|
44
47
|
resource?: string | undefined;
|
|
45
48
|
action?: string | undefined;
|
|
@@ -133,6 +136,4 @@ export declare function specli(options: SpecliToolOptions): import("ai").Tool<{
|
|
|
133
136
|
summary?: undefined;
|
|
134
137
|
args?: undefined;
|
|
135
138
|
flags?: undefined;
|
|
136
|
-
}
|
|
137
|
-
/** Clear cached spec context */
|
|
138
|
-
export declare function clearSpecliCache(spec?: string): void;
|
|
139
|
+
}>>;
|
package/dist/ai/tools.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* const result = await generateText({
|
|
12
12
|
* model: yourModel,
|
|
13
13
|
* tools: {
|
|
14
|
-
* api: specli({ spec: "https://api.example.com/openapi.json" }),
|
|
14
|
+
* api: await specli({ spec: "https://api.example.com/openapi.json" }),
|
|
15
15
|
* },
|
|
16
16
|
* prompt: "List all users",
|
|
17
17
|
* });
|
|
@@ -21,25 +21,20 @@ import { tool } from "ai";
|
|
|
21
21
|
import { z } from "zod";
|
|
22
22
|
import { buildRuntimeContext } from "../cli/runtime/context.js";
|
|
23
23
|
import { execute } from "../cli/runtime/execute.js";
|
|
24
|
-
// Cache contexts to avoid reloading spec on every call
|
|
25
|
-
const contextCache = new Map();
|
|
26
|
-
async function getContext(spec) {
|
|
27
|
-
let ctx = contextCache.get(spec);
|
|
28
|
-
if (!ctx) {
|
|
29
|
-
ctx = await buildRuntimeContext({ spec });
|
|
30
|
-
contextCache.set(spec, ctx);
|
|
31
|
-
}
|
|
32
|
-
return ctx;
|
|
33
|
-
}
|
|
34
24
|
function findAction(ctx, resource, action) {
|
|
35
25
|
const r = ctx.commands.resources.find((r) => r.resource.toLowerCase() === resource.toLowerCase());
|
|
36
26
|
return r?.actions.find((a) => a.action.toLowerCase() === action.toLowerCase());
|
|
37
27
|
}
|
|
38
28
|
/**
|
|
39
29
|
* Create an AI SDK tool for interacting with an OpenAPI spec.
|
|
30
|
+
*
|
|
31
|
+
* The spec is fetched once when this function is called, so the returned
|
|
32
|
+
* tool already has the spec loaded and ready to use.
|
|
40
33
|
*/
|
|
41
|
-
export function specli(options) {
|
|
34
|
+
export async function specli(options) {
|
|
42
35
|
const { spec, server, serverVars, bearerToken, apiKey, basicAuth, authScheme, } = options;
|
|
36
|
+
// Fetch and parse the spec upfront
|
|
37
|
+
const ctx = await buildRuntimeContext({ spec });
|
|
43
38
|
return tool({
|
|
44
39
|
description: `Execute API operations. Commands: "list" (show resources/actions), "help" (action details), "exec" (call API).`,
|
|
45
40
|
inputSchema: z.object({
|
|
@@ -56,7 +51,6 @@ export function specli(options) {
|
|
|
56
51
|
.describe("Named flags"),
|
|
57
52
|
}),
|
|
58
53
|
execute: async ({ command, resource, action, args, flags }) => {
|
|
59
|
-
const ctx = await getContext(spec);
|
|
60
54
|
if (command === "list") {
|
|
61
55
|
return {
|
|
62
56
|
resources: ctx.commands.resources.map((r) => ({
|
|
@@ -152,10 +146,3 @@ export function specli(options) {
|
|
|
152
146
|
},
|
|
153
147
|
});
|
|
154
148
|
}
|
|
155
|
-
/** Clear cached spec context */
|
|
156
|
-
export function clearSpecliCache(spec) {
|
|
157
|
-
if (spec)
|
|
158
|
-
contextCache.delete(spec);
|
|
159
|
-
else
|
|
160
|
-
contextCache.clear();
|
|
161
|
-
}
|
package/dist/ai/tools.test.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import { specli } from "./tools.js";
|
|
3
3
|
const mockOptions = {
|
|
4
4
|
toolCallId: "test-call-id",
|
|
5
5
|
abortSignal: new AbortController().signal,
|
|
6
6
|
messages: [],
|
|
7
7
|
};
|
|
8
8
|
describe("specli tool", () => {
|
|
9
|
-
test("creates a tool with correct structure", () => {
|
|
10
|
-
const tool = specli({
|
|
9
|
+
test("creates a tool with correct structure", async () => {
|
|
10
|
+
const tool = await specli({
|
|
11
11
|
spec: "https://petstore3.swagger.io/api/v3/openapi.json",
|
|
12
12
|
});
|
|
13
13
|
expect(tool).toHaveProperty("description");
|
|
@@ -16,7 +16,7 @@ describe("specli tool", () => {
|
|
|
16
16
|
expect(typeof tool.execute).toBe("function");
|
|
17
17
|
});
|
|
18
18
|
test("list command returns resources", async () => {
|
|
19
|
-
const tool = specli({
|
|
19
|
+
const tool = await specli({
|
|
20
20
|
spec: "https://petstore3.swagger.io/api/v3/openapi.json",
|
|
21
21
|
});
|
|
22
22
|
const result = (await tool.execute?.({ command: "list" }, mockOptions));
|
|
@@ -24,7 +24,7 @@ describe("specli tool", () => {
|
|
|
24
24
|
expect(Array.isArray(result.resources)).toBe(true);
|
|
25
25
|
});
|
|
26
26
|
test("help command returns action details", async () => {
|
|
27
|
-
const tool = specli({
|
|
27
|
+
const tool = await specli({
|
|
28
28
|
spec: "https://petstore3.swagger.io/api/v3/openapi.json",
|
|
29
29
|
});
|
|
30
30
|
const result = (await tool.execute?.({ command: "help", resource: "pets", action: "get" }, mockOptions));
|
|
@@ -32,25 +32,18 @@ describe("specli tool", () => {
|
|
|
32
32
|
expect(result.action).toBe("get");
|
|
33
33
|
});
|
|
34
34
|
test("help command with missing resource returns error", async () => {
|
|
35
|
-
const tool = specli({
|
|
35
|
+
const tool = await specli({
|
|
36
36
|
spec: "https://petstore3.swagger.io/api/v3/openapi.json",
|
|
37
37
|
});
|
|
38
38
|
const result = (await tool.execute?.({ command: "help" }, mockOptions));
|
|
39
39
|
expect(result).toHaveProperty("error");
|
|
40
40
|
});
|
|
41
41
|
test("exec command with missing args returns error", async () => {
|
|
42
|
-
const tool = specli({
|
|
42
|
+
const tool = await specli({
|
|
43
43
|
spec: "https://petstore3.swagger.io/api/v3/openapi.json",
|
|
44
44
|
});
|
|
45
45
|
const result = (await tool.execute?.({ command: "exec", resource: "pets", action: "get" }, mockOptions));
|
|
46
46
|
expect(result).toHaveProperty("error");
|
|
47
47
|
expect(result.error).toContain("Missing args");
|
|
48
48
|
});
|
|
49
|
-
test("clearCache works", async () => {
|
|
50
|
-
const spec = "https://petstore3.swagger.io/api/v3/openapi.json";
|
|
51
|
-
const tool = specli({ spec });
|
|
52
|
-
await tool.execute?.({ command: "list" }, mockOptions);
|
|
53
|
-
clearSpecliCache(spec);
|
|
54
|
-
clearSpecliCache();
|
|
55
|
-
});
|
|
56
49
|
});
|