Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(LLM): 18562 rewrite LLM atom to reatom v3. Add dependency on referenceAreaAtom #787

Merged
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"@nebula.gl/edit-modes": "1.1.0-alpha.5",
"@nebula.gl/layers": "1.1.0-alpha.5",
"@paypal/react-paypal-js": "^8.5.0",
"@reatom/async": "^3.15.2",
"@reatom/core": "^3.8.3",
"@reatom/core-v2": "^3.1.4",
"@reatom/logger": "^3.8.4",
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 28 additions & 12 deletions src/features/llm_analytics/atoms/llmAnalyticsResource.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import { createAsyncAtom } from '~utils/atoms/createAsyncAtom';
import { reatomResource, withDataAtom, withErrorAtom } from '@reatom/async';
import { atom } from '@reatom/core';
import { focusedGeometryAtom } from '~core/focused_geometry/model';
import { i18n } from '~core/localization';
import { dispatchMetricsEventOnce } from '~core/metrics/dispatch';
import { AppFeature } from '~core/auth/types';
import { isGeoJSONEmpty } from '~utils/geoJSON/helpers';
import { getLlmAnalysis } from '~core/api/insights';
import { referenceAreaAtom } from '~core/shared_state/referenceArea';
import type { LLMAnalyticsData } from '~core/types';

// TODO: rewrite to reatom v3
export const llmAnalyticsResourceAtom = createAsyncAtom(
focusedGeometryAtom,
async (fGeo, abortController) => {
if (!fGeo) return null;
const geometry = fGeo?.geometry as GeoJSON.FeatureCollection;
if (isGeoJSONEmpty(geometry)) {
export const llmAnalyticsAtom = atom((ctx) => {
const fetchLlmAnalyticsResult = ctx.spy(fetchLlmAnalyticsResource.dataAtom);
const isPending = ctx.spy(fetchLlmAnalyticsResource.pendingAtom) > 0;
const error = ctx.spy(fetchLlmAnalyticsResource.errorAtom);
return {
data: fetchLlmAnalyticsResult,
loading: isPending,
error: error?.message ?? null,
};
}, 'llmAnalyticsAtom');

export const fetchLlmAnalyticsResource = reatomResource<LLMAnalyticsData | null>(
async (ctx) => {
const focusedGeometry = ctx.spy(focusedGeometryAtom.v3atom);
// we spy on referenceAreaAtom because its change needs to trigger new LLM request
const referenceArea = ctx.spy(referenceAreaAtom);
const focusedGeoJSON = focusedGeometry?.geometry;
if (isGeoJSONEmpty(focusedGeoJSON)) {
return null;
}
let responseData: LLMAnalyticsData | null | undefined;
let responseData: LLMAnalyticsData | null;
try {
responseData = await getLlmAnalysis(geometry, abortController);
responseData = await getLlmAnalysis(
focusedGeoJSON as GeoJSON.FeatureCollection,
ctx.controller,
);
} catch (e: unknown) {
dispatchMetricsEventOnce(AppFeature.ANALYTICS_PANEL, false);
throw new Error(i18n.t('analytics_panel.error_loading'));
Expand All @@ -30,5 +46,5 @@ export const llmAnalyticsResourceAtom = createAsyncAtom(
}
return responseData;
},
'analyticsResource',
);
'fetchLlmAnalyticsResource',
).pipe(withDataAtom(null), withErrorAtom());
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useAtom } from '@reatom/react-v2';
import { useAtom } from '@reatom/npm-react';
import { LoadingSpinner } from '~components/LoadingSpinner/LoadingSpinner';
import { ErrorMessage } from '~components/ErrorMessage/ErrorMessage';
import { createStateMap } from '~utils/atoms';
import { focusedGeometryAtom } from '~core/focused_geometry/model';
import { PagesDocument } from '~core/pages';
import { i18n } from '~core/localization';
import { llmAnalyticsAtom } from '~features/llm_analytics/atoms/llmAnalyticsResource';
import { LLMAnalyticsEmptyState } from '../LLMAnalyticsEmptyState/LLMAnalyticsEmptyState';
import { llmAnalyticsResourceAtom } from '../../atoms/llmAnalyticsResource';
import { MarkdownWrapper } from '../MarkdownWrapper/MarkdownWrapper';

export const LLMAnalyticsContainer = () => {
const [{ error, loading, data }] = useAtom(llmAnalyticsResourceAtom);
const [focusedGeometry] = useAtom(focusedGeometryAtom);
const [{ error, loading, data }] = useAtom(llmAnalyticsAtom);
const [focusedGeometry] = useAtom(focusedGeometryAtom.v3atom);

const statesToComponents = createStateMap({
error,
Expand Down
Loading