@witqq/agent-sdk 0.6.0 → 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 +341 -22
- 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 +341 -22
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +133 -25
- 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 +133 -25
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/vercel-ai.cjs +66 -19
- 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 +66 -19
- 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,287 @@
|
|
|
1
|
+
import { IChatRuntime } from './runtime.js';
|
|
2
|
+
import { W as WritableResponse$1 } from '../transport-D1OaUgRk.js';
|
|
3
|
+
import { A as AuthToken, d as CopilotAuthToken, C as ClaudeAuthToken } from '../types-Bh5AhqD-.js';
|
|
4
|
+
import './core.js';
|
|
5
|
+
import '../types-CqvUAYxt.js';
|
|
6
|
+
import 'zod';
|
|
7
|
+
import '../types-DLZzlJxt.js';
|
|
8
|
+
import './sessions.js';
|
|
9
|
+
import './storage.js';
|
|
10
|
+
import '../errors-BDLbNu9w.js';
|
|
11
|
+
import './context.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* createChatHandler — maps RemoteChatRuntime contract endpoints to IChatRuntime calls.
|
|
15
|
+
*
|
|
16
|
+
* Implements all 10 routes from the RemoteChatRuntime server endpoint contract:
|
|
17
|
+
* - POST /sessions/create → runtime.createSession()
|
|
18
|
+
* - GET /sessions/{id} → runtime.getSession()
|
|
19
|
+
* - GET /sessions → runtime.listSessions()
|
|
20
|
+
* - DELETE /sessions/{id} → runtime.deleteSession()
|
|
21
|
+
* - POST /sessions/{id}/archive → runtime.archiveSession()
|
|
22
|
+
* - POST /send → runtime.send() via SSE
|
|
23
|
+
* - POST /abort → runtime.abort()
|
|
24
|
+
* - GET /models → runtime.listModels()
|
|
25
|
+
* - POST /backend/switch → runtime.switchBackend()
|
|
26
|
+
* - POST /model/switch → runtime.switchModel()
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** Minimal readable request interface (node:http IncomingMessage subset) */
|
|
30
|
+
interface ReadableRequest {
|
|
31
|
+
readonly method?: string;
|
|
32
|
+
readonly url?: string;
|
|
33
|
+
on(event: "data", listener: (chunk: Buffer | string) => void): void;
|
|
34
|
+
on(event: "end", listener: () => void): void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Writable HTTP response interface for chat handler.
|
|
38
|
+
* Extends the transport's WritableResponse with setHeader() and body-accepting end().
|
|
39
|
+
*/
|
|
40
|
+
interface WritableResponse extends WritableResponse$1 {
|
|
41
|
+
setHeader(name: string, value: string): void;
|
|
42
|
+
end(body?: string): void;
|
|
43
|
+
}
|
|
44
|
+
/** Configuration for createChatHandler */
|
|
45
|
+
interface ChatHandlerOptions {
|
|
46
|
+
/** Route prefix to strip from URL before matching. Default: "" (no prefix) */
|
|
47
|
+
prefix?: string;
|
|
48
|
+
/** Maximum request body size in bytes. Default: 1MB (1048576) */
|
|
49
|
+
maxBodySize?: number;
|
|
50
|
+
/** SSE heartbeat interval in milliseconds. 0 or undefined disables heartbeat. */
|
|
51
|
+
heartbeatMs?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create an HTTP request handler that maps RemoteChatRuntime contract
|
|
55
|
+
* endpoints to IChatRuntime method calls.
|
|
56
|
+
*
|
|
57
|
+
* @param runtime - The chat runtime instance to serve
|
|
58
|
+
* @param options - Handler configuration
|
|
59
|
+
* @returns Async request handler `(req, res) => Promise<void>`
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const handler = createChatHandler(runtime, { prefix: "/api/chat" });
|
|
64
|
+
* http.createServer(async (req, res) => {
|
|
65
|
+
* if (req.url?.startsWith("/api/chat")) {
|
|
66
|
+
* await handler(req, res);
|
|
67
|
+
* return;
|
|
68
|
+
* }
|
|
69
|
+
* res.writeHead(404).end();
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function createChatHandler(runtime: IChatRuntime, options?: ChatHandlerOptions): (req: ReadableRequest, res: WritableResponse) => Promise<void>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Token storage abstraction and default filesystem implementation.
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
/** Token storage interface for server-side token management */
|
|
80
|
+
interface ITokenStore {
|
|
81
|
+
/** Save a token for a provider. Overwrites if exists. */
|
|
82
|
+
save(provider: string, token: AuthToken): Promise<void>;
|
|
83
|
+
/** Load a previously saved token. Returns null if not found. */
|
|
84
|
+
load(provider: string): Promise<AuthToken | null>;
|
|
85
|
+
/** Remove a specific provider's token. */
|
|
86
|
+
clear(provider: string): Promise<void>;
|
|
87
|
+
/** Remove all stored tokens. */
|
|
88
|
+
clearAll(): Promise<void>;
|
|
89
|
+
/** List provider names that have saved tokens. */
|
|
90
|
+
list(): Promise<string[]>;
|
|
91
|
+
}
|
|
92
|
+
/** In-memory token store for testing and ephemeral use */
|
|
93
|
+
declare class InMemoryTokenStore implements ITokenStore {
|
|
94
|
+
private readonly tokens;
|
|
95
|
+
save(provider: string, token: AuthToken): Promise<void>;
|
|
96
|
+
load(provider: string): Promise<AuthToken | null>;
|
|
97
|
+
clear(provider: string): Promise<void>;
|
|
98
|
+
clearAll(): Promise<void>;
|
|
99
|
+
list(): Promise<string[]>;
|
|
100
|
+
}
|
|
101
|
+
/** Options for FileTokenStore */
|
|
102
|
+
interface FileTokenStoreOptions {
|
|
103
|
+
/** Directory to store token JSON files. Default: ".tokens" in cwd */
|
|
104
|
+
directory: string;
|
|
105
|
+
}
|
|
106
|
+
/** Filesystem-based token store using JSON files (one per provider) */
|
|
107
|
+
declare class FileTokenStore implements ITokenStore {
|
|
108
|
+
private readonly dir;
|
|
109
|
+
constructor(options: FileTokenStoreOptions);
|
|
110
|
+
save(provider: string, token: AuthToken): Promise<void>;
|
|
111
|
+
load(provider: string): Promise<AuthToken | null>;
|
|
112
|
+
clear(provider: string): Promise<void>;
|
|
113
|
+
clearAll(): Promise<void>;
|
|
114
|
+
list(): Promise<string[]>;
|
|
115
|
+
private filePath;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* createAuthHandler — server-mediated authentication for all three backends.
|
|
120
|
+
*
|
|
121
|
+
* Routes:
|
|
122
|
+
* - POST /auth/start → Start auth flow (copilot device flow, claude OAuth, vercel-ai passthrough)
|
|
123
|
+
* - POST /auth/copilot/poll → Poll for Copilot device flow completion
|
|
124
|
+
* - POST /auth/claude/complete → Exchange Claude OAuth code for token
|
|
125
|
+
* - POST /auth/vercel/complete → Submit Vercel AI API key
|
|
126
|
+
* - GET /tokens/saved → List providers with saved tokens
|
|
127
|
+
* - POST /tokens/use → Load and activate a saved token
|
|
128
|
+
* - POST /tokens/clear → Clear all saved tokens and invoke onLogout
|
|
129
|
+
* - POST /auth/dispose → Clear pending flows and invoke onLogout
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/** Auth provider names recognized by the handler */
|
|
133
|
+
type AuthProvider = "copilot" | "claude" | "vercel-ai";
|
|
134
|
+
/** Copilot auth class interface (matches CopilotAuth public API) */
|
|
135
|
+
interface ICopilotAuth {
|
|
136
|
+
startDeviceFlow(options?: {
|
|
137
|
+
scopes?: string;
|
|
138
|
+
signal?: AbortSignal;
|
|
139
|
+
}): Promise<{
|
|
140
|
+
userCode: string;
|
|
141
|
+
verificationUrl: string;
|
|
142
|
+
waitForToken: (signal?: AbortSignal) => Promise<CopilotAuthToken>;
|
|
143
|
+
}>;
|
|
144
|
+
}
|
|
145
|
+
/** Claude auth class interface (matches ClaudeAuth public API) */
|
|
146
|
+
interface IClaudeAuth {
|
|
147
|
+
startOAuthFlow(options?: {
|
|
148
|
+
redirectUri?: string;
|
|
149
|
+
scopes?: string;
|
|
150
|
+
}): {
|
|
151
|
+
authorizeUrl: string;
|
|
152
|
+
completeAuth: (codeOrUrl: string) => Promise<ClaudeAuthToken>;
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** Callback invoked after successful authentication */
|
|
156
|
+
type OnAuthCallback = (provider: AuthProvider, token: AuthToken) => void | Promise<void>;
|
|
157
|
+
/** Configuration for createAuthHandler */
|
|
158
|
+
interface AuthHandlerOptions {
|
|
159
|
+
/** Token storage implementation */
|
|
160
|
+
tokenStore: ITokenStore;
|
|
161
|
+
/** Factory for creating CopilotAuth instances */
|
|
162
|
+
createCopilotAuth?: () => ICopilotAuth;
|
|
163
|
+
/** Factory for creating ClaudeAuth instances */
|
|
164
|
+
createClaudeAuth?: () => IClaudeAuth;
|
|
165
|
+
/** Called after successful authentication for any provider */
|
|
166
|
+
onAuth?: OnAuthCallback;
|
|
167
|
+
/** Called when dispose/logout is requested */
|
|
168
|
+
onLogout?: () => void | Promise<void>;
|
|
169
|
+
/** Route prefix to strip from URL before matching. Default: "" */
|
|
170
|
+
prefix?: string;
|
|
171
|
+
/** Maximum request body size in bytes. Default: 1MB */
|
|
172
|
+
maxBodySize?: number;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create an HTTP request handler for server-mediated authentication.
|
|
176
|
+
*
|
|
177
|
+
* @param options - Auth handler configuration (token store, auth factories, callbacks)
|
|
178
|
+
* @returns Async request handler `(req, res) => Promise<void>`
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* import { CopilotAuth, ClaudeAuth } from "@witqq/agent-sdk/auth";
|
|
183
|
+
*
|
|
184
|
+
* const authHandler = createAuthHandler({
|
|
185
|
+
* tokenStore: new FileTokenStore({ directory: ".tokens" }),
|
|
186
|
+
* createCopilotAuth: () => new CopilotAuth(),
|
|
187
|
+
* createClaudeAuth: () => new ClaudeAuth(),
|
|
188
|
+
* onAuth: (provider, token) => {
|
|
189
|
+
* // Rebuild runtime with new credentials
|
|
190
|
+
* },
|
|
191
|
+
* });
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
declare function createAuthHandler(options: AuthHandlerOptions): (req: ReadableRequest, res: WritableResponse) => Promise<void>;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* CORS middleware — standalone composable function.
|
|
198
|
+
* Returns true if it fully handled the request (OPTIONS preflight),
|
|
199
|
+
* false if the caller should continue processing.
|
|
200
|
+
*/
|
|
201
|
+
/** Configuration for CORS middleware */
|
|
202
|
+
interface CorsOptions {
|
|
203
|
+
/** Allowed origins. Default: "*" (any origin) */
|
|
204
|
+
origin?: string | string[];
|
|
205
|
+
/** Allowed HTTP methods. Default: common REST methods */
|
|
206
|
+
methods?: string[];
|
|
207
|
+
/** Allowed request headers. Default: ["Content-Type"] */
|
|
208
|
+
headers?: string[];
|
|
209
|
+
/** Max age for preflight cache in seconds. Default: 86400 (24h) */
|
|
210
|
+
maxAge?: number;
|
|
211
|
+
}
|
|
212
|
+
/** Minimal request interface for CORS */
|
|
213
|
+
interface CorsRequest {
|
|
214
|
+
readonly method?: string;
|
|
215
|
+
readonly headers?: Record<string, string | string[] | undefined>;
|
|
216
|
+
}
|
|
217
|
+
/** Minimal response interface for CORS */
|
|
218
|
+
interface CorsResponse {
|
|
219
|
+
setHeader(name: string, value: string): void;
|
|
220
|
+
writeHead(statusCode: number): void;
|
|
221
|
+
end(): void;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Create a CORS middleware function.
|
|
225
|
+
*
|
|
226
|
+
* @returns A function that sets CORS headers and handles OPTIONS preflight.
|
|
227
|
+
* Returns `true` if the request was fully handled (preflight),
|
|
228
|
+
* `false` if the caller should continue processing.
|
|
229
|
+
*/
|
|
230
|
+
declare function corsMiddleware(options?: CorsOptions): (req: CorsRequest, res: CorsResponse) => boolean;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* createChatServer — one-call setup combining runtime, chat handler, auth handler, CORS,
|
|
234
|
+
* and static file serving into a single HTTP request handler.
|
|
235
|
+
*
|
|
236
|
+
* Routes:
|
|
237
|
+
* - CORS preflight (OPTIONS) on all paths
|
|
238
|
+
* - {chatPrefix}/* → createChatHandler routes
|
|
239
|
+
* - {authPrefix}/* → createAuthHandler routes (if tokenStore provided)
|
|
240
|
+
* - {staticPrefix}/* → static file serving (if staticDir provided)
|
|
241
|
+
* - Everything else → 404
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
/** Configuration for createChatServer */
|
|
245
|
+
interface ChatServerOptions {
|
|
246
|
+
/** The chat runtime instance to serve */
|
|
247
|
+
runtime: IChatRuntime;
|
|
248
|
+
/** Prefix for chat API routes. Default: "/api/chat" */
|
|
249
|
+
chatPrefix?: string;
|
|
250
|
+
/** Auth handler options. If provided, auth routes are mounted. */
|
|
251
|
+
auth?: AuthHandlerOptions;
|
|
252
|
+
/** Prefix for auth routes. Default: "/api/auth" */
|
|
253
|
+
authPrefix?: string;
|
|
254
|
+
/** CORS options. Pass false to disable CORS. Default: enabled with permissive settings */
|
|
255
|
+
cors?: CorsOptions | false;
|
|
256
|
+
/** Directory to serve static files from. Omit to disable static serving. */
|
|
257
|
+
staticDir?: string;
|
|
258
|
+
/** Prefix for static file routes. Default: "/" */
|
|
259
|
+
staticPrefix?: string;
|
|
260
|
+
/** Chat handler options (maxBodySize, etc.) */
|
|
261
|
+
chatHandlerOptions?: Omit<ChatHandlerOptions, "prefix">;
|
|
262
|
+
}
|
|
263
|
+
/** Request handler type returned by createChatServer */
|
|
264
|
+
type RequestHandler = (req: ReadableRequest, res: WritableResponse) => Promise<void>;
|
|
265
|
+
/**
|
|
266
|
+
* Create a combined HTTP request handler that routes to chat, auth, static, or 404.
|
|
267
|
+
*
|
|
268
|
+
* @param options - Server configuration
|
|
269
|
+
* @returns Async request handler
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* import http from "node:http";
|
|
274
|
+
* import { createChatServer } from "@witqq/agent-sdk/chat/server";
|
|
275
|
+
*
|
|
276
|
+
* const handler = createChatServer({
|
|
277
|
+
* runtime,
|
|
278
|
+
* auth: { tokenStore },
|
|
279
|
+
* staticDir: "./public",
|
|
280
|
+
* });
|
|
281
|
+
*
|
|
282
|
+
* http.createServer(handler).listen(3000);
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
declare function createChatServer(options: ChatServerOptions): RequestHandler;
|
|
286
|
+
|
|
287
|
+
export { type AuthHandlerOptions, type AuthProvider, type ChatHandlerOptions, type ChatServerOptions, type CorsOptions, FileTokenStore, type FileTokenStoreOptions, type IClaudeAuth, type ICopilotAuth, type ITokenStore, InMemoryTokenStore, type OnAuthCallback, type ReadableRequest, type RequestHandler, type WritableResponse, corsMiddleware, createAuthHandler, createChatHandler, createChatServer };
|