@svadmin/core 0.48.0 → 0.49.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/core",
3
- "version": "0.48.0",
3
+ "version": "0.49.1",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
@@ -68,6 +68,10 @@
68
68
  "./inferencer-graphql": {
69
69
  "types": "./src/inferencer-graphql.ts",
70
70
  "default": "./src/inferencer-graphql.ts"
71
+ },
72
+ "./admin-tool": {
73
+ "types": "./src/admin-tool.ts",
74
+ "default": "./src/admin-tool.ts"
71
75
  }
72
76
  },
73
77
  "bugs": {
@@ -86,7 +90,9 @@
86
90
  "sideEffects": false,
87
91
  "type": "module",
88
92
  "types": "src/index.ts",
89
- "dependencies": {},
93
+ "dependencies": {
94
+ "@sinclair/typebox": "^0.34.52"
95
+ },
90
96
  "peerDependenciesMeta": {
91
97
  "svelte": {
92
98
  "optional": true
@@ -0,0 +1,99 @@
1
+ import type { StaticDecode, TObject } from '@sinclair/typebox';
2
+ import { Value } from '@sinclair/typebox/value';
3
+
4
+ /** Result returned by a tool execution. */
5
+ export interface ToolResult {
6
+ success: boolean;
7
+ data?: unknown;
8
+ error?: string;
9
+ }
10
+
11
+ /**
12
+ * A tool that the Agent can invoke within the admin panel.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const deletePostTool = defineAdminTool({
17
+ * name: 'deletePosts',
18
+ * description: 'Delete posts matching a filter',
19
+ * parameters: Type.Object({
20
+ * status: Type.Union([Type.Literal('draft'), Type.Literal('archived')]),
21
+ * }),
22
+ * needsApproval: true,
23
+ * execute: async ({ status }) => {
24
+ * const result = await dataProvider.getList({
25
+ * resource: 'posts',
26
+ * filters: [{ field: 'status', operator: 'eq', value: status }],
27
+ * });
28
+ * return { success: true, data: result };
29
+ * },
30
+ * });
31
+ * ```
32
+ */
33
+ export interface AdminTool<Schema extends TObject = TObject> {
34
+ /** Unique tool name (e.g. 'getList', 'deleteRecords', 'generateReport') */
35
+ name: string;
36
+ /** Human-readable description for LLM tool-use prompting */
37
+ description: string;
38
+ /** TypeBox schema used for static inference, runtime validation, and LLM projection. */
39
+ parameters: Schema;
40
+ /** Pause execution until the user explicitly approves the invocation. */
41
+ needsApproval?: boolean;
42
+ /** 显式声明是否为只读操作(只读操作可并发执行,无副作用)。 */
43
+ readOnly?: boolean;
44
+ /** 显式声明是否为破坏性或高风险操作。 */
45
+ destructive?: boolean;
46
+ /** 显式声明是否支持并发调用。 */
47
+ concurrent?: boolean;
48
+ /** Execute the tool with decoded arguments. */
49
+ execute(args: StaticDecode<Schema>): Promise<ToolResult>;
50
+ }
51
+
52
+ /** 保留具体 TypeBox Schema,让 execute 参数获得精确的静态类型。 */
53
+ export function defineAdminTool<const Schema extends TObject>(
54
+ tool: AdminTool<Schema>,
55
+ ): AdminTool<Schema> {
56
+ return { ...tool, parameters: strictAdminToolSchema(tool.parameters) };
57
+ }
58
+
59
+ /** 在进入工具实现前统一执行 TypeBox 校验与转换。 */
60
+ export function decodeAdminToolArgs<const Schema extends TObject>(
61
+ tool: AdminTool<Schema>,
62
+ input: unknown,
63
+ ): StaticDecode<Schema> {
64
+ return Value.Decode(strictAdminToolSchema(tool.parameters), input);
65
+ }
66
+
67
+ /** 解码并执行工具,避免调用方绕过运行时参数边界。 */
68
+ export async function executeAdminTool<const Schema extends TObject>(
69
+ tool: AdminTool<Schema>,
70
+ input: unknown,
71
+ ): Promise<ToolResult> {
72
+ return tool.execute(decodeAdminToolArgs(tool, input));
73
+ }
74
+
75
+ /** 将工具投影为发给 LLM 或 MCP 客户端的公开 Schema。 */
76
+ export function projectAdminToolSchema<Schema extends TObject>(tool: AdminTool<Schema>): {
77
+ name: string;
78
+ description: string;
79
+ parameters: Schema;
80
+ readOnly?: boolean;
81
+ destructive?: boolean;
82
+ concurrent?: boolean;
83
+ needsApproval?: boolean;
84
+ } {
85
+ return {
86
+ name: tool.name,
87
+ description: tool.description,
88
+ parameters: strictAdminToolSchema(tool.parameters),
89
+ ...(tool.readOnly !== undefined ? { readOnly: tool.readOnly } : {}),
90
+ ...(tool.destructive !== undefined ? { destructive: tool.destructive } : {}),
91
+ ...(tool.concurrent !== undefined ? { concurrent: tool.concurrent } : {}),
92
+ ...(tool.needsApproval !== undefined ? { needsApproval: tool.needsApproval } : {}),
93
+ };
94
+ }
95
+
96
+ function strictAdminToolSchema<Schema extends TObject>(schema: Schema): Schema {
97
+ if (schema.additionalProperties === false) return schema;
98
+ return { ...schema, additionalProperties: false };
99
+ }
@@ -1,5 +1,15 @@
1
1
  // Headless AI contracts. Rendering lives in @svadmin/ai-elements.
2
2
 
3
+ import type { AdminTool, ToolResult } from './admin-tool';
4
+
5
+ export type { AdminTool, ToolResult } from './admin-tool';
6
+ export {
7
+ decodeAdminToolArgs,
8
+ defineAdminTool,
9
+ executeAdminTool,
10
+ projectAdminToolSchema,
11
+ } from './admin-tool';
12
+
3
13
  export type ToolState =
4
14
  | 'input-streaming'
5
15
  | 'input-available'
@@ -87,62 +97,6 @@ export interface ChatProvider {
87
97
  // • Approval gates — Dangerous tools require user confirmation before execution
88
98
  // • Generative UI — Agent returns component name + props for dynamic rendering
89
99
 
90
- /** JSON-Schema-compatible parameter definition for an admin tool. */
91
- export interface AdminToolParameter {
92
- type: 'string' | 'number' | 'boolean' | 'object' | 'array';
93
- description?: string;
94
- enum?: string[];
95
- required?: boolean;
96
- }
97
-
98
- /**
99
- * A tool that the Agent can invoke within the admin panel.
100
- *
101
- * @example
102
- * ```ts
103
- * const deletePostTool: AdminTool = {
104
- * name: 'deletePosts',
105
- * description: 'Delete posts matching a filter',
106
- * parameters: {
107
- * status: { type: 'string', description: 'Filter by status', enum: ['draft', 'archived'] },
108
- * },
109
- * needsApproval: true,
110
- * execute: async (params) => {
111
- * const result = await dataProvider.deleteMany({ resource: 'posts', ids: params.ids });
112
- * return { success: true, data: result };
113
- * },
114
- * };
115
- * ```
116
- */
117
- export interface AdminTool {
118
- /** Unique tool name (e.g. 'getList', 'deleteRecords', 'generateReport') */
119
- name: string;
120
- /** Human-readable description for LLM tool-use prompting */
121
- description: string;
122
- /** JSON-Schema-style parameter definitions */
123
- parameters: Record<string, AdminToolParameter>;
124
- /**
125
- * When `true`, execution is paused until the user explicitly approves.
126
- * The agent emits an `approval_request` event and waits for `approveToolCall()`.
127
- */
128
- needsApproval?: boolean;
129
- /** 显式声明是否为只读操作(只读操作可并发执行,无副作用) */
130
- readOnly?: boolean;
131
- /** 显式声明是否为破坏性/高风险操作(默认触发 Approval Gate) */
132
- destructive?: boolean;
133
- /** 显式声明是否支持并发调用 */
134
- concurrent?: boolean;
135
- /** Execute the tool with the given arguments. */
136
- execute(args: Record<string, unknown>): Promise<ToolResult>;
137
- }
138
-
139
- /** Result returned by a tool execution. */
140
- export interface ToolResult {
141
- success: boolean;
142
- data?: unknown;
143
- error?: string;
144
- }
145
-
146
100
  /** Discriminated union of events emitted by an AgentProvider. */
147
101
  export type AgentEvent =
148
102
  /** Streaming text chunk */
@@ -280,30 +234,6 @@ export function getChatContext(): ChatContext {
280
234
  return chatContext;
281
235
  }
282
236
 
283
- /**
284
- * 将 AdminTool 投影为发给 LLM / MCP 客户端的白名单 Schema。
285
- * 剥离底层执行逻辑(execute)与宿主私有状态,保留 name、description、parameters 及并发/安全声明。
286
- */
287
- export function projectAdminToolSchema(tool: AdminTool): {
288
- name: string;
289
- description: string;
290
- parameters: Record<string, AdminToolParameter>;
291
- readOnly?: boolean;
292
- destructive?: boolean;
293
- concurrent?: boolean;
294
- needsApproval?: boolean;
295
- } {
296
- return {
297
- name: tool.name,
298
- description: tool.description,
299
- parameters: { ...tool.parameters },
300
- ...(tool.readOnly !== undefined ? { readOnly: tool.readOnly } : {}),
301
- ...(tool.destructive !== undefined ? { destructive: tool.destructive } : {}),
302
- ...(tool.concurrent !== undefined ? { concurrent: tool.concurrent } : {}),
303
- ...(tool.needsApproval !== undefined ? { needsApproval: tool.needsApproval } : {}),
304
- };
305
- }
306
-
307
237
  export function resetChatProvider(): void {
308
238
  chatProvider = null;
309
239
  agentProvider = null;
package/src/index.ts CHANGED
@@ -116,11 +116,11 @@ export type {
116
116
  WebhookSummary,
117
117
  } from './enterprise';
118
118
  export { assertEnterpriseRequestContext, createEnterpriseRequestContext } from './enterprise';
119
- export { setChatProvider, getChatProvider, setChatContext, getChatContext, setAgentProvider, getAgentProvider, registerApproval, resolveApproval, hasPendingApprovals, resetChatProvider, projectAdminToolSchema } from './chatProvider.svelte';
119
+ export { setChatProvider, getChatProvider, setChatContext, getChatContext, setAgentProvider, getAgentProvider, registerApproval, resolveApproval, hasPendingApprovals, resetChatProvider, defineAdminTool, decodeAdminToolArgs, executeAdminTool, projectAdminToolSchema } from './chatProvider.svelte';
120
120
  export type {
121
121
  ChatProvider, ChatMessage, ChatMessagePart, ChatContext, ChatAttachment, ChatSource,
122
122
  MessageStatus, ToolState, AgentProvider, AgentEvent, AgentOptions, ApprovalResponseOptions,
123
- AdminTool, AdminToolParameter, ToolResult,
123
+ AdminTool, ToolResult,
124
124
  } from './chatProvider.svelte';
125
125
  export { getTheme, setTheme, toggleTheme, getResolvedTheme, getColorTheme, setColorTheme, getColorThemes, configureTheme, getThemeConfig, clearCssOverrides, builtinPresets, registerColorPreset, getColorPresets, registerThemeOwner, updateThemeOwner, unregisterThemeOwner, resetTheme } from './theme.svelte';
126
126
  export type { ThemeMode, ColorTheme, ThemeStrategy, ThemeConfig, ColorPreset, ThemeOwnerOptions, ThemeOwnerToken } from './theme.svelte';