Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/server/src/routes/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createStartupPins,
parseOptionalStringArrayField,
} from "./agent-startup.js";
import type { AgentRecord } from "../agents/manager.js";
import type { TemplateService } from "../templates/service.js";
import { parseTemplateArgs } from "../templates/store.js";
import {
Expand Down Expand Up @@ -88,6 +89,9 @@ function classifyErrorCode(message: string): number {
type TemplateRouteDeps = {
templateService: TemplateService;
publishUiEvent: (event: unknown) => void;
withStreamFlag: <T extends AgentRecord>(
agent: T
) => T & { hasStream: boolean };
};

export async function registerTemplateRoutes(
Expand Down Expand Up @@ -265,7 +269,7 @@ export async function registerTemplateRoutes(
});
deps.publishUiEvent({
type: "agent.upsert",
agent: result.agent,
agent: deps.withStreamFlag(result.agent),
});
return { agent: result.agent };
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ async function registerRoutes() {
await registerTemplateRoutes(app, {
templateService,
publishUiEvent: (event) => uiEventBroker.publish(event as UiEvent),
withStreamFlag,
});

await registerMcpRoutes(app, {
Expand Down
87 changes: 87 additions & 0 deletions apps/server/test/template-launch-publish.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* The launch route's `agent.upsert` publish must carry the `hasStream` flag,
* like every other `agent.upsert` publish site. `applyAgentUpsert` on the web
* side replaces the cached agent wholesale, so an unflagged payload drops the
* field from the cached row.
*/
import {
afterAll,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from "vitest";
import Fastify, { type FastifyInstance } from "fastify";
import fastifyMultipart from "@fastify/multipart";

import { registerTemplateRoutes } from "../src/routes/templates.js";

const LAUNCHED_AGENT = {
id: "agt_launched",
name: "launch-tmpl",
cwd: "/tmp",
status: "running",
};

let app: FastifyInstance;
let publishUiEvent: ReturnType<typeof vi.fn>;
let launchTemplate: ReturnType<typeof vi.fn>;

beforeAll(async () => {
publishUiEvent = vi.fn();
launchTemplate = vi.fn(async () => ({
agent: LAUNCHED_AGENT,
templateId: "tmpl-1",
templateName: "launch-tmpl",
}));

app = Fastify();
await app.register(fastifyMultipart);
await registerTemplateRoutes(app, {
templateService: { launchTemplate } as never,
publishUiEvent,
withStreamFlag: ((agent: object) => ({
...agent,
hasStream: true,
})) as never,
});
await app.ready();
});

afterAll(async () => {
await app.close();
});

beforeEach(() => {
publishUiEvent.mockClear();
});

describe("POST /api/v1/templates/:id/launch", () => {
it("publishes agent.upsert with the stream flag applied", async () => {
const res = await app.inject({
method: "POST",
url: "/api/v1/templates/tmpl-1/launch",
headers: { "content-type": "application/json" },
payload: {},
});

expect(res.statusCode).toBe(200);
expect(publishUiEvent).toHaveBeenCalledWith({
type: "agent.upsert",
agent: { ...LAUNCHED_AGENT, hasStream: true },
});
});

it("still returns the unflagged agent in the HTTP response", async () => {
const res = await app.inject({
method: "POST",
url: "/api/v1/templates/tmpl-1/launch",
headers: { "content-type": "application/json" },
payload: {},
});

expect(res.json()).toEqual({ agent: LAUNCHED_AGENT });
});
});
Loading