@svadmin/core 0.47.1 → 0.48.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/core",
3
- "version": "0.47.1",
3
+ "version": "0.48.0",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,49 +1,83 @@
1
- // Chat & Agent provider — headless interface for AI integration
2
- //
3
- // Two abstraction levels:
4
- // 1. ChatProvider — simple message-in / text-out (backward compatible)
5
- // 2. AgentProvider — tool calling, approval gates, generative UI events
6
- //
7
- // Inspired by Vercel AI SDK 6 agent abstractions.
8
-
9
- /** Describes an actionable button rendered in assistant messages (tool-call UX). */
10
- export interface ChatAction {
11
- label: string;
12
- /** Optional variant for styling (e.g. 'destructive' renders red) */
13
- variant?: 'default' | 'destructive' | 'outline';
14
- /** Arbitrary payload forwarded to the onAction handler */
15
- payload?: Record<string, unknown>;
1
+ // Headless AI contracts. Rendering lives in @svadmin/ai-elements.
2
+
3
+ export type ToolState =
4
+ | 'input-streaming'
5
+ | 'input-available'
6
+ | 'approval-requested'
7
+ | 'approval-responded'
8
+ | 'output-available'
9
+ | 'output-denied'
10
+ | 'output-error';
11
+
12
+ export type MessageStatus = 'submitted' | 'streaming' | 'complete' | 'aborted' | 'error';
13
+
14
+ export interface ChatAttachment {
15
+ id: string;
16
+ name: string;
17
+ mediaType?: string;
18
+ url?: string;
19
+ size?: number;
20
+ /** 原始浏览器文件,供上传适配器直接读取;持久化恢复时不会保留。 */
21
+ file?: File;
22
+ }
23
+
24
+ export interface ChatSource {
25
+ id?: string;
26
+ title: string;
27
+ url?: string;
28
+ quote?: string;
29
+ description?: string;
16
30
  }
17
31
 
32
+ export type ChatMessagePart =
33
+ | { id?: string; type: 'text'; text: string }
34
+ | { id?: string; type: 'reasoning'; text: string; streaming?: boolean }
35
+ | { id?: string; type: 'tool-call'; tool: string; input: unknown; state: ToolState; callId?: string }
36
+ | { id?: string; type: 'tool-result'; tool: string; output: unknown; error?: string; callId?: string }
37
+ | { id?: string; type: 'source'; source: ChatSource }
38
+ | { id?: string; type: 'image'; src: string; alt?: string; width?: number; height?: number }
39
+ | { id?: string; type: 'file'; file: ChatAttachment }
40
+ | {
41
+ id?: string;
42
+ type: 'approval';
43
+ approvalId: string;
44
+ tool: string;
45
+ input: Record<string, unknown>;
46
+ description: string;
47
+ approved?: boolean;
48
+ }
49
+ | { id?: string; type: 'component'; name: string; props: Record<string, unknown> };
50
+
18
51
  export interface ChatMessage {
19
52
  id: string;
20
53
  role: 'user' | 'assistant' | 'system';
21
- content: string;
22
- timestamp: number;
23
- /** Tool-call action buttons rendered below the message bubble */
24
- actions?: ChatAction[];
54
+ parts: ChatMessagePart[];
55
+ status?: MessageStatus;
56
+ createdAt: number;
57
+ attachments?: ChatAttachment[];
25
58
  }
26
59
 
27
- /** Admin context automatically injected into chat system messages. */
60
+ /** Admin context passed separately from untrusted conversation messages. */
28
61
  export interface ChatContext {
29
62
  currentResource?: string;
30
- selectedRecordId?: string;
31
- currentView?: 'list' | 'edit' | 'create' | 'show';
63
+ selectedRecordId?: string | number;
64
+ currentView?: 'list' | 'edit' | 'create' | 'show' | string;
32
65
  pathname?: string;
66
+ metadata?: Record<string, unknown>;
33
67
  }
34
68
 
35
69
  /**
36
70
  * ChatProvider interface for integrating AI chat into admin panels.
37
71
  * Implement `sendMessage` to connect to any AI backend (OpenAI, self-hosted, etc.)
38
72
  *
39
- * Return a `string` for non-streaming responses, or an `AsyncGenerator<string>`
40
- * for streaming (SSE / chunked) responses.
73
+ * Return text or structured parts for non-streaming responses. For streaming
74
+ * transports, yield text chunks and/or structured parts from an async generator.
41
75
  */
42
76
  export interface ChatProvider {
43
77
  sendMessage(
44
78
  messages: ChatMessage[],
45
- options?: { signal?: AbortSignal },
46
- ): Promise<string> | AsyncGenerator<string, void, unknown>;
79
+ options?: { signal?: AbortSignal; context?: ChatContext },
80
+ ): Promise<ChatMessagePart[] | string> | AsyncGenerator<ChatMessagePart | string, void, unknown>;
47
81
  }
48
82
 
49
83
  // ─── Agent Provider (extends ChatProvider concept) ─────────────
@@ -113,12 +147,16 @@ export interface ToolResult {
113
147
  export type AgentEvent =
114
148
  /** Streaming text chunk */
115
149
  | { type: 'text'; content: string }
150
+ /** Streaming reasoning chunk */
151
+ | { type: 'reasoning'; content: string; streaming?: boolean }
116
152
  /** Agent wants to call a tool (informational — execution is handled internally) */
117
- | { type: 'tool_call'; tool: string; args: Record<string, unknown> }
153
+ | { type: 'tool_call'; tool: string; args: Record<string, unknown>; callId?: string }
118
154
  /** Agent wants to call a tool that requires user approval before execution */
119
155
  | { type: 'approval_request'; id: string; tool: string; args: Record<string, unknown>; description: string }
120
156
  /** Tool execution completed */
121
- | { type: 'tool_result'; tool: string; result: ToolResult }
157
+ | { type: 'tool_result'; tool: string; result: ToolResult; callId?: string }
158
+ /** Source attribution emitted by a retrieval step */
159
+ | { type: 'source'; source: ChatSource }
122
160
  /** Agent wants to render a named component (Generative UI) */
123
161
  | { type: 'component'; name: string; props: Record<string, unknown> }
124
162
  /** Stream finished */
@@ -131,6 +169,14 @@ export interface AgentOptions {
131
169
  context?: ChatContext;
132
170
  }
133
171
 
172
+ /**
173
+ * Cancellation signal supplied while an approval decision is being submitted.
174
+ * Providers must stop before committing tool execution when this signal aborts.
175
+ */
176
+ export interface ApprovalResponseOptions {
177
+ signal: AbortSignal;
178
+ }
179
+
134
180
  /**
135
181
  * AgentProvider — advanced AI integration with tool calling and approval gates.
136
182
  *
@@ -159,7 +205,11 @@ export interface AgentProvider {
159
205
  messages: ChatMessage[],
160
206
  options?: AgentOptions,
161
207
  ): AsyncGenerator<AgentEvent, void, unknown>;
162
- approveToolCall?(id: string, approved: boolean): void;
208
+ approveToolCall?(
209
+ id: string,
210
+ approved: boolean,
211
+ options: ApprovalResponseOptions,
212
+ ): void | Promise<void>;
163
213
  }
164
214
 
165
215
  // ─── Chat Provider singleton ───────────────────────────────────
package/src/index.ts CHANGED
@@ -117,7 +117,11 @@ export type {
117
117
  } from './enterprise';
118
118
  export { assertEnterpriseRequestContext, createEnterpriseRequestContext } from './enterprise';
119
119
  export { setChatProvider, getChatProvider, setChatContext, getChatContext, setAgentProvider, getAgentProvider, registerApproval, resolveApproval, hasPendingApprovals, resetChatProvider, projectAdminToolSchema } from './chatProvider.svelte';
120
- export type { ChatProvider, ChatMessage, ChatContext, ChatAction, AgentProvider, AgentEvent, AgentOptions, AdminTool, AdminToolParameter, ToolResult } from './chatProvider.svelte';
120
+ export type {
121
+ ChatProvider, ChatMessage, ChatMessagePart, ChatContext, ChatAttachment, ChatSource,
122
+ MessageStatus, ToolState, AgentProvider, AgentEvent, AgentOptions, ApprovalResponseOptions,
123
+ AdminTool, AdminToolParameter, ToolResult,
124
+ } from './chatProvider.svelte';
121
125
  export { getTheme, setTheme, toggleTheme, getResolvedTheme, getColorTheme, setColorTheme, getColorThemes, configureTheme, getThemeConfig, clearCssOverrides, builtinPresets, registerColorPreset, getColorPresets, registerThemeOwner, updateThemeOwner, unregisterThemeOwner, resetTheme } from './theme.svelte';
122
126
  export type { ThemeMode, ColorTheme, ThemeStrategy, ThemeConfig, ColorPreset, ThemeOwnerOptions, ThemeOwnerToken } from './theme.svelte';
123
127
  export { setUnsavedChanges, getUnsavedChanges, initUnsavedChangesNotifier, resetUnsavedChanges } from './unsaved-changes.svelte';