@witqq/agent-sdk 0.8.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.
Files changed (113) hide show
  1. package/dist/{agent-DxY68NZL.d.cts → agent-C6H2CgJA.d.cts} +2 -0
  2. package/dist/{agent-CW9XbmG_.d.ts → agent-F7oB6eKp.d.ts} +2 -0
  3. package/dist/backends/claude.cjs.map +1 -1
  4. package/dist/backends/claude.d.cts +2 -2
  5. package/dist/backends/claude.d.ts +2 -2
  6. package/dist/backends/claude.js.map +1 -1
  7. package/dist/backends/copilot.cjs +8 -15
  8. package/dist/backends/copilot.cjs.map +1 -1
  9. package/dist/backends/copilot.d.cts +2 -2
  10. package/dist/backends/copilot.d.ts +2 -2
  11. package/dist/backends/copilot.js +8 -15
  12. package/dist/backends/copilot.js.map +1 -1
  13. package/dist/backends/mock-llm.cjs +719 -0
  14. package/dist/backends/mock-llm.cjs.map +1 -0
  15. package/dist/backends/mock-llm.d.cts +37 -0
  16. package/dist/backends/mock-llm.d.ts +37 -0
  17. package/dist/backends/mock-llm.js +717 -0
  18. package/dist/backends/mock-llm.js.map +1 -0
  19. package/dist/backends/vercel-ai.cjs +8 -1
  20. package/dist/backends/vercel-ai.cjs.map +1 -1
  21. package/dist/backends/vercel-ai.d.cts +2 -2
  22. package/dist/backends/vercel-ai.d.ts +2 -2
  23. package/dist/backends/vercel-ai.js +8 -1
  24. package/dist/backends/vercel-ai.js.map +1 -1
  25. package/dist/backends-Cno0gZjy.d.cts +114 -0
  26. package/dist/backends-Cno0gZjy.d.ts +114 -0
  27. package/dist/chat/accumulator.cjs.map +1 -1
  28. package/dist/chat/accumulator.d.cts +2 -2
  29. package/dist/chat/accumulator.d.ts +2 -2
  30. package/dist/chat/accumulator.js.map +1 -1
  31. package/dist/chat/backends.cjs +350 -77
  32. package/dist/chat/backends.cjs.map +1 -1
  33. package/dist/chat/backends.d.cts +7 -7
  34. package/dist/chat/backends.d.ts +7 -7
  35. package/dist/chat/backends.js +349 -78
  36. package/dist/chat/backends.js.map +1 -1
  37. package/dist/chat/context.d.cts +2 -2
  38. package/dist/chat/context.d.ts +2 -2
  39. package/dist/chat/core.cjs +35 -25
  40. package/dist/chat/core.cjs.map +1 -1
  41. package/dist/chat/core.d.cts +15 -5
  42. package/dist/chat/core.d.ts +15 -5
  43. package/dist/chat/core.js +35 -26
  44. package/dist/chat/core.js.map +1 -1
  45. package/dist/chat/events.d.cts +2 -2
  46. package/dist/chat/events.d.ts +2 -2
  47. package/dist/chat/index.cjs +418 -122
  48. package/dist/chat/index.cjs.map +1 -1
  49. package/dist/chat/index.d.cts +7 -7
  50. package/dist/chat/index.d.ts +7 -7
  51. package/dist/chat/index.js +418 -124
  52. package/dist/chat/index.js.map +1 -1
  53. package/dist/chat/react.cjs +216 -12
  54. package/dist/chat/react.cjs.map +1 -1
  55. package/dist/chat/react.d.cts +78 -4
  56. package/dist/chat/react.d.ts +78 -4
  57. package/dist/chat/react.js +215 -13
  58. package/dist/chat/react.js.map +1 -1
  59. package/dist/chat/runtime.cjs +6 -2
  60. package/dist/chat/runtime.cjs.map +1 -1
  61. package/dist/chat/runtime.d.cts +2 -2
  62. package/dist/chat/runtime.d.ts +2 -2
  63. package/dist/chat/runtime.js +6 -2
  64. package/dist/chat/runtime.js.map +1 -1
  65. package/dist/chat/server.cjs +15 -5
  66. package/dist/chat/server.cjs.map +1 -1
  67. package/dist/chat/server.d.cts +3 -3
  68. package/dist/chat/server.d.ts +3 -3
  69. package/dist/chat/server.js +15 -5
  70. package/dist/chat/server.js.map +1 -1
  71. package/dist/chat/sessions.cjs +39 -23
  72. package/dist/chat/sessions.cjs.map +1 -1
  73. package/dist/chat/sessions.d.cts +2 -2
  74. package/dist/chat/sessions.d.ts +2 -2
  75. package/dist/chat/sessions.js +40 -24
  76. package/dist/chat/sessions.js.map +1 -1
  77. package/dist/chat/sqlite.cjs +95 -0
  78. package/dist/chat/sqlite.cjs.map +1 -1
  79. package/dist/chat/sqlite.d.cts +39 -3
  80. package/dist/chat/sqlite.d.ts +39 -3
  81. package/dist/chat/sqlite.js +93 -1
  82. package/dist/chat/sqlite.js.map +1 -1
  83. package/dist/chat/state.d.cts +2 -2
  84. package/dist/chat/state.d.ts +2 -2
  85. package/dist/chat/storage.cjs +39 -23
  86. package/dist/chat/storage.cjs.map +1 -1
  87. package/dist/chat/storage.d.cts +7 -3
  88. package/dist/chat/storage.d.ts +7 -3
  89. package/dist/chat/storage.js +40 -24
  90. package/dist/chat/storage.js.map +1 -1
  91. package/dist/{in-process-transport-C1JnJGVR.d.ts → in-process-transport-7EIit9Xk.d.ts} +51 -17
  92. package/dist/{in-process-transport-C7DSqPyX.d.cts → in-process-transport-Ct9YcX8I.d.cts} +51 -17
  93. package/dist/index.cjs +14 -14
  94. package/dist/index.cjs.map +1 -1
  95. package/dist/index.d.cts +4 -2
  96. package/dist/index.d.ts +4 -2
  97. package/dist/index.js +13 -13
  98. package/dist/index.js.map +1 -1
  99. package/dist/testing.cjs +724 -0
  100. package/dist/testing.cjs.map +1 -1
  101. package/dist/testing.d.cts +14 -2
  102. package/dist/testing.d.ts +14 -2
  103. package/dist/testing.js +724 -0
  104. package/dist/testing.js.map +1 -1
  105. package/dist/{transport-Cdh3M0tS.d.cts → transport-DLWCN18G.d.cts} +1 -1
  106. package/dist/{transport-Ciap4PWK.d.ts → transport-DsuS-GeM.d.ts} +1 -1
  107. package/dist/{types-ajANVzf7.d.ts → types-DgtI1hzh.d.ts} +2 -1
  108. package/dist/{types-DRgd_9R7.d.cts → types-DkSXALKg.d.cts} +2 -1
  109. package/package.json +18 -7
  110. package/LICENSE +0 -21
  111. package/README.md +0 -1054
  112. package/dist/backends-BSrsBYFn.d.cts +0 -39
  113. package/dist/backends-BSrsBYFn.d.ts +0 -39
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var fs = require('fs');
4
+ var promises = require('fs/promises');
4
5
  var path = require('path');
5
6
 
6
7
  // src/chat/types.ts
@@ -114,25 +115,25 @@ var FileStorage = class {
114
115
  constructor(options) {
115
116
  this.directory = options.directory;
116
117
  this.extension = options.extension ?? ".json";
117
- this.ensureDirectory();
118
+ this.ensureDirectorySync();
118
119
  }
119
120
  /** @inheritdoc */
120
121
  async get(key) {
121
122
  const filePath = this.keyToPath(key);
122
- if (!fs.existsSync(filePath)) {
123
+ if (!await this.fileExists(filePath)) {
123
124
  return null;
124
125
  }
125
- return this.readFile(filePath);
126
+ return this.readJsonFile(filePath);
126
127
  }
127
128
  /** @inheritdoc */
128
129
  async list(options) {
129
- this.ensureDirectory();
130
- const files = fs.readdirSync(this.directory).filter(
130
+ await this.ensureDirectoryAsync();
131
+ const files = (await promises.readdir(this.directory)).filter(
131
132
  (f) => f.endsWith(this.extension)
132
133
  );
133
134
  let items = [];
134
135
  for (const file of files) {
135
- const item = this.readFile(path.join(this.directory, file));
136
+ const item = await this.readJsonFile(path.join(this.directory, file));
136
137
  items.push(item);
137
138
  }
138
139
  if (options?.filter) {
@@ -152,55 +153,55 @@ var FileStorage = class {
152
153
  /** @inheritdoc */
153
154
  async create(key, item) {
154
155
  const filePath = this.keyToPath(key);
155
- if (fs.existsSync(filePath)) {
156
+ if (await this.fileExists(filePath)) {
156
157
  throw new StorageError(
157
158
  `Item with key "${key}" already exists`,
158
159
  "STORAGE_DUPLICATE_KEY" /* STORAGE_DUPLICATE_KEY */
159
160
  );
160
161
  }
161
- this.writeFile(filePath, item);
162
+ await this.writeJsonFile(filePath, item);
162
163
  }
163
164
  /** @inheritdoc */
164
165
  async update(key, item) {
165
166
  const filePath = this.keyToPath(key);
166
- if (!fs.existsSync(filePath)) {
167
+ if (!await this.fileExists(filePath)) {
167
168
  throw new StorageError(
168
169
  `Item with key "${key}" not found`,
169
170
  "STORAGE_NOT_FOUND" /* STORAGE_NOT_FOUND */
170
171
  );
171
172
  }
172
- this.writeFile(filePath, item);
173
+ await this.writeJsonFile(filePath, item);
173
174
  }
174
175
  /** @inheritdoc */
175
176
  async delete(key) {
176
177
  const filePath = this.keyToPath(key);
177
- if (!fs.existsSync(filePath)) {
178
+ if (!await this.fileExists(filePath)) {
178
179
  throw new StorageError(
179
180
  `Item with key "${key}" not found`,
180
181
  "STORAGE_NOT_FOUND" /* STORAGE_NOT_FOUND */
181
182
  );
182
183
  }
183
- fs.unlinkSync(filePath);
184
+ await promises.unlink(filePath);
184
185
  }
185
186
  /** @inheritdoc */
186
187
  async has(key) {
187
- return fs.existsSync(this.keyToPath(key));
188
+ return this.fileExists(this.keyToPath(key));
188
189
  }
189
190
  /** @inheritdoc */
190
191
  async count() {
191
- this.ensureDirectory();
192
- return fs.readdirSync(this.directory).filter(
192
+ await this.ensureDirectoryAsync();
193
+ return (await promises.readdir(this.directory)).filter(
193
194
  (f) => f.endsWith(this.extension)
194
195
  ).length;
195
196
  }
196
197
  /** @inheritdoc */
197
198
  async clear() {
198
- this.ensureDirectory();
199
- const files = fs.readdirSync(this.directory).filter(
199
+ await this.ensureDirectoryAsync();
200
+ const files = (await promises.readdir(this.directory)).filter(
200
201
  (f) => f.endsWith(this.extension)
201
202
  );
202
203
  for (const file of files) {
203
- fs.unlinkSync(path.join(this.directory, file));
204
+ await promises.unlink(path.join(this.directory, file));
204
205
  }
205
206
  }
206
207
  keyToPath(key) {
@@ -210,14 +211,29 @@ var FileStorage = class {
210
211
  );
211
212
  return path.join(this.directory, `${safeKey}${this.extension}`);
212
213
  }
213
- ensureDirectory() {
214
+ /** Sync directory init — used only in constructor (one-time). */
215
+ ensureDirectorySync() {
214
216
  if (!fs.existsSync(this.directory)) {
215
217
  fs.mkdirSync(this.directory, { recursive: true });
216
218
  }
217
219
  }
218
- readFile(filePath) {
220
+ /** Async directory init — used in operations. */
221
+ async ensureDirectoryAsync() {
222
+ if (!await this.fileExists(this.directory)) {
223
+ await promises.mkdir(this.directory, { recursive: true });
224
+ }
225
+ }
226
+ async fileExists(filePath) {
227
+ try {
228
+ await promises.access(filePath);
229
+ return true;
230
+ } catch {
231
+ return false;
232
+ }
233
+ }
234
+ async readJsonFile(filePath) {
219
235
  try {
220
- const content = fs.readFileSync(filePath, "utf-8");
236
+ const content = await promises.readFile(filePath, "utf-8");
221
237
  return JSON.parse(content);
222
238
  } catch (error) {
223
239
  if (error instanceof SyntaxError) {
@@ -232,10 +248,10 @@ var FileStorage = class {
232
248
  );
233
249
  }
234
250
  }
235
- writeFile(filePath, item) {
251
+ async writeJsonFile(filePath, item) {
236
252
  try {
237
253
  const content = JSON.stringify(item, null, 2);
238
- fs.writeFileSync(filePath, content, "utf-8");
254
+ await promises.writeFile(filePath, content, "utf-8");
239
255
  } catch {
240
256
  throw new StorageError(
241
257
  `Failed to write file: ${filePath}`,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/chat/types.ts","../../src/errors.ts","../../src/chat/storage.ts","../../src/chat/sessions.ts"],"names":["existsSync","readdirSync","join","unlinkSync","mkdirSync","readFileSync","writeFileSync"],"mappings":";;;;;;AAmBO,SAAS,YAAA,GAAuB;AACrC,EAAA,OAAO,OAAO,UAAA,EAAW;AAC3B;;;ACLO,IAAM,aAAA,GAAN,cAA4B,KAAA,CAAM;AAAA;AAAA,EAE9B,cAAA,GAAiB,IAAA;AAAA;AAAA,EAEjB,IAAA;AAAA;AAAA,EAEA,SAAA;AAAA;AAAA,EAEA,UAAA;AAAA,EAET,WAAA,CAAY,SAAiB,OAAA,EAAgC;AAC3D,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AACtB,IAAA,IAAA,CAAK,IAAA,GAAO,eAAA;AACZ,IAAA,IAAA,CAAK,OAAO,OAAA,EAAS,IAAA;AACrB,IAAA,IAAA,CAAK,SAAA,GAAY,SAAS,SAAA,IAAa,KAAA;AACvC,IAAA,IAAA,CAAK,aAAa,OAAA,EAAS,UAAA;AAAA,EAC7B;AAAA;AAAA,EAGA,OAAO,GAAG,KAAA,EAAwC;AAChD,IAAA,OACE,KAAA,YAAiB,KAAA,IACjB,gBAAA,IAAoB,KAAA,IACnB,MA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C,GAAA,KAAQ;AACpC,UAAA,OAAO,IAAI,KAAA,CAAM,IAAA;AAAA,YACf,CAAC,IAAA,KACC,IAAA,CAAK,IAAA,KAAS,MAAA,IACd,KAAK,IAAA,CAAK,WAAA,EAAY,CAAE,QAAA,CAAS,KAAK;AAAA,WAC1C;AAAA,QACF,CAAC,CAAA;AAAA,MACH,CAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KAAA,GAAyB;AAC7B,IAAA,OAAO,IAAA,CAAK,QAAQ,KAAA,EAAM;AAAA,EAC5B;AAAA,EAEA,MAAM,KAAA,GAAuB;AAC3B,IAAA,OAAO,IAAA,CAAK,QAAQ,KAAA,EAAM;AAAA,EAC5B;AACF,CAAA;AAgBO,IAAM,oBAAA,GAAN,cAAmC,gBAAA,CAAiB;AAAA,EACzD,WAAA,GAAc;AACZ,IAAA,KAAA,CAAM,IAAI,iBAA8B,CAAA;AAAA,EAC1C;AACF;AAsBO,IAAM,gBAAA,GAAN,cAA+B,gBAAA,CAAiB;AAAA,EACrD,YAAY,OAAA,EAAkC;AAC5C,IAAA,KAAA,CAAM,IAAI,WAAA,CAAyB,EAAE,WAAW,OAAA,CAAQ,SAAA,EAAW,CAAC,CAAA;AAAA,EACtE;AACF","file":"sessions.cjs","sourcesContent":["/**\n * @witqq/agent-sdk — Chat domain types\n *\n * All type definitions and interfaces for the chat layer.\n * Pure types + ChatId generation (tightly coupled to branded type).\n */\n\nimport type { UsageData, ToolDefinition, ErrorCode } from \"../types.js\";\nimport type { AuthToken } from \"../auth/types.js\";\n\n// ─── Unique ID ─────────────────────────────────────────────────\n\n/** Branded type for unique identifiers */\nexport type ChatId = string & { readonly __brand: \"ChatId\" };\n\n/**\n * Generate a new unique ChatId (crypto.randomUUID-based)\n * @returns Branded ChatId string\n */\nexport function createChatId(): ChatId {\n return crypto.randomUUID() as ChatId;\n}\n\n/** UUID v4 pattern for ChatId validation */\nconst UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n\n/**\n * Cast a string to ChatId with UUID format validation.\n * Use this instead of manual `as ChatId` type assertions.\n *\n * @param value - String to validate and cast\n * @returns Branded ChatId\n * @throws {TypeError} If value is not a valid UUID v4 format\n */\nexport function toChatId(value: string): ChatId {\n if (!UUID_RE.test(value)) {\n throw new TypeError(`Invalid ChatId: \"${value}\" is not a valid UUID`);\n }\n return value as ChatId;\n}\n\n/**\n * Accepts either a plain string or branded ChatId for API convenience.\n * Use this in public API signatures so consumers don't need `as ChatId` casts.\n */\nexport type ChatIdLike = string | ChatId;\n\n// ─── Status Types ──────────────────────────────────────────────\n\n/** Lifecycle status of a message part (text, reasoning, etc.) */\nexport type PartStatus = \"pending\" | \"streaming\" | \"complete\" | \"error\";\n/** Lifecycle status of a tool call within a message */\nexport type ToolCallStatus = \"pending\" | \"running\" | \"requires_approval\" | \"complete\" | \"error\" | \"denied\";\n/** Lifecycle status of an entire message */\nexport type MessageStatus = \"pending\" | \"streaming\" | \"complete\" | \"error\" | \"cancelled\";\n/** Lifecycle status of a chat session */\nexport type SessionStatus = \"active\";\n/** Lifecycle status of the chat runtime */\nexport type RuntimeStatus = \"idle\" | \"streaming\" | \"error\" | \"disposed\";\n\n// ─── Message Parts (union) ─────────────────────────────────────\n\n/** Plain text content part */\nexport interface TextPart { type: \"text\"; text: string; status: PartStatus; }\n/** Model reasoning/thinking content part */\nexport interface ReasoningPart { type: \"reasoning\"; text: string; status: PartStatus; }\n/** Tool invocation part with call ID, arguments, optional result */\nexport interface ToolCallPart { type: \"tool_call\"; toolCallId: string; name: string; args: unknown; result?: unknown; status: ToolCallStatus; error?: string; }\n/** Source reference part (URL citation) */\nexport interface SourcePart { type: \"source\"; url: string; title?: string; status: PartStatus; }\n/** File attachment part (base64-encoded data) */\nexport interface FilePart { type: \"file\"; name: string; mimeType: string; data: string; status: PartStatus; }\n/** Union of all message part types */\nexport type MessagePart = TextPart | ReasoningPart | ToolCallPart | SourcePart | FilePart;\n\n// ─── Chat Message ──────────────────────────────────────────────\n\n/** Role of message author */\nexport type ChatRole = \"user\" | \"assistant\" | \"system\";\n\n/** Metadata attached to messages — useful preset for the TMetadata generic */\nexport interface ChatMessageMetadata {\n model?: string;\n backend?: string;\n usage?: UsageData;\n isSummary?: boolean;\n estimatedTokens?: number;\n custom?: Record<string, unknown>;\n}\n\n/** Message status */\nexport type ChatMessageStatus = MessageStatus;\n\n/** A single chat message — the fundamental unit of conversation */\nexport interface ChatMessage<TMetadata = unknown> {\n id: ChatId;\n role: ChatRole;\n parts: MessagePart[];\n metadata?: TMetadata;\n createdAt: string;\n updatedAt?: string;\n status: MessageStatus;\n}\n\n// ─── Supporting Types ──────────────────────────────────────────\n\n// ─── Chat Session ──────────────────────────────────────────────\n\n/** Session configuration snapshot */\nexport interface ChatSessionConfig {\n model: string;\n backend: string;\n systemPrompt?: string;\n temperature?: number;\n maxTokens?: number;\n}\n\n/**\n * Session metadata tracking usage statistics and custom extensions.\n *\n * Updated automatically by session stores on each `addMessage()` call.\n * The generic `TCustom` parameter allows type-safe application-specific\n * metadata via the `custom` field.\n *\n * @typeParam TCustom - Shape of the `custom` field (defaults to `Record<string, unknown>`)\n */\nexport interface ChatSessionMetadata<TCustom extends Record<string, unknown> = Record<string, unknown>> {\n /** Number of messages in the session (updated by session store) */\n messageCount: number;\n /** Total token count across all messages in the session */\n totalTokens: number;\n /** Optional tags for session categorization and filtering */\n tags?: string[];\n /** Application-specific metadata — typed via the TCustom generic parameter */\n custom?: TCustom;\n}\n\n/** Chat session — a conversation with ordered messages (pure serializable data) */\nexport interface ChatSession<TCustom extends Record<string, unknown> = Record<string, unknown>> {\n id: ChatId;\n title?: string;\n messages: ChatMessage[];\n config: ChatSessionConfig;\n metadata: ChatSessionMetadata<TCustom>;\n status: SessionStatus;\n createdAt: string;\n updatedAt: string;\n backendSessionId?: string;\n}\n\n/**\n * Reactive wrapper around ChatSession — provides subscribe/getSnapshot for\n * React useSyncExternalStore integration and lastMessage convenience getter.\n * Session stores may optionally return ObservableSession instances.\n */\nexport interface ObservableSession<TCustom extends Record<string, unknown> = Record<string, unknown>>\n extends ChatSession<TCustom> {\n /** Subscribe to session changes (for React useSyncExternalStore) */\n subscribe(callback: () => void): () => void;\n /** Get immutable snapshot of session state (for React useSyncExternalStore) */\n getSnapshot(): ChatSession<TCustom>;\n /** Last message in the session */\n readonly lastMessage: ChatMessage | undefined;\n}\n\n/** Lightweight session info for listing (without full message array) */\nexport interface SessionInfo {\n id: ChatId;\n title?: string;\n status: SessionStatus;\n messageCount: number;\n lastMessage?: ChatMessage;\n createdAt: string;\n updatedAt: string;\n}\n\n// ─── Chat Events ───────────────────────────────────────────────\n\n/** Events emitted during chat operation */\nexport type ChatEvent =\n | { type: \"message:start\"; messageId: ChatId; role: ChatRole }\n | { type: \"message:delta\"; messageId: ChatId; text: string }\n | { type: \"message:complete\"; messageId: ChatId; message: ChatMessage }\n | {\n type: \"tool:start\";\n messageId: ChatId;\n toolCallId: string;\n toolName: string;\n args: Record<string, unknown>;\n }\n | {\n type: \"tool:complete\";\n messageId: ChatId;\n toolCallId: string;\n toolName: string;\n result: unknown;\n isError?: boolean;\n }\n | { type: \"thinking:start\"; messageId: ChatId }\n | { type: \"thinking:delta\"; messageId: ChatId; text: string }\n | { type: \"thinking:end\"; messageId: ChatId }\n | {\n type: \"permission:request\";\n messageId: ChatId;\n toolName: string;\n toolArgs: Record<string, unknown>;\n }\n | {\n type: \"permission:response\";\n messageId: ChatId;\n toolName: string;\n allowed: boolean;\n }\n | {\n type: \"usage\";\n promptTokens: number;\n completionTokens: number;\n model?: string;\n }\n | { type: \"session:created\"; sessionId: ChatId }\n | { type: \"session:updated\"; sessionId: ChatId }\n | {\n type: \"error\";\n error: string;\n recoverable: boolean;\n code?: ErrorCode;\n messageId?: ChatId;\n }\n | { type: \"typing:start\" }\n | { type: \"typing:end\" }\n | { type: \"heartbeat\" }\n | { type: \"done\"; finalOutput?: string };\n\n/** All possible ChatEvent type strings */\nexport type ChatEventType = ChatEvent[\"type\"];\n\n// ─── Chat Middleware ───────────────────────────────────────────\n\n/** Context passed to ChatMiddleware hooks */\nexport interface ChatMiddlewareContext {\n sessionId: ChatId;\n signal: AbortSignal;\n}\n\n/** Runtime-level middleware for the send/receive lifecycle.\n * Different from EventMiddleware which operates at the event bus level. */\nexport interface ChatMiddleware {\n /** Transform message before sending to backend. Return null to reject the send. */\n onBeforeSend?(message: ChatMessage, context: ChatMiddlewareContext): ChatMessage | null | Promise<ChatMessage | null>;\n /** Transform/intercept stream events */\n onEvent?(event: ChatEvent, context: ChatMiddlewareContext): ChatEvent | null | Promise<ChatEvent | null>;\n /** Transform completed message after receiving from backend */\n onAfterReceive?(message: ChatMessage, context: ChatMiddlewareContext): ChatMessage | Promise<ChatMessage>;\n /** Intercept errors — return null to suppress, return error to propagate */\n onError?(error: Error, context: ChatMiddlewareContext): Error | null | Promise<Error | null>;\n}\n\n// ─── Chat Provider Abstraction ─────────────────────────────────\n\n/** Options for sending a message to a provider */\nexport interface SendMessageOptions {\n signal?: AbortSignal;\n /** Model to use for this request. Required for server-side runtime.send(). */\n model?: string;\n /** Per-call system prompt override (forwarded to the backend agent) */\n systemPrompt?: string;\n context?: Record<string, unknown>;\n /** Additional tools to include in this request */\n tools?: ToolDefinition[];\n}\n\n/** Options for runtime.send() — requires backend routing info */\nexport interface RuntimeSendOptions {\n /** Backend to route this request to (key in backends map) */\n backend: string;\n /** Authentication credentials for the backend factory */\n credentials: AuthToken;\n /** Model to use for this request */\n model: string;\n /** Per-call system prompt override (forwarded to the backend agent) */\n systemPrompt?: string;\n /** Abort signal */\n signal?: AbortSignal;\n /** Request-scoped context */\n context?: Record<string, unknown>;\n /** Additional tools */\n tools?: ToolDefinition[];\n}\n\n/**\n * @deprecated IChatProvider has been inlined into IChatBackend.\n * Import IChatBackend from \"@witqq/agent-sdk/chat/backends\" instead.\n * Kept as type alias for backward compatibility.\n */\nexport type IChatProvider = import(\"./backends/types.js\").IChatBackend;\n\n// ─── Factory Functions ─────────────────────────────────────────\n\n/**\n * Create a simple text ChatMessage.\n *\n * @param text - Message text content\n * @param role - Message role (default: \"user\")\n * @returns A complete ChatMessage with a single TextPart\n */\nexport function createTextMessage(text: string, role: ChatRole = \"user\"): ChatMessage {\n return {\n id: createChatId(),\n role,\n parts: [{ type: \"text\", text, status: \"complete\" }],\n createdAt: new Date().toISOString(),\n status: \"complete\",\n };\n}\n\n/** Type guard: checks if a session has reactive API (subscribe/getSnapshot) */\nexport function isObservableSession<TCustom extends Record<string, unknown> = Record<string, unknown>>(\n session: ChatSession<TCustom>,\n): session is ObservableSession<TCustom> {\n return \"subscribe\" in session && typeof (session as ObservableSession<TCustom>).subscribe === \"function\"\n && \"getSnapshot\" in session && typeof (session as ObservableSession<TCustom>).getSnapshot === \"function\";\n}\n","import { ErrorCode } from \"./types/errors.js\";\n\n/** Options for constructing an AgentSDKError */\nexport interface AgentSDKErrorOptions extends ErrorOptions {\n /** Machine-readable error code */\n code?: string;\n /** Whether this error is retryable (default: false) */\n retryable?: boolean;\n /** HTTP status code hint (e.g. 401, 429, 500) */\n httpStatus?: number;\n}\n\n/** Base error class for agent-sdk.\n *\n * Use `AgentSDKError.is(err)` for reliable cross-module `instanceof` checks\n * (works across separately bundled entry points where `instanceof` may fail). */\nexport class AgentSDKError extends Error {\n /** @internal Marker for cross-bundle identity checks */\n readonly _agentSDKError = true as const;\n /** Machine-readable error code. Prefer values from the ErrorCode enum. */\n readonly code?: string;\n /** Whether this error is safe to retry */\n readonly retryable: boolean;\n /** HTTP status code hint for error classification */\n readonly httpStatus?: number;\n\n constructor(message: string, options?: AgentSDKErrorOptions) {\n super(message, options);\n this.name = \"AgentSDKError\";\n this.code = options?.code;\n this.retryable = options?.retryable ?? false;\n this.httpStatus = options?.httpStatus;\n }\n\n /** Check if an error is an AgentSDKError (works across bundled copies) */\n static is(error: unknown): error is AgentSDKError {\n return (\n error instanceof Error &&\n \"_agentSDKError\" in error &&\n (error as AgentSDKError)._agentSDKError === true\n );\n }\n}\n\n/** Thrown when agent.run() is called while already running (M8 re-entrancy guard) */\nexport class ReentrancyError extends AgentSDKError {\n constructor() {\n super(\"Agent is already running. Await the current run before starting another.\", {\n code: ErrorCode.REENTRANCY,\n });\n this.name = \"ReentrancyError\";\n }\n}\n\n/** Thrown when an operation is attempted on a disposed agent/service */\nexport class DisposedError extends AgentSDKError {\n constructor(entity: string) {\n super(`${entity} has been disposed and cannot be used.`, {\n code: ErrorCode.DISPOSED,\n });\n this.name = \"DisposedError\";\n }\n}\n\n/** Thrown when a backend is not found in the registry */\nexport class BackendNotFoundError extends AgentSDKError {\n constructor(backend: string) {\n super(\n `Unknown backend: \"${backend}\". ` +\n `Built-in: copilot, claude, vercel-ai. ` +\n `Custom: use registerBackend() first.`,\n { code: ErrorCode.BACKEND_NOT_INSTALLED },\n );\n this.name = \"BackendNotFoundError\";\n }\n}\n\n/** Thrown when a backend is already registered */\nexport class BackendAlreadyRegisteredError extends AgentSDKError {\n constructor(backend: string) {\n super(`Backend \"${backend}\" is already registered. Use a different name or unregister first.`);\n this.name = \"BackendAlreadyRegisteredError\";\n }\n}\n\n/** Thrown when subprocess management fails */\nexport class SubprocessError extends AgentSDKError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, { ...options, code: ErrorCode.DEPENDENCY_MISSING });\n this.name = \"SubprocessError\";\n }\n}\n\n/** Thrown when a required peer dependency is not installed */\nexport class DependencyError extends AgentSDKError {\n public readonly packageName: string;\n\n constructor(packageName: string) {\n super(`${packageName} is not installed. Install it: npm install ${packageName}`, {\n code: ErrorCode.DEPENDENCY_MISSING,\n });\n this.name = \"DependencyError\";\n this.packageName = packageName;\n }\n}\n\n/** Thrown when an agent run is aborted */\nexport class AbortError extends AgentSDKError {\n constructor() {\n super(\"Agent run was aborted.\", { code: ErrorCode.ABORTED });\n this.name = \"AbortError\";\n }\n}\n\n/** Thrown when a tool execution fails */\nexport class ToolExecutionError extends AgentSDKError {\n public readonly toolName: string;\n\n constructor(toolName: string, message: string, options?: ErrorOptions) {\n super(`Tool \"${toolName}\" failed: ${message}`, { ...options, code: ErrorCode.TOOL_EXECUTION });\n this.name = \"ToolExecutionError\";\n this.toolName = toolName;\n }\n}\n\n/** Thrown when a stream has no activity within the configured timeout */\nexport class ActivityTimeoutError extends AgentSDKError {\n constructor(timeoutMs: number) {\n super(`Stream activity timeout: no event received within ${timeoutMs}ms.`, {\n code: ErrorCode.TIMEOUT,\n retryable: true,\n });\n this.name = \"ActivityTimeoutError\";\n }\n}\n\n/** Thrown when structured output parsing fails */\nexport class StructuredOutputError extends AgentSDKError {\n constructor(message: string, options?: ErrorOptions) {\n super(`Structured output error: ${message}`, { ...options, code: ErrorCode.INVALID_RESPONSE });\n this.name = \"StructuredOutputError\";\n }\n}\n","/**\n * @witqq/agent-sdk/chat/storage\n *\n * Generic storage adapter layer with pluggable backends.\n * Provides CRUD operations for any data type via `IStorageAdapter<T>`.\n * Implementations: `InMemoryStorage` (Map-based) and `FileStorage` (JSON files).\n */\n\nimport { existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { AgentSDKError } from \"../errors.js\";\nimport { ErrorCode } from \"../types/errors.js\";\n\n// ─── Storage Errors ────────────────────────────────────────────\n\n/**\n * Error thrown by storage operations.\n *\n * @example\n * ```typescript\n * try {\n * await store.get(\"missing-id\");\n * } catch (e) {\n * if (e instanceof StorageError && e.code === ErrorCode.STORAGE_NOT_FOUND) {\n * // handle missing item\n * }\n * }\n * ```\n */\nexport class StorageError extends AgentSDKError {\n /** Machine-readable error code from the unified ErrorCode enum */\n readonly code: StorageErrorCode;\n\n constructor(message: string, code: StorageErrorCode) {\n super(message);\n this.name = \"StorageError\";\n this.code = code;\n }\n}\n\n/** Storage-specific subset of ErrorCode */\nexport type StorageErrorCode =\n | ErrorCode.STORAGE_NOT_FOUND\n | ErrorCode.STORAGE_DUPLICATE_KEY\n | ErrorCode.STORAGE_IO_ERROR\n | ErrorCode.STORAGE_SERIALIZATION_ERROR;\n\n// ─── Storage Adapter Interface ─────────────────────────────────\n\n/**\n * Options for listing stored items.\n *\n * @typeParam T - The type of stored items\n */\nexport interface ListOptions<T> {\n /** Filter predicate — return `true` to include the item */\n filter?: (item: T) => boolean;\n /** Sort comparator — standard Array.sort semantics */\n sort?: (a: T, b: T) => number;\n /** Maximum number of items to return */\n limit?: number;\n /** Number of items to skip (for pagination) */\n offset?: number;\n}\n\n/**\n * Generic storage adapter for CRUD operations on any data type.\n * Items are identified by a string key.\n *\n * @typeParam T - The type of stored items\n *\n * @example\n * ```typescript\n * const store: IStorageAdapter<{ name: string }> = new InMemoryStorage();\n * await store.create(\"key1\", { name: \"Alice\" });\n * const item = await store.get(\"key1\"); // { name: \"Alice\" }\n * ```\n */\nexport interface IStorageAdapter<T> {\n /**\n * Retrieve an item by key.\n * @param key - Unique identifier\n * @returns The item, or `null` if not found\n */\n get(key: string): Promise<T | null>;\n\n /**\n * List items with optional filtering, sorting, and pagination.\n * @param options - Filter, sort, limit, offset options\n * @returns Array of matching items\n */\n list(options?: ListOptions<T>): Promise<T[]>;\n\n /**\n * Create a new item. Throws `StorageError` with code `DUPLICATE_KEY` if key exists.\n * @param key - Unique identifier\n * @param item - Data to store\n */\n create(key: string, item: T): Promise<void>;\n\n /**\n * Update an existing item. Throws `StorageError` with code `NOT_FOUND` if key missing.\n * @param key - Unique identifier\n * @param item - Updated data\n */\n update(key: string, item: T): Promise<void>;\n\n /**\n * Delete an item by key. Throws `StorageError` with code `NOT_FOUND` if key missing.\n * @param key - Unique identifier\n */\n delete(key: string): Promise<void>;\n\n /**\n * Check whether a key exists.\n * @param key - Unique identifier\n * @returns `true` if key exists\n */\n has(key: string): Promise<boolean>;\n\n /**\n * Return the number of stored items.\n * @returns Count of items\n */\n count(): Promise<number>;\n\n /**\n * Remove all items from storage.\n */\n clear(): Promise<void>;\n\n /**\n * Release any resources held by this adapter (DB connections, file handles).\n * Optional — adapters that don't hold resources need not implement this.\n */\n dispose?(): Promise<void>;\n}\n\n// ─── InMemoryStorage ───────────────────────────────────────────\n\n/**\n * In-memory storage adapter backed by a `Map`.\n * Suitable for development, testing, and short-lived processes.\n * Data is lost when the process exits.\n *\n * @typeParam T - The type of stored items\n *\n * @example\n * ```typescript\n * const store = new InMemoryStorage<{ name: string }>();\n * await store.create(\"k1\", { name: \"Alice\" });\n * await store.create(\"k2\", { name: \"Bob\" });\n * const items = await store.list({ filter: i => i.name.startsWith(\"A\") });\n * // [{ name: \"Alice\" }]\n * ```\n */\nexport class InMemoryStorage<T> implements IStorageAdapter<T> {\n private readonly data = new Map<string, T>();\n\n /** @inheritdoc */\n async get(key: string): Promise<T | null> {\n const item = this.data.get(key);\n return item !== undefined ? structuredClone(item) : null;\n }\n\n /** @inheritdoc */\n async list(options?: ListOptions<T>): Promise<T[]> {\n let items = Array.from(this.data.values()).map((item) => structuredClone(item));\n\n if (options?.filter) {\n items = items.filter(options.filter);\n }\n if (options?.sort) {\n items.sort(options.sort);\n }\n if (options?.offset !== undefined) {\n items = items.slice(options.offset);\n }\n if (options?.limit !== undefined) {\n items = items.slice(0, options.limit);\n }\n\n return items;\n }\n\n /** @inheritdoc */\n async create(key: string, item: T): Promise<void> {\n if (this.data.has(key)) {\n throw new StorageError(\n `Item with key \"${key}\" already exists`,\n ErrorCode.STORAGE_DUPLICATE_KEY,\n );\n }\n this.data.set(key, structuredClone(item));\n }\n\n /** @inheritdoc */\n async update(key: string, item: T): Promise<void> {\n if (!this.data.has(key)) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n this.data.set(key, structuredClone(item));\n }\n\n /** @inheritdoc */\n async delete(key: string): Promise<void> {\n if (!this.data.has(key)) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n this.data.delete(key);\n }\n\n /** @inheritdoc */\n async has(key: string): Promise<boolean> {\n return this.data.has(key);\n }\n\n /** @inheritdoc */\n async count(): Promise<number> {\n return this.data.size;\n }\n\n /** @inheritdoc */\n async clear(): Promise<void> {\n this.data.clear();\n }\n}\n\n// ─── FileStorage ───────────────────────────────────────────────\n\n/**\n * Options for configuring `FileStorage`.\n */\nexport interface FileStorageOptions {\n /** Directory path where JSON files are stored */\n directory: string;\n /** File extension (default: `.json`) */\n extension?: string;\n}\n\n/**\n * File-based storage adapter that persists each item as a JSON file.\n * Suitable for local applications, CLI tools, and development.\n * Creates the storage directory if it doesn't exist.\n *\n * @typeParam T - The type of stored items (must be JSON-serializable)\n *\n * @example\n * ```typescript\n * const store = new FileStorage<ChatSession>({\n * directory: \"./data/sessions\",\n * });\n * await store.create(\"session-1\", mySession);\n * ```\n */\nexport class FileStorage<T> implements IStorageAdapter<T> {\n private readonly directory: string;\n private readonly extension: string;\n\n constructor(options: FileStorageOptions) {\n this.directory = options.directory;\n this.extension = options.extension ?? \".json\";\n this.ensureDirectory();\n }\n\n /** @inheritdoc */\n async get(key: string): Promise<T | null> {\n const filePath = this.keyToPath(key);\n if (!existsSync(filePath)) {\n return null;\n }\n return this.readFile(filePath);\n }\n\n /** @inheritdoc */\n async list(options?: ListOptions<T>): Promise<T[]> {\n this.ensureDirectory();\n const files = readdirSync(this.directory).filter((f) =>\n f.endsWith(this.extension),\n );\n\n let items: T[] = [];\n for (const file of files) {\n const item = this.readFile(join(this.directory, file));\n items.push(item);\n }\n\n if (options?.filter) {\n items = items.filter(options.filter);\n }\n if (options?.sort) {\n items.sort(options.sort);\n }\n if (options?.offset !== undefined) {\n items = items.slice(options.offset);\n }\n if (options?.limit !== undefined) {\n items = items.slice(0, options.limit);\n }\n\n return items;\n }\n\n /** @inheritdoc */\n async create(key: string, item: T): Promise<void> {\n const filePath = this.keyToPath(key);\n if (existsSync(filePath)) {\n throw new StorageError(\n `Item with key \"${key}\" already exists`,\n ErrorCode.STORAGE_DUPLICATE_KEY,\n );\n }\n this.writeFile(filePath, item);\n }\n\n /** @inheritdoc */\n async update(key: string, item: T): Promise<void> {\n const filePath = this.keyToPath(key);\n if (!existsSync(filePath)) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n this.writeFile(filePath, item);\n }\n\n /** @inheritdoc */\n async delete(key: string): Promise<void> {\n const filePath = this.keyToPath(key);\n if (!existsSync(filePath)) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n unlinkSync(filePath);\n }\n\n /** @inheritdoc */\n async has(key: string): Promise<boolean> {\n return existsSync(this.keyToPath(key));\n }\n\n /** @inheritdoc */\n async count(): Promise<number> {\n this.ensureDirectory();\n return readdirSync(this.directory).filter((f) =>\n f.endsWith(this.extension),\n ).length;\n }\n\n /** @inheritdoc */\n async clear(): Promise<void> {\n this.ensureDirectory();\n const files = readdirSync(this.directory).filter((f) =>\n f.endsWith(this.extension),\n );\n for (const file of files) {\n unlinkSync(join(this.directory, file));\n }\n }\n\n private keyToPath(key: string): string {\n const safeKey = key.replace(/[^a-zA-Z0-9_-]/g, (c) =>\n \"%\" + c.charCodeAt(0).toString(16).padStart(2, \"0\"),\n );\n return join(this.directory, `${safeKey}${this.extension}`);\n }\n\n private ensureDirectory(): void {\n if (!existsSync(this.directory)) {\n mkdirSync(this.directory, { recursive: true });\n }\n }\n\n private readFile(filePath: string): T {\n try {\n const content = readFileSync(filePath, \"utf-8\");\n return JSON.parse(content) as T;\n } catch (error) {\n if (error instanceof SyntaxError) {\n throw new StorageError(\n `Failed to parse file: ${filePath}`,\n ErrorCode.STORAGE_SERIALIZATION_ERROR,\n );\n }\n throw new StorageError(\n `Failed to read file: ${filePath}`,\n ErrorCode.STORAGE_IO_ERROR,\n );\n }\n }\n\n private writeFile(filePath: string, item: T): void {\n try {\n const content = JSON.stringify(item, null, 2);\n writeFileSync(filePath, content, \"utf-8\");\n } catch {\n throw new StorageError(\n `Failed to write file: ${filePath}`,\n ErrorCode.STORAGE_IO_ERROR,\n );\n }\n }\n}\n","/**\n * @witqq/agent-sdk/chat/sessions\n *\n * Session store layer wrapping generic storage adapters.\n * Provides session-specific operations: message management,\n * paginated retrieval, search, and session lifecycle.\n */\n\nimport type {\n ChatSession,\n ChatMessage,\n ChatId,\n ChatSessionConfig,\n} from \"./core.js\";\nimport { createChatId } from \"./core.js\";\nimport type { IStorageAdapter, ListOptions } from \"./storage.js\";\nimport { InMemoryStorage, FileStorage, StorageError } from \"./storage.js\";\nimport { ErrorCode } from \"../types/errors.js\";\n\n// ─── Session Store Interface ───────────────────────────────────\n\n/** Options for creating a new session */\nexport interface CreateSessionOptions<TCustom extends Record<string, unknown> = Record<string, unknown>> {\n /** Session title (defaults to \"Untitled\") */\n title?: string;\n /** Session configuration (optional — runtime defaults used when omitted) */\n config?: Partial<ChatSessionConfig>;\n /** Initial tags */\n tags?: string[];\n /** Custom metadata */\n custom?: TCustom;\n}\n\n/** Paginated result of messages */\nexport interface PaginatedMessages {\n /** Messages in this page */\n messages: ChatMessage[];\n /** Total number of messages in session */\n total: number;\n /** Whether there are more messages after this page */\n hasMore: boolean;\n}\n\n/** Options for listing sessions */\nexport interface SessionListOptions {\n /** Filter predicate */\n filter?: (session: ChatSession) => boolean;\n /** Sort comparator */\n sort?: (a: ChatSession, b: ChatSession) => number;\n /** Maximum number of sessions to return */\n limit?: number;\n /** Number of sessions to skip */\n offset?: number;\n}\n\n/** Search options for finding sessions */\nexport interface SessionSearchOptions {\n /** Text query to match against title and message content */\n query: string;\n /** Maximum results (default: 20) */\n limit?: number;\n}\n\n/**\n * Read-only session operations.\n * Consumers needing read-only access (dashboards, analytics) implement only this.\n */\nexport interface ISessionReader {\n getSession(id: ChatId): Promise<ChatSession | null>;\n listSessions(options?: SessionListOptions): Promise<ChatSession[]>;\n loadMessages(\n sessionId: ChatId,\n options?: { limit?: number; offset?: number },\n ): Promise<PaginatedMessages>;\n searchSessions(options: SessionSearchOptions): Promise<ChatSession[]>;\n count(): Promise<number>;\n}\n\n/**\n * Write/mutate session operations.\n * Consumers needing full access implement both ISessionReader & ISessionWriter.\n */\nexport interface ISessionWriter {\n createSession(options: CreateSessionOptions): Promise<ChatSession>;\n updateTitle(id: ChatId, title: string): Promise<void>;\n updateConfig(id: ChatId, config: Partial<ChatSessionConfig>): Promise<void>;\n deleteSession(id: ChatId): Promise<void>;\n appendMessage(sessionId: ChatId, message: ChatMessage): Promise<void>;\n saveMessages(sessionId: ChatId, messages: ChatMessage[]): Promise<void>;\n clear(): Promise<void>;\n /** Release any resources held by this store (optional). */\n dispose?(): Promise<void>;\n}\n\n/**\n * Full session store interface — union of reader and writer.\n * Backward-compatible: all existing implementations continue to work.\n *\n * @example\n * ```typescript\n * const store = new InMemorySessionStore();\n * const session = await store.createSession({ config: { model: \"gpt-4\", backend: \"vercel-ai\" } });\n * await store.appendMessage(session.id, message);\n * const page = await store.loadMessages(session.id, { limit: 20, offset: 0 });\n * ```\n */\nexport interface IChatSessionStore extends ISessionReader, ISessionWriter {}\n\n// ─── Base Session Store ────────────────────────────────────────\n\n/**\n * Base session store implementation backed by any `IStorageAdapter<ChatSession>`.\n * Handles all session-specific logic; subclasses only need to provide the adapter.\n */\nclass BaseSessionStore implements IChatSessionStore {\n constructor(protected readonly adapter: IStorageAdapter<ChatSession>) {}\n\n async createSession(options: CreateSessionOptions): Promise<ChatSession> {\n const now = new Date().toISOString();\n const id = createChatId();\n const session: ChatSession = {\n id,\n title: options.title ?? \"Untitled\",\n messages: [],\n config: {\n model: options.config?.model ?? \"\",\n backend: options.config?.backend ?? \"\",\n ...options.config,\n },\n metadata: {\n messageCount: 0,\n totalTokens: 0,\n tags: options.tags ? [...options.tags] : undefined,\n custom: options.custom ? { ...options.custom } : undefined,\n },\n status: \"active\" as const,\n createdAt: now,\n updatedAt: now,\n };\n await this.adapter.create(id, session);\n return structuredClone(session);\n }\n\n async getSession(id: ChatId): Promise<ChatSession | null> {\n return this.adapter.get(id);\n }\n\n async listSessions(options?: SessionListOptions): Promise<ChatSession[]> {\n return this.adapter.list(options as ListOptions<ChatSession>);\n }\n\n async updateTitle(id: ChatId, title: string): Promise<void> {\n const session = await this.adapter.get(id);\n if (!session) {\n throw new StorageError(`Session \"${id}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n session.title = title;\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(id, session);\n }\n\n async updateConfig(\n id: ChatId,\n config: Partial<ChatSessionConfig>,\n ): Promise<void> {\n const session = await this.adapter.get(id);\n if (!session) {\n throw new StorageError(`Session \"${id}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n session.config = { ...session.config, ...config };\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(id, session);\n }\n\n async deleteSession(id: ChatId): Promise<void> {\n await this.adapter.delete(id);\n }\n\n async appendMessage(sessionId: ChatId, message: ChatMessage): Promise<void> {\n const session = await this.adapter.get(sessionId);\n if (!session) {\n throw new StorageError(`Session \"${sessionId}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n session.messages.push(structuredClone(message));\n session.metadata.messageCount = session.messages.length;\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(sessionId, session);\n }\n\n async saveMessages(sessionId: ChatId, messages: ChatMessage[]): Promise<void> {\n if (messages.length === 0) return;\n const session = await this.adapter.get(sessionId);\n if (!session) {\n throw new StorageError(`Session \"${sessionId}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n for (const msg of messages) {\n session.messages.push(structuredClone(msg));\n }\n session.metadata.messageCount = session.messages.length;\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(sessionId, session);\n }\n\n async loadMessages(\n sessionId: ChatId,\n options?: { limit?: number; offset?: number },\n ): Promise<PaginatedMessages> {\n const session = await this.adapter.get(sessionId);\n if (!session) {\n throw new StorageError(`Session \"${sessionId}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n const total = session.messages.length;\n const offset = options?.offset ?? 0;\n const limit = options?.limit ?? total;\n const messages = session.messages.slice(offset, offset + limit);\n return {\n messages: structuredClone(messages),\n total,\n hasMore: offset + limit < total,\n };\n }\n\n async searchSessions(\n options: SessionSearchOptions,\n ): Promise<ChatSession[]> {\n const query = options.query.toLowerCase();\n const limit = options.limit ?? 20;\n return this.adapter.list({\n filter: (session) => {\n if (session.title?.toLowerCase().includes(query)) return true;\n return session.messages.some((msg) => {\n return msg.parts.some(\n (part) =>\n part.type === \"text\" &&\n part.text.toLowerCase().includes(query),\n );\n });\n },\n limit,\n });\n }\n\n async count(): Promise<number> {\n return this.adapter.count();\n }\n\n async clear(): Promise<void> {\n return this.adapter.clear();\n }\n}\n\n// ─── InMemorySessionStore ──────────────────────────────────────\n\n/**\n * In-memory session store. Data is lost when the process exits.\n * Uses `InMemoryStorage` internally.\n *\n * @example\n * ```typescript\n * const store = new InMemorySessionStore();\n * const session = await store.createSession({\n * config: { model: \"gpt-4\", backend: \"vercel-ai\" },\n * });\n * ```\n */\nexport class InMemorySessionStore extends BaseSessionStore {\n constructor() {\n super(new InMemoryStorage<ChatSession>());\n }\n}\n\n// ─── FileSessionStore ──────────────────────────────────────────\n\n/** Configuration for FileSessionStore */\nexport interface FileSessionStoreOptions {\n /** Directory to store session JSON files */\n directory: string;\n}\n\n/**\n * File-based session store. Each session is a JSON file on disk.\n * Uses `FileStorage` internally.\n *\n * @example\n * ```typescript\n * const store = new FileSessionStore({ directory: \"./data/sessions\" });\n * const session = await store.createSession({\n * config: { model: \"claude-3\", backend: \"claude\" },\n * });\n * ```\n */\nexport class FileSessionStore extends BaseSessionStore {\n constructor(options: FileSessionStoreOptions) {\n super(new FileStorage<ChatSession>({ directory: options.directory }));\n }\n}\n\n// Re-export StorageError for consumers that only import from chat/sessions\nexport { StorageError } from \"./storage.js\";\n"]}
1
+ {"version":3,"sources":["../../src/chat/types.ts","../../src/errors.ts","../../src/chat/storage.ts","../../src/chat/sessions.ts"],"names":["readdir","join","unlink","existsS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,CAAS,KAAK,GAAG,OAAO,IAAA;AACzD,QAAA,OAAO,OAAA,CAAQ,QAAA,CAAS,IAAA,CAAK,CAAC,GAAA,KAAQ;AACpC,UAAA,OAAO,IAAI,KAAA,CAAM,IAAA;AAAA,YACf,CAAC,IAAA,KACC,IAAA,CAAK,IAAA,KAAS,MAAA,IACd,KAAK,IAAA,CAAK,WAAA,EAAY,CAAE,QAAA,CAAS,KAAK;AAAA,WAC1C;AAAA,QACF,CAAC,CAAA;AAAA,MACH,CAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,EACH;AAAA,EAEA,MAAM,KAAA,GAAyB;AAC7B,IAAA,OAAO,IAAA,CAAK,QAAQ,KAAA,EAAM;AAAA,EAC5B;AAAA,EAEA,MAAM,KAAA,GAAuB;AAC3B,IAAA,OAAO,IAAA,CAAK,QAAQ,KAAA,EAAM;AAAA,EAC5B;AACF,CAAA;AAgBO,IAAM,oBAAA,GAAN,cAAmC,gBAAA,CAAiB;AAAA,EACzD,WAAA,GAAc;AACZ,IAAA,KAAA,CAAM,IAAI,iBAA8B,CAAA;AAAA,EAC1C;AACF;AAsBO,IAAM,gBAAA,GAAN,cAA+B,gBAAA,CAAiB;AAAA,EACrD,YAAY,OAAA,EAAkC;AAC5C,IAAA,KAAA,CAAM,IAAI,WAAA,CAAyB,EAAE,WAAW,OAAA,CAAQ,SAAA,EAAW,CAAC,CAAA;AAAA,EACtE;AACF","file":"sessions.cjs","sourcesContent":["/**\n * @witqq/agent-sdk — Chat domain types\n *\n * All type definitions and interfaces for the chat layer.\n * Pure types + ChatId generation (tightly coupled to branded type).\n */\n\nimport type { UsageData, ToolDefinition, ErrorCode } from \"../types.js\";\nimport type { AuthToken } from \"../auth/types.js\";\n\n// ─── Unique ID ─────────────────────────────────────────────────\n\n/** Branded type for unique identifiers */\nexport type ChatId = string & { readonly __brand: \"ChatId\" };\n\n/**\n * Generate a new unique ChatId (crypto.randomUUID-based)\n * @returns Branded ChatId string\n */\nexport function createChatId(): ChatId {\n return crypto.randomUUID() as ChatId;\n}\n\n/** UUID v4 pattern for ChatId validation */\nconst UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n\n/**\n * Cast a string to ChatId with UUID format validation.\n * Use this instead of manual `as ChatId` type assertions.\n *\n * @param value - String to validate and cast\n * @returns Branded ChatId\n * @throws {TypeError} If value is not a valid UUID v4 format\n */\nexport function toChatId(value: string): ChatId {\n if (!UUID_RE.test(value)) {\n throw new TypeError(`Invalid ChatId: \"${value}\" is not a valid UUID`);\n }\n return value as ChatId;\n}\n\n/**\n * Accepts either a plain string or branded ChatId for API convenience.\n * Use this in public API signatures so consumers don't need `as ChatId` casts.\n */\nexport type ChatIdLike = string | ChatId;\n\n// ─── Status Types ──────────────────────────────────────────────\n\n/** Lifecycle status of a message part (text, reasoning, etc.) */\nexport type PartStatus = \"pending\" | \"streaming\" | \"complete\" | \"error\";\n/** Lifecycle status of a tool call within a message */\nexport type ToolCallStatus = \"pending\" | \"running\" | \"requires_approval\" | \"complete\" | \"error\" | \"denied\";\n/** Lifecycle status of an entire message */\nexport type MessageStatus = \"pending\" | \"streaming\" | \"complete\" | \"error\" | \"cancelled\";\n/** Lifecycle status of a chat session */\nexport type SessionStatus = \"active\";\n/** Lifecycle status of the chat runtime */\nexport type RuntimeStatus = \"idle\" | \"streaming\" | \"error\" | \"disposed\";\n\n// ─── Message Parts (union) ─────────────────────────────────────\n\n/** Plain text content part */\nexport interface TextPart { type: \"text\"; text: string; status: PartStatus; }\n/** Model reasoning/thinking content part */\nexport interface ReasoningPart { type: \"reasoning\"; text: string; status: PartStatus; }\n/** Tool invocation part with call ID, arguments, optional result */\nexport interface ToolCallPart { type: \"tool_call\"; toolCallId: string; name: string; args: unknown; result?: unknown; status: ToolCallStatus; error?: string; }\n/** Source reference part (URL citation) */\nexport interface SourcePart { type: \"source\"; url: string; title?: string; status: PartStatus; }\n/** File attachment part (base64-encoded data) */\nexport interface FilePart { type: \"file\"; name: string; mimeType: string; data: string; status: PartStatus; }\n/** Union of all message part types */\nexport type MessagePart = TextPart | ReasoningPart | ToolCallPart | SourcePart | FilePart;\n\n// ─── Chat Message ──────────────────────────────────────────────\n\n/** Role of message author */\nexport type ChatRole = \"user\" | \"assistant\" | \"system\";\n\n/** Metadata attached to messages — useful preset for the TMetadata generic */\nexport interface ChatMessageMetadata {\n model?: string;\n backend?: string;\n usage?: UsageData;\n isSummary?: boolean;\n estimatedTokens?: number;\n custom?: Record<string, unknown>;\n}\n\n/** Message status */\nexport type ChatMessageStatus = MessageStatus;\n\n/** A single chat message — the fundamental unit of conversation */\nexport interface ChatMessage<TMetadata = unknown> {\n id: ChatId;\n role: ChatRole;\n parts: MessagePart[];\n metadata?: TMetadata;\n createdAt: string;\n updatedAt?: string;\n status: MessageStatus;\n}\n\n// ─── Supporting Types ──────────────────────────────────────────\n\n// ─── Chat Session ──────────────────────────────────────────────\n\n/** Session configuration snapshot */\nexport interface ChatSessionConfig {\n model: string;\n backend: string;\n systemPrompt?: string;\n temperature?: number;\n maxTokens?: number;\n}\n\n/**\n * Session metadata tracking usage statistics and custom extensions.\n *\n * Updated automatically by session stores on each `addMessage()` call.\n * The generic `TCustom` parameter allows type-safe application-specific\n * metadata via the `custom` field.\n *\n * @typeParam TCustom - Shape of the `custom` field (defaults to `Record<string, unknown>`)\n */\nexport interface ChatSessionMetadata<TCustom extends Record<string, unknown> = Record<string, unknown>> {\n /** Number of messages in the session (updated by session store) */\n messageCount: number;\n /** Total token count across all messages in the session */\n totalTokens: number;\n /** Optional tags for session categorization and filtering */\n tags?: string[];\n /** Application-specific metadata — typed via the TCustom generic parameter */\n custom?: TCustom;\n}\n\n/** Chat session — a conversation with ordered messages (pure serializable data) */\nexport interface ChatSession<TCustom extends Record<string, unknown> = Record<string, unknown>> {\n id: ChatId;\n title?: string;\n messages: ChatMessage[];\n config: ChatSessionConfig;\n metadata: ChatSessionMetadata<TCustom>;\n status: SessionStatus;\n createdAt: string;\n updatedAt: string;\n backendSessionId?: string;\n}\n\n/**\n * Reactive wrapper around ChatSession — provides subscribe/getSnapshot for\n * React useSyncExternalStore integration and lastMessage convenience getter.\n * Session stores may optionally return ObservableSession instances.\n */\nexport interface ObservableSession<TCustom extends Record<string, unknown> = Record<string, unknown>>\n extends ChatSession<TCustom> {\n /** Subscribe to session changes (for React useSyncExternalStore) */\n subscribe(callback: () => void): () => void;\n /** Get immutable snapshot of session state (for React useSyncExternalStore) */\n getSnapshot(): ChatSession<TCustom>;\n /** Last message in the session */\n readonly lastMessage: ChatMessage | undefined;\n}\n\n/** Lightweight session info for listing (without full message array) */\nexport interface SessionInfo {\n id: ChatId;\n title?: string;\n status: SessionStatus;\n messageCount: number;\n lastMessage?: ChatMessage;\n createdAt: string;\n updatedAt: string;\n}\n\n// ─── Chat Events ───────────────────────────────────────────────\n\n/** Events emitted during chat operation */\nexport type ChatEvent =\n | { type: \"message:start\"; messageId: ChatId; role: ChatRole }\n | { type: \"message:delta\"; messageId: ChatId; text: string }\n | { type: \"message:complete\"; messageId: ChatId; message: ChatMessage }\n | {\n type: \"tool:start\";\n messageId: ChatId;\n toolCallId: string;\n toolName: string;\n args: Record<string, unknown>;\n }\n | {\n type: \"tool:complete\";\n messageId: ChatId;\n toolCallId: string;\n toolName: string;\n result: unknown;\n isError?: boolean;\n }\n | { type: \"thinking:start\"; messageId: ChatId }\n | { type: \"thinking:delta\"; messageId: ChatId; text: string }\n | { type: \"thinking:end\"; messageId: ChatId }\n | {\n type: \"permission:request\";\n messageId: ChatId;\n toolName: string;\n toolArgs: Record<string, unknown>;\n }\n | {\n type: \"permission:response\";\n messageId: ChatId;\n toolName: string;\n allowed: boolean;\n }\n | {\n type: \"usage\";\n promptTokens: number;\n completionTokens: number;\n model?: string;\n }\n | { type: \"session:created\"; sessionId: ChatId }\n | { type: \"session:updated\"; sessionId: ChatId }\n | {\n type: \"error\";\n error: string;\n recoverable: boolean;\n code?: ErrorCode;\n messageId?: ChatId;\n }\n | { type: \"typing:start\" }\n | { type: \"typing:end\" }\n | { type: \"heartbeat\" }\n | { type: \"done\"; finalOutput?: string; finishReason?: string };\n\n/** All possible ChatEvent type strings */\nexport type ChatEventType = ChatEvent[\"type\"];\n\n// ─── Chat Middleware ───────────────────────────────────────────\n\n/** Context passed to ChatMiddleware hooks */\nexport interface ChatMiddlewareContext {\n sessionId: ChatId;\n signal: AbortSignal;\n}\n\n/** Runtime-level middleware for the send/receive lifecycle.\n * Different from EventMiddleware which operates at the event bus level. */\nexport interface ChatMiddleware {\n /** Transform message before sending to backend. Return null to reject the send. */\n onBeforeSend?(message: ChatMessage, context: ChatMiddlewareContext): ChatMessage | null | Promise<ChatMessage | null>;\n /** Transform/intercept stream events */\n onEvent?(event: ChatEvent, context: ChatMiddlewareContext): ChatEvent | null | Promise<ChatEvent | null>;\n /** Transform completed message after receiving from backend */\n onAfterReceive?(message: ChatMessage, context: ChatMiddlewareContext): ChatMessage | Promise<ChatMessage>;\n /** Intercept errors — return null to suppress, return error to propagate */\n onError?(error: Error, context: ChatMiddlewareContext): Error | null | Promise<Error | null>;\n}\n\n// ─── Chat Provider Abstraction ─────────────────────────────────\n\n/** Options for sending a message to a provider */\nexport interface SendMessageOptions {\n signal?: AbortSignal;\n /** Model to use for this request. Required for server-side runtime.send(). */\n model?: string;\n /** Per-call system prompt override (forwarded to the backend agent) */\n systemPrompt?: string;\n context?: Record<string, unknown>;\n /** Additional tools to include in this request */\n tools?: ToolDefinition[];\n}\n\n/** Options for runtime.send() — requires backend routing info */\nexport interface RuntimeSendOptions {\n /** Backend to route this request to (key in backends map) */\n backend: string;\n /** Authentication credentials for the backend factory */\n credentials: AuthToken;\n /** Model to use for this request */\n model: string;\n /** Per-call system prompt override (forwarded to the backend agent) */\n systemPrompt?: string;\n /** Abort signal */\n signal?: AbortSignal;\n /** Request-scoped context */\n context?: Record<string, unknown>;\n /** Additional tools */\n tools?: ToolDefinition[];\n}\n\n/**\n * @deprecated IChatProvider has been inlined into IChatBackend.\n * Import IChatBackend from \"@witqq/agent-sdk/chat/backends\" instead.\n * Kept as type alias for backward compatibility.\n */\nexport type IChatProvider = import(\"./backends/types.js\").IChatBackend;\n\n// ─── Factory Functions ─────────────────────────────────────────\n\n/**\n * Create a simple text ChatMessage.\n *\n * @param text - Message text content\n * @param role - Message role (default: \"user\")\n * @returns A complete ChatMessage with a single TextPart\n */\nexport function createTextMessage(text: string, role: ChatRole = \"user\"): ChatMessage {\n return {\n id: createChatId(),\n role,\n parts: [{ type: \"text\", text, status: \"complete\" }],\n createdAt: new Date().toISOString(),\n status: \"complete\",\n };\n}\n\n/** Type guard: checks if a session has reactive API (subscribe/getSnapshot) */\nexport function isObservableSession<TCustom extends Record<string, unknown> = Record<string, unknown>>(\n session: ChatSession<TCustom>,\n): session is ObservableSession<TCustom> {\n return \"subscribe\" in session && typeof (session as ObservableSession<TCustom>).subscribe === \"function\"\n && \"getSnapshot\" in session && typeof (session as ObservableSession<TCustom>).getSnapshot === \"function\";\n}\n","import { ErrorCode } from \"./types/errors.js\";\n\n/** Options for constructing an AgentSDKError */\nexport interface AgentSDKErrorOptions extends ErrorOptions {\n /** Machine-readable error code */\n code?: string;\n /** Whether this error is retryable (default: false) */\n retryable?: boolean;\n /** HTTP status code hint (e.g. 401, 429, 500) */\n httpStatus?: number;\n}\n\n/** Base error class for agent-sdk.\n *\n * Use `AgentSDKError.is(err)` for reliable cross-module `instanceof` checks\n * (works across separately bundled entry points where `instanceof` may fail). */\nexport class AgentSDKError extends Error {\n /** @internal Marker for cross-bundle identity checks */\n readonly _agentSDKError = true as const;\n /** Machine-readable error code. Prefer values from the ErrorCode enum. */\n readonly code?: string;\n /** Whether this error is safe to retry */\n readonly retryable: boolean;\n /** HTTP status code hint for error classification */\n readonly httpStatus?: number;\n\n constructor(message: string, options?: AgentSDKErrorOptions) {\n super(message, options);\n this.name = \"AgentSDKError\";\n this.code = options?.code;\n this.retryable = options?.retryable ?? false;\n this.httpStatus = options?.httpStatus;\n }\n\n /** Check if an error is an AgentSDKError (works across bundled copies) */\n static is(error: unknown): error is AgentSDKError {\n return (\n error instanceof Error &&\n \"_agentSDKError\" in error &&\n (error as AgentSDKError)._agentSDKError === true\n );\n }\n}\n\n/** Thrown when agent.run() is called while already running (M8 re-entrancy guard) */\nexport class ReentrancyError extends AgentSDKError {\n constructor() {\n super(\"Agent is already running. Await the current run before starting another.\", {\n code: ErrorCode.REENTRANCY,\n });\n this.name = \"ReentrancyError\";\n }\n}\n\n/** Thrown when an operation is attempted on a disposed agent/service */\nexport class DisposedError extends AgentSDKError {\n constructor(entity: string) {\n super(`${entity} has been disposed and cannot be used.`, {\n code: ErrorCode.DISPOSED,\n });\n this.name = \"DisposedError\";\n }\n}\n\n/** Thrown when a backend is not found in the registry */\nexport class BackendNotFoundError extends AgentSDKError {\n constructor(backend: string) {\n super(\n `Unknown backend: \"${backend}\". ` +\n `Built-in: copilot, claude, vercel-ai. ` +\n `Custom: use registerBackend() first.`,\n { code: ErrorCode.BACKEND_NOT_INSTALLED },\n );\n this.name = \"BackendNotFoundError\";\n }\n}\n\n/** Thrown when a backend is already registered */\nexport class BackendAlreadyRegisteredError extends AgentSDKError {\n constructor(backend: string) {\n super(`Backend \"${backend}\" is already registered. Use a different name or unregister first.`);\n this.name = \"BackendAlreadyRegisteredError\";\n }\n}\n\n/** Thrown when subprocess management fails */\nexport class SubprocessError extends AgentSDKError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, { ...options, code: ErrorCode.DEPENDENCY_MISSING });\n this.name = \"SubprocessError\";\n }\n}\n\n/** Thrown when a required peer dependency is not installed */\nexport class DependencyError extends AgentSDKError {\n public readonly packageName: string;\n\n constructor(packageName: string) {\n super(`${packageName} is not installed. Install it: npm install ${packageName}`, {\n code: ErrorCode.DEPENDENCY_MISSING,\n });\n this.name = \"DependencyError\";\n this.packageName = packageName;\n }\n}\n\n/** Thrown when an agent run is aborted */\nexport class AbortError extends AgentSDKError {\n constructor() {\n super(\"Agent run was aborted.\", { code: ErrorCode.ABORTED });\n this.name = \"AbortError\";\n }\n}\n\n/** Thrown when a tool execution fails */\nexport class ToolExecutionError extends AgentSDKError {\n public readonly toolName: string;\n\n constructor(toolName: string, message: string, options?: ErrorOptions) {\n super(`Tool \"${toolName}\" failed: ${message}`, { ...options, code: ErrorCode.TOOL_EXECUTION });\n this.name = \"ToolExecutionError\";\n this.toolName = toolName;\n }\n}\n\n/** Thrown when a stream has no activity within the configured timeout */\nexport class ActivityTimeoutError extends AgentSDKError {\n constructor(timeoutMs: number) {\n super(`Stream activity timeout: no event received within ${timeoutMs}ms.`, {\n code: ErrorCode.TIMEOUT,\n retryable: true,\n });\n this.name = \"ActivityTimeoutError\";\n }\n}\n\n/** Thrown when structured output parsing fails */\nexport class StructuredOutputError extends AgentSDKError {\n constructor(message: string, options?: ErrorOptions) {\n super(`Structured output error: ${message}`, { ...options, code: ErrorCode.INVALID_RESPONSE });\n this.name = \"StructuredOutputError\";\n }\n}\n","/**\n * @witqq/agent-sdk/chat/storage\n *\n * Generic storage adapter layer with pluggable backends.\n * Provides CRUD operations for any data type via `IStorageAdapter<T>`.\n * Implementations: `InMemoryStorage` (Map-based) and `FileStorage` (JSON files).\n */\n\nimport { existsSync, mkdirSync } from \"node:fs\";\nimport { access, mkdir, readdir, readFile, unlink, writeFile } from \"node:fs/promises\";\nimport { join } from \"node:path\";\nimport { AgentSDKError } from \"../errors.js\";\nimport { ErrorCode } from \"../types/errors.js\";\n\n// ─── Storage Errors ────────────────────────────────────────────\n\n/**\n * Error thrown by storage operations.\n *\n * @example\n * ```typescript\n * try {\n * await store.get(\"missing-id\");\n * } catch (e) {\n * if (e instanceof StorageError && e.code === ErrorCode.STORAGE_NOT_FOUND) {\n * // handle missing item\n * }\n * }\n * ```\n */\nexport class StorageError extends AgentSDKError {\n /** Machine-readable error code from the unified ErrorCode enum */\n readonly code: StorageErrorCode;\n\n constructor(message: string, code: StorageErrorCode) {\n super(message);\n this.name = \"StorageError\";\n this.code = code;\n }\n}\n\n/** Storage-specific subset of ErrorCode */\nexport type StorageErrorCode =\n | ErrorCode.STORAGE_NOT_FOUND\n | ErrorCode.STORAGE_DUPLICATE_KEY\n | ErrorCode.STORAGE_IO_ERROR\n | ErrorCode.STORAGE_SERIALIZATION_ERROR;\n\n// ─── Storage Adapter Interface ─────────────────────────────────\n\n/**\n * Options for listing stored items.\n *\n * @typeParam T - The type of stored items\n */\nexport interface ListOptions<T> {\n /** Filter predicate — return `true` to include the item */\n filter?: (item: T) => boolean;\n /** Sort comparator — standard Array.sort semantics */\n sort?: (a: T, b: T) => number;\n /** Maximum number of items to return */\n limit?: number;\n /** Number of items to skip (for pagination) */\n offset?: number;\n}\n\n/**\n * Generic storage adapter for CRUD operations on any data type.\n * Items are identified by a string key.\n *\n * @typeParam T - The type of stored items\n *\n * @example\n * ```typescript\n * const store: IStorageAdapter<{ name: string }> = new InMemoryStorage();\n * await store.create(\"key1\", { name: \"Alice\" });\n * const item = await store.get(\"key1\"); // { name: \"Alice\" }\n * ```\n */\nexport interface IStorageAdapter<T> {\n /**\n * Retrieve an item by key.\n * @param key - Unique identifier\n * @returns The item, or `null` if not found\n */\n get(key: string): Promise<T | null>;\n\n /**\n * List items with optional filtering, sorting, and pagination.\n * @param options - Filter, sort, limit, offset options\n * @returns Array of matching items\n */\n list(options?: ListOptions<T>): Promise<T[]>;\n\n /**\n * Create a new item. Throws `StorageError` with code `DUPLICATE_KEY` if key exists.\n * @param key - Unique identifier\n * @param item - Data to store\n */\n create(key: string, item: T): Promise<void>;\n\n /**\n * Update an existing item. Throws `StorageError` with code `NOT_FOUND` if key missing.\n * @param key - Unique identifier\n * @param item - Updated data\n */\n update(key: string, item: T): Promise<void>;\n\n /**\n * Delete an item by key. Throws `StorageError` with code `NOT_FOUND` if key missing.\n * @param key - Unique identifier\n */\n delete(key: string): Promise<void>;\n\n /**\n * Check whether a key exists.\n * @param key - Unique identifier\n * @returns `true` if key exists\n */\n has(key: string): Promise<boolean>;\n\n /**\n * Return the number of stored items.\n * @returns Count of items\n */\n count(): Promise<number>;\n\n /**\n * Remove all items from storage.\n */\n clear(): Promise<void>;\n\n /**\n * Release any resources held by this adapter (DB connections, file handles).\n * Optional — adapters that don't hold resources need not implement this.\n */\n dispose?(): Promise<void>;\n}\n\n// ─── InMemoryStorage ───────────────────────────────────────────\n\n/**\n * In-memory storage adapter backed by a `Map`.\n * Suitable for development, testing, and short-lived processes.\n * Data is lost when the process exits.\n *\n * @typeParam T - The type of stored items\n *\n * @example\n * ```typescript\n * const store = new InMemoryStorage<{ name: string }>();\n * await store.create(\"k1\", { name: \"Alice\" });\n * await store.create(\"k2\", { name: \"Bob\" });\n * const items = await store.list({ filter: i => i.name.startsWith(\"A\") });\n * // [{ name: \"Alice\" }]\n * ```\n */\nexport class InMemoryStorage<T> implements IStorageAdapter<T> {\n private readonly data = new Map<string, T>();\n\n /** @inheritdoc */\n async get(key: string): Promise<T | null> {\n const item = this.data.get(key);\n return item !== undefined ? structuredClone(item) : null;\n }\n\n /** @inheritdoc */\n async list(options?: ListOptions<T>): Promise<T[]> {\n let items = Array.from(this.data.values()).map((item) => structuredClone(item));\n\n if (options?.filter) {\n items = items.filter(options.filter);\n }\n if (options?.sort) {\n items.sort(options.sort);\n }\n if (options?.offset !== undefined) {\n items = items.slice(options.offset);\n }\n if (options?.limit !== undefined) {\n items = items.slice(0, options.limit);\n }\n\n return items;\n }\n\n /** @inheritdoc */\n async create(key: string, item: T): Promise<void> {\n if (this.data.has(key)) {\n throw new StorageError(\n `Item with key \"${key}\" already exists`,\n ErrorCode.STORAGE_DUPLICATE_KEY,\n );\n }\n this.data.set(key, structuredClone(item));\n }\n\n /** @inheritdoc */\n async update(key: string, item: T): Promise<void> {\n if (!this.data.has(key)) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n this.data.set(key, structuredClone(item));\n }\n\n /** @inheritdoc */\n async delete(key: string): Promise<void> {\n if (!this.data.has(key)) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n this.data.delete(key);\n }\n\n /** @inheritdoc */\n async has(key: string): Promise<boolean> {\n return this.data.has(key);\n }\n\n /** @inheritdoc */\n async count(): Promise<number> {\n return this.data.size;\n }\n\n /** @inheritdoc */\n async clear(): Promise<void> {\n this.data.clear();\n }\n}\n\n// ─── FileStorage ───────────────────────────────────────────────\n\n/**\n * Options for configuring `FileStorage`.\n */\nexport interface FileStorageOptions {\n /** Directory path where JSON files are stored */\n directory: string;\n /** File extension (default: `.json`) */\n extension?: string;\n}\n\n/**\n * File-based storage adapter that persists each item as a JSON file.\n * Suitable for local applications, CLI tools, and development.\n * Creates the storage directory if it doesn't exist.\n *\n * @typeParam T - The type of stored items (must be JSON-serializable)\n *\n * @example\n * ```typescript\n * const store = new FileStorage<ChatSession>({\n * directory: \"./data/sessions\",\n * });\n * await store.create(\"session-1\", mySession);\n * ```\n */\nexport class FileStorage<T> implements IStorageAdapter<T> {\n private readonly directory: string;\n private readonly extension: string;\n\n constructor(options: FileStorageOptions) {\n this.directory = options.directory;\n this.extension = options.extension ?? \".json\";\n this.ensureDirectorySync();\n }\n\n /** @inheritdoc */\n async get(key: string): Promise<T | null> {\n const filePath = this.keyToPath(key);\n if (!(await this.fileExists(filePath))) {\n return null;\n }\n return this.readJsonFile(filePath);\n }\n\n /** @inheritdoc */\n async list(options?: ListOptions<T>): Promise<T[]> {\n await this.ensureDirectoryAsync();\n const files = (await readdir(this.directory)).filter((f) =>\n f.endsWith(this.extension),\n );\n\n let items: T[] = [];\n for (const file of files) {\n const item = await this.readJsonFile(join(this.directory, file));\n items.push(item);\n }\n\n if (options?.filter) {\n items = items.filter(options.filter);\n }\n if (options?.sort) {\n items.sort(options.sort);\n }\n if (options?.offset !== undefined) {\n items = items.slice(options.offset);\n }\n if (options?.limit !== undefined) {\n items = items.slice(0, options.limit);\n }\n\n return items;\n }\n\n /** @inheritdoc */\n async create(key: string, item: T): Promise<void> {\n const filePath = this.keyToPath(key);\n if (await this.fileExists(filePath)) {\n throw new StorageError(\n `Item with key \"${key}\" already exists`,\n ErrorCode.STORAGE_DUPLICATE_KEY,\n );\n }\n await this.writeJsonFile(filePath, item);\n }\n\n /** @inheritdoc */\n async update(key: string, item: T): Promise<void> {\n const filePath = this.keyToPath(key);\n if (!(await this.fileExists(filePath))) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n await this.writeJsonFile(filePath, item);\n }\n\n /** @inheritdoc */\n async delete(key: string): Promise<void> {\n const filePath = this.keyToPath(key);\n if (!(await this.fileExists(filePath))) {\n throw new StorageError(\n `Item with key \"${key}\" not found`,\n ErrorCode.STORAGE_NOT_FOUND,\n );\n }\n await unlink(filePath);\n }\n\n /** @inheritdoc */\n async has(key: string): Promise<boolean> {\n return this.fileExists(this.keyToPath(key));\n }\n\n /** @inheritdoc */\n async count(): Promise<number> {\n await this.ensureDirectoryAsync();\n return (await readdir(this.directory)).filter((f) =>\n f.endsWith(this.extension),\n ).length;\n }\n\n /** @inheritdoc */\n async clear(): Promise<void> {\n await this.ensureDirectoryAsync();\n const files = (await readdir(this.directory)).filter((f) =>\n f.endsWith(this.extension),\n );\n for (const file of files) {\n await unlink(join(this.directory, file));\n }\n }\n\n private keyToPath(key: string): string {\n const safeKey = key.replace(/[^a-zA-Z0-9_-]/g, (c) =>\n \"%\" + c.charCodeAt(0).toString(16).padStart(2, \"0\"),\n );\n return join(this.directory, `${safeKey}${this.extension}`);\n }\n\n /** Sync directory init — used only in constructor (one-time). */\n private ensureDirectorySync(): void {\n if (!existsSync(this.directory)) {\n mkdirSync(this.directory, { recursive: true });\n }\n }\n\n /** Async directory init — used in operations. */\n private async ensureDirectoryAsync(): Promise<void> {\n if (!(await this.fileExists(this.directory))) {\n await mkdir(this.directory, { recursive: true });\n }\n }\n\n private async fileExists(filePath: string): Promise<boolean> {\n try {\n await access(filePath);\n return true;\n } catch {\n return false;\n }\n }\n\n private async readJsonFile(filePath: string): Promise<T> {\n try {\n const content = await readFile(filePath, \"utf-8\");\n return JSON.parse(content) as T;\n } catch (error) {\n if (error instanceof SyntaxError) {\n throw new StorageError(\n `Failed to parse file: ${filePath}`,\n ErrorCode.STORAGE_SERIALIZATION_ERROR,\n );\n }\n throw new StorageError(\n `Failed to read file: ${filePath}`,\n ErrorCode.STORAGE_IO_ERROR,\n );\n }\n }\n\n private async writeJsonFile(filePath: string, item: T): Promise<void> {\n try {\n const content = JSON.stringify(item, null, 2);\n await writeFile(filePath, content, \"utf-8\");\n } catch {\n throw new StorageError(\n `Failed to write file: ${filePath}`,\n ErrorCode.STORAGE_IO_ERROR,\n );\n }\n }\n}\n","/**\n * @witqq/agent-sdk/chat/sessions\n *\n * Session store layer wrapping generic storage adapters.\n * Provides session-specific operations: message management,\n * paginated retrieval, search, and session lifecycle.\n */\n\nimport type {\n ChatSession,\n ChatMessage,\n ChatId,\n ChatSessionConfig,\n} from \"./core.js\";\nimport { createChatId } from \"./core.js\";\nimport type { IStorageAdapter, ListOptions } from \"./storage.js\";\nimport { InMemoryStorage, FileStorage, StorageError } from \"./storage.js\";\nimport { ErrorCode } from \"../types/errors.js\";\n\n// ─── Session Store Interface ───────────────────────────────────\n\n/** Options for creating a new session */\nexport interface CreateSessionOptions<TCustom extends Record<string, unknown> = Record<string, unknown>> {\n /** Session title (defaults to \"Untitled\") */\n title?: string;\n /** Session configuration (optional — runtime defaults used when omitted) */\n config?: Partial<ChatSessionConfig>;\n /** Initial tags */\n tags?: string[];\n /** Custom metadata */\n custom?: TCustom;\n}\n\n/** Paginated result of messages */\nexport interface PaginatedMessages {\n /** Messages in this page */\n messages: ChatMessage[];\n /** Total number of messages in session */\n total: number;\n /** Whether there are more messages after this page */\n hasMore: boolean;\n}\n\n/** Options for listing sessions */\nexport interface SessionListOptions {\n /** Filter predicate */\n filter?: (session: ChatSession) => boolean;\n /** Sort comparator */\n sort?: (a: ChatSession, b: ChatSession) => number;\n /** Maximum number of sessions to return */\n limit?: number;\n /** Number of sessions to skip */\n offset?: number;\n}\n\n/** Search options for finding sessions */\nexport interface SessionSearchOptions {\n /** Text query to match against title and message content */\n query: string;\n /** Maximum results (default: 20) */\n limit?: number;\n}\n\n/**\n * Read-only session operations.\n * Consumers needing read-only access (dashboards, analytics) implement only this.\n */\nexport interface ISessionReader {\n getSession(id: ChatId): Promise<ChatSession | null>;\n listSessions(options?: SessionListOptions): Promise<ChatSession[]>;\n loadMessages(\n sessionId: ChatId,\n options?: { limit?: number; offset?: number },\n ): Promise<PaginatedMessages>;\n searchSessions(options: SessionSearchOptions): Promise<ChatSession[]>;\n count(): Promise<number>;\n}\n\n/**\n * Write/mutate session operations.\n * Consumers needing full access implement both ISessionReader & ISessionWriter.\n */\nexport interface ISessionWriter {\n createSession(options: CreateSessionOptions): Promise<ChatSession>;\n updateTitle(id: ChatId, title: string): Promise<void>;\n updateConfig(id: ChatId, config: Partial<ChatSessionConfig>): Promise<void>;\n deleteSession(id: ChatId): Promise<void>;\n appendMessage(sessionId: ChatId, message: ChatMessage): Promise<void>;\n saveMessages(sessionId: ChatId, messages: ChatMessage[]): Promise<void>;\n clear(): Promise<void>;\n /** Release any resources held by this store (optional). */\n dispose?(): Promise<void>;\n}\n\n/**\n * Full session store interface — union of reader and writer.\n * Backward-compatible: all existing implementations continue to work.\n *\n * @example\n * ```typescript\n * const store = new InMemorySessionStore();\n * const session = await store.createSession({ config: { model: \"gpt-4\", backend: \"vercel-ai\" } });\n * await store.appendMessage(session.id, message);\n * const page = await store.loadMessages(session.id, { limit: 20, offset: 0 });\n * ```\n */\nexport interface IChatSessionStore extends ISessionReader, ISessionWriter {}\n\n// ─── Base Session Store ────────────────────────────────────────\n\n/**\n * Base session store implementation backed by any `IStorageAdapter<ChatSession>`.\n * Handles all session-specific logic; subclasses only need to provide the adapter.\n */\nclass BaseSessionStore implements IChatSessionStore {\n constructor(protected readonly adapter: IStorageAdapter<ChatSession>) {}\n\n async createSession(options: CreateSessionOptions): Promise<ChatSession> {\n const now = new Date().toISOString();\n const id = createChatId();\n const session: ChatSession = {\n id,\n title: options.title ?? \"Untitled\",\n messages: [],\n config: {\n model: options.config?.model ?? \"\",\n backend: options.config?.backend ?? \"\",\n ...options.config,\n },\n metadata: {\n messageCount: 0,\n totalTokens: 0,\n tags: options.tags ? [...options.tags] : undefined,\n custom: options.custom ? { ...options.custom } : undefined,\n },\n status: \"active\" as const,\n createdAt: now,\n updatedAt: now,\n };\n await this.adapter.create(id, session);\n return structuredClone(session);\n }\n\n async getSession(id: ChatId): Promise<ChatSession | null> {\n return this.adapter.get(id);\n }\n\n async listSessions(options?: SessionListOptions): Promise<ChatSession[]> {\n return this.adapter.list(options as ListOptions<ChatSession>);\n }\n\n async updateTitle(id: ChatId, title: string): Promise<void> {\n const session = await this.adapter.get(id);\n if (!session) {\n throw new StorageError(`Session \"${id}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n session.title = title;\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(id, session);\n }\n\n async updateConfig(\n id: ChatId,\n config: Partial<ChatSessionConfig>,\n ): Promise<void> {\n const session = await this.adapter.get(id);\n if (!session) {\n throw new StorageError(`Session \"${id}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n session.config = { ...session.config, ...config };\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(id, session);\n }\n\n async deleteSession(id: ChatId): Promise<void> {\n await this.adapter.delete(id);\n }\n\n async appendMessage(sessionId: ChatId, message: ChatMessage): Promise<void> {\n const session = await this.adapter.get(sessionId);\n if (!session) {\n throw new StorageError(`Session \"${sessionId}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n session.messages.push(structuredClone(message));\n session.metadata.messageCount = session.messages.length;\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(sessionId, session);\n }\n\n async saveMessages(sessionId: ChatId, messages: ChatMessage[]): Promise<void> {\n if (messages.length === 0) return;\n const session = await this.adapter.get(sessionId);\n if (!session) {\n throw new StorageError(`Session \"${sessionId}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n for (const msg of messages) {\n session.messages.push(structuredClone(msg));\n }\n session.metadata.messageCount = session.messages.length;\n session.updatedAt = new Date().toISOString();\n await this.adapter.update(sessionId, session);\n }\n\n async loadMessages(\n sessionId: ChatId,\n options?: { limit?: number; offset?: number },\n ): Promise<PaginatedMessages> {\n const session = await this.adapter.get(sessionId);\n if (!session) {\n throw new StorageError(`Session \"${sessionId}\" not found`, ErrorCode.STORAGE_NOT_FOUND);\n }\n const total = session.messages.length;\n const offset = options?.offset ?? 0;\n const limit = options?.limit ?? total;\n const messages = session.messages.slice(offset, offset + limit);\n return {\n messages: structuredClone(messages),\n total,\n hasMore: offset + limit < total,\n };\n }\n\n async searchSessions(\n options: SessionSearchOptions,\n ): Promise<ChatSession[]> {\n const query = options.query.toLowerCase();\n const limit = options.limit ?? 20;\n return this.adapter.list({\n filter: (session) => {\n if (session.title?.toLowerCase().includes(query)) return true;\n return session.messages.some((msg) => {\n return msg.parts.some(\n (part) =>\n part.type === \"text\" &&\n part.text.toLowerCase().includes(query),\n );\n });\n },\n limit,\n });\n }\n\n async count(): Promise<number> {\n return this.adapter.count();\n }\n\n async clear(): Promise<void> {\n return this.adapter.clear();\n }\n}\n\n// ─── InMemorySessionStore ──────────────────────────────────────\n\n/**\n * In-memory session store. Data is lost when the process exits.\n * Uses `InMemoryStorage` internally.\n *\n * @example\n * ```typescript\n * const store = new InMemorySessionStore();\n * const session = await store.createSession({\n * config: { model: \"gpt-4\", backend: \"vercel-ai\" },\n * });\n * ```\n */\nexport class InMemorySessionStore extends BaseSessionStore {\n constructor() {\n super(new InMemoryStorage<ChatSession>());\n }\n}\n\n// ─── FileSessionStore ──────────────────────────────────────────\n\n/** Configuration for FileSessionStore */\nexport interface FileSessionStoreOptions {\n /** Directory to store session JSON files */\n directory: string;\n}\n\n/**\n * File-based session store. Each session is a JSON file on disk.\n * Uses `FileStorage` internally.\n *\n * @example\n * ```typescript\n * const store = new FileSessionStore({ directory: \"./data/sessions\" });\n * const session = await store.createSession({\n * config: { model: \"claude-3\", backend: \"claude\" },\n * });\n * ```\n */\nexport class FileSessionStore extends BaseSessionStore {\n constructor(options: FileSessionStoreOptions) {\n super(new FileStorage<ChatSession>({ directory: options.directory }));\n }\n}\n\n// Re-export StorageError for consumers that only import from chat/sessions\nexport { StorageError } from \"./storage.js\";\n"]}
@@ -1,7 +1,7 @@
1
- import { b as ChatId, g as ChatSession, c as ChatMessage, j as ChatSessionConfig } from '../types-DRgd_9R7.cjs';
1
+ import { b as ChatId, g as ChatSession, c as ChatMessage, j as ChatSessionConfig } from '../types-DkSXALKg.cjs';
2
2
  import { IStorageAdapter } from './storage.cjs';
3
3
  export { StorageError } from './storage.cjs';
4
- import '../agent-DxY68NZL.cjs';
4
+ import '../agent-C6H2CgJA.cjs';
5
5
  import 'zod';
6
6
  import '../errors-C-so0M4t.cjs';
7
7
  import '../types-4vbcmPTp.cjs';
@@ -1,7 +1,7 @@
1
- import { b as ChatId, g as ChatSession, c as ChatMessage, j as ChatSessionConfig } from '../types-ajANVzf7.js';
1
+ import { b as ChatId, g as ChatSession, c as ChatMessage, j as ChatSessionConfig } from '../types-DgtI1hzh.js';
2
2
  import { IStorageAdapter } from './storage.js';
3
3
  export { StorageError } from './storage.js';
4
- import '../agent-CW9XbmG_.js';
4
+ import '../agent-F7oB6eKp.js';
5
5
  import 'zod';
6
6
  import '../errors-C-so0M4t.js';
7
7
  import '../types-BxggH0Yh.js';
@@ -1,4 +1,5 @@
1
- import { existsSync, readdirSync, unlinkSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
1
+ import { existsSync, mkdirSync } from 'fs';
2
+ import { readdir, unlink, mkdir, access, readFile, writeFile } from 'fs/promises';
2
3
  import { join } from 'path';
3
4
 
4
5
  // src/chat/types.ts
@@ -112,25 +113,25 @@ var FileStorage = class {
112
113
  constructor(options) {
113
114
  this.directory = options.directory;
114
115
  this.extension = options.extension ?? ".json";
115
- this.ensureDirectory();
116
+ this.ensureDirectorySync();
116
117
  }
117
118
  /** @inheritdoc */
118
119
  async get(key) {
119
120
  const filePath = this.keyToPath(key);
120
- if (!existsSync(filePath)) {
121
+ if (!await this.fileExists(filePath)) {
121
122
  return null;
122
123
  }
123
- return this.readFile(filePath);
124
+ return this.readJsonFile(filePath);
124
125
  }
125
126
  /** @inheritdoc */
126
127
  async list(options) {
127
- this.ensureDirectory();
128
- const files = readdirSync(this.directory).filter(
128
+ await this.ensureDirectoryAsync();
129
+ const files = (await readdir(this.directory)).filter(
129
130
  (f) => f.endsWith(this.extension)
130
131
  );
131
132
  let items = [];
132
133
  for (const file of files) {
133
- const item = this.readFile(join(this.directory, file));
134
+ const item = await this.readJsonFile(join(this.directory, file));
134
135
  items.push(item);
135
136
  }
136
137
  if (options?.filter) {
@@ -150,55 +151,55 @@ var FileStorage = class {
150
151
  /** @inheritdoc */
151
152
  async create(key, item) {
152
153
  const filePath = this.keyToPath(key);
153
- if (existsSync(filePath)) {
154
+ if (await this.fileExists(filePath)) {
154
155
  throw new StorageError(
155
156
  `Item with key "${key}" already exists`,
156
157
  "STORAGE_DUPLICATE_KEY" /* STORAGE_DUPLICATE_KEY */
157
158
  );
158
159
  }
159
- this.writeFile(filePath, item);
160
+ await this.writeJsonFile(filePath, item);
160
161
  }
161
162
  /** @inheritdoc */
162
163
  async update(key, item) {
163
164
  const filePath = this.keyToPath(key);
164
- if (!existsSync(filePath)) {
165
+ if (!await this.fileExists(filePath)) {
165
166
  throw new StorageError(
166
167
  `Item with key "${key}" not found`,
167
168
  "STORAGE_NOT_FOUND" /* STORAGE_NOT_FOUND */
168
169
  );
169
170
  }
170
- this.writeFile(filePath, item);
171
+ await this.writeJsonFile(filePath, item);
171
172
  }
172
173
  /** @inheritdoc */
173
174
  async delete(key) {
174
175
  const filePath = this.keyToPath(key);
175
- if (!existsSync(filePath)) {
176
+ if (!await this.fileExists(filePath)) {
176
177
  throw new StorageError(
177
178
  `Item with key "${key}" not found`,
178
179
  "STORAGE_NOT_FOUND" /* STORAGE_NOT_FOUND */
179
180
  );
180
181
  }
181
- unlinkSync(filePath);
182
+ await unlink(filePath);
182
183
  }
183
184
  /** @inheritdoc */
184
185
  async has(key) {
185
- return existsSync(this.keyToPath(key));
186
+ return this.fileExists(this.keyToPath(key));
186
187
  }
187
188
  /** @inheritdoc */
188
189
  async count() {
189
- this.ensureDirectory();
190
- return readdirSync(this.directory).filter(
190
+ await this.ensureDirectoryAsync();
191
+ return (await readdir(this.directory)).filter(
191
192
  (f) => f.endsWith(this.extension)
192
193
  ).length;
193
194
  }
194
195
  /** @inheritdoc */
195
196
  async clear() {
196
- this.ensureDirectory();
197
- const files = readdirSync(this.directory).filter(
197
+ await this.ensureDirectoryAsync();
198
+ const files = (await readdir(this.directory)).filter(
198
199
  (f) => f.endsWith(this.extension)
199
200
  );
200
201
  for (const file of files) {
201
- unlinkSync(join(this.directory, file));
202
+ await unlink(join(this.directory, file));
202
203
  }
203
204
  }
204
205
  keyToPath(key) {
@@ -208,14 +209,29 @@ var FileStorage = class {
208
209
  );
209
210
  return join(this.directory, `${safeKey}${this.extension}`);
210
211
  }
211
- ensureDirectory() {
212
+ /** Sync directory init — used only in constructor (one-time). */
213
+ ensureDirectorySync() {
212
214
  if (!existsSync(this.directory)) {
213
215
  mkdirSync(this.directory, { recursive: true });
214
216
  }
215
217
  }
216
- readFile(filePath) {
218
+ /** Async directory init — used in operations. */
219
+ async ensureDirectoryAsync() {
220
+ if (!await this.fileExists(this.directory)) {
221
+ await mkdir(this.directory, { recursive: true });
222
+ }
223
+ }
224
+ async fileExists(filePath) {
225
+ try {
226
+ await access(filePath);
227
+ return true;
228
+ } catch {
229
+ return false;
230
+ }
231
+ }
232
+ async readJsonFile(filePath) {
217
233
  try {
218
- const content = readFileSync(filePath, "utf-8");
234
+ const content = await readFile(filePath, "utf-8");
219
235
  return JSON.parse(content);
220
236
  } catch (error) {
221
237
  if (error instanceof SyntaxError) {
@@ -230,10 +246,10 @@ var FileStorage = class {
230
246
  );
231
247
  }
232
248
  }
233
- writeFile(filePath, item) {
249
+ async writeJsonFile(filePath, item) {
234
250
  try {
235
251
  const content = JSON.stringify(item, null, 2);
236
- writeFileSync(filePath, content, "utf-8");
252
+ await writeFile(filePath, content, "utf-8");
237
253
  } catch {
238
254
  throw new StorageError(
239
255
  `Failed to write file: ${filePath}`,