@convex-dev/agent 0.1.18-alpha.1 → 0.2.0-alpha.2
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/createTool.d.ts +31 -10
- package/dist/client/createTool.d.ts.map +1 -1
- package/dist/client/createTool.js +25 -10
- package/dist/client/createTool.js.map +1 -1
- package/dist/client/defaultComponent.d.ts +11 -0
- package/dist/client/defaultComponent.d.ts.map +1 -0
- package/dist/client/defaultComponent.js +7 -0
- package/dist/client/defaultComponent.js.map +1 -0
- package/dist/client/definePlaygroundAPI.d.ts +174 -199
- package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
- package/dist/client/definePlaygroundAPI.js +17 -35
- package/dist/client/definePlaygroundAPI.js.map +1 -1
- package/dist/client/files.d.ts +6 -3
- package/dist/client/files.d.ts.map +1 -1
- package/dist/client/files.js +7 -7
- package/dist/client/files.js.map +1 -1
- package/dist/client/index.d.ts +152 -589
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +138 -222
- package/dist/client/index.js.map +1 -1
- package/dist/client/messages.d.ts +538 -0
- package/dist/client/messages.d.ts.map +1 -0
- package/dist/client/messages.js +91 -0
- package/dist/client/messages.js.map +1 -0
- package/dist/client/search.d.ts +30 -7
- package/dist/client/search.d.ts.map +1 -1
- package/dist/client/search.js +20 -3
- package/dist/client/search.js.map +1 -1
- package/dist/client/streaming.d.ts +3 -2
- package/dist/client/streaming.d.ts.map +1 -1
- package/dist/client/streaming.js.map +1 -1
- package/dist/client/threads.d.ts +46 -0
- package/dist/client/threads.d.ts.map +1 -0
- package/dist/client/threads.js +49 -0
- package/dist/client/threads.js.map +1 -0
- package/dist/client/types.d.ts +39 -42
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +437 -75
- package/dist/component/messages.d.ts +254 -55
- package/dist/component/messages.d.ts.map +1 -1
- package/dist/component/messages.js +29 -25
- package/dist/component/messages.js.map +1 -1
- package/dist/component/schema.d.ts +1459 -158
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +8 -14
- package/dist/component/schema.js.map +1 -1
- package/dist/component/streams.d.ts +180 -6
- package/dist/component/streams.d.ts.map +1 -1
- package/dist/mapping.d.ts +11 -15
- package/dist/mapping.d.ts.map +1 -1
- package/dist/mapping.js +191 -61
- package/dist/mapping.js.map +1 -1
- package/dist/react/deltas.d.ts +0 -3
- package/dist/react/deltas.d.ts.map +1 -1
- package/dist/react/deltas.js +140 -44
- package/dist/react/deltas.js.map +1 -1
- package/dist/react/optimisticallySendMessage.d.ts.map +1 -1
- package/dist/react/optimisticallySendMessage.js +2 -1
- package/dist/react/optimisticallySendMessage.js.map +1 -1
- package/dist/react/toUIMessages.d.ts +5 -4
- package/dist/react/toUIMessages.d.ts.map +1 -1
- package/dist/react/toUIMessages.js +103 -40
- package/dist/react/toUIMessages.js.map +1 -1
- package/dist/validators.d.ts +1978 -1210
- package/dist/validators.d.ts.map +1 -1
- package/dist/validators.js +90 -54
- package/dist/validators.js.map +1 -1
- package/package.json +32 -28
- package/src/client/createTool.ts +69 -38
- package/src/client/defaultComponent.ts +17 -0
- package/src/client/definePlaygroundAPI.ts +29 -43
- package/src/client/files.ts +7 -8
- package/src/client/index.test.ts +20 -18
- package/src/client/index.ts +228 -434
- package/src/client/messages.ts +191 -0
- package/src/client/search.ts +30 -6
- package/src/client/streaming.ts +4 -3
- package/src/client/threads.ts +78 -0
- package/src/client/types.ts +50 -72
- package/src/component/_generated/api.d.ts +437 -75
- package/src/component/messages.test.ts +182 -40
- package/src/component/messages.ts +30 -32
- package/src/component/schema.ts +8 -14
- package/src/mapping.ts +230 -94
- package/src/react/deltas.ts +165 -52
- package/src/react/optimisticallySendMessage.ts +4 -1
- package/src/react/toUIMessages.test.ts +154 -36
- package/src/react/toUIMessages.ts +136 -57
- package/src/validators.test.ts +2 -101
- package/src/validators.ts +111 -68
- package/dist/client/listMessages.d.ts +0 -22
- package/dist/client/listMessages.d.ts.map +0 -1
- package/dist/client/listMessages.js +0 -25
- package/dist/client/listMessages.js.map +0 -1
- package/src/client/listMessages.ts +0 -38
- package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { ModelMessage } from "ai";
|
|
2
|
+
import type { PaginationOptions, PaginationResult } from "convex/server";
|
|
3
|
+
import type { MessageDoc } from "../component/schema.js";
|
|
4
|
+
import { validateVectorDimension } from "../component/vector/tables.js";
|
|
5
|
+
import {
|
|
6
|
+
vMessageWithMetadata,
|
|
7
|
+
type Message,
|
|
8
|
+
type MessageEmbeddings,
|
|
9
|
+
type MessageEmbeddingsWithDimension,
|
|
10
|
+
type MessageStatus,
|
|
11
|
+
type MessageWithMetadata,
|
|
12
|
+
} from "../validators.js";
|
|
13
|
+
import { serializeMessage } from "./index.js";
|
|
14
|
+
import type { AgentComponent, RunMutationCtx, RunQueryCtx } from "./types.js";
|
|
15
|
+
import { parse } from "convex-helpers/validators";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* List messages from a thread.
|
|
19
|
+
* @param ctx A ctx object from a query, mutation, or action.
|
|
20
|
+
* @param component The agent component, usually `components.agent`.
|
|
21
|
+
* @param args.threadId The thread to list messages from.
|
|
22
|
+
* @param args.paginationOpts Pagination options (e.g. via usePaginatedQuery).
|
|
23
|
+
* @param args.excludeToolMessages Whether to exclude tool messages.
|
|
24
|
+
* False by default.
|
|
25
|
+
* @param args.statuses What statuses to include. All by default.
|
|
26
|
+
* @returns The MessageDoc's in a format compatible with usePaginatedQuery.
|
|
27
|
+
*/
|
|
28
|
+
export async function listMessages(
|
|
29
|
+
ctx: RunQueryCtx,
|
|
30
|
+
component: AgentComponent,
|
|
31
|
+
args: {
|
|
32
|
+
threadId: string;
|
|
33
|
+
paginationOpts: PaginationOptions;
|
|
34
|
+
excludeToolMessages?: boolean;
|
|
35
|
+
statuses?: MessageStatus[];
|
|
36
|
+
},
|
|
37
|
+
): Promise<PaginationResult<MessageDoc>> {
|
|
38
|
+
if (args.paginationOpts.numItems === 0) {
|
|
39
|
+
return {
|
|
40
|
+
page: [],
|
|
41
|
+
isDone: true,
|
|
42
|
+
continueCursor: args.paginationOpts.cursor ?? "",
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return ctx.runQuery(component.messages.listMessagesByThreadId, {
|
|
46
|
+
order: "desc",
|
|
47
|
+
...args,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type SaveMessagesArgs = {
|
|
52
|
+
threadId: string;
|
|
53
|
+
userId?: string | null;
|
|
54
|
+
/**
|
|
55
|
+
* The message that these messages are in response to. They will be
|
|
56
|
+
* the same "order" as this message, at increasing stepOrder(s).
|
|
57
|
+
*/
|
|
58
|
+
promptMessageId?: string;
|
|
59
|
+
/**
|
|
60
|
+
* The messages to save.
|
|
61
|
+
*/
|
|
62
|
+
messages: (ModelMessage | Message)[];
|
|
63
|
+
/**
|
|
64
|
+
* Metadata to save with the messages. Each element corresponds to the
|
|
65
|
+
* message at the same index.
|
|
66
|
+
*/
|
|
67
|
+
metadata?: Omit<MessageWithMetadata, "message">[];
|
|
68
|
+
/**
|
|
69
|
+
* If true, it will fail any pending steps.
|
|
70
|
+
* Defaults to false.
|
|
71
|
+
*/
|
|
72
|
+
failPendingSteps?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* The embeddings to save with the messages.
|
|
75
|
+
*/
|
|
76
|
+
embeddings?: MessageEmbeddings;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Explicitly save messages associated with the thread (& user if provided)
|
|
81
|
+
*/
|
|
82
|
+
export async function saveMessages(
|
|
83
|
+
ctx: RunMutationCtx,
|
|
84
|
+
component: AgentComponent,
|
|
85
|
+
args: SaveMessagesArgs & {
|
|
86
|
+
/**
|
|
87
|
+
* The agent name to associate with the messages.
|
|
88
|
+
*/
|
|
89
|
+
agentName?: string;
|
|
90
|
+
},
|
|
91
|
+
) {
|
|
92
|
+
let embeddings: MessageEmbeddingsWithDimension | undefined;
|
|
93
|
+
if (args.embeddings) {
|
|
94
|
+
const dimension = args.embeddings.vectors.find((v) => v !== null)?.length;
|
|
95
|
+
if (dimension) {
|
|
96
|
+
validateVectorDimension(dimension);
|
|
97
|
+
embeddings = {
|
|
98
|
+
model: args.embeddings.model,
|
|
99
|
+
dimension,
|
|
100
|
+
vectors: args.embeddings.vectors,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const result = await ctx.runMutation(component.messages.addMessages, {
|
|
105
|
+
threadId: args.threadId,
|
|
106
|
+
userId: args.userId ?? undefined,
|
|
107
|
+
agentName: args.agentName,
|
|
108
|
+
promptMessageId: args.promptMessageId,
|
|
109
|
+
embeddings,
|
|
110
|
+
messages: await Promise.all(
|
|
111
|
+
args.messages.map(async (m, i) => {
|
|
112
|
+
const { message, fileIds } = await serializeMessage(ctx, component, m);
|
|
113
|
+
return parse(vMessageWithMetadata, {
|
|
114
|
+
...args.metadata?.[i],
|
|
115
|
+
message,
|
|
116
|
+
fileIds,
|
|
117
|
+
});
|
|
118
|
+
}),
|
|
119
|
+
),
|
|
120
|
+
failPendingSteps: args.failPendingSteps ?? false,
|
|
121
|
+
});
|
|
122
|
+
return { messages: result.messages };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type SaveMessageArgs = {
|
|
126
|
+
threadId: string;
|
|
127
|
+
userId?: string | null;
|
|
128
|
+
/**
|
|
129
|
+
* Metadata to save with the messages. Each element corresponds to the
|
|
130
|
+
* message at the same index.
|
|
131
|
+
*/
|
|
132
|
+
metadata?: Omit<MessageWithMetadata, "message">;
|
|
133
|
+
/**
|
|
134
|
+
* The embedding to save with the message.
|
|
135
|
+
*/
|
|
136
|
+
embedding?: { vector: number[]; model: string };
|
|
137
|
+
} & (
|
|
138
|
+
| {
|
|
139
|
+
prompt?: undefined;
|
|
140
|
+
/**
|
|
141
|
+
* The message to save.
|
|
142
|
+
*/
|
|
143
|
+
message: ModelMessage | Message;
|
|
144
|
+
}
|
|
145
|
+
| {
|
|
146
|
+
/*
|
|
147
|
+
* The prompt to save with the message.
|
|
148
|
+
*/
|
|
149
|
+
prompt: string;
|
|
150
|
+
message?: undefined;
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Save a message to the thread.
|
|
156
|
+
* @param ctx A ctx object from a mutation or action.
|
|
157
|
+
* @param args The message and what to associate it with (user / thread)
|
|
158
|
+
* You can pass extra metadata alongside the message, e.g. associated fileIds.
|
|
159
|
+
* @returns The messageId of the saved message.
|
|
160
|
+
*/
|
|
161
|
+
export async function saveMessage(
|
|
162
|
+
ctx: RunMutationCtx,
|
|
163
|
+
component: AgentComponent,
|
|
164
|
+
args: SaveMessageArgs & {
|
|
165
|
+
/**
|
|
166
|
+
* The agent name to associate with the message.
|
|
167
|
+
*/
|
|
168
|
+
agentName?: string;
|
|
169
|
+
},
|
|
170
|
+
) {
|
|
171
|
+
let embeddings: { vectors: number[][]; model: string } | undefined;
|
|
172
|
+
if (args.embedding && args.embedding.vector) {
|
|
173
|
+
embeddings = {
|
|
174
|
+
model: args.embedding.model,
|
|
175
|
+
vectors: [args.embedding.vector],
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const { messages } = await saveMessages(ctx, component, {
|
|
179
|
+
threadId: args.threadId,
|
|
180
|
+
userId: args.userId ?? undefined,
|
|
181
|
+
agentName: args.agentName,
|
|
182
|
+
messages:
|
|
183
|
+
args.prompt !== undefined
|
|
184
|
+
? [{ role: "user", content: args.prompt }]
|
|
185
|
+
: [args.message],
|
|
186
|
+
metadata: args.metadata ? [args.metadata] : undefined,
|
|
187
|
+
embeddings,
|
|
188
|
+
});
|
|
189
|
+
const message = messages.at(-1)!;
|
|
190
|
+
return { messageId: message._id, message };
|
|
191
|
+
}
|
package/src/client/search.ts
CHANGED
|
@@ -5,19 +5,20 @@ import type {
|
|
|
5
5
|
RunQueryCtx,
|
|
6
6
|
} from "./types.js";
|
|
7
7
|
import type { MessageDoc } from "../component/schema.js";
|
|
8
|
-
import type {
|
|
8
|
+
import type { EmbeddingModel, LanguageModel, ModelMessage } from "ai";
|
|
9
9
|
import { assert } from "convex-helpers";
|
|
10
10
|
import {
|
|
11
11
|
DEFAULT_MESSAGE_RANGE,
|
|
12
12
|
DEFAULT_RECENT_MESSAGES,
|
|
13
13
|
extractText,
|
|
14
14
|
} from "../shared.js";
|
|
15
|
+
import type { Message } from "../validators.js";
|
|
15
16
|
|
|
16
17
|
const DEFAULT_VECTOR_SCORE_THRESHOLD = 0.0;
|
|
17
18
|
|
|
18
19
|
export type GetEmbedding = (text: string) => Promise<{
|
|
19
20
|
embedding: number[];
|
|
20
|
-
embeddingModel: string
|
|
21
|
+
embeddingModel: string | EmbeddingModel<string>;
|
|
21
22
|
}>;
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -34,7 +35,7 @@ export async function fetchContextMessages(
|
|
|
34
35
|
args: {
|
|
35
36
|
userId: string | undefined;
|
|
36
37
|
threadId: string | undefined;
|
|
37
|
-
messages:
|
|
38
|
+
messages: (ModelMessage | Message)[];
|
|
38
39
|
/**
|
|
39
40
|
* If provided, it will search for messages up to and including this message.
|
|
40
41
|
* Note: if this is far in the past, text and vector search results may be more
|
|
@@ -102,13 +103,13 @@ export async function fetchContextMessages(
|
|
|
102
103
|
component.messages.searchMessages,
|
|
103
104
|
{
|
|
104
105
|
searchAllMessagesForUserId: opts?.searchOtherThreads
|
|
105
|
-
? args.userId ??
|
|
106
|
+
? (args.userId ??
|
|
106
107
|
(args.threadId &&
|
|
107
108
|
(
|
|
108
109
|
await ctx.runQuery(component.threads.getThread, {
|
|
109
110
|
threadId: args.threadId,
|
|
110
111
|
})
|
|
111
|
-
)?.userId)
|
|
112
|
+
)?.userId))
|
|
112
113
|
: undefined,
|
|
113
114
|
threadId: args.threadId,
|
|
114
115
|
beforeMessageId: args.upToAndIncludingMessageId,
|
|
@@ -122,7 +123,9 @@ export async function fetchContextMessages(
|
|
|
122
123
|
opts.searchOptions?.vectorScoreThreshold ??
|
|
123
124
|
DEFAULT_VECTOR_SCORE_THRESHOLD,
|
|
124
125
|
embedding: embeddingFields?.embedding,
|
|
125
|
-
embeddingModel: embeddingFields?.embeddingModel
|
|
126
|
+
embeddingModel: embeddingFields?.embeddingModel
|
|
127
|
+
? getModelName(embeddingFields.embeddingModel)
|
|
128
|
+
: undefined,
|
|
126
129
|
},
|
|
127
130
|
);
|
|
128
131
|
// TODO: track what messages we used for context
|
|
@@ -170,3 +173,24 @@ export function filterOutOrphanedToolMessages(docs: MessageDoc[]) {
|
|
|
170
173
|
}
|
|
171
174
|
return result;
|
|
172
175
|
}
|
|
176
|
+
|
|
177
|
+
export function getModelName(
|
|
178
|
+
embeddingModel: string | EmbeddingModel<string> | LanguageModel,
|
|
179
|
+
): string {
|
|
180
|
+
if (typeof embeddingModel === "string") {
|
|
181
|
+
if (embeddingModel.includes("/")) {
|
|
182
|
+
return embeddingModel.split("/").slice(1).join("/");
|
|
183
|
+
}
|
|
184
|
+
return embeddingModel;
|
|
185
|
+
}
|
|
186
|
+
return embeddingModel.modelId;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function getProviderName(
|
|
190
|
+
embeddingModel: string | EmbeddingModel<string> | LanguageModel,
|
|
191
|
+
): string {
|
|
192
|
+
if (typeof embeddingModel === "string") {
|
|
193
|
+
return embeddingModel.split("/").at(0)!;
|
|
194
|
+
}
|
|
195
|
+
return embeddingModel.provider;
|
|
196
|
+
}
|
package/src/client/streaming.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
StreamArgs,
|
|
10
10
|
StreamDelta,
|
|
11
11
|
StreamMessage,
|
|
12
|
-
|
|
12
|
+
vTextStreamPartV5,
|
|
13
13
|
} from "../validators.js";
|
|
14
14
|
import type { MessageDoc } from "../component/schema.js";
|
|
15
15
|
import type {
|
|
@@ -20,6 +20,7 @@ import type {
|
|
|
20
20
|
SyncStreamsReturnValue,
|
|
21
21
|
} from "./types.js";
|
|
22
22
|
import { omit } from "convex-helpers";
|
|
23
|
+
import type { Infer } from "convex/values";
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* A function that handles fetching stream deltas, used with the React hooks
|
|
@@ -158,7 +159,7 @@ export function mergeTransforms<TOOLS extends ToolSet>(
|
|
|
158
159
|
export class DeltaStreamer {
|
|
159
160
|
public streamId: string | undefined;
|
|
160
161
|
public readonly options: Required<StreamingOptions>;
|
|
161
|
-
#nextParts:
|
|
162
|
+
#nextParts: Infer<typeof vTextStreamPartV5>[] = [];
|
|
162
163
|
#nextOrder: number;
|
|
163
164
|
#nextStepOrder: number;
|
|
164
165
|
#latestWrite: number = 0;
|
|
@@ -205,7 +206,7 @@ export class DeltaStreamer {
|
|
|
205
206
|
});
|
|
206
207
|
}
|
|
207
208
|
}
|
|
208
|
-
public async addParts(parts:
|
|
209
|
+
public async addParts(parts: Infer<typeof vTextStreamPartV5>[]) {
|
|
209
210
|
if (this.abortController.signal.aborted) {
|
|
210
211
|
return;
|
|
211
212
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { WithoutSystemFields } from "convex/server";
|
|
2
|
+
import type { ThreadDoc } from "../component/schema.js";
|
|
3
|
+
import type { AgentComponent, RunMutationCtx, RunQueryCtx } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create a thread to store messages with an Agent.
|
|
7
|
+
* @param ctx The context from a mutation or action.
|
|
8
|
+
* @param component The Agent component, usually `components.agent`.
|
|
9
|
+
* @param args The associated thread metadata.
|
|
10
|
+
* @returns The id of the created thread.
|
|
11
|
+
*/
|
|
12
|
+
export async function createThread(
|
|
13
|
+
ctx: RunMutationCtx,
|
|
14
|
+
component: AgentComponent,
|
|
15
|
+
args?: { userId?: string | null; title?: string; summary?: string },
|
|
16
|
+
) {
|
|
17
|
+
const { _id: threadId } = await ctx.runMutation(
|
|
18
|
+
component.threads.createThread,
|
|
19
|
+
{
|
|
20
|
+
userId: args?.userId ?? undefined,
|
|
21
|
+
title: args?.title,
|
|
22
|
+
summary: args?.summary,
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
return threadId;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get the metadata for a thread.
|
|
30
|
+
* @param ctx A ctx object from a query, mutation, or action.
|
|
31
|
+
* @param args.threadId The thread to get the metadata for.
|
|
32
|
+
* @returns The metadata for the thread.
|
|
33
|
+
*/
|
|
34
|
+
export async function getThreadMetadata(
|
|
35
|
+
ctx: RunQueryCtx,
|
|
36
|
+
component: AgentComponent,
|
|
37
|
+
args: { threadId: string },
|
|
38
|
+
): Promise<ThreadDoc> {
|
|
39
|
+
const thread = await ctx.runQuery(component.threads.getThread, {
|
|
40
|
+
threadId: args.threadId,
|
|
41
|
+
});
|
|
42
|
+
if (!thread) {
|
|
43
|
+
throw new Error("Thread not found");
|
|
44
|
+
}
|
|
45
|
+
return thread;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function updateThreadMetadata(
|
|
49
|
+
ctx: RunMutationCtx,
|
|
50
|
+
component: AgentComponent,
|
|
51
|
+
args: { threadId: string; patch: Partial<WithoutSystemFields<ThreadDoc>> },
|
|
52
|
+
) {
|
|
53
|
+
return ctx.runMutation(component.threads.updateThread, {
|
|
54
|
+
threadId: args.threadId,
|
|
55
|
+
patch: args.patch,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Search for threads by title, paginated.
|
|
61
|
+
* @param ctx The context passed from the query/mutation/action.
|
|
62
|
+
* @returns The threads matching the search, paginated.
|
|
63
|
+
*/
|
|
64
|
+
export async function searchThreadTitles(
|
|
65
|
+
ctx: RunQueryCtx,
|
|
66
|
+
component: AgentComponent,
|
|
67
|
+
{
|
|
68
|
+
userId,
|
|
69
|
+
query,
|
|
70
|
+
limit,
|
|
71
|
+
}: { userId?: string | undefined; query: string; limit?: number },
|
|
72
|
+
): Promise<ThreadDoc[]> {
|
|
73
|
+
return ctx.runQuery(component.threads.searchThreadTitles, {
|
|
74
|
+
userId,
|
|
75
|
+
query,
|
|
76
|
+
limit: limit ?? 10,
|
|
77
|
+
});
|
|
78
|
+
}
|
package/src/client/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { LanguageModelV2 } from "@ai-sdk/provider";
|
|
1
2
|
import type {
|
|
2
|
-
|
|
3
|
+
ModelMessage,
|
|
3
4
|
DeepPartial,
|
|
4
5
|
generateObject,
|
|
5
6
|
GenerateObjectResult,
|
|
@@ -8,7 +9,6 @@ import type {
|
|
|
8
9
|
JSONValue,
|
|
9
10
|
LanguageModelRequestMetadata,
|
|
10
11
|
LanguageModelResponseMetadata,
|
|
11
|
-
LanguageModelV1,
|
|
12
12
|
RepairTextFunction,
|
|
13
13
|
streamObject,
|
|
14
14
|
StreamObjectResult,
|
|
@@ -17,6 +17,8 @@ import type {
|
|
|
17
17
|
TelemetrySettings,
|
|
18
18
|
ToolChoice,
|
|
19
19
|
ToolSet,
|
|
20
|
+
Schema,
|
|
21
|
+
LanguageModelUsage,
|
|
20
22
|
} from "ai";
|
|
21
23
|
import type {
|
|
22
24
|
Auth,
|
|
@@ -31,16 +33,15 @@ import type {
|
|
|
31
33
|
GenericDataModel,
|
|
32
34
|
} from "convex/server";
|
|
33
35
|
import type { GenericId } from "convex/values";
|
|
34
|
-
import type {
|
|
36
|
+
import type { z } from "zod/v3";
|
|
35
37
|
import type { Mounts } from "../component/_generated/api.js";
|
|
36
|
-
import type { ThreadDoc } from "../component/schema.js";
|
|
38
|
+
import type { MessageDoc, ThreadDoc } from "../component/schema.js";
|
|
37
39
|
import type {
|
|
38
40
|
CallSettings,
|
|
39
41
|
ProviderMetadata,
|
|
40
42
|
ProviderOptions,
|
|
41
43
|
StreamDelta,
|
|
42
44
|
StreamMessage,
|
|
43
|
-
Usage,
|
|
44
45
|
} from "../validators.js";
|
|
45
46
|
import type { StreamingOptions } from "./streaming.js";
|
|
46
47
|
|
|
@@ -114,7 +115,21 @@ export type StorageOptions = {
|
|
|
114
115
|
saveMessages?: "all" | "none" | "promptAndOutput";
|
|
115
116
|
};
|
|
116
117
|
|
|
117
|
-
export type GenerationOutputMetadata = {
|
|
118
|
+
export type GenerationOutputMetadata = {
|
|
119
|
+
/**
|
|
120
|
+
* The ID of the prompt message for the generation.
|
|
121
|
+
*/
|
|
122
|
+
messageId?: string;
|
|
123
|
+
/**
|
|
124
|
+
* The order of the prompt message for the generation.
|
|
125
|
+
*/
|
|
126
|
+
order?: number;
|
|
127
|
+
/**
|
|
128
|
+
* The messages saved for the generation - both saved input and output.
|
|
129
|
+
* If you passed promptMessageId, it will not include that message.
|
|
130
|
+
*/
|
|
131
|
+
messages?: MessageDoc[];
|
|
132
|
+
};
|
|
118
133
|
|
|
119
134
|
export type UsageHandler = (
|
|
120
135
|
ctx: RunActionCtx,
|
|
@@ -122,7 +137,7 @@ export type UsageHandler = (
|
|
|
122
137
|
userId: string | undefined;
|
|
123
138
|
threadId: string | undefined;
|
|
124
139
|
agentName: string | undefined;
|
|
125
|
-
usage:
|
|
140
|
+
usage: LanguageModelUsage;
|
|
126
141
|
// Often has more information, like cached token usage in the case of openai.
|
|
127
142
|
providerMetadata: ProviderMetadata | undefined;
|
|
128
143
|
model: string;
|
|
@@ -169,7 +184,7 @@ export type TextArgs<
|
|
|
169
184
|
* The model to use for the LLM calls. This will override the model specified
|
|
170
185
|
* in the Agent constructor.
|
|
171
186
|
*/
|
|
172
|
-
model?:
|
|
187
|
+
model?: LanguageModelV2;
|
|
173
188
|
/**
|
|
174
189
|
* The tools to use for the tool calls. This will override tools specified
|
|
175
190
|
* in the Agent constructor or createThread / continueThread.
|
|
@@ -208,7 +223,7 @@ export type StreamingTextArgs<
|
|
|
208
223
|
* The model to use for the tool calls. This will override the model specified
|
|
209
224
|
* in the Agent constructor.
|
|
210
225
|
*/
|
|
211
|
-
model?:
|
|
226
|
+
model?: LanguageModelV2;
|
|
212
227
|
/**
|
|
213
228
|
* The tools to use for the tool calls. This will override tools specified
|
|
214
229
|
* in the Agent constructor or createThread / continueThread.
|
|
@@ -226,7 +241,7 @@ type BaseGenerateObjectOptions = CallSettings & {
|
|
|
226
241
|
* The model to use for the object generation. This will override the model
|
|
227
242
|
* specified in the Agent constructor.
|
|
228
243
|
*/
|
|
229
|
-
model?:
|
|
244
|
+
model?: LanguageModelV2;
|
|
230
245
|
/**
|
|
231
246
|
* The system prompt to use for the object generation. This will override the
|
|
232
247
|
* system prompt specified in the Agent constructor.
|
|
@@ -236,13 +251,13 @@ type BaseGenerateObjectOptions = CallSettings & {
|
|
|
236
251
|
* The prompt to the LLM to use for the object generation.
|
|
237
252
|
* Specify this or messages, but not both.
|
|
238
253
|
*/
|
|
239
|
-
prompt?: string
|
|
254
|
+
prompt?: string | Array<ModelMessage>;
|
|
240
255
|
/**
|
|
241
256
|
* The messages to use for the object generation.
|
|
242
257
|
* Note: recent messages are automatically added based on the thread it's
|
|
243
258
|
* associated with and your contextOptions.
|
|
244
259
|
*/
|
|
245
|
-
messages?:
|
|
260
|
+
messages?: Array<ModelMessage>;
|
|
246
261
|
/**
|
|
247
262
|
* The message to use as the "prompt" for the object generation.
|
|
248
263
|
* If this is provided, it will be used instead of the prompt or messages.
|
|
@@ -256,52 +271,28 @@ type BaseGenerateObjectOptions = CallSettings & {
|
|
|
256
271
|
experimental_providerMetadata?: ProviderMetadata;
|
|
257
272
|
};
|
|
258
273
|
|
|
259
|
-
type
|
|
260
|
-
|
|
261
|
-
output?: "object";
|
|
262
|
-
mode?: "auto" | "json" | "tool";
|
|
263
|
-
schema: Schema<T>;
|
|
264
|
-
schemaName?: string;
|
|
265
|
-
schemaDescription?: string;
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
type GenerateObjectArrayOptions<T> = BaseGenerateObjectOptions & {
|
|
269
|
-
output: "array";
|
|
270
|
-
mode?: "auto" | "json" | "tool";
|
|
271
|
-
schema: Schema<T>;
|
|
274
|
+
type StandardGenerateObjectOptions<T> = {
|
|
275
|
+
schema: z.Schema<T>;
|
|
272
276
|
schemaName?: string;
|
|
273
277
|
schemaDescription?: string;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
type GenerateObjectWithEnumOptions<T extends string> =
|
|
277
|
-
BaseGenerateObjectOptions & {
|
|
278
|
-
output: "enum";
|
|
279
|
-
enum: Array<T>;
|
|
280
|
-
mode?: "auto" | "json" | "tool";
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
type GenerateObjectNoSchemaOptions = BaseGenerateObjectOptions & {
|
|
284
|
-
schema?: undefined;
|
|
285
|
-
mode?: "json";
|
|
278
|
+
output?: "object" | "array";
|
|
279
|
+
mode?: "auto" | "json" | "tool";
|
|
286
280
|
};
|
|
287
281
|
|
|
288
282
|
// TODO: simplify this to just use the generateObject args, with an optional
|
|
289
283
|
// model and tool/toolChoice types
|
|
290
|
-
type GenerateObjectArgs<T> =
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
:
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
? GenerateObjectWithEnumOptions<T>
|
|
297
|
-
: GenerateObjectNoSchemaOptions;
|
|
284
|
+
type GenerateObjectArgs<T> = BaseGenerateObjectOptions &
|
|
285
|
+
(
|
|
286
|
+
| StandardGenerateObjectOptions<T>
|
|
287
|
+
| { output: "enum"; enum: Array<T>; mode?: "auto" | "json" | "tool" }
|
|
288
|
+
| { output: "any"; schema: undefined; mode: "json" }
|
|
289
|
+
);
|
|
298
290
|
|
|
299
|
-
type StreamObjectArgs<T> =
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
:
|
|
303
|
-
|
|
304
|
-
: GenerateObjectNoSchemaOptions;
|
|
291
|
+
type StreamObjectArgs<T> = BaseGenerateObjectOptions &
|
|
292
|
+
(
|
|
293
|
+
| StandardGenerateObjectOptions<T>
|
|
294
|
+
| { output: "any"; schema: undefined; mode: "json" }
|
|
295
|
+
);
|
|
305
296
|
|
|
306
297
|
export type OurObjectArgs<T> = GenerateObjectArgs<T> &
|
|
307
298
|
Pick<
|
|
@@ -310,11 +301,12 @@ export type OurObjectArgs<T> = GenerateObjectArgs<T> &
|
|
|
310
301
|
"experimental_repairText" | "abortSignal"
|
|
311
302
|
>;
|
|
312
303
|
|
|
313
|
-
export type OurStreamObjectArgs<T
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
304
|
+
export type OurStreamObjectArgs<T extends Schema | z.Schema> =
|
|
305
|
+
StreamObjectArgs<T> &
|
|
306
|
+
Pick<
|
|
307
|
+
Parameters<typeof streamObject<T>>[0],
|
|
308
|
+
"onError" | "onFinish" | "abortSignal"
|
|
309
|
+
>;
|
|
318
310
|
|
|
319
311
|
type ThreadOutputMetadata = Required<GenerationOutputMetadata>;
|
|
320
312
|
|
|
@@ -415,24 +407,10 @@ export interface Thread<DefaultTools extends ToolSet> {
|
|
|
415
407
|
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
416
408
|
* @returns The result of the generateObject function.
|
|
417
409
|
*/
|
|
418
|
-
generateObject<T>(
|
|
410
|
+
generateObject<T = JSONValue>(
|
|
419
411
|
args: OurObjectArgs<T>,
|
|
420
412
|
options?: Options,
|
|
421
413
|
): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
|
|
422
|
-
/**
|
|
423
|
-
* This behaves like {@link generateObject} from the "ai" package except that
|
|
424
|
-
* it add context based on the userId and threadId and saves the input and
|
|
425
|
-
* resulting messages to the thread, if specified. This overload is for when there's no schema.
|
|
426
|
-
* Use {@link continueThread} to get a version of this function already scoped
|
|
427
|
-
* to a thread (and optionally userId).
|
|
428
|
-
* @param args The arguments to the generateObject function, along with extra controls
|
|
429
|
-
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
430
|
-
* @returns The result of the generateObject function.
|
|
431
|
-
*/
|
|
432
|
-
generateObject(
|
|
433
|
-
args: GenerateObjectNoSchemaOptions,
|
|
434
|
-
options?: Options,
|
|
435
|
-
): Promise<GenerateObjectResult<JSONValue> & ThreadOutputMetadata>;
|
|
436
414
|
/**
|
|
437
415
|
* This behaves like {@link streamObject} from the "ai" package except that
|
|
438
416
|
* it add context based on the userId and threadId and saves the input and
|
|
@@ -443,7 +421,7 @@ export interface Thread<DefaultTools extends ToolSet> {
|
|
|
443
421
|
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
444
422
|
* @returns The result of the streamObject function.
|
|
445
423
|
*/
|
|
446
|
-
streamObject<T>(
|
|
424
|
+
streamObject<T extends z.Schema | Schema>(
|
|
447
425
|
args: OurStreamObjectArgs<T>,
|
|
448
426
|
options?: Options,
|
|
449
427
|
): Promise<
|