@mastra/server-adapters-test-suite 0.1.4-alpha.2 → 0.2.0-alpha.3
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/CHANGELOG.md +20 -0
- package/dist/durable-disconnect-test-suite.d.ts +2 -0
- package/dist/durable-disconnect-test-suite.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +700 -4
- package/dist/index.js.map +1 -1
- package/dist/structured-wake-test-suite.d.ts +11 -0
- package/dist/structured-wake-test-suite.d.ts.map +1 -0
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
|
|
2
2
|
import { registerApiRoute } from "@mastra/core/server";
|
|
3
3
|
import { handleError } from "@mastra/server/handlers/error";
|
|
4
|
-
import { HTTPException, SERVER_ROUTES, WorkflowRegistry, createRoute } from "@mastra/server/server-adapter";
|
|
4
|
+
import { HTTPException, SERVER_ROUTES, WorkflowRegistry, createRoute, serializeStreamChunk } from "@mastra/server/server-adapter";
|
|
5
5
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
6
|
import * as zod from "zod";
|
|
7
7
|
import { z } from "zod";
|
|
@@ -25,7 +25,7 @@ import { CompositeVoice } from "@mastra/core/voice";
|
|
|
25
25
|
import { createStep, createWorkflow } from "@mastra/core/workflows";
|
|
26
26
|
import { LocalFilesystem, Workspace } from "@mastra/core/workspace";
|
|
27
27
|
import { MCPClient, MCPServer, defineEvent } from "@mastra/mcp";
|
|
28
|
-
import { createDurableAgent, createEventedAgent } from "@mastra/core/agent/durable";
|
|
28
|
+
import { AGENT_STREAM_TOPIC, createDurableAgent, createDurableAgentStream, createEventedAgent } from "@mastra/core/agent/durable";
|
|
29
29
|
import { Mastra as Mastra$1 } from "@mastra/core/mastra";
|
|
30
30
|
import http from "http";
|
|
31
31
|
import { MCPServerBase } from "@mastra/core/mcp";
|
|
@@ -241,7 +241,7 @@ function generateValidDataFromSchema(schema, fieldName) {
|
|
|
241
241
|
if (typeName === "ZodNull") return null;
|
|
242
242
|
if (typeName === "ZodUndefined") return void 0;
|
|
243
243
|
if (typeName === "ZodDate") return /* @__PURE__ */ new Date();
|
|
244
|
-
if (typeName === "ZodBigInt") return
|
|
244
|
+
if (typeName === "ZodBigInt") return 0n;
|
|
245
245
|
if (typeName === "ZodLiteral") {
|
|
246
246
|
if ("_zod" in schema) return def.values?.[0];
|
|
247
247
|
else return def.value;
|
|
@@ -3010,6 +3010,145 @@ function createRouteAdapterTestSuite(config) {
|
|
|
3010
3010
|
});
|
|
3011
3011
|
});
|
|
3012
3012
|
}
|
|
3013
|
+
// src/structured-wake-test-suite.ts
|
|
3014
|
+
const structuredOutput = {
|
|
3015
|
+
schema: {
|
|
3016
|
+
type: "object",
|
|
3017
|
+
properties: { summary: { type: "string" } },
|
|
3018
|
+
required: ["summary"]
|
|
3019
|
+
},
|
|
3020
|
+
jsonPromptInjection: true
|
|
3021
|
+
};
|
|
3022
|
+
function createStructuredWakeTestSuite(config) {
|
|
3023
|
+
describe.each([
|
|
3024
|
+
"plain",
|
|
3025
|
+
"durable",
|
|
3026
|
+
"evented"
|
|
3027
|
+
])("HTTP structured idle wake: %s", (kind) => {
|
|
3028
|
+
it.each([{
|
|
3029
|
+
name: "invalid plain text",
|
|
3030
|
+
content: "This is plain text, not JSON.",
|
|
3031
|
+
valid: false
|
|
3032
|
+
}, {
|
|
3033
|
+
name: "valid JSON",
|
|
3034
|
+
content: "{\"summary\":\"hello\"}",
|
|
3035
|
+
valid: true
|
|
3036
|
+
}])("delivers $name result before the terminal boundary", async ({ content, valid }) => {
|
|
3037
|
+
const model = await config.setupModel();
|
|
3038
|
+
const storage = new InMemoryStore();
|
|
3039
|
+
const inner = new Agent({
|
|
3040
|
+
id: "structured-wake-agent",
|
|
3041
|
+
name: "Structured wake agent",
|
|
3042
|
+
instructions: "Respond to the message.",
|
|
3043
|
+
model: model.model,
|
|
3044
|
+
memory: new MockMemory({ storage }),
|
|
3045
|
+
errorProcessors: []
|
|
3046
|
+
});
|
|
3047
|
+
const agent = kind === "durable" ? createDurableAgent({ agent: inner }) : kind === "evented" ? createEventedAgent({ agent: inner }) : inner;
|
|
3048
|
+
const mastra = new Mastra$1({
|
|
3049
|
+
agents: { agent },
|
|
3050
|
+
storage,
|
|
3051
|
+
logger: false
|
|
3052
|
+
});
|
|
3053
|
+
let server;
|
|
3054
|
+
let reader;
|
|
3055
|
+
const abort = new AbortController();
|
|
3056
|
+
const timeout = setTimeout(() => abort.abort(), 15e3);
|
|
3057
|
+
try {
|
|
3058
|
+
model.addResponse(0, { content });
|
|
3059
|
+
model.addResponse(1, { content });
|
|
3060
|
+
server = await config.startServer(mastra);
|
|
3061
|
+
const base = `${server.baseUrl}/api/agents/agent`;
|
|
3062
|
+
const post = (path, body) => fetch(`${base}/${path}`, {
|
|
3063
|
+
method: "POST",
|
|
3064
|
+
headers: { "content-type": "application/json" },
|
|
3065
|
+
body: JSON.stringify(body),
|
|
3066
|
+
signal: abort.signal
|
|
3067
|
+
});
|
|
3068
|
+
const subscription = await post("threads/subscribe", {
|
|
3069
|
+
resourceId: "r",
|
|
3070
|
+
threadId: "t"
|
|
3071
|
+
});
|
|
3072
|
+
expect(subscription.status).toBe(200);
|
|
3073
|
+
reader = subscription.body.getReader();
|
|
3074
|
+
expect(agent.getActiveThreadRunId({
|
|
3075
|
+
resourceId: "r",
|
|
3076
|
+
threadId: "t"
|
|
3077
|
+
})).toBeUndefined();
|
|
3078
|
+
const send = await post("send-message", {
|
|
3079
|
+
resourceId: "r",
|
|
3080
|
+
threadId: "t",
|
|
3081
|
+
message: "hi",
|
|
3082
|
+
ifIdle: { streamOptions: { structuredOutput } }
|
|
3083
|
+
});
|
|
3084
|
+
expect(send.status).toBe(200);
|
|
3085
|
+
await send.json();
|
|
3086
|
+
const frames = [];
|
|
3087
|
+
const decoder = new TextDecoder();
|
|
3088
|
+
let pending = "";
|
|
3089
|
+
let terminal = false;
|
|
3090
|
+
while (!terminal) {
|
|
3091
|
+
const { done, value } = await reader.read();
|
|
3092
|
+
if (done) break;
|
|
3093
|
+
pending += decoder.decode(value, { stream: true });
|
|
3094
|
+
let end;
|
|
3095
|
+
while (!terminal && (end = pending.indexOf("\n\n")) >= 0) {
|
|
3096
|
+
const block = pending.slice(0, end);
|
|
3097
|
+
pending = pending.slice(end + 2);
|
|
3098
|
+
for (const line of block.split("\n")) {
|
|
3099
|
+
if (!line.startsWith("data: ")) continue;
|
|
3100
|
+
const data = line.slice(6);
|
|
3101
|
+
if (data === "[DONE]") {
|
|
3102
|
+
terminal = true;
|
|
3103
|
+
break;
|
|
3104
|
+
}
|
|
3105
|
+
const frame = JSON.parse(data);
|
|
3106
|
+
frames.push(frame);
|
|
3107
|
+
if ([
|
|
3108
|
+
"finish",
|
|
3109
|
+
"error",
|
|
3110
|
+
"abort"
|
|
3111
|
+
].includes(frame.type)) {
|
|
3112
|
+
terminal = true;
|
|
3113
|
+
break;
|
|
3114
|
+
}
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
const control = await post("stream-until-idle", {
|
|
3119
|
+
messages: "hi",
|
|
3120
|
+
memory: {
|
|
3121
|
+
resource: "r",
|
|
3122
|
+
thread: "control"
|
|
3123
|
+
},
|
|
3124
|
+
structuredOutput
|
|
3125
|
+
});
|
|
3126
|
+
expect(control.status).toBe(200);
|
|
3127
|
+
const controlText = await control.text();
|
|
3128
|
+
if (valid) {
|
|
3129
|
+
expect(frames).toContainEqual(expect.objectContaining({
|
|
3130
|
+
type: "object-result",
|
|
3131
|
+
object: { summary: "hello" }
|
|
3132
|
+
}));
|
|
3133
|
+
expect(frames.at(-1)?.type).toBe("finish");
|
|
3134
|
+
expect(controlText).toContain("object-result");
|
|
3135
|
+
} else {
|
|
3136
|
+
expect(controlText).toContain("STRUCTURED_OUTPUT_SCHEMA_VALIDATION_FAILED");
|
|
3137
|
+
expect(JSON.stringify(frames)).toContain("STRUCTURED_OUTPUT_SCHEMA_VALIDATION_FAILED");
|
|
3138
|
+
expect(frames.at(-1)?.type).toBe("error");
|
|
3139
|
+
}
|
|
3140
|
+
expect(model.getRequestCount()).toBe(2);
|
|
3141
|
+
} finally {
|
|
3142
|
+
clearTimeout(timeout);
|
|
3143
|
+
abort.abort();
|
|
3144
|
+
await reader?.cancel().catch(() => {});
|
|
3145
|
+
await mastra.shutdown();
|
|
3146
|
+
await server?.close();
|
|
3147
|
+
await model.close();
|
|
3148
|
+
}
|
|
3149
|
+
}, 3e4);
|
|
3150
|
+
});
|
|
3151
|
+
}
|
|
3013
3152
|
// src/durable-disconnect-test-suite.ts
|
|
3014
3153
|
function deferred() {
|
|
3015
3154
|
let resolve;
|
|
@@ -3020,6 +3159,30 @@ function deferred() {
|
|
|
3020
3159
|
resolve
|
|
3021
3160
|
};
|
|
3022
3161
|
}
|
|
3162
|
+
async function collectPublicFrames(response, onFrame) {
|
|
3163
|
+
const frames = [];
|
|
3164
|
+
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
|
|
3165
|
+
let buffer = "";
|
|
3166
|
+
try {
|
|
3167
|
+
while (true) {
|
|
3168
|
+
const { done, value } = await reader.read();
|
|
3169
|
+
if (done) return frames;
|
|
3170
|
+
buffer += value;
|
|
3171
|
+
const events = buffer.split(/\r?\n\r?\n/);
|
|
3172
|
+
buffer = events.pop();
|
|
3173
|
+
for (const event of events) for (const line of event.split(/\r?\n/)) {
|
|
3174
|
+
if (!line.startsWith("data: ")) continue;
|
|
3175
|
+
const data = line.slice(6);
|
|
3176
|
+
if (data === "[DONE]") continue;
|
|
3177
|
+
const frame = JSON.parse(data);
|
|
3178
|
+
frames.push(frame);
|
|
3179
|
+
onFrame?.(frame);
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
} finally {
|
|
3183
|
+
reader.releaseLock();
|
|
3184
|
+
}
|
|
3185
|
+
}
|
|
3023
3186
|
function createDurableDisconnectTestSuite(config) {
|
|
3024
3187
|
describe.each([
|
|
3025
3188
|
"plain",
|
|
@@ -3037,6 +3200,539 @@ function createDurableDisconnectTestSuite(config) {
|
|
|
3037
3200
|
mastra = void 0;
|
|
3038
3201
|
fixture = void 0;
|
|
3039
3202
|
});
|
|
3203
|
+
it("forwards history and ordered run outcomes without closing the thread subscription (COR-1500)", async () => {
|
|
3204
|
+
fixture = await config.setupModel();
|
|
3205
|
+
fixture.addResponse(0, { content: "first answer" });
|
|
3206
|
+
fixture.addResponse(1, { content: "second answer" });
|
|
3207
|
+
const inner = new Agent({
|
|
3208
|
+
id: "subscription-agent",
|
|
3209
|
+
name: "Subscription agent",
|
|
3210
|
+
instructions: "Answer briefly.",
|
|
3211
|
+
model: fixture.model,
|
|
3212
|
+
memory: new MockMemory({ storage: new InMemoryStore() })
|
|
3213
|
+
});
|
|
3214
|
+
const agent = kind === "durable" ? createDurableAgent({ agent: inner }) : kind === "evented" ? createEventedAgent({ agent: inner }) : inner;
|
|
3215
|
+
mastra = new Mastra$1({
|
|
3216
|
+
agents: { agent },
|
|
3217
|
+
storage: new InMemoryStore()
|
|
3218
|
+
});
|
|
3219
|
+
server = await config.startServer(mastra, () => {});
|
|
3220
|
+
const controller = new AbortController();
|
|
3221
|
+
const coordinates = {
|
|
3222
|
+
resourceId: "subscription-user",
|
|
3223
|
+
threadId: `subscription-${kind}`
|
|
3224
|
+
};
|
|
3225
|
+
const response = await fetch(`${server.baseUrl}/api/agents/agent/threads/subscribe`, {
|
|
3226
|
+
method: "POST",
|
|
3227
|
+
headers: { "content-type": "application/json" },
|
|
3228
|
+
body: JSON.stringify({
|
|
3229
|
+
...coordinates,
|
|
3230
|
+
withInitialHistory: true,
|
|
3231
|
+
withRunStatus: true
|
|
3232
|
+
}),
|
|
3233
|
+
signal: controller.signal
|
|
3234
|
+
});
|
|
3235
|
+
expect(response.status).toBe(200);
|
|
3236
|
+
const reader = response.body.getReader();
|
|
3237
|
+
const decoder = new TextDecoder();
|
|
3238
|
+
let buffered = "";
|
|
3239
|
+
async function nextChunk() {
|
|
3240
|
+
for (;;) {
|
|
3241
|
+
const boundary = buffered.indexOf("\n\n");
|
|
3242
|
+
if (boundary !== -1) {
|
|
3243
|
+
const frame = buffered.slice(0, boundary);
|
|
3244
|
+
buffered = buffered.slice(boundary + 2);
|
|
3245
|
+
const data = frame.split("\n").find((line) => line.startsWith("data: "));
|
|
3246
|
+
if (data) return JSON.parse(data.slice(6));
|
|
3247
|
+
} else {
|
|
3248
|
+
const next = await reader.read();
|
|
3249
|
+
expect(next.done).toBe(false);
|
|
3250
|
+
buffered += decoder.decode(next.value, { stream: true });
|
|
3251
|
+
}
|
|
3252
|
+
}
|
|
3253
|
+
}
|
|
3254
|
+
try {
|
|
3255
|
+
expect(await nextChunk()).toMatchObject({
|
|
3256
|
+
type: "thread-history",
|
|
3257
|
+
payload: { activeRunId: null }
|
|
3258
|
+
});
|
|
3259
|
+
for (const answer of ["first answer", "second answer"]) {
|
|
3260
|
+
const reading = (async () => {
|
|
3261
|
+
let text = "";
|
|
3262
|
+
for (;;) {
|
|
3263
|
+
const chunk = await nextChunk();
|
|
3264
|
+
if (chunk.type === "text-delta") text += chunk.payload.text;
|
|
3265
|
+
if (chunk.type === "thread-run-status") return {
|
|
3266
|
+
text,
|
|
3267
|
+
runId: chunk.runId,
|
|
3268
|
+
status: chunk.payload.status
|
|
3269
|
+
};
|
|
3270
|
+
}
|
|
3271
|
+
})();
|
|
3272
|
+
const output = await agent.stream("Hello", { memory: {
|
|
3273
|
+
thread: coordinates.threadId,
|
|
3274
|
+
resource: coordinates.resourceId
|
|
3275
|
+
} });
|
|
3276
|
+
for await (const _chunk of output.fullStream);
|
|
3277
|
+
expect(await reading).toEqual({
|
|
3278
|
+
text: answer,
|
|
3279
|
+
runId: output.runId,
|
|
3280
|
+
status: "success"
|
|
3281
|
+
});
|
|
3282
|
+
}
|
|
3283
|
+
} finally {
|
|
3284
|
+
controller.abort();
|
|
3285
|
+
await reader.cancel().catch(() => {});
|
|
3286
|
+
}
|
|
3287
|
+
}, 3e4);
|
|
3288
|
+
if (kind !== "plain") {
|
|
3289
|
+
it.each([
|
|
3290
|
+
"error",
|
|
3291
|
+
"abort",
|
|
3292
|
+
"abort-without-text",
|
|
3293
|
+
"aborted-finish"
|
|
3294
|
+
])("replays %s as public terminal frames (COR-1498)", async (terminal) => {
|
|
3295
|
+
fixture = await config.setupModel();
|
|
3296
|
+
const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({
|
|
3297
|
+
agent: new Agent({
|
|
3298
|
+
id: "terminal-agent",
|
|
3299
|
+
name: "Terminal agent",
|
|
3300
|
+
instructions: "Unused",
|
|
3301
|
+
model: fixture.model
|
|
3302
|
+
}),
|
|
3303
|
+
cleanupTimeoutMs: 6e4
|
|
3304
|
+
});
|
|
3305
|
+
mastra = new Mastra$1({
|
|
3306
|
+
agents: { agent },
|
|
3307
|
+
storage: new InMemoryStore(),
|
|
3308
|
+
logger: false
|
|
3309
|
+
});
|
|
3310
|
+
server = await config.startServer(mastra, () => {});
|
|
3311
|
+
const runId = `observe-terminal-${kind}-${terminal}`;
|
|
3312
|
+
const topic = AGENT_STREAM_TOPIC(runId);
|
|
3313
|
+
await agent.pubsub.publish(topic, {
|
|
3314
|
+
runId,
|
|
3315
|
+
type: "iteration-complete",
|
|
3316
|
+
data: {}
|
|
3317
|
+
});
|
|
3318
|
+
await agent.pubsub.publish(topic, {
|
|
3319
|
+
runId,
|
|
3320
|
+
type: "suspended",
|
|
3321
|
+
data: { type: "suspension" }
|
|
3322
|
+
});
|
|
3323
|
+
await agent.pubsub.publish(topic, {
|
|
3324
|
+
runId,
|
|
3325
|
+
type: terminal === "aborted-finish" ? "finish" : terminal === "abort-without-text" ? "abort" : terminal,
|
|
3326
|
+
data: terminal === "error" ? { error: {
|
|
3327
|
+
id: "TEST_ERROR",
|
|
3328
|
+
name: "ProviderError",
|
|
3329
|
+
message: "Provider unavailable",
|
|
3330
|
+
stack: "producer-stack"
|
|
3331
|
+
} } : terminal === "abort" || terminal === "abort-without-text" ? {
|
|
3332
|
+
...terminal === "abort" ? { text: "partial answer" } : {},
|
|
3333
|
+
steps: []
|
|
3334
|
+
} : {
|
|
3335
|
+
output: {
|
|
3336
|
+
text: "partial answer",
|
|
3337
|
+
steps: [],
|
|
3338
|
+
usage: {
|
|
3339
|
+
inputTokens: 1,
|
|
3340
|
+
outputTokens: 2,
|
|
3341
|
+
totalTokens: 3
|
|
3342
|
+
}
|
|
3343
|
+
},
|
|
3344
|
+
stepResult: { reason: "abort" }
|
|
3345
|
+
}
|
|
3346
|
+
});
|
|
3347
|
+
const reference = createDurableAgentStream({
|
|
3348
|
+
pubsub: agent.pubsub,
|
|
3349
|
+
runId,
|
|
3350
|
+
messageId: "reference",
|
|
3351
|
+
model: {
|
|
3352
|
+
version: "v3",
|
|
3353
|
+
modelId: void 0,
|
|
3354
|
+
provider: void 0
|
|
3355
|
+
}
|
|
3356
|
+
});
|
|
3357
|
+
const expected = [];
|
|
3358
|
+
try {
|
|
3359
|
+
for await (const chunk of reference.output.fullStream) {
|
|
3360
|
+
const serialized = serializeStreamChunk(chunk);
|
|
3361
|
+
if (!serialized.ok) throw serialized.error;
|
|
3362
|
+
expected.push(JSON.parse(serialized.json));
|
|
3363
|
+
}
|
|
3364
|
+
} finally {
|
|
3365
|
+
await reference.cleanup();
|
|
3366
|
+
}
|
|
3367
|
+
for (const offset of [
|
|
3368
|
+
0,
|
|
3369
|
+
1,
|
|
3370
|
+
2
|
|
3371
|
+
]) {
|
|
3372
|
+
const response = await fetch(`${server.baseUrl}/api/agents/agent/observe`, {
|
|
3373
|
+
method: "POST",
|
|
3374
|
+
headers: { "content-type": "application/json" },
|
|
3375
|
+
body: JSON.stringify({
|
|
3376
|
+
runId,
|
|
3377
|
+
offset
|
|
3378
|
+
})
|
|
3379
|
+
});
|
|
3380
|
+
expect(response.status).toBe(200);
|
|
3381
|
+
expect(await collectPublicFrames(response)).toEqual(expected);
|
|
3382
|
+
}
|
|
3383
|
+
expect(expected).toHaveLength(terminal === "error" ? 1 : 2);
|
|
3384
|
+
if (config.observeWithClient) expect(await config.observeWithClient(server.baseUrl, runId)).toEqual(expected);
|
|
3385
|
+
});
|
|
3386
|
+
it("preserves finish metadata after a model error chunk (COR-1498)", async () => {
|
|
3387
|
+
fixture = await config.setupModel();
|
|
3388
|
+
const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({ agent: new Agent({
|
|
3389
|
+
id: "error-agent",
|
|
3390
|
+
name: "Error agent",
|
|
3391
|
+
instructions: "Unused",
|
|
3392
|
+
model: fixture.model
|
|
3393
|
+
}) });
|
|
3394
|
+
mastra = new Mastra$1({
|
|
3395
|
+
agents: { agent },
|
|
3396
|
+
storage: new InMemoryStore(),
|
|
3397
|
+
logger: false
|
|
3398
|
+
});
|
|
3399
|
+
server = await config.startServer(mastra, () => {});
|
|
3400
|
+
const runId = `observe-model-error-${kind}`;
|
|
3401
|
+
const usage = {
|
|
3402
|
+
inputTokens: 1,
|
|
3403
|
+
outputTokens: 2,
|
|
3404
|
+
totalTokens: 3
|
|
3405
|
+
};
|
|
3406
|
+
const output = {
|
|
3407
|
+
text: "",
|
|
3408
|
+
steps: [],
|
|
3409
|
+
usage
|
|
3410
|
+
};
|
|
3411
|
+
const stepResult = {
|
|
3412
|
+
reason: "error",
|
|
3413
|
+
warnings: [],
|
|
3414
|
+
isContinued: false
|
|
3415
|
+
};
|
|
3416
|
+
await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3417
|
+
runId,
|
|
3418
|
+
type: "chunk",
|
|
3419
|
+
data: {
|
|
3420
|
+
type: "error",
|
|
3421
|
+
runId,
|
|
3422
|
+
from: "AGENT",
|
|
3423
|
+
payload: { error: { message: "Provider unavailable" } }
|
|
3424
|
+
}
|
|
3425
|
+
});
|
|
3426
|
+
await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3427
|
+
runId,
|
|
3428
|
+
type: "chunk",
|
|
3429
|
+
data: {
|
|
3430
|
+
type: "step-finish",
|
|
3431
|
+
runId,
|
|
3432
|
+
from: "AGENT",
|
|
3433
|
+
payload: {
|
|
3434
|
+
output,
|
|
3435
|
+
stepResult,
|
|
3436
|
+
metadata: {}
|
|
3437
|
+
}
|
|
3438
|
+
}
|
|
3439
|
+
});
|
|
3440
|
+
await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3441
|
+
runId,
|
|
3442
|
+
type: "finish",
|
|
3443
|
+
data: {
|
|
3444
|
+
output,
|
|
3445
|
+
stepResult
|
|
3446
|
+
}
|
|
3447
|
+
});
|
|
3448
|
+
const reference = createDurableAgentStream({
|
|
3449
|
+
pubsub: agent.pubsub,
|
|
3450
|
+
runId,
|
|
3451
|
+
messageId: "reference",
|
|
3452
|
+
model: {
|
|
3453
|
+
version: "v3",
|
|
3454
|
+
modelId: void 0,
|
|
3455
|
+
provider: void 0
|
|
3456
|
+
}
|
|
3457
|
+
});
|
|
3458
|
+
const expected = [];
|
|
3459
|
+
try {
|
|
3460
|
+
for await (const chunk of reference.output.fullStream) {
|
|
3461
|
+
const serialized = serializeStreamChunk(chunk);
|
|
3462
|
+
if (!serialized.ok) throw serialized.error;
|
|
3463
|
+
expected.push(JSON.parse(serialized.json));
|
|
3464
|
+
}
|
|
3465
|
+
} finally {
|
|
3466
|
+
await reference.cleanup();
|
|
3467
|
+
}
|
|
3468
|
+
const frames = await collectPublicFrames(await fetch(`${server.baseUrl}/api/agents/agent/observe`, {
|
|
3469
|
+
method: "POST",
|
|
3470
|
+
headers: { "content-type": "application/json" },
|
|
3471
|
+
body: JSON.stringify({ runId })
|
|
3472
|
+
}));
|
|
3473
|
+
expect(frames.map((frame) => frame.type)).toEqual([
|
|
3474
|
+
"error",
|
|
3475
|
+
"step-finish",
|
|
3476
|
+
"finish"
|
|
3477
|
+
]);
|
|
3478
|
+
expect(frames).toEqual(expected);
|
|
3479
|
+
expect(frames.at(-1)).toMatchObject({ payload: {
|
|
3480
|
+
output: { usage },
|
|
3481
|
+
stepResult: { reason: "error" }
|
|
3482
|
+
} });
|
|
3483
|
+
if (config.observeWithClient) expect(await config.observeWithClient(server.baseUrl, runId)).toEqual(expected);
|
|
3484
|
+
});
|
|
3485
|
+
it.each([false, true])("matches public finish usage when terminal raw is present: %s (COR-1498)", async (explicit) => {
|
|
3486
|
+
fixture = await config.setupModel();
|
|
3487
|
+
const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({ agent: new Agent({
|
|
3488
|
+
id: "usage-agent",
|
|
3489
|
+
name: "Usage agent",
|
|
3490
|
+
instructions: "Unused",
|
|
3491
|
+
model: fixture.model
|
|
3492
|
+
}) });
|
|
3493
|
+
mastra = new Mastra$1({
|
|
3494
|
+
agents: { agent },
|
|
3495
|
+
storage: new InMemoryStore(),
|
|
3496
|
+
logger: false
|
|
3497
|
+
});
|
|
3498
|
+
server = await config.startServer(mastra, () => {});
|
|
3499
|
+
const runId = `observe-usage-${kind}-${explicit}`;
|
|
3500
|
+
const usage = {
|
|
3501
|
+
inputTokens: 1,
|
|
3502
|
+
outputTokens: 2,
|
|
3503
|
+
totalTokens: 3
|
|
3504
|
+
};
|
|
3505
|
+
const raw = { source: "first step" };
|
|
3506
|
+
const steps = [{ usage: {
|
|
3507
|
+
...usage,
|
|
3508
|
+
raw
|
|
3509
|
+
} }, { usage }];
|
|
3510
|
+
for (const step of steps) await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3511
|
+
runId,
|
|
3512
|
+
type: "chunk",
|
|
3513
|
+
data: {
|
|
3514
|
+
type: "step-finish",
|
|
3515
|
+
runId,
|
|
3516
|
+
from: "AGENT",
|
|
3517
|
+
payload: {
|
|
3518
|
+
output: step,
|
|
3519
|
+
stepResult: {
|
|
3520
|
+
reason: "stop",
|
|
3521
|
+
warnings: [],
|
|
3522
|
+
isContinued: false
|
|
3523
|
+
},
|
|
3524
|
+
metadata: {}
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
});
|
|
3528
|
+
await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3529
|
+
runId,
|
|
3530
|
+
type: "finish",
|
|
3531
|
+
data: {
|
|
3532
|
+
output: {
|
|
3533
|
+
text: "",
|
|
3534
|
+
steps,
|
|
3535
|
+
usage: {
|
|
3536
|
+
...usage,
|
|
3537
|
+
...explicit ? { raw: { source: "finish" } } : {}
|
|
3538
|
+
}
|
|
3539
|
+
},
|
|
3540
|
+
stepResult: { reason: "stop" }
|
|
3541
|
+
}
|
|
3542
|
+
});
|
|
3543
|
+
const reference = createDurableAgentStream({
|
|
3544
|
+
pubsub: agent.pubsub,
|
|
3545
|
+
runId,
|
|
3546
|
+
messageId: "reference",
|
|
3547
|
+
model: {
|
|
3548
|
+
version: "v3",
|
|
3549
|
+
modelId: void 0,
|
|
3550
|
+
provider: void 0
|
|
3551
|
+
}
|
|
3552
|
+
});
|
|
3553
|
+
let expected;
|
|
3554
|
+
try {
|
|
3555
|
+
for await (const chunk of reference.output.fullStream) if (chunk.type === "finish") {
|
|
3556
|
+
const serialized = serializeStreamChunk(chunk);
|
|
3557
|
+
if (!serialized.ok) throw serialized.error;
|
|
3558
|
+
expected = JSON.parse(serialized.json).payload.output.usage;
|
|
3559
|
+
}
|
|
3560
|
+
} finally {
|
|
3561
|
+
await reference.cleanup();
|
|
3562
|
+
}
|
|
3563
|
+
expect(expected).toEqual({
|
|
3564
|
+
...usage,
|
|
3565
|
+
raw
|
|
3566
|
+
});
|
|
3567
|
+
const finish = (await collectPublicFrames(await fetch(`${server.baseUrl}/api/agents/agent/observe`, {
|
|
3568
|
+
method: "POST",
|
|
3569
|
+
headers: { "content-type": "application/json" },
|
|
3570
|
+
body: JSON.stringify({ runId })
|
|
3571
|
+
}))).find((frame) => frame.type === "finish");
|
|
3572
|
+
expect(finish?.payload.output.usage).toEqual(expected);
|
|
3573
|
+
});
|
|
3574
|
+
it("drains replay before a rejecting error without emitting DONE (COR-1498)", async () => {
|
|
3575
|
+
fixture = await config.setupModel();
|
|
3576
|
+
const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({ agent: new Agent({
|
|
3577
|
+
id: "reject-agent",
|
|
3578
|
+
name: "Reject agent",
|
|
3579
|
+
instructions: "Unused",
|
|
3580
|
+
model: fixture.model
|
|
3581
|
+
}) });
|
|
3582
|
+
mastra = new Mastra$1({
|
|
3583
|
+
agents: { agent },
|
|
3584
|
+
storage: new InMemoryStore(),
|
|
3585
|
+
logger: false
|
|
3586
|
+
});
|
|
3587
|
+
server = await config.startServer(mastra, () => {});
|
|
3588
|
+
const runId = `observe-reject-${kind}`;
|
|
3589
|
+
const prefix = {
|
|
3590
|
+
type: "text-delta",
|
|
3591
|
+
runId,
|
|
3592
|
+
from: "AGENT",
|
|
3593
|
+
payload: { text: "before rejection" }
|
|
3594
|
+
};
|
|
3595
|
+
await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3596
|
+
runId,
|
|
3597
|
+
type: "chunk",
|
|
3598
|
+
data: prefix
|
|
3599
|
+
});
|
|
3600
|
+
await agent.pubsub.publish(AGENT_STREAM_TOPIC(runId), {
|
|
3601
|
+
runId,
|
|
3602
|
+
type: "error",
|
|
3603
|
+
data: {
|
|
3604
|
+
delivery: "reject",
|
|
3605
|
+
error: {
|
|
3606
|
+
name: "Error",
|
|
3607
|
+
message: "Run superseded"
|
|
3608
|
+
}
|
|
3609
|
+
}
|
|
3610
|
+
});
|
|
3611
|
+
const response = await fetch(`${server.baseUrl}/api/agents/agent/observe`, {
|
|
3612
|
+
method: "POST",
|
|
3613
|
+
headers: { "content-type": "application/json" },
|
|
3614
|
+
body: JSON.stringify({ runId })
|
|
3615
|
+
});
|
|
3616
|
+
expect(response.status).toBe(200);
|
|
3617
|
+
const wire = await response.text();
|
|
3618
|
+
expect(wire).toContain(JSON.stringify(prefix));
|
|
3619
|
+
expect(wire).not.toContain("[DONE]");
|
|
3620
|
+
expect(wire).not.toContain("\"type\":\"error\"");
|
|
3621
|
+
});
|
|
3622
|
+
it("reattaches with the same public frames as stream, including tools and finish metadata (COR-1498)", async () => {
|
|
3623
|
+
fixture = await config.setupModel();
|
|
3624
|
+
const entered = deferred();
|
|
3625
|
+
const gate = deferred();
|
|
3626
|
+
const liveFrames = [];
|
|
3627
|
+
const tool = createTool({
|
|
3628
|
+
id: "lookup",
|
|
3629
|
+
description: "Look up an answer",
|
|
3630
|
+
inputSchema: z.object({}),
|
|
3631
|
+
execute: async () => {
|
|
3632
|
+
entered.resolve();
|
|
3633
|
+
await gate.promise;
|
|
3634
|
+
return { answer: 42 };
|
|
3635
|
+
}
|
|
3636
|
+
});
|
|
3637
|
+
let processedChunks = 0;
|
|
3638
|
+
const inner = new Agent({
|
|
3639
|
+
id: "observe-agent",
|
|
3640
|
+
name: "Observe agent",
|
|
3641
|
+
instructions: "Look up the answer.",
|
|
3642
|
+
model: fixture.model,
|
|
3643
|
+
tools: { lookup: tool },
|
|
3644
|
+
outputProcessors: [{
|
|
3645
|
+
id: "count-output-chunks",
|
|
3646
|
+
async processOutputStream({ part }) {
|
|
3647
|
+
processedChunks++;
|
|
3648
|
+
return part;
|
|
3649
|
+
}
|
|
3650
|
+
}]
|
|
3651
|
+
});
|
|
3652
|
+
const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({
|
|
3653
|
+
agent: inner,
|
|
3654
|
+
cleanupTimeoutMs: 6e4
|
|
3655
|
+
});
|
|
3656
|
+
mastra = new Mastra$1({
|
|
3657
|
+
agents: { agent },
|
|
3658
|
+
storage: new InMemoryStore(),
|
|
3659
|
+
logger: false
|
|
3660
|
+
});
|
|
3661
|
+
server = await config.startServer(mastra, () => {});
|
|
3662
|
+
fixture.addResponse(0, {
|
|
3663
|
+
content: "Looking up",
|
|
3664
|
+
toolCalls: [{
|
|
3665
|
+
id: "lookup-1",
|
|
3666
|
+
name: "lookup",
|
|
3667
|
+
arguments: {}
|
|
3668
|
+
}]
|
|
3669
|
+
});
|
|
3670
|
+
fixture.addResponse(1, { content: "The answer is 42." });
|
|
3671
|
+
const runId = `observe-parity-${kind}`;
|
|
3672
|
+
const base = `${server.baseUrl}/api/agents/agent`;
|
|
3673
|
+
const post = async (route, body) => {
|
|
3674
|
+
const response = await fetch(`${base}/${route}`, {
|
|
3675
|
+
method: "POST",
|
|
3676
|
+
headers: { "content-type": "application/json" },
|
|
3677
|
+
body: JSON.stringify(body)
|
|
3678
|
+
});
|
|
3679
|
+
expect(response.status).toBe(200);
|
|
3680
|
+
return response;
|
|
3681
|
+
};
|
|
3682
|
+
const original = collectPublicFrames(await post("stream", {
|
|
3683
|
+
messages: "Find the answer",
|
|
3684
|
+
runId,
|
|
3685
|
+
maxSteps: 3
|
|
3686
|
+
}));
|
|
3687
|
+
try {
|
|
3688
|
+
await entered.promise;
|
|
3689
|
+
const live = collectPublicFrames(await post("observe", {
|
|
3690
|
+
runId,
|
|
3691
|
+
offset: 0
|
|
3692
|
+
}), (frame) => {
|
|
3693
|
+
liveFrames.push(frame);
|
|
3694
|
+
});
|
|
3695
|
+
await expect.poll(() => liveFrames.some((frame) => frame.type === "tool-call"), { timeout: 2e3 }).toBe(true);
|
|
3696
|
+
await (await post("observe", { runId })).body.cancel();
|
|
3697
|
+
gate.resolve();
|
|
3698
|
+
const frames = await original;
|
|
3699
|
+
expect(await live).toEqual(frames);
|
|
3700
|
+
expect(frames.filter((frame) => frame.type === "text-delta").map((frame) => frame.payload.text).join("")).toBe("Looking upThe answer is 42.");
|
|
3701
|
+
expect(frames.some((frame) => frame.type === "tool-result")).toBe(true);
|
|
3702
|
+
expect(frames.filter((frame) => frame.type === "step-start")).toHaveLength(2);
|
|
3703
|
+
expect(frames.at(-1)).toMatchObject({
|
|
3704
|
+
type: "finish",
|
|
3705
|
+
runId,
|
|
3706
|
+
payload: { stepResult: { reason: "stop" } }
|
|
3707
|
+
});
|
|
3708
|
+
const processedAtFinish = processedChunks;
|
|
3709
|
+
expect(processedAtFinish).toBeGreaterThan(0);
|
|
3710
|
+
for (const offset of [void 0, 0]) {
|
|
3711
|
+
expect(await collectPublicFrames(await post("observe", {
|
|
3712
|
+
runId,
|
|
3713
|
+
offset
|
|
3714
|
+
}))).toEqual(frames);
|
|
3715
|
+
if (config.observeWithClient) expect(await config.observeWithClient(server.baseUrl, runId, offset)).toEqual(frames);
|
|
3716
|
+
}
|
|
3717
|
+
const delta = (await agent.pubsub.getHistory(AGENT_STREAM_TOPIC(runId))).find((event) => event.type === "chunk" && event.data?.type === "text-delta");
|
|
3718
|
+
expect(delta).toBeDefined();
|
|
3719
|
+
const firstDelta = frames.findIndex((frame) => frame.type === "text-delta");
|
|
3720
|
+
expect(await collectPublicFrames(await post("observe", {
|
|
3721
|
+
runId,
|
|
3722
|
+
offset: delta.index
|
|
3723
|
+
}))).toEqual(frames.slice(firstDelta));
|
|
3724
|
+
expect(await collectPublicFrames(await post("observe", {
|
|
3725
|
+
runId,
|
|
3726
|
+
offset: delta.index + 1
|
|
3727
|
+
}))).toEqual(frames.slice(firstDelta + 1));
|
|
3728
|
+
if (config.observeWithClient) expect(await config.observeWithClient(server.baseUrl, runId, delta.index)).toEqual(frames.slice(firstDelta));
|
|
3729
|
+
expect(processedChunks).toBe(processedAtFinish);
|
|
3730
|
+
} finally {
|
|
3731
|
+
gate.resolve();
|
|
3732
|
+
await original;
|
|
3733
|
+
}
|
|
3734
|
+
});
|
|
3735
|
+
}
|
|
3040
3736
|
for (const route of [
|
|
3041
3737
|
"stream",
|
|
3042
3738
|
"stream-alias",
|
|
@@ -4936,6 +5632,6 @@ function createCustomRouteFGATestSuite(config) {
|
|
|
4936
5632
|
});
|
|
4937
5633
|
});
|
|
4938
5634
|
}
|
|
4939
|
-
export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, expectSerializedStreamChunks };
|
|
5635
|
+
export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
|
|
4940
5636
|
|
|
4941
5637
|
//# sourceMappingURL=index.js.map
|