fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
DOCK_WIDTH_DEFAULT,
|
||||
DOCK_WIDTH_MIN,
|
||||
DOCK_WIDTH_MAX,
|
||||
DOCK_GUTTER,
|
||||
MIN_PAGE_WIDTH,
|
||||
EXPANDED_WIDTH_MAX,
|
||||
FLOAT_MIN_W,
|
||||
FLOAT_MIN_H,
|
||||
FLOAT_KEEP_ON_SCREEN,
|
||||
clampDockWidth,
|
||||
clampFloatRect,
|
||||
containFloatRect,
|
||||
defaultFloatRect,
|
||||
expandedDockWidth,
|
||||
resizeFloatRect,
|
||||
resolveAgentPanelPrefs,
|
||||
serializeAgentPanelPrefs,
|
||||
} from '../geometry'
|
||||
|
||||
const WIDE = 1920
|
||||
const TALL = 1080
|
||||
const NAV = 248
|
||||
|
||||
describe('resolveAgentPanelPrefs', () => {
|
||||
it('defaults on absent input', () => {
|
||||
expect(resolveAgentPanelPrefs(undefined)).toEqual({
|
||||
mode: 'docked',
|
||||
dockWidth: DOCK_WIDTH_DEFAULT,
|
||||
float: null,
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps a valid persisted state', () => {
|
||||
const prefs = resolveAgentPanelPrefs({
|
||||
mode: 'floating',
|
||||
dock_width: 600,
|
||||
float: { x: 100, y: 50, w: 500, h: 700 },
|
||||
})
|
||||
expect(prefs).toEqual({
|
||||
mode: 'floating',
|
||||
dockWidth: 600,
|
||||
float: { x: 100, y: 50, w: 500, h: 700 },
|
||||
})
|
||||
})
|
||||
|
||||
it('discards garbage values instead of propagating them', () => {
|
||||
const prefs = resolveAgentPanelPrefs({
|
||||
mode: 'floating',
|
||||
dock_width: Number.NaN,
|
||||
// Partial/garbage rect: one bad member invalidates the whole rect.
|
||||
float: { x: 10, y: 10, w: Number.POSITIVE_INFINITY, h: 400 },
|
||||
})
|
||||
expect(prefs.dockWidth).toBe(DOCK_WIDTH_DEFAULT)
|
||||
expect(prefs.float).toBeNull()
|
||||
})
|
||||
|
||||
it('hard-clamps a persisted dock width to the fixed bounds', () => {
|
||||
expect(resolveAgentPanelPrefs({ dock_width: 40 }).dockWidth).toBe(DOCK_WIDTH_MIN)
|
||||
expect(resolveAgentPanelPrefs({ dock_width: 4000 }).dockWidth).toBe(DOCK_WIDTH_MAX)
|
||||
})
|
||||
})
|
||||
|
||||
describe('serializeAgentPanelPrefs', () => {
|
||||
it('rounds pixels to integers (the API schema rejects fractions)', () => {
|
||||
expect(
|
||||
serializeAgentPanelPrefs({
|
||||
mode: 'floating',
|
||||
dockWidth: 480.6,
|
||||
float: { x: 10.4, y: 20.5, w: 400.2, h: 500.9 },
|
||||
}),
|
||||
).toEqual({
|
||||
mode: 'floating',
|
||||
dock_width: 481,
|
||||
float: { x: 10, y: 21, w: 400, h: 501 },
|
||||
})
|
||||
})
|
||||
|
||||
it('omits float when there is none', () => {
|
||||
expect(serializeAgentPanelPrefs({ mode: 'docked', dockWidth: 480, float: null })).toEqual({
|
||||
mode: 'docked',
|
||||
dock_width: 480,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('clampDockWidth', () => {
|
||||
it('passes through a width the viewport can afford', () => {
|
||||
expect(clampDockWidth(600, WIDE, NAV)).toBe(600)
|
||||
})
|
||||
|
||||
it('caps at the fixed maximum on huge viewports', () => {
|
||||
expect(clampDockWidth(5000, 3840, NAV)).toBe(DOCK_WIDTH_MAX)
|
||||
})
|
||||
|
||||
it('never lets the page drop below its minimum readable width', () => {
|
||||
const viewport = 1280
|
||||
const clamped = clampDockWidth(DOCK_WIDTH_MAX, viewport, NAV)
|
||||
expect(viewport - NAV - clamped - DOCK_GUTTER).toBeGreaterThanOrEqual(MIN_PAGE_WIDTH)
|
||||
})
|
||||
|
||||
it('floors at the panel minimum even when the viewport is too small for both', () => {
|
||||
expect(clampDockWidth(100, 900, NAV)).toBe(DOCK_WIDTH_MIN)
|
||||
})
|
||||
})
|
||||
|
||||
describe('expandedDockWidth', () => {
|
||||
it('caps at the fixed expanded maximum on huge viewports', () => {
|
||||
expect(expandedDockWidth(3840, NAV)).toBe(EXPANDED_WIDTH_MAX)
|
||||
})
|
||||
|
||||
it('reserves the minimum page width on mid-size viewports', () => {
|
||||
const viewport = 1440
|
||||
expect(expandedDockWidth(viewport, NAV)).toBe(viewport - NAV - MIN_PAGE_WIDTH - DOCK_GUTTER)
|
||||
})
|
||||
|
||||
it('floors at the default compact width', () => {
|
||||
expect(expandedDockWidth(1000, NAV)).toBe(DOCK_WIDTH_DEFAULT)
|
||||
})
|
||||
})
|
||||
|
||||
describe('clampFloatRect', () => {
|
||||
it('keeps a legal rect unchanged', () => {
|
||||
const rect = { x: 200, y: 100, w: 420, h: 640 }
|
||||
expect(clampFloatRect(rect, WIDE, TALL)).toEqual(rect)
|
||||
})
|
||||
|
||||
it('enforces minimum size', () => {
|
||||
const r = clampFloatRect({ x: 0, y: 0, w: 10, h: 10 }, WIDE, TALL)
|
||||
expect(r.w).toBe(FLOAT_MIN_W)
|
||||
expect(r.h).toBe(FLOAT_MIN_H)
|
||||
})
|
||||
|
||||
it('keeps the panel reachable when dragged off the right edge', () => {
|
||||
const r = clampFloatRect({ x: 99999, y: 100, w: 420, h: 640 }, WIDE, TALL)
|
||||
expect(r.x).toBe(WIDE - FLOAT_KEEP_ON_SCREEN)
|
||||
})
|
||||
|
||||
it('never lets the header go above the top edge', () => {
|
||||
const r = clampFloatRect({ x: 100, y: -500, w: 420, h: 640 }, WIDE, TALL)
|
||||
expect(r.y).toBe(0)
|
||||
})
|
||||
|
||||
it('keeps the header reachable at the bottom', () => {
|
||||
const r = clampFloatRect({ x: 100, y: 99999, w: 420, h: 640 }, WIDE, TALL)
|
||||
expect(r.y).toBe(TALL - FLOAT_KEEP_ON_SCREEN)
|
||||
})
|
||||
})
|
||||
|
||||
describe('containFloatRect', () => {
|
||||
it('keeps a fully visible rect unchanged', () => {
|
||||
const rect = { x: 200, y: 100, w: 420, h: 640 }
|
||||
expect(containFloatRect(rect, WIDE, TALL)).toEqual(rect)
|
||||
})
|
||||
|
||||
it('pulls a right-edge sliver fully inside the viewport', () => {
|
||||
// x = 1392 is legal for clampFloatRect (48px visible) but renders as a
|
||||
// sliver; contain pulls the whole window on screen.
|
||||
const r = containFloatRect({ x: 1392, y: 100, w: 420, h: 640 }, 1440, 900)
|
||||
expect(r).toEqual({ x: 1440 - 420, y: 100, w: 420, h: 640 })
|
||||
})
|
||||
|
||||
it('pulls a left-hanging rect back to the left edge', () => {
|
||||
const r = containFloatRect({ x: -372, y: 100, w: 420, h: 640 }, 1440, 900)
|
||||
expect(r).toEqual({ x: 0, y: 100, w: 420, h: 640 })
|
||||
})
|
||||
|
||||
it('migrates a rect persisted on a larger screen fully on screen', () => {
|
||||
// Saved bottom-right on a 2560x1440 external monitor, reopened on a
|
||||
// 1440x900 laptop: both axes end flush with the smaller viewport.
|
||||
const r = containFloatRect({ x: 2116, y: 776, w: 420, h: 640 }, 1440, 900)
|
||||
expect(r).toEqual({ x: 1020, y: 260, w: 420, h: 640 })
|
||||
})
|
||||
|
||||
it('enforces the minimum size before containing', () => {
|
||||
const r = containFloatRect({ x: 99999, y: 99999, w: 10, h: 10 }, WIDE, TALL)
|
||||
expect(r.w).toBe(FLOAT_MIN_W)
|
||||
expect(r.h).toBe(FLOAT_MIN_H)
|
||||
expect(r.x).toBe(WIDE - FLOAT_MIN_W)
|
||||
expect(r.y).toBe(TALL - FLOAT_MIN_H)
|
||||
})
|
||||
|
||||
it('output is a fixpoint of clampFloatRect and of itself', () => {
|
||||
// Pins the render-clamp no-op invariant: the contained rect passed
|
||||
// through clampFloatRect (what AgentSheet renders) must not move again.
|
||||
const cases = [
|
||||
{ x: 2116, y: 776, w: 420, h: 640 },
|
||||
{ x: -372, y: -50, w: 420, h: 640 },
|
||||
{ x: 0, y: 0, w: 10, h: 10 },
|
||||
{ x: 500, y: 300, w: 5000, h: 5000 },
|
||||
]
|
||||
for (const rect of cases) {
|
||||
const contained = containFloatRect(rect, 1440, 900)
|
||||
expect(clampFloatRect(contained, 1440, 900)).toEqual(contained)
|
||||
expect(containFloatRect(contained, 1440, 900)).toEqual(contained)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('defaultFloatRect', () => {
|
||||
it('spawns bottom-right and fully on screen', () => {
|
||||
const r = defaultFloatRect(WIDE, TALL)
|
||||
expect(r.x + r.w).toBeLessThanOrEqual(WIDE)
|
||||
expect(r.y + r.h).toBeLessThanOrEqual(TALL)
|
||||
expect(r.x).toBeGreaterThan(WIDE / 2)
|
||||
})
|
||||
|
||||
it('shrinks to fit a small desktop viewport', () => {
|
||||
const r = defaultFloatRect(800, 500)
|
||||
expect(r.w).toBeLessThanOrEqual(800)
|
||||
expect(r.h).toBeLessThanOrEqual(500)
|
||||
expect(r.y).toBeGreaterThanOrEqual(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('resizeFloatRect', () => {
|
||||
const base = { x: 400, y: 200, w: 500, h: 600 }
|
||||
|
||||
it('grows from the right edge without moving x', () => {
|
||||
const r = resizeFloatRect(base, 80, 0, { right: true }, WIDE, TALL)
|
||||
expect(r).toEqual({ ...base, w: 580 })
|
||||
})
|
||||
|
||||
it('grows from the bottom edge', () => {
|
||||
const r = resizeFloatRect(base, 0, 50, { bottom: true }, WIDE, TALL)
|
||||
expect(r).toEqual({ ...base, h: 650 })
|
||||
})
|
||||
|
||||
it('anchors the right edge on a left-edge drag', () => {
|
||||
const r = resizeFloatRect(base, -60, 0, { left: true }, WIDE, TALL)
|
||||
expect(r.x).toBe(base.x - 60)
|
||||
expect(r.x + r.w).toBe(base.x + base.w)
|
||||
})
|
||||
|
||||
it('keeps the right edge anchored when the minimum width clamp kicks in', () => {
|
||||
const r = resizeFloatRect(base, 400, 0, { left: true }, WIDE, TALL)
|
||||
expect(r.w).toBe(FLOAT_MIN_W)
|
||||
expect(r.x + r.w).toBe(base.x + base.w)
|
||||
})
|
||||
|
||||
it('resizes both axes from a corner', () => {
|
||||
const r = resizeFloatRect(base, 40, 30, { right: true, bottom: true }, WIDE, TALL)
|
||||
expect(r).toEqual({ ...base, w: 540, h: 630 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import {
|
||||
AGENT_SHEET_SESSION_KEY,
|
||||
clearAgentSheetSession,
|
||||
parseAgentSheetSession,
|
||||
readAgentSheetSession,
|
||||
writeAgentSheetSession,
|
||||
} from '../session-restore'
|
||||
|
||||
// A minimal sessionStorage double on a fake window; the helpers must also be
|
||||
// inert when neither exists (server render) or when access throws.
|
||||
function installStorage(opts: { throwOnAccess?: boolean } = {}) {
|
||||
const map = new Map<string, string>()
|
||||
const store = {
|
||||
getItem: (k: string) => map.get(k) ?? null,
|
||||
setItem: (k: string, v: string) => {
|
||||
map.set(k, v)
|
||||
},
|
||||
removeItem: (k: string) => {
|
||||
map.delete(k)
|
||||
},
|
||||
}
|
||||
const win: Record<string, unknown> = {}
|
||||
if (opts.throwOnAccess) {
|
||||
Object.defineProperty(win, 'sessionStorage', {
|
||||
get() {
|
||||
throw new Error('SecurityError')
|
||||
},
|
||||
})
|
||||
} else {
|
||||
win.sessionStorage = store
|
||||
}
|
||||
;(globalThis as { window?: unknown }).window = win
|
||||
return map
|
||||
}
|
||||
|
||||
const saved = (globalThis as { window?: unknown }).window
|
||||
beforeEach(() => {
|
||||
delete (globalThis as { window?: unknown }).window
|
||||
})
|
||||
afterEach(() => {
|
||||
if (saved === undefined) delete (globalThis as { window?: unknown }).window
|
||||
else (globalThis as { window?: unknown }).window = saved
|
||||
})
|
||||
|
||||
const session = {
|
||||
conversationId: 'conv-1',
|
||||
intentId: 'general.help',
|
||||
contextRef: 'report:vat:2026-07',
|
||||
collapsed: false,
|
||||
}
|
||||
|
||||
describe('session-restore', () => {
|
||||
it('round-trips the open thread through sessionStorage', () => {
|
||||
const map = installStorage()
|
||||
writeAgentSheetSession(session)
|
||||
expect(map.has(AGENT_SHEET_SESSION_KEY)).toBe(true)
|
||||
expect(readAgentSheetSession()).toEqual(session)
|
||||
clearAgentSheetSession()
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
})
|
||||
|
||||
it('reads nothing without a window (server) and never throws', () => {
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
expect(() => writeAgentSheetSession(session)).not.toThrow()
|
||||
expect(() => clearAgentSheetSession()).not.toThrow()
|
||||
})
|
||||
|
||||
it('treats a storage that throws on access as empty', () => {
|
||||
installStorage({ throwOnAccess: true })
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
expect(() => writeAgentSheetSession(session)).not.toThrow()
|
||||
})
|
||||
|
||||
it('ignores malformed stored values', () => {
|
||||
const map = installStorage()
|
||||
map.set(AGENT_SHEET_SESSION_KEY, 'not json')
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
map.set(AGENT_SHEET_SESSION_KEY, JSON.stringify({ intentId: 'general.help' }))
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
})
|
||||
|
||||
it('parses defensively: contextRef defaults to null, collapsed to false', () => {
|
||||
expect(parseAgentSheetSession({ conversationId: 'c', intentId: 'i' })).toEqual({
|
||||
conversationId: 'c',
|
||||
intentId: 'i',
|
||||
contextRef: null,
|
||||
collapsed: false,
|
||||
})
|
||||
expect(parseAgentSheetSession({ conversationId: 'c', intentId: 'i', collapsed: true, contextRef: 7 })).toEqual({
|
||||
conversationId: 'c',
|
||||
intentId: 'i',
|
||||
contextRef: null,
|
||||
collapsed: true,
|
||||
})
|
||||
expect(parseAgentSheetSession(null)).toBeNull()
|
||||
expect(parseAgentSheetSession({ conversationId: '', intentId: 'i' })).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,186 @@
|
||||
import type { AgentPanelFloatRect, AgentPanelState } from '@/types'
|
||||
|
||||
/**
|
||||
* Pure geometry for the assistant panel (components/agent/AgentSheet).
|
||||
*
|
||||
* The panel has two modes:
|
||||
* - docked: full-height sheet on the right edge; the page panel gives up
|
||||
* `width + DOCK_GUTTER` of right margin (via --agent-dock-w) so content
|
||||
* reflows beside the panel instead of being covered by it.
|
||||
* - floating: a free window the user drags and resizes anywhere; the page
|
||||
* keeps its full width and the user decides what the window may cover.
|
||||
*
|
||||
* All clamping lives here, testable and shared between the live drag path
|
||||
* (imperative style writes) and the persisted-preference path.
|
||||
*/
|
||||
|
||||
export const DOCK_WIDTH_DEFAULT = 480
|
||||
export const DOCK_WIDTH_MIN = 380
|
||||
export const DOCK_WIDTH_MAX = 800
|
||||
/** Frame gutter added on top of the panel width when reserving page margin,
|
||||
* so panel and page float side by side with the same 10px seam as the frame. */
|
||||
export const DOCK_GUTTER = 10
|
||||
/** The page keeps at least this much width before the docked panel stops growing. */
|
||||
export const MIN_PAGE_WIDTH = 480
|
||||
/** Ceiling for the expanded (focus) width; beyond this a chat column stops
|
||||
* gaining readability. */
|
||||
export const EXPANDED_WIDTH_MAX = 1100
|
||||
|
||||
export const FLOAT_MIN_W = 360
|
||||
export const FLOAT_MIN_H = 400
|
||||
export const FLOAT_DEFAULT_W = 420
|
||||
export const FLOAT_DEFAULT_H = 640
|
||||
/** Gap kept between a freshly spawned floating panel and the viewport edge. */
|
||||
export const FLOAT_SPAWN_MARGIN = 24
|
||||
/** At least this much of the panel stays inside the viewport, so the header
|
||||
* (the drag surface) is always reachable to drag it back. */
|
||||
export const FLOAT_KEEP_ON_SCREEN = 48
|
||||
|
||||
/** Resolved, always-valid panel preferences (persisted shape is all-optional). */
|
||||
export interface ResolvedAgentPanelPrefs {
|
||||
mode: 'docked' | 'floating'
|
||||
dockWidth: number
|
||||
float: AgentPanelFloatRect | null
|
||||
}
|
||||
|
||||
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v)
|
||||
|
||||
/**
|
||||
* Normalize a persisted (or absent, or garbage) agent_panel value into a
|
||||
* usable preference object. Viewport-dependent clamping happens at use time;
|
||||
* this only guards types and hard bounds.
|
||||
*/
|
||||
export function resolveAgentPanelPrefs(
|
||||
raw: AgentPanelState | null | undefined,
|
||||
): ResolvedAgentPanelPrefs {
|
||||
const mode = raw?.mode === 'floating' ? 'floating' : 'docked'
|
||||
const dockWidth = finite(raw?.dock_width)
|
||||
? Math.min(Math.max(raw.dock_width, DOCK_WIDTH_MIN), DOCK_WIDTH_MAX)
|
||||
: DOCK_WIDTH_DEFAULT
|
||||
const f = raw?.float
|
||||
const float =
|
||||
f && finite(f.x) && finite(f.y) && finite(f.w) && finite(f.h)
|
||||
? { x: f.x, y: f.y, w: f.w, h: f.h }
|
||||
: null
|
||||
return { mode, dockWidth, float }
|
||||
}
|
||||
|
||||
/** Persisted wire shape (integers: the API schema rejects fractional pixels). */
|
||||
export function serializeAgentPanelPrefs(prefs: ResolvedAgentPanelPrefs): AgentPanelState {
|
||||
return {
|
||||
mode: prefs.mode,
|
||||
dock_width: Math.round(prefs.dockWidth),
|
||||
...(prefs.float
|
||||
? {
|
||||
float: {
|
||||
x: Math.round(prefs.float.x),
|
||||
y: Math.round(prefs.float.y),
|
||||
w: Math.round(prefs.float.w),
|
||||
h: Math.round(prefs.float.h),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamp a docked width so the page keeps MIN_PAGE_WIDTH beside the panel.
|
||||
* `navWidth` is the sidebar column (--nav-w); on viewports too small for
|
||||
* both minimums the panel floor wins (the md: margin is inert there anyway).
|
||||
*/
|
||||
export function clampDockWidth(px: number, viewportW: number, navWidth: number): number {
|
||||
const available = Math.max(DOCK_WIDTH_MIN, viewportW - navWidth - MIN_PAGE_WIDTH - DOCK_GUTTER)
|
||||
const max = Math.min(DOCK_WIDTH_MAX, available)
|
||||
return Math.round(Math.min(Math.max(px, DOCK_WIDTH_MIN), max))
|
||||
}
|
||||
|
||||
/**
|
||||
* Width of the expanded (focus) panel: as wide as the viewport allows while
|
||||
* the page keeps MIN_PAGE_WIDTH, capped at EXPANDED_WIDTH_MAX. Unlike the
|
||||
* old fixed 1100px overlay, this reserves page margin like the compact dock.
|
||||
*/
|
||||
export function expandedDockWidth(viewportW: number, navWidth: number): number {
|
||||
const available = viewportW - navWidth - MIN_PAGE_WIDTH - DOCK_GUTTER
|
||||
return Math.round(Math.min(EXPANDED_WIDTH_MAX, Math.max(DOCK_WIDTH_DEFAULT, available)))
|
||||
}
|
||||
|
||||
/** Keep a floating rect at legal size and reachably on screen. */
|
||||
export function clampFloatRect(
|
||||
rect: AgentPanelFloatRect,
|
||||
viewportW: number,
|
||||
viewportH: number,
|
||||
): AgentPanelFloatRect {
|
||||
const w = Math.round(Math.min(Math.max(rect.w, FLOAT_MIN_W), Math.max(FLOAT_MIN_W, viewportW)))
|
||||
const h = Math.round(Math.min(Math.max(rect.h, FLOAT_MIN_H), Math.max(FLOAT_MIN_H, viewportH)))
|
||||
// Horizontally the panel may hang off either side as long as
|
||||
// FLOAT_KEEP_ON_SCREEN of it stays visible; vertically the header must
|
||||
// never go above the top edge (it is the only drag surface).
|
||||
const x = Math.round(
|
||||
Math.min(Math.max(rect.x, FLOAT_KEEP_ON_SCREEN - w), viewportW - FLOAT_KEEP_ON_SCREEN),
|
||||
)
|
||||
const y = Math.round(Math.min(Math.max(rect.y, 0), Math.max(0, viewportH - FLOAT_KEEP_ON_SCREEN)))
|
||||
return { x, y, w, h }
|
||||
}
|
||||
|
||||
/**
|
||||
* Snap a floating rect fully inside the viewport. clampFloatRect above only
|
||||
* guarantees FLOAT_KEEP_ON_SCREEN of the panel stays reachable (so a live
|
||||
* drag may deliberately hang off an edge), which means a rect persisted at
|
||||
* the edge, or on a larger monitor, legally renders as a 48px sliver on the
|
||||
* next open. This is the open/resize-time validation: whole window visible.
|
||||
* The output is always a fixpoint of clampFloatRect (w and h end up >= the
|
||||
* minimums, x and y inside the reachability bounds), so the render clamp
|
||||
* applied afterwards stays a no-op.
|
||||
*/
|
||||
export function containFloatRect(
|
||||
rect: AgentPanelFloatRect,
|
||||
viewportW: number,
|
||||
viewportH: number,
|
||||
): AgentPanelFloatRect {
|
||||
const w = Math.round(Math.min(Math.max(rect.w, FLOAT_MIN_W), Math.max(FLOAT_MIN_W, viewportW)))
|
||||
const h = Math.round(Math.min(Math.max(rect.h, FLOAT_MIN_H), Math.max(FLOAT_MIN_H, viewportH)))
|
||||
const x = Math.round(Math.min(Math.max(rect.x, 0), Math.max(0, viewportW - w)))
|
||||
const y = Math.round(Math.min(Math.max(rect.y, 0), Math.max(0, viewportH - h)))
|
||||
return { x, y, w, h }
|
||||
}
|
||||
|
||||
/** First-undock placement: bottom-right, mirroring where the FAB lives. */
|
||||
export function defaultFloatRect(viewportW: number, viewportH: number): AgentPanelFloatRect {
|
||||
const w = Math.min(FLOAT_DEFAULT_W, Math.max(FLOAT_MIN_W, viewportW - 2 * FLOAT_SPAWN_MARGIN))
|
||||
const h = Math.min(FLOAT_DEFAULT_H, Math.max(FLOAT_MIN_H, viewportH - 2 * FLOAT_SPAWN_MARGIN))
|
||||
return clampFloatRect(
|
||||
{ x: viewportW - w - FLOAT_SPAWN_MARGIN, y: viewportH - h - FLOAT_SPAWN_MARGIN, w, h },
|
||||
viewportW,
|
||||
viewportH,
|
||||
)
|
||||
}
|
||||
|
||||
export interface ResizeEdges {
|
||||
left?: boolean
|
||||
right?: boolean
|
||||
bottom?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a pointer drag delta to a floating rect from the given edges.
|
||||
* A left-edge drag moves x with the width so the right edge stays anchored,
|
||||
* including when the width clamp kicks in.
|
||||
*/
|
||||
export function resizeFloatRect(
|
||||
base: AgentPanelFloatRect,
|
||||
dx: number,
|
||||
dy: number,
|
||||
edges: ResizeEdges,
|
||||
viewportW: number,
|
||||
viewportH: number,
|
||||
): AgentPanelFloatRect {
|
||||
let w = base.w
|
||||
if (edges.right) w = base.w + dx
|
||||
if (edges.left) w = base.w - dx
|
||||
const h = edges.bottom ? base.h + dy : base.h
|
||||
|
||||
const clampedW = Math.min(Math.max(w, FLOAT_MIN_W), Math.max(FLOAT_MIN_W, viewportW))
|
||||
const x = edges.left ? base.x + (base.w - clampedW) : base.x
|
||||
|
||||
return clampFloatRect({ x, y: base.y, w: clampedW, h }, viewportW, viewportH)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Per-tab memory of which thread the docked assistant panel had open.
|
||||
*
|
||||
* The panel's state lives in React only, so a full page reload (the "Ladda om"
|
||||
* deploy prompt, a browser refresh, a crash recovery) closed it and dropped the
|
||||
* thread from view; the user had to dig it out of "Tidigare konversationer" by
|
||||
* hand. This remembers just enough to reopen the same thread after a reload:
|
||||
* the conversation id (the thread itself is already persisted server-side),
|
||||
* the intent that shaped it, and whether the panel was collapsed.
|
||||
*
|
||||
* sessionStorage on purpose, not user_preferences: this is "this tab, this
|
||||
* session" state. It survives a reload of the tab and nothing else, so a
|
||||
* thread never follows the user to another device or another tab, and a closed
|
||||
* tab forgets it. Nothing here is authoritative: the id is re-validated by the
|
||||
* conversations API on restore, and a thread that no longer opens is dropped.
|
||||
*/
|
||||
|
||||
export const AGENT_SHEET_SESSION_KEY = 'accounted-agent-sheet'
|
||||
|
||||
export interface AgentSheetSession {
|
||||
conversationId: string
|
||||
intentId: string
|
||||
contextRef: string | null
|
||||
collapsed: boolean
|
||||
}
|
||||
|
||||
function storage(): Storage | null {
|
||||
try {
|
||||
if (typeof window === 'undefined') return null
|
||||
return window.sessionStorage
|
||||
} catch {
|
||||
// Storage access can throw (privacy modes, sandboxed frames): then there
|
||||
// is simply nothing to remember.
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function readAgentSheetSession(): AgentSheetSession | null {
|
||||
const store = storage()
|
||||
if (!store) return null
|
||||
try {
|
||||
const raw = store.getItem(AGENT_SHEET_SESSION_KEY)
|
||||
if (!raw) return null
|
||||
return parseAgentSheetSession(JSON.parse(raw))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate an untrusted parsed value; anything malformed reads as "nothing stored". */
|
||||
export function parseAgentSheetSession(value: unknown): AgentSheetSession | null {
|
||||
if (!value || typeof value !== 'object') return null
|
||||
const v = value as Record<string, unknown>
|
||||
if (typeof v.conversationId !== 'string' || v.conversationId.length === 0) return null
|
||||
if (typeof v.intentId !== 'string' || v.intentId.length === 0) return null
|
||||
return {
|
||||
conversationId: v.conversationId,
|
||||
intentId: v.intentId,
|
||||
contextRef: typeof v.contextRef === 'string' ? v.contextRef : null,
|
||||
collapsed: v.collapsed === true,
|
||||
}
|
||||
}
|
||||
|
||||
export function writeAgentSheetSession(session: AgentSheetSession): void {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
try {
|
||||
store.setItem(AGENT_SHEET_SESSION_KEY, JSON.stringify(session))
|
||||
} catch {
|
||||
// Quota or privacy mode: a lost write only costs the reload convenience.
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAgentSheetSession(): void {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
try {
|
||||
store.removeItem(AGENT_SHEET_SESSION_KEY)
|
||||
} catch {
|
||||
// ignored: see writeAgentSheetSession
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user