linkque-cli-v2 1.6.4 → 1.6.5
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/dev/worker.js +3 -16
- package/linkquejs.js +631 -1015
- package/package.json +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-KDHBKVSL.js → anthropic-messages-5DOLTIGP.js} +15 -14
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{azure-openai-responses-NPKMM3LF.js → azure-openai-responses-HVAVRLTK.js} +9 -7
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-JMBJJF7P.js → chunk-4XAZNSEQ.js} +5 -18
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-UZS5YLV2.js → chunk-IATVK6CO.js} +1 -1
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-J4JCPZDI.js → chunk-Q2Y6LMNC.js} +7 -5
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/chunk-RYONFGPP.js +317 -0
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-VBERH5QM.js → chunk-UTJ7DVT7.js} +133 -423
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-XOLFZ4XS.js → chunk-Y3G4J3TB.js} +1 -316
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-CO4I3RJE.js → chunk-YZLXAZFT.js} +2 -15
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{dist-TGBYXSBU.js → dist-ZPLSQEAM.js} +42 -27
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{google-generative-ai-KD7MXONK.js → google-generative-ai-OLT56CSV.js} +10 -8
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{google-vertex-TREYA472.js → google-vertex-4DBBJ7SR.js} +11 -9
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{mistral-conversations-OEOTQWE5.js → mistral-conversations-OEBGEDLQ.js} +9 -7
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{openai-codex-responses-GEFKSNVN.js → openai-codex-responses-3KOC6KHN.js} +12 -10
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{openai-completions-TAACO6T5.js → openai-completions-HG6OJRL5.js} +15 -13
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{openai-responses-4DDTXIGI.js → openai-responses-F6V2PG3Y.js} +14 -12
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/chat.js +60 -181
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/handler/clientCapability.js +2 -2
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/runtime/protocol.js +2 -2
- package/worker.js +183 -381
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/anthropic-messages-GSYNBLMR.js +0 -17
- package/vendor/linkque-agent-appwrite-integrate/dist/esm/{pi-messages-7JN3W3BC.js → pi-messages-DC4DVRAJ.js} +4 -4
package/package.json
CHANGED
|
@@ -5,29 +5,32 @@ import {
|
|
|
5
5
|
buildCopilotDynamicHeaders,
|
|
6
6
|
hasCopilotVisionInput
|
|
7
7
|
} from "./chunk-XMVFOHGJ.js";
|
|
8
|
-
import {
|
|
9
|
-
getProviderEnvValue
|
|
10
|
-
} from "./chunk-B5XRGKYX.js";
|
|
11
|
-
import {
|
|
12
|
-
parseJsonWithRepair,
|
|
13
|
-
parseStreamingJson
|
|
14
|
-
} from "./chunk-WJHOHUET.js";
|
|
15
8
|
import {
|
|
16
9
|
adjustMaxTokensForThinking,
|
|
17
10
|
buildBaseOptions,
|
|
18
|
-
calculateCost,
|
|
19
11
|
clampMaxTokensToContext,
|
|
20
12
|
transformMessages
|
|
21
|
-
} from "./chunk-
|
|
22
|
-
import {
|
|
23
|
-
AssistantMessageEventStream
|
|
24
|
-
} from "./chunk-46SEH6ER.js";
|
|
13
|
+
} from "./chunk-RYONFGPP.js";
|
|
25
14
|
import {
|
|
26
15
|
sanitizeSurrogates
|
|
27
16
|
} from "./chunk-CT4G24PS.js";
|
|
28
17
|
import {
|
|
29
18
|
headersToRecord
|
|
30
19
|
} from "./chunk-Y6UTJIT4.js";
|
|
20
|
+
import {
|
|
21
|
+
getProviderEnvValue
|
|
22
|
+
} from "./chunk-B5XRGKYX.js";
|
|
23
|
+
import {
|
|
24
|
+
parseJsonWithRepair,
|
|
25
|
+
parseStreamingJson
|
|
26
|
+
} from "./chunk-WJHOHUET.js";
|
|
27
|
+
import {
|
|
28
|
+
calculateCost
|
|
29
|
+
} from "./chunk-Y3G4J3TB.js";
|
|
30
|
+
import {
|
|
31
|
+
AssistantMessageEventStream
|
|
32
|
+
} from "./chunk-46SEH6ER.js";
|
|
33
|
+
import "./chunk-2ESYSVXG.js";
|
|
31
34
|
|
|
32
35
|
// ../../node_modules/@anthropic-ai/sdk/internal/tslib.mjs
|
|
33
36
|
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
@@ -7163,9 +7166,7 @@ function mapStopReason(reason, stopDetails) {
|
|
|
7163
7166
|
throw new Error(`Unhandled stop reason: ${reason}`);
|
|
7164
7167
|
}
|
|
7165
7168
|
}
|
|
7166
|
-
|
|
7167
7169
|
export {
|
|
7168
|
-
Anthropic,
|
|
7169
7170
|
stream,
|
|
7170
7171
|
streamSimple
|
|
7171
7172
|
};
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
convertResponsesMessages,
|
|
3
3
|
convertResponsesTools,
|
|
4
4
|
processResponsesStream
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-Q2Y6LMNC.js";
|
|
6
6
|
import "./chunk-UFVAKOTZ.js";
|
|
7
7
|
import {
|
|
8
8
|
clampOpenAIPromptCacheKey
|
|
@@ -14,21 +14,23 @@ import {
|
|
|
14
14
|
formatProviderError,
|
|
15
15
|
normalizeProviderError
|
|
16
16
|
} from "./chunk-NKEOCGYU.js";
|
|
17
|
+
import {
|
|
18
|
+
buildBaseOptions
|
|
19
|
+
} from "./chunk-RYONFGPP.js";
|
|
20
|
+
import "./chunk-CT4G24PS.js";
|
|
21
|
+
import {
|
|
22
|
+
headersToRecord
|
|
23
|
+
} from "./chunk-Y6UTJIT4.js";
|
|
17
24
|
import {
|
|
18
25
|
getProviderEnvValue
|
|
19
26
|
} from "./chunk-B5XRGKYX.js";
|
|
20
27
|
import "./chunk-WJHOHUET.js";
|
|
21
28
|
import {
|
|
22
|
-
buildBaseOptions,
|
|
23
29
|
clampThinkingLevel
|
|
24
|
-
} from "./chunk-
|
|
30
|
+
} from "./chunk-Y3G4J3TB.js";
|
|
25
31
|
import {
|
|
26
32
|
AssistantMessageEventStream
|
|
27
33
|
} from "./chunk-46SEH6ER.js";
|
|
28
|
-
import "./chunk-CT4G24PS.js";
|
|
29
|
-
import {
|
|
30
|
-
headersToRecord
|
|
31
|
-
} from "./chunk-Y6UTJIT4.js";
|
|
32
34
|
import "./chunk-2ESYSVXG.js";
|
|
33
35
|
|
|
34
36
|
// ../../node_modules/@earendil-works/pi-ai/dist/api/azure-openai-responses.js
|
package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-JMBJJF7P.js → chunk-4XAZNSEQ.js}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
transformMessages
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-RYONFGPP.js";
|
|
4
4
|
import {
|
|
5
5
|
sanitizeSurrogates
|
|
6
6
|
} from "./chunk-CT4G24PS.js";
|
|
@@ -1324,7 +1324,7 @@ Content-Type: ${partContentType}\r
|
|
|
1324
1324
|
* @returns A proxy agent
|
|
1325
1325
|
*/
|
|
1326
1326
|
static async #getProxyAgent() {
|
|
1327
|
-
this.#proxyAgent ||= (await import("./dist-
|
|
1327
|
+
this.#proxyAgent ||= (await import("./dist-ZPLSQEAM.js")).HttpsProxyAgent;
|
|
1328
1328
|
return this.#proxyAgent;
|
|
1329
1329
|
}
|
|
1330
1330
|
static async #getFetch() {
|
|
@@ -11618,23 +11618,11 @@ var require_websocket = __commonJS({
|
|
|
11618
11618
|
this._isServer = false;
|
|
11619
11619
|
this._redirects = 0;
|
|
11620
11620
|
if (protocols === void 0) {
|
|
11621
|
-
|
|
11622
|
-
protocols = [];
|
|
11623
|
-
} else if (Array.isArray(options.protocols)) {
|
|
11624
|
-
protocols = options.protocols;
|
|
11625
|
-
} else {
|
|
11626
|
-
protocols = [options.protocols];
|
|
11627
|
-
}
|
|
11621
|
+
protocols = [];
|
|
11628
11622
|
} else if (!Array.isArray(protocols)) {
|
|
11629
11623
|
if (typeof protocols === "object" && protocols !== null) {
|
|
11630
11624
|
options = protocols;
|
|
11631
|
-
|
|
11632
|
-
protocols = [];
|
|
11633
|
-
} else if (Array.isArray(options.protocols)) {
|
|
11634
|
-
protocols = options.protocols;
|
|
11635
|
-
} else {
|
|
11636
|
-
protocols = [options.protocols];
|
|
11637
|
-
}
|
|
11625
|
+
protocols = [];
|
|
11638
11626
|
} else {
|
|
11639
11627
|
protocols = [protocols];
|
|
11640
11628
|
}
|
|
@@ -11831,6 +11819,7 @@ var require_websocket = __commonJS({
|
|
|
11831
11819
|
}
|
|
11832
11820
|
return;
|
|
11833
11821
|
}
|
|
11822
|
+
this._readyState = _WebSocket.CLOSING;
|
|
11834
11823
|
this._sender.close(code, data, !this._isServer, (err) => {
|
|
11835
11824
|
if (err) return;
|
|
11836
11825
|
this._closeFrameSent = true;
|
|
@@ -11838,7 +11827,6 @@ var require_websocket = __commonJS({
|
|
|
11838
11827
|
this._socket.end();
|
|
11839
11828
|
}
|
|
11840
11829
|
});
|
|
11841
|
-
this._readyState = _WebSocket.CLOSING;
|
|
11842
11830
|
setCloseTimer(this);
|
|
11843
11831
|
}
|
|
11844
11832
|
/**
|
|
@@ -12063,7 +12051,6 @@ var require_websocket = __commonJS({
|
|
|
12063
12051
|
socketPath: void 0,
|
|
12064
12052
|
hostname: void 0,
|
|
12065
12053
|
protocol: void 0,
|
|
12066
|
-
protocols: void 0,
|
|
12067
12054
|
timeout: void 0,
|
|
12068
12055
|
method: "GET",
|
|
12069
12056
|
host: void 0,
|
package/vendor/linkque-agent-appwrite-integrate/dist/esm/{chunk-J4JCPZDI.js → chunk-Q2Y6LMNC.js}
RENAMED
|
@@ -2,15 +2,17 @@ import {
|
|
|
2
2
|
shortHash
|
|
3
3
|
} from "./chunk-UFVAKOTZ.js";
|
|
4
4
|
import {
|
|
5
|
-
parseStreamingJson
|
|
6
|
-
} from "./chunk-WJHOHUET.js";
|
|
7
|
-
import {
|
|
8
|
-
calculateCost,
|
|
9
5
|
transformMessages
|
|
10
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-RYONFGPP.js";
|
|
11
7
|
import {
|
|
12
8
|
sanitizeSurrogates
|
|
13
9
|
} from "./chunk-CT4G24PS.js";
|
|
10
|
+
import {
|
|
11
|
+
parseStreamingJson
|
|
12
|
+
} from "./chunk-WJHOHUET.js";
|
|
13
|
+
import {
|
|
14
|
+
calculateCost
|
|
15
|
+
} from "./chunk-Y3G4J3TB.js";
|
|
14
16
|
|
|
15
17
|
// ../../node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js
|
|
16
18
|
function encodeTextSignatureV1(id, phase) {
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// ../../node_modules/@earendil-works/pi-ai/dist/utils/estimate.js
|
|
2
|
+
var CHARS_PER_TOKEN = 4;
|
|
3
|
+
var ESTIMATED_IMAGE_CHARS = 4800;
|
|
4
|
+
function calculateContextTokens(usage) {
|
|
5
|
+
return usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
6
|
+
}
|
|
7
|
+
function safeJsonStringify(value) {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.stringify(value) ?? "undefined";
|
|
10
|
+
} catch {
|
|
11
|
+
return "[unserializable]";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function estimateTextAndImageContentChars(content) {
|
|
15
|
+
if (typeof content === "string")
|
|
16
|
+
return content.length;
|
|
17
|
+
let chars = 0;
|
|
18
|
+
for (const block of content)
|
|
19
|
+
chars += block.type === "text" ? block.text.length : ESTIMATED_IMAGE_CHARS;
|
|
20
|
+
return chars;
|
|
21
|
+
}
|
|
22
|
+
function estimateTextTokens(text) {
|
|
23
|
+
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
24
|
+
}
|
|
25
|
+
function estimateTextAndImageContentTokens(content) {
|
|
26
|
+
return Math.ceil(estimateTextAndImageContentChars(content) / CHARS_PER_TOKEN);
|
|
27
|
+
}
|
|
28
|
+
function estimateMessageTokens(message) {
|
|
29
|
+
let chars = 0;
|
|
30
|
+
if (message.role === "user")
|
|
31
|
+
return estimateTextAndImageContentTokens(message.content);
|
|
32
|
+
if (message.role === "toolResult")
|
|
33
|
+
return estimateTextAndImageContentTokens(message.content);
|
|
34
|
+
for (const block of message.content) {
|
|
35
|
+
if (block.type === "text") {
|
|
36
|
+
chars += block.text.length;
|
|
37
|
+
} else if (block.type === "thinking") {
|
|
38
|
+
chars += block.thinking.length;
|
|
39
|
+
} else {
|
|
40
|
+
chars += block.name.length + safeJsonStringify(block.arguments).length;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return Math.ceil(chars / CHARS_PER_TOKEN);
|
|
44
|
+
}
|
|
45
|
+
function getLastAssistantUsageInfo(messages) {
|
|
46
|
+
let latestPrefixTimestamp = Number.NEGATIVE_INFINITY;
|
|
47
|
+
let usageInfo;
|
|
48
|
+
for (let i = 0; i < messages.length; i++) {
|
|
49
|
+
const message = messages[i];
|
|
50
|
+
if (message.role === "assistant") {
|
|
51
|
+
const assistant = message;
|
|
52
|
+
const usageAppliesToPrefix = assistant.timestamp >= latestPrefixTimestamp;
|
|
53
|
+
if (usageAppliesToPrefix && assistant.stopReason !== "aborted" && assistant.stopReason !== "error" && calculateContextTokens(assistant.usage) > 0) {
|
|
54
|
+
usageInfo = { usage: assistant.usage, index: i };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
latestPrefixTimestamp = Math.max(latestPrefixTimestamp, message.timestamp);
|
|
58
|
+
}
|
|
59
|
+
return usageInfo;
|
|
60
|
+
}
|
|
61
|
+
function estimateMessages(messages) {
|
|
62
|
+
const usageInfo = getLastAssistantUsageInfo(messages);
|
|
63
|
+
if (usageInfo) {
|
|
64
|
+
const usageTokens = calculateContextTokens(usageInfo.usage);
|
|
65
|
+
let trailingTokens = 0;
|
|
66
|
+
for (let i = usageInfo.index + 1; i < messages.length; i++) {
|
|
67
|
+
trailingTokens += estimateMessageTokens(messages[i]);
|
|
68
|
+
}
|
|
69
|
+
return { tokens: usageTokens + trailingTokens, usageTokens, trailingTokens, lastUsageIndex: usageInfo.index };
|
|
70
|
+
}
|
|
71
|
+
let tokens = 0;
|
|
72
|
+
for (const message of messages)
|
|
73
|
+
tokens += estimateMessageTokens(message);
|
|
74
|
+
return { tokens, usageTokens: 0, trailingTokens: tokens, lastUsageIndex: null };
|
|
75
|
+
}
|
|
76
|
+
function estimateToolsTokens(tools) {
|
|
77
|
+
if (!tools || tools.length === 0)
|
|
78
|
+
return 0;
|
|
79
|
+
return estimateTextTokens(safeJsonStringify(tools));
|
|
80
|
+
}
|
|
81
|
+
function isMessageArray(value) {
|
|
82
|
+
return Array.isArray(value);
|
|
83
|
+
}
|
|
84
|
+
function estimateContextTokens(context) {
|
|
85
|
+
if (isMessageArray(context))
|
|
86
|
+
return estimateMessages(context);
|
|
87
|
+
const estimate = estimateMessages(context.messages);
|
|
88
|
+
if (estimate.lastUsageIndex !== null) {
|
|
89
|
+
const addedNames = new Set(context.messages.slice(estimate.lastUsageIndex + 1).filter((message) => message.role === "toolResult").flatMap((message) => message.addedToolNames ?? []));
|
|
90
|
+
const addedToolTokens = estimateToolsTokens(context.tools?.filter((tool) => addedNames.has(tool.name)));
|
|
91
|
+
return {
|
|
92
|
+
tokens: estimate.tokens + addedToolTokens,
|
|
93
|
+
usageTokens: estimate.usageTokens,
|
|
94
|
+
trailingTokens: estimate.trailingTokens + addedToolTokens,
|
|
95
|
+
lastUsageIndex: estimate.lastUsageIndex
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const prefixTokens = (context.systemPrompt ? estimateTextTokens(context.systemPrompt) : 0) + estimateToolsTokens(context.tools);
|
|
99
|
+
return {
|
|
100
|
+
tokens: estimate.tokens + prefixTokens,
|
|
101
|
+
usageTokens: estimate.usageTokens,
|
|
102
|
+
trailingTokens: estimate.trailingTokens + prefixTokens,
|
|
103
|
+
lastUsageIndex: estimate.lastUsageIndex
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ../../node_modules/@earendil-works/pi-ai/dist/api/simple-options.js
|
|
108
|
+
var CONTEXT_SAFETY_TOKENS = 4096;
|
|
109
|
+
var MIN_MAX_TOKENS = 1;
|
|
110
|
+
function clampMaxTokensToContext(model, context, maxTokens) {
|
|
111
|
+
if (model.contextWindow <= 0)
|
|
112
|
+
return Math.max(MIN_MAX_TOKENS, maxTokens);
|
|
113
|
+
const available = model.contextWindow - estimateContextTokens(context).tokens - CONTEXT_SAFETY_TOKENS;
|
|
114
|
+
return Math.min(maxTokens, Math.max(MIN_MAX_TOKENS, available));
|
|
115
|
+
}
|
|
116
|
+
function buildBaseOptions(model, context, options, apiKey) {
|
|
117
|
+
return {
|
|
118
|
+
temperature: options?.temperature,
|
|
119
|
+
maxTokens: clampMaxTokensToContext(model, context, options?.maxTokens ?? model.maxTokens),
|
|
120
|
+
signal: options?.signal,
|
|
121
|
+
apiKey: apiKey || options?.apiKey,
|
|
122
|
+
transport: options?.transport,
|
|
123
|
+
cacheRetention: options?.cacheRetention,
|
|
124
|
+
sessionId: options?.sessionId,
|
|
125
|
+
headers: options?.headers,
|
|
126
|
+
onPayload: options?.onPayload,
|
|
127
|
+
onResponse: options?.onResponse,
|
|
128
|
+
timeoutMs: options?.timeoutMs,
|
|
129
|
+
websocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,
|
|
130
|
+
maxRetries: options?.maxRetries,
|
|
131
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
132
|
+
metadata: options?.metadata,
|
|
133
|
+
env: options?.env
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function clampReasoning(effort) {
|
|
137
|
+
return effort === "xhigh" || effort === "max" ? "high" : effort;
|
|
138
|
+
}
|
|
139
|
+
function adjustMaxTokensForThinking(baseMaxTokens, modelMaxTokens, reasoningLevel, customBudgets) {
|
|
140
|
+
const defaultBudgets = {
|
|
141
|
+
minimal: 1024,
|
|
142
|
+
low: 2048,
|
|
143
|
+
medium: 8192,
|
|
144
|
+
high: 16384
|
|
145
|
+
};
|
|
146
|
+
const budgets = { ...defaultBudgets, ...customBudgets };
|
|
147
|
+
const minOutputTokens = 1024;
|
|
148
|
+
const level = clampReasoning(reasoningLevel);
|
|
149
|
+
let thinkingBudget = budgets[level];
|
|
150
|
+
const maxTokens = baseMaxTokens === void 0 ? modelMaxTokens : Math.min(baseMaxTokens + thinkingBudget, modelMaxTokens);
|
|
151
|
+
if (maxTokens <= thinkingBudget) {
|
|
152
|
+
thinkingBudget = Math.max(0, maxTokens - minOutputTokens);
|
|
153
|
+
}
|
|
154
|
+
return { maxTokens, thinkingBudget };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ../../node_modules/@earendil-works/pi-ai/dist/api/transform-messages.js
|
|
158
|
+
var NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
159
|
+
var NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
|
|
160
|
+
function replaceImagesWithPlaceholder(content, placeholder) {
|
|
161
|
+
const result = [];
|
|
162
|
+
let previousWasPlaceholder = false;
|
|
163
|
+
for (const block of content) {
|
|
164
|
+
if (block.type === "image") {
|
|
165
|
+
if (!previousWasPlaceholder) {
|
|
166
|
+
result.push({ type: "text", text: placeholder });
|
|
167
|
+
}
|
|
168
|
+
previousWasPlaceholder = true;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
result.push(block);
|
|
172
|
+
previousWasPlaceholder = block.text === placeholder;
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
function downgradeUnsupportedImages(messages, model) {
|
|
177
|
+
if (model.input.includes("image")) {
|
|
178
|
+
return messages;
|
|
179
|
+
}
|
|
180
|
+
return messages.map((msg) => {
|
|
181
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
182
|
+
return {
|
|
183
|
+
...msg,
|
|
184
|
+
content: replaceImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER)
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
if (msg.role === "toolResult") {
|
|
188
|
+
return {
|
|
189
|
+
...msg,
|
|
190
|
+
content: replaceImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return msg;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function transformMessages(messages, model, normalizeToolCallId) {
|
|
197
|
+
const toolCallIdMap = /* @__PURE__ */ new Map();
|
|
198
|
+
const normalizedMessages = messages.map((msg) => msg.content == null ? { ...msg, content: [] } : msg);
|
|
199
|
+
const imageAwareMessages = downgradeUnsupportedImages(normalizedMessages, model);
|
|
200
|
+
const transformed = imageAwareMessages.map((msg) => {
|
|
201
|
+
if (msg.role === "user") {
|
|
202
|
+
return msg;
|
|
203
|
+
}
|
|
204
|
+
if (msg.role === "toolResult") {
|
|
205
|
+
const normalizedId = toolCallIdMap.get(msg.toolCallId);
|
|
206
|
+
if (normalizedId && normalizedId !== msg.toolCallId) {
|
|
207
|
+
return { ...msg, toolCallId: normalizedId };
|
|
208
|
+
}
|
|
209
|
+
return msg;
|
|
210
|
+
}
|
|
211
|
+
if (msg.role === "assistant") {
|
|
212
|
+
const assistantMsg = msg;
|
|
213
|
+
const isSameModel = assistantMsg.provider === model.provider && assistantMsg.api === model.api && assistantMsg.model === model.id;
|
|
214
|
+
const transformedContent = assistantMsg.content.flatMap((block) => {
|
|
215
|
+
if (block.type === "thinking") {
|
|
216
|
+
if (block.redacted) {
|
|
217
|
+
return isSameModel ? block : [];
|
|
218
|
+
}
|
|
219
|
+
if (isSameModel && block.thinkingSignature)
|
|
220
|
+
return block;
|
|
221
|
+
if (!block.thinking || block.thinking.trim() === "")
|
|
222
|
+
return [];
|
|
223
|
+
if (isSameModel)
|
|
224
|
+
return block;
|
|
225
|
+
return {
|
|
226
|
+
type: "text",
|
|
227
|
+
text: block.thinking
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
if (block.type === "text") {
|
|
231
|
+
if (isSameModel)
|
|
232
|
+
return block;
|
|
233
|
+
return {
|
|
234
|
+
type: "text",
|
|
235
|
+
text: block.text
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
if (block.type === "toolCall") {
|
|
239
|
+
const toolCall = block;
|
|
240
|
+
let normalizedToolCall = toolCall;
|
|
241
|
+
if (!isSameModel && toolCall.thoughtSignature) {
|
|
242
|
+
normalizedToolCall = { ...toolCall };
|
|
243
|
+
delete normalizedToolCall.thoughtSignature;
|
|
244
|
+
}
|
|
245
|
+
if (!isSameModel && normalizeToolCallId) {
|
|
246
|
+
const normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);
|
|
247
|
+
if (normalizedId !== toolCall.id) {
|
|
248
|
+
toolCallIdMap.set(toolCall.id, normalizedId);
|
|
249
|
+
normalizedToolCall = { ...normalizedToolCall, id: normalizedId };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return normalizedToolCall;
|
|
253
|
+
}
|
|
254
|
+
return block;
|
|
255
|
+
});
|
|
256
|
+
return {
|
|
257
|
+
...assistantMsg,
|
|
258
|
+
content: transformedContent
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return msg;
|
|
262
|
+
});
|
|
263
|
+
const result = [];
|
|
264
|
+
let pendingToolCalls = [];
|
|
265
|
+
let existingToolResultIds = /* @__PURE__ */ new Set();
|
|
266
|
+
const insertSyntheticToolResults = () => {
|
|
267
|
+
if (pendingToolCalls.length > 0) {
|
|
268
|
+
for (const tc of pendingToolCalls) {
|
|
269
|
+
if (!existingToolResultIds.has(tc.id)) {
|
|
270
|
+
result.push({
|
|
271
|
+
role: "toolResult",
|
|
272
|
+
toolCallId: tc.id,
|
|
273
|
+
toolName: tc.name,
|
|
274
|
+
content: [{ type: "text", text: "No result provided" }],
|
|
275
|
+
isError: true,
|
|
276
|
+
timestamp: Date.now()
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
pendingToolCalls = [];
|
|
281
|
+
existingToolResultIds = /* @__PURE__ */ new Set();
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
for (let i = 0; i < transformed.length; i++) {
|
|
285
|
+
const msg = transformed[i];
|
|
286
|
+
if (msg.role === "assistant") {
|
|
287
|
+
insertSyntheticToolResults();
|
|
288
|
+
const assistantMsg = msg;
|
|
289
|
+
if (assistantMsg.stopReason === "error" || assistantMsg.stopReason === "aborted") {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const toolCalls = assistantMsg.content.filter((b) => b.type === "toolCall");
|
|
293
|
+
if (toolCalls.length > 0) {
|
|
294
|
+
pendingToolCalls = toolCalls;
|
|
295
|
+
existingToolResultIds = /* @__PURE__ */ new Set();
|
|
296
|
+
}
|
|
297
|
+
result.push(msg);
|
|
298
|
+
} else if (msg.role === "toolResult") {
|
|
299
|
+
existingToolResultIds.add(msg.toolCallId);
|
|
300
|
+
result.push(msg);
|
|
301
|
+
} else if (msg.role === "user") {
|
|
302
|
+
insertSyntheticToolResults();
|
|
303
|
+
result.push(msg);
|
|
304
|
+
} else {
|
|
305
|
+
result.push(msg);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
insertSyntheticToolResults();
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export {
|
|
313
|
+
clampMaxTokensToContext,
|
|
314
|
+
buildBaseOptions,
|
|
315
|
+
adjustMaxTokensForThinking,
|
|
316
|
+
transformMessages
|
|
317
|
+
};
|