@statelyai/agent 2.0.0-alpha.22 → 2.0.0-alpha.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-sdk.cjs +53 -25
- package/dist/ai-sdk.d.cts +20 -12
- package/dist/ai-sdk.d.mts +20 -12
- package/dist/ai-sdk.mjs +53 -25
- package/dist/{decision-DxSTqzgZ.mjs → decision-BBZ2jMO1.mjs} +444 -79
- package/dist/{decision-1o45_ZrS.cjs → decision-Cv6SfNWl.cjs} +525 -94
- package/dist/index.cjs +2224 -1032
- package/dist/index.d.cts +413 -275
- package/dist/index.d.mts +413 -275
- package/dist/index.mjs +2163 -983
- package/dist/machines.cjs +30 -2
- package/dist/machines.d.cts +1 -1
- package/dist/machines.d.mts +1 -1
- package/dist/machines.mjs +30 -2
- package/dist/openai.cjs +447 -0
- package/dist/openai.d.cts +177 -0
- package/dist/openai.d.mts +177 -0
- package/dist/openai.mjs +437 -0
- package/dist/otel.cjs +3 -6
- package/dist/otel.d.cts +1 -1
- package/dist/otel.d.mts +1 -1
- package/dist/otel.mjs +3 -6
- package/dist/run-agent-Ck7Sa3Tb.d.mts +968 -0
- package/dist/run-agent-DWqA8KiI.d.cts +968 -0
- package/dist/setup-agent-CeO2fARv.mjs +639 -0
- package/dist/setup-agent-Dl2VELLi.cjs +663 -0
- package/dist/{text-logic-BaxPrcLk.d.cts → text-logic-C7bSnH_B.d.cts} +195 -44
- package/dist/{text-logic-DhfFWzu9.d.mts → text-logic-DJBK3LbG.d.mts} +195 -44
- package/dist/{types-DSdj2tGs.d.cts → types-CjHk5OdH.d.cts} +4 -19
- package/dist/{types-CvWRGFxP.d.mts → types-DWA6X9R-.d.mts} +4 -19
- package/package.json +17 -21
- package/readme.md +32 -17
- package/dist/errors-BQRk9eiZ.d.cts +0 -19
- package/dist/errors-C9rxnWbX.d.mts +0 -19
- package/dist/errors-CeSXQx0v.mjs +0 -23
- package/dist/errors-DUBBzRLP.cjs +0 -28
- package/dist/event-log-store-Bz7HDBkE.d.cts +0 -145
- package/dist/event-log-store-DmIDosD6.mjs +0 -427
- package/dist/event-log-store-a_TKy1gk.cjs +0 -468
- package/dist/event-log-store-hrA1vqtN.d.mts +0 -145
- package/dist/run-agent-0fpEZ1wJ.d.mts +0 -1098
- package/dist/run-agent-Zbkx7XP8.d.cts +0 -1098
- package/dist/setup-agent-DkCTy5Eu.cjs +0 -1707
- package/dist/setup-agent-nSFYrNTP.mjs +0 -1587
- package/dist/sqlite.cjs +0 -129
- package/dist/sqlite.d.cts +0 -57
- package/dist/sqlite.d.mts +0 -57
- package/dist/sqlite.mjs +0 -127
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { i as AgentToolChoice, n as AgentMessage, s as AgentTools, u as ChosenEvent } from "./types-CjHk5OdH.cjs";
|
|
2
|
+
import { F as AgentDecisionRequest, K as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-C7bSnH_B.cjs";
|
|
3
|
+
import OpenAI from "openai";
|
|
4
|
+
import { ChatCompletion, ChatCompletionCreateParamsNonStreaming, ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources/chat/completions/completions.js";
|
|
5
|
+
|
|
6
|
+
//#region src/openai/index.d.ts
|
|
7
|
+
/** Maps `AgentTextRequest.messages`/`system`/`prompt` to OpenAI chat messages. */
|
|
8
|
+
declare function toOpenAiMessages(request: Pick<AgentTextRequest, "system" | "prompt"> & {
|
|
9
|
+
messages?: AgentMessage[];
|
|
10
|
+
}): ChatCompletionMessageParam[];
|
|
11
|
+
/**
|
|
12
|
+
* Maps the generation settings a text and a decision request share onto OpenAI
|
|
13
|
+
* Chat Completions parameters, with unset keys omitted.
|
|
14
|
+
*
|
|
15
|
+
* Omitting matters: these are the top layer of a stack — the host's
|
|
16
|
+
* {@link CreateOpenAiExecutorsOptions.settings}, then the request's — and an
|
|
17
|
+
* `undefined` spread over the layer below would erase it.
|
|
18
|
+
*
|
|
19
|
+
* `max_tokens` is deprecated by OpenAI in favor of `max_completion_tokens`, so
|
|
20
|
+
* that's what `maxOutputTokens` targets. Chat Completions has no `top_k`
|
|
21
|
+
* parameter, so `topK` is dropped.
|
|
22
|
+
*/
|
|
23
|
+
declare function toOpenAiCallSettings(request: Pick<AgentTextRequest, "temperature" | "maxOutputTokens" | "topP" | "seed" | "stopSequences">): Partial<{
|
|
24
|
+
temperature: number | undefined;
|
|
25
|
+
max_completion_tokens: number | undefined;
|
|
26
|
+
top_p: number | undefined;
|
|
27
|
+
seed: number | undefined;
|
|
28
|
+
stop: string[] | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
/** One OpenAI function tool per `AgentTools` entry. */
|
|
31
|
+
declare function toOpenAiTools(tools: AgentTools): ChatCompletionTool[];
|
|
32
|
+
/**
|
|
33
|
+
* Maps an {@link AgentToolChoice} onto OpenAI's `tool_choice` —
|
|
34
|
+
* `{ type: 'tool', name }` becomes `{ type: 'function', function: { name } }`;
|
|
35
|
+
* `'auto'`/`'none'`/`'required'` are OpenAI's own vocabulary and pass through.
|
|
36
|
+
* `undefined` stays `undefined`, so the key is omitted.
|
|
37
|
+
*/
|
|
38
|
+
declare function toOpenAiToolChoice(toolChoice: AgentToolChoice | undefined): "auto" | "none" | "required" | {
|
|
39
|
+
type: "function";
|
|
40
|
+
function: {
|
|
41
|
+
name: string;
|
|
42
|
+
};
|
|
43
|
+
} | undefined;
|
|
44
|
+
/** One OpenAI function tool per candidate decision event — the "tool-per-event
|
|
45
|
+
* + tool_choice: 'required'" recipe, mirroring `toAiSdkEventTools`. */
|
|
46
|
+
declare function toOpenAiEventTools(events: AgentEventDescriptor[]): ChatCompletionTool[];
|
|
47
|
+
/**
|
|
48
|
+
* Messages for a decision request, with prior failed `attempts` rendered as an
|
|
49
|
+
* appended user message so retries converge. Mirrors the AI SDK adapter's
|
|
50
|
+
* `toDecisionMessages`.
|
|
51
|
+
*/
|
|
52
|
+
declare function toDecisionMessages(request: Pick<AgentDecisionRequest, "messages" | "prompt" | "events" | "attempts">): ChatCompletionMessageParam[];
|
|
53
|
+
type OpenAiUsage = NonNullable<ChatCompletion["usage"]>;
|
|
54
|
+
/**
|
|
55
|
+
* Folds OpenAI's usage payload onto the flat {@link AgentCallUsage} field names
|
|
56
|
+
* core aggregates. OpenAI nests the reasoning count under
|
|
57
|
+
* `completion_tokens_details` and the cache-read count under
|
|
58
|
+
* `prompt_tokens_details`; a field the response omitted stays omitted, so it
|
|
59
|
+
* never contributes a `0` to a run's partial sums.
|
|
60
|
+
*/
|
|
61
|
+
declare function toAgentCallUsage(usage: OpenAiUsage | undefined): AgentCallUsage | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Maps an OpenAI `finish_reason` onto the portable {@link AgentFinishReason}.
|
|
64
|
+
* `'function_call'` (the deprecated legacy path) joins `'tool_calls'` under
|
|
65
|
+
* `'tool-calls'`; anything unrecognized, including a missing reason, lands on
|
|
66
|
+
* `'other'`. OpenAI's own string stays on the result's `raw`.
|
|
67
|
+
*/
|
|
68
|
+
declare function toAgentFinishReason(finishReason: ChatCompletion.Choice["finish_reason"] | null | undefined): AgentFinishReason;
|
|
69
|
+
/**
|
|
70
|
+
* Per-call OpenAI Chat Completions parameters a host can apply on top of what
|
|
71
|
+
* the machine asked for — `reasoning_effort`, `service_tier`,
|
|
72
|
+
* `max_completion_tokens`, and anything else the API accepts. Typed straight
|
|
73
|
+
* off the installed `openai` package, so it tracks that version instead of
|
|
74
|
+
* restating its vocabulary.
|
|
75
|
+
*
|
|
76
|
+
* The request's own fields are excluded: `model`, `messages`, `tools`,
|
|
77
|
+
* `tool_choice`, `response_format`, `n`, and the streaming switches belong to
|
|
78
|
+
* the machine and the adapter, and a host override would silently contradict
|
|
79
|
+
* them.
|
|
80
|
+
*/
|
|
81
|
+
type OpenAiCallSettings = Omit<Partial<ChatCompletionCreateParamsNonStreaming>, "model" | "messages" | "tools" | "tool_choice" | "response_format" | "n" | "stream">;
|
|
82
|
+
/**
|
|
83
|
+
* Options for {@link createOpenAiExecutors}.
|
|
84
|
+
*
|
|
85
|
+
* `client` is the injected `OpenAI` instance — this adapter never constructs
|
|
86
|
+
* one, so the API key, base URL, and transport stay the host's business.
|
|
87
|
+
*
|
|
88
|
+
* `resolveModel` maps a machine's model *ref* (e.g. `'ticketTriage'`) to a real
|
|
89
|
+
* OpenAI model id. A machine's requests carry refs, not ids, so a raw-SDK host
|
|
90
|
+
* must resolve them (the AI SDK adapter does this via its `models` map).
|
|
91
|
+
* Defaults to identity, for machines that already use real ids.
|
|
92
|
+
*
|
|
93
|
+
* `settings` carries provider knobs that are the HOST's business rather than
|
|
94
|
+
* the machine's, such as reasoning effort. Pass a map keyed by model ref to
|
|
95
|
+
* give a ref a persona — `deep` thinks harder than `quick` — without the
|
|
96
|
+
* machine naming a provider knob, or a function to vary settings per request
|
|
97
|
+
* (`request.name` is the request's registered key; return `undefined` for the
|
|
98
|
+
* defaults). Settings are merged UNDER what the request declared, so a machine
|
|
99
|
+
* that set `maxOutputTokens` still wins.
|
|
100
|
+
*/
|
|
101
|
+
type CreateOpenAiExecutorsOptions = {
|
|
102
|
+
client: OpenAI;
|
|
103
|
+
resolveModel?: (modelRef: string) => string;
|
|
104
|
+
settings?: Record<string, OpenAiCallSettings> | ((request: AgentTextRequest | AgentDecisionRequest) => OpenAiCallSettings | undefined);
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Raw result shape from {@link OpenAiExecutors.generateText} — the `{ output }`
|
|
108
|
+
* envelope (the validated structured object for structured-output requests, or
|
|
109
|
+
* the model's text otherwise) plus the call metadata. Core only reads `output`; everything else flows
|
|
110
|
+
* verbatim to `runAgent`'s `onResult(request, { raw })`.
|
|
111
|
+
*/
|
|
112
|
+
type OpenAiGenerateResult = {
|
|
113
|
+
output: unknown;
|
|
114
|
+
/** The model's reasoning, present only when the request opted in via
|
|
115
|
+
* `includeReasoning` and the model produced it. Never enters machine
|
|
116
|
+
* context/output. */
|
|
117
|
+
reasoning?: string;
|
|
118
|
+
/** The call's token usage, on the flat field names `runAgent` folds into the
|
|
119
|
+
* run result's aggregated `AgentUsage`. Absent when OpenAI reported none. */
|
|
120
|
+
usage?: AgentCallUsage; /** Why the call stopped, normalized to the portable {@link AgentFinishReason}. OpenAI's own value stays on `raw`. */
|
|
121
|
+
finishReason: AgentFinishReason; /** The untouched OpenAI response. */
|
|
122
|
+
raw: unknown;
|
|
123
|
+
};
|
|
124
|
+
/** Raw result shape from {@link OpenAiExecutors.streamText} — the accumulated text once the stream finishes (chunks are delivered separately via `onChunk`), plus the stream's final usage/finish metadata. */
|
|
125
|
+
type OpenAiStreamResult = {
|
|
126
|
+
output: string; /** Present when the stream carried a usage chunk (the adapter asks for one via `stream_options`). */
|
|
127
|
+
usage?: AgentCallUsage;
|
|
128
|
+
finishReason: AgentFinishReason; /** The last chunk seen, which carries the stream's finish metadata. */
|
|
129
|
+
raw: unknown;
|
|
130
|
+
};
|
|
131
|
+
/** Raw result shape from {@link OpenAiExecutors.decide} — the chosen event plus the call metadata, delivered per decision attempt to `onResult`. */
|
|
132
|
+
type OpenAiDecideResult = {
|
|
133
|
+
event: ChosenEvent;
|
|
134
|
+
usage?: AgentCallUsage;
|
|
135
|
+
finishReason: AgentFinishReason;
|
|
136
|
+
raw: unknown;
|
|
137
|
+
};
|
|
138
|
+
/** `createOpenAiExecutors` always populates all three slots (unlike the general
|
|
139
|
+
* `AgentRequestExecutors`, where `streamText`/`decide` are optional), and its
|
|
140
|
+
* results are concretely typed. */
|
|
141
|
+
interface OpenAiExecutors {
|
|
142
|
+
generateText: (request: AgentTextRequest & {
|
|
143
|
+
tools: AgentTools;
|
|
144
|
+
}, info?: AgentRequestExecutorInfo) => Promise<OpenAiGenerateResult>;
|
|
145
|
+
streamText: (request: AgentTextRequest & {
|
|
146
|
+
tools: AgentTools;
|
|
147
|
+
}, info?: AgentRequestExecutorInfo) => Promise<OpenAiStreamResult>;
|
|
148
|
+
decide: (request: AgentDecisionRequest, info?: AgentRequestExecutorInfo) => Promise<OpenAiDecideResult>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Adds two calls' usage together, field by field. A tool loop makes several
|
|
152
|
+
* OpenAI calls per request, and core folds ONE `usage` per executor result into
|
|
153
|
+
* the run's totals, so the steps' usages have to be summed here or the run
|
|
154
|
+
* undercounts. A field neither call reported stays absent.
|
|
155
|
+
*/
|
|
156
|
+
declare function addAgentCallUsage(total: AgentCallUsage | undefined, next: AgentCallUsage | undefined): AgentCallUsage | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* Builds the `{ generateText, streamText, decide }` executor set for the raw
|
|
159
|
+
* `openai` package's Chat Completions API. Compare `createAiSdkExecutors` —
|
|
160
|
+
* same shape, different SDK underneath.
|
|
161
|
+
*
|
|
162
|
+
* Structured output uses `response_format: { type: 'json_schema' }` around the
|
|
163
|
+
* uniform `{ result, reasoning? }` envelope, and decisions force a tool call
|
|
164
|
+
* with `tool_choice: 'required'`, one function tool per candidate event.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const executors = createOpenAiExecutors({
|
|
169
|
+
* client: new OpenAI(),
|
|
170
|
+
* resolveModel: () => 'gpt-5.4-mini',
|
|
171
|
+
* });
|
|
172
|
+
* const result = await runAgent(machine, { input, executors });
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare function createOpenAiExecutors(options: CreateOpenAiExecutorsOptions): OpenAiExecutors;
|
|
176
|
+
//#endregion
|
|
177
|
+
export { CreateOpenAiExecutorsOptions, OpenAiCallSettings, OpenAiDecideResult, OpenAiExecutors, OpenAiGenerateResult, OpenAiStreamResult, addAgentCallUsage, createOpenAiExecutors, toAgentCallUsage, toAgentFinishReason, toDecisionMessages, toOpenAiCallSettings, toOpenAiEventTools, toOpenAiMessages, toOpenAiToolChoice, toOpenAiTools };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { i as AgentToolChoice, n as AgentMessage, s as AgentTools, u as ChosenEvent } from "./types-DWA6X9R-.mjs";
|
|
2
|
+
import { F as AgentDecisionRequest, K as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-DJBK3LbG.mjs";
|
|
3
|
+
import OpenAI from "openai";
|
|
4
|
+
import { ChatCompletion, ChatCompletionCreateParamsNonStreaming, ChatCompletionMessageParam, ChatCompletionTool } from "openai/resources/chat/completions/completions.js";
|
|
5
|
+
|
|
6
|
+
//#region src/openai/index.d.ts
|
|
7
|
+
/** Maps `AgentTextRequest.messages`/`system`/`prompt` to OpenAI chat messages. */
|
|
8
|
+
declare function toOpenAiMessages(request: Pick<AgentTextRequest, "system" | "prompt"> & {
|
|
9
|
+
messages?: AgentMessage[];
|
|
10
|
+
}): ChatCompletionMessageParam[];
|
|
11
|
+
/**
|
|
12
|
+
* Maps the generation settings a text and a decision request share onto OpenAI
|
|
13
|
+
* Chat Completions parameters, with unset keys omitted.
|
|
14
|
+
*
|
|
15
|
+
* Omitting matters: these are the top layer of a stack — the host's
|
|
16
|
+
* {@link CreateOpenAiExecutorsOptions.settings}, then the request's — and an
|
|
17
|
+
* `undefined` spread over the layer below would erase it.
|
|
18
|
+
*
|
|
19
|
+
* `max_tokens` is deprecated by OpenAI in favor of `max_completion_tokens`, so
|
|
20
|
+
* that's what `maxOutputTokens` targets. Chat Completions has no `top_k`
|
|
21
|
+
* parameter, so `topK` is dropped.
|
|
22
|
+
*/
|
|
23
|
+
declare function toOpenAiCallSettings(request: Pick<AgentTextRequest, "temperature" | "maxOutputTokens" | "topP" | "seed" | "stopSequences">): Partial<{
|
|
24
|
+
temperature: number | undefined;
|
|
25
|
+
max_completion_tokens: number | undefined;
|
|
26
|
+
top_p: number | undefined;
|
|
27
|
+
seed: number | undefined;
|
|
28
|
+
stop: string[] | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
/** One OpenAI function tool per `AgentTools` entry. */
|
|
31
|
+
declare function toOpenAiTools(tools: AgentTools): ChatCompletionTool[];
|
|
32
|
+
/**
|
|
33
|
+
* Maps an {@link AgentToolChoice} onto OpenAI's `tool_choice` —
|
|
34
|
+
* `{ type: 'tool', name }` becomes `{ type: 'function', function: { name } }`;
|
|
35
|
+
* `'auto'`/`'none'`/`'required'` are OpenAI's own vocabulary and pass through.
|
|
36
|
+
* `undefined` stays `undefined`, so the key is omitted.
|
|
37
|
+
*/
|
|
38
|
+
declare function toOpenAiToolChoice(toolChoice: AgentToolChoice | undefined): "auto" | "none" | "required" | {
|
|
39
|
+
type: "function";
|
|
40
|
+
function: {
|
|
41
|
+
name: string;
|
|
42
|
+
};
|
|
43
|
+
} | undefined;
|
|
44
|
+
/** One OpenAI function tool per candidate decision event — the "tool-per-event
|
|
45
|
+
* + tool_choice: 'required'" recipe, mirroring `toAiSdkEventTools`. */
|
|
46
|
+
declare function toOpenAiEventTools(events: AgentEventDescriptor[]): ChatCompletionTool[];
|
|
47
|
+
/**
|
|
48
|
+
* Messages for a decision request, with prior failed `attempts` rendered as an
|
|
49
|
+
* appended user message so retries converge. Mirrors the AI SDK adapter's
|
|
50
|
+
* `toDecisionMessages`.
|
|
51
|
+
*/
|
|
52
|
+
declare function toDecisionMessages(request: Pick<AgentDecisionRequest, "messages" | "prompt" | "events" | "attempts">): ChatCompletionMessageParam[];
|
|
53
|
+
type OpenAiUsage = NonNullable<ChatCompletion["usage"]>;
|
|
54
|
+
/**
|
|
55
|
+
* Folds OpenAI's usage payload onto the flat {@link AgentCallUsage} field names
|
|
56
|
+
* core aggregates. OpenAI nests the reasoning count under
|
|
57
|
+
* `completion_tokens_details` and the cache-read count under
|
|
58
|
+
* `prompt_tokens_details`; a field the response omitted stays omitted, so it
|
|
59
|
+
* never contributes a `0` to a run's partial sums.
|
|
60
|
+
*/
|
|
61
|
+
declare function toAgentCallUsage(usage: OpenAiUsage | undefined): AgentCallUsage | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Maps an OpenAI `finish_reason` onto the portable {@link AgentFinishReason}.
|
|
64
|
+
* `'function_call'` (the deprecated legacy path) joins `'tool_calls'` under
|
|
65
|
+
* `'tool-calls'`; anything unrecognized, including a missing reason, lands on
|
|
66
|
+
* `'other'`. OpenAI's own string stays on the result's `raw`.
|
|
67
|
+
*/
|
|
68
|
+
declare function toAgentFinishReason(finishReason: ChatCompletion.Choice["finish_reason"] | null | undefined): AgentFinishReason;
|
|
69
|
+
/**
|
|
70
|
+
* Per-call OpenAI Chat Completions parameters a host can apply on top of what
|
|
71
|
+
* the machine asked for — `reasoning_effort`, `service_tier`,
|
|
72
|
+
* `max_completion_tokens`, and anything else the API accepts. Typed straight
|
|
73
|
+
* off the installed `openai` package, so it tracks that version instead of
|
|
74
|
+
* restating its vocabulary.
|
|
75
|
+
*
|
|
76
|
+
* The request's own fields are excluded: `model`, `messages`, `tools`,
|
|
77
|
+
* `tool_choice`, `response_format`, `n`, and the streaming switches belong to
|
|
78
|
+
* the machine and the adapter, and a host override would silently contradict
|
|
79
|
+
* them.
|
|
80
|
+
*/
|
|
81
|
+
type OpenAiCallSettings = Omit<Partial<ChatCompletionCreateParamsNonStreaming>, "model" | "messages" | "tools" | "tool_choice" | "response_format" | "n" | "stream">;
|
|
82
|
+
/**
|
|
83
|
+
* Options for {@link createOpenAiExecutors}.
|
|
84
|
+
*
|
|
85
|
+
* `client` is the injected `OpenAI` instance — this adapter never constructs
|
|
86
|
+
* one, so the API key, base URL, and transport stay the host's business.
|
|
87
|
+
*
|
|
88
|
+
* `resolveModel` maps a machine's model *ref* (e.g. `'ticketTriage'`) to a real
|
|
89
|
+
* OpenAI model id. A machine's requests carry refs, not ids, so a raw-SDK host
|
|
90
|
+
* must resolve them (the AI SDK adapter does this via its `models` map).
|
|
91
|
+
* Defaults to identity, for machines that already use real ids.
|
|
92
|
+
*
|
|
93
|
+
* `settings` carries provider knobs that are the HOST's business rather than
|
|
94
|
+
* the machine's, such as reasoning effort. Pass a map keyed by model ref to
|
|
95
|
+
* give a ref a persona — `deep` thinks harder than `quick` — without the
|
|
96
|
+
* machine naming a provider knob, or a function to vary settings per request
|
|
97
|
+
* (`request.name` is the request's registered key; return `undefined` for the
|
|
98
|
+
* defaults). Settings are merged UNDER what the request declared, so a machine
|
|
99
|
+
* that set `maxOutputTokens` still wins.
|
|
100
|
+
*/
|
|
101
|
+
type CreateOpenAiExecutorsOptions = {
|
|
102
|
+
client: OpenAI;
|
|
103
|
+
resolveModel?: (modelRef: string) => string;
|
|
104
|
+
settings?: Record<string, OpenAiCallSettings> | ((request: AgentTextRequest | AgentDecisionRequest) => OpenAiCallSettings | undefined);
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Raw result shape from {@link OpenAiExecutors.generateText} — the `{ output }`
|
|
108
|
+
* envelope (the validated structured object for structured-output requests, or
|
|
109
|
+
* the model's text otherwise) plus the call metadata. Core only reads `output`; everything else flows
|
|
110
|
+
* verbatim to `runAgent`'s `onResult(request, { raw })`.
|
|
111
|
+
*/
|
|
112
|
+
type OpenAiGenerateResult = {
|
|
113
|
+
output: unknown;
|
|
114
|
+
/** The model's reasoning, present only when the request opted in via
|
|
115
|
+
* `includeReasoning` and the model produced it. Never enters machine
|
|
116
|
+
* context/output. */
|
|
117
|
+
reasoning?: string;
|
|
118
|
+
/** The call's token usage, on the flat field names `runAgent` folds into the
|
|
119
|
+
* run result's aggregated `AgentUsage`. Absent when OpenAI reported none. */
|
|
120
|
+
usage?: AgentCallUsage; /** Why the call stopped, normalized to the portable {@link AgentFinishReason}. OpenAI's own value stays on `raw`. */
|
|
121
|
+
finishReason: AgentFinishReason; /** The untouched OpenAI response. */
|
|
122
|
+
raw: unknown;
|
|
123
|
+
};
|
|
124
|
+
/** Raw result shape from {@link OpenAiExecutors.streamText} — the accumulated text once the stream finishes (chunks are delivered separately via `onChunk`), plus the stream's final usage/finish metadata. */
|
|
125
|
+
type OpenAiStreamResult = {
|
|
126
|
+
output: string; /** Present when the stream carried a usage chunk (the adapter asks for one via `stream_options`). */
|
|
127
|
+
usage?: AgentCallUsage;
|
|
128
|
+
finishReason: AgentFinishReason; /** The last chunk seen, which carries the stream's finish metadata. */
|
|
129
|
+
raw: unknown;
|
|
130
|
+
};
|
|
131
|
+
/** Raw result shape from {@link OpenAiExecutors.decide} — the chosen event plus the call metadata, delivered per decision attempt to `onResult`. */
|
|
132
|
+
type OpenAiDecideResult = {
|
|
133
|
+
event: ChosenEvent;
|
|
134
|
+
usage?: AgentCallUsage;
|
|
135
|
+
finishReason: AgentFinishReason;
|
|
136
|
+
raw: unknown;
|
|
137
|
+
};
|
|
138
|
+
/** `createOpenAiExecutors` always populates all three slots (unlike the general
|
|
139
|
+
* `AgentRequestExecutors`, where `streamText`/`decide` are optional), and its
|
|
140
|
+
* results are concretely typed. */
|
|
141
|
+
interface OpenAiExecutors {
|
|
142
|
+
generateText: (request: AgentTextRequest & {
|
|
143
|
+
tools: AgentTools;
|
|
144
|
+
}, info?: AgentRequestExecutorInfo) => Promise<OpenAiGenerateResult>;
|
|
145
|
+
streamText: (request: AgentTextRequest & {
|
|
146
|
+
tools: AgentTools;
|
|
147
|
+
}, info?: AgentRequestExecutorInfo) => Promise<OpenAiStreamResult>;
|
|
148
|
+
decide: (request: AgentDecisionRequest, info?: AgentRequestExecutorInfo) => Promise<OpenAiDecideResult>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Adds two calls' usage together, field by field. A tool loop makes several
|
|
152
|
+
* OpenAI calls per request, and core folds ONE `usage` per executor result into
|
|
153
|
+
* the run's totals, so the steps' usages have to be summed here or the run
|
|
154
|
+
* undercounts. A field neither call reported stays absent.
|
|
155
|
+
*/
|
|
156
|
+
declare function addAgentCallUsage(total: AgentCallUsage | undefined, next: AgentCallUsage | undefined): AgentCallUsage | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* Builds the `{ generateText, streamText, decide }` executor set for the raw
|
|
159
|
+
* `openai` package's Chat Completions API. Compare `createAiSdkExecutors` —
|
|
160
|
+
* same shape, different SDK underneath.
|
|
161
|
+
*
|
|
162
|
+
* Structured output uses `response_format: { type: 'json_schema' }` around the
|
|
163
|
+
* uniform `{ result, reasoning? }` envelope, and decisions force a tool call
|
|
164
|
+
* with `tool_choice: 'required'`, one function tool per candidate event.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const executors = createOpenAiExecutors({
|
|
169
|
+
* client: new OpenAI(),
|
|
170
|
+
* resolveModel: () => 'gpt-5.4-mini',
|
|
171
|
+
* });
|
|
172
|
+
* const result = await runAgent(machine, { input, executors });
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare function createOpenAiExecutors(options: CreateOpenAiExecutorsOptions): OpenAiExecutors;
|
|
176
|
+
//#endregion
|
|
177
|
+
export { CreateOpenAiExecutorsOptions, OpenAiCallSettings, OpenAiDecideResult, OpenAiExecutors, OpenAiGenerateResult, OpenAiStreamResult, addAgentCallUsage, createOpenAiExecutors, toAgentCallUsage, toAgentFinishReason, toDecisionMessages, toOpenAiCallSettings, toOpenAiEventTools, toOpenAiMessages, toOpenAiToolChoice, toOpenAiTools };
|