@convex-dev/agent 0.1.10-alpha.1 → 0.1.10
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/README.md +1 -1
- package/dist/commonjs/client/createTool.js +8 -5
- package/dist/commonjs/client/createTool.js.map +1 -1
- package/dist/commonjs/client/files.js +8 -3
- package/dist/commonjs/client/files.js.map +1 -1
- package/dist/commonjs/client/index.d.ts +10 -11
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +80 -59
- 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 +14 -15
- package/dist/commonjs/client/streaming.js.map +1 -1
- package/dist/commonjs/client/types.js +2 -1
- package/dist/commonjs/component/_generated/api.d.ts +2 -13
- package/dist/commonjs/component/_generated/api.js +7 -4
- package/dist/commonjs/component/_generated/api.js.map +1 -1
- package/dist/commonjs/component/_generated/server.js +11 -8
- package/dist/commonjs/component/_generated/server.js.map +1 -1
- package/dist/commonjs/component/apiKeys.js +16 -13
- package/dist/commonjs/component/apiKeys.js.map +1 -1
- package/dist/commonjs/component/convex.config.js +4 -2
- package/dist/commonjs/component/convex.config.js.map +1 -1
- package/dist/commonjs/component/files.d.ts +1 -1
- package/dist/commonjs/component/files.d.ts.map +1 -1
- package/dist/commonjs/component/files.js +47 -40
- package/dist/commonjs/component/files.js.map +1 -1
- package/dist/commonjs/component/messages.d.ts +31 -31
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +112 -107
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/commonjs/component/schema.d.ts +74 -84
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +101 -99
- 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 +95 -118
- package/dist/commonjs/component/streams.js.map +1 -1
- package/dist/commonjs/component/threads.d.ts +8 -8
- package/dist/commonjs/component/threads.js +54 -51
- package/dist/commonjs/component/threads.js.map +1 -1
- package/dist/commonjs/component/users.js +49 -46
- package/dist/commonjs/component/users.js.map +1 -1
- package/dist/commonjs/component/vector/index.js +53 -48
- package/dist/commonjs/component/vector/index.js.map +1 -1
- package/dist/commonjs/component/vector/tables.d.ts +3 -5
- package/dist/commonjs/component/vector/tables.d.ts.map +1 -1
- package/dist/commonjs/component/vector/tables.js +32 -26
- package/dist/commonjs/component/vector/tables.js.map +1 -1
- package/dist/commonjs/mapping.d.ts +3 -3
- package/dist/commonjs/mapping.d.ts.map +1 -1
- package/dist/commonjs/mapping.js +39 -24
- package/dist/commonjs/mapping.js.map +1 -1
- package/dist/commonjs/react/deltas.d.ts.map +1 -1
- package/dist/commonjs/react/deltas.js +13 -7
- 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 +37 -42
- package/dist/commonjs/react/index.js.map +1 -1
- package/dist/commonjs/react/optimisticallySendMessage.js +7 -4
- package/dist/commonjs/react/optimisticallySendMessage.js.map +1 -1
- package/dist/commonjs/react/toUIMessages.js +8 -5
- package/dist/commonjs/react/toUIMessages.js.map +1 -1
- package/dist/commonjs/react/types.js +2 -1
- package/dist/commonjs/react/useSmoothText.js +8 -5
- package/dist/commonjs/react/useSmoothText.js.map +1 -1
- package/dist/commonjs/shared.js +9 -4
- package/dist/commonjs/shared.js.map +1 -1
- package/dist/commonjs/validators.d.ts +10 -15
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +249 -247
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/commonjs/vitest.config.js +4 -2
- package/dist/commonjs/vitest.config.js.map +1 -1
- package/dist/commonjs.tsbuildinfo +1 -1
- package/dist/esm/client/index.d.ts +10 -11
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +2 -4
- 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 +2 -13
- package/dist/esm/component/files.d.ts +1 -1
- package/dist/esm/component/files.d.ts.map +1 -1
- package/dist/esm/component/files.js +6 -4
- package/dist/esm/component/files.js.map +1 -1
- package/dist/esm/component/messages.d.ts +30 -30
- package/dist/esm/component/schema.d.ts +74 -84
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +2 -3
- 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 +22 -49
- package/dist/esm/component/streams.js.map +1 -1
- package/dist/esm/component/threads.d.ts +8 -8
- package/dist/esm/component/vector/tables.d.ts +3 -5
- package/dist/esm/component/vector/tables.d.ts.map +1 -1
- package/dist/esm/mapping.d.ts +3 -3
- package/dist/esm/mapping.d.ts.map +1 -1
- package/dist/esm/mapping.js +5 -3
- package/dist/esm/mapping.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 +10 -15
- 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 +9 -4
- package/react/package.json +5 -0
- package/src/client/index.ts +15 -12
- package/src/client/streaming.ts +1 -7
- package/src/component/_generated/api.d.ts +2 -13
- package/src/component/files.ts +6 -4
- package/src/component/schema.ts +2 -3
- package/src/component/streams.ts +39 -85
- package/src/component/vector/tables.ts +1 -1
- package/src/mapping.ts +8 -4
- 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/validators/package.json +5 -0
|
@@ -67,7 +67,7 @@ export type Mounts = {
|
|
|
67
67
|
"mutation",
|
|
68
68
|
"public",
|
|
69
69
|
{ fileIds: Array<string>; force?: boolean },
|
|
70
|
-
|
|
70
|
+
Array<string>
|
|
71
71
|
>;
|
|
72
72
|
get: FunctionReference<
|
|
73
73
|
"query",
|
|
@@ -1414,12 +1414,6 @@ export type Mounts = {
|
|
|
1414
1414
|
>;
|
|
1415
1415
|
};
|
|
1416
1416
|
streams: {
|
|
1417
|
-
abort: FunctionReference<
|
|
1418
|
-
"mutation",
|
|
1419
|
-
"public",
|
|
1420
|
-
{ reason: string; streamId: string },
|
|
1421
|
-
null
|
|
1422
|
-
>;
|
|
1423
1417
|
addDelta: FunctionReference<
|
|
1424
1418
|
"mutation",
|
|
1425
1419
|
"public",
|
|
@@ -1575,18 +1569,13 @@ export type Mounts = {
|
|
|
1575
1569
|
list: FunctionReference<
|
|
1576
1570
|
"query",
|
|
1577
1571
|
"public",
|
|
1578
|
-
{
|
|
1579
|
-
startOrder?: number;
|
|
1580
|
-
statuses?: Array<"streaming" | "finished" | "aborted">;
|
|
1581
|
-
threadId: string;
|
|
1582
|
-
},
|
|
1572
|
+
{ threadId: string },
|
|
1583
1573
|
Array<{
|
|
1584
1574
|
agentName?: string;
|
|
1585
1575
|
model?: string;
|
|
1586
1576
|
order: number;
|
|
1587
1577
|
provider?: string;
|
|
1588
1578
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1589
|
-
status: "streaming" | "finished" | "aborted";
|
|
1590
1579
|
stepOrder: number;
|
|
1591
1580
|
streamId: string;
|
|
1592
1581
|
userId?: string;
|
package/src/component/files.ts
CHANGED
|
@@ -149,25 +149,27 @@ export const deleteFiles = mutation({
|
|
|
149
149
|
fileIds: v.array(v.id("files")),
|
|
150
150
|
force: v.optional(v.boolean()),
|
|
151
151
|
},
|
|
152
|
+
returns: v.array(v.id("files")),
|
|
152
153
|
handler: async (ctx, args) => {
|
|
153
|
-
await Promise.all(
|
|
154
|
+
const deletedFileIds = await Promise.all(
|
|
154
155
|
args.fileIds.map(async (fileId) => {
|
|
155
156
|
const file = await ctx.db.get(fileId);
|
|
156
157
|
if (!file) {
|
|
157
158
|
console.error(`File ${fileId} not found when deleting, skipping...`);
|
|
158
|
-
return;
|
|
159
|
+
return null;
|
|
159
160
|
}
|
|
160
161
|
if (file.refcount && file.refcount > 0) {
|
|
161
162
|
if (!args.force) {
|
|
162
163
|
console.error(
|
|
163
164
|
`File ${fileId} has refcount ${file.refcount} > 0, skipping...`
|
|
164
165
|
);
|
|
165
|
-
return;
|
|
166
|
+
return null;
|
|
166
167
|
}
|
|
167
168
|
}
|
|
168
169
|
await ctx.db.delete(fileId);
|
|
170
|
+
return fileId;
|
|
169
171
|
})
|
|
170
172
|
);
|
|
173
|
+
return deletedFileIds.filter((fileId) => fileId !== null);
|
|
171
174
|
},
|
|
172
|
-
returns: v.null(),
|
|
173
175
|
});
|
package/src/component/schema.ts
CHANGED
|
@@ -108,11 +108,10 @@ export const schema = defineSchema({
|
|
|
108
108
|
v.object({
|
|
109
109
|
kind: v.literal("finished"),
|
|
110
110
|
endedAt: v.number(),
|
|
111
|
-
cleanupFnId: v.optional(v.id("_scheduled_functions")),
|
|
112
111
|
}),
|
|
113
112
|
v.object({
|
|
114
|
-
kind: v.literal("
|
|
115
|
-
|
|
113
|
+
kind: v.literal("error"),
|
|
114
|
+
error: v.string(),
|
|
116
115
|
})
|
|
117
116
|
),
|
|
118
117
|
})
|
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,15 +153,20 @@ export const finish = mutation({
|
|
|
207
153
|
);
|
|
208
154
|
return;
|
|
209
155
|
}
|
|
210
|
-
|
|
211
|
-
|
|
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
|
+
}
|
|
162
|
+
await ctx.db.patch(args.streamId, {
|
|
163
|
+
state: { kind: "finished", endedAt: Date.now() },
|
|
164
|
+
});
|
|
165
|
+
await ctx.scheduler.runAfter(
|
|
212
166
|
DELETE_STREAM_DELAY,
|
|
213
167
|
api.streams.deleteStreamAsync,
|
|
214
168
|
{ streamId: args.streamId }
|
|
215
169
|
);
|
|
216
|
-
await ctx.db.patch(args.streamId, {
|
|
217
|
-
state: { kind: "finished", endedAt: Date.now(), cleanupFnId },
|
|
218
|
-
});
|
|
219
170
|
},
|
|
220
171
|
});
|
|
221
172
|
|
|
@@ -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
|
},
|
|
@@ -294,9 +245,12 @@ async function deletePageForStreamId(
|
|
|
294
245
|
if (deltas.isDone) {
|
|
295
246
|
const stream = await ctx.db.get(args.streamId);
|
|
296
247
|
if (stream) {
|
|
297
|
-
|
|
298
|
-
if (
|
|
299
|
-
await ctx.
|
|
248
|
+
const state = stream.state;
|
|
249
|
+
if (state.kind === "streaming" && state.timeoutFnId) {
|
|
250
|
+
const timeoutFn = await ctx.db.system.get(state.timeoutFnId);
|
|
251
|
+
if (timeoutFn?.state.kind === "pending") {
|
|
252
|
+
await ctx.scheduler.cancel(state.timeoutFnId);
|
|
253
|
+
}
|
|
300
254
|
}
|
|
301
255
|
await ctx.db.delete(args.streamId);
|
|
302
256
|
}
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
type VObject,
|
|
16
16
|
type VUnion,
|
|
17
17
|
} from "convex/values";
|
|
18
|
-
import type { QueryCtx } from "../_generated/server";
|
|
18
|
+
import type { QueryCtx } from "../_generated/server.js";
|
|
19
19
|
|
|
20
20
|
// We only generate embeddings for non-tool, non-system messages
|
|
21
21
|
const embeddings = {
|
package/src/mapping.ts
CHANGED
|
@@ -85,6 +85,7 @@ export async function serializeNewMessagesInStep<TOOLS extends ToolSet>(
|
|
|
85
85
|
usage: step.usage,
|
|
86
86
|
warnings: step.warnings,
|
|
87
87
|
finishReason: step.finishReason,
|
|
88
|
+
sources: step.stepType === "tool-result" ? undefined : step.sources,
|
|
88
89
|
};
|
|
89
90
|
const toolFields = {
|
|
90
91
|
sources: step.sources,
|
|
@@ -113,17 +114,19 @@ export async function serializeNewMessagesInStep<TOOLS extends ToolSet>(
|
|
|
113
114
|
return messages;
|
|
114
115
|
}
|
|
115
116
|
|
|
116
|
-
export function serializeObjectResult(
|
|
117
|
+
export async function serializeObjectResult(
|
|
118
|
+
ctx: ActionCtx,
|
|
119
|
+
component: AgentComponent,
|
|
117
120
|
result: GenerateObjectResult<unknown>,
|
|
118
121
|
metadata: { model: string; provider: string }
|
|
119
|
-
): { messages: MessageWithMetadata[] } {
|
|
122
|
+
): Promise<{ messages: MessageWithMetadata[] }> {
|
|
120
123
|
const text = JSON.stringify(result.object);
|
|
121
124
|
|
|
122
|
-
const message = {
|
|
125
|
+
const { message, fileIds } = await serializeMessage(ctx, component, {
|
|
123
126
|
role: "assistant" as const,
|
|
124
127
|
content: text,
|
|
125
128
|
id: result.response.id,
|
|
126
|
-
};
|
|
129
|
+
});
|
|
127
130
|
return {
|
|
128
131
|
messages: [
|
|
129
132
|
{
|
|
@@ -136,6 +139,7 @@ export function serializeObjectResult(
|
|
|
136
139
|
text,
|
|
137
140
|
usage: result.usage,
|
|
138
141
|
warnings: result.warnings,
|
|
142
|
+
fileIds,
|
|
139
143
|
},
|
|
140
144
|
],
|
|
141
145
|
};
|
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
|