|
| 1 | +// Import the test harness FIRST — installs the resource catalog so |
| 2 | +// `chat.agent()` below registers its task functions correctly. |
| 3 | +import { mockChatAgent } from "../src/v3/test/index.js"; |
| 4 | + |
| 5 | +import { describe, expect, it } from "vitest"; |
| 6 | +import { chat } from "../src/v3/ai.js"; |
| 7 | +import { simulateReadableStream, streamText } from "ai"; |
| 8 | +import { MockLanguageModelV3 } from "ai/test"; |
| 9 | +import type { LanguageModelV3StreamPart } from "@ai-sdk/provider"; |
| 10 | + |
| 11 | +function textStream(text: string): ReadableStream<LanguageModelV3StreamPart> { |
| 12 | + return simulateReadableStream({ |
| 13 | + chunks: [ |
| 14 | + { type: "text-start", id: "t1" }, |
| 15 | + { type: "text-delta", id: "t1", delta: text }, |
| 16 | + { type: "text-end", id: "t1" }, |
| 17 | + { |
| 18 | + type: "finish", |
| 19 | + finishReason: { unified: "stop", raw: "stop" }, |
| 20 | + usage: { |
| 21 | + inputTokens: { total: 10, noCache: 10, cacheRead: undefined, cacheWrite: undefined }, |
| 22 | + outputTokens: { total: 10, text: 10, reasoning: undefined }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + ], |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +describe("chat.inject with a system role (TRI-13380)", () => { |
| 30 | + it("goes to the instructions lane instead of poisoning the prompt", async () => { |
| 31 | + const model = new MockLanguageModelV3({ |
| 32 | + doStream: async () => ({ stream: textStream("ok") }), |
| 33 | + }); |
| 34 | + |
| 35 | + let injected = false; |
| 36 | + |
| 37 | + const agent = chat.agent({ |
| 38 | + id: "inject-system-instructions", |
| 39 | + onBoot: async () => { |
| 40 | + chat.prompt.set({ |
| 41 | + promptId: "base", |
| 42 | + version: 1, |
| 43 | + labels: ["local"], |
| 44 | + text: "You are a helpful assistant.", |
| 45 | + model: undefined, |
| 46 | + config: undefined, |
| 47 | + toAISDKTelemetry: () => ({ experimental_telemetry: { isEnabled: true, metadata: {} } }), |
| 48 | + }); |
| 49 | + }, |
| 50 | + onTurnComplete: async () => { |
| 51 | + if (injected) return; |
| 52 | + injected = true; |
| 53 | + /** |
| 54 | + * The shape every docs example uses. On ai@7 a system message inside |
| 55 | + * `messages` is rejected by `standardizePrompt` for every provider, so |
| 56 | + * this used to kill the next turn — an error chunk reading "An error |
| 57 | + * occurred." and an assistant message with no parts. |
| 58 | + */ |
| 59 | + chat.inject([{ role: "system", content: "The user just upgraded to Pro." }]); |
| 60 | + }, |
| 61 | + run: async ({ messages, signal }) => |
| 62 | + streamText({ |
| 63 | + ...chat.toStreamTextOptions(), |
| 64 | + model, |
| 65 | + messages, |
| 66 | + abortSignal: signal, |
| 67 | + }), |
| 68 | + }); |
| 69 | + |
| 70 | + const harness = mockChatAgent(agent, { chatId: "inject-system-instructions" }); |
| 71 | + |
| 72 | + try { |
| 73 | + await harness.sendMessage({ id: "u1", role: "user", parts: [{ type: "text", text: "one" }] }); |
| 74 | + await new Promise((r) => setTimeout(r, 40)); |
| 75 | + |
| 76 | + const turn = await harness.sendMessage({ |
| 77 | + id: "u2", |
| 78 | + role: "user", |
| 79 | + parts: [{ type: "text", text: "two" }], |
| 80 | + }); |
| 81 | + await new Promise((r) => setTimeout(r, 40)); |
| 82 | + |
| 83 | + // The turn survives. |
| 84 | + const errors = turn.rawChunks.filter((c) => (c as { type?: string })?.type === "error"); |
| 85 | + expect(errors).toEqual([]); |
| 86 | + |
| 87 | + // The injected context arrives as a system block, alongside the base prompt, |
| 88 | + // and never as a system message inside `messages`. |
| 89 | + const prompt = model.doStreamCalls.at(-1)!.prompt; |
| 90 | + const systemBlocks = prompt.filter((m) => m.role === "system"); |
| 91 | + const asText = JSON.stringify(systemBlocks); |
| 92 | + |
| 93 | + expect(asText).toContain("You are a helpful assistant."); |
| 94 | + expect(asText).toContain("The user just upgraded to Pro."); |
| 95 | + |
| 96 | + const nonSystem = prompt.filter((m) => m.role !== "system"); |
| 97 | + expect(JSON.stringify(nonSystem)).not.toContain("upgraded to Pro"); |
| 98 | + } finally { |
| 99 | + await harness.close(); |
| 100 | + } |
| 101 | + }); |
| 102 | + it("emits one system value whether or not the base block is cached", async () => { |
| 103 | + /** |
| 104 | + * Never an array. ai@6+ accepts `Array<SystemModelMessage>` and would let a |
| 105 | + * cached base block keep its cache entry, but ai@5 rejects an array outright |
| 106 | + * ("Invalid prompt: system must be a string") while accepting a single |
| 107 | + * structured block — and the peer range still spans v5. So a plain base |
| 108 | + * concatenates into a string, and a cached base absorbs the injection into its |
| 109 | + * own content, keeping its provider options. |
| 110 | + */ |
| 111 | + const shapes: unknown[] = []; |
| 112 | + |
| 113 | + function agentFor(id: string, cacheControl: boolean) { |
| 114 | + let injected = false; |
| 115 | + return chat.agent({ |
| 116 | + id, |
| 117 | + onBoot: async () => { |
| 118 | + chat.prompt.set({ |
| 119 | + promptId: "base", |
| 120 | + version: 1, |
| 121 | + labels: ["local"], |
| 122 | + text: "Base instructions.", |
| 123 | + model: undefined, |
| 124 | + config: undefined, |
| 125 | + toAISDKTelemetry: () => ({ |
| 126 | + experimental_telemetry: { isEnabled: true, metadata: {} }, |
| 127 | + }), |
| 128 | + }); |
| 129 | + }, |
| 130 | + onTurnComplete: async () => { |
| 131 | + if (injected) return; |
| 132 | + injected = true; |
| 133 | + chat.inject([{ role: "system", content: "Amendment." }]); |
| 134 | + }, |
| 135 | + run: async ({ messages, signal }) => { |
| 136 | + const options = cacheControl |
| 137 | + ? chat.toStreamTextOptions({ cacheControl: { type: "ephemeral" } }) |
| 138 | + : chat.toStreamTextOptions(); |
| 139 | + shapes.push(Array.isArray(options.system) ? "array" : typeof options.system); |
| 140 | + return streamText({ |
| 141 | + ...options, |
| 142 | + model: new MockLanguageModelV3({ |
| 143 | + doStream: async () => ({ stream: textStream("ok") }), |
| 144 | + }), |
| 145 | + messages, |
| 146 | + abortSignal: signal, |
| 147 | + }); |
| 148 | + }, |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + for (const [id, cacheControl] of [ |
| 153 | + ["shape-plain", false], |
| 154 | + ["shape-cached", true], |
| 155 | + ] as const) { |
| 156 | + const harness = mockChatAgent(agentFor(id, cacheControl), { chatId: id }); |
| 157 | + try { |
| 158 | + await harness.sendMessage({ id: "u1", role: "user", parts: [{ type: "text", text: "one" }] }); |
| 159 | + await new Promise((r) => setTimeout(r, 40)); |
| 160 | + await harness.sendMessage({ id: "u2", role: "user", parts: [{ type: "text", text: "two" }] }); |
| 161 | + await new Promise((r) => setTimeout(r, 40)); |
| 162 | + } finally { |
| 163 | + await harness.close(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // [plain turn 1, plain turn 2 (injected), cached turn 1, cached turn 2 (injected)] |
| 168 | + expect(shapes).toEqual(["string", "string", "object", "object"]); |
| 169 | + }); |
| 170 | + |
| 171 | +}); |
0 commit comments