{"version":3,"sources":["../../src/client/agent.ts"],"sourcesContent":["/**\n * Copyright 2026 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n  createAgentAPI,\n  type AgentAPI,\n  type AgentTransport,\n  type SnapshotLookup,\n} from '@genkit-ai/ai/agent-core';\n\nimport type {\n  AgentInit,\n  AgentInput,\n  AgentOutput,\n  AgentStreamChunk,\n} from '@genkit-ai/ai';\nimport type { SessionSnapshot } from '@genkit-ai/ai/session';\nimport { runFlow, streamFlow } from './client.mjs';\n\n// Re-export the transport-agnostic agent-client surface so existing imports\n// from `genkit/beta/client` keep working.\nexport {\n  AgentError,\n  type AgentAPI,\n  type AgentChat,\n  type AgentChunk,\n  type AgentInterrupt,\n  type AgentResponse,\n  type AgentTurn,\n  type DetachedTask,\n} from '@genkit-ai/ai/agent-core';\n\n// Re-export the JSON Patch helper so apps can apply a chunk's `customPatch` to\n// their own locally tracked copy of the agent's custom state.\nexport { applyPatch, type JsonPatch } from '@genkit-ai/ai/json-patch';\n\n/**\n * Options for {@link remoteAgent}.\n */\nexport interface RemoteAgentOptions {\n  /** Required. The agent endpoint. */\n  url: string;\n  /** Optional. Defaults to `${url}/getSnapshot`. */\n  getSnapshotUrl?: string;\n  /** Optional. Defaults to `${url}/abort`. */\n  abortUrl?: string;\n  /** Optional. Static headers, or a function called per request. */\n  headers?:\n    | Record<string, string>\n    | (() => Record<string, string> | Promise<Record<string, string>>);\n  /** Optional. Declares server- vs client-managed state; inferred otherwise. */\n  stateManagement?: 'server' | 'client';\n}\n\n// ---------------------------------------------------------------------------\n// remoteAgent factory\n// ---------------------------------------------------------------------------\n\n/**\n * Creates a typed client for talking to a Genkit agent over HTTP.\n *\n * ```ts\n * import { remoteAgent } from 'genkit/beta/client';\n *\n * const agent = remoteAgent<WeatherState>({\n *   url: '/api/weatherAgent',\n * });\n * const chat = agent.chat();\n * const res = await chat.send('Weather in Tokyo?').response;\n * console.log(res.text);\n * ```\n */\nexport function remoteAgent<State = unknown>(\n  options: RemoteAgentOptions\n): AgentAPI<State> {\n  const { url } = options;\n  const getSnapshotUrl = options.getSnapshotUrl ?? `${url}/getSnapshot`;\n  const abortUrl = options.abortUrl ?? `${url}/abort`;\n\n  const resolveHeaders = async (): Promise<\n    Record<string, string> | undefined\n  > => {\n    if (!options.headers) return undefined;\n    if (typeof options.headers === 'function') {\n      return options.headers();\n    }\n    return options.headers;\n  };\n\n  const transport: AgentTransport = {\n    stateManagement: options.stateManagement,\n\n    runTurn(\n      input: AgentInput,\n      init: AgentInit,\n      opts: { abortSignal: AbortSignal }\n    ) {\n      // Kick off the request lazily so headers can be resolved asynchronously.\n      const started = (async () => {\n        const headers = await resolveHeaders();\n        return streamFlow<AgentOutput, AgentStreamChunk, AgentInit>({\n          url,\n          input,\n          init,\n          headers,\n          abortSignal: opts.abortSignal,\n        });\n      })();\n\n      const output = (async () => {\n        const { output } = await started;\n        return output;\n      })();\n\n      const stream = (async function* (): AsyncIterable<AgentStreamChunk> {\n        const { stream: rawStream } = await started;\n        yield* rawStream;\n      })();\n\n      return { stream, output };\n    },\n\n    async getSnapshot(lookup: SnapshotLookup) {\n      const headers = await resolveHeaders();\n      return runFlow<SessionSnapshot<State> | undefined>({\n        url: getSnapshotUrl,\n        input: lookup,\n        headers,\n      });\n    },\n\n    async abort(snapshotId: string) {\n      const headers = await resolveHeaders();\n      const result = await runFlow<{\n        snapshotId: string;\n        status?: SessionSnapshot['status'];\n      }>({\n        url: abortUrl,\n        input: { snapshotId },\n        headers,\n      });\n      return result?.status;\n    },\n  };\n\n  return createAgentAPI<State>(transport);\n}\n"],"mappings":"AAgBA;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