@contractspec/module.ai-chat 1.57.0 → 1.59.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 (120) hide show
  1. package/dist/ai-chat.capability.d.ts +2 -0
  2. package/dist/ai-chat.capability.d.ts.map +1 -0
  3. package/dist/ai-chat.feature.d.ts +1 -6
  4. package/dist/ai-chat.feature.d.ts.map +1 -1
  5. package/dist/ai-chat.operations.d.ts +217 -223
  6. package/dist/ai-chat.operations.d.ts.map +1 -1
  7. package/dist/browser/context/index.js +415 -0
  8. package/dist/browser/core/index.js +336 -0
  9. package/dist/browser/index.js +2291 -0
  10. package/dist/browser/presentation/components/index.js +974 -0
  11. package/dist/browser/presentation/hooks/index.js +556 -0
  12. package/dist/browser/presentation/index.js +1520 -0
  13. package/dist/browser/providers/index.js +51 -0
  14. package/dist/context/chat.test.d.ts +2 -0
  15. package/dist/context/chat.test.d.ts.map +1 -0
  16. package/dist/context/context-builder.d.ts +37 -36
  17. package/dist/context/context-builder.d.ts.map +1 -1
  18. package/dist/context/file-operations.d.ts +64 -67
  19. package/dist/context/file-operations.d.ts.map +1 -1
  20. package/dist/context/index.d.ts +7 -4
  21. package/dist/context/index.d.ts.map +1 -0
  22. package/dist/context/index.js +409 -4
  23. package/dist/context/workspace-context.d.ts +84 -87
  24. package/dist/context/workspace-context.d.ts.map +1 -1
  25. package/dist/core/chat-service.d.ts +56 -60
  26. package/dist/core/chat-service.d.ts.map +1 -1
  27. package/dist/core/conversation-store.d.ts +60 -61
  28. package/dist/core/conversation-store.d.ts.map +1 -1
  29. package/dist/core/index.d.ts +7 -4
  30. package/dist/core/index.d.ts.map +1 -0
  31. package/dist/core/index.js +330 -3
  32. package/dist/core/message-types.d.ts +94 -97
  33. package/dist/core/message-types.d.ts.map +1 -1
  34. package/dist/docs/ai-chat.docblock.d.ts +2 -0
  35. package/dist/docs/ai-chat.docblock.d.ts.map +1 -0
  36. package/dist/docs/index.d.ts +7 -0
  37. package/dist/docs/index.d.ts.map +1 -0
  38. package/dist/events.d.ts +103 -109
  39. package/dist/events.d.ts.map +1 -1
  40. package/dist/index.d.ts +16 -21
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +2286 -23
  43. package/dist/node/context/index.js +410 -0
  44. package/dist/node/core/index.js +331 -0
  45. package/dist/node/index.js +2286 -0
  46. package/dist/node/presentation/components/index.js +969 -0
  47. package/dist/node/presentation/hooks/index.js +551 -0
  48. package/dist/node/presentation/index.js +1515 -0
  49. package/dist/node/providers/index.js +46 -0
  50. package/dist/presentation/components/ChatContainer.d.ts +7 -16
  51. package/dist/presentation/components/ChatContainer.d.ts.map +1 -1
  52. package/dist/presentation/components/ChatInput.d.ts +17 -30
  53. package/dist/presentation/components/ChatInput.d.ts.map +1 -1
  54. package/dist/presentation/components/ChatMessage.d.ts +9 -19
  55. package/dist/presentation/components/ChatMessage.d.ts.map +1 -1
  56. package/dist/presentation/components/CodePreview.d.ts +20 -35
  57. package/dist/presentation/components/CodePreview.d.ts.map +1 -1
  58. package/dist/presentation/components/ContextIndicator.d.ts +11 -21
  59. package/dist/presentation/components/ContextIndicator.d.ts.map +1 -1
  60. package/dist/presentation/components/ModelPicker.d.ts +21 -32
  61. package/dist/presentation/components/ModelPicker.d.ts.map +1 -1
  62. package/dist/presentation/components/index.d.ts +10 -7
  63. package/dist/presentation/components/index.d.ts.map +1 -0
  64. package/dist/presentation/components/index.js +968 -7
  65. package/dist/presentation/hooks/index.d.ts +6 -3
  66. package/dist/presentation/hooks/index.d.ts.map +1 -0
  67. package/dist/presentation/hooks/index.js +550 -3
  68. package/dist/presentation/hooks/use-chat.test.d.ts +2 -0
  69. package/dist/presentation/hooks/use-chat.test.d.ts.map +1 -0
  70. package/dist/presentation/hooks/useChat.d.ts +50 -54
  71. package/dist/presentation/hooks/useChat.d.ts.map +1 -1
  72. package/dist/presentation/hooks/useProviders.d.ts +21 -25
  73. package/dist/presentation/hooks/useProviders.d.ts.map +1 -1
  74. package/dist/presentation/index.d.ts +8 -11
  75. package/dist/presentation/index.d.ts.map +1 -0
  76. package/dist/presentation/index.js +1515 -12
  77. package/dist/providers/chat-utilities.d.ts +18 -7
  78. package/dist/providers/chat-utilities.d.ts.map +1 -1
  79. package/dist/providers/index.d.ts +8 -3
  80. package/dist/providers/index.d.ts.map +1 -0
  81. package/dist/providers/index.js +45 -3
  82. package/dist/schema.d.ts +195 -200
  83. package/dist/schema.d.ts.map +1 -1
  84. package/package.json +123 -34
  85. package/dist/ai-chat.feature.js +0 -102
  86. package/dist/ai-chat.feature.js.map +0 -1
  87. package/dist/ai-chat.operations.js +0 -172
  88. package/dist/ai-chat.operations.js.map +0 -1
  89. package/dist/context/context-builder.js +0 -148
  90. package/dist/context/context-builder.js.map +0 -1
  91. package/dist/context/file-operations.js +0 -175
  92. package/dist/context/file-operations.js.map +0 -1
  93. package/dist/context/workspace-context.js +0 -124
  94. package/dist/context/workspace-context.js.map +0 -1
  95. package/dist/core/chat-service.js +0 -227
  96. package/dist/core/chat-service.js.map +0 -1
  97. package/dist/core/conversation-store.js +0 -109
  98. package/dist/core/conversation-store.js.map +0 -1
  99. package/dist/events.js +0 -98
  100. package/dist/events.js.map +0 -1
  101. package/dist/presentation/components/ChatContainer.js +0 -63
  102. package/dist/presentation/components/ChatContainer.js.map +0 -1
  103. package/dist/presentation/components/ChatInput.js +0 -149
  104. package/dist/presentation/components/ChatInput.js.map +0 -1
  105. package/dist/presentation/components/ChatMessage.js +0 -136
  106. package/dist/presentation/components/ChatMessage.js.map +0 -1
  107. package/dist/presentation/components/CodePreview.js +0 -127
  108. package/dist/presentation/components/CodePreview.js.map +0 -1
  109. package/dist/presentation/components/ContextIndicator.js +0 -97
  110. package/dist/presentation/components/ContextIndicator.js.map +0 -1
  111. package/dist/presentation/components/ModelPicker.js +0 -202
  112. package/dist/presentation/components/ModelPicker.js.map +0 -1
  113. package/dist/presentation/hooks/useChat.js +0 -172
  114. package/dist/presentation/hooks/useChat.js.map +0 -1
  115. package/dist/presentation/hooks/useProviders.js +0 -41
  116. package/dist/presentation/hooks/useProviders.js.map +0 -1
  117. package/dist/providers/chat-utilities.js +0 -17
  118. package/dist/providers/chat-utilities.js.map +0 -1
  119. package/dist/schema.js +0 -100
  120. package/dist/schema.js.map +0 -1
@@ -0,0 +1,415 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // src/context/workspace-context.ts
10
+ class WorkspaceContext {
11
+ workspacePath;
12
+ allowWrites;
13
+ specs = [];
14
+ files = [];
15
+ initialized = false;
16
+ constructor(config) {
17
+ this.workspacePath = config.workspacePath;
18
+ this.allowWrites = config.allowWrites ?? false;
19
+ }
20
+ async initialize() {
21
+ if (this.initialized)
22
+ return;
23
+ this.initialized = true;
24
+ }
25
+ getSpecs() {
26
+ return this.specs;
27
+ }
28
+ getFiles() {
29
+ return this.files;
30
+ }
31
+ addSpecs(specs) {
32
+ this.specs.push(...specs);
33
+ }
34
+ addFiles(files) {
35
+ this.files.push(...files);
36
+ }
37
+ getSummary() {
38
+ const commands = this.specs.filter((s) => s.type === "command").length;
39
+ const queries = this.specs.filter((s) => s.type === "query").length;
40
+ const events = this.specs.filter((s) => s.type === "event").length;
41
+ const presentations = this.specs.filter((s) => s.type === "presentation").length;
42
+ const tsFiles = this.files.filter((f) => f.extension === ".ts").length;
43
+ const specFiles = this.files.filter((f) => f.isSpec).length;
44
+ return {
45
+ name: this.workspacePath.split("/").pop() ?? "workspace",
46
+ path: this.workspacePath,
47
+ specs: {
48
+ total: this.specs.length,
49
+ commands,
50
+ queries,
51
+ events,
52
+ presentations
53
+ },
54
+ files: {
55
+ total: this.files.length,
56
+ typescript: tsFiles,
57
+ specFiles
58
+ }
59
+ };
60
+ }
61
+ getContextSummary() {
62
+ const summary = this.getSummary();
63
+ const parts = [
64
+ `Workspace: ${summary.name}`,
65
+ `Path: ${summary.path}`,
66
+ "",
67
+ "### Specs",
68
+ `- Commands: ${summary.specs.commands}`,
69
+ `- Queries: ${summary.specs.queries}`,
70
+ `- Events: ${summary.specs.events}`,
71
+ `- Presentations: ${summary.specs.presentations}`
72
+ ];
73
+ if (this.specs.length > 0) {
74
+ parts.push("", "### Available Specs");
75
+ for (const spec of this.specs.slice(0, 20)) {
76
+ parts.push(`- ${spec.name} (${spec.type})`);
77
+ }
78
+ if (this.specs.length > 20) {
79
+ parts.push(`- ... and ${this.specs.length - 20} more`);
80
+ }
81
+ }
82
+ return parts.join(`
83
+ `);
84
+ }
85
+ findSpecs(query) {
86
+ const lowerQuery = query.toLowerCase();
87
+ return this.specs.filter((s) => s.name.toLowerCase().includes(lowerQuery) || s.description?.toLowerCase().includes(lowerQuery) || s.tags?.some((t) => t.toLowerCase().includes(lowerQuery)));
88
+ }
89
+ findFiles(query) {
90
+ const lowerQuery = query.toLowerCase();
91
+ return this.files.filter((f) => f.path.toLowerCase().includes(lowerQuery) || f.name.toLowerCase().includes(lowerQuery));
92
+ }
93
+ }
94
+ async function createWorkspaceContext(path, options) {
95
+ const context = new WorkspaceContext({
96
+ workspacePath: path,
97
+ ...options
98
+ });
99
+ await context.initialize();
100
+ return context;
101
+ }
102
+ // src/context/context-builder.ts
103
+ function estimateTokens(text) {
104
+ return Math.ceil(text.length / 4);
105
+ }
106
+ function scoreSpec(spec, query) {
107
+ if (!query)
108
+ return 0.5;
109
+ const lowerQuery = query.toLowerCase();
110
+ let score = 0;
111
+ if (spec.name.toLowerCase().includes(lowerQuery)) {
112
+ score += 0.4;
113
+ }
114
+ if (spec.description?.toLowerCase().includes(lowerQuery)) {
115
+ score += 0.3;
116
+ }
117
+ if (spec.tags?.some((t) => t.toLowerCase().includes(lowerQuery))) {
118
+ score += 0.2;
119
+ }
120
+ return Math.min(score, 1);
121
+ }
122
+ function scoreFile(file, query) {
123
+ if (!query)
124
+ return 0.5;
125
+ const lowerQuery = query.toLowerCase();
126
+ let score = 0;
127
+ if (file.path.toLowerCase().includes(lowerQuery)) {
128
+ score += 0.5;
129
+ }
130
+ if (file.name.toLowerCase().includes(lowerQuery)) {
131
+ score += 0.3;
132
+ }
133
+ if (file.isSpec) {
134
+ score += 0.2;
135
+ }
136
+ return Math.min(score, 1);
137
+ }
138
+
139
+ class ContextBuilder {
140
+ context;
141
+ constructor(context) {
142
+ this.context = context;
143
+ }
144
+ build(options = {}) {
145
+ const maxTokens = options.maxTokens ?? 4000;
146
+ const entries = [];
147
+ let totalTokens = 0;
148
+ if (options.includeSpecs?.length) {
149
+ for (const specName of options.includeSpecs) {
150
+ const spec = this.context.getSpecs().find((s) => s.name === specName);
151
+ if (spec) {
152
+ const entry = {
153
+ type: "spec",
154
+ path: spec.path,
155
+ summary: `${spec.type}: ${spec.name}${spec.description ? ` - ${spec.description}` : ""}`,
156
+ relevance: 1
157
+ };
158
+ entries.push(entry);
159
+ totalTokens += estimateTokens(entry.summary ?? "");
160
+ }
161
+ }
162
+ }
163
+ if (options.includeFiles?.length) {
164
+ for (const filePath of options.includeFiles) {
165
+ const file = this.context.getFiles().find((f) => f.path === filePath);
166
+ if (file) {
167
+ const entry = {
168
+ type: "file",
169
+ path: file.path,
170
+ summary: `File: ${file.relativePath}`,
171
+ relevance: 1
172
+ };
173
+ entries.push(entry);
174
+ totalTokens += estimateTokens(entry.summary ?? "");
175
+ }
176
+ }
177
+ }
178
+ if (options.query) {
179
+ const scoredSpecs = this.context.getSpecs().map((spec) => ({ spec, score: scoreSpec(spec, options.query) })).filter(({ score }) => score > 0.2).sort((a, b) => b.score - a.score);
180
+ for (const { spec, score } of scoredSpecs) {
181
+ if (totalTokens >= maxTokens)
182
+ break;
183
+ if (entries.some((e) => e.path === spec.path))
184
+ continue;
185
+ const entry = {
186
+ type: "spec",
187
+ path: spec.path,
188
+ summary: `${spec.type}: ${spec.name}${spec.description ? ` - ${spec.description}` : ""}`,
189
+ relevance: score
190
+ };
191
+ entries.push(entry);
192
+ totalTokens += estimateTokens(entry.summary ?? "");
193
+ }
194
+ }
195
+ if (options.query) {
196
+ const scoredFiles = this.context.getFiles().map((file) => ({ file, score: scoreFile(file, options.query) })).filter(({ score }) => score > 0.2).sort((a, b) => b.score - a.score);
197
+ for (const { file, score } of scoredFiles) {
198
+ if (totalTokens >= maxTokens)
199
+ break;
200
+ if (entries.some((e) => e.path === file.path))
201
+ continue;
202
+ const entry = {
203
+ type: "file",
204
+ path: file.path,
205
+ summary: `File: ${file.relativePath}`,
206
+ relevance: score
207
+ };
208
+ entries.push(entry);
209
+ totalTokens += estimateTokens(entry.summary ?? "");
210
+ }
211
+ }
212
+ const summary = this.buildSummary(entries);
213
+ return {
214
+ entries,
215
+ summary,
216
+ totalTokensEstimate: totalTokens + estimateTokens(summary)
217
+ };
218
+ }
219
+ buildSummary(entries) {
220
+ if (entries.length === 0) {
221
+ return this.context.getContextSummary();
222
+ }
223
+ const parts = [];
224
+ const workspaceSummary = this.context.getSummary();
225
+ parts.push(`Workspace: ${workspaceSummary.name}`);
226
+ parts.push("");
227
+ const specs = entries.filter((e) => e.type === "spec");
228
+ if (specs.length > 0) {
229
+ parts.push("### Relevant Specs");
230
+ for (const entry of specs) {
231
+ parts.push(`- ${entry.summary}`);
232
+ }
233
+ parts.push("");
234
+ }
235
+ const files = entries.filter((e) => e.type === "file");
236
+ if (files.length > 0) {
237
+ parts.push("### Relevant Files");
238
+ for (const entry of files) {
239
+ parts.push(`- ${entry.summary}`);
240
+ }
241
+ }
242
+ return parts.join(`
243
+ `);
244
+ }
245
+ }
246
+ function createContextBuilder(context) {
247
+ return new ContextBuilder(context);
248
+ }
249
+ // src/context/file-operations.ts
250
+ class FileOperations {
251
+ fs;
252
+ workspacePath;
253
+ allowWrites;
254
+ constructor(fs, workspacePath, allowWrites = false) {
255
+ this.fs = fs;
256
+ this.workspacePath = workspacePath;
257
+ this.allowWrites = allowWrites;
258
+ }
259
+ async read(relativePath) {
260
+ const fullPath = this.resolvePath(relativePath);
261
+ try {
262
+ const content = await this.fs.readFile(fullPath);
263
+ return { success: true, path: relativePath, content };
264
+ } catch (error) {
265
+ return {
266
+ success: false,
267
+ path: relativePath,
268
+ error: error instanceof Error ? error.message : String(error)
269
+ };
270
+ }
271
+ }
272
+ async write(relativePath, content) {
273
+ if (!this.allowWrites) {
274
+ return {
275
+ success: false,
276
+ path: relativePath,
277
+ error: "File writes are not enabled"
278
+ };
279
+ }
280
+ const fullPath = this.resolvePath(relativePath);
281
+ try {
282
+ await this.fs.writeFile(fullPath, content);
283
+ return { success: true, path: relativePath };
284
+ } catch (error) {
285
+ return {
286
+ success: false,
287
+ path: relativePath,
288
+ error: error instanceof Error ? error.message : String(error)
289
+ };
290
+ }
291
+ }
292
+ async execute(operations) {
293
+ const results = [];
294
+ for (const operation of operations) {
295
+ let result;
296
+ switch (operation.type) {
297
+ case "read": {
298
+ const readResult = await this.read(operation.path);
299
+ result = {
300
+ operation,
301
+ success: readResult.success,
302
+ content: readResult.content,
303
+ error: readResult.error
304
+ };
305
+ break;
306
+ }
307
+ case "write":
308
+ case "create": {
309
+ if (!operation.content) {
310
+ result = {
311
+ operation,
312
+ success: false,
313
+ error: "Content is required for write operations"
314
+ };
315
+ } else {
316
+ const writeResult = await this.write(operation.path, operation.content);
317
+ result = {
318
+ operation,
319
+ success: writeResult.success,
320
+ error: writeResult.error
321
+ };
322
+ }
323
+ break;
324
+ }
325
+ case "delete": {
326
+ if (!this.allowWrites) {
327
+ result = {
328
+ operation,
329
+ success: false,
330
+ error: "File writes are not enabled"
331
+ };
332
+ } else {
333
+ try {
334
+ await this.fs.deleteFile(this.resolvePath(operation.path));
335
+ result = { operation, success: true };
336
+ } catch (error) {
337
+ result = {
338
+ operation,
339
+ success: false,
340
+ error: error instanceof Error ? error.message : String(error)
341
+ };
342
+ }
343
+ }
344
+ break;
345
+ }
346
+ default:
347
+ result = {
348
+ operation,
349
+ success: false,
350
+ error: `Unknown operation type: ${operation.type}`
351
+ };
352
+ }
353
+ results.push(result);
354
+ }
355
+ return results;
356
+ }
357
+ resolvePath(relativePath) {
358
+ const normalized = relativePath.replace(/\.\./g, "").replace(/^\//, "");
359
+ return `${this.workspacePath}/${normalized}`;
360
+ }
361
+ }
362
+ function createNodeFileOperations(workspacePath, allowWrites = false) {
363
+ const fs = {
364
+ async readFile(path) {
365
+ const { readFile } = await import("node:fs/promises");
366
+ return readFile(path, "utf-8");
367
+ },
368
+ async writeFile(path, content) {
369
+ const { writeFile, mkdir } = await import("node:fs/promises");
370
+ const { dirname } = await import("node:path");
371
+ await mkdir(dirname(path), { recursive: true });
372
+ await writeFile(path, content, "utf-8");
373
+ },
374
+ async exists(path) {
375
+ const { access } = await import("node:fs/promises");
376
+ try {
377
+ await access(path);
378
+ return true;
379
+ } catch {
380
+ return false;
381
+ }
382
+ },
383
+ async deleteFile(path) {
384
+ const { unlink } = await import("node:fs/promises");
385
+ await unlink(path);
386
+ },
387
+ async listFiles(directory, options) {
388
+ const { readdir } = await import("node:fs/promises");
389
+ const { join } = await import("node:path");
390
+ const files = [];
391
+ const entries = await readdir(directory, { withFileTypes: true });
392
+ for (const entry of entries) {
393
+ const fullPath = join(directory, entry.name);
394
+ if (entry.isDirectory() && options?.recursive) {
395
+ const subFiles = await this.listFiles(fullPath, options);
396
+ files.push(...subFiles);
397
+ } else if (entry.isFile()) {
398
+ if (!options?.pattern || entry.name.match(new RegExp(options.pattern))) {
399
+ files.push(fullPath);
400
+ }
401
+ }
402
+ }
403
+ return files;
404
+ }
405
+ };
406
+ return new FileOperations(fs, workspacePath, allowWrites);
407
+ }
408
+ export {
409
+ createWorkspaceContext,
410
+ createNodeFileOperations,
411
+ createContextBuilder,
412
+ WorkspaceContext,
413
+ FileOperations,
414
+ ContextBuilder
415
+ };