Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

1.6 KB
Raw Blame
'use client';

import { useTrueForgeAgentSpec, useTrueForgeUpdateAgentSpec } from '@truefoundry/trueforge-assistant-ui-runtime';
import { createContext, useCallback, useContext, useMemo, type ReactNode } from 'react';

import { useDebouncedAgentInstructions } from '../../hooks/useDebouncedAgentInstructions.js';

type AgentConfigInstructionsContextValue = {
  draft: string;
  onChange: (value: string) => void;
  flush: () => void;
};

const AgentConfigInstructionsContext = createContext<AgentConfigInstructionsContextValue | null>(null);

export function AgentConfigInstructionsProvider({ children }: { children: ReactNode }) {
  const { agentSpec } = useTrueForgeAgentSpec();
  const updateAgentSpec = useTrueForgeUpdateAgentSpec();
  const commit = useCallback((instructions: string) => updateAgentSpec?.({ instructions }), [updateAgentSpec]);
  const { draft, onChange, flush } = useDebouncedAgentInstructions({
    value: agentSpec?.instructions ?? '',
    onCommit: commit,
  });
  const value = useMemo(() => ({ draft, onChange, flush }), [draft, flush, onChange]);

  return <AgentConfigInstructionsContext.Provider value={value}>{children}</AgentConfigInstructionsContext.Provider>;
}

export function useAgentConfigInstructions(): AgentConfigInstructionsContextValue {
  const value = useContext(AgentConfigInstructionsContext);
  if (value === null) {
    throw new Error('useAgentConfigInstructions must be used within AgentConfigInstructionsProvider');
  }
  return value;
}

export function useOptionalAgentConfigInstructions(): AgentConfigInstructionsContextValue | null {
  return useContext(AgentConfigInstructionsContext);
}