(handler: (state: AgentState) => AgentState)
| 645 | } |
| 646 | |
| 647 | updateAgentState(handler: (state: AgentState) => AgentState): void { |
| 648 | this.agentStateLock.inLock(async () => { |
| 649 | await backoff(async () => { |
| 650 | const current = this.agentState ?? ({} as AgentState) |
| 651 | const updated = handler(current) |
| 652 | |
| 653 | const answer = await this.socket.emitWithAck('update-state', { |
| 654 | sid: this.sessionId, |
| 655 | expectedVersion: this.agentStateVersion, |
| 656 | agentState: updated |
| 657 | }) as unknown |
| 658 | |
| 659 | applyVersionedAck(answer, { |
| 660 | valueKey: 'agentState', |
| 661 | parseValue: (value) => { |
| 662 | const parsed = AgentStateSchema.safeParse(value) |
| 663 | return parsed.success ? parsed.data : null |
| 664 | }, |
| 665 | applyValue: (value) => { |
| 666 | this.agentState = value |
| 667 | }, |
| 668 | applyVersion: (version) => { |
| 669 | this.agentStateVersion = version |
| 670 | }, |
| 671 | logInvalidValue: (context, version) => { |
| 672 | const suffix = context === 'success' ? 'ack' : 'version-mismatch ack' |
| 673 | logger.debug(`[API] Ignoring invalid agentState value from ${suffix}`, { version }) |
| 674 | }, |
| 675 | invalidResponseMessage: 'Invalid update-state response', |
| 676 | errorMessage: 'Agent state update failed', |
| 677 | versionMismatchMessage: 'Agent state version mismatch' |
| 678 | }) |
| 679 | }) |
| 680 | }) |
| 681 | } |
| 682 | |
| 683 | private async waitForConnected(timeoutMs: number): Promise<boolean> { |
| 684 | if (this.socket.connected) { |
no test coverage detected