Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ export function TangentProjectAgentProvider({
attach: (input) => createProjectResource(store.projectId, input),
});
await refreshProjectResources();
return { pipelineName: file.storageKey, fileId: file.id };
return {
pipelineName: file.storageKey,
target: pipelineFileTarget(file),
};
},
readSessionResource: async (uri) => {
const read = readSessionResourceRef.current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,15 @@ describe("createWorkareaRemoteTools", () => {
});

it("import_pipeline imports the YAML, opens it, and reports the tab it can spawn into", async () => {
const importPipeline = vi
.fn()
.mockResolvedValue({ pipelineName: "Imported DAG", fileId: "file-7" });
const tab = { ...pipelineTab("tab-imp", "file-7"), title: "Imported DAG" };
const importPipeline = vi.fn().mockResolvedValue({
pipelineName: "Imported DAG",
target: { type: "pipeline", identity: "remote/file-7" },
});
const tab = {
...pipelineTab("tab-imp"),
target: { type: "pipeline", identity: "remote/file-7" } as const,
title: "Imported DAG",
};
const openTarget = vi.fn().mockResolvedValue(tab);
const tools = createWorkareaRemoteTools(() =>
makeDeps({
Expand All @@ -424,15 +429,15 @@ describe("createWorkareaRemoteTools", () => {
"Imported DAG",
);
expect(openTarget).toHaveBeenCalledWith(
{ type: "pipeline", identity: "id/file-7" },
{ type: "pipeline", identity: "remote/file-7" },
"Imported DAG",
);
expect(result).toEqual({
id: "tab-imp",
kind: "pipeline",
name: "Imported DAG",
title: "Imported DAG",
target: "pipeline://id/file-7",
target: "pipeline://remote/file-7",
active: true,
environmentId: "env-7",
ready: true,
Expand All @@ -443,9 +448,10 @@ describe("createWorkareaRemoteTools", () => {
const yaml =
"name: From Resource\nimplementation:\n graph:\n tasks: {}\n";
const readSessionResource = vi.fn().mockResolvedValue(yaml);
const importPipeline = vi
.fn()
.mockResolvedValue({ pipelineName: "From Resource", fileId: "file-r" });
const importPipeline = vi.fn().mockResolvedValue({
pipelineName: "From Resource",
target: { type: "pipeline", identity: idIdentity("file-r") },
});
const tab = { ...pipelineTab("tab-r", "file-r"), title: "From Resource" };
const tools = createWorkareaRemoteTools(() =>
makeDeps({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface WorkareaToolDeps {
importPipeline: (
yaml: string,
name?: string,
) => Promise<{ pipelineName: string; fileId: string }>;
) => Promise<{ pipelineName: string; target: WorkareaTarget }>;
readSessionResource: (uri: string) => Promise<string>;
clonePipeline: (
runId: string,
Expand Down Expand Up @@ -331,10 +331,10 @@ export function createWorkareaRemoteTools(
"handle of a Tangent session resource holding the pipeline YAML, which " +
"is how you bring in a pipeline another agent contributed — or inline " +
"`yaml`. The content must be a graph pipeline spec. `name` is optional " +
"and is made unique if it is already taken; otherwise the name from the " +
"YAML is used. The pipeline is attached to the project automatically " +
"and overrides the name in the YAML. " +
"The pipeline is attached to the project automatically " +
"(it shows up in Resources). Returns the tab summary plus the pipeline " +
"`name` and its `pipeline://id/<fileId>` `target`, including the " +
"`name` and its `target`, including the " +
"`environmentId` and `ready` flag for that tab's sub-agent host, so a " +
"spawn can follow straight on.",
inputSchema: {
Expand Down Expand Up @@ -374,14 +374,10 @@ export function createWorkareaRemoteTools(
"Provide a `resourceUri` to a session resource, or inline `yaml`.",
);
}
const { pipelineName, fileId } = await deps.importPipeline(
const { pipelineName, target } = await deps.importPipeline(
pipelineYaml,
optionalName(args),
);
const target: WorkareaTarget = {
type: "pipeline",
identity: idIdentity(fileId),
};
const tab = await openAndSummarize(getDeps, target, pipelineName);
return { ...tab, name: pipelineName };
},
Expand Down
32 changes: 31 additions & 1 deletion src/services/pipelineStorage/PipelineStorageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { LibraryDB } from "@/providers/ComponentLibraryProvider/libraries/storag
import { TourPipelineStorageService } from "@/providers/TourProvider/tourPipelineStorage/TourPipelineStorageService";
import type { SavedPipelineDocument } from "@/services/pipelineSpecService";
import { migrateLocalPipelineOnOpen } from "@/services/pipelineStorage/migrateLocalPipelineOnOpen";
import { createAndAttachPipeline } from "@/services/projects/createAndAttachPipeline";
import {
createAndAttachPipeline,
importAndAttachPipeline,
} from "@/services/projects/createAndAttachPipeline";
import { pipelineFileTarget } from "@/services/projects/pipelineResource";
import {
createProjectResource,
Expand Down Expand Up @@ -751,6 +754,33 @@ describe("remote drafts and project attachment", () => {
},
);

it("attaches an imported graph using the saved backend pipeline ID", async () => {
const attach = vi.fn().mockResolvedValue(undefined);
const localWrite = vi.spyOn(storage.rootFolder, "addFile");
const file = await importAndAttachPipeline({
storage,
yaml: content,
attach,
});
expect(attach).toHaveBeenCalledWith({
entity: "pipeline",
entityId: ref.pipelineId,
name: editedSpec.name,
});
expect(pipelineFileTarget(file).identity).toBe(`remote/${ref.pipelineId}`);
expect(localWrite).not.toHaveBeenCalled();
});

it("keeps the saved import when project attachment cannot be confirmed", async () => {
const remove = vi.spyOn(PipelineFile.prototype, "deleteFile");
const attach = vi.fn().mockRejectedValue(new Error("Response lost"));
await expect(
importAndAttachPipeline({ storage, yaml: content, attach }),
).rejects.toThrow(`Pipeline ${ref.pipelineId} was saved`);
expect(client.put).toHaveBeenCalledOnce();
expect(remove).not.toHaveBeenCalled();
});

it("rolls back a local pipeline when attachment is refused", async () => {
vi.stubEnv("VITE_REMOTE_PIPELINES", "false");
const deleteFile = vi.fn().mockResolvedValue(undefined);
Expand Down
23 changes: 18 additions & 5 deletions src/services/projects/createAndAttachPipeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createNewPipeline } from "@/routes/v2/pages/Editor/components/EditorMenuBar/components/fileMenu.actions";
import { availablePipelineName } from "@/services/localPipelines/localPipelinesService";
import { getPipelineCreationBackendUrl } from "@/services/pipelineStorage/pipelineCreation";
import {
createPipelineFromContent,
getPipelineCreationBackendUrl,
} from "@/services/pipelineStorage/pipelineCreation";
import type { PipelineFile } from "@/services/pipelineStorage/PipelineFile";
import type { PipelineStorageService } from "@/services/pipelineStorage/PipelineStorageService";
import { isGraphImplementation } from "@/utils/componentSpec";
Expand Down Expand Up @@ -39,6 +42,12 @@ async function attachOrCleanup(
try {
await attach(pipelineFileResourceInput(file));
} catch (error) {
if (file.remote) {
throw new Error(
`Pipeline ${file.id} was saved, but its project attachment could not be confirmed. Check the project before importing again.`,
{ cause: error },
);
}
await file
.deleteFile()
.catch((cleanupError: unknown) =>
Expand Down Expand Up @@ -90,14 +99,18 @@ export async function importAndAttachPipeline({
);
}

const storageKey = await availablePipelineName(
name ?? componentSpec.name ?? "Imported Pipeline",
);
const backendUrl = await getPipelineCreationBackendUrl();
const requestedName = name ?? componentSpec.name ?? "Imported Pipeline";
const storageKey = backendUrl
? requestedName
: await availablePipelineName(requestedName);
componentSpec.name = storageKey;

const file = await storage.rootFolder.addFile(
const file = await createPipelineFromContent(
storage,
storageKey,
componentSpecToYaml(componentSpec),
{ backendUrl },
);

return attachOrCleanup(file, attach);
Expand Down
Loading