@astropods/adapter-ai-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # @astropods/adapter-ai-sdk
2
+
3
+ `@astropods/adapter-ai-sdk` exports two functions you can use independently:
4
+
5
+ - `astroTelemetry()` returns AI SDK `experimental_telemetry` settings wired to Astro's OTLP exporter.
6
+ - `serve()` connects a `ToolLoopAgent` (`Experimental_Agent`) to Astro's messaging service to make your agent compatible with the Astropods playground.
7
+
8
+ Targets `ai >= 6.0.0`.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ bun add @astropods/adapter-ai-sdk
14
+ ```
15
+
16
+ ## Send telemetry to Astro
17
+
18
+ Add `astroTelemetry()` into the agent's `experimental_telemetry`:
19
+
20
+ ```typescript
21
+ import { Experimental_Agent as Agent } from "ai";
22
+ import { openai } from "@ai-sdk/openai";
23
+ import { astroTelemetry } from "@astropods/adapter-ai-sdk";
24
+
25
+ const agent = new Agent({
26
+ model: openai("gpt-4o"),
27
+ instructions: "You are a helpful assistant.",
28
+ experimental_telemetry: astroTelemetry(),
29
+ });
30
+ ```
31
+
32
+ Use this on its own when you serve the agent from your own framework and want AI traces reported in the dashboard.
33
+
34
+ ## Serve over Astro messaging
35
+
36
+ To run the agent on Astro messaging, pass it to `serve()`:
37
+
38
+ ```typescript
39
+ import { Experimental_Agent as Agent } from "ai";
40
+ import { openai } from "@ai-sdk/openai";
41
+ import { serve, astroTelemetry } from "@astropods/adapter-ai-sdk";
42
+
43
+ const instructions = "You are a helpful assistant.";
44
+
45
+ const agent = new Agent({
46
+ model: openai("gpt-4o"),
47
+ instructions,
48
+ experimental_telemetry: astroTelemetry(),
49
+ });
50
+
51
+ serve(agent, { name: "My Agent", instructions });
52
+ ```
53
+
54
+ Passing `instructions` into the `serve()` function allows your agent's system prompt to be visible in the Astropods playground. This is optional. To hide your prompts exclude `instructions` from the `serve` call.
55
+
56
+ `serve()` blocks until `SIGINT` or `SIGTERM`. Under `ast dev`, the CLI injects `GRPC_SERVER_ADDR` for you.
57
+
58
+ ## API
59
+
60
+ ### `serve(agent, options?)`
61
+
62
+ Connects the agent to the messaging service.
63
+
64
+ | Option | Type | Description |
65
+ |--------|------|-------------|
66
+ | `name` | `string` | Display name shown in logs and the playground. Defaults to `agent.id`, then `"AI SDK Agent"`. |
67
+ | `instructions` | `string` | Optional. System prompt shown in the playground when provided. |
68
+ | `serverAddress` | `string` | Override the gRPC address. Defaults to `process.env.GRPC_SERVER_ADDR ?? "localhost:9090"`. |
69
+
70
+ ### `astroTelemetry()`
71
+
72
+ Returns `experimental_telemetry` settings for the AI SDK, wired to Astro's OTLP exporter. The helper builds the tracer from an unregistered `NodeTracerProvider`, so it does not modify the OpenTelemetry global.
73
+
74
+ - `OTEL_EXPORTER_OTLP_ENDPOINT` set: returns `{ isEnabled: true, tracer }`.
75
+ - Env var unset (local dev): returns `{ isEnabled: false }`. The AI SDK skips telemetry.
76
+
77
+ Spread it on top of your own settings to add a `functionId` or `metadata`:
78
+
79
+ ```typescript
80
+ experimental_telemetry: { ...astroTelemetry(), functionId: "myAgent" }
81
+ ```
82
+
83
+ ### `AISDKAdapter`
84
+
85
+ The underlying `AgentAdapter` implementation. Use it to compose with other adapters or to call `serve()` from `@astropods/adapter-core`.
86
+
87
+ ## Stream mapping
88
+
89
+ The adapter reads `agent.stream({ prompt }).fullStream` and maps each event to a `StreamHooks` call:
90
+
91
+ | AI SDK event | Hook |
92
+ |--------------|------|
93
+ | `text-delta` | `onChunk(text)` |
94
+ | `reasoning-start` | `onStatusUpdate({ status: "THINKING" })` |
95
+ | `reasoning-end` | `onStatusUpdate({ status: "GENERATING" })` |
96
+ | `tool-input-start` | `onStatusUpdate({ status: "PROCESSING", customMessage: "Running ${toolName}" })` |
97
+ | `tool-input-end` | `onStatusUpdate({ status: "ANALYZING", customMessage: "Finished ${toolName}" })` |
98
+ | `tool-error` | `onError(error)` |
99
+ | `error` | `onError(error)` |
100
+ | `finish` | `onFinish()` |
101
+
102
+ The adapter ignores these events: `start`, `start-step`, `finish-step`, `text-start`, `text-end`, `tool-input-delta`, `tool-call`, `tool-result`, `source`, `file`, `raw`. None of them change what the playground or messaging clients display.
103
+
104
+ ## Troubleshooting
105
+
106
+ If nothing shows up:
107
+
108
+ - Confirm `experimental_telemetry: astroTelemetry()` is on the agent.
109
+ - Confirm `OTEL_EXPORTER_OTLP_ENDPOINT` is set in the deployed container.
110
+ - Check the container logs for OpenTelemetry export errors.
@@ -0,0 +1,17 @@
1
+ import type { Agent, ToolSet } from "ai";
2
+ import type { AgentConfig as MessagingAgentConfig } from "@astropods/messaging";
3
+ import type { AgentAdapter, StreamHooks, StreamOptions } from "@astropods/adapter-core";
4
+ export interface AISDKAdapterOptions {
5
+ name?: string;
6
+ /** The AI SDK `Agent` interface exposes no `instructions` field, so accept them here for the playground. */
7
+ instructions?: string;
8
+ }
9
+ export declare class AISDKAdapter<TOOLS extends ToolSet = ToolSet> implements AgentAdapter {
10
+ private agent;
11
+ readonly name: string;
12
+ private readonly instructions;
13
+ constructor(agent: Agent<never, TOOLS, any>, options?: AISDKAdapterOptions);
14
+ stream(prompt: string, hooks: StreamHooks, _options: StreamOptions): Promise<void>;
15
+ getConfig(): MessagingAgentConfig;
16
+ }
17
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AACzC,OAAO,KAAK,EAAE,WAAW,IAAI,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,aAAa,EACd,MAAM,yBAAyB,CAAC;AAEjC,MAAM,WAAW,mBAAmB;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4GAA4G;IAC5G,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,YAAY,CAAC,KAAK,SAAS,OAAO,GAAG,OAAO,CACvD,YAAW,YAAY;IAMrB,OAAO,CAAC,KAAK;IAJf,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;gBAG5B,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EACvC,OAAO,GAAE,mBAAwB;IAM7B,MAAM,CACV,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAAC,IAAI,CAAC;IA6DhB,SAAS,IAAI,oBAAoB;CAoBlC"}
@@ -0,0 +1,71 @@
1
+ export class AISDKAdapter {
2
+ agent;
3
+ name;
4
+ instructions;
5
+ constructor(agent, options = {}) {
6
+ this.agent = agent;
7
+ this.name = options.name ?? agent.id ?? "AI SDK Agent";
8
+ this.instructions = options.instructions ?? "";
9
+ }
10
+ async stream(prompt, hooks, _options) {
11
+ const result = await this.agent.stream({ prompt });
12
+ // tool-input-end carries only the call id; track id → name on -start.
13
+ const toolNames = new Map();
14
+ for await (const part of result.fullStream) {
15
+ switch (part.type) {
16
+ case "text-delta":
17
+ hooks.onChunk(part.text);
18
+ break;
19
+ case "reasoning-start":
20
+ hooks.onStatusUpdate({ status: "THINKING" });
21
+ break;
22
+ case "reasoning-end":
23
+ hooks.onStatusUpdate({ status: "GENERATING" });
24
+ break;
25
+ case "tool-input-start":
26
+ toolNames.set(part.id, part.toolName);
27
+ hooks.onStatusUpdate({
28
+ status: "PROCESSING",
29
+ customMessage: `Running ${part.toolName}`,
30
+ });
31
+ break;
32
+ case "tool-input-end": {
33
+ const toolName = toolNames.get(part.id) ?? "tool";
34
+ toolNames.delete(part.id);
35
+ hooks.onStatusUpdate({
36
+ status: "ANALYZING",
37
+ customMessage: `Finished ${toolName}`,
38
+ });
39
+ break;
40
+ }
41
+ case "tool-error":
42
+ hooks.onError(part.error instanceof Error
43
+ ? part.error
44
+ : new Error(String(part.error)));
45
+ break;
46
+ case "finish":
47
+ hooks.onFinish();
48
+ break;
49
+ case "error":
50
+ hooks.onError(part.error instanceof Error
51
+ ? part.error
52
+ : new Error(String(part.error)));
53
+ break;
54
+ }
55
+ }
56
+ }
57
+ getConfig() {
58
+ const tools = this.agent.tools;
59
+ const toolConfigs = Object.entries(tools).map(([name, tool]) => ({
60
+ name,
61
+ title: tool.title ?? name,
62
+ description: tool.description ?? "",
63
+ type: "other",
64
+ }));
65
+ return {
66
+ systemPrompt: this.instructions,
67
+ tools: toolConfigs,
68
+ };
69
+ }
70
+ }
71
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAcA,MAAM,OAAO,YAAY;IAOb;IAJD,IAAI,CAAS;IACL,YAAY,CAAS;IAEtC,YACU,KAA+B,EACvC,UAA+B,EAAE;QADzB,UAAK,GAAL,KAAK,CAA0B;QAGvC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,KAAK,CAAC,EAAE,IAAI,cAAc,CAAC;QACvD,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,EAAE,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,MAAM,CACV,MAAc,EACd,KAAkB,EAClB,QAAuB;QAEvB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QAEnD,sEAAsE;QACtE,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;QAE5C,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,MAAM,CAAC,UAAU,EAAE,CAAC;YAC3C,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;gBAClB,KAAK,YAAY;oBACf,KAAK,CAA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C,IAAI,CAAC,KAAK;wBACZ,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAClC,CAAC;oBACF,MAAM;YACV,CAAC;QACH,CAAC;IACH,CAAC;IAED,SAAS;QACP,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAGxB,CAAC;QAEF,MAAM,WAAW,GAAkC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAC1E,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;YACjB,IAAI;YACJ,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,IAAI;YACzB,WAAW,EAAE,IAAI,CAAC,WAAW,IAAI,EAAE;YACnC,IAAI,EAAE,OAAO;SACd,CAAC,CACH,CAAC;QAEF,OAAO;YACL,YAAY,EAAE,IAAI,CAAC,YAAY;YAC/B,KAAK,EAAE,WAAW;SACnB,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ import type { Agent, ToolSet } from "ai";
2
+ import type { ServeOptions } from "@astropods/adapter-core";
3
+ import type { AISDKAdapterOptions } from "./adapter";
4
+ export { AISDKAdapter } from "./adapter";
5
+ export type { AISDKAdapterOptions } from "./adapter";
6
+ export { astroTelemetry } from "./telemetry";
7
+ export declare function serve<TOOLS extends ToolSet = ToolSet>(agent: Agent<never, TOOLS, any>, options?: AISDKAdapterOptions & ServeOptions): void;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAEzC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAErD,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,YAAY,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,wBAAgB,KAAK,CAAC,KAAK,SAAS,OAAO,GAAG,OAAO,EACnD,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,EAC/B,OAAO,GAAE,mBAAmB,GAAG,YAAiB,GAC/C,IAAI,CAGN"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import { serve as serveAdapter } from "@astropods/adapter-core";
2
+ import { AISDKAdapter } from "./adapter";
3
+ export { AISDKAdapter } from "./adapter";
4
+ export { astroTelemetry } from "./telemetry";
5
+ export function serve(agent, options = {}) {
6
+ const adapter = new AISDKAdapter(agent, options);
7
+ serveAdapter(adapter, options);
8
+ }
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,IAAI,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEhE,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAGzC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,UAAU,KAAK,CACnB,KAA+B,EAC/B,UAA8C,EAAE;IAEhD,MAAM,OAAO,GAAG,IAAI,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACjD,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { TelemetrySettings } from "ai";
2
+ export declare function astroTelemetry(): TelemetrySettings;
3
+ //# sourceMappingURL=telemetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAE5C,wBAAgB,cAAc,IAAI,iBAAiB,CAIlD"}
@@ -0,0 +1,8 @@
1
+ import { getOrCreateAstroTracerProvider } from "@astropods/adapter-core";
2
+ export function astroTelemetry() {
3
+ const provider = getOrCreateAstroTracerProvider({ register: false });
4
+ if (!provider)
5
+ return { isEnabled: false };
6
+ return { isEnabled: true, tracer: provider.getTracer("ai.sdk") };
7
+ }
8
+ //# sourceMappingURL=telemetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../src/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AAGzE,MAAM,UAAU,cAAc;IAC5B,MAAM,QAAQ,GAAG,8BAA8B,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;IACrE,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC3C,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;AACnE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@astropods/adapter-ai-sdk",
3
+ "license": "Apache-2.0",
4
+ "version": "0.1.0",
5
+ "description": "Vercel AI SDK adapter for the Astro messaging service. Wraps an Agent and auto-wires OpenTelemetry tracing.",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "dev": "tsc --watch",
21
+ "test": "bun test"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/astropods/adapters.git",
26
+ "directory": "packages/ai-sdk"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "registry": "https://registry.npmjs.org"
31
+ },
32
+ "dependencies": {
33
+ "@astropods/adapter-core": "workspace:*",
34
+ "@astropods/messaging": "latest",
35
+ "@opentelemetry/api": "^1.9.0"
36
+ },
37
+ "peerDependencies": {
38
+ "ai": ">=6.0.0"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "ai": {
42
+ "optional": true
43
+ }
44
+ },
45
+ "devDependencies": {
46
+ "ai": "^6.0.0",
47
+ "@types/bun": "^1.3.5",
48
+ "typescript": "^5.3.0",
49
+ "zod": "^4.3.6"
50
+ }
51
+ }