@artooi/ag-ui-web-component 0.41.3 → 0.42.0
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/CHANGELOG.md +176 -1
- package/README.md +76 -19
- package/dist/ag-ui-web-component.bundle.js +21 -21
- package/dist/ag-ui-web-component.bundle.js.map +3 -3
- package/dist/constants.d.ts +3 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/core/ag_ui_chat.d.ts +17 -5
- package/dist/core/ag_ui_chat.d.ts.map +1 -1
- package/dist/core/agui_client.d.ts +20 -7
- package/dist/core/agui_client.d.ts.map +1 -1
- package/dist/core/storage_scope.d.ts +14 -4
- package/dist/core/storage_scope.d.ts.map +1 -1
- package/dist/index.js +573 -99
- package/dist/index.js.map +2 -2
- package/dist/tools/tool_dispatch.d.ts +13 -0
- package/dist/tools/tool_dispatch.d.ts.map +1 -1
- package/dist/ui/composer/voice_input.d.ts +10 -4
- package/dist/ui/composer/voice_input.d.ts.map +1 -1
- package/dist/ui/history/conversation_history.d.ts +68 -1
- package/dist/ui/history/conversation_history.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/constants.ts +3 -1
- package/src/core/ag_ui_chat.ts +398 -60
- package/src/core/agui_client.ts +55 -12
- package/src/core/storage_scope.ts +21 -8
- package/src/tools/tool_dispatch.ts +114 -3
- package/src/ui/composer/voice_input.ts +35 -4
- package/src/ui/history/conversation_history.ts +322 -55
- package/src/version.ts +1 -1
package/src/core/ag_ui_chat.ts
CHANGED
|
@@ -556,6 +556,13 @@ export class AgUiChat extends HTMLElement {
|
|
|
556
556
|
readonly #queuedRow: HTMLDivElement = document.createElement("div");
|
|
557
557
|
/** How far back the composer has been walked; null while the user is typing. */
|
|
558
558
|
#recallIndex: number | null = null;
|
|
559
|
+
/**
|
|
560
|
+
* What the walk last put in the composer, read back from it. A walk is live
|
|
561
|
+
* only while the composer still holds exactly this, so a write from anywhere
|
|
562
|
+
* else ends it. Stale while no walk is in progress, which is harmless: the
|
|
563
|
+
* comparison can only ever end a walk, never start one.
|
|
564
|
+
*/
|
|
565
|
+
#recalled = "";
|
|
559
566
|
|
|
560
567
|
/**
|
|
561
568
|
* Re-clamp the dragged launcher when the window changes size. Bound once as
|
|
@@ -605,6 +612,39 @@ export class AgUiChat extends HTMLElement {
|
|
|
605
612
|
* history replay would draw the conversation a second time beneath it.
|
|
606
613
|
*/
|
|
607
614
|
#connectedBefore = false;
|
|
615
|
+
/**
|
|
616
|
+
* The `user-key` whose state the element holds -- the transcript, the recall
|
|
617
|
+
* history, the Always allow waivers and the shared state -- or `null` before
|
|
618
|
+
* the first connect, when it holds nobody's.
|
|
619
|
+
*
|
|
620
|
+
* Not the same thing as the attribute. The attribute names the principal the
|
|
621
|
+
* host says is here now, and it can change while the element is out of the
|
|
622
|
+
* document, where nothing acts on it; this is who the state still belongs
|
|
623
|
+
* to. Connecting compares the two, which is how a key changed while detached
|
|
624
|
+
* still gets the handover a live change gets.
|
|
625
|
+
*/
|
|
626
|
+
#principalKey: string | null = null;
|
|
627
|
+
/**
|
|
628
|
+
* The current principal's tenure: replaced on every handover from one
|
|
629
|
+
* principal to another, and captured by each client as it is built, so that
|
|
630
|
+
* a client can tell whether the principal it was built for is still the one
|
|
631
|
+
* this element serves.
|
|
632
|
+
*
|
|
633
|
+
* A client outlives the handover that stops it. A stopped run saves what it
|
|
634
|
+
* had once its request closes, a tool handler cannot be aborted and its
|
|
635
|
+
* result is kept, and an event already read off the wire is still applied --
|
|
636
|
+
* all of it after the store was purged and scoped to whoever arrived, so
|
|
637
|
+
* each one wrote the previous principal's words into the next one's storage
|
|
638
|
+
* or state. Writing into the previous principal's store instead would only
|
|
639
|
+
* re-create what the purge just removed, so such a client writes nothing.
|
|
640
|
+
*
|
|
641
|
+
* An identity rather than the key itself, for two reasons. The key's first
|
|
642
|
+
* arrival is an adoption, not a handover, and a run in flight across it is
|
|
643
|
+
* the same person's and keeps saving. And a key that leaves and comes back
|
|
644
|
+
* is a second tenure: the first one's stopped run must not re-create a
|
|
645
|
+
* conversation the handover purged.
|
|
646
|
+
*/
|
|
647
|
+
#tenure: object = {};
|
|
608
648
|
/**
|
|
609
649
|
* The remote store `data-threads-url` wrapped around the conversation store,
|
|
610
650
|
* and the store inside it, so connecting again can wrap that store rather
|
|
@@ -624,6 +664,33 @@ export class AgUiChat extends HTMLElement {
|
|
|
624
664
|
// the Send⇄Stop button: `agent.isRunning` is false between frontend-tool
|
|
625
665
|
// rounds, but the user must still be able to stop there.
|
|
626
666
|
#running = false;
|
|
667
|
+
/**
|
|
668
|
+
* The send {@link sendMessage} has taken, or the retry {@link retryLastTurn}
|
|
669
|
+
* has, whose run has not started yet; null otherwise.
|
|
670
|
+
*
|
|
671
|
+
* `running` turns on when the client starts the run, a microtask after it is
|
|
672
|
+
* asked for one, and a send does two things before it asks: it draws the
|
|
673
|
+
* turn, and it dispatches the submit event, whose listeners run in the middle
|
|
674
|
+
* of the send. A send from one of them, or a second call in the same task,
|
|
675
|
+
* passed every guard and started a second run on the same client -- two
|
|
676
|
+
* requests, and the listener's turn drawn between the user's turn and its
|
|
677
|
+
* answer. It is the gap a picked checkpoint closes by counting from the pick,
|
|
678
|
+
* closed the same way for a send. A retry asks for its run the same way, so
|
|
679
|
+
* it takes the same hold, and so does a restore that will resume a run, from
|
|
680
|
+
* before it asks the store for the conversation, through the history host's
|
|
681
|
+
* `holdRun`.
|
|
682
|
+
*
|
|
683
|
+
* Let go of once the run starts, because `running` holds from then until the
|
|
684
|
+
* run settles, and a send held past that would refuse a host's follow-up from
|
|
685
|
+
* the run-finished event, which fires as the run settles. Let go of by the
|
|
686
|
+
* send's, the retry's or the restore's end when its run never starts.
|
|
687
|
+
*
|
|
688
|
+
* An object rather than a flag, so a send or a retry ending releases only
|
|
689
|
+
* what it took. By the time either ends, the turn queued behind it has
|
|
690
|
+
* usually taken the field for itself, and a Stop lets go of it for the next
|
|
691
|
+
* send to take; the earlier one ending must release neither.
|
|
692
|
+
*/
|
|
693
|
+
#sending: object | null = null;
|
|
627
694
|
/** The decision a run is suspended on, which a Stop abandons. */
|
|
628
695
|
readonly #decision = new PendingDecision();
|
|
629
696
|
/**
|
|
@@ -715,6 +782,9 @@ export class AgUiChat extends HTMLElement {
|
|
|
715
782
|
getContext: () => this.getContext(),
|
|
716
783
|
conversationStore: () => this.conversationStore,
|
|
717
784
|
threadId: () => this.#history.threadId,
|
|
785
|
+
// Read live and compared, the way each client compares the one it was
|
|
786
|
+
// built with: see #tenure.
|
|
787
|
+
tenure: () => this.#tenure,
|
|
718
788
|
});
|
|
719
789
|
this.#runHandlers = new RunHandlers({
|
|
720
790
|
element: this,
|
|
@@ -863,6 +933,7 @@ export class AgUiChat extends HTMLElement {
|
|
|
863
933
|
// A continuation sends only what the composer holds. The tray is not
|
|
864
934
|
// read, so nothing rides along to be drawn or announced.
|
|
865
935
|
announceTurn: (content) => this.#announceTurn(content, []),
|
|
936
|
+
recordTurn: (content) => this.#recordTurn(content),
|
|
866
937
|
autoGrow: () => autoGrow(this.#input),
|
|
867
938
|
continuationEnded: () => this.#flushQueued(),
|
|
868
939
|
client: () => this.#client,
|
|
@@ -871,7 +942,37 @@ export class AgUiChat extends HTMLElement {
|
|
|
871
942
|
releaseClient: () => {
|
|
872
943
|
this.#client = null;
|
|
873
944
|
},
|
|
874
|
-
|
|
945
|
+
// A held send or retry is as much in flight to a pick as a run is: it has
|
|
946
|
+
// asked this element's client for a run that has not started, and a
|
|
947
|
+
// continuation started beside it was a second run in one conversation.
|
|
948
|
+
// So is a restore that will resume a run, which holds through the same
|
|
949
|
+
// field from before the store answers.
|
|
950
|
+
running: () => this.#running || this.#sending !== null,
|
|
951
|
+
// The hold a send takes, taken for a restore that will resume a run, so
|
|
952
|
+
// what refuses a send held short of its run refuses one made while the
|
|
953
|
+
// store is still answering: `sendMessage` returns, Send parks the turn,
|
|
954
|
+
// a Retry and a pick refuse. The same field rather than a flag beside it,
|
|
955
|
+
// because what lets go of a send's hold is right for this one too. The
|
|
956
|
+
// resumed run starting lets go, in `#setRunning`, so there is no gap
|
|
957
|
+
// after the resume is asked for, and a Stop does, in `#cancelRun`. A flag
|
|
958
|
+
// the restore cleared itself would still be set when the resumed run
|
|
959
|
+
// settles and sends the parked turn, so that send would be refused after
|
|
960
|
+
// the turn had left the queue, and lost.
|
|
961
|
+
//
|
|
962
|
+
// Whether the restore was stopped is not read off this hold, because one
|
|
963
|
+
// with nothing to resume takes none; the restore asks its own generation.
|
|
964
|
+
holdRun: () => {
|
|
965
|
+
const hold = {};
|
|
966
|
+
this.#sending = hold;
|
|
967
|
+
return {
|
|
968
|
+
release: () => {
|
|
969
|
+
if (this.#sending === hold) {
|
|
970
|
+
this.#sending = null;
|
|
971
|
+
this.#flushQueued();
|
|
972
|
+
}
|
|
973
|
+
},
|
|
974
|
+
};
|
|
975
|
+
},
|
|
875
976
|
cancelRun: () => this.#cancelRun(),
|
|
876
977
|
resetState: () => this.#resetState(),
|
|
877
978
|
setRunning: (running) => this.#setRunning(running),
|
|
@@ -941,8 +1042,11 @@ export class AgUiChat extends HTMLElement {
|
|
|
941
1042
|
return;
|
|
942
1043
|
}
|
|
943
1044
|
if (name === "user-key") {
|
|
944
|
-
//
|
|
945
|
-
//
|
|
1045
|
+
// Acted on here only while connected. Before the first connect there is
|
|
1046
|
+
// nothing to move: connectedCallback resolves the namespace from the
|
|
1047
|
+
// attribute as it stands by then. Between a disconnect and the next
|
|
1048
|
+
// connect there is, and connecting hands it over -- see #principalKey
|
|
1049
|
+
// and connectedCallback for why it waits until then. An absent attribute
|
|
946
1050
|
// and an empty one name the same (unnamed) principal, so neither is a
|
|
947
1051
|
// change worth acting on.
|
|
948
1052
|
if (this.#connected && (previous ?? "") !== (value ?? "")) {
|
|
@@ -1238,7 +1342,8 @@ export class AgUiChat extends HTMLElement {
|
|
|
1238
1342
|
// run was cancelled on the way out, and the transcript is rebuilt below
|
|
1239
1343
|
// from persisted history -- which could only be appended to what is still
|
|
1240
1344
|
// showing. The composer's recall history is kept, because a move is not a
|
|
1241
|
-
// farewell and
|
|
1345
|
+
// farewell, and the Stop on the way out dropped the queued messages because
|
|
1346
|
+
// recall already held them.
|
|
1242
1347
|
if (this.#connectedBefore) {
|
|
1243
1348
|
this.#resetConversation();
|
|
1244
1349
|
}
|
|
@@ -1283,6 +1388,33 @@ export class AgUiChat extends HTMLElement {
|
|
|
1283
1388
|
}
|
|
1284
1389
|
this.#syncLauncher();
|
|
1285
1390
|
this.#skills.init();
|
|
1391
|
+
// A `user-key` that changed while the element was out of the document gets
|
|
1392
|
+
// the handover a live change gets, here, because this is the first moment
|
|
1393
|
+
// the element can make it. A plain move with the same key skips this and
|
|
1394
|
+
// keeps the recall history, as above; a first connect skips it too, since
|
|
1395
|
+
// an element that has never connected holds nobody's state -- and treating
|
|
1396
|
+
// its first key as an arrival would adopt whatever key-less conversation
|
|
1397
|
+
// an earlier visitor left in the tab.
|
|
1398
|
+
//
|
|
1399
|
+
// Deferred to here rather than made the moment the attribute changes, for
|
|
1400
|
+
// three reasons. Out of the document, the arriving half -- replaying the
|
|
1401
|
+
// next principal's history, listing their threads -- is requests, and
|
|
1402
|
+
// nothing should go out for a node that has left (see #startup). Only where
|
|
1403
|
+
// the key ends up counts: one that goes away and comes back while detached
|
|
1404
|
+
// hands nothing over, because nobody else saw this panel in between. And
|
|
1405
|
+
// the namespace was claimed again just above, so the previous principal's
|
|
1406
|
+
// is resolved against the one this element holds now: a namespace another
|
|
1407
|
+
// element claimed while this one was out is never purged from under it.
|
|
1408
|
+
//
|
|
1409
|
+
// Before the store is scoped, which is the arriving half connecting does
|
|
1410
|
+
// anyway: an adoption has to move the conversation before the store is
|
|
1411
|
+
// asked which thread is active, or it mints a fresh one there first.
|
|
1412
|
+
const held = this.#principalKey;
|
|
1413
|
+
const changed = held !== null && held !== this.userKey;
|
|
1414
|
+
if (changed) {
|
|
1415
|
+
this.#changePrincipal(held, this.userKey);
|
|
1416
|
+
}
|
|
1417
|
+
this.#principalKey = this.userKey;
|
|
1286
1418
|
// Namespace the built-in default store too (a host-injected store is used
|
|
1287
1419
|
// verbatim). Must precede #wireThreadStore, which wraps the current store.
|
|
1288
1420
|
this.conversationStore = this.#storage.scopeStore(this.#unwrappedStore(), this.userKey);
|
|
@@ -1301,6 +1433,13 @@ export class AgUiChat extends HTMLElement {
|
|
|
1301
1433
|
// through a framework ref still has a chance to be heard — see #startup.
|
|
1302
1434
|
queueMicrotask(() => this.#startup());
|
|
1303
1435
|
void this.#history.rehydrate();
|
|
1436
|
+
if (changed) {
|
|
1437
|
+
// The last of what a live change does: a drawer left open across the
|
|
1438
|
+
// move still lists the threads of the principal who left, titles and
|
|
1439
|
+
// all, until something reloads it. A plain move leaves it alone, since
|
|
1440
|
+
// the list it shows is still this principal's.
|
|
1441
|
+
void this.#history.refreshDrawer();
|
|
1442
|
+
}
|
|
1304
1443
|
// Last: everything above reads (and some of it sets) attributes, and none
|
|
1305
1444
|
// of that should trip the connect-time warning.
|
|
1306
1445
|
this.#connected = true;
|
|
@@ -1681,8 +1820,22 @@ export class AgUiChat extends HTMLElement {
|
|
|
1681
1820
|
* logout, because a logout is a navigation (or, in a single-page app, not
|
|
1682
1821
|
* even that) rather than a tab close. Nothing remounts, so the host naming
|
|
1683
1822
|
* the new principal is the only signal the element will ever get.
|
|
1823
|
+
*
|
|
1824
|
+
* Two callers. A live change runs all of it. connectedCallback runs it for a
|
|
1825
|
+
* key that changed while the element was out of the document, and there it
|
|
1826
|
+
* runs before the store is scoped and before `#connected` is set -- which is
|
|
1827
|
+
* what `live` reads, since `isConnected` is already true by then. That call
|
|
1828
|
+
* does the half about what storage and the element still hold. The half
|
|
1829
|
+
* about the principal arriving -- the store scoped under the new key, its
|
|
1830
|
+
* active thread, the replay -- is what connecting goes on to do anyway, and
|
|
1831
|
+
* run here too it would read the old store before the new one exists,
|
|
1832
|
+
* minting a thread pointer in the namespace just purged and replaying the
|
|
1833
|
+
* history twice. Nor would it leave alone a store the host assigned while
|
|
1834
|
+
* the element was out.
|
|
1684
1835
|
*/
|
|
1685
1836
|
#changePrincipal(previousKey: string, nextKey: string): void {
|
|
1837
|
+
this.#principalKey = nextKey;
|
|
1838
|
+
const live = this.#connected;
|
|
1686
1839
|
const previous = this.#storage.conversationNamespace(previousKey);
|
|
1687
1840
|
const next = this.#storage.conversationNamespace(nextKey);
|
|
1688
1841
|
if (previousKey === "") {
|
|
@@ -1693,11 +1846,18 @@ export class AgUiChat extends HTMLElement {
|
|
|
1693
1846
|
// copying also matters: a copy left behind under the unscoped namespace
|
|
1694
1847
|
// is a transcript the next key-less mount would happily adopt.
|
|
1695
1848
|
SessionStorageStore.adopt(previous, next);
|
|
1696
|
-
|
|
1849
|
+
if (live) {
|
|
1850
|
+
this.#rescopeStore(next);
|
|
1851
|
+
}
|
|
1697
1852
|
return;
|
|
1698
1853
|
}
|
|
1854
|
+
// First, ahead of the purge: from here on, nothing a client built for the
|
|
1855
|
+
// previous principal does is written anywhere. See #tenure.
|
|
1856
|
+
this.#tenure = {};
|
|
1699
1857
|
SessionStorageStore.purge(previous);
|
|
1700
|
-
|
|
1858
|
+
if (live) {
|
|
1859
|
+
this.#rescopeStore(next);
|
|
1860
|
+
}
|
|
1701
1861
|
// An Always allow is the previous principal's decision too, and it lives in
|
|
1702
1862
|
// memory rather than in the store just purged, so it has to be forgotten
|
|
1703
1863
|
// here or it outlives them. The adoption above keeps it, for the same
|
|
@@ -1715,8 +1875,29 @@ export class AgUiChat extends HTMLElement {
|
|
|
1715
1875
|
// the new principal's first run. A host that seeds state assigns it again
|
|
1716
1876
|
// for the one who arrived.
|
|
1717
1877
|
this.#sharedState = {};
|
|
1878
|
+
// And the turn being written, also only here. A turn typed and not sent is
|
|
1879
|
+
// the previous principal's words as much as the transcript is, and so is
|
|
1880
|
+
// everything else that reaches the box: a quotation and a skill's template
|
|
1881
|
+
// are both text in it. The tray went with the transcript above. New chat
|
|
1882
|
+
// keeps the text, because the same person is still typing, and an adoption
|
|
1883
|
+
// returned before any of this for the same reason. Through the input
|
|
1884
|
+
// handler rather than beside it, so the hint and the slash palette that
|
|
1885
|
+
// were answering that text come down with it.
|
|
1886
|
+
this.#input.value = "";
|
|
1887
|
+
this.#onInput();
|
|
1718
1888
|
this.#setRunning(false);
|
|
1719
1889
|
this.#setUnread(0);
|
|
1890
|
+
if (!live) {
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
// A recording in progress is theirs too, and once stopped it would be
|
|
1894
|
+
// transcribed into the next principal's composer. Taking the mic down ends
|
|
1895
|
+
// it untranscribed, and putting a fresh one up leaves the next principal
|
|
1896
|
+
// a working control -- the way leaving and connecting do it, which is also
|
|
1897
|
+
// why only a live change does it here: a detached one has been through
|
|
1898
|
+
// both already.
|
|
1899
|
+
this.#voice.dispose();
|
|
1900
|
+
this.#voice.wire();
|
|
1720
1901
|
this.#history.adoptActiveThread();
|
|
1721
1902
|
void this.#history.rehydrate();
|
|
1722
1903
|
void this.#history.refreshDrawer();
|
|
@@ -1726,13 +1907,17 @@ export class AgUiChat extends HTMLElement {
|
|
|
1726
1907
|
* Rebuild the `sessionStorage` store under `namespace`, re-wrapping it for
|
|
1727
1908
|
* `data-threads-url` exactly as connecting did.
|
|
1728
1909
|
*
|
|
1729
|
-
* A store
|
|
1730
|
-
* somewhere the element cannot see has to
|
|
1910
|
+
* A store the host assigned is left alone, whether before connecting or
|
|
1911
|
+
* since: a store that holds its data somewhere the element cannot see has to
|
|
1912
|
+
* scope itself. The exception is a plain `SessionStorageStore` assigned
|
|
1913
|
+
* before connecting, which connecting cannot tell from the default and so
|
|
1914
|
+
* took for the element's own. Hence the unwrapped store in use is what the scope is asked
|
|
1915
|
+
* about, rather than what it remembered when connecting. The transcript on
|
|
1731
1916
|
* screen is still cleared either way — the host swapped principals, and that
|
|
1732
1917
|
* much is the element's to act on.
|
|
1733
1918
|
*/
|
|
1734
1919
|
#rescopeStore(namespace: string): void {
|
|
1735
|
-
const store = this.#storage.rescopeStore(namespace);
|
|
1920
|
+
const store = this.#storage.rescopeStore(this.#unwrappedStore(), namespace);
|
|
1736
1921
|
if (store === null) {
|
|
1737
1922
|
return;
|
|
1738
1923
|
}
|
|
@@ -1900,8 +2085,17 @@ export class AgUiChat extends HTMLElement {
|
|
|
1900
2085
|
*
|
|
1901
2086
|
* History is truncated to the most recent user message inclusive and the run
|
|
1902
2087
|
* repeats, so the agent answers what it was asked rather than being told its
|
|
1903
|
-
* last answer was wrong. Returns `false` when there is nothing to retry or
|
|
1904
|
-
*
|
|
2088
|
+
* last answer was wrong. Returns `false` when there is nothing to retry, or
|
|
2089
|
+
* while anything that refuses a send is in flight: a run, a send or another
|
|
2090
|
+
* retry whose run has not started yet, a checkpoint continuation, or a
|
|
2091
|
+
* reload's restore that will resume a run. Also `false` when host code it
|
|
2092
|
+
* runs before its run starts -- the store's save, or an activity or tool
|
|
2093
|
+
* renderer the replay draws through -- has stopped it by starting a new chat
|
|
2094
|
+
* or changing `user-key`. A tool or context provider that stops it does so
|
|
2095
|
+
* inside the client's resume, past the last point this can see: nothing is
|
|
2096
|
+
* sent, and this resolves `true`, because both a run starting and a Stop let
|
|
2097
|
+
* go of the hold it checks. Rejects with the store's error when the store
|
|
2098
|
+
* refuses the save, and then the conversation is as it was.
|
|
1905
2099
|
*
|
|
1906
2100
|
* Public because a host with its own message UI wants the same button, and
|
|
1907
2101
|
* because the failed-run notice reaches it from outside the action row.
|
|
@@ -1912,24 +2106,63 @@ export class AgUiChat extends HTMLElement {
|
|
|
1912
2106
|
* again -- unless the user waived it for this session.
|
|
1913
2107
|
*/
|
|
1914
2108
|
async retryLastTurn(): Promise<boolean> {
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
if (
|
|
2109
|
+
// Refused for whatever refuses a send, because it starts a run on the same
|
|
2110
|
+
// client. That includes a send held short of its run, which `running` does
|
|
2111
|
+
// not see yet, and a picked checkpoint, which runs on a client of its own
|
|
2112
|
+
// and leaves this one idle -- so a Retry beside it was a second run in the
|
|
2113
|
+
// one conversation, each saving over the other.
|
|
2114
|
+
if (this.#running || this.#sending !== null || this.#history.continuation !== null) {
|
|
1921
2115
|
return false;
|
|
1922
2116
|
}
|
|
1923
|
-
//
|
|
1924
|
-
//
|
|
1925
|
-
//
|
|
1926
|
-
//
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
2117
|
+
// Held from here as a send is, and for the same reason: the client starts
|
|
2118
|
+
// the run a microtask after it is asked, and nothing above sees it until
|
|
2119
|
+
// then. A send in that time went out as a second run on the same client,
|
|
2120
|
+
// the built-in Send sent rather than queued, and a pick started a
|
|
2121
|
+
// continuation beside it. Released once the run starts, by `#setRunning`,
|
|
2122
|
+
// and below when it never does.
|
|
2123
|
+
const hold = {};
|
|
2124
|
+
this.#sending = hold;
|
|
2125
|
+
try {
|
|
2126
|
+
const client = this.#ensureClient();
|
|
2127
|
+
const kept = client.truncateToLastUser();
|
|
2128
|
+
if (kept === null) {
|
|
2129
|
+
return false;
|
|
2130
|
+
}
|
|
2131
|
+
// The truncation saved through the host's store, so host code has run
|
|
2132
|
+
// with the Retry under way, and a store that started a new chat or
|
|
2133
|
+
// changed `user-key` from there has stopped it. Going on drew the
|
|
2134
|
+
// abandoned conversation into the one that replaced it and asked its
|
|
2135
|
+
// answer again on a client nothing holds.
|
|
2136
|
+
if (this.#sending !== hold) {
|
|
2137
|
+
return false;
|
|
2138
|
+
}
|
|
2139
|
+
// Re-render between the truncation and the run: the kept turns replay as
|
|
2140
|
+
// restored history (static, no entrance animation), and only the new
|
|
2141
|
+
// answer arrives live. Streaming into the old transcript would put the
|
|
2142
|
+
// new answer underneath the one it replaces.
|
|
2143
|
+
this.#clearTranscript();
|
|
2144
|
+
for (const message of kept) {
|
|
2145
|
+
this.#history.replay(message);
|
|
2146
|
+
// A replay draws through the host's activity and tool renderers, which
|
|
2147
|
+
// can stop the Retry as the store's save can. Checked after each one,
|
|
2148
|
+
// so the rest of the abandoned conversation is not drawn into the chat
|
|
2149
|
+
// that replaced it, and after the last, so its answer is not asked
|
|
2150
|
+
// again.
|
|
2151
|
+
if (this.#sending !== hold) {
|
|
2152
|
+
return false;
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
await client.resume();
|
|
2156
|
+
return true;
|
|
2157
|
+
} finally {
|
|
2158
|
+
// Only its own hold, as a send's end releases only its own. A turn the
|
|
2159
|
+
// composer queued behind it is sent from here when its run never started,
|
|
2160
|
+
// because no run settles to send it.
|
|
2161
|
+
if (this.#sending === hold) {
|
|
2162
|
+
this.#sending = null;
|
|
2163
|
+
this.#flushQueued();
|
|
2164
|
+
}
|
|
1930
2165
|
}
|
|
1931
|
-
await client.resume();
|
|
1932
|
-
return true;
|
|
1933
2166
|
}
|
|
1934
2167
|
|
|
1935
2168
|
/**
|
|
@@ -2332,7 +2565,9 @@ export class AgUiChat extends HTMLElement {
|
|
|
2332
2565
|
autoGrow(this.#input);
|
|
2333
2566
|
// Typing puts the composer back in the user's hands: the next ArrowUp
|
|
2334
2567
|
// starts from the newest turn again rather than continuing a walk through
|
|
2335
|
-
// history the user has since edited.
|
|
2568
|
+
// history the user has since edited. Needed beside the walk's own check
|
|
2569
|
+
// below, which compares text: an edit typed and then deleted leaves the
|
|
2570
|
+
// turn there unchanged, and the user has still taken the box back.
|
|
2336
2571
|
this.#recallIndex = null;
|
|
2337
2572
|
}
|
|
2338
2573
|
|
|
@@ -2369,6 +2604,9 @@ export class AgUiChat extends HTMLElement {
|
|
|
2369
2604
|
* which is what every shell and every coding agent means by this. Arrowing
|
|
2370
2605
|
* forward past the newest empties the composer again rather than sticking on
|
|
2371
2606
|
* it, so the way out is the same key that got you in.
|
|
2607
|
+
*
|
|
2608
|
+
* A walk ends with any write into the composer that is not its own, so that
|
|
2609
|
+
* whatever was written is never replaced by the next step.
|
|
2372
2610
|
*/
|
|
2373
2611
|
#recallHistory(event: KeyboardEvent): void {
|
|
2374
2612
|
const back = event.key === "ArrowUp";
|
|
@@ -2379,6 +2617,17 @@ export class AgUiChat extends HTMLElement {
|
|
|
2379
2617
|
if (drafts.length === 0) {
|
|
2380
2618
|
return;
|
|
2381
2619
|
}
|
|
2620
|
+
// The walk checks the composer rather than trusting every writer to end
|
|
2621
|
+
// it. Writers are spread across the element -- a quotation, a skill
|
|
2622
|
+
// prefill, dictation, the clears after a send -- and a walk used to end
|
|
2623
|
+
// only for those that also went through the input handler or
|
|
2624
|
+
// `#recordTurn`. A quotation and a prefill did neither, and the next
|
|
2625
|
+
// ArrowUp replaced them with a past turn.
|
|
2626
|
+
// A host page can write the composer too, which no hook of ours can see.
|
|
2627
|
+
// Comparing here is the one rule a writer added later cannot forget.
|
|
2628
|
+
if (this.#input.value !== this.#recalled) {
|
|
2629
|
+
this.#recallIndex = null;
|
|
2630
|
+
}
|
|
2382
2631
|
// An empty composer is the only safe entry: anything typed is the user's,
|
|
2383
2632
|
// and replacing it with a past turn would lose it without asking.
|
|
2384
2633
|
if (this.#recallIndex === null && (!back || this.#input.value !== "")) {
|
|
@@ -2395,6 +2644,10 @@ export class AgUiChat extends HTMLElement {
|
|
|
2395
2644
|
// honestly -- and an unreachable default is worse than an assertion,
|
|
2396
2645
|
// because it looks like a case somebody thought about.
|
|
2397
2646
|
this.#input.value = next < 0 ? "" : (drafts[next] as string);
|
|
2647
|
+
// Read back rather than taken from the draft, so a value the textarea
|
|
2648
|
+
// normalises on the way in -- its line endings -- still reads as the
|
|
2649
|
+
// walk's own on the next step instead of ending it.
|
|
2650
|
+
this.#recalled = this.#input.value;
|
|
2398
2651
|
this.#input.setSelectionRange(this.#input.value.length, this.#input.value.length);
|
|
2399
2652
|
autoGrow(this.#input);
|
|
2400
2653
|
}
|
|
@@ -2410,24 +2663,24 @@ export class AgUiChat extends HTMLElement {
|
|
|
2410
2663
|
// the user has just stopped is the opposite of what stopping meant, and it
|
|
2411
2664
|
// would arrive after they had already turned away.
|
|
2412
2665
|
//
|
|
2413
|
-
// Not sending it is not the same as destroying it, though
|
|
2414
|
-
//
|
|
2415
|
-
//
|
|
2416
|
-
//
|
|
2417
|
-
//
|
|
2418
|
-
// puts the one typed last first.
|
|
2666
|
+
// Not sending it is not the same as destroying it, though, and nothing is
|
|
2667
|
+
// destroyed here: a queued turn entered the recall history the moment it
|
|
2668
|
+
// left the composer, so ArrowUp gets it back. This used to be where it
|
|
2669
|
+
// entered, which left it out of reach for as long as it waited, and once
|
|
2670
|
+
// it was recorded on the way in, moving it again put each one in twice.
|
|
2419
2671
|
//
|
|
2420
2672
|
// This path is also reached from `disconnectedCallback`, where a DOM move
|
|
2421
2673
|
// and a framework re-render both look like a farewell and neither is one.
|
|
2422
|
-
for (const text of this.#queued) {
|
|
2423
|
-
if (this.#sentDrafts[0] !== text) {
|
|
2424
|
-
this.#sentDrafts.unshift(text);
|
|
2425
|
-
}
|
|
2426
|
-
}
|
|
2427
2674
|
this.#queued.length = 0;
|
|
2428
2675
|
this.#renderQueued();
|
|
2429
2676
|
this.#decision.abort();
|
|
2430
2677
|
this.#client?.cancel();
|
|
2678
|
+
// A send whose run has not started is ended here too, because nothing else
|
|
2679
|
+
// can reach it: the cancel above lands on a client that has not been asked
|
|
2680
|
+
// for the run yet, and a client forgets a Stop when it is next asked for one.
|
|
2681
|
+
// This is how a submit listener that starts a new chat, switches thread or
|
|
2682
|
+
// moves the element stops the send it is hearing about.
|
|
2683
|
+
this.#sending = null;
|
|
2431
2684
|
// A checkpoint continuation is as much the run in flight -- the composer
|
|
2432
2685
|
// offers Stop for it -- but it runs on a client the element does not hold.
|
|
2433
2686
|
this.#history.stopContinuation();
|
|
@@ -2443,6 +2696,13 @@ export class AgUiChat extends HTMLElement {
|
|
|
2443
2696
|
#setRunning(running: boolean): void {
|
|
2444
2697
|
const settled = this.#running && !running;
|
|
2445
2698
|
this.#running = running;
|
|
2699
|
+
// The send that asked for this run is covered from here until it settles,
|
|
2700
|
+
// so its hold ends now. Held to the send's end instead, it was still held
|
|
2701
|
+
// when the run-finished event fired, and a host's follow-up from it was
|
|
2702
|
+
// refused.
|
|
2703
|
+
if (running) {
|
|
2704
|
+
this.#sending = null;
|
|
2705
|
+
}
|
|
2446
2706
|
const label = running ? this.#strings.stop : this.#strings.send;
|
|
2447
2707
|
this.#send.title = label;
|
|
2448
2708
|
this.#send.setAttribute("aria-label", label);
|
|
@@ -2502,6 +2762,28 @@ export class AgUiChat extends HTMLElement {
|
|
|
2502
2762
|
}
|
|
2503
2763
|
}
|
|
2504
2764
|
|
|
2765
|
+
/**
|
|
2766
|
+
* Put a turn that has left the composer at the front of the recall history,
|
|
2767
|
+
* and start the next walk from it.
|
|
2768
|
+
*
|
|
2769
|
+
* Every turn the user typed and pressed a key to send comes through here:
|
|
2770
|
+
* sent at once, queued behind a run, or sent to continue a checkpoint. A
|
|
2771
|
+
* host's own `sendMessage` does not, because its text was never in the
|
|
2772
|
+
* composer, and the arrow keys walk back through what the user wrote there.
|
|
2773
|
+
*/
|
|
2774
|
+
#recordTurn(content: string): void {
|
|
2775
|
+
// A repeat of the last one is not a second entry: the point is to reach
|
|
2776
|
+
// what was said, not how often. Nothing is recorded for an attachment sent
|
|
2777
|
+
// with no text, which has nothing for the composer to hold.
|
|
2778
|
+
if (content !== "" && this.#sentDrafts[0] !== content) {
|
|
2779
|
+
this.#sentDrafts.unshift(content);
|
|
2780
|
+
}
|
|
2781
|
+
// A press sends without an input event, which is what ends a walk
|
|
2782
|
+
// otherwise. Left where it was, the next ArrowUp stepped past the turn just
|
|
2783
|
+
// taken, or past whichever one now sits where it had walked to.
|
|
2784
|
+
this.#recallIndex = null;
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2505
2787
|
async #submit(): Promise<void> {
|
|
2506
2788
|
// Ignore a submit while a run is in flight — the single choke point for
|
|
2507
2789
|
// both Enter and the Send button. The button already turns into Stop, but
|
|
@@ -2528,7 +2810,20 @@ export class AgUiChat extends HTMLElement {
|
|
|
2528
2810
|
// an attachment is settled state the tray is holding and the composer has
|
|
2529
2811
|
// no second copy of, so parking it here would mean deciding what happens
|
|
2530
2812
|
// when the user then removes the chip.
|
|
2531
|
-
|
|
2813
|
+
//
|
|
2814
|
+
// A send counts from the moment it is taken, for the same reason a pick
|
|
2815
|
+
// does. A second Send in the same task as the first -- a host clicking it
|
|
2816
|
+
// from code -- reaches here before the first's run has started, and
|
|
2817
|
+
// `sendMessage` refuses it then, so without this the box would be cleared
|
|
2818
|
+
// of a turn that went nowhere.
|
|
2819
|
+
//
|
|
2820
|
+
// Recorded for recall before it is queued or sent, because it leaves the
|
|
2821
|
+
// box either way. A turn queued here is the newest thing the user typed,
|
|
2822
|
+
// and it is still the newest while it waits; the queue later sends it
|
|
2823
|
+
// through `sendMessage`, which records nothing, so this is the only point
|
|
2824
|
+
// that sees it as the composer's.
|
|
2825
|
+
this.#recordTurn(content);
|
|
2826
|
+
if (this.#running || this.#sending !== null || this.#history.continuation !== null) {
|
|
2532
2827
|
if (content !== "") {
|
|
2533
2828
|
this.#queued.push(content);
|
|
2534
2829
|
this.#renderQueued();
|
|
@@ -2537,13 +2832,6 @@ export class AgUiChat extends HTMLElement {
|
|
|
2537
2832
|
}
|
|
2538
2833
|
return;
|
|
2539
2834
|
}
|
|
2540
|
-
// Recorded before the box is cleared, newest first, so the arrow keys walk
|
|
2541
|
-
// back through it. A repeat of the last one is not a second entry: the
|
|
2542
|
-
// point is to reach what was said, not how often.
|
|
2543
|
-
if (content !== "" && this.#sentDrafts[0] !== content) {
|
|
2544
|
-
this.#sentDrafts.unshift(content);
|
|
2545
|
-
}
|
|
2546
|
-
this.#recallIndex = null;
|
|
2547
2835
|
this.#input.value = "";
|
|
2548
2836
|
autoGrow(this.#input);
|
|
2549
2837
|
// A file still uploading does not ride along — `readyRefs()` returns only
|
|
@@ -2580,29 +2868,57 @@ export class AgUiChat extends HTMLElement {
|
|
|
2580
2868
|
*
|
|
2581
2869
|
* No-ops on an empty message, and while a run or a picked checkpoint's
|
|
2582
2870
|
* continuation is in flight, since a second concurrent run would orphan the
|
|
2583
|
-
* first.
|
|
2584
|
-
*
|
|
2585
|
-
*
|
|
2871
|
+
* first. A send is in flight from the moment it is taken rather than from
|
|
2872
|
+
* its run's start, so this no-ops from a {@link SUBMIT_EVENT} listener and
|
|
2873
|
+
* for a second call in the same task too.
|
|
2874
|
+
* Unlike the built-in Send it does not queue: it returns, and the caller
|
|
2875
|
+
* keeps what it tried to send. Nor does it consult the tray -- what you pass
|
|
2876
|
+
* is what is sent.
|
|
2586
2877
|
*/
|
|
2587
2878
|
async sendMessage(content: string, attachments: readonly AttachmentRef[] = []): Promise<void> {
|
|
2588
2879
|
if (
|
|
2589
2880
|
this.#running ||
|
|
2881
|
+
this.#sending !== null ||
|
|
2590
2882
|
this.#history.continuation !== null ||
|
|
2591
2883
|
(content === "" && attachments.length === 0)
|
|
2592
2884
|
) {
|
|
2593
2885
|
return;
|
|
2594
2886
|
}
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2887
|
+
const send = {};
|
|
2888
|
+
this.#sending = send;
|
|
2889
|
+
try {
|
|
2890
|
+
// Only a send travels. Every other way out of the empty state -- a
|
|
2891
|
+
// restored transcript, a thread picked from the drawer, a resumed
|
|
2892
|
+
// checkpoint -- is a change of context rather than a continuation of what
|
|
2893
|
+
// the user was doing, and snaps. Armed before the bubble lands so both
|
|
2894
|
+
// writes reach the same style recalculation, which is what makes the
|
|
2895
|
+
// change a transition rather than a jump.
|
|
2896
|
+
if (this.#transcript.isEmpty()) {
|
|
2897
|
+
this.setAttribute("data-composer-settling", "");
|
|
2898
|
+
}
|
|
2899
|
+
// Taken before the announcement, because its listeners run here: a host's
|
|
2900
|
+
// own send from one is refused as overlapping this one, as it would be
|
|
2901
|
+
// from any later moment of it.
|
|
2902
|
+
this.#announceTurn(content, attachments);
|
|
2903
|
+
// A listener can also have stopped it -- New chat, a thread switch, a
|
|
2904
|
+
// change of principal, moving the element. All of them reach the Stop,
|
|
2905
|
+
// which let go of this send, and the turn would otherwise go out into the
|
|
2906
|
+
// conversation that replaced it, or from an element nothing can stop.
|
|
2907
|
+
if (this.#sending !== send) {
|
|
2908
|
+
return;
|
|
2909
|
+
}
|
|
2910
|
+
await this.#client_send(content, attachments);
|
|
2911
|
+
} finally {
|
|
2912
|
+
// Still held here only if its run never started: no endpoint, or a
|
|
2913
|
+
// client or store that threw before asking for one. No run settles
|
|
2914
|
+
// then, so this is what sends a turn queued behind it. Not released if
|
|
2915
|
+
// it is no longer this send's: the run starting let go of it, or a Stop
|
|
2916
|
+
// did, and the field may hold the next send by now.
|
|
2917
|
+
if (this.#sending === send) {
|
|
2918
|
+
this.#sending = null;
|
|
2919
|
+
this.#flushQueued();
|
|
2920
|
+
}
|
|
2603
2921
|
}
|
|
2604
|
-
this.#announceTurn(content, attachments);
|
|
2605
|
-
await this.#client_send(content, attachments);
|
|
2606
2922
|
}
|
|
2607
2923
|
|
|
2608
2924
|
/**
|
|
@@ -2714,6 +3030,10 @@ export class AgUiChat extends HTMLElement {
|
|
|
2714
3030
|
// closes -- and read live, that save landed after New chat had moved the
|
|
2715
3031
|
// active thread on, filing the conversation being left under the new one.
|
|
2716
3032
|
const threadId = this.#history.threadId;
|
|
3033
|
+
// Fixed for the same reason, and checked by every write below: see #tenure.
|
|
3034
|
+
// The thread id alone cannot do it, because the store is still read live --
|
|
3035
|
+
// it has to be, since an adoption rescopes it under a run that keeps going.
|
|
3036
|
+
const tenure = this.#tenure;
|
|
2717
3037
|
const agent = this.agentFactory({
|
|
2718
3038
|
endpoint: seed.endpoint,
|
|
2719
3039
|
headers: this.#requestHeaders(),
|
|
@@ -2738,12 +3058,30 @@ export class AgUiChat extends HTMLElement {
|
|
|
2738
3058
|
// list per thread. For the conversation's own client that is its
|
|
2739
3059
|
// history; a continuation holds only what it adds, and writes the
|
|
2740
3060
|
// conversation it continues ahead of that.
|
|
3061
|
+
//
|
|
3062
|
+
// Every message list a client saves reaches the store through here -- a
|
|
3063
|
+
// send, a round's end, a tool's result, the answers it writes for calls
|
|
3064
|
+
// left open or declined by a Stop, a Stop's last save -- and so does a
|
|
3065
|
+
// `data-threads-url` store's local copy, which is the only thing that
|
|
3066
|
+
// wrapper does on a save. So this is the one gate for them. `onSaved` is
|
|
3067
|
+
// behind it as well: it hands the saved conversation on as the seed of
|
|
3068
|
+
// the next client, which would be the next principal's.
|
|
2741
3069
|
onPersist: (messages) => {
|
|
3070
|
+
if (tenure !== this.#tenure) {
|
|
3071
|
+
return;
|
|
3072
|
+
}
|
|
2742
3073
|
const conversation = [...seed.follows, ...messages];
|
|
2743
3074
|
this.conversationStore.saveMessages(threadId, conversation);
|
|
2744
3075
|
seed.onSaved?.(conversation);
|
|
2745
3076
|
},
|
|
2746
|
-
|
|
3077
|
+
// Shared state is the next principal's first `RunAgentInput.state`, and
|
|
3078
|
+
// the handover has just emptied it. A snapshot already read off the wire
|
|
3079
|
+
// is applied after the Stop, and would put it back.
|
|
3080
|
+
onStateChanged: (state) => {
|
|
3081
|
+
if (tenure === this.#tenure) {
|
|
3082
|
+
this.#onSharedStateChanged(state);
|
|
3083
|
+
}
|
|
3084
|
+
},
|
|
2747
3085
|
connectionLostMessage: this.#strings.connectionLost,
|
|
2748
3086
|
unfinishedMessage: this.#strings.callNotFinished,
|
|
2749
3087
|
declinedMessage: this.#strings.declinedAction,
|