Skip to content

Commit 656c285

Browse files
authored
Merge pull request #14 from get-convex/ian/threads-no-order
`v0.0.5`
2 parents 2370496 + e718763 commit 656c285

File tree

11 files changed

+50
-55
lines changed

11 files changed

+50
-55
lines changed

example/convex/example.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,23 @@ export const t = action({
270270
},
271271
}),
272272
},
273-
maxSteps: 5,
273+
maxSteps: 20,
274274
});
275275

276-
const { threadId, thread } = await fastAgent.createThread(ctx, {});
277-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
278-
const s = await thread.streamText({
279-
prompt:
280-
"Do something four times, then do something else, then do something else again",
281-
});
282-
console.log("s", s);
283-
284-
for await (const chunk of s.textStream) {
285-
console.log(chunk);
286-
}
276+
// // eslint-disable-next-line @typescript-eslint/no-floating-promises
277+
await Promise.all(
278+
Array.from({ length: 10 }).map(async (i) => {
279+
const { threadId, thread } = await fastAgent.createThread(ctx, {
280+
userId: "123",
281+
});
282+
const s = await thread.streamText({
283+
prompt: "Do something twice",
284+
});
285+
console.log("agent", i);
286+
await s.response;
287+
}),
288+
);
289+
console.log("done");
287290
// return result.text;
288291
},
289292
});

example/convex/http.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { httpRouter } from "convex/server";
2-
import { httpAction } from "./_generated/server";
32
import { streamHttpAction } from "./example";
43

54
const http = httpRouter();
65

76
http.route({
87
path: "/streamText",
9-
method: "GET",
8+
method: "POST",
109
handler: streamHttpAction,
1110
});
1211

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"email": "support@convex.dev",
88
"url": "https://github.com/get-convex/agent/issues"
99
},
10-
"version": "0.0.4",
10+
"version": "0.0.5-alpha.1",
1111
"license": "Apache-2.0",
1212
"keywords": [
1313
"convex",

src/client/index.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
ToolChoice,
1515
ToolExecutionOptions,
1616
ToolSet,
17-
Message as UIMessage,
1817
} from "ai";
1918
import {
2019
generateObject,
@@ -38,6 +37,7 @@ import {
3837
VectorDimension,
3938
} from "../component/vector/tables";
4039
import {
40+
AIMessageWithoutId,
4141
promptOrMessagesToCoreMessages,
4242
serializeMessageWithId,
4343
serializeNewMessagesInStep,
@@ -288,17 +288,14 @@ export class Agent<AgentTools extends ToolSet> {
288288
included = new Set(searchMessages.map((m) => m._id));
289289
contextMessages.push(...searchMessages.map((m) => m.message!));
290290
}
291-
if (args.threadId) {
291+
if (args.threadId && opts.recentMessages !== 0) {
292292
const { page } = await ctx.runQuery(
293293
this.component.messages.getThreadMessages,
294294
{
295295
threadId: args.threadId,
296296
isTool: args.includeToolCalls ?? false,
297297
paginationOpts: {
298-
numItems:
299-
args.recentMessages ??
300-
this.options.contextOptions?.recentMessages ??
301-
DEFAULT_RECENT_MESSAGES,
298+
numItems: opts.recentMessages ?? DEFAULT_RECENT_MESSAGES,
302299
cursor: null,
303300
},
304301
parentMessageId: args.parentMessageId,
@@ -555,7 +552,7 @@ export class Agent<AgentTools extends ToolSet> {
555552
async saveMessagesAndFetchContext<
556553
T extends {
557554
prompt?: string;
558-
messages?: CoreMessage[] | Omit<UIMessage, "id">[];
555+
messages?: CoreMessage[] | AIMessageWithoutId[];
559556
system?: string;
560557
},
561558
>(
@@ -834,7 +831,7 @@ export class Agent<AgentTools extends ToolSet> {
834831
...rest,
835832
schema: jsonSchema(schema),
836833
} as unknown as OurStreamObjectArgs<unknown>);
837-
for await (const chunk of value.fullStream) {
834+
for await (const _ of value.fullStream) {
838835
// no-op, just consume the stream
839836
}
840837
return value.object;
@@ -859,23 +856,28 @@ export class Agent<AgentTools extends ToolSet> {
859856
args: Validator<unknown, "required", string>;
860857
contextOptions?: ContextOptions;
861858
maxSteps?: number;
859+
provideMessageHistory?: boolean;
862860
}) {
863861
return createTool({
864862
...spec,
865-
handler: async (ctx, args) => {
863+
handler: async (ctx, args, options) => {
866864
const maxSteps = spec.maxSteps ?? this.options.maxSteps;
867865
const contextOptions =
868866
spec.contextOptions && this.mergedContextOptions(spec.contextOptions);
869-
const value = await this.generateText(
870-
ctx,
871-
{ userId: ctx.userId, threadId: ctx.threadId },
872-
{
873-
prompt: JSON.stringify(args),
874-
parentMessageId: ctx.messageId,
875-
maxSteps,
876-
...contextOptions,
877-
}
878-
);
867+
const { thread } = await this.createThread(ctx, {
868+
parentThreadIds: ctx.threadId ? [ctx.threadId] : undefined,
869+
userId: ctx.userId,
870+
});
871+
const messages = spec.provideMessageHistory ? options.messages : [];
872+
messages.push({
873+
role: "user",
874+
content: JSON.stringify(args),
875+
});
876+
const value = await thread.generateText({
877+
messages,
878+
maxSteps,
879+
...contextOptions,
880+
});
879881
return value.text;
880882
},
881883
});

src/component/_generated/api.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* @module
99
*/
1010

11-
import type * as lib from "../lib.js";
1211
import type * as messages from "../messages.js";
1312
import type * as vector_index from "../vector/index.js";
1413
import type * as vector_tables from "../vector/tables.js";
@@ -28,7 +27,6 @@ import type {
2827
* ```
2928
*/
3029
declare const fullApi: ApiFromModules<{
31-
lib: typeof lib;
3230
messages: typeof messages;
3331
"vector/index": typeof vector_index;
3432
"vector/tables": typeof vector_tables;

src/component/lib.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/component/messages.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const getThreadsByUserId = query({
5555
handler: async (ctx, args) => {
5656
const threads = await paginator(ctx.db, schema)
5757
.query("threads")
58-
.withIndex("userId_order", (q) => q.eq("userId", args.userId))
58+
.withIndex("userId", (q) => q.eq("userId", args.userId))
5959
.order(args.order ?? "desc")
6060
.paginate(args.paginationOpts ?? { cursor: null, numItems: 100 });
6161
return threads;
@@ -68,15 +68,8 @@ const vThread = schema.tables.threads.validator;
6868
export const createThread = mutation({
6969
args: omit(vThread.fields, ["order", "status"]),
7070
handler: async (ctx, args) => {
71-
const latestThread = await ctx.db
72-
.query("threads")
73-
.withIndex("userId_order", (q) => q.eq("userId", args.userId))
74-
.order("desc")
75-
.first();
76-
const order = (latestThread?.order ?? -1) + 1;
7771
const threadId = await ctx.db.insert("threads", {
7872
...args,
79-
order,
8073
status: "active",
8174
});
8275
return (await ctx.db.get(threadId))!;
@@ -207,7 +200,7 @@ async function deletePageForUserId(
207200
): Promise<DeleteAllReturns> {
208201
const threads = await paginator(ctx.db, schema)
209202
.query("threads")
210-
.withIndex("userId_order", (q) => q.eq("userId", args.userId))
203+
.withIndex("userId", (q) => q.eq("userId", args.userId))
211204
.order("desc")
212205
.paginate({
213206
numItems: 100,

src/component/schema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import vectorTables, { vVectorId } from "./vector/tables";
77
export const schema = defineSchema({
88
threads: defineTable({
99
userId: v.optional(v.string()), // Unset for anonymous
10-
order: v.optional(v.number()), // within a domain
1110
// TODO: is this bubbling up in continue?
1211
defaultSystemPrompt: v.optional(v.string()),
1312
title: v.optional(v.string()),
@@ -17,7 +16,9 @@ export const schema = defineSchema({
1716
// the parent thread(s). There are multiple if the thread is a merging of
1817
// multiple threads.
1918
parentThreadIds: v.optional(v.array(v.id("threads"))),
20-
}).index("userId_order", ["userId", "order"]),
19+
// DEPRECATED
20+
order: /*DEPRECATED*/ v.optional(v.number()),
21+
}).index("userId", ["userId"]),
2122
// TODO: text search on title/ summary
2223
messages: defineTable({
2324
id: v.optional(v.string()), // external id, e.g. from Vercel AI SDK

0 commit comments

Comments
 (0)