@howaboua/pi-durable-imagegen 0.0.0-stage → 0.10.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.
@@ -0,0 +1,24 @@
1
+ export interface StoredChatGptCookie {
2
+ name: string;
3
+ value: string;
4
+ domain: string;
5
+ hostOnly: boolean;
6
+ path: string;
7
+ expiresAt?: number | undefined;
8
+ }
9
+ /** Server-owned session state, never a model tool or browser-cookie import. */
10
+ export interface CodexCookieStore {
11
+ requestHeader(url: URL): string | undefined | Promise<string | undefined>;
12
+ storeResponse(url: URL, setCookieHeaders: readonly string[]): void | Promise<void>;
13
+ }
14
+ export declare function isChatGptCookieUrl(url: URL): boolean;
15
+ export declare class ChatGptCloudflareCookieStore implements CodexCookieStore {
16
+ private readonly cookies;
17
+ constructor(snapshot?: unknown);
18
+ /** Sensitive host storage payload; do not expose it through Durable results. */
19
+ snapshot(): readonly StoredChatGptCookie[];
20
+ private removeExpired;
21
+ requestHeader(url: URL): string | undefined;
22
+ storeResponse(url: URL, setCookieHeaders: readonly string[]): void;
23
+ private storeCookie;
24
+ }
@@ -0,0 +1,7 @@
1
+ import { ChatGptCloudflareCookieStore } from "./cloudflare-cookies.ts";
2
+ import type { CodexToolProvider } from "./types.ts";
3
+ /** Marketplace-owned ephemeral cookies. Legacy direct factories retain their existing policy. */
4
+ export declare function createComponentCookieStores(): {
5
+ forProvider(provider: CodexToolProvider): ChatGptCloudflareCookieStore;
6
+ close(): void;
7
+ };
@@ -0,0 +1,8 @@
1
+ import type { CodexModel } from "./types.ts";
2
+ export interface CodexToolRouteConfig {
3
+ providers: Record<string, Record<string, string>>;
4
+ }
5
+ /** Parse the host-loaded pi-codex-tools.json without reading the native filesystem. */
6
+ export declare function normalizeCodexToolRouteConfig(value: unknown): CodexToolRouteConfig;
7
+ export declare function isCodexToolRoute(config: CodexToolRouteConfig, model: CodexModel | undefined): boolean;
8
+ export declare function resolveCodexToolModel(config: CodexToolRouteConfig, model: CodexModel | undefined, canonical: string): string;
@@ -0,0 +1,5 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import { type ToolExecutionApi } from "@earendil-works/pi-durable";
3
+ import type { CodexRuntimeOptions } from "./types.ts";
4
+ export declare function callingModel(api: ToolExecutionApi, options: CodexRuntimeOptions, context: Context): Promise<import("@earendil-works/pi-ai").Model<import("@earendil-works/pi-ai").Api> | undefined>;
5
+ export declare function providerSessionId(api: ToolExecutionApi, context: Context): Promise<string>;
@@ -0,0 +1,2 @@
1
+ import { type CodexToolProvider } from "./types.ts";
2
+ export declare function codexToolProviderHeaders(provider: CodexToolProvider, runtime?: "node" | "workerd"): Headers;
@@ -0,0 +1,10 @@
1
+ export declare function createNodeCodexTransport(): Promise<{
2
+ fetch(url: URL, init: {
3
+ method?: string;
4
+ headers: Headers;
5
+ body?: string;
6
+ signal?: AbortSignal;
7
+ redirect: "manual";
8
+ }): Promise<import("undici").Response>;
9
+ close(): Promise<void>;
10
+ }>;
@@ -0,0 +1,20 @@
1
+ import { type CodexCookieStore } from "./cloudflare-cookies.ts";
2
+ export interface CodexToolHttpResponse {
3
+ status: number;
4
+ statusText: string;
5
+ headers: Headers;
6
+ text: string;
7
+ /** Host-only sanitiser: apply to decoded strings, never blindly to JSON or image bytes. */
8
+ redact(text: string): string;
9
+ }
10
+ export declare function fetchCodexTool(url: string, options: {
11
+ method?: string;
12
+ headers?: Headers;
13
+ body?: string;
14
+ signal?: AbortSignal | null;
15
+ maxResponseBytes?: number;
16
+ runtime?: "node" | "workerd";
17
+ cookieStore?: CodexCookieStore;
18
+ /** Explicit host egress, e.g. a Worker service binding. No implicit network fallback. */
19
+ fetch?: typeof globalThis.fetch;
20
+ }): Promise<CodexToolHttpResponse>;
@@ -0,0 +1,3 @@
1
+ import type { JsonValue } from "@earendil-works/chord";
2
+ /** Keep tool details within Durable's persisted JSON contract. */
3
+ export declare function jsonValue(value: unknown): JsonValue;
@@ -0,0 +1,4 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import { type CodexModel, type CodexRuntimeOptions, type CodexToolProvider } from "./types.ts";
3
+ export declare function supportsExecutableCodexTool(model: CodexModel | undefined, options: CodexRuntimeOptions, images?: boolean): boolean;
4
+ export declare function resolveCodexToolProvider(current: CodexModel | undefined, options: CodexRuntimeOptions, context: Context): Promise<CodexToolProvider>;
@@ -0,0 +1,26 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import type { Api, Model, Models, ProviderHeaders } from "@earendil-works/pi-ai";
3
+ import type { CodexToolRouteConfig } from "./config.ts";
4
+ export declare const CODEX_TOOL_PROVIDER_UNSUPPORTED_MESSAGE = "Codex-backed tool requires an OpenAI Codex-compatible Responses provider";
5
+ export declare const CODEX_TOOL_ORIGINATOR = "codex_cli_rs";
6
+ export declare const OPENAI_CODEX_PROVIDER = "openai-codex";
7
+ export declare const DEFAULT_CODEX_BASE_URL = "https://chatgpt.com/backend-api";
8
+ export type CodexModel = Model<Api>;
9
+ export interface CodexToolProvider {
10
+ route: "openai-codex" | "configured-responses";
11
+ baseUrl: string;
12
+ responsesUrl: string;
13
+ searchUrl: string;
14
+ model: string | undefined;
15
+ token: string;
16
+ accountId: string;
17
+ headers?: ProviderHeaders;
18
+ }
19
+ export interface CodexRuntimeOptions {
20
+ models: Models;
21
+ routes?: CodexToolRouteConfig;
22
+ /** Explicit opt-in for an ordinary Responses backend with Codex tool endpoints. */
23
+ allowConfiguredProvider?: (model: CodexModel | undefined) => boolean;
24
+ allowCodexProviderFallback?: boolean;
25
+ resolveProvider?: (model: CodexModel | undefined, context: Context) => Promise<CodexToolProvider | undefined>;
26
+ }
@@ -0,0 +1,3 @@
1
+ export declare function resolveCodexApiProviderBaseUrl(modelBaseUrl: string | undefined): string;
2
+ export declare function resolveCodexResponsesUrl(providerBaseUrl: string): string;
3
+ export declare function resolveCodexSearchUrl(providerBaseUrl: string): string;
@@ -0,0 +1,9 @@
1
+ import { type ComponentInstance, type ComponentRequest } from "@howaboua/pi-durable-components";
2
+ import { createImageGenerationExtension, type ImageGenerationToolOptions } from "./index.ts";
3
+ /** All original route options, with explicit egress and conversation history access. */
4
+ export interface ImagegenComponentOptions extends ImageGenerationToolOptions {
5
+ fetch: typeof globalThis.fetch;
6
+ conversation: NonNullable<ImageGenerationToolOptions["conversation"]>;
7
+ }
8
+ export declare const imagegenOptions: import("@howaboua/pi-durable-components").ComponentCapability<ImagegenComponentOptions>;
9
+ export declare function createComponent(request: ComponentRequest): ComponentInstance<ReturnType<typeof createImageGenerationExtension>>;
@@ -0,0 +1,12 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import { type ExecutionEnv } from "@earendil-works/pi-durable/env";
3
+ import type { ImageResponse } from "./contract.ts";
4
+ import type { ImagegenOutput } from "./output.ts";
5
+ export declare function saveGeneratedImages(env: ExecutionEnv, context: Context, response: ImageResponse, requestId: string | undefined): Promise<{
6
+ output: ImagegenOutput;
7
+ content: {
8
+ type: "image";
9
+ mimeType: string;
10
+ data: string;
11
+ }[];
12
+ }>;
@@ -0,0 +1,27 @@
1
+ import type { JsonValue } from "@earendil-works/chord";
2
+ import { Type } from "typebox";
3
+ export declare const IMAGE_GENERATION_TOOL_NAME = "imagegen";
4
+ export declare const IMAGE_GENERATION_UNSUPPORTED_MESSAGE = "imagegen requires an image-capable OpenAI Codex-compatible Responses provider";
5
+ export declare const IMAGE_MODEL = "gpt-image-2.5";
6
+ export declare const MAX_EDIT_IMAGES = 5;
7
+ export declare const IMAGE_GENERATION_PARAMETERS: Type.TObject<{
8
+ prompt: Type.TString;
9
+ transparent_background: Type.TOptional<Type.TBoolean>;
10
+ referenced_image_paths: Type.TOptional<Type.TUnion<[Type.TArray<Type.TString>, Type.TNull]>>;
11
+ num_last_images_to_include: Type.TOptional<Type.TUnion<[Type.TInteger, Type.TNull]>>;
12
+ }>;
13
+ export interface ImagegenArgs {
14
+ prompt: string;
15
+ transparent_background?: boolean;
16
+ referenced_image_paths?: string[] | null;
17
+ num_last_images_to_include?: number | null;
18
+ }
19
+ export interface ImageResponse {
20
+ data: Array<{
21
+ b64_json: string;
22
+ }>;
23
+ background?: string | null;
24
+ quality?: string | null;
25
+ size?: string | null;
26
+ usage?: JsonValue;
27
+ }
@@ -0,0 +1,21 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import type { Conversation, ConversationId, ToolExecutionApi } from "@earendil-works/pi-durable";
3
+ import type { CodexCookieStore } from "../../../internal/codex/cloudflare-cookies.ts";
4
+ import type { CodexRuntimeOptions, CodexToolProvider } from "../../../internal/codex/types.ts";
5
+ import { type ImagegenArgs } from "./contract.ts";
6
+ export interface ImageGenerationToolOptions extends CodexRuntimeOptions {
7
+ /** Explicit host-owned egress; omitted by existing native callers. */
8
+ fetch?: typeof globalThis.fetch;
9
+ runtime?: "node" | "workerd";
10
+ cookieStore?: (provider: CodexToolProvider, context: Context) => CodexCookieStore | Promise<CodexCookieStore>;
11
+ /** Durable 1.0.2's tool conversation handle has no context reader. */
12
+ conversation?: (id: ConversationId, context: Context) => Promise<Pick<Conversation, "context"> | undefined>;
13
+ }
14
+ export declare function executeCodexImageGeneration(args: ImagegenArgs, api: ToolExecutionApi, context: Context, options: ImageGenerationToolOptions): Promise<{
15
+ output: import("./output.ts").ImagegenOutput;
16
+ content: {
17
+ type: "image";
18
+ mimeType: string;
19
+ data: string;
20
+ }[];
21
+ }>;
@@ -0,0 +1,2 @@
1
+ import type { Message } from "@earendil-works/pi-ai";
2
+ export declare function recentConversationImageUrls(messages: readonly Message[], count: number): string[];
@@ -0,0 +1 @@
1
+ export declare function validatedImageMime(bytes: Uint8Array): string | undefined;
@@ -0,0 +1,18 @@
1
+ import { type ImageGenerationToolOptions } from "./execute.ts";
2
+ export { normalizeCodexToolRouteConfig } from "../../../internal/codex/config.ts";
3
+ export type { CodexRuntimeOptions, CodexToolProvider, } from "../../../internal/codex/types.ts";
4
+ export { IMAGE_GENERATION_PARAMETERS } from "./contract.ts";
5
+ export type { ImageGenerationToolOptions } from "./execute.ts";
6
+ export type { ImagegenOutput } from "./output.ts";
7
+ export declare function createImageGenerationTool(options: ImageGenerationToolOptions): import("@earendil-works/pi-durable").ToolRegistration<import("typebox").TObject<{
8
+ prompt: import("typebox").TString;
9
+ transparent_background: import("typebox").TOptional<import("typebox").TBoolean>;
10
+ referenced_image_paths: import("typebox").TOptional<import("typebox").TUnion<[import("typebox").TArray<import("typebox").TString>, import("typebox").TNull]>>;
11
+ num_last_images_to_include: import("typebox").TOptional<import("typebox").TUnion<[import("typebox").TInteger, import("typebox").TNull]>>;
12
+ }>, import("@earendil-works/chord").JsonValue>;
13
+ export declare function createImageGenerationExtension(options: ImageGenerationToolOptions): import("@earendil-works/pi-durable").Extension<import("@earendil-works/pi-durable").ToolRegistration<import("typebox").TObject<{
14
+ prompt: import("typebox").TString;
15
+ transparent_background: import("typebox").TOptional<import("typebox").TBoolean>;
16
+ referenced_image_paths: import("typebox").TOptional<import("typebox").TUnion<[import("typebox").TArray<import("typebox").TString>, import("typebox").TNull]>>;
17
+ num_last_images_to_include: import("typebox").TOptional<import("typebox").TUnion<[import("typebox").TInteger, import("typebox").TNull]>>;
18
+ }>, import("@earendil-works/chord").JsonValue>>;
@@ -0,0 +1,19 @@
1
+ import type { JsonValue } from "@earendil-works/chord";
2
+ export interface SavedImage {
3
+ path: string;
4
+ absolute_path: string;
5
+ latest_path: string;
6
+ latest_absolute_path: string;
7
+ }
8
+ export type ImagegenOutput = {
9
+ path: string;
10
+ latest_path: string;
11
+ images: SavedImage[];
12
+ background?: string | null;
13
+ transparent_background?: boolean;
14
+ quality?: string | null;
15
+ size?: string | null;
16
+ imagegen_request_id?: string;
17
+ usage?: JsonValue;
18
+ };
19
+ export declare function formatImagegenOutput(output: ImagegenOutput): string;
@@ -0,0 +1,31 @@
1
+ import type { ImagegenOutput, SavedImage } from "./output.ts";
2
+ export interface ImagegenPresentationState {
3
+ images: SavedImage[];
4
+ detail: ImagegenOutput | null;
5
+ }
6
+ /** Project an existing receipt without acquiring image bytes or replaying generation. */
7
+ export declare function createImagegenPresentationState(output: ImagegenOutput): ImagegenPresentationState;
8
+ declare function parseState(value: unknown): ImagegenPresentationState;
9
+ /** Readonly receipts. The host owns binding, acquisition and ordinary tool invocation. */
10
+ export declare const imagegenCapability: {
11
+ id: string;
12
+ version: number;
13
+ parseState: typeof parseState;
14
+ actions: readonly string[];
15
+ streams: readonly string[];
16
+ presentations: readonly string[];
17
+ };
18
+ export declare const imagegenSummary: {
19
+ id: string;
20
+ select: (state: ImagegenPresentationState) => {
21
+ imageCount: number;
22
+ images: SavedImage[];
23
+ };
24
+ requests: readonly string[];
25
+ };
26
+ export declare const imagegenDetail: {
27
+ id: string;
28
+ select: (state: ImagegenPresentationState) => ImagegenOutput | null;
29
+ requests: readonly string[];
30
+ };
31
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import { type ExecutionEnv } from "@earendil-works/pi-durable/env";
3
+ import { type ImagegenArgs } from "./contract.ts";
4
+ export declare function buildImageGenerationRequest(args: ImagegenArgs, recentImages: string[] | undefined, env: ExecutionEnv, context: Context, model?: string): Promise<{
5
+ operation: "generations" | "edits";
6
+ body: Record<string, unknown>;
7
+ }>;
@@ -0,0 +1,90 @@
1
+ // packages/imagegen/src/presentation.ts
2
+ function createImagegenPresentationState(output) {
3
+ return parseState({ images: output.images, detail: null });
4
+ }
5
+ function saved(value) {
6
+ const image = record(value);
7
+ return {
8
+ path: string(image["path"]),
9
+ absolute_path: string(image["absolute_path"]),
10
+ latest_path: string(image["latest_path"]),
11
+ latest_absolute_path: string(image["latest_absolute_path"])
12
+ };
13
+ }
14
+ function parseState(value) {
15
+ const state = record(value);
16
+ if (!Array.isArray(state["images"]) || state["images"].length === 0)
17
+ throw new TypeError("Missing generated artifacts");
18
+ const images = state["images"].map(saved);
19
+ let detail = null;
20
+ if (state["detail"] !== null) {
21
+ const output = record(state["detail"]);
22
+ if (!Array.isArray(output["images"]))
23
+ throw new TypeError("Missing image output");
24
+ detail = {
25
+ path: string(output["path"]),
26
+ latest_path: string(output["latest_path"]),
27
+ images: output["images"].map(saved)
28
+ };
29
+ for (const key of ["background", "quality", "size"]) {
30
+ const item = output[key];
31
+ if (item !== undefined)
32
+ detail[key] = item === null ? null : string(item);
33
+ }
34
+ if (output["transparent_background"] !== undefined) {
35
+ if (typeof output["transparent_background"] !== "boolean")
36
+ throw new TypeError("Invalid transparency");
37
+ detail.transparent_background = output["transparent_background"];
38
+ }
39
+ if (output["imagegen_request_id"] !== undefined)
40
+ detail.imagegen_request_id = string(output["imagegen_request_id"]);
41
+ if (output["usage"] !== undefined) {
42
+ if (!isJson(output["usage"]))
43
+ throw new TypeError("Invalid usage metadata");
44
+ detail.usage = output["usage"];
45
+ }
46
+ if (JSON.stringify(detail.images) !== JSON.stringify(images))
47
+ throw new TypeError("Generated artifact mismatch");
48
+ }
49
+ return { images, detail };
50
+ }
51
+ function isJson(value) {
52
+ return value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value) || Array.isArray(value) && value.every(isJson) || !!value && typeof value === "object" && Object.values(value).every(isJson);
53
+ }
54
+ var imagegenCapability = {
55
+ id: "imagegen",
56
+ version: 1,
57
+ parseState,
58
+ actions: [],
59
+ streams: [],
60
+ presentations: ["summary", "detail"]
61
+ };
62
+ var imagegenSummary = {
63
+ id: "summary",
64
+ select: (state) => ({
65
+ imageCount: state.images.length,
66
+ images: state.images
67
+ }),
68
+ requests: ["detail"]
69
+ };
70
+ var imagegenDetail = {
71
+ id: "detail",
72
+ select: (state) => state.detail,
73
+ requests: []
74
+ };
75
+ function record(value) {
76
+ if (!value || typeof value !== "object" || Array.isArray(value))
77
+ throw new TypeError("Expected a receipt object");
78
+ return value;
79
+ }
80
+ function string(value) {
81
+ if (typeof value !== "string")
82
+ throw new TypeError("Expected a string");
83
+ return value;
84
+ }
85
+ export {
86
+ createImagegenPresentationState,
87
+ imagegenCapability,
88
+ imagegenDetail,
89
+ imagegenSummary
90
+ };
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
- "name": "@howaboua/pi-durable-imagegen",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@howaboua/pi-durable-imagegen",
3
+ "version": "0.10.0",
4
+ "description": "Codex image generation and editing for Pi Durable",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/IgorWarzocha/pi-durable-components.git",
10
+ "directory": "packages/imagegen"
11
+ },
12
+ "author": "Igor Warzocha",
13
+ "engines": {
14
+ "node": ">=22.19.0"
15
+ },
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/packages/imagegen/src/index.d.ts",
18
+ "exports": {
19
+ "./presentation": {
20
+ "types": "./dist/packages/imagegen/src/presentation.d.ts",
21
+ "import": "./dist/presentation.js"
22
+ },
23
+ ".": {
24
+ "types": "./dist/packages/imagegen/src/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ },
27
+ "./component": {
28
+ "types": "./dist/packages/imagegen/src/activation.d.ts",
29
+ "import": "./dist/activation.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "README.md",
35
+ "NOTICE",
36
+ "LICENSE"
37
+ ],
38
+ "sideEffects": false,
39
+ "dependencies": {
40
+ "proxy-from-env": "2.1.0",
41
+ "undici": "8.10.2",
42
+ "typebox": "1.3.27",
43
+ "@howaboua/pi-durable-components": "0.10.0"
44
+ },
45
+ "peerDependencies": {
46
+ "@earendil-works/chord": "1.4.0",
47
+ "@earendil-works/pi-ai": "1.4.0",
48
+ "@earendil-works/pi-durable": "1.4.0",
49
+ "@modelcontextprotocol/sdk": ">=1.25.2 <2"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "durable": {
55
+ "schemaVersion": 1,
56
+ "kind": "extension",
57
+ "entry": "./component",
58
+ "runtimes": [
59
+ "node"
60
+ ],
61
+ "requires": [
62
+ "durable.models.v1",
63
+ "durable.network.v1"
64
+ ],
65
+ "permissions": [
66
+ "filesystem",
67
+ "network",
68
+ "models"
69
+ ]
70
+ }
71
+ }