Skip to content

Commit 24a2929

Browse files
committed
feat(genkit): add @traceai/genkit
New TypeScript package typescript/packages/traceai_genkit (@traceai/genkit, JS only). FIGenkitSpanProcessor is passed to Genkit's enableTelemetry({ spanProcessors }). Genkit's own telemetry-server processor stays first (@genkit-ai/core src/tracing/node-telemetry-provider.ts:66-77), so the Dev UI keeps working. For each ended Genkit span the processor builds a mapped copy (Genkit's span is never modified) and hands it to the @traceai/fi-core provider's span processor and OTLP exporter with the Future AGI resource. Also: flushOnSignals(flush) for SIGTERM/SIGINT, README, root README row, jest unit tests, shared-harness contract test, lockfile entries. Tested: genkit 1.42.0 (@genkit-ai/core 1.42.0, @genkit-ai/ai 1.42.0, all Apache-2.0 per node_modules LICENSE/package.json), Node 20.20.2, 22.23.3, 26.8.1. genkit is a peerDependency (^1.42.0) and a 1.42.0 devDependency only. Inventory (contract/inventory.mjs, Genkit's mockModel from genkit/testing, one flow with an ai.run() step and generateStream with a tool loop, called and streamed: 14 spans, scope genkit-tracer, sdk-trace-base 1.25 shape): - action/flow: genkit:type, genkit:metadata:subtype, genkit:key, genkit:name, genkit:isRoot (bool), genkit:path, genkit:input, genkit:output, genkit:state, genkit:metadata:context - flowStep: genkit:type, genkit:name, genkit:path, genkit:output, genkit:state - util (generate): genkit:type, genkit:name, genkit:path, genkit:input, genkit:output, genkit:state - action/model and action/tool: genkit:type, genkit:metadata:subtype, genkit:key, genkit:name, genkit:path, genkit:input, genkit:output, genkit:state - model genkit:output JSON: message, finishReason, usage {inputTokens, outputTokens, totalTokens}, latencyMs - beta agent run adds promptTemplate (render, genkit:metadata:promptName), flowStep runTurn-1 (genkit:metadata:agent:snapshotId) and action/agent (genkit:metadata:agent:sessionId, genkit:init); errors add genkit:isFailureSource. Kind map from genkit:type labels (core action.ts:538-543, flow.ts:219-221, ai generate/action.ts:147-149, prompt.ts:273-275,490-492) and the ActionType list (core registry.ts:40-62): model/background-model LLM, tool/tool.v2 TOOL, retriever RETRIEVER, embedder EMBEDDING, reranker RERANKER, evaluator EVALUATOR, agent AGENT, flow and other actions CHAIN, flowStep/util/ promptTemplate/dotprompt CHAIN, unknown type: no kind, span still exported. Usage: only inputTokens/outputTokens/totalTokens (seen on the dumped model spans) go to gen_ai.usage.input_tokens/output_tokens/total_tokens, on model spans only. thoughtsTokens, cachedContentTokens and the character/media counters exist in GenerationUsageSchema (ai model-types.ts:260-275) but no inventoried span carried them: unavailable, not mapped. Promoted token/cost keys on a non-model span move to genkit.usage.<key>; the contract test checks the trace sum equals the two model calls (32/7/39). Model: gen_ai.request.model = genkit:name of the model action span (not parsed); provider and cost unavailable. Deviations from the TH-8240 architecture/docs, with evidence: 1. enableTelemetry and flushTracing are exported from genkit/tracing (genkit/src/tracing.ts:43-44), not genkit. genkit/tracing does not initialise the telemetry provider; importing genkit does (genkit/src/common.ts:173-175 -> @genkit-ai/core src/node.ts:20-23). enableTelemetry before any genkit import throws FAILED_PRECONDITION: TelemetryProvider is not initialized (observed). README says import genkit first. 2. @genkit-ai/core 1.42.0 telemetryTypes.ts is 24 lines and has only TelemetryConfig = Partial<NodeSDKConfiguration> (line 24); there is no TelemetryOptions with forceDevExport / metricExportIntervalMillis / disableMetrics. Those are @genkit-ai/google-cloud options (src/types.ts: 92,110). The example does not pass disableMetrics (types.test.ts asserts it is a type error). Metrics are not exported anyway: enableTelemetry adds no metricReader and @opentelemetry/sdk-node 0.52.1 builds a meter provider only with a reader or views (build/src/sdk.js:97-105,158). Dev UI export is not controlled by forceDevExport: Genkit always prepends its telemetry-server processor. 3. register() must be called with setGlobalTracerProvider: false (the docs example omits it). fi-core defaults it to true (otel.ts:486,604-605); Genkit takes its tracer from the global API (instrumentation.ts:96,197), so with fi-core global the NodeSDK cannot register and Genkit spans reach Future AGI raw (content, no kind). Contract control test covers this; the processor emits warning TRACEAI_GENKIT_GLOBAL_PROVIDER. 4. Session: a native key exists for beta agents only, genkit:metadata:agent:sessionId (ai/src/agent.ts:1061-1063), mapped to session.id on the agent span. Flows have none; the app sets it with fi-core setSession() around the call (read in onStart from the parent context). No session id is invented. 5. Tool name is genkit:name of the tool action span (spec: unverified). 6. AC-06 flush: flushTracing is confirmed (core src/tracing.ts:113). genkit registers SIGTERM/SIGINT listeners at import that call process.exit(0) without flushing (genkit/src/genkit.ts:786-793), so an app listener that awaits flushTracing() loses the race (contract control test). Added flushOnSignals(flushTracing), which runs the flush before the earlier listeners. 7. Genkit's NodeSDK uses sdk-trace-base 1.25 spans (parentSpanId, instrumentationLibrary); fi-core exports with 2.x (parentSpanContext, instrumentationScope). The processor converts the copy and reads the fi-core provider's _activeSpanProcessor/_resource (no public accessor). 8. Kinds beyond the spec's list (reranker, evaluator, agent, background-model, tool.v2, non-action types) come from the inventory. 9. Lockfile: @genkit-ai/core lists @genkit-ai/firebase as an optionalDependency, so the dev-only tree adds firebase-admin and @genkit-ai/google-cloud. Neither is a dependency of @traceai/genkit. pnpm-lock.yaml keeps 3eaadc8 byte for byte plus the new importer and the 177 packages / 180 snapshots reachable from it that the base lacked (git diff --minimal: 2374 insertions, 0 deletions); pnpm install --frozen-lockfile (with and without --ignore-scripts) exits 0: "Lockfile is up to date, resolution step is skipped". Tests (this tree): jest 5 suites, 62 tests passed (Node 26.8.1, 20.20.2, 22.23.3). Contract suite 20 passed with NODE_BINARY = node 26.8.1, 20.20.2 and 22.23.3. pnpm pack: 63 files, dist/ + README.md + package.json only. approved: Nikhil 2026-10-03 blanket Refs: TH-8240
1 parent 3eaadc8 commit 24a2929

22 files changed

Lines changed: 7920 additions & 2717 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ var tracer = TraceAI.Register(opts =>
331331
| [`@traceai/mastra`](https://www.npmjs.com/package/@traceai/mastra) | Mastra | [![npm](https://img.shields.io/npm/v/@traceai/mastra)](https://www.npmjs.com/package/@traceai/mastra) |
332332
| [`@traceai/beeai`](https://www.npmjs.com/package/@traceai/beeai) | IBM BeeAI | [![npm](https://img.shields.io/npm/v/@traceai/beeai)](https://www.npmjs.com/package/@traceai/beeai) |
333333
| [`@traceai/strands`](https://www.npmjs.com/package/@traceai/strands) | AWS Strands Agents | [![npm](https://img.shields.io/npm/v/@traceai/strands)](https://www.npmjs.com/package/@traceai/strands) |
334+
| [`@traceai/genkit`](https://www.npmjs.com/package/@traceai/genkit) | Firebase Genkit (JavaScript, span processor) | [![npm](https://img.shields.io/npm/v/@traceai/genkit)](https://www.npmjs.com/package/@traceai/genkit) |
334335

335336
#### Tools and Libraries
336337

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# @traceai/genkit
2+
3+
Send [Firebase Genkit](https://genkit.dev) (JavaScript) spans to Future AGI. Genkit already creates OpenTelemetry spans for flows, models, tools and steps. `FIGenkitSpanProcessor` is a span processor you pass to Genkit's `enableTelemetry({ spanProcessors })`. It maps Genkit's `genkit:*` attributes to Future AGI span kinds, model and token keys, and exports the mapped copy through `@traceai/fi-core`.
4+
5+
Alpha. Validated with real `genkit` 1.42.0 flows on Genkit's own test model (`mockModel` from `genkit/testing`), exported to a loopback OTLP receiver. No model vendor and no GCP were called.
6+
7+
JavaScript only. Genkit Python and Genkit Go are not covered. This package does not use the Firebase / Google Cloud plugin and does not wrap `ai.generate()`.
8+
9+
## Versions and licence
10+
11+
| Item | Value |
12+
|---|---|
13+
| Tested pin | `genkit` 1.42.0 (`@genkit-ai/core` 1.42.0, `@genkit-ai/ai` 1.42.0) |
14+
| Peer range | `genkit` `^1.42.0` |
15+
| Node | 20.20.2, 22.23.3 and 26.8.1 (contract suite and CJS/ESM import check) |
16+
| Genkit licence | Apache-2.0 (`node_modules/genkit/LICENSE`, `package.json` `license`) |
17+
18+
`genkit` is a **peer dependency**. Install it yourself. This package never bundles or re-exports Genkit; the pack test hashes every file of the installed `genkit`, `@genkit-ai/core` and `@genkit-ai/ai` and checks that none ships in the tarball.
19+
20+
## Install
21+
22+
```bash
23+
npm install @traceai/genkit @traceai/fi-core genkit
24+
```
25+
26+
```bash
27+
export FI_API_KEY="YOUR_API_KEY"
28+
export FI_SECRET_KEY="YOUR_SECRET_KEY"
29+
export FI_PROJECT_NAME="my-genkit-app"
30+
```
31+
32+
fi-core sends the keys as the `X-Api-Key` / `X-Secret-Key` headers. They are never copied onto spans.
33+
34+
## Set up
35+
36+
```typescript
37+
import { genkit } from "genkit"; // import genkit first: it initialises Genkit's telemetry provider
38+
import { enableTelemetry, flushTracing } from "genkit/tracing";
39+
import { register, ProjectType } from "@traceai/fi-core";
40+
import { FIGenkitSpanProcessor, flushOnSignals } from "@traceai/genkit";
41+
42+
const tracerProvider = register({
43+
projectType: ProjectType.OBSERVE,
44+
projectName: "my-genkit-app", // or FI_PROJECT_NAME; the collector drops a batch with no project_name
45+
setGlobalTracerProvider: false, // required: Genkit's NodeSDK must own the global provider
46+
});
47+
48+
await enableTelemetry({
49+
spanProcessors: [new FIGenkitSpanProcessor({ tracerProvider })], // captureContent defaults to false
50+
});
51+
52+
const ai = genkit({ plugins: [/* your model plugins */] });
53+
54+
// Long-running server: flush on SIGTERM / SIGINT before Genkit's own exit handler runs.
55+
flushOnSignals(flushTracing);
56+
```
57+
58+
Three things the setup depends on, each checked in the installed 1.42.0 source:
59+
60+
- `enableTelemetry` and `flushTracing` are exported from `genkit/tracing` (`genkit/src/tracing.ts:43-44`), not from `genkit`. They need Genkit's telemetry provider, which `genkit` / `genkit/beta` initialise at import (`genkit/src/common.ts:173-175`). Calling `enableTelemetry` before anything imported `genkit` throws `FAILED_PRECONDITION: TelemetryProvider is not initialized`.
61+
- `register()` must get `setGlobalTracerProvider: false`. Genkit takes its tracer from the global OpenTelemetry API (`@genkit-ai/core` `src/tracing/instrumentation.ts:96,197`) and its NodeSDK cannot register a global provider once fi-core has. Genkit spans would then skip both this processor and the Dev UI exporter, and reach Future AGI raw (content included, no kind). The processor warns (`TRACEAI_GENKIT_GLOBAL_PROVIDER`) when it detects this.
62+
- `enableTelemetry` keeps Genkit's own telemetry-server processor first and appends yours (`@genkit-ai/core` `src/tracing/node-telemetry-provider.ts:66-77`). The Genkit Dev UI keeps working. Passing `traceExporter` instead of `spanProcessors` throws in Genkit.
63+
64+
Metrics: `enableTelemetry({ spanProcessors })` passes no metric reader, and the NodeSDK Genkit uses (`@opentelemetry/sdk-node` 0.52.1) builds a meter provider only when one is configured, so no Genkit metrics go anywhere. `disableMetrics` and `forceDevExport` are options of the `@genkit-ai/google-cloud` plugin (`src/types.ts:92,110`), not of `enableTelemetry`'s `TelemetryConfig`; TypeScript rejects them there.
65+
66+
## What you get
67+
68+
### Span kinds
69+
70+
`fi.span.kind` and `gen_ai.span.kind` are both set. The map is written from the type strings in the 1.42.0 source (`genkit:type` labels in `core/src/action.ts:538-543`, `core/src/flow.ts:219-221`, `ai/src/generate/action.ts:147-149`, `ai/src/prompt.ts:273-275,490-492`; action subtypes in `core/src/registry.ts:40-62`).
71+
72+
| `genkit:type` | `genkit:metadata:subtype` | Kind |
73+
|---|---|---|
74+
| `action` | `model`, `background-model` | `LLM` |
75+
| `action` | `tool`, `tool.v2` | `TOOL` |
76+
| `action` | `retriever` | `RETRIEVER` |
77+
| `action` | `embedder` | `EMBEDDING` |
78+
| `action` | `reranker` | `RERANKER` |
79+
| `action` | `evaluator` | `EVALUATOR` |
80+
| `action` | `agent` (beta agents) | `AGENT` |
81+
| `action` | `flow` and every other subtype (`custom`, `prompt`, `executable-prompt`, `util`, `indexer`, `resource`, ...) | `CHAIN` |
82+
| `flowStep` (`ai.run()`), `util` (the `generate` span), `promptTemplate` (`render`), `dotprompt` | n/a | `CHAIN` |
83+
| anything else, or no `genkit:type` | | not set; the span is still exported |
84+
85+
### Attributes
86+
87+
| Future AGI key | Source | Where |
88+
|---|---|---|
89+
| `fi.span.kind`, `gen_ai.span.kind` | table above | every Genkit span |
90+
| `gen_ai.request.model` | `genkit:name` of the model action span (the registered name, e.g. `googleai/gemini-2.5-flash`; not parsed) | model spans |
91+
| `gen_ai.usage.input_tokens` / `output_tokens` / `total_tokens` | `usage.inputTokens` / `outputTokens` / `totalTokens` in the model span's `genkit:output` | model spans only |
92+
| `gen_ai.response.finish_reasons` | `finishReason` in the model span's `genkit:output` | model spans |
93+
| `tool.name`, `gen_ai.tool.name` | `genkit:name` of the tool action span | tool spans |
94+
| `session.id` | `genkit:metadata:agent:sessionId` (`ai/src/agent.ts:1061-1063`) | the beta agent span only |
95+
| `session.id`, `user.id`, `metadata`, `tag.tags` | fi-core `setSession` / `setUser` / `setMetadata` / `setTags` around the flow call | every span started in that context |
96+
| `input.value` / `output.value` (+ `application/json` mime types) | `genkit:input` / `genkit:output` | only with `captureContent: true` |
97+
| `genkit:*` | passed through, except the content keys below | every Genkit span |
98+
99+
Unavailable at 1.42.0, and not set: provider, cost, user id (unless the app sets it), retriever documents, and every `GenerationUsage` field the inventory did not see on a span (`thoughtsTokens`, `cachedContentTokens`, the character / image / video / audio counters, `custom`). Those stay inside `genkit:output`.
100+
101+
Tokens are written only on model call spans. The `generate` span's `genkit:output` repeats the last turn's usage; it stays inside that JSON and is not promoted. If any non-model span carries a key the collector promotes (`gen_ai.usage.*`, `llm.token_count.*`, `llm.usage.*`, `gen_ai.cost.total`, `llm.cost.total`), the processor moves it to `genkit.usage.<key>`, so the trace-wide token sum equals the model calls.
102+
103+
Errors: Genkit sets the OTel status to `ERROR` and records an `exception` event on every span the error passes through (`core/src/tracing/instrumentation.ts:153-172`); both pass through unchanged. `genkit:isFailureSource` marks the first failing span.
104+
105+
### Session
106+
107+
Genkit 1.42.0 has no session key on plain flows. Beta agents (`genkit/beta` `defineAgent` / `agent.chat()`) tag the agent span with `genkit:metadata:agent:sessionId`, which becomes `session.id` on that span. For flows, set the session yourself around the call:
108+
109+
```typescript
110+
import { context } from "@opentelemetry/api";
111+
import { setSession } from "@traceai/fi-core";
112+
113+
await context.with(setSession(context.active(), { sessionId: "chat-123" }), () => myFlow(input));
114+
```
115+
116+
## Content
117+
118+
Content is off by default. That is stricter than Genkit, which records inputs and outputs on its spans.
119+
120+
| Genkit key | Default | `captureContent: true` |
121+
|---|---|---|
122+
| `genkit:input`, `genkit:output`, `genkit:init` | dropped | exported, and copied to `input.value` / `output.value` |
123+
| `genkit:metadata:interrupt`, `genkit:metadata:resumed` (tool interrupts) | dropped | exported |
124+
| `genkit:metadata:context` (request context; Genkit redacts `auth` and `secrets`, headers can remain) | dropped | dropped |
125+
126+
Usage and finish reason are read from `genkit:output` before it is dropped, so tokens are present with content off.
127+
128+
The processor never changes Genkit's span. Genkit's Dev UI and any other exporter see the original attributes; Future AGI gets a mapped copy.
129+
130+
## Flush and shutdown
131+
132+
- `flushTracing()` from `genkit/tracing` (`core/src/tracing.ts:113`) flushes every processor, this one included. Call it before a short script exits.
133+
- `FIGenkitSpanProcessor.forceFlush()` and `shutdown()` never reject. Each is bounded by `flushTimeoutMillis` (default 30000 ms). A collector that is down is logged through the OTel diag logger; the flow still returns.
134+
- SIGTERM: `genkit` installs a SIGTERM / SIGINT listener at import that stops its reflection servers and calls `process.exit(0)` without flushing (`genkit/src/genkit.ts:786-793`). An app listener that awaits `flushTracing()` loses that race. `flushOnSignals(flush)` moves the listeners registered so far behind a bounded `flush` (default 10000 ms) and then runs them in order. Call it after `import "genkit"` and after `await enableTelemetry(...)`. It returns a function that restores the original listeners.
135+
- Export goes through the fi-core provider's own span processor and exporter. This package adds no queue of its own.
136+
137+
## Span inventory at 1.42.0
138+
139+
`contract/inventory.mjs` runs one flow (an `ai.run()` step, `generateStream` with a tool loop: two model calls, one tool) twice, once called and once streamed, on Genkit's `mockModel`, and records the spans Genkit hands to a processor. 14 spans, all from instrumentation scope `genkit-tracer`, all shaped like `@opentelemetry/sdk-trace-base` 1.25 spans (`parentSpanId`, `instrumentationLibrary`; the processor converts them for fi-core's 2.x exporter).
140+
141+
| Span | `genkit:type` / subtype | Attribute keys (all strings unless noted) |
142+
|---|---|---|
143+
| flow (`inventoryFlow`) | `action` / `flow` | `genkit:type`, `genkit:metadata:subtype`, `genkit:key`, `genkit:name`, `genkit:isRoot` (boolean), `genkit:path`, `genkit:input`, `genkit:output`, `genkit:state`, `genkit:metadata:context` |
144+
| step (`prepare`) | `flowStep` | `genkit:type`, `genkit:name`, `genkit:path`, `genkit:output`, `genkit:state` |
145+
| `generate` | `util` | `genkit:type`, `genkit:name`, `genkit:path`, `genkit:input`, `genkit:output`, `genkit:state` |
146+
| model (`inventory/mock`) | `action` / `model` | `genkit:type`, `genkit:metadata:subtype`, `genkit:key`, `genkit:name`, `genkit:path`, `genkit:input`, `genkit:output`, `genkit:state` |
147+
| tool (`lookup`) | `action` / `tool` | same keys as the model span |
148+
149+
The model span's `genkit:output` is the model response JSON: `message`, `finishReason`, `usage` (`inputTokens`, `outputTokens`, `totalTokens` from the mock), `latencyMs`. The `generate` span's `genkit:output` has `message`, `finishReason`, `usage` (the last turn's), `custom`, `request`. A beta agent run (contract `agent` journey) adds `render` (`promptTemplate`, `genkit:metadata:promptName`), a `runTurn-1` step (`flowStep`, `genkit:metadata:agent:snapshotId`) and the agent span (`action` / `agent`, `genkit:metadata:agent:sessionId`, `genkit:init`). Error spans add `genkit:isFailureSource`.
150+
151+
## Tests
152+
153+
From `typescript/packages/traceai_genkit`:
154+
155+
```bash
156+
pnpm exec jest
157+
```
158+
159+
From the repository root (shared harness in `python/tests/harness`):
160+
161+
```bash
162+
PYTHONPATH=python/tests uv run --no-project --python 3.11 --with pytest --with protobuf --with opentelemetry-proto \
163+
pytest typescript/packages/traceai_genkit/contract -q -p no:cacheprovider --noconftest -o addopts=''
164+
```
165+
166+
The contract suite builds the package, runs `contract/run_fixture.mjs` against the real fi-core exporter and the shared receiver, and checks the collector path and headers, the resource, span names, kinds and parenting, model and tokens, the trace token sum, content off by default with an opt-in control run, one closed model span for a streamed flow, error status and exception events, SIGTERM flushing (with a control that shows a plain listener loses the race), a collector that is down, Genkit's Dev UI reflection server and telemetry export in `GENKIT_ENV=dev`, the inventory drift check, the packed tarball, and the CJS / ESM entry points. `NODE_BINARY` selects the node binary; `TRACEAI_NODE_MATRIX` (path-separated) widens the entry-point check.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Span inventory for the pinned genkit version (TH-8240, architecture section 1).
3+
*
4+
* Runs one flow with Genkit's own `mockModel` (genkit/testing) and one tool,
5+
* once with generate() and once with streamFlow, and records every span Genkit
6+
* hands to a span processor passed through `enableTelemetry({ spanProcessors })`.
7+
* No network: GENKIT_TELEMETRY_SERVER is left unset, so Genkit's own
8+
* TraceServerExporter skips the POST, and nothing is exported anywhere else.
9+
*
10+
* Usage: node contract/inventory.mjs [--values]
11+
* Prints JSON: one entry per span with name, genkit:type, subtype, and the
12+
* attribute keys with their JS types. `--values` also prints values (the dump
13+
* then contains the fixture prompt and model output).
14+
*/
15+
import { genkit, z } from 'genkit';
16+
import { enableTelemetry, flushTracing } from 'genkit/tracing';
17+
import { mockModel } from 'genkit/testing';
18+
19+
const showValues = process.argv.includes('--values');
20+
const spans = [];
21+
const recorder = {
22+
onStart() {},
23+
onEnd(span) {
24+
spans.push(span);
25+
},
26+
forceFlush: async () => {},
27+
shutdown: async () => {},
28+
};
29+
30+
await enableTelemetry({ spanProcessors: [recorder] });
31+
32+
const ai = genkit({});
33+
const model = mockModel(ai, {
34+
name: 'inventory/mock',
35+
respond: (req, { sendChunk }) => {
36+
const last = req.messages.at(-1);
37+
if (last?.content?.some((p) => p.toolResponse)) {
38+
sendChunk('final ');
39+
return { text: 'final answer', usage: { inputTokens: 21, outputTokens: 4, totalTokens: 25 } };
40+
}
41+
return {
42+
toolRequests: [{ name: 'lookup', input: { id: 7 } }],
43+
usage: { inputTokens: 11, outputTokens: 3, totalTokens: 14 },
44+
};
45+
},
46+
});
47+
const lookup = ai.defineTool(
48+
{ name: 'lookup', description: 'look up a record', inputSchema: z.object({ id: z.number() }), outputSchema: z.string() },
49+
async ({ id }) => `record-${id}`
50+
);
51+
const flow = ai.defineFlow({ name: 'inventoryFlow', inputSchema: z.string(), outputSchema: z.string() }, async (q, { sendChunk }) => {
52+
const step = await ai.run('prepare', async () => q.trim());
53+
const { stream, response } = ai.generateStream({ model, prompt: step, tools: [lookup] });
54+
for await (const chunk of stream) sendChunk(chunk.text);
55+
return (await response).text;
56+
});
57+
58+
await flow(' hello ');
59+
const streamed = flow.stream(' streamed ');
60+
for await (const _ of streamed.stream) {
61+
// drain
62+
}
63+
await streamed.output;
64+
await flushTracing();
65+
66+
const out = spans.map((s) => {
67+
const attrs = s.attributes;
68+
const entry = {
69+
name: s.name,
70+
'genkit:type': attrs['genkit:type'],
71+
'genkit:metadata:subtype': attrs['genkit:metadata:subtype'],
72+
parentSpanId: s.parentSpanId ?? s.parentSpanContext?.spanId ?? null,
73+
spanId: s.spanContext().spanId,
74+
status: s.status,
75+
events: s.events.map((e) => e.name),
76+
keys: Object.fromEntries(Object.entries(attrs).map(([k, v]) => [k, typeof v])),
77+
readableSpanShape: {
78+
hasParentSpanId: 'parentSpanId' in s,
79+
hasParentSpanContext: 'parentSpanContext' in s,
80+
hasInstrumentationLibrary: 'instrumentationLibrary' in s,
81+
hasInstrumentationScope: 'instrumentationScope' in s,
82+
scope: (s.instrumentationScope ?? s.instrumentationLibrary)?.name,
83+
},
84+
};
85+
if (showValues) entry.values = attrs;
86+
return entry;
87+
});
88+
process.stdout.write(JSON.stringify(out, null, 2) + '\n');
89+
process.exit(0);

0 commit comments

Comments
 (0)