@convex-dev/agent 0.2.5-alpha.0 → 0.2.6-alpha.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/dist/client/definePlaygroundAPI.d.ts +248 -18
- package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
- package/dist/client/index.d.ts +211 -33
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +56 -52
- package/dist/client/index.js.map +1 -1
- package/dist/client/messages.d.ts +50 -4
- package/dist/client/messages.d.ts.map +1 -1
- package/dist/client/mockModel.d.ts +30 -0
- package/dist/client/mockModel.d.ts.map +1 -0
- package/dist/client/mockModel.js +153 -0
- package/dist/client/mockModel.js.map +1 -0
- package/dist/client/search.d.ts +50 -4
- package/dist/client/search.d.ts.map +1 -1
- package/dist/client/streaming.d.ts +603 -46
- package/dist/client/streaming.d.ts.map +1 -1
- package/dist/client/streaming.js +30 -35
- package/dist/client/streaming.js.map +1 -1
- package/dist/client/textStreamParts.d.ts +5 -0
- package/dist/client/textStreamParts.d.ts.map +1 -0
- package/dist/{parts.js → client/textStreamParts.js} +18 -1
- package/dist/client/textStreamParts.js.map +1 -0
- package/dist/component/_generated/api.d.ts +240 -16
- package/dist/component/messages.d.ts +549 -43
- package/dist/component/messages.d.ts.map +1 -1
- package/dist/component/messages.js +1 -1
- package/dist/component/messages.js.map +1 -1
- package/dist/component/schema.d.ts +1546 -98
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/mapping.d.ts.map +1 -1
- package/dist/mapping.js +30 -18
- package/dist/mapping.js.map +1 -1
- package/dist/react/useSmoothText.d.ts +1 -1
- package/dist/react/useSmoothText.d.ts.map +1 -1
- package/dist/react/useSmoothText.js +30 -22
- package/dist/react/useSmoothText.js.map +1 -1
- package/dist/validators.d.ts +3740 -201
- package/dist/validators.d.ts.map +1 -1
- package/dist/validators.js +10 -1
- package/dist/validators.js.map +1 -1
- package/package.json +1 -1
- package/src/client/index.ts +90 -82
- package/src/client/mockModel.ts +195 -0
- package/src/client/streaming.ts +46 -54
- package/src/{parts.ts → client/textStreamParts.ts} +33 -1
- package/src/component/_generated/api.d.ts +240 -16
- package/src/component/messages.ts +1 -1
- package/src/mapping.test.ts +50 -1
- package/src/mapping.ts +42 -17
- package/src/react/toUIMessages.test.ts +16 -4
- package/src/react/useSmoothText.ts +40 -31
- package/src/validators.ts +23 -1
- package/dist/parts.d.ts +0 -3
- package/dist/parts.d.ts.map +0 -1
- package/dist/parts.js.map +0 -1
|
@@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from "react";
|
|
|
2
2
|
|
|
3
3
|
const FPS = 20;
|
|
4
4
|
const MS_PER_FRAME = 1000 / FPS;
|
|
5
|
-
const
|
|
5
|
+
const INITIAL_CHARS_PER_SEC = 128;
|
|
6
6
|
|
|
7
7
|
export type SmoothTextOptions = {
|
|
8
8
|
/**
|
|
@@ -28,17 +28,21 @@ export type SmoothTextOptions = {
|
|
|
28
28
|
*/
|
|
29
29
|
export function useSmoothText(
|
|
30
30
|
text: string,
|
|
31
|
-
{
|
|
31
|
+
{
|
|
32
|
+
charsPerSec = INITIAL_CHARS_PER_SEC,
|
|
33
|
+
startStreaming = false,
|
|
34
|
+
}: SmoothTextOptions = {},
|
|
32
35
|
): [string, { cursor: number; isStreaming: boolean }] {
|
|
33
36
|
const [visibleText, setVisibleText] = useState(
|
|
34
37
|
startStreaming ? "" : text || "",
|
|
35
38
|
);
|
|
36
39
|
const smoothState = useRef({
|
|
37
|
-
tick: Date.now()
|
|
40
|
+
tick: Date.now(),
|
|
38
41
|
cursor: visibleText.length,
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
lastUpdate: Date.now(),
|
|
43
|
+
lastUpdateLength: text.length,
|
|
41
44
|
charsPerMs: charsPerSec / 1000,
|
|
45
|
+
initial: true,
|
|
42
46
|
});
|
|
43
47
|
|
|
44
48
|
const isStreaming = smoothState.current.cursor < text.length;
|
|
@@ -47,46 +51,51 @@ export function useSmoothText(
|
|
|
47
51
|
if (!isStreaming) {
|
|
48
52
|
return;
|
|
49
53
|
}
|
|
50
|
-
if (
|
|
51
|
-
smoothState.current.
|
|
52
|
-
smoothState.current.initialLength === 0
|
|
53
|
-
) {
|
|
54
|
-
smoothState.current.cursor = text.length;
|
|
55
|
-
smoothState.current.start = Date.now();
|
|
56
|
-
smoothState.current.initialLength = text.length;
|
|
57
|
-
smoothState.current.charsPerMs = charsPerSec / 1000;
|
|
58
|
-
} else {
|
|
54
|
+
if (smoothState.current.lastUpdateLength !== text.length) {
|
|
55
|
+
const timeSinceLastUpdate = Date.now() - smoothState.current.lastUpdate;
|
|
59
56
|
const latestCharsPerMs =
|
|
60
|
-
(text.length - smoothState.current.
|
|
61
|
-
|
|
62
|
-
//
|
|
57
|
+
(text.length - smoothState.current.lastUpdateLength) /
|
|
58
|
+
timeSinceLastUpdate;
|
|
59
|
+
// Is the rate increasing?
|
|
60
|
+
const rateError = latestCharsPerMs - smoothState.current.charsPerMs;
|
|
61
|
+
// Is our visible text falling behind what it could show?
|
|
62
|
+
const charLag =
|
|
63
|
+
smoothState.current.lastUpdateLength - smoothState.current.cursor;
|
|
64
|
+
const lagRate = charLag / timeSinceLastUpdate;
|
|
65
|
+
const charsPerMs =
|
|
66
|
+
latestCharsPerMs +
|
|
67
|
+
(smoothState.current.initial
|
|
68
|
+
? 0
|
|
69
|
+
: Math.max(0, (rateError + lagRate) / 2));
|
|
70
|
+
smoothState.current.initial = false;
|
|
71
|
+
// Smooth out the charsPerSec by weighting it with the previous value.
|
|
63
72
|
smoothState.current.charsPerMs = Math.min(
|
|
64
|
-
(2 *
|
|
73
|
+
(2 * charsPerMs + smoothState.current.charsPerMs) / 3,
|
|
65
74
|
smoothState.current.charsPerMs * 2,
|
|
66
75
|
);
|
|
67
|
-
smoothState.current.tick = Math.max(
|
|
68
|
-
smoothState.current.tick,
|
|
69
|
-
Date.now() - 2 * MS_PER_FRAME,
|
|
70
|
-
);
|
|
71
76
|
}
|
|
77
|
+
smoothState.current.tick = Math.max(
|
|
78
|
+
smoothState.current.tick,
|
|
79
|
+
Date.now() - MS_PER_FRAME,
|
|
80
|
+
);
|
|
81
|
+
smoothState.current.lastUpdate = Date.now();
|
|
82
|
+
smoothState.current.lastUpdateLength = text.length;
|
|
72
83
|
|
|
73
84
|
function update() {
|
|
74
85
|
if (smoothState.current.cursor >= text.length) {
|
|
75
86
|
return;
|
|
76
87
|
}
|
|
77
88
|
const now = Date.now();
|
|
78
|
-
const timeSinceLastUpdate =
|
|
79
|
-
|
|
80
|
-
now - smoothState.current.tick,
|
|
81
|
-
);
|
|
82
|
-
const chars = Math.floor(
|
|
89
|
+
const timeSinceLastUpdate = now - smoothState.current.tick;
|
|
90
|
+
const charsSinceLastUpdate = Math.floor(
|
|
83
91
|
timeSinceLastUpdate * smoothState.current.charsPerMs,
|
|
84
92
|
);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
text.length,
|
|
93
|
+
const chars = Math.min(
|
|
94
|
+
charsSinceLastUpdate,
|
|
95
|
+
text.length - smoothState.current.cursor,
|
|
88
96
|
);
|
|
89
|
-
smoothState.current.
|
|
97
|
+
smoothState.current.cursor += chars;
|
|
98
|
+
smoothState.current.tick += chars / smoothState.current.charsPerMs;
|
|
90
99
|
setVisibleText(text.slice(0, smoothState.current.cursor));
|
|
91
100
|
}
|
|
92
101
|
update();
|
package/src/validators.ts
CHANGED
|
@@ -131,16 +131,38 @@ const vToolResultContent = v.array(
|
|
|
131
131
|
),
|
|
132
132
|
);
|
|
133
133
|
|
|
134
|
+
export const vToolResultOutput = v.union(
|
|
135
|
+
v.object({ type: v.literal("text"), value: v.string() }),
|
|
136
|
+
v.object({ type: v.literal("json"), value: v.any() }),
|
|
137
|
+
v.object({ type: v.literal("error-text"), value: v.string() }),
|
|
138
|
+
v.object({ type: v.literal("error-json"), value: v.any() }),
|
|
139
|
+
v.object({
|
|
140
|
+
type: v.literal("content"),
|
|
141
|
+
value: v.array(
|
|
142
|
+
v.union(
|
|
143
|
+
v.object({ type: v.literal("text"), text: v.string() }),
|
|
144
|
+
v.object({
|
|
145
|
+
type: v.literal("media"),
|
|
146
|
+
data: v.string(),
|
|
147
|
+
mediaType: v.string(),
|
|
148
|
+
}),
|
|
149
|
+
),
|
|
150
|
+
),
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
|
|
134
154
|
export const vToolResultPart = v.object({
|
|
135
155
|
type: v.literal("tool-result"),
|
|
136
156
|
toolCallId: v.string(),
|
|
137
157
|
toolName: v.string(),
|
|
138
|
-
|
|
158
|
+
output: v.optional(vToolResultOutput),
|
|
159
|
+
|
|
139
160
|
providerOptions,
|
|
140
161
|
providerMetadata,
|
|
141
162
|
providerExecuted: v.optional(v.boolean()),
|
|
142
163
|
|
|
143
164
|
// Deprecated in ai v5
|
|
165
|
+
result: v.optional(v.any()), // either this or output will be present
|
|
144
166
|
isError: v.optional(v.boolean()),
|
|
145
167
|
// This is only here b/c steps include it in toolResults
|
|
146
168
|
// Normal ModelMessage doesn't have this
|
package/dist/parts.d.ts
DELETED
package/dist/parts.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parts.d.ts","sourceRoot":"","sources":["../src/parts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAElD,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,cAAc,CAAC,OAAO,CAAC,EAAE,GAC/B,cAAc,CAAC,OAAO,CAAC,EAAE,CAkC3B"}
|
package/dist/parts.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parts.js","sourceRoot":"","sources":["../src/parts.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,6BAA6B,CAC3C,KAAgC;IAEhC,MAAM,UAAU,GAA8B,EAAE,CAAC;IACjD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,IAAI,GAAG,UA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