@witqq/agent-sdk 0.6.1 → 0.7.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 +433 -6
- package/dist/auth/index.cjs +188 -1
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +154 -138
- package/dist/auth/index.d.ts +154 -138
- package/dist/auth/index.js +188 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/backends/claude.cjs +315 -21
- package/dist/backends/claude.cjs.map +1 -1
- package/dist/backends/claude.d.cts +2 -1
- package/dist/backends/claude.d.ts +2 -1
- package/dist/backends/claude.js +315 -21
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +132 -24
- package/dist/backends/copilot.cjs.map +1 -1
- package/dist/backends/copilot.d.cts +2 -1
- package/dist/backends/copilot.d.ts +2 -1
- package/dist/backends/copilot.js +132 -24
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/vercel-ai.cjs +56 -17
- package/dist/backends/vercel-ai.cjs.map +1 -1
- package/dist/backends/vercel-ai.d.cts +1 -1
- package/dist/backends/vercel-ai.d.ts +1 -1
- package/dist/backends/vercel-ai.js +56 -17
- package/dist/backends/vercel-ai.js.map +1 -1
- package/dist/chat/accumulator.cjs +147 -0
- package/dist/chat/accumulator.cjs.map +1 -0
- package/dist/chat/accumulator.d.cts +61 -0
- package/dist/chat/accumulator.d.ts +61 -0
- package/dist/chat/accumulator.js +145 -0
- package/dist/chat/accumulator.js.map +1 -0
- package/dist/chat/backends.cjs +3534 -0
- package/dist/chat/backends.cjs.map +1 -0
- package/dist/chat/backends.d.cts +62 -0
- package/dist/chat/backends.d.ts +62 -0
- package/dist/chat/backends.js +3501 -0
- package/dist/chat/backends.js.map +1 -0
- package/dist/chat/context.cjs +230 -0
- package/dist/chat/context.cjs.map +1 -0
- package/dist/chat/context.d.cts +167 -0
- package/dist/chat/context.d.ts +167 -0
- package/dist/chat/context.js +227 -0
- package/dist/chat/context.js.map +1 -0
- package/dist/chat/core.cjs +282 -0
- package/dist/chat/core.cjs.map +1 -0
- package/dist/chat/core.d.cts +435 -0
- package/dist/chat/core.d.ts +435 -0
- package/dist/chat/core.js +261 -0
- package/dist/chat/core.js.map +1 -0
- package/dist/chat/errors.cjs +251 -0
- package/dist/chat/errors.cjs.map +1 -0
- package/dist/chat/errors.d.cts +122 -0
- package/dist/chat/errors.d.ts +122 -0
- package/dist/chat/errors.js +243 -0
- package/dist/chat/errors.js.map +1 -0
- package/dist/chat/events.cjs +203 -0
- package/dist/chat/events.cjs.map +1 -0
- package/dist/chat/events.d.cts +241 -0
- package/dist/chat/events.d.ts +241 -0
- package/dist/chat/events.js +196 -0
- package/dist/chat/events.js.map +1 -0
- package/dist/chat/index.cjs +5359 -0
- package/dist/chat/index.cjs.map +1 -0
- package/dist/chat/index.d.cts +52 -0
- package/dist/chat/index.d.ts +52 -0
- package/dist/chat/index.js +5296 -0
- package/dist/chat/index.js.map +1 -0
- package/dist/chat/react.cjs +2739 -0
- package/dist/chat/react.cjs.map +1 -0
- package/dist/chat/react.d.cts +619 -0
- package/dist/chat/react.d.ts +619 -0
- package/dist/chat/react.js +2714 -0
- package/dist/chat/react.js.map +1 -0
- package/dist/chat/runtime.cjs +1030 -0
- package/dist/chat/runtime.cjs.map +1 -0
- package/dist/chat/runtime.d.cts +118 -0
- package/dist/chat/runtime.d.ts +118 -0
- package/dist/chat/runtime.js +1028 -0
- package/dist/chat/runtime.js.map +1 -0
- package/dist/chat/server.cjs +643 -0
- package/dist/chat/server.cjs.map +1 -0
- package/dist/chat/server.d.cts +287 -0
- package/dist/chat/server.d.ts +287 -0
- package/dist/chat/server.js +617 -0
- package/dist/chat/server.js.map +1 -0
- package/dist/chat/sessions.cjs +398 -0
- package/dist/chat/sessions.cjs.map +1 -0
- package/dist/chat/sessions.d.cts +239 -0
- package/dist/chat/sessions.d.ts +239 -0
- package/dist/chat/sessions.js +394 -0
- package/dist/chat/sessions.js.map +1 -0
- package/dist/chat/state.cjs +177 -0
- package/dist/chat/state.cjs.map +1 -0
- package/dist/chat/state.d.cts +92 -0
- package/dist/chat/state.d.ts +92 -0
- package/dist/chat/state.js +167 -0
- package/dist/chat/state.js.map +1 -0
- package/dist/chat/storage.cjs +240 -0
- package/dist/chat/storage.cjs.map +1 -0
- package/dist/chat/storage.d.cts +191 -0
- package/dist/chat/storage.d.ts +191 -0
- package/dist/chat/storage.js +236 -0
- package/dist/chat/storage.js.map +1 -0
- package/dist/errors-BDLbNu9w.d.cts +13 -0
- package/dist/errors-BDLbNu9w.d.ts +13 -0
- package/dist/in-process-transport-C2oPTYs6.d.ts +223 -0
- package/dist/in-process-transport-DG-w5G6k.d.cts +223 -0
- package/dist/index.cjs +25 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +25 -13
- package/dist/index.js.map +1 -1
- package/dist/transport-D1OaUgRk.d.ts +67 -0
- package/dist/transport-DX1Nhm4N.d.cts +67 -0
- package/dist/types-Bh5AhqD-.d.ts +141 -0
- package/dist/types-CGF7AEX1.d.cts +141 -0
- package/dist/{types-BvwNzZCj.d.cts → types-CqvUAYxt.d.cts} +21 -3
- package/dist/{types-BvwNzZCj.d.ts → types-CqvUAYxt.d.ts} +21 -3
- package/dist/types-DLZzlJxt.d.ts +39 -0
- package/dist/types-tE0CXwBl.d.cts +39 -0
- package/package.json +149 -2
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { IChatRuntime } from './runtime.cjs';
|
|
4
|
+
import { ChatMessage, RuntimeStatus, SessionInfo, TextPart, ReasoningPart, ToolCallPart, SourcePart, FilePart, ChatSession, ChatEvent, ChatId, ChatIdLike, SendMessageOptions, ChatMiddleware } from './core.cjs';
|
|
5
|
+
import { A as AuthToken } from '../types-CGF7AEX1.cjs';
|
|
6
|
+
import { d as ModelInfo$1, T as ToolDefinition } from '../types-CqvUAYxt.cjs';
|
|
7
|
+
import { CreateSessionOptions, SessionListOptions } from './sessions.cjs';
|
|
8
|
+
import { ContextStats } from './context.cjs';
|
|
9
|
+
import '../types-tE0CXwBl.cjs';
|
|
10
|
+
import '../errors-BDLbNu9w.cjs';
|
|
11
|
+
import 'zod';
|
|
12
|
+
import './storage.cjs';
|
|
13
|
+
|
|
14
|
+
/** Props for ChatProvider. */
|
|
15
|
+
interface ChatProviderProps {
|
|
16
|
+
/** The chat runtime instance to provide to descendants. */
|
|
17
|
+
runtime: IChatRuntime;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* React context provider wrapping IChatRuntime.
|
|
22
|
+
* All chat hooks must be used within a ChatProvider.
|
|
23
|
+
*/
|
|
24
|
+
declare function ChatProvider({ runtime, children }: ChatProviderProps): react.FunctionComponentElement<react.ProviderProps<IChatRuntime<Record<string, unknown>> | null>>;
|
|
25
|
+
/**
|
|
26
|
+
* Access the IChatRuntime from context.
|
|
27
|
+
* Must be used within a ChatProvider.
|
|
28
|
+
*
|
|
29
|
+
* @throws {Error} If used outside ChatProvider
|
|
30
|
+
*/
|
|
31
|
+
declare function useChatRuntime(): IChatRuntime;
|
|
32
|
+
|
|
33
|
+
/** Options for the useChat hook. */
|
|
34
|
+
interface UseChatOptions {
|
|
35
|
+
/** Session ID. If omitted, a new session is created on first send. */
|
|
36
|
+
sessionId?: string;
|
|
37
|
+
/** Called on error during send. */
|
|
38
|
+
onError?: (error: Error) => void;
|
|
39
|
+
}
|
|
40
|
+
/** Return value from useChat. */
|
|
41
|
+
interface UseChatReturn {
|
|
42
|
+
/** Current session ID (null until session created). */
|
|
43
|
+
sessionId: string | null;
|
|
44
|
+
/** Ordered messages in the current session. */
|
|
45
|
+
messages: ChatMessage[];
|
|
46
|
+
/** Send a user message and trigger assistant response. */
|
|
47
|
+
sendMessage: (content: string) => Promise<void>;
|
|
48
|
+
/** Abort the current generation. */
|
|
49
|
+
stop: () => void;
|
|
50
|
+
/** Whether the assistant is currently generating. */
|
|
51
|
+
isGenerating: boolean;
|
|
52
|
+
/** Current runtime status. */
|
|
53
|
+
status: RuntimeStatus;
|
|
54
|
+
/** Current error, if any. */
|
|
55
|
+
error: Error | null;
|
|
56
|
+
/** Clear the error state. */
|
|
57
|
+
clearError: () => void;
|
|
58
|
+
/** Create a new session, resetting messages. */
|
|
59
|
+
newSession: () => Promise<string>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convenience hook for chat interaction.
|
|
63
|
+
* Wraps IChatRuntime with React state management and progressive streaming.
|
|
64
|
+
* Messages update in real-time as tokens arrive (not after full response).
|
|
65
|
+
*/
|
|
66
|
+
declare function useChat(options?: UseChatOptions): UseChatReturn;
|
|
67
|
+
|
|
68
|
+
/** Options for the useMessages hook. */
|
|
69
|
+
interface UseMessagesOptions {
|
|
70
|
+
/** Session ID to observe. */
|
|
71
|
+
sessionId: string;
|
|
72
|
+
}
|
|
73
|
+
/** Return value from useMessages. */
|
|
74
|
+
interface UseMessagesReturn {
|
|
75
|
+
/** Ordered messages in the session. */
|
|
76
|
+
messages: ChatMessage[];
|
|
77
|
+
/** Whether the session was found. */
|
|
78
|
+
isLoaded: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Reactive message list via useSyncExternalStore.
|
|
82
|
+
*
|
|
83
|
+
* If the session supports subscribe/getSnapshot (reactive session),
|
|
84
|
+
* uses useSyncExternalStore for granular updates.
|
|
85
|
+
* Otherwise, falls back to polling via getSession().
|
|
86
|
+
*/
|
|
87
|
+
declare function useMessages(options: UseMessagesOptions): UseMessagesReturn;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @witqq/agent-sdk/chat/react — useSessions
|
|
91
|
+
*
|
|
92
|
+
* Reactive session list hook that subscribes to runtime session changes.
|
|
93
|
+
* Auto-updates without manual polling via `onSessionChange` subscription.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/** Return type of useSessions hook. */
|
|
97
|
+
interface UseSessionsReturn {
|
|
98
|
+
/** Current session list (lightweight SessionInfo format) */
|
|
99
|
+
sessions: SessionInfo[];
|
|
100
|
+
/** Whether initial load or refresh is in progress */
|
|
101
|
+
loading: boolean;
|
|
102
|
+
/** Last error from session fetch */
|
|
103
|
+
error: Error | null;
|
|
104
|
+
/** Manually trigger a refresh */
|
|
105
|
+
refresh: () => void;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Reactive session list hook.
|
|
109
|
+
* Subscribes to `runtime.onSessionChange()` and refreshes the list automatically
|
|
110
|
+
* on create, delete, archive, and message send completion.
|
|
111
|
+
*/
|
|
112
|
+
declare function useSessions(): UseSessionsReturn;
|
|
113
|
+
|
|
114
|
+
/** Props for the Message component. */
|
|
115
|
+
interface MessageProps {
|
|
116
|
+
message: ChatMessage;
|
|
117
|
+
renderText?: (part: TextPart, index: number) => ReactNode;
|
|
118
|
+
renderReasoning?: (part: ReasoningPart, index: number) => ReactNode;
|
|
119
|
+
renderToolCall?: (part: ToolCallPart, index: number) => ReactNode;
|
|
120
|
+
renderSource?: (part: SourcePart, index: number) => ReactNode;
|
|
121
|
+
renderFile?: (part: FilePart, index: number) => ReactNode;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Headless message component rendering ChatMessage parts.
|
|
125
|
+
* Wraps parts in a div with data-role and data-status attributes.
|
|
126
|
+
*/
|
|
127
|
+
declare function Message(props: MessageProps): ReactNode;
|
|
128
|
+
|
|
129
|
+
/** Props for the ThinkingBlock component. */
|
|
130
|
+
interface ThinkingBlockProps {
|
|
131
|
+
text: string;
|
|
132
|
+
isStreaming?: boolean;
|
|
133
|
+
defaultOpen?: boolean;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Headless thinking/reasoning block using native details/summary elements.
|
|
137
|
+
* Displays "Thinking..." while streaming, "Reasoning" when complete.
|
|
138
|
+
*/
|
|
139
|
+
declare function ThinkingBlock({ text, isStreaming, defaultOpen }: ThinkingBlockProps): ReactNode;
|
|
140
|
+
|
|
141
|
+
/** Props for the ToolCallView component. */
|
|
142
|
+
interface ToolCallViewProps {
|
|
143
|
+
part: ToolCallPart;
|
|
144
|
+
onApprove?: () => void;
|
|
145
|
+
onDeny?: () => void;
|
|
146
|
+
renderArgs?: (args: unknown) => ReactNode;
|
|
147
|
+
renderResult?: (result: unknown) => ReactNode;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Headless tool call display component.
|
|
151
|
+
* Shows tool name, status, args, result, and approval buttons when needed.
|
|
152
|
+
*/
|
|
153
|
+
declare function ToolCallView({ part, onApprove, onDeny, renderArgs, renderResult }: ToolCallViewProps): ReactNode;
|
|
154
|
+
|
|
155
|
+
/** A pending tool call requiring user approval. */
|
|
156
|
+
interface PendingToolRequest {
|
|
157
|
+
toolCallId: string;
|
|
158
|
+
toolName: string;
|
|
159
|
+
toolArgs: Record<string, unknown>;
|
|
160
|
+
messageId: string;
|
|
161
|
+
}
|
|
162
|
+
/** Return value from useToolApproval. */
|
|
163
|
+
interface UseToolApprovalReturn {
|
|
164
|
+
pendingRequests: PendingToolRequest[];
|
|
165
|
+
approve: (toolCallId: string) => void;
|
|
166
|
+
deny: (toolCallId: string) => void;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Hook that tracks tool calls requiring approval from messages.
|
|
170
|
+
*
|
|
171
|
+
* Scans messages for ToolCallParts with status "requires_approval"
|
|
172
|
+
* and provides approve/deny callbacks. Currently state-only
|
|
173
|
+
* (no ChatEventBus integration).
|
|
174
|
+
*
|
|
175
|
+
* @param messages - Messages to scan for pending tool approvals
|
|
176
|
+
* @param onApprove - Called when a tool call is approved
|
|
177
|
+
* @param onDeny - Called when a tool call is denied
|
|
178
|
+
*/
|
|
179
|
+
declare function useToolApproval(messages: ChatMessage[], onApprove?: (toolCallId: string) => void, onDeny?: (toolCallId: string) => void): UseToolApprovalReturn;
|
|
180
|
+
|
|
181
|
+
/** Props for the MarkdownRenderer component. */
|
|
182
|
+
interface MarkdownRendererProps {
|
|
183
|
+
content: string;
|
|
184
|
+
renderCode?: (code: string, language?: string) => ReactNode;
|
|
185
|
+
renderLink?: (href: string, text: string) => ReactNode;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Headless markdown renderer.
|
|
189
|
+
* Parses markdown text to semantic HTML elements via createElement.
|
|
190
|
+
* Supports headings, paragraphs, bold, italic, inline code, code blocks,
|
|
191
|
+
* links, blockquotes, and lists. No external dependencies.
|
|
192
|
+
*/
|
|
193
|
+
declare function MarkdownRenderer(props: MarkdownRendererProps): ReactNode;
|
|
194
|
+
|
|
195
|
+
/** Props for the Thread component. */
|
|
196
|
+
interface ThreadProps {
|
|
197
|
+
messages: ChatMessage[];
|
|
198
|
+
isGenerating?: boolean;
|
|
199
|
+
autoScroll?: boolean;
|
|
200
|
+
className?: string;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Headless thread component wrapping a scrollable message list.
|
|
204
|
+
* Auto-scrolls to bottom when new messages arrive unless user has scrolled up.
|
|
205
|
+
*/
|
|
206
|
+
declare function Thread({ messages, isGenerating, autoScroll, className, }: ThreadProps): ReactNode;
|
|
207
|
+
|
|
208
|
+
/** Props for the Composer component. */
|
|
209
|
+
interface ComposerProps {
|
|
210
|
+
onSend: (text: string) => void;
|
|
211
|
+
onStop?: () => void;
|
|
212
|
+
isGenerating?: boolean;
|
|
213
|
+
disabled?: boolean;
|
|
214
|
+
placeholder?: string;
|
|
215
|
+
maxRows?: number;
|
|
216
|
+
className?: string;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Headless composer component for sending messages.
|
|
220
|
+
* Includes auto-resizing textarea, send/stop buttons, and keyboard shortcuts.
|
|
221
|
+
*/
|
|
222
|
+
declare function Composer({ onSend, onStop, isGenerating, disabled, placeholder, maxRows, className, }: ComposerProps): ReactNode;
|
|
223
|
+
|
|
224
|
+
/** Slot override functions for Thread customization. */
|
|
225
|
+
interface ThreadSlotOverrides {
|
|
226
|
+
renderMessage?: (message: ChatMessage, index: number) => ReactNode;
|
|
227
|
+
renderToolCall?: (part: ToolCallPart, index: number) => ReactNode;
|
|
228
|
+
renderThinkingBlock?: (part: ReasoningPart, index: number) => ReactNode;
|
|
229
|
+
}
|
|
230
|
+
/** Props for ThreadProvider. */
|
|
231
|
+
interface ThreadProviderProps extends ThreadSlotOverrides {
|
|
232
|
+
children: ReactNode;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Provides slot-based customization for Thread message rendering.
|
|
236
|
+
* Wrap a Thread in ThreadProvider to override how messages, tool calls,
|
|
237
|
+
* or thinking blocks are rendered.
|
|
238
|
+
*/
|
|
239
|
+
declare function ThreadProvider({ children, renderMessage, renderToolCall, renderThinkingBlock, }: ThreadProviderProps): ReactNode;
|
|
240
|
+
/**
|
|
241
|
+
* Access slot overrides from ThreadProvider context.
|
|
242
|
+
* @throws {Error} If used outside a ThreadProvider
|
|
243
|
+
*/
|
|
244
|
+
declare function useThreadSlots(): ThreadSlotOverrides;
|
|
245
|
+
/**
|
|
246
|
+
* Access slot overrides if inside a ThreadProvider, or null if not.
|
|
247
|
+
* Safe to call without a ThreadProvider ancestor.
|
|
248
|
+
*/
|
|
249
|
+
declare function useOptionalThreadSlots(): ThreadSlotOverrides | null;
|
|
250
|
+
|
|
251
|
+
/** A session item that ThreadList can display — either full ChatSession or lightweight SessionInfo. */
|
|
252
|
+
type SessionItem = SessionInfo | ChatSession;
|
|
253
|
+
/** Props for the ThreadList component. */
|
|
254
|
+
interface ThreadListProps {
|
|
255
|
+
sessions: SessionItem[];
|
|
256
|
+
activeSessionId?: string;
|
|
257
|
+
onSelect: (id: string) => void;
|
|
258
|
+
onCreate?: () => void;
|
|
259
|
+
onDelete?: (id: string) => void;
|
|
260
|
+
searchQuery?: string;
|
|
261
|
+
onSearchChange?: (query: string) => void;
|
|
262
|
+
className?: string;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Session sidebar component for listing, searching, creating, and deleting sessions.
|
|
266
|
+
*/
|
|
267
|
+
declare function ThreadList({ sessions, activeSessionId, onSelect, onCreate, onDelete, searchQuery, onSearchChange, className, }: ThreadListProps): ReactNode;
|
|
268
|
+
|
|
269
|
+
/** Connection status of the SSE hook. */
|
|
270
|
+
type SSEStatus = "idle" | "connecting" | "open" | "closed" | "error";
|
|
271
|
+
/** Options for the useSSE hook. */
|
|
272
|
+
interface UseSSEOptions {
|
|
273
|
+
headers?: Record<string, string>;
|
|
274
|
+
onEvent?: (event: ChatEvent) => void;
|
|
275
|
+
onError?: (error: Error) => void;
|
|
276
|
+
reconnect?: boolean;
|
|
277
|
+
reconnectInterval?: number;
|
|
278
|
+
}
|
|
279
|
+
/** Return type for the useSSE hook. */
|
|
280
|
+
interface UseSSEReturn {
|
|
281
|
+
status: SSEStatus;
|
|
282
|
+
connect: () => void;
|
|
283
|
+
disconnect: () => void;
|
|
284
|
+
lastEvent: ChatEvent | null;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* SSE transport hook using fetch (not EventSource).
|
|
288
|
+
* Parses text/event-stream format with support for multi-line data and event types.
|
|
289
|
+
*/
|
|
290
|
+
declare function useSSE(url: string | null, options?: UseSSEOptions): UseSSEReturn;
|
|
291
|
+
|
|
292
|
+
/** Model display option returned by useModels (mapped from core ModelInfo). */
|
|
293
|
+
interface ModelOption {
|
|
294
|
+
id: string;
|
|
295
|
+
name: string;
|
|
296
|
+
tier?: string;
|
|
297
|
+
}
|
|
298
|
+
/** @deprecated Use ModelOption instead — renamed to avoid collision with core ModelInfo */
|
|
299
|
+
type ModelInfo = ModelOption;
|
|
300
|
+
/** Return type for the useModels hook. */
|
|
301
|
+
interface UseModelsReturn {
|
|
302
|
+
models: ModelOption[];
|
|
303
|
+
isLoading: boolean;
|
|
304
|
+
error: Error | null;
|
|
305
|
+
refresh: () => void;
|
|
306
|
+
search: (query: string) => ModelOption[];
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Hook for fetching and searching available models from the chat runtime.
|
|
310
|
+
*/
|
|
311
|
+
declare function useModels(): UseModelsReturn;
|
|
312
|
+
|
|
313
|
+
/** Props for the ModelSelector component. */
|
|
314
|
+
interface ModelSelectorProps {
|
|
315
|
+
models: ModelOption[];
|
|
316
|
+
selectedModel?: string;
|
|
317
|
+
onSelect: (modelId: string) => void;
|
|
318
|
+
placeholder?: string;
|
|
319
|
+
className?: string;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Dropdown model selector with search and keyboard navigation.
|
|
323
|
+
*/
|
|
324
|
+
declare function ModelSelector({ models, selectedModel, onSelect, placeholder, className, }: ModelSelectorProps): ReactNode;
|
|
325
|
+
|
|
326
|
+
/** Supported auth backends. */
|
|
327
|
+
type AuthBackend = "copilot" | "claude" | "api-key";
|
|
328
|
+
/** Auth status state machine: idle → pending → authenticated | error. */
|
|
329
|
+
type AuthStatus = "idle" | "pending" | "authenticated" | "error";
|
|
330
|
+
/** Options for the useAuth hook. */
|
|
331
|
+
interface UseAuthOptions {
|
|
332
|
+
backend: AuthBackend;
|
|
333
|
+
onAuthenticated?: (token: AuthToken) => void;
|
|
334
|
+
}
|
|
335
|
+
/** Return value from useAuth. */
|
|
336
|
+
interface UseAuthReturn {
|
|
337
|
+
status: AuthStatus;
|
|
338
|
+
error: Error | null;
|
|
339
|
+
startDeviceFlow: () => Promise<void>;
|
|
340
|
+
deviceCode: string | null;
|
|
341
|
+
verificationUrl: string | null;
|
|
342
|
+
startOAuthFlow: () => Promise<void>;
|
|
343
|
+
authorizeUrl: string | null;
|
|
344
|
+
completeOAuth: (codeOrUrl: string) => Promise<void>;
|
|
345
|
+
submitApiKey: (key: string) => void;
|
|
346
|
+
token: AuthToken | null;
|
|
347
|
+
reset: () => void;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Hook for multi-backend authentication.
|
|
351
|
+
*
|
|
352
|
+
* Wraps CopilotAuth (Device Flow), ClaudeAuth (OAuth+PKCE),
|
|
353
|
+
* and plain API key validation into a unified React state machine.
|
|
354
|
+
*
|
|
355
|
+
* @deprecated Use `useRemoteAuth` instead for browser contexts.
|
|
356
|
+
* `useAuth` directly instantiates auth classes which require `node:crypto`
|
|
357
|
+
* (via ClaudeAuth). The `useRemoteAuth` hook delegates authentication to
|
|
358
|
+
* server endpoints (createAuthHandler), avoiding browser crypto dependencies.
|
|
359
|
+
*
|
|
360
|
+
* Migration: replace `useAuth({ backend })` with
|
|
361
|
+
* `useRemoteAuth({ backend, baseUrl: "/api/auth" })`.
|
|
362
|
+
* Note: the `"api-key"` backend is renamed to `"vercel-ai"` in useRemoteAuth,
|
|
363
|
+
* and `submitApiKey` is now async (returns Promise<void>).
|
|
364
|
+
* The return interface is otherwise compatible — `startDeviceFlow`, `startOAuthFlow`,
|
|
365
|
+
* `completeOAuth`, `submitApiKey`, `token`, `status`, `error`, `reset`
|
|
366
|
+
* all work the same way.
|
|
367
|
+
*/
|
|
368
|
+
declare function useAuth(options: UseAuthOptions): UseAuthReturn;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* @witqq/agent-sdk/chat/react — useRemoteAuth
|
|
372
|
+
*
|
|
373
|
+
* Server-delegated authentication hook. Delegates all auth flows to
|
|
374
|
+
* the server auth handler (createAuthHandler) via HTTP, avoiding
|
|
375
|
+
* browser crypto dependencies entirely.
|
|
376
|
+
*
|
|
377
|
+
* Replaces useAuth for browser contexts. Same interface shape for easy migration.
|
|
378
|
+
*/
|
|
379
|
+
|
|
380
|
+
/** Supported remote auth backends. */
|
|
381
|
+
type RemoteAuthBackend = "copilot" | "claude" | "vercel-ai";
|
|
382
|
+
/** Auth status state machine: idle → pending → authenticated | error. */
|
|
383
|
+
type RemoteAuthStatus = "idle" | "pending" | "authenticated" | "error";
|
|
384
|
+
/** Options for the useRemoteAuth hook. */
|
|
385
|
+
interface UseRemoteAuthOptions {
|
|
386
|
+
/** Auth backend to use */
|
|
387
|
+
backend: RemoteAuthBackend;
|
|
388
|
+
/** Base URL of the auth server (e.g. "http://localhost:3456/api/auth") */
|
|
389
|
+
baseUrl: string;
|
|
390
|
+
/** Called after successful authentication */
|
|
391
|
+
onAuthenticated?: (token: AuthToken) => void;
|
|
392
|
+
/** Optional fetch override (for testing) */
|
|
393
|
+
fetch?: typeof globalThis.fetch;
|
|
394
|
+
/** Optional headers for all requests */
|
|
395
|
+
headers?: Record<string, string>;
|
|
396
|
+
}
|
|
397
|
+
/** Return value from useRemoteAuth. */
|
|
398
|
+
interface UseRemoteAuthReturn {
|
|
399
|
+
status: RemoteAuthStatus;
|
|
400
|
+
error: Error | null;
|
|
401
|
+
startDeviceFlow: () => Promise<void>;
|
|
402
|
+
deviceCode: string | null;
|
|
403
|
+
verificationUrl: string | null;
|
|
404
|
+
startOAuthFlow: () => Promise<void>;
|
|
405
|
+
authorizeUrl: string | null;
|
|
406
|
+
completeOAuth: (codeOrUrl: string) => Promise<void>;
|
|
407
|
+
submitApiKey: (key: string, baseUrl?: string) => Promise<void>;
|
|
408
|
+
start: (provider?: RemoteAuthBackend) => Promise<void>;
|
|
409
|
+
token: AuthToken | null;
|
|
410
|
+
reset: () => void;
|
|
411
|
+
savedProviders: string[];
|
|
412
|
+
loadSavedTokens: () => Promise<void>;
|
|
413
|
+
useSavedToken: (provider: RemoteAuthBackend) => Promise<void>;
|
|
414
|
+
clearTokens: () => Promise<void>;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Server-delegated authentication hook.
|
|
418
|
+
*
|
|
419
|
+
* Communicates with server auth handler endpoints (POST /auth/start,
|
|
420
|
+
* POST /auth/copilot/poll, etc.) instead of running auth flows in the browser.
|
|
421
|
+
* No node:crypto dependency since all crypto operations happen server-side.
|
|
422
|
+
*
|
|
423
|
+
* @param options - Hook configuration
|
|
424
|
+
* @returns Auth state and action methods
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* ```ts
|
|
428
|
+
* const auth = useRemoteAuth({
|
|
429
|
+
* backend: "copilot",
|
|
430
|
+
* baseUrl: "/api/auth",
|
|
431
|
+
* onAuthenticated: (token) => console.log("Authenticated:", token),
|
|
432
|
+
* });
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
declare function useRemoteAuth(options: UseRemoteAuthOptions): UseRemoteAuthReturn;
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* @witqq/agent-sdk/chat/react — useRemoteChat
|
|
439
|
+
*
|
|
440
|
+
* Lifecycle hook that orchestrates auth → runtime → session.
|
|
441
|
+
*
|
|
442
|
+
* On mount, checks for saved auth tokens and auto-restores.
|
|
443
|
+
* Once authenticated, creates a RemoteChatRuntime and initial session.
|
|
444
|
+
* Exposes readiness phase, the runtime instance, and the auth sub-hook.
|
|
445
|
+
*/
|
|
446
|
+
|
|
447
|
+
/** Lifecycle phase for the useRemoteChat hook. */
|
|
448
|
+
type RemoteChatPhase = "initializing" | "unauthenticated" | "authenticating" | "creating" | "ready" | "error";
|
|
449
|
+
/** Options for useRemoteChat. */
|
|
450
|
+
interface UseRemoteChatOptions {
|
|
451
|
+
/** Base URL for chat API (e.g. "/api/chat"). */
|
|
452
|
+
chatBaseUrl: string;
|
|
453
|
+
/** Base URL for auth API (e.g. "/api/auth"). */
|
|
454
|
+
authBaseUrl: string;
|
|
455
|
+
/** Auth backend to use. */
|
|
456
|
+
backend: RemoteAuthBackend;
|
|
457
|
+
/** Called when lifecycle reaches "ready" phase. */
|
|
458
|
+
onReady?: () => void;
|
|
459
|
+
/** Custom fetch for testability. */
|
|
460
|
+
fetch?: typeof globalThis.fetch;
|
|
461
|
+
/** Optional headers for all requests. */
|
|
462
|
+
headers?: Record<string, string>;
|
|
463
|
+
/** Auto-restore saved tokens on mount (default: true). */
|
|
464
|
+
autoRestore?: boolean;
|
|
465
|
+
}
|
|
466
|
+
/** Return value from useRemoteChat. */
|
|
467
|
+
interface UseRemoteChatReturn {
|
|
468
|
+
/** Current lifecycle phase. */
|
|
469
|
+
phase: RemoteChatPhase;
|
|
470
|
+
/** Chat runtime (null until phase = "ready"). */
|
|
471
|
+
runtime: IChatRuntime | null;
|
|
472
|
+
/** Initial session ID (null until phase = "ready"). */
|
|
473
|
+
sessionId: string | null;
|
|
474
|
+
/** Auth sub-hook for manual auth control. */
|
|
475
|
+
auth: UseRemoteAuthReturn;
|
|
476
|
+
/** Current error (null when no error). */
|
|
477
|
+
error: Error | null;
|
|
478
|
+
/** Create a new chat session. Returns session ID. */
|
|
479
|
+
newSession: () => Promise<string>;
|
|
480
|
+
/** Logout: clear tokens, dispose runtime, reset to unauthenticated. */
|
|
481
|
+
logout: () => Promise<void>;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Lifecycle hook: auth → runtime → session.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```tsx
|
|
488
|
+
* const chat = useRemoteChat({
|
|
489
|
+
* chatBaseUrl: "/api/chat",
|
|
490
|
+
* authBaseUrl: "/api/auth",
|
|
491
|
+
* backend: "copilot",
|
|
492
|
+
* });
|
|
493
|
+
*
|
|
494
|
+
* if (chat.phase === "unauthenticated") {
|
|
495
|
+
* return <button onClick={() => chat.auth.start()}>Login</button>;
|
|
496
|
+
* }
|
|
497
|
+
* if (chat.phase === "ready" && chat.runtime) {
|
|
498
|
+
* return <ChatProvider runtime={chat.runtime}>...</ChatProvider>;
|
|
499
|
+
* }
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
declare function useRemoteChat(options: UseRemoteChatOptions): UseRemoteChatReturn;
|
|
503
|
+
|
|
504
|
+
/** Props for the AuthDialog component. */
|
|
505
|
+
interface AuthDialogProps {
|
|
506
|
+
backends: AuthBackend[];
|
|
507
|
+
selectedBackend?: AuthBackend;
|
|
508
|
+
onBackendChange?: (backend: AuthBackend) => void;
|
|
509
|
+
onAuthenticated?: (token: AuthToken) => void;
|
|
510
|
+
renderCopilotFlow?: (state: {
|
|
511
|
+
deviceCode: string;
|
|
512
|
+
verificationUrl: string;
|
|
513
|
+
status: AuthStatus;
|
|
514
|
+
}) => ReactNode;
|
|
515
|
+
renderClaudeFlow?: (state: {
|
|
516
|
+
authorizeUrl: string | null;
|
|
517
|
+
status: AuthStatus;
|
|
518
|
+
completeOAuth: (code: string) => Promise<void>;
|
|
519
|
+
}) => ReactNode;
|
|
520
|
+
renderApiKeyFlow?: (state: {
|
|
521
|
+
submitApiKey: (key: string) => void;
|
|
522
|
+
status: AuthStatus;
|
|
523
|
+
}) => ReactNode;
|
|
524
|
+
className?: string;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Multi-backend auth dialog component.
|
|
528
|
+
*
|
|
529
|
+
* Headless component that renders backend selector buttons and
|
|
530
|
+
* per-backend auth flows. Uses data attributes for styling hooks.
|
|
531
|
+
* Supports custom render props for each flow type.
|
|
532
|
+
*/
|
|
533
|
+
declare function AuthDialog({ backends, selectedBackend: controlledBackend, onBackendChange, onAuthenticated, renderCopilotFlow, renderClaudeFlow, renderApiKeyFlow, className, }: AuthDialogProps): ReactNode;
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Client-side runtime adapter that implements IChatRuntime by delegating
|
|
537
|
+
* operations over HTTP/SSE to a remote server.
|
|
538
|
+
*
|
|
539
|
+
* Bridges the gap between SDK React hooks (which require an in-process runtime)
|
|
540
|
+
* and the common architecture where ChatRuntime runs on a server.
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* ```ts
|
|
544
|
+
* import { RemoteChatRuntime } from "@witqq/agent-sdk/chat/react";
|
|
545
|
+
* import { ChatProvider } from "@witqq/agent-sdk/chat/react";
|
|
546
|
+
*
|
|
547
|
+
* const runtime = new RemoteChatRuntime({ baseUrl: "/api" });
|
|
548
|
+
* <ChatProvider runtime={runtime}> ... </ChatProvider>
|
|
549
|
+
* ```
|
|
550
|
+
*/
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Standard server endpoint contract.
|
|
554
|
+
* Server implementations expose these routes to work with RemoteChatRuntime.
|
|
555
|
+
*
|
|
556
|
+
* POST {baseUrl}/sessions/create — Create session
|
|
557
|
+
* GET {baseUrl}/sessions/{id} — Get session
|
|
558
|
+
* GET {baseUrl}/sessions — List sessions
|
|
559
|
+
* DELETE {baseUrl}/sessions/{id} — Delete session
|
|
560
|
+
* POST {baseUrl}/sessions/{id}/archive — Archive session
|
|
561
|
+
* POST {baseUrl}/send — Send message (SSE stream response)
|
|
562
|
+
* POST {baseUrl}/abort — Abort current stream
|
|
563
|
+
* GET {baseUrl}/models — List models
|
|
564
|
+
* POST {baseUrl}/backend/switch — Switch backend
|
|
565
|
+
* POST {baseUrl}/model/switch — Switch model
|
|
566
|
+
*/
|
|
567
|
+
interface RemoteChatRuntimeOptions {
|
|
568
|
+
/** Base URL for API endpoints (e.g. "/api" or "https://example.com/api") */
|
|
569
|
+
baseUrl: string;
|
|
570
|
+
/** Optional headers for all requests (e.g. auth tokens) */
|
|
571
|
+
headers?: Record<string, string>;
|
|
572
|
+
/** Custom fetch implementation for testability */
|
|
573
|
+
fetch?: typeof globalThis.fetch;
|
|
574
|
+
}
|
|
575
|
+
declare class RemoteChatRuntime implements IChatRuntime {
|
|
576
|
+
private _status;
|
|
577
|
+
private _activeSessionId;
|
|
578
|
+
private _currentBackend;
|
|
579
|
+
private _currentModel;
|
|
580
|
+
private _abortController;
|
|
581
|
+
private readonly _tools;
|
|
582
|
+
private readonly _middlewares;
|
|
583
|
+
private readonly baseUrl;
|
|
584
|
+
private readonly headers;
|
|
585
|
+
private readonly _fetch;
|
|
586
|
+
constructor(options: RemoteChatRuntimeOptions);
|
|
587
|
+
get status(): RuntimeStatus;
|
|
588
|
+
dispose(): Promise<void>;
|
|
589
|
+
private assertNotDisposed;
|
|
590
|
+
get activeSessionId(): ChatId | null;
|
|
591
|
+
createSession(options: CreateSessionOptions): Promise<ChatSession>;
|
|
592
|
+
getSession(id: ChatIdLike): Promise<ChatSession | null>;
|
|
593
|
+
listSessions(_options?: SessionListOptions): Promise<ChatSession[]>;
|
|
594
|
+
deleteSession(id: ChatIdLike): Promise<void>;
|
|
595
|
+
archiveSession(id: ChatIdLike): Promise<void>;
|
|
596
|
+
switchSession(id: ChatIdLike): Promise<ChatSession>;
|
|
597
|
+
send(sessionId: ChatIdLike, message: string, options?: SendMessageOptions): AsyncIterable<ChatEvent>;
|
|
598
|
+
abort(): void;
|
|
599
|
+
get currentBackend(): string;
|
|
600
|
+
get currentModel(): string | undefined;
|
|
601
|
+
switchBackend(name: string): Promise<void>;
|
|
602
|
+
switchModel(model: string): void;
|
|
603
|
+
listModels(): Promise<ModelInfo$1[]>;
|
|
604
|
+
get registeredTools(): ReadonlyMap<string, ToolDefinition>;
|
|
605
|
+
registerTool(tool: ToolDefinition): void;
|
|
606
|
+
removeTool(name: string): void;
|
|
607
|
+
use(middleware: ChatMiddleware): void;
|
|
608
|
+
removeMiddleware(middleware: ChatMiddleware): void;
|
|
609
|
+
getContextStats(_sessionId: ChatIdLike): ContextStats | null;
|
|
610
|
+
private readonly _sessionListeners;
|
|
611
|
+
onSessionChange(callback: () => void): () => void;
|
|
612
|
+
private _notifySessionChange;
|
|
613
|
+
private _get;
|
|
614
|
+
private _post;
|
|
615
|
+
private _delete;
|
|
616
|
+
private _parseSSEStream;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
export { type AuthBackend, AuthDialog, type AuthDialogProps, type AuthStatus, ChatProvider, Composer, type ComposerProps, MarkdownRenderer, type MarkdownRendererProps, Message, type MessageProps, type ModelInfo, type ModelOption, ModelSelector, type ModelSelectorProps, type PendingToolRequest, type RemoteAuthBackend, type RemoteAuthStatus, type RemoteChatPhase, RemoteChatRuntime, type RemoteChatRuntimeOptions, type SSEStatus, ThinkingBlock, type ThinkingBlockProps, Thread, ThreadList, type ThreadListProps, type ThreadProps, ThreadProvider, type ThreadProviderProps, type ThreadSlotOverrides, ToolCallView, type ToolCallViewProps, type UseAuthOptions, type UseAuthReturn, type UseChatOptions, type UseChatReturn, type UseMessagesOptions, type UseMessagesReturn, type UseModelsReturn, type UseRemoteAuthOptions, type UseRemoteAuthReturn, type UseRemoteChatOptions, type UseRemoteChatReturn, type UseSSEOptions, type UseSSEReturn, type UseSessionsReturn, type UseToolApprovalReturn, useAuth, useChat, useChatRuntime, useMessages, useModels, useOptionalThreadSlots, useRemoteAuth, useRemoteChat, useSSE, useSessions, useThreadSlots, useToolApproval };
|