Skip to content

Commit

Permalink
Refetch input schema on config change aka. support configurable promp…
Browse files Browse the repository at this point in the history
…ts (#66)
  • Loading branch information
nfcampos authored Oct 19, 2023
1 parent b93ccf9 commit a449791
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 25 deletions.
1 change: 1 addition & 0 deletions langserve/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwind-merge": "^1.14.0",
"use-debounce": "^9.0.4",
"vaul": "^0.7.3"
},
"devDependencies": {
Expand Down
17 changes: 12 additions & 5 deletions langserve/playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ function App() {

// store form state
const [configData, setConfigData] = useState<
Pick<JsonFormsCore, "data" | "errors">
>({ data: {}, errors: [] });
Pick<JsonFormsCore, "data" | "errors"> & { defaults: boolean }
>({ data: {}, errors: [], defaults: true });

const [inputData, setInputData] = useState<
Pick<JsonFormsCore, "data" | "errors">
>({ data: null, errors: [] });
// fetch input and config schemas from the server
const schemas = useSchemas();
const schemas = useSchemas(configData);
// apply defaults defined in each schema
useEffect(() => {
if (schemas.config) {
Expand All @@ -182,6 +182,7 @@ function App() {
initConfigData.current ??
defaults(schemas.config),
errors: [],
defaults: true,
});
setInputData({ data: null, errors: [] });
}
Expand All @@ -204,7 +205,11 @@ function App() {
const value: { config: JsonFormsCore["data"] } = message.value;
if (Object.keys(value.config).length > 0) {
initConfigData.current = value.config;
setConfigData({ data: value.config, errors: [] });
setConfigData({
data: value.config,
errors: [],
defaults: false,
});
break;
}
}
Expand Down Expand Up @@ -232,7 +237,9 @@ function App() {
renderers={renderers}
cells={cells}
onChange={({ data, errors }) =>
data ? setConfigData({ data, errors }) : undefined
data
? setConfigData({ data, errors, defaults: false })
: undefined
}
/>
{!!configData.errors?.length && configData.data && (
Expand Down
25 changes: 24 additions & 1 deletion langserve/playground/src/useSchemas.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useEffect, useState } from "react";
import { resolveApiUrl } from "./utils/url";
import { simplifySchema } from "./utils/simplifySchema";
import { JsonFormsCore } from "@jsonforms/core";
import { compressToEncodedURIComponent } from "lz-string";
import { useDebounce } from "use-debounce";

declare global {
interface Window {
Expand All @@ -11,7 +14,9 @@ declare global {
}
}

export function useSchemas() {
export function useSchemas(
configData: Pick<JsonFormsCore, "data" | "errors"> & { defaults: boolean }
) {
const [schemas, setSchemas] = useState({
config: null,
input: null,
Expand Down Expand Up @@ -44,5 +49,23 @@ export function useSchemas() {
save();
}, []);

const [debouncedConfigData] = useDebounce(configData, 500);

useEffect(() => {
if (!debouncedConfigData.defaults) {
fetch(
resolveApiUrl(
`c/${compressToEncodedURIComponent(

This comment has been minimized.

Copy link
@xleven

xleven Oct 20, 2023

Hi @nfcampos do you think there is a missing / before c?

JSON.stringify(debouncedConfigData.data)
)}/input_schema`
)
)
.then((r) => r.json())
.then(simplifySchema)
.then((input) => setSchemas((current) => ({ ...current, input })))
.catch(() => {}); // ignore errors, eg. due to incomplete config
}
}, [debouncedConfigData]);

return schemas;
}
5 changes: 5 additions & 0 deletions langserve/playground/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,11 @@ use-callback-ref@^1.3.0:
dependencies:
tslib "^2.0.0"

use-debounce@^9.0.4:
version "9.0.4"
resolved "https://registry.yarnpkg.com/use-debounce/-/use-debounce-9.0.4.tgz#51d25d856fbdfeb537553972ce3943b897f1ac85"
integrity sha512-6X8H/mikbrt0XE8e+JXRtZ8yYVvKkdYRfmIhWZYsP8rcNs9hk3APV8Ua2mFkKRLcJKVdnX2/Vwrmg2GWKUQEaQ==

use-sidecar@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/use-sidecar/-/use-sidecar-1.1.2.tgz#2f43126ba2d7d7e117aa5855e5d8f0276dfe73c2"
Expand Down
38 changes: 19 additions & 19 deletions poetry.lock

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

0 comments on commit a449791

Please sign in to comment.