diff --git a/.agents/skills/frontend-code-review/SKILL.md b/.agents/skills/frontend-code-review/SKILL.md index e99d90c3590..b8f6b046e2e 100644 --- a/.agents/skills/frontend-code-review/SKILL.md +++ b/.agents/skills/frontend-code-review/SKILL.md @@ -5,14 +5,14 @@ description: Use only when the user explicitly requests a review or audit of fro # Frontend Code Review -Review the requested scope for concrete, reproducible regressions. This skill owns the review phase and routes directly to its bundled rule packs. For a combined review-and-fix request, establish findings before applying implementation or testing guidance. +Review the requested scope for concrete defects and violations of explicit project contracts. This skill owns review decisions; its references route to canonical rules without activating another skill's implementation workflow. ## Evidence First 1. Establish the review scope from the requested files or current diff. 2. Read the changed lines, their behavior owner, and the nearest scoped `AGENTS.md`. 3. Trace public consumers, generated contracts, primitive APIs, or runtime configuration only when they decide correctness. -4. Report only findings tied to an observable failure, violated contract, security boundary, or demonstrated maintenance risk. +4. Report findings tied to an observable failure, violated contract, security boundary, or demonstrated maintenance risk. Explicit team conventions are contracts: establish their scope and exceptions, and do not invent user impact to justify a convention finding. ## Rule Routing @@ -33,10 +33,10 @@ Read `packages/dify-ui/README.md`, `packages/dify-ui/AGENTS.md`, `packages/dify- - **P0**: security or privacy leak, data loss, production crash, or inaccessible critical workflow. - **P1**: user-visible regression, invalid API or authorization contract, hydration failure, or broken primary interaction. -- **P2**: concrete maintainability, performance, test, or accessibility defect likely to cause incorrect behavior. +- **P2**: concrete maintainability, performance, test, or accessibility defect, or a material violation of an explicit project contract. - **P3**: minor actionable cleanup; omit unless the user requested a thorough audit. -Lead with findings ordered by severity. Include a tight file and line reference, the failing contract or reproduction path, impact, and a concrete fix direction. If there are no findings, say `No issues found.` and state any material verification gap. Do not add praise sections, speculative risks, or an unsolicited offer to implement fixes. +Lead with findings ordered by severity. Include a tight file and line reference, the observed failure or applicable project rule, and a concrete fix direction. Explain the rule's applicability for convention findings; describe downstream consequences only when supported by evidence. When no findings remain, say so briefly and state any material verification gap. Do not add praise sections, speculative risks, or an unsolicited offer to implement fixes. [accessibility]: references/accessibility-ui.md [code-quality]: references/code-quality.md diff --git a/.agents/skills/frontend-code-review/references/accessibility-ui.md b/.agents/skills/frontend-code-review/references/accessibility-ui.md index eb9cdd47728..e8a841502e0 100644 --- a/.agents/skills/frontend-code-review/references/accessibility-ui.md +++ b/.agents/skills/frontend-code-review/references/accessibility-ui.md @@ -4,19 +4,15 @@ Accessibility findings are first-class review findings. Treat broken keyboard ac ## Review Evidence -Before finalizing UI or accessibility findings, fetch the latest Web Interface Guidelines as a required baseline: - -```text -https://raw.githubusercontent.com/vercel-labs/web-interface-guidelines/main/command.md -``` - -Do not treat that document as the complete accessibility rule set. Combine it with: +Use the sources relevant to the changed contract: - `packages/dify-ui/README.md`, `packages/dify-ui/AGENTS.md`, and the relevant primitive implementation when code uses `@langgenius/dify-ui/*`. - Base UI docs and local `.d.ts` contracts when primitive semantics, focus target, labels, or popup reachability are unclear. - MDN or relevant WAI-ARIA/browser standards when behavior, compatibility, or deprecation status matters. - The current feature's product semantics, because an accessible primitive can still be used in an inaccessible workflow. +Consult current official documentation or standards when these sources leave a behavior unresolved. The [Web Interface Guidelines] are an optional broader UI reference, not a required fetch for each review. + ## Semantic HTML Flag: @@ -63,7 +59,6 @@ Flag: - Missing stable `name` on form fields that submit or validate. - Incorrect input `type`, `inputMode`, `autoComplete`, or `spellCheck` for email, token, URL, number, search, code, or username fields. - Labels that are not clickable. -- Submit buttons disabled before a request starts, preventing normal submit behavior. - Non-submit buttons inside forms missing `type="button"`. - Errors not associated with fields or not reachable by screen readers. - Error recovery that does not focus or expose the first invalid field. @@ -77,10 +72,14 @@ Prefer visible labels and associate them through the appropriate field primitive Flag: -- Loading state without `aria-busy`, `role="status"`, or another accessible update path when it changes user interaction. +- Loading controls whose accessible name disappears, or whose user-relevant progress has no + feature-owned status path. Follow the Dify UI Button contract for focused loading buttons; do not + add `aria-busy` to a button as a generic pending-state substitute. +- The same pending state passed to both Dify UI Button `loading` and `disabled`, which duplicates + state ownership and obscures whether `disabled` expresses independent unavailability. - Spinner or decorative loading icon exposed to screen readers. - Disabled controls that hide the reason users cannot proceed. -- `aria-disabled` used without manually blocking click, Space, and Enter. +- Controls marked `aria-disabled` that still activate through supported pointer or keyboard input. Check the primitive's handling before requesting manual event guards. - Toasts, inline validation, or async status changes that are not announced when users need the update to continue. - Icon-only loading/error affordances without text or accessible status where the state matters. @@ -105,7 +104,7 @@ Use Popover for explanatory content, rich help, and infotips. Use Tooltip only a Flag: - Text in flex/grid children without `min-w-0` when it can overflow. -- Names, labels, file names, model names, workspace names, or user content lacking `truncate`, `line-clamp`, or `break-words`. +- Long names, labels, or user content that overflow, obscure adjacent controls, or become unreadable in supported layouts. - Right-side icons, badges, checks, or actions that shrink before the text area. - Empty arrays or empty strings rendering broken layout instead of an empty state. - Button, tab, badge, chip, menu item, or card text that can overlap sibling controls at common viewport widths. @@ -124,3 +123,4 @@ Flag: - Hardcoded dates, times, numbers, or currency formats instead of `Intl.*`. [Accessible names and descriptions]: ../../../../packages/dify-ui/docs/accessible-names-and-descriptions.md +[Web Interface Guidelines]: https://raw.githubusercontent.com/vercel-labs/web-interface-guidelines/main/command.md diff --git a/.agents/skills/frontend-code-review/references/code-quality.md b/.agents/skills/frontend-code-review/references/code-quality.md index 9e014a93229..8ecf4b7b122 100644 --- a/.agents/skills/frontend-code-review/references/code-quality.md +++ b/.agents/skills/frontend-code-review/references/code-quality.md @@ -33,7 +33,7 @@ Flag: - Generic color utilities where Dify semantic tokens exist. - Hardcoded magic class values for colors, spacing, radius, shadow, z-index, or typography when Dify tokens, component variants, or documented radius mappings exist. - `!` important modifiers or important CSS overrides without a narrow, documented reason. -- Manual string concatenation, template strings, array `.join(' ')`, or custom ternaries for conditional or multi-line classes. +- Manual class-list assembly through string concatenation, template strings, array `.join(' ')`, or a custom conditional combiner instead of `cn(...)`. Conditional values passed to `cn(...)` remain valid. - JS conditional class branches for primitive visual states already exposed by Dify UI/Base UI `data-*` selectors. - Incoming `className` placed before default classes in `cn(...)`, preventing call-site overrides. - Arbitrary z-index or one-off layering fixes on overlays. diff --git a/.agents/skills/frontend-code-review/references/component-architecture.md b/.agents/skills/frontend-code-review/references/component-architecture.md index 1218fe22959..7bf9b172b39 100644 --- a/.agents/skills/frontend-code-review/references/component-architecture.md +++ b/.agents/skills/frontend-code-review/references/component-architecture.md @@ -1,103 +1,31 @@ -# Component Architecture Rules +# Component Architecture Review -Use these rules for React component structure, ownership, state, props, effects, and module organization. +Use the canonical reference for the changed concern. These links share rules; they do not activate the implementation skill or its workflow. -## Ownership +| Concern | Canonical rules | +| --- | --- | +| Vertical modules, public entrypoints, data/handler placement, wrappers, Props, and types | [Ownership] | +| Local/Jotai state, form drafts, route identity, URL state, and persistence | [State] | +| Effects, navigation, memoization, and subscriptions | [Runtime] | +| Hotkeys, focus, and secondary surfaces | [Interactions] | -Flag: +## Apply Rules In Their Actual Scope -- State, query, mutation, or handlers hoisted above the lowest component that actually uses them. -- Parent components owning row/item actions that do not coordinate a workflow. -- Prop drilling through multiple pass-through layers. -- A page/tab-level section component becoming the data owner without needing a shared snapshot or shared loading/error/empty UI. -- Feature code promoted to shared only because it appears once or might be reused later. +Explicit team conventions are reviewable contracts, including module organization and public API boundaries. Check the documented exception before reporting a violation. Do not infer an exception solely because the code appears to work, or invent a user-facing failure for a convention finding. -Accept repeated TanStack Query hooks for the same key and input in Client Component siblings under one QueryClient; -shared cache is not a reason to hoist. Separate Server Component QueryClients do not share it, so request-level -deduplication needs an identified request-local cache or verified framework or transport owner. +- For owner placement, trace the consumers and required lifetime. Establish whether the parent coordinates a snapshot, submission, navigation, shared UI, or persistence before asking to move state or handlers. +- For component boundaries, identify the ownership or encapsulation the proposed extraction would improve; file length alone establishes neither. +- For props and types, check the domain contract and public API. Do not report private props typing style alone; declaration/export syntax matters only for a documented package rule or concrete type, export, or framework defect. +- For state and Effects, trace the source of truth, external synchronization target, and mount/reset boundary. Controlledness alone does not prove that a draft is lifted or persisted; follow the form and overlay contracts linked by [State]. +- For navigation, distinguish ordinary links from mutation success, guarded redirects, command flows, and submission side effects. -## Component Boundaries +## Preserve Existing Product Contracts -Flag: +During refactors, trace the interaction being moved through its real consumer. Navigation, sidebar, dropdown, webapp-list, and app-switching changes must preserve expansion controls, hover persistence, pin/delete actions, routing, keyboard/focus handling, and open-state ownership where present. -- React component files over 300 lines when the file mixes multiple responsibilities that can be split into focused colocated components, hooks, or utilities. -- Shallow wrappers that only rename props or hide the real primitive. -- Extra DOM wrappers that do not provide layout, semantics, accessibility, state ownership, or library integration. -- Dialog/dropdown/popover hidden surfaces that obscure the parent flow when they should be extracted into a small local component. -- Business forms, menu bodies, or one-off helpers moved away from their owner without reuse or semantic value. +Check that the changed owner still handles reachable empty, loading, and missing optional-data states, and that primitive wrappers preserve accessible semantics and the public controlled-state contract. Report the actual lost behavior or explicit rule violation; use the package testing policy when assessing regression coverage. -Prefer colocated components split by actual data and state needs. - -## Bad Component Design Patterns - -Flag: - -- Refactors of existing navigation, sidebar, dropdown, webapp list, or app-switching UI that do not preserve behavior-sensitive interactions such as expand/collapse arrows, hover persistence, pin/delete controls, routing, keyboard/focus handling, or open-state ownership. -- Components that mix data fetching, mutation side effects, popup state, form validation, layout, and row rendering without a clear owner. -- Generic components with many boolean props that encode one feature's workflow. -- A shared component that imports feature-specific copy, routes, or API contracts. -- A feature component that accepts pre-rendered fragments only to avoid placing ownership correctly. -- A child component that receives both raw server data and separately derived flags for the same concept. -- A wrapper that changes accessible semantics of the primitive it wraps. -- A component that exposes controlled props but still keeps a competing private state for the same value. -- A component that cannot render empty, loading, or missing optional API fields without caller-side preprocessing. - -When existing components already own interaction logic, prefer reusing or extending them. If a refactor is necessary, preserve the old interaction contract and add or update focused tests for changed behavior. - -## Props And Types - -Flag: - -- Declaration or export rewrites made only for stylistic uniformity, without changing an owned behavior or contract. -- Named `Props` types for trivial one-off props where inline typing is clearer. -- Props named by UI implementation instead of domain/API role. -- API data converted too early or under a generic name that breaks traceability. -- Callers duplicating fallback checks that the lowest rendering component already handles. - -Do not flag `FC`, `React.FC`, function declarations, arrow functions, named exports, or default exports by syntax alone. Report them only when the chosen form causes a concrete type, lifecycle, export, framework, or enforced package-contract defect. - -## Effects - -Flag effects that: - -- Transform props/state for rendering. -- Copy one state value into another representing the same concept. -- Handle user actions that belong in event handlers. -- Reset local state from props or visibility when derivation, a stable semantic identity, or the intended mounted owner already expresses the lifecycle. -- Fetch data that belongs in framework APIs or TanStack Query. - -If an effect remains, it must synchronize with a named external system: browser API, subscription, timer, analytics-on-visibility, non-React widget, or imperative DOM integration. - -## State Modeling - -Flag: - -- Storing derived booleans, disabled flags, default tabs, or loading labels that can be calculated from current query/feature state. -- Per-session state held by a longer-lived visibility coordinator and cleared through an open-state Effect or a generated key when the primitive's mounted-content lifecycle already matches the intended state lifetime. -- A DOM field mirrored into competing prop, default, and React state sources when editing does not require those sources to synchronize. -- Local state used to fake server data or generated contract fields. -- UI state persisted to localStorage when it is live app state. -- Feature-local mock shells wired to unrelated existing APIs before the real API is confirmed. - -Review state lifetime before its storage mechanism. For a hidden surface, distinguish the -visibility coordinator from mounted content. State private to one mounted session belongs in that -content owner; promote it only when the draft must survive that content owner's unmount or another -owner coordinates it. A stable semantic identity key may create a new snapshot when the represented -identity changes; a generated key is not a routine reset command. - -Prefer render-time derivation. Keep true local state for user choices, transient input, controlled -popups, and feature UI state that has no server source. Submit-only DOM fields may remain -uncontrolled; use local controlled state when React must own the current value to drive rendering -or coordination. Observing change events or tracking a derived fact such as dirty state does not -require mirroring the field value. Do not flag controlled state by itself without a concrete -competing-source, stale-state, or ownership defect. - -## Navigation - -Flag: - -- Imperative router navigation for ordinary links. -- Button semantics used for navigation. -- Navigation state hidden in component state when URL state is required for shareable filters, tabs, or pagination. - -Use `Link` for normal navigation. Use router APIs for mutation success, guarded redirects, command flows, or form submission side effects. +[Interactions]: ../../how-to-write-component/references/interactions.md +[Ownership]: ../../how-to-write-component/references/ownership.md +[Runtime]: ../../how-to-write-component/references/runtime.md +[State]: ../../how-to-write-component/references/state.md diff --git a/.agents/skills/frontend-code-review/references/data-query-contracts.md b/.agents/skills/frontend-code-review/references/data-query-contracts.md index c68d7e0d1c4..1dc9edc3e0b 100644 --- a/.agents/skills/frontend-code-review/references/data-query-contracts.md +++ b/.agents/skills/frontend-code-review/references/data-query-contracts.md @@ -1,92 +1,28 @@ -# Data, Query, And Contract Rules +# Data, Query, And Contract Review -Use these rules for generated contracts, TanStack Query, mutations, auth/SSR boundaries, URL state, and client persistence. +[Data and queries] owns generated-client, Query options, mutation/cache, imperative access, SSR, authentication, and tenant rules. [State ownership] owns URL state and persistence. Read only the reference needed by the diff; reading a shared reference does not activate its implementation skill. -## Generated Contracts +## Generated Contracts And Query Conventions -Flag: +Review explicit team conventions as contracts, including direct generated options, `skipToken` for missing required input, and shared cache policy. A lint rule may enforce part of a convention; neither a green lint result nor a suppression proves the full contract is satisfied. -- New legacy service/helper wrappers around generated `queryOptions()` or `mutationOptions()`. -- Continuing to use deprecated contract operations when a ready generated contract exists. -- Assuming a generated file means an operation is ready without checking deprecated markers, schema shape, and the actual UI consumer. -- Re-declaring API DTOs in components. -- Adding compatibility layers instead of migrating the pointed line and deleting the old layer. +- Establish whether the changed call belongs to a new or migrated surface and whether the generated operation is ready. Check deprecated markers, schema shape, and the real UI consumer before prescribing a migration. +- Distinguish a pass-through wrapper from a feature hook with actual orchestration. Check whether an independent execution condition or Promise composition justifies the documented query/mutation exception. +- Trace generated input and output types through their boundaries. Identify the exact DTO mirror, field widening, placeholder input, or lost intentional empty value when reporting a violation. +- Check whether a local mutation callback owns feature feedback or replaces shared invalidation, retry, or cache defaults. Match optimistic changes to the current list/detail owner. -Backend Pydantic and OpenAPI schemas own API shape. Generated clients and schemas under `packages/contracts/generated/*` are authoritative at frontend boundaries and use the `{ params, query?, body? }` input shape. +## Imperative Access And SSR -## Queries +- Check freshness, projection, retries, and the caller's execution condition separately against [Data and queries]. Trace Promise ownership to distinguish an awaited hard gate from soft prefetching with an explicit failure owner. +- For Server Components, identify who renders the data and who may revalidate it. Check dehydration, the same-key client consumer, error handling, and the intended Suspense/server-rendered-content contract when the diff changes streaming. +- For auth, setup, roles, branding, or availability, trace authoritative data and the loading/fallback path. A static redirect or placeholder value cannot stand in for a request-dependent decision. -Flag: +## Tenant, URL, And Persistence Boundaries -- `enabled` used to hide missing required input instead of `input: skipToken`. -- Fake fallback IDs or placeholder inputs used to force a query to run. -- Query results copied into local state for rendering. -- Shared query behavior such as invalidation, stale defaults, or retry rules reimplemented at call sites. -- Deprecated imperative reads such as `fetchQuery`, `prefetchQuery`, `ensureQueryData`, or their infinite variants when - the current `query` or `infiniteQuery` contract applies. +- Trace the current workspace-switch flow and cache lifetime before reporting missing identity in a query key. Verify backend meaning before treating `workspace_id` and `tenant_id` as interchangeable. +- For URL and storage changes, identify whether the value is shareable navigation state, live app state, a one-shot signal, or a low-frequency preference. Apply [State ownership] to that category and verify its write/reset owner. -Use `useQuery(consoleQuery.xxx.queryOptions(...))` or `useQuery(marketplaceQuery.xxx.queryOptions(...))` directly unless a feature hook performs real orchestration. +Report the violated rule and applicable scope or the concrete failing path. Do not invent runtime impact when the finding is a team-convention violation. -For imperative access, treat the choices as independent dimensions: - -- `query` or `infiniteQuery` resolves the generated query and returns its data. -- `staleTime` decides whether cached data satisfies this call: `0` treats it as stale, a finite value accepts a freshness - window, `Infinity` accepts it until invalidation, and `'static'` accepts available data even after invalidation. -- `select` projects the resolved value without replacing cached query-function data. An imperative query defaults to no - retries when `retry` is not configured; `enabled` is observer-only, so guard before a conditional call. -- `await` blocks the current flow, `return` transfers the Promise to the caller, and `void` discards the result without - handling rejection. Handle rejection before discarding a potentially rejecting Promise; use `.catch(noop)` only for - intentional silence or feedback owned elsewhere, and preserve rejection for hard gates. - -## Mutations - -Flag: - -- Deprecated `useInvalid` or `useReset`. -- `mutateAsync` used without a need for Promise semantics. -- Awaited mutations without `try/catch`. -- Components owning shared cache invalidation that belongs in query defaults. -- Optimistic updates that do not match current list/detail ownership. - -Use generated `mutationOptions()` directly when possible. Put shared cache behavior in `createTanstackQueryUtils(...experimental_defaults...)`. - -## SSR, Auth, And Route Boundaries - -Flag: - -- Request-time auth, setup, workspace role, or tenant decisions moved into static `next.config redirects()`. -- Dynamic role gates depending on `workspaces.current` implemented as static path redirects. -- Authorization logic depending on an imperative query whose rejection is swallowed. -- Removing a client fallback before server API unavailable behavior is defined. -- Global placeholder query contracts introduced to solve a route-local Suspense issue. -- Branding-sensitive UI reading placeholder defaults without checking pending/placeholder state. -- A Server Component rendering or passing an imperative query result that the browser can independently revalidate, - leaving server and client output with different owners. -- A non-blocking Server Component query without pending-query dehydration, Next-compatible error redaction, a - `HydrationBoundary` covering the same-key client consumer, or an explicit Suspense and SSR-content decision. - -Hard gates await `query` or `infiniteQuery` and preserve rejection; soft prefetches handle failure at the fallback owner. -Treat Server Components as prefetch-and-dehydrate owners by default, rendering returned data only under exclusive server -ownership or a freshness contract that prevents server/client drift. - -## Workspace And Tenant - -Flag: - -- Treating workspace switch as ordinary CRUD invalidation when the current app flow performs server switch plus full reload. -- Query keys that omit workspace/tenant identity when the query truly varies by workspace and no full reload boundary applies. -- Mixing `workspace_id` and `tenant_id` without tracing the current backend/API contract. - -Current Dify workspace switch should be reviewed as a tenant cache boundary first. - -## URL State And Local Storage - -Flag: - -- Shareable filters, tabs, pagination, selected panels, or search state hidden only in component state. -- One-shot navigation signals modeled as subscribed persistent state. -- Live app state stored in localStorage. -- Direct `window.localStorage`, `globalThis.localStorage`, or raw storage calls in app code. -- High-frequency interaction state persisted on every change instead of on commit/settle. - -Use URL state for shareable UI state, feature/Jotai/store state for live UI state, and `@/hooks/use-local-storage` only for low-frequency client-only preferences, dismissed notices, and UI defaults. +[Data and queries]: ../../how-to-write-component/references/data.md +[State ownership]: ../../how-to-write-component/references/state.md diff --git a/.agents/skills/frontend-code-review/references/performance.md b/.agents/skills/frontend-code-review/references/performance.md index 3bd6e25867a..1e9aaf8ce5d 100644 --- a/.agents/skills/frontend-code-review/references/performance.md +++ b/.agents/skills/frontend-code-review/references/performance.md @@ -71,8 +71,6 @@ Flag: ## React Flow -For workflow React Flow components, keep this Dify-specific rule: +Use [Dify invariants] for React Flow node/edge consumption and provider availability in RAG Pipe template rendering. Callback-only reads or mutations can use `useStoreApi`. -- UI consumption should use React Flow hooks such as `useNodes` / `useEdges`. -- Callback-only reads or mutations can use `useStoreApi`. -- Node components under `web/app/components/workflow/nodes/[nodeName]/node.tsx` must not depend on workflow stores that are absent in RAG Pipe template rendering. +[Dify invariants]: dify-invariants.md diff --git a/.agents/skills/frontend-code-review/references/testing.md b/.agents/skills/frontend-code-review/references/testing.md index 3bda124c01b..93b349b21bb 100644 --- a/.agents/skills/frontend-code-review/references/testing.md +++ b/.agents/skills/frontend-code-review/references/testing.md @@ -1,6 +1,6 @@ # Testing Review Rules -`web/docs/test.md` is the canonical frontend testing policy. Use this file only to translate that policy into review findings. +Use `web/docs/test.md` for tests owned by `web/` and `packages/dify-ui/docs/testing.md` for tests owned by Dify UI. These owners define their test boundaries, environments, and checks; this reference only adds review questions. ## Request Missing Tests When Risk Justifies Them @@ -30,8 +30,7 @@ Flag tests that: - Prefer semantic queries and accessible names. - Prefer real feature components when integration semantics matter. -- Allow intentional child or provider mocks when setup would dominate the test and that boundary is covered independently. -- Do not accept semantically inaccurate mocks of Dify UI or legacy base primitives. +- Check mocks against the owning package's policy; allowed mocks must preserve the public contract and leave the behavior under review real. - Require a real-browser or visual verification plan when `happy-dom` cannot represent the risk. Treat test quality, determinism, and regression value as the review criteria. Do not use test count or coverage percentage as a proxy for quality. diff --git a/.agents/skills/frontend-testing/SKILL.md b/.agents/skills/frontend-testing/SKILL.md index a09aa940b1d..3e1ca6833cc 100644 --- a/.agents/skills/frontend-testing/SKILL.md +++ b/.agents/skills/frontend-testing/SKILL.md @@ -5,12 +5,9 @@ description: Use when writing or changing Vitest or React Testing Library tests # Frontend Testing -`web/docs/test.md` is the single policy owner. Read it before changing frontend tests; this skill adds no separate requirements. +Read the testing policy for the package that owns the changed tests: -1. Identify the observable contract and regression risk. -2. Choose the smallest boundary that includes the behavior owner. -3. Establish the failing case first when practical, then implement one coherent scenario. -4. Run the focused spec before the affected suite and relevant static checks. -5. Report the behavior verified and any remaining browser, visual, or end-to-end risk. +- `web/`: `web/docs/test.md` owns Web test boundaries, environments, and commands. +- `packages/dify-ui/`: `packages/dify-ui/docs/testing.md` owns primitive test boundaries, environments, Storybook, and commands. -Recommend deleting low-value tests as readily as adding missing behavior coverage. Use `web/docs/test.md` for policy and Web commands; use `packages/dify-ui/docs/testing.md` for Dify UI commands. +This skill adds no parallel policy or check sequence. Follow the selected owner's requirements, including its validation commands. Recommend deleting low-value tests as readily as adding missing behavior coverage, and report the contract verified and any material verification gap. diff --git a/.agents/skills/how-to-write-component/SKILL.md b/.agents/skills/how-to-write-component/SKILL.md index ec64e18fdeb..85be6988323 100644 --- a/.agents/skills/how-to-write-component/SKILL.md +++ b/.agents/skills/how-to-write-component/SKILL.md @@ -7,19 +7,6 @@ description: Use when implementing or refactoring React/TypeScript components an Use this skill to route component architecture decisions to its bundled references. Read only the references required by the current change. -## First Decisions - -| Question | Default | Choose differently when | -| --- | --- | --- | -| Where should code live? | In the product workflow, route, or feature owner. | Several verticals need the same stable contract. | -| Who owns state and handlers? | The lowest owner that consumes them and whose lifetime matches the state. | Another owner coordinates the value or it must survive the local owner's unmount. | -| Should React control a value? | Leave submit-only DOM fields uncontrolled. | The workflow must own the current value to drive rendering or coordination. | -| Should state enter Jotai? | Keep component and form state local. | Siblings need one source of truth or scoped workflow persistence. | -| Who owns URL state? | Next.js route APIs and `nuqs`. | Atoms require a read-only route-identity bridge. | -| Who owns remote state? | TanStack Query at the lowest consumer. | Atom state drives the query or shared derivations consume it. | -| Is a wrapper needed? | Use the primitive or direct code. | The wrapper owns behavior, validation, state, or semantics. | -| Is an Effect needed? | Derive during render or handle the user action. | A named external system must be synchronized. | - ## Topic Routing - Component moves, module boundaries, props, types, or owner placement: read [`references/ownership.md`][ownership]. @@ -28,12 +15,9 @@ Use this skill to route component architecture decisions to its bundled referenc - Hotkeys, focus, dialogs, menus, popovers, or other secondary surfaces: read [`references/interactions.md`][interactions] and the overlay guide it references when applicable. Also read [`references/state.md`][state] when the surface owns a draft or other local session state. - Effects, navigation, memoization, preloading, or render cost: read [`references/runtime.md`][runtime]. -## Workflow +## Scope And Verification -1. Identify the behavior owner, the required state lifetime, and the public contract being changed. -2. Read the nearby implementation, tests, and only the routed skill references. -3. Implement one coherent vertical slice. Do not expand into equivalent patterns elsewhere unless the current contract cannot be completed without them. -4. Verify observable behavior at the narrowest sufficient boundary, then run the checks documented by the owning package: `web/docs/test.md` or `web/docs/lint.md` for Web, and `packages/dify-ui/docs/testing.md` for Dify UI. +Identify the behavior owner, state lifetime, and public contract from the nearby implementation and relevant references. Keep the change within that vertical slice unless the contract requires changes elsewhere. Verify the changed behavior and complete the owning package's required checks. For Web, read `web/docs/test.md` for test work and `web/docs/lint.md` for static checks. Dify UI verification is owned by `packages/dify-ui/docs/testing.md`. ## Tailwind CSS diff --git a/.agents/skills/how-to-write-component/references/data.md b/.agents/skills/how-to-write-component/references/data.md index f0ca17faf16..57c23a8f284 100644 --- a/.agents/skills/how-to-write-component/references/data.md +++ b/.agents/skills/how-to-write-component/references/data.md @@ -8,9 +8,10 @@ Read this document when a component consumes generated contracts, nullable API v - Backend Pydantic and OpenAPI schemas own API shape. Follow the generated `{ params, query?, body? }` input shape; when it is wrong, fix the backend schema and regenerate `packages/contracts/generated/*`. - Do not hand-write DTO mirrors, widen generated fields or enums, edit generated output, or add a parallel frontend status layer unless it models product state absent from the API. - Check deprecated markers, schema shape, and the actual consumer before assuming that a generated operation is ready to use. +- When a ready generated operation exists for the changed call, migrate deprecated operations and remove the replaced layer instead of adding compatibility wrappers. - Normalize only at real boundaries such as user input, search, URL params, filenames, DOM IDs, or a required legacy adapter. - Preserve `null`, `undefined`, and intentional empty strings until the final boundary. Do not use `value || undefined` when an empty string means clearing a field. -- Build required values in the branch that proves them. Avoid `filter(Boolean)`, truthiness filters, non-null assertions after filters, and placeholder values used only to satisfy types. +- Build required values in the branch that proves them. Do not use truthiness filters, non-null assertions, or placeholders to conceal missing required input or discard valid `0`, `false`, or empty-string values. ## Queries @@ -26,6 +27,7 @@ Read this document when a component consumes generated contracts, nullable API v ## Mutations And Cache - Use generated `mutationOptions()` directly for owner-local mutations. +- Do not introduce deprecated `useInvalid` or `useReset` APIs. - Put shared invalidation, retries, and cache behavior in `createTanstackQueryUtils(...experimental_defaults...)`. Local callbacks may own toast, close, and navigation effects but must not replace shared cache policy. - Prefer `mutate(...)`. Use `mutateAsync(...)` only when Promise composition is required, and catch awaited failures. - Preserve intentional empty values and current list/detail ownership when updating data. Do not add optimistic updates without a verified owner contract. @@ -36,6 +38,7 @@ Read this document when a component consumes generated contracts, nullable API v - Use `query` or `infiniteQuery` for imperative access. `staleTime` defines cache acceptance; `select` projects the return value without replacing cached query-function data. Imperative queries default to no retries when `retry` is not configured, and observer-only `enabled` does not prevent an imperative call. +- Migrate deprecated `fetchQuery`, `prefetchQuery`, `ensureQueryData`, and their infinite variants when the current `query` or `infiniteQuery` contract applies. A `staleTime` of `0` treats data as stale; a finite value accepts that freshness window; `Infinity` accepts data until invalidation; `'static'` accepts available data even after invalidation. - `await` blocks, `return` transfers the Promise, and `void` discards its value without handling rejection. Handle rejection before discarding a potentially rejecting Promise; use `.catch(noop)` only for intentional silence or feedback owned elsewhere. Hard server gates await the query and preserve rejection. @@ -49,5 +52,7 @@ Read this document when a component consumes generated contracts, nullable API v - Non-blocking RSC streaming requires pending-query dehydration without redacting Next.js server errors, a `HydrationBoundary` around the same-key client consumer, and Suspense when that content must be server-rendered. - Never reuse tenant-scoped state after switching workspaces. Discard it at the switch boundary or isolate it by workspace identity. +- Trace the current switch flow before choosing cache handling: server switch plus full reload is a tenant boundary, not ordinary CRUD invalidation. Include workspace identity in varying query keys when no full-reload boundary applies, and trace the backend contract before interchanging `workspace_id` and `tenant_id`. - Do not make product or authorization decisions from bootstrap defaults. Wait for authoritative data, or render an explicit loading or error state. +- Preserve an existing client fallback until server API-unavailable behavior has an explicit owner. - Keep loading and Suspense behavior inside the feature that owns the request. Do not add fake global data merely to bypass that boundary. diff --git a/.agents/skills/how-to-write-component/references/interactions.md b/.agents/skills/how-to-write-component/references/interactions.md index ba9315f981e..6559b134258 100644 --- a/.agents/skills/how-to-write-component/references/interactions.md +++ b/.agents/skills/how-to-write-component/references/interactions.md @@ -23,10 +23,9 @@ Read this document when a change involves application hotkeys, focus, dialogs, m - Follow the [overlay contract] for primitive choice and shared mechanics. The nearest consumer `AGENTS.md` owns application-specific composite reuse policy. - Separate behavior ownership from placement ownership: the action may own trigger, open state, and menu content while the caller owns slots, offsets, and alignment. - Keep menu and dialog surfaces as siblings when a menu command opens a dialog. Mount the dialog outside popup content. -- Keep overlay open-state ownership separate from content-session ownership. A controlled root does not require controlled fields or root-owned drafts. -- Match transient state to the primitive's content mount lifecycle. State below an unmounting content boundary gets a fresh instance after unmount; intentionally kept-mounted content needs an explicit persistence or reset policy. -- Keep a controlled overlay root at its coordination owner so the primitive can complete exit transitions, focus restoration, and detached-handle behavior. Do not conditionally remove the root to reset content state, and use keys only for stable semantic identity. +- Use the [overlay contract] to determine content lifetime and preserve the Root's closing lifecycle. Follow [state ownership] for draft placement and semantic identity; portal placement alone does not locate the state owner. - Place query subscriptions and mutation observers at the owner whose lifetime matches when they should run. Mounted-session work may belong inside content; work that must start or stop exactly with `open` needs an explicit open-state condition. - Prefer primitive-owned open state unless another owner must observe or coordinate it. Analytics callbacks and local cleanup alone do not require a controlled root. [overlay contract]: ../../../../packages/dify-ui/docs/overlays.md +[state ownership]: state.md diff --git a/.agents/skills/how-to-write-component/references/ownership.md b/.agents/skills/how-to-write-component/references/ownership.md index 941dca051d1..369e9800bc1 100644 --- a/.agents/skills/how-to-write-component/references/ownership.md +++ b/.agents/skills/how-to-write-component/references/ownership.md @@ -18,25 +18,28 @@ Read this document when adding, moving, splitting, or refactoring React componen acceptable; shared cache is not a reason to hoist. Separate Server Component QueryClients do not share it, so request-level deduplication needs an identified request-local cache or verified framework or transport owner. - Pass stable domain identity across boundaries. Do not pass raw server data together with separately derived flags for the same concept. -- One pass-through prop layer is acceptable. Repeated forwarding means ownership should move closer to the consumer or into feature-scoped shared state. +- Revisit prop forwarding when intermediate components obscure the behavior owner; keep clear data flow rather than introducing shared state merely to avoid passing props. - Do not replace prop drilling with one large view-model hook. Move each query, derived value, and handler to the concrete owner that consumes it. - Keep source selection, defaults, validation, dirty checks, and payload shaping beside the workflow that owns submission. ## Boundaries +- Prefer reusing or extending the component that already owns an interaction over rebuilding that behavior in a parallel component. - State-heavy wizards, drawers, modals, and secondary workflows can form a small vertical surface with an entrypoint, optional feature-local state, and shallow owners matching real visual regions. - The entrypoint owns route integration, provider wiring, placement, and open-state coordination. A content or session owner keeps state scoped to that mounted surface. - Judge hook lifetime by the component that declares the hook and the primitive's mount contract, not only by where its rendered controls appear in JSX. - Separate hidden dialogs, dropdowns, and popovers into small local owners when their content obscures the parent flow. - Keep cohesive forms, menu bodies, and one-off helpers local unless they have their own state, reuse, or semantic boundary. +- Extract components or hooks when they clarify ownership or hide a cohesive implementation; keep logic local when extraction only shortens the file or relocates the same coordination. - Avoid wrapper components and wrapper DOM that only rename props, pass children through, or hide the real primitive. A wrapper must own behavior, validation, state, accessibility, layout, or library integration. +- Keep feature workflows out of shared components: do not encode one feature through many boolean props or import its copy, routes, and API contracts into a generic component. Do not pass pre-rendered fragments merely to avoid assigning the behavior to its owner. - Loading states for page sections, cards, lists, tables, forms, and drawers should use skeletons scoped to the loaded content. Reserve spinners for small inline busy indicators. ## Components And Types - Choose component declaration and export forms from the actual component contract, framework requirements, and enforced package rules. Existing style is context, not authority; do not rewrite unaffected code solely to normalize `FC`, `function`, arrow-function, named-export, or default-export forms. -- Type simple one-off props inline. Name a `Props` type when it is reused, exported, complex, or materially clearer. - Use API-generated or API-returned types at component boundaries. Keep one-off UI refinements and conversions beside their owner. +- Name props and converted data after their domain/API role, preserving traceability to the original contract. - Preserve domain value types for selections. Do not widen enums, unions, booleans, numbers, objects, or nullable values to `string` before a real boundary requires it. - Avoid generic `common.tsx` buckets and aliases that only rename another type. Name files, values, and public types after their domain role. - Put fallback and invariant checks in the lowest component that already renders that state. Do not extract helpers whose only purpose is hiding missing display data. diff --git a/.agents/skills/how-to-write-component/references/state.md b/.agents/skills/how-to-write-component/references/state.md index 680d77fb8b9..e1536aac3d4 100644 --- a/.agents/skills/how-to-write-component/references/state.md +++ b/.agents/skills/how-to-write-component/references/state.md @@ -11,18 +11,19 @@ Read this document when a change involves Jotai, form drafts, route identity, sh ## Forms And Sessions -- Keep form state in the narrowest owner whose lifetime matches the draft. A draft scoped to one mounted surface belongs to that content or session owner; a draft that must survive its current owner's unmount belongs to an explicit longer-lived feature owner. -- Prefer uncontrolled fields when values are only read at submit time. Use local controlled state only when React must own the current value to drive dependent UI or linked fields; track derived facts such as dirty state without mirroring the field value. Controlledness does not decide whether a draft is local or persisted. +- Follow the [form contract] for field controlledness and the [overlay contract] for Root/content mounting and state lifetime. These primitive contracts also apply when reviewing a consumer. +- Choose the Web draft owner from the required lifetime. Keep mounted-session drafts in the content owner; start with the lowest shared React owner when another component needs the draft or it must survive unmounting. Use feature-scoped atoms only when their coordination or persistence contract is needed. - For query-backed defaults, establish the form session after the required defaults are available. `defaultValue` initializes the current mount; a stable semantic identity key may create a fresh snapshot when the represented identity changes. Do not use a generated key as a routine reset command. -- Promote drafts beyond the session only when another owner reacts to in-progress values, several workflow steps share one draft, or the draft must intentionally survive unmounting. Start with the lowest shared React owner; use feature-scoped atoms only when their coordination or persistence contract is needed. - Keep validation, source priority, fallback behavior, dirty checks, and payload assembly in the workflow that owns submission. +- Derive booleans, disabled flags, default tabs, and loading labels from current state. Do not mirror one value into competing prop, default, and local sources; controlled state alone is not a competing-source defect. +- Do not use local state to fake server data or generated contract fields, or connect a feature mock shell to an unrelated API before its actual contract is confirmed. ## Route And URL State - Treat `useParams`, route arguments, and `nuqs` as the owners of URL identity and updates. - Hydrate a primitive atom at the route or surface boundary only when query atoms or shared derived atoms require route identity. Keep URL writes in route and query-state APIs. - Within one route-owned feature, choose one route-identity source. Do not hydrate route identity into atoms while also threading the same ID through multiple component layers. -- Put shareable filters, tabs, pagination, and search state in the URL. Keep one-shot navigation signals and transient UI state out of persistent subscriptions. +- Put shareable filters, tabs, selected panels, pagination, and search state in the URL. Keep one-shot navigation signals and transient UI state out of persistent subscriptions. ## Jotai And Query @@ -38,3 +39,6 @@ Read this document when a change involves Jotai, form drafts, route identity, sh - Use feature-owned storage modules built on `createLocalStorageState`; callers should not scatter direct storage access or raw keys. - Persist high-frequency interaction state only on commit or after updates settle. - Do not add ad hoc global event listeners for shared state. Centralize subscriptions through the owning atom, store, or subscription hook. + +[form contract]: ../../../../packages/dify-ui/docs/forms.md +[overlay contract]: ../../../../packages/dify-ui/docs/overlays.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 467d75ad153..bbe309600e5 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -1,7 +1,7 @@ name: '🕷️ Bug report' description: Report errors or unexpected behavior labels: - - bug + - 🐞 bug body: - type: checkboxes attributes: diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index 05cad470e7c..364598db372 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -1,7 +1,7 @@ name: '⭐ Feature or enhancement request' description: Propose something new. labels: - - enhancement + - 💪 enhancement body: - type: checkboxes attributes: diff --git a/.github/actions/setup-web/action.yml b/.github/actions/setup-web/action.yml index dbc20df50af..69565020d79 100644 --- a/.github/actions/setup-web/action.yml +++ b/.github/actions/setup-web/action.yml @@ -4,13 +4,13 @@ description: Set up Node.js, Vite+, pnpm, and web dependencies runs: using: composite steps: - - name: Setup pnpm - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 + - name: Setup pnpm and Node.js + uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0 with: - run_install: false + install: false - name: Setup Vite+ uses: voidzero-dev/setup-vp@1b32467adbe183473499fd9d5d372c3ed9641754 # v1.18.0 with: - node-version-file: package.json + node-manager: false cache: true run-install: true diff --git a/.github/workflows/api-tests.yml b/.github/workflows/api-tests.yml index 1b1afe96529..4e2abe4c433 100644 --- a/.github/workflows/api-tests.yml +++ b/.github/workflows/api-tests.yml @@ -71,10 +71,10 @@ jobs: retention-days: 1 api-integration: - name: API Integration Tests - runs-on: depot-ubuntu-24.04 + name: API Integration Tests (${{ matrix.python-version }}, shard ${{ matrix.shardIndex }}/${{ matrix.shardTotal }}) + runs-on: depot-ubuntu-24.04-4 env: - COVERAGE_FILE: coverage-integration + COVERAGE_FILE: coverage-integration-${{ matrix.python-version }}-${{ matrix.shardIndex }} STORAGE_TYPE: opendal OPENDAL_SCHEME: fs OPENDAL_FS_ROOT: /tmp/dify-storage @@ -82,9 +82,17 @@ jobs: run: shell: bash strategy: + fail-fast: false matrix: python-version: - '3.12' + shardIndex: + - 1 + - 2 + - 3 + - 4 + shardTotal: + - 4 steps: - name: Checkout code @@ -112,6 +120,8 @@ jobs: -p no:benchmark \ --start-middleware \ -n auto \ + --shard-index "${{ matrix.shardIndex }}" \ + --shard-total "${{ matrix.shardTotal }}" \ --timeout "${PYTEST_TIMEOUT:-180}" \ api/tests/integration_tests/workflow \ api/tests/integration_tests/tools \ @@ -120,8 +130,9 @@ jobs: - name: Upload integration coverage data uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: - name: api-coverage-integration - path: coverage-integration + name: api-coverage-integration-${{ matrix.python-version }}-${{ matrix.shardIndex }} + path: ${{ env.COVERAGE_FILE }} + if-no-files-found: error retention-days: 1 api-coverage: @@ -171,12 +182,15 @@ jobs: echo "" >> "$GITHUB_STEP_SUMMARY" unit_coverage="$(find coverage-data -type f -name coverage-unit -print -quit)" - integration_coverage="$(find coverage-data -type f -name coverage-integration -print -quit)" + mapfile -t integration_coverage < <(find coverage-data -type f -name 'coverage-integration-*' -print | sort) : "${unit_coverage:?coverage-unit artifact not found}" - : "${integration_coverage:?coverage-integration artifact not found}" + if [[ "${#integration_coverage[@]}" -ne 4 ]]; then + echo "expected 4 integration coverage artifacts, found ${#integration_coverage[@]}" >&2 + exit 1 + fi report_file="$(mktemp)" - uv run --project api coverage combine "$unit_coverage" "$integration_coverage" + uv run --project api coverage combine "$unit_coverage" "${integration_coverage[@]}" uv run --project api coverage report --show-missing | tee "$report_file" echo "Summary: \`$(tail -n 1 "$report_file")\`" >> "$GITHUB_STEP_SUMMARY" { diff --git a/.github/workflows/cli-e2e.yml b/.github/workflows/cli-e2e.yml index 51c5485ce0f..b58e94d9b0d 100644 --- a/.github/workflows/cli-e2e.yml +++ b/.github/workflows/cli-e2e.yml @@ -87,10 +87,9 @@ jobs: with: bun-version: latest - - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 + - uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0 with: - package_json_field: packageManager - run_install: false + install: false - name: Install CLI dependencies working-directory: cli @@ -131,11 +130,6 @@ jobs: - uses: oven-sh/setup-bun@v2 with: bun-version: latest - - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 - with: - package_json_field: packageManager - run_install: false - - run: pnpm install --frozen-lockfile - run: pnpm tree:gen - name: Run framework + output + error-handling @@ -181,11 +175,6 @@ jobs: - uses: oven-sh/setup-bun@v2 with: bun-version: latest - - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 - with: - package_json_field: packageManager - run_install: false - - run: pnpm install --frozen-lockfile - run: pnpm tree:gen - name: Run discovery suite @@ -247,11 +236,6 @@ jobs: - uses: oven-sh/setup-bun@v2 with: bun-version: latest - - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 - with: - package_json_field: packageManager - run_install: false - - run: pnpm install --frozen-lockfile - run: pnpm tree:gen - name: 'Run run/${{ matrix.name }}' @@ -312,11 +296,6 @@ jobs: - uses: oven-sh/setup-bun@v2 with: bun-version: latest - - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 - with: - package_json_field: packageManager - run_install: false - - run: pnpm install --frozen-lockfile - run: pnpm tree:gen - name: Run auth/login + status + whoami @@ -371,11 +350,6 @@ jobs: - uses: oven-sh/setup-bun@v2 with: bun-version: latest - - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 - with: - package_json_field: packageManager - run_install: false - - run: pnpm install --frozen-lockfile - run: pnpm tree:gen - name: Run use / devices / logout / agent (serial) diff --git a/.github/workflows/main-ci.yml b/.github/workflows/main-ci.yml index 6023aa7fae2..b924fd18936 100644 --- a/.github/workflows/main-ci.yml +++ b/.github/workflows/main-ci.yml @@ -37,8 +37,6 @@ jobs: # Check which paths were changed to determine which tests to run check-changes: name: Check Changed Files - needs: pre_job - if: needs.pre_job.outputs.should_skip != 'true' runs-on: depot-ubuntu-24.04 outputs: api-changed: ${{ steps.changes.outputs.api }} @@ -49,6 +47,11 @@ jobs: migration-changed: ${{ steps.changes.outputs.migration }} sandbox-runtime-changed: ${{ steps.changes.outputs.sandbox-runtime }} dify-agent-changed: ${{ steps.changes.outputs.dify-agent }} + python-style-changed: ${{ steps.changes.outputs.python-style }} + dify-agent-style-changed: ${{ steps.changes.outputs.dify-agent-style }} + web-style-changed: ${{ steps.changes.outputs.web-style }} + ts-common-style-changed: ${{ steps.changes.outputs.ts-common-style }} + superlinter-changed: ${{ steps.changes.outputs.superlinter }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3 @@ -150,6 +153,58 @@ jobs: - 'docker/generate_docker_compose' - 'docker/ssrf_proxy/**' - 'docker/volumes/sandbox/conf/**' + python-style: + - 'api/**' + - 'scripts/ast_grep_guard.py' + - 'scripts/check_no_new_getattr.py' + - 'scripts/check_no_new_controller_sqlalchemy.py' + - 'scripts/lint_controller_sqlalchemy.py' + - 'scripts/ast_grep_rules/no_new_getattr.yml' + - 'scripts/ast_grep_rules/no_new_controller_sqlalchemy.yml' + - '.github/workflows/style.yml' + - '.github/workflows/main-ci.yml' + dify-agent-style: + - 'dify-agent/**' + - '.github/workflows/style.yml' + - '.github/workflows/main-ci.yml' + web-style: + - 'web/**' + - 'e2e/**' + - 'sdks/nodejs-client/**' + - 'packages/**' + - 'package.json' + - 'pnpm-lock.yaml' + - 'pnpm-workspace.yaml' + - 'knip.config.ts' + - 'scripts/check-web-production-unused-after-knip-fix.mjs' + - '.github/workflows/style.yml' + - '.github/actions/setup-web/**' + ts-common-style: + - 'web/**' + - 'cli/**' + - 'e2e/**' + - 'sdks/nodejs-client/**' + - 'packages/**' + - 'package.json' + - 'pnpm-lock.yaml' + - 'pnpm-workspace.yaml' + - 'vite.config.ts' + - 'lint.config.ts' + - 'eslint.config.mjs' + - 'knip.config.ts' + - 'scripts/check-web-production-unused-after-knip-fix.mjs' + - 'oxlint-suppressions.json' + - 'eslint-suppressions.json' + - '.vscode/**' + - '.github/**' + superlinter: + - '**.sh' + - '**.yaml' + - '**.yml' + - '**Dockerfile' + - 'dev/**' + - '.editorconfig' + - '.vite-hooks/**' # Run tests in parallel while always emitting stable required checks. api-tests-run: @@ -396,10 +451,19 @@ jobs: style-check: name: Style Check - needs: pre_job + if: ${{ always() }} + needs: + - pre_job + - check-changes uses: ./.github/workflows/style.yml with: base-rev: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + classification-succeeded: ${{ needs.check-changes.result == 'success' }} + run-python-style: ${{ needs.check-changes.outputs.python-style-changed == 'true' }} + run-dify-agent-style: ${{ needs.check-changes.outputs.dify-agent-style-changed == 'true' }} + run-web-style: ${{ needs.check-changes.outputs.web-style-changed == 'true' }} + run-ts-common-style: ${{ needs.check-changes.outputs.ts-common-style-changed == 'true' }} + run-superlinter: ${{ needs.check-changes.outputs.superlinter-changed == 'true' }} skip-checks: ${{ needs.pre_job.outputs.should_skip == 'true' }} vdb-tests-run: diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index ca9d93d136e..11fb97b6963 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -6,6 +6,30 @@ on: base-rev: required: true type: string + classification-succeeded: + description: Whether the caller successfully classified the changed files. + required: true + type: boolean + run-python-style: + description: Run Python style checks for the changed files. + required: true + type: boolean + run-dify-agent-style: + description: Run Dify Agent style checks for the changed files. + required: true + type: boolean + run-web-style: + description: Run Web style checks for the changed files. + required: true + type: boolean + run-ts-common-style: + description: Run shared TypeScript style checks for the changed files. + required: true + type: boolean + run-superlinter: + description: Run SuperLinter for the changed files. + required: true + type: boolean skip-checks: description: Create the required check runs without repeating previously successful work. required: false @@ -20,72 +44,72 @@ permissions: jobs: python-style: name: Python Style - if: ${{ !inputs.skip-checks }} + if: ${{ !inputs.skip-checks && (!inputs.classification-succeeded || inputs.run-python-style || inputs.run-dify-agent-style) }} runs-on: depot-ubuntu-24.04 steps: + - name: Verify changed-file classification + if: ${{ !inputs.classification-succeeded }} + run: | + echo "Changed-file classification failed; refusing to skip Python style checks." >&2 + exit 1 + - name: Checkout code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false fetch-depth: 0 - - name: Check changed files - id: changed-files - uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6 - with: - files: | - api/** - scripts/ast_grep_guard.py - scripts/check_no_new_getattr.py - scripts/check_no_new_controller_sqlalchemy.py - scripts/lint_controller_sqlalchemy.py - scripts/ast_grep_rules/no_new_getattr.yml - scripts/ast_grep_rules/no_new_controller_sqlalchemy.yml - .github/workflows/style.yml - .github/workflows/main-ci.yml - - name: Setup UV and Python - if: steps.changed-files.outputs.any_changed == 'true' uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1 with: enable-cache: false python-version: '3.12' - cache-dependency-glob: api/uv.lock + cache-dependency-glob: | + api/uv.lock + dify-agent/uv.lock - name: Install dependencies - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style run: uv sync --project api --dev - name: Run Import Linter - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style run: uv run --directory api --dev lint-imports - name: Run Response Contract Linter - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style run: uv run --project api --dev python api/dev/lint_response_contracts.py --fail-on-mismatch - name: Run No New Getattr Guard - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style run: uv run --project api python scripts/check_no_new_getattr.py --base-rev "${{ inputs.base-rev }}" - name: Run No New Controller SQLAlchemy Guard - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style run: uv run --project api python scripts/check_no_new_controller_sqlalchemy.py --base-rev "${{ inputs.base-rev }}" - name: Run Type Checks - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style env: PYREFLY_OUTPUT_FORMAT: github run: make type-check-core + - name: Run Dify Agent Ruff + if: inputs.run-dify-agent-style + run: make -C dify-agent check + + - name: Run Dify Agent Type Checks + if: inputs.run-dify-agent-style + run: make -C dify-agent typecheck + - name: Dotenv check - if: steps.changed-files.outputs.any_changed == 'true' + if: inputs.run-python-style run: uv run --project api dotenv-linter ./api/.env.example ./web/.env.example web-style: name: Web Style - if: ${{ !inputs.skip-checks }} + if: ${{ !inputs.skip-checks && (!inputs.classification-succeeded || inputs.run-web-style) }} runs-on: depot-ubuntu-24.04 defaults: run: @@ -95,126 +119,84 @@ jobs: pull-requests: read steps: + - name: Verify changed-file classification + if: ${{ !inputs.classification-succeeded }} + working-directory: . + run: | + echo "Changed-file classification failed; refusing to skip Web style checks." >&2 + exit 1 + - name: Checkout code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - name: Check changed files - id: changed-files - uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6 - with: - files: | - web/** - e2e/** - sdks/nodejs-client/** - packages/** - package.json - pnpm-lock.yaml - pnpm-workspace.yaml - knip.config.ts - scripts/check-web-production-unused-after-knip-fix.mjs - .github/workflows/style.yml - .github/actions/setup-web/** - - name: Setup web environment - if: steps.changed-files.outputs.any_changed == 'true' uses: ./.github/actions/setup-web - name: Web tsslint - if: steps.changed-files.outputs.any_changed == 'true' env: NODE_OPTIONS: --max-old-space-size=4096 run: vp run lint:tss - name: Web dead code check - if: steps.changed-files.outputs.any_changed == 'true' working-directory: . run: vp run knip - name: Web dead code check production - if: steps.changed-files.outputs.any_changed == 'true' working-directory: . run: vp run knip:production - name: Web production unused declarations check - if: steps.changed-files.outputs.any_changed == 'true' working-directory: . run: vp run knip:production-unused-check ts-common-style: name: TS Common - if: ${{ !inputs.skip-checks }} + if: ${{ !inputs.skip-checks && (!inputs.classification-succeeded || inputs.run-ts-common-style) }} runs-on: depot-ubuntu-24.04-4 permissions: checks: write pull-requests: read steps: + - name: Verify changed-file classification + if: ${{ !inputs.classification-succeeded }} + run: | + echo "Changed-file classification failed; refusing to skip TypeScript style checks." >&2 + exit 1 + - name: Checkout code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - name: Check changed files - id: changed-files - uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6 - with: - files: | - web/** - cli/** - e2e/** - sdks/nodejs-client/** - packages/** - package.json - pnpm-lock.yaml - pnpm-workspace.yaml - vite.config.ts - lint.config.ts - eslint.config.mjs - knip.config.ts - scripts/check-web-production-unused-after-knip-fix.mjs - oxlint-suppressions.json - eslint-suppressions.json - .vscode/** - .github/** - - name: Setup web environment - if: steps.changed-files.outputs.any_changed == 'true' uses: ./.github/actions/setup-web - name: Static check - if: steps.changed-files.outputs.any_changed == 'true' run: pnpm -w check superlinter: name: SuperLinter - if: ${{ !inputs.skip-checks }} + if: ${{ !inputs.skip-checks && (!inputs.classification-succeeded || inputs.run-superlinter) }} runs-on: depot-ubuntu-24.04 steps: + - name: Verify changed-file classification + if: ${{ !inputs.classification-succeeded }} + run: | + echo "Changed-file classification failed; refusing to skip SuperLinter." >&2 + exit 1 + - name: Checkout code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 persist-credentials: false - - name: Check changed files - id: changed-files - uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6 - with: - files: | - **.sh - **.yaml - **.yml - **Dockerfile - dev/** - .editorconfig - .vite-hooks/** - - name: Super-linter uses: super-linter/super-linter/slim@4ce20838b8ab83717e78138c5b3a1407148e0918 # v8.7.0 - if: steps.changed-files.outputs.any_changed == 'true' env: BASH_SEVERITY: warning DEFAULT_BRANCH: origin/main diff --git a/AGENTS.md b/AGENTS.md index d7b7320aa2d..35b1fa3301e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,15 +1,9 @@ # AGENTS.md -Dify is an open-source platform for building LLM applications, agentic workflows, and RAG pipelines. This monorepo contains the backend API (`api/`), frontend application (`web/`), deployment assets (`docker/`), standalone agent backend (`dify-agent/`), CLI (`cli/`), and end-to-end suite (`e2e/`). Follow the nearest scoped `AGENTS.md` for the files being changed. +Dify is an open-source platform for building LLM applications, agentic workflows, and RAG pipelines. This monorepo contains the backend API (`api/`), frontend application (`web/`), deployment assets (`docker/`), standalone agent backend (`dify-agent/`), CLI (`cli/`), and end-to-end suite (`e2e/`). Follow the nearest scoped `AGENTS.md` for the files being changed. Apply its guidance within the user's requested scope; explicit user instructions take precedence over workflow defaults. ## Repository Gotchas - Run backend commands through `uv run --project api `. - Backend integration tests are CI-only and are not expected to run locally. - Keep `docker/.env.example` limited to variables required for a default Docker Compose deployment to start. Put optional and provider-specific settings in the matching `docker/envs/*.env.example` file; `docker/.env` overrides those service-specific env files. - -## Frontend Workflow - -- For truncated text disclosure and native `title` decisions, follow [Truncated Text Disclosure]. - -[Truncated Text Disclosure]: web/docs/truncated-text-disclosure.md diff --git a/api/.importlinter b/api/.importlinter index 99bff19c96d..b29dd9cc4ae 100644 --- a/api/.importlinter +++ b/api/.importlinter @@ -203,6 +203,7 @@ forbidden_modules = name = Account application services and contracts are framework and persistence neutral type = forbidden source_modules = + services.account_access_service services.account_avatar_service services.account_change_email_ports services.account_change_email_service @@ -219,6 +220,7 @@ source_modules = services.account_password_service services.account_ports services.account_profile_service + services.entities.account_access_entities services.entities.account_entities services.entities.account_login_entities services.entities.account_oauth_entities @@ -262,6 +264,21 @@ forbidden_modules = sqlalchemy werkzeug +[importlinter:contract:app-tracing-config-service-boundary] +name = App tracing configuration service is framework and persistence neutral +type = forbidden +source_modules = + services.app_tracing_config_service +forbidden_modules = + configs + controllers + extensions + flask + models + repositories + sqlalchemy + werkzeug + [importlinter:contract:webapp-access-query-service-boundary] name = Web app access query application service is framework and persistence neutral type = forbidden @@ -423,6 +440,22 @@ forbidden_modules = sqlalchemy werkzeug +[importlinter:contract:file-grant-service-boundary] +name = File grant application service is framework and persistence neutral +type = forbidden +source_modules = + services.file_grant_service + services.entities.file_grant_entities +forbidden_modules = + configs + controllers + extensions + flask + models + repositories + sqlalchemy + werkzeug + [importlinter:contract:account-activation-service-boundary] name = Account activation application service is framework and persistence neutral type = forbidden diff --git a/api/README.md b/api/README.md index b41ee99b090..47ade969b93 100644 --- a/api/README.md +++ b/api/README.md @@ -96,7 +96,7 @@ The scripts resolve paths relative to their location, so you can run them from a uv run pytest tests/integration_tests/ # Integration tests # Code quality - ./dev/reformat # Run all formatters and linters + ../dev/reformat # Run all formatters and linters uv run ruff check --fix ./ # Fix linting issues uv run ruff format ./ # Format code uv run pyrefly check # Type checking diff --git a/api/conftest.py b/api/conftest.py index 350b0306016..e967a723d2a 100644 --- a/api/conftest.py +++ b/api/conftest.py @@ -33,6 +33,18 @@ ensure_backend_test_environment(_REPO_ROOT) def pytest_addoption(parser: pytest.Parser) -> None: group = parser.getgroup("dify") + group.addoption( + "--shard-index", + type=int, + default=1, + help="One-based index of the test shard to run.", + ) + group.addoption( + "--shard-total", + type=int, + default=1, + help="Total number of test shards.", + ) group.addoption( "--start-middleware", action="store_true", @@ -58,9 +70,33 @@ def pytest_addoption(parser: pytest.Parser) -> None: def pytest_configure(config: pytest.Config) -> None: + shard_index = config.getoption("shard_index") + shard_total = config.getoption("shard_total") + if shard_total < 1: + raise pytest.UsageError("--shard-total must be at least 1") + if not 1 <= shard_index <= shard_total: + raise pytest.UsageError("--shard-index must be between 1 and --shard-total") + config.stash[_DIFY_COMPOSE_STACKS_KEY] = [] +def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: + """Select a deterministic, evenly sized slice of collected tests for this shard.""" + shard_index = config.getoption("shard_index") + shard_total = config.getoption("shard_total") + if shard_total == 1: + return + + selected: list[pytest.Item] = [] + deselected: list[pytest.Item] = [] + for item_index, item in enumerate(items): + target = selected if item_index % shard_total == shard_index - 1 else deselected + target.append(item) + + config.hook.pytest_deselected(items=deselected) + items[:] = selected + + def pytest_sessionstart(session: pytest.Session) -> None: config = session.config if hasattr(config, "workerinput"): diff --git a/api/constants/languages.py b/api/constants/languages.py index 8c1ff455363..3ae6773a149 100644 --- a/api/constants/languages.py +++ b/api/constants/languages.py @@ -8,6 +8,7 @@ language_timezone_mapping = { "de-DE": "Europe/Berlin", "ja-JP": "Asia/Tokyo", "ko-KR": "Asia/Seoul", + "lo-LA": "Asia/Vientiane", "ru-RU": "Europe/Moscow", "it-IT": "Europe/Rome", "uk-UA": "Europe/Kyiv", diff --git a/api/controllers/console/app/error.py b/api/controllers/console/app/error.py index 2a84336596e..dd9725291ee 100644 --- a/api/controllers/console/app/error.py +++ b/api/controllers/console/app/error.py @@ -107,24 +107,42 @@ class DraftWorkflowNotSync(BaseHTTPException): code = 409 -class TracingConfigNotExist(BaseHTTPException): - error_code = "trace_config_not_exist" - description = "Trace config not exist." +class TracingConfigNotFoundError(BaseHTTPException): + error_code = "trace_config_not_found" + description = "Tracing configuration not found." + code = 404 + + +class TracingConfigAlreadyExistsError(BaseHTTPException): + error_code = "trace_config_already_exists" + description = "A tracing configuration already exists for this provider." + code = 409 + + +class UnsupportedTracingProviderError(BaseHTTPException): + error_code = "unsupported_tracing_provider" + description = "The tracing provider is not supported." code = 400 -class TracingConfigIsExist(BaseHTTPException): - error_code = "trace_config_is_exist" - description = "Trace config is exist." +class InvalidTracingConfigError(BaseHTTPException): + error_code = "invalid_tracing_config" + description = "The tracing configuration is invalid." code = 400 -class TracingConfigCheckError(BaseHTTPException): - error_code = "trace_config_check_error" - description = "Invalid Credentials." +class TracingConfigVerificationFailedError(BaseHTTPException): + error_code = "tracing_config_verification_failed" + description = "The tracing configuration could not be verified." code = 400 +class TracingConfigProcessingError(BaseHTTPException): + error_code = "tracing_config_processing_failed" + description = "The tracing configuration could not be processed." + code = 500 + + class InvokeRateLimitError(BaseHTTPException): """Raised when the Invoke returns rate limit error.""" diff --git a/api/controllers/console/app/ops_trace.py b/api/controllers/console/app/ops_trace.py index 2332f164cd9..9f9cdfb48f2 100644 --- a/api/controllers/console/app/ops_trace.py +++ b/api/controllers/console/app/ops_trace.py @@ -1,27 +1,49 @@ from typing import Any +from uuid import UUID from flask_restx import Resource from pydantic import BaseModel, Field -from werkzeug.exceptions import BadRequest from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models from controllers.console import console_ns -from controllers.console.app.error import TracingConfigCheckError, TracingConfigIsExist, TracingConfigNotExist -from controllers.console.app.wraps import get_app_model +from controllers.console.app.error import ( + AppNotFoundError, + InvalidTracingConfigError, + TracingConfigAlreadyExistsError, + TracingConfigNotFoundError, + TracingConfigProcessingError, + TracingConfigVerificationFailedError, + UnsupportedTracingProviderError, +) +from controllers.console.flask_admission import console_account_admission from controllers.console.wraps import ( RBACPermission, RBACResourceScope, - account_initialization_required, - edit_permission_required, model_validate, - rbac_permission_required, - setup_required, ) -from extensions.ext_database import db +from extensions.ext_application_services import application_services from fields.base import ResponseModel -from libs.login import login_required -from models import App -from services.ops_service import OpsService +from libs.helper import dump_response +from machinery.context import RequestContext +from models.account import TenantAccountRole +from services.app_tracing_config_service import ( + AppTracingConfigAlreadyExistsError, + AppTracingConfigAppNotFoundError, + AppTracingConfigInvalidConfigurationError, + AppTracingConfigInvalidProviderError, + AppTracingConfigNotFoundError, + AppTracingConfigProcessingError, + AppTracingConfigRecord, + AppTracingConfigVerificationFailedError, +) + +_APP_TRACING_CONFIG_EDIT_ROLES = frozenset( + { + TenantAccountRole.OWNER, + TenantAccountRole.ADMIN, + TenantAccountRole.EDITOR, + } +) class TraceProviderQuery(BaseModel): @@ -48,6 +70,18 @@ class TraceAppConfigResponse(ResponseModel): created_at: str | None = None updated_at: str | None = None + @classmethod + def from_record(cls, record: AppTracingConfigRecord) -> "TraceAppConfigResponse": + return cls( + id=record.id, + app_id=record.app_id, + tracing_provider=record.tracing_provider, + tracing_config=record.tracing_config, + is_active=record.is_active, + created_at=str(record.created_at), + updated_at=str(record.updated_at), + ) + register_schema_models(console_ns, TraceProviderQuery, TraceConfigPayload) register_response_schema_models(console_ns, TraceAppConfigResponse) @@ -68,23 +102,42 @@ class TraceAppConfigApi(Resource): "Tracing configuration retrieved successfully", console_ns.models[TraceAppConfigResponse.__name__], ) - @console_ns.response(400, "Invalid request parameters") - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_TRACING_CONFIG) - @get_app_model + @console_ns.response(400, "Invalid request parameters or unsupported tracing provider") + @console_ns.response(404, "Application not found") + @console_ns.response(500, "Tracing configuration processing failed") + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_TRACING_CONFIG, + ) @model_validate(TraceProviderQuery) - def get(self, req_data: TraceProviderQuery, app_model: App): + def get( + self, + req_data: TraceProviderQuery, + request_context: RequestContext, + app_id: UUID, + ): try: - trace_config = OpsService.get_tracing_app_config( - app_id=app_model.id, tracing_provider=req_data.tracing_provider, session=db.session() + trace_config = application_services().app_tracing_configs.get( + context=request_context, + app_id=str(app_id), + tracing_provider=req_data.tracing_provider, ) - if not trace_config: - return {"has_not_configured": True} - return trace_config - except Exception as e: - raise BadRequest(str(e)) + except AppTracingConfigAppNotFoundError as error: + raise AppNotFoundError() from error + except AppTracingConfigInvalidProviderError as error: + raise UnsupportedTracingProviderError() from error + except AppTracingConfigProcessingError as error: + raise TracingConfigProcessingError() from error + except ValueError as error: + raise TracingConfigProcessingError() from error + + if trace_config is None: + return dump_response(TraceAppConfigResponse, {"has_not_configured": True}, exclude_none=True) + return dump_response( + TraceAppConfigResponse, + TraceAppConfigResponse.from_record(trace_config), + exclude_none=True, + ) @console_ns.doc("create_trace_app_config") @console_ns.doc(description="Create a new tracing configuration for an application") @@ -95,31 +148,47 @@ class TraceAppConfigApi(Resource): "Tracing configuration created successfully", console_ns.models[TraceAppConfigResponse.__name__], ) - @console_ns.response(400, "Invalid request parameters or configuration already exists") + @console_ns.response(400, "Invalid request parameters or tracing configuration") @console_ns.response(403, "Insufficient permissions") - @setup_required - @login_required - @account_initialization_required - @edit_permission_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_TRACING_CONFIG) - @get_app_model + @console_ns.response(404, "Application not found") + @console_ns.response(409, "Tracing configuration already exists") + @console_ns.response(500, "Tracing configuration processing failed") + @console_account_admission( + allowed_roles=_APP_TRACING_CONFIG_EDIT_ROLES, + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_TRACING_CONFIG, + ) @model_validate(TraceConfigPayload) - def post(self, req_data: TraceConfigPayload, app_model: App): + def post( + self, + req_data: TraceConfigPayload, + request_context: RequestContext, + app_id: UUID, + ): """Create a new trace app configuration""" try: - result = OpsService.create_tracing_app_config( - app_id=app_model.id, + application_services().app_tracing_configs.create( + context=request_context, + app_id=str(app_id), tracing_provider=req_data.tracing_provider, tracing_config=req_data.tracing_config, - session=db.session(), ) - if not result: - raise TracingConfigIsExist() - if result.get("error"): - raise TracingConfigCheckError() - return result - except Exception as e: - raise BadRequest(str(e)) + except AppTracingConfigAppNotFoundError as error: + raise AppNotFoundError() from error + except AppTracingConfigAlreadyExistsError as error: + raise TracingConfigAlreadyExistsError() from error + except AppTracingConfigInvalidProviderError as error: + raise UnsupportedTracingProviderError() from error + except AppTracingConfigInvalidConfigurationError as error: + raise InvalidTracingConfigError() from error + except AppTracingConfigVerificationFailedError as error: + raise TracingConfigVerificationFailedError() from error + except AppTracingConfigProcessingError as error: + raise TracingConfigProcessingError() from error + except ValueError as error: + raise TracingConfigProcessingError() from error + + return dump_response(TraceAppConfigResponse, {"result": "success"}, exclude_none=True), 201 @console_ns.doc("update_trace_app_config") @console_ns.doc(description="Update an existing tracing configuration for an application") @@ -130,52 +199,84 @@ class TraceAppConfigApi(Resource): "Tracing configuration updated successfully", console_ns.models[TraceAppConfigResponse.__name__], ) - @console_ns.response(400, "Invalid request parameters or configuration not found") + @console_ns.response(400, "Invalid request parameters or tracing configuration") @console_ns.response(403, "Insufficient permissions") - @setup_required - @login_required - @account_initialization_required - @edit_permission_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_TRACING_CONFIG) - @get_app_model + @console_ns.response(404, "Application or tracing configuration not found") + @console_ns.response(500, "Tracing configuration processing failed") + @console_account_admission( + allowed_roles=_APP_TRACING_CONFIG_EDIT_ROLES, + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_TRACING_CONFIG, + ) @model_validate(TraceConfigPayload) - def patch(self, req_data: TraceConfigPayload, app_model: App): + def patch( + self, + req_data: TraceConfigPayload, + request_context: RequestContext, + app_id: UUID, + ): """Update an existing trace app configuration""" try: - result = OpsService.update_tracing_app_config( - app_id=app_model.id, + application_services().app_tracing_configs.update( + context=request_context, + app_id=str(app_id), tracing_provider=req_data.tracing_provider, tracing_config=req_data.tracing_config, - session=db.session(), ) - if not result: - raise TracingConfigNotExist() - return {"result": "success"} - except Exception as e: - raise BadRequest(str(e)) + except AppTracingConfigAppNotFoundError as error: + raise AppNotFoundError() from error + except AppTracingConfigNotFoundError as error: + raise TracingConfigNotFoundError() from error + except AppTracingConfigInvalidProviderError as error: + raise UnsupportedTracingProviderError() from error + except AppTracingConfigInvalidConfigurationError as error: + raise InvalidTracingConfigError() from error + except AppTracingConfigVerificationFailedError as error: + raise TracingConfigVerificationFailedError() from error + except AppTracingConfigProcessingError as error: + raise TracingConfigProcessingError() from error + except ValueError as error: + raise TracingConfigProcessingError() from error + + return dump_response(TraceAppConfigResponse, {"result": "success"}, exclude_none=True) @console_ns.doc("delete_trace_app_config") @console_ns.doc(description="Delete an existing tracing configuration for an application") @console_ns.doc(params={"app_id": "Application ID"}) @console_ns.doc(params=query_params_from_model(TraceProviderQuery)) @console_ns.response(204, "Tracing configuration deleted successfully") - @console_ns.response(400, "Invalid request parameters or configuration not found") + @console_ns.response(400, "Invalid request parameters or unsupported tracing provider") @console_ns.response(403, "Insufficient permissions") - @setup_required - @login_required - @account_initialization_required - @edit_permission_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_TRACING_CONFIG) - @get_app_model + @console_ns.response(404, "Application or tracing configuration not found") + @console_ns.response(500, "Tracing configuration processing failed") + @console_account_admission( + allowed_roles=_APP_TRACING_CONFIG_EDIT_ROLES, + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_TRACING_CONFIG, + ) @model_validate(TraceProviderQuery) - def delete(self, req_data: TraceProviderQuery, app_model: App): + def delete( + self, + req_data: TraceProviderQuery, + request_context: RequestContext, + app_id: UUID, + ): """Delete an existing trace app configuration""" try: - result = OpsService.delete_tracing_app_config( - app_id=app_model.id, tracing_provider=req_data.tracing_provider, session=db.session() + application_services().app_tracing_configs.delete( + context=request_context, + app_id=str(app_id), + tracing_provider=req_data.tracing_provider, ) - if not result: - raise TracingConfigNotExist() - return "", 204 - except Exception as e: - raise BadRequest(str(e)) + except AppTracingConfigAppNotFoundError as error: + raise AppNotFoundError() from error + except AppTracingConfigNotFoundError as error: + raise TracingConfigNotFoundError() from error + except AppTracingConfigInvalidProviderError as error: + raise UnsupportedTracingProviderError() from error + except AppTracingConfigProcessingError as error: + raise TracingConfigProcessingError() from error + except ValueError as error: + raise TracingConfigProcessingError() from error + + return "", 204 diff --git a/api/controllers/console/app/statistic.py b/api/controllers/console/app/statistic.py index 48635166e3a..2ebf60bdcfe 100644 --- a/api/controllers/console/app/statistic.py +++ b/api/controllers/console/app/statistic.py @@ -1,31 +1,22 @@ +from datetime import datetime from decimal import Decimal -import sqlalchemy as sa -from flask import abort from flask_restx import Resource from pydantic import BaseModel, Field, field_validator +from werkzeug.exceptions import BadRequest from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models from controllers.console import console_ns from controllers.console.app.wraps import get_app_model -from controllers.console.wraps import ( - RBACPermission, - RBACResourceScope, - account_initialization_required, - model_validate, - rbac_permission_required, - setup_required, - with_current_user, -) -from core.app.entities.app_invoke_entities import InvokeFrom -from extensions.ext_database import db +from controllers.console.flask_admission import console_account_admission +from controllers.console.wraps import RBACPermission, RBACResourceScope, model_validate +from extensions.ext_application_services import application_services from fields.base import ResponseModel from libs.datetime_utils import parse_time_range -from libs.helper import convert_datetime_to_date, dump_response -from libs.login import login_required -from models import AppMode -from models.account import Account -from models.model import App +from libs.helper import dump_response +from libs.login import current_account_with_tenant +from machinery.context import RequestContext +from models.model import App, AppMode class StatisticTimeRangeQuery(BaseModel): @@ -142,6 +133,20 @@ register_response_schema_models( ) +def _resolve_statistic_time_range( + req_data: StatisticTimeRangeQuery, +) -> tuple[datetime | None, datetime | None, str]: + timezone = current_account_with_tenant().account.timezone + assert timezone is not None + + try: + start_date, end_date = parse_time_range(req_data.start, req_data.end, timezone) + except ValueError as error: + raise BadRequest(str(error)) from error + + return start_date, end_date, timezone + + @console_ns.route("/apps//statistics/daily-messages") class DailyMessageStatistic(Resource): @console_ns.doc("get_daily_message_statistics") @@ -152,52 +157,20 @@ class DailyMessageStatistic(Resource): "Daily message statistics retrieved successfully", console_ns.models[DailyMessageStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - COUNT(*) AS message_count -FROM - messages -WHERE - app_id = :app_id - AND invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append({"date": str(i.date), "message_count": i.message_count}) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_daily_messages( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(DailyMessageStatisticResponse, {"data": response_data}) @@ -212,51 +185,20 @@ class DailyConversationStatistic(Resource): "Daily conversation statistics retrieved successfully", console_ns.models[DailyConversationStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - COUNT(DISTINCT conversation_id) AS conversation_count -FROM - messages -WHERE - app_id = :app_id - AND invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append({"date": str(i.date), "conversation_count": i.conversation_count}) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_daily_conversations( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(DailyConversationStatisticResponse, {"data": response_data}) @@ -271,52 +213,20 @@ class DailyTerminalsStatistic(Resource): "Daily terminal statistics retrieved successfully", console_ns.models[DailyTerminalStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - COUNT(DISTINCT messages.from_end_user_id) AS terminal_count -FROM - messages -WHERE - app_id = :app_id - AND invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append({"date": str(i.date), "terminal_count": i.terminal_count}) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_daily_terminals( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(DailyTerminalStatisticResponse, {"data": response_data}) @@ -331,55 +241,20 @@ class DailyTokenCostStatistic(Resource): "Daily token cost statistics retrieved successfully", console_ns.models[DailyTokenCostStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - (SUM(messages.message_tokens) + SUM(messages.answer_tokens)) AS token_count, - SUM(total_price) AS total_price -FROM - messages -WHERE - app_id = :app_id - AND invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append( - {"date": str(i.date), "token_count": i.token_count, "total_price": i.total_price, "currency": "USD"} - ) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_daily_token_costs( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(DailyTokenCostStatisticResponse, {"data": response_data}) @@ -394,71 +269,20 @@ class AverageSessionInteractionStatistic(Resource): "Average session interaction statistics retrieved successfully", console_ns.models[AverageSessionInteractionStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT]) @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("c.created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - AVG(subquery.message_count) AS interactions -FROM - ( - SELECT - m.conversation_id, - COUNT(m.id) AS message_count - FROM - conversations c - JOIN - messages m - ON c.id = m.conversation_id - WHERE - c.app_id = :app_id - AND m.invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND c.created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND c.created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += """ - GROUP BY m.conversation_id - ) subquery -LEFT JOIN - conversations c - ON c.id = subquery.conversation_id -GROUP BY - date -ORDER BY - date""" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append( - {"date": str(i.date), "interactions": float(i.interactions.quantize(Decimal("0.01")))} - ) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_average_session_interactions( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(AverageSessionInteractionStatisticResponse, {"data": response_data}) @@ -473,61 +297,20 @@ class UserSatisfactionRateStatistic(Resource): "User satisfaction rate statistics retrieved successfully", console_ns.models[UserSatisfactionRateStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("m.created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - COUNT(m.id) AS message_count, - COUNT(mf.id) AS feedback_count -FROM - messages m -LEFT JOIN - message_feedbacks mf - ON mf.message_id=m.id AND mf.rating='like' -WHERE - m.app_id = :app_id - AND m.invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND m.created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND m.created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append( - { - "date": str(i.date), - "rate": round((i.feedback_count * 1000 / i.message_count) if i.message_count > 0 else 0, 2), - } - ) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_user_satisfaction_rates( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(UserSatisfactionRateStatisticResponse, {"data": response_data}) @@ -542,52 +325,20 @@ class AverageResponseTimeStatistic(Resource): "Average response time statistics retrieved successfully", console_ns.models[AverageResponseTimeStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model(mode=AppMode.COMPLETION) @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - AVG(provider_response_latency) AS latency -FROM - messages -WHERE - app_id = :app_id - AND invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append({"date": str(i.date), "latency": round(i.latency * 1000, 4)}) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_average_response_times( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(AverageResponseTimeStatisticResponse, {"data": response_data}) @@ -602,54 +353,19 @@ class TokensPerSecondStatistic(Resource): "Tokens per second statistics retrieved successfully", console_ns.models[TokensPerSecondStatisticResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_MONITOR) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_MONITOR, + ) @get_app_model @model_validate(StatisticTimeRangeQuery) - def get(self, req_data: StatisticTimeRangeQuery, account: Account, app_model: App): - - converted_created_at = convert_datetime_to_date("created_at") - sql_query = f"""SELECT - {converted_created_at} AS date, - CASE - WHEN SUM(provider_response_latency) = 0 THEN 0 - ELSE (SUM(answer_tokens) / SUM(provider_response_latency)) - END as tokens_per_second -FROM - messages -WHERE - app_id = :app_id - AND invoke_from != :invoke_from""" - assert account.timezone is not None - arg_dict: dict[str, object] = { - "tz": account.timezone, - "app_id": app_model.id, - "invoke_from": InvokeFrom.DEBUGGER, - } - - try: - start_datetime_utc, end_datetime_utc = parse_time_range(req_data.start, req_data.end, account.timezone) - except ValueError as e: - abort(400, description=str(e)) - - if start_datetime_utc: - sql_query += " AND created_at >= :start" - arg_dict["start"] = start_datetime_utc - - if end_datetime_utc: - sql_query += " AND created_at < :end" - arg_dict["end"] = end_datetime_utc - - sql_query += " GROUP BY date ORDER BY date" - - response_data = [] - - with db.engine.begin() as conn: - rs = conn.execute(sa.text(sql_query), arg_dict) - for i in rs: - response_data.append({"date": str(i.date), "tps": round(i.tokens_per_second, 4)}) + def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App): + start_date, end_date, timezone = _resolve_statistic_time_range(req_data) + response_data = application_services().app_statistics.get_tokens_per_second( + app_id=app_model.id, + start_date=start_date, + end_date=end_date, + timezone=timezone, + ) return dump_response(TokensPerSecondStatisticResponse, {"data": response_data}) diff --git a/api/controllers/console/app/workflow_app_log.py b/api/controllers/console/app/workflow_app_log.py index 78db9c346d7..e74abd03f66 100644 --- a/api/controllers/console/app/workflow_app_log.py +++ b/api/controllers/console/app/workflow_app_log.py @@ -4,29 +4,25 @@ from typing import Any from dateutil.parser import isoparse from flask_restx import Resource from pydantic import BaseModel, Field, field_validator -from sqlalchemy.orm import sessionmaker from controllers.common.schema import query_params_from_model, register_schema_models from controllers.console import console_ns from controllers.console.app.wraps import get_app_model +from controllers.console.flask_admission import console_account_admission from controllers.console.wraps import ( RBACPermission, RBACResourceScope, - account_initialization_required, model_validate, - rbac_permission_required, - setup_required, ) -from extensions.ext_database import db +from extensions.ext_application_services import application_services from fields.base import ResponseModel from fields.end_user_fields import SimpleEndUser from fields.member_fields import SimpleAccount from graphon.enums import WorkflowExecutionStatus -from libs.helper import to_timestamp -from libs.login import login_required +from libs.helper import dump_response, to_timestamp +from machinery.context import RequestContext from models import App from models.model import AppMode -from services.workflow_app_service import WorkflowAppService class WorkflowAppLogQuery(BaseModel): @@ -136,34 +132,32 @@ class WorkflowAppLogApi(Resource): "Workflow app logs retrieved successfully", console_ns.models[WorkflowAppLogPaginationResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_LOG_AND_ANNOTATION) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_LOG_AND_ANNOTATION, + ) @get_app_model(mode=[AppMode.WORKFLOW]) @model_validate(WorkflowAppLogQuery) - def get(self, req_data: WorkflowAppLogQuery, app_model: App): + def get( + self, + req_data: WorkflowAppLogQuery, + _request_context: RequestContext, + app_model: App, + ): """ Get workflow app logs """ - - # get paginate workflow app logs - workflow_app_service = WorkflowAppService() - with sessionmaker(db.engine, expire_on_commit=False).begin() as session: - workflow_app_log_pagination = workflow_app_service.get_paginate_workflow_app_logs( - session=session, - app_model=app_model, - keyword=req_data.keyword, - status=req_data.status, - created_at_before=req_data.created_at__before, - created_at_after=req_data.created_at__after, - page=req_data.page, - limit=req_data.limit, - detail=req_data.detail, - created_by_end_user_session_id=req_data.created_by_end_user_session_id, - created_by_account=req_data.created_by_account, - ) - - return WorkflowAppLogPaginationResponse.model_validate( - workflow_app_log_pagination, from_attributes=True - ).model_dump(mode="json") + result = application_services().workflow_app_logs.list_logs( + tenant_id=app_model.tenant_id, + app_id=app_model.id, + keyword=req_data.keyword, + status=req_data.status, + created_at_before=req_data.created_at__before, + created_at_after=req_data.created_at__after, + page=req_data.page, + limit=req_data.limit, + detail=req_data.detail, + created_by_end_user_session_id=req_data.created_by_end_user_session_id, + created_by_account=req_data.created_by_account, + ) + return dump_response(WorkflowAppLogPaginationResponse, result) diff --git a/api/controllers/console/app/workflow_run.py b/api/controllers/console/app/workflow_run.py index 26606e1c174..8a4b179439c 100644 --- a/api/controllers/console/app/workflow_run.py +++ b/api/controllers/console/app/workflow_run.py @@ -3,26 +3,19 @@ from uuid import UUID from flask_restx import Resource from pydantic import BaseModel, Field, field_validator -from sqlalchemy.orm import sessionmaker from configs import dify_config from controllers.common.errors import NotFoundError from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models from controllers.console import console_ns from controllers.console.app.wraps import get_app_model +from controllers.console.flask_admission import console_account_admission from controllers.console.wraps import ( RBACPermission, RBACResourceScope, - account_initialization_required, model_validate, - rbac_permission_required, - setup_required, - with_current_tenant_id, - with_current_user, ) -from core.workflow.human_input_forms import load_form_tokens_by_form_id as _load_form_tokens_by_form_id -from core.workflow.nodes.human_input.pause_reason import HumanInputRequired -from extensions.ext_database import db +from extensions.ext_application_services import application_services from fields.base import ResponseModel from fields.workflow_run_fields import ( AdvancedChatWorkflowRunPaginationResponse, @@ -32,14 +25,11 @@ from fields.workflow_run_fields import ( WorkflowRunNodeExecutionResponse, WorkflowRunPaginationResponse, ) -from graphon.enums import WorkflowExecutionStatus from libs.custom_inputs import time_duration -from libs.helper import uuid_value -from libs.login import login_required -from models import Account, App, AppMode, WorkflowRunTriggeredFrom -from models.workflow import WorkflowRun -from repositories.factory import DifyAPIRepositoryFactory -from services.workflow_run_service import WorkflowRunListArgs, WorkflowRunService +from libs.helper import dump_response, uuid_value +from machinery.context import RequestContext +from models import App, AppMode, WorkflowRunTriggeredFrom +from services.workflow_run_service import WorkflowRunListArgs def _build_backstage_input_url(form_token: str | None) -> str | None: @@ -51,10 +41,6 @@ def _build_backstage_input_url(form_token: str | None) -> str | None: return f"{base_url.rstrip('/')}/form/{form_token}" -# Workflow run status choices for filtering -WORKFLOW_RUN_STATUS_CHOICES = ["running", "succeeded", "failed", "stopped", "partial-succeeded"] - - class WorkflowRunListQuery(BaseModel): last_id: str | None = Field(default=None, description="Last run ID for pagination") limit: int = Field(default=20, ge=1, le=100, description="Number of items per page (1-100)") @@ -96,6 +82,19 @@ class WorkflowRunCountQuery(BaseModel): return time_duration(value) +def _workflow_run_list_args(req_data: WorkflowRunListQuery) -> WorkflowRunListArgs: + args: WorkflowRunListArgs = {"limit": req_data.limit} + if req_data.last_id is not None: + args["last_id"] = req_data.last_id + if req_data.status is not None: + args["status"] = req_data.status + return args + + +def _triggered_from(value: str | None) -> WorkflowRunTriggeredFrom: + return WorkflowRunTriggeredFrom(value) if value else WorkflowRunTriggeredFrom.DEBUGGING + + class HumanInputPauseTypeResponse(ResponseModel): type: Literal["human_input"] form_id: str @@ -143,37 +142,24 @@ class AdvancedChatAppWorkflowRunListApi(Resource): "Workflow runs retrieved successfully", console_ns.models[AdvancedChatWorkflowRunPaginationResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_CREATE_AND_MANAGEMENT) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_CREATE_AND_MANAGEMENT, + ) @get_app_model(mode=[AppMode.ADVANCED_CHAT]) @model_validate(WorkflowRunListQuery) - def get(self, req_data: WorkflowRunListQuery, app_model: App): + def get(self, req_data: WorkflowRunListQuery, request_context: RequestContext, app_model: App): """ Get advanced chat app workflow run list """ - args: WorkflowRunListArgs = {"limit": req_data.limit} - if req_data.last_id is not None: - args["last_id"] = req_data.last_id - if req_data.status is not None: - args["status"] = req_data.status - - # Default to DEBUGGING if not specified - triggered_from = ( - WorkflowRunTriggeredFrom(req_data.triggered_from) - if req_data.triggered_from - else WorkflowRunTriggeredFrom.DEBUGGING + result = application_services().workflow_runs.get_paginate_advanced_chat_workflow_runs( + request_context, + app_id=app_model.id, + args=_workflow_run_list_args(req_data), + triggered_from=_triggered_from(req_data.triggered_from), ) - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_paginate_advanced_chat_workflow_runs( - app_model=app_model, args=args, triggered_from=triggered_from - ) - - return AdvancedChatWorkflowRunPaginationResponse.model_validate(result, from_attributes=True).model_dump( - mode="json" - ) + return dump_response(AdvancedChatWorkflowRunPaginationResponse, result) @console_ns.route("/apps//advanced-chat/workflow-runs/count") @@ -187,34 +173,25 @@ class AdvancedChatAppWorkflowRunCountApi(Resource): "Workflow runs count retrieved successfully", console_ns.models[WorkflowRunCountResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_CREATE_AND_MANAGEMENT) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_CREATE_AND_MANAGEMENT, + ) @get_app_model(mode=[AppMode.ADVANCED_CHAT]) @model_validate(WorkflowRunCountQuery) - def get(self, req_data: WorkflowRunCountQuery, app_model: App): + def get(self, req_data: WorkflowRunCountQuery, request_context: RequestContext, app_model: App): """ Get advanced chat workflow runs count statistics """ - args = req_data.model_dump(exclude_none=True) - - # Default to DEBUGGING if not specified - triggered_from = ( - WorkflowRunTriggeredFrom(req_data.triggered_from) - if req_data.triggered_from - else WorkflowRunTriggeredFrom.DEBUGGING + result = application_services().workflow_runs.get_workflow_runs_count( + request_context, + app_id=app_model.id, + status=req_data.status, + time_range=req_data.time_range, + triggered_from=_triggered_from(req_data.triggered_from), ) - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_workflow_runs_count( - app_model=app_model, - status=args.get("status"), - time_range=args.get("time_range"), - triggered_from=triggered_from, - ) - - return WorkflowRunCountResponse.model_validate(result).model_dump(mode="json") + return dump_response(WorkflowRunCountResponse, result) @console_ns.route("/apps//workflow-runs") @@ -228,35 +205,24 @@ class WorkflowRunListApi(Resource): "Workflow runs retrieved successfully", console_ns.models[WorkflowRunPaginationResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_CREATE_AND_MANAGEMENT) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_CREATE_AND_MANAGEMENT, + ) @get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW]) @model_validate(WorkflowRunListQuery) - def get(self, req_data: WorkflowRunListQuery, app_model: App): + def get(self, req_data: WorkflowRunListQuery, request_context: RequestContext, app_model: App): """ Get workflow run list """ - args: WorkflowRunListArgs = {"limit": req_data.limit} - if req_data.last_id is not None: - args["last_id"] = req_data.last_id - if req_data.status is not None: - args["status"] = req_data.status - - # Default to DEBUGGING for workflow if not specified (backward compatibility) - triggered_from = ( - WorkflowRunTriggeredFrom(req_data.triggered_from) - if req_data.triggered_from - else WorkflowRunTriggeredFrom.DEBUGGING + result = application_services().workflow_runs.get_paginate_workflow_runs( + request_context, + app_id=app_model.id, + args=_workflow_run_list_args(req_data), + triggered_from=_triggered_from(req_data.triggered_from), ) - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_paginate_workflow_runs( - app_model=app_model, args=args, triggered_from=triggered_from - ) - - return WorkflowRunPaginationResponse.model_validate(result, from_attributes=True).model_dump(mode="json") + return dump_response(WorkflowRunPaginationResponse, result) @console_ns.route("/apps//workflow-runs/count") @@ -270,34 +236,25 @@ class WorkflowRunCountApi(Resource): "Workflow runs count retrieved successfully", console_ns.models[WorkflowRunCountResponse.__name__], ) - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_CREATE_AND_MANAGEMENT) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_CREATE_AND_MANAGEMENT, + ) @get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW]) @model_validate(WorkflowRunCountQuery) - def get(self, req_data: WorkflowRunCountQuery, app_model: App): + def get(self, req_data: WorkflowRunCountQuery, request_context: RequestContext, app_model: App): """ Get workflow runs count statistics """ - args = req_data.model_dump(exclude_none=True) - - # Default to DEBUGGING for workflow if not specified (backward compatibility) - triggered_from = ( - WorkflowRunTriggeredFrom(req_data.triggered_from) - if req_data.triggered_from - else WorkflowRunTriggeredFrom.DEBUGGING + result = application_services().workflow_runs.get_workflow_runs_count( + request_context, + app_id=app_model.id, + status=req_data.status, + time_range=req_data.time_range, + triggered_from=_triggered_from(req_data.triggered_from), ) - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_workflow_runs_count( - app_model=app_model, - status=args.get("status"), - time_range=args.get("time_range"), - triggered_from=triggered_from, - ) - - return WorkflowRunCountResponse.model_validate(result).model_dump(mode="json") + return dump_response(WorkflowRunCountResponse, result) @console_ns.route("/apps//workflow-runs/") @@ -311,23 +268,24 @@ class WorkflowRunDetailApi(Resource): console_ns.models[WorkflowRunDetailResponse.__name__], ) @console_ns.response(404, "Workflow run not found") - @setup_required - @login_required - @account_initialization_required - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_CREATE_AND_MANAGEMENT) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_CREATE_AND_MANAGEMENT, + ) @get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW]) - def get(self, app_model: App, run_id: UUID): + def get(self, request_context: RequestContext, app_model: App, run_id: UUID): """ Get workflow run detail """ - run_id_str = str(run_id) - - workflow_run_service = WorkflowRunService() - workflow_run = workflow_run_service.get_workflow_run(app_model=app_model, run_id=run_id_str) + workflow_run = application_services().workflow_runs.get_workflow_run( + request_context, + app_id=app_model.id, + run_id=str(run_id), + ) if workflow_run is None: raise NotFoundError("Workflow run not found") - return WorkflowRunDetailResponse.model_validate(workflow_run, from_attributes=True).model_dump(mode="json") + return dump_response(WorkflowRunDetailResponse, workflow_run) @console_ns.route("/apps//workflow-runs//node-executions") @@ -341,28 +299,22 @@ class WorkflowRunNodeExecutionListApi(Resource): console_ns.models[WorkflowRunNodeExecutionListResponse.__name__], ) @console_ns.response(404, "Workflow run not found") - @setup_required - @login_required - @account_initialization_required - @with_current_user - @rbac_permission_required(RBACResourceScope.APP, RBACPermission.APP_CREATE_AND_MANAGEMENT) + @console_account_admission( + rbac_resource_scope=RBACResourceScope.APP, + rbac_permission=RBACPermission.APP_CREATE_AND_MANAGEMENT, + ) @get_app_model(mode=[AppMode.ADVANCED_CHAT, AppMode.WORKFLOW]) - def get(self, current_user: Account, app_model: App, run_id: UUID): + def get(self, request_context: RequestContext, app_model: App, run_id: UUID): """ Get workflow run node execution list """ - run_id_str = str(run_id) - - workflow_run_service = WorkflowRunService() - node_executions = workflow_run_service.get_workflow_run_node_executions( - app_model=app_model, - run_id=run_id_str, - user=current_user, + node_executions = application_services().workflow_runs.get_workflow_run_node_executions( + request_context, + app_id=app_model.id, + run_id=str(run_id), ) - return WorkflowRunNodeExecutionListResponse.model_validate( - {"data": node_executions}, from_attributes=True - ).model_dump(mode="json") + return dump_response(WorkflowRunNodeExecutionListResponse, {"data": node_executions}) @console_ns.route("/workflow//pause-details") @@ -378,11 +330,8 @@ class ConsoleWorkflowPauseDetailsApi(Resource): console_ns.models[WorkflowPauseDetailsResponse.__name__], ) @console_ns.response(404, "Workflow run not found") - @setup_required - @login_required - @account_initialization_required - @with_current_tenant_id - def get(self, current_tenant_id: str, workflow_run_id: str): + @console_account_admission() + def get(self, request_context: RequestContext, workflow_run_id: str): """ Get workflow pause details. @@ -391,51 +340,31 @@ class ConsoleWorkflowPauseDetailsApi(Resource): Returns information about why and where the workflow is paused. """ - # Query WorkflowRun to determine if workflow is suspended - session_maker = sessionmaker(bind=db.engine) - workflow_run_repo = DifyAPIRepositoryFactory.create_api_workflow_run_repository(session_maker=session_maker) - - workflow_run = db.session.get(WorkflowRun, workflow_run_id) - if not workflow_run: + details = application_services().workflow_runs.get_pause_details( + request_context, + workflow_run_id=workflow_run_id, + ) + if details is None: raise NotFoundError("Workflow run not found") - if workflow_run.tenant_id != current_tenant_id: - raise NotFoundError("Workflow run not found") - - # Check if workflow is suspended - is_paused = workflow_run.status == WorkflowExecutionStatus.PAUSED - if not is_paused: - empty_response = WorkflowPauseDetailsResponse(paused_at=None, paused_nodes=[]) - return empty_response.model_dump(mode="json"), 200 - - pause_entity = workflow_run_repo.get_workflow_pause(workflow_run_id) - pause_reasons = pause_entity.get_pause_reasons() if pause_entity else [] - form_tokens_by_form_id = _load_form_tokens_by_form_id( - [reason.form_id for reason in pause_reasons if isinstance(reason, HumanInputRequired)] + return ( + dump_response( + WorkflowPauseDetailsResponse, + { + "paused_at": details.paused_at.isoformat() + "Z" if details.paused_at else None, + "paused_nodes": [ + { + "node_id": node.node_id, + "node_title": node.node_title, + "pause_type": { + "type": "human_input", + "form_id": node.form_id, + "backstage_input_url": _build_backstage_input_url(node.form_token), + }, + } + for node in details.paused_nodes + ], + }, + ), + 200, ) - - # Build response - paused_at = pause_entity.paused_at if pause_entity else None - paused_nodes: list[PausedNodeResponse] = [] - - for reason in pause_reasons: - if isinstance(reason, HumanInputRequired): - paused_nodes.append( - PausedNodeResponse( - node_id=reason.node_id, - node_title=reason.node_title, - pause_type=HumanInputPauseTypeResponse( - type="human_input", - form_id=reason.form_id, - backstage_input_url=_build_backstage_input_url(form_tokens_by_form_id.get(reason.form_id)), - ), - ) - ) - else: - raise AssertionError("unimplemented.") - - response = WorkflowPauseDetailsResponse( - paused_at=paused_at.isoformat() + "Z" if paused_at else None, - paused_nodes=paused_nodes, - ) - return response.model_dump(mode="json"), 200 diff --git a/api/controllers/console/explore/recommended_app.py b/api/controllers/console/explore/recommended_app.py index 1125c0bc580..1fdf61f049d 100644 --- a/api/controllers/console/explore/recommended_app.py +++ b/api/controllers/console/explore/recommended_app.py @@ -7,17 +7,17 @@ from pydantic import BaseModel, Field, computed_field, field_validator from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models from controllers.console import console_ns from controllers.console.explore.error import RecommendedAppNotFoundError -from controllers.console.wraps import account_initialization_required, model_validate, with_current_user +from controllers.console.flask_admission import console_account_admission +from controllers.console.wraps import model_validate from extensions.ext_application_services import application_services from fields.base import ResponseModel from libs.helper import build_icon_url, dump_response -from libs.login import login_required -from models import Account +from machinery.context import RequestContext from services.recommended_app_query_service import RecommendedAppNotFoundError as RecommendedAppQueryNotFoundError class RecommendedAppsQuery(BaseModel): - language: str | None = Field(default=None, description="Language code for recommended app localization") + language: str = Field(default="en-US", description="Language code for recommended app localization") class RecommendedAppInfoResponse(ResponseModel): @@ -97,16 +97,13 @@ register_response_schema_models( class RecommendedAppListApi(Resource): @console_ns.doc(params=query_params_from_model(RecommendedAppsQuery)) @console_ns.response(200, "Success", console_ns.models[RecommendedAppListResponse.__name__]) - @login_required - @account_initialization_required - @with_current_user + @console_account_admission() @model_validate(RecommendedAppsQuery) - def get(self, req_data: RecommendedAppsQuery, current_user: Account): + def get(self, req_data: RecommendedAppsQuery, _request_context: RequestContext): return dump_response( RecommendedAppListResponse, application_services().recommended_app_queries.list_recommended( - requested_language=req_data.language, - interface_language=current_user.interface_language, + language=req_data.language, ), ) @@ -115,16 +112,13 @@ class RecommendedAppListApi(Resource): class LearnDifyAppListApi(Resource): @console_ns.doc(params=query_params_from_model(RecommendedAppsQuery)) @console_ns.response(200, "Success", console_ns.models[LearnDifyAppListResponse.__name__]) - @login_required - @account_initialization_required - @with_current_user + @console_account_admission() @model_validate(RecommendedAppsQuery) - def get(self, req_data: RecommendedAppsQuery, current_user: Account): + def get(self, req_data: RecommendedAppsQuery, _request_context: RequestContext): return dump_response( LearnDifyAppListResponse, application_services().recommended_app_queries.list_learn_dify( - requested_language=req_data.language, - interface_language=current_user.interface_language, + language=req_data.language, ), ) @@ -133,9 +127,8 @@ class LearnDifyAppListApi(Resource): class RecommendedAppApi(Resource): @console_ns.response(200, "Success", console_ns.models[RecommendedAppDetailResponse.__name__]) @console_ns.response(404, "Recommended app not found") - @login_required - @account_initialization_required - def get(self, app_id: UUID): + @console_account_admission() + def get(self, _request_context: RequestContext, app_id: UUID): try: result = application_services().recommended_app_queries.get_detail(str(app_id)) except RecommendedAppQueryNotFoundError: diff --git a/api/controllers/console/notification.py b/api/controllers/console/notification.py index 080080bb361..03ebbc2ba6d 100644 --- a/api/controllers/console/notification.py +++ b/api/controllers/console/notification.py @@ -2,7 +2,7 @@ from flask_restx import Resource from pydantic import BaseModel, Field from controllers.common.fields import SimpleResultResponse -from controllers.common.schema import register_response_schema_models, register_schema_models +from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models from controllers.console import console_ns from controllers.console.flask_admission import console_account_admission from controllers.console.wraps import model_validate @@ -17,6 +17,10 @@ class DismissNotificationPayload(BaseModel): notification_id: str = Field(...) +class NotificationQuery(BaseModel): + language: str = Field(default="en-US", description="Notification language") + + class NotificationItemResponse(ResponseModel): notification_id: str | None = None frequency: str | None = None @@ -32,17 +36,19 @@ class NotificationResponse(ResponseModel): notifications: list[NotificationItemResponse] -register_schema_models(console_ns, DismissNotificationPayload) +register_schema_models(console_ns, DismissNotificationPayload, NotificationQuery) register_response_schema_models(console_ns, SimpleResultResponse, NotificationResponse) @console_ns.route("/notification") class NotificationApi(Resource): @console_ns.doc("get_notification") + @console_ns.doc(params=query_params_from_model(NotificationQuery)) @console_ns.doc( description=( "Return the active in-product notification for the current user " - "in their interface language (falls back to English if unavailable). " + "in the requested language (defaults to English when omitted). " + "Unavailable translations fall back to English, then the first available content. " "The notification is NOT marked as seen here; call POST /notification/dismiss " "when the user explicitly closes the modal." ), @@ -53,8 +59,9 @@ class NotificationApi(Resource): ) @console_ns.response(200, "Success", console_ns.models[NotificationResponse.__name__]) @console_account_admission(editions=frozenset({DeploymentEdition.CLOUD})) - def get(self, request_context: RequestContext): - result = application_services().notifications.get_active(request_context) + @model_validate(NotificationQuery) + def get(self, query: NotificationQuery, request_context: RequestContext): + result = application_services().notifications.get_active(request_context, query.language) return dump_response(NotificationResponse, result), 200 diff --git a/api/controllers/console/socketio/workflow.py b/api/controllers/console/socketio/workflow.py index 545af703dda..4569310bbe4 100644 --- a/api/controllers/console/socketio/workflow.py +++ b/api/controllers/console/socketio/workflow.py @@ -93,12 +93,10 @@ def handle_collaboration_event(sid, data): 1. mouse_move 2. vars_and_features_update 3. sync_request (ask leader to update graph) - 4. app_state_update - 5. mcp_server_update - 6. workflow_update - 7. comments_update - 8. node_panel_presence - 9. graph_view_state (session reports tab visibility; drives leader election) + 4. workflow_update + 5. comments_update + 6. node_panel_presence + 7. graph_view_state (session reports tab visibility; drives leader election) """ return collaboration_service.relay_collaboration_event(sid, data) diff --git a/api/controllers/console/workspace/models.py b/api/controllers/console/workspace/models.py index 54d021251fd..02b5a68f9c8 100644 --- a/api/controllers/console/workspace/models.py +++ b/api/controllers/console/workspace/models.py @@ -349,6 +349,8 @@ class ModelProviderModelCredentialApi(Resource): ) @setup_required @login_required + @is_admin_or_owner_required + @rbac_permission_required(RBACResourceScope.WORKSPACE, RBACPermission.CREDENTIAL_MANAGE, resource_required=False) @account_initialization_required @with_current_user @with_current_tenant_id diff --git a/api/controllers/files/__init__.py b/api/controllers/files/__init__.py index f8976b86b9f..42b3761b92d 100644 --- a/api/controllers/files/__init__.py +++ b/api/controllers/files/__init__.py @@ -14,12 +14,13 @@ api = ExternalApi( files_ns = Namespace("files", description="File operations", path="/") -from . import image_preview, tool_files, upload +from . import appdeploy_files, image_preview, tool_files, upload api.add_namespace(files_ns) __all__ = [ "api", + "appdeploy_files", "bp", "files_ns", "image_preview", diff --git a/api/controllers/files/appdeploy_files.py b/api/controllers/files/appdeploy_files.py new file mode 100644 index 00000000000..265884cf7df --- /dev/null +++ b/api/controllers/files/appdeploy_files.py @@ -0,0 +1,401 @@ +"""File endpoints reached with an AppDeploy file grant. + +Upload, remote-upload, produce, and resolve authenticate with a Bearer grant; +content authenticates with a per-file token in the query string, because an +```` cannot carry a header. + +They stay on the public ``files`` blueprint rather than moving under +``inner_api``, whose contract is a fully trusted caller holding the master key. +None of the five meets it: ``content`` is a browser-facing surface, ``upload`` +carries an end user's payload, and ``produced`` and ``resolve`` are called by +workers executing third-party plugin code. The one genuinely server-to-server +step, minting the grant, already lives in ``inner_api``. +""" + +from __future__ import annotations + +from typing import IO +from urllib.parse import quote +from uuid import UUID + +from flask import Response, request +from flask_restx import Resource +from pydantic import BaseModel, Field, HttpUrl, ValidationError +from werkzeug.datastructures import FileStorage + +import services +from controllers.common.errors import ( + BlockedFileExtensionError, + FilenameNotExistsError, + FileTooLargeError, + NoFileUploadedError, + RemoteFileUploadError, + TooManyFilesError, + UnsupportedFileTypeError, +) +from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models +from controllers.files import files_ns +from controllers.files.wraps import FileGrantInvalidError, GrantedFileNotFoundError, file_grant_required +from extensions.ext_application_services import application_services +from fields.base import ResponseModel +from fields.file_fields import FileResponse +from fields.file_grant_fields import ResolvedFileResponse +from libs.exception import BaseHTTPException +from libs.helper import dump_response +from services.entities.file_grant_entities import ( + FileContent, + FileGrantClaims, + FileGrantContext, + FileGrantScope, + FileKind, + FileRef, + StoredUpload, +) +from services.errors.file_grant import ( + EndUserNotFoundError, + InvalidFileGrantError, + RemoteFileUnavailableError, + TooManyFileRefsError, +) +from services.file_grant_service import MAX_FILE_GRANT_REFS + +# Everything outside this whitelist is served as an attachment. Produced files +# carry a plugin-declared MIME type, so SVG and the rest of the XML family must +# never render in the viewer's origin. +INLINE_MIME_TYPES = frozenset({"image/png", "image/jpeg", "image/gif", "image/webp"}) + + +class InvalidFileRequestError(BaseHTTPException): + error_code = "invalid_request" + description = "The file request is malformed." + code = 400 + + +class FileRefPayload(BaseModel): + id: str + kind: FileKind + + +class RemoteFileUploadPayload(BaseModel): + url: HttpUrl = Field(description="Remote file URL to fetch and store") + + +class FileResolvePayload(BaseModel): + files: list[FileRefPayload] = Field(default_factory=list, max_length=MAX_FILE_GRANT_REFS) + + +class FileContentQuery(BaseModel): + token: str = Field(description="Signed content token scoped to this file") + + +class RemoteFileUploadResponse(FileResponse): + """Dify's upload shape plus the ``url`` key its remote-upload clients read. + + Dify has no service-api remote upload for this endpoint to stand in for, and + the web and console one it does have answers under ``url``. Carrying both + names lets a client of either move over untouched; they hold one URL. + """ + + url: str + + +class ProducedFileResponse(ResponseModel): + id: str + name: str + size: int + mime_type: str | None = None + url: str + internal_url: str + + +class FileResolveResponse(ResponseModel): + files: list[ResolvedFileResponse] + + +register_schema_models(files_ns, RemoteFileUploadPayload, FileResolvePayload) +register_response_schema_models( + files_ns, FileResponse, RemoteFileUploadResponse, ProducedFileResponse, FileResolveResponse +) + + +@files_ns.route("/appdeploy/upload") +class GrantedFileUploadApi(Resource): + """Store one uploaded file against the grant's end user.""" + + @file_grant_required(FileGrantScope.UPLOAD) + @files_ns.doc("grant_upload_file") + @files_ns.doc( + responses={ + 201: "File uploaded", + 400: "No file uploaded, the file has no name, or its extension is blocked", + 401: "Invalid grant", + 403: "Grant lacks the upload scope", + 413: "File too large", + 415: "Unsupported file type", + } + ) + @files_ns.response(201, "File uploaded", files_ns.models[FileResponse.__name__]) + def post(self, grant: FileGrantClaims): + upload = _single_upload() + upload_file = _store_upload( + grant, + filename=upload.filename or "", + stream=upload.stream, + mimetype=upload.mimetype, + ) + return _granted_file_response(upload_file), 201 + + +@files_ns.route("/appdeploy/remote-upload") +class GrantedRemoteFileUploadApi(Resource): + """Fetch a remote URL through the SSRF-safe fetcher and store it.""" + + @file_grant_required(FileGrantScope.UPLOAD) + @files_ns.doc("grant_upload_remote_file") + @files_ns.expect(files_ns.models[RemoteFileUploadPayload.__name__]) + @files_ns.doc( + responses={ + 201: "Remote file uploaded", + 400: "Invalid URL, unfetchable remote file, or a blocked extension", + 401: "Invalid grant", + 403: "Grant lacks the upload scope", + 413: "File too large", + 415: "Unsupported file type", + } + ) + @files_ns.response(201, "Remote file uploaded", files_ns.models[RemoteFileUploadResponse.__name__]) + def post(self, grant: FileGrantClaims): + try: + payload = RemoteFileUploadPayload.model_validate(files_ns.payload or {}) + except ValidationError as exc: + raise InvalidFileRequestError(str(exc)) from exc + + try: + upload_file = application_services().file_grants.store_remote_upload( + context=_grant_context(grant), + url=str(payload.url), + ) + except RemoteFileUnavailableError as exc: + raise RemoteFileUploadError(f"Failed to fetch file from {payload.url}") from exc + except EndUserNotFoundError as exc: + raise GrantedFileNotFoundError() from exc + except services.errors.file.FileTooLargeError as exc: + raise FileTooLargeError(exc.description) from exc + except services.errors.file.BlockedFileExtensionError as exc: + raise BlockedFileExtensionError(exc.description) from exc + except services.errors.file.UnsupportedFileTypeError: + raise UnsupportedFileTypeError() + return _remote_granted_file_response(upload_file), 201 + + +@files_ns.route("/appdeploy/produced") +class ProducedFileApi(Resource): + """Store one file produced by a running workflow node.""" + + @file_grant_required(FileGrantScope.PRODUCE) + @files_ns.doc("grant_upload_produced_file") + @files_ns.doc( + responses={ + 201: "Produced file stored", + 400: "No file uploaded", + 401: "Invalid grant", + 403: "Grant lacks the produce scope", + 413: "File too large", + } + ) + @files_ns.response(201, "Produced file stored", files_ns.models[ProducedFileResponse.__name__]) + def post(self, grant: FileGrantClaims): + upload = _single_upload() + try: + tool_file, access = application_services().file_grants.store_produced( + context=_grant_context(grant), + filename=upload.filename, + stream=upload.stream, + mimetype=upload.mimetype, + ) + except services.errors.file.FileTooLargeError as exc: + raise FileTooLargeError(exc.description) + except EndUserNotFoundError as exc: + raise GrantedFileNotFoundError() from exc + + return ProducedFileResponse( + id=tool_file.id, + name=tool_file.name, + size=tool_file.size, + mime_type=tool_file.mime_type, + url=access.external_url, + internal_url=access.internal_url, + ).model_dump(mode="json"), 201 + + +@files_ns.route("/appdeploy/resolve") +class GrantedFileResolveApi(Resource): + """Re-check ownership and sign fresh URLs at the moment of use.""" + + @file_grant_required(FileGrantScope.RESOLVE) + @files_ns.doc("grant_resolve_files") + @files_ns.expect(files_ns.models[FileResolvePayload.__name__]) + @files_ns.doc( + responses={ + 200: "Files resolved", + 400: "Malformed request", + 401: "Invalid grant", + 403: "Grant lacks the resolve scope", + } + ) + @files_ns.response(200, "Files resolved", files_ns.models[FileResolveResponse.__name__]) + def post(self, grant: FileGrantClaims): + try: + payload = FileResolvePayload.model_validate(files_ns.payload or {}) + except ValidationError as exc: + raise InvalidFileRequestError(str(exc)) from exc + + refs = [FileRef(id=ref.id, kind=ref.kind) for ref in payload.files] + try: + resolved = application_services().file_grants.resolve_file_access( + context=_grant_context(grant), + refs=refs, + ) + except EndUserNotFoundError as exc: + raise GrantedFileNotFoundError() from exc + except TooManyFileRefsError as exc: + raise InvalidFileRequestError(str(exc)) from exc + + return FileResolveResponse( + files=[ResolvedFileResponse.from_resolved(ref.id, file) for ref, file in zip(refs, resolved, strict=True)] + ).model_dump(mode="json") + + +@files_ns.route("/appdeploy//content") +class GrantedFileContentApi(Resource): + """Stream one file's bytes to a holder of its content token.""" + + @files_ns.doc("grant_file_content") + @files_ns.doc(params=query_params_from_model(FileContentQuery)) + @files_ns.doc( + responses={ + 200: "File stream returned", + 401: "Invalid or expired content token", + 404: "File not found", + } + ) + def get(self, file_id: UUID): + try: + query = FileContentQuery.model_validate(request.args.to_dict(flat=True)) + except ValidationError as exc: + raise FileGrantInvalidError() from exc + + try: + content = application_services().file_grants.load_content( + token=query.token, + requested_file_id=str(file_id), + ) + except InvalidFileGrantError as exc: + raise FileGrantInvalidError() from exc + if content is None: + raise GrantedFileNotFoundError() + + return _content_response(content) + + +def _content_response(content: FileContent) -> Response: + mime_type = _normalized_mime_type(content.mime_type) + inline = mime_type in INLINE_MIME_TYPES + + response = Response( + content.stream, + mimetype=mime_type if inline else "application/octet-stream", + direct_passthrough=True, + headers={}, + ) + response.headers["X-Content-Type-Options"] = "nosniff" + if not inline: + encoded_filename = quote(content.name or "") + response.headers["Content-Disposition"] = f"attachment; filename*=UTF-8''{encoded_filename}" + response.headers["Content-Type"] = "application/octet-stream" + # The sibling preview endpoints advertise `Accept-Ranges` for audio and video. + # Every such type downloads here, so the hint could only ever ride on a + # response no player will seek. + if content.size > 0: + response.headers["Content-Length"] = str(content.size) + return response + + +def _normalized_mime_type(mime_type: str | None) -> str: + return mime_type.split(";", 1)[0].strip().lower() if mime_type else "" + + +def _single_upload() -> FileStorage: + if "file" not in request.files: + raise NoFileUploadedError() + if len(request.files) > 1: + raise TooManyFilesError() + + upload = request.files["file"] + if not upload.filename: + raise FilenameNotExistsError() + return upload + + +def _store_upload( + grant: FileGrantClaims, + *, + filename: str, + stream: IO[bytes], + mimetype: str, +) -> StoredUpload: + try: + return application_services().file_grants.store_upload( + context=_grant_context(grant), + filename=filename, + stream=stream, + mimetype=mimetype, + ) + except EndUserNotFoundError as exc: + raise GrantedFileNotFoundError() from exc + except services.errors.file.FileTooLargeError as exc: + raise FileTooLargeError(exc.description) + except services.errors.file.BlockedFileExtensionError as exc: + raise BlockedFileExtensionError(exc.description) from exc + except services.errors.file.UnsupportedFileTypeError: + raise UnsupportedFileTypeError() + + +def _grant_context(grant: FileGrantClaims) -> FileGrantContext: + return FileGrantContext(tenant_id=grant.tenant_id, app_id=grant.app_id, end_user_id=grant.sub) + + +def _granted_file_response(upload_file: StoredUpload) -> dict[str, object]: + """Answer an upload exactly as dify's own upload endpoints answer it. + + A client moving off ``POST /v1/files/upload`` must not have to read a second + shape, so the same model reads the same ``upload_files`` row: every key dify + leaves null for such a row is null here too. Only the value of ``source_url`` + is ours. Dify signs a ``file-preview`` URL there and this channel signs a + content-token URL, which keeps that key's promise of a signed URL that + retrieves the file while keeping the grant its only way in. + """ + + signed_url, _ = application_services().file_grants.content_urls(file_id=upload_file.id, kind=FileKind.UPLOAD) + return dump_response(FileResponse, upload_file) | {"source_url": signed_url} + + +def _remote_granted_file_response(upload_file: StoredUpload) -> dict[str, object]: + """Answer a remote upload with the upload shape plus dify's ``url`` key. + + Reuses the URL already signed for ``source_url`` rather than signing a + second one, so the two keys are one value under the two names dify's two + kinds of client look for. + """ + + response = _granted_file_response(upload_file) + return response | {"url": response["source_url"]} + + +__all__ = [ + "GrantedFileContentApi", + "GrantedFileResolveApi", + "GrantedFileUploadApi", + "GrantedRemoteFileUploadApi", + "ProducedFileApi", +] diff --git a/api/controllers/files/wraps.py b/api/controllers/files/wraps.py new file mode 100644 index 00000000000..b72925028d8 --- /dev/null +++ b/api/controllers/files/wraps.py @@ -0,0 +1,57 @@ +"""Bearer authentication for the AppDeploy file grant endpoints.""" + +from collections.abc import Callable +from functools import wraps + +from flask import request + +from extensions.ext_application_services import application_services +from libs.exception import BaseHTTPException +from services.entities.file_grant_entities import FileGrantClaims, FileGrantScope + + +class FileGrantInvalidError(BaseHTTPException): + error_code = "grant_invalid" + description = "The file grant is missing, malformed, or expired." + code = 401 + + +class FileGrantScopeDeniedError(BaseHTTPException): + error_code = "grant_scope_denied" + description = "The file grant does not carry the required scope." + code = 403 + + +class GrantedFileNotFoundError(BaseHTTPException): + error_code = "file_not_found" + description = "File not found." + code = 404 + + +def file_grant_required[**P, R](scope: FileGrantScope) -> Callable[[Callable[P, R]], Callable[P, R]]: + """Require a valid file grant carrying ``scope`` and inject its claims.""" + + def decorator(view: Callable[P, R]) -> Callable[P, R]: + @wraps(view) + def decorated(*args: P.args, **kwargs: P.kwargs) -> R: + kwargs["grant"] = _authenticated_claims(scope) + return view(*args, **kwargs) + + return decorated + + return decorator + + +def _authenticated_claims(scope: FileGrantScope) -> FileGrantClaims: + scheme, _, token = request.headers.get("Authorization", "").partition(" ") + if scheme.lower() != "bearer" or not token: + raise FileGrantInvalidError() + + claims = application_services().file_grants.decode_grant(token) + if claims is None: + raise FileGrantInvalidError() + + if scope not in claims.scopes: + raise FileGrantScopeDeniedError() + + return claims diff --git a/api/controllers/inner_api/__init__.py b/api/controllers/inner_api/__init__.py index cb7ddd7f107..1656df8aaff 100644 --- a/api/controllers/inner_api/__init__.py +++ b/api/controllers/inner_api/__init__.py @@ -21,6 +21,7 @@ from .agent import files as _agent_files from .agent import llm as _agent_llm from .agent import tools as _agent_tools from .app import dsl as _app_dsl +from .app import file_grants as _app_file_grants from .knowledge import retrieval as _knowledge_retrieval from .plugin import agent_config as _agent_config from .plugin import plugin as _plugin @@ -35,6 +36,7 @@ __all__ = [ "_agent_llm", "_agent_tools", "_app_dsl", + "_app_file_grants", "_knowledge_retrieval", "_mail", "_plugin", diff --git a/api/controllers/inner_api/app/file_grants.py b/api/controllers/inner_api/app/file_grants.py new file mode 100644 index 00000000000..0acc9754c73 --- /dev/null +++ b/api/controllers/inner_api/app/file_grants.py @@ -0,0 +1,203 @@ +"""Mint AppDeploy file grants for the enterprise control plane. + +This is the only endpoint that asserts an AppDeploy identity: the application +service upserts the subject's ``EndUser`` row, validates the files the caller +claims to reference, and signs a short-lived grant. +""" + +from __future__ import annotations + +from flask_restx import Resource +from pydantic import BaseModel, Field, ValidationError + +from controllers.common.schema import register_response_schema_models, register_schema_models +from controllers.console.wraps import setup_required +from controllers.files.wraps import GrantedFileNotFoundError +from controllers.inner_api import inner_api_ns +from controllers.inner_api.wraps import enterprise_inner_api_only +from extensions.ext_application_services import application_services +from fields.base import ResponseModel +from fields.file_grant_fields import ResolvedFileResponse +from libs.exception import BaseHTTPException +from services.entities.file_grant_entities import FileGrantMintRequest, FileGrantScope, FileKind, FileRef +from services.file_grant_service import ( + AppNotFoundError, + EndUserNotFoundError, + TooManyFileRefsError, +) +from services.file_grant_service import ( + GrantedFileNotFoundError as ServiceGrantedFileNotFoundError, +) +from services.file_grant_service import ( + GrantTtlTooLongError as ServiceGrantTtlTooLongError, +) +from services.file_grant_service import ( + InvalidGrantRequestError as ServiceInvalidGrantRequestError, +) +from services.file_grant_service import ( + InvalidSubjectError as ServiceInvalidSubjectError, +) + + +class InvalidGrantRequestError(BaseHTTPException): + error_code = "invalid_request" + description = "The file grant request is malformed." + code = 400 + + +class GrantTtlTooLongError(BaseHTTPException): + error_code = "grant_ttl_too_long" + description = "The requested file grant lifetime exceeds its allowed window." + code = 400 + + +class InvalidSubjectError(BaseHTTPException): + error_code = "invalid_subject" + description = "The subject is empty or contains a NUL byte." + code = 400 + + +class GrantAppNotFoundError(BaseHTTPException): + error_code = "app_not_found" + description = "App not found." + code = 404 + + +class FileGrantFileRef(BaseModel): + id: str + kind: FileKind + + +class FileGrantMintPayload(BaseModel): + tenant_id: str + app_id: str + subject: str + is_anonymous: bool = False + scopes: list[FileGrantScope] + ttl_seconds: int = Field(gt=0) + # Recorded by the enterprise caller's own audit log; it never enters the + # grant because a rotated key must not strand the files it uploaded. + actor_key_digest: str | None = None + file_ids: list[FileGrantFileRef] = Field(default_factory=list) + optional_file_ids: list[FileGrantFileRef] = Field(default_factory=list) + run_deadline: int | None = None + + +class FileGrantLimits(ResponseModel): + file_size_limit: int + image_file_size_limit: int + audio_file_size_limit: int + video_file_size_limit: int + workflow_file_upload_limit: int + batch_count_limit: int + + +class FileGrantFileMetadata(ResponseModel): + id: str + kind: FileKind + name: str + size: int + extension: str + mime_type: str | None = None + + +class FileGrantMintResponse(ResponseModel): + grant: str + expires_at: int + limits: FileGrantLimits + files: list[FileGrantFileMetadata] + optional_files: list[ResolvedFileResponse] + + +register_schema_models(inner_api_ns, FileGrantMintPayload) +register_response_schema_models(inner_api_ns, FileGrantMintResponse) + + +@inner_api_ns.route("/enterprise/file-grants") +class EnterpriseFileGrantApi(Resource): + """Assert one AppDeploy subject and sign a grant for it.""" + + @setup_required + @enterprise_inner_api_only + @inner_api_ns.doc("enterprise_mint_file_grant") + @inner_api_ns.expect(inner_api_ns.models[FileGrantMintPayload.__name__]) + @inner_api_ns.response( + 200, + "File grant minted", + inner_api_ns.models[FileGrantMintResponse.__name__], + ) + @inner_api_ns.doc( + responses={ + 400: "Invalid request, subject, or grant TTL", + 404: "App not found, or a required file is not owned by the subject", + } + ) + def post(self): + try: + payload = FileGrantMintPayload.model_validate(inner_api_ns.payload or {}) + except ValidationError as exc: + raise InvalidGrantRequestError(str(exc)) from exc + + try: + result = application_services().file_grants.mint( + FileGrantMintRequest( + tenant_id=payload.tenant_id, + app_id=payload.app_id, + subject=payload.subject, + is_anonymous=payload.is_anonymous, + scopes=tuple(payload.scopes), + ttl_seconds=payload.ttl_seconds, + file_refs=tuple(FileRef(id=ref.id, kind=ref.kind) for ref in payload.file_ids), + optional_file_refs=tuple(FileRef(id=ref.id, kind=ref.kind) for ref in payload.optional_file_ids), + run_deadline=payload.run_deadline, + ) + ) + except AppNotFoundError as exc: + raise GrantAppNotFoundError() from exc + except ServiceGrantTtlTooLongError as exc: + raise GrantTtlTooLongError() from exc + except ServiceInvalidSubjectError as exc: + raise InvalidSubjectError() from exc + except (ServiceInvalidGrantRequestError, TooManyFileRefsError) as exc: + raise InvalidGrantRequestError(str(exc)) from exc + except (EndUserNotFoundError, ServiceGrantedFileNotFoundError) as exc: + raise GrantedFileNotFoundError() from exc + + return FileGrantMintResponse( + grant=result.grant, + expires_at=result.expires_at, + limits=FileGrantLimits( + file_size_limit=result.limits.file_size_limit, + image_file_size_limit=result.limits.image_file_size_limit, + audio_file_size_limit=result.limits.audio_file_size_limit, + video_file_size_limit=result.limits.video_file_size_limit, + workflow_file_upload_limit=result.limits.workflow_file_upload_limit, + batch_count_limit=result.limits.batch_count_limit, + ), + files=[ + FileGrantFileMetadata( + id=file.id, + kind=file.kind, + name=file.name, + size=file.size, + extension=file.extension, + mime_type=file.mime_type, + ) + for file in result.files + ], + optional_files=[ + ResolvedFileResponse.from_resolved(ref.id, access) + for ref, access in zip( + payload.optional_file_ids, + result.optional_files, + strict=True, + ) + ], + ).model_dump(mode="json") + + +__all__ = [ + "EnterpriseFileGrantApi", + "FileGrantMintPayload", + "FileGrantMintResponse", +] diff --git a/api/controllers/openapi/_contract.py b/api/controllers/openapi/_contract.py index a7dcf9093da..aa92cdf411d 100644 --- a/api/controllers/openapi/_contract.py +++ b/api/controllers/openapi/_contract.py @@ -5,8 +5,9 @@ reference — emitting the Swagger schema AND doing the runtime validation/ serialisation — so the advertised and enforced contracts can't drift. Validation failures map to a single shape: 422. -They must sit BELOW ``@auth_router.guard`` so auth runs before validation and the -``view.__wrapped__`` unit-test seam unwraps exactly the guard layer. +They must sit below route admission (or a direct ``@auth_router.guard``) so +authentication runs before validation and the ``view.__wrapped__`` unit-test +seam can bypass the outer admission layer. """ from __future__ import annotations diff --git a/api/controllers/openapi/account.py b/api/controllers/openapi/account.py index f1b02ef115e..6ddd9a14ef2 100644 --- a/api/controllers/openapi/account.py +++ b/api/controllers/openapi/account.py @@ -1,12 +1,11 @@ from __future__ import annotations from datetime import UTC, datetime +from uuid import UUID from flask_restx import Resource -from sqlalchemy.orm import Session -from werkzeug.exceptions import NotFound +from werkzeug.exceptions import NotFound, Unauthorized -from controllers.common.session import with_session from controllers.openapi import openapi_ns from controllers.openapi._contract import accepts, returns from controllers.openapi._models import ( @@ -18,114 +17,77 @@ from controllers.openapi._models import ( SessionRow, WorkspacePayload, ) -from controllers.openapi.auth.composition import auth_router -from controllers.openapi.auth.data import AuthData -from extensions.ext_redis import redis_client -from libs.oauth_bearer import ( - Scope, - TokenType, - get_auth_ctx, -) -from libs.rate_limit import ( - LIMIT_ME_PER_ACCOUNT, - enforce, -) -from services.account_service import AccountService, TenantService -from services.oauth_device_flow import ( - list_active_sessions, - revoke_oauth_token, - token_belongs_to_subject, -) +from controllers.openapi.flask_admission import openapi_account_admission +from extensions.ext_application_services import application_services +from libs.oauth_bearer import Scope +from libs.rate_limit import LIMIT_ME_PER_ACCOUNT +from machinery.context import AccountRequestContext +from services.account_errors import AccountNotFoundError, AccountSessionNotFoundError +from services.entities.account_access_entities import AccountSessionSnapshot, AccountWorkspaceSnapshot +from services.entities.account_entities import AccountSnapshot @openapi_ns.route("/account") class AccountApi(Resource): - @auth_router.guard(scope=Scope.FULL, allowed_token_types=frozenset({TokenType.OAUTH_ACCOUNT})) + @openapi_account_admission(scope=Scope.FULL, rate_limit=LIMIT_ME_PER_ACCOUNT) @returns(200, AccountResponse, description="Account info") - @with_session(write=False) - def get(self, session: Session, *, auth_data: AuthData): - enforce(LIMIT_ME_PER_ACCOUNT, key=f"account:{auth_data.account_id}") - - account_id_str = str(auth_data.account_id) if auth_data.account_id else None - account = AccountService.get_account_by_id(account_id_str, session=session) if account_id_str else None - memberships = TenantService.get_account_memberships(account_id_str, session=session) if account_id_str else [] - default_ws_id = _pick_default_workspace(memberships) - + def get(self, request_context: AccountRequestContext): + try: + snapshot = application_services().accounts.access.get(request_context) + except AccountNotFoundError: + raise Unauthorized("account not found") from None return AccountResponse( subject_type="account", - subject_email=account.email if account else None, - account=_account_payload(account) if account else None, - workspaces=[_workspace_payload(m) for m in memberships], - default_workspace_id=default_ws_id, + subject_email=snapshot.account.email, + account=_account_payload(snapshot.account), + workspaces=[_workspace_payload(workspace) for workspace in snapshot.workspaces], + default_workspace_id=snapshot.default_workspace_id, ) @openapi_ns.route("/account/sessions/self") class AccountSessionsSelfApi(Resource): - @auth_router.guard(scope=Scope.FULL, allowed_token_types=frozenset({TokenType.OAUTH_ACCOUNT})) + @openapi_account_admission(scope=Scope.FULL) @returns(200, RevokeResponse, description="Session revoked") - @with_session - def delete(self, session: Session, *, auth_data: AuthData): - revoke_oauth_token(redis_client, str(auth_data.token_id), session=session) + def delete(self, request_context: AccountRequestContext): + application_services().accounts.access.revoke_current_session(request_context) return RevokeResponse(status="revoked") @openapi_ns.route("/account/sessions") class AccountSessionsApi(Resource): - @auth_router.guard(scope=Scope.FULL, allowed_token_types=frozenset({TokenType.OAUTH_ACCOUNT})) + @openapi_account_admission(scope=Scope.FULL) @returns(200, SessionListResponse, description="Session list") @accepts(query=SessionListQuery) - @with_session(write=False) - def get(self, session: Session, *, auth_data: AuthData, query: SessionListQuery): - # SessionListQuery enforces the advertised bounds (extra='forbid', page>=1, - # 1<=limit<=MAX_PAGE_LIMIT) so the server rejects out-of-range paging rather - # than silently coercing (e.g. page=0 -> empty slice). - ctx = get_auth_ctx() - now = datetime.now(UTC) - page = query.page - limit = query.limit - - all_rows = list_active_sessions(ctx, now, session=session) - - total = len(all_rows) - sliced = all_rows[(page - 1) * limit : page * limit] - - items = [ - SessionRow( - id=str(r.id), - prefix=r.prefix, - client_id=r.client_id, - device_label=r.device_label, - created_at=_iso(r.created_at), - last_used_at=_iso(r.last_used_at), - expires_at=_iso(r.expires_at), - ) - for r in sliced - ] - + def get(self, request_context: AccountRequestContext, *, query: SessionListQuery): + page = application_services().accounts.access.list_sessions( + request_context, + page=query.page, + limit=query.limit, + ) return SessionListResponse( - page=page, - limit=limit, - total=total, - has_more=page * limit < total, - data=items, + page=page.page, + limit=page.limit, + total=page.total, + has_more=page.has_more, + data=[_session_row(session) for session in page.items], ) @openapi_ns.route("/account/sessions/") class AccountSessionByIdApi(Resource): - @auth_router.guard(scope=Scope.FULL, allowed_token_types=frozenset({TokenType.OAUTH_ACCOUNT})) + @openapi_account_admission(scope=Scope.FULL) @returns(200, RevokeResponse, description="Session revoked") - @with_session - def delete(self, session: Session, session_id: str, *, auth_data: AuthData): - ctx = get_auth_ctx() - - # 404 (not 403) on cross-subject so the endpoint doesn't leak - # token IDs that belong to other subjects. - if not token_belongs_to_subject(session_id, ctx, session=session): - raise NotFound("session not found") - - revoke_oauth_token(redis_client, session_id, session=session) + def delete(self, request_context: AccountRequestContext, session_id: str): + try: + token_id = str(UUID(session_id)) + except ValueError: + raise NotFound("session not found") from None + try: + application_services().accounts.access.revoke_session(request_context, token_id=token_id) + except AccountSessionNotFoundError: + # Do not reveal whether a token ID belongs to another account. + raise NotFound("session not found") from None return RevokeResponse(status="revoked") @@ -137,19 +99,21 @@ def _iso(dt: datetime | None) -> str | None: return dt.isoformat().replace("+00:00", "Z") -def _pick_default_workspace(memberships) -> str | None: - if not memberships: - return None - for join, tenant in memberships: - if getattr(join, "current", False): - return str(tenant.id) - return str(memberships[0][1].id) +def _session_row(session: AccountSessionSnapshot) -> SessionRow: + return SessionRow( + id=session.id, + prefix=session.prefix, + client_id=session.client_id, + device_label=session.device_label, + created_at=_iso(session.created_at), + last_used_at=_iso(session.last_used_at), + expires_at=_iso(session.expires_at), + ) -def _workspace_payload(row) -> WorkspacePayload: - join, tenant = row - return WorkspacePayload(id=str(tenant.id), name=tenant.name, role=getattr(join, "role", "")) +def _workspace_payload(workspace: AccountWorkspaceSnapshot) -> WorkspacePayload: + return WorkspacePayload(id=workspace.id, name=workspace.name, role=workspace.role) -def _account_payload(account) -> AccountPayload: - return AccountPayload(id=str(account.id), email=account.email, name=account.name) +def _account_payload(account: AccountSnapshot) -> AccountPayload: + return AccountPayload(id=account.id, email=account.email, name=account.name) diff --git a/api/controllers/openapi/auth/pipeline.py b/api/controllers/openapi/auth/pipeline.py index 6a68e5c91fe..9f14105a942 100644 --- a/api/controllers/openapi/auth/pipeline.py +++ b/api/controllers/openapi/auth/pipeline.py @@ -119,8 +119,8 @@ class PipelineRouter: """Entry point for openapi auth. `guard()` is the decorator that endpoints attach to. It applies - global gates (edition, token type) then dispatches to the matching - `PipelineRoute` for the token type. + global gates (edition, license, token type) then dispatches to the + matching `PipelineRoute` for the token type. """ def __init__(self, routes: dict[TokenType, PipelineRoute]) -> None: @@ -132,6 +132,7 @@ class PipelineRouter: scope: Scope | None = None, allowed_token_types: frozenset[TokenType] | None = None, edition: frozenset[DeploymentEdition] | None = None, + require_valid_enterprise_license: bool = False, workspace_membership: bool = False, allowed_roles: frozenset[TenantAccountRole] | None = None, rbac: RBACRequirement | None = None, @@ -140,6 +141,7 @@ class PipelineRouter: scope=scope, allowed_token_types=allowed_token_types, edition=edition, + require_valid_enterprise_license=require_valid_enterprise_license, workspace_membership=workspace_membership, allowed_roles=allowed_roles, rbac=rbac, @@ -151,6 +153,7 @@ class PipelineRouter: scope: Scope | None = None, allowed_token_types: frozenset[TokenType] | None = None, edition: frozenset[DeploymentEdition] | None = None, + require_valid_enterprise_license: bool = False, allowed_roles: frozenset[TenantAccountRole] | None = None, rbac: RBACRequirement | None = None, ) -> Callable: @@ -158,6 +161,7 @@ class PipelineRouter: scope=scope, allowed_token_types=allowed_token_types, edition=edition, + require_valid_enterprise_license=require_valid_enterprise_license, workspace_membership=True, allowed_roles=allowed_roles, rbac=rbac, @@ -169,6 +173,7 @@ class PipelineRouter: scope: Scope | None, allowed_token_types: frozenset[TokenType] | None, edition: frozenset[DeploymentEdition] | None, + require_valid_enterprise_license: bool, workspace_membership: bool, allowed_roles: frozenset[TenantAccountRole] | None, rbac: RBACRequirement | None, @@ -183,6 +188,7 @@ class PipelineRouter: scope=scope, allowed_token_types=allowed_token_types, edition=edition, + require_valid_enterprise_license=require_valid_enterprise_license, workspace_membership=workspace_membership, allowed_roles=allowed_roles, rbac=rbac, @@ -201,6 +207,7 @@ class PipelineRouter: scope: Scope | None, allowed_token_types: frozenset[TokenType] | None, edition: frozenset[DeploymentEdition] | None, + require_valid_enterprise_license: bool, workspace_membership: bool = False, allowed_roles: frozenset[TenantAccountRole] | None = None, rbac: RBACRequirement | None = None, @@ -210,7 +217,9 @@ class PipelineRouter: raise NotFound() license_checked = False - if edition is not None and DeploymentEdition.ENTERPRISE in edition: + if dify_config.DEPLOYMENT_EDITION == DeploymentEdition.ENTERPRISE and ( + require_valid_enterprise_license or (edition is not None and DeploymentEdition.ENTERPRISE in edition) + ): _check_license() license_checked = True diff --git a/api/controllers/openapi/flask_admission.py b/api/controllers/openapi/flask_admission.py new file mode 100644 index 00000000000..7e485c70db6 --- /dev/null +++ b/api/controllers/openapi/flask_admission.py @@ -0,0 +1,88 @@ +"""Flask adapter for account-authenticated OpenAPI admission.""" + +from __future__ import annotations + +from collections.abc import Callable +from functools import wraps +from typing import Concatenate + +from flask import Response, request +from werkzeug.exceptions import Unauthorized + +from controllers.openapi.auth.composition import auth_router +from controllers.openapi.auth.data import AuthData +from core.logging.context import get_request_id, get_trace_id +from enums import DeploymentEdition +from libs.oauth_bearer import Scope, TokenType +from libs.rate_limit import RateLimit, enforce +from machinery.context import AccountRequestContext +from models.account import Account, AccountStatus + + +def openapi_account_admission[T, **P, R]( + *, + scope: Scope, + editions: frozenset[DeploymentEdition] | None = None, + require_initialized: bool = True, + require_valid_enterprise_license: bool = True, + rate_limit: RateLimit | None = None, +) -> Callable[ + [Callable[Concatenate[T, AccountRequestContext, P], R]], + Callable[Concatenate[T, P], R | Response], +]: + """Authenticate an account bearer and inject framework-neutral identity. + + Client-version admission remains attached to the OpenAPI blueprint so it + can also reject requests for removed routes. Edition and Enterprise + license checks are delegated to the shared auth router before the stable + context is constructed. + """ + + def decorator( + view: Callable[Concatenate[T, AccountRequestContext, P], R], + ) -> Callable[Concatenate[T, P], R | Response]: + @wraps(view) + def inject_request_context( + self: T, + /, + *args: P.args, + **kwargs: P.kwargs, + ) -> R: + auth_data = kwargs.pop("auth_data", None) + if not isinstance(auth_data, AuthData): + raise RuntimeError("OpenAPI auth pipeline did not provide valid AuthData") + account = auth_data.caller + if not isinstance(account, Account) or auth_data.account_id is None: + raise Unauthorized("account not found") + if require_initialized and account.status == AccountStatus.UNINITIALIZED: + raise Unauthorized("account not initialized") + + account_id = str(auth_data.account_id) + if rate_limit is not None: + enforce(rate_limit, key=f"account:{account_id}") + + context = AccountRequestContext( + request_id=get_request_id(), + trace_id=get_trace_id() or request.headers.get("X-Trace-Id"), + account_id=account_id, + access_token_id=str(auth_data.token_id) if auth_data.token_id is not None else None, + ) + return view(self, context, *args, **kwargs) + + authenticated = auth_router.guard( + scope=scope, + allowed_token_types=frozenset({TokenType.OAUTH_ACCOUNT}), + edition=editions, + require_valid_enterprise_license=require_valid_enterprise_license, + )(inject_request_context) + + # Keep one stable test seam: one ``__wrapped__`` skips route admission + # and reaches input parsing/response handling. Client-version admission + # stays blueprint-wide by design. + @wraps(view) + def admitted(self: T, /, *args: P.args, **kwargs: P.kwargs) -> R | Response: + return authenticated(self, *args, **kwargs) + + return admitted + + return decorator diff --git a/api/controllers/service_api/app/workflow.py b/api/controllers/service_api/app/workflow.py index 33a2f3a4b64..d40438e6fa3 100644 --- a/api/controllers/service_api/app/workflow.py +++ b/api/controllers/service_api/app/workflow.py @@ -48,6 +48,7 @@ from core.errors.error import ( ) from core.helper.trace_id_helper import get_external_trace_id, get_trace_session_id, omit_trace_session_id_from_payload from enums import CloudPlan, DeploymentEdition +from extensions.ext_application_services import application_services from extensions.ext_database import db from extensions.ext_redis import redis_client from fields.base import ResponseModel @@ -71,7 +72,6 @@ from services.errors.app import ( TriggerWorkflowServiceModeUnavailableError as TriggerWorkflowServiceModeUnavailableServiceError, ) from services.errors.llm import InvokeRateLimitError -from services.workflow_app_service import WorkflowAppService logger = logging.getLogger(__name__) @@ -614,20 +614,16 @@ class WorkflowAppLogApi(Resource): created_at_before = isoparse(args.created_at__before) if args.created_at__before else None created_at_after = isoparse(args.created_at__after) if args.created_at__after else None - # get paginate workflow app logs - workflow_app_service = WorkflowAppService() - with sessionmaker(db.engine).begin() as session: - workflow_app_log_pagination = workflow_app_service.get_paginate_workflow_app_logs( - session=session, - app_model=app_model, - keyword=args.keyword, - status=status, - created_at_before=created_at_before, - created_at_after=created_at_after, - page=args.page, - limit=args.limit, - created_by_end_user_session_id=args.created_by_end_user_session_id, - created_by_account=args.created_by_account, - ) - - return dump_response(WorkflowAppLogPaginationResponse, workflow_app_log_pagination) + result = application_services().workflow_app_logs.list_logs( + tenant_id=app_model.tenant_id, + app_id=app_model.id, + keyword=args.keyword, + status=status, + created_at_before=created_at_before, + created_at_after=created_at_after, + page=args.page, + limit=args.limit, + created_by_end_user_session_id=args.created_by_end_user_session_id, + created_by_account=args.created_by_account, + ) + return dump_response(WorkflowAppLogPaginationResponse, result) diff --git a/api/controllers/service_api/dataset/dataset.py b/api/controllers/service_api/dataset/dataset.py index 09a404b9eff..32035f539b1 100644 --- a/api/controllers/service_api/dataset/dataset.py +++ b/api/controllers/service_api/dataset/dataset.py @@ -430,10 +430,11 @@ class DatasetListApi(DatasetApiResource): query_params["tag_ids"] = request.args.getlist("tag_ids") query = DatasetListQuery.model_validate(query_params) # provider = request.args.get("provider", default="vendor") + effective_limit = min(query.limit, 100) datasets, total = DatasetService.get_datasets( query.page, - query.limit, + effective_limit, session, tenant_id, current_user, @@ -467,8 +468,8 @@ class DatasetListApi(DatasetApiResource): item["embedding_available"] = True response = { "data": data, - "has_more": len(datasets) == query.limit, - "limit": query.limit, + "has_more": query.page * effective_limit < total, + "limit": effective_limit, "total": total, "page": query.page, } diff --git a/api/controllers/service_api/dataset/document.py b/api/controllers/service_api/dataset/document.py index 5049c2a3782..a2288a99c43 100644 --- a/api/controllers/service_api/dataset/document.py +++ b/api/controllers/service_api/dataset/document.py @@ -1015,8 +1015,9 @@ class DocumentListApi(DatasetApiResource): query = query.order_by(desc(Document.created_at), desc(Document.position)) + effective_limit = min(query_params.limit, 100) paginated_documents = paginate_query( - query, session=session, page=query_params.page, per_page=query_params.limit, max_per_page=100 + query, session=session, page=query_params.page, per_page=effective_limit, max_per_page=100 ) documents = paginated_documents.items @@ -1029,8 +1030,8 @@ class DocumentListApi(DatasetApiResource): response = { "data": document_responses(documents, session=session), - "has_more": len(documents) == query_params.limit, - "limit": query_params.limit, + "has_more": query_params.page * effective_limit < paginated_documents.total, + "limit": effective_limit, "total": paginated_documents.total, "page": query_params.page, } diff --git a/api/controllers/service_api/dataset/segment.py b/api/controllers/service_api/dataset/segment.py index 7585e5f3dc1..921c59a0942 100644 --- a/api/controllers/service_api/dataset/segment.py +++ b/api/controllers/service_api/dataset/segment.py @@ -281,7 +281,7 @@ class SegmentApi(DatasetApiResource): use_defaults_for_malformed_ints=True, ) page = args.page - limit = args.limit + limit = min(args.limit, 100) dataset_id_str = str(dataset_id) dataset = session.scalar( select(Dataset).where(Dataset.tenant_id == tenant_id, Dataset.id == dataset_id_str).limit(1) @@ -331,7 +331,7 @@ class SegmentApi(DatasetApiResource): "data": segment_responses_with_summaries(segments, summaries, session=session), "doc_form": document.doc_form, "total": total, - "has_more": len(segments) == limit, + "has_more": page * limit < total, "limit": limit, "page": page, } diff --git a/api/controllers/web/app.py b/api/controllers/web/app.py index 1e985b77cc1..929cd6ae51a 100644 --- a/api/controllers/web/app.py +++ b/api/controllers/web/app.py @@ -26,6 +26,7 @@ from libs.passport import PassportService from libs.token import extract_webapp_passport from models.model import App, EndUser from services.app_definition_query_service import AppDefinitionNotPublishedError, AppDefinitionUnavailableError +from services.web_passport_service import WebAppAuthType from services.webapp_access_query_service import ( WebAppAccessAppNotFoundError, WebAppAccessReferenceRequiredError, @@ -184,12 +185,16 @@ class AppWebAuthPermission(Resource): if not tk: raise Unauthorized("Access token is missing.") decoded = PassportService().verify(tk) - user_id = decoded.get("user_id", "visitor") except Unauthorized: raise WebAppAuthRequiredError() from None except Exception: logger.exception("Unexpected error during auth verification") raise + if decoded.get("auth_type") != WebAppAuthType.INTERNAL: + raise WebAppAuthRequiredError() + user_id = decoded.get("user_id") + if not user_id: + raise WebAppAuthRequiredError() try: is_allowed = webapp_access.is_user_allowed(user_id=str(user_id), app_id=app_id) diff --git a/api/controllers/web/passport.py b/api/controllers/web/passport.py index 3e4a7960984..46482b9c53e 100644 --- a/api/controllers/web/passport.py +++ b/api/controllers/web/passport.py @@ -5,6 +5,7 @@ from werkzeug.exceptions import NotFound, Unauthorized from constants import HEADER_NAME_APP_CODE from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models +from controllers.console.wraps import model_validate from controllers.web import web_ns from controllers.web.error import WebAppAuthRequiredError from extensions.ext_application_services import application_services @@ -48,12 +49,12 @@ class PassportResource(Resource): } ) @web_ns.response(200, "Passport retrieved successfully", web_ns.models[PassportAccessTokenResponse.__name__]) - def get(self): + @model_validate(PassportQuery) + def get(self, query: PassportQuery): app_code = request.headers.get(HEADER_NAME_APP_CODE) if app_code is None: raise Unauthorized("X-App-Code header is missing.") - query = PassportQuery.model_validate(request.args.to_dict(flat=True)) passport_request = WebPassportRequest( app_code=app_code, user_session_id=query.user_id, diff --git a/api/controllers/web/wraps.py b/api/controllers/web/wraps.py index 69e0edb059f..d6aac7e97c6 100644 --- a/api/controllers/web/wraps.py +++ b/api/controllers/web/wraps.py @@ -19,6 +19,7 @@ from models.model import App, EndUser, Site from services.app_service import AppService from services.enterprise.enterprise_service import EnterpriseService, WebAppAccessMode, WebAppSettings from services.system_feature_service import SystemFeatureService +from services.web_passport_gateways import resolve_web_app_auth_type from services.webapp_auth_service import WebAppAuthService @@ -133,6 +134,14 @@ def _validate_user_accessibility( if not webapp_settings: raise WebAppAuthRequiredError("Web app settings not found.") + auth_type = decoded.get("auth_type") + if not auth_type: + raise WebAppAuthRequiredError("Missing auth_type in the token.") + + expected_auth_type = resolve_web_app_auth_type(webapp_settings.access_mode) + if auth_type != expected_auth_type: + raise WebAppAuthRequiredError() + if WebAppAuthService.is_app_require_permission_check( access_mode=webapp_settings.access_mode, session=db.session() ): @@ -140,10 +149,7 @@ def _validate_user_accessibility( if not EnterpriseService.WebAppAuth.is_user_allowed_to_access_webapp(user_id, app_id): raise WebAppAuthAccessDeniedError() - auth_type = decoded.get("auth_type") granted_at = decoded.get("granted_at") - if not auth_type: - raise WebAppAuthAccessDeniedError("Missing auth_type in the token.") if not granted_at: raise WebAppAuthAccessDeniedError("Missing granted_at in the token.") # check if sso has been updated diff --git a/api/core/rag/index_processor/index_processor_base.py b/api/core/rag/index_processor/index_processor_base.py index 7af2c517b84..06bacd68669 100644 --- a/api/core/rag/index_processor/index_processor_base.py +++ b/api/core/rag/index_processor/index_processor_base.py @@ -190,7 +190,16 @@ class BaseIndexProcessor(ABC): upload_file_id_list.append(upload_file_id) continue if current_user: - upload_file_id = self._download_image(image.split(" ")[0], current_user) + image_url = image.split(" ")[0] + try: + parsed_url = urlparse(image_url) + except ValueError: + logging.debug("Skipping malformed image reference: %s", image_url) + continue + if parsed_url.scheme not in {"http", "https"} or not parsed_url.netloc: + logging.debug("Skipping non-HTTP image reference: %s", image_url) + continue + upload_file_id = self._download_image(image_url, current_user) if upload_file_id: upload_file_id_list.append(upload_file_id) diff --git a/api/core/workflow/nodes/agent/message_transformer.py b/api/core/workflow/nodes/agent/message_transformer.py index 4a145c84928..891cf4d0d77 100644 --- a/api/core/workflow/nodes/agent/message_transformer.py +++ b/api/core/workflow/nodes/agent/message_transformer.py @@ -27,6 +27,7 @@ from services.tools.builtin_tools_manage_service import BuiltinToolManageService from .events import AgentLogEvent from .exceptions import AgentNodeError, AgentVariableTypeError, ToolFileNotFoundError +from .think_tags import ThinkStreamState _file_access_controller = DatabaseFileAccessController() @@ -54,7 +55,7 @@ class AgentMessageTransformer: conversation_id=conversation_id, ) - text = "" + think_state = ThinkStreamState() files: list[File] = [] json_list: list[dict | list] = [] @@ -125,10 +126,10 @@ class AgentMessageTransformer: ) elif message.type == ToolInvokeMessage.MessageType.TEXT: assert isinstance(message.message, ToolInvokeMessage.TextMessage) - text += message.message.text + chunk = think_state.feed_text(message.message.text) yield StreamChunkEvent( selector=[node_id, "text"], - chunk=message.message.text, + chunk=chunk, is_final=False, ) elif message.type == ToolInvokeMessage.MessageType.JSON: @@ -152,8 +153,9 @@ class AgentMessageTransformer: linked_file = self._file_from_link_message(message=message, tenant_id=tenant_id) if linked_file is not None: files.append(linked_file) + yield from self._close_open_think(think_state=think_state, node_id=node_id) stream_text = f"{'File' if linked_file is not None else 'Link'}: {message.message.text}\n" - text += stream_text + think_state.feed_text(stream_text) yield StreamChunkEvent( selector=[node_id, "text"], chunk=stream_text, @@ -249,6 +251,8 @@ class AgentMessageTransformer: else: agent_logs.append(agent_log) + yield from self._close_open_think(think_state=think_state, node_id=node_id) + yield agent_log json_output: list[dict[str, Any] | list[Any]] = [] @@ -271,6 +275,8 @@ class AgentMessageTransformer: else: json_output.append({"data": []}) + yield from self._close_open_think(think_state=think_state, node_id=node_id) + yield StreamChunkEvent( selector=[node_id, "text"], chunk="", @@ -288,7 +294,7 @@ class AgentMessageTransformer: node_run_result=NodeRunResult( status=WorkflowNodeExecutionStatus.SUCCEEDED, outputs={ - "text": text, + "text": think_state.text, "usage": jsonable_encoder(llm_usage), "files": ArrayFileSegment(value=files), "json": json_output, @@ -304,6 +310,17 @@ class AgentMessageTransformer: ) ) + @staticmethod + def _close_open_think(*, think_state: ThinkStreamState, node_id: str) -> Generator[StreamChunkEvent, None, None]: + closed = think_state.close_if_open() + if closed is None: + return + yield StreamChunkEvent( + selector=[node_id, "text"], + chunk=closed, + is_final=False, + ) + @staticmethod def _file_from_link_message(*, message: ToolInvokeMessage, tenant_id: str) -> File | None: if not isinstance(message.message, ToolInvokeMessage.TextMessage): diff --git a/api/core/workflow/nodes/agent/think_tags.py b/api/core/workflow/nodes/agent/think_tags.py new file mode 100644 index 00000000000..fd6e741084c --- /dev/null +++ b/api/core/workflow/nodes/agent/think_tags.py @@ -0,0 +1,109 @@ +"""Normalize unclosed ```` tags in agent/workflow text streams. + +Reasoning models (GLM, DeepSeek, etc.) wrap chain-of-thought in ```` +tags. A tool call often interrupts generation before ```` is emitted, +so a later ```` or the final answer stays nested in the still-open tag +and the UI renders the reply as thinking. See https://github.com/langgenius/dify/issues/41558 +""" + +from __future__ import annotations + +THINK_OPEN = "" +THINK_CLOSE = "" + + +def has_unclosed_think(text: str) -> bool: + """Return True when a ```` is still open in ``text``.""" + _, inside, _ = _walk(text, inside=False, content_since_open=False, close_at_end=False) + return inside + + +def close_unclosed_think_tags(text: str) -> str: + """Insert missing ```` before a nested open tag and at end of text.""" + repaired, _, _ = _walk(text, inside=False, content_since_open=False, close_at_end=True) + return repaired + + +def normalize_think_chunk( + chunk: str, + *, + inside: bool, + content_since_open: bool = False, +) -> tuple[str, bool, bool]: + """Repair nested opens in a stream chunk and return updated open state. + + Does not append a trailing ```` — the caller closes on tool-call + interruptions and at end of stream. + """ + return _walk(chunk, inside=inside, content_since_open=content_since_open, close_at_end=False) + + +class ThinkStreamState: + """Track concatenated text and whether a ```` block is still open.""" + + def __init__(self) -> None: + self.text = "" + self.inside = False + self.content_since_open = False + + def feed_text(self, chunk: str) -> str: + repaired, self.inside, self.content_since_open = normalize_think_chunk( + chunk, + inside=self.inside, + content_since_open=self.content_since_open, + ) + self.text += repaired + return repaired + + def close_if_open(self) -> str | None: + if not self.inside: + return None + self.inside = False + self.content_since_open = False + self.text += THINK_CLOSE + return THINK_CLOSE + + +def _walk( + text: str, + *, + inside: bool, + content_since_open: bool, + close_at_end: bool, +) -> tuple[str, bool, bool]: + parts: list[str] = [] + i = 0 + length = len(text) + open_len = len(THINK_OPEN) + close_len = len(THINK_CLOSE) + + while i < length: + if text.startswith(THINK_OPEN, i): + if inside and content_since_open: + parts.append(THINK_CLOSE) + elif inside: + i += open_len + continue + parts.append(THINK_OPEN) + inside = True + content_since_open = False + i += open_len + continue + if text.startswith(THINK_CLOSE, i): + parts.append(THINK_CLOSE) + inside = False + content_since_open = False + i += close_len + continue + ch = text[i] + parts.append(ch) + if inside and not ch.isspace(): + content_since_open = True + i += 1 + + if close_at_end and inside: + parts.append(THINK_CLOSE) + inside = False + content_since_open = False + + return "".join(parts), inside, content_since_open diff --git a/api/events/event_handlers/sync_plugin_trigger_when_app_created.py b/api/events/event_handlers/sync_plugin_trigger_when_app_created.py index 68be37dfdbc..cc6e851ad87 100644 --- a/api/events/event_handlers/sync_plugin_trigger_when_app_created.py +++ b/api/events/event_handlers/sync_plugin_trigger_when_app_created.py @@ -1,6 +1,6 @@ import logging -from events.app_event import app_draft_workflow_was_synced +from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated from models.model import App, AppMode from models.workflow import Workflow from services.trigger.trigger_service import TriggerService @@ -9,14 +9,26 @@ logger = logging.getLogger(__name__) @app_draft_workflow_was_synced.connect -def handle(sender, synced_draft_workflow: Workflow, **kwargs): +@app_published_workflow_was_updated.connect +def handle( + sender, + synced_draft_workflow: Workflow | None = None, + published_workflow: Workflow | None = None, + **kwargs, +): """ - While creating a workflow or updating a workflow, we may need to sync - its plugin trigger relationships in DB. + Sync plugin trigger relationships when a draft changes or is published. + + The published workflow must be reconciled as well because production trigger + dispatch relies on these relationships, while debug dispatch does not. """ app: App = sender if app.mode != AppMode.WORKFLOW.value: # only handle workflow app, chatflow is not supported yet return - TriggerService.sync_plugin_trigger_relationships(app, synced_draft_workflow) + workflow = published_workflow if published_workflow is not None else synced_draft_workflow + if workflow is None: + return + + TriggerService.sync_plugin_trigger_relationships(app, workflow) diff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.py index 5885589c27b..b68354b194d 100644 --- a/api/extensions/ext_application_services.py +++ b/api/extensions/ext_application_services.py @@ -1,9 +1,11 @@ """Composition root for application services used by transport adapters.""" import json +import time from collections.abc import Mapping from dataclasses import dataclass from datetime import UTC, datetime +from functools import partial from typing import cast from uuid import uuid4 @@ -18,11 +20,14 @@ from constants.languages import languages from core.db.session_factory import get_session_maker from core.helper.ssrf_proxy import ssrf_proxy from core.schemas.schema_manager import SchemaManager +from core.tools.tool_file_manager import ToolFileManager from enums import DeploymentEdition, WebAppAccessMode from extensions.ext_redis import RedisClientWrapper, redis_client -from libs.datetime_utils import naive_utc_now +from extensions.ext_storage import storage +from libs.datetime_utils import naive_utc_now, utc_now from libs.helper import RateLimiter from libs.oauth import GitHubOAuth, GoogleOAuth +from libs.oauth_bearer import invalidate_oauth_token_cache from libs.passport import PassportService from repositories.account_activation_repository import SQLAlchemyAccountActivationRepository from repositories.account_integration_repository import SQLAlchemyAccountIntegrationRepository @@ -35,22 +40,29 @@ from repositories.account_oauth_repository import ( from repositories.account_repository import SQLAlchemyAccountRepository from repositories.app_definition_query_repository import AppDefinitionQueryRepository from repositories.app_site_command_repository import AppSiteCommandRepository +from repositories.app_statistic_query_repository import AppStatisticQueryRepository +from repositories.app_tracing_config_repository import SQLAlchemyAppTracingConfigRepository from repositories.data_source_api_key_auth_repository import SQLAlchemyDataSourceApiKeyAuthBindingRepository from repositories.data_source_oauth_binding_repository import SQLAlchemyDataSourceOAuthBindingRepository from repositories.explore_banner_query_repository import ExploreBannerQueryRepository from repositories.factory import DifyAPIRepositoryFactory +from repositories.file_grant_repository import FileGrantRepository from repositories.installation_state_repository import InstallationStateRepository +from repositories.oauth_access_token_repository import SQLAlchemyOAuthAccessTokenRepository from repositories.oauth_server_repository import RedisOAuthServerTokenRepository, SQLAlchemyOAuthServerRepository from repositories.recommended_app_catalog_repository import DatabaseRecommendedAppCatalogRepository +from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository from repositories.step_by_step_tour_repository import SQLAlchemyStepByStepTourStateRepository from repositories.tag_repository import TagRepository from repositories.trial_app_query_repository import TrialAppQueryRepository from repositories.trial_app_usage_repository import TrialAppUsageRepository from repositories.web_passport_repository import WebPassportRepository from repositories.webapp_access_query_repository import WebAppAccessQueryRepository +from repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepository from repositories.workflow_run_archive_repository import WorkflowRunArchiveBundleQueryRepository from repositories.workspace_member_query_repository import WorkspaceMemberQueryRepository from repositories.workspace_query_repository import WorkspaceQueryRepository +from services.account_access_service import AccountAccessService from services.account_activation_service import AccountActivationService from services.account_adapters import ( BillingAccountActivationEligibility, @@ -122,6 +134,9 @@ from services.account_password_service import AccountPasswordService from services.account_profile_service import AccountProfileService from services.app_definition_query_service import AppDefinitionQueryService from services.app_site_service import AppSiteService +from services.app_statistic_query import AppStatisticQuery +from services.app_tracing_config_gateway import OpsTraceManagerGateway +from services.app_tracing_config_service import AppTracingConfigService from services.auth.data_source_api_key_auth_gateways import ( ProviderApiKeyAuthCredentialValidator, TenantApiKeyAuthCredentialEncryptor, @@ -132,10 +147,13 @@ from services.billing_service import BillingService from services.compliance_download_service import ComplianceDownloadService from services.data_source_oauth_service import DataSourceOAuthService, InvalidDataSourceOAuthProviderError from services.enterprise.enterprise_service import EnterpriseService +from services.entities.file_grant_entities import FileGrantLimits from services.errors.enterprise import EnterpriseServiceError from services.explore_banner_query_service import ExploreBannerQueryService from services.feature_query_service import FeatureQueryService from services.feature_service_gateway import FeatureServiceGateway +from services.file_grant_gateways import FileGrantFileGateway, FileGrantRemoteFileGateway, FileGrantTokenGateway +from services.file_grant_service import FileGrantService from services.file_service import FileService from services.init_validation_service import InitValidationService from services.inner_mail_service import InnerMailService @@ -174,6 +192,8 @@ from services.webapp_access_query_service import ( WebAppAccessQueryService, WebAppAccessUnavailableError, ) +from services.workflow_app_log_query_service import WorkflowAppLogQueryService +from services.workflow_run_service import WorkflowRunService from services.workflow_statistic_query_service import WorkflowStatisticQueryService from services.workspace_member_query_service import WorkspaceMemberQueryService from services.workspace_member_role_resolver import DeploymentWorkspaceMemberRoleResolver @@ -204,6 +224,7 @@ def _is_user_allowed_to_access_webapp(user_id: str, app_id: str) -> bool: @dataclass(frozen=True, slots=True) class AccountServices: + access: AccountAccessService authentication: ConsoleAuthenticationService avatar: AccountAvatarService change_email: AccountChangeEmailService @@ -225,6 +246,8 @@ class ApplicationServices: account_activation: AccountActivationService app_definitions: AppDefinitionQueryService app_sites: AppSiteService + app_statistics: AppStatisticQuery + app_tracing_configs: AppTracingConfigService billing_portal: BillingPortalService compliance_downloads: ComplianceDownloadService data_source_api_key_auth: DataSourceApiKeyAuthService @@ -235,6 +258,7 @@ class ApplicationServices: schema_definitions: SchemaDefinitionService setup: SetupService feature_queries: FeatureQueryService + file_grants: FileGrantService files: FileService oauth_server: OAuthServerService init_validation: InitValidationService @@ -245,8 +269,10 @@ class ApplicationServices: remote_files: RemoteFileService trial_app_usage: TrialAppUsageRecorder workflow_run_archives: WorkflowRunArchiveService + workflow_runs: WorkflowRunService workspace_queries: WorkspaceQueryService workspace_member_queries: WorkspaceMemberQueryService + workflow_app_logs: WorkflowAppLogQueryService inner_mail: InnerMailService web_passport: WebPassportService tags: TagApplicationService @@ -293,6 +319,37 @@ def _build_oauth_server_service( ) +def _build_file_grant_service(*, database_client: sessionmaker[Session]) -> FileGrantService: + repository = FileGrantRepository(session_factory=database_client) + return FileGrantService( + repository=repository, + files=FileGrantFileGateway( + load_end_user=repository.get_end_user, + subject_exists=repository.subject_exists, + file_service=FileService(session_factory=database_client), + tool_files=ToolFileManager(), + storage=storage, + ), + remote_files=FileGrantRemoteFileGateway(), + tokens=FileGrantTokenGateway( + secret_key=dify_config.SECRET_KEY, + external_files_url=dify_config.FILES_URL, + internal_files_url=dify_config.INTERNAL_FILES_URL or dify_config.FILES_URL, + content_token_ttl_seconds=dify_config.FILES_ACCESS_TIMEOUT, + now=lambda: int(time.time()), + ), + limits=FileGrantLimits( + file_size_limit=dify_config.UPLOAD_FILE_SIZE_LIMIT, + image_file_size_limit=dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT, + audio_file_size_limit=dify_config.UPLOAD_AUDIO_FILE_SIZE_LIMIT, + video_file_size_limit=dify_config.UPLOAD_VIDEO_FILE_SIZE_LIMIT, + workflow_file_upload_limit=dify_config.WORKFLOW_FILE_UPLOAD_LIMIT, + batch_count_limit=dify_config.UPLOAD_FILE_BATCH_LIMIT, + ), + now=lambda: int(time.time()), + ) + + def _build_account_oauth_service( *, database_client: sessionmaker[Session], @@ -370,8 +427,19 @@ def build_application_services( invitation_tokens = RedisInvitationTokenStore(redis=redis) activation_accounts = SQLAlchemyAccountActivationRepository(session_factory=database_client) account_provisioning = SQLAlchemyConsoleAuthProvisioningGateway(session_factory=database_client) + workflow_run_repository = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=database_client) + workflow_node_execution_repository = DifyAPIRepositoryFactory.create_api_workflow_node_execution_repository( + session_maker=database_client + ) return ApplicationServices( accounts=AccountServices( + access=AccountAccessService( + accounts=accounts, + workspaces=workspace_query_repository, + sessions=SQLAlchemyOAuthAccessTokenRepository(session_factory=database_client), + invalidate_token_cache=partial(invalidate_oauth_token_cache, redis), + now=utc_now, + ), authentication=ConsoleAuthenticationService( accounts=accounts, workspaces=workspace_query_repository, @@ -527,6 +595,11 @@ def build_application_services( app_sites=AppSiteService( sites=AppSiteCommandRepository(session_factory=database_client), ), + app_statistics=AppStatisticQueryRepository(session_factory=database_client), + app_tracing_configs=AppTracingConfigService( + configs=SQLAlchemyAppTracingConfigRepository(session_factory=database_client), + provider=OpsTraceManagerGateway(), + ), billing_portal=BillingPortalService( accounts=accounts, get_subscription=BillingService.get_subscription, @@ -574,6 +647,7 @@ def build_application_services( features=feature_gateway, app_dsl_version=CURRENT_APP_DSL_VERSION, ), + file_grants=_build_file_grant_service(database_client=database_client), files=file_service, oauth_server=_build_oauth_server_service(database_client=database_client, redis=redis), init_validation=InitValidationService( @@ -582,7 +656,6 @@ def build_application_services( expected_password=initialization_password, ), notifications=NotificationService( - accounts=accounts, notifications=BillingNotificationGateway(), ), step_by_step_tour=StepByStepTourService( @@ -609,6 +682,10 @@ def build_application_services( dispatcher=dispatch_workflow_run_archive_download_task, sign_download_url=sign_workflow_run_archive_download_url, ), + workflow_runs=WorkflowRunService( + workflow_runs=workflow_run_repository, + node_executions=workflow_node_execution_repository, + ), workspace_queries=WorkspaceQueryService( workspaces=workspace_query_repository, plans=DeploymentWorkspacePlanGateway(), @@ -619,6 +696,9 @@ def build_application_services( ), roles=DeploymentWorkspaceMemberRoleResolver(), ), + workflow_app_logs=WorkflowAppLogQueryService( + logs=WorkflowAppLogQueryRepository(session_factory=database_client), + ), inner_mail=InnerMailService(dispatch=enqueue_inner_mail), web_passport=WebPassportService( passports=WebPassportRepository( diff --git a/api/fields/file_grant_fields.py b/api/fields/file_grant_fields.py new file mode 100644 index 00000000000..b0c550bb425 --- /dev/null +++ b/api/fields/file_grant_fields.py @@ -0,0 +1,49 @@ +"""Response DTOs shared by the AppDeploy file grant surfaces.""" + +from __future__ import annotations + +from fields.base import ResponseModel +from services.entities.file_grant_entities import FileKind, ResolvedFileAccess + + +class ResolvedFileResponse(ResponseModel): + """One requested file reference, either resolved or accounted for. + + The resolve endpoint and optional mint references answer item by item so one + missing history file cannot fail a whole run. A file that exists but belongs + to another owner is reported exactly like one that never existed. + """ + + id: str + ok: bool + kind: FileKind | None = None + name: str | None = None + size: int | None = None + extension: str | None = None + mime_type: str | None = None + url: str | None = None + internal_url: str | None = None + error: str | None = None + + @classmethod + def from_resolved(cls, file_id: str, access: ResolvedFileAccess | None) -> ResolvedFileResponse: + """Describe one resolved reference without performing infrastructure work.""" + + if access is None: + return cls(id=file_id, ok=False, error="not_found") + + file = access.file + return cls( + id=file.id, + ok=True, + kind=file.kind, + name=file.name, + size=file.size, + extension=file.extension, + mime_type=file.mime_type, + url=access.external_url, + internal_url=access.internal_url, + ) + + +__all__ = ["ResolvedFileResponse"] diff --git a/api/libs/datetime_utils.py b/api/libs/datetime_utils.py index d962d81e78a..a158bb1d5ea 100644 --- a/api/libs/datetime_utils.py +++ b/api/libs/datetime_utils.py @@ -17,11 +17,16 @@ class _NowFunction(Protocol): _now_func: _NowFunction = datetime.datetime.now +def utc_now() -> datetime.datetime: + """Return a timezone-aware datetime representing the current UTC time.""" + return _now_func(datetime.UTC) + + def naive_utc_now() -> datetime.datetime: """Return a naive datetime object (without timezone information) representing current UTC time. """ - return _now_func(datetime.UTC).replace(tzinfo=None) + return utc_now().replace(tzinfo=None) def ensure_naive_utc(dt: datetime.datetime) -> datetime.datetime: diff --git a/api/libs/oauth_bearer.py b/api/libs/oauth_bearer.py index ed17503bb3b..89ede4b8a12 100644 --- a/api/libs/oauth_bearer.py +++ b/api/libs/oauth_bearer.py @@ -318,6 +318,14 @@ AUDIT_OAUTH_EXPIRED = "oauth.token_expired" ScopeVariant = Literal["account", "external_sso"] +class _TokenCacheClient(Protocol): + def delete(self, *names: str | bytes) -> object: ... + + +def invalidate_oauth_token_cache(client: _TokenCacheClient, token_hash: str) -> None: + client.delete(TOKEN_CACHE_KEY_FMT.format(hash=token_hash)) + + class OAuthAccessTokenResolver: """``.for_account()`` / ``.for_external_sso()`` are variant-scoped views sharing DB + cache plumbing. @@ -385,7 +393,7 @@ class OAuthAccessTokenResolver: row_id, extra={"audit": True, "token_id": str(row_id)}, ) - self._redis.delete(self._cache_key(token_hash)) + invalidate_oauth_token_cache(self._redis, token_hash) self.cache_set_negative(token_hash) diff --git a/api/machinery/context.py b/api/machinery/context.py index 5d6f5436619..4facf6caf9b 100644 --- a/api/machinery/context.py +++ b/api/machinery/context.py @@ -8,3 +8,12 @@ class RequestContext(NamedTuple): trace_id: str | None account_id: str active_workspace_id: str + + +class AccountRequestContext(NamedTuple): + """Stable identity for account-scoped use cases that do not require a workspace.""" + + request_id: str + trace_id: str | None + account_id: str + access_token_id: str | None = None diff --git a/api/models/account.py b/api/models/account.py index 6700fc86c12..ed7bc2258b2 100644 --- a/api/models/account.py +++ b/api/models/account.py @@ -303,7 +303,7 @@ class TenantAccountJoin(TypeBase): ) tenant_id: Mapped[str] = mapped_column(StringUUID) account_id: Mapped[str] = mapped_column(StringUUID) - current: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.text("false"), default=False) + current: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.false(), default=False) role: Mapped[TenantAccountRole] = mapped_column( EnumText(TenantAccountRole, length=16), server_default="normal", default=TenantAccountRole.NORMAL ) diff --git a/api/models/agent.py b/api/models/agent.py index d981194c714..eb1fafae98a 100644 --- a/api/models/agent.py +++ b/api/models/agent.py @@ -189,13 +189,13 @@ class Agent(DefaultFieldsMixin, Base): workflow_node_id: Mapped[str | None] = mapped_column(String(255), nullable=True) active_config_snapshot_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True) active_config_has_model: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, default=False, server_default=sa.text("false") + sa.Boolean, nullable=False, default=False, server_default=sa.false() ) active_config_is_published: Mapped[bool] = mapped_column( sa.Boolean, nullable=False, default=False, - server_default=sa.text("false"), + server_default=sa.false(), comment=( "Whether the normal shared Agent draft has been published into the active config snapshot. " "User-scoped debug drafts do not affect this flag." diff --git a/api/models/comment.py b/api/models/comment.py index 222284f42b3..565169126f7 100644 --- a/api/models/comment.py +++ b/api/models/comment.py @@ -64,7 +64,7 @@ class WorkflowComment(TypeBase): resolved_at: Mapped[datetime | None] = mapped_column(sa.DateTime, default=None) resolved_by: Mapped[str | None] = mapped_column(StringUUID, default=None) - resolved: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) + resolved: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) # Relationships replies: Mapped[list[WorkflowCommentReply]] = relationship( lambda: WorkflowCommentReply, back_populates="comment", cascade="all, delete-orphan", init=False diff --git a/api/models/credential_permission.py b/api/models/credential_permission.py index effa78635b1..969a2e32d0d 100644 --- a/api/models/credential_permission.py +++ b/api/models/credential_permission.py @@ -45,9 +45,7 @@ class CredentialPermission(TypeBase): credential_type: Mapped[str] = mapped_column(String(40), nullable=False) account_id: Mapped[str] = mapped_column(StringUUID, nullable=False) tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False) - has_permission: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("true"), default=True - ) + has_permission: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), default=True) created_at: Mapped[datetime] = mapped_column( DateTime, nullable=False, server_default=func.current_timestamp(), init=False ) diff --git a/api/models/dataset.py b/api/models/dataset.py index 5e45d4b9a68..327c9503420 100644 --- a/api/models/dataset.py +++ b/api/models/dataset.py @@ -202,15 +202,15 @@ class Dataset(Base): collection_binding_id = mapped_column(StringUUID, nullable=True) retrieval_model = mapped_column(AdjustedJSON, nullable=True) summary_index_setting = mapped_column(AdjustedJSON, nullable=True) - built_in_field_enabled = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + built_in_field_enabled = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) icon_info = mapped_column(AdjustedJSON, nullable=True) runtime_mode = mapped_column( EnumText(DatasetRuntimeMode, length=255), nullable=True, server_default=sa.text("'general'") ) pipeline_id = mapped_column(StringUUID, nullable=True) chunk_structure = mapped_column(sa.String(255), nullable=True) - enable_api = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true")) - is_multimodal = mapped_column(sa.Boolean, default=False, nullable=False, server_default=sa.text("false")) + enable_api = mapped_column(sa.Boolean, nullable=False, server_default=sa.true()) + is_multimodal = mapped_column(sa.Boolean, default=False, nullable=False, server_default=sa.false()) def get_total_documents(self, *, session: Session) -> int: return self.get_document_count(session=session) @@ -560,7 +560,7 @@ class Document(Base): completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # pause - is_paused: Mapped[bool | None] = mapped_column(sa.Boolean, nullable=True, server_default=sa.text("false")) + is_paused: Mapped[bool | None] = mapped_column(sa.Boolean, nullable=True, server_default=sa.false()) paused_by = mapped_column(StringUUID, nullable=True) paused_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) @@ -572,10 +572,10 @@ class Document(Base): indexing_status = mapped_column( EnumText(IndexingStatus, length=255), nullable=False, server_default=sa.text("'waiting'") ) - enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true")) + enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true()) disabled_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) disabled_by = mapped_column(StringUUID, nullable=True) - archived: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + archived: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) archived_reason = mapped_column(String(255), nullable=True) archived_by = mapped_column(StringUUID, nullable=True) archived_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) @@ -588,7 +588,7 @@ class Document(Base): EnumText(IndexStructureType, length=255), nullable=False, server_default=sa.text("'text_model'") ) doc_language = mapped_column(String(255), nullable=True) - need_summary: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + need_summary: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) DATA_SOURCES = ["upload_file", "notion_import", "website_crawl"] @@ -901,7 +901,7 @@ class DocumentSegment(TypeBase): # indexing fields index_node_id: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None) index_node_hash: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None) - enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"), default=True) + enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), default=True) answer: Mapped[str | None] = mapped_column(LongText, nullable=True, default=None) keywords: Mapped[Any] = mapped_column(sa.JSON, nullable=True, default=None) disabled_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, default=None) @@ -1362,7 +1362,7 @@ class TidbAuthBinding(TypeBase): tenant_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True) cluster_id: Mapped[str] = mapped_column(String(255), nullable=False) cluster_name: Mapped[str] = mapped_column(String(255), nullable=False) - active: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + active: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) status: Mapped[TidbAuthBindingStatus] = mapped_column( EnumText(TidbAuthBindingStatus, length=255), nullable=False, server_default=sa.text("'CREATING'") ) @@ -1413,9 +1413,7 @@ class DatasetPermission(TypeBase): dataset_id: Mapped[str] = mapped_column(StringUUID, nullable=False) account_id: Mapped[str] = mapped_column(StringUUID, nullable=False) tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False) - has_permission: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("true"), default=True - ) + has_permission: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), default=True) created_at: Mapped[datetime] = mapped_column( DateTime, nullable=False, server_default=func.current_timestamp(), init=False ) @@ -1531,7 +1529,7 @@ class DatasetAutoDisableLog(TypeBase): tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False) dataset_id: Mapped[str] = mapped_column(StringUUID, nullable=False) document_id: Mapped[str] = mapped_column(StringUUID, nullable=False) - notified: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) + notified: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) created_at: Mapped[datetime] = mapped_column( DateTime, nullable=False, server_default=sa.func.current_timestamp(), init=False ) @@ -1687,10 +1685,8 @@ class Pipeline(TypeBase): name: Mapped[str] = mapped_column(sa.String(255), nullable=False) description: Mapped[str] = mapped_column(LongText, nullable=False, default=sa.text("''")) workflow_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True, default=None) - is_public: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) - is_published: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("false"), default=False - ) + is_public: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) + is_published: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) created_by: Mapped[str | None] = mapped_column(StringUUID, nullable=True, default=None) created_at: Mapped[datetime] = mapped_column( sa.DateTime, nullable=False, server_default=func.current_timestamp(), init=False @@ -1814,7 +1810,7 @@ class DocumentSegmentSummary(TypeBase): default=SummaryStatus.GENERATING, ) error: Mapped[str | None] = mapped_column(LongText, nullable=True, default=None) - enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"), default=True) + enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), default=True) disabled_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, default=None) disabled_by: Mapped[str | None] = mapped_column(StringUUID, nullable=True, default=None) created_at: Mapped[datetime] = mapped_column( diff --git a/api/models/enums.py b/api/models/enums.py index 4cdd46a4e12..944047d3ddd 100644 --- a/api/models/enums.py +++ b/api/models/enums.py @@ -208,6 +208,7 @@ class InvokeFrom(StrEnum): class EndUserType(StrEnum): """Persisted type values for the ``end_users.type`` column.""" + APP_DEPLOY = "app-deploy" BROWSER = "browser" MCP = "mcp" OPENAPI = "openapi" diff --git a/api/models/model.py b/api/models/model.py index dde53291b81..a6324973865 100644 --- a/api/models/model.py +++ b/api/models/model.py @@ -434,9 +434,9 @@ class App(Base): enable_api: Mapped[bool] = mapped_column(sa.Boolean) api_rpm: Mapped[int] = mapped_column(sa.Integer, server_default=sa.text("0")) api_rph: Mapped[int] = mapped_column(sa.Integer, server_default=sa.text("0")) - is_demo: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.text("false")) - is_public: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.text("false")) - is_universal: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.text("false")) + is_demo: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.false()) + is_public: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.false()) + is_universal: Mapped[bool] = mapped_column(sa.Boolean, server_default=sa.false()) tracing = mapped_column(LongText, nullable=True) max_active_requests: Mapped[int | None] created_by = mapped_column(StringUUID, nullable=True) @@ -446,7 +446,7 @@ class App(Base): updated_at: Mapped[datetime] = mapped_column( sa.DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp() ) - use_icon_as_answer_icon: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + use_icon_as_answer_icon: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) @property def desc_or_prompt(self) -> str: @@ -988,12 +988,8 @@ class RecommendedApp(TypeBase): custom_disclaimer: Mapped[str] = mapped_column(LongText, default="") position: Mapped[int] = mapped_column(sa.Integer, nullable=False, default=0) is_listed: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, default=True) - is_learn_dify: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("false"), default=False - ) - is_cloud_only: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("false"), default=False - ) + is_learn_dify: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) + is_cloud_only: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) install_count: Mapped[int] = mapped_column(sa.Integer, nullable=False, default=0) language: Mapped[str] = mapped_column( String(255), @@ -1033,7 +1029,7 @@ class InstalledApp(TypeBase): app_id: Mapped[str] = mapped_column(StringUUID, nullable=False) app_owner_tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False) position: Mapped[int] = mapped_column(sa.Integer, nullable=False, default=0) - is_pinned: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) + is_pinned: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) last_used_at: Mapped[datetime | None] = mapped_column(sa.DateTime, nullable=True, default=None) created_at: Mapped[datetime] = mapped_column( sa.DateTime, nullable=False, server_default=func.current_timestamp(), init=False @@ -1238,7 +1234,7 @@ class Conversation(Base): lambda: MessageAnnotation, backref="conversation", lazy="select", passive_deletes="all" ) - is_deleted: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + is_deleted: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) @property def inputs(self) -> dict[str, Any]: @@ -1597,7 +1593,7 @@ class Message(Base): updated_at: Mapped[datetime] = mapped_column( sa.DateTime, nullable=False, server_default=func.current_timestamp(), onupdate=func.current_timestamp() ) - agent_based: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + agent_based: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) workflow_run_id: Mapped[str | None] = mapped_column(StringUUID) app_mode: Mapped[AppMode | None] = mapped_column(EnumText(AppMode, length=255), nullable=True) @@ -2127,17 +2123,15 @@ class AppAnnotationHitHistory(TypeBase): annotation_question: Mapped[str] = mapped_column(LongText, nullable=False) annotation_content: Mapped[str] = mapped_column(LongText, nullable=False) - @property - def account(self): - return db.session.scalar( + def account(self, session: Session) -> Account | None: + return session.scalar( select(Account) .join(MessageAnnotation, MessageAnnotation.account_id == Account.id) .where(MessageAnnotation.id == self.annotation_id) ) - @property - def annotation_create_account(self): - return db.session.scalar(select(Account).where(Account.id == self.account_id)) + def annotation_create_account(self, session: Session) -> Account | None: + return session.scalar(select(Account).where(Account.id == self.account_id)) class AppAnnotationSetting(TypeBase): @@ -2248,9 +2242,7 @@ class EndUser(Base, UserMixin): type: Mapped[EndUserType] = mapped_column(EnumText(EndUserType, length=255), nullable=False) external_user_id = mapped_column(String(255), nullable=True) name = mapped_column(String(255)) - _is_anonymous: Mapped[bool] = mapped_column( - "is_anonymous", sa.Boolean, nullable=False, server_default=sa.text("true") - ) + _is_anonymous: Mapped[bool] = mapped_column("is_anonymous", sa.Boolean, nullable=False, server_default=sa.true()) @property @override @@ -2365,17 +2357,15 @@ class Site(TypeBase): customize_domain: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None) chat_color_theme: Mapped[str | None] = mapped_column(String(255), nullable=True, default=None) - prompt_public: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("false"), default=False - ) + prompt_public: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) chat_color_theme_inverted: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("false"), default=False + sa.Boolean, nullable=False, server_default=sa.false(), default=False ) show_workflow_steps: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("true"), default=True + sa.Boolean, nullable=False, server_default=sa.true(), default=True ) use_icon_as_answer_icon: Mapped[bool] = mapped_column( - sa.Boolean, nullable=False, server_default=sa.text("false"), default=False + sa.Boolean, nullable=False, server_default=sa.false(), default=False ) custom_disclaimer: Mapped[str] = mapped_column(LongText, nullable=False, default="") status: Mapped[AppStatus] = mapped_column( @@ -2510,7 +2500,7 @@ class UploadFile(TypeBase): # 3. Avoid relying on these fields for logic, as their values may not always be accurate. # # `used` may indicate whether the file has been utilized by another service. - used: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + used: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) # The `created_by_role` field indicates whether the file was created by an `Account` or an `EndUser`. # Its value is derived from the `CreatorUserRole` enumeration. @@ -2821,7 +2811,7 @@ class TraceAppConfig(TypeBase): onupdate=func.current_timestamp(), init=False, ) - is_active: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"), default=True) + is_active: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), default=True) @property def tracing_config_dict(self) -> dict[str, Any]: diff --git a/api/models/oauth.py b/api/models/oauth.py index 84be7eb37ce..b3effe1f5e2 100644 --- a/api/models/oauth.py +++ b/api/models/oauth.py @@ -45,7 +45,7 @@ class DatasourceProvider(TypeBase): encrypted_credentials: Mapped[dict[str, Any]] = mapped_column(AdjustedJSON, nullable=False) user_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True, default=None) avatar_url: Mapped[str] = mapped_column(LongText, nullable=True, default="default") - is_default: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) + is_default: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) expires_at: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default="-1", default=-1) visibility: Mapped[PermissionEnum] = mapped_column( EnumText(PermissionEnum, length=40), diff --git a/api/models/onboarding.py b/api/models/onboarding.py index 3495d91274e..e8d06070fc9 100644 --- a/api/models/onboarding.py +++ b/api/models/onboarding.py @@ -32,7 +32,7 @@ class AccountStepByStepTourState(TypeBase): ) account_id: Mapped[str] = mapped_column(StringUUID, nullable=False) first_workspace_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True, default=None) - skipped: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) + skipped: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) completed_task_ids: Mapped[list[str]] = mapped_column(AdjustedJSON, nullable=False, default_factory=list) manually_enabled_workspace_ids: Mapped[list[str]] = mapped_column( AdjustedJSON, diff --git a/api/models/snippet.py b/api/models/snippet.py index b6fbc4ed824..4a9320e51f6 100644 --- a/api/models/snippet.py +++ b/api/models/snippet.py @@ -47,7 +47,7 @@ class CustomizedSnippet(Base): workflow_id: Mapped[str | None] = mapped_column(StringUUID, nullable=True) # State flags - is_published: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false")) + is_published: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false()) version: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("1")) use_count: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=sa.text("0")) diff --git a/api/models/source.py b/api/models/source.py index 8fce7df205d..c8cc0eb6508 100644 --- a/api/models/source.py +++ b/api/models/source.py @@ -36,7 +36,7 @@ class DataSourceOauthBinding(TypeBase): onupdate=func.current_timestamp(), init=False, ) - disabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=True, server_default=sa.text("false"), default=False) + disabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=True, server_default=sa.false(), default=False) class DataSourceApiKeyAuthBindingDict(TypedDict): @@ -75,7 +75,7 @@ class DataSourceApiKeyAuthBinding(TypeBase): onupdate=func.current_timestamp(), init=False, ) - disabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=True, server_default=sa.text("false"), default=False) + disabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=True, server_default=sa.false(), default=False) def to_dict(self) -> DataSourceApiKeyAuthBindingDict: result: DataSourceApiKeyAuthBindingDict = { diff --git a/api/models/tools.py b/api/models/tools.py index c8468925aab..ceb54207a58 100644 --- a/api/models/tools.py +++ b/api/models/tools.py @@ -9,7 +9,7 @@ from uuid import uuid4 import sqlalchemy as sa from deprecated import deprecated from sqlalchemy import ForeignKey, String, func, select -from sqlalchemy.orm import Mapped, mapped_column +from sqlalchemy.orm import Mapped, Session, mapped_column from core.plugin.entities.plugin_daemon import CredentialType from core.tools.entities.common_entities import I18nObject @@ -62,7 +62,7 @@ class ToolOAuthTenantClient(TypeBase): tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False) plugin_id: Mapped[str] = mapped_column(String(255), nullable=False) provider: Mapped[str] = mapped_column(String(255), nullable=False) - enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"), init=False) + enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), init=False) # oauth params of the tool provider encrypted_oauth_params: Mapped[str] = mapped_column(LongText, nullable=False, init=False) @@ -109,7 +109,7 @@ class BuiltinToolProvider(TypeBase): onupdate=func.current_timestamp(), init=False, ) - is_default: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("false"), default=False) + is_default: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false(), default=False) # credential type, e.g., "api-key", "oauth2" credential_type: Mapped[CredentialType] = mapped_column( EnumText(CredentialType, length=32), @@ -203,9 +203,8 @@ class ApiToolProvider(TypeBase): return None return db.session.scalar(select(Account).where(Account.id == self.user_id)) - @property - def tenant(self) -> Tenant | None: - return db.session.scalar(select(Tenant).where(Tenant.id == self.tenant_id)) + def tenant(self, session: Session) -> Tenant | None: + return session.scalar(select(Tenant).where(Tenant.id == self.tenant_id)) class ToolLabelBinding(TypeBase): @@ -277,13 +276,11 @@ class WorkflowToolProvider(TypeBase): init=False, ) - @property - def user(self) -> Account | None: - return db.session.scalar(select(Account).where(Account.id == self.user_id)) + def user(self, session: Session) -> Account | None: + return session.scalar(select(Account).where(Account.id == self.user_id)) - @property - def tenant(self) -> Tenant | None: - return db.session.scalar(select(Tenant).where(Tenant.id == self.tenant_id)) + def tenant(self, session: Session) -> Tenant | None: + return session.scalar(select(Tenant).where(Tenant.id == self.tenant_id)) @property def parameter_configurations(self) -> list[WorkflowToolParameterConfiguration]: @@ -292,9 +289,8 @@ class WorkflowToolProvider(TypeBase): for config in json.loads(self.parameter_configuration) ] - @property - def app(self) -> App | None: - return db.session.scalar(select(App).where(App.id == self.app_id)) + def app(self, session: Session) -> App | None: + return session.scalar(select(App).where(App.id == self.app_id)) class MCPToolProvider(TypeBase): diff --git a/api/models/trigger.py b/api/models/trigger.py index fe61a479495..4da9314d7df 100644 --- a/api/models/trigger.py +++ b/api/models/trigger.py @@ -199,7 +199,7 @@ class TriggerOAuthTenantClient(TypeBase): tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False) plugin_id: Mapped[str] = mapped_column(String(255), nullable=False) provider: Mapped[str] = mapped_column(String(255), nullable=False) - enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.text("true"), default=True) + enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.true(), default=True) # oauth params of the trigger provider encrypted_oauth_params: Mapped[str] = mapped_column(LongText, nullable=False, default="{}") created_at: Mapped[datetime] = mapped_column( diff --git a/api/openapi/markdown/console-openapi.md b/api/openapi/markdown/console-openapi.md index 018fe4e47a8..2bf4c995c66 100644 --- a/api/openapi/markdown/console-openapi.md +++ b/api/openapi/markdown/console-openapi.md @@ -3228,8 +3228,10 @@ Delete an existing tracing configuration for an application | Code | Description | | ---- | ----------- | | 204 | Tracing configuration deleted successfully | -| 400 | Invalid request parameters or configuration not found | +| 400 | Invalid request parameters or unsupported tracing provider | | 403 | Insufficient permissions | +| 404 | Application or tracing configuration not found | +| 500 | Tracing configuration processing failed | ### [GET] /apps/{app_id}/trace-config Get tracing configuration for an application @@ -3246,7 +3248,9 @@ Get tracing configuration for an application | Code | Description | Schema | | ---- | ----------- | ------ | | 200 | Tracing configuration retrieved successfully | **application/json**: [TraceAppConfigResponse](#traceappconfigresponse)
| -| 400 | Invalid request parameters | | +| 400 | Invalid request parameters or unsupported tracing provider | | +| 404 | Application not found | | +| 500 | Tracing configuration processing failed | | ### [PATCH] /apps/{app_id}/trace-config **Update an existing trace app configuration** @@ -3270,8 +3274,10 @@ Update an existing tracing configuration for an application | Code | Description | Schema | | ---- | ----------- | ------ | | 200 | Tracing configuration updated successfully | **application/json**: [TraceAppConfigResponse](#traceappconfigresponse)
| -| 400 | Invalid request parameters or configuration not found | | +| 400 | Invalid request parameters or tracing configuration | | | 403 | Insufficient permissions | | +| 404 | Application or tracing configuration not found | | +| 500 | Tracing configuration processing failed | | ### [POST] /apps/{app_id}/trace-config **Create a new trace app configuration** @@ -3295,8 +3301,11 @@ Create a new tracing configuration for an application | Code | Description | Schema | | ---- | ----------- | ------ | | 201 | Tracing configuration created successfully | **application/json**: [TraceAppConfigResponse](#traceappconfigresponse)
| -| 400 | Invalid request parameters or configuration already exists | | +| 400 | Invalid request parameters or tracing configuration | | | 403 | Insufficient permissions | | +| 404 | Application not found | | +| 409 | Tracing configuration already exists | | +| 500 | Tracing configuration processing failed | | ### [POST] /apps/{app_id}/trigger-enable **Update app trigger (enable/disable)** @@ -6507,7 +6516,7 @@ Check if dataset is in use | Name | Located in | Description | Required | Schema | | ---- | ---------- | ----------- | -------- | ------ | -| language | query | Language code for recommended app localization | No | string | +| language | query | Language code for recommended app localization | No | string,
**Default:** en-US | #### Responses @@ -6520,7 +6529,7 @@ Check if dataset is in use | Name | Located in | Description | Required | Schema | | ---- | ---------- | ----------- | -------- | ------ | -| language | query | Language code for recommended app localization | No | string | +| language | query | Language code for recommended app localization | No | string,
**Default:** en-US | #### Responses @@ -7196,7 +7205,13 @@ Get instruction generation template | 302 | Redirect to OAuth callback page | ### [GET] /notification -Return the active in-product notification for the current user in their interface language (falls back to English if unavailable). The notification is NOT marked as seen here; call POST /notification/dismiss when the user explicitly closes the modal. +Return the active in-product notification for the current user in the requested language (defaults to English when omitted). Unavailable translations fall back to English, then the first available content. The notification is NOT marked as seen here; call POST /notification/dismiss when the user explicitly closes the modal. + +#### Parameters + +| Name | Located in | Description | Required | Schema | +| ---- | ---------- | ----------- | -------- | ------ | +| language | query | Notification language | No | string,
**Default:** en-US | #### Responses @@ -19670,6 +19685,12 @@ Coarse node-level status used by Inspector to pick a banner. | title | string | | Yes | | title_pic_url | string | | Yes | +#### NotificationQuery + +| Name | Type | Description | Required | +| ---- | ---- | ----------- | -------- | +| language | string,
**Default:** en-US | Notification language | No | + #### NotificationResponse | Name | Type | Description | Required | @@ -21319,7 +21340,7 @@ Model class for provider quota configuration. | Name | Type | Description | Required | | ---- | ---- | ----------- | -------- | -| language | string | Language code for recommended app localization | No | +| language | string,
**Default:** en-US | Language code for recommended app localization | No | #### RedirectResponse diff --git a/api/repositories/api_workflow_run_repository.py b/api/repositories/api_workflow_run_repository.py index b3225794db9..f08782153b0 100644 --- a/api/repositories/api_workflow_run_repository.py +++ b/api/repositories/api_workflow_run_repository.py @@ -41,7 +41,6 @@ from typing import Protocol, TypedDict from sqlalchemy.orm import Session -from core.repositories.factory import WorkflowExecutionRepository from core.workflow.nodes.human_input.pause_reason import PauseReason as DifyPauseReason from graphon.entities.pause_reason import PauseReason as GraphonPauseReason from graphon.enums import WorkflowType @@ -82,7 +81,7 @@ class WorkflowRunCleanupRef: created_at: datetime -class APIWorkflowRunRepository(WorkflowExecutionRepository, Protocol): +class APIWorkflowRunRepository(Protocol): """ Protocol for service-layer WorkflowRun repository operations. diff --git a/api/repositories/app_statistic_query_repository.py b/api/repositories/app_statistic_query_repository.py new file mode 100644 index 00000000000..d20502c4bf2 --- /dev/null +++ b/api/repositories/app_statistic_query_repository.py @@ -0,0 +1,401 @@ +"""Database read model for app monitoring statistics.""" + +from datetime import datetime +from decimal import Decimal +from typing import override + +import sqlalchemy as sa +from sqlalchemy.engine import RowMapping +from sqlalchemy.orm import Session, sessionmaker + +from core.app.entities.app_invoke_entities import InvokeFrom +from libs.helper import convert_datetime_to_date +from services.app_statistic_query import ( + AppStatisticQuery, + AverageResponseTimeStatisticRecord, + AverageSessionInteractionStatisticRecord, + DailyConversationStatisticRecord, + DailyMessageStatisticRecord, + DailyTerminalStatisticRecord, + DailyTokenCostStatisticRecord, + TokensPerSecondStatisticRecord, + UserSatisfactionRateStatisticRecord, +) + + +def _append_time_range( + sql_query: str, + parameters: dict[str, object], + *, + column: str, + start_date: datetime | None, + end_date: datetime | None, +) -> str: + if start_date is not None: + sql_query += f" AND {column} >= :start_date" + parameters["start_date"] = start_date + + if end_date is not None: + sql_query += f" AND {column} < :end_date" + parameters["end_date"] = end_date + + return sql_query + + +class AppStatisticQueryRepository(AppStatisticQuery): + def __init__(self, *, session_factory: sessionmaker[Session]) -> None: + self._session_factory = session_factory + + def _execute(self, sql_query: str, parameters: dict[str, object]) -> tuple[RowMapping, ...]: + with self._session_factory() as session: + return tuple(session.execute(sa.text(sql_query), parameters).mappings()) + + @staticmethod + def _parameters( + *, + app_id: str, + timezone: str, + ) -> dict[str, object]: + return { + "tz": timezone, + "app_id": app_id, + "excluded_invoke_from": InvokeFrom.DEBUGGER, + } + + @override + def get_daily_messages( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[DailyMessageStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + COUNT(*) AS message_count +FROM + messages +WHERE + app_id = :app_id + AND invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + DailyMessageStatisticRecord(date=str(row["date"]), message_count=row["message_count"]) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_daily_conversations( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[DailyConversationStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + COUNT(DISTINCT conversation_id) AS conversation_count +FROM + messages +WHERE + app_id = :app_id + AND invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + DailyConversationStatisticRecord( + date=str(row["date"]), + conversation_count=row["conversation_count"], + ) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_daily_terminals( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[DailyTerminalStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + COUNT(DISTINCT messages.from_end_user_id) AS terminal_count +FROM + messages +WHERE + app_id = :app_id + AND invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + DailyTerminalStatisticRecord(date=str(row["date"]), terminal_count=row["terminal_count"]) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_daily_token_costs( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[DailyTokenCostStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + (SUM(messages.message_tokens) + SUM(messages.answer_tokens)) AS token_count, + SUM(total_price) AS total_price +FROM + messages +WHERE + app_id = :app_id + AND invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + DailyTokenCostStatisticRecord( + date=str(row["date"]), + token_count=int(row["token_count"]) if row["token_count"] is not None else None, + total_price=row["total_price"], + currency="USD", + ) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_average_session_interactions( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[AverageSessionInteractionStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("c.created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + AVG(subquery.message_count) AS interactions +FROM + ( + SELECT + m.conversation_id, + COUNT(m.id) AS message_count + FROM + conversations c + JOIN + messages m + ON c.id = m.conversation_id + WHERE + c.app_id = :app_id + AND m.invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="c.created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += """ + GROUP BY m.conversation_id + ) subquery +LEFT JOIN + conversations c + ON c.id = subquery.conversation_id +GROUP BY + date +ORDER BY + date""" + + return tuple( + AverageSessionInteractionStatisticRecord( + date=str(row["date"]), + interactions=float(row["interactions"].quantize(Decimal("0.01"))), + ) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_user_satisfaction_rates( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[UserSatisfactionRateStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("m.created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + COUNT(m.id) AS message_count, + COUNT(mf.id) AS feedback_count +FROM + messages m +LEFT JOIN + message_feedbacks mf + ON mf.message_id=m.id AND mf.rating='like' +WHERE + m.app_id = :app_id + AND m.invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="m.created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + UserSatisfactionRateStatisticRecord( + date=str(row["date"]), + rate=float( + round( + row["feedback_count"] * 1000 / row["message_count"] if row["message_count"] > 0 else 0, + 2, + ) + ), + ) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_average_response_times( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[AverageResponseTimeStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + AVG(provider_response_latency) AS latency +FROM + messages +WHERE + app_id = :app_id + AND invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + AverageResponseTimeStatisticRecord( + date=str(row["date"]), + latency=round(row["latency"] * 1000, 4), + ) + for row in self._execute(sql_query, parameters) + ) + + @override + def get_tokens_per_second( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> tuple[TokensPerSecondStatisticRecord, ...]: + converted_created_at = convert_datetime_to_date("created_at") + sql_query = f"""SELECT + {converted_created_at} AS date, + CASE + WHEN SUM(provider_response_latency) = 0 THEN 0 + ELSE (SUM(answer_tokens) / SUM(provider_response_latency)) + END as tokens_per_second +FROM + messages +WHERE + app_id = :app_id + AND invoke_from != :excluded_invoke_from""" + parameters = self._parameters( + app_id=app_id, + timezone=timezone, + ) + sql_query = _append_time_range( + sql_query, + parameters, + column="created_at", + start_date=start_date, + end_date=end_date, + ) + sql_query += " GROUP BY date ORDER BY date" + + return tuple( + TokensPerSecondStatisticRecord( + date=str(row["date"]), + tps=round(row["tokens_per_second"], 4), + ) + for row in self._execute(sql_query, parameters) + ) diff --git a/api/repositories/app_tracing_config_repository.py b/api/repositories/app_tracing_config_repository.py new file mode 100644 index 00000000000..1d40ad947c3 --- /dev/null +++ b/api/repositories/app_tracing_config_repository.py @@ -0,0 +1,128 @@ +"""SQLAlchemy persistence adapter for app tracing provider configurations.""" + +from typing import Any, override + +from sqlalchemy import select +from sqlalchemy.orm import Session, sessionmaker + +from models.enums import AppStatus +from models.model import App, TraceAppConfig +from services.app_tracing_config_service import ( + AppTracingConfigAppNotFoundError, + AppTracingConfigRecord, + AppTracingConfigStore, +) + + +class SQLAlchemyAppTracingConfigRepository(AppTracingConfigStore): + def __init__(self, *, session_factory: sessionmaker[Session]) -> None: + self._session_factory = session_factory + + @override + def get( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + ) -> AppTracingConfigRecord | None: + with self._session_factory() as session: + self._require_app(session, workspace_id, app_id) + config = self._get_config(session, app_id, tracing_provider) + return self._to_record(config) if config is not None else None + + @override + def create( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> bool: + with self._session_factory.begin() as session: + self._require_app(session, workspace_id, app_id) + if self._get_config(session, app_id, tracing_provider) is not None: + return False + + session.add( + TraceAppConfig( + app_id=app_id, + tracing_provider=tracing_provider, + tracing_config=dict(tracing_config), + ) + ) + return True + + @override + def update( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> bool: + with self._session_factory.begin() as session: + self._require_app(session, workspace_id, app_id) + config = self._get_config(session, app_id, tracing_provider) + if config is None: + return False + + config.tracing_config = dict(tracing_config) + return True + + @override + def delete( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + ) -> bool: + with self._session_factory.begin() as session: + self._require_app(session, workspace_id, app_id) + config = self._get_config(session, app_id, tracing_provider) + if config is None: + return False + + session.delete(config) + return True + + @staticmethod + def _require_app(session: Session, workspace_id: str, app_id: str) -> None: + app_exists = session.scalar( + select(App.id) + .where( + App.id == app_id, + App.tenant_id == workspace_id, + App.status == AppStatus.NORMAL, + ) + .limit(1) + ) + if app_exists is None: + raise AppTracingConfigAppNotFoundError + + @staticmethod + def _get_config(session: Session, app_id: str, tracing_provider: str) -> TraceAppConfig | None: + return session.scalar( + select(TraceAppConfig) + .where( + TraceAppConfig.app_id == app_id, + TraceAppConfig.tracing_provider == tracing_provider, + ) + .limit(1) + ) + + @staticmethod + def _to_record(config: TraceAppConfig) -> AppTracingConfigRecord: + tracing_config = dict(config.tracing_config) if config.tracing_config is not None else None + return AppTracingConfigRecord( + id=config.id, + app_id=config.app_id, + tracing_provider=config.tracing_provider, + tracing_config=tracing_config, + is_active=config.is_active, + created_at=config.created_at, + updated_at=config.updated_at, + ) diff --git a/api/repositories/file_grant_repository.py b/api/repositories/file_grant_repository.py new file mode 100644 index 00000000000..98ff0fa8b16 --- /dev/null +++ b/api/repositories/file_grant_repository.py @@ -0,0 +1,213 @@ +from __future__ import annotations + +import os +from collections.abc import Sequence + +from sqlalchemy import select +from sqlalchemy.orm import Session, sessionmaker + +from models.enums import CreatorUserRole, EndUserType +from models.model import App, EndUser, UploadFile +from models.tools import ToolFile +from services.entities.file_grant_entities import ( + FileContentRecord, + FileGrantContext, + FileKind, + FileRef, + ResolvedFile, +) + + +class FileGrantRepository: + def __init__(self, *, session_factory: sessionmaker[Session]) -> None: + self._session_factory = session_factory + + def get_or_create_subject( + self, + *, + tenant_id: str, + app_id: str, + session_id: str, + external_user_id: str, + is_anonymous: bool, + ) -> str | None: + with self._session_factory() as session: + end_user = session.scalar( + self._subject_statement( + tenant_id=tenant_id, + app_id=app_id, + session_id=session_id, + require_app=True, + ).limit(1) + ) + if end_user is not None: + return end_user.id + + with self._session_factory.begin() as session: + app = session.scalar(select(App).where(App.id == app_id, App.tenant_id == tenant_id).with_for_update()) + if app is None: + return None + + end_user = session.scalar( + self._subject_statement( + tenant_id=tenant_id, + app_id=app_id, + session_id=session_id, + require_app=False, + ).limit(1) + ) + if end_user is None: + end_user = EndUser( + tenant_id=tenant_id, + app_id=app_id, + type=EndUserType.APP_DEPLOY, + is_anonymous=is_anonymous, + session_id=session_id, + external_user_id=external_user_id, + ) + session.add(end_user) + session.flush() + return end_user.id + + def subject_exists(self, context: FileGrantContext) -> bool: + with self._session_factory() as session: + return ( + session.scalar( + select(EndUser.id) + .where( + EndUser.id == context.end_user_id, + EndUser.tenant_id == context.tenant_id, + EndUser.app_id == context.app_id, + EndUser.type == EndUserType.APP_DEPLOY, + ) + .limit(1) + ) + is not None + ) + + def get_end_user(self, context: FileGrantContext) -> EndUser | None: + with self._session_factory(expire_on_commit=False) as session: + return session.scalar( + select(EndUser) + .where( + EndUser.id == context.end_user_id, + EndUser.tenant_id == context.tenant_id, + EndUser.app_id == context.app_id, + EndUser.type == EndUserType.APP_DEPLOY, + ) + .limit(1) + ) + + def resolve_owned_files( + self, + *, + context: FileGrantContext, + refs: Sequence[FileRef], + ) -> list[ResolvedFile | None]: + upload_ids = {ref.id for ref in refs if ref.kind == FileKind.UPLOAD} + tool_ids = {ref.id for ref in refs if ref.kind == FileKind.TOOL} + + with self._session_factory() as session: + uploads = self._load_uploads(session, context=context, file_ids=upload_ids) + tool_files = self._load_tool_files(session, context=context, file_ids=tool_ids) + + return [uploads.get(ref.id) if ref.kind == FileKind.UPLOAD else tool_files.get(ref.id) for ref in refs] + + def get_content_record(self, *, file_id: str, kind: FileKind) -> FileContentRecord | None: + with self._session_factory() as session: + match kind: + case FileKind.UPLOAD: + upload_file = session.scalar(select(UploadFile).where(UploadFile.id == file_id).limit(1)) + if upload_file is None: + return None + return FileContentRecord( + name=upload_file.name, + size=upload_file.size, + mime_type=upload_file.mime_type, + storage_key=upload_file.key, + ) + case FileKind.TOOL: + tool_file = session.scalar(select(ToolFile).where(ToolFile.id == file_id).limit(1)) + if tool_file is None: + return None + return FileContentRecord( + name=tool_file.name or "", + size=tool_file.size, + mime_type=tool_file.mimetype, + storage_key=tool_file.file_key, + ) + + @staticmethod + def _subject_statement(*, tenant_id: str, app_id: str, session_id: str, require_app: bool): + statement = select(EndUser) + if require_app: + statement = statement.join(App, App.id == EndUser.app_id) + predicates = [ + EndUser.tenant_id == tenant_id, + EndUser.app_id == app_id, + EndUser.session_id == session_id, + EndUser.type == EndUserType.APP_DEPLOY, + ] + if require_app: + predicates.append(App.tenant_id == tenant_id) + return statement.where(*predicates) + + @staticmethod + def _load_uploads( + session: Session, + *, + context: FileGrantContext, + file_ids: set[str], + ) -> dict[str, ResolvedFile]: + if not file_ids: + return {} + rows = session.scalars( + select(UploadFile).where( + UploadFile.id.in_(file_ids), + UploadFile.tenant_id == context.tenant_id, + UploadFile.created_by_role == CreatorUserRole.END_USER, + UploadFile.created_by == context.end_user_id, + ) + ).all() + return { + row.id: ResolvedFile( + id=row.id, + kind=FileKind.UPLOAD, + name=row.name, + size=row.size, + extension=row.extension, + mime_type=row.mime_type, + ) + for row in rows + } + + @staticmethod + def _load_tool_files( + session: Session, + *, + context: FileGrantContext, + file_ids: set[str], + ) -> dict[str, ResolvedFile]: + if not file_ids: + return {} + rows = session.scalars( + select(ToolFile).where( + ToolFile.id.in_(file_ids), + ToolFile.tenant_id == context.tenant_id, + ToolFile.user_id == context.end_user_id, + ) + ).all() + return { + row.id: ResolvedFile( + id=row.id, + kind=FileKind.TOOL, + name=row.name or "", + size=row.size, + extension=os.path.splitext(row.name or "")[1].lstrip(".").lower(), + mime_type=row.mimetype, + ) + for row in rows + } + + +__all__ = ["FileGrantRepository"] diff --git a/api/repositories/oauth_access_token_repository.py b/api/repositories/oauth_access_token_repository.py new file mode 100644 index 00000000000..87ba4704093 --- /dev/null +++ b/api/repositories/oauth_access_token_repository.py @@ -0,0 +1,89 @@ +"""SQLAlchemy persistence for account-scoped OAuth access sessions.""" + +from __future__ import annotations + +from datetime import datetime +from typing import override + +from sqlalchemy import func, select, update +from sqlalchemy.orm import Session, sessionmaker + +from models.oauth import OAuthAccessToken +from services.account_ports import AccountSessionRepository +from services.entities.account_access_entities import ( + AccountSessionRevocation, + AccountSessionSnapshot, +) + + +class SQLAlchemyOAuthAccessTokenRepository(AccountSessionRepository): + def __init__(self, *, session_factory: sessionmaker[Session]) -> None: + self._session_factory = session_factory + + @override + def list_active( + self, + *, + account_id: str, + active_at: datetime, + offset: int, + limit: int, + ) -> tuple[int, tuple[AccountSessionSnapshot, ...]]: + predicates = ( + OAuthAccessToken.account_id == account_id, + OAuthAccessToken.revoked_at.is_(None), + OAuthAccessToken.token_hash.is_not(None), + OAuthAccessToken.expires_at > active_at, + ) + with self._session_factory() as session: + total = session.scalar(select(func.count()).select_from(OAuthAccessToken).where(*predicates)) or 0 + rows = session.scalars( + select(OAuthAccessToken) + .where(*predicates) + .order_by(OAuthAccessToken.created_at.desc(), OAuthAccessToken.id.desc()) + .offset(offset) + .limit(limit) + ).all() + return int(total), tuple(self._to_snapshot(row) for row in rows) + + @override + def revoke( + self, + *, + account_id: str, + token_id: str, + revoked_at: datetime, + ) -> AccountSessionRevocation: + with self._session_factory.begin() as session: + row = session.execute( + select(OAuthAccessToken.account_id, OAuthAccessToken.token_hash) + .where(OAuthAccessToken.id == token_id) + .with_for_update() + ).one_or_none() + if row is None or row.account_id != account_id: + return AccountSessionRevocation(owned=False) + + token_hash = row.token_hash + if token_hash is not None: + session.execute( + update(OAuthAccessToken) + .where( + OAuthAccessToken.id == token_id, + OAuthAccessToken.account_id == account_id, + OAuthAccessToken.revoked_at.is_(None), + ) + .values(revoked_at=revoked_at, token_hash=None) + ) + return AccountSessionRevocation(owned=True, token_hash=token_hash) + + @staticmethod + def _to_snapshot(row: OAuthAccessToken) -> AccountSessionSnapshot: + return AccountSessionSnapshot( + id=str(row.id), + prefix=row.prefix, + client_id=row.client_id, + device_label=row.device_label, + created_at=row.created_at, + last_used_at=row.last_used_at, + expires_at=row.expires_at, + ) diff --git a/api/repositories/sqlalchemy_api_workflow_run_repository.py b/api/repositories/sqlalchemy_api_workflow_run_repository.py index bc0a494384d..aa858282df1 100644 --- a/api/repositories/sqlalchemy_api_workflow_run_repository.py +++ b/api/repositories/sqlalchemy_api_workflow_run_repository.py @@ -22,10 +22,10 @@ Implementation Notes: import json import logging import uuid -from collections.abc import Callable, Sequence +from collections.abc import Callable, Mapping, Sequence from datetime import datetime from decimal import Decimal -from typing import Any, cast, override +from typing import Any, NamedTuple, cast, override import sqlalchemy as sa from pydantic import ValidationError @@ -33,6 +33,7 @@ from sqlalchemy import and_, delete, func, null, or_, select, tuple_ from sqlalchemy.engine import CursorResult from sqlalchemy.orm import Session, selectinload, sessionmaker +from core.workflow.human_input_forms import load_form_tokens_by_form_id from core.workflow.nodes.human_input.entities import FormDefinition from core.workflow.nodes.human_input.pause_reason import ( HumanInputRequired, @@ -55,6 +56,7 @@ from libs.datetime_utils import naive_utc_now from libs.helper import convert_datetime_to_date from libs.infinite_scroll_pagination import InfiniteScrollPagination from libs.time_parser import get_time_threshold +from models import Message from models.enums import WorkflowRunTriggeredFrom from models.human_input import HumanInputForm, HumanInputFormRecipient from models.workflow import WorkflowAppLog, WorkflowArchiveLog, WorkflowPause, WorkflowPauseReason, WorkflowRun @@ -76,6 +78,18 @@ logger = logging.getLogger(__name__) _HITL_REASON_TYPES = frozenset({PauseReasonType.LEGACY_HUMAN_INPUT_REQUIRED, PauseReasonType.HITL_REQUIRED}) +class WorkflowRunMessageRef(NamedTuple): + message_id: str + conversation_id: str + + +class WorkflowRunPauseRecord(NamedTuple): + status: WorkflowExecutionStatus + paused_at: datetime | None + reasons: tuple[DifyPauseReason, ...] + form_tokens: Mapping[str, str] + + class _WorkflowRunError(Exception): pass @@ -184,6 +198,31 @@ class DifyAPISQLAlchemyWorkflowRunRepository(APIWorkflowRunRepository): """ self._session_maker = session_maker + def get_message_refs( + self, + *, + app_id: str, + workflow_run_ids: Sequence[str], + ) -> dict[str, WorkflowRunMessageRef]: + if not workflow_run_ids: + return {} + + stmt = select(Message.workflow_run_id, Message.id, Message.conversation_id).where( + Message.app_id == app_id, + Message.workflow_run_id.in_(workflow_run_ids), + ) + with self._session_maker() as session: + rows = session.execute(stmt).all() + + messages_by_run_id: dict[str, WorkflowRunMessageRef] = {} + for workflow_run_id, message_id, conversation_id in rows: + if workflow_run_id is not None: + messages_by_run_id.setdefault( + workflow_run_id, + WorkflowRunMessageRef(message_id=message_id, conversation_id=conversation_id), + ) + return messages_by_run_id + @override def get_paginated_workflow_runs( self, @@ -1152,6 +1191,48 @@ class DifyAPISQLAlchemyWorkflowRunRepository(APIWorkflowRunRepository): pause_reasons=pause_reasons, ) + def get_pause_record( + self, + *, + workspace_id: str, + workflow_run_id: str, + ) -> WorkflowRunPauseRecord | None: + stmt = ( + select(WorkflowRun) + .options(selectinload(WorkflowRun.pause)) + .where( + WorkflowRun.tenant_id == workspace_id, + WorkflowRun.id == workflow_run_id, + ) + ) + with self._session_maker() as session: + workflow_run = session.scalar(stmt) + if workflow_run is None: + return None + if workflow_run.status != WorkflowExecutionStatus.PAUSED: + return WorkflowRunPauseRecord( + status=workflow_run.status, + paused_at=None, + reasons=(), + form_tokens={}, + ) + + pause_model = workflow_run.pause + if pause_model is None: + reasons: tuple[DifyPauseReason, ...] = () + else: + reason_models = self._get_reasons_by_pause_id(session, pause_model.id) + reasons = tuple(self._hydrate_pause_reasons(session, reason_models)) + form_ids = [reason.form_id for reason in reasons if isinstance(reason, HumanInputRequired)] + form_tokens = load_form_tokens_by_form_id(form_ids, session=session) + + return WorkflowRunPauseRecord( + status=workflow_run.status, + paused_at=pause_model.created_at if pause_model is not None else None, + reasons=reasons, + form_tokens=form_tokens, + ) + @override def resume_workflow_pause( self, diff --git a/api/repositories/workflow_app_log_query_repository.py b/api/repositories/workflow_app_log_query_repository.py new file mode 100644 index 00000000000..440337ede47 --- /dev/null +++ b/api/repositories/workflow_app_log_query_repository.py @@ -0,0 +1,261 @@ +import uuid +from datetime import datetime +from enum import Enum +from typing import override + +from sqlalchemy import and_, func, literal, or_, select +from sqlalchemy.engine import RowMapping +from sqlalchemy.orm import Session, aliased, sessionmaker + +from graphon.enums import WorkflowExecutionStatus +from libs.helper import escape_like_pattern +from models import Account, EndUser, TenantAccountJoin, WorkflowAppLog, WorkflowRun +from models.enums import CreatorUserRole +from models.trigger import WorkflowTriggerLog +from services.workflow_app_log_query_service import ( + WorkflowAppLogAccount, + WorkflowAppLogEndUser, + WorkflowAppLogItem, + WorkflowAppLogPage, + WorkflowAppLogQuery, + WorkflowAppLogRunSummary, +) + + +class WorkflowAppLogQueryRepository(WorkflowAppLogQuery): + def __init__(self, *, session_factory: sessionmaker[Session]) -> None: + self._session_factory = session_factory + + @override + def get_paginated( + self, + *, + tenant_id: str, + app_id: str, + keyword: str | None = None, + status: WorkflowExecutionStatus | None = None, + created_at_before: datetime | None = None, + created_at_after: datetime | None = None, + page: int = 1, + limit: int = 20, + detail: bool = False, + created_by_end_user_session_id: str | None = None, + created_by_account: str | None = None, + ) -> WorkflowAppLogPage: + with self._session_factory() as session: + workflow_run = aliased(WorkflowRun) + stmt = ( + select( + WorkflowAppLog.id.label("log_id"), + WorkflowAppLog.created_from.label("log_created_from"), + WorkflowAppLog.created_by_role.label("log_created_by_role"), + WorkflowAppLog.created_at.label("log_created_at"), + workflow_run.id.label("run_id"), + workflow_run.version.label("run_version"), + workflow_run.status.label("run_status"), + workflow_run.triggered_from.label("run_triggered_from"), + workflow_run.error.label("run_error"), + workflow_run.elapsed_time.label("run_elapsed_time"), + workflow_run.total_tokens.label("run_total_tokens"), + workflow_run.total_steps.label("run_total_steps"), + workflow_run.created_at.label("run_created_at"), + workflow_run.finished_at.label("run_finished_at"), + workflow_run.exceptions_count.label("run_exceptions_count"), + ) + .select_from(WorkflowAppLog) + .outerjoin( + workflow_run, + and_( + workflow_run.id == WorkflowAppLog.workflow_run_id, + workflow_run.tenant_id == WorkflowAppLog.tenant_id, + workflow_run.app_id == WorkflowAppLog.app_id, + ), + ) + .where( + WorkflowAppLog.tenant_id == tenant_id, + WorkflowAppLog.app_id == app_id, + ) + ) + + if detail: + workflow_trigger_log = aliased(WorkflowTriggerLog) + stmt = stmt.outerjoin( + workflow_trigger_log, + and_( + workflow_trigger_log.tenant_id == tenant_id, + workflow_trigger_log.app_id == app_id, + workflow_trigger_log.workflow_run_id == WorkflowAppLog.workflow_run_id, + ), + ).add_columns(workflow_trigger_log.trigger_metadata.label("trigger_metadata")) + else: + stmt = stmt.add_columns(literal(None).label("trigger_metadata")) + + if keyword: + escaped_keyword = escape_like_pattern(keyword[:30]) + keyword_like_value = f"%{escaped_keyword}%" + run_creator = aliased(EndUser) + keyword_conditions = ( + workflow_run.inputs.ilike(keyword_like_value, escape="\\"), + workflow_run.outputs.ilike(keyword_like_value, escape="\\"), + select(1) + .select_from(run_creator) + .where( + workflow_run.created_by_role == CreatorUserRole.END_USER, + run_creator.id == workflow_run.created_by, + run_creator.session_id.ilike(keyword_like_value, escape="\\"), + ) + .exists(), + ) + keyword_uuid = self._safe_parse_uuid(keyword) + if keyword_uuid is not None: + stmt = stmt.where(or_(*keyword_conditions, workflow_run.id == keyword_uuid)) + else: + stmt = stmt.where(or_(*keyword_conditions)) + + if status is not None: + stmt = stmt.where(workflow_run.status == status) + if created_at_before is not None: + stmt = stmt.where(WorkflowAppLog.created_at <= created_at_before) + if created_at_after is not None: + stmt = stmt.where(WorkflowAppLog.created_at >= created_at_after) + + if created_by_end_user_session_id: + log_creator = aliased(EndUser) + stmt = stmt.where( + WorkflowAppLog.created_by_role == CreatorUserRole.END_USER, + select(1) + .select_from(log_creator) + .where( + log_creator.id == WorkflowAppLog.created_by, + log_creator.session_id == created_by_end_user_session_id, + ) + .exists(), + ) + if created_by_account: + account_id = session.scalar( + select(Account.id) + .join(TenantAccountJoin, TenantAccountJoin.account_id == Account.id) + .where( + Account.email == created_by_account, + TenantAccountJoin.tenant_id == tenant_id, + ) + ) + if account_id is None: + raise ValueError(f"Account not found: {created_by_account}") + + stmt = stmt.where( + WorkflowAppLog.created_by_role == CreatorUserRole.ACCOUNT, + WorkflowAppLog.created_by == account_id, + ) + + total = session.scalar(select(func.count()).select_from(stmt.subquery())) or 0 + + actor_account = aliased(Account) + actor_end_user = aliased(EndUser) + paginated_stmt = ( + stmt.outerjoin( + actor_account, + and_( + WorkflowAppLog.created_by_role == CreatorUserRole.ACCOUNT, + actor_account.id == WorkflowAppLog.created_by, + ), + ) + .outerjoin( + actor_end_user, + and_( + WorkflowAppLog.created_by_role == CreatorUserRole.END_USER, + actor_end_user.id == WorkflowAppLog.created_by, + ), + ) + .add_columns( + actor_account.id.label("account_id"), + actor_account.name.label("account_name"), + actor_account.email.label("account_email"), + actor_end_user.id.label("end_user_id"), + actor_end_user.type.label("end_user_type"), + actor_end_user.session_id.label("end_user_session_id"), + ) + .order_by(WorkflowAppLog.created_at.desc()) + .offset((page - 1) * limit) + .limit(limit) + ) + rows = session.execute(paginated_stmt).mappings().all() + + return WorkflowAppLogPage( + page=page, + limit=limit, + total=total, + has_more=total > page * limit, + data=tuple(self._to_item(row=row, detail=detail) for row in rows), + ) + + @staticmethod + def _to_item(*, row: RowMapping, detail: bool) -> WorkflowAppLogItem: + run_id = row["run_id"] + workflow_run = ( + WorkflowAppLogRunSummary( + id=run_id, + version=row["run_version"], + status=WorkflowAppLogQueryRepository._enum_value(row["run_status"]), + triggered_from=WorkflowAppLogQueryRepository._enum_value(row["run_triggered_from"]), + error=row["run_error"], + elapsed_time=row["run_elapsed_time"], + total_tokens=row["run_total_tokens"], + total_steps=row["run_total_steps"], + created_at=row["run_created_at"], + finished_at=row["run_finished_at"], + exceptions_count=row["run_exceptions_count"], + ) + if run_id is not None + else None + ) + + account_id = row["account_id"] + account = ( + WorkflowAppLogAccount( + id=account_id, + name=row["account_name"], + email=row["account_email"], + ) + if account_id is not None + else None + ) + end_user_id = row["end_user_id"] + end_user = ( + WorkflowAppLogEndUser( + id=end_user_id, + type=WorkflowAppLogQueryRepository._enum_value(row["end_user_type"]), + is_anonymous=False, + session_id=row["end_user_session_id"], + ) + if end_user_id is not None + else None + ) + + return WorkflowAppLogItem( + id=row["log_id"], + workflow_run=workflow_run, + details={"trigger_metadata": row["trigger_metadata"]} if detail else None, + created_from=WorkflowAppLogQueryRepository._enum_value(row["log_created_from"]), + created_by_role=WorkflowAppLogQueryRepository._enum_value(row["log_created_by_role"]), + created_by_account=account, + created_by_end_user=end_user, + created_at=row["log_created_at"], + ) + + @staticmethod + def _enum_value(value: Enum | str | None) -> str: + if value is None: + raise ValueError("Required enum value is missing") + if isinstance(value, str): + return value + return str(value.value) + + @staticmethod + def _safe_parse_uuid(value: str) -> uuid.UUID | None: + if len(value) < 32: + return None + try: + return uuid.UUID(value) + except ValueError: + return None diff --git a/api/repositories/workspace_query_repository.py b/api/repositories/workspace_query_repository.py index 84758d997be..8e83d158c47 100644 --- a/api/repositories/workspace_query_repository.py +++ b/api/repositories/workspace_query_repository.py @@ -7,11 +7,17 @@ from sqlalchemy.orm import Session, sessionmaker from models.account import Tenant, TenantAccountJoin, TenantStatus from services.account_login_service import ConsoleAuthWorkspaceQuery -from services.account_ports import AccountWorkspaceMembershipQuery +from services.account_ports import AccountWorkspaceMembershipQuery, AccountWorkspaceSnapshotQuery +from services.entities.account_access_entities import AccountWorkspaceSnapshot from services.workspace_query_service import WorkspaceQuery, WorkspaceRecord -class WorkspaceQueryRepository(WorkspaceQuery, AccountWorkspaceMembershipQuery, ConsoleAuthWorkspaceQuery): +class WorkspaceQueryRepository( + WorkspaceQuery, + AccountWorkspaceMembershipQuery, + AccountWorkspaceSnapshotQuery, + ConsoleAuthWorkspaceQuery, +): def __init__(self, session_factory: sessionmaker[Session]) -> None: self._session_factory = session_factory @@ -52,6 +58,35 @@ class WorkspaceQueryRepository(WorkspaceQuery, AccountWorkspaceMembershipQuery, with self._session_factory() as session: return tuple(session.scalars(stmt).all()) + @override + def list_account_access_workspaces(self, account_id: str) -> tuple[AccountWorkspaceSnapshot, ...]: + """List every membership for the OpenAPI account identity response. + + Unlike the Console workspace picker, the identity response preserves + its existing behavior of including archived memberships. + """ + stmt = ( + select( + Tenant.id, + Tenant.name, + TenantAccountJoin.role, + TenantAccountJoin.current, + ) + .join(TenantAccountJoin, TenantAccountJoin.tenant_id == Tenant.id) + .where(TenantAccountJoin.account_id == account_id) + .order_by(Tenant.created_at.asc(), Tenant.id.asc()) + ) + with self._session_factory() as session: + return tuple( + AccountWorkspaceSnapshot( + id=workspace_id, + name=name, + role=role.value, + current=current, + ) + for workspace_id, name, role, current in session.execute(stmt).all() + ) + @override def has_active_for_account(self, account_id: str) -> bool: stmt = ( diff --git a/api/services/account_access_service.py b/api/services/account_access_service.py new file mode 100644 index 00000000000..9fb3a768cb3 --- /dev/null +++ b/api/services/account_access_service.py @@ -0,0 +1,77 @@ +"""Application service for account identity and access-session use cases.""" + +from __future__ import annotations + +from collections.abc import Callable +from datetime import datetime + +from machinery.context import AccountRequestContext +from services.account_errors import AccountNotFoundError, AccountSessionNotFoundError +from services.account_ports import ( + AccountSessionRepository, + AccountSnapshotQuery, + AccountTokenCacheInvalidator, + AccountWorkspaceSnapshotQuery, +) +from services.entities.account_access_entities import AccountAccessSnapshot, AccountSessionPage + + +class AccountAccessService: + def __init__( + self, + *, + accounts: AccountSnapshotQuery, + workspaces: AccountWorkspaceSnapshotQuery, + sessions: AccountSessionRepository, + invalidate_token_cache: AccountTokenCacheInvalidator, + now: Callable[[], datetime], + ) -> None: + self._accounts = accounts + self._workspaces = workspaces + self._sessions = sessions + self._invalidate_token_cache = invalidate_token_cache + self._now = now + + def get(self, context: AccountRequestContext) -> AccountAccessSnapshot: + account = self._accounts.get(context.account_id) + if account is None: + raise AccountNotFoundError + + workspaces = tuple(self._workspaces.list_account_access_workspaces(context.account_id)) + default_workspace_id = next((workspace.id for workspace in workspaces if workspace.current), None) + if default_workspace_id is None and workspaces: + default_workspace_id = workspaces[0].id + + return AccountAccessSnapshot( + account=account, + workspaces=workspaces, + default_workspace_id=default_workspace_id, + ) + + def list_sessions(self, context: AccountRequestContext, *, page: int, limit: int) -> AccountSessionPage: + total, sessions = self._sessions.list_active( + account_id=context.account_id, + active_at=self._now(), + offset=(page - 1) * limit, + limit=limit, + ) + return AccountSessionPage(page=page, limit=limit, total=total, items=tuple(sessions)) + + def revoke_current_session(self, context: AccountRequestContext) -> None: + if context.access_token_id is None: + raise RuntimeError("OpenAPI account admission did not resolve an access token") + self._revoke(context, token_id=context.access_token_id, require_owned=False) + + def revoke_session(self, context: AccountRequestContext, *, token_id: str) -> None: + self._revoke(context, token_id=token_id, require_owned=True) + + def _revoke(self, context: AccountRequestContext, *, token_id: str, require_owned: bool) -> None: + revocation = self._sessions.revoke( + account_id=context.account_id, + token_id=token_id, + revoked_at=self._now(), + ) + if require_owned and not revocation.owned: + raise AccountSessionNotFoundError + if revocation.token_hash is not None: + self._invalidate_token_cache(revocation.token_hash) diff --git a/api/services/account_errors.py b/api/services/account_errors.py index 773ae90ea46..9ff9d5ec73a 100644 --- a/api/services/account_errors.py +++ b/api/services/account_errors.py @@ -9,6 +9,10 @@ class AccountNotFoundError(AccountApplicationError): """The admitted account no longer exists.""" +class AccountSessionNotFoundError(AccountApplicationError): + """The requested access session is not owned by the admitted account.""" + + class CurrentAccountPasswordIncorrectError(AccountApplicationError): """The supplied current password does not match the account credential.""" diff --git a/api/services/account_ports.py b/api/services/account_ports.py index 8ea4f06e074..bc0b5d1eeb9 100644 --- a/api/services/account_ports.py +++ b/api/services/account_ports.py @@ -1,9 +1,14 @@ -"""Persistence ports used by account application services.""" +"""Ports used by account application services.""" from collections.abc import Sequence from datetime import datetime from typing import Protocol +from services.entities.account_access_entities import ( + AccountSessionRevocation, + AccountSessionSnapshot, + AccountWorkspaceSnapshot, +) from services.entities.account_entities import ( AccountCredentials, AccountDeletionChallenge, @@ -17,6 +22,10 @@ from services.entities.account_entities import ( ) +class AccountSnapshotQuery(Protocol): + def get(self, account_id: str) -> AccountSnapshot | None: ... + + class AccountRepository(Protocol): def get(self, account_id: str) -> AccountSnapshot | None: ... @@ -58,6 +67,33 @@ class AccountWorkspaceMembershipQuery(Protocol): def has_active_membership(self, account_id: str) -> bool: ... +class AccountWorkspaceSnapshotQuery(Protocol): + def list_account_access_workspaces(self, account_id: str) -> Sequence[AccountWorkspaceSnapshot]: ... + + +class AccountSessionRepository(Protocol): + def list_active( + self, + *, + account_id: str, + active_at: datetime, + offset: int, + limit: int, + ) -> tuple[int, Sequence[AccountSessionSnapshot]]: ... + + def revoke( + self, + *, + account_id: str, + token_id: str, + revoked_at: datetime, + ) -> AccountSessionRevocation: ... + + +class AccountTokenCacheInvalidator(Protocol): + def __call__(self, token_hash: str) -> None: ... + + class AccountAvatarFileGateway(Protocol): def get_owned_signed_url(self, *, account_id: str, upload_file_id: str) -> str | None: ... diff --git a/api/services/account_service.py b/api/services/account_service.py index dae4ea2b4a6..e33af75e385 100644 --- a/api/services/account_service.py +++ b/api/services/account_service.py @@ -321,9 +321,9 @@ class AccountService: @staticmethod def get_account_by_id(account_id: str, *, session: Session) -> Account | None: """Plain ``Account`` getter — no banned check, no tenant rotation, - no ``last_active_at`` write. Use this from read-only identity - endpoints (``/openapi/v1/account``) where ``load_user``'s - side-effects (current-tenant assignment, commit) are unwanted. + no ``last_active_at`` write. Use this from authentication and read + paths where ``load_user``'s current-tenant assignment and commit are + unwanted. ``session`` is injected by the caller so this service stays free of a Flask-scoped session import. @@ -1212,36 +1212,12 @@ class TenantService: ).all() ) - @staticmethod - def get_account_memberships(account_id: str, *, session: Session) -> list[Row[tuple[TenantAccountJoin, Tenant]]]: - """Return ``(TenantAccountJoin, Tenant)`` rows for every workspace - the account belongs to. Unlike :meth:`get_join_tenants` this keeps - the join row so callers can read ``role``/``current`` alongside the - tenant — used by ``/openapi/v1/account`` to render workspace - membership + pick the default workspace. - - ``session`` is injected by the caller so this service stays free - of a Flask-scoped session import. - - No tenant-status filter: parity with the legacy controller query - (the openapi identity endpoint listed all joined tenants). - """ - return ( - session.query(TenantAccountJoin, Tenant) - .join(Tenant, Tenant.id == TenantAccountJoin.tenant_id) - .filter(TenantAccountJoin.account_id == account_id) - .all() - ) - @staticmethod def get_workspaces_for_account(account_id: str, *, session: Session) -> list[Row[tuple[Tenant, TenantAccountJoin]]]: """``(Tenant, TenantAccountJoin)`` rows for every workspace the account belongs to, ordered by ``Tenant.created_at`` ASC — the - canonical ordering for ``/openapi/v1/workspaces``. - - Distinct from :meth:`get_account_memberships`: tuple order is - flipped (tenant first) and rows are sorted, so the workspace - listing is stable across requests. + canonical ordering for ``/openapi/v1/workspaces``. Rows keep the + tenant first so callers can serialize the workspace directly. """ return list( session.execute( diff --git a/api/services/app_statistic_query.py b/api/services/app_statistic_query.py new file mode 100644 index 00000000000..3b1913b6bca --- /dev/null +++ b/api/services/app_statistic_query.py @@ -0,0 +1,122 @@ +"""Application boundary for app monitoring statistics.""" + +from collections.abc import Sequence +from datetime import datetime +from decimal import Decimal +from typing import NamedTuple, Protocol + + +class DailyMessageStatisticRecord(NamedTuple): + date: str + message_count: int + + +class DailyConversationStatisticRecord(NamedTuple): + date: str + conversation_count: int + + +class DailyTerminalStatisticRecord(NamedTuple): + date: str + terminal_count: int + + +class DailyTokenCostStatisticRecord(NamedTuple): + date: str + token_count: int | None + total_price: Decimal | None + currency: str | None + + +class AverageSessionInteractionStatisticRecord(NamedTuple): + date: str + interactions: float + + +class UserSatisfactionRateStatisticRecord(NamedTuple): + date: str + rate: float + + +class AverageResponseTimeStatisticRecord(NamedTuple): + date: str + latency: float + + +class TokensPerSecondStatisticRecord(NamedTuple): + date: str + tps: float + + +class AppStatisticQuery(Protocol): + def get_daily_messages( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[DailyMessageStatisticRecord]: ... + + def get_daily_conversations( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[DailyConversationStatisticRecord]: ... + + def get_daily_terminals( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[DailyTerminalStatisticRecord]: ... + + def get_daily_token_costs( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[DailyTokenCostStatisticRecord]: ... + + def get_average_session_interactions( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[AverageSessionInteractionStatisticRecord]: ... + + def get_user_satisfaction_rates( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[UserSatisfactionRateStatisticRecord]: ... + + def get_average_response_times( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[AverageResponseTimeStatisticRecord]: ... + + def get_tokens_per_second( + self, + *, + app_id: str, + start_date: datetime | None, + end_date: datetime | None, + timezone: str, + ) -> Sequence[TokensPerSecondStatisticRecord]: ... diff --git a/api/services/app_tracing_config_gateway.py b/api/services/app_tracing_config_gateway.py new file mode 100644 index 00000000000..637260df48d --- /dev/null +++ b/api/services/app_tracing_config_gateway.py @@ -0,0 +1,184 @@ +"""Compatibility gateway for the legacy tracing provider implementations.""" + +import logging +from typing import Any, override + +from pydantic import ValidationError + +from core.ops.entities.config_entity import BaseTracingConfig +from core.ops.ops_trace_manager import OpsTraceManager, TracingProviderConfigEntry, provider_config_map +from services.app_tracing_config_service import ( + AppTracingConfigInvalidConfigurationError, + AppTracingConfigInvalidProviderError, + AppTracingConfigProcessingError, + AppTracingConfigVerificationFailedError, + TracingConfigProviderGateway, +) + +logger = logging.getLogger(__name__) + +_PROJECT_URL_FALLBACKS = { + "arize": "https://app.arize.com/", + "phoenix": "https://app.phoenix.arize.com/projects/", + "langsmith": "https://smith.langchain.com/", + "opik": "https://www.comet.com/opik/", + "weave": "https://wandb.ai/", + "aliyun": "https://arms.console.aliyun.com/", + "tencent": "https://console.cloud.tencent.com/apm", + "mlflow": "http://localhost:5000/", + "databricks": "https://www.databricks.com/", +} + + +class OpsTraceManagerGateway(TracingConfigProviderGateway): + @override + def validate_provider(self, tracing_provider: str) -> None: + self._provider_config(tracing_provider) + + @override + def prepare_new_config( + self, + *, + workspace_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> dict[str, Any]: + provider_config = self._provider_config(tracing_provider) + normalized_config = self._normalize_config(provider_config, tracing_config) + self._verify_config(normalized_config, tracing_provider) + + project_url = self._get_project_url_for_create(normalized_config, tracing_provider) + try: + encrypted_config = OpsTraceManager.encrypt_tracing_config( + workspace_id, + tracing_provider, + normalized_config, + ) + except Exception as error: + raise AppTracingConfigProcessingError from error + if project_url: + encrypted_config["project_url"] = project_url + return encrypted_config + + @override + def prepare_updated_config( + self, + *, + workspace_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + current_tracing_config: dict[str, Any] | None, + ) -> dict[str, Any]: + provider_config = self._provider_config(tracing_provider) + self._validate_config(provider_config, tracing_config) + try: + encrypted_config = OpsTraceManager.encrypt_tracing_config( + workspace_id, + tracing_provider, + dict(tracing_config), + current_tracing_config, + ) + decrypted_config = OpsTraceManager.decrypt_tracing_config( + workspace_id, + tracing_provider, + encrypted_config, + ) + except Exception as error: + raise AppTracingConfigProcessingError from error + + self._verify_config(decrypted_config, tracing_provider) + return encrypted_config + + @override + def present_config( + self, + *, + workspace_id: str, + tracing_provider: str, + tracing_config: dict[str, Any] | None, + ) -> dict[str, Any]: + if tracing_config is None: + raise AppTracingConfigProcessingError + + try: + decrypted_config = OpsTraceManager.decrypt_tracing_config( + workspace_id, + tracing_provider, + tracing_config, + ) + presented_config = OpsTraceManager.obfuscated_decrypt_token(tracing_provider, decrypted_config) + except Exception as error: + raise AppTracingConfigProcessingError from error + + if tracing_provider == "langfuse" and not decrypted_config.get("project_key"): + try: + project_key = OpsTraceManager.get_trace_config_project_key(decrypted_config, tracing_provider) + presented_config["project_url"] = f"{decrypted_config.get('host')}/project/{project_key}" + except Exception: + presented_config["project_url"] = f"{decrypted_config.get('host')}/" + elif tracing_provider in _PROJECT_URL_FALLBACKS and not decrypted_config.get("project_url"): + try: + presented_config["project_url"] = OpsTraceManager.get_trace_config_project_url( + decrypted_config, + tracing_provider, + ) + except Exception: + presented_config["project_url"] = _PROJECT_URL_FALLBACKS[tracing_provider] + + return presented_config + + @staticmethod + def _provider_config(tracing_provider: str) -> TracingProviderConfigEntry: + try: + return provider_config_map[tracing_provider] + except KeyError as error: + raise AppTracingConfigInvalidProviderError(tracing_provider) from error + + @staticmethod + def _normalize_config( + provider_config: TracingProviderConfigEntry, + tracing_config: dict[str, Any], + ) -> dict[str, Any]: + normalized_config = dict(tracing_config) + default_config = OpsTraceManagerGateway._validate_config(provider_config, normalized_config) + + default_values = default_config.model_dump() + for key in provider_config["other_keys"]: + if normalized_config.get(key) == "": + normalized_config[key] = default_values.get(key) + return normalized_config + + @staticmethod + def _validate_config( + provider_config: TracingProviderConfigEntry, + tracing_config: dict[str, Any], + ) -> BaseTracingConfig: + config_class: type[BaseTracingConfig] = provider_config["config_class"] + try: + return config_class.model_validate(tracing_config) + except ValidationError as error: + raise AppTracingConfigInvalidConfigurationError from error + + @staticmethod + def _verify_config(tracing_config: dict[str, Any], tracing_provider: str) -> None: + try: + is_effective = OpsTraceManager.check_trace_config_is_effective(tracing_config, tracing_provider) + except ValueError as error: + logger.warning("Tracing configuration verification failed for provider %s", tracing_provider, exc_info=True) + raise AppTracingConfigVerificationFailedError from error + if not is_effective: + raise AppTracingConfigVerificationFailedError + + @staticmethod + def _get_project_url_for_create(tracing_config: dict[str, Any], tracing_provider: str) -> str | None: + try: + if tracing_provider in ("arize", "phoenix"): + return OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider) + if tracing_provider == "langfuse": + project_key = OpsTraceManager.get_trace_config_project_key(tracing_config, tracing_provider) + return f"{tracing_config.get('host')}/project/{project_key}" + if tracing_provider in ("langsmith", "opik", "mlflow", "databricks", "tencent"): + return OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider) + except Exception: + return None + return None diff --git a/api/services/app_tracing_config_service.py b/api/services/app_tracing_config_service.py new file mode 100644 index 00000000000..10d3eac61a8 --- /dev/null +++ b/api/services/app_tracing_config_service.py @@ -0,0 +1,230 @@ +"""Application boundary for app tracing provider configurations.""" + +from dataclasses import dataclass, replace +from datetime import datetime +from typing import Any, Protocol + +from machinery.context import RequestContext + + +@dataclass(frozen=True, slots=True) +class AppTracingConfigRecord: + id: str + app_id: str + tracing_provider: str | None + tracing_config: dict[str, Any] | None + is_active: bool + created_at: datetime + updated_at: datetime + + +class AppTracingConfigStore(Protocol): + def get( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + ) -> AppTracingConfigRecord | None: ... + + def create( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> bool: ... + + def update( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> bool: ... + + def delete( + self, + *, + workspace_id: str, + app_id: str, + tracing_provider: str, + ) -> bool: ... + + +class TracingConfigProviderGateway(Protocol): + def validate_provider(self, tracing_provider: str) -> None: ... + + def prepare_new_config( + self, + *, + workspace_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> dict[str, Any]: ... + + def prepare_updated_config( + self, + *, + workspace_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + current_tracing_config: dict[str, Any] | None, + ) -> dict[str, Any]: ... + + def present_config( + self, + *, + workspace_id: str, + tracing_provider: str, + tracing_config: dict[str, Any] | None, + ) -> dict[str, Any]: ... + + +class AppTracingConfigError(Exception): + """Base class for framework-neutral app tracing configuration failures.""" + + +class AppTracingConfigAppNotFoundError(AppTracingConfigError): + def __init__(self) -> None: + super().__init__("App not found") + + +class AppTracingConfigAlreadyExistsError(AppTracingConfigError): + def __init__(self) -> None: + super().__init__("Trace config is exist.") + + +class AppTracingConfigNotFoundError(AppTracingConfigError): + def __init__(self) -> None: + super().__init__("Trace config not exist.") + + +class AppTracingConfigInvalidProviderError(AppTracingConfigError): + def __init__(self, tracing_provider: str) -> None: + super().__init__(f"Invalid tracing provider: {tracing_provider}") + + +class AppTracingConfigInvalidConfigurationError(AppTracingConfigError): + """The submitted provider configuration does not match its schema.""" + + def __init__(self) -> None: + super().__init__("Invalid tracing configuration") + + +class AppTracingConfigVerificationFailedError(AppTracingConfigError): + """The submitted configuration could not be verified by its provider.""" + + def __init__(self) -> None: + super().__init__("Tracing configuration verification failed") + + +class AppTracingConfigProcessingError(AppTracingConfigError): + """A validated or stored configuration could not be processed internally.""" + + def __init__(self) -> None: + super().__init__("Tracing configuration processing failed") + + +class AppTracingConfigService: + def __init__(self, *, configs: AppTracingConfigStore, provider: TracingConfigProviderGateway) -> None: + self._configs = configs + self._provider = provider + + def get( + self, + context: RequestContext, + app_id: str, + tracing_provider: str, + ) -> AppTracingConfigRecord | None: + workspace_id = context.active_workspace_id + self._provider.validate_provider(tracing_provider) + record = self._configs.get( + workspace_id=workspace_id, + app_id=app_id, + tracing_provider=tracing_provider, + ) + if record is None: + return None + + tracing_config = self._provider.present_config( + workspace_id=workspace_id, + tracing_provider=tracing_provider, + tracing_config=record.tracing_config, + ) + return replace(record, tracing_config=tracing_config) + + def create( + self, + context: RequestContext, + app_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> None: + workspace_id = context.active_workspace_id + current = self._configs.get( + workspace_id=workspace_id, + app_id=app_id, + tracing_provider=tracing_provider, + ) + if current is not None: + raise AppTracingConfigAlreadyExistsError + + encrypted_config = self._provider.prepare_new_config( + workspace_id=workspace_id, + tracing_provider=tracing_provider, + tracing_config=tracing_config, + ) + + created = self._configs.create( + workspace_id=workspace_id, + app_id=app_id, + tracing_provider=tracing_provider, + tracing_config=encrypted_config, + ) + if not created: + raise AppTracingConfigAlreadyExistsError + + def update( + self, + context: RequestContext, + app_id: str, + tracing_provider: str, + tracing_config: dict[str, Any], + ) -> None: + workspace_id = context.active_workspace_id + current = self._configs.get( + workspace_id=workspace_id, + app_id=app_id, + tracing_provider=tracing_provider, + ) + self._provider.validate_provider(tracing_provider) + if current is None: + raise AppTracingConfigNotFoundError + + encrypted_config = self._provider.prepare_updated_config( + workspace_id=workspace_id, + tracing_provider=tracing_provider, + tracing_config=tracing_config, + current_tracing_config=current.tracing_config, + ) + updated = self._configs.update( + workspace_id=workspace_id, + app_id=app_id, + tracing_provider=tracing_provider, + tracing_config=encrypted_config, + ) + if not updated: + raise AppTracingConfigNotFoundError + + def delete(self, context: RequestContext, app_id: str, tracing_provider: str) -> None: + self._provider.validate_provider(tracing_provider) + deleted = self._configs.delete( + workspace_id=context.active_workspace_id, + app_id=app_id, + tracing_provider=tracing_provider, + ) + if not deleted: + raise AppTracingConfigNotFoundError diff --git a/api/services/dataset_service.py b/api/services/dataset_service.py index 964a4dc8845..37296e4b1ec 100644 --- a/api/services/dataset_service.py +++ b/api/services/dataset_service.py @@ -3850,7 +3850,8 @@ class SegmentService: logger.exception("Failed to regenerate summary for segment %s", segment.id) # Don't fail the entire update if summary regeneration fails # update multimodel vector index - VectorService.update_multimodel_vector(segment, args.attachment_ids or [], dataset, session=session) + if args.attachment_ids is not None: + VectorService.update_multimodel_vector(segment, args.attachment_ids, dataset, session=session) except Exception as e: logger.exception("update segment index failed") segment.enabled = False diff --git a/api/services/end_user_service.py b/api/services/end_user_service.py index c15e9949abb..a01d3a8128f 100644 --- a/api/services/end_user_service.py +++ b/api/services/end_user_service.py @@ -63,6 +63,11 @@ class EndUserService: EndUser.tenant_id == tenant_id, EndUser.app_id == app_id, EndUser.session_id == user_id, + # An AppDeploy row is never a legacy row this could upgrade: + # the type was added after the split, and FileGrantService + # reads its rows by type. Retyping one here would hide it + # from that read and strand the files it owns. + EndUser.type != EndUserType.APP_DEPLOY, ) .order_by( # Prioritize records with matching type (0 = match, 1 = no match) diff --git a/api/services/entities/account_access_entities.py b/api/services/entities/account_access_entities.py new file mode 100644 index 00000000000..a59db492259 --- /dev/null +++ b/api/services/entities/account_access_entities.py @@ -0,0 +1,52 @@ +"""Framework-neutral data contracts for account identity and access sessions.""" + +from __future__ import annotations + +from dataclasses import dataclass +from datetime import datetime + +from services.entities.account_entities import AccountSnapshot + + +@dataclass(frozen=True, slots=True) +class AccountWorkspaceSnapshot: + id: str + name: str + role: str + current: bool + + +@dataclass(frozen=True, slots=True) +class AccountAccessSnapshot: + account: AccountSnapshot + workspaces: tuple[AccountWorkspaceSnapshot, ...] + default_workspace_id: str | None + + +@dataclass(frozen=True, slots=True) +class AccountSessionSnapshot: + id: str + prefix: str + client_id: str + device_label: str + created_at: datetime | None + last_used_at: datetime | None + expires_at: datetime | None + + +@dataclass(frozen=True, slots=True) +class AccountSessionPage: + page: int + limit: int + total: int + items: tuple[AccountSessionSnapshot, ...] + + @property + def has_more(self) -> bool: + return self.page * self.limit < self.total + + +@dataclass(frozen=True, slots=True) +class AccountSessionRevocation: + owned: bool + token_hash: str | None = None diff --git a/api/services/entities/account_entities.py b/api/services/entities/account_entities.py index 3e4293871e8..cfc637b09e1 100644 --- a/api/services/entities/account_entities.py +++ b/api/services/entities/account_entities.py @@ -1,4 +1,4 @@ -"""Framework-neutral contracts for Console account use cases.""" +"""Framework-neutral contracts shared by account use cases.""" from __future__ import annotations diff --git a/api/services/entities/file_grant_entities.py b/api/services/entities/file_grant_entities.py new file mode 100644 index 00000000000..5d7ffb44d7d --- /dev/null +++ b/api/services/entities/file_grant_entities.py @@ -0,0 +1,139 @@ +from __future__ import annotations + +from collections.abc import Iterable +from dataclasses import dataclass +from datetime import datetime +from enum import StrEnum + +from pydantic import BaseModel + + +class FileKind(StrEnum): + UPLOAD = "upload" + TOOL = "tool" + + +class FileGrantScope(StrEnum): + UPLOAD = "upload" + RESOLVE = "resolve" + PRODUCE = "produce" + + +class FileGrantClaims(BaseModel): + sub: str + tenant_id: str + app_id: str + scopes: list[FileGrantScope] + exp: int + + +class FileContentClaims(BaseModel): + kind: FileKind + file_id: str + exp: int + + +@dataclass(frozen=True, slots=True) +class FileGrantContext: + tenant_id: str + app_id: str + end_user_id: str + + +@dataclass(frozen=True, slots=True) +class FileRef: + id: str + kind: FileKind + + +@dataclass(frozen=True, slots=True) +class ResolvedFile: + id: str + kind: FileKind + name: str + size: int + extension: str + mime_type: str | None + + +@dataclass(frozen=True, slots=True) +class ResolvedFileAccess: + file: ResolvedFile + external_url: str + internal_url: str + + +@dataclass(frozen=True, slots=True) +class FileContentRecord: + name: str + size: int + mime_type: str | None + storage_key: str + + +@dataclass(frozen=True, slots=True) +class FileContent: + name: str + size: int + mime_type: str | None + stream: Iterable[bytes] + + +@dataclass(frozen=True, slots=True) +class StoredUpload: + id: str + name: str + size: int + extension: str | None + mime_type: str | None + created_by: str | None + created_at: datetime | None + tenant_id: str | None + source_url: str + + +@dataclass(frozen=True, slots=True) +class StoredProducedFile: + id: str + name: str + size: int + mime_type: str | None + + +@dataclass(frozen=True, slots=True) +class RemoteFile: + filename: str + mimetype: str + content: bytes + + +@dataclass(frozen=True, slots=True) +class FileGrantLimits: + file_size_limit: int + image_file_size_limit: int + audio_file_size_limit: int + video_file_size_limit: int + workflow_file_upload_limit: int + batch_count_limit: int + + +@dataclass(frozen=True, slots=True) +class FileGrantMintRequest: + tenant_id: str + app_id: str + subject: str + is_anonymous: bool + scopes: tuple[FileGrantScope, ...] + ttl_seconds: int + file_refs: tuple[FileRef, ...] + optional_file_refs: tuple[FileRef, ...] + run_deadline: int | None + + +@dataclass(frozen=True, slots=True) +class FileGrantMintResult: + grant: str + expires_at: int + limits: FileGrantLimits + files: tuple[ResolvedFile, ...] + optional_files: tuple[ResolvedFileAccess | None, ...] diff --git a/api/services/errors/file_grant.py b/api/services/errors/file_grant.py new file mode 100644 index 00000000000..17f08db9115 --- /dev/null +++ b/api/services/errors/file_grant.py @@ -0,0 +1,34 @@ +class AppNotFoundError(Exception): + pass + + +class EndUserNotFoundError(Exception): + pass + + +class InvalidGrantRequestError(Exception): + pass + + +class InvalidFileGrantError(Exception): + pass + + +class GrantTtlTooLongError(Exception): + pass + + +class GrantedFileNotFoundError(Exception): + pass + + +class InvalidSubjectError(Exception): + pass + + +class RemoteFileUnavailableError(Exception): + pass + + +class TooManyFileRefsError(Exception): + pass diff --git a/api/services/explore_banner_query_service.py b/api/services/explore_banner_query_service.py index 8ef4f3827f4..4eb204f7d85 100644 --- a/api/services/explore_banner_query_service.py +++ b/api/services/explore_banner_query_service.py @@ -7,6 +7,8 @@ from collections.abc import Sequence from datetime import datetime from typing import Any, NamedTuple, Protocol +from constants.languages import languages + _DEFAULT_LANGUAGE = "en-US" @@ -37,6 +39,7 @@ class ExploreBannerQueryService: if not self._enabled: return () + language = language if language in languages else _DEFAULT_LANGUAGE banners = tuple(self._banners.list_enabled(language)) if banners or language == _DEFAULT_LANGUAGE: return banners diff --git a/api/services/file_grant_gateways.py b/api/services/file_grant_gateways.py new file mode 100644 index 00000000000..3fdf2493ee8 --- /dev/null +++ b/api/services/file_grant_gateways.py @@ -0,0 +1,327 @@ +from __future__ import annotations + +import mimetypes +import os +import re +import urllib.parse +from collections.abc import Callable, Sequence +from typing import IO, cast +from uuid import uuid4 + +import httpx +import jwt +from pydantic import ValidationError + +from core.file import remote_fetcher +from core.helper import ssrf_proxy +from core.tools.tool_file_manager import ToolFileManager, resolve_extension +from extensions.ext_storage import Storage +from models.model import EndUser +from services.entities.file_grant_entities import ( + FileContent, + FileContentClaims, + FileContentRecord, + FileGrantClaims, + FileGrantContext, + FileGrantScope, + FileKind, + RemoteFile, + StoredProducedFile, + StoredUpload, +) +from services.errors.file import FileTooLargeError +from services.errors.file_grant import EndUserNotFoundError +from services.file_service import FileService + +FILE_GRANT_AUDIENCE = "dify-files" +FILE_CONTENT_AUDIENCE = "dify-files-content" +_ALGORITHM = "HS256" + + +class FileGrantTokenGateway: + def __init__( + self, + *, + secret_key: str, + external_files_url: str, + internal_files_url: str, + content_token_ttl_seconds: int, + now: Callable[[], int], + ) -> None: + self._secret_key = secret_key + self._external_files_url = external_files_url + self._internal_files_url = internal_files_url + self._content_token_ttl_seconds = content_token_ttl_seconds + self._now = now + + def issue_grant( + self, + *, + context: FileGrantContext, + scopes: Sequence[FileGrantScope], + ttl_seconds: int, + ) -> tuple[str, int]: + expires_at = self._now() + ttl_seconds + token = jwt.encode( + { + "aud": FILE_GRANT_AUDIENCE, + "sub": context.end_user_id, + "tenant_id": context.tenant_id, + "app_id": context.app_id, + "scopes": [str(scope) for scope in scopes], + "exp": expires_at, + }, + self._secret_key, + algorithm=_ALGORITHM, + ) + return token, expires_at + + def decode_grant(self, token: str) -> FileGrantClaims | None: + payload = self._decode( + token, audience=FILE_GRANT_AUDIENCE, required=["exp", "sub", "tenant_id", "app_id", "scopes"] + ) + if payload is None: + return None + try: + return FileGrantClaims.model_validate(payload) + except ValidationError: + return None + + def issue_content_urls(self, *, file_id: str, kind: FileKind) -> tuple[str, str]: + external_token = self._issue_content_token(file_id=file_id, kind=kind) + internal_token = self._issue_content_token(file_id=file_id, kind=kind) + path = f"/files/appdeploy/{file_id}/content" + return ( + f"{self._external_files_url}{path}?token={external_token}", + f"{self._internal_files_url}{path}?token={internal_token}", + ) + + def decode_content_token(self, token: str) -> FileContentClaims | None: + payload = self._decode( + token, + audience=FILE_CONTENT_AUDIENCE, + required=["exp", "kind", "file_id"], + ) + if payload is None: + return None + try: + return FileContentClaims.model_validate(payload) + except ValidationError: + return None + + def _issue_content_token(self, *, file_id: str, kind: FileKind) -> str: + return jwt.encode( + { + "aud": FILE_CONTENT_AUDIENCE, + "kind": str(kind), + "file_id": file_id, + "nonce": os.urandom(8).hex(), + "exp": self._now() + self._content_token_ttl_seconds, + }, + self._secret_key, + algorithm=_ALGORITHM, + ) + + def _decode(self, token: str, *, audience: str, required: list[str]) -> dict[str, object] | None: + try: + return cast( + dict[str, object], + jwt.decode( + token, + self._secret_key, + algorithms=[_ALGORITHM], + audience=audience, + options={"require": ["aud", *required]}, + ), + ) + except jwt.PyJWTError: + return None + + +class FileGrantFileGateway: + def __init__( + self, + *, + load_end_user: Callable[[FileGrantContext], EndUser | None], + subject_exists: Callable[[FileGrantContext], bool], + file_service: FileService, + tool_files: ToolFileManager, + storage: Storage, + ) -> None: + self._load_end_user = load_end_user + self._subject_exists = subject_exists + self._file_service = file_service + self._tool_files = tool_files + self._storage = storage + + def store_upload_stream( + self, + *, + context: FileGrantContext, + filename: str, + stream: IO[bytes], + mimetype: str, + ) -> StoredUpload: + if not self._subject_exists(context): + raise EndUserNotFoundError(context.end_user_id) + extension = os.path.splitext(filename)[1].lstrip(".").lower() + limit = FileService.file_size_limit(extension=extension) + content = stream.read(limit + 1) + if len(content) > limit: + raise FileTooLargeError(f"File size exceeded. The limit is {limit} bytes.") + return self.store_upload( + context=context, + filename=filename, + content=content, + mimetype=mimetype, + ) + + def store_upload( + self, + *, + context: FileGrantContext, + filename: str, + content: bytes, + mimetype: str, + source_url: str = "", + ) -> StoredUpload: + end_user = self._load_end_user(context) + if end_user is None: + raise EndUserNotFoundError(context.end_user_id) + upload = self._file_service.upload_file( + filename=filename, + content=content, + mimetype=mimetype, + user=end_user, + source_url=source_url, + ) + return StoredUpload( + id=upload.id, + name=upload.name, + size=upload.size, + extension=upload.extension, + mime_type=upload.mime_type, + created_by=upload.created_by, + created_at=upload.created_at, + tenant_id=upload.tenant_id, + source_url=upload.source_url, + ) + + def store_produced( + self, + *, + context: FileGrantContext, + filename: str | None, + stream: IO[bytes], + mimetype: str, + ) -> StoredProducedFile: + extension = resolve_extension(filename=filename, mimetype=mimetype).lstrip(".").lower() + limit = FileService.file_size_limit(extension=extension) + content = stream.read(limit + 1) + if len(content) > limit: + raise FileTooLargeError(f"File size exceeded. The limit is {limit} bytes.") + stored = self._tool_files.create_file_by_raw( + user_id=context.end_user_id, + tenant_id=context.tenant_id, + conversation_id=None, + file_binary=content, + mimetype=mimetype, + filename=filename, + ) + return StoredProducedFile( + id=stored.id, + name=stored.name or "", + size=stored.size, + mime_type=stored.mimetype, + ) + + def open_content(self, record: FileContentRecord) -> FileContent: + return FileContent( + name=record.name, + size=record.size, + mime_type=record.mime_type, + stream=self._storage.load(record.storage_key, stream=True), + ) + + +class FileGrantRemoteFileGateway: + def fetch(self, url: str) -> RemoteFile | None: + try: + metadata = remote_fetcher.make_request("HEAD", url=url, follow_redirects=True) + if metadata.status_code != httpx.codes.OK: + metadata.close() + metadata = remote_fetcher.make_request( + "GET", + url=url, + timeout=3, + follow_redirects=True, + stream_response=True, + ) + if metadata.status_code != httpx.codes.OK: + metadata.close() + return None + + filename, extension, mimetype = self._file_info(metadata) + limit = FileService.file_size_limit(extension=extension) + declared_size = self._declared_size(metadata) + if declared_size is not None and declared_size > limit: + metadata.close() + raise FileTooLargeError(f"File size exceeded. The limit is {limit} bytes.") + + if metadata.request.method == "HEAD": + metadata.close() + response = remote_fetcher.make_request( + "GET", + url=url, + timeout=3, + follow_redirects=True, + stream_response=True, + ) + if response.status_code != httpx.codes.OK: + response.close() + return None + else: + response = metadata + + try: + buffered = ssrf_proxy.buffer_response(response, max_response_bytes=limit) + except ssrf_proxy.ResponseTooLargeError as exc: + raise FileTooLargeError(f"File size exceeded. The limit is {limit} bytes.") from exc + return RemoteFile(filename=filename, mimetype=mimetype, content=buffered.content) + except (httpx.RequestError, ssrf_proxy.UnsupportedResponseEncodingError): + return None + + @staticmethod + def _file_info(response: httpx.Response) -> tuple[str, str, str]: + parsed_url = urllib.parse.urlparse(str(response.url)) + filename = urllib.parse.unquote(os.path.basename(parsed_url.path)) + if not filename: + content_disposition = response.headers.get("Content-Disposition", "") + filename_match = re.search(r'filename="?([^";]+)', content_disposition) + filename = filename_match.group(1) if filename_match else uuid4().hex + extension = os.path.splitext(filename)[1].lstrip(".").lower() + mimetype = ( + mimetypes.guess_type(filename)[0] + or response.headers.get("Content-Type", "").split(";", 1)[0].strip() + or "application/octet-stream" + ) + return filename, extension, mimetype + + @staticmethod + def _declared_size(response: httpx.Response) -> int | None: + value = response.headers.get("Content-Length") + if value is None: + return None + try: + return int(value) + except ValueError: + return None + + +__all__ = [ + "FILE_CONTENT_AUDIENCE", + "FILE_GRANT_AUDIENCE", + "FileGrantFileGateway", + "FileGrantRemoteFileGateway", + "FileGrantTokenGateway", +] diff --git a/api/services/file_grant_service.py b/api/services/file_grant_service.py new file mode 100644 index 00000000000..1cf542f278c --- /dev/null +++ b/api/services/file_grant_service.py @@ -0,0 +1,314 @@ +from __future__ import annotations + +import base64 +import hashlib +from collections.abc import Callable, Sequence +from typing import IO, Protocol + +from services.entities.file_grant_entities import ( + FileContent, + FileContentClaims, + FileContentRecord, + FileGrantClaims, + FileGrantContext, + FileGrantLimits, + FileGrantMintRequest, + FileGrantMintResult, + FileGrantScope, + FileKind, + FileRef, + RemoteFile, + ResolvedFile, + ResolvedFileAccess, + StoredProducedFile, + StoredUpload, +) +from services.errors.file_grant import ( + AppNotFoundError, + EndUserNotFoundError, + GrantedFileNotFoundError, + GrantTtlTooLongError, + InvalidFileGrantError, + InvalidGrantRequestError, + InvalidSubjectError, + RemoteFileUnavailableError, + TooManyFileRefsError, +) + +MAX_SESSION_GRANT_TTL_SECONDS = 7200 +MAX_WORKFLOW_EXECUTION_SECONDS = 24 * 60 * 60 +RUN_GRANT_EXPIRY_GRACE_SECONDS = 5 * 60 +MAX_RUN_GRANT_TTL_SECONDS = MAX_WORKFLOW_EXECUTION_SECONDS + RUN_GRANT_EXPIRY_GRACE_SECONDS +MAX_FILE_GRANT_REFS = 100 + + +class FileGrantRepository(Protocol): + def get_or_create_subject( + self, + *, + tenant_id: str, + app_id: str, + session_id: str, + external_user_id: str, + is_anonymous: bool, + ) -> str | None: ... + + def subject_exists(self, context: FileGrantContext) -> bool: ... + + def resolve_owned_files( + self, + *, + context: FileGrantContext, + refs: Sequence[FileRef], + ) -> list[ResolvedFile | None]: ... + + def get_content_record(self, *, file_id: str, kind: FileKind) -> FileContentRecord | None: ... + + +class FileGrantFiles(Protocol): + def store_upload_stream( + self, + *, + context: FileGrantContext, + filename: str, + stream: IO[bytes], + mimetype: str, + ) -> StoredUpload: ... + + def store_upload( + self, + *, + context: FileGrantContext, + filename: str, + content: bytes, + mimetype: str, + source_url: str = "", + ) -> StoredUpload: ... + + def store_produced( + self, + *, + context: FileGrantContext, + filename: str | None, + stream: IO[bytes], + mimetype: str, + ) -> StoredProducedFile: ... + + def open_content(self, record: FileContentRecord) -> FileContent: ... + + +class FileGrantTokens(Protocol): + def issue_grant( + self, + *, + context: FileGrantContext, + scopes: Sequence[FileGrantScope], + ttl_seconds: int, + ) -> tuple[str, int]: ... + + def decode_grant(self, token: str) -> FileGrantClaims | None: ... + + def issue_content_urls(self, *, file_id: str, kind: FileKind) -> tuple[str, str]: ... + + def decode_content_token(self, token: str) -> FileContentClaims | None: ... + + +class FileGrantRemoteFiles(Protocol): + def fetch(self, url: str) -> RemoteFile | None: ... + + +class FileGrantService: + def __init__( + self, + *, + repository: FileGrantRepository, + files: FileGrantFiles, + tokens: FileGrantTokens, + remote_files: FileGrantRemoteFiles, + limits: FileGrantLimits, + now: Callable[[], int], + ) -> None: + self._repository = repository + self._files = files + self._tokens = tokens + self._remote_files = remote_files + self._limits = limits + self._now = now + + @staticmethod + def session_id_for_subject(subject: str) -> str: + digest = hashlib.sha256(subject.encode()).digest() + return base64.urlsafe_b64encode(digest).decode().rstrip("=") + + def mint(self, request: FileGrantMintRequest) -> FileGrantMintResult: + self._validate_subject(request.subject) + self._validate_ref_count((*request.file_refs, *request.optional_file_refs)) + ttl_seconds = self._effective_ttl_seconds(request) + + end_user_id = self._repository.get_or_create_subject( + tenant_id=request.tenant_id, + app_id=request.app_id, + session_id=self.session_id_for_subject(request.subject), + external_user_id=request.subject[:255], + is_anonymous=request.is_anonymous, + ) + if end_user_id is None: + raise AppNotFoundError(request.app_id) + + context = FileGrantContext(request.tenant_id, request.app_id, end_user_id) + strict_file_count = len(request.file_refs) + resolved_files = self._repository.resolve_owned_files( + context=context, + refs=(*request.file_refs, *request.optional_file_refs), + ) + strict_files = resolved_files[:strict_file_count] + if any(file is None for file in strict_files): + raise GrantedFileNotFoundError() + optional_files = resolved_files[strict_file_count:] + grant, expires_at = self._tokens.issue_grant( + context=context, + scopes=request.scopes, + ttl_seconds=ttl_seconds, + ) + return FileGrantMintResult( + grant=grant, + expires_at=expires_at, + limits=self._limits, + files=tuple(file for file in strict_files if file is not None), + optional_files=tuple(self._with_access(file) if file is not None else None for file in optional_files), + ) + + def decode_grant(self, token: str) -> FileGrantClaims | None: + return self._tokens.decode_grant(token) + + def store_upload( + self, + *, + context: FileGrantContext, + filename: str, + stream: IO[bytes], + mimetype: str, + ) -> StoredUpload: + return self._files.store_upload_stream( + context=context, + filename=filename, + stream=stream, + mimetype=mimetype, + ) + + def store_remote_upload(self, *, context: FileGrantContext, url: str) -> StoredUpload: + if not self._repository.subject_exists(context): + raise EndUserNotFoundError(context.end_user_id) + remote_file = self._remote_files.fetch(url) + if remote_file is None: + raise RemoteFileUnavailableError(url) + return self._files.store_upload( + context=context, + filename=remote_file.filename, + content=remote_file.content, + mimetype=remote_file.mimetype, + source_url=url, + ) + + def store_produced( + self, + *, + context: FileGrantContext, + filename: str | None, + stream: IO[bytes], + mimetype: str, + ) -> tuple[StoredProducedFile, ResolvedFileAccess]: + if not self._repository.subject_exists(context): + raise EndUserNotFoundError(context.end_user_id) + stored = self._files.store_produced( + context=context, + filename=filename, + stream=stream, + mimetype=mimetype, + ) + file = ResolvedFile(stored.id, FileKind.TOOL, stored.name, stored.size, "", stored.mime_type) + return stored, self._with_access(file) + + def resolve_files( + self, + *, + context: FileGrantContext, + refs: Sequence[FileRef], + ) -> list[ResolvedFile | None]: + self._validate_ref_count(refs) + if not self._repository.subject_exists(context): + raise EndUserNotFoundError(context.end_user_id) + return self._repository.resolve_owned_files(context=context, refs=refs) + + def resolve_file_access( + self, + *, + context: FileGrantContext, + refs: Sequence[FileRef], + ) -> list[ResolvedFileAccess | None]: + return [ + self._with_access(file) if file is not None else None + for file in self.resolve_files(context=context, refs=refs) + ] + + def load_content(self, *, token: str, requested_file_id: str) -> FileContent | None: + claims = self._tokens.decode_content_token(token) + if claims is None: + raise InvalidFileGrantError() + if claims.file_id != requested_file_id: + return None + record = self._repository.get_content_record(file_id=requested_file_id, kind=claims.kind) + return self._files.open_content(record) if record is not None else None + + def content_urls(self, *, file_id: str, kind: FileKind) -> tuple[str, str]: + return self._tokens.issue_content_urls(file_id=file_id, kind=kind) + + def _with_access(self, file: ResolvedFile) -> ResolvedFileAccess: + external_url, internal_url = self._tokens.issue_content_urls(file_id=file.id, kind=file.kind) + return ResolvedFileAccess(file=file, external_url=external_url, internal_url=internal_url) + + def _effective_ttl_seconds(self, request: FileGrantMintRequest) -> int: + if request.run_deadline is None: + if request.ttl_seconds > MAX_SESSION_GRANT_TTL_SECONDS: + raise GrantTtlTooLongError() + return request.ttl_seconds + + now = self._now() + if FileGrantScope.PRODUCE not in request.scopes: + raise InvalidGrantRequestError("A run deadline requires the produce scope.") + if request.run_deadline <= now: + raise InvalidGrantRequestError("The run deadline has expired.") + if request.run_deadline > now + MAX_WORKFLOW_EXECUTION_SECONDS: + raise InvalidGrantRequestError("The run deadline exceeds the workflow execution limit.") + if request.ttl_seconds > MAX_RUN_GRANT_TTL_SECONDS: + raise GrantTtlTooLongError() + return min(request.ttl_seconds, request.run_deadline - now + RUN_GRANT_EXPIRY_GRACE_SECONDS) + + @staticmethod + def _validate_subject(subject: str) -> None: + if not subject.strip() or "\x00" in subject: + raise InvalidSubjectError() + + @staticmethod + def _validate_ref_count(refs: Sequence[FileRef]) -> None: + if len(refs) > MAX_FILE_GRANT_REFS: + raise TooManyFileRefsError(f"A file grant request may contain at most {MAX_FILE_GRANT_REFS} references.") + + +__all__ = [ + "MAX_FILE_GRANT_REFS", + "MAX_RUN_GRANT_TTL_SECONDS", + "MAX_SESSION_GRANT_TTL_SECONDS", + "MAX_WORKFLOW_EXECUTION_SECONDS", + "RUN_GRANT_EXPIRY_GRACE_SECONDS", + "AppNotFoundError", + "EndUserNotFoundError", + "FileGrantService", + "GrantTtlTooLongError", + "GrantedFileNotFoundError", + "InvalidFileGrantError", + "InvalidGrantRequestError", + "InvalidSubjectError", + "RemoteFileUnavailableError", + "TooManyFileRefsError", +] diff --git a/api/services/file_service.py b/api/services/file_service.py index 4497639eb25..9c387720002 100644 --- a/api/services/file_service.py +++ b/api/services/file_service.py @@ -131,21 +131,32 @@ class FileService: file_size: int, default_file_size_limit: int | None = None, ) -> bool: + return file_size <= FileService.file_size_limit( + extension=extension, + default_file_size_limit=default_file_size_limit, + ) + + @staticmethod + def file_size_limit( + *, + extension: str, + default_file_size_limit: int | None = None, + ) -> int: + """Return the size an extension is allowed, in bytes.""" + if extension in IMAGE_EXTENSIONS: - file_size_limit = dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT * 1024 * 1024 + file_size_limit = dify_config.UPLOAD_IMAGE_FILE_SIZE_LIMIT elif extension in VIDEO_EXTENSIONS: - file_size_limit = dify_config.UPLOAD_VIDEO_FILE_SIZE_LIMIT * 1024 * 1024 + file_size_limit = dify_config.UPLOAD_VIDEO_FILE_SIZE_LIMIT elif extension in AUDIO_EXTENSIONS: - file_size_limit = dify_config.UPLOAD_AUDIO_FILE_SIZE_LIMIT * 1024 * 1024 + file_size_limit = dify_config.UPLOAD_AUDIO_FILE_SIZE_LIMIT else: # Context-specific uploads may override the default limit without changing media-specific limits. file_size_limit = ( - (default_file_size_limit if default_file_size_limit is not None else dify_config.UPLOAD_FILE_SIZE_LIMIT) - * 1024 - * 1024 + default_file_size_limit if default_file_size_limit is not None else dify_config.UPLOAD_FILE_SIZE_LIMIT ) - return file_size <= file_size_limit + return file_size_limit * 1024 * 1024 def get_file_base64(self, file_id: str) -> str: with self._session_maker(expire_on_commit=False) as session: diff --git a/api/services/notification_service.py b/api/services/notification_service.py index 13236ef16ed..1d20f01f14a 100644 --- a/api/services/notification_service.py +++ b/api/services/notification_service.py @@ -2,8 +2,8 @@ from typing import Protocol +from constants.languages import languages from machinery.context import RequestContext -from services.account_ports import AccountRepository from services.entities.notification_entities import ( AccountNotification, AccountNotificationBatch, @@ -22,20 +22,15 @@ class NotificationGateway(Protocol): class NotificationService: - def __init__(self, *, accounts: AccountRepository, notifications: NotificationGateway) -> None: - self._accounts = accounts + def __init__(self, *, notifications: NotificationGateway) -> None: self._notifications = notifications - def get_active(self, context: RequestContext) -> NotificationResult: + def get_active(self, context: RequestContext, language: str) -> NotificationResult: batch = self._notifications.get_active(context.account_id) if not batch.should_show: return NotificationResult(should_show=False, notifications=()) - account = self._accounts.get(context.account_id) - if account is None: - raise RuntimeError("Console account admission resolved an unknown account") - language = account.interface_language or _FALLBACK_LANGUAGE - + language = language if language in languages else _FALLBACK_LANGUAGE notifications = tuple(self._localize(notification, language) for notification in batch.notifications) return NotificationResult(should_show=bool(notifications), notifications=notifications) diff --git a/api/services/oauth_device_flow.py b/api/services/oauth_device_flow.py index 9e59b8c326a..f3967026aad 100644 --- a/api/services/oauth_device_flow.py +++ b/api/services/oauth_device_flow.py @@ -10,12 +10,12 @@ import uuid from dataclasses import asdict, dataclass, field from datetime import UTC, datetime, timedelta from enum import StrEnum -from typing import Any, NotRequired, TypedDict +from typing import NotRequired, TypedDict -from sqlalchemy import and_, func, select, update +from sqlalchemy import func, select, update from sqlalchemy.orm import Session -from libs.oauth_bearer import TOKEN_CACHE_KEY_FMT, AuthContext, SubjectType +from libs.oauth_bearer import SubjectType, invalidate_oauth_token_cache from models.oauth import OAuthAccessToken logger = logging.getLogger(__name__) @@ -382,7 +382,7 @@ def mint_oauth_token( ) if outcome.rotated and outcome.old_hash: - redis_client.delete(TOKEN_CACHE_KEY_FMT.format(hash=outcome.old_hash)) + invalidate_oauth_token_cache(redis_client, outcome.old_hash) return MintResult(token=token, token_id=outcome.token_id, expires_at=expires_at) @@ -487,70 +487,3 @@ def oauth_ttl_days(tenant_id: str | None = None) -> int: logger.warning("%s=%d above max %d; clamping", _TTL_ENV_VAR, value, MAX_TTL_DAYS) return MAX_TTL_DAYS return value - - -def subject_match_clauses(ctx: AuthContext) -> tuple[Any, ...]: - if ctx.subject_type == SubjectType.ACCOUNT: - return (OAuthAccessToken.account_id == str(ctx.account_id),) - return ( - OAuthAccessToken.subject_email == ctx.subject_email, - OAuthAccessToken.subject_issuer == ctx.subject_issuer, - OAuthAccessToken.account_id.is_(None), - ) - - -def list_active_sessions(ctx: AuthContext, now: datetime, *, session: Session) -> list[OAuthAccessToken]: - return list( - session.execute( - select(OAuthAccessToken) - .where( - and_( - *subject_match_clauses(ctx), - OAuthAccessToken.revoked_at.is_(None), - OAuthAccessToken.token_hash.is_not(None), - OAuthAccessToken.expires_at > now, - ) - ) - .order_by(OAuthAccessToken.created_at.desc()) - ) - .scalars() - .all() - ) - - -def token_belongs_to_subject(token_id: str, ctx: AuthContext, *, session: Session) -> bool: - row = session.execute( - select(OAuthAccessToken.id).where( - and_( - OAuthAccessToken.id == token_id, - *subject_match_clauses(ctx), - ) - ) - ).first() - return row is not None - - -def revoke_oauth_token(redis_client: Any, token_id: str, *, session: Session) -> None: - row = ( - session.query(OAuthAccessToken.token_hash) - .filter( - OAuthAccessToken.id == token_id, - OAuthAccessToken.revoked_at.is_(None), - ) - .one_or_none() - ) - pre_revoke_hash = row[0] if row else None - - stmt = ( - update(OAuthAccessToken) - .where( - OAuthAccessToken.id == token_id, - OAuthAccessToken.revoked_at.is_(None), - ) - .values(revoked_at=datetime.now(UTC), token_hash=None) - ) - session.execute(stmt) - session.commit() - - if pre_revoke_hash: - redis_client.delete(TOKEN_CACHE_KEY_FMT.format(hash=pre_revoke_hash)) diff --git a/api/services/ops_service.py b/api/services/ops_service.py deleted file mode 100644 index b6f17168b3c..00000000000 --- a/api/services/ops_service.py +++ /dev/null @@ -1,282 +0,0 @@ -from typing import Any - -from sqlalchemy import select -from sqlalchemy.orm import Session - -from core.ops.entities.config_entity import BaseTracingConfig -from core.ops.ops_trace_manager import OpsTraceManager, TracingProviderConfigEntry, provider_config_map -from models.model import App, TraceAppConfig - - -class OpsService: - @classmethod - def get_tracing_app_config(cls, app_id: str, tracing_provider: str, session: Session): - """ - Get tracing app config - :param app_id: app id - :param tracing_provider: tracing provider - :return: - """ - trace_config_data: TraceAppConfig | None = session.scalar( - select(TraceAppConfig) - .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider) - .limit(1) - ) - - if not trace_config_data: - return None - - # decrypt_token and obfuscated_token - app = session.get(App, app_id) - if not app: - return None - tenant_id = app.tenant_id - if trace_config_data.tracing_config is None: - raise ValueError("Tracing config cannot be None.") - decrypt_tracing_config = OpsTraceManager.decrypt_tracing_config( - tenant_id, tracing_provider, trace_config_data.tracing_config - ) - new_decrypt_tracing_config = OpsTraceManager.obfuscated_decrypt_token(tracing_provider, decrypt_tracing_config) - - if tracing_provider == "arize" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://app.arize.com/"}) - - if tracing_provider == "phoenix" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://app.phoenix.arize.com/projects/"}) - - if tracing_provider == "langfuse" and ( - "project_key" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_key") - ): - try: - project_key = OpsTraceManager.get_trace_config_project_key(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update( - { - "project_url": "{host}/project/{key}".format( - host=decrypt_tracing_config.get("host"), key=project_key - ) - } - ) - except Exception: - new_decrypt_tracing_config.update({"project_url": f"{decrypt_tracing_config.get('host')}/"}) - - if tracing_provider == "langsmith" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://smith.langchain.com/"}) - - if tracing_provider == "opik" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://www.comet.com/opik/"}) - if tracing_provider == "weave" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://wandb.ai/"}) - - if tracing_provider == "aliyun" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://arms.console.aliyun.com/"}) - - if tracing_provider == "tencent" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://console.cloud.tencent.com/apm"}) - - if tracing_provider == "mlflow" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "http://localhost:5000/"}) - - if tracing_provider == "databricks" and ( - "project_url" not in decrypt_tracing_config or not decrypt_tracing_config.get("project_url") - ): - try: - project_url = OpsTraceManager.get_trace_config_project_url(decrypt_tracing_config, tracing_provider) - new_decrypt_tracing_config.update({"project_url": project_url}) - except Exception: - new_decrypt_tracing_config.update({"project_url": "https://www.databricks.com/"}) - - trace_config_data.tracing_config = new_decrypt_tracing_config - return trace_config_data.to_dict() - - @classmethod - def create_tracing_app_config( - cls, app_id: str, tracing_provider: str, tracing_config: dict[str, Any], session: Session - ): - """ - Create tracing app config - :param app_id: app id - :param tracing_provider: tracing provider - :param tracing_config: tracing config - :return: - """ - try: - provider_config_map[tracing_provider] - except KeyError: - return {"error": f"Invalid tracing provider: {tracing_provider}"} - - provider_config: TracingProviderConfigEntry = provider_config_map[tracing_provider] - config_class: type[BaseTracingConfig] = provider_config["config_class"] - other_keys: list[str] = provider_config["other_keys"] - - default_config_instance = config_class.model_validate(tracing_config) - for key in other_keys: - if key in tracing_config and tracing_config[key] == "": - tracing_config[key] = getattr(default_config_instance, key, None) - - # api check - if not OpsTraceManager.check_trace_config_is_effective(tracing_config, tracing_provider): - return {"error": "Invalid Credentials"} - - # get project url - if tracing_provider in ("arize", "phoenix"): - try: - project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider) - except Exception: - project_url = None - elif tracing_provider == "langfuse": - try: - project_key = OpsTraceManager.get_trace_config_project_key(tracing_config, tracing_provider) - project_url = f"{tracing_config.get('host')}/project/{project_key}" - except Exception: - project_url = None - elif tracing_provider in ("langsmith", "opik", "mlflow", "databricks", "tencent"): - try: - project_url = OpsTraceManager.get_trace_config_project_url(tracing_config, tracing_provider) - except Exception: - project_url = None - else: - project_url = None - - # check if trace config already exists - trace_config_data: TraceAppConfig | None = session.scalar( - select(TraceAppConfig) - .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider) - .limit(1) - ) - - if trace_config_data: - return None - - # get tenant id - app = session.get(App, app_id) - if not app: - return None - tenant_id = app.tenant_id - tracing_config = OpsTraceManager.encrypt_tracing_config(tenant_id, tracing_provider, tracing_config) - if project_url: - tracing_config["project_url"] = project_url - trace_config_data = TraceAppConfig( - app_id=app_id, - tracing_provider=tracing_provider, - tracing_config=tracing_config, - ) - session.add(trace_config_data) - session.commit() - - return {"result": "success"} - - @classmethod - def update_tracing_app_config( - cls, app_id: str, tracing_provider: str, tracing_config: dict[str, Any], session: Session - ): - """ - Update tracing app config - :param app_id: app id - :param tracing_provider: tracing provider - :param tracing_config: tracing config - :return: - """ - try: - provider_config_map[tracing_provider] - except KeyError: - raise ValueError(f"Invalid tracing provider: {tracing_provider}") - - # check if trace config already exists - current_trace_config = session.scalar( - select(TraceAppConfig) - .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider) - .limit(1) - ) - - if not current_trace_config: - return None - - # get tenant id - app = session.get(App, app_id) - if not app: - return None - tenant_id = app.tenant_id - tracing_config = OpsTraceManager.encrypt_tracing_config( - tenant_id, tracing_provider, tracing_config, current_trace_config.tracing_config - ) - - # api check - # decrypt_token - decrypt_tracing_config = OpsTraceManager.decrypt_tracing_config(tenant_id, tracing_provider, tracing_config) - if not OpsTraceManager.check_trace_config_is_effective(decrypt_tracing_config, tracing_provider): - raise ValueError("Invalid Credentials") - - current_trace_config.tracing_config = tracing_config - session.commit() - - return current_trace_config.to_dict() - - @classmethod - def delete_tracing_app_config(cls, app_id: str, tracing_provider: str, session: Session): - """ - Delete tracing app config - :param app_id: app id - :param tracing_provider: tracing provider - :return: - """ - trace_config = session.scalar( - select(TraceAppConfig) - .where(TraceAppConfig.app_id == app_id, TraceAppConfig.tracing_provider == tracing_provider) - .limit(1) - ) - - if not trace_config: - return None - - session.delete(trace_config) - session.commit() - - return True diff --git a/api/services/recommended_app_query_service.py b/api/services/recommended_app_query_service.py index 5b79f40d505..f0da2259797 100644 --- a/api/services/recommended_app_query_service.py +++ b/api/services/recommended_app_query_service.py @@ -5,6 +5,8 @@ from typing import NamedTuple, Protocol from constants.languages import languages +_DEFAULT_LANGUAGE = "en-US" + class RecommendedAppInfoRecord(NamedTuple): id: str @@ -116,10 +118,9 @@ class RecommendedAppQueryService: def list_recommended( self, *, - requested_language: str | None, - interface_language: str | None, + language: str, ) -> RecommendedAppListResult: - language = self._resolve_language(requested_language, interface_language) + language = language if language in languages else _DEFAULT_LANGUAGE page = self._catalog.list_recommended(language) return RecommendedAppListResult( @@ -130,10 +131,9 @@ class RecommendedAppQueryService: def list_learn_dify( self, *, - requested_language: str | None, - interface_language: str | None, + language: str, ) -> LearnDifyAppListResult: - language = self._resolve_language(requested_language, interface_language) + language = language if language in languages else _DEFAULT_LANGUAGE page = self._catalog.list_learn_dify(language) return LearnDifyAppListResult(recommended_apps=self._with_trial_status(page.recommended_apps)) @@ -176,11 +176,3 @@ class RecommendedAppQueryService: ) for app in apps ) - - @staticmethod - def _resolve_language(requested_language: str | None, interface_language: str | None) -> str: - if requested_language and requested_language in languages: - return requested_language - if interface_language: - return interface_language - return languages[0] diff --git a/api/services/skill_management_service.py b/api/services/skill_management_service.py index 92d3de2f2ea..4d4c4299b79 100644 --- a/api/services/skill_management_service.py +++ b/api/services/skill_management_service.py @@ -3747,14 +3747,22 @@ class SkillManagementService: @staticmethod def _strip_single_root(paths: list[str]) -> dict[str, str]: - if not paths: - return {} - first_segments = {path.split("/", 1)[0] for path in paths if "/" in path} - root = next(iter(first_segments)) if len(first_segments) == 1 else None - if root is None or f"{root}/{_SKILL_MD}" not in paths or _SKILL_MD in paths: + if not paths or _SKILL_MD in paths: return {path: path for path in paths} - stripped = {path: path.removeprefix(f"{root}/") for path in paths} - return stripped + # Identify the root by the unique top-level `/SKILL.md`, rather than + # requiring every path to share one first segment: tools like macOS Finder's + # "Compress" add a sibling `__MACOSX/` metadata folder that must not defeat + # stripping of the real skill folder. Entries outside the detected root + # (like `__MACOSX/...`) are dropped rather than passed through, matching + # skill_package_service._normalize_members(ignore_outside_selected_root=True). + skill_md_roots = { + path.split("/", 1)[0] for path in paths if path.count("/") == 1 and path.endswith(f"/{_SKILL_MD}") + } + if len(skill_md_roots) != 1: + return {path: path for path in paths} + root = next(iter(skill_md_roots)) + prefix = f"{root}/" + return {path: path.removeprefix(prefix) for path in paths if path == root or path.startswith(prefix)} def _draft_payload_from_zip( self, @@ -3775,6 +3783,8 @@ class SkillManagementService: skill_md_content = "" for info in infos: raw_path = normalize_skill_file_path(info.filename.strip("/")) + if raw_path not in path_map: + continue path = normalize_skill_file_path(path_map[raw_path]) if info.is_dir(): items.append(SkillDraftTreeItemPayload(path=path, kind=SkillFileKind.DIRECTORY)) diff --git a/api/services/trigger/trigger_subscription_builder_service.py b/api/services/trigger/trigger_subscription_builder_service.py index 0901e5ccf7f..d10d96d2258 100644 --- a/api/services/trigger/trigger_subscription_builder_service.py +++ b/api/services/trigger/trigger_subscription_builder_service.py @@ -267,7 +267,11 @@ class TriggerSubscriptionBuilderService: credentials=subscription_builder.credentials, credential_type=credential_type, credential_expires_at=subscription_builder.credential_expires_at or -1, - expires_at=subscription_builder.expires_at, + expires_at=( + subscription.expires_at + if subscription.expires_at is not None + else (subscription_builder.expires_at or -1) + ), ) # Delete the builder after successful subscription creation diff --git a/api/services/web_passport_gateways.py b/api/services/web_passport_gateways.py index f6cf423302a..774a864e74a 100644 --- a/api/services/web_passport_gateways.py +++ b/api/services/web_passport_gateways.py @@ -10,6 +10,16 @@ from services.enterprise.enterprise_service import PERMISSION_CHECK_MODES, WebAp from services.web_passport_service import WebAppAuthType, WebPassportUnauthorizedError +def resolve_web_app_auth_type(access_mode: str) -> WebAppAuthType: + if access_mode == WebAppAccessMode.PUBLIC: + return WebAppAuthType.PUBLIC + if access_mode in PERMISSION_CHECK_MODES: + return WebAppAuthType.INTERNAL + if access_mode == WebAppAccessMode.SSO_VERIFIED: + return WebAppAuthType.EXTERNAL + raise ValueError(f"Unsupported web app access mode: {access_mode}") + + class DeploymentWebPassportAuthGateway: def __init__( self, @@ -25,13 +35,7 @@ class DeploymentWebPassportAuthGateway: def get_app_auth_type(self, app_id: str) -> WebAppAuthType: access_mode = self._get_app_access_mode(app_id).access_mode - if access_mode == WebAppAccessMode.PUBLIC: - return WebAppAuthType.PUBLIC - if access_mode in PERMISSION_CHECK_MODES: - return WebAppAuthType.INTERNAL - if access_mode == WebAppAccessMode.SSO_VERIFIED: - return WebAppAuthType.EXTERNAL - raise ValueError(f"Unsupported web app access mode: {access_mode}") + return resolve_web_app_auth_type(access_mode) class PassportTokenGateway: diff --git a/api/services/workflow_app_log_query_service.py b/api/services/workflow_app_log_query_service.py new file mode 100644 index 00000000000..4eb1df365d8 --- /dev/null +++ b/api/services/workflow_app_log_query_service.py @@ -0,0 +1,174 @@ +import json +from collections.abc import Mapping +from dataclasses import dataclass, replace +from datetime import datetime +from typing import Any, Protocol + +from core.plugin.plugin_service import PluginService +from graphon.enums import WorkflowExecutionStatus +from models.enums import AppTriggerType +from services.workflow.entities import TriggerMetadata + + +@dataclass(frozen=True, slots=True) +class WorkflowAppLogAccount: + id: str + name: str + email: str + + +@dataclass(frozen=True, slots=True) +class WorkflowAppLogEndUser: + id: str + type: str + is_anonymous: bool + session_id: str | None + + +@dataclass(frozen=True, slots=True) +class WorkflowAppLogRunSummary: + id: str + version: str | None + status: str + triggered_from: str + error: str | None + elapsed_time: float | None + total_tokens: int | None + total_steps: int | None + created_at: datetime | None + finished_at: datetime | None + exceptions_count: int | None + + +@dataclass(frozen=True, slots=True) +class WorkflowAppLogItem: + id: str + workflow_run: WorkflowAppLogRunSummary | None + details: dict[str, Any] | None + created_from: str + created_by_role: str + created_by_account: WorkflowAppLogAccount | None + created_by_end_user: WorkflowAppLogEndUser | None + created_at: datetime + + +@dataclass(frozen=True, slots=True) +class WorkflowAppLogPage: + page: int + limit: int + total: int + has_more: bool + data: tuple[WorkflowAppLogItem, ...] + + +class WorkflowAppLogQuery(Protocol): + def get_paginated( + self, + *, + tenant_id: str, + app_id: str, + keyword: str | None = None, + status: WorkflowExecutionStatus | None = None, + created_at_before: datetime | None = None, + created_at_after: datetime | None = None, + page: int = 1, + limit: int = 20, + detail: bool = False, + created_by_end_user_session_id: str | None = None, + created_by_account: str | None = None, + ) -> WorkflowAppLogPage: ... + + +class WorkflowAppLogQueryService: + def __init__( + self, + *, + logs: WorkflowAppLogQuery, + ) -> None: + self._logs = logs + + def list_logs( + self, + *, + tenant_id: str, + app_id: str, + keyword: str | None = None, + status: WorkflowExecutionStatus | None = None, + created_at_before: datetime | None = None, + created_at_after: datetime | None = None, + page: int = 1, + limit: int = 20, + detail: bool = False, + created_by_end_user_session_id: str | None = None, + created_by_account: str | None = None, + ) -> WorkflowAppLogPage: + result = self._logs.get_paginated( + tenant_id=tenant_id, + app_id=app_id, + keyword=keyword, + status=status, + created_at_before=created_at_before, + created_at_after=created_at_after, + page=page, + limit=limit, + detail=detail, + created_by_end_user_session_id=created_by_end_user_session_id, + created_by_account=created_by_account, + ) + + return WorkflowAppLogPage( + page=result.page, + limit=result.limit, + total=result.total, + has_more=result.has_more, + data=tuple(self._resolve_details(tenant_id=tenant_id, item=item) for item in result.data), + ) + + def _resolve_details( + self, + *, + tenant_id: str, + item: WorkflowAppLogItem, + ) -> WorkflowAppLogItem: + if item.details is None: + return item + + return replace( + item, + details={ + "trigger_metadata": self._resolve_trigger_metadata( + tenant_id, + item.details.get("trigger_metadata"), + ) + }, + ) + + @staticmethod + def _resolve_trigger_metadata( + tenant_id: str, + value: str | Mapping[str, Any] | None, + ) -> dict[str, Any]: + metadata = WorkflowAppLogQueryService._safe_json_loads(value) + if not metadata: + return {} + + trigger_metadata = TriggerMetadata.model_validate(metadata) + if trigger_metadata.type == AppTriggerType.TRIGGER_PLUGIN: + icon = metadata.get("icon_filename") + icon_dark = metadata.get("icon_dark_filename") + metadata["icon"] = PluginService.get_plugin_icon_url(tenant_id=tenant_id, filename=icon) if icon else None + metadata["icon_dark"] = ( + PluginService.get_plugin_icon_url(tenant_id=tenant_id, filename=icon_dark) if icon_dark else None + ) + return metadata + + @staticmethod + def _safe_json_loads(value: Any) -> Any: + if not value: + return None + if isinstance(value, str): + try: + return json.loads(value) + except Exception: + return None + return value diff --git a/api/services/workflow_app_service.py b/api/services/workflow_app_service.py deleted file mode 100644 index 453c130e1aa..00000000000 --- a/api/services/workflow_app_service.py +++ /dev/null @@ -1,231 +0,0 @@ -import json -import uuid -from datetime import datetime -from typing import Any, TypedDict - -from sqlalchemy import and_, func, or_, select -from sqlalchemy.orm import Session - -from core.plugin.plugin_service import PluginService -from graphon.enums import WorkflowExecutionStatus -from models import Account, App, EndUser, TenantAccountJoin, WorkflowAppLog, WorkflowRun -from models.enums import AppTriggerType, CreatorUserRole -from models.trigger import WorkflowTriggerLog -from services.workflow.entities import TriggerMetadata - - -class LogViewDetails(TypedDict): - trigger_metadata: dict[str, Any] | None - - -# Since the workflow_app_log table has exceeded 100 million records, we use an additional details field to extend it -class LogView: - """Lightweight wrapper for WorkflowAppLog with computed details. - - - Exposes `details_` for marshalling to `details` in API response - - Resolves the account/end-user accessors through the session it was built with - - Proxies all other attributes to the underlying `WorkflowAppLog` - """ - - def __init__(self, log: WorkflowAppLog, details: LogViewDetails | None, session: Session): - self.log = log - self.details_ = details - self._session = session - - @property - def details(self) -> LogViewDetails | None: - return self.details_ - - @property - def created_by_account(self) -> Account | None: - return self.log.created_by_account(self._session) - - @property - def created_by_end_user(self) -> EndUser | None: - return self.log.created_by_end_user(self._session) - - def __getattr__(self, name): - return getattr(self.log, name) - - -class WorkflowAppService: - def get_paginate_workflow_app_logs( - self, - *, - session: Session, - app_model: App, - keyword: str | None = None, - status: WorkflowExecutionStatus | None = None, - created_at_before: datetime | None = None, - created_at_after: datetime | None = None, - page: int = 1, - limit: int = 20, - detail: bool = False, - created_by_end_user_session_id: str | None = None, - created_by_account: str | None = None, - ): - """ - Get paginate workflow app logs using SQLAlchemy 2.0 style - :param session: SQLAlchemy session - :param app_model: app model - :param keyword: search keyword - :param status: filter by status - :param created_at_before: filter logs created before this timestamp - :param created_at_after: filter logs created after this timestamp - :param page: page number - :param limit: items per page - :param detail: whether to return detailed logs - :param created_by_end_user_session_id: filter by end user session id - :param created_by_account: filter by account email - :return: Pagination object - """ - # Build base statement using SQLAlchemy 2.0 style - stmt = select(WorkflowAppLog).where( - WorkflowAppLog.tenant_id == app_model.tenant_id, WorkflowAppLog.app_id == app_model.id - ) - - if detail: - # Simple left join by workflow_run_id to fetch trigger_metadata - stmt = stmt.outerjoin( - WorkflowTriggerLog, - and_( - WorkflowTriggerLog.tenant_id == app_model.tenant_id, - WorkflowTriggerLog.app_id == app_model.id, - WorkflowTriggerLog.workflow_run_id == WorkflowAppLog.workflow_run_id, - ), - ).add_columns(WorkflowTriggerLog.trigger_metadata) - - if keyword or status: - stmt = stmt.join(WorkflowRun, WorkflowRun.id == WorkflowAppLog.workflow_run_id) - # Join to workflow run for filtering when needed. - - if keyword: - from libs.helper import escape_like_pattern - - # Escape special characters in keyword to prevent SQL injection via LIKE wildcards - escaped_keyword = escape_like_pattern(keyword[:30]) - keyword_like_val = f"%{escaped_keyword}%" - keyword_conditions = [ - WorkflowRun.inputs.ilike(keyword_like_val, escape="\\"), - WorkflowRun.outputs.ilike(keyword_like_val, escape="\\"), - # filter keyword by end user session id if created by end user role - and_( - WorkflowRun.created_by_role == "end_user", - EndUser.session_id.ilike(keyword_like_val, escape="\\"), - ), - ] - - # filter keyword by workflow run id - keyword_uuid = self._safe_parse_uuid(keyword) - if keyword_uuid: - keyword_conditions.append(WorkflowRun.id == keyword_uuid) - - stmt = stmt.outerjoin( - EndUser, - and_(WorkflowRun.created_by == EndUser.id, WorkflowRun.created_by_role == CreatorUserRole.END_USER), - ).where(or_(*keyword_conditions)) - - if status: - stmt = stmt.where(WorkflowRun.status == status) - - # Add time-based filtering - if created_at_before: - stmt = stmt.where(WorkflowAppLog.created_at <= created_at_before) - - if created_at_after: - stmt = stmt.where(WorkflowAppLog.created_at >= created_at_after) - - # Filter by end user session id or account email - if created_by_end_user_session_id: - stmt = stmt.join( - EndUser, - and_( - WorkflowAppLog.created_by == EndUser.id, - WorkflowAppLog.created_by_role == CreatorUserRole.END_USER, - EndUser.session_id == created_by_end_user_session_id, - ), - ) - if created_by_account: - account = session.scalar( - select(Account) - .join(TenantAccountJoin, TenantAccountJoin.account_id == Account.id) - .where( - Account.email == created_by_account, - TenantAccountJoin.tenant_id == app_model.tenant_id, - ) - ) - if not account: - raise ValueError(f"Account not found: {created_by_account}") - - stmt = stmt.join( - Account, - and_( - WorkflowAppLog.created_by == Account.id, - WorkflowAppLog.created_by_role == CreatorUserRole.ACCOUNT, - Account.id == account.id, - ), - ) - - stmt = stmt.order_by(WorkflowAppLog.created_at.desc()) - - # Get total count using the same filters - count_stmt = select(func.count()).select_from(stmt.subquery()) - total = session.scalar(count_stmt) or 0 - - # Apply pagination limits - offset_stmt = stmt.offset((page - 1) * limit).limit(limit) - - # wrapper moved to module scope as `LogView` - - # Execute query and get items - if detail: - rows = session.execute(offset_stmt).all() - items = [ - LogView(log, {"trigger_metadata": self.handle_trigger_metadata(app_model.tenant_id, meta_val)}, session) - for log, meta_val in rows - ] - else: - items = [LogView(log, None, session) for log in session.scalars(offset_stmt).all()] - return { - "page": page, - "limit": limit, - "total": total, - "has_more": total > page * limit, - "data": items, - } - - def handle_trigger_metadata(self, tenant_id: str, meta_val: str | None) -> dict[str, Any]: - metadata: dict[str, Any] | None = self._safe_json_loads(meta_val) - if not metadata: - return {} - trigger_metadata = TriggerMetadata.model_validate(metadata) - if trigger_metadata.type == AppTriggerType.TRIGGER_PLUGIN: - icon = metadata.get("icon_filename") - icon_dark = metadata.get("icon_dark_filename") - metadata["icon"] = PluginService.get_plugin_icon_url(tenant_id=tenant_id, filename=icon) if icon else None - metadata["icon_dark"] = ( - PluginService.get_plugin_icon_url(tenant_id=tenant_id, filename=icon_dark) if icon_dark else None - ) - return metadata - - @staticmethod - def _safe_json_loads(val): - if not val: - return None - if isinstance(val, str): - try: - return json.loads(val) - except Exception: - return None - return val - - @staticmethod - def _safe_parse_uuid(value: str): - # fast check - if len(value) < 32: - return None - - try: - return uuid.UUID(value) - except ValueError: - return None diff --git a/api/services/workflow_run_service.py b/api/services/workflow_run_service.py index 8618b343ae6..dac000065cd 100644 --- a/api/services/workflow_run_service.py +++ b/api/services/workflow_run_service.py @@ -1,22 +1,19 @@ import threading +from dataclasses import dataclass +from datetime import datetime from typing import TypedDict -from sqlalchemy import Engine, select -from sqlalchemy.orm import sessionmaker - import contexts -from extensions.ext_database import db +from core.workflow.nodes.human_input.pause_reason import HumanInputRequired +from graphon.enums import WorkflowExecutionStatus from libs.infinite_scroll_pagination import InfiniteScrollPagination +from machinery.context import RequestContext from models import ( - Account, - App, - EndUser, - Message, WorkflowRun, WorkflowRunTriggeredFrom, ) -from repositories.api_workflow_run_repository import APIWorkflowRunRepository -from repositories.factory import DifyAPIRepositoryFactory +from repositories.api_workflow_node_execution_repository import DifyAPIWorkflowNodeExecutionRepository +from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository from services.workflow_node_execution_trace_service import ( WorkflowNodeExecutionTrace, assemble_workflow_node_execution_traces, @@ -31,34 +28,43 @@ class WorkflowRunListArgs(TypedDict, total=False): status: str +@dataclass(frozen=True, slots=True) +class WorkflowRunPausedNode: + node_id: str + node_title: str + form_id: str + form_token: str | None + + +@dataclass(frozen=True, slots=True) +class WorkflowRunPauseDetails: + paused_at: datetime | None + paused_nodes: tuple[WorkflowRunPausedNode, ...] + + class WorkflowRunService: - _session_factory: sessionmaker - _workflow_run_repo: APIWorkflowRunRepository - - def __init__(self, session_factory: Engine | sessionmaker | None = None): - """Initialize WorkflowRunService with repository dependencies.""" - match session_factory: - case None: - session_factory = sessionmaker(bind=db.engine, expire_on_commit=False) - case Engine(): - session_factory = sessionmaker(bind=session_factory, expire_on_commit=False) - - self._session_factory = session_factory - self._node_execution_service_repo = DifyAPIRepositoryFactory.create_api_workflow_node_execution_repository( - self._session_factory - ) - self._workflow_run_repo = DifyAPIRepositoryFactory.create_api_workflow_run_repository(self._session_factory) + def __init__( + self, + *, + workflow_runs: DifyAPISQLAlchemyWorkflowRunRepository, + node_executions: DifyAPIWorkflowNodeExecutionRepository, + ) -> None: + self._workflow_runs = workflow_runs + self._node_executions = node_executions def get_paginate_advanced_chat_workflow_runs( self, - app_model: App, + context: RequestContext, + *, + app_id: str, args: WorkflowRunListArgs, triggered_from: WorkflowRunTriggeredFrom = WorkflowRunTriggeredFrom.DEBUGGING, ) -> InfiniteScrollPagination: """ Get advanced chat app workflow run list - :param app_model: app model + :param context: admitted Console request context + :param app_id: app id :param args: request args :param triggered_from: workflow run triggered from (default: DEBUGGING for preview runs) """ @@ -73,35 +79,29 @@ class WorkflowRunService: def __getattr__(self, item): return getattr(self._workflow_run, item) - pagination = self.get_paginate_workflow_runs(app_model, args, triggered_from) + pagination = self.get_paginate_workflow_runs( + context, + app_id=app_id, + args=args, + triggered_from=triggered_from, + ) # Batch-load the associated Message for every run in a single query to avoid # an N+1 pattern: the deprecated WorkflowRun.message property issues one query # per run. The filter matches that property exactly (app_id + workflow_run_id). workflow_runs = pagination.data run_ids = [workflow_run.id for workflow_run in workflow_runs] - messages_by_run_id: dict[str, Message] = {} - if run_ids: - with self._session_factory() as session: - messages = session.scalars( - select(Message).where( - Message.app_id == app_model.id, - Message.workflow_run_id.in_(run_ids), - ) - ).all() - for loaded_message in messages: - run_id = loaded_message.workflow_run_id - if run_id is None: - continue - # setdefault mirrors scalar()'s single-row-per-run semantics. - messages_by_run_id.setdefault(run_id, loaded_message) + messages_by_run_id = self._workflow_runs.get_message_refs( + app_id=app_id, + workflow_run_ids=run_ids, + ) with_message_workflow_runs = [] for workflow_run in workflow_runs: message = messages_by_run_id.get(workflow_run.id) with_message_workflow_run = WorkflowWithMessage(workflow_run=workflow_run) if message: - with_message_workflow_run.message_id = message.id + with_message_workflow_run.message_id = message.message_id with_message_workflow_run.conversation_id = message.conversation_id with_message_workflow_runs.append(with_message_workflow_run) @@ -111,14 +111,17 @@ class WorkflowRunService: def get_paginate_workflow_runs( self, - app_model: App, + context: RequestContext, + *, + app_id: str, args: WorkflowRunListArgs, triggered_from: WorkflowRunTriggeredFrom = WorkflowRunTriggeredFrom.DEBUGGING, ) -> InfiniteScrollPagination: """ Get workflow run list - :param app_model: app model + :param context: admitted Console request context + :param app_id: app id :param args: request args :param triggered_from: workflow run triggered from (default: DEBUGGING) """ @@ -126,31 +129,34 @@ class WorkflowRunService: last_id = args.get("last_id") status = args.get("status") - return self._workflow_run_repo.get_paginated_workflow_runs( - tenant_id=app_model.tenant_id, - app_id=app_model.id, + return self._workflow_runs.get_paginated_workflow_runs( + tenant_id=context.active_workspace_id, + app_id=app_id, triggered_from=triggered_from, limit=limit, last_id=last_id, status=status, ) - def get_workflow_run(self, app_model: App, run_id: str) -> WorkflowRun | None: + def get_workflow_run(self, context: RequestContext, *, app_id: str, run_id: str) -> WorkflowRun | None: """ Get workflow run detail - :param app_model: app model + :param context: admitted Console request context + :param app_id: app id :param run_id: workflow run id """ - return self._workflow_run_repo.get_workflow_run_by_id( - tenant_id=app_model.tenant_id, - app_id=app_model.id, + return self._workflow_runs.get_workflow_run_by_id( + tenant_id=context.active_workspace_id, + app_id=app_id, run_id=run_id, ) def get_workflow_runs_count( self, - app_model: App, + context: RequestContext, + *, + app_id: str, status: str | None = None, time_range: str | None = None, triggered_from: WorkflowRunTriggeredFrom = WorkflowRunTriggeredFrom.DEBUGGING, @@ -158,15 +164,16 @@ class WorkflowRunService: """ Get workflow runs count statistics - :param app_model: app model + :param context: admitted Console request context + :param app_id: app id :param status: optional status filter :param time_range: optional time range filter (e.g., "7d", "4h", "30m", "30s") :param triggered_from: workflow run triggered from (default: DEBUGGING) :return: dict with total and status counts """ - return self._workflow_run_repo.get_workflow_runs_count( - tenant_id=app_model.tenant_id, - app_id=app_model.id, + return self._workflow_runs.get_workflow_runs_count( + tenant_id=context.active_workspace_id, + app_id=app_id, triggered_from=triggered_from, status=status, time_range=time_range, @@ -174,14 +181,15 @@ class WorkflowRunService: def get_workflow_run_node_executions( self, - app_model: App, + context: RequestContext, + *, + app_id: str, run_id: str, - user: Account | EndUser, ) -> list[WorkflowNodeExecutionTrace]: """ Get workflow run node execution list """ - workflow_run = self.get_workflow_run(app_model, run_id) + workflow_run = self.get_workflow_run(context, app_id=app_id, run_id=run_id) contexts.plugin_tool_providers.set({}) contexts.plugin_tool_providers_lock.set(threading.Lock()) @@ -189,14 +197,43 @@ class WorkflowRunService: if not workflow_run: return [] - # Get tenant_id from user - tenant_id = user.tenant_id if isinstance(user, EndUser) else user.current_tenant_id - if tenant_id is None: - raise ValueError("User tenant_id cannot be None") - - node_executions = self._node_execution_service_repo.get_executions_by_workflow_run( - tenant_id=tenant_id, - app_id=app_model.id, + node_executions = self._node_executions.get_executions_by_workflow_run( + tenant_id=context.active_workspace_id, + app_id=app_id, workflow_run_id=run_id, ) - return assemble_workflow_node_execution_traces(node_executions, self._node_execution_service_repo) + return assemble_workflow_node_execution_traces(node_executions, self._node_executions) + + def get_pause_details( + self, + context: RequestContext, + *, + workflow_run_id: str, + ) -> WorkflowRunPauseDetails | None: + pause_record = self._workflow_runs.get_pause_record( + workspace_id=context.active_workspace_id, + workflow_run_id=workflow_run_id, + ) + if pause_record is None: + return None + if pause_record.status != WorkflowExecutionStatus.PAUSED: + return WorkflowRunPauseDetails(paused_at=None, paused_nodes=()) + + human_input_reasons: list[HumanInputRequired] = [] + for reason in pause_record.reasons: + if not isinstance(reason, HumanInputRequired): + raise NotImplementedError(f"Pause details do not support {type(reason).__name__}") + human_input_reasons.append(reason) + + return WorkflowRunPauseDetails( + paused_at=pause_record.paused_at, + paused_nodes=tuple( + WorkflowRunPausedNode( + node_id=reason.node_id, + node_title=reason.node_title, + form_id=reason.form_id, + form_token=pause_record.form_tokens.get(reason.form_id), + ) + for reason in human_input_reasons + ), + ) diff --git a/api/tests/test_containers_integration_tests/.ruff.toml b/api/tests/test_containers_integration_tests/.ruff.toml index a58180c68b1..4124d24b082 100644 --- a/api/tests/test_containers_integration_tests/.ruff.toml +++ b/api/tests/test_containers_integration_tests/.ruff.toml @@ -41,7 +41,6 @@ extend-select = ["ANN401", "ARG"] "services/test_metadata_service.py" = ["ARG002"] "services/test_model_load_balancing_service.py" = ["ARG002"] "services/test_model_provider_service.py" = ["ARG002"] -"services/test_ops_service.py" = ["ARG002"] "services/test_webapp_auth_service.py" = ["ARG002"] "services/test_webhook_service.py" = ["ARG002"] "services/test_workflow_draft_variable_service.py" = ["ARG002"] diff --git a/api/tests/test_containers_integration_tests/controllers/openapi/conftest.py b/api/tests/test_containers_integration_tests/controllers/openapi/conftest.py index d06da5bc018..2047ee765d1 100644 --- a/api/tests/test_containers_integration_tests/controllers/openapi/conftest.py +++ b/api/tests/test_containers_integration_tests/controllers/openapi/conftest.py @@ -1,8 +1,7 @@ from __future__ import annotations import uuid -from collections.abc import Callable, Generator -from contextlib import contextmanager +from collections.abc import Callable from typing import Literal from unittest.mock import patch @@ -12,7 +11,8 @@ from flask import Flask from sqlalchemy.orm import Session from controllers.openapi.auth.data import AuthData -from libs.oauth_bearer import AuthContext, Scope, SubjectType, TokenType, reset_auth_ctx, set_auth_ctx +from libs.oauth_bearer import Scope, TokenType +from machinery.context import AccountRequestContext from models import Account, Tenant from services.account_service import AccountService, TenantService from tests.test_containers_integration_tests.helpers import generate_valid_password @@ -91,34 +91,14 @@ def auth_for( ) -@contextmanager -def account_auth_context( +def request_context_for( account: Account, *, - token_id: uuid.UUID, - client_id: str = "integration-cli", -) -> Generator[AuthContext]: - """Publish an account ``AuthContext`` for handlers that read ``get_auth_ctx()``. - - The auth pipeline normally sets this ContextVar; the integration suite - bypasses the pipeline via ``inspect.unwrap``, so endpoints that resolve the - caller through ``get_auth_ctx()`` (the ``/account/sessions*`` family) need it - set explicitly. Resets on exit so the worker thread can't leak identity. - """ - ctx = AuthContext( - subject_type=SubjectType.ACCOUNT, - subject_email=account.email, - subject_issuer=None, - account_id=uuid.UUID(str(account.id)), - client_id=client_id, - scopes=frozenset({Scope.FULL}), - token_id=token_id, - token_type=TokenType.OAUTH_ACCOUNT, - expires_at=None, - token_hash="integration-test", + token_id: uuid.UUID | None = None, +) -> AccountRequestContext: + return AccountRequestContext( + request_id="integration-request", + trace_id="integration-trace", + account_id=str(account.id), + access_token_id=str(token_id) if token_id is not None else None, ) - reset_token = set_auth_ctx(ctx) - try: - yield ctx - finally: - reset_auth_ctx(reset_token) diff --git a/api/tests/test_containers_integration_tests/controllers/openapi/test_account.py b/api/tests/test_containers_integration_tests/controllers/openapi/test_account.py index 0a04c4fdad0..d2181b8398d 100644 --- a/api/tests/test_containers_integration_tests/controllers/openapi/test_account.py +++ b/api/tests/test_containers_integration_tests/controllers/openapi/test_account.py @@ -9,20 +9,21 @@ from sqlalchemy.orm import Session from controllers.openapi.account import AccountApi from models import Account from models.account import TenantAccountRole -from tests.test_containers_integration_tests.controllers.openapi.conftest import add_tenant_for_account, auth_for +from tests.test_containers_integration_tests.controllers.openapi.conftest import ( + add_tenant_for_account, + request_context_for, +) class TestAccountInfo: - def test_returns_account_and_owner_workspace( - self, app: Flask, db_session_with_containers: Session, make_account: Callable[..., Account] - ) -> None: + def test_returns_account_and_owner_workspace(self, app: Flask, make_account: Callable[..., Account]) -> None: account = make_account() owner_tenant = account.current_tenant assert owner_tenant is not None api = AccountApi() with app.test_request_context("/openapi/v1/account"): - result = unwrap(api.get)(api, db_session_with_containers, auth_data=auth_for(account)) + result = unwrap(api.get)(api, request_context_for(account)) assert result.subject_type == "account" assert result.subject_email == account.email @@ -47,7 +48,7 @@ class TestAccountInfo: api = AccountApi() with app.test_request_context("/openapi/v1/account"): - result = unwrap(api.get)(api, db_session_with_containers, auth_data=auth_for(account)) + result = unwrap(api.get)(api, request_context_for(account)) assert {w.id for w in result.workspaces} == {owner_tenant.id, second.id} roles = {w.id: w.role for w in result.workspaces} diff --git a/api/tests/test_containers_integration_tests/controllers/openapi/test_account_sessions.py b/api/tests/test_containers_integration_tests/controllers/openapi/test_account_sessions.py index 9e6acab5543..d2e339c565e 100644 --- a/api/tests/test_containers_integration_tests/controllers/openapi/test_account_sessions.py +++ b/api/tests/test_containers_integration_tests/controllers/openapi/test_account_sessions.py @@ -18,7 +18,7 @@ from controllers.openapi.account import ( from extensions.ext_redis import redis_client from models import Account from services.oauth_device_flow import PREFIX_OAUTH_ACCOUNT, MintResult, mint_oauth_token -from tests.test_containers_integration_tests.controllers.openapi.conftest import account_auth_context, auth_for +from tests.test_containers_integration_tests.controllers.openapi.conftest import request_context_for def _mint_account_token( @@ -51,13 +51,11 @@ class TestSessionList: api = AccountSessionsApi() with app.test_request_context("/openapi/v1/account/sessions"): - with account_auth_context(account, token_id=mint.token_id): - result = unwrap(api.get)( - api, - db_session_with_containers, - auth_data=auth_for(account, token_id=mint.token_id), - query=SessionListQuery(), - ) + result = unwrap(api.get)( + api, + request_context_for(account, token_id=mint.token_id), + query=SessionListQuery(), + ) assert result.total == 1 row = result.data[0] @@ -76,13 +74,11 @@ class TestSessionList: api = AccountSessionsApi() with app.test_request_context("/openapi/v1/account/sessions"): - with account_auth_context(account, token_id=mine.token_id): - result = unwrap(api.get)( - api, - db_session_with_containers, - auth_data=auth_for(account, token_id=mine.token_id), - query=SessionListQuery(), - ) + result = unwrap(api.get)( + api, + request_context_for(account, token_id=mine.token_id), + query=SessionListQuery(), + ) assert {row.id for row in result.data} == {str(mine.token_id)} @@ -96,23 +92,18 @@ class TestSessionRevoke: revoke_api = AccountSessionsSelfApi() with app.test_request_context("/openapi/v1/account/sessions/self", method="DELETE"): - with account_auth_context(account, token_id=mint.token_id): - result = unwrap(revoke_api.delete)( - revoke_api, db_session_with_containers, auth_data=auth_for(account, token_id=mint.token_id) - ) + result = unwrap(revoke_api.delete)(revoke_api, request_context_for(account, token_id=mint.token_id)) assert result.status == "revoked" # Revocation persisted: the real list path no longer returns it. list_api = AccountSessionsApi() with app.test_request_context("/openapi/v1/account/sessions"): - with account_auth_context(account, token_id=mint.token_id): - listing = unwrap(list_api.get)( - list_api, - db_session_with_containers, - auth_data=auth_for(account, token_id=mint.token_id), - query=SessionListQuery(), - ) + listing = unwrap(list_api.get)( + list_api, + request_context_for(account, token_id=mint.token_id), + query=SessionListQuery(), + ) assert listing.total == 0 def test_revoke_by_id_for_own_session( @@ -124,13 +115,11 @@ class TestSessionRevoke: api = AccountSessionByIdApi() with app.test_request_context(f"/openapi/v1/account/sessions/{session_id}", method="DELETE"): - with account_auth_context(account, token_id=mint.token_id): - result = unwrap(api.delete)( - api, - db_session_with_containers, - session_id=session_id, - auth_data=auth_for(account, token_id=mint.token_id), - ) + result = unwrap(api.delete)( + api, + request_context_for(account, token_id=mint.token_id), + session_id=session_id, + ) assert result.status == "revoked" @@ -146,11 +135,9 @@ class TestSessionRevoke: api = AccountSessionByIdApi() session_id = str(foreign.token_id) with app.test_request_context(f"/openapi/v1/account/sessions/{session_id}", method="DELETE"): - with account_auth_context(outsider, token_id=uuid4()): - with pytest.raises(NotFound): - unwrap(api.delete)( - api, - db_session_with_containers, - session_id=session_id, - auth_data=auth_for(outsider, token_id=uuid4()), - ) + with pytest.raises(NotFound): + unwrap(api.delete)( + api, + request_context_for(outsider, token_id=uuid4()), + session_id=session_id, + ) diff --git a/api/tests/test_containers_integration_tests/controllers/web/test_human_input_form.py b/api/tests/test_containers_integration_tests/controllers/web/test_human_input_form.py index 363197e6c74..d80e3fe31ff 100644 --- a/api/tests/test_containers_integration_tests/controllers/web/test_human_input_form.py +++ b/api/tests/test_containers_integration_tests/controllers/web/test_human_input_form.py @@ -2,7 +2,6 @@ from __future__ import annotations import json from datetime import UTC, datetime, timedelta -from typing import override from uuid import uuid4 import pytest @@ -22,7 +21,6 @@ from core.workflow.nodes.human_input.entities import ( ) from core.workflow.nodes.human_input.enums import HumanInputFormKind, HumanInputFormStatus, ValueSourceType from core.workflow.nodes.human_input.pause_reason import HumanInputRequired -from graphon.entities import WorkflowExecution from graphon.enums import WorkflowExecutionStatus from graphon.runtime import GraphRuntimeState, VariablePool from models.account import Account, Tenant, TenantAccountJoin, TenantAccountRole @@ -40,14 +38,6 @@ from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchem from services.entities.feature_entities import FeatureModel -class _TestWorkflowRunRepository(DifyAPISQLAlchemyWorkflowRunRepository): - """Concrete repository for tests where save() is not under test.""" - - @override - def save(self, execution: WorkflowExecution) -> None: - return None - - def _create_app_with_site(session: Session) -> tuple[App, Account]: tenant = Tenant(name="Test Tenant") account = Account(name="Tester", email=f"tester-{uuid4()}@example.com") @@ -218,7 +208,9 @@ def test_get_human_input_form_resolves_runtime_select_options( ) engine = db_session_with_containers.get_bind() assert isinstance(engine, Engine) - workflow_run_repo = _TestWorkflowRunRepository(session_maker=sessionmaker(bind=engine, expire_on_commit=False)) + workflow_run_repo = DifyAPISQLAlchemyWorkflowRunRepository( + session_maker=sessionmaker(bind=engine, expire_on_commit=False) + ) workflow_run_repo.create_workflow_pause( workflow_run_id=workflow_run.id, state_owner_user_id=account.id, diff --git a/api/tests/test_containers_integration_tests/controllers/web/test_wraps.py b/api/tests/test_containers_integration_tests/controllers/web/test_wraps.py index 9a143ab3bc3..5bda3738af5 100644 --- a/api/tests/test_containers_integration_tests/controllers/web/test_wraps.py +++ b/api/tests/test_containers_integration_tests/controllers/web/test_wraps.py @@ -91,8 +91,8 @@ class TestValidateUserAccessibility: def test_missing_auth_type_raises(self) -> None: decoded = {"user_id": "u1", "granted_at": 1} - settings = SimpleNamespace(access_mode="public") - with pytest.raises(WebAppAuthAccessDeniedError, match="auth_type"): + settings = SimpleNamespace(access_mode="private") + with pytest.raises(WebAppAuthRequiredError, match="auth_type"): _validate_user_accessibility( decoded=decoded, app_code="code", @@ -103,7 +103,7 @@ class TestValidateUserAccessibility: def test_missing_granted_at_raises(self) -> None: decoded = {"user_id": "u1", "auth_type": "external"} - settings = SimpleNamespace(access_mode="public") + settings = SimpleNamespace(access_mode="sso_verified") with pytest.raises(WebAppAuthAccessDeniedError, match="granted_at"): _validate_user_accessibility( decoded=decoded, @@ -121,7 +121,7 @@ class TestValidateUserAccessibility: mock_sso_time.return_value = datetime.now(UTC) old_granted = int((datetime.now(UTC) - timedelta(hours=1)).timestamp()) decoded = {"user_id": "u1", "auth_type": "external", "granted_at": old_granted} - settings = SimpleNamespace(access_mode="public") + settings = SimpleNamespace(access_mode="sso_verified") with pytest.raises(WebAppAuthAccessDeniedError, match="SSO settings"): _validate_user_accessibility( decoded=decoded, @@ -139,7 +139,7 @@ class TestValidateUserAccessibility: mock_workspace_sso.return_value = datetime.now(UTC) old_granted = int((datetime.now(UTC) - timedelta(hours=1)).timestamp()) decoded = {"user_id": "u1", "auth_type": "internal", "granted_at": old_granted} - settings = SimpleNamespace(access_mode="public") + settings = SimpleNamespace(access_mode="private") with pytest.raises(WebAppAuthAccessDeniedError, match="SSO settings"): _validate_user_accessibility( decoded=decoded, @@ -157,7 +157,7 @@ class TestValidateUserAccessibility: mock_sso_time.return_value = datetime.now(UTC) - timedelta(hours=2) recent_granted = int(datetime.now(UTC).timestamp()) decoded = {"user_id": "u1", "auth_type": "external", "granted_at": recent_granted} - settings = SimpleNamespace(access_mode="public") + settings = SimpleNamespace(access_mode="sso_verified") _validate_user_accessibility( decoded=decoded, app_code="code", @@ -172,8 +172,8 @@ class TestValidateUserAccessibility: def test_permission_check_denies_unauthorized_user( self, mock_perm: MagicMock, mock_app_id: MagicMock, mock_allowed: MagicMock ) -> None: - decoded = {"user_id": "u1", "auth_type": "external", "granted_at": int(datetime.now(UTC).timestamp())} - settings = SimpleNamespace(access_mode="internal") + decoded = {"user_id": "u1", "auth_type": "internal", "granted_at": int(datetime.now(UTC).timestamp())} + settings = SimpleNamespace(access_mode="private") with pytest.raises(WebAppAuthAccessDeniedError): _validate_user_accessibility( decoded=decoded, @@ -183,6 +183,37 @@ class TestValidateUserAccessibility: webapp_settings=settings, ) + @pytest.mark.parametrize( + ("access_mode", "auth_type"), + [ + pytest.param("private", "external", id="private-rejects-external"), + pytest.param("private_all", "external", id="private-all-rejects-external"), + pytest.param("sso_verified", "internal", id="sso-verified-rejects-internal"), + ], + ) + @patch("controllers.web.wraps.EnterpriseService.WebAppAuth.is_user_allowed_to_access_webapp") + @patch("controllers.web.wraps.WebAppAuthService.is_app_require_permission_check") + def test_auth_type_must_match_current_access_mode( + self, + mock_permission_check: MagicMock, + mock_allowed: MagicMock, + access_mode: str, + auth_type: str, + ) -> None: + decoded = {"user_id": "u1", "auth_type": auth_type, "granted_at": int(datetime.now(UTC).timestamp())} + + with pytest.raises(WebAppAuthRequiredError): + _validate_user_accessibility( + decoded=decoded, + app_code="code", + app_web_auth_enabled=True, + system_webapp_auth_enabled=True, + webapp_settings=SimpleNamespace(access_mode=access_mode), + ) + + mock_permission_check.assert_not_called() + mock_allowed.assert_not_called() + class TestDecodeJwtToken: @pytest.fixture diff --git a/api/tests/test_containers_integration_tests/core/app/layers/test_pause_state_persist_layer.py b/api/tests/test_containers_integration_tests/core/app/layers/test_pause_state_persist_layer.py index 2d5e3ebcca9..56da1c948d1 100644 --- a/api/tests/test_containers_integration_tests/core/app/layers/test_pause_state_persist_layer.py +++ b/api/tests/test_containers_integration_tests/core/app/layers/test_pause_state_persist_layer.py @@ -23,7 +23,7 @@ from unittest.mock import Mock import pytest from sqlalchemy import Engine, delete, select -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, sessionmaker from core.app.app_config.entities import WorkflowUIBasedAppConfig from core.app.entities.app_invoke_entities import InvokeFrom, WorkflowAppGenerateEntity @@ -46,6 +46,8 @@ from models import Account from models import WorkflowPause as WorkflowPauseModel from models.model import AppMode, UploadFile from models.workflow import Workflow, WorkflowRun +from repositories.factory import DifyAPIRepositoryFactory +from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository from services.file_service import FileService from services.workflow_run_service import WorkflowRunService @@ -99,7 +101,14 @@ class TestPauseStatePersistenceLayerTestContainers: @pytest.fixture def workflow_run_service(self, engine: Engine, file_service: FileService): """Create WorkflowRunService instance with TestContainers engine and FileService.""" - return WorkflowRunService(engine) + session_factory = sessionmaker(bind=engine, expire_on_commit=False) + workflow_runs = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=session_factory) + return WorkflowRunService( + workflow_runs=workflow_runs, + node_executions=DifyAPIRepositoryFactory.create_api_workflow_node_execution_repository( + session_maker=session_factory + ), + ) @pytest.fixture(autouse=True) def setup_test_data(self, db_session_with_containers: Session, file_service, workflow_run_service): @@ -403,7 +412,7 @@ class TestPauseStatePersistenceLayerTestContainers: layer.on_event(event) # Assert - Retrieve and verify - pause_entity = self.workflow_run_service._workflow_run_repo.get_workflow_pause(self.test_workflow_run_id) + pause_entity = self.workflow_run_service._workflow_runs.get_workflow_pause(self.test_workflow_run_id) assert pause_entity is not None assert pause_entity.workflow_execution_id == self.test_workflow_run_id assert pause_entity.get_pause_reasons() == event.reasons @@ -542,7 +551,7 @@ class TestPauseStatePersistenceLayerTestContainers: assert pause_model is not None # Verify the state owner is the workflow creator - pause_entity = self.workflow_run_service._workflow_run_repo.get_workflow_pause(different_workflow_run.id) + pause_entity = self.workflow_run_service._workflow_runs.get_workflow_pause(different_workflow_run.id) assert pause_entity is not None resumption_context = WorkflowResumptionContext.loads(pause_entity.get_state().decode()) assert resumption_context.get_generate_entity().workflow_execution_id == different_workflow_run.id diff --git a/api/tests/test_containers_integration_tests/pyrefly.toml b/api/tests/test_containers_integration_tests/pyrefly.toml index cd654925428..bf1701447c7 100644 --- a/api/tests/test_containers_integration_tests/pyrefly.toml +++ b/api/tests/test_containers_integration_tests/pyrefly.toml @@ -80,7 +80,6 @@ project-excludes = [ "services/test_messages_clean_service.py", "services/test_model_load_balancing_service.py", "services/test_model_provider_service.py", - "services/test_ops_service.py", "services/test_restore_archived_workflow_run.py", "services/test_saved_message_service.py", "services/test_schedule_service.py", diff --git a/api/tests/test_containers_integration_tests/repositories/test_sqlalchemy_workflow_run_cleanup_repository.py b/api/tests/test_containers_integration_tests/repositories/test_sqlalchemy_workflow_run_cleanup_repository.py index be659fac184..7fc557f9b11 100644 --- a/api/tests/test_containers_integration_tests/repositories/test_sqlalchemy_workflow_run_cleanup_repository.py +++ b/api/tests/test_containers_integration_tests/repositories/test_sqlalchemy_workflow_run_cleanup_repository.py @@ -4,13 +4,11 @@ from __future__ import annotations from dataclasses import dataclass, field from datetime import datetime, timedelta -from typing import override from uuid import uuid4 from sqlalchemy import Engine, select from sqlalchemy.orm import Session, sessionmaker -from graphon.entities import WorkflowExecution from graphon.entities.pause_reason import PauseReasonType from graphon.enums import WorkflowExecutionStatus, WorkflowType from models.enums import CreatorUserRole, WorkflowRunTriggeredFrom @@ -18,14 +16,6 @@ from models.workflow import WorkflowAppLog, WorkflowAppLogCreatedFrom, WorkflowP from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository -class _TestWorkflowRunRepository(DifyAPISQLAlchemyWorkflowRunRepository): - """Concrete repository for tests where save() is not under test.""" - - @override - def save(self, execution: WorkflowExecution) -> None: - return None - - @dataclass class _TestScope: """Per-test identifiers for rows created by cleanup repository tests.""" @@ -39,7 +29,7 @@ class _TestScope: def _repository(db_session_with_containers: Session) -> DifyAPISQLAlchemyWorkflowRunRepository: engine = db_session_with_containers.get_bind() assert isinstance(engine, Engine) - return _TestWorkflowRunRepository(session_maker=sessionmaker(bind=engine, expire_on_commit=False)) + return DifyAPISQLAlchemyWorkflowRunRepository(session_maker=sessionmaker(bind=engine, expire_on_commit=False)) def _create_workflow_run( diff --git a/api/tests/test_containers_integration_tests/services/test_ops_service.py b/api/tests/test_containers_integration_tests/services/test_ops_service.py deleted file mode 100644 index 029c6ba9420..00000000000 --- a/api/tests/test_containers_integration_tests/services/test_ops_service.py +++ /dev/null @@ -1,385 +0,0 @@ -from __future__ import annotations - -import uuid -from unittest.mock import patch - -import pytest -from faker import Faker -from sqlalchemy import select -from sqlalchemy.orm import Session - -from core.ops.entities.config_entity import TracingProviderEnum -from models.model import TraceAppConfig -from services.account_service import AccountService, TenantService -from services.app_service import AppService, CreateAppParams -from services.ops_service import OpsService -from tests.test_containers_integration_tests.helpers import generate_valid_password - - -class TestOpsService: - @pytest.fixture - def mock_external_service_dependencies(self): - with ( - patch("services.app_service.SystemFeatureService") as mock_feature_service, - patch("services.app_service.EnterpriseService") as mock_enterprise_service, - patch("services.app_service.ModelManager.for_tenant") as mock_model_manager, - patch("services.account_service.SystemFeatureService") as mock_account_feature_service, - ): - mock_feature_service.is_webapp_auth_enabled.return_value = False - mock_enterprise_service.WebAppAuth.update_app_access_mode.return_value = None - mock_enterprise_service.WebAppAuth.cleanup_webapp.return_value = None - mock_account_feature_service.is_registration_allowed.return_value = True - mock_model_instance = mock_model_manager.return_value - mock_model_instance.get_default_model_instance.return_value = None - mock_model_instance.get_default_provider_model_name.return_value = ("openai", "gpt-3.5-turbo") - yield { - "feature_service": mock_feature_service, - "enterprise_service": mock_enterprise_service, - "model_manager": mock_model_manager, - "account_feature_service": mock_account_feature_service, - } - - @pytest.fixture - def mock_ops_trace_manager(self): - with patch("services.ops_service.OpsTraceManager") as mock: - yield mock - - def _create_app(self, db_session_with_containers: Session, mock_external_service_dependencies): - fake = Faker() - account = AccountService.create_account( - email=fake.email(), - name=fake.name(), - interface_language="en-US", - password=generate_valid_password(fake), - session=db_session_with_containers, - ) - TenantService.create_owner_tenant_if_not_exist(account, name=fake.company(), session=db_session_with_containers) - tenant = account.current_tenant - app_service = AppService() - app = app_service.create_app( - tenant.id, - CreateAppParams( - name=fake.company(), - description=fake.text(max_nb_chars=100), - mode="chat", - icon_type="emoji", - icon="🤖", - icon_background="#FF6B6B", - ), - account, - session=db_session_with_containers, - ) - return app, account - - _SENTINEL = object() - - def _insert_trace_config( - self, - db_session: Session, - app_id: str, - provider: str, - tracing_config: dict | None | object = _SENTINEL, - ) -> TraceAppConfig: - trace_config = TraceAppConfig( - app_id=app_id, - tracing_provider=provider, - tracing_config=tracing_config if tracing_config is not self._SENTINEL else {"some": "config"}, - ) - db_session.add(trace_config) - db_session.commit() - return trace_config - - # ── get_tracing_app_config ───────────────────────────────────────── - - def test_get_tracing_app_config_no_config(self, db_session_with_containers: Session, mock_ops_trace_manager): - result = OpsService.get_tracing_app_config(str(uuid.uuid4()), "arize", db_session_with_containers) - assert result is None - - def test_get_tracing_app_config_no_app(self, db_session_with_containers: Session, mock_ops_trace_manager): - fake_app_id = str(uuid.uuid4()) - self._insert_trace_config(db_session_with_containers, fake_app_id, "arize") - result = OpsService.get_tracing_app_config(fake_app_id, "arize", db_session_with_containers) - assert result is None - - def test_get_tracing_app_config_none_config( - self, db_session_with_containers: Session, mock_external_service_dependencies, mock_ops_trace_manager - ): - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, "arize", tracing_config=None) - - with pytest.raises(ValueError, match="Tracing config cannot be None."): - OpsService.get_tracing_app_config(app.id, "arize", db_session_with_containers) - - @pytest.mark.parametrize( - ("provider", "default_url"), - [ - ("arize", "https://app.arize.com/"), - ("phoenix", "https://app.phoenix.arize.com/projects/"), - ("langsmith", "https://smith.langchain.com/"), - ("opik", "https://www.comet.com/opik/"), - ("weave", "https://wandb.ai/"), - ("aliyun", "https://arms.console.aliyun.com/"), - ("tencent", "https://console.cloud.tencent.com/apm"), - ("mlflow", "http://localhost:5000/"), - ("databricks", "https://www.databricks.com/"), - ], - ) - def test_get_tracing_app_config_providers_exception( - self, db_session_with_containers: Session, mock_external_service_dependencies, provider, default_url - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.decrypt_tracing_config.return_value = {} - mock_otm.obfuscated_decrypt_token.return_value = {} - mock_otm.get_trace_config_project_url.side_effect = Exception("error") - mock_otm.get_trace_config_project_key.side_effect = Exception("error") - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, provider) - - result = OpsService.get_tracing_app_config(app.id, provider, db_session_with_containers) - - assert result is not None - assert result["tracing_config"]["project_url"] == default_url - - @pytest.mark.parametrize( - "provider", - ["arize", "phoenix", "langsmith", "opik", "weave", "aliyun", "tencent", "mlflow", "databricks"], - ) - def test_get_tracing_app_config_providers_success( - self, db_session_with_containers: Session, mock_external_service_dependencies, provider - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.decrypt_tracing_config.return_value = {} - mock_otm.obfuscated_decrypt_token.return_value = {"project_url": "success_url"} - mock_otm.get_trace_config_project_url.return_value = "success_url" - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, provider) - - result = OpsService.get_tracing_app_config(app.id, provider, db_session_with_containers) - - assert result is not None - assert result["tracing_config"]["project_url"] == "success_url" - - def test_get_tracing_app_config_langfuse_success( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.decrypt_tracing_config.return_value = {"host": "https://api.langfuse.com"} - mock_otm.obfuscated_decrypt_token.return_value = {"host": "https://api.langfuse.com"} - mock_otm.get_trace_config_project_key.return_value = "key" - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, "langfuse") - - result = OpsService.get_tracing_app_config(app.id, "langfuse", db_session_with_containers) - - assert result is not None - assert result["tracing_config"]["project_url"] == "https://api.langfuse.com/project/key" - - def test_get_tracing_app_config_langfuse_exception( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.decrypt_tracing_config.return_value = {"host": "https://api.langfuse.com"} - mock_otm.obfuscated_decrypt_token.return_value = {"host": "https://api.langfuse.com"} - mock_otm.get_trace_config_project_key.side_effect = Exception("error") - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, "langfuse") - - result = OpsService.get_tracing_app_config(app.id, "langfuse", db_session_with_containers) - - assert result is not None - assert result["tracing_config"]["project_url"] == "https://api.langfuse.com/" - - # ── create_tracing_app_config ────────────────────────────────────── - - def test_create_tracing_app_config_invalid_provider(self, db_session_with_containers: Session): - result = OpsService.create_tracing_app_config( - str(uuid.uuid4()), "invalid_provider", {}, db_session_with_containers - ) - assert result == {"error": "Invalid tracing provider: invalid_provider"} - - def test_create_tracing_app_config_invalid_credentials( - self, db_session_with_containers: Session, mock_ops_trace_manager - ): - mock_ops_trace_manager.check_trace_config_is_effective.return_value = False - result = OpsService.create_tracing_app_config( - str(uuid.uuid4()), - TracingProviderEnum.LANGFUSE, - {"public_key": "p", "secret_key": "s"}, - db_session_with_containers, - ) - assert result == {"error": "Invalid Credentials"} - - @pytest.mark.parametrize( - ("provider", "config"), - [ - (TracingProviderEnum.ARIZE, {}), - (TracingProviderEnum.LANGFUSE, {"public_key": "p", "secret_key": "s"}), - (TracingProviderEnum.LANGSMITH, {"api_key": "k", "project": "p"}), - (TracingProviderEnum.ALIYUN, {"license_key": "k", "endpoint": "https://aliyun.com"}), - ], - ) - def test_create_tracing_app_config_project_url_exception( - self, db_session_with_containers: Session, mock_external_service_dependencies, provider, config - ): - # Existing config causes the service to return None before reaching the DB insert - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.check_trace_config_is_effective.return_value = True - mock_otm.get_trace_config_project_url.side_effect = Exception("error") - mock_otm.get_trace_config_project_key.side_effect = Exception("error") - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, str(provider)) - - result = OpsService.create_tracing_app_config(app.id, provider, config, db_session_with_containers) - - assert result is None - - def test_create_tracing_app_config_langfuse_success( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.check_trace_config_is_effective.return_value = True - mock_otm.get_trace_config_project_key.return_value = "key" - mock_otm.encrypt_tracing_config.return_value = {} - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - result = OpsService.create_tracing_app_config( - app.id, - TracingProviderEnum.LANGFUSE, - {"public_key": "p", "secret_key": "s", "host": "https://api.langfuse.com"}, - db_session_with_containers, - ) - - assert result == {"result": "success"} - - def test_create_tracing_app_config_already_exists( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.check_trace_config_is_effective.return_value = True - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, str(TracingProviderEnum.ARIZE)) - - result = OpsService.create_tracing_app_config( - app.id, TracingProviderEnum.ARIZE, {}, db_session_with_containers - ) - - assert result is None - - def test_create_tracing_app_config_no_app(self, db_session_with_containers: Session, mock_ops_trace_manager): - mock_ops_trace_manager.check_trace_config_is_effective.return_value = True - result = OpsService.create_tracing_app_config( - str(uuid.uuid4()), TracingProviderEnum.ARIZE, {}, db_session_with_containers - ) - assert result is None - - def test_create_tracing_app_config_with_empty_other_keys( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - # "project" is in other_keys for Arize; providing "" triggers default substitution - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.check_trace_config_is_effective.return_value = True - mock_otm.get_trace_config_project_url.side_effect = Exception("no url") - mock_otm.encrypt_tracing_config.return_value = {} - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - result = OpsService.create_tracing_app_config( - app.id, TracingProviderEnum.ARIZE, {"project": ""}, db_session_with_containers - ) - - assert result == {"result": "success"} - - def test_create_tracing_app_config_success( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.check_trace_config_is_effective.return_value = True - mock_otm.get_trace_config_project_url.return_value = "http://project_url" - mock_otm.encrypt_tracing_config.return_value = {"encrypted": "config"} - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - result = OpsService.create_tracing_app_config( - app.id, TracingProviderEnum.ARIZE, {}, db_session_with_containers - ) - - assert result == {"result": "success"} - - # ── update_tracing_app_config ────────────────────────────────────── - - def test_update_tracing_app_config_invalid_provider(self, db_session_with_containers: Session): - with pytest.raises(ValueError, match="Invalid tracing provider: invalid_provider"): - OpsService.update_tracing_app_config(str(uuid.uuid4()), "invalid_provider", {}, db_session_with_containers) - - def test_update_tracing_app_config_no_config(self, db_session_with_containers: Session, mock_ops_trace_manager): - result = OpsService.update_tracing_app_config( - str(uuid.uuid4()), TracingProviderEnum.ARIZE, {}, db_session_with_containers - ) - assert result is None - - def test_update_tracing_app_config_no_app(self, db_session_with_containers: Session, mock_ops_trace_manager): - fake_app_id = str(uuid.uuid4()) - self._insert_trace_config(db_session_with_containers, fake_app_id, str(TracingProviderEnum.ARIZE)) - mock_ops_trace_manager.encrypt_tracing_config.return_value = {} - result = OpsService.update_tracing_app_config( - fake_app_id, TracingProviderEnum.ARIZE, {}, db_session_with_containers - ) - assert result is None - - def test_update_tracing_app_config_invalid_credentials( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.encrypt_tracing_config.return_value = {} - mock_otm.decrypt_tracing_config.return_value = {} - mock_otm.check_trace_config_is_effective.return_value = False - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, str(TracingProviderEnum.ARIZE)) - - with pytest.raises(ValueError, match="Invalid Credentials"): - OpsService.update_tracing_app_config(app.id, TracingProviderEnum.ARIZE, {}, db_session_with_containers) - - def test_update_tracing_app_config_success( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - with patch("services.ops_service.OpsTraceManager") as mock_otm: - mock_otm.encrypt_tracing_config.return_value = {"updated": "config"} - mock_otm.decrypt_tracing_config.return_value = {} - mock_otm.check_trace_config_is_effective.return_value = True - - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, str(TracingProviderEnum.ARIZE)) - - result = OpsService.update_tracing_app_config( - app.id, TracingProviderEnum.ARIZE, {}, db_session_with_containers - ) - - assert result is not None - assert result["app_id"] == app.id - - # ── delete_tracing_app_config ────────────────────────────────────── - - def test_delete_tracing_app_config_no_config(self, db_session_with_containers: Session): - result = OpsService.delete_tracing_app_config(str(uuid.uuid4()), "arize", db_session_with_containers) - assert result is None - - def test_delete_tracing_app_config_success( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - app, _ = self._create_app(db_session_with_containers, mock_external_service_dependencies) - self._insert_trace_config(db_session_with_containers, app.id, "arize") - - result = OpsService.delete_tracing_app_config(app.id, "arize", db_session_with_containers) - - assert result is True - remaining = db_session_with_containers.scalar( - select(TraceAppConfig) - .where(TraceAppConfig.app_id == app.id, TraceAppConfig.tracing_provider == "arize") - .limit(1) - ) - assert remaining is None diff --git a/api/tests/test_containers_integration_tests/services/test_workflow_app_service.py b/api/tests/test_containers_integration_tests/services/test_workflow_app_service.py index 617c27f41a6..e31cf7d80ad 100644 --- a/api/tests/test_containers_integration_tests/services/test_workflow_app_service.py +++ b/api/tests/test_containers_integration_tests/services/test_workflow_app_service.py @@ -7,22 +7,48 @@ from unittest.mock import patch import pytest from faker import Faker -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, sessionmaker from graphon.enums import WorkflowExecutionStatus from models import EndUser, Workflow, WorkflowAppLog, WorkflowRun from models.enums import CreatorUserRole, EndUserType from models.workflow import WorkflowAppLogCreatedFrom +from repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepository from services.account_service import AccountService, TenantService - -# Delay import of AppService to avoid circular dependency -# from services.app_service import AppService, CreateAppParams -from services.workflow_app_service import WorkflowAppService +from services.workflow_app_log_query_service import WorkflowAppLogQueryService from tests.test_containers_integration_tests.helpers import generate_valid_password -class TestWorkflowAppService: - """Integration tests for WorkflowAppService using testcontainers.""" +class _WorkflowAppLogTestClient: + def __init__(self, session: Session) -> None: + session_factory = sessionmaker(bind=session.get_bind(), expire_on_commit=False) + self._service = WorkflowAppLogQueryService( + logs=WorkflowAppLogQueryRepository(session_factory=session_factory), + ) + + def get_paginate_workflow_app_logs(self, *, session: Session, app_model, **kwargs): + assert session.get_bind() is not None + result = self._service.list_logs( + tenant_id=app_model.tenant_id, + app_id=app_model.id, + **kwargs, + ) + return { + "page": result.page, + "limit": result.limit, + "total": result.total, + "has_more": result.has_more, + "data": list(result.data), + } + + +def _workflow_run(log): + assert log.workflow_run is not None + return log.workflow_run + + +class TestWorkflowAppLogQueryService: + """Integration tests for workflow app log queries using testcontainers.""" @pytest.fixture def mock_external_service_dependencies(self): @@ -247,7 +273,7 @@ class TestWorkflowAppService: ) # Act: Execute the method under test - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) result = service.get_paginate_workflow_app_logs( session=db_session_with_containers, app_model=app, page=1, limit=20 ) @@ -263,10 +289,8 @@ class TestWorkflowAppService: # Verify the returned data log_entry = result["data"][0] assert log_entry.id == workflow_app_log.id - assert log_entry.tenant_id == app.tenant_id - assert log_entry.app_id == app.id - assert log_entry.workflow_id == workflow.id - assert log_entry.workflow_run_id == workflow_run.id + returned_run = _workflow_run(log_entry) + assert returned_run.id == workflow_run.id # Verify database state @@ -293,7 +317,7 @@ class TestWorkflowAppService: db_session_with_containers.commit() # Act: Execute the method under test with keyword search - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) result = service.get_paginate_workflow_app_logs( session=db_session_with_containers, app_model=app, keyword="test_keyword", page=1, limit=20 ) @@ -305,7 +329,7 @@ class TestWorkflowAppService: # Verify the returned data contains the searched keyword log_entry = result["data"][0] - assert log_entry.workflow_run_id == workflow_run.id + assert _workflow_run(log_entry).id == workflow_run.id # Test with non-matching keyword result_no_match = service.get_paginate_workflow_app_logs( @@ -331,7 +355,7 @@ class TestWorkflowAppService: app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies) workflow, _, _ = self._create_test_workflow_data(db_session_with_containers, app, account) - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test 1: Search with % character workflow_run_1 = WorkflowRun( @@ -373,7 +397,7 @@ class TestWorkflowAppService: # Should find the workflow_run_1 entry assert result["total"] >= 1 assert len(result["data"]) >= 1 - assert any(log.workflow_run_id == workflow_run_1.id for log in result["data"]) + assert any(_workflow_run(log).id == workflow_run_1.id for log in result["data"]) # Test 2: Search with _ character workflow_run_2 = WorkflowRun( @@ -415,7 +439,7 @@ class TestWorkflowAppService: # Should find the workflow_run_2 entry assert result["total"] >= 1 assert len(result["data"]) >= 1 - assert any(log.workflow_run_id == workflow_run_2.id for log in result["data"]) + assert any(_workflow_run(log).id == workflow_run_2.id for log in result["data"]) # Test 3: Search with % should NOT match 100% (verifies escaping works correctly) workflow_run_4 = WorkflowRun( @@ -459,7 +483,7 @@ class TestWorkflowAppService: assert result["total"] >= 1 assert len(result["data"]) >= 1 # Verify that we found workflow_run_1 (50% discount) but not workflow_run_4 (100% different) - found_run_ids = [log.workflow_run_id for log in result["data"]] + found_run_ids = [_workflow_run(log).id for log in result["data"]] assert workflow_run_1.id in found_run_ids assert workflow_run_4.id not in found_run_ids @@ -535,7 +559,7 @@ class TestWorkflowAppService: workflow_app_logs.append(workflow_app_log) # Act & Assert: Test filtering by different statuses - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test succeeded status filter result_succeeded = service.get_paginate_workflow_app_logs( @@ -641,7 +665,7 @@ class TestWorkflowAppService: workflow_app_logs.append(workflow_app_log) # Act & Assert: Test time-based filtering - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test filtering logs created after 2 hours ago result_after = service.get_paginate_workflow_app_logs( @@ -746,7 +770,7 @@ class TestWorkflowAppService: workflow_app_logs.append(workflow_app_log) # Act & Assert: Test pagination - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test first page with limit 10 result_page1 = service.get_paginate_workflow_app_logs( @@ -916,7 +940,7 @@ class TestWorkflowAppService: workflow_app_logs.append(workflow_app_log) # Act & Assert: Test user role filtering - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test filtering by end user session ID result_session_filter = service.get_paginate_workflow_app_logs( @@ -1048,14 +1072,14 @@ class TestWorkflowAppService: db_session_with_containers.commit() # Act & Assert: Test UUID keyword search - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test searching by workflow run UUID result_uuid_search = service.get_paginate_workflow_app_logs( session=db_session_with_containers, app_model=app, keyword=workflow_run_id, page=1, limit=20 ) assert result_uuid_search["total"] == 1 - assert result_uuid_search["data"][0].workflow_run_id == workflow_run_id + assert _workflow_run(result_uuid_search["data"][0]).id == workflow_run_id # Test searching by partial UUID (should not match) partial_uuid = workflow_run_id[:8] @@ -1136,7 +1160,7 @@ class TestWorkflowAppService: db_session_with_containers.commit() # Act & Assert: Test edge cases - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test with page 1 (normal case) result_page_one = service.get_paginate_workflow_app_logs( @@ -1180,7 +1204,7 @@ class TestWorkflowAppService: app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies) # Act & Assert: Test empty results - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test with no workflow logs result_no_logs = service.get_paginate_workflow_app_logs( @@ -1291,7 +1315,7 @@ class TestWorkflowAppService: db_session_with_containers.commit() - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test complex combination: keyword + status + time range + pagination result_complex = service.get_paginate_workflow_app_logs( @@ -1391,7 +1415,7 @@ class TestWorkflowAppService: db_session_with_containers.commit() - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test performance with large dataset and pagination import time @@ -1442,14 +1466,18 @@ class TestWorkflowAppService: db_session_with_containers, mock_external_service_dependencies ) app1 = self._create_test_app(db_session_with_containers, tenant1, account1) - workflow1, _, _ = self._create_test_workflow_data(db_session_with_containers, app1, account1) + workflow1, workflow_run1, _ = self._create_test_workflow_data(db_session_with_containers, app1, account1) # Create second tenant and app tenant2, account2 = self._create_test_tenant_and_account( db_session_with_containers, mock_external_service_dependencies ) app2 = self._create_test_app(db_session_with_containers, tenant2, account2) - workflow2, _, _ = self._create_test_workflow_data(db_session_with_containers, app2, account2) + workflow2, workflow_run2, _ = self._create_test_workflow_data(db_session_with_containers, app2, account2) + run_ids_by_app = { + app1.id: {workflow_run1.id}, + app2.id: {workflow_run2.id}, + } # Create logs for both tenants for i, (app, workflow, account) in enumerate([(app1, workflow1, account1), (app2, workflow2, account2)]): @@ -1476,6 +1504,7 @@ class TestWorkflowAppService: ) db_session_with_containers.add(workflow_run) db_session_with_containers.flush() + run_ids_by_app[app.id].add(workflow_run.id) log = WorkflowAppLog( tenant_id=app.tenant_id, @@ -1492,7 +1521,7 @@ class TestWorkflowAppService: db_session_with_containers.commit() - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) # Test tenant isolation: tenant1 should only see its own logs result_tenant1 = service.get_paginate_workflow_app_logs( @@ -1500,9 +1529,7 @@ class TestWorkflowAppService: ) assert result_tenant1["total"] == 4 # 3 new logs + 1 from _create_test_workflow_data - for log in result_tenant1["data"]: - assert log.tenant_id == app1.tenant_id - assert log.app_id == app1.id + assert {_workflow_run(log).id for log in result_tenant1["data"]} == run_ids_by_app[app1.id] # Test tenant isolation: tenant2 should only see its own logs result_tenant2 = service.get_paginate_workflow_app_logs( @@ -1510,9 +1537,7 @@ class TestWorkflowAppService: ) assert result_tenant2["total"] == 4 # 3 new logs + 1 from _create_test_workflow_data - for log in result_tenant2["data"]: - assert log.tenant_id == app2.tenant_id - assert log.app_id == app2.id + assert {_workflow_run(log).id for log in result_tenant2["data"]} == run_ids_by_app[app2.id] # Test cross-tenant search should not work result_cross_tenant = service.get_paginate_workflow_app_logs( @@ -1530,7 +1555,7 @@ class TestWorkflowAppService: self, db_session_with_containers: Session, mock_external_service_dependencies ): app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies) - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) with pytest.raises(ValueError, match="Account not found: nonexistent@example.com"): service.get_paginate_workflow_app_logs( @@ -1543,7 +1568,7 @@ class TestWorkflowAppService: self, db_session_with_containers: Session, mock_external_service_dependencies ): app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies) - service = WorkflowAppService() + service = _WorkflowAppLogTestClient(db_session_with_containers) workflow, workflow_run, _log = self._create_test_workflow_data(db_session_with_containers, app, account) result = service.get_paginate_workflow_app_logs( diff --git a/api/tests/test_containers_integration_tests/services/test_workflow_run_service.py b/api/tests/test_containers_integration_tests/services/test_workflow_run_service.py index 263f41e5144..8d37aaa4e8c 100644 --- a/api/tests/test_containers_integration_tests/services/test_workflow_run_service.py +++ b/api/tests/test_containers_integration_tests/services/test_workflow_run_service.py @@ -5,13 +5,17 @@ from unittest.mock import patch import pytest from faker import Faker -from sqlalchemy.orm import Session +from sqlalchemy import Engine +from sqlalchemy.orm import Session, sessionmaker -from models.enums import ConversationFromSource, CreatorUserRole, EndUserType +from machinery.context import RequestContext +from models.enums import ConversationFromSource, CreatorUserRole from models.model import ( Message, ) from models.workflow import WorkflowRun +from repositories.factory import DifyAPIRepositoryFactory +from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository from services.account_service import AccountService, TenantService from services.app_service import AppService, CreateAppParams from services.workflow_run_service import WorkflowRunService @@ -21,6 +25,19 @@ from tests.test_containers_integration_tests.helpers import generate_valid_passw class TestWorkflowRunService: """Integration tests for WorkflowRunService using testcontainers.""" + @pytest.fixture + def workflow_run_service(self, db_session_with_containers: Session) -> WorkflowRunService: + engine = db_session_with_containers.get_bind() + assert isinstance(engine, Engine) + session_factory = sessionmaker(bind=engine, expire_on_commit=False) + workflow_runs = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=session_factory) + return WorkflowRunService( + workflow_runs=workflow_runs, + node_executions=DifyAPIRepositoryFactory.create_api_workflow_node_execution_repository( + session_maker=session_factory + ), + ) + @pytest.fixture def mock_external_service_dependencies(self): """Mock setup for external service dependencies.""" @@ -50,6 +67,15 @@ class TestWorkflowRunService: "account_feature_service": mock_account_feature_service, } + @staticmethod + def _request_context(app, account) -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id=account.id, + active_workspace_id=app.tenant_id, + ) + def _create_test_app_and_account(self, db_session_with_containers: Session, mock_external_service_dependencies): """ Helper method to create a test app and account for testing. @@ -196,7 +222,10 @@ class TestWorkflowRunService: return message def test_get_paginate_workflow_runs_success( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test successful pagination of workflow runs with debugging trigger. @@ -218,9 +247,12 @@ class TestWorkflowRunService: workflow_runs.append(workflow_run) # Act: Execute the method under test - workflow_run_service = WorkflowRunService() args = {"limit": 3, "last_id": None} - result = workflow_run_service.get_paginate_workflow_runs(app, args) + result = workflow_run_service.get_paginate_workflow_runs( + self._request_context(app, account), + app_id=app.id, + args=args, + ) # Assert: Verify the expected outcomes assert result is not None @@ -238,7 +270,10 @@ class TestWorkflowRunService: assert workflow_run.tenant_id == app.tenant_id def test_get_paginate_workflow_runs_with_last_id( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test pagination of workflow runs with last_id parameter. @@ -261,9 +296,12 @@ class TestWorkflowRunService: workflow_runs.append(workflow_run) # Act: Execute the method under test with last_id - workflow_run_service = WorkflowRunService() args = {"limit": 2, "last_id": workflow_runs[1].id} - result = workflow_run_service.get_paginate_workflow_runs(app, args) + result = workflow_run_service.get_paginate_workflow_runs( + self._request_context(app, account), + app_id=app.id, + args=args, + ) # Assert: Verify the expected outcomes assert result is not None @@ -281,7 +319,10 @@ class TestWorkflowRunService: assert workflow_run.tenant_id == app.tenant_id def test_get_paginate_workflow_runs_default_limit( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test pagination of workflow runs with default limit. @@ -299,9 +340,12 @@ class TestWorkflowRunService: workflow_run = self._create_test_workflow_run(db_session_with_containers, app, account, "debugging") # Act: Execute the method under test without limit - workflow_run_service = WorkflowRunService() args = {} # No limit specified - result = workflow_run_service.get_paginate_workflow_runs(app, args) + result = workflow_run_service.get_paginate_workflow_runs( + self._request_context(app, account), + app_id=app.id, + args=args, + ) # Assert: Verify the expected outcomes assert result is not None @@ -319,7 +363,10 @@ class TestWorkflowRunService: assert workflow_run_result.tenant_id == app.tenant_id def test_get_paginate_advanced_chat_workflow_runs_success( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test successful pagination of advanced chat workflow runs with message information. @@ -344,9 +391,12 @@ class TestWorkflowRunService: workflow_runs.append(workflow_run) # Act: Execute the method under test - workflow_run_service = WorkflowRunService() args = {"limit": 2, "last_id": None} - result = workflow_run_service.get_paginate_advanced_chat_workflow_runs(app, args) + result = workflow_run_service.get_paginate_advanced_chat_workflow_runs( + self._request_context(app, account), + app_id=app.id, + args=args, + ) # Assert: Verify the expected outcomes assert result is not None @@ -364,7 +414,12 @@ class TestWorkflowRunService: assert workflow_run.app_id == app.id assert workflow_run.tenant_id == app.tenant_id - def test_get_workflow_run_success(self, db_session_with_containers: Session, mock_external_service_dependencies): + def test_get_workflow_run_success( + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, + ): """ Test successful retrieval of workflow run by ID. @@ -381,8 +436,11 @@ class TestWorkflowRunService: workflow_run = self._create_test_workflow_run(db_session_with_containers, app, account, "debugging") # Act: Execute the method under test - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_workflow_run(app, workflow_run.id) + result = workflow_run_service.get_workflow_run( + self._request_context(app, account), + app_id=app.id, + run_id=workflow_run.id, + ) # Assert: Verify the expected outcomes assert result is not None @@ -394,7 +452,12 @@ class TestWorkflowRunService: assert result.type == "chat" assert result.version == "1.0.0" - def test_get_workflow_run_not_found(self, db_session_with_containers: Session, mock_external_service_dependencies): + def test_get_workflow_run_not_found( + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, + ): """ Test workflow run retrieval when run ID does not exist. @@ -411,14 +474,20 @@ class TestWorkflowRunService: non_existent_id = str(uuid.uuid4()) # Act: Execute the method under test - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_workflow_run(app, non_existent_id) + result = workflow_run_service.get_workflow_run( + self._request_context(app, account), + app_id=app.id, + run_id=non_existent_id, + ) # Assert: Verify the expected outcomes assert result is None def test_get_workflow_run_node_executions_success( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test successful retrieval of workflow run node executions. @@ -487,8 +556,11 @@ class TestWorkflowRunService: db_session_with_containers.commit() # Act: Execute the method under test - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_workflow_run_node_executions(app, workflow_run.id, account) + result = workflow_run_service.get_workflow_run_node_executions( + self._request_context(app, account), + app_id=app.id, + run_id=workflow_run.id, + ) # Assert: Verify the expected outcomes assert result is not None @@ -507,7 +579,10 @@ class TestWorkflowRunService: assert node_execution.node_id.startswith("node_") def test_get_workflow_run_node_executions_empty( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test getting node executions for a workflow run with no executions. @@ -521,7 +596,6 @@ class TestWorkflowRunService: account_service = AccountService() tenant_service = TenantService() app_service = AppService() - workflow_run_service = WorkflowRunService() # Create account and tenant account = account_service.create_account( @@ -549,9 +623,9 @@ class TestWorkflowRunService: # Act: Get node executions result = workflow_run_service.get_workflow_run_node_executions( - app_model=app, + self._request_context(app, account), + app_id=app.id, run_id=workflow_run.id, - user=account, ) # Assert: Verify empty result @@ -559,7 +633,10 @@ class TestWorkflowRunService: assert len(result) == 0 def test_get_workflow_run_node_executions_invalid_workflow_run_id( - self, db_session_with_containers: Session, mock_external_service_dependencies + self, + db_session_with_containers: Session, + mock_external_service_dependencies, + workflow_run_service: WorkflowRunService, ): """ Test getting node executions with invalid workflow run ID. @@ -573,7 +650,6 @@ class TestWorkflowRunService: account_service = AccountService() tenant_service = TenantService() app_service = AppService() - workflow_run_service = WorkflowRunService() # Create account and tenant account = account_service.create_account( @@ -601,137 +677,11 @@ class TestWorkflowRunService: # Act: Get node executions with invalid ID result = workflow_run_service.get_workflow_run_node_executions( - app_model=app, + self._request_context(app, account), + app_id=app.id, run_id=invalid_workflow_run_id, - user=account, ) # Assert: Verify empty result assert result is not None assert len(result) == 0 - - def test_get_workflow_run_node_executions_database_error( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - """ - Test getting node executions when database encounters an error. - - This test verifies: - - Proper error handling when database operations fail - - Graceful degradation in error scenarios - - Error propagation to calling code - """ - # Arrange: Setup test data - account_service = AccountService() - tenant_service = TenantService() - app_service = AppService() - workflow_run_service = WorkflowRunService() - - # Create account and tenant - account = account_service.create_account( - email="test@example.com", - name="Test User", - password="password123", - interface_language="en-US", - session=db_session_with_containers, - ) - TenantService.create_owner_tenant_if_not_exist(account, name="test_tenant", session=db_session_with_containers) - tenant = account.current_tenant - - # Create app - app_args = CreateAppParams( - name="Test App", - mode="chat", - icon_type="emoji", - icon="🚀", - icon_background="#4ECDC4", - ) - app = app_service.create_app(tenant.id, app_args, account, session=db_session_with_containers) - - # Create workflow run - workflow_run = self._create_test_workflow_run(db_session_with_containers, app, account, "debugging") - - # Mock database error by closing the session - db_session_with_containers.close() - - # Act & Assert: Verify error handling - with pytest.raises((Exception, RuntimeError)): - workflow_run_service.get_workflow_run_node_executions( - app_model=app, - run_id=workflow_run.id, - user=account, - ) - - def test_get_workflow_run_node_executions_end_user( - self, db_session_with_containers: Session, mock_external_service_dependencies - ): - """ - Test node execution retrieval for end user. - - This test verifies: - - Proper handling of end user vs account user - - Correct tenant ID extraction for end users - - Repository method calls with proper parameters - """ - # Arrange: Create test data - fake = Faker() - app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies) - - # Create workflow run - workflow_run = self._create_test_workflow_run(db_session_with_containers, app, account, "debugging") - - # Create end user - from models.model import EndUser - - end_user = EndUser( - tenant_id=app.tenant_id, - app_id=app.id, - type=EndUserType.BROWSER, - is_anonymous=False, - session_id=str(uuid.uuid4()), - external_user_id=str(uuid.uuid4()), - name=fake.name(), - ) - db_session_with_containers.add(end_user) - db_session_with_containers.commit() - - # Create node execution - from models.workflow import WorkflowNodeExecutionModel - - node_execution = WorkflowNodeExecutionModel( - tenant_id=app.tenant_id, - app_id=app.id, - workflow_id=workflow_run.workflow_id, - triggered_from="workflow-run", - workflow_run_id=workflow_run.id, - index=0, - node_id="node_0", - node_type="llm", - title="Node 0", - inputs=json.dumps({"input": "test_input"}), - process_data=json.dumps({"process": "test_process"}), - status="succeeded", - elapsed_time=0.5, - execution_metadata=json.dumps({"tokens": 50}), - created_by_role=CreatorUserRole.END_USER, - created_by=end_user.id, - created_at=datetime.now(UTC), - ) - db_session_with_containers.add(node_execution) - db_session_with_containers.commit() - - # Act: Execute the method under test - workflow_run_service = WorkflowRunService() - result = workflow_run_service.get_workflow_run_node_executions(app, workflow_run.id, end_user) - - # Assert: Verify the expected outcomes - assert result is not None - assert len(result) == 1 - - # Verify node execution properties - node_exec = result[0] - assert node_exec.tenant_id == app.tenant_id - assert node_exec.app_id == app.id - assert node_exec.workflow_run_id == workflow_run.id - assert node_exec.created_by == end_user.id - assert node_exec.created_by_role == CreatorUserRole.END_USER diff --git a/api/tests/unit_tests/commands/test_check_no_new_getattr.py b/api/tests/unit_tests/commands/test_check_no_new_getattr.py index a63569c706f..a846955a770 100644 --- a/api/tests/unit_tests/commands/test_check_no_new_getattr.py +++ b/api/tests/unit_tests/commands/test_check_no_new_getattr.py @@ -228,19 +228,6 @@ def test_style_workflow_wires_no_new_getattr_guard() -> None: assert checkout_step is not None assert "fetch-depth: 0" in checkout_step.group("step") - changed_files_step = re.search( - r"(?ms)^ - name: Check changed files\n.*?^ files: \|\n(?P(?:^ \S[^\n]*\n)+)", - job_text, - ) - assert changed_files_step is not None - - files_block = changed_files_step.group("files") - assert "api/**\n" in files_block - assert "scripts/check_no_new_getattr.py\n" in files_block - assert "scripts/ast_grep_rules/no_new_getattr.yml\n" in files_block - assert ".github/workflows/style.yml\n" in files_block - assert ".github/workflows/main-ci.yml\n" in files_block - guard_command = 'scripts/check_no_new_getattr.py --base-rev "${{ inputs.base-rev }}"' assert guard_command in job_text @@ -251,6 +238,7 @@ def test_style_workflow_wires_no_new_getattr_guard() -> None: ) assert guard_step is not None + assert "if: inputs.run-python-style" in guard_step.group("step") assert "GITHUB_BASE_SHA" not in guard_step.group("step") @@ -266,17 +254,22 @@ def test_main_ci_passes_style_base_rev_input() -> None: "base-rev: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }}" in style_job.group("job") ) - - api_filter = re.search( - r"(?ms)^ api:\n(?P(?:^ - '[^']+'\n)+)", - workflow, + assert "run-python-style: ${{ needs.check-changes.outputs.python-style-changed == 'true' }}" in style_job.group( + "job" ) - assert api_filter is not None - filter_text = api_filter.group("filter") - assert "scripts/check_no_new_getattr.py" in filter_text - assert "scripts/ast_grep_rules/no_new_getattr.yml" in filter_text - assert ".github/workflows/style.yml" in filter_text - assert ".github/workflows/main-ci.yml" in filter_text + + for filter_name in ("api", "python-style"): + path_filter = re.search( + rf"(?ms)^ {re.escape(filter_name)}:\n(?P(?:^ - '[^']+'\n)+)", + workflow, + ) + assert path_filter is not None, filter_name + filter_text = path_filter.group("filter") + assert "api/**" in filter_text + assert "scripts/check_no_new_getattr.py" in filter_text + assert "scripts/ast_grep_rules/no_new_getattr.yml" in filter_text + assert ".github/workflows/style.yml" in filter_text + assert ".github/workflows/main-ci.yml" in filter_text def test_base_rev_mode_passes_when_only_legacy_getattr_exists(tmp_path: Path) -> None: diff --git a/api/tests/unit_tests/controllers/console/app/test_app_apis.py b/api/tests/unit_tests/controllers/console/app/test_app_apis.py index 6d574657017..461afa9efd4 100644 --- a/api/tests/unit_tests/controllers/console/app/test_app_apis.py +++ b/api/tests/unit_tests/controllers/console/app/test_app_apis.py @@ -53,7 +53,11 @@ from controllers.console.app import ( wraps as wraps_module, ) from controllers.console.app.completion import ChatMessagePayload, CompletionMessagePayload -from controllers.console.app.error import AppNotFoundError +from controllers.console.app.error import ( + AppNotFoundError, + TracingConfigNotFoundError, + TracingConfigVerificationFailedError, +) from controllers.console.app.mcp_server import MCPServerCreatePayload, MCPServerUpdatePayload from controllers.console.app.ops_trace import TraceConfigPayload, TraceProviderQuery from controllers.console.app.site import AppSiteUpdatePayload @@ -77,6 +81,10 @@ from services.app_site_service import ( AppSiteCommandResult, AppSiteNotFoundError, ) +from services.app_tracing_config_service import ( + AppTracingConfigNotFoundError, + AppTracingConfigVerificationFailedError, +) from tests.unit_tests.config_override import apply_config_overrides APP_ID = "11111111-1111-1111-1111-111111111111" @@ -387,52 +395,71 @@ class TestOpsTraceEndpoints: def test_trace_app_config_get_empty(self, app: Flask, monkeypatch: pytest.MonkeyPatch): api = ops_trace_module.TraceAppConfigApi() method = unwrap(api.get) - + tracing_configs = MagicMock() + tracing_configs.get.return_value = None monkeypatch.setattr( - ops_trace_module.OpsService, - "get_tracing_app_config", - lambda **_kwargs: None, + ops_trace_module, + "application_services", + lambda: SimpleNamespace(app_tracing_configs=tracing_configs), ) with app.test_request_context("/?tracing_provider=langfuse"): - result = method(api, TraceProviderQuery(tracing_provider="langfuse"), _make_app()) + result = method( + api, + TraceProviderQuery(tracing_provider="langfuse"), + _make_request_context(), + uuid.UUID(APP_ID), + ) assert result == {"has_not_configured": True} + tracing_configs.get.assert_called_once_with( + context=_make_request_context(), + app_id=APP_ID, + tracing_provider="langfuse", + ) def test_trace_app_config_post_invalid(self, app: Flask, monkeypatch: pytest.MonkeyPatch): api = ops_trace_module.TraceAppConfigApi() method = unwrap(api.post) - + tracing_configs = MagicMock() + tracing_configs.create.side_effect = AppTracingConfigVerificationFailedError() monkeypatch.setattr( - ops_trace_module.OpsService, - "create_tracing_app_config", - lambda **_kwargs: {"error": True}, + ops_trace_module, + "application_services", + lambda: SimpleNamespace(app_tracing_configs=tracing_configs), ) with app.test_request_context( "/", json={"tracing_provider": "langfuse", "tracing_config": {"api_key": "k"}}, ): - with pytest.raises(BadRequest): + with pytest.raises(TracingConfigVerificationFailedError): method( api, TraceConfigPayload(tracing_provider="langfuse", tracing_config={"api_key": "k"}), - _make_app(), + _make_request_context(), + uuid.UUID(APP_ID), ) def test_trace_app_config_delete_not_found(self, app: Flask, monkeypatch: pytest.MonkeyPatch): api = ops_trace_module.TraceAppConfigApi() method = unwrap(api.delete) - + tracing_configs = MagicMock() + tracing_configs.delete.side_effect = AppTracingConfigNotFoundError() monkeypatch.setattr( - ops_trace_module.OpsService, - "delete_tracing_app_config", - lambda **_kwargs: False, + ops_trace_module, + "application_services", + lambda: SimpleNamespace(app_tracing_configs=tracing_configs), ) with app.test_request_context("/?tracing_provider=langfuse"): - with pytest.raises(BadRequest): - method(api, TraceProviderQuery(tracing_provider="langfuse"), _make_app()) + with pytest.raises(TracingConfigNotFoundError): + method( + api, + TraceProviderQuery(tracing_provider="langfuse"), + _make_request_context(), + uuid.UUID(APP_ID), + ) class TestSiteEndpoints: @@ -573,21 +600,36 @@ class TestWorkflowAppLogEndpoints: def test_workflow_app_log_api_get(self, database_app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: api = workflow_app_log_module.WorkflowAppLogApi() method = unwrap(api.get) - - def fake_get_paginate(self, *, session: Session, **_kwargs): - assert session.get_bind() is db.engine - return {"page": 1, "limit": 20, "total": 0, "has_more": False, "data": []} - - monkeypatch.setattr( - workflow_app_log_module.WorkflowAppService, - "get_paginate_workflow_app_logs", - fake_get_paginate, - ) + workflow_app_logs = MagicMock() + workflow_app_logs.list_logs.return_value = { + "page": 1, + "limit": 20, + "total": 0, + "has_more": False, + "data": [], + } + services = MagicMock(workflow_app_logs=workflow_app_logs) + monkeypatch.setattr(workflow_app_log_module, "application_services", lambda: services) + context = RequestContext("request-1", None, USER_ID, TENANT_ID) + app_model = _make_app("app-1") with database_app.test_request_context("/?page=1&limit=20"): - result = method(api, WorkflowAppLogQuery(page=1, limit=20), app_model=_make_app("app-1")) + result = method(api, WorkflowAppLogQuery(page=1, limit=20), context, app_model=app_model) assert result == {"page": 1, "limit": 20, "total": 0, "has_more": False, "data": []} + workflow_app_logs.list_logs.assert_called_once_with( + tenant_id=app_model.tenant_id, + app_id=app_model.id, + keyword=None, + status=None, + created_at_before=None, + created_at_after=None, + page=1, + limit=20, + detail=False, + created_by_end_user_session_id=None, + created_by_account=None, + ) class TestWorkflowDraftVariableEndpoints: diff --git a/api/tests/unit_tests/controllers/console/app/test_ops_trace_api.py b/api/tests/unit_tests/controllers/console/app/test_ops_trace_api.py index 1c1ee83e19a..03922de106d 100644 --- a/api/tests/unit_tests/controllers/console/app/test_ops_trace_api.py +++ b/api/tests/unit_tests/controllers/console/app/test_ops_trace_api.py @@ -1,197 +1,527 @@ from __future__ import annotations -from contextlib import nullcontext -from unittest.mock import MagicMock, PropertyMock, patch +import inspect +from collections.abc import Callable +from datetime import datetime +from types import SimpleNamespace +from unittest.mock import MagicMock +from uuid import UUID import pytest from flask import Flask -from sqlalchemy.orm import Session from werkzeug.exceptions import Forbidden -from controllers.common import wraps as common_wraps -from controllers.console import console_ns -from controllers.console import wraps as console_wraps +from controllers.console import flask_admission from controllers.console.app import ops_trace as ops_trace_module -from controllers.console.app import wraps as app_wraps -from enums import DeploymentEdition -from libs import login as login_lib -from models import Tenant +from controllers.console.app.error import ( + AppNotFoundError, + InvalidTracingConfigError, + TracingConfigAlreadyExistsError, + TracingConfigNotFoundError, + TracingConfigProcessingError, + TracingConfigVerificationFailedError, + UnsupportedTracingProviderError, +) +from libs.exception import BaseHTTPException +from libs.login import AccountWithTenant +from machinery.context import RequestContext from models.account import Account, AccountStatus, TenantAccountRole -from models.model import App, AppMode, IconType +from services.app_tracing_config_service import ( + AppTracingConfigAlreadyExistsError, + AppTracingConfigAppNotFoundError, + AppTracingConfigInvalidConfigurationError, + AppTracingConfigInvalidProviderError, + AppTracingConfigNotFoundError, + AppTracingConfigProcessingError, + AppTracingConfigRecord, + AppTracingConfigVerificationFailedError, +) from tests.unit_tests.config_override import apply_config_overrides +APP_ID = "11111111-1111-1111-1111-111111111111" +WORKSPACE_ID = "22222222-2222-2222-2222-222222222222" +ACCOUNT_ID = "33333333-3333-3333-3333-333333333333" +PROVIDER = "langfuse" +_MUTATION_METHODS = ( + ops_trace_module.TraceAppConfigApi.post, + ops_trace_module.TraceAppConfigApi.patch, + ops_trace_module.TraceAppConfigApi.delete, +) +_CONTROLLER_METHODS: dict[str, Callable[..., object]] = { + "get": ops_trace_module.TraceAppConfigApi.get, + "post": ops_trace_module.TraceAppConfigApi.post, + "patch": ops_trace_module.TraceAppConfigApi.patch, + "delete": ops_trace_module.TraceAppConfigApi.delete, +} -def _make_account(role: TenantAccountRole) -> Account: - account = Account(name="tester", email="tester@example.com") - account.id = "account-123" # type: ignore[assignment] - account.status = AccountStatus.ACTIVE + +def _service_method(tracing_configs: MagicMock, method_name: str) -> MagicMock: + return { + "get": tracing_configs.get, + "post": tracing_configs.create, + "patch": tracing_configs.update, + "delete": tracing_configs.delete, + }[method_name] + + +def _account(role: TenantAccountRole) -> Account: + account = Account( + name="Trace User", + email=f"{role.value}@example.com", + status=AccountStatus.ACTIVE, + ) + account.id = ACCOUNT_ID account.role = role - tenant = Tenant(name="Test tenant") - tenant.id = "tenant-123" - account._current_tenant = tenant - account._get_current_object = lambda: account # type: ignore[attr-defined] return account -def _make_app() -> App: - return App( - id="app-123", - tenant_id="tenant-123", - name="Trace app", - description="", - mode=AppMode.CHAT, - icon_type=IconType.EMOJI, - icon="robot", - icon_background="#FFFFFF", - enable_site=True, - enable_api=True, - max_active_requests=None, +def _request_context() -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id=ACCOUNT_ID, + active_workspace_id=WORKSPACE_ID, ) -def _patch_console_guards( - monkeypatch: pytest.MonkeyPatch, - account: Account, - app_model: App, - *, - rbac_enabled: bool = False, -) -> None: - apply_config_overrides( - monkeypatch, - LOGIN_DISABLED=True, - RBAC_ENABLED=rbac_enabled, - DEPLOYMENT_EDITION=DeploymentEdition.CLOUD, +def _original(method: Callable[..., object]) -> Callable[..., object]: + return inspect.unwrap(method) + + +def _admission_injector(method: Callable[..., object]) -> Callable[..., object]: + return inspect.unwrap( + method, + stop=lambda candidate: "allowed_roles" in inspect.getclosurevars(candidate).nonlocals, ) - monkeypatch.setattr(login_lib, "current_user", account) - monkeypatch.setattr(login_lib, "current_account_with_tenant", lambda: (account, account.current_tenant_id)) - monkeypatch.setattr(console_wraps, "current_account_with_tenant", lambda: (account, account.current_tenant_id)) - monkeypatch.setattr(common_wraps, "current_account_with_tenant", lambda: (account, account.current_tenant_id)) - monkeypatch.setattr(app_wraps, "_load_app_model_from_scoped_session", lambda _app_id: app_model) -def _patch_payload(payload: dict[str, object] | None): - if payload is None: - return nullcontext() - return patch.object(type(console_ns), "payload", new_callable=PropertyMock, return_value=payload) +@pytest.fixture +def tracing_configs(monkeypatch: pytest.MonkeyPatch) -> MagicMock: + service = MagicMock() + monkeypatch.setattr( + ops_trace_module, + "application_services", + lambda: SimpleNamespace(app_tracing_configs=service), + ) + return service + + +@pytest.mark.parametrize("method", _MUTATION_METHODS) +def test_trace_config_mutations_reject_read_only_member_when_rbac_is_disabled( + app: Flask, + monkeypatch: pytest.MonkeyPatch, + method: Callable[..., object], +) -> None: + account = _account(TenantAccountRole.NORMAL) + apply_config_overrides(monkeypatch, RBAC_ENABLED=False) + monkeypatch.setattr( + flask_admission, + "current_account_with_tenant", + lambda: AccountWithTenant(account=account, tenant_id=WORKSPACE_ID), + ) + + with app.test_request_context(), pytest.raises(Forbidden): + _admission_injector(method)(None, app_id=UUID(APP_ID)) + + +@pytest.mark.parametrize("method", _MUTATION_METHODS) +def test_trace_config_mutations_require_app_tracing_permission_when_rbac_is_enabled( + app: Flask, + monkeypatch: pytest.MonkeyPatch, + method: Callable[..., object], +) -> None: + account = _account(TenantAccountRole.NORMAL) + apply_config_overrides(monkeypatch, RBAC_ENABLED=True) + monkeypatch.setattr( + flask_admission, + "current_account_with_tenant", + lambda: AccountWithTenant(account=account, tenant_id=WORKSPACE_ID), + ) + denied = MagicMock(side_effect=Forbidden()) + monkeypatch.setattr(flask_admission, "enforce_rbac_access", denied) + + with app.test_request_context(), pytest.raises(Forbidden): + _admission_injector(method)(None, app_id=UUID(APP_ID)) + + denied.assert_called_once_with( + tenant_id=WORKSPACE_ID, + account_id=ACCOUNT_ID, + resource_type=ops_trace_module.RBACResourceScope.APP, + scene=ops_trace_module.RBACPermission.APP_TRACING_CONFIG, + resource_required=True, + path_args={"app_id": UUID(APP_ID)}, + ) + + +def test_trace_config_get_preserves_read_access_for_normal_member() -> None: + admission = _admission_injector(ops_trace_module.TraceAppConfigApi.get) + + assert inspect.getclosurevars(admission).nonlocals["allowed_roles"] is None + + +@pytest.mark.parametrize("method", _MUTATION_METHODS) +def test_trace_config_mutations_preserve_legacy_edit_roles(method: Callable[..., object]) -> None: + admission = _admission_injector(method) + + assert inspect.getclosurevars(admission).nonlocals["allowed_roles"] == frozenset( + { + TenantAccountRole.OWNER, + TenantAccountRole.ADMIN, + TenantAccountRole.EDITOR, + } + ) + + +def test_trace_app_config_get_empty_returns_exact_legacy_body( + app: Flask, + tracing_configs: MagicMock, +) -> None: + tracing_configs.get.return_value = None + + with app.test_request_context("/?tracing_provider=langfuse"): + result = _original(ops_trace_module.TraceAppConfigApi.get)( + ops_trace_module.TraceAppConfigApi(), + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER), + _request_context(), + UUID(APP_ID), + ) + + assert result == {"has_not_configured": True} + tracing_configs.get.assert_called_once_with( + context=_request_context(), + app_id=APP_ID, + tracing_provider=PROVIDER, + ) + + +def test_trace_app_config_get_configured_returns_exact_legacy_body( + app: Flask, + tracing_configs: MagicMock, +) -> None: + tracing_configs.get.return_value = AppTracingConfigRecord( + id="trace-config-1", + app_id=APP_ID, + tracing_provider=PROVIDER, + tracing_config={"public_key": "pk", "secret_key": "******"}, + is_active=True, + created_at=datetime(2026, 1, 2, 3, 4, 5), + updated_at=datetime(2026, 1, 3, 4, 5, 6), + ) + + with app.test_request_context("/?tracing_provider=langfuse"): + result = _original(ops_trace_module.TraceAppConfigApi.get)( + ops_trace_module.TraceAppConfigApi(), + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER), + _request_context(), + UUID(APP_ID), + ) + + assert result == { + "id": "trace-config-1", + "app_id": APP_ID, + "tracing_provider": PROVIDER, + "tracing_config": {"public_key": "pk", "secret_key": "******"}, + "is_active": True, + "created_at": "2026-01-02 03:04:05", + "updated_at": "2026-01-03 04:05:06", + } @pytest.mark.parametrize( - ("method_name", "path", "payload", "service_method_name", "service_result"), + ("method_name", "expected_result"), [ - ( - "post", - "/console/api/apps/app-123/trace-config", - {"tracing_provider": "mlflow", "tracing_config": {"endpoint": "https://trace.example.com"}}, - "create_tracing_app_config", - {"id": "trace-config-1"}, - ), - ( - "patch", - "/console/api/apps/app-123/trace-config", - {"tracing_provider": "mlflow", "tracing_config": {"endpoint": "https://trace.example.com"}}, - "update_tracing_app_config", - True, - ), - ( - "delete", - "/console/api/apps/app-123/trace-config?tracing_provider=mlflow", - None, - "delete_tracing_app_config", - True, - ), + pytest.param("post", ({"result": "success"}, 201), id="create"), + pytest.param("patch", {"result": "success"}, id="update"), ], ) -def test_trace_config_mutations_require_edit_permission( +def test_trace_app_config_write_returns_expected_response( app: Flask, - monkeypatch: pytest.MonkeyPatch, + tracing_configs: MagicMock, method_name: str, - path: str, - payload: dict[str, object] | None, - service_method_name: str, - service_result: object, + expected_result: object, ) -> None: - app.config.setdefault("RESTX_MASK_HEADER", "X-Fields") - account = _make_account(TenantAccountRole.NORMAL) - _patch_console_guards(monkeypatch, account, _make_app()) - service_mock = MagicMock(return_value=service_result) - monkeypatch.setattr(ops_trace_module.OpsService, service_method_name, service_mock) + payload = ops_trace_module.TraceConfigPayload( + tracing_provider=PROVIDER, + tracing_config={"public_key": "pk", "secret_key": "sk"}, + ) - with app.test_request_context(path, method=method_name.upper(), json=payload): - with _patch_payload(payload): - with pytest.raises(Forbidden): - getattr(ops_trace_module.TraceAppConfigApi(), method_name)(app_id="app-123") + with app.test_request_context("/", method=method_name.upper()): + result = _original(_CONTROLLER_METHODS[method_name])( + ops_trace_module.TraceAppConfigApi(), + payload, + _request_context(), + UUID(APP_ID), + ) - service_mock.assert_not_called() + assert result == expected_result + _service_method(tracing_configs, method_name).assert_called_once_with( + context=_request_context(), + app_id=APP_ID, + tracing_provider=PROVIDER, + tracing_config={"public_key": "pk", "secret_key": "sk"}, + ) + + +def test_trace_app_config_delete_returns_exact_204_response( + app: Flask, + tracing_configs: MagicMock, +) -> None: + with app.test_request_context("/?tracing_provider=langfuse", method="DELETE"): + result = _original(ops_trace_module.TraceAppConfigApi.delete)( + ops_trace_module.TraceAppConfigApi(), + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER), + _request_context(), + UUID(APP_ID), + ) + + assert result == ("", 204) + tracing_configs.delete.assert_called_once_with( + context=_request_context(), + app_id=APP_ID, + tracing_provider=PROVIDER, + ) + + +@pytest.mark.parametrize("method_name", ["get", "post", "patch", "delete"]) +def test_trace_app_config_maps_missing_app_to_404( + app: Flask, + tracing_configs: MagicMock, + method_name: str, +) -> None: + service_method = _service_method(tracing_configs, method_name) + service_method.side_effect = AppTracingConfigAppNotFoundError() + query_or_payload = ( + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER) + if method_name in {"get", "delete"} + else ops_trace_module.TraceConfigPayload(tracing_provider=PROVIDER, tracing_config={}) + ) + + with app.test_request_context("/"): + with pytest.raises(AppNotFoundError) as exc_info: + _original(_CONTROLLER_METHODS[method_name])( + ops_trace_module.TraceAppConfigApi(), + query_or_payload, + _request_context(), + UUID(APP_ID), + ) + + assert exc_info.value.code == 404 + assert exc_info.value.error_code == "app_not_found" @pytest.mark.parametrize( - ("method_name", "path", "payload", "service_method_name", "service_result"), + ("method_name", "service_error", "expected_http_error", "expected_status", "expected_code"), [ - ( + pytest.param( "post", - "/console/api/apps/app-123/trace-config", - {"tracing_provider": "mlflow", "tracing_config": {"endpoint": "https://trace.example.com"}}, - "create_tracing_app_config", - {"id": "trace-config-1"}, + AppTracingConfigAlreadyExistsError(), + TracingConfigAlreadyExistsError, + 409, + "trace_config_already_exists", + id="already-exists", ), - ( + pytest.param( "patch", - "/console/api/apps/app-123/trace-config", - {"tracing_provider": "mlflow", "tracing_config": {"endpoint": "https://trace.example.com"}}, - "update_tracing_app_config", - True, + AppTracingConfigNotFoundError(), + TracingConfigNotFoundError, + 404, + "trace_config_not_found", + id="patch-not-found", ), - ( + pytest.param( "delete", - "/console/api/apps/app-123/trace-config?tracing_provider=mlflow", - None, - "delete_tracing_app_config", - True, + AppTracingConfigNotFoundError(), + TracingConfigNotFoundError, + 404, + "trace_config_not_found", + id="delete-not-found", + ), + pytest.param( + "get", + AppTracingConfigInvalidProviderError("unknown"), + UnsupportedTracingProviderError, + 400, + "unsupported_tracing_provider", + id="get-unsupported-provider", + ), + pytest.param( + "post", + AppTracingConfigInvalidProviderError("unknown"), + UnsupportedTracingProviderError, + 400, + "unsupported_tracing_provider", + id="post-unsupported-provider", + ), + pytest.param( + "patch", + AppTracingConfigInvalidProviderError("unknown"), + UnsupportedTracingProviderError, + 400, + "unsupported_tracing_provider", + id="patch-unsupported-provider", + ), + pytest.param( + "delete", + AppTracingConfigInvalidProviderError("unknown"), + UnsupportedTracingProviderError, + 400, + "unsupported_tracing_provider", + id="delete-unsupported-provider", + ), + pytest.param( + "post", + AppTracingConfigInvalidConfigurationError(), + InvalidTracingConfigError, + 400, + "invalid_tracing_config", + id="post-invalid-config", + ), + pytest.param( + "patch", + AppTracingConfigInvalidConfigurationError(), + InvalidTracingConfigError, + 400, + "invalid_tracing_config", + id="patch-invalid-config", + ), + pytest.param( + "post", + AppTracingConfigVerificationFailedError(), + TracingConfigVerificationFailedError, + 400, + "tracing_config_verification_failed", + id="post-verification-failed", + ), + pytest.param( + "patch", + AppTracingConfigVerificationFailedError(), + TracingConfigVerificationFailedError, + 400, + "tracing_config_verification_failed", + id="patch-verification-failed", + ), + pytest.param( + "get", + AppTracingConfigProcessingError(), + TracingConfigProcessingError, + 500, + "tracing_config_processing_failed", + id="get-processing-failed", + ), + pytest.param( + "post", + AppTracingConfigProcessingError(), + TracingConfigProcessingError, + 500, + "tracing_config_processing_failed", + id="post-processing-failed", + ), + pytest.param( + "patch", + AppTracingConfigProcessingError(), + TracingConfigProcessingError, + 500, + "tracing_config_processing_failed", + id="patch-processing-failed", + ), + pytest.param( + "delete", + AppTracingConfigProcessingError(), + TracingConfigProcessingError, + 500, + "tracing_config_processing_failed", + id="delete-processing-failed", ), ], ) -def test_trace_config_mutations_require_rbac_permission( +def test_trace_app_config_maps_application_errors_at_the_controller_boundary( app: Flask, - monkeypatch: pytest.MonkeyPatch, + tracing_configs: MagicMock, method_name: str, - path: str, - payload: dict[str, object] | None, - service_method_name: str, - service_result: object, - sqlite_session: Session, + service_error: Exception, + expected_http_error: type[BaseHTTPException], + expected_status: int, + expected_code: str, ) -> None: - app.config.setdefault("RESTX_MASK_HEADER", "X-Fields") - account = _make_account(TenantAccountRole.NORMAL) - _patch_console_guards(monkeypatch, account, _make_app(), rbac_enabled=True) - owned_app = App() - owned_app.id = "app-123" - owned_app.tenant_id = "tenant-123" - owned_app.name = "Trace app" - owned_app.description = "" - owned_app.mode = AppMode.CHAT - owned_app.icon_type = IconType.EMOJI - owned_app.icon = "robot" - owned_app.icon_background = "#ffffff" - owned_app.enable_site = False - owned_app.enable_api = False - owned_app.api_rpm = 0 - owned_app.api_rph = 0 - owned_app.is_demo = False - owned_app.is_public = False - owned_app.is_universal = False - owned_app.max_active_requests = None - owned_app.maintainer = "other-account" - owned_app.use_icon_as_answer_icon = False - sqlite_session.add(owned_app) - sqlite_session.commit() - monkeypatch.setattr(common_wraps.db, "session", sqlite_session) - monkeypatch.setattr(common_wraps.RBACService.CheckAccess, "check", MagicMock(return_value=False)) - service_mock = MagicMock(return_value=service_result) - monkeypatch.setattr(ops_trace_module.OpsService, service_method_name, service_mock) + service_method = _service_method(tracing_configs, method_name) + service_method.side_effect = service_error + query_or_payload = ( + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER) + if method_name in {"get", "delete"} + else ops_trace_module.TraceConfigPayload(tracing_provider=PROVIDER, tracing_config={}) + ) - with app.test_request_context(path, method=method_name.upper(), json=payload): - with _patch_payload(payload): - with pytest.raises(Forbidden): - getattr(ops_trace_module.TraceAppConfigApi(), method_name)(app_id="app-123") + with app.test_request_context("/"): + with pytest.raises(expected_http_error) as exc_info: + _original(_CONTROLLER_METHODS[method_name])( + ops_trace_module.TraceAppConfigApi(), + query_or_payload, + _request_context(), + UUID(APP_ID), + ) - service_mock.assert_not_called() + assert exc_info.value.code == expected_status + assert exc_info.value.error_code == expected_code + assert exc_info.value.data == { + "code": expected_code, + "message": exc_info.value.description, + "status": expected_status, + } + + +@pytest.mark.parametrize("method_name", ["get", "post", "patch", "delete"]) +def test_trace_app_config_maps_untyped_value_errors_to_internal_error( + app: Flask, + tracing_configs: MagicMock, + method_name: str, +) -> None: + service_method = _service_method(tracing_configs, method_name) + service_method.side_effect = ValueError("internal detail") + query_or_payload = ( + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER) + if method_name in {"get", "delete"} + else ops_trace_module.TraceConfigPayload(tracing_provider=PROVIDER, tracing_config={}) + ) + + with app.test_request_context("/"), pytest.raises(TracingConfigProcessingError) as exc_info: + _original(_CONTROLLER_METHODS[method_name])( + ops_trace_module.TraceAppConfigApi(), + query_or_payload, + _request_context(), + UUID(APP_ID), + ) + + assert exc_info.value.code == 500 + assert exc_info.value.error_code == "tracing_config_processing_failed" + assert exc_info.value.description == "The tracing configuration could not be processed." + assert exc_info.value.data == { + "code": "tracing_config_processing_failed", + "message": "The tracing configuration could not be processed.", + "status": 500, + } + + +@pytest.mark.parametrize("method_name", ["get", "post", "patch", "delete"]) +def test_trace_app_config_does_not_mask_unexpected_errors( + app: Flask, + tracing_configs: MagicMock, + method_name: str, +) -> None: + service_method = _service_method(tracing_configs, method_name) + unexpected_error = RuntimeError("unexpected") + service_method.side_effect = unexpected_error + query_or_payload = ( + ops_trace_module.TraceProviderQuery(tracing_provider=PROVIDER) + if method_name in {"get", "delete"} + else ops_trace_module.TraceConfigPayload(tracing_provider=PROVIDER, tracing_config={}) + ) + + with app.test_request_context("/"), pytest.raises(RuntimeError) as exc_info: + _original(_CONTROLLER_METHODS[method_name])( + ops_trace_module.TraceAppConfigApi(), + query_or_payload, + _request_context(), + UUID(APP_ID), + ) + + assert exc_info.value is unexpected_error diff --git a/api/tests/unit_tests/controllers/console/app/test_statistic_api.py b/api/tests/unit_tests/controllers/console/app/test_statistic_api.py index 2228069b40c..ed77c3ea5df 100644 --- a/api/tests/unit_tests/controllers/console/app/test_statistic_api.py +++ b/api/tests/unit_tests/controllers/console/app/test_statistic_api.py @@ -1,255 +1,198 @@ from __future__ import annotations +from collections.abc import Callable +from datetime import UTC, datetime from decimal import Decimal from inspect import unwrap from types import SimpleNamespace from typing import Any +from unittest.mock import MagicMock import pytest from flask import Flask from werkzeug.exceptions import BadRequest from controllers.console.app import statistic as statistic_module -from models.account import Account +from machinery.context import RequestContext from models.model import App +from services.app_statistic_query import ( + AppStatisticQuery, + AverageResponseTimeStatisticRecord, + AverageSessionInteractionStatisticRecord, + DailyConversationStatisticRecord, + DailyMessageStatisticRecord, + DailyTerminalStatisticRecord, + DailyTokenCostStatisticRecord, + TokensPerSecondStatisticRecord, + UserSatisfactionRateStatisticRecord, +) -def _account() -> Account: - account = Account(name="Statistics Tester", email="statistics-tester@example.com", timezone="UTC") - account.id = "account-1" - return account +def _request_context() -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + active_workspace_id="tenant-1", + ) def _app_model() -> App: return App(id="app-1", tenant_id="tenant-1", name="Statistics App") -class _ConnContext: - def __init__(self, rows): - self._rows = rows - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc, tb): - return False - - def execute(self, _query, _args): - return self._rows - - -def _install_db(monkeypatch: pytest.MonkeyPatch, rows) -> None: - engine = SimpleNamespace(begin=lambda: _ConnContext(rows)) - monkeypatch.setattr(statistic_module, "db", SimpleNamespace(engine=engine)) - - -def _install_common(monkeypatch: pytest.MonkeyPatch) -> None: +def _install_dependencies( + monkeypatch: pytest.MonkeyPatch, + statistics: MagicMock, + *, + time_range: tuple[datetime | None, datetime | None] = (None, None), +) -> None: monkeypatch.setattr( statistic_module, - "parse_time_range", - lambda *_args, **_kwargs: (None, None), + "application_services", + lambda: SimpleNamespace(app_statistics=statistics), ) - monkeypatch.setattr(statistic_module, "convert_datetime_to_date", lambda field: field) + monkeypatch.setattr( + statistic_module, + "current_account_with_tenant", + lambda: SimpleNamespace(account=SimpleNamespace(timezone="UTC")), + ) + monkeypatch.setattr(statistic_module, "parse_time_range", lambda *_args, **_kwargs: time_range) -def _json_payload(response: Any) -> dict[str, Any]: +def _invoke( + app: Flask, + resource_type: type, + *, + start: str | None = None, + end: str | None = None, +) -> dict[str, Any]: + resource = resource_type() + method = unwrap(resource.get) + with app.test_request_context("/console/api/apps/app-1/statistics", method="GET"): + response = method( + resource, + statistic_module.StatisticTimeRangeQuery(start=start, end=end), + _request_context(), + app_model=_app_model(), + ) return response if isinstance(response, dict) else response.get_json() -def test_daily_message_statistic_returns_rows(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyMessageStatistic() - method = unwrap(api.get) +@pytest.mark.parametrize( + ("resource_type", "query_call_getter", "record", "expected"), + [ + pytest.param( + statistic_module.DailyMessageStatistic, + lambda query: query.get_daily_messages, + DailyMessageStatisticRecord(date="2024-01-01", message_count=3), + {"date": "2024-01-01", "message_count": 3}, + id="daily-messages", + ), + pytest.param( + statistic_module.DailyConversationStatistic, + lambda query: query.get_daily_conversations, + DailyConversationStatisticRecord(date="2024-01-02", conversation_count=5), + {"date": "2024-01-02", "conversation_count": 5}, + id="daily-conversations", + ), + pytest.param( + statistic_module.DailyTerminalsStatistic, + lambda query: query.get_daily_terminals, + DailyTerminalStatisticRecord(date="2024-01-03", terminal_count=7), + {"date": "2024-01-03", "terminal_count": 7}, + id="daily-terminals", + ), + pytest.param( + statistic_module.DailyTokenCostStatistic, + lambda query: query.get_daily_token_costs, + DailyTokenCostStatisticRecord( + date="2024-01-04", + token_count=10, + total_price=Decimal("0.25"), + currency="USD", + ), + {"date": "2024-01-04", "token_count": 10, "total_price": "0.25", "currency": "USD"}, + id="daily-token-costs", + ), + pytest.param( + statistic_module.AverageSessionInteractionStatistic, + lambda query: query.get_average_session_interactions, + AverageSessionInteractionStatisticRecord(date="2024-01-05", interactions=2.5), + {"date": "2024-01-05", "interactions": 2.5}, + id="average-session-interactions", + ), + pytest.param( + statistic_module.UserSatisfactionRateStatistic, + lambda query: query.get_user_satisfaction_rates, + UserSatisfactionRateStatisticRecord(date="2024-01-06", rate=100.0), + {"date": "2024-01-06", "rate": 100.0}, + id="user-satisfaction-rate", + ), + pytest.param( + statistic_module.AverageResponseTimeStatistic, + lambda query: query.get_average_response_times, + AverageResponseTimeStatisticRecord(date="2024-01-07", latency=1234.0), + {"date": "2024-01-07", "latency": 1234.0}, + id="average-response-time", + ), + pytest.param( + statistic_module.TokensPerSecondStatistic, + lambda query: query.get_tokens_per_second, + TokensPerSecondStatisticRecord(date="2024-01-08", tps=15.5), + {"date": "2024-01-08", "tps": 15.5}, + id="tokens-per-second", + ), + ], +) +def test_statistic_endpoint_delegates_to_statistic_query( + app: Flask, + monkeypatch: pytest.MonkeyPatch, + resource_type: type, + query_call_getter: Callable[[MagicMock], MagicMock], + record: tuple, + expected: dict[str, Any], +) -> None: + statistics = MagicMock(spec=AppStatisticQuery) + query_call = query_call_getter(statistics) + query_call.return_value = [record] + _install_dependencies(monkeypatch, statistics) - rows = [SimpleNamespace(date="2024-01-01", message_count=3)] - _install_common(monkeypatch) - _install_db(monkeypatch, rows) - - with app.test_request_context("/console/api/apps/app-1/statistics/daily-messages", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - assert _json_payload(response) == {"data": [{"date": "2024-01-01", "message_count": 3}]} + assert _invoke(app, resource_type) == {"data": [expected]} + query_call.assert_called_once_with( + app_id="app-1", + start_date=None, + end_date=None, + timezone="UTC", + ) -def test_daily_conversation_statistic_returns_rows(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyConversationStatistic() - method = unwrap(api.get) +def test_statistic_endpoint_passes_time_range(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: + statistics = MagicMock(spec=AppStatisticQuery) + statistics.get_daily_messages.return_value = [] + start_date = datetime(2024, 1, 1, tzinfo=UTC) + end_date = datetime(2024, 1, 2, tzinfo=UTC) + _install_dependencies(monkeypatch, statistics, time_range=(start_date, end_date)) - rows = [SimpleNamespace(date="2024-01-02", conversation_count=5)] - _install_common(monkeypatch) - _install_db(monkeypatch, rows) - - with app.test_request_context("/console/api/apps/app-1/statistics/daily-conversations", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - assert _json_payload(response) == {"data": [{"date": "2024-01-02", "conversation_count": 5}]} + assert _invoke(app, statistic_module.DailyMessageStatistic, start="start", end="end") == {"data": []} + statistics.get_daily_messages.assert_called_once_with( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="UTC", + ) -def test_daily_token_cost_statistic_returns_rows(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyTokenCostStatistic() - method = unwrap(api.get) - - rows = [SimpleNamespace(date="2024-01-03", token_count=10, total_price=0.25, currency="USD")] - _install_common(monkeypatch) - _install_db(monkeypatch, rows) - - with app.test_request_context("/console/api/apps/app-1/statistics/token-costs", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - data = _json_payload(response) - assert len(data["data"]) == 1 - assert data["data"][0]["date"] == "2024-01-03" - assert data["data"][0]["token_count"] == 10 - assert Decimal(data["data"][0]["total_price"]) == Decimal("0.25") - - -def test_daily_terminals_statistic_returns_rows(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyTerminalsStatistic() - method = unwrap(api.get) - - rows = [SimpleNamespace(date="2024-01-04", terminal_count=7)] - _install_common(monkeypatch) - _install_db(monkeypatch, rows) - - with app.test_request_context("/console/api/apps/app-1/statistics/daily-end-users", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - assert _json_payload(response) == {"data": [{"date": "2024-01-04", "terminal_count": 7}]} - - -def test_average_session_interaction_statistic_requires_chat_mode(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - """Test that AverageSessionInteractionStatistic is limited to chat/agent modes.""" - # This just verifies the decorator is applied correctly - # Actual endpoint testing would require complex JOIN mocking - api = statistic_module.AverageSessionInteractionStatistic() - method = unwrap(api.get) - assert callable(method) - - -def test_daily_message_statistic_with_invalid_time_range(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyMessageStatistic() - method = unwrap(api.get) - - def mock_parse(*args, **kwargs): - raise ValueError("Invalid time range") - - _install_db(monkeypatch, []) - monkeypatch.setattr(statistic_module, "parse_time_range", mock_parse) - monkeypatch.setattr(statistic_module, "convert_datetime_to_date", lambda field: field) - - with app.test_request_context("/console/api/apps/app-1/statistics/daily-messages", method="GET"): - with pytest.raises(BadRequest): - method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - -def test_daily_message_statistic_multiple_rows(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyMessageStatistic() - method = unwrap(api.get) - - rows = [ - SimpleNamespace(date="2024-01-01", message_count=10), - SimpleNamespace(date="2024-01-02", message_count=15), - SimpleNamespace(date="2024-01-03", message_count=12), - ] - _install_common(monkeypatch) - _install_db(monkeypatch, rows) - - with app.test_request_context("/console/api/apps/app-1/statistics/daily-messages", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - data = _json_payload(response) - assert len(data["data"]) == 3 - - -def test_daily_message_statistic_empty_result(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyMessageStatistic() - method = unwrap(api.get) - - _install_common(monkeypatch) - _install_db(monkeypatch, []) - - with app.test_request_context("/console/api/apps/app-1/statistics/daily-messages", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - assert _json_payload(response) == {"data": []} - - -def test_daily_conversation_statistic_with_time_range(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyConversationStatistic() - method = unwrap(api.get) - - rows = [SimpleNamespace(date="2024-01-02", conversation_count=5)] - _install_db(monkeypatch, rows) +def test_statistic_endpoint_rejects_invalid_time_range(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: + statistics = MagicMock(spec=AppStatisticQuery) + _install_dependencies(monkeypatch, statistics) monkeypatch.setattr( statistic_module, "parse_time_range", - lambda *_args, **_kwargs: ("s", "e"), + MagicMock(side_effect=ValueError("Invalid time range")), ) - monkeypatch.setattr(statistic_module, "convert_datetime_to_date", lambda field: field) - with app.test_request_context("/console/api/apps/app-1/statistics/daily-conversations", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) + with pytest.raises(BadRequest, match="Invalid time range"): + _invoke(app, statistic_module.DailyMessageStatistic) - assert _json_payload(response) == {"data": [{"date": "2024-01-02", "conversation_count": 5}]} - - -def test_daily_token_cost_with_multiple_currencies(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: - api = statistic_module.DailyTokenCostStatistic() - method = unwrap(api.get) - - rows = [ - SimpleNamespace(date="2024-01-01", token_count=100, total_price=Decimal("0.50"), currency="USD"), - SimpleNamespace(date="2024-01-02", token_count=200, total_price=Decimal("1.00"), currency="USD"), - ] - _install_common(monkeypatch) - _install_db(monkeypatch, rows) - - with app.test_request_context("/console/api/apps/app-1/statistics/token-costs", method="GET"): - response = method( - api, - SimpleNamespace(start=None, end=None), - _account(), - app_model=_app_model(), - ) - - data = _json_payload(response) - assert len(data["data"]) == 2 + statistics.get_daily_messages.assert_not_called() diff --git a/api/tests/unit_tests/controllers/console/app/test_workflow_pause_details_api.py b/api/tests/unit_tests/controllers/console/app/test_workflow_pause_details_api.py index a1be8254d63..c5872bbf434 100644 --- a/api/tests/unit_tests/controllers/console/app/test_workflow_pause_details_api.py +++ b/api/tests/unit_tests/controllers/console/app/test_workflow_pause_details_api.py @@ -1,193 +1,111 @@ -"""Console workflow pause-detail tests backed by persisted workflow execution state.""" +"""Controller tests for Console workflow pause details.""" from __future__ import annotations -import inspect -from dataclasses import dataclass from datetime import datetime +from inspect import unwrap +from types import SimpleNamespace from unittest.mock import Mock -from uuid import uuid4 import pytest from flask import Flask -from sqlalchemy.engine import Engine -from sqlalchemy.orm import Session from controllers.common.errors import NotFoundError from controllers.console.app import workflow_run as workflow_run_module -from core.workflow.nodes.human_input.entities import ParagraphInputConfig, UserActionConfig -from core.workflow.nodes.human_input.pause_reason import HumanInputRequired -from graphon.enums import WorkflowExecutionStatus -from models.enums import CreatorUserRole, WorkflowRunTriggeredFrom -from models.workflow import WorkflowPause, WorkflowRun, WorkflowType +from machinery.context import RequestContext +from services.workflow_run_service import WorkflowRunPauseDetails, WorkflowRunPausedNode from tests.unit_tests.config_override import apply_config_overrides -@dataclass(frozen=True) -class _Database: - engine: Engine - session: Session - - -def _persist_run( - session: Session, - *, - run_id: str, - tenant_id: str, - status: WorkflowExecutionStatus, - paused: bool = False, -) -> WorkflowRun: - workflow_id = str(uuid4()) - workflow_run = WorkflowRun( - id=run_id, - tenant_id=tenant_id, - app_id=str(uuid4()), - workflow_id=workflow_id, - type=WorkflowType.WORKFLOW, - triggered_from=WorkflowRunTriggeredFrom.DEBUGGING, - version="draft", - graph="{}", - inputs="{}", - status=status, - created_by_role=CreatorUserRole.ACCOUNT, - created_by=str(uuid4()), - created_at=datetime(2024, 1, 1, 12, 0, 0), +def _request_context(*, workspace_id: str = "tenant-1") -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + active_workspace_id=workspace_id, ) - session.add(workflow_run) - if paused: - session.add( - WorkflowPause( - workflow_id=workflow_id, - workflow_run_id=run_id, - state_object_key="workflow-pauses/state.json", - ) - ) - session.commit() - return workflow_run -class _PauseEntity: - def __init__(self, paused_at: datetime, reasons: list[HumanInputRequired]): - self.paused_at = paused_at - self._reasons = reasons - - def get_pause_reasons(self): - return self._reasons - - -def test_pause_details_returns_backstage_input_url( - app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session -) -> None: - apply_config_overrides(monkeypatch, APP_WEB_URL="https://web.example.com") - - tenant_id = str(uuid4()) - run_id = str(uuid4()) - _persist_run( - sqlite_session, - run_id=run_id, - tenant_id=tenant_id, - status=WorkflowExecutionStatus.PAUSED, - paused=True, - ) +def _mock_application_services(monkeypatch: pytest.MonkeyPatch, workflow_runs: Mock) -> None: monkeypatch.setattr( workflow_run_module, - "db", - _Database(engine=sqlite_session.get_bind(), session=sqlite_session), + "application_services", + lambda: SimpleNamespace(workflow_runs=workflow_runs), ) - reason = HumanInputRequired( - form_id="form-1", - form_content="content", - inputs=[ParagraphInputConfig(output_variable_name="name")], - actions=[UserActionConfig(id="approve", title="Approve")], - node_id="node-1", - node_title="Ask Name", - ) - pause_entity = _PauseEntity(paused_at=datetime(2024, 1, 1, 12, 0, 0), reasons=[reason]) - repo = Mock() - repo.get_workflow_pause.return_value = pause_entity - monkeypatch.setattr( - workflow_run_module.DifyAPIRepositoryFactory, - "create_api_workflow_run_repository", - lambda *_, **__: repo, - ) - monkeypatch.setattr( - workflow_run_module, - "_load_form_tokens_by_form_id", - lambda _form_ids: {"form-1": "backstage-token"}, +def test_pause_details_returns_backstage_input_url(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: + apply_config_overrides(monkeypatch, APP_WEB_URL="https://web.example.com/") + workflow_runs = Mock() + workflow_runs.get_pause_details.return_value = WorkflowRunPauseDetails( + paused_at=datetime(2024, 1, 1, 12, 0, 0), + paused_nodes=( + WorkflowRunPausedNode( + node_id="node-1", + node_title="Ask Name", + form_id="form-1", + form_token="backstage-token", + ), + ), ) + _mock_application_services(monkeypatch, workflow_runs) + request_context = _request_context() - with app.test_request_context(f"/console/api/workflow/{run_id}/pause-details", method="GET"): - handler = inspect.unwrap(workflow_run_module.ConsoleWorkflowPauseDetailsApi.get) - response, status = handler( - workflow_run_module.ConsoleWorkflowPauseDetailsApi(), - tenant_id, - workflow_run_id=run_id, - ) + api = workflow_run_module.ConsoleWorkflowPauseDetailsApi() + handler = unwrap(api.get) + with app.test_request_context("/console/api/workflow/run-1/pause-details", method="GET"): + response, status = handler(api, request_context, workflow_run_id="run-1") assert status == 200 - assert response["paused_at"] == "2024-01-01T12:00:00Z" - assert response["paused_nodes"][0]["node_id"] == "node-1" - assert response["paused_nodes"][0]["pause_type"]["type"] == "human_input" - assert ( - response["paused_nodes"][0]["pause_type"]["backstage_input_url"] - == "https://web.example.com/form/backstage-token" - ) - assert "pending_human_inputs" not in response - - -def test_pause_details_tenant_isolation(app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None: - apply_config_overrides(monkeypatch, APP_WEB_URL="https://web.example.com") - - run_id = str(uuid4()) - _persist_run( - sqlite_session, - run_id=run_id, - tenant_id=str(uuid4()), - status=WorkflowExecutionStatus.PAUSED, - paused=True, - ) - monkeypatch.setattr( - workflow_run_module, - "db", - _Database(engine=sqlite_session.get_bind(), session=sqlite_session), + assert response == { + "paused_at": "2024-01-01T12:00:00Z", + "paused_nodes": [ + { + "node_id": "node-1", + "node_title": "Ask Name", + "pause_type": { + "type": "human_input", + "form_id": "form-1", + "backstage_input_url": "https://web.example.com/form/backstage-token", + }, + } + ], + } + workflow_runs.get_pause_details.assert_called_once_with( + request_context, + workflow_run_id="run-1", ) - handler = inspect.unwrap(workflow_run_module.ConsoleWorkflowPauseDetailsApi.get) - with app.test_request_context(f"/console/api/workflow/{run_id}/pause-details", method="GET"): - with pytest.raises(NotFoundError): - handler( - workflow_run_module.ConsoleWorkflowPauseDetailsApi(), - str(uuid4()), - workflow_run_id=run_id, - ) - -def test_pause_details_returns_empty_response_for_non_paused_run( - app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session +def test_pause_details_maps_missing_or_inaccessible_run_to_not_found( + app: Flask, monkeypatch: pytest.MonkeyPatch ) -> None: - tenant_id = str(uuid4()) - run_id = str(uuid4()) - _persist_run( - sqlite_session, - run_id=run_id, - tenant_id=tenant_id, - status=WorkflowExecutionStatus.RUNNING, - ) - monkeypatch.setattr( - workflow_run_module, - "db", - _Database(engine=sqlite_session.get_bind(), session=sqlite_session), + workflow_runs = Mock() + workflow_runs.get_pause_details.return_value = None + _mock_application_services(monkeypatch, workflow_runs) + request_context = _request_context(workspace_id="other-tenant") + api = workflow_run_module.ConsoleWorkflowPauseDetailsApi() + handler = unwrap(api.get) + + with app.test_request_context("/console/api/workflow/run-1/pause-details", method="GET"): + with pytest.raises(NotFoundError, match="Workflow run not found"): + handler(api, request_context, workflow_run_id="run-1") + + workflow_runs.get_pause_details.assert_called_once_with( + request_context, + workflow_run_id="run-1", ) - with app.test_request_context(f"/console/api/workflow/{run_id}/pause-details", method="GET"): - handler = inspect.unwrap(workflow_run_module.ConsoleWorkflowPauseDetailsApi.get) - response, status = handler( - workflow_run_module.ConsoleWorkflowPauseDetailsApi(), - tenant_id, - workflow_run_id=run_id, - ) + +def test_pause_details_returns_empty_response_for_non_paused_run(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: + workflow_runs = Mock() + workflow_runs.get_pause_details.return_value = WorkflowRunPauseDetails(paused_at=None, paused_nodes=()) + _mock_application_services(monkeypatch, workflow_runs) + + api = workflow_run_module.ConsoleWorkflowPauseDetailsApi() + handler = unwrap(api.get) + with app.test_request_context("/console/api/workflow/run-1/pause-details", method="GET"): + response, status = handler(api, _request_context(), workflow_run_id="run-1") assert status == 200 assert response == {"paused_at": None, "paused_nodes": []} diff --git a/api/tests/unit_tests/controllers/console/app/test_workflow_run_api.py b/api/tests/unit_tests/controllers/console/app/test_workflow_run_api.py index c1176d4155b..a9686f0cccf 100644 --- a/api/tests/unit_tests/controllers/console/app/test_workflow_run_api.py +++ b/api/tests/unit_tests/controllers/console/app/test_workflow_run_api.py @@ -3,15 +3,20 @@ from __future__ import annotations import json from datetime import UTC, datetime from inspect import unwrap +from types import SimpleNamespace from typing import Any +from unittest.mock import Mock import pytest from flask import Flask from flask_restx import marshal from sqlalchemy.orm import Session +from controllers.common.errors import NotFoundError from controllers.console.app import workflow_run as workflow_run_module +from extensions.ext_database import db from graphon.enums import WorkflowExecutionStatus, WorkflowNodeExecutionStatus +from machinery.context import RequestContext from models import Account, App, AppMode from models.enums import CreatorUserRole, WorkflowRunTriggeredFrom from models.model import IconType @@ -58,6 +63,23 @@ def _app() -> App: ) +def _request_context(*, workspace_id: str = "tenant-1") -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + active_workspace_id=workspace_id, + ) + + +def _mock_application_services(monkeypatch: pytest.MonkeyPatch, workflow_runs: Mock) -> None: + monkeypatch.setattr( + workflow_run_module, + "application_services", + lambda: SimpleNamespace(workflow_runs=workflow_runs), + ) + + def _workflow_run_summary(session: Session, **overrides: object) -> WorkflowRun: created_at = datetime(2026, 1, 2, 3, 4, 5, tzinfo=UTC) workflow_run = WorkflowRun( @@ -129,17 +151,15 @@ def test_workflow_run_list_returns_frontend_history_contract( ) -> None: _account(sqlite_session) workflow_run = _workflow_run_summary(sqlite_session) - - class WorkflowRunService: - def get_paginate_workflow_runs(self, **_kwargs): - return { - "limit": 10, - "has_more": False, - "data": [workflow_run], - } - - monkeypatch.setattr(workflow_run_module, "WorkflowRunService", WorkflowRunService) - monkeypatch.setattr(workflow_run_module.db, "session", sqlite_session) + workflow_runs = Mock() + workflow_runs.get_paginate_workflow_runs.return_value = { + "limit": 10, + "has_more": False, + "data": [workflow_run], + } + _mock_application_services(monkeypatch, workflow_runs) + monkeypatch.setattr(db, "session", sqlite_session) + request_context = _request_context() api = workflow_run_module.WorkflowRunListApi() handler = unwrap(api.get) @@ -148,6 +168,7 @@ def test_workflow_run_list_returns_frontend_history_contract( payload = handler( api, workflow_run_module.WorkflowRunListQuery(limit=10), + request_context, app_model=_app(), ) @@ -168,6 +189,12 @@ def test_workflow_run_list_returns_frontend_history_contract( "exceptions_count": 0, "retry_index": 0, } + workflow_runs.get_paginate_workflow_runs.assert_called_once_with( + request_context, + app_id="app-1", + args={"limit": 10}, + triggered_from=WorkflowRunTriggeredFrom.DEBUGGING, + ) def test_advanced_chat_workflow_run_list_keeps_message_fields( @@ -179,17 +206,15 @@ def test_advanced_chat_workflow_run_list_keeps_message_fields( conversation_id="conversation-1", message_id="message-1", ) - - class WorkflowRunService: - def get_paginate_advanced_chat_workflow_runs(self, **_kwargs): - return { - "limit": 1, - "has_more": True, - "data": [workflow_run], - } - - monkeypatch.setattr(workflow_run_module, "WorkflowRunService", WorkflowRunService) - monkeypatch.setattr(workflow_run_module.db, "session", sqlite_session) + workflow_runs = Mock() + workflow_runs.get_paginate_advanced_chat_workflow_runs.return_value = { + "limit": 1, + "has_more": True, + "data": [workflow_run], + } + _mock_application_services(monkeypatch, workflow_runs) + monkeypatch.setattr(db, "session", sqlite_session) + request_context = _request_context() api = workflow_run_module.AdvancedChatAppWorkflowRunListApi() handler = unwrap(api.get) @@ -198,6 +223,7 @@ def test_advanced_chat_workflow_run_list_keeps_message_fields( payload = handler( api, workflow_run_module.WorkflowRunListQuery(limit=1), + request_context, app_model=_app(), ) @@ -205,6 +231,52 @@ def test_advanced_chat_workflow_run_list_keeps_message_fields( assert response["data"][0]["conversation_id"] == "conversation-1" assert response["data"][0]["message_id"] == "message-1" + workflow_runs.get_paginate_advanced_chat_workflow_runs.assert_called_once_with( + request_context, + app_id="app-1", + args={"limit": 1}, + triggered_from=WorkflowRunTriggeredFrom.DEBUGGING, + ) + + +def test_workflow_run_count_passes_filters_to_application_service(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: + workflow_runs = Mock() + workflow_runs.get_workflow_runs_count.return_value = { + "total": 2, + "running": 0, + "succeeded": 2, + "failed": 0, + "stopped": 0, + "partial-succeeded": 0, + } + _mock_application_services(monkeypatch, workflow_runs) + request_context = _request_context() + query = workflow_run_module.WorkflowRunCountQuery( + status="succeeded", + time_range="7d", + triggered_from="app-run", + ) + + api = workflow_run_module.WorkflowRunCountApi() + handler = unwrap(api.get) + with app.test_request_context("/apps/app-1/workflow-runs/count", method="GET"): + payload = handler(api, query, request_context, app_model=_app()) + + assert payload == { + "total": 2, + "running": 0, + "succeeded": 2, + "failed": 0, + "stopped": 0, + "partial_succeeded": 0, + } + workflow_runs.get_workflow_runs_count.assert_called_once_with( + request_context, + app_id="app-1", + status="succeeded", + time_range="7d", + triggered_from=WorkflowRunTriggeredFrom.APP_RUN, + ) def test_workflow_run_detail_returns_frontend_detail_contract( @@ -212,19 +284,17 @@ def test_workflow_run_detail_returns_frontend_detail_contract( ) -> None: _account(sqlite_session) workflow_run = _workflow_run_summary(sqlite_session) - - class WorkflowRunService: - def get_workflow_run(self, **_kwargs): - return workflow_run - - monkeypatch.setattr(workflow_run_module, "WorkflowRunService", WorkflowRunService) - monkeypatch.setattr(workflow_run_module.db, "session", sqlite_session) + workflow_runs = Mock() + workflow_runs.get_workflow_run.return_value = workflow_run + _mock_application_services(monkeypatch, workflow_runs) + monkeypatch.setattr(db, "session", sqlite_session) + request_context = _request_context() api = workflow_run_module.WorkflowRunDetailApi() handler = unwrap(api.get) with app.test_request_context("/apps/app-1/workflow-runs/run-1", method="GET"): - payload = handler(api, app_model=_app(), run_id="run-1") + payload = handler(api, request_context, app_model=_app(), run_id="run-1") response = _serialize_200_response(api.get, payload) @@ -246,26 +316,48 @@ def test_workflow_run_detail_returns_frontend_detail_contract( "finished_at": 1767323045, "exceptions_count": 0, } + workflow_runs.get_workflow_run.assert_called_once_with( + request_context, + app_id="app-1", + run_id="run-1", + ) + + +def test_workflow_run_detail_maps_missing_run_to_not_found(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None: + workflow_runs = Mock() + workflow_runs.get_workflow_run.return_value = None + _mock_application_services(monkeypatch, workflow_runs) + request_context = _request_context(workspace_id="tenant-2") + api = workflow_run_module.WorkflowRunDetailApi() + handler = unwrap(api.get) + + with app.test_request_context("/apps/app-1/workflow-runs/run-1", method="GET"): + with pytest.raises(NotFoundError, match="Workflow run not found"): + handler(api, request_context, app_model=_app(), run_id="run-1") + + workflow_runs.get_workflow_run.assert_called_once_with( + request_context, + app_id="app-1", + run_id="run-1", + ) def test_workflow_run_node_executions_return_frontend_trace_contract( app: Flask, monkeypatch: pytest.MonkeyPatch, sqlite_session: Session ) -> None: - account = _account(sqlite_session) + _account(sqlite_session) execution = _workflow_run_node_execution(sqlite_session) - - class WorkflowRunService: - def get_workflow_run_node_executions(self, **_kwargs): - return [execution] - - monkeypatch.setattr(workflow_run_module, "WorkflowRunService", WorkflowRunService) - monkeypatch.setattr(workflow_run_module.db, "session", sqlite_session) + workflow_runs = Mock() + workflow_runs.get_workflow_run_node_executions.return_value = [execution] + _mock_application_services(monkeypatch, workflow_runs) + monkeypatch.setattr(db, "session", sqlite_session) + request_context = _request_context() api = workflow_run_module.WorkflowRunNodeExecutionListApi() handler = unwrap(api.get) with app.test_request_context("/apps/app-1/workflow-runs/run-1/node-executions", method="GET"): - payload = handler(api, account, app_model=_app(), run_id="run-1") + payload = handler(api, request_context, app_model=_app(), run_id="run-1") response = _serialize_200_response(api.get, payload) @@ -297,3 +389,8 @@ def test_workflow_run_node_executions_return_frontend_trace_contract( } ] } + workflow_runs.get_workflow_run_node_executions.assert_called_once_with( + request_context, + app_id="app-1", + run_id="run-1", + ) diff --git a/api/tests/unit_tests/controllers/console/explore/test_banner.py b/api/tests/unit_tests/controllers/console/explore/test_banner.py index 5c44ea4b8f5..9ade4ba82ad 100644 --- a/api/tests/unit_tests/controllers/console/explore/test_banner.py +++ b/api/tests/unit_tests/controllers/console/explore/test_banner.py @@ -110,7 +110,7 @@ class TestExploreBannerQueryService: assert service.list_for_language("fr-FR") == (record,) assert banners.requested_languages == ["fr-FR"] - def test_falls_back_to_en_us(self) -> None: + def test_falls_back_to_en_us_when_requested_translation_is_missing(self) -> None: record = _record(title="fallback") banners = FakeExploreBannerQuery({"en-US": (record,)}) service = ExploreBannerQueryService(banners=banners, enabled=True) @@ -118,6 +118,14 @@ class TestExploreBannerQueryService: assert service.list_for_language("es-ES") == (record,) assert banners.requested_languages == ["es-ES", "en-US"] + def test_invalid_language_uses_en_us(self) -> None: + record = _record(title="fallback") + banners = FakeExploreBannerQuery({"en-US": (record,)}) + service = ExploreBannerQueryService(banners=banners, enabled=True) + + assert service.list_for_language("invalid") == (record,) + assert banners.requested_languages == ["en-US"] + def test_does_not_repeat_default_language_query(self) -> None: banners = FakeExploreBannerQuery() service = ExploreBannerQueryService(banners=banners, enabled=True) diff --git a/api/tests/unit_tests/controllers/console/explore/test_recommended_app.py b/api/tests/unit_tests/controllers/console/explore/test_recommended_app.py index fccbba38d6d..e281a22caac 100644 --- a/api/tests/unit_tests/controllers/console/explore/test_recommended_app.py +++ b/api/tests/unit_tests/controllers/console/explore/test_recommended_app.py @@ -7,7 +7,7 @@ from flask import Flask from pydantic import ValidationError import controllers.console.explore.recommended_app as module -from models import Account +from machinery.context import RequestContext from models.model import AppMode, IconType from services.recommended_app_query_service import ( LearnDifyAppListResult, @@ -21,11 +21,13 @@ from services.recommended_app_query_service import ( ) -def make_account(interface_language: str | None) -> Account: - account = Account(name="Test User", email="user@example.com") - account.id = "account-1" - account.interface_language = interface_language - return account +def _request_context() -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + active_workspace_id="workspace-1", + ) class TestRecommendedAppListApi: @@ -44,17 +46,16 @@ class TestRecommendedAppListApi: return_value=SimpleNamespace(recommended_app_queries=queries), ), ): - result = method(api, module.RecommendedAppsQuery(language="en-US"), make_account("fr-FR")) + result = method(api, module.RecommendedAppsQuery(language="en-US"), _request_context()) queries.list_recommended.assert_called_once_with( - requested_language="en-US", - interface_language="fr-FR", + language="en-US", ) assert result == {"recommended_apps": [], "categories": []} class TestLearnDifyAppListApi: - def test_get_with_language_param(self, app: Flask) -> None: + def test_get_uses_default_language(self, app: Flask) -> None: api = module.LearnDifyAppListApi() method = unwrap(api.get) @@ -62,18 +63,17 @@ class TestLearnDifyAppListApi: queries.list_learn_dify.return_value = LearnDifyAppListResult(recommended_apps=()) with ( - app.test_request_context("/", query_string={"language": "en-US"}), + app.test_request_context("/"), patch.object( module, "application_services", return_value=SimpleNamespace(recommended_app_queries=queries), ), ): - result = method(api, module.RecommendedAppsQuery(language="en-US"), make_account("fr-FR")) + result = method(api, module.RecommendedAppsQuery(), _request_context()) queries.list_learn_dify.assert_called_once_with( - requested_language="en-US", - interface_language="fr-FR", + language="en-US", ) assert result == {"recommended_apps": []} @@ -102,7 +102,7 @@ class TestRecommendedAppApi: return_value=SimpleNamespace(recommended_app_queries=queries), ), ): - result = method(api, "11111111-1111-1111-1111-111111111111") + result = method(api, _request_context(), "11111111-1111-1111-1111-111111111111") queries.get_detail.assert_called_once_with("11111111-1111-1111-1111-111111111111") assert result == { @@ -130,7 +130,7 @@ class TestRecommendedAppApi: ), ): with pytest.raises(module.RecommendedAppNotFoundError) as exc_info: - method(api, "11111111-1111-1111-1111-111111111111") + method(api, _request_context(), "11111111-1111-1111-1111-111111111111") assert exc_info.value.data == { "code": "recommended_app_not_found", diff --git a/api/tests/unit_tests/controllers/console/test_notification.py b/api/tests/unit_tests/controllers/console/test_notification.py index 48843d1af8a..8a37c06c1a0 100644 --- a/api/tests/unit_tests/controllers/console/test_notification.py +++ b/api/tests/unit_tests/controllers/console/test_notification.py @@ -2,6 +2,9 @@ from inspect import unwrap from types import SimpleNamespace from unittest.mock import Mock, patch +import pytest +from flask import Flask + from controllers.console.notification import ( DismissNotificationPayload, NotificationApi, @@ -20,7 +23,15 @@ def _request_context() -> RequestContext: ) -def test_get_notification_delegates_and_serializes_result() -> None: +@pytest.mark.parametrize( + ("query_string", "expected_language"), + [({}, "en-US"), ({"language": "zh-Hans"}, "zh-Hans")], +) +def test_get_notification_validates_language_query_and_serializes_result( + app: Flask, + query_string: dict[str, str], + expected_language: str, +) -> None: service = Mock() service.get_active.return_value = NotificationResult( should_show=True, @@ -38,10 +49,13 @@ def test_get_notification_delegates_and_serializes_result() -> None: ) services = SimpleNamespace(notifications=service) api = NotificationApi() - method = unwrap(api.get) + method = api.get.__wrapped__ context = _request_context() - with patch("controllers.console.notification.application_services", return_value=services): + with ( + app.test_request_context("/notification", query_string=query_string), + patch("controllers.console.notification.application_services", return_value=services), + ): result, status = method(api, context) assert status == 200 @@ -59,7 +73,7 @@ def test_get_notification_delegates_and_serializes_result() -> None: } ], } - service.get_active.assert_called_once_with(context) + service.get_active.assert_called_once_with(context, expected_language) def test_dismiss_notification_delegates_with_stable_account_context() -> None: diff --git a/api/tests/unit_tests/controllers/console/test_workspace_credential_mutation_permissions.py b/api/tests/unit_tests/controllers/console/test_workspace_credential_mutation_permissions.py index e6082a2e8b8..9714a7cceb9 100644 --- a/api/tests/unit_tests/controllers/console/test_workspace_credential_mutation_permissions.py +++ b/api/tests/unit_tests/controllers/console/test_workspace_credential_mutation_permissions.py @@ -9,6 +9,7 @@ from controllers.console.agent.roster import AgentAppApi from controllers.console.datasets.data_source import DataSourceApi from controllers.console.datasets.rag_pipeline.datasource_auth import DatasourceAuth from controllers.console.workspace.model_providers import ModelProviderCredentialApi +from controllers.console.workspace.models import ModelProviderModelCredentialApi from controllers.console.workspace.tool_providers import ToolBuiltinProviderAddApi, ToolOAuthCustomClient @@ -37,6 +38,7 @@ def test_workspace_credential_mutations_require_management_permission( "method", [ ModelProviderCredentialApi.get, + ModelProviderModelCredentialApi.get, ], ) def test_model_provider_credential_get_requires_admin_and_rbac( diff --git a/api/tests/unit_tests/controllers/console/workspace/test_workspace.py b/api/tests/unit_tests/controllers/console/workspace/test_workspace.py index dadfa2a23a5..2bf35fc6917 100644 --- a/api/tests/unit_tests/controllers/console/workspace/test_workspace.py +++ b/api/tests/unit_tests/controllers/console/workspace/test_workspace.py @@ -201,6 +201,7 @@ class TestWorkspaceQueryRepository: TenantAccountJoin( tenant_id=earlier.id, account_id="account-1", + current=True, last_opened_at=last_opened_at, ), TenantAccountJoin(tenant_id=later.id, account_id="account-1"), @@ -214,6 +215,7 @@ class TestWorkspaceQueryRepository: repository = WorkspaceQueryRepository(workspace_session.session_factory) result = repository.list_for_account("account-1") membership_ids = repository.list_ids_for_account("account-1") + access_workspaces = repository.list_account_access_workspaces("account-1") assert repository.has_active_for_account("account-1") is True assert repository.has_active_for_account("missing-account") is False @@ -234,6 +236,10 @@ class TestWorkspaceQueryRepository: ), ) assert set(membership_ids) == {earlier.id, later.id, archived.id} + access_by_id = {workspace.id: workspace for workspace in access_workspaces} + assert set(access_by_id) == {earlier.id, later.id, archived.id} + assert access_by_id[earlier.id].current is True + assert access_by_id[earlier.id].role == "normal" assert repository.has_active_membership("account-1") is True assert repository.has_active_membership("account-3") is False diff --git a/api/tests/unit_tests/controllers/files/test_appdeploy_files.py b/api/tests/unit_tests/controllers/files/test_appdeploy_files.py new file mode 100644 index 00000000000..b9e7d6e289e --- /dev/null +++ b/api/tests/unit_tests/controllers/files/test_appdeploy_files.py @@ -0,0 +1,833 @@ +"""Tests for the file endpoints reached with an AppDeploy file grant.""" + +import time +from collections.abc import Callable, Iterator +from datetime import datetime +from io import BytesIO +from types import SimpleNamespace +from typing import IO, cast +from unittest.mock import MagicMock, patch +from uuid import UUID + +import jwt +import pytest +from flask import Flask +from sqlalchemy import update +from sqlalchemy.engine import Engine +from sqlalchemy.orm import Session, sessionmaker + +from controllers.common.errors import ( + BlockedFileExtensionError, + FilenameNotExistsError, + FileTooLargeError, + NoFileUploadedError, + TooManyFilesError, +) +from controllers.files.appdeploy_files import ( + GrantedFileContentApi, + GrantedFileResolveApi, + GrantedFileUploadApi, + GrantedRemoteFileUploadApi, + InvalidFileRequestError, + ProducedFileApi, +) +from controllers.files.wraps import FileGrantInvalidError, GrantedFileNotFoundError +from extensions.ext_application_services import _build_file_grant_service +from extensions.storage.storage_type import StorageType +from libs.datetime_utils import naive_utc_now +from models.enums import CreatorUserRole, EndUserType +from models.model import EndUser, UploadFile +from models.tools import ToolFile +from services.entities.file_grant_entities import FileGrantContext, FileGrantScope, FileKind, RemoteFile +from services.errors.file import FileTooLargeError as FileTooLargeServiceError +from services.file_grant_gateways import FILE_CONTENT_AUDIENCE, FileGrantFileGateway +from services.file_grant_service import MAX_FILE_GRANT_REFS, FileGrantService +from tests.unit_tests.file_grant_test_utils import issue_file_grant + +CONTROLLER_MODULE = "controllers.files.appdeploy_files" + +SECRET_KEY = "file-grant-test-secret-long-enough-for-hs256" +TENANT_ID = "11111111-1111-4111-8111-111111111111" +OTHER_TENANT_ID = "1a1a1a1a-1111-4111-8111-111111111111" +APP_ID = "22222222-2222-4222-8222-222222222222" +FILE_ID = UUID("66666666-6666-4666-8666-666666666666") +UPLOAD_FILE_ID = "77777777-7777-4777-8777-777777777777" +UPLOADED_AT = datetime(2026, 8, 20, 12, 0) + +# What ``POST /v1/files/upload`` answers with. The grant channel is a drop-in +# for it, so the whole key set travels together and a key dify leaves null must +# still be present and null here. +DIFY_UPLOAD_RESPONSE_KEYS = frozenset( + { + "id", + "reference", + "name", + "size", + "extension", + "mime_type", + "created_by", + "created_at", + "preview_url", + "source_url", + "original_url", + "user_id", + "tenant_id", + "conversation_id", + "file_key", + } +) + +# The subset dify's own web upload client reads, from +# ``web/app/components/base/file-uploader/utils.ts``. +WEB_CLIENT_KEYS = frozenset( + { + "id", + "name", + "size", + "extension", + "mime_type", + "created_by", + "created_at", + "preview_url", + "source_url", + } +) + + +@pytest.fixture(autouse=True) +def granted_config(config_overrides: Callable[..., None]) -> None: + config_overrides( + SECRET_KEY=SECRET_KEY, + FILES_URL="https://files.example.com", + INTERNAL_FILES_URL="http://dify-api.dify.svc:5001", + FILES_ACCESS_TIMEOUT=300, + ) + + +@pytest.fixture +def sqlite_db(sqlite_engine: Engine) -> Iterator[FileGrantService]: + service = _build_file_grant_service(database_client=sessionmaker(bind=sqlite_engine, expire_on_commit=False)) + services = SimpleNamespace(file_grants=service) + with ( + patch(f"{CONTROLLER_MODULE}.application_services", return_value=services), + patch("controllers.files.wraps.application_services", return_value=services), + ): + yield service + + +@pytest.fixture +def file_gateway(sqlite_db: FileGrantService) -> FileGrantFileGateway: + return cast(FileGrantFileGateway, sqlite_db._files) + + +@pytest.fixture +def end_user(sqlite_session: Session) -> EndUser: + record = EndUser( + tenant_id=TENANT_ID, + app_id=APP_ID, + type=EndUserType.APP_DEPLOY, + is_anonymous=True, + session_id="seeded", + external_user_id="adp1.seeded", + ) + sqlite_session.add(record) + sqlite_session.commit() + return record + + +def _bearer(*scopes: FileGrantScope, end_user_id: str, tenant_id: str = TENANT_ID) -> dict[str, str]: + token, _ = issue_file_grant( + end_user_id=end_user_id, + tenant_id=tenant_id, + app_id=APP_ID, + scopes=scopes, + ttl_seconds=600, + ) + return {"Authorization": f"Bearer {token}"} + + +def _content_token(*, file_id: str, kind: FileKind, expires_in: int = 300) -> str: + return jwt.encode( + { + "aud": FILE_CONTENT_AUDIENCE, + "kind": str(kind), + "file_id": file_id, + "nonce": "0011223344556677", + "exp": int(time.time()) + expires_in, + }, + SECRET_KEY, + algorithm="HS256", + ) + + +def _stub_upload_file(**overrides: object) -> SimpleNamespace: + """Stand in for the ``upload_files`` row ``FileService`` hands back.""" + + return SimpleNamespace( + **{ + "id": UPLOAD_FILE_ID, + "name": "report.pdf", + "size": 2048, + "extension": "pdf", + "mime_type": "application/pdf", + "tenant_id": TENANT_ID, + "created_by": "99999999-9999-4999-8999-999999999999", + "created_at": UPLOADED_AT, + "source_url": "", + **overrides, + } + ) + + +def _persist_upload_file(session: Session, *, owner_id: str, tenant_id: str = TENANT_ID) -> UploadFile: + upload_file = UploadFile( + tenant_id=tenant_id, + storage_type=StorageType.OPENDAL, + key="upload_files/report.pdf", + name="report.pdf", + size=2048, + extension="pdf", + mime_type="application/pdf", + created_by=owner_id, + created_by_role=CreatorUserRole.END_USER, + created_at=naive_utc_now(), + used=False, + ) + session.add(upload_file) + session.commit() + return upload_file + + +def _persist_tool_file(session: Session, *, owner_id: str, mimetype: str = "image/png") -> ToolFile: + tool_file = ToolFile( + user_id=owner_id, + tenant_id=TENANT_ID, + conversation_id=None, + file_key="tools/chart.png", + mimetype=mimetype, + name="chart.png", + size=64, + ) + session.add(tool_file) + session.commit() + return tool_file + + +def test_upload_stores_the_file_for_the_grant_end_user( + app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway +) -> None: + with ( + patch.object(file_gateway, "store_upload", wraps=file_gateway.store_upload) as store_upload, + patch.object(file_gateway._file_service, "upload_file", return_value=_stub_upload_file()), + ): + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")}, + content_type="multipart/form-data", + ): + body, status = GrantedFileUploadApi().post() + + assert status == 201 + assert body["id"] == UPLOAD_FILE_ID + assert body["extension"] == "pdf" + assert store_upload.call_args.kwargs["context"].end_user_id == end_user.id + + +def test_upload_answers_in_dify_s_own_upload_shape( + app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway +) -> None: + """A client moving off ``POST /v1/files/upload`` must not meet a second shape.""" + + with patch.object( + file_gateway._file_service, + "upload_file", + return_value=_stub_upload_file(created_by=end_user.id), + ): + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")}, + content_type="multipart/form-data", + ): + body, _ = GrantedFileUploadApi().post() + + source_url = body.pop("source_url") + assert body == { + "id": UPLOAD_FILE_ID, + "reference": None, + "name": "report.pdf", + "size": 2048, + "extension": "pdf", + "mime_type": "application/pdf", + "created_by": end_user.id, + "created_at": int(UPLOADED_AT.timestamp()), + "preview_url": None, + "original_url": None, + "user_id": None, + "tenant_id": TENANT_ID, + "conversation_id": None, + "file_key": None, + } + assert source_url.startswith(f"https://files.example.com/files/appdeploy/{UPLOAD_FILE_ID}/content?token=") + + +def test_upload_carries_every_key_dify_s_web_client_reads( + app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway +) -> None: + with patch.object(file_gateway._file_service, "upload_file", return_value=_stub_upload_file()): + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")}, + content_type="multipart/form-data", + ): + body, _ = GrantedFileUploadApi().post() + + assert set(body) >= WEB_CLIENT_KEYS + + +@pytest.mark.usefixtures("sqlite_db") +def test_upload_rejects_a_grant_from_another_tenant(app: Flask, end_user: EndUser) -> None: + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id, tenant_id=OTHER_TENANT_ID), + data={"file": (BytesIO(b"pdf-bytes"), "report.pdf")}, + content_type="multipart/form-data", + ): + with pytest.raises(GrantedFileNotFoundError): + GrantedFileUploadApi().post() + + +@pytest.mark.usefixtures("sqlite_db") +def test_upload_applies_the_shared_per_extension_size_limit( + app: Flask, end_user: EndUser, config_overrides: Callable[..., None] +) -> None: + config_overrides(UPLOAD_FILE_SIZE_LIMIT=1) + + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"file": (BytesIO(b"0" * (1024 * 1024 + 1)), "big.bin")}, + content_type="multipart/form-data", + ): + with pytest.raises(FileTooLargeError): + GrantedFileUploadApi().post() + + +@pytest.mark.usefixtures("sqlite_db") +def test_upload_rejects_a_blacklisted_extension( + app: Flask, end_user: EndUser, config_overrides: Callable[..., None] +) -> None: + config_overrides(inner_UPLOAD_FILE_EXTENSION_BLACKLIST="exe") + + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"file": (BytesIO(b"MZ"), "payload.exe")}, + content_type="multipart/form-data", + ): + with pytest.raises(BlockedFileExtensionError): + GrantedFileUploadApi().post() + + +@pytest.mark.usefixtures("sqlite_db") +def test_upload_rejects_a_request_carrying_no_file(app: Flask, end_user: EndUser) -> None: + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"note": "no file here"}, + content_type="multipart/form-data", + ): + with pytest.raises(NoFileUploadedError): + GrantedFileUploadApi().post() + + +@pytest.mark.usefixtures("sqlite_db") +def test_upload_rejects_a_batch_of_files(app: Flask, end_user: EndUser) -> None: + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={ + "file": (BytesIO(b"first"), "first.pdf"), + "second": (BytesIO(b"second"), "second.pdf"), + }, + content_type="multipart/form-data", + ): + with pytest.raises(TooManyFilesError): + GrantedFileUploadApi().post() + + +@pytest.mark.usefixtures("sqlite_db") +def test_upload_rejects_a_file_without_a_name(app: Flask, end_user: EndUser) -> None: + with app.test_request_context( + "/files/appdeploy/upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + data={"file": (BytesIO(b"nameless"), "")}, + content_type="multipart/form-data", + ): + with pytest.raises(FilenameNotExistsError): + GrantedFileUploadApi().post() + + +def test_remote_upload_fetches_through_the_ssrf_safe_fetcher( + app: Flask, end_user: EndUser, sqlite_db: FileGrantService, file_gateway: FileGrantFileGateway +) -> None: + url = "https://example.com/docs/report.pdf" + + with ( + patch.object( + sqlite_db._remote_files, + "fetch", + return_value=RemoteFile(filename="report.pdf", mimetype="application/pdf", content=b"pdf-bytes"), + ) as fetch, + patch.object(file_gateway._file_service, "upload_file", return_value=_stub_upload_file()) as upload_file, + ): + with app.test_request_context( + "/files/appdeploy/remote-upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + json={"url": url}, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = {"url": url} + body, status = GrantedRemoteFileUploadApi().post() + + assert status == 201 + assert body["id"] == UPLOAD_FILE_ID + fetch.assert_called_once_with(url) + kwargs = upload_file.call_args.kwargs + assert kwargs["source_url"] == url + assert kwargs["content"] == b"pdf-bytes" + assert kwargs["user"].id == end_user.id + + +def test_remote_upload_answers_in_the_upload_shape_plus_dify_s_url_key( + app: Flask, end_user: EndUser, sqlite_db: FileGrantService, file_gateway: FileGrantFileGateway +) -> None: + """Dify's own remote upload answers under ``url``, so this one answers under both.""" + + url = "https://example.com/docs/report.pdf" + with ( + patch.object( + sqlite_db._remote_files, + "fetch", + return_value=RemoteFile(filename="report.pdf", mimetype="application/pdf", content=b"pdf-bytes"), + ), + patch.object( + file_gateway._file_service, + "upload_file", + return_value=_stub_upload_file(source_url=url), + ), + ): + with app.test_request_context( + "/files/appdeploy/remote-upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + json={"url": url}, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = {"url": url} + body, _ = GrantedRemoteFileUploadApi().post() + + assert set(body) == DIFY_UPLOAD_RESPONSE_KEYS | {"url"} + # The row records where the bytes came from; the response hands back the + # signed URL that fetches them, exactly as dify's own remote upload does. + assert body["source_url"].startswith(f"https://files.example.com/files/appdeploy/{UPLOAD_FILE_ID}/content?token=") + # One URL under both names, not two signings of the same file. + assert body["url"] == body["source_url"] + + +def test_remote_upload_honours_the_size_precheck(app: Flask, end_user: EndUser, sqlite_db: FileGrantService) -> None: + url = "https://example.com/docs/huge.pdf" + with patch.object( + sqlite_db._remote_files, + "fetch", + side_effect=FileTooLargeServiceError("too large"), + ): + with app.test_request_context( + "/files/appdeploy/remote-upload", + method="POST", + headers=_bearer(FileGrantScope.UPLOAD, end_user_id=end_user.id), + json={"url": url}, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = {"url": url} + with pytest.raises(FileTooLargeError): + GrantedRemoteFileUploadApi().post() + + +def test_produced_stores_a_tool_file_and_returns_both_urls( + app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway +) -> None: + tool_file = SimpleNamespace( + id="88888888-8888-4888-8888-888888888888", + name="chart.png", + size=64, + mimetype="image/png", + ) + + with patch.object(file_gateway._tool_files, "create_file_by_raw", return_value=tool_file) as create_file: + with app.test_request_context( + "/files/appdeploy/produced", + method="POST", + headers=_bearer(FileGrantScope.PRODUCE, end_user_id=end_user.id), + data={"file": (BytesIO(b"0" * 16), "chart.png")}, + content_type="multipart/form-data", + ): + body, status = ProducedFileApi().post() + + assert status == 201 + kwargs = create_file.call_args.kwargs + assert kwargs["user_id"] == end_user.id + assert kwargs["tenant_id"] == TENANT_ID + assert kwargs["conversation_id"] is None + assert body["url"].startswith(f"https://files.example.com/files/appdeploy/{tool_file.id}/content?token=") + assert body["internal_url"].startswith( + f"http://dify-api.dify.svc:5001/files/appdeploy/{tool_file.id}/content?token=" + ) + + +def test_produced_rejects_a_grant_whose_subject_was_deleted(app: Flask, file_gateway: FileGrantFileGateway) -> None: + with patch.object(file_gateway._tool_files, "create_file_by_raw") as create_file: + with app.test_request_context( + "/files/appdeploy/produced", + method="POST", + headers=_bearer( + FileGrantScope.PRODUCE, + end_user_id="99999999-9999-4999-8999-999999999999", + ), + data={"file": (BytesIO(b"content"), "chart.png")}, + content_type="multipart/form-data", + ): + with pytest.raises(GrantedFileNotFoundError): + ProducedFileApi().post() + + create_file.assert_not_called() + + +@pytest.fixture +def one_megabyte_image_limit(config_overrides: Callable[..., None]) -> int: + """Hold images to one mebibyte while every other kind stays far above it.""" + + config_overrides( + UPLOAD_FILE_SIZE_LIMIT=64, + UPLOAD_IMAGE_FILE_SIZE_LIMIT=1, + UPLOAD_VIDEO_FILE_SIZE_LIMIT=64, + UPLOAD_AUDIO_FILE_SIZE_LIMIT=64, + ) + return 1024 * 1024 + + +def test_produced_accepts_a_file_of_exactly_the_per_extension_limit( + app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway, one_megabyte_image_limit: int +) -> None: + with patch.object( + file_gateway._tool_files, + "create_file_by_raw", + return_value=SimpleNamespace( + id="88888888-8888-4888-8888-888888888888", + name="chart.png", + size=one_megabyte_image_limit, + mimetype="image/png", + ), + ) as create_file: + with app.test_request_context( + "/files/appdeploy/produced", + method="POST", + headers=_bearer(FileGrantScope.PRODUCE, end_user_id=end_user.id), + data={"file": (BytesIO(b"0" * one_megabyte_image_limit), "chart.png")}, + content_type="multipart/form-data", + ): + _, status = ProducedFileApi().post() + + assert status == 201 + assert len(create_file.call_args.kwargs["file_binary"]) == one_megabyte_image_limit + + +def test_produced_rejects_a_file_one_byte_over_the_per_extension_limit( + app: Flask, end_user: EndUser, file_gateway: FileGrantFileGateway, one_megabyte_image_limit: int +) -> None: + """``create_file_by_raw`` has no limit of its own, so nothing else would stop this.""" + + with patch.object(file_gateway._tool_files, "create_file_by_raw") as create_file: + with app.test_request_context( + "/files/appdeploy/produced", + method="POST", + headers=_bearer(FileGrantScope.PRODUCE, end_user_id=end_user.id), + data={"file": (BytesIO(b"0" * (one_megabyte_image_limit + 1)), "chart.png")}, + content_type="multipart/form-data", + ): + with pytest.raises(FileTooLargeError) as raised: + ProducedFileApi().post() + + assert raised.value.code == 413 + create_file.assert_not_called() + + +class _CountingStream: + """A body that reports how much of itself a reader actually pulled.""" + + def __init__(self, size: int) -> None: + self._remaining = size + self.bytes_read = 0 + + def read(self, size: int = -1) -> bytes: + served = self._remaining if size < 0 else min(size, self._remaining) + self._remaining -= served + self.bytes_read += served + return b"0" * served + + +def test_produced_stops_reading_an_oversized_body_at_the_per_extension_limit( + end_user: EndUser, + sqlite_db: FileGrantService, + file_gateway: FileGrantFileGateway, + one_megabyte_image_limit: int, +) -> None: + """The caller is a worker running plugin code with no proxy body limit in front of it.""" + + stream = _CountingStream(one_megabyte_image_limit * 64) + + with patch.object(file_gateway._tool_files, "create_file_by_raw") as create_file: + with pytest.raises(FileTooLargeServiceError): + sqlite_db.store_produced( + context=FileGrantContext(TENANT_ID, APP_ID, end_user.id), + filename="chart.png", + stream=cast(IO[bytes], stream), + mimetype="image/png", + ) + + assert stream.bytes_read == one_megabyte_image_limit + 1 + create_file.assert_not_called() + + +@pytest.mark.usefixtures("sqlite_db") +def test_resolve_signs_urls_per_item_and_hides_foreign_files( + app: Flask, end_user: EndUser, sqlite_session: Session +) -> None: + owned = _persist_upload_file(sqlite_session, owner_id=end_user.id) + foreign = _persist_upload_file(sqlite_session, owner_id="00000000-0000-4000-8000-000000000000") + payload = { + "files": [ + {"id": owned.id, "kind": "upload"}, + {"id": foreign.id, "kind": "upload"}, + ] + } + + with app.test_request_context( + "/files/appdeploy/resolve", + method="POST", + headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id), + json=payload, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = payload + body = GrantedFileResolveApi().post() + + resolved, hidden = body["files"] + assert resolved["ok"] is True + assert resolved["kind"] == "upload" + assert resolved["extension"] == "pdf" + assert resolved["url"].startswith(f"https://files.example.com/files/appdeploy/{owned.id}/content?token=") + assert resolved["internal_url"].startswith( + f"http://dify-api.dify.svc:5001/files/appdeploy/{owned.id}/content?token=" + ) + assert hidden == { + "id": foreign.id, + "ok": False, + "kind": None, + "name": None, + "size": None, + "extension": None, + "mime_type": None, + "url": None, + "internal_url": None, + "error": "not_found", + } + + +@pytest.mark.usefixtures("sqlite_db") +def test_resolve_answers_a_mixed_batch_item_by_item(app: Flask, end_user: EndUser, sqlite_session: Session) -> None: + owned_upload = _persist_upload_file(sqlite_session, owner_id=end_user.id) + owned_tool = _persist_tool_file(sqlite_session, owner_id=end_user.id) + missing_id = "44444444-4444-4444-8444-444444444444" + payload = { + "files": [ + {"id": owned_upload.id, "kind": "upload"}, + {"id": missing_id, "kind": "upload"}, + # A real file, but looked up in the wrong table. + {"id": owned_upload.id, "kind": "tool"}, + {"id": owned_tool.id, "kind": "tool"}, + ] + } + + with app.test_request_context( + "/files/appdeploy/resolve", + method="POST", + headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id), + json=payload, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = payload + body = GrantedFileResolveApi().post() + + assert [(file["id"], file["ok"], file["kind"], file["error"]) for file in body["files"]] == [ + (owned_upload.id, True, "upload", None), + (missing_id, False, None, "not_found"), + (owned_upload.id, False, None, "not_found"), + (owned_tool.id, True, "tool", None), + ] + + +@pytest.mark.usefixtures("sqlite_db") +def test_resolve_returns_an_empty_batch_unchanged(app: Flask, end_user: EndUser) -> None: + payload: dict[str, object] = {"files": []} + + with app.test_request_context( + "/files/appdeploy/resolve", + method="POST", + headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id), + json=payload, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = payload + assert GrantedFileResolveApi().post() == {"files": []} + + +def test_resolve_rejects_an_unbounded_batch(app: Flask, end_user: EndUser, sqlite_db: FileGrantService) -> None: + del sqlite_db + payload = { + "files": [ + {"id": f"00000000-0000-4000-8000-{index:012d}", "kind": "upload"} + for index in range(MAX_FILE_GRANT_REFS + 1) + ] + } + with app.test_request_context( + "/files/appdeploy/resolve", + method="POST", + headers=_bearer(FileGrantScope.RESOLVE, end_user_id=end_user.id), + json=payload, + ): + with patch(f"{CONTROLLER_MODULE}.files_ns") as files_ns: + files_ns.payload = payload + with pytest.raises(InvalidFileRequestError): + GrantedFileResolveApi().post() + + +@pytest.fixture +def stored_bytes(file_gateway: FileGrantFileGateway) -> Iterator[MagicMock]: + with patch.object(file_gateway._storage, "load", return_value=iter([b"file-bytes"])) as load: + yield load + + +@pytest.mark.usefixtures("sqlite_db", "stored_bytes") +@pytest.mark.parametrize( + ("mime_type", "expected_content_type", "expects_attachment"), + [ + ("image/png", "image/png", False), + ("image/jpeg", "image/jpeg", False), + ("image/gif", "image/gif", False), + ("image/webp", "image/webp", False), + # Case and parameters are normalized before the whitelist is consulted. + ("IMAGE/PNG; charset=binary", "image/png", False), + ("application/pdf", "application/octet-stream", True), + ("image/svg+xml", "application/octet-stream", True), + ("text/html", "application/octet-stream", True), + ("application/xhtml+xml", "application/octet-stream", True), + ("text/javascript", "application/octet-stream", True), + ("audio/mpeg", "application/octet-stream", True), + ("video/mp4", "application/octet-stream", True), + ], +) +def test_content_disposition_follows_the_inline_whitelist( + app: Flask, + sqlite_session: Session, + mime_type: str, + expected_content_type: str, + expects_attachment: bool, +) -> None: + tool_file = _persist_tool_file(sqlite_session, owner_id="anyone", mimetype=mime_type) + token = _content_token(file_id=tool_file.id, kind=FileKind.TOOL) + + with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}): + response = GrantedFileContentApi().get(UUID(tool_file.id)) + + assert response.headers["X-Content-Type-Options"] == "nosniff" + assert response.headers["Content-Type"].startswith(expected_content_type) + assert ("Content-Disposition" in response.headers) is expects_attachment + if expects_attachment: + assert response.headers["Content-Disposition"] == "attachment; filename*=UTF-8''chart.png" + # Range is never honoured here, so the hint must not be advertised either. + assert "Accept-Ranges" not in response.headers + assert response.headers["Content-Length"] == "64" + + +@pytest.mark.usefixtures("sqlite_db", "stored_bytes") +def test_content_downloads_a_file_with_no_recorded_mime_type(app: Flask, sqlite_session: Session) -> None: + upload_file = _persist_upload_file(sqlite_session, owner_id="anyone") + sqlite_session.execute(update(UploadFile).where(UploadFile.id == upload_file.id).values(mime_type=None)) + sqlite_session.commit() + token = _content_token(file_id=upload_file.id, kind=FileKind.UPLOAD) + + with app.test_request_context(f"/files/appdeploy/{upload_file.id}/content", query_string={"token": token}): + response = GrantedFileContentApi().get(UUID(upload_file.id)) + + assert response.headers["X-Content-Type-Options"] == "nosniff" + assert response.headers["Content-Type"].startswith("application/octet-stream") + assert response.headers["Content-Disposition"] == "attachment; filename*=UTF-8''report.pdf" + + +@pytest.mark.usefixtures("sqlite_db", "stored_bytes") +def test_content_rejects_an_expired_token(app: Flask, sqlite_session: Session) -> None: + tool_file = _persist_tool_file(sqlite_session, owner_id="anyone") + token = _content_token(file_id=tool_file.id, kind=FileKind.TOOL, expires_in=-1) + + with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}): + with pytest.raises(FileGrantInvalidError): + GrantedFileContentApi().get(UUID(tool_file.id)) + + +@pytest.mark.usefixtures("sqlite_db", "stored_bytes") +def test_content_rejects_a_token_minted_for_another_file(app: Flask, sqlite_session: Session) -> None: + tool_file = _persist_tool_file(sqlite_session, owner_id="anyone") + token = _content_token(file_id=str(FILE_ID), kind=FileKind.TOOL) + + with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}): + with pytest.raises(GrantedFileNotFoundError): + GrantedFileContentApi().get(UUID(tool_file.id)) + + +@pytest.mark.usefixtures("sqlite_db", "stored_bytes") +def test_content_rejects_a_token_naming_the_wrong_table(app: Flask, sqlite_session: Session) -> None: + tool_file = _persist_tool_file(sqlite_session, owner_id="anyone") + token = _content_token(file_id=tool_file.id, kind=FileKind.UPLOAD) + + with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": token}): + with pytest.raises(GrantedFileNotFoundError): + GrantedFileContentApi().get(UUID(tool_file.id)) + + +@pytest.mark.usefixtures("sqlite_db", "stored_bytes") +def test_content_rejects_a_file_grant_replayed_as_a_content_token(app: Flask, sqlite_session: Session) -> None: + tool_file = _persist_tool_file(sqlite_session, owner_id="anyone") + grant, _ = issue_file_grant( + end_user_id="anyone", + tenant_id=TENANT_ID, + app_id=APP_ID, + scopes=[FileGrantScope.RESOLVE], + ttl_seconds=600, + ) + + with app.test_request_context(f"/files/appdeploy/{tool_file.id}/content", query_string={"token": grant}): + with pytest.raises(FileGrantInvalidError): + GrantedFileContentApi().get(UUID(tool_file.id)) diff --git a/api/tests/unit_tests/controllers/files/test_file_grant_wraps.py b/api/tests/unit_tests/controllers/files/test_file_grant_wraps.py new file mode 100644 index 00000000000..893001f54df --- /dev/null +++ b/api/tests/unit_tests/controllers/files/test_file_grant_wraps.py @@ -0,0 +1,151 @@ +"""Tests for the Bearer file-grant decorator.""" + +import time +from collections.abc import Callable +from types import SimpleNamespace +from typing import cast + +import jwt +import pytest +from flask import Flask + +from controllers.files.wraps import FileGrantInvalidError, FileGrantScopeDeniedError, file_grant_required +from libs.passport import PassportService +from services.entities.file_grant_entities import FileGrantClaims, FileGrantScope +from services.file_grant_gateways import FILE_GRANT_AUDIENCE +from tests.unit_tests.file_grant_test_utils import issue_file_grant, token_gateway + +SECRET_KEY = "file-grant-test-secret-long-enough-for-hs256" +TENANT_ID = "11111111-1111-4111-8111-111111111111" +APP_ID = "22222222-2222-4222-8222-222222222222" +END_USER_ID = "55555555-5555-4555-8555-555555555555" + + +@pytest.fixture(autouse=True) +def granted_config(config_overrides: Callable[..., None]) -> None: + config_overrides(SECRET_KEY=SECRET_KEY) + + +@pytest.fixture(autouse=True) +def file_grant_service(granted_config: None, monkeypatch: pytest.MonkeyPatch) -> None: + del granted_config + service = SimpleNamespace(decode_grant=token_gateway().decode_grant) + monkeypatch.setattr( + "controllers.files.wraps.application_services", + lambda: SimpleNamespace(file_grants=service), + ) + + +@file_grant_required(FileGrantScope.UPLOAD) +def _view(grant: FileGrantClaims) -> FileGrantClaims: + return grant + + +def _call(app: Flask, authorization: str | None) -> FileGrantClaims: + headers: dict[str, str] = {"Authorization": authorization} if authorization is not None else {} + with app.test_request_context("/", method="POST", headers=headers): + return cast(Callable[[], FileGrantClaims], _view)() + + +def _grant(*scopes: FileGrantScope, ttl_seconds: int = 600) -> str: + token, _ = issue_file_grant( + end_user_id=END_USER_ID, + tenant_id=TENANT_ID, + app_id=APP_ID, + scopes=scopes, + ttl_seconds=ttl_seconds, + ) + return token + + +def test_valid_grant_is_injected_into_the_view(app: Flask) -> None: + claims = _call(app, f"Bearer {_grant(FileGrantScope.UPLOAD, FileGrantScope.RESOLVE)}") + + assert claims.sub == END_USER_ID + assert claims.tenant_id == TENANT_ID + assert claims.app_id == APP_ID + assert claims.scopes == [FileGrantScope.UPLOAD, FileGrantScope.RESOLVE] + + +def test_missing_authorization_is_rejected(app: Flask) -> None: + with pytest.raises(FileGrantInvalidError): + _call(app, None) + + +def test_non_bearer_authorization_is_rejected(app: Flask) -> None: + with pytest.raises(FileGrantInvalidError): + _call(app, f"Basic {_grant(FileGrantScope.UPLOAD)}") + + +def test_webapp_passport_cannot_be_replayed_as_a_grant(app: Flask) -> None: + """The passport is signed with the same key, so only ``aud`` separates them.""" + + passport = PassportService().issue( + { + "iss": "SELF_HOSTED", + "sub": "Web API Passport", + "app_id": APP_ID, + "end_user_id": END_USER_ID, + "exp": int(time.time()) + 600, + } + ) + + with pytest.raises(FileGrantInvalidError): + _call(app, f"Bearer {passport}") + + +def test_content_token_audience_is_not_accepted_as_a_grant(app: Flask) -> None: + content_token = jwt.encode( + { + "aud": "dify-files-content", + "kind": "upload", + "file_id": "66666666-6666-4666-8666-666666666666", + "exp": int(time.time()) + 600, + }, + SECRET_KEY, + algorithm="HS256", + ) + + with pytest.raises(FileGrantInvalidError): + _call(app, f"Bearer {content_token}") + + +def test_expired_grant_is_rejected(app: Flask) -> None: + expired = jwt.encode( + { + "aud": FILE_GRANT_AUDIENCE, + "sub": END_USER_ID, + "tenant_id": TENANT_ID, + "app_id": APP_ID, + "scopes": ["upload"], + "exp": int(time.time()) - 1, + }, + SECRET_KEY, + algorithm="HS256", + ) + + with pytest.raises(FileGrantInvalidError): + _call(app, f"Bearer {expired}") + + +def test_grant_signed_with_another_key_is_rejected(app: Flask) -> None: + forged = jwt.encode( + { + "aud": FILE_GRANT_AUDIENCE, + "sub": END_USER_ID, + "tenant_id": TENANT_ID, + "app_id": APP_ID, + "scopes": ["upload"], + "exp": int(time.time()) + 600, + }, + "some-other-secret-long-enough-for-hs256-signing", + algorithm="HS256", + ) + + with pytest.raises(FileGrantInvalidError): + _call(app, f"Bearer {forged}") + + +def test_grant_without_the_required_scope_is_denied(app: Flask) -> None: + with pytest.raises(FileGrantScopeDeniedError): + _call(app, f"Bearer {_grant(FileGrantScope.RESOLVE, FileGrantScope.PRODUCE)}") diff --git a/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py b/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py new file mode 100644 index 00000000000..97b5e1adffc --- /dev/null +++ b/api/tests/unit_tests/controllers/inner_api/app/test_file_grants.py @@ -0,0 +1,493 @@ +"""Tests for the AppDeploy file grant minting endpoint.""" + +import inspect +import os +import time +from collections.abc import Callable, Iterator +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + +import jwt +import pytest +from flask import Flask +from sqlalchemy import select +from sqlalchemy.engine import Engine +from sqlalchemy.orm import Session, sessionmaker + +from controllers.files.wraps import GrantedFileNotFoundError +from controllers.inner_api.app.file_grants import ( + EnterpriseFileGrantApi, + GrantAppNotFoundError, + GrantTtlTooLongError, + InvalidGrantRequestError, + InvalidSubjectError, +) +from extensions.ext_application_services import _build_file_grant_service +from extensions.storage.storage_type import StorageType +from libs.datetime_utils import naive_utc_now +from models.enums import CreatorUserRole, EndUserType +from models.model import App, EndUser, UploadFile +from models.tools import ToolFile +from services import end_user_service +from services.end_user_service import EndUserService +from services.file_grant_gateways import FILE_GRANT_AUDIENCE +from services.file_grant_service import ( + MAX_RUN_GRANT_TTL_SECONDS, + MAX_SESSION_GRANT_TTL_SECONDS, + MAX_WORKFLOW_EXECUTION_SECONDS, + RUN_GRANT_EXPIRY_GRACE_SECONDS, + FileGrantService, +) + +CONTROLLER_MODULE = "controllers.inner_api.app.file_grants" + +SECRET_KEY = "file-grant-test-secret-long-enough-for-hs256" +TENANT_ID = "11111111-1111-4111-8111-111111111111" +APP_ID = "22222222-2222-4222-8222-222222222222" +SUBJECT = "adp1.dGVzdC1zdWJqZWN0" + + +@pytest.fixture +def granted_config(config_overrides: Callable[..., None]) -> None: + config_overrides( + SECRET_KEY=SECRET_KEY, + FILES_URL="https://files.example.com", + INTERNAL_FILES_URL="http://dify-api.dify.svc:5001", + FILES_ACCESS_TIMEOUT=300, + ) + + +@pytest.fixture +def seeded_app(sqlite_session: Session) -> App: + app_model = App( + id=APP_ID, + tenant_id=TENANT_ID, + name="deployed app", + mode="workflow", + enable_site=True, + enable_api=True, + ) + sqlite_session.add(app_model) + sqlite_session.commit() + return app_model + + +def _mint(app: Flask, payload: dict[str, object]) -> dict[str, object]: + handler = EnterpriseFileGrantApi() + with app.test_request_context("/", method="POST", json=payload): + with patch(f"{CONTROLLER_MODULE}.inner_api_ns") as mock_ns: + mock_ns.payload = payload + return inspect.unwrap(handler.post)(handler) + + +def _subject_of(response: dict[str, object]) -> str: + grant = response["grant"] + assert isinstance(grant, str) + return str(jwt.decode(grant, SECRET_KEY, algorithms=["HS256"], audience=FILE_GRANT_AUDIENCE)["sub"]) + + +def _payload(**overrides: object) -> dict[str, object]: + return { + "tenant_id": TENANT_ID, + "app_id": APP_ID, + "subject": SUBJECT, + "is_anonymous": True, + "scopes": ["upload"], + "ttl_seconds": 600, + } | overrides + + +def _persist_upload_file(session: Session, *, owner_id: str, tenant_id: str = TENANT_ID) -> UploadFile: + upload_file = UploadFile( + tenant_id=tenant_id, + storage_type=StorageType.OPENDAL, + key="upload_files/report.pdf", + name="report.pdf", + size=2048, + extension="pdf", + mime_type="application/pdf", + created_by=owner_id, + created_by_role=CreatorUserRole.END_USER, + created_at=naive_utc_now(), + used=False, + ) + session.add(upload_file) + session.commit() + return upload_file + + +def _persist_tool_file(session: Session, *, owner_id: str, tenant_id: str = TENANT_ID) -> ToolFile: + tool_file = ToolFile( + user_id=owner_id, + tenant_id=tenant_id, + conversation_id=None, + file_key="tools/chart.png", + mimetype="image/png", + name="chart.png", + size=64, + ) + session.add(tool_file) + session.commit() + return tool_file + + +@pytest.fixture +def sqlite_db(sqlite_engine: Engine, granted_config: None) -> Iterator[None]: + del granted_config + service = _build_file_grant_service(database_client=sessionmaker(bind=sqlite_engine, expire_on_commit=False)) + services = SimpleNamespace(file_grants=service) + with patch(f"{CONTROLLER_MODULE}.application_services", return_value=services): + yield + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_creates_exactly_one_end_user_and_reuses_it(app: Flask, sqlite_session: Session) -> None: + first = _mint(app, _payload()) + second = _mint(app, _payload(ttl_seconds=900)) + + end_users = list(sqlite_session.scalars(select(EndUser).where(EndUser.tenant_id == TENANT_ID)).all()) + assert len(end_users) == 1 + assert end_users[0].type == EndUserType.APP_DEPLOY + assert end_users[0].session_id == FileGrantService.session_id_for_subject(SUBJECT) + assert end_users[0].external_user_id == SUBJECT + + first_grant = first["grant"] + second_grant = second["grant"] + assert isinstance(first_grant, str) + assert isinstance(second_grant, str) + first_claims = jwt.decode(first_grant, SECRET_KEY, algorithms=["HS256"], audience=FILE_GRANT_AUDIENCE) + second_claims = jwt.decode(second_grant, SECRET_KEY, algorithms=["HS256"], audience=FILE_GRANT_AUDIENCE) + assert first_claims["sub"] == second_claims["sub"] == end_users[0].id + assert first_claims["tenant_id"] == TENANT_ID + assert first_claims["app_id"] == APP_ID + assert first_claims["scopes"] == ["upload"] + first_expires_at = first["expires_at"] + second_expires_at = second["expires_at"] + assert isinstance(first_expires_at, int) + assert isinstance(second_expires_at, int) + assert second_expires_at > first_expires_at + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_returns_dify_upload_limits(app: Flask, config_overrides: Callable[..., None]) -> None: + config_overrides( + UPLOAD_FILE_SIZE_LIMIT=15, + UPLOAD_IMAGE_FILE_SIZE_LIMIT=10, + UPLOAD_AUDIO_FILE_SIZE_LIMIT=50, + UPLOAD_VIDEO_FILE_SIZE_LIMIT=100, + WORKFLOW_FILE_UPLOAD_LIMIT=10, + UPLOAD_FILE_BATCH_LIMIT=5, + ) + + response = _mint(app, _payload()) + + assert response["limits"] == { + "file_size_limit": 15, + "image_file_size_limit": 10, + "audio_file_size_limit": 50, + "video_file_size_limit": 100, + "workflow_file_upload_limit": 10, + "batch_count_limit": 5, + } + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_rejects_ttl_over_the_cap_before_touching_identity(app: Flask, sqlite_session: Session) -> None: + with pytest.raises(GrantTtlTooLongError): + _mint(app, _payload(ttl_seconds=MAX_SESSION_GRANT_TTL_SECONDS + 1)) + + assert sqlite_session.scalars(select(EndUser)).all() == [] + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_accepts_a_session_ttl_exactly_at_the_cap(app: Flask) -> None: + before = int(time.time()) + + response = _mint(app, _payload(ttl_seconds=MAX_SESSION_GRANT_TTL_SECONDS)) + + expires_at = response["expires_at"] + assert isinstance(expires_at, int) + assert MAX_SESSION_GRANT_TTL_SECONDS <= expires_at - before <= MAX_SESSION_GRANT_TTL_SECONDS + 5 + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_accepts_a_run_ttl_until_deadline_plus_grace(app: Flask) -> None: + now = int(time.time()) + run_duration = MAX_RUN_GRANT_TTL_SECONDS - RUN_GRANT_EXPIRY_GRACE_SECONDS + + response = _mint( + app, + _payload( + scopes=["resolve", "produce"], + ttl_seconds=MAX_RUN_GRANT_TTL_SECONDS, + run_deadline=now + run_duration, + ), + ) + + expires_at = response["expires_at"] + assert isinstance(expires_at, int) + assert MAX_RUN_GRANT_TTL_SECONDS <= expires_at - now <= MAX_RUN_GRANT_TTL_SECONDS + 5 + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_rejects_a_run_ttl_past_deadline_grace(app: Flask) -> None: + now = int(time.time()) + + with pytest.raises(GrantTtlTooLongError): + _mint( + app, + _payload( + scopes=["resolve", "produce"], + ttl_seconds=MAX_RUN_GRANT_TTL_SECONDS + 1, + run_deadline=now + MAX_WORKFLOW_EXECUTION_SECONDS, + ), + ) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_rejects_an_expired_run_deadline(app: Flask) -> None: + with pytest.raises(InvalidGrantRequestError): + _mint( + app, + _payload(scopes=["resolve", "produce"], ttl_seconds=1, run_deadline=int(time.time()) - 1), + ) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_rejects_a_run_deadline_without_produce_scope(app: Flask) -> None: + with pytest.raises(InvalidGrantRequestError): + _mint(app, _payload(run_deadline=int(time.time()) + 60)) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_rejects_a_run_deadline_beyond_the_workflow_limit(app: Flask) -> None: + with pytest.raises(InvalidGrantRequestError): + _mint( + app, + _payload( + scopes=["resolve", "produce"], + run_deadline=int(time.time()) + MAX_WORKFLOW_EXECUTION_SECONDS + 1, + ), + ) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_caps_a_run_grant_at_deadline_plus_grace(app: Flask) -> None: + now = int(time.time()) + run_deadline = now + 60 + + response = _mint( + app, + _payload( + scopes=["resolve", "produce"], + ttl_seconds=1200, + run_deadline=run_deadline, + ), + ) + + expires_at = response["expires_at"] + assert isinstance(expires_at, int) + assert ( + run_deadline + RUN_GRANT_EXPIRY_GRACE_SECONDS + <= expires_at + <= (run_deadline + RUN_GRANT_EXPIRY_GRACE_SECONDS + 1) + ) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +@pytest.mark.parametrize("ttl_seconds", [0, -1]) +def test_mint_rejects_a_non_positive_ttl(app: Flask, ttl_seconds: int) -> None: + with pytest.raises(InvalidGrantRequestError): + _mint(app, _payload(ttl_seconds=ttl_seconds)) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +@pytest.mark.parametrize("subject", ["", " ", "\t\n", "adp1.with\x00nul", "\x00"]) +def test_mint_rejects_an_unusable_subject(app: Flask, subject: str, sqlite_session: Session) -> None: + """A NUL would reach ``external_user_id`` verbatim and blow up on PostgreSQL.""" + + with pytest.raises(InvalidSubjectError): + _mint(app, _payload(subject=subject)) + + assert sqlite_session.scalars(select(EndUser)).all() == [] + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_folds_an_oversized_subject_into_one_identity(app: Flask, sqlite_session: Session) -> None: + """``external_user_id`` truncates at 255, so ``session_id`` is what keeps them apart.""" + + long_subject = "adp1." + "s" * 4000 + sibling = long_subject[:-1] + "t" + + first = _mint(app, _payload(subject=long_subject)) + again = _mint(app, _payload(subject=long_subject)) + other = _mint(app, _payload(subject=sibling)) + + end_users = sqlite_session.scalars(select(EndUser).order_by(EndUser.created_at)).all() + assert len(end_users) == 2 + assert all(len(end_user.external_user_id) == 255 for end_user in end_users) + assert _subject_of(first) == _subject_of(again) != _subject_of(other) + + +@pytest.mark.usefixtures("granted_config", "sqlite_db") +def test_mint_rejects_unknown_app(app: Flask) -> None: + with pytest.raises(GrantAppNotFoundError): + _mint(app, _payload()) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_returns_strict_metadata_without_urls(app: Flask, sqlite_session: Session) -> None: + owner_id = _subject_of(_mint(app, _payload())) + upload_file = _persist_upload_file(sqlite_session, owner_id=owner_id) + tool_file = _persist_tool_file(sqlite_session, owner_id=owner_id) + + response = _mint( + app, + _payload(file_ids=[{"id": upload_file.id, "kind": "upload"}, {"id": tool_file.id, "kind": "tool"}]), + ) + + assert response["files"] == [ + { + "id": upload_file.id, + "kind": "upload", + "name": "report.pdf", + "size": 2048, + "extension": "pdf", + "mime_type": "application/pdf", + }, + { + "id": tool_file.id, + "kind": "tool", + "name": "chart.png", + "size": 64, + "extension": "png", + "mime_type": "image/png", + }, + ] + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_fails_the_whole_strict_batch_on_one_miss(app: Flask, sqlite_session: Session) -> None: + owner_id = _subject_of(_mint(app, _payload())) + upload_file = _persist_upload_file(sqlite_session, owner_id=owner_id) + + with pytest.raises(GrantedFileNotFoundError): + _mint( + app, + _payload( + file_ids=[ + {"id": upload_file.id, "kind": "upload"}, + {"id": "33333333-3333-4333-8333-333333333333", "kind": "upload"}, + ] + ), + ) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_hides_files_owned_by_another_subject(app: Flask, sqlite_session: Session) -> None: + other_owner = _subject_of(_mint(app, _payload(subject="adp1.other"))) + foreign_file = _persist_upload_file(sqlite_session, owner_id=other_owner) + + with pytest.raises(GrantedFileNotFoundError): + _mint(app, _payload(file_ids=[{"id": foreign_file.id, "kind": "upload"}])) + + +@pytest.mark.usefixtures("granted_config", "seeded_app", "sqlite_db") +def test_mint_reports_optional_files_item_by_item(app: Flask, sqlite_session: Session) -> None: + owner_id = _subject_of(_mint(app, _payload())) + upload_file = _persist_upload_file(sqlite_session, owner_id=owner_id) + missing_id = "44444444-4444-4444-8444-444444444444" + + response = _mint( + app, + _payload( + optional_file_ids=[ + {"id": upload_file.id, "kind": "upload"}, + {"id": missing_id, "kind": "tool"}, + ] + ), + ) + + optional_files = response["optional_files"] + assert isinstance(optional_files, list) + present, absent = optional_files + assert present["ok"] is True + assert present["name"] == "report.pdf" + assert present["url"].startswith(f"https://files.example.com/files/appdeploy/{upload_file.id}/content?token=") + assert present["internal_url"].startswith( + f"http://dify-api.dify.svc:5001/files/appdeploy/{upload_file.id}/content?token=" + ) + assert absent == { + "id": missing_id, + "ok": False, + "kind": None, + "name": None, + "size": None, + "extension": None, + "mime_type": None, + "url": None, + "internal_url": None, + "error": "not_found", + } + assert response["files"] == [] + + +@pytest.mark.usefixtures("seeded_app") +def test_end_user_service_never_retypes_an_app_deploy_row( + sqlite_engine: Engine, + sqlite_session: Session, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Retyping would hide the row from the grant read and strand its files.""" + + subject = "subject-that-also-reaches-the-service-api" + session_id = FileGrantService.session_id_for_subject(subject) + owner = EndUser( + tenant_id=TENANT_ID, + app_id=APP_ID, + type=EndUserType.APP_DEPLOY, + is_anonymous=True, + session_id=session_id, + external_user_id=session_id, + ) + sqlite_session.add(owner) + sqlite_session.commit() + owner_id = owner.id + monkeypatch.setattr(end_user_service, "db", SimpleNamespace(engine=sqlite_engine)) + + EndUserService.get_or_create_end_user_by_type(EndUserType.SERVICE_API, TENANT_ID, APP_ID, session_id) + + sqlite_session.expire_all() + persisted_owner = sqlite_session.get(EndUser, owner_id) + assert persisted_owner is not None + assert persisted_owner.type == EndUserType.APP_DEPLOY + + +SKIPPED_DIRECTORY_NAMES = frozenset({".git", ".venv", "__pycache__", "migrations", "node_modules", "tests"}) + + +def test_app_deploy_end_users_have_exactly_one_writer() -> None: + """``end_users`` has no unique constraint, so a second writer would fork identities. + + ``end_user_service`` names the type only to exclude it from the legacy retype; + the behavioural guard above is what holds that exclusion in place. + """ + + api_root = Path(__file__).resolve().parents[5] + assert api_root.name == "api" + + referencing_modules: set[str] = set() + for directory, subdirectories, filenames in os.walk(api_root): + subdirectories[:] = [name for name in subdirectories if name not in SKIPPED_DIRECTORY_NAMES] + for filename in filenames: + if not filename.endswith(".py"): + continue + path = Path(directory) / filename + if "EndUserType.APP_DEPLOY" in path.read_text(encoding="utf-8"): + referencing_modules.add(path.relative_to(api_root).as_posix()) + + assert referencing_modules == { + "repositories/file_grant_repository.py", + "services/end_user_service.py", + } diff --git a/api/tests/unit_tests/controllers/openapi/conftest.py b/api/tests/unit_tests/controllers/openapi/conftest.py index 3a900011f54..9f60b5979a4 100644 --- a/api/tests/unit_tests/controllers/openapi/conftest.py +++ b/api/tests/unit_tests/controllers/openapi/conftest.py @@ -18,6 +18,7 @@ def _stub_execute( scope=None, allowed_token_types=None, edition=None, + require_valid_enterprise_license=False, workspace_membership=False, allowed_roles=None, rbac=None, diff --git a/api/tests/unit_tests/controllers/openapi/test_account.py b/api/tests/unit_tests/controllers/openapi/test_account.py index d4b28fa554e..a623d29ca69 100644 --- a/api/tests/unit_tests/controllers/openapi/test_account.py +++ b/api/tests/unit_tests/controllers/openapi/test_account.py @@ -2,13 +2,12 @@ import builtins import sys -import uuid from types import SimpleNamespace import pytest from flask import Flask from flask.views import MethodView -from werkzeug.exceptions import UnprocessableEntity +from werkzeug.exceptions import NotFound, UnprocessableEntity from controllers.openapi import bp as openapi_bp from controllers.openapi.account import ( @@ -17,8 +16,8 @@ from controllers.openapi.account import ( AccountSessionsApi, AccountSessionsSelfApi, ) -from controllers.openapi.auth.data import AuthData -from libs.oauth_bearer import Scope, TokenType +from machinery.context import AccountRequestContext +from services.entities.account_access_entities import AccountSessionPage if not hasattr(builtins, "MethodView"): builtins.MethodView = MethodView # type: ignore[attr-defined] @@ -88,96 +87,47 @@ def test_session_by_id_dispatches_to_correct_class(openapi_app: Flask): assert "DELETE" in rule.methods -def test_subject_match_for_account_filters_by_account_id(): - """Account subject scopes queries via account_id.""" - import uuid as _uuid - - from libs.oauth_bearer import AuthContext, SubjectType, TokenType - from services.oauth_device_flow import subject_match_clauses - - aid = _uuid.uuid4() - ctx = AuthContext( - subject_type=SubjectType.ACCOUNT, - subject_email="user@example.com", - subject_issuer="dify:account", - account_id=aid, - client_id="difyctl", - scopes=frozenset({"full"}), - token_id=_uuid.uuid4(), - token_type=TokenType.OAUTH_ACCOUNT, - expires_at=None, - token_hash="h1", - verified_tenants={}, - ) - clauses = subject_match_clauses(ctx) - # One predicate, on account_id - assert len(clauses) == 1 - assert "account_id" in str(clauses[0]) - - -def test_subject_match_for_external_sso_filters_by_email_and_issuer(): - """External SSO subject scopes via (subject_email, subject_issuer) - AND account_id IS NULL — so a same-email account row from a - federated tenant cannot be revoked through an SSO bearer. - """ - import uuid as _uuid - - from libs.oauth_bearer import AuthContext, SubjectType, TokenType - from services.oauth_device_flow import subject_match_clauses - - ctx = AuthContext( - subject_type=SubjectType.EXTERNAL_SSO, - subject_email="sso@partner.com", - subject_issuer="https://idp.partner.com", - account_id=None, - client_id="difyctl", - scopes=frozenset({"apps:run"}), - token_id=_uuid.uuid4(), - token_type=TokenType.OAUTH_EXTERNAL_SSO, - expires_at=None, - token_hash="h1", - verified_tenants={}, - ) - clauses = subject_match_clauses(ctx) - assert len(clauses) == 3 - rendered = " ".join(str(c) for c in clauses) - assert "subject_email" in rendered - assert "subject_issuer" in rendered - assert "account_id IS NULL" in rendered +def test_session_by_id_rejects_malformed_uuid(app: Flask) -> None: + api = AccountSessionByIdApi() + with app.test_request_context("/openapi/v1/account/sessions/not-a-uuid", method="DELETE"): + with pytest.raises(NotFound, match="session not found"): + api.delete.__wrapped__(api, _request_context(), session_id="not-a-uuid") # --- GET /account/sessions query validation (the handler routes ?page/?limit through -# SessionListQuery so the server enforces the bounds the contract advertises). The auth ctx and -# DB read are stubbed so these exercise only the validation + paging path; __wrapped__ skips the -# auth guard, which is covered separately in auth/. --- +# SessionListQuery so the server enforces the bounds the contract advertises). The application +# service is replaced with a small fake so these exercise only parsing and serialization; +# __wrapped__ skips the complete Admission boundary. --- _ACCOUNT_MOD = "controllers.openapi.account" -def _session_auth_data() -> AuthData: - return AuthData( - token_type=TokenType.OAUTH_ACCOUNT, - account_id=uuid.uuid4(), - token_hash="test", - token_id=uuid.uuid4(), - scopes=frozenset({Scope.FULL}), - required_scope=Scope.FULL, - allowed_roles=None, +def _request_context() -> AccountRequestContext: + return AccountRequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + access_token_id="token-1", ) -def _stub_session_deps(monkeypatch: pytest.MonkeyPatch, rows): +class _SessionListService: + def list_sessions(self, _context: AccountRequestContext, *, page: int, limit: int) -> AccountSessionPage: + return AccountSessionPage(page=page, limit=limit, total=0, items=()) + + +def _stub_account_service(monkeypatch: pytest.MonkeyPatch) -> None: mod = sys.modules[_ACCOUNT_MOD] - monkeypatch.setattr(mod, "get_auth_ctx", lambda: SimpleNamespace()) - monkeypatch.setattr(mod, "list_active_sessions", lambda *args, **kwargs: rows) + services = SimpleNamespace(accounts=SimpleNamespace(access=_SessionListService())) + monkeypatch.setattr(mod, "application_services", lambda: services) def test_sessions_list_valid_query_parses_page_and_limit(app: Flask, monkeypatch: pytest.MonkeyPatch): """A valid ?page&limit round-trips through SessionListQuery into the response envelope.""" api = AccountSessionsApi() - _stub_session_deps(monkeypatch, []) + _stub_account_service(monkeypatch) with app.test_request_context("/openapi/v1/account/sessions?page=2&limit=5"): - body, status = api.get.__wrapped__(api, auth_data=_session_auth_data()) + body, status = api.get.__wrapped__(api, _request_context()) assert status == 200 assert body["page"] == 2 assert body["limit"] == 5 @@ -188,9 +138,9 @@ def test_sessions_list_valid_query_parses_page_and_limit(app: Flask, monkeypatch def test_sessions_list_defaults_when_query_omitted(app: Flask, monkeypatch: pytest.MonkeyPatch): """No query → the model's defaults (page=1, limit=100) drive the envelope.""" api = AccountSessionsApi() - _stub_session_deps(monkeypatch, []) + _stub_account_service(monkeypatch) with app.test_request_context("/openapi/v1/account/sessions"): - body, status = api.get.__wrapped__(api, auth_data=_session_auth_data()) + body, status = api.get.__wrapped__(api, _request_context()) assert status == 200 assert body["page"] == 1 assert body["limit"] == 100 @@ -210,7 +160,7 @@ def test_sessions_list_defaults_when_query_omitted(app: Flask, monkeypatch: pyte def test_sessions_list_rejects_out_of_bounds_query(app: Flask, monkeypatch: pytest.MonkeyPatch, query): """Out-of-range / unknown query params raise 422 instead of being silently coerced.""" api = AccountSessionsApi() - _stub_session_deps(monkeypatch, []) + _stub_account_service(monkeypatch) with app.test_request_context(f"/openapi/v1/account/sessions?{query}"): with pytest.raises(UnprocessableEntity): - api.get.__wrapped__(api, auth_data=_session_auth_data()) + api.get.__wrapped__(api, _request_context()) diff --git a/api/tests/unit_tests/controllers/openapi/test_flask_admission.py b/api/tests/unit_tests/controllers/openapi/test_flask_admission.py new file mode 100644 index 00000000000..fb18eea75ae --- /dev/null +++ b/api/tests/unit_tests/controllers/openapi/test_flask_admission.py @@ -0,0 +1,116 @@ +from __future__ import annotations + +from collections.abc import Callable +from functools import wraps +from uuid import UUID + +import pytest +from flask import Flask +from werkzeug.exceptions import Unauthorized + +from controllers.openapi import flask_admission +from controllers.openapi.auth.data import AuthData +from enums import DeploymentEdition +from libs.oauth_bearer import Scope, TokenType +from libs.rate_limit import LIMIT_ME_PER_ACCOUNT +from machinery.context import AccountRequestContext +from models.account import Account, AccountStatus + + +def _auth_data(*, status: AccountStatus = AccountStatus.ACTIVE) -> AuthData: + account = Account(name="Ada", email="ada@example.com", status=status) + account.id = "11111111-1111-1111-1111-111111111111" + return AuthData( + token_type=TokenType.OAUTH_ACCOUNT, + account_id=UUID(account.id), + token_hash="hash-1", + token_id=UUID("22222222-2222-2222-2222-222222222222"), + scopes=frozenset({Scope.FULL}), + caller=account, + ) + + +def _install_fake_transport( + monkeypatch: pytest.MonkeyPatch, + auth_data: AuthData, + captured: dict[str, object], +) -> None: + def guard(**requirements: object) -> Callable[[Callable[..., object]], Callable[..., object]]: + captured.update(requirements) + + def decorator(view: Callable[..., object]) -> Callable[..., object]: + @wraps(view) + def admitted(*args: object, **kwargs: object) -> object: + return view(*args, auth_data=auth_data, **kwargs) + + return admitted + + return decorator + + monkeypatch.setattr(flask_admission.auth_router, "guard", guard) + + +def test_admission_builds_stable_request_context( + monkeypatch: pytest.MonkeyPatch, +) -> None: + captured: dict[str, object] = {} + limited: list[tuple[object, str]] = [] + _install_fake_transport(monkeypatch, _auth_data(), captured) + monkeypatch.setattr(flask_admission, "get_request_id", lambda: "request-1") + monkeypatch.setattr(flask_admission, "get_trace_id", lambda: "trace-1") + monkeypatch.setattr(flask_admission, "enforce", lambda spec, *, key: limited.append((spec, key))) + + @flask_admission.openapi_account_admission( + scope=Scope.FULL, + editions=frozenset({DeploymentEdition.ENTERPRISE}), + rate_limit=LIMIT_ME_PER_ACCOUNT, + ) + def view(_self: object, context: AccountRequestContext) -> AccountRequestContext: + return context + + with Flask(__name__).test_request_context("/openapi/v1/account"): + context = view(object()) + + assert context == AccountRequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="11111111-1111-1111-1111-111111111111", + access_token_id="22222222-2222-2222-2222-222222222222", + ) + assert captured == { + "scope": Scope.FULL, + "allowed_token_types": frozenset({TokenType.OAUTH_ACCOUNT}), + "edition": frozenset({DeploymentEdition.ENTERPRISE}), + "require_valid_enterprise_license": True, + } + assert limited == [(LIMIT_ME_PER_ACCOUNT, "account:11111111-1111-1111-1111-111111111111")] + + +def test_admission_rejects_uninitialized_account(monkeypatch: pytest.MonkeyPatch) -> None: + _install_fake_transport(monkeypatch, _auth_data(status=AccountStatus.UNINITIALIZED), {}) + + @flask_admission.openapi_account_admission(scope=Scope.FULL) + def view(_self: object, _context: AccountRequestContext) -> None: + raise AssertionError("view must not run") + + with Flask(__name__).test_request_context("/openapi/v1/account"): + with pytest.raises(Unauthorized, match="account not initialized"): + view(object()) + + +def test_admission_rejects_missing_auth_data(monkeypatch: pytest.MonkeyPatch) -> None: + def guard(**_requirements: object) -> Callable[[Callable[..., object]], Callable[..., object]]: + def decorator(view: Callable[..., object]) -> Callable[..., object]: + return view + + return decorator + + monkeypatch.setattr(flask_admission.auth_router, "guard", guard) + + @flask_admission.openapi_account_admission(scope=Scope.FULL) + def view(_self: object, _context: AccountRequestContext) -> None: + raise AssertionError("view must not run") + + with Flask(__name__).test_request_context("/openapi/v1/account"): + with pytest.raises(RuntimeError, match="did not provide valid AuthData"): + view(object()) diff --git a/api/tests/unit_tests/controllers/service_api/app/test_workflow.py b/api/tests/unit_tests/controllers/service_api/app/test_workflow.py index 9dd436d8795..1c44c2397db 100644 --- a/api/tests/unit_tests/controllers/service_api/app/test_workflow.py +++ b/api/tests/unit_tests/controllers/service_api/app/test_workflow.py @@ -53,6 +53,7 @@ from models import Account from models.enums import CreatorUserRole, WorkflowRunTriggeredFrom from models.model import App, AppMode, EndUser from models.workflow import WorkflowAppLog, WorkflowAppLogCreatedFrom, WorkflowRun, WorkflowType +from repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepository from services.app_generate_service import AppGenerateService from services.billing_service import BillingService from services.errors.app import ( @@ -63,7 +64,7 @@ from services.errors.app import ( TriggerWorkflowServiceModeUnavailableError as TriggerWorkflowServiceModeUnavailableServiceError, ) from services.errors.llm import InvokeRateLimitError -from services.workflow_app_service import WorkflowAppService +from services.workflow_app_log_query_service import WorkflowAppLogQueryService def _default_workflow_inputs() -> dict[str, object]: @@ -162,8 +163,11 @@ def _persist_workflow_log( app_id: str, ) -> None: workflow_run_id = "log-run-1" + account = Account(name="Log Account", email="log-account@example.com") + account.id = "account-1" sqlite_session.add_all( [ + account, _make_workflow_run( run_id=workflow_run_id, tenant_id=tenant_id, @@ -206,7 +210,11 @@ def _expected_workflow_log_pagination_payload() -> dict[str, object]: "details": None, "created_from": "service-api", "created_by_role": "account", - "created_by_account": None, + "created_by_account": { + "id": "account-1", + "name": "Log Account", + "email": "log-account@example.com", + }, "created_by_end_user": None, "created_at": int(datetime(2026, 1, 1, 1, 0, 3).timestamp()), } @@ -214,6 +222,18 @@ def _expected_workflow_log_pagination_payload() -> dict[str, object]: } +def _stub_workflow_app_logs(monkeypatch: pytest.MonkeyPatch) -> MagicMock: + workflow_app_logs = MagicMock() + workflow_app_logs.list_logs.return_value = _expected_workflow_log_pagination_payload() + services = SimpleNamespace(workflow_app_logs=workflow_app_logs) + monkeypatch.setattr( + sys.modules["controllers.service_api.app.workflow"], + "application_services", + lambda: services, + ) + return workflow_app_logs + + class TestWorkflowRunPayload: """Test suite for WorkflowRunPayload Pydantic model.""" @@ -355,46 +375,6 @@ class TestWorkflowRunResponse: } -class TestWorkflowAppService: - """Test WorkflowAppService interface.""" - - def test_service_exists(self): - """Test WorkflowAppService class exists.""" - service = WorkflowAppService() - assert service is not None - - def test_get_paginate_workflow_app_logs_method_exists(self): - """Test get_paginate_workflow_app_logs method exists.""" - assert hasattr(WorkflowAppService, "get_paginate_workflow_app_logs") - assert callable(WorkflowAppService.get_paginate_workflow_app_logs) - - @pytest.mark.parametrize("sqlite_session", [(WorkflowAppLog,)], indirect=True) - def test_get_paginate_workflow_app_logs_returns_pagination(self, sqlite_session: Session): - """Test pagination returns committed logs scoped to the requested app.""" - log = _make_workflow_app_log() - sqlite_session.add(log) - sqlite_session.commit() - service = WorkflowAppService() - result = service.get_paginate_workflow_app_logs( - session=sqlite_session, - app_model=_make_app_model(), - keyword=None, - status=None, - created_at_before=None, - created_at_after=None, - page=1, - limit=20, - created_by_end_user_session_id=None, - created_by_account=None, - ) - - assert result["page"] == 1 - assert result["limit"] == 20 - assert result["total"] == 1 - assert result["has_more"] is False - assert [item.id for item in result["data"]] == [log.id] - - class TestWorkflowExecutionStatus: """Test WorkflowExecutionStatus enum.""" @@ -847,20 +827,26 @@ class TestWorkflowTaskStopApi: class TestWorkflowAppLogApi: - @pytest.mark.parametrize("sqlite_session", [(WorkflowRun, WorkflowAppLog, Account)], indirect=True) def test_success( self, app: Flask, monkeypatch: pytest.MonkeyPatch, - sqlite_engine: Engine, sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], ) -> None: - _persist_workflow_log(sqlite_session, tenant_id="tenant-1", app_id="a1") - _bind_sqlite_database(monkeypatch, sqlite_engine, sqlite_session) - api = WorkflowAppLogApi() handler = unwrap(api.get) app_model = _make_app_model(app_id="a1") + _persist_workflow_log(sqlite_session, tenant_id=app_model.tenant_id, app_id=app_model.id) + + workflow_app_logs = WorkflowAppLogQueryService( + logs=WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory), + ) + monkeypatch.setattr( + sys.modules["controllers.service_api.app.workflow"], + "application_services", + lambda: SimpleNamespace(workflow_app_logs=workflow_app_logs), + ) with app.test_request_context("/workflows/logs", method="GET"): response = handler(api, app_model=app_model) @@ -994,18 +980,14 @@ class TestWorkflowAppLogApiGet: ``get`` is wrapped by ``@validate_app_token``. """ - @pytest.mark.parametrize("sqlite_session", [(WorkflowRun, WorkflowAppLog, Account)], indirect=True) def test_get_workflow_logs_success( self, app: Flask, workflow_app: App, monkeypatch: pytest.MonkeyPatch, - sqlite_engine: Engine, - sqlite_session: Session, ): """Test successful workflow log retrieval.""" - _persist_workflow_log(sqlite_session, tenant_id=workflow_app.tenant_id, app_id=workflow_app.id) - _bind_sqlite_database(monkeypatch, sqlite_engine, sqlite_session) + _stub_workflow_app_logs(monkeypatch) from controllers.service_api.app.workflow import WorkflowAppLogApi diff --git a/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_apis.py b/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_apis.py index 581f95d8ce5..8b3c7d42c79 100644 --- a/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_apis.py +++ b/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_apis.py @@ -257,6 +257,66 @@ class TestDatasetListApiGet: False, ) + @patch("controllers.service_api.dataset.dataset.create_plugin_provider_manager") + @patch("controllers.service_api.dataset.dataset.DatasetService") + def test_list_datasets_has_more_false_on_last_page_exact_limit( + self, + mock_dataset_svc: MagicMock, + mock_provider_mgr: MagicMock, + app: Flask, + account: Account, + tenant: Tenant, + controller_session: Session, + ) -> None: + """A full last page must set has_more false instead of forcing another fetch.""" + from controllers.service_api.dataset.dataset import DatasetListApi + + page_size = 20 + dataset = make_dataset(controller_session, tenant, account) + mock_dataset_svc.get_datasets.return_value = ([dataset] * page_size, page_size) + mock_provider_mgr.return_value.get_configurations.return_value.get_models.return_value = list[object]() + + with app.test_request_context(f"/datasets?page=1&limit={page_size}", method="GET"): + api = DatasetListApi() + response, status = unwrap(api.get)(api, controller_session, tenant_id=tenant.id) + + assert status == 200 + assert response["has_more"] is False + assert response["limit"] == page_size + assert response["total"] == page_size + assert response["page"] == 1 + + @patch("controllers.service_api.dataset.dataset.create_plugin_provider_manager") + @patch("controllers.service_api.dataset.dataset.DatasetService") + def test_list_datasets_has_more_true_when_limit_exceeds_cap( + self, + mock_dataset_svc: MagicMock, + mock_provider_mgr: MagicMock, + app: Flask, + account: Account, + tenant: Tenant, + controller_session: Session, + ) -> None: + """limit>100 still reports remaining rows after the server cap of 100.""" + from controllers.service_api.dataset.dataset import DatasetListApi + + returned_count = 100 + total = 150 + dataset = make_dataset(controller_session, tenant, account) + mock_dataset_svc.get_datasets.return_value = ([dataset] * returned_count, total) + mock_provider_mgr.return_value.get_configurations.return_value.get_models.return_value = list[object]() + + with app.test_request_context("/datasets?page=1&limit=200", method="GET"): + api = DatasetListApi() + response, status = unwrap(api.get)(api, controller_session, tenant_id=tenant.id) + + assert status == 200 + assert response["has_more"] is True + assert response["limit"] == 100 + assert response["total"] == total + assert response["page"] == 1 + assert mock_dataset_svc.get_datasets.call_args.args[1] == 100 + class TestDatasetListApiPost: """Test suite for DatasetListApi.post() endpoint.""" diff --git a/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py b/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py index fc5d540ce72..ca2fe02c02d 100644 --- a/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py +++ b/api/tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py @@ -969,22 +969,28 @@ class TestSegmentPagination: assert limit >= 1 assert limit <= 100 - def test_has_more_calculation(self): - """Test has_more pagination flag calculation.""" - segments_count = 20 + def test_has_more_false_on_last_page_exact_limit(self): + """Last page that fills the limit exactly must not claim more rows.""" + page = 1 limit = 20 + total = 20 + effective_limit = min(limit, 100) - has_more = segments_count == limit - assert has_more is True - - def test_no_more_when_incomplete_page(self): - """Test has_more is False for incomplete page.""" - segments_count = 15 - limit = 20 - - has_more = segments_count == limit + has_more = page * effective_limit < total + assert effective_limit == 20 assert has_more is False + def test_has_more_true_when_limit_exceeds_cap_with_remaining_rows(self): + """Capped pages must still report remaining rows after the first 100.""" + page = 1 + limit = 200 + total = 150 + effective_limit = min(limit, 100) + + has_more = page * effective_limit < total + assert effective_limit == 100 + assert has_more is True + # ============================================================================= # API Endpoint Tests @@ -1070,6 +1076,91 @@ class TestSegmentApiGet(SQLiteEndpointTest): mock_dump_segments.assert_called_once_with([mock_segment], {}, session=ANY) assert isinstance(mock_dump_segments.call_args.kwargs["session"], Session) + @patch("controllers.service_api.dataset.segment.segment_responses_with_summaries") + @patch("controllers.service_api.dataset.segment.SummaryIndexService.get_segments_summaries") + @patch("controllers.service_api.dataset.segment.SegmentService") + @patch("controllers.service_api.dataset.segment.DocumentService") + @patch("controllers.service_api.dataset.segment.current_account_with_tenant") + def test_list_segments_has_more_false_on_last_page_exact_limit( + self, + mock_account_fn, + mock_doc_svc, + mock_seg_svc, + mock_get_summaries, + mock_dump_segments, + app: Flask, + mock_tenant, + mock_dataset, + mock_segment, + ): + """A full last page must set has_more false instead of forcing another fetch.""" + mock_account_fn.return_value = (_account(), mock_tenant.id) + self._persist_dataset(mock_dataset, mock_tenant.id) + mock_doc_svc.get_document.return_value = _document_for_dataset( + mock_dataset, doc_form=IndexStructureType.PARAGRAPH_INDEX + ) + page_size = 20 + segments = [mock_segment] * page_size + mock_seg_svc.get_segments.return_value = (segments, page_size) + mock_get_summaries.return_value = {} + mock_dump_segments.return_value = [_segment_response_dict() for _ in range(page_size)] + + with app.test_request_context( + f"/datasets/{mock_dataset.id}/documents/doc-id/segments?page=1&limit={page_size}", + method="GET", + ): + api = SegmentApi() + response, status = api.get(tenant_id=mock_tenant.id, dataset_id=mock_dataset.id, document_id="doc-id") + + assert status == 200 + assert response["has_more"] is False + assert response["limit"] == page_size + assert response["total"] == page_size + assert response["page"] == 1 + + @patch("controllers.service_api.dataset.segment.segment_responses_with_summaries") + @patch("controllers.service_api.dataset.segment.SummaryIndexService.get_segments_summaries") + @patch("controllers.service_api.dataset.segment.SegmentService") + @patch("controllers.service_api.dataset.segment.DocumentService") + @patch("controllers.service_api.dataset.segment.current_account_with_tenant") + def test_list_segments_has_more_true_when_limit_exceeds_cap( + self, + mock_account_fn, + mock_doc_svc, + mock_seg_svc, + mock_get_summaries, + mock_dump_segments, + app: Flask, + mock_tenant, + mock_dataset, + mock_segment, + ): + """limit>100 still reports remaining rows after the server cap of 100.""" + mock_account_fn.return_value = (_account(), mock_tenant.id) + self._persist_dataset(mock_dataset, mock_tenant.id) + mock_doc_svc.get_document.return_value = _document_for_dataset( + mock_dataset, doc_form=IndexStructureType.PARAGRAPH_INDEX + ) + returned_count = 100 + total = 150 + segments = [mock_segment] * returned_count + mock_seg_svc.get_segments.return_value = (segments, total) + mock_get_summaries.return_value = {} + mock_dump_segments.return_value = [_segment_response_dict() for _ in range(returned_count)] + + with app.test_request_context( + f"/datasets/{mock_dataset.id}/documents/doc-id/segments?page=1&limit=200", + method="GET", + ): + api = SegmentApi() + response, status = api.get(tenant_id=mock_tenant.id, dataset_id=mock_dataset.id, document_id="doc-id") + + assert status == 200 + assert response["has_more"] is True + assert response["limit"] == 100 + assert response["total"] == total + assert response["page"] == 1 + @patch("controllers.service_api.dataset.segment.current_account_with_tenant") def test_list_segments_dataset_not_found(self, mock_account_fn, app, mock_tenant, mock_dataset): """Test 404 when dataset not found.""" diff --git a/api/tests/unit_tests/controllers/service_api/dataset/test_document.py b/api/tests/unit_tests/controllers/service_api/dataset/test_document.py index a2e8d760feb..c297b95987e 100644 --- a/api/tests/unit_tests/controllers/service_api/dataset/test_document.py +++ b/api/tests/unit_tests/controllers/service_api/dataset/test_document.py @@ -1189,6 +1189,76 @@ class TestDocumentListApi(SQLiteControllerTest): assert "data_source_info_dict" not in response["data"][0] assert "doc_metadata_details" not in response["data"][0] + @patch("controllers.service_api.dataset.document.paginate_query") + @patch("controllers.service_api.dataset.document.DocumentService") + def test_list_documents_has_more_false_on_last_page_exact_limit( + self, mock_doc_svc, mock_paginate, app: Flask, mock_tenant, mock_dataset + ): + """A full last page must set has_more false instead of forcing another fetch.""" + self._persist_dataset(mock_dataset) + page_size = 20 + documents = [ + make_serializable_document( + id=f"doc-{index}", + name=f"Document {index}", + tenant_id=mock_tenant, + dataset_id=mock_dataset.id, + ) + for index in range(page_size) + ] + mock_paginate.return_value = _PaginationRecord(items=documents, total=page_size) + mock_doc_svc.enrich_documents_with_summary_index_status.return_value = None + + with app.test_request_context( + f"/datasets/{mock_dataset.id}/documents?page=1&limit={page_size}", + method="GET", + ): + api = DocumentListApi() + response = inspect.unwrap(type(api).get)( + api, self.session, tenant_id=mock_tenant, dataset_id=mock_dataset.id + ) + + assert response["has_more"] is False + assert response["limit"] == page_size + assert response["total"] == page_size + assert response["page"] == 1 + + @patch("controllers.service_api.dataset.document.paginate_query") + @patch("controllers.service_api.dataset.document.DocumentService") + def test_list_documents_has_more_true_when_limit_exceeds_cap( + self, mock_doc_svc, mock_paginate, app: Flask, mock_tenant, mock_dataset + ): + """limit>100 still reports remaining rows after the server cap of 100.""" + self._persist_dataset(mock_dataset) + returned_count = 100 + total = 150 + documents = [ + make_serializable_document( + id=f"doc-{index}", + name=f"Document {index}", + tenant_id=mock_tenant, + dataset_id=mock_dataset.id, + ) + for index in range(returned_count) + ] + mock_paginate.return_value = _PaginationRecord(items=documents, total=total) + mock_doc_svc.enrich_documents_with_summary_index_status.return_value = None + + with app.test_request_context( + f"/datasets/{mock_dataset.id}/documents?page=1&limit=200", + method="GET", + ): + api = DocumentListApi() + response = inspect.unwrap(type(api).get)( + api, self.session, tenant_id=mock_tenant, dataset_id=mock_dataset.id + ) + + assert response["has_more"] is True + assert response["limit"] == 100 + assert response["total"] == total + assert response["page"] == 1 + assert mock_paginate.call_args.kwargs["per_page"] == 100 + def test_list_documents_dataset_not_found(self, app: Flask, mock_tenant, mock_dataset): """Test 404 when dataset not found.""" # Arrange diff --git a/api/tests/unit_tests/controllers/web/test_app.py b/api/tests/unit_tests/controllers/web/test_app.py index 005c23e7f25..9b1247bf431 100644 --- a/api/tests/unit_tests/controllers/web/test_app.py +++ b/api/tests/unit_tests/controllers/web/test_app.py @@ -216,18 +216,17 @@ class TestAppWebAuthPermission: extract_passport.assert_not_called() @pytest.mark.parametrize( - ("decoded", "expected_user_id", "allowed"), + ("user_id", "allowed"), [ - pytest.param({"user_id": "user-1"}, "user-1", True, id="identified-user"), - pytest.param({}, "visitor", False, id="visitor-fallback"), + pytest.param("user-1", True, id="allowed-user"), + pytest.param("user-2", False, id="denied-user"), ], ) @patch("controllers.web.app.application_services") def test_checks_private_app_permission( self, application_services: MagicMock, - decoded: dict[str, str], - expected_user_id: str, + user_id: str, allowed: bool, app: Flask, ) -> None: @@ -241,14 +240,41 @@ class TestAppWebAuthPermission: patch("controllers.web.app.extract_webapp_passport", return_value="passport") as extract_passport, patch("controllers.web.app.PassportService") as passport_service, ): - passport_service.return_value.verify.return_value = decoded + passport_service.return_value.verify.return_value = {"user_id": user_id, "auth_type": "internal"} result = AppWebAuthPermission().get() assert result == {"result": allowed} webapp_access.requires_permission_check.assert_called_once_with("app-1") extract_passport.assert_called_once() passport_service.return_value.verify.assert_called_once_with("passport") - webapp_access.is_user_allowed.assert_called_once_with(user_id=expected_user_id, app_id="app-1") + webapp_access.is_user_allowed.assert_called_once_with(user_id=user_id, app_id="app-1") + + @pytest.mark.parametrize( + "decoded", + [ + pytest.param({}, id="missing-auth-type"), + pytest.param({"auth_type": "internal"}, id="missing-user-id"), + pytest.param({"user_id": "sso_external_user", "auth_type": "external"}, id="external-auth-type"), + ], + ) + @patch("controllers.web.app.application_services") + def test_private_app_requires_internal_identity( + self, application_services: MagicMock, decoded: dict[str, str], app: Flask + ) -> None: + webapp_access = MagicMock() + webapp_access.requires_permission_check.return_value = True + application_services.return_value = SimpleNamespace(webapp_access=webapp_access) + + with ( + app.test_request_context("/webapp/permission?appId=app-1", headers={"X-App-Code": "code1"}), + patch("controllers.web.app.extract_webapp_passport", return_value="passport"), + patch("controllers.web.app.PassportService") as passport_service, + ): + passport_service.return_value.verify.return_value = decoded + with pytest.raises(WebAppAuthRequiredError): + AppWebAuthPermission().get() + + webapp_access.is_user_allowed.assert_not_called() @pytest.mark.parametrize("failing_method", ["requires_permission_check", "is_user_allowed"]) @patch("controllers.web.app.application_services") @@ -264,7 +290,7 @@ class TestAppWebAuthPermission: application_services.return_value = SimpleNamespace(webapp_access=webapp_access) passport_service = MagicMock() - passport_service.return_value.verify.return_value = {"user_id": "user-1"} + passport_service.return_value.verify.return_value = {"user_id": "user-1", "auth_type": "internal"} with ( app.test_request_context("/webapp/permission?appId=app-1", headers={"X-App-Code": "code1"}), patch("controllers.web.app.extract_webapp_passport", return_value="passport"), diff --git a/api/tests/unit_tests/controllers/web/test_web_passport.py b/api/tests/unit_tests/controllers/web/test_web_passport.py index 9ac0b731a3a..fd6cd80d062 100644 --- a/api/tests/unit_tests/controllers/web/test_web_passport.py +++ b/api/tests/unit_tests/controllers/web/test_web_passport.py @@ -1,6 +1,5 @@ """Unit tests for the thin web-passport Flask adapter.""" -from inspect import unwrap from types import SimpleNamespace from unittest.mock import MagicMock, patch @@ -31,7 +30,7 @@ def test_passport_resource_parses_input_and_serializes_result(app: Flask) -> Non patch("controllers.web.passport.application_services", return_value=services), patch("controllers.web.passport.extract_webapp_access_token", return_value="login-token"), ): - result = unwrap(PassportResource.get)(PassportResource()) + result = PassportResource().get() assert result == {"access_token": "issued-token"} service.issue.assert_called_once_with( @@ -41,7 +40,7 @@ def test_passport_resource_parses_input_and_serializes_result(app: Flask) -> Non def test_passport_resource_requires_app_code(app: Flask) -> None: with app.test_request_context("/passport"), pytest.raises(Unauthorized, match="X-App-Code"): - unwrap(PassportResource.get)(PassportResource()) + PassportResource().get() @pytest.mark.parametrize( @@ -66,4 +65,4 @@ def test_passport_resource_translates_application_errors( patch("controllers.web.passport.application_services", return_value=services), pytest.raises(http_error), ): - unwrap(PassportResource.get)(PassportResource()) + PassportResource().get() diff --git a/api/tests/unit_tests/core/agent/test_base_agent_runner.py b/api/tests/unit_tests/core/agent/test_base_agent_runner.py index 7411164512c..c6808ed39f6 100644 --- a/api/tests/unit_tests/core/agent/test_base_agent_runner.py +++ b/api/tests/unit_tests/core/agent/test_base_agent_runner.py @@ -1,670 +1,693 @@ import json +from datetime import datetime from decimal import Decimal -from unittest.mock import MagicMock import pytest from pytest_mock import MockerFixture +from sqlalchemy.orm import Session import core.agent.base_agent_runner as module -import models.model as model_module from core.agent.base_agent_runner import BaseAgentRunner +from core.agent.entities import AgentEntity, AgentToolEntity +from core.app.app_config.entities import ( + AppAdditionalFeatures, + DatasetEntity, + DatasetRetrieveConfigEntity, + EasyUIBasedAppModelConfigFrom, + ModelConfigEntity, + PromptTemplateEntity, +) +from core.app.apps.agent_chat.app_config_manager import AgentChatAppConfig +from core.app.apps.base_app_queue_manager import AppQueueManager +from core.app.entities.app_invoke_entities import ( + AgentChatAppGenerateEntity, + InvokeFrom, + ModelConfigWithCredentialsEntity, +) +from core.model_manager import ModelInstance +from core.tools.__base.tool import Tool +from core.tools.__base.tool_runtime import ToolRuntime +from core.tools.entities.common_entities import I18nObject +from core.tools.entities.tool_entities import ( + ToolDescription, + ToolEntity, + ToolIdentity, + ToolProviderType, +) +from core.tools.utils.dataset_retriever.dataset_retriever_base_tool import DatasetRetrieverBaseTool +from core.tools.utils.dataset_retriever_tool import DatasetRetrieverTool +from extensions.ext_storage import storage +from extensions.storage.storage_type import StorageType +from graphon.file import FileTransferMethod, FileType +from graphon.model_runtime.entities import LLMUsage, PromptMessageTool +from graphon.model_runtime.model_providers.base.large_language_model import LargeLanguageModel +from models.enums import ConversationFromSource, CreatorUserRole, MessageFileBelongsTo +from models.model import AppMode, AppModelConfig, Conversation, Message, MessageAgentThought, MessageFile, UploadFile -# ========================================================== -# Fixtures -# ========================================================== + +def _message( + *, + message_id: str = "msg_current", + conversation_id: str = "conv1", + query: str = "hello", + answer: str = "", +) -> Message: + message = Message( + id=message_id, + app_id="app1", + conversation_id=conversation_id, + query=query, + message={"role": "user", "content": query}, + answer=answer, + message_unit_price=Decimal(0), + answer_unit_price=Decimal(0), + currency="USD", + from_source=ConversationFromSource.CONSOLE, + from_account_id="user", + ) + message._inputs = {} + return message + + +def _conversation(*, app_model_config_id: str | None = None) -> Conversation: + conversation = Conversation( + id="conv1", + app_id="app1", + app_model_config_id=app_model_config_id, + mode=AppMode.AGENT_CHAT, + name="Conversation", + from_source=ConversationFromSource.CONSOLE, + from_account_id="user", + is_deleted=False, + ) + conversation._inputs = {} + return conversation + + +def _thought( + *, + thought_id: str, + message_id: str = "m1", + tool: str | None = None, + tool_input: str | None = None, + observation: str | None = None, + thought: str = "thinking", +) -> MessageAgentThought: + row = MessageAgentThought( + message_id=message_id, + position=1, + created_by_role=CreatorUserRole.ACCOUNT, + created_by="user", + thought=thought, + tool=tool, + tool_input=tool_input, + observation=observation, + tool_labels_str="{}", + tool_meta_str="{}", + ) + row.id = thought_id + return row + + +def _persist_history(session: Session, *messages: Message, thoughts: list[MessageAgentThought] | None = None) -> None: + session.add_all([_conversation(), *messages, *(thoughts or [])]) + session.commit() + + +def _app_config( + *, + simple_prompt_template: str | None = "", + agent: AgentEntity | None = None, + dataset: DatasetEntity | None = None, + additional_features: AppAdditionalFeatures | None = None, +) -> AgentChatAppConfig: + return AgentChatAppConfig( + tenant_id="tenant", + app_id="app1", + app_mode=AppMode.AGENT_CHAT, + app_model_config_from=EasyUIBasedAppModelConfigFrom.ARGS, + app_model_config_dict={}, + model=ModelConfigEntity(provider="provider", model="model"), + prompt_template=PromptTemplateEntity( + prompt_type=PromptTemplateEntity.PromptType.SIMPLE, + simple_prompt_template=simple_prompt_template, + ), + agent=agent, + dataset=dataset, + additional_features=additional_features, + ) + + +def _app_generate( + *, + app_config: AgentChatAppConfig | None = None, + files: list[str] | None = None, +) -> AgentChatAppGenerateEntity: + """Build the real generate entity with only the fields used by these unit tests.""" + + return AgentChatAppGenerateEntity.model_construct( + task_id="task", + app_config=app_config or _app_config(), + inputs={}, + files=files or [], + user_id="user", + stream=False, + invoke_from=InvokeFrom.DEBUGGER, + ) + + +def _agent_tool(tool_name: str) -> AgentToolEntity: + return AgentToolEntity( + provider_type=ToolProviderType.BUILT_IN, + provider_id="provider", + tool_name=tool_name, + ) + + +def _agent(*tools: AgentToolEntity) -> AgentEntity: + return AgentEntity( + provider="provider", + model="model", + strategy=AgentEntity.Strategy.FUNCTION_CALLING, + tools=list(tools), + ) + + +def _tool_entity(name: str) -> ToolEntity: + return ToolEntity( + identity=ToolIdentity( + author="author", + name=name, + label=I18nObject(en_US=name), + provider="provider", + ), + description=ToolDescription( + human=I18nObject(en_US="Description"), + llm="desc", + ), + ) + + +def _dataset_tool(mocker: MockerFixture, name: str) -> DatasetRetrieverTool: + return DatasetRetrieverTool( + entity=_tool_entity(name), + runtime=ToolRuntime(tenant_id="tenant"), + retrieval_tool=mocker.Mock(spec=DatasetRetrieverBaseTool), + ) @pytest.fixture -def mock_db_session(mocker: MockerFixture): - session = mocker.MagicMock() - mocker.patch.object(module.db, "session", session) - return session +def database_session(sqlite_session: Session, monkeypatch: pytest.MonkeyPatch) -> Session: + """Bind legacy global-session writes to a real SQLite Session.""" + + monkeypatch.setattr(module.db, "session", sqlite_session) + return sqlite_session @pytest.fixture -def runner(mocker: MockerFixture): - r = BaseAgentRunner.__new__(BaseAgentRunner) - r.tenant_id = "tenant" - r.user_id = "user" - r.agent_thought_count = 0 - r.message = mocker.MagicMock(id="msg_current", conversation_id="conv1") - r.app_config = mocker.MagicMock() - r.app_config.app_id = "app1" - r.app_config.agent = None - r.dataset_tools = [] - r.application_generate_entity = mocker.MagicMock(invoke_from="test") - r._current_thoughts = [] - return r +def runner(sqlite_session: Session, mocker: MockerFixture) -> BaseAgentRunner: + app_config = _app_config() + llm = mocker.Mock(spec=LargeLanguageModel) + llm.get_model_schema.return_value = None + model_instance = mocker.Mock( + spec=ModelInstance, + model_type_instance=llm, + model_name="model", + credentials={}, + ) - -# ========================================================== -# _repack_app_generate_entity -# ========================================================== + return BaseAgentRunner( + session=sqlite_session, + tenant_id="tenant", + application_generate_entity=_app_generate(app_config=app_config), + conversation=_conversation(), + app_config=app_config, + model_config=ModelConfigWithCredentialsEntity.model_construct(), + config=_agent(), + queue_manager=mocker.Mock(spec=AppQueueManager), + message=_message(), + user_id="user", + model_instance=model_instance, + ) class TestRepack: - def test_sets_empty_if_none(self, runner: BaseAgentRunner, mocker: MockerFixture): - entity = mocker.MagicMock() - entity.app_config.prompt_template.simple_prompt_template = None + def test_sets_empty_if_none(self, runner: BaseAgentRunner) -> None: + entity = _app_generate(app_config=_app_config(simple_prompt_template=None)) result = runner._repack_app_generate_entity(entity) assert result.app_config.prompt_template.simple_prompt_template == "" - def test_keeps_existing(self, runner: BaseAgentRunner, mocker: MockerFixture): - entity = mocker.MagicMock() - entity.app_config.prompt_template.simple_prompt_template = "abc" + def test_keeps_existing(self, runner: BaseAgentRunner) -> None: + entity = _app_generate(app_config=_app_config(simple_prompt_template="abc")) result = runner._repack_app_generate_entity(entity) assert result.app_config.prompt_template.simple_prompt_template == "abc" -# ========================================================== -# update_prompt_message_tool -# ========================================================== - - -class TestUpdatePromptTool: - def test_replaces_prompt_tool_parameters_with_tool_schema(self, runner: BaseAgentRunner, mocker: MockerFixture): - tool = mocker.MagicMock() - schema = { - "type": "object", - "properties": {"p1": {"type": "string", "description": "desc"}}, - "required": ["p1"], - } - tool.get_llm_parameters_json_schema.return_value = schema - - prompt_tool = mocker.MagicMock() - prompt_tool.parameters = {"properties": {}, "required": []} - - result = runner.update_prompt_message_tool(tool, prompt_tool) - assert result.parameters == schema - - -# ========================================================== -# create_agent_thought -# ========================================================== - - -class TestCreateAgentThought: - def test_with_files(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - mock_thought = mocker.MagicMock(id=10) - mocker.patch.object(module, "MessageAgentThought", return_value=mock_thought) - - result = runner.create_agent_thought("m", "msg", "tool", "input", ["f1"]) - assert result == "10" - assert runner.agent_thought_count == 1 - mock_db_session.add.assert_called_once_with(mock_thought) - mock_db_session.commit.assert_called_once_with() - mock_db_session.close.assert_called_once_with() - - def test_without_files(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - mock_thought = mocker.MagicMock(id=11) - mocker.patch.object(module, "MessageAgentThought", return_value=mock_thought) - - result = runner.create_agent_thought("m", "msg", "tool", "input", []) - assert result == "11" - - -# ========================================================== -# save_agent_thought -# ========================================================== - - -class TestSaveAgentThought: - def setup_agent(self, mocker: MockerFixture): - agent = mocker.MagicMock() - agent.tool = "tool1;tool2" - agent.tool_labels = {} - agent.thought = "" - return agent - - def test_not_found(self, runner: BaseAgentRunner, mock_db_session): - mock_db_session.scalar.return_value = None - with pytest.raises(ValueError): - runner.save_agent_thought("id", None, None, None, None, None, None, [], None) - - def test_full_update(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = self.setup_agent(mocker) - mock_db_session.scalar.return_value = agent - - mock_label = mocker.MagicMock() - mock_label.to_dict.return_value = {"en_US": "label"} - mocker.patch.object(module.ToolManager, "get_tool_label", return_value=mock_label) - - usage = mocker.MagicMock( - prompt_tokens=1, - prompt_price_unit=Decimal("0.1"), - prompt_unit_price=Decimal("0.1"), - completion_tokens=2, - completion_price_unit=Decimal("0.2"), - completion_unit_price=Decimal("0.2"), - total_tokens=3, - total_price=Decimal("0.3"), - ) - - runner.save_agent_thought( - "id", - "tool1;tool2", - {"a": 1}, - "thought", - {"b": 2}, - {"meta": 1}, - "answer", - ["f1"], - usage, - ) - - assert agent.answer == "answer" - assert agent.tokens == 3 - assert "tool1" in json.loads(agent.tool_labels_str) - mock_db_session.commit.assert_called_once_with() - mock_db_session.close.assert_called_once_with() - - def test_label_fallback_when_none(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = self.setup_agent(mocker) - agent.tool = "unknown_tool" - mock_db_session.scalar.return_value = agent - mocker.patch.object(module.ToolManager, "get_tool_label", return_value=None) - - runner.save_agent_thought("id", None, None, None, None, None, None, [], None) - labels = json.loads(agent.tool_labels_str) - assert "unknown_tool" in labels - - def test_json_failure_paths(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = self.setup_agent(mocker) - mock_db_session.scalar.return_value = agent - - bad_obj = MagicMock() - bad_obj.__str__.return_value = "bad" - - runner.save_agent_thought( - "id", - None, - bad_obj, - None, - bad_obj, - bad_obj, - None, - [], - None, - ) - - assert mock_db_session.commit.called - - def test_messages_ids_none(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = self.setup_agent(mocker) - mock_db_session.scalar.return_value = agent - runner.save_agent_thought("id", None, None, None, None, None, None, None, None) - assert mock_db_session.commit.called - - def test_success_dict_serialization(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = self.setup_agent(mocker) - mock_db_session.scalar.return_value = agent - - runner.save_agent_thought( - "id", - None, - {"a": 1}, - None, - {"b": 2}, - None, - None, - [], - None, - ) - - assert isinstance(agent.tool_input, str) - assert isinstance(agent.observation, str) - - -# ========================================================== -# organize_agent_user_prompt -# ========================================================== - - -class TestOrganizeUserPrompt: - def test_no_files(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - caller_session = mocker.MagicMock() - caller_session.scalars.return_value.all.return_value = [] - msg = mocker.MagicMock(id="1", query="hello", app_model_config=None) - result = runner.organize_agent_user_prompt(msg, session=caller_session) - assert result.content == "hello" - assert mock_db_session.mock_calls == [] - - def test_with_files_no_config(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - mock_db_session.scalars.return_value.all.return_value = [mocker.MagicMock()] - msg = mocker.MagicMock(id="1", query="hello", app_model_config=None) - msg.app_model_config_with_session.return_value = None - result = runner.organize_agent_user_prompt(msg, session=mock_db_session) - assert result.content == "hello" - - def test_image_detail_low_fallback(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - mock_db_session.scalars.return_value.all.return_value = [mocker.MagicMock()] - file_config = mocker.MagicMock() - file_config.image_config = mocker.MagicMock(detail=None) - mocker.patch.object(module.FileUploadConfigManager, "convert", return_value=file_config) - mocker.patch.object(module.file_factory, "build_from_message_files", return_value=[]) - - msg = mocker.MagicMock(id="1", query="hello") - app_model_config = mocker.MagicMock() - app_model_config.app_id = "app1" - app_model_config.to_dict.return_value = {} - msg.app_model_config_with_session.return_value = app_model_config - load_annotation_reply_config = mocker.patch.object( - module, "load_annotation_reply_config", return_value={"enabled": False} - ) - - result = runner.organize_agent_user_prompt(msg, session=mock_db_session) - assert result.content == "hello" - load_annotation_reply_config.assert_called_once_with(mock_db_session, "app1") - app_model_config.to_dict.assert_called_once_with(annotation_reply={"enabled": False}) - - -# ========================================================== -# organize_agent_history -# ========================================================== - - -class TestOrganizeHistory: - def test_empty(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - caller_session = mocker.MagicMock() - caller_session.execute.return_value.scalars.return_value.all.return_value = [] - mocker.patch.object(module, "extract_thread_messages", return_value=[]) - result = runner.organize_agent_history([], session=caller_session) - assert result == [] - assert mock_db_session.mock_calls == [] - - def test_with_answer_only(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - msg = mocker.MagicMock(id="m1", answer="ans", agent_thoughts=[], app_model_config=None) - msg.agent_thoughts_with_session.return_value = [] - msg.app_model_config_with_session.return_value = None - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - result = runner.organize_agent_history([], session=mock_db_session) - assert any(isinstance(x, module.AssistantPromptMessage) for x in result) - - def test_skip_current_message(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - msg = mocker.MagicMock(id="msg_current", agent_thoughts=[], answer="ans", app_model_config=None) - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - result = runner.organize_agent_history([], session=mock_db_session) - assert result == [] - - def test_with_tool_calls_invalid_json(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - thought = mocker.MagicMock( - tool="tool1", - tool_input="invalid", - observation="invalid", - thought="thinking", - ) - msg = mocker.MagicMock(id="m2", agent_thoughts=[thought], answer=None, app_model_config=None) - msg.agent_thoughts_with_session.return_value = [thought] - msg.app_model_config_with_session.return_value = None - - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - mocker.patch("uuid.uuid4", return_value="uuid") - - result = runner.organize_agent_history([], session=mock_db_session) - assert isinstance(result, list) - - def test_empty_tool_name_split(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - thought = mocker.MagicMock(tool=";", thought="thinking") - msg = mocker.MagicMock(id="m5", agent_thoughts=[thought], answer=None, app_model_config=None) - msg.agent_thoughts_with_session.return_value = [thought] - msg.app_model_config_with_session.return_value = None - - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - result = runner.organize_agent_history([], session=mock_db_session) - assert isinstance(result, list) - - def test_valid_json_tool_flow(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - thought = mocker.MagicMock( - tool="tool1", - tool_input=json.dumps({"tool1": {"x": 1}}), - observation=json.dumps({"tool1": "obs"}), - thought="thinking", - ) - - msg = mocker.MagicMock( - id="m100", - agent_thoughts=[thought], - answer=None, - app_model_config=None, - ) - msg.agent_thoughts_with_session.return_value = [thought] - msg.app_model_config_with_session.return_value = None - - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - mocker.patch("uuid.uuid4", return_value="uuid") - - result = runner.organize_agent_history([], session=mock_db_session) - assert isinstance(result, list) - - -# ========================================================== -# _convert_tool_to_prompt_message_tool (new coverage) -# ========================================================== - - -class TestConvertToolToPromptMessageTool: - def test_basic_conversion(self, runner: BaseAgentRunner, mocker: MockerFixture): - tool = mocker.MagicMock(tool_name="tool1") - - tool_entity = mocker.MagicMock() - tool_entity.entity.description.llm = "desc" - schema = { - "type": "object", - "properties": {"param1": {"type": "string", "description": "desc"}}, - "required": ["param1"], - } - tool_entity.get_llm_parameters_json_schema.return_value = schema - - mocker.patch.object(module.ToolManager, "get_agent_tool_runtime", return_value=tool_entity) - mocker.patch.object(module, "PromptMessageTool", side_effect=lambda **kw: MagicMock(**kw)) - - prompt_tool, entity = runner._convert_tool_to_prompt_message_tool(tool) - assert entity == tool_entity - assert prompt_tool.parameters == schema - - -# ========================================================== -# _init_prompt_tools additional branches -# ========================================================== - - -class TestInitPromptToolsExtended: - def test_agent_tool_branch(self, runner: BaseAgentRunner, mocker: MockerFixture): - agent_tool = mocker.MagicMock(tool_name="agent_tool") - runner.app_config.agent = mocker.MagicMock(tools=[agent_tool]) - mocker.patch.object(runner, "_convert_tool_to_prompt_message_tool", return_value=(MagicMock(), "entity")) - - tools, prompts = runner._init_prompt_tools() - assert "agent_tool" in tools - - def test_exception_in_conversion(self, runner: BaseAgentRunner, mocker: MockerFixture): - agent_tool = mocker.MagicMock(tool_name="bad_tool") - runner.app_config.agent = mocker.MagicMock(tools=[agent_tool]) - mocker.patch.object(runner, "_convert_tool_to_prompt_message_tool", side_effect=Exception) - - tools, prompts = runner._init_prompt_tools() - assert tools == {} - - -# ========================================================== -# Additional Coverage Tests (DO NOT MODIFY EXISTING TESTS) -# ========================================================== - - -class TestAdditionalCoverage: - def test_save_agent_thought_existing_labels(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = mocker.MagicMock() - agent.tool = "tool1" - agent.tool_labels = {"tool1": {"en_US": "existing"}} - agent.thought = "" - mock_db_session.scalar.return_value = agent - - runner.save_agent_thought("id", None, None, None, None, None, None, [], None) - labels = json.loads(agent.tool_labels_str) - assert labels["tool1"]["en_US"] == "existing" - - def test_save_agent_thought_tool_meta_string(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - agent = mocker.MagicMock() - agent.tool = "tool1" - agent.tool_labels = {} - agent.thought = "" - mock_db_session.scalar.return_value = agent +def test_update_prompt_tool_replaces_parameters(runner: BaseAgentRunner, mocker: MockerFixture) -> None: + tool = mocker.Mock(spec=Tool) + schema = { + "type": "object", + "properties": {"p1": {"type": "string", "description": "desc"}}, + "required": ["p1"], + } + tool.get_llm_parameters_json_schema.return_value = schema + prompt_tool = PromptMessageTool(name="tool", description="", parameters={"properties": {}, "required": []}) + + result = runner.update_prompt_message_tool(tool, prompt_tool) + + assert result.parameters == schema + + +@pytest.mark.parametrize(("files", "expected_files"), [(["f1"], '["f1"]'), ([], "")]) +def test_create_agent_thought_persists_row( + runner: BaseAgentRunner, + database_session: Session, + files: list[str], + expected_files: str, +) -> None: + thought_id = runner.create_agent_thought("message-1", "message", "tool", "input", files) + + stored = database_session.get(MessageAgentThought, thought_id) + assert stored is not None + assert stored.message_id == "message-1" + assert stored.message_files == expected_files + assert stored.position == 1 + assert runner.agent_thought_count == 1 + + +def _persist_thought(session: Session, *, tool: str = "tool1;tool2") -> MessageAgentThought: + thought = _thought(thought_id="thought-1", tool=tool, thought="") + session.add(thought) + session.commit() + return thought + + +def test_save_agent_thought_rejects_missing_row(runner: BaseAgentRunner, database_session: Session) -> None: + assert database_session.get(MessageAgentThought, "missing") is None + with pytest.raises(ValueError, match="agent thought not found"): + runner.save_agent_thought("missing", None, None, None, None, None, None, [], None) + + +def test_save_agent_thought_full_update( + runner: BaseAgentRunner, + database_session: Session, + mocker: MockerFixture, +) -> None: + thought = _persist_thought(database_session) + label = I18nObject(en_US="label") + mocker.patch.object(module.ToolManager, "get_tool_label", return_value=label) + usage = LLMUsage( + prompt_tokens=1, + prompt_price_unit=Decimal("0.1"), + prompt_unit_price=Decimal("0.1"), + prompt_price=Decimal("0.1"), + completion_tokens=2, + completion_price_unit=Decimal("0.2"), + completion_unit_price=Decimal("0.2"), + completion_price=Decimal("0.2"), + total_tokens=3, + total_price=Decimal("0.3"), + currency="USD", + latency=0, + ) + + runner.save_agent_thought( + thought.id, + "tool1;tool2", + {"a": 1}, + "thought", + {"b": 2}, + {"meta": 1}, + "answer", + ["f1"], + usage, + ) + + stored = database_session.get(MessageAgentThought, thought.id) + assert stored is not None + assert stored.answer == "answer" + assert stored.tokens == 3 + assert stored.tool_input == '{"a": 1}' + assert stored.observation == '{"b": 2}' + assert stored.message_files == '["f1"]' + assert "tool1" in json.loads(stored.tool_labels_str) + + +def test_save_agent_thought_uses_label_fallback( + runner: BaseAgentRunner, + database_session: Session, + mocker: MockerFixture, +) -> None: + thought = _persist_thought(database_session, tool="unknown_tool") + mocker.patch.object(module.ToolManager, "get_tool_label", return_value=None) + + runner.save_agent_thought(thought.id, None, None, None, None, None, None, [], None) + + stored = database_session.get(MessageAgentThought, thought.id) + assert stored is not None + assert json.loads(stored.tool_labels_str)["unknown_tool"]["en_US"] == "unknown_tool" + + +def test_save_agent_thought_preserves_existing_labels( + runner: BaseAgentRunner, + database_session: Session, +) -> None: + thought = _persist_thought(database_session, tool="tool1") + thought.tool_labels_str = json.dumps({"tool1": {"en_US": "existing"}}) + database_session.commit() + + runner.save_agent_thought(thought.id, None, None, None, None, None, None, [], None) + + stored = database_session.get(MessageAgentThought, thought.id) + assert stored is not None + assert json.loads(stored.tool_labels_str)["tool1"]["en_US"] == "existing" + + +def test_save_agent_thought_serialization_fallbacks( + runner: BaseAgentRunner, + database_session: Session, + mocker: MockerFixture, +) -> None: + thought = _persist_thought(database_session, tool="tool1;;") + mocker.patch.object(module.ToolManager, "get_tool_label", return_value=None) + tool_input = {"a": 1} + observation = {"b": 2} + tool_meta = {"c": 3} + real_dumps = json.dumps + + def dumps_side_effect(value, *args, **kwargs): + if value in (tool_input, observation, tool_meta) and kwargs.get("ensure_ascii") is False: + raise TypeError("fail") + return real_dumps(value, *args, **kwargs) + + mocker.patch.object(module.json, "dumps", side_effect=dumps_side_effect) + + runner.save_agent_thought( + thought.id, + "tool1;;", + tool_input, + None, + observation, + tool_meta, + None, + [], + None, + ) + + stored = database_session.get(MessageAgentThought, thought.id) + assert stored is not None + assert isinstance(stored.tool_input, str) + assert isinstance(stored.observation, str) + assert isinstance(stored.tool_meta_str, str) + assert "" not in json.loads(stored.tool_labels_str) + + +@pytest.mark.parametrize("messages_ids", [None, []]) +def test_save_agent_thought_accepts_empty_message_ids( + runner: BaseAgentRunner, + database_session: Session, + messages_ids: list[str] | None, +) -> None: + thought = _persist_thought(database_session) + runner.save_agent_thought(thought.id, None, None, None, None, "meta_string", None, messages_ids, None) # type: ignore[arg-type] + + stored = database_session.get(MessageAgentThought, thought.id) + assert stored is not None + assert stored.tool_meta_str == "meta_string" + + +def _message_file( + *, + message_id: str = "m1", + belongs_to: MessageFileBelongsTo = MessageFileBelongsTo.ASSISTANT, + upload_file_id: str = "upload-1", +) -> MessageFile: + return MessageFile( + message_id=message_id, + type=FileType.IMAGE, + transfer_method=FileTransferMethod.LOCAL_FILE, + created_by_role=CreatorUserRole.ACCOUNT, + created_by="user", + belongs_to=belongs_to, + upload_file_id=upload_file_id, + ) + + +def _file_enabled_app_model_config() -> AppModelConfig: + return AppModelConfig( + app_id="app1", + file_upload=json.dumps( + { + "enabled": True, + "allowed_file_types": [FileType.IMAGE], + "allowed_file_extensions": [".png"], + "allowed_file_upload_methods": [FileTransferMethod.LOCAL_FILE], + "number_limits": 1, + "image": {"detail": "low"}, + } + ), + ) + + +def _upload_file() -> UploadFile: + return UploadFile( + tenant_id="tenant", + storage_type=StorageType.LOCAL, + key="image.png", + name="image.png", + size=1, + extension="png", + mime_type="image/png", + created_by_role=CreatorUserRole.ACCOUNT, + created_by="user", + created_at=datetime.now(), + used=False, + ) + + +def test_organize_user_prompt_without_files(runner: BaseAgentRunner, sqlite_session: Session) -> None: + result = runner.organize_agent_user_prompt(_message(message_id="m1"), session=sqlite_session) + assert result.content == "hello" + + +def test_organize_user_prompt_with_files_but_no_config(runner: BaseAgentRunner, sqlite_session: Session) -> None: + sqlite_session.add(_message_file()) + sqlite_session.commit() + + result = runner.organize_agent_user_prompt(_message(message_id="m1"), session=sqlite_session) + + assert result.content == "hello" + + +def test_organize_user_prompt_uses_file_config( + runner: BaseAgentRunner, + sqlite_session: Session, +) -> None: + config = _file_enabled_app_model_config() + sqlite_session.add_all([config, _conversation(app_model_config_id=config.id), _message_file()]) + sqlite_session.commit() + + result = runner.organize_agent_user_prompt( + _message(message_id="m1"), + session=sqlite_session, + ) + + assert result.content == "hello" + + +def test_organize_user_prompt_builds_file_content( + runner: BaseAgentRunner, + sqlite_session: Session, + mocker: MockerFixture, +) -> None: + config = _file_enabled_app_model_config() + upload_file = _upload_file() + message_file = _message_file( + belongs_to=MessageFileBelongsTo.USER, + upload_file_id=upload_file.id, + ) + sqlite_session.add_all([config, _conversation(app_model_config_id=config.id), upload_file, message_file]) + sqlite_session.commit() + mocker.patch.object(storage, "load", return_value=b"image") + + result = runner.organize_agent_user_prompt( + _message(message_id="m1"), + session=sqlite_session, + ) + + assert isinstance(result.content, list) + assert isinstance(result.content[0], module.ImagePromptMessageContent) + assert isinstance(result.content[-1], module.TextPromptMessageContent) + + +def test_organize_history_empty_preserves_system_prompt(runner: BaseAgentRunner, sqlite_session: Session) -> None: + system_message = module.SystemPromptMessage(content="sys") + result = runner.organize_agent_history([system_message], session=sqlite_session) + assert result == [system_message] + + +def test_organize_history_with_answer_only(runner: BaseAgentRunner, sqlite_session: Session) -> None: + _persist_history(sqlite_session, _message(message_id="m1", answer="answer")) + result = runner.organize_agent_history([], session=sqlite_session) + assert any(isinstance(item, module.AssistantPromptMessage) and item.content == "answer" for item in result) + - runner.save_agent_thought("id", None, None, None, None, "meta_string", None, [], None) - assert agent.tool_meta_str == "meta_string" - - def test_convert_dataset_retriever_tool(self, runner: BaseAgentRunner, mocker: MockerFixture): - ds_tool = mocker.MagicMock() - ds_tool.entity.identity.name = "ds" - ds_tool.entity.description.llm = "desc" - - param = mocker.MagicMock() - param.name = "query" - param.llm_description = "desc" - param.required = True - - ds_tool.get_runtime_parameters.return_value = [param] - - mocker.patch.object(module, "PromptMessageTool", side_effect=lambda **kw: MagicMock(**kw)) - - prompt = runner._convert_dataset_retriever_tool_to_prompt_message_tool(ds_tool) - assert prompt is not None - - def test_organize_user_prompt_with_file_objects( - self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture - ): - mock_db_session.scalars.return_value.all.return_value = [mocker.MagicMock()] - - file_config = mocker.MagicMock() - file_config.image_config = mocker.MagicMock(detail=None) - - mocker.patch.object(module.FileUploadConfigManager, "convert", return_value=file_config) - mocker.patch.object(module.file_factory, "build_from_message_files", return_value=["file1"]) - mocker.patch.object(module.file_manager, "to_prompt_message_content", return_value=mocker.MagicMock()) - - mocker.patch.object(module, "UserPromptMessage", side_effect=lambda **kw: MagicMock(**kw)) - mocker.patch.object(module, "TextPromptMessageContent", side_effect=lambda **kw: MagicMock(**kw)) - - msg = mocker.MagicMock(id="1", query="hello") - app_model_config = mocker.MagicMock() - app_model_config.app_id = "app1" - app_model_config.to_dict.return_value = {} - msg.app_model_config_with_session.return_value = app_model_config - mocker.patch.object(module, "load_annotation_reply_config", return_value={"enabled": False}) - - result = runner.organize_agent_user_prompt(msg, session=mock_db_session) - assert result is not None - - def test_organize_history_without_tool_names(self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture): - thought = mocker.MagicMock(tool=None, thought="thinking") - msg = mocker.MagicMock(id="m3", agent_thoughts=[thought], answer=None, app_model_config=None) - msg.agent_thoughts_with_session.return_value = [thought] - msg.app_model_config_with_session.return_value = None - - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - - result = runner.organize_agent_history([], session=mock_db_session) - assert isinstance(result, list) - - def test_organize_history_multiple_tools_split( - self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture - ): - thought = mocker.MagicMock( - tool="tool1;tool2", - tool_input=json.dumps({"tool1": {}, "tool2": {}}), - observation=json.dumps({"tool1": "o1", "tool2": "o2"}), - thought="thinking", - ) - msg = mocker.MagicMock(id="m4", agent_thoughts=[thought], answer=None, app_model_config=None) - msg.agent_thoughts_with_session.return_value = [thought] - msg.app_model_config_with_session.return_value = None - - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - mocker.patch("uuid.uuid4", return_value="uuid") - - result = runner.organize_agent_history([], session=mock_db_session) - assert isinstance(result, list) - - -class TestConvertDatasetRetrieverTool: - def test_required_param_added(self, runner: BaseAgentRunner, mocker: MockerFixture): - ds_tool = mocker.MagicMock() - ds_tool.entity.identity.name = "ds" - ds_tool.entity.description.llm = "desc" - - param = mocker.MagicMock() - param.name = "query" - param.llm_description = "desc" - param.required = True - - ds_tool.get_runtime_parameters.return_value = [param] - - mocker.patch.object(module, "PromptMessageTool", side_effect=lambda **kw: MagicMock(**kw)) - - prompt = runner._convert_dataset_retriever_tool_to_prompt_message_tool(ds_tool) - - assert prompt is not None - - -class TestBaseAgentRunnerInit: - def test_init_sets_stream_tool_call_and_files(self, mocker: MockerFixture): - caller_session = mocker.MagicMock() - caller_session.scalar.return_value = 2 - global_session = mocker.MagicMock() - mocker.patch.object(model_module.db, "session", global_session) - organize_agent_history = mocker.patch.object(BaseAgentRunner, "organize_agent_history", return_value=[]) - get_dataset_tools = mocker.patch.object( - module.DatasetRetrieverTool, "get_dataset_tools", return_value=["ds_tool"] - ) - - llm = mocker.MagicMock() - llm.get_model_schema.return_value = mocker.MagicMock( - features=[module.ModelFeature.STREAM_TOOL_CALL, module.ModelFeature.VISION] - ) - model_instance = mocker.MagicMock(model_type_instance=llm, model="m", credentials="c") - - app_config = mocker.MagicMock() - app_config.app_id = "app1" - app_config.agent = None - app_config.dataset = mocker.MagicMock(dataset_ids=["d1"], retrieve_config={"k": "v"}) - app_config.additional_features = mocker.MagicMock(show_retrieve_source=True) - - app_generate = mocker.MagicMock(invoke_from="test", inputs={}, files=["file1"]) - message = mocker.MagicMock(id="msg1", conversation_id="conv1") - - runner = BaseAgentRunner( - session=caller_session, - tenant_id="tenant", - application_generate_entity=app_generate, - conversation=mocker.MagicMock(), - app_config=app_config, - model_config=mocker.MagicMock(), - config=mocker.MagicMock(), - queue_manager=mocker.MagicMock(), - message=message, - user_id="user", - model_instance=model_instance, - ) - - assert runner.stream_tool_call is True - assert runner.files == ["file1"] - assert runner.dataset_tools == ["ds_tool"] - assert runner.agent_thought_count == 2 - organize_agent_history.assert_called_once_with(session=caller_session, prompt_messages=[]) - assert get_dataset_tools.call_args.kwargs["session"] is caller_session - assert global_session.mock_calls == [] - - -class TestBaseAgentRunnerCoverage: - def test_init_prompt_tools_adds_dataset_tools(self, runner: BaseAgentRunner, mocker: MockerFixture): - dataset_tool = mocker.MagicMock() - dataset_tool.entity.identity.name = "ds" - runner.dataset_tools = [dataset_tool] - - mocker.patch.object(runner, "_convert_dataset_retriever_tool_to_prompt_message_tool", return_value=MagicMock()) - - tools, prompt_tools = runner._init_prompt_tools() - - assert tools["ds"] == dataset_tool - assert len(prompt_tools) == 1 - - def test_save_agent_thought_json_dumps_fallbacks( - self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture - ): - agent = mocker.MagicMock() - agent.tool = "tool1" - agent.tool_labels = {} - agent.thought = "" - mock_db_session.scalar.return_value = agent - - mocker.patch.object(module.ToolManager, "get_tool_label", return_value=None) - - tool_input = {"a": 1} - observation = {"b": 2} - tool_meta = {"c": 3} - - real_dumps = json.dumps - - def dumps_side_effect(value, *args, **kwargs): - if value in (tool_input, observation, tool_meta) and kwargs.get("ensure_ascii") is False: - raise TypeError("fail") - return real_dumps(value, *args, **kwargs) - - mocker.patch.object(module.json, "dumps", side_effect=dumps_side_effect) - - runner.save_agent_thought( - "id", - "tool1", - tool_input, - None, - observation, - tool_meta, - None, - [], - None, - ) - - assert isinstance(agent.tool_input, str) - assert isinstance(agent.observation, str) - assert isinstance(agent.tool_meta_str, str) - - def test_save_agent_thought_skips_empty_tool_name( - self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture - ): - agent = mocker.MagicMock() - agent.tool = "tool1;;" - agent.tool_labels = {} - agent.thought = "" - mock_db_session.scalar.return_value = agent - - mocker.patch.object(module.ToolManager, "get_tool_label", return_value=None) - - runner.save_agent_thought("id", None, None, None, None, None, None, [], None) - - labels = json.loads(agent.tool_labels_str) - assert "" not in labels - - def test_organize_history_includes_system_prompt( - self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture - ): - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [] - mocker.patch.object(module, "extract_thread_messages", return_value=[]) - - system_message = module.SystemPromptMessage(content="sys") - - result = runner.organize_agent_history([system_message], session=mock_db_session) - - assert system_message in result - - def test_organize_history_tool_inputs_and_observation_none( - self, runner: BaseAgentRunner, mock_db_session, mocker: MockerFixture - ): - thought = mocker.MagicMock( - tool="tool1", - tool_input=None, - observation=None, - thought="thinking", - ) - msg = mocker.MagicMock(id="m6", agent_thoughts=[thought], answer=None, app_model_config=None) - msg.agent_thoughts_with_session.return_value = [thought] - - mock_db_session.execute.return_value.scalars.return_value.all.return_value = [msg] - mocker.patch.object(module, "extract_thread_messages", return_value=[msg]) - mocker.patch("uuid.uuid4", return_value="uuid") - - mocker.patch.object( - runner, - "organize_agent_user_prompt", - return_value=module.UserPromptMessage(content="user"), - ) - - result = runner.organize_agent_history([], session=mock_db_session) - - assert any(isinstance(item, module.ToolPromptMessage) for item in result) +def test_organize_history_skips_current_message(runner: BaseAgentRunner, sqlite_session: Session) -> None: + _persist_history(sqlite_session, _message(message_id="msg_current", answer="answer")) + result = runner.organize_agent_history([], session=sqlite_session) + assert result == [] + + +@pytest.mark.parametrize( + ("tool", "tool_input", "observation"), + [ + ("tool1", "invalid", "invalid"), + (";", None, None), + ("tool1", None, None), + ("tool1;tool2", json.dumps({"tool1": {}, "tool2": {}}), json.dumps({"tool1": "o1", "tool2": "o2"})), + ], +) +def test_organize_history_reconstructs_tool_flows( + runner: BaseAgentRunner, + sqlite_session: Session, + tool: str, + tool_input: str | None, + observation: str | None, +) -> None: + message = _message(message_id="m2") + thought = _thought( + thought_id="thought-1", + message_id=message.id, + tool=tool, + tool_input=tool_input, + observation=observation, + ) + _persist_history(sqlite_session, message, thoughts=[thought]) + + result = runner.organize_agent_history([], session=sqlite_session) + + assert isinstance(result, list) + assert any(isinstance(item, module.AssistantPromptMessage) for item in result) + + +def test_organize_history_without_tool_name(runner: BaseAgentRunner, sqlite_session: Session) -> None: + message = _message(message_id="m3") + thought = _thought(thought_id="thought-1", message_id=message.id, tool=None) + _persist_history(sqlite_session, message, thoughts=[thought]) + + result = runner.organize_agent_history([], session=sqlite_session) + + assert any(isinstance(item, module.AssistantPromptMessage) and item.content == "thinking" for item in result) + + +def test_convert_tool_to_prompt_message_tool(runner: BaseAgentRunner, mocker: MockerFixture) -> None: + tool = _agent_tool("tool1") + tool_entity = mocker.Mock(spec=Tool, entity=_tool_entity("tool1")) + schema = { + "type": "object", + "properties": {"param1": {"type": "string", "description": "desc"}}, + "required": ["param1"], + } + tool_entity.get_llm_parameters_json_schema.return_value = schema + mocker.patch.object(module.ToolManager, "get_agent_tool_runtime", return_value=tool_entity) + + prompt_tool, entity = runner._convert_tool_to_prompt_message_tool(tool) + + assert entity is tool_entity + assert prompt_tool.parameters == schema + + +def test_convert_dataset_retriever_tool(runner: BaseAgentRunner, mocker: MockerFixture) -> None: + dataset_tool = _dataset_tool(mocker, "ds") + + prompt = runner._convert_dataset_retriever_tool_to_prompt_message_tool(dataset_tool) + + assert prompt.name == "ds" + assert prompt.parameters["required"] == ["query"] + + +def test_init_prompt_tools_adds_agent_and_dataset_tools(runner: BaseAgentRunner, mocker: MockerFixture) -> None: + agent_tool = _agent_tool("agent_tool") + agent_runtime = mocker.Mock(spec=Tool, entity=_tool_entity("agent_tool")) + agent_runtime.get_llm_parameters_json_schema.return_value = {"type": "object", "properties": {}} + mocker.patch.object(module.ToolManager, "get_agent_tool_runtime", return_value=agent_runtime) + dataset_tool = _dataset_tool(mocker, "dataset_tool") + runner.app_config.agent = _agent(agent_tool) + runner.dataset_tools = [dataset_tool] + + tools, prompts = runner._init_prompt_tools() + + assert tools == {"agent_tool": agent_runtime, "dataset_tool": dataset_tool} + assert len(prompts) == 2 + + +def test_init_prompt_tools_skips_deleted_agent_tool(runner: BaseAgentRunner, mocker: MockerFixture) -> None: + agent_tool = _agent_tool("bad_tool") + runner.app_config.agent = _agent(agent_tool) + mocker.patch.object(module.ToolManager, "get_agent_tool_runtime", side_effect=Exception) + + tools, prompts = runner._init_prompt_tools() + + assert tools == {} + assert prompts == [] + + +def test_init_uses_real_session_for_count_and_dependencies( + sqlite_session: Session, + mocker: MockerFixture, +) -> None: + sqlite_session.add_all( + [ + _thought(thought_id="thought-1", message_id="msg1"), + _thought(thought_id="thought-2", message_id="msg1"), + _thought(thought_id="decoy-thought", message_id="other-message"), + ] + ) + sqlite_session.commit() + get_dataset_tools = mocker.patch.object( + module.DatasetRetrieverTool, + "get_dataset_tools", + return_value=["ds_tool"], + ) + llm = mocker.Mock(spec=LargeLanguageModel) + llm.get_model_schema.return_value = mocker.Mock( + features=[module.ModelFeature.STREAM_TOOL_CALL, module.ModelFeature.VISION] + ) + model_instance = mocker.Mock( + spec=ModelInstance, + model_type_instance=llm, + model_name="m", + credentials="c", + ) + app_config = _app_config( + dataset=DatasetEntity( + dataset_ids=["d1"], + retrieve_config=DatasetRetrieveConfigEntity( + retrieve_strategy=DatasetRetrieveConfigEntity.RetrieveStrategy.MULTIPLE, + ), + ), + additional_features=AppAdditionalFeatures(show_retrieve_source=True), + ) + app_generate = _app_generate(app_config=app_config, files=["file1"]) + message = _message(message_id="msg1") + + initialized = BaseAgentRunner( + session=sqlite_session, + tenant_id="tenant", + application_generate_entity=app_generate, + conversation=_conversation(), + app_config=app_config, + model_config=ModelConfigWithCredentialsEntity.model_construct(), + config=_agent(), + queue_manager=mocker.Mock(spec=AppQueueManager), + message=message, + user_id="user", + model_instance=model_instance, + ) + + assert initialized.stream_tool_call is True + assert initialized.files == ["file1"] + assert initialized.dataset_tools == ["ds_tool"] + assert initialized.agent_thought_count == 2 + assert initialized.history_prompt_messages == [] + assert get_dataset_tools.call_args.kwargs["session"] is sqlite_session diff --git a/api/tests/unit_tests/core/app/apps/agent_app/test_app_generator.py b/api/tests/unit_tests/core/app/apps/agent_app/test_app_generator.py index f4a0c4e90f3..410efadba33 100644 --- a/api/tests/unit_tests/core/app/apps/agent_app/test_app_generator.py +++ b/api/tests/unit_tests/core/app/apps/agent_app/test_app_generator.py @@ -12,9 +12,13 @@ from __future__ import annotations import contextlib import inspect import json +from decimal import Decimal +from types import SimpleNamespace import pytest from pytest_mock import MockerFixture +from sqlalchemy import event +from sqlalchemy.orm import Session import core.app.apps.agent_app.app_generator as module from core.app.apps.agent_app.app_generator import ( @@ -27,14 +31,107 @@ from core.app.entities.app_invoke_entities import InvokeFrom, UserFrom from core.app.entities.queue_entities import QueueAnnotationReplyEvent from core.workflow.file_reference import build_file_reference from models import Account, AppModelConfig +from models.agent import Agent, AgentConfigSnapshot, AgentScope, AgentSource, AgentStatus +from models.agent_config_entities import AgentSoulConfig +from models.enums import AppStatus, ConversationFromSource +from models.model import App, AppMode, Conversation, Message, MessageAnnotation MODULE = "core.app.apps.agent_app.app_generator" -class DummyAccount: - def __init__(self, user_id: str) -> None: - self.id = user_id - self.session_id = f"session-{user_id}" +def _account(user_id: str = "user") -> Account: + account = Account(name="User", email=f"{user_id}@example.com") + account.id = user_id + return account + + +def _app(*, app_model_config_id: str | None = None) -> App: + return App( + id="app1", + tenant_id="tenant", + name="Agent App", + description="", + mode=AppMode.AGENT, + app_model_config_id=app_model_config_id, + status=AppStatus.NORMAL, + enable_site=False, + enable_api=False, + api_rpm=0, + api_rph=0, + ) + + +def _agent(*, agent_id: str = "agent1") -> Agent: + return Agent( + id=agent_id, + tenant_id="tenant", + name="Agent", + scope=AgentScope.ROSTER, + source=AgentSource.AGENT_APP, + status=AgentStatus.ACTIVE, + app_id="app1", + ) + + +def _snapshot(*, snapshot_id: str = "snap1", agent_id: str = "agent1") -> AgentConfigSnapshot: + return AgentConfigSnapshot( + id=snapshot_id, + tenant_id="tenant", + agent_id=agent_id, + version=1, + config_snapshot=AgentSoulConfig(), + home_snapshot_id="home-1", + ) + + +def _conversation(*, invoke_from: InvokeFrom = InvokeFrom.WEB_APP) -> Conversation: + conversation = Conversation( + id="conv", + app_id="app1", + mode=AppMode.AGENT, + name="Conversation", + invoke_from=invoke_from, + from_source=ConversationFromSource.CONSOLE, + from_account_id="user", + is_deleted=False, + ) + conversation._inputs = {} + return conversation + + +def _message(*, query: str = "query") -> Message: + message = Message( + id="msg", + app_id="app1", + conversation_id="conv", + query=query, + message={"role": "user", "content": query}, + answer="", + message_unit_price=Decimal(0), + answer_unit_price=Decimal(0), + currency="USD", + from_source=ConversationFromSource.CONSOLE, + from_account_id="user", + ) + message._inputs = {} + return message + + +_CURRENT_SESSION: Session | None = None + + +@pytest.fixture(autouse=True) +def _bind_real_session(sqlite_session: Session, monkeypatch: pytest.MonkeyPatch): + global _CURRENT_SESSION + _CURRENT_SESSION = sqlite_session + monkeypatch.setattr(module.db, "session", sqlite_session) + yield + _CURRENT_SESSION = None + + +def _session() -> Session: + assert _CURRENT_SESSION is not None + return _CURRENT_SESSION @pytest.fixture @@ -46,35 +143,35 @@ def generator(mocker: MockerFixture) -> AgentAppGenerator: class TestGenerateGuards: - def test_rejects_blocking_mode(self, generator: AgentAppGenerator, mocker: MockerFixture): + def test_rejects_blocking_mode(self, generator: AgentAppGenerator): with pytest.raises(AgentAppGeneratorError, match="only supports streaming"): generator.generate( - app_model=mocker.MagicMock(), - user=DummyAccount("u"), + app_model=_app(), + user=_account("u"), args={}, invoke_from=InvokeFrom.WEB_APP, - session=mocker.MagicMock(), + session=_session(), streaming=False, ) - def test_requires_query(self, generator: AgentAppGenerator, mocker: MockerFixture): + def test_requires_query(self, generator: AgentAppGenerator): with pytest.raises(AgentAppGeneratorError, match="query is required"): generator.generate( - app_model=mocker.MagicMock(), - user=DummyAccount("u"), + app_model=_app(), + user=_account("u"), args={"inputs": {}}, invoke_from=InvokeFrom.WEB_APP, - session=mocker.MagicMock(), + session=_session(), ) - def test_rejects_blank_query(self, generator: AgentAppGenerator, mocker: MockerFixture): + def test_rejects_blank_query(self, generator: AgentAppGenerator): with pytest.raises(AgentAppGeneratorError, match="query is required"): generator.generate( - app_model=mocker.MagicMock(), - user=DummyAccount("u"), + app_model=_app(), + user=_account("u"), args={"query": " ", "inputs": {}}, invoke_from=InvokeFrom.WEB_APP, - session=mocker.MagicMock(), + session=_session(), ) @@ -94,18 +191,17 @@ class TestGenerateSuccess: ) def test_generate_orchestrates_and_starts_worker(self, generator, mocker: MockerFixture): - app_model = mocker.MagicMock(id="app1", tenant_id="tenant", mode="agent") - app_model.app_model_config_id = "config-1" - user = DummyAccount("user") - session = mocker.MagicMock() + config = AppModelConfig(app_id="app1") + config.id = "config-1" + session = _session() + session.add(config) + session.commit() + app_model = _app(app_model_config_id=config.id) + user = _account() - generator._resolve_agent = mocker.MagicMock( - return_value=(mocker.MagicMock(id="agent1"), "snap1", "snapshot", mocker.MagicMock()) - ) + generator._resolve_agent = mocker.MagicMock(return_value=(_agent(), "snap1", "snapshot", AgentSoulConfig())) generator._prepare_user_inputs = mocker.MagicMock(return_value={"x": 1}) - generator._init_generate_records = mocker.MagicMock( - return_value=(mocker.MagicMock(id="conv", mode="agent"), mocker.MagicMock(id="msg")) - ) + generator._init_generate_records = mocker.MagicMock(return_value=(_conversation(), _message())) generator._handle_response = mocker.MagicMock(return_value="raw-response") mocker.patch( @@ -151,23 +247,19 @@ class TestGenerateSuccess: session=session, conversation=None, ) - session.get.assert_called_once_with(AppModelConfig, "config-1") + assert session.get(AppModelConfig, "config-1") is config assert generate_entity.call_args.kwargs["prompt_file_mappings"] == file_mappings assert "agent_runtime_exit_intent" not in generate_entity.call_args.kwargs def test_generate_loads_existing_conversation(self, generator: AgentAppGenerator, mocker: MockerFixture): - app_model = mocker.MagicMock(id="app1", tenant_id="tenant", mode="agent") + app_model = _app() generator._resolve_agent = mocker.MagicMock( - return_value=(mocker.MagicMock(id="a"), "snap1", "snapshot", mocker.MagicMock()) + return_value=(_agent(agent_id="a"), "snap1", "snapshot", AgentSoulConfig()) ) generator._prepare_user_inputs = mocker.MagicMock(return_value={}) - generator._init_generate_records = mocker.MagicMock( - return_value=(mocker.MagicMock(id="conv", mode="agent"), mocker.MagicMock(id="msg")) - ) + generator._init_generate_records = mocker.MagicMock(return_value=(_conversation(), _message())) generator._handle_response = mocker.MagicMock(return_value="raw") - get_conv = mocker.patch( - f"{MODULE}.ConversationService.get_conversation", return_value=mocker.MagicMock(id="conv") - ) + get_conv = mocker.patch(f"{MODULE}.ConversationService.get_conversation", return_value=_conversation()) mocker.patch(f"{MODULE}.AgentAppConfigManager.get_app_config", return_value=mocker.MagicMock(variables=[])) mocker.patch(f"{MODULE}.load_annotation_reply_config", return_value={"enabled": False}) mocker.patch(f"{MODULE}.ModelConfigConverter.convert", return_value=mocker.MagicMock()) @@ -176,8 +268,8 @@ class TestGenerateSuccess: mocker.patch(f"{MODULE}.MessageBasedAppQueueManager", return_value=mocker.MagicMock()) mocker.patch(f"{MODULE}.threading.Thread", return_value=mocker.MagicMock()) mocker.patch(f"{MODULE}.AgentAppGenerateResponseConverter.convert", return_value={"result": "ok"}) - session = mocker.MagicMock() - user = DummyAccount("user") + session = _session() + user = _account() generator.generate( app_model=app_model, @@ -200,16 +292,12 @@ class TestGenerateSuccess: def test_generate_does_not_include_trace_session_id_in_extras( self, generator: AgentAppGenerator, mocker: MockerFixture ): - app_model = mocker.MagicMock(id="app1", tenant_id="tenant", mode="agent") - user = DummyAccount("user") + app_model = _app() + user = _account() - generator._resolve_agent = mocker.MagicMock( - return_value=(mocker.MagicMock(id="agent1"), "snap1", "snapshot", mocker.MagicMock()) - ) + generator._resolve_agent = mocker.MagicMock(return_value=(_agent(), "snap1", "snapshot", AgentSoulConfig())) generator._prepare_user_inputs = mocker.MagicMock(return_value={}) - generator._init_generate_records = mocker.MagicMock( - return_value=(mocker.MagicMock(id="conv", mode="agent"), mocker.MagicMock(id="msg")) - ) + generator._init_generate_records = mocker.MagicMock(return_value=(_conversation(), _message())) generator._handle_response = mocker.MagicMock(return_value="raw-response") mocker.patch( @@ -230,7 +318,7 @@ class TestGenerateSuccess: user=user, args={"query": "hello", "inputs": {}, "trace_session_id": "session-1"}, invoke_from=InvokeFrom.WEB_APP, - session=mocker.MagicMock(), + session=_session(), streaming=True, ) @@ -255,25 +343,22 @@ class TestGenerateWorker: handled=False, guard_query="query", ): - generator._get_conversation = mocker.MagicMock(return_value=mocker.MagicMock(id="conv")) - generator._get_message = mocker.MagicMock(return_value=mocker.MagicMock(id="msg")) + generator._get_conversation = mocker.MagicMock(return_value=_conversation()) + generator._get_message = mocker.MagicMock(return_value=_message()) generator._run_input_guards = mocker.MagicMock(return_value=(handled, guard_query, None)) - resolved_agent = mocker.MagicMock(id="a") - resolved_config = mocker.MagicMock(id="s", home_snapshot_id="home-1") - generator._resolve_agent_by_id = mocker.MagicMock( - return_value=(resolved_agent, resolved_config, mocker.MagicMock()) - ) - session = mocker.MagicMock() - session.get.return_value = mocker.MagicMock(id="app1") - session_context = mocker.MagicMock() - session_context.__enter__.return_value = session - session_maker = mocker.patch(f"{MODULE}.session_factory.get_session_maker").return_value - session_maker.begin.return_value = session_context - resolver_session = mocker.MagicMock() - resolver_context = mocker.MagicMock() - resolver_context.__enter__.return_value = resolver_session - mocker.patch(f"{MODULE}.session_factory.create_session", return_value=resolver_context) - mocker.patch(f"{MODULE}.db.session.close") + resolved_agent = _agent(agent_id="a") + resolved_config = _snapshot(snapshot_id="s", agent_id="a") + resolver_sessions: list[Session] = [] + + def resolve_agent_by_id(**kwargs): + resolver_sessions.append(kwargs["session"]) + return resolved_agent, resolved_config, AgentSoulConfig() + + generator._resolve_agent_by_id = mocker.MagicMock(side_effect=resolve_agent_by_id) + session = _session() + if session.get(App, "app1") is None: + session.add(_app()) + session.commit() mocker.patch(f"{MODULE}.DifyRunContext", return_value=mocker.MagicMock()) mocker.patch(f"{MODULE}.AgentAppRuntimeRequestBuilder", return_value=mocker.MagicMock()) mocker.patch(f"{MODULE}.create_agent_backend_run_client", return_value=mocker.MagicMock()) @@ -283,7 +368,7 @@ class TestGenerateWorker: if run_side_effect is not None: runner.run.side_effect = run_side_effect mocker.patch(f"{MODULE}.AgentAppRunner", return_value=runner) - return runner, resolver_session + return runner, resolver_sessions def _call( self, @@ -300,6 +385,7 @@ class TestGenerateWorker: flask_app=mocker.MagicMock(), context=mocker.MagicMock(), application_generate_entity=mocker.MagicMock( + app_config=SimpleNamespace(app_id="app1", tenant_id="tenant"), agent_id="a", agent_config_snapshot_id="s", agent_session_scope_config_version_id=session_scope_config_version_id, @@ -315,11 +401,12 @@ class TestGenerateWorker: ) def test_happy_path_runs_backend(self, generator: AgentAppGenerator, mocker: MockerFixture): - runner, resolver_session = self._wire(generator, mocker) + runner, resolver_sessions = self._wire(generator, mocker) queue_manager = mocker.MagicMock() self._call(generator, mocker, queue_manager) runner.run.assert_called_once() - assert generator._resolve_agent_by_id.call_args.kwargs["session"] is resolver_session + assert resolver_sessions == [generator._resolve_agent_by_id.call_args.kwargs["session"]] + assert resolver_sessions[0].get_bind() is not None assert runner.run.call_args.kwargs["home_snapshot_id"] == "home-1" assert "home_snapshot_ref" not in runner.run.call_args.kwargs queue_manager.publish_error.assert_not_called() @@ -384,20 +471,30 @@ class TestGenerateWorker: def test_annotation_reply_publishes_after_guard_transaction_commits(self, generator, mocker: MockerFixture): runner, _ = self._wire(generator, mocker, handled=True) - annotation_reply = mocker.MagicMock(id="annotation-1", content="annotated answer") + annotation_reply = MessageAnnotation( + app_id="app1", + question="query", + content="annotated answer", + account_id="user", + ) generator._run_input_guards.return_value = (True, "query", annotation_reply) events: list[str] = [] - guard_context = module.session_factory.get_session_maker.return_value.begin.return_value - guard_context.__exit__.side_effect = lambda *args: events.append("commit") or False queue_manager = mocker.MagicMock() + def record_commit(_session: Session) -> None: + events.append("commit") + def publish(event, *_args): if isinstance(event, QueueAnnotationReplyEvent): events.append("publish") queue_manager.publish.side_effect = publish - self._call(generator, mocker, queue_manager) + event.listen(type(_session()), "after_commit", record_commit) + try: + self._call(generator, mocker, queue_manager) + finally: + event.remove(type(_session()), "after_commit", record_commit) assert events == ["commit", "publish"] runner.run.assert_not_called() @@ -451,16 +548,12 @@ class TestResumeAfterFormSubmission: composition's user-prompt layer matches the suspended snapshot (never blank).""" def _wire(self, generator, mocker: MockerFixture): - generator._resolve_agent = mocker.MagicMock( - return_value=(mocker.MagicMock(id="agent1"), "snap1", "draft", mocker.MagicMock()) - ) - generator._init_generate_records = mocker.MagicMock( - return_value=(mocker.MagicMock(id="conv", mode="agent"), mocker.MagicMock(id="msg")) - ) + generator._resolve_agent = mocker.MagicMock(return_value=(_agent(), "snap1", "draft", AgentSoulConfig())) + generator._init_generate_records = mocker.MagicMock(return_value=(_conversation(), _message())) generator._handle_response = mocker.MagicMock(return_value=None) get_conversation = mocker.patch( f"{MODULE}.ConversationService.get_conversation", - return_value=mocker.MagicMock(id="conv", invoke_from=InvokeFrom.WEB_APP), + return_value=_conversation(), ) mocker.patch(f"{MODULE}.AgentAppConfigManager.get_app_config", return_value=mocker.MagicMock(variables=[])) mocker.patch(f"{MODULE}.load_annotation_reply_config", return_value={"enabled": False}) @@ -478,12 +571,13 @@ class TestResumeAfterFormSubmission: def test_resume_resends_paused_turn_query(self, generator, mocker: MockerFixture): entity, get_conversation = self._wire(generator, mocker) - app_model = mocker.MagicMock(id="app1", tenant_id="tenant", mode="agent") - app_model.app_model_config_id = "config-1" - user = DummyAccount("user") - session = mocker.MagicMock() - session.get.return_value = mocker.MagicMock() - session.scalar.return_value = mocker.MagicMock(query="original question") + session = _session() + config = AppModelConfig(app_id="app1") + config.id = "config-1" + session.add_all([config, _conversation(), _message(query="original question")]) + session.commit() + app_model = _app(app_model_config_id=config.id) + user = _account() generator.resume_after_form_submission( app_model=app_model, @@ -504,17 +598,16 @@ class TestResumeAfterFormSubmission: session=session, ) assert generator._init_generate_records.call_args.kwargs["session"] is session - session.get.assert_called_once_with(AppModelConfig, "config-1") + assert session.get(AppModelConfig, "config-1") is config assert generator._resolve_agent.call_args.kwargs["session"] is session def test_resume_falls_back_to_placeholder_when_no_paused_message(self, generator, mocker: MockerFixture): entity, _ = self._wire(generator, mocker) - session = mocker.MagicMock() - session.scalar.return_value = None + session = _session() generator.resume_after_form_submission( - app_model=mocker.MagicMock(id="app1", tenant_id="tenant", mode="agent"), - user=DummyAccount("user"), + app_model=_app(), + user=_account(), conversation_id="conv", form_id="form-1", invoke_from=InvokeFrom.WEB_APP, @@ -526,15 +619,17 @@ class TestResumeAfterFormSubmission: def test_resume_uses_build_draft_for_debugger_conversation(self, generator, mocker: MockerFixture): self._wire(generator, mocker) - conversation = mocker.MagicMock(id="conv", invoke_from=InvokeFrom.DEBUGGER) + conversation = _conversation(invoke_from=InvokeFrom.DEBUGGER) mocker.patch(f"{MODULE}.ConversationService.get_conversation", return_value=conversation) generator._resolve_resume_draft.return_value = ("debug_build", "draft-build-1") account_user = Account(name="Test Account", email="test@example.com") account_user.id = "user" - app_model = mocker.MagicMock(id="app1", tenant_id="tenant", mode="agent") - app_model.app_model_config_id = "config-1" - session = mocker.MagicMock() - session.scalar.return_value = mocker.MagicMock(query="original question") + session = _session() + config = AppModelConfig(app_id="app1") + config.id = "config-1" + session.add_all([config, conversation, _message(query="original question")]) + session.commit() + app_model = _app(app_model_config_id=config.id) generator.resume_after_form_submission( app_model=app_model, diff --git a/api/tests/unit_tests/core/app/apps/agent_chat/test_agent_chat_app_runner.py b/api/tests/unit_tests/core/app/apps/agent_chat/test_agent_chat_app_runner.py index 1bbfb9e5f1f..1c1ee3663dd 100644 --- a/api/tests/unit_tests/core/app/apps/agent_chat/test_agent_chat_app_runner.py +++ b/api/tests/unit_tests/core/app/apps/agent_chat/test_agent_chat_app_runner.py @@ -3,7 +3,7 @@ from datetime import datetime import pytest from pytest_mock import MockerFixture from sqlalchemy import event -from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy.orm import Session from core.agent.entities import AgentEntity from core.app.apps.agent_chat.app_runner import AgentChatAppRunner @@ -12,7 +12,7 @@ from core.moderation.base import ModerationError from graphon.model_runtime.entities.llm_entities import LLMMode from graphon.model_runtime.entities.model_entities import ModelFeature, ModelPropertyKey from models.enums import ConversationFromSource -from models.model import App, AppMode, Conversation, Message +from models.model import App, AppMode, Conversation, Message, MessageAnnotation @pytest.fixture @@ -74,9 +74,12 @@ def runner(sqlite_session: Session): return AgentChatAppRunner() -@pytest.fixture(autouse=True) -def _patch_create_session(mocker: MockerFixture, sqlite_session_factory: sessionmaker[Session]) -> None: - mocker.patch("core.app.apps.agent_chat.app_runner.create_session", side_effect=sqlite_session_factory) +def _records(session: Session) -> tuple[Conversation, Message]: + conversation = session.get(Conversation, "conv") + message = session.get(Message, "msg") + assert conversation is not None + assert message is not None + return conversation, message class TestAgentChatAppRunnerRun: @@ -88,14 +91,14 @@ class TestAgentChatAppRunnerRun: assert app is not None sqlite_session.delete(app) sqlite_session.commit() + conversation, message = _records(sqlite_session) with pytest.raises(ValueError): - runner.run(generate_entity, mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), sqlite_session) + runner.run(generate_entity, mocker.MagicMock(), conversation, message, sqlite_session) def test_run_moderation_error_direct_output( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = mocker.MagicMock() generate_entity = mocker.MagicMock( @@ -111,15 +114,15 @@ class TestAgentChatAppRunnerRun: mocker.patch.object(runner, "organize_prompt_messages", return_value=([], None)) mocker.patch.object(runner, "moderation_for_inputs", side_effect=ModerationError("bad")) mocker.patch.object(runner, "direct_output") + conversation, message = _records(sqlite_session) - runner.run(generate_entity, mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), sqlite_session) + runner.run(generate_entity, mocker.MagicMock(), conversation, message, sqlite_session) runner.direct_output.assert_called_once() def test_run_annotation_reply_short_circuits( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = mocker.MagicMock() generate_entity = mocker.MagicMock( @@ -136,13 +139,19 @@ class TestAgentChatAppRunnerRun: mocker.patch.object(runner, "organize_prompt_messages", return_value=([], None)) mocker.patch.object(runner, "moderation_for_inputs", return_value=(None, {}, "q")) - annotation = mocker.MagicMock(id="anno", content="answer") + annotation = MessageAnnotation( + app_id="app1", + question="q", + content="answer", + account_id="user", + ) annotation_query = mocker.patch.object(runner, "query_app_annotations_to_reply", return_value=annotation) mocker.patch.object(runner, "direct_output") queue_manager = mocker.MagicMock() write_session = sqlite_session - runner.run(generate_entity, queue_manager, mocker.MagicMock(), mocker.MagicMock(), write_session) + conversation, message = _records(sqlite_session) + runner.run(generate_entity, queue_manager, conversation, message, write_session) queue_manager.publish.assert_called_once() assert annotation_query.call_args.kwargs["session"] is write_session @@ -151,7 +160,6 @@ class TestAgentChatAppRunnerRun: def test_run_hosting_moderation_short_circuits( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = mocker.MagicMock() generate_entity = mocker.MagicMock( @@ -170,11 +178,11 @@ class TestAgentChatAppRunnerRun: mocker.patch.object(runner, "moderation_for_inputs", return_value=(None, {}, "q")) mocker.patch.object(runner, "query_app_annotations_to_reply", return_value=None) mocker.patch.object(runner, "check_hosting_moderation", return_value=True) + conversation, message = _records(sqlite_session) - runner.run(generate_entity, mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), sqlite_session) + runner.run(generate_entity, mocker.MagicMock(), conversation, message, sqlite_session) def test_run_model_schema_missing(self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = AgentEntity(provider="p", model="m", strategy=AgentEntity.Strategy.CHAIN_OF_THOUGHT) @@ -203,9 +211,10 @@ class TestAgentChatAppRunnerRun: llm_instance = mocker.MagicMock() llm_instance.model_type_instance.get_model_schema.return_value = None mocker.patch("core.app.apps.agent_chat.app_runner.ModelInstance", return_value=llm_instance) + conversation, message = _records(sqlite_session) with pytest.raises(ValueError): - runner.run(generate_entity, mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock(), sqlite_session) + runner.run(generate_entity, mocker.MagicMock(), conversation, message, sqlite_session) @pytest.mark.parametrize( ("mode", "expected_runner"), @@ -222,7 +231,6 @@ class TestAgentChatAppRunnerRun: expected_runner, sqlite_session: Session, ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = AgentEntity(provider="p", model="m", strategy=AgentEntity.Strategy.CHAIN_OF_THOUGHT) @@ -256,8 +264,7 @@ class TestAgentChatAppRunnerRun: llm_instance.model_type_instance.get_model_schema.return_value = model_schema mocker.patch("core.app.apps.agent_chat.app_runner.ModelInstance", return_value=llm_instance) - conversation = mocker.MagicMock(id="conv") - message = mocker.MagicMock(id="msg") + conversation, message = _records(sqlite_session) runner_cls = mocker.MagicMock() mocker.patch(f"core.app.apps.agent_chat.app_runner.{expected_runner}", runner_cls) @@ -268,25 +275,23 @@ class TestAgentChatAppRunnerRun: runner_instance.run.side_effect = lambda **_kwargs: events.append("agent-run") or [] mocker.patch.object(runner, "_handle_invoke_result") session = sqlite_session - event.listen(session, "after_commit", lambda _session: events.append("commit")) - original_close = session.close - def close_session() -> None: - events.append("close") - original_close() + def record_commit(_session: Session) -> None: + events.append("commit") - session.close = close_session + event.listen(session, "after_commit", record_commit) + try: + runner.run(generate_entity, mocker.MagicMock(), conversation, message, session) + finally: + event.remove(session, "after_commit", record_commit) - runner.run(generate_entity, mocker.MagicMock(), conversation, message, session) - - assert events == ["commit", "close", "commit", "close", "agent-run"] + assert events == ["commit", "commit", "agent-run"] runner_instance.run.assert_called_once() runner._handle_invoke_result.assert_called_once() def test_run_invalid_llm_mode_raises( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = AgentEntity(provider="p", model="m", strategy=AgentEntity.Strategy.CHAIN_OF_THOUGHT) @@ -320,8 +325,7 @@ class TestAgentChatAppRunnerRun: llm_instance.model_type_instance.get_model_schema.return_value = model_schema mocker.patch("core.app.apps.agent_chat.app_runner.ModelInstance", return_value=llm_instance) - conversation = mocker.MagicMock(id="conv") - message = mocker.MagicMock(id="msg") + conversation, message = _records(sqlite_session) with pytest.raises(ValueError): runner.run(generate_entity, mocker.MagicMock(), conversation, message, sqlite_session) @@ -329,7 +333,6 @@ class TestAgentChatAppRunnerRun: def test_run_function_calling_strategy_selected_by_features( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = AgentEntity(provider="p", model="m", strategy=AgentEntity.Strategy.CHAIN_OF_THOUGHT) @@ -363,8 +366,7 @@ class TestAgentChatAppRunnerRun: llm_instance.model_type_instance.get_model_schema.return_value = model_schema mocker.patch("core.app.apps.agent_chat.app_runner.ModelInstance", return_value=llm_instance) - conversation = mocker.MagicMock(id="conv") - message = mocker.MagicMock(id="msg") + conversation, message = _records(sqlite_session) runner_cls = mocker.MagicMock() mocker.patch("core.app.apps.agent_chat.app_runner.FunctionCallAgentRunner", runner_cls) @@ -382,7 +384,6 @@ class TestAgentChatAppRunnerRun: def test_run_conversation_not_found( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = AgentEntity(provider="p", model="m", strategy=AgentEntity.Strategy.FUNCTION_CALLING) @@ -403,8 +404,7 @@ class TestAgentChatAppRunnerRun: user_id="user", ) - conversation_record = sqlite_session.get(Conversation, "conv") - assert conversation_record is not None + conversation_record, message_record = _records(sqlite_session) sqlite_session.delete(conversation_record) sqlite_session.commit() mocker.patch.object(runner, "organize_prompt_messages", return_value=([], None)) @@ -416,13 +416,12 @@ class TestAgentChatAppRunnerRun: runner.run( generate_entity, mocker.MagicMock(), - mocker.MagicMock(id="conv"), - mocker.MagicMock(id="msg"), + conversation_record, + message_record, sqlite_session, ) def test_run_message_not_found(self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = AgentEntity(provider="p", model="m", strategy=AgentEntity.Strategy.FUNCTION_CALLING) @@ -443,8 +442,7 @@ class TestAgentChatAppRunnerRun: user_id="user", ) - message_record = sqlite_session.get(Message, "msg") - assert message_record is not None + conversation_record, message_record = _records(sqlite_session) sqlite_session.delete(message_record) sqlite_session.commit() mocker.patch.object(runner, "organize_prompt_messages", return_value=([], None)) @@ -456,15 +454,14 @@ class TestAgentChatAppRunnerRun: runner.run( generate_entity, mocker.MagicMock(), - mocker.MagicMock(id="conv"), - mocker.MagicMock(id="msg"), + conversation_record, + message_record, sqlite_session, ) def test_run_invalid_agent_strategy_raises( self, runner: AgentChatAppRunner, mocker: MockerFixture, sqlite_session: Session ): - app_record = mocker.MagicMock(id="app1", tenant_id="tenant") app_config = mocker.MagicMock(app_id="app1", tenant_id="tenant", prompt_template=mocker.MagicMock()) app_config.agent = mocker.MagicMock(strategy="invalid", provider="p", model="m") @@ -498,8 +495,7 @@ class TestAgentChatAppRunnerRun: llm_instance.model_type_instance.get_model_schema.return_value = model_schema mocker.patch("core.app.apps.agent_chat.app_runner.ModelInstance", return_value=llm_instance) - conversation = mocker.MagicMock(id="conv") - message = mocker.MagicMock(id="msg") + conversation, message = _records(sqlite_session) with pytest.raises(ValueError): runner.run(generate_entity, mocker.MagicMock(), conversation, message, sqlite_session) diff --git a/api/tests/unit_tests/core/app/apps/chat/test_app_generator_and_runner.py b/api/tests/unit_tests/core/app/apps/chat/test_app_generator_and_runner.py index 5f31cf6b49a..d98c39d9215 100644 --- a/api/tests/unit_tests/core/app/apps/chat/test_app_generator_and_runner.py +++ b/api/tests/unit_tests/core/app/apps/chat/test_app_generator_and_runner.py @@ -1,10 +1,12 @@ -from contextlib import contextmanager +from decimal import Decimal from types import SimpleNamespace from unittest.mock import ANY, MagicMock, Mock, patch import pytest -from sqlalchemy.orm import Session, sessionmaker +from sqlalchemy import event +from sqlalchemy.orm import Session +import core.app.apps.chat.app_generator as generator_module from core.app.apps.chat.app_generator import ChatAppGenerator from core.app.apps.chat.app_runner import ChatAppRunner from core.app.apps.exc import GenerateTaskStoppedError @@ -12,7 +14,9 @@ from core.app.entities.app_invoke_entities import InvokeFrom from core.app.entities.queue_entities import QueueAnnotationReplyEvent from core.moderation.base import ModerationError from graphon.model_runtime.errors.invoke import InvokeAuthorizationError -from models.model import App, AppMode, IconType +from models import Account +from models.enums import ConversationFromSource +from models.model import App, AppMode, AppModelConfig, Conversation, IconType, Message, MessageAnnotation class DummyGenerateEntity: @@ -31,19 +35,8 @@ class DummyQueueManager: self.published.append((event, pub_from)) -@contextmanager -def patched_create_session(session_factory: sessionmaker[Session]): - @contextmanager - def create_session(): - with session_factory() as session: - yield session - - with patch("core.app.apps.chat.app_runner.create_session", create_session): - yield - - -def _persist_app(session: Session) -> App: - app = App( +def _app() -> App: + return App( id="app-1", tenant_id="tenant-1", name="Chat app", @@ -54,9 +47,75 @@ def _persist_app(session: Session) -> App: enable_site=False, enable_api=True, ) - session.add(app) + + +def _account() -> Account: + account = Account(name="User", email="user-1@example.com") + account.id = "user-1" + return account + + +def _conversation() -> Conversation: + conversation = Conversation( + id="c1", + app_id="app-1", + app_model_config_id=None, + model_provider=None, + override_model_configs=None, + model_id=None, + mode=AppMode.CHAT, + name="Conversation", + inputs={}, + introduction="", + system_instruction="", + system_instruction_tokens=0, + status="normal", + invoke_from=InvokeFrom.SERVICE_API, + from_source=ConversationFromSource.API, + from_end_user_id=None, + from_account_id="user-1", + ) + return conversation + + +def _message() -> Message: + return Message( + id="m1", + app_id="app-1", + conversation_id="c1", + inputs={}, + query="hi", + message={}, + message_tokens=0, + message_unit_price=Decimal(0), + message_price_unit=Decimal(0), + answer="", + answer_tokens=0, + answer_unit_price=Decimal(0), + answer_price_unit=Decimal(0), + provider_response_latency=0, + total_price=Decimal(0), + currency="USD", + invoke_from=InvokeFrom.SERVICE_API, + from_source=ConversationFromSource.API, + from_end_user_id=None, + from_account_id="user-1", + app_mode=AppMode.CHAT, + ) + + +def _persist_records(session: Session) -> tuple[App, Conversation, Message]: + app = _app() + conversation = _conversation() + message = _message() + session.add_all([app, conversation, message]) session.commit() - return app + return app, conversation, message + + +@pytest.fixture(autouse=True) +def _bind_db_session(sqlite_session: Session, monkeypatch: pytest.MonkeyPatch): + monkeypatch.setattr(generator_module.db, "session", sqlite_session) class TestChatAppGenerator: @@ -65,8 +124,8 @@ class TestChatAppGenerator: with pytest.raises(ValueError): generator.generate( session=unbound_session, - app_model=SimpleNamespace(), - user=SimpleNamespace(), + app_model=_app(), + user=_account(), args={"inputs": {}}, invoke_from=InvokeFrom.SERVICE_API, streaming=False, @@ -77,8 +136,8 @@ class TestChatAppGenerator: with pytest.raises(ValueError): generator.generate( session=unbound_session, - app_model=SimpleNamespace(), - user=SimpleNamespace(), + app_model=_app(), + user=_account(), args={"query": 1, "inputs": {}}, invoke_from=InvokeFrom.SERVICE_API, streaming=False, @@ -86,8 +145,8 @@ class TestChatAppGenerator: def test_generate_debugger_overrides_model_config(self, unbound_session: Session): generator = ChatAppGenerator() - app_model = SimpleNamespace(id="app-1", tenant_id="tenant-1") - user = SimpleNamespace(id="user-1", session_id="session-1") + app_model = _app() + user = _account() args = { "query": "hi", "inputs": {}, @@ -119,12 +178,12 @@ class TestChatAppGenerator: patch( "core.app.apps.chat.app_generator.ChatAppGenerateResponseConverter.convert", return_value={"ok": True} ), - patch.object(ChatAppGenerator, "_get_app_model_config", return_value=SimpleNamespace(to_dict=lambda: {})), + patch.object(ChatAppGenerator, "_get_app_model_config", return_value=AppModelConfig(app_id="app-1")), patch.object(ChatAppGenerator, "_prepare_user_inputs", return_value={}), patch.object( ChatAppGenerator, "_init_generate_records", - return_value=(SimpleNamespace(id="c1", mode="chat"), SimpleNamespace(id="m1")), + return_value=(_conversation(), _message()), ), patch.object(ChatAppGenerator, "_handle_response", return_value={"response": True}), patch("core.app.apps.chat.app_generator.copy_current_request_context", side_effect=lambda f: f), @@ -141,13 +200,14 @@ class TestChatAppGenerator: def test_generate_uses_session_for_annotation_reply(self, unbound_session: Session): generator = ChatAppGenerator() - app_model = SimpleNamespace(id="app-1", tenant_id="tenant-1") - app_model_config = MagicMock(id="config-1", app_id="app-1") + app_model = _app() + app_model_config = AppModelConfig(app_id="app-1") annotation_reply = {"enabled": False} - user = SimpleNamespace(id="user-1", session_id="session-1") + user = _account() with ( patch.object(ChatAppGenerator, "_get_app_model_config", return_value=app_model_config), + patch.object(AppModelConfig, "to_dict", return_value={}) as to_dict, patch( "core.app.apps.chat.app_generator.load_annotation_reply_config", return_value=annotation_reply, @@ -168,40 +228,34 @@ class TestChatAppGenerator: ) load_annotation_reply_config.assert_called_once_with(unbound_session, "app-1") - app_model_config.to_dict.assert_called_once_with(annotation_reply=annotation_reply) + to_dict.assert_called_once_with(annotation_reply=annotation_reply) assert get_app_config.call_args.kwargs["annotation_reply"] is annotation_reply def test_generate_rejects_model_config_override_for_non_debugger(self, unbound_session: Session): generator = ChatAppGenerator() with pytest.raises(ValueError): with ( - patch.object( - ChatAppGenerator, "_get_app_model_config", return_value=SimpleNamespace(to_dict=lambda: {}) - ), + patch.object(ChatAppGenerator, "_get_app_model_config", return_value=AppModelConfig(app_id="app-1")), + patch.object(AppModelConfig, "to_dict", return_value={}), ): generator.generate( session=unbound_session, - app_model=SimpleNamespace(tenant_id="t1", id="a1", mode=AppMode.CHAT.value), - user=SimpleNamespace(id="u1", session_id="s1"), + app_model=_app(), + user=_account(), args={"query": "hi", "inputs": {}, "model_config": {"foo": "bar"}}, invoke_from=InvokeFrom.SERVICE_API, streaming=False, ) - def test_generate_worker_handles_exceptions(self, unbound_session_factory: sessionmaker[Session]): + def test_generate_worker_handles_exceptions(self): generator = ChatAppGenerator() queue_manager = DummyQueueManager() entity = DummyGenerateEntity(task_id="t1", user_id="u1") with ( - patch.object(ChatAppGenerator, "_get_conversation", return_value=SimpleNamespace()), - patch.object(ChatAppGenerator, "_get_message", return_value=SimpleNamespace()), + patch.object(ChatAppGenerator, "_get_conversation", return_value=_conversation()), + patch.object(ChatAppGenerator, "_get_message", return_value=_message()), patch("core.app.apps.chat.app_generator.ChatAppRunner.run", side_effect=InvokeAuthorizationError()), - patch( - "core.app.apps.chat.app_generator.session_factory", - SimpleNamespace(create_session=unbound_session_factory), - ), - patch("core.app.apps.chat.app_generator.db.session.close"), ): generator._generate_worker( flask_app=Mock(app_context=Mock(return_value=Mock(__enter__=Mock(), __exit__=Mock()))), @@ -214,14 +268,9 @@ class TestChatAppGenerator: assert queue_manager.published with ( - patch.object(ChatAppGenerator, "_get_conversation", return_value=SimpleNamespace()), - patch.object(ChatAppGenerator, "_get_message", return_value=SimpleNamespace()), + patch.object(ChatAppGenerator, "_get_conversation", return_value=_conversation()), + patch.object(ChatAppGenerator, "_get_message", return_value=_message()), patch("core.app.apps.chat.app_generator.ChatAppRunner.run", side_effect=GenerateTaskStoppedError()), - patch( - "core.app.apps.chat.app_generator.session_factory", - SimpleNamespace(create_session=unbound_session_factory), - ), - patch("core.app.apps.chat.app_generator.db.session.close"), ): generator._generate_worker( flask_app=Mock(app_context=Mock(return_value=Mock(__enter__=Mock(), __exit__=Mock()))), @@ -233,7 +282,7 @@ class TestChatAppGenerator: class TestChatAppRunner: - def test_run_raises_when_app_missing(self, sqlite_session_factory: sessionmaker[Session], unbound_session: Session): + def test_run_raises_when_app_missing(self, sqlite_session: Session): runner = ChatAppRunner() app_config = SimpleNamespace( app_id="app-1", tenant_id="tenant-1", prompt_template=None, external_data_variables=[] @@ -251,20 +300,17 @@ class TestChatAppRunner: invoke_from=InvokeFrom.SERVICE_API, ) - with patched_create_session(sqlite_session_factory): - with pytest.raises(ValueError): - runner.run( - app_generate_entity, - DummyQueueManager(), - SimpleNamespace(), - SimpleNamespace(id="m1"), - unbound_session, - ) + with pytest.raises(ValueError): + runner.run( + app_generate_entity, + DummyQueueManager(), + _conversation(), + _message(), + sqlite_session, + ) - def test_run_moderation_error_direct_output( - self, sqlite_session: Session, sqlite_session_factory: sessionmaker[Session] - ): - _persist_app(sqlite_session) + def test_run_moderation_error_direct_output(self, sqlite_session: Session): + _, conversation, message = _persist_records(sqlite_session) runner = ChatAppRunner() app_config = SimpleNamespace( app_id="app-1", @@ -289,7 +335,6 @@ class TestChatAppRunner: ) with ( - patched_create_session(sqlite_session_factory), patch.object(ChatAppRunner, "organize_prompt_messages", return_value=([], [])), patch.object(ChatAppRunner, "moderation_for_inputs", side_effect=ModerationError("blocked")), patch.object(ChatAppRunner, "direct_output") as mock_direct, @@ -297,17 +342,15 @@ class TestChatAppRunner: runner.run( app_generate_entity, DummyQueueManager(), - SimpleNamespace(), - SimpleNamespace(id="m1"), + conversation, + message, sqlite_session, ) mock_direct.assert_called_once() - def test_run_annotation_reply_short_circuits( - self, sqlite_session: Session, sqlite_session_factory: sessionmaker[Session] - ): - _persist_app(sqlite_session) + def test_run_annotation_reply_short_circuits(self, sqlite_session: Session): + _, conversation, message = _persist_records(sqlite_session) runner = ChatAppRunner() app_config = SimpleNamespace( app_id="app-1", @@ -331,26 +374,28 @@ class TestChatAppRunner: invoke_from=InvokeFrom.SERVICE_API, ) - annotation = SimpleNamespace(id="ann-1", content="answer") + annotation = MessageAnnotation( + app_id="app-1", + question="hi", + content="answer", + account_id="user-1", + ) with ( - patched_create_session(sqlite_session_factory), patch.object(ChatAppRunner, "organize_prompt_messages", return_value=([], [])), patch.object(ChatAppRunner, "moderation_for_inputs", return_value=(None, {}, "hi")), patch.object(ChatAppRunner, "query_app_annotations_to_reply", return_value=annotation) as annotation_query, patch.object(ChatAppRunner, "direct_output") as mock_direct, ): queue_manager = DummyQueueManager() - runner.run(app_generate_entity, queue_manager, SimpleNamespace(), SimpleNamespace(id="m1"), sqlite_session) + runner.run(app_generate_entity, queue_manager, conversation, message, sqlite_session) assert any(isinstance(item[0], QueueAnnotationReplyEvent) for item in queue_manager.published) assert annotation_query.call_args.kwargs["session"] is sqlite_session mock_direct.assert_called_once() - def test_run_returns_when_hosting_moderation_blocks( - self, sqlite_session: Session, sqlite_session_factory: sessionmaker[Session] - ): - _persist_app(sqlite_session) + def test_run_returns_when_hosting_moderation_blocks(self, sqlite_session: Session): + _, conversation, message = _persist_records(sqlite_session) runner = ChatAppRunner() app_config = SimpleNamespace( app_id="app-1", @@ -374,7 +419,6 @@ class TestChatAppRunner: ) with ( - patched_create_session(sqlite_session_factory), patch.object(ChatAppRunner, "organize_prompt_messages", return_value=([], [])), patch.object(ChatAppRunner, "moderation_for_inputs", return_value=(None, {}, "hi")), patch.object(ChatAppRunner, "query_app_annotations_to_reply", return_value=None), @@ -383,15 +427,13 @@ class TestChatAppRunner: runner.run( app_generate_entity, DummyQueueManager(), - SimpleNamespace(), - SimpleNamespace(id="m1"), + conversation, + message, sqlite_session, ) - def test_run_closes_explicit_session_before_stream_consumption( - self, sqlite_session: Session, sqlite_session_factory: sessionmaker[Session] - ): - _persist_app(sqlite_session) + def test_run_closes_explicit_session_before_stream_consumption(self, sqlite_session: Session): + _, conversation, message = _persist_records(sqlite_session) runner = ChatAppRunner() app_config = SimpleNamespace( app_id="app-1", @@ -418,8 +460,9 @@ class TestChatAppRunner: events = [] queue_manager = DummyQueueManager() model_instance = MagicMock() - original_commit = sqlite_session.commit - original_close = sqlite_session.close + + def record_commit(_session: Session) -> None: + events.append("commit") def invoke_stream(): events.append("first-chunk") @@ -429,26 +472,27 @@ class TestChatAppRunner: events.append("invoke") return invoke_stream() - with ( - patched_create_session(sqlite_session_factory), - patch.object(ChatAppRunner, "organize_prompt_messages", return_value=([], [])), - patch.object(ChatAppRunner, "moderation_for_inputs", return_value=(None, {}, "hi")), - patch.object(ChatAppRunner, "query_app_annotations_to_reply", return_value=None), - patch.object(ChatAppRunner, "check_hosting_moderation", return_value=False), - patch.object(ChatAppRunner, "recalc_llm_max_tokens"), - patch.object( - ChatAppRunner, - "_handle_invoke_result", - side_effect=lambda invoke_result, **kwargs: list(invoke_result), - ) as mock_handle, - patch("core.app.apps.chat.app_runner.ModelInstance", return_value=model_instance), - patch.object(sqlite_session, "commit", side_effect=lambda: (events.append("commit"), original_commit())[1]), - patch.object(sqlite_session, "close", side_effect=lambda: (events.append("close"), original_close())[1]), - ): - model_instance.invoke_llm.side_effect = invoke_llm - runner.run(app_generate_entity, queue_manager, SimpleNamespace(), SimpleNamespace(id="m1"), sqlite_session) + event.listen(sqlite_session, "after_commit", record_commit) + try: + with ( + patch.object(ChatAppRunner, "organize_prompt_messages", return_value=([], [])), + patch.object(ChatAppRunner, "moderation_for_inputs", return_value=(None, {}, "hi")), + patch.object(ChatAppRunner, "query_app_annotations_to_reply", return_value=None), + patch.object(ChatAppRunner, "check_hosting_moderation", return_value=False), + patch.object(ChatAppRunner, "recalc_llm_max_tokens"), + patch.object( + ChatAppRunner, + "_handle_invoke_result", + side_effect=lambda invoke_result, **kwargs: list(invoke_result), + ) as mock_handle, + patch("core.app.apps.chat.app_runner.ModelInstance", return_value=model_instance), + ): + model_instance.invoke_llm.side_effect = invoke_llm + runner.run(app_generate_entity, queue_manager, conversation, message, sqlite_session) + finally: + event.remove(sqlite_session, "after_commit", record_commit) - assert events == ["commit", "close", "commit", "close", "invoke", "first-chunk"] + assert events == ["commit", "commit", "invoke", "first-chunk"] mock_handle.assert_called_once_with( invoke_result=ANY, queue_manager=queue_manager, diff --git a/api/tests/unit_tests/core/rag/indexing/test_index_processor_base.py b/api/tests/unit_tests/core/rag/indexing/test_index_processor_base.py index 6dc66e5ff59..6dc50ba8a08 100644 --- a/api/tests/unit_tests/core/rag/indexing/test_index_processor_base.py +++ b/api/tests/unit_tests/core/rag/indexing/test_index_processor_base.py @@ -206,6 +206,27 @@ class TestBaseIndexProcessor: assert files == [] + def test_get_content_files_skips_invalid_remote_image_references( + self, processor: _ForwardingBaseIndexProcessor, unbound_session: Session + ) -> None: + document = Document(page_content="ignored", metadata={"document_id": "doc-1", "dataset_id": "ds-1"}) + images = [ + "document_images/image.png", + "//example.com/image.png", + "data:image/png;base64,AAAA", + "ftp://example.com/image.png", + "http://[invalid", + ] + + with ( + patch.object(processor, "_extract_markdown_images", return_value=images), + patch.object(processor, "_download_image") as mock_image_download, + ): + files = processor._get_content_files(document, current_user=Mock(), session=unbound_session) + + assert files == [] + mock_image_download.assert_not_called() + def test_get_content_files_ignores_missing_upload_records( self, processor: _ForwardingBaseIndexProcessor, sqlite_session: Session ) -> None: diff --git a/api/tests/unit_tests/core/workflow/nodes/agent/test_message_transformer.py b/api/tests/unit_tests/core/workflow/nodes/agent/test_message_transformer.py index 5e7c553b5f6..26204b040ca 100644 --- a/api/tests/unit_tests/core/workflow/nodes/agent/test_message_transformer.py +++ b/api/tests/unit_tests/core/workflow/nodes/agent/test_message_transformer.py @@ -11,7 +11,7 @@ from core.workflow.nodes.agent.exceptions import ToolFileNotFoundError from core.workflow.nodes.agent.message_transformer import AgentMessageTransformer from graphon.enums import BuiltinNodeTypes from graphon.file import File, FileTransferMethod, FileType -from graphon.node_events import StreamCompletedEvent +from graphon.node_events import StreamChunkEvent, StreamCompletedEvent from graphon.variables.segments import ArrayFileSegment @@ -194,3 +194,80 @@ def test_transform_keeps_plain_link_as_text() -> None: assert text == "Link: https://dify.ai\n" assert files.value == [] + + +def _text(content: str) -> ToolInvokeMessage: + return ToolInvokeMessage( + type=ToolInvokeMessage.MessageType.TEXT, + message=ToolInvokeMessage.TextMessage(text=content), + ) + + +def _log(*, message_id: str = "log-1", label: str = "ROUND 1") -> ToolInvokeMessage: + return ToolInvokeMessage( + type=ToolInvokeMessage.MessageType.LOG, + message=ToolInvokeMessage.LogMessage( + id=message_id, + label=label, + status=ToolInvokeMessage.LogMessage.LogStatus.START, + data={}, + ), + ) + + +def test_transform_closes_think_tag_before_nested_open() -> None: + text, _ = _run_transform( + [ + _text("first pass\n"), + _text("second pass"), + ] + ) + + assert text == "first pass\nsecond pass" + + +def test_transform_closes_think_tag_when_tool_log_interrupts() -> None: + text, _ = _run_transform( + [ + _text("need to search"), + _log(label="CALL search"), + _text("search result summary"), + ] + ) + + assert text == "need to searchsearch result summary" + + +def test_transform_streams_close_tag_before_post_tool_text() -> None: + events = list( + AgentMessageTransformer().transform( + messages=_message_stream( + [ + _text("need to search"), + _log(label="CALL search"), + _text("visible reply"), + ] + ), + tool_info={}, + parameters_for_log={}, + user_id="user-id", + tenant_id="tenant-id", + conversation_id=None, + node_type=BuiltinNodeTypes.AGENT, + node_id="node-id", + node_execution_id="execution-id", + ) + ) + text_chunks = [ + event.chunk for event in events if isinstance(event, StreamChunkEvent) and event.selector == ["node-id", "text"] + ] + + assert text_chunks == ["need to search", "", "visible reply", ""] + + +def test_close_unclosed_think_tags_helper() -> None: + from core.workflow.nodes.agent.think_tags import close_unclosed_think_tags, has_unclosed_think + + assert has_unclosed_think("open") + assert not has_unclosed_think("opendone") + assert close_unclosed_think_tags("a\nb") == "a\nb" diff --git a/api/tests/unit_tests/events/event_handlers/test_sync_plugin_trigger_when_app_created.py b/api/tests/unit_tests/events/event_handlers/test_sync_plugin_trigger_when_app_created.py new file mode 100644 index 00000000000..a9d631cf7dd --- /dev/null +++ b/api/tests/unit_tests/events/event_handlers/test_sync_plugin_trigger_when_app_created.py @@ -0,0 +1,31 @@ +from types import SimpleNamespace +from typing import cast +from unittest.mock import patch + +from events.event_handlers.sync_plugin_trigger_when_app_created import handle +from models.model import AppMode +from models.workflow import Workflow + + +def test_syncs_plugin_trigger_relationships_from_published_workflow() -> None: + app = SimpleNamespace(mode=AppMode.WORKFLOW.value) + published_workflow = cast(Workflow, object()) + + with patch( + "events.event_handlers.sync_plugin_trigger_when_app_created.TriggerService.sync_plugin_trigger_relationships" + ) as sync_relationships: + handle(app, published_workflow=published_workflow) + + sync_relationships.assert_called_once_with(app, published_workflow) + + +def test_keeps_draft_workflow_relationship_sync() -> None: + app = SimpleNamespace(mode=AppMode.WORKFLOW.value) + draft_workflow = cast(Workflow, object()) + + with patch( + "events.event_handlers.sync_plugin_trigger_when_app_created.TriggerService.sync_plugin_trigger_relationships" + ) as sync_relationships: + handle(app, synced_draft_workflow=draft_workflow) + + sync_relationships.assert_called_once_with(app, draft_workflow) diff --git a/api/tests/unit_tests/extensions/test_ext_application_services.py b/api/tests/unit_tests/extensions/test_ext_application_services.py index ade8e369fa1..2be0f0c358e 100644 --- a/api/tests/unit_tests/extensions/test_ext_application_services.py +++ b/api/tests/unit_tests/extensions/test_ext_application_services.py @@ -29,6 +29,10 @@ from repositories.account_oauth_repository import ( ) from repositories.account_repository import SQLAlchemyAccountRepository from repositories.app_site_command_repository import AppSiteCommandRepository +from repositories.app_statistic_query_repository import AppStatisticQueryRepository +from repositories.app_tracing_config_repository import SQLAlchemyAppTracingConfigRepository +from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemyWorkflowRunRepository +from repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepository from repositories.workflow_run_archive_repository import WorkflowRunArchiveBundleQueryRepository from services import account_forgot_password_service, recommended_app_catalog_gateway from services.account_adapters import ( @@ -55,6 +59,8 @@ from services.account_oauth_adapters import ( RedisOAuthAccountClaimLock, ) from services.app_site_service import AppSiteService +from services.app_tracing_config_gateway import OpsTraceManagerGateway +from services.app_tracing_config_service import AppTracingConfigService from services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthService from services.billing_portal_service import BillingPortalService from services.billing_service import BillingService @@ -68,6 +74,8 @@ from services.retention.workflow_run.archive_download_task_cache import Workflow from services.retention.workflow_run.archive_log_service import WorkflowRunArchiveService from services.tag_application_service import TagApplicationService from services.webapp_access_query_service import WebAppAccessUnavailableError +from services.workflow_app_log_query_service import WorkflowAppLogQueryService +from services.workflow_run_service import WorkflowRunService from services.workflow_statistic_query_service import WorkflowStatisticQueryService from tests.unit_tests.config_override import apply_config_overrides @@ -261,6 +269,67 @@ def test_build_application_services_wires_app_site_boundary( assert services.app_sites._sites._session_factory is sqlite_session_factory +def test_build_application_services_wires_app_tracing_config_boundary( + sqlite_session_factory: sessionmaker[Session], +) -> None: + services = ext_application_services.build_application_services( + database_client=sqlite_session_factory, + deployment_edition=DeploymentEdition.COMMUNITY, + initialization_password="", + redis=MagicMock(spec=RedisClientWrapper), + ) + + assert isinstance(services.app_tracing_configs, AppTracingConfigService) + assert isinstance(services.app_tracing_configs._configs, SQLAlchemyAppTracingConfigRepository) + assert services.app_tracing_configs._configs._session_factory is sqlite_session_factory + assert isinstance(services.app_tracing_configs._provider, OpsTraceManagerGateway) + + +def test_build_application_services_wires_workflow_app_log_boundary( + sqlite_session_factory: sessionmaker[Session], +) -> None: + services = ext_application_services.build_application_services( + database_client=sqlite_session_factory, + deployment_edition=DeploymentEdition.COMMUNITY, + initialization_password="", + redis=MagicMock(spec=RedisClientWrapper), + ) + + assert isinstance(services.workflow_app_logs, WorkflowAppLogQueryService) + assert isinstance(services.workflow_app_logs._logs, WorkflowAppLogQueryRepository) + assert services.workflow_app_logs._logs._session_factory is sqlite_session_factory + + +def test_build_application_services_wires_app_statistic_boundary( + sqlite_session_factory: sessionmaker[Session], +) -> None: + services = ext_application_services.build_application_services( + database_client=sqlite_session_factory, + deployment_edition=DeploymentEdition.COMMUNITY, + initialization_password="", + redis=MagicMock(spec=RedisClientWrapper), + ) + + assert isinstance(services.app_statistics, AppStatisticQueryRepository) + assert services.app_statistics._session_factory is sqlite_session_factory + + +def test_build_application_services_wires_workflow_run_service( + sqlite_session_factory: sessionmaker[Session], +) -> None: + services = ext_application_services.build_application_services( + database_client=sqlite_session_factory, + deployment_edition=DeploymentEdition.COMMUNITY, + initialization_password="", + redis=MagicMock(spec=RedisClientWrapper), + ) + + workflow_runs = services.workflow_runs + assert isinstance(workflow_runs, WorkflowRunService) + assert isinstance(workflow_runs._workflow_runs, DifyAPISQLAlchemyWorkflowRunRepository) + assert workflow_runs._workflow_runs._session_maker is sqlite_session_factory + + def test_build_application_services_wires_billing_service( sqlite_session: Session, sqlite_session_factory: sessionmaker[Session], @@ -434,7 +503,6 @@ def test_build_application_services_wires_account_profile_repository( assert services.accounts.deletion._accounts is accounts assert services.accounts.authentication._accounts is accounts assert services.accounts.authentication._workspaces is services.workspace_queries._workspaces - assert services.notifications._accounts is accounts assert services.step_by_step_tour._accounts is accounts assert services.accounts.deletion._memberships is services.workspace_queries._workspaces integrations = services.accounts.integrations._integrations @@ -722,14 +790,12 @@ def test_build_application_services_wires_dynamic_recommended_catalog( ) with patch.object(recommended_app_catalog_gateway.Path, "read_text", return_value=builtin_payload): result = services.recommended_app_queries.list_recommended( - requested_language="en-US", - interface_language=None, + language="en-US", ) assert result.recommended_apps apply_config_overrides(monkeypatch, HOSTED_FETCH_APP_TEMPLATES_MODE="invalid") with pytest.raises(ValueError, match="invalid fetch recommended apps mode: invalid"): services.recommended_app_queries.list_recommended( - requested_language="en-US", - interface_language=None, + language="en-US", ) diff --git a/api/tests/unit_tests/file_grant_test_utils.py b/api/tests/unit_tests/file_grant_test_utils.py new file mode 100644 index 00000000000..0fb7eae9408 --- /dev/null +++ b/api/tests/unit_tests/file_grant_test_utils.py @@ -0,0 +1,31 @@ +import time +from collections.abc import Sequence + +from configs import dify_config +from services.entities.file_grant_entities import FileGrantContext, FileGrantScope +from services.file_grant_gateways import FileGrantTokenGateway + + +def token_gateway() -> FileGrantTokenGateway: + return FileGrantTokenGateway( + secret_key=dify_config.SECRET_KEY, + external_files_url=dify_config.FILES_URL, + internal_files_url=dify_config.INTERNAL_FILES_URL or dify_config.FILES_URL, + content_token_ttl_seconds=dify_config.FILES_ACCESS_TIMEOUT, + now=lambda: int(time.time()), + ) + + +def issue_file_grant( + *, + end_user_id: str, + tenant_id: str, + app_id: str, + scopes: Sequence[FileGrantScope], + ttl_seconds: int, +) -> tuple[str, int]: + return token_gateway().issue_grant( + context=FileGrantContext(tenant_id=tenant_id, app_id=app_id, end_user_id=end_user_id), + scopes=scopes, + ttl_seconds=ttl_seconds, + ) diff --git a/api/tests/unit_tests/libs/test_datetime_utils.py b/api/tests/unit_tests/libs/test_datetime_utils.py index e2dce54d1e0..a4229d20777 100644 --- a/api/tests/unit_tests/libs/test_datetime_utils.py +++ b/api/tests/unit_tests/libs/test_datetime_utils.py @@ -4,7 +4,19 @@ from unittest.mock import patch import pytest import pytz -from libs.datetime_utils import naive_utc_now, parse_time_range, to_utc_timestamp +from libs.datetime_utils import naive_utc_now, parse_time_range, to_utc_timestamp, utc_now + + +def test_utc_now(monkeypatch: pytest.MonkeyPatch): + expected = datetime.datetime(2026, 8, 26, 12, tzinfo=datetime.UTC) + + def _now_func(tz: datetime.timezone | None) -> datetime.datetime: + return expected.astimezone(tz) + + monkeypatch.setattr("libs.datetime_utils._now_func", _now_func) + + assert utc_now() == expected + assert utc_now().tzinfo is datetime.UTC def test_naive_utc_now(monkeypatch: pytest.MonkeyPatch): diff --git a/api/tests/unit_tests/models/test_annotation_hit_history_session_accessors.py b/api/tests/unit_tests/models/test_annotation_hit_history_session_accessors.py new file mode 100644 index 00000000000..d4bb5c624e5 --- /dev/null +++ b/api/tests/unit_tests/models/test_annotation_hit_history_session_accessors.py @@ -0,0 +1,93 @@ +"""Regression coverage for the ``@property``→session-parameter refactor on +``AppAnnotationHitHistory`` accessors. + +Covers two of `api/models/model.py`'s legacy `@property` accessors that reached for the global +``db.session`` internally and have been converted to plain methods taking an explicit +``session: Session`` (per the pattern established in #40370/#40797/#41394/#41830, tracked in +#40372): + +- ``AppAnnotationHitHistory.account`` (joins through ``MessageAnnotation.account_id``) +- ``AppAnnotationHitHistory.annotation_create_account`` (looks up ``self.account_id`` directly) + +Each accessor is exercised against the real ``sqlite_session`` fixture (a genuine SQLAlchemy +``Session`` bound to a pristine full-schema SQLite database) so the assertions cover actual +query behaviour rather than a mock's recorded call. +""" + +from uuid import uuid4 + +from sqlalchemy.orm import Session + +from models.account import Account +from models.model import AppAnnotationHitHistory, MessageAnnotation + + +def _persist_account(session: Session) -> Account: + account = Account(name="Test Account", email="test@example.com") + session.add(account) + session.flush() + return account + + +def _persist_annotation(session: Session, *, app_id: str, account_id: str) -> MessageAnnotation: + annotation = MessageAnnotation( + app_id=app_id, + conversation_id=None, + message_id=None, + question="What is AI?", + content="AI stands for Artificial Intelligence.", + account_id=account_id, + ) + session.add(annotation) + session.flush() + return annotation + + +def _hit_history(*, app_id: str, annotation_id: str, account_id: str) -> AppAnnotationHitHistory: + return AppAnnotationHitHistory( + app_id=app_id, + annotation_id=annotation_id, + source="api", + question="What is AI?", + account_id=account_id, + score=0.95, + message_id=str(uuid4()), + annotation_question="What is AI?", + annotation_content="AI stands for Artificial Intelligence.", + ) + + +class TestAppAnnotationHitHistoryAccount: + def test_returns_the_annotation_creator(self, sqlite_session: Session) -> None: + app_id = str(uuid4()) + creator = _persist_account(sqlite_session) + annotation = _persist_annotation(sqlite_session, app_id=app_id, account_id=creator.id) + history = _hit_history(app_id=app_id, annotation_id=annotation.id, account_id=str(uuid4())) + + result = history.account(session=sqlite_session) + + assert result is not None + assert result.id == creator.id + + def test_returns_none_when_annotation_missing(self, sqlite_session: Session) -> None: + history = _hit_history(app_id=str(uuid4()), annotation_id=str(uuid4()), account_id=str(uuid4())) + + assert history.account(session=sqlite_session) is None + + +class TestAppAnnotationHitHistoryAnnotationCreateAccount: + def test_returns_the_hit_requester(self, sqlite_session: Session) -> None: + app_id = str(uuid4()) + requester = _persist_account(sqlite_session) + annotation = _persist_annotation(sqlite_session, app_id=app_id, account_id=str(uuid4())) + history = _hit_history(app_id=app_id, annotation_id=annotation.id, account_id=requester.id) + + result = history.annotation_create_account(session=sqlite_session) + + assert result is not None + assert result.id == requester.id + + def test_returns_none_when_account_missing(self, sqlite_session: Session) -> None: + history = _hit_history(app_id=str(uuid4()), annotation_id=str(uuid4()), account_id=str(uuid4())) + + assert history.annotation_create_account(session=sqlite_session) is None diff --git a/api/tests/unit_tests/models/test_end_user_type.py b/api/tests/unit_tests/models/test_end_user_type.py index 222945814b4..af6407c7b7c 100644 --- a/api/tests/unit_tests/models/test_end_user_type.py +++ b/api/tests/unit_tests/models/test_end_user_type.py @@ -15,6 +15,7 @@ API_ROOT = Path(__file__).resolve().parents[3] def test_end_user_type_covers_persisted_creation_values(): assert {member.value for member in EndUserType} == { + "app-deploy", "browser", "mcp", "openapi", diff --git a/api/tests/unit_tests/models/test_tool_provider_session_accessors.py b/api/tests/unit_tests/models/test_tool_provider_session_accessors.py new file mode 100644 index 00000000000..02506476d05 --- /dev/null +++ b/api/tests/unit_tests/models/test_tool_provider_session_accessors.py @@ -0,0 +1,152 @@ +"""Regression coverage for the ``@property``→session-parameter refactor on tool provider accessors. + +Covers four of `api/models/tools.py`'s legacy `@property` accessors that reached for the global +``db.session`` internally and have been converted to plain methods taking an explicit +``session: Session`` (per the pattern established in #40370/#40797/#41394, tracked in #40372): + +- ``ApiToolProvider.tenant`` +- ``WorkflowToolProvider.user`` +- ``WorkflowToolProvider.tenant`` +- ``WorkflowToolProvider.app`` + +Each accessor is exercised against the real ``sqlite_session`` fixture (a genuine SQLAlchemy +``Session`` bound to a pristine full-schema SQLite database) so the assertions cover actual +query behaviour rather than a mock's recorded call. +""" + +from uuid import uuid4 + +from sqlalchemy.orm import Session + +from core.tools.entities.tool_entities import ApiProviderSchemaType +from models.account import Account, Tenant +from models.model import App, AppMode +from models.tools import ApiToolProvider, WorkflowToolProvider + + +def _persist_account(session: Session) -> Account: + account = Account(name="Test Account", email="test@example.com") + session.add(account) + session.flush() + return account + + +def _persist_tenant(session: Session) -> Tenant: + tenant = Tenant(name="Test Tenant") + session.add(tenant) + session.flush() + return tenant + + +def _persist_app(session: Session, *, tenant_id: str) -> App: + app = App( + tenant_id=tenant_id, + name="Test App", + mode=AppMode.WORKFLOW, + enable_site=True, + enable_api=True, + created_by=str(uuid4()), + ) + session.add(app) + session.flush() + return app + + +def _api_tool_provider(*, tenant_id: str) -> ApiToolProvider: + return ApiToolProvider( + tenant_id=tenant_id, + user_id=str(uuid4()), + name="Test API Provider", + icon="{}", + schema="{}", + schema_type_str=ApiProviderSchemaType.OPENAPI, + description="", + tools_str="[]", + credentials_str="{}", + ) + + +def _workflow_tool_provider(*, tenant_id: str, app_id: str, user_id: str) -> WorkflowToolProvider: + return WorkflowToolProvider( + name="Test Workflow Provider", + label="Test Workflow Provider", + icon="{}", + app_id=app_id, + version="1", + user_id=user_id, + tenant_id=tenant_id, + description="", + ) + + +class TestApiToolProviderTenant: + def test_returns_persisted_tenant(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + provider = _api_tool_provider(tenant_id=tenant.id) + + result = provider.tenant(session=sqlite_session) + + assert result is not None + assert result.id == tenant.id + + def test_returns_none_when_tenant_missing(self, sqlite_session: Session) -> None: + provider = _api_tool_provider(tenant_id=str(uuid4())) + + assert provider.tenant(session=sqlite_session) is None + + +class TestWorkflowToolProviderUser: + def test_returns_persisted_user(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + app = _persist_app(sqlite_session, tenant_id=tenant.id) + account = _persist_account(sqlite_session) + provider = _workflow_tool_provider(tenant_id=tenant.id, app_id=app.id, user_id=account.id) + + result = provider.user(session=sqlite_session) + + assert result is not None + assert result.id == account.id + + def test_returns_none_when_user_missing(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + app = _persist_app(sqlite_session, tenant_id=tenant.id) + provider = _workflow_tool_provider(tenant_id=tenant.id, app_id=app.id, user_id=str(uuid4())) + + assert provider.user(session=sqlite_session) is None + + +class TestWorkflowToolProviderTenant: + def test_returns_persisted_tenant(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + app = _persist_app(sqlite_session, tenant_id=tenant.id) + provider = _workflow_tool_provider(tenant_id=tenant.id, app_id=app.id, user_id=str(uuid4())) + + result = provider.tenant(session=sqlite_session) + + assert result is not None + assert result.id == tenant.id + + def test_returns_none_when_tenant_missing(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + app = _persist_app(sqlite_session, tenant_id=tenant.id) + provider = _workflow_tool_provider(tenant_id=str(uuid4()), app_id=app.id, user_id=str(uuid4())) + + assert provider.tenant(session=sqlite_session) is None + + +class TestWorkflowToolProviderApp: + def test_returns_persisted_app(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + app = _persist_app(sqlite_session, tenant_id=tenant.id) + provider = _workflow_tool_provider(tenant_id=tenant.id, app_id=app.id, user_id=str(uuid4())) + + result = provider.app(session=sqlite_session) + + assert result is not None + assert result.id == app.id + + def test_returns_none_when_app_missing(self, sqlite_session: Session) -> None: + tenant = _persist_tenant(sqlite_session) + provider = _workflow_tool_provider(tenant_id=tenant.id, app_id=str(uuid4()), user_id=str(uuid4())) + + assert provider.app(session=sqlite_session) is None diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml index c36941f0469..42abc4e6e29 100644 --- a/api/tests/unit_tests/pyrefly.toml +++ b/api/tests/unit_tests/pyrefly.toml @@ -863,7 +863,6 @@ project-excludes = [ "services/test_webhook_service.py", "services/test_webhook_service_additional.py", "services/test_website_service.py", - "services/test_workflow_app_service_metadata.py", "services/test_workflow_collaboration_service.py", "services/test_workflow_comment_service.py", "services/test_workflow_generator_service.py", diff --git a/api/tests/unit_tests/repositories/test_app_statistic_query_repository.py b/api/tests/unit_tests/repositories/test_app_statistic_query_repository.py new file mode 100644 index 00000000000..b8fb51e3dad --- /dev/null +++ b/api/tests/unit_tests/repositories/test_app_statistic_query_repository.py @@ -0,0 +1,155 @@ +from datetime import UTC, datetime +from decimal import Decimal +from typing import cast, override + +import pytest +from sqlalchemy.engine import RowMapping +from sqlalchemy.orm import Session, sessionmaker + +from core.app.entities.app_invoke_entities import InvokeFrom +from repositories import app_statistic_query_repository as repository_module +from repositories.app_statistic_query_repository import AppStatisticQueryRepository +from services.app_statistic_query import ( + AverageResponseTimeStatisticRecord, + AverageSessionInteractionStatisticRecord, + DailyConversationStatisticRecord, + DailyMessageStatisticRecord, + DailyTerminalStatisticRecord, + DailyTokenCostStatisticRecord, + TokensPerSecondStatisticRecord, + UserSatisfactionRateStatisticRecord, +) + + +class _RecordingRepository(AppStatisticQueryRepository): + def __init__(self) -> None: + super().__init__(session_factory=cast(sessionmaker[Session], object())) + self.rows: tuple[RowMapping, ...] = () + self.calls: list[tuple[str, dict[str, object]]] = [] + + @override + def _execute(self, sql_query: str, parameters: dict[str, object]) -> tuple[RowMapping, ...]: + self.calls.append((sql_query, parameters.copy())) + return self.rows + + +def _row(**values: object) -> RowMapping: + return cast(RowMapping, values) + + +def test_app_statistic_repository_maps_results_and_preserves_query_scope( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(repository_module, "convert_datetime_to_date", lambda field: field) + repository = _RecordingRepository() + start_date = datetime(2024, 1, 1, tzinfo=UTC) + end_date = datetime(2024, 1, 2, tzinfo=UTC) + + repository.rows = (_row(date="2024-01-01", message_count=2),) + assert repository.get_daily_messages( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (DailyMessageStatisticRecord(date="2024-01-01", message_count=2),) + + repository.rows = (_row(date="2024-01-01", conversation_count=3),) + assert repository.get_daily_conversations( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (DailyConversationStatisticRecord(date="2024-01-01", conversation_count=3),) + + repository.rows = (_row(date="2024-01-01", terminal_count=4),) + assert repository.get_daily_terminals( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (DailyTerminalStatisticRecord(date="2024-01-01", terminal_count=4),) + + repository.rows = (_row(date="2024-01-01", token_count=Decimal(5), total_price=Decimal("0.25")),) + assert repository.get_daily_token_costs( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == ( + DailyTokenCostStatisticRecord( + date="2024-01-01", + token_count=5, + total_price=Decimal("0.25"), + currency="USD", + ), + ) + + repository.rows = (_row(date="2024-01-01", interactions=Decimal("2.345")),) + assert repository.get_average_session_interactions( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (AverageSessionInteractionStatisticRecord(date="2024-01-01", interactions=2.34),) + + repository.rows = (_row(date="2024-01-01", message_count=10, feedback_count=1),) + assert repository.get_user_satisfaction_rates( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (UserSatisfactionRateStatisticRecord(date="2024-01-01", rate=100.0),) + + repository.rows = (_row(date="2024-01-01", latency=1.234),) + assert repository.get_average_response_times( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (AverageResponseTimeStatisticRecord(date="2024-01-01", latency=1234.0),) + + repository.rows = (_row(date="2024-01-01", tokens_per_second=15.55555),) + assert repository.get_tokens_per_second( + app_id="app-1", + start_date=start_date, + end_date=end_date, + timezone="Asia/Shanghai", + ) == (TokensPerSecondStatisticRecord(date="2024-01-01", tps=15.5556),) + + assert len(repository.calls) == 8 + for sql_query, parameters in repository.calls: + assert "created_at >= :start_date" in sql_query + assert "created_at < :end_date" in sql_query + assert parameters == { + "tz": "Asia/Shanghai", + "app_id": "app-1", + "excluded_invoke_from": InvokeFrom.DEBUGGER, + "start_date": start_date, + "end_date": end_date, + } + + +def test_daily_messages_omit_time_range_when_not_provided( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(repository_module, "convert_datetime_to_date", lambda field: field) + repository = _RecordingRepository() + + assert ( + repository.get_daily_messages( + app_id="app-1", + start_date=None, + end_date=None, + timezone="Asia/Shanghai", + ) + == () + ) + + sql_query, parameters = repository.calls[0] + assert ":start_date" not in sql_query + assert ":end_date" not in sql_query + assert parameters == { + "tz": "Asia/Shanghai", + "app_id": "app-1", + "excluded_invoke_from": InvokeFrom.DEBUGGER, + } diff --git a/api/tests/unit_tests/repositories/test_app_tracing_config_repository.py b/api/tests/unit_tests/repositories/test_app_tracing_config_repository.py new file mode 100644 index 00000000000..f58efbbbfc2 --- /dev/null +++ b/api/tests/unit_tests/repositories/test_app_tracing_config_repository.py @@ -0,0 +1,145 @@ +from collections.abc import Callable + +import pytest +from sqlalchemy import select, text +from sqlalchemy.orm import Session, sessionmaker + +from models.model import App, AppMode, TraceAppConfig +from repositories.app_tracing_config_repository import SQLAlchemyAppTracingConfigRepository +from services.app_tracing_config_service import AppTracingConfigAppNotFoundError, AppTracingConfigRecord + +_APP_ID = "11111111-1111-1111-1111-111111111111" +_WORKSPACE_ID = "22222222-2222-2222-2222-222222222222" +_OTHER_WORKSPACE_ID = "33333333-3333-3333-3333-333333333333" +_PROVIDER = "langfuse" + + +def _persist_app(session: Session) -> None: + session.add( + App( + id=_APP_ID, + tenant_id=_WORKSPACE_ID, + name="Tracing App", + description="", + mode=AppMode.CHAT, + icon_type=None, + icon=None, + icon_background=None, + enable_site=True, + enable_api=True, + ) + ) + session.commit() + + +def _repository(session_factory: sessionmaker[Session]) -> SQLAlchemyAppTracingConfigRepository: + return SQLAlchemyAppTracingConfigRepository(session_factory=session_factory) + + +def test_config_lifecycle_is_persisted_by_owned_transactions( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + _persist_app(sqlite_session) + repository = _repository(sqlite_session_factory) + + assert repository.get(workspace_id=_WORKSPACE_ID, app_id=_APP_ID, tracing_provider=_PROVIDER) is None + assert repository.create( + workspace_id=_WORKSPACE_ID, + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={"public_key": "original"}, + ) + assert not repository.create( + workspace_id=_WORKSPACE_ID, + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={"public_key": "duplicate"}, + ) + + record = repository.get(workspace_id=_WORKSPACE_ID, app_id=_APP_ID, tracing_provider=_PROVIDER) + assert isinstance(record, AppTracingConfigRecord) + assert record.app_id == _APP_ID + assert record.tracing_provider == _PROVIDER + assert record.tracing_config == {"public_key": "original"} + + assert repository.update( + workspace_id=_WORKSPACE_ID, + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={"public_key": "updated"}, + ) + with sqlite_session_factory() as session: + config = session.scalar(select(TraceAppConfig).where(TraceAppConfig.app_id == _APP_ID)) + assert config is not None + assert config.tracing_config == {"public_key": "updated"} + + assert repository.delete(workspace_id=_WORKSPACE_ID, app_id=_APP_ID, tracing_provider=_PROVIDER) + assert not repository.update( + workspace_id=_WORKSPACE_ID, + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={"public_key": "missing"}, + ) + assert not repository.delete(workspace_id=_WORKSPACE_ID, app_id=_APP_ID, tracing_provider=_PROVIDER) + with sqlite_session_factory() as session: + assert session.scalar(select(TraceAppConfig).where(TraceAppConfig.app_id == _APP_ID)) is None + + +@pytest.mark.parametrize("app_state", ["other-workspace", "non-normal"]) +def test_all_operations_reject_apps_outside_the_active_workspace_scope( + app_state: str, + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + _persist_app(sqlite_session) + workspace_id = _OTHER_WORKSPACE_ID + if app_state == "non-normal": + sqlite_session.execute(text("UPDATE apps SET status = 'disabled' WHERE id = :app_id"), {"app_id": _APP_ID}) + sqlite_session.commit() + workspace_id = _WORKSPACE_ID + + repository = _repository(sqlite_session_factory) + operations: tuple[Callable[[], object], ...] = ( + lambda: repository.get(workspace_id=workspace_id, app_id=_APP_ID, tracing_provider=_PROVIDER), + lambda: repository.create( + workspace_id=workspace_id, + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={}, + ), + lambda: repository.update( + workspace_id=workspace_id, + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={}, + ), + lambda: repository.delete(workspace_id=workspace_id, app_id=_APP_ID, tracing_provider=_PROVIDER), + ) + + for operation in operations: + with pytest.raises(AppTracingConfigAppNotFoundError): + operation() + + +def test_record_mapping_does_not_expose_the_orm_model_or_its_config_dict( + sqlite_session: Session, +) -> None: + _persist_app(sqlite_session) + config = TraceAppConfig( + app_id=_APP_ID, + tracing_provider=_PROVIDER, + tracing_config={"public_key": "original"}, + ) + sqlite_session.add(config) + sqlite_session.commit() + + record = SQLAlchemyAppTracingConfigRepository._to_record(config) + + assert isinstance(record, AppTracingConfigRecord) + assert record is not config + assert record.tracing_config == config.tracing_config + assert record.tracing_config is not config.tracing_config + assert record.tracing_config is not None + record.tracing_config["public_key"] = "changed" + assert config.tracing_config == {"public_key": "original"} diff --git a/api/tests/unit_tests/repositories/test_file_grant_repository.py b/api/tests/unit_tests/repositories/test_file_grant_repository.py new file mode 100644 index 00000000000..b27353a622b --- /dev/null +++ b/api/tests/unit_tests/repositories/test_file_grant_repository.py @@ -0,0 +1,75 @@ +from sqlalchemy import Engine, event +from sqlalchemy.orm import Session, sessionmaker + +from extensions.storage.storage_type import StorageType +from libs.datetime_utils import naive_utc_now +from models.enums import CreatorUserRole +from models.model import UploadFile +from models.tools import ToolFile +from repositories.file_grant_repository import FileGrantRepository +from services.entities.file_grant_entities import FileGrantContext, FileKind, FileRef + + +def test_resolve_owned_files_uses_one_query_per_file_kind( + sqlite_engine: Engine, + sqlite_session_factory: sessionmaker[Session], +) -> None: + end_user_id = "11111111-1111-4111-8111-111111111111" + tenant_id = "22222222-2222-4222-8222-222222222222" + with sqlite_session_factory.begin() as session: + upload = UploadFile( + tenant_id=tenant_id, + storage_type=StorageType.OPENDAL, + key="upload_files/report.pdf", + name="report.pdf", + size=10, + extension="pdf", + mime_type="application/pdf", + created_by=end_user_id, + created_by_role=CreatorUserRole.END_USER, + created_at=naive_utc_now(), + used=False, + ) + tool_file = ToolFile( + user_id=end_user_id, + tenant_id=tenant_id, + conversation_id=None, + file_key="tools/chart.png", + mimetype="image/png", + name="chart.png", + size=20, + ) + session.add_all([upload, tool_file]) + session.flush() + upload_id = upload.id + tool_file_id = tool_file.id + + statements: list[str] = [] + + def record_statement( + _connection: object, + _cursor: object, + statement: str, + _parameters: object, + _context: object, + _executemany: object, + ) -> None: + statements.append(statement) + + event.listen(sqlite_engine, "before_cursor_execute", record_statement) + try: + refs = tuple( + FileRef(id=upload_id, kind=FileKind.UPLOAD) + if index % 2 == 0 + else FileRef(id=tool_file_id, kind=FileKind.TOOL) + for index in range(100) + ) + resolved = FileGrantRepository(session_factory=sqlite_session_factory).resolve_owned_files( + context=FileGrantContext(tenant_id, "app-1", end_user_id), + refs=refs, + ) + finally: + event.remove(sqlite_engine, "before_cursor_execute", record_statement) + + assert len(statements) == 2 + assert [file.kind if file is not None else None for file in resolved] == [ref.kind for ref in refs] diff --git a/api/tests/unit_tests/repositories/test_oauth_access_token_repository.py b/api/tests/unit_tests/repositories/test_oauth_access_token_repository.py new file mode 100644 index 00000000000..3fd15186d84 --- /dev/null +++ b/api/tests/unit_tests/repositories/test_oauth_access_token_repository.py @@ -0,0 +1,126 @@ +from __future__ import annotations + +from datetime import UTC, datetime, timedelta + +import pytest +from sqlalchemy.orm import Session, sessionmaker + +from models.oauth import OAuthAccessToken +from repositories.oauth_access_token_repository import SQLAlchemyOAuthAccessTokenRepository + +ACCOUNT_ID = "11111111-1111-1111-1111-111111111111" +OTHER_ACCOUNT_ID = "22222222-2222-2222-2222-222222222222" +TOKEN_ID = "33333333-3333-3333-3333-333333333333" +OTHER_TOKEN_ID = "44444444-4444-4444-4444-444444444444" +NOW = datetime(2026, 8, 25, 12, tzinfo=UTC) + + +def _token( + *, + token_id: str = TOKEN_ID, + account_id: str | None = ACCOUNT_ID, + token_hash: str | None = "live-hash", + expires_at: datetime | None = None, + revoked_at: datetime | None = None, + created_at: datetime | None = None, +) -> OAuthAccessToken: + token = OAuthAccessToken( + subject_email="user@example.com", + subject_issuer="dify:account" if account_id is not None else "https://idp.example.com", + account_id=account_id, + client_id="difyctl", + device_label="test-device", + prefix="dfoa_" if account_id is not None else "dfoe_", + token_hash=token_hash, + expires_at=expires_at or NOW + timedelta(days=1), + revoked_at=revoked_at, + ) + token.id = token_id + if created_at is not None: + token.created_at = created_at + return token + + +@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) +def test_list_active_is_account_scoped_and_database_paginated( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + sqlite_session.add_all( + [ + _token(token_id=TOKEN_ID, created_at=NOW - timedelta(minutes=1)), + _token(token_id=OTHER_TOKEN_ID, created_at=NOW - timedelta(minutes=2)), + _token( + token_id="55555555-5555-5555-5555-555555555555", + expires_at=NOW - timedelta(seconds=1), + ), + _token( + token_id="66666666-6666-6666-6666-666666666666", + token_hash=None, + revoked_at=NOW - timedelta(seconds=1), + ), + _token(token_id="77777777-7777-7777-7777-777777777777", account_id=OTHER_ACCOUNT_ID), + _token(token_id="88888888-8888-8888-8888-888888888888", account_id=None), + ] + ) + sqlite_session.commit() + repository = SQLAlchemyOAuthAccessTokenRepository(session_factory=sqlite_session_factory) + + total, rows = repository.list_active(account_id=ACCOUNT_ID, active_at=NOW, offset=1, limit=1) + + assert total == 2 + assert [row.id for row in rows] == [OTHER_TOKEN_ID] + + +@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) +def test_revoke_returns_hash_and_persists_revocation( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + sqlite_session.add(_token()) + sqlite_session.commit() + repository = SQLAlchemyOAuthAccessTokenRepository(session_factory=sqlite_session_factory) + + result = repository.revoke(account_id=ACCOUNT_ID, token_id=TOKEN_ID, revoked_at=NOW) + + assert result.owned is True + assert result.token_hash == "live-hash" + sqlite_session.expire_all() + persisted = sqlite_session.get(OAuthAccessToken, TOKEN_ID) + assert persisted is not None + assert persisted.token_hash is None + assert persisted.revoked_at is not None + + +@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) +def test_revoke_is_idempotent_for_an_owned_session( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + sqlite_session.add(_token(token_hash=None, revoked_at=NOW - timedelta(minutes=1))) + sqlite_session.commit() + repository = SQLAlchemyOAuthAccessTokenRepository(session_factory=sqlite_session_factory) + + result = repository.revoke(account_id=ACCOUNT_ID, token_id=TOKEN_ID, revoked_at=NOW) + + assert result.owned is True + assert result.token_hash is None + + +@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) +def test_revoke_does_not_disclose_another_accounts_session( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + sqlite_session.add(_token(account_id=OTHER_ACCOUNT_ID)) + sqlite_session.commit() + repository = SQLAlchemyOAuthAccessTokenRepository(session_factory=sqlite_session_factory) + + result = repository.revoke(account_id=ACCOUNT_ID, token_id=TOKEN_ID, revoked_at=NOW) + + assert result.owned is False + assert result.token_hash is None + sqlite_session.expire_all() + persisted = sqlite_session.get(OAuthAccessToken, TOKEN_ID) + assert persisted is not None + assert persisted.token_hash == "live-hash" diff --git a/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py b/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py index 76383d88b9d..0606ccd23c8 100644 --- a/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py +++ b/api/tests/unit_tests/repositories/test_sqlalchemy_api_workflow_run_repository.py @@ -2,19 +2,25 @@ from __future__ import annotations import logging from datetime import UTC, datetime +from decimal import Decimal from unittest.mock import Mock, patch import pytest -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, sessionmaker from core.workflow.nodes.human_input.entities import FormDefinition, ParagraphInputConfig, UserActionConfig from core.workflow.nodes.human_input.enums import FormInputType from core.workflow.nodes.human_input.pause_reason import HumanInputRequired from graphon.entities.pause_reason import HitlRequired, PauseReasonType +from graphon.enums import WorkflowExecutionStatus, WorkflowType +from models import Message +from models.enums import ConversationFromSource, CreatorUserRole, WorkflowRunTriggeredFrom from models.human_input import HumanInputForm, HumanInputFormRecipient, RecipientType -from models.workflow import WorkflowPause, WorkflowPauseReason +from models.workflow import WorkflowPause, WorkflowPauseReason, WorkflowRun from repositories.sqlalchemy_api_workflow_run_repository import ( DifyAPISQLAlchemyWorkflowRunRepository, + WorkflowRunMessageRef, + WorkflowRunPauseRecord, _build_human_input_required_reason, _PrivateWorkflowPauseEntity, ) @@ -159,6 +165,133 @@ def test_private_workflow_pause_entity_preserves_list_shaped_pause_reasons() -> assert result == pause_reasons +def _message(*, message_id: str, app_id: str, workflow_run_id: str, conversation_id: str) -> Message: + message = Message( + app_id=app_id, + conversation_id=conversation_id, + query="query", + message={"role": "user", "content": "query"}, + answer="answer", + message_unit_price=Decimal("0.0001"), + answer_unit_price=Decimal("0.0001"), + currency="USD", + from_source=ConversationFromSource.API, + ) + message.id = message_id + message._inputs = {} + message.workflow_run_id = workflow_run_id + return message + + +def _workflow_run(*, run_id: str, tenant_id: str, status: WorkflowExecutionStatus) -> WorkflowRun: + return WorkflowRun( + id=run_id, + tenant_id=tenant_id, + app_id="app-1", + workflow_id="workflow-1", + type=WorkflowType.WORKFLOW, + triggered_from=WorkflowRunTriggeredFrom.DEBUGGING, + version="1", + graph="{}", + inputs="{}", + status=status, + created_by_role=CreatorUserRole.ACCOUNT, + created_by="account-1", + ) + + +def test_get_message_refs_filters_by_app_and_returns_lightweight_records( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + sqlite_session.add_all( + [ + _message(message_id="msg-1", app_id="app-1", workflow_run_id="run-1", conversation_id="conv-1"), + _message(message_id="msg-2", app_id="app-2", workflow_run_id="run-2", conversation_id="conv-2"), + ] + ) + sqlite_session.commit() + repository = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=sqlite_session_factory) + + result = repository.get_message_refs( + app_id="app-1", + workflow_run_ids=["run-1", "run-2"], + ) + + assert result == { + "run-1": WorkflowRunMessageRef(message_id="msg-1", conversation_id="conv-1"), + } + + +def test_get_pause_record_scopes_the_workflow_run_to_the_workspace( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + sqlite_session.add( + _workflow_run( + run_id="run-1", + tenant_id="tenant-1", + status=WorkflowExecutionStatus.SUCCEEDED, + ) + ) + sqlite_session.commit() + repository = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=sqlite_session_factory) + + assert repository.get_pause_record(workspace_id="tenant-2", workflow_run_id="run-1") is None + assert repository.get_pause_record( + workspace_id="tenant-1", + workflow_run_id="run-1", + ) == WorkflowRunPauseRecord( + status=WorkflowExecutionStatus.SUCCEEDED, + paused_at=None, + reasons=(), + form_tokens={}, + ) + + +def test_get_pause_record_loads_reasons_and_tokens_in_one_repository_call( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + workflow_run = _workflow_run( + run_id="run-1", + tenant_id="tenant-1", + status=WorkflowExecutionStatus.PAUSED, + ) + pause = WorkflowPause( + workflow_id=workflow_run.workflow_id, + workflow_run_id=workflow_run.id, + state_object_key="pause-state", + ) + pause.id = "pause-1" + reason = WorkflowPauseReason( + pause_id=pause.id, + type_=PauseReasonType.HITL_REQUIRED, + form_id="form-1", + node_id="node-1", + ) + recipient = HumanInputFormRecipient( + form_id="form-1", + delivery_id="delivery-1", + recipient_type=RecipientType.CONSOLE, + recipient_payload="{}", + access_token="form-token", + ) + sqlite_session.add_all([workflow_run, pause, reason, recipient]) + sqlite_session.commit() + repository = DifyAPISQLAlchemyWorkflowRunRepository(session_maker=sqlite_session_factory) + + result = repository.get_pause_record(workspace_id="tenant-1", workflow_run_id="run-1") + + assert result is not None + assert result.status == WorkflowExecutionStatus.PAUSED + assert result.paused_at == pause.created_at + assert len(result.reasons) == 1 + assert isinstance(result.reasons[0], HumanInputRequired) + assert result.reasons[0].form_id == "form-1" + assert result.form_tokens == {"form-1": "form-token"} + + def test_delete_pause_model_deletes_record_when_state_object_delete_fails( caplog: pytest.LogCaptureFixture, ) -> None: diff --git a/api/tests/unit_tests/repositories/test_workflow_app_log_query_repository.py b/api/tests/unit_tests/repositories/test_workflow_app_log_query_repository.py new file mode 100644 index 00000000000..2de5e684a02 --- /dev/null +++ b/api/tests/unit_tests/repositories/test_workflow_app_log_query_repository.py @@ -0,0 +1,304 @@ +import json +from datetime import UTC, datetime, timedelta + +import pytest +from sqlalchemy.orm import Session, sessionmaker + +from graphon.enums import WorkflowExecutionStatus +from models.account import Account, Tenant, TenantAccountJoin, TenantAccountRole +from models.enums import ( + AppTriggerType, + CreatorUserRole, + EndUserType, + WorkflowRunTriggeredFrom, + WorkflowTriggerStatus, +) +from models.model import EndUser +from models.trigger import WorkflowTriggerLog +from models.workflow import WorkflowAppLog, WorkflowAppLogCreatedFrom, WorkflowRun, WorkflowType +from repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepository +from services.workflow_app_log_query_service import WorkflowAppLogAccount, WorkflowAppLogEndUser + + +def _log( + log_id: str, + *, + created_by: str, + created_by_role: CreatorUserRole, + created_at: datetime, +) -> WorkflowAppLog: + log = WorkflowAppLog( + tenant_id="tenant-1", + app_id="app-1", + workflow_id="workflow-1", + workflow_run_id=f"run-{log_id}", + created_from=WorkflowAppLogCreatedFrom.SERVICE_API, + created_by_role=created_by_role, + created_by=created_by, + ) + log.id = log_id + log.created_at = created_at + return log + + +def _run( + run_id: str, + *, + tenant_id: str = "tenant-1", + app_id: str = "app-1", + triggered_from: WorkflowRunTriggeredFrom = WorkflowRunTriggeredFrom.APP_RUN, +) -> WorkflowRun: + created_at = datetime(2026, 1, 1, tzinfo=UTC) + return WorkflowRun( + id=run_id, + tenant_id=tenant_id, + app_id=app_id, + workflow_id="workflow-1", + type=WorkflowType.WORKFLOW, + triggered_from=triggered_from, + version="2026-01-01", + graph=json.dumps({"nodes": [], "edges": []}), + inputs=json.dumps({"input": "value"}), + status=WorkflowExecutionStatus.SUCCEEDED, + outputs=json.dumps({"output": "value"}), + error=None, + elapsed_time=0.5, + total_tokens=10, + total_steps=2, + created_by_role=CreatorUserRole.ACCOUNT, + created_by="actor-1", + created_at=created_at, + finished_at=created_at + timedelta(seconds=1), + exceptions_count=0, + ) + + +def test_get_paginated_returns_detached_actor_records_by_role_when_ids_overlap( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + created_at = datetime(2026, 1, 1) + tenant = Tenant(name="Workspace") + tenant.id = "tenant-1" + account = Account(name="Account", email="account@example.com") + account.id = "actor-1" + end_user = EndUser( + id="actor-1", + tenant_id=tenant.id, + app_id="app-1", + type=EndUserType.BROWSER, + session_id="session-1", + ) + sqlite_session.add_all( + [ + tenant, + account, + end_user, + TenantAccountJoin( + tenant_id=tenant.id, + account_id=account.id, + role=TenantAccountRole.OWNER, + ), + _log( + "account-log", + created_by=account.id, + created_by_role=CreatorUserRole.ACCOUNT, + created_at=created_at, + ), + _log( + "end-user-log", + created_by=end_user.id, + created_by_role=CreatorUserRole.END_USER, + created_at=created_at + timedelta(seconds=1), + ), + ] + ) + sqlite_session.commit() + sqlite_session.close() + + result = WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory).get_paginated( + tenant_id=tenant.id, + app_id="app-1", + ) + + assert result.total == 2 + assert [record.id for record in result.data] == ["end-user-log", "account-log"] + by_id = {record.id: record for record in result.data} + assert by_id["account-log"].created_by_account == WorkflowAppLogAccount( + id=account.id, + name=account.name, + email=account.email, + ) + assert by_id["account-log"].created_by_end_user is None + assert by_id["end-user-log"].created_by_account is None + assert by_id["end-user-log"].created_by_end_user == WorkflowAppLogEndUser( + id=end_user.id, + type=end_user.type.value, + is_anonymous=False, + session_id=end_user.session_id, + ) + + +def test_get_paginated_preserves_missing_account_filter_error( + sqlite_session_factory: sessionmaker[Session], +) -> None: + repository = WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory) + + with pytest.raises(ValueError, match=r"^Account not found: missing@example\.com$"): + repository.get_paginated( + tenant_id="tenant-1", + app_id="app-1", + created_by_account="missing@example.com", + ) + + +def test_enum_value_rejects_missing_required_value() -> None: + with pytest.raises(ValueError, match="Required enum value is missing"): + WorkflowAppLogQueryRepository._enum_value(None) + + +def test_get_paginated_projects_plugin_workflow_run_summary( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + log = _log( + "plugin-log", + created_by="actor-1", + created_by_role=CreatorUserRole.ACCOUNT, + created_at=datetime(2026, 1, 1, tzinfo=UTC), + ) + run = _run(log.workflow_run_id, triggered_from=WorkflowRunTriggeredFrom.PLUGIN) + sqlite_session.add_all([run, log]) + sqlite_session.commit() + + result = WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory).get_paginated( + tenant_id="tenant-1", + app_id="app-1", + ) + + assert result.total == 1 + summary = result.data[0].workflow_run + assert summary is not None + assert summary.id == run.id + assert summary.status == WorkflowExecutionStatus.SUCCEEDED.value + assert summary.triggered_from == WorkflowRunTriggeredFrom.PLUGIN.value + assert summary.version == run.version + assert summary.total_tokens == run.total_tokens + + +def test_get_paginated_keeps_log_when_workflow_run_is_missing( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + log = _log( + "orphan-log", + created_by="actor-1", + created_by_role=CreatorUserRole.ACCOUNT, + created_at=datetime(2026, 1, 1, tzinfo=UTC), + ) + sqlite_session.add(log) + sqlite_session.commit() + + repository = WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory) + result = repository.get_paginated(tenant_id="tenant-1", app_id="app-1") + + assert result.total == 1 + assert result.data[0].workflow_run is None + assert ( + repository.get_paginated( + tenant_id="tenant-1", + app_id="app-1", + status=WorkflowExecutionStatus.SUCCEEDED, + ).total + == 0 + ) + + +def test_get_paginated_includes_trigger_metadata_only_with_detail( + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + log = _log( + "trigger-log", + created_by="actor-1", + created_by_role=CreatorUserRole.ACCOUNT, + created_at=datetime(2026, 1, 1, tzinfo=UTC), + ) + trigger_metadata = json.dumps({"type": AppTriggerType.TRIGGER_SCHEDULE.value}) + sqlite_session.add_all( + [ + _run(log.workflow_run_id), + log, + WorkflowTriggerLog( + tenant_id="tenant-1", + app_id="app-1", + workflow_id="workflow-1", + workflow_run_id=log.workflow_run_id, + root_node_id=None, + trigger_metadata=trigger_metadata, + trigger_type=AppTriggerType.TRIGGER_SCHEDULE, + trigger_data="{}", + inputs="{}", + outputs=None, + status=WorkflowTriggerStatus.SUCCEEDED, + error=None, + queue_name="default", + celery_task_id=None, + created_by_role=CreatorUserRole.ACCOUNT, + created_by="actor-1", + retry_count=0, + ), + ] + ) + sqlite_session.commit() + + repository = WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory) + + assert repository.get_paginated(tenant_id="tenant-1", app_id="app-1").data[0].details is None + assert repository.get_paginated( + tenant_id="tenant-1", + app_id="app-1", + detail=True, + ).data[0].details == {"trigger_metadata": trigger_metadata} + + +@pytest.mark.parametrize( + ("run_tenant_id", "run_app_id"), + [ + ("tenant-2", "app-1"), + ("tenant-1", "app-2"), + ], +) +def test_get_paginated_does_not_attach_workflow_run_from_another_scope( + run_tenant_id: str, + run_app_id: str, + sqlite_session: Session, + sqlite_session_factory: sessionmaker[Session], +) -> None: + log = _log( + "mismatched-log", + created_by="actor-1", + created_by_role=CreatorUserRole.ACCOUNT, + created_at=datetime(2026, 1, 1, tzinfo=UTC), + ) + sqlite_session.add_all( + [ + _run(log.workflow_run_id, tenant_id=run_tenant_id, app_id=run_app_id), + log, + ] + ) + sqlite_session.commit() + + repository = WorkflowAppLogQueryRepository(session_factory=sqlite_session_factory) + result = repository.get_paginated(tenant_id="tenant-1", app_id="app-1") + + assert result.total == 1 + assert result.data[0].workflow_run is None + assert ( + repository.get_paginated( + tenant_id="tenant-1", + app_id="app-1", + status=WorkflowExecutionStatus.SUCCEEDED, + ).total + == 0 + ) diff --git a/api/tests/unit_tests/services/test_account_access_service.py b/api/tests/unit_tests/services/test_account_access_service.py new file mode 100644 index 00000000000..7121d8cc1da --- /dev/null +++ b/api/tests/unit_tests/services/test_account_access_service.py @@ -0,0 +1,173 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from datetime import UTC, datetime + +import pytest + +from machinery.context import AccountRequestContext +from services.account_access_service import AccountAccessService +from services.account_errors import AccountNotFoundError, AccountSessionNotFoundError +from services.entities.account_access_entities import ( + AccountSessionRevocation, + AccountSessionSnapshot, + AccountWorkspaceSnapshot, +) +from services.entities.account_entities import AccountSnapshot + +NOW = datetime(2026, 8, 25, 12, tzinfo=UTC) + + +def _context(*, token_id: str | None = "token-1") -> AccountRequestContext: + return AccountRequestContext("request-1", "trace-1", "account-1", token_id) + + +def _account() -> AccountSnapshot: + return AccountSnapshot( + id="account-1", + name="Ada", + email="ada@example.com", + avatar=None, + is_password_set=False, + interface_language=None, + interface_theme=None, + timezone=None, + last_login_at=None, + last_login_ip=None, + status="active", + initialized_at=None, + created_at=NOW, + ) + + +@dataclass +class _Accounts: + account: AccountSnapshot | None = field(default_factory=_account) + + def get(self, account_id: str) -> AccountSnapshot | None: + assert account_id == "account-1" + return self.account + + +@dataclass +class _Workspaces: + items: tuple[AccountWorkspaceSnapshot, ...] = () + + def list_account_access_workspaces(self, account_id: str) -> tuple[AccountWorkspaceSnapshot, ...]: + assert account_id == "account-1" + return self.items + + +@dataclass +class _Sessions: + items: tuple[AccountSessionSnapshot, ...] = () + total: int = 0 + revocation: AccountSessionRevocation = AccountSessionRevocation(owned=True) + list_call: tuple[str, datetime, int, int] | None = None + revoke_call: tuple[str, str, datetime] | None = None + + def list_active( + self, + *, + account_id: str, + active_at: datetime, + offset: int, + limit: int, + ) -> tuple[int, tuple[AccountSessionSnapshot, ...]]: + self.list_call = (account_id, active_at, offset, limit) + return self.total, self.items + + def revoke(self, *, account_id: str, token_id: str, revoked_at: datetime) -> AccountSessionRevocation: + self.revoke_call = (account_id, token_id, revoked_at) + return self.revocation + + +@dataclass +class _TokenCache: + invalidated: list[str] = field(default_factory=list) + + def __call__(self, token_hash: str) -> None: + self.invalidated.append(token_hash) + + +def _service( + *, + accounts: _Accounts | None = None, + workspaces: _Workspaces | None = None, + sessions: _Sessions | None = None, + token_cache: _TokenCache | None = None, +) -> AccountAccessService: + return AccountAccessService( + accounts=accounts or _Accounts(), + workspaces=workspaces or _Workspaces(), + sessions=sessions or _Sessions(), + invalidate_token_cache=token_cache or _TokenCache(), + now=lambda: NOW, + ) + + +def test_get_prefers_current_workspace_as_default() -> None: + workspaces = _Workspaces( + items=( + AccountWorkspaceSnapshot("workspace-1", "First", "normal", False), + AccountWorkspaceSnapshot("workspace-2", "Current", "owner", True), + ) + ) + + snapshot = _service(workspaces=workspaces).get(_context()) + + assert snapshot.account.email == "ada@example.com" + assert snapshot.workspaces == workspaces.items + assert snapshot.default_workspace_id == "workspace-2" + + +def test_get_falls_back_to_first_workspace() -> None: + workspaces = _Workspaces( + items=( + AccountWorkspaceSnapshot("workspace-1", "First", "normal", False), + AccountWorkspaceSnapshot("workspace-2", "Second", "owner", False), + ) + ) + + assert _service(workspaces=workspaces).get(_context()).default_workspace_id == "workspace-1" + + +def test_get_raises_when_admitted_account_disappeared() -> None: + with pytest.raises(AccountNotFoundError): + _service(accounts=_Accounts(account=None)).get(_context()) + + +def test_list_sessions_delegates_database_pagination() -> None: + sessions = _Sessions(total=12) + + page = _service(sessions=sessions).list_sessions(_context(), page=3, limit=5) + + assert sessions.list_call == ("account-1", NOW, 10, 5) + assert page.page == 3 + assert page.total == 12 + assert page.has_more is False + + +def test_revoke_current_session_invalidates_live_token_cache() -> None: + sessions = _Sessions(revocation=AccountSessionRevocation(owned=True, token_hash="hash-1")) + cache = _TokenCache() + + _service(sessions=sessions, token_cache=cache).revoke_current_session(_context()) + + assert sessions.revoke_call == ("account-1", "token-1", NOW) + assert cache.invalidated == ["hash-1"] + + +def test_revoke_foreign_session_does_not_invalidate_cache() -> None: + sessions = _Sessions(revocation=AccountSessionRevocation(owned=False)) + cache = _TokenCache() + + with pytest.raises(AccountSessionNotFoundError): + _service(sessions=sessions, token_cache=cache).revoke_session(_context(), token_id="foreign") + + assert cache.invalidated == [] + + +def test_revoke_current_requires_admitted_token_id() -> None: + with pytest.raises(RuntimeError, match="did not resolve an access token"): + _service().revoke_current_session(_context(token_id=None)) diff --git a/api/tests/unit_tests/services/test_account_service.py b/api/tests/unit_tests/services/test_account_service.py index 64ffe961cd6..75221d81f73 100644 --- a/api/tests/unit_tests/services/test_account_service.py +++ b/api/tests/unit_tests/services/test_account_service.py @@ -2901,22 +2901,6 @@ class TestSessionInjectedGetters: def test_account_belongs_to_tenant_false_when_no_join(self, sqlite_session: Session) -> None: assert TenantService.account_belongs_to_tenant("user-1", "tenant-1", session=sqlite_session) is False - def test_get_account_memberships_returns_join_tenant_pairs(self, sqlite_session: Session) -> None: - """Returns every ``(TenantAccountJoin, Tenant)`` pair for an account.""" - tenant = Tenant(name="Joined Workspace") - other_tenant = Tenant(name="Other Workspace") - sqlite_session.add_all([tenant, other_tenant]) - sqlite_session.flush() - join = self._add_tenant_account_join(sqlite_session, tenant, "user-123", TenantAccountRole.NORMAL, current=True) - self._add_tenant_account_join(sqlite_session, other_tenant, "other-user", TenantAccountRole.NORMAL) - sqlite_session.commit() - - out = TenantService.get_account_memberships("user-123", session=sqlite_session) - - assert len(out) == 1 - assert out[0][0] is join - assert out[0][1] is tenant - def test_get_workspaces_for_account_uses_session_execute(self, sqlite_session: Session) -> None: """The list endpoint orders by ``Tenant.created_at``; the helper returns ``(Tenant, TenantAccountJoin)`` rows in that order. diff --git a/api/tests/unit_tests/services/test_app_generate_service.py b/api/tests/unit_tests/services/test_app_generate_service.py index 646a30d5cfb..5b20675c9c8 100644 --- a/api/tests/unit_tests/services/test_app_generate_service.py +++ b/api/tests/unit_tests/services/test_app_generate_service.py @@ -13,6 +13,7 @@ Covers: - get_response_generator (ended / non-ended workflow run) """ +import json import threading import uuid from collections.abc import Callable @@ -26,7 +27,11 @@ from sqlalchemy.orm import Session import services.app_generate_service as ags_module from core.app.entities.app_invoke_entities import InvokeFrom from enums import DeploymentEdition, QuotaType -from models.model import AppMode +from graphon.enums import WorkflowExecutionStatus +from models.account import Account +from models.enums import AppStatus, CreatorUserRole +from models.model import App, AppMode +from models.workflow import Workflow, WorkflowRun, WorkflowRunTriggeredFrom, WorkflowType from services.app_generate_service import AppGenerateService from services.errors.app import ( TriggerWorkflowServiceModeUnavailableError, @@ -67,19 +72,27 @@ class _DummyRateLimit: return generator -def _make_app(mode: AppMode | str, *, max_active_requests: int = 0, is_agent: bool = False) -> MagicMock: - app = MagicMock() - app.mode = mode - app.id = "app-id" - app.tenant_id = "tenant-id" - app.max_active_requests = max_active_requests - app.is_agent = is_agent - app.is_agent_with_session.return_value = is_agent +def _make_app(mode: AppMode | str, *, max_active_requests: int = 0) -> App: + app = App( + id="app-id", + tenant_id="tenant-id", + name="App", + description="", + mode=AppMode.CHAT if isinstance(mode, str) and mode == "invalid-mode" else mode, + status=AppStatus.NORMAL, + enable_site=False, + enable_api=False, + api_rpm=0, + api_rph=0, + max_active_requests=max_active_requests, + ) + if mode == "invalid-mode": + app.mode = mode # type: ignore[assignment] return app -def _make_user() -> MagicMock: - user = MagicMock() +def _make_user() -> Account: + user = Account(name="User", email="user@example.com") user.id = "user-id" return user @@ -95,14 +108,42 @@ def _make_workflow( workflow_id: str = "workflow-id", created_by: str = "owner-id", node_types: tuple[str, ...] = (), -) -> MagicMock: - workflow = MagicMock() - workflow.id = workflow_id - workflow.created_by = created_by - workflow.walk_nodes.return_value = [ - (f"node-{index}", {"type": node_type}) for index, node_type in enumerate(node_types) - ] - return workflow +) -> Workflow: + return Workflow( + id=workflow_id, + tenant_id="tenant-id", + app_id="app-id", + type=WorkflowType.WORKFLOW, + version=Workflow.VERSION_DRAFT, + graph=json.dumps( + { + "nodes": [ + {"id": f"node-{index}", "data": {"type": node_type}} for index, node_type in enumerate(node_types) + ], + "edges": [], + } + ), + features={}, + created_by=created_by, + environment_variables=[], + conversation_variables=[], + ) + + +def _make_workflow_run(*, run_id: str, ended: bool) -> WorkflowRun: + run = WorkflowRun( + tenant_id="tenant-id", + app_id="app-id", + workflow_id="workflow-id", + type=WorkflowType.WORKFLOW, + triggered_from=WorkflowRunTriggeredFrom.APP_RUN, + version="published", + status=WorkflowExecutionStatus.SUCCEEDED if ended else WorkflowExecutionStatus.RUNNING, + created_by_role=CreatorUserRole.ACCOUNT, + created_by="user-id", + ) + run.id = run_id + return run @contextmanager @@ -335,7 +376,8 @@ class TestGenerate(_RealSessionTest): "services.app_generate_service.AgentChatAppGenerator.convert_to_event_stream", side_effect=lambda x: x, ) - app = _make_app(AppMode.CHAT, is_agent=True) + app = _make_app(AppMode.CHAT) + is_agent = mocker.patch.object(App, "is_agent_with_session", return_value=True) session = self.session result = AppGenerateService.generate( app_model=app, @@ -347,7 +389,7 @@ class TestGenerate(_RealSessionTest): ) assert result == {"result": "agent-via-flag"} gen_spy.assert_called_once() - app.is_agent_with_session.assert_called_once_with(session=session) + is_agent.assert_called_once_with(session=session) # -- AGENT -------------------------------------------------------------- def test_agent_mode_passes_session(self, mocker: MockerFixture): @@ -383,7 +425,7 @@ class TestGenerate(_RealSessionTest): "services.app_generate_service.ChatAppGenerator.convert_to_event_stream", side_effect=lambda x: x, ) - app = _make_app(AppMode.CHAT, is_agent=False) + app = _make_app(AppMode.CHAT) result = AppGenerateService.generate( app_model=app, user=_make_user(), @@ -600,7 +642,7 @@ class TestGenerate(_RealSessionTest): # -- Invalid mode ------------------------------------------------------- def test_invalid_mode_raises(self, mocker: MockerFixture): - app = _make_app("invalid-mode", is_agent=False) + app = _make_app("invalid-mode") with pytest.raises(ValueError, match="Invalid app mode"): AppGenerateService.generate( app_model=app, @@ -1039,9 +1081,7 @@ class TestGenerateMoreLikeThis(_RealSessionTest): class TestGetResponseGenerator: def test_non_ended_workflow_run(self, mocker: MockerFixture): app = _make_app(AppMode.ADVANCED_CHAT) - workflow_run = MagicMock() - workflow_run.id = "run-1" - workflow_run.status.is_ended.return_value = False + workflow_run = _make_workflow_run(run_id="run-1", ended=False) gen_instance = MagicMock() gen_instance.retrieve_events.return_value = iter([{"event": "started"}]) @@ -1057,9 +1097,7 @@ class TestGetResponseGenerator: def test_ended_workflow_run_still_returns_generator(self, mocker: MockerFixture): """Even when the run is ended, the current code still returns a generator (TODO branch).""" app = _make_app(AppMode.WORKFLOW) - workflow_run = MagicMock() - workflow_run.id = "run-2" - workflow_run.status.is_ended.return_value = True + workflow_run = _make_workflow_run(run_id="run-2", ended=True) gen_instance = MagicMock() gen_instance.retrieve_events.return_value = iter([]) diff --git a/api/tests/unit_tests/services/test_app_tracing_config_gateway.py b/api/tests/unit_tests/services/test_app_tracing_config_gateway.py new file mode 100644 index 00000000000..7a5d7f9e75e --- /dev/null +++ b/api/tests/unit_tests/services/test_app_tracing_config_gateway.py @@ -0,0 +1,360 @@ +from unittest.mock import patch + +import pytest +from pydantic import ValidationInfo, field_validator + +from core.ops.entities.config_entity import BaseTracingConfig +from core.ops.ops_trace_manager import TracingProviderConfigEntry +from services import app_tracing_config_gateway as gateway_module +from services.app_tracing_config_gateway import OpsTraceManagerGateway +from services.app_tracing_config_service import ( + AppTracingConfigInvalidConfigurationError, + AppTracingConfigInvalidProviderError, + AppTracingConfigProcessingError, + AppTracingConfigVerificationFailedError, +) + + +class _ProviderConfig(BaseTracingConfig): + endpoint: str = "https://default.example.com" + project: str = "default-project" + + @field_validator("endpoint", "project", mode="before") + @classmethod + def replace_empty_with_default(cls, value: object, info: ValidationInfo) -> object: + if value != "": + return value + if info.field_name == "endpoint": + return "https://default.example.com" + return "default-project" + + +def _provider_entry(*, other_keys: list[str] | None = None) -> TracingProviderConfigEntry: + return { + "config_class": _ProviderConfig, + "secret_keys": ["api_key"], + "other_keys": other_keys or [], + "trace_instance": object, + } + + +def test_validate_provider_rejects_unknown_provider(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {}) + + with pytest.raises(AppTracingConfigInvalidProviderError, match="Invalid tracing provider: unknown"): + OpsTraceManagerGateway().validate_provider("unknown") + + +def test_prepare_new_config_applies_defaults_validates_and_encrypts(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr( + gateway_module, + "provider_config_map", + {"arize": _provider_entry(other_keys=["endpoint", "project"])}, + ) + submitted = {"api_key": "plain", "endpoint": "", "project": ""} + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.return_value = True + manager.get_trace_config_project_url.return_value = "https://project.example.com" + manager.encrypt_tracing_config.return_value = {"api_key": "encrypted"} + + result = OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config=submitted, + ) + + normalized = { + "api_key": "plain", + "endpoint": "https://default.example.com", + "project": "default-project", + } + assert submitted == {"api_key": "plain", "endpoint": "", "project": ""} + assert result == {"api_key": "encrypted", "project_url": "https://project.example.com"} + manager.check_trace_config_is_effective.assert_called_once_with(normalized, "arize") + manager.encrypt_tracing_config.assert_called_once_with("workspace-1", "arize", normalized) + + +def test_prepare_new_config_reports_failed_verification_before_encryption( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.return_value = False + + with pytest.raises(AppTracingConfigVerificationFailedError): + OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"api_key": "plain"}, + ) + + manager.encrypt_tracing_config.assert_not_called() + + +def test_prepare_new_config_keeps_success_when_project_url_lookup_fails( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.return_value = True + manager.get_trace_config_project_url.side_effect = RuntimeError("provider unavailable") + manager.encrypt_tracing_config.return_value = {"api_key": "encrypted"} + + result = OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"api_key": "plain"}, + ) + + assert result == {"api_key": "encrypted"} + + +def test_prepare_new_langfuse_config_builds_project_url(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"langfuse": _provider_entry()}) + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.return_value = True + manager.get_trace_config_project_key.return_value = "project-key" + manager.encrypt_tracing_config.return_value = {"secret_key": "encrypted"} + + result = OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="langfuse", + tracing_config={"host": "https://langfuse.example.com"}, + ) + + assert result["project_url"] == "https://langfuse.example.com/project/project-key" + + +def test_prepare_new_config_reports_provider_check_exception_as_failed_verification( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.side_effect = ValueError("verification failed") + + with pytest.raises(AppTracingConfigVerificationFailedError) as caught: + OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={}, + ) + + assert isinstance(caught.value.__cause__, ValueError) + + +def test_prepare_new_config_propagates_unexpected_verification_failure( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + failure = RuntimeError("unexpected provider bug") + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.side_effect = failure + + with pytest.raises(RuntimeError) as caught: + OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={}, + ) + + assert caught.value is failure + manager.encrypt_tracing_config.assert_not_called() + + +def test_prepare_new_config_rejects_invalid_schema_before_verification( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + + with patch.object(gateway_module, "OpsTraceManager") as manager: + with pytest.raises(AppTracingConfigInvalidConfigurationError): + OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"endpoint": {"invalid": "value"}}, + ) + + manager.check_trace_config_is_effective.assert_not_called() + manager.encrypt_tracing_config.assert_not_called() + + +def test_prepare_new_config_reports_encryption_failure_as_processing_error(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + failure = RuntimeError("key provider unavailable") + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.check_trace_config_is_effective.return_value = True + manager.get_trace_config_project_url.return_value = None + manager.encrypt_tracing_config.side_effect = failure + + with pytest.raises(AppTracingConfigProcessingError) as caught: + OpsTraceManagerGateway().prepare_new_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={}, + ) + + assert caught.value.__cause__ is failure + + +def test_prepare_updated_config_preserves_masked_secret_from_current_config( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + submitted = {"api_key": "******", "project": "new-project"} + current = {"api_key": "old-encrypted", "project": "old-project"} + encrypted = {"api_key": "old-encrypted", "project": "new-project"} + decrypted = {"api_key": "old-plain", "project": "new-project"} + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.encrypt_tracing_config.return_value = encrypted + manager.decrypt_tracing_config.return_value = decrypted + manager.check_trace_config_is_effective.return_value = True + + result = OpsTraceManagerGateway().prepare_updated_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config=submitted, + current_tracing_config=current, + ) + + assert result == encrypted + assert submitted == {"api_key": "******", "project": "new-project"} + manager.encrypt_tracing_config.assert_called_once_with("workspace-1", "arize", submitted, current) + manager.decrypt_tracing_config.assert_called_once_with("workspace-1", "arize", encrypted) + manager.check_trace_config_is_effective.assert_called_once_with(decrypted, "arize") + + +def test_prepare_updated_config_validates_schema_before_encryption(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + + with patch.object(gateway_module, "OpsTraceManager") as manager: + with pytest.raises(AppTracingConfigInvalidConfigurationError): + OpsTraceManagerGateway().prepare_updated_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"endpoint": {"invalid": "value"}}, + current_tracing_config={"api_key": "old-encrypted"}, + ) + + manager.encrypt_tracing_config.assert_not_called() + manager.decrypt_tracing_config.assert_not_called() + manager.check_trace_config_is_effective.assert_not_called() + + +def test_prepare_updated_config_reports_decryption_failure_as_processing_error( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(gateway_module, "provider_config_map", {"arize": _provider_entry()}) + failure = RuntimeError("stored credential cannot be decrypted") + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.encrypt_tracing_config.return_value = {"api_key": "encrypted"} + manager.decrypt_tracing_config.side_effect = failure + + with pytest.raises(AppTracingConfigProcessingError) as caught: + OpsTraceManagerGateway().prepare_updated_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"project": "new-project"}, + current_tracing_config={"api_key": "old-encrypted"}, + ) + + assert caught.value.__cause__ is failure + manager.check_trace_config_is_effective.assert_not_called() + + +@pytest.mark.parametrize( + ("provider", "fallback_url"), + [ + ("arize", "https://app.arize.com/"), + ("phoenix", "https://app.phoenix.arize.com/projects/"), + ("langsmith", "https://smith.langchain.com/"), + ("opik", "https://www.comet.com/opik/"), + ("weave", "https://wandb.ai/"), + ("aliyun", "https://arms.console.aliyun.com/"), + ("tencent", "https://console.cloud.tencent.com/apm"), + ("mlflow", "http://localhost:5000/"), + ("databricks", "https://www.databricks.com/"), + ], +) +def test_present_config_uses_provider_fallback_when_project_lookup_fails( + provider: str, + fallback_url: str, +) -> None: + with patch.object(gateway_module, "OpsTraceManager") as manager: + decrypted_config: dict[str, object] = {} + presented_config: dict[str, object] = {} + manager.decrypt_tracing_config.return_value = decrypted_config + manager.obfuscated_decrypt_token.return_value = presented_config + manager.get_trace_config_project_url.side_effect = RuntimeError("provider unavailable") + + result = OpsTraceManagerGateway().present_config( + workspace_id="workspace-1", + tracing_provider=provider, + tracing_config={"encrypted": "config"}, + ) + + assert result == {"project_url": fallback_url} + + +def test_present_langfuse_config_builds_project_url() -> None: + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.decrypt_tracing_config.return_value = {"host": "https://langfuse.example.com"} + manager.obfuscated_decrypt_token.return_value = {"host": "https://langfuse.example.com"} + manager.get_trace_config_project_key.return_value = "project-key" + + result = OpsTraceManagerGateway().present_config( + workspace_id="workspace-1", + tracing_provider="langfuse", + tracing_config={"encrypted": "config"}, + ) + + assert result["project_url"] == "https://langfuse.example.com/project/project-key" + + +def test_present_langfuse_config_falls_back_to_host() -> None: + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.decrypt_tracing_config.return_value = {"host": "https://langfuse.example.com"} + manager.obfuscated_decrypt_token.return_value = {"host": "https://langfuse.example.com"} + manager.get_trace_config_project_key.side_effect = RuntimeError("provider unavailable") + + result = OpsTraceManagerGateway().present_config( + workspace_id="workspace-1", + tracing_provider="langfuse", + tracing_config={"encrypted": "config"}, + ) + + assert result["project_url"] == "https://langfuse.example.com/" + + +def test_present_config_rejects_missing_stored_config() -> None: + with pytest.raises(AppTracingConfigProcessingError, match="processing failed"): + OpsTraceManagerGateway().present_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config=None, + ) + + +def test_present_config_reports_decryption_failure_as_processing_error() -> None: + failure = RuntimeError("stored credential cannot be decrypted") + + with patch.object(gateway_module, "OpsTraceManager") as manager: + manager.decrypt_tracing_config.side_effect = failure + + with pytest.raises(AppTracingConfigProcessingError) as caught: + OpsTraceManagerGateway().present_config( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"encrypted": "config"}, + ) + + assert caught.value.__cause__ is failure + manager.obfuscated_decrypt_token.assert_not_called() diff --git a/api/tests/unit_tests/services/test_app_tracing_config_service.py b/api/tests/unit_tests/services/test_app_tracing_config_service.py new file mode 100644 index 00000000000..208b5d4a972 --- /dev/null +++ b/api/tests/unit_tests/services/test_app_tracing_config_service.py @@ -0,0 +1,229 @@ +from datetime import UTC, datetime +from unittest.mock import MagicMock + +import pytest + +from machinery.context import RequestContext +from services.app_tracing_config_service import ( + AppTracingConfigAlreadyExistsError, + AppTracingConfigNotFoundError, + AppTracingConfigRecord, + AppTracingConfigService, +) + + +def _context() -> RequestContext: + return RequestContext("request-1", None, "account-1", "workspace-1") + + +def _record(*, tracing_config: dict[str, object] | None = None) -> AppTracingConfigRecord: + timestamp = datetime(2026, 1, 1, tzinfo=UTC) + return AppTracingConfigRecord( + id="config-1", + app_id="app-1", + tracing_provider="arize", + tracing_config=tracing_config, + is_active=True, + created_at=timestamp, + updated_at=timestamp, + ) + + +def test_get_scopes_lookup_to_workspace_and_presents_config() -> None: + stored = _record(tracing_config={"api_key": "encrypted"}) + configs = MagicMock() + configs.get.return_value = stored + provider = MagicMock() + provider.present_config.return_value = {"api_key": "******", "project_url": "https://example.com"} + service = AppTracingConfigService(configs=configs, provider=provider) + + result = service.get(_context(), "app-1", "arize") + + assert result == _record(tracing_config={"api_key": "******", "project_url": "https://example.com"}) + assert stored.tracing_config == {"api_key": "encrypted"} + provider.validate_provider.assert_called_once_with("arize") + configs.get.assert_called_once_with( + workspace_id="workspace-1", + app_id="app-1", + tracing_provider="arize", + ) + provider.present_config.assert_called_once_with( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"api_key": "encrypted"}, + ) + + +def test_get_returns_none_without_calling_provider_for_missing_config() -> None: + configs = MagicMock() + configs.get.return_value = None + provider = MagicMock() + service = AppTracingConfigService(configs=configs, provider=provider) + + assert service.get(_context(), "app-1", "arize") is None + + provider.validate_provider.assert_called_once_with("arize") + provider.present_config.assert_not_called() + + +def test_get_validates_provider_before_store_lookup() -> None: + events: list[str] = [] + configs = MagicMock() + configs.get.side_effect = lambda **_: events.append("store.get") + provider = MagicMock() + provider.validate_provider.side_effect = lambda *_: events.append("provider.validate") + service = AppTracingConfigService(configs=configs, provider=provider) + + assert service.get(_context(), "app-1", "arize") is None + + assert events == ["provider.validate", "store.get"] + + +def test_create_calls_provider_between_separate_store_operations() -> None: + events: list[str] = [] + configs = MagicMock() + + def get_config(**_: object) -> None: + events.append("store.get:closed") + + def create_config(**_: object) -> bool: + events.append("store.create:closed") + return True + + configs.get.side_effect = get_config + configs.create.side_effect = create_config + provider = MagicMock() + + def prepare_config(**_: object) -> dict[str, str]: + events.append("provider.prepare") + return {"api_key": "encrypted"} + + provider.prepare_new_config.side_effect = prepare_config + service = AppTracingConfigService(configs=configs, provider=provider) + + service.create(_context(), "app-1", "arize", {"api_key": "plain"}) + + assert events == ["store.get:closed", "provider.prepare", "store.create:closed"] + provider.prepare_new_config.assert_called_once_with( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"api_key": "plain"}, + ) + configs.create.assert_called_once_with( + workspace_id="workspace-1", + app_id="app-1", + tracing_provider="arize", + tracing_config={"api_key": "encrypted"}, + ) + + +def test_create_reports_duplicate_before_provider_preparation() -> None: + configs = MagicMock() + configs.get.return_value = _record(tracing_config={"api_key": "encrypted"}) + provider = MagicMock() + provider.prepare_new_config.return_value = {"api_key": "new-encrypted"} + service = AppTracingConfigService(configs=configs, provider=provider) + + with pytest.raises(AppTracingConfigAlreadyExistsError, match="Trace config is exist"): + service.create(_context(), "app-1", "arize", {"api_key": "plain"}) + + provider.prepare_new_config.assert_not_called() + configs.create.assert_not_called() + + +def test_create_reports_duplicate_detected_during_write() -> None: + configs = MagicMock() + configs.get.return_value = None + configs.create.return_value = False + provider = MagicMock() + provider.prepare_new_config.return_value = {"api_key": "encrypted"} + service = AppTracingConfigService(configs=configs, provider=provider) + + with pytest.raises(AppTracingConfigAlreadyExistsError): + service.create(_context(), "app-1", "arize", {"api_key": "plain"}) + + +def test_update_validates_reads_prepares_and_writes_in_order() -> None: + events: list[str] = [] + current = _record(tracing_config={"api_key": "old-encrypted"}) + configs = MagicMock() + configs.get.side_effect = lambda **_: events.append("store.get:closed") or current + configs.update.side_effect = lambda **_: events.append("store.update:closed") or True + provider = MagicMock() + provider.validate_provider.side_effect = lambda *_: events.append("provider.validate") + provider.prepare_updated_config.side_effect = lambda **_: ( + events.append("provider.prepare") or {"api_key": "new-encrypted"} + ) + service = AppTracingConfigService(configs=configs, provider=provider) + + service.update(_context(), "app-1", "arize", {"api_key": "******"}) + + assert events == ["store.get:closed", "provider.validate", "provider.prepare", "store.update:closed"] + provider.prepare_updated_config.assert_called_once_with( + workspace_id="workspace-1", + tracing_provider="arize", + tracing_config={"api_key": "******"}, + current_tracing_config={"api_key": "old-encrypted"}, + ) + configs.update.assert_called_once_with( + workspace_id="workspace-1", + app_id="app-1", + tracing_provider="arize", + tracing_config={"api_key": "new-encrypted"}, + ) + + +def test_update_reports_config_missing_before_provider_preparation() -> None: + configs = MagicMock() + configs.get.return_value = None + provider = MagicMock() + service = AppTracingConfigService(configs=configs, provider=provider) + + with pytest.raises(AppTracingConfigNotFoundError, match="Trace config not exist"): + service.update(_context(), "app-1", "arize", {}) + + provider.validate_provider.assert_called_once_with("arize") + provider.prepare_updated_config.assert_not_called() + configs.update.assert_not_called() + + +def test_update_reports_config_removed_before_write() -> None: + configs = MagicMock() + configs.get.return_value = _record(tracing_config={"api_key": "old-encrypted"}) + configs.update.return_value = False + provider = MagicMock() + provider.prepare_updated_config.return_value = {"api_key": "new-encrypted"} + service = AppTracingConfigService(configs=configs, provider=provider) + + with pytest.raises(AppTracingConfigNotFoundError): + service.update(_context(), "app-1", "arize", {"api_key": "******"}) + + +def test_delete_scopes_to_workspace_and_reports_missing_config() -> None: + configs = MagicMock() + configs.delete.return_value = False + provider = MagicMock() + service = AppTracingConfigService(configs=configs, provider=provider) + + with pytest.raises(AppTracingConfigNotFoundError): + service.delete(_context(), "app-1", "arize") + + configs.delete.assert_called_once_with( + workspace_id="workspace-1", + app_id="app-1", + tracing_provider="arize", + ) + provider.validate_provider.assert_called_once_with("arize") + + +def test_delete_validates_provider_before_store_delete() -> None: + events: list[str] = [] + configs = MagicMock() + configs.delete.side_effect = lambda **_: events.append("store.delete") or True + provider = MagicMock() + provider.validate_provider.side_effect = lambda *_: events.append("provider.validate") + service = AppTracingConfigService(configs=configs, provider=provider) + + service.delete(_context(), "app-1", "arize") + + assert events == ["provider.validate", "store.delete"] diff --git a/api/tests/unit_tests/services/test_dataset_service_segment.py b/api/tests/unit_tests/services/test_dataset_service_segment.py index 5598361f977..1d50e5a487a 100644 --- a/api/tests/unit_tests/services/test_dataset_service_segment.py +++ b/api/tests/unit_tests/services/test_dataset_service_segment.py @@ -677,6 +677,46 @@ class TestSegmentServiceMutations: assert result is refreshed_segment assert segment.keywords == ["new"] vector_service.update_segment_vector.assert_called_once_with(["new"], segment, dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() + + def test_update_segment_omits_attachment_ids_leaves_existing_bindings_unchanged(self, account_context): + session = MagicMock() + segment = _make_segment(content="same content") + document = _make_document(doc_form=IndexStructureType.PARAGRAPH_INDEX, word_count=20) + dataset = _make_dataset() + refreshed_segment = SimpleNamespace(id=segment.id) + args = SegmentUpdateArgs(content="same content") + + with ( + patch("services.dataset_service.redis_client") as mock_redis, + patch("services.dataset_service.VectorService") as vector_service, + ): + mock_redis.get.return_value = None + session.get.return_value = refreshed_segment + + result = SegmentService.update_segment(args, segment, document, dataset, session) + + assert result is refreshed_segment + vector_service.update_multimodel_vector.assert_not_called() + + def test_update_segment_explicit_empty_attachment_ids_clears_bindings(self, account_context): + session = MagicMock() + segment = _make_segment(content="same content") + document = _make_document(doc_form=IndexStructureType.PARAGRAPH_INDEX, word_count=20) + dataset = _make_dataset() + refreshed_segment = SimpleNamespace(id=segment.id) + args = SegmentUpdateArgs(content="same content", attachment_ids=[]) + + with ( + patch("services.dataset_service.redis_client") as mock_redis, + patch("services.dataset_service.VectorService") as vector_service, + ): + mock_redis.get.return_value = None + session.get.return_value = refreshed_segment + + result = SegmentService.update_segment(args, segment, document, dataset, session) + + assert result is refreshed_segment vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) def test_update_segment_regenerates_child_chunks_and_updates_manual_summary(self, account_context): @@ -724,7 +764,7 @@ class TestSegmentServiceMutations: session=session, ) update_summary.assert_called_once_with(segment, dataset, "new summary", session=session) - vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() def test_update_segment_auto_regenerates_summary_after_content_change(self, account_context): session = MagicMock() @@ -763,7 +803,7 @@ class TestSegmentServiceMutations: assert document.word_count == 18 vector_service.update_segment_vector.assert_called_once_with(["kw-1"], segment, dataset, session=session) generate_summary.assert_called_once_with(segment, dataset, {"enable": True}, session=session) - vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() def test_update_segment_regenerates_summary_when_manual_summary_is_unchanged(self, account_context): session = MagicMock() @@ -799,7 +839,7 @@ class TestSegmentServiceMutations: assert result is refreshed_segment generate_summary.assert_called_once_with(segment, dataset, {"enable": True}, session=session) update_summary.assert_not_called() - vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() def test_delete_segment_removes_index_and_updates_document_word_count(self): session = MagicMock() @@ -1012,7 +1052,7 @@ class TestSegmentServiceAdditionalRegenerationBranches: assert segment.word_count == len("question") + len("new answer") assert document.word_count == 20 + (len("question") + len("new answer") - 8) vector_service.update_segment_vector.assert_not_called() - vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() def test_update_segment_content_change_uses_answer_when_counting_tokens_for_qa_segments(self, account_context): session = MagicMock() @@ -1049,7 +1089,7 @@ class TestSegmentServiceAdditionalRegenerationBranches: assert segment.tokens == 21 assert segment.word_count == len("new question") + len("new answer") vector_service.update_segment_vector.assert_called_once_with(["kw-1"], segment, dataset, session=session) - vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() def test_update_segment_content_change_parent_child_uses_default_embedding_and_ignores_summary_failures( self, account_context @@ -1107,7 +1147,7 @@ class TestSegmentServiceAdditionalRegenerationBranches: session=session, ) update_summary.assert_called_once_with(segment, dataset, "new summary", session=session) - vector_service.update_multimodel_vector.assert_called_once_with(segment, [], dataset, session=session) + vector_service.update_multimodel_vector.assert_not_called() def test_update_segment_same_content_parent_child_marks_segment_error_for_non_high_quality_dataset( self, account_context diff --git a/api/tests/unit_tests/services/test_file_grant_gateways.py b/api/tests/unit_tests/services/test_file_grant_gateways.py new file mode 100644 index 00000000000..2b909f2ca03 --- /dev/null +++ b/api/tests/unit_tests/services/test_file_grant_gateways.py @@ -0,0 +1,90 @@ +from collections.abc import Callable +from unittest.mock import patch + +import httpx +import pytest + +from services.errors.file import FileTooLargeError +from services.file_grant_gateways import FileGrantRemoteFileGateway + + +def test_remote_file_gateway_bounds_a_get_without_content_length( + config_overrides: Callable[..., None], +) -> None: + config_overrides(UPLOAD_FILE_SIZE_LIMIT=1) + url = "https://example.com/report.pdf" + head = httpx.Response(200, request=httpx.Request("HEAD", url)) + download = httpx.Response( + 200, + content=b"0" * (1024 * 1024 + 1), + request=httpx.Request("GET", url), + ) + + with patch("services.file_grant_gateways.remote_fetcher.make_request", side_effect=[head, download]) as request: + with pytest.raises(FileTooLargeError): + FileGrantRemoteFileGateway().fetch(url) + + assert request.call_args_list[1].kwargs["stream_response"] is True + assert download.is_closed + + +def test_remote_file_gateway_uses_the_actual_body_size_when_content_length_is_incorrect( + config_overrides: Callable[..., None], +) -> None: + config_overrides(UPLOAD_FILE_SIZE_LIMIT=1) + url = "https://example.com/report.pdf" + head = httpx.Response( + 200, + headers={"Content-Length": "1"}, + request=httpx.Request("HEAD", url), + ) + download = httpx.Response( + 200, + headers={"Content-Length": "1"}, + content=b"0" * (1024 * 1024 + 1), + request=httpx.Request("GET", url), + ) + + with patch("services.file_grant_gateways.remote_fetcher.make_request", side_effect=[head, download]): + with pytest.raises(FileTooLargeError): + FileGrantRemoteFileGateway().fetch(url) + + assert download.is_closed + + +def test_remote_file_gateway_rejects_encoded_content_that_cannot_be_safely_bounded( + config_overrides: Callable[..., None], +) -> None: + config_overrides(UPLOAD_FILE_SIZE_LIMIT=1) + url = "https://example.com/report.pdf" + head = httpx.Response(200, request=httpx.Request("HEAD", url)) + download = httpx.Response( + 200, + headers={"Content-Encoding": "gzip"}, + stream=httpx.ByteStream(b"compressed"), + request=httpx.Request("GET", url), + ) + + with patch("services.file_grant_gateways.remote_fetcher.make_request", side_effect=[head, download]): + assert FileGrantRemoteFileGateway().fetch(url) is None + + assert download.is_closed + + +def test_remote_file_gateway_returns_bounded_content(config_overrides: Callable[..., None]) -> None: + config_overrides(UPLOAD_FILE_SIZE_LIMIT=1) + url = "https://example.com/report.pdf" + head = httpx.Response( + 200, + headers={"Content-Length": "9", "Content-Type": "application/pdf"}, + request=httpx.Request("HEAD", url), + ) + download = httpx.Response(200, content=b"pdf-bytes", request=httpx.Request("GET", url)) + + with patch("services.file_grant_gateways.remote_fetcher.make_request", side_effect=[head, download]): + file = FileGrantRemoteFileGateway().fetch(url) + + assert file is not None + assert file.filename == "report.pdf" + assert file.mimetype == "application/pdf" + assert file.content == b"pdf-bytes" diff --git a/api/tests/unit_tests/services/test_file_grant_service.py b/api/tests/unit_tests/services/test_file_grant_service.py new file mode 100644 index 00000000000..741eb9471d4 --- /dev/null +++ b/api/tests/unit_tests/services/test_file_grant_service.py @@ -0,0 +1,147 @@ +from io import BytesIO +from unittest.mock import MagicMock + +import pytest + +from services.entities.file_grant_entities import ( + FileGrantContext, + FileGrantLimits, + FileGrantMintRequest, + FileGrantScope, + FileKind, + FileRef, + ResolvedFile, +) +from services.errors.file_grant import EndUserNotFoundError, GrantTtlTooLongError +from services.file_grant_service import MAX_SESSION_GRANT_TTL_SECONDS, FileGrantService + + +def _service() -> tuple[FileGrantService, MagicMock, MagicMock, MagicMock, MagicMock]: + repository = MagicMock() + repository.get_or_create_subject.return_value = "end-user-1" + repository.subject_exists.return_value = True + repository.resolve_owned_files.return_value = list[ResolvedFile | None]() + files = MagicMock() + tokens = MagicMock() + tokens.issue_grant.return_value = ("grant", 1600) + remote_files = MagicMock() + service = FileGrantService( + repository=repository, + files=files, + tokens=tokens, + remote_files=remote_files, + limits=FileGrantLimits(15, 10, 50, 100, 10, 5), + now=lambda: 1000, + ) + return service, repository, files, tokens, remote_files + + +def _mint_request( + *, + ttl_seconds: int = 600, + file_refs: tuple[FileRef, ...] = (), + optional_file_refs: tuple[FileRef, ...] = (), +) -> FileGrantMintRequest: + return FileGrantMintRequest( + tenant_id="tenant-1", + app_id="app-1", + subject="subject-1", + is_anonymous=True, + scopes=(FileGrantScope.UPLOAD,), + ttl_seconds=ttl_seconds, + file_refs=file_refs, + optional_file_refs=optional_file_refs, + run_deadline=None, + ) + + +def test_mint_rejects_an_invalid_ttl_before_persistence() -> None: + service, repository, _files, tokens, _remote_files = _service() + + with pytest.raises(GrantTtlTooLongError): + service.mint(_mint_request(ttl_seconds=MAX_SESSION_GRANT_TTL_SECONDS + 1)) + + repository.get_or_create_subject.assert_not_called() + tokens.issue_grant.assert_not_called() + + +def test_mint_orchestrates_identity_resolution_and_token_issuance() -> None: + service, repository, _files, tokens, _remote_files = _service() + + result = service.mint(_mint_request()) + + assert result.grant == "grant" + repository.get_or_create_subject.assert_called_once() + tokens.issue_grant.assert_called_once_with( + context=FileGrantContext("tenant-1", "app-1", "end-user-1"), + scopes=(FileGrantScope.UPLOAD,), + ttl_seconds=600, + ) + + +def test_mint_resolves_required_and_optional_files_in_one_batch() -> None: + service, repository, _files, tokens, _remote_files = _service() + required_ref = FileRef(id="upload-1", kind=FileKind.UPLOAD) + optional_ref = FileRef(id="tool-1", kind=FileKind.TOOL) + required_file = ResolvedFile("upload-1", FileKind.UPLOAD, "report.pdf", 10, "pdf", "application/pdf") + optional_file = ResolvedFile("tool-1", FileKind.TOOL, "chart.png", 20, "png", "image/png") + repository.resolve_owned_files.return_value = [required_file, optional_file] + tokens.issue_content_urls.return_value = ("https://files/tool-1", "http://files/tool-1") + + result = service.mint( + _mint_request( + file_refs=(required_ref,), + optional_file_refs=(optional_ref,), + ) + ) + + repository.resolve_owned_files.assert_called_once_with( + context=FileGrantContext("tenant-1", "app-1", "end-user-1"), + refs=(required_ref, optional_ref), + ) + assert result.files == (required_file,) + assert result.optional_files[0] is not None + assert result.optional_files[0].file == optional_file + + +def test_store_produced_rejects_a_deleted_subject_before_reading_the_file() -> None: + service, repository, files, _tokens, _remote_files = _service() + repository.subject_exists.return_value = False + stream = BytesIO(b"produced content") + + with pytest.raises(EndUserNotFoundError): + service.store_produced( + context=FileGrantContext("tenant-1", "app-1", "deleted-user"), + filename="result.txt", + stream=stream, + mimetype="text/plain", + ) + + assert stream.tell() == 0 + files.store_produced.assert_not_called() + + +def test_store_remote_upload_rejects_a_deleted_subject_before_fetching() -> None: + service, repository, _files, _tokens, remote_files = _service() + repository.subject_exists.return_value = False + + with pytest.raises(EndUserNotFoundError): + service.store_remote_upload( + context=FileGrantContext("tenant-1", "app-1", "deleted-user"), + url="https://example.com/report.pdf", + ) + + remote_files.fetch.assert_not_called() + + +def test_resolve_rejects_a_deleted_subject_before_querying_files() -> None: + service, repository, _files, _tokens, _remote_files = _service() + repository.subject_exists.return_value = False + + with pytest.raises(EndUserNotFoundError): + service.resolve_files( + context=FileGrantContext("tenant-1", "app-1", "deleted-user"), + refs=(), + ) + + repository.resolve_owned_files.assert_not_called() diff --git a/api/tests/unit_tests/services/test_file_service.py b/api/tests/unit_tests/services/test_file_service.py index 49367183d9c..470235cf610 100644 --- a/api/tests/unit_tests/services/test_file_service.py +++ b/api/tests/unit_tests/services/test_file_service.py @@ -258,6 +258,20 @@ class TestFileService: is False ) + def test_file_size_limit(self, config_overrides: Callable[..., None]): + config_overrides( + UPLOAD_IMAGE_FILE_SIZE_LIMIT=10, + UPLOAD_VIDEO_FILE_SIZE_LIMIT=20, + UPLOAD_AUDIO_FILE_SIZE_LIMIT=30, + UPLOAD_FILE_SIZE_LIMIT=5, + ) + + assert FileService.file_size_limit(extension="jpg") == 10 * 1024 * 1024 + assert FileService.file_size_limit(extension="mp4") == 20 * 1024 * 1024 + assert FileService.file_size_limit(extension="mp3") == 30 * 1024 * 1024 + assert FileService.file_size_limit(extension="txt") == 5 * 1024 * 1024 + assert FileService.file_size_limit(extension="txt", default_file_size_limit=7) == 7 * 1024 * 1024 + def test_get_file_base64_success(self, file_service: FileService, db_session: Session): self._persist_upload_file(db_session, key="test_key") diff --git a/api/tests/unit_tests/services/test_notification_service.py b/api/tests/unit_tests/services/test_notification_service.py index 3be7f08a6f7..18b3dfef93f 100644 --- a/api/tests/unit_tests/services/test_notification_service.py +++ b/api/tests/unit_tests/services/test_notification_service.py @@ -1,11 +1,4 @@ -from datetime import datetime -from unittest.mock import Mock - -import pytest - from machinery.context import RequestContext -from services.account_ports import AccountRepository -from services.entities.account_entities import AccountSnapshot from services.entities.notification_entities import ( AccountNotification, AccountNotificationBatch, @@ -39,30 +32,6 @@ class NotificationGatewayStub: self.dismissals.append((notification_id, account_id)) -def _account(language: str | None = "zh-Hans") -> AccountSnapshot: - return AccountSnapshot( - id="account-1", - name="Account", - email="account@example.com", - avatar=None, - is_password_set=False, - interface_language=language, - interface_theme="light", - timezone="UTC", - last_login_at=None, - last_login_ip=None, - status="active", - initialized_at=None, - created_at=datetime(2026, 1, 1), - ) - - -def _accounts(account: AccountSnapshot | None) -> Mock: - accounts = Mock(spec=AccountRepository) - accounts.get.return_value = account - return accounts - - def _notification(contents: dict[str, NotificationContent]) -> AccountNotification: return AccountNotification( notification_id="notification-1", @@ -71,19 +40,18 @@ def _notification(contents: dict[str, NotificationContent]) -> AccountNotificati ) -def test_get_active_localizes_notification_for_account_language() -> None: +def test_get_active_localizes_notification_for_requested_language() -> None: chinese = NotificationContent("zh-Hans", "标题", "副标题", "正文", "zh.png") english = NotificationContent("en-US", "Title", "Subtitle", "Body", "en.png") gateway = NotificationGatewayStub( AccountNotificationBatch(True, (_notification({"zh-Hans": chinese, "en-US": english}),)) ) - service = NotificationService(accounts=_accounts(_account()), notifications=gateway) + service = NotificationService(notifications=gateway) - result = service.get_active(_context()) + result = service.get_active(_context(), "zh-Hans") - assert result == NotificationResult( - should_show=True, - notifications=(NotificationItem("notification-1", "once", "zh-Hans", "标题", "副标题", "正文", "zh.png"),), + assert result.notifications == ( + NotificationItem("notification-1", "once", "zh-Hans", "标题", "副标题", "正文", "zh.png"), ) assert gateway.get_account_ids == ["account-1"] @@ -91,47 +59,48 @@ def test_get_active_localizes_notification_for_account_language() -> None: def test_get_active_falls_back_to_english() -> None: english = NotificationContent("en-US", "Title", "Subtitle", "Body", "en.png") gateway = NotificationGatewayStub(AccountNotificationBatch(True, (_notification({"en-US": english}),))) - service = NotificationService(accounts=_accounts(_account("fr-FR")), notifications=gateway) + service = NotificationService(notifications=gateway) - result = service.get_active(_context()) + result = service.get_active(_context(), "fr-FR") assert result.notifications[0].lang == "en-US" assert result.notifications[0].title == "Title" -def test_get_active_skips_account_query_when_gateway_says_not_to_show() -> None: - accounts = _accounts(None) - service = NotificationService( - accounts=accounts, - notifications=NotificationGatewayStub(AccountNotificationBatch(False, ())), +def test_get_active_falls_back_to_english_for_unsupported_language() -> None: + unsupported = NotificationContent("xx-YY", "Unknown", "Unknown", "Unknown", "unknown.png") + english = NotificationContent("en-US", "Title", "Subtitle", "Body", "en.png") + gateway = NotificationGatewayStub( + AccountNotificationBatch(True, (_notification({"xx-YY": unsupported, "en-US": english}),)) ) + service = NotificationService(notifications=gateway) - result = service.get_active(_context()) + result = service.get_active(_context(), "xx-YY") + + assert result.notifications[0].lang == "en-US" + assert result.notifications[0].title == "Title" + + +def test_get_active_returns_empty_when_gateway_says_not_to_show() -> None: + service = NotificationService(notifications=NotificationGatewayStub(AccountNotificationBatch(False, ()))) + + result = service.get_active(_context(), "zh-Hans") assert result == NotificationResult(False, ()) - accounts.get.assert_not_called() def test_get_active_uses_empty_content_when_notification_has_no_translations() -> None: gateway = NotificationGatewayStub(AccountNotificationBatch(True, (_notification({}),))) - service = NotificationService(accounts=_accounts(_account(None)), notifications=gateway) + service = NotificationService(notifications=gateway) - result = service.get_active(_context()) + result = service.get_active(_context(), "") assert result.notifications == (NotificationItem("notification-1", "once", "en-US", "", "", "", ""),) -def test_get_active_rejects_unknown_admitted_account() -> None: - gateway = NotificationGatewayStub(AccountNotificationBatch(True, (_notification({}),))) - service = NotificationService(accounts=_accounts(None), notifications=gateway) - - with pytest.raises(RuntimeError, match="unknown account"): - service.get_active(_context()) - - def test_dismiss_delegates_identifiers_to_gateway() -> None: gateway = NotificationGatewayStub(AccountNotificationBatch(False, ())) - service = NotificationService(accounts=_accounts(_account()), notifications=gateway) + service = NotificationService(notifications=gateway) service.dismiss(_context(), "notification-1") diff --git a/api/tests/unit_tests/services/test_oauth_device_flow.py b/api/tests/unit_tests/services/test_oauth_device_flow.py deleted file mode 100644 index 35c5a691672..00000000000 --- a/api/tests/unit_tests/services/test_oauth_device_flow.py +++ /dev/null @@ -1,219 +0,0 @@ -from __future__ import annotations - -import uuid -from datetime import UTC, datetime, timedelta -from unittest.mock import MagicMock - -import pytest -from sqlalchemy.orm import Session - -from libs.oauth_bearer import TOKEN_CACHE_KEY_FMT, AuthContext, SubjectType, TokenType -from models.oauth import OAuthAccessToken -from services.oauth_device_flow import ( - list_active_sessions, - revoke_oauth_token, - subject_match_clauses, - token_belongs_to_subject, -) - -ACCOUNT_ID = uuid.UUID("11111111-1111-1111-1111-111111111111") -OTHER_ACCOUNT_ID = uuid.UUID("22222222-2222-2222-2222-222222222222") -TOKEN_ID = uuid.UUID("33333333-3333-3333-3333-333333333333") -OTHER_TOKEN_ID = uuid.UUID("44444444-4444-4444-4444-444444444444") - - -def _token( - *, - token_id: uuid.UUID = TOKEN_ID, - account_id: uuid.UUID | None = ACCOUNT_ID, - subject_email: str = "user@example.com", - subject_issuer: str = "dify:account", - token_hash: str | None = "live-hash", - expires_at: datetime | None = None, - revoked_at: datetime | None = None, - created_at: datetime | None = None, -) -> OAuthAccessToken: - token = OAuthAccessToken( - subject_email=subject_email, - subject_issuer=subject_issuer, - account_id=str(account_id) if account_id is not None else None, - client_id="difyctl", - device_label="test-device", - prefix="dfoa_" if account_id is not None else "dfoe_", - token_hash=token_hash, - expires_at=expires_at or datetime.now(UTC) + timedelta(days=1), - revoked_at=revoked_at, - ) - token.id = str(token_id) - if created_at is not None: - token.created_at = created_at - return token - - -def _account_ctx(*, account_id: uuid.UUID = ACCOUNT_ID) -> AuthContext: - return AuthContext( - subject_type=SubjectType.ACCOUNT, - subject_email="user@example.com", - subject_issuer="dify:account", - account_id=account_id, - client_id="difyctl", - scopes=frozenset({"full"}), - token_id=uuid.uuid4(), - token_type=TokenType.OAUTH_ACCOUNT, - expires_at=None, - token_hash="h1", - verified_tenants={}, - ) - - -def _sso_ctx() -> AuthContext: - return AuthContext( - subject_type=SubjectType.EXTERNAL_SSO, - subject_email="sso@partner.com", - subject_issuer="https://idp.partner.com", - account_id=None, - client_id="difyctl", - scopes=frozenset({"apps:run"}), - token_id=uuid.uuid4(), - token_type=TokenType.OAUTH_EXTERNAL_SSO, - expires_at=None, - token_hash="h1", - verified_tenants={}, - ) - - -# --------------------------------------------------------------------------- -# subject_match_clauses -# --------------------------------------------------------------------------- - - -def test_subject_match_clauses_account_matches_only_account_id(): - clauses = subject_match_clauses(_account_ctx()) - assert len(clauses) == 1 - assert "account_id" in str(clauses[0]) - - -def test_subject_match_clauses_external_sso_requires_null_account_id(): - """External SSO must additionally require ``account_id IS NULL`` so a - same-email account-flow row from a federated tenant cannot be - enumerated/revoked through an SSO bearer. - """ - clauses = subject_match_clauses(_sso_ctx()) - assert len(clauses) == 3 - rendered = " ".join(str(c) for c in clauses) - assert "subject_email" in rendered - assert "subject_issuer" in rendered - assert "account_id IS NULL" in rendered - - -# --------------------------------------------------------------------------- -# revoke_oauth_token -# --------------------------------------------------------------------------- - - -@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) -def test_revoke_oauth_token_invalidates_redis_cache_when_live_hash_seen(sqlite_session: Session): - """Happy path: snapshot finds a live ``token_hash`` → UPDATE runs + - Redis cache entry is DEL'd so the next bearer probe re-reads the now - revoked row from DB. - """ - sqlite_session.add(_token()) - sqlite_session.commit() - - redis = MagicMock() - - revoke_oauth_token(redis, str(TOKEN_ID), session=sqlite_session) - - assert not sqlite_session.in_transaction() - persisted = sqlite_session.get(OAuthAccessToken, str(TOKEN_ID)) - assert persisted is not None - assert persisted.token_hash is None - assert persisted.revoked_at is not None - redis.delete.assert_called_once_with(TOKEN_CACHE_KEY_FMT.format(hash="live-hash")) - - -@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) -def test_revoke_oauth_token_is_idempotent_when_already_revoked(sqlite_session: Session): - """Second call (or race-loser): no live hash → UPDATE still runs (it - is itself idempotent thanks to ``WHERE revoked_at IS NULL``) but the - Redis invalidation is skipped because there's no cache entry to - drop. - """ - revoked_at = datetime.now(UTC) - timedelta(minutes=1) - sqlite_session.add(_token(token_hash=None, revoked_at=revoked_at)) - sqlite_session.commit() - - redis = MagicMock() - - revoke_oauth_token(redis, str(TOKEN_ID), session=sqlite_session) - - assert not sqlite_session.in_transaction() - persisted = sqlite_session.get(OAuthAccessToken, str(TOKEN_ID)) - assert persisted is not None - assert persisted.token_hash is None - assert persisted.revoked_at is not None - redis.delete.assert_not_called() - - -# --------------------------------------------------------------------------- -# list_active_sessions / token_belongs_to_subject -# --------------------------------------------------------------------------- - - -@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) -def test_list_active_sessions_returns_only_live_subject_tokens(sqlite_session: Session): - """Only live, hashed rows for the authenticated subject are returned newest-first.""" - - now = datetime.now(UTC) - active_new = _token(token_id=TOKEN_ID, created_at=now - timedelta(minutes=1)) - active_old = _token(token_id=OTHER_TOKEN_ID, created_at=now - timedelta(minutes=2)) - expired = _token( - token_id=uuid.UUID(int=5), - expires_at=now - timedelta(seconds=1), - created_at=now - timedelta(minutes=3), - ) - revoked = _token( - token_id=uuid.UUID(int=6), - token_hash=None, - revoked_at=now - timedelta(seconds=1), - created_at=now - timedelta(minutes=4), - ) - hashless = _token( - token_id=uuid.UUID(int=7), - token_hash=None, - created_at=now - timedelta(minutes=5), - ) - other_account = _token( - token_id=uuid.UUID(int=8), - account_id=OTHER_ACCOUNT_ID, - created_at=now - timedelta(minutes=6), - ) - external_sso = _token( - token_id=uuid.UUID(int=9), - account_id=None, - subject_email="user@example.com", - subject_issuer="https://idp.example.com", - created_at=now - timedelta(minutes=7), - ) - sqlite_session.add_all([active_new, active_old, expired, revoked, hashless, other_account, external_sso]) - sqlite_session.commit() - - out = list_active_sessions(_account_ctx(), now, session=sqlite_session) - - assert [token.id for token in out] == [str(TOKEN_ID), str(OTHER_TOKEN_ID)] - - -@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) -def test_token_belongs_to_subject_true_when_row_present(sqlite_session: Session): - sqlite_session.add(_token()) - sqlite_session.commit() - - assert token_belongs_to_subject(str(TOKEN_ID), _account_ctx(), session=sqlite_session) is True - - -@pytest.mark.parametrize("sqlite_session", [(OAuthAccessToken,)], indirect=True) -def test_token_belongs_to_subject_false_for_other_account(sqlite_session: Session): - sqlite_session.add(_token(account_id=OTHER_ACCOUNT_ID)) - sqlite_session.commit() - - assert token_belongs_to_subject(str(TOKEN_ID), _account_ctx(), session=sqlite_session) is False diff --git a/api/tests/unit_tests/services/test_recommended_app_query_service.py b/api/tests/unit_tests/services/test_recommended_app_query_service.py index 09d661b6754..3e762b12167 100644 --- a/api/tests/unit_tests/services/test_recommended_app_query_service.py +++ b/api/tests/unit_tests/services/test_recommended_app_query_service.py @@ -2,7 +2,6 @@ from unittest.mock import MagicMock import pytest -from constants.languages import languages from services.recommended_app_query_service import ( RecommendedAppCatalogPage, RecommendedAppDetailRecord, @@ -83,17 +82,14 @@ def test_is_previewable_falls_back_to_catalog(expected: bool) -> None: @pytest.mark.parametrize( - ("requested_language", "interface_language", "expected"), + ("language", "expected"), [ - ("en-US", "fr-FR", "en-US"), - ("invalid", "fr-FR", "fr-FR"), - (None, "custom-language", "custom-language"), - (None, None, languages[0]), + ("fr-FR", "fr-FR"), + ("invalid", "en-US"), ], ) def test_list_recommended_resolves_language( - requested_language: str | None, - interface_language: str | None, + language: str, expected: str, ) -> None: catalog = MagicMock() @@ -101,8 +97,7 @@ def test_list_recommended_resolves_language( service, _ = _service(catalog=catalog) service.list_recommended( - requested_language=requested_language, - interface_language=interface_language, + language=language, ) catalog.list_recommended.assert_called_once_with(expected) @@ -113,7 +108,7 @@ def test_list_recommended_disables_upstream_trial_without_querying_trial_apps() catalog.list_recommended.return_value = _page("app-1") service, trial_apps = _service(catalog=catalog) - result = service.list_recommended(requested_language="en-US", interface_language=None) + result = service.list_recommended(language="en-US") assert result.recommended_apps[0].can_trial is False trial_apps.existing_ids.assert_not_called() @@ -130,7 +125,7 @@ def test_list_recommended_enriches_trial_status_in_one_bulk_query() -> None: trial_enabled=True, ) - result = service.list_recommended(requested_language="en-US", interface_language=None) + result = service.list_recommended(language="en-US") assert [app.can_trial for app in result.recommended_apps] == [True, False] trial_apps.existing_ids.assert_called_once_with(["app-1", "app-2"]) @@ -141,9 +136,9 @@ def test_list_learn_dify_does_not_return_categories() -> None: catalog.list_learn_dify.return_value = _page(categories=("ignored",)) service, _ = _service(catalog=catalog) - result = service.list_learn_dify(requested_language="invalid", interface_language="fr-FR") + result = service.list_learn_dify(language="invalid") - catalog.list_learn_dify.assert_called_once_with("fr-FR") + catalog.list_learn_dify.assert_called_once_with("en-US") assert result.recommended_apps == () assert not hasattr(result, "categories") diff --git a/api/tests/unit_tests/services/test_skill_management_service.py b/api/tests/unit_tests/services/test_skill_management_service.py index 8e9fd12529b..62807577ed0 100644 --- a/api/tests/unit_tests/services/test_skill_management_service.py +++ b/api/tests/unit_tests/services/test_skill_management_service.py @@ -2664,6 +2664,32 @@ def test_import_skill_package_creates_draft_and_rejects_name_conflicts() -> None assert exc_info.value.code == "skill_name_conflict" +def test_import_skill_package_strips_root_alongside_macos_metadata_folder() -> None: + package = io.BytesIO() + with zipfile.ZipFile(package, "w") as archive: + archive.writestr( + "expense-sop/SKILL.md", + "---\nname: expense-sop\ndescription: Expenses\n---\n# Expenses", + ) + archive.writestr("expense-sop/references/policy.md", "Policy") + # macOS Finder "Compress" adds an AppleDouble metadata sibling folder + # for archives whose source files carry extended attributes. + archive.writestr("__MACOSX/expense-sop/._SKILL.md", b"\x00") + + service = SkillManagementService(tool_file_manager=_FakeToolFileManager()) + imported = service.import_skill( + tenant_id=TENANT, + user_id=USER, + payload=SkillImportPayload(content=package.getvalue(), filename="expense-sop.zip"), + ) + + assert imported["name"] == "expense-sop" + imported_paths = [item["path"] for item in imported["files"]] + assert "SKILL.md" in imported_paths + assert "references/policy.md" in imported_paths + assert not any(path.startswith("__MACOSX") for path in imported_paths) + + def test_import_skill_package_rejects_missing_frontmatter_description() -> None: package = io.BytesIO() with zipfile.ZipFile(package, "w") as archive: diff --git a/api/tests/unit_tests/services/test_trigger_subscription_builder_service.py b/api/tests/unit_tests/services/test_trigger_subscription_builder_service.py index 6316de75792..1eb28c3dcd2 100644 --- a/api/tests/unit_tests/services/test_trigger_subscription_builder_service.py +++ b/api/tests/unit_tests/services/test_trigger_subscription_builder_service.py @@ -5,7 +5,7 @@ from unittest.mock import Mock, patch import pytest from core.plugin.entities.plugin_daemon import CredentialType -from core.trigger.entities.entities import SubscriptionBuilder, SubscriptionBuilderUpdater +from core.trigger.entities.entities import Subscription, SubscriptionBuilder, SubscriptionBuilderUpdater from core.trigger.trigger_manager import TriggerManager from models.provider_ids import TriggerProviderID from services.trigger.trigger_subscription_builder_service import TriggerSubscriptionBuilderService @@ -249,3 +249,50 @@ def test_process_validation_endpoint_uses_the_public_capability() -> None: assert str(provider_call["provider_id"]) == str(PROVIDER_ID) controller.dispatch.assert_called_once() append_log.assert_called_once() + + +def test_update_and_build_persists_provider_subscription_expires_at() -> None: + """Authorized create must store the plugin lease, not the builder's -1 placeholder.""" + builder = subscription_builder().model_copy( + update={ + "credential_type": CredentialType.OAUTH2, + "credentials": {"token": "x"}, + "credential_expires_at": 1_787_560_741, + } + ) + assert builder.expires_at == -1 + real_lease = 1_787_560_681 + provider_subscription = Subscription( + expires_at=real_lease, + endpoint="https://dify.example.com/triggers/plugin/builder-1", + parameters={"label_ids": ["INBOX"]}, + properties={"topic_name": "projects/p/topics/t"}, + ) + + with ( + patch.object(TriggerManager, "get_trigger_provider", return_value=Mock()), + patch.object(TriggerSubscriptionBuilderService, "acquire_builder_lock", return_value=nullcontext()), + patch.object(TriggerSubscriptionBuilderService, "get_subscription_builder", return_value=builder), + patch.object(TriggerManager, "subscribe_trigger", return_value=provider_subscription) as subscribe_trigger, + patch( + "services.trigger.trigger_subscription_builder_service.TriggerProviderService.add_trigger_subscription" + ) as add_subscription, + patch("services.trigger.trigger_subscription_builder_service.redis_client.setex"), + patch("services.trigger.trigger_subscription_builder_service.redis_client.delete"), + ): + TriggerSubscriptionBuilderService.update_and_build_builder( + tenant_id="tenant-1", + user_id="user-1", + provider_id=PROVIDER_ID, + subscription_builder_id=builder.id, + subscription_builder_updater=SubscriptionBuilderUpdater(), + ) + + subscribe_trigger.assert_called_once() + add_subscription.assert_called_once() + persisted = add_subscription.call_args.kwargs + assert persisted["expires_at"] == real_lease + assert persisted["expires_at"] != -1 + assert persisted["properties"] == {"topic_name": "projects/p/topics/t"} + assert persisted["credential_expires_at"] == 1_787_560_741 + assert persisted["credentials"] == {"token": "x"} diff --git a/api/tests/unit_tests/services/test_workflow_app_log_query_service.py b/api/tests/unit_tests/services/test_workflow_app_log_query_service.py new file mode 100644 index 00000000000..f5a3c61492e --- /dev/null +++ b/api/tests/unit_tests/services/test_workflow_app_log_query_service.py @@ -0,0 +1,119 @@ +import json +import uuid +from collections.abc import Mapping +from datetime import UTC, datetime +from unittest.mock import MagicMock, patch + +import pytest + +from models.enums import AppTriggerType +from repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepository +from services.workflow_app_log_query_service import ( + WorkflowAppLogItem, + WorkflowAppLogPage, + WorkflowAppLogQueryService, +) + + +def _service_with_metadata( + value: str | Mapping[str, object] | None, + *, + detail: bool = True, +) -> WorkflowAppLogQueryService: + logs = MagicMock() + logs.get_paginated.return_value = WorkflowAppLogPage( + page=1, + limit=20, + total=1, + has_more=False, + data=( + WorkflowAppLogItem( + id="log-1", + workflow_run=None, + details={"trigger_metadata": value} if detail else None, + created_from="web-app", + created_by_role="account", + created_by_account=None, + created_by_end_user=None, + created_at=datetime(2026, 1, 1, tzinfo=UTC), + ), + ), + ) + return WorkflowAppLogQueryService(logs=logs) + + +def test_list_logs_preserves_missing_workflow_run_and_resolves_empty_metadata() -> None: + service = _service_with_metadata(None) + + result = service.list_logs(tenant_id="tenant-1", app_id="app-1", detail=True) + + assert result.data[0].details == {"trigger_metadata": {}} + assert result.data[0].workflow_run is None + + +def test_list_logs_enriches_plugin_icons() -> None: + metadata = { + "type": AppTriggerType.TRIGGER_PLUGIN.value, + "icon_filename": "light.png", + "icon_dark_filename": "dark.png", + } + service = _service_with_metadata(json.dumps(metadata)) + + with patch( + "services.workflow_app_log_query_service.PluginService.get_plugin_icon_url", + side_effect=["https://cdn/light.png", "https://cdn/dark.png"], + ) as get_icon_url: + result = service.list_logs(tenant_id="tenant-1", app_id="app-1", detail=True) + + details = result.data[0].details + assert details is not None + trigger_metadata = details["trigger_metadata"] + assert trigger_metadata["icon"] == "https://cdn/light.png" + assert trigger_metadata["icon_dark"] == "https://cdn/dark.png" + assert get_icon_url.call_count == 2 + + +def test_list_logs_does_not_fetch_icons_for_non_plugin_metadata() -> None: + service = _service_with_metadata(json.dumps({"type": AppTriggerType.TRIGGER_WEBHOOK.value})) + + with patch("services.workflow_app_log_query_service.PluginService.get_plugin_icon_url") as get_icon_url: + result = service.list_logs(tenant_id="tenant-1", app_id="app-1", detail=True) + + assert result.data[0].details == {"trigger_metadata": {"type": AppTriggerType.TRIGGER_WEBHOOK.value}} + get_icon_url.assert_not_called() + + +def test_list_logs_does_not_resolve_metadata_without_detail() -> None: + service = _service_with_metadata("not-json", detail=False) + + result = service.list_logs(tenant_id="tenant-1", app_id="app-1") + + assert result.data[0].details is None + + +@pytest.mark.parametrize( + ("value", "expected"), + [ + (None, None), + ("", None), + ('{"k":"v"}', {"k": "v"}), + ("not-json", None), + ({"raw": True}, {"raw": True}), + ], +) +def test_safe_json_loads(value: object, expected: object) -> None: + assert WorkflowAppLogQueryService._safe_json_loads(value) == expected + + +def test_safe_parse_uuid_rejects_short_and_invalid_values() -> None: + assert WorkflowAppLogQueryRepository._safe_parse_uuid("short") is None + assert WorkflowAppLogQueryRepository._safe_parse_uuid("x" * 40) is None + + +def test_safe_parse_uuid_accepts_uuid() -> None: + raw = str(uuid.uuid4()) + + result = WorkflowAppLogQueryRepository._safe_parse_uuid(raw) + + assert result is not None + assert str(result) == raw diff --git a/api/tests/unit_tests/services/test_workflow_app_service_metadata.py b/api/tests/unit_tests/services/test_workflow_app_service_metadata.py deleted file mode 100644 index c51de1abaa9..00000000000 --- a/api/tests/unit_tests/services/test_workflow_app_service_metadata.py +++ /dev/null @@ -1,111 +0,0 @@ -"""Unit tests for workflow app log views and trigger metadata helpers.""" - -import json -import uuid -from unittest.mock import MagicMock, patch - -import pytest -from sqlalchemy.orm import Session - -from models.account import Account -from models.enums import AppTriggerType, CreatorUserRole -from models.workflow import WorkflowAppLog, WorkflowAppLogCreatedFrom -from services.workflow_app_service import LogView, WorkflowAppService - - -class TestLogView: - def test_details_and_proxy_attributes(self) -> None: - log = WorkflowAppLog( - tenant_id="tenant-1", - app_id="app-1", - workflow_id="workflow-1", - workflow_run_id="run-1", - created_from=WorkflowAppLogCreatedFrom.WEB_APP, - created_by_role=CreatorUserRole.ACCOUNT, - created_by="account-1", - ) - log.id = "log-1" - - view = LogView(log=log, details={"trigger_metadata": {"type": "plugin"}}, session=MagicMock()) - - assert view.details == {"trigger_metadata": {"type": "plugin"}} - assert view.id == "log-1" - - def test_account_accessors_resolve_via_wrapped_session(self, sqlite_session: Session) -> None: - account = Account(name="Test Account", email="test@example.com") - sqlite_session.add(account) - sqlite_session.flush() - log = WorkflowAppLog( - tenant_id="tenant-1", - app_id="app-1", - workflow_id="workflow-1", - workflow_run_id="run-1", - created_from=WorkflowAppLogCreatedFrom.WEB_APP, - created_by_role=CreatorUserRole.ACCOUNT, - created_by=account.id, - ) - - view = LogView(log=log, details=None, session=sqlite_session) - - resolved = view.created_by_account - assert resolved is not None - assert resolved.id == account.id - assert view.created_by_end_user is None - - -class TestHandleTriggerMetadata: - def test_returns_empty_dict_when_metadata_missing(self) -> None: - assert WorkflowAppService().handle_trigger_metadata("tenant-1", None) == {} - - def test_enriches_plugin_icons(self) -> None: - metadata = { - "type": AppTriggerType.TRIGGER_PLUGIN.value, - "icon_filename": "light.png", - "icon_dark_filename": "dark.png", - } - with patch( - "services.workflow_app_service.PluginService.get_plugin_icon_url", - side_effect=["https://cdn/light.png", "https://cdn/dark.png"], - ) as mock_icon: - result = WorkflowAppService().handle_trigger_metadata("tenant-1", json.dumps(metadata)) - - assert result["icon"] == "https://cdn/light.png" - assert result["icon_dark"] == "https://cdn/dark.png" - assert mock_icon.call_count == 2 - - def test_non_plugin_metadata_without_icon_lookup(self) -> None: - metadata = {"type": AppTriggerType.TRIGGER_WEBHOOK.value} - with patch("services.workflow_app_service.PluginService.get_plugin_icon_url") as mock_icon: - result = WorkflowAppService().handle_trigger_metadata("tenant-1", json.dumps(metadata)) - - assert result["type"] == AppTriggerType.TRIGGER_WEBHOOK.value - mock_icon.assert_not_called() - - -class TestSafeJsonLoads: - @pytest.mark.parametrize( - ("value", "expected"), - [ - (None, None), - ("", None), - ('{"k":"v"}', {"k": "v"}), - ("not-json", None), - ({"raw": True}, {"raw": True}), - ], - ) - def test_handles_various_inputs(self, value, expected) -> None: - assert WorkflowAppService._safe_json_loads(value) == expected - - -class TestSafeParseUuid: - def test_returns_none_for_short_or_invalid_values(self) -> None: - assert WorkflowAppService._safe_parse_uuid("short") is None - assert WorkflowAppService._safe_parse_uuid("x" * 40) is None - - def test_returns_uuid_for_valid_string(self) -> None: - raw = str(uuid.uuid4()) - - result = WorkflowAppService._safe_parse_uuid(raw) - - assert result is not None - assert str(result) == raw diff --git a/api/tests/unit_tests/services/test_workflow_run_service.py b/api/tests/unit_tests/services/test_workflow_run_service.py index c1aa0fd9cea..b71829dcf7c 100644 --- a/api/tests/unit_tests/services/test_workflow_run_service.py +++ b/api/tests/unit_tests/services/test_workflow_run_service.py @@ -1,62 +1,38 @@ -"""Workflow-run service tests with real SQLite-bound session factories.""" +"""Unit tests for the Console workflow-run application service.""" -from decimal import Decimal from types import SimpleNamespace -from typing import Any from unittest.mock import MagicMock import pytest -from sqlalchemy import Engine, event -from sqlalchemy.orm import Session, sessionmaker from graphon.enums import WorkflowExecutionStatus -from models import Account, App, EndUser, Message, WorkflowRun, WorkflowRunTriggeredFrom, WorkflowType -from models.account import Tenant -from models.enums import ConversationFromSource, CreatorUserRole, EndUserType -from models.model import AppMode +from machinery.context import RequestContext +from models import WorkflowRun, WorkflowRunTriggeredFrom, WorkflowType +from models.enums import CreatorUserRole +from repositories.sqlalchemy_api_workflow_run_repository import WorkflowRunMessageRef from services import workflow_run_service as service_module from services.workflow_run_service import WorkflowRunService @pytest.fixture -def repository_factory_mocks(monkeypatch: pytest.MonkeyPatch) -> tuple[MagicMock, MagicMock, Any]: - node_repo = MagicMock() - workflow_run_repo = MagicMock() - factory = SimpleNamespace( - create_api_workflow_node_execution_repository=MagicMock(return_value=node_repo), - create_api_workflow_run_repository=MagicMock(return_value=workflow_run_repo), - ) - monkeypatch.setattr(service_module, "DifyAPIRepositoryFactory", factory) - return node_repo, workflow_run_repo, factory +def service_dependencies() -> tuple[MagicMock, MagicMock]: + return MagicMock(), MagicMock() -def _app_model(*, app_id: str = "app-1", tenant_id: str = "tenant-1") -> App: - return App( - id=app_id, - tenant_id=tenant_id, - name="Workflow App", - mode=AppMode.ADVANCED_CHAT, - enable_site=False, - enable_api=False, +def _service(dependencies: tuple[MagicMock, MagicMock]) -> WorkflowRunService: + node_executions, workflow_runs = dependencies + return WorkflowRunService( + workflow_runs=workflow_runs, + node_executions=node_executions, ) -def _account(*, account_id: str = "account-1", current_tenant_id: str | None = "tenant-1") -> Account: - account = Account(name="Workflow User", email=f"{account_id}@example.com") - account.id = account_id - if current_tenant_id is not None: - tenant = Tenant(name="Workflow Tenant") - tenant.id = current_tenant_id - account._current_tenant = tenant - return account - - -def _end_user(*, end_user_id: str = "end-user-1", tenant_id: str = "tenant-1") -> EndUser: - return EndUser( - id=end_user_id, - tenant_id=tenant_id, - type=EndUserType.SERVICE_API, - session_id=f"session-{end_user_id}", +def _request_context(*, workspace_id: str = "tenant-1") -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + active_workspace_id=workspace_id, ) @@ -79,87 +55,37 @@ def _workflow_run( ) -def _message(*, message_id: str, workflow_run_id: str, conversation_id: str) -> Message: - message = Message( - app_id="app-1", - conversation_id=conversation_id, - query="query", - message={"role": "user", "content": "query"}, - answer="answer", - message_unit_price=Decimal("0.0001"), - answer_unit_price=Decimal("0.0001"), - currency="USD", - from_source=ConversationFromSource.API, - ) - message.id = message_id - message._inputs = {} - message.workflow_run_id = workflow_run_id - return message +def test_init_keeps_injected_dependencies( + service_dependencies: tuple[MagicMock, MagicMock], +) -> None: + node_executions, workflow_runs = service_dependencies + service = _service(service_dependencies) -class TestWorkflowRunServiceInitialization: - def test___init___should_create_sessionmaker_from_db_engine_when_session_factory_missing( - self, - monkeypatch: pytest.MonkeyPatch, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - sqlite_engine: Engine, - ) -> None: - monkeypatch.setattr(service_module, "db", SimpleNamespace(engine=sqlite_engine)) - - service = WorkflowRunService() - - assert isinstance(service._session_factory, sessionmaker) - assert service._session_factory.kw["bind"] is sqlite_engine - assert service._session_factory.kw["expire_on_commit"] is False - - def test___init___should_create_sessionmaker_when_engine_is_provided( - self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - sqlite_engine: Engine, - ) -> None: - service = WorkflowRunService(session_factory=sqlite_engine) - - assert isinstance(service._session_factory, sessionmaker) - assert service._session_factory.kw["bind"] is sqlite_engine - assert service._session_factory.kw["expire_on_commit"] is False - - def test___init___should_keep_provided_sessionmaker_and_create_repositories( - self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - sqlite_session_factory: sessionmaker[Session], - ) -> None: - node_repo, workflow_run_repo, factory = repository_factory_mocks - - service = WorkflowRunService(session_factory=sqlite_session_factory) - - assert service._session_factory is sqlite_session_factory - assert service._node_execution_service_repo is node_repo - assert service._workflow_run_repo is workflow_run_repo - factory.create_api_workflow_node_execution_repository.assert_called_once_with(sqlite_session_factory) - factory.create_api_workflow_run_repository.assert_called_once_with(sqlite_session_factory) + assert service._workflow_runs is workflow_runs + assert service._node_executions is node_executions class TestWorkflowRunServiceQueries: def test_get_paginate_workflow_runs_should_forward_filters_and_parse_limit( self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - sqlite_session_factory: sessionmaker[Session], + service_dependencies: tuple[MagicMock, MagicMock], ) -> None: - _, workflow_run_repo, _ = repository_factory_mocks - service = WorkflowRunService(session_factory=sqlite_session_factory) - app_model = _app_model(tenant_id="tenant-1", app_id="app-1") + _, workflow_runs = service_dependencies + service = _service(service_dependencies) expected = MagicMock(name="pagination") - workflow_run_repo.get_paginated_workflow_runs.return_value = expected + workflow_runs.get_paginated_workflow_runs.return_value = expected args = {"limit": "7", "last_id": "last-1", "status": "succeeded"} result = service.get_paginate_workflow_runs( - app_model=app_model, + _request_context(workspace_id="tenant-1"), + app_id="app-1", args=args, triggered_from=WorkflowRunTriggeredFrom.APP_RUN, ) assert result is expected - workflow_run_repo.get_paginated_workflow_runs.assert_called_once_with( + workflow_runs.get_paginated_workflow_runs.assert_called_once_with( tenant_id="tenant-1", app_id="app-1", triggered_from=WorkflowRunTriggeredFrom.APP_RUN, @@ -168,25 +94,26 @@ class TestWorkflowRunServiceQueries: status="succeeded", ) - @pytest.mark.parametrize("sqlite_session", [(Message,)], indirect=True) def test_get_paginate_advanced_chat_workflow_runs_should_attach_message_fields_when_message_exists( self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], + service_dependencies: tuple[MagicMock, MagicMock], monkeypatch: pytest.MonkeyPatch, - sqlite_session_factory: sessionmaker[Session], - sqlite_session: Session, ) -> None: - service = WorkflowRunService(session_factory=sqlite_session_factory) - app_model = _app_model(tenant_id="tenant-1", app_id="app-1") + _, workflow_runs = service_dependencies + service = _service(service_dependencies) run_with_message = _workflow_run(status=WorkflowExecutionStatus.RUNNING) run_without_message = _workflow_run(run_id="run-2") pagination = SimpleNamespace(data=[run_with_message, run_without_message]) monkeypatch.setattr(service, "get_paginate_workflow_runs", MagicMock(return_value=pagination)) + workflow_runs.get_message_refs.return_value = { + "run-1": WorkflowRunMessageRef(message_id="msg-1", conversation_id="conv-1") + } - sqlite_session.add(_message(message_id="msg-1", conversation_id="conv-1", workflow_run_id="run-1")) - sqlite_session.commit() - - result = service.get_paginate_advanced_chat_workflow_runs(app_model=app_model, args={"limit": "2"}) + result = service.get_paginate_advanced_chat_workflow_runs( + _request_context(), + app_id="app-1", + args={"limit": "2"}, + ) assert result is pagination assert len(result.data) == 2 @@ -195,56 +122,24 @@ class TestWorkflowRunServiceQueries: assert result.data[0].status == "running" assert not hasattr(result.data[1], "message_id") assert result.data[1].id == "run-2" - - @pytest.mark.parametrize("sqlite_session", [(Message,)], indirect=True) - def test_get_paginate_advanced_chat_workflow_runs_batch_loads_messages_without_n_plus_one( - self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - monkeypatch: pytest.MonkeyPatch, - sqlite_session_factory: sessionmaker[Session], - sqlite_session: Session, - ) -> None: - """Messages must load with a constant query count regardless of run count. - - Previously the deprecated WorkflowRun.message property issued one query per - run (N+1); they are now batch-loaded in a single query. - """ - service = WorkflowRunService(session_factory=sqlite_session_factory) - app_model = _app_model(tenant_id="tenant-1", app_id="app-1") - runs = [_workflow_run(run_id=f"run-{i}") for i in range(5)] - pagination = SimpleNamespace(data=runs) - monkeypatch.setattr(service, "get_paginate_workflow_runs", MagicMock(return_value=pagination)) - - message_query_count = 0 - - def count_message_query(*_args: object) -> None: - nonlocal message_query_count - message_query_count += 1 - - engine = sqlite_session.get_bind() - event.listen(engine, "before_cursor_execute", count_message_query) - try: - service.get_paginate_advanced_chat_workflow_runs(app_model=app_model, args={}) - finally: - event.remove(engine, "before_cursor_execute", count_message_query) - assert all(not hasattr(run, "message_id") for run in runs) - assert message_query_count == 1 + workflow_runs.get_message_refs.assert_called_once_with( + app_id="app-1", + workflow_run_ids=["run-1", "run-2"], + ) def test_get_workflow_run_should_delegate_to_repository_by_tenant_and_app( self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - sqlite_session_factory: sessionmaker[Session], + service_dependencies: tuple[MagicMock, MagicMock], ) -> None: - _, workflow_run_repo, _ = repository_factory_mocks - service = WorkflowRunService(session_factory=sqlite_session_factory) - app_model = _app_model(tenant_id="tenant-1", app_id="app-1") + _, workflow_runs = service_dependencies + service = _service(service_dependencies) expected = _workflow_run() - workflow_run_repo.get_workflow_run_by_id.return_value = expected + workflow_runs.get_workflow_run_by_id.return_value = expected - result = service.get_workflow_run(app_model=app_model, run_id="run-1") + result = service.get_workflow_run(_request_context(), app_id="app-1", run_id="run-1") assert result is expected - workflow_run_repo.get_workflow_run_by_id.assert_called_once_with( + workflow_runs.get_workflow_run_by_id.assert_called_once_with( tenant_id="tenant-1", app_id="app-1", run_id="run-1", @@ -252,24 +147,23 @@ class TestWorkflowRunServiceQueries: def test_get_workflow_runs_count_should_forward_optional_filters( self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - sqlite_session_factory: sessionmaker[Session], + service_dependencies: tuple[MagicMock, MagicMock], ) -> None: - _, workflow_run_repo, _ = repository_factory_mocks - service = WorkflowRunService(session_factory=sqlite_session_factory) - app_model = _app_model(tenant_id="tenant-1", app_id="app-1") + _, workflow_runs = service_dependencies + service = _service(service_dependencies) expected = {"total": 3, "succeeded": 2} - workflow_run_repo.get_workflow_runs_count.return_value = expected + workflow_runs.get_workflow_runs_count.return_value = expected result = service.get_workflow_runs_count( - app_model=app_model, + _request_context(), + app_id="app-1", status="succeeded", time_range="7d", triggered_from=WorkflowRunTriggeredFrom.APP_RUN, ) assert result == expected - workflow_run_repo.get_workflow_runs_count.assert_called_once_with( + workflow_runs.get_workflow_runs_count.assert_called_once_with( tenant_id="tenant-1", app_id="app-1", triggered_from=WorkflowRunTriggeredFrom.APP_RUN, @@ -279,83 +173,44 @@ class TestWorkflowRunServiceQueries: def test_get_workflow_run_node_executions_should_return_empty_list_when_run_not_found( self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], + service_dependencies: tuple[MagicMock, MagicMock], monkeypatch: pytest.MonkeyPatch, - sqlite_session_factory: sessionmaker[Session], ) -> None: - service = WorkflowRunService(session_factory=sqlite_session_factory) + service = _service(service_dependencies) monkeypatch.setattr(service, "get_workflow_run", MagicMock(return_value=None)) - app_model = _app_model(app_id="app-1") - user = _account(current_tenant_id="tenant-1") - result = service.get_workflow_run_node_executions(app_model=app_model, run_id="run-1", user=user) + result = service.get_workflow_run_node_executions( + _request_context(), + app_id="app-1", + run_id="run-1", + ) assert result == [] - def test_get_workflow_run_node_executions_should_use_end_user_tenant_id( + def test_get_workflow_run_node_executions_should_use_request_workspace( self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], + service_dependencies: tuple[MagicMock, MagicMock], monkeypatch: pytest.MonkeyPatch, - sqlite_session_factory: sessionmaker[Session], ) -> None: - node_repo, _, _ = repository_factory_mocks - service = WorkflowRunService(session_factory=sqlite_session_factory) + node_executions, _ = service_dependencies + service = _service(service_dependencies) monkeypatch.setattr(service, "get_workflow_run", MagicMock(return_value=_workflow_run())) - user = _end_user(tenant_id="tenant-end-user") - app_model = _app_model(app_id="app-1") - expected_executions = [SimpleNamespace(id="exec-1")] - expected_traces = [SimpleNamespace(id="exec-1:retry:1")] - node_repo.get_executions_by_workflow_run.return_value = expected_executions - mock_assemble = MagicMock(return_value=expected_traces) - monkeypatch.setattr(service_module, "assemble_workflow_node_execution_traces", mock_assemble) - - result = service.get_workflow_run_node_executions(app_model=app_model, run_id="run-1", user=user) - - assert result == expected_traces - node_repo.get_executions_by_workflow_run.assert_called_once_with( - tenant_id="tenant-end-user", - app_id="app-1", - workflow_run_id="run-1", - ) - mock_assemble.assert_called_once_with(expected_executions, node_repo) - - def test_get_workflow_run_node_executions_should_use_account_current_tenant_id( - self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - monkeypatch: pytest.MonkeyPatch, - sqlite_session_factory: sessionmaker[Session], - ) -> None: - node_repo, _, _ = repository_factory_mocks - service = WorkflowRunService(session_factory=sqlite_session_factory) - monkeypatch.setattr(service, "get_workflow_run", MagicMock(return_value=_workflow_run())) - app_model = _app_model(app_id="app-1") - user = _account(current_tenant_id="tenant-account") expected_executions = [SimpleNamespace(id="exec-1"), SimpleNamespace(id="exec-2")] expected_traces = [SimpleNamespace(id="exec-1:retry:1"), SimpleNamespace(id="exec-1")] - node_repo.get_executions_by_workflow_run.return_value = expected_executions + node_executions.get_executions_by_workflow_run.return_value = expected_executions mock_assemble = MagicMock(return_value=expected_traces) monkeypatch.setattr(service_module, "assemble_workflow_node_execution_traces", mock_assemble) - result = service.get_workflow_run_node_executions(app_model=app_model, run_id="run-1", user=user) + result = service.get_workflow_run_node_executions( + _request_context(workspace_id="tenant-context"), + app_id="app-1", + run_id="run-1", + ) assert result == expected_traces - node_repo.get_executions_by_workflow_run.assert_called_once_with( - tenant_id="tenant-account", + node_executions.get_executions_by_workflow_run.assert_called_once_with( + tenant_id="tenant-context", app_id="app-1", workflow_run_id="run-1", ) - mock_assemble.assert_called_once_with(expected_executions, node_repo) - - def test_get_workflow_run_node_executions_should_raise_when_resolved_tenant_id_is_none( - self, - repository_factory_mocks: tuple[MagicMock, MagicMock, Any], - monkeypatch: pytest.MonkeyPatch, - sqlite_session_factory: sessionmaker[Session], - ) -> None: - service = WorkflowRunService(session_factory=sqlite_session_factory) - monkeypatch.setattr(service, "get_workflow_run", MagicMock(return_value=_workflow_run())) - app_model = _app_model(app_id="app-1") - user = _account(current_tenant_id=None) - - with pytest.raises(ValueError, match="tenant_id cannot be None"): - service.get_workflow_run_node_executions(app_model=app_model, run_id="run-1", user=user) + mock_assemble.assert_called_once_with(expected_executions, node_executions) diff --git a/api/tests/unit_tests/services/test_workflow_run_service_pause.py b/api/tests/unit_tests/services/test_workflow_run_service_pause.py index 5b24cfd8a66..3552cbd7c9d 100644 --- a/api/tests/unit_tests/services/test_workflow_run_service_pause.py +++ b/api/tests/unit_tests/services/test_workflow_run_service_pause.py @@ -1,57 +1,112 @@ -"""Tests for the session lifecycle owned by ``WorkflowRunService``.""" +"""Tests for Console workflow pause details.""" -from unittest.mock import create_autospec, patch +from datetime import datetime +from unittest.mock import MagicMock import pytest -from sqlalchemy import Engine, text -from sqlalchemy.orm import Session, sessionmaker -from repositories.api_workflow_run_repository import APIWorkflowRunRepository -from services.workflow_run_service import WorkflowRunService +from core.workflow.nodes.human_input.pause_reason import HumanInputRequired +from graphon.entities.pause_reason import SchedulingPause +from graphon.enums import WorkflowExecutionStatus +from machinery.context import RequestContext +from repositories.sqlalchemy_api_workflow_run_repository import WorkflowRunPauseRecord +from services.workflow_run_service import ( + WorkflowRunPauseDetails, + WorkflowRunPausedNode, + WorkflowRunService, +) @pytest.fixture -def sqlite_session_factory(sqlite_engine: Engine) -> sessionmaker[Session]: - """Return a real factory whose sessions are bound to the isolated SQLite engine.""" - return sessionmaker(bind=sqlite_engine, expire_on_commit=False) +def workflow_runs() -> MagicMock: + return MagicMock() -@pytest.fixture -def workflow_run_repository(): - """Keep the repository boundary mocked while exercising real session construction.""" - return create_autospec(APIWorkflowRunRepository) +def _service(workflow_runs: MagicMock) -> WorkflowRunService: + return WorkflowRunService( + workflow_runs=workflow_runs, + node_executions=MagicMock(), + ) -def test_init_with_session_factory( - sqlite_session_factory: sessionmaker[Session], workflow_run_repository: APIWorkflowRunRepository -) -> None: - with patch("services.workflow_run_service.DifyAPIRepositoryFactory", autospec=True) as repository_factory: - repository_factory.create_api_workflow_run_repository.return_value = workflow_run_repository - - service = WorkflowRunService(sqlite_session_factory) - - assert service._session_factory is sqlite_session_factory - repository_factory.create_api_workflow_run_repository.assert_called_once_with(sqlite_session_factory) - with service._session_factory() as session: - assert session.scalar(text("SELECT 1")) == 1 +def _request_context(*, workspace_id: str = "tenant-1") -> RequestContext: + return RequestContext( + request_id="request-1", + trace_id="trace-1", + account_id="account-1", + active_workspace_id=workspace_id, + ) -def test_init_with_engine_creates_bound_session_factory( - sqlite_engine: Engine, workflow_run_repository: APIWorkflowRunRepository -) -> None: - with patch("services.workflow_run_service.DifyAPIRepositoryFactory", autospec=True) as repository_factory: - repository_factory.create_api_workflow_run_repository.return_value = workflow_run_repository +def test_get_pause_details_returns_none_when_run_is_not_found(workflow_runs: MagicMock) -> None: + workflow_runs.get_pause_record.return_value = None - service = WorkflowRunService(sqlite_engine) + result = _service(workflow_runs).get_pause_details(_request_context(), workflow_run_id="run-1") - assert service._session_factory.kw["bind"] is sqlite_engine - assert service._session_factory.kw["expire_on_commit"] is False - repository_factory.create_api_workflow_run_repository.assert_called_once_with(service._session_factory) - with service._session_factory() as session: - assert session.scalar(text("SELECT 1")) == 1 + assert result is None + workflow_runs.get_pause_record.assert_called_once_with( + workspace_id="tenant-1", + workflow_run_id="run-1", + ) -def test_init_with_default_repository_dependencies(sqlite_session_factory: sessionmaker[Session]) -> None: - service = WorkflowRunService(sqlite_session_factory) +def test_get_pause_details_returns_empty_details_for_non_paused_run(workflow_runs: MagicMock) -> None: + workflow_runs.get_pause_record.return_value = WorkflowRunPauseRecord( + status=WorkflowExecutionStatus.SUCCEEDED, + paused_at=None, + reasons=(), + form_tokens={}, + ) - assert service._session_factory is sqlite_session_factory + result = _service(workflow_runs).get_pause_details(_request_context(), workflow_run_id="run-1") + + assert result == WorkflowRunPauseDetails(paused_at=None, paused_nodes=()) + + +def test_get_pause_details_maps_human_input_and_token(workflow_runs: MagicMock) -> None: + reason = HumanInputRequired( + form_id="form-1", + form_content="Approve?", + node_id="node-1", + node_title="Approval", + ) + paused_at = datetime(2026, 1, 2, 3, 4, 5) + workflow_runs.get_pause_record.return_value = WorkflowRunPauseRecord( + status=WorkflowExecutionStatus.PAUSED, + paused_at=paused_at, + reasons=(reason,), + form_tokens={"form-1": "form-token"}, + ) + + result = _service(workflow_runs).get_pause_details( + _request_context(workspace_id="tenant-context"), + workflow_run_id="run-1", + ) + + assert result == WorkflowRunPauseDetails( + paused_at=paused_at, + paused_nodes=( + WorkflowRunPausedNode( + node_id="node-1", + node_title="Approval", + form_id="form-1", + form_token="form-token", + ), + ), + ) + workflow_runs.get_pause_record.assert_called_once_with( + workspace_id="tenant-context", + workflow_run_id="run-1", + ) + + +def test_get_pause_details_rejects_unsupported_pause_reason(workflow_runs: MagicMock) -> None: + workflow_runs.get_pause_record.return_value = WorkflowRunPauseRecord( + status=WorkflowExecutionStatus.PAUSED, + paused_at=None, + reasons=(SchedulingPause(message="Waiting for external input"),), + form_tokens={}, + ) + + with pytest.raises(NotImplementedError, match="Pause details do not support SchedulingPause"): + _service(workflow_runs).get_pause_details(_request_context(), workflow_run_id="run-1") diff --git a/api/uv.lock b/api/uv.lock index 5575c5fdc2c..ffb54ba5bc8 100644 --- a/api/uv.lock +++ b/api/uv.lock @@ -1330,8 +1330,8 @@ provides-extras = ["server"] [package.metadata.requires-dev] dev = [ - { name = "basedpyright", specifier = ">=1.39.3" }, { name = "coverage", extras = ["toml"], specifier = ">=7.10.7" }, + { name = "pyrefly", specifier = ">=1.2.0" }, { name = "pytest", specifier = ">=9.0.3" }, { name = "pytest-examples", specifier = ">=0.0.18" }, { name = "pytest-mock", specifier = ">=3.14.0" }, diff --git a/dify-agent/Makefile b/dify-agent/Makefile index 0decb13ae3d..0755fd58ba5 100644 --- a/dify-agent/Makefile +++ b/dify-agent/Makefile @@ -10,7 +10,7 @@ help: @echo " make dev - Run the Dify Agent server with reload" @echo " make check - Run Ruff for dify-agent" @echo " make fix - Format and fix Ruff issues" - @echo " make typecheck - Run basedpyright for src, examples, and tests" + @echo " make typecheck - Run pyrefly for src and examples" @echo " make test - Run local tests and docs/example tests" @echo " make update-examples - Rewrite docs example outputs when needed" @echo " make docs - Build MkDocs documentation" @@ -30,7 +30,7 @@ fix: @uv --directory "$(PROJECT_DIR)" run --project . python -m ruff check --fix . typecheck: - @uv --directory "$(PROJECT_DIR)" run --project . basedpyright --level error src examples tests + @uv --directory "$(PROJECT_DIR)" run --project . --group dev --extra server pyrefly check test: @uv --directory "$(PROJECT_DIR)" run --project . --extra server python -m pytest tests diff --git a/dify-agent/pyproject.toml b/dify-agent/pyproject.toml index 58dcc648151..064dfe5dee6 100644 --- a/dify-agent/pyproject.toml +++ b/dify-agent/pyproject.toml @@ -38,12 +38,12 @@ include = ["agenton*", "agenton_collections*", "dify_agent*", "shellctl*"] [tool.setuptools.package-data] "dify_agent.layers" = ["_agent_cli_help.json"] -[tool.pyright] -include = ["src", "examples", "tests"] -venvPath = "." -venv = ".venv" -pythonVersion = "3.12" -extraPaths = ["src", "examples/agenton", "examples/dify_agent"] +[tool.pyrefly] +project-includes = ["src", "examples"] +project-excludes = [".venv", "tests/"] +search-path = ["src", "examples/agenton", "examples/dify_agent"] +python-platform = "linux" +python-version = "3.12.0" [tool.pytest.ini_options] # Several test modules share a basename across directories (test_layer.py, @@ -61,8 +61,8 @@ include = ["src/**/*.py", "examples/**/*.py", "tests/**/*.py", "docs/**/*.py"] [dependency-groups] dev = [ - "basedpyright>=1.39.3", "coverage[toml]>=7.10.7", + "pyrefly>=1.2.0", "pytest>=9.0.3", "pytest-examples>=0.0.18", "pytest-mock>=3.14.0", diff --git a/dify-agent/src/agenton/compositor/core.py b/dify-agent/src/agenton/compositor/core.py index b948ace52e0..eb3763b2f76 100644 --- a/dify-agent/src/agenton/compositor/core.py +++ b/dify-agent/src/agenton/compositor/core.py @@ -18,7 +18,7 @@ from collections import OrderedDict from collections.abc import AsyncGenerator, Mapping, Sequence from contextlib import asynccontextmanager from dataclasses import dataclass -from typing import Any, Generic, cast +from typing import Any, Generic, Self, cast from pydantic import JsonValue @@ -131,7 +131,7 @@ class Compositor(Generic[PromptT, ToolT, LayerPromptT, LayerToolT, UserPromptT, prompt_transformer: CompositorTransformer[LayerPromptT, PromptT] | None = None, user_prompt_transformer: CompositorTransformer[LayerUserPromptT, UserPromptT] | None = None, tool_transformer: CompositorTransformer[LayerToolT, ToolT] | None = None, - ) -> "Compositor[PromptT, ToolT, LayerPromptT, LayerToolT, UserPromptT, LayerUserPromptT]": + ) -> Self: """Create a reusable compositor plan from serializable graph config. ``providers`` resolve graph node ``type`` ids. ``node_providers`` are diff --git a/dify-agent/src/agenton/layers/base.py b/dify-agent/src/agenton/layers/base.py index 73f9f490143..d8f8035c1cd 100644 --- a/dify-agent/src/agenton/layers/base.py +++ b/dify-agent/src/agenton/layers/base.py @@ -226,7 +226,7 @@ class Layer( if deps_type is None and is_generic_template: return if deps_type is not None: - cls.deps_type = deps_type # pyright: ignore[reportAttributeAccessIssue] + cls.deps_type = cast(type[_DepsT], deps_type) if deps_type is None: raise TypeError(f"{cls.__name__} must define deps_type or inherit from Layer[DepsT].") if not isinstance(deps_type, type) or not issubclass(deps_type, LayerDeps): diff --git a/dify-agent/src/dify_agent/agent_stub/protocol/agent_stub.py b/dify-agent/src/dify_agent/agent_stub/protocol/agent_stub.py index 8a65c19d94d..ffd4aca3782 100644 --- a/dify-agent/src/dify_agent/agent_stub/protocol/agent_stub.py +++ b/dify-agent/src/dify_agent/agent_stub/protocol/agent_stub.py @@ -18,7 +18,7 @@ from urllib.parse import urlsplit, urlunsplit from pydantic import AliasChoices, BaseModel, ConfigDict, Field, JsonValue, model_validator -AGENT_STUB_PROTOCOL_VERSION: Final[int] = 1 +AGENT_STUB_PROTOCOL_VERSION: Final[Literal[1]] = 1 AGENT_STUB_API_BASE_URL_ENV_VAR: Final[str] = "DIFY_AGENT_STUB_API_BASE_URL" AGENT_STUB_AUTH_JWE_ENV_VAR: Final[str] = "DIFY_AGENT_STUB_AUTH_JWE" diff --git a/dify-agent/src/dify_agent/layers/ask_human/configs.py b/dify-agent/src/dify_agent/layers/ask_human/configs.py index 432d0aad56c..87fd1a4af0b 100644 --- a/dify-agent/src/dify_agent/layers/ask_human/configs.py +++ b/dify-agent/src/dify_agent/layers/ask_human/configs.py @@ -40,6 +40,10 @@ _HARD_MAX_ACTION_LABEL_CHARS = 120 _FILE_FIELD_TYPES: Final[frozenset[AskHumanFieldType]] = frozenset({"file", "file-list"}) +def _default_allowed_field_types() -> list[AskHumanFieldType]: + return ["paragraph", "select"] + + class DifyAskHumanLayerConfig(LayerConfig): """Public config for the optional ask-human deferred tool layer. @@ -58,7 +62,7 @@ class DifyAskHumanLayerConfig(LayerConfig): tool_description: str | None = None max_fields: int = Field(default=8, ge=0) max_actions: int = Field(default=4, ge=1) - allowed_field_types: list[AskHumanFieldType] = Field(default_factory=lambda: ["paragraph", "select"]) + allowed_field_types: list[AskHumanFieldType] = Field(default_factory=_default_allowed_field_types) allow_file_fields: bool = False max_markdown_chars: int = Field(default=8_000, ge=0) max_question_chars: int = Field(default=1_000, ge=1) diff --git a/dify-agent/src/dify_agent/layers/ask_human/layer.py b/dify-agent/src/dify_agent/layers/ask_human/layer.py index a94de9af72e..399f28ab063 100644 --- a/dify-agent/src/dify_agent/layers/ask_human/layer.py +++ b/dify-agent/src/dify_agent/layers/ask_human/layer.py @@ -20,7 +20,7 @@ from typing import Any, ClassVar, cast from pydantic import JsonValue, ValidationError from pydantic_ai import Tool from pydantic_ai.exceptions import ModelRetry -from pydantic_ai.tools import DeferredToolRequests, RunContext, ToolDefinition +from pydantic_ai.tools import ArgsValidatorFunc, DeferredToolRequests, RunContext, ToolDefinition from typing_extensions import Self, override from agenton.layers import EmptyRuntimeState, NoLayerDeps, PydanticAILayer, PydanticAIPrompt, PydanticAITool @@ -70,7 +70,7 @@ class DifyAskHumanLayer(PydanticAILayer[NoLayerDeps, object, DifyAskHumanLayerCo name=self.config.tool_name, description=self.config.effective_tool_description, prepare=self._prepare_tool_definition, - args_validator=self._validate_tool_args, + args_validator=cast(ArgsValidatorFunc[object, ...], self._validate_tool_args), sequential=True, ) ] diff --git a/dify-agent/src/dify_agent/layers/dify_core_tools/configs.py b/dify-agent/src/dify_agent/layers/dify_core_tools/configs.py index 47184d04b56..19a25898cf8 100644 --- a/dify-agent/src/dify_agent/layers/dify_core_tools/configs.py +++ b/dify-agent/src/dify_agent/layers/dify_core_tools/configs.py @@ -21,6 +21,10 @@ type DifyCoreToolProviderType = Literal["plugin", "builtin", "api", "workflow", DIFY_CORE_TOOLS_LAYER_TYPE_ID: Final[str] = "dify.core.tools" +def _default_parameters_json_schema() -> dict[str, JsonValue]: + return {"type": "object", "properties": {}, "required": []} + + class DifyCoreToolConfig(LayerConfig): """Prepared API-routed tool declaration exposed to the model.""" @@ -32,9 +36,7 @@ class DifyCoreToolConfig(LayerConfig): description: str | None = None runtime_parameters: dict[str, JsonValue] = Field(default_factory=dict) parameters: list[DifyPluginToolParameter] = Field(default_factory=list) - parameters_json_schema: dict[str, JsonValue] = Field( - default_factory=lambda: {"type": "object", "properties": {}, "required": []} - ) + parameters_json_schema: dict[str, JsonValue] = Field(default_factory=_default_parameters_json_schema) model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid", arbitrary_types_allowed=True) diff --git a/dify-agent/src/dify_agent/layers/dify_plugin/configs.py b/dify-agent/src/dify_agent/layers/dify_plugin/configs.py index 3d2ac615e5a..ad289c055d8 100644 --- a/dify-agent/src/dify_agent/layers/dify_plugin/configs.py +++ b/dify-agent/src/dify_agent/layers/dify_plugin/configs.py @@ -30,6 +30,10 @@ DIFY_PLUGIN_LLM_LAYER_TYPE_ID: Final[str] = "dify.plugin.llm" DIFY_PLUGIN_TOOLS_LAYER_TYPE_ID: Final[str] = "dify.plugin.tools" +def _default_parameters_json_schema() -> dict[str, JsonValue]: + return {"type": "object", "properties": {}, "required": []} + + class DifyPluginToolOption(BaseModel): """Selectable tool option value exposed to the model. @@ -151,9 +155,7 @@ class DifyPluginToolConfig(LayerConfig): credentials: dict[str, DifyPluginCredentialValue] = Field(default_factory=dict) runtime_parameters: dict[str, DifyPluginToolValue] = Field(default_factory=dict) parameters: list[DifyPluginToolParameter] = Field(default_factory=list) - parameters_json_schema: dict[str, JsonValue] = Field( - default_factory=lambda: {"type": "object", "properties": {}, "required": []} - ) + parameters_json_schema: dict[str, JsonValue] = Field(default_factory=_default_parameters_json_schema) model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid", arbitrary_types_allowed=True) diff --git a/dify-agent/src/dify_agent/runtime/compositor_factory.py b/dify-agent/src/dify_agent/runtime/compositor_factory.py index 5563d43a706..f56b37e3c17 100644 --- a/dify-agent/src/dify_agent/runtime/compositor_factory.py +++ b/dify-agent/src/dify_agent/runtime/compositor_factory.py @@ -29,7 +29,7 @@ snapshots. from __future__ import annotations from collections.abc import Mapping, Sequence -from typing import Any, cast +from typing import Any from pydantic_ai.messages import UserContent @@ -164,21 +164,18 @@ def build_pydantic_ai_compositor( selected provider set explicitly so provider defaulting stays at outer runtime boundaries rather than being duplicated here. """ - return cast( - Compositor[ - PydanticAIPrompt[object], - PydanticAITool[object], - AllPromptTypes, - AllToolTypes, - UserContent, - AllUserPromptTypes, - ], - Compositor.from_config( - config, - providers=providers, - node_providers=node_providers, - **PYDANTIC_AI_TRANSFORMERS, # pyright: ignore[reportArgumentType] - ), + return Compositor[ + PydanticAIPrompt[object], + PydanticAITool[object], + AllPromptTypes, + AllToolTypes, + UserContent, + AllUserPromptTypes, + ].from_config( + config, + providers=providers, + node_providers=node_providers, + **PYDANTIC_AI_TRANSFORMERS, ) diff --git a/dify-agent/src/dify_agent/server/app.py b/dify-agent/src/dify_agent/server/app.py index 4b1d4ce3782..5af355f376c 100644 --- a/dify-agent/src/dify_agent/server/app.py +++ b/dify-agent/src/dify_agent/server/app.py @@ -16,6 +16,7 @@ environment configuration provides a token. from collections.abc import AsyncGenerator from contextlib import asynccontextmanager +from typing_extensions import cast import httpx from fastapi import APIRouter, FastAPI @@ -132,10 +133,10 @@ def create_app(settings: ServerSettings | None = None) -> FastAPI: configure_server_observability(app) def get_store() -> RedisRunStore: - return state["store"] # pyright: ignore[reportReturnType] + return cast(RedisRunStore, state["store"]) def get_scheduler() -> RunScheduler: - return state["scheduler"] # pyright: ignore[reportReturnType] + return cast(RunScheduler, state["scheduler"]) control_plane_router = APIRouter( dependencies=[create_bearer_token_dependency(resolved_settings.api_token)], diff --git a/dify-agent/tests/local/test_packaging.py b/dify-agent/tests/local/test_packaging.py index d5f767dcdfd..c2b82a21d01 100644 --- a/dify-agent/tests/local/test_packaging.py +++ b/dify-agent/tests/local/test_packaging.py @@ -29,8 +29,8 @@ SERVER_RUNTIME_DEPENDENCIES = { } DEV_DEPENDENCIES = { - "basedpyright>=1.39.3", "coverage[toml]>=7.10.7", + "pyrefly>=1.2.0", "pytest>=9.0.3", "pytest-examples>=0.0.18", "pytest-mock>=3.14.0", diff --git a/dify-agent/uv.lock b/dify-agent/uv.lock index 282cdbd1917..88f8ccb3784 100644 --- a/dify-agent/uv.lock +++ b/dify-agent/uv.lock @@ -109,18 +109,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/3e/5c/fb93d3092640a24dfb7bd7727a24016d7c01774ca013e60efd3f683c8002/backrefs-7.0-py314-none-any.whl", hash = "sha256:a6448b28180e3ca01134c9cf09dcebafad8531072e09903c5451748a05f24bc9", size = 412349, upload-time = "2026-04-28T16:28:02.412Z" }, ] -[[package]] -name = "basedpyright" -version = "1.39.3" -source = { registry = "https://pypi.org/simple" } -dependencies = [ - { name = "nodejs-wheel-binaries" }, -] -sdist = { url = "https://files.pythonhosted.org/packages/04/19/5a5b9b9197973da732638957be3a65cf514d2f5a4964eeedbf33b6c65bbd/basedpyright-1.39.3.tar.gz", hash = "sha256:2f794e6b5f4260fb89f614ca6cd23c6f305373bb6b50c4ed7794ff2ae647fb14", size = 25503187, upload-time = "2026-04-20T22:14:47.424Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/54/5c/f950c1239ad26f3bb453e665428a2cf1893995de725a5eb0b64a2520b366/basedpyright-1.39.3-py3-none-any.whl", hash = "sha256:aba760dc83307727554f936d6b4381caa14482f30dbc2173167710e217c1f7ab", size = 12419181, upload-time = "2026-04-20T22:14:51.975Z" }, -] - [[package]] name = "beautifulsoup4" version = "4.14.3" @@ -627,8 +615,8 @@ server = [ [package.dev-dependencies] dev = [ - { name = "basedpyright" }, { name = "coverage" }, + { name = "pyrefly" }, { name = "pytest" }, { name = "pytest-examples" }, { name = "pytest-mock" }, @@ -664,8 +652,8 @@ provides-extras = ["server"] [package.metadata.requires-dev] dev = [ - { name = "basedpyright", specifier = ">=1.39.3" }, { name = "coverage", extras = ["toml"], specifier = ">=7.10.7" }, + { name = "pyrefly", specifier = ">=1.2.0" }, { name = "pytest", specifier = ">=9.0.3" }, { name = "pytest-examples", specifier = ">=0.0.18" }, { name = "pytest-mock", specifier = ">=3.14.0" }, @@ -707,7 +695,7 @@ wheels = [ [[package]] name = "e2b" -version = "2.38.0" +version = "2.46.4" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "attrs" }, @@ -723,9 +711,9 @@ dependencies = [ { name = "typing-extensions" }, { name = "wcmatch" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/e2/e2/1acb7bd6e16811a3affc89521304420410f97040a0a386497b101f898de3/e2b-2.38.0.tar.gz", hash = "sha256:b245976cc946d144baf77341b19fa537bfd7cbd9028dc8ef59bd6947f5e329c1", size = 205432, upload-time = "2026-08-10T17:57:07.643Z" } +sdist = { url = "https://files.pythonhosted.org/packages/7d/04/3e7d3ca8abaeffcfc41d36b9d6fe3aa055cecf3b56234e051991de848b2d/e2b-2.46.4.tar.gz", hash = "sha256:777d35beb00194b401a6c4ac2498aa836628839649f17aae86e85920382c8f6c", size = 228801, upload-time = "2026-09-02T20:48:36.725Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/03/fa/e6d50d307a9dcde63038589fce7c1cb32ce3efde3d70c20a6adb11eee3c6/e2b-2.38.0-py3-none-any.whl", hash = "sha256:9f43803aa23e9a33ed48c1c778d588f83c0390db21edf3098255137981541b67", size = 365646, upload-time = "2026-08-10T17:57:08.947Z" }, + { url = "https://files.pythonhosted.org/packages/a0/b5/585a30223d9ee95bc94f32f8d5358b147bf93832e37b809545a3ab7892a0/e2b-2.46.4-py3-none-any.whl", hash = "sha256:5d77e9fee8571a0d24a179f14244984127d18bd93a99a5e90226c876087035b5", size = 403067, upload-time = "2026-09-02T20:48:35.082Z" }, ] [[package]] @@ -922,15 +910,15 @@ wheels = [ [[package]] name = "h2" -version = "4.3.0" +version = "4.4.1" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "hpack" }, { name = "hyperframe" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/1d/17/afa56379f94ad0fe8defd37d6eb3f89a25404ffc71d4d848893d270325fc/h2-4.3.0.tar.gz", hash = "sha256:6c59efe4323fa18b47a632221a1888bd7fde6249819beda254aeca909f221bf1", size = 2152026, upload-time = "2025-08-23T18:12:19.778Z" } +sdist = { url = "https://files.pythonhosted.org/packages/e7/85/7c366e69d84c17bb778fe41419e1fbcce3033d5b7ce29bbffff0a98b859f/h2-4.4.1.tar.gz", hash = "sha256:4e866ffb1a869ae14dd9b5e6beb5c24a13da0495ad72b65925ded182521c1516", size = 2157281, upload-time = "2026-08-03T11:45:09.509Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/69/b2/119f6e6dcbd96f9069ce9a2665e0146588dc9f88f29549711853645e736a/h2-4.3.0-py3-none-any.whl", hash = "sha256:c438f029a25f7945c69e0ccf0fb951dc3f73a5f6412981daee861431b70e2bdd", size = 61779, upload-time = "2025-08-23T18:12:17.779Z" }, + { url = "https://files.pythonhosted.org/packages/7e/22/e85faf23bd72a92d1921e37d674ca56eb298a3c8be31fdecef0ff2b3aaac/h2-4.4.1-py3-none-any.whl", hash = "sha256:0e25f1462b23c9cb82d9eb02e28bc706dac2a68cb457c6a0d74d63c8a2a5d0e6", size = 62636, upload-time = "2026-08-03T11:44:59.164Z" }, ] [[package]] @@ -967,11 +955,11 @@ wheels = [ [[package]] name = "hpack" -version = "4.1.0" +version = "4.2.0" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/2c/48/71de9ed269fdae9c8057e5a4c0aa7402e8bb16f2c6e90b3aa53327b113f8/hpack-4.1.0.tar.gz", hash = "sha256:ec5eca154f7056aa06f196a557655c5b009b382873ac8d1e66e79e87535f1dca", size = 51276, upload-time = "2025-01-22T21:44:58.347Z" } +sdist = { url = "https://files.pythonhosted.org/packages/26/5b/fcabf6028144a8723726318b07a32c2f3314acdff6265743cf08a344b18e/hpack-4.2.0.tar.gz", hash = "sha256:0895cfa3b5531fc65fe439c05eb65144f123bf7a394fcaa56aa423548d8e45c0", size = 51300, upload-time = "2026-06-23T18:34:46.667Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/07/c6/80c95b1b2b94682a72cbdbfb85b81ae2daffa4291fbfa1b1464502ede10d/hpack-4.1.0-py3-none-any.whl", hash = "sha256:157ac792668d995c657d93111f46b4535ed114f0c9c8d672271bbec7eae1b496", size = 34357, upload-time = "2025-01-22T21:44:56.92Z" }, + { url = "https://files.pythonhosted.org/packages/71/b4/4a9fcfb2aef6ba44d9073ecd301443aa00b3dac95de5619f2a7de7ec8a91/hpack-4.2.0-py3-none-any.whl", hash = "sha256:858ac0b02280fa582b5080d68db0899c62a80375e0e5413a74970c5e518b6986", size = 34246, upload-time = "2026-06-23T18:34:45.472Z" }, ] [[package]] @@ -1593,7 +1581,7 @@ wheels = [ [[package]] name = "mkdocs-material" -version = "9.7.6" +version = "9.7.7" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "babel" }, @@ -1608,9 +1596,9 @@ dependencies = [ { name = "pymdown-extensions" }, { name = "requests" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/45/29/6d2bcf41ae40802c4beda2432396fff97b8456fb496371d1bc7aad6512ec/mkdocs_material-9.7.6.tar.gz", hash = "sha256:00bdde50574f776d328b1862fe65daeaf581ec309bd150f7bff345a098c64a69", size = 4097959, upload-time = "2026-03-19T15:41:58.161Z" } +sdist = { url = "https://files.pythonhosted.org/packages/f1/cd/c05d3a530ba7934f144fb45f7203cd236adc25c7bdcc34673d202f4b0278/mkdocs_material-9.7.7.tar.gz", hash = "sha256:c0649c065b1b0512d60aad8c10f947f8e455284475239b364b610f2deb4d0855", size = 4097923, upload-time = "2026-07-17T16:21:33.156Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/2c/01/bc663630c510822c95c47a66af9fa7a443c295b47d5f041e5e6ae62ef659/mkdocs_material-9.7.6-py3-none-any.whl", hash = "sha256:71b84353921b8ea1ba84fe11c50912cc512da8fe0881038fcc9a0761c0e635ba", size = 9305470, upload-time = "2026-03-19T15:41:55.217Z" }, + { url = "https://files.pythonhosted.org/packages/ad/21/17c1bc9e6f47c972ad66fb2ac2568f99f90f1207eeb6fc3b34d094dba7b5/mkdocs_material-9.7.7-py3-none-any.whl", hash = "sha256:8ea9bb1737a5b524a5f9dcf2e1b4ebda8274ae3008aa7845720a97083bef708f", size = 9305438, upload-time = "2026-07-17T16:21:30.017Z" }, ] [[package]] @@ -1710,22 +1698,6 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/79/7b/2c79738432f5c924bef5071f933bcc9efd0473bac3b4aa584a6f7c1c8df8/mypy_extensions-1.1.0-py3-none-any.whl", hash = "sha256:1be4cccdb0f2482337c4743e60421de3a356cd97508abadd57d47403e94f5505", size = 4963, upload-time = "2025-04-22T14:54:22.983Z" }, ] -[[package]] -name = "nodejs-wheel-binaries" -version = "24.15.0" -source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/3f/70/a1e4f4d5986768ab90cc860b1cc3660fd2ded74ca175a900a5c29f839c7d/nodejs_wheel_binaries-24.15.0.tar.gz", hash = "sha256:b43f5c4f6e5768d8845b2ae4682eb703a19bf7aadc84187e2d903ed3a611c859", size = 8057, upload-time = "2026-04-19T15:48:16.899Z" } -wheels = [ - { url = "https://files.pythonhosted.org/packages/85/66/54051d14853d6ab4fb85f8be9b042b530be653357fb9a19557498bc91ab7/nodejs_wheel_binaries-24.15.0-py2.py3-none-macosx_13_0_arm64.whl", hash = "sha256:a6232fa8b754220941f52388c8ead923f7c1c7fdf0ea0d98f657523bd9a81ef4", size = 55173485, upload-time = "2026-04-19T15:47:34.561Z" }, - { url = "https://files.pythonhosted.org/packages/ad/5f/66acada164da5ca10a0824db021aa7394ae18396c550cd9280e839a43126/nodejs_wheel_binaries-24.15.0-py2.py3-none-macosx_13_0_x86_64.whl", hash = "sha256:001a6b62c69d9109c1738163cca00608dd2722e8663af59300054ea02610972d", size = 55348100, upload-time = "2026-04-19T15:47:40.521Z" }, - { url = "https://files.pythonhosted.org/packages/0d/2d/0cbd5ff40c9bb030ca1735d8f8793bd74f08a4cbd49100a1d19313ea57ab/nodejs_wheel_binaries-24.15.0-py2.py3-none-manylinux_2_28_aarch64.whl", hash = "sha256:0fbc48765e60ed0ff30d43898dbf5cadbadf2e5f1e7f204afc2b01493b7ebce6", size = 59668206, upload-time = "2026-04-19T15:47:46.848Z" }, - { url = "https://files.pythonhosted.org/packages/da/d5/91ac63951ec75927a486b83b8cafe650e360fa70ac01dc94adfb32b93b97/nodejs_wheel_binaries-24.15.0-py2.py3-none-manylinux_2_28_x86_64.whl", hash = "sha256:20ee0536809795da8a4942fc1ab4cbdebbcaaf29383eab67ba8874268fb00008", size = 60206736, upload-time = "2026-04-19T15:47:52.668Z" }, - { url = "https://files.pythonhosted.org/packages/db/72/dc22776974d928869c0c30d23ee98ed7df254243c2df68f09f5963e8e8b8/nodejs_wheel_binaries-24.15.0-py2.py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:1fade6c214285e72472ca40a631e98ff36559671cd5eefc8bf009471d67f04b4", size = 61720456, upload-time = "2026-04-19T15:47:58.325Z" }, - { url = "https://files.pythonhosted.org/packages/01/0a/34461b9050cb45ee371dccdefc622aef6351506ea2691b08fc761ca67150/nodejs_wheel_binaries-24.15.0-py2.py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:3984cb8d87766567aee67a49743227ab40ede6f47734ec990ff90e50b74e7740", size = 62326172, upload-time = "2026-04-19T15:48:04.094Z" }, - { url = "https://files.pythonhosted.org/packages/c9/17/09252bf35672dba926649d59dfe51443a0f6955ad13784e91131d5ec82a2/nodejs_wheel_binaries-24.15.0-py2.py3-none-win_amd64.whl", hash = "sha256:a437601956b532dcb3082046e6978e622733f90edc0932cbb9adb3bb97a16501", size = 41543461, upload-time = "2026-04-19T15:48:09.332Z" }, - { url = "https://files.pythonhosted.org/packages/0a/7e/b649777d148e1e0c2ce349156603cdb12f7ed99921b95d93717393650193/nodejs_wheel_binaries-24.15.0-py2.py3-none-win_arm64.whl", hash = "sha256:bdf4a431e08321a32efc604111c6f23941f87055d796a537e8c4110daecad23f", size = 39233248, upload-time = "2026-04-19T15:48:13.326Z" }, -] - [[package]] name = "numba" version = "0.65.1" @@ -2687,48 +2659,67 @@ wheels = [ [[package]] name = "pyqwest" -version = "0.9.0" +version = "0.10.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "opentelemetry-api" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/8d/59/97531fd9d0a06d54e84f5493775739b0c1d0d13ec704974d04fa423a3332/pyqwest-0.9.0.tar.gz", hash = "sha256:514dd0b37d7a1bcb978b5d6423d15f89cf7dcf8058ac2a37aa42cd30090de89e", size = 477462, upload-time = "2026-08-10T01:55:36.219Z" } +sdist = { url = "https://files.pythonhosted.org/packages/07/ee/0ff9facfa9e7a4f6df2a770d4eaf1ad0f74165da7e8c28e888461f07604c/pyqwest-0.10.0.tar.gz", hash = "sha256:6c1a693be17d57d2c2eca4085e32c2809c53090c16719a907c90ebcf1f40dc01", size = 482248, upload-time = "2026-08-21T06:09:20.656Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/47/34/d556fa07d9bff21fc6c9c16738614c6336164b65d9ced1ce9f1c1044aaeb/pyqwest-0.9.0-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:517d2eb295d56ef1530cebe0cf290a66fa199c57955bd1c5f1dc592240fbb7d6", size = 5224594, upload-time = "2026-08-10T01:54:18.646Z" }, - { url = "https://files.pythonhosted.org/packages/c9/3f/1d7d1a08ebf8838c0c85e99ab5db5c1aae457f76ee03b60c8f4fc4948e01/pyqwest-0.9.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:408dc692835363b6824bee61ee9e4c9a5bba9c08d75b903f82f9a802e41d1b30", size = 5100753, upload-time = "2026-08-10T01:54:20.669Z" }, - { url = "https://files.pythonhosted.org/packages/66/fe/22c4056b526baa4c2c953f4e99925ce0bb14e7747d66e6ac3448a34f76e5/pyqwest-0.9.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e1abf1ab3a0c1b545651ab37a643acc7909c4e7644a63a2e4af36e3e230a0db5", size = 5619882, upload-time = "2026-08-10T01:54:22.468Z" }, - { url = "https://files.pythonhosted.org/packages/de/2c/8f1159a30bbc905b2fcbc386740a260e76ae64372feb263103438ef5201c/pyqwest-0.9.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9b02e5c9ce57e079eb3716c464d3818ad2573d7743483c39526ad77ee95af806", size = 5564744, upload-time = "2026-08-10T01:54:24.814Z" }, - { url = "https://files.pythonhosted.org/packages/ac/fd/ff15034d7874fd208d606bd80c924fb0b1091db8159c892672168b656de1/pyqwest-0.9.0-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:98bdaa30a3a8d8c71506a33c084e485a243fbf51e77130bb39e0f249dd116142", size = 5779373, upload-time = "2026-08-10T01:54:26.753Z" }, - { url = "https://files.pythonhosted.org/packages/ed/b5/eeafdeb65ad8fe9c39044aedcfc97610f4e5c7b5620ca9fe2d504f669108/pyqwest-0.9.0-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:10eba84f9d8512d23bd16dd60b1c77dab956470361559214981dda065df4ad1a", size = 5973306, upload-time = "2026-08-10T01:54:28.729Z" }, - { url = "https://files.pythonhosted.org/packages/34/db/a8979dab5c591529f858d1505d09b3c8b6dec337921f15bed4858c55716e/pyqwest-0.9.0-cp310-abi3-win_amd64.whl", hash = "sha256:af4e859800b02cd1fcdd898af26a881c4a8ce9520f71f6cb0a5403523d2cbbdf", size = 4834706, upload-time = "2026-08-10T01:54:30.335Z" }, - { url = "https://files.pythonhosted.org/packages/1d/9e/d070ca1ce3c202952770436172889a4b0556a564a490baf726f7bb895eba/pyqwest-0.9.0-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:abe322cc1b63947b493bc06028615e0d5de655928159227aab64f60a0eb0e27d", size = 5244745, upload-time = "2026-08-10T01:54:32.257Z" }, - { url = "https://files.pythonhosted.org/packages/41/97/ebded0bb35a79f51bb2d1519e34922d79c8825f9e71b696bb20f387a08c2/pyqwest-0.9.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:c6b2dc6a9c583859d031941e1041b8e711afe0ad4c130628207731cef2b1a486", size = 5101044, upload-time = "2026-08-10T01:54:34.088Z" }, - { url = "https://files.pythonhosted.org/packages/a9/2c/a7f117e793b4e27642807bf7230b9fa297ff1e1ac489fc260fc3d4350ae4/pyqwest-0.9.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:551e47714da72a72939958029eb37a754cecd974344471c4a2038583a66a5396", size = 5630841, upload-time = "2026-08-10T01:54:35.885Z" }, - { url = "https://files.pythonhosted.org/packages/b4/b4/be47fc141529941352a848cd77da581803137b733993a51bf464f2c48915/pyqwest-0.9.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3be3d38ccdab3077bf1cfe90c346ce927f9b49c56dcdf20e060f5f2502d01021", size = 5572909, upload-time = "2026-08-10T01:54:37.653Z" }, - { url = "https://files.pythonhosted.org/packages/cb/fd/703635e12710ce7ef5e961f5e1b22c609aecc8f07eedd79477b48d1edb74/pyqwest-0.9.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:45d9d424da878b22604799d1ce2a0a5df054924487ee5a851ad08025e2076e5c", size = 5794049, upload-time = "2026-08-10T01:54:39.363Z" }, - { url = "https://files.pythonhosted.org/packages/3c/15/941aad56b108c743df9d41e124be82ecb6be0eba1007e945b6a39f5dd1a0/pyqwest-0.9.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:e4420adcd2b756da706b31a04f095a521f6e440dab284b7a1b46c76e048c1f78", size = 5974542, upload-time = "2026-08-10T01:54:41.202Z" }, - { url = "https://files.pythonhosted.org/packages/b7/86/8445fe05b47322047347464a909ae95dba023bf8409b9967f66bda2905ad/pyqwest-0.9.0-cp312-cp312-win_amd64.whl", hash = "sha256:25734bd9798bbfaa53c83d395649dd9ea9a791a64b9848734955cb78ad52c832", size = 4847034, upload-time = "2026-08-10T01:54:42.899Z" }, - { url = "https://files.pythonhosted.org/packages/a7/92/013fc3c94f0dc4eeca556431797da477d76ad43d69134837ba149468dcc3/pyqwest-0.9.0-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:6e4c2afe6c7293d9e42cfdb0489b0b5f64002159c2dc9c45d9322c1b4ffb42cd", size = 5244793, upload-time = "2026-08-10T01:54:44.891Z" }, - { url = "https://files.pythonhosted.org/packages/48/61/c685e0c595f3f1b7842a776c48aaf2b3f47270a61672967d9d38f7c5e7f6/pyqwest-0.9.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:67f153b94dfbb9aeabc38f20a1bada1acfd9361bec12db86268bd951be35a154", size = 5100715, upload-time = "2026-08-10T01:54:46.777Z" }, - { url = "https://files.pythonhosted.org/packages/f9/a1/a28b2cd704d7dd37043f1e6b398798576c12293f3efae84f00e7f6a5625c/pyqwest-0.9.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b9942ffa243c8c2243e3ce04e3e3ec9b4c119346c963fd0051cd989f0defecf6", size = 5628787, upload-time = "2026-08-10T01:54:48.488Z" }, - { url = "https://files.pythonhosted.org/packages/36/31/6255c7f17cd00e39f1c4df3003cea2d2d087e483fb4c49a6535307cd21e0/pyqwest-0.9.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7f8f9c880c19826fead1838e12b042865b15df3ec844928cc74188a8b740f761", size = 5571946, upload-time = "2026-08-10T01:54:50.181Z" }, - { url = "https://files.pythonhosted.org/packages/37/71/344738bdb38bddbc07cb29e5b1287c08db9e53d6b322581492571bdef748/pyqwest-0.9.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:56040761f6ca7abb036c7d288863bf143a9b166d824d98a604908ef20c6906e0", size = 5792872, upload-time = "2026-08-10T01:54:52.045Z" }, - { url = "https://files.pythonhosted.org/packages/fc/ee/c5c74e3afb6dd0a25bca23bb4757d1a7135fab09a0a054036e837d49dd11/pyqwest-0.9.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:02a51df744521e45eb5379bc325b91de4512ec64a9f4aba3248f9166ec4e5b88", size = 5974192, upload-time = "2026-08-10T01:54:53.818Z" }, - { url = "https://files.pythonhosted.org/packages/6e/08/302960836e3e43ca3051313ee3b767534d1610bd3b8033c7bbad41b06e1b/pyqwest-0.9.0-cp313-cp313-win_amd64.whl", hash = "sha256:e9a0e3123aee446d5f6e57c43cae87b4dab4b1af3fa970d114d74d5d4b9d075c", size = 4847050, upload-time = "2026-08-10T01:54:56.155Z" }, - { url = "https://files.pythonhosted.org/packages/fc/3e/a36940844325fbc59adf92116d3bcd1155cc2c72d1a390880886ca492bb4/pyqwest-0.9.0-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:fdab21bbedff4a21209135423f5f54f287a01dd6143cbd7aeab63d5d8c889654", size = 5246741, upload-time = "2026-08-10T01:54:57.697Z" }, - { url = "https://files.pythonhosted.org/packages/41/8d/efb9741449a81f2489f6929a7691a2145662c3650e06bcb139c64f543d0a/pyqwest-0.9.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:21a04ddf4497ef3c7fe36cdc473c094999b7710005d38bd8a0dfd4a3feace12f", size = 5102423, upload-time = "2026-08-10T01:54:59.253Z" }, - { url = "https://files.pythonhosted.org/packages/8f/a8/0b667cf9c07e62861cfe24c09ad30357a9c2a0fc3863cfc5610af5b66e3e/pyqwest-0.9.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c8c51c1a27ea529fffab90c127a045f0dfe1abc54d5b9ad5f4165e954439c9b2", size = 5627663, upload-time = "2026-08-10T01:55:01.036Z" }, - { url = "https://files.pythonhosted.org/packages/5f/7c/1f88be3d35afe92b79e361aa40faf3904e3a7852e8638a9d3d5c00891ab3/pyqwest-0.9.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:10f283ffc40a2774e0dc8fbb47e4a8f9f8a698a99feae3bf4ad75b340c98031b", size = 5572750, upload-time = "2026-08-10T01:55:03.163Z" }, - { url = "https://files.pythonhosted.org/packages/21/3e/98c1e151772e77dcf00cb8b1c070f2c8edcb15fe12fdee30c1022006ca8e/pyqwest-0.9.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:1f73d4a8e75b5860741fbd0623a0ce792a53336dbfec5f34736004acd5446843", size = 5790615, upload-time = "2026-08-10T01:55:04.758Z" }, - { url = "https://files.pythonhosted.org/packages/7b/f1/12bb4b119cffb7fe5301b2d997ec9ffc1719637f6f4faf2ff2315e13b201/pyqwest-0.9.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:e34d55a53492b44b82b690c2b11957fc35e99ebab4db2e9bcb2a1e2621d71b62", size = 5976516, upload-time = "2026-08-10T01:55:06.453Z" }, - { url = "https://files.pythonhosted.org/packages/53/55/046dbf68ccedd816770fd212c6c2612372a6f0d7ecf2d8273480db460ddf/pyqwest-0.9.0-cp314-cp314-win_amd64.whl", hash = "sha256:48c367150783f6866d0af43c209cc405f8287743d7b2472c7d757fda2478fd57", size = 4841470, upload-time = "2026-08-10T01:55:08.035Z" }, - { url = "https://files.pythonhosted.org/packages/55/cd/38ff77d145385afd71b2dbd6eb116251ce3ffddbed0edde16c64394fbec2/pyqwest-0.9.0-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:10fae206270355c993b49c49bf9778995167f5efe591807d50a8e682921d71ea", size = 5222689, upload-time = "2026-08-10T01:55:09.639Z" }, - { url = "https://files.pythonhosted.org/packages/82/95/d8c6804eeb72b7b89842b58f1f05d5cfda5be410c4a22442d2574d510d39/pyqwest-0.9.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:5f475129fb792593222a9ce0fefbbc10bda1544a7411f3946d771c9a61c50e61", size = 5085846, upload-time = "2026-08-10T01:55:11.569Z" }, - { url = "https://files.pythonhosted.org/packages/f2/e7/04fe8962ec416be0199093ebdd326653a5a7b8f53ddfdb29af81756f7c08/pyqwest-0.9.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:78f6f676d349535cb094e8e3bcf33863238a142fb7dcc7afb9e61749e2351047", size = 5613296, upload-time = "2026-08-10T01:55:13.315Z" }, - { url = "https://files.pythonhosted.org/packages/03/0c/7a11728155a34838d2e7c78fb24c937f2cd844ec791ef416030c52f4bb5e/pyqwest-0.9.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f460f5fcdae8cc46e6a679128836f6cd47fbf4bf73e5c9d6ea868e4a90b2e7ee", size = 5557492, upload-time = "2026-08-10T01:55:15.103Z" }, - { url = "https://files.pythonhosted.org/packages/4a/d5/8c6e68f0e978b5257b309809fec00d946c1ed20dacef97f25c23de5b91a9/pyqwest-0.9.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:cba6804151e973d27c99b9f02b3f0bcab7270c3f99725b508ada9c147365cabd", size = 5774204, upload-time = "2026-08-10T01:55:16.932Z" }, - { url = "https://files.pythonhosted.org/packages/17/20/fed89b62d50b4efad2ebe78c3867e0e402f824726652034728a5dd0a342e/pyqwest-0.9.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:bc9dfeca326918d70a5368114de0fc627d9f64fafa36563ab484ae58ffe42c0d", size = 5964506, upload-time = "2026-08-10T01:55:19.031Z" }, - { url = "https://files.pythonhosted.org/packages/11/de/30575019efebae84502269d097e706e89a30631e3719218b053e4e0dc179/pyqwest-0.9.0-cp314-cp314t-win_amd64.whl", hash = "sha256:384693adc0908f16324e5b508a3c25fb9a92b88f9bdf7ccb6820507db4a95888", size = 4809013, upload-time = "2026-08-10T01:55:21.042Z" }, + { url = "https://files.pythonhosted.org/packages/43/ee/b1a28f57c689606cfd065d8a553841150f7daaa91d20e58dcc2c5ea191f8/pyqwest-0.10.0-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:aa492d5777dd145a60795ed95d9d4707a3cd1091fdcdfc93a82ac7fdc43ebacd", size = 5261059, upload-time = "2026-08-21T06:08:04.999Z" }, + { url = "https://files.pythonhosted.org/packages/dc/13/9c5046cfd6ef705bde0b620ba8a794335bcabc0839342a2a647f2427b27e/pyqwest-0.10.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:59f3f16628e518c674102e7b5fcff2101bba6abb4f6737ec5fade9b9278e6a53", size = 5134207, upload-time = "2026-08-21T06:08:06.955Z" }, + { url = "https://files.pythonhosted.org/packages/93/7d/50021dd88d82d6966ab1c27593ceaee9d1ed62fbe597c40e8dc187cfa5fd/pyqwest-0.10.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d6e7db305a8318b1f3218053e87501f8f245ca8bd63e948e0282d04bf0883470", size = 5640730, upload-time = "2026-08-21T06:08:09.135Z" }, + { url = "https://files.pythonhosted.org/packages/ff/3f/5bf6c32e9e701837a8c47ce6e3ad38978cfec8eb7bc6596181e5f9e1eaeb/pyqwest-0.10.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a5c757cfac5f53c8671dcb4850d5fc4c4339ea3e90636331c9318f8e3ddabc06", size = 5561462, upload-time = "2026-08-21T06:08:10.836Z" }, + { url = "https://files.pythonhosted.org/packages/5f/61/ca9ba5721461b7ce5cfaac373ab3a1723ddcc434af7430f8bd628da6e623/pyqwest-0.10.0-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:234b3f71e3f314d997c203d8cf829b7117edd041153f9c277d0060ab90134148", size = 5801847, upload-time = "2026-08-21T06:08:12.502Z" }, + { url = "https://files.pythonhosted.org/packages/d2/44/95593919b996a417093f598d887822b9b899e8d025588c9bfaf8c60dd812/pyqwest-0.10.0-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:5637256a0dac0ef57e0eaa02b032014965e4a4c995e1deca1b1b97e6d1765f78", size = 5978692, upload-time = "2026-08-21T06:08:14.383Z" }, + { url = "https://files.pythonhosted.org/packages/a5/b9/b2821ce5188457168ebb25d5ff65b1ca1bf27bc6b4a33df4bcc2357e625c/pyqwest-0.10.0-cp310-abi3-win_amd64.whl", hash = "sha256:7ea761937acf3a00d1a7e70e982949d18946e5471d1419266ab3a78bbfa19759", size = 4876627, upload-time = "2026-08-21T06:08:16.084Z" }, + { url = "https://files.pythonhosted.org/packages/86/b4/16ccef1c203fa258ce46a86aefc1a79c13b5f0b8d49627347d90eef25efd/pyqwest-0.10.0-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:a21f1f15252a8303623b4f17b9c6de595ace11b3ade07f2adb6d07121e8191aa", size = 5274815, upload-time = "2026-08-21T06:08:17.777Z" }, + { url = "https://files.pythonhosted.org/packages/5f/61/6a87f84f571441ea43279587d4bfcad4543505918ae2b83a1ebdcfa98be5/pyqwest-0.10.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:eb472c6e5d6833ebfec79db310e426eb17b01ac64c0e2c251bd9192c0d2ee0c5", size = 5123656, upload-time = "2026-08-21T06:08:19.501Z" }, + { url = "https://files.pythonhosted.org/packages/8b/f2/ab69e581cf9b798b0e169f7b27fd3f8b6f9f1631bd4d3b6e22e5abaf8d8b/pyqwest-0.10.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:83578e24cccd5e0dc04d60a0af7bfb43325b5f22d03ff74ff79ed0ecf553b50d", size = 5641253, upload-time = "2026-08-21T06:08:21.627Z" }, + { url = "https://files.pythonhosted.org/packages/9f/dd/f1a62eebf8321ace506bd94551a01431f6a45b882225455eae3ea6e8c6d1/pyqwest-0.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6bb511c434f79c641efb5573e5795e56dc972252f4b96e52a9636d4ece5231a4", size = 5567341, upload-time = "2026-08-21T06:08:23.346Z" }, + { url = "https://files.pythonhosted.org/packages/4c/48/51d767691973e046887f5e6d96e32142fe296823b163ccba732233a6ef72/pyqwest-0.10.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:1aaccd8a9db9430b2aedb5bad8ead80742cbc056b85c229516c70dc80539f906", size = 5803874, upload-time = "2026-08-21T06:08:25.096Z" }, + { url = "https://files.pythonhosted.org/packages/58/0a/d2834ccc6e59ad110718895cc65ff2a68aa6e010f0ba8fbe42a57ea33c21/pyqwest-0.10.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:73d9eb438ab4a957a1ce0619d3af8c1c1126bfb9181033b123d792fcf4224531", size = 5981518, upload-time = "2026-08-21T06:08:27.594Z" }, + { url = "https://files.pythonhosted.org/packages/75/f3/274b4c268e9a55fbdb1b3637ac50b5bf42cd3a85d1cfbdc15c602a7b0d9c/pyqwest-0.10.0-cp312-cp312-win_amd64.whl", hash = "sha256:317a74d633abe3bc5bccabf479e069c515dab9e6a755274b0ccb1d8a5bbfede3", size = 4870638, upload-time = "2026-08-21T06:08:29.421Z" }, + { url = "https://files.pythonhosted.org/packages/8d/3d/8b1092f25159bf61a9470ebd35438c669b91ef553a7ee205bdec8006107b/pyqwest-0.10.0-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:3978e794b9cfd8eaa500fb5d7aee63bc6172c605efa0abc1f62d85485bc049e1", size = 5273599, upload-time = "2026-08-21T06:08:31.107Z" }, + { url = "https://files.pythonhosted.org/packages/75/10/54a9786123942b124c2afb9562b74e158afec7be40ef0caa0d37f615d379/pyqwest-0.10.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:715991fd4f04862cd7a9d7452daabcdbd74dff4dff55eb20c22d60382dc2a4ed", size = 5122920, upload-time = "2026-08-21T06:08:33.025Z" }, + { url = "https://files.pythonhosted.org/packages/a2/5b/6a6bd76f91e068b9a619f62aef9fe5ef201f859ddbb6b0a11ad3875ecdda/pyqwest-0.10.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c04798bed79c1dfa0e5b0e30fb137124311083490d44d6dfbe068d3dd254349e", size = 5639577, upload-time = "2026-08-21T06:08:34.896Z" }, + { url = "https://files.pythonhosted.org/packages/68/28/12277a24a8dd74b0a7f124c624d9ed58eccb41e2087ff1087a14d348c778/pyqwest-0.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:35b472877e73dd63fed089c2bc8fa198407f005c8c19e0a93f025ebefde01a81", size = 5565835, upload-time = "2026-08-21T06:08:36.567Z" }, + { url = "https://files.pythonhosted.org/packages/9e/b6/b7da4a3f1fe43600154ae75e91ba7969024d886d60077dd3a1ba8e66d170/pyqwest-0.10.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:564ec360b7848b35e009038ffbca00466305a9708ab21829477f64aa8cad4c64", size = 5803078, upload-time = "2026-08-21T06:08:38.362Z" }, + { url = "https://files.pythonhosted.org/packages/0c/6f/0c9ba210f49f232289afaa8f06369c5f135ac786e1ca0cb22243b7f1fe2c/pyqwest-0.10.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:b5c80e88a5967c1cadb3237c450f91a84a3683f8838c8dca96f09fee3612e762", size = 5980431, upload-time = "2026-08-21T06:08:39.967Z" }, + { url = "https://files.pythonhosted.org/packages/9b/f5/306eeed41a3cd3100247e6e442f4345277b70f1d24efe1641181b14839cd/pyqwest-0.10.0-cp313-cp313-win_amd64.whl", hash = "sha256:dc3d80b402fb59dbe015e25993ac8147456fb231a4c949f92a89f31315ad50f9", size = 4870198, upload-time = "2026-08-21T06:08:41.687Z" }, + { url = "https://files.pythonhosted.org/packages/64/18/0086a408e7cbf39dab18fa5b7e42c969a98382da5a4e6debe40f05acc6a1/pyqwest-0.10.0-cp314-cp314-macosx_10_12_x86_64.whl", hash = "sha256:23a28beb55fa6d975949bffae4adfb69378f3229bb5cbd71231e95bf66f5b26c", size = 5274542, upload-time = "2026-08-21T06:08:43.323Z" }, + { url = "https://files.pythonhosted.org/packages/0c/7b/e1b9aaaf7596e4faaa53cefc2efaca4e3cde721e308e6385e366361cfdcc/pyqwest-0.10.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:e4415ae40b8eedb1713dab14d7f9fecc3f79d26f3206c561087b88b99d5ce24b", size = 5127922, upload-time = "2026-08-21T06:08:45.116Z" }, + { url = "https://files.pythonhosted.org/packages/75/cd/e6d68bb1de5dd26100fcfc878cbd67c402a928774edf1e8ae304c5a84f5b/pyqwest-0.10.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:14b875d2273212d7fa8e4b755d8d736ffd226b1c707a9c0017dfdc8393a96eca", size = 5645856, upload-time = "2026-08-21T06:08:47.055Z" }, + { url = "https://files.pythonhosted.org/packages/f7/48/8c9f9f0467c41f6a563146d57a52f8f6d60c0cb09d0fd3ef88ad5a1c442f/pyqwest-0.10.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b5071491e416ea54e3b95bf9ffbed0bd065b093cb96e10a75c3d8f2cbe3c9823", size = 5569831, upload-time = "2026-08-21T06:08:48.78Z" }, + { url = "https://files.pythonhosted.org/packages/16/ac/c85ab70c6c72078d49a82da76b820e46aaf95a3f6fe271dac955ac195d21/pyqwest-0.10.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:b68b5e68d513a4c63a072f8f40e38015160cf90bfbf7e8ef7c3935ca87e9e022", size = 5806735, upload-time = "2026-08-21T06:08:50.652Z" }, + { url = "https://files.pythonhosted.org/packages/66/db/ad7375b22fb2d0807431dcc9bc2aaf840e374c298cd15071024d5f6dd6d1/pyqwest-0.10.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:c48910d27820b9c46fcd001b0fe514a3cf47d4784f59512dcdb8c91c395f82e4", size = 5984786, upload-time = "2026-08-21T06:08:52.377Z" }, + { url = "https://files.pythonhosted.org/packages/17/88/c449a772afe129683fd7acc657cbc7c69bec085dc75b6e8710a50fbb44e7/pyqwest-0.10.0-cp314-cp314-win_amd64.whl", hash = "sha256:d03ba2cd17948b623a6210981d342eb122546d8a8e910ec77511aff4b1acdd00", size = 4872288, upload-time = "2026-08-21T06:08:54.101Z" }, + { url = "https://files.pythonhosted.org/packages/3d/f8/439ffc0ee12cd7d9b57ac07ccea78ad3ba66b0d6817d429dd661d73308c4/pyqwest-0.10.0-cp314-cp314t-macosx_10_12_x86_64.whl", hash = "sha256:07a0eb595f4096232c2d22549b6e4612c1ecada7934e46462c2c37ce14a89cfb", size = 5256823, upload-time = "2026-08-21T06:08:55.681Z" }, + { url = "https://files.pythonhosted.org/packages/df/2b/72ecd27d104d2b4284710194cce796d607674966c3ea66436768e812a66a/pyqwest-0.10.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:26401baf7dafc71c8d12d2e8389519d141e6f7c14094d0dd4cf9ec1d3b5555bd", size = 5112624, upload-time = "2026-08-21T06:08:57.366Z" }, + { url = "https://files.pythonhosted.org/packages/a4/24/0fb89c3f7d5a0410fcb7560588b4c741ef24b19195c307d4263f04e75c2b/pyqwest-0.10.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e5e3c436e041d8873ce5bb0fdcf9f9e86f5604e8f0ef9e03149efebd8cb474f6", size = 5631844, upload-time = "2026-08-21T06:08:59.165Z" }, + { url = "https://files.pythonhosted.org/packages/4c/4f/921d14754a186f0143ad62b50108dd808328e347e98e9dafab3897eeb405/pyqwest-0.10.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:09364115761579eabfc79d1e954cdb3ded508dac1903fac7285d4c6f058c683f", size = 5555869, upload-time = "2026-08-21T06:09:00.783Z" }, + { url = "https://files.pythonhosted.org/packages/b8/70/504780417319a626fe9549a7e6f9020a3d448eddf8a09617238a3426e90c/pyqwest-0.10.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:559674a98a8b1217e1830ecd41c9905bf2b60983c6b8017063dfac199f00727c", size = 5793738, upload-time = "2026-08-21T06:09:03.324Z" }, + { url = "https://files.pythonhosted.org/packages/45/f7/8d0a5b8a3289f4300dc9005ebde75d316f2371a2671617f942036337731a/pyqwest-0.10.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:f399a696392fff3db3eef0a18ef65b8a3b8396d129193487d966b8eb11006376", size = 5972889, upload-time = "2026-08-21T06:09:05.2Z" }, + { url = "https://files.pythonhosted.org/packages/89/c4/f4c781e475c451cb5f4762a2f814a1750ef375b8db4db09bb2dcea03c4e5/pyqwest-0.10.0-cp314-cp314t-win_amd64.whl", hash = "sha256:0f9163d6dd991bf1bf27308ba38ba021af660b15fffa47ebca98e41cf6f00309", size = 4858036, upload-time = "2026-08-21T06:09:06.926Z" }, +] + +[[package]] +name = "pyrefly" +version = "1.2.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/89/01/a86e9f24722b095c3f88e3616132b75a21b0df53804bdc6a45314dd4d93c/pyrefly-1.2.0.tar.gz", hash = "sha256:5485f960fc2481617068c918335c39ab1507ef90b6b5bd35bf57726e60e73185", size = 6243654, upload-time = "2026-08-01T02:56:27.592Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7d/9d/3c0ef1d4843987b22f996ed381ec9cf5a3b1273e29804db276252e4c95eb/pyrefly-1.2.0-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:7f46d983ac49ddd2b043694960a01dc6a19a5cfd8eec609d6bd9c42866f91b4e", size = 14026305, upload-time = "2026-08-01T02:56:02.611Z" }, + { url = "https://files.pythonhosted.org/packages/0a/06/03bbb78fbea54cdc65b626619f3597d5611aca4fdef11e72a4e8360e7e63/pyrefly-1.2.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:756f669b5555090f5c1a4fef30db1785fabe657764f7e4e6dc88994dfb8ca82d", size = 13463880, upload-time = "2026-08-01T02:56:04.93Z" }, + { url = "https://files.pythonhosted.org/packages/13/5a/7d8bc00a38e93bbc9c3e7bd14d305f7948717e667c9bcddeab9dd42fd255/pyrefly-1.2.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e3465812ce5ef4781fb592edbf2724547296f0a3124be115d73c7e8b2401862d", size = 13907329, upload-time = "2026-08-01T02:56:07.104Z" }, + { url = "https://files.pythonhosted.org/packages/be/94/9e08b4bf799d0b8f36b55a2783c7ba5f51730cf0632a85a67b5b5ed876cd/pyrefly-1.2.0-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:5de7b2ad2bba5c8055181681a84b74143eac2234a48ba5d1b7ed7e7a722b02bd", size = 15039020, upload-time = "2026-08-01T02:56:09.208Z" }, + { url = "https://files.pythonhosted.org/packages/5b/bd/bca5fd0c80f4daf8ee6903a29df9f3de1feb05ff0946b8f35ec8c5096b13/pyrefly-1.2.0-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:25822ea9505f589ea8a725e4268b475132fb89e038fbf092e446510443ac142a", size = 14986199, upload-time = "2026-08-01T02:56:11.924Z" }, + { url = "https://files.pythonhosted.org/packages/97/f7/f07087f3d185ad2eced0c56cef89ca5474dfb4ff25f146cd50a861c97553/pyrefly-1.2.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:90efe75e17491ef5d636e10469e9278d7d0256b3b4c5e1f4750069bf3ae0f5d1", size = 14393715, upload-time = "2026-08-01T02:56:14.143Z" }, + { url = "https://files.pythonhosted.org/packages/d3/70/0d142c320e284b9e3ce35e9b1e58b8ce2ee1f578f2a7234bc30e5022b94f/pyrefly-1.2.0-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:368aaf7eee4f511ddc0f8e564cf14e01ab2f10b0db9105c6d5b153bf498d07bf", size = 13933008, upload-time = "2026-08-01T02:56:16.525Z" }, + { url = "https://files.pythonhosted.org/packages/5d/e8/e84f11b6e1f63fd453ad3654213b9a0f6f4de8cef6b58038eef2d0d5955d/pyrefly-1.2.0-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:d52d5da7bc65fb7675fbaa80eda879d4f8787c494f04cac21603330d3abbdbbe", size = 14431827, upload-time = "2026-08-01T02:56:18.645Z" }, + { url = "https://files.pythonhosted.org/packages/0f/06/810d31380f66c75e1c0779a408d3b16117b1b368b57894f6aa66bef21686/pyrefly-1.2.0-py3-none-win32.whl", hash = "sha256:8c90751de8506d938e8f802659c74cf35bd7a0036510ee6c634a38eebb280bfa", size = 13229447, upload-time = "2026-08-01T02:56:20.921Z" }, + { url = "https://files.pythonhosted.org/packages/ed/98/4dafa3c7a1caed2dc8cc708dde09ba27963c7736508f55b626fff3024113/pyrefly-1.2.0-py3-none-win_amd64.whl", hash = "sha256:8a8964c224ccc4882730130955815de21ff443c1ac3f0b90685b19bf63848170", size = 14087387, upload-time = "2026-08-01T02:56:23.188Z" }, + { url = "https://files.pythonhosted.org/packages/1b/1c/df3cb0a2e5591660ded7a1836cd2f29dc48c91adb1c0a3a700a96f6d09e1/pyrefly-1.2.0-py3-none-win_arm64.whl", hash = "sha256:3a90bb8df39dfbac74b1f3b2e9d7c526b8f80568884c3944d955023a73ebf61e", size = 13430873, upload-time = "2026-08-01T02:56:25.425Z" }, ] [[package]] @@ -3728,7 +3719,7 @@ wheels = [ [[package]] name = "transformers" -version = "5.10.1" +version = "5.6.2" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "huggingface-hub" }, @@ -3741,9 +3732,9 @@ dependencies = [ { name = "tqdm" }, { name = "typer" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/65/d3/5b7c2f1a52ff0e57355efdc21554aab7e4602f6592ab4582a34c988ab956/transformers-5.10.1.tar.gz", hash = "sha256:31112d1dcdfcf9934242acbba891f44e2279ff74b9b8ba4595640e0e04195a3a", size = 8798372, upload-time = "2026-06-03T15:37:03.289Z" } +sdist = { url = "https://files.pythonhosted.org/packages/a4/e9/c6c80a07690142a7d05444271f47b9f3c8aac7dea01d52e1137ee480ad78/transformers-5.6.2.tar.gz", hash = "sha256:e657134c3e5a6bc00a3c35f4e2674bb51adfcd89898495b788a18552bac2b91a", size = 8311867, upload-time = "2026-04-23T18:33:29.332Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/e3/8c/3119596c7fcd9b8b8d924d5b5ba1bfdfafd6d8738bdd81ca09c60b4a38b3/transformers-5.10.1-py3-none-any.whl", hash = "sha256:ccb919ea1b77338b44d0d45d23f7472081906b1bb6ed8e5f5cf4d692d1da03d4", size = 11003770, upload-time = "2026-06-03T15:37:00.433Z" }, + { url = "https://files.pythonhosted.org/packages/5d/95/0b0218149b0d6f14df35f5b8f676fa83df4f19ed253c3cc447107ef86eca/transformers-5.6.2-py3-none-any.whl", hash = "sha256:f8d3a1bb96778fed9b8aabfd0dd6e19843e4b0f2bb6b59f32b8a92051b0f348f", size = 10364898, upload-time = "2026-04-23T18:33:26.081Z" }, ] [[package]] diff --git a/e2e/features/step-definitions/agent-v2/access-point-helpers.ts b/e2e/features/step-definitions/agent-v2/access-point-helpers.ts index 654455d1283..f0e5c01d776 100644 --- a/e2e/features/step-definitions/agent-v2/access-point-helpers.ts +++ b/e2e/features/step-definitions/agent-v2/access-point-helpers.ts @@ -28,7 +28,7 @@ export const getAccessRegion = (world: DifyWorld) => export type AccessSurfaceName = 'Web app' | 'Backend service API' export const getAccessSurfaceCard = (world: DifyWorld, surface: AccessSurfaceName) => - getAccessRegion(world).getByRole('article', { name: surface }).first() + getAccessRegion(world).getByRole('region', { name: surface }).first() export const getWebAppCard = (world: DifyWorld) => getAccessSurfaceCard(world, 'Web app') diff --git a/e2e/features/step-definitions/agent-v2/access-point-web-app.steps.ts b/e2e/features/step-definitions/agent-v2/access-point-web-app.steps.ts index 53eba93fdae..a71d04f295d 100644 --- a/e2e/features/step-definitions/agent-v2/access-point-web-app.steps.ts +++ b/e2e/features/step-definitions/agent-v2/access-point-web-app.steps.ts @@ -22,9 +22,9 @@ Then('I should see the Agent v2 Web app access URL', async function (this: DifyW const webAppCard = getWebAppCard(this) await expect(webAppCard.getByRole('heading', { name: 'Web app' })).toBeVisible() - await expect(webAppCard.getByText('Web App URL')).toBeVisible() + await expect(webAppCard.getByText('Access URL')).toBeVisible() await expect(webAppCard.getByLabel('Copy access URL')).toBeEnabled() - await expect(webAppCard.getByRole('link', { name: 'Launch' })).toBeVisible() + await expect(webAppCard.getByRole('link', { name: 'Open' })).toBeVisible() }) When('I copy the Agent v2 Web app access URL', async function (this: DifyWorld) { @@ -38,11 +38,11 @@ Then('the Agent v2 Web app access URL should show it was copied', async function When('I launch the Agent v2 Web app', async function (this: DifyWorld) { await recordComposerDraftSnapshot(this) - const launchLink = getWebAppCard(this).getByRole('link', { name: 'Launch' }) - const href = await launchLink.getAttribute('href') - if (!href) throw new Error('Agent v2 Web app Launch link does not expose an href.') + const openLink = getWebAppCard(this).getByRole('link', { name: 'Open' }) + const href = await openLink.getAttribute('href') + if (!href) throw new Error('Agent v2 Web app Open link does not expose an href.') - const [webAppPage] = await Promise.all([this.getPage().waitForEvent('popup'), launchLink.click()]) + const [webAppPage] = await Promise.all([this.getPage().waitForEvent('popup'), openLink.click()]) this.agentBuilder.accessPoint.webAppURL = href this.agentBuilder.accessPoint.webAppPage = webAppPage @@ -130,7 +130,7 @@ When('I close the Agent v2 Web app', async function (this: DifyWorld) { When('I open Agent v2 Embedded configuration', async function (this: DifyWorld) { await recordComposerDraftSnapshot(this) - await getWebAppCard(this).getByRole('button', { name: 'Embedded' }).click() + await getWebAppCard(this).getByRole('button', { name: 'Embed Into Site' }).click() }) Then('I should see the Agent v2 Embedded configuration dialog', async function (this: DifyWorld) { @@ -143,7 +143,7 @@ Then('I should see the Agent v2 Embedded configuration dialog', async function ( When('I open Agent v2 Web app customization', async function (this: DifyWorld) { await recordComposerDraftSnapshot(this) - await getWebAppCard(this).getByRole('button', { name: 'Custom Frontend' }).click() + await getWebAppCard(this).getByRole('button', { name: 'Custom frontend' }).click() }) Then('I should see the Agent v2 Web app customization dialog', async function (this: DifyWorld) { @@ -156,7 +156,7 @@ Then('I should see the Agent v2 Web app customization dialog', async function (t When('I open Agent v2 Web app settings', async function (this: DifyWorld) { await recordComposerDraftSnapshot(this) - await getWebAppCard(this).getByRole('button', { name: 'Branding' }).click() + await getWebAppCard(this).getByRole('button', { name: 'Settings' }).click() }) Then('I should see the Agent v2 Web app settings dialog', async function (this: DifyWorld) { diff --git a/e2e/features/step-definitions/agent-v2/access-point.steps.ts b/e2e/features/step-definitions/agent-v2/access-point.steps.ts index 38706e41a7d..956d4f0b776 100644 --- a/e2e/features/step-definitions/agent-v2/access-point.steps.ts +++ b/e2e/features/step-definitions/agent-v2/access-point.steps.ts @@ -81,7 +81,7 @@ Then( await expect(webAppCard.getByText('Out of service')).toBeVisible({ timeout: 30_000 }) await expect(webAppCard.getByLabel('Toggle Web app access')).toBeDisabled() - await expect(webAppCard.getByRole('button', { name: 'Launch' })).toBeDisabled() + await expect(webAppCard.getByRole('button', { name: 'Open' })).toBeDisabled() await expect(serviceApiCard.getByText('Out of service')).toBeVisible() await expect(serviceApiCard.getByLabel('Toggle Backend service API access')).toBeDisabled() await expect(serviceApiCard.getByRole('button', { name: /^API Key\b/ })).toBeDisabled() @@ -94,9 +94,9 @@ When( const accessSurfaceCard = getAccessSurfaceCard(this, surface) if (surface === 'Web app') { - const launchLink = accessSurfaceCard.getByRole('link', { name: 'Launch' }) - const href = await launchLink.getAttribute('href') - if (!href) throw new Error('Agent v2 Web app Launch link does not expose an href.') + const openLink = accessSurfaceCard.getByRole('link', { name: 'Open' }) + const href = await openLink.getAttribute('href') + if (!href) throw new Error('Agent v2 Web app Open link does not expose an href.') this.agentBuilder.accessPoint.webAppURL = href } @@ -122,7 +122,7 @@ Then( await expect(toggle).toBeEnabled() await expect(toggle).toHaveAttribute('aria-checked', 'false') if (surface === 'Web app') - await expect(accessSurfaceCard.getByRole('button', { name: 'Launch' })).toBeDisabled() + await expect(accessSurfaceCard.getByRole('button', { name: 'Open' })).toBeDisabled() }, ) @@ -136,6 +136,6 @@ Then( await expect(toggle).toBeEnabled() await expect(toggle).toHaveAttribute('aria-checked', 'true') if (surface === 'Web app') - await expect(accessSurfaceCard.getByRole('link', { name: 'Launch' })).toBeVisible() + await expect(accessSurfaceCard.getByRole('link', { name: 'Open' })).toBeVisible() }, ) diff --git a/e2e/features/step-definitions/apps/create-app.steps.ts b/e2e/features/step-definitions/apps/create-app.steps.ts index 88113afa3fb..f632f310a33 100644 --- a/e2e/features/step-definitions/apps/create-app.steps.ts +++ b/e2e/features/step-definitions/apps/create-app.steps.ts @@ -20,6 +20,10 @@ const getLatestCreatedAppId = (world: DifyWorld) => { return appId } +const expectAppEditorContent = async (world: DifyWorld) => { + await expect(world.getPage().getByRole('link', { name: 'Orchestrate' })).toBeVisible() +} + When('I start creating a blank app', async function (this: DifyWorld) { await openBlankAppCreation(this.getPage()) }) @@ -78,14 +82,17 @@ Then('I should land on the app editor', async function (this: DifyWorld) { await expect(this.getPage()).toHaveURL( new RegExp(`/app/${appId}/(workflow|configuration)(?:\\?.*)?$`), ) + await expectAppEditorContent(this) }) Then('I should land on the workflow editor', async function (this: DifyWorld) { const appId = getLatestCreatedAppId(this) await expect(this.getPage()).toHaveURL(new RegExp(`/app/${appId}/workflow(?:\\?.*)?$`)) + await expectAppEditorContent(this) }) Then('I should land on the app configuration page', async function (this: DifyWorld) { const appId = getLatestCreatedAppId(this) await expect(this.getPage()).toHaveURL(new RegExp(`/app/${appId}/configuration(?:\\?.*)?$`)) + await expectAppEditorContent(this) }) diff --git a/knip.config.ts b/knip.config.ts index 2914f398f8c..3931cfd7a2f 100644 --- a/knip.config.ts +++ b/knip.config.ts @@ -15,6 +15,10 @@ const config: KnipConfig = { 'tsslint.config.ts', 'dev-proxy.config.ts', 'plugins/eslint/index.js', + // Public surface consumed by the standalone Marketplace host. + // The `!` suffix keeps these entries in `knip --production`. + 'app/components/plugins/marketplace/standalone/server.ts!', + 'app/components/plugins/marketplace/standalone/client.ts!', ], project: [ '**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts,css,mdx}!', diff --git a/lint.config.ts b/lint.config.ts index e0cd7283be0..7a47e29a00d 100644 --- a/lint.config.ts +++ b/lint.config.ts @@ -771,6 +771,7 @@ export const lintConfig = { '@tanstack/query/infinite-query-property-order': 'error', '@tanstack/query/no-void-query-fn': 'error', '@tanstack/query/mutation-property-order': 'error', + '@tanstack/query/prefer-query-options': 'error', 'react/exhaustive-deps': 'warn', 'react/no-array-index-key': 'warn', 'react/no-clone-element': 'warn', diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index d5e612f84e0..fc508e12a38 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -191,12 +191,6 @@ "eslint-react/set-state-in-effect": { "count": 5 }, - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 1 - }, "typescript/no-explicit-any": { "count": 1 } @@ -295,12 +289,6 @@ "eslint-react/set-state-in-effect": { "count": 1 }, - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 1 - }, "typescript/no-explicit-any": { "count": 4 } @@ -414,6 +402,16 @@ "count": 1 } }, + "web/app/components/app/deploy/shared/deployment-configuration/use-deployment-configuration-queries.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, + "web/app/components/app/in-site-message/__tests__/notification.spec.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, "web/app/components/app/log/filter.tsx": { "react/only-export-components": { "count": 1 @@ -608,14 +606,6 @@ "count": 3 } }, - "web/app/components/base/chat/chat-with-history/sidebar/item.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 2 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 2 - } - }, "web/app/components/base/chat/chat/__tests__/hooks.spec.tsx": { "no-restricted-imports": { "count": 1 @@ -705,14 +695,6 @@ "count": 10 } }, - "web/app/components/base/date-and-time-picker/date-picker/index.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 1 - } - }, "web/app/components/base/date-and-time-picker/hooks.ts": { "eslint-react/no-unnecessary-use-prefix": { "count": 2 @@ -1129,7 +1111,7 @@ }, "web/app/components/base/icons/src/vender/plugin/index.ts": { "no-barrel-files/no-barrel-files": { - "count": 3 + "count": 2 } }, "web/app/components/base/icons/src/vender/solid/FinanceAndECommerce/index.ts": { @@ -1207,14 +1189,6 @@ "count": 3 } }, - "web/app/components/base/image-gallery/index.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-noninteractive-element-interactions": { - "count": 1 - } - }, "web/app/components/base/image-uploader/__tests__/image-preview.spec.tsx": { "erasable-syntax-only/parameter-properties": { "count": 1 @@ -1238,14 +1212,6 @@ "count": 1 } }, - "web/app/components/base/image-uploader/image-list.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-noninteractive-element-interactions": { - "count": 1 - } - }, "web/app/components/base/image-uploader/image-preview.tsx": { "jsx-a11y/no-static-element-interactions": { "count": 1 @@ -1307,12 +1273,6 @@ } }, "web/app/components/base/markdown-blocks/link.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-noninteractive-element-interactions": { - "count": 1 - }, "typescript/no-explicit-any": { "count": 1 } @@ -1626,14 +1586,6 @@ "count": 1 } }, - "web/app/components/base/tab-slider-plain/index.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 1 - } - }, "web/app/components/base/tag-input/index.stories.tsx": { "jsx-a11y/label-has-associated-control": { "count": 12 @@ -1696,11 +1648,6 @@ "count": 3 } }, - "web/app/components/datasets/chunk.tsx": { - "jsx-a11y/label-has-associated-control": { - "count": 2 - } - }, "web/app/components/datasets/common/document-status-with-action/status-with-action.tsx": { "eslint-react/static-components": { "count": 2 @@ -1792,14 +1739,6 @@ "count": 2 } }, - "web/app/components/datasets/create/file-uploader/components/upload-dropzone.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-noninteractive-element-interactions": { - "count": 1 - } - }, "web/app/components/datasets/create/file-uploader/hooks/use-file-upload.ts": { "no-restricted-imports": { "count": 1 @@ -1961,14 +1900,6 @@ "count": 2 } }, - "web/app/components/datasets/documents/create-from-pipeline/data-source/local-file/components/upload-dropzone.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-noninteractive-element-interactions": { - "count": 1 - } - }, "web/app/components/datasets/documents/create-from-pipeline/data-source/online-documents/index.tsx": { "no-restricted-imports": { "count": 1 @@ -2135,6 +2066,11 @@ "count": 1 } }, + "web/app/components/datasets/documents/detail/embedding/hooks/use-embedding-status.ts": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, "web/app/components/datasets/documents/detail/metadata/index.tsx": { "no-barrel-files/no-barrel-files": { "count": 1 @@ -2153,12 +2089,6 @@ "web/app/components/datasets/external-knowledge-base/create/ExternalApiSelect.tsx": { "eslint-react/set-state-in-effect": { "count": 1 - }, - "jsx-a11y/click-events-have-key-events": { - "count": 3 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 3 } }, "web/app/components/datasets/external-knowledge-base/create/ExternalApiSelection.tsx": { @@ -2362,6 +2292,11 @@ "count": 4 } }, + "web/app/components/header/account-setting/model-provider-page/hooks.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/app/components/header/account-setting/model-provider-page/model-auth/add-custom-model.tsx": { "jsx-a11y/click-events-have-key-events": { "count": 2 @@ -2464,6 +2399,11 @@ "count": 3 } }, + "web/app/components/header/account-setting/model-provider-page/provider-added-card/provider-card-actions.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/app/components/header/account-setting/model-provider-page/utils.ts": { "no-barrel-files/no-barrel-files": { "count": 2 @@ -2477,6 +2417,11 @@ "count": 1 } }, + "web/app/components/header/github-star/index.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, "web/app/components/main-nav/components/workspace-switcher.tsx": { "jsx-a11y/no-autofocus": { "count": 1 @@ -2542,15 +2487,15 @@ } }, "web/app/components/plugins/marketplace/hooks.ts": { + "@tanstack/query/prefer-query-options": { + "count": 3 + }, "no-restricted-imports": { "count": 1 } }, - "web/app/components/plugins/marketplace/list/list-with-collection.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { + "web/app/components/plugins/marketplace/query.ts": { + "@tanstack/query/prefer-query-options": { "count": 1 } }, @@ -2594,9 +2539,6 @@ } }, "web/app/components/plugins/plugin-detail-panel/app-selector/app-inputs-form.tsx": { - "no-restricted-imports": { - "count": 1 - }, "typescript/no-explicit-any": { "count": 8 } @@ -2975,9 +2917,6 @@ "eslint-react/set-state-in-effect": { "count": 1 }, - "no-restricted-imports": { - "count": 1 - }, "typescript/no-explicit-any": { "count": 3 } @@ -2995,6 +2934,11 @@ "count": 1 } }, + "web/app/components/snippet-list/__tests__/reflow.browser.spec.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/app/components/snippets/components/snippet-run-panel.tsx": { "jsx-a11y/click-events-have-key-events": { "count": 5 @@ -3348,14 +3292,6 @@ "count": 2 } }, - "web/app/components/workflow/nodes/_base/components/form-input-boolean.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 2 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 2 - } - }, "web/app/components/workflow/nodes/_base/components/form-input-item.tsx": { "no-restricted-imports": { "count": 1 @@ -3595,6 +3531,11 @@ "count": 1 } }, + "web/app/components/workflow/nodes/agent-v2/__tests__/hooks.spec.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 6 + } + }, "web/app/components/workflow/nodes/agent/default.ts": { "typescript/no-explicit-any": { "count": 3 @@ -3728,14 +3669,6 @@ "count": 1 } }, - "web/app/components/workflow/nodes/http/components/authorization/radio-group.tsx": { - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 1 - } - }, "web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx": { "typescript/no-explicit-any": { "count": 2 @@ -4491,12 +4424,6 @@ "eslint-react/set-state-in-effect": { "count": 4 }, - "jsx-a11y/click-events-have-key-events": { - "count": 1 - }, - "jsx-a11y/no-static-element-interactions": { - "count": 1 - }, "no-restricted-imports": { "count": 1 } @@ -4862,6 +4789,11 @@ "count": 1 } }, + "web/app/signin/normal-form.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, "web/app/signup/layout.tsx": { "typescript/no-explicit-any": { "count": 1 @@ -4880,6 +4812,36 @@ "count": 1 } }, + "web/features/agent-v2/agent-detail/configure/components/orchestrate/files/__tests__/index.spec.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, + "web/features/agent-v2/agent-detail/configure/components/preview/working-directory-panel.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 4 + } + }, + "web/features/agent-v2/agent-detail/configure/use-agent-configure-build-draft.ts": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, + "web/features/agent-v2/agent-detail/logs/__tests__/page.spec.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, + "web/features/new-rag/__tests__/use-query-data-update-count.spec.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, + "web/features/skills/detail/file-editor.tsx": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/features/tag-management/components/tag-item-editor.tsx": { "jsx-a11y/no-autofocus": { "count": 1 @@ -4981,21 +4943,33 @@ } }, "web/service/access-control/use-app-access-control.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + }, "no-restricted-imports": { "count": 1 } }, "web/service/access-control/use-member-roles.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + }, "no-restricted-imports": { "count": 1 } }, "web/service/access-control/use-workspace-access-rules.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + }, "no-restricted-imports": { "count": 1 } }, "web/service/access-control/use-workspace-roles.ts": { + "@tanstack/query/prefer-query-options": { + "count": 6 + }, "no-restricted-imports": { "count": 1 } @@ -5029,6 +5003,11 @@ "count": 3 } }, + "web/service/common.spec.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/service/common.ts": { "no-restricted-imports": { "count": 1 @@ -5072,11 +5051,17 @@ } }, "web/service/knowledge/use-dataset.ts": { + "@tanstack/query/prefer-query-options": { + "count": 8 + }, "no-restricted-imports": { "count": 1 } }, "web/service/knowledge/use-document.ts": { + "@tanstack/query/prefer-query-options": { + "count": 4 + }, "no-restricted-imports": { "count": 1 } @@ -5087,16 +5072,25 @@ } }, "web/service/knowledge/use-import.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + }, "no-restricted-imports": { "count": 1 } }, "web/service/knowledge/use-metadata.ts": { + "@tanstack/query/prefer-query-options": { + "count": 8 + }, "no-restricted-imports": { "count": 1 } }, "web/service/knowledge/use-segment.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + }, "no-restricted-imports": { "count": 1 } @@ -5142,12 +5136,23 @@ "count": 1 } }, + "web/service/use-common.ts": { + "@tanstack/query/prefer-query-options": { + "count": 12 + } + }, "web/service/use-datasource.ts": { + "@tanstack/query/prefer-query-options": { + "count": 3 + }, "no-restricted-imports": { "count": 1 } }, "web/service/use-endpoints.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + }, "no-restricted-imports": { "count": 1 }, @@ -5155,27 +5160,44 @@ "count": 7 } }, + "web/service/use-explore.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/service/use-flow.ts": { "eslint-react/no-unnecessary-use-prefix": { "count": 1 } }, "web/service/use-log.ts": { + "@tanstack/query/prefer-query-options": { + "count": 7 + }, "no-restricted-imports": { "count": 1 } }, "web/service/use-models.ts": { + "@tanstack/query/prefer-query-options": { + "count": 3 + }, "no-restricted-imports": { "count": 1 } }, "web/service/use-pipeline.ts": { + "@tanstack/query/prefer-query-options": { + "count": 9 + }, "no-restricted-imports": { "count": 1 } }, "web/service/use-plugins-auth.ts": { + "@tanstack/query/prefer-query-options": { + "count": 3 + }, "no-restricted-imports": { "count": 1 }, @@ -5183,12 +5205,53 @@ "count": 4 } }, + "web/service/use-plugins.ts": { + "@tanstack/query/prefer-query-options": { + "count": 13 + } + }, + "web/service/use-share.ts": { + "@tanstack/query/prefer-query-options": { + "count": 8 + } + }, + "web/service/use-snippet-workflows.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, + "web/service/use-snippets.ts": { + "@tanstack/query/prefer-query-options": { + "count": 1 + } + }, + "web/service/use-strategy.ts": { + "@tanstack/query/prefer-query-options": { + "count": 2 + } + }, "web/service/use-tools.ts": { + "@tanstack/query/prefer-query-options": { + "count": 15 + }, "no-restricted-imports": { "count": 1 } }, + "web/service/use-triggers.ts": { + "@tanstack/query/prefer-query-options": { + "count": 6 + } + }, + "web/service/use-try-app.ts": { + "@tanstack/query/prefer-query-options": { + "count": 4 + } + }, "web/service/use-workflow.ts": { + "@tanstack/query/prefer-query-options": { + "count": 9 + }, "no-restricted-imports": { "count": 1 }, @@ -5197,6 +5260,9 @@ } }, "web/service/use-workspace.ts": { + "@tanstack/query/prefer-query-options": { + "count": 1 + }, "no-restricted-imports": { "count": 1 } @@ -5272,4 +5338,4 @@ "count": 2 } } -} +} \ No newline at end of file diff --git a/packages/contracts/generated/api/console/apps/types.gen.ts b/packages/contracts/generated/api/console/apps/types.gen.ts index 3539ca8d6ea..1098bb2809e 100644 --- a/packages/contracts/generated/api/console/apps/types.gen.ts +++ b/packages/contracts/generated/api/console/apps/types.gen.ts @@ -5003,6 +5003,8 @@ export type DeleteAppsByAppIdTraceConfigData = { export type DeleteAppsByAppIdTraceConfigErrors = { 400: unknown 403: unknown + 404: unknown + 500: unknown } export type DeleteAppsByAppIdTraceConfigResponses = { @@ -5025,6 +5027,8 @@ export type GetAppsByAppIdTraceConfigData = { export type GetAppsByAppIdTraceConfigErrors = { 400: unknown + 404: unknown + 500: unknown } export type GetAppsByAppIdTraceConfigResponses = { @@ -5046,6 +5050,8 @@ export type PatchAppsByAppIdTraceConfigData = { export type PatchAppsByAppIdTraceConfigErrors = { 400: unknown 403: unknown + 404: unknown + 500: unknown } export type PatchAppsByAppIdTraceConfigResponses = { @@ -5067,6 +5073,9 @@ export type PostAppsByAppIdTraceConfigData = { export type PostAppsByAppIdTraceConfigErrors = { 400: unknown 403: unknown + 404: unknown + 409: unknown + 500: unknown } export type PostAppsByAppIdTraceConfigResponses = { diff --git a/packages/contracts/generated/api/console/explore/zod.gen.ts b/packages/contracts/generated/api/console/explore/zod.gen.ts index 272b2884146..995a5b98a58 100644 --- a/packages/contracts/generated/api/console/explore/zod.gen.ts +++ b/packages/contracts/generated/api/console/explore/zod.gen.ts @@ -137,7 +137,7 @@ export const zLearnDifyAppListResponseWritable = z.object({ }) export const zGetExploreAppsQuery = z.object({ - language: z.string().optional(), + language: z.string().optional().default('en-US'), }) /** @@ -146,7 +146,7 @@ export const zGetExploreAppsQuery = z.object({ export const zGetExploreAppsResponse = zRecommendedAppListResponse export const zGetExploreAppsLearnDifyQuery = z.object({ - language: z.string().optional(), + language: z.string().optional().default('en-US'), }) /** diff --git a/packages/contracts/generated/api/console/notification/orpc.gen.ts b/packages/contracts/generated/api/console/notification/orpc.gen.ts index 9e01e1a62e0..7024ed95c31 100644 --- a/packages/contracts/generated/api/console/notification/orpc.gen.ts +++ b/packages/contracts/generated/api/console/notification/orpc.gen.ts @@ -3,6 +3,7 @@ import { oc } from '@orpc/contract' import * as z from 'zod' import { + zGetNotificationQuery, zGetNotificationResponse, zPostNotificationDismissBody, zPostNotificationDismissResponse, @@ -28,18 +29,19 @@ export const dismiss = { } /** - * Return the active in-product notification for the current user in their interface language (falls back to English if unavailable). The notification is NOT marked as seen here; call POST /notification/dismiss when the user explicitly closes the modal. + * Return the active in-product notification for the current user in the requested language (defaults to English when omitted). Unavailable translations fall back to English, then the first available content. The notification is NOT marked as seen here; call POST /notification/dismiss when the user explicitly closes the modal. */ export const get = oc .route({ description: - 'Return the active in-product notification for the current user in their interface language (falls back to English if unavailable). The notification is NOT marked as seen here; call POST /notification/dismiss when the user explicitly closes the modal.', + 'Return the active in-product notification for the current user in the requested language (defaults to English when omitted). Unavailable translations fall back to English, then the first available content. The notification is NOT marked as seen here; call POST /notification/dismiss when the user explicitly closes the modal.', inputStructure: 'detailed', method: 'GET', operationId: 'getNotification', path: '/notification', tags: ['console'], }) + .input(z.object({ query: zGetNotificationQuery.optional() })) .output(zGetNotificationResponse) export const notification = { diff --git a/packages/contracts/generated/api/console/notification/types.gen.ts b/packages/contracts/generated/api/console/notification/types.gen.ts index 26469735f21..f89148d641a 100644 --- a/packages/contracts/generated/api/console/notification/types.gen.ts +++ b/packages/contracts/generated/api/console/notification/types.gen.ts @@ -30,7 +30,9 @@ export type NotificationItemResponse = { export type GetNotificationData = { body?: never path?: never - query?: never + query?: { + language?: string + } url: '/notification' } diff --git a/packages/contracts/generated/api/console/notification/zod.gen.ts b/packages/contracts/generated/api/console/notification/zod.gen.ts index fb39eada2d2..4fadd36755a 100644 --- a/packages/contracts/generated/api/console/notification/zod.gen.ts +++ b/packages/contracts/generated/api/console/notification/zod.gen.ts @@ -37,6 +37,10 @@ export const zNotificationResponse = z.object({ should_show: z.boolean(), }) +export const zGetNotificationQuery = z.object({ + language: z.string().optional().default('en-US'), +}) + /** * Success — inspect should_show to decide whether to render the modal */ diff --git a/packages/contracts/generated/enterprise-app-deploy/types.gen.ts b/packages/contracts/generated/enterprise-app-deploy/types.gen.ts index c64c433d4b6..f7c6aa4f4b9 100644 --- a/packages/contracts/generated/enterprise-app-deploy/types.gen.ts +++ b/packages/contracts/generated/enterprise-app-deploy/types.gen.ts @@ -4,6 +4,8 @@ export type ClientOptions = { baseUrl: `${string}://${string}` | (string & {}) } +export type GoogleProtobufValue = unknown + export const EnvironmentStatus = { ENVIRONMENT_STATUS_UNSPECIFIED: 'ENVIRONMENT_STATUS_UNSPECIFIED', ENVIRONMENT_STATUS_PENDING: 'ENVIRONMENT_STATUS_PENDING', @@ -21,11 +23,27 @@ export const ApplicationInteractionStatus = { APPLICATION_INTERACTION_STATUS_FAILED: 'APPLICATION_INTERACTION_STATUS_FAILED', APPLICATION_INTERACTION_STATUS_PARTIAL_SUCCEEDED: 'APPLICATION_INTERACTION_STATUS_PARTIAL_SUCCEEDED', + APPLICATION_INTERACTION_STATUS_STOPPED: 'APPLICATION_INTERACTION_STATUS_STOPPED', + APPLICATION_INTERACTION_STATUS_PAUSED: 'APPLICATION_INTERACTION_STATUS_PAUSED', } as const export type ApplicationInteractionStatus = (typeof ApplicationInteractionStatus)[keyof typeof ApplicationInteractionStatus] +export const ApplicationInteractionSource = { + APPLICATION_INTERACTION_SOURCE_UNSPECIFIED: 'APPLICATION_INTERACTION_SOURCE_UNSPECIFIED', + APPLICATION_INTERACTION_SOURCE_WEB_APP: 'APPLICATION_INTERACTION_SOURCE_WEB_APP', + APPLICATION_INTERACTION_SOURCE_SERVICE_API: 'APPLICATION_INTERACTION_SOURCE_SERVICE_API', + APPLICATION_INTERACTION_SOURCE_TRIGGER: 'APPLICATION_INTERACTION_SOURCE_TRIGGER', + APPLICATION_INTERACTION_SOURCE_EXPLORE: 'APPLICATION_INTERACTION_SOURCE_EXPLORE', + APPLICATION_INTERACTION_SOURCE_DEBUGGER: 'APPLICATION_INTERACTION_SOURCE_DEBUGGER', + APPLICATION_INTERACTION_SOURCE_VALIDATION: 'APPLICATION_INTERACTION_SOURCE_VALIDATION', + APPLICATION_INTERACTION_SOURCE_OPENAPI: 'APPLICATION_INTERACTION_SOURCE_OPENAPI', +} as const + +export type ApplicationInteractionSource = + (typeof ApplicationInteractionSource)[keyof typeof ApplicationInteractionSource] + export const EnvironmentMode = { ENVIRONMENT_MODE_UNSPECIFIED: 'ENVIRONMENT_MODE_UNSPECIFIED', ENVIRONMENT_MODE_SHARED: 'ENVIRONMENT_MODE_SHARED', @@ -124,36 +142,17 @@ export const EnvironmentBackend = { export type EnvironmentBackend = (typeof EnvironmentBackend)[keyof typeof EnvironmentBackend] -export const EnvironmentManagedBy = { - ENVIRONMENT_MANAGED_BY_UNSPECIFIED: 'ENVIRONMENT_MANAGED_BY_UNSPECIFIED', - ENVIRONMENT_MANAGED_BY_SYSTEM: 'ENVIRONMENT_MANAGED_BY_SYSTEM', - ENVIRONMENT_MANAGED_BY_USER: 'ENVIRONMENT_MANAGED_BY_USER', +export const RuntimeState = { + RUNTIME_STATE_UNSPECIFIED: 'RUNTIME_STATE_UNSPECIFIED', + RUNTIME_STATE_UNDEPLOYED: 'RUNTIME_STATE_UNDEPLOYED', + RUNTIME_STATE_RUNNING: 'RUNTIME_STATE_RUNNING', + RUNTIME_STATE_STARTING: 'RUNTIME_STATE_STARTING', + RUNTIME_STATE_STOPPING: 'RUNTIME_STATE_STOPPING', + RUNTIME_STATE_ERROR: 'RUNTIME_STATE_ERROR', + RUNTIME_STATE_UNKNOWN: 'RUNTIME_STATE_UNKNOWN', } as const -export type EnvironmentManagedBy = (typeof EnvironmentManagedBy)[keyof typeof EnvironmentManagedBy] - -export const EnvironmentDeployedAppStatus = { - ENVIRONMENT_DEPLOYED_APP_STATUS_UNSPECIFIED: 'ENVIRONMENT_DEPLOYED_APP_STATUS_UNSPECIFIED', - ENVIRONMENT_DEPLOYED_APP_STATUS_DEPLOYED: 'ENVIRONMENT_DEPLOYED_APP_STATUS_DEPLOYED', - ENVIRONMENT_DEPLOYED_APP_STATUS_DEPLOYING: 'ENVIRONMENT_DEPLOYED_APP_STATUS_DEPLOYING', - ENVIRONMENT_DEPLOYED_APP_STATUS_FAILED: 'ENVIRONMENT_DEPLOYED_APP_STATUS_FAILED', - ENVIRONMENT_DEPLOYED_APP_STATUS_UNDEPLOYED: 'ENVIRONMENT_DEPLOYED_APP_STATUS_UNDEPLOYED', -} as const - -export type EnvironmentDeployedAppStatus = - (typeof EnvironmentDeployedAppStatus)[keyof typeof EnvironmentDeployedAppStatus] - -export const DeploymentStatus = { - DEPLOYMENT_STATUS_UNSPECIFIED: 'DEPLOYMENT_STATUS_UNSPECIFIED', - DEPLOYMENT_STATUS_UNDEPLOYED: 'DEPLOYMENT_STATUS_UNDEPLOYED', - DEPLOYMENT_STATUS_DEPLOYING: 'DEPLOYMENT_STATUS_DEPLOYING', - DEPLOYMENT_STATUS_RUNNING: 'DEPLOYMENT_STATUS_RUNNING', - DEPLOYMENT_STATUS_UNDEPLOYING: 'DEPLOYMENT_STATUS_UNDEPLOYING', - DEPLOYMENT_STATUS_INVALID: 'DEPLOYMENT_STATUS_INVALID', - DEPLOYMENT_STATUS_FAILED: 'DEPLOYMENT_STATUS_FAILED', -} as const - -export type DeploymentStatus = (typeof DeploymentStatus)[keyof typeof DeploymentStatus] +export type RuntimeState = (typeof RuntimeState)[keyof typeof RuntimeState] export const EnvVarValueSource = { ENV_VAR_VALUE_SOURCE_UNSPECIFIED: 'ENV_VAR_VALUE_SOURCE_UNSPECIFIED', @@ -169,6 +168,7 @@ export const EnvVarValueType = { ENV_VAR_VALUE_TYPE_STRING: 'ENV_VAR_VALUE_TYPE_STRING', ENV_VAR_VALUE_TYPE_NUMBER: 'ENV_VAR_VALUE_TYPE_NUMBER', ENV_VAR_VALUE_TYPE_SECRET: 'ENV_VAR_VALUE_TYPE_SECRET', + ENV_VAR_VALUE_TYPE_LLM: 'ENV_VAR_VALUE_TYPE_LLM', } as const export type EnvVarValueType = (typeof EnvVarValueType)[keyof typeof EnvVarValueType] @@ -203,22 +203,17 @@ export type AppEnvironment = { export type ApplicationInteraction = { id: string timestamp: string - workflowRunId: string status: ApplicationInteractionStatus durationSeconds: number totalTokens: string workspace: NamedRef environment: NamedRef app: NamedRef - operator?: Operator - invokeFrom: string - traceId: string - difyTraceId: string - deploymentVersionId: string + operator?: NamedRef + source: ApplicationInteractionSource + version?: WorkflowVersion + traceId?: string error?: string - body?: string - attributesJson?: string - resourceAttributesJson?: string } export type BatchGetSourceVersionDeploymentsRequest = { @@ -262,7 +257,7 @@ export type CreateEnvironmentRequest = { displayName: string description?: string mode: EnvironmentMode - cpuPool: number + cpuPoolMillicores: number namespace?: string maxMemoryMib?: string } @@ -292,12 +287,7 @@ export type CredentialSlot = { last_deployed_credential_id?: string icon?: string icon_dark?: string -} - -export type DashboardApp = { - id: string - workspaceId: string - displayName: string + workflow_as_tool_dependency?: WorkflowAsToolDependency } export type DeleteEnvironmentApiKeyResponse = { @@ -308,6 +298,15 @@ export type DeleteEnvironmentResponse = { [key: string]: unknown } +export type DeleteServiceApiConversationRequest = { + conversationId: string + user: string +} + +export type DeleteServiceApiConversationResponse = { + [key: string]: unknown +} + export type DeployWorkflowResponse = { operation: DeploymentOperationReceipt } @@ -358,8 +357,7 @@ export type Environment = { statusMessage: string lastError?: Error namespace?: string - managedBy?: EnvironmentManagedBy - cpuPool: number + cpuPoolMillicores: number createdAt: string updatedAt: string memory?: RunnerMemory @@ -381,6 +379,14 @@ export type EnvironmentAccess = { enable_api: boolean } +export type EnvironmentActivity = { + environmentId: string + invocationCount: string + failedInvocationCount: string + failedDeploymentCount: string + deployedAppCount: string +} + export type EnvironmentApiKey = { id: string type: string @@ -393,13 +399,15 @@ export type EnvironmentDeployedApp = { deploymentId: string workspace: NamedRef app: NamedRef - status: EnvironmentDeployedAppStatus + runtimeState: RuntimeState currentVersion?: WorkflowVersion deployedAt?: string deployedBy?: Operator latestAttempt?: EnvironmentDeployedAppAttempt sizing?: RunnerSizing occupiesPool?: boolean + recentInvocationCount?: string + versionsBehind?: number } export type EnvironmentDeployedAppAttempt = { @@ -412,13 +420,6 @@ export type EnvironmentDeployedAppAttempt = { finalizedAt?: string } -export type EnvironmentDeployedAppSummary = { - total: number - deployed: number - deploying: number - failed: number -} - export type EnvironmentDeployment = { environment: DeploymentEnvironment deployment?: EnvironmentDeploymentState @@ -435,7 +436,7 @@ export type EnvironmentDeploymentOperation = { } export type EnvironmentDeploymentState = { - status: DeploymentStatus + runtimeState: RuntimeState current_version?: WorkflowVersion versions_behind?: number deployed_at?: number @@ -449,17 +450,17 @@ export type EnvironmentMcpServer = { export type EnvironmentPoolComposition = { topApps?: Array - otherCpu?: number + otherCpuMillicores?: number otherAppCount?: number } export type EnvironmentPoolShare = { app: NamedRef - isolatedCpu: number + isolatedCpuMillicores: number } export type EnvironmentPoolUsage = { - occupiedCpu: number + occupiedCpuMillicores: number appCount: number } @@ -478,10 +479,16 @@ export type EnvironmentTrigger = { [key: string]: unknown } +export type EnvironmentVariableGroup = { + from_app?: WorkflowReference + from_workflow_as_tool?: WorkflowAsToolSource + environment_variable_slots: Array +} + export type EnvironmentVariableInput = { key: string value_source: EnvVarValueSource - value?: string + value?: GoogleProtobufValue } export type EnvironmentVariableSlot = { @@ -490,8 +497,8 @@ export type EnvironmentVariableSlot = { description: string has_configured_value: boolean has_last_deployed_value: boolean - configured_value?: string - last_deployed_value?: string + configured_value?: GoogleProtobufValue + last_deployed_value?: GoogleProtobufValue } export type EnvironmentWebAppAccessModeUpdate = { @@ -540,7 +547,6 @@ export type Error = { | 'APPDEPLOY_APP_LOG_INVALID_TIME_RANGE' | 'APPDEPLOY_APP_LOG_INVALID_CURSOR' | 'APPDEPLOY_APP_LOG_CURSOR_FILTER_MISMATCH' - | 'APPDEPLOY_APP_LOG_ID_INVALID' | 'APPDEPLOY_UNSUPPORTED_NODE_TYPE' | 'APPDEPLOY_UNSUPPORTED_TOOL_PROVIDER_TYPE' | 'APPDEPLOY_TOOL_PROVIDER_TYPE_INVALID' @@ -552,15 +558,12 @@ export type Error = { | 'APPDEPLOY_INVALID_WORKFLOW_ID' | 'APPDEPLOY_INVALID_DEPLOYMENT_VERSION_ID' | 'APPDEPLOY_DEVELOPER_API_URL_NOT_CONFIGURED' - | 'APPDEPLOY_INVALID_DEPLOYMENT_OPERATION_ID' - | 'APPDEPLOY_APP_LOG_EXPORT_RANGE_TOO_WIDE' - | 'APPDEPLOY_APP_LOG_EXPORT_TOO_MANY_ROWS' - | 'APPDEPLOY_APP_LOG_EXPORT_TOO_LARGE' | 'APPDEPLOY_UNAUTHORIZED' | 'APPDEPLOY_FORBIDDEN' | 'APPDEPLOY_APP_RUNNER_AUTH_REQUIRED' | 'APPDEPLOY_APP_RUNNER_INVALID_JOIN_TOKEN' | 'APPDEPLOY_APP_RUNNER_INVALID_CONTROL_TOKEN' + | 'APPDEPLOY_WEB_APP_ACCESS_DENIED' | 'APPDEPLOY_ENVIRONMENT_NOT_FOUND' | 'APPDEPLOY_DEPLOYMENT_NOT_FOUND' | 'APPDEPLOY_REVISION_NOT_FOUND' @@ -571,15 +574,15 @@ export type Error = { | 'APPDEPLOY_ACCESS_SUBJECT_NOT_FOUND' | 'APPDEPLOY_API_KEY_NOT_FOUND' | 'APPDEPLOY_SOURCE_VERSION_NOT_FOUND' - | 'APPDEPLOY_APP_LOG_NOT_FOUND' | 'APPDEPLOY_WORKSPACE_NOT_FOUND' | 'APPDEPLOY_APP_RUNNER_NOT_FOUND' | 'APPDEPLOY_WORKFLOW_NOT_FOUND' - | 'APPDEPLOY_DEPLOYMENT_OPERATION_NOT_FOUND' | 'APPDEPLOY_RUN_FILE_NOT_FOUND' | 'APPDEPLOY_APPLICATION_UNAVAILABLE' | 'APPDEPLOY_TARGET_ENVIRONMENT_REMOVED' | 'APPDEPLOY_VERSION_UNAVAILABLE' + | 'APPDEPLOY_CONVERSATION_NOT_FOUND' + | 'APPDEPLOY_CHAT_MESSAGE_NOT_FOUND' | 'APPDEPLOY_CONFLICT' | 'APPDEPLOY_DEPLOYMENT_IN_PROGRESS' | 'APPDEPLOY_ALREADY_UNDEPLOYED' @@ -618,10 +621,13 @@ export type Error = { | 'APPDEPLOY_ENVIRONMENT_CPU_POOL_EXHAUSTED' | 'APPDEPLOY_RESOURCE_NOT_APPLICABLE_FOR_MODE' | 'APPDEPLOY_ENVIRONMENT_CPU_POOL_BELOW_ALLOCATED' + | 'APPDEPLOY_CHAT_CONTEXT_TOO_LARGE' + | 'APPDEPLOY_FILE_GRANT_UNAVAILABLE' | 'APPDEPLOY_APP_RUNNER_CONTROL_NOT_CONFIGURED' | 'APPDEPLOY_RUNTIME_ASSIGNMENT_FAILED' | 'APPDEPLOY_REVISION_TIMEOUT' | 'APPDEPLOY_INTERNAL_ERROR' + | 'APPDEPLOY_RECEIPT_RETRY' | 'APPDEPLOY_ENVIRONMENT_BOOTSTRAP_AUTH_REJECTED' | 'APPDEPLOY_ENVIRONMENT_BOOTSTRAP_NAMESPACE_MISSING' | 'APPDEPLOY_ENVIRONMENT_BOOTSTRAP_INSUFFICIENT_RBAC' @@ -640,12 +646,14 @@ export type Error = { detailCode?: string } -export type GetApplicationInteractionResponse = { - interaction: ApplicationInteraction +export type GetApplicationInteractionSummaryResponse = { + totalCount: string + failedCount: string + lookbackStart: string } -export type GetDeploymentOperationResponse = { - operation: DeploymentOperation +export type GetEnvironmentActivityResponse = { + data: Array } export type GetEnvironmentCapabilitiesResponse = { @@ -673,12 +681,17 @@ export type GetWebAppAccessModeResponse = { accessMode?: string } +export type GetWebAppLoginStatusResponse = { + logged_in?: boolean + app_logged_in?: boolean +} + export type GetWebAppPermissionResponse = { result?: boolean } export type GetWorkflowDeploymentOptionsResponse = { - environment_variable_slots: Array + environment_variable_groups: Array credential_slots: Array } @@ -686,14 +699,15 @@ export type ListAppEnvironmentsResponse = { data: Array } -export type ListApplicationInteractionsResponse = { - data: Array +export type ListApplicationInteractionAppsResponse = { + data: Array pagination: Pagination } -export type ListAppsResponse = { - data: Array - pagination: Pagination +export type ListApplicationInteractionsResponse = { + data: Array + nextPageToken?: string + previousPageToken?: string } export type ListDeploymentOperationsResponse = { @@ -707,7 +721,6 @@ export type ListEnvironmentApiKeysResponse = { export type ListEnvironmentDeployedAppsResponse = { data: Array - summary: EnvironmentDeployedAppSummary pagination: Pagination } @@ -724,11 +737,34 @@ export type ListEnvironmentsResponse = { pagination: Pagination } +export type ListOperationAppsResponse = { + data: Array + pagination: Pagination +} + +export type MintServiceApiFileGrantRequest = { + tenantId?: string + appId?: string + environmentId?: string + user?: string +} + +export type MintServiceApiFileGrantResponse = { + grant?: string + expiresAt?: string +} + export type NamedRef = { id: string displayName: string } +export type OperationApp = { + id?: string + workspaceId?: string + displayName?: string +} + export type Operator = { type: OperatorType id: string @@ -744,6 +780,20 @@ export type PrepareAppDeletionRequest = { appId?: string } +export type RenameServiceApiConversationRequest = { + conversationId: string + user: string + name?: string + autoGenerate?: boolean +} + +export type RenameWebAppConversationRequest = { + appCode: string + conversationId: string + name?: string + autoGenerate?: boolean +} + export type ResolveApiTokenRouteRequest = { token?: string } @@ -753,17 +803,14 @@ export type ResolveApiTokenRouteResponse = { namespace?: string serviceName?: string servicePort?: number - environmentStatus?: EnvironmentStatus appId?: string tenantId?: string deploymentId?: string servingRevisionId?: string - deploymentStatus?: DeploymentStatus - revoked?: boolean - unavailableReason?: string targetKind?: RouteTargetKind directUpstream?: string - deploymentGeneration?: string + assignmentGeneration?: string + decision?: string } export type ResolveWebAppRouteRequest = { @@ -777,18 +824,18 @@ export type ResolveWebAppRouteResponse = { namespace?: string serviceName?: string servicePort?: number - environmentStatus?: EnvironmentStatus appId?: string tenantId?: string deploymentId?: string servingRevisionId?: string - deploymentStatus?: DeploymentStatus - unavailableReason?: string targetKind?: RouteTargetKind directUpstream?: string - deploymentGeneration?: string - endUserId?: string - authType?: string + assignmentGeneration?: string + userId?: string + userFrom?: string + userAuthType?: string + fileGrant?: string + fileGrantExpiresAt?: string } export type RetryEnvironmentBootstrapRequest = { @@ -806,7 +853,7 @@ export type RunnerMemory = { } export type RunnerSizing = { - isolatedCpu: number + isolatedCpuMillicores: number memory: RunnerMemory } @@ -818,7 +865,12 @@ export type SimpleAccount = { export type SourceVersionDeployment = { sourceVersionId?: string - environments?: Array + environments?: Array +} + +export type SourceVersionDeploymentEnvironment = { + id?: string + name?: string } export type TestConnectionRequest = { @@ -843,6 +895,7 @@ export type UnsupportedNode = { type: string title: string provider?: UnsupportedNodeProvider + workflow_as_tool_dependency?: WorkflowAsToolDependency } export type UnsupportedNodeProvider = { @@ -855,15 +908,15 @@ export type UnsupportedNodeProvider = { export type UpdateEnvironmentDeployedAppResourcesRequest = { environmentId: string deploymentId: string - isolatedCpu: number + isolatedCpuMillicores: number maxMemoryMib?: string } export type UpdateEnvironmentDeployedAppResourcesResponse = { deploymentId: string - isolatedCpu: number - allocatedCpuCount: number - poolCpuCount: number + isolatedCpuMillicores: number + allocatedCpuMillicores: number + poolCpuMillicores: number memory: RunnerMemory } @@ -871,7 +924,7 @@ export type UpdateEnvironmentRequest = { environmentId?: string displayName?: string description?: string - cpuPool?: number + cpuPoolMillicores?: number maxMemoryMib?: string } @@ -879,16 +932,46 @@ export type UpdateEnvironmentResponse = { environment: Environment } -export type WorkflowDeploymentEnvironment = { - id?: string - name?: string +export type UpdateServiceApiConversationVariableRequest = { + conversationId: string + variableId: string + user: string + value: GoogleProtobufValue +} + +export type WorkflowAsToolDependency = { + paths: Array +} + +export type WorkflowAsToolSource = { + workflow: WorkflowReference + paths: Array } export type WorkflowDeploymentInput = { - environment_variables?: Array + environment_variable_groups: Array credentials?: Array } +export type WorkflowEnvironmentVariableInputGroup = { + workflow_id: string + environment_variables: Array +} + +export type WorkflowPath = { + workflows: Array +} + +export type WorkflowReference = { + app_id: string + workflow_id: string + name: string + icon: string + icon_background: string + icon_type: string + icon_url?: string +} + export type WorkflowVersion = { version?: string marked_name?: string @@ -898,7 +981,6 @@ export type WorkflowVersion = { created_at?: number created_by?: SimpleAccount dsl_hash?: string - deleted?: boolean } export type Pagination = { @@ -920,6 +1002,10 @@ export type DeleteEnvironmentResponseWritable = { [key: string]: unknown } +export type DeleteServiceApiConversationResponseWritable = { + [key: string]: unknown +} + export type EnvironmentWritable = { id: string displayName: string @@ -930,8 +1016,7 @@ export type EnvironmentWritable = { statusMessage: string lastError?: Error namespace?: string - managedBy?: EnvironmentManagedBy - cpuPool: number + cpuPoolMillicores: number createdAt: string updatedAt: string memory?: RunnerMemoryWritable @@ -942,13 +1027,15 @@ export type EnvironmentDeployedAppWritable = { deploymentId: string workspace: NamedRef app: NamedRef - status: EnvironmentDeployedAppStatus + runtimeState: RuntimeState currentVersion?: WorkflowVersion deployedAt?: string deployedBy?: Operator latestAttempt?: EnvironmentDeployedAppAttempt sizing?: RunnerSizingWritable occupiesPool?: boolean + recentInvocationCount?: string + versionsBehind?: number } export type EnvironmentMcpServerWritable = { @@ -966,7 +1053,6 @@ export type GetEnvironmentResponseWritable = { export type ListEnvironmentDeployedAppsResponseWritable = { data: Array - summary: EnvironmentDeployedAppSummary pagination: Pagination } @@ -984,15 +1070,15 @@ export type RunnerMemoryWritable = { } export type RunnerSizingWritable = { - isolatedCpu: number + isolatedCpuMillicores: number memory: RunnerMemoryWritable } export type UpdateEnvironmentDeployedAppResourcesResponseWritable = { deploymentId: string - isolatedCpu: number - allocatedCpuCount: number - poolCpuCount: number + isolatedCpuMillicores: number + allocatedCpuMillicores: number + poolCpuMillicores: number memory: RunnerMemoryWritable } diff --git a/packages/contracts/generated/enterprise-app-deploy/zod.gen.ts b/packages/contracts/generated/enterprise-app-deploy/zod.gen.ts index c823bce2015..ae2591ffbf2 100644 --- a/packages/contracts/generated/enterprise-app-deploy/zod.gen.ts +++ b/packages/contracts/generated/enterprise-app-deploy/zod.gen.ts @@ -2,6 +2,11 @@ import * as z from 'zod' +/** + * Represents a dynamically typed value which can be either null, a number, a string, a boolean, a recursive struct value, or a list of values. + */ +export const zGoogleProtobufValue = z.unknown() + export const zEnvironmentStatus = z.enum([ 'ENVIRONMENT_STATUS_UNSPECIFIED', 'ENVIRONMENT_STATUS_PENDING', @@ -16,6 +21,19 @@ export const zApplicationInteractionStatus = z.enum([ 'APPLICATION_INTERACTION_STATUS_SUCCEEDED', 'APPLICATION_INTERACTION_STATUS_FAILED', 'APPLICATION_INTERACTION_STATUS_PARTIAL_SUCCEEDED', + 'APPLICATION_INTERACTION_STATUS_STOPPED', + 'APPLICATION_INTERACTION_STATUS_PAUSED', +]) + +export const zApplicationInteractionSource = z.enum([ + 'APPLICATION_INTERACTION_SOURCE_UNSPECIFIED', + 'APPLICATION_INTERACTION_SOURCE_WEB_APP', + 'APPLICATION_INTERACTION_SOURCE_SERVICE_API', + 'APPLICATION_INTERACTION_SOURCE_TRIGGER', + 'APPLICATION_INTERACTION_SOURCE_EXPLORE', + 'APPLICATION_INTERACTION_SOURCE_DEBUGGER', + 'APPLICATION_INTERACTION_SOURCE_VALIDATION', + 'APPLICATION_INTERACTION_SOURCE_OPENAPI', ]) export const zEnvironmentMode = z.enum([ @@ -79,28 +97,14 @@ export const zEnvironmentBackend = z.enum([ 'ENVIRONMENT_BACKEND_EXTERNAL', ]) -export const zEnvironmentManagedBy = z.enum([ - 'ENVIRONMENT_MANAGED_BY_UNSPECIFIED', - 'ENVIRONMENT_MANAGED_BY_SYSTEM', - 'ENVIRONMENT_MANAGED_BY_USER', -]) - -export const zEnvironmentDeployedAppStatus = z.enum([ - 'ENVIRONMENT_DEPLOYED_APP_STATUS_UNSPECIFIED', - 'ENVIRONMENT_DEPLOYED_APP_STATUS_DEPLOYED', - 'ENVIRONMENT_DEPLOYED_APP_STATUS_DEPLOYING', - 'ENVIRONMENT_DEPLOYED_APP_STATUS_FAILED', - 'ENVIRONMENT_DEPLOYED_APP_STATUS_UNDEPLOYED', -]) - -export const zDeploymentStatus = z.enum([ - 'DEPLOYMENT_STATUS_UNSPECIFIED', - 'DEPLOYMENT_STATUS_UNDEPLOYED', - 'DEPLOYMENT_STATUS_DEPLOYING', - 'DEPLOYMENT_STATUS_RUNNING', - 'DEPLOYMENT_STATUS_UNDEPLOYING', - 'DEPLOYMENT_STATUS_INVALID', - 'DEPLOYMENT_STATUS_FAILED', +export const zRuntimeState = z.enum([ + 'RUNTIME_STATE_UNSPECIFIED', + 'RUNTIME_STATE_UNDEPLOYED', + 'RUNTIME_STATE_RUNNING', + 'RUNTIME_STATE_STARTING', + 'RUNTIME_STATE_STOPPING', + 'RUNTIME_STATE_ERROR', + 'RUNTIME_STATE_UNKNOWN', ]) export const zEnvVarValueSource = z.enum([ @@ -115,6 +119,7 @@ export const zEnvVarValueType = z.enum([ 'ENV_VAR_VALUE_TYPE_STRING', 'ENV_VAR_VALUE_TYPE_NUMBER', 'ENV_VAR_VALUE_TYPE_SECRET', + 'ENV_VAR_VALUE_TYPE_LLM', ]) export const zOperatorType = z.enum([ @@ -173,7 +178,10 @@ export const zCreateEnvironmentRequest = z.object({ displayName: z.string(), description: z.string().optional(), mode: zEnvironmentMode, - cpuPool: z.number(), + cpuPoolMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), namespace: z.string().optional(), maxMemoryMib: z.string().optional(), }) @@ -192,25 +200,17 @@ export const zCredentialSelectionInput = z.object({ credential_id: z.string(), }) -export const zCredentialSlot = z.object({ - provider_id: z.string(), - category: zPluginCategory, - candidates: z.array(zCredentialCandidate), - last_deployed_credential_id: z.string().optional(), - icon: z.string().optional(), - icon_dark: z.string().optional(), -}) - -export const zDashboardApp = z.object({ - id: z.string(), - workspaceId: z.string(), - displayName: z.string(), -}) - export const zDeleteEnvironmentApiKeyResponse = z.record(z.string(), z.unknown()) export const zDeleteEnvironmentResponse = z.record(z.string(), z.unknown()) +export const zDeleteServiceApiConversationRequest = z.object({ + conversationId: z.string(), + user: z.string(), +}) + +export const zDeleteServiceApiConversationResponse = z.record(z.string(), z.unknown()) + export const zDeploymentEnvironment = z.object({ id: z.string(), display_name: z.string(), @@ -257,6 +257,18 @@ export const zEnvironmentAccess = z.object({ enable_api: z.boolean(), }) +/** + * EnvironmentActivity reports a numerator and a denominator rather than a rate, + * so a caller can tell a healthy environment from one nothing has called. + */ +export const zEnvironmentActivity = z.object({ + environmentId: z.string(), + invocationCount: z.string(), + failedInvocationCount: z.string(), + failedDeploymentCount: z.string(), + deployedAppCount: z.string(), +}) + export const zEnvironmentApiKey = z.object({ id: z.string(), type: z.string(), @@ -275,29 +287,13 @@ export const zEnvironmentDeployedAppAttempt = z.object({ finalizedAt: z.iso.datetime().optional(), }) -export const zEnvironmentDeployedAppSummary = z.object({ - total: z - .int() - .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) - .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), - deployed: z - .int() - .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) - .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), - deploying: z - .int() - .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) - .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), - failed: z - .int() - .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) - .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), -}) - export const zEnvironmentMcpServer = z.record(z.string(), z.unknown()) export const zEnvironmentPoolUsage = z.object({ - occupiedCpu: z.number(), + occupiedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), appCount: z .int() .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) @@ -320,7 +316,7 @@ export const zEnvironmentTrigger = z.record(z.string(), z.unknown()) export const zEnvironmentVariableInput = z.object({ key: z.string(), value_source: zEnvVarValueSource, - value: z.string().optional(), + value: zGoogleProtobufValue.optional(), }) export const zEnvironmentVariableSlot = z.object({ @@ -329,8 +325,8 @@ export const zEnvironmentVariableSlot = z.object({ description: z.string(), has_configured_value: z.boolean(), has_last_deployed_value: z.boolean(), - configured_value: z.string().optional(), - last_deployed_value: z.string().optional(), + configured_value: zGoogleProtobufValue.optional(), + last_deployed_value: zGoogleProtobufValue.optional(), }) export const zEnvironmentWebAppSubjectAccountData = z.object({ @@ -384,7 +380,6 @@ export const zError = z.object({ 'APPDEPLOY_APP_LOG_INVALID_TIME_RANGE', 'APPDEPLOY_APP_LOG_INVALID_CURSOR', 'APPDEPLOY_APP_LOG_CURSOR_FILTER_MISMATCH', - 'APPDEPLOY_APP_LOG_ID_INVALID', 'APPDEPLOY_UNSUPPORTED_NODE_TYPE', 'APPDEPLOY_UNSUPPORTED_TOOL_PROVIDER_TYPE', 'APPDEPLOY_TOOL_PROVIDER_TYPE_INVALID', @@ -396,15 +391,12 @@ export const zError = z.object({ 'APPDEPLOY_INVALID_WORKFLOW_ID', 'APPDEPLOY_INVALID_DEPLOYMENT_VERSION_ID', 'APPDEPLOY_DEVELOPER_API_URL_NOT_CONFIGURED', - 'APPDEPLOY_INVALID_DEPLOYMENT_OPERATION_ID', - 'APPDEPLOY_APP_LOG_EXPORT_RANGE_TOO_WIDE', - 'APPDEPLOY_APP_LOG_EXPORT_TOO_MANY_ROWS', - 'APPDEPLOY_APP_LOG_EXPORT_TOO_LARGE', 'APPDEPLOY_UNAUTHORIZED', 'APPDEPLOY_FORBIDDEN', 'APPDEPLOY_APP_RUNNER_AUTH_REQUIRED', 'APPDEPLOY_APP_RUNNER_INVALID_JOIN_TOKEN', 'APPDEPLOY_APP_RUNNER_INVALID_CONTROL_TOKEN', + 'APPDEPLOY_WEB_APP_ACCESS_DENIED', 'APPDEPLOY_ENVIRONMENT_NOT_FOUND', 'APPDEPLOY_DEPLOYMENT_NOT_FOUND', 'APPDEPLOY_REVISION_NOT_FOUND', @@ -415,15 +407,15 @@ export const zError = z.object({ 'APPDEPLOY_ACCESS_SUBJECT_NOT_FOUND', 'APPDEPLOY_API_KEY_NOT_FOUND', 'APPDEPLOY_SOURCE_VERSION_NOT_FOUND', - 'APPDEPLOY_APP_LOG_NOT_FOUND', 'APPDEPLOY_WORKSPACE_NOT_FOUND', 'APPDEPLOY_APP_RUNNER_NOT_FOUND', 'APPDEPLOY_WORKFLOW_NOT_FOUND', - 'APPDEPLOY_DEPLOYMENT_OPERATION_NOT_FOUND', 'APPDEPLOY_RUN_FILE_NOT_FOUND', 'APPDEPLOY_APPLICATION_UNAVAILABLE', 'APPDEPLOY_TARGET_ENVIRONMENT_REMOVED', 'APPDEPLOY_VERSION_UNAVAILABLE', + 'APPDEPLOY_CONVERSATION_NOT_FOUND', + 'APPDEPLOY_CHAT_MESSAGE_NOT_FOUND', 'APPDEPLOY_CONFLICT', 'APPDEPLOY_DEPLOYMENT_IN_PROGRESS', 'APPDEPLOY_ALREADY_UNDEPLOYED', @@ -462,10 +454,13 @@ export const zError = z.object({ 'APPDEPLOY_ENVIRONMENT_CPU_POOL_EXHAUSTED', 'APPDEPLOY_RESOURCE_NOT_APPLICABLE_FOR_MODE', 'APPDEPLOY_ENVIRONMENT_CPU_POOL_BELOW_ALLOCATED', + 'APPDEPLOY_CHAT_CONTEXT_TOO_LARGE', + 'APPDEPLOY_FILE_GRANT_UNAVAILABLE', 'APPDEPLOY_APP_RUNNER_CONTROL_NOT_CONFIGURED', 'APPDEPLOY_RUNTIME_ASSIGNMENT_FAILED', 'APPDEPLOY_REVISION_TIMEOUT', 'APPDEPLOY_INTERNAL_ERROR', + 'APPDEPLOY_RECEIPT_RETRY', 'APPDEPLOY_ENVIRONMENT_BOOTSTRAP_AUTH_REJECTED', 'APPDEPLOY_ENVIRONMENT_BOOTSTRAP_NAMESPACE_MISSING', 'APPDEPLOY_ENVIRONMENT_BOOTSTRAP_INSUFFICIENT_RBAC', @@ -486,6 +481,16 @@ export const zError = z.object({ detailCode: z.string().optional(), }) +export const zGetApplicationInteractionSummaryResponse = z.object({ + totalCount: z.string(), + failedCount: z.string(), + lookbackStart: z.iso.datetime(), +}) + +export const zGetEnvironmentActivityResponse = z.object({ + data: z.array(zEnvironmentActivity), +}) + export const zGetEnvironmentCapabilitiesResponse = z.object({ backend: zEnvironmentBackend, supportedModes: z.array( @@ -506,13 +511,13 @@ export const zGetWebAppAccessModeResponse = z.object({ accessMode: z.string().optional(), }) -export const zGetWebAppPermissionResponse = z.object({ - result: z.boolean().optional(), +export const zGetWebAppLoginStatusResponse = z.object({ + logged_in: z.boolean().optional(), + app_logged_in: z.boolean().optional(), }) -export const zGetWorkflowDeploymentOptionsResponse = z.object({ - environment_variable_slots: z.array(zEnvironmentVariableSlot), - credential_slots: z.array(zCredentialSlot), +export const zGetWebAppPermissionResponse = z.object({ + result: z.boolean().optional(), }) export const zListAppEnvironmentsResponse = z.object({ @@ -527,6 +532,18 @@ export const zListEnvironmentTriggersResponse = z.object({ data: z.array(zEnvironmentTrigger), }) +export const zMintServiceApiFileGrantRequest = z.object({ + tenantId: z.string().optional(), + appId: z.string().optional(), + environmentId: z.string().optional(), + user: z.string().optional(), +}) + +export const zMintServiceApiFileGrantResponse = z.object({ + grant: z.string().optional(), + expiresAt: z.string().optional(), +}) + export const zNamedRef = z.object({ id: z.string(), displayName: z.string(), @@ -534,7 +551,10 @@ export const zNamedRef = z.object({ export const zEnvironmentPoolShare = z.object({ app: zNamedRef, - isolatedCpu: z.number(), + isolatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), }) /** @@ -544,7 +564,11 @@ export const zEnvironmentPoolShare = z.object({ */ export const zEnvironmentPoolComposition = z.object({ topApps: z.array(zEnvironmentPoolShare).optional(), - otherCpu: z.number().optional(), + otherCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }) + .optional(), otherAppCount: z .int() .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) @@ -552,42 +576,37 @@ export const zEnvironmentPoolComposition = z.object({ .optional(), }) +export const zOperationApp = z.object({ + id: z.string().optional(), + workspaceId: z.string().optional(), + displayName: z.string().optional(), +}) + export const zOperator = z.object({ type: zOperatorType, id: z.string(), display_name: z.string(), }) -export const zApplicationInteraction = z.object({ - id: z.string(), - timestamp: z.iso.datetime(), - workflowRunId: z.string(), - status: zApplicationInteractionStatus, - durationSeconds: z.number(), - totalTokens: z.string(), - workspace: zNamedRef, - environment: zNamedRef, - app: zNamedRef, - operator: zOperator.optional(), - invokeFrom: z.string(), - traceId: z.string(), - difyTraceId: z.string(), - deploymentVersionId: z.string(), - error: z.string().optional(), - body: z.string().optional(), - attributesJson: z.string().optional(), - resourceAttributesJson: z.string().optional(), -}) - -export const zGetApplicationInteractionResponse = z.object({ - interaction: zApplicationInteraction, -}) - export const zPrepareAppDeletionRequest = z.object({ tenantId: z.string().optional(), appId: z.string().optional(), }) +export const zRenameServiceApiConversationRequest = z.object({ + conversationId: z.string(), + user: z.string(), + name: z.string().optional(), + autoGenerate: z.boolean().optional(), +}) + +export const zRenameWebAppConversationRequest = z.object({ + appCode: z.string(), + conversationId: z.string(), + name: z.string().optional(), + autoGenerate: z.boolean().optional(), +}) + export const zResolveApiTokenRouteRequest = z.object({ token: z.string().optional(), }) @@ -601,17 +620,14 @@ export const zResolveApiTokenRouteResponse = z.object({ .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }) .optional(), - environmentStatus: zEnvironmentStatus.optional(), appId: z.string().optional(), tenantId: z.string().optional(), deploymentId: z.string().optional(), servingRevisionId: z.string().optional(), - deploymentStatus: zDeploymentStatus.optional(), - revoked: z.boolean().optional(), - unavailableReason: z.string().optional(), targetKind: zRouteTargetKind.optional(), directUpstream: z.string().optional(), - deploymentGeneration: z.string().optional(), + assignmentGeneration: z.string().optional(), + decision: z.string().optional(), }) export const zResolveWebAppRouteRequest = z.object({ @@ -629,18 +645,18 @@ export const zResolveWebAppRouteResponse = z.object({ .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }) .optional(), - environmentStatus: zEnvironmentStatus.optional(), appId: z.string().optional(), tenantId: z.string().optional(), deploymentId: z.string().optional(), servingRevisionId: z.string().optional(), - deploymentStatus: zDeploymentStatus.optional(), - unavailableReason: z.string().optional(), targetKind: zRouteTargetKind.optional(), directUpstream: z.string().optional(), - deploymentGeneration: z.string().optional(), - endUserId: z.string().optional(), - authType: z.string().optional(), + assignmentGeneration: z.string().optional(), + userId: z.string().optional(), + userFrom: z.string().optional(), + userAuthType: z.string().optional(), + fileGrant: z.string().optional(), + fileGrantExpiresAt: z.string().optional(), }) export const zRetryEnvironmentBootstrapRequest = z.object({ @@ -667,8 +683,10 @@ export const zEnvironment = z.object({ statusMessage: z.string(), lastError: zError.optional(), namespace: z.string().optional(), - managedBy: zEnvironmentManagedBy.optional(), - cpuPool: z.number(), + cpuPoolMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), createdAt: z.iso.datetime(), updatedAt: z.iso.datetime(), memory: zRunnerMemory.optional(), @@ -693,7 +711,10 @@ export const zRetryEnvironmentBootstrapResponse = z.object({ * in an isolated environment; elsewhere the runner belongs to the environment. */ export const zRunnerSizing = z.object({ - isolatedCpu: z.number(), + isolatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), memory: zRunnerMemory, }) @@ -707,6 +728,20 @@ export const zSimpleAccount = z.object({ email: z.string().optional(), }) +export const zSourceVersionDeploymentEnvironment = z.object({ + id: z.string().optional(), + name: z.string().optional(), +}) + +export const zSourceVersionDeployment = z.object({ + sourceVersionId: z.string().optional(), + environments: z.array(zSourceVersionDeploymentEnvironment).optional(), +}) + +export const zBatchGetSourceVersionDeploymentsResponse = z.object({ + items: z.array(zSourceVersionDeployment).optional(), +}) + export const zTestConnectionRequest = z.object({ environmentId: z.string().optional(), }) @@ -731,29 +766,30 @@ export const zUnsupportedNodeProvider = z.object({ provider_name: z.string(), }) -export const zUnsupportedNode = z.object({ - id: z.string(), - type: z.string(), - title: z.string(), - provider: zUnsupportedNodeProvider.optional(), -}) - -export const zPrecheckWorkflowDeploymentResponse = z.object({ - unsupported_nodes: z.array(zUnsupportedNode), -}) - export const zUpdateEnvironmentDeployedAppResourcesRequest = z.object({ environmentId: z.string(), deploymentId: z.string(), - isolatedCpu: z.number(), + isolatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), maxMemoryMib: z.string().optional(), }) export const zUpdateEnvironmentDeployedAppResourcesResponse = z.object({ deploymentId: z.string(), - isolatedCpu: z.number(), - allocatedCpuCount: z.number(), - poolCpuCount: z.number(), + isolatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), + allocatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), + poolCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), memory: zRunnerMemory, }) @@ -761,7 +797,11 @@ export const zUpdateEnvironmentRequest = z.object({ environmentId: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), - cpuPool: z.number().optional(), + cpuPoolMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }) + .optional(), maxMemoryMib: z.string().optional(), }) @@ -769,25 +809,79 @@ export const zUpdateEnvironmentResponse = z.object({ environment: zEnvironment, }) -export const zWorkflowDeploymentEnvironment = z.object({ - id: z.string().optional(), - name: z.string().optional(), +export const zUpdateServiceApiConversationVariableRequest = z.object({ + conversationId: z.string(), + variableId: z.string(), + user: z.string(), + value: zGoogleProtobufValue, }) -export const zSourceVersionDeployment = z.object({ - sourceVersionId: z.string().optional(), - environments: z.array(zWorkflowDeploymentEnvironment).optional(), -}) - -export const zBatchGetSourceVersionDeploymentsResponse = z.object({ - items: z.array(zSourceVersionDeployment).optional(), +export const zWorkflowEnvironmentVariableInputGroup = z.object({ + workflow_id: z.string(), + environment_variables: z.array(zEnvironmentVariableInput), }) export const zWorkflowDeploymentInput = z.object({ - environment_variables: z.array(zEnvironmentVariableInput).optional(), + environment_variable_groups: z.array(zWorkflowEnvironmentVariableInputGroup), credentials: z.array(zCredentialSelectionInput).optional(), }) +export const zWorkflowReference = z.object({ + app_id: z.string(), + workflow_id: z.string(), + name: z.string(), + icon: z.string(), + icon_background: z.string(), + icon_type: z.string(), + icon_url: z.string().optional(), +}) + +export const zWorkflowPath = z.object({ + workflows: z.array(zWorkflowReference), +}) + +export const zWorkflowAsToolDependency = z.object({ + paths: z.array(zWorkflowPath), +}) + +export const zCredentialSlot = z.object({ + provider_id: z.string(), + category: zPluginCategory, + candidates: z.array(zCredentialCandidate), + last_deployed_credential_id: z.string().optional(), + icon: z.string().optional(), + icon_dark: z.string().optional(), + workflow_as_tool_dependency: zWorkflowAsToolDependency.optional(), +}) + +export const zUnsupportedNode = z.object({ + id: z.string(), + type: z.string(), + title: z.string(), + provider: zUnsupportedNodeProvider.optional(), + workflow_as_tool_dependency: zWorkflowAsToolDependency.optional(), +}) + +export const zPrecheckWorkflowDeploymentResponse = z.object({ + unsupported_nodes: z.array(zUnsupportedNode), +}) + +export const zWorkflowAsToolSource = z.object({ + workflow: zWorkflowReference, + paths: z.array(zWorkflowPath), +}) + +export const zEnvironmentVariableGroup = z.object({ + from_app: zWorkflowReference.optional(), + from_workflow_as_tool: zWorkflowAsToolSource.optional(), + environment_variable_slots: z.array(zEnvironmentVariableSlot), +}) + +export const zGetWorkflowDeploymentOptionsResponse = z.object({ + environment_variable_groups: z.array(zEnvironmentVariableGroup), + credential_slots: z.array(zCredentialSlot), +}) + export const zWorkflowVersion = z.object({ version: z.string().optional(), marked_name: z.string().optional(), @@ -801,7 +895,22 @@ export const zWorkflowVersion = z.object({ created_at: z.number().optional(), created_by: zSimpleAccount.optional(), dsl_hash: z.string().optional(), - deleted: z.boolean().optional(), +}) + +export const zApplicationInteraction = z.object({ + id: z.string(), + timestamp: z.iso.datetime(), + status: zApplicationInteractionStatus, + durationSeconds: z.number(), + totalTokens: z.string(), + workspace: zNamedRef, + environment: zNamedRef, + app: zNamedRef, + operator: zNamedRef.optional(), + source: zApplicationInteractionSource, + version: zWorkflowVersion.optional(), + traceId: z.string().optional(), + error: z.string().optional(), }) export const zDeploymentOperation = z.object({ @@ -824,13 +933,19 @@ export const zEnvironmentDeployedApp = z.object({ deploymentId: z.string(), workspace: zNamedRef, app: zNamedRef, - status: zEnvironmentDeployedAppStatus, + runtimeState: zRuntimeState, currentVersion: zWorkflowVersion.optional(), deployedAt: z.iso.datetime().optional(), deployedBy: zOperator.optional(), latestAttempt: zEnvironmentDeployedAppAttempt.optional(), sizing: zRunnerSizing.optional(), occupiesPool: z.boolean().optional(), + recentInvocationCount: z.string().optional(), + versionsBehind: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }) + .optional(), }) export const zEnvironmentDeploymentOperation = z.object({ @@ -843,7 +958,7 @@ export const zEnvironmentDeploymentOperation = z.object({ }) export const zEnvironmentDeploymentState = z.object({ - status: zDeploymentStatus, + runtimeState: zRuntimeState, current_version: zWorkflowVersion.optional(), versions_behind: z .int() @@ -861,14 +976,16 @@ export const zEnvironmentDeployment = z.object({ access: zEnvironmentAccess, }) -export const zGetDeploymentOperationResponse = z.object({ - operation: zDeploymentOperation, -}) - export const zGetEnvironmentDeploymentResponse = z.object({ environment_deployment: zEnvironmentDeployment, }) +export const zListApplicationInteractionsResponse = z.object({ + data: z.array(zApplicationInteraction), + nextPageToken: z.string().optional(), + previousPageToken: z.string().optional(), +}) + export const zListEnvironmentDeploymentsResponse = z.object({ environment_deployments: z.array(zEnvironmentDeployment), }) @@ -899,13 +1016,8 @@ export const zPagination = z.object({ .optional(), }) -export const zListApplicationInteractionsResponse = z.object({ - data: z.array(zApplicationInteraction), - pagination: zPagination, -}) - -export const zListAppsResponse = z.object({ - data: z.array(zDashboardApp), +export const zListApplicationInteractionAppsResponse = z.object({ + data: z.array(zNamedRef), pagination: zPagination, }) @@ -916,7 +1028,6 @@ export const zListDeploymentOperationsResponse = z.object({ export const zListEnvironmentDeployedAppsResponse = z.object({ data: z.array(zEnvironmentDeployedApp), - summary: zEnvironmentDeployedAppSummary, pagination: zPagination, }) @@ -925,10 +1036,17 @@ export const zListEnvironmentsResponse = z.object({ pagination: zPagination, }) +export const zListOperationAppsResponse = z.object({ + data: z.array(zOperationApp), + pagination: zPagination, +}) + export const zDeleteEnvironmentApiKeyResponseWritable = z.record(z.string(), z.unknown()) export const zDeleteEnvironmentResponseWritable = z.record(z.string(), z.unknown()) +export const zDeleteServiceApiConversationResponseWritable = z.record(z.string(), z.unknown()) + export const zEnvironmentMcpServerWritable = z.record(z.string(), z.unknown()) export const zEnvironmentTriggerWritable = z.record(z.string(), z.unknown()) @@ -951,8 +1069,10 @@ export const zEnvironmentWritable = z.object({ statusMessage: z.string(), lastError: zError.optional(), namespace: z.string().optional(), - managedBy: zEnvironmentManagedBy.optional(), - cpuPool: z.number(), + cpuPoolMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), createdAt: z.iso.datetime(), updatedAt: z.iso.datetime(), memory: zRunnerMemoryWritable.optional(), @@ -982,7 +1102,10 @@ export const zRetryEnvironmentBootstrapResponseWritable = z.object({ * in an isolated environment; elsewhere the runner belongs to the environment. */ export const zRunnerSizingWritable = z.object({ - isolatedCpu: z.number(), + isolatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), memory: zRunnerMemoryWritable, }) @@ -990,26 +1113,40 @@ export const zEnvironmentDeployedAppWritable = z.object({ deploymentId: z.string(), workspace: zNamedRef, app: zNamedRef, - status: zEnvironmentDeployedAppStatus, + runtimeState: zRuntimeState, currentVersion: zWorkflowVersion.optional(), deployedAt: z.iso.datetime().optional(), deployedBy: zOperator.optional(), latestAttempt: zEnvironmentDeployedAppAttempt.optional(), sizing: zRunnerSizingWritable.optional(), occupiesPool: z.boolean().optional(), + recentInvocationCount: z.string().optional(), + versionsBehind: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }) + .optional(), }) export const zListEnvironmentDeployedAppsResponseWritable = z.object({ data: z.array(zEnvironmentDeployedAppWritable), - summary: zEnvironmentDeployedAppSummary, pagination: zPagination, }) export const zUpdateEnvironmentDeployedAppResourcesResponseWritable = z.object({ deploymentId: z.string(), - isolatedCpu: z.number(), - allocatedCpuCount: z.number(), - poolCpuCount: z.number(), + isolatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), + allocatedCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), + poolCpuMillicores: z + .int() + .min(-2147483648, { error: 'Invalid value: Expected int32 to be >= -2147483648' }) + .max(2147483647, { error: 'Invalid value: Expected int32 to be <= 2147483647' }), memory: zRunnerMemoryWritable, }) diff --git a/packages/contracts/marketplace.ts b/packages/contracts/marketplace.ts index 459f3d778e6..06353129a8f 100644 --- a/packages/contracts/marketplace.ts +++ b/packages/contracts/marketplace.ts @@ -22,6 +22,10 @@ export type MarketplaceCollection = { search_params?: SearchParamsFromCollection } +export type MarketplaceTimestamp = string | number +export type MarketplaceCreatorStatus = 'pending' | 'active' | 'inactive' | 'deleted' +export type MarketplaceOrganizationStatus = 'active' | 'inactive' | 'deleted' + export type PluginsSearchParams = { query: string page?: number @@ -44,6 +48,7 @@ export type CollectionsAndPluginsSearchParams = { condition?: string exclude?: string[] type?: 'plugin' | 'bundle' + limit?: number } export type MarketplaceTemplate = { @@ -53,9 +58,65 @@ export type MarketplaceTemplate = { icon: string icon_background: string icon_file_key: string - publisher_unique_handle: string + publisher_unique_handle?: string + publisher_handle?: string + publisher_type?: string + creator_email?: string usage_count: number categories: string[] + deps_plugins?: string[] + preferred_languages?: string[] + badges?: string[] + created_at?: MarketplaceTimestamp + updated_at?: MarketplaceTimestamp +} + +export type MarketplaceCreator = { + id?: string + email?: string + name?: string + display_name?: string + unique_handle: string + display_email?: string + description?: string + avatar?: string + background_image?: string + social_links?: string[] + badges?: string[] + verified?: boolean + status?: MarketplaceCreatorStatus + public?: boolean + plugin_count?: number + template_count?: number + created_at?: string + updated_at?: string +} + +export type MarketplaceOrganization = { + id?: string + email?: string + name?: string + display_name?: string + unique_handle?: string + display_email?: string + description?: string + avatar?: string + background_image?: string + social_links?: string[] + badges?: string[] + verified?: boolean + status?: MarketplaceOrganizationStatus + created_at?: string + updated_at?: string +} + +export type MarketplaceTemplateCollection = { + name: string + description: Record + label: Record + searchable?: boolean + search_params?: SearchParamsFromCollection + priority: number } export type MarketplacePluginCategory = @@ -109,6 +170,9 @@ export type MarketplacePlugin = { authorized_category: 'langgenius' | 'partner' | 'community' } from: MarketplacePluginDependencySource + created_at?: MarketplaceTimestamp + updated_at?: MarketplaceTimestamp + version_updated_at?: MarketplaceTimestamp | null } export type PluginInfoFromMarketPlace = { @@ -154,8 +218,151 @@ export type TemplateDetailResponse = { data: MarketplaceTemplate } +export type TemplateCollectionsResponse = { + data?: { + collections?: MarketplaceTemplateCollection[] + total?: number + } +} + +export type TemplateCollectionTemplatesResponse = { + data?: { + templates?: MarketplaceTemplate[] + total?: number + } +} + +export type TemplateSearchResponse = { + data?: { + templates?: MarketplaceTemplate[] + total?: number + } +} + export type DownloadPluginResponse = Blob +export type CreatorDetailResponse = { + code?: number + data?: { + creator?: MarketplaceCreator + } + msg?: string +} + +export type OrganizationDetailResponse = { + code?: number + data?: { + organization?: MarketplaceOrganization + } + msg?: string +} + +export type PublisherPluginsResponse = { + code?: number + data?: { + plugins?: MarketplacePlugin[] + total?: number + } + msg?: string +} + +export type PublisherTemplatesResponse = { + code?: number + data?: { + templates?: MarketplaceTemplate[] + total?: number + } + msg?: string +} + +// Banner payload shapes shared by the standalone marketplace and the embedded +// console. The banners endpoint output stays `unknown` in the contract because +// the delivery format is normalized and runtime-validated in +// `web/app/components/plugins/marketplace/home/banners.ts`. +export type BannerBase = { + id: string + title: string + sort: number + language: string +} + +export type BannerRecommendCard = { + item_type: 'plugin' | 'template' + item_id: string + display_name: string + icon_url?: string + icon?: string + icon_background?: string + creator?: string + badges?: Array<'partner' | 'verified'> + link: string + card_position: number + auto_batch_id?: string | null +} + +export type BannerRecommend = BannerBase & { + style_type: 'recommend' + content: { + theme_type: 'newest' | 'hottest' | 'partner' + heading?: string + subheadings?: string[] + description?: string + cards: BannerRecommendCard[] + } +} + +export type BannerBlog = BannerBase & { + style_type: 'blog' + content: { + blog_title: string + subtitle?: string + description?: string + link: string + link_target_type: 'blog' | 'github' + } +} + +export type BannerImageContent = { + images: { + desktop: string + tablet?: string + mobile?: string + } + link: string + alt_text?: string + activity_id?: string +} + +export type BannerEvent = BannerBase & { + style_type: 'event' + content: BannerImageContent +} + +export type BannerAd = BannerBase & { + style_type: 'ad' + content: BannerImageContent & { + partner_id?: string + campaign_id?: string + } +} + +export type PluginBanner = BannerRecommend | BannerBlog | BannerEvent | BannerAd + +const bannerListContract = base + .route({ + path: '/banners', + method: 'GET', + }) + .input( + type<{ + query: { + page: 'plugins' | 'templates' + language: string + } + }>(), + ) + .output(type()) + const collectionsContract = base .route({ path: '/collections', @@ -212,6 +419,58 @@ const templateDetailContract = base ) .output(type()) +const templateCollectionsContract = base + .route({ + path: '/template-collections', + method: 'GET', + }) + .input( + type<{ + query?: { + page?: number + page_size?: number + } + }>(), + ) + .output(type()) + +const templateCollectionTemplatesContract = base + .route({ + path: '/template-collections/{collectionName}/templates', + method: 'POST', + }) + .input( + type<{ + params: { + collectionName: string + } + body?: { + limit?: number + } + }>(), + ) + .output(type()) + +const templateSearchContract = base + .route({ + path: '/templates/search/advanced', + method: 'POST', + }) + .input( + type<{ + body: { + page: number + page_size: number + query: string + sort_by: string + sort_order: string + categories?: string[] + languages?: string[] + } + }>(), + ) + .output(type()) + const downloadPluginContract = base .route({ path: '/plugins/{organization}/{pluginName}/{version}/download', @@ -228,12 +487,90 @@ const downloadPluginContract = base ) .output(type()) +const creatorDetailContract = base + .route({ + path: '/creators/{uniqueHandle}', + method: 'GET', + }) + .input( + type<{ + params: { + uniqueHandle: string + } + }>(), + ) + .output(type()) + +const organizationDetailContract = base + .route({ + path: '/organizations/{id}', + method: 'GET', + }) + .input( + type<{ + params: { + id: string + } + }>(), + ) + .output(type()) + +const publisherPluginsContract = base + .route({ + path: '/plugins/publisher/{uniqueHandle}', + method: 'GET', + }) + .input( + type<{ + params: { + uniqueHandle: string + } + query: { + page: number + page_size: number + sort_by?: string + sort_order?: string + } + }>(), + ) + .output(type()) + +const publisherTemplatesContract = base + .route({ + path: '/templates/publisher/{uniqueHandle}', + method: 'GET', + }) + .input( + type<{ + params: { + uniqueHandle: string + } + query: { + page: number + page_size: number + sort_by?: string + sort_order?: string + } + }>(), + ) + .output(type()) + export const marketplaceRouterContract = { + banners: { + list: bannerListContract, + }, collections: collectionsContract, collectionPlugins: collectionPluginsContract, searchAdvanced: searchAdvancedContract, + templateCollections: templateCollectionsContract, + templateCollectionTemplates: templateCollectionTemplatesContract, templateDetail: templateDetailContract, + templateSearch: templateSearchContract, downloadPlugin: downloadPluginContract, + creatorDetail: creatorDetailContract, + organizationDetail: organizationDetailContract, + publisherPlugins: publisherPluginsContract, + publisherTemplates: publisherTemplatesContract, } export type MarketPlaceInputs = InferContractRouterInputs diff --git a/packages/contracts/openapi-ts.api.config.ts b/packages/contracts/openapi-ts.api.config.ts index 1feab712fcb..a2b272336cb 100644 --- a/packages/contracts/openapi-ts.api.config.ts +++ b/packages/contracts/openapi-ts.api.config.ts @@ -40,7 +40,6 @@ type ApiSpec = { } type ApiJob = { - clean?: boolean document: SwaggerDocument outputPath: string plugins?: UserConfig['plugins'] @@ -527,7 +526,6 @@ const writeConsoleRouterContract = (segments: string[]) => { const createConsoleContractEntryJob = (document: SwaggerDocument, segments: string[]): ApiJob => { return { - clean: false, document, outputPath: 'generated/api/console', plugins: [], @@ -587,7 +585,7 @@ const createApiConfig = (job: ApiJob): UserConfig => ({ file: false, }, output: { - ...(job.clean === undefined ? {} : { clean: job.clean }), + clean: false, entryFile: false, fileName: { suffix: '.gen', diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 9dadfe9a4de..dd88b0ec552 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -30,7 +30,7 @@ } }, "scripts": { - "gen-api-contract": "uv run --project ../../api ../../api/dev/generate_swagger_specs.py --output-dir openapi && uv run --project ../../api ../../api/dev/generate_fastopenapi_specs.py --output-dir openapi && node -e \"fs.rmSync('generated/api', { recursive: true, force: true })\" && openapi-ts -f openapi-ts.api.config.ts && node scripts/patch-api-streaming-contracts.mjs && vp fmt generated/api", + "gen-api-contract": "uv run --project ../../api ../../api/dev/generate_swagger_specs.py --output-dir openapi && uv run --project ../../api ../../api/dev/generate_fastopenapi_specs.py --output-dir openapi && node -e \"if (process.env.CI) fs.rmSync('generated/api', { recursive: true, force: true })\" && openapi-ts -f openapi-ts.api.config.ts && node scripts/patch-api-streaming-contracts.mjs && vp fmt generated/api", "gen-enterprise-contract": "openapi-ts -f openapi-ts.enterprise.config.ts", "gen-enterprise-app-deploy-contract": "openapi-ts -f openapi-ts.enterprise-app-deploy.config.ts", "gen-knowledge-fs-contract": "node scripts/generate-knowledge-fs-contract.mjs", diff --git a/packages/dify-ui/AGENTS.md b/packages/dify-ui/AGENTS.md index 5e17256c562..6c45faca8af 100644 --- a/packages/dify-ui/AGENTS.md +++ b/packages/dify-ui/AGENTS.md @@ -1,12 +1,14 @@ # @langgenius/dify-ui -This file owns the package boundary and routes detailed contracts. Start from the [package index], -then read only the guide for the contract being changed. +This file owns the package boundary. Read the matching contract guide below directly; use the +[package index] when discovering available primitives or usage examples. ## Package boundary - Keep this an independent primitive package. Do not import from application packages or depend on routing, i18n, application state, schemas, data fetching, or business APIs. +- Keep package contracts and contributor guidance self-contained here. Application docs and agent + skills may reference these guides; these guides must not require Web docs or skills to define package behavior. - Prefer `@base-ui/react` when it owns the required headless behavior. Style primitives with `cva`, `cn`, and Dify design tokens. Keep one primitive per `src//` folder with optional colocated stories and tests. diff --git a/packages/dify-ui/README.md b/packages/dify-ui/README.md index 645665fb89e..db32e4e26dd 100644 --- a/packages/dify-ui/README.md +++ b/packages/dify-ui/README.md @@ -49,13 +49,13 @@ Import `styles.css` once from the consumer's root stylesheet or entrypoint. Utilities: -- `./cn` composes conditional classes with `clsx` and `tailwind-merge`. +- `./cn` re-exports `cn` from the `cn` package through Dify UI's public subpath. - `./styles.css` provides design tokens, theme variables, and shared utilities. ## Guides -Start here, then open only the guide for the contract being changed. Component-specific Dify -behavior lives beside the component. Contracts shared by several primitives live in `docs/`. +Open only the guide for the contract being changed. Component-specific Dify behavior lives beside +the component. Contracts shared by several primitives live in `docs/`. Upstream behavior remains owned by the [Base UI documentation]. ### Component guides @@ -80,8 +80,8 @@ Upstream behavior remains owned by the [Base UI documentation]. ## Contributing -Read [component authoring rules] before modifying the package, then open only the matching owner -guide. This index intentionally does not duplicate those contracts. +[Package rules][component authoring rules] own the package boundary and contributor guidance. +For a known contract, go directly to its guide above. [Accessible names and descriptions]: ./docs/accessible-names-and-descriptions.md [Base UI documentation]: https://base-ui.com/llms.txt diff --git a/packages/dify-ui/docs/accessible-names-and-descriptions.md b/packages/dify-ui/docs/accessible-names-and-descriptions.md index d948c3290ff..71c3cbaafa6 100644 --- a/packages/dify-ui/docs/accessible-names-and-descriptions.md +++ b/packages/dify-ui/docs/accessible-names-and-descriptions.md @@ -38,7 +38,7 @@ text is not a label relationship by proximity alone. | Surface | Contract | | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | -| Text button or link | Follow [Button]. Let meaningful child text name the action; do not repeat it in `aria-label`. | +| Text button or link | Let meaningful child text name the action; do not repeat it in `aria-label`. Follow [Button] when a focused loading button changes its label. | | Form control | Follow [Forms]. Use its label primitive or an associated native `label`, preserving label activation. | | Icon-only command | Follow [IconButton]. Its component-specific contract requires one accessible-name source and a decorative glyph. | | Dialog or named region | Reuse the visible title through the primitive title API or `aria-labelledby`; use `aria-label` only when no suitable visible title exists. | @@ -58,9 +58,9 @@ A description is optional when the name is sufficient. For a file action, the na the operation and file, while the description explains retention or recovery. Avoid repeating the same sentence in both. See the [name and description computation specification][accname]. -A name or description attribute is not an announcement mechanism. Keep progress and asynchronous -updates with their existing feature owner. Follow [Button] for loading behavior and [Forms] for -field error relationships. +A name or description attribute is not a general announcement mechanism. Keep progress and +asynchronous updates with their existing feature owner. Follow [Button] for the narrower case of a +focused loading button whose visible label changes, and [Forms] for field error relationships. ## Overrides and References diff --git a/packages/dify-ui/docs/selection.md b/packages/dify-ui/docs/selection.md index 16a2a397a88..2edcabc3ca1 100644 --- a/packages/dify-ui/docs/selection.md +++ b/packages/dify-ui/docs/selection.md @@ -17,6 +17,12 @@ domain value type through its public API. Multiple-selection comboboxes follow the Base UI chips composition: chips and the input share the input group, chips wrap, and the group grows vertically. +For chip-based multiple comboboxes, render `ComboboxValue` around `ComboboxChips` and label the +chips container only while it has the conditional `toolbar` role. Consumers must localize the +`ComboboxChip` Backspace/Delete description, the item-specific `ComboboxChipRemove` name, and the +input's selection count and Left Arrow hint. If `FieldDescription` already sets `aria-describedby`, +put the input hint there because it takes precedence over `aria-description`. + Autocomplete, Combobox, and Select popups use Base UI's `--anchor-width` and `--available-width` variables to follow their trigger while clamping to the viewport. Do not replace that sizing with a fixed width or an unclamped minimum width. @@ -74,6 +80,72 @@ single-or-multiple union. Prefer the Base UI `items` collection pattern so the root, value display, and item list share one runtime source of truth. Convert values to strings only at real serialization boundaries. +Treat collection values exposed for rendering as read-only views. In multiple mode, `SelectValue` +may receive a shared frozen empty array, and `useAutocompleteFilteredItems` exposes internal +filtered state. Use non-mutating transforms such as `map`, `filter`, or `toSorted`; create an +explicit copy only when a mutable array is required. + +### Combobox source items and selected values + +Combobox has separate types for the selected business value and the source record rendered by the +list: + +```tsx +import { + Combobox, + ComboboxItem, + ComboboxList, + createComboboxItems, +} from '@langgenius/dify-ui/combobox' +import { useMemo } from 'react' + +const userItems = useMemo( + () => + createComboboxItems(users, { + getValue: user => user.id, + getLabel: user => user.name, + }), + [users], +) + + + multiple + items={userItems} + value={selectedUserIds} + onValueChange={setSelectedUserIds} +> + > + {user => value={user.id}>{user.name}} + + +``` + +The first generic is `Value`, the second is the literal multiple-selection mode, and the third is +the source `Item`. `ComboboxValue` and `ComboboxItem` use `Value`; `filter`, `ComboboxList`, +`ComboboxGroup`, `ComboboxCollection`, and `useComboboxFilteredItems` use source items. Grouped +roots use the leaf record as `Item`, while the list callback receives the group object. + +Use `createComboboxItems` when the business contract stores a stable primitive ID but list rows +need complete records. Its `getValue` result must be unique and stable, and `getLabel` owns default +filtering, typeahead, and selected-value display. Create static collections at module scope and +memoize collections derived from changing data. Treat the returned collection as opaque and pass +it directly to `items`. + +For server-side search, keep the complete set of records needed to resolve selected labels in the +collection passed to `items`, and pass the current result window to `filteredItems`. Filtered items +are source `Item` records, not derived IDs, and grouped results must retain the collection's group +shape. + +Keep object values when the selected record itself is the business state or the selection callback +immediately needs the full record. When async refreshes may replace object references, provide +`isItemEqualToValue` using the stable domain identity. + +`itemToStringValue` only serializes a selected `Value` for forms and autofill; it does not change +`onValueChange` into an ID callback. Do not add it, `itemToStringLabel`, or a comparator as a +mechanical trio. Primitive IDs normally use the default equality. For async or paged data, keep +selected records in the collection when their labels must remain available after they leave the +current result window, or provide an ID-only label fallback. + `CheckboxGroup` follows Base UI and uses `string[]`. Model stronger business ID distinctions at the domain boundary unless the upstream primitive contract changes. diff --git a/packages/dify-ui/docs/testing.md b/packages/dify-ui/docs/testing.md index 7063601e01e..90cc1c11680 100644 --- a/packages/dify-ui/docs/testing.md +++ b/packages/dify-ui/docs/testing.md @@ -10,6 +10,9 @@ diagnostics. Run the remaining commands from `packages/dify-ui/`: ## Test boundary +This guide owns the Dify UI testing policy and runtime setup. Add tests for observable Dify +integration behavior or a reproducible regression, not merely because a component or prop exists. + The package has two [Vitest projects]. Both run in Playwright Chromium [Browser Mode]; the project name identifies the behavior owner, not a different runtime. @@ -18,9 +21,10 @@ configured accessibility checks through the [Storybook Vitest addon]. Add `play` also owns visible state changes, user interaction, keyboard paths, overlay flows, form behavior, loading behavior, or controlled-state coordination. -Use regular Vitest tests for lower-level wrapper contracts such as class variants, Base UI -passthrough props, hidden-input serialization, data-attribute hooks, stores, and edge cases that -do not need a documented example. +Use regular Vitest tests for Dify integration behavior that does not need a documented example, +such as submitted values, store behavior, or a known regression reached through a public API. +Prop passthrough alone does not justify a test. Assert the resulting behavior instead of CSS class +names or private structure, and do not duplicate behavior already owned by Base UI or the browser. Storybook [accessibility testing] uses `a11y.test = 'error'`, so enabled violations fail the test. Color contrast is the only globally disabled rule because it is a known design-token gap. Do not @@ -30,22 +34,13 @@ use a `play` test in place of an accessibility fix. ## Animation setup Base UI can wait for `element.getAnimations()` before unmounting transition-driven components. -Set its test flag in a Vitest setup file when a test asserts final DOM state rather than animation -behavior: - -```ts -;( - globalThis as typeof globalThis & { - BASE_UI_ANIMATIONS_DISABLED: boolean - } -).BASE_UI_ANIMATIONS_DISABLED = true -``` - -`vitest.setup.ts` already applies this for primitive tests. Storybook uses its preview setup and -must retain real animation lifecycles. A unit test that intentionally asserts animation behavior -may restore the flag to `false` locally, but must restore the previous value during cleanup. +[`vitest.setup.ts`] sets `BASE_UI_ANIMATIONS_DISABLED = true` for primitive tests +that assert final DOM state. Storybook uses its preview setup and retains real animation lifecycles. +A unit test that intentionally asserts animation behavior may set the flag to `false` locally, +but must restore the previous value during cleanup. [Browser Mode]: https://vitest.dev/guide/browser [Storybook Vitest addon]: https://storybook.js.org/docs/writing-tests/integrations/vitest-addon/index [Vitest projects]: https://vitest.dev/guide/projects.html +[`vitest.setup.ts`]: ../vitest.setup.ts [accessibility testing]: https://storybook.js.org/docs/writing-tests/accessibility-testing diff --git a/packages/dify-ui/src/alert-dialog/index.stories.tsx b/packages/dify-ui/src/alert-dialog/index.stories.tsx index bc2cf398148..d0a8fa7efe4 100644 --- a/packages/dify-ui/src/alert-dialog/index.stories.tsx +++ b/packages/dify-ui/src/alert-dialog/index.stories.tsx @@ -152,6 +152,7 @@ export const Controlled: Story = { const LoadingConfirmDemo = () => { const [pending, setPending] = React.useState(false) const [open, setOpen] = React.useState(false) + const confirmLabelId = React.useId() const handleConfirm = () => { setPending(true) @@ -180,8 +181,13 @@ const LoadingConfirmDemo = () => { Cancel - - {pending ? 'Archiving…' : 'Archive'} + + {pending ? 'Archiving…' : 'Archive'} @@ -191,4 +197,16 @@ const LoadingConfirmDemo = () => { export const LoadingConfirm: Story = { render: () => , + play: async ({ canvas, canvasElement, userEvent }) => { + const body = within(canvasElement.ownerDocument.body) + + await userEvent.click(canvas.getByRole('button', { name: 'Archive workspace' })) + const confirmButton = body.getByRole('button', { name: 'Archive' }) + await userEvent.click(confirmButton) + + await expect(confirmButton).toHaveAccessibleName('Archiving…') + await expect(confirmButton).toHaveAttribute('aria-disabled', 'true') + await expect(confirmButton).not.toHaveAttribute('aria-busy') + await expect(confirmButton).toHaveFocus() + }, } diff --git a/packages/dify-ui/src/autocomplete/__tests__/index.spec.tsx b/packages/dify-ui/src/autocomplete/__tests__/index.spec.tsx index 111da03ebb3..024c3984bcf 100644 --- a/packages/dify-ui/src/autocomplete/__tests__/index.spec.tsx +++ b/packages/dify-ui/src/autocomplete/__tests__/index.spec.tsx @@ -19,8 +19,20 @@ import { AutocompleteSeparator, AutocompleteStatus, AutocompleteTrigger, + useAutocompleteFilteredItems, } from '../index' +function AutocompleteTypeExamples() { + const filteredItems = useAutocompleteFilteredItems() + + // @ts-expect-error internally filtered items are read-only + filteredItems.push('workflow') + + return null +} + +void AutocompleteTypeExamples + const renderWithSafeViewport = (ui: React.ReactNode) => render(
{ui}
) diff --git a/packages/dify-ui/src/autocomplete/index.tsx b/packages/dify-ui/src/autocomplete/index.tsx index 42bd7ca5786..e42c71f5484 100644 --- a/packages/dify-ui/src/autocomplete/index.tsx +++ b/packages/dify-ui/src/autocomplete/index.tsx @@ -38,7 +38,8 @@ function Autocomplete(props: AutocompleteProps): React.JSX.Element { const AutocompleteValue = BaseAutocomplete.Value const AutocompleteRow = BaseAutocomplete.Row const useAutocompleteFilter = BaseAutocomplete.useFilter -const useAutocompleteFilteredItems = BaseAutocomplete.useFilteredItems +const useAutocompleteFilteredItems: () => readonly Value[] = + BaseAutocomplete.useFilteredItems type AutocompleteValueProps = BaseAutocomplete.Value.Props type AutocompleteRowProps = BaseAutocomplete.Row.Props diff --git a/packages/dify-ui/src/button/README.md b/packages/dify-ui/src/button/README.md index 8ece6c6bdc0..aa130b0ea91 100644 --- a/packages/dify-ui/src/button/README.md +++ b/packages/dify-ui/src/button/README.md @@ -41,65 +41,42 @@ button semantics. It is not a link mode. | `disabled` | The action is unavailable. | Native-disabled and removed from the tab order. | | `loading` | The action was triggered and is now pending. | Activation is blocked while the button retains focus. | -Internally, Dify UI maps these states to Base UI's interaction contract: +`loading` owns Base UI's disabled interaction, retained focus, and the decorative spinner. The +button remains in the tab order with `aria-disabled`, and Dify UI suppresses activation. Pass the +pending state only to `loading`; keep independent availability conditions in `disabled`: ```tsx -disabled={disabled || loading} -focusableWhenDisabled={focusableWhenDisabled ?? loading} -``` - -Base UI recommends disabling a loading button while setting `focusableWhenDisabled` so that an -action does not lose focus after it is triggered. Dify UI's `loading` prop owns that wiring and -adds the visible spinner. The loading button remains in the tab order with `aria-disabled` -instead of the native [`disabled`] attribute. Unlike native disabled, [`aria-disabled`] preserves -focusability but requires the component to suppress activation. Callers should pass the pending -state only to `loading`: - -```tsx - -``` - -Keep independent availability conditions in `disabled`: - -```tsx - ``` -Do not repeat the same pending state in `disabled`: - -```tsx -// Incorrect: loading already blocks activation. - - -// Incorrect: keep only the independent availability condition in disabled. - - -// Correct. - -``` - -It is valid for `loading` and an independent `disabled` condition to both evaluate to `true`. -The loading focus policy applies while the action is pending; when loading ends, the remaining -availability condition still determines whether the button is disabled. - Pass `focusableWhenDisabled={false}` only when a loading button should opt into native disabled behavior and may leave the tab order. ### Accessible loading feedback -The loading spinner is decorative and does not replace the button's visible label. `Button` does -not add `aria-busy`: [WAI-ARIA `aria-busy`] defines it for an element being modified whose -content changes may be deferred by assistive technology, not as a generic substitute for a -pending action state. When a long-running operation needs an announcement or progress updates, -the feature owns the corresponding status, live region, or progress component. +The spinner is decorative. Keep a non-empty visible label throughout loading. If the visible label +stays the same, its text continues to name the button. + +When the label changes while the focused button enters loading, give the changing text a stable ID +and reference it explicitly. Some browser and screen-reader combinations do not reliably announce +changes to a focused button's descendant text: + +```tsx +const labelId = useId() + + +``` + +The consumer owns this relationship because only it knows whether the label changes and whether +other visible context must also be referenced. Do not replace the changing text with `aria-label`. + +`Button` does not add `aria-busy`: [WAI-ARIA `aria-busy`] describes an element whose own updates may +be deferred by assistive technology, not a generic pending action. Long-running announcements and +progress remain with the feature's status, live-region, or progress owner. ## Content and spacing @@ -126,5 +103,3 @@ for the other variants. Use a `className` override only for a documented layout [Base UI Button]: https://base-ui.com/react/components/button [WAI-ARIA `aria-busy`]: https://www.w3.org/TR/wai-aria#aria-busy [`IconButton`]: ../icon-button/README.md -[`aria-disabled`]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-disabled -[`disabled`]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/disabled diff --git a/packages/dify-ui/src/button/index.stories.tsx b/packages/dify-ui/src/button/index.stories.tsx index 793d7bffd50..4bdb6d3c24b 100644 --- a/packages/dify-ui/src/button/index.stories.tsx +++ b/packages/dify-ui/src/button/index.stories.tsx @@ -1,7 +1,30 @@ import type { Meta, StoryObj } from '@storybook/react-vite' +import type { ButtonProps } from '.' +import * as React from 'react' import { expect, fn } from 'storybook/test' import { Button, buttonVariants } from '.' +type LoadingButtonExampleProps = Pick + +function LoadingButtonExample({ onClick, variant }: LoadingButtonExampleProps) { + const [loading, setLoading] = React.useState(false) + const labelId = React.useId() + + return ( + + ) +} + const meta = { title: 'Base/UI/Button', component: Button, @@ -88,27 +111,30 @@ export const Disabled: Story = { export const Loading: Story = { args: { variant: 'primary', - loading: true, onClick: fn(), - children: 'Loading Button', }, + render: ({ onClick, variant }) => , play: async ({ args, canvas, userEvent }) => { - const button = canvas.getByRole('button', { name: 'Loading Button' }) - - await expect(button).toHaveAttribute('aria-disabled', 'true') - await expect(button).not.toHaveAttribute('aria-busy') + const button = canvas.getByRole('button', { name: 'Save' }) button.focus() await expect(button).toHaveFocus() await userEvent.click(button) - await expect(args.onClick).not.toHaveBeenCalled() + await expect(args.onClick).toHaveBeenCalledTimes(1) + await expect(button).toHaveAccessibleName('Saving') + await expect(button).toHaveAttribute('aria-disabled', 'true') + await expect(button).not.toHaveAttribute('aria-busy') + await expect(button).toHaveFocus() + + await userEvent.keyboard('{Enter}') + await expect(args.onClick).toHaveBeenCalledTimes(1) }, parameters: { docs: { description: { story: - 'Loading buttons remain focusable by default so focus is not lost after activation. Pass `focusableWhenDisabled={false}` to opt out.', + 'When a focused button changes its visible label during loading, give that label a stable ID and reference it with `aria-labelledby`. Loading blocks repeated activation while retaining focus.', }, }, }, diff --git a/packages/dify-ui/src/button/index.tsx b/packages/dify-ui/src/button/index.tsx index d6cb0b7f1d9..629b1032d89 100644 --- a/packages/dify-ui/src/button/index.tsx +++ b/packages/dify-ui/src/button/index.tsx @@ -105,6 +105,11 @@ const buttonVariants = cva( type ButtonProps = Omit & VariantProps & { + /** + * Marks an action as pending, blocks activation, and keeps the button focusable by default. + * Keep a non-empty visible label. If that label changes while loading, give it a stable ID and + * reference it with `aria-labelledby` so it remains the explicit accessible name. + */ loading?: boolean className?: string } diff --git a/packages/dify-ui/src/combobox/__tests__/index.spec.tsx b/packages/dify-ui/src/combobox/__tests__/index.spec.tsx index 0b3ab2673ee..893ff6c9001 100644 --- a/packages/dify-ui/src/combobox/__tests__/index.spec.tsx +++ b/packages/dify-ui/src/combobox/__tests__/index.spec.tsx @@ -7,6 +7,7 @@ import { ComboboxChipRemove, ComboboxChips, ComboboxClear, + ComboboxCollection, ComboboxEmpty, ComboboxGroup, ComboboxGroupLabel, @@ -25,8 +26,56 @@ import { ComboboxStatus, ComboboxTrigger, ComboboxValue, + createComboboxItems, } from '../index' +type ResourceOption = { + id: string + label: string +} + +const resourceOptions: ResourceOption[] = [ + { id: 'workflow', label: 'Workflow' }, + { id: 'dataset', label: 'Dataset' }, +] +const resourceItems = createComboboxItems(resourceOptions, { + getValue: (item) => item.id, + getLabel: (item) => item.label, +}) + +function ComboboxTypeExamples() { + return ( + + + multiple + items={resourceItems} + value={['workflow']} + filter={(item, query) => item.label.includes(query)} + onValueChange={(value) => { + const selectedIds: string[] = value + void selectedIds + }} + > + >{(value) => value?.join(', ') ?? ''} + > + {(item) => value={item.id}>{item.label}} + + items={resourceOptions}> + > + {(item) => value={item.id}>{item.label}} + + + {/* @ts-expect-error item anatomy accepts the derived string value, not the source object */} + value={resourceOptions[0]} /> + + {/* @ts-expect-error root value uses the derived string domain, not the source object */} + items={resourceItems} value={resourceOptions[0]} /> + + ) +} + +void ComboboxTypeExamples + const renderWithSafeViewport = (ui: React.ReactNode) => render(
{ui}
) @@ -113,6 +162,33 @@ describe('Combobox wrappers', () => { .element(screen.getByRole('combobox', { name: 'Resource type' })) .toBeInTheDocument() }) + + it('should expose readonly styling state while allowing options to be inspected', async () => { + const screen = await render( + + + + + + + + + Workflow + Dataset + + + + + , + ) + const trigger = screen.getByRole('combobox', { name: 'Resource type' }) + + await expect.element(trigger).toHaveAttribute('data-readonly') + await trigger.click() + await expect.element(screen.getByRole('option', { name: 'Dataset' })).toBeVisible() + await screen.getByRole('option', { name: 'Dataset' }).click() + await expect.element(trigger).toHaveTextContent('workflow') + }) }) describe('Input group and controls', () => { @@ -206,6 +282,35 @@ describe('Combobox wrappers', () => { }) describe('Popup anatomy and options', () => { + it('should render source objects while exposing primitive selected values', async () => { + const onValueChange = vi.fn() + const screen = await render( + + defaultOpen + items={resourceItems} + defaultValue="workflow" + filter={(item, query) => item.label.toLowerCase().includes(query.toLowerCase())} + onValueChange={(nextValue) => onValueChange(nextValue)} + > + + > + {(item) => ( + key={item.id} value={item.id}> + {item.label} + + )} + + , + ) + + await expect + .element(screen.getByRole('option', { name: 'Workflow' })) + .toHaveAttribute('aria-selected', 'true') + await userEvent.click(screen.getByRole('option', { name: 'Dataset' })) + + expect(onValueChange).toHaveBeenCalledWith('dataset') + }) + it('should use default overlay placement', async () => { const screen = await renderSelectLikeCombobox({ open: true }) diff --git a/packages/dify-ui/src/combobox/index.stories.tsx b/packages/dify-ui/src/combobox/index.stories.tsx index 7b93bff4fe8..406270cf695 100644 --- a/packages/dify-ui/src/combobox/index.stories.tsx +++ b/packages/dify-ui/src/combobox/index.stories.tsx @@ -28,6 +28,7 @@ import { ComboboxStatus, ComboboxTrigger, ComboboxValue, + createComboboxItems, useComboboxFilter, useComboboxFilteredItems, } from '.' @@ -191,6 +192,10 @@ const tagOptions: Option[] = [ { value: 'finance', label: 'Finance' }, { value: 'support', label: 'Support' }, ] +const tagItems = createComboboxItems(tagOptions, { + getValue: (option) => option.value, + getLabel: (option) => option.label, +}) const directoryOptions: Option[] = [ { @@ -832,24 +837,31 @@ const MultipleChipsDemo = () => { Reviewers - - > - {(selectedValue) => ( - - {selectedValue?.map((item) => ( - + > + {(selectedValue) => { + const selectedReviewers = selectedValue ?? [] + + return ( + 0 ? 'Selected reviewers' : undefined} + > + {selectedReviewers.map((item) => ( + {item.label} ))} 0 ? '' : 'Assign reviewers…'} className="min-w-24 px-1 py-0.5" /> - - )} - - + + ) + }} + @@ -861,6 +873,11 @@ const MultipleChipsDemo = () => { Selected reviewers wrap inside the input instead of scrolling horizontally. + {value.length > 0 && ( + + {` ${value.length} selected. From the start of the input, press Left Arrow to focus the selected items`} + + )} ) @@ -871,11 +888,25 @@ export const MultipleChips: Story = { play: async ({ canvas, userEvent }) => { await expect(canvas.getByText('Maya Chen')).toBeVisible() await expect(canvas.getByText('Liam Brooks')).toBeVisible() + await expect(canvas.getByRole('toolbar', { name: 'Selected reviewers' })).toBeVisible() - await userEvent.click(canvas.getByRole('button', { name: 'Remove Maya Chen' })) + await expect(canvas.getByText('Liam Brooks').parentElement!).toHaveAccessibleDescription( + 'Press Backspace or Delete to remove', + ) - await expect(canvas.queryByText('Maya Chen')).not.toBeInTheDocument() - await expect(canvas.getByText('Liam Brooks')).toBeVisible() + const input = canvas.getByRole('combobox', { name: 'Reviewers' }) + await expect(input).toHaveAccessibleDescription( + 'Selected reviewers wrap inside the input instead of scrolling horizontally. 2 selected. From the start of the input, press Left Arrow to focus the selected items', + ) + + input.focus() + await userEvent.keyboard('{ArrowLeft}{Delete}') + + await expect(canvas.queryByText('Liam Brooks')).not.toBeInTheDocument() + await expect(canvas.getByText('Maya Chen')).toBeVisible() + await expect(input).toHaveAccessibleDescription( + 'Selected reviewers wrap inside the input instead of scrolling horizontally. 1 selected. From the start of the input, press Left Arrow to focus the selected items', + ) }, } @@ -955,15 +986,31 @@ export const ReadOnly: Story = { ), + play: async ({ canvas, canvasElement, userEvent }) => { + const input = canvas.getByRole('combobox', { name: 'Read-only source' }) + const body = within(canvasElement.ownerDocument.body) + + await expect(input).toHaveValue('Website crawler') + await userEvent.click(input) + await waitFor(async () => { + await expect(body.getByRole('option', { name: /Notion/ })).toBeVisible() + }) + await userEvent.keyboard('{ArrowDown}') + await expect(body.getByRole('option', { name: /S3 bucket/ })).toHaveAttribute( + 'data-highlighted', + ) + await userEvent.keyboard('{Enter}') + await expect(input).toHaveValue('Website crawler') + }, } const ControlledDemo = () => { - const [value, setValue] = React.useState