@convex-dev/agent 0.1.9-alpha.11 → 0.1.9-alpha.12
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/commonjs/client/index.d.ts +0 -1
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +0 -2
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/streaming.d.ts.map +1 -1
- package/dist/commonjs/client/streaming.js +1 -7
- package/dist/commonjs/client/streaming.js.map +1 -1
- package/dist/commonjs/component/_generated/api.d.ts +1 -12
- package/dist/commonjs/component/schema.d.ts +26 -26
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +2 -2
- package/dist/commonjs/component/schema.js.map +1 -1
- package/dist/commonjs/component/streams.d.ts +0 -7
- package/dist/commonjs/component/streams.d.ts.map +1 -1
- package/dist/commonjs/component/streams.js +14 -44
- package/dist/commonjs/component/streams.js.map +1 -1
- package/dist/commonjs/react/deltas.d.ts.map +1 -1
- package/dist/commonjs/react/deltas.js +2 -2
- package/dist/commonjs/react/deltas.js.map +1 -1
- package/dist/commonjs/react/index.d.ts +2 -4
- package/dist/commonjs/react/index.d.ts.map +1 -1
- package/dist/commonjs/react/index.js +9 -22
- package/dist/commonjs/react/index.js.map +1 -1
- package/dist/commonjs/react/optimisticallySendMessage.js +1 -1
- package/dist/commonjs/react/optimisticallySendMessage.js.map +1 -1
- package/dist/commonjs/validators.d.ts +3 -8
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +0 -2
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/commonjs.tsbuildinfo +1 -1
- package/dist/esm/client/index.d.ts +0 -1
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +0 -2
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/streaming.d.ts.map +1 -1
- package/dist/esm/client/streaming.js +1 -7
- package/dist/esm/client/streaming.js.map +1 -1
- package/dist/esm/component/_generated/api.d.ts +1 -12
- package/dist/esm/component/schema.d.ts +26 -26
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +2 -2
- package/dist/esm/component/schema.js.map +1 -1
- package/dist/esm/component/streams.d.ts +0 -7
- package/dist/esm/component/streams.d.ts.map +1 -1
- package/dist/esm/component/streams.js +14 -44
- package/dist/esm/component/streams.js.map +1 -1
- package/dist/esm/react/deltas.d.ts.map +1 -1
- package/dist/esm/react/deltas.js +2 -2
- package/dist/esm/react/deltas.js.map +1 -1
- package/dist/esm/react/index.d.ts +2 -4
- package/dist/esm/react/index.d.ts.map +1 -1
- package/dist/esm/react/index.js +9 -22
- package/dist/esm/react/index.js.map +1 -1
- package/dist/esm/react/optimisticallySendMessage.js +1 -1
- package/dist/esm/react/optimisticallySendMessage.js.map +1 -1
- package/dist/esm/validators.d.ts +3 -8
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +0 -2
- package/dist/esm/validators.js.map +1 -1
- package/dist/esm.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/client/index.ts +0 -4
- package/src/client/streaming.ts +1 -7
- package/src/component/_generated/api.d.ts +1 -12
- package/src/component/schema.ts +2 -2
- package/src/component/streams.ts +29 -78
- package/src/react/deltas.ts +2 -4
- package/src/react/index.ts +11 -23
- package/src/react/optimisticallySendMessage.ts +1 -1
- package/src/validators.ts +0 -6
package/src/component/streams.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
vStreamMessage,
|
|
7
7
|
} from "../validators.js";
|
|
8
8
|
import { api, internal } from "./_generated/api.js";
|
|
9
|
-
import type {
|
|
9
|
+
import type { Id } from "./_generated/dataModel.js";
|
|
10
10
|
import {
|
|
11
11
|
internalMutation,
|
|
12
12
|
mutation,
|
|
@@ -106,87 +106,33 @@ export const create = mutation({
|
|
|
106
106
|
export const list = query({
|
|
107
107
|
args: {
|
|
108
108
|
threadId: v.id("threads"),
|
|
109
|
-
startOrder: v.optional(v.number()),
|
|
110
|
-
statuses: v.optional(
|
|
111
|
-
v.array(
|
|
112
|
-
v.union(
|
|
113
|
-
v.literal("streaming"),
|
|
114
|
-
v.literal("finished"),
|
|
115
|
-
v.literal("aborted")
|
|
116
|
-
)
|
|
117
|
-
)
|
|
118
|
-
),
|
|
119
109
|
},
|
|
120
110
|
returns: v.array(vStreamMessage),
|
|
121
111
|
handler: async (ctx, args) => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
"order",
|
|
143
|
-
"stepOrder",
|
|
144
|
-
"userId",
|
|
145
|
-
"agentName",
|
|
146
|
-
"model",
|
|
147
|
-
"provider",
|
|
148
|
-
"providerOptions",
|
|
149
|
-
]),
|
|
150
|
-
}));
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
export const abort = mutation({
|
|
155
|
-
args: {
|
|
156
|
-
streamId: v.id("streamingMessages"),
|
|
157
|
-
reason: v.string(),
|
|
158
|
-
},
|
|
159
|
-
returns: v.null(),
|
|
160
|
-
handler: async (ctx, args) => {
|
|
161
|
-
const stream = await ctx.db.get(args.streamId);
|
|
162
|
-
if (!stream) {
|
|
163
|
-
throw new Error(`Stream not found: ${args.streamId}`);
|
|
164
|
-
}
|
|
165
|
-
if (stream.state.kind !== "streaming") {
|
|
166
|
-
console.warn(
|
|
167
|
-
`Stream trying to abort but not currently streaming (${stream.state.kind}): ${args.streamId}`
|
|
112
|
+
return ctx.db
|
|
113
|
+
.query("streamingMessages")
|
|
114
|
+
.withIndex("threadId_state_order_stepOrder", (q) =>
|
|
115
|
+
q.eq("threadId", args.threadId).eq("state.kind", "streaming")
|
|
116
|
+
)
|
|
117
|
+
.order("desc")
|
|
118
|
+
.take(100)
|
|
119
|
+
.then((msgs) =>
|
|
120
|
+
msgs.map((m) => ({
|
|
121
|
+
streamId: m._id,
|
|
122
|
+
...pick(m, [
|
|
123
|
+
"order",
|
|
124
|
+
"stepOrder",
|
|
125
|
+
"userId",
|
|
126
|
+
"agentName",
|
|
127
|
+
"model",
|
|
128
|
+
"provider",
|
|
129
|
+
"providerOptions",
|
|
130
|
+
]),
|
|
131
|
+
}))
|
|
168
132
|
);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
await cleanupTimeoutFn(ctx, stream);
|
|
172
|
-
await ctx.db.patch(args.streamId, {
|
|
173
|
-
state: { kind: "aborted", reason: args.reason },
|
|
174
|
-
});
|
|
175
133
|
},
|
|
176
134
|
});
|
|
177
135
|
|
|
178
|
-
async function cleanupTimeoutFn(
|
|
179
|
-
ctx: MutationCtx,
|
|
180
|
-
stream: Doc<"streamingMessages">
|
|
181
|
-
) {
|
|
182
|
-
if (stream.state.kind === "streaming" && stream.state.timeoutFnId) {
|
|
183
|
-
const timeoutFn = await ctx.db.system.get(stream.state.timeoutFnId);
|
|
184
|
-
if (timeoutFn?.state.kind === "pending") {
|
|
185
|
-
await ctx.scheduler.cancel(stream.state.timeoutFnId);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
136
|
export const finish = mutation({
|
|
191
137
|
args: {
|
|
192
138
|
streamId: v.id("streamingMessages"),
|
|
@@ -207,7 +153,12 @@ export const finish = mutation({
|
|
|
207
153
|
);
|
|
208
154
|
return;
|
|
209
155
|
}
|
|
210
|
-
|
|
156
|
+
if (stream.state.timeoutFnId) {
|
|
157
|
+
const timeoutFn = await ctx.db.system.get(stream.state.timeoutFnId);
|
|
158
|
+
if (timeoutFn?.state.kind === "pending") {
|
|
159
|
+
await ctx.scheduler.cancel(stream.state.timeoutFnId);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
211
162
|
await ctx.db.patch(args.streamId, {
|
|
212
163
|
state: { kind: "finished", endedAt: Date.now() },
|
|
213
164
|
});
|
|
@@ -272,8 +223,8 @@ export const timeoutStream = internalMutation({
|
|
|
272
223
|
}
|
|
273
224
|
await ctx.db.patch(args.streamId, {
|
|
274
225
|
state: {
|
|
275
|
-
kind: "
|
|
276
|
-
|
|
226
|
+
kind: "finished",
|
|
227
|
+
endedAt: Date.now(),
|
|
277
228
|
},
|
|
278
229
|
});
|
|
279
230
|
},
|
package/src/react/deltas.ts
CHANGED
|
@@ -256,14 +256,12 @@ export function createStreamingMessage(
|
|
|
256
256
|
): MessageDoc {
|
|
257
257
|
const { streamId, ...rest } = message;
|
|
258
258
|
const metadata: MessageDoc = {
|
|
259
|
-
...rest,
|
|
260
259
|
_id: `${streamId}-${index}`,
|
|
261
260
|
_creationTime: Date.now(),
|
|
262
|
-
status:
|
|
263
|
-
{ streaming: "pending", finished: "success", aborted: "failed" } as const
|
|
264
|
-
)[message.status],
|
|
261
|
+
status: "pending",
|
|
265
262
|
threadId,
|
|
266
263
|
tool: false,
|
|
264
|
+
...rest,
|
|
267
265
|
};
|
|
268
266
|
switch (part.type) {
|
|
269
267
|
case "text-delta":
|
package/src/react/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import type { ErrorMessage } from "convex-helpers";
|
|
3
3
|
import {
|
|
4
4
|
type PaginatedQueryArgs,
|
|
5
5
|
type UsePaginatedQueryResult,
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "convex/react";
|
|
8
8
|
import { usePaginatedQuery } from "convex-helpers/react";
|
|
9
9
|
import type { FunctionArgs } from "convex/server";
|
|
10
|
-
import { useMemo,
|
|
10
|
+
import { useMemo, useState } from "react";
|
|
11
11
|
import type { MessageDoc } from "../client/index.js";
|
|
12
12
|
import type { SyncStreamsReturnValue } from "../client/types.js";
|
|
13
13
|
import type { StreamArgs } from "../validators.js";
|
|
@@ -103,18 +103,14 @@ export function useThreadMessages<
|
|
|
103
103
|
ThreadMessagesArgs<Query>,
|
|
104
104
|
ThreadMessagesResult<Query>
|
|
105
105
|
>,
|
|
106
|
-
!options.stream
|
|
107
|
-
args === "skip" ||
|
|
108
|
-
paginated.status === "LoadingFirstPage"
|
|
109
|
-
? "skip"
|
|
110
|
-
: { ...args, startOrder: paginated.results.at(-1)?.order }
|
|
106
|
+
!options.stream ? "skip" : args
|
|
111
107
|
);
|
|
112
108
|
|
|
113
109
|
const merged = useMemo(() => {
|
|
114
110
|
const streamListMessages =
|
|
115
111
|
streamMessages?.map((m) => ({
|
|
116
112
|
...m,
|
|
117
|
-
streaming:
|
|
113
|
+
streaming: true,
|
|
118
114
|
})) ?? [];
|
|
119
115
|
return {
|
|
120
116
|
...paginated,
|
|
@@ -154,7 +150,7 @@ export function useStreamingThreadMessages<
|
|
|
154
150
|
Query extends ThreadStreamQuery<any, any>,
|
|
155
151
|
>(
|
|
156
152
|
query: Query,
|
|
157
|
-
args:
|
|
153
|
+
args: ThreadMessagesArgs<Query> | "skip"
|
|
158
154
|
): Array<ThreadMessagesResult<Query>> | undefined {
|
|
159
155
|
// Invariant: streamMessages[streamId] is comprised of all deltas up to the
|
|
160
156
|
// cursor. There can be multiple messages in the same stream, e.g. for tool
|
|
@@ -162,23 +158,15 @@ export function useStreamingThreadMessages<
|
|
|
162
158
|
const [streams, setStreams] = useState<
|
|
163
159
|
Array<{ streamId: string; cursor: number; messages: MessageDoc[] }>
|
|
164
160
|
>([]);
|
|
165
|
-
const startOrderRef = useRef<number>(0);
|
|
166
|
-
const queryArgs = args === "skip" ? args : omit(args, ["startOrder"]);
|
|
167
|
-
if (args !== "skip" && !startOrderRef.current && args.startOrder) {
|
|
168
|
-
startOrderRef.current = args.startOrder;
|
|
169
|
-
}
|
|
170
161
|
// Get all the active streams
|
|
171
162
|
const streamList = useQuery(
|
|
172
163
|
query,
|
|
173
|
-
|
|
174
|
-
?
|
|
164
|
+
args === "skip"
|
|
165
|
+
? args
|
|
175
166
|
: ({
|
|
176
|
-
...
|
|
167
|
+
...args,
|
|
177
168
|
paginationOpts: { cursor: null, numItems: 0 },
|
|
178
|
-
streamArgs: {
|
|
179
|
-
kind: "list",
|
|
180
|
-
startOrder: startOrderRef.current,
|
|
181
|
-
} as StreamArgs,
|
|
169
|
+
streamArgs: { kind: "list" } as StreamArgs,
|
|
182
170
|
} as FunctionArgs<Query>)
|
|
183
171
|
) as
|
|
184
172
|
| { streams: Extract<SyncStreamsReturnValue, { kind: "list" }> }
|
|
@@ -198,10 +186,10 @@ export function useStreamingThreadMessages<
|
|
|
198
186
|
// Get the deltas for all the active streams, if any.
|
|
199
187
|
const cursorQuery = useQuery(
|
|
200
188
|
query,
|
|
201
|
-
|
|
189
|
+
args === "skip" || !streamList
|
|
202
190
|
? ("skip" as const)
|
|
203
191
|
: ({
|
|
204
|
-
...
|
|
192
|
+
...args,
|
|
205
193
|
paginationOpts: { cursor: null, numItems: 0 },
|
|
206
194
|
streamArgs: { kind: "deltas", cursors } as StreamArgs,
|
|
207
195
|
} as FunctionArgs<Query>)
|
|
@@ -11,7 +11,7 @@ export function optimisticallySendMessage(
|
|
|
11
11
|
) => void {
|
|
12
12
|
return (store, args) => {
|
|
13
13
|
const queries = store.getAllQueries(query);
|
|
14
|
-
let maxOrder =
|
|
14
|
+
let maxOrder = 0;
|
|
15
15
|
let maxStepOrder = 0;
|
|
16
16
|
for (const q of queries) {
|
|
17
17
|
if (q.args?.threadId !== args.threadId) continue;
|
package/src/validators.ts
CHANGED
|
@@ -460,7 +460,6 @@ export const vStreamArgs = v.optional(
|
|
|
460
460
|
v.union(
|
|
461
461
|
v.object({
|
|
462
462
|
kind: v.literal("list"),
|
|
463
|
-
startOrder: v.optional(v.number()),
|
|
464
463
|
}),
|
|
465
464
|
v.object({
|
|
466
465
|
kind: v.literal("deltas"),
|
|
@@ -472,11 +471,6 @@ export type StreamArgs = Infer<typeof vStreamArgs>;
|
|
|
472
471
|
|
|
473
472
|
export const vStreamMessage = v.object({
|
|
474
473
|
streamId: v.string(),
|
|
475
|
-
status: v.union(
|
|
476
|
-
v.literal("streaming"),
|
|
477
|
-
v.literal("finished"),
|
|
478
|
-
v.literal("aborted")
|
|
479
|
-
),
|
|
480
474
|
order: v.number(),
|
|
481
475
|
stepOrder: v.number(),
|
|
482
476
|
// metadata
|