Function Calling & AI Tools
Relevant source files
The following files were used as context for generating this wiki page:
Function Calling and AI Tools enable the LV.AI system to interact with structured data and the user interface through executable code paths. The system uses these tools to create, modify, and analyze construction industry documents such as Leistungsverzeichnisse (LV), price mirrors (Preisspiegel), and bid evaluations.
Sources: lib/ai/prompts.ts:32-45, lib/ai/tools/create-document.ts:18-24
Tool Architecture and Execution Flow
The system integrates AI tools using a schema-first approach. Each tool defines an inputSchema using Zod to validate arguments provided by the Large Language Model (LLM). When the LLM decides to call a tool, the system executes the tool's logic and returns a result that may include UI updates or database operations.
The diagram shows the lifecycle of a tool call from the user request to the final response.
Sources: lib/ai/tools/create-document.ts:25-70, lib/ai/tools/request-suggestions.ts:26-34
Execution Rules
The artifactsPrompt establishes critical constraints for tool usage to ensure stability and user experience:
- Exclusivity: The LLM must call only ONE tool per response.
- Termination: After calling any create, edit, or update tool, the model must stop further processing.
- Conciseness: The model must not repeat artifact content in the chat; it only provides a 1-2 sentence confirmation.
Sources: lib/ai/prompts.ts:35-40
Core AI Tools
createDocument
This tool generates new artifacts in the side panel. It supports three distinct kinds: text, code, and sheet. For construction documents like LVs or cost estimates, the system requires the sheet kind to handle tabular data.
| Parameter | Type | Description |
|---|---|---|
title | string | The descriptive title of the artifact. |
kind | enum | Defines the artifact type: 'code', 'text', or 'sheet'. |
Sources: lib/ai/tools/create-document.ts:25-33, lib/ai/prompts.ts:47-51
requestSuggestions
This tool analyzes existing documents to offer improvements. It retrieves the document from the database, streams text through a specialized "writing assistant" prompt, and generates up to 5 suggestions.
This diagram illustrates the asynchronous flow of retrieving content and streaming suggestions to the client.
Sources: lib/ai/tools/request-suggestions.ts:35-108
User Interface Elements
Tool components handle the visual representation of tool states within the chat history. The system uses a Collapsible component to manage space, showing parameters and results in structured blocks.
Tool Status Indicators
The interface provides real-time feedback on the tool's execution status using specific badges and icons.
| Status | Label | Icon |
|---|---|---|
input-available | Running | ClockIcon (Animated) |
output-available | Completed | CheckCircleIcon |
output-denied | Denied | XCircleIcon |
output-error | Error | XCircleIcon |
approval-requested | Awaiting Approval | ClockIcon |
Sources: components/ai-elements/tool.tsx:43-61
Message Integration
The PreviewMessage component dynamically renders tool calls based on their type. For document tools, it uses specialized preview components to show the progress and result of artifact creation or updates.
if (type === "tool-createDocument") {
const { toolCallId } = part;
// ... error handling
return (
<DocumentPreview
isReadonly={isReadonly}
key={toolCallId}
result={part.output}
/>
);
}
Sources: components/chat/message.tsx:219-236, components/chat/document.tsx:58-100
Implementation Details
Data Streaming
Tools utilize UIMessageStreamWriter to send transient data to the client before the execution finishes. This allows the UI to update titles, document IDs, and artifact kinds immediately as they are determined.
dataStream.write({
type: "data-kind",
data: kind,
transient: true,
});
Sources: lib/ai/tools/create-document.ts:37-41
Prompt Engineering for Tools
Tool selection is heavily guided by modularized system prompts. The sheetPrompt specifically instructs the AI to use GAEB-compliant headers (OZ, Kurztext, Langtext, ME, Menge, EP, GP) and German decimal notation when creating spreadsheets for the construction industry.
Sources: lib/ai/prompts.ts:167-175
AI tools in LV.AI serve as the functional core for document management, enabling a seamless transition from conversational requests to structured construction data. This system ensures legal and technical compliance (VOB/ÖNORM/SIA) by enforcing specific output formats and workflows through tool execution and targeted prompting.