mirror of
https://github.com/block/buzz.git
synced 2026-12-01 13:42:45 +00:00
fix(desktop): preserve template agent deployment context
Wait for initially loading template dependencies before deployment and preserve each team-derived agent context with a fresh team instance. Co-authored-by: Carl <3c4caeafb646d23867f1c4832e68211d77e2561946171625f75c3ce1a3f2670f@buzz.block.builderlab.xyz> Signed-off-by: kenny lopez <klopez4212@gmail.com>
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
buildTemplateAgentInputs,
|
||||
dataAfterInitialLoad,
|
||||
} from "./templateAgentInputs.ts";
|
||||
|
||||
function createPersona(id, overrides = {}) {
|
||||
return {
|
||||
id,
|
||||
displayName: `Persona ${id}`,
|
||||
avatarUrl: null,
|
||||
systemPrompt: `Prompt ${id}`,
|
||||
runtime: null,
|
||||
model: null,
|
||||
provider: null,
|
||||
namePool: [],
|
||||
isBuiltIn: false,
|
||||
isActive: true,
|
||||
shared: false,
|
||||
envVars: {},
|
||||
respondTo: null,
|
||||
respondToAllowlist: [],
|
||||
parallelism: null,
|
||||
createdAt: "2026-01-01T00:00:00Z",
|
||||
updatedAt: "2026-01-01T00:00:00Z",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function createTeam(id, personaIds) {
|
||||
return {
|
||||
id,
|
||||
name: `Team ${id}`,
|
||||
description: null,
|
||||
instructions: `Instructions ${id}`,
|
||||
personaIds,
|
||||
isBuiltin: false,
|
||||
sourceDir: null,
|
||||
isSymlink: false,
|
||||
symlinkTarget: null,
|
||||
version: null,
|
||||
createdAt: "2026-01-01T00:00:00Z",
|
||||
updatedAt: "2026-01-01T00:00:00Z",
|
||||
};
|
||||
}
|
||||
|
||||
function createTemplate(agents) {
|
||||
return {
|
||||
id: "template-1",
|
||||
name: "Template",
|
||||
description: null,
|
||||
channelType: "stream",
|
||||
visibility: "open",
|
||||
canvasTemplate: null,
|
||||
projectFolders: [],
|
||||
projectFolder: null,
|
||||
worktree: null,
|
||||
agents,
|
||||
isBuiltin: false,
|
||||
createdAt: "2026-01-01T00:00:00Z",
|
||||
updatedAt: "2026-01-01T00:00:00Z",
|
||||
};
|
||||
}
|
||||
|
||||
const runtime = {
|
||||
id: "runtime-1",
|
||||
label: "Runtime",
|
||||
avatarUrl: "",
|
||||
availability: "available",
|
||||
command: "runtime-command",
|
||||
binaryPath: "/runtime-command",
|
||||
defaultArgs: [],
|
||||
mcpCommand: null,
|
||||
modelEnvVar: null,
|
||||
providerEnvVar: null,
|
||||
thinkingEnvVar: null,
|
||||
maxTokensEnvVar: null,
|
||||
contextLimitEnvVar: null,
|
||||
maxRoundsEnvVar: null,
|
||||
installHint: "",
|
||||
installInstructionsUrl: "",
|
||||
canAutoInstall: false,
|
||||
requiresExternalCli: false,
|
||||
underlyingCliPath: null,
|
||||
nodeRequired: false,
|
||||
authStatus: { status: "not_applicable" },
|
||||
loginHint: null,
|
||||
source: "builtin",
|
||||
};
|
||||
|
||||
test("team template entries preserve team context and force a fresh instance", () => {
|
||||
const persona = createPersona("persona-1");
|
||||
const inputs = buildTemplateAgentInputs({
|
||||
template: createTemplate({
|
||||
personas: [],
|
||||
teams: [
|
||||
{
|
||||
teamId: "team-1",
|
||||
runtime: null,
|
||||
model: null,
|
||||
backend: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
personas: [persona],
|
||||
teams: [createTeam("team-1", [persona.id])],
|
||||
runtimes: [runtime],
|
||||
lastRuntimeId: null,
|
||||
});
|
||||
|
||||
assert.equal(inputs.length, 1);
|
||||
assert.equal(inputs[0].personaId, persona.id);
|
||||
assert.equal(inputs[0].teamId, "team-1");
|
||||
assert.equal(inputs[0].forceNewInstance, true);
|
||||
});
|
||||
|
||||
test("the same persona can retain distinct direct and per-team contexts", () => {
|
||||
const persona = createPersona("persona-1");
|
||||
const inputs = buildTemplateAgentInputs({
|
||||
template: createTemplate({
|
||||
personas: [
|
||||
{
|
||||
personaId: persona.id,
|
||||
runtime: null,
|
||||
model: null,
|
||||
role: null,
|
||||
backend: null,
|
||||
},
|
||||
],
|
||||
teams: [
|
||||
{
|
||||
teamId: "team-1",
|
||||
runtime: null,
|
||||
model: null,
|
||||
backend: null,
|
||||
},
|
||||
{
|
||||
teamId: "team-2",
|
||||
runtime: null,
|
||||
model: null,
|
||||
backend: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
personas: [persona],
|
||||
teams: [
|
||||
createTeam("team-1", [persona.id]),
|
||||
createTeam("team-2", [persona.id]),
|
||||
],
|
||||
runtimes: [runtime],
|
||||
lastRuntimeId: null,
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
inputs.map((input) => input.teamId ?? null),
|
||||
[null, "team-1", "team-2"],
|
||||
);
|
||||
});
|
||||
|
||||
test("dataAfterInitialLoad waits for missing query data", async () => {
|
||||
const expected = [createPersona("persona-1")];
|
||||
let refetched = false;
|
||||
|
||||
const result = await dataAfterInitialLoad({
|
||||
data: undefined,
|
||||
async refetch() {
|
||||
refetched = true;
|
||||
return { data: expected, error: null };
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(refetched, true);
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
|
||||
test("dataAfterInitialLoad reuses cached query data without refetching", async () => {
|
||||
const expected = [createPersona("persona-1")];
|
||||
|
||||
const result = await dataAfterInitialLoad({
|
||||
data: expected,
|
||||
async refetch() {
|
||||
assert.fail("cached data should not refetch");
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(result, expected);
|
||||
});
|
||||
115
desktop/src/features/channel-templates/templateAgentInputs.ts
Normal file
115
desktop/src/features/channel-templates/templateAgentInputs.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import type { CreateChannelManagedAgentInput } from "@/features/agents/channelAgents";
|
||||
import { resolvePersonaRuntime } from "@/features/agents/lib/resolvePersonaRuntime";
|
||||
import { resolveTeamPersonas } from "@/features/agents/lib/teamPersonas";
|
||||
import type {
|
||||
AcpRuntime,
|
||||
AgentPersona,
|
||||
AgentTeam,
|
||||
ChannelTemplate,
|
||||
} from "@/shared/api/types";
|
||||
|
||||
type InitialLoadQuery<T> = {
|
||||
data: T | undefined;
|
||||
refetch: () => Promise<{ data: T | undefined; error: unknown }>;
|
||||
};
|
||||
|
||||
/** Return cached query data, or wait for its initial request to finish. */
|
||||
export async function dataAfterInitialLoad<T>(
|
||||
query: InitialLoadQuery<T>,
|
||||
): Promise<T> {
|
||||
if (query.data !== undefined) return query.data;
|
||||
|
||||
const result = await query.refetch();
|
||||
if (result.data !== undefined) return result.data;
|
||||
|
||||
throw (
|
||||
result.error ?? new Error("Required template data could not be loaded.")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* TemplateBackend omits `config` — supply an empty object for provider backends.
|
||||
*/
|
||||
function toManagedBackend(
|
||||
backend: ChannelTemplate["agents"]["personas"][number]["backend"],
|
||||
): CreateChannelManagedAgentInput["backend"] {
|
||||
if (!backend || backend.type === "local") return { type: "local" };
|
||||
return { type: "provider", id: backend.id, config: {} };
|
||||
}
|
||||
|
||||
export function buildTemplateAgentInputs({
|
||||
template,
|
||||
personas,
|
||||
teams,
|
||||
runtimes,
|
||||
lastRuntimeId,
|
||||
}: {
|
||||
template: ChannelTemplate;
|
||||
personas: AgentPersona[];
|
||||
teams: readonly AgentTeam[];
|
||||
runtimes: readonly AcpRuntime[];
|
||||
lastRuntimeId: string | null;
|
||||
}): CreateChannelManagedAgentInput[] {
|
||||
const defaultProvider =
|
||||
runtimes.find((runtime) => runtime.id === lastRuntimeId) ??
|
||||
runtimes[0] ??
|
||||
null;
|
||||
if (!defaultProvider) return [];
|
||||
|
||||
const directPersonaIds = new Set<string>();
|
||||
const teamPersonaKeys = new Set<string>();
|
||||
const inputs: CreateChannelManagedAgentInput[] = [];
|
||||
|
||||
for (const entry of template.agents.personas) {
|
||||
const persona = personas.find(
|
||||
(candidate) => candidate.id === entry.personaId,
|
||||
);
|
||||
if (!persona || directPersonaIds.has(persona.id)) continue;
|
||||
directPersonaIds.add(persona.id);
|
||||
const resolved = resolvePersonaRuntime(
|
||||
entry.runtime ?? persona.runtime,
|
||||
runtimes,
|
||||
defaultProvider,
|
||||
);
|
||||
inputs.push({
|
||||
runtime: resolved.runtime ?? defaultProvider,
|
||||
name: persona.displayName,
|
||||
personaId: persona.id,
|
||||
systemPrompt: persona.systemPrompt,
|
||||
avatarUrl: persona.avatarUrl ?? undefined,
|
||||
model: entry.model ?? persona.model ?? undefined,
|
||||
role: "bot",
|
||||
backend: toManagedBackend(entry.backend),
|
||||
});
|
||||
}
|
||||
|
||||
for (const teamEntry of template.agents.teams) {
|
||||
const team = teams.find((candidate) => candidate.id === teamEntry.teamId);
|
||||
if (!team) continue;
|
||||
const { resolvedPersonas } = resolveTeamPersonas(team, personas);
|
||||
for (const persona of resolvedPersonas) {
|
||||
const teamPersonaKey = `${team.id}\0${persona.id}`;
|
||||
if (teamPersonaKeys.has(teamPersonaKey)) continue;
|
||||
teamPersonaKeys.add(teamPersonaKey);
|
||||
const resolved = resolvePersonaRuntime(
|
||||
teamEntry.runtime ?? persona.runtime,
|
||||
runtimes,
|
||||
defaultProvider,
|
||||
);
|
||||
inputs.push({
|
||||
runtime: resolved.runtime ?? defaultProvider,
|
||||
name: persona.displayName,
|
||||
personaId: persona.id,
|
||||
teamId: team.id,
|
||||
forceNewInstance: true,
|
||||
systemPrompt: persona.systemPrompt,
|
||||
avatarUrl: persona.avatarUrl ?? undefined,
|
||||
model: teamEntry.model ?? persona.model ?? undefined,
|
||||
role: "bot",
|
||||
backend: toManagedBackend(teamEntry.backend),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return inputs;
|
||||
}
|
||||
@@ -1,29 +1,28 @@
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { createChannelManagedAgents } from "@/features/agents/channelAgents";
|
||||
import {
|
||||
createChannelManagedAgents,
|
||||
type CreateChannelManagedAgentInput,
|
||||
} from "@/features/agents/channelAgents";
|
||||
import {
|
||||
useAvailableAcpRuntimes,
|
||||
useAcpRuntimesQuery,
|
||||
usePersonasQuery,
|
||||
useTeamsQuery,
|
||||
} from "@/features/agents/hooks";
|
||||
import { resolvePersonaRuntime } from "@/features/agents/lib/resolvePersonaRuntime";
|
||||
import { resolveTeamPersonas } from "@/features/agents/lib/teamPersonas";
|
||||
import {
|
||||
buildTemplateAgentInputs,
|
||||
dataAfterInitialLoad,
|
||||
} from "@/features/channel-templates/templateAgentInputs";
|
||||
import { useLastRuntime } from "@/features/agents/lib/useLastRuntime";
|
||||
import { useChannelTemplatesQuery } from "@/features/channel-templates/hooks";
|
||||
import { setCanvas } from "@/shared/api/tauri";
|
||||
import type { ChannelTemplate } from "@/shared/api/types";
|
||||
import type {
|
||||
AcpRuntime,
|
||||
AcpRuntimeCatalogEntry,
|
||||
ChannelTemplate,
|
||||
} from "@/shared/api/types";
|
||||
|
||||
/**
|
||||
* TemplateBackend omits `config` — supply an empty object for provider backends.
|
||||
*/
|
||||
function toManagedBackend(
|
||||
backend: ChannelTemplate["agents"]["personas"][number]["backend"],
|
||||
): CreateChannelManagedAgentInput["backend"] {
|
||||
if (!backend || backend.type === "local") return { type: "local" };
|
||||
return { type: "provider", id: backend.id, config: {} };
|
||||
function isAvailableRuntime(
|
||||
runtime: AcpRuntimeCatalogEntry,
|
||||
): runtime is AcpRuntime {
|
||||
return runtime.availability === "available";
|
||||
}
|
||||
|
||||
export function buildProjectFolderCanvas(
|
||||
@@ -58,7 +57,7 @@ export function buildWorktreeCanvas(
|
||||
export function useApplyTemplate() {
|
||||
const queryClient = useQueryClient();
|
||||
const channelTemplatesQuery = useChannelTemplatesQuery();
|
||||
const acpRuntimesQuery = useAvailableAcpRuntimes();
|
||||
const acpRuntimesQuery = useAcpRuntimesQuery();
|
||||
const personasQuery = usePersonasQuery();
|
||||
const teamsQuery = useTeamsQuery();
|
||||
const { lastRuntimeId } = useLastRuntime();
|
||||
@@ -114,71 +113,24 @@ export function useApplyTemplate() {
|
||||
template.agents;
|
||||
if (templatePersonas.length === 0 && templateTeams.length === 0) return;
|
||||
|
||||
const allPersonas = personasQuery.data ?? [];
|
||||
const allTeams = teamsQuery.data ?? [];
|
||||
const runtimes = acpRuntimesQuery.data ?? [];
|
||||
if (runtimes.length === 0) return; // No runtimes — skip silently
|
||||
|
||||
// Resolve default provider: user's last-used preference, or first available
|
||||
const defaultProvider =
|
||||
runtimes.find((p) => p.id === lastRuntimeId) ?? runtimes[0] ?? null;
|
||||
if (!defaultProvider) return;
|
||||
|
||||
const seenPersonaIds = new Set<string>();
|
||||
const inputs: CreateChannelManagedAgentInput[] = [];
|
||||
|
||||
// Direct personas from template
|
||||
for (const entry of templatePersonas) {
|
||||
const persona = allPersonas.find((p) => p.id === entry.personaId);
|
||||
if (!persona) continue;
|
||||
if (seenPersonaIds.has(persona.id)) continue;
|
||||
seenPersonaIds.add(persona.id);
|
||||
const resolved = resolvePersonaRuntime(
|
||||
entry.runtime ?? persona.runtime,
|
||||
runtimes,
|
||||
defaultProvider,
|
||||
);
|
||||
inputs.push({
|
||||
runtime: resolved.runtime ?? defaultProvider,
|
||||
name: persona.displayName,
|
||||
personaId: persona.id,
|
||||
systemPrompt: persona.systemPrompt,
|
||||
avatarUrl: persona.avatarUrl ?? undefined,
|
||||
model: entry.model ?? persona.model ?? undefined,
|
||||
role: "bot",
|
||||
backend: toManagedBackend(entry.backend),
|
||||
});
|
||||
}
|
||||
|
||||
// Team-expanded personas (skip dupes)
|
||||
for (const teamEntry of templateTeams) {
|
||||
const team = allTeams.find((t) => t.id === teamEntry.teamId);
|
||||
if (!team) continue;
|
||||
const { resolvedPersonas } = resolveTeamPersonas(team, allPersonas);
|
||||
for (const persona of resolvedPersonas) {
|
||||
if (seenPersonaIds.has(persona.id)) continue;
|
||||
seenPersonaIds.add(persona.id);
|
||||
const resolved = resolvePersonaRuntime(
|
||||
teamEntry.runtime ?? persona.runtime,
|
||||
runtimes,
|
||||
defaultProvider,
|
||||
);
|
||||
inputs.push({
|
||||
runtime: resolved.runtime ?? defaultProvider,
|
||||
name: persona.displayName,
|
||||
personaId: persona.id,
|
||||
systemPrompt: persona.systemPrompt,
|
||||
avatarUrl: persona.avatarUrl ?? undefined,
|
||||
model: teamEntry.model ?? persona.model ?? undefined,
|
||||
role: "bot",
|
||||
backend: toManagedBackend(teamEntry.backend),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (inputs.length === 0) return;
|
||||
|
||||
try {
|
||||
const [allPersonas, allTeams, runtimeCatalog] = await Promise.all([
|
||||
dataAfterInitialLoad(personasQuery),
|
||||
dataAfterInitialLoad(teamsQuery),
|
||||
dataAfterInitialLoad(acpRuntimesQuery),
|
||||
]);
|
||||
const runtimes = runtimeCatalog.filter(isAvailableRuntime);
|
||||
if (runtimes.length === 0) return; // No runtimes — skip silently
|
||||
|
||||
const inputs = buildTemplateAgentInputs({
|
||||
template,
|
||||
personas: allPersonas,
|
||||
teams: allTeams,
|
||||
runtimes,
|
||||
lastRuntimeId,
|
||||
});
|
||||
if (inputs.length === 0) return;
|
||||
|
||||
const result = await createChannelManagedAgents(channelId, inputs);
|
||||
if (result.failures.length > 0) {
|
||||
const { toast } = await import("sonner");
|
||||
|
||||
Reference in New Issue
Block a user