@janole/ai-sdk-provider-codex-asp 0.2.3 → 0.2.4

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/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV3CallOptions, LanguageModelV3, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, LanguageModelV3StreamPart, LanguageModelV3Prompt, ProviderV3 } from '@ai-sdk/provider';
1
+ import { LanguageModelV3CallOptions, LanguageModelV3, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, LanguageModelV3StreamPart, ProviderV3, LanguageModelV3Prompt } from '@ai-sdk/provider';
2
2
 
3
3
  /** Base error type for this provider package. */
4
4
  declare class CodexProviderError extends Error {
@@ -1403,24 +1403,6 @@ declare class CodexEventMapper {
1403
1403
  map(event: CodexEventMapperInput): LanguageModelV3StreamPart[];
1404
1404
  }
1405
1405
 
1406
- /**
1407
- * Extracts system messages from the prompt and concatenates them into a single
1408
- * string suitable for `developerInstructions` on `thread/start` or
1409
- * `thread/resume`. Returns `undefined` when no system content is present.
1410
- */
1411
- declare function mapSystemPrompt(prompt: LanguageModelV3Prompt): string | undefined;
1412
- /**
1413
- * Maps the prompt to the `input` array for a `turn/start` request.
1414
- *
1415
- * System messages are **not** included here — they are routed to
1416
- * `developerInstructions` via {@link mapSystemPrompt} instead.
1417
- *
1418
- * @param isResume - When true the thread already holds the full history on
1419
- * disk, so only the last user message is extracted and sent. When false
1420
- * (fresh thread) all user text is folded into a single item.
1421
- */
1422
- declare function mapPromptToTurnInput(prompt: LanguageModelV3Prompt, isResume?: boolean): CodexTurnInputItem[];
1423
-
1424
1406
  declare const CODEX_PROVIDER_ID = "@janole/ai-sdk-provider-codex-asp";
1425
1407
  declare function codexProviderMetadata(threadId: string | undefined): {
1426
1408
  "@janole/ai-sdk-provider-codex-asp": {
@@ -1442,4 +1424,95 @@ declare const codexAppServer: CodexProvider;
1442
1424
  */
1443
1425
  declare const createCodexProvider: typeof createCodexAppServer;
1444
1426
 
1445
- export { type AgentMessageDeltaNotification, AppServerClient, type AppServerClientSettings, ApprovalsDispatcher, type ApprovalsDispatcherSettings, type AskForApproval, CODEX_PROVIDER_ID, type CodexCommandApprovalRequest, type CodexDynamicToolDefinition, CodexEventMapper, type CodexEventMapperInput, type CodexEventMapperOptions, type CodexFileChangeApprovalRequest, type CodexInitializeParams, type CodexInitializeResult, type CodexInitializedNotification, CodexLanguageModel, type CodexLanguageModelSettings, type CodexModelConfig, CodexNotImplementedError, type CodexNotification, type CodexProvider, CodexProviderError, type CodexProviderSettings, type CodexThreadDefaults, type CodexThreadResumeParams, type CodexThreadResumeResult, type CodexThreadStartParams, type CodexThreadStartResult, type CodexToolCallDeltaNotification, type CodexToolCallFinishedNotification, type CodexToolCallRequestParams, type CodexToolCallResult, type CodexToolCallStartedNotification, type CodexToolResultContentItem, type CodexTransport, type CodexTransportEventMap, type CodexTurnInputImage, type CodexTurnInputItem, type CodexTurnInputLocalImage, type CodexTurnInputMention, type CodexTurnInputSkill, type CodexTurnInputText, type CodexTurnStartParams, type CodexTurnStartResult, CodexWorker, CodexWorkerPool, type CodexWorkerPoolSettings, type CodexWorkerSettings, type CommandApprovalHandler, type CommandExecutionApprovalDecision, type CommandExecutionRequestApprovalParams, type CommandExecutionRequestApprovalResponse, type DynamicToolDefinition, type DynamicToolExecutionContext, type DynamicToolHandler, DynamicToolsDispatcher, type DynamicToolsDispatcherSettings, type FileChangeApprovalDecision, type FileChangeApprovalHandler, type FileChangeRequestApprovalParams, type FileChangeRequestApprovalResponse, type ItemCompletedNotification, type ItemStartedNotification, JsonRpcError, type JsonRpcErrorResponse, type JsonRpcId, type JsonRpcMessage, type JsonRpcMessageBase, type JsonRpcNotification, type JsonRpcRequest, type JsonRpcResponse, type JsonRpcSuccessResponse, PACKAGE_NAME, PACKAGE_VERSION, type PendingToolCall, PersistentTransport, type PersistentTransportSettings, type SandboxMode, StdioTransport, type StdioTransportSettings, type ThreadTokenUsageUpdatedNotification, type TurnCompletedNotification, type TurnStartedNotification, WebSocketTransport, type WebSocketTransportSettings, codexAppServer, codexProviderMetadata, createCodexAppServer, createCodexProvider, mapPromptToTurnInput, mapSystemPrompt, withProviderMetadata };
1427
+ /**
1428
+ * Extracts system messages from the prompt and concatenates them into a single
1429
+ * string suitable for `developerInstructions` on `thread/start` or
1430
+ * `thread/resume`. Returns `undefined` when no system content is present.
1431
+ */
1432
+ declare function mapSystemPrompt(prompt: LanguageModelV3Prompt): string | undefined;
1433
+ /**
1434
+ * Pluggable backend for persisting inline binary data so that the Codex
1435
+ * protocol can reference it by URL.
1436
+ *
1437
+ * Implement this interface to use a different storage backend (e.g. S3, GCS).
1438
+ *
1439
+ * - A `file:` URL maps to `{ type: "localImage", path }` in the Codex protocol.
1440
+ * - An `http(s):` URL maps to `{ type: "image", url }`.
1441
+ */
1442
+ interface FileWriter {
1443
+ /** Persist `data` and return a URL that Codex can use to access it. */
1444
+ write(data: Uint8Array | string, mediaType: string): Promise<URL>;
1445
+ /**
1446
+ * Remove previously written files. Best-effort — implementations should
1447
+ * never throw.
1448
+ */
1449
+ cleanup(urls: URL[]): Promise<void>;
1450
+ }
1451
+ /**
1452
+ * A {@link FileWriter} that writes to `os.tmpdir()` and returns `file:` URLs.
1453
+ */
1454
+ declare class LocalFileWriter implements FileWriter {
1455
+ write(data: Uint8Array | string, mediaType: string): Promise<URL>;
1456
+ cleanup(urls: URL[]): Promise<void>;
1457
+ }
1458
+ /**
1459
+ * Resolves inline binary data in AI SDK prompts and maps user content to
1460
+ * {@link CodexTurnInputItem} arrays ready for `turn/start`.
1461
+ *
1462
+ * Instantiate with an optional custom {@link FileWriter} for non-local storage
1463
+ * (e.g. S3). Tracks all written URLs so that {@link cleanup} can remove them
1464
+ * after the turn completes.
1465
+ *
1466
+ * @example
1467
+ * ```ts
1468
+ * const fileResolver = new PromptFileResolver();
1469
+ * const turnInput = await fileResolver.resolve(prompt, isResume);
1470
+ * // … after the turn …
1471
+ * await fileResolver.cleanup();
1472
+ * ```
1473
+ */
1474
+ declare class PromptFileResolver {
1475
+ private readonly writer;
1476
+ private readonly written;
1477
+ constructor(writer?: FileWriter);
1478
+ /**
1479
+ * Resolve inline file data and map user content to Codex input items.
1480
+ *
1481
+ * - Inline image data (base64 / Uint8Array) is written via the
1482
+ * {@link FileWriter} and converted to `localImage` or `image` items.
1483
+ * - URL-based image file parts are converted directly.
1484
+ * - Inline text file data is decoded and inlined as text.
1485
+ * - Unsupported media types are silently skipped.
1486
+ *
1487
+ * @param isResume - When true only the last user message is extracted.
1488
+ * When false (fresh thread) all user text is accumulated with images
1489
+ * flushing the text buffer to preserve ordering.
1490
+ */
1491
+ resolve(prompt: LanguageModelV3Prompt, isResume?: boolean): Promise<CodexTurnInputItem[]>;
1492
+ /**
1493
+ * Remove all files created by previous {@link resolve} calls.
1494
+ * Best-effort — never throws.
1495
+ */
1496
+ cleanup(): Promise<void>;
1497
+ /**
1498
+ * Convert a resolved image URL to a Codex input item.
1499
+ */
1500
+ private mapImageUrl;
1501
+ /**
1502
+ * Resolve a single file part: write inline data via the writer, then
1503
+ * convert to a Codex input item. Text files are decoded and returned
1504
+ * as text items. Returns `null` for unsupported media types.
1505
+ */
1506
+ private resolveFilePart;
1507
+ /**
1508
+ * Resume path: extract parts from the last user message individually.
1509
+ */
1510
+ private resolveResumed;
1511
+ /**
1512
+ * Fresh thread path: accumulate text chunks across all user messages,
1513
+ * flushing before each image to preserve ordering.
1514
+ */
1515
+ private resolveFresh;
1516
+ }
1517
+
1518
+ export { type AgentMessageDeltaNotification, AppServerClient, type AppServerClientSettings, ApprovalsDispatcher, type ApprovalsDispatcherSettings, type AskForApproval, CODEX_PROVIDER_ID, type CodexCommandApprovalRequest, type CodexDynamicToolDefinition, CodexEventMapper, type CodexEventMapperInput, type CodexEventMapperOptions, type CodexFileChangeApprovalRequest, type CodexInitializeParams, type CodexInitializeResult, type CodexInitializedNotification, CodexLanguageModel, type CodexLanguageModelSettings, type CodexModelConfig, CodexNotImplementedError, type CodexNotification, type CodexProvider, CodexProviderError, type CodexProviderSettings, type CodexThreadDefaults, type CodexThreadResumeParams, type CodexThreadResumeResult, type CodexThreadStartParams, type CodexThreadStartResult, type CodexToolCallDeltaNotification, type CodexToolCallFinishedNotification, type CodexToolCallRequestParams, type CodexToolCallResult, type CodexToolCallStartedNotification, type CodexToolResultContentItem, type CodexTransport, type CodexTransportEventMap, type CodexTurnInputImage, type CodexTurnInputItem, type CodexTurnInputLocalImage, type CodexTurnInputMention, type CodexTurnInputSkill, type CodexTurnInputText, type CodexTurnStartParams, type CodexTurnStartResult, CodexWorker, CodexWorkerPool, type CodexWorkerPoolSettings, type CodexWorkerSettings, type CommandApprovalHandler, type CommandExecutionApprovalDecision, type CommandExecutionRequestApprovalParams, type CommandExecutionRequestApprovalResponse, type DynamicToolDefinition, type DynamicToolExecutionContext, type DynamicToolHandler, DynamicToolsDispatcher, type DynamicToolsDispatcherSettings, type FileChangeApprovalDecision, type FileChangeApprovalHandler, type FileChangeRequestApprovalParams, type FileChangeRequestApprovalResponse, type FileWriter, type ItemCompletedNotification, type ItemStartedNotification, JsonRpcError, type JsonRpcErrorResponse, type JsonRpcId, type JsonRpcMessage, type JsonRpcMessageBase, type JsonRpcNotification, type JsonRpcRequest, type JsonRpcResponse, type JsonRpcSuccessResponse, LocalFileWriter, PACKAGE_NAME, PACKAGE_VERSION, type PendingToolCall, PersistentTransport, type PersistentTransportSettings, PromptFileResolver, type SandboxMode, StdioTransport, type StdioTransportSettings, type ThreadTokenUsageUpdatedNotification, type TurnCompletedNotification, type TurnStartedNotification, WebSocketTransport, type WebSocketTransportSettings, codexAppServer, codexProviderMetadata, createCodexAppServer, createCodexProvider, mapSystemPrompt, withProviderMetadata };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV3CallOptions, LanguageModelV3, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, LanguageModelV3StreamPart, LanguageModelV3Prompt, ProviderV3 } from '@ai-sdk/provider';
1
+ import { LanguageModelV3CallOptions, LanguageModelV3, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, LanguageModelV3StreamPart, ProviderV3, LanguageModelV3Prompt } from '@ai-sdk/provider';
2
2
 
3
3
  /** Base error type for this provider package. */
4
4
  declare class CodexProviderError extends Error {
@@ -1403,24 +1403,6 @@ declare class CodexEventMapper {
1403
1403
  map(event: CodexEventMapperInput): LanguageModelV3StreamPart[];
1404
1404
  }
1405
1405
 
1406
- /**
1407
- * Extracts system messages from the prompt and concatenates them into a single
1408
- * string suitable for `developerInstructions` on `thread/start` or
1409
- * `thread/resume`. Returns `undefined` when no system content is present.
1410
- */
1411
- declare function mapSystemPrompt(prompt: LanguageModelV3Prompt): string | undefined;
1412
- /**
1413
- * Maps the prompt to the `input` array for a `turn/start` request.
1414
- *
1415
- * System messages are **not** included here — they are routed to
1416
- * `developerInstructions` via {@link mapSystemPrompt} instead.
1417
- *
1418
- * @param isResume - When true the thread already holds the full history on
1419
- * disk, so only the last user message is extracted and sent. When false
1420
- * (fresh thread) all user text is folded into a single item.
1421
- */
1422
- declare function mapPromptToTurnInput(prompt: LanguageModelV3Prompt, isResume?: boolean): CodexTurnInputItem[];
1423
-
1424
1406
  declare const CODEX_PROVIDER_ID = "@janole/ai-sdk-provider-codex-asp";
1425
1407
  declare function codexProviderMetadata(threadId: string | undefined): {
1426
1408
  "@janole/ai-sdk-provider-codex-asp": {
@@ -1442,4 +1424,95 @@ declare const codexAppServer: CodexProvider;
1442
1424
  */
1443
1425
  declare const createCodexProvider: typeof createCodexAppServer;
1444
1426
 
1445
- export { type AgentMessageDeltaNotification, AppServerClient, type AppServerClientSettings, ApprovalsDispatcher, type ApprovalsDispatcherSettings, type AskForApproval, CODEX_PROVIDER_ID, type CodexCommandApprovalRequest, type CodexDynamicToolDefinition, CodexEventMapper, type CodexEventMapperInput, type CodexEventMapperOptions, type CodexFileChangeApprovalRequest, type CodexInitializeParams, type CodexInitializeResult, type CodexInitializedNotification, CodexLanguageModel, type CodexLanguageModelSettings, type CodexModelConfig, CodexNotImplementedError, type CodexNotification, type CodexProvider, CodexProviderError, type CodexProviderSettings, type CodexThreadDefaults, type CodexThreadResumeParams, type CodexThreadResumeResult, type CodexThreadStartParams, type CodexThreadStartResult, type CodexToolCallDeltaNotification, type CodexToolCallFinishedNotification, type CodexToolCallRequestParams, type CodexToolCallResult, type CodexToolCallStartedNotification, type CodexToolResultContentItem, type CodexTransport, type CodexTransportEventMap, type CodexTurnInputImage, type CodexTurnInputItem, type CodexTurnInputLocalImage, type CodexTurnInputMention, type CodexTurnInputSkill, type CodexTurnInputText, type CodexTurnStartParams, type CodexTurnStartResult, CodexWorker, CodexWorkerPool, type CodexWorkerPoolSettings, type CodexWorkerSettings, type CommandApprovalHandler, type CommandExecutionApprovalDecision, type CommandExecutionRequestApprovalParams, type CommandExecutionRequestApprovalResponse, type DynamicToolDefinition, type DynamicToolExecutionContext, type DynamicToolHandler, DynamicToolsDispatcher, type DynamicToolsDispatcherSettings, type FileChangeApprovalDecision, type FileChangeApprovalHandler, type FileChangeRequestApprovalParams, type FileChangeRequestApprovalResponse, type ItemCompletedNotification, type ItemStartedNotification, JsonRpcError, type JsonRpcErrorResponse, type JsonRpcId, type JsonRpcMessage, type JsonRpcMessageBase, type JsonRpcNotification, type JsonRpcRequest, type JsonRpcResponse, type JsonRpcSuccessResponse, PACKAGE_NAME, PACKAGE_VERSION, type PendingToolCall, PersistentTransport, type PersistentTransportSettings, type SandboxMode, StdioTransport, type StdioTransportSettings, type ThreadTokenUsageUpdatedNotification, type TurnCompletedNotification, type TurnStartedNotification, WebSocketTransport, type WebSocketTransportSettings, codexAppServer, codexProviderMetadata, createCodexAppServer, createCodexProvider, mapPromptToTurnInput, mapSystemPrompt, withProviderMetadata };
1427
+ /**
1428
+ * Extracts system messages from the prompt and concatenates them into a single
1429
+ * string suitable for `developerInstructions` on `thread/start` or
1430
+ * `thread/resume`. Returns `undefined` when no system content is present.
1431
+ */
1432
+ declare function mapSystemPrompt(prompt: LanguageModelV3Prompt): string | undefined;
1433
+ /**
1434
+ * Pluggable backend for persisting inline binary data so that the Codex
1435
+ * protocol can reference it by URL.
1436
+ *
1437
+ * Implement this interface to use a different storage backend (e.g. S3, GCS).
1438
+ *
1439
+ * - A `file:` URL maps to `{ type: "localImage", path }` in the Codex protocol.
1440
+ * - An `http(s):` URL maps to `{ type: "image", url }`.
1441
+ */
1442
+ interface FileWriter {
1443
+ /** Persist `data` and return a URL that Codex can use to access it. */
1444
+ write(data: Uint8Array | string, mediaType: string): Promise<URL>;
1445
+ /**
1446
+ * Remove previously written files. Best-effort — implementations should
1447
+ * never throw.
1448
+ */
1449
+ cleanup(urls: URL[]): Promise<void>;
1450
+ }
1451
+ /**
1452
+ * A {@link FileWriter} that writes to `os.tmpdir()` and returns `file:` URLs.
1453
+ */
1454
+ declare class LocalFileWriter implements FileWriter {
1455
+ write(data: Uint8Array | string, mediaType: string): Promise<URL>;
1456
+ cleanup(urls: URL[]): Promise<void>;
1457
+ }
1458
+ /**
1459
+ * Resolves inline binary data in AI SDK prompts and maps user content to
1460
+ * {@link CodexTurnInputItem} arrays ready for `turn/start`.
1461
+ *
1462
+ * Instantiate with an optional custom {@link FileWriter} for non-local storage
1463
+ * (e.g. S3). Tracks all written URLs so that {@link cleanup} can remove them
1464
+ * after the turn completes.
1465
+ *
1466
+ * @example
1467
+ * ```ts
1468
+ * const fileResolver = new PromptFileResolver();
1469
+ * const turnInput = await fileResolver.resolve(prompt, isResume);
1470
+ * // … after the turn …
1471
+ * await fileResolver.cleanup();
1472
+ * ```
1473
+ */
1474
+ declare class PromptFileResolver {
1475
+ private readonly writer;
1476
+ private readonly written;
1477
+ constructor(writer?: FileWriter);
1478
+ /**
1479
+ * Resolve inline file data and map user content to Codex input items.
1480
+ *
1481
+ * - Inline image data (base64 / Uint8Array) is written via the
1482
+ * {@link FileWriter} and converted to `localImage` or `image` items.
1483
+ * - URL-based image file parts are converted directly.
1484
+ * - Inline text file data is decoded and inlined as text.
1485
+ * - Unsupported media types are silently skipped.
1486
+ *
1487
+ * @param isResume - When true only the last user message is extracted.
1488
+ * When false (fresh thread) all user text is accumulated with images
1489
+ * flushing the text buffer to preserve ordering.
1490
+ */
1491
+ resolve(prompt: LanguageModelV3Prompt, isResume?: boolean): Promise<CodexTurnInputItem[]>;
1492
+ /**
1493
+ * Remove all files created by previous {@link resolve} calls.
1494
+ * Best-effort — never throws.
1495
+ */
1496
+ cleanup(): Promise<void>;
1497
+ /**
1498
+ * Convert a resolved image URL to a Codex input item.
1499
+ */
1500
+ private mapImageUrl;
1501
+ /**
1502
+ * Resolve a single file part: write inline data via the writer, then
1503
+ * convert to a Codex input item. Text files are decoded and returned
1504
+ * as text items. Returns `null` for unsupported media types.
1505
+ */
1506
+ private resolveFilePart;
1507
+ /**
1508
+ * Resume path: extract parts from the last user message individually.
1509
+ */
1510
+ private resolveResumed;
1511
+ /**
1512
+ * Fresh thread path: accumulate text chunks across all user messages,
1513
+ * flushing before each image to preserve ordering.
1514
+ */
1515
+ private resolveFresh;
1516
+ }
1517
+
1518
+ export { type AgentMessageDeltaNotification, AppServerClient, type AppServerClientSettings, ApprovalsDispatcher, type ApprovalsDispatcherSettings, type AskForApproval, CODEX_PROVIDER_ID, type CodexCommandApprovalRequest, type CodexDynamicToolDefinition, CodexEventMapper, type CodexEventMapperInput, type CodexEventMapperOptions, type CodexFileChangeApprovalRequest, type CodexInitializeParams, type CodexInitializeResult, type CodexInitializedNotification, CodexLanguageModel, type CodexLanguageModelSettings, type CodexModelConfig, CodexNotImplementedError, type CodexNotification, type CodexProvider, CodexProviderError, type CodexProviderSettings, type CodexThreadDefaults, type CodexThreadResumeParams, type CodexThreadResumeResult, type CodexThreadStartParams, type CodexThreadStartResult, type CodexToolCallDeltaNotification, type CodexToolCallFinishedNotification, type CodexToolCallRequestParams, type CodexToolCallResult, type CodexToolCallStartedNotification, type CodexToolResultContentItem, type CodexTransport, type CodexTransportEventMap, type CodexTurnInputImage, type CodexTurnInputItem, type CodexTurnInputLocalImage, type CodexTurnInputMention, type CodexTurnInputSkill, type CodexTurnInputText, type CodexTurnStartParams, type CodexTurnStartResult, CodexWorker, CodexWorkerPool, type CodexWorkerPoolSettings, type CodexWorkerSettings, type CommandApprovalHandler, type CommandExecutionApprovalDecision, type CommandExecutionRequestApprovalParams, type CommandExecutionRequestApprovalResponse, type DynamicToolDefinition, type DynamicToolExecutionContext, type DynamicToolHandler, DynamicToolsDispatcher, type DynamicToolsDispatcherSettings, type FileChangeApprovalDecision, type FileChangeApprovalHandler, type FileChangeRequestApprovalParams, type FileChangeRequestApprovalResponse, type FileWriter, type ItemCompletedNotification, type ItemStartedNotification, JsonRpcError, type JsonRpcErrorResponse, type JsonRpcId, type JsonRpcMessage, type JsonRpcMessageBase, type JsonRpcNotification, type JsonRpcRequest, type JsonRpcResponse, type JsonRpcSuccessResponse, LocalFileWriter, PACKAGE_NAME, PACKAGE_VERSION, type PendingToolCall, PersistentTransport, type PersistentTransportSettings, PromptFileResolver, type SandboxMode, StdioTransport, type StdioTransportSettings, type ThreadTokenUsageUpdatedNotification, type TurnCompletedNotification, type TurnStartedNotification, WebSocketTransport, type WebSocketTransportSettings, codexAppServer, codexProviderMetadata, createCodexAppServer, createCodexProvider, mapSystemPrompt, withProviderMetadata };
package/dist/index.js CHANGED
@@ -1,4 +1,9 @@
1
1
  import { spawn } from 'child_process';
2
+ import { randomUUID } from 'crypto';
3
+ import { writeFile, unlink } from 'fs/promises';
4
+ import { tmpdir } from 'os';
5
+ import { join } from 'path';
6
+ import { fileURLToPath, pathToFileURL } from 'url';
2
7
  import { NoSuchModelError } from '@ai-sdk/provider';
3
8
 
4
9
  // src/utils/object.ts
@@ -928,7 +933,7 @@ var DynamicToolsDispatcher = class {
928
933
  // package.json
929
934
  var package_default = {
930
935
  name: "@janole/ai-sdk-provider-codex-asp",
931
- version: "0.2.3"};
936
+ version: "0.2.4"};
932
937
 
933
938
  // src/package-info.ts
934
939
  var PACKAGE_NAME = package_default.name;
@@ -1315,8 +1320,6 @@ var CodexEventMapper = class {
1315
1320
  return parts;
1316
1321
  }
1317
1322
  };
1318
-
1319
- // src/protocol/prompt-mapper.ts
1320
1323
  function mapSystemPrompt(prompt) {
1321
1324
  const chunks = [];
1322
1325
  for (const message of prompt) {
@@ -1329,8 +1332,111 @@ function mapSystemPrompt(prompt) {
1329
1332
  }
1330
1333
  return chunks.length > 0 ? chunks.join("\n\n") : void 0;
1331
1334
  }
1332
- function mapPromptToTurnInput(prompt, isResume = false) {
1333
- if (isResume) {
1335
+ function textItem(text) {
1336
+ return { type: "text", text, text_elements: [] };
1337
+ }
1338
+ var MEDIA_TYPE_TO_EXT = {
1339
+ "image/png": ".png",
1340
+ "image/jpeg": ".jpg",
1341
+ "image/gif": ".gif",
1342
+ "image/webp": ".webp",
1343
+ "image/svg+xml": ".svg",
1344
+ "image/bmp": ".bmp",
1345
+ "image/tiff": ".tiff"
1346
+ };
1347
+ function extensionForMediaType(mediaType) {
1348
+ return MEDIA_TYPE_TO_EXT[mediaType] ?? ".bin";
1349
+ }
1350
+ var LocalFileWriter = class {
1351
+ async write(data, mediaType) {
1352
+ const ext = extensionForMediaType(mediaType);
1353
+ const filename = `codex-ai-sdk-${randomUUID()}${ext}`;
1354
+ const filepath = join(tmpdir(), filename);
1355
+ const buffer = typeof data === "string" ? Buffer.from(data, "base64") : data;
1356
+ await writeFile(filepath, buffer);
1357
+ return pathToFileURL(filepath);
1358
+ }
1359
+ async cleanup(urls) {
1360
+ await Promise.allSettled(
1361
+ urls.filter((u) => u.protocol === "file:").map((u) => unlink(u))
1362
+ );
1363
+ }
1364
+ };
1365
+ var PromptFileResolver = class {
1366
+ writer;
1367
+ written = [];
1368
+ constructor(writer) {
1369
+ this.writer = writer ?? new LocalFileWriter();
1370
+ }
1371
+ /**
1372
+ * Resolve inline file data and map user content to Codex input items.
1373
+ *
1374
+ * - Inline image data (base64 / Uint8Array) is written via the
1375
+ * {@link FileWriter} and converted to `localImage` or `image` items.
1376
+ * - URL-based image file parts are converted directly.
1377
+ * - Inline text file data is decoded and inlined as text.
1378
+ * - Unsupported media types are silently skipped.
1379
+ *
1380
+ * @param isResume - When true only the last user message is extracted.
1381
+ * When false (fresh thread) all user text is accumulated with images
1382
+ * flushing the text buffer to preserve ordering.
1383
+ */
1384
+ async resolve(prompt, isResume = false) {
1385
+ if (isResume) {
1386
+ return this.resolveResumed(prompt);
1387
+ }
1388
+ return this.resolveFresh(prompt);
1389
+ }
1390
+ /**
1391
+ * Remove all files created by previous {@link resolve} calls.
1392
+ * Best-effort — never throws.
1393
+ */
1394
+ async cleanup() {
1395
+ const urls = this.written.splice(0);
1396
+ if (urls.length > 0) {
1397
+ await this.writer.cleanup(urls);
1398
+ }
1399
+ }
1400
+ /**
1401
+ * Convert a resolved image URL to a Codex input item.
1402
+ */
1403
+ mapImageUrl(mediaType, data) {
1404
+ if (!mediaType.startsWith("image/")) {
1405
+ return null;
1406
+ }
1407
+ if (data.protocol === "file:") {
1408
+ return { type: "localImage", path: fileURLToPath(data) };
1409
+ }
1410
+ return { type: "image", url: data.href };
1411
+ }
1412
+ /**
1413
+ * Resolve a single file part: write inline data via the writer, then
1414
+ * convert to a Codex input item. Text files are decoded and returned
1415
+ * as text items. Returns `null` for unsupported media types.
1416
+ */
1417
+ async resolveFilePart(part) {
1418
+ const { mediaType, data } = part;
1419
+ if (mediaType.startsWith("text/")) {
1420
+ if (data instanceof URL) {
1421
+ return textItem(data.href);
1422
+ }
1423
+ const text = typeof data === "string" ? Buffer.from(data, "base64").toString("utf-8") : new TextDecoder().decode(data);
1424
+ return textItem(text);
1425
+ }
1426
+ if (mediaType.startsWith("image/") && !(data instanceof URL)) {
1427
+ const url = await this.writer.write(data, mediaType);
1428
+ this.written.push(url);
1429
+ return this.mapImageUrl(mediaType, url);
1430
+ }
1431
+ if (data instanceof URL) {
1432
+ return this.mapImageUrl(mediaType, data);
1433
+ }
1434
+ return null;
1435
+ }
1436
+ /**
1437
+ * Resume path: extract parts from the last user message individually.
1438
+ */
1439
+ async resolveResumed(prompt) {
1334
1440
  for (let i = prompt.length - 1; i >= 0; i--) {
1335
1441
  const message = prompt[i];
1336
1442
  if (message?.role === "user") {
@@ -1339,7 +1445,12 @@ function mapPromptToTurnInput(prompt, isResume = false) {
1339
1445
  if (part.type === "text") {
1340
1446
  const text = part.text.trim();
1341
1447
  if (text.length > 0) {
1342
- items.push({ type: "text", text, text_elements: [] });
1448
+ items.push(textItem(text));
1449
+ }
1450
+ } else if (part.type === "file") {
1451
+ const mapped = await this.resolveFilePart(part);
1452
+ if (mapped) {
1453
+ items.push(mapped);
1343
1454
  }
1344
1455
  }
1345
1456
  }
@@ -1348,21 +1459,45 @@ function mapPromptToTurnInput(prompt, isResume = false) {
1348
1459
  }
1349
1460
  return [];
1350
1461
  }
1351
- const chunks = [];
1352
- for (const message of prompt) {
1353
- if (message.role === "user") {
1354
- for (const part of message.content) {
1355
- if (part.type === "text") {
1356
- const text = part.text.trim();
1357
- if (text.length > 0) {
1358
- chunks.push(text);
1462
+ /**
1463
+ * Fresh thread path: accumulate text chunks across all user messages,
1464
+ * flushing before each image to preserve ordering.
1465
+ */
1466
+ async resolveFresh(prompt) {
1467
+ const items = [];
1468
+ const textChunks = [];
1469
+ const flushText = () => {
1470
+ if (textChunks.length > 0) {
1471
+ items.push(textItem(textChunks.join("\n\n")));
1472
+ textChunks.length = 0;
1473
+ }
1474
+ };
1475
+ for (const message of prompt) {
1476
+ if (message.role === "user") {
1477
+ for (const part of message.content) {
1478
+ if (part.type === "text") {
1479
+ const text = part.text.trim();
1480
+ if (text.length > 0) {
1481
+ textChunks.push(text);
1482
+ }
1483
+ } else if (part.type === "file") {
1484
+ const mapped = await this.resolveFilePart(part);
1485
+ if (mapped) {
1486
+ if (mapped.type === "text") {
1487
+ textChunks.push(mapped.text);
1488
+ } else {
1489
+ flushText();
1490
+ items.push(mapped);
1491
+ }
1492
+ }
1359
1493
  }
1360
1494
  }
1361
1495
  }
1362
1496
  }
1497
+ flushText();
1498
+ return items;
1363
1499
  }
1364
- return [{ type: "text", text: chunks.join("\n\n"), text_elements: [] }];
1365
- }
1500
+ };
1366
1501
 
1367
1502
  // src/model.ts
1368
1503
  function createEmptyUsage() {
@@ -1629,6 +1764,7 @@ var CodexLanguageModel = class {
1629
1764
  debugLog?.("outbound", "turn/interrupt", interruptParams);
1630
1765
  await client.request("turn/interrupt", interruptParams, interruptTimeoutMs);
1631
1766
  };
1767
+ const fileResolver = new PromptFileResolver();
1632
1768
  const stream = new ReadableStream({
1633
1769
  start: (controller) => {
1634
1770
  let closed = false;
@@ -1641,6 +1777,7 @@ var CodexLanguageModel = class {
1641
1777
  try {
1642
1778
  controller.close();
1643
1779
  } finally {
1780
+ await fileResolver.cleanup();
1644
1781
  await client.disconnect();
1645
1782
  }
1646
1783
  };
@@ -1652,6 +1789,7 @@ var CodexLanguageModel = class {
1652
1789
  try {
1653
1790
  controller.close();
1654
1791
  } finally {
1792
+ await fileResolver.cleanup();
1655
1793
  await client.disconnect();
1656
1794
  }
1657
1795
  };
@@ -1876,7 +2014,7 @@ var CodexLanguageModel = class {
1876
2014
  closeSuccessfully
1877
2015
  );
1878
2016
  }
1879
- const turnInput = mapPromptToTurnInput(options.prompt, !!resumeThreadId);
2017
+ const turnInput = await fileResolver.resolve(options.prompt, !!resumeThreadId);
1880
2018
  const turnStartParams = stripUndefined({
1881
2019
  threadId,
1882
2020
  input: turnInput,
@@ -1900,6 +2038,7 @@ var CodexLanguageModel = class {
1900
2038
  await interruptTurnIfPossible();
1901
2039
  } catch {
1902
2040
  }
2041
+ await fileResolver.cleanup();
1903
2042
  await client.disconnect();
1904
2043
  }
1905
2044
  });
@@ -2070,6 +2209,6 @@ function createCodexAppServer(settings = {}) {
2070
2209
  var codexAppServer = createCodexAppServer();
2071
2210
  var createCodexProvider = createCodexAppServer;
2072
2211
 
2073
- export { AppServerClient, ApprovalsDispatcher, CODEX_PROVIDER_ID, CodexEventMapper, CodexLanguageModel, CodexNotImplementedError, CodexProviderError, CodexWorker, CodexWorkerPool, DynamicToolsDispatcher, JsonRpcError, PACKAGE_NAME, PACKAGE_VERSION, PersistentTransport, StdioTransport, WebSocketTransport, codexAppServer, codexProviderMetadata, createCodexAppServer, createCodexProvider, mapPromptToTurnInput, mapSystemPrompt, withProviderMetadata };
2212
+ export { AppServerClient, ApprovalsDispatcher, CODEX_PROVIDER_ID, CodexEventMapper, CodexLanguageModel, CodexNotImplementedError, CodexProviderError, CodexWorker, CodexWorkerPool, DynamicToolsDispatcher, JsonRpcError, LocalFileWriter, PACKAGE_NAME, PACKAGE_VERSION, PersistentTransport, PromptFileResolver, StdioTransport, WebSocketTransport, codexAppServer, codexProviderMetadata, createCodexAppServer, createCodexProvider, mapSystemPrompt, withProviderMetadata };
2074
2213
  //# sourceMappingURL=index.js.map
2075
2214
  //# sourceMappingURL=index.js.map