@codeworksh/aikit 0.6.0-dev.20260613095948 → 0.7.0
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/cli.d.mts +1 -1
- package/cli.mjs +7 -238
- package/cli.mjs.map +1 -1
- package/{codex-C36oU6yH.mjs → codex-DyQHzQmn.mjs} +24 -24
- package/codex-DyQHzQmn.mjs.map +1 -0
- package/index.d.mts +7948 -6669
- package/index.mjs +788 -614
- package/index.mjs.map +1 -1
- package/model-En2JLLXC.d.mts +328 -0
- package/modelgen-BcsqB44a.mjs +325 -0
- package/modelgen-BcsqB44a.mjs.map +1 -0
- package/modelgen.d.mts +9 -0
- package/modelgen.mjs +2 -0
- package/oauth/openai/codex.mjs +1 -1
- package/openai-codex-FcyE8vxh.mjs +2 -0
- package/openai-codex-_3XkJ4bs.mjs +1158 -0
- package/openai-codex-_3XkJ4bs.mjs.map +1 -0
- package/package.json +18 -13
- package/{registry-DUKrGxt4.mjs → registry-CMkIE_B4.mjs} +591 -503
- package/{registry-DUKrGxt4.mjs.map → registry-CMkIE_B4.mjs.map} +1 -1
- package/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/codex-C36oU6yH.mjs.map +0 -1
- package/openai-codex-CZrBnLAJ.mjs +0 -753
- package/openai-codex-CZrBnLAJ.mjs.map +0 -1
- package/openai-codex-bzfev3us.mjs +0 -2
package/index.mjs
CHANGED
|
@@ -1,580 +1,673 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { E as NamedError, _ as getProviders, c as APIMethodEnumSchema, d as KnownProviderEnumSchema, f as ProviderInfo, g as getModels, h as getModel, i as packageForModel, l as ActiveThinkingLevel, m as clampThinkingLevel, o as resolveLanguageModel, p as calculateCost, r as loadProviderFactory, u as KnownProviderEnum, v as model_exports, x as registry, y as modelsAreEqual } from "./registry-CMkIE_B4.mjs";
|
|
3
|
+
import { g as shortHash, n as createOpenAICodex, r as openaiCodex, v as openAICodexTools, x as resolveOpenAICodexToolConstraint } from "./openai-codex-_3XkJ4bs.mjs";
|
|
4
|
+
import Type$2, { Type, Type as Type$1 } from "typebox";
|
|
4
5
|
import { uuidv7 } from "uuidv7";
|
|
5
6
|
import Value from "typebox/value";
|
|
6
7
|
import { jsonSchema, streamText } from "ai";
|
|
7
8
|
import { parse } from "partial-json";
|
|
8
9
|
import Schema from "typebox/schema";
|
|
9
10
|
//#region src/message/message.ts
|
|
10
|
-
|
|
11
|
-
(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
11
|
+
var message_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
AssistantMessageSchema: () => AssistantMessageSchema,
|
|
13
|
+
ConstrainedSamplingSchema: () => ConstrainedSamplingSchema,
|
|
14
|
+
ContextSchema: () => ContextSchema,
|
|
15
|
+
GrammarVariantsSchema: () => GrammarVariantsSchema,
|
|
16
|
+
ImageContentSchema: () => ImageContentSchema,
|
|
17
|
+
MessageSchema: () => MessageSchema,
|
|
18
|
+
StopReasonSchema: () => StopReasonSchema,
|
|
19
|
+
TextContentSchema: () => TextContentSchema,
|
|
20
|
+
ThinkingContentSchema: () => ThinkingContentSchema,
|
|
21
|
+
ToolAbortedSchema: () => ToolAbortedSchema,
|
|
22
|
+
ToolCallAbortedPartSchema: () => ToolCallAbortedPartSchema,
|
|
23
|
+
ToolCallBaseSchema: () => ToolCallBaseSchema,
|
|
24
|
+
ToolCallCompletedPartSchema: () => ToolCallCompletedPartSchema,
|
|
25
|
+
ToolCallErrorPartSchema: () => ToolCallErrorPartSchema,
|
|
26
|
+
ToolCallInFlightSchema: () => ToolCallInFlightSchema,
|
|
27
|
+
ToolCallPendingPartSchema: () => ToolCallPendingPartSchema,
|
|
28
|
+
ToolCallRunningPartSchema: () => ToolCallRunningPartSchema,
|
|
29
|
+
ToolCallSchema: () => ToolCallSchema,
|
|
30
|
+
ToolCallSkippedPartSchema: () => ToolCallSkippedPartSchema,
|
|
31
|
+
ToolCallTerminalPartSchema: () => ToolCallTerminalPartSchema,
|
|
32
|
+
ToolCompletedSchema: () => ToolCompletedSchema,
|
|
33
|
+
ToolErrorResult: () => ToolErrorResult,
|
|
34
|
+
ToolErrorSchema: () => ToolErrorSchema,
|
|
35
|
+
ToolRunningPartial: () => ToolRunningPartial,
|
|
36
|
+
ToolRunningSchema: () => ToolRunningSchema,
|
|
37
|
+
ToolSchema: () => ToolSchema,
|
|
38
|
+
ToolSkippedSchema: () => ToolSkippedSchema,
|
|
39
|
+
ToolStatusEnum: () => ToolStatusEnum,
|
|
40
|
+
ToolSuccessResult: () => ToolSuccessResult,
|
|
41
|
+
UsageSchema: () => UsageSchema,
|
|
42
|
+
UserMessageSchema: () => UserMessageSchema,
|
|
43
|
+
createAssistantMessage: () => createAssistantMessage$1,
|
|
44
|
+
createMessageId: () => createMessageId,
|
|
45
|
+
createUserMessage: () => createUserMessage,
|
|
46
|
+
defineTool: () => defineTool,
|
|
47
|
+
transformMessages: () => transformMessages
|
|
48
|
+
});
|
|
49
|
+
const TextContentSchema = Type$2.Object({
|
|
50
|
+
type: Type$2.Literal("text"),
|
|
51
|
+
text: Type$2.String(),
|
|
52
|
+
textSignature: Type$2.Optional(Type$2.String())
|
|
53
|
+
});
|
|
54
|
+
const ImageContentSchema = Type$2.Object({
|
|
55
|
+
type: Type$2.Literal("image"),
|
|
56
|
+
data: Type$2.String(),
|
|
57
|
+
mimeType: Type$2.String()
|
|
58
|
+
});
|
|
59
|
+
const ThinkingContentSchema = Type$2.Object({
|
|
60
|
+
type: Type$2.Literal("thinking"),
|
|
61
|
+
thinking: Type$2.String(),
|
|
62
|
+
thinkingSignature: Type$2.Optional(Type$2.String()),
|
|
63
|
+
/** When true, the thinking content was redacted by safety filters. The opaque
|
|
64
|
+
* encrypted payload is stored in `thinkingSignature` so it can be passed back
|
|
65
|
+
* to the API for multi-turn continuity. */
|
|
66
|
+
redacted: Type$2.Optional(Type$2.Boolean())
|
|
67
|
+
});
|
|
68
|
+
const ToolCallBaseSchema = Type$2.Object({
|
|
69
|
+
type: Type$2.Literal("toolCall"),
|
|
70
|
+
callID: Type$2.String(),
|
|
71
|
+
name: Type$2.String(),
|
|
72
|
+
arguments: Type$2.Record(Type$2.String(), Type$2.Any()),
|
|
73
|
+
thoughtSignature: Type$2.Optional(Type$2.String()),
|
|
74
|
+
namespace: Type$2.Optional(Type$2.String()),
|
|
75
|
+
/** Names from Context.tools that became available after this tool result. */
|
|
76
|
+
addedToolNames: Type$2.Optional(Type$2.Array(Type$2.String())),
|
|
77
|
+
time: Type$2.Object({
|
|
78
|
+
start: Type$2.Number(),
|
|
79
|
+
end: Type$2.Number()
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
const ToolSuccessResult = Type$2.Object({
|
|
83
|
+
content: Type$2.Array(Type$2.Union([TextContentSchema, ImageContentSchema])),
|
|
84
|
+
details: Type$2.Optional(Type$2.Any()),
|
|
85
|
+
isError: Type$2.Literal(false)
|
|
86
|
+
});
|
|
87
|
+
const ToolErrorResult = Type$2.Object({
|
|
88
|
+
content: Type$2.Array(Type$2.Union([TextContentSchema, ImageContentSchema])),
|
|
89
|
+
details: Type$2.Optional(Type$2.Any()),
|
|
90
|
+
isError: Type$2.Literal(true)
|
|
91
|
+
});
|
|
92
|
+
const ToolRunningPartial = Type$2.Object({
|
|
93
|
+
content: Type$2.Optional(Type$2.Array(Type$2.Union([TextContentSchema, ImageContentSchema]))),
|
|
94
|
+
details: Type$2.Optional(Type$2.Any())
|
|
95
|
+
});
|
|
96
|
+
const ToolStatusEnum = {
|
|
97
|
+
pending: "pending",
|
|
98
|
+
running: "running",
|
|
99
|
+
completed: "completed",
|
|
100
|
+
error: "error",
|
|
101
|
+
skipped: "skipped",
|
|
102
|
+
aborted: "aborted"
|
|
103
|
+
};
|
|
104
|
+
const ToolRunningSchema = Type$2.Object({
|
|
105
|
+
status: Type$2.Literal(ToolStatusEnum.running),
|
|
106
|
+
partial: Type$2.Optional(ToolRunningPartial)
|
|
107
|
+
});
|
|
108
|
+
const ToolCompletedSchema = Type$2.Object({
|
|
109
|
+
status: Type$2.Literal(ToolStatusEnum.completed),
|
|
110
|
+
result: ToolSuccessResult
|
|
111
|
+
});
|
|
112
|
+
const ToolErrorSchema = Type$2.Object({
|
|
113
|
+
status: Type$2.Literal(ToolStatusEnum.error),
|
|
114
|
+
result: ToolErrorResult
|
|
115
|
+
});
|
|
116
|
+
const ToolSkippedSchema = Type$2.Object({
|
|
117
|
+
status: Type$2.Literal(ToolStatusEnum.skipped),
|
|
118
|
+
result: ToolErrorResult
|
|
119
|
+
});
|
|
120
|
+
const ToolAbortedSchema = Type$2.Object({
|
|
121
|
+
status: Type$2.Literal(ToolStatusEnum.aborted),
|
|
122
|
+
result: ToolErrorResult
|
|
123
|
+
});
|
|
124
|
+
const ToolCallPendingPartSchema = Type$2.Intersect([ToolCallBaseSchema, Type$2.Object({ status: Type$2.Literal(ToolStatusEnum.pending) })]);
|
|
125
|
+
const ToolCallRunningPartSchema = Type$2.Evaluate(Type$2.Intersect([ToolCallBaseSchema, ToolRunningSchema]));
|
|
126
|
+
const ToolCallCompletedPartSchema = Type$2.Evaluate(Type$2.Intersect([ToolCallBaseSchema, ToolCompletedSchema]));
|
|
127
|
+
const ToolCallErrorPartSchema = Type$2.Evaluate(Type$2.Intersect([ToolCallBaseSchema, ToolErrorSchema]));
|
|
128
|
+
const ToolCallSkippedPartSchema = Type$2.Evaluate(Type$2.Intersect([ToolCallBaseSchema, ToolSkippedSchema]));
|
|
129
|
+
const ToolCallAbortedPartSchema = Type$2.Evaluate(Type$2.Intersect([ToolCallBaseSchema, ToolAbortedSchema]));
|
|
130
|
+
const ToolCallTerminalPartSchema = Type$2.Union([
|
|
131
|
+
ToolCallCompletedPartSchema,
|
|
132
|
+
ToolCallErrorPartSchema,
|
|
133
|
+
ToolCallSkippedPartSchema,
|
|
134
|
+
ToolCallAbortedPartSchema
|
|
135
|
+
]);
|
|
136
|
+
const ToolCallInFlightSchema = Type$2.Object({
|
|
137
|
+
callID: Type$2.String(),
|
|
138
|
+
name: Type$2.String(),
|
|
139
|
+
rawArgs: Type$2.Record(Type$2.String(), Type$2.Unknown()),
|
|
140
|
+
args: Type$2.Optional(Type$2.Unknown())
|
|
141
|
+
});
|
|
142
|
+
const ToolCallSchema = Type$2.Union([
|
|
143
|
+
ToolCallPendingPartSchema,
|
|
144
|
+
ToolCallRunningPartSchema,
|
|
145
|
+
ToolCallTerminalPartSchema
|
|
146
|
+
]);
|
|
147
|
+
const UsageSchema = Type$2.Object({
|
|
148
|
+
input: Type$2.Number(),
|
|
149
|
+
output: Type$2.Number(),
|
|
150
|
+
cacheRead: Type$2.Number(),
|
|
151
|
+
cacheWrite: Type$2.Number(),
|
|
152
|
+
reasoning: Type$2.Optional(Type$2.Number()),
|
|
153
|
+
totalTokens: Type$2.Number(),
|
|
154
|
+
cost: Type$2.Object({
|
|
155
|
+
input: Type$2.Number(),
|
|
156
|
+
output: Type$2.Number(),
|
|
157
|
+
cacheRead: Type$2.Number(),
|
|
158
|
+
cacheWrite: Type$2.Number(),
|
|
159
|
+
total: Type$2.Number()
|
|
160
|
+
})
|
|
161
|
+
});
|
|
162
|
+
const StopReasonSchema = Type$2.Union([
|
|
163
|
+
Type$2.Literal("stop"),
|
|
164
|
+
Type$2.Literal("length"),
|
|
165
|
+
Type$2.Literal("toolUse"),
|
|
166
|
+
Type$2.Literal("error"),
|
|
167
|
+
Type$2.Literal("aborted")
|
|
168
|
+
]);
|
|
169
|
+
const UserMessageSchema = Type$2.Object({
|
|
170
|
+
messageId: Type$2.String(),
|
|
171
|
+
role: Type$2.Literal("user"),
|
|
172
|
+
time: Type$2.Object({ created: Type$2.Number() }),
|
|
173
|
+
parts: Type$2.Array(Type$2.Union([TextContentSchema, ImageContentSchema]))
|
|
174
|
+
});
|
|
175
|
+
const AssistantMessageSchema = Type$2.Object({
|
|
176
|
+
role: Type$2.Literal("assistant"),
|
|
177
|
+
protocol: KnownProviderEnumSchema,
|
|
178
|
+
provider: ProviderInfo,
|
|
179
|
+
model: Type$2.String(),
|
|
180
|
+
usage: UsageSchema,
|
|
181
|
+
stopReason: StopReasonSchema,
|
|
182
|
+
errorMessage: Type$2.Optional(Type$2.String()),
|
|
183
|
+
time: Type$2.Object({
|
|
184
|
+
created: Type$2.Number(),
|
|
185
|
+
completed: Type$2.Number()
|
|
186
|
+
}),
|
|
187
|
+
parts: Type$2.Array(Type$2.Union([
|
|
188
|
+
TextContentSchema,
|
|
189
|
+
ImageContentSchema,
|
|
190
|
+
ThinkingContentSchema,
|
|
191
|
+
ToolCallSchema
|
|
192
|
+
])),
|
|
193
|
+
responseId: Type$2.Optional(Type$2.String()),
|
|
194
|
+
responseModel: Type$2.Optional(Type$2.String()),
|
|
195
|
+
messageId: Type$2.String()
|
|
196
|
+
});
|
|
197
|
+
const MessageSchema = Type$2.Union([UserMessageSchema, AssistantMessageSchema]);
|
|
198
|
+
function createMessageId() {
|
|
199
|
+
return uuidv7();
|
|
200
|
+
}
|
|
201
|
+
function createUserMessage(message) {
|
|
202
|
+
const { messageId = createMessageId(), ...rest } = message;
|
|
203
|
+
return {
|
|
204
|
+
messageId,
|
|
205
|
+
...rest
|
|
60
206
|
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
parts
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
responseId: Type$1.Optional(Type$1.String()),
|
|
147
|
-
responseModel: Type$1.Optional(Type$1.String()),
|
|
148
|
-
messageId: Type$1.String()
|
|
149
|
-
});
|
|
150
|
-
const MessageSchema = _Message.MessageSchema = Type$1.Union([UserMessageSchema, AssistantMessageSchema]);
|
|
151
|
-
function createMessageId() {
|
|
152
|
-
return uuidv7();
|
|
153
|
-
}
|
|
154
|
-
_Message.createMessageId = createMessageId;
|
|
155
|
-
function createUserMessage(message) {
|
|
156
|
-
const { messageId = createMessageId(), ...rest } = message;
|
|
157
|
-
return {
|
|
158
|
-
messageId,
|
|
159
|
-
...rest
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
_Message.createUserMessage = createUserMessage;
|
|
163
|
-
function createAssistantMessage(message) {
|
|
164
|
-
const { messageId = createMessageId(), ...rest } = message;
|
|
165
|
-
return {
|
|
166
|
-
messageId,
|
|
167
|
-
...rest
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
_Message.createAssistantMessage = createAssistantMessage;
|
|
171
|
-
const ToolSchema = _Message.ToolSchema = Type$1.Object({
|
|
172
|
-
name: Type$1.String(),
|
|
173
|
-
description: Type$1.String(),
|
|
174
|
-
parameters: Type$1.Unsafe({})
|
|
175
|
-
});
|
|
176
|
-
function defineTool(tool) {
|
|
177
|
-
return tool;
|
|
178
|
-
}
|
|
179
|
-
_Message.defineTool = defineTool;
|
|
180
|
-
_Message.ContextSchema = Type$1.Object({
|
|
181
|
-
systemPrompt: Type$1.Optional(Type$1.String()),
|
|
182
|
-
messages: Type$1.Array(MessageSchema),
|
|
183
|
-
tools: Type$1.Optional(Type$1.Array(ToolSchema))
|
|
184
|
-
});
|
|
185
|
-
function isUnresolvedToolCall(toolCall) {
|
|
186
|
-
return toolCall.status === ToolStatusEnum.pending || toolCall.status === ToolStatusEnum.running;
|
|
187
|
-
}
|
|
188
|
-
function syntheticSkippedToolCall(toolCall) {
|
|
189
|
-
const { status: _status, partial: _partial, ...base } = toolCall;
|
|
190
|
-
return {
|
|
191
|
-
...base,
|
|
192
|
-
status: ToolStatusEnum.skipped,
|
|
193
|
-
result: {
|
|
194
|
-
content: [{
|
|
207
|
+
}
|
|
208
|
+
function createAssistantMessage$1(message) {
|
|
209
|
+
const { messageId = createMessageId(), ...rest } = message;
|
|
210
|
+
return {
|
|
211
|
+
messageId,
|
|
212
|
+
...rest
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const GrammarVariantsSchema = Type$2.Partial(Type$2.Object({
|
|
216
|
+
openai_lark: Type$2.String(),
|
|
217
|
+
openai_regex: Type$2.String()
|
|
218
|
+
}));
|
|
219
|
+
const ConstrainedSamplingSchema = Type$2.Union([Type$2.Object({
|
|
220
|
+
type: Type$2.Literal("json_schema"),
|
|
221
|
+
strict: Type$2.Union([Type$2.Literal("prefer"), Type$2.Literal("require")])
|
|
222
|
+
}), Type$2.Object({
|
|
223
|
+
type: Type$2.Literal("grammar"),
|
|
224
|
+
variants: GrammarVariantsSchema
|
|
225
|
+
})]);
|
|
226
|
+
/**
|
|
227
|
+
* Generic tool definition with typed parameter schema.
|
|
228
|
+
* Usage:
|
|
229
|
+
*
|
|
230
|
+
* const search = Message.defineTool({
|
|
231
|
+
* name: "search",
|
|
232
|
+
* description: "Search documents",
|
|
233
|
+
* parameters: Type.Object({
|
|
234
|
+
* query: Type.String(),
|
|
235
|
+
* limit: Type.Optional(Type.Number()),
|
|
236
|
+
* }),
|
|
237
|
+
* });
|
|
238
|
+
*
|
|
239
|
+
* type SearchParams = Static<typeof search.parameters>;
|
|
240
|
+
*/
|
|
241
|
+
const ToolSchema = Type$2.Object({
|
|
242
|
+
name: Type$2.String(),
|
|
243
|
+
description: Type$2.String(),
|
|
244
|
+
parameters: Type$2.Unsafe({}),
|
|
245
|
+
constrainedSampling: Type$2.Optional(Type$2.Union([Type$2.Literal(false), ConstrainedSamplingSchema]))
|
|
246
|
+
});
|
|
247
|
+
function defineTool(tool) {
|
|
248
|
+
return tool;
|
|
249
|
+
}
|
|
250
|
+
const ContextSchema = Type$2.Object({
|
|
251
|
+
systemPrompt: Type$2.Optional(Type$2.String()),
|
|
252
|
+
messages: Type$2.Array(MessageSchema),
|
|
253
|
+
tools: Type$2.Optional(Type$2.Array(ToolSchema))
|
|
254
|
+
});
|
|
255
|
+
function isUnresolvedToolCall(toolCall) {
|
|
256
|
+
return toolCall.status === ToolStatusEnum.pending || toolCall.status === ToolStatusEnum.running;
|
|
257
|
+
}
|
|
258
|
+
function syntheticSkippedToolCall(toolCall) {
|
|
259
|
+
const { status: _status, partial: _partial, ...base } = toolCall;
|
|
260
|
+
return {
|
|
261
|
+
...base,
|
|
262
|
+
status: ToolStatusEnum.skipped,
|
|
263
|
+
result: {
|
|
264
|
+
content: [{
|
|
265
|
+
type: "text",
|
|
266
|
+
text: "No result provided"
|
|
267
|
+
}],
|
|
268
|
+
isError: true
|
|
269
|
+
},
|
|
270
|
+
time: {
|
|
271
|
+
...toolCall.time,
|
|
272
|
+
end: Math.max(toolCall.time.end, Date.now())
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function transformMessages(messages, model, normalizeToolCallId) {
|
|
277
|
+
const toolCallIDMap = /* @__PURE__ */ new Map();
|
|
278
|
+
const transformed = [];
|
|
279
|
+
for (const msg of messages) {
|
|
280
|
+
if (msg.role === "user") {
|
|
281
|
+
transformed.push(msg);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const assistantMsg = msg;
|
|
285
|
+
const isSameModel = assistantMsg.provider.id === model.provider.id && assistantMsg.protocol === model.protocol && assistantMsg.model === model.id;
|
|
286
|
+
const parts = assistantMsg.parts.flatMap((block) => {
|
|
287
|
+
if (block.type === "thinking") {
|
|
288
|
+
if (isSameModel && block.thinkingSignature) return block;
|
|
289
|
+
if (!block.thinking || block.thinking.trim() === "") return [];
|
|
290
|
+
if (isSameModel) return block;
|
|
291
|
+
return {
|
|
195
292
|
type: "text",
|
|
196
|
-
text:
|
|
197
|
-
}
|
|
198
|
-
isError: true
|
|
199
|
-
},
|
|
200
|
-
time: {
|
|
201
|
-
...toolCall.time,
|
|
202
|
-
end: Math.max(toolCall.time.end, Date.now())
|
|
293
|
+
text: block.thinking
|
|
294
|
+
};
|
|
203
295
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (msg.role === "user") {
|
|
211
|
-
transformed.push(msg);
|
|
212
|
-
continue;
|
|
296
|
+
if (block.type === "text") {
|
|
297
|
+
if (isSameModel) return block;
|
|
298
|
+
return {
|
|
299
|
+
type: "text",
|
|
300
|
+
text: block.text
|
|
301
|
+
};
|
|
213
302
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
if (isSameModel) return block;
|
|
221
|
-
return {
|
|
222
|
-
type: "text",
|
|
223
|
-
text: block.thinking
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
if (block.type === "text") {
|
|
227
|
-
if (isSameModel) return block;
|
|
228
|
-
return {
|
|
229
|
-
type: "text",
|
|
230
|
-
text: block.text
|
|
231
|
-
};
|
|
303
|
+
if (block.type === "toolCall") {
|
|
304
|
+
const toolCall = block;
|
|
305
|
+
let normalizedToolCall = toolCall;
|
|
306
|
+
if (!isSameModel && toolCall.thoughtSignature) {
|
|
307
|
+
normalizedToolCall = { ...toolCall };
|
|
308
|
+
delete normalizedToolCall.thoughtSignature;
|
|
232
309
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
310
|
+
const normalizedID = toolCallIDMap.get(block.callID);
|
|
311
|
+
if (normalizedID && normalizedID !== block.callID) normalizedToolCall = {
|
|
312
|
+
...normalizedToolCall,
|
|
313
|
+
callID: normalizedID
|
|
314
|
+
};
|
|
315
|
+
else if (!isSameModel && normalizeToolCallId) {
|
|
316
|
+
const nextID = normalizeToolCallId(toolCall.callID, model, assistantMsg);
|
|
317
|
+
if (nextID !== toolCall.callID) {
|
|
318
|
+
toolCallIDMap.set(toolCall.callID, nextID);
|
|
319
|
+
normalizedToolCall = {
|
|
320
|
+
...normalizedToolCall,
|
|
321
|
+
callID: nextID
|
|
322
|
+
};
|
|
239
323
|
}
|
|
240
|
-
const normalizedID = toolCallIDMap.get(block.callID);
|
|
241
|
-
if (normalizedID && normalizedID !== block.callID) normalizedToolCall = {
|
|
242
|
-
...normalizedToolCall,
|
|
243
|
-
callID: normalizedID
|
|
244
|
-
};
|
|
245
|
-
else if (!isSameModel && normalizeToolCallId) {
|
|
246
|
-
const nextID = normalizeToolCallId(toolCall.callID, model, assistantMsg);
|
|
247
|
-
if (nextID !== toolCall.callID) {
|
|
248
|
-
toolCallIDMap.set(toolCall.callID, nextID);
|
|
249
|
-
normalizedToolCall = {
|
|
250
|
-
...normalizedToolCall,
|
|
251
|
-
callID: nextID
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
return normalizedToolCall;
|
|
256
324
|
}
|
|
257
|
-
return
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
325
|
+
return normalizedToolCall;
|
|
326
|
+
}
|
|
327
|
+
return block;
|
|
328
|
+
}).map((block) => {
|
|
329
|
+
if (block.type === "toolCall" && isUnresolvedToolCall(block)) return syntheticSkippedToolCall(block);
|
|
330
|
+
return block;
|
|
331
|
+
});
|
|
332
|
+
transformed.push({
|
|
333
|
+
...assistantMsg,
|
|
334
|
+
parts
|
|
335
|
+
});
|
|
268
336
|
}
|
|
269
|
-
|
|
270
|
-
}
|
|
337
|
+
return transformed;
|
|
338
|
+
}
|
|
271
339
|
//#endregion
|
|
272
340
|
//#region src/event/event.ts
|
|
273
|
-
|
|
274
|
-
(
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
turnStart: "turn.start",
|
|
371
|
-
turnEnd: "turn.end",
|
|
372
|
-
messageStart: "message.start",
|
|
373
|
-
messagePartStart: "message.part.start",
|
|
374
|
-
messagePartUpdate: "message.part.update",
|
|
375
|
-
messagePartEnd: "message.part.end",
|
|
376
|
-
messageUpdate: "message.update",
|
|
377
|
-
messageEnd: "message.end",
|
|
378
|
-
toolExecutionStart: "tool.execution.start",
|
|
379
|
-
toolExecutionUpdate: "tool.execution.update",
|
|
380
|
-
toolExecutionEnd: "tool.execution.end"
|
|
381
|
-
};
|
|
382
|
-
const UserMessagePart = Type$1.Union([Message.TextContentSchema, Message.ImageContentSchema]);
|
|
383
|
-
const AssistantMessagePart = Type$1.Union([
|
|
384
|
-
Message.TextContentSchema,
|
|
385
|
-
Message.ImageContentSchema,
|
|
386
|
-
Message.ThinkingContentSchema,
|
|
387
|
-
Message.ToolCallSchema
|
|
388
|
-
]);
|
|
389
|
-
const UserMessagePartStart = Type$1.Object({
|
|
390
|
-
type: Type$1.Literal(AgentEventType.messagePartStart),
|
|
391
|
-
message: Message.UserMessageSchema,
|
|
392
|
-
partIndex: Type$1.Number(),
|
|
393
|
-
part: UserMessagePart
|
|
394
|
-
});
|
|
395
|
-
const UserMessagePartEnd = Type$1.Object({
|
|
396
|
-
type: Type$1.Literal(AgentEventType.messagePartEnd),
|
|
397
|
-
message: Message.UserMessageSchema,
|
|
398
|
-
partIndex: Type$1.Number(),
|
|
399
|
-
part: UserMessagePart
|
|
400
|
-
});
|
|
401
|
-
const AssistantMessagePartStart = Type$1.Object({
|
|
402
|
-
type: Type$1.Literal(AgentEventType.messagePartStart),
|
|
403
|
-
message: Message.AssistantMessageSchema,
|
|
404
|
-
partIndex: Type$1.Number(),
|
|
405
|
-
part: AssistantMessagePart
|
|
406
|
-
});
|
|
407
|
-
const AssistantMessagePartLLMUpdate = Type$1.Object({
|
|
408
|
-
type: Type$1.Literal(AgentEventType.messagePartUpdate),
|
|
409
|
-
message: Message.AssistantMessageSchema,
|
|
410
|
-
partIndex: Type$1.Number(),
|
|
411
|
-
part: AssistantMessagePart,
|
|
412
|
-
source: Type$1.Literal("llm")
|
|
413
|
-
});
|
|
414
|
-
const AssistantMessagePartToolUpdate = Type$1.Object({
|
|
415
|
-
type: Type$1.Literal(AgentEventType.messagePartUpdate),
|
|
416
|
-
message: Message.AssistantMessageSchema,
|
|
417
|
-
partIndex: Type$1.Number(),
|
|
418
|
-
part: AssistantMessagePart,
|
|
419
|
-
source: Type$1.Literal("tool")
|
|
420
|
-
});
|
|
421
|
-
const AssistantMessagePartEnd = Type$1.Object({
|
|
422
|
-
type: Type$1.Literal(AgentEventType.messagePartEnd),
|
|
423
|
-
message: Message.AssistantMessageSchema,
|
|
424
|
-
partIndex: Type$1.Number(),
|
|
425
|
-
part: AssistantMessagePart
|
|
426
|
-
});
|
|
427
|
-
const ToolExecutionStartSchema = Type$1.Evaluate(Type$1.Intersect([Message.ToolCallInFlightSchema, Type$1.Object({ type: Type$1.Literal(AgentEventType.toolExecutionStart) })]));
|
|
428
|
-
const ToolExecutionUpdateSchema = Type$1.Evaluate(Type$1.Intersect([
|
|
429
|
-
Message.ToolCallInFlightSchema,
|
|
430
|
-
Type$1.Object({ type: Type$1.Literal(AgentEventType.toolExecutionUpdate) }),
|
|
431
|
-
Message.ToolRunningSchema
|
|
432
|
-
]));
|
|
433
|
-
const ToolExecutionEndSchema = Type$1.Evaluate(Type$1.Intersect([
|
|
434
|
-
Message.ToolCallInFlightSchema,
|
|
435
|
-
Type$1.Object({ type: Type$1.Literal(AgentEventType.toolExecutionEnd) }),
|
|
436
|
-
Type$1.Union([Message.ToolCompletedSchema, Message.ToolErrorSchema])
|
|
437
|
-
]));
|
|
438
|
-
_Event.AgentEventSchema = Type$1.Union([
|
|
439
|
-
Type$1.Object({ type: Type$1.Literal(AgentEventType.agentStart) }),
|
|
440
|
-
Type$1.Object({
|
|
441
|
-
type: Type$1.Literal(AgentEventType.agentEnd),
|
|
442
|
-
messages: Type$1.Array(Message.MessageSchema)
|
|
443
|
-
}),
|
|
444
|
-
Type$1.Object({ type: Type$1.Literal(AgentEventType.turnStart) }),
|
|
445
|
-
Type$1.Object({
|
|
446
|
-
type: Type$1.Literal(AgentEventType.turnEnd),
|
|
447
|
-
message: Message.AssistantMessageSchema
|
|
448
|
-
}),
|
|
449
|
-
Type$1.Object({
|
|
450
|
-
type: Type$1.Literal(AgentEventType.messageStart),
|
|
451
|
-
message: Message.MessageSchema
|
|
452
|
-
}),
|
|
453
|
-
Type$1.Union([UserMessagePartStart, UserMessagePartEnd]),
|
|
454
|
-
Type$1.Union([
|
|
455
|
-
AssistantMessagePartStart,
|
|
456
|
-
AssistantMessagePartLLMUpdate,
|
|
457
|
-
AssistantMessagePartToolUpdate,
|
|
458
|
-
AssistantMessagePartEnd
|
|
341
|
+
var event_exports = /* @__PURE__ */ __exportAll({
|
|
342
|
+
AgentEventSchema: () => AgentEventSchema,
|
|
343
|
+
AgentEventType: () => AgentEventType,
|
|
344
|
+
LLMMessageEventSchema: () => LLMMessageEventSchema,
|
|
345
|
+
LLMMessageEventType: () => LLMMessageEventType
|
|
346
|
+
});
|
|
347
|
+
const LLMMessageEventType = {
|
|
348
|
+
start: "start",
|
|
349
|
+
textStart: "text.start",
|
|
350
|
+
textDelta: "text.delta",
|
|
351
|
+
textEnd: "text.end",
|
|
352
|
+
thinkingStart: "thinking.start",
|
|
353
|
+
thinkingDelta: "thinking.delta",
|
|
354
|
+
thinkingEnd: "thinking.end",
|
|
355
|
+
toolcallStart: "toolcall.start",
|
|
356
|
+
toolcallDelta: "toolcall.delta",
|
|
357
|
+
toolcallEnd: "toolcall.end",
|
|
358
|
+
toolcallFinal: "toolcall.final",
|
|
359
|
+
done: "done",
|
|
360
|
+
error: "error"
|
|
361
|
+
};
|
|
362
|
+
/**
|
|
363
|
+
* Event protocol for AssistantMessageEventStream.
|
|
364
|
+
*
|
|
365
|
+
* Streams should emit `start` before partial updates, then terminate with either:
|
|
366
|
+
* - `done` carrying the final successful AssistantMessage, or
|
|
367
|
+
* - `error` carrying the final AssistantMessage with stopReason "error" or "aborted"
|
|
368
|
+
* and errorMessage.
|
|
369
|
+
*/
|
|
370
|
+
const LLMMessageEventSchema = Type$2.Union([
|
|
371
|
+
Type$2.Object({
|
|
372
|
+
type: Type$2.Literal(LLMMessageEventType.start),
|
|
373
|
+
partial: AssistantMessageSchema
|
|
374
|
+
}),
|
|
375
|
+
Type$2.Object({
|
|
376
|
+
type: Type$2.Literal(LLMMessageEventType.textStart),
|
|
377
|
+
partIndex: Type$2.Number(),
|
|
378
|
+
partial: AssistantMessageSchema
|
|
379
|
+
}),
|
|
380
|
+
Type$2.Object({
|
|
381
|
+
type: Type$2.Literal(LLMMessageEventType.textDelta),
|
|
382
|
+
partIndex: Type$2.Number(),
|
|
383
|
+
delta: Type$2.String(),
|
|
384
|
+
partial: AssistantMessageSchema
|
|
385
|
+
}),
|
|
386
|
+
Type$2.Object({
|
|
387
|
+
type: Type$2.Literal(LLMMessageEventType.textEnd),
|
|
388
|
+
partIndex: Type$2.Number(),
|
|
389
|
+
content: Type$2.String(),
|
|
390
|
+
partial: AssistantMessageSchema
|
|
391
|
+
}),
|
|
392
|
+
Type$2.Object({
|
|
393
|
+
type: Type$2.Literal(LLMMessageEventType.thinkingStart),
|
|
394
|
+
partIndex: Type$2.Number(),
|
|
395
|
+
partial: AssistantMessageSchema
|
|
396
|
+
}),
|
|
397
|
+
Type$2.Object({
|
|
398
|
+
type: Type$2.Literal(LLMMessageEventType.thinkingDelta),
|
|
399
|
+
partIndex: Type$2.Number(),
|
|
400
|
+
delta: Type$2.String(),
|
|
401
|
+
partial: AssistantMessageSchema
|
|
402
|
+
}),
|
|
403
|
+
Type$2.Object({
|
|
404
|
+
type: Type$2.Literal(LLMMessageEventType.thinkingEnd),
|
|
405
|
+
partIndex: Type$2.Number(),
|
|
406
|
+
content: Type$2.String(),
|
|
407
|
+
partial: AssistantMessageSchema
|
|
408
|
+
}),
|
|
409
|
+
Type$2.Object({
|
|
410
|
+
type: Type$2.Literal(LLMMessageEventType.toolcallStart),
|
|
411
|
+
partIndex: Type$2.Number(),
|
|
412
|
+
partial: AssistantMessageSchema
|
|
413
|
+
}),
|
|
414
|
+
Type$2.Object({
|
|
415
|
+
type: Type$2.Literal(LLMMessageEventType.toolcallDelta),
|
|
416
|
+
partIndex: Type$2.Number(),
|
|
417
|
+
delta: Type$2.String(),
|
|
418
|
+
partial: AssistantMessageSchema
|
|
419
|
+
}),
|
|
420
|
+
Type$2.Object({
|
|
421
|
+
type: Type$2.Literal(LLMMessageEventType.toolcallEnd),
|
|
422
|
+
partIndex: Type$2.Number(),
|
|
423
|
+
toolCall: ToolCallSchema,
|
|
424
|
+
partial: AssistantMessageSchema
|
|
425
|
+
}),
|
|
426
|
+
Type$2.Object({
|
|
427
|
+
type: Type$2.Literal(LLMMessageEventType.toolcallFinal),
|
|
428
|
+
partIndex: Type$2.Number(),
|
|
429
|
+
toolCall: ToolCallSchema,
|
|
430
|
+
partial: AssistantMessageSchema
|
|
431
|
+
}),
|
|
432
|
+
Type$2.Object({
|
|
433
|
+
type: Type$2.Literal(LLMMessageEventType.done),
|
|
434
|
+
reason: Type$2.Union([
|
|
435
|
+
Type$2.Literal("stop"),
|
|
436
|
+
Type$2.Literal("length"),
|
|
437
|
+
Type$2.Literal("toolUse")
|
|
459
438
|
]),
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
Type$
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
439
|
+
message: AssistantMessageSchema
|
|
440
|
+
}),
|
|
441
|
+
Type$2.Object({
|
|
442
|
+
type: Type$2.Literal(LLMMessageEventType.error),
|
|
443
|
+
reason: Type$2.Union([Type$2.Literal("aborted"), Type$2.Literal("error")]),
|
|
444
|
+
error: AssistantMessageSchema
|
|
445
|
+
})
|
|
446
|
+
]);
|
|
447
|
+
const AgentEventType = {
|
|
448
|
+
agentStart: "agent.start",
|
|
449
|
+
agentEnd: "agent.end",
|
|
450
|
+
turnStart: "turn.start",
|
|
451
|
+
turnEnd: "turn.end",
|
|
452
|
+
messageStart: "message.start",
|
|
453
|
+
messagePartStart: "message.part.start",
|
|
454
|
+
messagePartUpdate: "message.part.update",
|
|
455
|
+
messagePartEnd: "message.part.end",
|
|
456
|
+
messageUpdate: "message.update",
|
|
457
|
+
messageEnd: "message.end",
|
|
458
|
+
toolExecutionStart: "tool.execution.start",
|
|
459
|
+
toolExecutionUpdate: "tool.execution.update",
|
|
460
|
+
toolExecutionEnd: "tool.execution.end"
|
|
461
|
+
};
|
|
462
|
+
const UserMessagePart = Type$2.Union([TextContentSchema, ImageContentSchema]);
|
|
463
|
+
const AssistantMessagePart = Type$2.Union([
|
|
464
|
+
TextContentSchema,
|
|
465
|
+
ImageContentSchema,
|
|
466
|
+
ThinkingContentSchema,
|
|
467
|
+
ToolCallSchema
|
|
468
|
+
]);
|
|
469
|
+
const UserMessagePartStart = Type$2.Object({
|
|
470
|
+
type: Type$2.Literal(AgentEventType.messagePartStart),
|
|
471
|
+
message: UserMessageSchema,
|
|
472
|
+
partIndex: Type$2.Number(),
|
|
473
|
+
part: UserMessagePart
|
|
474
|
+
});
|
|
475
|
+
const UserMessagePartEnd = Type$2.Object({
|
|
476
|
+
type: Type$2.Literal(AgentEventType.messagePartEnd),
|
|
477
|
+
message: UserMessageSchema,
|
|
478
|
+
partIndex: Type$2.Number(),
|
|
479
|
+
part: UserMessagePart
|
|
480
|
+
});
|
|
481
|
+
const AssistantMessagePartStart = Type$2.Object({
|
|
482
|
+
type: Type$2.Literal(AgentEventType.messagePartStart),
|
|
483
|
+
message: AssistantMessageSchema,
|
|
484
|
+
partIndex: Type$2.Number(),
|
|
485
|
+
part: AssistantMessagePart
|
|
486
|
+
});
|
|
487
|
+
const AssistantMessagePartLLMUpdate = Type$2.Object({
|
|
488
|
+
type: Type$2.Literal(AgentEventType.messagePartUpdate),
|
|
489
|
+
message: AssistantMessageSchema,
|
|
490
|
+
partIndex: Type$2.Number(),
|
|
491
|
+
part: AssistantMessagePart,
|
|
492
|
+
source: Type$2.Literal("llm")
|
|
493
|
+
});
|
|
494
|
+
const AssistantMessagePartToolUpdate = Type$2.Object({
|
|
495
|
+
type: Type$2.Literal(AgentEventType.messagePartUpdate),
|
|
496
|
+
message: AssistantMessageSchema,
|
|
497
|
+
partIndex: Type$2.Number(),
|
|
498
|
+
part: AssistantMessagePart,
|
|
499
|
+
source: Type$2.Literal("tool")
|
|
500
|
+
});
|
|
501
|
+
const AssistantMessagePartEnd = Type$2.Object({
|
|
502
|
+
type: Type$2.Literal(AgentEventType.messagePartEnd),
|
|
503
|
+
message: AssistantMessageSchema,
|
|
504
|
+
partIndex: Type$2.Number(),
|
|
505
|
+
part: AssistantMessagePart
|
|
506
|
+
});
|
|
507
|
+
const ToolExecutionStartSchema = Type$2.Object({
|
|
508
|
+
type: Type$2.Literal(AgentEventType.toolExecutionStart),
|
|
509
|
+
messageId: Type$2.String(),
|
|
510
|
+
partIndex: Type$2.Number(),
|
|
511
|
+
toolCall: ToolCallPendingPartSchema
|
|
512
|
+
});
|
|
513
|
+
const ToolExecutionUpdateSchema = Type$2.Object({
|
|
514
|
+
type: Type$2.Literal(AgentEventType.toolExecutionUpdate),
|
|
515
|
+
messageId: Type$2.String(),
|
|
516
|
+
partIndex: Type$2.Number(),
|
|
517
|
+
toolCall: ToolCallRunningPartSchema
|
|
518
|
+
});
|
|
519
|
+
const ToolExecutionEndSchema = Type$2.Object({
|
|
520
|
+
type: Type$2.Literal(AgentEventType.toolExecutionEnd),
|
|
521
|
+
messageId: Type$2.String(),
|
|
522
|
+
partIndex: Type$2.Number(),
|
|
523
|
+
toolCall: ToolCallTerminalPartSchema
|
|
524
|
+
});
|
|
525
|
+
/**
|
|
526
|
+
* Events emitted by the Agent lifecycle
|
|
527
|
+
* These events provide fine-grained lifecycle information for messages, turns, and tool execution.
|
|
528
|
+
*/
|
|
529
|
+
const AgentEventSchema = Type$2.Union([
|
|
530
|
+
Type$2.Object({ type: Type$2.Literal(AgentEventType.agentStart) }),
|
|
531
|
+
Type$2.Object({
|
|
532
|
+
type: Type$2.Literal(AgentEventType.agentEnd),
|
|
533
|
+
messages: Type$2.Array(MessageSchema)
|
|
534
|
+
}),
|
|
535
|
+
Type$2.Object({ type: Type$2.Literal(AgentEventType.turnStart) }),
|
|
536
|
+
Type$2.Object({
|
|
537
|
+
type: Type$2.Literal(AgentEventType.turnEnd),
|
|
538
|
+
message: AssistantMessageSchema
|
|
539
|
+
}),
|
|
540
|
+
Type$2.Object({
|
|
541
|
+
type: Type$2.Literal(AgentEventType.messageStart),
|
|
542
|
+
message: MessageSchema
|
|
543
|
+
}),
|
|
544
|
+
Type$2.Union([UserMessagePartStart, UserMessagePartEnd]),
|
|
545
|
+
Type$2.Union([
|
|
546
|
+
AssistantMessagePartStart,
|
|
547
|
+
AssistantMessagePartLLMUpdate,
|
|
548
|
+
AssistantMessagePartToolUpdate,
|
|
549
|
+
AssistantMessagePartEnd
|
|
550
|
+
]),
|
|
551
|
+
Type$2.Object({
|
|
552
|
+
type: Type$2.Literal(AgentEventType.messageUpdate),
|
|
553
|
+
message: MessageSchema
|
|
554
|
+
}),
|
|
555
|
+
Type$2.Object({
|
|
556
|
+
type: Type$2.Literal(AgentEventType.messageEnd),
|
|
557
|
+
message: MessageSchema
|
|
558
|
+
}),
|
|
559
|
+
ToolExecutionStartSchema,
|
|
560
|
+
ToolExecutionUpdateSchema,
|
|
561
|
+
ToolExecutionEndSchema
|
|
562
|
+
]);
|
|
473
563
|
//#endregion
|
|
474
564
|
//#region src/llm.ts
|
|
475
|
-
const llmImpl = async (provider, model, overrides) =>
|
|
565
|
+
const llmImpl = async (provider, model, overrides) => getModel(provider, model, overrides);
|
|
476
566
|
const llm = Object.assign(llmImpl, {
|
|
477
|
-
model:
|
|
478
|
-
models:
|
|
479
|
-
providers:
|
|
480
|
-
registry
|
|
481
|
-
modelsAreEqual
|
|
567
|
+
model: getModel,
|
|
568
|
+
models: getModels,
|
|
569
|
+
providers: getProviders,
|
|
570
|
+
registry,
|
|
571
|
+
modelsAreEqual
|
|
482
572
|
});
|
|
483
573
|
//#endregion
|
|
484
574
|
//#region src/llm/protocol.ts
|
|
485
|
-
|
|
486
|
-
(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
sourceId
|
|
575
|
+
var protocol_exports = /* @__PURE__ */ __exportAll({
|
|
576
|
+
ProtocolAuthError: () => ProtocolAuthError,
|
|
577
|
+
ProtocolMismatchError: () => ProtocolMismatchError,
|
|
578
|
+
ProviderProtocolNotFoundError: () => ProviderProtocolNotFoundError,
|
|
579
|
+
clearProtocolProviders: () => clearProtocolProviders,
|
|
580
|
+
complete: () => complete,
|
|
581
|
+
getProtocolProvider: () => getProtocolProvider,
|
|
582
|
+
getProtocolProviders: () => getProtocolProviders,
|
|
583
|
+
registerProtocolProvider: () => registerProtocolProvider,
|
|
584
|
+
resolveProtocolProvider: () => resolveProtocolProvider,
|
|
585
|
+
stream: () => stream$2,
|
|
586
|
+
unregisterProtocolProviders: () => unregisterProtocolProviders
|
|
587
|
+
});
|
|
588
|
+
const ProviderProtocolNotFoundError = NamedError.create("ProviderProtocolNotFoundError", Type$2.Object({ protocol: KnownProviderEnumSchema }));
|
|
589
|
+
const ProtocolMismatchError = NamedError.create("ProtocolMismatchError", Type$2.Object({
|
|
590
|
+
actual: KnownProviderEnumSchema,
|
|
591
|
+
expected: KnownProviderEnumSchema
|
|
592
|
+
}));
|
|
593
|
+
const ProtocolAuthError = NamedError.create("ProtocolAuthError", Type$2.Object({
|
|
594
|
+
message: Type$2.String(),
|
|
595
|
+
protocol: Type$2.String()
|
|
596
|
+
}));
|
|
597
|
+
const protocolRegistry = /* @__PURE__ */ new Map();
|
|
598
|
+
function wrapStream(protocol, stream) {
|
|
599
|
+
return (model, context, options) => {
|
|
600
|
+
if (model.protocol !== protocol) throw new ProtocolMismatchError({
|
|
601
|
+
actual: model.protocol,
|
|
602
|
+
expected: protocol
|
|
514
603
|
});
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
}
|
|
604
|
+
return stream(model, context, options);
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
function registerProtocolProvider(provider, sourceId) {
|
|
608
|
+
const registered = {
|
|
609
|
+
protocol: provider.protocol,
|
|
610
|
+
stream: wrapStream(provider.protocol, provider.stream)
|
|
611
|
+
};
|
|
612
|
+
protocolRegistry.set(provider.protocol, {
|
|
613
|
+
provider: registered,
|
|
614
|
+
...sourceId !== void 0 && { sourceId }
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
function getProtocolProvider(protocol) {
|
|
618
|
+
return protocolRegistry.get(protocol)?.provider;
|
|
619
|
+
}
|
|
620
|
+
function resolveProtocolProvider(model) {
|
|
621
|
+
const provider = getProtocolProvider(model.protocol);
|
|
622
|
+
if (!provider) throw new ProviderProtocolNotFoundError({ protocol: model.protocol });
|
|
623
|
+
return provider;
|
|
624
|
+
}
|
|
625
|
+
function getProtocolProviders() {
|
|
626
|
+
return Array.from(protocolRegistry.values(), (entry) => entry.provider);
|
|
627
|
+
}
|
|
628
|
+
function stream$2(model, context, options) {
|
|
629
|
+
return resolveProtocolProvider(model).stream(model, context, options);
|
|
630
|
+
}
|
|
631
|
+
async function complete(model, context, options) {
|
|
632
|
+
return stream$2(model, context, options).result();
|
|
633
|
+
}
|
|
634
|
+
function unregisterProtocolProviders(sourceId) {
|
|
635
|
+
for (const [protocol, entry] of protocolRegistry.entries()) if (entry.sourceId === sourceId) protocolRegistry.delete(protocol);
|
|
636
|
+
}
|
|
637
|
+
function clearProtocolProviders() {
|
|
638
|
+
protocolRegistry.clear();
|
|
639
|
+
}
|
|
548
640
|
//#endregion
|
|
549
641
|
//#region src/llm/shared.ts
|
|
550
|
-
const CacheRetention = Type$
|
|
551
|
-
Type$
|
|
552
|
-
Type$
|
|
553
|
-
Type$
|
|
642
|
+
const CacheRetention = Type$1.Union([
|
|
643
|
+
Type$1.Literal("none"),
|
|
644
|
+
Type$1.Literal("short"),
|
|
645
|
+
Type$1.Literal("long")
|
|
554
646
|
]);
|
|
555
|
-
const GenerationOptions = Type$
|
|
556
|
-
maxTokens: Type$
|
|
557
|
-
temperature: Type$
|
|
558
|
-
cacheRetention: Type$
|
|
647
|
+
const GenerationOptions = Type$1.Object({
|
|
648
|
+
maxTokens: Type$1.Optional(Type$1.Number()),
|
|
649
|
+
temperature: Type$1.Optional(Type$1.Number()),
|
|
650
|
+
cacheRetention: Type$1.Optional(CacheRetention)
|
|
559
651
|
});
|
|
560
|
-
const HelperOptions = Type$
|
|
561
|
-
headers: Type$
|
|
562
|
-
sessionId: Type$
|
|
563
|
-
apiKey: Type$
|
|
564
|
-
timeoutMs: Type$
|
|
565
|
-
maxRetries: Type$
|
|
566
|
-
signal: Type$
|
|
567
|
-
onPayload: Type$
|
|
568
|
-
metadata: Type$
|
|
652
|
+
const HelperOptions = Type$1.Object({
|
|
653
|
+
headers: Type$1.Optional(Type$1.Record(Type$1.String(), Type$1.String())),
|
|
654
|
+
sessionId: Type$1.Optional(Type$1.String()),
|
|
655
|
+
apiKey: Type$1.Optional(Type$1.String()),
|
|
656
|
+
timeoutMs: Type$1.Optional(Type$1.Number()),
|
|
657
|
+
maxRetries: Type$1.Optional(Type$1.Number()),
|
|
658
|
+
signal: Type$1.Optional(Type$1.Unsafe({})),
|
|
659
|
+
onPayload: Type$1.Optional(Type$1.Unsafe({})),
|
|
660
|
+
metadata: Type$1.Optional(Type$1.Record(Type$1.String(), Type$1.Unknown()))
|
|
569
661
|
});
|
|
570
|
-
const ThinkingBudgets = Type$
|
|
571
|
-
minimal: Type$
|
|
572
|
-
low: Type$
|
|
573
|
-
medium: Type$
|
|
574
|
-
high: Type$
|
|
575
|
-
xhigh: Type$
|
|
662
|
+
const ThinkingBudgets = Type$1.Object({
|
|
663
|
+
minimal: Type$1.Optional(Type$1.Number()),
|
|
664
|
+
low: Type$1.Optional(Type$1.Number()),
|
|
665
|
+
medium: Type$1.Optional(Type$1.Number()),
|
|
666
|
+
high: Type$1.Optional(Type$1.Number()),
|
|
667
|
+
xhigh: Type$1.Optional(Type$1.Number()),
|
|
668
|
+
max: Type$1.Optional(Type$1.Number())
|
|
576
669
|
});
|
|
577
|
-
const SharedOptions = Type$
|
|
670
|
+
const SharedOptions = Type$1.Evaluate(Type$1.Intersect([GenerationOptions, HelperOptions]));
|
|
578
671
|
const DEFAULT_MAX_OUTPUT_TOKENS = 32e3;
|
|
579
672
|
const CONTEXT_WINDOW_OUTPUT_TOLERANCE = 1024;
|
|
580
673
|
function applyDefaultMaxTokens(model, options) {
|
|
@@ -584,16 +677,16 @@ function applyDefaultMaxTokens(model, options) {
|
|
|
584
677
|
maxTokens: options?.maxTokens ?? defaultMaxTokens
|
|
585
678
|
};
|
|
586
679
|
}
|
|
587
|
-
Type$
|
|
588
|
-
baseURL: Type$
|
|
589
|
-
method: Type$
|
|
590
|
-
modelId: Type$
|
|
591
|
-
reasoning: Type$
|
|
592
|
-
thinkingBudgets: Type$
|
|
593
|
-
toolChoice: Type$
|
|
594
|
-
activeTools: Type$
|
|
595
|
-
factoryOptions: Type$
|
|
596
|
-
providerOptions: Type$
|
|
680
|
+
Type$1.Evaluate(Type$1.Intersect([SharedOptions, Type$1.Object({
|
|
681
|
+
baseURL: Type$1.Optional(Type$1.String()),
|
|
682
|
+
method: Type$1.Optional(APIMethodEnumSchema),
|
|
683
|
+
modelId: Type$1.Optional(Type$1.String()),
|
|
684
|
+
reasoning: Type$1.Optional(ActiveThinkingLevel),
|
|
685
|
+
thinkingBudgets: Type$1.Optional(ThinkingBudgets),
|
|
686
|
+
toolChoice: Type$1.Optional(Type$1.Any()),
|
|
687
|
+
activeTools: Type$1.Optional(Type$1.Array(Type$1.String())),
|
|
688
|
+
factoryOptions: Type$1.Optional(Type$1.Record(Type$1.String(), Type$1.Unknown())),
|
|
689
|
+
providerOptions: Type$1.Optional(Type$1.Record(Type$1.String(), Type$1.Record(Type$1.String(), Type$1.Unknown())))
|
|
597
690
|
})]));
|
|
598
691
|
//#endregion
|
|
599
692
|
//#region src/utils/eventstream.ts
|
|
@@ -603,6 +696,8 @@ var EventStream = class {
|
|
|
603
696
|
done = false;
|
|
604
697
|
finalResultPromise;
|
|
605
698
|
resolveFinalResult;
|
|
699
|
+
isComplete;
|
|
700
|
+
extractResult;
|
|
606
701
|
constructor(isComplete, extractResult) {
|
|
607
702
|
this.isComplete = isComplete;
|
|
608
703
|
this.extractResult = extractResult;
|
|
@@ -658,6 +753,21 @@ function createAssistantMessageEventStream() {
|
|
|
658
753
|
return new AssistantMessageEventStream();
|
|
659
754
|
}
|
|
660
755
|
//#endregion
|
|
756
|
+
//#region src/utils/helpers.ts
|
|
757
|
+
/**
|
|
758
|
+
* Drops keys whose value is `undefined`.
|
|
759
|
+
*
|
|
760
|
+
* Under `exactOptionalPropertyTypes` an explicit `undefined` is not assignable to an optional
|
|
761
|
+
* property, so a literal built from optional inputs has to omit the key rather than pass it
|
|
762
|
+
* through. Only worth reaching for when a literal forwards many optional values at once — for a
|
|
763
|
+
* handful of keys a conditional spread reads better.
|
|
764
|
+
*/
|
|
765
|
+
function compact(value) {
|
|
766
|
+
const result = {};
|
|
767
|
+
for (const [key, entry] of Object.entries(value)) if (entry !== void 0) result[key] = entry;
|
|
768
|
+
return result;
|
|
769
|
+
}
|
|
770
|
+
//#endregion
|
|
661
771
|
//#region src/llm/runtime.ts
|
|
662
772
|
function mergeHeaders(...headerSources) {
|
|
663
773
|
const merged = {};
|
|
@@ -705,15 +815,23 @@ async function resolveAISDKLanguageModel(model, options) {
|
|
|
705
815
|
if (Object.keys(headers).length > 0) factoryOptions.headers = headers;
|
|
706
816
|
if (baseURL) factoryOptions.baseURL = baseURL;
|
|
707
817
|
if (npm === "@ai-sdk/openai-compatible") {
|
|
708
|
-
if (!baseURL) throw new
|
|
818
|
+
if (!baseURL) throw new ProtocolAuthError({
|
|
709
819
|
protocol: model.protocol,
|
|
710
820
|
message: "AI SDK openai-compatible transport requires model.baseUrl, model.api.url, or options.baseURL"
|
|
711
821
|
});
|
|
712
822
|
factoryOptions.name ??= model.provider.id;
|
|
713
823
|
factoryOptions.includeUsage ??= true;
|
|
714
824
|
}
|
|
715
|
-
if (npm === "@codeworksh/ai-sdk-openai-codex"
|
|
716
|
-
|
|
825
|
+
if (npm === "@codeworksh/ai-sdk-openai-codex") {
|
|
826
|
+
const cacheDisabled = options.cacheRetention === "none" || typeof process !== "undefined" && process.env.CODEWORK_CACHE_RETENTION === "none";
|
|
827
|
+
if (options.sessionId && !cacheDisabled) factoryOptions.sessionId ??= options.sessionId;
|
|
828
|
+
factoryOptions.compat ??= {
|
|
829
|
+
...model.compat,
|
|
830
|
+
supportsImages: model.input.includes("image")
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
const provider = createProvider(factoryOptions);
|
|
834
|
+
return resolveLanguageModel(provider, model, options.method);
|
|
717
835
|
}
|
|
718
836
|
//#endregion
|
|
719
837
|
//#region src/utils/jsonparse.ts
|
|
@@ -788,9 +906,7 @@ function repairInvalidJsonStringContent(input) {
|
|
|
788
906
|
case " ":
|
|
789
907
|
output += "\\t";
|
|
790
908
|
break;
|
|
791
|
-
default:
|
|
792
|
-
output += char;
|
|
793
|
-
break;
|
|
909
|
+
default: output += char;
|
|
794
910
|
}
|
|
795
911
|
}
|
|
796
912
|
if (escaping) output += "\\\\";
|
|
@@ -824,7 +940,7 @@ function sanitizeSurrogates(text) {
|
|
|
824
940
|
//#endregion
|
|
825
941
|
//#region src/llm/transform.ts
|
|
826
942
|
function createAssistantMessage(model) {
|
|
827
|
-
return
|
|
943
|
+
return createAssistantMessage$1({
|
|
828
944
|
role: "assistant",
|
|
829
945
|
parts: [],
|
|
830
946
|
protocol: model.protocol,
|
|
@@ -863,8 +979,8 @@ function userContent(parts, supportsImages) {
|
|
|
863
979
|
continue;
|
|
864
980
|
}
|
|
865
981
|
if (supportsImages) content.push({
|
|
866
|
-
type: "
|
|
867
|
-
|
|
982
|
+
type: "file",
|
|
983
|
+
data: part.data,
|
|
868
984
|
mediaType: part.mimeType
|
|
869
985
|
});
|
|
870
986
|
}
|
|
@@ -904,13 +1020,27 @@ function toolResultOutput(toolCall) {
|
|
|
904
1020
|
type: "text",
|
|
905
1021
|
text
|
|
906
1022
|
}] : [], ...images.map((image) => ({
|
|
907
|
-
type: "
|
|
908
|
-
data:
|
|
1023
|
+
type: "file",
|
|
1024
|
+
data: {
|
|
1025
|
+
type: "data",
|
|
1026
|
+
data: image.data
|
|
1027
|
+
},
|
|
909
1028
|
mediaType: image.mimeType
|
|
910
1029
|
}))]
|
|
911
1030
|
};
|
|
912
1031
|
}
|
|
913
|
-
function
|
|
1032
|
+
function normalizeOpenAICodexIdPart(value) {
|
|
1033
|
+
return value.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64).replace(/_+$/, "");
|
|
1034
|
+
}
|
|
1035
|
+
function normalizeOpenAICodexToolCallId(id, model, source) {
|
|
1036
|
+
if (!id.includes("|")) return normalizeOpenAICodexIdPart(id);
|
|
1037
|
+
const [callId, itemId = ""] = id.split("|");
|
|
1038
|
+
const normalizedCallId = normalizeOpenAICodexIdPart(callId ?? "");
|
|
1039
|
+
let normalizedItemId = source.provider.id !== model.provider.id || source.protocol !== model.protocol ? `fc_${shortHash(itemId)}` : normalizeOpenAICodexIdPart(itemId);
|
|
1040
|
+
if (!normalizedItemId.startsWith("fc_") && !normalizedItemId.startsWith("ctc_")) normalizedItemId = normalizeOpenAICodexIdPart(`fc_${normalizedItemId}`);
|
|
1041
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
1042
|
+
}
|
|
1043
|
+
function assistantMessages(message, model) {
|
|
914
1044
|
if (message.stopReason === "error" || message.stopReason === "aborted") return [];
|
|
915
1045
|
const assistantContent = [];
|
|
916
1046
|
const toolResults = [];
|
|
@@ -920,33 +1050,39 @@ function assistantMessages(message) {
|
|
|
920
1050
|
if (text.trim().length === 0) continue;
|
|
921
1051
|
assistantContent.push({
|
|
922
1052
|
type: "text",
|
|
923
|
-
text
|
|
1053
|
+
text,
|
|
1054
|
+
...part.textSignature && message.protocol === KnownProviderEnum.openaiCodex ? { providerOptions: { "openai-codex": { messageId: part.textSignature } } } : {}
|
|
924
1055
|
});
|
|
925
1056
|
continue;
|
|
926
1057
|
}
|
|
927
1058
|
if (part.type === "thinking") {
|
|
928
1059
|
const thinking = sanitizeSurrogates(part.thinking);
|
|
929
|
-
if (thinking.trim().length === 0) continue;
|
|
1060
|
+
if (thinking.trim().length === 0 && !part.thinkingSignature) continue;
|
|
930
1061
|
const reasoning = {
|
|
931
1062
|
type: "reasoning",
|
|
932
1063
|
text: thinking
|
|
933
1064
|
};
|
|
934
|
-
if (part.thinkingSignature) reasoning.providerOptions = { anthropic: { signature: part.thinkingSignature } };
|
|
1065
|
+
if (part.thinkingSignature) reasoning.providerOptions = message.protocol === KnownProviderEnum.openaiCodex ? { "openai-codex": { reasoningItem: part.thinkingSignature } } : { anthropic: { signature: part.thinkingSignature } };
|
|
935
1066
|
assistantContent.push(reasoning);
|
|
936
1067
|
continue;
|
|
937
1068
|
}
|
|
938
1069
|
if (part.type !== "toolCall") continue;
|
|
1070
|
+
const codexToolMetadata = {};
|
|
1071
|
+
if (part.namespace && message.protocol === KnownProviderEnum.openaiCodex) codexToolMetadata.namespace = part.namespace;
|
|
1072
|
+
if (model.protocol === KnownProviderEnum.openaiCodex && message.protocol === model.protocol && message.provider.id === model.provider.id && message.model !== model.id) codexToolMetadata.omitItemId = true;
|
|
939
1073
|
assistantContent.push({
|
|
940
1074
|
type: "tool-call",
|
|
941
1075
|
toolCallId: part.callID,
|
|
942
1076
|
toolName: part.name,
|
|
943
|
-
input: sanitizeRecord(part.arguments ?? {})
|
|
1077
|
+
input: sanitizeRecord(part.arguments ?? {}),
|
|
1078
|
+
...Object.keys(codexToolMetadata).length > 0 ? { providerOptions: { "openai-codex": codexToolMetadata } } : {}
|
|
944
1079
|
});
|
|
945
1080
|
const terminal = part.status === "pending" || part.status === "running" ? void 0 : part;
|
|
946
1081
|
toolResults.push({
|
|
947
1082
|
type: "tool-result",
|
|
948
1083
|
toolCallId: part.callID,
|
|
949
1084
|
toolName: part.name,
|
|
1085
|
+
...terminal?.addedToolNames?.length ? { providerOptions: { "openai-codex": { addedToolNames: terminal.addedToolNames } } } : {},
|
|
950
1086
|
output: terminal ? toolResultOutput(terminal) : {
|
|
951
1087
|
type: "error-text",
|
|
952
1088
|
value: "No result provided"
|
|
@@ -966,23 +1102,28 @@ function assistantMessages(message) {
|
|
|
966
1102
|
}
|
|
967
1103
|
function convertMessages(context, model) {
|
|
968
1104
|
const messages = [];
|
|
969
|
-
const transformedMessages =
|
|
1105
|
+
const transformedMessages = transformMessages(context.messages, model, model.protocol === KnownProviderEnum.openaiCodex ? normalizeOpenAICodexToolCallId : void 0);
|
|
970
1106
|
for (const msg of transformedMessages) {
|
|
971
1107
|
if (msg.role === "user") {
|
|
972
1108
|
messages.push(...userContent(msg.parts, model.input.includes("image")));
|
|
973
1109
|
continue;
|
|
974
1110
|
}
|
|
975
|
-
messages.push(...assistantMessages(msg));
|
|
1111
|
+
messages.push(...assistantMessages(msg, model));
|
|
976
1112
|
}
|
|
977
1113
|
return messages;
|
|
978
1114
|
}
|
|
979
|
-
function convertTools(tools) {
|
|
1115
|
+
function convertTools(tools, model) {
|
|
980
1116
|
if (!tools || tools.length === 0) return;
|
|
981
1117
|
const result = {};
|
|
982
|
-
for (const tool of tools)
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1118
|
+
for (const tool of tools) {
|
|
1119
|
+
const constraint = model?.protocol === KnownProviderEnum.openaiCodex ? resolveOpenAICodexToolConstraint(tool, model.compat) : void 0;
|
|
1120
|
+
result[tool.name] = {
|
|
1121
|
+
description: tool.description,
|
|
1122
|
+
inputSchema: jsonSchema(tool.parameters),
|
|
1123
|
+
...constraint?.type === "json_schema" ? { strict: true } : {},
|
|
1124
|
+
...constraint?.type === "grammar" ? { providerOptions: { "openai-codex": { grammar: constraint } } } : {}
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
986
1127
|
return result;
|
|
987
1128
|
}
|
|
988
1129
|
function mapFinishReason(reason) {
|
|
@@ -995,17 +1136,19 @@ function mapFinishReason(reason) {
|
|
|
995
1136
|
default: return "stop";
|
|
996
1137
|
}
|
|
997
1138
|
}
|
|
998
|
-
function mapUsage(usage, model) {
|
|
999
|
-
const cacheRead = usage?.inputTokenDetails
|
|
1139
|
+
function mapUsage(usage, model, costMultiplier = 1) {
|
|
1140
|
+
const cacheRead = usage?.inputTokenDetails.cacheReadTokens ?? 0;
|
|
1000
1141
|
const cacheWrite = usage?.inputTokenDetails?.cacheWriteTokens ?? 0;
|
|
1001
1142
|
const input = usage?.inputTokenDetails?.noCacheTokens ?? Math.max((usage?.inputTokens ?? 0) - cacheRead - cacheWrite, 0);
|
|
1002
1143
|
const output = usage?.outputTokens ?? 0;
|
|
1144
|
+
const totalTokens = usage?.totalTokens ?? input + cacheRead + cacheWrite + output;
|
|
1003
1145
|
const result = {
|
|
1004
1146
|
input,
|
|
1005
1147
|
output,
|
|
1006
1148
|
cacheRead,
|
|
1007
1149
|
cacheWrite,
|
|
1008
|
-
|
|
1150
|
+
...usage?.outputTokenDetails?.reasoningTokens !== void 0 ? { reasoning: usage.outputTokenDetails.reasoningTokens } : {},
|
|
1151
|
+
totalTokens,
|
|
1009
1152
|
cost: {
|
|
1010
1153
|
input: 0,
|
|
1011
1154
|
output: 0,
|
|
@@ -1014,7 +1157,14 @@ function mapUsage(usage, model) {
|
|
|
1014
1157
|
total: 0
|
|
1015
1158
|
}
|
|
1016
1159
|
};
|
|
1017
|
-
|
|
1160
|
+
calculateCost(model, result);
|
|
1161
|
+
if (costMultiplier !== 1) {
|
|
1162
|
+
result.cost.input *= costMultiplier;
|
|
1163
|
+
result.cost.output *= costMultiplier;
|
|
1164
|
+
result.cost.cacheRead *= costMultiplier;
|
|
1165
|
+
result.cost.cacheWrite *= costMultiplier;
|
|
1166
|
+
result.cost.total = result.cost.input + result.cost.output + result.cost.cacheRead + result.cost.cacheWrite;
|
|
1167
|
+
}
|
|
1018
1168
|
return result;
|
|
1019
1169
|
}
|
|
1020
1170
|
function toolCallFromPart(part) {
|
|
@@ -1082,7 +1232,8 @@ const DEFAULT_THINKING_BUDGET_FRACTIONS = {
|
|
|
1082
1232
|
low: .1,
|
|
1083
1233
|
medium: .25,
|
|
1084
1234
|
high: .5,
|
|
1085
|
-
xhigh: .8
|
|
1235
|
+
xhigh: .8,
|
|
1236
|
+
max: .95
|
|
1086
1237
|
};
|
|
1087
1238
|
const MIN_THINKING_BUDGET = 1024;
|
|
1088
1239
|
function resolveThinkingBudget(model, options, level) {
|
|
@@ -1097,7 +1248,7 @@ function resolveThinkingBudget(model, options, level) {
|
|
|
1097
1248
|
function reasoningProviderOptions(model, options) {
|
|
1098
1249
|
const requestedLevel = options.reasoning;
|
|
1099
1250
|
if (!requestedLevel) return {};
|
|
1100
|
-
const level =
|
|
1251
|
+
const level = clampThinkingLevel(model, requestedLevel);
|
|
1101
1252
|
if (level === "off") return {};
|
|
1102
1253
|
const mapped = model.thinkingLevelMap?.[level] ?? level;
|
|
1103
1254
|
if (!mapped) return {};
|
|
@@ -1125,6 +1276,11 @@ function resolveProviderOptions(model, options) {
|
|
|
1125
1276
|
function partIndex(output, block) {
|
|
1126
1277
|
return output.parts.indexOf(block);
|
|
1127
1278
|
}
|
|
1279
|
+
function openAICodexMetadata(value) {
|
|
1280
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
1281
|
+
const metadata = value["openai-codex"];
|
|
1282
|
+
return typeof metadata === "object" && metadata !== null && !Array.isArray(metadata) ? metadata : void 0;
|
|
1283
|
+
}
|
|
1128
1284
|
function ensureTextBlock(output, id, stream) {
|
|
1129
1285
|
const existing = output.parts.find((part) => part.type === "text" && part.streamId === id);
|
|
1130
1286
|
if (existing) return existing;
|
|
@@ -1182,9 +1338,10 @@ function ensureToolCallBlock(output, id, name, stream, emitStart) {
|
|
|
1182
1338
|
});
|
|
1183
1339
|
return block;
|
|
1184
1340
|
}
|
|
1185
|
-
function finishTextBlock(output, id, stream) {
|
|
1341
|
+
function finishTextBlock(output, id, stream, textSignature) {
|
|
1186
1342
|
const block = output.parts.find((part) => part.type === "text" && part.streamId === id);
|
|
1187
1343
|
if (!block) return;
|
|
1344
|
+
if (textSignature) block.textSignature = textSignature;
|
|
1188
1345
|
const index = partIndex(output, block);
|
|
1189
1346
|
delete block.streamId;
|
|
1190
1347
|
stream.push({
|
|
@@ -1194,9 +1351,10 @@ function finishTextBlock(output, id, stream) {
|
|
|
1194
1351
|
partial: output
|
|
1195
1352
|
});
|
|
1196
1353
|
}
|
|
1197
|
-
function finishThinkingBlock(output, id, stream) {
|
|
1354
|
+
function finishThinkingBlock(output, id, stream, thinkingSignature) {
|
|
1198
1355
|
const block = output.parts.find((part) => part.type === "thinking" && part.streamId === id);
|
|
1199
1356
|
if (!block) return;
|
|
1357
|
+
if (thinkingSignature) block.thinkingSignature = thinkingSignature;
|
|
1200
1358
|
const index = partIndex(output, block);
|
|
1201
1359
|
delete block.streamId;
|
|
1202
1360
|
stream.push({
|
|
@@ -1224,6 +1382,8 @@ function finalizeToolCall(output, part, stream) {
|
|
|
1224
1382
|
block.callID = part.toolCallId;
|
|
1225
1383
|
block.name = part.toolName;
|
|
1226
1384
|
block.arguments = typeof part.input === "object" && part.input !== null ? part.input : {};
|
|
1385
|
+
const namespace = openAICodexMetadata(part.providerMetadata)?.namespace;
|
|
1386
|
+
if (typeof namespace === "string") block.namespace = namespace;
|
|
1227
1387
|
block.time.end = Date.now();
|
|
1228
1388
|
delete block.partialJson;
|
|
1229
1389
|
if (!existing) output.parts.push(block);
|
|
@@ -1235,7 +1395,7 @@ function finalizeToolCall(output, part, stream) {
|
|
|
1235
1395
|
partial: output
|
|
1236
1396
|
});
|
|
1237
1397
|
}
|
|
1238
|
-
function handlePart(part, output, model, stream) {
|
|
1398
|
+
function handlePart(part, output, model, stream, costMultiplier) {
|
|
1239
1399
|
switch (part.type) {
|
|
1240
1400
|
case "text-start":
|
|
1241
1401
|
ensureTextBlock(output, part.id, stream);
|
|
@@ -1251,9 +1411,11 @@ function handlePart(part, output, model, stream) {
|
|
|
1251
1411
|
});
|
|
1252
1412
|
break;
|
|
1253
1413
|
}
|
|
1254
|
-
case "text-end":
|
|
1255
|
-
|
|
1414
|
+
case "text-end": {
|
|
1415
|
+
const messageId = openAICodexMetadata(part.providerMetadata)?.messageId;
|
|
1416
|
+
finishTextBlock(output, part.id, stream, typeof messageId === "string" ? messageId : void 0);
|
|
1256
1417
|
break;
|
|
1418
|
+
}
|
|
1257
1419
|
case "reasoning-start":
|
|
1258
1420
|
ensureThinkingBlock(output, part.id, stream);
|
|
1259
1421
|
break;
|
|
@@ -1270,9 +1432,11 @@ function handlePart(part, output, model, stream) {
|
|
|
1270
1432
|
});
|
|
1271
1433
|
break;
|
|
1272
1434
|
}
|
|
1273
|
-
case "reasoning-end":
|
|
1274
|
-
|
|
1435
|
+
case "reasoning-end": {
|
|
1436
|
+
const reasoningItem = openAICodexMetadata(part.providerMetadata)?.reasoningItem;
|
|
1437
|
+
finishThinkingBlock(output, part.id, stream, typeof reasoningItem === "string" ? reasoningItem : void 0);
|
|
1275
1438
|
break;
|
|
1439
|
+
}
|
|
1276
1440
|
case "tool-input-start":
|
|
1277
1441
|
ensureToolCallBlock(output, part.id, part.toolName, stream, true);
|
|
1278
1442
|
break;
|
|
@@ -1296,20 +1460,30 @@ function handlePart(part, output, model, stream) {
|
|
|
1296
1460
|
case "finish-step":
|
|
1297
1461
|
output.responseId ||= part.response.id;
|
|
1298
1462
|
if (part.response.modelId && part.response.modelId !== model.id) output.responseModel ||= part.response.modelId;
|
|
1299
|
-
output.usage = mapUsage(part.usage, model);
|
|
1463
|
+
output.usage = mapUsage(part.usage, model, costMultiplier);
|
|
1300
1464
|
output.stopReason = mapFinishReason(part.finishReason);
|
|
1301
1465
|
break;
|
|
1302
1466
|
case "finish":
|
|
1303
|
-
output.usage = mapUsage(part.totalUsage, model);
|
|
1467
|
+
output.usage = mapUsage(part.totalUsage, model, costMultiplier);
|
|
1304
1468
|
output.stopReason = mapFinishReason(part.finishReason);
|
|
1305
1469
|
break;
|
|
1306
1470
|
case "abort":
|
|
1307
1471
|
output.stopReason = "aborted";
|
|
1308
|
-
output.errorMessage = part.reason;
|
|
1472
|
+
if (part.reason !== void 0) output.errorMessage = part.reason;
|
|
1309
1473
|
break;
|
|
1310
1474
|
case "error": throw part.error instanceof Error ? part.error : new Error(formatThrownError(part.error));
|
|
1311
1475
|
}
|
|
1312
1476
|
}
|
|
1477
|
+
function resolveCostMultiplier(model, options, providerOptions) {
|
|
1478
|
+
if (providerOptionsKey(model) !== "openai-codex") return 1;
|
|
1479
|
+
const providerTier = providerOptions["openai-codex"]?.serviceTier;
|
|
1480
|
+
const factoryTier = options.factoryOptions?.serviceTier;
|
|
1481
|
+
const modelTier = model.options?.serviceTier;
|
|
1482
|
+
const serviceTier = providerTier ?? factoryTier ?? modelTier;
|
|
1483
|
+
if (serviceTier === "flex") return .5;
|
|
1484
|
+
if (serviceTier === "priority") return model.id === "gpt-5.5" ? 2.5 : 2;
|
|
1485
|
+
return 1;
|
|
1486
|
+
}
|
|
1313
1487
|
/**
|
|
1314
1488
|
* Resolve maxOutputTokens so that (maxOutputTokens + thinkingBudget) does not exceed model.maxTokens.
|
|
1315
1489
|
* Without this adjustment @ai-sdk/anthropic logs a warning and silently caps the value.
|
|
@@ -1332,11 +1506,12 @@ const stream$1 = (model, context, options) => {
|
|
|
1332
1506
|
const output = createAssistantMessage(model);
|
|
1333
1507
|
try {
|
|
1334
1508
|
const languageModel = await resolveAISDKLanguageModel(model, runtimeOptions);
|
|
1335
|
-
const tools = convertTools(context.tools);
|
|
1509
|
+
const tools = convertTools(context.tools, model);
|
|
1336
1510
|
const messages = convertMessages(context, model);
|
|
1337
1511
|
const providerOptions = resolveProviderOptions(model, runtimeOptions);
|
|
1512
|
+
const costMultiplier = resolveCostMultiplier(model, runtimeOptions, providerOptions);
|
|
1338
1513
|
const activeTools = runtimeOptions.activeTools?.filter((name) => !tools || name in tools);
|
|
1339
|
-
let params = {
|
|
1514
|
+
let params = compact({
|
|
1340
1515
|
model: languageModel,
|
|
1341
1516
|
system: context.systemPrompt,
|
|
1342
1517
|
messages,
|
|
@@ -1350,7 +1525,7 @@ const stream$1 = (model, context, options) => {
|
|
|
1350
1525
|
timeout: runtimeOptions.timeoutMs,
|
|
1351
1526
|
maxRetries: runtimeOptions.maxRetries,
|
|
1352
1527
|
headers: runtimeOptions.headers
|
|
1353
|
-
};
|
|
1528
|
+
});
|
|
1354
1529
|
const payload = await runtimeOptions.onPayload?.(params, model);
|
|
1355
1530
|
if (payload !== void 0) params = payload;
|
|
1356
1531
|
const result = streamText(params);
|
|
@@ -1358,7 +1533,7 @@ const stream$1 = (model, context, options) => {
|
|
|
1358
1533
|
type: "start",
|
|
1359
1534
|
partial: output
|
|
1360
1535
|
});
|
|
1361
|
-
for await (const part of result.fullStream) handlePart(part, output, model, stream);
|
|
1536
|
+
for await (const part of result.fullStream) handlePart(part, output, model, stream, costMultiplier);
|
|
1362
1537
|
if (runtimeOptions.signal?.aborted || output.stopReason === "aborted") throw new Error(output.errorMessage || "Request was aborted");
|
|
1363
1538
|
if (output.stopReason === "error") throw new Error(output.errorMessage || "Provider returned an error stop reason");
|
|
1364
1539
|
output.time.completed = Date.now();
|
|
@@ -1396,22 +1571,22 @@ function createAISDKProtocol(protocol) {
|
|
|
1396
1571
|
};
|
|
1397
1572
|
}
|
|
1398
1573
|
function registerAISDKProtocols() {
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1574
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.anthropic));
|
|
1575
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.google));
|
|
1576
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.googleVertex));
|
|
1577
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.googleVertexAnthropic));
|
|
1578
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.openai));
|
|
1579
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.openaiCompatible));
|
|
1580
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.openaiCodex));
|
|
1581
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.openrouter));
|
|
1582
|
+
registerProtocolProvider(createAISDKProtocol(KnownProviderEnum.xai));
|
|
1408
1583
|
}
|
|
1409
1584
|
//#endregion
|
|
1410
1585
|
//#region src/llm/register.ts
|
|
1411
1586
|
registerAISDKProtocols();
|
|
1412
1587
|
//#endregion
|
|
1413
1588
|
//#region src/stream.ts
|
|
1414
|
-
const streamImpl = (model, context, options) =>
|
|
1589
|
+
const streamImpl = (model, context, options) => stream$2(model, context, options);
|
|
1415
1590
|
/**
|
|
1416
1591
|
* Start a streaming assistant response for a model/context pair.
|
|
1417
1592
|
*
|
|
@@ -1422,10 +1597,9 @@ const streamImpl = (model, context, options) => Protocol.stream(model, context,
|
|
|
1422
1597
|
* - `stream.resolveProtocolProvider(...)` exposes protocol resolution for advanced integrations.
|
|
1423
1598
|
*/
|
|
1424
1599
|
const stream = Object.assign(streamImpl, {
|
|
1425
|
-
complete
|
|
1426
|
-
resolveProtocolProvider
|
|
1600
|
+
complete,
|
|
1601
|
+
resolveProtocolProvider
|
|
1427
1602
|
});
|
|
1428
|
-
Protocol.complete;
|
|
1429
1603
|
//#endregion
|
|
1430
1604
|
//#region src/utils/validation.ts
|
|
1431
1605
|
const validators = /* @__PURE__ */ new WeakMap();
|
|
@@ -1478,6 +1652,6 @@ function validateToolArguments(tool, toolExecution) {
|
|
|
1478
1652
|
return validateSchema(tool.parameters, toolExecution.rawArgs, `Tool "${toolExecution.name}"`);
|
|
1479
1653
|
}
|
|
1480
1654
|
//#endregion
|
|
1481
|
-
export { Event, EventStream, Message, Model, Protocol, ThinkingBudgets, Type, createAssistantMessageEventStream, createOpenAICodex, llm, openaiCodex, stream, validateSchema, validateToolArguments, validateToolCall };
|
|
1655
|
+
export { event_exports as Event, EventStream, message_exports as Message, model_exports as Model, protocol_exports as Protocol, ThinkingBudgets, Type, createAssistantMessageEventStream, createOpenAICodex, llm, openAICodexTools, openaiCodex, stream, validateSchema, validateToolArguments, validateToolCall };
|
|
1482
1656
|
|
|
1483
1657
|
//# sourceMappingURL=index.mjs.map
|