Skip to content

Commit cd70571

Browse files
committed
refactor: address review comments for local protocol
1 parent a488c81 commit cd70571

4 files changed

Lines changed: 16 additions & 17 deletions

File tree

packages/core/src/agent/content-utils.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,14 @@ describe('contentPartsToGeminiParts', () => {
187187
]);
188188
});
189189

190-
it('serializes unknown ContentPart variants', () => {
190+
it('throws on unknown ContentPart variants', () => {
191191
// Force an unknown variant past the type system
192192
const content = [
193193
{ type: 'custom_widget', payload: 123 },
194194
] as unknown as ContentPart[];
195-
const result = contentPartsToGeminiParts(content);
196-
expect(result).toHaveLength(1);
197-
expect(result[0]).toEqual({
198-
text: JSON.stringify({ type: 'custom_widget', payload: 123 }),
199-
});
195+
expect(() => contentPartsToGeminiParts(content)).toThrow(
196+
'Unhandled ContentPart type: {"type":"custom_widget","payload":123}',
197+
);
200198
});
201199
});
202200

packages/core/src/agent/content-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ export function contentPartsToGeminiParts(content: ContentPart[]): Part[] {
9292
// References are converted to text for the model
9393
result.push({ text: part.text });
9494
break;
95-
default:
96-
// Serialize unknown ContentPart variants instead of dropping them
97-
result.push({ text: JSON.stringify(part) });
95+
default: {
96+
((x: never) => {
97+
throw new Error(`Unhandled ContentPart type: ${JSON.stringify(x)}`);
98+
})(part);
9899
break;
100+
}
99101
}
100102
}
101103
return result;

packages/core/src/agent/legacy-agent-session.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,10 @@ describe('LegacyAgentSession', () => {
239239
);
240240
});
241241

242-
it('throws for non-message payloads', async () => {
242+
it('returns null streamId for non-message payloads', async () => {
243243
const session = new LegacyAgentSession(deps);
244-
await expect(session.send({ update: { title: 'test' } })).rejects.toThrow(
245-
'only supports message sends',
246-
);
244+
const result = await session.send({ update: { title: 'test' } });
245+
expect(result.streamId).toBeNull();
247246
});
248247

249248
it('throws if send is called while a stream is active', async () => {

packages/core/src/agent/legacy-agent-session.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,10 @@ export class LegacyAgentProtocol implements AgentProtocol {
105105
};
106106
}
107107

108-
async send(payload: AgentSend): Promise<{ streamId: string }> {
108+
async send(payload: AgentSend): Promise<{ streamId: string | null }> {
109109
const message = 'message' in payload ? payload.message : undefined;
110110
if (!message) {
111-
throw new Error(
112-
'LegacyAgentSession.send() only supports message sends for the moment.',
113-
);
111+
return { streamId: null };
114112
}
115113

116114
if (this._activeStreamId) {
@@ -166,6 +164,7 @@ export class LegacyAgentProtocol implements AgentProtocol {
166164
} else {
167165
this._emitErrorAndAgentEnd(err);
168166
}
167+
} finally {
169168
this._clearActiveStream();
170169
}
171170
}
@@ -387,6 +386,7 @@ export class LegacyAgentProtocol implements AgentProtocol {
387386
const meta: Record<string, unknown> = {};
388387
if (err instanceof Error) {
389388
meta['errorName'] = err.constructor.name;
389+
meta['stack'] = err.stack;
390390
if ('exitCode' in err && typeof err.exitCode === 'number') {
391391
meta['exitCode'] = err.exitCode;
392392
}

0 commit comments

Comments
 (0)