@contractspec/module.ai-chat 1.46.0 → 1.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-chat.feature.d.ts +2 -2
- package/dist/ai-chat.feature.d.ts.map +1 -1
- package/dist/ai-chat.feature.js +9 -2
- package/dist/ai-chat.feature.js.map +1 -1
- package/dist/ai-chat.operations.d.ts +8 -8
- package/dist/ai-chat.operations.js +1 -1
- package/dist/context/context-builder.js.map +1 -1
- package/dist/context/file-operations.js.map +1 -1
- package/dist/context/workspace-context.js.map +1 -1
- package/dist/core/chat-service.js.map +1 -1
- package/dist/core/conversation-store.js.map +1 -1
- package/dist/events.d.ts +1 -1
- package/dist/events.js +1 -1
- package/dist/presentation/components/ChatInput.js.map +1 -1
- package/dist/presentation/components/ChatMessage.js.map +1 -1
- package/dist/presentation/components/CodePreview.js.map +1 -1
- package/dist/presentation/components/ModelPicker.js.map +1 -1
- package/dist/presentation/hooks/useChat.js.map +1 -1
- package/package.json +16 -18
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _contractspec_lib_contracts4 from "@contractspec/lib.contracts";
|
|
2
2
|
|
|
3
3
|
//#region src/ai-chat.feature.d.ts
|
|
4
4
|
|
|
@@ -6,7 +6,7 @@ import { FeatureModuleSpec } from "@contractspec/lib.contracts";
|
|
|
6
6
|
* AI Chat feature module that bundles conversational AI assistance
|
|
7
7
|
* for ContractSpec development across CLI, VSCode, and Studio.
|
|
8
8
|
*/
|
|
9
|
-
declare const AiChatFeature: FeatureModuleSpec;
|
|
9
|
+
declare const AiChatFeature: _contractspec_lib_contracts4.FeatureModuleSpec;
|
|
10
10
|
//#endregion
|
|
11
11
|
export { AiChatFeature };
|
|
12
12
|
//# sourceMappingURL=ai-chat.feature.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-chat.feature.d.ts","names":[],"sources":["../src/ai-chat.feature.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ai-chat.feature.d.ts","names":[],"sources":["../src/ai-chat.feature.ts"],"sourcesContent":[],"mappings":";;;;;;;AAWA;cAAa,eA8CX,4BAAA,CA9CwB"}
|
package/dist/ai-chat.feature.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
import { defineFeature } from "@contractspec/lib.contracts";
|
|
2
|
+
|
|
1
3
|
//#region src/ai-chat.feature.ts
|
|
2
4
|
/**
|
|
5
|
+
* AI Chat Feature Module Specification
|
|
6
|
+
*
|
|
7
|
+
* Defines the feature module for AI-powered vibe coding chat.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
3
10
|
* AI Chat feature module that bundles conversational AI assistance
|
|
4
11
|
* for ContractSpec development across CLI, VSCode, and Studio.
|
|
5
12
|
*/
|
|
6
|
-
const AiChatFeature = {
|
|
13
|
+
const AiChatFeature = defineFeature({
|
|
7
14
|
meta: {
|
|
8
15
|
key: "ai-chat",
|
|
9
16
|
version: "1.0.0",
|
|
@@ -88,7 +95,7 @@ const AiChatFeature = {
|
|
|
88
95
|
version: "1.0.0"
|
|
89
96
|
}]
|
|
90
97
|
}
|
|
91
|
-
};
|
|
98
|
+
});
|
|
92
99
|
|
|
93
100
|
//#endregion
|
|
94
101
|
export { AiChatFeature };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-chat.feature.js","names":[
|
|
1
|
+
{"version":3,"file":"ai-chat.feature.js","names":[],"sources":["../src/ai-chat.feature.ts"],"sourcesContent":["/**\n * AI Chat Feature Module Specification\n *\n * Defines the feature module for AI-powered vibe coding chat.\n */\nimport { defineFeature } from '@contractspec/lib.contracts';\n\n/**\n * AI Chat feature module that bundles conversational AI assistance\n * for ContractSpec development across CLI, VSCode, and Studio.\n */\nexport const AiChatFeature = defineFeature({\n meta: {\n key: 'ai-chat',\n version: '1.0.0',\n title: 'AI Vibe Coding Chat',\n description:\n 'AI-powered conversational coding assistant with full workspace context',\n domain: 'platform',\n owners: ['@platform.ai'],\n tags: ['ai', 'chat', 'llm', 'vibe-coding', 'assistant'],\n stability: 'experimental',\n },\n\n // Contract operations for chat functionality\n operations: [\n { key: 'ai-chat.send', version: '1.0.0' },\n { key: 'ai-chat.stream', version: '1.0.0' },\n { key: 'ai-chat.conversations.list', version: '1.0.0' },\n { key: 'ai-chat.conversations.get', version: '1.0.0' },\n { key: 'ai-chat.conversations.delete', version: '1.0.0' },\n { key: 'ai-chat.providers.list', version: '1.0.0' },\n { key: 'ai-chat.context.scan', version: '1.0.0' },\n ],\n\n // Events emitted by the chat system\n events: [\n { key: 'ai-chat.message.sent', version: '1.0.0' },\n { key: 'ai-chat.message.received', version: '1.0.0' },\n { key: 'ai-chat.conversation.created', version: '1.0.0' },\n { key: 'ai-chat.conversation.deleted', version: '1.0.0' },\n { key: 'ai-chat.error', version: '1.0.0' },\n ],\n\n // No presentations for core module\n presentations: [],\n opToPresentation: [],\n presentationsTargets: [],\n\n // Capability definitions\n capabilities: {\n provides: [{ key: 'ai-chat', version: '1.0.0' }],\n requires: [\n { key: 'identity', version: '1.0.0' },\n { key: 'metering', version: '1.0.0' },\n ],\n },\n});\n"],"mappings":";;;;;;;;;;;;AAWA,MAAa,gBAAgB,cAAc;CACzC,MAAM;EACJ,KAAK;EACL,SAAS;EACT,OAAO;EACP,aACE;EACF,QAAQ;EACR,QAAQ,CAAC,eAAe;EACxB,MAAM;GAAC;GAAM;GAAQ;GAAO;GAAe;GAAY;EACvD,WAAW;EACZ;CAGD,YAAY;EACV;GAAE,KAAK;GAAgB,SAAS;GAAS;EACzC;GAAE,KAAK;GAAkB,SAAS;GAAS;EAC3C;GAAE,KAAK;GAA8B,SAAS;GAAS;EACvD;GAAE,KAAK;GAA6B,SAAS;GAAS;EACtD;GAAE,KAAK;GAAgC,SAAS;GAAS;EACzD;GAAE,KAAK;GAA0B,SAAS;GAAS;EACnD;GAAE,KAAK;GAAwB,SAAS;GAAS;EAClD;CAGD,QAAQ;EACN;GAAE,KAAK;GAAwB,SAAS;GAAS;EACjD;GAAE,KAAK;GAA4B,SAAS;GAAS;EACrD;GAAE,KAAK;GAAgC,SAAS;GAAS;EACzD;GAAE,KAAK;GAAgC,SAAS;GAAS;EACzD;GAAE,KAAK;GAAiB,SAAS;GAAS;EAC3C;CAGD,eAAe,EAAE;CACjB,kBAAkB,EAAE;CACpB,sBAAsB,EAAE;CAGxB,cAAc;EACZ,UAAU,CAAC;GAAE,KAAK;GAAW,SAAS;GAAS,CAAC;EAChD,UAAU,CACR;GAAE,KAAK;GAAY,SAAS;GAAS,EACrC;GAAE,KAAK;GAAY,SAAS;GAAS,CACtC;EACF;CACF,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import * as _contractspec_lib_contracts5 from "@contractspec/lib.contracts";
|
|
1
2
|
import * as _contractspec_lib_schema82 from "@contractspec/lib.schema";
|
|
2
|
-
import * as _contractspec_lib_contracts4 from "@contractspec/lib.contracts";
|
|
3
3
|
|
|
4
4
|
//#region src/ai-chat.operations.d.ts
|
|
5
|
-
declare const SendMessageContract:
|
|
5
|
+
declare const SendMessageContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{
|
|
6
6
|
conversationId: {
|
|
7
7
|
type: _contractspec_lib_schema82.FieldType<string, string>;
|
|
8
8
|
isOptional: true;
|
|
@@ -93,7 +93,7 @@ declare const SendMessageContract: _contractspec_lib_contracts4.OperationSpec<_c
|
|
|
93
93
|
isOptional: false;
|
|
94
94
|
};
|
|
95
95
|
}>, undefined>;
|
|
96
|
-
declare const StreamMessageContract:
|
|
96
|
+
declare const StreamMessageContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{
|
|
97
97
|
conversationId: {
|
|
98
98
|
type: _contractspec_lib_schema82.FieldType<string, string>;
|
|
99
99
|
isOptional: true;
|
|
@@ -112,7 +112,7 @@ declare const StreamMessageContract: _contractspec_lib_contracts4.OperationSpec<
|
|
|
112
112
|
isOptional: false;
|
|
113
113
|
};
|
|
114
114
|
}>, undefined>;
|
|
115
|
-
declare const ListConversationsContract:
|
|
115
|
+
declare const ListConversationsContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{}>, _contractspec_lib_schema82.SchemaModel<{
|
|
116
116
|
conversations: {
|
|
117
117
|
type: _contractspec_lib_schema82.SchemaModel<{
|
|
118
118
|
id: {
|
|
@@ -166,7 +166,7 @@ declare const ListConversationsContract: _contractspec_lib_contracts4.OperationS
|
|
|
166
166
|
isOptional: false;
|
|
167
167
|
};
|
|
168
168
|
}>, undefined>;
|
|
169
|
-
declare const GetConversationContract:
|
|
169
|
+
declare const GetConversationContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{
|
|
170
170
|
id: {
|
|
171
171
|
type: _contractspec_lib_schema82.FieldType<string, string>;
|
|
172
172
|
isOptional: false;
|
|
@@ -219,20 +219,20 @@ declare const GetConversationContract: _contractspec_lib_contracts4.OperationSpe
|
|
|
219
219
|
isOptional: false;
|
|
220
220
|
};
|
|
221
221
|
}>, undefined>;
|
|
222
|
-
declare const DeleteConversationContract:
|
|
222
|
+
declare const DeleteConversationContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{
|
|
223
223
|
id: {
|
|
224
224
|
type: _contractspec_lib_schema82.FieldType<string, string>;
|
|
225
225
|
isOptional: false;
|
|
226
226
|
};
|
|
227
227
|
}>, _contractspec_lib_schema82.SchemaModel<{}>, undefined>;
|
|
228
|
-
declare const ListProvidersContract:
|
|
228
|
+
declare const ListProvidersContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{}>, _contractspec_lib_schema82.SchemaModel<{
|
|
229
229
|
providers: {
|
|
230
230
|
type: _contractspec_lib_schema82.FieldType<string, string>;
|
|
231
231
|
isArray: true;
|
|
232
232
|
isOptional: false;
|
|
233
233
|
};
|
|
234
234
|
}>, undefined>;
|
|
235
|
-
declare const ScanContextContract:
|
|
235
|
+
declare const ScanContextContract: _contractspec_lib_contracts5.OperationSpec<_contractspec_lib_schema82.SchemaModel<{
|
|
236
236
|
path: {
|
|
237
237
|
type: _contractspec_lib_schema82.FieldType<string, string>;
|
|
238
238
|
isOptional: false;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ChatConversationModel, ListConversationsOutputModel, SendMessageInputModel, SendMessageOutputModel } from "./schema.js";
|
|
2
|
-
import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
|
|
3
2
|
import { defineCommand, defineQuery } from "@contractspec/lib.contracts";
|
|
3
|
+
import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
|
|
4
4
|
|
|
5
5
|
//#region src/ai-chat.operations.ts
|
|
6
6
|
const SendMessageContract = defineCommand({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-builder.js","names":[
|
|
1
|
+
{"version":3,"file":"context-builder.js","names":[],"sources":["../../src/context/context-builder.ts"],"sourcesContent":["/**\n * Context builder for LLM prompts\n *\n * Builds rich context from workspace information for AI assistance.\n */\nimport type { WorkspaceContext, SpecInfo, FileInfo } from './workspace-context';\n\n/**\n * Context entry for a file or spec\n */\nexport interface ContextEntry {\n type: 'spec' | 'file' | 'reference';\n path: string;\n content?: string;\n summary?: string;\n relevance: number; // 0-1 score\n}\n\n/**\n * Built context for LLM\n */\nexport interface BuiltContext {\n entries: ContextEntry[];\n summary: string;\n totalTokensEstimate: number;\n}\n\n/**\n * Options for building context\n */\nexport interface ContextBuilderOptions {\n /** Maximum estimated tokens for context */\n maxTokens?: number;\n /** Query to use for relevance scoring */\n query?: string;\n /** Specific files to include */\n includeFiles?: string[];\n /** Specific specs to include */\n includeSpecs?: string[];\n}\n\n/**\n * Estimates token count from string (rough approximation)\n */\nfunction estimateTokens(text: string): number {\n // Rough estimate: ~4 characters per token\n return Math.ceil(text.length / 4);\n}\n\n/**\n * Calculate relevance score for a spec\n */\nfunction scoreSpec(spec: SpecInfo, query?: string): number {\n if (!query) return 0.5;\n\n const lowerQuery = query.toLowerCase();\n let score = 0;\n\n // Name match\n if (spec.name.toLowerCase().includes(lowerQuery)) {\n score += 0.4;\n }\n\n // Description match\n if (spec.description?.toLowerCase().includes(lowerQuery)) {\n score += 0.3;\n }\n\n // Tag match\n if (spec.tags?.some((t) => t.toLowerCase().includes(lowerQuery))) {\n score += 0.2;\n }\n\n return Math.min(score, 1);\n}\n\n/**\n * Calculate relevance score for a file\n */\nfunction scoreFile(file: FileInfo, query?: string): number {\n if (!query) return 0.5;\n\n const lowerQuery = query.toLowerCase();\n let score = 0;\n\n // Path match\n if (file.path.toLowerCase().includes(lowerQuery)) {\n score += 0.5;\n }\n\n // Name match\n if (file.name.toLowerCase().includes(lowerQuery)) {\n score += 0.3;\n }\n\n // Spec file bonus\n if (file.isSpec) {\n score += 0.2;\n }\n\n return Math.min(score, 1);\n}\n\n/**\n * Context builder for creating rich LLM context\n */\nexport class ContextBuilder {\n private readonly context: WorkspaceContext;\n\n constructor(context: WorkspaceContext) {\n this.context = context;\n }\n\n /**\n * Build context for a chat message\n */\n build(options: ContextBuilderOptions = {}): BuiltContext {\n const maxTokens = options.maxTokens ?? 4000;\n const entries: ContextEntry[] = [];\n let totalTokens = 0;\n\n // Add explicitly included specs\n if (options.includeSpecs?.length) {\n for (const specName of options.includeSpecs) {\n const spec = this.context.getSpecs().find((s) => s.name === specName);\n if (spec) {\n const entry: ContextEntry = {\n type: 'spec',\n path: spec.path,\n summary: `${spec.type}: ${spec.name}${spec.description ? ` - ${spec.description}` : ''}`,\n relevance: 1,\n };\n entries.push(entry);\n totalTokens += estimateTokens(entry.summary ?? '');\n }\n }\n }\n\n // Add explicitly included files\n if (options.includeFiles?.length) {\n for (const filePath of options.includeFiles) {\n const file = this.context.getFiles().find((f) => f.path === filePath);\n if (file) {\n const entry: ContextEntry = {\n type: 'file',\n path: file.path,\n summary: `File: ${file.relativePath}`,\n relevance: 1,\n };\n entries.push(entry);\n totalTokens += estimateTokens(entry.summary ?? '');\n }\n }\n }\n\n // Add relevant specs based on query\n if (options.query) {\n const scoredSpecs = this.context\n .getSpecs()\n .map((spec) => ({ spec, score: scoreSpec(spec, options.query) }))\n .filter(({ score }) => score > 0.2)\n .sort((a, b) => b.score - a.score);\n\n for (const { spec, score } of scoredSpecs) {\n if (totalTokens >= maxTokens) break;\n if (entries.some((e) => e.path === spec.path)) continue;\n\n const entry: ContextEntry = {\n type: 'spec',\n path: spec.path,\n summary: `${spec.type}: ${spec.name}${spec.description ? ` - ${spec.description}` : ''}`,\n relevance: score,\n };\n entries.push(entry);\n totalTokens += estimateTokens(entry.summary ?? '');\n }\n }\n\n // Add relevant files based on query\n if (options.query) {\n const scoredFiles = this.context\n .getFiles()\n .map((file) => ({ file, score: scoreFile(file, options.query) }))\n .filter(({ score }) => score > 0.2)\n .sort((a, b) => b.score - a.score);\n\n for (const { file, score } of scoredFiles) {\n if (totalTokens >= maxTokens) break;\n if (entries.some((e) => e.path === file.path)) continue;\n\n const entry: ContextEntry = {\n type: 'file',\n path: file.path,\n summary: `File: ${file.relativePath}`,\n relevance: score,\n };\n entries.push(entry);\n totalTokens += estimateTokens(entry.summary ?? '');\n }\n }\n\n // Build summary\n const summary = this.buildSummary(entries);\n\n return {\n entries,\n summary,\n totalTokensEstimate: totalTokens + estimateTokens(summary),\n };\n }\n\n /**\n * Build a text summary of the context entries\n */\n private buildSummary(entries: ContextEntry[]): string {\n if (entries.length === 0) {\n return this.context.getContextSummary();\n }\n\n const parts: string[] = [];\n\n // Workspace info\n const workspaceSummary = this.context.getSummary();\n parts.push(`Workspace: ${workspaceSummary.name}`);\n parts.push('');\n\n // Relevant specs\n const specs = entries.filter((e) => e.type === 'spec');\n if (specs.length > 0) {\n parts.push('### Relevant Specs');\n for (const entry of specs) {\n parts.push(`- ${entry.summary}`);\n }\n parts.push('');\n }\n\n // Relevant files\n const files = entries.filter((e) => e.type === 'file');\n if (files.length > 0) {\n parts.push('### Relevant Files');\n for (const entry of files) {\n parts.push(`- ${entry.summary}`);\n }\n }\n\n return parts.join('\\n');\n }\n}\n\n/**\n * Create a context builder\n */\nexport function createContextBuilder(\n context: WorkspaceContext\n): ContextBuilder {\n return new ContextBuilder(context);\n}\n"],"mappings":";;;;AA4CA,SAAS,eAAe,MAAsB;AAE5C,QAAO,KAAK,KAAK,KAAK,SAAS,EAAE;;;;;AAMnC,SAAS,UAAU,MAAgB,OAAwB;AACzD,KAAI,CAAC,MAAO,QAAO;CAEnB,MAAM,aAAa,MAAM,aAAa;CACtC,IAAI,QAAQ;AAGZ,KAAI,KAAK,KAAK,aAAa,CAAC,SAAS,WAAW,CAC9C,UAAS;AAIX,KAAI,KAAK,aAAa,aAAa,CAAC,SAAS,WAAW,CACtD,UAAS;AAIX,KAAI,KAAK,MAAM,MAAM,MAAM,EAAE,aAAa,CAAC,SAAS,WAAW,CAAC,CAC9D,UAAS;AAGX,QAAO,KAAK,IAAI,OAAO,EAAE;;;;;AAM3B,SAAS,UAAU,MAAgB,OAAwB;AACzD,KAAI,CAAC,MAAO,QAAO;CAEnB,MAAM,aAAa,MAAM,aAAa;CACtC,IAAI,QAAQ;AAGZ,KAAI,KAAK,KAAK,aAAa,CAAC,SAAS,WAAW,CAC9C,UAAS;AAIX,KAAI,KAAK,KAAK,aAAa,CAAC,SAAS,WAAW,CAC9C,UAAS;AAIX,KAAI,KAAK,OACP,UAAS;AAGX,QAAO,KAAK,IAAI,OAAO,EAAE;;;;;AAM3B,IAAa,iBAAb,MAA4B;CAC1B,AAAiB;CAEjB,YAAY,SAA2B;AACrC,OAAK,UAAU;;;;;CAMjB,MAAM,UAAiC,EAAE,EAAgB;EACvD,MAAM,YAAY,QAAQ,aAAa;EACvC,MAAM,UAA0B,EAAE;EAClC,IAAI,cAAc;AAGlB,MAAI,QAAQ,cAAc,OACxB,MAAK,MAAM,YAAY,QAAQ,cAAc;GAC3C,MAAM,OAAO,KAAK,QAAQ,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
|
|
|
1
|
-
{"version":3,"file":"file-operations.js","names":[
|
|
1
|
+
{"version":3,"file":"file-operations.js","names":[],"sources":["../../src/context/file-operations.ts"],"sourcesContent":["/**\n * File operations for workspace context\n *\n * Provides read/write operations for files in the workspace.\n */\n\n/**\n * Result of a file read operation\n */\nexport interface FileReadResult {\n success: boolean;\n path: string;\n content?: string;\n error?: string;\n}\n\n/**\n * Result of a file write operation\n */\nexport interface FileWriteResult {\n success: boolean;\n path: string;\n error?: string;\n}\n\n/**\n * File operation to perform\n */\nexport interface FileOperation {\n type: 'read' | 'write' | 'create' | 'delete';\n path: string;\n content?: string;\n}\n\n/**\n * Result of a file operation\n */\nexport interface FileOperationResult {\n operation: FileOperation;\n success: boolean;\n content?: string;\n error?: string;\n}\n\n/**\n * Interface for file system operations\n */\nexport interface FileSystem {\n /**\n * Read a file's contents\n */\n readFile(path: string): Promise<string>;\n\n /**\n * Write content to a file\n */\n writeFile(path: string, content: string): Promise<void>;\n\n /**\n * Check if a file exists\n */\n exists(path: string): Promise<boolean>;\n\n /**\n * Delete a file\n */\n deleteFile(path: string): Promise<void>;\n\n /**\n * List files in a directory\n */\n listFiles(\n directory: string,\n options?: { recursive?: boolean; pattern?: string }\n ): Promise<string[]>;\n}\n\n/**\n * File operations executor\n */\nexport class FileOperations {\n constructor(\n private readonly fs: FileSystem,\n private readonly workspacePath: string,\n private readonly allowWrites = false\n ) {}\n\n /**\n * Read a file\n */\n async read(relativePath: string): Promise<FileReadResult> {\n const fullPath = this.resolvePath(relativePath);\n\n try {\n const content = await this.fs.readFile(fullPath);\n return { success: true, path: relativePath, content };\n } catch (error) {\n return {\n success: false,\n path: relativePath,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n }\n\n /**\n * Write to a file\n */\n async write(relativePath: string, content: string): Promise<FileWriteResult> {\n if (!this.allowWrites) {\n return {\n success: false,\n path: relativePath,\n error: 'File writes are not enabled',\n };\n }\n\n const fullPath = this.resolvePath(relativePath);\n\n try {\n await this.fs.writeFile(fullPath, content);\n return { success: true, path: relativePath };\n } catch (error) {\n return {\n success: false,\n path: relativePath,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n }\n\n /**\n * Execute multiple file operations\n */\n async execute(operations: FileOperation[]): Promise<FileOperationResult[]> {\n const results: FileOperationResult[] = [];\n\n for (const operation of operations) {\n let result: FileOperationResult;\n\n switch (operation.type) {\n case 'read': {\n const readResult = await this.read(operation.path);\n result = {\n operation,\n success: readResult.success,\n content: readResult.content,\n error: readResult.error,\n };\n break;\n }\n\n case 'write':\n case 'create': {\n if (!operation.content) {\n result = {\n operation,\n success: false,\n error: 'Content is required for write operations',\n };\n } else {\n const writeResult = await this.write(\n operation.path,\n operation.content\n );\n result = {\n operation,\n success: writeResult.success,\n error: writeResult.error,\n };\n }\n break;\n }\n\n case 'delete': {\n if (!this.allowWrites) {\n result = {\n operation,\n success: false,\n error: 'File writes are not enabled',\n };\n } else {\n try {\n await this.fs.deleteFile(this.resolvePath(operation.path));\n result = { operation, success: true };\n } catch (error) {\n result = {\n operation,\n success: false,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n }\n break;\n }\n\n default:\n result = {\n operation,\n success: false,\n error: `Unknown operation type: ${(operation as FileOperation).type}`,\n };\n }\n\n results.push(result);\n }\n\n return results;\n }\n\n /**\n * Resolve a relative path to an absolute path\n */\n private resolvePath(relativePath: string): string {\n // Prevent path traversal\n const normalized = relativePath.replace(/\\.\\./g, '').replace(/^\\//, '');\n return `${this.workspacePath}/${normalized}`;\n }\n}\n\n/**\n * Create a file operations instance with Node.js fs\n */\nexport function createNodeFileOperations(\n workspacePath: string,\n allowWrites = false\n): FileOperations {\n // Dynamic import to avoid issues in browser environments\n const fs: FileSystem = {\n async readFile(path: string): Promise<string> {\n const { readFile } = await import('node:fs/promises');\n return readFile(path, 'utf-8');\n },\n async writeFile(path: string, content: string): Promise<void> {\n const { writeFile, mkdir } = await import('node:fs/promises');\n const { dirname } = await import('node:path');\n await mkdir(dirname(path), { recursive: true });\n await writeFile(path, content, 'utf-8');\n },\n async exists(path: string): Promise<boolean> {\n const { access } = await import('node:fs/promises');\n try {\n await access(path);\n return true;\n } catch {\n return false;\n }\n },\n async deleteFile(path: string): Promise<void> {\n const { unlink } = await import('node:fs/promises');\n await unlink(path);\n },\n async listFiles(\n directory: string,\n options?: { recursive?: boolean; pattern?: string }\n ): Promise<string[]> {\n const { readdir } = await import('node:fs/promises');\n const { join } = await import('node:path');\n\n const files: string[] = [];\n const entries = await readdir(directory, { withFileTypes: true });\n\n for (const entry of entries) {\n const fullPath = join(directory, entry.name);\n if (entry.isDirectory() && options?.recursive) {\n const subFiles = await this.listFiles(fullPath, options);\n files.push(...subFiles);\n } else if (entry.isFile()) {\n if (\n !options?.pattern ||\n entry.name.match(new RegExp(options.pattern))\n ) {\n files.push(fullPath);\n }\n }\n }\n\n return files;\n },\n };\n\n return new FileOperations(fs, workspacePath, allowWrites);\n}\n"],"mappings":";;;;AAgFA,IAAa,iBAAb,MAA4B;CAC1B,YACE,AAAiB,IACjB,AAAiB,eACjB,AAAiB,cAAc,OAC/B;EAHiB;EACA;EACA;;;;;CAMnB,MAAM,KAAK,cAA+C;EACxD,MAAM,WAAW,KAAK,YAAY,aAAa;AAE/C,MAAI;AAEF,UAAO;IAAE,SAAS;IAAM,MAAM;IAAc,SAD5B,MAAM,KAAK,GAAG,SAAS,SAAS;IACK;WAC9C,OAAO;AACd,UAAO;IACL,SAAS;IACT,MAAM;IACN,OAAO,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;IAC9D;;;;;;CAOL,MAAM,MAAM,cAAsB,SAA2C;AAC3E,MAAI,CAAC,KAAK,YACR,QAAO;GACL,SAAS;GACT,MAAM;GACN,OAAO;GACR;EAGH,MAAM,WAAW,KAAK,YAAY,aAAa;AAE/C,MAAI;AACF,SAAM,KAAK,GAAG,UAAU,UAAU,QAAQ;AAC1C,UAAO;IAAE,SAAS;IAAM,MAAM;IAAc;WACrC,OAAO;AACd,UAAO;IACL,S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UAAO,SAAS,MAAM,QAAQ;;EAEhC,MAAM,UAAU,MAAc,SAAgC;GAC5D,MAAM,EAAE,WAAW,UAAU,MAAM,OAAO;GAC1C,MAAM,EAAE,YAAY,MAAM,OAAO;AACjC,SAAM,MAAM,QAAQ,KAAK,EAAE,EAAE,WAAW,MAAM,CAAC;AAC/C,SAAM,UAAU,MAAM,SAAS,QAAQ;;EAEzC,MAAM,OAAO,MAAgC;GAC3C,MAAM,EAAE,WAAW,MAAM,OAAO;AAChC,OAAI;AACF,UAAM,OAAO,KAAK;AAClB,WAAO;WACD;AACN,WAAO;;;EAGX,MAAM,WAAW,MAA6B;GAC5C,MAAM,EAAE,WAAW,MAAM,OAAO;AAChC,SAAM,OAAO,KAAK;;EAEpB,MAAM,UACJ,WACA,SACmB;GACnB,MAAM,EAAE,YAAY,MAAM,OAAO;GACjC,MAAM,EAAE,SAAS,MAAM,OAAO;GAE9B,MAAM,QAAkB,EAAE;GAC1B,MAAM,UAAU,MAAM,QAAQ,WAAW,EAAE,eAAe,MAAM,CAAC;AAEjE,QAAK,MAAM,SAAS,SAAS;IAC3B,MAAM,WAAW,KAAK,WAAW,MAAM,KAAK;AAC5C,QAAI,MAAM,aAAa,IAAI,SAAS,WAAW;KAC7C,MAAM,WAAW,MAAM,KAAK,UAAU,UAAU,QAAQ;AACxD,WAAM,KAAK,GAAG,SAAS;eACd,MAAM,QAAQ,EACvB;SACE,CAAC,SAAS,WACV,MAAM,KAAK,MAAM,IAAI,OAAO,QAAQ,QAAQ,CAAC,CAE7C,OAAM,KAAK,SAAS;;;AAK1B,UAAO;;EAEV,EAE6B,eAAe,YAAY"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workspace-context.js","names":[
|
|
1
|
+
{"version":3,"file":"workspace-context.js","names":[],"sources":["../../src/context/workspace-context.ts"],"sourcesContent":["/**\n * Workspace context management\n *\n * Provides access to specs, files, and codebase information\n * for context-aware AI chat assistance.\n */\n\n/**\n * Spec information for context\n */\nexport interface SpecInfo {\n name: string;\n version: string;\n type: 'command' | 'query' | 'event' | 'presentation';\n path: string;\n description?: string;\n tags?: string[];\n}\n\n/**\n * File information for context\n */\nexport interface FileInfo {\n path: string;\n relativePath: string;\n name: string;\n extension: string;\n size: number;\n isSpec: boolean;\n}\n\n/**\n * Workspace summary for context\n */\nexport interface WorkspaceSummary {\n name: string;\n path: string;\n specs: {\n total: number;\n commands: number;\n queries: number;\n events: number;\n presentations: number;\n };\n files: {\n total: number;\n typescript: number;\n specFiles: number;\n };\n}\n\n/**\n * Configuration for workspace context\n */\nexport interface WorkspaceContextConfig {\n /** Root path of the workspace */\n workspacePath: string;\n /** File patterns to include */\n includePatterns?: string[];\n /** File patterns to exclude */\n excludePatterns?: string[];\n /** Maximum file size to read (bytes) */\n maxFileSize?: number;\n /** Whether to enable file writes */\n allowWrites?: boolean;\n}\n\n/**\n * Workspace context for AI chat\n */\nexport class WorkspaceContext {\n readonly workspacePath: string;\n readonly allowWrites: boolean;\n\n private specs: SpecInfo[] = [];\n private files: FileInfo[] = [];\n private initialized = false;\n\n constructor(config: WorkspaceContextConfig) {\n this.workspacePath = config.workspacePath;\n this.allowWrites = config.allowWrites ?? false;\n }\n\n /**\n * Initialize the workspace context by scanning files\n */\n async initialize(): Promise<void> {\n if (this.initialized) return;\n\n // This would scan the workspace for specs and files\n // For now, we just mark as initialized\n this.initialized = true;\n }\n\n /**\n * Get all discovered specs\n */\n getSpecs(): SpecInfo[] {\n return this.specs;\n }\n\n /**\n * Get all discovered files\n */\n getFiles(): FileInfo[] {\n return this.files;\n }\n\n /**\n * Add specs to the context\n */\n addSpecs(specs: SpecInfo[]): void {\n this.specs.push(...specs);\n }\n\n /**\n * Add files to the context\n */\n addFiles(files: FileInfo[]): void {\n this.files.push(...files);\n }\n\n /**\n * Get a summary of the workspace for context\n */\n getSummary(): WorkspaceSummary {\n const commands = this.specs.filter((s) => s.type === 'command').length;\n const queries = this.specs.filter((s) => s.type === 'query').length;\n const events = this.specs.filter((s) => s.type === 'event').length;\n const presentations = this.specs.filter(\n (s) => s.type === 'presentation'\n ).length;\n\n const tsFiles = this.files.filter((f) => f.extension === '.ts').length;\n const specFiles = this.files.filter((f) => f.isSpec).length;\n\n return {\n name: this.workspacePath.split('/').pop() ?? 'workspace',\n path: this.workspacePath,\n specs: {\n total: this.specs.length,\n commands,\n queries,\n events,\n presentations,\n },\n files: {\n total: this.files.length,\n typescript: tsFiles,\n specFiles,\n },\n };\n }\n\n /**\n * Get a context summary for LLM prompts\n */\n getContextSummary(): string {\n const summary = this.getSummary();\n\n const parts: string[] = [\n `Workspace: ${summary.name}`,\n `Path: ${summary.path}`,\n '',\n '### Specs',\n `- Commands: ${summary.specs.commands}`,\n `- Queries: ${summary.specs.queries}`,\n `- Events: ${summary.specs.events}`,\n `- Presentations: ${summary.specs.presentations}`,\n ];\n\n if (this.specs.length > 0) {\n parts.push('', '### Available Specs');\n for (const spec of this.specs.slice(0, 20)) {\n parts.push(`- ${spec.name} (${spec.type})`);\n }\n if (this.specs.length > 20) {\n parts.push(`- ... and ${this.specs.length - 20} more`);\n }\n }\n\n return parts.join('\\n');\n }\n\n /**\n * Find specs matching a query\n */\n findSpecs(query: string): SpecInfo[] {\n const lowerQuery = query.toLowerCase();\n return this.specs.filter(\n (s) =>\n s.name.toLowerCase().includes(lowerQuery) ||\n s.description?.toLowerCase().includes(lowerQuery) ||\n s.tags?.some((t) => t.toLowerCase().includes(lowerQuery))\n );\n }\n\n /**\n * Find files matching a query\n */\n findFiles(query: string): FileInfo[] {\n const lowerQuery = query.toLowerCase();\n return this.files.filter(\n (f) =>\n f.path.toLowerCase().includes(lowerQuery) ||\n f.name.toLowerCase().includes(lowerQuery)\n );\n }\n}\n\n/**\n * Create a workspace context from a path\n */\nexport async function createWorkspaceContext(\n path: string,\n options?: Partial<WorkspaceContextConfig>\n): Promise<WorkspaceContext> {\n const context = new WorkspaceContext({\n workspacePath: path,\n ...options,\n });\n await context.initialize();\n return context;\n}\n"],"m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AAe,QAAQ,MAAM;GAC7B,cAAc,QAAQ,MAAM;GAC5B,aAAa,QAAQ,MAAM;GAC3B,oBAAoB,QAAQ,MAAM;GACnC;AAED,MAAI,KAAK,MAAM,SAAS,GAAG;AACzB,SAAM,KAAK,IAAI,sBAAsB;AACrC,QAAK,MAAM,QAAQ,KAAK,MAAM,MAAM,GAAG,GAAG,CACxC,OAAM,KAAK,KAAK,KAAK,KAAK,IAAI,KAAK,KAAK,GAAG;AAE7C,OAAI,KAAK,MAAM,SAAS,GACtB,OAAM,KAAK,aAAa,KAAK,MAAM,SAAS,GAAG,OAAO;;AAI1D,SAAO,MAAM,KAAK,KAAK;;;;;CAMzB,UAAU,OAA2B;EACnC,MAAM,aAAa,MAAM,aAAa;AACtC,SAAO,KAAK,MAAM,QACf,MACC,EAAE,KAAK,aAAa,CAAC,SAAS,WAAW,IACzC,EAAE,aAAa,aAAa,CAAC,SAAS,WAAW,IACjD,EAAE,MAAM,MAAM,MAAM,EAAE,aAAa,CAAC,SAAS,WAAW,CAAC,CAC5D;;;;;CAMH,UAAU,OAA2B;EACnC,MAAM,aAAa,MAAM,aAAa;AACtC,SAAO,KAAK,MAAM,QACf,MACC,EAAE,KAAK,aAAa,CAAC,SAAS,WAAW,IACzC,EAAE,KAAK,aAAa,CAAC,SAAS,WAAW,CAC5C;;;;;;AAOL,eAAsB,uBACpB,MACA,SAC2B;CAC3B,MAAM,UAAU,IAAI,iBAAiB;EACnC,eAAe;EACf,GAAG;EACJ,CAAC;AACF,OAAM,QAAQ,YAAY;AAC1B,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat-service.js","names":["conversation: ChatConversation"],"sources":["../../src/core/chat-service.ts"],"sourcesContent":["/**\n * Main chat orchestration service\n */\nimport { generateText, streamText } from 'ai';\nimport type { Provider as ChatProvider } from '@contractspec/lib.ai-providers';\nimport type { WorkspaceContext } from '../context/workspace-context';\nimport type { ConversationStore } from './conversation-store';\nimport { InMemoryConversationStore } from './conversation-store';\nimport type {\n ChatConversation,\n ChatStreamChunk,\n SendMessageOptions,\n SendMessageResult,\n StreamMessageResult,\n} from './message-types';\n\n/**\n * Configuration for ChatService\n */\nexport interface ChatServiceConfig {\n /** LLM provider to use */\n provider: ChatProvider;\n /** Optional workspace context for code-aware chat */\n context?: WorkspaceContext;\n /** Optional conversation store (defaults to in-memory) */\n store?: ConversationStore;\n /** Default system prompt */\n systemPrompt?: string;\n /** Maximum conversation history to include */\n maxHistoryMessages?: number;\n /** Callback for usage tracking */\n onUsage?: (usage: { inputTokens: number; outputTokens: number }) => void;\n}\n\n/**\n * Default system prompt for ContractSpec vibe coding\n */\nconst DEFAULT_SYSTEM_PROMPT = `You are ContractSpec AI, an expert coding assistant specialized in ContractSpec development.\n\nYour capabilities:\n- Help users create, modify, and understand ContractSpec specifications\n- Generate code that follows ContractSpec patterns and best practices\n- Explain concepts from the ContractSpec documentation\n- Suggest improvements and identify issues in specs and implementations\n\nGuidelines:\n- Be concise but thorough\n- Provide code examples when helpful\n- Reference relevant ContractSpec concepts and patterns\n- Ask clarifying questions when the user's intent is unclear\n- When suggesting code changes, explain the rationale`;\n\n/**\n * Main chat service for AI-powered conversations\n */\nexport class ChatService {\n private readonly provider: ChatProvider;\n private readonly context?: WorkspaceContext;\n private readonly store: ConversationStore;\n private readonly systemPrompt: string;\n private readonly maxHistoryMessages: number;\n private readonly onUsage?: (usage: {\n inputTokens: number;\n outputTokens: number;\n }) => void;\n\n constructor(config: ChatServiceConfig) {\n this.provider = config.provider;\n this.context = config.context;\n this.store = config.store ?? new InMemoryConversationStore();\n this.systemPrompt = config.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;\n this.maxHistoryMessages = config.maxHistoryMessages ?? 20;\n this.onUsage = config.onUsage;\n }\n\n /**\n * Send a message and get a complete response\n */\n async send(options: SendMessageOptions): Promise<SendMessageResult> {\n // Get or create conversation\n let conversation: ChatConversation;\n if (options.conversationId) {\n const existing = await this.store.get(options.conversationId);\n if (!existing) {\n throw new Error(`Conversation ${options.conversationId} not found`);\n }\n conversation = existing;\n } else {\n conversation = await this.store.create({\n status: 'active',\n provider: this.provider.name,\n model: this.provider.model,\n messages: [],\n workspacePath: this.context?.workspacePath,\n });\n }\n\n // Add user message\n await this.store.appendMessage(conversation.id, {\n role: 'user',\n content: options.content,\n status: 'completed',\n attachments: options.attachments,\n });\n\n // Build prompt from messages\n const prompt = this.buildPrompt(conversation, options);\n\n // Get the language model\n const model = this.provider.getModel();\n\n try {\n // Generate response\n const result = await generateText({\n model,\n prompt,\n system: this.systemPrompt,\n });\n\n // Save assistant message\n const assistantMessage = await this.store.appendMessage(conversation.id, {\n role: 'assistant',\n content: result.text,\n status: 'completed',\n });\n\n // Refresh conversation\n const updatedConversation = await this.store.get(conversation.id);\n if (!updatedConversation) {\n throw new Error('Conversation lost after update');\n }\n\n return {\n message: assistantMessage,\n conversation: updatedConversation,\n };\n } catch (error) {\n // Save error message\n await this.store.appendMessage(conversation.id, {\n role: 'assistant',\n content: '',\n status: 'error',\n error: {\n code: 'generation_failed',\n message: error instanceof Error ? error.message : String(error),\n },\n });\n\n throw error;\n }\n }\n\n /**\n * Send a message and get a streaming response\n */\n async stream(options: SendMessageOptions): Promise<StreamMessageResult> {\n // Get or create conversation\n let conversation: ChatConversation;\n if (options.conversationId) {\n const existing = await this.store.get(options.conversationId);\n if (!existing) {\n throw new Error(`Conversation ${options.conversationId} not found`);\n }\n conversation = existing;\n } else {\n conversation = await this.store.create({\n status: 'active',\n provider: this.provider.name,\n model: this.provider.model,\n messages: [],\n workspacePath: this.context?.workspacePath,\n });\n }\n\n // Add user message\n await this.store.appendMessage(conversation.id, {\n role: 'user',\n content: options.content,\n status: 'completed',\n attachments: options.attachments,\n });\n\n // Create placeholder for assistant message\n const assistantMessage = await this.store.appendMessage(conversation.id, {\n role: 'assistant',\n content: '',\n status: 'streaming',\n });\n\n // Build prompt\n const prompt = this.buildPrompt(conversation, options);\n\n // Get the language model\n const model = this.provider.getModel();\n\n // Create async generator for streaming\n const self = {\n systemPrompt: this.systemPrompt,\n store: this.store,\n };\n async function* streamGenerator(): AsyncIterable<ChatStreamChunk> {\n let fullContent = '';\n\n try {\n const result = streamText({\n model,\n prompt,\n system: self.systemPrompt,\n });\n\n for await (const chunk of result.textStream) {\n fullContent += chunk;\n yield { type: 'text', content: chunk };\n }\n\n // Update message with final content\n await self.store.updateMessage(conversation.id, assistantMessage.id, {\n content: fullContent,\n status: 'completed',\n });\n\n yield {\n type: 'done',\n };\n } catch (error) {\n await self.store.updateMessage(conversation.id, assistantMessage.id, {\n content: fullContent,\n status: 'error',\n error: {\n code: 'stream_failed',\n message: error instanceof Error ? error.message : String(error),\n },\n });\n\n yield {\n type: 'error',\n error: {\n code: 'stream_failed',\n message: error instanceof Error ? error.message : String(error),\n },\n };\n }\n }\n\n return {\n conversationId: conversation.id,\n messageId: assistantMessage.id,\n stream: streamGenerator(),\n };\n }\n\n /**\n * Get a conversation by ID\n */\n async getConversation(\n conversationId: string\n ): Promise<ChatConversation | null> {\n return this.store.get(conversationId);\n }\n\n /**\n * List conversations\n */\n async listConversations(options?: {\n limit?: number;\n offset?: number;\n }): Promise<ChatConversation[]> {\n return this.store.list({\n status: 'active',\n ...options,\n });\n }\n\n /**\n * Delete a conversation\n */\n async deleteConversation(conversationId: string): Promise<boolean> {\n return this.store.delete(conversationId);\n }\n\n /**\n * Build prompt string for LLM\n */\n private buildPrompt(\n conversation: ChatConversation,\n options: SendMessageOptions\n ): string {\n let prompt = '';\n\n // Add conversation history (limited)\n const historyStart = Math.max(\n 0,\n conversation.messages.length - this.maxHistoryMessages\n );\n for (let i = historyStart; i < conversation.messages.length; i++) {\n const msg = conversation.messages[i];\n if (!msg) continue;\n if (msg.role === 'user' || msg.role === 'assistant') {\n prompt += `${msg.role === 'user' ? 'User' : 'Assistant'}: ${msg.content}\\n\\n`;\n }\n }\n\n // Add current message with attachments\n let content = options.content;\n if (options.attachments?.length) {\n const attachmentInfo = options.attachments\n .map((a) => {\n if (a.type === 'file' || a.type === 'code') {\n return `\\n\\n### ${a.name}\\n\\`\\`\\`\\n${a.content}\\n\\`\\`\\``;\n }\n return `\\n\\n[Attachment: ${a.name}]`;\n })\n .join('');\n content += attachmentInfo;\n }\n\n prompt += `User: ${content}\\n\\nAssistant:`;\n\n return prompt;\n }\n}\n\n/**\n * Create a chat service with the given configuration\n */\nexport function createChatService(config: ChatServiceConfig): ChatService {\n return new ChatService(config);\n}\n"],"mappings":";;;;;;;;;;AAqCA,MAAM,wBAAwB;;;;;;;;;;;;;;;;;AAkB9B,IAAa,cAAb,MAAyB;CACvB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAKjB,YAAY,QAA2B;AACrC,OAAK,WAAW,OAAO;AACvB,OAAK,UAAU,OAAO;AACtB,OAAK,QAAQ,OAAO,SAAS,IAAI,2BAA2B;AAC5D,OAAK,eAAe,OAAO,gBAAgB;AAC3C,OAAK,qBAAqB,OAAO,sBAAsB;AACvD,OAAK,UAAU,OAAO;;;;;CAMxB,MAAM,KAAK,SAAyD;EAElE,IAAIA;AACJ,MAAI,QAAQ,gBAAgB;GAC1B,MAAM,WAAW,MAAM,KAAK,MAAM,IAAI,QAAQ,eAAe;AAC7D,OAAI,CAAC,SACH,OAAM,IAAI,MAAM,gBAAgB,QAAQ,eAAe,YAAY;AAErE,kBAAe;QAEf,gBAAe,MAAM,KAAK,MAAM,OAAO;GACrC,QAAQ;GACR,UAAU,KAAK,SAAS;GACxB,OAAO,KAAK,SAAS;GACrB,UAAU,EAAE;GACZ,eAAe,KAAK,SAAS;GAC9B,CAAC;AAIJ,QAAM,KAAK,MAAM,cAAc,aAAa,IAAI;GAC9C,MAAM;GACN,SAAS,QAAQ;GACjB,QAAQ;GACR,aAAa,QAAQ;GACtB,CAAC;EAGF,MAAM,SAAS,KAAK,YAAY,cAAc,QAAQ;EAGtD,MAAM,QAAQ,KAAK,SAAS,UAAU;AAE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S,YACtC,WAAU,GAAG,IAAI,SAAS,SAAS,SAAS,YAAY,IAAI,IAAI,QAAQ;;EAK5E,IAAI,UAAU,QAAQ;AACtB,MAAI,QAAQ,aAAa,QAAQ;GAC/B,MAAM,iBAAiB,QAAQ,YAC5B,KAAK,MAAM;AACV,QAAI,EAAE,SAAS,UAAU,EAAE,SAAS,OAClC,QAAO,WAAW,EAAE,KAAK,YAAY,EAAE,QAAQ;AAEjD,WAAO,oBAAoB,EAAE,KAAK;KAClC,CACD,KAAK,GAAG;AACX,cAAW;;AAGb,YAAU,SAAS,QAAQ;AAE3B,SAAO;;;;;;AAOX,SAAgB,kBAAkB,QAAwC;AACxE,QAAO,IAAI,YAAY,OAAO"}
|
|
1
|
+
{"version":3,"file":"chat-service.js","names":[],"sources":["../../src/core/chat-service.ts"],"sourcesContent":["/**\n * Main chat orchestration service\n */\nimport { generateText, streamText } from 'ai';\nimport type { Provider as ChatProvider } from '@contractspec/lib.ai-providers';\nimport type { WorkspaceContext } from '../context/workspace-context';\nimport type { ConversationStore } from './conversation-store';\nimport { InMemoryConversationStore } from './conversation-store';\nimport type {\n ChatConversation,\n ChatStreamChunk,\n SendMessageOptions,\n SendMessageResult,\n StreamMessageResult,\n} from './message-types';\n\n/**\n * Configuration for ChatService\n */\nexport interface ChatServiceConfig {\n /** LLM provider to use */\n provider: ChatProvider;\n /** Optional workspace context for code-aware chat */\n context?: WorkspaceContext;\n /** Optional conversation store (defaults to in-memory) */\n store?: ConversationStore;\n /** Default system prompt */\n systemPrompt?: string;\n /** Maximum conversation history to include */\n maxHistoryMessages?: number;\n /** Callback for usage tracking */\n onUsage?: (usage: { inputTokens: number; outputTokens: number }) => void;\n}\n\n/**\n * Default system prompt for ContractSpec vibe coding\n */\nconst DEFAULT_SYSTEM_PROMPT = `You are ContractSpec AI, an expert coding assistant specialized in ContractSpec development.\n\nYour capabilities:\n- Help users create, modify, and understand ContractSpec specifications\n- Generate code that follows ContractSpec patterns and best practices\n- Explain concepts from the ContractSpec documentation\n- Suggest improvements and identify issues in specs and implementations\n\nGuidelines:\n- Be concise but thorough\n- Provide code examples when helpful\n- Reference relevant ContractSpec concepts and patterns\n- Ask clarifying questions when the user's intent is unclear\n- When suggesting code changes, explain the rationale`;\n\n/**\n * Main chat service for AI-powered conversations\n */\nexport class ChatService {\n private readonly provider: ChatProvider;\n private readonly context?: WorkspaceContext;\n private readonly store: ConversationStore;\n private readonly systemPrompt: string;\n private readonly maxHistoryMessages: number;\n private readonly onUsage?: (usage: {\n inputTokens: number;\n outputTokens: number;\n }) => void;\n\n constructor(config: ChatServiceConfig) {\n this.provider = config.provider;\n this.context = config.context;\n this.store = config.store ?? new InMemoryConversationStore();\n this.systemPrompt = config.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;\n this.maxHistoryMessages = config.maxHistoryMessages ?? 20;\n this.onUsage = config.onUsage;\n }\n\n /**\n * Send a message and get a complete response\n */\n async send(options: SendMessageOptions): Promise<SendMessageResult> {\n // Get or create conversation\n let conversation: ChatConversation;\n if (options.conversationId) {\n const existing = await this.store.get(options.conversationId);\n if (!existing) {\n throw new Error(`Conversation ${options.conversationId} not found`);\n }\n conversation = existing;\n } else {\n conversation = await this.store.create({\n status: 'active',\n provider: this.provider.name,\n model: this.provider.model,\n messages: [],\n workspacePath: this.context?.workspacePath,\n });\n }\n\n // Add user message\n await this.store.appendMessage(conversation.id, {\n role: 'user',\n content: options.content,\n status: 'completed',\n attachments: options.attachments,\n });\n\n // Build prompt from messages\n const prompt = this.buildPrompt(conversation, options);\n\n // Get the language model\n const model = this.provider.getModel();\n\n try {\n // Generate response\n const result = await generateText({\n model,\n prompt,\n system: this.systemPrompt,\n });\n\n // Save assistant message\n const assistantMessage = await this.store.appendMessage(conversation.id, {\n role: 'assistant',\n content: result.text,\n status: 'completed',\n });\n\n // Refresh conversation\n const updatedConversation = await this.store.get(conversation.id);\n if (!updatedConversation) {\n throw new Error('Conversation lost after update');\n }\n\n return {\n message: assistantMessage,\n conversation: updatedConversation,\n };\n } catch (error) {\n // Save error message\n await this.store.appendMessage(conversation.id, {\n role: 'assistant',\n content: '',\n status: 'error',\n error: {\n code: 'generation_failed',\n message: error instanceof Error ? error.message : String(error),\n },\n });\n\n throw error;\n }\n }\n\n /**\n * Send a message and get a streaming response\n */\n async stream(options: SendMessageOptions): Promise<StreamMessageResult> {\n // Get or create conversation\n let conversation: ChatConversation;\n if (options.conversationId) {\n const existing = await this.store.get(options.conversationId);\n if (!existing) {\n throw new Error(`Conversation ${options.conversationId} not found`);\n }\n conversation = existing;\n } else {\n conversation = await this.store.create({\n status: 'active',\n provider: this.provider.name,\n model: this.provider.model,\n messages: [],\n workspacePath: this.context?.workspacePath,\n });\n }\n\n // Add user message\n await this.store.appendMessage(conversation.id, {\n role: 'user',\n content: options.content,\n status: 'completed',\n attachments: options.attachments,\n });\n\n // Create placeholder for assistant message\n const assistantMessage = await this.store.appendMessage(conversation.id, {\n role: 'assistant',\n content: '',\n status: 'streaming',\n });\n\n // Build prompt\n const prompt = this.buildPrompt(conversation, options);\n\n // Get the language model\n const model = this.provider.getModel();\n\n // Create async generator for streaming\n const self = {\n systemPrompt: this.systemPrompt,\n store: this.store,\n };\n async function* streamGenerator(): AsyncIterable<ChatStreamChunk> {\n let fullContent = '';\n\n try {\n const result = streamText({\n model,\n prompt,\n system: self.systemPrompt,\n });\n\n for await (const chunk of result.textStream) {\n fullContent += chunk;\n yield { type: 'text', content: chunk };\n }\n\n // Update message with final content\n await self.store.updateMessage(conversation.id, assistantMessage.id, {\n content: fullContent,\n status: 'completed',\n });\n\n yield {\n type: 'done',\n };\n } catch (error) {\n await self.store.updateMessage(conversation.id, assistantMessage.id, {\n content: fullContent,\n status: 'error',\n error: {\n code: 'stream_failed',\n message: error instanceof Error ? error.message : String(error),\n },\n });\n\n yield {\n type: 'error',\n error: {\n code: 'stream_failed',\n message: error instanceof Error ? error.message : String(error),\n },\n };\n }\n }\n\n return {\n conversationId: conversation.id,\n messageId: assistantMessage.id,\n stream: streamGenerator(),\n };\n }\n\n /**\n * Get a conversation by ID\n */\n async getConversation(\n conversationId: string\n ): Promise<ChatConversation | null> {\n return this.store.get(conversationId);\n }\n\n /**\n * List conversations\n */\n async listConversations(options?: {\n limit?: number;\n offset?: number;\n }): Promise<ChatConversation[]> {\n return this.store.list({\n status: 'active',\n ...options,\n });\n }\n\n /**\n * Delete a conversation\n */\n async deleteConversation(conversationId: string): Promise<boolean> {\n return this.store.delete(conversationId);\n }\n\n /**\n * Build prompt string for LLM\n */\n private buildPrompt(\n conversation: ChatConversation,\n options: SendMessageOptions\n ): string {\n let prompt = '';\n\n // Add conversation history (limited)\n const historyStart = Math.max(\n 0,\n conversation.messages.length - this.maxHistoryMessages\n );\n for (let i = historyStart; i < conversation.messages.length; i++) {\n const msg = conversation.messages[i];\n if (!msg) continue;\n if (msg.role === 'user' || msg.role === 'assistant') {\n prompt += `${msg.role === 'user' ? 'User' : 'Assistant'}: ${msg.content}\\n\\n`;\n }\n }\n\n // Add current message with attachments\n let content = options.content;\n if (options.attachments?.length) {\n const attachmentInfo = options.attachments\n .map((a) => {\n if (a.type === 'file' || a.type === 'code') {\n return `\\n\\n### ${a.name}\\n\\`\\`\\`\\n${a.content}\\n\\`\\`\\``;\n }\n return `\\n\\n[Attachment: ${a.name}]`;\n })\n .join('');\n content += attachmentInfo;\n }\n\n prompt += `User: ${content}\\n\\nAssistant:`;\n\n return prompt;\n }\n}\n\n/**\n * Create a chat service with the given configuration\n */\nexport function createChatService(config: ChatServiceConfig): ChatService {\n return new ChatService(config);\n}\n"],"mappings":";;;;;;;;;;AAqCA,MAAM,wBAAwB;;;;;;;;;;;;;;;;;AAkB9B,IAAa,cAAb,MAAyB;CACvB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAKjB,YAAY,QAA2B;AACrC,OAAK,WAAW,OAAO;AACvB,OAAK,UAAU,OAAO;AACtB,OAAK,QAAQ,OAAO,SAAS,IAA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O;MACL,MAAM;MACN,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;MAChE;KACF;;;AAIL,SAAO;GACL,gBAAgB,aAAa;GAC7B,WAAW,iBAAiB;GAC5B,QAAQ,iBAAiB;GAC1B;;;;;CAMH,MAAM,gBACJ,gBACkC;AAClC,SAAO,KAAK,MAAM,IAAI,eAAe;;;;;CAMvC,MAAM,kBAAkB,SAGQ;AAC9B,SAAO,KAAK,MAAM,KAAK;GACrB,QAAQ;GACR,GAAG;GACJ,CAAC;;;;;CAMJ,MAAM,mBAAmB,gBAA0C;AACjE,SAAO,KAAK,MAAM,OAAO,eAAe;;;;;CAM1C,AAAQ,YACN,cACA,SACQ;EACR,IAAI,SAAS;EAGb,MAAM,eAAe,KAAK,IACxB,GACA,aAAa,SAAS,SAAS,KAAK,mBACrC;AACD,OAAK,IAAI,IAAI,cAAc,IAAI,aAAa,SAAS,QAAQ,KAAK;GAChE,MAAM,MAAM,aAAa,SAAS;AAClC,OAAI,CAAC,IAAK;AACV,OAAI,IAAI,SAAS,UAAU,IAAI,SAAS,YACtC,WAAU,GAAG,IAAI,SAAS,SAAS,SAAS,YAAY,IAAI,IAAI,QAAQ;;EAK5E,IAAI,UAAU,QAAQ;AACtB,MAAI,QAAQ,aAAa,QAAQ;GAC/B,MAAM,iBAAiB,QAAQ,YAC5B,KAAK,MAAM;AACV,QAAI,EAAE,SAAS,UAAU,EAAE,SAAS,OAClC,QAAO,WAAW,EAAE,KAAK,YAAY,EAAE,QAAQ;AAEjD,WAAO,oBAAoB,EAAE,KAAK;KAClC,CACD,KAAK,GAAG;AACX,cAAW;;AAGb,YAAU,SAAS,QAAQ;AAE3B,SAAO;;;;;;AAOX,SAAgB,kBAAkB,QAAwC;AACxE,QAAO,IAAI,YAAY,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversation-store.js","names":[
|
|
1
|
+
{"version":3,"file":"conversation-store.js","names":[],"sources":["../../src/core/conversation-store.ts"],"sourcesContent":["/**\n * Conversation storage interface and implementations\n */\nimport type {\n ChatConversation,\n ChatMessage,\n ConversationStatus,\n} from './message-types';\n\n/**\n * Interface for conversation persistence\n */\nexport interface ConversationStore {\n /**\n * Get a conversation by ID\n */\n get(conversationId: string): Promise<ChatConversation | null>;\n\n /**\n * Create a new conversation\n */\n create(\n conversation: Omit<ChatConversation, 'id' | 'createdAt' | 'updatedAt'>\n ): Promise<ChatConversation>;\n\n /**\n * Update conversation properties\n */\n update(\n conversationId: string,\n updates: Partial<\n Pick<ChatConversation, 'title' | 'status' | 'summary' | 'metadata'>\n >\n ): Promise<ChatConversation | null>;\n\n /**\n * Append a message to a conversation\n */\n appendMessage(\n conversationId: string,\n message: Omit<\n ChatMessage,\n 'id' | 'conversationId' | 'createdAt' | 'updatedAt'\n >\n ): Promise<ChatMessage>;\n\n /**\n * Update a message in a conversation\n */\n updateMessage(\n conversationId: string,\n messageId: string,\n updates: Partial<ChatMessage>\n ): Promise<ChatMessage | null>;\n\n /**\n * Delete a conversation\n */\n delete(conversationId: string): Promise<boolean>;\n\n /**\n * List conversations with optional filters\n */\n list(options?: {\n status?: ConversationStatus;\n limit?: number;\n offset?: number;\n }): Promise<ChatConversation[]>;\n\n /**\n * Search conversations by content\n */\n search(query: string, limit?: number): Promise<ChatConversation[]>;\n}\n\n/**\n * Generate a unique ID\n */\nfunction generateId(prefix: string): string {\n return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;\n}\n\n/**\n * In-memory conversation store for development and testing\n */\nexport class InMemoryConversationStore implements ConversationStore {\n private readonly conversations = new Map<string, ChatConversation>();\n\n async get(conversationId: string): Promise<ChatConversation | null> {\n return this.conversations.get(conversationId) ?? null;\n }\n\n async create(\n conversation: Omit<ChatConversation, 'id' | 'createdAt' | 'updatedAt'>\n ): Promise<ChatConversation> {\n const now = new Date();\n const fullConversation: ChatConversation = {\n ...conversation,\n id: generateId('conv'),\n createdAt: now,\n updatedAt: now,\n };\n this.conversations.set(fullConversation.id, fullConversation);\n return fullConversation;\n }\n\n async update(\n conversationId: string,\n updates: Partial<\n Pick<ChatConversation, 'title' | 'status' | 'summary' | 'metadata'>\n >\n ): Promise<ChatConversation | null> {\n const conversation = this.conversations.get(conversationId);\n if (!conversation) return null;\n\n const updated = {\n ...conversation,\n ...updates,\n updatedAt: new Date(),\n };\n this.conversations.set(conversationId, updated);\n return updated;\n }\n\n async appendMessage(\n conversationId: string,\n message: Omit<\n ChatMessage,\n 'id' | 'conversationId' | 'createdAt' | 'updatedAt'\n >\n ): Promise<ChatMessage> {\n const conversation = this.conversations.get(conversationId);\n if (!conversation) {\n throw new Error(`Conversation ${conversationId} not found`);\n }\n\n const now = new Date();\n const fullMessage: ChatMessage = {\n ...message,\n id: generateId('msg'),\n conversationId,\n createdAt: now,\n updatedAt: now,\n };\n\n conversation.messages.push(fullMessage);\n conversation.updatedAt = now;\n return fullMessage;\n }\n\n async updateMessage(\n conversationId: string,\n messageId: string,\n updates: Partial<ChatMessage>\n ): Promise<ChatMessage | null> {\n const conversation = this.conversations.get(conversationId);\n if (!conversation) return null;\n\n const messageIndex = conversation.messages.findIndex(\n (m) => m.id === messageId\n );\n if (messageIndex === -1) return null;\n\n const message = conversation.messages[messageIndex];\n if (!message) return null;\n\n const updated = {\n ...message,\n ...updates,\n updatedAt: new Date(),\n };\n conversation.messages[messageIndex] = updated;\n conversation.updatedAt = new Date();\n return updated;\n }\n\n async delete(conversationId: string): Promise<boolean> {\n return this.conversations.delete(conversationId);\n }\n\n async list(options?: {\n status?: ConversationStatus;\n limit?: number;\n offset?: number;\n }): Promise<ChatConversation[]> {\n let results = Array.from(this.conversations.values());\n\n if (options?.status) {\n results = results.filter((c) => c.status === options.status);\n }\n\n // Sort by updatedAt descending\n results.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());\n\n const offset = options?.offset ?? 0;\n const limit = options?.limit ?? 100;\n return results.slice(offset, offset + limit);\n }\n\n async search(query: string, limit = 20): Promise<ChatConversation[]> {\n const lowerQuery = query.toLowerCase();\n const results: ChatConversation[] = [];\n\n for (const conversation of this.conversations.values()) {\n // Search in title\n if (conversation.title?.toLowerCase().includes(lowerQuery)) {\n results.push(conversation);\n continue;\n }\n\n // Search in messages\n const hasMatch = conversation.messages.some((m) =>\n m.content.toLowerCase().includes(lowerQuery)\n );\n if (hasMatch) {\n results.push(conversation);\n }\n\n if (results.length >= limit) break;\n }\n\n return results;\n }\n\n /**\n * Clear all conversations (for testing)\n */\n clear(): void {\n this.conversations.clear();\n }\n}\n\n/**\n * Create an in-memory conversation store\n */\nexport function createInMemoryConversationStore(): ConversationStore {\n return new InMemoryConversationStore();\n}\n"],"mappings":";;;;AA8EA,SAAS,WAAW,QAAwB;AAC1C,QAAO,GAAG,OAAO,GAAG,KAAK,KAAK,CAAC,GAAG,KAAK,QAAQ,CAAC,SAAS,GAAG,CAAC,MAAM,GAAG,GAAG;;;;;AAM3E,IAAa,4BAAb,MAAoE;CAClE,AAAiB,gCAAgB,IAAI,KAA+B;CAEpE,MAAM,IAAI,gBAA0D;AAClE,SAAO,KAAK,cAAc,IAAI,eAAe,IAAI;;CAGnD,MAAM,OACJ,cAC2B;EAC3B,MAAM,sBAAM,IAAI,MAAM;EACtB,MAAM,mBAAqC;GACzC,GAAG;GACH,IAAI,WAAW,OAAO;GACtB,WAAW;GACX,WAAW;GACZ;AACD,OAAK,cAAc,IAAI,iBAAiB,IAAI,iBAAiB;AAC7D,SAAO;;CAGT,MAAM,OACJ,gBACA,SAGkC;EAClC,MAAM,eAAe,KAAK,cAAc,IAAI,eAAe;AAC3D,MAAI,CAAC,aAAc,QAAO;EAE1B,MAAM,UAAU;GACd,GAAG;GACH,GAAG;GACH,2BAAW,IAAI,MAAM;GACtB;AACD,OAAK,cAAc,IAAI,gBAAgB,QAAQ;AAC/C,SAAO;;CAGT,MAAM,cACJ,gBACA,SAIsB;EACtB,MAAM,eAAe,KAAK,cAAc,IAAI,eAAe;AAC3D,MAAI,CAAC,aACH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EAAE;AAC1D,YAAQ,KAAK,aAAa;AAC1B;;AAOF,OAHiB,aAAa,SAAS,MAAM,MAC3C,EAAE,QAAQ,aAAa,CAAC,SAAS,WAAW,CAC7C,CAEC,SAAQ,KAAK,aAAa;AAG5B,OAAI,QAAQ,UAAU,MAAO;;AAG/B,SAAO;;;;;CAMT,QAAc;AACZ,OAAK,cAAc,OAAO;;;;;;AAO9B,SAAgB,kCAAqD;AACnE,QAAO,IAAI,2BAA2B"}
|
package/dist/events.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as _contractspec_lib_schema0 from "@contractspec/lib.schema";
|
|
2
1
|
import * as _contractspec_lib_contracts0 from "@contractspec/lib.contracts";
|
|
2
|
+
import * as _contractspec_lib_schema0 from "@contractspec/lib.schema";
|
|
3
3
|
|
|
4
4
|
//#region src/events.d.ts
|
|
5
5
|
declare const MessageSentEvent: _contractspec_lib_contracts0.EventSpec<_contractspec_lib_schema0.SchemaModel<{
|
package/dist/events.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ChatConversationModel, ChatMessageModel } from "./schema.js";
|
|
2
|
-
import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
|
|
3
2
|
import { defineEvent } from "@contractspec/lib.contracts";
|
|
3
|
+
import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
|
|
4
4
|
|
|
5
5
|
//#region src/events.ts
|
|
6
6
|
const MessageSentEvent = defineEvent({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatInput.js","names":["
|
|
1
|
+
{"version":3,"file":"ChatInput.js","names":["content"],"sources":["../../../src/presentation/components/ChatInput.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { cn } from '@contractspec/lib.ui-kit-web/ui/utils';\nimport { Textarea } from '@contractspec/lib.design-system';\nimport { Button } from '@contractspec/lib.design-system';\nimport { Send, Paperclip, X, Loader2, FileText, Code } from 'lucide-react';\nimport type { ChatAttachment } from '../../core/message-types';\n\nexport interface ChatInputProps {\n /** Called when a message is sent */\n onSend: (content: string, attachments?: ChatAttachment[]) => void;\n /** Whether input is disabled (e.g., during streaming) */\n disabled?: boolean;\n /** Whether currently loading/streaming */\n isLoading?: boolean;\n /** Placeholder text */\n placeholder?: string;\n /** Additional class name */\n className?: string;\n /** Show attachment button */\n showAttachments?: boolean;\n /** Max attachments allowed */\n maxAttachments?: number;\n}\n\n/**\n * Chat input component with attachment support\n */\nexport function ChatInput({\n onSend,\n disabled = false,\n isLoading = false,\n placeholder = 'Type a message...',\n className,\n showAttachments = true,\n maxAttachments = 5,\n}: ChatInputProps) {\n const [content, setContent] = React.useState('');\n const [attachments, setAttachments] = React.useState<ChatAttachment[]>([]);\n const textareaRef = React.useRef<HTMLTextAreaElement>(null);\n const fileInputRef = React.useRef<HTMLInputElement>(null);\n\n const canSend = content.trim().length > 0 || attachments.length > 0;\n\n const handleSubmit = React.useCallback(\n (e?: React.FormEvent) => {\n e?.preventDefault();\n if (!canSend || disabled || isLoading) return;\n\n onSend(content.trim(), attachments.length > 0 ? attachments : undefined);\n setContent('');\n setAttachments([]);\n\n // Focus back on textarea\n textareaRef.current?.focus();\n },\n [canSend, content, attachments, disabled, isLoading, onSend]\n );\n\n const handleKeyDown = React.useCallback(\n (e: React.KeyboardEvent) => {\n // Submit on Enter (without Shift)\n if (e.key === 'Enter' && !e.shiftKey) {\n e.preventDefault();\n handleSubmit();\n }\n },\n [handleSubmit]\n );\n\n const handleFileSelect = React.useCallback(\n async (e: React.ChangeEvent<HTMLInputElement>) => {\n const files = e.target.files;\n if (!files) return;\n\n const newAttachments: ChatAttachment[] = [];\n\n for (const file of Array.from(files)) {\n if (attachments.length + newAttachments.length >= maxAttachments) break;\n\n const content = await file.text();\n const extension = file.name.split('.').pop()?.toLowerCase() ?? '';\n const isCode = [\n 'ts',\n 'tsx',\n 'js',\n 'jsx',\n 'py',\n 'go',\n 'rs',\n 'java',\n ].includes(extension);\n\n newAttachments.push({\n id: `att_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`,\n type: isCode ? 'code' : 'file',\n name: file.name,\n content,\n mimeType: file.type,\n size: file.size,\n });\n }\n\n setAttachments((prev) => [...prev, ...newAttachments]);\n\n // Reset file input\n e.target.value = '';\n },\n [attachments.length, maxAttachments]\n );\n\n const removeAttachment = React.useCallback((id: string) => {\n setAttachments((prev) => prev.filter((a) => a.id !== id));\n }, []);\n\n return (\n <div className={cn('flex flex-col gap-2', className)}>\n {/* Attachments preview */}\n {attachments.length > 0 && (\n <div className=\"flex flex-wrap gap-2\">\n {attachments.map((attachment) => (\n <div\n key={attachment.id}\n className={cn(\n 'flex items-center gap-1.5 rounded-md px-2 py-1',\n 'bg-muted text-muted-foreground text-sm'\n )}\n >\n {attachment.type === 'code' ? (\n <Code className=\"h-3.5 w-3.5\" />\n ) : (\n <FileText className=\"h-3.5 w-3.5\" />\n )}\n <span className=\"max-w-[150px] truncate\">{attachment.name}</span>\n <button\n type=\"button\"\n onClick={() => removeAttachment(attachment.id)}\n className=\"hover:text-foreground\"\n aria-label={`Remove ${attachment.name}`}\n >\n <X className=\"h-3.5 w-3.5\" />\n </button>\n </div>\n ))}\n </div>\n )}\n\n {/* Input form */}\n <form onSubmit={handleSubmit} className=\"flex items-end gap-2\">\n {/* Attachment button */}\n {showAttachments && (\n <>\n <input\n ref={fileInputRef}\n type=\"file\"\n multiple\n accept=\".ts,.tsx,.js,.jsx,.json,.md,.txt,.py,.go,.rs,.java,.yaml,.yml\"\n onChange={handleFileSelect}\n className=\"hidden\"\n aria-label=\"Attach files\"\n />\n <Button\n type=\"button\"\n variant=\"ghost\"\n size=\"sm\"\n onPress={() => fileInputRef.current?.click()}\n disabled={disabled || attachments.length >= maxAttachments}\n aria-label=\"Attach files\"\n >\n <Paperclip className=\"h-4 w-4\" />\n </Button>\n </>\n )}\n\n {/* Text input */}\n <div className=\"relative flex-1\">\n <Textarea\n value={content}\n onChange={(e) => setContent(e.target.value)}\n onKeyDown={handleKeyDown}\n placeholder={placeholder}\n disabled={disabled}\n className={cn(\n 'max-h-[200px] min-h-[44px] resize-none pr-12',\n 'focus-visible:ring-1'\n )}\n rows={1}\n aria-label=\"Chat message\"\n />\n </div>\n\n {/* Send button */}\n <Button\n type=\"submit\"\n disabled={!canSend || disabled || isLoading}\n size=\"sm\"\n aria-label={isLoading ? 'Sending...' : 'Send message'}\n >\n {isLoading ? (\n <Loader2 className=\"h-4 w-4 animate-spin\" />\n ) : (\n <Send className=\"h-4 w-4\" />\n )}\n </Button>\n </form>\n\n {/* Helper text */}\n <p className=\"text-muted-foreground text-xs\">\n Press Enter to send, Shift+Enter for new line\n </p>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;AA6BA,SAAgB,UAAU,EACxB,QACA,WAAW,OACX,YAAY,OACZ,cAAc,qBACd,WACA,kBAAkB,MAClB,iBAAiB,KACA;CACjB,MAAM,CAAC,SAAS,cAAc,MAAM,SAAS,GAAG;CAChD,MAAM,CAAC,aAAa,kBAAkB,MAAM,SAA2B,EAAE,CAAC;CAC1E,MAAM,cAAc,MAAM,OAA4B,KAAK;CAC3D,MAAM,eAAe,MAAM,OAAyB,KAAK;CAEzD,MAAM,UAAU,QAAQ,MAAM,CAAC,SAAS,KAAK,YAAY,SAAS;CAElE,MAAM,eAAe,MAAM,aACxB,MAAwB;AACvB,KAAG,gBAAgB;AACnB,MAAI,CAAC,WAAW,YAAY,UAAW;AAEvC,SAAO,QAAQ,MAAM,EAAE,YAAY,SAAS,IAAI,cAAc,OAAU;AACxE,aAAW,GAAG;AACd,iBAAe,EAAE,CAAC;AAGlB,cAAY,SAAS,OAAO;IAE9B;EAAC;EAAS;EAAS;EAAa;EAAU;EAAW;EAAO,CAC7D;CAED,MAAM,gBAAgB,MAAM,aACzB,MAA2B;AAE1B,MAAI,EAAE,QAAQ,WAAW,CAAC,EAAE,UAAU;AACpC,KAAE,gBAAgB;AAClB,iBAAc;;IAGlB,CAAC,aAAa,C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V,cAAY,UAAU,WAAW;iBAEjC,oBAAC,KAAE,WAAU,gBAAgB;QACtB;;OAnBJ,WAAW,GAoBZ,CACN;KACE;GAIR,qBAAC;IAAK,UAAU;IAAc,WAAU;;KAErC,mBACC,4CACE,oBAAC;MACC,KAAK;MACL,MAAK;MACL;MACA,QAAO;MACP,UAAU;MACV,WAAU;MACV,cAAW;OACX,EACF,oBAAC;MACC,MAAK;MACL,SAAQ;MACR,MAAK;MACL,eAAe,aAAa,SAAS,OAAO;MAC5C,UAAU,YAAY,YAAY,UAAU;MAC5C,cAAW;gBAEX,oBAAC,aAAU,WAAU,YAAY;OAC1B,IACR;KAIL,oBAAC;MAAI,WAAU;gBACb,oBAAC;OACC,OAAO;OACP,WAAW,MAAM,WAAW,EAAE,OAAO,MAAM;OAC3C,WAAW;OACE;OACH;OACV,WAAW,GACT,gDACA,uBACD;OACD,MAAM;OACN,cAAW;QACX;OACE;KAGN,oBAAC;MACC,MAAK;MACL,UAAU,CAAC,WAAW,YAAY;MAClC,MAAK;MACL,cAAY,YAAY,eAAe;gBAEtC,YACC,oBAAC,WAAQ,WAAU,yBAAyB,GAE5C,oBAAC,QAAK,WAAU,YAAY;OAEvB;;KACJ;GAGP,oBAAC;IAAE,WAAU;cAAgC;KAEzC;;GACA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ChatMessage.js","names":[
|
|
1
|
+
{"version":3,"file":"ChatMessage.js","names":[],"sources":["../../../src/presentation/components/ChatMessage.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { cn } from '@contractspec/lib.ui-kit-web/ui/utils';\nimport { Avatar, AvatarFallback } from '@contractspec/lib.ui-kit-web/ui/avatar';\nimport { Skeleton } from '@contractspec/lib.ui-kit-web/ui/skeleton';\nimport { Bot, User, AlertCircle, Copy, Check } from 'lucide-react';\nimport { Button } from '@contractspec/lib.design-system';\nimport type { ChatMessage as ChatMessageType } from '../../core/message-types';\nimport { CodePreview } from './CodePreview';\n\nexport interface ChatMessageProps {\n message: ChatMessageType;\n className?: string;\n /** Show copy button */\n showCopy?: boolean;\n /** Show avatar */\n showAvatar?: boolean;\n}\n\n/**\n * Extract code blocks from message content\n */\nfunction extractCodeBlocks(\n content: string\n): { language: string; code: string; raw: string }[] {\n const codeBlockRegex = /```(\\w+)?\\n([\\s\\S]*?)```/g;\n const blocks: { language: string; code: string; raw: string }[] = [];\n let match;\n\n while ((match = codeBlockRegex.exec(content)) !== null) {\n blocks.push({\n language: match[1] ?? 'text',\n code: match[2] ?? '',\n raw: match[0],\n });\n }\n\n return blocks;\n}\n\n/**\n * Render message content with code blocks\n */\nfunction MessageContent({ content }: { content: string }) {\n const codeBlocks = extractCodeBlocks(content);\n\n if (codeBlocks.length === 0) {\n return <p className=\"whitespace-pre-wrap\">{content}</p>;\n }\n\n // Split content by code blocks and render\n let remaining = content;\n const parts: React.ReactNode[] = [];\n let key = 0;\n\n for (const block of codeBlocks) {\n const [before, after] = remaining.split(block.raw);\n if (before) {\n parts.push(\n <p key={key++} className=\"whitespace-pre-wrap\">\n {before.trim()}\n </p>\n );\n }\n parts.push(\n <CodePreview\n key={key++}\n code={block.code}\n language={block.language}\n className=\"my-2\"\n />\n );\n remaining = after ?? '';\n }\n\n if (remaining.trim()) {\n parts.push(\n <p key={key++} className=\"whitespace-pre-wrap\">\n {remaining.trim()}\n </p>\n );\n }\n\n return <>{parts}</>;\n}\n\n/**\n * Chat message component\n */\nexport function ChatMessage({\n message,\n className,\n showCopy = true,\n showAvatar = true,\n}: ChatMessageProps) {\n const [copied, setCopied] = React.useState(false);\n\n const isUser = message.role === 'user';\n const isError = message.status === 'error';\n const isStreaming = message.status === 'streaming';\n\n const handleCopy = React.useCallback(async () => {\n await navigator.clipboard.writeText(message.content);\n setCopied(true);\n setTimeout(() => setCopied(false), 2000);\n }, [message.content]);\n\n return (\n <div\n className={cn(\n 'group flex gap-3',\n isUser && 'flex-row-reverse',\n className\n )}\n >\n {showAvatar && (\n <Avatar className=\"h-8 w-8 shrink-0\">\n <AvatarFallback\n className={cn(\n isUser ? 'bg-primary text-primary-foreground' : 'bg-muted'\n )}\n >\n {isUser ? (\n <User className=\"h-4 w-4\" />\n ) : (\n <Bot className=\"h-4 w-4\" />\n )}\n </AvatarFallback>\n </Avatar>\n )}\n\n <div\n className={cn('flex max-w-[80%] flex-col gap-1', isUser && 'items-end')}\n >\n <div\n className={cn(\n 'rounded-2xl px-4 py-2',\n isUser\n ? 'bg-primary text-primary-foreground'\n : 'bg-muted text-foreground',\n isError && 'border-destructive bg-destructive/10 border'\n )}\n >\n {isError && message.error ? (\n <div className=\"flex items-start gap-2\">\n <AlertCircle className=\"text-destructive mt-0.5 h-4 w-4 shrink-0\" />\n <div>\n <p className=\"text-destructive font-medium\">\n {message.error.code}\n </p>\n <p className=\"text-muted-foreground text-sm\">\n {message.error.message}\n </p>\n </div>\n </div>\n ) : isStreaming && !message.content ? (\n <div className=\"flex flex-col gap-2\">\n <Skeleton className=\"h-4 w-48\" />\n <Skeleton className=\"h-4 w-32\" />\n </div>\n ) : (\n <MessageContent content={message.content} />\n )}\n </div>\n\n {/* Message meta */}\n <div\n className={cn(\n 'flex items-center gap-2 text-xs',\n 'text-muted-foreground opacity-0 transition-opacity',\n 'group-hover:opacity-100'\n )}\n >\n <span>\n {new Date(message.createdAt).toLocaleTimeString([], {\n hour: '2-digit',\n minute: '2-digit',\n })}\n </span>\n\n {message.usage && (\n <span>\n {message.usage.inputTokens + message.usage.outputTokens} tokens\n </span>\n )}\n\n {showCopy && !isUser && message.content && (\n <Button\n variant=\"ghost\"\n size=\"sm\"\n className=\"h-6 w-6 p-0\"\n onPress={handleCopy}\n aria-label={copied ? 'Copied' : 'Copy message'}\n >\n {copied ? (\n <Check className=\"h-3 w-3\" />\n ) : (\n <Copy className=\"h-3 w-3\" />\n )}\n </Button>\n )}\n </div>\n\n {/* Reasoning (for models that support it) */}\n {message.reasoning && (\n <details className=\"text-muted-foreground mt-2 text-sm\">\n <summary className=\"cursor-pointer hover:underline\">\n View reasoning\n </summary>\n <div className=\"bg-muted mt-1 rounded-md p-2\">\n <p className=\"whitespace-pre-wrap\">{message.reasoning}</p>\n </div>\n </details>\n )}\n </div>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAuBA,SAAS,kBACP,SACmD;CACnD,MAAM,iBAAiB;CACvB,MAAM,SAA4D,EAAE;CACpE,IAAI;AAEJ,SAAQ,QAAQ,eAAe,KAAK,QAAQ,MAAM,KAChD,QAAO,KAAK;EACV,UAAU,MAAM,MAAM;EACtB,MAAM,MAAM,MAAM;EAClB,KAAK,MAAM;EACZ,CAAC;AAGJ,QAAO;;;;;AAMT,SAAS,eAAe,EAAE,WAAgC;CACxD,MAAM,aAAa,kBAAkB,QAAQ;AAE7C,KAAI,WAAW,WAAW,EACxB,QAAO,oBAAC;EAAE,WAAU;YAAuB;GAAY;CAIzD,IAAI,YAAY;CAChB,MAAM,QAA2B,EAAE;CACnC,IAAI,MAAM;AAEV,MAAK,MAAM,SAAS,YAAY;EAC9B,MAAM,CAAC,QAAQ,SAAS,UAAU,MAAM,MAAM,IAAI;AAClD,MAAI,OACF,OAAM,KACJ,oBAAC;GAAc,WAAU;aACtB,OAAO,MAAM;KADR,MAEJ,CACL;AAEH,QAAM,KACJ,oBAAC;GAEC,MAAM,MAAM;GACZ,UAAU,MAAM;GAChB,WAAU;KAHL,MAIL,CACH;AACD,cAAY,SAAS;;AAGvB,KAAI,UAAU,MAAM,CAClB,OAAM,KACJ,oBAAC;EAAc,WAAU;YACtB,UAAU,MAAM;IADX,MAEJ,CACL;AAGH,QAAO,0CAAG,QAAS;;;;;AAMrB,SAAgB,YAAY,EAC1B,SACA,WACA,WAAW,MACX,aAAa,QACM;CACnB,MAAM,CAAC,QAAQ,aAAa,MAAM,SAAS,MAAM;CAEjD,MAAM,SAAS,QAAQ,SAAS;CAChC,MAAM,UAAU,QAAQ,WAAW;CACnC,MAAM,cAAc,QAAQ,WAAW;CAEvC,MAAM,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,WAAU,YAAY,GAE7B,oBAAC,QAAK,WAAU,YAAY;QAEvB;;MAEP;IAGL,QAAQ,aACP,qBAAC;KAAQ,WAAU;gBACjB,oBAAC;MAAQ,WAAU;gBAAiC;OAE1C,EACV,oBAAC;MAAI,WAAU;gBACb,oBAAC;OAAE,WAAU;iBAAuB,QAAQ;QAAc;OACtD;MACE;;IAER;GACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodePreview.js","names":[
|
|
1
|
+
{"version":3,"file":"CodePreview.js","names":[],"sources":["../../../src/presentation/components/CodePreview.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { cn } from '@contractspec/lib.ui-kit-web/ui/utils';\nimport { Button } from '@contractspec/lib.design-system';\nimport { Copy, Check, Play, Download } from 'lucide-react';\n\nexport interface CodePreviewProps {\n /** Code content */\n code: string;\n /** Programming language */\n language?: string;\n /** File name */\n filename?: string;\n /** Additional class name */\n className?: string;\n /** Show copy button */\n showCopy?: boolean;\n /** Show execute button (for applicable languages) */\n showExecute?: boolean;\n /** Called when execute is clicked */\n onExecute?: (code: string) => void;\n /** Show download button */\n showDownload?: boolean;\n /** Max height before scroll */\n maxHeight?: number;\n}\n\n/**\n * Language display names\n */\nconst LANGUAGE_NAMES: Record<string, string> = {\n ts: 'TypeScript',\n tsx: 'TypeScript (React)',\n typescript: 'TypeScript',\n js: 'JavaScript',\n jsx: 'JavaScript (React)',\n javascript: 'JavaScript',\n json: 'JSON',\n md: 'Markdown',\n yaml: 'YAML',\n yml: 'YAML',\n bash: 'Bash',\n sh: 'Shell',\n sql: 'SQL',\n py: 'Python',\n python: 'Python',\n go: 'Go',\n rust: 'Rust',\n rs: 'Rust',\n};\n\n/**\n * Code preview component with syntax highlighting placeholder\n */\nexport function CodePreview({\n code,\n language = 'text',\n filename,\n className,\n showCopy = true,\n showExecute = false,\n onExecute,\n showDownload = false,\n maxHeight = 400,\n}: CodePreviewProps) {\n const [copied, setCopied] = React.useState(false);\n\n const displayLanguage = LANGUAGE_NAMES[language.toLowerCase()] ?? language;\n const lines = code.split('\\n');\n\n const handleCopy = React.useCallback(async () => {\n await navigator.clipboard.writeText(code);\n setCopied(true);\n setTimeout(() => setCopied(false), 2000);\n }, [code]);\n\n const handleDownload = React.useCallback(() => {\n const blob = new Blob([code], { type: 'text/plain' });\n const url = URL.createObjectURL(blob);\n const a = document.createElement('a');\n a.href = url;\n a.download = filename ?? `code.${language}`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n URL.revokeObjectURL(url);\n }, [code, filename, language]);\n\n return (\n <div\n className={cn(\n 'overflow-hidden rounded-lg border',\n 'bg-muted/50',\n className\n )}\n >\n {/* Header */}\n <div\n className={cn(\n 'flex items-center justify-between px-3 py-1.5',\n 'bg-muted/80 border-b'\n )}\n >\n <div className=\"flex items-center gap-2 text-sm\">\n {filename && (\n <span className=\"text-foreground font-mono\">{filename}</span>\n )}\n <span className=\"text-muted-foreground\">{displayLanguage}</span>\n </div>\n\n <div className=\"flex items-center gap-1\">\n {showExecute && onExecute && (\n <Button\n variant=\"ghost\"\n size=\"sm\"\n onPress={() => onExecute(code)}\n className=\"h-7 w-7 p-0\"\n aria-label=\"Execute code\"\n >\n <Play className=\"h-3.5 w-3.5\" />\n </Button>\n )}\n\n {showDownload && (\n <Button\n variant=\"ghost\"\n size=\"sm\"\n onPress={handleDownload}\n className=\"h-7 w-7 p-0\"\n aria-label=\"Download code\"\n >\n <Download className=\"h-3.5 w-3.5\" />\n </Button>\n )}\n\n {showCopy && (\n <Button\n variant=\"ghost\"\n size=\"sm\"\n onPress={handleCopy}\n className=\"h-7 w-7 p-0\"\n aria-label={copied ? 'Copied' : 'Copy code'}\n >\n {copied ? (\n <Check className=\"h-3.5 w-3.5 text-green-500\" />\n ) : (\n <Copy className=\"h-3.5 w-3.5\" />\n )}\n </Button>\n )}\n </div>\n </div>\n\n {/* Code content */}\n <div className=\"overflow-auto\" style={{ maxHeight }}>\n <pre className=\"p-3\">\n <code className=\"text-sm\">\n {lines.map((line, i) => (\n <div key={i} className=\"flex\">\n <span className=\"text-muted-foreground mr-4 w-8 text-right select-none\">\n {i + 1}\n </span>\n <span className=\"flex-1\">{line || ' '}</span>\n </div>\n ))}\n </code>\n </pre>\n </div>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;AA+BA,MAAM,iBAAyC;CAC7C,IAAI;CACJ,KAAK;CACL,YAAY;CACZ,IAAI;CACJ,KAAK;CACL,YAAY;CACZ,MAAM;CACN,IAAI;CACJ,MAAM;CACN,KAAK;CACL,MAAM;CACN,IAAI;CACJ,KAAK;CACL,IAAI;CACJ,QAAQ;CACR,IAAI;CACJ,MAAM;CACN,IAAI;CACL;;;;AAKD,SAAgB,YAAY,EAC1B,MACA,WAAW,QACX,UACA,WACA,WAAW,MACX,cAAc,OACd,WACA,eAAe,OACf,YAAY,OACO;CACnB,MAAM,CAAC,QAAQ,aAAa,MAAM,SAAS,MAAM;CAEjD,MAAM,kBAAkB,eAAe,SAAS,aAAa,KAAK;CAClE,MAAM,QAAQ,KAAK,MAAM,KAAK;CAE9B,MAAM,aAAa,MAAM,YAAY,YAAY;AAC/C,QAAM,UAAU,UAAU,UAAU,KAAK;AACzC,YAAU,KAAK;AACf,mBAAiB,UAAU,MAAM,EAAE,IAAK;IACvC,CAAC,KAAK,CAAC;CAEV,MAAM,iBAAiB,MAAM,kBAAkB;EAC7C,MAAM,OAAO,IAAI,KAAK,CAAC,KAAK,EAAE,EAAE,MAAM,cAAc,CAAC;EACrD,MAAM,MAAM,IAAI,gBAAgB,KAAK;EACrC,MAAM,IAAI,SAAS,cAAc,IAAI;AACrC,IAAE,OAAO;AACT,IAAE,WAAW,YAAY,QAAQ;AACjC,WAAS,KAAK,YAAY,EAAE;AAC5B,IAAE,OAAO;AACT,WAAS,KAAK,YAAY,EAAE;AAC5B,MAAI,gBAAgB,IAAI;IACvB;EAAC;EAAM;EAAU;EAAS,CAAC;AAE9B,QACE,qBAAC;EACC,WAAW,GACT,qCACA,eACA,UACD;aAGD,qBAAC;GACC,WAAW,GACT,iDACA,uBACD;cAED,qBAAC;IAAI,WAAU;eACZ,YACC,oBAAC;KAAK,WAAU;eAA6B;MAAgB,EAE/D,oBAAC;KAAK,WAAU;eAAyB;MAAuB;KAC5D,EAEN,qBAAC;IAAI,WAAU;;KACZ,eAAe,aACd,oBAAC;MACC,SAAQ;MACR,MAAK;MACL,eAAe,UAAU,KAAK;MAC9B,WAAU;MACV,cAAW;gBAEX,oBAAC,QAAK,WAAU,gBAAgB;OACzB;KAGV,gBACC,oBAAC;MACC,SAAQ;MACR,MAAK;MACL,SAAS;MACT,WAAU;MACV,cAAW;gBAEX,oBAAC,YAAS,WAAU,gBAAgB;OAC7B;KAGV,YACC,oBAAC;MACC,SAAQ;MACR,MAAK;MACL,SAAS;MACT,WAAU;MACV,cAAY,SAAS,WAAW;gBAE/B,SACC,oBAAC,SAAM,WAAU,+BAA+B,GAEhD,oBAAC,QAAK,WAAU,gBAAgB;OAE3B;;KAEP;IACF,EAGN,oBAAC;GAAI,WAAU;GAAgB,OAAO,EAAE,WAAW;aACjD,oBAAC;IAAI,WAAU;cACb,oBAAC;KAAK,WAAU;eACb,MAAM,KAAK,MAAM,MAChB,qBAAC;MAAY,WAAU;iBACrB,oBAAC;OAAK,WAAU;iBACb,IAAI;QACA,EACP,oBAAC;OAAK,WAAU;iBAAU,QAAQ;QAAW;QAJrC,EAKJ,CACN;MACG;KACH;IACF;GACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ModelPicker.js","names":["PROVIDER_ICONS: Record<ProviderName, React.ReactNode>","PROVIDER_NAMES: Record<ProviderName, string>","MODE_BADGES: Record<\n ProviderMode,\n { label: string; variant: 'default' | 'secondary' | 'outline' }\n>","models: ModelInfo[]"],"sources":["../../../src/presentation/components/ModelPicker.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { cn } from '@contractspec/lib.ui-kit-web/ui/utils';\nimport { Button } from '@contractspec/lib.design-system';\nimport {\n Select,\n SelectContent,\n SelectItem,\n SelectTrigger,\n SelectValue,\n} from '@contractspec/lib.ui-kit-web/ui/select';\nimport { Badge } from '@contractspec/lib.ui-kit-web/ui/badge';\nimport { Label } from '@contractspec/lib.ui-kit-web/ui/label';\nimport { Bot, Cloud, Cpu, Sparkles } from 'lucide-react';\nimport {\n getModelsForProvider,\n type ModelInfo,\n type ProviderMode,\n type ProviderName,\n} from '@contractspec/lib.ai-providers';\n\nexport interface ModelSelection {\n provider: ProviderName;\n model: string;\n mode: ProviderMode;\n}\n\nexport interface ModelPickerProps {\n /** Currently selected provider/model */\n value: ModelSelection;\n /** Called when selection changes */\n onChange: (value: ModelSelection) => void;\n /** Available providers (with availability info) */\n availableProviders?: {\n provider: ProviderName;\n available: boolean;\n mode: ProviderMode;\n reason?: string;\n }[];\n /** Additional class name */\n className?: string;\n /** Compact mode (smaller) */\n compact?: boolean;\n}\n\nconst PROVIDER_ICONS: Record<ProviderName, React.ReactNode> = {\n ollama: <Cpu className=\"h-4 w-4\" />,\n openai: <Bot className=\"h-4 w-4\" />,\n anthropic: <Sparkles className=\"h-4 w-4\" />,\n mistral: <Cloud className=\"h-4 w-4\" />,\n gemini: <Sparkles className=\"h-4 w-4\" />,\n};\n\nconst PROVIDER_NAMES: Record<ProviderName, string> = {\n ollama: 'Ollama (Local)',\n openai: 'OpenAI',\n anthropic: 'Anthropic',\n mistral: 'Mistral',\n gemini: 'Google Gemini',\n};\n\nconst MODE_BADGES: Record<\n ProviderMode,\n { label: string; variant: 'default' | 'secondary' | 'outline' }\n> = {\n local: { label: 'Local', variant: 'secondary' },\n byok: { label: 'BYOK', variant: 'outline' },\n managed: { label: 'Managed', variant: 'default' },\n};\n\n/**\n * Model picker component for selecting AI provider and model\n */\nexport function ModelPicker({\n value,\n onChange,\n availableProviders,\n className,\n compact = false,\n}: ModelPickerProps) {\n const providers = availableProviders ?? [\n { provider: 'ollama' as const, available: true, mode: 'local' as const },\n { provider: 'openai' as const, available: true, mode: 'byok' as const },\n { provider: 'anthropic' as const, available: true, mode: 'byok' as const },\n { provider: 'mistral' as const, available: true, mode: 'byok' as const },\n { provider: 'gemini' as const, available: true, mode: 'byok' as const },\n ];\n\n const models: ModelInfo[] = getModelsForProvider(value.provider);\n const selectedModel = models.find((m) => m.id === value.model);\n\n const handleProviderChange = React.useCallback(\n (providerName: string) => {\n const provider = providerName as ProviderName;\n const providerInfo = providers.find((p) => p.provider === provider);\n const providerModels = getModelsForProvider(provider);\n const defaultModel = providerModels[0]?.id ?? '';\n\n onChange({\n provider,\n model: defaultModel,\n mode: providerInfo?.mode ?? 'byok',\n });\n },\n [onChange, providers]\n );\n\n const handleModelChange = React.useCallback(\n (modelId: string) => {\n onChange({\n ...value,\n model: modelId,\n });\n },\n [onChange, value]\n );\n\n if (compact) {\n return (\n <div className={cn('flex items-center gap-2', className)}>\n <Select value={value.provider} onValueChange={handleProviderChange}>\n <SelectTrigger className=\"w-[140px]\">\n <SelectValue />\n </SelectTrigger>\n <SelectContent>\n {providers.map((p) => (\n <SelectItem\n key={p.provider}\n value={p.provider}\n disabled={!p.available}\n >\n <div className=\"flex items-center gap-2\">\n {PROVIDER_ICONS[p.provider]}\n <span>{PROVIDER_NAMES[p.provider]}</span>\n </div>\n </SelectItem>\n ))}\n </SelectContent>\n </Select>\n\n <Select value={value.model} onValueChange={handleModelChange}>\n <SelectTrigger className=\"w-[160px]\">\n <SelectValue />\n </SelectTrigger>\n <SelectContent>\n {models.map((m) => (\n <SelectItem key={m.id} value={m.id}>\n {m.name}\n </SelectItem>\n ))}\n </SelectContent>\n </Select>\n </div>\n );\n }\n\n return (\n <div className={cn('flex flex-col gap-3', className)}>\n {/* Provider selection */}\n <div className=\"flex flex-col gap-1.5\">\n <Label htmlFor=\"provider-selection\" className=\"text-sm font-medium\">\n Provider\n </Label>\n <div className=\"flex flex-wrap gap-2\" id=\"provider-selection\">\n {providers.map((p) => (\n <Button\n key={p.provider}\n variant={value.provider === p.provider ? 'default' : 'outline'}\n size=\"sm\"\n onPress={() => p.available && handleProviderChange(p.provider)}\n disabled={!p.available}\n className={cn(!p.available && 'opacity-50')}\n >\n {PROVIDER_ICONS[p.provider]}\n <span>{PROVIDER_NAMES[p.provider]}</span>\n <Badge variant={MODE_BADGES[p.mode].variant} className=\"ml-1\">\n {MODE_BADGES[p.mode].label}\n </Badge>\n </Button>\n ))}\n </div>\n </div>\n\n {/* Model selection */}\n <div className=\"flex flex-col gap-1.5\">\n <Label htmlFor=\"model-picker\" className=\"text-sm font-medium\">\n Model\n </Label>\n <Select\n name=\"model-picker\"\n value={value.model}\n onValueChange={handleModelChange}\n >\n <SelectTrigger>\n <SelectValue placeholder=\"Select a model\" />\n </SelectTrigger>\n <SelectContent>\n {models.map((m) => (\n <SelectItem key={m.id} value={m.id}>\n <div className=\"flex items-center gap-2\">\n <span>{m.name}</span>\n <span className=\"text-muted-foreground text-xs\">\n {Math.round(m.contextWindow / 1000)}K\n </span>\n {m.capabilities.vision && (\n <Badge variant=\"outline\" className=\"text-xs\">\n Vision\n </Badge>\n )}\n {m.capabilities.reasoning && (\n <Badge variant=\"outline\" className=\"text-xs\">\n Reasoning\n </Badge>\n )}\n </div>\n </SelectItem>\n ))}\n </SelectContent>\n </Select>\n </div>\n\n {/* Model info */}\n {selectedModel && (\n <div className=\"text-muted-foreground flex flex-wrap gap-2 text-xs\">\n <span>\n Context: {Math.round(selectedModel.contextWindow / 1000)}K tokens\n </span>\n {selectedModel.capabilities.vision && <span>• Vision</span>}\n {selectedModel.capabilities.tools && <span>• Tools</span>}\n {selectedModel.capabilities.reasoning && <span>• Reasoning</span>}\n </div>\n )}\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;;AA8CA,MAAMA,iBAAwD;CAC5D,QAAQ,oBAAC,OAAI,WAAU,YAAY;CACnC,QAAQ,oBAAC,OAAI,WAAU,YAAY;CACnC,WAAW,oBAAC,YAAS,WAAU,YAAY;CAC3C,SAAS,oBAAC,SA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C9B,qBAAC;OAAI,WAAU;;QACb,oBAAC,oBAAM,EAAE,OAAY;QACrB,qBAAC;SAAK,WAAU;oBACb,KAAK,MAAM,EAAE,gBAAgB,IAAK,EAAC;UAC/B;QACN,EAAE,aAAa,UACd,oBAAC;SAAM,SAAQ;SAAU,WAAU;mBAAU;UAErC;QAET,EAAE,aAAa,aACd,oBAAC;SAAM,SAAQ;SAAU,WAAU;mBAAU;UAErC;;QAEN;QAhBS,EAAE,GAiBN,CACb,GACY;MACT;KACL;GAGL,iBACC,qBAAC;IAAI,WAAU;;KACb,qBAAC;MAAK;MACM,KAAK,MAAM,cAAc,gBAAgB,IAAK;MAAC;SACpD;KACN,cAAc,aAAa,UAAU,oBAAC,oBAAK,aAAe;KAC1D,cAAc,aAAa,SAAS,oBAAC,oBAAK,YAAc;KACxD,cAAc,aAAa,aAAa,oBAAC,oBAAK,gBAAkB;;KAC7D;;GAEJ"}
|
|
1
|
+
{"version":3,"file":"ModelPicker.js","names":[],"sources":["../../../src/presentation/components/ModelPicker.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { cn } from '@contractspec/lib.ui-kit-web/ui/utils';\nimport { Button } from '@contractspec/lib.design-system';\nimport {\n Select,\n SelectContent,\n SelectItem,\n SelectTrigger,\n SelectValue,\n} from '@contractspec/lib.ui-kit-web/ui/select';\nimport { Badge } from '@contractspec/lib.ui-kit-web/ui/badge';\nimport { Label } from '@contractspec/lib.ui-kit-web/ui/label';\nimport { Bot, Cloud, Cpu, Sparkles } from 'lucide-react';\nimport {\n getModelsForProvider,\n type ModelInfo,\n type ProviderMode,\n type ProviderName,\n} from '@contractspec/lib.ai-providers';\n\nexport interface ModelSelection {\n provider: ProviderName;\n model: string;\n mode: ProviderMode;\n}\n\nexport interface ModelPickerProps {\n /** Currently selected provider/model */\n value: ModelSelection;\n /** Called when selection changes */\n onChange: (value: ModelSelection) => void;\n /** Available providers (with availability info) */\n availableProviders?: {\n provider: ProviderName;\n available: boolean;\n mode: ProviderMode;\n reason?: string;\n }[];\n /** Additional class name */\n className?: string;\n /** Compact mode (smaller) */\n compact?: boolean;\n}\n\nconst PROVIDER_ICONS: Record<ProviderName, React.ReactNode> = {\n ollama: <Cpu className=\"h-4 w-4\" />,\n openai: <Bot className=\"h-4 w-4\" />,\n anthropic: <Sparkles className=\"h-4 w-4\" />,\n mistral: <Cloud className=\"h-4 w-4\" />,\n gemini: <Sparkles className=\"h-4 w-4\" />,\n};\n\nconst PROVIDER_NAMES: Record<ProviderName, string> = {\n ollama: 'Ollama (Local)',\n openai: 'OpenAI',\n anthropic: 'Anthropic',\n mistral: 'Mistral',\n gemini: 'Google Gemini',\n};\n\nconst MODE_BADGES: Record<\n ProviderMode,\n { label: string; variant: 'default' | 'secondary' | 'outline' }\n> = {\n local: { label: 'Local', variant: 'secondary' },\n byok: { label: 'BYOK', variant: 'outline' },\n managed: { label: 'Managed', variant: 'default' },\n};\n\n/**\n * Model picker component for selecting AI provider and model\n */\nexport function ModelPicker({\n value,\n onChange,\n availableProviders,\n className,\n compact = false,\n}: ModelPickerProps) {\n const providers = availableProviders ?? [\n { provider: 'ollama' as const, available: true, mode: 'local' as const },\n { provider: 'openai' as const, available: true, mode: 'byok' as const },\n { provider: 'anthropic' as const, available: true, mode: 'byok' as const },\n { provider: 'mistral' as const, available: true, mode: 'byok' as const },\n { provider: 'gemini' as const, available: true, mode: 'byok' as const },\n ];\n\n const models: ModelInfo[] = getModelsForProvider(value.provider);\n const selectedModel = models.find((m) => m.id === value.model);\n\n const handleProviderChange = React.useCallback(\n (providerName: string) => {\n const provider = providerName as ProviderName;\n const providerInfo = providers.find((p) => p.provider === provider);\n const providerModels = getModelsForProvider(provider);\n const defaultModel = providerModels[0]?.id ?? '';\n\n onChange({\n provider,\n model: defaultModel,\n mode: providerInfo?.mode ?? 'byok',\n });\n },\n [onChange, providers]\n );\n\n const handleModelChange = React.useCallback(\n (modelId: string) => {\n onChange({\n ...value,\n model: modelId,\n });\n },\n [onChange, value]\n );\n\n if (compact) {\n return (\n <div className={cn('flex items-center gap-2', className)}>\n <Select value={value.provider} onValueChange={handleProviderChange}>\n <SelectTrigger className=\"w-[140px]\">\n <SelectValue />\n </SelectTrigger>\n <SelectContent>\n {providers.map((p) => (\n <SelectItem\n key={p.provider}\n value={p.provider}\n disabled={!p.available}\n >\n <div className=\"flex items-center gap-2\">\n {PROVIDER_ICONS[p.provider]}\n <span>{PROVIDER_NAMES[p.provider]}</span>\n </div>\n </SelectItem>\n ))}\n </SelectContent>\n </Select>\n\n <Select value={value.model} onValueChange={handleModelChange}>\n <SelectTrigger className=\"w-[160px]\">\n <SelectValue />\n </SelectTrigger>\n <SelectContent>\n {models.map((m) => (\n <SelectItem key={m.id} value={m.id}>\n {m.name}\n </SelectItem>\n ))}\n </SelectContent>\n </Select>\n </div>\n );\n }\n\n return (\n <div className={cn('flex flex-col gap-3', className)}>\n {/* Provider selection */}\n <div className=\"flex flex-col gap-1.5\">\n <Label htmlFor=\"provider-selection\" className=\"text-sm font-medium\">\n Provider\n </Label>\n <div className=\"flex flex-wrap gap-2\" id=\"provider-selection\">\n {providers.map((p) => (\n <Button\n key={p.provider}\n variant={value.provider === p.provider ? 'default' : 'outline'}\n size=\"sm\"\n onPress={() => p.available && handleProviderChange(p.provider)}\n disabled={!p.available}\n className={cn(!p.available && 'opacity-50')}\n >\n {PROVIDER_ICONS[p.provider]}\n <span>{PROVIDER_NAMES[p.provider]}</span>\n <Badge variant={MODE_BADGES[p.mode].variant} className=\"ml-1\">\n {MODE_BADGES[p.mode].label}\n </Badge>\n </Button>\n ))}\n </div>\n </div>\n\n {/* Model selection */}\n <div className=\"flex flex-col gap-1.5\">\n <Label htmlFor=\"model-picker\" className=\"text-sm font-medium\">\n Model\n </Label>\n <Select\n name=\"model-picker\"\n value={value.model}\n onValueChange={handleModelChange}\n >\n <SelectTrigger>\n <SelectValue placeholder=\"Select a model\" />\n </SelectTrigger>\n <SelectContent>\n {models.map((m) => (\n <SelectItem key={m.id} value={m.id}>\n <div className=\"flex items-center gap-2\">\n <span>{m.name}</span>\n <span className=\"text-muted-foreground text-xs\">\n {Math.round(m.contextWindow / 1000)}K\n </span>\n {m.capabilities.vision && (\n <Badge variant=\"outline\" className=\"text-xs\">\n Vision\n </Badge>\n )}\n {m.capabilities.reasoning && (\n <Badge variant=\"outline\" className=\"text-xs\">\n Reasoning\n </Badge>\n )}\n </div>\n </SelectItem>\n ))}\n </SelectContent>\n </Select>\n </div>\n\n {/* Model info */}\n {selectedModel && (\n <div className=\"text-muted-foreground flex flex-wrap gap-2 text-xs\">\n <span>\n Context: {Math.round(selectedModel.contextWindow / 1000)}K tokens\n </span>\n {selectedModel.capabilities.vision && <span>• Vision</span>}\n {selectedModel.capabilities.tools && <span>• Tools</span>}\n {selectedModel.capabilities.reasoning && <span>• Reasoning</span>}\n </div>\n )}\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;;AA8CA,MAAM,iBAAwD;CAC5D,QAAQ,oBAAC,OAAI,WAAU,YAAY;CACnC,QAAQ,oBAAC,OAAI,WAAU,YAAY;CACnC,WAAW,oBAAC,YAAS,WAAU,YAAY;CAC3C,SAAS,oBAAC,SAAM,WAAU,YAAY;CACtC,QAAQ,oBAAC,YAAS,WAAU,YAAY;CACzC;AAED,MAAM,iBAA+C;CACnD,QAAQ;CACR,QAAQ;CACR,WAAW;CACX,SAAS;CACT,QAAQ;CACT;AAED,MAAM,cAGF;CACF,OAAO;EAAE,OAAO;EAAS,SAAS;EAAa;CAC/C,MAAM;EAAE,OAAO;EAAQ,SAAS;EAAW;CAC3C,SAAS;EAAE,OAAO;EAAW,SAAS;EAAW;CAClD;;;;AAKD,SAAgB,YAAY,EAC1B,OACA,UACA,oBACA,WACA,UAAU,SACS;CACnB,MAAM,YAAY,sBAAsB;EACtC;GAAE,UAAU;GAAmB,WAAW;GAAM,MAAM;GAAkB;EACxE;GAAE,UAAU;GAAmB,WAAW;GAAM,MAAM;GAAiB;EACvE;GAAE,UAAU;GAAsB,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Ac,aAAa,aAAa,oBAAC,oBAAK,gBAAkB;;KAC7D;;GAEJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useChat.js","names":["userMessage: ChatMessage","assistantMessage: ChatMessage","error"],"sources":["../../../src/presentation/hooks/useChat.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport type {\n ChatAttachment,\n ChatConversation,\n ChatMessage,\n} from '../../core/message-types';\nimport { ChatService } from '../../core/chat-service';\nimport {\n createProvider,\n type ProviderMode,\n type ProviderName,\n} from '@contractspec/lib.ai-providers';\n\n/**\n * Options for useChat hook\n */\nexport interface UseChatOptions {\n /** Provider to use */\n provider?: ProviderName;\n /** Provider mode */\n mode?: ProviderMode;\n /** Model to use */\n model?: string;\n /** API key for BYOK mode */\n apiKey?: string;\n /** API proxy URL for managed mode */\n proxyUrl?: string;\n /** Initial conversation ID to resume */\n conversationId?: string;\n /** System prompt override */\n systemPrompt?: string;\n /** Enable streaming */\n streaming?: boolean;\n /** Called when a message is sent */\n onSend?: (message: ChatMessage) => void;\n /** Called when a response is received */\n onResponse?: (message: ChatMessage) => void;\n /** Called on error */\n onError?: (error: Error) => void;\n /** Called when usage is recorded */\n onUsage?: (usage: { inputTokens: number; outputTokens: number }) => void;\n}\n\n/**\n * Return type for useChat hook\n */\nexport interface UseChatReturn {\n /** Current messages */\n messages: ChatMessage[];\n /** Current conversation */\n conversation: ChatConversation | null;\n /** Whether currently loading/streaming */\n isLoading: boolean;\n /** Current error */\n error: Error | null;\n /** Send a message */\n sendMessage: (\n content: string,\n attachments?: ChatAttachment[]\n ) => Promise<void>;\n /** Clear conversation and start fresh */\n clearConversation: () => void;\n /** Set conversation ID to resume */\n setConversationId: (id: string | null) => void;\n /** Regenerate last response */\n regenerate: () => Promise<void>;\n /** Stop current generation */\n stop: () => void;\n}\n\n/**\n * Hook for managing AI chat state\n */\nexport function useChat(options: UseChatOptions = {}): UseChatReturn {\n const {\n provider = 'openai',\n mode = 'byok',\n model,\n apiKey,\n proxyUrl,\n conversationId: initialConversationId,\n systemPrompt,\n streaming = true,\n onSend,\n onResponse,\n onError,\n onUsage,\n } = options;\n\n const [messages, setMessages] = React.useState<ChatMessage[]>([]);\n const [conversation, setConversation] =\n React.useState<ChatConversation | null>(null);\n const [isLoading, setIsLoading] = React.useState(false);\n const [error, setError] = React.useState<Error | null>(null);\n const [conversationId, setConversationId] = React.useState<string | null>(\n initialConversationId ?? null\n );\n\n const abortControllerRef = React.useRef<AbortController | null>(null);\n const chatServiceRef = React.useRef<ChatService | null>(null);\n\n // Initialize chat service\n React.useEffect(() => {\n const chatProvider = createProvider({\n provider,\n model,\n apiKey,\n proxyUrl,\n });\n\n chatServiceRef.current = new ChatService({\n provider: chatProvider,\n systemPrompt,\n onUsage,\n });\n }, [provider, mode, model, apiKey, proxyUrl, systemPrompt, onUsage]);\n\n // Load existing conversation\n React.useEffect(() => {\n if (!conversationId || !chatServiceRef.current) return;\n\n const loadConversation = async () => {\n if (!chatServiceRef.current) return;\n\n const conv = await chatServiceRef.current.getConversation(conversationId);\n if (conv) {\n setConversation(conv);\n setMessages(conv.messages);\n }\n };\n\n loadConversation().catch(console.error);\n }, [conversationId]);\n\n const sendMessage = React.useCallback(\n async (content: string, attachments?: ChatAttachment[]) => {\n if (!chatServiceRef.current) {\n throw new Error('Chat service not initialized');\n }\n\n setIsLoading(true);\n setError(null);\n\n // Create abort controller\n abortControllerRef.current = new AbortController();\n\n try {\n // Add user message immediately\n const userMessage: ChatMessage = {\n id: `msg_${Date.now()}`,\n conversationId: conversationId ?? '',\n role: 'user',\n content,\n status: 'completed',\n createdAt: new Date(),\n updatedAt: new Date(),\n attachments,\n };\n setMessages((prev) => [...prev, userMessage]);\n onSend?.(userMessage);\n\n if (streaming) {\n // Streaming mode\n const result = await chatServiceRef.current.stream({\n conversationId: conversationId ?? undefined,\n content,\n attachments,\n });\n\n // Update conversation ID if new\n if (!conversationId) {\n setConversationId(result.conversationId);\n }\n\n // Add placeholder for assistant message\n const assistantMessage: ChatMessage = {\n id: result.messageId,\n conversationId: result.conversationId,\n role: 'assistant',\n content: '',\n status: 'streaming',\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n setMessages((prev) => [...prev, assistantMessage]);\n\n // Process stream\n let fullContent = '';\n for await (const chunk of result.stream) {\n if (chunk.type === 'text' && chunk.content) {\n fullContent += chunk.content;\n setMessages((prev) =>\n prev.map((m) =>\n m.id === result.messageId ? { ...m, content: fullContent } : m\n )\n );\n } else if (chunk.type === 'done') {\n setMessages((prev) =>\n prev.map((m) =>\n m.id === result.messageId\n ? {\n ...m,\n status: 'completed',\n usage: chunk.usage,\n updatedAt: new Date(),\n }\n : m\n )\n );\n onResponse?.(\n messages.find((m) => m.id === result.messageId) ??\n assistantMessage\n );\n } else if (chunk.type === 'error') {\n setMessages((prev) =>\n prev.map((m) =>\n m.id === result.messageId\n ? {\n ...m,\n status: 'error',\n error: chunk.error,\n updatedAt: new Date(),\n }\n : m\n )\n );\n if (chunk.error) {\n const err = new Error(chunk.error.message);\n setError(err);\n onError?.(err);\n }\n }\n }\n } else {\n // Non-streaming mode\n const result = await chatServiceRef.current.send({\n conversationId: conversationId ?? undefined,\n content,\n attachments,\n });\n\n setConversation(result.conversation);\n setMessages(result.conversation.messages);\n\n if (!conversationId) {\n setConversationId(result.conversation.id);\n }\n\n onResponse?.(result.message);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n setError(error);\n onError?.(error);\n } finally {\n setIsLoading(false);\n abortControllerRef.current = null;\n }\n },\n [conversationId, streaming, onSend, onResponse, onError, messages]\n );\n\n const clearConversation = React.useCallback(() => {\n setMessages([]);\n setConversation(null);\n setConversationId(null);\n setError(null);\n }, []);\n\n const regenerate = React.useCallback(async () => {\n // Find the last user message\n const lastUserMessageIndex = messages.findLastIndex(\n (m) => m.role === 'user'\n );\n if (lastUserMessageIndex === -1) return;\n\n const lastUserMessage = messages[lastUserMessageIndex];\n if (!lastUserMessage) return;\n\n // Remove the last assistant message\n setMessages((prev) => prev.slice(0, lastUserMessageIndex + 1));\n\n // Resend\n await sendMessage(lastUserMessage.content, lastUserMessage.attachments);\n }, [messages, sendMessage]);\n\n const stop = React.useCallback(() => {\n abortControllerRef.current?.abort();\n setIsLoading(false);\n }, []);\n\n return {\n messages,\n conversation,\n isLoading,\n error,\n sendMessage,\n clearConversation,\n setConversationId,\n regenerate,\n stop,\n };\n}\n"],"mappings":";;;;;;;;;;AA2EA,SAAgB,QAAQ,UAA0B,EAAE,EAAiB;CACnE,MAAM,EACJ,WAAW,UACX,OAAO,QACP,OACA,QACA,UACA,gBAAgB,uBAChB,cACA,YAAY,MACZ,QACA,YACA,SACA,YACE;CAEJ,MAAM,CAAC,UAAU,eAAe,MAAM,SAAwB,EAAE,CAAC;CACjE,MAAM,CAAC,cAAc,mBACnB,MAAM,SAAkC,KAAK;CAC/C,MAAM,CAAC,WAAW,gBAAgB,MAAM,SAAS,MAAM;CACvD,MAAM,CAAC,OAAO,YAAY,MAAM,SAAuB,KAAK;CAC5D,MAAM,CAAC,gBAAgB,qBAAqB,MAAM,SAChD,yBAAyB,KAC1B;CAED,MAAM,qBAAqB,MAAM,OAA+B,KAAK;CACrE,MAAM,iBAAiB,MAAM,OAA2B,KAAK;AAG7D,OAAM,gBAAgB;AAQpB,iBAAe,UAAU,IAAI,YAAY;GACvC,UARmB,eAAe;IAClC;IACA;IACA;IACA;IACD,CAAC;GAIA;GACA;GACD,CAAC;IACD;EAAC;EAAU;EAAM;EAAO;EAAQ;EAAU;EAAc;EAAQ,CAAC;AAGpE,OAAM,gBAAgB;AACpB,MAAI,CAAC,kBAAkB,CAAC,eAAe,QAAS;EAEhD,MAAM,mBAAmB,YAAY;AACnC,OAAI,CAAC,eAAe,QAAS;GAE7B,MAAM,OAAO,MAAM,eAAe,QAAQ,gBAAgB,eAAe;AACzE,OAAI,MAAM;AACR,oBAAgB,KAAK;AACrB,gBAAY,KAAK,SAAS;;;AAI9B,oBAAkB,CAAC,MAA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AU,YAAY,CAAC;EAgBzB,MAdW,MAAM,kBAAkB;AACnC,sBAAmB,SAAS,OAAO;AACnC,gBAAa,MAAM;KAClB,EAAE,CAAC;EAYL"}
|
|
1
|
+
{"version":3,"file":"useChat.js","names":["error"],"sources":["../../../src/presentation/hooks/useChat.tsx"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport type {\n ChatAttachment,\n ChatConversation,\n ChatMessage,\n} from '../../core/message-types';\nimport { ChatService } from '../../core/chat-service';\nimport {\n createProvider,\n type ProviderMode,\n type ProviderName,\n} from '@contractspec/lib.ai-providers';\n\n/**\n * Options for useChat hook\n */\nexport interface UseChatOptions {\n /** Provider to use */\n provider?: ProviderName;\n /** Provider mode */\n mode?: ProviderMode;\n /** Model to use */\n model?: string;\n /** API key for BYOK mode */\n apiKey?: string;\n /** API proxy URL for managed mode */\n proxyUrl?: string;\n /** Initial conversation ID to resume */\n conversationId?: string;\n /** System prompt override */\n systemPrompt?: string;\n /** Enable streaming */\n streaming?: boolean;\n /** Called when a message is sent */\n onSend?: (message: ChatMessage) => void;\n /** Called when a response is received */\n onResponse?: (message: ChatMessage) => void;\n /** Called on error */\n onError?: (error: Error) => void;\n /** Called when usage is recorded */\n onUsage?: (usage: { inputTokens: number; outputTokens: number }) => void;\n}\n\n/**\n * Return type for useChat hook\n */\nexport interface UseChatReturn {\n /** Current messages */\n messages: ChatMessage[];\n /** Current conversation */\n conversation: ChatConversation | null;\n /** Whether currently loading/streaming */\n isLoading: boolean;\n /** Current error */\n error: Error | null;\n /** Send a message */\n sendMessage: (\n content: string,\n attachments?: ChatAttachment[]\n ) => Promise<void>;\n /** Clear conversation and start fresh */\n clearConversation: () => void;\n /** Set conversation ID to resume */\n setConversationId: (id: string | null) => void;\n /** Regenerate last response */\n regenerate: () => Promise<void>;\n /** Stop current generation */\n stop: () => void;\n}\n\n/**\n * Hook for managing AI chat state\n */\nexport function useChat(options: UseChatOptions = {}): UseChatReturn {\n const {\n provider = 'openai',\n mode = 'byok',\n model,\n apiKey,\n proxyUrl,\n conversationId: initialConversationId,\n systemPrompt,\n streaming = true,\n onSend,\n onResponse,\n onError,\n onUsage,\n } = options;\n\n const [messages, setMessages] = React.useState<ChatMessage[]>([]);\n const [conversation, setConversation] =\n React.useState<ChatConversation | null>(null);\n const [isLoading, setIsLoading] = React.useState(false);\n const [error, setError] = React.useState<Error | null>(null);\n const [conversationId, setConversationId] = React.useState<string | null>(\n initialConversationId ?? null\n );\n\n const abortControllerRef = React.useRef<AbortController | null>(null);\n const chatServiceRef = React.useRef<ChatService | null>(null);\n\n // Initialize chat service\n React.useEffect(() => {\n const chatProvider = createProvider({\n provider,\n model,\n apiKey,\n proxyUrl,\n });\n\n chatServiceRef.current = new ChatService({\n provider: chatProvider,\n systemPrompt,\n onUsage,\n });\n }, [provider, mode, model, apiKey, proxyUrl, systemPrompt, onUsage]);\n\n // Load existing conversation\n React.useEffect(() => {\n if (!conversationId || !chatServiceRef.current) return;\n\n const loadConversation = async () => {\n if (!chatServiceRef.current) return;\n\n const conv = await chatServiceRef.current.getConversation(conversationId);\n if (conv) {\n setConversation(conv);\n setMessages(conv.messages);\n }\n };\n\n loadConversation().catch(console.error);\n }, [conversationId]);\n\n const sendMessage = React.useCallback(\n async (content: string, attachments?: ChatAttachment[]) => {\n if (!chatServiceRef.current) {\n throw new Error('Chat service not initialized');\n }\n\n setIsLoading(true);\n setError(null);\n\n // Create abort controller\n abortControllerRef.current = new AbortController();\n\n try {\n // Add user message immediately\n const userMessage: ChatMessage = {\n id: `msg_${Date.now()}`,\n conversationId: conversationId ?? '',\n role: 'user',\n content,\n status: 'completed',\n createdAt: new Date(),\n updatedAt: new Date(),\n attachments,\n };\n setMessages((prev) => [...prev, userMessage]);\n onSend?.(userMessage);\n\n if (streaming) {\n // Streaming mode\n const result = await chatServiceRef.current.stream({\n conversationId: conversationId ?? undefined,\n content,\n attachments,\n });\n\n // Update conversation ID if new\n if (!conversationId) {\n setConversationId(result.conversationId);\n }\n\n // Add placeholder for assistant message\n const assistantMessage: ChatMessage = {\n id: result.messageId,\n conversationId: result.conversationId,\n role: 'assistant',\n content: '',\n status: 'streaming',\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n setMessages((prev) => [...prev, assistantMessage]);\n\n // Process stream\n let fullContent = '';\n for await (const chunk of result.stream) {\n if (chunk.type === 'text' && chunk.content) {\n fullContent += chunk.content;\n setMessages((prev) =>\n prev.map((m) =>\n m.id === result.messageId ? { ...m, content: fullContent } : m\n )\n );\n } else if (chunk.type === 'done') {\n setMessages((prev) =>\n prev.map((m) =>\n m.id === result.messageId\n ? {\n ...m,\n status: 'completed',\n usage: chunk.usage,\n updatedAt: new Date(),\n }\n : m\n )\n );\n onResponse?.(\n messages.find((m) => m.id === result.messageId) ??\n assistantMessage\n );\n } else if (chunk.type === 'error') {\n setMessages((prev) =>\n prev.map((m) =>\n m.id === result.messageId\n ? {\n ...m,\n status: 'error',\n error: chunk.error,\n updatedAt: new Date(),\n }\n : m\n )\n );\n if (chunk.error) {\n const err = new Error(chunk.error.message);\n setError(err);\n onError?.(err);\n }\n }\n }\n } else {\n // Non-streaming mode\n const result = await chatServiceRef.current.send({\n conversationId: conversationId ?? undefined,\n content,\n attachments,\n });\n\n setConversation(result.conversation);\n setMessages(result.conversation.messages);\n\n if (!conversationId) {\n setConversationId(result.conversation.id);\n }\n\n onResponse?.(result.message);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n setError(error);\n onError?.(error);\n } finally {\n setIsLoading(false);\n abortControllerRef.current = null;\n }\n },\n [conversationId, streaming, onSend, onResponse, onError, messages]\n );\n\n const clearConversation = React.useCallback(() => {\n setMessages([]);\n setConversation(null);\n setConversationId(null);\n setError(null);\n }, []);\n\n const regenerate = React.useCallback(async () => {\n // Find the last user message\n const lastUserMessageIndex = messages.findLastIndex(\n (m) => m.role === 'user'\n );\n if (lastUserMessageIndex === -1) return;\n\n const lastUserMessage = messages[lastUserMessageIndex];\n if (!lastUserMessage) return;\n\n // Remove the last assistant message\n setMessages((prev) => prev.slice(0, lastUserMessageIndex + 1));\n\n // Resend\n await sendMessage(lastUserMessage.content, lastUserMessage.attachments);\n }, [messages, sendMessage]);\n\n const stop = React.useCallback(() => {\n abortControllerRef.current?.abort();\n setIsLoading(false);\n }, []);\n\n return {\n messages,\n conversation,\n isLoading,\n error,\n sendMessage,\n clearConversation,\n setConversationId,\n regenerate,\n stop,\n };\n}\n"],"mappings":";;;;;;;;;;AA2EA,SAAgB,QAAQ,UAA0B,EAAE,EAAiB;CACnE,MAAM,EACJ,WAAW,UACX,OAAO,QACP,OACA,QACA,UACA,gBAAgB,uBAChB,cACA,YAAY,MACZ,QACA,YACA,SACA,YACE;CAEJ,MAAM,CAAC,UAAU,eAAe,MAAM,SAAwB,EAAE,CAAC;CACjE,MAAM,CAAC,cAAc,mBACnB,MAAM,SAAkC,KAAK;CAC/C,MAAM,CAAC,WAAW,gBAAgB,MAAM,SAAS,MAAM;CACvD,MAAM,CAAC,OAAO,YAAY,MAAM,SAAuB,KAAK;CAC5D,MAAM,CAAC,gBAAgB,qBAAqB,MAAM,SAChD,yBAAyB,KAC1B;CAED,MAAM,qBAAqB,MAAM,OAA+B,KAAK;CACrE,MAAM,iBAAiB,MAAM,OAA2B,KAAK;AAG7D,OAAM,gBAAgB;AAQpB,iBAAe,UAAU,IAAI,YAAY;GACvC,UARmB,eAAe;IAClC;IACA;IACA;IACA;IACD,CAAC;GAIA;GACA;GACD,CAAC;IACD;EAAC;EAAU;EAAM;EAAO;EAAQ;EAAU;EAAc;EAAQ,CAAC;AAGpE,OAAM,gBAAgB;AACpB,MAAI,CAAC,kBAAkB,CAAC,eAAe,QAAS;EAEhD,MAAM,mBAAmB,YAAY;AACnC,OAAI,CAAC,eAAe,QAAS;GAE7B,MAAM,OAAO,MAAM,eAAe,QAAQ,gBAAgB,eAAe;AACzE,OAAI,MAAM;AACR,oBAAgB,KAAK;AACrB,gBAAY,KAAK,SAAS;;;AAI9B,oBAAkB,CAA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,UAAU,YAAY,CAAC;EAgBzB,MAdW,MAAM,kBAAkB;AACnC,sBAAmB,SAAS,OAAO;AACnC,gBAAa,MAAM;KAClB,EAAE,CAAC;EAYL"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/module.ai-chat",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.47.0",
|
|
4
4
|
"description": "AI chat interface module",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contractspec",
|
|
@@ -10,8 +10,6 @@
|
|
|
10
10
|
"typescript"
|
|
11
11
|
],
|
|
12
12
|
"type": "module",
|
|
13
|
-
"main": "./dist/index.js",
|
|
14
|
-
"module": "./dist/index.js",
|
|
15
13
|
"types": "./dist/index.d.ts",
|
|
16
14
|
"files": [
|
|
17
15
|
"dist",
|
|
@@ -31,25 +29,25 @@
|
|
|
31
29
|
"test": "bun test"
|
|
32
30
|
},
|
|
33
31
|
"dependencies": {
|
|
34
|
-
"@contractspec/lib.ai-agent": "1.
|
|
35
|
-
"@contractspec/lib.ai-providers": "1.
|
|
36
|
-
"@contractspec/lib.contracts": "1.
|
|
37
|
-
"@contractspec/lib.schema": "1.
|
|
38
|
-
"@contractspec/lib.metering": "1.
|
|
39
|
-
"@contractspec/lib.cost-tracking": "1.
|
|
40
|
-
"@contractspec/lib.design-system": "1.
|
|
41
|
-
"@contractspec/lib.ui-kit-web": "1.
|
|
42
|
-
"@ai-sdk/react": "3.0.
|
|
43
|
-
"ai": "6.0.
|
|
32
|
+
"@contractspec/lib.ai-agent": "1.47.0",
|
|
33
|
+
"@contractspec/lib.ai-providers": "1.47.0",
|
|
34
|
+
"@contractspec/lib.contracts": "1.47.0",
|
|
35
|
+
"@contractspec/lib.schema": "1.47.0",
|
|
36
|
+
"@contractspec/lib.metering": "1.47.0",
|
|
37
|
+
"@contractspec/lib.cost-tracking": "1.47.0",
|
|
38
|
+
"@contractspec/lib.design-system": "1.47.0",
|
|
39
|
+
"@contractspec/lib.ui-kit-web": "1.47.0",
|
|
40
|
+
"@ai-sdk/react": "3.0.31",
|
|
41
|
+
"ai": "6.0.29",
|
|
44
42
|
"lucide-react": "^0.562.0",
|
|
45
43
|
"react": "19.2.3",
|
|
46
|
-
"zod": "^4.
|
|
44
|
+
"zod": "^4.3.5"
|
|
47
45
|
},
|
|
48
46
|
"devDependencies": {
|
|
49
|
-
"@contractspec/tool.tsdown": "1.
|
|
50
|
-
"@contractspec/tool.typescript": "1.
|
|
51
|
-
"@types/react": "^19.
|
|
52
|
-
"tsdown": "^0.
|
|
47
|
+
"@contractspec/tool.tsdown": "1.47.0",
|
|
48
|
+
"@contractspec/tool.typescript": "1.47.0",
|
|
49
|
+
"@types/react": "^19.2.8",
|
|
50
|
+
"tsdown": "^0.19.0",
|
|
53
51
|
"typescript": "^5.9.3"
|
|
54
52
|
},
|
|
55
53
|
"peerDependencies": {
|