Skip to content

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

1.0 KB
Raw Blame
import type { AgentChatServer, AgentSpec } from '../server/types.js';

export const DRAFT_SESSION_LAST_UPDATED_AT_HEADER = 'x-tfy-session-last-updated-at';

export interface DraftSessionBridge {
  syncAgentSpec: (draftSessionId: string, agentSpec: AgentSpec) => Promise<string>;
  getDraftAgentSpec: (draftSessionId: string) => Promise<AgentSpec | null>;
}

export function createDraftSessionBridge(server: AgentChatServer): DraftSessionBridge {
  return {
    async getDraftAgentSpec(draftSessionId) {
      const session = await server.getSession({ sessionId: draftSessionId });
      // Sessions without an agentSpec (e.g. immutable/SDK chats) have no draft configuration.
      // Return null so callers gracefully fallback instead of crashing with a red error toast.
      return session.agentSpec ?? null;
    },

    async syncAgentSpec(draftSessionId, agentSpec) {
      const updated = await server.updateSession({
        sessionId: draftSessionId,
        agentSpec,
      });
      return updated.updatedAt;
    },
  };
}