Merge branch 'main' into feat-google-drive-folder-select

This commit is contained in:
phact 2025-10-06 21:53:11 -04:00
commit 653825527a
24 changed files with 2662 additions and 2324 deletions

File diff suppressed because it is too large Load diff

View file

@ -67,7 +67,7 @@ export function KnowledgeFilterList({
<div className="flex-1 min-h-0 flex flex-col">
<div className="px-3 flex-1 min-h-0 flex flex-col">
<div className="flex-shrink-0">
<div className="flex items-center justify-between mb-3 ml-3 mr-2">
<div className="flex items-center justify-between mb-3 mr-2 ml-4">
<h3 className="text-xs font-medium text-muted-foreground">
Knowledge Filters
</h3>
@ -82,11 +82,11 @@ export function KnowledgeFilterList({
</div>
<div className="overflow-y-auto scrollbar-hide space-y-1">
{loading ? (
<div className="text-[13px] text-muted-foreground p-2 ml-1">
<div className="text-[13px] text-muted-foreground p-2 ml-2">
Loading...
</div>
) : filters.length === 0 ? (
<div className="text-[13px] text-muted-foreground p-2 ml-1">
<div className="text-[13px] text-muted-foreground pb-2 pt-3 ml-4">
{searchQuery ? "No filters found" : "No saved filters"}
</div>
) : (

View file

@ -136,7 +136,7 @@ export function KnowledgeFilterPanel() {
// Load available facets using search aggregations hook
const { data: aggregations } = useGetSearchAggregations("*", 1, 0, {
enabled: isPanelOpen,
placeholderData: (prev) => prev,
placeholderData: prev => prev,
staleTime: 60_000,
gcTime: 5 * 60_000,
});
@ -214,7 +214,7 @@ export function KnowledgeFilterPanel() {
facetType: keyof typeof selectedFilters,
newValues: string[]
) => {
setSelectedFilters((prev) => ({
setSelectedFilters(prev => ({
...prev,
[facetType]: newValues,
}));
@ -234,7 +234,7 @@ export function KnowledgeFilterPanel() {
return (
<div className="h-full bg-background border-l">
<Card className="h-full rounded-none border-0 flex flex-col">
<CardHeader className="pb-3">
<CardHeader className="pb-3 pt-3">
<div className="flex items-center justify-between">
<CardTitle className="text-lg flex items-center gap-2">
Knowledge Filter
@ -271,7 +271,7 @@ export function KnowledgeFilterPanel() {
<Input
id="filter-name"
value={name}
onChange={(e) => {
onChange={e => {
const v = e.target.value;
setName(v);
if (nameError && v.trim()) {
@ -302,7 +302,7 @@ export function KnowledgeFilterPanel() {
<Textarea
id="filter-description"
value={description}
onChange={(e) => setDescription(e.target.value)}
onChange={e => setDescription(e.target.value)}
placeholder="Provide a brief description of your knowledge filter..."
rows={3}
/>
@ -319,7 +319,7 @@ export function KnowledgeFilterPanel() {
placeholder="Enter your search query..."
value={query}
className="font-mono placeholder:font-mono"
onChange={(e) => setQuery(e.target.value)}
onChange={e => setQuery(e.target.value)}
rows={2}
disabled={!!queryOverride && !createMode}
/>
@ -329,13 +329,13 @@ export function KnowledgeFilterPanel() {
<div className="space-y-4">
<div className="space-y-2">
<MultiSelect
options={(availableFacets.data_sources || []).map((bucket) => ({
options={(availableFacets.data_sources || []).map(bucket => ({
value: bucket.key,
label: bucket.key,
count: bucket.count,
}))}
value={selectedFilters.data_sources}
onValueChange={(values) =>
onValueChange={values =>
handleFilterChange("data_sources", values)
}
placeholder="Select sources..."
@ -345,15 +345,13 @@ export function KnowledgeFilterPanel() {
<div className="space-y-2">
<MultiSelect
options={(availableFacets.document_types || []).map(
(bucket) => ({
value: bucket.key,
label: bucket.key,
count: bucket.count,
})
)}
options={(availableFacets.document_types || []).map(bucket => ({
value: bucket.key,
label: bucket.key,
count: bucket.count,
}))}
value={selectedFilters.document_types}
onValueChange={(values) =>
onValueChange={values =>
handleFilterChange("document_types", values)
}
placeholder="Select types..."
@ -363,13 +361,13 @@ export function KnowledgeFilterPanel() {
<div className="space-y-2">
<MultiSelect
options={(availableFacets.owners || []).map((bucket) => ({
options={(availableFacets.owners || []).map(bucket => ({
value: bucket.key,
label: bucket.key,
count: bucket.count,
}))}
value={selectedFilters.owners}
onValueChange={(values) => handleFilterChange("owners", values)}
onValueChange={values => handleFilterChange("owners", values)}
placeholder="Select owners..."
allOptionLabel="All Owners"
/>
@ -378,14 +376,14 @@ export function KnowledgeFilterPanel() {
<div className="space-y-2">
<MultiSelect
options={(availableFacets.connector_types || []).map(
(bucket) => ({
bucket => ({
value: bucket.key,
label: bucket.key,
count: bucket.count,
})
)}
value={selectedFilters.connector_types}
onValueChange={(values) =>
onValueChange={values =>
handleFilterChange("connector_types", values)
}
placeholder="Select connectors..."
@ -405,7 +403,7 @@ export function KnowledgeFilterPanel() {
min="1"
max="1000"
value={resultLimit}
onChange={(e) => {
onChange={e => {
const newLimit = Math.max(
1,
Math.min(1000, parseInt(e.target.value) || 1)
@ -418,7 +416,7 @@ export function KnowledgeFilterPanel() {
</div>
<Slider
value={[resultLimit]}
onValueChange={(values) => setResultLimit(values[0])}
onValueChange={values => setResultLimit(values[0])}
max={1000}
min={1}
step={1}
@ -438,7 +436,7 @@ export function KnowledgeFilterPanel() {
max="5"
step="0.1"
value={scoreThreshold}
onChange={(e) =>
onChange={e =>
setScoreThreshold(parseFloat(e.target.value) || 0)
}
className="h-6 text-xs text-right px-2 bg-muted/30 !border-0 rounded ml-auto focus:ring-0 focus:outline-none"
@ -447,7 +445,7 @@ export function KnowledgeFilterPanel() {
</div>
<Slider
value={[scoreThreshold]}
onValueChange={(values) => setScoreThreshold(values[0])}
onValueChange={values => setScoreThreshold(values[0])}
max={5}
min={0}
step={0.1}

View file

@ -3,7 +3,7 @@ import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
const buttonVariants = cva(
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:opacity-50 disabled:pointer-events-none disabled:select-none [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-lg text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:opacity-50 disabled:pointer-events-none disabled:select-none [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
{
variants: {
variant: {

View file

@ -0,0 +1,62 @@
import { ModelOption } from "@/app/api/queries/useGetModelsQuery";
import {
getFallbackModels,
ModelProvider,
} from "@/app/settings/helpers/model-helpers";
import { ModelSelectItems } from "@/app/settings/helpers/model-select-item";
import { LabelWrapper } from "@/components/label-wrapper";
import {
Select,
SelectContent,
SelectTrigger,
SelectValue,
} from "@radix-ui/react-select";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@radix-ui/react-tooltip";
interface EmbeddingModelInputProps {
disabled?: boolean;
value: string;
onChange: (value: string) => void;
modelsData?: {
embedding_models: ModelOption[];
};
currentProvider?: ModelProvider;
}
export const EmbeddingModelInput = ({
disabled,
value,
onChange,
modelsData,
currentProvider = "openai",
}: EmbeddingModelInputProps) => {
return (
<LabelWrapper
helperText="Model used for knowledge ingest and retrieval"
id="embedding-model-select"
label="Embedding model"
>
<Select disabled={disabled} value={value} onValueChange={onChange}>
<Tooltip delayDuration={0}>
<TooltipTrigger asChild>
<SelectTrigger disabled id="embedding-model-select">
<SelectValue placeholder="Select an embedding model" />
</SelectTrigger>
</TooltipTrigger>
<TooltipContent>Locked to keep embeddings consistent</TooltipContent>
</Tooltip>
<SelectContent>
<ModelSelectItems
models={modelsData?.embedding_models}
fallbackModels={getFallbackModels(currentProvider).embedding}
provider={currentProvider}
/>
</SelectContent>
</Select>
</LabelWrapper>
);
};

View file

@ -0,0 +1,74 @@
import { LabelWrapper } from "@/components/label-wrapper";
import { Button } from "../button";
import { Input } from "../input";
import { Minus, Plus } from "lucide-react";
interface NumberInputProps {
id: string;
label: string;
value: number;
onChange: (value: number) => void;
unit: string;
min?: number;
max?: number;
disabled?: boolean;
}
export const NumberInput = ({
id,
label,
value,
onChange,
min = 1,
max,
disabled,
unit,
}: NumberInputProps) => {
return (
<LabelWrapper id={id} label={label}>
<div className="relative">
<Input
id="chunk-size"
type="number"
disabled={disabled}
max={max}
min={min}
value={value}
onChange={(e) => onChange(parseInt(e.target.value) || 0)}
className="w-full pr-20 [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"
/>
<div className="absolute inset-y-0 right-0 top-0 p-[1px] py-[1.5px] flex items-center ">
<span className="text-sm text-placeholder-foreground mr-4 pointer-events-none">
{unit}
</span>
<div className="flex flex-col mt-[2px] mb-[2px]">
<Button
aria-label={`Increase ${label} value`}
className="h-5 rounded-l-none rounded-br-none border-input border-t-transparent border-r-transparent border-b-[0.5px] hover:border-t-[.5px] hover:border-foreground"
variant="outline"
size="iconSm"
onClick={() => onChange(value + 1)}
>
<Plus
className="text-muted-foreground hover:text-foreground"
size={8}
/>
</Button>
<Button
aria-label={`Decrease ${label} value`}
className="h-5 rounded-l-none rounded-tr-none border-input border-b-transparent border-r-transparent hover:border-b-1 hover:border-b-[.5px] hover:border-foreground"
variant="outline"
size="iconSm"
onClick={() => onChange(value - 1)}
>
<Minus
className="text-muted-foreground hover:text-foreground"
size={8}
/>
</Button>
</div>
</div>
</div>
</LabelWrapper>
);
};

View file

@ -6,8 +6,7 @@ import {
type Nudge = string;
const DEFAULT_NUDGES = [
];
const DEFAULT_NUDGES: Nudge[] = [];
export const useGetNudgesQuery = (
chatId?: string | null,

View file

@ -230,7 +230,7 @@ function ChatPage() {
content: `🔄 Starting upload of **${file.name}**...`,
timestamp: new Date(),
};
setMessages((prev) => [...prev, uploadStartMessage]);
setMessages(prev => [...prev, uploadStartMessage]);
try {
const formData = new FormData();
@ -282,7 +282,7 @@ function ChatPage() {
content: `⏳ Upload initiated for **${file.name}**. Processing in background... (Task ID: ${taskId})`,
timestamp: new Date(),
};
setMessages((prev) => [...prev.slice(0, -1), pollingMessage]);
setMessages(prev => [...prev.slice(0, -1), pollingMessage]);
} else if (response.ok) {
// Original flow: Direct response
@ -296,7 +296,7 @@ function ChatPage() {
timestamp: new Date(),
};
setMessages((prev) => [...prev.slice(0, -1), uploadMessage]);
setMessages(prev => [...prev.slice(0, -1), uploadMessage]);
// Add file to conversation docs
if (result.filename) {
@ -305,7 +305,7 @@ function ChatPage() {
// Update the response ID for this endpoint
if (result.response_id) {
setPreviousResponseIds((prev) => ({
setPreviousResponseIds(prev => ({
...prev,
[endpoint]: result.response_id,
}));
@ -329,7 +329,7 @@ function ChatPage() {
content: `❌ Failed to process document. Please try again.`,
timestamp: new Date(),
};
setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
setMessages(prev => [...prev.slice(0, -1), errorMessage]);
} finally {
setIsUploading(false);
setLoading(false);
@ -620,7 +620,7 @@ function ChatPage() {
lastLoadedConversationRef.current = conversationData.response_id;
// Set the previous response ID for this conversation
setPreviousResponseIds((prev) => ({
setPreviousResponseIds(prev => ({
...prev,
[conversationData.endpoint]: conversationData.response_id,
}));
@ -662,7 +662,7 @@ function ChatPage() {
content: `🔄 Starting upload of **${filename}**...`,
timestamp: new Date(),
};
setMessages((prev) => [...prev, uploadStartMessage]);
setMessages(prev => [...prev, uploadStartMessage]);
};
const handleFileUploaded = (event: CustomEvent) => {
@ -680,11 +680,11 @@ function ChatPage() {
timestamp: new Date(),
};
setMessages((prev) => [...prev.slice(0, -1), uploadMessage]);
setMessages(prev => [...prev.slice(0, -1), uploadMessage]);
// Update the response ID for this endpoint
if (result.response_id) {
setPreviousResponseIds((prev) => ({
setPreviousResponseIds(prev => ({
...prev,
[endpoint]: result.response_id,
}));
@ -711,7 +711,7 @@ function ChatPage() {
content: `❌ Upload failed for **${filename}**: ${error}`,
timestamp: new Date(),
};
setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
setMessages(prev => [...prev.slice(0, -1), errorMessage]);
};
window.addEventListener(
@ -1007,7 +1007,7 @@ function ChatPage() {
if (chunk.delta.finish_reason) {
console.log("Finish reason:", chunk.delta.finish_reason);
// Mark any pending function calls as completed
currentFunctionCalls.forEach((fc) => {
currentFunctionCalls.forEach(fc => {
if (fc.status === "pending" && fc.argumentsString) {
try {
fc.arguments = JSON.parse(fc.argumentsString);
@ -1040,13 +1040,13 @@ function ChatPage() {
// Try to find an existing pending call to update (created by earlier deltas)
let existing = currentFunctionCalls.find(
(fc) => fc.id === chunk.item.id
fc => fc.id === chunk.item.id
);
if (!existing) {
existing = [...currentFunctionCalls]
.reverse()
.find(
(fc) =>
fc =>
fc.status === "pending" &&
!fc.id &&
fc.name === (chunk.item.tool_name || chunk.item.name)
@ -1077,7 +1077,7 @@ function ChatPage() {
currentFunctionCalls.push(functionCall);
console.log(
"🟢 Function calls now:",
currentFunctionCalls.map((fc) => ({
currentFunctionCalls.map(fc => ({
id: fc.id,
name: fc.name,
}))
@ -1150,7 +1150,7 @@ function ChatPage() {
);
console.log(
"🔵 Looking for existing function calls:",
currentFunctionCalls.map((fc) => ({
currentFunctionCalls.map(fc => ({
id: fc.id,
name: fc.name,
}))
@ -1158,7 +1158,7 @@ function ChatPage() {
// Find existing function call by ID or name
const functionCall = currentFunctionCalls.find(
(fc) =>
fc =>
fc.id === chunk.item.id ||
fc.name === chunk.item.tool_name ||
fc.name === chunk.item.name
@ -1206,7 +1206,7 @@ function ChatPage() {
// Find existing function call by ID, or by name/type if ID not available
const functionCall = currentFunctionCalls.find(
(fc) =>
fc =>
fc.id === chunk.item.id ||
fc.name === chunk.item.tool_name ||
fc.name === chunk.item.name ||
@ -1261,13 +1261,13 @@ function ChatPage() {
// Dedupe by id or pending with same name
let existing = currentFunctionCalls.find(
(fc) => fc.id === chunk.item.id
fc => fc.id === chunk.item.id
);
if (!existing) {
existing = [...currentFunctionCalls]
.reverse()
.find(
(fc) =>
fc =>
fc.status === "pending" &&
!fc.id &&
fc.name ===
@ -1306,7 +1306,7 @@ function ChatPage() {
currentFunctionCalls.push(functionCall);
console.log(
"🟡 Function calls now:",
currentFunctionCalls.map((fc) => ({
currentFunctionCalls.map(fc => ({
id: fc.id,
name: fc.name,
type: fc.type,
@ -1404,7 +1404,7 @@ function ChatPage() {
};
if (!controller.signal.aborted && thisStreamId === streamIdRef.current) {
setMessages((prev) => [...prev, finalMessage]);
setMessages(prev => [...prev, finalMessage]);
setStreamingMessage(null);
if (previousResponseIds[endpoint]) {
cancelNudges();
@ -1417,7 +1417,7 @@ function ChatPage() {
!controller.signal.aborted &&
thisStreamId === streamIdRef.current
) {
setPreviousResponseIds((prev) => ({
setPreviousResponseIds(prev => ({
...prev,
[endpoint]: newResponseId,
}));
@ -1445,7 +1445,7 @@ function ChatPage() {
"Sorry, I couldn't connect to the chat service. Please try again.",
timestamp: new Date(),
};
setMessages((prev) => [...prev, errorMessage]);
setMessages(prev => [...prev, errorMessage]);
}
};
@ -1458,7 +1458,7 @@ function ChatPage() {
timestamp: new Date(),
};
setMessages((prev) => [...prev, userMessage]);
setMessages(prev => [...prev, userMessage]);
setInput("");
setLoading(true);
setIsFilterHighlighted(false);
@ -1524,14 +1524,14 @@ function ChatPage() {
content: result.response,
timestamp: new Date(),
};
setMessages((prev) => [...prev, assistantMessage]);
setMessages(prev => [...prev, assistantMessage]);
if (result.response_id) {
cancelNudges();
}
// Store the response ID if present for this endpoint
if (result.response_id) {
setPreviousResponseIds((prev) => ({
setPreviousResponseIds(prev => ({
...prev,
[endpoint]: result.response_id,
}));
@ -1552,7 +1552,7 @@ function ChatPage() {
content: "Sorry, I encountered an error. Please try again.",
timestamp: new Date(),
};
setMessages((prev) => [...prev, errorMessage]);
setMessages(prev => [...prev, errorMessage]);
}
} catch (error) {
console.error("Chat error:", error);
@ -1562,7 +1562,7 @@ function ChatPage() {
"Sorry, I couldn't connect to the chat service. Please try again.",
timestamp: new Date(),
};
setMessages((prev) => [...prev, errorMessage]);
setMessages(prev => [...prev, errorMessage]);
}
}
@ -1575,7 +1575,7 @@ function ChatPage() {
};
const toggleFunctionCall = (functionCallId: string) => {
setExpandedFunctionCalls((prev) => {
setExpandedFunctionCalls(prev => {
const newSet = new Set(prev);
if (newSet.has(functionCallId)) {
newSet.delete(functionCallId);
@ -1632,7 +1632,7 @@ function ChatPage() {
// Set the response_id we want to continue from as the previous response ID
// This tells the backend to continue the conversation from this point
setPreviousResponseIds((prev) => ({
setPreviousResponseIds(prev => ({
...prev,
[endpoint]: responseIdToForkFrom,
}));
@ -1903,7 +1903,7 @@ function ChatPage() {
}
if (isFilterDropdownOpen) {
const filteredFilters = availableFilters.filter((filter) =>
const filteredFilters = availableFilters.filter(filter =>
filter.name.toLowerCase().includes(filterSearchTerm.toLowerCase())
);
@ -1921,7 +1921,7 @@ function ChatPage() {
if (e.key === "ArrowDown") {
e.preventDefault();
setSelectedFilterIndex((prev) =>
setSelectedFilterIndex(prev =>
prev < filteredFilters.length - 1 ? prev + 1 : 0
);
return;
@ -1929,7 +1929,7 @@ function ChatPage() {
if (e.key === "ArrowUp") {
e.preventDefault();
setSelectedFilterIndex((prev) =>
setSelectedFilterIndex(prev =>
prev > 0 ? prev - 1 : filteredFilters.length - 1
);
return;
@ -2159,7 +2159,7 @@ function ChatPage() {
{endpoint === "chat" && (
<div className="flex-shrink-0 ml-2">
<button
onClick={(e) => handleForkConversation(index, e)}
onClick={e => handleForkConversation(index, e)}
className="opacity-0 group-hover:opacity-100 transition-opacity p-1 hover:bg-accent rounded text-muted-foreground hover:text-foreground"
title="Fork conversation from here"
>
@ -2223,8 +2223,8 @@ function ChatPage() {
)}
{/* Input Area - Fixed at bottom */}
<div className="flex-shrink-0 p-6 pb-8 pt-4 flex justify-center">
<div className="w-full max-w-[75%]">
<div className="pb-8 pt-4 flex px-6">
<div className="w-full">
<form onSubmit={handleSubmit} className="relative">
<div className="relative w-full bg-muted/20 rounded-lg border border-border/50 focus-within:ring-1 focus-within:ring-ring">
{selectedFilter && (
@ -2257,7 +2257,7 @@ function ChatPage() {
value={input}
onChange={onChange}
onKeyDown={handleKeyDown}
onHeightChange={(height) => setTextareaHeight(height)}
onHeightChange={height => setTextareaHeight(height)}
maxRows={7}
minRows={2}
placeholder="Type to ask a question..."
@ -2286,7 +2286,7 @@ function ChatPage() {
variant="outline"
size="iconSm"
className="absolute bottom-3 left-3 h-8 w-8 p-0 rounded-full hover:bg-muted/50"
onMouseDown={(e) => {
onMouseDown={e => {
e.preventDefault();
}}
onClick={onAtClick}
@ -2296,7 +2296,7 @@ function ChatPage() {
</Button>
<Popover
open={isFilterDropdownOpen}
onOpenChange={(open) => {
onOpenChange={open => {
setIsFilterDropdownOpen(open);
}}
>
@ -2321,7 +2321,7 @@ function ChatPage() {
align="start"
sideOffset={6}
alignOffset={-18}
onOpenAutoFocus={(e) => {
onOpenAutoFocus={e => {
// Prevent auto focus on the popover content
e.preventDefault();
// Keep focus on the input
@ -2354,7 +2354,7 @@ function ChatPage() {
</button>
)}
{availableFilters
.filter((filter) =>
.filter(filter =>
filter.name
.toLowerCase()
.includes(filterSearchTerm.toLowerCase())
@ -2383,7 +2383,7 @@ function ChatPage() {
)}
</button>
))}
{availableFilters.filter((filter) =>
{availableFilters.filter(filter =>
filter.name
.toLowerCase()
.includes(filterSearchTerm.toLowerCase())

View file

@ -92,6 +92,7 @@ export default function ConnectorsPage() {
selectedFiles={selectedFiles}
isAuthenticated={false} // This would come from auth context in real usage
accessToken={undefined} // This would come from connected account
isIngesting={isSyncing}
/>
</div>

View file

@ -351,4 +351,17 @@
.discord-error {
@apply text-xs opacity-70;
}
.box-shadow-inner::after {
content: " ";
position: absolute;
bottom: 0;
left: 0;
right: 0;
pointer-events: none;
background: linear-gradient(to top, hsl(var(--background)), transparent);
display: block;
width: 100%;
height: 30px;
}
}

View file

@ -14,19 +14,19 @@ import { Label } from "@/components/ui/label";
import { useKnowledgeFilter } from "@/contexts/knowledge-filter-context";
import { useTask } from "@/contexts/task-context";
import {
type ChunkResult,
type File,
useGetSearchQuery,
type ChunkResult,
type File,
useGetSearchQuery,
} from "../../api/queries/useGetSearchQuery";
// import { Label } from "@/components/ui/label";
// import { Checkbox } from "@/components/ui/checkbox";
import { KnowledgeSearchInput } from "@/components/knowledge-search-input";
const getFileTypeLabel = (mimetype: string) => {
if (mimetype === "application/pdf") return "PDF";
if (mimetype === "text/plain") return "Text";
if (mimetype === "application/msword") return "Word Document";
return "Unknown";
if (mimetype === "application/pdf") return "PDF";
if (mimetype === "text/plain") return "Text";
if (mimetype === "application/msword") return "Word Document";
return "Unknown";
};
function ChunksPageContent() {
@ -43,13 +43,13 @@ function ChunksPageContent() {
number | null
>(null);
// Calculate average chunk length
const averageChunkLength = useMemo(
() =>
chunks.reduce((acc, chunk) => acc + chunk.text.length, 0) /
chunks.length || 0,
[chunks],
);
// Calculate average chunk length
const averageChunkLength = useMemo(
() =>
chunks.reduce((acc, chunk) => acc + chunk.text.length, 0) /
chunks.length || 0,
[chunks]
);
// const [selectAll, setSelectAll] = useState(false);
@ -59,70 +59,70 @@ function ChunksPageContent() {
parsedFilterData
);
const handleCopy = useCallback((text: string, index: number) => {
// Trim whitespace and remove new lines/tabs for cleaner copy
navigator.clipboard.writeText(text.trim().replace(/[\n\r\t]/gm, ""));
setActiveCopiedChunkIndex(index);
setTimeout(() => setActiveCopiedChunkIndex(null), 10 * 1000); // 10 seconds
}, []);
const handleCopy = useCallback((text: string, index: number) => {
// Trim whitespace and remove new lines/tabs for cleaner copy
navigator.clipboard.writeText(text.trim().replace(/[\n\r\t]/gm, ""));
setActiveCopiedChunkIndex(index);
setTimeout(() => setActiveCopiedChunkIndex(null), 10 * 1000); // 10 seconds
}, []);
const fileData = (data as File[]).find(
(file: File) => file.filename === filename,
);
const fileData = (data as File[]).find(
(file: File) => file.filename === filename
);
// Extract chunks for the specific file
useEffect(() => {
if (!filename || !(data as File[]).length) {
setChunks([]);
return;
}
// Extract chunks for the specific file
useEffect(() => {
if (!filename || !(data as File[]).length) {
setChunks([]);
return;
}
setChunks(
fileData?.chunks?.map((chunk, i) => ({ ...chunk, index: i + 1 })) || [],
);
}, [data, filename]);
setChunks(
fileData?.chunks?.map((chunk, i) => ({ ...chunk, index: i + 1 })) || []
);
}, [data, filename]);
// Set selected state for all checkboxes when selectAll changes
useEffect(() => {
if (selectAll) {
setSelectedChunks(new Set(chunks.map((_, index) => index)));
} else {
setSelectedChunks(new Set());
}
}, [selectAll, setSelectedChunks, chunks]);
// Set selected state for all checkboxes when selectAll changes
// useEffect(() => {
// if (selectAll) {
// setSelectedChunks(new Set(chunks.map((_, index) => index)));
// } else {
// setSelectedChunks(new Set());
// }
// }, [selectAll, setSelectedChunks, chunks]);
const handleBack = useCallback(() => {
router.push("/knowledge");
}, [router]);
const handleBack = useCallback(() => {
router.push("/knowledge");
}, [router]);
// const handleChunkCardCheckboxChange = useCallback(
// (index: number) => {
// setSelectedChunks((prevSelected) => {
// const newSelected = new Set(prevSelected);
// if (newSelected.has(index)) {
// newSelected.delete(index);
// } else {
// newSelected.add(index);
// }
// return newSelected;
// });
// },
// [setSelectedChunks]
// );
// const handleChunkCardCheckboxChange = useCallback(
// (index: number) => {
// setSelectedChunks((prevSelected) => {
// const newSelected = new Set(prevSelected);
// if (newSelected.has(index)) {
// newSelected.delete(index);
// } else {
// newSelected.add(index);
// }
// return newSelected;
// });
// },
// [setSelectedChunks]
// );
if (!filename) {
return (
<div className="flex items-center justify-center h-64">
<div className="text-center">
<Search className="h-12 w-12 mx-auto mb-4 text-muted-foreground/50" />
<p className="text-lg text-muted-foreground">No file specified</p>
<p className="text-sm text-muted-foreground/70 mt-2">
Please select a file from the knowledge page
</p>
</div>
</div>
);
}
if (!filename) {
return (
<div className="flex items-center justify-center h-64">
<div className="text-center">
<Search className="h-12 w-12 mx-auto mb-4 text-muted-foreground/50" />
<p className="text-lg text-muted-foreground">No file specified</p>
<p className="text-sm text-muted-foreground/70 mt-2">
Please select a file from the knowledge page
</p>
</div>
</div>
);
}
return (
<div className="flex flex-col h-full">
@ -149,7 +149,7 @@ function ChunksPageContent() {
<Checkbox
id="selectAllChunks"
checked={selectAll}
onCheckedChange={(handleSelectAll) =>
onCheckedChange={handleSelectAll =>
setSelectAll(!!handleSelectAll)
}
/>
@ -160,8 +160,8 @@ function ChunksPageContent() {
Select all
</Label>
</div> */}
</div>
</div>
</div>
</div>
{/* Content Area - matches knowledge page structure */}
<div className="flex-1 overflow-auto pr-6">
@ -200,73 +200,73 @@ function ChunksPageContent() {
}
/>
</div> */}
<span className="text-sm font-bold">
Chunk {chunk.index}
</span>
<span className="bg-background p-1 rounded text-xs text-muted-foreground/70">
{chunk.text.length} chars
</span>
<div className="py-1">
<Button
onClick={() => handleCopy(chunk.text, index)}
variant="ghost"
size="sm"
>
{activeCopiedChunkIndex === index ? (
<Check className="text-muted-foreground" />
) : (
<Copy className="text-muted-foreground" />
)}
</Button>
</div>
</div>
<span className="text-sm font-bold">
Chunk {chunk.index}
</span>
<span className="bg-background p-1 rounded text-xs text-muted-foreground/70">
{chunk.text.length} chars
</span>
<div className="py-1">
<Button
onClick={() => handleCopy(chunk.text, index)}
variant="ghost"
size="sm"
>
{activeCopiedChunkIndex === index ? (
<Check className="text-muted-foreground" />
) : (
<Copy className="text-muted-foreground" />
)}
</Button>
</div>
</div>
<span className="bg-background p-1 rounded text-xs text-muted-foreground/70">
{chunk.score.toFixed(2)} score
</span>
<span className="bg-background p-1 rounded text-xs text-muted-foreground/70">
{chunk.score.toFixed(2)} score
</span>
{/* TODO: Update to use active toggle */}
{/* <span className="px-2 py-1 text-green-500">
{/* TODO: Update to use active toggle */}
{/* <span className="px-2 py-1 text-green-500">
<Switch
className="ml-2 bg-green-500"
checked={true}
/>
Active
</span> */}
</div>
<blockquote className="text-sm text-muted-foreground leading-relaxed ml-1.5">
{chunk.text}
</blockquote>
</div>
))}
</div>
)}
</div>
</div>
{/* Right panel - Summary (TODO), Technical details, */}
{chunks.length > 0 && (
<div className="w-[320px] py-20 px-2">
<div className="mb-8">
<h2 className="text-xl font-semibold mt-3 mb-4">
Technical details
</h2>
<dl>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">
Total chunks
</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{chunks.length}
</dd>
</div>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Avg length</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{averageChunkLength.toFixed(0)} chars
</dd>
</div>
{/* TODO: Uncomment after data is available */}
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
</div>
<blockquote className="text-sm text-muted-foreground leading-relaxed ml-1.5">
{chunk.text}
</blockquote>
</div>
))}
</div>
)}
</div>
</div>
{/* Right panel - Summary (TODO), Technical details, */}
{chunks.length > 0 && (
<div className="w-[320px] py-20 px-2">
<div className="mb-8">
<h2 className="text-xl font-semibold mt-3 mb-4">
Technical details
</h2>
<dl>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">
Total chunks
</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{chunks.length}
</dd>
</div>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Avg length</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{averageChunkLength.toFixed(0)} chars
</dd>
</div>
{/* TODO: Uncomment after data is available */}
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Process time</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
</dd>
@ -276,79 +276,79 @@ function ChunksPageContent() {
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
</dd>
</div> */}
</dl>
</div>
<div className="mb-8">
<h2 className="text-xl font-semibold mt-2 mb-3">
Original document
</h2>
<dl>
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
</dl>
</div>
<div className="mb-8">
<h2 className="text-xl font-semibold mt-2 mb-3">
Original document
</h2>
<dl>
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Name</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{fileData?.filename}
</dd>
</div> */}
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Type</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{fileData ? getFileTypeLabel(fileData.mimetype) : "Unknown"}
</dd>
</div>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Size</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{fileData?.size
? `${Math.round(fileData.size / 1024)} KB`
: "Unknown"}
</dd>
</div>
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Type</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{fileData ? getFileTypeLabel(fileData.mimetype) : "Unknown"}
</dd>
</div>
<div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Size</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
{fileData?.size
? `${Math.round(fileData.size / 1024)} KB`
: "Unknown"}
</dd>
</div>
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Uploaded</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
N/A
</dd>
</div> */}
{/* TODO: Uncomment after data is available */}
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
{/* TODO: Uncomment after data is available */}
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Source</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0"></dd>
</div> */}
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
{/* <div className="sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0 mb-2.5">
<dt className="text-sm/6 text-muted-foreground">Updated</dt>
<dd className="mt-1 text-sm/6 text-gray-100 sm:col-span-2 sm:mt-0">
N/A
</dd>
</div> */}
</dl>
</div>
</div>
)}
</div>
);
</dl>
</div>
</div>
)}
</div>
);
}
function ChunksPage() {
return (
<Suspense
fallback={
<div className="flex items-center justify-center h-64">
<div className="text-center">
<Loader2 className="h-12 w-12 mx-auto mb-4 text-muted-foreground/50 animate-spin" />
<p className="text-lg text-muted-foreground">Loading...</p>
</div>
</div>
}
>
<ChunksPageContent />
</Suspense>
);
return (
<Suspense
fallback={
<div className="flex items-center justify-center h-64">
<div className="text-center">
<Loader2 className="h-12 w-12 mx-auto mb-4 text-muted-foreground/50 animate-spin" />
<p className="text-lg text-muted-foreground">Loading...</p>
</div>
</div>
}
>
<ChunksPageContent />
</Suspense>
);
}
export default function ProtectedChunksPage() {
return (
<ProtectedRoute>
<ChunksPage />
</ProtectedRoute>
);
return (
<ProtectedRoute>
<ChunksPage />
</ProtectedRoute>
);
}

View file

@ -69,7 +69,7 @@ function SearchPage() {
parsedFilterData
);
// Convert TaskFiles to File format and merge with backend results
const taskFilesAsFiles: File[] = taskFiles.map((taskFile) => {
const taskFilesAsFiles: File[] = taskFiles.map(taskFile => {
return {
filename: taskFile.filename,
mimetype: taskFile.mimetype,
@ -82,12 +82,12 @@ function SearchPage() {
// Create a map of task files by filename for quick lookup
const taskFileMap = new Map(
taskFilesAsFiles.map((file) => [file.filename, file])
taskFilesAsFiles.map(file => [file.filename, file])
);
// Override backend files with task file status if they exist
const backendFiles = (searchData as File[])
.map((file) => {
.map(file => {
const taskFile = taskFileMap.get(file.filename);
if (taskFile) {
// Override backend file with task file data (includes status)
@ -95,17 +95,17 @@ function SearchPage() {
}
return file;
})
.filter((file) => {
.filter(file => {
// Only filter out files that are currently processing AND in taskFiles
const taskFile = taskFileMap.get(file.filename);
return !taskFile || taskFile.status !== "processing";
});
const filteredTaskFiles = taskFilesAsFiles.filter((taskFile) => {
const filteredTaskFiles = taskFilesAsFiles.filter(taskFile => {
return (
taskFile.status !== "active" &&
!backendFiles.some(
(backendFile) => backendFile.filename === taskFile.filename
backendFile => backendFile.filename === taskFile.filename
)
);
});
@ -184,7 +184,6 @@ function SearchPage() {
{
field: "avgScore",
headerName: "Avg score",
initialFlex: 0.5,
cellRenderer: ({ value }: CustomCellRendererProps<File>) => {
return (
<span className="text-xs text-accent-emerald-foreground bg-accent-emerald px-2 py-1 rounded">
@ -246,7 +245,7 @@ function SearchPage() {
try {
// Delete each file individually since the API expects one filename at a time
const deletePromises = selectedRows.map((row) =>
const deletePromises = selectedRows.map(row =>
deleteDocumentMutation.mutateAsync({ filename: row.filename })
);
@ -345,7 +344,7 @@ function SearchPage() {
}? This will remove all chunks and data associated with these documents. This action cannot be undone.
Documents to be deleted:
${selectedRows.map((row) => `${row.filename}`).join("\n")}`}
${selectedRows.map(row => `${row.filename}`).join("\n")}`}
confirmText="Delete All"
onConfirm={handleBulkDelete}
isLoading={deleteDocumentMutation.isPending}

File diff suppressed because it is too large Load diff

View file

@ -6,373 +6,394 @@ import { useEffect, useState } from "react";
import { type CloudFile, UnifiedCloudPicker } from "@/components/cloud-picker";
import type { IngestSettings } from "@/components/cloud-picker/types";
import { Button } from "@/components/ui/button";
import { Toast } from "@/components/ui/toast";
import { useTask } from "@/contexts/task-context";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
// CloudFile interface is now imported from the unified cloud picker
interface CloudConnector {
id: string;
name: string;
description: string;
status: "not_connected" | "connecting" | "connected" | "error";
type: string;
connectionId?: string;
clientId: string;
hasAccessToken: boolean;
accessTokenError?: string;
id: string;
name: string;
description: string;
status: "not_connected" | "connecting" | "connected" | "error";
type: string;
connectionId?: string;
clientId: string;
hasAccessToken: boolean;
accessTokenError?: string;
}
export default function UploadProviderPage() {
const params = useParams();
const router = useRouter();
const provider = params.provider as string;
const { addTask, tasks } = useTask();
const params = useParams();
const router = useRouter();
const provider = params.provider as string;
const { addTask, tasks } = useTask();
const [connector, setConnector] = useState<CloudConnector | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [accessToken, setAccessToken] = useState<string | null>(null);
const [selectedFiles, setSelectedFiles] = useState<CloudFile[]>([]);
const [isIngesting, setIsIngesting] = useState<boolean>(false);
const [currentSyncTaskId, setCurrentSyncTaskId] = useState<string | null>(
null,
);
const [ingestSettings, setIngestSettings] = useState<IngestSettings>({
chunkSize: 1000,
chunkOverlap: 200,
ocr: false,
pictureDescriptions: false,
embeddingModel: "text-embedding-3-small",
});
const [connector, setConnector] = useState<CloudConnector | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [accessToken, setAccessToken] = useState<string | null>(null);
const [selectedFiles, setSelectedFiles] = useState<CloudFile[]>([]);
const [isIngesting, setIsIngesting] = useState<boolean>(false);
const [currentSyncTaskId, setCurrentSyncTaskId] = useState<string | null>(
null
);
const [ingestSettings, setIngestSettings] = useState<IngestSettings>({
chunkSize: 1000,
chunkOverlap: 200,
ocr: false,
pictureDescriptions: false,
embeddingModel: "text-embedding-3-small",
});
useEffect(() => {
const fetchConnectorInfo = async () => {
setIsLoading(true);
setError(null);
useEffect(() => {
const fetchConnectorInfo = async () => {
setIsLoading(true);
setError(null);
try {
// Fetch available connectors to validate the provider
const connectorsResponse = await fetch("/api/connectors");
if (!connectorsResponse.ok) {
throw new Error("Failed to load connectors");
}
try {
// Fetch available connectors to validate the provider
const connectorsResponse = await fetch("/api/connectors");
if (!connectorsResponse.ok) {
throw new Error("Failed to load connectors");
}
const connectorsResult = await connectorsResponse.json();
const providerInfo = connectorsResult.connectors[provider];
const connectorsResult = await connectorsResponse.json();
const providerInfo = connectorsResult.connectors[provider];
if (!providerInfo || !providerInfo.available) {
setError(
`Cloud provider "${provider}" is not available or configured.`,
);
return;
}
if (!providerInfo || !providerInfo.available) {
setError(
`Cloud provider "${provider}" is not available or configured.`
);
return;
}
// Check connector status
const statusResponse = await fetch(
`/api/connectors/${provider}/status`,
);
if (!statusResponse.ok) {
throw new Error(`Failed to check ${provider} status`);
}
// Check connector status
const statusResponse = await fetch(
`/api/connectors/${provider}/status`
);
if (!statusResponse.ok) {
throw new Error(`Failed to check ${provider} status`);
}
const statusData = await statusResponse.json();
const connections = statusData.connections || [];
const activeConnection = connections.find(
(conn: { is_active: boolean; connection_id: string }) =>
conn.is_active,
);
const isConnected = activeConnection !== undefined;
const statusData = await statusResponse.json();
const connections = statusData.connections || [];
const activeConnection = connections.find(
(conn: { is_active: boolean; connection_id: string }) =>
conn.is_active
);
const isConnected = activeConnection !== undefined;
let hasAccessToken = false;
let accessTokenError: string | undefined;
let hasAccessToken = false;
let accessTokenError: string | undefined;
// Try to get access token for connected connectors
if (isConnected && activeConnection) {
try {
const tokenResponse = await fetch(
`/api/connectors/${provider}/token?connection_id=${activeConnection.connection_id}`,
);
if (tokenResponse.ok) {
const tokenData = await tokenResponse.json();
if (tokenData.access_token) {
hasAccessToken = true;
setAccessToken(tokenData.access_token);
}
} else {
const errorData = await tokenResponse
.json()
.catch(() => ({ error: "Token unavailable" }));
accessTokenError = errorData.error || "Access token unavailable";
}
} catch {
accessTokenError = "Failed to fetch access token";
}
}
// Try to get access token for connected connectors
if (isConnected && activeConnection) {
try {
const tokenResponse = await fetch(
`/api/connectors/${provider}/token?connection_id=${activeConnection.connection_id}`
);
if (tokenResponse.ok) {
const tokenData = await tokenResponse.json();
if (tokenData.access_token) {
hasAccessToken = true;
setAccessToken(tokenData.access_token);
}
} else {
const errorData = await tokenResponse
.json()
.catch(() => ({ error: "Token unavailable" }));
accessTokenError = errorData.error || "Access token unavailable";
}
} catch {
accessTokenError = "Failed to fetch access token";
}
}
setConnector({
id: provider,
name: providerInfo.name,
description: providerInfo.description,
status: isConnected ? "connected" : "not_connected",
type: provider,
connectionId: activeConnection?.connection_id,
clientId: activeConnection?.client_id,
hasAccessToken,
accessTokenError,
});
} catch (error) {
console.error("Failed to load connector info:", error);
setError(
error instanceof Error
? error.message
: "Failed to load connector information",
);
} finally {
setIsLoading(false);
}
};
setConnector({
id: provider,
name: providerInfo.name,
description: providerInfo.description,
status: isConnected ? "connected" : "not_connected",
type: provider,
connectionId: activeConnection?.connection_id,
clientId: activeConnection?.client_id,
hasAccessToken,
accessTokenError,
});
} catch (error) {
console.error("Failed to load connector info:", error);
setError(
error instanceof Error
? error.message
: "Failed to load connector information"
);
} finally {
setIsLoading(false);
}
};
if (provider) {
fetchConnectorInfo();
}
}, [provider]);
if (provider) {
fetchConnectorInfo();
}
}, [provider]);
// Watch for sync task completion and redirect
useEffect(() => {
if (!currentSyncTaskId) return;
// Watch for sync task completion and redirect
useEffect(() => {
if (!currentSyncTaskId) return;
const currentTask = tasks.find(
(task) => task.task_id === currentSyncTaskId,
);
const currentTask = tasks.find(
(task) => task.task_id === currentSyncTaskId
);
if (currentTask && currentTask.status === "completed") {
// Task completed successfully, show toast and redirect
setIsIngesting(false);
setTimeout(() => {
router.push("/knowledge");
}, 2000); // 2 second delay to let user see toast
} else if (currentTask && currentTask.status === "failed") {
// Task failed, clear the tracking but don't redirect
setIsIngesting(false);
setCurrentSyncTaskId(null);
}
}, [tasks, currentSyncTaskId, router]);
if (currentTask && currentTask.status === "completed") {
// Task completed successfully, show toast and redirect
setIsIngesting(false);
setTimeout(() => {
router.push("/knowledge");
}, 2000); // 2 second delay to let user see toast
} else if (currentTask && currentTask.status === "failed") {
// Task failed, clear the tracking but don't redirect
setIsIngesting(false);
setCurrentSyncTaskId(null);
}
}, [tasks, currentSyncTaskId, router]);
const handleFileSelected = (files: CloudFile[]) => {
setSelectedFiles(files);
console.log(`Selected ${files.length} files from ${provider}:`, files);
// You can add additional handling here like triggering sync, etc.
};
const handleFileSelected = (files: CloudFile[]) => {
setSelectedFiles(files);
console.log(`Selected ${files.length} files from ${provider}:`, files);
// You can add additional handling here like triggering sync, etc.
};
const handleSync = async (connector: CloudConnector) => {
if (!connector.connectionId || selectedFiles.length === 0) return;
const handleSync = async (connector: CloudConnector) => {
if (!connector.connectionId || selectedFiles.length === 0) return;
setIsIngesting(true);
setIsIngesting(true);
try {
const syncBody: {
connection_id: string;
max_files?: number;
selected_files?: string[];
settings?: IngestSettings;
} = {
connection_id: connector.connectionId,
selected_files: selectedFiles.map((file) => file.id),
settings: ingestSettings,
};
try {
const syncBody: {
connection_id: string;
max_files?: number;
selected_files?: string[];
settings?: IngestSettings;
} = {
connection_id: connector.connectionId,
selected_files: selectedFiles.map((file) => file.id),
settings: ingestSettings,
};
const response = await fetch(`/api/connectors/${connector.type}/sync`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(syncBody),
});
const response = await fetch(`/api/connectors/${connector.type}/sync`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(syncBody),
});
const result = await response.json();
const result = await response.json();
if (response.status === 201) {
const taskIds = result.task_ids;
if (taskIds && taskIds.length > 0) {
const taskId = taskIds[0]; // Use the first task ID
addTask(taskId);
setCurrentSyncTaskId(taskId);
}
} else {
console.error("Sync failed:", result.error);
}
} catch (error) {
console.error("Sync error:", error);
setIsIngesting(false);
}
};
if (response.status === 201) {
const taskIds = result.task_ids;
if (taskIds && taskIds.length > 0) {
const taskId = taskIds[0]; // Use the first task ID
addTask(taskId);
setCurrentSyncTaskId(taskId);
}
} else {
console.error("Sync failed:", result.error);
}
} catch (error) {
console.error("Sync error:", error);
setIsIngesting(false);
}
};
const getProviderDisplayName = () => {
const nameMap: { [key: string]: string } = {
google_drive: "Google Drive",
onedrive: "OneDrive",
sharepoint: "SharePoint",
};
return nameMap[provider] || provider;
};
const getProviderDisplayName = () => {
const nameMap: { [key: string]: string } = {
google_drive: "Google Drive",
onedrive: "OneDrive",
sharepoint: "SharePoint",
};
return nameMap[provider] || provider;
};
if (isLoading) {
return (
<div className="container mx-auto p-6">
<div className="flex items-center justify-center py-12">
<div className="text-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto mb-4"></div>
<p>Loading {getProviderDisplayName()} connector...</p>
</div>
</div>
</div>
);
}
if (isLoading) {
return (
<div className="container mx-auto p-6">
<div className="flex items-center justify-center py-12">
<div className="text-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto mb-4"></div>
<p>Loading {getProviderDisplayName()} connector...</p>
</div>
</div>
</div>
);
}
if (error || !connector) {
return (
<div className="container mx-auto p-6">
<div className="mb-6">
<Button
variant="ghost"
onClick={() => router.back()}
className="mb-4"
>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
</div>
if (error || !connector) {
return (
<div className="container mx-auto p-6">
<div className="mb-6">
<Button
variant="ghost"
onClick={() => router.back()}
className="mb-4"
>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
</div>
<div className="flex items-center justify-center py-12">
<div className="text-center max-w-md">
<AlertCircle className="h-12 w-12 text-red-500 mx-auto mb-4" />
<h2 className="text-xl font-semibold mb-2">
Provider Not Available
</h2>
<p className="text-muted-foreground mb-4">{error}</p>
<Button onClick={() => router.push("/settings")}>
Configure Connectors
</Button>
</div>
</div>
</div>
);
}
<div className="flex items-center justify-center py-12">
<div className="text-center max-w-md">
<AlertCircle className="h-12 w-12 text-red-500 mx-auto mb-4" />
<h2 className="text-xl font-semibold mb-2">
Provider Not Available
</h2>
<p className="text-muted-foreground mb-4">{error}</p>
<Button onClick={() => router.push("/settings")}>
Configure Connectors
</Button>
</div>
</div>
</div>
);
}
if (connector.status !== "connected") {
return (
<div className="container mx-auto p-6">
<div className="mb-6">
<Button
variant="ghost"
onClick={() => router.back()}
className="mb-4"
>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
</div>
if (connector.status !== "connected") {
return (
<div className="container mx-auto p-6">
<div className="mb-6">
<Button
variant="ghost"
onClick={() => router.back()}
className="mb-4"
>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
</div>
<div className="flex items-center justify-center py-12">
<div className="text-center max-w-md">
<AlertCircle className="h-12 w-12 text-yellow-500 mx-auto mb-4" />
<h2 className="text-xl font-semibold mb-2">
{connector.name} Not Connected
</h2>
<p className="text-muted-foreground mb-4">
You need to connect your {connector.name} account before you can
select files.
</p>
<Button onClick={() => router.push("/settings")}>
Connect {connector.name}
</Button>
</div>
</div>
</div>
);
}
<div className="flex items-center justify-center py-12">
<div className="text-center max-w-md">
<AlertCircle className="h-12 w-12 text-yellow-500 mx-auto mb-4" />
<h2 className="text-xl font-semibold mb-2">
{connector.name} Not Connected
</h2>
<p className="text-muted-foreground mb-4">
You need to connect your {connector.name} account before you can
select files.
</p>
<Button onClick={() => router.push("/settings")}>
Connect {connector.name}
</Button>
</div>
</div>
</div>
);
}
if (!connector.hasAccessToken) {
return (
<div className="container mx-auto p-6">
<div className="mb-6">
<Button
variant="ghost"
onClick={() => router.back()}
className="mb-4"
>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
</div>
if (!connector.hasAccessToken) {
return (
<div className="container mx-auto p-6">
<div className="mb-6">
<Button
variant="ghost"
onClick={() => router.back()}
className="mb-4"
>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
</div>
<div className="flex items-center justify-center py-12">
<div className="text-center max-w-md">
<AlertCircle className="h-12 w-12 text-red-500 mx-auto mb-4" />
<h2 className="text-xl font-semibold mb-2">
Access Token Required
</h2>
<p className="text-muted-foreground mb-4">
{connector.accessTokenError ||
`Unable to get access token for ${connector.name}. Try reconnecting your account.`}
</p>
<Button onClick={() => router.push("/settings")}>
Reconnect {connector.name}
</Button>
</div>
</div>
</div>
);
}
<div className="flex items-center justify-center py-12">
<div className="text-center max-w-md">
<AlertCircle className="h-12 w-12 text-red-500 mx-auto mb-4" />
<h2 className="text-xl font-semibold mb-2">
Access Token Required
</h2>
<p className="text-muted-foreground mb-4">
{connector.accessTokenError ||
`Unable to get access token for ${connector.name}. Try reconnecting your account.`}
</p>
<Button onClick={() => router.push("/settings")}>
Reconnect {connector.name}
</Button>
</div>
</div>
</div>
);
}
return (
<div className="container mx-auto max-w-3xl p-6">
<div className="mb-6 flex gap-2 items-center">
<Button variant="ghost" onClick={() => router.back()}>
<ArrowLeft className="h-4 w-4 scale-125" />
</Button>
<h2 className="text-2xl font-bold">
Add from {getProviderDisplayName()}
</h2>
</div>
const hasSelectedFiles = selectedFiles.length > 0;
<div className="max-w-3xl mx-auto">
<UnifiedCloudPicker
provider={
connector.type as "google_drive" | "onedrive" | "sharepoint"
}
onFileSelected={handleFileSelected}
selectedFiles={selectedFiles}
isAuthenticated={true}
accessToken={accessToken || undefined}
clientId={connector.clientId}
onSettingsChange={setIngestSettings}
/>
</div>
return (
<div className="container mx-auto max-w-3xl px-6">
<div className="mb-8 flex gap-2 items-center">
<Button variant="ghost" onClick={() => router.back()} size="icon">
<ArrowLeft size={18} />
</Button>
<h2 className="text-xl text-[18px] font-semibold">
Add from {getProviderDisplayName()}
</h2>
</div>
<div className="max-w-3xl mx-auto mt-6">
<div className="flex justify-between gap-3 mb-4">
<Button
variant="ghost"
className=" border bg-transparent border-border rounded-lg text-secondary-foreground"
onClick={() => router.back()}
>
Back
</Button>
<Button
variant="secondary"
onClick={() => handleSync(connector)}
disabled={selectedFiles.length === 0 || isIngesting}
>
{isIngesting ? (
<>Ingesting {selectedFiles.length} Files...</>
) : (
<>Start ingest</>
)}
</Button>
</div>
</div>
</div>
);
<div className="max-w-3xl mx-auto">
<UnifiedCloudPicker
provider={
connector.type as "google_drive" | "onedrive" | "sharepoint"
}
onFileSelected={handleFileSelected}
selectedFiles={selectedFiles}
isAuthenticated={true}
isIngesting={isIngesting}
accessToken={accessToken || undefined}
clientId={connector.clientId}
onSettingsChange={setIngestSettings}
/>
</div>
<div className="max-w-3xl mx-auto mt-6 sticky bottom-0 left-0 right-0 pb-6 bg-background pt-4">
<div className="flex justify-between gap-3 mb-4">
<Button
variant="ghost"
className="border bg-transparent border-border rounded-lg text-secondary-foreground"
onClick={() => router.back()}
>
Back
</Button>
<Tooltip>
<TooltipTrigger>
<Button
className="bg-foreground text-background hover:bg-foreground/90 font-semibold"
variant={!hasSelectedFiles ? "secondary" : undefined}
onClick={() => handleSync(connector)}
loading={isIngesting}
disabled={!hasSelectedFiles || isIngesting}
>
{!hasSelectedFiles ? (
<>Ingest files</>
) : (
<>
Ingest {selectedFiles.length} file
{selectedFiles.length > 1 ? "s" : ""}
</>
)}
</Button>
</TooltipTrigger>
{!hasSelectedFiles ? (
<TooltipContent side="left">
Select at least one file before ingesting
</TooltipContent>
) : null}
</Tooltip>
</div>
</div>
</div>
);
}

View file

@ -283,6 +283,7 @@ export function CloudConnectorsDialog({
accessToken={connectorAccessTokens[connector.type]}
onPickerStateChange={() => {}}
clientId={connector.clientId}
isIngesting={false}
/>
</div>
);

View file

@ -1,11 +1,16 @@
"use client";
import { Badge } from "@/components/ui/badge";
import { FileText, Folder, Trash } from "lucide-react";
import { FileText, Folder, Trash2 } from "lucide-react";
import { CloudFile } from "./types";
import GoogleDriveIcon from "@/app/settings/icons/google-drive-icon";
import SharePointIcon from "@/app/settings/icons/share-point-icon";
import OneDriveIcon from "@/app/settings/icons/one-drive-icon";
import { Button } from "@/components/ui/button";
interface FileItemProps {
provider: string;
file: CloudFile;
shouldDisableActions: boolean;
onRemove: (fileId: string) => void;
}
@ -41,27 +46,43 @@ const formatFileSize = (bytes?: number) => {
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
};
export const FileItem = ({ file, onRemove }: FileItemProps) => (
const getProviderIcon = (provider: string) => {
switch (provider) {
case "google_drive":
return <GoogleDriveIcon />;
case "onedrive":
return <OneDriveIcon />;
case "sharepoint":
return <SharePointIcon />;
default:
return <FileText className="h-6 w-6" />;
}
};
export const FileItem = ({ file, onRemove, provider }: FileItemProps) => (
<div
key={file.id}
className="flex items-center justify-between p-2 rounded-md text-xs"
className="flex items-center justify-between p-1.5 rounded-md text-xs"
>
<div className="flex items-center gap-2 flex-1 min-w-0">
{getFileIcon(file.mimeType)}
{provider ? getProviderIcon(provider) : getFileIcon(file.mimeType)}
<span className="truncate font-medium text-sm mr-2">{file.name}</span>
<Badge variant="secondary" className="text-xs px-1 py-0.5 h-auto">
<span className="text-sm text-muted-foreground">
{getMimeTypeLabel(file.mimeType)}
</Badge>
</span>
</div>
<div className="flex items-center gap-2">
<div className="flex items-center gap-1">
<span className="text-xs text-muted-foreground mr-4" title="file size">
{formatFileSize(file.size) || "—"}
</span>
<Trash
className="text-muted-foreground w-5 h-5 cursor-pointer hover:text-destructive"
<Button
className="text-muted-foreground hover:text-destructive"
size="icon"
variant="ghost"
onClick={() => onRemove(file.id)}
/>
>
<Trash2 size={16} />
</Button>
</div>
</div>
);

View file

@ -5,37 +5,50 @@ import { CloudFile } from "./types";
import { FileItem } from "./file-item";
interface FileListProps {
provider: string;
files: CloudFile[];
onClearAll: () => void;
onRemoveFile: (fileId: string) => void;
shouldDisableActions: boolean;
}
export const FileList = ({
provider,
files,
onClearAll,
onRemoveFile,
shouldDisableActions,
}: FileListProps) => {
if (files.length === 0) {
return null;
}
return (
<div className="space-y-2">
<div className="space-y-2 relative">
<div className="flex items-center justify-between">
<p className="text-sm font-medium">Added files</p>
<p className="text-sm font-medium">Added files ({files.length})</p>
<Button
ignoreTitleCase={true}
onClick={onClearAll}
size="sm"
variant="ghost"
className="text-sm text-muted-foreground"
>
Clear all
Remove all
</Button>
</div>
<div className="max-h-64 overflow-y-auto space-y-1">
{files.map(file => (
<FileItem key={file.id} file={file} onRemove={onRemoveFile} />
))}
<div className="box-shadow-inner">
<div className="max-h-[calc(100vh-720px)] overflow-y-auto space-y-1 pr-1 pb-4 relative">
{files.map((file) => (
<FileItem
key={file.id}
file={file}
onRemove={onRemoveFile}
provider={provider}
shouldDisableActions={shouldDisableActions}
/>
))}
</div>
</div>
</div>
);

View file

@ -1,14 +1,28 @@
"use client";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { ChevronRight, Info } from "lucide-react";
import { ChevronRight } from "lucide-react";
import { IngestSettings as IngestSettingsType } from "./types";
import { LabelWrapper } from "@/components/label-wrapper";
import {
Select,
SelectContent,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { ModelSelectItems } from "@/app/settings/helpers/model-select-item";
import { getFallbackModels } from "@/app/settings/helpers/model-helpers";
import { NumberInput } from "@/components/ui/inputs/number-input";
interface IngestSettingsProps {
isOpen: boolean;
@ -44,7 +58,7 @@ export const IngestSettings = ({
<Collapsible
open={isOpen}
onOpenChange={onOpenChange}
className="border rounded-md p-4 border-muted-foreground/20"
className="border rounded-xl p-4 border-border"
>
<CollapsibleTrigger className="flex items-center gap-2 justify-between w-full -m-4 p-4 rounded-md transition-colors">
<div className="flex items-center gap-2">
@ -58,35 +72,85 @@ export const IngestSettings = ({
</CollapsibleTrigger>
<CollapsibleContent className="data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:slide-up-2 data-[state=open]:slide-down-2">
<div className="pt-5 space-y-5">
<div className="flex items-center gap-4 w-full">
<div className="mt-6">
{/* Embedding model selection - currently disabled */}
<LabelWrapper
helperText="Model used for knowledge ingest and retrieval"
id="embedding-model-select"
label="Embedding model"
>
<Select
// Disabled until API supports multiple embedding models
disabled={true}
value={currentSettings.embeddingModel}
onValueChange={() => {}}
>
<Tooltip>
<TooltipTrigger asChild>
<SelectTrigger disabled id="embedding-model-select">
<SelectValue placeholder="Select an embedding model" />
</SelectTrigger>
</TooltipTrigger>
<TooltipContent>
Locked to keep embeddings consistent
</TooltipContent>
</Tooltip>
<SelectContent>
<ModelSelectItems
models={[
{
value: "text-embedding-3-small",
label: "text-embedding-3-small",
},
]}
fallbackModels={getFallbackModels("openai").embedding}
provider={"openai"}
/>
</SelectContent>
</Select>
</LabelWrapper>
</div>
<div className="mt-6">
<div className="flex items-center gap-4 w-full mb-6">
<div className="w-full">
<div className="text-sm mb-2 font-semibold">Chunk size</div>
<Input
type="number"
<NumberInput
id="chunk-size"
label="Chunk size"
value={currentSettings.chunkSize}
onChange={e =>
handleSettingsChange({
chunkSize: parseInt(e.target.value) || 0,
})
}
onChange={(value) => handleSettingsChange({ chunkSize: value })}
unit="characters"
/>
</div>
<div className="w-full">
<div className="text-sm mb-2 font-semibold">Chunk overlap</div>
<Input
type="number"
<NumberInput
id="chunk-overlap"
label="Chunk overlap"
value={currentSettings.chunkOverlap}
onChange={e =>
handleSettingsChange({
chunkOverlap: parseInt(e.target.value) || 0,
})
onChange={(value) =>
handleSettingsChange({ chunkOverlap: value })
}
unit="characters"
/>
</div>
</div>
<div className="flex gap-2 items-center justify-between">
{/* <div className="flex gap-2 items-center justify-between">
<div>
<div className="text-sm font-semibold pb-2">Table Structure</div>
<div className="text-sm text-muted-foreground">
Capture table structure during ingest.
</div>
</div>
<Switch
id="table-structure"
checked={currentSettings.tableStructure}
onCheckedChange={(checked) =>
handleSettingsChange({ tableStructure: checked })
}
/>
</div> */}
<div className="flex items-center justify-between border-b pb-3 mb-3">
<div>
<div className="text-sm font-semibold pb-2">OCR</div>
<div className="text-sm text-muted-foreground">
@ -95,13 +159,13 @@ export const IngestSettings = ({
</div>
<Switch
checked={currentSettings.ocr}
onCheckedChange={checked =>
onCheckedChange={(checked) =>
handleSettingsChange({ ocr: checked })
}
/>
</div>
<div className="flex gap-2 items-center justify-between">
<div className="flex items-center justify-between">
<div>
<div className="text-sm pb-2 font-semibold">
Picture descriptions
@ -112,26 +176,11 @@ export const IngestSettings = ({
</div>
<Switch
checked={currentSettings.pictureDescriptions}
onCheckedChange={checked =>
onCheckedChange={(checked) =>
handleSettingsChange({ pictureDescriptions: checked })
}
/>
</div>
<div>
<div className="text-sm font-semibold pb-2 flex items-center">
Embedding model
<Info className="w-3.5 h-3.5 text-muted-foreground ml-2" />
</div>
<Input
disabled
value={currentSettings.embeddingModel}
onChange={e =>
handleSettingsChange({ embeddingModel: e.target.value })
}
placeholder="text-embedding-3-small"
/>
</div>
</div>
</CollapsibleContent>
</Collapsible>

View file

@ -51,19 +51,13 @@ export const PickerHeader = ({
Select files from {getProviderName(provider)} to ingest.
</p>
<Button
size="sm"
onClick={onAddFiles}
disabled={!isPickerLoaded || isPickerOpen || !accessToken}
className="bg-foreground text-background hover:bg-foreground/90 font-semibold"
>
<Plus className="h-4 w-4" />
Add Files
{isPickerOpen ? "Opening picker..." : "Add files"}
</Button>
<div className="text-xs text-muted-foreground pt-4">
csv, json, pdf,{" "}
<a className="underline dark:text-pink-400 text-pink-600">+16 more</a>{" "}
<b>150 MB</b> max
</div>
</CardContent>
</Card>
);

View file

@ -25,6 +25,7 @@ export interface UnifiedCloudPickerProps {
baseUrl?: string;
// Ingest settings
onSettingsChange?: (settings: IngestSettings) => void;
isIngesting: boolean;
}
export interface GoogleAPI {

View file

@ -16,6 +16,7 @@ export const UnifiedCloudPicker = ({
onFileSelected,
selectedFiles = [],
isAuthenticated,
isIngesting,
accessToken,
onPickerStateChange,
clientId,
@ -116,7 +117,7 @@ export const UnifiedCloudPicker = ({
const handler = createProviderHandler(
provider,
accessToken,
isOpen => {
(isOpen) => {
setIsPickerOpen(isOpen);
onPickerStateChange?.(isOpen);
},
@ -126,8 +127,8 @@ export const UnifiedCloudPicker = ({
handler.openPicker((files: CloudFile[]) => {
// Merge new files with existing ones, avoiding duplicates
const existingIds = new Set(selectedFiles.map(f => f.id));
const newFiles = files.filter(f => !existingIds.has(f.id));
const existingIds = new Set(selectedFiles.map((f) => f.id));
const newFiles = files.filter((f) => !existingIds.has(f.id));
onFileSelected([...selectedFiles, ...newFiles]);
});
} catch (error) {
@ -138,7 +139,7 @@ export const UnifiedCloudPicker = ({
};
const handleRemoveFile = (fileId: string) => {
const updatedFiles = selectedFiles.filter(file => file.id !== fileId);
const updatedFiles = selectedFiles.filter((file) => file.id !== fileId);
onFileSelected(updatedFiles);
};
@ -168,20 +169,24 @@ export const UnifiedCloudPicker = ({
}
return (
<div className="space-y-6">
<PickerHeader
provider={provider}
onAddFiles={handleAddFiles}
isPickerLoaded={isPickerLoaded}
isPickerOpen={isPickerOpen}
accessToken={accessToken}
isAuthenticated={isAuthenticated}
/>
<div>
<div className="mb-6">
<PickerHeader
provider={provider}
onAddFiles={handleAddFiles}
isPickerLoaded={isPickerLoaded}
isPickerOpen={isPickerOpen}
accessToken={accessToken}
isAuthenticated={isAuthenticated}
/>
</div>
<FileList
provider={provider}
files={selectedFiles}
onClearAll={handleClearAll}
onRemoveFile={handleRemoveFile}
shouldDisableActions={isIngesting}
/>
<IngestSettings

View file

@ -60,12 +60,12 @@ export function LayoutWrapper({ children }: { children: React.ReactNode }) {
const isOnKnowledgePage = pathname.startsWith("/knowledge");
// List of paths with smaller max-width
const smallWidthPaths = ["/settings", "/settings/connector/new"];
const smallWidthPaths = ["/settings/connector/new"];
const isSmallWidthPath = smallWidthPaths.includes(pathname);
// Calculate active tasks for the bell icon
const activeTasks = tasks.filter(
(task) =>
task =>
task.status === "pending" ||
task.status === "running" ||
task.status === "processing"

View file

@ -19,6 +19,7 @@ import {
import { useAuth } from "@/contexts/auth-context";
// Task interface is now imported from useGetTasksQuery
export type { Task };
export interface TaskFile {
filename: string;

104
src/api/docling.py Normal file
View file

@ -0,0 +1,104 @@
"""Docling service proxy endpoints."""
import socket
import struct
from pathlib import Path
import httpx
from starlette.requests import Request
from starlette.responses import JSONResponse
from utils.container_utils import (
detect_container_environment,
get_container_host,
guess_host_ip_for_containers,
)
from utils.logging_config import get_logger
logger = get_logger(__name__)
def _get_gateway_ip_from_route() -> str | None:
"""Return the default gateway IP visible from the current network namespace."""
try:
with Path("/proc/net/route").open() as route_table:
next(route_table) # Skip header
for line in route_table:
fields = line.strip().split()
min_fields = 3 # interface, destination, gateway
if len(fields) >= min_fields and fields[1] == "00000000":
gateway_hex = fields[2]
gw_int = int(gateway_hex, 16)
gateway_ip = socket.inet_ntoa(struct.pack("<L", gw_int))
return gateway_ip
except (FileNotFoundError, PermissionError, IndexError, ValueError) as err:
logger.warning("Could not read routing table: %s", err)
return None
def determine_docling_host() -> str:
"""Determine the host address used for docling health checks."""
container_type = detect_container_environment()
if container_type:
container_host = get_container_host()
if container_host:
logger.info("Using container-aware host '%s'", container_host)
return container_host
gateway_ip = _get_gateway_ip_from_route()
if gateway_ip:
logger.info("Detected host gateway IP: %s", gateway_ip)
return gateway_ip
# Either we're not inside a container or gateway detection failed.
fallback_ip = guess_host_ip_for_containers(logger=logger)
if container_type:
logger.info("Falling back to container bridge host %s", fallback_ip)
else:
logger.info("Running outside a container; using host %s", fallback_ip)
return fallback_ip
# Detect the host IP once at startup
HOST_IP = determine_docling_host()
DOCLING_SERVICE_URL = f"http://{HOST_IP}:5001"
async def health(request: Request) -> JSONResponse:
"""
Proxy health check to docling-serve.
This allows the frontend to check docling status via same-origin request.
"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"{DOCLING_SERVICE_URL}/health",
timeout=2.0
)
if response.status_code == 200:
return JSONResponse({
"status": "healthy",
"host": HOST_IP
})
else:
return JSONResponse({
"status": "unhealthy",
"message": f"Health check failed with status: {response.status_code}",
"host": HOST_IP
}, status_code=503)
except httpx.TimeoutException:
return JSONResponse({
"status": "unhealthy",
"message": "Connection timeout",
"host": HOST_IP
}, status_code=503)
except Exception as e:
logger.error("Docling health check failed", error=str(e))
return JSONResponse({
"status": "unhealthy",
"message": str(e),
"host": HOST_IP
}, status_code=503)