Skip to content

Commit

Permalink
Update cli entrypoint to avoid potential crash
Browse files Browse the repository at this point in the history
  • Loading branch information
icodesign committed Jan 2, 2025
1 parent efb4fa7 commit 7960f18
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-dots-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rootapp/dolphin': minor
---

Update cli entrypoint to avoid potential crash
2 changes: 1 addition & 1 deletion apps/cli/bin/cli.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

var cli = require('../dist/index.cjs');
var { cli } = require('../dist/index.cjs');
cli();
12 changes: 12 additions & 0 deletions apps/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import localizeCommand from './localize.js';

// import translateCommand from './translate.js';

process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at:', (reason as any).stack || reason);
// Recommended: send the information to sentry.io
// or whatever crash reporting service you use
});

process.on('uncaughtException', (error) => {
console.log('Uncaught Exception at:', error.stack || error);
// Recommended: send the information to sentry.io
// or whatever crash reporting service you use
});

export const commands: CommandModule<{}, any>[] = [
// exportCommand,
// importCommand,
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ const parser = yargs(hideBin(process.argv))
.help()
.alias('help', 'h');

(async () => {
export const cli = async () => {
await parser.argv;
})();
};
2 changes: 1 addition & 1 deletion packages/provider/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Translate from en-US to zh-CN, ja:
);
logger.info(`Translating with instructions: ${instructions}`);
logger.info(`Translating with user content: ${userContent}`);
const result = await streamObject({
const result = streamObject({
model: this.model,
mode: 'json',
schema: TranslationReponseSchema,
Expand Down
55 changes: 36 additions & 19 deletions packages/translate/src/translator/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,46 @@ export class OpenAITranslator implements Translator {
', ',
)}, keys: ${batch.contents.map((c) => c.key).join(', ')}`,
);
const stream = await this.provider.translate({
context: config.globalContext,
sourceLanguage: batch.sourceLanguage,
targetLanguages: batch.targetLanguages,
contents: batch.contents,
});
try {
const stream = await this.provider.translate({
context: config.globalContext,
sourceLanguage: batch.sourceLanguage,
targetLanguages: batch.targetLanguages,
contents: batch.contents,
});

for await (const s of stream.fullStream) {
// Empty loop to consume the stream so we can get the final object
}
logger.info(`Start openai translating stream`);
try {
for await (const s of stream.partialObjectStream) {
// Log partial results and handle them appropriately
logger.debug(`Received partial translation: ${JSON.stringify(s)}`);
}
} catch (streamError) {
logger.error(`Error during stream processing: ${streamError}`);
throw streamError;
}

const translationResponse = await stream.object;
const usage = await stream.usage;
logger.info(`Streaming finished`);
const translationResponse = await stream.object;
logger.info(
`Translation response: ${JSON.stringify(translationResponse)}`,
);
const usage = await stream.usage;
logger.info(`Usage: ${JSON.stringify(usage)}`);

// Update usage
this.usage.promptTokens += usage.promptTokens;
this.usage.completionTokens += usage.completionTokens;
this.usage.totalTokens += usage.totalTokens;
// Update usage
this.usage.promptTokens += usage.promptTokens;
this.usage.completionTokens += usage.completionTokens;
this.usage.totalTokens += usage.totalTokens;

if (Object.keys(translationResponse).length === 0) {
throw new Error('Failed to receive translation response object');
}
if (Object.keys(translationResponse).length === 0) {
throw new Error('Failed to receive translation response object');
}

return translationResponse;
return translationResponse;
} catch (error) {
logger.error(`Error translating openai batch: ${error}`);
throw error;
}
}
}

0 comments on commit 7960f18

Please sign in to comment.