@convex-dev/agent 0.0.11-alpha.0 → 0.0.11-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/README.md +10 -10
- package/dist/commonjs/client/index.d.ts +47 -36
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +17 -49
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/types.d.ts +0 -3
- package/dist/commonjs/client/types.d.ts.map +1 -1
- 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/esm/client/index.d.ts +47 -36
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +17 -49
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/types.d.ts +0 -3
- package/dist/esm/client/types.d.ts.map +1 -1
- 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/package.json +1 -1
- package/src/client/index.ts +79 -81
- package/src/client/types.ts +0 -3
- package/src/component/schema.ts +7 -2
package/src/client/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
GenerateTextResult,
|
|
7
7
|
JSONValue,
|
|
8
8
|
RepairTextFunction,
|
|
9
|
+
Schema,
|
|
9
10
|
StepResult,
|
|
10
11
|
StreamObjectResult,
|
|
11
12
|
StreamTextResult,
|
|
@@ -21,55 +22,60 @@ import {
|
|
|
21
22
|
jsonSchema,
|
|
22
23
|
streamObject,
|
|
23
24
|
streamText,
|
|
25
|
+
tool,
|
|
24
26
|
} from "ai";
|
|
25
27
|
import { assert } from "convex-helpers";
|
|
26
|
-
import {
|
|
27
|
-
ConvexToZod,
|
|
28
|
-
convexToZod,
|
|
29
|
-
zodToConvex,
|
|
30
|
-
} from "convex-helpers/server/zod";
|
|
31
28
|
import { internalActionGeneric } from "convex/server";
|
|
32
|
-
import { Infer, v
|
|
29
|
+
import { Infer, v } from "convex/values";
|
|
33
30
|
import { z } from "zod";
|
|
34
|
-
import { Mounts } from "../component/_generated/api";
|
|
31
|
+
import { Mounts } from "../component/_generated/api.js";
|
|
35
32
|
import {
|
|
36
33
|
validateVectorDimension,
|
|
37
|
-
VectorDimension,
|
|
38
|
-
} from "../component/vector/tables";
|
|
34
|
+
type VectorDimension,
|
|
35
|
+
} from "../component/vector/tables.js";
|
|
39
36
|
import {
|
|
40
|
-
AIMessageWithoutId,
|
|
37
|
+
type AIMessageWithoutId,
|
|
41
38
|
deserializeMessage,
|
|
42
39
|
promptOrMessagesToCoreMessages,
|
|
43
40
|
serializeMessageWithId,
|
|
44
41
|
serializeNewMessagesInStep,
|
|
45
42
|
serializeObjectResult,
|
|
46
43
|
serializeStep,
|
|
47
|
-
} from "../mapping";
|
|
44
|
+
} from "../mapping.js";
|
|
48
45
|
import {
|
|
49
46
|
DEFAULT_MESSAGE_RANGE,
|
|
50
47
|
DEFAULT_RECENT_MESSAGES,
|
|
51
48
|
extractText,
|
|
52
|
-
} from "../shared";
|
|
49
|
+
} from "../shared.js";
|
|
53
50
|
import {
|
|
54
|
-
CallSettings,
|
|
55
|
-
ProviderMetadata,
|
|
56
|
-
ProviderOptions,
|
|
57
|
-
SearchOptions,
|
|
51
|
+
type CallSettings,
|
|
52
|
+
type ProviderMetadata,
|
|
53
|
+
type ProviderOptions,
|
|
54
|
+
type SearchOptions,
|
|
58
55
|
vContextOptions,
|
|
59
56
|
vSafeObjectArgs,
|
|
60
57
|
vStorageOptions,
|
|
61
58
|
vTextArgs,
|
|
62
|
-
} from "../validators";
|
|
63
|
-
import {
|
|
64
|
-
|
|
59
|
+
} from "../validators.js";
|
|
60
|
+
import type {
|
|
61
|
+
OpaqueIds,
|
|
65
62
|
RunActionCtx,
|
|
66
63
|
RunMutationCtx,
|
|
67
64
|
RunQueryCtx,
|
|
68
65
|
UseApi,
|
|
69
66
|
} from "./types.js";
|
|
67
|
+
import schema from "../component/schema.js";
|
|
70
68
|
|
|
71
|
-
export
|
|
72
|
-
|
|
69
|
+
export type ThreadDoc = OpaqueIds<
|
|
70
|
+
{ _id: string; _creationTime: number } & Infer<
|
|
71
|
+
typeof schema.tables.threads.validator
|
|
72
|
+
>
|
|
73
|
+
>;
|
|
74
|
+
export type MessageDoc = OpaqueIds<
|
|
75
|
+
{ _id: string; _creationTime: number } & Infer<
|
|
76
|
+
typeof schema.tables.messages.validator
|
|
77
|
+
>
|
|
78
|
+
>;
|
|
73
79
|
|
|
74
80
|
/**
|
|
75
81
|
* Options to configure what messages are fetched as context,
|
|
@@ -179,8 +185,6 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
179
185
|
* They can be AI SDK tools (import {tool} from "ai")
|
|
180
186
|
* or tools that have Convex context
|
|
181
187
|
* (import { createTool } from "@convex-dev/agent")
|
|
182
|
-
* Note: Convex tools can't currently annotate the parameters
|
|
183
|
-
* with descriptions, so the names should be self-evident from naming.
|
|
184
188
|
*/
|
|
185
189
|
tools?: AgentTools;
|
|
186
190
|
/**
|
|
@@ -1096,45 +1100,6 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1096
1100
|
},
|
|
1097
1101
|
});
|
|
1098
1102
|
}
|
|
1099
|
-
|
|
1100
|
-
/**
|
|
1101
|
-
* Create a tool out of this agent so other agents can call this one.
|
|
1102
|
-
* Create a tool out of this agent so other agents can call this one.
|
|
1103
|
-
* @param spec The specification for the arguments to this agent.
|
|
1104
|
-
* They will be encoded as JSON and passed to the agent.
|
|
1105
|
-
* @returns The agent as a tool that can be passed to other agents.
|
|
1106
|
-
*/
|
|
1107
|
-
asTool(spec: {
|
|
1108
|
-
description: string;
|
|
1109
|
-
args: Validator<unknown, "required", string>;
|
|
1110
|
-
contextOptions?: ContextOptions;
|
|
1111
|
-
maxSteps?: number;
|
|
1112
|
-
provideMessageHistory?: boolean;
|
|
1113
|
-
}) {
|
|
1114
|
-
return createTool({
|
|
1115
|
-
...spec,
|
|
1116
|
-
handler: async (ctx, args, options) => {
|
|
1117
|
-
const maxSteps = spec.maxSteps ?? this.options.maxSteps;
|
|
1118
|
-
const contextOptions =
|
|
1119
|
-
spec.contextOptions && this.mergedContextOptions(spec.contextOptions);
|
|
1120
|
-
const { thread } = await this.createThread(ctx, {
|
|
1121
|
-
parentThreadIds: ctx.threadId ? [ctx.threadId] : undefined,
|
|
1122
|
-
userId: ctx.userId,
|
|
1123
|
-
});
|
|
1124
|
-
const messages = spec.provideMessageHistory ? options.messages : [];
|
|
1125
|
-
messages.push({
|
|
1126
|
-
role: "user",
|
|
1127
|
-
content: JSON.stringify(args),
|
|
1128
|
-
});
|
|
1129
|
-
const value = await thread.generateText({
|
|
1130
|
-
messages,
|
|
1131
|
-
maxSteps,
|
|
1132
|
-
...contextOptions,
|
|
1133
|
-
});
|
|
1134
|
-
return value.text;
|
|
1135
|
-
},
|
|
1136
|
-
});
|
|
1137
|
-
}
|
|
1138
1103
|
}
|
|
1139
1104
|
|
|
1140
1105
|
export type ToolCtx = RunActionCtx & {
|
|
@@ -1143,32 +1108,65 @@ export type ToolCtx = RunActionCtx & {
|
|
|
1143
1108
|
messageId?: string;
|
|
1144
1109
|
};
|
|
1145
1110
|
|
|
1111
|
+
// Vendoring in from "ai" package since it wasn't exported
|
|
1112
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1113
|
+
type ToolParameters = z.ZodTypeAny | Schema<any>;
|
|
1114
|
+
type inferParameters<PARAMETERS extends ToolParameters> =
|
|
1115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1116
|
+
PARAMETERS extends Schema<any>
|
|
1117
|
+
? PARAMETERS["_type"]
|
|
1118
|
+
: PARAMETERS extends z.ZodTypeAny
|
|
1119
|
+
? z.infer<PARAMETERS>
|
|
1120
|
+
: never;
|
|
1121
|
+
|
|
1146
1122
|
/**
|
|
1147
|
-
* This is a wrapper around the ai.tool function that adds
|
|
1148
|
-
*
|
|
1149
|
-
* @param tool The
|
|
1150
|
-
*
|
|
1123
|
+
* This is a wrapper around the ai.tool function that adds extra context to the
|
|
1124
|
+
* tool call, including the action context, userId, threadId, and messageId.
|
|
1125
|
+
* @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
|
|
1126
|
+
* but swap parameters for args and handler for execute.
|
|
1127
|
+
* @returns A tool to be used with the AI SDK.
|
|
1151
1128
|
*/
|
|
1152
|
-
export function createTool<
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1129
|
+
export function createTool<PARAMETERS extends ToolParameters, RESULT>(t: {
|
|
1130
|
+
/**
|
|
1131
|
+
An optional description of what the tool does.
|
|
1132
|
+
Will be used by the language model to decide whether to use the tool.
|
|
1133
|
+
Not used for provider-defined tools.
|
|
1134
|
+
*/
|
|
1158
1135
|
description?: string;
|
|
1136
|
+
/**
|
|
1137
|
+
The schema of the input that the tool expects. The language model will use this to generate the input.
|
|
1138
|
+
It is also used to validate the output of the language model.
|
|
1139
|
+
Use descriptions to make the input understandable for the language model.
|
|
1140
|
+
*/
|
|
1141
|
+
args: PARAMETERS;
|
|
1142
|
+
/**
|
|
1143
|
+
An async function that is called with the arguments from the tool call and produces a result.
|
|
1144
|
+
If not provided, the tool will not be executed automatically.
|
|
1145
|
+
|
|
1146
|
+
@args is the input of the tool call.
|
|
1147
|
+
@options.abortSignal is a signal that can be used to abort the tool call.
|
|
1148
|
+
*/
|
|
1159
1149
|
handler: (
|
|
1160
1150
|
ctx: ToolCtx,
|
|
1161
|
-
args:
|
|
1151
|
+
args: inferParameters<PARAMETERS>,
|
|
1162
1152
|
options: ToolExecutionOptions
|
|
1163
1153
|
) => PromiseLike<RESULT>;
|
|
1164
1154
|
ctx?: ToolCtx;
|
|
1165
|
-
}): Tool<
|
|
1166
|
-
|
|
1155
|
+
}): Tool<PARAMETERS, RESULT> & {
|
|
1156
|
+
execute: (
|
|
1157
|
+
args: inferParameters<PARAMETERS>,
|
|
1158
|
+
options: ToolExecutionOptions
|
|
1159
|
+
) => PromiseLike<RESULT>;
|
|
1160
|
+
} {
|
|
1161
|
+
const args = {
|
|
1167
1162
|
__acceptsCtx: true,
|
|
1168
|
-
ctx:
|
|
1169
|
-
description:
|
|
1170
|
-
parameters:
|
|
1171
|
-
async execute(
|
|
1163
|
+
ctx: t.ctx,
|
|
1164
|
+
description: t.description,
|
|
1165
|
+
parameters: t.args,
|
|
1166
|
+
async execute(
|
|
1167
|
+
args: inferParameters<PARAMETERS>,
|
|
1168
|
+
options: ToolExecutionOptions
|
|
1169
|
+
) {
|
|
1172
1170
|
if (!this.ctx) {
|
|
1173
1171
|
throw new Error(
|
|
1174
1172
|
"To use a Convex tool, you must either provide the ctx" +
|
|
@@ -1176,10 +1174,10 @@ export function createTool<
|
|
|
1176
1174
|
" call it (which injects the ctx, userId and threadId)"
|
|
1177
1175
|
);
|
|
1178
1176
|
}
|
|
1179
|
-
return
|
|
1177
|
+
return t.handler(this.ctx, args, options);
|
|
1180
1178
|
},
|
|
1181
1179
|
};
|
|
1182
|
-
return tool;
|
|
1180
|
+
return tool(args);
|
|
1183
1181
|
}
|
|
1184
1182
|
|
|
1185
1183
|
function wrapTools(
|
package/src/client/types.ts
CHANGED
|
@@ -7,10 +7,7 @@ import {
|
|
|
7
7
|
GenericActionCtx,
|
|
8
8
|
} from "convex/server";
|
|
9
9
|
import { GenericId } from "convex/values";
|
|
10
|
-
import type { Doc } from "../component/_generated/dataModel";
|
|
11
10
|
|
|
12
|
-
export type ThreadDoc = OpaqueIds<Doc<"threads">>;
|
|
13
|
-
export type MessageDoc = OpaqueIds<Doc<"messages">>;
|
|
14
11
|
|
|
15
12
|
/* Type utils follow */
|
|
16
13
|
export type RunQueryCtx = {
|
package/src/component/schema.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { defineSchema, defineTable } from "convex/server";
|
|
2
2
|
import { v } from "convex/values";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
vThreadStatus,
|
|
5
|
+
vMessage,
|
|
6
|
+
vMessageStatus,
|
|
7
|
+
vStep,
|
|
8
|
+
} from "../validators.js";
|
|
4
9
|
import { typedV } from "convex-helpers/validators";
|
|
5
|
-
import vectorTables, { vVectorId } from "./vector/tables";
|
|
10
|
+
import vectorTables, { vVectorId } from "./vector/tables.js";
|
|
6
11
|
|
|
7
12
|
export const schema = defineSchema({
|
|
8
13
|
threads: defineTable({
|