Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ VITE_DISABLE_FEEDBACK_FEATURE=true
VITE_ENABLE_DOCUNINJA=false

VITE_DISABLE_QUICKBOOKS_INTEGRATION=true

VITE_AI_PROVIDER=
VITE_AI_MODEL=
VITE_OPENAI_API_KEY=
VITE_GEMINI_API_KEY=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"check": "biome check --write --unsafe src/",
"test": "vitest run",
"test:watch": "vitest",
"eval:ai": "vitest run --config tests/ai/vitest.config.ts",
"test:e2e": "playwright test --workers=1",
"test:e2e:parallel": "node scripts/playwright-spec-orchestrator.mjs",
"test:e2e:isolated": "node scripts/playwright-spec-orchestrator.mjs",
Expand Down
142 changes: 142 additions & 0 deletions src/common/interfaces/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import { ModuleBitmask } from '$app/common/enums/module-bitmask';
import { Permissions } from '$app/common/hooks/permissions/useHasPermission';
import { RefetchKey } from '$app/common/hooks/useRefetch';

export interface AiAccess {
permission: Permissions;
bitmask: ModuleBitmask;
}

export type AiInput = 'text' | 'date' | 'select' | 'client';

export interface AiOption {
value: string;
label: string;
}

export interface AiSummaryField {
key: string;
label: string;
value: string;
text: string;
input?: AiInput;
options?: AiOption[];
}

export interface AiSummaryItem {
label: string;
detail: string;
value: string;
}

export interface AiSummary {
title: string;
fields: AiSummaryField[];
items?: AiSummaryItem[];
totals?: AiSummaryField[];
}

export interface AiCreated {
entity: RefetchKey;
id: string;
label: string;
detail?: string;
}

export interface AiFailure {
label: string;
message: string;
}

export type AiPlanStatus =
| 'collecting'
| 'ready'
| 'confirmed'
| 'executing'
| 'done'
| 'failed'
| 'cancelled';

export interface AiStatus {
type: 'status';
label: string;
}

export interface AiReply {
type: 'reply';
text: string;
points?: string[];
closing?: string;
suggestions?: string[];
}

export interface AiQuestion {
type: 'question';
field: string;
text: string;
input: AiInput;
options?: AiOption[];
}

export interface AiPlan {
type: 'plan';
planId: string;
version: number;
module: string;
status: AiPlanStatus;
summary: AiSummary;
warnings: string[];
editable: boolean;
}

export interface AiProgress {
type: 'progress';
planId: string;
done: number;
total: number;
}

export interface AiResult {
type: 'result';
planId: string;
created: AiCreated[];
failed: AiFailure[];
}

export interface AiError {
type: 'error';
message: string;
}

export type AiEvent =
| AiStatus
| AiReply
| AiQuestion
| AiPlan
| AiProgress
| AiResult
| AiError;

export interface AiRun {
provider: string;
model: string;
inputTokens: number;
outputTokens: number;
cost: number;
}

export interface AiUsage {
provider: string;
model: string;
runs: AiRun[];
}
58 changes: 58 additions & 0 deletions src/common/queries/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import { AiAccess, AiEvent, AiUsage } from '$app/common/interfaces/ai';
import * as server from '$app/server/ai';

export const sendMessage = (
conversationId: string,
text: string,
value?: string
): AsyncIterable<AiEvent> => {
return server.sendMessage(conversationId, text, value);
};

export const updatePlan = (
planId: string,
field: string,
value: string
): AsyncIterable<AiEvent> => {
return server.updatePlan(planId, field, value);
};

export const confirmPlan = (
planId: string,
version: number
): AsyncIterable<AiEvent> => {
return server.confirmPlan(planId, version);
};

export const cancelPlan = (planId: string): AsyncIterable<AiEvent> => {
return server.cancelPlan(planId);
};

export const completePlan = (
planId: string,
record: object
): AsyncIterable<AiEvent> => {
return server.completePlan(planId, record);
};

export const draftPlan = (planId: string): Promise<object | undefined> => {
return server.draftPlan(planId);
};

export const usage = (): Promise<AiUsage> => {
return server.usage();
};

export const access = (): AiAccess[] => {
return server.access();
};
49 changes: 49 additions & 0 deletions src/components/ai/AiAssistant.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import classNames from 'classnames';
import { useEffect, useRef } from 'react';
import { useReactSettings } from '$app/common/hooks/useReactSettings';
import { ChatPanel } from './ChatPanel';
import { useAiAssistant, useIsAiAvailable } from './common/hooks';
import { Composer } from './Composer';

export function AiAssistant() {
const { isOpen } = useAiAssistant();
const isAvailable = useIsAiAvailable();
const reactSettings = useReactSettings();

const wasOpen = useRef(isOpen);

useEffect(() => {
wasOpen.current = isOpen;
}, [isOpen]);

if (!isAvailable) {
return null;
}

if (isOpen) {
return <ChatPanel autoFocus={!wasOpen.current} />;
}

return (
<div
className={classNames(
'pointer-events-none fixed inset-x-0 bottom-5 z-[5] flex justify-center px-4',
reactSettings.show_mini_sidebar ? 'md:left-16' : 'md:left-64'
)}
>
<div className="pointer-events-auto w-full max-w-2xl">
<Composer />
</div>
</div>
);
}
43 changes: 43 additions & 0 deletions src/components/ai/Bubble.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import classNames from 'classnames';
import { ReactNode } from 'react';
import { useColorScheme } from '$app/common/colors';
import { useAccentColor } from '$app/common/hooks/useAccentColor';

interface Props {
isUser?: boolean;
children: ReactNode;
}

export function Bubble({ isUser, children }: Props) {
const colors = useColorScheme();
const accentColor = useAccentColor();

return (
<div
className={classNames(
'w-fit max-w-[85%] whitespace-pre-wrap break-words rounded-2xl px-4 py-2.5 text-sm leading-6',
isUser ? 'ml-auto rounded-br-md' : 'rounded-bl-md'
)}
style={
isUser
? { backgroundColor: colors.$18, color: colors.$1 }
: {
backgroundColor: `color-mix(in srgb, ${accentColor} 10%, ${colors.$2})`,
color: colors.$3,
}
}
>
{children}
</div>
);
}
61 changes: 61 additions & 0 deletions src/components/ai/ChatEntry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import { LuAlertTriangle } from 'react-icons/lu';
import { Icon } from '$app/components/icons/Icon';
import { Bubble } from './Bubble';
import { AiEntry } from './common/hooks';
import { PlanSummary } from './PlanSummary';
import { QuestionEntry } from './QuestionEntry';
import { ReplyEntry } from './ReplyEntry';
import { ResultCard } from './ResultCard';

interface Props {
entry: AiEntry;
isActive: boolean;
isLast: boolean;
}

export function ChatEntry({ entry, isActive, isLast }: Props) {
if (entry.type === 'message') {
return <Bubble isUser>{entry.text}</Bubble>;
}

if (entry.type === 'reply') {
return <ReplyEntry reply={entry} isLast={isLast} />;
}

if (entry.type === 'question') {
return <QuestionEntry question={entry} isActive={isActive} />;
}

if (entry.type === 'plan') {
return <PlanSummary plan={entry} isActive={isActive} />;
}

if (entry.type === 'result') {
return <ResultCard result={entry} />;
}

return (
<Bubble>
<span className="flex items-start gap-2">
<Icon
element={LuAlertTriangle}
size={16}
color="#ef4444"
className="mt-1 shrink-0"
/>

<span>{entry.message}</span>
</span>
</Bubble>
);
}
Loading
Loading