Skip to content

Commit

Permalink
Merge branch 'main' of github.com:cohere-ai/cohere-toolkit into stagi…
Browse files Browse the repository at this point in the history
…ng/deploy
  • Loading branch information
BeatrixCohere committed Aug 15, 2024
2 parents bbd888d + 82ba73f commit 0711cc9
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 106 deletions.
4 changes: 2 additions & 2 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* @cohere-ai/toolkit @EugeneLightsOn @malexw @scottmx81 @tianjing-li
src/interfaces/coral_web @tomtobac @abimacarmes @knajjars
src/interfaces/assistants_web @tomtobac @abimacarmes @knajjars
src/interfaces/coral_web @tomtobac @abimacarmes @knajjars @BeatrixCohere
src/interfaces/assistants_web @tomtobac @abimacarmes @knajjars @BeatrixCohere
10 changes: 5 additions & 5 deletions src/backend/services/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ def invoke(
index_name=parameters["index"]
)
case self.ValidActions.CREATE.value:
self._create(parameters, **kwargs)
return self._create(parameters, **kwargs)
case self.ValidActions.SEARCH.value:
return self._search(parameters, **kwargs)
case self.ValidActions.UPDATE.value:
self._update(parameters, **kwargs)
return self._update(parameters, **kwargs)
case self.ValidActions.DELETE.value:
self._delete(parameters, **kwargs)
return self._delete(parameters, **kwargs)
case self.ValidActions.GET_DOCUMENT.value:
return self._get_document(parameters, **kwargs)
case self.ValidActions.ADD_CONTEXT.value:
self._add_context(parameters, **kwargs)
return self._add_context(parameters, **kwargs)
case self.ValidActions.REFRESH.value:
self._refresh(parameters, **kwargs)
return self._refresh(parameters, **kwargs)
case self.ValidActions.PROCESS_FILE.value:
return self._process_file(parameters, **kwargs)
case _:
Expand Down
11 changes: 10 additions & 1 deletion src/backend/services/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ def delete_all_conversation_files(
if self.is_compass_enabled:
compass = get_compass()
try:
logger.info(
event=f"[Compass File Service] Deleting conversation {conversation_id} files from Compass"
)
compass.invoke(
action=Compass.ValidActions.DELETE_INDEX,
parameters={"index": conversation_id},
Expand Down Expand Up @@ -377,6 +380,9 @@ def delete_file_in_compass(
compass = get_compass()

try:
logger.info(
event=f"[Compass File Service] Deleting file {file_id} from Compass {index}"
)
compass.invoke(
action=Compass.ValidActions.DELETE,
parameters={"index": index, "file_id": file_id},
Expand Down Expand Up @@ -496,9 +502,12 @@ async def consolidate_agent_files_in_compass(
action=Compass.ValidActions.REFRESH,
parameters={"index": agent_id},
)
logger.info(
event=f"[Compass File Service] Delete temporary file index: {file_id}"
)
# Remove the temporary file index entry
compass.invoke(
action=Compass.ValidActions.DELETE_INDEX, parameters={"index": file_id}
action=Compass.ValidActions.DELETE_INDEX, parameters={"index": agent_id}
)
except Exception as e:
logger.error(
Expand Down
2 changes: 1 addition & 1 deletion src/backend/tools/google_drive/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GoogleDrive(BaseTool):

@classmethod
def is_available(cls) -> bool:
return cls.CLIENT_ID is not None and cls.CLIENT_ID is not None
return cls.CLIENT_ID is not None and cls.CLIENT_SECRET is not None

def _handle_tool_specific_errors(cls, error: Exception, **kwargs: Any):
message = "[Google Drive] Tool Error: {}".format(str(error))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextPage } from 'next';

import { UpdateAgent } from '@/components/Agents/UpdateAgent';
import { getCohereServerClient } from '@/server/cohereServerClient';

type Props = {
params: {
Expand All @@ -9,12 +10,11 @@ type Props = {
searchParams: Record<string, string>;
};

const Page: NextPage<Props> = ({ params }) => {
return (
<div className="h-full w-full rounded-lg border border-marble-950 bg-marble-980 dark:border-volcanic-150 dark:bg-volcanic-100">
<UpdateAgent agentId={params.agentId} />
</div>
);
const Page: NextPage<Props> = async ({ params }) => {
const cohereServerClient = getCohereServerClient();
const agent = await cohereServerClient.getAgent(params.agentId);

return <UpdateAgent agent={agent} />;
};

export default Page;
9 changes: 1 addition & 8 deletions src/interfaces/assistants_web/src/app/(main)/new/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Metadata } from 'next';
import { Suspense } from 'react';

import { CreateAgent } from '@/components/Agents/CreateAgent';

Expand All @@ -8,13 +7,7 @@ export const metadata: Metadata = {
};

const NewAssistantPage: React.FC = () => {
return (
<Suspense>
<div className="h-full w-full rounded-lg border border-marble-950 bg-marble-980 dark:border-volcanic-150 dark:bg-volcanic-100">
<CreateAgent />
</div>
</Suspense>
);
return <CreateAgent />;
};

export default NewAssistantPage;
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const AgentSettingsForm: React.FC<Props> = (props) => {
};

return (
<div className="flex flex-col space-y-6 p-8">
<div className="flex flex-col space-y-6">
{/* Step 1: Define your assistant - name, description, instruction */}
<CollapsibleSection
title="Define your assistant"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const CreateAgent: React.FC = () => {
};

return (
<div className="relative flex h-full w-full flex-col overflow-y-auto">
<div className="flex h-full w-full flex-col overflow-y-auto">
<header className="flex flex-col gap-y-3 border-b px-4 py-6 dark:border-volcanic-150 lg:px-10 lg:py-10">
<MobileHeader />
<div className="flex items-center space-x-2">
Expand All @@ -114,14 +114,16 @@ export const CreateAgent: React.FC = () => {
</div>
<Text styleAs="h4">Create assistant</Text>
</header>
<div className="overflow-y-auto">
<AgentSettingsForm
source="create"
fields={fields}
setFields={setFields}
onSubmit={handleOpenSubmitModal}
savePendingAssistant={() => setPendingAssistant(fields)}
/>
<div className="flex flex-grow flex-col gap-y-8 overflow-y-hidden px-8 pt-8">
<div className="flex-grow overflow-y-auto">
<AgentSettingsForm
source="create"
fields={fields}
setFields={setFields}
onSubmit={handleOpenSubmitModal}
savePendingAssistant={() => setPendingAssistant(fields)}
/>
</div>
</div>
</div>
);
Expand Down
85 changes: 33 additions & 52 deletions src/interfaces/assistants_web/src/components/Agents/UpdateAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import { useLocalStorageValue } from '@react-hookz/web';
import Link from 'next/link';
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';

import { AgentPublic } from '@/cohere-client';
import {
AgentSettingsFields,
AgentSettingsForm,
Expand All @@ -13,22 +14,30 @@ import { MobileHeader } from '@/components/MobileHeader';
import { Button, Icon, Spinner, Text } from '@/components/Shared';
import { DEFAULT_AGENT_MODEL, DEPLOYMENT_COHERE_PLATFORM } from '@/constants';
import { useContextStore } from '@/context';
import { useAgent, useIsAgentNameUnique, useUpdateAgent } from '@/hooks/agents';
import { useIsAgentNameUnique, useUpdateAgent } from '@/hooks/agents';
import { useNotify } from '@/hooks/toast';

type Props = {
agentId: string;
agent: AgentPublic;
};

export const UpdateAgent: React.FC<Props> = ({ agentId }) => {
export const UpdateAgent: React.FC<Props> = ({ agent }) => {
const { error, success } = useNotify();
const { data: agent, isLoading } = useAgent({ agentId });
const { open, close } = useContextStore();

const { mutateAsync: updateAgent } = useUpdateAgent();
const isAgentNameUnique = useIsAgentNameUnique();
const [isSubmitting, setIsSubmitting] = useState(false);
const [fields, setFields] = useState<AgentSettingsFields>();
const [fields, setFields] = useState<AgentSettingsFields>({
name: agent.name,
description: agent.description,
deployment: agent.deployment ?? DEPLOYMENT_COHERE_PLATFORM,
model: agent.model ?? DEFAULT_AGENT_MODEL,
tools: agent.tools,
preamble: agent.preamble,
tools_metadata: agent.tools_metadata,
is_private: agent.is_private,
});

const { set: setPendingAssistant } = useLocalStorageValue<AgentSettingsFields>(
'pending_assistant',
Expand All @@ -38,54 +47,24 @@ export const UpdateAgent: React.FC<Props> = ({ agentId }) => {
}
);

useEffect(() => {
if (agent) {
setFields({
name: agent.name,
description: agent.description,
deployment: agent.deployment ?? DEPLOYMENT_COHERE_PLATFORM,
model: agent.model ?? DEFAULT_AGENT_MODEL,
tools: agent.tools,
preamble: agent.preamble,
tools_metadata: agent.tools_metadata,
is_private: agent.is_private,
});
}
}, [agent]);

const handleOpenDeleteModal = () => {
if (!agent || !agent.name || !agentId) return;
open({
title: `Delete ${agent.name}`,
content: <DeleteAgent name={agent.name} agentId={agentId} onClose={close} />,
content: <DeleteAgent name={agent.name} agentId={agent.id} onClose={close} />,
});
};

if (isLoading && !fields) {
return (
<div className="flex h-full w-full items-center justify-center">
<Spinner />
</div>
);
}

if (!agent || !fields) {
return (
<div className="flex h-full w-full items-center justify-center">
<Text className="text-danger-350">Unable to load assistant information</Text>
</div>
);
}

const handleSubmit = async () => {
if (!agentId) return;
const tools_metadata = (fields.tools_metadata ?? []).map((tool) => ({
...tool,
id: agent.tools_metadata?.find((t) => t.tool_name === tool.tool_name)?.id,
}));
try {
setIsSubmitting(true);
const newAgent = await updateAgent({ request: { ...fields, tools_metadata }, agentId });
const newAgent = await updateAgent({
request: { ...fields, tools_metadata },
agentId: agent.id,
});
setIsSubmitting(false);
success(`Updated ${newAgent?.name}`);
} catch (e) {
Expand All @@ -96,7 +75,7 @@ export const UpdateAgent: React.FC<Props> = ({ agentId }) => {
};

return (
<div className="relative flex h-full w-full flex-col overflow-y-auto">
<div className="flex h-full w-full flex-col overflow-y-auto">
<header className="flex flex-col gap-y-3 border-b px-4 py-6 dark:border-volcanic-150 lg:px-10 lg:py-10">
<MobileHeader />
<div className="flex items-center space-x-2">
Expand All @@ -108,16 +87,18 @@ export const UpdateAgent: React.FC<Props> = ({ agentId }) => {
</div>
<Text styleAs="h4">Edit {agent.name}</Text>
</header>
<div className="flex flex-col overflow-y-auto">
<AgentSettingsForm
source="update"
fields={fields}
setFields={setFields}
onSubmit={handleSubmit}
savePendingAssistant={() => setPendingAssistant(fields)}
agentId={agentId}
/>
<div className="space-y-5 p-8">
<div className="flex flex-grow flex-col gap-y-8 overflow-y-hidden px-8 pt-8">
<div className="flex-grow overflow-y-auto">
<AgentSettingsForm
source="update"
fields={fields}
setFields={setFields}
onSubmit={handleSubmit}
savePendingAssistant={() => setPendingAssistant(fields)}
agentId={agent.id}
/>
</div>
<div className="space-y-5 pb-8">
<div className="flex w-full max-w-screen-md items-center justify-between ">
<Button label="Cancel" kind="secondary" href="/discover" />
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { ReactNode, useEffect, useRef } from 'react';
import { ReactNode } from 'react';

import { Icon, Text } from '@/components/Shared';
import { cn } from '@/utils';
Expand All @@ -21,20 +21,8 @@ export const CollapsibleSection: React.FC<Props> = ({
isExpanded = false,
setIsExpanded,
}) => {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (isExpanded) {
ref.current?.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'nearest',
});
}
}, [isExpanded]);

return (
<div
ref={ref}
className={cn(
'flex w-full max-w-screen-md flex-col rounded-md',
'space-y-5 border p-6',
Expand Down
6 changes: 5 additions & 1 deletion src/interfaces/assistants_web/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const TOOL_CALCULATOR_ID = 'toolkit_calculator';
export const TOOL_WEB_SCRAPE_ID = 'web_scrape';
export const TOOL_GOOGLE_DRIVE_ID = 'google_drive';
export const FILE_UPLOAD_TOOLS = [TOOL_SEARCH_FILE_ID, TOOL_READ_DOCUMENT_ID];
export const AGENT_SETTINGS_TOOLS = [TOOL_WEB_SEARCH_ID, TOOL_PYTHON_INTERPRETER_ID];
export const AGENT_SETTINGS_TOOLS = [
TOOL_WEB_SEARCH_ID,
TOOL_PYTHON_INTERPRETER_ID,
TOOL_WEB_SCRAPE_ID,
];

export const TOOL_FALLBACK_ICON = 'circles-four';
export const TOOL_ID_TO_DISPLAY_INFO: { [id: string]: { icon: IconName } } = {
Expand Down
11 changes: 4 additions & 7 deletions src/interfaces/assistants_web/src/hooks/brandedColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,10 @@ const getAssistantContrastColor = (assistantId: string | undefined): string => {
};

export const getCohereTheme = (assistantId?: string): COHERE_BRANDED_COLORS => {
if (assistantId === undefined) {
return COHERE_THEMES_MAP.default;
} else {
const idNumber = assistantId.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
const index = idNumber % COHERE_THEMES_MAP.branded.length;
return COHERE_THEMES_MAP.branded[index];
}
if (!assistantId) return COHERE_THEMES_MAP.default;
const idNumber = assistantId.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
const index = idNumber % COHERE_THEMES_MAP.branded.length;
return COHERE_THEMES_MAP.branded[index];
};

const getDarkMode = (color: string) => `dark:${color}`;
Expand Down

0 comments on commit 0711cc9

Please sign in to comment.