@witqq/agent-sdk 0.7.0 → 0.8.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 (147) hide show
  1. package/README.md +140 -34
  2. package/dist/{types-CqvUAYxt.d.cts → agent-CW9XbmG_.d.ts} +137 -102
  3. package/dist/{types-CqvUAYxt.d.ts → agent-DxY68NZL.d.cts} +137 -102
  4. package/dist/auth/index.cjs +72 -1
  5. package/dist/auth/index.cjs.map +1 -1
  6. package/dist/auth/index.d.cts +21 -154
  7. package/dist/auth/index.d.ts +21 -154
  8. package/dist/auth/index.js +72 -1
  9. package/dist/auth/index.js.map +1 -1
  10. package/dist/backends/claude.cjs +480 -261
  11. package/dist/backends/claude.cjs.map +1 -1
  12. package/dist/backends/claude.d.cts +3 -1
  13. package/dist/backends/claude.d.ts +3 -1
  14. package/dist/backends/claude.js +480 -261
  15. package/dist/backends/claude.js.map +1 -1
  16. package/dist/backends/copilot.cjs +329 -97
  17. package/dist/backends/copilot.cjs.map +1 -1
  18. package/dist/backends/copilot.d.cts +12 -4
  19. package/dist/backends/copilot.d.ts +12 -4
  20. package/dist/backends/copilot.js +329 -97
  21. package/dist/backends/copilot.js.map +1 -1
  22. package/dist/backends/vercel-ai.cjs +294 -61
  23. package/dist/backends/vercel-ai.cjs.map +1 -1
  24. package/dist/backends/vercel-ai.d.cts +3 -1
  25. package/dist/backends/vercel-ai.d.ts +3 -1
  26. package/dist/backends/vercel-ai.js +294 -61
  27. package/dist/backends/vercel-ai.js.map +1 -1
  28. package/dist/backends-BSrsBYFn.d.cts +39 -0
  29. package/dist/backends-BSrsBYFn.d.ts +39 -0
  30. package/dist/chat/accumulator.cjs +1 -1
  31. package/dist/chat/accumulator.cjs.map +1 -1
  32. package/dist/chat/accumulator.d.cts +5 -2
  33. package/dist/chat/accumulator.d.ts +5 -2
  34. package/dist/chat/accumulator.js +1 -1
  35. package/dist/chat/accumulator.js.map +1 -1
  36. package/dist/chat/backends.cjs +736 -746
  37. package/dist/chat/backends.cjs.map +1 -1
  38. package/dist/chat/backends.d.cts +10 -6
  39. package/dist/chat/backends.d.ts +10 -6
  40. package/dist/chat/backends.js +736 -725
  41. package/dist/chat/backends.js.map +1 -1
  42. package/dist/chat/context.cjs +50 -0
  43. package/dist/chat/context.cjs.map +1 -1
  44. package/dist/chat/context.d.cts +27 -3
  45. package/dist/chat/context.d.ts +27 -3
  46. package/dist/chat/context.js +50 -0
  47. package/dist/chat/context.js.map +1 -1
  48. package/dist/chat/core.cjs +25 -2
  49. package/dist/chat/core.cjs.map +1 -1
  50. package/dist/chat/core.d.cts +30 -381
  51. package/dist/chat/core.d.ts +30 -381
  52. package/dist/chat/core.js +24 -3
  53. package/dist/chat/core.js.map +1 -1
  54. package/dist/chat/errors.cjs +48 -26
  55. package/dist/chat/errors.cjs.map +1 -1
  56. package/dist/chat/errors.d.cts +6 -31
  57. package/dist/chat/errors.d.ts +6 -31
  58. package/dist/chat/errors.js +48 -25
  59. package/dist/chat/errors.js.map +1 -1
  60. package/dist/chat/events.cjs.map +1 -1
  61. package/dist/chat/events.d.cts +6 -2
  62. package/dist/chat/events.d.ts +6 -2
  63. package/dist/chat/events.js.map +1 -1
  64. package/dist/chat/index.cjs +1199 -1008
  65. package/dist/chat/index.cjs.map +1 -1
  66. package/dist/chat/index.d.cts +35 -10
  67. package/dist/chat/index.d.ts +35 -10
  68. package/dist/chat/index.js +1196 -987
  69. package/dist/chat/index.js.map +1 -1
  70. package/dist/chat/react/theme.css +2517 -0
  71. package/dist/chat/react.cjs +2003 -1153
  72. package/dist/chat/react.cjs.map +1 -1
  73. package/dist/chat/react.d.cts +590 -121
  74. package/dist/chat/react.d.ts +590 -121
  75. package/dist/chat/react.js +1984 -1151
  76. package/dist/chat/react.js.map +1 -1
  77. package/dist/chat/runtime.cjs +401 -186
  78. package/dist/chat/runtime.cjs.map +1 -1
  79. package/dist/chat/runtime.d.cts +92 -28
  80. package/dist/chat/runtime.d.ts +92 -28
  81. package/dist/chat/runtime.js +401 -186
  82. package/dist/chat/runtime.js.map +1 -1
  83. package/dist/chat/server.cjs +2234 -209
  84. package/dist/chat/server.cjs.map +1 -1
  85. package/dist/chat/server.d.cts +451 -90
  86. package/dist/chat/server.d.ts +451 -90
  87. package/dist/chat/server.js +2221 -210
  88. package/dist/chat/server.js.map +1 -1
  89. package/dist/chat/sessions.cjs +25 -43
  90. package/dist/chat/sessions.cjs.map +1 -1
  91. package/dist/chat/sessions.d.cts +37 -118
  92. package/dist/chat/sessions.d.ts +37 -118
  93. package/dist/chat/sessions.js +25 -43
  94. package/dist/chat/sessions.js.map +1 -1
  95. package/dist/chat/sqlite.cjs +441 -0
  96. package/dist/chat/sqlite.cjs.map +1 -0
  97. package/dist/chat/sqlite.d.cts +128 -0
  98. package/dist/chat/sqlite.d.ts +128 -0
  99. package/dist/chat/sqlite.js +435 -0
  100. package/dist/chat/sqlite.js.map +1 -0
  101. package/dist/chat/state.cjs +14 -1
  102. package/dist/chat/state.cjs.map +1 -1
  103. package/dist/chat/state.d.cts +5 -2
  104. package/dist/chat/state.d.ts +5 -2
  105. package/dist/chat/state.js +14 -1
  106. package/dist/chat/state.js.map +1 -1
  107. package/dist/chat/storage.cjs +19 -10
  108. package/dist/chat/storage.cjs.map +1 -1
  109. package/dist/chat/storage.d.cts +11 -5
  110. package/dist/chat/storage.d.ts +11 -5
  111. package/dist/chat/storage.js +19 -10
  112. package/dist/chat/storage.js.map +1 -1
  113. package/dist/errors-C-so0M4t.d.cts +33 -0
  114. package/dist/errors-C-so0M4t.d.ts +33 -0
  115. package/dist/errors-CmVvczxZ.d.cts +28 -0
  116. package/dist/errors-CmVvczxZ.d.ts +28 -0
  117. package/dist/{in-process-transport-C2oPTYs6.d.ts → in-process-transport-C1JnJGVR.d.ts} +28 -23
  118. package/dist/{in-process-transport-DG-w5G6k.d.cts → in-process-transport-C7DSqPyX.d.cts} +28 -23
  119. package/dist/index.cjs +340 -46
  120. package/dist/index.cjs.map +1 -1
  121. package/dist/index.d.cts +292 -123
  122. package/dist/index.d.ts +292 -123
  123. package/dist/index.js +334 -47
  124. package/dist/index.js.map +1 -1
  125. package/dist/provider-types-PTSlRPNB.d.cts +39 -0
  126. package/dist/provider-types-PTSlRPNB.d.ts +39 -0
  127. package/dist/refresh-manager-B81PpYBr.d.cts +153 -0
  128. package/dist/refresh-manager-Dlv_iNZi.d.ts +153 -0
  129. package/dist/testing.cjs +383 -0
  130. package/dist/testing.cjs.map +1 -0
  131. package/dist/testing.d.cts +132 -0
  132. package/dist/testing.d.ts +132 -0
  133. package/dist/testing.js +377 -0
  134. package/dist/testing.js.map +1 -0
  135. package/dist/token-store-CSUBgYwn.d.ts +48 -0
  136. package/dist/token-store-CuC4hB9Z.d.cts +48 -0
  137. package/dist/{transport-DX1Nhm4N.d.cts → transport-Cdh3M0tS.d.cts} +5 -4
  138. package/dist/{transport-D1OaUgRk.d.ts → transport-Ciap4PWK.d.ts} +5 -4
  139. package/dist/{types-CGF7AEX1.d.cts → types-4vbcmPTp.d.cts} +4 -2
  140. package/dist/{types-Bh5AhqD-.d.ts → types-BxggH0Yh.d.ts} +4 -2
  141. package/dist/types-DRgd_9R7.d.cts +363 -0
  142. package/dist/types-ajANVzf7.d.ts +363 -0
  143. package/package.json +31 -6
  144. package/dist/errors-BDLbNu9w.d.cts +0 -13
  145. package/dist/errors-BDLbNu9w.d.ts +0 -13
  146. package/dist/types-DLZzlJxt.d.ts +0 -39
  147. package/dist/types-tE0CXwBl.d.cts +0 -39
@@ -1,34 +1,11 @@
1
1
  import { z } from 'zod';
2
-
3
- /** Pluggable store for persisting permission (scope) decisions across runs. */
4
- interface IPermissionStore {
5
- /** Check if tool is already approved */
6
- isApproved(toolName: string): Promise<boolean>;
7
- /** Store an approval decision */
8
- approve(toolName: string, scope: PermissionScope): Promise<void>;
9
- /** Revoke approval for a tool */
10
- revoke(toolName: string): Promise<void>;
11
- /** Clear all approvals */
12
- clear(): Promise<void>;
13
- /** Dispose resources */
14
- dispose(): Promise<void>;
15
- }
2
+ import { E as ErrorCode } from './errors-C-so0M4t.cjs';
16
3
 
17
4
  /** JSON-serializable value used for tool arguments and results */
18
5
  type JSONValue = string | number | boolean | null | JSONValue[] | {
19
6
  [key: string]: JSONValue;
20
7
  };
21
- /** Message content — plain string or array of text/image parts */
22
- type MessageContent = string | Array<ContentPart>;
23
- /** Individual content part within a multi-part message */
24
- type ContentPart = {
25
- type: "text";
26
- text: string;
27
- } | {
28
- type: "image";
29
- data: string;
30
- mimeType: string;
31
- };
8
+
32
9
  /** What the LLM sees — name, description, schema. Passed to all backends. */
33
10
  interface ToolDeclaration<TParams = unknown> {
34
11
  name: string;
@@ -46,7 +23,7 @@ interface ToolDeclaration<TParams = unknown> {
46
23
  * The optional second parameter receives request-scoped context
47
24
  * when invoked through ChatRuntime (session ID, user data, custom metadata). */
48
25
  interface ToolDefinition<TParams = unknown> extends ToolDeclaration<TParams> {
49
- execute: (params: TParams, context?: ToolContext) => Promise<JSONValue> | JSONValue;
26
+ execute: (params: TParams, context?: ToolContext) => Promise<unknown> | unknown;
50
27
  }
51
28
  /** Request-scoped context passed to tool execute functions via ChatRuntime.
52
29
  * Contains session identity and user-defined metadata from the current session. */
@@ -69,6 +46,18 @@ interface ToolResult {
69
46
  result: JSONValue;
70
47
  isError?: boolean;
71
48
  }
49
+
50
+ /** Message content — plain string or array of text/image parts */
51
+ type MessageContent = string | Array<ContentPart>;
52
+ /** Individual content part within a multi-part message */
53
+ type ContentPart = {
54
+ type: "text";
55
+ text: string;
56
+ } | {
57
+ type: "image";
58
+ data: string;
59
+ mimeType: string;
60
+ };
72
61
  /** Conversation message — discriminated union on `role` */
73
62
  type Message = {
74
63
  role: "user";
@@ -85,12 +74,15 @@ type Message = {
85
74
  role: "system";
86
75
  content: string;
87
76
  };
77
+
88
78
  /** Scope for "remember this decision" */
89
79
  type PermissionScope = "once" | "session" | "project" | "always";
90
80
  /** What the permission callback receives */
91
81
  interface PermissionRequest {
92
82
  toolName: string;
93
83
  toolArgs: Record<string, unknown>;
84
+ /** Unique identifier for this specific tool call */
85
+ toolCallId?: string;
94
86
  /** SDK-suggested scope (from Claude CLI's suggestions) */
95
87
  suggestedScope?: PermissionScope;
96
88
  /** Original SDK permission request (for pass-through) */
@@ -128,12 +120,32 @@ interface SupervisorHooks {
128
120
  onPermission?: PermissionCallback;
129
121
  onAskUser?: (request: UserInputRequest, signal: AbortSignal) => Promise<UserInputResponse>;
130
122
  }
131
- /** Configuration for typed structured output from LLM */
132
- interface StructuredOutputConfig<T = unknown> {
133
- schema: z.ZodType<T>;
123
+
124
+ /** Model metadata returned by listModels() */
125
+ interface ModelInfo {
126
+ id: string;
134
127
  name?: string;
135
- description?: string;
128
+ provider?: string;
129
+ /** Model tier for UI categorization and cost hints */
130
+ tier?: "fast" | "standard" | "premium";
131
+ /** Context window size in tokens */
132
+ contextWindow?: number;
133
+ /** Model capabilities (e.g. "vision", "tools", "structured") */
134
+ capabilities?: string[];
135
+ }
136
+ /** LLM model parameters */
137
+ interface ModelParams {
138
+ temperature?: number;
139
+ maxTokens?: number;
140
+ topP?: number;
141
+ stopSequences?: string[];
142
+ }
143
+ /** Result of backend validation check */
144
+ interface ValidationResult {
145
+ valid: boolean;
146
+ errors: string[];
136
147
  }
148
+
137
149
  /** Usage data from LLM execution — tokens consumed plus optional metadata */
138
150
  interface UsageData {
139
151
  promptTokens: number;
@@ -192,24 +204,78 @@ type AgentEvent = {
192
204
  type: "error";
193
205
  error: string;
194
206
  recoverable: boolean;
207
+ code?: ErrorCode;
195
208
  } | {
196
209
  type: "done";
197
210
  finalOutput: string | null;
198
211
  structuredOutput?: unknown;
212
+ streamed?: boolean;
199
213
  };
200
- /** Options passed to agent.run() / agent.stream() */
201
- interface RunOptions {
202
- /** AbortSignal for cancellation */
214
+
215
+ /** Pluggable store for persisting permission (scope) decisions across runs. */
216
+ interface IPermissionStore {
217
+ /** Check if tool is already approved */
218
+ isApproved(toolName: string): Promise<boolean>;
219
+ /** Store an approval decision */
220
+ approve(toolName: string, scope: PermissionScope): Promise<void>;
221
+ /** Revoke approval for a tool */
222
+ revoke(toolName: string): Promise<void>;
223
+ /** Clear all approvals */
224
+ clear(): Promise<void>;
225
+ /** Dispose resources */
226
+ dispose(): Promise<void>;
227
+ }
228
+
229
+ /** Per-call overrides passed to run(), stream(), runStructured().
230
+ * Allows overriding the model, tools, signal, and other parameters
231
+ * on a per-request basis without modifying the agent configuration. */
232
+ interface CallOptions {
233
+ /** Override the default model for this call */
234
+ model?: string;
235
+ /** Override/extend tools for this call */
236
+ tools?: ToolDefinition[];
237
+ /** Per-call abort signal */
203
238
  signal?: AbortSignal;
239
+ /** Override system message for this call */
240
+ systemMessage?: string;
241
+ /** Provider-specific options passed through to the underlying SDK */
242
+ providerOptions?: Record<string, unknown>;
243
+ /** Per-call timeout in milliseconds */
244
+ timeout?: number;
245
+ /** Per-call token limit */
246
+ maxTokens?: number;
247
+ /** Retry configuration for this call */
248
+ retry?: RetryConfig;
249
+ }
250
+ /** Configuration for automatic retries on transient errors */
251
+ interface RetryConfig {
252
+ /** Maximum number of retries (default: 0 — no retry) */
253
+ maxRetries?: number;
254
+ /** Initial delay in ms before first retry (default: 1000) */
255
+ initialDelayMs?: number;
256
+ /** Backoff multiplier (default: 2) */
257
+ backoffMultiplier?: number;
258
+ /** Which error codes to retry (default: all recoverable codes) */
259
+ retryableErrors?: ErrorCode[];
260
+ }
261
+ /** Configuration for typed structured output from LLM */
262
+ interface StructuredOutputConfig<T = unknown> {
263
+ schema: z.ZodType<T>;
264
+ name?: string;
265
+ description?: string;
266
+ }
267
+ /** Options passed to agent.run() / agent.stream().
268
+ * Extends CallOptions with run-specific fields (context, activityTimeoutMs).
269
+ * model is REQUIRED — every agent call must specify the model explicitly. */
270
+ interface RunOptions extends CallOptions {
271
+ /** Model to use for this call (required — no implicit defaults) */
272
+ model: string;
204
273
  /** Arbitrary context passed to the agent run */
205
274
  context?: Record<string, unknown>;
206
- }
207
- /** LLM model parameters */
208
- interface ModelParams {
209
- temperature?: number;
210
- maxTokens?: number;
211
- topP?: number;
212
- stopSequences?: string[];
275
+ /** Inactivity timeout for streaming (ms). When set, the stream aborts if no
276
+ * event (including heartbeats/progress) arrives within this period. Resets on
277
+ * every received event. Default: no timeout. Only affects stream()/streamWithContext(). */
278
+ activityTimeoutMs?: number;
213
279
  }
214
280
  /** Timeout configuration for agent operations */
215
281
  interface TimeoutConfig {
@@ -234,12 +300,10 @@ interface ErrorHandlingConfig {
234
300
  phase: "tool" | "llm" | "permission" | "ask-user";
235
301
  }) => void;
236
302
  }
237
- /** Configuration for creating an agent */
303
+ /** Identity-only agent configuration defines the agent's behavior, NOT per-call defaults.
304
+ * For creating an agent with model/tools defaults, use FullAgentConfig. */
238
305
  interface AgentConfig {
239
- model?: string;
240
- modelParams?: ModelParams;
241
306
  systemPrompt: string;
242
- tools: ToolDefinition[];
243
307
  supervisor?: SupervisorHooks;
244
308
  maxTurns?: number;
245
309
  timeout?: TimeoutConfig;
@@ -249,8 +313,14 @@ interface AgentConfig {
249
313
  /** How to apply systemPrompt: "append" adds to backend default, "replace" overrides it.
250
314
  * Default: "append". Currently used by the Copilot backend. */
251
315
  systemMessageMode?: "append" | "replace";
252
- /** Filter for backend built-in tools (e.g. ["web_search", "web_fetch"] for Copilot).
253
- * When set, only listed built-in tools are available. Backend-specific. */
316
+ /**
317
+ * Filter for backend built-in tools (e.g. `["web_search", "web_fetch"]` for Copilot).
318
+ * When set, only listed built-in tools are available. Backend-specific.
319
+ *
320
+ * **Security note**: This is a trust boundary — it controls which backend-native tools
321
+ * the AI agent can invoke. By default, backends expose ALL their built-in tools.
322
+ * Set this to restrict access (e.g. prevent file system access in a web-facing agent).
323
+ */
254
324
  availableTools?: string[];
255
325
  /** Callback invoked with usage data after run completion or during streaming.
256
326
  * Fire-and-forget: errors are logged but not propagated. */
@@ -264,11 +334,24 @@ interface AgentConfig {
264
334
  * "persistent": reuses the same CLI session across calls, preserving conversation
265
335
  * history natively in the CLI backend. Session is destroyed on agent dispose(). */
266
336
  sessionMode?: "per-call" | "persistent";
337
+ }
338
+ /** Per-call defaults that can be provided at agent creation time.
339
+ * Each field can also be overridden on individual calls via RunOptions. */
340
+ interface CallDefaults {
341
+ /** Default model (overridable per-call via RunOptions.model) */
342
+ model?: string;
343
+ /** Default model parameters */
344
+ modelParams?: ModelParams;
345
+ /** Default tools (overridable per-call via RunOptions.tools) */
346
+ tools?: ToolDefinition[];
267
347
  /** Provider-specific options passed through to the underlying SDK.
268
348
  * For Vercel AI: passed as providerOptions to generateText/streamText.
269
349
  * Example: { google: { thinkingConfig: { thinkingBudget: 1024 } } } */
270
350
  providerOptions?: Record<string, Record<string, unknown>>;
271
351
  }
352
+ /** Full agent configuration: identity + per-call defaults.
353
+ * This is what createAgent() accepts. Backward-compatible with the old AgentConfig shape. */
354
+ type FullAgentConfig = AgentConfig & CallDefaults;
272
355
  /** Result of an agent run, generic over structured output type T */
273
356
  interface AgentResult<T = void> {
274
357
  output: string | null;
@@ -290,15 +373,15 @@ interface IAgent {
290
373
  * or before the first call. Can be stored externally for session resume. */
291
374
  readonly sessionId: string | undefined;
292
375
  /** Run a single prompt and return the result. Wraps prompt in a user message. */
293
- run(prompt: MessageContent, options?: RunOptions): Promise<AgentResult>;
376
+ run(prompt: MessageContent, options: RunOptions): Promise<AgentResult>;
294
377
  /** Run with full conversation history. Messages are passed directly to the backend. */
295
- runWithContext(messages: Message[], options?: RunOptions): Promise<AgentResult>;
378
+ runWithContext(messages: Message[], options: RunOptions): Promise<AgentResult>;
296
379
  /** Run with structured output validated against a Zod schema. */
297
- runStructured<T>(prompt: MessageContent, schema: StructuredOutputConfig<T>, options?: RunOptions): Promise<AgentResult<T>>;
380
+ runStructured<T>(prompt: MessageContent, schema: StructuredOutputConfig<T>, options: RunOptions): Promise<AgentResult<T>>;
298
381
  /** Stream events for a single prompt. Wraps prompt in a user message. */
299
- stream(prompt: MessageContent, options?: RunOptions): AsyncIterable<AgentEvent>;
382
+ stream(prompt: MessageContent, options: RunOptions): AsyncIterable<AgentEvent>;
300
383
  /** Stream events with full conversation history. Messages are passed directly to the backend. */
301
- streamWithContext(messages: Message[], options?: RunOptions): AsyncIterable<AgentEvent>;
384
+ streamWithContext(messages: Message[], options: RunOptions): AsyncIterable<AgentEvent>;
302
385
  /** Abort the current operation. No-op if not running. */
303
386
  abort(): void;
304
387
  /** Gracefully interrupt the current operation. Resolves when the backend acknowledges. */
@@ -306,65 +389,17 @@ interface IAgent {
306
389
  /** Get current agent lifecycle state. */
307
390
  getState(): AgentState;
308
391
  /** Get frozen agent configuration. */
309
- getConfig(): Readonly<AgentConfig>;
392
+ getConfig(): Readonly<FullAgentConfig>;
310
393
  /** Release resources. After dispose(), agent must not be used. */
311
394
  dispose(): void;
312
395
  }
313
- /** Model metadata returned by listModels() */
314
- interface ModelInfo {
315
- id: string;
316
- name?: string;
317
- provider?: string;
318
- }
319
- /** Result of backend validation check */
320
- interface ValidationResult {
321
- valid: boolean;
322
- errors: string[];
323
- }
324
396
  /** Backend service interface — creates agents, lists models, validates config */
325
397
  interface IAgentService {
326
398
  readonly name: string;
327
- createAgent(config: AgentConfig): IAgent;
399
+ createAgent(config: FullAgentConfig): IAgent;
328
400
  listModels(): Promise<ModelInfo[]>;
329
401
  validate(): Promise<ValidationResult>;
330
402
  dispose(): Promise<void>;
331
403
  }
332
- /** Options for Copilot CLI backend */
333
- interface CopilotBackendOptions {
334
- cliPath?: string;
335
- workingDirectory?: string;
336
- githubToken?: string;
337
- useLoggedInUser?: boolean;
338
- /** Extra CLI arguments passed to the Copilot subprocess (e.g. ["--allow-all"]) */
339
- cliArgs?: string[];
340
- /** Timeout in milliseconds for sendAndWait() calls. When undefined, uses copilot-sdk default (60s). */
341
- timeout?: number;
342
- /** Timeout in milliseconds for CLI startup and auth check (default: 30000). */
343
- startupTimeoutMs?: number;
344
- /** Custom environment variables merged into the subprocess env */
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;
349
- }
350
- /** Options for Claude CLI backend */
351
- interface ClaudeBackendOptions {
352
- cliPath?: string;
353
- workingDirectory?: string;
354
- maxTurns?: number;
355
- /** OAuth token for Claude authentication (set as CLAUDE_CODE_OAUTH_TOKEN env var) */
356
- oauthToken?: string;
357
- /** Custom environment variables merged into the subprocess env */
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;
362
- }
363
- /** Options for Vercel AI SDK backend */
364
- interface VercelAIBackendOptions {
365
- apiKey: string;
366
- provider?: string;
367
- baseUrl?: string;
368
- }
369
404
 
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 };
405
+ export type { AgentEvent as A, FullAgentConfig as F, IAgentService as I, ModelInfo as M, RunOptions as R, ToolDefinition as T, UsageData as U, ValidationResult as V, MessageContent as a, AgentResult as b, IAgent as c, Message as d, ToolResult as e };
@@ -6,9 +6,18 @@ var crypto = require('crypto');
6
6
  var AgentSDKError = class extends Error {
7
7
  /** @internal Marker for cross-bundle identity checks */
8
8
  _agentSDKError = true;
9
+ /** Machine-readable error code. Prefer values from the ErrorCode enum. */
10
+ code;
11
+ /** Whether this error is safe to retry */
12
+ retryable;
13
+ /** HTTP status code hint for error classification */
14
+ httpStatus;
9
15
  constructor(message, options) {
10
16
  super(message, options);
11
17
  this.name = "AgentSDKError";
18
+ this.code = options?.code;
19
+ this.retryable = options?.retryable ?? false;
20
+ this.httpStatus = options?.httpStatus;
12
21
  }
13
22
  /** Check if an error is an AgentSDKError (works across bundled copies) */
14
23
  static is(error) {
@@ -19,7 +28,7 @@ var AgentSDKError = class extends Error {
19
28
  // src/auth/types.ts
20
29
  var AuthError = class extends AgentSDKError {
21
30
  constructor(message, options) {
22
- super(message, options);
31
+ super(message, { ...options, code: "AUTH_EXPIRED" /* AUTH_EXPIRED */ });
23
32
  this.name = "AuthError";
24
33
  }
25
34
  };
@@ -132,6 +141,12 @@ var CopilotAuth = class {
132
141
  tokenType: data.token_type ?? "bearer",
133
142
  obtainedAt: Date.now()
134
143
  };
144
+ if (data.refresh_token) {
145
+ token.refreshToken = data.refresh_token;
146
+ }
147
+ if (data.expires_in) {
148
+ token.expiresIn = data.expires_in;
149
+ }
135
150
  try {
136
151
  const login = await this.fetchUserLogin(data.access_token, signal);
137
152
  if (login) token.login = login;
@@ -169,6 +184,62 @@ var CopilotAuth = class {
169
184
  const user = await response.json();
170
185
  return user.login;
171
186
  }
187
+ /**
188
+ * Refresh an expired Copilot token using a refresh token.
189
+ * Only works for GitHub App tokens that include a refresh_token.
190
+ *
191
+ * @param refreshToken - The refresh token from the original auth flow
192
+ * @param signal - Optional abort signal
193
+ * @returns Fresh CopilotAuthToken with new access and refresh tokens
194
+ * @throws {TokenExchangeError} If the refresh request fails
195
+ *
196
+ * @example
197
+ * ```ts
198
+ * const auth = new CopilotAuth();
199
+ * const newToken = await auth.refreshToken(oldToken.refreshToken!);
200
+ * ```
201
+ */
202
+ async refreshToken(refreshToken, signal) {
203
+ const response = await this.fetchFn(ACCESS_TOKEN_URL, {
204
+ method: "POST",
205
+ headers: {
206
+ "Content-Type": "application/x-www-form-urlencoded",
207
+ Accept: "application/json"
208
+ },
209
+ body: new URLSearchParams({
210
+ client_id: CLIENT_ID,
211
+ grant_type: "refresh_token",
212
+ refresh_token: refreshToken
213
+ }),
214
+ signal
215
+ });
216
+ if (!response.ok) {
217
+ throw new TokenExchangeError(
218
+ `Token refresh failed: ${response.status} ${response.statusText}`
219
+ );
220
+ }
221
+ const data = await response.json();
222
+ if (data.error) {
223
+ throw new TokenExchangeError(
224
+ data.error_description ?? `Token refresh error: ${data.error}`
225
+ );
226
+ }
227
+ if (!data.access_token) {
228
+ throw new TokenExchangeError("Token refresh response missing access_token");
229
+ }
230
+ const token = {
231
+ accessToken: data.access_token,
232
+ tokenType: data.token_type ?? "bearer",
233
+ obtainedAt: Date.now()
234
+ };
235
+ if (data.refresh_token) {
236
+ token.refreshToken = data.refresh_token;
237
+ }
238
+ if (data.expires_in) {
239
+ token.expiresIn = data.expires_in;
240
+ }
241
+ return token;
242
+ }
172
243
  delay(ms, signal) {
173
244
  return new Promise((resolve, reject) => {
174
245
  const timer = setTimeout(resolve, ms);