Skip to content

Commit a33c6e1

Browse files
Avoid incorrect use of console.log as that would break on the stdio transport
1 parent ee39340 commit a33c6e1

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

nodejs/docs/agent-author.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ tools: [
107107
- Tool names must be unique across ALL loaded extensions. Collisions cause the second extension to fail to load.
108108
- Handler must return a string or `{ textResultForLlm: string, resultType?: string }`.
109109
- Handler receives `(args, invocation)` — the second argument has `sessionId`, `toolCallId`, `toolName`.
110-
- Use `console.error()` for debug logging (stdout is reserved for JSON-RPC).
110+
- Use `session.log()` to surface messages to the user. Don't use `console.log()` (stdout is reserved for JSON-RPC).
111111

112112
---
113113

@@ -212,7 +212,7 @@ await session.send({
212212
Send and block until the agent finishes (resolves on `session.idle`):
213213
```js
214214
const response = await session.sendAndWait({ prompt: "What is 2+2?" });
215-
console.error(response?.data.content);
215+
// response?.data.content contains the agent's reply
216216
```
217217

218218
### session.log(message, options?)
@@ -230,7 +230,7 @@ await session.log("Processing...", { ephemeral: true }); // transient, not persi
230230
Subscribe to session events. Returns an unsubscribe function.
231231
```js
232232
const unsub = session.on("tool.execution_complete", (event) => {
233-
console.error(`Tool ${event.data.toolName}: ${event.data.success}`);
233+
// event.data.toolName, event.data.success, event.data.result
234234
});
235235
```
236236

@@ -259,7 +259,7 @@ Low-level typed RPC access to all session APIs (model, mode, plan, workspace, et
259259

260260
## Gotchas
261261

262-
1. **stdout is reserved for JSON-RPC.** Use `console.error()` for debug output. `console.log()` will corrupt the protocol.
262+
1. **stdout is reserved for JSON-RPC.** Don't use `console.log()` — it will corrupt the protocol. Use `session.log()` to surface messages to the user.
263263
2. **Tool name collisions are fatal.** If two extensions register the same tool name, the second extension fails to initialize.
264264
3. **`disableResume: true` is required.** Extensions always attach to existing sessions.
265265
4. **Don't call `session.send()` synchronously from `onUserPromptSubmitted`.** Use `setTimeout(() => session.send(...), 0)` to avoid infinite loops.

nodejs/docs/examples.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,11 @@ hooks: {
324324
```js
325325
hooks: {
326326
onSessionStart: async (input) => {
327-
console.log(`Session started (source: ${input.source})`);
327+
// input.source is "startup", "resume", or "new"
328328
return { additionalContext: "Remember to write tests for all changes." };
329329
},
330330
onSessionEnd: async (input) => {
331-
console.log(`Session ended (reason: ${input.reason})`);
331+
// input.reason is "complete", "error", "abort", "timeout", or "user_exit"
332332
},
333333
}
334334
```
@@ -343,15 +343,15 @@ After calling `resumeSession`, use `session.on()` to react to events in real tim
343343
344344
```js
345345
session.on("assistant.message", (event) => {
346-
console.log("Agent said:", event.data.content);
346+
// event.data.content has the agent's response text
347347
});
348348
```
349349
350350
### Listening to all events
351351
352352
```js
353353
session.on((event) => {
354-
console.log(`[${event.type}]`, event.data);
354+
// event.type and event.data are available for all events
355355
});
356356
```
357357
@@ -361,7 +361,7 @@ session.on((event) => {
361361
362362
```js
363363
const unsubscribe = session.on("tool.execution_complete", (event) => {
364-
console.log(`Tool ${event.data.toolName} finished`);
364+
// event.data.toolName, event.data.success, event.data.result, event.data.error
365365
});
366366
367367
// Later, stop listening
@@ -546,7 +546,7 @@ await session.send({ prompt: "Analyze the test results." });
546546
547547
```js
548548
const response = await session.sendAndWait({ prompt: "What is 2 + 2?" });
549-
console.log(response?.data.content); // "4"
549+
// response?.data.content contains the agent's reply
550550
```
551551
552552
### Send with file attachments
@@ -570,7 +570,7 @@ await session.send({
570570
const session = await extension.resumeSession(process.env.SESSION_ID, {
571571
onPermissionRequest: async (request) => {
572572
if (request.kind === "shell") {
573-
console.log(`Shell command: ${request.fullCommandText}`);
573+
// request.fullCommandText has the shell command
574574
return { kind: "approved" };
575575
}
576576
if (request.kind === "write") {
@@ -589,11 +589,8 @@ Register `onUserInputRequest` to enable the agent's `ask_user` tool:
589589
const session = await extension.resumeSession(process.env.SESSION_ID, {
590590
onPermissionRequest: approveAll,
591591
onUserInputRequest: async (request) => {
592-
console.log(`Agent asks: ${request.question}`);
593-
if (request.choices) {
594-
console.log(`Options: ${request.choices.join(", ")}`);
595-
}
596-
// Return the user's answer
592+
// request.question has the agent's question
593+
// request.choices has the options (if multiple choice)
597594
return { answer: "yes", wasFreeform: false };
598595
},
599596
});
@@ -684,8 +681,7 @@ session.on("assistant.message", (event) => {
684681
});
685682
686683
session.on("tool.execution_complete", (event) => {
687-
const status = event.data.success ? "✓" : "✗";
688-
console.log(`[${status}] ${event.data.toolName}`);
684+
// event.data.success, event.data.toolName, event.data.result
689685
});
690686
```
691687

0 commit comments

Comments
 (0)