(context: ContextState)
| 550 | } |
| 551 | |
| 552 | private async createChatObject(context: ContextState) { |
| 553 | if (!this.promptConfig.systemPrompt && !this.promptConfig.initialMessages) { |
| 554 | throw new Error( |
| 555 | 'PromptConfig must have either `systemPrompt` or `initialMessages` defined.', |
| 556 | ); |
| 557 | } |
| 558 | if (this.promptConfig.systemPrompt && this.promptConfig.initialMessages) { |
| 559 | throw new Error( |
| 560 | 'PromptConfig cannot have both `systemPrompt` and `initialMessages` defined.', |
| 561 | ); |
| 562 | } |
| 563 | |
| 564 | const envParts = await getEnvironmentContext(this.runtimeContext); |
| 565 | const envHistory: Content[] = [ |
| 566 | { role: 'user', parts: envParts }, |
| 567 | { role: 'model', parts: [{ text: 'Got it. Thanks for the context!' }] }, |
| 568 | ]; |
| 569 | |
| 570 | const start_history = [ |
| 571 | ...envHistory, |
| 572 | ...(this.promptConfig.initialMessages ?? []), |
| 573 | ]; |
| 574 | |
| 575 | const systemInstruction = this.promptConfig.systemPrompt |
| 576 | ? this.buildChatSystemPrompt(context) |
| 577 | : undefined; |
| 578 | |
| 579 | try { |
| 580 | const generationConfig: GenerateContentConfig & { |
| 581 | systemInstruction?: string | Content; |
| 582 | } = { |
| 583 | temperature: this.modelConfig.temp, |
| 584 | topP: this.modelConfig.top_p, |
| 585 | }; |
| 586 | |
| 587 | if (systemInstruction) { |
| 588 | generationConfig.systemInstruction = systemInstruction; |
| 589 | } |
| 590 | |
| 591 | const contentGenerator = await createContentGenerator( |
| 592 | this.runtimeContext.getContentGeneratorConfig(), |
| 593 | this.runtimeContext, |
| 594 | this.runtimeContext.getSessionId(), |
| 595 | ); |
| 596 | |
| 597 | this.runtimeContext.setModel(this.modelConfig.model); |
| 598 | |
| 599 | return new AnusChat( |
| 600 | this.runtimeContext, |
| 601 | contentGenerator, |
| 602 | generationConfig, |
| 603 | start_history, |
| 604 | ); |
| 605 | } catch (error) { |
| 606 | await reportError( |
| 607 | error, |
| 608 | 'Error initializing Anus chat session.', |
| 609 | start_history, |
no test coverage detected