@assistant-ui/core 0.3.5 → 0.3.6
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.d.ts +3 -3
- package/dist/index.js.map +1 -1
- package/dist/internal.js +1 -1
- package/dist/react/AssistantProvider.d.ts +3 -2
- package/dist/react/AssistantProvider.d.ts.map +1 -1
- package/dist/react/AssistantProvider.js +19 -26
- package/dist/react/AssistantProvider.js.map +1 -1
- package/dist/react/AssistantRuntimeProvider.d.ts +7 -2
- package/dist/react/AssistantRuntimeProvider.d.ts.map +1 -1
- package/dist/react/AssistantRuntimeProvider.js +9 -7
- package/dist/react/AssistantRuntimeProvider.js.map +1 -1
- package/dist/react/providers/AttachmentByIndexProvider.d.ts.map +1 -1
- package/dist/react/providers/AttachmentByIndexProvider.js +33 -39
- package/dist/react/providers/AttachmentByIndexProvider.js.map +1 -1
- package/dist/react/providers/ChainOfThoughtByIndicesProvider.d.ts.map +1 -1
- package/dist/react/providers/ChainOfThoughtByIndicesProvider.js +15 -20
- package/dist/react/providers/ChainOfThoughtByIndicesProvider.js.map +1 -1
- package/dist/react/providers/ChainOfThoughtPartByIndexProvider.js +17 -20
- package/dist/react/providers/ChainOfThoughtPartByIndexProvider.js.map +1 -1
- package/dist/react/providers/MessageByIdProvider.js +28 -38
- package/dist/react/providers/MessageByIdProvider.js.map +1 -1
- package/dist/react/providers/MessageByIndexProvider.js +28 -38
- package/dist/react/providers/MessageByIndexProvider.js.map +1 -1
- package/dist/react/providers/PartByIndexProvider.js +17 -20
- package/dist/react/providers/PartByIndexProvider.js.map +1 -1
- package/dist/react/providers/QueueItemByIndexProvider.js +21 -21
- package/dist/react/providers/QueueItemByIndexProvider.js.map +1 -1
- package/dist/react/providers/ReadonlyThreadProvider.js +24 -32
- package/dist/react/providers/ReadonlyThreadProvider.js.map +1 -1
- package/dist/react/providers/SuggestionByIndexProvider.js +17 -20
- package/dist/react/providers/SuggestionByIndexProvider.js.map +1 -1
- package/dist/react/providers/TextMessagePartProvider.d.ts.map +1 -1
- package/dist/react/providers/TextMessagePartProvider.js +16 -19
- package/dist/react/providers/TextMessagePartProvider.js.map +1 -1
- package/dist/react/providers/ThreadListItemByIndexProvider.js +17 -20
- package/dist/react/providers/ThreadListItemByIndexProvider.js.map +1 -1
- package/dist/react/providers/ThreadListItemRuntimeProvider.js +15 -18
- package/dist/react/providers/ThreadListItemRuntimeProvider.js.map +1 -1
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts +7 -3
- package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts +5 -2
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js +3 -2
- package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js.map +1 -1
- package/dist/react/runtimes/useLocalRuntime.d.ts +3 -1
- package/dist/react/runtimes/useLocalRuntime.d.ts.map +1 -1
- package/dist/react/runtimes/useLocalRuntime.js +4 -2
- package/dist/react/runtimes/useLocalRuntime.js.map +1 -1
- package/dist/runtime/api/composer-runtime.d.ts +5 -1
- package/dist/runtime/api/composer-runtime.d.ts.map +1 -1
- package/dist/runtime/api/composer-runtime.js +8 -1
- package/dist/runtime/api/composer-runtime.js.map +1 -1
- package/dist/runtime/api/message-runtime.js +1 -1
- package/dist/runtime/api/message-runtime.js.map +1 -1
- package/dist/runtime/api/thread-runtime.d.ts +4 -2
- package/dist/runtime/api/thread-runtime.d.ts.map +1 -1
- package/dist/runtime/base/base-composer-runtime-core.d.ts +2 -1
- package/dist/runtime/base/base-composer-runtime-core.d.ts.map +1 -1
- package/dist/runtime/base/base-composer-runtime-core.js +1 -1
- package/dist/runtime/base/base-composer-runtime-core.js.map +1 -1
- package/dist/runtime/base/default-thread-composer-runtime-core.d.ts +3 -1
- package/dist/runtime/base/default-thread-composer-runtime-core.d.ts.map +1 -1
- package/dist/runtime/base/default-thread-composer-runtime-core.js +14 -3
- package/dist/runtime/base/default-thread-composer-runtime-core.js.map +1 -1
- package/dist/runtime/interfaces/composer-runtime-core.d.ts +2 -1
- package/dist/runtime/interfaces/composer-runtime-core.d.ts.map +1 -1
- package/dist/runtime/interfaces/thread-runtime-core.d.ts +3 -1
- package/dist/runtime/interfaces/thread-runtime-core.d.ts.map +1 -1
- package/dist/runtime/queue/external-thread-queue-adapter.d.ts +19 -6
- package/dist/runtime/queue/external-thread-queue-adapter.d.ts.map +1 -1
- package/dist/runtime/queue/message-queue.d.ts +10 -1
- package/dist/runtime/queue/message-queue.d.ts.map +1 -1
- package/dist/runtime/queue/message-queue.js +138 -36
- package/dist/runtime/queue/message-queue.js.map +1 -1
- package/dist/runtime/utils/thread-message-like.d.ts.map +1 -1
- package/dist/runtime/utils/thread-message-like.js +2 -1
- package/dist/runtime/utils/thread-message-like.js.map +1 -1
- package/dist/runtimes/external-store/external-store-adapter.d.ts +1 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts +3 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts.map +1 -1
- package/dist/runtimes/external-store/external-store-thread-runtime-core.js +7 -6
- package/dist/runtimes/external-store/external-store-thread-runtime-core.js.map +1 -1
- package/dist/runtimes/local/local-runtime-options.d.ts +13 -0
- package/dist/runtimes/local/local-runtime-options.d.ts.map +1 -1
- package/dist/runtimes/local/local-thread-runtime-core.d.ts +5 -1
- package/dist/runtimes/local/local-thread-runtime-core.d.ts.map +1 -1
- package/dist/runtimes/local/local-thread-runtime-core.js +30 -14
- package/dist/runtimes/local/local-thread-runtime-core.js.map +1 -1
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.d.ts +1 -1
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.js +1 -1
- package/dist/runtimes/readonly/ReadonlyThreadRuntimeCore.js.map +1 -1
- package/dist/runtimes/remote-thread-list/empty-thread-core.js +1 -1
- package/dist/runtimes/remote-thread-list/empty-thread-core.js.map +1 -1
- package/dist/runtimes/remote-thread-list/types.d.ts +6 -3
- package/dist/runtimes/remote-thread-list/types.d.ts.map +1 -1
- package/dist/store/runtime-clients/composer-runtime-client.d.ts.map +1 -1
- package/dist/store/runtime-clients/composer-runtime-client.js +11 -4
- package/dist/store/runtime-clients/composer-runtime-client.js.map +1 -1
- package/dist/store/scopes/composer.d.ts +7 -3
- package/dist/store/scopes/composer.d.ts.map +1 -1
- package/dist/store/scopes/queue-item.d.ts +10 -0
- package/dist/store/scopes/queue-item.d.ts.map +1 -1
- package/dist/store/scopes/queue-item.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +5 -1
- package/src/react/AssistantProvider.tsx +20 -18
- package/src/react/AssistantRuntimeProvider.test.tsx +55 -1
- package/src/react/AssistantRuntimeProvider.tsx +12 -2
- package/src/react/providers/AttachmentByIndexProvider.tsx +15 -7
- package/src/react/providers/ChainOfThoughtByIndicesProvider.tsx +12 -4
- package/src/react/providers/ChainOfThoughtPartByIndexProvider.tsx +8 -4
- package/src/react/providers/MessageByIdProvider.tsx +8 -4
- package/src/react/providers/MessageByIndexProvider.tsx +8 -4
- package/src/react/providers/PartByIndexProvider.tsx +8 -4
- package/src/react/providers/QueueItemByIndexProvider.tsx +9 -5
- package/src/react/providers/ReadonlyThreadProvider.tsx +9 -4
- package/src/react/providers/SuggestionByIndexProvider.tsx +8 -4
- package/src/react/providers/TextMessagePartProvider.tsx +13 -4
- package/src/react/providers/ThreadListItemByIndexProvider.tsx +8 -4
- package/src/react/providers/ThreadListItemRuntimeProvider.tsx +8 -4
- package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.tsx +19 -4
- package/src/react/runtimes/useLocalRuntime.ts +4 -0
- package/src/runtime/api/composer-runtime.ts +11 -2
- package/src/runtime/api/message-runtime.test.ts +25 -0
- package/src/runtime/api/message-runtime.ts +1 -1
- package/src/runtime/base/base-composer-runtime-core.ts +5 -1
- package/src/runtime/base/default-thread-composer-runtime-core.ts +27 -3
- package/src/runtime/interfaces/composer-runtime-core.ts +2 -1
- package/src/runtime/interfaces/thread-runtime-core.ts +3 -1
- package/src/runtime/queue/external-thread-queue-adapter.ts +19 -3
- package/src/runtime/queue/message-queue.ts +200 -38
- package/src/runtime/utils/thread-message-like.ts +2 -4
- package/src/runtimes/external-store/external-store-thread-runtime-core.ts +10 -8
- package/src/runtimes/local/local-runtime-options.ts +15 -0
- package/src/runtimes/local/local-thread-runtime-core.test.ts +43 -0
- package/src/runtimes/local/local-thread-runtime-core.ts +48 -13
- package/src/runtimes/readonly/ReadonlyThreadRuntimeCore.ts +1 -1
- package/src/runtimes/remote-thread-list/empty-thread-core.ts +1 -1
- package/src/runtimes/remote-thread-list/types.ts +9 -2
- package/src/store/runtime-clients/composer-runtime-client.ts +13 -5
- package/src/store/scopes/composer.ts +6 -4
- package/src/store/scopes/queue-item.ts +8 -1
- package/src/tests/external-store-thread-runtime-core.test.ts +56 -13
- package/src/tests/framework-neutral-boundary.test.ts +221 -0
- package/src/tests/message-queue.test.ts +494 -34
- package/src/tests/thread-message-like.test.ts +66 -0
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
createMessageQueue,
|
|
29
29
|
type MessageQueueController,
|
|
30
30
|
} from "../../runtime/queue/message-queue";
|
|
31
|
+
import type { QueuePlacement } from "../../runtime/queue/external-thread-queue-adapter";
|
|
31
32
|
import {
|
|
32
33
|
EMPTY_QUEUE_ITEMS,
|
|
33
34
|
type QueueItemState,
|
|
@@ -68,6 +69,8 @@ export class LocalThreadRuntimeCore
|
|
|
68
69
|
|
|
69
70
|
private _queue: MessageQueueController | null = null;
|
|
70
71
|
private _queueRunInFlight = false;
|
|
72
|
+
private _activeRun: { cancelled: boolean } | null = null;
|
|
73
|
+
private _runGeneration = 0;
|
|
71
74
|
|
|
72
75
|
private _historyWrites = new Map<string, Promise<void>>();
|
|
73
76
|
|
|
@@ -206,17 +209,23 @@ export class LocalThreadRuntimeCore
|
|
|
206
209
|
// release the queue when the dispatch settles, even if it rejects
|
|
207
210
|
// before reaching startRun's finally, so a failure can't deadlock it
|
|
208
211
|
this._queueRunInFlight = true;
|
|
209
|
-
|
|
212
|
+
const generation = this._runGeneration;
|
|
213
|
+
// the tail may have moved since the message was enqueued
|
|
214
|
+
void this._runAppend({
|
|
215
|
+
...message,
|
|
216
|
+
parentId: this.messages.at(-1)?.id ?? null,
|
|
217
|
+
})
|
|
210
218
|
.finally(() => {
|
|
211
219
|
this._queueRunInFlight = false;
|
|
212
|
-
|
|
220
|
+
// a dispatch that failed before starting a run settles here;
|
|
221
|
+
// runs that did start release from _runLoop
|
|
222
|
+
if (this._runGeneration === generation) this._queue?.notifyIdle();
|
|
213
223
|
})
|
|
214
224
|
.catch(() => {});
|
|
215
225
|
},
|
|
216
226
|
});
|
|
217
227
|
this._queue.subscribe(() => this._notifySubscribers());
|
|
218
228
|
} else if (!canQueue && this._queue) {
|
|
219
|
-
this._queue.adapter.clear("cancel-run");
|
|
220
229
|
this._queue = null;
|
|
221
230
|
}
|
|
222
231
|
if (this.capabilities.queue !== canQueue) {
|
|
@@ -271,10 +280,17 @@ export class LocalThreadRuntimeCore
|
|
|
271
280
|
const isTail = message.parentId === (this.messages.at(-1)?.id ?? null);
|
|
272
281
|
const willRun = message.startRun ?? message.role === "user";
|
|
273
282
|
if (this._queue && willRun && isTail) {
|
|
274
|
-
|
|
283
|
+
if (message.steer ?? this._queueRunInFlight)
|
|
284
|
+
this._queue.adapter.steer(message);
|
|
285
|
+
else this._queue.adapter.enqueue(message);
|
|
275
286
|
return;
|
|
276
287
|
}
|
|
277
|
-
if (
|
|
288
|
+
if (
|
|
289
|
+
this._queue &&
|
|
290
|
+
!isTail &&
|
|
291
|
+
(this._options.unstable_queueClearOnRewind ?? true)
|
|
292
|
+
)
|
|
293
|
+
this._queue.clear();
|
|
278
294
|
return this._runAppend(message);
|
|
279
295
|
}
|
|
280
296
|
|
|
@@ -284,8 +300,12 @@ export class LocalThreadRuntimeCore
|
|
|
284
300
|
return this._queue?.adapter.items ?? EMPTY_QUEUE_ITEMS;
|
|
285
301
|
}
|
|
286
302
|
|
|
287
|
-
public
|
|
288
|
-
this._queue?.adapter.
|
|
303
|
+
public getSteerQueueItems(): readonly QueueItemState[] {
|
|
304
|
+
return this._queue?.adapter.steerItems ?? EMPTY_QUEUE_ITEMS;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
public moveQueueItem(queueItemId: string, placement: QueuePlacement): void {
|
|
308
|
+
this._queue?.adapter.move(queueItemId, placement);
|
|
289
309
|
}
|
|
290
310
|
|
|
291
311
|
public removeQueueItem(queueItemId: string): void {
|
|
@@ -397,6 +417,10 @@ export class LocalThreadRuntimeCore
|
|
|
397
417
|
message.status?.type === "requires-action" &&
|
|
398
418
|
this._options.adapters.history?.update !== undefined;
|
|
399
419
|
|
|
420
|
+
const run = { cancelled: false };
|
|
421
|
+
this._activeRun = run;
|
|
422
|
+
this._runGeneration++;
|
|
423
|
+
|
|
400
424
|
try {
|
|
401
425
|
// mark busy for runs not started through the queue (regenerate, resume)
|
|
402
426
|
this._queue?.notifyBusy();
|
|
@@ -417,9 +441,12 @@ export class LocalThreadRuntimeCore
|
|
|
417
441
|
} while (shouldContinue(message, this._options.unstable_humanToolNames));
|
|
418
442
|
} finally {
|
|
419
443
|
this._notifyEventSubscribers("runEnd", {});
|
|
420
|
-
//
|
|
421
|
-
//
|
|
422
|
-
|
|
444
|
+
// the settle belongs to this run only while it is still the active run
|
|
445
|
+
// or was cancelled (the engine expects a cancelled run's settle); a run
|
|
446
|
+
// superseded by a newer one stays silent
|
|
447
|
+
const active = this._activeRun === run;
|
|
448
|
+
if (active) this._activeRun = null;
|
|
449
|
+
if (active || run.cancelled) {
|
|
423
450
|
queueMicrotask(() => this._queue?.notifyIdle());
|
|
424
451
|
}
|
|
425
452
|
}
|
|
@@ -644,7 +671,8 @@ export class LocalThreadRuntimeCore
|
|
|
644
671
|
}
|
|
645
672
|
|
|
646
673
|
public detach() {
|
|
647
|
-
|
|
674
|
+
// drop the queue so pending items cannot dispatch on a detached thread
|
|
675
|
+
this._queue = null;
|
|
648
676
|
const error = new AbortError(true);
|
|
649
677
|
this.abortController?.abort(error);
|
|
650
678
|
this.abortController = null;
|
|
@@ -653,7 +681,14 @@ export class LocalThreadRuntimeCore
|
|
|
653
681
|
}
|
|
654
682
|
|
|
655
683
|
public cancelRun() {
|
|
656
|
-
this._queue
|
|
684
|
+
if (this._queue) {
|
|
685
|
+
if (this._options.unstable_queueClearOnCancel ?? true) {
|
|
686
|
+
this._queue.clear();
|
|
687
|
+
} else {
|
|
688
|
+
this._queue.notifyCancelled();
|
|
689
|
+
if (this._activeRun) this._activeRun.cancelled = true;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
657
692
|
const error = new AbortError(false);
|
|
658
693
|
this.abortController?.abort(error);
|
|
659
694
|
this.abortController = null;
|
|
@@ -681,7 +716,7 @@ export class LocalThreadRuntimeCore
|
|
|
681
716
|
if (c.type !== "tool-call") return c;
|
|
682
717
|
if (c.toolCallId !== toolCallId) return c;
|
|
683
718
|
found = true;
|
|
684
|
-
if (
|
|
719
|
+
if (c.result === undefined) added = true;
|
|
685
720
|
return {
|
|
686
721
|
...c,
|
|
687
722
|
result,
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import type { ComponentType, PropsWithChildren } from "react";
|
|
2
1
|
import type { ThreadMessage } from "../../types/message";
|
|
3
2
|
import type { AssistantRuntime } from "../../runtime/api/assistant-runtime";
|
|
4
3
|
import type { AssistantStream } from "assistant-stream";
|
|
5
4
|
|
|
5
|
+
/* oxlint-disable typescript/no-explicit-any -- structural stand-in for ComponentType without depending on react types */
|
|
6
|
+
type RemoteThreadListProviderProps = { children?: any };
|
|
7
|
+
|
|
8
|
+
export type RemoteThreadListProviderComponent =
|
|
9
|
+
| ((props: RemoteThreadListProviderProps) => any)
|
|
10
|
+
| (new (props: RemoteThreadListProviderProps) => any);
|
|
11
|
+
/* oxlint-enable typescript/no-explicit-any */
|
|
12
|
+
|
|
6
13
|
export type RemoteThreadInitializeResponse = {
|
|
7
14
|
remoteId: string;
|
|
8
15
|
externalId?: string | undefined;
|
|
@@ -54,7 +61,7 @@ export type RemoteThreadListAdapter = {
|
|
|
54
61
|
* is unsupported and leaves thread context unavailable to downstream
|
|
55
62
|
* consumers. Load data inside an always-mounted child instead.
|
|
56
63
|
*/
|
|
57
|
-
unstable_Provider?:
|
|
64
|
+
unstable_Provider?: RemoteThreadListProviderComponent | undefined;
|
|
58
65
|
};
|
|
59
66
|
|
|
60
67
|
export type RemoteThreadListOptions = {
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
} from "../../runtime/api/composer-runtime";
|
|
13
13
|
import type { ComposerState } from "../scopes/composer";
|
|
14
14
|
import type { QueueItemState } from "../scopes/queue-item";
|
|
15
|
+
import type { QueuePlacement } from "../../runtime/queue/external-thread-queue-adapter";
|
|
15
16
|
import { AttachmentRuntimeClient } from "./attachment-runtime-client";
|
|
16
17
|
import { useSubscribable } from "./useSubscribable";
|
|
17
18
|
|
|
@@ -40,16 +41,17 @@ const ComposerAttachmentClientByIndex = resource(
|
|
|
40
41
|
|
|
41
42
|
const useQueueItemClient = ({
|
|
42
43
|
item,
|
|
43
|
-
|
|
44
|
+
onMove,
|
|
44
45
|
onRemove,
|
|
45
46
|
}: {
|
|
46
47
|
item: QueueItemState;
|
|
47
|
-
|
|
48
|
+
onMove: (placement: QueuePlacement) => void;
|
|
48
49
|
onRemove: () => void;
|
|
49
50
|
}): ClientOutput<"queueItem"> => {
|
|
50
51
|
return {
|
|
51
52
|
getState: () => item,
|
|
52
|
-
steer:
|
|
53
|
+
steer: () => onMove({ lane: "steer", insertAfter: null }),
|
|
54
|
+
move: onMove,
|
|
53
55
|
remove: onRemove,
|
|
54
56
|
};
|
|
55
57
|
};
|
|
@@ -121,7 +123,7 @@ const useComposerClient = ({
|
|
|
121
123
|
item.id,
|
|
122
124
|
QueueItemClient({
|
|
123
125
|
item,
|
|
124
|
-
|
|
126
|
+
onMove: (placement) => runtime.moveQueueItem(item.id, placement),
|
|
125
127
|
onRemove: () => runtime.removeQueueItem(item.id),
|
|
126
128
|
}),
|
|
127
129
|
),
|
|
@@ -171,7 +173,13 @@ const useComposerClient = ({
|
|
|
171
173
|
return attachments.get(selector);
|
|
172
174
|
}
|
|
173
175
|
},
|
|
174
|
-
queueItem: (selector) =>
|
|
176
|
+
queueItem: (selector) => {
|
|
177
|
+
if ("id" in selector) {
|
|
178
|
+
return queueItems.get({ key: selector.id });
|
|
179
|
+
} else {
|
|
180
|
+
return queueItems.get(selector);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
175
183
|
__internal_getRuntime: () => runtime,
|
|
176
184
|
};
|
|
177
185
|
};
|
|
@@ -13,8 +13,10 @@ import type { QueueItemState, QueueItemMethods } from "./queue-item";
|
|
|
13
13
|
|
|
14
14
|
export type ComposerSendOptions = SendOptions & {
|
|
15
15
|
/**
|
|
16
|
-
* Whether to steer (
|
|
17
|
-
*
|
|
16
|
+
* Whether to steer (process this message next, interrupting the current run
|
|
17
|
+
* when the runtime supports it). Defaults to true while a queued run is in
|
|
18
|
+
* flight and false when idle. Pass false to queue behind the pending
|
|
19
|
+
* messages.
|
|
18
20
|
*/
|
|
19
21
|
steer?: boolean;
|
|
20
22
|
};
|
|
@@ -88,9 +90,9 @@ export type ComposerMethods = {
|
|
|
88
90
|
setQuote(quote: QuoteInfo | undefined): void;
|
|
89
91
|
|
|
90
92
|
/**
|
|
91
|
-
* Access a queue item by index.
|
|
93
|
+
* Access a queue item by index or id.
|
|
92
94
|
*/
|
|
93
|
-
queueItem(selector: { index: number }): QueueItemMethods;
|
|
95
|
+
queueItem(selector: { index: number } | { id: string }): QueueItemMethods;
|
|
94
96
|
|
|
95
97
|
__internal_getRuntime?(): ComposerRuntime;
|
|
96
98
|
};
|
|
@@ -1,19 +1,26 @@
|
|
|
1
|
+
import type { FileMessagePart, TextMessagePart } from "../../types/message";
|
|
2
|
+
import type { QueuePlacement } from "../../runtime/queue/external-thread-queue-adapter";
|
|
3
|
+
|
|
1
4
|
export type QueueItemState = {
|
|
2
5
|
readonly id: string;
|
|
6
|
+
/** @deprecated Derive from the text parts of `parts` instead. Removal after 2026-11-05. */
|
|
3
7
|
readonly prompt: string;
|
|
8
|
+
readonly parts: readonly (FileMessagePart | TextMessagePart)[];
|
|
4
9
|
};
|
|
5
10
|
|
|
6
11
|
export const EMPTY_QUEUE_ITEMS: readonly QueueItemState[] = Object.freeze([]);
|
|
7
12
|
|
|
8
13
|
export type QueueItemMethods = {
|
|
9
14
|
getState(): QueueItemState;
|
|
15
|
+
/** @deprecated Use `move({ lane: "steer", insertAfter: null })` instead. Removal after 2026-11-05. */
|
|
10
16
|
steer(): void;
|
|
17
|
+
move(placement: QueuePlacement): void;
|
|
11
18
|
remove(): void;
|
|
12
19
|
};
|
|
13
20
|
|
|
14
21
|
export type QueueItemMeta = {
|
|
15
22
|
source: "composer";
|
|
16
|
-
query: { index: number };
|
|
23
|
+
query: { type: "index"; index: number } | { type: "id"; id: string };
|
|
17
24
|
};
|
|
18
25
|
|
|
19
26
|
export type QueueItemClientSchema = {
|
|
@@ -832,10 +832,12 @@ describe("ExternalStoreThreadRuntimeCore - initialize event replay", () => {
|
|
|
832
832
|
describe("ExternalStoreThreadRuntimeCore - message queue", () => {
|
|
833
833
|
const makeQueue = () => ({
|
|
834
834
|
items: [] as never[],
|
|
835
|
+
steerItems: [] as never[],
|
|
835
836
|
enqueue: vi.fn(),
|
|
836
837
|
steer: vi.fn(),
|
|
838
|
+
move: vi.fn(),
|
|
839
|
+
edit: vi.fn(),
|
|
837
840
|
remove: vi.fn(),
|
|
838
|
-
clear: vi.fn(),
|
|
839
841
|
});
|
|
840
842
|
|
|
841
843
|
const appendMessage = (
|
|
@@ -866,7 +868,7 @@ describe("ExternalStoreThreadRuntimeCore - message queue", () => {
|
|
|
866
868
|
expect(withoutQueue.capabilities.queue).toBe(false);
|
|
867
869
|
});
|
|
868
870
|
|
|
869
|
-
it("routes a tail append through queue
|
|
871
|
+
it("routes a tail append through the queue adapter instead of onNew", async () => {
|
|
870
872
|
const queue = makeQueue();
|
|
871
873
|
const onNew = vi.fn();
|
|
872
874
|
const runtime = new ExternalStoreThreadRuntimeCore(
|
|
@@ -877,8 +879,48 @@ describe("ExternalStoreThreadRuntimeCore - message queue", () => {
|
|
|
877
879
|
await runtime.append(appendMessage({ steer: true }));
|
|
878
880
|
|
|
879
881
|
expect(onNew).not.toHaveBeenCalled();
|
|
882
|
+
expect(queue.enqueue).not.toHaveBeenCalled();
|
|
883
|
+
expect(queue.steer).toHaveBeenCalledTimes(1);
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it("defaults a mid-run send to steer and an idle send to enqueue", async () => {
|
|
887
|
+
const queue = makeQueue();
|
|
888
|
+
const running = new ExternalStoreThreadRuntimeCore(
|
|
889
|
+
mockContextProvider,
|
|
890
|
+
makeStore({ queue, onNew: vi.fn(), isRunning: true }),
|
|
891
|
+
);
|
|
892
|
+
await running.append(
|
|
893
|
+
appendMessage({ parentId: running.messages.at(-1)?.id ?? null }),
|
|
894
|
+
);
|
|
895
|
+
expect(queue.steer).toHaveBeenCalledTimes(1);
|
|
896
|
+
expect(queue.enqueue).not.toHaveBeenCalled();
|
|
897
|
+
|
|
898
|
+
const idleQueue = makeQueue();
|
|
899
|
+
const idle = new ExternalStoreThreadRuntimeCore(
|
|
900
|
+
mockContextProvider,
|
|
901
|
+
makeStore({ queue: idleQueue, onNew: vi.fn(), isRunning: false }),
|
|
902
|
+
);
|
|
903
|
+
await idle.append(
|
|
904
|
+
appendMessage({ parentId: idle.messages.at(-1)?.id ?? null }),
|
|
905
|
+
);
|
|
906
|
+
expect(idleQueue.enqueue).toHaveBeenCalledTimes(1);
|
|
907
|
+
expect(idleQueue.steer).not.toHaveBeenCalled();
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
it("queues behind pending items when steer is explicitly false mid-run", async () => {
|
|
911
|
+
const queue = makeQueue();
|
|
912
|
+
const runtime = new ExternalStoreThreadRuntimeCore(
|
|
913
|
+
mockContextProvider,
|
|
914
|
+
makeStore({ queue, onNew: vi.fn(), isRunning: true }),
|
|
915
|
+
);
|
|
916
|
+
await runtime.append(
|
|
917
|
+
appendMessage({
|
|
918
|
+
steer: false,
|
|
919
|
+
parentId: runtime.messages.at(-1)?.id ?? null,
|
|
920
|
+
}),
|
|
921
|
+
);
|
|
880
922
|
expect(queue.enqueue).toHaveBeenCalledTimes(1);
|
|
881
|
-
expect(queue.
|
|
923
|
+
expect(queue.steer).not.toHaveBeenCalled();
|
|
882
924
|
});
|
|
883
925
|
|
|
884
926
|
it("does not abort in-flight tools when buffering a queued send", async () => {
|
|
@@ -913,7 +955,7 @@ describe("ExternalStoreThreadRuntimeCore - message queue", () => {
|
|
|
913
955
|
expect(abort).toHaveBeenCalledTimes(1);
|
|
914
956
|
});
|
|
915
957
|
|
|
916
|
-
it("
|
|
958
|
+
it("keeps the queue on cancel, reload, and edit", async () => {
|
|
917
959
|
const queue = makeQueue();
|
|
918
960
|
const runtime = new ExternalStoreThreadRuntimeCore(
|
|
919
961
|
mockContextProvider,
|
|
@@ -926,29 +968,30 @@ describe("ExternalStoreThreadRuntimeCore - message queue", () => {
|
|
|
926
968
|
);
|
|
927
969
|
|
|
928
970
|
runtime.cancelRun();
|
|
929
|
-
expect(queue.clear).toHaveBeenCalledWith("cancel-run");
|
|
930
|
-
|
|
931
971
|
await runtime.startRun({ parentId: null, sourceId: null, runConfig: {} });
|
|
932
|
-
expect(queue.clear).toHaveBeenCalledWith("reload");
|
|
933
|
-
|
|
934
972
|
// a non-tail parentId routes to the edit branch
|
|
935
973
|
await runtime.append(appendMessage({ parentId: "not-the-tail" }));
|
|
936
|
-
|
|
974
|
+
|
|
975
|
+
expect(queue.remove).not.toHaveBeenCalled();
|
|
976
|
+
expect(queue.move).not.toHaveBeenCalled();
|
|
937
977
|
});
|
|
938
978
|
|
|
939
|
-
it("delegates
|
|
979
|
+
it("delegates queue reads / move / remove to the adapter", () => {
|
|
940
980
|
const queue = makeQueue();
|
|
941
|
-
const items = [{ id: "q1", prompt: "queued" }];
|
|
981
|
+
const items = [{ id: "q1", prompt: "queued", parts: [] }];
|
|
982
|
+
const steerItems = [{ id: "s1", prompt: "steered", parts: [] }];
|
|
942
983
|
queue.items = items as never;
|
|
984
|
+
queue.steerItems = steerItems as never;
|
|
943
985
|
const runtime = new ExternalStoreThreadRuntimeCore(
|
|
944
986
|
mockContextProvider,
|
|
945
987
|
makeStore({ queue }),
|
|
946
988
|
);
|
|
947
989
|
|
|
948
990
|
expect(runtime.getQueueItems()).toBe(items);
|
|
949
|
-
runtime.
|
|
991
|
+
expect(runtime.getSteerQueueItems()).toBe(steerItems);
|
|
992
|
+
runtime.moveQueueItem("q1", { lane: "steer" });
|
|
950
993
|
runtime.removeQueueItem("q1");
|
|
951
|
-
expect(queue.
|
|
994
|
+
expect(queue.move).toHaveBeenCalledWith("q1", { lane: "steer" });
|
|
952
995
|
expect(queue.remove).toHaveBeenCalledWith("q1");
|
|
953
996
|
});
|
|
954
997
|
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
4
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
5
|
+
|
|
6
|
+
const SRC_DIR = resolve(__dirname, "..");
|
|
7
|
+
|
|
8
|
+
const FORBIDDEN_MODULES = new Set([
|
|
9
|
+
"react",
|
|
10
|
+
"react/jsx-runtime",
|
|
11
|
+
"@assistant-ui/store",
|
|
12
|
+
"@assistant-ui/tap",
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
const STORE_ALLOWED_REACT_NAMES = new Set([
|
|
16
|
+
"useState",
|
|
17
|
+
"useReducer",
|
|
18
|
+
"useRef",
|
|
19
|
+
"useMemo",
|
|
20
|
+
"useCallback",
|
|
21
|
+
"useEffect",
|
|
22
|
+
"useLayoutEffect",
|
|
23
|
+
"useInsertionEffect",
|
|
24
|
+
"useEffectEvent",
|
|
25
|
+
"useSyncExternalStore",
|
|
26
|
+
"useDebugValue",
|
|
27
|
+
"use",
|
|
28
|
+
"useContext",
|
|
29
|
+
"createContext",
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const IMPORT_RE =
|
|
33
|
+
/(?:import|export)\s+(?:type\s+)?(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)(?:\s*,\s*(?:\{[^}]*\}|\*\s+as\s+\w+|\w+))*\s+from\s+)?["']([^"']+)["']/g;
|
|
34
|
+
|
|
35
|
+
const SIDE_EFFECT_IMPORT_RE = /import\s+["']([^"']+)["']/g;
|
|
36
|
+
|
|
37
|
+
const EXPORT_STAR_RE = /export\s+\*\s+from\s+["']([^"']+)["']/g;
|
|
38
|
+
|
|
39
|
+
const DYNAMIC_IMPORT_RE = /\b(?:import|require)\s*\(\s*["']([^"']+)["']\s*\)/g;
|
|
40
|
+
|
|
41
|
+
const FROM_REACT_RE =
|
|
42
|
+
/(?:import|export)\s+(?:type\s+)?([\s\S]*?)\s+from\s+["']react["']/g;
|
|
43
|
+
|
|
44
|
+
function resolveRelative(fromFile: string, spec: string): string | null {
|
|
45
|
+
if (!spec.startsWith(".")) return null;
|
|
46
|
+
const base = resolve(dirname(fromFile), spec);
|
|
47
|
+
const candidates = [
|
|
48
|
+
base,
|
|
49
|
+
`${base}.ts`,
|
|
50
|
+
`${base}.tsx`,
|
|
51
|
+
join(base, "index.ts"),
|
|
52
|
+
join(base, "index.tsx"),
|
|
53
|
+
];
|
|
54
|
+
for (const candidate of candidates) {
|
|
55
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
56
|
+
return candidate;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function collectImports(content: string): string[] {
|
|
63
|
+
const specs: string[] = [];
|
|
64
|
+
for (const re of [
|
|
65
|
+
IMPORT_RE,
|
|
66
|
+
SIDE_EFFECT_IMPORT_RE,
|
|
67
|
+
EXPORT_STAR_RE,
|
|
68
|
+
DYNAMIC_IMPORT_RE,
|
|
69
|
+
]) {
|
|
70
|
+
re.lastIndex = 0;
|
|
71
|
+
let match: RegExpExecArray | null;
|
|
72
|
+
while ((match = re.exec(content)) !== null) {
|
|
73
|
+
specs.push(match[1]!);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return specs;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function walkFrom(entry: string): { forbidden: string[]; jsxFiles: string[] } {
|
|
80
|
+
const visited = new Set<string>();
|
|
81
|
+
const queue = [entry];
|
|
82
|
+
const forbidden: string[] = [];
|
|
83
|
+
|
|
84
|
+
while (queue.length > 0) {
|
|
85
|
+
const file = queue.pop()!;
|
|
86
|
+
if (visited.has(file)) continue;
|
|
87
|
+
visited.add(file);
|
|
88
|
+
|
|
89
|
+
const content = readFileSync(file, "utf-8");
|
|
90
|
+
const specs = collectImports(content);
|
|
91
|
+
|
|
92
|
+
for (const spec of specs) {
|
|
93
|
+
if (FORBIDDEN_MODULES.has(spec)) {
|
|
94
|
+
forbidden.push(`${relative(SRC_DIR, file)} imports "${spec}"`);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const resolved = resolveRelative(file, spec);
|
|
98
|
+
if (resolved && !visited.has(resolved)) {
|
|
99
|
+
queue.push(resolved);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const jsxFiles = [...visited]
|
|
105
|
+
.filter((file) => file.endsWith(".tsx"))
|
|
106
|
+
.map((file) => relative(SRC_DIR, file));
|
|
107
|
+
|
|
108
|
+
return { forbidden, jsxFiles };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function findFiles(dir: string, ext: string[]): string[] {
|
|
112
|
+
const results: string[] = [];
|
|
113
|
+
for (const entry of readdirSync(dir)) {
|
|
114
|
+
const full = join(dir, entry);
|
|
115
|
+
if (statSync(full).isDirectory()) {
|
|
116
|
+
results.push(...findFiles(full, ext));
|
|
117
|
+
} else if (ext.some((e) => full.endsWith(e))) {
|
|
118
|
+
results.push(full);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return results;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function nonTypeImportNames(clause: string): string[] {
|
|
125
|
+
const names: string[] = [];
|
|
126
|
+
const trimmed = clause.trim();
|
|
127
|
+
if (trimmed.startsWith("type ")) return names;
|
|
128
|
+
if (/^\*\s+as\s+\w+$/.test(trimmed)) return names;
|
|
129
|
+
|
|
130
|
+
const defaultAndNamed = trimmed.match(/^(\w+)\s*,\s*\{([\s\S]*)\}$/);
|
|
131
|
+
const namedOnly = trimmed.match(/^\{([\s\S]*)\}$/);
|
|
132
|
+
const defaultOnly = trimmed.match(/^(\w+)$/);
|
|
133
|
+
|
|
134
|
+
let namedBody = "";
|
|
135
|
+
if (defaultAndNamed) {
|
|
136
|
+
names.push(defaultAndNamed[1]!);
|
|
137
|
+
namedBody = defaultAndNamed[2]!;
|
|
138
|
+
} else if (namedOnly) {
|
|
139
|
+
namedBody = namedOnly[1]!;
|
|
140
|
+
} else if (defaultOnly) {
|
|
141
|
+
names.push(defaultOnly[1]!);
|
|
142
|
+
return names;
|
|
143
|
+
} else {
|
|
144
|
+
return names;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const part of namedBody.split(",")) {
|
|
148
|
+
const p = part.trim();
|
|
149
|
+
if (!p || p.startsWith("type ")) continue;
|
|
150
|
+
const asMatch = p.match(/^(\w+)(?:\s+as\s+\w+)?$/);
|
|
151
|
+
if (asMatch) names.push(asMatch[1]!);
|
|
152
|
+
}
|
|
153
|
+
return names;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
describe("framework neutral boundary", () => {
|
|
157
|
+
it("the default entry graph must not import react, react/jsx-runtime, @assistant-ui/tap, or @assistant-ui/store", () => {
|
|
158
|
+
const { forbidden, jsxFiles } = walkFrom(resolve(SRC_DIR, "index.ts"));
|
|
159
|
+
expect(
|
|
160
|
+
forbidden,
|
|
161
|
+
`The default entry must stay loadable without react, @assistant-ui/tap, or @assistant-ui/store installed.\n` +
|
|
162
|
+
`These modules in the src/index.ts graph import a forbidden package:\n` +
|
|
163
|
+
forbidden.map((f) => ` - ${f}`).join("\n"),
|
|
164
|
+
).toEqual([]);
|
|
165
|
+
expect(
|
|
166
|
+
jsxFiles,
|
|
167
|
+
`The JSX transform injects react/jsx-runtime at build time, so the default entry graph must not contain .tsx files:\n` +
|
|
168
|
+
jsxFiles.map((f) => ` - ${f}`).join("\n"),
|
|
169
|
+
).toEqual([]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("the internal entry graph must not import react, react/jsx-runtime, @assistant-ui/tap, or @assistant-ui/store", () => {
|
|
173
|
+
const { forbidden, jsxFiles } = walkFrom(resolve(SRC_DIR, "internal.ts"));
|
|
174
|
+
expect(
|
|
175
|
+
forbidden,
|
|
176
|
+
"The internal entry must stay loadable without react, react/jsx-runtime, @assistant-ui/tap, or @assistant-ui/store installed.\nViolations:\n" +
|
|
177
|
+
forbidden.map((f) => ` - ${f}`).join("\n"),
|
|
178
|
+
).toEqual([]);
|
|
179
|
+
expect(
|
|
180
|
+
jsxFiles,
|
|
181
|
+
`The JSX transform injects react/jsx-runtime at build time, so the internal entry graph must not contain .tsx files:\n` +
|
|
182
|
+
jsxFiles.map((f) => ` - ${f}`).join("\n"),
|
|
183
|
+
).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("src/store may only pull react names the tap shim dispatches", () => {
|
|
187
|
+
const storeDir = resolve(SRC_DIR, "store");
|
|
188
|
+
const files = findFiles(storeDir, [".ts"]).filter(
|
|
189
|
+
(f) => !f.includes(".test."),
|
|
190
|
+
);
|
|
191
|
+
const violations: string[] = [];
|
|
192
|
+
|
|
193
|
+
for (const file of files) {
|
|
194
|
+
const content = readFileSync(file, "utf-8");
|
|
195
|
+
FROM_REACT_RE.lastIndex = 0;
|
|
196
|
+
let match: RegExpExecArray | null;
|
|
197
|
+
while ((match = FROM_REACT_RE.exec(content)) !== null) {
|
|
198
|
+
if (/^(?:import|export)\s+type\b/.test(match[0]!)) continue;
|
|
199
|
+
const clause = match[1]!;
|
|
200
|
+
if (clause.trimStart().startsWith("type ")) continue;
|
|
201
|
+
for (const name of nonTypeImportNames(clause)) {
|
|
202
|
+
if (!STORE_ALLOWED_REACT_NAMES.has(name)) {
|
|
203
|
+
violations.push(
|
|
204
|
+
`${relative(SRC_DIR, file)} imports "${name}" from "react"`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
expect(
|
|
212
|
+
violations,
|
|
213
|
+
`src/store may only use react names the tap shim dispatches ` +
|
|
214
|
+
`(useState, useReducer, useRef, useMemo, useCallback, useEffect, useLayoutEffect, ` +
|
|
215
|
+
`useInsertionEffect, useEffectEvent, useSyncExternalStore, useDebugValue, use, ` +
|
|
216
|
+
`useContext, createContext).\n` +
|
|
217
|
+
`Violations:\n` +
|
|
218
|
+
violations.map((f) => ` - ${f}`).join("\n"),
|
|
219
|
+
).toEqual([]);
|
|
220
|
+
});
|
|
221
|
+
});
|