Repository navigation
Expand file tree
/
Copy pathlive.mjs
More file actions
142 lines (141 loc) · 5.54 KB
/
Copy pathlive.mjs
File metadata and controls
142 lines (141 loc) · 5.54 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
import assert from "node:assert/strict"
import { mkdir, readFile, writeFile } from "node:fs/promises"
import { join } from "node:path"
import { pathToFileURL } from "node:url"
import { inspect } from "./inspect.mjs"
import { run } from "./process.mjs"
import { restoreAuth } from "/sandbox/auth.mjs"
await run("npm", [
"install",
"--prefix",
"/lab/plugins",
"--omit=dev",
"--ignore-scripts",
"/artifacts/opencode-request-logger-0.1.0.tgz",
"/artifacts/tarquinen-opencode-dcp-3.1.15.tgz",
])
const logger = "/lab/plugins/node_modules/opencode-request-logger"
const dcp = "/lab/plugins/node_modules/@tarquinen/opencode-dcp"
const { createRelay } = await import(pathToFileURL(join(logger, "relay.mjs")))
const root = "/lab/live"
const directory = join(root, "project")
const config = join(root, "config/opencode")
const cli = "/opt/v2/node_modules/.bin/opencode2"
const env = {
...process.env,
HOME: root,
PWD: directory,
XDG_CONFIG_HOME: join(root, "config"),
XDG_DATA_HOME: join(root, "data"),
XDG_STATE_HOME: join(root, "state"),
XDG_CACHE_HOME: join(root, "cache"),
OPENCODE_CONFIG_DIR: config,
OPENCODE_LOG_LEVEL: "DEBUG",
}
await Promise.all([directory, config].map((path) => mkdir(path, { recursive: true })))
for (const transport of ["http", "websocket"]) {
const logs = join(root, transport, "logs")
await mkdir(logs, { recursive: true })
const relay = createRelay({ directory: logs })
await new Promise((resolve) => relay.server.listen(0, "127.0.0.1", resolve))
try {
await writeFile(
join(config, "opencode.json"),
JSON.stringify({
update: "disable",
model: "openai/gpt-5.6-sol",
plugins: [
{ package: dcp },
{
package: logger,
options: {
directory: logs,
relay: `ws://127.0.0.1:${relay.server.address().port}`,
},
},
],
permissions: [{ action: "compress", resource: "*", effect: "allow" }],
providers: {
openai: {
models: { "gpt-5.6-sol": { transport } },
},
},
}),
)
await writeFile(
join(config, "dcp.json"),
JSON.stringify({ autoUpdate: false, debug: true, pruneNotification: "off" }),
)
if (transport === "http") await restoreAuth(2, "/artifacts/auth.json", cli, env)
const prompt =
"LIVE_RAW_PAYLOAD: We are testing DCP in an isolated environment. Everything in this user message is disposable test content. Call compress exactly once on this message using its injected message ID, with summary 'DCP_LIVE_SUMMARY: disposable test fixture. Compression is done; reply with exactly DCP_LIVE_OK and do not use more tools.' After the tool completes, reply with exactly DCP_LIVE_OK. Do not use other tools."
const output = await run(
cli,
["run", "--standalone", "--format", "json", "--model", "openai/gpt-5.6-sol", prompt],
{
cwd: directory,
record: join(root, transport, "run"),
env,
},
)
const { captures, summary } = await inspect(logs)
const requests = captures
.filter(
(entry) =>
entry.kind === "primary" &&
(transport === "http"
? entry.type === "http.request"
: entry.type === "ws.frame" && entry.direction === "request"),
)
.map((entry) => entry.body)
assert.ok(
requests.length >= 2,
"Live compression did not produce a follow-up model request",
)
assert.ok(
requests.some(
(body) =>
JSON.stringify(body.input).includes("DCP_LIVE_SUMMARY") &&
!JSON.stringify(body.input).includes("LIVE_RAW_PAYLOAD"),
),
"Live compression did not replace the original content on the wire",
)
const state = JSON.parse(
await readFile(
join(root, "data/opencode/storage/plugin/dcp", `${summary.sessions[0]}.json`),
"utf8",
),
)
assert.equal(state.prune.messages.activeBlockIds.length, 1)
const result = {
transport,
...summary,
reply: output
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line))
.some(
(entry) => entry.type === "text" && entry.part?.text.trim() === "DCP_LIVE_OK",
),
compression: true,
}
await writeFile(join(root, transport, "result.json"), JSON.stringify(result, null, 2))
console.log(JSON.stringify(result))
assert.ok(
result.reply,
"Live model did not return the expected reply; inspect isolated run output",
)
assert.ok(
captures.some(
(entry) =>
entry.kind === "primary" &&
(transport === "http"
? entry.type === "http.request"
: entry.type === "ws.frame" && entry.direction === "request"),
),
`Live ${transport} traffic was not captured`,
)
} finally {
await relay.close()
}
}