@assistant-ui/react 0.7.85 → 0.7.87
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/api/AssistantRuntime.d.ts +10 -3
- package/dist/api/AssistantRuntime.d.ts.map +1 -1
- package/dist/api/AssistantRuntime.js +8 -6
- package/dist/api/AssistantRuntime.js.map +1 -1
- package/dist/api/AssistantRuntime.mjs +8 -8
- package/dist/api/AssistantRuntime.mjs.map +1 -1
- package/dist/cloud/useCloudThreadListRuntime.d.ts +1 -1
- package/dist/cloud/useCloudThreadListRuntime.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/index.mjs.map +1 -1
- package/dist/model-context/ModelContextTypes.d.ts +1 -1
- package/dist/model-context/ModelContextTypes.d.ts.map +1 -1
- package/dist/model-context/ModelContextTypes.js +2 -1
- package/dist/model-context/ModelContextTypes.js.map +1 -1
- package/dist/model-context/ModelContextTypes.mjs +2 -1
- package/dist/model-context/ModelContextTypes.mjs.map +1 -1
- package/dist/model-context/index.d.ts +2 -0
- package/dist/model-context/index.d.ts.map +1 -1
- package/dist/model-context/index.js +6 -0
- package/dist/model-context/index.js.map +1 -1
- package/dist/model-context/index.mjs +4 -0
- package/dist/model-context/index.mjs.map +1 -1
- package/dist/model-context/makeAssistantReadable.d.ts +6 -0
- package/dist/model-context/makeAssistantReadable.d.ts.map +1 -0
- package/dist/model-context/makeAssistantReadable.js +86 -0
- package/dist/model-context/makeAssistantReadable.js.map +1 -0
- package/dist/model-context/makeAssistantReadable.mjs +69 -0
- package/dist/model-context/makeAssistantReadable.mjs.map +1 -0
- package/dist/model-context/tool.d.ts +12 -0
- package/dist/model-context/tool.d.ts.map +1 -0
- package/dist/model-context/tool.js +33 -0
- package/dist/model-context/tool.js.map +1 -0
- package/dist/model-context/tool.mjs +8 -0
- package/dist/model-context/tool.mjs.map +1 -0
- package/dist/runtimes/core/BaseThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtimes/core/BaseThreadRuntimeCore.js +2 -0
- package/dist/runtimes/core/BaseThreadRuntimeCore.js.map +1 -1
- package/dist/runtimes/core/BaseThreadRuntimeCore.mjs +2 -0
- package/dist/runtimes/core/BaseThreadRuntimeCore.mjs.map +1 -1
- package/dist/runtimes/dangerous-in-browser/useDangerousInBrowserRuntime.d.ts +1 -1
- package/dist/runtimes/dangerous-in-browser/useDangerousInBrowserRuntime.d.ts.map +1 -1
- package/dist/runtimes/edge/useEdgeRuntime.d.ts +1 -1
- package/dist/runtimes/edge/useEdgeRuntime.d.ts.map +1 -1
- package/dist/runtimes/external-store/useExternalStoreRuntime.d.ts +2 -1
- package/dist/runtimes/external-store/useExternalStoreRuntime.d.ts.map +1 -1
- package/dist/runtimes/external-store/useExternalStoreRuntime.js +1 -5
- package/dist/runtimes/external-store/useExternalStoreRuntime.js.map +1 -1
- package/dist/runtimes/external-store/useExternalStoreRuntime.mjs +1 -5
- package/dist/runtimes/external-store/useExternalStoreRuntime.mjs.map +1 -1
- package/dist/runtimes/index.d.ts +1 -0
- package/dist/runtimes/index.d.ts.map +1 -1
- package/dist/runtimes/index.js +9 -0
- package/dist/runtimes/index.js.map +1 -1
- package/dist/runtimes/index.mjs +4 -0
- package/dist/runtimes/index.mjs.map +1 -1
- package/dist/runtimes/local/LocalRuntimeCore.d.ts +0 -4
- package/dist/runtimes/local/LocalRuntimeCore.d.ts.map +1 -1
- package/dist/runtimes/local/LocalRuntimeCore.js +2 -23
- package/dist/runtimes/local/LocalRuntimeCore.js.map +1 -1
- package/dist/runtimes/local/LocalRuntimeCore.mjs +2 -23
- package/dist/runtimes/local/LocalRuntimeCore.mjs.map +1 -1
- package/dist/runtimes/local/useLocalRuntime.d.ts +2 -6
- package/dist/runtimes/local/useLocalRuntime.d.ts.map +1 -1
- package/dist/runtimes/local/useLocalRuntime.js +2 -19
- package/dist/runtimes/local/useLocalRuntime.js.map +1 -1
- package/dist/runtimes/local/useLocalRuntime.mjs +2 -21
- package/dist/runtimes/local/useLocalRuntime.mjs.map +1 -1
- package/dist/runtimes/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts +4 -4
- package/dist/runtimes/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts +4 -4
- package/dist/runtimes/remote-thread-list/useRemoteThreadListRuntime.d.ts +2 -1
- package/dist/runtimes/remote-thread-list/useRemoteThreadListRuntime.d.ts.map +1 -1
- package/dist/runtimes/remote-thread-list/useRemoteThreadListRuntime.js +1 -1
- package/dist/runtimes/remote-thread-list/useRemoteThreadListRuntime.js.map +1 -1
- package/dist/runtimes/remote-thread-list/useRemoteThreadListRuntime.mjs +1 -1
- package/dist/runtimes/remote-thread-list/useRemoteThreadListRuntime.mjs.map +1 -1
- package/dist/runtimes/utils/MessageRepository.d.ts +7 -2
- package/dist/runtimes/utils/MessageRepository.d.ts.map +1 -1
- package/dist/runtimes/utils/MessageRepository.js +26 -0
- package/dist/runtimes/utils/MessageRepository.js.map +1 -1
- package/dist/runtimes/utils/MessageRepository.mjs +26 -1
- package/dist/runtimes/utils/MessageRepository.mjs.map +1 -1
- package/dist/tailwindcss/index.d.ts.map +1 -1
- package/dist/tailwindcss/index.js +1 -4
- package/dist/tailwindcss/index.js.map +1 -1
- package/dist/tailwindcss/index.mjs +6 -9
- package/dist/tailwindcss/index.mjs.map +1 -1
- package/dist/utils/json/fix-json.d.ts +1 -1
- package/dist/utils/json/fix-json.d.ts.map +1 -1
- package/dist/utils/json/fix-json.js +5 -1
- package/dist/utils/json/fix-json.js.map +1 -1
- package/dist/utils/json/fix-json.mjs +5 -1
- package/dist/utils/json/fix-json.mjs.map +1 -1
- package/dist/utils/json/parse-partial-json.d.ts +2 -0
- package/dist/utils/json/parse-partial-json.d.ts.map +1 -1
- package/dist/utils/json/parse-partial-json.js +40 -3
- package/dist/utils/json/parse-partial-json.js.map +1 -1
- package/dist/utils/json/parse-partial-json.mjs +38 -2
- package/dist/utils/json/parse-partial-json.mjs.map +1 -1
- package/package.json +4 -1
- package/src/api/AssistantRuntime.ts +15 -22
- package/src/index.ts +2 -0
- package/src/model-context/ModelContextTypes.ts +4 -2
- package/src/model-context/index.ts +3 -0
- package/src/model-context/makeAssistantReadable.tsx +81 -0
- package/src/model-context/tool.ts +26 -0
- package/src/runtimes/core/BaseThreadRuntimeCore.tsx +2 -1
- package/src/runtimes/external-store/useExternalStoreRuntime.tsx +1 -5
- package/src/runtimes/index.ts +2 -0
- package/src/runtimes/local/LocalRuntimeCore.tsx +1 -33
- package/src/runtimes/local/useLocalRuntime.tsx +2 -29
- package/src/runtimes/remote-thread-list/useRemoteThreadListRuntime.tsx +1 -1
- package/src/runtimes/utils/MessageRepository.tsx +33 -3
- package/src/tailwindcss/index.ts +6 -9
- package/src/utils/json/fix-json.ts +6 -2
- package/src/utils/json/parse-partial-json.ts +61 -1
@@ -167,13 +167,14 @@ export abstract class BaseThreadRuntimeCore implements ThreadRuntimeCore {
|
|
167
167
|
}
|
168
168
|
}
|
169
169
|
|
170
|
+
// TODO import()/export() on external store doesn't make much sense
|
170
171
|
public export() {
|
171
172
|
return this.repository.export();
|
172
173
|
}
|
173
174
|
|
174
175
|
public import(data: ExportedMessageRepository) {
|
175
176
|
this.ensureInitialized();
|
176
|
-
|
177
|
+
this.repository.clear();
|
177
178
|
this.repository.import(data);
|
178
179
|
this._notifySubscribers();
|
179
180
|
}
|
@@ -4,7 +4,6 @@ import { useEffect, useMemo, useState } from "react";
|
|
4
4
|
import { ExternalStoreRuntimeCore } from "./ExternalStoreRuntimeCore";
|
5
5
|
import { ExternalStoreAdapter } from "./ExternalStoreAdapter";
|
6
6
|
import { AssistantRuntimeImpl } from "../../api/AssistantRuntime";
|
7
|
-
import { ThreadRuntimeImpl } from "../../api/ThreadRuntime";
|
8
7
|
import { useRuntimeAdapters } from "../adapters/RuntimeAdapterProvider";
|
9
8
|
|
10
9
|
export const useExternalStoreRuntime = <T,>(store: ExternalStoreAdapter<T>) => {
|
@@ -21,8 +20,5 @@ export const useExternalStoreRuntime = <T,>(store: ExternalStoreAdapter<T>) => {
|
|
21
20
|
return runtime.registerModelContextProvider(modelContext);
|
22
21
|
}, [modelContext, runtime]);
|
23
22
|
|
24
|
-
return useMemo(
|
25
|
-
() => AssistantRuntimeImpl.create(runtime, ThreadRuntimeImpl),
|
26
|
-
[runtime],
|
27
|
-
);
|
23
|
+
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
28
24
|
};
|
package/src/runtimes/index.ts
CHANGED
@@ -1,28 +1,9 @@
|
|
1
|
-
import type { CoreMessage } from "../../types/AssistantTypes";
|
2
1
|
import { BaseAssistantRuntimeCore } from "../core/BaseAssistantRuntimeCore";
|
3
2
|
import { LocalThreadRuntimeCore } from "./LocalThreadRuntimeCore";
|
4
3
|
import { LocalRuntimeOptionsBase } from "./LocalRuntimeOptions";
|
5
4
|
import { LocalThreadListRuntimeCore } from "./LocalThreadListRuntimeCore";
|
6
5
|
import { ExportedMessageRepository } from "../utils/MessageRepository";
|
7
6
|
import { ThreadMessageLike } from "../external-store";
|
8
|
-
import { fromThreadMessageLike } from "../external-store/ThreadMessageLike";
|
9
|
-
import { generateId } from "../../internal";
|
10
|
-
import { getAutoStatus } from "../external-store/auto-status";
|
11
|
-
|
12
|
-
const getExportFromInitialMessages = (
|
13
|
-
initialMessages: readonly ThreadMessageLike[],
|
14
|
-
): ExportedMessageRepository => {
|
15
|
-
const messages = initialMessages.map((i, idx) => {
|
16
|
-
const isLast = idx === initialMessages.length - 1;
|
17
|
-
return fromThreadMessageLike(i, generateId(), getAutoStatus(isLast, false));
|
18
|
-
});
|
19
|
-
return {
|
20
|
-
messages: messages.map((m, idx) => ({
|
21
|
-
parentId: messages[idx - 1]?.id ?? null,
|
22
|
-
message: m,
|
23
|
-
})),
|
24
|
-
};
|
25
|
-
};
|
26
7
|
|
27
8
|
export class LocalRuntimeCore extends BaseAssistantRuntimeCore {
|
28
9
|
public readonly threads;
|
@@ -45,20 +26,7 @@ export class LocalRuntimeCore extends BaseAssistantRuntimeCore {
|
|
45
26
|
if (initialMessages) {
|
46
27
|
this.threads
|
47
28
|
.getMainThreadRuntimeCore()
|
48
|
-
.import(
|
29
|
+
.import(ExportedMessageRepository.fromArray(initialMessages));
|
49
30
|
}
|
50
31
|
}
|
51
|
-
|
52
|
-
public reset({
|
53
|
-
initialMessages,
|
54
|
-
}: {
|
55
|
-
initialMessages?: readonly CoreMessage[] | undefined;
|
56
|
-
} = {}) {
|
57
|
-
this.threads.switchToNewThread();
|
58
|
-
if (!initialMessages) return;
|
59
|
-
|
60
|
-
this.threads
|
61
|
-
.getMainThreadRuntimeCore()
|
62
|
-
.import(getExportFromInitialMessages(initialMessages));
|
63
|
-
}
|
64
32
|
}
|
@@ -4,37 +4,10 @@ import { useEffect, useMemo, useState } from "react";
|
|
4
4
|
import type { ChatModelAdapter } from "./ChatModelAdapter";
|
5
5
|
import { LocalRuntimeCore } from "./LocalRuntimeCore";
|
6
6
|
import { LocalRuntimeOptions } from "./LocalRuntimeOptions";
|
7
|
-
import {
|
8
|
-
AssistantRuntime,
|
9
|
-
AssistantRuntimeImpl,
|
10
|
-
} from "../../api/AssistantRuntime";
|
11
|
-
import { ThreadRuntimeImpl } from "../../internal";
|
12
7
|
import { useRuntimeAdapters } from "../adapters/RuntimeAdapterProvider";
|
13
8
|
import { useRemoteThreadListRuntime } from "../remote-thread-list/useRemoteThreadListRuntime";
|
14
9
|
import { useCloudThreadListAdapter } from "../remote-thread-list/adapter/cloud";
|
15
|
-
|
16
|
-
export type LocalRuntime = AssistantRuntime & {
|
17
|
-
reset: (options?: Parameters<LocalRuntimeCore["reset"]>[0]) => void;
|
18
|
-
};
|
19
|
-
|
20
|
-
class LocalRuntimeImpl extends AssistantRuntimeImpl implements LocalRuntime {
|
21
|
-
private constructor(private core: LocalRuntimeCore) {
|
22
|
-
super(core, ThreadRuntimeImpl);
|
23
|
-
}
|
24
|
-
|
25
|
-
public override __internal_bindMethods() {
|
26
|
-
super.__internal_bindMethods();
|
27
|
-
this.reset = this.reset.bind(this);
|
28
|
-
}
|
29
|
-
|
30
|
-
public reset(options?: Parameters<LocalRuntimeCore["reset"]>[0]) {
|
31
|
-
this.core.reset(options);
|
32
|
-
}
|
33
|
-
|
34
|
-
public static override create(_core: LocalRuntimeCore): LocalRuntime {
|
35
|
-
return new LocalRuntimeImpl(_core);
|
36
|
-
}
|
37
|
-
}
|
10
|
+
import { AssistantRuntimeImpl } from "../../internal";
|
38
11
|
|
39
12
|
const useLocalThreadRuntime = (
|
40
13
|
adapter: ChatModelAdapter,
|
@@ -65,7 +38,7 @@ const useLocalThreadRuntime = (
|
|
65
38
|
return runtime.registerModelContextProvider(modelContext);
|
66
39
|
}, [modelContext, runtime]);
|
67
40
|
|
68
|
-
return useMemo(() =>
|
41
|
+
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
69
42
|
};
|
70
43
|
|
71
44
|
export const useLocalRuntime = (
|
@@ -34,5 +34,5 @@ export const useRemoteThreadListRuntime = (
|
|
34
34
|
runtime.threads.__internal_setOptions(options);
|
35
35
|
runtime.threads.__internal_load();
|
36
36
|
}, [runtime, options]);
|
37
|
-
return useMemo(() => AssistantRuntimeImpl
|
37
|
+
return useMemo(() => new AssistantRuntimeImpl(runtime), [runtime]);
|
38
38
|
};
|
@@ -1,6 +1,9 @@
|
|
1
1
|
import type { CoreMessage, ThreadMessage } from "../../types/AssistantTypes";
|
2
|
-
import { generateOptimisticId } from "../../utils/idUtils";
|
2
|
+
import { generateId, generateOptimisticId } from "../../utils/idUtils";
|
3
3
|
import { fromCoreMessage } from "../edge/converters/fromCoreMessage";
|
4
|
+
import { ThreadMessageLike } from "../external-store";
|
5
|
+
import { getAutoStatus } from "../external-store/auto-status";
|
6
|
+
import { fromThreadMessageLike } from "../external-store/ThreadMessageLike";
|
4
7
|
|
5
8
|
type RepositoryParent = {
|
6
9
|
children: string[];
|
@@ -18,13 +21,30 @@ export type ExportedMessageRepositoryItem = {
|
|
18
21
|
parentId: string | null;
|
19
22
|
};
|
20
23
|
|
21
|
-
export
|
24
|
+
export type ExportedMessageRepository = {
|
22
25
|
headId?: string | null;
|
23
26
|
messages: Array<{
|
24
27
|
message: ThreadMessage;
|
25
28
|
parentId: string | null;
|
26
29
|
}>;
|
27
|
-
}
|
30
|
+
};
|
31
|
+
|
32
|
+
export const ExportedMessageRepository = {
|
33
|
+
fromArray: (
|
34
|
+
messages: readonly ThreadMessageLike[],
|
35
|
+
): ExportedMessageRepository => {
|
36
|
+
const conv = messages.map((m) =>
|
37
|
+
fromThreadMessageLike(m, generateId(), getAutoStatus(false, false)),
|
38
|
+
);
|
39
|
+
|
40
|
+
return {
|
41
|
+
messages: conv.map((m, idx) => ({
|
42
|
+
parentId: idx > 0 ? conv[idx - 1]!.id : null,
|
43
|
+
message: m,
|
44
|
+
})),
|
45
|
+
};
|
46
|
+
},
|
47
|
+
};
|
28
48
|
|
29
49
|
const findHead = (
|
30
50
|
message: RepositoryMessage | RepositoryParent,
|
@@ -287,6 +307,16 @@ export class MessageRepository {
|
|
287
307
|
this._messages.dirty();
|
288
308
|
}
|
289
309
|
|
310
|
+
clear(): void {
|
311
|
+
this.messages.clear();
|
312
|
+
this.head = null;
|
313
|
+
this.root = {
|
314
|
+
children: [],
|
315
|
+
next: null,
|
316
|
+
};
|
317
|
+
this._messages.dirty();
|
318
|
+
}
|
319
|
+
|
290
320
|
export(): ExportedMessageRepository {
|
291
321
|
const exportItems: ExportedMessageRepository["messages"] = [];
|
292
322
|
|
package/src/tailwindcss/index.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import plugin from "tailwindcss/plugin.js";
|
2
|
-
import baseComponentsCSS from "../../dist/styles/tailwindcss/base-components.css.json";
|
3
|
-
import threadCSS from "../../dist/styles/tailwindcss/thread.css.json";
|
4
|
-
import modalCSS from "../../dist/styles/tailwindcss/modal.css.json";
|
5
|
-
import defaultThemeCSS from "../../dist/styles/themes/default.css.json";
|
6
|
-
import shadcnExtrasCSS from "../../dist/styles/themes/shadcn-extras.css.json";
|
2
|
+
import baseComponentsCSS from "../../dist/styles/tailwindcss/base-components.css.json" with { type: "json" };
|
3
|
+
import threadCSS from "../../dist/styles/tailwindcss/thread.css.json" with { type: "json" };
|
4
|
+
import modalCSS from "../../dist/styles/tailwindcss/modal.css.json" with { type: "json" };
|
5
|
+
import defaultThemeCSS from "../../dist/styles/themes/default.css.json" with { type: "json" };
|
6
|
+
import shadcnExtrasCSS from "../../dist/styles/themes/shadcn-extras.css.json" with { type: "json" };
|
7
7
|
|
8
8
|
type AssistantTailwindPluginColors = {
|
9
9
|
border: string;
|
@@ -77,10 +77,7 @@ const auiPlugin = plugin.withOptions<AssisstantTailwindPluginOptions>(
|
|
77
77
|
addComponents(modalCSS);
|
78
78
|
}
|
79
79
|
},
|
80
|
-
({
|
81
|
-
colors = {},
|
82
|
-
shadcn = false,
|
83
|
-
} = {}) => {
|
80
|
+
({ colors = {}, shadcn = false } = {}) => {
|
84
81
|
const prefix = !shadcn ? "--aui-" : "--";
|
85
82
|
return {
|
86
83
|
safelist: [{ pattern: /^aui-/ }],
|
@@ -40,7 +40,7 @@ type State =
|
|
40
40
|
// Please note that invalid JSON is not considered/covered, because it
|
41
41
|
// is assumed that the resulting JSON will be processed by a standard
|
42
42
|
// JSON parser that will detect any invalid JSON.
|
43
|
-
export function fixJson(input: string): string {
|
43
|
+
export function fixJson(input: string): [string, number] {
|
44
44
|
const stack: State[] = ["ROOT"];
|
45
45
|
let lastValidIndex = -1;
|
46
46
|
let literalStart: number | null = null;
|
@@ -372,6 +372,7 @@ export function fixJson(input: string): string {
|
|
372
372
|
}
|
373
373
|
|
374
374
|
let result = input.slice(0, lastValidIndex + 1);
|
375
|
+
let partialCount = 0;
|
375
376
|
|
376
377
|
for (let i = stack.length - 1; i >= 0; i--) {
|
377
378
|
const state = stack[i];
|
@@ -379,6 +380,7 @@ export function fixJson(input: string): string {
|
|
379
380
|
switch (state) {
|
380
381
|
case "INSIDE_STRING": {
|
381
382
|
result += '"';
|
383
|
+
partialCount++;
|
382
384
|
break;
|
383
385
|
}
|
384
386
|
|
@@ -389,6 +391,7 @@ export function fixJson(input: string): string {
|
|
389
391
|
case "INSIDE_OBJECT_BEFORE_VALUE":
|
390
392
|
case "INSIDE_OBJECT_AFTER_VALUE": {
|
391
393
|
result += "}";
|
394
|
+
partialCount++;
|
392
395
|
break;
|
393
396
|
}
|
394
397
|
|
@@ -396,6 +399,7 @@ export function fixJson(input: string): string {
|
|
396
399
|
case "INSIDE_ARRAY_AFTER_COMMA":
|
397
400
|
case "INSIDE_ARRAY_AFTER_VALUE": {
|
398
401
|
result += "]";
|
402
|
+
partialCount++;
|
399
403
|
break;
|
400
404
|
}
|
401
405
|
|
@@ -413,5 +417,5 @@ export function fixJson(input: string): string {
|
|
413
417
|
}
|
414
418
|
}
|
415
419
|
|
416
|
-
return result;
|
420
|
+
return [result, partialCount];
|
417
421
|
}
|
@@ -1,14 +1,74 @@
|
|
1
1
|
import sjson from "secure-json-parse";
|
2
2
|
import { fixJson } from "./fix-json";
|
3
|
+
import {
|
4
|
+
ContentPartStatus,
|
5
|
+
ToolCallContentPartStatus,
|
6
|
+
} from "../../types/AssistantTypes";
|
7
|
+
import { useContentPart } from "../../context";
|
3
8
|
|
9
|
+
const PARTIAL_JSON_COUNT_SYMBOL = Symbol("partial-json-count");
|
4
10
|
export const parsePartialJson = (json: string) => {
|
5
11
|
try {
|
6
12
|
return sjson.parse(json);
|
7
13
|
} catch {
|
8
14
|
try {
|
9
|
-
|
15
|
+
const [fixedJson, partialCount] = fixJson(json);
|
16
|
+
const res = sjson.parse(fixedJson);
|
17
|
+
res[PARTIAL_JSON_COUNT_SYMBOL] = partialCount;
|
18
|
+
return res;
|
10
19
|
} catch {
|
11
20
|
return undefined;
|
12
21
|
}
|
13
22
|
}
|
14
23
|
};
|
24
|
+
|
25
|
+
const COMPLETE_STATUS = Object.freeze({ type: "complete" });
|
26
|
+
|
27
|
+
const getFieldStatus = (
|
28
|
+
lastState: ContentPartStatus,
|
29
|
+
args: unknown,
|
30
|
+
fieldPath: string[],
|
31
|
+
partialCount: number,
|
32
|
+
): ContentPartStatus => {
|
33
|
+
if (fieldPath.length === 0) return lastState;
|
34
|
+
if (typeof args !== "object" || args === null) return COMPLETE_STATUS;
|
35
|
+
|
36
|
+
const path = fieldPath.at(-1)!;
|
37
|
+
|
38
|
+
// If the expected property does not exist, mark as incomplete
|
39
|
+
if (!Object.prototype.hasOwnProperty.call(args, path)) {
|
40
|
+
return lastState;
|
41
|
+
}
|
42
|
+
|
43
|
+
const argsKeys = Object.keys(args);
|
44
|
+
const isLast = argsKeys[argsKeys.length - 1] === path;
|
45
|
+
if (!isLast) return COMPLETE_STATUS;
|
46
|
+
|
47
|
+
return getFieldStatus(
|
48
|
+
lastState,
|
49
|
+
(args as Record<string, unknown>)[path],
|
50
|
+
fieldPath.slice(0, -1),
|
51
|
+
partialCount - 1,
|
52
|
+
);
|
53
|
+
};
|
54
|
+
|
55
|
+
const getToolArgsFieldStatus = (
|
56
|
+
status: ToolCallContentPartStatus,
|
57
|
+
args: Record<string, unknown>,
|
58
|
+
fieldPath: string[],
|
59
|
+
): ContentPartStatus => {
|
60
|
+
const partialCount = (args as any)[PARTIAL_JSON_COUNT_SYMBOL] ?? 0;
|
61
|
+
if (partialCount === 0) return COMPLETE_STATUS;
|
62
|
+
|
63
|
+
const lastState: ContentPartStatus =
|
64
|
+
status.type !== "requires-action" ? status : COMPLETE_STATUS;
|
65
|
+
|
66
|
+
return getFieldStatus(lastState, args, fieldPath, partialCount);
|
67
|
+
};
|
68
|
+
|
69
|
+
export const useToolArgsFieldStatus = (fieldPath: string[]) => {
|
70
|
+
return useContentPart((p) => {
|
71
|
+
if (p.type !== "tool-call") throw new Error("not a tool call");
|
72
|
+
return getToolArgsFieldStatus(p.status, p.args, fieldPath);
|
73
|
+
});
|
74
|
+
};
|