@trama-dev/runtime 0.6.0 → 0.6.1
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/dist/model-relay.d.ts +63 -5
- package/dist/model-relay.js +171 -18
- package/dist/pi-adapter.js +149 -28
- package/package.json +1 -1
package/dist/model-relay.d.ts
CHANGED
|
@@ -4,8 +4,17 @@ export type RelayModel = {
|
|
|
4
4
|
id: string;
|
|
5
5
|
api?: string;
|
|
6
6
|
} & Record<string, unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* The ways pi has of asking a model, that a relay asks by: for a reply, as
|
|
9
|
+
* pi's agent asks (by options that are any provider's) or by the options of
|
|
10
|
+
* the model's own provider; for images; for what a classifier makes of a
|
|
11
|
+
* state. Each is a method of pi's models by that name.
|
|
12
|
+
*/
|
|
13
|
+
export type RelayWay = "streamSimple" | "stream" | "generateImages" | "classify";
|
|
7
14
|
/** A request of a model: plain data, all of it. */
|
|
8
15
|
export interface RelayRequest {
|
|
16
|
+
/** The way the model is asked: as pi's agent asks, where it is not said. */
|
|
17
|
+
by?: RelayWay;
|
|
9
18
|
/** The model, as the session that asks has it. */
|
|
10
19
|
model: RelayModel;
|
|
11
20
|
/** What the model is to be sent: as pi hands it to a provider. */
|
|
@@ -15,7 +24,13 @@ export interface RelayRequest {
|
|
|
15
24
|
/** Whether it is asked beside a call that is over — one that has its answer, or has failed (see AgentCall.sofar). */
|
|
16
25
|
answered?: true;
|
|
17
26
|
}
|
|
18
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* An event of a reply, as pi's stream of it has them. The last is of type
|
|
29
|
+
* "done" or "error", and has the reply whole. What comes all at once —
|
|
30
|
+
* images, a classifier's answers — comes as a reply of one event, "done",
|
|
31
|
+
* with it as its `message`: so that it is counted, and waited for, as any
|
|
32
|
+
* reply is (see `wholeOf`).
|
|
33
|
+
*/
|
|
19
34
|
export type RelayEvent = {
|
|
20
35
|
type: string;
|
|
21
36
|
} & Record<string, unknown>;
|
|
@@ -28,12 +43,54 @@ export interface ModelRelay {
|
|
|
28
43
|
ask(request: RelayRequest, signal?: AbortSignal): Promise<AsyncIterable<RelayEvent> | undefined>;
|
|
29
44
|
}
|
|
30
45
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
46
|
+
* Whether a value is data: what it is on the other side of a channel is what
|
|
47
|
+
* it is here. Text, numbers that are numbers, yes and no, nothing; and lists
|
|
48
|
+
* and plain records of those. Not a function, nor anything that is of a kind
|
|
49
|
+
* of its own (a date, a signal, bytes; a list or a record that is more than
|
|
50
|
+
* one), nor what holds itself, nor what says of itself something other than
|
|
51
|
+
* it shows. (What a record has by a symbol, or has and does not show, is no
|
|
52
|
+
* part of it on any channel, and a provider is sent none of it either: pi's
|
|
53
|
+
* schemas have such.)
|
|
54
|
+
*/
|
|
55
|
+
export declare function isData(value: unknown, within?: Set<object>): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* The options a session asks with, as those that go with a relayed request —
|
|
58
|
+
* or undefined where one is there that cannot go, and would be missed.
|
|
59
|
+
*
|
|
60
|
+
* What goes is whatever is data, by whatever name: a provider's own options
|
|
61
|
+
* are its own, and are not known here by name. What cannot go: a key of the
|
|
62
|
+
* asker's own, a way of its own to reach the provider, a span to report to,
|
|
63
|
+
* a reply that is to be fetched later (it is fetched where it was asked
|
|
64
|
+
* for) — and anything else that is not data: a provider may read whatever
|
|
65
|
+
* it is given, and what goes with a request is its asker's to mean something
|
|
66
|
+
* by. (What whoever hands a request over knows to be nothing a provider is
|
|
67
|
+
* handed, they leave out first: see `account`, pi-adapter.ts.) The word to
|
|
68
|
+
* stop by is no option here: it goes beside the request.
|
|
35
69
|
*/
|
|
36
70
|
export declare function dataOf(options?: Record<string, unknown>): Record<string, unknown> | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Data as it goes on a channel, where a record in it has nothing under a
|
|
73
|
+
* name. A channel carries no such name — and to have nothing under a name is
|
|
74
|
+
* not to be without it, where records are laid one over another, name by
|
|
75
|
+
* name and in the order of their names (as pi lays a request's headers, and
|
|
76
|
+
* what a model is sampled by, over the model's own). So a word is put under
|
|
77
|
+
* each such name, that is nothing else: said beside the data (`mark`), and
|
|
78
|
+
* taken out again where the data is had (`unmarked`). The name then stands
|
|
79
|
+
* where it stood, with nothing under it.
|
|
80
|
+
*/
|
|
81
|
+
export declare function marked<T>(data: T): {
|
|
82
|
+
data: T;
|
|
83
|
+
mark?: string;
|
|
84
|
+
};
|
|
85
|
+
/** Data that came by a channel (see `marked`), with nothing under the names of its records that the mark is under. (In a list the mark is a word like any: nothing is ever marked there.) */
|
|
86
|
+
export declare function unmarked<T>(data: T, mark: unknown): T;
|
|
87
|
+
/**
|
|
88
|
+
* What came of asking a model, of a reply that is gone through to its end:
|
|
89
|
+
* the reply whole — or, of a request that failed before it was one, its
|
|
90
|
+
* failure thrown (as pi's own `generateImages` and `classify` have what a
|
|
91
|
+
* provider throws, and make of it what they give back).
|
|
92
|
+
*/
|
|
93
|
+
export declare function wholeOf(reply: AsyncIterable<RelayEvent>): Promise<unknown>;
|
|
37
94
|
/** What a reply used, as pi has it on the reply. */
|
|
38
95
|
export interface ReplyUsage {
|
|
39
96
|
input: number;
|
|
@@ -87,6 +144,7 @@ export type RelayAsked = {
|
|
|
87
144
|
relay: {
|
|
88
145
|
id: number;
|
|
89
146
|
request: RelayRequest;
|
|
147
|
+
mark?: string;
|
|
90
148
|
};
|
|
91
149
|
} | {
|
|
92
150
|
relayAbort: number;
|
package/dist/model-relay.js
CHANGED
|
@@ -14,29 +14,142 @@
|
|
|
14
14
|
// asks its model itself, as it always did — and what it used is what its
|
|
15
15
|
// process says. This is not a wall: pi does not hold an extension to it.
|
|
16
16
|
//
|
|
17
|
+
// A model: one that is asked for a reply as pi's agent asks, or by its
|
|
18
|
+
// provider's own options (which is how an extension asks that would have the
|
|
19
|
+
// whole answer: pi's `complete`), one that makes images, one that classifies.
|
|
20
|
+
//
|
|
17
21
|
// Three things are here: the one that asks (createModelRelay), and the two
|
|
18
22
|
// ends of a channel between it and a session (relayClient, relayServer).
|
|
23
|
+
import { randomUUID } from "crypto";
|
|
19
24
|
import { createRequire } from "module";
|
|
20
25
|
import { pathToFileURL } from "url";
|
|
21
26
|
import { onAbort } from "./abort.js";
|
|
22
27
|
import { messageOf } from "./errors.js";
|
|
23
28
|
const loadPi = () => import("@earendil-works/pi-coding-agent");
|
|
24
|
-
/**
|
|
25
|
-
const
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
]
|
|
29
|
+
/** Of each way: the type of model it asks, as pi's catalog has them — and whether what comes of it comes as the events of a reply (and not all at once). */
|
|
30
|
+
const WAYS = new Map([
|
|
31
|
+
["streamSimple", { of: "chat", events: true }],
|
|
32
|
+
["stream", { of: "chat", events: true }],
|
|
33
|
+
["generateImages", { of: "image", events: false }],
|
|
34
|
+
["classify", { of: "classifier", events: false }],
|
|
35
|
+
]);
|
|
36
|
+
/**
|
|
37
|
+
* Whether a value is data: what it is on the other side of a channel is what
|
|
38
|
+
* it is here. Text, numbers that are numbers, yes and no, nothing; and lists
|
|
39
|
+
* and plain records of those. Not a function, nor anything that is of a kind
|
|
40
|
+
* of its own (a date, a signal, bytes; a list or a record that is more than
|
|
41
|
+
* one), nor what holds itself, nor what says of itself something other than
|
|
42
|
+
* it shows. (What a record has by a symbol, or has and does not show, is no
|
|
43
|
+
* part of it on any channel, and a provider is sent none of it either: pi's
|
|
44
|
+
* schemas have such.)
|
|
45
|
+
*/
|
|
46
|
+
export function isData(value, within = new Set()) {
|
|
47
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
48
|
+
return true;
|
|
49
|
+
if (typeof value === "number")
|
|
50
|
+
return Number.isFinite(value);
|
|
51
|
+
if (typeof value !== "object" || within.has(value))
|
|
52
|
+
return false;
|
|
53
|
+
// (A list or a record, and of no kind of its own: nor one that would be said otherwise than it shows.)
|
|
54
|
+
const kind = Object.getPrototypeOf(value);
|
|
55
|
+
if (Array.isArray(value) ? kind !== Array.prototype : kind !== Object.prototype && kind !== null)
|
|
56
|
+
return false;
|
|
57
|
+
if (typeof value.toJSON === "function")
|
|
58
|
+
return false;
|
|
59
|
+
within.add(value);
|
|
60
|
+
try {
|
|
61
|
+
// (A place of a list that has nothing is something else on the other side; a name of a record that has nothing is no
|
|
62
|
+
// name there. A list is gone through place by place, as a channel goes through it — not as it would have itself gone through.)
|
|
63
|
+
if (Array.isArray(value))
|
|
64
|
+
return Array.from({ length: value.length }, (_, at) => value[at]).every(one => isData(one, within));
|
|
65
|
+
return Object.values(value).every(one => one === undefined || isData(one, within));
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
within.delete(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
30
71
|
/**
|
|
31
|
-
*
|
|
32
|
-
* or undefined where one is there that cannot go, and would be missed
|
|
33
|
-
*
|
|
34
|
-
*
|
|
72
|
+
* The options a session asks with, as those that go with a relayed request —
|
|
73
|
+
* or undefined where one is there that cannot go, and would be missed.
|
|
74
|
+
*
|
|
75
|
+
* What goes is whatever is data, by whatever name: a provider's own options
|
|
76
|
+
* are its own, and are not known here by name. What cannot go: a key of the
|
|
77
|
+
* asker's own, a way of its own to reach the provider, a span to report to,
|
|
78
|
+
* a reply that is to be fetched later (it is fetched where it was asked
|
|
79
|
+
* for) — and anything else that is not data: a provider may read whatever
|
|
80
|
+
* it is given, and what goes with a request is its asker's to mean something
|
|
81
|
+
* by. (What whoever hands a request over knows to be nothing a provider is
|
|
82
|
+
* handed, they leave out first: see `account`, pi-adapter.ts.) The word to
|
|
83
|
+
* stop by is no option here: it goes beside the request.
|
|
35
84
|
*/
|
|
36
85
|
export function dataOf(options = {}) {
|
|
37
86
|
if (["apiKey", "fetch", "telemetry", "deferred"].some(key => options[key] !== undefined))
|
|
38
87
|
return undefined;
|
|
39
|
-
|
|
88
|
+
const data = Object.entries(options).filter(([key]) => key !== "signal");
|
|
89
|
+
if (data.some(([, value]) => value !== undefined && !isData(value)))
|
|
90
|
+
return undefined;
|
|
91
|
+
// (Each under its own name, whatever the name: none of them is taken for anything else.)
|
|
92
|
+
return Object.fromEntries(data);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Data as it goes on a channel, where a record in it has nothing under a
|
|
96
|
+
* name. A channel carries no such name — and to have nothing under a name is
|
|
97
|
+
* not to be without it, where records are laid one over another, name by
|
|
98
|
+
* name and in the order of their names (as pi lays a request's headers, and
|
|
99
|
+
* what a model is sampled by, over the model's own). So a word is put under
|
|
100
|
+
* each such name, that is nothing else: said beside the data (`mark`), and
|
|
101
|
+
* taken out again where the data is had (`unmarked`). The name then stands
|
|
102
|
+
* where it stood, with nothing under it.
|
|
103
|
+
*/
|
|
104
|
+
export function marked(data) {
|
|
105
|
+
const mark = `nothing:${randomUUID()}`;
|
|
106
|
+
let found = false;
|
|
107
|
+
const copy = (value) => {
|
|
108
|
+
if (Array.isArray(value))
|
|
109
|
+
return value.map(copy);
|
|
110
|
+
if (value === null || typeof value !== "object")
|
|
111
|
+
return value;
|
|
112
|
+
return Object.fromEntries(Object.entries(value).map(([name, one]) => {
|
|
113
|
+
if (one !== undefined)
|
|
114
|
+
return [name, copy(one)];
|
|
115
|
+
found = true;
|
|
116
|
+
return [name, mark];
|
|
117
|
+
}));
|
|
118
|
+
};
|
|
119
|
+
const made = copy(data);
|
|
120
|
+
// (A copy either way: what goes on the channel is what the data shows, name by name, whatever else it has.)
|
|
121
|
+
return found ? { data: made, mark } : { data: made };
|
|
122
|
+
}
|
|
123
|
+
/** Data that came by a channel (see `marked`), with nothing under the names of its records that the mark is under. (In a list the mark is a word like any: nothing is ever marked there.) */
|
|
124
|
+
export function unmarked(data, mark) {
|
|
125
|
+
if (typeof mark !== "string" || mark === "")
|
|
126
|
+
return data;
|
|
127
|
+
const clear = (value) => {
|
|
128
|
+
if (value === null || typeof value !== "object")
|
|
129
|
+
return;
|
|
130
|
+
for (const [name, one] of Object.entries(value)) {
|
|
131
|
+
if (one === mark && !Array.isArray(value))
|
|
132
|
+
value[name] = undefined;
|
|
133
|
+
else
|
|
134
|
+
clear(one);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
clear(data);
|
|
138
|
+
return data;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* What came of asking a model, of a reply that is gone through to its end:
|
|
142
|
+
* the reply whole — or, of a request that failed before it was one, its
|
|
143
|
+
* failure thrown (as pi's own `generateImages` and `classify` have what a
|
|
144
|
+
* provider throws, and make of it what they give back).
|
|
145
|
+
*/
|
|
146
|
+
export async function wholeOf(reply) {
|
|
147
|
+
let last;
|
|
148
|
+
for await (const event of reply)
|
|
149
|
+
last = event;
|
|
150
|
+
if (last?.type === "done")
|
|
151
|
+
return last.message;
|
|
152
|
+
throw new Error(String(last?.error?.errorMessage ?? "The model was not asked."));
|
|
40
153
|
}
|
|
41
154
|
/** What the reply an event is the last of used — undefined of any other event. */
|
|
42
155
|
export function usageAtEnd(event) {
|
|
@@ -156,7 +269,12 @@ export function createModelRelay() {
|
|
|
156
269
|
try {
|
|
157
270
|
const found = await (models ??= loadPi().then(pi => pi.ModelRuntime.create()));
|
|
158
271
|
const { provider, id } = request.model;
|
|
159
|
-
const
|
|
272
|
+
const by = request.by ?? "streamSimple";
|
|
273
|
+
// (A way that is none of these is not one to ask by, whatever pi's models have by that name.)
|
|
274
|
+
const way = WAYS.get(by);
|
|
275
|
+
if (!way)
|
|
276
|
+
return undefined;
|
|
277
|
+
const own = way.of === "chat" ? found.getModel(provider, id) : found.getModelOfType(way.of, provider, id);
|
|
160
278
|
// The same model, to the letter, with a key to ask it by — and a provider nobody has had a hand in.
|
|
161
279
|
if (!own || JSON.stringify(own) !== JSON.stringify(request.model) || !found.hasConfiguredAuth(provider))
|
|
162
280
|
return undefined;
|
|
@@ -166,7 +284,10 @@ export function createModelRelay() {
|
|
|
166
284
|
return undefined;
|
|
167
285
|
if (typeof options.sessionId === "string")
|
|
168
286
|
sessions.add(options.sessionId);
|
|
169
|
-
|
|
287
|
+
// (pi's models, asked by the name of the way: each takes the model, what it is to be sent, and the options.)
|
|
288
|
+
const ask = found[by].bind(found);
|
|
289
|
+
const asked = ask(own, request.context, { ...options, signal: stop.signal });
|
|
290
|
+
reply = way.events ? asked : (async function* () { yield { type: "done", message: await asked }; })();
|
|
170
291
|
}
|
|
171
292
|
catch {
|
|
172
293
|
// (A pi that cannot be had here is not one to ask by: whoever asked asks itself.)
|
|
@@ -257,7 +378,9 @@ export function relayClient(send) {
|
|
|
257
378
|
end: () => add(notAsked(request.model, "The process that was asking the model ended before the reply had come.", "error")),
|
|
258
379
|
});
|
|
259
380
|
try {
|
|
260
|
-
|
|
381
|
+
// (Where its options have nothing under a name, that is said: the channel would not say it.)
|
|
382
|
+
const { data: options, mark } = marked(request.options);
|
|
383
|
+
send({ relay: { id, request: { ...request, options }, mark } });
|
|
261
384
|
}
|
|
262
385
|
catch {
|
|
263
386
|
// (A request that cannot be put on the channel — something in it is not data — is one nobody served.)
|
|
@@ -286,10 +409,23 @@ export function relayClient(send) {
|
|
|
286
409
|
*/
|
|
287
410
|
export function relayServer(relay, send, on = {}) {
|
|
288
411
|
const asking = new Map();
|
|
412
|
+
// What cannot be said — the channel will not carry it, or is gone — is not said: whoever asks for others
|
|
413
|
+
// is not failed by one of them.
|
|
414
|
+
const say = (answer) => {
|
|
415
|
+
try {
|
|
416
|
+
send(answer);
|
|
417
|
+
}
|
|
418
|
+
catch { /* (Nobody is there to hear it.) */ }
|
|
419
|
+
};
|
|
289
420
|
const serve = async (id, request, stop) => {
|
|
421
|
+
// Of a request that is not asked, what is said back of its model is its names: whatever else came as
|
|
422
|
+
// the model may be nothing the channel will carry.
|
|
423
|
+
let model = { provider: "", id: "" };
|
|
290
424
|
try {
|
|
425
|
+
const { provider, id: name, api } = request.model;
|
|
426
|
+
model = { provider: String(provider), id: String(name), ...(typeof api === "string" && { api }) };
|
|
291
427
|
if (await on.refuse?.(request))
|
|
292
|
-
return send({ relayed: { id, event: notAsked(
|
|
428
|
+
return send({ relayed: { id, event: notAsked(model, "The call is to ask no more.") } });
|
|
293
429
|
const events = await relay.ask(request, stop.signal);
|
|
294
430
|
if (!events)
|
|
295
431
|
return send({ relayed: { id, not: true } });
|
|
@@ -301,7 +437,13 @@ export function relayServer(relay, send, on = {}) {
|
|
|
301
437
|
}
|
|
302
438
|
}
|
|
303
439
|
catch (err) {
|
|
304
|
-
|
|
440
|
+
// (Whatever was thrown, and whether or not it can be said what.)
|
|
441
|
+
let why = "it cannot be said why";
|
|
442
|
+
try {
|
|
443
|
+
why = messageOf(err);
|
|
444
|
+
}
|
|
445
|
+
catch { /* (As it is.) */ }
|
|
446
|
+
say({ relayed: { id, event: notAsked(model, `The model could not be asked: ${why}`, "error") } });
|
|
305
447
|
}
|
|
306
448
|
finally {
|
|
307
449
|
asking.delete(id);
|
|
@@ -311,10 +453,21 @@ export function relayServer(relay, send, on = {}) {
|
|
|
311
453
|
heard(message) {
|
|
312
454
|
if ("relayAbort" in message)
|
|
313
455
|
return asking.get(message.relayAbort)?.abort();
|
|
314
|
-
const { id, request } = message.relay;
|
|
456
|
+
const { id, request, mark } = message.relay;
|
|
457
|
+
// (A request by a number another is being asked by is nobody's: what would stop it would stop the other.)
|
|
458
|
+
if (asking.has(id))
|
|
459
|
+
return;
|
|
460
|
+
let asked;
|
|
461
|
+
try {
|
|
462
|
+
asked = { ...request, options: unmarked(request.options, mark) };
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
// (What cannot be gone through — it goes deeper than anything a request goes with — is not one to serve.)
|
|
466
|
+
return say({ relayed: { id, not: true } });
|
|
467
|
+
}
|
|
315
468
|
const stop = new AbortController();
|
|
316
469
|
asking.set(id, stop);
|
|
317
|
-
void serve(id,
|
|
470
|
+
void serve(id, asked, stop);
|
|
318
471
|
},
|
|
319
472
|
close() {
|
|
320
473
|
for (const stop of asking.values())
|
package/dist/pi-adapter.js
CHANGED
|
@@ -4,7 +4,7 @@ import { createAgentHosts } from "./agent-hosts.js";
|
|
|
4
4
|
import { CutOff, addReply, atLeast, nothingYet } from "./agent-backend.js";
|
|
5
5
|
import * as http from "http";
|
|
6
6
|
import * as https from "https";
|
|
7
|
-
import { dataOf, notAsked, usageAtEnd } from "./model-relay.js";
|
|
7
|
+
import { dataOf, isData, notAsked, usageAtEnd, wholeOf } from "./model-relay.js";
|
|
8
8
|
/**
|
|
9
9
|
* pi is loaded when it is first needed, not when this file is: it is most of
|
|
10
10
|
* what the runtime weighs, and listing projects, reading logs, or running a
|
|
@@ -182,6 +182,41 @@ function either(own, cut) {
|
|
|
182
182
|
}
|
|
183
183
|
/** What an extension may have a hand in, of a request to a provider: a session where one does asks its model itself. */
|
|
184
184
|
const HANDS_IN = ["before_provider_request", "before_provider_headers", "after_provider_response", "provider_stream_event"];
|
|
185
|
+
/**
|
|
186
|
+
* What pi's agent has every request of its own go with, to be told of what
|
|
187
|
+
* a provider is sent and answers: pi's own, which do nothing where no
|
|
188
|
+
* extension has a hand in (HANDS_IN). A request that goes with all of them,
|
|
189
|
+
* by the way the agent asks, is one the agent makes for itself — or one made
|
|
190
|
+
* with all the agent's was made with, which is the same to a provider.
|
|
191
|
+
*/
|
|
192
|
+
const HOOKS = ["onPayload", "onResponse", "onProviderStreamEvent"];
|
|
193
|
+
/**
|
|
194
|
+
* What else goes with a request the agent makes for itself, and is nothing
|
|
195
|
+
* a provider is handed: what the agent's loop calls as a turn goes, which pi
|
|
196
|
+
* passes along with what a provider is to have. (The functions of
|
|
197
|
+
* pi-agent-core's `createLoopConfig` that pi sets, less the hooks above. The
|
|
198
|
+
* rest of what a loop is set up by is data, and goes as any data.) Named,
|
|
199
|
+
* because it cannot be told otherwise: a function of the loop's is a
|
|
200
|
+
* function like one an asker might mean a provider to have. A pi whose loop
|
|
201
|
+
* is set up by a function more has its agent's requests asked where the call
|
|
202
|
+
* is served, until it is named here.
|
|
203
|
+
*/
|
|
204
|
+
const LOOPS = ["beforeToolCall", "afterToolCall", "finishTurn", "prepareRequest", "prepareNextTurn", "convertToLlm", "transformContext", "getSteeringMessages", "getFollowUpMessages"];
|
|
205
|
+
/**
|
|
206
|
+
* What a provider is asked by, of what it has: whether what comes of it comes
|
|
207
|
+
* as the events of a reply, and the way a relay asks by in its place (none,
|
|
208
|
+
* of a reply that was to be fetched later: it is fetched where it was asked
|
|
209
|
+
* for). Whatever else a provider has is not asking a model.
|
|
210
|
+
*/
|
|
211
|
+
const ASKED_BY = new Map([
|
|
212
|
+
["stream", { events: true, way: "stream" }],
|
|
213
|
+
["streamSimple", { events: true, way: "streamSimple" }],
|
|
214
|
+
["fetchDeferred", { events: true }],
|
|
215
|
+
["generateImages", { events: false, way: "generateImages" }],
|
|
216
|
+
["classify", { events: false, way: "classify" }],
|
|
217
|
+
]);
|
|
218
|
+
/** What is said of a request that is not made: its call is to ask no more. */
|
|
219
|
+
const NO_MORE = "The call is to ask no more.";
|
|
185
220
|
/**
|
|
186
221
|
* What a request to a provider goes by, that is a process's own: its
|
|
187
222
|
* variables — a provider's key may be one — and its ways to the network. An
|
|
@@ -219,8 +254,24 @@ function asItWas() {
|
|
|
219
254
|
function watch(models, asking) {
|
|
220
255
|
const standing = models;
|
|
221
256
|
const prepare = standing.prepareRequest.bind(models);
|
|
222
|
-
|
|
223
|
-
|
|
257
|
+
// A provider for a call that is to ask no more: whatever it is asked by, the request ends as one that was
|
|
258
|
+
// cancelled before it was sent — or, where what comes of it comes all at once, fails saying so. But to have
|
|
259
|
+
// a provider give up a reply it was keeping for later is to ask no model: that is done, whoever is stopped.
|
|
260
|
+
const refusing = (model, options) => {
|
|
261
|
+
const refused = async function* () {
|
|
262
|
+
yield notAsked(model, NO_MORE);
|
|
263
|
+
};
|
|
264
|
+
const failed = async () => { throw new Error(NO_MORE); };
|
|
265
|
+
return {
|
|
266
|
+
stream: refused, streamSimple: refused, fetchDeferred: refused, generateImages: failed, classify: failed,
|
|
267
|
+
cancelDeferred: async (_model, handle) => {
|
|
268
|
+
const real = await prepare(model, options);
|
|
269
|
+
const cancel = real.provider.cancelDeferred;
|
|
270
|
+
if (typeof cancel !== "function")
|
|
271
|
+
throw new Error(`Provider ${model.provider} does not support deferred responses`);
|
|
272
|
+
await cancel.call(real.provider, real.model, handle, real.options);
|
|
273
|
+
},
|
|
274
|
+
};
|
|
224
275
|
};
|
|
225
276
|
// A provider that is an extension's own code may ask another model by these same models, and hand back what
|
|
226
277
|
// that model answered — there and then, from a queue of its own, or a reply it had asked for before: a reply
|
|
@@ -260,35 +311,70 @@ function watch(models, asking) {
|
|
|
260
311
|
const { signal, release } = either(options?.signal, asking.cut.signal);
|
|
261
312
|
return { options: { ...options, signal }, release };
|
|
262
313
|
};
|
|
314
|
+
// What a request's asker would make of its headers (pi's agent has a function go with every request of its
|
|
315
|
+
// own, for extensions that would: it makes nothing of them where none does) is seen as it is made: whether
|
|
316
|
+
// the headers came out of it other than they went in — name by name, in their order, and a name with nothing
|
|
317
|
+
// under it a name all the same: pi lays headers one over another by all of that.
|
|
318
|
+
// (What cannot be told at all is not what it was, for all that can be said of it.)
|
|
319
|
+
const told = (headers) => {
|
|
320
|
+
try {
|
|
321
|
+
return JSON.stringify(headers !== null && typeof headers === "object"
|
|
322
|
+
? Object.entries(headers).map(([name, value]) => [name, value === undefined ? 0 : [value]]) : [headers]);
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const seeing = (options, headers) => {
|
|
329
|
+
const theirs = options?.transformHeaders;
|
|
330
|
+
if (typeof theirs !== "function")
|
|
331
|
+
return options;
|
|
332
|
+
return {
|
|
333
|
+
...options,
|
|
334
|
+
transformHeaders: async (had) => {
|
|
335
|
+
const were = told(had);
|
|
336
|
+
const made = await theirs(had);
|
|
337
|
+
if (were === undefined || told(made) !== were)
|
|
338
|
+
headers.changed = true;
|
|
339
|
+
return made;
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
};
|
|
263
343
|
standing.prepareRequest = async (model, options) => {
|
|
264
344
|
count("readying", 1);
|
|
265
345
|
try {
|
|
266
346
|
await asking.before?.();
|
|
267
|
-
// (Asked no more
|
|
347
|
+
// (Asked no more.)
|
|
268
348
|
if (asking.stopped?.())
|
|
269
|
-
return { provider:
|
|
270
|
-
const
|
|
271
|
-
const
|
|
349
|
+
return { provider: refusing(model, options), model, options: options ?? {} };
|
|
350
|
+
const headers = { changed: false };
|
|
351
|
+
const prepared = await prepare(model, seeing(options, headers));
|
|
272
352
|
return {
|
|
273
353
|
...prepared,
|
|
274
|
-
provider: new Proxy(provider, {
|
|
354
|
+
provider: new Proxy(prepared.provider, {
|
|
275
355
|
get(target, key) {
|
|
276
|
-
if (key === "stream") {
|
|
277
|
-
return (...sent) => {
|
|
278
|
-
const here = stoppable(sent[2]);
|
|
279
|
-
return heard(target.stream(sent[0], sent[1], here.options), here.release);
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
|
-
if (key === "streamSimple") {
|
|
283
|
-
return (...sent) => {
|
|
284
|
-
const there = stoppable(options), here = stoppable(sent[2]);
|
|
285
|
-
return heard((async function* () {
|
|
286
|
-
yield* await asking.elsewhere?.(model, sent[1], there.options) ?? target.streamSimple(sent[0], sent[1], here.options);
|
|
287
|
-
})(), there.release, here.release);
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
356
|
const value = Reflect.get(target, key, target);
|
|
291
|
-
|
|
357
|
+
if (typeof value !== "function")
|
|
358
|
+
return value;
|
|
359
|
+
const by = ASKED_BY.get(key);
|
|
360
|
+
if (!by)
|
|
361
|
+
return value.bind(target);
|
|
362
|
+
// A model is asked: what a provider is handed is the model, what it is to be sent, and the options.
|
|
363
|
+
// Elsewhere, where that can be — or here, by the provider itself. What comes all at once is made a
|
|
364
|
+
// reply of one event, and counted and waited for as any reply; and whoever asked is given it whole.
|
|
365
|
+
return (...sent) => {
|
|
366
|
+
const there = stoppable(options), here = stoppable(sent[2]);
|
|
367
|
+
const reply = heard((async function* () {
|
|
368
|
+
const elsewhere = by.way && await asking.elsewhere?.(by.way, model, sent[1], there.options, headers);
|
|
369
|
+
if (elsewhere)
|
|
370
|
+
yield* elsewhere;
|
|
371
|
+
else if (by.events)
|
|
372
|
+
yield* value.call(target, sent[0], sent[1], here.options);
|
|
373
|
+
else
|
|
374
|
+
yield { type: "done", message: await value.call(target, sent[0], sent[1], here.options) };
|
|
375
|
+
})(), there.release, here.release);
|
|
376
|
+
return by.events ? reply : wholeOf(reply);
|
|
377
|
+
};
|
|
292
378
|
},
|
|
293
379
|
}),
|
|
294
380
|
};
|
|
@@ -353,15 +439,50 @@ function account(asking, call, signal, session) {
|
|
|
353
439
|
// a reply used is then read where no extension is. Not where an extension has made the model's
|
|
354
440
|
// provider its own, has a hand in what a provider is sent and answers, or has made
|
|
355
441
|
// this process other than it was in what a request goes by — the model is then asked here, as the
|
|
356
|
-
// extension means it to be — nor where the request is not one that can be handed over
|
|
442
|
+
// extension means it to be — nor where the request is not one that can be handed over: something
|
|
443
|
+
// goes with it that is not data, and whoever asked may mean something by it (see dataOf).
|
|
357
444
|
if (relay && call.extensions) {
|
|
358
|
-
asking.elsewhere = async (model, context, options) => {
|
|
359
|
-
const
|
|
360
|
-
if (!
|
|
445
|
+
asking.elsewhere = async (by, model, context, options = {}, headers) => {
|
|
446
|
+
const its = session();
|
|
447
|
+
if (!its || !asItWas() || asking.models.getRegisteredProviderIds().includes(model.provider))
|
|
361
448
|
return undefined;
|
|
362
449
|
if (HANDS_IN.some(event => its.extensionRunner.hasHandlers(event)))
|
|
363
450
|
return undefined;
|
|
364
|
-
|
|
451
|
+
// All that goes with a request is data, or the request is not handed over: whoever made it may mean
|
|
452
|
+
// something by it. But what is known to be nothing a provider is handed is left out first.
|
|
453
|
+
// What would make something of the request's headers, where that was seen to be nothing (see `watch`):
|
|
454
|
+
// the request is the same without it.
|
|
455
|
+
if (headers.changed)
|
|
456
|
+
return undefined;
|
|
457
|
+
const left = new Set(["transformHeaders"]);
|
|
458
|
+
// pi's own hooks, which do nothing here (see HANDS_IN) — and are pi's own where they are the very ones
|
|
459
|
+
// the session's agent has: any other is its asker's hand in the request.
|
|
460
|
+
const agent = its.agent;
|
|
461
|
+
for (const hook of HOOKS) {
|
|
462
|
+
if (options[hook] === undefined)
|
|
463
|
+
continue;
|
|
464
|
+
if (options[hook] !== agent[hook])
|
|
465
|
+
return undefined;
|
|
466
|
+
left.add(hook);
|
|
467
|
+
}
|
|
468
|
+
// And of a request the agent makes for itself, the rest of what its loop is set up by (see LOOPS); and
|
|
469
|
+
// in its conversation, which is of pi's making, what a tool kept beside its result — no provider is
|
|
470
|
+
// sent that, so it need not be what it was on the other side.
|
|
471
|
+
const agents = by === "streamSimple" && HOOKS.every(hook => options[hook] === agent[hook]);
|
|
472
|
+
if (agents)
|
|
473
|
+
for (const name of LOOPS)
|
|
474
|
+
left.add(name);
|
|
475
|
+
let data;
|
|
476
|
+
try {
|
|
477
|
+
data = dataOf(Object.fromEntries(Object.entries(options).filter(([name]) => !left.has(name))));
|
|
478
|
+
if (!data || (!agents && !isData(context)))
|
|
479
|
+
return undefined;
|
|
480
|
+
}
|
|
481
|
+
catch {
|
|
482
|
+
// (What cannot be gone through — it goes deeper than can be followed — is not known to be data.)
|
|
483
|
+
return undefined;
|
|
484
|
+
}
|
|
485
|
+
return relay.ask({ ...(by !== "streamSimple" && { by }), model, context, options: data, ...(asking.over && { answered: true }) }, options.signal);
|
|
365
486
|
};
|
|
366
487
|
}
|
|
367
488
|
return stopped;
|