@operad-io/harness-pi 0.1.1 → 0.2.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 +4 -0
- package/package.json +14 -6
- package/src/auth-file.ts +350 -0
- package/src/index.ts +394 -382
- package/src/login-protocol.ts +64 -0
- package/src/login-worker.ts +205 -0
- package/src/login.ts +395 -0
- package/src/model-list.ts +478 -0
- package/src/pi-install.ts +95 -64
- package/src/protocol.ts +86 -94
- package/src/worker.ts +301 -399
package/README.md
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
This package is part of [operad](https://github.com/operad-io/operad). Most people install only [`@operad-io/cli`](https://www.npmjs.com/package/@operad-io/cli).
|
|
6
6
|
|
|
7
|
+
## Signed in
|
|
8
|
+
|
|
9
|
+
The plugin's `signedIn` answer, which `GET /v1/plugins` carries for clients, reports whether pi's auth file (`authPath`, `~/.pi/agent/auth.json` by default) holds a credential for the provider pi will use: the provider of the configured `model`, or pi's `defaultProvider`, or any stored credential when neither names one. Only the presence of that credential is read; the secret is never read into operad, kept or returned. A missing file is `false`; a file that cannot be read or parsed is absent.
|
|
10
|
+
|
|
7
11
|
## Documentation
|
|
8
12
|
|
|
9
13
|
- User guide: [docs/user/dev.md](https://github.com/operad-io/operad/blob/main/docs/user/dev.md)
|
package/package.json
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/harness-pi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/access-match": "0.
|
|
10
|
-
"@operad-io/engine": "0.
|
|
11
|
-
"@operad-io/harness-process": "0.
|
|
12
|
-
"@operad-io/mcp": "0.
|
|
9
|
+
"@operad-io/access-match": "0.2.0",
|
|
10
|
+
"@operad-io/engine": "0.2.0",
|
|
11
|
+
"@operad-io/harness-process": "0.2.0",
|
|
12
|
+
"@operad-io/mcp": "0.2.0"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@earendil-works/pi-coding-agent": "1.6.0"
|
|
16
|
+
},
|
|
17
|
+
"peerDependenciesMeta": {
|
|
18
|
+
"@earendil-works/pi-coding-agent": {
|
|
19
|
+
"optional": true
|
|
20
|
+
}
|
|
13
21
|
},
|
|
14
22
|
"devDependencies": {
|
|
15
|
-
"@earendil-works/pi-coding-agent": "1.
|
|
23
|
+
"@earendil-works/pi-coding-agent": "1.6.0"
|
|
16
24
|
},
|
|
17
25
|
"description": "operad: the pi harness, a client of the pi coding agent library",
|
|
18
26
|
"repository": {
|
package/src/auth-file.ts
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads pi's auth file as a shape, never as values: a credential's secret
|
|
3
|
+
* strings are replaced as they stream past, so no secret reaches a string or
|
|
4
|
+
* an object operad keeps. Only the top-level keys and which values are objects
|
|
5
|
+
* survive, which is all `signedIn` needs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { isMissingFile, isRecord } from "@operad-io/engine";
|
|
9
|
+
import { open } from "node:fs/promises";
|
|
10
|
+
|
|
11
|
+
const QUOTE = 0x22;
|
|
12
|
+
const COMMA = 0x2c;
|
|
13
|
+
const COLON = 0x3a;
|
|
14
|
+
const OPEN_ARRAY = 0x5b;
|
|
15
|
+
const BACKSLASH = 0x5c;
|
|
16
|
+
const CLOSE_ARRAY = 0x5d;
|
|
17
|
+
const OPEN_OBJECT = 0x7b;
|
|
18
|
+
const CLOSE_OBJECT = 0x7d;
|
|
19
|
+
|
|
20
|
+
interface Frame {
|
|
21
|
+
kind: "object" | "array";
|
|
22
|
+
state: "key" | "colon" | "value" | "comma";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isSpace(byte: number): boolean {
|
|
26
|
+
return byte === 0x20 || byte === 0x09 || byte === 0x0a || byte === 0x0d;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isHex(byte: number): boolean {
|
|
30
|
+
return (
|
|
31
|
+
(byte >= 0x30 && byte <= 0x39) ||
|
|
32
|
+
(byte >= 0x41 && byte <= 0x46) ||
|
|
33
|
+
(byte >= 0x61 && byte <= 0x66)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isEscape(byte: number): boolean {
|
|
38
|
+
return (
|
|
39
|
+
byte === QUOTE ||
|
|
40
|
+
byte === BACKSLASH ||
|
|
41
|
+
byte === 0x2f ||
|
|
42
|
+
byte === 0x62 ||
|
|
43
|
+
byte === 0x66 ||
|
|
44
|
+
byte === 0x6e ||
|
|
45
|
+
byte === 0x72 ||
|
|
46
|
+
byte === 0x74
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Turns a JSON stream into the same structure with every string value emptied. */
|
|
51
|
+
class JsonRedactor {
|
|
52
|
+
readonly #out: number[] = [];
|
|
53
|
+
readonly #frames: Frame[] = [];
|
|
54
|
+
#root: "value" | "done" = "value";
|
|
55
|
+
#inString = false;
|
|
56
|
+
#stringValue = false;
|
|
57
|
+
#escaped = false;
|
|
58
|
+
#unicode = 0;
|
|
59
|
+
#inScalar = false;
|
|
60
|
+
#broken = false;
|
|
61
|
+
|
|
62
|
+
get broken(): boolean {
|
|
63
|
+
return this.#broken;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
write(chunk: Uint8Array): void {
|
|
67
|
+
for (let at = 0; at < chunk.length && !this.#broken; at += 1) {
|
|
68
|
+
this.#step(chunk[at] ?? 0);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
finish(): string | undefined {
|
|
73
|
+
if (this.#broken || this.#inString || this.#escaped || this.#unicode > 0) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
if (this.#inScalar) {
|
|
77
|
+
this.#inScalar = false;
|
|
78
|
+
this.#complete();
|
|
79
|
+
}
|
|
80
|
+
if (this.#frames.length > 0 || this.#root !== "done") return undefined;
|
|
81
|
+
return new TextDecoder().decode(Uint8Array.from(this.#out));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#push(byte: number): void {
|
|
85
|
+
this.#out.push(byte);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#top(): Frame | undefined {
|
|
89
|
+
return this.#frames[this.#frames.length - 1];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#topKind(): "object" | "array" | undefined {
|
|
93
|
+
return this.#top()?.kind;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#topState(state: Frame["state"]): void {
|
|
97
|
+
const top = this.#top();
|
|
98
|
+
if (top === undefined) {
|
|
99
|
+
this.#broken = true;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
top.state = state;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
#expectsKey(): boolean {
|
|
106
|
+
const top = this.#top();
|
|
107
|
+
return top?.kind === "object" && top.state === "key";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#colonState(): boolean {
|
|
111
|
+
const top = this.#top();
|
|
112
|
+
return top?.kind === "object" && top.state === "colon";
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#expectsValue(): boolean {
|
|
116
|
+
if (this.#frames.length === 0) return this.#root === "value";
|
|
117
|
+
return this.#top()?.state === "value";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#commaState(): boolean {
|
|
121
|
+
return this.#top()?.state === "comma";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#complete(): void {
|
|
125
|
+
if (this.#frames.length === 0) {
|
|
126
|
+
this.#root = "done";
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this.#topState("comma");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#close(byte: number): void {
|
|
133
|
+
this.#push(byte);
|
|
134
|
+
if (this.#frames.pop() === undefined) {
|
|
135
|
+
this.#broken = true;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
this.#complete();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#value(byte: number): void {
|
|
142
|
+
if (byte === OPEN_OBJECT) {
|
|
143
|
+
this.#push(byte);
|
|
144
|
+
this.#frames.push({ kind: "object", state: "key" });
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (byte === OPEN_ARRAY) {
|
|
148
|
+
this.#push(byte);
|
|
149
|
+
this.#frames.push({ kind: "array", state: "value" });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (byte === QUOTE) {
|
|
153
|
+
this.#push(byte);
|
|
154
|
+
this.#inString = true;
|
|
155
|
+
this.#stringValue = true;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (byte === CLOSE_OBJECT || byte === CLOSE_ARRAY || byte === COMMA || byte === COLON) {
|
|
159
|
+
this.#broken = true;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
this.#push(byte);
|
|
163
|
+
this.#inScalar = true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#string(byte: number): void {
|
|
167
|
+
if (this.#unicode > 0) {
|
|
168
|
+
if (!isHex(byte)) {
|
|
169
|
+
this.#broken = true;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!this.#stringValue) this.#push(byte);
|
|
173
|
+
this.#unicode -= 1;
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (this.#escaped) {
|
|
177
|
+
this.#escaped = false;
|
|
178
|
+
if (byte === 0x75) {
|
|
179
|
+
this.#unicode = 4;
|
|
180
|
+
if (!this.#stringValue) this.#push(byte);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (!isEscape(byte)) {
|
|
184
|
+
this.#broken = true;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (!this.#stringValue) this.#push(byte);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (byte === BACKSLASH) {
|
|
191
|
+
this.#escaped = true;
|
|
192
|
+
if (!this.#stringValue) this.#push(byte);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (byte === QUOTE) {
|
|
196
|
+
this.#inString = false;
|
|
197
|
+
this.#push(byte);
|
|
198
|
+
if (this.#stringValue) {
|
|
199
|
+
this.#stringValue = false;
|
|
200
|
+
this.#complete();
|
|
201
|
+
} else {
|
|
202
|
+
this.#topState("colon");
|
|
203
|
+
}
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (byte < 0x20) {
|
|
207
|
+
this.#broken = true;
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (!this.#stringValue) this.#push(byte);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#step(byte: number): void {
|
|
214
|
+
if (this.#inString) {
|
|
215
|
+
this.#string(byte);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (this.#inScalar) {
|
|
219
|
+
if (isSpace(byte) || byte === COMMA || byte === CLOSE_ARRAY || byte === CLOSE_OBJECT) {
|
|
220
|
+
this.#inScalar = false;
|
|
221
|
+
this.#complete();
|
|
222
|
+
this.#step(byte);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (byte === OPEN_OBJECT || byte === OPEN_ARRAY || byte === QUOTE || byte === COLON) {
|
|
226
|
+
this.#broken = true;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
this.#push(byte);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (isSpace(byte)) {
|
|
233
|
+
this.#push(byte);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (this.#expectsKey()) {
|
|
237
|
+
if (byte === QUOTE) {
|
|
238
|
+
this.#push(byte);
|
|
239
|
+
this.#inString = true;
|
|
240
|
+
this.#stringValue = false;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (byte === CLOSE_OBJECT) {
|
|
244
|
+
this.#close(byte);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
this.#broken = true;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (this.#colonState()) {
|
|
251
|
+
if (byte !== COLON) {
|
|
252
|
+
this.#broken = true;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.#push(byte);
|
|
256
|
+
this.#topState("value");
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (this.#expectsValue()) {
|
|
260
|
+
if (byte === CLOSE_ARRAY && this.#topKind() === "array") {
|
|
261
|
+
this.#close(byte);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
this.#value(byte);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (this.#commaState()) {
|
|
268
|
+
if (byte === COMMA) {
|
|
269
|
+
this.#push(byte);
|
|
270
|
+
this.#topState(this.#topKind() === "object" ? "key" : "value");
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (
|
|
274
|
+
(byte === CLOSE_OBJECT && this.#topKind() === "object") ||
|
|
275
|
+
(byte === CLOSE_ARRAY && this.#topKind() === "array")
|
|
276
|
+
) {
|
|
277
|
+
this.#close(byte);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
this.#broken = true;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The redacted JSON text for these chunks: keys and structure kept, every
|
|
287
|
+
* string value emptied; undefined when the text is not JSON.
|
|
288
|
+
*/
|
|
289
|
+
export function redactJsonValues(chunks: Iterable<Uint8Array>): string | undefined {
|
|
290
|
+
const redactor = new JsonRedactor();
|
|
291
|
+
for (const chunk of chunks) {
|
|
292
|
+
redactor.write(chunk);
|
|
293
|
+
if (redactor.broken) return undefined;
|
|
294
|
+
}
|
|
295
|
+
const redacted = redactor.finish();
|
|
296
|
+
if (redacted === undefined) return undefined;
|
|
297
|
+
try {
|
|
298
|
+
JSON.parse(redacted);
|
|
299
|
+
} catch {
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
return redacted;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function hasCredential(auth: Record<string, unknown>, provider: string | undefined): boolean {
|
|
306
|
+
const stored = (id: string): boolean => Object.hasOwn(auth, id) && isRecord(auth[id]);
|
|
307
|
+
return provider === undefined ? Object.keys(auth).some(stored) : stored(provider);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Whether pi's auth file holds a credential for the provider it will use. The
|
|
312
|
+
* file is streamed and its values are dropped as they pass, so no credential
|
|
313
|
+
* value is kept or returned; a missing file means no credential (false), and
|
|
314
|
+
* an unreadable or malformed one means it cannot tell (undefined).
|
|
315
|
+
*/
|
|
316
|
+
export async function authFileHasCredential(
|
|
317
|
+
authPath: string,
|
|
318
|
+
provider: string | undefined,
|
|
319
|
+
): Promise<boolean | undefined> {
|
|
320
|
+
let handle: Awaited<ReturnType<typeof open>>;
|
|
321
|
+
try {
|
|
322
|
+
handle = await open(authPath, "r");
|
|
323
|
+
} catch (error) {
|
|
324
|
+
return isMissingFile(error) ? false : undefined;
|
|
325
|
+
}
|
|
326
|
+
const redactor = new JsonRedactor();
|
|
327
|
+
try {
|
|
328
|
+
const buffer = new Uint8Array(64 * 1024);
|
|
329
|
+
for (;;) {
|
|
330
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
|
|
331
|
+
if (bytesRead === 0) break;
|
|
332
|
+
redactor.write(buffer.subarray(0, bytesRead));
|
|
333
|
+
if (redactor.broken) return undefined;
|
|
334
|
+
}
|
|
335
|
+
} catch {
|
|
336
|
+
return undefined;
|
|
337
|
+
} finally {
|
|
338
|
+
await handle.close().catch(() => undefined);
|
|
339
|
+
}
|
|
340
|
+
const redacted = redactor.finish();
|
|
341
|
+
if (redacted === undefined) return undefined;
|
|
342
|
+
let value: unknown;
|
|
343
|
+
try {
|
|
344
|
+
value = JSON.parse(redacted);
|
|
345
|
+
} catch {
|
|
346
|
+
return undefined;
|
|
347
|
+
}
|
|
348
|
+
if (!isRecord(value)) return undefined;
|
|
349
|
+
return hasCredential(value, provider);
|
|
350
|
+
}
|