pybao-cli 1.3.3
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/LICENSE +201 -0
- package/README.md +440 -0
- package/README.zh-CN.md +338 -0
- package/cli-acp.js +82 -0
- package/cli.js +105 -0
- package/dist/REPL-WPV32MTF.js +42 -0
- package/dist/REPL-WPV32MTF.js.map +7 -0
- package/dist/acp-75HO2LBV.js +1357 -0
- package/dist/acp-75HO2LBV.js.map +7 -0
- package/dist/agentsValidate-6Z57ARKC.js +373 -0
- package/dist/agentsValidate-6Z57ARKC.js.map +7 -0
- package/dist/ask-NXXXCGY4.js +125 -0
- package/dist/ask-NXXXCGY4.js.map +7 -0
- package/dist/autoUpdater-PJMGNPUG.js +17 -0
- package/dist/autoUpdater-PJMGNPUG.js.map +7 -0
- package/dist/chunk-27GYWUY2.js +72 -0
- package/dist/chunk-27GYWUY2.js.map +7 -0
- package/dist/chunk-3DFBSQIT.js +23 -0
- package/dist/chunk-3DFBSQIT.js.map +7 -0
- package/dist/chunk-3KNGJX7Q.js +794 -0
- package/dist/chunk-3KNGJX7Q.js.map +7 -0
- package/dist/chunk-3PDD7M4T.js +164 -0
- package/dist/chunk-3PDD7M4T.js.map +7 -0
- package/dist/chunk-3ZNSAB7B.js +515 -0
- package/dist/chunk-3ZNSAB7B.js.map +7 -0
- package/dist/chunk-4SNFQYCY.js +511 -0
- package/dist/chunk-4SNFQYCY.js.map +7 -0
- package/dist/chunk-4XPNRLJG.js +1609 -0
- package/dist/chunk-4XPNRLJG.js.map +7 -0
- package/dist/chunk-5P7HBXTD.js +12 -0
- package/dist/chunk-5P7HBXTD.js.map +7 -0
- package/dist/chunk-6RZIUY5K.js +191 -0
- package/dist/chunk-6RZIUY5K.js.map +7 -0
- package/dist/chunk-6WELHKDA.js +240 -0
- package/dist/chunk-6WELHKDA.js.map +7 -0
- package/dist/chunk-7AAE6EO2.js +145 -0
- package/dist/chunk-7AAE6EO2.js.map +7 -0
- package/dist/chunk-A3BVXXA3.js +47 -0
- package/dist/chunk-A3BVXXA3.js.map +7 -0
- package/dist/chunk-A6PUMROK.js +152 -0
- package/dist/chunk-A6PUMROK.js.map +7 -0
- package/dist/chunk-BH3Y62E3.js +11 -0
- package/dist/chunk-BH3Y62E3.js.map +7 -0
- package/dist/chunk-BJSWTHRM.js +16 -0
- package/dist/chunk-BJSWTHRM.js.map +7 -0
- package/dist/chunk-BQA2EOUU.js +124 -0
- package/dist/chunk-BQA2EOUU.js.map +7 -0
- package/dist/chunk-CZZKRPE2.js +19 -0
- package/dist/chunk-CZZKRPE2.js.map +7 -0
- package/dist/chunk-ERMQRV55.js +24 -0
- package/dist/chunk-ERMQRV55.js.map +7 -0
- package/dist/chunk-HB2P6645.js +34 -0
- package/dist/chunk-HB2P6645.js.map +7 -0
- package/dist/chunk-HIRIJ2LQ.js +1256 -0
- package/dist/chunk-HIRIJ2LQ.js.map +7 -0
- package/dist/chunk-ICTEVBLN.js +735 -0
- package/dist/chunk-ICTEVBLN.js.map +7 -0
- package/dist/chunk-JKGOGSFT.js +128 -0
- package/dist/chunk-JKGOGSFT.js.map +7 -0
- package/dist/chunk-JZDE77EH.js +836 -0
- package/dist/chunk-JZDE77EH.js.map +7 -0
- package/dist/chunk-M624LT6O.js +17 -0
- package/dist/chunk-M624LT6O.js.map +7 -0
- package/dist/chunk-OMELVAJD.js +96 -0
- package/dist/chunk-OMELVAJD.js.map +7 -0
- package/dist/chunk-OUXHGDLH.js +95 -0
- package/dist/chunk-OUXHGDLH.js.map +7 -0
- package/dist/chunk-PCXUZ6AT.js +249 -0
- package/dist/chunk-PCXUZ6AT.js.map +7 -0
- package/dist/chunk-Q24ZGKIE.js +1097 -0
- package/dist/chunk-Q24ZGKIE.js.map +7 -0
- package/dist/chunk-QBHEERCF.js +30254 -0
- package/dist/chunk-QBHEERCF.js.map +7 -0
- package/dist/chunk-QIHB5PYM.js +472 -0
- package/dist/chunk-QIHB5PYM.js.map +7 -0
- package/dist/chunk-RQVLBMP7.js +24 -0
- package/dist/chunk-RQVLBMP7.js.map +7 -0
- package/dist/chunk-SWYJOV5E.js +490 -0
- package/dist/chunk-SWYJOV5E.js.map +7 -0
- package/dist/chunk-T6GVXTNQ.js +21 -0
- package/dist/chunk-T6GVXTNQ.js.map +7 -0
- package/dist/chunk-T7GPUZVK.js +766 -0
- package/dist/chunk-T7GPUZVK.js.map +7 -0
- package/dist/chunk-TXFCNQDE.js +2934 -0
- package/dist/chunk-TXFCNQDE.js.map +7 -0
- package/dist/chunk-UNNVICVU.js +95 -0
- package/dist/chunk-UNNVICVU.js.map +7 -0
- package/dist/chunk-UUNVJZWA.js +515 -0
- package/dist/chunk-UUNVJZWA.js.map +7 -0
- package/dist/chunk-VRGR4ZTQ.js +49 -0
- package/dist/chunk-VRGR4ZTQ.js.map +7 -0
- package/dist/chunk-VTVTEE5N.js +2613 -0
- package/dist/chunk-VTVTEE5N.js.map +7 -0
- package/dist/chunk-WPTPPOYN.js +936 -0
- package/dist/chunk-WPTPPOYN.js.map +7 -0
- package/dist/chunk-XXFY63TM.js +196 -0
- package/dist/chunk-XXFY63TM.js.map +7 -0
- package/dist/chunk-Z3HMXDXP.js +654 -0
- package/dist/chunk-Z3HMXDXP.js.map +7 -0
- package/dist/chunk-ZJGXEWKF.js +138 -0
- package/dist/chunk-ZJGXEWKF.js.map +7 -0
- package/dist/cli-RFYBXM7F.js +3917 -0
- package/dist/cli-RFYBXM7F.js.map +7 -0
- package/dist/commands-YOXMODDO.js +46 -0
- package/dist/commands-YOXMODDO.js.map +7 -0
- package/dist/config-5OPX3H2K.js +81 -0
- package/dist/config-5OPX3H2K.js.map +7 -0
- package/dist/context-THRRBPFP.js +30 -0
- package/dist/context-THRRBPFP.js.map +7 -0
- package/dist/costTracker-ELNBZ2DN.js +19 -0
- package/dist/costTracker-ELNBZ2DN.js.map +7 -0
- package/dist/customCommands-4XOZH44N.js +25 -0
- package/dist/customCommands-4XOZH44N.js.map +7 -0
- package/dist/env-EL4KBHMB.js +22 -0
- package/dist/env-EL4KBHMB.js.map +7 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +7 -0
- package/dist/kodeAgentSessionId-PROTVRBR.js +13 -0
- package/dist/kodeAgentSessionId-PROTVRBR.js.map +7 -0
- package/dist/kodeAgentSessionLoad-UMPV7MC3.js +18 -0
- package/dist/kodeAgentSessionLoad-UMPV7MC3.js.map +7 -0
- package/dist/kodeAgentSessionResume-YJS4FVQM.js +16 -0
- package/dist/kodeAgentSessionResume-YJS4FVQM.js.map +7 -0
- package/dist/kodeAgentStreamJson-3T26CHCP.js +13 -0
- package/dist/kodeAgentStreamJson-3T26CHCP.js.map +7 -0
- package/dist/kodeAgentStreamJsonSession-BZS2VDCY.js +131 -0
- package/dist/kodeAgentStreamJsonSession-BZS2VDCY.js.map +7 -0
- package/dist/kodeAgentStructuredStdio-TNB6U6SP.js +10 -0
- package/dist/kodeAgentStructuredStdio-TNB6U6SP.js.map +7 -0
- package/dist/kodeHooks-VUAWIY2D.js +36 -0
- package/dist/kodeHooks-VUAWIY2D.js.map +7 -0
- package/dist/llm-A3BCM4Q2.js +3118 -0
- package/dist/llm-A3BCM4Q2.js.map +7 -0
- package/dist/llmLazy-ZJSRLZVD.js +15 -0
- package/dist/llmLazy-ZJSRLZVD.js.map +7 -0
- package/dist/loader-HZQBWO74.js +28 -0
- package/dist/loader-HZQBWO74.js.map +7 -0
- package/dist/mcp-XKOJ55B2.js +49 -0
- package/dist/mcp-XKOJ55B2.js.map +7 -0
- package/dist/mentionProcessor-ANYU5MLF.js +211 -0
- package/dist/mentionProcessor-ANYU5MLF.js.map +7 -0
- package/dist/messages-75DL5XBP.js +63 -0
- package/dist/messages-75DL5XBP.js.map +7 -0
- package/dist/model-OPJGJZRC.js +30 -0
- package/dist/model-OPJGJZRC.js.map +7 -0
- package/dist/openai-DT54BAFP.js +29 -0
- package/dist/openai-DT54BAFP.js.map +7 -0
- package/dist/outputStyles-TPFVI52O.js +28 -0
- package/dist/outputStyles-TPFVI52O.js.map +7 -0
- package/dist/package.json +4 -0
- package/dist/pluginRuntime-W74PYSZ4.js +218 -0
- package/dist/pluginRuntime-W74PYSZ4.js.map +7 -0
- package/dist/pluginValidation-FALYRVI2.js +17 -0
- package/dist/pluginValidation-FALYRVI2.js.map +7 -0
- package/dist/prompts-J4TPRMJ3.js +48 -0
- package/dist/prompts-J4TPRMJ3.js.map +7 -0
- package/dist/query-K3QKBVDN.js +50 -0
- package/dist/query-K3QKBVDN.js.map +7 -0
- package/dist/responsesStreaming-HMB74TRD.js +10 -0
- package/dist/responsesStreaming-HMB74TRD.js.map +7 -0
- package/dist/ripgrep-XJGSUBG7.js +17 -0
- package/dist/ripgrep-XJGSUBG7.js.map +7 -0
- package/dist/skillMarketplace-AUGKNCPW.js +37 -0
- package/dist/skillMarketplace-AUGKNCPW.js.map +7 -0
- package/dist/state-DQYRXKTG.js +16 -0
- package/dist/state-DQYRXKTG.js.map +7 -0
- package/dist/theme-MS5HDUBJ.js +14 -0
- package/dist/theme-MS5HDUBJ.js.map +7 -0
- package/dist/toolPermissionContext-GYD5LYFK.js +17 -0
- package/dist/toolPermissionContext-GYD5LYFK.js.map +7 -0
- package/dist/toolPermissionSettings-4MPZVYDR.js +18 -0
- package/dist/toolPermissionSettings-4MPZVYDR.js.map +7 -0
- package/dist/tools-QW6SIJLJ.js +47 -0
- package/dist/tools-QW6SIJLJ.js.map +7 -0
- package/dist/userInput-F2PGBRFU.js +311 -0
- package/dist/userInput-F2PGBRFU.js.map +7 -0
- package/dist/uuid-GYYCQ6QK.js +9 -0
- package/dist/uuid-GYYCQ6QK.js.map +7 -0
- package/dist/yoga.wasm +0 -0
- package/package.json +136 -0
- package/scripts/binary-utils.cjs +62 -0
- package/scripts/cli-acp-wrapper.cjs +82 -0
- package/scripts/cli-wrapper.cjs +105 -0
- package/scripts/postinstall.js +144 -0
- package/yoga.wasm +0 -0
|
@@ -0,0 +1,3118 @@
|
|
|
1
|
+
import { createRequire as __pybCreateRequire } from "node:module";
|
|
2
|
+
const require = __pybCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
getToolDescription
|
|
5
|
+
} from "./chunk-M624LT6O.js";
|
|
6
|
+
import {
|
|
7
|
+
processResponsesStream
|
|
8
|
+
} from "./chunk-OMELVAJD.js";
|
|
9
|
+
import {
|
|
10
|
+
formatSystemPromptWithContext,
|
|
11
|
+
generateKodeContext,
|
|
12
|
+
getCLISyspromptPrefix,
|
|
13
|
+
getReasoningEffort,
|
|
14
|
+
models_default,
|
|
15
|
+
refreshKodeContext
|
|
16
|
+
} from "./chunk-QBHEERCF.js";
|
|
17
|
+
import "./chunk-XXFY63TM.js";
|
|
18
|
+
import "./chunk-4XPNRLJG.js";
|
|
19
|
+
import "./chunk-QIHB5PYM.js";
|
|
20
|
+
import "./chunk-Q24ZGKIE.js";
|
|
21
|
+
import "./chunk-ZJGXEWKF.js";
|
|
22
|
+
import "./chunk-6WELHKDA.js";
|
|
23
|
+
import "./chunk-5P7HBXTD.js";
|
|
24
|
+
import {
|
|
25
|
+
setRequestStatus
|
|
26
|
+
} from "./chunk-3DFBSQIT.js";
|
|
27
|
+
import "./chunk-HIRIJ2LQ.js";
|
|
28
|
+
import "./chunk-T6GVXTNQ.js";
|
|
29
|
+
import "./chunk-4SNFQYCY.js";
|
|
30
|
+
import {
|
|
31
|
+
getCompletionWithProfile,
|
|
32
|
+
getGPT5CompletionWithProfile
|
|
33
|
+
} from "./chunk-WPTPPOYN.js";
|
|
34
|
+
import "./chunk-HB2P6645.js";
|
|
35
|
+
import "./chunk-3PDD7M4T.js";
|
|
36
|
+
import "./chunk-T7GPUZVK.js";
|
|
37
|
+
import "./chunk-6RZIUY5K.js";
|
|
38
|
+
import "./chunk-7AAE6EO2.js";
|
|
39
|
+
import "./chunk-UNNVICVU.js";
|
|
40
|
+
import "./chunk-UUNVJZWA.js";
|
|
41
|
+
import "./chunk-ERMQRV55.js";
|
|
42
|
+
import "./chunk-ICTEVBLN.js";
|
|
43
|
+
import "./chunk-A3BVXXA3.js";
|
|
44
|
+
import "./chunk-BJSWTHRM.js";
|
|
45
|
+
import {
|
|
46
|
+
API_ERROR_MESSAGE_PREFIX,
|
|
47
|
+
CREDIT_BALANCE_TOO_LOW_ERROR_MESSAGE,
|
|
48
|
+
INVALID_API_KEY_ERROR_MESSAGE,
|
|
49
|
+
MAIN_QUERY_TEMPERATURE,
|
|
50
|
+
NO_CONTENT_MESSAGE,
|
|
51
|
+
PROMPT_TOO_LONG_ERROR_MESSAGE,
|
|
52
|
+
createAssistantAPIErrorMessage,
|
|
53
|
+
normalizeContentFromAPI
|
|
54
|
+
} from "./chunk-SWYJOV5E.js";
|
|
55
|
+
import {
|
|
56
|
+
USE_BEDROCK,
|
|
57
|
+
USE_VERTEX,
|
|
58
|
+
getModelManager,
|
|
59
|
+
getVertexRegionForModel
|
|
60
|
+
} from "./chunk-Z3HMXDXP.js";
|
|
61
|
+
import "./chunk-3ZNSAB7B.js";
|
|
62
|
+
import "./chunk-BQA2EOUU.js";
|
|
63
|
+
import {
|
|
64
|
+
getAnthropicApiKey,
|
|
65
|
+
getGlobalConfig
|
|
66
|
+
} from "./chunk-JZDE77EH.js";
|
|
67
|
+
import "./chunk-RQVLBMP7.js";
|
|
68
|
+
import {
|
|
69
|
+
debug,
|
|
70
|
+
getCurrentRequest,
|
|
71
|
+
logErrorWithDiagnosis,
|
|
72
|
+
logLLMInteraction,
|
|
73
|
+
logSystemPromptConstruction,
|
|
74
|
+
markPhase
|
|
75
|
+
} from "./chunk-3KNGJX7Q.js";
|
|
76
|
+
import {
|
|
77
|
+
PRODUCT_COMMAND,
|
|
78
|
+
env,
|
|
79
|
+
getCwd,
|
|
80
|
+
logError
|
|
81
|
+
} from "./chunk-TXFCNQDE.js";
|
|
82
|
+
import {
|
|
83
|
+
MACRO
|
|
84
|
+
} from "./chunk-A6PUMROK.js";
|
|
85
|
+
import {
|
|
86
|
+
addToTotalCost
|
|
87
|
+
} from "./chunk-OUXHGDLH.js";
|
|
88
|
+
import "./chunk-BH3Y62E3.js";
|
|
89
|
+
|
|
90
|
+
// src/services/ai/llm.ts
|
|
91
|
+
import "@anthropic-ai/sdk/shims/node";
|
|
92
|
+
import Anthropic, { APIConnectionError, APIError } from "@anthropic-ai/sdk";
|
|
93
|
+
import { AnthropicBedrock } from "@anthropic-ai/bedrock-sdk";
|
|
94
|
+
import { AnthropicVertex } from "@anthropic-ai/vertex-sdk";
|
|
95
|
+
import chalk from "chalk";
|
|
96
|
+
import { randomUUID } from "crypto";
|
|
97
|
+
import "dotenv/config";
|
|
98
|
+
|
|
99
|
+
// src/utils/system/http.ts
|
|
100
|
+
var USER_AGENT = `${PRODUCT_COMMAND}/${MACRO.VERSION} (${process.env.USER_TYPE})`;
|
|
101
|
+
|
|
102
|
+
// src/services/system/vcr.ts
|
|
103
|
+
import { createHash } from "crypto";
|
|
104
|
+
import { mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
105
|
+
import { dirname } from "path";
|
|
106
|
+
import { existsSync } from "fs";
|
|
107
|
+
import * as path from "path";
|
|
108
|
+
import { mapValues } from "lodash-es";
|
|
109
|
+
async function withVCR(messages, f) {
|
|
110
|
+
if (process.env.NODE_ENV !== "test") {
|
|
111
|
+
return await f();
|
|
112
|
+
}
|
|
113
|
+
const dehydratedInput = mapMessages(
|
|
114
|
+
messages.map((_) => _.message.content),
|
|
115
|
+
dehydrateValue
|
|
116
|
+
);
|
|
117
|
+
const filename = `./fixtures/${dehydratedInput.map((_) => createHash("sha1").update(JSON.stringify(_)).digest("hex").slice(0, 6)).join("-")}.json`;
|
|
118
|
+
if (existsSync(filename)) {
|
|
119
|
+
const cached = JSON.parse(readFileSync(filename, "utf-8"));
|
|
120
|
+
return mapAssistantMessage(cached.output, hydrateValue);
|
|
121
|
+
}
|
|
122
|
+
if (env.isCI) {
|
|
123
|
+
process.stderr.write(
|
|
124
|
+
`Anthropic API fixture missing. Re-run bun test locally, then commit the result. ${JSON.stringify({ input: dehydratedInput }, null, 2)}
|
|
125
|
+
`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
const result = await f();
|
|
129
|
+
if (env.isCI) {
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
if (!existsSync(dirname(filename))) {
|
|
133
|
+
mkdirSync(dirname(filename), { recursive: true });
|
|
134
|
+
}
|
|
135
|
+
writeFileSync(
|
|
136
|
+
filename,
|
|
137
|
+
JSON.stringify(
|
|
138
|
+
{
|
|
139
|
+
input: dehydratedInput,
|
|
140
|
+
output: mapAssistantMessage(result, dehydrateValue)
|
|
141
|
+
},
|
|
142
|
+
null,
|
|
143
|
+
2
|
|
144
|
+
)
|
|
145
|
+
);
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
function mapMessages(messages, f) {
|
|
149
|
+
return messages.map((_) => {
|
|
150
|
+
if (typeof _ === "string") {
|
|
151
|
+
return f(_);
|
|
152
|
+
}
|
|
153
|
+
return _.map((_2) => {
|
|
154
|
+
switch (_2.type) {
|
|
155
|
+
case "tool_result":
|
|
156
|
+
if (typeof _2.content === "string") {
|
|
157
|
+
return { ..._2, content: f(_2.content) };
|
|
158
|
+
}
|
|
159
|
+
if (Array.isArray(_2.content)) {
|
|
160
|
+
return {
|
|
161
|
+
..._2,
|
|
162
|
+
content: _2.content.map((_3) => {
|
|
163
|
+
switch (_3.type) {
|
|
164
|
+
case "text":
|
|
165
|
+
return { ..._3, text: f(_3.text) };
|
|
166
|
+
case "image":
|
|
167
|
+
return _3;
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return _2;
|
|
173
|
+
case "text":
|
|
174
|
+
return { ..._2, text: f(_2.text) };
|
|
175
|
+
case "tool_use":
|
|
176
|
+
return {
|
|
177
|
+
..._2,
|
|
178
|
+
input: mapValues(_2.input, f)
|
|
179
|
+
};
|
|
180
|
+
case "image":
|
|
181
|
+
return _2;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function mapAssistantMessage(message, f) {
|
|
187
|
+
return {
|
|
188
|
+
durationMs: "DURATION",
|
|
189
|
+
costUSD: "COST",
|
|
190
|
+
uuid: "UUID",
|
|
191
|
+
message: {
|
|
192
|
+
...message.message,
|
|
193
|
+
content: message.message.content.map((_) => {
|
|
194
|
+
switch (_.type) {
|
|
195
|
+
case "text":
|
|
196
|
+
return {
|
|
197
|
+
..._,
|
|
198
|
+
text: f(_.text),
|
|
199
|
+
citations: _.citations || []
|
|
200
|
+
};
|
|
201
|
+
case "tool_use":
|
|
202
|
+
return {
|
|
203
|
+
..._,
|
|
204
|
+
input: mapValues(_.input, f)
|
|
205
|
+
};
|
|
206
|
+
default:
|
|
207
|
+
return _;
|
|
208
|
+
}
|
|
209
|
+
}).filter(Boolean)
|
|
210
|
+
},
|
|
211
|
+
type: "assistant"
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function dehydrateValue(s) {
|
|
215
|
+
if (typeof s !== "string") {
|
|
216
|
+
return s;
|
|
217
|
+
}
|
|
218
|
+
const s1 = s.replace(/num_files="\d+"/g, 'num_files="[NUM]"').replace(/duration_ms="\d+"/g, 'duration_ms="[DURATION]"').replace(/cost_usd="\d+"/g, 'cost_usd="[COST]"').replace(/\//g, path.sep).replaceAll(getCwd(), "[CWD]");
|
|
219
|
+
if (s1.includes("Files modified by user:")) {
|
|
220
|
+
return "Files modified by user: [FILES]";
|
|
221
|
+
}
|
|
222
|
+
return s1;
|
|
223
|
+
}
|
|
224
|
+
function hydrateValue(s) {
|
|
225
|
+
if (typeof s !== "string") {
|
|
226
|
+
return s;
|
|
227
|
+
}
|
|
228
|
+
return s.replaceAll("[NUM]", "1").replaceAll("[DURATION]", "100").replaceAll("[CWD]", getCwd());
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/services/ai/llm.ts
|
|
232
|
+
import { zodToJsonSchema as zodToJsonSchema4 } from "zod-to-json-schema";
|
|
233
|
+
|
|
234
|
+
// src/services/ai/adapters/base.ts
|
|
235
|
+
function normalizeTokens(apiResponse) {
|
|
236
|
+
if (!apiResponse || typeof apiResponse !== "object") {
|
|
237
|
+
return { input: 0, output: 0 };
|
|
238
|
+
}
|
|
239
|
+
const input = Number(
|
|
240
|
+
apiResponse.prompt_tokens ?? apiResponse.input_tokens ?? apiResponse.promptTokens
|
|
241
|
+
) || 0;
|
|
242
|
+
const output = Number(
|
|
243
|
+
apiResponse.completion_tokens ?? apiResponse.output_tokens ?? apiResponse.completionTokens
|
|
244
|
+
) || 0;
|
|
245
|
+
const total = Number(apiResponse.total_tokens ?? apiResponse.totalTokens) || void 0;
|
|
246
|
+
const reasoning = Number(apiResponse.reasoning_tokens ?? apiResponse.reasoningTokens) || void 0;
|
|
247
|
+
return {
|
|
248
|
+
input,
|
|
249
|
+
output,
|
|
250
|
+
total: total && total > 0 ? total : void 0,
|
|
251
|
+
reasoning: reasoning && reasoning > 0 ? reasoning : void 0
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
var ModelAPIAdapter = class {
|
|
255
|
+
constructor(capabilities, modelProfile) {
|
|
256
|
+
this.capabilities = capabilities;
|
|
257
|
+
this.modelProfile = modelProfile;
|
|
258
|
+
}
|
|
259
|
+
cumulativeUsage = { input: 0, output: 0 };
|
|
260
|
+
async *parseStreamingResponse(response, signal) {
|
|
261
|
+
return;
|
|
262
|
+
yield;
|
|
263
|
+
}
|
|
264
|
+
resetCumulativeUsage() {
|
|
265
|
+
this.cumulativeUsage = { input: 0, output: 0 };
|
|
266
|
+
}
|
|
267
|
+
updateCumulativeUsage(usage) {
|
|
268
|
+
this.cumulativeUsage.input += usage.input;
|
|
269
|
+
this.cumulativeUsage.output += usage.output;
|
|
270
|
+
if (usage.total) {
|
|
271
|
+
this.cumulativeUsage.total = (this.cumulativeUsage.total || 0) + usage.total;
|
|
272
|
+
}
|
|
273
|
+
if (usage.reasoning) {
|
|
274
|
+
this.cumulativeUsage.reasoning = (this.cumulativeUsage.reasoning || 0) + usage.reasoning;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
getMaxTokensParam() {
|
|
278
|
+
return this.capabilities.parameters.maxTokensField;
|
|
279
|
+
}
|
|
280
|
+
getTemperature() {
|
|
281
|
+
if (this.capabilities.parameters.temperatureMode === "fixed_one") {
|
|
282
|
+
return 1;
|
|
283
|
+
}
|
|
284
|
+
if (this.capabilities.parameters.temperatureMode === "restricted") {
|
|
285
|
+
return Math.min(1, 0.7);
|
|
286
|
+
}
|
|
287
|
+
return 0.7;
|
|
288
|
+
}
|
|
289
|
+
shouldIncludeReasoningEffort() {
|
|
290
|
+
return this.capabilities.parameters.supportsReasoningEffort;
|
|
291
|
+
}
|
|
292
|
+
shouldIncludeVerbosity() {
|
|
293
|
+
return this.capabilities.parameters.supportsVerbosity;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/services/ai/adapters/openaiAdapter.ts
|
|
298
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
299
|
+
var OpenAIAdapter = class extends ModelAPIAdapter {
|
|
300
|
+
constructor(capabilities, modelProfile) {
|
|
301
|
+
super(capabilities, modelProfile);
|
|
302
|
+
}
|
|
303
|
+
async parseResponse(response) {
|
|
304
|
+
if (response?.body instanceof ReadableStream) {
|
|
305
|
+
const { assistantMessage } = await this.parseStreamingOpenAIResponse(response);
|
|
306
|
+
return {
|
|
307
|
+
id: assistantMessage.responseId,
|
|
308
|
+
content: assistantMessage.message.content,
|
|
309
|
+
toolCalls: assistantMessage.message.content.filter((block) => block.type === "tool_use").map((block) => ({
|
|
310
|
+
id: block.id,
|
|
311
|
+
type: "function",
|
|
312
|
+
function: {
|
|
313
|
+
name: block.name,
|
|
314
|
+
arguments: JSON.stringify(block.input)
|
|
315
|
+
}
|
|
316
|
+
})),
|
|
317
|
+
usage: this.normalizeUsageForAdapter(assistantMessage.message.usage),
|
|
318
|
+
responseId: assistantMessage.responseId
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return this.parseNonStreamingResponse(response);
|
|
322
|
+
}
|
|
323
|
+
async *parseStreamingResponse(response) {
|
|
324
|
+
const reader = response.body.getReader();
|
|
325
|
+
const decoder = new TextDecoder();
|
|
326
|
+
let buffer = "";
|
|
327
|
+
let responseId = response.id || `openai_${Date.now()}`;
|
|
328
|
+
let hasStarted = false;
|
|
329
|
+
let accumulatedContent = "";
|
|
330
|
+
const reasoningContext = {
|
|
331
|
+
thinkOpen: false,
|
|
332
|
+
thinkClosed: false,
|
|
333
|
+
sawAnySummary: false,
|
|
334
|
+
pendingSummaryParagraph: false
|
|
335
|
+
};
|
|
336
|
+
try {
|
|
337
|
+
while (true) {
|
|
338
|
+
const { done, value } = await reader.read();
|
|
339
|
+
if (done) break;
|
|
340
|
+
buffer += decoder.decode(value, { stream: true });
|
|
341
|
+
const lines = buffer.split("\n");
|
|
342
|
+
buffer = lines.pop() || "";
|
|
343
|
+
for (const line of lines) {
|
|
344
|
+
if (line.trim()) {
|
|
345
|
+
const parsed = this.parseSSEChunk(line);
|
|
346
|
+
if (parsed) {
|
|
347
|
+
if (parsed.id) {
|
|
348
|
+
responseId = parsed.id;
|
|
349
|
+
}
|
|
350
|
+
yield* this.processStreamingChunk(
|
|
351
|
+
parsed,
|
|
352
|
+
responseId,
|
|
353
|
+
hasStarted,
|
|
354
|
+
accumulatedContent,
|
|
355
|
+
reasoningContext
|
|
356
|
+
);
|
|
357
|
+
const stateUpdate = this.updateStreamingState(
|
|
358
|
+
parsed,
|
|
359
|
+
accumulatedContent
|
|
360
|
+
);
|
|
361
|
+
if (stateUpdate.content) accumulatedContent = stateUpdate.content;
|
|
362
|
+
if (stateUpdate.hasStarted) hasStarted = true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
} catch (error) {
|
|
368
|
+
logError(error);
|
|
369
|
+
debug.warn("OPENAI_ADAPTER_STREAM_READ_ERROR", {
|
|
370
|
+
error: error instanceof Error ? error.message : String(error)
|
|
371
|
+
});
|
|
372
|
+
yield {
|
|
373
|
+
type: "error",
|
|
374
|
+
error: error instanceof Error ? error.message : String(error)
|
|
375
|
+
};
|
|
376
|
+
} finally {
|
|
377
|
+
reader.releaseLock();
|
|
378
|
+
}
|
|
379
|
+
const finalContent = accumulatedContent ? [{ type: "text", text: accumulatedContent, citations: [] }] : [{ type: "text", text: "", citations: [] }];
|
|
380
|
+
yield {
|
|
381
|
+
type: "message_stop",
|
|
382
|
+
message: {
|
|
383
|
+
id: responseId,
|
|
384
|
+
role: "assistant",
|
|
385
|
+
content: finalContent,
|
|
386
|
+
responseId
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
parseSSEChunk(line) {
|
|
391
|
+
if (line.startsWith("data: ")) {
|
|
392
|
+
const data = line.slice(6).trim();
|
|
393
|
+
if (data === "[DONE]") {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
if (data) {
|
|
397
|
+
try {
|
|
398
|
+
return JSON.parse(data);
|
|
399
|
+
} catch (error) {
|
|
400
|
+
logError(error);
|
|
401
|
+
debug.warn("OPENAI_ADAPTER_SSE_PARSE_ERROR", {
|
|
402
|
+
error: error instanceof Error ? error.message : String(error)
|
|
403
|
+
});
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
handleTextDelta(delta, responseId, hasStarted) {
|
|
411
|
+
const events = [];
|
|
412
|
+
if (!hasStarted && delta) {
|
|
413
|
+
events.push({
|
|
414
|
+
type: "message_start",
|
|
415
|
+
message: {
|
|
416
|
+
role: "assistant",
|
|
417
|
+
content: []
|
|
418
|
+
},
|
|
419
|
+
responseId
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (delta) {
|
|
423
|
+
events.push({
|
|
424
|
+
type: "text_delta",
|
|
425
|
+
delta,
|
|
426
|
+
responseId
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
return events;
|
|
430
|
+
}
|
|
431
|
+
normalizeUsageForAdapter(usage) {
|
|
432
|
+
if (!usage) {
|
|
433
|
+
return {
|
|
434
|
+
input_tokens: 0,
|
|
435
|
+
output_tokens: 0,
|
|
436
|
+
promptTokens: 0,
|
|
437
|
+
completionTokens: 0,
|
|
438
|
+
totalTokens: 0,
|
|
439
|
+
reasoningTokens: 0
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
const inputTokens = usage.input_tokens ?? usage.prompt_tokens ?? usage.promptTokens ?? 0;
|
|
443
|
+
const outputTokens = usage.output_tokens ?? usage.completion_tokens ?? usage.completionTokens ?? 0;
|
|
444
|
+
return {
|
|
445
|
+
...usage,
|
|
446
|
+
input_tokens: inputTokens,
|
|
447
|
+
output_tokens: outputTokens,
|
|
448
|
+
promptTokens: inputTokens,
|
|
449
|
+
completionTokens: outputTokens,
|
|
450
|
+
totalTokens: usage.totalTokens ?? inputTokens + outputTokens,
|
|
451
|
+
reasoningTokens: usage.reasoningTokens ?? 0
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
buildTools(tools) {
|
|
455
|
+
return tools.map((tool) => ({
|
|
456
|
+
type: "function",
|
|
457
|
+
function: {
|
|
458
|
+
name: tool.name,
|
|
459
|
+
description: getToolDescription(tool),
|
|
460
|
+
parameters: zodToJsonSchema(tool.inputSchema)
|
|
461
|
+
}
|
|
462
|
+
}));
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
// src/services/ai/adapters/responsesAPI.ts
|
|
467
|
+
import { zodToJsonSchema as zodToJsonSchema2 } from "zod-to-json-schema";
|
|
468
|
+
var ResponsesAPIAdapter = class extends OpenAIAdapter {
|
|
469
|
+
createRequest(params) {
|
|
470
|
+
const {
|
|
471
|
+
messages,
|
|
472
|
+
systemPrompt,
|
|
473
|
+
tools,
|
|
474
|
+
maxTokens,
|
|
475
|
+
reasoningEffort,
|
|
476
|
+
stopSequences
|
|
477
|
+
} = params;
|
|
478
|
+
const request = {
|
|
479
|
+
model: this.modelProfile.modelName,
|
|
480
|
+
input: this.convertMessagesToInput(messages),
|
|
481
|
+
instructions: this.buildInstructions(systemPrompt)
|
|
482
|
+
};
|
|
483
|
+
const maxTokensField = this.getMaxTokensParam();
|
|
484
|
+
request[maxTokensField] = maxTokens;
|
|
485
|
+
if (stopSequences && stopSequences.length > 0) {
|
|
486
|
+
request.stop = stopSequences;
|
|
487
|
+
}
|
|
488
|
+
request.stream = params.stream !== false && this.capabilities.streaming.supported;
|
|
489
|
+
const temperature = this.getTemperature();
|
|
490
|
+
if (temperature !== void 0) {
|
|
491
|
+
request.temperature = temperature;
|
|
492
|
+
}
|
|
493
|
+
const include = [];
|
|
494
|
+
if (this.capabilities.parameters.supportsReasoningEffort && (this.shouldIncludeReasoningEffort() || reasoningEffort)) {
|
|
495
|
+
include.push("reasoning.encrypted_content");
|
|
496
|
+
request.reasoning = {
|
|
497
|
+
effort: reasoningEffort || this.modelProfile.reasoningEffort || "medium"
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
if (this.capabilities.parameters.supportsVerbosity && this.shouldIncludeVerbosity()) {
|
|
501
|
+
let defaultVerbosity = "medium";
|
|
502
|
+
if (params.verbosity) {
|
|
503
|
+
defaultVerbosity = params.verbosity;
|
|
504
|
+
} else {
|
|
505
|
+
const modelNameLower = this.modelProfile.modelName.toLowerCase();
|
|
506
|
+
if (modelNameLower.includes("high")) {
|
|
507
|
+
defaultVerbosity = "high";
|
|
508
|
+
} else if (modelNameLower.includes("low")) {
|
|
509
|
+
defaultVerbosity = "low";
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
request.text = {
|
|
513
|
+
verbosity: defaultVerbosity
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
if (tools && tools.length > 0) {
|
|
517
|
+
request.tools = this.buildTools(tools);
|
|
518
|
+
}
|
|
519
|
+
request.tool_choice = "auto";
|
|
520
|
+
if (this.capabilities.toolCalling.supportsParallelCalls) {
|
|
521
|
+
request.parallel_tool_calls = true;
|
|
522
|
+
}
|
|
523
|
+
request.store = false;
|
|
524
|
+
if (params.previousResponseId && this.capabilities.stateManagement.supportsPreviousResponseId) {
|
|
525
|
+
request.previous_response_id = params.previousResponseId;
|
|
526
|
+
}
|
|
527
|
+
if (include.length > 0) {
|
|
528
|
+
request.include = include;
|
|
529
|
+
}
|
|
530
|
+
return request;
|
|
531
|
+
}
|
|
532
|
+
buildTools(tools) {
|
|
533
|
+
return tools.map((tool) => {
|
|
534
|
+
let parameters = tool.inputJSONSchema;
|
|
535
|
+
if (!parameters && tool.inputSchema) {
|
|
536
|
+
const isPlainObject = (obj) => {
|
|
537
|
+
return obj !== null && typeof obj === "object" && !Array.isArray(obj);
|
|
538
|
+
};
|
|
539
|
+
if (isPlainObject(tool.inputSchema) && ("type" in tool.inputSchema || "properties" in tool.inputSchema)) {
|
|
540
|
+
parameters = tool.inputSchema;
|
|
541
|
+
} else {
|
|
542
|
+
try {
|
|
543
|
+
parameters = zodToJsonSchema2(tool.inputSchema);
|
|
544
|
+
} catch (error) {
|
|
545
|
+
logError(error);
|
|
546
|
+
debug.warn("RESPONSES_API_TOOL_SCHEMA_CONVERSION_FAILED", {
|
|
547
|
+
toolName: tool.name,
|
|
548
|
+
error: error instanceof Error ? error.message : String(error)
|
|
549
|
+
});
|
|
550
|
+
parameters = { type: "object", properties: {} };
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return {
|
|
555
|
+
type: "function",
|
|
556
|
+
name: tool.name,
|
|
557
|
+
description: getToolDescription(tool),
|
|
558
|
+
parameters: parameters || { type: "object", properties: {} }
|
|
559
|
+
};
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
async parseResponse(response) {
|
|
563
|
+
if (response?.body instanceof ReadableStream) {
|
|
564
|
+
const { assistantMessage } = await processResponsesStream(
|
|
565
|
+
this.parseStreamingResponse(response),
|
|
566
|
+
Date.now(),
|
|
567
|
+
response.id ?? `resp_${Date.now()}`
|
|
568
|
+
);
|
|
569
|
+
const hasToolUseBlocks = assistantMessage.message.content.some(
|
|
570
|
+
(block) => block.type === "tool_use"
|
|
571
|
+
);
|
|
572
|
+
return {
|
|
573
|
+
id: assistantMessage.responseId,
|
|
574
|
+
content: assistantMessage.message.content,
|
|
575
|
+
toolCalls: hasToolUseBlocks ? [] : [],
|
|
576
|
+
usage: this.normalizeUsageForAdapter(assistantMessage.message.usage),
|
|
577
|
+
responseId: assistantMessage.responseId
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
return this.parseNonStreamingResponse(response);
|
|
581
|
+
}
|
|
582
|
+
parseNonStreamingResponse(response) {
|
|
583
|
+
let content = response.output_text || "";
|
|
584
|
+
let reasoningContent = "";
|
|
585
|
+
if (response.output && Array.isArray(response.output)) {
|
|
586
|
+
const messageItems = response.output.filter(
|
|
587
|
+
(item) => item.type === "message"
|
|
588
|
+
);
|
|
589
|
+
if (messageItems.length > 0) {
|
|
590
|
+
content = messageItems.map((item) => {
|
|
591
|
+
if (item.content && Array.isArray(item.content)) {
|
|
592
|
+
return item.content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
593
|
+
}
|
|
594
|
+
return item.content || "";
|
|
595
|
+
}).filter(Boolean).join("\n\n");
|
|
596
|
+
}
|
|
597
|
+
const reasoningItems = response.output.filter(
|
|
598
|
+
(item) => item.type === "reasoning"
|
|
599
|
+
);
|
|
600
|
+
if (reasoningItems.length > 0) {
|
|
601
|
+
reasoningContent = reasoningItems.map((item) => item.content || "").filter(Boolean).join("\n\n");
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
if (reasoningContent) {
|
|
605
|
+
const thinkBlock = `
|
|
606
|
+
|
|
607
|
+
${reasoningContent}
|
|
608
|
+
|
|
609
|
+
`;
|
|
610
|
+
content = thinkBlock + content;
|
|
611
|
+
}
|
|
612
|
+
const toolCalls = this.parseToolCalls(response);
|
|
613
|
+
const contentArray = content ? [{ type: "text", text: content, citations: [] }] : [{ type: "text", text: "", citations: [] }];
|
|
614
|
+
const promptTokens = response.usage?.input_tokens || 0;
|
|
615
|
+
const completionTokens = response.usage?.output_tokens || 0;
|
|
616
|
+
const totalTokens = response.usage?.total_tokens ?? promptTokens + completionTokens;
|
|
617
|
+
return {
|
|
618
|
+
id: response.id || `resp_${Date.now()}`,
|
|
619
|
+
content: contentArray,
|
|
620
|
+
toolCalls,
|
|
621
|
+
usage: {
|
|
622
|
+
promptTokens,
|
|
623
|
+
completionTokens,
|
|
624
|
+
reasoningTokens: response.usage?.output_tokens_details?.reasoning_tokens
|
|
625
|
+
},
|
|
626
|
+
responseId: response.id
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
async *processStreamingChunk(parsed, responseId, hasStarted, accumulatedContent, reasoningContext) {
|
|
630
|
+
if (parsed.type === "response.reasoning_summary_part.added") {
|
|
631
|
+
const partIndex = parsed.summary_index || 0;
|
|
632
|
+
if (!reasoningContext?.thinkingContent) {
|
|
633
|
+
reasoningContext.thinkingContent = "";
|
|
634
|
+
reasoningContext.currentPartIndex = -1;
|
|
635
|
+
}
|
|
636
|
+
reasoningContext.currentPartIndex = partIndex;
|
|
637
|
+
if (partIndex > 0 && reasoningContext.thinkingContent) {
|
|
638
|
+
reasoningContext.thinkingContent += "\n\n";
|
|
639
|
+
yield {
|
|
640
|
+
type: "text_delta",
|
|
641
|
+
delta: "\n\n",
|
|
642
|
+
responseId
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
if (parsed.type === "response.reasoning_summary_text.delta") {
|
|
648
|
+
const delta = parsed.delta || "";
|
|
649
|
+
if (delta && reasoningContext) {
|
|
650
|
+
reasoningContext.thinkingContent += delta;
|
|
651
|
+
yield {
|
|
652
|
+
type: "text_delta",
|
|
653
|
+
delta,
|
|
654
|
+
responseId
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
if (parsed.type === "response.reasoning_text.delta") {
|
|
660
|
+
const delta = parsed.delta || "";
|
|
661
|
+
if (delta && reasoningContext) {
|
|
662
|
+
reasoningContext.thinkingContent += delta;
|
|
663
|
+
yield {
|
|
664
|
+
type: "text_delta",
|
|
665
|
+
delta,
|
|
666
|
+
responseId
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
if (parsed.type === "response.output_text.delta") {
|
|
672
|
+
const delta = parsed.delta || "";
|
|
673
|
+
if (delta) {
|
|
674
|
+
const textEvents = this.handleTextDelta(delta, responseId, hasStarted);
|
|
675
|
+
for (const event of textEvents) {
|
|
676
|
+
yield event;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
if (parsed.type === "response.output_item.done") {
|
|
681
|
+
const item = parsed.item || {};
|
|
682
|
+
if (item.type === "function_call") {
|
|
683
|
+
const callId = item.call_id || item.id;
|
|
684
|
+
const name = item.name;
|
|
685
|
+
const args = item.arguments;
|
|
686
|
+
if (typeof callId === "string" && typeof name === "string" && typeof args === "string") {
|
|
687
|
+
yield {
|
|
688
|
+
type: "tool_request",
|
|
689
|
+
tool: {
|
|
690
|
+
id: callId,
|
|
691
|
+
name,
|
|
692
|
+
input: args
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
if (parsed.usage) {
|
|
699
|
+
const normalizedUsage = normalizeTokens(parsed.usage);
|
|
700
|
+
if (parsed.usage.output_tokens_details?.reasoning_tokens) {
|
|
701
|
+
normalizedUsage.reasoning = parsed.usage.output_tokens_details.reasoning_tokens;
|
|
702
|
+
}
|
|
703
|
+
yield {
|
|
704
|
+
type: "usage",
|
|
705
|
+
usage: normalizedUsage
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
updateStreamingState(parsed, accumulatedContent) {
|
|
710
|
+
const state = {};
|
|
711
|
+
if (parsed.type === "response.output_text.delta" && parsed.delta) {
|
|
712
|
+
state.content = accumulatedContent + parsed.delta;
|
|
713
|
+
state.hasStarted = true;
|
|
714
|
+
}
|
|
715
|
+
return state;
|
|
716
|
+
}
|
|
717
|
+
async parseStreamingOpenAIResponse(response) {
|
|
718
|
+
const { processResponsesStream: processResponsesStream2 } = await import("./responsesStreaming-HMB74TRD.js");
|
|
719
|
+
return await processResponsesStream2(
|
|
720
|
+
this.parseStreamingResponse(response),
|
|
721
|
+
Date.now(),
|
|
722
|
+
response.id ?? `resp_${Date.now()}`
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
normalizeUsageForAdapter(usage) {
|
|
726
|
+
const baseUsage = super.normalizeUsageForAdapter(usage);
|
|
727
|
+
return {
|
|
728
|
+
...baseUsage,
|
|
729
|
+
reasoningTokens: usage?.output_tokens_details?.reasoning_tokens ?? 0
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
convertMessagesToInput(messages) {
|
|
733
|
+
const inputItems = [];
|
|
734
|
+
for (const message of messages) {
|
|
735
|
+
const role = message.role;
|
|
736
|
+
if (role === "tool") {
|
|
737
|
+
const callId = message.tool_call_id || message.id;
|
|
738
|
+
if (typeof callId === "string" && callId) {
|
|
739
|
+
let content2 = message.content || "";
|
|
740
|
+
if (Array.isArray(content2)) {
|
|
741
|
+
const texts = [];
|
|
742
|
+
for (const part of content2) {
|
|
743
|
+
if (typeof part === "object" && part !== null) {
|
|
744
|
+
const t = part.text || part.content;
|
|
745
|
+
if (typeof t === "string" && t) {
|
|
746
|
+
texts.push(t);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
content2 = texts.join("\n");
|
|
751
|
+
}
|
|
752
|
+
if (typeof content2 === "string") {
|
|
753
|
+
inputItems.push({
|
|
754
|
+
type: "function_call_output",
|
|
755
|
+
call_id: callId,
|
|
756
|
+
output: content2
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
if (role === "assistant" && Array.isArray(message.tool_calls)) {
|
|
763
|
+
for (const tc of message.tool_calls) {
|
|
764
|
+
if (typeof tc !== "object" || tc === null) {
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
const tcType = tc.type || "function";
|
|
768
|
+
if (tcType !== "function") {
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
const callId = tc.id || tc.call_id;
|
|
772
|
+
const fn = tc.function;
|
|
773
|
+
const name = typeof fn === "object" && fn !== null ? fn.name : null;
|
|
774
|
+
const args = typeof fn === "object" && fn !== null ? fn.arguments : null;
|
|
775
|
+
if (typeof callId === "string" && typeof name === "string" && typeof args === "string") {
|
|
776
|
+
inputItems.push({
|
|
777
|
+
type: "function_call",
|
|
778
|
+
name,
|
|
779
|
+
arguments: args,
|
|
780
|
+
call_id: callId
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
const content = message.content || "";
|
|
787
|
+
const contentItems = [];
|
|
788
|
+
if (Array.isArray(content)) {
|
|
789
|
+
for (const part of content) {
|
|
790
|
+
if (typeof part !== "object" || part === null) continue;
|
|
791
|
+
const ptype = part.type;
|
|
792
|
+
if (ptype === "text") {
|
|
793
|
+
const text = part.text || part.content || "";
|
|
794
|
+
if (typeof text === "string" && text) {
|
|
795
|
+
const kind = role === "assistant" ? "output_text" : "input_text";
|
|
796
|
+
contentItems.push({ type: kind, text });
|
|
797
|
+
}
|
|
798
|
+
} else if (ptype === "image_url") {
|
|
799
|
+
const image = part.image_url;
|
|
800
|
+
const url = typeof image === "object" && image !== null ? image.url : image;
|
|
801
|
+
if (typeof url === "string" && url) {
|
|
802
|
+
contentItems.push({ type: "input_image", image_url: url });
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
} else if (typeof content === "string" && content) {
|
|
807
|
+
const kind = role === "assistant" ? "output_text" : "input_text";
|
|
808
|
+
contentItems.push({ type: kind, text: content });
|
|
809
|
+
}
|
|
810
|
+
if (contentItems.length) {
|
|
811
|
+
const roleOut = role === "assistant" ? "assistant" : "user";
|
|
812
|
+
inputItems.push({
|
|
813
|
+
type: "message",
|
|
814
|
+
role: roleOut,
|
|
815
|
+
content: contentItems
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
return inputItems;
|
|
820
|
+
}
|
|
821
|
+
buildInstructions(systemPrompt) {
|
|
822
|
+
const systemContent = systemPrompt.filter((content) => content.trim()).join("\n\n");
|
|
823
|
+
return systemContent;
|
|
824
|
+
}
|
|
825
|
+
parseToolCalls(response) {
|
|
826
|
+
if (!response.output || !Array.isArray(response.output)) {
|
|
827
|
+
return [];
|
|
828
|
+
}
|
|
829
|
+
const toolCalls = [];
|
|
830
|
+
for (const item of response.output) {
|
|
831
|
+
if (item.type === "function_call") {
|
|
832
|
+
const callId = item.call_id || item.id;
|
|
833
|
+
const name = item.name || "";
|
|
834
|
+
const args = item.arguments || "{}";
|
|
835
|
+
if (typeof callId === "string" && typeof name === "string" && typeof args === "string") {
|
|
836
|
+
toolCalls.push({
|
|
837
|
+
id: callId,
|
|
838
|
+
type: "function",
|
|
839
|
+
function: {
|
|
840
|
+
name,
|
|
841
|
+
arguments: args
|
|
842
|
+
}
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
} else if (item.type === "tool_call") {
|
|
846
|
+
const callId = item.id || `tool_${Math.random().toString(36).substring(2, 15)}`;
|
|
847
|
+
toolCalls.push({
|
|
848
|
+
id: callId,
|
|
849
|
+
type: "tool_call",
|
|
850
|
+
name: item.name,
|
|
851
|
+
arguments: item.arguments
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
return toolCalls;
|
|
856
|
+
}
|
|
857
|
+
applyReasoningToMessage(message, reasoningSummaryText, reasoningFullText) {
|
|
858
|
+
const rtxtParts = [];
|
|
859
|
+
if (typeof reasoningSummaryText === "string" && reasoningSummaryText.trim()) {
|
|
860
|
+
rtxtParts.push(reasoningSummaryText);
|
|
861
|
+
}
|
|
862
|
+
if (typeof reasoningFullText === "string" && reasoningFullText.trim()) {
|
|
863
|
+
rtxtParts.push(reasoningFullText);
|
|
864
|
+
}
|
|
865
|
+
const rtxt = rtxtParts.filter((p) => p).join("\n\n");
|
|
866
|
+
if (rtxt) {
|
|
867
|
+
const thinkBlock = `<think>
|
|
868
|
+
${rtxt}
|
|
869
|
+
</think>
|
|
870
|
+
`;
|
|
871
|
+
const contentText = message.content || "";
|
|
872
|
+
message.content = thinkBlock + (typeof contentText === "string" ? contentText : "");
|
|
873
|
+
}
|
|
874
|
+
return message;
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
// src/services/ai/adapters/chatCompletions.ts
|
|
879
|
+
import { zodToJsonSchema as zodToJsonSchema3 } from "zod-to-json-schema";
|
|
880
|
+
var ChatCompletionsAdapter = class extends OpenAIAdapter {
|
|
881
|
+
createRequest(params) {
|
|
882
|
+
const { messages, systemPrompt, tools, maxTokens, stream } = params;
|
|
883
|
+
const fullMessages = this.buildMessages(systemPrompt, messages);
|
|
884
|
+
const request = {
|
|
885
|
+
model: this.modelProfile.modelName,
|
|
886
|
+
messages: fullMessages,
|
|
887
|
+
[this.getMaxTokensParam()]: maxTokens,
|
|
888
|
+
temperature: this.getTemperature()
|
|
889
|
+
};
|
|
890
|
+
if (tools && tools.length > 0) {
|
|
891
|
+
request.tools = this.buildTools(tools);
|
|
892
|
+
request.tool_choice = "auto";
|
|
893
|
+
}
|
|
894
|
+
if (this.capabilities.parameters.supportsReasoningEffort && params.reasoningEffort) {
|
|
895
|
+
request.reasoning_effort = params.reasoningEffort;
|
|
896
|
+
}
|
|
897
|
+
if (this.capabilities.parameters.supportsVerbosity && params.verbosity) {
|
|
898
|
+
request.verbosity = params.verbosity;
|
|
899
|
+
}
|
|
900
|
+
if (stream && this.capabilities.streaming.supported) {
|
|
901
|
+
request.stream = true;
|
|
902
|
+
if (this.capabilities.streaming.includesUsage) {
|
|
903
|
+
request.stream_options = {
|
|
904
|
+
include_usage: true
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
if (this.capabilities.parameters.temperatureMode === "fixed_one") {
|
|
909
|
+
delete request.temperature;
|
|
910
|
+
}
|
|
911
|
+
if (!this.capabilities.streaming.supported) {
|
|
912
|
+
delete request.stream;
|
|
913
|
+
delete request.stream_options;
|
|
914
|
+
}
|
|
915
|
+
return request;
|
|
916
|
+
}
|
|
917
|
+
buildTools(tools) {
|
|
918
|
+
return tools.map((tool) => ({
|
|
919
|
+
type: "function",
|
|
920
|
+
function: {
|
|
921
|
+
name: tool.name,
|
|
922
|
+
description: getToolDescription(tool),
|
|
923
|
+
parameters: tool.inputJSONSchema || zodToJsonSchema3(tool.inputSchema)
|
|
924
|
+
}
|
|
925
|
+
}));
|
|
926
|
+
}
|
|
927
|
+
parseNonStreamingResponse(response) {
|
|
928
|
+
if (!response || typeof response !== "object") {
|
|
929
|
+
throw new Error("Invalid response: response must be an object");
|
|
930
|
+
}
|
|
931
|
+
const choice = response.choices?.[0];
|
|
932
|
+
if (!choice) {
|
|
933
|
+
throw new Error("Invalid response: no choices found in response");
|
|
934
|
+
}
|
|
935
|
+
const message = choice.message || {};
|
|
936
|
+
const content = typeof message.content === "string" ? message.content : "";
|
|
937
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
938
|
+
const usage = response.usage || {};
|
|
939
|
+
const promptTokens = Number(usage.prompt_tokens) || 0;
|
|
940
|
+
const completionTokens = Number(usage.completion_tokens) || 0;
|
|
941
|
+
return {
|
|
942
|
+
id: response.id || `chatcmpl_${Date.now()}`,
|
|
943
|
+
content,
|
|
944
|
+
toolCalls,
|
|
945
|
+
usage: {
|
|
946
|
+
promptTokens,
|
|
947
|
+
completionTokens
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
buildMessages(systemPrompt, messages) {
|
|
952
|
+
const systemMessages = systemPrompt.map((prompt) => ({
|
|
953
|
+
role: "system",
|
|
954
|
+
content: prompt
|
|
955
|
+
}));
|
|
956
|
+
const normalizedMessages = this.normalizeToolMessages(messages);
|
|
957
|
+
return [...systemMessages, ...normalizedMessages];
|
|
958
|
+
}
|
|
959
|
+
normalizeToolMessages(messages) {
|
|
960
|
+
if (!Array.isArray(messages)) {
|
|
961
|
+
return [];
|
|
962
|
+
}
|
|
963
|
+
return messages.map((msg) => {
|
|
964
|
+
if (!msg || typeof msg !== "object") {
|
|
965
|
+
return msg;
|
|
966
|
+
}
|
|
967
|
+
if (msg.role === "tool") {
|
|
968
|
+
if (Array.isArray(msg.content)) {
|
|
969
|
+
return {
|
|
970
|
+
...msg,
|
|
971
|
+
content: msg.content.map((c) => c?.text || "").filter(Boolean).join("\n\n") || "(empty content)"
|
|
972
|
+
};
|
|
973
|
+
} else if (typeof msg.content !== "string") {
|
|
974
|
+
return {
|
|
975
|
+
...msg,
|
|
976
|
+
content: msg.content === null || msg.content === void 0 ? "(empty content)" : JSON.stringify(msg.content)
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
return msg;
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
async *processStreamingChunk(parsed, responseId, hasStarted, accumulatedContent, reasoningContext) {
|
|
984
|
+
if (!parsed || typeof parsed !== "object") {
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
const choice = parsed.choices?.[0];
|
|
988
|
+
if (choice?.delta && typeof choice.delta === "object") {
|
|
989
|
+
const delta = typeof choice.delta.content === "string" ? choice.delta.content : "";
|
|
990
|
+
const reasoningDelta = typeof choice.delta.reasoning_content === "string" ? choice.delta.reasoning_content : "";
|
|
991
|
+
const fullDelta = delta + reasoningDelta;
|
|
992
|
+
if (fullDelta) {
|
|
993
|
+
const textEvents = this.handleTextDelta(
|
|
994
|
+
fullDelta,
|
|
995
|
+
responseId,
|
|
996
|
+
hasStarted
|
|
997
|
+
);
|
|
998
|
+
for (const event of textEvents) {
|
|
999
|
+
yield event;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (choice?.delta?.tool_calls && Array.isArray(choice.delta.tool_calls)) {
|
|
1004
|
+
for (const toolCall of choice.delta.tool_calls) {
|
|
1005
|
+
if (toolCall && typeof toolCall === "object") {
|
|
1006
|
+
yield {
|
|
1007
|
+
type: "tool_request",
|
|
1008
|
+
tool: {
|
|
1009
|
+
id: toolCall.id || `tool_${Date.now()}`,
|
|
1010
|
+
name: toolCall.function?.name || "unknown",
|
|
1011
|
+
input: toolCall.function?.arguments || "{}"
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
if (parsed.usage && typeof parsed.usage === "object") {
|
|
1018
|
+
const normalizedUsage = normalizeTokens(parsed.usage);
|
|
1019
|
+
this.updateCumulativeUsage(normalizedUsage);
|
|
1020
|
+
yield {
|
|
1021
|
+
type: "usage",
|
|
1022
|
+
usage: { ...this.cumulativeUsage }
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
updateStreamingState(parsed, accumulatedContent) {
|
|
1027
|
+
const state = {};
|
|
1028
|
+
const choice = parsed.choices?.[0];
|
|
1029
|
+
if (choice?.delta) {
|
|
1030
|
+
const delta = choice.delta.content || "";
|
|
1031
|
+
const reasoningDelta = choice.delta.reasoning_content || "";
|
|
1032
|
+
const fullDelta = delta + reasoningDelta;
|
|
1033
|
+
if (fullDelta) {
|
|
1034
|
+
state.content = accumulatedContent + fullDelta;
|
|
1035
|
+
state.hasStarted = true;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
return state;
|
|
1039
|
+
}
|
|
1040
|
+
async parseStreamingOpenAIResponse(response, signal) {
|
|
1041
|
+
const contentBlocks = [];
|
|
1042
|
+
const usage = {
|
|
1043
|
+
prompt_tokens: 0,
|
|
1044
|
+
completion_tokens: 0
|
|
1045
|
+
};
|
|
1046
|
+
let responseId = response.id || `chatcmpl_${Date.now()}`;
|
|
1047
|
+
const pendingToolCalls = [];
|
|
1048
|
+
let hasMarkedStreaming = false;
|
|
1049
|
+
try {
|
|
1050
|
+
this.resetCumulativeUsage();
|
|
1051
|
+
for await (const event of this.parseStreamingResponse(response)) {
|
|
1052
|
+
if (signal?.aborted) {
|
|
1053
|
+
throw new Error("Stream aborted by user");
|
|
1054
|
+
}
|
|
1055
|
+
if (event.type === "message_start") {
|
|
1056
|
+
responseId = event.responseId || responseId;
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
1059
|
+
if (event.type === "text_delta") {
|
|
1060
|
+
if (!hasMarkedStreaming) {
|
|
1061
|
+
setRequestStatus({ kind: "streaming" });
|
|
1062
|
+
hasMarkedStreaming = true;
|
|
1063
|
+
}
|
|
1064
|
+
const last = contentBlocks[contentBlocks.length - 1];
|
|
1065
|
+
if (!last || last.type !== "text") {
|
|
1066
|
+
contentBlocks.push({
|
|
1067
|
+
type: "text",
|
|
1068
|
+
text: event.delta,
|
|
1069
|
+
citations: []
|
|
1070
|
+
});
|
|
1071
|
+
} else {
|
|
1072
|
+
last.text += event.delta;
|
|
1073
|
+
}
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
if (event.type === "tool_request") {
|
|
1077
|
+
setRequestStatus({ kind: "tool", detail: event.tool?.name });
|
|
1078
|
+
pendingToolCalls.push(event.tool);
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
if (event.type === "usage") {
|
|
1082
|
+
usage.prompt_tokens = event.usage.input;
|
|
1083
|
+
usage.completion_tokens = event.usage.output;
|
|
1084
|
+
usage.totalTokens = event.usage.total ?? event.usage.input + event.usage.output;
|
|
1085
|
+
usage.promptTokens = event.usage.input;
|
|
1086
|
+
usage.completionTokens = event.usage.output;
|
|
1087
|
+
continue;
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
} catch (error) {
|
|
1091
|
+
if (signal?.aborted) {
|
|
1092
|
+
const assistantMessage2 = {
|
|
1093
|
+
type: "assistant",
|
|
1094
|
+
message: {
|
|
1095
|
+
role: "assistant",
|
|
1096
|
+
content: contentBlocks,
|
|
1097
|
+
usage: {
|
|
1098
|
+
input_tokens: usage.prompt_tokens ?? 0,
|
|
1099
|
+
output_tokens: usage.completion_tokens ?? 0,
|
|
1100
|
+
prompt_tokens: usage.prompt_tokens ?? 0,
|
|
1101
|
+
completion_tokens: usage.completion_tokens ?? 0,
|
|
1102
|
+
totalTokens: (usage.prompt_tokens || 0) + (usage.completion_tokens || 0)
|
|
1103
|
+
}
|
|
1104
|
+
},
|
|
1105
|
+
costUSD: 0,
|
|
1106
|
+
durationMs: Date.now() - Date.now(),
|
|
1107
|
+
uuid: `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
1108
|
+
responseId
|
|
1109
|
+
};
|
|
1110
|
+
return {
|
|
1111
|
+
assistantMessage: assistantMessage2,
|
|
1112
|
+
rawResponse: {
|
|
1113
|
+
id: responseId,
|
|
1114
|
+
content: contentBlocks,
|
|
1115
|
+
usage,
|
|
1116
|
+
aborted: true
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
throw error;
|
|
1121
|
+
}
|
|
1122
|
+
for (const toolCall of pendingToolCalls) {
|
|
1123
|
+
let toolArgs = {};
|
|
1124
|
+
try {
|
|
1125
|
+
toolArgs = toolCall.input ? JSON.parse(toolCall.input) : {};
|
|
1126
|
+
} catch {
|
|
1127
|
+
}
|
|
1128
|
+
contentBlocks.push({
|
|
1129
|
+
type: "tool_use",
|
|
1130
|
+
id: toolCall.id,
|
|
1131
|
+
name: toolCall.name,
|
|
1132
|
+
input: toolArgs
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
const assistantMessage = {
|
|
1136
|
+
type: "assistant",
|
|
1137
|
+
message: {
|
|
1138
|
+
role: "assistant",
|
|
1139
|
+
content: contentBlocks,
|
|
1140
|
+
usage: {
|
|
1141
|
+
input_tokens: usage.prompt_tokens ?? 0,
|
|
1142
|
+
output_tokens: usage.completion_tokens ?? 0,
|
|
1143
|
+
prompt_tokens: usage.prompt_tokens ?? 0,
|
|
1144
|
+
completion_tokens: usage.completion_tokens ?? 0,
|
|
1145
|
+
totalTokens: usage.totalTokens ?? (usage.prompt_tokens || 0) + (usage.completion_tokens || 0)
|
|
1146
|
+
}
|
|
1147
|
+
},
|
|
1148
|
+
costUSD: 0,
|
|
1149
|
+
durationMs: Date.now() - Date.now(),
|
|
1150
|
+
uuid: `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
|
|
1151
|
+
responseId
|
|
1152
|
+
};
|
|
1153
|
+
return {
|
|
1154
|
+
assistantMessage,
|
|
1155
|
+
rawResponse: {
|
|
1156
|
+
id: responseId,
|
|
1157
|
+
content: contentBlocks,
|
|
1158
|
+
usage
|
|
1159
|
+
}
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
normalizeUsageForAdapter(usage) {
|
|
1163
|
+
return super.normalizeUsageForAdapter(usage);
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
|
|
1167
|
+
// src/constants/modelCapabilities.ts
|
|
1168
|
+
var GPT5_CAPABILITIES = {
|
|
1169
|
+
apiArchitecture: {
|
|
1170
|
+
primary: "responses_api",
|
|
1171
|
+
fallback: "chat_completions"
|
|
1172
|
+
},
|
|
1173
|
+
parameters: {
|
|
1174
|
+
maxTokensField: "max_output_tokens",
|
|
1175
|
+
supportsReasoningEffort: true,
|
|
1176
|
+
supportsVerbosity: true,
|
|
1177
|
+
temperatureMode: "fixed_one"
|
|
1178
|
+
},
|
|
1179
|
+
toolCalling: {
|
|
1180
|
+
mode: "custom_tools",
|
|
1181
|
+
supportsFreeform: true,
|
|
1182
|
+
supportsAllowedTools: true,
|
|
1183
|
+
supportsParallelCalls: true
|
|
1184
|
+
},
|
|
1185
|
+
stateManagement: {
|
|
1186
|
+
supportsResponseId: true,
|
|
1187
|
+
supportsConversationChaining: true,
|
|
1188
|
+
supportsPreviousResponseId: true
|
|
1189
|
+
},
|
|
1190
|
+
streaming: {
|
|
1191
|
+
supported: true,
|
|
1192
|
+
includesUsage: true
|
|
1193
|
+
}
|
|
1194
|
+
};
|
|
1195
|
+
var CHAT_COMPLETIONS_CAPABILITIES = {
|
|
1196
|
+
apiArchitecture: {
|
|
1197
|
+
primary: "chat_completions"
|
|
1198
|
+
},
|
|
1199
|
+
parameters: {
|
|
1200
|
+
maxTokensField: "max_tokens",
|
|
1201
|
+
supportsReasoningEffort: false,
|
|
1202
|
+
supportsVerbosity: false,
|
|
1203
|
+
temperatureMode: "flexible"
|
|
1204
|
+
},
|
|
1205
|
+
toolCalling: {
|
|
1206
|
+
mode: "function_calling",
|
|
1207
|
+
supportsFreeform: false,
|
|
1208
|
+
supportsAllowedTools: false,
|
|
1209
|
+
supportsParallelCalls: true
|
|
1210
|
+
},
|
|
1211
|
+
stateManagement: {
|
|
1212
|
+
supportsResponseId: false,
|
|
1213
|
+
supportsConversationChaining: false,
|
|
1214
|
+
supportsPreviousResponseId: false
|
|
1215
|
+
},
|
|
1216
|
+
streaming: {
|
|
1217
|
+
supported: true,
|
|
1218
|
+
includesUsage: true
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
var MODEL_CAPABILITIES_REGISTRY = {
|
|
1222
|
+
"gpt-5": GPT5_CAPABILITIES,
|
|
1223
|
+
"gpt-5-mini": GPT5_CAPABILITIES,
|
|
1224
|
+
"gpt-5-nano": GPT5_CAPABILITIES,
|
|
1225
|
+
"gpt-5-chat-latest": GPT5_CAPABILITIES,
|
|
1226
|
+
"gpt-5-codex": GPT5_CAPABILITIES,
|
|
1227
|
+
"gpt-4o": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1228
|
+
"gpt-4o-mini": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1229
|
+
"gpt-4-turbo": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1230
|
+
"gpt-4": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1231
|
+
"claude-3-5-sonnet-20241022": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1232
|
+
"claude-3-5-haiku-20241022": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1233
|
+
"claude-3-opus-20240229": CHAT_COMPLETIONS_CAPABILITIES,
|
|
1234
|
+
o1: {
|
|
1235
|
+
...CHAT_COMPLETIONS_CAPABILITIES,
|
|
1236
|
+
parameters: {
|
|
1237
|
+
...CHAT_COMPLETIONS_CAPABILITIES.parameters,
|
|
1238
|
+
maxTokensField: "max_completion_tokens",
|
|
1239
|
+
temperatureMode: "fixed_one"
|
|
1240
|
+
}
|
|
1241
|
+
},
|
|
1242
|
+
"o1-mini": {
|
|
1243
|
+
...CHAT_COMPLETIONS_CAPABILITIES,
|
|
1244
|
+
parameters: {
|
|
1245
|
+
...CHAT_COMPLETIONS_CAPABILITIES.parameters,
|
|
1246
|
+
maxTokensField: "max_completion_tokens",
|
|
1247
|
+
temperatureMode: "fixed_one"
|
|
1248
|
+
}
|
|
1249
|
+
},
|
|
1250
|
+
"o1-preview": {
|
|
1251
|
+
...CHAT_COMPLETIONS_CAPABILITIES,
|
|
1252
|
+
parameters: {
|
|
1253
|
+
...CHAT_COMPLETIONS_CAPABILITIES.parameters,
|
|
1254
|
+
maxTokensField: "max_completion_tokens",
|
|
1255
|
+
temperatureMode: "fixed_one"
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
function inferModelCapabilities(modelName) {
|
|
1260
|
+
if (!modelName) return null;
|
|
1261
|
+
const lowerName = modelName.toLowerCase();
|
|
1262
|
+
if (lowerName.includes("gpt-5") || lowerName.includes("gpt5")) {
|
|
1263
|
+
return GPT5_CAPABILITIES;
|
|
1264
|
+
}
|
|
1265
|
+
if (lowerName.includes("gpt-6") || lowerName.includes("gpt6")) {
|
|
1266
|
+
return {
|
|
1267
|
+
...GPT5_CAPABILITIES,
|
|
1268
|
+
streaming: { supported: true, includesUsage: true }
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1271
|
+
if (lowerName.includes("glm-5") || lowerName.includes("glm5")) {
|
|
1272
|
+
return {
|
|
1273
|
+
...CHAT_COMPLETIONS_CAPABILITIES,
|
|
1274
|
+
toolCalling: {
|
|
1275
|
+
...CHAT_COMPLETIONS_CAPABILITIES.toolCalling,
|
|
1276
|
+
supportsAllowedTools: false
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
if (lowerName.startsWith("o1") || lowerName.includes("o1-")) {
|
|
1281
|
+
return {
|
|
1282
|
+
...CHAT_COMPLETIONS_CAPABILITIES,
|
|
1283
|
+
parameters: {
|
|
1284
|
+
...CHAT_COMPLETIONS_CAPABILITIES.parameters,
|
|
1285
|
+
maxTokensField: "max_completion_tokens",
|
|
1286
|
+
temperatureMode: "fixed_one"
|
|
1287
|
+
}
|
|
1288
|
+
};
|
|
1289
|
+
}
|
|
1290
|
+
return null;
|
|
1291
|
+
}
|
|
1292
|
+
var capabilityCache = /* @__PURE__ */ new Map();
|
|
1293
|
+
function getModelCapabilities(modelName) {
|
|
1294
|
+
if (capabilityCache.has(modelName)) {
|
|
1295
|
+
return capabilityCache.get(modelName);
|
|
1296
|
+
}
|
|
1297
|
+
if (MODEL_CAPABILITIES_REGISTRY[modelName]) {
|
|
1298
|
+
const capabilities = MODEL_CAPABILITIES_REGISTRY[modelName];
|
|
1299
|
+
capabilityCache.set(modelName, capabilities);
|
|
1300
|
+
return capabilities;
|
|
1301
|
+
}
|
|
1302
|
+
const inferred = inferModelCapabilities(modelName);
|
|
1303
|
+
if (inferred) {
|
|
1304
|
+
capabilityCache.set(modelName, inferred);
|
|
1305
|
+
return inferred;
|
|
1306
|
+
}
|
|
1307
|
+
const defaultCapabilities = CHAT_COMPLETIONS_CAPABILITIES;
|
|
1308
|
+
capabilityCache.set(modelName, defaultCapabilities);
|
|
1309
|
+
return defaultCapabilities;
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// src/services/ai/modelAdapterFactory.ts
|
|
1313
|
+
var ModelAdapterFactory = class {
|
|
1314
|
+
static createAdapter(modelProfile) {
|
|
1315
|
+
const capabilities = getModelCapabilities(modelProfile.modelName);
|
|
1316
|
+
const apiType = this.determineAPIType(modelProfile, capabilities);
|
|
1317
|
+
switch (apiType) {
|
|
1318
|
+
case "responses_api":
|
|
1319
|
+
return new ResponsesAPIAdapter(capabilities, modelProfile);
|
|
1320
|
+
case "chat_completions":
|
|
1321
|
+
default:
|
|
1322
|
+
return new ChatCompletionsAdapter(capabilities, modelProfile);
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
static determineAPIType(modelProfile, capabilities) {
|
|
1326
|
+
if (capabilities.apiArchitecture.primary !== "responses_api") {
|
|
1327
|
+
return "chat_completions";
|
|
1328
|
+
}
|
|
1329
|
+
const isOfficialOpenAI = !modelProfile.baseURL || modelProfile.baseURL.includes("api.openai.com");
|
|
1330
|
+
if (!isOfficialOpenAI) {
|
|
1331
|
+
if (capabilities.apiArchitecture.fallback === "chat_completions") {
|
|
1332
|
+
return capabilities.apiArchitecture.primary;
|
|
1333
|
+
}
|
|
1334
|
+
return capabilities.apiArchitecture.primary;
|
|
1335
|
+
}
|
|
1336
|
+
return capabilities.apiArchitecture.primary;
|
|
1337
|
+
}
|
|
1338
|
+
static shouldUseResponsesAPI(modelProfile) {
|
|
1339
|
+
const capabilities = getModelCapabilities(modelProfile.modelName);
|
|
1340
|
+
const apiType = this.determineAPIType(modelProfile, capabilities);
|
|
1341
|
+
return apiType === "responses_api";
|
|
1342
|
+
}
|
|
1343
|
+
};
|
|
1344
|
+
|
|
1345
|
+
// src/services/ai/responseStateManager.ts
|
|
1346
|
+
var ResponseStateManager = class {
|
|
1347
|
+
conversationStates = /* @__PURE__ */ new Map();
|
|
1348
|
+
CLEANUP_INTERVAL = 60 * 60 * 1e3;
|
|
1349
|
+
constructor() {
|
|
1350
|
+
setInterval(() => {
|
|
1351
|
+
this.cleanup();
|
|
1352
|
+
}, this.CLEANUP_INTERVAL);
|
|
1353
|
+
}
|
|
1354
|
+
setPreviousResponseId(conversationId, responseId) {
|
|
1355
|
+
this.conversationStates.set(conversationId, {
|
|
1356
|
+
previousResponseId: responseId,
|
|
1357
|
+
lastUpdate: Date.now()
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
getPreviousResponseId(conversationId) {
|
|
1361
|
+
const state = this.conversationStates.get(conversationId);
|
|
1362
|
+
if (state) {
|
|
1363
|
+
state.lastUpdate = Date.now();
|
|
1364
|
+
return state.previousResponseId;
|
|
1365
|
+
}
|
|
1366
|
+
return void 0;
|
|
1367
|
+
}
|
|
1368
|
+
clearConversation(conversationId) {
|
|
1369
|
+
this.conversationStates.delete(conversationId);
|
|
1370
|
+
}
|
|
1371
|
+
clearAll() {
|
|
1372
|
+
this.conversationStates.clear();
|
|
1373
|
+
}
|
|
1374
|
+
cleanup() {
|
|
1375
|
+
const now = Date.now();
|
|
1376
|
+
for (const [conversationId, state] of this.conversationStates.entries()) {
|
|
1377
|
+
if (now - state.lastUpdate > this.CLEANUP_INTERVAL) {
|
|
1378
|
+
this.conversationStates.delete(conversationId);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
getStateSize() {
|
|
1383
|
+
return this.conversationStates.size;
|
|
1384
|
+
}
|
|
1385
|
+
};
|
|
1386
|
+
var responseStateManager = new ResponseStateManager();
|
|
1387
|
+
function getConversationId(agentId, messageId) {
|
|
1388
|
+
return agentId || messageId || `conv_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
// src/services/ai/llm.ts
|
|
1392
|
+
import { nanoid } from "nanoid";
|
|
1393
|
+
|
|
1394
|
+
// src/utils/tooling/toolUsePartialJson.ts
|
|
1395
|
+
function tokenizePartialJson(input) {
|
|
1396
|
+
let index = 0;
|
|
1397
|
+
const tokens = [];
|
|
1398
|
+
while (index < input.length) {
|
|
1399
|
+
let ch = input[index];
|
|
1400
|
+
if (ch === "\\") {
|
|
1401
|
+
index++;
|
|
1402
|
+
continue;
|
|
1403
|
+
}
|
|
1404
|
+
if (ch === "{") {
|
|
1405
|
+
tokens.push({ type: "brace", value: "{" });
|
|
1406
|
+
index++;
|
|
1407
|
+
continue;
|
|
1408
|
+
}
|
|
1409
|
+
if (ch === "}") {
|
|
1410
|
+
tokens.push({ type: "brace", value: "}" });
|
|
1411
|
+
index++;
|
|
1412
|
+
continue;
|
|
1413
|
+
}
|
|
1414
|
+
if (ch === "[") {
|
|
1415
|
+
tokens.push({ type: "paren", value: "[" });
|
|
1416
|
+
index++;
|
|
1417
|
+
continue;
|
|
1418
|
+
}
|
|
1419
|
+
if (ch === "]") {
|
|
1420
|
+
tokens.push({ type: "paren", value: "]" });
|
|
1421
|
+
index++;
|
|
1422
|
+
continue;
|
|
1423
|
+
}
|
|
1424
|
+
if (ch === ":") {
|
|
1425
|
+
tokens.push({ type: "separator", value: ":" });
|
|
1426
|
+
index++;
|
|
1427
|
+
continue;
|
|
1428
|
+
}
|
|
1429
|
+
if (ch === ",") {
|
|
1430
|
+
tokens.push({ type: "delimiter", value: "," });
|
|
1431
|
+
index++;
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
if (ch === '"') {
|
|
1435
|
+
let value = "";
|
|
1436
|
+
let incomplete = false;
|
|
1437
|
+
ch = input[++index];
|
|
1438
|
+
while (ch !== '"') {
|
|
1439
|
+
if (index === input.length) {
|
|
1440
|
+
incomplete = true;
|
|
1441
|
+
break;
|
|
1442
|
+
}
|
|
1443
|
+
if (ch === "\\") {
|
|
1444
|
+
if (++index === input.length) {
|
|
1445
|
+
incomplete = true;
|
|
1446
|
+
break;
|
|
1447
|
+
}
|
|
1448
|
+
value += ch + input[index];
|
|
1449
|
+
ch = input[++index];
|
|
1450
|
+
} else {
|
|
1451
|
+
value += ch;
|
|
1452
|
+
ch = input[++index];
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
ch = input[++index];
|
|
1456
|
+
if (!incomplete) tokens.push({ type: "string", value });
|
|
1457
|
+
continue;
|
|
1458
|
+
}
|
|
1459
|
+
if (ch && /\s/.test(ch)) {
|
|
1460
|
+
index++;
|
|
1461
|
+
continue;
|
|
1462
|
+
}
|
|
1463
|
+
const digit = /[0-9]/;
|
|
1464
|
+
if (ch && digit.test(ch) || ch === "-" || ch === ".") {
|
|
1465
|
+
let value = "";
|
|
1466
|
+
if (ch === "-") {
|
|
1467
|
+
value += ch;
|
|
1468
|
+
ch = input[++index];
|
|
1469
|
+
}
|
|
1470
|
+
while (ch && digit.test(ch) || ch === ".") {
|
|
1471
|
+
value += ch;
|
|
1472
|
+
ch = input[++index];
|
|
1473
|
+
}
|
|
1474
|
+
tokens.push({ type: "number", value });
|
|
1475
|
+
continue;
|
|
1476
|
+
}
|
|
1477
|
+
const alpha = /[a-z]/i;
|
|
1478
|
+
if (ch && alpha.test(ch)) {
|
|
1479
|
+
let value = "";
|
|
1480
|
+
while (ch && alpha.test(ch)) {
|
|
1481
|
+
if (index === input.length) break;
|
|
1482
|
+
value += ch;
|
|
1483
|
+
ch = input[++index];
|
|
1484
|
+
}
|
|
1485
|
+
if (value === "true" || value === "false" || value === "null") {
|
|
1486
|
+
tokens.push({ type: "name", value });
|
|
1487
|
+
} else {
|
|
1488
|
+
index++;
|
|
1489
|
+
}
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
index++;
|
|
1493
|
+
}
|
|
1494
|
+
return tokens;
|
|
1495
|
+
}
|
|
1496
|
+
function trimTrailingIncompleteTokens(tokens) {
|
|
1497
|
+
if (tokens.length === 0) return tokens;
|
|
1498
|
+
const last = tokens[tokens.length - 1];
|
|
1499
|
+
if (last.type === "separator") {
|
|
1500
|
+
return trimTrailingIncompleteTokens(tokens.slice(0, -1));
|
|
1501
|
+
}
|
|
1502
|
+
if (last.type === "number") {
|
|
1503
|
+
const lastChar = last.value[last.value.length - 1];
|
|
1504
|
+
if (lastChar === "." || lastChar === "-") {
|
|
1505
|
+
return trimTrailingIncompleteTokens(tokens.slice(0, -1));
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
if (last.type === "string" || last.type === "number") {
|
|
1509
|
+
const previous = tokens[tokens.length - 2];
|
|
1510
|
+
if (previous?.type === "delimiter") {
|
|
1511
|
+
return trimTrailingIncompleteTokens(tokens.slice(0, -1));
|
|
1512
|
+
}
|
|
1513
|
+
if (previous?.type === "brace" && previous.value === "{") {
|
|
1514
|
+
return trimTrailingIncompleteTokens(tokens.slice(0, -1));
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
if (last.type === "delimiter") {
|
|
1518
|
+
return trimTrailingIncompleteTokens(tokens.slice(0, -1));
|
|
1519
|
+
}
|
|
1520
|
+
return tokens;
|
|
1521
|
+
}
|
|
1522
|
+
function closeOpenBrackets(tokens) {
|
|
1523
|
+
const missingClosers = [];
|
|
1524
|
+
for (const token of tokens) {
|
|
1525
|
+
if (token.type === "brace") {
|
|
1526
|
+
if (token.value === "{") missingClosers.push("}");
|
|
1527
|
+
else missingClosers.splice(missingClosers.lastIndexOf("}"), 1);
|
|
1528
|
+
continue;
|
|
1529
|
+
}
|
|
1530
|
+
if (token.type === "paren") {
|
|
1531
|
+
if (token.value === "[") missingClosers.push("]");
|
|
1532
|
+
else missingClosers.splice(missingClosers.lastIndexOf("]"), 1);
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
if (missingClosers.length > 0) {
|
|
1536
|
+
missingClosers.reverse();
|
|
1537
|
+
for (const closer of missingClosers) {
|
|
1538
|
+
if (closer === "}") tokens.push({ type: "brace", value: "}" });
|
|
1539
|
+
else tokens.push({ type: "paren", value: "]" });
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
return tokens;
|
|
1543
|
+
}
|
|
1544
|
+
function tokensToJson(tokens) {
|
|
1545
|
+
let out = "";
|
|
1546
|
+
for (const token of tokens) {
|
|
1547
|
+
if (token.type === "string") out += `"${token.value}"`;
|
|
1548
|
+
else out += token.value;
|
|
1549
|
+
}
|
|
1550
|
+
return out;
|
|
1551
|
+
}
|
|
1552
|
+
function parseToolUsePartialJson(input) {
|
|
1553
|
+
const tokens = tokenizePartialJson(input);
|
|
1554
|
+
const trimmed = trimTrailingIncompleteTokens(tokens);
|
|
1555
|
+
const completed = closeOpenBrackets(trimmed);
|
|
1556
|
+
return JSON.parse(tokensToJson(completed));
|
|
1557
|
+
}
|
|
1558
|
+
function parseToolUsePartialJsonOrThrow(input) {
|
|
1559
|
+
try {
|
|
1560
|
+
return parseToolUsePartialJson(input);
|
|
1561
|
+
} catch (error) {
|
|
1562
|
+
throw new Error(
|
|
1563
|
+
`Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt. Error: ${String(error)}. JSON: ${input}`
|
|
1564
|
+
);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// src/utils/model/openaiMessageConversion.ts
|
|
1569
|
+
function convertAnthropicMessagesToOpenAIMessages(messages) {
|
|
1570
|
+
const openaiMessages = [];
|
|
1571
|
+
const toolResults = {};
|
|
1572
|
+
for (const message of messages) {
|
|
1573
|
+
const blocks = [];
|
|
1574
|
+
if (typeof message.message.content === "string") {
|
|
1575
|
+
blocks.push({ type: "text", text: message.message.content });
|
|
1576
|
+
} else if (Array.isArray(message.message.content)) {
|
|
1577
|
+
blocks.push(...message.message.content);
|
|
1578
|
+
} else if (message.message.content) {
|
|
1579
|
+
blocks.push(message.message.content);
|
|
1580
|
+
}
|
|
1581
|
+
const role = message.message.role;
|
|
1582
|
+
const userContentParts = [];
|
|
1583
|
+
const assistantTextParts = [];
|
|
1584
|
+
const assistantToolCalls = [];
|
|
1585
|
+
for (const block of blocks) {
|
|
1586
|
+
if (block.type === "text") {
|
|
1587
|
+
const text = typeof block.text === "string" ? block.text : "";
|
|
1588
|
+
if (!text) continue;
|
|
1589
|
+
if (role === "user") {
|
|
1590
|
+
userContentParts.push({ type: "text", text });
|
|
1591
|
+
} else if (role === "assistant") {
|
|
1592
|
+
assistantTextParts.push(text);
|
|
1593
|
+
}
|
|
1594
|
+
continue;
|
|
1595
|
+
}
|
|
1596
|
+
if (block.type === "image" && role === "user") {
|
|
1597
|
+
const source = block.source;
|
|
1598
|
+
if (source?.type === "base64") {
|
|
1599
|
+
userContentParts.push({
|
|
1600
|
+
type: "image_url",
|
|
1601
|
+
image_url: {
|
|
1602
|
+
url: `data:${source.media_type};base64,${source.data}`
|
|
1603
|
+
}
|
|
1604
|
+
});
|
|
1605
|
+
} else if (source?.type === "url") {
|
|
1606
|
+
userContentParts.push({
|
|
1607
|
+
type: "image_url",
|
|
1608
|
+
image_url: { url: source.url }
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1611
|
+
continue;
|
|
1612
|
+
}
|
|
1613
|
+
if (block.type === "tool_use") {
|
|
1614
|
+
assistantToolCalls.push({
|
|
1615
|
+
type: "function",
|
|
1616
|
+
function: {
|
|
1617
|
+
name: block.name,
|
|
1618
|
+
arguments: JSON.stringify(block.input)
|
|
1619
|
+
},
|
|
1620
|
+
id: block.id
|
|
1621
|
+
});
|
|
1622
|
+
continue;
|
|
1623
|
+
}
|
|
1624
|
+
if (block.type === "tool_result") {
|
|
1625
|
+
const toolUseId = block.tool_use_id;
|
|
1626
|
+
const rawToolContent = block.content;
|
|
1627
|
+
const toolContent = typeof rawToolContent === "string" ? rawToolContent : JSON.stringify(rawToolContent);
|
|
1628
|
+
toolResults[toolUseId] = {
|
|
1629
|
+
role: "tool",
|
|
1630
|
+
content: toolContent,
|
|
1631
|
+
tool_call_id: toolUseId
|
|
1632
|
+
};
|
|
1633
|
+
continue;
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
if (role === "user") {
|
|
1637
|
+
if (userContentParts.length === 1 && userContentParts[0]?.type === "text") {
|
|
1638
|
+
openaiMessages.push({
|
|
1639
|
+
role: "user",
|
|
1640
|
+
content: userContentParts[0].text
|
|
1641
|
+
});
|
|
1642
|
+
} else if (userContentParts.length > 0) {
|
|
1643
|
+
openaiMessages.push({ role: "user", content: userContentParts });
|
|
1644
|
+
}
|
|
1645
|
+
continue;
|
|
1646
|
+
}
|
|
1647
|
+
if (role === "assistant") {
|
|
1648
|
+
const text = assistantTextParts.filter(Boolean).join("\n");
|
|
1649
|
+
if (assistantToolCalls.length > 0) {
|
|
1650
|
+
openaiMessages.push({
|
|
1651
|
+
role: "assistant",
|
|
1652
|
+
content: text ? text : void 0,
|
|
1653
|
+
tool_calls: assistantToolCalls
|
|
1654
|
+
});
|
|
1655
|
+
continue;
|
|
1656
|
+
}
|
|
1657
|
+
if (text) {
|
|
1658
|
+
openaiMessages.push({ role: "assistant", content: text });
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
const finalMessages = [];
|
|
1663
|
+
for (const message of openaiMessages) {
|
|
1664
|
+
finalMessages.push(message);
|
|
1665
|
+
if ("tool_calls" in message && message.tool_calls) {
|
|
1666
|
+
for (const toolCall of message.tool_calls) {
|
|
1667
|
+
if (toolResults[toolCall.id]) {
|
|
1668
|
+
finalMessages.push(toolResults[toolCall.id]);
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
return finalMessages;
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
// src/services/ai/llm.ts
|
|
1677
|
+
function isGPT5Model(modelName) {
|
|
1678
|
+
return modelName.startsWith("gpt-5");
|
|
1679
|
+
}
|
|
1680
|
+
var PROMPT_CACHING_ENABLED = !process.env.DISABLE_PROMPT_CACHING;
|
|
1681
|
+
var SONNET_COST_PER_MILLION_INPUT_TOKENS = 3;
|
|
1682
|
+
var SONNET_COST_PER_MILLION_OUTPUT_TOKENS = 15;
|
|
1683
|
+
var SONNET_COST_PER_MILLION_PROMPT_CACHE_WRITE_TOKENS = 3.75;
|
|
1684
|
+
var SONNET_COST_PER_MILLION_PROMPT_CACHE_READ_TOKENS = 0.3;
|
|
1685
|
+
var MAX_RETRIES = process.env.USER_TYPE === "SWE_BENCH" ? 100 : 10;
|
|
1686
|
+
var BASE_DELAY_MS = 500;
|
|
1687
|
+
function abortableDelay(delayMs, signal) {
|
|
1688
|
+
return new Promise((resolve, reject) => {
|
|
1689
|
+
if (signal?.aborted) {
|
|
1690
|
+
reject(new Error("Request was aborted"));
|
|
1691
|
+
return;
|
|
1692
|
+
}
|
|
1693
|
+
const timeoutId = setTimeout(() => {
|
|
1694
|
+
resolve();
|
|
1695
|
+
}, delayMs);
|
|
1696
|
+
if (signal) {
|
|
1697
|
+
const abortHandler = () => {
|
|
1698
|
+
clearTimeout(timeoutId);
|
|
1699
|
+
reject(new Error("Request was aborted"));
|
|
1700
|
+
};
|
|
1701
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
1702
|
+
}
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
function getRetryDelay(attempt, retryAfterHeader) {
|
|
1706
|
+
if (retryAfterHeader) {
|
|
1707
|
+
const seconds = parseInt(retryAfterHeader, 10);
|
|
1708
|
+
if (!isNaN(seconds)) {
|
|
1709
|
+
return seconds * 1e3;
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
return Math.min(BASE_DELAY_MS * Math.pow(2, attempt - 1), 32e3);
|
|
1713
|
+
}
|
|
1714
|
+
function shouldRetry(error) {
|
|
1715
|
+
if (error.message?.includes('"type":"overloaded_error"')) {
|
|
1716
|
+
return process.env.USER_TYPE === "SWE_BENCH";
|
|
1717
|
+
}
|
|
1718
|
+
const shouldRetryHeader = error.headers?.["x-should-retry"];
|
|
1719
|
+
if (shouldRetryHeader === "true") return true;
|
|
1720
|
+
if (shouldRetryHeader === "false") return false;
|
|
1721
|
+
if (error instanceof APIConnectionError) {
|
|
1722
|
+
return true;
|
|
1723
|
+
}
|
|
1724
|
+
if (!error.status) return false;
|
|
1725
|
+
if (error.status === 408) return true;
|
|
1726
|
+
if (error.status === 409) return true;
|
|
1727
|
+
if (error.status === 429) return true;
|
|
1728
|
+
if (error.status && error.status >= 500) return true;
|
|
1729
|
+
return false;
|
|
1730
|
+
}
|
|
1731
|
+
async function withRetry(operation, options = {}) {
|
|
1732
|
+
const maxRetries = options.maxRetries ?? MAX_RETRIES;
|
|
1733
|
+
let lastError;
|
|
1734
|
+
for (let attempt = 1; attempt <= maxRetries + 1; attempt++) {
|
|
1735
|
+
try {
|
|
1736
|
+
return await operation(attempt);
|
|
1737
|
+
} catch (error) {
|
|
1738
|
+
lastError = error;
|
|
1739
|
+
if (attempt > maxRetries || !(error instanceof APIError) || !shouldRetry(error)) {
|
|
1740
|
+
throw error;
|
|
1741
|
+
}
|
|
1742
|
+
if (options.signal?.aborted) {
|
|
1743
|
+
throw new Error("Request cancelled by user");
|
|
1744
|
+
}
|
|
1745
|
+
const retryAfter = error.headers?.["retry-after"] ?? null;
|
|
1746
|
+
const delayMs = getRetryDelay(attempt, retryAfter);
|
|
1747
|
+
debug.warn("LLM_API_RETRY", {
|
|
1748
|
+
name: error.name,
|
|
1749
|
+
message: error.message,
|
|
1750
|
+
status: error.status,
|
|
1751
|
+
attempt,
|
|
1752
|
+
maxRetries,
|
|
1753
|
+
delayMs
|
|
1754
|
+
});
|
|
1755
|
+
try {
|
|
1756
|
+
await abortableDelay(delayMs, options.signal);
|
|
1757
|
+
} catch (delayError) {
|
|
1758
|
+
if (delayError.message === "Request was aborted") {
|
|
1759
|
+
throw new Error("Request cancelled by user");
|
|
1760
|
+
}
|
|
1761
|
+
throw delayError;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
throw lastError;
|
|
1766
|
+
}
|
|
1767
|
+
async function fetchAnthropicModels(baseURL, apiKey) {
|
|
1768
|
+
try {
|
|
1769
|
+
const modelsURL = baseURL ? `${baseURL.replace(/\/+$/, "")}/v1/models` : "https://api.anthropic.com/v1/models";
|
|
1770
|
+
const response = await fetch(modelsURL, {
|
|
1771
|
+
method: "GET",
|
|
1772
|
+
headers: {
|
|
1773
|
+
"x-api-key": apiKey,
|
|
1774
|
+
"anthropic-version": "2023-06-01",
|
|
1775
|
+
"User-Agent": USER_AGENT
|
|
1776
|
+
}
|
|
1777
|
+
});
|
|
1778
|
+
if (!response.ok) {
|
|
1779
|
+
if (response.status === 401) {
|
|
1780
|
+
throw new Error(
|
|
1781
|
+
"Invalid API key. Please check your Anthropic API key and try again."
|
|
1782
|
+
);
|
|
1783
|
+
} else if (response.status === 403) {
|
|
1784
|
+
throw new Error(
|
|
1785
|
+
"API key does not have permission to access models. Please check your API key permissions."
|
|
1786
|
+
);
|
|
1787
|
+
} else if (response.status === 429) {
|
|
1788
|
+
throw new Error(
|
|
1789
|
+
"Too many requests. Please wait a moment and try again."
|
|
1790
|
+
);
|
|
1791
|
+
} else if (response.status >= 500) {
|
|
1792
|
+
throw new Error(
|
|
1793
|
+
"Anthropic service is temporarily unavailable. Please try again later."
|
|
1794
|
+
);
|
|
1795
|
+
} else {
|
|
1796
|
+
throw new Error(
|
|
1797
|
+
`Unable to connect to Anthropic API (${response.status}). Please check your internet connection and API key.`
|
|
1798
|
+
);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
const data = await response.json();
|
|
1802
|
+
return data.data || [];
|
|
1803
|
+
} catch (error) {
|
|
1804
|
+
if (error instanceof Error && error.message.includes("API key") || error instanceof Error && error.message.includes("Anthropic")) {
|
|
1805
|
+
throw error;
|
|
1806
|
+
}
|
|
1807
|
+
logError(error);
|
|
1808
|
+
debug.warn("ANTHROPIC_MODELS_FETCH_FAILED", {
|
|
1809
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1810
|
+
});
|
|
1811
|
+
throw new Error(
|
|
1812
|
+
"Unable to connect to Anthropic API. Please check your internet connection and try again."
|
|
1813
|
+
);
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
async function verifyApiKey(apiKey, baseURL, provider) {
|
|
1817
|
+
if (!apiKey) {
|
|
1818
|
+
return false;
|
|
1819
|
+
}
|
|
1820
|
+
if (provider && provider !== "anthropic") {
|
|
1821
|
+
try {
|
|
1822
|
+
const headers = {
|
|
1823
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1824
|
+
"Content-Type": "application/json"
|
|
1825
|
+
};
|
|
1826
|
+
if (!baseURL) {
|
|
1827
|
+
debug.warn("API_VERIFICATION_MISSING_BASE_URL", { provider });
|
|
1828
|
+
return false;
|
|
1829
|
+
}
|
|
1830
|
+
const modelsURL = `${baseURL.replace(/\/+$/, "")}/models`;
|
|
1831
|
+
const response = await fetch(modelsURL, {
|
|
1832
|
+
method: "GET",
|
|
1833
|
+
headers
|
|
1834
|
+
});
|
|
1835
|
+
return response.ok;
|
|
1836
|
+
} catch (error) {
|
|
1837
|
+
logError(error);
|
|
1838
|
+
debug.warn("API_VERIFICATION_FAILED", {
|
|
1839
|
+
provider,
|
|
1840
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1841
|
+
});
|
|
1842
|
+
return false;
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
const clientConfig = {
|
|
1846
|
+
apiKey,
|
|
1847
|
+
dangerouslyAllowBrowser: true,
|
|
1848
|
+
maxRetries: 3,
|
|
1849
|
+
defaultHeaders: {
|
|
1850
|
+
"User-Agent": USER_AGENT
|
|
1851
|
+
}
|
|
1852
|
+
};
|
|
1853
|
+
if (baseURL && (provider === "anthropic" || provider === "minimax-coding")) {
|
|
1854
|
+
clientConfig.baseURL = baseURL;
|
|
1855
|
+
}
|
|
1856
|
+
const anthropic = new Anthropic(clientConfig);
|
|
1857
|
+
try {
|
|
1858
|
+
await withRetry(
|
|
1859
|
+
async () => {
|
|
1860
|
+
const model = "claude-sonnet-4-20250514";
|
|
1861
|
+
const messages = [{ role: "user", content: "test" }];
|
|
1862
|
+
await anthropic.messages.create({
|
|
1863
|
+
model,
|
|
1864
|
+
max_tokens: 1e3,
|
|
1865
|
+
messages,
|
|
1866
|
+
temperature: 0
|
|
1867
|
+
});
|
|
1868
|
+
return true;
|
|
1869
|
+
},
|
|
1870
|
+
{ maxRetries: 2 }
|
|
1871
|
+
);
|
|
1872
|
+
return true;
|
|
1873
|
+
} catch (error) {
|
|
1874
|
+
logError(error);
|
|
1875
|
+
if (error instanceof Error && error.message.includes(
|
|
1876
|
+
'{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}'
|
|
1877
|
+
)) {
|
|
1878
|
+
return false;
|
|
1879
|
+
}
|
|
1880
|
+
throw error;
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
function convertAnthropicMessagesToOpenAIMessages2(messages) {
|
|
1884
|
+
return convertAnthropicMessagesToOpenAIMessages(messages);
|
|
1885
|
+
}
|
|
1886
|
+
function messageReducer(previous, item) {
|
|
1887
|
+
const reduce = (acc, delta) => {
|
|
1888
|
+
acc = { ...acc };
|
|
1889
|
+
for (const [key, value] of Object.entries(delta)) {
|
|
1890
|
+
if (acc[key] === void 0 || acc[key] === null) {
|
|
1891
|
+
acc[key] = value;
|
|
1892
|
+
if (Array.isArray(acc[key])) {
|
|
1893
|
+
for (const arr of acc[key]) {
|
|
1894
|
+
delete arr.index;
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
} else if (typeof acc[key] === "string" && typeof value === "string") {
|
|
1898
|
+
acc[key] += value;
|
|
1899
|
+
} else if (typeof acc[key] === "number" && typeof value === "number") {
|
|
1900
|
+
acc[key] = value;
|
|
1901
|
+
} else if (Array.isArray(acc[key]) && Array.isArray(value)) {
|
|
1902
|
+
const accArray = acc[key];
|
|
1903
|
+
for (let i = 0; i < value.length; i++) {
|
|
1904
|
+
const { index, ...chunkTool } = value[i];
|
|
1905
|
+
if (index - accArray.length > 1) {
|
|
1906
|
+
throw new Error(
|
|
1907
|
+
`Error: An array has an empty value when tool_calls are constructed. tool_calls: ${accArray}; tool: ${value}`
|
|
1908
|
+
);
|
|
1909
|
+
}
|
|
1910
|
+
accArray[index] = reduce(accArray[index], chunkTool);
|
|
1911
|
+
}
|
|
1912
|
+
} else if (typeof acc[key] === "object" && typeof value === "object") {
|
|
1913
|
+
acc[key] = reduce(acc[key], value);
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
return acc;
|
|
1917
|
+
};
|
|
1918
|
+
const choice = item.choices?.[0];
|
|
1919
|
+
if (!choice) {
|
|
1920
|
+
return previous;
|
|
1921
|
+
}
|
|
1922
|
+
return reduce(previous, choice.delta);
|
|
1923
|
+
}
|
|
1924
|
+
async function handleMessageStream(stream, signal) {
|
|
1925
|
+
const streamStartTime = Date.now();
|
|
1926
|
+
let ttftMs;
|
|
1927
|
+
let chunkCount = 0;
|
|
1928
|
+
let errorCount = 0;
|
|
1929
|
+
debug.api("OPENAI_STREAM_START", {
|
|
1930
|
+
streamStartTime: String(streamStartTime)
|
|
1931
|
+
});
|
|
1932
|
+
let message = {};
|
|
1933
|
+
let id, model, created, object, usage;
|
|
1934
|
+
try {
|
|
1935
|
+
for await (const chunk of stream) {
|
|
1936
|
+
if (signal?.aborted) {
|
|
1937
|
+
debug.flow("OPENAI_STREAM_ABORTED", {
|
|
1938
|
+
chunkCount,
|
|
1939
|
+
timestamp: Date.now()
|
|
1940
|
+
});
|
|
1941
|
+
throw new Error("Request was cancelled");
|
|
1942
|
+
}
|
|
1943
|
+
chunkCount++;
|
|
1944
|
+
try {
|
|
1945
|
+
if (!id) {
|
|
1946
|
+
id = chunk.id;
|
|
1947
|
+
debug.api("OPENAI_STREAM_ID_RECEIVED", {
|
|
1948
|
+
id,
|
|
1949
|
+
chunkNumber: String(chunkCount)
|
|
1950
|
+
});
|
|
1951
|
+
}
|
|
1952
|
+
if (!model) {
|
|
1953
|
+
model = chunk.model;
|
|
1954
|
+
debug.api("OPENAI_STREAM_MODEL_RECEIVED", {
|
|
1955
|
+
model,
|
|
1956
|
+
chunkNumber: String(chunkCount)
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1959
|
+
if (!created) {
|
|
1960
|
+
created = chunk.created;
|
|
1961
|
+
}
|
|
1962
|
+
if (!object) {
|
|
1963
|
+
object = chunk.object;
|
|
1964
|
+
}
|
|
1965
|
+
if (!usage) {
|
|
1966
|
+
usage = chunk.usage;
|
|
1967
|
+
}
|
|
1968
|
+
message = messageReducer(message, chunk);
|
|
1969
|
+
if (chunk?.choices?.[0]?.delta?.content) {
|
|
1970
|
+
if (!ttftMs) {
|
|
1971
|
+
ttftMs = Date.now() - streamStartTime;
|
|
1972
|
+
debug.api("OPENAI_STREAM_FIRST_TOKEN", {
|
|
1973
|
+
ttftMs: String(ttftMs),
|
|
1974
|
+
chunkNumber: String(chunkCount)
|
|
1975
|
+
});
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
} catch (chunkError) {
|
|
1979
|
+
errorCount++;
|
|
1980
|
+
debug.error("OPENAI_STREAM_CHUNK_ERROR", {
|
|
1981
|
+
chunkNumber: String(chunkCount),
|
|
1982
|
+
errorMessage: chunkError instanceof Error ? chunkError.message : String(chunkError),
|
|
1983
|
+
errorType: chunkError instanceof Error ? chunkError.constructor.name : typeof chunkError
|
|
1984
|
+
});
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
debug.api("OPENAI_STREAM_COMPLETE", {
|
|
1988
|
+
totalChunks: String(chunkCount),
|
|
1989
|
+
errorCount: String(errorCount),
|
|
1990
|
+
totalDuration: String(Date.now() - streamStartTime),
|
|
1991
|
+
ttftMs: String(ttftMs || 0),
|
|
1992
|
+
finalMessageId: id || "undefined"
|
|
1993
|
+
});
|
|
1994
|
+
} catch (streamError) {
|
|
1995
|
+
debug.error("OPENAI_STREAM_FATAL_ERROR", {
|
|
1996
|
+
totalChunks: String(chunkCount),
|
|
1997
|
+
errorCount: String(errorCount),
|
|
1998
|
+
errorMessage: streamError instanceof Error ? streamError.message : String(streamError),
|
|
1999
|
+
errorType: streamError instanceof Error ? streamError.constructor.name : typeof streamError
|
|
2000
|
+
});
|
|
2001
|
+
throw streamError;
|
|
2002
|
+
}
|
|
2003
|
+
return {
|
|
2004
|
+
id,
|
|
2005
|
+
created,
|
|
2006
|
+
model,
|
|
2007
|
+
object,
|
|
2008
|
+
choices: [
|
|
2009
|
+
{
|
|
2010
|
+
index: 0,
|
|
2011
|
+
message,
|
|
2012
|
+
finish_reason: "stop",
|
|
2013
|
+
logprobs: void 0
|
|
2014
|
+
}
|
|
2015
|
+
],
|
|
2016
|
+
usage
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
function convertOpenAIResponseToAnthropic(response, tools) {
|
|
2020
|
+
let contentBlocks = [];
|
|
2021
|
+
const message = response.choices?.[0]?.message;
|
|
2022
|
+
if (!message) {
|
|
2023
|
+
return {
|
|
2024
|
+
role: "assistant",
|
|
2025
|
+
content: [],
|
|
2026
|
+
stop_reason: response.choices?.[0]?.finish_reason,
|
|
2027
|
+
type: "message",
|
|
2028
|
+
usage: response.usage
|
|
2029
|
+
};
|
|
2030
|
+
}
|
|
2031
|
+
if (message?.tool_calls) {
|
|
2032
|
+
for (const toolCall of message.tool_calls) {
|
|
2033
|
+
const tool = toolCall.function;
|
|
2034
|
+
const toolName = tool?.name;
|
|
2035
|
+
let toolArgs = {};
|
|
2036
|
+
try {
|
|
2037
|
+
toolArgs = tool?.arguments ? JSON.parse(tool.arguments) : {};
|
|
2038
|
+
} catch (e) {
|
|
2039
|
+
}
|
|
2040
|
+
contentBlocks.push({
|
|
2041
|
+
type: "tool_use",
|
|
2042
|
+
input: toolArgs,
|
|
2043
|
+
name: toolName,
|
|
2044
|
+
id: toolCall.id?.length > 0 ? toolCall.id : nanoid()
|
|
2045
|
+
});
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
if (message.reasoning) {
|
|
2049
|
+
contentBlocks.push({
|
|
2050
|
+
type: "thinking",
|
|
2051
|
+
thinking: message.reasoning,
|
|
2052
|
+
signature: ""
|
|
2053
|
+
});
|
|
2054
|
+
}
|
|
2055
|
+
if (message.reasoning_content) {
|
|
2056
|
+
contentBlocks.push({
|
|
2057
|
+
type: "thinking",
|
|
2058
|
+
thinking: message.reasoning_content,
|
|
2059
|
+
signature: ""
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
if (message.content) {
|
|
2063
|
+
contentBlocks.push({
|
|
2064
|
+
type: "text",
|
|
2065
|
+
text: message?.content,
|
|
2066
|
+
citations: []
|
|
2067
|
+
});
|
|
2068
|
+
}
|
|
2069
|
+
const finalMessage = {
|
|
2070
|
+
role: "assistant",
|
|
2071
|
+
content: contentBlocks,
|
|
2072
|
+
stop_reason: response.choices?.[0]?.finish_reason,
|
|
2073
|
+
type: "message",
|
|
2074
|
+
usage: response.usage
|
|
2075
|
+
};
|
|
2076
|
+
return finalMessage;
|
|
2077
|
+
}
|
|
2078
|
+
var anthropicClient = null;
|
|
2079
|
+
function getAnthropicClient(model) {
|
|
2080
|
+
const config = getGlobalConfig();
|
|
2081
|
+
const provider = config.primaryProvider;
|
|
2082
|
+
if (anthropicClient && provider) {
|
|
2083
|
+
anthropicClient = null;
|
|
2084
|
+
}
|
|
2085
|
+
if (anthropicClient) {
|
|
2086
|
+
return anthropicClient;
|
|
2087
|
+
}
|
|
2088
|
+
const region = getVertexRegionForModel(model);
|
|
2089
|
+
const modelManager = getModelManager();
|
|
2090
|
+
const modelProfile = modelManager.getModel("main");
|
|
2091
|
+
const defaultHeaders = {
|
|
2092
|
+
"x-app": "cli",
|
|
2093
|
+
"User-Agent": USER_AGENT
|
|
2094
|
+
};
|
|
2095
|
+
if (process.env.ANTHROPIC_AUTH_TOKEN) {
|
|
2096
|
+
defaultHeaders["Authorization"] = `Bearer ${process.env.ANTHROPIC_AUTH_TOKEN}`;
|
|
2097
|
+
}
|
|
2098
|
+
const ARGS = {
|
|
2099
|
+
defaultHeaders,
|
|
2100
|
+
maxRetries: 0,
|
|
2101
|
+
timeout: parseInt(process.env.API_TIMEOUT_MS || String(60 * 1e3), 10)
|
|
2102
|
+
};
|
|
2103
|
+
if (USE_BEDROCK) {
|
|
2104
|
+
const client = new AnthropicBedrock(ARGS);
|
|
2105
|
+
anthropicClient = client;
|
|
2106
|
+
return client;
|
|
2107
|
+
}
|
|
2108
|
+
if (USE_VERTEX) {
|
|
2109
|
+
const vertexArgs = {
|
|
2110
|
+
...ARGS,
|
|
2111
|
+
region: region || process.env.CLOUD_ML_REGION || "us-east5"
|
|
2112
|
+
};
|
|
2113
|
+
const client = new AnthropicVertex(vertexArgs);
|
|
2114
|
+
anthropicClient = client;
|
|
2115
|
+
return client;
|
|
2116
|
+
}
|
|
2117
|
+
let apiKey;
|
|
2118
|
+
let baseURL;
|
|
2119
|
+
if (modelProfile) {
|
|
2120
|
+
apiKey = modelProfile.apiKey || "";
|
|
2121
|
+
baseURL = modelProfile.baseURL;
|
|
2122
|
+
} else {
|
|
2123
|
+
apiKey = getAnthropicApiKey();
|
|
2124
|
+
baseURL = void 0;
|
|
2125
|
+
}
|
|
2126
|
+
if (process.env.USER_TYPE === "ant" && !apiKey && provider === "anthropic") {
|
|
2127
|
+
console.error(
|
|
2128
|
+
chalk.red(
|
|
2129
|
+
"[ANT-ONLY] Missing API key. Configure an API key in your model profile or environment variables."
|
|
2130
|
+
)
|
|
2131
|
+
);
|
|
2132
|
+
}
|
|
2133
|
+
const clientConfig = {
|
|
2134
|
+
apiKey,
|
|
2135
|
+
dangerouslyAllowBrowser: true,
|
|
2136
|
+
...ARGS,
|
|
2137
|
+
...baseURL && { baseURL }
|
|
2138
|
+
};
|
|
2139
|
+
anthropicClient = new Anthropic(clientConfig);
|
|
2140
|
+
return anthropicClient;
|
|
2141
|
+
}
|
|
2142
|
+
function resetAnthropicClient() {
|
|
2143
|
+
anthropicClient = null;
|
|
2144
|
+
}
|
|
2145
|
+
function applyCacheControlWithLimits(systemBlocks, messageParams) {
|
|
2146
|
+
if (!PROMPT_CACHING_ENABLED) {
|
|
2147
|
+
return { systemBlocks, messageParams };
|
|
2148
|
+
}
|
|
2149
|
+
const maxCacheBlocks = 4;
|
|
2150
|
+
let usedCacheBlocks = 0;
|
|
2151
|
+
const processedSystemBlocks = systemBlocks.map((block, index) => {
|
|
2152
|
+
if (usedCacheBlocks < maxCacheBlocks && block.text.length > 1e3) {
|
|
2153
|
+
usedCacheBlocks++;
|
|
2154
|
+
return {
|
|
2155
|
+
...block,
|
|
2156
|
+
cache_control: { type: "ephemeral" }
|
|
2157
|
+
};
|
|
2158
|
+
}
|
|
2159
|
+
const { cache_control, ...blockWithoutCache } = block;
|
|
2160
|
+
return blockWithoutCache;
|
|
2161
|
+
});
|
|
2162
|
+
const processedMessageParams = messageParams.map((message, messageIndex) => {
|
|
2163
|
+
if (Array.isArray(message.content)) {
|
|
2164
|
+
const processedContent = message.content.map(
|
|
2165
|
+
(contentBlock, blockIndex) => {
|
|
2166
|
+
const shouldCache = usedCacheBlocks < maxCacheBlocks && contentBlock.type === "text" && typeof contentBlock.text === "string" && (contentBlock.text.length > 2e3 || messageIndex === messageParams.length - 1 && blockIndex === message.content.length - 1 && contentBlock.text.length > 500);
|
|
2167
|
+
if (shouldCache) {
|
|
2168
|
+
usedCacheBlocks++;
|
|
2169
|
+
return {
|
|
2170
|
+
...contentBlock,
|
|
2171
|
+
cache_control: { type: "ephemeral" }
|
|
2172
|
+
};
|
|
2173
|
+
}
|
|
2174
|
+
const { cache_control, ...blockWithoutCache } = contentBlock;
|
|
2175
|
+
return blockWithoutCache;
|
|
2176
|
+
}
|
|
2177
|
+
);
|
|
2178
|
+
return {
|
|
2179
|
+
...message,
|
|
2180
|
+
content: processedContent
|
|
2181
|
+
};
|
|
2182
|
+
}
|
|
2183
|
+
return message;
|
|
2184
|
+
});
|
|
2185
|
+
return {
|
|
2186
|
+
systemBlocks: processedSystemBlocks,
|
|
2187
|
+
messageParams: processedMessageParams
|
|
2188
|
+
};
|
|
2189
|
+
}
|
|
2190
|
+
function userMessageToMessageParam(message, addCache = false) {
|
|
2191
|
+
if (addCache) {
|
|
2192
|
+
if (typeof message.message.content === "string") {
|
|
2193
|
+
return {
|
|
2194
|
+
role: "user",
|
|
2195
|
+
content: [
|
|
2196
|
+
{
|
|
2197
|
+
type: "text",
|
|
2198
|
+
text: message.message.content
|
|
2199
|
+
}
|
|
2200
|
+
]
|
|
2201
|
+
};
|
|
2202
|
+
} else {
|
|
2203
|
+
return {
|
|
2204
|
+
role: "user",
|
|
2205
|
+
content: message.message.content.map((_) => ({ ..._ }))
|
|
2206
|
+
};
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
return {
|
|
2210
|
+
role: "user",
|
|
2211
|
+
content: message.message.content
|
|
2212
|
+
};
|
|
2213
|
+
}
|
|
2214
|
+
function assistantMessageToMessageParam(message, addCache = false) {
|
|
2215
|
+
if (addCache) {
|
|
2216
|
+
if (typeof message.message.content === "string") {
|
|
2217
|
+
return {
|
|
2218
|
+
role: "assistant",
|
|
2219
|
+
content: [
|
|
2220
|
+
{
|
|
2221
|
+
type: "text",
|
|
2222
|
+
text: message.message.content
|
|
2223
|
+
}
|
|
2224
|
+
]
|
|
2225
|
+
};
|
|
2226
|
+
} else {
|
|
2227
|
+
return {
|
|
2228
|
+
role: "assistant",
|
|
2229
|
+
content: message.message.content.map((_) => ({ ..._ }))
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
}
|
|
2233
|
+
return {
|
|
2234
|
+
role: "assistant",
|
|
2235
|
+
content: message.message.content
|
|
2236
|
+
};
|
|
2237
|
+
}
|
|
2238
|
+
function splitSysPromptPrefix(systemPrompt) {
|
|
2239
|
+
const systemPromptFirstBlock = systemPrompt[0] || "";
|
|
2240
|
+
const systemPromptRest = systemPrompt.slice(1);
|
|
2241
|
+
return [systemPromptFirstBlock, systemPromptRest.join("\n")].filter(Boolean);
|
|
2242
|
+
}
|
|
2243
|
+
async function queryLLM(messages, systemPrompt, maxThinkingTokens, tools, signal, options) {
|
|
2244
|
+
const modelManager = options.__testModelManager ?? getModelManager();
|
|
2245
|
+
const modelResolution = modelManager.resolveModelWithInfo(options.model);
|
|
2246
|
+
if (!modelResolution.success || !modelResolution.profile) {
|
|
2247
|
+
const fallbackProfile = modelManager.resolveModel(options.model);
|
|
2248
|
+
if (!fallbackProfile) {
|
|
2249
|
+
throw new Error(
|
|
2250
|
+
modelResolution.error || `Failed to resolve model: ${options.model}`
|
|
2251
|
+
);
|
|
2252
|
+
}
|
|
2253
|
+
debug.warn("MODEL_RESOLUTION_FALLBACK", {
|
|
2254
|
+
inputParam: options.model,
|
|
2255
|
+
error: modelResolution.error,
|
|
2256
|
+
fallbackModelName: fallbackProfile.modelName,
|
|
2257
|
+
fallbackProvider: fallbackProfile.provider,
|
|
2258
|
+
requestId: getCurrentRequest()?.id
|
|
2259
|
+
});
|
|
2260
|
+
modelResolution.success = true;
|
|
2261
|
+
modelResolution.profile = fallbackProfile;
|
|
2262
|
+
}
|
|
2263
|
+
const modelProfile = modelResolution.profile;
|
|
2264
|
+
const resolvedModel = modelProfile.modelName;
|
|
2265
|
+
const toolUseContext = options.toolUseContext;
|
|
2266
|
+
if (toolUseContext && !toolUseContext.responseState) {
|
|
2267
|
+
const conversationId = getConversationId(
|
|
2268
|
+
toolUseContext.agentId,
|
|
2269
|
+
toolUseContext.messageId
|
|
2270
|
+
);
|
|
2271
|
+
const previousResponseId = responseStateManager.getPreviousResponseId(conversationId);
|
|
2272
|
+
toolUseContext.responseState = {
|
|
2273
|
+
previousResponseId,
|
|
2274
|
+
conversationId
|
|
2275
|
+
};
|
|
2276
|
+
}
|
|
2277
|
+
debug.api("MODEL_RESOLVED", {
|
|
2278
|
+
inputParam: options.model,
|
|
2279
|
+
resolvedModelName: resolvedModel,
|
|
2280
|
+
provider: modelProfile.provider,
|
|
2281
|
+
isPointer: ["main", "task", "compact", "quick"].includes(options.model),
|
|
2282
|
+
hasResponseState: !!toolUseContext?.responseState,
|
|
2283
|
+
conversationId: toolUseContext?.responseState?.conversationId,
|
|
2284
|
+
requestId: getCurrentRequest()?.id
|
|
2285
|
+
});
|
|
2286
|
+
const currentRequest = getCurrentRequest();
|
|
2287
|
+
debug.api("LLM_REQUEST_START", {
|
|
2288
|
+
messageCount: messages.length,
|
|
2289
|
+
systemPromptLength: systemPrompt.join(" ").length,
|
|
2290
|
+
toolCount: tools.length,
|
|
2291
|
+
model: resolvedModel,
|
|
2292
|
+
originalModelParam: options.model,
|
|
2293
|
+
requestId: getCurrentRequest()?.id
|
|
2294
|
+
});
|
|
2295
|
+
markPhase("LLM_CALL");
|
|
2296
|
+
try {
|
|
2297
|
+
const queryFn = options.__testQueryLLMWithPromptCaching ?? queryLLMWithPromptCaching;
|
|
2298
|
+
const cleanOptions = { ...options };
|
|
2299
|
+
delete cleanOptions.__testModelManager;
|
|
2300
|
+
delete cleanOptions.__testQueryLLMWithPromptCaching;
|
|
2301
|
+
const runQuery = () => queryFn(
|
|
2302
|
+
messages,
|
|
2303
|
+
systemPrompt,
|
|
2304
|
+
maxThinkingTokens,
|
|
2305
|
+
tools,
|
|
2306
|
+
signal,
|
|
2307
|
+
{
|
|
2308
|
+
...cleanOptions,
|
|
2309
|
+
model: resolvedModel,
|
|
2310
|
+
modelProfile,
|
|
2311
|
+
toolUseContext
|
|
2312
|
+
}
|
|
2313
|
+
);
|
|
2314
|
+
const result = options.__testQueryLLMWithPromptCaching ? await runQuery() : await withVCR(messages, runQuery);
|
|
2315
|
+
debug.api("LLM_REQUEST_SUCCESS", {
|
|
2316
|
+
costUSD: result.costUSD,
|
|
2317
|
+
durationMs: result.durationMs,
|
|
2318
|
+
responseLength: result.message.content?.length || 0,
|
|
2319
|
+
requestId: getCurrentRequest()?.id
|
|
2320
|
+
});
|
|
2321
|
+
if (toolUseContext?.responseState?.conversationId && result.responseId) {
|
|
2322
|
+
responseStateManager.setPreviousResponseId(
|
|
2323
|
+
toolUseContext.responseState.conversationId,
|
|
2324
|
+
result.responseId
|
|
2325
|
+
);
|
|
2326
|
+
debug.api("RESPONSE_STATE_UPDATED", {
|
|
2327
|
+
conversationId: toolUseContext.responseState.conversationId,
|
|
2328
|
+
responseId: result.responseId,
|
|
2329
|
+
requestId: getCurrentRequest()?.id
|
|
2330
|
+
});
|
|
2331
|
+
}
|
|
2332
|
+
return result;
|
|
2333
|
+
} catch (error) {
|
|
2334
|
+
logErrorWithDiagnosis(
|
|
2335
|
+
error,
|
|
2336
|
+
{
|
|
2337
|
+
messageCount: messages.length,
|
|
2338
|
+
systemPromptLength: systemPrompt.join(" ").length,
|
|
2339
|
+
model: options.model,
|
|
2340
|
+
toolCount: tools.length,
|
|
2341
|
+
phase: "LLM_CALL"
|
|
2342
|
+
},
|
|
2343
|
+
currentRequest?.id
|
|
2344
|
+
);
|
|
2345
|
+
throw error;
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
async function queryLLMWithPromptCaching(messages, systemPrompt, maxThinkingTokens, tools, signal, options) {
|
|
2349
|
+
const config = getGlobalConfig();
|
|
2350
|
+
const modelManager = getModelManager();
|
|
2351
|
+
const toolUseContext = options.toolUseContext;
|
|
2352
|
+
const modelProfile = options.modelProfile || modelManager.getModel("main");
|
|
2353
|
+
let provider;
|
|
2354
|
+
if (modelProfile) {
|
|
2355
|
+
provider = modelProfile.provider || config.primaryProvider || "anthropic";
|
|
2356
|
+
} else {
|
|
2357
|
+
provider = config.primaryProvider || "anthropic";
|
|
2358
|
+
}
|
|
2359
|
+
if (provider === "anthropic" || provider === "bigdream" || provider === "opendev" || provider === "minimax-coding") {
|
|
2360
|
+
return queryAnthropicNative(
|
|
2361
|
+
messages,
|
|
2362
|
+
systemPrompt,
|
|
2363
|
+
maxThinkingTokens,
|
|
2364
|
+
tools,
|
|
2365
|
+
signal,
|
|
2366
|
+
{ ...options, modelProfile, toolUseContext }
|
|
2367
|
+
);
|
|
2368
|
+
}
|
|
2369
|
+
return queryOpenAI(messages, systemPrompt, maxThinkingTokens, tools, signal, {
|
|
2370
|
+
...options,
|
|
2371
|
+
modelProfile,
|
|
2372
|
+
toolUseContext
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
async function queryAnthropicNative(messages, systemPrompt, maxThinkingTokens, tools, signal, options) {
|
|
2376
|
+
const config = getGlobalConfig();
|
|
2377
|
+
const modelManager = getModelManager();
|
|
2378
|
+
const toolUseContext = options?.toolUseContext;
|
|
2379
|
+
const modelProfile = options?.modelProfile || modelManager.getModel("main");
|
|
2380
|
+
let anthropic;
|
|
2381
|
+
let model;
|
|
2382
|
+
let provider;
|
|
2383
|
+
debug.api("MODEL_CONFIG_ANTHROPIC", {
|
|
2384
|
+
modelProfileFound: !!modelProfile,
|
|
2385
|
+
modelProfileId: modelProfile?.modelName,
|
|
2386
|
+
modelProfileName: modelProfile?.name,
|
|
2387
|
+
modelProfileModelName: modelProfile?.modelName,
|
|
2388
|
+
modelProfileProvider: modelProfile?.provider,
|
|
2389
|
+
modelProfileBaseURL: modelProfile?.baseURL,
|
|
2390
|
+
modelProfileApiKeyExists: !!modelProfile?.apiKey,
|
|
2391
|
+
optionsModel: options?.model,
|
|
2392
|
+
requestId: getCurrentRequest()?.id
|
|
2393
|
+
});
|
|
2394
|
+
if (modelProfile) {
|
|
2395
|
+
model = modelProfile.modelName;
|
|
2396
|
+
provider = modelProfile.provider || config.primaryProvider || "anthropic";
|
|
2397
|
+
if (modelProfile.provider === "anthropic" || modelProfile.provider === "minimax-coding") {
|
|
2398
|
+
const clientConfig = {
|
|
2399
|
+
apiKey: modelProfile.apiKey,
|
|
2400
|
+
dangerouslyAllowBrowser: true,
|
|
2401
|
+
maxRetries: 0,
|
|
2402
|
+
timeout: parseInt(process.env.API_TIMEOUT_MS || String(60 * 1e3), 10),
|
|
2403
|
+
defaultHeaders: {
|
|
2404
|
+
"x-app": "cli",
|
|
2405
|
+
"User-Agent": USER_AGENT
|
|
2406
|
+
}
|
|
2407
|
+
};
|
|
2408
|
+
if (modelProfile.baseURL) {
|
|
2409
|
+
clientConfig.baseURL = modelProfile.baseURL;
|
|
2410
|
+
}
|
|
2411
|
+
anthropic = new Anthropic(clientConfig);
|
|
2412
|
+
} else {
|
|
2413
|
+
anthropic = getAnthropicClient(model);
|
|
2414
|
+
}
|
|
2415
|
+
} else {
|
|
2416
|
+
const errorDetails = {
|
|
2417
|
+
modelProfileExists: !!modelProfile,
|
|
2418
|
+
modelProfileModelName: modelProfile?.modelName,
|
|
2419
|
+
requestedModel: options?.model,
|
|
2420
|
+
requestId: getCurrentRequest()?.id
|
|
2421
|
+
};
|
|
2422
|
+
debug.error("ANTHROPIC_FALLBACK_ERROR", errorDetails);
|
|
2423
|
+
throw new Error(
|
|
2424
|
+
`No valid ModelProfile available for Anthropic provider. Please configure model through /model command. Debug: ${JSON.stringify(errorDetails)}`
|
|
2425
|
+
);
|
|
2426
|
+
}
|
|
2427
|
+
if (options?.prependCLISysprompt) {
|
|
2428
|
+
const [firstSyspromptBlock] = splitSysPromptPrefix(systemPrompt);
|
|
2429
|
+
systemPrompt = [getCLISyspromptPrefix(), ...systemPrompt];
|
|
2430
|
+
}
|
|
2431
|
+
const system = splitSysPromptPrefix(systemPrompt).map(
|
|
2432
|
+
(_) => ({
|
|
2433
|
+
text: _,
|
|
2434
|
+
type: "text"
|
|
2435
|
+
})
|
|
2436
|
+
);
|
|
2437
|
+
const toolSchemas = await Promise.all(
|
|
2438
|
+
tools.map(
|
|
2439
|
+
async (tool) => ({
|
|
2440
|
+
name: tool.name,
|
|
2441
|
+
description: getToolDescription(tool),
|
|
2442
|
+
input_schema: "inputJSONSchema" in tool && tool.inputJSONSchema ? tool.inputJSONSchema : zodToJsonSchema4(tool.inputSchema)
|
|
2443
|
+
})
|
|
2444
|
+
)
|
|
2445
|
+
);
|
|
2446
|
+
const anthropicMessages = addCacheBreakpoints(messages);
|
|
2447
|
+
const { systemBlocks: processedSystem, messageParams: processedMessages } = applyCacheControlWithLimits(system, anthropicMessages);
|
|
2448
|
+
const startIncludingRetries = Date.now();
|
|
2449
|
+
logSystemPromptConstruction({
|
|
2450
|
+
basePrompt: systemPrompt.join("\n"),
|
|
2451
|
+
kodeContext: generateKodeContext() || "",
|
|
2452
|
+
reminders: [],
|
|
2453
|
+
finalPrompt: systemPrompt.join("\n")
|
|
2454
|
+
});
|
|
2455
|
+
let start = Date.now();
|
|
2456
|
+
let attemptNumber = 0;
|
|
2457
|
+
let response;
|
|
2458
|
+
try {
|
|
2459
|
+
response = await withRetry(
|
|
2460
|
+
async (attempt) => {
|
|
2461
|
+
attemptNumber = attempt;
|
|
2462
|
+
start = Date.now();
|
|
2463
|
+
const params = {
|
|
2464
|
+
model,
|
|
2465
|
+
max_tokens: options?.maxTokens ?? getMaxTokensFromProfile(modelProfile),
|
|
2466
|
+
messages: processedMessages,
|
|
2467
|
+
system: processedSystem,
|
|
2468
|
+
tools: toolSchemas.length > 0 ? toolSchemas : void 0,
|
|
2469
|
+
tool_choice: toolSchemas.length > 0 ? { type: "auto" } : void 0,
|
|
2470
|
+
...options?.temperature !== void 0 ? { temperature: options.temperature } : {},
|
|
2471
|
+
...options?.stopSequences && options.stopSequences.length > 0 ? { stop_sequences: options.stopSequences } : {}
|
|
2472
|
+
};
|
|
2473
|
+
if (maxThinkingTokens > 0) {
|
|
2474
|
+
;
|
|
2475
|
+
params.extra_headers = {
|
|
2476
|
+
"anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15"
|
|
2477
|
+
};
|
|
2478
|
+
params.thinking = { max_tokens: maxThinkingTokens };
|
|
2479
|
+
}
|
|
2480
|
+
debug.api("ANTHROPIC_API_CALL_START_STREAMING", {
|
|
2481
|
+
endpoint: modelProfile?.baseURL || "DEFAULT_ANTHROPIC",
|
|
2482
|
+
model,
|
|
2483
|
+
provider,
|
|
2484
|
+
apiKeyConfigured: !!modelProfile?.apiKey,
|
|
2485
|
+
apiKeyPrefix: modelProfile?.apiKey ? modelProfile.apiKey.substring(0, 8) : null,
|
|
2486
|
+
maxTokens: params.max_tokens,
|
|
2487
|
+
temperature: options?.temperature ?? MAIN_QUERY_TEMPERATURE,
|
|
2488
|
+
params,
|
|
2489
|
+
messageCount: params.messages?.length || 0,
|
|
2490
|
+
streamMode: true,
|
|
2491
|
+
toolsCount: toolSchemas.length,
|
|
2492
|
+
thinkingTokens: maxThinkingTokens,
|
|
2493
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2494
|
+
modelProfileId: modelProfile?.modelName,
|
|
2495
|
+
modelProfileName: modelProfile?.name
|
|
2496
|
+
});
|
|
2497
|
+
if (config.stream) {
|
|
2498
|
+
const stream = await anthropic.beta.messages.create(
|
|
2499
|
+
{
|
|
2500
|
+
...params,
|
|
2501
|
+
stream: true
|
|
2502
|
+
},
|
|
2503
|
+
{
|
|
2504
|
+
signal
|
|
2505
|
+
}
|
|
2506
|
+
);
|
|
2507
|
+
let finalResponse = null;
|
|
2508
|
+
let messageStartEvent = null;
|
|
2509
|
+
const contentBlocks = [];
|
|
2510
|
+
const inputJSONBuffers = /* @__PURE__ */ new Map();
|
|
2511
|
+
let usage = null;
|
|
2512
|
+
let stopReason = null;
|
|
2513
|
+
let stopSequence = null;
|
|
2514
|
+
let hasMarkedStreaming = false;
|
|
2515
|
+
for await (const event of stream) {
|
|
2516
|
+
if (signal.aborted) {
|
|
2517
|
+
debug.flow("STREAM_ABORTED", {
|
|
2518
|
+
eventType: event.type,
|
|
2519
|
+
timestamp: Date.now()
|
|
2520
|
+
});
|
|
2521
|
+
throw new Error("Request was cancelled");
|
|
2522
|
+
}
|
|
2523
|
+
switch (event.type) {
|
|
2524
|
+
case "message_start":
|
|
2525
|
+
messageStartEvent = event;
|
|
2526
|
+
finalResponse = {
|
|
2527
|
+
...event.message,
|
|
2528
|
+
content: []
|
|
2529
|
+
};
|
|
2530
|
+
break;
|
|
2531
|
+
case "content_block_start":
|
|
2532
|
+
contentBlocks[event.index] = { ...event.content_block };
|
|
2533
|
+
const contentBlockType = event.content_block.type;
|
|
2534
|
+
if (contentBlockType === "tool_use" || contentBlockType === "server_tool_use" || contentBlockType === "mcp_tool_use") {
|
|
2535
|
+
setRequestStatus({
|
|
2536
|
+
kind: "tool",
|
|
2537
|
+
detail: event.content_block.name
|
|
2538
|
+
});
|
|
2539
|
+
inputJSONBuffers.set(event.index, "");
|
|
2540
|
+
}
|
|
2541
|
+
break;
|
|
2542
|
+
case "content_block_delta":
|
|
2543
|
+
const blockIndex = event.index;
|
|
2544
|
+
if (!contentBlocks[blockIndex]) {
|
|
2545
|
+
contentBlocks[blockIndex] = {
|
|
2546
|
+
type: event.delta.type === "text_delta" ? "text" : "tool_use",
|
|
2547
|
+
text: event.delta.type === "text_delta" ? "" : void 0
|
|
2548
|
+
};
|
|
2549
|
+
if (event.delta.type === "input_json_delta") {
|
|
2550
|
+
inputJSONBuffers.set(blockIndex, "");
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
if (event.delta.type === "text_delta") {
|
|
2554
|
+
if (!hasMarkedStreaming) {
|
|
2555
|
+
setRequestStatus({ kind: "streaming" });
|
|
2556
|
+
hasMarkedStreaming = true;
|
|
2557
|
+
}
|
|
2558
|
+
contentBlocks[blockIndex].text += event.delta.text;
|
|
2559
|
+
} else if (event.delta.type === "input_json_delta") {
|
|
2560
|
+
const currentBuffer = inputJSONBuffers.get(blockIndex) || "";
|
|
2561
|
+
const nextBuffer = currentBuffer + event.delta.partial_json;
|
|
2562
|
+
inputJSONBuffers.set(blockIndex, nextBuffer);
|
|
2563
|
+
const trimmed = nextBuffer.trim();
|
|
2564
|
+
if (trimmed.length === 0) {
|
|
2565
|
+
contentBlocks[blockIndex].input = {};
|
|
2566
|
+
break;
|
|
2567
|
+
}
|
|
2568
|
+
contentBlocks[blockIndex].input = parseToolUsePartialJsonOrThrow(nextBuffer) ?? {};
|
|
2569
|
+
}
|
|
2570
|
+
break;
|
|
2571
|
+
case "message_delta":
|
|
2572
|
+
if (event.delta.stop_reason)
|
|
2573
|
+
stopReason = event.delta.stop_reason;
|
|
2574
|
+
if (event.delta.stop_sequence)
|
|
2575
|
+
stopSequence = event.delta.stop_sequence;
|
|
2576
|
+
if (event.usage) usage = { ...usage, ...event.usage };
|
|
2577
|
+
break;
|
|
2578
|
+
case "content_block_stop":
|
|
2579
|
+
const stopIndex = event.index;
|
|
2580
|
+
const block = contentBlocks[stopIndex];
|
|
2581
|
+
if ((block?.type === "tool_use" || block?.type === "server_tool_use" || block?.type === "mcp_tool_use") && inputJSONBuffers.has(stopIndex)) {
|
|
2582
|
+
const jsonStr = inputJSONBuffers.get(stopIndex) ?? "";
|
|
2583
|
+
if (block.input === void 0) {
|
|
2584
|
+
const trimmed = jsonStr.trim();
|
|
2585
|
+
if (trimmed.length === 0) {
|
|
2586
|
+
block.input = {};
|
|
2587
|
+
} else {
|
|
2588
|
+
block.input = parseToolUsePartialJsonOrThrow(jsonStr) ?? {};
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
inputJSONBuffers.delete(stopIndex);
|
|
2592
|
+
}
|
|
2593
|
+
break;
|
|
2594
|
+
case "message_stop":
|
|
2595
|
+
inputJSONBuffers.clear();
|
|
2596
|
+
break;
|
|
2597
|
+
}
|
|
2598
|
+
if (event.type === "message_stop") {
|
|
2599
|
+
break;
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
if (!finalResponse || !messageStartEvent) {
|
|
2603
|
+
throw new Error("Stream ended without proper message structure");
|
|
2604
|
+
}
|
|
2605
|
+
finalResponse = {
|
|
2606
|
+
...messageStartEvent.message,
|
|
2607
|
+
content: contentBlocks.filter(Boolean),
|
|
2608
|
+
stop_reason: stopReason,
|
|
2609
|
+
stop_sequence: stopSequence,
|
|
2610
|
+
usage: {
|
|
2611
|
+
...messageStartEvent.message.usage,
|
|
2612
|
+
...usage
|
|
2613
|
+
}
|
|
2614
|
+
};
|
|
2615
|
+
return finalResponse;
|
|
2616
|
+
} else {
|
|
2617
|
+
debug.api("ANTHROPIC_API_CALL_START_NON_STREAMING", {
|
|
2618
|
+
endpoint: modelProfile?.baseURL || "DEFAULT_ANTHROPIC",
|
|
2619
|
+
model,
|
|
2620
|
+
provider,
|
|
2621
|
+
apiKeyConfigured: !!modelProfile?.apiKey,
|
|
2622
|
+
apiKeyPrefix: modelProfile?.apiKey ? modelProfile.apiKey.substring(0, 8) : null,
|
|
2623
|
+
maxTokens: params.max_tokens,
|
|
2624
|
+
temperature: options?.temperature ?? MAIN_QUERY_TEMPERATURE,
|
|
2625
|
+
messageCount: params.messages?.length || 0,
|
|
2626
|
+
streamMode: false,
|
|
2627
|
+
toolsCount: toolSchemas.length,
|
|
2628
|
+
thinkingTokens: maxThinkingTokens,
|
|
2629
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2630
|
+
modelProfileId: modelProfile?.modelName,
|
|
2631
|
+
modelProfileName: modelProfile?.name
|
|
2632
|
+
});
|
|
2633
|
+
return await anthropic.beta.messages.create(params, {
|
|
2634
|
+
signal
|
|
2635
|
+
});
|
|
2636
|
+
}
|
|
2637
|
+
},
|
|
2638
|
+
{ signal }
|
|
2639
|
+
);
|
|
2640
|
+
debug.api("ANTHROPIC_API_CALL_SUCCESS", {
|
|
2641
|
+
content: response.content
|
|
2642
|
+
});
|
|
2643
|
+
const ttftMs = start - Date.now();
|
|
2644
|
+
const durationMs = Date.now() - startIncludingRetries;
|
|
2645
|
+
const content = response.content.map((block) => {
|
|
2646
|
+
if (block.type === "text") {
|
|
2647
|
+
return {
|
|
2648
|
+
type: "text",
|
|
2649
|
+
text: block.text
|
|
2650
|
+
};
|
|
2651
|
+
} else if (block.type === "tool_use") {
|
|
2652
|
+
return {
|
|
2653
|
+
type: "tool_use",
|
|
2654
|
+
id: block.id,
|
|
2655
|
+
name: block.name,
|
|
2656
|
+
input: block.input
|
|
2657
|
+
};
|
|
2658
|
+
}
|
|
2659
|
+
return block;
|
|
2660
|
+
});
|
|
2661
|
+
const assistantMessage = {
|
|
2662
|
+
message: {
|
|
2663
|
+
id: response.id,
|
|
2664
|
+
content,
|
|
2665
|
+
model: response.model,
|
|
2666
|
+
role: "assistant",
|
|
2667
|
+
stop_reason: response.stop_reason,
|
|
2668
|
+
stop_sequence: response.stop_sequence,
|
|
2669
|
+
type: "message",
|
|
2670
|
+
usage: response.usage
|
|
2671
|
+
},
|
|
2672
|
+
type: "assistant",
|
|
2673
|
+
uuid: nanoid(),
|
|
2674
|
+
durationMs,
|
|
2675
|
+
costUSD: 0
|
|
2676
|
+
};
|
|
2677
|
+
const systemMessages = system.map((block) => ({
|
|
2678
|
+
role: "system",
|
|
2679
|
+
content: block.text
|
|
2680
|
+
}));
|
|
2681
|
+
logLLMInteraction({
|
|
2682
|
+
systemPrompt: systemPrompt.join("\n"),
|
|
2683
|
+
messages: [...systemMessages, ...anthropicMessages],
|
|
2684
|
+
response,
|
|
2685
|
+
usage: response.usage ? {
|
|
2686
|
+
inputTokens: response.usage.input_tokens,
|
|
2687
|
+
outputTokens: response.usage.output_tokens
|
|
2688
|
+
} : void 0,
|
|
2689
|
+
timing: {
|
|
2690
|
+
start,
|
|
2691
|
+
end: Date.now()
|
|
2692
|
+
},
|
|
2693
|
+
apiFormat: "anthropic"
|
|
2694
|
+
});
|
|
2695
|
+
const inputTokens = response.usage.input_tokens;
|
|
2696
|
+
const outputTokens = response.usage.output_tokens;
|
|
2697
|
+
const cacheCreationInputTokens = response.usage.cache_creation_input_tokens ?? 0;
|
|
2698
|
+
const cacheReadInputTokens = response.usage.cache_read_input_tokens ?? 0;
|
|
2699
|
+
const costUSD = inputTokens / 1e6 * getModelInputTokenCostUSD(model) + outputTokens / 1e6 * getModelOutputTokenCostUSD(model) + cacheCreationInputTokens / 1e6 * getModelInputTokenCostUSD(model) + cacheReadInputTokens / 1e6 * (getModelInputTokenCostUSD(model) * 0.1);
|
|
2700
|
+
assistantMessage.costUSD = costUSD;
|
|
2701
|
+
addToTotalCost(costUSD, durationMs);
|
|
2702
|
+
return assistantMessage;
|
|
2703
|
+
} catch (error) {
|
|
2704
|
+
return getAssistantMessageFromError(error);
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
function getAssistantMessageFromError(error) {
|
|
2708
|
+
if (error instanceof Error && error.message.includes("prompt is too long")) {
|
|
2709
|
+
return createAssistantAPIErrorMessage(PROMPT_TOO_LONG_ERROR_MESSAGE);
|
|
2710
|
+
}
|
|
2711
|
+
if (error instanceof Error && error.message.includes("Your credit balance is too low")) {
|
|
2712
|
+
return createAssistantAPIErrorMessage(CREDIT_BALANCE_TOO_LOW_ERROR_MESSAGE);
|
|
2713
|
+
}
|
|
2714
|
+
if (error instanceof Error && error.message.toLowerCase().includes("x-api-key")) {
|
|
2715
|
+
return createAssistantAPIErrorMessage(INVALID_API_KEY_ERROR_MESSAGE);
|
|
2716
|
+
}
|
|
2717
|
+
if (error instanceof Error) {
|
|
2718
|
+
if (process.env.NODE_ENV === "development") {
|
|
2719
|
+
debug.error("ANTHROPIC_API_ERROR", {
|
|
2720
|
+
message: error.message,
|
|
2721
|
+
stack: error.stack
|
|
2722
|
+
});
|
|
2723
|
+
}
|
|
2724
|
+
return createAssistantAPIErrorMessage(
|
|
2725
|
+
`${API_ERROR_MESSAGE_PREFIX}: ${error.message}`
|
|
2726
|
+
);
|
|
2727
|
+
}
|
|
2728
|
+
return createAssistantAPIErrorMessage(API_ERROR_MESSAGE_PREFIX);
|
|
2729
|
+
}
|
|
2730
|
+
function addCacheBreakpoints(messages) {
|
|
2731
|
+
return messages.map((msg, index) => {
|
|
2732
|
+
return msg.type === "user" ? userMessageToMessageParam(msg, index > messages.length - 3) : assistantMessageToMessageParam(msg, index > messages.length - 3);
|
|
2733
|
+
});
|
|
2734
|
+
}
|
|
2735
|
+
async function queryOpenAI(messages, systemPrompt, maxThinkingTokens, tools, signal, options) {
|
|
2736
|
+
const config = getGlobalConfig();
|
|
2737
|
+
const modelManager = getModelManager();
|
|
2738
|
+
const toolUseContext = options?.toolUseContext;
|
|
2739
|
+
const modelProfile = options?.modelProfile || modelManager.getModel("main");
|
|
2740
|
+
let model;
|
|
2741
|
+
const currentRequest = getCurrentRequest();
|
|
2742
|
+
debug.api("MODEL_CONFIG_OPENAI", {
|
|
2743
|
+
modelProfileFound: !!modelProfile,
|
|
2744
|
+
modelProfileId: modelProfile?.modelName,
|
|
2745
|
+
modelProfileName: modelProfile?.name,
|
|
2746
|
+
modelProfileModelName: modelProfile?.modelName,
|
|
2747
|
+
modelProfileProvider: modelProfile?.provider,
|
|
2748
|
+
modelProfileBaseURL: modelProfile?.baseURL,
|
|
2749
|
+
modelProfileApiKeyExists: !!modelProfile?.apiKey,
|
|
2750
|
+
optionsModel: options?.model,
|
|
2751
|
+
requestId: getCurrentRequest()?.id
|
|
2752
|
+
});
|
|
2753
|
+
if (modelProfile) {
|
|
2754
|
+
model = modelProfile.modelName;
|
|
2755
|
+
} else {
|
|
2756
|
+
model = options?.model || modelProfile?.modelName || "";
|
|
2757
|
+
}
|
|
2758
|
+
if (options?.prependCLISysprompt) {
|
|
2759
|
+
const [firstSyspromptBlock] = splitSysPromptPrefix(systemPrompt);
|
|
2760
|
+
systemPrompt = [getCLISyspromptPrefix() + systemPrompt];
|
|
2761
|
+
}
|
|
2762
|
+
const system = splitSysPromptPrefix(systemPrompt).map(
|
|
2763
|
+
(_) => ({
|
|
2764
|
+
...PROMPT_CACHING_ENABLED ? { cache_control: { type: "ephemeral" } } : {},
|
|
2765
|
+
text: _,
|
|
2766
|
+
type: "text"
|
|
2767
|
+
})
|
|
2768
|
+
);
|
|
2769
|
+
const toolSchemas = await Promise.all(
|
|
2770
|
+
tools.map(
|
|
2771
|
+
async (_) => ({
|
|
2772
|
+
type: "function",
|
|
2773
|
+
function: {
|
|
2774
|
+
name: _.name,
|
|
2775
|
+
description: await _.prompt({
|
|
2776
|
+
safeMode: options?.safeMode
|
|
2777
|
+
}),
|
|
2778
|
+
parameters: "inputJSONSchema" in _ && _.inputJSONSchema ? _.inputJSONSchema : zodToJsonSchema4(_.inputSchema)
|
|
2779
|
+
}
|
|
2780
|
+
})
|
|
2781
|
+
)
|
|
2782
|
+
);
|
|
2783
|
+
const openaiSystem = system.map(
|
|
2784
|
+
(s) => ({
|
|
2785
|
+
role: "system",
|
|
2786
|
+
content: s.text
|
|
2787
|
+
})
|
|
2788
|
+
);
|
|
2789
|
+
const openaiMessages = convertAnthropicMessagesToOpenAIMessages2(messages);
|
|
2790
|
+
logSystemPromptConstruction({
|
|
2791
|
+
basePrompt: systemPrompt.join("\n"),
|
|
2792
|
+
kodeContext: generateKodeContext() || "",
|
|
2793
|
+
reminders: [],
|
|
2794
|
+
finalPrompt: systemPrompt.join("\n")
|
|
2795
|
+
});
|
|
2796
|
+
let start = Date.now();
|
|
2797
|
+
let adapterContext = null;
|
|
2798
|
+
if (modelProfile && modelProfile.modelName) {
|
|
2799
|
+
debug.api("CHECKING_ADAPTER_SYSTEM", {
|
|
2800
|
+
modelProfileName: modelProfile.modelName,
|
|
2801
|
+
modelName: modelProfile.modelName,
|
|
2802
|
+
provider: modelProfile.provider,
|
|
2803
|
+
requestId: getCurrentRequest()?.id
|
|
2804
|
+
});
|
|
2805
|
+
const USE_NEW_ADAPTER_SYSTEM = process.env.USE_NEW_ADAPTERS !== "false";
|
|
2806
|
+
if (USE_NEW_ADAPTER_SYSTEM) {
|
|
2807
|
+
const shouldUseResponses = ModelAdapterFactory.shouldUseResponsesAPI(modelProfile);
|
|
2808
|
+
if (shouldUseResponses) {
|
|
2809
|
+
const adapter = ModelAdapterFactory.createAdapter(modelProfile);
|
|
2810
|
+
const reasoningEffort = await getReasoningEffort(modelProfile, messages);
|
|
2811
|
+
let verbosity = "medium";
|
|
2812
|
+
const modelNameLower = modelProfile.modelName.toLowerCase();
|
|
2813
|
+
if (modelNameLower.includes("high")) {
|
|
2814
|
+
verbosity = "high";
|
|
2815
|
+
} else if (modelNameLower.includes("low")) {
|
|
2816
|
+
verbosity = "low";
|
|
2817
|
+
}
|
|
2818
|
+
const unifiedParams = {
|
|
2819
|
+
messages: openaiMessages,
|
|
2820
|
+
systemPrompt: openaiSystem.map((s) => s.content),
|
|
2821
|
+
tools,
|
|
2822
|
+
maxTokens: options?.maxTokens ?? getMaxTokensFromProfile(modelProfile),
|
|
2823
|
+
stream: config.stream,
|
|
2824
|
+
reasoningEffort,
|
|
2825
|
+
temperature: options?.temperature ?? (isGPT5Model(model) ? 1 : MAIN_QUERY_TEMPERATURE),
|
|
2826
|
+
previousResponseId: toolUseContext?.responseState?.previousResponseId,
|
|
2827
|
+
verbosity,
|
|
2828
|
+
...options?.stopSequences && options.stopSequences.length > 0 ? { stopSequences: options.stopSequences } : {}
|
|
2829
|
+
};
|
|
2830
|
+
adapterContext = {
|
|
2831
|
+
adapter,
|
|
2832
|
+
request: adapter.createRequest(unifiedParams),
|
|
2833
|
+
shouldUseResponses: true
|
|
2834
|
+
};
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
}
|
|
2838
|
+
let queryResult;
|
|
2839
|
+
let startIncludingRetries = Date.now();
|
|
2840
|
+
try {
|
|
2841
|
+
queryResult = await withRetry(
|
|
2842
|
+
async () => {
|
|
2843
|
+
start = Date.now();
|
|
2844
|
+
if (adapterContext) {
|
|
2845
|
+
if (adapterContext.shouldUseResponses) {
|
|
2846
|
+
const { callGPT5ResponsesAPI } = await import("./openai-DT54BAFP.js");
|
|
2847
|
+
const response = await callGPT5ResponsesAPI(
|
|
2848
|
+
modelProfile,
|
|
2849
|
+
adapterContext.request,
|
|
2850
|
+
signal
|
|
2851
|
+
);
|
|
2852
|
+
const unifiedResponse = await adapterContext.adapter.parseResponse(response);
|
|
2853
|
+
const assistantMessage2 = buildAssistantMessageFromUnifiedResponse(
|
|
2854
|
+
unifiedResponse,
|
|
2855
|
+
start
|
|
2856
|
+
);
|
|
2857
|
+
assistantMessage2.message.usage = normalizeUsage(
|
|
2858
|
+
assistantMessage2.message.usage
|
|
2859
|
+
);
|
|
2860
|
+
return {
|
|
2861
|
+
assistantMessage: assistantMessage2,
|
|
2862
|
+
rawResponse: unifiedResponse,
|
|
2863
|
+
apiFormat: "openai"
|
|
2864
|
+
};
|
|
2865
|
+
}
|
|
2866
|
+
const s2 = await getCompletionWithProfile(
|
|
2867
|
+
modelProfile,
|
|
2868
|
+
adapterContext.request,
|
|
2869
|
+
0,
|
|
2870
|
+
10,
|
|
2871
|
+
signal
|
|
2872
|
+
);
|
|
2873
|
+
let finalResponse2;
|
|
2874
|
+
if (config.stream) {
|
|
2875
|
+
finalResponse2 = await handleMessageStream(
|
|
2876
|
+
s2,
|
|
2877
|
+
signal
|
|
2878
|
+
);
|
|
2879
|
+
} else {
|
|
2880
|
+
finalResponse2 = s2;
|
|
2881
|
+
}
|
|
2882
|
+
const message2 = convertOpenAIResponseToAnthropic(finalResponse2, tools);
|
|
2883
|
+
const assistantMsg2 = {
|
|
2884
|
+
type: "assistant",
|
|
2885
|
+
message: message2,
|
|
2886
|
+
costUSD: 0,
|
|
2887
|
+
durationMs: Date.now() - start,
|
|
2888
|
+
uuid: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
|
2889
|
+
};
|
|
2890
|
+
return {
|
|
2891
|
+
assistantMessage: assistantMsg2,
|
|
2892
|
+
rawResponse: finalResponse2,
|
|
2893
|
+
apiFormat: "openai"
|
|
2894
|
+
};
|
|
2895
|
+
}
|
|
2896
|
+
const maxTokens = options?.maxTokens ?? getMaxTokensFromProfile(modelProfile);
|
|
2897
|
+
const isGPT5 = isGPT5Model(model);
|
|
2898
|
+
const opts = {
|
|
2899
|
+
model,
|
|
2900
|
+
...isGPT5 ? { max_completion_tokens: maxTokens } : { max_tokens: maxTokens },
|
|
2901
|
+
messages: [...openaiSystem, ...openaiMessages],
|
|
2902
|
+
temperature: options?.temperature ?? (isGPT5 ? 1 : MAIN_QUERY_TEMPERATURE)
|
|
2903
|
+
};
|
|
2904
|
+
if (options?.stopSequences && options.stopSequences.length > 0) {
|
|
2905
|
+
;
|
|
2906
|
+
opts.stop = options.stopSequences;
|
|
2907
|
+
}
|
|
2908
|
+
if (config.stream) {
|
|
2909
|
+
;
|
|
2910
|
+
opts.stream = true;
|
|
2911
|
+
opts.stream_options = {
|
|
2912
|
+
include_usage: true
|
|
2913
|
+
};
|
|
2914
|
+
}
|
|
2915
|
+
if (toolSchemas.length > 0) {
|
|
2916
|
+
opts.tools = toolSchemas;
|
|
2917
|
+
opts.tool_choice = "auto";
|
|
2918
|
+
}
|
|
2919
|
+
const reasoningEffort = await getReasoningEffort(modelProfile, messages);
|
|
2920
|
+
if (reasoningEffort) {
|
|
2921
|
+
opts.reasoning_effort = reasoningEffort;
|
|
2922
|
+
}
|
|
2923
|
+
const completionFunction = isGPT5Model(modelProfile?.modelName || "") ? getGPT5CompletionWithProfile : getCompletionWithProfile;
|
|
2924
|
+
const s = await completionFunction(modelProfile, opts, 0, 10, signal);
|
|
2925
|
+
let finalResponse;
|
|
2926
|
+
if (opts.stream) {
|
|
2927
|
+
finalResponse = await handleMessageStream(
|
|
2928
|
+
s,
|
|
2929
|
+
signal
|
|
2930
|
+
);
|
|
2931
|
+
} else {
|
|
2932
|
+
finalResponse = s;
|
|
2933
|
+
}
|
|
2934
|
+
const message = convertOpenAIResponseToAnthropic(finalResponse, tools);
|
|
2935
|
+
const assistantMsg = {
|
|
2936
|
+
type: "assistant",
|
|
2937
|
+
message,
|
|
2938
|
+
costUSD: 0,
|
|
2939
|
+
durationMs: Date.now() - start,
|
|
2940
|
+
uuid: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
|
2941
|
+
};
|
|
2942
|
+
return {
|
|
2943
|
+
assistantMessage: assistantMsg,
|
|
2944
|
+
rawResponse: finalResponse,
|
|
2945
|
+
apiFormat: "openai"
|
|
2946
|
+
};
|
|
2947
|
+
},
|
|
2948
|
+
{ signal }
|
|
2949
|
+
);
|
|
2950
|
+
} catch (error) {
|
|
2951
|
+
logError(error);
|
|
2952
|
+
return getAssistantMessageFromError(error);
|
|
2953
|
+
}
|
|
2954
|
+
const durationMs = Date.now() - start;
|
|
2955
|
+
const durationMsIncludingRetries = Date.now() - startIncludingRetries;
|
|
2956
|
+
const assistantMessage = queryResult.assistantMessage;
|
|
2957
|
+
assistantMessage.message.content = normalizeContentFromAPI(
|
|
2958
|
+
assistantMessage.message.content || []
|
|
2959
|
+
);
|
|
2960
|
+
const normalizedUsage = normalizeUsage(assistantMessage.message.usage);
|
|
2961
|
+
assistantMessage.message.usage = normalizedUsage;
|
|
2962
|
+
const inputTokens = normalizedUsage.input_tokens ?? 0;
|
|
2963
|
+
const outputTokens = normalizedUsage.output_tokens ?? 0;
|
|
2964
|
+
const cacheReadInputTokens = normalizedUsage.cache_read_input_tokens ?? 0;
|
|
2965
|
+
const cacheCreationInputTokens = normalizedUsage.cache_creation_input_tokens ?? 0;
|
|
2966
|
+
const costUSD = inputTokens / 1e6 * SONNET_COST_PER_MILLION_INPUT_TOKENS + outputTokens / 1e6 * SONNET_COST_PER_MILLION_OUTPUT_TOKENS + cacheReadInputTokens / 1e6 * SONNET_COST_PER_MILLION_PROMPT_CACHE_READ_TOKENS + cacheCreationInputTokens / 1e6 * SONNET_COST_PER_MILLION_PROMPT_CACHE_WRITE_TOKENS;
|
|
2967
|
+
addToTotalCost(costUSD, durationMsIncludingRetries);
|
|
2968
|
+
logLLMInteraction({
|
|
2969
|
+
systemPrompt: systemPrompt.join("\n"),
|
|
2970
|
+
messages: [...openaiSystem, ...openaiMessages],
|
|
2971
|
+
response: assistantMessage.message || queryResult.rawResponse,
|
|
2972
|
+
usage: {
|
|
2973
|
+
inputTokens,
|
|
2974
|
+
outputTokens
|
|
2975
|
+
},
|
|
2976
|
+
timing: {
|
|
2977
|
+
start,
|
|
2978
|
+
end: Date.now()
|
|
2979
|
+
},
|
|
2980
|
+
apiFormat: queryResult.apiFormat
|
|
2981
|
+
});
|
|
2982
|
+
assistantMessage.costUSD = costUSD;
|
|
2983
|
+
assistantMessage.durationMs = durationMs;
|
|
2984
|
+
assistantMessage.uuid = assistantMessage.uuid || randomUUID();
|
|
2985
|
+
return assistantMessage;
|
|
2986
|
+
}
|
|
2987
|
+
function getMaxTokensFromProfile(modelProfile) {
|
|
2988
|
+
return modelProfile?.maxTokens || 8e3;
|
|
2989
|
+
}
|
|
2990
|
+
function buildAssistantMessageFromUnifiedResponse(unifiedResponse, startTime) {
|
|
2991
|
+
const contentBlocks = [...unifiedResponse.content || []];
|
|
2992
|
+
if (unifiedResponse.toolCalls && unifiedResponse.toolCalls.length > 0) {
|
|
2993
|
+
for (const toolCall of unifiedResponse.toolCalls) {
|
|
2994
|
+
const tool = toolCall.function;
|
|
2995
|
+
const toolName = tool?.name;
|
|
2996
|
+
let toolArgs = {};
|
|
2997
|
+
try {
|
|
2998
|
+
toolArgs = tool?.arguments ? JSON.parse(tool.arguments) : {};
|
|
2999
|
+
} catch (e) {
|
|
3000
|
+
}
|
|
3001
|
+
contentBlocks.push({
|
|
3002
|
+
type: "tool_use",
|
|
3003
|
+
input: toolArgs,
|
|
3004
|
+
name: toolName,
|
|
3005
|
+
id: toolCall.id?.length > 0 ? toolCall.id : nanoid()
|
|
3006
|
+
});
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
3009
|
+
return {
|
|
3010
|
+
type: "assistant",
|
|
3011
|
+
message: {
|
|
3012
|
+
role: "assistant",
|
|
3013
|
+
content: contentBlocks,
|
|
3014
|
+
usage: {
|
|
3015
|
+
input_tokens: unifiedResponse.usage?.promptTokens ?? unifiedResponse.usage?.input_tokens ?? 0,
|
|
3016
|
+
output_tokens: unifiedResponse.usage?.completionTokens ?? unifiedResponse.usage?.output_tokens ?? 0,
|
|
3017
|
+
prompt_tokens: unifiedResponse.usage?.promptTokens ?? unifiedResponse.usage?.input_tokens ?? 0,
|
|
3018
|
+
completion_tokens: unifiedResponse.usage?.completionTokens ?? unifiedResponse.usage?.output_tokens ?? 0,
|
|
3019
|
+
promptTokens: unifiedResponse.usage?.promptTokens ?? unifiedResponse.usage?.input_tokens ?? 0,
|
|
3020
|
+
completionTokens: unifiedResponse.usage?.completionTokens ?? unifiedResponse.usage?.output_tokens ?? 0,
|
|
3021
|
+
totalTokens: unifiedResponse.usage?.totalTokens ?? (unifiedResponse.usage?.promptTokens ?? unifiedResponse.usage?.input_tokens ?? 0) + (unifiedResponse.usage?.completionTokens ?? unifiedResponse.usage?.output_tokens ?? 0)
|
|
3022
|
+
}
|
|
3023
|
+
},
|
|
3024
|
+
costUSD: 0,
|
|
3025
|
+
durationMs: Date.now() - startTime,
|
|
3026
|
+
uuid: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
|
|
3027
|
+
responseId: unifiedResponse.responseId
|
|
3028
|
+
};
|
|
3029
|
+
}
|
|
3030
|
+
function normalizeUsage(usage) {
|
|
3031
|
+
if (!usage) {
|
|
3032
|
+
return {
|
|
3033
|
+
input_tokens: 0,
|
|
3034
|
+
output_tokens: 0,
|
|
3035
|
+
cache_read_input_tokens: 0,
|
|
3036
|
+
cache_creation_input_tokens: 0
|
|
3037
|
+
};
|
|
3038
|
+
}
|
|
3039
|
+
const inputTokens = usage.input_tokens ?? usage.prompt_tokens ?? usage.inputTokens ?? 0;
|
|
3040
|
+
const outputTokens = usage.output_tokens ?? usage.completion_tokens ?? usage.outputTokens ?? 0;
|
|
3041
|
+
const cacheReadInputTokens = usage.cache_read_input_tokens ?? usage.prompt_token_details?.cached_tokens ?? usage.cacheReadInputTokens ?? 0;
|
|
3042
|
+
const cacheCreationInputTokens = usage.cache_creation_input_tokens ?? usage.cacheCreatedInputTokens ?? 0;
|
|
3043
|
+
return {
|
|
3044
|
+
...usage,
|
|
3045
|
+
input_tokens: inputTokens,
|
|
3046
|
+
output_tokens: outputTokens,
|
|
3047
|
+
cache_read_input_tokens: cacheReadInputTokens,
|
|
3048
|
+
cache_creation_input_tokens: cacheCreationInputTokens
|
|
3049
|
+
};
|
|
3050
|
+
}
|
|
3051
|
+
function getModelInputTokenCostUSD(model) {
|
|
3052
|
+
for (const providerModels of Object.values(models_default)) {
|
|
3053
|
+
const modelInfo = providerModels.find((m) => m.model === model);
|
|
3054
|
+
if (modelInfo) {
|
|
3055
|
+
return modelInfo.input_cost_per_token || 0;
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
return 3e-6;
|
|
3059
|
+
}
|
|
3060
|
+
function getModelOutputTokenCostUSD(model) {
|
|
3061
|
+
for (const providerModels of Object.values(models_default)) {
|
|
3062
|
+
const modelInfo = providerModels.find((m) => m.model === model);
|
|
3063
|
+
if (modelInfo) {
|
|
3064
|
+
return modelInfo.output_cost_per_token || 0;
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
return 15e-6;
|
|
3068
|
+
}
|
|
3069
|
+
async function queryModel(modelPointer, messages, systemPrompt = [], signal) {
|
|
3070
|
+
return queryLLM(
|
|
3071
|
+
messages,
|
|
3072
|
+
systemPrompt,
|
|
3073
|
+
0,
|
|
3074
|
+
[],
|
|
3075
|
+
signal || new AbortController().signal,
|
|
3076
|
+
{
|
|
3077
|
+
safeMode: false,
|
|
3078
|
+
model: modelPointer,
|
|
3079
|
+
prependCLISysprompt: true
|
|
3080
|
+
}
|
|
3081
|
+
);
|
|
3082
|
+
}
|
|
3083
|
+
async function queryQuick({
|
|
3084
|
+
systemPrompt = [],
|
|
3085
|
+
userPrompt,
|
|
3086
|
+
assistantPrompt,
|
|
3087
|
+
enablePromptCaching = false,
|
|
3088
|
+
signal
|
|
3089
|
+
}) {
|
|
3090
|
+
const messages = [
|
|
3091
|
+
{
|
|
3092
|
+
message: { role: "user", content: userPrompt },
|
|
3093
|
+
type: "user",
|
|
3094
|
+
uuid: randomUUID()
|
|
3095
|
+
}
|
|
3096
|
+
];
|
|
3097
|
+
return queryModel("quick", messages, systemPrompt, signal);
|
|
3098
|
+
}
|
|
3099
|
+
export {
|
|
3100
|
+
API_ERROR_MESSAGE_PREFIX,
|
|
3101
|
+
CREDIT_BALANCE_TOO_LOW_ERROR_MESSAGE,
|
|
3102
|
+
INVALID_API_KEY_ERROR_MESSAGE,
|
|
3103
|
+
MAIN_QUERY_TEMPERATURE,
|
|
3104
|
+
NO_CONTENT_MESSAGE,
|
|
3105
|
+
PROMPT_TOO_LONG_ERROR_MESSAGE,
|
|
3106
|
+
assistantMessageToMessageParam,
|
|
3107
|
+
fetchAnthropicModels,
|
|
3108
|
+
formatSystemPromptWithContext,
|
|
3109
|
+
generateKodeContext,
|
|
3110
|
+
getAnthropicClient,
|
|
3111
|
+
queryLLM,
|
|
3112
|
+
queryModel,
|
|
3113
|
+
queryQuick,
|
|
3114
|
+
refreshKodeContext,
|
|
3115
|
+
resetAnthropicClient,
|
|
3116
|
+
userMessageToMessageParam,
|
|
3117
|
+
verifyApiKey
|
|
3118
|
+
};
|