@lucid-agents/pi-xgate 0.0.0-stage → 0.1.6
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/LICENSE +21 -0
- package/README.md +211 -2
- package/dist/codec.d.ts +34 -0
- package/dist/codec.js +510 -0
- package/dist/extension.d.ts +16 -0
- package/dist/extension.js +132 -0
- package/dist/generated/openai-model-capabilities.json +847 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/provider.d.ts +24 -0
- package/dist/provider.js +243 -0
- package/dist/taskmarket.d.ts +131 -0
- package/dist/taskmarket.js +275 -0
- package/dist/transport.d.ts +72 -0
- package/dist/transport.js +21 -0
- package/package.json +63 -4
- package/src/codec.ts +658 -0
- package/src/extension.ts +178 -0
- package/src/generated/openai-model-capabilities.json +847 -0
- package/src/index.ts +45 -0
- package/src/provider.ts +313 -0
- package/src/taskmarket.ts +484 -0
- package/src/transport.ts +94 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Supported entry point. Hosts build the provider around their own transport; the default
|
|
2
|
+
// Pi extension (`./extension`) keeps the standalone wallet broker.
|
|
3
|
+
export {
|
|
4
|
+
createXgateProvider,
|
|
5
|
+
invocationId,
|
|
6
|
+
type Broker,
|
|
7
|
+
type XgateProviderOptions,
|
|
8
|
+
type XgatePurchaseFailure,
|
|
9
|
+
} from "./provider.ts";
|
|
10
|
+
export {
|
|
11
|
+
XgatePurchaseError,
|
|
12
|
+
type XgateChannelBalance,
|
|
13
|
+
type XgatePayment,
|
|
14
|
+
type XgatePurchaseResult,
|
|
15
|
+
type XgateResponseHead,
|
|
16
|
+
type XgateTransport,
|
|
17
|
+
} from "./transport.ts";
|
|
18
|
+
export {
|
|
19
|
+
createXgateExtension,
|
|
20
|
+
type XgateExtensionOptions,
|
|
21
|
+
} from "./extension.ts";
|
|
22
|
+
export { XgateError } from "@lucid-agents/xgate-client/types";
|
|
23
|
+
export type {
|
|
24
|
+
Invocation,
|
|
25
|
+
PurchaseResult,
|
|
26
|
+
Route,
|
|
27
|
+
Settlement,
|
|
28
|
+
} from "@lucid-agents/xgate-client/types";
|
|
29
|
+
export {
|
|
30
|
+
createTaskMarketTransport,
|
|
31
|
+
type TaskMarketChargeStatus,
|
|
32
|
+
type TaskMarketFundingBalance,
|
|
33
|
+
type TaskMarketInvocationState,
|
|
34
|
+
type TaskMarketInvocationStatus,
|
|
35
|
+
type TaskMarketPayCallOptions,
|
|
36
|
+
type TaskMarketPayErrorCode,
|
|
37
|
+
type TaskMarketPayer,
|
|
38
|
+
type TaskMarketPayerLimits,
|
|
39
|
+
type TaskMarketPayRequest,
|
|
40
|
+
type TaskMarketPayResponse,
|
|
41
|
+
type TaskMarketPayResult,
|
|
42
|
+
type TaskMarketPaymentSummary,
|
|
43
|
+
type TaskMarketPaymentTerms,
|
|
44
|
+
type TaskMarketTransportOptions,
|
|
45
|
+
} from "./taskmarket.ts";
|
package/src/provider.ts
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
createAssistantMessageEventStream,
|
|
4
|
+
type AssistantMessage,
|
|
5
|
+
type Model,
|
|
6
|
+
type Provider,
|
|
7
|
+
type SimpleStreamOptions,
|
|
8
|
+
} from "@earendil-works/pi-ai";
|
|
9
|
+
import { XgateError } from "@lucid-agents/xgate-client/types";
|
|
10
|
+
import { catalogEntries, encode, decode, type PiContext } from "./codec.ts";
|
|
11
|
+
import {
|
|
12
|
+
XgatePurchaseError,
|
|
13
|
+
type XgatePayment,
|
|
14
|
+
type XgatePurchaseResult,
|
|
15
|
+
type XgateTransport,
|
|
16
|
+
} from "./transport.ts";
|
|
17
|
+
const hash = (value: string): string =>
|
|
18
|
+
createHash("sha256").update(value).digest("hex");
|
|
19
|
+
/** @deprecated Use `XgateTransport`. */
|
|
20
|
+
export type Broker = XgateTransport;
|
|
21
|
+
export interface XgateProviderOptions {
|
|
22
|
+
/** Invocation identity scope when Pi supplies no `sessionId`. Defaults to `"standalone"`. */
|
|
23
|
+
sessionId?: () => string;
|
|
24
|
+
/** Display-only notification of a verified paid result; it cannot change the outcome. */
|
|
25
|
+
onPaid?: (result: XgatePurchaseResult) => void;
|
|
26
|
+
/** Terminal notification of a purchase that ended without a verified result. */
|
|
27
|
+
onPurchaseFailed?: (failure: XgatePurchaseFailure) => void;
|
|
28
|
+
}
|
|
29
|
+
export interface XgatePurchaseFailure {
|
|
30
|
+
/** The original invocation ID; reconcile it, never retry under a new one. */
|
|
31
|
+
invocationId: string;
|
|
32
|
+
code: string;
|
|
33
|
+
/** False only when the transport proved no unresolved payment. */
|
|
34
|
+
pending: boolean;
|
|
35
|
+
payment?: XgatePayment;
|
|
36
|
+
}
|
|
37
|
+
const zero = () => ({
|
|
38
|
+
input: 0,
|
|
39
|
+
output: 0,
|
|
40
|
+
cacheRead: 0,
|
|
41
|
+
cacheWrite: 0,
|
|
42
|
+
totalTokens: 0,
|
|
43
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
44
|
+
});
|
|
45
|
+
/** Stable across automatic retries and payload hooks; a changed paid payload cannot claim a new intent. */
|
|
46
|
+
export function invocationId(
|
|
47
|
+
model: string,
|
|
48
|
+
context: PiContext,
|
|
49
|
+
session: string,
|
|
50
|
+
): string {
|
|
51
|
+
// Pi 0.85 carries the prompt and tools beside the messages; Pi 1.1 carries them inside.
|
|
52
|
+
const legacy =
|
|
53
|
+
"systemPrompt" in context || "tools" in context
|
|
54
|
+
? { systemPrompt: context.systemPrompt, tools: context.tools }
|
|
55
|
+
: {};
|
|
56
|
+
return hash(
|
|
57
|
+
JSON.stringify({
|
|
58
|
+
session,
|
|
59
|
+
model,
|
|
60
|
+
messages: context.messages.filter(
|
|
61
|
+
(m) =>
|
|
62
|
+
m.role !== "assistant" ||
|
|
63
|
+
!["error", "aborted"].includes(m.stopReason),
|
|
64
|
+
),
|
|
65
|
+
...legacy,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
export async function createXgateProvider(
|
|
70
|
+
transport: XgateTransport,
|
|
71
|
+
{
|
|
72
|
+
sessionId: identity = () => "standalone",
|
|
73
|
+
onPaid,
|
|
74
|
+
onPurchaseFailed,
|
|
75
|
+
}: XgateProviderOptions = {},
|
|
76
|
+
): Promise<Provider> {
|
|
77
|
+
let entries = catalogEntries(await transport.models());
|
|
78
|
+
const stream = (
|
|
79
|
+
model: Model<any>,
|
|
80
|
+
context: PiContext,
|
|
81
|
+
options: SimpleStreamOptions = {},
|
|
82
|
+
) => {
|
|
83
|
+
const events = createAssistantMessageEventStream();
|
|
84
|
+
const timeout = options.timeoutMs ?? 180000;
|
|
85
|
+
if (!Number.isSafeInteger(timeout) || timeout <= 0 || timeout > 180000)
|
|
86
|
+
options = { ...options, timeoutMs: 180000 };
|
|
87
|
+
const deadline = AbortSignal.timeout(
|
|
88
|
+
Math.min(options.timeoutMs ?? 180000, 180000),
|
|
89
|
+
);
|
|
90
|
+
options = {
|
|
91
|
+
...options,
|
|
92
|
+
signal: options.signal
|
|
93
|
+
? AbortSignal.any([options.signal, deadline])
|
|
94
|
+
: deadline,
|
|
95
|
+
};
|
|
96
|
+
const message: AssistantMessage = {
|
|
97
|
+
role: "assistant",
|
|
98
|
+
provider: "xgate",
|
|
99
|
+
api: model.api,
|
|
100
|
+
model: model.id,
|
|
101
|
+
content: [],
|
|
102
|
+
usage: zero(),
|
|
103
|
+
stopReason: "pending",
|
|
104
|
+
timestamp: Date.now(),
|
|
105
|
+
};
|
|
106
|
+
let attempted: string | undefined, paid: XgatePurchaseResult | undefined;
|
|
107
|
+
void (async () => {
|
|
108
|
+
try {
|
|
109
|
+
options.signal?.throwIfAborted();
|
|
110
|
+
const current = catalogEntries(await transport.models(options.signal)),
|
|
111
|
+
entry = current.find(
|
|
112
|
+
(e) => e.model.id === model.id && e.model.api === model.api,
|
|
113
|
+
);
|
|
114
|
+
if (!entry) throw new XgateError("model_not_qualified");
|
|
115
|
+
const original = encode(entry, context, {
|
|
116
|
+
...options,
|
|
117
|
+
// Respect a lower Pi model override without exceeding live catalog limits.
|
|
118
|
+
maxTokens:
|
|
119
|
+
options.maxTokens ??
|
|
120
|
+
Math.min(4096, model.maxTokens, entry.model.maxTokens),
|
|
121
|
+
}),
|
|
122
|
+
replacement = await options.onPayload?.(
|
|
123
|
+
structuredClone(original),
|
|
124
|
+
model,
|
|
125
|
+
),
|
|
126
|
+
payload = replacement === undefined ? original : (replacement as any);
|
|
127
|
+
const limit = entry.route.endsWith("/messages")
|
|
128
|
+
? "max_tokens"
|
|
129
|
+
: "max_output_tokens";
|
|
130
|
+
if (
|
|
131
|
+
!payload ||
|
|
132
|
+
payload.model !== model.id ||
|
|
133
|
+
payload.stream !== false ||
|
|
134
|
+
payload[limit] !== original[limit] ||
|
|
135
|
+
JSON.stringify(payload.tools) !== JSON.stringify(original.tools) ||
|
|
136
|
+
(!entry.route.endsWith("/messages") && payload.store !== false)
|
|
137
|
+
)
|
|
138
|
+
throw new XgateError("payload_hook_changed_scope");
|
|
139
|
+
const body = JSON.stringify(payload);
|
|
140
|
+
if (Buffer.byteLength(body) > 8 * 1024 * 1024)
|
|
141
|
+
throw new XgateError("request_bound");
|
|
142
|
+
const id = invocationId(
|
|
143
|
+
model.id,
|
|
144
|
+
context,
|
|
145
|
+
options.sessionId ?? identity(),
|
|
146
|
+
);
|
|
147
|
+
let observed = false;
|
|
148
|
+
attempted = id;
|
|
149
|
+
const result = await transport.purchase(
|
|
150
|
+
{ id, route: entry.route, body },
|
|
151
|
+
options.signal,
|
|
152
|
+
async (response) => {
|
|
153
|
+
observed = true;
|
|
154
|
+
await options.onResponse?.(response, model);
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
paid = result;
|
|
158
|
+
// In-process injected transports have no HTTP body; retain the observation hook there too.
|
|
159
|
+
if (!observed)
|
|
160
|
+
await options.onResponse?.(
|
|
161
|
+
{
|
|
162
|
+
status: 200,
|
|
163
|
+
headers: {
|
|
164
|
+
"content-type": "application/json",
|
|
165
|
+
"cache-control": "no-store",
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
model,
|
|
169
|
+
);
|
|
170
|
+
const value = decode(entry, result.body);
|
|
171
|
+
message.usage = value.usage;
|
|
172
|
+
message.responseId = value.responseId;
|
|
173
|
+
options.signal?.throwIfAborted();
|
|
174
|
+
events.push({ type: "start", partial: message });
|
|
175
|
+
for (const block of value.content) {
|
|
176
|
+
const contentIndex = message.content.length;
|
|
177
|
+
if (block.type === "text") {
|
|
178
|
+
message.content.push({ ...block, text: "" });
|
|
179
|
+
events.push({ type: "text_start", contentIndex, partial: message });
|
|
180
|
+
(message.content[contentIndex] as typeof block).text = block.text;
|
|
181
|
+
events.push({
|
|
182
|
+
type: "text_delta",
|
|
183
|
+
contentIndex,
|
|
184
|
+
delta: block.text,
|
|
185
|
+
partial: message,
|
|
186
|
+
});
|
|
187
|
+
events.push({
|
|
188
|
+
type: "text_end",
|
|
189
|
+
contentIndex,
|
|
190
|
+
content: block.text,
|
|
191
|
+
partial: message,
|
|
192
|
+
});
|
|
193
|
+
} else if (block.type === "thinking") {
|
|
194
|
+
message.content.push({
|
|
195
|
+
...block,
|
|
196
|
+
thinking: block.redacted ? block.thinking : "",
|
|
197
|
+
});
|
|
198
|
+
events.push({
|
|
199
|
+
type: "thinking_start",
|
|
200
|
+
contentIndex,
|
|
201
|
+
partial: message,
|
|
202
|
+
});
|
|
203
|
+
if (!block.redacted) {
|
|
204
|
+
(message.content[contentIndex] as typeof block).thinking =
|
|
205
|
+
block.thinking;
|
|
206
|
+
events.push({
|
|
207
|
+
type: "thinking_delta",
|
|
208
|
+
contentIndex,
|
|
209
|
+
delta: block.thinking,
|
|
210
|
+
partial: message,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
events.push({
|
|
214
|
+
type: "thinking_end",
|
|
215
|
+
contentIndex,
|
|
216
|
+
content: block.thinking,
|
|
217
|
+
partial: message,
|
|
218
|
+
});
|
|
219
|
+
} else {
|
|
220
|
+
message.content.push({ ...block, arguments: {} });
|
|
221
|
+
events.push({
|
|
222
|
+
type: "toolcall_start",
|
|
223
|
+
contentIndex,
|
|
224
|
+
partial: message,
|
|
225
|
+
});
|
|
226
|
+
(message.content[contentIndex] as typeof block).arguments =
|
|
227
|
+
block.arguments;
|
|
228
|
+
events.push({
|
|
229
|
+
type: "toolcall_delta",
|
|
230
|
+
contentIndex,
|
|
231
|
+
delta: JSON.stringify(block.arguments),
|
|
232
|
+
partial: message,
|
|
233
|
+
});
|
|
234
|
+
events.push({
|
|
235
|
+
type: "toolcall_end",
|
|
236
|
+
contentIndex,
|
|
237
|
+
toolCall: block,
|
|
238
|
+
partial: message,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
message.stopReason = value.stopReason;
|
|
243
|
+
try {
|
|
244
|
+
onPaid?.(result);
|
|
245
|
+
} catch {
|
|
246
|
+
/* UI diagnostics cannot reverse verified completion. */
|
|
247
|
+
}
|
|
248
|
+
events.push({ type: "done", reason: value.stopReason, message });
|
|
249
|
+
} catch (error) {
|
|
250
|
+
const aborted = options.signal?.aborted === true;
|
|
251
|
+
message.stopReason = aborted ? "aborted" : "error";
|
|
252
|
+
message.errorMessage = aborted
|
|
253
|
+
? "Xgate operation stopped. Inspect /xgate status; payment may require reconciliation."
|
|
254
|
+
: error instanceof XgateError
|
|
255
|
+
? error.message
|
|
256
|
+
: "Xgate operation stopped. Inspect /xgate status before requesting another purchase.";
|
|
257
|
+
if (attempted !== undefined) {
|
|
258
|
+
// After a verified result the charge is known even if Pi discards the content.
|
|
259
|
+
const payment =
|
|
260
|
+
paid?.payment ??
|
|
261
|
+
(error instanceof XgatePurchaseError ? error.payment : undefined);
|
|
262
|
+
const failure: XgatePurchaseFailure = {
|
|
263
|
+
invocationId: attempted,
|
|
264
|
+
code: error instanceof XgateError ? error.code : "purchase_failed",
|
|
265
|
+
pending:
|
|
266
|
+
paid === undefined &&
|
|
267
|
+
!(error instanceof XgatePurchaseError && !error.pending),
|
|
268
|
+
...(payment ? { payment } : {}),
|
|
269
|
+
};
|
|
270
|
+
// The original ID is the only reconciliation key. "Billing" also keeps Pi's transient
|
|
271
|
+
// error classifier from retrying a message whose hex ID happens to contain "500".
|
|
272
|
+
message.errorMessage += ` Billing invocation: ${attempted}.`;
|
|
273
|
+
try {
|
|
274
|
+
onPurchaseFailed?.(failure);
|
|
275
|
+
} catch {
|
|
276
|
+
/* Diagnostics cannot change the terminal outcome. */
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
events.push({
|
|
280
|
+
type: "error",
|
|
281
|
+
reason: message.stopReason,
|
|
282
|
+
error: message,
|
|
283
|
+
});
|
|
284
|
+
} finally {
|
|
285
|
+
events.end();
|
|
286
|
+
}
|
|
287
|
+
})();
|
|
288
|
+
return events;
|
|
289
|
+
};
|
|
290
|
+
return {
|
|
291
|
+
id: "xgate",
|
|
292
|
+
name: "xgate",
|
|
293
|
+
auth: {
|
|
294
|
+
apiKey: {
|
|
295
|
+
name: "Policy constrained xgate wallet",
|
|
296
|
+
resolve: async () => ({ auth: {}, source: "xgate wallet broker" }),
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
getModels: () => entries.map((e) => e.model),
|
|
300
|
+
refreshModels: async (context) => {
|
|
301
|
+
if (!context.allowNetwork) return;
|
|
302
|
+
const updated = catalogEntries(await transport.models(context.signal));
|
|
303
|
+
await context.publish({
|
|
304
|
+
update: () => {
|
|
305
|
+
entries = updated;
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
stream: (model, context, options) =>
|
|
310
|
+
stream(model, context, options as SimpleStreamOptions),
|
|
311
|
+
streamSimple: stream,
|
|
312
|
+
};
|
|
313
|
+
}
|