diff --git a/README.md b/README.md index 414e4b02c..a2c862c01 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ The local backend (`start_local.sh`) accepts requests from any `http://localhost **Projects** groups pipelines, runs and documents, and every project belongs to a workspace. **Tangent** turns a project into a chat with an agent that can build, run and debug its pipelines. Both are beta features and are off by default. Turn them on under **Settings → Beta Features** once you have a backend that supports them: - **Projects** needs a Tangle backend that includes the Projects API (workspaces and projects). The released open-source backend does not include it yet. The backend also needs at least one active workspace. An admin can create one with **Create Workspace** in the backend's API docs (`/docs`). -- **Tangent** needs Projects, a running [Tangent shell](https://github.com/TangleML/tangent-shell), and the app started with `VITE_ENABLE_TANGENT=true`; without that variable the Tangent toggle stays locked. In development the app looks for the shell at `http://localhost:5173`. `pnpm oss:dev` sets up and starts the shell next to the app. +- **Tangent** needs Projects, a running [Tangent shell](https://github.com/TangleML/tangent-shell), and the app started with `VITE_ENABLE_TANGENT=true`; without that variable the Tangent toggle stays locked. In development the app looks for the shell at `http://localhost:5173`. Sessions use the agent bundle named in the workspace's data as `tangentBundleId`, or `tangle` when it isn't set; the open-source shell ships `tangle-oss`, so a workspace that uses it sets `"tangentBundleId": "tangle-oss"`. `pnpm oss:dev` sets up and starts the shell next to the app. ### AI provider configuration diff --git a/src/routes/v2/pages/Tangent/components/AttachAgentButton.test.tsx b/src/routes/v2/pages/Tangent/components/AttachAgentButton.test.tsx index d7b77f8ea..545aa3602 100644 --- a/src/routes/v2/pages/Tangent/components/AttachAgentButton.test.tsx +++ b/src/routes/v2/pages/Tangent/components/AttachAgentButton.test.tsx @@ -39,6 +39,7 @@ function mockBaseUrl(baseUrl: string | null) { vi.mocked(useTangentBaseUrl).mockReturnValue({ baseUrl, localAddress: null, + bundleId: "tangle", isLoading: false, isError: false, }); diff --git a/src/routes/v2/pages/Tangent/components/LocalEnvIndicator.test.tsx b/src/routes/v2/pages/Tangent/components/LocalEnvIndicator.test.tsx index e5958d4aa..c143461a7 100644 --- a/src/routes/v2/pages/Tangent/components/LocalEnvIndicator.test.tsx +++ b/src/routes/v2/pages/Tangent/components/LocalEnvIndicator.test.tsx @@ -50,6 +50,7 @@ function mockBaseUrl(baseUrl: string | null) { vi.mocked(useTangentBaseUrl).mockReturnValue({ baseUrl, localAddress: null, + bundleId: "tangle", isLoading: false, isError: false, }); diff --git a/src/routes/v2/pages/Tangent/context/TangentProjectContext.tsx b/src/routes/v2/pages/Tangent/context/TangentProjectContext.tsx index e29eecdcd..74a7a790c 100644 --- a/src/routes/v2/pages/Tangent/context/TangentProjectContext.tsx +++ b/src/routes/v2/pages/Tangent/context/TangentProjectContext.tsx @@ -50,6 +50,7 @@ function useProjectStore( askedSessionId?: string; userId?: string; tangentBaseUrl: string | null; + bundleId: string; }, ): TangentProjectStore { const create = () => { @@ -61,6 +62,7 @@ function useProjectStore( : undefined, ); store.setTangentBaseUrl(tangentBaseUrl); + store.setBundleId(seed.bundleId); store.setDefaultSessionId(seed.defaultSessionId); if (seed.askedSessionId) store.selectSession(seed.askedSessionId); return store; @@ -84,13 +86,14 @@ export function TangentProjectProvider({ } = useProjectSessions(projectId); const ensureSharedFolderId = useProjectSharedFolder(projectId); const asked = readTangentSessionParam(useSearch({ strict: false })); - const { baseUrl } = useTangentBaseUrl(projectId); + const { baseUrl, bundleId } = useTangentBaseUrl(projectId); const queryClient = useQueryClient(); const store = useProjectStore(projectId, { defaultSessionId: sessions[0]?.sessionId, askedSessionId: asked?.kind === "existing" ? asked.sessionId : undefined, userId: queryClient.getQueryData(userQueryOptions.queryKey)?.id, tangentBaseUrl: baseUrl, + bundleId, }); const storage = usePipelineStorage(); const recordSessionRun = useRecordSessionRun(); @@ -135,6 +138,10 @@ export function TangentProjectProvider({ store.setDefaultSessionId(defaultSessionId); }, [store, defaultSessionId]); + useEffect(() => { + store.setBundleId(bundleId); + }, [store, bundleId]); + const authToken = useRemoteEnvAuthToken(); useBackfillSessionTags({ projectId, diff --git a/src/routes/v2/pages/Tangent/hooks/useProjectSharedFolder.test.tsx b/src/routes/v2/pages/Tangent/hooks/useProjectSharedFolder.test.tsx index 033533586..6b5d1d82e 100644 --- a/src/routes/v2/pages/Tangent/hooks/useProjectSharedFolder.test.tsx +++ b/src/routes/v2/pages/Tangent/hooks/useProjectSharedFolder.test.tsx @@ -51,6 +51,7 @@ function given( vi.mocked(useTangentBaseUrl).mockReturnValue({ baseUrl, localAddress: null, + bundleId: "tangle", isLoading: false, isError: false, }); diff --git a/src/routes/v2/pages/Tangent/hooks/useTangentBaseUrl.ts b/src/routes/v2/pages/Tangent/hooks/useTangentBaseUrl.ts index 1add7a564..9f8f86d9e 100644 --- a/src/routes/v2/pages/Tangent/hooks/useTangentBaseUrl.ts +++ b/src/routes/v2/pages/Tangent/hooks/useTangentBaseUrl.ts @@ -1,10 +1,15 @@ -import { resolveTangentBaseUrl } from "@/routes/v2/pages/Tangent/services/tangentBaseUrl"; +import { TANGENT_BUNDLE_ID } from "@/routes/v2/pages/Tangent/constants"; +import { + resolveTangentBaseUrl, + resolveTangentBundleId, +} from "@/routes/v2/pages/Tangent/services/tangentBaseUrl"; import { useProject } from "@/services/projects/useProjects"; import { useWorkspace } from "@/services/projects/useWorkspaces"; interface TangentBaseUrl { baseUrl: string | null; localAddress: string | null; + bundleId: string; isLoading: boolean; isError: boolean; } @@ -33,6 +38,9 @@ export function useTangentBaseUrl(projectId: string): TangentBaseUrl { return { baseUrl: resolution?.baseUrl ?? null, localAddress: resolution?.localAddress ?? null, + bundleId: workspace + ? resolveTangentBundleId(workspace.metadata) + : TANGENT_BUNDLE_ID, isLoading: isProjectLoading || isWorkspaceLoading, isError: isProjectError || isWorkspaceError, }; diff --git a/src/routes/v2/pages/Tangent/services/sessionStartError.ts b/src/routes/v2/pages/Tangent/services/sessionStartError.ts index 10ac8c1a4..f77394479 100644 --- a/src/routes/v2/pages/Tangent/services/sessionStartError.ts +++ b/src/routes/v2/pages/Tangent/services/sessionStartError.ts @@ -1,4 +1,3 @@ -import { TANGENT_BUNDLE_ID } from "@/routes/v2/pages/Tangent/constants"; import { getErrorMessage, safeJsonParse } from "@/utils/string"; import { isRecord } from "@/utils/typeGuards"; @@ -18,9 +17,12 @@ export function isMissingBundleError(error: unknown): boolean { return tangentMessage(error) === MISSING_BUNDLE; } -export function describeSessionStartError(error: unknown): string { +export function describeSessionStartError( + error: unknown, + bundleId: string, +): string { if (isMissingBundleError(error)) { - return `This Tangent server doesn't have the "${TANGENT_BUNDLE_ID}" agent bundle installed.`; + return `This Tangent server doesn't have the "${bundleId}" agent bundle installed. To use a different bundle, set "tangentBundleId" in the workspace's data.`; } return tangentMessage(error); } diff --git a/src/routes/v2/pages/Tangent/services/tangentBaseUrl.test.ts b/src/routes/v2/pages/Tangent/services/tangentBaseUrl.test.ts index 912169bc9..f3045b3ba 100644 --- a/src/routes/v2/pages/Tangent/services/tangentBaseUrl.test.ts +++ b/src/routes/v2/pages/Tangent/services/tangentBaseUrl.test.ts @@ -1,8 +1,14 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { DEV_TANGENT_BASE_URL } from "@/routes/v2/pages/Tangent/constants"; +import { + DEV_TANGENT_BASE_URL, + TANGENT_BUNDLE_ID, +} from "@/routes/v2/pages/Tangent/constants"; -import { resolveTangentBaseUrl } from "./tangentBaseUrl"; +import { + resolveTangentBaseUrl, + resolveTangentBundleId, +} from "./tangentBaseUrl"; afterEach(() => { vi.unstubAllEnvs(); @@ -110,3 +116,20 @@ describe("resolveTangentBaseUrl", () => { }); }); }); + +describe("resolveTangentBundleId", () => { + it("uses the bundle the workspace names", () => { + expect(resolveTangentBundleId({ tangentBundleId: " tangle-oss " })).toBe( + "tangle-oss", + ); + }); + + it.each([ + ["no metadata", null], + ["no tangentBundleId", { tangentBaseUrl: "https://tangent.example.com" }], + ["a blank tangentBundleId", { tangentBundleId: " " }], + ["a non-string tangentBundleId", { tangentBundleId: 7 }], + ])("falls back to the default bundle for %s", (_case, metadata) => { + expect(resolveTangentBundleId(metadata)).toBe(TANGENT_BUNDLE_ID); + }); +}); diff --git a/src/routes/v2/pages/Tangent/services/tangentBaseUrl.ts b/src/routes/v2/pages/Tangent/services/tangentBaseUrl.ts index df64cfd67..71314e907 100644 --- a/src/routes/v2/pages/Tangent/services/tangentBaseUrl.ts +++ b/src/routes/v2/pages/Tangent/services/tangentBaseUrl.ts @@ -1,4 +1,7 @@ -import { DEV_TANGENT_BASE_URL } from "@/routes/v2/pages/Tangent/constants"; +import { + DEV_TANGENT_BASE_URL, + TANGENT_BUNDLE_ID, +} from "@/routes/v2/pages/Tangent/constants"; import { isRecord } from "@/utils/typeGuards"; export interface TangentBaseUrlResolution { @@ -73,3 +76,18 @@ export function resolveTangentBaseUrl( return { baseUrl: normalized, localAddress: null }; } + +/** + * The agent bundle that backs sessions started in a workspace. Defaults to + * {@link TANGENT_BUNDLE_ID} so a workspace that doesn't name one keeps the + * standard Tangle bundle. + */ +export function resolveTangentBundleId( + metadata: Record | null | undefined, +): string { + if (!isRecord(metadata)) return TANGENT_BUNDLE_ID; + const { tangentBundleId } = metadata; + if (typeof tangentBundleId !== "string") return TANGENT_BUNDLE_ID; + const trimmed = tangentBundleId.trim(); + return trimmed.length > 0 ? trimmed : TANGENT_BUNDLE_ID; +} diff --git a/src/routes/v2/pages/Tangent/store/TangentProjectStore.test.ts b/src/routes/v2/pages/Tangent/store/TangentProjectStore.test.ts index be30420ac..c005f5cc4 100644 --- a/src/routes/v2/pages/Tangent/store/TangentProjectStore.test.ts +++ b/src/routes/v2/pages/Tangent/store/TangentProjectStore.test.ts @@ -800,12 +800,31 @@ describe("TangentProjectStore.startSession", () => { await expect(store.startSession()).resolves.toBe(false); - const message = `This Tangent server doesn't have the "${TANGENT_BUNDLE_ID}" agent bundle installed.`; + const message = `This Tangent server doesn't have the "${TANGENT_BUNDLE_ID}" agent bundle installed. To use a different bundle, set "tangentBundleId" in the workspace's data.`; expect(io.newSession).toHaveBeenCalledTimes(1); expect(io.notify).toHaveBeenCalledWith(message, "error"); expect(store.sessionStartError).toBe(message); }); + it("starts sessions with the workspace's bundle and names it when it is missing", async () => { + const store = new TangentProjectStore("project-1"); + const io = makeSessionIo(null); + io.newSession.mockRejectedValue( + new Error('{"error":"Agent bundle not found"}'), + ); + store.setSessionIo(io); + store.setBundleId("tangle-oss"); + + await store.startSession(); + + expect(io.newSession).toHaveBeenCalledWith( + "", + "tangle-oss", + expect.anything(), + ); + expect(store.sessionStartError).toContain('the "tangle-oss" agent bundle'); + }); + it("reads the message out of a refusal's JSON body", async () => { const store = new TangentProjectStore("project-1"); const io = makeSessionIo(null); diff --git a/src/routes/v2/pages/Tangent/store/TangentProjectStore.ts b/src/routes/v2/pages/Tangent/store/TangentProjectStore.ts index 4b7e4818d..f9c56e5de 100644 --- a/src/routes/v2/pages/Tangent/store/TangentProjectStore.ts +++ b/src/routes/v2/pages/Tangent/store/TangentProjectStore.ts @@ -134,6 +134,7 @@ export class TangentProjectStore { @observable.ref accessor openTabs: OpenTabs; @observable accessor tangentBaseUrl: string | null = null; + @observable accessor bundleId: string = TANGENT_BUNDLE_ID; @observable.shallow accessor chatBySession = new Map(); // Live tab wiring is intentionally non-observable: it's registered/read by the @@ -165,6 +166,10 @@ export class TangentProjectStore { this.tangentBaseUrl = baseUrl; } + @action setBundleId(bundleId: string) { + this.bundleId = bundleId; + } + @action setSessionIo(io: TangentSessionIo) { this.#io = io; } @@ -242,6 +247,7 @@ export class TangentProjectStore { const io = this.#io; if (!io) return false; const previous = this.activeSessionId; + const bundleId = this.bundleId; runInAction(() => { this.isStartingSession = true; this.sessionStartError = undefined; @@ -279,7 +285,7 @@ export class TangentProjectStore { memory: shared?.memory ?? BUNDLE_KNOWLEDGE_BINDING, }); const { sessionId } = await io - .newSession(prompt, TANGENT_BUNDLE_ID, sessionOptions(sharedMemory)) + .newSession(prompt, bundleId, sessionOptions(sharedMemory)) .catch((error: unknown) => { if (!sharedMemory || isMissingBundleError(error)) throw error; // The stored folder id may be unknown to this Tangent server (base @@ -289,11 +295,7 @@ export class TangentProjectStore { "Failed to start a session with the project's shared folder; retrying without it", error, ); - return io.newSession( - prompt, - TANGENT_BUNDLE_ID, - sessionOptions(undefined), - ); + return io.newSession(prompt, bundleId, sessionOptions(undefined)); }); const resource = await io.attachSession(sessionId, options?.name); runInAction(() => { @@ -306,7 +308,7 @@ export class TangentProjectStore { } return true; } catch (error) { - const message = describeSessionStartError(error); + const message = describeSessionStartError(error, bundleId); runInAction(() => { this.sessionStartError = message; });