@agent-native/actions 0.0.0-stage → 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/README.md +41 -2
- package/dist/chunk-4X4LYULO.js +108 -0
- package/dist/chunk-4X4LYULO.js.map +1 -0
- package/dist/chunk-D66CG6AI.js +265 -0
- package/dist/chunk-D66CG6AI.js.map +1 -0
- package/dist/chunk-KWA4U6DI.js +273 -0
- package/dist/chunk-KWA4U6DI.js.map +1 -0
- package/dist/chunk-SXOEFWAM.js +147 -0
- package/dist/chunk-SXOEFWAM.js.map +1 -0
- package/dist/cli.js +1765 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-C87WZhlZ.d.ts +223 -0
- package/dist/client.d.ts +70 -0
- package/dist/client.js +110 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +251 -0
- package/dist/contract.js +372 -0
- package/dist/contract.js.map +1 -0
- package/dist/index.d.ts +251 -0
- package/dist/index.js +704 -0
- package/dist/index.js.map +1 -0
- package/dist/next-config.d.ts +16 -0
- package/dist/next-config.js +46 -0
- package/dist/next-config.js.map +1 -0
- package/dist/next-pages.d.ts +12 -0
- package/dist/next-pages.js +13 -0
- package/dist/next-pages.js.map +1 -0
- package/dist/next-server.d.ts +41 -0
- package/dist/next-server.js +96 -0
- package/dist/next-server.js.map +1 -0
- package/dist/next.d.ts +39 -0
- package/dist/next.js +68 -0
- package/dist/next.js.map +1 -0
- package/dist/nuxt.d.ts +17 -0
- package/dist/nuxt.js +22 -0
- package/dist/nuxt.js.map +1 -0
- package/dist/react.d.ts +50 -0
- package/dist/react.js +194 -0
- package/dist/react.js.map +1 -0
- package/dist/registry-CIe_aYoE.d.ts +263 -0
- package/dist/solid.d.ts +39 -0
- package/dist/solid.js +229 -0
- package/dist/solid.js.map +1 -0
- package/dist/svelte-server.d.ts +23 -0
- package/dist/svelte-server.js +33 -0
- package/dist/svelte-server.js.map +1 -0
- package/dist/svelte.d.ts +53 -0
- package/dist/svelte.js +244 -0
- package/dist/svelte.js.map +1 -0
- package/dist/vite.d.ts +30 -0
- package/dist/vite.js +131 -0
- package/dist/vite.js.map +1 -0
- package/dist/vue-server.d.ts +20 -0
- package/dist/vue-server.js +26 -0
- package/dist/vue-server.js.map +1 -0
- package/dist/vue.d.ts +64 -0
- package/dist/vue.js +250 -0
- package/dist/vue.js.map +1 -0
- package/dist/webmcp.d.ts +19 -0
- package/dist/webmcp.js +36 -0
- package/dist/webmcp.js.map +1 -0
- package/package.json +172 -4
- package/skills/agent-native-actions/SKILL.md +76 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createActionFetchHandler,
|
|
3
|
+
stripBasePath,
|
|
4
|
+
toNodeHandler
|
|
5
|
+
} from "./chunk-D66CG6AI.js";
|
|
6
|
+
import {
|
|
7
|
+
createActionBuilder,
|
|
8
|
+
defineAction,
|
|
9
|
+
executeAction,
|
|
10
|
+
isActionReadOnly,
|
|
11
|
+
isDefinedAction,
|
|
12
|
+
normalizeLlmToolInput,
|
|
13
|
+
resolveActionHttpMethod,
|
|
14
|
+
resolveActionHttpPath,
|
|
15
|
+
schemaToJsonSchema,
|
|
16
|
+
validateInputSchema,
|
|
17
|
+
validateOutputSchema
|
|
18
|
+
} from "./chunk-KWA4U6DI.js";
|
|
19
|
+
import {
|
|
20
|
+
ActionError,
|
|
21
|
+
ActionPostCommitError,
|
|
22
|
+
defineActionErrors,
|
|
23
|
+
fail,
|
|
24
|
+
isActionError,
|
|
25
|
+
isActionPostCommitError,
|
|
26
|
+
serializeActionErrorBody
|
|
27
|
+
} from "./chunk-4X4LYULO.js";
|
|
28
|
+
import {
|
|
29
|
+
generateActionArtifacts,
|
|
30
|
+
scanActionsDirectory
|
|
31
|
+
} from "./chunk-SXOEFWAM.js";
|
|
32
|
+
|
|
33
|
+
// src/ai-sdk.ts
|
|
34
|
+
function selectAgentActions(registry, options) {
|
|
35
|
+
return Object.entries(registry.actions).filter(([name, action]) => {
|
|
36
|
+
if (action.agentTool === false) return false;
|
|
37
|
+
if (options.only && !options.only.includes(name)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (options.exclude && options.exclude.includes(name)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function requireToolJsonSchema(name, schema, explicitOverride) {
|
|
47
|
+
const converted = schemaToJsonSchema(schema, "input", explicitOverride);
|
|
48
|
+
if (!converted) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Action "${name}" uses a schema without Standard JSON Schema support. Provide a Zod v4 / Standard JSON Schema validator or set \`toolSchema\` on the action.`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return converted;
|
|
54
|
+
}
|
|
55
|
+
function toAiSdkTools(registry, options) {
|
|
56
|
+
const tools = {};
|
|
57
|
+
for (const [name, action] of selectAgentActions(registry, options)) {
|
|
58
|
+
const schemaOrJson = action.schema ?? requireToolJsonSchema(name, void 0, action.toolSchema);
|
|
59
|
+
tools[name] = {
|
|
60
|
+
description: action.description,
|
|
61
|
+
inputSchema: schemaOrJson,
|
|
62
|
+
parameters: schemaOrJson,
|
|
63
|
+
execute: async (args) => registry.invokeWithContext(name, args ?? {}, options.context, {
|
|
64
|
+
caller: "agent",
|
|
65
|
+
...options.approvalToken !== void 0 ? { approvalToken: options.approvalToken } : {}
|
|
66
|
+
})
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
return tools;
|
|
70
|
+
}
|
|
71
|
+
function toAnthropicTools(registry, options) {
|
|
72
|
+
const selected = selectAgentActions(registry, options);
|
|
73
|
+
const allowed = new Set(selected.map(([name]) => name));
|
|
74
|
+
const tools = selected.map(([name, action]) => ({
|
|
75
|
+
name,
|
|
76
|
+
description: action.description,
|
|
77
|
+
input_schema: requireToolJsonSchema(name, action.schema, action.toolSchema)
|
|
78
|
+
}));
|
|
79
|
+
return {
|
|
80
|
+
tools,
|
|
81
|
+
executeToolUse: async (toolUse) => {
|
|
82
|
+
if (!allowed.has(toolUse.name)) {
|
|
83
|
+
return {
|
|
84
|
+
type: "tool_result",
|
|
85
|
+
tool_use_id: toolUse.id,
|
|
86
|
+
is_error: true,
|
|
87
|
+
content: JSON.stringify({
|
|
88
|
+
error: `Tool "${toolUse.name}" is not enabled.`,
|
|
89
|
+
errorCode: "not_found"
|
|
90
|
+
})
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const result = await registry.invokeWithContext(
|
|
95
|
+
toolUse.name,
|
|
96
|
+
toolUse.input ?? {},
|
|
97
|
+
options.context,
|
|
98
|
+
{
|
|
99
|
+
caller: "agent",
|
|
100
|
+
...options.approvalToken !== void 0 ? { approvalToken: options.approvalToken } : {}
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
return {
|
|
104
|
+
type: "tool_result",
|
|
105
|
+
tool_use_id: toolUse.id,
|
|
106
|
+
content: JSON.stringify(result ?? null)
|
|
107
|
+
};
|
|
108
|
+
} catch (error) {
|
|
109
|
+
const { body } = serializeActionErrorBody(error);
|
|
110
|
+
return {
|
|
111
|
+
type: "tool_result",
|
|
112
|
+
tool_use_id: toolUse.id,
|
|
113
|
+
is_error: true,
|
|
114
|
+
content: JSON.stringify(body)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function toOpenAiTools(registry, options) {
|
|
121
|
+
const selected = selectAgentActions(registry, options);
|
|
122
|
+
const allowed = new Set(selected.map(([name]) => name));
|
|
123
|
+
const tools = selected.map(([name, action]) => ({
|
|
124
|
+
type: "function",
|
|
125
|
+
function: {
|
|
126
|
+
name,
|
|
127
|
+
description: action.description,
|
|
128
|
+
parameters: requireToolJsonSchema(name, action.schema, action.toolSchema)
|
|
129
|
+
}
|
|
130
|
+
}));
|
|
131
|
+
return {
|
|
132
|
+
tools,
|
|
133
|
+
executeToolCall: async (toolCall) => {
|
|
134
|
+
const name = toolCall.function.name;
|
|
135
|
+
if (!allowed.has(name)) {
|
|
136
|
+
return {
|
|
137
|
+
role: "tool",
|
|
138
|
+
tool_call_id: toolCall.id,
|
|
139
|
+
content: JSON.stringify({
|
|
140
|
+
error: `Tool "${name}" is not enabled.`,
|
|
141
|
+
errorCode: "not_found"
|
|
142
|
+
})
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const parsedArgs = toolCall.function.arguments.trim() ? JSON.parse(toolCall.function.arguments) : {};
|
|
147
|
+
const result = await registry.invokeWithContext(
|
|
148
|
+
name,
|
|
149
|
+
parsedArgs,
|
|
150
|
+
options.context,
|
|
151
|
+
{
|
|
152
|
+
caller: "agent",
|
|
153
|
+
...options.approvalToken !== void 0 ? { approvalToken: options.approvalToken } : {}
|
|
154
|
+
}
|
|
155
|
+
);
|
|
156
|
+
return {
|
|
157
|
+
role: "tool",
|
|
158
|
+
tool_call_id: toolCall.id,
|
|
159
|
+
content: JSON.stringify(result ?? null)
|
|
160
|
+
};
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const { body } = serializeActionErrorBody(error);
|
|
163
|
+
return {
|
|
164
|
+
role: "tool",
|
|
165
|
+
tool_call_id: toolCall.id,
|
|
166
|
+
content: JSON.stringify(body)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/mcp.ts
|
|
174
|
+
import {
|
|
175
|
+
Server,
|
|
176
|
+
createMcpHandler
|
|
177
|
+
} from "@modelcontextprotocol/server";
|
|
178
|
+
function isPlainRecord(value) {
|
|
179
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
180
|
+
}
|
|
181
|
+
function formatMcpErrorText(error) {
|
|
182
|
+
const { body } = serializeActionErrorBody(error);
|
|
183
|
+
const parts = [body.error];
|
|
184
|
+
if (body.errorCode && body.errorCode !== "action_failed") {
|
|
185
|
+
parts.push(`(errorCode: ${body.errorCode})`);
|
|
186
|
+
}
|
|
187
|
+
if (body.committed) {
|
|
188
|
+
parts.push("[committed: true \u2014 do not blindly retry]");
|
|
189
|
+
}
|
|
190
|
+
if (body.details) {
|
|
191
|
+
const serialized = JSON.stringify(body.details);
|
|
192
|
+
parts.push(
|
|
193
|
+
`Error details: ${serialized.length > 2e3 ? `${serialized.slice(0, 1997)}...` : serialized}`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
return parts.join("\n");
|
|
197
|
+
}
|
|
198
|
+
function isExposedToMcp(name, action, options) {
|
|
199
|
+
if (action.agentTool === false || action.mcpTool === false) return false;
|
|
200
|
+
if (options.include && !options.include.includes(name)) return false;
|
|
201
|
+
if (options.exclude && options.exclude.includes(name)) return false;
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
function buildMcpServerForRequest(registry, options, invocation) {
|
|
205
|
+
const server = new Server(
|
|
206
|
+
{
|
|
207
|
+
name: options.name ?? "agent-native-actions",
|
|
208
|
+
version: options.version ?? "0.1.0"
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
capabilities: { tools: {} },
|
|
212
|
+
...options.instructions ? { instructions: options.instructions } : {}
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
server.setRequestHandler("tools/list", async () => {
|
|
216
|
+
const tools = [];
|
|
217
|
+
for (const [name, action] of Object.entries(registry.actions)) {
|
|
218
|
+
if (!isExposedToMcp(name, action, options)) continue;
|
|
219
|
+
const inputSchema = schemaToJsonSchema(
|
|
220
|
+
action.schema,
|
|
221
|
+
"input",
|
|
222
|
+
action.toolSchema
|
|
223
|
+
) ?? {
|
|
224
|
+
type: "object",
|
|
225
|
+
properties: {}
|
|
226
|
+
};
|
|
227
|
+
const outputSchema = action.mcpOutputSchema !== false && action.outputSchema ? schemaToJsonSchema(action.outputSchema, "output") : null;
|
|
228
|
+
const tool = {
|
|
229
|
+
name,
|
|
230
|
+
...action.title ? { title: action.title } : {},
|
|
231
|
+
description: action.description,
|
|
232
|
+
inputSchema,
|
|
233
|
+
...outputSchema && outputSchema["type"] === "object" ? { outputSchema } : {},
|
|
234
|
+
annotations: {
|
|
235
|
+
readOnlyHint: isActionReadOnly(action),
|
|
236
|
+
destructiveHint: Boolean(action.destructive),
|
|
237
|
+
idempotentHint: Boolean(action.retrySafe),
|
|
238
|
+
openWorldHint: false
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
tools.push(tool);
|
|
242
|
+
}
|
|
243
|
+
return { tools };
|
|
244
|
+
});
|
|
245
|
+
server.setRequestHandler("tools/call", async (request) => {
|
|
246
|
+
const toolName = request.params.name;
|
|
247
|
+
const args = request.params.arguments ?? {};
|
|
248
|
+
const action = registry.actions[toolName];
|
|
249
|
+
if (!action || !isExposedToMcp(toolName, action, options)) {
|
|
250
|
+
return {
|
|
251
|
+
isError: true,
|
|
252
|
+
content: [
|
|
253
|
+
{
|
|
254
|
+
type: "text",
|
|
255
|
+
text: `Action "${toolName}" is not exposed over MCP.
|
|
256
|
+
(errorCode: not_found)`
|
|
257
|
+
}
|
|
258
|
+
]
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
try {
|
|
262
|
+
const result = invocation.request ? await registry.invokeFromRequest(toolName, args, {
|
|
263
|
+
caller: "mcp",
|
|
264
|
+
request: invocation.request,
|
|
265
|
+
...invocation.platform !== void 0 ? { platform: invocation.platform } : {}
|
|
266
|
+
}) : await registry.invokeFromCliOrStdio(toolName, args, {
|
|
267
|
+
caller: "mcp"
|
|
268
|
+
});
|
|
269
|
+
const structuredContent = isPlainRecord(result) ? result : Array.isArray(result) ? { items: result } : void 0;
|
|
270
|
+
const text = typeof result === "string" ? result : JSON.stringify(result ?? null, null, 2);
|
|
271
|
+
return {
|
|
272
|
+
content: [{ type: "text", text }],
|
|
273
|
+
...structuredContent ? { structuredContent } : {}
|
|
274
|
+
};
|
|
275
|
+
} catch (error) {
|
|
276
|
+
const { body } = serializeActionErrorBody(error);
|
|
277
|
+
return {
|
|
278
|
+
isError: true,
|
|
279
|
+
content: [{ type: "text", text: formatMcpErrorText(error) }],
|
|
280
|
+
structuredContent: {
|
|
281
|
+
error: {
|
|
282
|
+
message: body.error,
|
|
283
|
+
errorCode: body.errorCode,
|
|
284
|
+
...body.committed ? { committed: true } : {},
|
|
285
|
+
...body.details ? { details: body.details } : {}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
return server;
|
|
292
|
+
}
|
|
293
|
+
function createMcpFetchHandler(registry, options = {}) {
|
|
294
|
+
return async (request, override) => {
|
|
295
|
+
if (options.authenticate) {
|
|
296
|
+
const allowed = await options.authenticate(request);
|
|
297
|
+
if (!allowed) {
|
|
298
|
+
return new Response(
|
|
299
|
+
JSON.stringify({
|
|
300
|
+
error: "Unauthorized MCP request.",
|
|
301
|
+
errorCode: "unauthorized"
|
|
302
|
+
}),
|
|
303
|
+
{
|
|
304
|
+
status: 401,
|
|
305
|
+
headers: {
|
|
306
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
307
|
+
"Cache-Control": "no-store"
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
const handler = createMcpHandler(
|
|
314
|
+
() => buildMcpServerForRequest(registry, options, {
|
|
315
|
+
request,
|
|
316
|
+
...override?.platform !== void 0 ? { platform: override.platform } : {}
|
|
317
|
+
}),
|
|
318
|
+
{
|
|
319
|
+
legacy: "stateless",
|
|
320
|
+
responseMode: "auto"
|
|
321
|
+
}
|
|
322
|
+
);
|
|
323
|
+
try {
|
|
324
|
+
return await handler.fetch(request);
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (isActionError(error)) {
|
|
327
|
+
const { statusCode, body } = serializeActionErrorBody(error);
|
|
328
|
+
return new Response(JSON.stringify(body), {
|
|
329
|
+
status: statusCode,
|
|
330
|
+
headers: {
|
|
331
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
332
|
+
"Cache-Control": "no-store"
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
throw error;
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
async function runStdioMcpServer(registry, options = {}) {
|
|
341
|
+
const { serveStdio } = await import("@modelcontextprotocol/server/stdio");
|
|
342
|
+
const stdio = serveStdio(
|
|
343
|
+
() => buildMcpServerForRequest(registry, options, {}),
|
|
344
|
+
{ legacy: "serve" }
|
|
345
|
+
);
|
|
346
|
+
await new Promise((resolve) => {
|
|
347
|
+
process.stdin.once("end", resolve);
|
|
348
|
+
process.once("SIGINT", resolve);
|
|
349
|
+
process.once("SIGTERM", resolve);
|
|
350
|
+
});
|
|
351
|
+
await stdio.close();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/registry.ts
|
|
355
|
+
function readRuntimeEnv() {
|
|
356
|
+
return typeof process !== "undefined" ? process.env : {};
|
|
357
|
+
}
|
|
358
|
+
function defaultContext(input) {
|
|
359
|
+
return {
|
|
360
|
+
caller: input.caller,
|
|
361
|
+
source: input.source,
|
|
362
|
+
headers: input.headers,
|
|
363
|
+
...input.request ? { request: input.request } : {},
|
|
364
|
+
...input.signal ? { signal: input.signal } : {},
|
|
365
|
+
...input.actionName ? { actionName: input.actionName } : {}
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
function createActions(options) {
|
|
369
|
+
const { actions } = options;
|
|
370
|
+
const httpRouteIndex = /* @__PURE__ */ new Map();
|
|
371
|
+
for (const [name, action] of Object.entries(actions)) {
|
|
372
|
+
const method = resolveActionHttpMethod(action);
|
|
373
|
+
const routePath = resolveActionHttpPath(name, action);
|
|
374
|
+
if (method && routePath) {
|
|
375
|
+
if (httpRouteIndex.has(routePath)) {
|
|
376
|
+
throw new Error(
|
|
377
|
+
`Duplicate HTTP action route "${routePath}" in createActions().`
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
httpRouteIndex.set(routePath, {
|
|
381
|
+
name,
|
|
382
|
+
method
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const resolveContext = async (input) => {
|
|
387
|
+
if (options.createContext) {
|
|
388
|
+
return options.createContext(input);
|
|
389
|
+
}
|
|
390
|
+
return defaultContext(input);
|
|
391
|
+
};
|
|
392
|
+
const invokeWithContext = async (name, rawInput, ctx, callOpts) => {
|
|
393
|
+
const action = actions[name];
|
|
394
|
+
if (!action) {
|
|
395
|
+
throw new ActionError(`Action "${name}" not found.`, {
|
|
396
|
+
statusCode: 404,
|
|
397
|
+
errorCode: "not_found"
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
return executeAction({
|
|
401
|
+
name,
|
|
402
|
+
action,
|
|
403
|
+
rawInput,
|
|
404
|
+
ctx,
|
|
405
|
+
caller: callOpts.caller,
|
|
406
|
+
...callOpts.approvalToken !== void 0 ? { approvalToken: callOpts.approvalToken } : {},
|
|
407
|
+
...callOpts.approved !== void 0 ? { approved: callOpts.approved } : {},
|
|
408
|
+
...options.verifyApproval ? { verifyApproval: options.verifyApproval } : {},
|
|
409
|
+
...options.middleware ? { middleware: options.middleware } : {},
|
|
410
|
+
...options.onAfterMutate ? { onAfterMutate: options.onAfterMutate } : {},
|
|
411
|
+
...options.onError ? { onError: options.onError } : {}
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
const invokeFromRequest = async (name, rawInput, reqOpts) => {
|
|
415
|
+
const ctx = await resolveContext({
|
|
416
|
+
caller: reqOpts.caller,
|
|
417
|
+
source: reqOpts.caller,
|
|
418
|
+
actionName: name,
|
|
419
|
+
request: reqOpts.request,
|
|
420
|
+
headers: reqOpts.request.headers,
|
|
421
|
+
signal: reqOpts.request.signal,
|
|
422
|
+
env: readRuntimeEnv(),
|
|
423
|
+
...reqOpts.platform !== void 0 ? { platform: reqOpts.platform } : {}
|
|
424
|
+
});
|
|
425
|
+
return invokeWithContext(name, rawInput, ctx, {
|
|
426
|
+
caller: reqOpts.caller,
|
|
427
|
+
...reqOpts.approvalToken !== void 0 ? { approvalToken: reqOpts.approvalToken } : {}
|
|
428
|
+
});
|
|
429
|
+
};
|
|
430
|
+
const invokeFromCliOrStdio = async (name, rawInput, cliOpts) => {
|
|
431
|
+
const ctx = await resolveContext({
|
|
432
|
+
caller: cliOpts.caller,
|
|
433
|
+
source: cliOpts.caller,
|
|
434
|
+
actionName: name,
|
|
435
|
+
headers: new Headers(),
|
|
436
|
+
env: readRuntimeEnv()
|
|
437
|
+
});
|
|
438
|
+
return invokeWithContext(name, rawInput, ctx, {
|
|
439
|
+
caller: cliOpts.caller,
|
|
440
|
+
...cliOpts.approved !== void 0 ? { approved: cliOpts.approved } : {},
|
|
441
|
+
...cliOpts.approvalToken !== void 0 ? { approvalToken: cliOpts.approvalToken } : {}
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
const registry = {
|
|
445
|
+
actions,
|
|
446
|
+
list: () => Object.keys(actions),
|
|
447
|
+
describe: () => Object.entries(actions).map(([name, action]) => ({
|
|
448
|
+
name,
|
|
449
|
+
...action.title ? { title: action.title } : {},
|
|
450
|
+
description: action.description,
|
|
451
|
+
httpMethod: resolveActionHttpMethod(action),
|
|
452
|
+
httpPath: resolveActionHttpPath(name, action),
|
|
453
|
+
readOnly: isActionReadOnly(action),
|
|
454
|
+
destructive: Boolean(action.destructive),
|
|
455
|
+
retrySafe: Boolean(action.retrySafe),
|
|
456
|
+
mcpExposed: action.agentTool !== false && action.mcpTool !== false,
|
|
457
|
+
agentExposed: action.agentTool !== false,
|
|
458
|
+
hasInputSchema: Boolean(action.schema),
|
|
459
|
+
hasOutputSchema: Boolean(action.outputSchema)
|
|
460
|
+
})),
|
|
461
|
+
httpMethodMap: () => {
|
|
462
|
+
const map = {};
|
|
463
|
+
for (const [name, action] of Object.entries(actions)) {
|
|
464
|
+
const method = resolveActionHttpMethod(action);
|
|
465
|
+
if (method) map[name] = method;
|
|
466
|
+
}
|
|
467
|
+
return map;
|
|
468
|
+
},
|
|
469
|
+
resolveHttpAction: (relativePath) => httpRouteIndex.get(relativePath) ?? null,
|
|
470
|
+
call: async (name, input, callOpts = {}) => {
|
|
471
|
+
const caller = callOpts.caller ?? "server";
|
|
472
|
+
const ctx = callOpts.ctx !== void 0 ? callOpts.ctx : await resolveContext({
|
|
473
|
+
caller,
|
|
474
|
+
source: caller,
|
|
475
|
+
actionName: name,
|
|
476
|
+
...callOpts.request ? { request: callOpts.request } : {},
|
|
477
|
+
headers: new Headers(
|
|
478
|
+
callOpts.headers ?? callOpts.request?.headers
|
|
479
|
+
),
|
|
480
|
+
...callOpts.signal ? { signal: callOpts.signal } : callOpts.request ? { signal: callOpts.request.signal } : {},
|
|
481
|
+
env: readRuntimeEnv(),
|
|
482
|
+
...callOpts.platform !== void 0 ? { platform: callOpts.platform } : {}
|
|
483
|
+
});
|
|
484
|
+
return await invokeWithContext(name, input, ctx, {
|
|
485
|
+
caller,
|
|
486
|
+
...callOpts.approvalToken !== void 0 ? { approvalToken: callOpts.approvalToken } : {},
|
|
487
|
+
...callOpts.approved !== void 0 ? { approved: callOpts.approved } : {}
|
|
488
|
+
});
|
|
489
|
+
},
|
|
490
|
+
invokeFromRequest,
|
|
491
|
+
invokeFromCliOrStdio,
|
|
492
|
+
invokeWithContext,
|
|
493
|
+
fetchHandler: async () => new Response("Not initialized", { status: 500 }),
|
|
494
|
+
mcpHandler: async () => new Response("Not initialized", { status: 500 }),
|
|
495
|
+
handler: async () => new Response("Not initialized", { status: 500 }),
|
|
496
|
+
createHandler: (combinedOptions = {}) => {
|
|
497
|
+
const basePath = combinedOptions.basePath ?? "/api/agent-native";
|
|
498
|
+
const actionBasePath = `${basePath.replace(/\/+$/, "")}/actions`;
|
|
499
|
+
const actionFetch = createActionFetchHandler(registry, {
|
|
500
|
+
...options.http,
|
|
501
|
+
...combinedOptions.http,
|
|
502
|
+
basePath: actionBasePath
|
|
503
|
+
});
|
|
504
|
+
const mcpEnabled = combinedOptions.mcp !== false;
|
|
505
|
+
const mcpOpts = typeof combinedOptions.mcp === "object" ? { ...options.mcp, ...combinedOptions.mcp } : { ...options.mcp };
|
|
506
|
+
const mcpFetch = mcpEnabled ? createMcpFetchHandler(registry, mcpOpts) : null;
|
|
507
|
+
return async (request, override) => {
|
|
508
|
+
const url = new URL(request.url);
|
|
509
|
+
const rel = override?.pathSegments !== void 0 ? override.pathSegments.join("/") : stripBasePath(url.pathname, basePath);
|
|
510
|
+
if (rel === "mcp" || rel.startsWith("mcp/")) {
|
|
511
|
+
if (!mcpFetch) {
|
|
512
|
+
return new Response(
|
|
513
|
+
JSON.stringify({
|
|
514
|
+
error: "MCP endpoint is disabled.",
|
|
515
|
+
errorCode: "not_found"
|
|
516
|
+
}),
|
|
517
|
+
{
|
|
518
|
+
status: 404,
|
|
519
|
+
headers: { "Content-Type": "application/json; charset=utf-8" }
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
return mcpFetch(
|
|
524
|
+
request,
|
|
525
|
+
override?.platform !== void 0 ? { platform: override.platform } : void 0
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
if (rel === "actions" || rel.startsWith("actions/")) {
|
|
529
|
+
const actionPath = rel === "actions" ? "" : rel.slice("actions/".length);
|
|
530
|
+
return actionFetch(request, {
|
|
531
|
+
actionPath,
|
|
532
|
+
...override?.platform !== void 0 ? { platform: override.platform } : {}
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
return new Response(
|
|
536
|
+
JSON.stringify({
|
|
537
|
+
error: `Unknown route "${rel}". Expected /actions/<name> or /mcp.`,
|
|
538
|
+
errorCode: "not_found"
|
|
539
|
+
}),
|
|
540
|
+
{
|
|
541
|
+
status: 404,
|
|
542
|
+
headers: { "Content-Type": "application/json; charset=utf-8" }
|
|
543
|
+
}
|
|
544
|
+
);
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
registry.fetchHandler = createActionFetchHandler(registry, options.http);
|
|
549
|
+
registry.mcpHandler = createMcpFetchHandler(registry, options.mcp);
|
|
550
|
+
registry.handler = registry.createHandler();
|
|
551
|
+
return registry;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// src/skill.ts
|
|
555
|
+
import fs from "fs";
|
|
556
|
+
import path from "path";
|
|
557
|
+
var AGENT_NATIVE_ACTIONS_SKILL_NAME = "agent-native-actions";
|
|
558
|
+
var AGENT_NATIVE_ACTIONS_SKILL_RELATIVE_PATH = path.join(
|
|
559
|
+
".agents",
|
|
560
|
+
"skills",
|
|
561
|
+
AGENT_NATIVE_ACTIONS_SKILL_NAME,
|
|
562
|
+
"SKILL.md"
|
|
563
|
+
);
|
|
564
|
+
var AGENT_NATIVE_ACTIONS_SKILL_MD = `---
|
|
565
|
+
name: agent-native-actions
|
|
566
|
+
description: Build and maintain backend operations with @agent-native/actions. Use whenever adding a new feature, endpoint, mutation, data fetch, CLI command, or AI tool in this app so UI, HTTP, CLI, MCP, and AI SDKs always share one typed action.
|
|
567
|
+
---
|
|
568
|
+
|
|
569
|
+
# Agent-Native Actions
|
|
570
|
+
|
|
571
|
+
This repository uses \`@agent-native/actions\` as the single source of truth for application operations.
|
|
572
|
+
|
|
573
|
+
## Core Rule: Always Build New Capabilities as Actions
|
|
574
|
+
|
|
575
|
+
When adding any new backend feature, query, mutation, or integration:
|
|
576
|
+
|
|
577
|
+
1. **Create one file per operation in \`actions/<verb-noun>.ts\`** (for example \`actions/list-deals.ts\`, \`actions/create-invoice.ts\`, \`actions/refund-charge.ts\`).
|
|
578
|
+
2. **Do not create parallel ad-hoc \`/api/*\` REST routes, Next.js Server Actions, tRPC procedures, or standalone database scripts** for normal app operations. Put the operation in \`actions/\` so your UI hooks, HTTP API, CLI, MCP server, and AI SDK tools automatically stay in parity.
|
|
579
|
+
3. Reserve custom routes only for streaming responses, binary/file uploads, inbound third-party webhooks, OAuth callbacks, or non-JSON endpoints.
|
|
580
|
+
|
|
581
|
+
## Defining an Action
|
|
582
|
+
|
|
583
|
+
Use the app's typed \`defineAction\` helper from \`actions/context.ts\` if present (otherwise import \`defineAction\` from \`@agent-native/actions\`):
|
|
584
|
+
|
|
585
|
+
\`\`\`ts
|
|
586
|
+
// actions/list-todos.ts
|
|
587
|
+
import { z } from "zod";
|
|
588
|
+
import { defineAction } from "./context"; // or "@agent-native/actions"
|
|
589
|
+
|
|
590
|
+
export default defineAction({
|
|
591
|
+
description: "List todos for the current user",
|
|
592
|
+
schema: z.object({
|
|
593
|
+
done: z.boolean().optional().describe("Filter by completion status"),
|
|
594
|
+
}),
|
|
595
|
+
http: { method: "GET" },
|
|
596
|
+
readOnly: true,
|
|
597
|
+
authorize: (_input, ctx) => Boolean(ctx.user),
|
|
598
|
+
run: async ({ done }, ctx) => {
|
|
599
|
+
return db.todos.findMany({ where: { userId: ctx.user.id, done } });
|
|
600
|
+
},
|
|
601
|
+
});
|
|
602
|
+
\`\`\`
|
|
603
|
+
|
|
604
|
+
### Action Authoring Checklist
|
|
605
|
+
|
|
606
|
+
- **Filename = action name:** \`actions/create-todo.ts\` becomes \`create-todo\`. Subfolders join with hyphens (\`actions/billing/refund.ts\` -> \`billing-refund\`). Prefix shared helpers with \`_\` (\`actions/_shared/db.ts\`) so they are not registered as actions.
|
|
607
|
+
- **Describe every field:** Always provide a concise one-line \`description\` and \`.describe(...)\` on schema fields so the CLI \`--help\`, MCP clients, and AI agents know how to call the action.
|
|
608
|
+
- **Mark reads explicitly:** Set \`http: { method: "GET" }\` and \`readOnly: true\` on read-only queries. Leave mutations on default \`POST\` (or \`PUT\` / \`DELETE\`).
|
|
609
|
+
- **Enforce auth in \`authorize(input, ctx)\`:** Never trust UI hiding. Check \`ctx\` (resolved once in \`actions/context.ts\`) inside \`authorize(input, ctx)\` so HTTP, CLI, MCP, and AI callers all go through the same check.
|
|
610
|
+
- **Gate sensitive writes with \`needsApproval\`:** For destructive, irreversible, or high-value mutations (deleting data, issuing refunds, sending external emails), set \`needsApproval: true\` or \`needsApproval: (input, ctx) => ...\` and \`destructive: true\`.
|
|
611
|
+
- **Throw structured errors with \`fail()\` or \`defineActionErrors()\`:** Never throw bare strings or swallow errors into fake empty results. Use \`fail("Invoice not found", { statusCode: 404, errorCode: "not_found" })\`.
|
|
612
|
+
- **Hide internal-only actions from external agents when needed:** Set \`mcpTool: false\` (or \`agentTool: false\`) on UI-only or internal admin operations.
|
|
613
|
+
|
|
614
|
+
## Calling Actions from UI Code
|
|
615
|
+
|
|
616
|
+
Use the typed client hooks for this repo's framework \u2014 never hand-write \`fetch("/api/...")\` calls for actions:
|
|
617
|
+
|
|
618
|
+
- **React / Next.js:** \`useActionQuery("list-todos", { done: false })\` and \`useActionMutation("create-todo")\` from \`@agent-native/actions/react\` (or \`@agent-native/actions/next\`)
|
|
619
|
+
- **SolidJS:** \`createActionQuery\` / \`createActionMutation\` from \`@agent-native/actions/solid\`
|
|
620
|
+
- **Vue / Nuxt:** \`useActionQuery\` / \`useActionMutation\` from \`@agent-native/actions/vue\`
|
|
621
|
+
- **Svelte 5 / SvelteKit:** \`createActionQuery\` / \`createActionMutation\` from \`@agent-native/actions/svelte\`
|
|
622
|
+
- **Plain TypeScript:** \`createActionClient()\` from \`@agent-native/actions/client\`
|
|
623
|
+
|
|
624
|
+
Mutations automatically invalidate related queries so views stay fresh without manual query-key plumbing.
|
|
625
|
+
|
|
626
|
+
## Regenerating Types & Verifying via CLI
|
|
627
|
+
|
|
628
|
+
After adding or changing actions:
|
|
629
|
+
|
|
630
|
+
1. Regenerate \`.generated/actions.ts\` and \`.generated/action-types.d.ts\` if the dev server is not running:
|
|
631
|
+
\`\`\`bash
|
|
632
|
+
npx @agent-native/actions generate
|
|
633
|
+
\`\`\`
|
|
634
|
+
2. Verify discovery and execution through the CLI:
|
|
635
|
+
\`\`\`bash
|
|
636
|
+
pnpm action --list
|
|
637
|
+
pnpm action <action-name> --help
|
|
638
|
+
pnpm action <action-name> '{"key":"value"}'
|
|
639
|
+
\`\`\`
|
|
640
|
+
`;
|
|
641
|
+
function installActionsSkill(options) {
|
|
642
|
+
const filePath = path.join(
|
|
643
|
+
options.projectRoot,
|
|
644
|
+
AGENT_NATIVE_ACTIONS_SKILL_RELATIVE_PATH
|
|
645
|
+
);
|
|
646
|
+
const exists = fs.existsSync(filePath);
|
|
647
|
+
if (exists && !options.overwrite) {
|
|
648
|
+
return {
|
|
649
|
+
filePath,
|
|
650
|
+
relativePath: AGENT_NATIVE_ACTIONS_SKILL_RELATIVE_PATH,
|
|
651
|
+
status: "skipped"
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
if (options.dryRun) {
|
|
655
|
+
return {
|
|
656
|
+
filePath,
|
|
657
|
+
relativePath: AGENT_NATIVE_ACTIONS_SKILL_RELATIVE_PATH,
|
|
658
|
+
status: exists ? "would-update" : "would-create"
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
662
|
+
fs.writeFileSync(filePath, AGENT_NATIVE_ACTIONS_SKILL_MD, "utf8");
|
|
663
|
+
return {
|
|
664
|
+
filePath,
|
|
665
|
+
relativePath: AGENT_NATIVE_ACTIONS_SKILL_RELATIVE_PATH,
|
|
666
|
+
status: exists ? "updated" : "created"
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
export {
|
|
670
|
+
AGENT_NATIVE_ACTIONS_SKILL_MD,
|
|
671
|
+
AGENT_NATIVE_ACTIONS_SKILL_NAME,
|
|
672
|
+
AGENT_NATIVE_ACTIONS_SKILL_RELATIVE_PATH,
|
|
673
|
+
ActionError,
|
|
674
|
+
ActionPostCommitError,
|
|
675
|
+
createActionBuilder,
|
|
676
|
+
createActionFetchHandler,
|
|
677
|
+
createActions,
|
|
678
|
+
createMcpFetchHandler,
|
|
679
|
+
defineAction,
|
|
680
|
+
defineActionErrors,
|
|
681
|
+
executeAction,
|
|
682
|
+
fail,
|
|
683
|
+
generateActionArtifacts,
|
|
684
|
+
installActionsSkill,
|
|
685
|
+
isActionError,
|
|
686
|
+
isActionPostCommitError,
|
|
687
|
+
isActionReadOnly,
|
|
688
|
+
isDefinedAction,
|
|
689
|
+
normalizeLlmToolInput,
|
|
690
|
+
resolveActionHttpMethod,
|
|
691
|
+
resolveActionHttpPath,
|
|
692
|
+
runStdioMcpServer,
|
|
693
|
+
scanActionsDirectory,
|
|
694
|
+
schemaToJsonSchema,
|
|
695
|
+
serializeActionErrorBody,
|
|
696
|
+
stripBasePath,
|
|
697
|
+
toAiSdkTools,
|
|
698
|
+
toAnthropicTools,
|
|
699
|
+
toNodeHandler,
|
|
700
|
+
toOpenAiTools,
|
|
701
|
+
validateInputSchema,
|
|
702
|
+
validateOutputSchema
|
|
703
|
+
};
|
|
704
|
+
//# sourceMappingURL=index.js.map
|