Wire AI assistant to arcadia: domain primer, tool calling, admin context
Make /ai and /assistant operate as the platform admin's assistant
against arcadia-app's API:
- Add `arcadia-knowledge.ts` — domain primer (multi-tenant Phoenix
backend, tenant lifecycle, platform_admins identity, etc.) baked into
every system prompt.
- Add `admin-tools.ts` — curated tool registry exposing `list_tenants`
and `get_tenant`, callable via OpenAI-native function calling. Tools
hit arcadia through `useArcadiaClient()` and inherit the operator's
JWT + tenant header. `runLLMToolCalls()` returns `tool` role messages
ready to push back into history.
- Add `admin-context.ts` — runtime registry pages publish to so the
assistant can answer factual questions about live UI state without
scraping the DOM. Tenants page registers its summary on mount.
- Replace generic Vibespace personas (Atlas/Forge/Inkwell/Pilot/Cursor)
with arcadia-flavoured ones: Operator, Auditor, Triage, Analyst,
UI Operator. Auto-migrate stored agents from the legacy set.
- /assistant: build admin preface (role + primer + persona + ctx) and
pass it as the `useChat` system at construction. Pass `tools` on every
`send()`. Auto-loop reads `toolCalls` off the streaming assistant
message and uses `continueChat()` to push tool results.
- /ai: same wiring (this is the canonical admin chat surface; the user
prefers its look).
- MessageBody renders tool-result cards (role: "tool") and a "Called X"
pill on assistant messages with toolCalls. Strips Qwen-style
`<tool_call>` XML from prose when the tags were converted to
structured calls.
- Extend ThreadMessage with the `tool` role + tool-call metadata so
conversations round-trip through localStorage.
- Tenants page: row actions get `data-action="tenant-<slug>-{suspend,
activate,deactivate}"` (via lib-table-ui's new dataAction prop);
registers tenant summary into admin-context.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
76
app/lib/admin-context.ts
Normal file
76
app/lib/admin-context.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
// Shared state surface that any admin page can publish to so the assistant
|
||||
// can read live data without scraping the DOM.
|
||||
//
|
||||
// Pages call `useRegisterAdminContext("tenants", { tenants: [...] })` while
|
||||
// mounted; the assistant calls `getAdminContextSnapshot()` each turn to
|
||||
// inject a structured snapshot into the system prompt.
|
||||
|
||||
import { useEffect } from "react"
|
||||
|
||||
type Surface = Record<string, unknown>
|
||||
|
||||
export type AdminContextSnapshot = {
|
||||
route: string
|
||||
surfaces: Record<string, Surface>
|
||||
}
|
||||
|
||||
const surfaces = new Map<string, Surface>()
|
||||
|
||||
export function publishAdminSurface(name: string, data: Surface): void {
|
||||
surfaces.set(name, data)
|
||||
if (typeof window !== "undefined") {
|
||||
;(window as unknown as { __adminContext?: unknown }).__adminContext = getAdminContextSnapshot()
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAdminSurface(name: string): void {
|
||||
surfaces.delete(name)
|
||||
if (typeof window !== "undefined") {
|
||||
;(window as unknown as { __adminContext?: unknown }).__adminContext = getAdminContextSnapshot()
|
||||
}
|
||||
}
|
||||
|
||||
export function getAdminContextSnapshot(): AdminContextSnapshot {
|
||||
const route = typeof window !== "undefined" ? window.location.pathname : ""
|
||||
return {
|
||||
route,
|
||||
surfaces: Object.fromEntries(surfaces.entries()),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a snapshot as a markdown block for the LLM system prompt.
|
||||
* Keeps it compact: route, then one section per surface with JSON.
|
||||
*/
|
||||
export function formatAdminContextForPrompt(snapshot = getAdminContextSnapshot()): string {
|
||||
const sections: string[] = [`Admin context (read-only — for answering factual questions):`]
|
||||
sections.push(`Route: ${snapshot.route || "?"}`)
|
||||
const names = Object.keys(snapshot.surfaces)
|
||||
if (names.length === 0) {
|
||||
sections.push(`Surfaces: (none registered)`)
|
||||
} else {
|
||||
for (const name of names) {
|
||||
const json = safeJson(snapshot.surfaces[name])
|
||||
sections.push(`Surface "${name}":\n${json}`)
|
||||
}
|
||||
}
|
||||
return sections.join("\n\n")
|
||||
}
|
||||
|
||||
function safeJson(value: unknown): string {
|
||||
try {
|
||||
const text = JSON.stringify(value, null, 2)
|
||||
if (text.length > 4000) return text.slice(0, 4000) + "\n…(truncated)"
|
||||
return text
|
||||
} catch {
|
||||
return "(unserializable)"
|
||||
}
|
||||
}
|
||||
|
||||
/** Hook: publish a surface while the component is mounted. */
|
||||
export function useRegisterAdminContext(name: string, data: Surface): void {
|
||||
useEffect(() => {
|
||||
publishAdminSurface(name, data)
|
||||
return () => clearAdminSurface(name)
|
||||
}, [name, data])
|
||||
}
|
||||
Reference in New Issue
Block a user