Skip to content

Commit 2bd71ae

Browse files
feat(api): add translation endpoint
1 parent 32c703f commit 2bd71ae

6 files changed

Lines changed: 214 additions & 2 deletions

File tree

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 30
1+
configured_endpoints: 31
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/writerai%2Fwriter-a8e20e751972f7b8c948a833764b02d0835b380189eaf7393d63eb2250dbd598.yml
33
openapi_spec_hash: 6f27335468bc5bf01fbcd1454330fb07
4-
config_hash: ee2e5a914fd298a6f4c740f5ce187f87
4+
config_hash: 1f6d0bf7309d0007e28ab85b89a0de85

api.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ Methods:
157157

158158
- <code title="post /v1/tools/comprehend/medical">client.tools.comprehend.<a href="./src/resources/tools/comprehend.ts">medical</a>({ ...params }) -> ComprehendMedicalResponse</code>
159159

160+
# Translation
161+
162+
Types:
163+
164+
- <code><a href="./src/resources/translation.ts">TranslationRequest</a></code>
165+
- <code><a href="./src/resources/translation.ts">TranslationResponse</a></code>
166+
167+
Methods:
168+
169+
- <code title="post /v1/translation">client.translation.<a href="./src/resources/translation.ts">translate</a>({ ...params }) -> TranslationResponse</code>
170+
160171
# Vision
161172

162173
Types:

src/client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ import {
7979
QuestionResponseChunk,
8080
} from './resources/graphs';
8181
import { ModelListResponse, Models } from './resources/models';
82+
import {
83+
Translation,
84+
TranslationRequest,
85+
TranslationResponse,
86+
TranslationTranslateParams,
87+
} from './resources/translation';
8288
import { Vision, VisionAnalyzeParams, VisionRequest, VisionResponse } from './resources/vision';
8389
import { readEnv } from './internal/utils/env';
8490
import { formatRequestDetails, loggerFor } from './internal/utils/log';
@@ -778,6 +784,7 @@ export class Writer {
778784
graphs: API.Graphs = new API.Graphs(this);
779785
files: API.Files = new API.Files(this);
780786
tools: API.Tools = new API.Tools(this);
787+
translation: API.Translation = new API.Translation(this);
781788
vision: API.Vision = new API.Vision(this);
782789
}
783790
Writer.Applications = Applications;
@@ -787,6 +794,7 @@ Writer.Models = Models;
787794
Writer.Graphs = Graphs;
788795
Writer.Files = Files;
789796
Writer.Tools = Tools;
797+
Writer.Translation = Translation;
790798
Writer.Vision = Vision;
791799
export declare namespace Writer {
792800
export type RequestOptions = Opts.RequestOptions;
@@ -877,6 +885,13 @@ export declare namespace Writer {
877885
type ToolParsePdfParams as ToolParsePdfParams,
878886
};
879887

888+
export {
889+
Translation as Translation,
890+
type TranslationRequest as TranslationRequest,
891+
type TranslationResponse as TranslationResponse,
892+
type TranslationTranslateParams as TranslationTranslateParams,
893+
};
894+
880895
export {
881896
Vision as Vision,
882897
type VisionRequest as VisionRequest,

src/resources/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,10 @@ export {
7171
type ToolContextAwareSplittingParams,
7272
type ToolParsePdfParams,
7373
} from './tools/tools';
74+
export {
75+
Translation,
76+
type TranslationRequest,
77+
type TranslationResponse,
78+
type TranslationTranslateParams,
79+
} from './translation';
7480
export { Vision, type VisionRequest, type VisionResponse, type VisionAnalyzeParams } from './vision';

src/resources/translation.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import { APIResource } from '../core/resource';
4+
import { APIPromise } from '../core/api-promise';
5+
import { RequestOptions } from '../internal/request-options';
6+
7+
export class Translation extends APIResource {
8+
/**
9+
* Translate text from one language to another.
10+
*/
11+
translate(body: TranslationTranslateParams, options?: RequestOptions): APIPromise<TranslationResponse> {
12+
return this._client.post('/v1/translation', { body, ...options });
13+
}
14+
}
15+
16+
export interface TranslationRequest {
17+
/**
18+
* Whether to use formal or informal language in the translation. See the
19+
* [list of languages that support formality](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#formality).
20+
* If the language does not support formality, this parameter is ignored.
21+
*/
22+
formality: boolean;
23+
24+
/**
25+
* Whether to control the length of the translated text. See the
26+
* [list of languages that support length control](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#length-control).
27+
* If the language does not support length control, this parameter is ignored.
28+
*/
29+
length_control: boolean;
30+
31+
/**
32+
* Whether to mask profane words in the translated text. See the
33+
* [list of languages that do not support profanity masking](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#profanity-masking).
34+
* If the language does not support profanity masking, this parameter is ignored.
35+
*/
36+
mask_profanity: boolean;
37+
38+
/**
39+
* The model to use for translation.
40+
*/
41+
model: 'palmyra-translate';
42+
43+
/**
44+
* The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
45+
* language code of the original text to translate. For example, `en` for English,
46+
* `zh` for Chinese, `fr` for French, `es` for Spanish. If the language has a
47+
* variant, the code appends the two-digit
48+
* [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
49+
* For example, Mexican Spanish is `es-MX`. See the
50+
* [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
51+
*/
52+
source_language_code: string;
53+
54+
/**
55+
* The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
56+
* language code of the target language for the translation. For example, `en` for
57+
* English, `zh` for Chinese, `fr` for French, `es` for Spanish. If the language
58+
* has a variant, the code appends the two-digit
59+
* [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
60+
* For example, Mexican Spanish is `es-MX`. See the
61+
* [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
62+
*/
63+
target_language_code: string;
64+
65+
/**
66+
* The text to translate. Maximum of 100,000 words.
67+
*/
68+
text: string;
69+
}
70+
71+
export interface TranslationResponse {
72+
/**
73+
* The result of the translation.
74+
*/
75+
data: string;
76+
}
77+
78+
export interface TranslationTranslateParams {
79+
/**
80+
* Whether to use formal or informal language in the translation. See the
81+
* [list of languages that support formality](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#formality).
82+
* If the language does not support formality, this parameter is ignored.
83+
*/
84+
formality: boolean;
85+
86+
/**
87+
* Whether to control the length of the translated text. See the
88+
* [list of languages that support length control](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#length-control).
89+
* If the language does not support length control, this parameter is ignored.
90+
*/
91+
length_control: boolean;
92+
93+
/**
94+
* Whether to mask profane words in the translated text. See the
95+
* [list of languages that do not support profanity masking](https://dev.writer.com/api-guides/api-reference/translation-api/language-support#profanity-masking).
96+
* If the language does not support profanity masking, this parameter is ignored.
97+
*/
98+
mask_profanity: boolean;
99+
100+
/**
101+
* The model to use for translation.
102+
*/
103+
model: 'palmyra-translate';
104+
105+
/**
106+
* The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
107+
* language code of the original text to translate. For example, `en` for English,
108+
* `zh` for Chinese, `fr` for French, `es` for Spanish. If the language has a
109+
* variant, the code appends the two-digit
110+
* [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
111+
* For example, Mexican Spanish is `es-MX`. See the
112+
* [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
113+
*/
114+
source_language_code: string;
115+
116+
/**
117+
* The [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes)
118+
* language code of the target language for the translation. For example, `en` for
119+
* English, `zh` for Chinese, `fr` for French, `es` for Spanish. If the language
120+
* has a variant, the code appends the two-digit
121+
* [ISO-3166 country code](https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes).
122+
* For example, Mexican Spanish is `es-MX`. See the
123+
* [list of supported languages and language codes](https://dev.writer.com/api-guides/api-reference/translation-api/language-support).
124+
*/
125+
target_language_code: string;
126+
127+
/**
128+
* The text to translate. Maximum of 100,000 words.
129+
*/
130+
text: string;
131+
}
132+
133+
export declare namespace Translation {
134+
export {
135+
type TranslationRequest as TranslationRequest,
136+
type TranslationResponse as TranslationResponse,
137+
type TranslationTranslateParams as TranslationTranslateParams,
138+
};
139+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import Writer from 'writer-sdk';
4+
5+
const client = new Writer({
6+
apiKey: 'My API Key',
7+
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
8+
});
9+
10+
describe('resource translation', () => {
11+
test('translate: only required params', async () => {
12+
const responsePromise = client.translation.translate({
13+
formality: true,
14+
length_control: true,
15+
mask_profanity: true,
16+
model: 'palmyra-translate',
17+
source_language_code: 'en',
18+
target_language_code: 'es',
19+
text: 'Hello, world!',
20+
});
21+
const rawResponse = await responsePromise.asResponse();
22+
expect(rawResponse).toBeInstanceOf(Response);
23+
const response = await responsePromise;
24+
expect(response).not.toBeInstanceOf(Response);
25+
const dataAndResponse = await responsePromise.withResponse();
26+
expect(dataAndResponse.data).toBe(response);
27+
expect(dataAndResponse.response).toBe(rawResponse);
28+
});
29+
30+
test('translate: required and optional params', async () => {
31+
const response = await client.translation.translate({
32+
formality: true,
33+
length_control: true,
34+
mask_profanity: true,
35+
model: 'palmyra-translate',
36+
source_language_code: 'en',
37+
target_language_code: 'es',
38+
text: 'Hello, world!',
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)