Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

feat: use ai sdk useChat

dextmorgn07f32c983a81
diff --git a/flowsint-api/alembic/versions/a1b2c3d4e5f6_add_enricher_templates_table.py b/flowsint-api/alembic/versions/a1b2c3d4e5f6_add_enricher_templates_table.py
deleted file mode 100644
index b2a0bdfa..00000000
--- a/flowsint-api/alembic/versions/a1b2c3d4e5f6_add_enricher_templates_table.py
+++ /dev/null
@@ -1,48 +0,0 @@
-"""add enricher_templates table
-
-Revision ID: a1b2c3d4e5f6
-Revises: 8173aba964e7
-Create Date: 2025-01-31
-
-"""
-from typing import Sequence, Union
-
-from alembic import op
-import sqlalchemy as sa
-from sqlalchemy.dialects import postgresql
-
-# revision identifiers, used by Alembic.
-revision: str = 'a1b2c3d4e5f6'
-down_revision: Union[str, None] = '8173aba964e7'
-branch_labels: Union[str, Sequence[str], None] = None
-depends_on: Union[str, Sequence[str], None] = None
-
-
-def upgrade() -> None:
- """Upgrade schema."""
- op.create_table('enricher_templates',
- sa.Column('id', sa.UUID(), nullable=False),
- sa.Column('name', sa.Text(), nullable=False),
- sa.Column('category', sa.Text(), nullable=False),
- sa.Column('version', sa.Float(), nullable=False, server_default='1.0'),
- sa.Column('content', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
- sa.Column('is_public', sa.Boolean(), nullable=False, server_default='false'),
- sa.Column('owner_id', sa.UUID(), nullable=False),
- sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
- sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
- sa.ForeignKeyConstraint(['owner_id'], ['profiles.id'], onupdate='CASCADE', ondelete='CASCADE'),
- sa.PrimaryKeyConstraint('id')
- )
- op.create_index('idx_enricher_templates_owner_id', 'enricher_templates', ['owner_id'], unique=False)
- op.create_index('idx_enricher_templates_name', 'enricher_templates', ['name'], unique=False)
- op.create_index('idx_enricher_templates_category', 'enricher_templates', ['category'], unique=False)
- op.create_index('idx_enricher_templates_is_public', 'enricher_templates', ['is_public'], unique=False)
-
-
-def downgrade() -> None:
- """Downgrade schema."""
- op.drop_index('idx_enricher_templates_is_public', table_name='enricher_templates')
- op.drop_index('idx_enricher_templates_category', table_name='enricher_templates')
- op.drop_index('idx_enricher_templates_name', table_name='enricher_templates')
- op.drop_index('idx_enricher_templates_owner_id', table_name='enricher_templates')
- op.drop_table('enricher_templates')
diff --git a/flowsint-api/alembic/versions/c9d8e7f6a5b4_add_enricher_templates_table.py b/flowsint-api/alembic/versions/c9d8e7f6a5b4_add_enricher_templates_table.py
new file mode 100644
index 00000000..56fc7628
--- /dev/null
+++ b/flowsint-api/alembic/versions/c9d8e7f6a5b4_add_enricher_templates_table.py
@@ -0,0 +1,80 @@
+"""add enricher_templates table
+
+Revision ID: a1b2c3d4e5f6
+Revises: 8173aba964e7
+Create Date: 2025-01-31
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+from sqlalchemy.dialects import postgresql
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "c9d8e7f6a5b4"
+down_revision: Union[str, None] = "a1b2c3d4e5f6"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ """Upgrade schema."""
+ op.create_table(
+ "enricher_templates",
+ sa.Column("id", sa.UUID(), nullable=False),
+ sa.Column("name", sa.Text(), nullable=False),
+ sa.Column("category", sa.Text(), nullable=False),
+ sa.Column("version", sa.Float(), nullable=False, server_default="1.0"),
+ sa.Column("content", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
+ sa.Column("is_public", sa.Boolean(), nullable=False, server_default="false"),
+ sa.Column("owner_id", sa.UUID(), nullable=False),
+ sa.Column(
+ "created_at",
+ sa.DateTime(timezone=True),
+ server_default=sa.text("now()"),
+ nullable=True,
+ ),
+ sa.Column(
+ "updated_at",
+ sa.DateTime(timezone=True),
+ server_default=sa.text("now()"),
+ nullable=True,
+ ),
+ sa.ForeignKeyConstraint(
+ ["owner_id"], ["profiles.id"], onupdate="CASCADE", ondelete="CASCADE"
+ ),
+ sa.PrimaryKeyConstraint("id"),
+ )
+ op.create_index(
+ "idx_enricher_templates_owner_id",
+ "enricher_templates",
+ ["owner_id"],
+ unique=False,
+ )
+ op.create_index(
+ "idx_enricher_templates_name", "enricher_templates", ["name"], unique=False
+ )
+ op.create_index(
+ "idx_enricher_templates_category",
+ "enricher_templates",
+ ["category"],
+ unique=False,
+ )
+ op.create_index(
+ "idx_enricher_templates_is_public",
+ "enricher_templates",
+ ["is_public"],
+ unique=False,
+ )
+
+
+def downgrade() -> None:
+ """Downgrade schema."""
+ op.drop_index("idx_enricher_templates_is_public", table_name="enricher_templates")
+ op.drop_index("idx_enricher_templates_category", table_name="enricher_templates")
+ op.drop_index("idx_enricher_templates_name", table_name="enricher_templates")
+ op.drop_index("idx_enricher_templates_owner_id", table_name="enricher_templates")
+ op.drop_table("enricher_templates")
diff --git a/flowsint-api/alembic/versions/f5fae279ec04_merge_portable_column_types_and_.py b/flowsint-api/alembic/versions/f5fae279ec04_merge_portable_column_types_and_.py
new file mode 100644
index 00000000..912d31f1
--- /dev/null
+++ b/flowsint-api/alembic/versions/f5fae279ec04_merge_portable_column_types_and_.py
@@ -0,0 +1,28 @@
+"""merge portable column types and enricher templates
+
+Revision ID: f5fae279ec04
+Revises: b2c3d4e5f6a7, c9d8e7f6a5b4
+Create Date: 2026-02-07 18:00:18.801891
+
+"""
+from typing import Sequence, Union
+
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision: str = 'f5fae279ec04'
+down_revision: Union[str, None] = ('b2c3d4e5f6a7', 'c9d8e7f6a5b4')
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ """Upgrade schema."""
+ pass
+
+
+def downgrade() -> None:
+ """Downgrade schema."""
+ pass
diff --git a/flowsint-api/app/api/routes/chat.py b/flowsint-api/app/api/routes/chat.py
index a0228329..c1bccdd4 100644
--- a/flowsint-api/app/api/routes/chat.py
+++ b/flowsint-api/app/api/routes/chat.py
@@ -67,6 +67,7 @@ async def stream_chat(
return StreamingResponse(
service.stream_response(chat_id, llm_messages, provider),
media_type="text/event-stream",
+ headers={"x-vercel-ai-ui-message-stream": "v1"},
)
diff --git a/flowsint-api/app/api/routes/enricher_templates.py b/flowsint-api/app/api/routes/enricher_templates.py
index cf5f9479..e06cf9a4 100644
--- a/flowsint-api/app/api/routes/enricher_templates.py
+++ b/flowsint-api/app/api/routes/enricher_templates.py
@@ -9,7 +9,9 @@ from flowsint_core.core.postgre_db import get_db
from flowsint_core.core.services import (
ConflictError,
NotFoundError,
+ ValidationError,
create_enricher_template_service,
+ create_template_generator_service,
)
from flowsint_core.core.template_enricher import TemplateEnricher
from flowsint_core.core.vault import Vault
@@ -19,6 +21,8 @@ from sqlalchemy.orm import Session
from app.api.deps import get_current_user
from app.api.schemas.enricher_template import (
EnricherTemplateCreate,
+ EnricherTemplateGenerateRequest,
+ EnricherTemplateGenerateResponse,
EnricherTemplateList,
EnricherTemplateRead,
EnricherTemplateTestRequest,
@@ -73,6 +77,21 @@ def list_templates(
return service.list_templates(current_user.id, category, include_public)
[email protected]("/generate", response_model=EnricherTemplateGenerateResponse)
+async def generate_template(
+ request: EnricherTemplateGenerateRequest,
+ db: Session = Depends(get_db),
+ current_user: Profile = Depends(get_current_user),
+):
+ """Generate an enricher template from a free-text description using AI."""
+ service = create_template_generator_service(db)
+ try:
+ yaml_content = await service.generate(request.prompt, current_user.id)
+ except ValidationError as e:
+ raise HTTPException(status_code=422, detail=str(e))
+ return EnricherTemplateGenerateResponse(yaml_content=yaml_content)
+
+
@router.post("/{template_id}/test", response_model=EnricherTemplateTestResponse)
async def test_template(
template_id: UUID,
diff --git a/flowsint-api/app/api/routes/enrichers.py b/flowsint-api/app/api/routes/enrichers.py
index 2f2b77fb..2d977a8a 100644
--- a/flowsint-api/app/api/routes/enrichers.py
+++ b/flowsint-api/app/api/routes/enrichers.py
@@ -1,9 +1,6 @@
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, Query
-from pydantic import BaseModel
-from sqlalchemy.orm import Session
-
from flowsint_core.core.celery import celery
from flowsint_core.core.graph import create_graph_service
from flowsint_core.core.models import Profile
@@ -13,6 +10,9 @@ from flowsint_core.core.services import (
create_enricher_template_service,
)
from flowsint_enrichers import ENRICHER_REGISTRY, load_all_enrichers
+from pydantic import BaseModel
+from sqlalchemy.orm import Session
+
from app.api.deps import get_current_user
load_all_enrichers()
@@ -34,17 +34,10 @@ def get_enrichers(
):
"""Get all enrichers, optionally filtered by category."""
enricher_service = create_enricher_service(db)
- base_enrichers = enricher_service.get_enrichers(
+ return enricher_service.get_all_enrichers(
category, current_user.id, ENRICHER_REGISTRY
)
- template_service = create_enricher_template_service(db)
- template_enrichers = template_service.list_by_category_for_user(
- current_user.id, category
- )
-
- return [*base_enrichers, *template_enrichers]
-
@router.post("/{enricher_name}/launch")
async def launch_enricher(
@@ -59,12 +52,15 @@ async def launch_enricher(
entities = graph_service.get_nodes_by_ids_for_task(payload.node_ids)
# Send deserialized nodes
- entities = [entity.model_dump(mode="json", serialize_as_any=True) for entity in entities]
+ entities = [
+ entity.model_dump(mode="json", serialize_as_any=True) for entity in entities
+ ]
if not entities:
raise HTTPException(
status_code=404, detail="No entities found with provided IDs"
)
+ is_template = False
enricher_in_registry = ENRICHER_REGISTRY.enricher_exists(enricher_name)
if not enricher_in_registry:
template_service = create_enricher_template_service(db)
@@ -72,12 +68,11 @@ async def launch_enricher(
if not template:
raise HTTPException(
status_code=404,
- detail=f"Enricher '{enricher_name}' not found in registry",
+ detail=f"Enricher '{enricher_name}' not found",
)
+ is_template = True
- task_name = (
- "run_enricher" if enricher_in_registry else "run_enricher_from_template"
- )
+ task_name = "run_template_enricher" if is_template else "run_enricher"
task = celery.send_task(
task_name,
args=[
diff --git a/flowsint-api/app/api/schemas/enricher_template.py b/flowsint-api/app/api/schemas/enricher_template.py
index 27a8f96e..9eb585ae 100644
--- a/flowsint-api/app/api/schemas/enricher_template.py
+++ b/flowsint-api/app/api/schemas/enricher_template.py
@@ -163,3 +163,22 @@ class EnricherTemplateTestResponse(BaseModel):
error: Optional[str] = None
status_code: Optional[int] = None
url: str
+
+
+class EnricherTemplateGenerateRequest(BaseModel):
+ """Schema for AI-assisted template generation."""
+
+ prompt: str = Field(
+ ...,
+ min_length=10,
+ max_length=2000,
+ description="Free-text description of the desired enricher template",
+ )
+
+
+class EnricherTemplateGenerateResponse(BaseModel):
+ """Schema for the generated template response."""
+
+ yaml_content: str = Field(
+ ..., description="Raw YAML string of the generated template"
+ )
diff --git a/flowsint-app/package.json b/flowsint-app/package.json
index 889a618b..ddf2063f 100644
--- a/flowsint-app/package.json
+++ b/flowsint-app/package.json
@@ -16,6 +16,7 @@
"preview": "vite preview"
},
"dependencies": {
+ "@ai-sdk/react": "^3.0.79",
"@dagrejs/dagre": "^1.1.4",
"@hookform/resolvers": "^5.0.1",
"@monaco-editor/react": "^4.7.0",
@@ -77,6 +78,7 @@
"@xterm/addon-webgl": "^0.18.0",
"@xterm/xterm": "^5.5.0",
"@xyflow/react": "^12.6.4",
+ "ai": "^6.0.77",
"babel-plugin-react-compiler": "^19.1.0-rc.2",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
diff --git a/flowsint-app/src/api/chat-service.ts b/flowsint-app/src/api/chat-service.ts
index 2aaced36..449411e6 100644
--- a/flowsint-app/src/api/chat-service.ts
+++ b/flowsint-app/src/api/chat-service.ts
@@ -1,98 +1,4 @@
import { fetchWithAuth } from './api'
-import { useAuthStore } from '@/stores/auth-store'
-
-export interface ChatResponse {
- content: string
-}
-
-export class ChatService {
- private static instance: ChatService
-
- public static getInstance(): ChatService {
- if (!ChatService.instance) {
- ChatService.instance = new ChatService()
- }
- return ChatService.instance
- }
-
- async streamChat(
- chatId: string,
- prompt: string,
- onChunk: (content: string) => void,
- context?: any
- ): Promise<string> {
- const token = useAuthStore.getState().token
- const API_URL = import.meta.env.VITE_API_URL
-
- const headers: HeadersInit = {
- 'Content-Type': 'application/json'
- }
-
- if (token) {
- headers['Authorization'] = `Bearer ${token}`
- }
-
- const response = await fetch(`${API_URL}/api/chats/stream/${chatId}`, {
- method: 'POST',
- headers,
- body: JSON.stringify({
- prompt,
- context
- })
- })
-
- if (response.status === 401) {
- useAuthStore.getState().logout()
- window.location.href = '/login'
- throw new Error('Session expired, login again.')
- }
-
- if (!response.ok) {
- const errorData = await response.json().catch(() => ({}))
- throw new Error(errorData.detail || `Error ${response.status}`)
- }
-
- const reader = response.body?.getReader()
- if (!reader) {
- console.error('No reader available')
- throw new Error('No reader available')
- }
-
- let accumulatedContent = ''
-
- while (true) {
- const { done, value } = await reader.read()
- if (done) {
- break
- }
-
- const text = new TextDecoder().decode(value)
- const lines = text.split('\n')
-
- for (const line of lines) {
- if (line.startsWith('data: ')) {
- const data = line.slice(6)
- if (data === '[DONE]') {
- return accumulatedContent
- }
- try {
- const parsed = JSON.parse(data)
- if (parsed.content) {
- accumulatedContent += parsed.content
- onChunk(accumulatedContent)
- }
- } catch (e) {
- console.error('Error parsing SSE data:', data, e)
- }
- }
- }
- }
-
- return accumulatedContent
- }
-}
-
-export const chatService = ChatService.getInstance()
export const chatCRUDService = {
get: async (): Promise<any> => {
diff --git a/flowsint-app/src/api/chat-transport.ts b/flowsint-app/src/api/chat-transport.ts
new file mode 100644
index 00000000..1330a94d
--- /dev/null
+++ b/flowsint-app/src/api/chat-transport.ts
@@ -0,0 +1,26 @@
+import { DefaultChatTransport } from 'ai'
+import { useAuthStore } from '@/stores/auth-store'
+
+const API_URL = import.meta.env.VITE_API_URL
+
+export function createChatTransport(chatId: string) {
+ return new DefaultChatTransport({
+ api: `${API_URL}/api/chats/stream/${chatId}`,
+ headers: (): Record<string, string> => {
+ const token = useAuthStore.getState().token
+ if (token) return { Authorization: `Bearer ${token}` }
+ return {}
+ },
+ prepareSendMessagesRequest: ({ messages }) => {
+ const lastUserMessage = [...messages].reverse().find((m) => m.role === 'user')
+ const prompt = lastUserMessage?.parts
+ ?.filter((p): p is { type: 'text'; text: string } => p.type === 'text')
+ .map((p) => p.text)
+ .join('') ?? ''
+
+ return {
+ body: { prompt },
+ }
+ },
+ })
+}
diff --git a/flowsint-app/src/api/template-service.ts b/flowsint-app/src/api/template-service.ts
index 507f574f..148cb177 100644
--- a/flowsint-app/src/api/template-service.ts
+++ b/flowsint-app/src/api/template-service.ts
@@ -39,6 +39,10 @@ export interface TestTemplateResponse {
url: string
}
+export interface GenerateTemplateResponse {
+ yaml_content: string
+}
+
export const templateService = {
getAll: async (): Promise<Template[]> => {
return fetchWithAuth('/api/enrichers/templates', {
@@ -84,5 +88,12 @@ export const templateService = {
method: 'POST',
body: JSON.stringify({ input_value: inputValue, content })
})
+ },
+
+ generate: async (prompt: string): Promise<GenerateTemplateResponse> => {
+ return fetchWithAuth('/api/enrichers/templates/generate', {
+ method: 'POST',
+ body: JSON.stringify({ prompt })
+ })
}
}
diff --git a/flowsint-app/src/components/chat/chat-prompt.tsx b/flowsint-app/src/components/chat/chat-prompt.tsx
index 91d196e7..1ed1d59c 100644
--- a/flowsint-app/src/components/chat/chat-prompt.tsx
+++ b/flowsint-app/src/components/chat/chat-prompt.tsx
@@ -3,24 +3,16 @@ import { Badge } from '@/components/ui/badge'
import { Textarea } from '@/components/ui/textarea'
import { XIcon, ArrowUp } from 'lucide-react'
import { useGraphStore } from '@/stores/graph-store'
-import { useRef, useEffect, memo } from 'react'
+import { useRef, useEffect, memo, useState } from 'react'
import { useNodesDisplaySettings } from '@/stores/node-display-settings'
interface ChatPanelProps {
- customPrompt: string
- setCustomPrompt: (prompt: string) => void
- handleCustomPrompt: (editorValue: any) => void
- isAiLoading: boolean
- editorValue: any
+ onSend: (text: string) => void
+ isLoading: boolean
}
-export const ChatPanel = ({
- customPrompt,
- setCustomPrompt,
- handleCustomPrompt,
- isAiLoading,
- editorValue
-}: ChatPanelProps) => {
+export const ChatPanel = ({ onSend, isLoading }: ChatPanelProps) => {
+ const [input, setInput] = useState('')
const selectedNodes = useGraphStore((s) => s.selectedNodes)
const clearSelectedNodes = useGraphStore((s) => s.clearSelectedNodes)
const textareaRef = useRef<HTMLTextAreaElement>(null)
@@ -40,11 +32,12 @@ export const ChatPanel = ({
textareaRef.current.style.height = 'auto'
textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 120)}px`
}
- }, [customPrompt])
+ }, [input])
const handleSubmit = () => {
- if (customPrompt.trim() && !isAiLoading) {
- handleCustomPrompt(editorValue)
+ if (input.trim() && !isLoading) {
+ onSend(input.trim())
+ setInput('')
}
}
@@ -79,21 +72,21 @@ export const ChatPanel = ({
ref={textareaRef}
autoFocus
placeholder="Ask me anything about your investigation..."
- value={customPrompt}
- onChange={(e) => setCustomPrompt(e.target.value)}
+ value={input}
+ onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
className={`
- min-h-[44px] max-h-[120px] resize-none
- border border-border bg-background
+ min-h-[44px] max-h-[120px] resize-none
+ border border-border bg-background
focus:ring-2 focus:ring-primary/20 focus:border-primary
transition-all duration-200
pr-12 py-3 px-4 rounded-xl
`}
- disabled={isAiLoading}
+ disabled={isLoading}
/>
<Button
onClick={handleSubmit}
- disabled={!customPrompt.trim() || isAiLoading}
+ disabled={!input.trim() || isLoading}
size="icon"
variant={'ghost'}
className={`
@@ -101,28 +94,19 @@ export const ChatPanel = ({
h-8 w-8 rounded-full
transition-all duration-200
${
- customPrompt.trim() && !isAiLoading
+ input.trim() && !isLoading
? 'text-primary-foreground'
: 'bg-muted text-muted-foreground'
}
`}
>
- {isAiLoading ? (
+ {isLoading ? (
<div className="w-4 h-4 border-2 border-current border-t-transparent rounded-full animate-spin" />
) : (
<ArrowUp className="w-4 h-4" />
)}
</Button>
</div>
-
- {/* <div className="text-xs text-muted-foreground flex items-center gap-2">
- {isAiLoading && (
- <div className="flex items-center gap-1">
- <div className="w-2 h-2 bg-primary rounded-full animate-pulse" />
- <span>Processing...</span>
- </div>
- )}
- </div> */}
</div>
</div>
</div>
diff --git a/flowsint-app/src/components/chat/floating-chat.tsx b/flowsint-app/src/components/chat/floating-chat.tsx
index ce025b21..83fd8ed4 100644
--- a/flowsint-app/src/components/chat/floating-chat.tsx
+++ b/flowsint-app/src/components/chat/floating-chat.tsx
@@ -1,15 +1,11 @@
import { useChat } from '@/hooks/use-chat'
-import { memo, useEffect, useState, useRef } from 'react'
-import { ChatPanel, ContextList } from './chat-prompt'
+import { memo, useEffect, useRef, useState } from 'react'
+import { ChatPanel } from './chat-prompt'
import { Button } from '../ui/button'
import { X, Plus, History } from 'lucide-react'
import { useKeyboardShortcut } from '@/hooks/use-keyboard-shortcut'
import { Card } from '../ui/card'
import { useLayoutStore } from '@/stores/layout-store'
-import { useChatState } from '@/stores/use-chat-store'
-import { useQuery } from '@tanstack/react-query'
-import { chatCRUDService } from '@/api/chat-service'
-import { ChatMessage } from '@/types'
import { cn } from '@/lib/utils'
import { MemoizedMarkdown } from './memoized-markdown'
import { ChatSkeleton } from './chat-skeleton'
@@ -17,78 +13,52 @@ import { ResizableChat } from './resizable-chat'
import ChatHistory from './chat-history'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../ui/tooltip'
import { CopyButton } from '../copy'
+import type { UIMessage } from 'ai'
function FloatingChat() {
- const [editorValue, setEditorValue] = useState<any>('')
const bottomRef = useRef<HTMLDivElement>(null)
- const currentChatId = useChatState((s) => s.currentChatId)
const isOpenChat = useLayoutStore((s) => s.isOpenChat)
const toggleChat = useLayoutStore((s) => s.toggleChat)
const closeChat = useLayoutStore((s) => s.closeChat)
- const messages = useChatState((s) => s.messages)
- const setMessages = useChatState((s) => s.setMessages)
const [view, setView] = useState<'chat' | 'history'>('chat')
- const { data: chat, isLoading } = useQuery({
- queryKey: ['chat', currentChatId],
- enabled: Boolean(currentChatId) && isOpenChat,
- queryFn: () => chatCRUDService.getById(currentChatId as string)
- })
-
- useEffect(() => {
- setEditorValue('')
- setMessages([])
- }, [currentChatId, setMessages])
-
- useEffect(() => {
- if (chat?.messages) {
- setMessages(chat.messages)
- }
- }, [chat, setMessages])
-
- // Chat hook
const {
- isAiLoading,
- customPrompt,
- setCustomPrompt,
- handleCustomPrompt,
+ messages,
+ status,
+ sendMessage,
+ currentChat,
+ isLoadingChat,
createNewChat,
- deleteChatMutation
- } = useChat({
- onContentUpdate: setEditorValue,
- onSuccess: () => {}
- })
+ deleteChatMutation,
+ currentChatId,
+ } = useChat()
+
+ const isStreaming = status === 'streaming' || status === 'submitted'
const { isMac } = useKeyboardShortcut({
key: 'e',
ctrlOrCmd: true,
- callback: toggleChat
+ callback: toggleChat,
})
useKeyboardShortcut({
key: 'Escape',
ctrlOrCmd: false,
- callback: closeChat
+ callback: closeChat,
})
- const handlecreateNewChat = () => {
+ const handleCreateNewChat = () => {
createNewChat()
setView('chat')
}
- // Check if there's content in the editor
- const hasContent =
- editorValue &&
- (typeof editorValue === 'string'
- ? editorValue.trim() !== ''
- : editorValue.content && editorValue.content.length > 0)
const hasMessages = messages.length > 0
useEffect(() => {
if (bottomRef.current) {
bottomRef.current.scrollIntoView({ behavior: 'smooth' })
}
- }, [messages, editorValue])
+ }, [messages])
const keyboardShortcut = isMac ? '⌘+E' : 'Ctrl+E'
@@ -105,7 +75,6 @@ function FloatingChat() {
variant="outline"
onClick={toggleChat}
>
- {/* <Sparkles className='!h-5 !w-5' /> */}
<img src="/icon.png" alt="Flowsint" className="h-12 w-12 object-contain" />
</Button>
</div>
@@ -130,7 +99,7 @@ function FloatingChat() {
<ChatHistory
setView={setView}
deleteChatMutation={deleteChatMutation}
- handleCreateNewChat={handlecreateNewChat}
+ handleCreateNewChat={handleCreateNewChat}
/>
) : (
<>
@@ -142,13 +111,13 @@ function FloatingChat() {
size="icon"
className="h-6 w-6"
//@ts-ignore
- onClick={handlecreateNewChat}
+ onClick={handleCreateNewChat}
title="Create new chat"
>
<Plus className="h-3 w-3" />
</Button>
<span className="text-sm opacity-60 truncate text-ellipsis">
- {chat?.title}
+ {currentChat?.title}
</span>
</div>
<div className="flex items-center gap-2">
@@ -171,32 +140,19 @@ function FloatingChat() {
{/* Content */}
<div className="flex flex-col flex-1 overflow-auto">
- {isLoading ? (
+ {isLoadingChat ? (
<ChatSkeleton />
) : hasMessages ? (
<div className="grow p-4 flex flex-col gap-2">
- {messages.map((message: ChatMessage) => (
+ {messages.map((message: UIMessage) => (
<ChatMessageComponent key={message.id} message={message} />
))}
- {hasContent && (
- <ChatMessageComponent
- key="draft"
- message={{
- content: editorValue,
- is_bot: true,
- id: new Date().getTime().toString(),
- created_at: new Date().toISOString(),
- chatId: currentChatId || undefined
- }}
- />
- )}
<div ref={bottomRef} />
</div>
) : null}
- {!hasMessages && !hasContent && !isLoading && (
+ {!hasMessages && !isLoadingChat && (
<div className="flex-1 flex flex-col items-center justify-center p-8 text-center text-muted-foreground">
{!currentChatId ? (
- // No currentChatId - Empty state with sparkles
<>
<div className="relative mb-6">
<div className="absolute inset-0 bg-gradient-to-r from-primary/20 to-primary/10 rounded-full blur-xl animate-pulse"></div>
@@ -221,7 +177,7 @@ function FloatingChat() {
<Button
variant="outline"
size="sm"
- onClick={handlecreateNewChat}
+ onClick={handleCreateNewChat}
className="bg-gradient-to-r from-primary/10 to-primary/5 border-primary/20 hover:from-primary/20 hover:to-primary/10"
>
Start your first chat
@@ -229,14 +185,7 @@ function FloatingChat() {
</div>
</>
) : (
- // Has currentChatId but no messages - Examples of things to ask
<>
- {/* <div className="relative mb-6">
- <div className="absolute inset-0 bg-gradient-to-r from-primary/20 to-primary/10 rounded-full blur-xl animate-pulse"></div>
- <div className="relative bg-gradient-to-br from-primary/10 to-primary/5 rounded-full p-6 border border-primary/20">
- <Sparkles className="h-8 w-8 text-primary/60" />
- </div>
- </div> */}
<div className="space-y-2 max-w-sm">
<h3 className="text-lg font-semibold text-foreground">
Start your conversation with{' '}
@@ -255,14 +204,6 @@ function FloatingChat() {
<li>• "Summarize the key findings from this investigation"</li>
</ul>
</div>
- {/* <div className="text-xs opacity-60 space-y-1">
- <p className="font-medium">Data Exploration:</p>
- <ul className="space-y-1 text-left">
- <li>• "Find all IP addresses related to this domain"</li>
- <li>• "Show me all crypto wallets connected to this address"</li>
- <li>• "What social media accounts are linked to this email?"</li>
- </ul>
- </div> */}
<div className="text-xs space-y-1">
<p className="font-medium text-left">Investigation Help</p>
<ul className="space-y-1 text-left opacity-60">
@@ -278,13 +219,7 @@ function FloatingChat() {
)}
</div>
<div className="border-t">
- <ChatPanel
- customPrompt={customPrompt}
- setCustomPrompt={setCustomPrompt}
- handleCustomPrompt={handleCustomPrompt}
- isAiLoading={isAiLoading}
- editorValue={editorValue}
- />
+ <ChatPanel onSend={sendMessage} isLoading={isStreaming} />
</div>
</>
)}
@@ -296,20 +231,29 @@ function FloatingChat() {
)
}
-const ChatMessageComponent = ({ message }: { message: ChatMessage }) => {
- if (message.is_bot)
+function getMessageText(message: UIMessage): string {
+ return message.parts
+ .filter((p): p is { type: 'text'; text: string } => p.type === 'text')
+ .map((p) => p.text)
+ .join('')
+}
+
+const ChatMessageComponent = ({ message }: { message: UIMessage }) => {
+ const content = getMessageText(message)
+
+ if (message.role === 'assistant')
return (
- <MessageContainer copyContent={message.content}>
+ <MessageContainer copyContent={content}>
<div className={cn('justify-start', 'flex w-full')}>
<div className={cn('w-full', 'p-3 rounded-xl max-w-full', 'flex flex-col gap-2')}>
- <MemoizedMarkdown id={message.id} content={message.content} />
+ <MemoizedMarkdown id={message.id} content={content} />
</div>
</div>
</MessageContainer>
)
return (
- <MessageContainer copyContent={message.content}>
+ <MessageContainer copyContent={content}>
<div className={cn('items-end', 'flex w-full flex-col gap-2')}>
<div
className={cn(
@@ -318,9 +262,8 @@ const ChatMessageComponent = ({ message }: { message: ChatMessage }) => {
'flex flex-col items-end overflow-x-hidden'
)}
>
- {/* {message?.context && message.context.length > 0 && <div className='flex items-center w-full overflow-x-auto justify-end mb-2'><ContextList context={message.context} /></div>} */}
<span className="px-3">
- <MemoizedMarkdown id={message.id} content={message.content} />
+ <MemoizedMarkdown id={message.id} content={content} />
</span>
</div>
</div>
diff --git a/flowsint-app/src/components/templates/ai-chat-panel.tsx b/flowsint-app/src/components/templates/ai-chat-panel.tsx
new file mode 100644
index 00000000..ea0cc32a
--- /dev/null
+++ b/flowsint-app/src/components/templates/ai-chat-panel.tsx
@@ -0,0 +1,370 @@
+import {
+ useState,
+ useRef,
+ useCallback,
+ useEffect,
+ forwardRef,
+ useImperativeHandle
+} from 'react'
+import {
+ Sparkles,
+ ArrowUp,
+ Loader2,
+ Bot,
+ Copy,
+ Check,
+ ArrowDownToLine,
+ Trash2,
+ Globe,
+ Zap,
+ Wand2
+} from 'lucide-react'
+import { toast } from 'sonner'
+
+import { Button } from '@/components/ui/button'
+import { Textarea } from '@/components/ui/textarea'
+import { MemoizedMarkdown } from '@/components/chat/memoized-markdown'
+import { templateService } from '@/api/template-service'
+import { cn } from '@/lib/utils'
+
+// ---------------------------------------------------------------------------
+// Types
+// ---------------------------------------------------------------------------
+
+interface ChatMessage {
+ id: string
+ role: 'user' | 'assistant'
+ content: string
+ timestamp: Date
+ yamlContent?: string
+}
+
+interface AIChatPanelProps {
+ onApplyYaml: (yaml: string) => void
+ currentYaml: string
+}
+
+export interface AIChatPanelHandle {
+ focusInput: () => void
+ sendMessage: (prompt: string) => void
+}
+
+// ---------------------------------------------------------------------------
+// Constants
+// ---------------------------------------------------------------------------
+
+const SUGGESTED_PROMPTS = [
+ { label: 'Generate a basic enricher', icon: Wand2 },
+ { label: 'Add an HTTP source', icon: Globe },
+ { label: 'Improve performance', icon: Zap }
+]
+
+// ---------------------------------------------------------------------------
+// Main Component
+// ---------------------------------------------------------------------------
+
+export const AIChatPanel = forwardRef<AIChatPanelHandle, AIChatPanelProps>(
+ ({ onApplyYaml, currentYaml }, ref) => {
+ const [messages, setMessages] = useState<ChatMessage[]>([])
+ const [input, setInput] = useState('')
+ const [isLoading, setIsLoading] = useState(false)
+ const messagesEndRef = useRef<HTMLDivElement>(null)
+ const textareaRef = useRef<HTMLTextAreaElement>(null)
+
+ const handleSend = useCallback(
+ async (prompt?: string) => {
+ const text = prompt || input.trim()
+ if (!text || isLoading) return
+
+ const userMessage: ChatMessage = {
+ id: crypto.randomUUID(),
+ role: 'user',
+ content: text,
+ timestamp: new Date()
+ }
+
+ setMessages((prev) => [...prev, userMessage])
+ setInput('')
+ setIsLoading(true)
+
+ try {
+ const contextPrompt = currentYaml?.trim()
+ ? `Current YAML template:\n\`\`\`yaml\n${currentYaml}\n\`\`\`\n\nUser request: ${text}`
+ : text
+
+ const response = await templateService.generate(contextPrompt)
+
+ const assistantMessage: ChatMessage = {
+ id: crypto.randomUUID(),
+ role: 'assistant',
+ content: `Here's the generated template:\n\n\`\`\`yaml\n${response.yaml_content}\n\`\`\``,
+ timestamp: new Date(),
+ yamlContent: response.yaml_content
+ }
+
+ setMessages((prev) => [...prev, assistantMessage])
+ } catch (error) {
+ const errorMessage: ChatMessage = {
+ id: crypto.randomUUID(),
+ role: 'assistant',
+ content: `I encountered an error: ${(error as Error).message}. Please try again.`,
+ timestamp: new Date()
+ }
+ setMessages((prev) => [...prev, errorMessage])
+ } finally {
+ setIsLoading(false)
+ }
+ },
+ [input, isLoading, currentYaml]
+ )
+
+ useImperativeHandle(
+ ref,
+ () => ({
+ focusInput: () => textareaRef.current?.focus(),
+ sendMessage: (prompt: string) => handleSend(prompt)
+ }),
+ [handleSend]
+ )
+
+ // Auto-scroll to bottom on new messages
+ useEffect(() => {
+ messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
+ }, [messages, isLoading])
+
+ // Auto-resize textarea
+ useEffect(() => {
+ const el = textareaRef.current
+ if (el) {
+ el.style.height = 'auto'
+ el.style.height = `${Math.min(el.scrollHeight, 120)}px`
+ }
+ }, [input])
+
+ const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
+ if (e.key === 'Enter' && !e.shiftKey) {
+ e.preventDefault()
+ handleSend()
+ }
+ }
+
+ const handleApply = useCallback(
+ (yaml: string) => {
+ onApplyYaml(yaml)
+ toast.success('Applied to editor')
+ },
+ [onApplyYaml]
+ )
+
+ const handleClear = useCallback(() => {
+ setMessages([])
+ }, [])
+
+ return (
+ <div className="h-full flex flex-col overflow-hidden">
+ {/* Header */}
+ <div className="shrink-0 flex items-center justify-between px-4 py-2.5 border-b bg-card/30">
+ <div className="flex items-center gap-2">
+ <div className="p-1 rounded-md bg-primary/10">
+ <Sparkles className="h-3.5 w-3.5 text-primary" />
+ </div>
+ <span className="text-sm font-medium">AI Assistant</span>
+ </div>
+ {messages.length > 0 && (
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={handleClear}
+ className="h-7 px-2 text-xs text-muted-foreground hover:text-foreground"
+ >
+ <Trash2 className="h-3 w-3 mr-1" />
+ Clear
+ </Button>
+ )}
+ </div>
+
+ {/* Messages — scrollable area */}
+ <div className="flex-1 min-h-0 overflow-y-auto">
+ <div className="p-4 space-y-0">
+ {messages.length === 0 ? (
+ <EmptyState onPromptClick={handleSend} />
+ ) : (
+ <>
+ {messages.map((message, i) => (
+ <div key={message.id}>
+ {i > 0 && <div className="border-t border-border/40" />}
+ <MessageItem message={message} onApply={handleApply} />
+ </div>
+ ))}
+ {isLoading && <TypingIndicator />}
+ <div ref={messagesEndRef} />
+ </>
+ )}
+ </div>
+ </div>
+
+ {/* Quick prompts (when conversation active) */}
+ {messages.length > 0 && !isLoading && (
+ <div className="shrink-0 px-4 pb-2 flex flex-wrap gap-1.5">
+ {SUGGESTED_PROMPTS.map((p) => (
+ <button
+ key={p.label}
+ onClick={() => handleSend(p.label)}
+ className="text-xs px-2.5 py-1 rounded-full border border-border/50 text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
+ >
+ {p.label}
+ </button>
+ ))}
+ </div>
+ )}
+
+ {/* Input */}
+ <div className="shrink-0 p-3 border-t bg-card/20">
+ <div className="relative">
+ <Textarea
+ ref={textareaRef}
+ value={input}
+ onChange={(e) => setInput(e.target.value)}
+ onKeyDown={handleKeyDown}
+ placeholder="Describe the enricher you need..."
+ disabled={isLoading}
+ className="min-h-[44px] max-h-[120px] resize-none pr-12 py-3 px-4 rounded-xl bg-muted/30 border-border/50 focus-visible:ring-1 focus-visible:ring-primary/30 focus-visible:border-primary/50 text-sm"
+ />
+ <Button
+ onClick={() => handleSend()}
+ disabled={!input.trim() || isLoading}
+ size="icon"
+ variant="ghost"
+ className={cn(
+ 'absolute right-2 bottom-2 h-8 w-8 rounded-lg transition-all',
+ input.trim() && !isLoading
+ ? 'bg-primary text-primary-foreground hover:bg-primary/90'
+ : 'bg-muted text-muted-foreground'
+ )}
+ >
+ {isLoading ? (
+ <Loader2 className="h-4 w-4 animate-spin" />
+ ) : (
+ <ArrowUp className="h-4 w-4" />
+ )}
+ </Button>
+ </div>
+ <p className="text-[10px] text-muted-foreground/50 mt-1.5 px-1">
+ Enter to send &middot; Shift+Enter for new line
+ </p>
+ </div>
+ </div>
+ )
+ }
+)
+
+AIChatPanel.displayName = 'AIChatPanel'
+
+// ---------------------------------------------------------------------------
+// Sub-components
+// ---------------------------------------------------------------------------
+
+function EmptyState({ onPromptClick }: { onPromptClick: (prompt: string) => void }) {
+ return (
+ <div className="flex flex-col items-center justify-center text-center px-6 py-12">
+ <div className="p-3 rounded-2xl bg-primary/5 border border-primary/10 mb-4">
+ <Sparkles className="h-8 w-8 text-primary/30" />
+ </div>
+ <h3 className="text-sm font-medium mb-1">Template Assistant</h3>
+ <p className="text-xs text-muted-foreground mb-6 max-w-[260px] leading-relaxed">
+ Describe the enricher you want to build. I understand OSINT concepts, HTTP APIs, and data
+ enrichment workflows.
+ </p>
+ <div className="w-full max-w-[280px] space-y-1.5">
+ {SUGGESTED_PROMPTS.map((prompt) => (
+ <button
+ key={prompt.label}
+ onClick={() => onPromptClick(prompt.label)}
+ className="w-full text-left text-xs px-3 py-2.5 rounded-lg border border-border/50 bg-card/50 hover:bg-muted/50 text-muted-foreground hover:text-foreground transition-colors flex items-center gap-2.5 group"
+ >
+ <prompt.icon className="h-3.5 w-3.5 shrink-0 text-muted-foreground/40 group-hover:text-primary/60 transition-colors" />
+ <span>{prompt.label}</span>
+ </button>
+ ))}
+ </div>
+ </div>
+ )
+}
+
+function MessageItem({
+ message,
+ onApply
+}: {
+ message: ChatMessage
+ onApply: (yaml: string) => void
+}) {
+ const [copied, setCopied] = useState(false)
+
+ const handleCopy = async () => {
+ const textToCopy = message.yamlContent || message.content
+ await navigator.clipboard.writeText(textToCopy)
+ setCopied(true)
+ toast.success('Copied to clipboard')
+ setTimeout(() => setCopied(false), 2000)
+ }
+
+ if (message.role === 'user') {
+ return (
+ <div className="py-2">
+ <div className="flex items-center gap-1.5 mb-1">
+ <span className="text-xs font-medium text-muted-foreground">You</span>
+ </div>
+ <p className="text-sm leading-relaxed">{message.content}</p>
+ </div>
+ )
+ }
+
+ return (
+ <div className="py-2">
+ <div className="flex items-center gap-1.5 mb-1">
+ <Bot className="h-3 w-3 text-primary/70" />
+ <span className="text-xs font-medium text-muted-foreground">Assistant</span>
+ </div>
+ <div className="text-sm leading-relaxed">
+ <MemoizedMarkdown content={message.content} id={message.id} />
+ </div>
+ {message.yamlContent && (
+ <div className="flex gap-1.5 mt-2">
+ <Button
+ size="sm"
+ onClick={() => onApply(message.yamlContent!)}
+ className="h-7 text-xs gap-1.5 rounded-lg"
+ >
+ <ArrowDownToLine className="h-3 w-3" />
+ Apply to Editor
+ </Button>
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handleCopy}
+ className="h-7 text-xs gap-1.5 rounded-lg"
+ >
+ {copied ? <Check className="h-3 w-3" /> : <Copy className="h-3 w-3" />}
+ {copied ? 'Copied' : 'Copy'}
+ </Button>
+ </div>
+ )}
+ </div>
+ )
+}
+
+function TypingIndicator() {
+ return (
+ <div className="py-2">
+ <div className="flex items-center gap-1.5 mb-1">
+ <Bot className="h-3 w-3 text-primary/70" />
+ <span className="text-xs font-medium text-muted-foreground">Assistant</span>
+ </div>
+ <div className="flex items-center gap-1.5 py-1">
+ <div className="w-1.5 h-1.5 rounded-full bg-muted-foreground/40 animate-bounce [animation-delay:0ms]" />
+ <div className="w-1.5 h-1.5 rounded-full bg-muted-foreground/40 animate-bounce [animation-delay:150ms]" />
+ <div className="w-1.5 h-1.5 rounded-full bg-muted-foreground/40 animate-bounce [animation-delay:300ms]" />
+ </div>
+ </div>
+ )
+}
diff --git a/flowsint-app/src/components/templates/index.ts b/flowsint-app/src/components/templates/index.ts
index 48cf2121..0b6c14db 100644
--- a/flowsint-app/src/components/templates/index.ts
+++ b/flowsint-app/src/components/templates/index.ts
@@ -1,5 +1,7 @@
export { YamlEditor } from './yaml-editor'
export { JsonViewer } from './json-viewer'
export { TemplateEditor } from './template-editor'
+export { AIChatPanel } from './ai-chat-panel'
+export type { AIChatPanelHandle } from './ai-chat-panel'
export { templateSchema, defaultTemplate } from './template-schema'
export type { TemplateData, TemplateInput, TemplateHttpRequest, TemplateHttpResponse, TemplateOutput } from './template-schema'
diff --git a/flowsint-app/src/components/templates/template-editor.tsx b/flowsint-app/src/components/templates/template-editor.tsx
index 54c6a6b5..6e5eb594 100644
--- a/flowsint-app/src/components/templates/template-editor.tsx
+++ b/flowsint-app/src/components/templates/template-editor.tsx
@@ -4,7 +4,6 @@ import { useNavigate } from '@tanstack/react-router'
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml'
import { toast } from 'sonner'
import {
- Save,
Copy,
Check,
AlertCircle,
@@ -16,31 +15,35 @@ import {
Loader2,
CheckCircle2,
XCircle,
- Info,
- Keyboard
+ Keyboard,
+ Sparkles,
+ Save
} from 'lucide-react'
import type { editor } from 'monaco-editor'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
-import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'
+import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
-import { ScrollArea } from '@/components/ui/scroll-area'
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'
+import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
+import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '@/components/ui/resizable'
import { useConfirm } from '@/components/use-confirm-dialog'
import { YamlEditor } from './yaml-editor'
import { JsonViewer } from './json-viewer'
+import { AIChatPanel, type AIChatPanelHandle } from './ai-chat-panel'
import { defaultTemplate, templateSchema, type TemplateData } from './template-schema'
import { templateService } from '@/api/template-service'
+// ---------------------------------------------------------------------------
+// Types
+// ---------------------------------------------------------------------------
+
interface TemplateEditorProps {
- /** Template ID for edit mode. If not provided, component is in create mode. */
templateId?: string
- /** Initial content for edit mode. Uses defaultTemplate for create mode. */
initialContent?: TemplateData
- /** Raw YAML string to use as initial content (e.g., from imported file). Takes precedence over initialContent. */
importedYaml?: string
}
@@ -52,6 +55,10 @@ interface TestResult {
url?: string
}
+// ---------------------------------------------------------------------------
+// Validation
+// ---------------------------------------------------------------------------
+
function validateTemplate(content: string): {
valid: boolean
errors: string[]
@@ -107,6 +114,10 @@ function validateTemplate(content: string): {
return { valid: errors.length === 0, errors, data: parsed }
}
+// ---------------------------------------------------------------------------
+// Main Component
+// ---------------------------------------------------------------------------
+
export function TemplateEditor({ templateId, initialContent, importedYaml }: TemplateEditorProps) {
const navigate = useNavigate()
const queryClient = useQueryClient()
@@ -119,6 +130,7 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
[initialContent, importedYaml]
)
+ // ── Editor state ──────────────────────────────────────────────────────────
const [content, setContent] = useState(initialYaml)
const [savedContent, setSavedContent] = useState(initialYaml)
const [copied, setCopied] = useState(false)
@@ -126,60 +138,28 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
const [validationResult, setValidationResult] = useState(() => validateTemplate(initialYaml))
const [activeTab, setActiveTab] = useState<'editor' | 'test'>('editor')
- // Test state
+ // ── Test state ────────────────────────────────────────────────────────────
const [testInput, setTestInput] = useState('')
const [testParams, setTestParams] = useState<Record<string, string>>({})
const [testResult, setTestResult] = useState<TestResult | null>(null)
const [isTesting, setIsTesting] = useState(false)
- // Get params from template, replacing {{key}} placeholders with empty string for display
- const templateParams = validationResult.data?.request?.params || {}
- const paramKeys = Object.keys(templateParams)
-
- // Build the full URL with query params for preview
- const buildPreviewUrl = () => {
- if (!validationResult.data?.request?.url) return ''
-
- const inputKey = validationResult.data.input?.key || 'value'
- let url = validationResult.data.request.url.replace(
- new RegExp(`\\{\\{${inputKey}\\}\\}`, 'g'),
- testInput || `{${inputKey}}`
- )
-
- if (paramKeys.length > 0) {
- const paramsObj: Record<string, string> = {}
- for (const key of paramKeys) {
- // Convert to string since YAML may parse numbers/booleans as their native types
- const templateValue = String(templateParams[key] ?? '')
- // Replace {{key}} in param value with test input
- const resolvedValue = templateValue.replace(
- new RegExp(`\\{\\{${inputKey}\\}\\}`, 'g'),
- testInput || `{${inputKey}}`
- )
- // Use custom test value if provided, otherwise use resolved template value
- paramsObj[key] = testParams[key] || resolvedValue
- }
- const searchParams = new URLSearchParams(paramsObj)
- url += (url.includes('?') ? '&' : '?') + searchParams.toString()
- }
-
- return url
- }
+ // ── Refs ──────────────────────────────────────────────────────────────────
+ const chatRef = useRef<AIChatPanelHandle>(null)
+ const contentBeingSavedRef = useRef<string | null>(null)
+ // ── Derived state ─────────────────────────────────────────────────────────
const hasChanges = content !== savedContent
const hasErrors = !validationResult.valid || editorErrors.some((e) => e.severity >= 8)
-
- // Derive template name from content
const templateName = validationResult.data?.name || 'Untitled'
+ const templateParams = validationResult.data?.request?.params || {}
+ const paramKeys = Object.keys(templateParams)
- // Ref to always have latest state for keyboard shortcut
const stateRef = useRef({ hasErrors, hasChanges, data: validationResult.data, content })
stateRef.current = { hasErrors, hasChanges, data: validationResult.data, content }
- // Ref to track content being saved
- const contentBeingSavedRef = useRef<string | null>(null)
+ // ── Mutations ─────────────────────────────────────────────────────────────
- // Create mutation (for new templates)
const createMutation = useMutation({
mutationFn: (data: TemplateData) =>
templateService.create({
@@ -198,7 +178,6 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
}
})
- // Update mutation (for existing templates)
const updateMutation = useMutation({
mutationFn: (data: TemplateData) => templateService.update(templateId!, { content: data }),
onSuccess: () => {
@@ -216,7 +195,6 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
}
})
- // Delete mutation (for existing templates)
const deleteMutation = useMutation({
mutationFn: () => templateService.delete(templateId!),
onSuccess: () => {
@@ -229,6 +207,10 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
}
})
+ const isSaving = createMutation.isPending || updateMutation.isPending
+
+ // ── Handlers ──────────────────────────────────────────────────────────────
+
const handleChange = useCallback((value: string) => {
setContent(value)
setValidationResult(validateTemplate(value))
@@ -244,7 +226,6 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
toast.error('Please fix validation errors before saving')
return
}
-
if (isEditMode) {
if (!hasChanges) return
contentBeingSavedRef.current = content
@@ -254,18 +235,6 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
}
}, [isEditMode, updateMutation, createMutation])
- // Keyboard shortcuts
- useEffect(() => {
- const handleKeyDown = (e: KeyboardEvent) => {
- if ((e.metaKey || e.ctrlKey) && e.key === 's') {
- e.preventDefault()
- handleSave()
- }
- }
- window.addEventListener('keydown', handleKeyDown)
- return () => window.removeEventListener('keydown', handleKeyDown)
- }, [handleSave])
-
const handleDelete = useCallback(async () => {
const confirmed = await confirm({
title: 'Delete template?',
@@ -287,26 +256,26 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
}
}, [content])
+ const handleApplyYaml = useCallback((yaml: string) => {
+ setContent(yaml)
+ setValidationResult(validateTemplate(yaml))
+ }, [])
+
const handleTest = useCallback(async () => {
if (!testInput.trim()) {
toast.error('Please enter a test value')
return
}
-
if (!validationResult.data) {
toast.error('Please fix validation errors before testing')
return
}
-
setIsTesting(true)
setTestResult(null)
-
try {
- // Use different API based on mode
const response = isEditMode
? await templateService.test(templateId!, testInput.trim())
: await templateService.testContent(testInput.trim(), validationResult.data)
-
setTestResult({
success: response.success,
data: response.data,
@@ -325,13 +294,55 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
}
}, [isEditMode, templateId, testInput, validationResult.data])
- const isSaving = createMutation.isPending || updateMutation.isPending
+ const buildPreviewUrl = () => {
+ if (!validationResult.data?.request?.url) return ''
+ const inputKey = validationResult.data.input?.key || 'value'
+ let url = validationResult.data.request.url.replace(
+ new RegExp(`\\{\\{${inputKey}\\}\\}`, 'g'),
+ testInput || `{${inputKey}}`
+ )
+ if (paramKeys.length > 0) {
+ const paramsObj: Record<string, string> = {}
+ for (const key of paramKeys) {
+ const templateValue = String(templateParams[key] ?? '')
+ const resolvedValue = templateValue.replace(
+ new RegExp(`\\{\\{${inputKey}\\}\\}`, 'g'),
+ testInput || `{${inputKey}}`
+ )
+ paramsObj[key] = testParams[key] || resolvedValue
+ }
+ const searchParams = new URLSearchParams(paramsObj)
+ url += (url.includes('?') ? '&' : '?') + searchParams.toString()
+ }
+ return url
+ }
+
+ // ── Keyboard shortcuts ────────────────────────────────────────────────────
+
+ useEffect(() => {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if ((e.metaKey || e.ctrlKey) && e.key === 's') {
+ e.preventDefault()
+ handleSave()
+ }
+ }
+ window.addEventListener('keydown', handleKeyDown)
+ return () => window.removeEventListener('keydown', handleKeyDown)
+ }, [handleSave])
+
+ // ── Derived ───────────────────────────────────────────────────────────────
+
+ const totalErrors =
+ validationResult.errors.length + editorErrors.filter((e) => e.severity >= 8).length
+
+ // ── Render ────────────────────────────────────────────────────────────────
return (
<TooltipProvider>
- <div className="h-full flex flex-col bg-background">
- {/* Header */}
- <header className="flex items-center justify-between px-4 py-3 border-b bg-card/50">
+ <div className="h-full flex flex-col overflow-hidden bg-background">
+ {/* ── Header ──────────────────────────────────────────────── */}
+ <header className="shrink-0 flex items-center justify-between px-4 py-2 border-b bg-card/30">
+ {/* Left: Breadcrumb + Status */}
<div className="flex items-center gap-2 text-sm">
<button
onClick={() => navigate({ to: '/dashboard/enrichers' })}
@@ -339,15 +350,14 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
>
Templates
</button>
- <ChevronRight className="h-4 w-4 text-muted-foreground" />
+ <ChevronRight className="h-3.5 w-3.5 text-muted-foreground/40" />
<span className="font-medium">{isEditMode ? templateName : 'New Template'}</span>
- {/* Status Badge */}
{isEditMode ? (
hasChanges ? (
<Badge
variant="outline"
- className="ml-2 text-xs bg-amber-500/10 text-amber-600 border-amber-500/20"
+ className="ml-1 text-[10px] bg-amber-500/10 text-amber-600 border-amber-500/20"
>
Unsaved
</Badge>
@@ -355,9 +365,9 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
!hasErrors && (
<Badge
variant="outline"
- className="ml-2 text-xs bg-emerald-500/10 text-emerald-600 border-emerald-500/20"
+ className="ml-1 text-[10px] bg-emerald-500/10 text-emerald-600 border-emerald-500/20"
>
- <CheckCircle2 className="h-3 w-3 mr-1" />
+ <CheckCircle2 className="h-2.5 w-2.5 mr-0.5" />
Saved
</Badge>
)
@@ -365,62 +375,153 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
) : (
<Badge
variant="outline"
- className="ml-2 text-xs bg-blue-500/10 text-blue-600 border-blue-500/20"
+ className="ml-1 text-[10px] bg-blue-500/10 text-blue-600 border-blue-500/20"
>
Draft
</Badge>
)}
</div>
- <div className="flex items-center gap-2">
+ {/* Right: Toolbar */}
+ <div className="flex items-center gap-1">
+ {/* Validate (popover with errors) */}
+ <Popover>
+ <PopoverTrigger asChild>
+ <Button
+ variant="ghost"
+ size="sm"
+ className={`h-7 gap-1.5 text-xs ${hasErrors ? 'text-destructive hover:text-destructive' : 'text-emerald-500 hover:text-emerald-500'}`}
+ >
+ {hasErrors ? (
+ <>
+ <XCircle className="h-3.5 w-3.5" />
+ {totalErrors} error{totalErrors !== 1 ? 's' : ''}
+ </>
+ ) : (
+ <>
+ <CheckCircle2 className="h-3.5 w-3.5" />
+ Valid
+ </>
+ )}
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent align="end" className="w-80 p-0">
+ <div className="p-3 border-b">
+ <p className="text-sm font-medium">
+ {hasErrors ? 'Validation Errors' : 'Validation Passed'}
+ </p>
+ </div>
+ {hasErrors ? (
+ <div className="p-3 space-y-2 max-h-[200px] overflow-y-auto">
+ {validationResult.errors.map((error, i) => (
+ <div key={i} className="flex gap-2 text-xs">
+ <AlertCircle className="h-3.5 w-3.5 text-destructive shrink-0 mt-0.5" />
+ <span className="text-destructive">{error}</span>
+ </div>
+ ))}
+ {editorErrors
+ .filter((e) => e.severity >= 8)
+ .map((error, i) => (
+ <div key={`e-${i}`} className="flex gap-2 text-xs">
+ <AlertCircle className="h-3.5 w-3.5 text-destructive shrink-0 mt-0.5" />
+ <span className="text-destructive">
+ Line {error.startLineNumber}: {error.message}
+ </span>
+ </div>
+ ))}
+ </div>
+ ) : (
+ <div className="p-3">
+ <p className="text-xs text-muted-foreground">
+ All required fields are present and valid.
+ </p>
+ </div>
+ )}
+ </PopoverContent>
+ </Popover>
+
+ <div className="w-px h-4 bg-border mx-0.5" />
+
+ {/* Generate */}
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={() => {
+ setActiveTab('editor')
+ setTimeout(() => chatRef.current?.focusInput(), 50)
+ }}
+ className="h-7 gap-1.5 text-xs"
+ >
+ <Sparkles className="h-3.5 w-3.5" />
+ Generate
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent>Focus AI assistant</TooltipContent>
+ </Tooltip>
+
+ <div className="w-px h-4 bg-border mx-0.5" />
+
+ {/* Copy */}
<Tooltip>
<TooltipTrigger asChild>
- <Button variant="ghost" size="sm" onClick={handleCopy} className="h-8 px-2">
- {copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
+ <Button variant="ghost" size="sm" onClick={handleCopy} className="h-7 w-7 p-0">
+ {copied ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />}
</Button>
</TooltipTrigger>
<TooltipContent>Copy YAML</TooltipContent>
</Tooltip>
- <div className="w-px h-6 bg-border" />
+
+ {/* Save */}
<Tooltip>
<TooltipTrigger asChild>
<Button
- variant="default"
size="sm"
onClick={handleSave}
disabled={(isEditMode && !hasChanges) || hasErrors || isSaving}
- className="h-8 gap-1.5"
+ className="h-7 gap-1.5 text-xs"
>
- {isSaving && <Loader2 className="h-4 w-4 animate-spin" />}
- {isEditMode ? 'Save' : 'Create template'}
+ {isSaving ? (
+ <Loader2 className="h-3.5 w-3.5 animate-spin" />
+ ) : (
+ <Save className="h-3.5 w-3.5" />
+ )}
+ Save Enricher
</Button>
</TooltipTrigger>
<TooltipContent>
- <div className="flex items-center gap-2">
- <Keyboard className="h-3 w-3" />
- <span>⌘S</span>
- </div>
+ <span className="flex items-center gap-1.5">
+ <Keyboard className="h-3 w-3" /> ⌘S
+ </span>
</TooltipContent>
</Tooltip>
+
+ {/* Delete */}
{isEditMode && (
- <Tooltip>
- <TooltipTrigger asChild>
- <Button
- variant="ghost"
- size="sm"
- onClick={handleDelete}
- disabled={deleteMutation.isPending}
- className="h-8 px-2 text-muted-foreground hover:text-destructive hover:bg-destructive/10"
- >
- <Trash2 className="h-4 w-4" />
- </Button>
- </TooltipTrigger>
- <TooltipContent>Delete template</TooltipContent>
- </Tooltip>
+ <>
+ <div className="w-px h-4 bg-border mx-0.5" />
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={handleDelete}
+ disabled={deleteMutation.isPending}
+ className="h-7 w-7 p-0 text-muted-foreground hover:text-destructive hover:bg-destructive/10"
+ >
+ <Trash2 className="h-3.5 w-3.5" />
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent>Delete template</TooltipContent>
+ </Tooltip>
+ </>
)}
</div>
</header>
- <div className="border-b bg-card/30">
+
+ {/* ── Tab Bar ─────────────────────────────────────────────── */}
+ <div className="shrink-0 border-b bg-card/30">
<Tabs
value={activeTab}
onValueChange={(v) => setActiveTab(v as 'editor' | 'test')}
@@ -438,283 +539,240 @@ export function TemplateEditor({ templateId, initialContent, importedYaml }: Tem
</TabsList>
</Tabs>
</div>
- <div className="flex-1 flex overflow-hidden">
- <Tabs value={activeTab} className="flex-1 flex">
- <TabsContent
- value="editor"
- className="flex-1 grow flex m-0 data-[state=inactive]:hidden"
- >
- <div className="flex-1 min-w-0 min-h-0 overflow-hidden">
- <YamlEditor
- value={content}
- onChange={handleChange}
- onValidate={handleEditorValidate}
- />
- </div>
- <div className="w-72 border-l bg-card/30 flex flex-col overflow-auto">
- <div className="p-4 border-b">
- <div className="flex items-center gap-2">
- {hasErrors ? (
- <>
- <div className="p-1.5 rounded-full bg-destructive/10">
- <XCircle className="h-4 w-4 text-destructive" />
- </div>
- <div>
- <p className="text-sm font-medium text-destructive">Validation failed</p>
- <p className="text-xs text-muted-foreground">
- {validationResult.errors.length +
- editorErrors.filter((e) => e.severity >= 8).length}{' '}
- error(s)
- </p>
- </div>
- </>
- ) : (
- <>
- <div className="p-1.5 rounded-full bg-emerald-500/10">
- <CheckCircle2 className="h-4 w-4 text-emerald-500" />
- </div>
- <div>
- <p className="text-sm font-medium text-emerald-600">Valid template</p>
- <p className="text-xs text-muted-foreground">
- {isEditMode ? 'Ready to save' : 'Ready to create'}
- </p>
- </div>
- </>
- )}
- </div>
- </div>
- <div className="flex-1 grow overflow-auto">
- <div className="p-4 space-y-3">
- {validationResult.errors.map((error, i) => (
- <div key={i} className="flex gap-2 text-sm">
- <AlertCircle className="h-4 w-4 text-destructive shrink-0 mt-0.5" />
- <span className="text-destructive">{error}</span>
- </div>
- ))}
- {editorErrors
- .filter((e) => e.severity >= 8)
- .map((error, i) => (
- <div key={`editor-${i}`} className="flex gap-2 text-sm">
- <AlertCircle className="h-4 w-4 text-destructive shrink-0 mt-0.5" />
- <span className="text-destructive">
- Line {error.startLineNumber}: {error.message}
- </span>
- </div>
- ))}
- </div>
- <div className="p-4">
- <div className="flex items-center gap-2 mb-3">
- <Info className="h-4 w-4 text-muted-foreground" />
- <span className="text-sm font-medium">Schema reference</span>
+
+ {/* ── Tab Content ─────────────────────────────────────────── */}
+ <div className="flex-1 min-h-0 overflow-hidden">
+ {/* ── Editor Tab ───────────────────────────────────────── */}
+ {activeTab === 'editor' && (
+ <div className="h-full">
+ <ResizablePanelGroup direction="horizontal">
+ {/* Left: Code Editor */}
+ <ResizablePanel defaultSize={60} minSize={30}>
+ <div className="h-full flex flex-col overflow-hidden">
+ <div className="flex-1 min-h-0">
+ <YamlEditor
+ value={content}
+ onChange={handleChange}
+ onValidate={handleEditorValidate}
+ />
</div>
- <div className="text-xs text-muted-foreground space-y-2">
- <div className="space-y-1">
- <p className="font-medium text-foreground">Required:</p>
- <ul className="list-disc list-inside space-y-0.5 ml-1">
- <li>name, category, version</li>
- <li>input.type</li>
- <li>request.method, request.url</li>
- <li>output.type</li>
- <li>response.expect</li>
- </ul>
- </div>
- <div className="space-y-1">
- <p className="font-medium text-foreground">Optional:</p>
- <ul className="list-disc list-inside space-y-0.5 ml-1">
- <li>description (root level)</li>
- <li>input.key (default: nodeLabel)</li>
- <li>request.headers, request.params, request.body</li>
- <li>response.map</li>
- </ul>
+
+ {/* Status Bar */}
+ <div className="shrink-0 flex items-center justify-between px-3 py-1 border-t bg-card/20 text-[11px] text-muted-foreground select-none">
+ <div className="flex items-center gap-3">
+ {hasErrors ? (
+ <span className="flex items-center gap-1 text-destructive">
+ <XCircle className="h-3 w-3" />
+ {totalErrors} error{totalErrors !== 1 ? 's' : ''}
+ </span>
+ ) : (
+ <span className="flex items-center gap-1 text-emerald-500/80">
+ <CheckCircle2 className="h-3 w-3" />
+ Valid
+ </span>
+ )}
+ {hasChanges && <span className="text-amber-500">Modified</span>}
</div>
- <div className="pt-2">
- <p className="font-medium text-foreground">URL Variables:</p>
- <code className="text-xs bg-muted px-1 py-0.5 rounded">{'{{key}}'}</code>
+ <div className="flex items-center gap-3">
+ <span>{content.split('\n').length} lines</span>
+ <span>YAML</span>
</div>
</div>
</div>
+ </ResizablePanel>
+
+ {/* Resize Handle */}
+ <ResizableHandle className="hover:bg-primary/20 active:bg-primary/30 transition-colors data-[resize-handle-state=drag]:bg-primary/30" />
+
+ {/* Right: AI Chat */}
+ <ResizablePanel defaultSize={40} minSize={25}>
+ <AIChatPanel
+ ref={chatRef}
+ onApplyYaml={handleApplyYaml}
+ currentYaml={content}
+ />
+ </ResizablePanel>
+ </ResizablePanelGroup>
+ </div>
+ )}
+
+ {/* ── Test Tab ─────────────────────────────────────────── */}
+ {activeTab === 'test' && (
+ <div className="h-full flex overflow-hidden">
+ {/* Test Input */}
+ <div className="w-80 shrink-0 border-r p-6 flex flex-col gap-6 overflow-y-auto">
+ <div>
+ <h3 className="text-lg font-semibold mb-1">Test your template</h3>
+ <p className="text-sm text-muted-foreground">
+ Enter a test value to simulate the enricher request.
+ </p>
</div>
- </div>
- </TabsContent>
-
- {/* Test Tab */}
- <TabsContent value="test" className="flex-1 m-0 data-[state=inactive]:hidden">
- <div className="h-full flex">
- {/* Test Input */}
- <div className="w-80 border-r p-6 flex flex-col gap-6">
- <div>
- <h3 className="text-lg font-semibold mb-1">Test your template</h3>
- <p className="text-sm text-muted-foreground">
- Enter a test value to simulate the enricher request.
- </p>
+
+ <div className="space-y-4">
+ <div className="space-y-2">
+ <Label htmlFor="test-input">
+ Input Value
+ {validationResult.data?.input?.key && (
+ <span className="text-muted-foreground ml-1">
+ ({validationResult.data.input.key})
+ </span>
+ )}
+ </Label>
+ <Input
+ id="test-input"
+ placeholder={`Enter ${validationResult.data?.input?.type || 'value'}...`}
+ value={testInput}
+ onChange={(e) => setTestInput(e.target.value)}
+ onKeyDown={(e) => e.key === 'Enter' && handleTest()}
+ />
</div>
- <div className="space-y-4">
+ {paramKeys.length > 0 && (
<div className="space-y-2">
- <Label htmlFor="test-input">
- Input Value
- {validationResult.data?.input?.key && (
- <span className="text-muted-foreground ml-1">
- ({validationResult.data.input.key})
- </span>
- )}
- </Label>
- <Input
- id="test-input"
- placeholder={`Enter ${validationResult.data?.input?.type || 'value'}...`}
- value={testInput}
- onChange={(e) => setTestInput(e.target.value)}
- onKeyDown={(e) => e.key === 'Enter' && handleTest()}
- />
+ <Label className="text-muted-foreground">Query Parameters</Label>
+ <div className="border rounded-md overflow-hidden">
+ <table className="w-full text-sm">
+ <thead>
+ <tr className="bg-muted/50 border-b">
+ <th className="text-left px-3 py-1.5 text-xs font-medium text-muted-foreground w-1/3">
+ Key
+ </th>
+ <th className="text-left px-3 py-1.5 text-xs font-medium text-muted-foreground">
+ Value
+ </th>
+ </tr>
+ </thead>
+ <tbody>
+ {paramKeys.map((paramKey, idx) => {
+ const defaultValue = templateParams[paramKey] ?? ''
+ return (
+ <tr
+ key={paramKey}
+ className={idx < paramKeys.length - 1 ? 'border-b' : ''}
+ >
+ <td className="px-3 py-1.5 font-mono text-xs text-muted-foreground">
+ {paramKey}
+ </td>
+ <td className="px-1 py-1">
+ <Input
+ placeholder={String(defaultValue) || 'Value'}
+ value={testParams[paramKey] ?? ''}
+ onChange={(e) =>
+ setTestParams((prev) => ({
+ ...prev,
+ [paramKey]: e.target.value
+ }))
+ }
+ className="h-7 text-xs border-0 bg-transparent focus-visible:ring-1 focus-visible:ring-offset-0"
+ />
+ </td>
+ </tr>
+ )
+ })}
+ </tbody>
+ </table>
+ </div>
</div>
+ )}
- {paramKeys.length > 0 && (
- <div className="space-y-2">
- <Label className="text-muted-foreground">Query Parameters</Label>
- <div className="border rounded-md overflow-hidden">
- <table className="w-full text-sm">
- <thead>
- <tr className="bg-muted/50 border-b">
- <th className="text-left px-3 py-1.5 text-xs font-medium text-muted-foreground w-1/3">
- Key
- </th>
- <th className="text-left px-3 py-1.5 text-xs font-medium text-muted-foreground">
- Value
- </th>
- </tr>
- </thead>
- <tbody>
- {paramKeys.map((paramKey, idx) => {
- const defaultValue = templateParams[paramKey] ?? ''
- return (
- <tr
- key={paramKey}
- className={idx < paramKeys.length - 1 ? 'border-b' : ''}
- >
- <td className="px-3 py-1.5 font-mono text-xs text-muted-foreground">
- {paramKey}
- </td>
- <td className="px-1 py-1">
- <Input
- placeholder={defaultValue || 'Value'}
- value={testParams[paramKey] ?? ''}
- onChange={(e) =>
- setTestParams((prev) => ({
- ...prev,
- [paramKey]: e.target.value
- }))
- }
- className="h-7 text-xs border-0 bg-transparent focus-visible:ring-1 focus-visible:ring-offset-0"
- />
- </td>
- </tr>
- )
- })}
- </tbody>
- </table>
- </div>
- </div>
- )}
+ {validationResult.data?.request?.url && (
+ <div className="space-y-2">
+ <Label className="text-muted-foreground">Request URL</Label>
+ <code className="block text-xs bg-muted p-2 rounded break-all">
+ {buildPreviewUrl()}
+ </code>
+ </div>
+ )}
- {validationResult.data?.request?.url && (
- <div className="space-y-2">
- <Label className="text-muted-foreground">Request URL</Label>
- <code className="block text-xs bg-muted p-2 rounded break-all">
- {buildPreviewUrl()}
- </code>
- </div>
+ <Button
+ onClick={handleTest}
+ disabled={isTesting || hasErrors || !testInput.trim()}
+ className="w-full gap-2"
+ >
+ {isTesting ? (
+ <>
+ <Loader2 className="h-4 w-4 animate-spin" />
+ Testing...
+ </>
+ ) : (
+ <>
+ <Play className="h-4 w-4" />
+ Run test
+ </>
)}
+ </Button>
+ </div>
- <Button
- onClick={handleTest}
- disabled={isTesting || hasErrors || !testInput.trim()}
- className="w-full gap-2"
- >
- {isTesting ? (
- <>
- <Loader2 className="h-4 w-4 animate-spin" />
- Testing...
- </>
- ) : (
- <>
- <Play className="h-4 w-4" />
- Run test
- </>
- )}
- </Button>
+ {hasErrors && (
+ <div className="p-3 rounded-lg bg-destructive/10 border border-destructive/20">
+ <p className="text-sm text-destructive">
+ Fix validation errors before testing.
+ </p>
</div>
+ )}
+ </div>
- {hasErrors && (
- <div className="p-3 rounded-lg bg-destructive/10 border border-destructive/20">
- <p className="text-sm text-destructive">
- Fix validation errors before testing.
- </p>
- </div>
- )}
- </div>
-
- {/* Test Result */}
- <div className="flex-1 p-6 bg-muted/20">
- <div className="h-full flex flex-col">
- <div className="flex items-center justify-between mb-4">
- <h3 className="text-lg font-semibold">Response</h3>
- {testResult?.duration !== undefined && testResult.duration > 0 && (
- <Badge variant="secondary" className="text-xs">
- {testResult.duration}ms
- </Badge>
- )}
- </div>
+ {/* Test Result */}
+ <div className="flex-1 min-w-0 p-6 bg-muted/20 overflow-y-auto">
+ <div className="h-full flex flex-col">
+ <div className="shrink-0 flex items-center justify-between mb-4">
+ <h3 className="text-lg font-semibold">Response</h3>
+ {testResult?.duration !== undefined && testResult.duration > 0 && (
+ <Badge variant="secondary" className="text-xs">
+ {testResult.duration}ms
+ </Badge>
+ )}
+ </div>
- {!testResult ? (
- <div className="flex-1 flex items-center justify-center">
- <div className="text-center text-muted-foreground">
- <FlaskConical className="h-12 w-12 mx-auto mb-4 opacity-20" />
- <p className="text-sm">Run a test to see the response here.</p>
- </div>
+ {!testResult ? (
+ <div className="flex-1 flex items-center justify-center">
+ <div className="text-center text-muted-foreground">
+ <FlaskConical className="h-12 w-12 mx-auto mb-4 opacity-20" />
+ <p className="text-sm">Run a test to see the response here.</p>
</div>
- ) : (
- <div className="flex-1 flex flex-col gap-4">
- <div
- className={`p-3 rounded-lg border ${
- testResult.success
- ? 'bg-emerald-500/10 border-emerald-500/20'
- : 'bg-destructive/10 border-destructive/20'
- }`}
- >
- <div className="flex items-center gap-2">
- {testResult.success ? (
- <>
- <CheckCircle2 className="h-5 w-5 text-emerald-500" />
- <span className="font-medium text-emerald-600">
- Request Successful
- </span>
- </>
- ) : (
- <>
- <XCircle className="h-5 w-5 text-destructive" />
- <span className="font-medium text-destructive">Request Failed</span>
- </>
- )}
- </div>
- {testResult.error && (
- <p className="text-sm text-destructive mt-1">{testResult.error}</p>
+ </div>
+ ) : (
+ <div className="flex-1 min-h-0 flex flex-col gap-4">
+ <div
+ className={`shrink-0 p-3 rounded-lg border ${
+ testResult.success
+ ? 'bg-emerald-500/10 border-emerald-500/20'
+ : 'bg-destructive/10 border-destructive/20'
+ }`}
+ >
+ <div className="flex items-center gap-2">
+ {testResult.success ? (
+ <>
+ <CheckCircle2 className="h-5 w-5 text-emerald-500" />
+ <span className="font-medium text-emerald-600">
+ Request Successful
+ </span>
+ </>
+ ) : (
+ <>
+ <XCircle className="h-5 w-5 text-destructive" />
+ <span className="font-medium text-destructive">Request Failed</span>
+ </>
)}
</div>
- {testResult.data && (
- <div className="flex-1 min-h-0 flex flex-col">
- <Label className="mb-2 block">Response Data</Label>
- <div className="flex-1 min-h-[300px] rounded-md border overflow-hidden">
- <JsonViewer data={testResult.data} />
- </div>
- </div>
+ {testResult.error && (
+ <p className="text-sm text-destructive mt-1">{testResult.error}</p>
)}
</div>
- )}
- </div>
+ {testResult.data != null && (
+ <div className="flex-1 min-h-0 flex flex-col">
+ <Label className="shrink-0 mb-2 block">Response Data</Label>
+ <div className="flex-1 min-h-[200px] rounded-md border overflow-hidden">
+ <JsonViewer data={testResult.data} />
+ </div>
+ </div>
+ )}
+ </div>
+ )}
</div>
</div>
- </TabsContent>
- </Tabs>
+ </div>
+ )}
</div>
</div>
</TooltipProvider>
diff --git a/flowsint-app/src/hooks/use-chat.ts b/flowsint-app/src/hooks/use-chat.ts
index ad4d4e6d..515c324a 100644
--- a/flowsint-app/src/hooks/use-chat.ts
+++ b/flowsint-app/src/hooks/use-chat.ts
@@ -1,72 +1,86 @@
-import { useState } from 'react'
+import { useEffect, useMemo, useRef } from 'react'
+import { useChat as useAIChat } from '@ai-sdk/react'
import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { chatService, chatCRUDService } from '@/api/chat-service'
-import { marked } from 'marked'
-import { generateJSON } from '@tiptap/core'
-import { StarterKit } from '@tiptap/starter-kit'
-import { Typography } from '@tiptap/extension-typography'
-import { Placeholder } from '@tiptap/extension-placeholder'
-import { Underline } from '@tiptap/extension-underline'
-import { TextStyle } from '@tiptap/extension-text-style'
-import { Link } from '@tiptap/extension-link'
-import { Image } from '@tiptap/extension-image'
-import { HorizontalRule } from '@tiptap/extension-horizontal-rule'
-import { CodeBlockLowlight } from '@tiptap/extension-code-block-lowlight'
-import { Color } from '@tiptap/extension-color'
-import type { Editor } from '@tiptap/react'
+import { chatCRUDService } from '@/api/chat-service'
+import { createChatTransport } from '@/api/chat-transport'
import { useGraphStore } from '@/stores/graph-store'
import { useChatState } from '@/stores/use-chat-store'
import { useParams } from '@tanstack/react-router'
import { queryKeys } from '@/api/query-keys'
+import { toUIMessage } from '@/types/chat'
-const markdownToHtml = (markdown: string) => {
- return marked(markdown, {
- breaks: true, // Convert \n to <br>
- gfm: true, // Enable GitHub Flavored Markdown
- async: false
- })
-}
-
-interface UseChatOptions {
- onContentUpdate: (content: any) => void
- onSuccess?: () => void
- editor?: Editor
-}
-
-export const useChat = ({ onContentUpdate, onSuccess, editor }: UseChatOptions) => {
- const [isAiLoading, setIsAiLoading] = useState(false)
- const [customPrompt, setCustomPrompt] = useState('')
+export const useChat = () => {
const selectedNodes = useGraphStore((s) => s.selectedNodes)
const currentChatId = useChatState((s) => s.currentChatId)
const setCurrentChatId = useChatState((s) => s.setCurrentChatId)
const deleteChat = useChatState((s) => s.deleteChat)
- const addMessage = useChatState((s) => s.addMessage)
const { investigationId } = useParams({ strict: false })
const queryClient = useQueryClient()
+ const transport = useMemo(
+ () => (currentChatId ? createChatTransport(currentChatId) : undefined),
+ [currentChatId]
+ )
+
// Fetch current chat data with messages
const { data: currentChat, isLoading: isLoadingChat } = useQuery({
queryKey: queryKeys.chats.detail(currentChatId!),
queryFn: () => chatCRUDService.getById(currentChatId!),
enabled: !!currentChatId,
- refetchOnWindowFocus: false
+ refetchOnWindowFocus: false,
})
+ // AI SDK useChat
+ const {
+ messages,
+ status,
+ sendMessage,
+ stop,
+ setMessages,
+ } = useAIChat({
+ id: currentChatId ?? undefined,
+ transport,
+ onFinish: () => {
+ if (currentChatId) {
+ queryClient.invalidateQueries({ queryKey: queryKeys.chats.detail(currentChatId) })
+ }
+ },
+ onError: (error) => {
+ console.error('Chat error:', error)
+ toast.error(
+ 'Failed to get AI response: ' + (error instanceof Error ? error.message : 'Unknown error')
+ )
+ },
+ })
+
+ // Sync server-loaded messages when switching chats
+ const prevChatIdRef = useRef(currentChatId)
+ useEffect(() => {
+ if (currentChatId !== prevChatIdRef.current) {
+ setMessages([])
+ prevChatIdRef.current = currentChatId
+ }
+ }, [currentChatId, setMessages])
+
+ useEffect(() => {
+ if (currentChat?.messages?.length) {
+ setMessages(currentChat.messages.map(toUIMessage))
+ }
+ }, [currentChat, setMessages])
+
// Mutation to create a new chat
const createChatMutation = useMutation({
mutationFn: async ({ title, description }: { title: string; description?: string }) => {
const chatData = {
title,
description: description || '',
- investigation_id: investigationId
+ investigation_id: investigationId,
}
return await chatCRUDService.create(JSON.stringify(chatData))
},
onSuccess: (data) => {
- // Add the new chat to the store
- // Invalidate chats query to refresh the list
- queryClient.invalidateQueries({ queryKey: queryKeys.chats.detail(currentChatId!) })
+ queryClient.invalidateQueries({ queryKey: queryKeys.chats.list })
setCurrentChatId(data.id)
toast.success('New chat created successfully!')
},
@@ -75,7 +89,7 @@ export const useChat = ({ onContentUpdate, onSuccess, editor }: UseChatOptions)
toast.error(
'Failed to create chat: ' + (error instanceof Error ? error.message : 'Unknown error')
)
- }
+ },
})
// Mutation to delete a chat
@@ -85,8 +99,6 @@ export const useChat = ({ onContentUpdate, onSuccess, editor }: UseChatOptions)
return await chatCRUDService.delete(chatId)
},
onSuccess: () => {
- // Remove the chat from the store
- // Invalidate chats query to refresh the list
queryClient.invalidateQueries({ queryKey: queryKeys.chats.list })
toast.success('Chat deleted successfully!')
},
@@ -95,134 +107,67 @@ export const useChat = ({ onContentUpdate, onSuccess, editor }: UseChatOptions)
toast.error(
'Failed to delete chat: ' + (error instanceof Error ? error.message : 'Unknown error')
)
- }
+ },
})
- const aiCompletionMutation = useMutation({
- mutationFn: async ({ chatId, prompt }: { chatId: string; prompt: string }) => {
- setIsAiLoading(true)
- try {
- return await chatService.streamChat(
- chatId,
- prompt,
- (content) => {
- const htmlContent = markdownToHtml(content)
- const extensions = [
- StarterKit.configure({
- horizontalRule: false,
- codeBlock: false,
- paragraph: { HTMLAttributes: { class: 'text-node' } },
- heading: { HTMLAttributes: { class: 'heading-node' } },
- blockquote: { HTMLAttributes: { class: 'block-node' } },
- bulletList: { HTMLAttributes: { class: 'list-node' } },
- orderedList: { HTMLAttributes: { class: 'list-node' } },
- code: { HTMLAttributes: { class: 'inline', spellcheck: 'false' } },
- dropcursor: { width: 2, class: 'ProseMirror-dropcursor border' }
- }),
- Link,
- Underline,
- Image,
- Color,
- TextStyle,
- HorizontalRule,
- CodeBlockLowlight,
- Typography,
- Placeholder.configure({ placeholder: 'Enter your analysis...' })
- ]
- const jsonContent = generateJSON(htmlContent, extensions)
-
- if (editor) {
- // If we have an editor instance, use its commands
- editor.commands.setContent(jsonContent)
- onContentUpdate(jsonContent)
- } else {
- // Otherwise, use the callback
- onContentUpdate(content)
- }
- },
- selectedNodes
- )
- } catch (error) {
- console.error('Error in chat stream:', error)
- throw error
- } finally {
- setIsAiLoading(false)
- }
- },
- onSuccess: () => {
- onSuccess?.()
- // Invalidate the current chat query to refresh messages
- if (currentChatId) {
- onContentUpdate('')
- queryClient.invalidateQueries({ queryKey: queryKeys.chats.detail(currentChatId) })
- }
- },
- onError: (error) => {
- console.error('Chat error:', error)
- toast.error(
- 'Failed to get AI completion: ' + (error instanceof Error ? error.message : 'Unknown error')
- )
+ const handleSendMessage = async (text: string) => {
+ if (!text.trim()) {
+ toast.error('Please enter a prompt')
+ return
}
- })
- const handleCustomPrompt = async () => {
- // Case where no chat is created - create a new chat first
+ // Auto-create chat if none exists
if (!currentChatId) {
const chatTitle =
selectedNodes.length > 0
- ? `${selectedNodes
+ ? selectedNodes
.map((node) => node.data.label)
.join(', ')
- .slice(0, 100)}`
+ .slice(0, 100)
: 'New Chat'
try {
const newChat = await createChatMutation.mutateAsync({
title: chatTitle,
- description: 'Chat created from analysis'
+ description: 'Chat created from analysis',
})
- // After creating the chat, proceed with the prompt
- if (newChat?.id) {
- await handlePromptWithChat(newChat.id)
- }
+ if (!newChat?.id) return
+ // sendMessage will fire once transport updates via the new currentChatId
+ // but we need to wait for the next render, so we queue the send
+ queuedMessageRef.current = text
} catch (error) {
console.error('Failed to create chat:', error)
- return
}
- } else {
- addMessage({
- content: customPrompt.trim(),
- context: selectedNodes.map((node) => node.data.label),
- is_bot: false,
- id: new Date().getTime().toString(),
- created_at: new Date().toISOString(),
- chatId: currentChatId
- })
- await handlePromptWithChat(currentChatId)
+ return
}
+
+ sendMessage({ role: 'user', parts: [{ type: 'text', text }] })
}
- const handlePromptWithChat = async (chatId: string) => {
- if (!customPrompt.trim()) {
- toast.error('Please enter a prompt')
- return
+ // Handle queued message after chat creation
+ const queuedMessageRef = useRef<string | null>(null)
+ useEffect(() => {
+ if (queuedMessageRef.current && currentChatId && transport) {
+ const text = queuedMessageRef.current
+ queuedMessageRef.current = null
+ // Small delay to allow transport to settle
+ setTimeout(() => {
+ sendMessage({ role: 'user', parts: [{ type: 'text', text }] })
+ }, 50)
}
- const fullPrompt = `${customPrompt}`
- setCustomPrompt('')
- aiCompletionMutation.mutate({ chatId, prompt: fullPrompt })
- }
+ }, [currentChatId, transport, sendMessage])
const createNewChat = async (title?: string) => {
const chatTitle =
title ||
(selectedNodes.length > 0
- ? `${selectedNodes[0].data.label}`
- : 'New Chat #' + Math.floor(Math.random() * 900) + 1000)
+ ? selectedNodes[0].data.label
+ : 'New Chat #' + (Math.floor(Math.random() * 900) + 1000))
try {
await createChatMutation.mutateAsync({
title: chatTitle,
- description: 'Chat created from analysis'
+ description: 'Chat created from analysis',
})
} catch (error) {
console.error('Failed to create new chat:', error)
@@ -246,17 +191,18 @@ export const useChat = ({ onContentUpdate, onSuccess, editor }: UseChatOptions)
}
return {
- isAiLoading,
- customPrompt,
- setCustomPrompt,
- handleCustomPrompt,
- createChatMutation,
- deleteChatMutation,
+ messages,
+ status,
+ sendMessage: handleSendMessage,
+ stop,
+ setMessages,
currentChat,
isLoadingChat,
createNewChat,
deleteCurrentChat,
switchToChat,
- currentChatId
+ currentChatId,
+ createChatMutation,
+ deleteChatMutation,
}
}
diff --git a/flowsint-app/src/stores/use-chat-store.tsx b/flowsint-app/src/stores/use-chat-store.tsx
index 20659e2f..410f8bdb 100644
--- a/flowsint-app/src/stores/use-chat-store.tsx
+++ b/flowsint-app/src/stores/use-chat-store.tsx
@@ -1,40 +1,27 @@
-import { ChatMessage } from '@/types'
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
interface ChatState {
currentChatId: string | null
setCurrentChatId: (chatId: string | null) => void
- messages: ChatMessage[]
- setMessages: (messages: ChatMessage[]) => void
- addMessage: (message: ChatMessage) => void
deleteChat: (chatId: string) => void
}
export const useChatState = create<ChatState>()(
persist(
- (set, _) => ({
+ (set) => ({
currentChatId: null,
- messages: [],
setCurrentChatId: (chatId) => set({ currentChatId: chatId }),
- setMessages: (messages) => set({ messages: messages }),
- addMessage: (message) =>
- set((state) => ({
- messages: [
- ...state.messages,
- { ...message, chatId: message.chatId || state.currentChatId || undefined }
- ]
- })),
deleteChat: (chatId) =>
set((state) => ({
- currentChatId: state.currentChatId === chatId ? null : state.currentChatId
- }))
+ currentChatId: state.currentChatId === chatId ? null : state.currentChatId,
+ })),
}),
{
name: 'chat-state-storage',
partialize: (state) => ({
- currentChatId: state.currentChatId
- })
+ currentChatId: state.currentChatId,
+ }),
}
)
)
diff --git a/flowsint-app/src/types/chat.ts b/flowsint-app/src/types/chat.ts
index 1fbdce2c..90b7ab50 100644
--- a/flowsint-app/src/types/chat.ts
+++ b/flowsint-app/src/types/chat.ts
@@ -1,3 +1,5 @@
+import type { UIMessage } from 'ai'
+
export interface ChatMessage {
id: string
content: string
@@ -14,3 +16,11 @@ export interface Chat {
created_at: string
last_updated_at: string
}
+
+export function toUIMessage(msg: ChatMessage): UIMessage {
+ return {
+ id: msg.id,
+ role: msg.is_bot ? 'assistant' : 'user',
+ parts: [{ type: 'text', text: msg.content }],
+ }
+}
diff --git a/flowsint-core/src/flowsint_core/core/llm/protocol.py b/flowsint-core/src/flowsint_core/core/llm/protocol.py
index c7a993c2..3a91c2da 100644
--- a/flowsint-core/src/flowsint_core/core/llm/protocol.py
+++ b/flowsint-core/src/flowsint_core/core/llm/protocol.py
@@ -5,3 +5,4 @@ from .types import ChatMessage
class LLMProvider(Protocol):
async def stream(self, messages: List[ChatMessage]) -> AsyncIterator[str]: ...
+ async def complete(self, messages: List[ChatMessage]) -> str: ...
diff --git a/flowsint-core/src/flowsint_core/core/llm/providers/mistral.py b/flowsint-core/src/flowsint_core/core/llm/providers/mistral.py
index 0ab56224..7829fc4b 100644
--- a/flowsint-core/src/flowsint_core/core/llm/providers/mistral.py
+++ b/flowsint-core/src/flowsint_core/core/llm/providers/mistral.py
@@ -17,7 +17,7 @@ class MistralProvider:
self._client = Mistral(api_key=api_key)
self._model = model
- async def stream(self, messages: List[ChatMessage]) -> AsyncIterator[str]:
+ def _build_messages(self, messages: List[ChatMessage]):
from mistralai.models import UserMessage, AssistantMessage, SystemMessage
type_map = {
@@ -25,8 +25,10 @@ class MistralProvider:
MessageRole.USER: UserMessage,
MessageRole.ASSISTANT: AssistantMessage,
}
+ return [type_map[m.role](content=m.content) for m in messages]
- sdk_messages = [type_map[m.role](content=m.content) for m in messages]
+ async def stream(self, messages: List[ChatMessage]) -> AsyncIterator[str]:
+ sdk_messages = self._build_messages(messages)
response = await self._client.chat.stream_async(
model=self._model, messages=sdk_messages
@@ -35,3 +37,12 @@ class MistralProvider:
async for chunk in response:
if chunk.data.choices[0].delta.content is not None:
yield chunk.data.choices[0].delta.content
+
+ async def complete(self, messages: List[ChatMessage]) -> str:
+ sdk_messages = self._build_messages(messages)
+
+ response = await self._client.chat.complete_async(
+ model=self._model, messages=sdk_messages
+ )
+
+ return response.choices[0].message.content
diff --git a/flowsint-core/src/flowsint_core/core/llm/providers/openai.py b/flowsint-core/src/flowsint_core/core/llm/providers/openai.py
index ef63cf7f..90f8e2b0 100644
--- a/flowsint-core/src/flowsint_core/core/llm/providers/openai.py
+++ b/flowsint-core/src/flowsint_core/core/llm/providers/openai.py
@@ -10,10 +10,11 @@ class OpenAIProvider:
self._client = AsyncOpenAI(api_key=api_key)
self._model = model
+ def _build_messages(self, messages: List[ChatMessage]):
+ return [{"role": m.role.value, "content": m.content} for m in messages]
+
async def stream(self, messages: List[ChatMessage]) -> AsyncIterator[str]:
- sdk_messages = [
- {"role": m.role.value, "content": m.content} for m in messages
- ]
+ sdk_messages = self._build_messages(messages)
response = await self._client.chat.completions.create(
model=self._model, messages=sdk_messages, stream=True
@@ -22,3 +23,12 @@ class OpenAIProvider:
async for chunk in response:
if chunk.choices and chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
+
+ async def complete(self, messages: List[ChatMessage]) -> str:
+ sdk_messages = self._build_messages(messages)
+
+ response = await self._client.chat.completions.create(
+ model=self._model, messages=sdk_messages
+ )
+
+ return response.choices[0].message.content
diff --git a/flowsint-core/src/flowsint_core/core/models.py b/flowsint-core/src/flowsint_core/core/models.py
index 64bafe3f..be741fdc 100644
--- a/flowsint-core/src/flowsint_core/core/models.py
+++ b/flowsint-core/src/flowsint_core/core/models.py
@@ -1,24 +1,30 @@
import json
import uuid
from datetime import datetime, timezone
-from flowsint_core.core.types import Role
+
from sqlalchemy import (
- String,
- Text,
+ Boolean,
+ JSON,
+ Column,
DateTime,
+ Float,
ForeignKey,
Index,
- func,
- JSON,
- Column,
- Enum as SQLEnum,
LargeBinary,
+ String,
+ Text,
UniqueConstraint,
Uuid,
+ func,
+)
+from sqlalchemy import (
+ Enum as SQLEnum,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy.types import TypeDecorator
+
from flowsint_core.core.enums import EventLevel
+from flowsint_core.core.types import Role
class RoleListType(TypeDecorator):
@@ -48,17 +54,13 @@ class Feedback(Base):
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
content = mapped_column(Text, nullable=True)
- owner_id = mapped_column(
- Uuid, ForeignKey("profiles.id"), nullable=True
- )
+ owner_id = mapped_column(Uuid, ForeignKey("profiles.id"), nullable=True)
class Investigation(Base):
__tablename__ = "investigations"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
name = mapped_column(Text)
description = mapped_column(Text)
@@ -88,15 +90,13 @@ class Investigation(Base):
class Log(Base):
__tablename__ = "logs"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
content = mapped_column(JSON, nullable=True)
# Allow both server-side default and application-side timestamp
created_at = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
- server_default=func.now()
+ server_default=func.now(),
)
sketch_id = mapped_column(
Uuid,
@@ -109,9 +109,7 @@ class Log(Base):
class Profile(Base):
__tablename__ = "profiles"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
first_name = mapped_column(Text, nullable=True)
last_name = mapped_column(Text, nullable=True)
avatar_url = mapped_column(Text, nullable=True)
@@ -124,9 +122,7 @@ class Profile(Base):
class Scan(Base):
__tablename__ = "scans"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
sketch_id: Mapped[uuid.UUID] = mapped_column(
Uuid,
ForeignKey("sketches.id", onupdate="CASCADE", ondelete="CASCADE"),
@@ -148,9 +144,7 @@ class Scan(Base):
class Sketch(Base):
__tablename__ = "sketches"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
title = mapped_column(Text)
description = mapped_column(Text)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
@@ -204,9 +198,7 @@ class SketchesProfiles(Base):
class Flow(Base):
__tablename__ = "flows"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
name = mapped_column(Text, nullable=False)
description = mapped_column(Text, nullable=True)
category = mapped_column(JSON, nullable=True)
@@ -218,9 +210,7 @@ class Flow(Base):
class Analysis(Base):
__tablename__ = "analyses"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
title = mapped_column(Text, nullable=False)
description = mapped_column(Text, nullable=True)
content = mapped_column(JSON, nullable=True)
@@ -247,9 +237,7 @@ class Analysis(Base):
class Chat(Base):
__tablename__ = "chats"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
title = mapped_column(Text, nullable=False)
description = mapped_column(Text, nullable=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
@@ -275,9 +263,7 @@ class Chat(Base):
class ChatMessage(Base):
__tablename__ = "messages"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
content = mapped_column(JSON, nullable=True)
context = mapped_column(JSON, nullable=True)
is_bot: Mapped[bool] = mapped_column(default=False)
@@ -294,9 +280,7 @@ class ChatMessage(Base):
class Key(Base):
__tablename__ = "keys"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
name: Mapped[str] = mapped_column(String, nullable=False) # ex: "shodan", "whocy"
@@ -366,9 +350,7 @@ class InvestigationUserRole(Base):
class CustomType(Base):
__tablename__ = "custom_types"
- id: Mapped[uuid.UUID] = mapped_column(
- Uuid, primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
name: Mapped[str] = mapped_column(Text, nullable=False)
owner_id: Mapped[uuid.UUID] = mapped_column(
Uuid,
@@ -380,7 +362,9 @@ class CustomType(Base):
checksum: Mapped[str] = mapped_column(String, nullable=True)
description: Mapped[str] = mapped_column(Text, nullable=True)
created_at = mapped_column(DateTime(timezone=True), server_default=func.now())
- updated_at = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
+ updated_at = mapped_column(
+ DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
+ )
# Relationships
owner = relationship("Profile", foreign_keys=[owner_id])
@@ -395,17 +379,15 @@ class CustomType(Base):
class EnricherTemplate(Base):
__tablename__ = "enricher_templates"
- id: Mapped[uuid.UUID] = mapped_column(
- PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
- )
+ id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
name: Mapped[str] = mapped_column(Text, nullable=False)
- description: Mapped[str | None] = mapped_column(Text, nullable=True)
+ description: Mapped[str] = mapped_column(Text, nullable=True)
category: Mapped[str] = mapped_column(Text, nullable=False)
- version: Mapped[float] = mapped_column(nullable=False, default=1.0)
- content: Mapped[dict] = mapped_column(JSONB, nullable=False)
- is_public: Mapped[bool] = mapped_column(default=False)
+ version: Mapped[float] = mapped_column(Float, nullable=False, default=1.0)
+ content: Mapped[dict] = mapped_column(JSON, nullable=False)
+ is_public: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
owner_id: Mapped[uuid.UUID] = mapped_column(
- PGUUID(as_uuid=True),
+ Uuid,
ForeignKey("profiles.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
@@ -414,7 +396,6 @@ class EnricherTemplate(Base):
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
- # Relationships
owner = relationship("Profile", foreign_keys=[owner_id])
__table_args__ = (
diff --git a/flowsint-core/src/flowsint_core/core/repositories/__init__.py b/flowsint-core/src/flowsint_core/core/repositories/__init__.py
index 07bd191d..4fbcd2a0 100644
--- a/flowsint-core/src/flowsint_core/core/repositories/__init__.py
+++ b/flowsint-core/src/flowsint_core/core/repositories/__init__.py
@@ -10,6 +10,7 @@ from .log_repository import LogRepository
from .key_repository import KeyRepository
from .flow_repository import FlowRepository
from .custom_type_repository import CustomTypeRepository
+from .enricher_template_repository import EnricherTemplateRepository
__all__ = [
"BaseRepository",
@@ -23,4 +24,5 @@ __all__ = [
"KeyRepository",
"FlowRepository",
"CustomTypeRepository",
+ "EnricherTemplateRepository",
]
diff --git a/flowsint-core/src/flowsint_core/core/repositories/enricher_template_repository.py b/flowsint-core/src/flowsint_core/core/repositories/enricher_template_repository.py
new file mode 100644
index 00000000..46d279ac
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/repositories/enricher_template_repository.py
@@ -0,0 +1,88 @@
+"""Repository for EnricherTemplate model."""
+from typing import List, Optional
+from uuid import UUID
+
+from sqlalchemy import or_
+
+from ..models import EnricherTemplate
+from .base import BaseRepository
+
+
+class EnricherTemplateRepository(BaseRepository[EnricherTemplate]):
+ model = EnricherTemplate
+
+ def get_by_owner_or_public(
+ self, owner_id: UUID, category: Optional[str] = None
+ ) -> List[EnricherTemplate]:
+ query = self._db.query(EnricherTemplate).filter(
+ or_(
+ EnricherTemplate.owner_id == owner_id,
+ EnricherTemplate.is_public,
+ )
+ )
+ if category:
+ query = query.filter(EnricherTemplate.category == category)
+ return query.order_by(EnricherTemplate.created_at.desc()).all()
+
+ def get_by_owner(
+ self, owner_id: UUID, category: Optional[str] = None
+ ) -> List[EnricherTemplate]:
+ query = self._db.query(EnricherTemplate).filter(
+ EnricherTemplate.owner_id == owner_id
+ )
+ if category:
+ query = query.filter(EnricherTemplate.category == category)
+ return query.order_by(EnricherTemplate.created_at.desc()).all()
+
+ def get_by_id_and_owner_or_public(
+ self, template_id: UUID, user_id: UUID
+ ) -> Optional[EnricherTemplate]:
+ return (
+ self._db.query(EnricherTemplate)
+ .filter(
+ EnricherTemplate.id == template_id,
+ or_(
+ EnricherTemplate.owner_id == user_id,
+ EnricherTemplate.is_public,
+ ),
+ )
+ .first()
+ )
+
+ def get_by_id_and_owner(
+ self, template_id: UUID, owner_id: UUID
+ ) -> Optional[EnricherTemplate]:
+ return (
+ self._db.query(EnricherTemplate)
+ .filter(
+ EnricherTemplate.id == template_id,
+ EnricherTemplate.owner_id == owner_id,
+ )
+ .first()
+ )
+
+ def find_by_name_and_owner_or_public(
+ self, name: str, user_id: UUID
+ ) -> Optional[EnricherTemplate]:
+ return (
+ self._db.query(EnricherTemplate)
+ .filter(
+ EnricherTemplate.name == name,
+ or_(
+ EnricherTemplate.owner_id == user_id,
+ EnricherTemplate.is_public,
+ ),
+ )
+ .first()
+ )
+
+ def find_by_name_and_owner(
+ self, name: str, owner_id: UUID, exclude_id: Optional[UUID] = None
+ ) -> Optional[EnricherTemplate]:
+ query = self._db.query(EnricherTemplate).filter(
+ EnricherTemplate.owner_id == owner_id,
+ EnricherTemplate.name == name,
+ )
+ if exclude_id:
+ query = query.filter(EnricherTemplate.id != exclude_id)
+ return query.first()
diff --git a/flowsint-core/src/flowsint_core/core/services/__init__.py b/flowsint-core/src/flowsint_core/core/services/__init__.py
index bf20d671..3ae586ad 100644
--- a/flowsint-core/src/flowsint_core/core/services/__init__.py
+++ b/flowsint-core/src/flowsint_core/core/services/__init__.py
@@ -27,6 +27,8 @@ from .flow_service import FlowService, create_flow_service
from .custom_type_service import CustomTypeService, create_custom_type_service
from .type_registry_service import TypeRegistryService, create_type_registry_service
from .enricher_service import EnricherService, create_enricher_service
+from .enricher_template_service import EnricherTemplateService, create_enricher_template_service
+from .template_generator_service import TemplateGeneratorService, create_template_generator_service
from .vault_service import VaultService, create_vault_service
__all__ = [
@@ -68,6 +70,10 @@ __all__ = [
"create_type_registry_service",
"EnricherService",
"create_enricher_service",
+ "EnricherTemplateService",
+ "create_enricher_template_service",
+ "TemplateGeneratorService",
+ "create_template_generator_service",
# Vault
"VaultService",
"create_vault_service",
diff --git a/flowsint-core/src/flowsint_core/core/services/chat_service.py b/flowsint-core/src/flowsint_core/core/services/chat_service.py
index 381d1972..f9afeea0 100644
--- a/flowsint-core/src/flowsint_core/core/services/chat_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/chat_service.py
@@ -224,11 +224,21 @@ class ChatService(BaseService):
llm_messages: List[LLMChatMessage],
provider: LLMProvider,
) -> AsyncIterator[str]:
+ import uuid as _uuid
+
+ message_id = str(_uuid.uuid4())
+ text_id = str(_uuid.uuid4())
accumulated: list[str] = []
+ yield f"data: {json.dumps({'type': 'start', 'messageId': message_id})}\n\n"
+ yield f"data: {json.dumps({'type': 'text-start', 'id': text_id})}\n\n"
+
async for token in provider.stream(llm_messages):
accumulated.append(token)
- yield f"data: {json.dumps({'content': token})}\n\n"
+ yield f"data: {json.dumps({'type': 'text-delta', 'id': text_id, 'delta': token})}\n\n"
+
+ yield f"data: {json.dumps({'type': 'text-end', 'id': text_id})}\n\n"
+ yield f"data: {json.dumps({'type': 'finish'})}\n\n"
self.add_bot_message(chat_id, "".join(accumulated))
diff --git a/flowsint-core/src/flowsint_core/core/services/enricher_service.py b/flowsint-core/src/flowsint_core/core/services/enricher_service.py
index 0bd7a9b7..52484e48 100644
--- a/flowsint-core/src/flowsint_core/core/services/enricher_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/enricher_service.py
@@ -7,7 +7,7 @@ from uuid import UUID
from sqlalchemy.orm import Session
-from ..repositories import CustomTypeRepository
+from ..repositories import CustomTypeRepository, EnricherTemplateRepository
from .base import BaseService
@@ -16,9 +16,16 @@ class EnricherService(BaseService):
Service for enricher operations and listing.
"""
- def __init__(self, db: Session, custom_type_repo: CustomTypeRepository, **kwargs):
+ def __init__(
+ self,
+ db: Session,
+ custom_type_repo: CustomTypeRepository,
+ enricher_template_repo: EnricherTemplateRepository,
+ **kwargs,
+ ):
super().__init__(db, **kwargs)
self._custom_type_repo = custom_type_repo
+ self._enricher_template_repo = enricher_template_repo
def get_enrichers(
self, category: Optional[str], user_id: UUID, enricher_registry
@@ -35,9 +42,19 @@ class EnricherService(BaseService):
return enricher_registry.list_by_input_type(category, exclude=["n8n_connector"])
+ def get_all_enrichers(
+ self, category: Optional[str], user_id: UUID, enricher_registry
+ ) -> list:
+ base_enrichers = self.get_enrichers(category, user_id, enricher_registry)
+ template_enrichers = self._enricher_template_repo.get_by_owner(
+ user_id, category
+ )
+ return [*base_enrichers, *template_enrichers]
+
def create_enricher_service(db: Session) -> EnricherService:
return EnricherService(
db=db,
custom_type_repo=CustomTypeRepository(db),
+ enricher_template_repo=EnricherTemplateRepository(db),
)
diff --git a/flowsint-core/src/flowsint_core/core/services/enricher_template_service.py b/flowsint-core/src/flowsint_core/core/services/enricher_template_service.py
index bfca0f6a..a6b19292 100644
--- a/flowsint-core/src/flowsint_core/core/services/enricher_template_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/enricher_template_service.py
@@ -2,20 +2,29 @@
Enricher template service for managing enricher template operations.
"""
-from typing import Dict, List, Optional
+from typing import List, Optional
from uuid import UUID
-from sqlalchemy import or_
from sqlalchemy.orm import Session
from ..models import EnricherTemplate
+from ..repositories import EnricherTemplateRepository
from .base import BaseService
-from .exceptions import ConflictError, NotFoundError, PermissionDeniedError
+from .exceptions import ConflictError, NotFoundError
class EnricherTemplateService(BaseService):
"""Service for enricher template CRUD and lookup operations."""
+ def __init__(
+ self,
+ db: Session,
+ enricher_template_repo: EnricherTemplateRepository,
+ **kwargs,
+ ):
+ super().__init__(db, **kwargs)
+ self._repo = enricher_template_repo
+
def create_template(
self,
name: str,
@@ -37,7 +46,7 @@ class EnricherTemplateService(BaseService):
is_public=is_public,
owner_id=owner_id,
)
- self._add(template)
+ self._repo.add(template)
self._commit()
self._refresh(template)
return template
@@ -49,34 +58,11 @@ class EnricherTemplateService(BaseService):
include_public: bool = True,
) -> List[EnricherTemplate]:
if include_public:
- query = self._db.query(EnricherTemplate).filter(
- or_(
- EnricherTemplate.owner_id == owner_id,
- EnricherTemplate.is_public,
- )
- )
- else:
- query = self._db.query(EnricherTemplate).filter(
- EnricherTemplate.owner_id == owner_id
- )
-
- if category:
- query = query.filter(EnricherTemplate.category == category)
-
- return query.order_by(EnricherTemplate.created_at.desc()).all()
+ return self._repo.get_by_owner_or_public(owner_id, category)
+ return self._repo.get_by_owner(owner_id, category)
def get_template(self, template_id: UUID, user_id: UUID) -> EnricherTemplate:
- template = (
- self._db.query(EnricherTemplate)
- .filter(
- EnricherTemplate.id == template_id,
- or_(
- EnricherTemplate.owner_id == user_id,
- EnricherTemplate.is_public,
- ),
- )
- .first()
- )
+ template = self._repo.get_by_id_and_owner_or_public(template_id, user_id)
if not template:
raise NotFoundError("Template not found")
return template
@@ -84,14 +70,7 @@ class EnricherTemplateService(BaseService):
def get_owned_template(
self, template_id: UUID, owner_id: UUID
) -> EnricherTemplate:
- template = (
- self._db.query(EnricherTemplate)
- .filter(
- EnricherTemplate.id == template_id,
- EnricherTemplate.owner_id == owner_id,
- )
- .first()
- )
+ template = self._repo.get_by_id_and_owner(template_id, owner_id)
if not template:
raise NotFoundError("Template not found")
return template
@@ -147,31 +126,16 @@ class EnricherTemplateService(BaseService):
def delete_template(self, template_id: UUID, owner_id: UUID) -> None:
template = self.get_owned_template(template_id, owner_id)
- self._delete(template)
+ self._repo.delete(template)
self._commit()
def find_by_name(self, name: str, user_id: UUID) -> Optional[EnricherTemplate]:
- return (
- self._db.query(EnricherTemplate)
- .filter(
- EnricherTemplate.name == name,
- or_(
- EnricherTemplate.owner_id == user_id,
- EnricherTemplate.is_public,
- ),
- )
- .first()
- )
+ return self._repo.find_by_name_and_owner_or_public(name, user_id)
def list_by_category_for_user(
self, owner_id: UUID, category: Optional[str] = None
) -> List[EnricherTemplate]:
- query = self._db.query(EnricherTemplate).filter(
- EnricherTemplate.owner_id == owner_id
- )
- if category:
- query = query.filter(EnricherTemplate.category == category)
- return query.order_by(EnricherTemplate.created_at.desc()).all()
+ return self._repo.get_by_owner(owner_id, category)
def _check_duplicate_name(
self,
@@ -179,16 +143,14 @@ class EnricherTemplateService(BaseService):
owner_id: UUID,
exclude_id: Optional[UUID] = None,
) -> None:
- query = self._db.query(EnricherTemplate).filter(
- EnricherTemplate.owner_id == owner_id,
- EnricherTemplate.name == name,
- )
- if exclude_id:
- query = query.filter(EnricherTemplate.id != exclude_id)
- if query.first():
+ existing = self._repo.find_by_name_and_owner(name, owner_id, exclude_id)
+ if existing:
raise ConflictError(f"Template with name '{name}' already exists")
def create_enricher_template_service(db: Session) -> EnricherTemplateService:
"""Factory function to create an EnricherTemplateService instance."""
- return EnricherTemplateService(db=db)
+ return EnricherTemplateService(
+ db=db,
+ enricher_template_repo=EnricherTemplateRepository(db),
+ )
diff --git a/flowsint-core/src/flowsint_core/core/services/template_generator_service.py b/flowsint-core/src/flowsint_core/core/services/template_generator_service.py
new file mode 100644
index 00000000..31b65064
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/template_generator_service.py
@@ -0,0 +1,197 @@
+"""
+Service for AI-assisted enricher template generation.
+
+Uses an LLM to generate valid enricher template YAML from a free-text prompt.
+"""
+
+import os
+import re
+from uuid import UUID
+
+import yaml
+from sqlalchemy.orm import Session
+
+from ..llm import ChatMessage, MessageRole, create_llm_provider
+from .base import BaseService
+from .exceptions import ValidationError
+from .vault_service import VaultService
+from flowsint_core.templates.types import Template
+
+
+_SYSTEM_PROMPT = """\
+You are a YAML template generator for Flowsint enrichers. Given a user's description, \
+generate a valid enricher template in YAML format.
+
+## Template Schema
+
+A template has the following fields:
+
+### Required fields:
+- `name` (str): Unique name for the template (lowercase, hyphenated, e.g. "ip-api-lookup")
+- `category` (str): Category matching the input type (e.g. "Ip", "Domain", "Username", "Email")
+- `version` (float): Template version, start at 1.0
+- `input`: Input configuration
+ - `type` (str, required): The Flowsint type this template accepts (e.g. "Ip", "Domain", "Username", "Email")
+ - `key` (str, default "nodeLabel"): The attribute to extract from the input for use in the template URL/body
+- `request`: HTTP request configuration
+ - `method` (str): "GET" or "POST"
+ - `url` (str): URL with {{variable}} placeholders (e.g. "http://api.example.com/lookup/{{address}}")
+ - `headers` (dict, optional): HTTP headers, values can use {{variable}} or {{secrets.SECRET_NAME}} placeholders
+ - `params` (dict, optional): Query parameters
+ - `body` (str, optional): Request body for POST requests
+ - `timeout` (float, default 30): Request timeout in seconds (1-300)
+- `response`: Response parsing configuration
+ - `expect` (str): Expected format - "json", "xml", or "text"
+ - `map` (dict): Mapping from output type field names to response paths (supports dot notation for nested fields)
+- `output`: Output configuration
+ - `type` (str, required): The Flowsint type to return (e.g. "Ip", "Domain", "SocialAccount")
+ - `is_array` (bool, default false): Whether the response produces multiple outputs
+ - `array_path` (str, optional): Dot-notation path to the array in response (e.g. "data.results")
+
+### Optional fields:
+- `description` (str): Human-readable description of what the template does
+- `secrets`: List of secrets the template requires (fetched from user's vault)
+ - `name` (str): Secret name, used as {{secrets.NAME}} in the template
+ - `required` (bool, default true): Whether the secret is required
+ - `description` (str, optional): What the secret is used for
+- `retry`: Retry configuration for failed requests
+ - `max_retries` (int, default 3, 0-10)
+ - `backoff_factor` (float, default 0.5, 0.1-10.0)
+ - `retry_on_status` (list[int], default [429, 500, 502, 503, 504])
+
+## Variable Placeholders
+
+- `{{key}}` — replaced with the input value (where `key` is `input.key`, e.g. `{{address}}` for IP)
+- `{{secrets.SECRET_NAME}}` — replaced with the secret value from the user's vault
+
+## Examples
+
+### Example 1: Simple GET lookup (no auth)
+```yaml
+name: ip-api-lookup
+category: Ip
+version: 1.0
+input:
+ type: Ip
+ key: address
+request:
+ method: GET
+ url: http://ip-api.com/json/{{address}}
+ params:
+ fields: query,status,country,city,lat,lon,isp
+ timeout: 30
+response:
+ expect: json
+ map:
+ address: query
+ latitude: lat
+ longitude: lon
+ country: country
+ city: city
+ isp: isp
+output:
+ type: Ip
+```
+
+### Example 2: With API key authentication
+```yaml
+name: api-with-secrets
+category: Ip
+version: 1.0
+input:
+ type: Ip
+ key: address
+secrets:
+ - name: API_KEY
+ required: true
+ description: API key for the service
+request:
+ method: GET
+ url: https://api.example.com/lookup/{{address}}
+ headers:
+ Authorization: "Bearer {{secrets.API_KEY}}"
+ timeout: 30
+output:
+ type: Ip
+response:
+ expect: json
+ map:
+ address: ip
+ country: country
+```
+
+## Instructions
+
+- Output ONLY the YAML template. No explanations, no markdown fences, no extra text.
+- Infer the appropriate category, input type, and output type from the user's description.
+- Use realistic field mappings based on common API response structures.
+- If the API likely requires authentication, include a `secrets` section.
+- Keep the template simple and focused on what the user asked for.
+"""
+
+
+def _extract_yaml(text: str) -> str:
+ """Extract YAML content from LLM response, stripping markdown fences if present."""
+ # Try to extract from markdown code fences
+ match = re.search(r"```(?:ya?ml)?\s*\n(.*?)```", text, re.DOTALL)
+ if match:
+ return match.group(1).strip()
+ return text.strip()
+
+
+class TemplateGeneratorService(BaseService):
+ """Generates enricher template YAML from a free-text prompt using an LLM."""
+
+ def __init__(self, db: Session, vault_service: VaultService):
+ super().__init__(db=db)
+ self._vault_service = vault_service
+
+ def _get_llm_provider(self, owner_id: UUID):
+ provider_name = os.environ.get("LLM_PROVIDER", "mistral")
+ vault_key = f"{provider_name.upper()}_API_KEY"
+ api_key = self._vault_service.get_secret(owner_id, vault_key)
+ return create_llm_provider(provider=provider_name, api_key=api_key)
+
+ async def generate(self, prompt: str, owner_id: UUID) -> str:
+ """Generate an enricher template YAML from a free-text description.
+
+ Args:
+ prompt: User's free-text description of the desired enricher.
+ owner_id: ID of the user (for vault-based LLM API key).
+
+ Returns:
+ Raw YAML string of the generated template.
+
+ Raises:
+ ValidationError: If the LLM output is not valid YAML or doesn't
+ match the Template schema.
+ """
+ provider = self._get_llm_provider(owner_id)
+
+ messages = [
+ ChatMessage(role=MessageRole.SYSTEM, content=_SYSTEM_PROMPT),
+ ChatMessage(role=MessageRole.USER, content=prompt),
+ ]
+
+ response = await provider.complete(messages)
+ yaml_str = _extract_yaml(response)
+
+ # Validate the YAML
+ try:
+ parsed = yaml.safe_load(yaml_str)
+ except yaml.YAMLError as e:
+ raise ValidationError(f"LLM produced invalid YAML: {e}")
+
+ if not isinstance(parsed, dict):
+ raise ValidationError("LLM produced non-object YAML output")
+
+ try:
+ Template(**parsed)
+ except Exception as e:
+ raise ValidationError(f"Generated template failed validation: {e}")
+
+ return yaml_str
+
+
+def create_template_generator_service(db: Session) -> TemplateGeneratorService:
+ return TemplateGeneratorService(db=db, vault_service=VaultService(db=db))
diff --git a/flowsint-core/src/flowsint_core/core/template_enricher.py b/flowsint-core/src/flowsint_core/core/template_enricher.py
index 430f6978..59278258 100644
--- a/flowsint-core/src/flowsint_core/core/template_enricher.py
+++ b/flowsint-core/src/flowsint_core/core/template_enricher.py
@@ -527,9 +527,12 @@ class TemplateEnricher(Enricher):
def postprocess(self, results: List[Any], input_data: List[Any] = []) -> List[Any]:
"""Log results and return them."""
- for res in results:
+ for input, output in zip(input_data, results):
+ self.create_node(input)
+ self.create_node(output)
+ self.create_relationship(input, output, "HAS_SOCIAL_ACCOUNT")
self.log_graph_message(
- f"Template '{self.template.name}' produced: {res.model_dump()}"
+ f"[{self.template.name.upper()}] {input.nodeLabel} -> {output.nodeLabel}"
)
return results
diff --git a/flowsint-core/src/flowsint_core/tasks/enricher.py b/flowsint-core/src/flowsint_core/tasks/enricher.py
index 40ddcd32..f08ede65 100644
--- a/flowsint-core/src/flowsint_core/tasks/enricher.py
+++ b/flowsint-core/src/flowsint_core/tasks/enricher.py
@@ -4,24 +4,18 @@ from typing import List, Optional
from celery import states
from flowsint_enrichers import ENRICHER_REGISTRY, load_all_enrichers
-from sqlalchemy import or_
from sqlalchemy.orm import Session
-from flowsint_core.core.template_enricher import TemplateEnricher
-from flowsint_core.templates.types import Template
from flowsint_core.utils import to_json_serializable
from ..core.celery import celery
from ..core.enums import EventLevel
from ..core.logger import Logger
-from ..core.models import EnricherTemplate, Scan
-from ..core.postgre_db import SessionLocal, get_db
-from ..core.services import create_vault_service
from ..core.models import Scan
-from sqlalchemy.orm import Session
-from ..core.logger import Logger
-from ..core.enums import EventLevel
-from flowsint_core.utils import to_json_serializable
+from ..core.postgre_db import SessionLocal, get_db
+from ..core.services import create_enricher_template_service, create_vault_service
+from ..core.template_enricher import TemplateEnricher
+from ..templates.types import Template
# Auto-discover and register all enrichers
load_all_enrichers()
@@ -34,7 +28,7 @@ def run_enricher(
self,
enricher_name: str,
serialized_objects: List[dict],
- sketch_id: str,
+ sketch_id: str | None,
owner_id: Optional[str] = None,
):
session = SessionLocal()
@@ -83,6 +77,7 @@ def run_enricher(
except Exception as ex:
session.rollback()
error_logs = f"An error occurred: {str(ex)}"
+ print(f"Error in task: {error_logs}")
scan = session.query(Scan).filter(Scan.id == uuid.UUID(self.request.id)).first()
if scan:
@@ -97,14 +92,15 @@ def run_enricher(
session.close()
[email protected](name="run_enricher_from_template", bind=True)
-def run_enricher_from_template(
[email protected](name="run_template_enricher", bind=True)
+def run_template_enricher(
self,
- enricher_name: str,
+ template_name: str,
serialized_objects: List[dict],
- sketch_id: str,
- owner_id: Optional[str] = None,
+ sketch_id: str | None,
+ owner_id: str,
):
+ """Run an enricher defined by a YAML template stored in the database."""
session = SessionLocal()
try:
@@ -118,37 +114,34 @@ def run_enricher_from_template(
session.add(scan)
session.commit()
- # Create vault instance if owner_id is provided
+ # Resolve vault for secrets
vault = None
- if owner_id:
- try:
- vault = Vault(session, uuid.UUID(owner_id))
- except Exception as e:
- raise ValueError(f"Error creating vault: {e}")
-
- template = (
- db.query(EnricherTemplate)
- .filter(
- EnricherTemplate.name == enricher_name,
- or_(
- EnricherTemplate.owner_id == owner_id,
- EnricherTemplate.is_public,
- ),
+ try:
+ vault = create_vault_service(session).for_user(uuid.UUID(owner_id))
+ except Exception as e:
+ Logger.error(
+ sketch_id, {"message": f"Failed to create vault: {str(e)}"}
)
- .first()
+
+ # Load template from database
+ template_service = create_enricher_template_service(session)
+ db_template = template_service.find_by_name(
+ template_name, uuid.UUID(owner_id)
)
+ if not db_template:
+ raise ValueError(
+ f"Template '{template_name}' not found for user {owner_id}"
+ )
- if not template:
- raise ValueError(f"Enricher {enricher_name} not found.")
+ template = Template(**db_template.content)
- content = template.content
- template = Template(**content)
- assert isinstance(template, Template)
enricher = TemplateEnricher(
- sketch_id=sketch_id, scan_id=str(scan_id), template=template
+ template=template,
+ sketch_id=sketch_id,
+ scan_id=str(scan_id),
+ vault=vault,
)
- # Deserialize objects back into Pydantic models
- # The preprocess method in Enricher will handle these already-parsed objects
+
results = asyncio.run(enricher.execute(values=serialized_objects))
scan.status = EventLevel.COMPLETED
@@ -160,9 +153,13 @@ def run_enricher_from_template(
except Exception as ex:
session.rollback()
error_logs = f"An error occurred: {str(ex)}"
- print(f"Error in task: {error_logs}")
+ print(f"Error in template task: {error_logs}")
- scan = session.query(Scan).filter(Scan.id == uuid.UUID(self.request.id)).first()
+ scan = (
+ session.query(Scan)
+ .filter(Scan.id == uuid.UUID(self.request.id))
+ .first()
+ )
if scan:
scan.status = EventLevel.FAILED
scan.results = {"error": error_logs}
diff --git a/flowsint-core/src/flowsint_core/tasks/event.py b/flowsint-core/src/flowsint_core/tasks/event.py
index 1ac81fa9..b79b0fe1 100644
--- a/flowsint-core/src/flowsint_core/tasks/event.py
+++ b/flowsint-core/src/flowsint_core/tasks/event.py
@@ -1,9 +1,11 @@
# tasks/logging.py
-from typing import Dict
-from ..core.celery import celery
-import redis
import logging
import os
+from typing import Dict
+
+import redis
+
+from ..core.celery import celery
from ..core.enums import EventLevel
from ..core.types import Event
@@ -24,7 +26,9 @@ def emit_event_task(log_id: str, sketch_id: str, log_type: EventLevel, content:
@celery.task(name="emit_status_event")
-def emit_status_event_task(log_id: str, sketch_id: str, log_type: EventLevel, content: Dict):
+def emit_status_event_task(
+ log_id: str, sketch_id: str, log_type: EventLevel, content: Dict
+):
"""Celery task to emit a status event (COMPLETED) to status channel"""
try:
event = Event(
diff --git a/flowsint-core/src/flowsint_core/tasks/flow.py b/flowsint-core/src/flowsint_core/tasks/flow.py
index 1e4ba9bf..ebce0256 100644
--- a/flowsint-core/src/flowsint_core/tasks/flow.py
+++ b/flowsint-core/src/flowsint_core/tasks/flow.py
@@ -1,16 +1,19 @@
import uuid
from typing import List, Optional
+
from celery import states
+from sqlalchemy.orm import Session
+
+from flowsint_core.utils import to_json_serializable
+
from ..core.celery import celery
+from ..core.enums import EventLevel
+from ..core.logger import Logger
+from ..core.models import Scan
from ..core.orchestrator import FlowOrchestrator
from ..core.postgre_db import SessionLocal, get_db
from ..core.services import create_vault_service
from ..core.types import FlowBranch
-from ..core.models import Scan
-from sqlalchemy.orm import Session
-from ..core.logger import Logger
-from ..core.enums import EventLevel
-from flowsint_core.utils import to_json_serializable
db: Session = next(get_db())
diff --git a/flowsint-core/tests/factories.py b/flowsint-core/tests/factories.py
index d8cbb891..c4557170 100644
--- a/flowsint-core/tests/factories.py
+++ b/flowsint-core/tests/factories.py
@@ -11,6 +11,7 @@ from flowsint_core.core.models import (
Chat,
ChatMessage,
CustomType,
+ EnricherTemplate,
Flow,
Investigation,
InvestigationUserRole,
@@ -183,3 +184,21 @@ class CustomTypeFactory(SQLAlchemyModelFactory):
)
status = "draft"
description = "Test custom type"
+
+
+class EnricherTemplateFactory(SQLAlchemyModelFactory):
+ class Meta:
+ model = EnricherTemplate
+ sqlalchemy_session_persistence = "commit"
+
+ id = factory.LazyFunction(uuid4)
+ name = factory.Sequence(lambda n: f"Template{n}")
+ description = "Test template"
+ category = "ip"
+ version = 1.0
+ content = factory.LazyFunction(
+ lambda: {"name": "Template", "request": {"url": "https://example.com"}}
+ )
+ is_public = False
+ owner = factory.SubFactory(ProfileFactory)
+ owner_id = factory.LazyAttribute(lambda o: o.owner.id)
diff --git a/flowsint-core/tests/services/__init__.py b/flowsint-core/tests/services/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/flowsint-core/tests/services/test_enricher_template_service.py b/flowsint-core/tests/services/test_enricher_template_service.py
new file mode 100644
index 00000000..04444873
--- /dev/null
+++ b/flowsint-core/tests/services/test_enricher_template_service.py
@@ -0,0 +1,271 @@
+"""Tests for EnricherTemplateService."""
+import pytest
+
+from tests.factories import EnricherTemplateFactory, ProfileFactory
+from flowsint_core.core.repositories import EnricherTemplateRepository
+from flowsint_core.core.services.enricher_template_service import (
+ EnricherTemplateService,
+)
+from flowsint_core.core.services.exceptions import ConflictError, NotFoundError
+
+
+class TestEnricherTemplateService:
+ def _setup(self, db_session):
+ ProfileFactory._meta.sqlalchemy_session = db_session
+ EnricherTemplateFactory._meta.sqlalchemy_session = db_session
+
+ def _make_service(self, db_session):
+ repo = EnricherTemplateRepository(db_session)
+ return EnricherTemplateService(db=db_session, enricher_template_repo=repo)
+
+ # -- create_template --
+
+ def test_create_template(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ service = self._make_service(db_session)
+
+ template = service.create_template(
+ name="My Template",
+ description="desc",
+ category="ip",
+ version=1.0,
+ content={"name": "My Template"},
+ is_public=False,
+ owner_id=user.id,
+ )
+
+ assert template.id is not None
+ assert template.name == "My Template"
+ assert template.owner_id == user.id
+
+ def test_create_template_duplicate_name_raises(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ EnricherTemplateFactory(owner=user, name="Dup")
+ service = self._make_service(db_session)
+
+ with pytest.raises(ConflictError):
+ service.create_template(
+ name="Dup",
+ description=None,
+ category="ip",
+ version=1.0,
+ content={"name": "Dup"},
+ is_public=False,
+ owner_id=user.id,
+ )
+
+ # -- list_templates --
+
+ def test_list_templates_includes_public(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ EnricherTemplateFactory(owner=user, name="Mine")
+ EnricherTemplateFactory(owner=other, name="Public", is_public=True)
+ EnricherTemplateFactory(owner=other, name="Private", is_public=False)
+ service = self._make_service(db_session)
+
+ results = service.list_templates(user.id, include_public=True)
+
+ names = {t.name for t in results}
+ assert "Mine" in names
+ assert "Public" in names
+ assert "Private" not in names
+
+ def test_list_templates_exclude_public(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ EnricherTemplateFactory(owner=user, name="Mine")
+ EnricherTemplateFactory(owner=other, name="Public", is_public=True)
+ service = self._make_service(db_session)
+
+ results = service.list_templates(user.id, include_public=False)
+
+ assert len(results) == 1
+ assert results[0].name == "Mine"
+
+ def test_list_templates_filter_by_category(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ EnricherTemplateFactory(owner=user, category="ip")
+ EnricherTemplateFactory(owner=user, category="domain")
+ service = self._make_service(db_session)
+
+ results = service.list_templates(user.id, category="ip")
+
+ assert len(results) == 1
+ assert results[0].category == "ip"
+
+ # -- get_template --
+
+ def test_get_template_own(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user)
+ service = self._make_service(db_session)
+
+ result = service.get_template(t.id, user.id)
+ assert result.id == t.id
+
+ def test_get_template_public(self, db_session):
+ self._setup(db_session)
+ owner = ProfileFactory()
+ viewer = ProfileFactory()
+ t = EnricherTemplateFactory(owner=owner, is_public=True)
+ service = self._make_service(db_session)
+
+ result = service.get_template(t.id, viewer.id)
+ assert result.id == t.id
+
+ def test_get_template_not_found(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ t = EnricherTemplateFactory(owner=other, is_public=False)
+ service = self._make_service(db_session)
+
+ with pytest.raises(NotFoundError):
+ service.get_template(t.id, user.id)
+
+ # -- get_owned_template --
+
+ def test_get_owned_template(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user)
+ service = self._make_service(db_session)
+
+ result = service.get_owned_template(t.id, user.id)
+ assert result.id == t.id
+
+ def test_get_owned_template_wrong_owner(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user)
+ service = self._make_service(db_session)
+
+ with pytest.raises(NotFoundError):
+ service.get_owned_template(t.id, other.id)
+
+ # -- update_template --
+
+ def test_update_template_fields(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user, name="Old", category="ip")
+ service = self._make_service(db_session)
+
+ updated = service.update_template(
+ t.id, user.id, {"name": "New", "category": "domain", "is_public": True}
+ )
+
+ assert updated.name == "New"
+ assert updated.category == "domain"
+ assert updated.is_public is True
+
+ def test_update_template_content(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user, name="T1", version=1.0)
+ service = self._make_service(db_session)
+
+ new_content = {"name": "T2", "category": "domain", "version": 2.0, "description": "new desc"}
+ updated = service.update_template(t.id, user.id, {"content": new_content})
+
+ assert updated.name == "T2"
+ assert updated.category == "domain"
+ assert updated.version == 2.0
+ assert updated.content == new_content
+
+ def test_update_template_duplicate_name_raises(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ EnricherTemplateFactory(owner=user, name="Taken")
+ t = EnricherTemplateFactory(owner=user, name="Original")
+ service = self._make_service(db_session)
+
+ with pytest.raises(ConflictError):
+ service.update_template(t.id, user.id, {"name": "Taken"})
+
+ def test_update_template_not_owner_raises(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user)
+ service = self._make_service(db_session)
+
+ with pytest.raises(NotFoundError):
+ service.update_template(t.id, other.id, {"name": "Hack"})
+
+ # -- delete_template --
+
+ def test_delete_template(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user)
+ service = self._make_service(db_session)
+
+ service.delete_template(t.id, user.id)
+
+ with pytest.raises(NotFoundError):
+ service.get_owned_template(t.id, user.id)
+
+ def test_delete_template_not_owner_raises(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ t = EnricherTemplateFactory(owner=user)
+ service = self._make_service(db_session)
+
+ with pytest.raises(NotFoundError):
+ service.delete_template(t.id, other.id)
+
+ # -- find_by_name --
+
+ def test_find_by_name_own(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ EnricherTemplateFactory(owner=user, name="Finder")
+ service = self._make_service(db_session)
+
+ result = service.find_by_name("Finder", user.id)
+ assert result is not None
+ assert result.name == "Finder"
+
+ def test_find_by_name_public(self, db_session):
+ self._setup(db_session)
+ owner = ProfileFactory()
+ viewer = ProfileFactory()
+ EnricherTemplateFactory(owner=owner, name="PubFind", is_public=True)
+ service = self._make_service(db_session)
+
+ result = service.find_by_name("PubFind", viewer.id)
+ assert result is not None
+
+ def test_find_by_name_not_found(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ service = self._make_service(db_session)
+
+ result = service.find_by_name("Ghost", user.id)
+ assert result is None
+
+ # -- list_by_category_for_user --
+
+ def test_list_by_category_for_user(self, db_session):
+ self._setup(db_session)
+ user = ProfileFactory()
+ other = ProfileFactory()
+ EnricherTemplateFactory(owner=user, category="ip")
+ EnricherTemplateFactory(owner=user, category="domain")
+ EnricherTemplateFactory(owner=other, category="ip", is_public=True)
+ service = self._make_service(db_session)
+
+ results = service.list_by_category_for_user(user.id, category="ip")
+
+ assert len(results) == 1
+ assert results[0].owner_id == user.id
diff --git a/flowsint-core/tests/services/test_template_generator_service.py b/flowsint-core/tests/services/test_template_generator_service.py
new file mode 100644
index 00000000..ae92e0d7
--- /dev/null
+++ b/flowsint-core/tests/services/test_template_generator_service.py
@@ -0,0 +1,169 @@
+"""Tests for TemplateGeneratorService."""
+
+from unittest.mock import AsyncMock, MagicMock, patch
+from uuid import uuid4
+
+import pytest
+
+from flowsint_core.core.services.exceptions import ValidationError
+from flowsint_core.core.services.template_generator_service import (
+ TemplateGeneratorService,
+ _extract_yaml,
+)
+
+
+VALID_YAML = """\
+name: ip-api-lookup
+category: Ip
+version: 1.0
+input:
+ type: Ip
+ key: address
+request:
+ method: GET
+ url: http://ip-api.com/json/{{address}}
+ timeout: 30
+response:
+ expect: json
+ map:
+ address: query
+ country: country
+output:
+ type: Ip
+"""
+
+VALID_YAML_WITH_FENCES = f"""\
+Here is the template:
+
+```yaml
+{VALID_YAML}```
+
+Let me know if you need changes.
+"""
+
+INVALID_YAML = """\
+name: bad-template
+this is: [not: valid: yaml:
+ broken:
+"""
+
+MISSING_FIELDS_YAML = """\
+name: incomplete
+category: Ip
+version: 1.0
+"""
+
+
+def _make_service():
+ """Create a TemplateGeneratorService with mocked dependencies."""
+ db = MagicMock()
+ vault_service = MagicMock()
+ vault_service.get_secret.return_value = "fake-api-key"
+ return TemplateGeneratorService(db=db, vault_service=vault_service)
+
+
+class TestExtractYaml:
+ def test_plain_yaml(self):
+ assert _extract_yaml("name: foo\nversion: 1.0") == "name: foo\nversion: 1.0"
+
+ def test_strips_yaml_fences(self):
+ text = "```yaml\nname: foo\n```"
+ assert _extract_yaml(text) == "name: foo"
+
+ def test_strips_yml_fences(self):
+ text = "```yml\nname: foo\n```"
+ assert _extract_yaml(text) == "name: foo"
+
+ def test_strips_plain_fences(self):
+ text = "```\nname: foo\n```"
+ assert _extract_yaml(text) == "name: foo"
+
+ def test_strips_fences_with_surrounding_text(self):
+ text = "Here is the template:\n\n```yaml\nname: foo\n```\n\nDone."
+ assert _extract_yaml(text) == "name: foo"
+
+
+class TestTemplateGeneratorService:
+ @pytest.mark.asyncio
+ async def test_generate_valid_yaml(self):
+ service = _make_service()
+ mock_provider = MagicMock()
+ mock_provider.complete = AsyncMock(return_value=VALID_YAML)
+
+ with patch.object(service, "_get_llm_provider", return_value=mock_provider):
+ result = await service.generate("lookup IP geolocation", uuid4())
+
+ assert "name: ip-api-lookup" in result
+ assert "category: Ip" in result
+ # Verify provider.complete was called with system + user messages
+ mock_provider.complete.assert_called_once()
+ messages = mock_provider.complete.call_args[0][0]
+ assert len(messages) == 2
+ assert messages[0].role.value == "system"
+ assert messages[1].role.value == "user"
+ assert messages[1].content == "lookup IP geolocation"
+
+ @pytest.mark.asyncio
+ async def test_generate_strips_markdown_fences(self):
+ service = _make_service()
+ mock_provider = MagicMock()
+ mock_provider.complete = AsyncMock(return_value=VALID_YAML_WITH_FENCES)
+
+ with patch.object(service, "_get_llm_provider", return_value=mock_provider):
+ result = await service.generate("lookup IP geolocation", uuid4())
+
+ assert "```" not in result
+ assert "name: ip-api-lookup" in result
+
+ @pytest.mark.asyncio
+ async def test_generate_invalid_yaml_raises(self):
+ service = _make_service()
+ mock_provider = MagicMock()
+ mock_provider.complete = AsyncMock(return_value=INVALID_YAML)
+
+ with patch.object(service, "_get_llm_provider", return_value=mock_provider):
+ with pytest.raises(ValidationError, match="invalid YAML"):
+ await service.generate("do something", uuid4())
+
+ @pytest.mark.asyncio
+ async def test_generate_missing_fields_raises(self):
+ service = _make_service()
+ mock_provider = MagicMock()
+ mock_provider.complete = AsyncMock(return_value=MISSING_FIELDS_YAML)
+
+ with patch.object(service, "_get_llm_provider", return_value=mock_provider):
+ with pytest.raises(ValidationError, match="failed validation"):
+ await service.generate("do something", uuid4())
+
+ @pytest.mark.asyncio
+ async def test_generate_non_dict_yaml_raises(self):
+ service = _make_service()
+ mock_provider = MagicMock()
+ mock_provider.complete = AsyncMock(return_value="- item1\n- item2\n")
+
+ with patch.object(service, "_get_llm_provider", return_value=mock_provider):
+ with pytest.raises(ValidationError, match="non-object"):
+ await service.generate("do something", uuid4())
+
+ @pytest.mark.asyncio
+ async def test_system_prompt_contains_schema(self):
+ service = _make_service()
+ mock_provider = MagicMock()
+ mock_provider.complete = AsyncMock(return_value=VALID_YAML)
+
+ with patch.object(service, "_get_llm_provider", return_value=mock_provider):
+ await service.generate("test prompt", uuid4())
+
+ messages = mock_provider.complete.call_args[0][0]
+ system_prompt = messages[0].content
+ # Verify schema elements are present
+ assert "name" in system_prompt
+ assert "category" in system_prompt
+ assert "input" in system_prompt
+ assert "request" in system_prompt
+ assert "response" in system_prompt
+ assert "output" in system_prompt
+ assert "secrets" in system_prompt
+ # Verify examples are present
+ assert "ip-api-lookup" in system_prompt
+ assert "api-with-secrets" in system_prompt
diff --git a/yarn.lock b/yarn.lock
index bca603c7..bf7a2db2 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2,6 +2,41 @@
# yarn lockfile v1
+"@ai-sdk/[email protected]":
+ version "3.0.39"
+ resolved "https://registry.yarnpkg.com/@ai-sdk/gateway/-/gateway-3.0.39.tgz#59c86bd2d6b470342425097aa6556d9a7eacbf5d"
+ integrity sha512-SeCZBAdDNbWpVUXiYgOAqis22p5MEYfrjRw0hiBa5hM+7sDGYQpMinUjkM8kbPXMkY+AhKLrHleBl+SuqpzlgA==
+ dependencies:
+ "@ai-sdk/provider" "3.0.8"
+ "@ai-sdk/provider-utils" "4.0.14"
+ "@vercel/oidc" "3.1.0"
+
+"@ai-sdk/[email protected]":
+ version "4.0.14"
+ resolved "https://registry.yarnpkg.com/@ai-sdk/provider-utils/-/provider-utils-4.0.14.tgz#61e4e8707d7ae759e3de1f896131deda7b8ab159"
+ integrity sha512-7bzKd9lgiDeXM7O4U4nQ8iTxguAOkg8LZGD9AfDVZYjO5cKYRwBPwVjboFcVrxncRHu0tYxZtXZtiLKpG4pEng==
+ dependencies:
+ "@ai-sdk/provider" "3.0.8"
+ "@standard-schema/spec" "^1.1.0"
+ eventsource-parser "^3.0.6"
+
+"@ai-sdk/[email protected]":
+ version "3.0.8"
+ resolved "https://registry.yarnpkg.com/@ai-sdk/provider/-/provider-3.0.8.tgz#fd7fac7533c03534ac1d3fb710a6b96e2aa00263"
+ integrity sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ==
+ dependencies:
+ json-schema "^0.4.0"
+
+"@ai-sdk/react@^3.0.79":
+ version "3.0.79"
+ resolved "https://registry.yarnpkg.com/@ai-sdk/react/-/react-3.0.79.tgz#10d5b24859f959677de857b9b4b3fb0560eb3596"
+ integrity sha512-s/Y+/sISlsEX5Zo/by0jwOyA6vnQ7+CldpRYGv5hMmgnarZ1m5B6myw3Y1Bc2xnozUy+wrmwA6HttlmR4xOOEg==
+ dependencies:
+ "@ai-sdk/provider-utils" "4.0.14"
+ ai "6.0.77"
+ swr "^2.2.5"
+ throttleit "2.1.0"
+
"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.28.6":
version "7.28.6"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.28.6.tgz#72499312ec58b1e2245ba4a4f550c132be4982f7"
@@ -1070,6 +1105,11 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"
+"@opentelemetry/[email protected]":
+ version "1.9.0"
+ resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.9.0.tgz#d03eba68273dc0f7509e2a3d5cba21eae10379fe"
+ integrity sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==
+
"@pixi/colord@^2.9.6":
version "2.9.6"
resolved "https://registry.yarnpkg.com/@pixi/colord/-/colord-2.9.6.tgz#7e4e7851480da6fd3cef4e331f008d60be7e1204"
@@ -1798,6 +1838,11 @@
resolved "https://registry.yarnpkg.com/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.56.0.tgz#9eec0212732a432c71bde0350bc40b673d15b2db"
integrity sha512-H8AE9Ur/t0+1VXujj90w0HrSOuv0Nq9r1vSZF2t5km20NTfosQsGGUXDaKdQZzwuLts7IyL1fYT4hM95TI9c4g==
+"@standard-schema/spec@^1.1.0":
+ version "1.1.0"
+ resolved "https://registry.yarnpkg.com/@standard-schema/spec/-/spec-1.1.0.tgz#a79b55dbaf8604812f52d140b2c9ab41bc150bb8"
+ integrity sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==
+
"@standard-schema/utils@^0.3.0":
version "0.3.0"
resolved "https://registry.npmjs.org/@standard-schema/utils/-/utils-0.3.0.tgz"
@@ -2782,6 +2827,16 @@ add-stream@^1.0.0:
resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa"
integrity sha512-qQLMr+8o0WC4FZGQTcJiKBVC59JylcPSrTtk6usvmIDFUOCKegapy1VHQwRbFMOFyb/inzUVqHs+eMYKDM1YeQ==
[email protected], ai@^6.0.77:
+ version "6.0.77"
+ resolved "https://registry.yarnpkg.com/ai/-/ai-6.0.77.tgz#08fd62c00ca7fb134b9bf42b2ec58444fddc9c15"
+ integrity sha512-tyyhrRpCRFVlivdNIFLK8cexSBB2jwTqO0z1qJQagk+UxZ+MW8h5V8xsvvb+xdKDY482Y8KAm0mr7TDnPKvvlw==
+ dependencies:
+ "@ai-sdk/gateway" "3.0.39"
+ "@ai-sdk/provider" "3.0.8"
+ "@ai-sdk/provider-utils" "4.0.14"
+ "@opentelemetry/api" "1.9.0"
+
ajv@^6.12.4:
version "6.12.6"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
@@ -4086,7 +4141,7 @@ delaunator@5:
dependencies:
robust-predicates "^3.0.2"
-dequal@^2.0.0:
+dequal@^2.0.0, dequal@^2.0.3:
version "2.0.3"
resolved "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz"
integrity sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA==
@@ -4608,6 +4663,11 @@ eventemitter3@^5.0.1:
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4"
integrity sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==
+eventsource-parser@^3.0.6:
+ version "3.0.6"
+ resolved "https://registry.yarnpkg.com/eventsource-parser/-/eventsource-parser-3.0.6.tgz#292e165e34cacbc936c3c92719ef326d4aeb4e90"
+ integrity sha512-Vo1ab+QXPzZ4tCa8SwIHJFaSzy4R6SHf7BY79rFBDf0idraZWAkYrDjDj8uWaSm3S2TK+hJ7/t1CEmZ7jXw+pg==
+
expand-tilde@^2.0.0, expand-tilde@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/expand-tilde/-/expand-tilde-2.0.2.tgz#97e801aa052df02454de46b02bf621642cdc8502"
@@ -5812,6 +5872,11 @@ json-schema-traverse@^1.0.0:
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2"
integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==
+json-schema@^0.4.0:
+ version "0.4.0"
+ resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.4.0.tgz#f7de4cf6efab838ebaeb3236474cbba5a1930ab5"
+ integrity sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==
+
json-stable-stringify-without-jsonify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
@@ -8508,6 +8573,14 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
+swr@^2.2.5:
+ version "2.4.0"
+ resolved "https://registry.yarnpkg.com/swr/-/swr-2.4.0.tgz#cd11e368cb13597f61ee3334428aa20b5e81f36e"
+ integrity sha512-sUlC20T8EOt1pHmDiqueUWMmRRX03W7w5YxovWX7VR2KHEPCTMly85x05vpkP5i6Bu4h44ePSMD9Tc+G2MItFw==
+ dependencies:
+ dequal "^2.0.3"
+ use-sync-external-store "^1.6.0"
+
tailwind-merge@^3.3.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/tailwind-merge/-/tailwind-merge-3.4.0.tgz#5a264e131a096879965f1175d11f8c36e6b64eca"
@@ -8543,6 +8616,11 @@ text-table@^0.2.0:
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
integrity sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==
+ version "2.1.0"
+ resolved "https://registry.yarnpkg.com/throttleit/-/throttleit-2.1.0.tgz#a7e4aa0bf4845a5bd10daa39ea0c783f631a07b4"
+ integrity sha512-nt6AMGKW1p/70DF/hGBdJB57B8Tspmbp5gfJ8ilhLnt7kkr2ye7hzD6NVG8GGErk2HWF34igrL2CXmNIkzKqKw==
+
through2@^2.0.0:
version "2.0.5"
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"