🚧 chore: merge latest dev build to main repo (#3844)

* agents - phase 1 (#30)

* chore: copy assistant files

* feat: frontend and data-provider

* feat: backend get endpoint test

* fix(MessageEndpointIcon): switched to AgentName and AgentAvatar

* fix: small fixes

* fix: agent endpoint config

* fix: show Agent Builder

* chore: install agentus

* chore: initial scaffolding for agents

* fix: updated Assistant logic to Agent Logic for some Agent components

* WIP first pass, demo of agent package

* WIP: initial backend infra for agents

* fix: agent list error

* wip: agents routing

* chore: Refactor useSSE hook to handle different data events

* wip: correctly emit events

* chore: Update @librechat/agentus npm dependency to version 1.0.9

* remove comment

* first pass: streaming agent text

* chore: Remove @librechat/agentus root-level workspace npm dependency

* feat: Agent Schema and Model

* fix: content handling fixes

* fix: content message save

* WIP: new content data

* fix: run step issue with tool calls

* chore: Update @librechat/agentus npm dependency to version 1.1.5

* feat: update controller and agent routes

* wip: initial backend tool and tool error handling support

* wip: tool chunks

* chore: Update @librechat/agentus npm dependency to version 1.1.7

* chore: update tool_call typing, add test conditions and logs

* fix: create agent

* fix: create agent

* first pass: render completed content parts

* fix: remove logging, fix step handler typing

* chore: Update @librechat/agentus npm dependency to version 1.1.9

* refactor: cleanup maps on unmount

* chore: Update BaseClient.js to safely count tokens for string, number, and boolean values

* fix: support subsequent messages with tool_calls

* chore: export order

* fix: select agent

* fix: tool call types and handling

* chore: switch to anthropic for testing

* fix: AgentSelect

* refactor: experimental: OpenAIClient to use array for intermediateReply

* fix(useSSE): revert old condition for streaming legacy client tokens

* fix: lint

* revert `agent_id` to `id`

* chore: update localization keys for agent-related components

* feat: zod schema handling for actions

* refactor(actions): if no params, no zodSchema

* chore: Update @librechat/agentus npm dependency to version 1.2.1

* feat: first pass, actions

* refactor: empty schema for actions without params

* feat: Update createRun function to accept additional options

* fix: message payload formatting; feat: add more client options

* fix: ToolCall component rendering when action has no args but has output

* refactor(ToolCall): allow non-stringy args

* WIP: first pass, correctly formatted tool_calls between providers

* refactor: Remove duplicate import of 'roles' module

* refactor: Exclude 'vite.config.ts' from TypeScript compilation

* refactor: fix agent related types
> - no need to use endpoint/model fields for identifying agent metadata
> - add `provider` distinction for agent-configured 'endpoint'
- no need for agent-endpoint map
- reduce complexity of tools as functions into tools as string[]
- fix types related to above changes
- reduce unnecessary variables for queries/mutations and corresponding react-query keys

* refactor: Add tools and tool_kwargs fields to agent schema

* refactor: Remove unused code and update dependencies

* refactor: Update updateAgentHandler to use req.body directly

* refactor: Update AgentSelect component to use localized hooks

* refactor: Update agent schema to include tools and provider fields

* refactor(AgentPanel): add scrollbar gutter, add provider field to form, fix agent schema required values

* refactor: Update AgentSwitcher component to use selectedAgentId instead of selectedAgent

* refactor: Update AgentPanel component to include alternateName import and defaultAgentFormValues

* refactor(SelectDropDown): allow setting value as option while still supporting legacy usage (string values only)

* refactor: SelectDropdown changes - Only necessary when the available values are objects with label/value fields and the selected value is expected to be a string.

* refactor: TypeError issues and handle provider as option

* feat: Add placeholder for provider selection in AgentPanel component

* refactor: Update agent schema to include author and provider fields

* fix: show expected 'create agent' placeholder when creating agent

* chore: fix localization strings, hide capabilities form for now

* chore: typing

* refactor: import order and use compact agents schema for now

* chore: typing

* refactor: Update AgentForm type to use AgentCapabilities

* fix agent form agent selection issues

* feat: responsive agent selection

* fix: Handle cancelled fetch in useSelectAgent hook

* fix: reset agent form on accordion close/open

* feat: Add agent_id to default conversation for agents endpoint

* feat: agents endpoint request handling

* refactor: reset conversation model on agent select

* refactor: add `additional_instructions` to conversation schema, organize other fields

* chore: casing

* chore: types

* refactor(loadAgentTools): explicitly pass agent_id, do not pass `model` to loadAgentTools for now, load action sets by agent_id

* WIP: initial draft of real agent client initialization

* WIP: first pass, anthropic agent requests

* feat: remember last selected agent

* feat: openai and azure connected

* fix: prioritize agent model for runs unless an explicit override model is passed from client

* feat: Agent Actions

* fix: save agent id to convo

* feat: model panel (#29)

* feat: model panel

* bring back comments

* fix: method still null

* fix: AgentPanel FormContext

* feat: add more parameters

* fix: style issues; refactor: Agent Controller

* fix: cherry-pick

* fix: Update AgentAvatar component to use AssistantIcon instead of BrainCircuit

* feat: OGDialog for delete agent; feat(assistant): update Agent types, introduced `model_parameters`

* feat: icon and general `model_parameters` update

* feat: use react-hook-form better

* fix: agent builder form reset issue when switching panels

* refactor: modularize agent builder form

---------

Co-authored-by: Danny Avila <danny@librechat.ai>

* fix: AgentPanel and ModelPanel type issues and use `useFormContext` and `watch` instead of `methods` directly and `useWatch`.

* fix: tool call issues due to invalid input (anthropic) of empty string

* fix: handle empty text in Part component

---------

Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>

* refactor: remove form ModelPanel and fixed nested ternary expressions in AgentConfig

* fix: Model Parameters not saved correctly

* refactor: remove console log

* feat: avatar upload and get for Agents (#36)

Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>

* chore: update to public package

* fix: typing, optional chaining

* fix: cursor not showing for content parts

* chore: conditionally enable agents

* ci: fix azure test

* ci: fix frontend tests, fix eslint api

* refactor: Remove unused errorContentPart variable

* continue of the agent message PR (#40)

* last fixes

* fix: agentMap

* pr merge test  (#41)

* fix: model icon not fetching correctly

* remove console logs

* feat: agent name

* refactor: pass documentsMap as a prop to allow re-render of assistant form

* refactor: pass documentsMap as a prop to allow re-render of assistant form

* chore: Bump version to 0.7.419

* fix: TypeError: Cannot read properties of undefined (reading 'id')

* refactor: update AgentSwitcher component to use ControlCombobox instead of Combobox

---------

Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
This commit is contained in:
Danny Avila 2024-08-31 16:33:51 -04:00 committed by GitHub
parent 618be4bf2b
commit a0291ed155
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
141 changed files with 14473 additions and 5714 deletions

View file

@ -1,4 +1,5 @@
import axios from 'axios';
import { z } from 'zod';
import { OpenAPIV3 } from 'openapi-types';
import {
createURL,
@ -8,7 +9,12 @@ import {
FunctionSignature,
validateAndParseOpenAPISpec,
} from '../src/actions';
import { getWeatherOpenapiSpec, whimsicalOpenapiSpec, scholarAIOpenapiSpec } from './openapiSpecs';
import {
getWeatherOpenapiSpec,
whimsicalOpenapiSpec,
scholarAIOpenapiSpec,
swapidev,
} from './openapiSpecs';
import { AuthorizationTypeEnum, AuthTypeEnum } from '../src/types/assistants';
import type { FlowchartSchema } from './openapiSpecs';
import type { ParametersSchema } from '../src/actions';
@ -548,4 +554,273 @@ describe('createURL', () => {
'https://example.com/subdirectory/api/v1/users',
);
});
describe('openapiToFunction zodSchemas', () => {
describe('getWeatherOpenapiSpec', () => {
const { zodSchemas } = openapiToFunction(getWeatherOpenapiSpec, true);
it('generates correct Zod schema for GetCurrentWeather', () => {
expect(zodSchemas).toBeDefined();
expect(zodSchemas?.GetCurrentWeather).toBeDefined();
const GetCurrentWeatherSchema = zodSchemas?.GetCurrentWeather;
expect(GetCurrentWeatherSchema instanceof z.ZodObject).toBe(true);
if (!(GetCurrentWeatherSchema instanceof z.ZodObject)) {
throw new Error('GetCurrentWeatherSchema is not a ZodObject');
}
const shape = GetCurrentWeatherSchema.shape;
expect(shape.location instanceof z.ZodString).toBe(true);
// Check locations property
expect(shape.locations).toBeDefined();
expect(shape.locations instanceof z.ZodOptional).toBe(true);
if (!(shape.locations instanceof z.ZodOptional)) {
throw new Error('locations is not a ZodOptional');
}
const locationsInnerType = shape.locations._def.innerType;
expect(locationsInnerType instanceof z.ZodArray).toBe(true);
if (!(locationsInnerType instanceof z.ZodArray)) {
throw new Error('locationsInnerType is not a ZodArray');
}
const locationsItemSchema = locationsInnerType.element;
expect(locationsItemSchema instanceof z.ZodObject).toBe(true);
if (!(locationsItemSchema instanceof z.ZodObject)) {
throw new Error('locationsItemSchema is not a ZodObject');
}
// Validate the structure of locationsItemSchema
expect(locationsItemSchema.shape.city instanceof z.ZodString).toBe(true);
expect(locationsItemSchema.shape.state instanceof z.ZodString).toBe(true);
expect(locationsItemSchema.shape.countryCode instanceof z.ZodString).toBe(true);
// Check if time is optional
const timeSchema = locationsItemSchema.shape.time;
expect(timeSchema instanceof z.ZodOptional).toBe(true);
if (!(timeSchema instanceof z.ZodOptional)) {
throw new Error('timeSchema is not a ZodOptional');
}
expect(timeSchema._def.innerType instanceof z.ZodString).toBe(true);
// Check the description
expect(shape.locations._def.description).toBe(
'A list of locations to retrieve the weather for.',
);
});
it('validates correct data for GetCurrentWeather', () => {
const GetCurrentWeatherSchema = zodSchemas?.GetCurrentWeather as z.ZodTypeAny;
const validData = {
location: 'New York',
locations: [
{ city: 'New York', state: 'NY', countryCode: 'US', time: '2023-12-04T14:00:00Z' },
],
};
expect(() => GetCurrentWeatherSchema.parse(validData)).not.toThrow();
});
it('throws error for invalid data for GetCurrentWeather', () => {
const GetCurrentWeatherSchema = zodSchemas?.GetCurrentWeather as z.ZodTypeAny;
const invalidData = {
location: 123,
locations: [{ city: 'New York', state: 'NY', countryCode: 'US', time: 'invalid-time' }],
};
expect(() => GetCurrentWeatherSchema.parse(invalidData)).toThrow();
});
});
describe('whimsicalOpenapiSpec', () => {
const { zodSchemas } = openapiToFunction(whimsicalOpenapiSpec, true);
it('generates correct Zod schema for postRenderFlowchart', () => {
expect(zodSchemas).toBeDefined();
expect(zodSchemas?.postRenderFlowchart).toBeDefined();
const PostRenderFlowchartSchema = zodSchemas?.postRenderFlowchart;
expect(PostRenderFlowchartSchema).toBeInstanceOf(z.ZodObject);
if (!(PostRenderFlowchartSchema instanceof z.ZodObject)) {
return;
}
const shape = PostRenderFlowchartSchema.shape;
expect(shape.mermaid).toBeInstanceOf(z.ZodString);
expect(shape.title).toBeInstanceOf(z.ZodOptional);
expect((shape.title as z.ZodOptional<z.ZodString>)._def.innerType).toBeInstanceOf(
z.ZodString,
);
});
it('validates correct data for postRenderFlowchart', () => {
const PostRenderFlowchartSchema = zodSchemas?.postRenderFlowchart;
const validData = {
mermaid: 'graph TD; A-->B; B-->C; C-->D;',
title: 'Test Flowchart',
};
expect(() => PostRenderFlowchartSchema?.parse(validData)).not.toThrow();
});
it('throws error for invalid data for postRenderFlowchart', () => {
const PostRenderFlowchartSchema = zodSchemas?.postRenderFlowchart;
const invalidData = {
mermaid: 123,
title: 42,
};
expect(() => PostRenderFlowchartSchema?.parse(invalidData)).toThrow();
});
});
describe('scholarAIOpenapiSpec', () => {
const result = validateAndParseOpenAPISpec(scholarAIOpenapiSpec);
const spec = result.spec as OpenAPIV3.Document;
const { zodSchemas } = openapiToFunction(spec, true);
it('generates correct Zod schema for searchAbstracts', () => {
expect(zodSchemas).toBeDefined();
expect(zodSchemas?.searchAbstracts).toBeDefined();
const SearchAbstractsSchema = zodSchemas?.searchAbstracts;
expect(SearchAbstractsSchema).toBeInstanceOf(z.ZodObject);
if (!(SearchAbstractsSchema instanceof z.ZodObject)) {
return;
}
const shape = SearchAbstractsSchema.shape;
expect(shape.keywords).toBeInstanceOf(z.ZodString);
expect(shape.sort).toBeInstanceOf(z.ZodOptional);
expect(
(shape.sort as z.ZodOptional<z.ZodEnum<[string, ...string[]]>>)._def.innerType,
).toBeInstanceOf(z.ZodEnum);
expect(shape.query).toBeInstanceOf(z.ZodString);
expect(shape.peer_reviewed_only).toBeInstanceOf(z.ZodOptional);
expect(shape.start_year).toBeInstanceOf(z.ZodOptional);
expect(shape.end_year).toBeInstanceOf(z.ZodOptional);
expect(shape.offset).toBeInstanceOf(z.ZodOptional);
});
it('validates correct data for searchAbstracts', () => {
const SearchAbstractsSchema = zodSchemas?.searchAbstracts;
const validData = {
keywords: 'machine learning',
sort: 'cited_by_count',
query: 'AI applications',
peer_reviewed_only: 'true',
start_year: '2020',
end_year: '2023',
offset: '0',
};
expect(() => SearchAbstractsSchema?.parse(validData)).not.toThrow();
});
it('throws error for invalid data for searchAbstracts', () => {
const SearchAbstractsSchema = zodSchemas?.searchAbstracts;
const invalidData = {
keywords: 123,
sort: 'invalid_sort',
query: 42,
peer_reviewed_only: 'maybe',
start_year: 2020,
end_year: 2023,
offset: 0,
};
expect(() => SearchAbstractsSchema?.parse(invalidData)).toThrow();
});
it('generates correct Zod schema for getFullText', () => {
expect(zodSchemas?.getFullText).toBeDefined();
const GetFullTextSchema = zodSchemas?.getFullText;
expect(GetFullTextSchema).toBeInstanceOf(z.ZodObject);
if (!(GetFullTextSchema instanceof z.ZodObject)) {
return;
}
const shape = GetFullTextSchema.shape;
expect(shape.pdf_url).toBeInstanceOf(z.ZodString);
expect(shape.chunk).toBeInstanceOf(z.ZodOptional);
expect((shape.chunk as z.ZodOptional<z.ZodNumber>)._def.innerType).toBeInstanceOf(
z.ZodNumber,
);
});
it('generates correct Zod schema for saveCitation', () => {
expect(zodSchemas?.saveCitation).toBeDefined();
const SaveCitationSchema = zodSchemas?.saveCitation;
expect(SaveCitationSchema).toBeInstanceOf(z.ZodObject);
if (!(SaveCitationSchema instanceof z.ZodObject)) {
return;
}
const shape = SaveCitationSchema.shape;
expect(shape.doi).toBeInstanceOf(z.ZodString);
expect(shape.zotero_user_id).toBeInstanceOf(z.ZodString);
expect(shape.zotero_api_key).toBeInstanceOf(z.ZodString);
});
});
});
describe('openapiToFunction zodSchemas for SWAPI', () => {
const result = validateAndParseOpenAPISpec(swapidev);
const spec = result.spec as OpenAPIV3.Document;
const { zodSchemas } = openapiToFunction(spec, true);
describe('getPeople schema', () => {
it('does not generate Zod schema for getPeople (no parameters)', () => {
expect(zodSchemas).toBeDefined();
expect(zodSchemas?.getPeople).toBeUndefined();
});
it('validates correct data for getPeople', () => {
const GetPeopleSchema = zodSchemas?.getPeople;
expect(GetPeopleSchema).toBeUndefined();
});
it('does not throw for invalid data for getPeople', () => {
const GetPeopleSchema = zodSchemas?.getPeople;
expect(GetPeopleSchema).toBeUndefined();
});
});
describe('getPersonById schema', () => {
it('generates correct Zod schema for getPersonById', () => {
expect(zodSchemas).toBeDefined();
expect(zodSchemas?.getPersonById).toBeDefined();
const GetPersonByIdSchema = zodSchemas?.getPersonById;
expect(GetPersonByIdSchema).toBeInstanceOf(z.ZodObject);
if (!(GetPersonByIdSchema instanceof z.ZodObject)) {
return;
}
const shape = GetPersonByIdSchema.shape;
expect(shape.id).toBeInstanceOf(z.ZodString);
});
it('validates correct data for getPersonById', () => {
const GetPersonByIdSchema = zodSchemas?.getPersonById;
const validData = { id: '1' };
expect(() => GetPersonByIdSchema?.parse(validData)).not.toThrow();
});
it('throws error for invalid data for getPersonById', () => {
const GetPersonByIdSchema = zodSchemas?.getPersonById;
const invalidData = { id: 1 }; // should be string
expect(() => GetPersonByIdSchema?.parse(invalidData)).toThrow();
});
});
});
});