{"version":3,"file":"workflow.mjs","names":[],"sources":["../src/workflow.ts"],"sourcesContent":["import { WorkflowAgent } from '@ai-sdk/workflow'\nimport type {\n  WorkflowAgentOptions,\n  WorkflowAgentStreamOptions,\n  WorkflowAgentStreamResult,\n  TelemetryOptions,\n} from '@ai-sdk/workflow'\nimport type {\n  LanguageModel,\n  ToolSet,\n  StepResult,\n  FinishReason,\n  LanguageModelUsage,\n  LanguageModelResponseMetadata,\n  ModelMessage,\n  StopCondition,\n} from 'ai'\nimport { createGithubTools } from './index'\nimport { resolveInstructions } from './agents'\nimport type { AllGithubTools } from './core/tool-types'\nimport type { CombinedPresetToolNames, GithubToolPreset, PresetToolName } from './core/presets'\nimport type { GithubToolName } from './core/tool-names'\nimport type { ApprovalConfig } from './index'\nimport type { CommitIdentity } from './types'\nimport type { GithubTokenInput } from './core/token'\nimport type { GithubToolsContext } from './core/context'\nimport type { Context } from '@ai-sdk/provider-utils'\n\n/**\n * Result of {@link DurableGithubAgent.generate}.\n */\nexport interface DurableGithubAgentGenerateResult<TTools extends ToolSet = ToolSet> {\n  text: string\n  finishReason: FinishReason\n  usage: LanguageModelUsage\n  steps: StepResult<TTools>[]\n  response: LanguageModelResponseMetadata & { messages: ModelMessage[] }\n}\n\n/**\n * A wrapper around {@link WorkflowAgent} that adds a non-streaming\n * `.generate()` method alongside the existing `.stream()`.\n *\n * - `.stream()` — delegates to `WorkflowAgent.stream()`. Each tool call is\n *   an individually retriable workflow step. Supports `needsApproval` on tools.\n * - `.generate()` — uses `generateText` from the AI SDK for non-streaming\n *   execution. Must be called from within a `\"use step\"` function in\n *   workflow context (the Workflow runtime blocks raw I/O in workflow scope).\n */\nexport class DurableGithubAgent<TTools extends ToolSet = ToolSet> {\n  private agent: WorkflowAgent<TTools>\n  private _model: LanguageModel\n  private _instructions?: string\n  private _telemetry?: TelemetryOptions\n  private _tools: TTools\n\n  constructor(options: WorkflowAgentOptions<TTools>) {\n    this.agent = new WorkflowAgent(options)\n    this._model = options.model\n    this._instructions = typeof options.instructions === 'string' ? options.instructions : undefined\n    this._telemetry = options.telemetry\n    this._tools = options.tools ?? {} as TTools\n  }\n\n  /** The tool set configured for this agent. */\n  get tools(): TTools {\n    return this.agent.tools\n  }\n\n  /**\n   * Stream the agent's response. Delegates directly to `WorkflowAgent.stream()`.\n   * Works in workflow context — each tool call is a durable step.\n   */\n  stream<TStreamTools extends TTools = TTools, OUTPUT = never, PARTIAL_OUTPUT = never>(\n    options: WorkflowAgentStreamOptions<TStreamTools, Context, OUTPUT, PARTIAL_OUTPUT>,\n  ): Promise<WorkflowAgentStreamResult<TStreamTools, OUTPUT>> {\n    return this.agent.stream(options)\n  }\n\n  /**\n   * Generate a non-streaming response using `generateText` from the AI SDK.\n   *\n   * In workflow context this **must** be called from within a `\"use step\"`\n   * function, because the Workflow runtime blocks direct I/O (HTTP calls)\n   * at the workflow scope level.\n   */\n  async generate({ prompt, stopWhen }: { prompt: string, stopWhen?: StopCondition<TTools> | Array<StopCondition<TTools>> }): Promise<DurableGithubAgentGenerateResult<TTools>> {\n    const { generateText } = await import('ai')\n\n    const system = this._instructions\n\n    const result = await generateText({\n      model: this._model,\n      tools: this._tools,\n      system,\n      prompt,\n      stopWhen,\n      experimental_telemetry: this._telemetry as Parameters<typeof generateText>[0]['experimental_telemetry'],\n    })\n\n    return {\n      text: result.text,\n      finishReason: result.finishReason,\n      usage: result.usage,\n      steps: result.steps as StepResult<TTools>[],\n      response: result.response,\n    }\n  }\n}\n\n/**\n * Options for creating a durable GitHub agent via {@link createDurableGithubAgent}.\n *\n * Extends all `WorkflowAgentOptions` (temperature, telemetry, callbacks, etc.)\n * and adds GitHub-specific fields (token, preset, approval config).\n */\nexport type CreateDurableGithubAgentOptions =\n  Omit<WorkflowAgentOptions, 'model' | 'tools' | 'instructions'> & {\n    model: string | LanguageModel\n    /**\n     * GitHub personal access token or async token provider.\n     * Falls back to `process.env.GITHUB_TOKEN` when omitted.\n     */\n    token?: GithubTokenInput\n    /**\n     * Restrict tools and system prompt to a predefined preset.\n     *\n     * Selects a subset of tools and, when a single preset is passed,\n     * sets a matching system prompt. Combine presets with an array to merge tool sets.\n     *\n     * @see {@link GithubToolPreset} for available presets and included tools.\n     */\n    preset?: GithubToolPreset | GithubToolPreset[]\n    /**\n     * Control whether write operations require user approval before execution.\n     *\n     * @see {@link ApprovalConfig} for global and per-tool options.\n     */\n    requireApproval?: ApprovalConfig\n    /**\n     * Fully replace the default system prompt.\n     * When set, `preset` system prompts and `additionalInstructions` are ignored.\n     */\n    instructions?: string\n    /**\n     * Append text to the preset-specific (or default) system prompt.\n     * Ignored when `instructions` is set.\n     */\n    additionalInstructions?: string\n    /**\n     * Default owner / repo / PR / issue / ref values for tools and the system prompt.\n     */\n    context?: GithubToolsContext\n    /**\n     * Default author for commit-creating tools.\n     * Falls back to the authenticated user when omitted.\n     */\n    author?: CommitIdentity\n    /**\n     * Default committer for commit-creating tools.\n     * Falls back to the authenticated user when omitted.\n     */\n    committer?: CommitIdentity\n    /**\n     * Co-authors to attribute on all commits.\n     * Added as \"Co-authored-by\" trailers to commit messages.\n     */\n    coAuthors?: CommitIdentity[]\n  }\n\n/**\n * Create a pre-configured durable GitHub agent powered by `WorkflowAgent`\n * from `@ai-sdk/workflow`.\n *\n * Returns a {@link DurableGithubAgent} with two interaction modes:\n *\n * - `.stream()` — works directly in workflow scope; each tool call is a durable step.\n *   Write tools honor `requireApproval` via `needsApproval` and pause the workflow\n *   until the user approves or denies.\n * - `.generate()` — uses `generateText` from the AI SDK; must be called from\n *   within a `\"use step\"` function when running inside a workflow.\n *\n * @example Streaming (chat UI — works in workflow scope)\n * ```ts\n * import { createDurableGithubAgent } from '@github-tools/sdk/workflow'\n * import { getWritable } from 'workflow'\n * import type { ModelMessage, ModelCallStreamPart } from 'ai'\n *\n * async function chatWorkflow(messages: ModelMessage[], token: string) {\n *   \"use workflow\"\n *   const agent = createDurableGithubAgent({\n *     model: 'anthropic/claude-sonnet-4.6',\n *     token,\n *     preset: 'code-review',\n *   })\n *   const writable = getWritable<ModelCallStreamPart>()\n *   await agent.stream({ messages, writable })\n * }\n * ```\n */\nexport function createDurableGithubAgent(\n  options: CreateDurableGithubAgentOptions & { preset?: undefined },\n): DurableGithubAgent<AllGithubTools>\nexport function createDurableGithubAgent<P extends GithubToolPreset>(\n  options: CreateDurableGithubAgentOptions & { preset: P },\n): DurableGithubAgent<Pick<AllGithubTools, PresetToolName<P>>>\nexport function createDurableGithubAgent<P extends readonly GithubToolPreset[]>(\n  options: CreateDurableGithubAgentOptions & { preset: P },\n): DurableGithubAgent<Pick<AllGithubTools, CombinedPresetToolNames<P>>>\nexport function createDurableGithubAgent({\n  model,\n  token,\n  preset,\n  requireApproval,\n  context,\n  instructions,\n  additionalInstructions,\n  author,\n  committer,\n  coAuthors,\n  ...agentOptions\n}: CreateDurableGithubAgentOptions): DurableGithubAgent<AllGithubTools | Pick<AllGithubTools, GithubToolName>> {\n  const tools = createGithubTools({ token, requireApproval, preset, context, author, committer, coAuthors })\n\n  return new DurableGithubAgent({\n    ...agentOptions,\n    model,\n    tools,\n    instructions: resolveInstructions({ preset, instructions, additionalInstructions, context }),\n  }) as DurableGithubAgent<typeof tools>\n}\n\nexport { createGithubTools, createGithubAgent } from './index'\nexport type { CommitIdentity, CommitToolOptions, GithubTools, GithubToolsOptions, GithubToolPreset, GithubToolName, GithubWriteToolName, ApprovalConfig, ToolOverrides, AllGithubTools, PresetToolName, CombinedPresetToolNames, GithubToolsForPreset, PickGithubTools, GithubTokenInput, GithubToolsContext } from './index'\nexport { PRESET_TOOLS, GITHUB_TOOL_NAMES, GITHUB_WRITE_TOOLS, resolveGithubToken } from './index'\nexport type { CreateGithubAgentOptions } from './agents'\n"],"mappings":";;;;;;;;;;;;;;AAiDA,IAAa,qBAAb,MAAkE;CAChE;CACA;CACA;CACA;CACA;CAEA,YAAY,SAAuC;EACjD,KAAK,QAAQ,IAAI,cAAc,OAAO;EACtC,KAAK,SAAS,QAAQ;EACtB,KAAK,gBAAgB,OAAO,QAAQ,iBAAiB,WAAW,QAAQ,eAAe,KAAA;EACvF,KAAK,aAAa,QAAQ;EAC1B,KAAK,SAAS,QAAQ,SAAS,CAAC;CAClC;;CAGA,IAAI,QAAgB;EAClB,OAAO,KAAK,MAAM;CACpB;;;;;CAMA,OACE,SAC0D;EAC1D,OAAO,KAAK,MAAM,OAAO,OAAO;CAClC;;;;;;;;CASA,MAAM,SAAS,EAAE,QAAQ,YAAoJ;EAC3K,MAAM,EAAE,iBAAiB,MAAM,OAAO;EAEtC,MAAM,SAAS,KAAK;EAEpB,MAAM,SAAS,MAAM,aAAa;GAChC,OAAO,KAAK;GACZ,OAAO,KAAK;GACZ;GACA;GACA;GACA,wBAAwB,KAAK;EAC/B,CAAC;EAED,OAAO;GACL,MAAM,OAAO;GACb,cAAc,OAAO;GACrB,OAAO,OAAO;GACd,OAAO,OAAO;GACd,UAAU,OAAO;EACnB;CACF;AACF;AAqGA,SAAgB,yBAAyB,EACvC,OACA,OACA,QACA,iBACA,SACA,cACA,wBACA,QACA,WACA,WACA,GAAG,gBAC0G;CAC7G,MAAM,QAAQ,kBAAkB;EAAE;EAAO;EAAiB;EAAQ;EAAS;EAAQ;EAAW;CAAU,CAAC;CAEzG,OAAO,IAAI,mBAAmB;EAC5B,GAAG;EACH;EACA;EACA,cAAc,oBAAoB;GAAE;GAAQ;GAAc;GAAwB;EAAQ,CAAC;CAC7F,CAAC;AACH"}