@caupulican/pi-ai 0.93.1 → 0.93.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/image-support.d.ts +6 -0
- package/dist/image-support.d.ts.map +1 -0
- package/dist/image-support.js +48 -0
- package/dist/image-support.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/transform-messages.d.ts.map +1 -1
- package/dist/providers/transform-messages.js +2 -50
- package/dist/providers/transform-messages.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Api, Message, Model } from "./types.ts";
|
|
2
|
+
export declare const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
3
|
+
export declare const NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
|
|
4
|
+
/** Provider capability projection shared by transport, token admission, and request-body budgeting. */
|
|
5
|
+
export declare function projectMessagesForModelImageSupport<TApi extends Api>(messages: Message[], model: Model<TApi>): Message[];
|
|
6
|
+
//# sourceMappingURL=image-support.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-support.d.ts","sourceRoot":"","sources":["../src/image-support.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAgB,OAAO,EAAE,KAAK,EAAe,MAAM,YAAY,CAAC;AAEjF,eAAO,MAAM,iCAAiC,mDAAmD,CAAC;AAClG,eAAO,MAAM,iCAAiC,wDAAwD,CAAC;AA0BvG,uGAAuG;AACvG,wBAAgB,mCAAmC,CAAC,IAAI,SAAS,GAAG,EACnE,QAAQ,EAAE,OAAO,EAAE,EACnB,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,GAChB,OAAO,EAAE,CAkCX"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
|
|
2
|
+
export const NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
|
|
3
|
+
function replaceUnsupportedImagesWithPlaceholder(content, placeholder, isSupported) {
|
|
4
|
+
let changed = false;
|
|
5
|
+
const result = [];
|
|
6
|
+
let previousWasPlaceholder = false;
|
|
7
|
+
for (const block of content) {
|
|
8
|
+
if (block.type === "image" && !isSupported(block)) {
|
|
9
|
+
changed = true;
|
|
10
|
+
if (!previousWasPlaceholder)
|
|
11
|
+
result.push({ type: "text", text: placeholder });
|
|
12
|
+
previousWasPlaceholder = true;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
result.push(block);
|
|
16
|
+
previousWasPlaceholder = block.type === "text" && block.text === placeholder;
|
|
17
|
+
}
|
|
18
|
+
return changed ? result : content;
|
|
19
|
+
}
|
|
20
|
+
/** Provider capability projection shared by transport, token admission, and request-body budgeting. */
|
|
21
|
+
export function projectMessagesForModelImageSupport(messages, model) {
|
|
22
|
+
if (model.input.includes("image") && !model.supportedImageMimeTypes)
|
|
23
|
+
return messages;
|
|
24
|
+
const supportedMimeTypes = model.input.includes("image")
|
|
25
|
+
? new Set(model.supportedImageMimeTypes?.map((mimeType) => mimeType.toLowerCase()))
|
|
26
|
+
: undefined;
|
|
27
|
+
const isSupported = (block) => supportedMimeTypes?.has(block.mimeType.toLowerCase()) === true;
|
|
28
|
+
let changed = false;
|
|
29
|
+
const projected = messages.map((message) => {
|
|
30
|
+
if (message.role === "user" && Array.isArray(message.content)) {
|
|
31
|
+
const content = replaceUnsupportedImagesWithPlaceholder(message.content, NON_VISION_USER_IMAGE_PLACEHOLDER, isSupported);
|
|
32
|
+
if (content === message.content)
|
|
33
|
+
return message;
|
|
34
|
+
changed = true;
|
|
35
|
+
return { ...message, content };
|
|
36
|
+
}
|
|
37
|
+
if (message.role === "toolResult") {
|
|
38
|
+
const content = replaceUnsupportedImagesWithPlaceholder(message.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER, isSupported);
|
|
39
|
+
if (content === message.content)
|
|
40
|
+
return message;
|
|
41
|
+
changed = true;
|
|
42
|
+
return { ...message, content };
|
|
43
|
+
}
|
|
44
|
+
return message;
|
|
45
|
+
});
|
|
46
|
+
return changed ? projected : messages;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=image-support.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-support.js","sourceRoot":"","sources":["../src/image-support.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,iCAAiC,GAAG,gDAAgD,CAAC;AAClG,MAAM,CAAC,MAAM,iCAAiC,GAAG,qDAAqD,CAAC;AAEvG,SAAS,uCAAuC,CAC/C,OAAuC,EACvC,WAAmB,EACnB,WAA6C;IAE7C,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,MAAM,MAAM,GAAmC,EAAE,CAAC;IAClD,IAAI,sBAAsB,GAAG,KAAK,CAAC;IAEnC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;YACnD,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,CAAC,sBAAsB;gBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC;YAC9E,sBAAsB,GAAG,IAAI,CAAC;YAC9B,SAAS;QACV,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnB,sBAAsB,GAAG,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,CAAC;IAC9E,CAAC;IAED,OAAO,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;AACnC,CAAC;AAED,uGAAuG;AACvG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OAAO,CAAC;YAChD,OAAO,GAAG,IAAI,CAAC;YACf,OAAO,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,CAAC;QAChC,CAAC;QAED,OAAO,OAAO,CAAC;IAChB,CAAC,CAAC,CAAC;IACH,OAAO,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC;AACvC,CAAC","sourcesContent":["import type { Api, ImageContent, Message, Model, TextContent } from \"./types.ts\";\n\nexport const NON_VISION_USER_IMAGE_PLACEHOLDER = \"(image omitted: model does not support images)\";\nexport const NON_VISION_TOOL_IMAGE_PLACEHOLDER = \"(tool image omitted: model does not support images)\";\n\nfunction replaceUnsupportedImagesWithPlaceholder(\n\tcontent: (TextContent | ImageContent)[],\n\tplaceholder: string,\n\tisSupported: (block: ImageContent) => boolean,\n): (TextContent | ImageContent)[] {\n\tlet changed = false;\n\tconst result: (TextContent | ImageContent)[] = [];\n\tlet previousWasPlaceholder = false;\n\n\tfor (const block of content) {\n\t\tif (block.type === \"image\" && !isSupported(block)) {\n\t\t\tchanged = true;\n\t\t\tif (!previousWasPlaceholder) result.push({ type: \"text\", text: placeholder });\n\t\t\tpreviousWasPlaceholder = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tresult.push(block);\n\t\tpreviousWasPlaceholder = block.type === \"text\" && block.text === placeholder;\n\t}\n\n\treturn changed ? result : content;\n}\n\n/** Provider capability projection shared by transport, token admission, and request-body budgeting. */\nexport function projectMessagesForModelImageSupport<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n): Message[] {\n\tif (model.input.includes(\"image\") && !model.supportedImageMimeTypes) return messages;\n\n\tconst supportedMimeTypes = model.input.includes(\"image\")\n\t\t? new Set(model.supportedImageMimeTypes?.map((mimeType) => mimeType.toLowerCase()))\n\t\t: undefined;\n\tconst isSupported = (block: ImageContent): boolean => supportedMimeTypes?.has(block.mimeType.toLowerCase()) === true;\n\tlet changed = false;\n\tconst projected = messages.map((message) => {\n\t\tif (message.role === \"user\" && Array.isArray(message.content)) {\n\t\t\tconst content = replaceUnsupportedImagesWithPlaceholder(\n\t\t\t\tmessage.content,\n\t\t\t\tNON_VISION_USER_IMAGE_PLACEHOLDER,\n\t\t\t\tisSupported,\n\t\t\t);\n\t\t\tif (content === message.content) return message;\n\t\t\tchanged = true;\n\t\t\treturn { ...message, content };\n\t\t}\n\n\t\tif (message.role === \"toolResult\") {\n\t\t\tconst content = replaceUnsupportedImagesWithPlaceholder(\n\t\t\t\tmessage.content,\n\t\t\t\tNON_VISION_TOOL_IMAGE_PLACEHOLDER,\n\t\t\t\tisSupported,\n\t\t\t);\n\t\t\tif (content === message.content) return message;\n\t\t\tchanged = true;\n\t\t\treturn { ...message, content };\n\t\t}\n\n\t\treturn message;\n\t});\n\treturn changed ? projected : messages;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { Type } from "typebox";
|
|
|
3
3
|
export * from "./api-registry.ts";
|
|
4
4
|
export * from "./env-api-keys.ts";
|
|
5
5
|
export * from "./image-models.ts";
|
|
6
|
+
export * from "./image-support.ts";
|
|
6
7
|
export * from "./images.ts";
|
|
7
8
|
export * from "./images-api-registry.ts";
|
|
8
9
|
export * from "./models.ts";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,aAAa,CAAC;AAC5B,YAAY,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AAC5F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AAC;AACtC,YAAY,EACX,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACnB,WAAW,EACX,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,sBAAsB,EACtB,iBAAiB,EACjB,iBAAiB,GACjB,MAAM,wBAAwB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sCAAsC,CAAC;AACrD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export { Type } from "typebox";
|
|
|
2
2
|
export * from "./api-registry.js";
|
|
3
3
|
export * from "./env-api-keys.js";
|
|
4
4
|
export * from "./image-models.js";
|
|
5
|
+
export * from "./image-support.js";
|
|
5
6
|
export * from "./images.js";
|
|
6
7
|
export * from "./images-api-registry.js";
|
|
7
8
|
export * from "./models.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,aAAa,CAAC;AAI5B,cAAc,qBAAqB,CAAC;AAIpC,cAAc,yCAAyC,CAAC;AAExD,cAAc,qCAAqC,CAAC;AAOpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AActC,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sCAAsC,CAAC;AACrD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC","sourcesContent":["export type { Static, TSchema } from \"typebox\";\nexport { Type } from \"typebox\";\n\nexport * from \"./api-registry.ts\";\nexport * from \"./env-api-keys.ts\";\nexport * from \"./image-models.ts\";\nexport * from \"./images.ts\";\nexport * from \"./images-api-registry.ts\";\nexport * from \"./models.ts\";\nexport type { BedrockOptions, BedrockThinkingDisplay } from \"./providers/amazon-bedrock.ts\";\nexport type { AnthropicEffort, AnthropicOptions, AnthropicThinkingDisplay } from \"./providers/anthropic.ts\";\nexport type { AzureOpenAIResponsesOptions } from \"./providers/azure-openai-responses.ts\";\nexport * from \"./providers/faux.ts\";\nexport type { GoogleOptions } from \"./providers/google.ts\";\nexport type { GoogleThinkingLevel } from \"./providers/google-shared.ts\";\nexport type { GoogleVertexOptions } from \"./providers/google-vertex.ts\";\nexport * from \"./providers/images/register-builtins.ts\";\nexport type { MistralOptions } from \"./providers/mistral.ts\";\nexport * from \"./providers/openai-codex-account.ts\";\nexport type {\n\tOpenAICodexResponsesOptions,\n\tOpenAICodexWebSocketDebugStats,\n} from \"./providers/openai-codex-responses.ts\";\nexport type { OpenAICompletionsOptions } from \"./providers/openai-completions.ts\";\nexport type { OpenAIResponsesOptions } from \"./providers/openai-responses.ts\";\nexport * from \"./providers/register-builtins.ts\";\nexport * from \"./session-resources.ts\";\nexport * from \"./stream.ts\";\nexport * from \"./types.ts\";\nexport * from \"./usage.ts\";\nexport * from \"./utils/diagnostics.ts\";\nexport * from \"./utils/event-stream.ts\";\nexport * from \"./utils/json-parse.ts\";\nexport type {\n\tOAuthAuthInfo,\n\tOAuthCredentials,\n\tOAuthDeviceCodeInfo,\n\tOAuthLoginCallbacks,\n\tOAuthPrompt,\n\tOAuthProvider,\n\tOAuthProviderId,\n\tOAuthProviderInfo,\n\tOAuthProviderInterface,\n\tOAuthSelectOption,\n\tOAuthSelectPrompt,\n} from \"./utils/oauth/types.ts\";\nexport * from \"./utils/overflow.ts\";\nexport * from \"./utils/provider-retry.ts\";\nexport * from \"./utils/streaming-lines.ts\";\nexport * from \"./utils/tool-repair/registry.ts\";\nexport * from \"./utils/tool-repair/replay.ts\";\nexport * from \"./utils/tool-repair/text-protocol.ts\";\nexport * from \"./utils/typebox-helpers.ts\";\nexport * from \"./utils/uuid.ts\";\nexport * from \"./utils/validation.ts\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,aAAa,CAAC;AAI5B,cAAc,qBAAqB,CAAC;AAIpC,cAAc,yCAAyC,CAAC;AAExD,cAAc,qCAAqC,CAAC;AAOpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AActC,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sCAAsC,CAAC;AACrD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC","sourcesContent":["export type { Static, TSchema } from \"typebox\";\nexport { Type } from \"typebox\";\n\nexport * from \"./api-registry.ts\";\nexport * from \"./env-api-keys.ts\";\nexport * from \"./image-models.ts\";\nexport * from \"./image-support.ts\";\nexport * from \"./images.ts\";\nexport * from \"./images-api-registry.ts\";\nexport * from \"./models.ts\";\nexport type { BedrockOptions, BedrockThinkingDisplay } from \"./providers/amazon-bedrock.ts\";\nexport type { AnthropicEffort, AnthropicOptions, AnthropicThinkingDisplay } from \"./providers/anthropic.ts\";\nexport type { AzureOpenAIResponsesOptions } from \"./providers/azure-openai-responses.ts\";\nexport * from \"./providers/faux.ts\";\nexport type { GoogleOptions } from \"./providers/google.ts\";\nexport type { GoogleThinkingLevel } from \"./providers/google-shared.ts\";\nexport type { GoogleVertexOptions } from \"./providers/google-vertex.ts\";\nexport * from \"./providers/images/register-builtins.ts\";\nexport type { MistralOptions } from \"./providers/mistral.ts\";\nexport * from \"./providers/openai-codex-account.ts\";\nexport type {\n\tOpenAICodexResponsesOptions,\n\tOpenAICodexWebSocketDebugStats,\n} from \"./providers/openai-codex-responses.ts\";\nexport type { OpenAICompletionsOptions } from \"./providers/openai-completions.ts\";\nexport type { OpenAIResponsesOptions } from \"./providers/openai-responses.ts\";\nexport * from \"./providers/register-builtins.ts\";\nexport * from \"./session-resources.ts\";\nexport * from \"./stream.ts\";\nexport * from \"./types.ts\";\nexport * from \"./usage.ts\";\nexport * from \"./utils/diagnostics.ts\";\nexport * from \"./utils/event-stream.ts\";\nexport * from \"./utils/json-parse.ts\";\nexport type {\n\tOAuthAuthInfo,\n\tOAuthCredentials,\n\tOAuthDeviceCodeInfo,\n\tOAuthLoginCallbacks,\n\tOAuthPrompt,\n\tOAuthProvider,\n\tOAuthProviderId,\n\tOAuthProviderInfo,\n\tOAuthProviderInterface,\n\tOAuthSelectOption,\n\tOAuthSelectPrompt,\n} from \"./utils/oauth/types.ts\";\nexport * from \"./utils/overflow.ts\";\nexport * from \"./utils/provider-retry.ts\";\nexport * from \"./utils/streaming-lines.ts\";\nexport * from \"./utils/tool-repair/registry.ts\";\nexport * from \"./utils/tool-repair/replay.ts\";\nexport * from \"./utils/tool-repair/text-protocol.ts\";\nexport * from \"./utils/typebox-helpers.ts\";\nexport * from \"./utils/uuid.ts\";\nexport * from \"./utils/validation.ts\";\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-messages.d.ts","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transform-messages.d.ts","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,GAAG,EACH,gBAAgB,EAChB,YAAY,EACZ,OAAO,EACP,KAAK,EACL,WAAW,EAGX,MAAM,aAAa,CAAC;AAErB,eAAO,MAAM,4BAA4B,uMAC4I,CAAC;AAEtL,iFAAiF;AACjF,wBAAgB,eAAe,CAAC,OAAO,EAAE,SAAS,CAAC,WAAW,GAAG,YAAY,CAAC,EAAE,GAAG,MAAM,CAaxF;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,SAAS,GAAG,EACjD,QAAQ,EAAE,OAAO,EAAE,EACnB,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,mBAAmB,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,gBAAgB,KAAK,MAAM,GACxF,OAAO,EAAE,CAgKX"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
const NON_VISION_TOOL_IMAGE_PLACEHOLDER = "(tool image omitted: model does not support images)";
|
|
1
|
+
import { projectMessagesForModelImageSupport } from "../image-support.js";
|
|
3
2
|
export const INTERRUPTED_TOOL_RESULT_TEXT = '[harness] {"state":"interrupted","outcome":"unknown","reason":"missing_terminal_result","next_action":"Inspect side effects before retrying; do not assume the operation failed."}';
|
|
4
3
|
/** Reuse the original immutable string for the common single-text-block case. */
|
|
5
4
|
export function joinTextContent(content) {
|
|
@@ -18,53 +17,6 @@ export function joinTextContent(content) {
|
|
|
18
17
|
}
|
|
19
18
|
return parts?.join("\n") ?? first ?? "";
|
|
20
19
|
}
|
|
21
|
-
function replaceUnsupportedImagesWithPlaceholder(content, placeholder, isSupported) {
|
|
22
|
-
const result = [];
|
|
23
|
-
let previousWasPlaceholder = false;
|
|
24
|
-
for (const block of content) {
|
|
25
|
-
if (block.type === "image" && !isSupported(block)) {
|
|
26
|
-
if (!previousWasPlaceholder) {
|
|
27
|
-
result.push({ type: "text", text: placeholder });
|
|
28
|
-
}
|
|
29
|
-
previousWasPlaceholder = true;
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
result.push(block);
|
|
33
|
-
previousWasPlaceholder = block.type === "text" && block.text === placeholder;
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
function createImageSupportPredicate(model) {
|
|
38
|
-
if (!model.input.includes("image")) {
|
|
39
|
-
return () => false;
|
|
40
|
-
}
|
|
41
|
-
if (!model.supportedImageMimeTypes) {
|
|
42
|
-
return () => true;
|
|
43
|
-
}
|
|
44
|
-
const supportedMimeTypes = new Set(model.supportedImageMimeTypes.map((mimeType) => mimeType.toLowerCase()));
|
|
45
|
-
return (block) => supportedMimeTypes.has(block.mimeType.toLowerCase());
|
|
46
|
-
}
|
|
47
|
-
function downgradeUnsupportedImages(messages, model) {
|
|
48
|
-
if (model.input.includes("image") && !model.supportedImageMimeTypes) {
|
|
49
|
-
return messages;
|
|
50
|
-
}
|
|
51
|
-
const isSupportedImage = createImageSupportPredicate(model);
|
|
52
|
-
return messages.map((msg) => {
|
|
53
|
-
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
54
|
-
return {
|
|
55
|
-
...msg,
|
|
56
|
-
content: replaceUnsupportedImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER, isSupportedImage),
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
if (msg.role === "toolResult") {
|
|
60
|
-
return {
|
|
61
|
-
...msg,
|
|
62
|
-
content: replaceUnsupportedImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER, isSupportedImage),
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
return msg;
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
20
|
/**
|
|
69
21
|
* Normalize tool call ID for cross-provider compatibility.
|
|
70
22
|
* OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.
|
|
@@ -73,7 +25,7 @@ function downgradeUnsupportedImages(messages, model) {
|
|
|
73
25
|
export function transformMessages(messages, model, normalizeToolCallId) {
|
|
74
26
|
// Build a map of original tool call IDs to normalized IDs
|
|
75
27
|
const toolCallIdMap = new Map();
|
|
76
|
-
const imageAwareMessages =
|
|
28
|
+
const imageAwareMessages = projectMessagesForModelImageSupport(messages, model);
|
|
77
29
|
// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)
|
|
78
30
|
const transformed = imageAwareMessages.map((msg) => {
|
|
79
31
|
// User messages pass through unchanged
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-messages.js","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AAWA,MAAM,iCAAiC,GAAG,gDAAgD,CAAC;AAC3F,MAAM,iCAAiC,GAAG,qDAAqD,CAAC;AAChG,MAAM,CAAC,MAAM,4BAA4B,GACxC,oLAAoL,CAAC;AAEtL,iFAAiF;AACjF,MAAM,UAAU,eAAe,CAAC,OAAgD;IAC/E,IAAI,KAAyB,CAAC;IAC9B,IAAI,KAA2B,CAAC;IAChC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,I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AAU,CAAC,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAChC,kFAAkF;YAClF,0BAA0B,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,+EAA+E;IAC/E,0BAA0B,EAAE,CAAC;IAE7B,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import type {\n\tApi,\n\tAssistantMessage,\n\tImageContent,\n\tMessage,\n\tModel,\n\tTextContent,\n\tToolCall,\n\tToolResultMessage,\n} from \"../types.ts\";\n\nconst NON_VISION_USER_IMAGE_PLACEHOLDER = \"(image omitted: model does not support images)\";\nconst NON_VISION_TOOL_IMAGE_PLACEHOLDER = \"(tool image omitted: model does not support images)\";\nexport const INTERRUPTED_TOOL_RESULT_TEXT =\n\t'[harness] {\"state\":\"interrupted\",\"outcome\":\"unknown\",\"reason\":\"missing_terminal_result\",\"next_action\":\"Inspect side effects before retrying; do not assume the operation failed.\"}';\n\n/** Reuse the original immutable string for the common single-text-block case. */\nexport function joinTextContent(content: readonly (TextContent | ImageContent)[]): string {\n\tlet first: string | undefined;\n\tlet parts: string[] | undefined;\n\tfor (const block of content) {\n\t\tif (block.type !== \"text\") continue;\n\t\tif (first === undefined) {\n\t\t\tfirst = block.text;\n\t\t} else {\n\t\t\tparts ??= [first];\n\t\t\tparts.push(block.text);\n\t\t}\n\t}\n\treturn parts?.join(\"\\n\") ?? first ?? \"\";\n}\n\nfunction replaceUnsupportedImagesWithPlaceholder(\n\tcontent: (TextContent | ImageContent)[],\n\tplaceholder: string,\n\tisSupported: (block: ImageContent) => boolean,\n): (TextContent | ImageContent)[] {\n\tconst result: (TextContent | ImageContent)[] = [];\n\tlet previousWasPlaceholder = false;\n\n\tfor (const block of content) {\n\t\tif (block.type === \"image\" && !isSupported(block)) {\n\t\t\tif (!previousWasPlaceholder) {\n\t\t\t\tresult.push({ type: \"text\", text: placeholder });\n\t\t\t}\n\t\t\tpreviousWasPlaceholder = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tresult.push(block);\n\t\tpreviousWasPlaceholder = block.type === \"text\" && block.text === placeholder;\n\t}\n\n\treturn result;\n}\n\nfunction createImageSupportPredicate<TApi extends Api>(model: Model<TApi>): (block: ImageContent) => boolean {\n\tif (!model.input.includes(\"image\")) {\n\t\treturn () => false;\n\t}\n\n\tif (!model.supportedImageMimeTypes) {\n\t\treturn () => true;\n\t}\n\n\tconst supportedMimeTypes = new Set(model.supportedImageMimeTypes.map((mimeType) => mimeType.toLowerCase()));\n\treturn (block) => supportedMimeTypes.has(block.mimeType.toLowerCase());\n}\n\nfunction downgradeUnsupportedImages<TApi extends Api>(messages: Message[], model: Model<TApi>): Message[] {\n\tif (model.input.includes(\"image\") && !model.supportedImageMimeTypes) {\n\t\treturn messages;\n\t}\n\n\tconst isSupportedImage = createImageSupportPredicate(model);\n\n\treturn messages.map((msg) => {\n\t\tif (msg.role === \"user\" && Array.isArray(msg.content)) {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceUnsupportedImagesWithPlaceholder(\n\t\t\t\t\tmsg.content,\n\t\t\t\t\tNON_VISION_USER_IMAGE_PLACEHOLDER,\n\t\t\t\t\tisSupportedImage,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\tif (msg.role === \"toolResult\") {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceUnsupportedImagesWithPlaceholder(\n\t\t\t\t\tmsg.content,\n\t\t\t\t\tNON_VISION_TOOL_IMAGE_PLACEHOLDER,\n\t\t\t\t\tisSupportedImage,\n\t\t\t\t),\n\t\t\t};\n\t\t}\n\n\t\treturn msg;\n\t});\n}\n\n/**\n * Normalize tool call ID for cross-provider compatibility.\n * OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.\n * Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).\n */\nexport function transformMessages<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n): Message[] {\n\t// Build a map of original tool call IDs to normalized IDs\n\tconst toolCallIdMap = new Map<string, string>();\n\tconst imageAwareMessages = downgradeUnsupportedImages(messages, model);\n\n\t// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)\n\tconst transformed = imageAwareMessages.map((msg) => {\n\t\t// User messages pass through unchanged\n\t\tif (msg.role === \"user\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Handle toolResult messages - normalize toolCallId if we have a mapping\n\t\tif (msg.role === \"toolResult\") {\n\t\t\tconst normalizedId = toolCallIdMap.get(msg.toolCallId);\n\t\t\tif (normalizedId && normalizedId !== msg.toolCallId) {\n\t\t\t\treturn { ...msg, toolCallId: normalizedId };\n\t\t\t}\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tconst isSameModel =\n\t\t\t\tassistantMsg.provider === model.provider &&\n\t\t\t\tassistantMsg.api === model.api &&\n\t\t\t\tassistantMsg.model === model.id;\n\n\t\t\tconst transformedContent = assistantMsg.content.flatMap((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Redacted thinking is opaque encrypted content, only valid for the same model.\n\t\t\t\t\t// Drop it for cross-model to avoid API errors.\n\t\t\t\t\tif (block.redacted) {\n\t\t\t\t\t\treturn isSameModel ? block : [];\n\t\t\t\t\t}\n\t\t\t\t\t// For same model: keep thinking blocks with signatures (needed for replay)\n\t\t\t\t\t// even if the thinking text is empty (OpenAI encrypted reasoning)\n\t\t\t\t\tif (isSameModel && block.thinkingSignature) return block;\n\t\t\t\t\t// Skip empty thinking blocks, convert others to plain text\n\t\t\t\t\tif (!block.thinking || block.thinking.trim() === \"\") return [];\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"toolCall\") {\n\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\tlet normalizedToolCall: ToolCall = toolCall;\n\n\t\t\t\t\tif (!isSameModel && toolCall.thoughtSignature) {\n\t\t\t\t\t\tnormalizedToolCall = { ...toolCall };\n\t\t\t\t\t\tdelete (normalizedToolCall as { thoughtSignature?: string }).thoughtSignature;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!isSameModel && normalizeToolCallId) {\n\t\t\t\t\t\tconst normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);\n\t\t\t\t\t\tif (normalizedId !== toolCall.id) {\n\t\t\t\t\t\t\ttoolCallIdMap.set(toolCall.id, normalizedId);\n\t\t\t\t\t\t\tnormalizedToolCall = { ...normalizedToolCall, id: normalizedId };\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn normalizedToolCall;\n\t\t\t\t}\n\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\t\treturn msg;\n\t});\n\n\t// Second pass: insert synthetic empty tool results for orphaned tool calls\n\t// This preserves thinking signatures and satisfies API requirements\n\tconst result: Message[] = [];\n\tlet pendingToolCalls: ToolCall[] = [];\n\tlet existingToolResultIds = new Set<string>();\n\tconst droppedToolCallIds = new Set<string>();\n\tconst insertSyntheticToolResults = () => {\n\t\tif (pendingToolCalls.length > 0) {\n\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\tresult.push({\n\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: INTERRUPTED_TOOL_RESULT_TEXT }],\n\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t}\n\t\t\t}\n\t\t\tpendingToolCalls = [];\n\t\t\texistingToolResultIds = new Set();\n\t\t}\n\t};\n\n\tfor (let i = 0; i < transformed.length; i++) {\n\t\tconst msg = transformed[i];\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\t// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now\n\t\t\tinsertSyntheticToolResults();\n\n\t\t\t// Skip errored/aborted assistant messages entirely.\n\t\t\t// These are incomplete turns that shouldn't be replayed:\n\t\t\t// - May have partial content (reasoning without message, incomplete tool calls)\n\t\t\t// - Replaying them can cause API errors (e.g., OpenAI \"reasoning without following item\")\n\t\t\t// - The model should retry from the last valid state\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tif (assistantMsg.stopReason === \"error\" || assistantMsg.stopReason === \"aborted\") {\n\t\t\t\tconst skippedToolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\t\tfor (const toolCall of skippedToolCalls) {\n\t\t\t\t\tdroppedToolCallIds.add(toolCall.id);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Track tool calls from this assistant message\n\t\t\tconst toolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\tif (toolCalls.length > 0) {\n\t\t\t\tpendingToolCalls = toolCalls;\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\tif (droppedToolCallIds.has(msg.toolCallId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\texistingToolResultIds.add(msg.toolCallId);\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"user\") {\n\t\t\t// User message interrupts tool flow - insert synthetic results for orphaned calls\n\t\t\tinsertSyntheticToolResults();\n\t\t\tresult.push(msg);\n\t\t} else {\n\t\t\tresult.push(msg);\n\t\t}\n\t}\n\n\t// If the conversation ends with unresolved tool calls, synthesize results now.\n\tinsertSyntheticToolResults();\n\n\treturn result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"transform-messages.js","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAE,MAAM,qBAAqB,CAAC;AAY1E,MAAM,CAAC,MAAM,4BAA4B,GACxC,oLAAoL,CAAC;AAEtL,iFAAiF;AACjF,MAAM,UAAU,eAAe,CAAC,OAAgD;IAC/E,IAAI,KAAyB,CAAC;IAC9B,IAAI,KAA2B,CAAC;IAChC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;YAAE,SAAS;QACpC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACzB,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC;QACpB,CAAC;aAAM,CAAC;YACP,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC;YAClB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;IACF,CAAC;IACD,OAAO,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,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BAAqB,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAChC,kFAAkF;YAClF,0BAA0B,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,+EAA+E;IAC/E,0BAA0B,EAAE,CAAC;IAE7B,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import { projectMessagesForModelImageSupport } from \"../image-support.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tImageContent,\n\tMessage,\n\tModel,\n\tTextContent,\n\tToolCall,\n\tToolResultMessage,\n} from \"../types.ts\";\n\nexport const INTERRUPTED_TOOL_RESULT_TEXT =\n\t'[harness] {\"state\":\"interrupted\",\"outcome\":\"unknown\",\"reason\":\"missing_terminal_result\",\"next_action\":\"Inspect side effects before retrying; do not assume the operation failed.\"}';\n\n/** Reuse the original immutable string for the common single-text-block case. */\nexport function joinTextContent(content: readonly (TextContent | ImageContent)[]): string {\n\tlet first: string | undefined;\n\tlet parts: string[] | undefined;\n\tfor (const block of content) {\n\t\tif (block.type !== \"text\") continue;\n\t\tif (first === undefined) {\n\t\t\tfirst = block.text;\n\t\t} else {\n\t\t\tparts ??= [first];\n\t\t\tparts.push(block.text);\n\t\t}\n\t}\n\treturn parts?.join(\"\\n\") ?? first ?? \"\";\n}\n\n/**\n * Normalize tool call ID for cross-provider compatibility.\n * OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.\n * Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).\n */\nexport function transformMessages<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n): Message[] {\n\t// Build a map of original tool call IDs to normalized IDs\n\tconst toolCallIdMap = new Map<string, string>();\n\tconst imageAwareMessages = projectMessagesForModelImageSupport(messages, model);\n\n\t// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)\n\tconst transformed = imageAwareMessages.map((msg) => {\n\t\t// User messages pass through unchanged\n\t\tif (msg.role === \"user\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Handle toolResult messages - normalize toolCallId if we have a mapping\n\t\tif (msg.role === \"toolResult\") {\n\t\t\tconst normalizedId = toolCallIdMap.get(msg.toolCallId);\n\t\t\tif (normalizedId && normalizedId !== msg.toolCallId) {\n\t\t\t\treturn { ...msg, toolCallId: normalizedId };\n\t\t\t}\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tconst isSameModel =\n\t\t\t\tassistantMsg.provider === model.provider &&\n\t\t\t\tassistantMsg.api === model.api &&\n\t\t\t\tassistantMsg.model === model.id;\n\n\t\t\tconst transformedContent = assistantMsg.content.flatMap((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Redacted thinking is opaque encrypted content, only valid for the same model.\n\t\t\t\t\t// Drop it for cross-model to avoid API errors.\n\t\t\t\t\tif (block.redacted) {\n\t\t\t\t\t\treturn isSameModel ? block : [];\n\t\t\t\t\t}\n\t\t\t\t\t// For same model: keep thinking blocks with signatures (needed for replay)\n\t\t\t\t\t// even if the thinking text is empty (OpenAI encrypted reasoning)\n\t\t\t\t\tif (isSameModel && block.thinkingSignature) return block;\n\t\t\t\t\t// Skip empty thinking blocks, convert others to plain text\n\t\t\t\t\tif (!block.thinking || block.thinking.trim() === \"\") return [];\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"toolCall\") {\n\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\tlet normalizedToolCall: ToolCall = toolCall;\n\n\t\t\t\t\tif (!isSameModel && toolCall.thoughtSignature) {\n\t\t\t\t\t\tnormalizedToolCall = { ...toolCall };\n\t\t\t\t\t\tdelete (normalizedToolCall as { thoughtSignature?: string }).thoughtSignature;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!isSameModel && normalizeToolCallId) {\n\t\t\t\t\t\tconst normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);\n\t\t\t\t\t\tif (normalizedId !== toolCall.id) {\n\t\t\t\t\t\t\ttoolCallIdMap.set(toolCall.id, normalizedId);\n\t\t\t\t\t\t\tnormalizedToolCall = { ...normalizedToolCall, id: normalizedId };\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn normalizedToolCall;\n\t\t\t\t}\n\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\t\treturn msg;\n\t});\n\n\t// Second pass: insert synthetic empty tool results for orphaned tool calls\n\t// This preserves thinking signatures and satisfies API requirements\n\tconst result: Message[] = [];\n\tlet pendingToolCalls: ToolCall[] = [];\n\tlet existingToolResultIds = new Set<string>();\n\tconst droppedToolCallIds = new Set<string>();\n\tconst insertSyntheticToolResults = () => {\n\t\tif (pendingToolCalls.length > 0) {\n\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\tresult.push({\n\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: INTERRUPTED_TOOL_RESULT_TEXT }],\n\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t}\n\t\t\t}\n\t\t\tpendingToolCalls = [];\n\t\t\texistingToolResultIds = new Set();\n\t\t}\n\t};\n\n\tfor (let i = 0; i < transformed.length; i++) {\n\t\tconst msg = transformed[i];\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\t// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now\n\t\t\tinsertSyntheticToolResults();\n\n\t\t\t// Skip errored/aborted assistant messages entirely.\n\t\t\t// These are incomplete turns that shouldn't be replayed:\n\t\t\t// - May have partial content (reasoning without message, incomplete tool calls)\n\t\t\t// - Replaying them can cause API errors (e.g., OpenAI \"reasoning without following item\")\n\t\t\t// - The model should retry from the last valid state\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tif (assistantMsg.stopReason === \"error\" || assistantMsg.stopReason === \"aborted\") {\n\t\t\t\tconst skippedToolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\t\tfor (const toolCall of skippedToolCalls) {\n\t\t\t\t\tdroppedToolCallIds.add(toolCall.id);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Track tool calls from this assistant message\n\t\t\tconst toolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\tif (toolCalls.length > 0) {\n\t\t\t\tpendingToolCalls = toolCalls;\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\tif (droppedToolCallIds.has(msg.toolCallId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\texistingToolResultIds.add(msg.toolCallId);\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"user\") {\n\t\t\t// User message interrupts tool flow - insert synthetic results for orphaned calls\n\t\t\tinsertSyntheticToolResults();\n\t\t\tresult.push(msg);\n\t\t} else {\n\t\t\tresult.push(msg);\n\t\t}\n\t}\n\n\t// If the conversation ends with unresolved tool calls, synthesize results now.\n\tinsertSyntheticToolResults();\n\n\treturn result;\n}\n"]}
|