managarten/packages/shared-ai/src/index.ts
Till JS fad7f4bea3 feat(ai): guardrail layer — pre/post-plan + pre-execute checks
Add a guardrail system that runs alongside the Mission Runner pipeline
to catch obvious issues before they waste tokens or corrupt data.

Architecture (packages/shared-ai/src/guardrails/):
- types.ts: Guardrail, GuardrailResult, 4 phase interfaces
- builtin.ts: 4 built-in guardrails (always active):
  - input-size-limit: blocks >100K chars of resolved input
  - plan-step-limit: blocks plans with >25 steps (runaway planner)
  - duplicate-destructive-tool: warns if undo_drink called 2x
  - empty-required-params: blocks create_task without title
- runner.ts: runPrePlanGuardrails/runPostPlanGuardrails/runPreExecuteGuardrails

Wired into runner.ts at 3 checkpoints:
- Before deps.plan() — pre-plan check
- After plan received — post-plan check
- Before each stage() call — pre-execute check

Guardrails are synchronous, never hit the network, and produce
clear error messages when they block.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 15:11:34 +02:00

110 lines
2.2 KiB
TypeScript

/**
* @mana/shared-ai
*
* AI Workbench code that both the webapp (SvelteKit/Vite) and the
* server-side mana-ai service (Bun) import. Keep this package free of
* runtime imports from storage layers (Dexie, Postgres) — the types +
* pure functions here must work in both environments.
*/
export type {
Actor,
ActorKind,
BaseActor,
UserActor,
AiActor,
SystemActor,
SystemSource,
} from './actor';
export {
SYSTEM_PROJECTION,
SYSTEM_RULE,
SYSTEM_MIGRATION,
SYSTEM_STREAM,
SYSTEM_MISSION_RUNNER,
LEGACY_USER_PRINCIPAL,
LEGACY_AI_PRINCIPAL,
LEGACY_SYSTEM_PRINCIPAL,
LEGACY_DISPLAY_NAME,
USER_ACTOR,
makeUserActor,
makeAgentActor,
makeSystemActor,
normalizeActor,
isUserActor,
isAiActor,
isSystemActor,
isFromMissionRunner,
} from './actor';
export type {
IterationPhase,
Mission,
MissionCadence,
MissionInputRef,
MissionIteration,
MissionState,
PlanStep,
GrantDerivation,
GrantDerivationVersion,
MissionGrant,
} from './missions';
export {
GRANT_DERIVATION_VERSION,
canonicalInfoString,
deriveMissionDataKey,
deriveMissionDataKeyRaw,
} from './missions';
export type {
AiPlanInput,
AiPlanOutput,
AvailableTool,
ParseResult,
PlannedStep,
PlannerMessages,
ResolvedInput,
} from './planner';
export { buildPlannerPrompt, parsePlannerResponse } from './planner';
export {
AI_PROPOSABLE_TOOL_NAMES,
AI_PROPOSABLE_TOOL_SET,
type AiProposableToolName,
type AiPolicy,
type PolicyDecision,
} from './policy';
export type { ToolSchema } from './tools';
export { AI_TOOL_CATALOG, AI_TOOL_CATALOG_BY_NAME } from './tools';
export type {
Guardrail,
GuardrailPhase,
GuardrailResult,
GuardrailCheckResult,
} from './guardrails';
export {
BUILTIN_GUARDRAILS,
runPrePlanGuardrails,
runPostPlanGuardrails,
runPreExecuteGuardrails,
} from './guardrails';
export type {
Agent,
AgentState,
AgentTemplate,
AgentTemplateAgentPart,
AgentTemplateScenePart,
AgentTemplateSceneApp,
AgentTemplateMissionPart,
WorkbenchTemplate,
WorkbenchTemplateAgentPart,
WorkbenchTemplateScenePart,
WorkbenchTemplateSceneApp,
WorkbenchTemplateMissionPart,
WorkbenchTemplateSeedItem,
WorkbenchTemplateCategory,
} from './agents';
export { DEFAULT_AGENT_ID, DEFAULT_AGENT_NAME, ALL_TEMPLATES, getTemplateById } from './agents';