Files
windmill/frontend/src/lib/components/operatingWorkspace.svelte.ts
d6c2b2c683 feat: add a use/plan/build mode picker and read-only prod sessions (#11940)
* feat: read-only ai sessions on protected workspaces with a use, plan and build picker

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: address local review findings on the use, plan and build picker

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: address CI review findings on use mode and protected sessions

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: hold test-run forms and pending protection lookups to use mode

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor: fetch rules once per send and dedupe helpers and prompt text

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: send role-locked operators to an admin and cover it with an eval

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: count the work mode each session message runs in

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore: regenerate the mcp session definitions after merging main

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat: rename build to use & build and let the chat ask to switch modes

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: word a declined mode switch by the switch, not the mode

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore: update ee-repo-ref to 55445977dd05c25791388070182c77f4dbc20c0e

This commit updates the EE repository reference after PR #915 was merged in windmill-ee-private.

Previous ee-repo-ref: 7f7bf7003e356346ce74395af02c5bdf14f38781

New ee-repo-ref: 55445977dd05c25791388070182c77f4dbc20c0e

Automated by sync-ee-ref workflow.

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
2026-10-23 15:53:04 +02:00

75 lines
3.9 KiB
TypeScript

import { getContext, setContext } from 'svelte'
import { fromStore, toStore, type Readable } from 'svelte/store'
import { workspaceStore } from '$lib/stores'
import { withWorkspaceParam } from './sessions/sessionMode.svelte'
import { useActingUser } from '$lib/actingUser.svelte'
// The workspace a subtree acts on. An AI session edits its (possibly forked) workspace while
// `workspaceStore` stays on the navigation workspace, and a component that reads the navigation
// store directly writes to the parent from inside a fork's editor. So the hosts that embed an
// editor for another workspace set it once, and every component under them reads
// `useOperatingWorkspace()` instead of `$workspaceStore` — nothing has to thread it through props.
// Outside such a host it is the navigation store itself.
const KEY = Symbol('operatingWorkspace')
const navigation = fromStore(workspaceStore)
/** Declare the workspace this subtree acts on. A getter, read wherever the workspace is used;
* resolving to nothing defers to the enclosing host. Reads context: call during component
* initialisation. */
export function setOperatingWorkspace(resolve: () => string | undefined): void {
const outer = getContext<(() => string | undefined) | undefined>(KEY)
setContext(KEY, outer ? () => resolve() ?? outer() : resolve)
}
const READ_ONLY_KEY = Symbol('readOnlyWorkspace')
/** Declare which workspaces this subtree may not change: a session in Use mode. `useEditRights`
* refuses every check there, so each control gated on it, the operator gates included, follows.
* Reads context: call during component initialisation. */
export function setReadOnlyWorkspace(isReadOnly: (workspace: string | undefined) => boolean): void {
setContext(READ_ONLY_KEY, isReadOnly)
}
/** Whether this subtree may not change `workspace`. Reads context: call during component
* initialisation. */
export function useReadOnlyWorkspace(): (workspace: string | undefined) => boolean {
return (
getContext<((workspace: string | undefined) => boolean) | undefined>(READ_ONLY_KEY) ??
(() => false)
)
}
/** This subtree's operating workspace as an entry for a `ContextBridge` contexts map. A snippet
* handed to the page header's band renders outside the subtree that declared the workspace, so
* without this it would quietly act on the navigation workspace instead. Reads context: call
* during component initialisation. */
export function operatingWorkspaceContextEntry(): [any, any] {
return [KEY, getContext(KEY)]
}
/** The workspace this component acts on, as a store. Reads context: call during component
* initialisation. Falls back to the navigation workspace where no host set one, or where the
* host's resolves to nothing. */
export function useOperatingWorkspace(): Readable<string | undefined> {
const resolve = getContext<(() => string | undefined) | undefined>(KEY)
return resolve ? toStore(() => resolve() ?? navigation.current) : workspaceStore
}
/** The user acting in this subtree's operating workspace: `$userStore` describes the navigation
* workspace, so a permission check reading it inside a fork's editor answers about the parent —
* enabling or disabling controls by the wrong roles. Unresolved reads as unknown, which
* `canWrite`/`isOwner` refuse; ask `resolved` before rendering that as a denial. Reads context
* and registers an effect: call during component initialisation. */
export function useOperatingUser(): ReturnType<typeof useActingUser> {
const operating = fromStore(useOperatingWorkspace())
return useActingUser(() => operating.current)
}
/** Stamps this component's operating workspace onto an in-app link, which otherwise opens in
* the navigation workspace. Reads context: call during component initialisation. */
export function useOperatingWorkspaceHref(): (href: string) => string {
const operating = fromStore(useOperatingWorkspace())
return (href) => withWorkspaceParam(href, operating.current)
}