@ai-translate/provider-openai 0.2.2 → 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.mts +2 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +12 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -16,6 +16,8 @@ export interface OpenAiTransportOptions {
|
|
|
16
16
|
/**
|
|
17
17
|
* Speaks the OpenAI Chat Completions dialect of the engine's request contract.
|
|
18
18
|
* Retries stay off here because the engine owns the retry and repair loop.
|
|
19
|
+
* Client creation is deferred so configuration validation and dry runs work
|
|
20
|
+
* before credentials are configured.
|
|
19
21
|
*/
|
|
20
22
|
export declare function createOpenAiTransport(options?: OpenAiTransportOptions): StructuredCompletionTransport$1;
|
|
21
23
|
export interface OpenAiTranslationProviderOptions extends Omit<StructuredTranslationProviderOptions, "model" | "transport"> {
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;qBAwCa;qBACA;iBAII;EACf;EACA,SAAS;EACT
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;;;qBAwCa;qBACA;iBAII;EACf;EACA,SAAS;EACT;;;;;;;;wBASc,sBACd,UAAS,yBACR;iBA2Dc,yCACP,KAAK;EACb;EACA,SAAS;EACT;;qBAGW,kCAAkC;EAC7C,YAAY,UAAS;;iBAiBN,2CACP,KAAK;EACb;EACA,SAAS;;qBAGE,oCAAoC;EAC/C,YAAY,UAAS;;wBAeP,gCACd,UAAS,mCACR;wBAIa,kCACd,UAAS,qCACR"}
|
package/dist/index.mjs
CHANGED
|
@@ -12,17 +12,23 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 45e3;
|
|
|
12
12
|
/**
|
|
13
13
|
* Speaks the OpenAI Chat Completions dialect of the engine's request contract.
|
|
14
14
|
* Retries stay off here because the engine owns the retry and repair loop.
|
|
15
|
+
* Client creation is deferred so configuration validation and dry runs work
|
|
16
|
+
* before credentials are configured.
|
|
15
17
|
*/
|
|
16
18
|
function createOpenAiTransport(options = {}) {
|
|
17
|
-
if (!options.client && !options.apiKey) throw new Error("OpenAI transport requires either apiKey or client.");
|
|
18
19
|
const requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
maxRetries: 0,
|
|
22
|
-
timeout: requestTimeoutMs
|
|
23
|
-
});
|
|
20
|
+
const apiKey = options.apiKey;
|
|
21
|
+
let client = options.client;
|
|
24
22
|
return {
|
|
25
23
|
async complete(request) {
|
|
24
|
+
if (client === void 0) {
|
|
25
|
+
if (!apiKey?.trim()) throw new Error("OpenAI transport requires either apiKey or client. Set OPENAI_API_KEY before translating.");
|
|
26
|
+
client = new OpenAI({
|
|
27
|
+
apiKey,
|
|
28
|
+
maxRetries: 0,
|
|
29
|
+
timeout: requestTimeoutMs
|
|
30
|
+
});
|
|
31
|
+
}
|
|
26
32
|
const completion = await client.chat.completions.parse({
|
|
27
33
|
messages: request.messages.map((message) => ({
|
|
28
34
|
content: message.content,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { SemanticAuditProvider, TranslationProvider } from \"@ai-translate/core/types\";\nimport {\n StructuredSemanticAuditProvider,\n StructuredTranslationProvider,\n} from \"@ai-translate/provider-core\";\nimport type {\n StructuredCompletionRequest,\n StructuredCompletionTransport,\n StructuredSemanticAuditProviderOptions,\n StructuredTranslationProviderOptions,\n} from \"@ai-translate/provider-core\";\nimport OpenAI from \"openai\";\nimport { zodResponseFormat } from \"openai/helpers/zod\";\n\nexport {\n SEMANTIC_AUDIT_OUTPUT_CONTRACT_MATERIAL,\n SEMANTIC_AUDIT_OUTPUT_CONTRACT_REVISION,\n TRANSLATION_OUTPUT_CONTRACT_MATERIAL,\n TRANSLATION_OUTPUT_CONTRACT_REVISION,\n createSemanticAuditOutputContractRevision,\n createTranslationOutputContractRevision,\n} from \"@ai-translate/provider-core\";\nexport type {\n ReasoningEffort,\n SemanticAuditOutputContractMaterial,\n SemanticAuditPrompt,\n SemanticAuditPromptArgs,\n SemanticAuditResponseCache,\n StructuredCompletionMessage,\n StructuredCompletionRequest,\n StructuredCompletionTransport,\n SystemPrompt,\n SystemPromptArgs,\n TranslationOutputContractMaterial,\n} from \"@ai-translate/provider-core\";\n\n/**\n * Reasoning-capable and inexpensive. Callers who want a different trade-off\n * pass `model` explicitly.\n */\nexport const DEFAULT_MODEL = \"gpt-5.6-luna\";\nexport const DEFAULT_REASONING_EFFORT = \"medium\";\n\nconst DEFAULT_REQUEST_TIMEOUT_MS = 45_000;\n\nexport interface OpenAiTransportOptions {\n apiKey?: string;\n client?: OpenAI;\n requestTimeoutMs?: number;\n}\n\n/**\n * Speaks the OpenAI Chat Completions dialect of the engine's request contract.\n * Retries stay off here because the engine owns the retry and repair loop.\n */\nexport function createOpenAiTransport(\n options: OpenAiTransportOptions = {},\n): StructuredCompletionTransport {\n
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { SemanticAuditProvider, TranslationProvider } from \"@ai-translate/core/types\";\nimport {\n StructuredSemanticAuditProvider,\n StructuredTranslationProvider,\n} from \"@ai-translate/provider-core\";\nimport type {\n StructuredCompletionRequest,\n StructuredCompletionTransport,\n StructuredSemanticAuditProviderOptions,\n StructuredTranslationProviderOptions,\n} from \"@ai-translate/provider-core\";\nimport OpenAI from \"openai\";\nimport { zodResponseFormat } from \"openai/helpers/zod\";\n\nexport {\n SEMANTIC_AUDIT_OUTPUT_CONTRACT_MATERIAL,\n SEMANTIC_AUDIT_OUTPUT_CONTRACT_REVISION,\n TRANSLATION_OUTPUT_CONTRACT_MATERIAL,\n TRANSLATION_OUTPUT_CONTRACT_REVISION,\n createSemanticAuditOutputContractRevision,\n createTranslationOutputContractRevision,\n} from \"@ai-translate/provider-core\";\nexport type {\n ReasoningEffort,\n SemanticAuditOutputContractMaterial,\n SemanticAuditPrompt,\n SemanticAuditPromptArgs,\n SemanticAuditResponseCache,\n StructuredCompletionMessage,\n StructuredCompletionRequest,\n StructuredCompletionTransport,\n SystemPrompt,\n SystemPromptArgs,\n TranslationOutputContractMaterial,\n} from \"@ai-translate/provider-core\";\n\n/**\n * Reasoning-capable and inexpensive. Callers who want a different trade-off\n * pass `model` explicitly.\n */\nexport const DEFAULT_MODEL = \"gpt-5.6-luna\";\nexport const DEFAULT_REASONING_EFFORT = \"medium\";\n\nconst DEFAULT_REQUEST_TIMEOUT_MS = 45_000;\n\nexport interface OpenAiTransportOptions {\n apiKey?: string;\n client?: OpenAI;\n requestTimeoutMs?: number;\n}\n\n/**\n * Speaks the OpenAI Chat Completions dialect of the engine's request contract.\n * Retries stay off here because the engine owns the retry and repair loop.\n * Client creation is deferred so configuration validation and dry runs work\n * before credentials are configured.\n */\nexport function createOpenAiTransport(\n options: OpenAiTransportOptions = {},\n): StructuredCompletionTransport {\n const requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;\n const apiKey = options.apiKey;\n let client = options.client;\n\n return {\n async complete(request: StructuredCompletionRequest): Promise<unknown> {\n if (client === undefined) {\n if (!apiKey?.trim()) {\n throw new Error(\"OpenAI transport requires either apiKey or client. Set OPENAI_API_KEY before translating.\");\n }\n client = new OpenAI({ apiKey, maxRetries: 0, timeout: requestTimeoutMs });\n }\n const completion = await client.chat.completions.parse(\n {\n messages: request.messages.map((message) => ({\n content: message.content,\n role: message.role,\n })),\n model: request.modelId,\n ...(request.maxCompletionTokens === undefined\n ? {}\n : { max_completion_tokens: request.maxCompletionTokens }),\n ...(request.promptCacheKey === undefined\n ? {}\n : { prompt_cache_key: request.promptCacheKey }),\n ...(request.reasoningEffort === undefined\n ? {}\n : { reasoning_effort: request.reasoningEffort }),\n response_format: zodResponseFormat(request.schema, request.schemaName),\n ...(request.temperature === undefined ? {} : { temperature: request.temperature }),\n },\n {\n maxRetries: 0,\n ...(request.signal === undefined ? {} : { signal: request.signal }),\n timeout: requestTimeoutMs,\n },\n );\n if (completion.usage !== undefined) {\n request.onUsage?.({\n inputTokens: completion.usage.prompt_tokens,\n outputTokens: completion.usage.completion_tokens,\n ...(completion.usage.prompt_tokens_details?.cached_tokens === undefined\n ? {}\n : { cachedInputTokens: completion.usage.prompt_tokens_details.cached_tokens }),\n ...(completion.usage.completion_tokens_details?.reasoning_tokens === undefined\n ? {}\n : { reasoningTokens: completion.usage.completion_tokens_details.reasoning_tokens }),\n ...(completion.usage.prompt_tokens_details?.cache_write_tokens === undefined\n ? {}\n : { cacheWriteInputTokens: completion.usage.prompt_tokens_details.cache_write_tokens }),\n });\n }\n return completion.choices[0]?.message.parsed;\n },\n label: \"OpenAI\",\n };\n}\n\nexport interface OpenAiTranslationProviderOptions\n extends Omit<StructuredTranslationProviderOptions, \"model\" | \"transport\"> {\n apiKey?: string;\n client?: OpenAI;\n model?: string;\n}\n\nexport class OpenAiTranslationProvider extends StructuredTranslationProvider {\n constructor(options: OpenAiTranslationProviderOptions = {}) {\n const { apiKey, client, model, ...engineOptions } = options;\n super({\n ...engineOptions,\n model: model ?? DEFAULT_MODEL,\n reasoningEffort: engineOptions.reasoningEffort ?? DEFAULT_REASONING_EFFORT,\n transport: createOpenAiTransport({\n ...(apiKey === undefined ? {} : { apiKey }),\n ...(client === undefined ? {} : { client }),\n ...(options.requestTimeoutMs === undefined\n ? {}\n : { requestTimeoutMs: options.requestTimeoutMs }),\n }),\n });\n }\n}\n\nexport interface OpenAiSemanticAuditProviderOptions\n extends Omit<StructuredSemanticAuditProviderOptions, \"transport\"> {\n apiKey?: string;\n client?: OpenAI;\n}\n\nexport class OpenAiSemanticAuditProvider extends StructuredSemanticAuditProvider {\n constructor(options: OpenAiSemanticAuditProviderOptions = {}) {\n const { apiKey, client, ...engineOptions } = options;\n super({\n ...engineOptions,\n transport: createOpenAiTransport({\n ...(apiKey === undefined ? {} : { apiKey }),\n ...(client === undefined ? {} : { client }),\n ...(options.requestTimeoutMs === undefined\n ? {}\n : { requestTimeoutMs: options.requestTimeoutMs }),\n }),\n });\n }\n}\n\nexport function createOpenAiTranslationProvider(\n options: OpenAiTranslationProviderOptions = {},\n): TranslationProvider {\n return new OpenAiTranslationProvider(options);\n}\n\nexport function createOpenAiSemanticAuditProvider(\n options: OpenAiSemanticAuditProviderOptions = {},\n): SemanticAuditProvider {\n return new OpenAiSemanticAuditProvider(options);\n}\n"],"mappings":";;;;;;;;AAwCA,MAAa,gBAAgB;AAC7B,MAAa,2BAA2B;AAExC,MAAM,6BAA6B;;;;;;;AAcnC,SAAgB,sBACd,UAAkC,CAAC,GACJ;CAC/B,MAAM,mBAAmB,QAAQ,oBAAoB;CACrD,MAAM,SAAS,QAAQ;CACvB,IAAI,SAAS,QAAQ;CAErB,OAAO;EACL,MAAM,SAAS,SAAwD;GACrE,IAAI,WAAW,KAAA,GAAW;IACxB,IAAI,CAAC,QAAQ,KAAK,GAChB,MAAM,IAAI,MAAM,2FAA2F;IAE7G,SAAS,IAAI,OAAO;KAAE;KAAQ,YAAY;KAAG,SAAS;IAAiB,CAAC;GAC1E;GACA,MAAM,aAAa,MAAM,OAAO,KAAK,YAAY,MAC/C;IACE,UAAU,QAAQ,SAAS,KAAK,aAAa;KAC3C,SAAS,QAAQ;KACjB,MAAM,QAAQ;IAChB,EAAE;IACF,OAAO,QAAQ;IACf,GAAI,QAAQ,wBAAwB,KAAA,IAChC,CAAC,IACD,EAAE,uBAAuB,QAAQ,oBAAoB;IACzD,GAAI,QAAQ,mBAAmB,KAAA,IAC3B,CAAC,IACD,EAAE,kBAAkB,QAAQ,eAAe;IAC/C,GAAI,QAAQ,oBAAoB,KAAA,IAC5B,CAAC,IACD,EAA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AiD,gCAAgC;CAC/E,YAAY,UAA8C,CAAC,GAAG;EAC5D,MAAM,EAAE,QAAQ,QAAQ,GAAG,kBAAkB;EAC7C,MAAM;GACJ,GAAG;GACH,WAAW,sBAAsB;IAC/B,GAAI,WAAW,KAAA,IAAY,CAAC,IAAI,EAAE,OAAO;IACzC,GAAI,WAAW,KAAA,IAAY,CAAC,IAAI,EAAE,OAAO;IACzC,GAAI,QAAQ,qBAAqB,KAAA,IAC7B,CAAC,IACD,EAAE,kBAAkB,QAAQ,iBAAiB;GACnD,CAAC;EACH,CAAC;CACH;AACF;AAEA,SAAgB,gCACd,UAA4C,CAAC,GACxB;CACrB,OAAO,IAAI,0BAA0B,OAAO;AAC9C;AAEA,SAAgB,kCACd,UAA8C,CAAC,GACxB;CACvB,OAAO,IAAI,4BAA4B,OAAO;AAChD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-translate/provider-openai",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "OpenAI translation and semantic-audit providers for ai-translate, with structured output and batching.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"openai": "7.18.0",
|
|
46
46
|
"zod": "4.6.5",
|
|
47
|
-
"@ai-translate/core": "0.
|
|
48
|
-
"@ai-translate/provider-core": "0.3.
|
|
47
|
+
"@ai-translate/core": "0.4.0",
|
|
48
|
+
"@ai-translate/provider-core": "0.3.3"
|
|
49
49
|
},
|
|
50
50
|
"engines": {
|
|
51
51
|
"node": ">=22.0.0"
|