TypeScript
fix(mobile): image-only messages ask the agent to respond, like desktop (#16480)
Ali Younes8d199b4f7b91
diff --git a/apps/mobile/src/lib/composerContext.test.ts b/apps/mobile/src/lib/composerContext.test.tsindex d28b58d055b..3b00f7c5e7c 100644--- a/apps/mobile/src/lib/composerContext.test.ts+++ b/apps/mobile/src/lib/composerContext.test.ts@@ -8,6 +8,7 @@ import {} from "@t3tools/contracts";import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";import {+ ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,collectComposerContextReferences,formatComposerContextReference,projectComposerContextForProvider,@@ -207,6 +208,20 @@ describe("mobile composer context", () => {});describe("host context compatibility", () => {+ it("sends an image-only prompt as the attachment-only prompt and keeps the image", () => {+ const context: OrchestrationMessageContext = { version: 1, records: [image] };+ const reference = formatComposerContextReference(image);+ expect(serializeComposerMessageForServer(reference, context, true)).toEqual({+ text: ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,+ context,+ });+ const withProse = `${reference} what is this?`;+ expect(serializeComposerMessageForServer(withProse, context, true)).toEqual({+ text: withProse,+ context,+ });+ });+it.each(["existing-thread", "new-task"])("serializes %s sends for an older host", (path) => {const pr = pullRequestComposerContext({diff --git a/apps/mobile/src/lib/composerContext.ts b/apps/mobile/src/lib/composerContext.tsindex c5422405c96..f15d9cc300e 100644--- a/apps/mobile/src/lib/composerContext.ts+++ b/apps/mobile/src/lib/composerContext.ts@@ -13,8 +13,10 @@ import {} from "@t3tools/contracts";import * as Schema from "effect/Schema";import {+ ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,collectComposerContextReferences,formatComposerContextReference,+ isImageReferencesOnly,replaceComposerContextReferences,sanitizeComposerContextLabel,} from "@t3tools/shared/composerContextReferences";@@ -229,12 +231,19 @@ export function reidentifyComposerContext(};}-/** Keep queued records canonical; choose the wire format against the host at dispatch time. */+/**+ * Keep queued records canonical; choose the wire format against the host at dispatch time.+ * An image-only prompt goes out as the attachment-only prompt web sends, so the agent is still+ * asked to respond. The images stay in the message's attachments and context records.+ */export function serializeComposerMessageForServer(- text: string,+ composerText: string,context: OrchestrationMessageContext | undefined,supportsInlineMessageContext: boolean,): { text: string; context?: OrchestrationMessageContext } {+ const text = isImageReferencesOnly(composerText)+ ? ATTACHMENT_ONLY_BOOTSTRAP_PROMPT+ : composerText;return supportsInlineMessageContext? { text, ...(context ? { context } : {}) }: { text: serializeLegacyContextMessage({ text, records: context?.records ?? [] }) };diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 20c4d08b5bc..84f5b476a13 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -607,10 +607,8 @@ import {supportsServerUpdateThreadContinuation,} from "../versionSkew";import { useAssetUrls } from "../assets/assetUrls";-import {- ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,- recallableComposerPrompt,-} from "./chat/composerPromptHistory";+import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";+import { recallableComposerPrompt } from "./chat/composerPromptHistory";import { observeResize } from "~/lib/observeResize";const EMPTY_PROVIDERS: ServerProvider[] = [];diff --git a/apps/web/src/components/chat/composerPromptHistory.test.ts b/apps/web/src/components/chat/composerPromptHistory.test.tsindex b3752cf7dbe..deddc467941 100644--- a/apps/web/src/components/chat/composerPromptHistory.test.ts+++ b/apps/web/src/components/chat/composerPromptHistory.test.ts@@ -1,8 +1,8 @@import { describe, expect, it } from "vite-plus/test";+import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";import { buildPlanImplementationPrompt } from "../../proposedPlan";import {- ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,buildComposerPromptHistoryEntries,recallableComposerPrompt,stepComposerPromptHistory,diff --git a/apps/web/src/components/chat/composerPromptHistory.ts b/apps/web/src/components/chat/composerPromptHistory.tsindex f50280f6d24..0977637cdf1 100644--- a/apps/web/src/components/chat/composerPromptHistory.ts+++ b/apps/web/src/components/chat/composerPromptHistory.ts@@ -1,4 +1,7 @@-import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";+import {+ ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,+ collectComposerContextReferences,+} from "@t3tools/shared/composerContextReferences";import { PLAN_IMPLEMENTATION_PROMPT_PREFIX } from "../../proposedPlan";/**@@ -15,10 +18,6 @@ const REVIEW_COMMENT_BLOCK_PATTERN = /<review_comment\b[^>]*>[\s\S]*?<\/review_cconst TRAILING_LEGACY_CONTEXT =/\n*<(terminal_context|element_context|preview_annotation)>\n([\s\S]*?)\n<\/\1>\s*$/;-/** Text sent in place of an empty prompt when a message is attachments only. */-export const ATTACHMENT_ONLY_BOOTSTRAP_PROMPT =- "[User attached one or more files without additional text. Respond using the conversation context and the attached files.]";-export interface ComposerPromptHistoryMessage {readonly id: string;readonly role: string;diff --git a/packages/shared/src/composerContextReferences.test.ts b/packages/shared/src/composerContextReferences.test.tsindex bd078c8f1d4..8b59a6c261b 100644--- a/packages/shared/src/composerContextReferences.test.ts+++ b/packages/shared/src/composerContextReferences.test.ts@@ -6,6 +6,7 @@ import {formatComposerContextHref,formatComposerContextProviderMarker,formatComposerContextReference,+ isImageReferencesOnly,parseComposerContextHref,projectComposerContextForProvider,replaceComposerContextReferences,@@ -86,6 +87,17 @@ describe("labels and reference links", () => {expect(collectComposerContextReferences("[x](https://example.com)")).toEqual([]);});+ it("recognizes a prompt made only of image references", () => {+ const image = "";+ const file = "[notes.md](t3-context://v1/file/ctx_3)";+ expect(isImageReferencesOnly(image)).toBe(true);+ expect(isImageReferencesOnly(` ${image}\n `)).toBe(true);+ expect(isImageReferencesOnly(`${image} what is this?`)).toBe(false);+ expect(isImageReferencesOnly(file)).toBe(false);+ expect(isImageReferencesOnly(`${image} ${file}`)).toBe(false);+ expect(isImageReferencesOnly(" ")).toBe(false);+ });+it("replaces occurrences in place", () => {const text = "a [x](t3-context://v1/skill/ctx_1) b [y](t3-context://v1/file/ctx_2) c";expect(replaceComposerContextReferences(text, (o) => `<${o.contextId}>`)).toBe(diff --git a/packages/shared/src/composerContextReferences.ts b/packages/shared/src/composerContextReferences.tsindex f7877fe937a..ae586489908 100644--- a/packages/shared/src/composerContextReferences.ts+++ b/packages/shared/src/composerContextReferences.ts@@ -92,6 +92,20 @@ export function collectComposerContextReferences(return occurrences;}+/** Text sent in place of an empty prompt when a message is attachments only. */+export const ATTACHMENT_ONLY_BOOTSTRAP_PROMPT =+ "[User attached one or more files without additional text. Respond using the conversation context and the attached files.]";++/** Whether `text` is image references and nothing else, which is how an image-only prompt reads. */+export function isImageReferencesOnly(text: string): boolean {+ const references = collectComposerContextReferences(text);+ return (+ references.length > 0 &&+ references.every((reference) => reference.kind === "image") &&+ replaceComposerContextReferences(text, () => "").trim() === ""+ );+}+export function replaceComposerContextReferences(text: string,replace: (occurrence: ComposerContextReferenceOccurrence) => string,