Skip to content

Commit bfebe1b

Browse files
authored
docs: fix the Node.js inbound trace-context example to use a real tool-registration API (#2223)
The Node.js example in the "CLI -> SDK (inbound)" section of docs/observability/opentelemetry.md registered a tool by calling session.registerTool(myTool, handler). That method is not part of the public CopilotSession API, so copying the example fails to compile with error TS2339: Property 'registerTool' does not exist on type 'CopilotSession' and, if the types are bypassed, throws "TypeError: session.registerTool is not a function" at runtime. The plural registerTools() is marked @internal and stripped from the shipped declarations, so there is no public session method to register a tool with after the session exists. Move the trace-restoring handler into defineTool() and register the tool through client.createSession({ tools: [myTool] }), which is the public registration path and the one used by the rest of the documentation. The trace-context logic is unchanged; only the registration mechanism is corrected. As a side effect the handler's args and invocation parameters are now contextually typed, so the invocation.traceparent / invocation.tracestate access in the example is actually type-checked. Co-authored-by: examon <examon@users.noreply.github.com>
1 parent dd8d869 commit bfebe1b

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

docs/observability/opentelemetry.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,36 @@ When the CLI invokes a tool handler, the `traceparent` and `tracestate` from the
152152

153153
<!-- docs-validate: skip -->
154154
```typescript
155+
import { defineTool } from "@github/copilot-sdk";
155156
import { propagation, context, trace } from "@opentelemetry/api";
156157

157-
session.registerTool(myTool, async (args, invocation) => {
158-
// Restore the CLI's trace context as the active context
159-
const carrier = {
160-
traceparent: invocation.traceparent,
161-
tracestate: invocation.tracestate,
162-
};
163-
const parentCtx = propagation.extract(context.active(), carrier);
164-
165-
// Create a child span under the CLI's span
166-
const tracer = trace.getTracer("my-app");
167-
return context.with(parentCtx, () =>
168-
tracer.startActiveSpan("my-tool", async (span) => {
169-
try {
170-
const result = await doWork(args);
171-
return result;
172-
} finally {
173-
span.end();
174-
}
175-
})
176-
);
158+
const myTool = defineTool("my-tool", {
159+
description: "Do work",
160+
handler: async (args, invocation) => {
161+
// Restore the CLI's trace context as the active context
162+
const carrier = {
163+
traceparent: invocation.traceparent,
164+
tracestate: invocation.tracestate,
165+
};
166+
const parentCtx = propagation.extract(context.active(), carrier);
167+
168+
// Create a child span under the CLI's span
169+
const tracer = trace.getTracer("my-app");
170+
return context.with(parentCtx, () =>
171+
tracer.startActiveSpan("my-tool", async (span) => {
172+
try {
173+
const result = await doWork(args);
174+
return result;
175+
} finally {
176+
span.end();
177+
}
178+
})
179+
);
180+
},
177181
});
182+
183+
// Tool handlers are registered when the session is created.
184+
const session = await client.createSession({ tools: [myTool] });
178185
```
179186

180187
### Per-language dependencies

0 commit comments

Comments
 (0)