@convex-dev/agent 0.1.1-alpha.0 → 0.1.2-alpha.0

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.
Files changed (131) hide show
  1. package/README.md +9 -3
  2. package/dist/commonjs/client/createTool.d.ts +49 -0
  3. package/dist/commonjs/client/createTool.d.ts.map +1 -0
  4. package/dist/commonjs/client/createTool.js +45 -0
  5. package/dist/commonjs/client/createTool.js.map +1 -0
  6. package/dist/commonjs/client/index.d.ts +101 -322
  7. package/dist/commonjs/client/index.d.ts.map +1 -1
  8. package/dist/commonjs/client/index.js +142 -194
  9. package/dist/commonjs/client/index.js.map +1 -1
  10. package/dist/commonjs/client/streaming.d.ts +54 -0
  11. package/dist/commonjs/client/streaming.d.ts.map +1 -0
  12. package/dist/commonjs/client/streaming.js +121 -0
  13. package/dist/commonjs/client/streaming.js.map +1 -0
  14. package/dist/commonjs/client/types.d.ts +290 -2
  15. package/dist/commonjs/client/types.d.ts.map +1 -1
  16. package/dist/commonjs/component/messages.d.ts +109 -143
  17. package/dist/commonjs/component/messages.d.ts.map +1 -1
  18. package/dist/commonjs/component/messages.js +15 -14
  19. package/dist/commonjs/component/messages.js.map +1 -1
  20. package/dist/commonjs/component/schema.d.ts +1026 -52
  21. package/dist/commonjs/component/schema.d.ts.map +1 -1
  22. package/dist/commonjs/component/schema.js +39 -1
  23. package/dist/commonjs/component/schema.js.map +1 -1
  24. package/dist/commonjs/component/streams.d.ts +191 -0
  25. package/dist/commonjs/component/streams.d.ts.map +1 -0
  26. package/dist/commonjs/component/streams.js +169 -0
  27. package/dist/commonjs/component/streams.js.map +1 -0
  28. package/dist/commonjs/component/threads.d.ts +1 -1
  29. package/dist/commonjs/component/threads.js +3 -3
  30. package/dist/commonjs/component/threads.js.map +1 -1
  31. package/dist/commonjs/component/users.js +2 -2
  32. package/dist/commonjs/component/users.js.map +1 -1
  33. package/dist/commonjs/component/vector/index.d.ts +8 -8
  34. package/dist/commonjs/component/vector/index.d.ts.map +1 -1
  35. package/dist/commonjs/component/vector/tables.d.ts +5 -5
  36. package/dist/commonjs/component/vector/tables.d.ts.map +1 -1
  37. package/dist/commonjs/component/vector/tables.js +1 -1
  38. package/dist/commonjs/component/vector/tables.js.map +1 -1
  39. package/dist/commonjs/mapping.d.ts.map +1 -1
  40. package/dist/commonjs/mapping.js +3 -1
  41. package/dist/commonjs/mapping.js.map +1 -1
  42. package/dist/commonjs/react/index.d.ts +99 -0
  43. package/dist/commonjs/react/index.d.ts.map +1 -1
  44. package/dist/commonjs/react/index.js +505 -1
  45. package/dist/commonjs/react/index.js.map +1 -1
  46. package/dist/commonjs/react/toUIMessages.d.ts +8 -0
  47. package/dist/commonjs/react/toUIMessages.d.ts.map +1 -0
  48. package/dist/commonjs/react/toUIMessages.js +134 -0
  49. package/dist/commonjs/react/toUIMessages.js.map +1 -0
  50. package/dist/commonjs/validators.d.ts +513 -11
  51. package/dist/commonjs/validators.d.ts.map +1 -1
  52. package/dist/commonjs/validators.js +49 -2
  53. package/dist/commonjs/validators.js.map +1 -1
  54. package/dist/esm/client/createTool.d.ts +49 -0
  55. package/dist/esm/client/createTool.d.ts.map +1 -0
  56. package/dist/esm/client/createTool.js +45 -0
  57. package/dist/esm/client/createTool.js.map +1 -0
  58. package/dist/esm/client/index.d.ts +101 -322
  59. package/dist/esm/client/index.d.ts.map +1 -1
  60. package/dist/esm/client/index.js +142 -194
  61. package/dist/esm/client/index.js.map +1 -1
  62. package/dist/esm/client/streaming.d.ts +54 -0
  63. package/dist/esm/client/streaming.d.ts.map +1 -0
  64. package/dist/esm/client/streaming.js +121 -0
  65. package/dist/esm/client/streaming.js.map +1 -0
  66. package/dist/esm/client/types.d.ts +290 -2
  67. package/dist/esm/client/types.d.ts.map +1 -1
  68. package/dist/esm/component/messages.d.ts +109 -143
  69. package/dist/esm/component/messages.d.ts.map +1 -1
  70. package/dist/esm/component/messages.js +15 -14
  71. package/dist/esm/component/messages.js.map +1 -1
  72. package/dist/esm/component/schema.d.ts +1026 -52
  73. package/dist/esm/component/schema.d.ts.map +1 -1
  74. package/dist/esm/component/schema.js +39 -1
  75. package/dist/esm/component/schema.js.map +1 -1
  76. package/dist/esm/component/streams.d.ts +191 -0
  77. package/dist/esm/component/streams.d.ts.map +1 -0
  78. package/dist/esm/component/streams.js +169 -0
  79. package/dist/esm/component/streams.js.map +1 -0
  80. package/dist/esm/component/threads.d.ts +1 -1
  81. package/dist/esm/component/threads.js +3 -3
  82. package/dist/esm/component/threads.js.map +1 -1
  83. package/dist/esm/component/users.js +2 -2
  84. package/dist/esm/component/users.js.map +1 -1
  85. package/dist/esm/component/vector/index.d.ts +8 -8
  86. package/dist/esm/component/vector/index.d.ts.map +1 -1
  87. package/dist/esm/component/vector/tables.d.ts +5 -5
  88. package/dist/esm/component/vector/tables.d.ts.map +1 -1
  89. package/dist/esm/component/vector/tables.js +1 -1
  90. package/dist/esm/component/vector/tables.js.map +1 -1
  91. package/dist/esm/mapping.d.ts.map +1 -1
  92. package/dist/esm/mapping.js +3 -1
  93. package/dist/esm/mapping.js.map +1 -1
  94. package/dist/esm/react/index.d.ts +99 -0
  95. package/dist/esm/react/index.d.ts.map +1 -1
  96. package/dist/esm/react/index.js +505 -1
  97. package/dist/esm/react/index.js.map +1 -1
  98. package/dist/esm/react/toUIMessages.d.ts +8 -0
  99. package/dist/esm/react/toUIMessages.d.ts.map +1 -0
  100. package/dist/esm/react/toUIMessages.js +134 -0
  101. package/dist/esm/react/toUIMessages.js.map +1 -0
  102. package/dist/esm/validators.d.ts +513 -11
  103. package/dist/esm/validators.d.ts.map +1 -1
  104. package/dist/esm/validators.js +49 -2
  105. package/dist/esm/validators.js.map +1 -1
  106. package/package.json +6 -2
  107. package/src/client/createTool.ts +108 -0
  108. package/src/client/index.ts +270 -689
  109. package/src/client/streaming.ts +166 -0
  110. package/src/client/types.ts +430 -5
  111. package/src/component/_generated/api.d.ts +367 -190
  112. package/src/component/messages.ts +15 -14
  113. package/src/component/schema.ts +46 -0
  114. package/src/component/streams.ts +206 -0
  115. package/src/component/threads.ts +3 -3
  116. package/src/component/users.ts +2 -2
  117. package/src/component/vector/tables.ts +1 -1
  118. package/src/mapping.ts +3 -1
  119. package/src/react/index.ts +687 -1
  120. package/src/react/toUIMessages.ts +153 -0
  121. package/src/validators.test.ts +18 -0
  122. package/src/validators.ts +72 -2
  123. package/dist/commonjs/deltaStreaming.d.ts +0 -2
  124. package/dist/commonjs/deltaStreaming.d.ts.map +0 -1
  125. package/dist/commonjs/deltaStreaming.js +0 -2
  126. package/dist/commonjs/deltaStreaming.js.map +0 -1
  127. package/dist/esm/deltaStreaming.d.ts +0 -2
  128. package/dist/esm/deltaStreaming.d.ts.map +0 -1
  129. package/dist/esm/deltaStreaming.js +0 -2
  130. package/dist/esm/deltaStreaming.js.map +0 -1
  131. package/src/deltaStreaming.ts +0 -1
package/README.md CHANGED
@@ -17,11 +17,17 @@ AI Agent framework built on Convex.
17
17
  Enables long-lived, durable workflows defined as code.
18
18
  - Reactive & realtime updates from asynchronous functions / workflows.
19
19
  - Support for streaming text and storing the final result.
20
+ See [examples/chat-streaming](./examples/chat-streaming/README.md).
20
21
  - Optionally filter tool calls out of the thread history.
21
22
 
22
23
  [Read the associated Stack post here](https://stack.convex.dev/ai-agents).
23
24
 
24
- Example usage:
25
+ Play with the [examples](./examples) by cloning this repo and running:
26
+ ```sh
27
+ npm run example
28
+ ```
29
+
30
+ ## Example usage:
25
31
 
26
32
  ```ts
27
33
  // Define an agent similarly to the AI SDK
@@ -128,8 +134,6 @@ const supportAgent = new Agent(components.agent, {
128
134
  // The default system prompt if not overriden.
129
135
  instructions: "You are a helpful assistant.",
130
136
  tools: {
131
- // Standard AI SDK tool
132
- myTool: tool({ description, parameters, execute: () => {}}),
133
137
  // Convex tool
134
138
  myConvexTool: createTool({
135
139
  description: "My Convex tool",
@@ -139,6 +143,8 @@ const supportAgent = new Agent(components.agent, {
139
143
  return "Hello, world!";
140
144
  },
141
145
  }),
146
+ // Standard AI SDK tool
147
+ myTool: tool({ description, parameters, execute: () => {}}),
142
148
  },
143
149
  // Embedding model to power vector search of message history (RAG).
144
150
  textEmbedding: openai.embedding("text-embedding-3-small"),
@@ -0,0 +1,49 @@
1
+ import { Schema, Tool } from "ai";
2
+ import { ToolExecutionOptions } from "ai";
3
+ import { ToolSet } from "ai";
4
+ import { z } from "zod";
5
+ import { RunActionCtx } from "./types.js";
6
+ import { Agent } from "./index.js";
7
+ export type ToolCtx<TOOLS extends ToolSet = ToolSet> = RunActionCtx & {
8
+ agent: Agent<TOOLS>;
9
+ userId?: string;
10
+ threadId?: string;
11
+ messageId?: string;
12
+ };
13
+ /**
14
+ * This is a wrapper around the ai.tool function that adds extra context to the
15
+ * tool call, including the action context, userId, threadId, and messageId.
16
+ * @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
17
+ * but swap parameters for args and handler for execute.
18
+ * @returns A tool to be used with the AI SDK.
19
+ */
20
+ export declare function createTool<PARAMETERS extends ToolParameters, RESULT>(t: {
21
+ /**
22
+ An optional description of what the tool does.
23
+ Will be used by the language model to decide whether to use the tool.
24
+ Not used for provider-defined tools.
25
+ */
26
+ description?: string;
27
+ /**
28
+ The schema of the input that the tool expects. The language model will use this to generate the input.
29
+ It is also used to validate the output of the language model.
30
+ Use descriptions to make the input understandable for the language model.
31
+ */
32
+ args: PARAMETERS;
33
+ /**
34
+ An async function that is called with the arguments from the tool call and produces a result.
35
+ If not provided, the tool will not be executed automatically.
36
+
37
+ @args is the input of the tool call.
38
+ @options.abortSignal is a signal that can be used to abort the tool call.
39
+ */
40
+ handler: (ctx: ToolCtx, args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
41
+ ctx?: ToolCtx;
42
+ }): Tool<PARAMETERS, RESULT> & {
43
+ execute: (args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
44
+ };
45
+ export declare function wrapTools(ctx: ToolCtx, ...toolSets: (ToolSet | undefined)[]): ToolSet;
46
+ type ToolParameters = z.ZodTypeAny | Schema<any>;
47
+ type inferParameters<PARAMETERS extends ToolParameters> = PARAMETERS extends Schema<any> ? PARAMETERS["_type"] : PARAMETERS extends z.ZodTypeAny ? z.infer<PARAMETERS> : never;
48
+ export {};
49
+ //# sourceMappingURL=createTool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTool.d.ts","sourceRoot":"","sources":["../../../src/client/createTool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAClC,OAAO,EAAE,oBAAoB,EAAE,MAAM,IAAI,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAC7B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,MAAM,MAAM,OAAO,CAAC,KAAK,SAAS,OAAO,GAAG,OAAO,IAAI,YAAY,GAAG;IACpE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,UAAU,SAAS,cAAc,EAAE,MAAM,EAAE,CAAC,EAAE;IACvE;;;;SAIK;IACL,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;SAIK;IACL,IAAI,EAAE,UAAU,CAAC;IACjB;;;;;;SAMK;IACL,OAAO,EAAE,CACP,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,eAAe,CAAC,UAAU,CAAC,EACjC,OAAO,EAAE,oBAAoB,KAC1B,WAAW,CAAC,MAAM,CAAC,CAAC;IACzB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG;IAC7B,OAAO,EAAE,CACP,IAAI,EAAE,eAAe,CAAC,UAAU,CAAC,EACjC,OAAO,EAAE,oBAAoB,KAC1B,WAAW,CAAC,MAAM,CAAC,CAAC;CAC1B,CAqBA;AAED,wBAAgB,SAAS,CACvB,GAAG,EAAE,OAAO,EACZ,GAAG,QAAQ,EAAE,CAAC,OAAO,GAAG,SAAS,CAAC,EAAE,GACnC,OAAO,CAiBT;AAID,KAAK,cAAc,GAAG,CAAC,CAAC,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;AACjD,KAAK,eAAe,CAAC,UAAU,SAAS,cAAc,IAEpD,UAAU,SAAS,MAAM,CAAC,GAAG,CAAC,GAC1B,UAAU,CAAC,OAAO,CAAC,GACnB,UAAU,SAAS,CAAC,CAAC,UAAU,GAC7B,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,GACnB,KAAK,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { tool } from "ai";
2
+ /**
3
+ * This is a wrapper around the ai.tool function that adds extra context to the
4
+ * tool call, including the action context, userId, threadId, and messageId.
5
+ * @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
6
+ * but swap parameters for args and handler for execute.
7
+ * @returns A tool to be used with the AI SDK.
8
+ */
9
+ export function createTool(t) {
10
+ const args = {
11
+ __acceptsCtx: true,
12
+ ctx: t.ctx,
13
+ description: t.description,
14
+ parameters: t.args,
15
+ async execute(args, options) {
16
+ if (!this.ctx) {
17
+ throw new Error("To use a Convex tool, you must either provide the ctx" +
18
+ " at definition time (dynamically in an action), or use the Agent to" +
19
+ " call it (which injects the ctx, userId and threadId)");
20
+ }
21
+ return t.handler(this.ctx, args, options);
22
+ },
23
+ };
24
+ return tool(args);
25
+ }
26
+ export function wrapTools(ctx, ...toolSets) {
27
+ const output = {};
28
+ for (const toolSet of toolSets) {
29
+ if (!toolSet) {
30
+ continue;
31
+ }
32
+ for (const [name, tool] of Object.entries(toolSet)) {
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ if (!tool.__acceptsCtx) {
35
+ output[name] = tool;
36
+ }
37
+ else {
38
+ const out = { ...tool, ctx };
39
+ output[name] = out;
40
+ }
41
+ }
42
+ }
43
+ return output;
44
+ }
45
+ //# sourceMappingURL=createTool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createTool.js","sourceRoot":"","sources":["../../../src/client/createTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAe1B;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAA4C,CA0BrE;IAMC,MAAM,IAAI,GAAG;QACX,YAAY,EAAE,IAAI;QAClB,GAAG,EAAE,CAAC,CAAC,GAAG;QACV,WAAW,EAAE,CAAC,CAAC,WAAW;QAC1B,UAAU,EAAE,CAAC,CAAC,IAAI;QAClB,KAAK,CAAC,OAAO,CACX,IAAiC,EACjC,OAA6B;YAE7B,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;gBACd,MAAM,IAAI,KAAK,CACb,uDAAuD;oBACrD,qEAAqE;oBACrE,uDAAuD,CAC1D,CAAC;YACJ,CAAC;YACD,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAC5C,CAAC;KACF,CAAC;IACF,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,SAAS,CACvB,GAAY,EACZ,GAAG,QAAiC;IAEpC,MAAM,MAAM,GAAG,EAAa,CAAC;IAC7B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,SAAS;QACX,CAAC;QACD,KAAK,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACnD,8DAA8D;YAC9D,IAAI,CAAE,IAAY,CAAC,YAAY,EAAE,CAAC;gBAChC,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YACtB,CAAC;iBAAM,CAAC;gBACN,MAAM,GAAG,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,CAAC;gBAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -1,105 +1,18 @@
1
1
  import type { EmbeddingModelV1, LanguageModelV1 } from "@ai-sdk/provider";
2
- import type { CoreMessage, DeepPartial, GenerateObjectResult, GenerateTextResult, JSONValue, RepairTextFunction, Schema, StepResult, StreamObjectResult, StreamTextResult, TelemetrySettings, Tool, ToolChoice, ToolExecutionOptions, ToolSet, UIMessage } from "ai";
3
- import { generateObject, generateText, streamObject, streamText } from "ai";
4
- import { z } from "zod";
5
- import { Mounts } from "../component/_generated/api.js";
2
+ import type { CoreMessage, DeepPartial, GenerateObjectResult, GenerateTextResult, StepResult, StreamObjectResult, StreamTextResult, ToolSet } from "ai";
3
+ import { PaginationOptions, PaginationResult } from "convex/server";
6
4
  import { type VectorDimension } from "../component/vector/tables.js";
7
5
  import { type AIMessageWithoutId } from "../mapping.js";
8
6
  import { extractText, isTool } from "../shared.js";
9
- import { type CallSettings, type MessageWithMetadata as InnerMessageWithMetadata, type ProviderMetadata, type ProviderOptions, type SearchOptions, type Usage } from "../validators.js";
10
- import type { OpaqueIds, RunActionCtx, RunMutationCtx, RunQueryCtx, UseApi } from "./types.js";
7
+ import { type MessageWithMetadata as InnerMessageWithMetadata, type MessageStatus, type ProviderMetadata, type SearchOptions, type StreamArgs, type Usage } from "../validators.js";
8
+ import type { AgentComponent, ContextOptions, GenerationOutputMetadata, OpaqueIds, Options, OurObjectArgs, OurStreamObjectArgs, RunActionCtx, RunMutationCtx, RunQueryCtx, StorageOptions, StreamingTextArgs, SyncStreamsReturnValue, TextArgs, Thread, UsageHandler } from "./types.js";
11
9
  import type { MessageDoc, ThreadDoc } from "../component/schema.js";
10
+ import { createTool } from "./createTool.js";
11
+ import { StreamingOptions } from "./streaming.js";
12
12
  export { vMessageDoc, vThreadDoc } from "../component/schema.js";
13
- export { extractText, isTool };
14
- export type { Usage, ProviderMetadata, MessageDoc, ThreadDoc };
15
- export {
16
- /** @deprecated Use vPaginationResult instead. */
17
- paginationResultValidator, paginationResultValidator as vPaginationResult, vContextOptions, vUsage, vProviderMetadata, vUserMessage, vAssistantMessage, vToolMessage, vStorageOptions, vSystemMessage, vMessage, } from "../validators.js";
18
- /**
19
- * Options to configure what messages are fetched as context,
20
- * automatically with thread.generateText, or directly via search.
21
- */
22
- export type ContextOptions = {
23
- /** @deprecated Use excludeToolMessages instead. */
24
- includeToolCalls?: boolean;
25
- /**
26
- * Whether to include tool messages in the context.
27
- * By default, tool calls and results are not included.
28
- */
29
- excludeToolMessages?: boolean;
30
- /**
31
- * How many recent messages to include. These are added after the search
32
- * messages, and do not count against the search limit.
33
- * Default: 100
34
- */
35
- recentMessages?: number;
36
- /**
37
- * Options for searching messages.
38
- */
39
- searchOptions?: {
40
- /**
41
- * The maximum number of messages to fetch. Default is 10.
42
- */
43
- limit: number;
44
- /**
45
- * Whether to use text search to find messages. Default is false.
46
- */
47
- textSearch?: boolean;
48
- /**
49
- * Whether to use vector search to find messages. Default is false.
50
- * At least one of textSearch or vectorSearch must be true.
51
- */
52
- vectorSearch?: boolean;
53
- /**
54
- * What messages around the search results to include.
55
- * Default: { before: 2, after: 1 }
56
- * (two before, and one after each message found in the search)
57
- * Note, this is after the limit is applied.
58
- * By default this will quadruple the number of messages fetched.
59
- */
60
- messageRange?: {
61
- before: number;
62
- after: number;
63
- };
64
- };
65
- /**
66
- * Whether to search across other threads for relevant messages.
67
- * By default, only the current thread is searched.
68
- */
69
- searchOtherThreads?: boolean;
70
- };
71
- /**
72
- * Options to configure the automatic saving of messages
73
- * when generating text / objects in a thread.
74
- */
75
- export type StorageOptions = {
76
- /**
77
- * Defaults to false, allowing you to pass in arbitrary context that will
78
- * be in addition to automatically fetched content.
79
- * Pass true to have all input messages saved to the thread history.
80
- */
81
- saveAllInputMessages?: boolean;
82
- /** Defaults to true, saving the prompt, or last message passed to generateText. */
83
- saveAnyInputMessages?: boolean;
84
- /** Defaults to true. Whether to save messages generated while chatting. */
85
- saveOutputMessages?: boolean;
86
- };
87
- export type GenerationOutputMetadata = {
88
- messageId?: string;
89
- };
90
- type CoreMessageMaybeWithId = CoreMessage & {
91
- id?: string | undefined;
92
- };
93
- export type UsageHandler = (ctx: RunActionCtx, args: {
94
- userId: string | undefined;
95
- threadId: string | undefined;
96
- agentName: string | undefined;
97
- usage: Usage;
98
- providerMetadata: ProviderMetadata | undefined;
99
- model: string;
100
- provider: string;
101
- }) => void | Promise<void>;
102
- export type AgentComponent = UseApi<Mounts>;
13
+ export { vAssistantMessage, vContextOptions, vMessage, vPaginationResult, vProviderMetadata, vStorageOptions, vStreamArgs, vSystemMessage, vToolMessage, vUsage, vUserMessage, } from "../validators.js";
14
+ export { createTool, extractText, isTool };
15
+ export type { AgentComponent, ContextOptions, MessageDoc, ProviderMetadata, StorageOptions, SyncStreamsReturnValue, Thread, ThreadDoc, Usage, UsageHandler, };
103
16
  export declare class Agent<AgentTools extends ToolSet> {
104
17
  component: AgentComponent;
105
18
  options: {
@@ -333,6 +246,34 @@ export declare class Agent<AgentTools extends ToolSet> {
333
246
  }): Promise<{
334
247
  thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
335
248
  }>;
249
+ /**
250
+ * List messages from a thread.
251
+ * @param ctx A ctx object from a query, mutation, or action.
252
+ * @param args.threadId The thread to list messages from.
253
+ * @param args.paginationOpts Pagination options (e.g. via usePaginatedQuery).
254
+ * @param args.excludeToolMessages Whether to exclude tool messages.
255
+ * False by default.
256
+ * @param args.statuses What statuses to include. All by default.
257
+ * @returns The MessageDoc's in a format compatible with usePaginatedQuery.
258
+ */
259
+ listMessages(ctx: RunQueryCtx, args: {
260
+ threadId: string;
261
+ paginationOpts: PaginationOptions;
262
+ excludeToolMessages?: boolean;
263
+ statuses?: MessageStatus[];
264
+ }): Promise<PaginationResult<MessageDoc>>;
265
+ /**
266
+ * A function that handles fetching stream deltas, used with the React hooks
267
+ * `useThreadMessages` or `useStreamingThreadMessages`.
268
+ * @param ctx A ctx object from a query, mutation, or action.
269
+ * @param args.threadId The thread to sync streams for.
270
+ * @param args.streamArgs The stream arguments with per-stream cursors.
271
+ * @returns The deltas for each stream from their existing cursor.
272
+ */
273
+ syncStreams(ctx: RunQueryCtx, args: {
274
+ threadId: string;
275
+ streamArgs: StreamArgs | undefined;
276
+ }): Promise<SyncStreamsReturnValue | undefined>;
336
277
  /**
337
278
  * Fetch the context messages for a thread.
338
279
  * @param ctx Either a query, mutation, or action ctx.
@@ -363,6 +304,19 @@ export declare class Agent<AgentTools extends ToolSet> {
363
304
  dimension: VectorDimension;
364
305
  model: string;
365
306
  } | undefined>;
307
+ saveMessage(ctx: RunMutationCtx, args: {
308
+ threadId: string;
309
+ userId?: string;
310
+ metadata?: Omit<MessageWithMetadata, "message">;
311
+ } & ({
312
+ prompt?: undefined;
313
+ message: CoreMessage;
314
+ } | {
315
+ prompt: string;
316
+ message?: undefined;
317
+ })): Promise<{
318
+ messageId: string;
319
+ }>;
366
320
  /**
367
321
  * Explicitly save messages associated with the thread (& user if provided)
368
322
  * @param ctx The ctx parameter to a mutation or action.
@@ -399,7 +353,7 @@ export declare class Agent<AgentTools extends ToolSet> {
399
353
  failPendingSteps?: boolean;
400
354
  }): Promise<{
401
355
  lastMessageId: string;
402
- messageIds: string[];
356
+ messages: MessageDoc[];
403
357
  }>;
404
358
  /**
405
359
  * Save messages to the thread.
@@ -566,7 +520,7 @@ export declare class Agent<AgentTools extends ToolSet> {
566
520
  * Defaults to the chat provider for the Agent.
567
521
  */
568
522
  provider?: string;
569
- }): Promise<void>;
523
+ }): Promise<MessageDoc[]>;
570
524
  /**
571
525
  * Commit or rollback a message that was pending.
572
526
  * This is done automatically when saving messages by default.
@@ -634,7 +588,19 @@ export declare class Agent<AgentTools extends ToolSet> {
634
588
  * The {@link ContextOptions} and {@link StorageOptions}
635
589
  * options to use for fetching contextual messages and saving input/output messages.
636
590
  */
637
- options?: Options): Promise<StreamTextResult<TOOLS extends undefined ? AgentTools : TOOLS, PARTIAL_OUTPUT> & GenerationOutputMetadata>;
591
+ options?: Options & {
592
+ /**
593
+ * Whether to save incremental data (deltas) from streaming responses.
594
+ * Defaults to false.
595
+ * If false, it will not save any deltas to the database.
596
+ * If true, it will save deltas with {@link DEFAULT_STREAMING_OPTIONS}.
597
+ *
598
+ * Regardless of this option, when streaming you are able to use this
599
+ * `streamText` function as you would with the "ai" package's version:
600
+ * iterating over the text, streaming it over HTTP, etc.
601
+ */
602
+ saveStreamDeltas?: boolean | StreamingOptions;
603
+ }): Promise<StreamTextResult<TOOLS extends undefined ? AgentTools : TOOLS, PARTIAL_OUTPUT> & GenerationOutputMetadata>;
638
604
  _saveMessagesAndFetchContext<T extends {
639
605
  id?: string;
640
606
  prompt?: string;
@@ -651,6 +617,8 @@ export declare class Agent<AgentTools extends ToolSet> {
651
617
  model: LanguageModelV1;
652
618
  };
653
619
  messageId: string | undefined;
620
+ order: number | undefined;
621
+ stepOrder: number | undefined;
654
622
  }>;
655
623
  /**
656
624
  * This behaves like {@link generateObject} from the "ai" package except that
@@ -708,8 +676,8 @@ export declare class Agent<AgentTools extends ToolSet> {
708
676
  result: GenerateObjectResult<unknown>;
709
677
  metadata?: Omit<MessageWithMetadata, "message">;
710
678
  }): Promise<void>;
711
- mergedContextOptions(opts: ContextOptions | undefined): ContextOptions;
712
- searchOptionsWithDefaults(contextOptions: ContextOptions, messages: CoreMessage[]): Promise<SearchOptions>;
679
+ _mergedContextOptions(opts: ContextOptions | undefined): ContextOptions;
680
+ _searchOptionsWithDefaults(contextOptions: ContextOptions, messages: CoreMessage[]): Promise<SearchOptions>;
713
681
  /**
714
682
  * Create a mutation that creates a thread so you can call it from a Workflow.
715
683
  * e.g.
@@ -742,9 +710,30 @@ export declare class Agent<AgentTools extends ToolSet> {
742
710
  * {@link ContextOptions}, {@link StorageOptions}, and maxSteps.
743
711
  */
744
712
  asTextAction(spec?: {
713
+ /**
714
+ * The maximum number of steps to take in this action.
715
+ * Defaults to the {@link Agent.maxSteps} option.
716
+ */
745
717
  maxSteps?: number;
718
+ /**
719
+ * The {@link ContextOptions} to use for fetching contextual messages and
720
+ * saving input/output messages.
721
+ * Defaults to the {@link Agent.contextOptions} option.
722
+ */
746
723
  contextOptions?: ContextOptions;
724
+ /**
725
+ * The {@link StorageOptions} to use for saving input/output messages.
726
+ * Defaults to the {@link Agent.storageOptions} option.
727
+ */
747
728
  storageOptions?: StorageOptions;
729
+ /**
730
+ * Whether to stream the text.
731
+ * If false, it will generate the text in a single call. (default)
732
+ * If true or {@link StreamingOptions}, it will stream the text from the LLM
733
+ * and save the chunks to the database with the options you specify, or the
734
+ * defaults if you pass true.
735
+ */
736
+ stream?: boolean | StreamingOptions;
748
737
  }): import("convex/server").RegisteredAction<"internal", {
749
738
  userId?: string | undefined;
750
739
  threadId?: string | undefined;
@@ -830,6 +819,7 @@ export declare class Agent<AgentTools extends ToolSet> {
830
819
  frequencyPenalty?: number | undefined;
831
820
  seed?: number | undefined;
832
821
  maxRetries?: number | undefined;
822
+ stream?: boolean | undefined;
833
823
  toolChoice?: "required" | "auto" | "none" | {
834
824
  type: "tool";
835
825
  toolName: string;
@@ -837,6 +827,7 @@ export declare class Agent<AgentTools extends ToolSet> {
837
827
  maxSteps?: number | undefined;
838
828
  experimental_continueSteps?: boolean | undefined;
839
829
  prompt?: string | undefined;
830
+ promptMessageId?: string | undefined;
840
831
  contextOptions?: {
841
832
  excludeToolMessages?: boolean | undefined;
842
833
  recentMessages?: number | undefined;
@@ -856,7 +847,11 @@ export declare class Agent<AgentTools extends ToolSet> {
856
847
  saveAnyInputMessages?: boolean | undefined;
857
848
  saveOutputMessages?: boolean | undefined;
858
849
  } | undefined;
859
- }, Promise<string>>;
850
+ }, Promise<{
851
+ text: string;
852
+ finishReason: import("@ai-sdk/provider").LanguageModelV1FinishReason;
853
+ messageId: string | undefined;
854
+ }>>;
860
855
  /**
861
856
  * Create an action that generates an object out of this agent so you can call
862
857
  * it from workflows or other actions without a wrapping function.
@@ -955,6 +950,7 @@ export declare class Agent<AgentTools extends ToolSet> {
955
950
  seed?: number | undefined;
956
951
  maxRetries?: number | undefined;
957
952
  prompt?: string | undefined;
953
+ promptMessageId?: string | undefined;
958
954
  contextOptions?: {
959
955
  excludeToolMessages?: boolean | undefined;
960
956
  recentMessages?: number | undefined;
@@ -1108,225 +1104,8 @@ export declare function filterOutOrphanedToolMessages(docs: MessageDoc[]): {
1108
1104
  tool: boolean;
1109
1105
  _id: string;
1110
1106
  }[];
1111
- export type ToolCtx = RunActionCtx & {
1112
- userId?: string;
1113
- threadId?: string;
1114
- messageId?: string;
1115
- };
1116
- type ToolParameters = z.ZodTypeAny | Schema<any>;
1117
- type inferParameters<PARAMETERS extends ToolParameters> = PARAMETERS extends Schema<any> ? PARAMETERS["_type"] : PARAMETERS extends z.ZodTypeAny ? z.infer<PARAMETERS> : never;
1118
- /**
1119
- * This is a wrapper around the ai.tool function that adds extra context to the
1120
- * tool call, including the action context, userId, threadId, and messageId.
1121
- * @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
1122
- * but swap parameters for args and handler for execute.
1123
- * @returns A tool to be used with the AI SDK.
1124
- */
1125
- export declare function createTool<PARAMETERS extends ToolParameters, RESULT>(t: {
1126
- /**
1127
- An optional description of what the tool does.
1128
- Will be used by the language model to decide whether to use the tool.
1129
- Not used for provider-defined tools.
1130
- */
1131
- description?: string;
1132
- /**
1133
- The schema of the input that the tool expects. The language model will use this to generate the input.
1134
- It is also used to validate the output of the language model.
1135
- Use descriptions to make the input understandable for the language model.
1136
- */
1137
- args: PARAMETERS;
1138
- /**
1139
- An async function that is called with the arguments from the tool call and produces a result.
1140
- If not provided, the tool will not be executed automatically.
1141
-
1142
- @args is the input of the tool call.
1143
- @options.abortSignal is a signal that can be used to abort the tool call.
1144
- */
1145
- handler: (ctx: ToolCtx, args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
1146
- ctx?: ToolCtx;
1147
- }): Tool<PARAMETERS, RESULT> & {
1148
- execute: (args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
1149
- };
1150
- type Options = {
1151
- /**
1152
- * The context options to use for passing in message history to the LLM.
1153
- */
1154
- contextOptions?: ContextOptions;
1155
- /**
1156
- * The storage options to use for saving the input and output messages to the thread.
1157
- */
1158
- storageOptions?: StorageOptions;
1159
- };
1160
- type TextArgs<AgentTools extends ToolSet, TOOLS extends ToolSet | undefined = undefined, OUTPUT = never, OUTPUT_PARTIAL = never> = Omit<Parameters<typeof generateText<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT, OUTPUT_PARTIAL>>[0], "toolChoice" | "tools" | "model"> & {
1161
- /**
1162
- * If provided, this message will be used as the "prompt" for the LLM call,
1163
- * instead of the prompt or messages.
1164
- * This is useful if you want to first save a user message, then use it as
1165
- * the prompt for the LLM call in another call.
1166
- */
1167
- promptMessageId?: string;
1168
- /**
1169
- * The model to use for the tool calls. This will override the model specified
1170
- * in the Agent constructor.
1171
- */
1172
- model?: LanguageModelV1;
1173
- /**
1174
- * The tools to use for the tool calls. This will override tools specified
1175
- * in the Agent constructor or createThread / continueThread.
1176
- */
1177
- tools?: TOOLS;
1178
- /**
1179
- * The tool choice to use for the tool calls. This must be one of the tools
1180
- * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
1181
- */
1182
- toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
1183
- };
1184
- type StreamingTextArgs<AgentTools extends ToolSet, TOOLS extends ToolSet | undefined = undefined, OUTPUT = never, OUTPUT_PARTIAL = never> = Omit<Parameters<typeof streamText<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT, OUTPUT_PARTIAL>>[0], "toolChoice" | "tools" | "model"> & {
1185
- /**
1186
- * The model to use for the tool calls. This will override the model specified
1187
- * in the Agent constructor.
1188
- */
1189
- model?: LanguageModelV1;
1190
- /**
1191
- * The tools to use for the tool calls. This will override tools specified
1192
- * in the Agent constructor or createThread / continueThread.
1193
- */
1194
- tools?: TOOLS;
1195
- /**
1196
- * The tool choice to use for the tool calls. This must be one of the tools
1197
- * specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
1198
- */
1199
- toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
1200
- };
1201
- type BaseGenerateObjectOptions = CallSettings & {
1202
- /**
1203
- * The model to use for the object generation. This will override the model
1204
- * specified in the Agent constructor.
1205
- */
1206
- model?: LanguageModelV1;
1207
- /**
1208
- * The system prompt to use for the object generation. This will override the
1209
- * system prompt specified in the Agent constructor.
1210
- */
1211
- system?: string;
1212
- /**
1213
- * The prompt to the LLM to use for the object generation.
1214
- * Specify this or messages, but not both.
1215
- */
1216
- prompt?: string;
1217
- /**
1218
- * The messages to use for the object generation.
1219
- * Note: recent messages are automatically added based on the thread it's
1220
- * associated with and your contextOptions.
1221
- */
1222
- messages?: CoreMessage[];
1223
- /**
1224
- * The message to use as the "prompt" for the object generation.
1225
- * If this is provided, it will be used instead of the prompt or messages.
1226
- * This is useful if you want to first save a user message, then use it as
1227
- * the prompt for the object generation in another call.
1228
- */
1229
- promptMessageId?: string;
1230
- experimental_repairText?: RepairTextFunction;
1231
- experimental_telemetry?: TelemetrySettings;
1232
- providerOptions?: ProviderOptions;
1233
- experimental_providerMetadata?: ProviderMetadata;
1234
- };
1235
- type GenerateObjectObjectOptions<T extends Record<string, unknown>> = BaseGenerateObjectOptions & {
1236
- output?: "object";
1237
- mode?: "auto" | "json" | "tool";
1238
- schema: z.Schema<T>;
1239
- schemaName?: string;
1240
- schemaDescription?: string;
1241
- };
1242
- type GenerateObjectArrayOptions<T> = BaseGenerateObjectOptions & {
1243
- output: "array";
1244
- mode?: "auto" | "json" | "tool";
1245
- schema: z.Schema<T>;
1246
- schemaName?: string;
1247
- schemaDescription?: string;
1248
- };
1249
- type GenerateObjectWithEnumOptions<T extends string> = BaseGenerateObjectOptions & {
1250
- output: "enum";
1251
- enum: Array<T>;
1252
- mode?: "auto" | "json" | "tool";
1253
- };
1254
- type GenerateObjectNoSchemaOptions = BaseGenerateObjectOptions & {
1255
- schema?: undefined;
1256
- mode?: "json";
1257
- };
1258
- type GenerateObjectArgs<T> = T extends Record<string, unknown> ? GenerateObjectObjectOptions<T> : T extends Array<unknown> ? GenerateObjectArrayOptions<T> : T extends string ? GenerateObjectWithEnumOptions<T> : GenerateObjectNoSchemaOptions;
1259
- type StreamObjectArgs<T> = T extends Record<string, unknown> ? GenerateObjectObjectOptions<T> : T extends Array<unknown> ? GenerateObjectArrayOptions<T> : GenerateObjectNoSchemaOptions;
1260
- type OurObjectArgs<T> = GenerateObjectArgs<T> & Pick<Parameters<typeof generateObject<any>>[0], "experimental_repairText" | "abortSignal">;
1261
- type OurStreamObjectArgs<T> = StreamObjectArgs<T> & Pick<Parameters<typeof streamObject<T>>[0], "onError" | "onFinish" | "abortSignal">;
1262
- type ThreadOutputMetadata = GenerationOutputMetadata & {
1263
- messageId: string;
1264
- };
1265
- /**
1266
- * The interface for a thread returned from {@link createThread} or {@link continueThread}.
1267
- * This is contextual to a thread and/or user.
1268
- */
1269
- interface Thread<DefaultTools extends ToolSet> {
1270
- /**
1271
- * The target threadId, from the startThread or continueThread initializers.
1272
- */
1273
- threadId: string;
1274
- /**
1275
- * This behaves like {@link generateText} from the "ai" package except that
1276
- * it add context based on the userId and threadId and saves the input and
1277
- * resulting messages to the thread, if specified.
1278
- * Use {@link continueThread} to get a version of this function already scoped
1279
- * to a thread (and optionally userId).
1280
- * @param args The arguments to the generateText function, along with extra controls
1281
- * for the {@link ContextOptions} and {@link StorageOptions}.
1282
- * @returns The result of the generateText function.
1283
- */
1284
- generateText<TOOLS extends ToolSet | undefined = undefined, OUTPUT = never, OUTPUT_PARTIAL = never>(args: TextArgs<TOOLS extends undefined ? DefaultTools : TOOLS, TOOLS, OUTPUT, OUTPUT_PARTIAL>, options?: Options): Promise<GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> & ThreadOutputMetadata>;
1285
- /**
1286
- * This behaves like {@link streamText} from the "ai" package except that
1287
- * it add context based on the userId and threadId and saves the input and
1288
- * resulting messages to the thread, if specified.
1289
- * Use {@link continueThread} to get a version of this function already scoped
1290
- * to a thread (and optionally userId).
1291
- * @param args The arguments to the streamText function, along with extra controls
1292
- * for the {@link ContextOptions} and {@link StorageOptions}.
1293
- * @returns The result of the streamText function.
1294
- */
1295
- streamText<TOOLS extends ToolSet | undefined = undefined, OUTPUT = never, PARTIAL_OUTPUT = never>(args: StreamingTextArgs<TOOLS extends undefined ? DefaultTools : TOOLS, TOOLS, OUTPUT, PARTIAL_OUTPUT>, options?: Options): Promise<StreamTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, PARTIAL_OUTPUT> & ThreadOutputMetadata>;
1296
- /**
1297
- * This behaves like {@link generateObject} from the "ai" package except that
1298
- * it add context based on the userId and threadId and saves the input and
1299
- * resulting messages to the thread, if specified. This overload is for objects, arrays, and enums.
1300
- * Use {@link continueThread} to get a version of this function already scoped
1301
- * to a thread (and optionally userId).
1302
- * @param args The arguments to the generateObject function, along with extra controls
1303
- * for the {@link ContextOptions} and {@link StorageOptions}.
1304
- * @returns The result of the generateObject function.
1305
- */
1306
- generateObject<T>(args: OurObjectArgs<T>, options?: Options): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
1307
- /**
1308
- * This behaves like {@link generateObject} from the "ai" package except that
1309
- * it add context based on the userId and threadId and saves the input and
1310
- * resulting messages to the thread, if specified. This overload is for when there's no schema.
1311
- * Use {@link continueThread} to get a version of this function already scoped
1312
- * to a thread (and optionally userId).
1313
- * @param args The arguments to the generateObject function, along with extra controls
1314
- * for the {@link ContextOptions} and {@link StorageOptions}.
1315
- * @returns The result of the generateObject function.
1316
- */
1317
- generateObject(args: GenerateObjectNoSchemaOptions, options?: Options): Promise<GenerateObjectResult<JSONValue> & ThreadOutputMetadata>;
1318
- /**
1319
- * This behaves like {@link streamObject} from the "ai" package except that
1320
- * it add context based on the userId and threadId and saves the input and
1321
- * resulting messages to the thread, if specified.
1322
- * Use {@link continueThread} to get a version of this function already scoped
1323
- * to a thread (and optionally userId).
1324
- * @param args The arguments to the streamObject function, along with extra controls
1325
- * for the {@link ContextOptions} and {@link StorageOptions}.
1326
- * @returns The result of the streamObject function.
1327
- */
1328
- streamObject<T>(args: OurStreamObjectArgs<T>, options?: Options): Promise<StreamObjectResult<DeepPartial<T>, T, never> & ThreadOutputMetadata>;
1329
- }
1330
1107
  type MessageWithMetadata = OpaqueIds<InnerMessageWithMetadata>;
1331
- export declare function toUIMessages(messages: MessageDoc[]): UIMessage[];
1108
+ type CoreMessageMaybeWithId = CoreMessage & {
1109
+ id?: string | undefined;
1110
+ };
1332
1111
  //# sourceMappingURL=index.d.ts.map