/** * @license * Copyright 2025 Steven Roussey * SPDX-License-Identifier: Apache-2.0 */ import type { StreamEvent } from "@workglow/task-graph"; import type { ToolCallingTaskOutput } from "../task/ToolCallingTask"; import type { ToolCalls, ToolDefinition } from "../task/ToolCallingUtils"; /** * Shared helpers for providers that expose an OpenAI-compatible chat-completions * API (currently OpenAI itself and Hugging Face Inference). They cover: * * - Building the OpenAI `tools` array from workglow {@link ToolDefinition}s. * - Mapping workglow `toolChoice` strings to the OpenAI union, with optional * named-function support (HFI accepts only `auto | none | required`). * - Streaming a chat-completions response into workglow {@link StreamEvent}s, * accumulating fragmented tool-call argument JSON internally and yielding * `text-delta` + per-tool-call `object-delta` events. The final `finish` * event carries `{}` per the streaming convention in CLAUDE.md — the * consumer (`StreamProcessor`) is responsible for accumulating deltas. */ /** OpenAI-shape tool entry. Kept loose because each SDK names the type differently. */ export interface OpenAIShapedTool { readonly type: "function"; readonly function: { readonly name: string; readonly description: string; readonly parameters: any; }; } export type OpenAIShapedToolChoice = "auto" | "none" | "required" | { readonly type: "function"; readonly function: { readonly name: string; }; }; export declare function buildOpenAITools(tools: readonly ToolDefinition[]): OpenAIShapedTool[]; /** * Map workglow `toolChoice` to an OpenAI-shaped tool_choice value. * * @param toolChoice - One of `"auto" | "none" | "required" | ` (or undefined). * @param allowNamedFunction - When true, an unrecognized value is treated as a * tool name and emitted as `{ type: "function", function: { name } }`. When * false (HFI), it falls back to `"auto"` because HFI's API only accepts the * three keywords. */ export declare function mapOpenAIToolChoice(toolChoice: string | undefined, allowNamedFunction: boolean): OpenAIShapedToolChoice; /** * Parse a non-streaming OpenAI-shape chat completion message into workglow * {@link ToolCalls}. Used by the synchronous `*_ToolCalling` runFns. * * Some providers (HFI) don't always return an `id` per tool call; we * auto-generate `call_` in that case. */ export declare function parseOpenAIToolCallMessage(toolCallsRaw: readonly any[] | undefined): ToolCalls; /** * Adapt an OpenAI-shape streaming response (each chunk has * `choices[0].delta.{content?, tool_calls?[]}`) to workglow stream events. * Forwards events via the `emit` callback: * - `text-delta` for each non-empty `delta.content`. * - `object-delta` (single-element array) per tool-call delta with the latest * parsed input. The downstream `StreamProcessor` upserts by `id`. * * The caller is responsible for emitting its own `finish` event after this * returns — most callers will use structural defaults like * `{ text: "", toolCalls: [] }` so the final output satisfies * {@link ToolCallingTaskOutput} even when the model streams only * `tool_calls` (no `content` deltas). */ export declare function accumulateOpenAIStream(stream: AsyncIterable, emit: (event: StreamEvent) => void): Promise; //# sourceMappingURL=OpenAIShapedChat.d.ts.map