@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.
Files changed (122) hide show
  1. package/README.md +433 -6
  2. package/dist/auth/index.cjs +188 -1
  3. package/dist/auth/index.cjs.map +1 -1
  4. package/dist/auth/index.d.cts +154 -138
  5. package/dist/auth/index.d.ts +154 -138
  6. package/dist/auth/index.js +188 -2
  7. package/dist/auth/index.js.map +1 -1
  8. package/dist/backends/claude.cjs +341 -22
  9. package/dist/backends/claude.cjs.map +1 -1
  10. package/dist/backends/claude.d.cts +2 -1
  11. package/dist/backends/claude.d.ts +2 -1
  12. package/dist/backends/claude.js +341 -22
  13. package/dist/backends/claude.js.map +1 -1
  14. package/dist/backends/copilot.cjs +133 -25
  15. package/dist/backends/copilot.cjs.map +1 -1
  16. package/dist/backends/copilot.d.cts +2 -1
  17. package/dist/backends/copilot.d.ts +2 -1
  18. package/dist/backends/copilot.js +133 -25
  19. package/dist/backends/copilot.js.map +1 -1
  20. package/dist/backends/vercel-ai.cjs +66 -19
  21. package/dist/backends/vercel-ai.cjs.map +1 -1
  22. package/dist/backends/vercel-ai.d.cts +1 -1
  23. package/dist/backends/vercel-ai.d.ts +1 -1
  24. package/dist/backends/vercel-ai.js +66 -19
  25. package/dist/backends/vercel-ai.js.map +1 -1
  26. package/dist/chat/accumulator.cjs +147 -0
  27. package/dist/chat/accumulator.cjs.map +1 -0
  28. package/dist/chat/accumulator.d.cts +61 -0
  29. package/dist/chat/accumulator.d.ts +61 -0
  30. package/dist/chat/accumulator.js +145 -0
  31. package/dist/chat/accumulator.js.map +1 -0
  32. package/dist/chat/backends.cjs +3534 -0
  33. package/dist/chat/backends.cjs.map +1 -0
  34. package/dist/chat/backends.d.cts +62 -0
  35. package/dist/chat/backends.d.ts +62 -0
  36. package/dist/chat/backends.js +3501 -0
  37. package/dist/chat/backends.js.map +1 -0
  38. package/dist/chat/context.cjs +230 -0
  39. package/dist/chat/context.cjs.map +1 -0
  40. package/dist/chat/context.d.cts +167 -0
  41. package/dist/chat/context.d.ts +167 -0
  42. package/dist/chat/context.js +227 -0
  43. package/dist/chat/context.js.map +1 -0
  44. package/dist/chat/core.cjs +282 -0
  45. package/dist/chat/core.cjs.map +1 -0
  46. package/dist/chat/core.d.cts +435 -0
  47. package/dist/chat/core.d.ts +435 -0
  48. package/dist/chat/core.js +261 -0
  49. package/dist/chat/core.js.map +1 -0
  50. package/dist/chat/errors.cjs +251 -0
  51. package/dist/chat/errors.cjs.map +1 -0
  52. package/dist/chat/errors.d.cts +122 -0
  53. package/dist/chat/errors.d.ts +122 -0
  54. package/dist/chat/errors.js +243 -0
  55. package/dist/chat/errors.js.map +1 -0
  56. package/dist/chat/events.cjs +203 -0
  57. package/dist/chat/events.cjs.map +1 -0
  58. package/dist/chat/events.d.cts +241 -0
  59. package/dist/chat/events.d.ts +241 -0
  60. package/dist/chat/events.js +196 -0
  61. package/dist/chat/events.js.map +1 -0
  62. package/dist/chat/index.cjs +5359 -0
  63. package/dist/chat/index.cjs.map +1 -0
  64. package/dist/chat/index.d.cts +52 -0
  65. package/dist/chat/index.d.ts +52 -0
  66. package/dist/chat/index.js +5296 -0
  67. package/dist/chat/index.js.map +1 -0
  68. package/dist/chat/react.cjs +2739 -0
  69. package/dist/chat/react.cjs.map +1 -0
  70. package/dist/chat/react.d.cts +619 -0
  71. package/dist/chat/react.d.ts +619 -0
  72. package/dist/chat/react.js +2714 -0
  73. package/dist/chat/react.js.map +1 -0
  74. package/dist/chat/runtime.cjs +1030 -0
  75. package/dist/chat/runtime.cjs.map +1 -0
  76. package/dist/chat/runtime.d.cts +118 -0
  77. package/dist/chat/runtime.d.ts +118 -0
  78. package/dist/chat/runtime.js +1028 -0
  79. package/dist/chat/runtime.js.map +1 -0
  80. package/dist/chat/server.cjs +643 -0
  81. package/dist/chat/server.cjs.map +1 -0
  82. package/dist/chat/server.d.cts +287 -0
  83. package/dist/chat/server.d.ts +287 -0
  84. package/dist/chat/server.js +617 -0
  85. package/dist/chat/server.js.map +1 -0
  86. package/dist/chat/sessions.cjs +398 -0
  87. package/dist/chat/sessions.cjs.map +1 -0
  88. package/dist/chat/sessions.d.cts +239 -0
  89. package/dist/chat/sessions.d.ts +239 -0
  90. package/dist/chat/sessions.js +394 -0
  91. package/dist/chat/sessions.js.map +1 -0
  92. package/dist/chat/state.cjs +177 -0
  93. package/dist/chat/state.cjs.map +1 -0
  94. package/dist/chat/state.d.cts +92 -0
  95. package/dist/chat/state.d.ts +92 -0
  96. package/dist/chat/state.js +167 -0
  97. package/dist/chat/state.js.map +1 -0
  98. package/dist/chat/storage.cjs +240 -0
  99. package/dist/chat/storage.cjs.map +1 -0
  100. package/dist/chat/storage.d.cts +191 -0
  101. package/dist/chat/storage.d.ts +191 -0
  102. package/dist/chat/storage.js +236 -0
  103. package/dist/chat/storage.js.map +1 -0
  104. package/dist/errors-BDLbNu9w.d.cts +13 -0
  105. package/dist/errors-BDLbNu9w.d.ts +13 -0
  106. package/dist/in-process-transport-C2oPTYs6.d.ts +223 -0
  107. package/dist/in-process-transport-DG-w5G6k.d.cts +223 -0
  108. package/dist/index.cjs +25 -13
  109. package/dist/index.cjs.map +1 -1
  110. package/dist/index.d.cts +32 -4
  111. package/dist/index.d.ts +32 -4
  112. package/dist/index.js +25 -13
  113. package/dist/index.js.map +1 -1
  114. package/dist/transport-D1OaUgRk.d.ts +67 -0
  115. package/dist/transport-DX1Nhm4N.d.cts +67 -0
  116. package/dist/types-Bh5AhqD-.d.ts +141 -0
  117. package/dist/types-CGF7AEX1.d.cts +141 -0
  118. package/dist/{types-BvwNzZCj.d.cts → types-CqvUAYxt.d.cts} +21 -3
  119. package/dist/{types-BvwNzZCj.d.ts → types-CqvUAYxt.d.ts} +21 -3
  120. package/dist/types-DLZzlJxt.d.ts +39 -0
  121. package/dist/types-tE0CXwBl.d.cts +39 -0
  122. package/package.json +149 -2
@@ -0,0 +1,67 @@
1
+ import { ChatEvent } from './chat/core.js';
2
+
3
+ /**
4
+ * @witqq/agent-sdk/chat/backends/transport
5
+ *
6
+ * IChatTransport abstracts how ChatEvents are delivered to clients.
7
+ * SSEChatTransport implements Server-Sent Events over HTTP.
8
+ */
9
+
10
+ /**
11
+ * Abstraction for delivering chat events to a client.
12
+ * Implementations handle protocol details (SSE, WebSocket, etc.).
13
+ */
14
+ interface IChatTransport {
15
+ /** Send a single chat event to the client */
16
+ send(event: ChatEvent): void;
17
+ /** Signal stream completion and close the connection */
18
+ close(): void;
19
+ /** Signal an error to the client */
20
+ error(err: Error): void;
21
+ /** Whether the transport is still open */
22
+ readonly isOpen: boolean;
23
+ }
24
+ /** Writable HTTP response interface (Node.js http.ServerResponse subset) */
25
+ interface WritableResponse {
26
+ writeHead(statusCode: number, headers: Record<string, string>): void;
27
+ write(chunk: string): boolean;
28
+ end(): void;
29
+ readonly writableEnded: boolean;
30
+ }
31
+ /** Minimal interface for detecting client disconnection */
32
+ interface CloseDetectable {
33
+ on(event: "close", listener: () => void): void;
34
+ }
35
+ /** Configuration options for SSEChatTransport */
36
+ interface SSETransportOptions {
37
+ /** Heartbeat interval in milliseconds. 0 or undefined disables heartbeat. */
38
+ heartbeatMs?: number;
39
+ /** Request object for detecting client disconnection (listens for 'close' event) */
40
+ request?: CloseDetectable;
41
+ }
42
+ /**
43
+ * Server-Sent Events transport for ChatEvent streaming.
44
+ * Sends events as `data: JSON\n\n` lines with SSE headers.
45
+ */
46
+ declare class SSEChatTransport implements IChatTransport {
47
+ private readonly res;
48
+ private _open;
49
+ private _heartbeatTimer;
50
+ constructor(res: WritableResponse, options?: SSETransportOptions);
51
+ get isOpen(): boolean;
52
+ send(event: ChatEvent): void;
53
+ close(): void;
54
+ error(err: Error): void;
55
+ private _cleanup;
56
+ private _clearHeartbeat;
57
+ }
58
+ /**
59
+ * Pipes an async iterable of ChatEvents into a transport.
60
+ * Handles errors and ensures transport is closed on completion.
61
+ *
62
+ * @param events - Async iterable of ChatEvent (from adapter.streamMessage)
63
+ * @param transport - Transport to send events through
64
+ */
65
+ declare function streamToTransport(events: AsyncIterable<ChatEvent>, transport: IChatTransport): Promise<void>;
66
+
67
+ export { type CloseDetectable as C, type IChatTransport as I, SSEChatTransport as S, type WritableResponse as W, type SSETransportOptions as a, streamToTransport as s };
@@ -0,0 +1,67 @@
1
+ import { ChatEvent } from './chat/core.cjs';
2
+
3
+ /**
4
+ * @witqq/agent-sdk/chat/backends/transport
5
+ *
6
+ * IChatTransport abstracts how ChatEvents are delivered to clients.
7
+ * SSEChatTransport implements Server-Sent Events over HTTP.
8
+ */
9
+
10
+ /**
11
+ * Abstraction for delivering chat events to a client.
12
+ * Implementations handle protocol details (SSE, WebSocket, etc.).
13
+ */
14
+ interface IChatTransport {
15
+ /** Send a single chat event to the client */
16
+ send(event: ChatEvent): void;
17
+ /** Signal stream completion and close the connection */
18
+ close(): void;
19
+ /** Signal an error to the client */
20
+ error(err: Error): void;
21
+ /** Whether the transport is still open */
22
+ readonly isOpen: boolean;
23
+ }
24
+ /** Writable HTTP response interface (Node.js http.ServerResponse subset) */
25
+ interface WritableResponse {
26
+ writeHead(statusCode: number, headers: Record<string, string>): void;
27
+ write(chunk: string): boolean;
28
+ end(): void;
29
+ readonly writableEnded: boolean;
30
+ }
31
+ /** Minimal interface for detecting client disconnection */
32
+ interface CloseDetectable {
33
+ on(event: "close", listener: () => void): void;
34
+ }
35
+ /** Configuration options for SSEChatTransport */
36
+ interface SSETransportOptions {
37
+ /** Heartbeat interval in milliseconds. 0 or undefined disables heartbeat. */
38
+ heartbeatMs?: number;
39
+ /** Request object for detecting client disconnection (listens for 'close' event) */
40
+ request?: CloseDetectable;
41
+ }
42
+ /**
43
+ * Server-Sent Events transport for ChatEvent streaming.
44
+ * Sends events as `data: JSON\n\n` lines with SSE headers.
45
+ */
46
+ declare class SSEChatTransport implements IChatTransport {
47
+ private readonly res;
48
+ private _open;
49
+ private _heartbeatTimer;
50
+ constructor(res: WritableResponse, options?: SSETransportOptions);
51
+ get isOpen(): boolean;
52
+ send(event: ChatEvent): void;
53
+ close(): void;
54
+ error(err: Error): void;
55
+ private _cleanup;
56
+ private _clearHeartbeat;
57
+ }
58
+ /**
59
+ * Pipes an async iterable of ChatEvents into a transport.
60
+ * Handles errors and ensures transport is closed on completion.
61
+ *
62
+ * @param events - Async iterable of ChatEvent (from adapter.streamMessage)
63
+ * @param transport - Transport to send events through
64
+ */
65
+ declare function streamToTransport(events: AsyncIterable<ChatEvent>, transport: IChatTransport): Promise<void>;
66
+
67
+ export { type CloseDetectable as C, type IChatTransport as I, SSEChatTransport as S, type WritableResponse as W, type SSETransportOptions as a, streamToTransport as s };
@@ -0,0 +1,141 @@
1
+ import { A as AgentSDKError } from './errors-BDLbNu9w.js';
2
+
3
+ /**
4
+ * Base auth token returned by all auth providers.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import type { AuthToken } from "@witqq/agent-sdk/auth";
9
+ *
10
+ * const token: AuthToken = {
11
+ * accessToken: "gho_abc123...",
12
+ * tokenType: "bearer",
13
+ * obtainedAt: Date.now(),
14
+ * };
15
+ * ```
16
+ */
17
+ interface AuthToken {
18
+ /** The access token string */
19
+ accessToken: string;
20
+ /** Token type (e.g. "bearer") */
21
+ tokenType: string;
22
+ /** Seconds until token expires (undefined = long-lived) */
23
+ expiresIn?: number;
24
+ /** Timestamp when the token was obtained */
25
+ obtainedAt: number;
26
+ }
27
+ /**
28
+ * Copilot-specific token (GitHub OAuth, long-lived).
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import type { CopilotAuthToken } from "@witqq/agent-sdk/auth";
33
+ *
34
+ * const token: CopilotAuthToken = {
35
+ * accessToken: "gho_abc123...",
36
+ * tokenType: "bearer",
37
+ * obtainedAt: Date.now(),
38
+ * login: "octocat",
39
+ * };
40
+ * ```
41
+ */
42
+ interface CopilotAuthToken extends AuthToken {
43
+ /** GitHub user login associated with the token */
44
+ login?: string;
45
+ }
46
+ /**
47
+ * Claude-specific token (OAuth+PKCE, expires in 8h).
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import type { ClaudeAuthToken } from "@witqq/agent-sdk/auth";
52
+ *
53
+ * const token: ClaudeAuthToken = {
54
+ * accessToken: "sk-ant-oat01-...",
55
+ * tokenType: "bearer",
56
+ * expiresIn: 28800,
57
+ * obtainedAt: Date.now(),
58
+ * refreshToken: "sk-ant-rt01-...",
59
+ * scopes: ["user:inference", "user:profile"],
60
+ * };
61
+ * ```
62
+ */
63
+ interface ClaudeAuthToken extends AuthToken {
64
+ /** Refresh token for obtaining new access tokens */
65
+ refreshToken: string;
66
+ /** OAuth scopes granted */
67
+ scopes: string[];
68
+ }
69
+ /**
70
+ * Result of initiating a GitHub Device Flow.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * import { CopilotAuth } from "@witqq/agent-sdk/auth";
75
+ *
76
+ * const auth = new CopilotAuth();
77
+ * const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
78
+ * console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
79
+ * const token = await waitForToken();
80
+ * ```
81
+ */
82
+ interface DeviceFlowResult {
83
+ /** The code the user must enter at the verification URL */
84
+ userCode: string;
85
+ /** URL where the user enters the code */
86
+ verificationUrl: string;
87
+ /** Polls GitHub until user authorizes; resolves with token */
88
+ waitForToken: (signal?: AbortSignal) => Promise<CopilotAuthToken>;
89
+ }
90
+ /** Options for starting a Claude OAuth flow */
91
+ interface OAuthFlowOptions {
92
+ /** The redirect URI registered with the OAuth app */
93
+ redirectUri?: string;
94
+ /** OAuth scopes to request (defaults to user:profile user:inference) */
95
+ scopes?: string;
96
+ }
97
+ /**
98
+ * Result of initiating a Claude OAuth flow.
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * import type { OAuthFlowResult } from "@witqq/agent-sdk/auth";
103
+ *
104
+ * const result: OAuthFlowResult = {
105
+ * authorizeUrl: "https://claude.ai/oauth/authorize?...",
106
+ * completeAuth: async (code) => ({ ... }),
107
+ * };
108
+ * // Open result.authorizeUrl in browser, get code from redirect
109
+ * const token = await result.completeAuth(code);
110
+ * ```
111
+ */
112
+ interface OAuthFlowResult {
113
+ /** URL to open in browser for user authorization */
114
+ authorizeUrl: string;
115
+ /** Exchange the authorization code (or full redirect URL) for tokens */
116
+ completeAuth: (codeOrUrl: string) => Promise<ClaudeAuthToken>;
117
+ }
118
+ /** Base error for auth operations.
119
+ * @param message - Error description
120
+ * @param options - Standard ErrorOptions (e.g. cause)
121
+ */
122
+ declare class AuthError extends AgentSDKError {
123
+ constructor(message: string, options?: ErrorOptions);
124
+ }
125
+ /** Device code expired before user authorized */
126
+ declare class DeviceCodeExpiredError extends AuthError {
127
+ constructor();
128
+ }
129
+ /** User denied access during OAuth flow */
130
+ declare class AccessDeniedError extends AuthError {
131
+ constructor();
132
+ }
133
+ /** Token exchange or refresh failed.
134
+ * @param message - Error description
135
+ * @param options - Standard ErrorOptions (e.g. cause)
136
+ */
137
+ declare class TokenExchangeError extends AuthError {
138
+ constructor(message: string, options?: ErrorOptions);
139
+ }
140
+
141
+ export { type AuthToken as A, type ClaudeAuthToken as C, type DeviceFlowResult as D, type OAuthFlowOptions as O, TokenExchangeError as T, type OAuthFlowResult as a, AccessDeniedError as b, AuthError as c, type CopilotAuthToken as d, DeviceCodeExpiredError as e };
@@ -0,0 +1,141 @@
1
+ import { A as AgentSDKError } from './errors-BDLbNu9w.cjs';
2
+
3
+ /**
4
+ * Base auth token returned by all auth providers.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import type { AuthToken } from "@witqq/agent-sdk/auth";
9
+ *
10
+ * const token: AuthToken = {
11
+ * accessToken: "gho_abc123...",
12
+ * tokenType: "bearer",
13
+ * obtainedAt: Date.now(),
14
+ * };
15
+ * ```
16
+ */
17
+ interface AuthToken {
18
+ /** The access token string */
19
+ accessToken: string;
20
+ /** Token type (e.g. "bearer") */
21
+ tokenType: string;
22
+ /** Seconds until token expires (undefined = long-lived) */
23
+ expiresIn?: number;
24
+ /** Timestamp when the token was obtained */
25
+ obtainedAt: number;
26
+ }
27
+ /**
28
+ * Copilot-specific token (GitHub OAuth, long-lived).
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import type { CopilotAuthToken } from "@witqq/agent-sdk/auth";
33
+ *
34
+ * const token: CopilotAuthToken = {
35
+ * accessToken: "gho_abc123...",
36
+ * tokenType: "bearer",
37
+ * obtainedAt: Date.now(),
38
+ * login: "octocat",
39
+ * };
40
+ * ```
41
+ */
42
+ interface CopilotAuthToken extends AuthToken {
43
+ /** GitHub user login associated with the token */
44
+ login?: string;
45
+ }
46
+ /**
47
+ * Claude-specific token (OAuth+PKCE, expires in 8h).
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import type { ClaudeAuthToken } from "@witqq/agent-sdk/auth";
52
+ *
53
+ * const token: ClaudeAuthToken = {
54
+ * accessToken: "sk-ant-oat01-...",
55
+ * tokenType: "bearer",
56
+ * expiresIn: 28800,
57
+ * obtainedAt: Date.now(),
58
+ * refreshToken: "sk-ant-rt01-...",
59
+ * scopes: ["user:inference", "user:profile"],
60
+ * };
61
+ * ```
62
+ */
63
+ interface ClaudeAuthToken extends AuthToken {
64
+ /** Refresh token for obtaining new access tokens */
65
+ refreshToken: string;
66
+ /** OAuth scopes granted */
67
+ scopes: string[];
68
+ }
69
+ /**
70
+ * Result of initiating a GitHub Device Flow.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * import { CopilotAuth } from "@witqq/agent-sdk/auth";
75
+ *
76
+ * const auth = new CopilotAuth();
77
+ * const { userCode, verificationUrl, waitForToken } = await auth.startDeviceFlow();
78
+ * console.log(`Open ${verificationUrl} and enter code: ${userCode}`);
79
+ * const token = await waitForToken();
80
+ * ```
81
+ */
82
+ interface DeviceFlowResult {
83
+ /** The code the user must enter at the verification URL */
84
+ userCode: string;
85
+ /** URL where the user enters the code */
86
+ verificationUrl: string;
87
+ /** Polls GitHub until user authorizes; resolves with token */
88
+ waitForToken: (signal?: AbortSignal) => Promise<CopilotAuthToken>;
89
+ }
90
+ /** Options for starting a Claude OAuth flow */
91
+ interface OAuthFlowOptions {
92
+ /** The redirect URI registered with the OAuth app */
93
+ redirectUri?: string;
94
+ /** OAuth scopes to request (defaults to user:profile user:inference) */
95
+ scopes?: string;
96
+ }
97
+ /**
98
+ * Result of initiating a Claude OAuth flow.
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * import type { OAuthFlowResult } from "@witqq/agent-sdk/auth";
103
+ *
104
+ * const result: OAuthFlowResult = {
105
+ * authorizeUrl: "https://claude.ai/oauth/authorize?...",
106
+ * completeAuth: async (code) => ({ ... }),
107
+ * };
108
+ * // Open result.authorizeUrl in browser, get code from redirect
109
+ * const token = await result.completeAuth(code);
110
+ * ```
111
+ */
112
+ interface OAuthFlowResult {
113
+ /** URL to open in browser for user authorization */
114
+ authorizeUrl: string;
115
+ /** Exchange the authorization code (or full redirect URL) for tokens */
116
+ completeAuth: (codeOrUrl: string) => Promise<ClaudeAuthToken>;
117
+ }
118
+ /** Base error for auth operations.
119
+ * @param message - Error description
120
+ * @param options - Standard ErrorOptions (e.g. cause)
121
+ */
122
+ declare class AuthError extends AgentSDKError {
123
+ constructor(message: string, options?: ErrorOptions);
124
+ }
125
+ /** Device code expired before user authorized */
126
+ declare class DeviceCodeExpiredError extends AuthError {
127
+ constructor();
128
+ }
129
+ /** User denied access during OAuth flow */
130
+ declare class AccessDeniedError extends AuthError {
131
+ constructor();
132
+ }
133
+ /** Token exchange or refresh failed.
134
+ * @param message - Error description
135
+ * @param options - Standard ErrorOptions (e.g. cause)
136
+ */
137
+ declare class TokenExchangeError extends AuthError {
138
+ constructor(message: string, options?: ErrorOptions);
139
+ }
140
+
141
+ export { type AuthToken as A, type ClaudeAuthToken as C, type DeviceFlowResult as D, type OAuthFlowOptions as O, TokenExchangeError as T, type OAuthFlowResult as a, AccessDeniedError as b, AuthError as c, type CopilotAuthToken as d, DeviceCodeExpiredError as e };
@@ -42,9 +42,19 @@ interface ToolDeclaration<TParams = unknown> {
42
42
  };
43
43
  }
44
44
  /** Full tool with execute function. Required for API-based backends.
45
- * CLI backends extract declaration; execute map held internally. */
45
+ * CLI backends extract declaration; execute map held internally.
46
+ * The optional second parameter receives request-scoped context
47
+ * when invoked through ChatRuntime (session ID, user data, custom metadata). */
46
48
  interface ToolDefinition<TParams = unknown> extends ToolDeclaration<TParams> {
47
- execute: (params: TParams) => Promise<JSONValue> | JSONValue;
49
+ execute: (params: TParams, context?: ToolContext) => Promise<JSONValue> | JSONValue;
50
+ }
51
+ /** Request-scoped context passed to tool execute functions via ChatRuntime.
52
+ * Contains session identity and user-defined metadata from the current session. */
53
+ interface ToolContext {
54
+ /** Active chat session ID */
55
+ sessionId: string;
56
+ /** Custom metadata from the session (e.g. user ID, tenant, permissions) */
57
+ custom?: Record<string, unknown>;
48
58
  }
49
59
  /** A tool call made by the LLM during execution */
50
60
  interface ToolCall {
@@ -329,8 +339,13 @@ interface CopilotBackendOptions {
329
339
  cliArgs?: string[];
330
340
  /** Timeout in milliseconds for sendAndWait() calls. When undefined, uses copilot-sdk default (60s). */
331
341
  timeout?: number;
342
+ /** Timeout in milliseconds for CLI startup and auth check (default: 30000). */
343
+ startupTimeoutMs?: number;
332
344
  /** Custom environment variables merged into the subprocess env */
333
345
  env?: Record<string, string | undefined>;
346
+ /** Session ID to resume after server restart. On startup, the backend attempts
347
+ * to resume this session before creating a new one. */
348
+ resumeSessionId?: string;
334
349
  }
335
350
  /** Options for Claude CLI backend */
336
351
  interface ClaudeBackendOptions {
@@ -341,6 +356,9 @@ interface ClaudeBackendOptions {
341
356
  oauthToken?: string;
342
357
  /** Custom environment variables merged into the subprocess env */
343
358
  env?: Record<string, string | undefined>;
359
+ /** Session ID to resume after server restart. On startup, the backend attempts
360
+ * to resume this session before creating a new one. */
361
+ resumeSessionId?: string;
344
362
  }
345
363
  /** Options for Vercel AI SDK backend */
346
364
  interface VercelAIBackendOptions {
@@ -349,4 +367,4 @@ interface VercelAIBackendOptions {
349
367
  baseUrl?: string;
350
368
  }
351
369
 
352
- export type { CopilotBackendOptions as C, IAgentService as I, VercelAIBackendOptions as V, ClaudeBackendOptions as a };
370
+ export type { AgentEvent as A, CopilotBackendOptions as C, IAgentService as I, Message as M, ToolDefinition as T, UsageData as U, VercelAIBackendOptions as V, ClaudeBackendOptions as a, IAgent as b, AgentConfig as c, ModelInfo as d, ToolResult as e };
@@ -42,9 +42,19 @@ interface ToolDeclaration<TParams = unknown> {
42
42
  };
43
43
  }
44
44
  /** Full tool with execute function. Required for API-based backends.
45
- * CLI backends extract declaration; execute map held internally. */
45
+ * CLI backends extract declaration; execute map held internally.
46
+ * The optional second parameter receives request-scoped context
47
+ * when invoked through ChatRuntime (session ID, user data, custom metadata). */
46
48
  interface ToolDefinition<TParams = unknown> extends ToolDeclaration<TParams> {
47
- execute: (params: TParams) => Promise<JSONValue> | JSONValue;
49
+ execute: (params: TParams, context?: ToolContext) => Promise<JSONValue> | JSONValue;
50
+ }
51
+ /** Request-scoped context passed to tool execute functions via ChatRuntime.
52
+ * Contains session identity and user-defined metadata from the current session. */
53
+ interface ToolContext {
54
+ /** Active chat session ID */
55
+ sessionId: string;
56
+ /** Custom metadata from the session (e.g. user ID, tenant, permissions) */
57
+ custom?: Record<string, unknown>;
48
58
  }
49
59
  /** A tool call made by the LLM during execution */
50
60
  interface ToolCall {
@@ -329,8 +339,13 @@ interface CopilotBackendOptions {
329
339
  cliArgs?: string[];
330
340
  /** Timeout in milliseconds for sendAndWait() calls. When undefined, uses copilot-sdk default (60s). */
331
341
  timeout?: number;
342
+ /** Timeout in milliseconds for CLI startup and auth check (default: 30000). */
343
+ startupTimeoutMs?: number;
332
344
  /** Custom environment variables merged into the subprocess env */
333
345
  env?: Record<string, string | undefined>;
346
+ /** Session ID to resume after server restart. On startup, the backend attempts
347
+ * to resume this session before creating a new one. */
348
+ resumeSessionId?: string;
334
349
  }
335
350
  /** Options for Claude CLI backend */
336
351
  interface ClaudeBackendOptions {
@@ -341,6 +356,9 @@ interface ClaudeBackendOptions {
341
356
  oauthToken?: string;
342
357
  /** Custom environment variables merged into the subprocess env */
343
358
  env?: Record<string, string | undefined>;
359
+ /** Session ID to resume after server restart. On startup, the backend attempts
360
+ * to resume this session before creating a new one. */
361
+ resumeSessionId?: string;
344
362
  }
345
363
  /** Options for Vercel AI SDK backend */
346
364
  interface VercelAIBackendOptions {
@@ -349,4 +367,4 @@ interface VercelAIBackendOptions {
349
367
  baseUrl?: string;
350
368
  }
351
369
 
352
- export type { CopilotBackendOptions as C, IAgentService as I, VercelAIBackendOptions as V, ClaudeBackendOptions as a };
370
+ export type { AgentEvent as A, CopilotBackendOptions as C, IAgentService as I, Message as M, ToolDefinition as T, UsageData as U, VercelAIBackendOptions as V, ClaudeBackendOptions as a, IAgent as b, AgentConfig as c, ModelInfo as d, ToolResult as e };
@@ -0,0 +1,39 @@
1
+ import { IChatProvider, ChatSession, SendMessageOptions, ChatEvent } from './chat/core.js';
2
+ import { I as IAgentService, c as AgentConfig } from './types-CqvUAYxt.js';
3
+
4
+ /**
5
+ * @witqq/agent-sdk/chat/backends/types
6
+ *
7
+ * IBackendAdapter extends IChatProvider with session resume capabilities.
8
+ * Backend adapters own the IAgentService lifecycle and normalize events.
9
+ */
10
+
11
+ /** Options for creating a backend adapter */
12
+ interface BackendAdapterOptions {
13
+ /** Agent configuration (model, systemPrompt, tools, etc.) */
14
+ agentConfig: AgentConfig;
15
+ /** Pre-created agent service (if adapter should not own lifecycle) */
16
+ agentService?: IAgentService;
17
+ }
18
+ /**
19
+ * Extended chat provider with session resume support.
20
+ * Adapters wrap an IAgentService, manage its lifecycle,
21
+ * and bridge AgentEvent → ChatEvent via the existing bridge.
22
+ */
23
+ interface IBackendAdapter extends IChatProvider {
24
+ /** Whether this adapter supports session resume */
25
+ canResume(): boolean;
26
+ /**
27
+ * Resume a previous session by its backend session ID.
28
+ * Streams events from the resumed session.
29
+ * @throws ChatError with SESSION_EXPIRED if session is no longer valid
30
+ * @throws ChatError with SESSION_NOT_FOUND if session ID is unknown
31
+ */
32
+ resume(session: ChatSession, backendSessionId: string, options?: SendMessageOptions): AsyncIterable<ChatEvent>;
33
+ /** The backend session ID from the last stream, or null if not yet streamed */
34
+ readonly backendSessionId: string | null;
35
+ /** The underlying agent service (for advanced consumers) */
36
+ readonly agentService: IAgentService;
37
+ }
38
+
39
+ export type { BackendAdapterOptions as B, IBackendAdapter as I };
@@ -0,0 +1,39 @@
1
+ import { IChatProvider, ChatSession, SendMessageOptions, ChatEvent } from './chat/core.cjs';
2
+ import { I as IAgentService, c as AgentConfig } from './types-CqvUAYxt.cjs';
3
+
4
+ /**
5
+ * @witqq/agent-sdk/chat/backends/types
6
+ *
7
+ * IBackendAdapter extends IChatProvider with session resume capabilities.
8
+ * Backend adapters own the IAgentService lifecycle and normalize events.
9
+ */
10
+
11
+ /** Options for creating a backend adapter */
12
+ interface BackendAdapterOptions {
13
+ /** Agent configuration (model, systemPrompt, tools, etc.) */
14
+ agentConfig: AgentConfig;
15
+ /** Pre-created agent service (if adapter should not own lifecycle) */
16
+ agentService?: IAgentService;
17
+ }
18
+ /**
19
+ * Extended chat provider with session resume support.
20
+ * Adapters wrap an IAgentService, manage its lifecycle,
21
+ * and bridge AgentEvent → ChatEvent via the existing bridge.
22
+ */
23
+ interface IBackendAdapter extends IChatProvider {
24
+ /** Whether this adapter supports session resume */
25
+ canResume(): boolean;
26
+ /**
27
+ * Resume a previous session by its backend session ID.
28
+ * Streams events from the resumed session.
29
+ * @throws ChatError with SESSION_EXPIRED if session is no longer valid
30
+ * @throws ChatError with SESSION_NOT_FOUND if session ID is unknown
31
+ */
32
+ resume(session: ChatSession, backendSessionId: string, options?: SendMessageOptions): AsyncIterable<ChatEvent>;
33
+ /** The backend session ID from the last stream, or null if not yet streamed */
34
+ readonly backendSessionId: string | null;
35
+ /** The underlying agent service (for advanced consumers) */
36
+ readonly agentService: IAgentService;
37
+ }
38
+
39
+ export type { BackendAdapterOptions as B, IBackendAdapter as I };