@arbidocs/react 0.5.49 → 0.5.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +79 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +79 -26
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1450,41 +1450,94 @@ function configDelta(active, defaults) {
|
|
|
1450
1450
|
}
|
|
1451
1451
|
return delta;
|
|
1452
1452
|
}
|
|
1453
|
-
function
|
|
1453
|
+
function deltaOnto(changes, active, defaults) {
|
|
1454
|
+
const delta = configDelta(active, defaults);
|
|
1455
|
+
for (const [instrument, fields] of Object.entries(changes)) {
|
|
1456
|
+
delta[instrument] = { ...delta[instrument] ?? {}, ...fields };
|
|
1457
|
+
const defaultInstrument = defaults[instrument];
|
|
1458
|
+
if (defaultInstrument) {
|
|
1459
|
+
const instrumentDelta = delta[instrument];
|
|
1460
|
+
for (const [field, value] of Object.entries(fields)) {
|
|
1461
|
+
if (JSON.stringify(value) === JSON.stringify(defaultInstrument[field])) {
|
|
1462
|
+
delete instrumentDelta[field];
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
if (Object.keys(instrumentDelta).length === 0) delete delta[instrument];
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
return delta;
|
|
1469
|
+
}
|
|
1470
|
+
function useSaveConfigField(baseConfigId) {
|
|
1471
|
+
const arbi = chunkYIUUTRMQ_cjs.useArbi();
|
|
1472
|
+
const queryClient = reactQuery.useQueryClient();
|
|
1454
1473
|
const saveConfig = useSaveConfig();
|
|
1455
1474
|
const { data: defaultConfig } = chunkME4TS33T_cjs.useConfig("default");
|
|
1456
1475
|
const chatModel = chunkME4TS33T_cjs.useArbiSessionState((state) => state.chatModel);
|
|
1457
1476
|
const { data: userSettings } = useUserSettings();
|
|
1458
|
-
const activeConfigId = chatModel || userSettings?.last_config || "default";
|
|
1477
|
+
const activeConfigId = baseConfigId || chatModel || userSettings?.last_config || "default";
|
|
1459
1478
|
const { data: activeConfigData } = chunkME4TS33T_cjs.useConfig(activeConfigId);
|
|
1460
|
-
const
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1479
|
+
const session = chunkME4TS33T_cjs.useArbiSessionStore();
|
|
1480
|
+
const chain = React.useRef(null);
|
|
1481
|
+
const buildDelta = (changes) => deltaOnto(
|
|
1482
|
+
changes,
|
|
1483
|
+
activeConfigData ?? {},
|
|
1484
|
+
defaultConfig ?? {}
|
|
1485
|
+
);
|
|
1486
|
+
const readConfig = (configId) => queryClient.fetchQuery({
|
|
1487
|
+
queryKey: chunkRRWAX52L_cjs.arbiQueryKeys.config(configId),
|
|
1488
|
+
queryFn: () => arbi.agentConfig.get(configId),
|
|
1489
|
+
staleTime: 10 * 60 * 1e3
|
|
1490
|
+
});
|
|
1491
|
+
const saveOnBase = (base, changes, title) => {
|
|
1492
|
+
const current = chain.current?.ids.has(base) ? chain.current : null;
|
|
1493
|
+
const ids = current?.ids ?? /* @__PURE__ */ new Set([base]);
|
|
1494
|
+
const from = current?.latest ?? Promise.resolve(base);
|
|
1495
|
+
const saved = from.then(async (configId) => {
|
|
1496
|
+
const [defaults, active] = await Promise.all([
|
|
1497
|
+
readConfig("default"),
|
|
1498
|
+
readConfig(configId)
|
|
1499
|
+
]).catch((error) => {
|
|
1500
|
+
chunkVAMPYLQ4_cjs.notify.error(formatConfigValidationError(error), {
|
|
1501
|
+
description: "Configuration not saved \u2014 the settings it builds on could not be read."
|
|
1502
|
+
});
|
|
1503
|
+
throw error;
|
|
1504
|
+
});
|
|
1505
|
+
const response = await saveConfig.mutateAsync({
|
|
1506
|
+
...deltaOnto(changes, active ?? {}, defaults ?? {}),
|
|
1507
|
+
title
|
|
1508
|
+
});
|
|
1509
|
+
if (response?.external_id) {
|
|
1510
|
+
ids.add(response.external_id);
|
|
1511
|
+
const { chatModel: chatModel2 } = session.getState();
|
|
1512
|
+
const followsSettings = !chatModel2 && base === userSettings?.last_config;
|
|
1513
|
+
if (chain.current?.ids === ids && !followsSettings) {
|
|
1514
|
+
session.setChatModel(response.external_id);
|
|
1473
1515
|
}
|
|
1474
|
-
if (Object.keys(instrumentDelta).length === 0) delete delta[instrument];
|
|
1475
1516
|
}
|
|
1476
|
-
|
|
1477
|
-
|
|
1517
|
+
return { response, configId: response?.external_id ?? configId };
|
|
1518
|
+
});
|
|
1519
|
+
chain.current = {
|
|
1520
|
+
ids,
|
|
1521
|
+
latest: Promise.all([from, saved]).then(
|
|
1522
|
+
([, done]) => done.configId,
|
|
1523
|
+
() => from
|
|
1524
|
+
)
|
|
1525
|
+
};
|
|
1526
|
+
return saved.then(({ response }) => response);
|
|
1478
1527
|
};
|
|
1479
1528
|
return {
|
|
1480
|
-
save: (instrument, field, value, title) =>
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
...buildDelta(changes),
|
|
1486
|
-
|
|
1487
|
-
|
|
1529
|
+
save: (instrument, field, value, title) => {
|
|
1530
|
+
const changes = { [instrument]: { [field]: value } };
|
|
1531
|
+
const name = title ?? `prefs-${Date.now()}`;
|
|
1532
|
+
if (baseConfigId) return void saveOnBase(baseConfigId, changes, name).catch(() => {
|
|
1533
|
+
});
|
|
1534
|
+
saveConfig.mutate({ ...buildDelta(changes), title: name });
|
|
1535
|
+
},
|
|
1536
|
+
saveDelta: (changes, title) => {
|
|
1537
|
+
const name = title ?? `prefs-${Date.now()}`;
|
|
1538
|
+
if (baseConfigId) return saveOnBase(baseConfigId, changes, name);
|
|
1539
|
+
return saveConfig.mutateAsync({ ...buildDelta(changes), title: name });
|
|
1540
|
+
},
|
|
1488
1541
|
isPending: saveConfig.isPending
|
|
1489
1542
|
};
|
|
1490
1543
|
}
|