|
| 1 | +import { IdGenerator } from '@ai-sdk/provider-utils'; |
| 2 | +import { |
| 3 | + generateText, |
| 4 | + GenerateTextOnStepFinishCallback, |
| 5 | +} from '../../core/generate-text/generate-text'; |
| 6 | +import { Output } from '../../core/generate-text/output'; |
| 7 | +import { PrepareStepFunction } from '../../core/generate-text/prepare-step'; |
| 8 | +import { StopCondition } from '../../core/generate-text/stop-condition'; |
| 9 | +import { ToolCallRepairFunction } from '../../core/generate-text/tool-call-repair-function'; |
| 10 | +import { ToolSet } from '../../core/generate-text/tool-set'; |
| 11 | +import { CallSettings } from '../../core/prompt/call-settings'; |
| 12 | +import { TelemetrySettings } from '../../core/telemetry/telemetry-settings'; |
| 13 | +import { LanguageModel, ToolChoice } from '../../core/types/language-model'; |
| 14 | +import { streamText } from '../../core/generate-text/stream-text'; |
| 15 | +import { StreamTextResult } from '../../core/generate-text/stream-text-result'; |
| 16 | +import { Prompt } from '../../core/prompt/prompt'; |
| 17 | +import { ProviderMetadata } from '../../core/types/provider-metadata'; |
| 18 | +import { GenerateTextResult } from '../../core/generate-text/generate-text-result'; |
| 19 | + |
| 20 | +export type AgentSettings< |
| 21 | + TOOLS extends ToolSet, |
| 22 | + OUTPUT = never, |
| 23 | + OUTPUT_PARTIAL = never, |
| 24 | +> = CallSettings & { |
| 25 | + /** |
| 26 | +The language model to use. |
| 27 | + */ |
| 28 | + model: LanguageModel; |
| 29 | + |
| 30 | + /** |
| 31 | +The tools that the model can call. The model needs to support calling tools. |
| 32 | +*/ |
| 33 | + tools?: TOOLS; |
| 34 | + |
| 35 | + /** |
| 36 | +The tool choice strategy. Default: 'auto'. |
| 37 | + */ |
| 38 | + toolChoice?: ToolChoice<NoInfer<TOOLS>>; |
| 39 | + |
| 40 | + /** |
| 41 | +Condition for stopping the generation when there are tool results in the last step. |
| 42 | +When the condition is an array, any of the conditions can be met to stop the generation. |
| 43 | +
|
| 44 | +@default stepCountIs(1) |
| 45 | + */ |
| 46 | + stopWhen?: |
| 47 | + | StopCondition<NoInfer<TOOLS>> |
| 48 | + | Array<StopCondition<NoInfer<TOOLS>>>; |
| 49 | + |
| 50 | + /** |
| 51 | +Optional telemetry configuration (experimental). |
| 52 | + */ |
| 53 | + experimental_telemetry?: TelemetrySettings; |
| 54 | + |
| 55 | + /** |
| 56 | +Limits the tools that are available for the model to call without |
| 57 | +changing the tool call and result types in the result. |
| 58 | + */ |
| 59 | + activeTools?: Array<keyof NoInfer<TOOLS>>; |
| 60 | + |
| 61 | + /** |
| 62 | +Optional specification for parsing structured outputs from the LLM response. |
| 63 | + */ |
| 64 | + experimental_output?: Output<OUTPUT, OUTPUT_PARTIAL>; |
| 65 | + |
| 66 | + /** |
| 67 | + * @deprecated Use `prepareStep` instead. |
| 68 | + */ |
| 69 | + experimental_prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>; |
| 70 | + |
| 71 | + /** |
| 72 | +Optional function that you can use to provide different settings for a step. |
| 73 | + */ |
| 74 | + prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>; |
| 75 | + |
| 76 | + /** |
| 77 | +A function that attempts to repair a tool call that failed to parse. |
| 78 | + */ |
| 79 | + experimental_repairToolCall?: ToolCallRepairFunction<NoInfer<TOOLS>>; |
| 80 | + |
| 81 | + /** |
| 82 | + Callback that is called when each step (LLM call) is finished, including intermediate steps. |
| 83 | + */ |
| 84 | + onStepFinish?: GenerateTextOnStepFinishCallback<NoInfer<TOOLS>>; |
| 85 | + |
| 86 | + /** |
| 87 | + * Internal. For test use only. May change without notice. |
| 88 | + */ |
| 89 | + _internal?: { |
| 90 | + generateId?: IdGenerator; |
| 91 | + currentDate?: () => Date; |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +export class Agent< |
| 96 | + TOOLS extends ToolSet, |
| 97 | + OUTPUT = never, |
| 98 | + OUTPUT_PARTIAL = never, |
| 99 | +> { |
| 100 | + private readonly settings: AgentSettings<TOOLS, OUTPUT, OUTPUT_PARTIAL>; |
| 101 | + |
| 102 | + constructor(settings: AgentSettings<TOOLS, OUTPUT, OUTPUT_PARTIAL>) { |
| 103 | + this.settings = settings; |
| 104 | + } |
| 105 | + |
| 106 | + async generate( |
| 107 | + options: Prompt & { |
| 108 | + /** |
| 109 | +Additional provider-specific metadata. They are passed through |
| 110 | +from the provider to the AI SDK and enable provider-specific |
| 111 | +results that can be fully encapsulated in the provider. |
| 112 | + */ |
| 113 | + providerMetadata?: ProviderMetadata; |
| 114 | + }, |
| 115 | + ): Promise<GenerateTextResult<TOOLS, OUTPUT>> { |
| 116 | + return generateText({ ...this.settings, ...options }); |
| 117 | + } |
| 118 | + |
| 119 | + stream( |
| 120 | + options: Prompt & { |
| 121 | + /** |
| 122 | +Additional provider-specific metadata. They are passed through |
| 123 | +from the provider to the AI SDK and enable provider-specific |
| 124 | +results that can be fully encapsulated in the provider. |
| 125 | + */ |
| 126 | + providerMetadata?: ProviderMetadata; |
| 127 | + }, |
| 128 | + ): StreamTextResult<TOOLS, OUTPUT_PARTIAL> { |
| 129 | + return streamText({ ...this.settings, ...options }); |
| 130 | + } |
| 131 | +} |
0 commit comments