libfx 0.0.13 → 0.0.14-dev.1561.g1a070af32cdf

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.
@@ -0,0 +1,95 @@
1
+ // `libfx/durable-world` (durable/world.mjs): sessions kept in a World the app
2
+ // supplies, such as Workflow's Postgres World.
3
+ import type { Durability } from "./libfx.cjs";
4
+
5
+ export type { Durability } from "./libfx.cjs";
6
+
7
+ /**
8
+ * What libfx uses of a World: its events, streams, and queue. Workflow's
9
+ * Worlds fit. Methods are typed loosely so any conforming World is accepted.
10
+ */
11
+ export interface World {
12
+ readonly specVersion?: number;
13
+ readonly events: {
14
+ create(runId: string, data: unknown, params?: unknown): unknown;
15
+ list(params: unknown): unknown;
16
+ };
17
+ readonly streams: {
18
+ write(runId: string, name: string, chunk: string): unknown;
19
+ writeMulti?(runId: string, name: string, chunks: string[]): unknown;
20
+ get(runId: string, name: string, startIndex?: number): unknown;
21
+ getInfo(runId: string, name: string): unknown;
22
+ };
23
+ queue(queueName: string, message: unknown, options?: unknown): unknown;
24
+ createQueueHandler(prefix: string, handler: (message: unknown, meta: any) => Promise<unknown>): (request: Request) => Promise<Response>;
25
+ /** A World with `registerHandler` delivers to the agent in its process; any other delivers over HTTP to `agent.wakeHandler()`. */
26
+ registerHandler?(prefix: string, handler: (request: Request) => Promise<Response>): unknown;
27
+ getRuntimeDeadline?(): unknown;
28
+ getDeploymentId?(): unknown;
29
+ createRunId?(options?: Readonly<Record<string, unknown>>): string;
30
+ start?(): unknown;
31
+ close?(): unknown;
32
+ }
33
+
34
+ /** A lease as `alive()` sees it: the fields `holderInfo()` returned when it was taken, beside libfx's own. */
35
+ export type WorldLease = Readonly<Record<string, unknown>>;
36
+
37
+ /** A World, or a function that creates one. libfx starts and closes a World it creates; a World passed in stays the app's. */
38
+ export type WorldSource = World | (() => World | Promise<World>);
39
+
40
+ /** What `world()` cannot learn from the World itself. */
41
+ export type WorldOptions = {
42
+ /** The durability's name in errors. Default `"world"`. */
43
+ name?: string;
44
+ /**
45
+ * The store this World keeps sessions in. Queue messages name it, so one route hands each delivery to the agents on
46
+ * its store, and on one store an agent name belongs to one configuration. Give each customer of an app a store of its
47
+ * own. Default the durability's `name`.
48
+ */
49
+ store?: string;
50
+ /** The queue keeps a message until a delivery acknowledges it, across crashes. `prompt()` stores the prompt first either way. Default false. */
51
+ queueDurable?: boolean;
52
+ /** How often a waiting worker reads the session again. Default 1000. */
53
+ pollMs?: number;
54
+ /**
55
+ * The queue keeps a message from every other delivery while one handles it, and delivers it again once that
56
+ * delivery's process is gone, as Vercel Queues does. Without liveness, a worker then holds its session for as long as
57
+ * its delivery runs, up to its deadline, and renews nothing: the next delivery of the same message takes the session
58
+ * over. The World must pass each handler call the queue's `messageId` and delivery `attempt`. A worker with no
59
+ * deadline renews its lease all the same. Default false.
60
+ */
61
+ queueLeases?: boolean;
62
+ /**
63
+ * With `queueLeases`: a prompt runs in the request that took it when no worker holds the session, and the queue keeps
64
+ * the prompt as a backstop it delivers only after that request's deadline. The request's lease names the backstop, so
65
+ * if the request died, the backstop takes the session over. Default true with `queueLeases`.
66
+ */
67
+ runInRequest?: boolean;
68
+ /** Keeps the request alive until `work`, a turn it runs after its response, settles, such as Vercel's `waitUntil` or Next's `after()`. */
69
+ waitUntil?(work: Promise<void>): void;
70
+ /** Without liveness, how long a lease lasts after its worker took or last renewed it; renewed every third of that. At least 1000. Default 15000. */
71
+ leaseMs?: number;
72
+ /** Each delivery stops `reserveMs` before `maxDurationMs`, or before the World's `getRuntimeDeadline()`. No limit by default. */
73
+ maxDurationMs?: number;
74
+ /** Default 30000. */
75
+ reserveMs?: number;
76
+ /** What a worker puts in each lease it takes, for `alive()` to read. */
77
+ holderInfo?(): Record<string, unknown>;
78
+ /** The AI Gateway credential when the agent has no `apiKey`. */
79
+ gatewayKey?(): string | Promise<string>;
80
+ } & (
81
+ | {
82
+ /** `alive(lease)` answers whether a lease's holder still runs, so a crashed worker's session frees at once. */
83
+ livenessKnown: true;
84
+ /** True when the holder runs, false when it does not, and null when unknown. */
85
+ alive(lease: WorldLease): boolean | null;
86
+ }
87
+ | {
88
+ /** Without liveness, a worker renews its lease while it runs, and the session of one that died frees `leaseMs` after its last renewal. */
89
+ livenessKnown?: false;
90
+ alive?(lease: WorldLease): boolean | null;
91
+ }
92
+ );
93
+
94
+ /** Keeps sessions in a World the app supplies: its store, streams, and queue. */
95
+ export declare function world(source: WorldSource, options?: WorldOptions): Durability;
@@ -0,0 +1,61 @@
1
+ // `libfx/grok` (grok-sign-in.js): sign in with a Grok plan for a Grok agent.
2
+ import type { FxProviderCredentials, FxFetch } from "./libfx.cjs";
3
+
4
+ /** Options every network step accepts. */
5
+ export interface GrokRequestOptions {
6
+ /** Replaces the global `fetch` for xAI's sign-in endpoints. */
7
+ fetch?: FxFetch;
8
+ signal?: AbortSignal;
9
+ }
10
+
11
+ /** A sign-in in progress. Keep it on the server: `verifier` is secret. */
12
+ export interface GrokSignIn {
13
+ /** Where the user signs in. Afterwards xAI opens an address at `redirectUri` that does not load; the user pastes it back. */
14
+ readonly url: string;
15
+ readonly state: string;
16
+ readonly verifier: string;
17
+ readonly redirectUri: string;
18
+ }
19
+
20
+ /** A device code sign-in in progress. Plain data, so a later request can poll it. */
21
+ export interface GrokDeviceSignIn {
22
+ /** Where the user enters `userCode`. */
23
+ readonly verificationUrl: string;
24
+ readonly userCode: string;
25
+ readonly deviceCode: string;
26
+ /** How often to poll, in milliseconds. */
27
+ readonly intervalMs: number;
28
+ /** Milliseconds since the epoch. */
29
+ readonly expiresAt: number;
30
+ }
31
+
32
+ /** The `code` of an error from this module. */
33
+ export type GrokSignInErrorCode =
34
+ | "LIBFX_GROK_SIGN_IN_INVALID_ADDRESS"
35
+ | "LIBFX_GROK_SIGN_IN_STATE_MISMATCH"
36
+ | "LIBFX_GROK_SIGN_IN_DENIED"
37
+ | "LIBFX_GROK_SIGN_IN_EXPIRED"
38
+ | "LIBFX_GROK_SIGN_IN_FAILED"
39
+ | "LIBFX_GROK_DEVICE_CODE_UNAVAILABLE"
40
+ | "LIBFX_GROK_DEVICE_CODE_EXPIRED"
41
+ | "LIBFX_GROK_ACCOUNT_UNAVAILABLE"
42
+ | "LIBFX_GROK_ACCOUNT_CHANGED"
43
+ | "LIBFX_GROK_REFRESH_FAILED";
44
+
45
+ /** Starts a sign-in where the user pastes back the address xAI opens. */
46
+ export declare function startSignIn(): Promise<GrokSignIn>;
47
+
48
+ /** Finishes a sign-in from the pasted address, its query, or either wrapped in quotes or backticks. */
49
+ export declare function finishSignIn(signIn: GrokSignIn, address: string, options?: GrokRequestOptions): Promise<FxProviderCredentials>;
50
+
51
+ /** Starts a device code sign-in. */
52
+ export declare function startDeviceSignIn(options?: GrokRequestOptions): Promise<GrokDeviceSignIn>;
53
+
54
+ /** Checks once: credentials after the user entered the code, otherwise null. */
55
+ export declare function pollDeviceSignIn(device: GrokDeviceSignIn, options?: GrokRequestOptions): Promise<FxProviderCredentials | null>;
56
+
57
+ /** Waits until the user enters the code, polling at xAI's interval. */
58
+ export declare function completeDeviceSignIn(device: GrokDeviceSignIn, options?: GrokRequestOptions): Promise<FxProviderCredentials>;
59
+
60
+ /** Refreshes credentials. Grok agents refresh on their own; this is for hosts. */
61
+ export declare function refreshCredentials(credentials: FxProviderCredentials, options?: GrokRequestOptions): Promise<FxProviderCredentials>;