Skip to content

Commit ae4c231

Browse files
saphidclaude
andcommitted
fix(opencode): OpenCode 2 titles for image messages skip images the server cannot open
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f6503f0 commit ae4c231

2 files changed

Lines changed: 71 additions & 27 deletions

File tree

‎apps/server/src/textGeneration/OpenCode2TextGeneration.test.ts‎

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { assert, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3-
import { ProviderInstanceId } from "@t3tools/contracts";
3+
import * as NodeURL from "node:url";
4+
import { ChatAttachmentId, ChatImageAttachment, ProviderInstanceId } from "@t3tools/contracts";
45
import * as Deferred from "effect/Deferred";
56
import * as Effect from "effect/Effect";
7+
import * as FileSystem from "effect/FileSystem";
68
import * as Fiber from "effect/Fiber";
79
import * as Layer from "effect/Layer";
810
import { TestClock } from "effect/testing";
911

12+
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
1013
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
1114
import * as OpenCode2AdapterV2Testkit from "../orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts";
1215
import * as OpenCode2Server from "@t3tools/provider-opencode/server/v2/OpenCode2Server";
@@ -16,28 +19,58 @@ import { OPENCODE2_TITLE_GENERATION } from "./OpenCode2TextGeneration.fixture.ts
1619
const layer = TestProviderHost.layer().pipe(Layer.provideMerge(NodeServices.layer));
1720

1821
it.layer(layer)("OpenCode2TextGeneration", (it) => {
19-
it.effect("generates a title in a temporary session on the free tier and removes it", () =>
20-
Effect.gen(function* () {
21-
const server = yield* OpenCode2AdapterV2Testkit.replayServer({
22-
provider: "opencode",
23-
protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
24-
version: "2.0.18",
25-
scenario: "opencode2_title_generation",
26-
entries: [...OPENCODE2_TITLE_GENERATION, { type: "runtime_exit", status: "success" }],
27-
});
28-
const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
29-
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
30-
);
31-
const title = yield* textGeneration.generateThreadTitle({
32-
cwd: process.cwd(),
33-
message: "fix the login redirect loop after oauth",
34-
modelSelection: {
35-
instanceId: ProviderInstanceId.make("opencode"),
36-
model: "opencode/big-pickle",
37-
},
38-
});
39-
assert.equal(title.title, "Fix OAuth Login Redirect Loop");
40-
}).pipe(Effect.scoped),
22+
it.effect.each([false, true] as const)(
23+
"generates a title in a temporary session on the free tier and removes it (external: %s)",
24+
(external) =>
25+
Effect.gen(function* () {
26+
const host = yield* ProviderHost.ProviderHost;
27+
const fileSystem = yield* FileSystem.FileSystem;
28+
const image = (id: string, name: string, mimeType: string) =>
29+
ChatImageAttachment.make({
30+
type: "image",
31+
id: ChatAttachmentId.make(id),
32+
name,
33+
mimeType,
34+
sizeBytes: 4,
35+
});
36+
const screenshot = image("oc2-title-screenshot", "screen.png", "image/png");
37+
const missing = image("oc2-title-missing", "missing.png", "image/png");
38+
const unsupported = image("oc2-title-diagram", "diagram.svg", "image/svg+xml");
39+
const screenshotPath = host.resolveAttachmentPath(screenshot)!;
40+
yield* fileSystem.writeFileString(screenshotPath, "data");
41+
yield* fileSystem.writeFileString(host.resolveAttachmentPath(unsupported)!, "<svg/>");
42+
const prompts: Array<unknown> = [];
43+
const server = yield* OpenCode2AdapterV2Testkit.replayServer(
44+
{
45+
provider: "opencode",
46+
protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
47+
version: "2.0.18",
48+
scenario: "opencode2_title_generation",
49+
entries: [...OPENCODE2_TITLE_GENERATION, { type: "runtime_exit", status: "success" }],
50+
},
51+
{ external, onPrompt: (input) => prompts.push(input) },
52+
);
53+
const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
54+
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
55+
);
56+
const title = yield* textGeneration.generateThreadTitle({
57+
cwd: process.cwd(),
58+
message: "fix the login redirect loop after oauth",
59+
attachments: [screenshot, missing, unsupported],
60+
modelSelection: {
61+
instanceId: ProviderInstanceId.make("opencode"),
62+
model: "opencode/big-pickle",
63+
},
64+
});
65+
assert.equal(title.title, "Fix OAuth Login Redirect Loop");
66+
if (external) {
67+
assert.notProperty(prompts[0], "files");
68+
} else {
69+
assert.deepInclude(prompts[0], {
70+
files: [{ uri: NodeURL.pathToFileURL(screenshotPath).href, name: "screen.png" }],
71+
});
72+
}
73+
}).pipe(Effect.scoped),
4174
);
4275

4376
it.effect("keeps a provider's own error message out of the caller-visible detail", () =>

‎packages/provider-opencode/src/server/v2/textGeneration.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import { AbsolutePath, Location, Model, Provider, Session } from "@opencode/client/effect";
33
import { TextGenerationError } from "@t3tools/contracts";
44
import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
5+
import * as NodeURL from "node:url";
56
import * as Deferred from "effect/Deferred";
67
import * as Effect from "effect/Effect";
8+
import * as FileSystem from "effect/FileSystem";
79
import * as Schema from "effect/Schema";
810
import * as Stream from "effect/Stream";
911

12+
import { isProviderNativeImageAttachment } from "@t3tools/provider-core/server/attachmentPrompt";
1013
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
1114
import * as OpenCode2Server from "./OpenCode2Server.ts";
1215
import * as OpenCodeRuntime from "../OpenCodeRuntime.ts";
@@ -29,6 +32,7 @@ const runOnServer = Effect.fn("Opencode2TextGeneration.runOnServer")(function* (
2932
input: TextGenerationOperations.Request<Schema.Top>,
3033
) {
3134
const { resolveAttachmentPath } = yield* ProviderHost.ProviderHost;
35+
const fileSystem = yield* FileSystem.FileSystem;
3236
const { client } = connection;
3337
const parsed = OpenCodeRuntime.parseOpenCodeModelSlug(input.modelSelection.model);
3438
if (parsed === null) {
@@ -131,11 +135,16 @@ const runOnServer = Effect.fn("Opencode2TextGeneration.runOnServer")(function* (
131135
.remove({ sessionID: session.id })
132136
.pipe(Effect.timeout("5 seconds"), Effect.ignore({ log: true })),
133137
);
134-
const images = (input.attachments ?? []).flatMap((attachment) => {
135-
if (attachment.type !== "image") return [];
138+
const images: Array<{ uri: string; name: string }> = [];
139+
// As in the adapter: an external server may not share T3's attachment directory, and
140+
// OpenCode rejects the whole prompt if an attached file is missing.
141+
for (const attachment of connection.external ? [] : (input.attachments ?? [])) {
142+
if (!isProviderNativeImageAttachment(attachment)) continue;
136143
const path = resolveAttachmentPath(attachment);
137-
return path === null ? [] : [{ uri: `file://${path}`, name: attachment.name }];
138-
});
144+
if (path === null) continue;
145+
const exists = yield* fileSystem.exists(path).pipe(Effect.orElseSucceed(() => false));
146+
if (exists) images.push({ uri: NodeURL.pathToFileURL(path).href, name: attachment.name });
147+
}
139148
yield* client.session.prompt({
140149
sessionID: session.id,
141150
text: input.prompt,
@@ -179,11 +188,13 @@ const runOnServer = Effect.fn("Opencode2TextGeneration.runOnServer")(function* (
179188
export const make = Effect.fn("OpenCode2TextGeneration.make")(function* () {
180189
const server = yield* OpenCode2Server.OpenCode2Server;
181190
const host = yield* ProviderHost.ProviderHost;
191+
const fileSystem = yield* FileSystem.FileSystem;
182192
const run: TextGenerationOperations.Runner = (input) =>
183193
server
184194
.withConnection((connection) => runOnServer(connection, input))
185195
.pipe(
186196
Effect.provideService(ProviderHost.ProviderHost, host),
197+
Effect.provideService(FileSystem.FileSystem, fileSystem),
187198
Effect.mapError((cause) =>
188199
isTextGenerationError(cause)
189200
? cause

0 commit comments

Comments
 (0)