@agentshouse/mdruntime 0.1.0-alpha.21 → 0.1.0-alpha.22

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.
@@ -79,6 +79,7 @@ export function createMdRuntime(options) {
79
79
  let refusal = null;
80
80
  let storageLoss = null;
81
81
  let status = STARTING;
82
+ let shown = STARTING;
82
83
  let discoveryPaused = false;
83
84
  let timer;
84
85
  let closed = false;
@@ -98,12 +99,14 @@ export function createMdRuntime(options) {
98
99
  const draftListeners = new Set();
99
100
  const presentationListeners = new Set();
100
101
  let withdrawConversations;
102
+ let currencyQueued = false;
101
103
  const conversations = createConversationStore({
102
104
  cards: declaredCards,
103
- documents: ()=>visible({}),
105
+ documents: ()=>visible({}).documents,
104
106
  call: (name, input)=>invokeAction(name, input),
105
107
  durably,
106
- path: ()=>options.adapter.conversations,
108
+ stream: ()=>options.adapter.conversations,
109
+ publish: publishChangeSet,
107
110
  closed: ()=>closed
108
111
  });
109
112
  const journal = createLog();
@@ -263,9 +266,19 @@ export function createMdRuntime(options) {
263
266
  for (const listener of storageLossListeners)listener(storageLoss);
264
267
  }
265
268
  function moveTo(next) {
266
- if (closed || same(status, next)) return;
269
+ if (closed) return;
267
270
  status = next;
268
- for (const listener of statusListeners)listener(status);
271
+ show();
272
+ }
273
+ function show() {
274
+ if (closed) return;
275
+ const next = status.state === 'synced' && !conversations.current() ? {
276
+ ...status,
277
+ state: 'syncing'
278
+ } : status;
279
+ if (same(shown, next)) return;
280
+ shown = next;
281
+ for (const listener of statusListeners)listener(shown);
269
282
  }
270
283
  function visible(request) {
271
284
  if (options.queryBinding === null) {
@@ -307,6 +320,11 @@ export function createMdRuntime(options) {
307
320
  for (const locator of set.queues)emitQueue(locator);
308
321
  if (set.drafts.length > 0) emitDrafts();
309
322
  for (const app of set.presentations)emitPresentation(app);
323
+ conversations.changed(set);
324
+ const cards = declaredCards();
325
+ if (cards !== undefined && set.documents.some((change)=>change.locator === cards.locator)) {
326
+ scheduleCurrency();
327
+ }
310
328
  }
311
329
  function stopTimer() {
312
330
  if (timer === undefined) return;
@@ -494,14 +512,14 @@ export function createMdRuntime(options) {
494
512
  return model;
495
513
  }
496
514
  async function hydrate(opened) {
497
- const [records, queues, recordedOutcomes, kept, presented, cached, unsent, lastCompleted, declared, named, listedSlots] = await Promise.all([
515
+ const [records, queues, recordedOutcomes, kept, presented, held, chatDrafts, lastCompleted, declared, named, listedSlots] = await Promise.all([
498
516
  opened.all(),
499
517
  opened.queues(),
500
518
  opened.outcomes(),
501
519
  opened.drafts(),
502
520
  opened.presentation(),
503
521
  opened.conversations(),
504
- opened.instructions(),
522
+ opened.chatDrafts(),
505
523
  opened.lastCompleted(),
506
524
  opened.providerLimits(),
507
525
  opened.capabilities(),
@@ -514,7 +532,7 @@ export function createMdRuntime(options) {
514
532
  for (const draft of kept)rememberDraft(draft.locator, draft.path, draft.content);
515
533
  presentations.clear();
516
534
  for (const record of presented)presentations.set(record.app, record.value);
517
- conversations.hydrate(cached, unsent);
535
+ conversations.hydrate(held, chatDrafts);
518
536
  const queuedBatches = new Map(queues.map((queue)=>[
519
537
  queue.locator,
520
538
  queue.batches
@@ -1476,6 +1494,10 @@ export function createMdRuntime(options) {
1476
1494
  handSelected(result.rooms.filter((room)=>room.replica.admission === 'admitted').map((room)=>room.locator).sort());
1477
1495
  progress.complete = true;
1478
1496
  await synchronize(result.rooms, local, progress);
1497
+ if (closed || discoveryPaused) return;
1498
+ await conversations.reconcile();
1499
+ if (status.providerUnreachable) conversations.reconnected();
1500
+ await conversations.catchUp(progress);
1479
1501
  }
1480
1502
  async function ensureAuthorization() {
1481
1503
  if (authorization !== undefined) return;
@@ -1539,12 +1561,44 @@ export function createMdRuntime(options) {
1539
1561
  });
1540
1562
  }
1541
1563
  function observeConversations() {
1542
- const path = options.adapter.conversations;
1543
- if (path === undefined || withdrawConversations !== undefined) return;
1544
- withdrawConversations = path.observe((event)=>{
1564
+ const stream = options.adapter.conversations;
1565
+ if (stream === undefined || withdrawConversations !== undefined) return;
1566
+ withdrawConversations = stream.observe((event)=>{
1545
1567
  conversations.receive(event);
1568
+ if (event.kind !== 'gap' || conversations.current()) return;
1569
+ show();
1570
+ scheduleCurrency();
1546
1571
  });
1547
1572
  }
1573
+ function scheduleCurrency() {
1574
+ if (currencyQueued || closed) return;
1575
+ currencyQueued = true;
1576
+ chain = chain.then(runCurrency, runCurrency);
1577
+ }
1578
+ async function runCurrency() {
1579
+ currencyQueued = false;
1580
+ if (closed || discoveryPaused) return;
1581
+ await conversations.reconcile();
1582
+ if (conversations.current()) {
1583
+ show();
1584
+ return;
1585
+ }
1586
+ const resting = status;
1587
+ const progress = {
1588
+ complete: false,
1589
+ reachable: !resting.providerUnreachable
1590
+ };
1591
+ moveTo(transferring(resting));
1592
+ try {
1593
+ await conversations.catchUp(progress);
1594
+ } catch {
1595
+ progress.reachable = false;
1596
+ } finally{
1597
+ if (!closed) {
1598
+ moveTo(settled(resting, progress, resting.lastCompleted, pending(), attention()));
1599
+ }
1600
+ }
1601
+ }
1548
1602
  function handSelected(locators) {
1549
1603
  const path = options.adapter.roomVersions;
1550
1604
  if (path === undefined || withdrawNotifications === undefined) return;
@@ -1660,7 +1714,7 @@ export function createMdRuntime(options) {
1660
1714
  };
1661
1715
  },
1662
1716
  syncStatus () {
1663
- return status;
1717
+ return shown;
1664
1718
  },
1665
1719
  draft: {
1666
1720
  list () {
@@ -1695,6 +1749,7 @@ export function createMdRuntime(options) {
1695
1749
  }
1696
1750
  },
1697
1751
  conversation: conversations.surface,
1752
+ chatDraft: conversations.chatDraft,
1698
1753
  presentation: {
1699
1754
  read (app) {
1700
1755
  return exclusive(async ()=>{
@@ -1750,7 +1805,7 @@ export function createMdRuntime(options) {
1750
1805
  persistence () {
1751
1806
  return options.shell.persistentStorage;
1752
1807
  },
1753
- observe (topic, second, third) {
1808
+ observe (topic, second, third, fourth) {
1754
1809
  if (topic === 'drafts') {
1755
1810
  const notify = second;
1756
1811
  const listener = {
@@ -1771,7 +1826,7 @@ export function createMdRuntime(options) {
1771
1826
  };
1772
1827
  }
1773
1828
  if (topic === 'conversation') {
1774
- return conversations.observe(second, third);
1829
+ return conversations.observe(second, third, fourth);
1775
1830
  }
1776
1831
  if (topic === 'presentation') {
1777
1832
  const listener = {
@@ -1818,7 +1873,7 @@ export function createMdRuntime(options) {
1818
1873
  if (topic === 'sync-status') {
1819
1874
  const notify = second;
1820
1875
  statusListeners.add(notify);
1821
- notify(status);
1876
+ notify(shown);
1822
1877
  return ()=>{
1823
1878
  statusListeners.delete(notify);
1824
1879
  };