@convex-dev/agent 0.2.5 → 0.2.6-alpha.1

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.
Files changed (55) hide show
  1. package/dist/client/definePlaygroundAPI.d.ts +248 -18
  2. package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
  3. package/dist/client/index.d.ts +198 -21
  4. package/dist/client/index.d.ts.map +1 -1
  5. package/dist/client/index.js +10 -7
  6. package/dist/client/index.js.map +1 -1
  7. package/dist/client/messages.d.ts +50 -4
  8. package/dist/client/messages.d.ts.map +1 -1
  9. package/dist/client/search.d.ts +50 -4
  10. package/dist/client/search.d.ts.map +1 -1
  11. package/dist/client/streaming.d.ts +603 -47
  12. package/dist/client/streaming.d.ts.map +1 -1
  13. package/dist/client/streaming.js +26 -33
  14. package/dist/client/streaming.js.map +1 -1
  15. package/dist/client/textStreamParts.d.ts +5 -0
  16. package/dist/client/textStreamParts.d.ts.map +1 -0
  17. package/dist/{parts.js → client/textStreamParts.js} +18 -1
  18. package/dist/client/textStreamParts.js.map +1 -0
  19. package/dist/client/types.d.ts +9 -1
  20. package/dist/client/types.d.ts.map +1 -1
  21. package/dist/component/_generated/api.d.ts +240 -16
  22. package/dist/component/messages.d.ts +549 -43
  23. package/dist/component/messages.d.ts.map +1 -1
  24. package/dist/component/schema.d.ts +1546 -98
  25. package/dist/component/schema.d.ts.map +1 -1
  26. package/dist/mapping.d.ts +2 -1
  27. package/dist/mapping.d.ts.map +1 -1
  28. package/dist/mapping.js +30 -18
  29. package/dist/mapping.js.map +1 -1
  30. package/dist/react/deltas.d.ts.map +1 -1
  31. package/dist/react/deltas.js +14 -5
  32. package/dist/react/deltas.js.map +1 -1
  33. package/dist/react/useSmoothText.d.ts +1 -1
  34. package/dist/react/useSmoothText.d.ts.map +1 -1
  35. package/dist/react/useSmoothText.js +30 -22
  36. package/dist/react/useSmoothText.js.map +1 -1
  37. package/dist/validators.d.ts +3740 -201
  38. package/dist/validators.d.ts.map +1 -1
  39. package/dist/validators.js +10 -1
  40. package/dist/validators.js.map +1 -1
  41. package/package.json +1 -1
  42. package/src/client/index.ts +12 -8
  43. package/src/client/streaming.ts +42 -51
  44. package/src/{parts.ts → client/textStreamParts.ts} +33 -1
  45. package/src/client/types.ts +9 -2
  46. package/src/component/_generated/api.d.ts +240 -16
  47. package/src/mapping.test.ts +50 -1
  48. package/src/mapping.ts +42 -17
  49. package/src/react/deltas.ts +17 -5
  50. package/src/react/toUIMessages.test.ts +16 -4
  51. package/src/react/useSmoothText.ts +40 -31
  52. package/src/validators.ts +23 -1
  53. package/dist/parts.d.ts +0 -3
  54. package/dist/parts.d.ts.map +0 -1
  55. package/dist/parts.js.map +0 -1
@@ -106,7 +106,10 @@ describe("toUIMessages", () => {
106
106
  type: "tool-result",
107
107
  toolCallId: "call1",
108
108
  toolName: "myTool",
109
- result: "42",
109
+ output: {
110
+ type: "text",
111
+ value: "42",
112
+ },
110
113
  },
111
114
  ],
112
115
  },
@@ -129,7 +132,10 @@ describe("toUIMessages", () => {
129
132
  type: "tool-myTool",
130
133
  toolCallId: "call1",
131
134
  state: "output-available",
132
- output: "42",
135
+ output: {
136
+ type: "text",
137
+ value: "42",
138
+ },
133
139
  });
134
140
  });
135
141
 
@@ -590,7 +596,10 @@ describe("toUIMessages", () => {
590
596
  type: "tool-result",
591
597
  toolCallId: "call1",
592
598
  toolName: "calculator",
593
- result: "42",
599
+ output: {
600
+ type: "text",
601
+ value: "42",
602
+ },
594
603
  },
595
604
  ],
596
605
  },
@@ -639,7 +648,10 @@ describe("toUIMessages", () => {
639
648
  toolCallId: "call1",
640
649
  state: "output-available",
641
650
  input: { operation: "add", a: 40, b: 2 },
642
- output: "42",
651
+ output: {
652
+ type: "text",
653
+ value: "42",
654
+ },
643
655
  });
644
656
 
645
657
  // Should also have text part
@@ -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 MAX_TIME_JUMP_MS = 250;
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
- { charsPerSec = 256, startStreaming = false }: SmoothTextOptions = {},
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() + (visibleText.length * 1000) / charsPerSec,
40
+ tick: Date.now(),
38
41
  cursor: visibleText.length,
39
- start: Date.now(),
40
- initialLength: visibleText.length,
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.cursor === 0 &&
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.initialLength) /
61
- (Date.now() - smoothState.current.start);
62
- // Smooth out the charsPerSec by averaging it with the previous value.
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 * latestCharsPerMs + smoothState.current.charsPerMs) / 3,
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 = Math.min(
79
- MAX_TIME_JUMP_MS,
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
- smoothState.current.cursor = Math.min(
86
- smoothState.current.cursor + chars,
87
- text.length,
93
+ const chars = Math.min(
94
+ charsSinceLastUpdate,
95
+ text.length - smoothState.current.cursor,
88
96
  );
89
- smoothState.current.tick = now;
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
- result: v.any(),
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
@@ -1,3 +0,0 @@
1
- import type { TextStreamPart, ToolSet } from "ai";
2
- export declare function serializeTextStreamingPartsV5(parts: TextStreamPart<ToolSet>[]): TextStreamPart<ToolSet>[];
3
- //# sourceMappingURL=parts.d.ts.map
@@ -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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