Repository navigation
Expand file tree
/
Copy pathOpenCode2TextGeneration.test.ts
More file actions
224 lines (219 loc) · 8.94 KB
/
Copy pathOpenCode2TextGeneration.test.ts
File metadata and controls
224 lines (219 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { assert, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as NodeURL from "node:url";
import { ChatAttachmentId, ChatImageAttachment, ProviderInstanceId } from "@t3tools/contracts";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import { TestClock } from "effect/testing";
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
import * as OpenCode2AdapterV2Testkit from "../orchestration-v2/Adapters/OpenCode2AdapterV2.testkit.ts";
import * as OpenCode2Server from "@t3tools/provider-opencode/server/v2/OpenCode2Server";
import * as OpenCode2TextGeneration from "@t3tools/provider-opencode/server/v2/textGeneration";
import { OPENCODE2_TITLE_GENERATION } from "./OpenCode2TextGeneration.fixture.ts";
const layer = TestProviderHost.layer().pipe(Layer.provideMerge(NodeServices.layer));
it.layer(layer)("OpenCode2TextGeneration", (it) => {
it.effect.each([false, true] as const)(
"generates a title in a temporary session on the free tier and removes it (external: %s)",
(external) =>
Effect.gen(function* () {
const host = yield* ProviderHost.ProviderHost;
const fileSystem = yield* FileSystem.FileSystem;
const image = (id: string, name: string, mimeType: string) =>
ChatImageAttachment.make({
type: "image",
id: ChatAttachmentId.make(id),
name,
mimeType,
sizeBytes: 4,
});
const screenshot = image("oc2-title-screenshot", "screen.png", "image/png");
const missing = image("oc2-title-missing", "missing.png", "image/png");
const unsupported = image("oc2-title-diagram", "diagram.svg", "image/svg+xml");
const screenshotPath = host.resolveAttachmentPath(screenshot)!;
yield* fileSystem.writeFileString(screenshotPath, "data");
yield* fileSystem.writeFileString(host.resolveAttachmentPath(unsupported)!, "<svg/>");
const prompts: Array<unknown> = [];
const server = yield* OpenCode2AdapterV2Testkit.replayServer(
{
provider: "opencode",
protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
version: "2.0.18",
scenario: "opencode2_title_generation",
entries: [...OPENCODE2_TITLE_GENERATION, { type: "runtime_exit", status: "success" }],
},
{ external, onPrompt: (input) => prompts.push(input) },
);
const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
);
const title = yield* textGeneration.generateThreadTitle({
cwd: process.cwd(),
message: "fix the login redirect loop after oauth",
attachments: [screenshot, missing, unsupported],
modelSelection: {
instanceId: ProviderInstanceId.make("opencode"),
model: "opencode/big-pickle",
},
});
assert.equal(title.title, "Fix OAuth Login Redirect Loop");
if (external) {
assert.notProperty(prompts[0], "files");
} else {
assert.deepInclude(prompts[0], {
files: [{ uri: NodeURL.pathToFileURL(screenshotPath).href, name: "screen.png" }],
});
}
}).pipe(Effect.scoped),
);
it.effect("keeps a provider's own error message out of the caller-visible detail", () =>
Effect.gen(function* () {
const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
const refusal = {
type: "provider.auth",
message:
"Error from provider (Console): OpenCode's free tier can only be used from within OpenCode",
status: 403,
};
const server = yield* OpenCode2AdapterV2Testkit.replayServer({
provider: "opencode",
protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
version: "2.0.18",
scenario: "opencode2_title_generation_refused",
entries: [
subscribe!,
connected!,
create!,
created!,
{
type: "expect_outbound",
frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
},
{
type: "emit_inbound",
frame: {
type: "sdk.response",
operation: "session.prompt",
data: {
data: {
id: "msg_1",
sessionID: sessionId,
time: { created: 1 },
type: "user",
payload: { text: "t" },
delivery: "steer",
},
},
},
},
{
type: "emit_inbound",
frame: {
type: "sdk.event",
event: {
id: "evt_0eb7f9ae0001VCNDHVLnqTfoPG",
created: 1790657403616,
type: "session.execution.failed",
data: { sessionID: sessionId, error: refusal },
durable: { aggregateID: sessionId, seq: 7, version: 1 },
},
},
},
{
type: "expect_outbound",
frame: { type: "session.remove", input: { sessionID: sessionId } },
},
{
type: "emit_inbound",
frame: { type: "sdk.response", operation: "session.remove", data: null },
},
{ type: "runtime_exit", status: "success" },
],
});
const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
);
const failure = yield* textGeneration
.generateThreadTitle({
cwd: process.cwd(),
message: "fix the login redirect loop after oauth",
modelSelection: {
instanceId: ProviderInstanceId.make("opencode"),
model: "opencode/big-pickle",
},
})
.pipe(Effect.flip);
assert.equal(failure.detail, "OpenCode could not generate the text.");
assert.deepEqual(failure.cause, refusal);
}).pipe(Effect.scoped),
);
it.effect("times out when the prompt request and its interrupt never answer", () =>
Effect.gen(function* () {
const [subscribe, connected, create, created] = OPENCODE2_TITLE_GENERATION;
const sessionId = "ses_f0ec85cf6ffebfalpvV2E9nocu";
const prompted = yield* Deferred.make<void>();
const server = yield* OpenCode2AdapterV2Testkit.replayServer(
{
provider: "opencode",
protocol: OpenCode2AdapterV2Testkit.OPENCODE2_HTTP_PROTOCOL,
version: "2.0.18",
scenario: "opencode2_title_generation_stalled",
entries: [
subscribe!,
connected!,
create!,
created!,
{
type: "expect_outbound",
frame: { type: "session.prompt", input: { sessionID: sessionId, text: "<any>" } },
},
{
type: "emit_inbound",
frame: { type: "sdk.response", operation: "session.prompt", data: "<hang>" },
},
{
type: "expect_outbound",
frame: { type: "session.interrupt", input: { sessionID: sessionId } },
},
{
type: "emit_inbound",
frame: { type: "sdk.response", operation: "session.interrupt", data: "<hang>" },
},
{
type: "expect_outbound",
frame: { type: "session.remove", input: { sessionID: sessionId } },
},
{
type: "emit_inbound",
frame: { type: "sdk.response", operation: "session.remove", data: null },
},
{ type: "runtime_exit", status: "success" },
],
},
{ onPrompt: () => Deferred.doneUnsafe(prompted, Effect.void) },
);
const textGeneration = yield* OpenCode2TextGeneration.make().pipe(
Effect.provideService(OpenCode2Server.OpenCode2Server, server),
);
const generation = yield* textGeneration
.generateThreadTitle({
cwd: process.cwd(),
message: "fix the login redirect loop after oauth",
modelSelection: {
instanceId: ProviderInstanceId.make("opencode"),
model: "opencode/big-pickle",
},
})
.pipe(Effect.flip, Effect.forkScoped);
yield* Deferred.await(prompted);
yield* TestClock.adjust("3 minutes");
yield* TestClock.adjust("5 seconds");
const failure = yield* Fiber.join(generation);
assert.equal(failure.detail, "OpenCode did not finish generating in time.");
}).pipe(Effect.scoped),
);
});