@operad-io/harness-codex 0.1.1 → 0.3.0
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/README.md +9 -1
- package/package.json +5 -5
- package/src/access.ts +59 -71
- package/src/app-server.ts +585 -0
- package/src/index.ts +387 -282
- package/src/install.ts +353 -32
- package/src/log.ts +274 -139
- package/src/models-probe.ts +132 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads `model/list` from a `codex app-server` and prints the choices as JSON:
|
|
3
|
+
* `[{ id, isDefault, ...what the app-server says }]`. Run by `refreshCodexModels` in a short-lived child in
|
|
4
|
+
* the background; the synchronous `models` answer only reads the cache. Not
|
|
5
|
+
* imported by the plugin.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
|
|
12
|
+
/** How long the app-server has to answer `model/list`. */
|
|
13
|
+
const DEFAULT_TIMEOUT_MS = 8_000;
|
|
14
|
+
|
|
15
|
+
function timeoutFrom(value: string | undefined): number {
|
|
16
|
+
const found = Number(value);
|
|
17
|
+
return Number.isFinite(found) && found > 0 ? found : DEFAULT_TIMEOUT_MS;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// The caller passes its timeout as `--timeout-ms <ms>` before the command.
|
|
21
|
+
let timeoutMs = DEFAULT_TIMEOUT_MS;
|
|
22
|
+
if (args[0] === "--timeout-ms") {
|
|
23
|
+
timeoutMs = timeoutFrom(args[1]);
|
|
24
|
+
args.splice(0, 2);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const program = args[0];
|
|
28
|
+
if (program === undefined) {
|
|
29
|
+
process.stdout.write("[]\n");
|
|
30
|
+
process.exit(2);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const child = spawn(program, args.slice(1), { stdio: ["pipe", "pipe", "ignore"] });
|
|
34
|
+
let buffered = "";
|
|
35
|
+
let finished = false;
|
|
36
|
+
|
|
37
|
+
function finish(value: unknown): void {
|
|
38
|
+
if (finished) return;
|
|
39
|
+
finished = true;
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
process.stdout.write(`${JSON.stringify(value)}\n`, () => {
|
|
42
|
+
child.kill("SIGKILL");
|
|
43
|
+
process.exit(0);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const timer = setTimeout(() => finish([]), timeoutMs);
|
|
48
|
+
|
|
49
|
+
function send(message: Record<string, unknown>): void {
|
|
50
|
+
child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", ...message })}\n`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function handle(message: Record<string, unknown>): void {
|
|
54
|
+
if (message["id"] === 1) {
|
|
55
|
+
send({ method: "initialized" });
|
|
56
|
+
send({ id: 2, method: "model/list", params: {} });
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (message["id"] !== 2) return;
|
|
60
|
+
const result = message["result"];
|
|
61
|
+
if (typeof result !== "object" || result === null) {
|
|
62
|
+
finish([]);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const data = Reflect.get(result, "data");
|
|
66
|
+
if (!Array.isArray(data)) {
|
|
67
|
+
finish([]);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const choices: Record<string, unknown>[] = [];
|
|
71
|
+
const fields = [
|
|
72
|
+
"model",
|
|
73
|
+
"displayName",
|
|
74
|
+
"name",
|
|
75
|
+
"provider",
|
|
76
|
+
"modelProvider",
|
|
77
|
+
"contextWindow",
|
|
78
|
+
"modelContextWindow",
|
|
79
|
+
"input",
|
|
80
|
+
"inputModalities",
|
|
81
|
+
"reasoning",
|
|
82
|
+
"tools",
|
|
83
|
+
"webSearch",
|
|
84
|
+
"supportsImages",
|
|
85
|
+
"supportsReasoning",
|
|
86
|
+
"supportsTools",
|
|
87
|
+
"supportsWebSearch",
|
|
88
|
+
"capabilities",
|
|
89
|
+
];
|
|
90
|
+
for (const entry of data) {
|
|
91
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
92
|
+
const id = Reflect.get(entry, "id");
|
|
93
|
+
if (typeof id === "string" && id.length > 0) {
|
|
94
|
+
const choice: Record<string, unknown> = {
|
|
95
|
+
id,
|
|
96
|
+
isDefault: Reflect.get(entry, "isDefault") === true,
|
|
97
|
+
};
|
|
98
|
+
for (const field of fields) {
|
|
99
|
+
const value = Reflect.get(entry, field);
|
|
100
|
+
if (value !== undefined) choice[field] = value;
|
|
101
|
+
}
|
|
102
|
+
choices.push(choice);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
finish(choices);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
child.stdout.on("data", (chunk: Buffer) => {
|
|
109
|
+
buffered += chunk.toString("utf8");
|
|
110
|
+
for (let newline = buffered.indexOf("\n"); newline !== -1; newline = buffered.indexOf("\n")) {
|
|
111
|
+
const line = buffered.slice(0, newline);
|
|
112
|
+
buffered = buffered.slice(newline + 1);
|
|
113
|
+
if (line.trim().length === 0) continue;
|
|
114
|
+
try {
|
|
115
|
+
const value: unknown = JSON.parse(line);
|
|
116
|
+
if (typeof value === "object" && value !== null) handle(value as Record<string, unknown>);
|
|
117
|
+
} catch {
|
|
118
|
+
// The app-server may print a warning; only JSON lines matter.
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
child.on("error", () => finish([]));
|
|
123
|
+
child.on("exit", () => finish([]));
|
|
124
|
+
|
|
125
|
+
send({
|
|
126
|
+
id: 1,
|
|
127
|
+
method: "initialize",
|
|
128
|
+
params: {
|
|
129
|
+
clientInfo: { name: "operad-models", title: "operad", version: "0.0.0" },
|
|
130
|
+
capabilities: { experimentalApi: false, requestAttestation: false },
|
|
131
|
+
},
|
|
132
|
+
});
|