refactor: address review comments for local protocol

This commit is contained in:
Adam Weidman
2026-05-07 14:09:21 -04:00
parent 405b02e4b0
commit dc3c7994d1
4 changed files with 16 additions and 26 deletions
+4 -12
View File
@@ -187,22 +187,14 @@ describe('contentPartsToGeminiParts', () => {
]);
});
it('serializes unknown ContentPart variants', () => {
it('throws on unknown ContentPart variants', () => {
// Force an unknown variant past the type system
const content = [
{ type: 'custom_widget', payload: 123 },
] as unknown as ContentPart[];
const warnSpy = vi.spyOn(debugLogger, 'warn');
const result = contentPartsToGeminiParts(content);
expect(warnSpy).toHaveBeenCalled();
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
text: JSON.stringify({ type: 'custom_widget', payload: 123 }),
});
warnSpy.mockRestore();
expect(() => contentPartsToGeminiParts(content)).toThrow(
'Unhandled ContentPart type: {"type":"custom_widget","payload":123}',
);
});
});
+7 -6
View File
@@ -93,13 +93,14 @@ export function contentPartsToGeminiParts(content: ContentPart[]): Part[] {
// References are converted to text for the model
result.push({ text: part.text });
break;
default:
debugLogger.warn(
`Unhandled ContentPart type: ${JSON.stringify(part)} fallback to serialization`,
);
// Serialize unknown ContentPart variants instead of dropping them
result.push({ text: JSON.stringify(part) });
default: {
((x: never) => {
throw new Error(`Unhandled ContentPart type: ${JSON.stringify(x)}`);
})(part);
break;
}
break;
}
}
}
return result;
@@ -241,11 +241,10 @@ describe('LegacyAgentSession', () => {
);
});
it('throws for non-message payloads', async () => {
it('returns null streamId for non-message payloads', async () => {
const session = new LegacyAgentSession(deps);
await expect(session.send({ update: { title: 'test' } })).rejects.toThrow(
'only supports message sends',
);
const result = await session.send({ update: { title: 'test' } });
expect(result.streamId).toBeNull();
});
it('throws if send is called while a stream is active', async () => {
@@ -105,12 +105,10 @@ export class LegacyAgentProtocol implements AgentProtocol {
};
}
async send(payload: AgentSend): Promise<{ streamId: string }> {
async send(payload: AgentSend): Promise<{ streamId: string | null }> {
const message = 'message' in payload ? payload.message : undefined;
if (!message) {
throw new Error(
'LegacyAgentSession.send() only supports message sends for the moment.',
);
return { streamId: null };
}
if (this._activeStreamId) {