@capekai/core 1.0.10 → 1.0.11
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/package.json
CHANGED
package/src/adapters/ai-sdk.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { createOpenAI } from '@ai-sdk/openai';
|
|
2
2
|
import {
|
|
3
|
+
wrapLanguageModel,
|
|
3
4
|
dynamicTool,
|
|
4
5
|
jsonSchema,
|
|
5
6
|
streamText,
|
|
6
7
|
type JSONSchema7,
|
|
7
|
-
type LanguageModel,
|
|
8
8
|
type Tool,
|
|
9
9
|
} from 'ai';
|
|
10
10
|
import { getModelWithMetadata } from '../core/model-utils';
|
|
11
11
|
import { openAiModelOmitsTemperature } from '../core/provider-utils';
|
|
12
12
|
import type { ModelFactoryResult } from '../providers/types';
|
|
13
|
+
import { codexNetworkRetryMiddleware } from './codex-network-retry';
|
|
13
14
|
|
|
14
15
|
export interface TextModelRequest {
|
|
15
16
|
modelId?: string;
|
|
@@ -55,7 +56,10 @@ export function createOpenAiResponsesModel(request: OpenAiResponsesModelRequest)
|
|
|
55
56
|
fetch: request.fetch,
|
|
56
57
|
});
|
|
57
58
|
return {
|
|
58
|
-
model:
|
|
59
|
+
model: wrapLanguageModel({
|
|
60
|
+
model: openai.responses(request.modelId),
|
|
61
|
+
middleware: codexNetworkRetryMiddleware,
|
|
62
|
+
}),
|
|
59
63
|
useProviderInstructions: true,
|
|
60
64
|
omitMaxOutputTokens: true,
|
|
61
65
|
omitTemperature: openAiModelOmitsTemperature(request.modelId),
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { APICallError, type LanguageModelMiddleware } from 'ai';
|
|
2
|
+
import { ApiErrorType, classifyApiError } from '../utils/errors';
|
|
3
|
+
|
|
4
|
+
export const codexNetworkRetryMiddleware: LanguageModelMiddleware = {
|
|
5
|
+
specificationVersion: 'v3',
|
|
6
|
+
wrapStream: async ({ doStream, params }) => {
|
|
7
|
+
try {
|
|
8
|
+
return await doStream();
|
|
9
|
+
} catch (error: unknown) {
|
|
10
|
+
if (params.abortSignal?.aborted || APICallError.isInstance(error)
|
|
11
|
+
|| (error instanceof Error && error.name === 'AbortError')) throw error;
|
|
12
|
+
const classified = classifyApiError(error);
|
|
13
|
+
if (classified.type !== ApiErrorType.Network) throw error;
|
|
14
|
+
// OpenAI's early stream probe can throw a raw socket error. Normalize
|
|
15
|
+
// only before stream handoff so SDK retries this request, not prior tools.
|
|
16
|
+
throw new APICallError({
|
|
17
|
+
message: classified.message,
|
|
18
|
+
url: 'https://chatgpt.com/backend-api/codex/responses',
|
|
19
|
+
requestBodyValues: undefined,
|
|
20
|
+
cause: error,
|
|
21
|
+
isRetryable: true,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
};
|