@contractspec/module.ai-chat 4.0.3 → 4.1.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/README.md +130 -10
- package/dist/adapters/ai-sdk-bundle-adapter.d.ts +18 -0
- package/dist/adapters/index.d.ts +4 -0
- package/dist/browser/core/index.js +1138 -21
- package/dist/browser/index.js +2816 -651
- package/dist/browser/presentation/components/index.js +3143 -358
- package/dist/browser/presentation/hooks/index.js +961 -43
- package/dist/browser/presentation/index.js +2784 -666
- package/dist/core/agent-adapter.d.ts +53 -0
- package/dist/core/agent-tools-adapter.d.ts +12 -0
- package/dist/core/chat-service.d.ts +49 -1
- package/dist/core/contracts-context.d.ts +46 -0
- package/dist/core/contracts-context.test.d.ts +1 -0
- package/dist/core/conversation-store.d.ts +16 -2
- package/dist/core/create-chat-route.d.ts +3 -0
- package/dist/core/export-formatters.d.ts +29 -0
- package/dist/core/export-formatters.test.d.ts +1 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.js +1138 -21
- package/dist/core/local-storage-conversation-store.d.ts +33 -0
- package/dist/core/message-types.d.ts +6 -0
- package/dist/core/surface-planner-tools.d.ts +23 -0
- package/dist/core/surface-planner-tools.test.d.ts +1 -0
- package/dist/core/thinking-levels.d.ts +38 -0
- package/dist/core/thinking-levels.test.d.ts +1 -0
- package/dist/core/workflow-tools.d.ts +18 -0
- package/dist/core/workflow-tools.test.d.ts +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2816 -651
- package/dist/node/core/index.js +1138 -21
- package/dist/node/index.js +2816 -651
- package/dist/node/presentation/components/index.js +3143 -358
- package/dist/node/presentation/hooks/index.js +961 -43
- package/dist/node/presentation/index.js +2787 -669
- package/dist/presentation/components/ChatContainer.d.ts +3 -1
- package/dist/presentation/components/ChatExportToolbar.d.ts +25 -0
- package/dist/presentation/components/ChatMessage.d.ts +16 -1
- package/dist/presentation/components/ChatSidebar.d.ts +26 -0
- package/dist/presentation/components/ChatWithExport.d.ts +34 -0
- package/dist/presentation/components/ChatWithSidebar.d.ts +19 -0
- package/dist/presentation/components/ThinkingLevelPicker.d.ts +16 -0
- package/dist/presentation/components/ToolResultRenderer.d.ts +33 -0
- package/dist/presentation/components/index.d.ts +6 -0
- package/dist/presentation/components/index.js +3143 -358
- package/dist/presentation/hooks/index.d.ts +2 -0
- package/dist/presentation/hooks/index.js +961 -43
- package/dist/presentation/hooks/useChat.d.ts +44 -2
- package/dist/presentation/hooks/useConversations.d.ts +18 -0
- package/dist/presentation/hooks/useMessageSelection.d.ts +13 -0
- package/dist/presentation/index.js +2787 -669
- package/package.json +14 -18
package/README.md
CHANGED
|
@@ -17,6 +17,16 @@ This module provides a reusable AI chat system that can be integrated into CLI,
|
|
|
17
17
|
- **Streaming Responses**: Real-time token streaming for responsive UX
|
|
18
18
|
- **Usage Tracking**: Integrated metering and cost tracking
|
|
19
19
|
- **UI Components**: React components for chat interfaces
|
|
20
|
+
- **Export**: Export conversations to Markdown, TXT, or JSON; select one or many messages for export
|
|
21
|
+
- **Conversation Management**: New conversation, history sidebar, fork, edit messages, organize with projects and tags
|
|
22
|
+
- **Thinking Levels**: Select reasoning depth (instant, thinking, extra thinking, max); maps to Anthropic budgetTokens and OpenAI reasoningEffort
|
|
23
|
+
- **Workflow Creation Tools**: Create and modify workflows conversationally via `create_workflow_extension`, `compose_workflow`, and `generate_workflow_spec_code` (requires `@contractspec/lib.workflow-composer`)
|
|
24
|
+
- **ModelSelector**: Dynamic model selection by task dimension (reasoning vs latency) when using `@contractspec/lib.ai-providers` ModelSelector
|
|
25
|
+
- **Contracts-Spec Context**: Expose agent, data-views, operations, forms, and presentations to the model via `contractsContext`; agent tools can be wired from `AgentToolConfig[]`
|
|
26
|
+
- **Surface-Runtime Integration**: Full support for `@contractspec/lib.surface-runtime` — pass `surfacePlanConfig` to enable `propose-patch` tool; chat can propose layout changes for user approval
|
|
27
|
+
- **Presentation/Form Rendering**: Pass `presentationRenderer` and `formRenderer` to `ChatWithSidebar`; tool results with `presentationKey` or `formKey` render via host-provided components
|
|
28
|
+
- **MCP Tools**: Pass `mcpServers` (from `@contractspec/lib.ai-agent`) to `useChat`; tools from MCP servers are merged into chat tools
|
|
29
|
+
- **Agent Mode**: Pass `agentMode: { agent }` with a `ChatAgentAdapter` (use `createChatAgentAdapter` to wrap `ContractSpecAgent`); chat uses the agent for generation instead of ChatService
|
|
20
30
|
|
|
21
31
|
## Bundle Spec Alignment (07_ai_native_chat)
|
|
22
32
|
|
|
@@ -24,9 +34,10 @@ This module aligns with `specs/contractspec_modules_bundle_spec_2026-03-08`. `us
|
|
|
24
34
|
|
|
25
35
|
## Related Packages
|
|
26
36
|
|
|
27
|
-
- `@contractspec/lib.ai-providers` — Shared provider abstraction (types, factory, validation)
|
|
28
|
-
- `@contractspec/lib.
|
|
29
|
-
- `@contractspec/lib.
|
|
37
|
+
- `@contractspec/lib.ai-providers` — Shared provider abstraction (types, factory, validation), ModelSelector for dynamic model selection
|
|
38
|
+
- `@contractspec/lib.workflow-composer` — Workflow composition and validation (optional; required for workflow creation tools)
|
|
39
|
+
- `@contractspec/lib.ai-agent` — Agent orchestration and tool execution; `AgentToolConfig` for agent tools
|
|
40
|
+
- `@contractspec/lib.surface-runtime` — Bundle surfaces, planner tools, `AiSdkBundleAdapter`; full integration when used in PM workbench
|
|
30
41
|
|
|
31
42
|
## Providers
|
|
32
43
|
|
|
@@ -78,21 +89,55 @@ const response = await chatService.send({
|
|
|
78
89
|
|
|
79
90
|
### React Components
|
|
80
91
|
|
|
92
|
+
**With export and message selection** (recommended):
|
|
93
|
+
|
|
81
94
|
```tsx
|
|
82
|
-
import {
|
|
83
|
-
import { useChat } from '@contractspec/module.ai-chat/presentation/hooks';
|
|
95
|
+
import { ChatWithExport, ChatInput, useChat } from '@contractspec/module.ai-chat';
|
|
84
96
|
|
|
85
97
|
function VibeCodingChat() {
|
|
86
|
-
const { messages, sendMessage, isLoading } = useChat({
|
|
98
|
+
const { messages, conversation, sendMessage, isLoading } = useChat({
|
|
87
99
|
provider: 'openai',
|
|
88
100
|
mode: 'managed',
|
|
89
101
|
});
|
|
90
102
|
|
|
103
|
+
return (
|
|
104
|
+
<ChatWithExport messages={messages} conversation={conversation}>
|
|
105
|
+
<ChatInput onSend={sendMessage} disabled={isLoading} />
|
|
106
|
+
</ChatWithExport>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`ChatWithExport` provides an export toolbar (Markdown, TXT, JSON, copy to clipboard) and message selection checkboxes. Select messages to export only those, or export all when none are selected.
|
|
112
|
+
|
|
113
|
+
**With sidebar (history, new, fork, edit, project/tags)**:
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
import { ChatWithSidebar } from '@contractspec/module.ai-chat';
|
|
117
|
+
|
|
118
|
+
function FullChat() {
|
|
119
|
+
return (
|
|
120
|
+
<ChatWithSidebar
|
|
121
|
+
systemPrompt="You are a helpful assistant."
|
|
122
|
+
/>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`ChatWithSidebar` includes a conversation history sidebar (LocalStorage-persisted), New/Fork buttons, message edit, project/tags organization, and a **Thinking Level** picker (instant, thinking, extra thinking, max). Uses `createLocalStorageConversationStore()` by default.
|
|
128
|
+
|
|
129
|
+
**Basic (no export)**:
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
import { ChatContainer, ChatMessage, ChatInput } from '@contractspec/module.ai-chat/presentation/components';
|
|
133
|
+
import { useChat } from '@contractspec/module.ai-chat/presentation/hooks';
|
|
134
|
+
|
|
135
|
+
function BasicChat() {
|
|
136
|
+
const { messages, sendMessage, isLoading } = useChat();
|
|
137
|
+
|
|
91
138
|
return (
|
|
92
139
|
<ChatContainer>
|
|
93
|
-
{messages.map((msg) =>
|
|
94
|
-
<ChatMessage key={msg.id} message={msg} />
|
|
95
|
-
))}
|
|
140
|
+
{messages.map((msg) => <ChatMessage key={msg.id} message={msg} />)}
|
|
96
141
|
<ChatInput onSend={sendMessage} disabled={isLoading} />
|
|
97
142
|
</ChatContainer>
|
|
98
143
|
);
|
|
@@ -104,9 +149,12 @@ function VibeCodingChat() {
|
|
|
104
149
|
This module aligns with the [Vercel AI SDK](https://sdk.vercel.ai) and AI Elements feature set:
|
|
105
150
|
|
|
106
151
|
- **fullStream**: Reasoning, tools, and sources from `streamText` fullStream
|
|
107
|
-
- **
|
|
152
|
+
- **Thinking Levels**: `thinkingLevel` option (instant, thinking, extra_thinking, max) maps to Anthropic `budgetTokens` and OpenAI `reasoningEffort`; `ThinkingLevelPicker` in `ChatWithSidebar`
|
|
153
|
+
- **Tools**: Pass `tools` to `ChatServiceConfig` or `useChat`; supports `requireApproval` for approval workflow; optional `workflowToolsConfig` adds workflow creation tools
|
|
108
154
|
- **Message parts**: `ChatMessage` renders reasoning (collapsible), sources (citations), and tool invocations
|
|
109
155
|
- **Markdown**: Inline links and code blocks in message content
|
|
156
|
+
- **Export**: `ChatWithExport` and `ChatExportToolbar` support Markdown (.md), Plain Text (.txt), and JSON (.json); select messages for partial export or export all
|
|
157
|
+
- **Conversation Management**: `ChatWithSidebar` provides history, new conversation, fork, edit messages; `useChat` exposes `createNewConversation`, `editMessage`, `forkConversation`, `updateConversation`; `useConversations` for listing; `createLocalStorageConversationStore()` for persistence
|
|
110
158
|
|
|
111
159
|
### Server Route (Full AI SDK + Tool Approval)
|
|
112
160
|
|
|
@@ -137,6 +185,50 @@ const { messages, sendMessage } = useChat({
|
|
|
137
185
|
|
|
138
186
|
The custom `useChat` from this module works with `ChatService` for simple deployments (no tools, no approval). For tools with `requireApproval`, use the server route pattern above.
|
|
139
187
|
|
|
188
|
+
### Workflow Creation Tools
|
|
189
|
+
|
|
190
|
+
When `workflowToolsConfig` is provided, the chat can create and modify workflows via AI SDK tools:
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
import { ChatService, createWorkflowTools } from '@contractspec/module.ai-chat';
|
|
194
|
+
import { WorkflowComposer } from '@contractspec/lib.workflow-composer';
|
|
195
|
+
import { createProvider } from '@contractspec/lib.ai-providers';
|
|
196
|
+
|
|
197
|
+
const baseWorkflows = [/* your WorkflowSpec[] */];
|
|
198
|
+
const provider = createProvider({ provider: 'anthropic', model: 'claude-sonnet-4' });
|
|
199
|
+
|
|
200
|
+
const chatService = new ChatService({
|
|
201
|
+
provider,
|
|
202
|
+
workflowToolsConfig: {
|
|
203
|
+
baseWorkflows,
|
|
204
|
+
composer: new WorkflowComposer(),
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// User can say: "Add a legal review step after validate-invoice"
|
|
209
|
+
// The model will call create_workflow_extension, compose_workflow, generate_workflow_spec_code
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Tools: `create_workflow_extension` (validate extensions), `compose_workflow` (apply extensions to base), `generate_workflow_spec_code` (output TypeScript). Export `createWorkflowTools(baseWorkflows, composer)` for manual wiring.
|
|
213
|
+
|
|
214
|
+
### ModelSelector (Dynamic Model Selection)
|
|
215
|
+
|
|
216
|
+
Use `modelSelector` to pick models by task dimension (e.g. reasoning vs latency):
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import { createModelSelector } from '@contractspec/lib.ai-providers';
|
|
220
|
+
import { ChatService } from '@contractspec/module.ai-chat';
|
|
221
|
+
|
|
222
|
+
const modelSelector = createModelSelector(/* ranking config */);
|
|
223
|
+
const chatService = new ChatService({
|
|
224
|
+
provider: createProvider({ /* ... */ }),
|
|
225
|
+
modelSelector,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// ChatService will call modelSelector.selectAndCreate({ taskDimension: 'reasoning' | 'latency' })
|
|
229
|
+
// based on thinking level when modelSelector is set
|
|
230
|
+
```
|
|
231
|
+
|
|
140
232
|
### Optional AI Elements
|
|
141
233
|
|
|
142
234
|
Apps can optionally use [AI Elements](https://elements.ai-sdk.dev) for UI. This module does not depend on ai-elements; provide an adapter from `ChatMessage` to `UIMessage` when integrating.
|
|
@@ -156,6 +248,34 @@ const { completion, complete, isLoading } = useCompletion({
|
|
|
156
248
|
|
|
157
249
|
Use `createCompletionRoute` for the API endpoint (see `createChatRoute` pattern).
|
|
158
250
|
|
|
251
|
+
### Contracts-Spec Context and Surface-Runtime
|
|
252
|
+
|
|
253
|
+
Pass `contractsContext` to expose agent, data-views, operations, forms, and presentations to the model. Pass `surfacePlanConfig` when embedding chat in a surface-runtime (e.g. PM workbench) to enable the `propose-patch` tool:
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
import { ChatWithSidebar } from '@contractspec/module.ai-chat';
|
|
257
|
+
import type { ResolvedSurfacePlan } from '@contractspec/lib.surface-runtime/runtime/resolve-bundle';
|
|
258
|
+
|
|
259
|
+
function PmWorkbench({ plan }: { plan: ResolvedSurfacePlan }) {
|
|
260
|
+
const [currentPlan, setPlan] = useState(plan);
|
|
261
|
+
const onPatchProposal = useCallback((proposal) => {
|
|
262
|
+
setPlan(prev => ({
|
|
263
|
+
...prev,
|
|
264
|
+
ai: { ...prev.ai, proposals: [...(prev.ai?.proposals ?? []), proposal] },
|
|
265
|
+
}));
|
|
266
|
+
}, []);
|
|
267
|
+
|
|
268
|
+
return (
|
|
269
|
+
<ChatWithSidebar
|
|
270
|
+
surfacePlanConfig={{ plan: currentPlan, onPatchProposal }}
|
|
271
|
+
systemPrompt="You are a PM workbench assistant. Propose layout changes when helpful."
|
|
272
|
+
/>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
`createAiSdkBundleAdapter` from `@contractspec/module.ai-chat/adapters` implements `AiSdkBundleAdapter` for surface-runtime planner integration.
|
|
278
|
+
|
|
159
279
|
### streamObject / generateObject
|
|
160
280
|
|
|
161
281
|
For structured output (schema-driven generation), use the AI SDK directly: `streamObject` and `generateObject` from `ai`. This module focuses on chat; add `useObject` or equivalent in a separate module when needed.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AiSdkBundleAdapter implementation using ChatService.
|
|
3
|
+
* Enables surface-runtime to drive chat for planner/requestPatches flows.
|
|
4
|
+
*/
|
|
5
|
+
import type { AiSdkBundleAdapter } from '@contractspec/lib.surface-runtime/adapters/interfaces';
|
|
6
|
+
import type { SurfacePatchProposal } from '@contractspec/lib.surface-runtime/spec/types';
|
|
7
|
+
import type { Provider as ChatProvider } from '@contractspec/lib.ai-providers';
|
|
8
|
+
export interface CreateAiSdkBundleAdapterDeps {
|
|
9
|
+
/** Provider for creating ChatService (from createProvider) */
|
|
10
|
+
provider: ChatProvider;
|
|
11
|
+
/** Called when a patch proposal is produced during requestPatches */
|
|
12
|
+
onPatchProposal?: (proposal: SurfacePatchProposal) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create an AiSdkBundleAdapter that uses ChatService for planner integration.
|
|
16
|
+
* requestPatches sends the user message to the chat and collects patch proposals from tool calls.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createAiSdkBundleAdapter(deps: CreateAiSdkBundleAdapterDeps): AiSdkBundleAdapter;
|