@meetsmore-oss/use-ai-server 1.16.0 → 1.17.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.
- package/dist/agents/AISDKAgent.d.ts +123 -144
- package/dist/agents/AISDKAgent.d.ts.map +1 -1
- package/dist/agents/index.d.ts +1 -1
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/testing/MockReasoningModel.d.ts +1 -1
- package/dist/agents/testing/MockReasoningModel.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +72 -55
- package/dist/plugins/FeedbackPlugin.d.ts +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/src/agents/AISDKAgent.d.ts +123 -144
- package/dist/src/agents/AISDKAgent.d.ts.map +1 -1
- package/dist/src/agents/index.d.ts +1 -1
- package/dist/src/agents/index.d.ts.map +1 -1
- package/dist/src/agents/testing/MockReasoningModel.d.ts +1 -1
- package/dist/src/agents/testing/MockReasoningModel.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/plugins/FeedbackPlugin.d.ts +1 -1
- package/dist/src/server.d.ts +2 -2
- package/dist/src/utils/toolFilters.d.ts +5 -5
- package/dist/test/integration-test-utils.d.ts +2 -1
- package/dist/test/integration-test-utils.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/toolFilters.d.ts +5 -5
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -144951,6 +144951,26 @@ var uiMessagesSchema = lazySchema(() => zodSchema(exports_external2.array(export
|
|
|
144951
144951
|
])).nonempty("Message must contain at least one part")
|
|
144952
144952
|
})).nonempty("Messages array must not be empty")));
|
|
144953
144953
|
var originalGenerateId3 = createIdGenerator({ prefix: "aiobj", size: 24 });
|
|
144954
|
+
function simulateReadableStream({
|
|
144955
|
+
chunks,
|
|
144956
|
+
initialDelayInMs = 0,
|
|
144957
|
+
chunkDelayInMs = 0,
|
|
144958
|
+
_internal
|
|
144959
|
+
}) {
|
|
144960
|
+
var _a152;
|
|
144961
|
+
const delay2 = (_a152 = _internal == null ? undefined : _internal.delay) != null ? _a152 : delay;
|
|
144962
|
+
let index = 0;
|
|
144963
|
+
return new ReadableStream({
|
|
144964
|
+
async pull(controller) {
|
|
144965
|
+
if (index < chunks.length) {
|
|
144966
|
+
await delay2(index === 0 ? initialDelayInMs : chunkDelayInMs);
|
|
144967
|
+
controller.enqueue(chunks[index++]);
|
|
144968
|
+
} else {
|
|
144969
|
+
controller.close();
|
|
144970
|
+
}
|
|
144971
|
+
}
|
|
144972
|
+
});
|
|
144973
|
+
}
|
|
144954
144974
|
var originalGenerateId4 = createIdGenerator({ prefix: "aiobj", size: 24 });
|
|
144955
144975
|
var name142 = "AI_NoSuchProviderError";
|
|
144956
144976
|
var marker142 = `vercel.ai.error.${name142}`;
|
|
@@ -145167,27 +145187,17 @@ function extractReasoningSignature(providerMetadata) {
|
|
|
145167
145187
|
}
|
|
145168
145188
|
|
|
145169
145189
|
class AISDKAgent {
|
|
145170
|
-
model;
|
|
145171
|
-
providerOptions;
|
|
145172
145190
|
name;
|
|
145173
145191
|
annotation;
|
|
145174
145192
|
toolFilter;
|
|
145175
|
-
systemPrompt;
|
|
145176
145193
|
cacheBreakpoint;
|
|
145177
|
-
|
|
145178
|
-
temperature;
|
|
145179
|
-
maxSteps;
|
|
145194
|
+
loadConfig;
|
|
145180
145195
|
constructor(config2) {
|
|
145181
|
-
this.model = config2.model;
|
|
145182
|
-
this.providerOptions = config2.providerOptions;
|
|
145183
145196
|
this.name = config2.name || "ai-sdk";
|
|
145184
145197
|
this.annotation = config2.annotation;
|
|
145185
145198
|
this.toolFilter = config2.toolFilter;
|
|
145186
|
-
this.systemPrompt = config2.systemPrompt;
|
|
145187
145199
|
this.cacheBreakpoint = config2.cacheBreakpoint;
|
|
145188
|
-
this.
|
|
145189
|
-
this.temperature = config2.temperature;
|
|
145190
|
-
this.maxSteps = config2.maxSteps ?? DEFAULT_MAX_STEPS;
|
|
145200
|
+
this.loadConfig = config2.hooks.loadConfig;
|
|
145191
145201
|
}
|
|
145192
145202
|
getName() {
|
|
145193
145203
|
return this.name;
|
|
@@ -145199,7 +145209,13 @@ class AISDKAgent {
|
|
|
145199
145209
|
await flushTelemetry();
|
|
145200
145210
|
}
|
|
145201
145211
|
async run(input, events) {
|
|
145202
|
-
|
|
145212
|
+
let runConfig;
|
|
145213
|
+
try {
|
|
145214
|
+
runConfig = await this.loadConfig();
|
|
145215
|
+
} catch (error40) {
|
|
145216
|
+
return this.handleConfigLoadError(error40, input, events);
|
|
145217
|
+
}
|
|
145218
|
+
const ctx = this.createRunContext(input, runConfig);
|
|
145203
145219
|
this.emitRunStartEvents(ctx, events);
|
|
145204
145220
|
const span = this.startTelemetrySpan(ctx);
|
|
145205
145221
|
try {
|
|
@@ -145210,12 +145226,12 @@ class AISDKAgent {
|
|
|
145210
145226
|
return this.handleRunError(error40, ctx, events, span);
|
|
145211
145227
|
}
|
|
145212
145228
|
}
|
|
145213
|
-
|
|
145229
|
+
createRunContext(input, runConfig) {
|
|
145214
145230
|
const { session, runId, messages, tools, state, systemPrompt: runtimeSystemPrompt, originalInput } = input;
|
|
145215
145231
|
if (session.tools.length === 0 && tools.length > 0) {
|
|
145216
145232
|
session.tools = tools;
|
|
145217
145233
|
}
|
|
145218
|
-
const configSystemPrompt =
|
|
145234
|
+
const configSystemPrompt = runConfig.systemPrompt || undefined;
|
|
145219
145235
|
const staticSystemMessages = this.buildStaticSystemMessages(configSystemPrompt, runtimeSystemPrompt);
|
|
145220
145236
|
const sanitizedInputMessages = this.sanitizeMessages(messages);
|
|
145221
145237
|
return {
|
|
@@ -145226,6 +145242,13 @@ class AISDKAgent {
|
|
|
145226
145242
|
state,
|
|
145227
145243
|
originalInput,
|
|
145228
145244
|
staticSystemMessages,
|
|
145245
|
+
resolved: {
|
|
145246
|
+
model: runConfig.model,
|
|
145247
|
+
temperature: runConfig.temperature,
|
|
145248
|
+
maxOutputTokens: runConfig.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS,
|
|
145249
|
+
maxSteps: runConfig.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
145250
|
+
providerOptions: runConfig.providerOptions
|
|
145251
|
+
},
|
|
145229
145252
|
streamTextStarted: false,
|
|
145230
145253
|
finalText: "",
|
|
145231
145254
|
currentStepNumber: 0,
|
|
@@ -145238,6 +145261,30 @@ class AISDKAgent {
|
|
|
145238
145261
|
pendingTextMessageId: null
|
|
145239
145262
|
};
|
|
145240
145263
|
}
|
|
145264
|
+
handleConfigLoadError(error40, input, events) {
|
|
145265
|
+
const errorMessage = error40 instanceof Error ? error40.message : String(error40);
|
|
145266
|
+
logger2.error("hooks.loadConfig failed; aborting run", {
|
|
145267
|
+
clientId: input.session.clientId,
|
|
145268
|
+
error: errorMessage
|
|
145269
|
+
});
|
|
145270
|
+
events.emit({
|
|
145271
|
+
type: EventType.RUN_STARTED,
|
|
145272
|
+
threadId: input.session.threadId,
|
|
145273
|
+
runId: input.runId,
|
|
145274
|
+
input: input.originalInput,
|
|
145275
|
+
timestamp: Date.now()
|
|
145276
|
+
});
|
|
145277
|
+
events.emit({
|
|
145278
|
+
type: EventType.RUN_ERROR,
|
|
145279
|
+
message: ErrorCode.UNKNOWN_ERROR,
|
|
145280
|
+
timestamp: Date.now()
|
|
145281
|
+
});
|
|
145282
|
+
return {
|
|
145283
|
+
success: false,
|
|
145284
|
+
error: errorMessage,
|
|
145285
|
+
conversationHistory: input.messages
|
|
145286
|
+
};
|
|
145287
|
+
}
|
|
145241
145288
|
emitRunStartEvents(ctx, events) {
|
|
145242
145289
|
events.emit({
|
|
145243
145290
|
type: EventType.RUN_STARTED,
|
|
@@ -145282,8 +145329,8 @@ class AISDKAgent {
|
|
|
145282
145329
|
});
|
|
145283
145330
|
}
|
|
145284
145331
|
async executeStepLoop(ctx, events, span) {
|
|
145285
|
-
for (let stepIteration = 0;stepIteration <=
|
|
145286
|
-
const isGracefulSummaryStep = stepIteration ===
|
|
145332
|
+
for (let stepIteration = 0;stepIteration <= ctx.resolved.maxSteps; stepIteration++) {
|
|
145333
|
+
const isGracefulSummaryStep = stepIteration === ctx.resolved.maxSteps;
|
|
145287
145334
|
if (isGracefulSummaryStep && !ctx.lastStepHadToolCalls)
|
|
145288
145335
|
break;
|
|
145289
145336
|
const isTruncationFallbackStep = ctx.truncationFallbackPending;
|
|
@@ -145324,18 +145371,18 @@ class AISDKAgent {
|
|
|
145324
145371
|
this.applyGracefulSummaryOverrides(isGracefulSummaryStep, stepConfig);
|
|
145325
145372
|
this.applyTruncationFallbackOverrides(isTruncationFallbackStep, stepConfig);
|
|
145326
145373
|
logger2.debug("Starting step iteration", { stepIteration, ...stepConfig.metadata });
|
|
145327
|
-
const messagesWithCache = applyCacheBreakpoints(stepConfig.messages, this.cacheBreakpoint,
|
|
145374
|
+
const messagesWithCache = applyCacheBreakpoints(stepConfig.messages, this.cacheBreakpoint, ctx.resolved.model);
|
|
145328
145375
|
ctx.streamTextStarted = true;
|
|
145329
|
-
const stepMaxOutputTokens = isTruncationFallbackStep ? Math.min(
|
|
145376
|
+
const stepMaxOutputTokens = isTruncationFallbackStep ? Math.min(ctx.resolved.maxOutputTokens, TRUNCATION_FALLBACK_MAX_TOKENS) : ctx.resolved.maxOutputTokens;
|
|
145330
145377
|
const createStream = () => streamText({
|
|
145331
|
-
model:
|
|
145378
|
+
model: ctx.resolved.model,
|
|
145332
145379
|
messages: messagesWithCache,
|
|
145333
145380
|
tools: stepConfig.tools,
|
|
145334
145381
|
stopWhen: stepCountIs(1),
|
|
145335
145382
|
maxOutputTokens: stepMaxOutputTokens,
|
|
145336
|
-
temperature:
|
|
145383
|
+
temperature: ctx.resolved.temperature,
|
|
145337
145384
|
abortSignal: ctx.session.abortController?.signal,
|
|
145338
|
-
providerOptions:
|
|
145385
|
+
providerOptions: ctx.resolved.providerOptions,
|
|
145339
145386
|
experimental_telemetry: span.active ? { isEnabled: true, functionId: "use-ai", metadata: stepConfig.metadata } : undefined,
|
|
145340
145387
|
onStepFinish: ({ usage, finishReason }) => {
|
|
145341
145388
|
logger2.debug("Step finished", { usage, finishReason, stepIteration });
|
|
@@ -145435,14 +145482,14 @@ class AISDKAgent {
|
|
|
145435
145482
|
ctx.currentMessages = [...ctx.currentMessages, fallbackMessage];
|
|
145436
145483
|
ctx.truncationFallbackPending = true;
|
|
145437
145484
|
logger2.warn("Output truncated mid-text/reasoning by maxOutputTokens; running fallback iteration", {
|
|
145438
|
-
maxOutputTokens:
|
|
145485
|
+
maxOutputTokens: ctx.resolved.maxOutputTokens,
|
|
145439
145486
|
appendingToMessageId: ctx.pendingTextMessageId ?? undefined
|
|
145440
145487
|
});
|
|
145441
145488
|
return true;
|
|
145442
145489
|
}
|
|
145443
145490
|
handleIncompleteToolCalls(ctx, stepCtx) {
|
|
145444
145491
|
const incompleteToolCalls = [...stepCtx.activeToolCalls.entries()].filter(([id]) => !stepCtx.completedToolCalls.has(id)).map(([id, call]) => ({ id, ...call }));
|
|
145445
|
-
const recoveryMessages = buildRecoveryToolResults(incompleteToolCalls, stepCtx.stepFinishReason,
|
|
145492
|
+
const recoveryMessages = buildRecoveryToolResults(incompleteToolCalls, stepCtx.stepFinishReason, ctx.resolved.maxOutputTokens);
|
|
145446
145493
|
if (recoveryMessages.length === 0) {
|
|
145447
145494
|
return false;
|
|
145448
145495
|
}
|
|
@@ -145775,16 +145822,6 @@ class AISDKAgent {
|
|
|
145775
145822
|
conversationHistory: ctx.messages
|
|
145776
145823
|
};
|
|
145777
145824
|
}
|
|
145778
|
-
async resolveSystemPrompt() {
|
|
145779
|
-
if (!this.systemPrompt) {
|
|
145780
|
-
return;
|
|
145781
|
-
}
|
|
145782
|
-
if (typeof this.systemPrompt === "string") {
|
|
145783
|
-
return this.systemPrompt;
|
|
145784
|
-
}
|
|
145785
|
-
const result = await this.systemPrompt();
|
|
145786
|
-
return result || undefined;
|
|
145787
|
-
}
|
|
145788
145825
|
buildStaticSystemMessages(configPrompt, runtimePrompt) {
|
|
145789
145826
|
const messages = [];
|
|
145790
145827
|
if (configPrompt) {
|
|
@@ -146064,26 +146101,6 @@ var MockLanguageModelV3 = class {
|
|
|
146064
146101
|
return this._supportedUrls();
|
|
146065
146102
|
}
|
|
146066
146103
|
};
|
|
146067
|
-
function simulateReadableStream({
|
|
146068
|
-
chunks,
|
|
146069
|
-
initialDelayInMs = 0,
|
|
146070
|
-
chunkDelayInMs = 0,
|
|
146071
|
-
_internal
|
|
146072
|
-
}) {
|
|
146073
|
-
const delay2 = _internal?.delay ?? delay;
|
|
146074
|
-
let index = 0;
|
|
146075
|
-
return new ReadableStream({
|
|
146076
|
-
async pull(controller) {
|
|
146077
|
-
if (index < chunks.length) {
|
|
146078
|
-
await delay2(index === 0 ? initialDelayInMs : chunkDelayInMs);
|
|
146079
|
-
controller.enqueue(chunks[index++]);
|
|
146080
|
-
} else {
|
|
146081
|
-
controller.close();
|
|
146082
|
-
}
|
|
146083
|
-
}
|
|
146084
|
-
});
|
|
146085
|
-
}
|
|
146086
|
-
var simulateReadableStream2 = simulateReadableStream;
|
|
146087
146104
|
|
|
146088
146105
|
// src/agents/testing/MockReasoningModel.ts
|
|
146089
146106
|
var CHUNK_DELAY_MS = 100;
|
|
@@ -146161,7 +146178,7 @@ function toolCallChunks(toolCallId, toolName, input) {
|
|
|
146161
146178
|
}
|
|
146162
146179
|
function makeStreamResponse(chunks, content) {
|
|
146163
146180
|
return {
|
|
146164
|
-
stream:
|
|
146181
|
+
stream: simulateReadableStream({ chunks, chunkDelayInMs: CHUNK_DELAY_MS }),
|
|
146165
146182
|
response: {
|
|
146166
146183
|
id: `mock-response-${Date.now()}`,
|
|
146167
146184
|
timestamp: new Date,
|
|
@@ -17,7 +17,7 @@ import type { ClientSession } from '../agents/types';
|
|
|
17
17
|
* import { FeedbackPlugin } from '@meetsmore-oss/use-ai-server';
|
|
18
18
|
*
|
|
19
19
|
* const server = new UseAIServer({
|
|
20
|
-
* agents: { claude: new AISDKAgent({ model }) },
|
|
20
|
+
* agents: { claude: new AISDKAgent({ hooks: { loadConfig: () => ({ model }) } }) },
|
|
21
21
|
* defaultAgent: 'claude',
|
|
22
22
|
* plugins: [
|
|
23
23
|
* new FeedbackPlugin(),
|
package/dist/server.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ export type { ClientSession } from './agents/types';
|
|
|
26
26
|
* apiKey: process.env.ANTHROPIC_API_KEY,
|
|
27
27
|
* });
|
|
28
28
|
* const claudeAgent = new AISDKAgent({
|
|
29
|
-
* model: anthropic('claude-sonnet-4-20250514'),
|
|
29
|
+
* hooks: { loadConfig: () => ({ model: anthropic('claude-sonnet-4-20250514') }) },
|
|
30
30
|
* });
|
|
31
31
|
* const server = new UseAIServer({
|
|
32
32
|
* port: 8081,
|
|
@@ -36,7 +36,7 @@ export type { ClientSession } from './agents/types';
|
|
|
36
36
|
*
|
|
37
37
|
* // Multiple agents (Claude + OpenAI)
|
|
38
38
|
* const gptAgent = new AISDKAgent({
|
|
39
|
-
* model: openai('gpt-4-turbo'),
|
|
39
|
+
* hooks: { loadConfig: () => ({ model: openai('gpt-4-turbo') }) },
|
|
40
40
|
* });
|
|
41
41
|
* const multiServer = new UseAIServer({
|
|
42
42
|
* port: 8081,
|
|
@@ -3,81 +3,141 @@ import type { Agent, AgentInput, EventEmitter, AgentResult } from './types';
|
|
|
3
3
|
import type { ToolDefinition } from '../types';
|
|
4
4
|
import { type CacheBreakpointFn } from './anthropicCache';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* The generation configuration for a single run(), returned by
|
|
7
|
+
* {@link AISDKAgentHooks.loadConfig}.
|
|
8
|
+
*
|
|
9
|
+
* Because `loadConfig` is called once per run, returning different values here
|
|
10
|
+
* changes the model / parameters for the next run without a server restart
|
|
11
|
+
* (e.g. values fetched from Langfuse prompt config).
|
|
12
|
+
*
|
|
13
|
+
* Only `model` is required. Omitted optional fields use library defaults.
|
|
7
14
|
*/
|
|
8
|
-
export interface
|
|
15
|
+
export interface AISDKRunConfig {
|
|
9
16
|
/**
|
|
10
|
-
* AI SDK Language Model (works with any provider).
|
|
17
|
+
* AI SDK Language Model (works with any provider). Accepts a model instance
|
|
18
|
+
* or a gateway model ID string.
|
|
19
|
+
*
|
|
20
|
+
* Required: use-ai is provider-agnostic and cannot pick a default model.
|
|
11
21
|
*
|
|
12
22
|
* @example
|
|
13
23
|
* ```typescript
|
|
14
24
|
* import { anthropic } from '@ai-sdk/anthropic';
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* // With Anthropic Claude
|
|
19
|
-
* { model: anthropic('claude-3-5-sonnet-20241022') }
|
|
20
|
-
*
|
|
21
|
-
* // With OpenAI GPT
|
|
22
|
-
* { model: openai('gpt-4-turbo') }
|
|
23
|
-
*
|
|
24
|
-
* // With Google Gemini
|
|
25
|
-
* { model: google('gemini-pro') }
|
|
25
|
+
* { model: anthropic('claude-3-5-sonnet-20241022') } // model instance
|
|
26
|
+
* { model: 'anthropic/claude-3-5-sonnet-20241022' } // gateway model ID
|
|
26
27
|
* ```
|
|
27
28
|
*/
|
|
28
29
|
model: LanguageModel;
|
|
29
30
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
31
|
+
* System prompt for this run, set on the backend and not exposed to the
|
|
32
|
+
* frontend (suitable for sensitive instructions). An empty string is treated
|
|
33
|
+
* as absent. When the request also carries a system prompt (AgentInput), the
|
|
34
|
+
* two are sent as separate system messages with this one first.
|
|
32
35
|
*/
|
|
33
|
-
|
|
36
|
+
systemPrompt?: string;
|
|
34
37
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
+
* Temperature for model responses. Lower (e.g. 0) is more deterministic.
|
|
39
|
+
* @default undefined (uses the model's default)
|
|
40
|
+
*/
|
|
41
|
+
temperature?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Maximum number of tokens the model can output per response.
|
|
44
|
+
* @default 4096
|
|
45
|
+
*/
|
|
46
|
+
maxOutputTokens?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Maximum number of model step iterations per run. Each iteration performs
|
|
49
|
+
* one model invocation and may include tool calls.
|
|
50
|
+
* @default 10
|
|
51
|
+
*/
|
|
52
|
+
maxSteps?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Provider-specific options passed directly to `streamText`.
|
|
55
|
+
* Used for AI Gateway features like model fallbacks and provider routing.
|
|
38
56
|
*
|
|
39
57
|
* @example
|
|
40
58
|
* ```typescript
|
|
41
|
-
* {
|
|
42
|
-
* {
|
|
59
|
+
* {
|
|
60
|
+
* providerOptions: {
|
|
61
|
+
* gateway: {
|
|
62
|
+
* models: ['anthropic/claude-opus-4.6', 'google/gemini-3.1-pro-preview'],
|
|
63
|
+
* order: ['azure', 'openai'],
|
|
64
|
+
* },
|
|
65
|
+
* },
|
|
66
|
+
* }
|
|
43
67
|
* ```
|
|
44
68
|
*/
|
|
45
|
-
|
|
69
|
+
providerOptions?: Record<string, Record<string, JSONValue>>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Function-valued configuration points for {@link AISDKAgent}.
|
|
73
|
+
*
|
|
74
|
+
* Anything that can change at runtime is supplied through a hook rather than a
|
|
75
|
+
* static config field, so there is a single source of truth and no "static
|
|
76
|
+
* value vs. override" ambiguity.
|
|
77
|
+
*/
|
|
78
|
+
export interface AISDKAgentHooks {
|
|
46
79
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
80
|
+
* Loads the generation config for a run.
|
|
81
|
+
*
|
|
82
|
+
* Called once at the start of every run(), so a value fetched from an external
|
|
83
|
+
* source (e.g. Langfuse prompt config) takes effect on the next request
|
|
84
|
+
* without a server restart. The agent does not cache the result.
|
|
50
85
|
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* be dynamically resolved (e.g., fetched from Langfuse or other external
|
|
54
|
-
* sources) so updates take effect immediately without server restart.
|
|
86
|
+
* Required, because it is the only source of the model. For a static setup,
|
|
87
|
+
* return a constant object.
|
|
55
88
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
89
|
+
* If it throws, the run fails with RUN_ERROR — there is no static fallback.
|
|
90
|
+
* Implement resilience (e.g. return a default on a Langfuse outage) inside the
|
|
91
|
+
* hook itself.
|
|
58
92
|
*
|
|
59
93
|
* @example
|
|
60
94
|
* ```typescript
|
|
61
|
-
* // Static
|
|
62
|
-
* {
|
|
63
|
-
* systemPrompt: 'You are a helpful assistant.'
|
|
64
|
-
* }
|
|
95
|
+
* // Static
|
|
96
|
+
* { loadConfig: () => ({ model: anthropic('claude-3-5-sonnet-20241022') }) }
|
|
65
97
|
*
|
|
66
|
-
* //
|
|
98
|
+
* // Dynamic, fetched from Langfuse (with a fallback owned by the host)
|
|
67
99
|
* {
|
|
68
|
-
*
|
|
100
|
+
* loadConfig: async () => {
|
|
101
|
+
* try {
|
|
102
|
+
* const prompt = await langfuse.prompt.get('my-prompt', { type: 'text' });
|
|
103
|
+
* return { model: prompt.config.model, systemPrompt: prompt.prompt };
|
|
104
|
+
* } catch {
|
|
105
|
+
* return { model: 'anthropic/claude-3-5-sonnet-20241022' };
|
|
106
|
+
* }
|
|
107
|
+
* },
|
|
69
108
|
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
loadConfig: () => AISDKRunConfig | Promise<AISDKRunConfig>;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Configuration for AISDKAgent.
|
|
115
|
+
*
|
|
116
|
+
* Structural settings (name, tool filtering, cache breakpoints) live here;
|
|
117
|
+
* everything that can vary per run is supplied via {@link AISDKAgentHooks}.
|
|
118
|
+
*/
|
|
119
|
+
export interface AISDKAgentConfig {
|
|
120
|
+
/**
|
|
121
|
+
* Function-valued configuration. `hooks.loadConfig` (required) supplies the
|
|
122
|
+
* model and generation parameters for each run.
|
|
123
|
+
*/
|
|
124
|
+
hooks: AISDKAgentHooks;
|
|
125
|
+
/**
|
|
126
|
+
* Agent name for identification (defaults to 'ai-sdk').
|
|
127
|
+
* Use this to differentiate multiple AI SDK agents.
|
|
128
|
+
*/
|
|
129
|
+
name?: string;
|
|
130
|
+
/**
|
|
131
|
+
* Optional annotation/description for the agent.
|
|
132
|
+
* Displayed in the use-ai agent selector UI to help users understand
|
|
133
|
+
* the agent's capabilities or purpose.
|
|
70
134
|
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* const prompt = await langfuse.getPrompt('my-prompt');
|
|
75
|
-
* return prompt.compile();
|
|
76
|
-
* }
|
|
77
|
-
* }
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* { annotation: 'Fast responses for simple tasks' }
|
|
78
138
|
* ```
|
|
79
139
|
*/
|
|
80
|
-
|
|
140
|
+
annotation?: string;
|
|
81
141
|
/**
|
|
82
142
|
* Optional filter function for tools.
|
|
83
143
|
* Use this to control which tools are available to this agent.
|
|
@@ -86,17 +146,7 @@ export interface AISDKAgentConfig {
|
|
|
86
146
|
* @example
|
|
87
147
|
* ```typescript
|
|
88
148
|
* // Only allow MCP tools starting with 'db_'
|
|
89
|
-
* {
|
|
90
|
-
* toolFilter: (tool) =>
|
|
91
|
-
* !tool._remote || tool.name.startsWith('db_')
|
|
92
|
-
* }
|
|
93
|
-
*
|
|
94
|
-
* // Block dangerous MCP tools
|
|
95
|
-
* {
|
|
96
|
-
* toolFilter: (tool) =>
|
|
97
|
-
* !tool._remote ||
|
|
98
|
-
* (!tool.name.includes('delete') && !tool.name.includes('drop'))
|
|
99
|
-
* }
|
|
149
|
+
* { toolFilter: (tool) => !tool._remote || tool.name.startsWith('db_') }
|
|
100
150
|
* ```
|
|
101
151
|
*/
|
|
102
152
|
toolFilter?: (tool: ToolDefinition) => boolean;
|
|
@@ -105,36 +155,16 @@ export interface AISDKAgentConfig {
|
|
|
105
155
|
* Only applies when using Anthropic models (Claude).
|
|
106
156
|
*
|
|
107
157
|
* Prompt caching reduces costs and latency by caching message prefixes.
|
|
108
|
-
* Cache breakpoints mark where the cacheable prefix ends.
|
|
109
|
-
*
|
|
110
158
|
* The function receives each message with positional context and returns
|
|
111
|
-
* true to add a cache breakpoint after that message.
|
|
112
|
-
*
|
|
113
|
-
* System prompt is included as role: 'system' at index 0 when present.
|
|
159
|
+
* true to add a cache breakpoint after that message. System prompt is
|
|
160
|
+
* included as role: 'system' at index 0 when present.
|
|
114
161
|
*
|
|
115
162
|
* @see https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
|
|
116
163
|
*
|
|
117
164
|
* @example
|
|
118
165
|
* ```typescript
|
|
119
166
|
* // Cache system prompt + last message (most common pattern)
|
|
120
|
-
* {
|
|
121
|
-
* cacheBreakpoint: (msg) => msg.role === 'system' || msg.isLast
|
|
122
|
-
* }
|
|
123
|
-
*
|
|
124
|
-
* // Cache only the last message
|
|
125
|
-
* {
|
|
126
|
-
* cacheBreakpoint: (msg) => msg.isLast
|
|
127
|
-
* }
|
|
128
|
-
*
|
|
129
|
-
* // Cache system prompt only
|
|
130
|
-
* {
|
|
131
|
-
* cacheBreakpoint: (msg) => msg.role === 'system'
|
|
132
|
-
* }
|
|
133
|
-
*
|
|
134
|
-
* // Cache first 3 messages + last
|
|
135
|
-
* {
|
|
136
|
-
* cacheBreakpoint: (msg) => msg.index < 3 || msg.isLast
|
|
137
|
-
* }
|
|
167
|
+
* { cacheBreakpoint: (msg) => msg.role === 'system' || msg.isLast }
|
|
138
168
|
*
|
|
139
169
|
* // System prompt with 1h TTL, last message with 5m TTL
|
|
140
170
|
* {
|
|
@@ -147,52 +177,6 @@ export interface AISDKAgentConfig {
|
|
|
147
177
|
* ```
|
|
148
178
|
*/
|
|
149
179
|
cacheBreakpoint?: CacheBreakpointFn;
|
|
150
|
-
/**
|
|
151
|
-
* Provider-specific options passed directly to `streamText`.
|
|
152
|
-
* Can be used for AI Gateway features like model fallbacks, provider routing, etc.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* // Model fallbacks via AI Gateway
|
|
157
|
-
* {
|
|
158
|
-
* providerOptions: {
|
|
159
|
-
* gateway: {
|
|
160
|
-
* models: ['anthropic/claude-opus-4.6', 'google/gemini-3.1-pro-preview'],
|
|
161
|
-
* },
|
|
162
|
-
* },
|
|
163
|
-
* }
|
|
164
|
-
*
|
|
165
|
-
* // Model fallbacks + provider routing
|
|
166
|
-
* {
|
|
167
|
-
* providerOptions: {
|
|
168
|
-
* gateway: {
|
|
169
|
-
* models: ['openai/gpt-5-nano', 'anthropic/claude-opus-4.6'],
|
|
170
|
-
* order: ['azure', 'openai'],
|
|
171
|
-
* },
|
|
172
|
-
* },
|
|
173
|
-
* }
|
|
174
|
-
* ```
|
|
175
|
-
*/
|
|
176
|
-
providerOptions?: Record<string, Record<string, JSONValue>>;
|
|
177
|
-
/**
|
|
178
|
-
* Maximum number of tokens the model can output per response.
|
|
179
|
-
* @default 4096
|
|
180
|
-
*/
|
|
181
|
-
maxOutputTokens?: number;
|
|
182
|
-
/**
|
|
183
|
-
* Temperature for model responses.
|
|
184
|
-
* Lower values (e.g., 0) make responses more deterministic.
|
|
185
|
-
* Higher values (e.g., 1) make responses more creative/random.
|
|
186
|
-
* Useful for testing where deterministic behavior is desired.
|
|
187
|
-
* @default undefined (uses model's default)
|
|
188
|
-
*/
|
|
189
|
-
temperature?: number;
|
|
190
|
-
/**
|
|
191
|
-
* Maximum number of model step iterations per run.
|
|
192
|
-
* Each iteration performs one model invocation and may include tool calls.
|
|
193
|
-
* @default 10
|
|
194
|
-
*/
|
|
195
|
-
maxSteps?: number;
|
|
196
180
|
}
|
|
197
181
|
/**
|
|
198
182
|
* Agent implementation for AI SDK models (Anthropic, OpenAI, Google, etc.).
|
|
@@ -226,12 +210,12 @@ export interface AISDKAgentConfig {
|
|
|
226
210
|
* // With Claude
|
|
227
211
|
* const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
|
|
228
212
|
* const claudeAgent = new AISDKAgent({
|
|
229
|
-
* model: anthropic('claude-3-5-sonnet-20241022'),
|
|
213
|
+
* hooks: { loadConfig: () => ({ model: anthropic('claude-3-5-sonnet-20241022') }) },
|
|
230
214
|
* });
|
|
231
215
|
*
|
|
232
216
|
* // With GPT-4
|
|
233
217
|
* const gptAgent = new AISDKAgent({
|
|
234
|
-
* model: openai('gpt-4-turbo'),
|
|
218
|
+
* hooks: { loadConfig: () => ({ model: openai('gpt-4-turbo') }) },
|
|
235
219
|
* });
|
|
236
220
|
*
|
|
237
221
|
* // Agent names come from agents object keys, not from agent config
|
|
@@ -245,16 +229,11 @@ export interface AISDKAgentConfig {
|
|
|
245
229
|
* ```
|
|
246
230
|
*/
|
|
247
231
|
export declare class AISDKAgent implements Agent {
|
|
248
|
-
private model;
|
|
249
|
-
private providerOptions?;
|
|
250
232
|
private name;
|
|
251
233
|
private annotation?;
|
|
252
234
|
private toolFilter?;
|
|
253
|
-
private systemPrompt?;
|
|
254
235
|
private cacheBreakpoint?;
|
|
255
|
-
private
|
|
256
|
-
private temperature?;
|
|
257
|
-
private maxSteps;
|
|
236
|
+
private loadConfig;
|
|
258
237
|
constructor(config: AISDKAgentConfig);
|
|
259
238
|
getName(): string;
|
|
260
239
|
getAnnotation(): string | undefined;
|
|
@@ -265,10 +244,17 @@ export declare class AISDKAgent implements Agent {
|
|
|
265
244
|
flushTelemetry(): Promise<void>;
|
|
266
245
|
run(input: AgentInput, events: EventEmitter): Promise<AgentResult>;
|
|
267
246
|
/**
|
|
268
|
-
* Creates the RunContext for a single run() invocation
|
|
269
|
-
*
|
|
247
|
+
* Creates the RunContext for a single run() invocation from the loaded run
|
|
248
|
+
* config. Builds the system messages and initializes all mutable state.
|
|
270
249
|
*/
|
|
271
250
|
private createRunContext;
|
|
251
|
+
/**
|
|
252
|
+
* Handles a failure of the `hooks.loadConfig` hook, which runs before the
|
|
253
|
+
* RunContext exists. Emits a well-formed lifecycle (RUN_STARTED then
|
|
254
|
+
* RUN_ERROR) built from the input so the client is not left hanging, and
|
|
255
|
+
* returns a failed AgentResult.
|
|
256
|
+
*/
|
|
257
|
+
private handleConfigLoadError;
|
|
272
258
|
/**
|
|
273
259
|
* Emits initial lifecycle events: RUN_STARTED, MESSAGES_SNAPSHOT, STATE_SNAPSHOT.
|
|
274
260
|
*/
|
|
@@ -359,18 +345,11 @@ export declare class AISDKAgent implements Agent {
|
|
|
359
345
|
* emits RUN_ERROR, and returns the AgentResult.
|
|
360
346
|
*/
|
|
361
347
|
private handleRunError;
|
|
362
|
-
/**
|
|
363
|
-
* Resolves the systemPrompt configuration value.
|
|
364
|
-
* Handles string, sync function, and async function cases.
|
|
365
|
-
*
|
|
366
|
-
* @returns The resolved system prompt string, or undefined if not configured or empty
|
|
367
|
-
*/
|
|
368
|
-
private resolveSystemPrompt;
|
|
369
348
|
/**
|
|
370
349
|
* Builds an array of static system messages from config and runtime prompts.
|
|
371
350
|
* These are built once per run and remain constant across steps (cacheable prefix).
|
|
372
351
|
*
|
|
373
|
-
* @param configPrompt - Resolved system prompt from
|
|
352
|
+
* @param configPrompt - Resolved system prompt from the run config (empty string already normalized to undefined)
|
|
374
353
|
* @param runtimePrompt - System prompt from AgentInput (static instructions from server)
|
|
375
354
|
* @returns Array of SystemModelMessage objects, or undefined if both are empty
|
|
376
355
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AISDKAgent.d.ts","sourceRoot":"","sources":["../../../src/agents/AISDKAgent.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,aAAa,EAA8G,KAAK,SAAS,EAAE,MAAM,IAAI,CAAC;AAMvL,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAiB,MAAM,SAAS,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAuB,MAAM,UAAU,CAAC;AAgCpE,OAAO,EAAyB,KAAK,iBAAiB,EAAE,MAAM,kBA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CAAC,cAAc;IA6DtB;;;;;;;OAOG;IACH,OAAO,CAAC,yBAAyB;IAgBjC;;;;;;OAMG;IACH,OAAO,CAAC,iBAAiB;IAQzB;;;;OAIG;IACH,OAAO,CAAC,6BAA6B;IAUrC,2EAA2E;IAC3E,OAAO,CAAC,gCAAgC;IASxC;;;OAGG;IACH,OAAO,CAAC,WAAW;IAsBnB;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;IAyD7B,OAAO,CAAC,mBAAmB;IAyC3B;;;;;OAKG;IACH;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,uBAAuB,CAmB3C;IAEJ;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAiBzC;IAEJ,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAG9B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAG/B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAI9B;IAEX;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,4BAA4B,CAWhD;IAEJ;;;;;;;;;;OAUG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,sBAAsB,CAW1C;IAEJ,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAQtC;IAEH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAM1B;IAEX,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,mBAAmB,CAAqC;IAEhF;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;CAgBzB"}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Provides pluggable AI agent backends that emit AG-UI protocol events.
|
|
4
4
|
*/
|
|
5
5
|
export type { Agent, AgentInput, EventEmitter, AgentResult, ClientSession } from './types';
|
|
6
|
-
export { AISDKAgent, type AISDKAgentConfig } from './AISDKAgent';
|
|
6
|
+
export { AISDKAgent, type AISDKAgentConfig, type AISDKAgentHooks, type AISDKRunConfig } from './AISDKAgent';
|
|
7
7
|
export { createMockReasoningModel } from './testing/MockReasoningModel';
|
|
8
8
|
export { applyCacheBreakpoints, isAnthropicModel, type MessageWithCacheContext, type CacheTtl, type CacheBreakpointResult, type CacheBreakpointFn, } from './anthropicCache';
|
|
9
9
|
export { toolNeedsApproval, createApprovalWrapper, waitForApproval, type ToolArguments, type ToolResult, type ToolExecutor, } from './toolApproval';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agents/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAC5G,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,QAAQ,EACb,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,GACvB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,iBAAiB,EACjB,qBAAqB,EACrB,eAAe,EACf,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,gBAAgB,CAAC"}
|