Skip to content

Commit 1ab9ca7

Browse files
committed
update w/andrei comments
1 parent 802e9f7 commit 1ab9ca7

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

.cursor/rules/adding-a-new-ai-integration.mdc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ gen_ai.invoke_agent (ai.generateText)
4343

4444
**References:**
4545

46-
- Vercel AI: `packages/core/src/tracing/vercel-ai/constants.ts:8-23`
47-
- LangChain: `packages/core/src/tracing/langchain/index.ts:199-207`
46+
- Vercel AI: `packages/core/src/tracing/vercel-ai/constants.ts`
47+
- LangChain: `onChainStart` callback in `packages/core/src/tracing/langchain/index.ts`
4848

4949
---
5050

5151
## Streaming vs Non-Streaming
5252

5353
**Non-streaming:** Use `startSpan()`, set attributes immediately from response
5454

55-
**Streaming:** Use `startSpanManual()` with this pattern:
55+
**Streaming:** Use `startSpanManual()` and prefer event listeners/hooks when available (like Anthropic's `stream.on()`). If not available, use async generator pattern:
5656

5757
```typescript
5858
interface StreamingState {
@@ -83,11 +83,13 @@ async function* instrumentStream(stream, span, recordOutputs) {
8383
- Set `GEN_AI_RESPONSE_STREAMING_ATTRIBUTE: true`
8484
- Call `span.end()` in finally block
8585

86+
**Detection:** Check request parameters for `stream: true` to determine if response will be streamed.
87+
8688
**References:**
8789

88-
- OpenAI: `packages/core/src/tracing/openai/streaming.ts`
89-
- Anthropic: `packages/core/src/tracing/anthropic-ai/streaming.ts`
90-
- Detection: `packages/core/src/tracing/openai/index.ts:183-221`
90+
- OpenAI async generator: `instrumentStream` in `packages/core/src/tracing/openai/streaming.ts`
91+
- Anthropic event listeners: `instrumentMessageStream` in `packages/core/src/tracing/anthropic-ai/streaming.ts`
92+
- Detection logic: Check `params.stream === true` in `packages/core/src/tracing/openai/index.ts`
9193

9294
---
9395

@@ -117,7 +119,7 @@ for (const span of event.spans) {
117119
}
118120
```
119121

120-
**Reference:** `packages/core/src/tracing/vercel-ai/index.ts:110-140`
122+
**Reference:** `vercelAiEventProcessor` and `accumulateTokensForParent` in `packages/core/src/tracing/vercel-ai/`
121123

122124
---
123125

@@ -158,7 +160,7 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
158160

159161
2. **Node.js:** Add performance optimization in `packages/node/src/integrations/tracing/{provider}/index.ts`
160162
- Use `callWhenPatched()` to defer processor registration
161-
- Only register when package is actually imported (see vercelai:36)
163+
- Only register when package is actually imported (see `vercelAIIntegration` function)
162164

163165
3. **Edge:** Direct registration in `packages/cloudflare/src/integrations/tracing/{provider}.ts`
164166
- No OTel patching available
@@ -181,11 +183,11 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
181183
2. **Node.js Instrumentation:** Patch module exports in `instrumentation.ts`
182184
- Wrap client constructor
183185
- Check `_INTERNAL_shouldSkipAiProviderWrapping()` (for LangChain)
184-
- See openai/instrumentation.ts:70-86
186+
- See `instrumentOpenAi` in `packages/node/src/integrations/tracing/openai/instrumentation.ts`
185187

186188
3. **Node.js Integration:** Export instrumentation function
187189
- Use `generateInstrumentOnce()` helper
188-
- See openai/index.ts:6-9
190+
- See `openAIIntegration` in `packages/node/src/integrations/tracing/openai/index.ts`
189191

190192
**Reference:** `packages/node/src/integrations/tracing/openai/`
191193

@@ -203,7 +205,7 @@ OpenTelemetry Semantic Convention attribute names. **Always use these constants!
203205

204206
2. **Node.js Instrumentation:** Auto-inject callbacks
205207
- Patch runnable methods to add handler automatically
206-
- **Important:** Disable underlying AI provider wrapping (langchain/instrumentation.ts:103-105)
208+
- **Important:** Disable underlying AI provider wrapping (see `instrumentLangchain` in `packages/node/src/integrations/tracing/langchain/instrumentation.ts`)
207209

208210
**Reference:** `packages/node/src/integrations/tracing/langchain/`
209211

0 commit comments

Comments
 (0)