@witqq/agent-sdk 0.7.0 → 0.9.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/dist/{types-CqvUAYxt.d.ts → agent-C6H2CgJA.d.cts} +139 -102
- package/dist/{types-CqvUAYxt.d.cts → agent-F7oB6eKp.d.ts} +139 -102
- package/dist/auth/index.cjs +72 -1
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +21 -154
- package/dist/auth/index.d.ts +21 -154
- package/dist/auth/index.js +72 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/backends/claude.cjs +480 -261
- package/dist/backends/claude.cjs.map +1 -1
- package/dist/backends/claude.d.cts +3 -1
- package/dist/backends/claude.d.ts +3 -1
- package/dist/backends/claude.js +480 -261
- package/dist/backends/claude.js.map +1 -1
- package/dist/backends/copilot.cjs +337 -112
- package/dist/backends/copilot.cjs.map +1 -1
- package/dist/backends/copilot.d.cts +12 -4
- package/dist/backends/copilot.d.ts +12 -4
- package/dist/backends/copilot.js +337 -112
- package/dist/backends/copilot.js.map +1 -1
- package/dist/backends/mock-llm.cjs +719 -0
- package/dist/backends/mock-llm.cjs.map +1 -0
- package/dist/backends/mock-llm.d.cts +37 -0
- package/dist/backends/mock-llm.d.ts +37 -0
- package/dist/backends/mock-llm.js +717 -0
- package/dist/backends/mock-llm.js.map +1 -0
- package/dist/backends/vercel-ai.cjs +301 -61
- package/dist/backends/vercel-ai.cjs.map +1 -1
- package/dist/backends/vercel-ai.d.cts +3 -1
- package/dist/backends/vercel-ai.d.ts +3 -1
- package/dist/backends/vercel-ai.js +301 -61
- package/dist/backends/vercel-ai.js.map +1 -1
- package/dist/backends-Cno0gZjy.d.cts +114 -0
- package/dist/backends-Cno0gZjy.d.ts +114 -0
- package/dist/chat/accumulator.cjs +1 -1
- package/dist/chat/accumulator.cjs.map +1 -1
- package/dist/chat/accumulator.d.cts +5 -2
- package/dist/chat/accumulator.d.ts +5 -2
- package/dist/chat/accumulator.js +1 -1
- package/dist/chat/accumulator.js.map +1 -1
- package/dist/chat/backends.cjs +1084 -821
- package/dist/chat/backends.cjs.map +1 -1
- package/dist/chat/backends.d.cts +10 -6
- package/dist/chat/backends.d.ts +10 -6
- package/dist/chat/backends.js +1082 -800
- package/dist/chat/backends.js.map +1 -1
- package/dist/chat/context.cjs +50 -0
- package/dist/chat/context.cjs.map +1 -1
- package/dist/chat/context.d.cts +27 -3
- package/dist/chat/context.d.ts +27 -3
- package/dist/chat/context.js +50 -0
- package/dist/chat/context.js.map +1 -1
- package/dist/chat/core.cjs +60 -27
- package/dist/chat/core.cjs.map +1 -1
- package/dist/chat/core.d.cts +41 -382
- package/dist/chat/core.d.ts +41 -382
- package/dist/chat/core.js +58 -28
- package/dist/chat/core.js.map +1 -1
- package/dist/chat/errors.cjs +48 -26
- package/dist/chat/errors.cjs.map +1 -1
- package/dist/chat/errors.d.cts +6 -31
- package/dist/chat/errors.d.ts +6 -31
- package/dist/chat/errors.js +48 -25
- package/dist/chat/errors.js.map +1 -1
- package/dist/chat/events.cjs.map +1 -1
- package/dist/chat/events.d.cts +6 -2
- package/dist/chat/events.d.ts +6 -2
- package/dist/chat/events.js.map +1 -1
- package/dist/chat/index.cjs +1612 -1125
- package/dist/chat/index.cjs.map +1 -1
- package/dist/chat/index.d.cts +35 -10
- package/dist/chat/index.d.ts +35 -10
- package/dist/chat/index.js +1600 -1097
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react/theme.css +2517 -0
- package/dist/chat/react.cjs +2212 -1158
- package/dist/chat/react.cjs.map +1 -1
- package/dist/chat/react.d.cts +665 -122
- package/dist/chat/react.d.ts +665 -122
- package/dist/chat/react.js +2191 -1156
- package/dist/chat/react.js.map +1 -1
- package/dist/chat/runtime.cjs +405 -186
- package/dist/chat/runtime.cjs.map +1 -1
- package/dist/chat/runtime.d.cts +92 -28
- package/dist/chat/runtime.d.ts +92 -28
- package/dist/chat/runtime.js +405 -186
- package/dist/chat/runtime.js.map +1 -1
- package/dist/chat/server.cjs +2247 -212
- package/dist/chat/server.cjs.map +1 -1
- package/dist/chat/server.d.cts +451 -90
- package/dist/chat/server.d.ts +451 -90
- package/dist/chat/server.js +2234 -213
- package/dist/chat/server.js.map +1 -1
- package/dist/chat/sessions.cjs +64 -66
- package/dist/chat/sessions.cjs.map +1 -1
- package/dist/chat/sessions.d.cts +37 -118
- package/dist/chat/sessions.d.ts +37 -118
- package/dist/chat/sessions.js +65 -67
- package/dist/chat/sessions.js.map +1 -1
- package/dist/chat/sqlite.cjs +536 -0
- package/dist/chat/sqlite.cjs.map +1 -0
- package/dist/chat/sqlite.d.cts +164 -0
- package/dist/chat/sqlite.d.ts +164 -0
- package/dist/chat/sqlite.js +527 -0
- package/dist/chat/sqlite.js.map +1 -0
- package/dist/chat/state.cjs +14 -1
- package/dist/chat/state.cjs.map +1 -1
- package/dist/chat/state.d.cts +5 -2
- package/dist/chat/state.d.ts +5 -2
- package/dist/chat/state.js +14 -1
- package/dist/chat/state.js.map +1 -1
- package/dist/chat/storage.cjs +58 -33
- package/dist/chat/storage.cjs.map +1 -1
- package/dist/chat/storage.d.cts +18 -8
- package/dist/chat/storage.d.ts +18 -8
- package/dist/chat/storage.js +59 -34
- package/dist/chat/storage.js.map +1 -1
- package/dist/errors-C-so0M4t.d.cts +33 -0
- package/dist/errors-C-so0M4t.d.ts +33 -0
- package/dist/errors-CmVvczxZ.d.cts +28 -0
- package/dist/errors-CmVvczxZ.d.ts +28 -0
- package/dist/{in-process-transport-C2oPTYs6.d.ts → in-process-transport-7EIit9Xk.d.ts} +72 -33
- package/dist/{in-process-transport-DG-w5G6k.d.cts → in-process-transport-Ct9YcX8I.d.cts} +72 -33
- package/dist/index.cjs +354 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +294 -123
- package/dist/index.d.ts +294 -123
- package/dist/index.js +347 -60
- package/dist/index.js.map +1 -1
- package/dist/provider-types-PTSlRPNB.d.cts +39 -0
- package/dist/provider-types-PTSlRPNB.d.ts +39 -0
- package/dist/refresh-manager-B81PpYBr.d.cts +153 -0
- package/dist/refresh-manager-Dlv_iNZi.d.ts +153 -0
- package/dist/testing.cjs +1107 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +144 -0
- package/dist/testing.d.ts +144 -0
- package/dist/testing.js +1101 -0
- package/dist/testing.js.map +1 -0
- package/dist/token-store-CSUBgYwn.d.ts +48 -0
- package/dist/token-store-CuC4hB9Z.d.cts +48 -0
- package/dist/{transport-DX1Nhm4N.d.cts → transport-DLWCN18G.d.cts} +5 -4
- package/dist/{transport-D1OaUgRk.d.ts → transport-DsuS-GeM.d.ts} +5 -4
- package/dist/{types-CGF7AEX1.d.cts → types-4vbcmPTp.d.cts} +4 -2
- package/dist/{types-Bh5AhqD-.d.ts → types-BxggH0Yh.d.ts} +4 -2
- package/dist/types-DgtI1hzh.d.ts +364 -0
- package/dist/types-DkSXALKg.d.cts +364 -0
- package/package.json +41 -5
- package/LICENSE +0 -21
- package/README.md +0 -948
- package/dist/errors-BDLbNu9w.d.cts +0 -13
- package/dist/errors-BDLbNu9w.d.ts +0 -13
- package/dist/types-DLZzlJxt.d.ts +0 -39
- package/dist/types-tE0CXwBl.d.cts +0 -39
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { A as AuthToken } from './types-BxggH0Yh.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Automatic background token refresh manager.
|
|
5
|
+
*
|
|
6
|
+
* Schedules token refresh at a configurable threshold before expiry
|
|
7
|
+
* (default 80% of token lifetime). Emits events on refresh, failure,
|
|
8
|
+
* and token expiry. Handles retry on failure and clean disposal.
|
|
9
|
+
*
|
|
10
|
+
* When all retries are exhausted but the token hasn't expired yet,
|
|
11
|
+
* the manager waits until the token's expiry time and then starts
|
|
12
|
+
* a fresh retry cycle. This continues until refresh succeeds or
|
|
13
|
+
* the token expires.
|
|
14
|
+
*
|
|
15
|
+
* @example Basic usage
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { TokenRefreshManager } from "@witqq/agent-sdk/auth";
|
|
18
|
+
*
|
|
19
|
+
* const manager = new TokenRefreshManager({
|
|
20
|
+
* token: claudeToken,
|
|
21
|
+
* refresh: (rt) => claudeAuth.refreshToken(rt),
|
|
22
|
+
* });
|
|
23
|
+
* manager.on("refreshed", (newToken) => { tokenStore.save("claude", newToken); });
|
|
24
|
+
* manager.on("error", (err) => { console.error("Refresh failed:", err); });
|
|
25
|
+
* manager.start();
|
|
26
|
+
* // ...later
|
|
27
|
+
* manager.dispose();
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example Integration with createAuthHandler
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { TokenRefreshManager } from "@witqq/agent-sdk/auth";
|
|
33
|
+
* import { createAuthHandler } from "@witqq/agent-sdk/chat/server";
|
|
34
|
+
* import type { ClaudeAuthToken } from "@witqq/agent-sdk/auth";
|
|
35
|
+
*
|
|
36
|
+
* let refreshManager: TokenRefreshManager | undefined;
|
|
37
|
+
*
|
|
38
|
+
* const authHandler = createAuthHandler({
|
|
39
|
+
* tokenStore,
|
|
40
|
+
* onAuth: (provider, token) => {
|
|
41
|
+
* // Clean up previous manager
|
|
42
|
+
* refreshManager?.dispose();
|
|
43
|
+
* refreshManager = undefined;
|
|
44
|
+
*
|
|
45
|
+
* if (provider === "claude" && token.expiresIn) {
|
|
46
|
+
* refreshManager = new TokenRefreshManager({
|
|
47
|
+
* token,
|
|
48
|
+
* refresh: (t) =>
|
|
49
|
+
* claudeAuth.refreshToken((t as ClaudeAuthToken).refreshToken),
|
|
50
|
+
* });
|
|
51
|
+
* refreshManager.on("refreshed", (newToken) => {
|
|
52
|
+
* tokenStore.save("claude", newToken);
|
|
53
|
+
* });
|
|
54
|
+
* refreshManager.on("expired", () => {
|
|
55
|
+
* console.warn("Claude token expired — re-authentication required");
|
|
56
|
+
* });
|
|
57
|
+
* refreshManager.start();
|
|
58
|
+
* }
|
|
59
|
+
* },
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/** Events emitted by TokenRefreshManager */
|
|
65
|
+
interface TokenRefreshEvents {
|
|
66
|
+
/** Emitted when token was successfully refreshed */
|
|
67
|
+
refreshed: (token: AuthToken) => void;
|
|
68
|
+
/** Emitted when refresh attempt failed (may retry) */
|
|
69
|
+
error: (error: Error, attempt: number) => void;
|
|
70
|
+
/** Emitted when token expired and could not be refreshed */
|
|
71
|
+
expired: () => void;
|
|
72
|
+
/** Emitted when manager is disposed */
|
|
73
|
+
disposed: () => void;
|
|
74
|
+
}
|
|
75
|
+
/** Configuration for TokenRefreshManager */
|
|
76
|
+
interface TokenRefreshOptions {
|
|
77
|
+
/** Current token with expiresIn and obtainedAt */
|
|
78
|
+
token: AuthToken;
|
|
79
|
+
/**
|
|
80
|
+
* Function that performs the actual token refresh.
|
|
81
|
+
* Receives the current token and returns a new one.
|
|
82
|
+
*/
|
|
83
|
+
refresh: (token: AuthToken) => Promise<AuthToken>;
|
|
84
|
+
/**
|
|
85
|
+
* Fraction of token lifetime at which to trigger refresh (0-1).
|
|
86
|
+
* Default: 0.8 (refresh at 80% of lifetime, i.e. with 20% remaining)
|
|
87
|
+
*/
|
|
88
|
+
refreshThreshold?: number;
|
|
89
|
+
/**
|
|
90
|
+
* Maximum retry attempts on refresh failure. Default: 3
|
|
91
|
+
*/
|
|
92
|
+
maxRetries?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Base delay between retries in ms. Exponential backoff applied. Default: 1000
|
|
95
|
+
*/
|
|
96
|
+
retryDelayMs?: number;
|
|
97
|
+
/**
|
|
98
|
+
* Minimum schedule delay in ms (prevents scheduling in the past). Default: 1000
|
|
99
|
+
*/
|
|
100
|
+
minDelayMs?: number;
|
|
101
|
+
}
|
|
102
|
+
type EventName = keyof TokenRefreshEvents;
|
|
103
|
+
/**
|
|
104
|
+
* Background token refresh manager with event emission and retry logic.
|
|
105
|
+
*
|
|
106
|
+
* Lifecycle: `new` → `start()` → (auto-refreshes) → `stop()` or `dispose()`
|
|
107
|
+
*/
|
|
108
|
+
declare class TokenRefreshManager {
|
|
109
|
+
private currentToken;
|
|
110
|
+
private readonly refreshFn;
|
|
111
|
+
private readonly threshold;
|
|
112
|
+
private readonly maxRetries;
|
|
113
|
+
private readonly retryDelayMs;
|
|
114
|
+
private readonly minDelayMs;
|
|
115
|
+
private timerId;
|
|
116
|
+
private running;
|
|
117
|
+
private disposed;
|
|
118
|
+
private readonly listeners;
|
|
119
|
+
constructor(options: TokenRefreshOptions);
|
|
120
|
+
/** Register an event listener */
|
|
121
|
+
on<K extends EventName>(event: K, listener: TokenRefreshEvents[K]): this;
|
|
122
|
+
/** Remove an event listener */
|
|
123
|
+
off<K extends EventName>(event: K, listener: TokenRefreshEvents[K]): this;
|
|
124
|
+
/** Current token managed by this instance */
|
|
125
|
+
get token(): AuthToken;
|
|
126
|
+
/** Whether the manager is currently running */
|
|
127
|
+
get isRunning(): boolean;
|
|
128
|
+
/** Whether the manager has been disposed */
|
|
129
|
+
get isDisposed(): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Start automatic refresh scheduling.
|
|
132
|
+
* If the token is already expired, emits "expired" immediately.
|
|
133
|
+
* If the token has no expiresIn, does nothing (long-lived token).
|
|
134
|
+
*/
|
|
135
|
+
start(): void;
|
|
136
|
+
/** Stop automatic refresh (can be restarted with start()) */
|
|
137
|
+
stop(): void;
|
|
138
|
+
/**
|
|
139
|
+
* Update the managed token (e.g. after manual refresh).
|
|
140
|
+
* Reschedules automatic refresh if running.
|
|
141
|
+
*/
|
|
142
|
+
updateToken(token: AuthToken): void;
|
|
143
|
+
/** Stop and clean up all resources */
|
|
144
|
+
dispose(): void;
|
|
145
|
+
private schedule;
|
|
146
|
+
private performRefresh;
|
|
147
|
+
private computeRefreshDelay;
|
|
148
|
+
private isTokenExpired;
|
|
149
|
+
private clearTimer;
|
|
150
|
+
private emit;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export { type TokenRefreshEvents as T, TokenRefreshManager as a, type TokenRefreshOptions as b };
|