|
| 1 | +/** |
| 2 | + * ACP Client AI Provider - OpenCode |
| 3 | + * |
| 4 | + * This example demonstrates how to stream text responses from OpenCode |
| 5 | + * using ACP mode through the AI SDK. |
| 6 | + * |
| 7 | + * Prerequisites: |
| 8 | + * - Install OpenCode CLI (if not already installed) |
| 9 | + * - Run: opencode acp |
| 10 | + * |
| 11 | + * Run with: |
| 12 | + * deno run --allow-all examples/opencode-stream-text.ts |
| 13 | + * or |
| 14 | + * npx tsx examples/opencode-stream-text.ts |
| 15 | + */ |
| 16 | + |
| 17 | +import { createACPProvider } from "../mod.ts"; |
| 18 | +import { streamText } from "ai"; |
| 19 | +import process from "node:process"; |
| 20 | +import { logChunkToConsole } from "../src/utils.ts"; |
| 21 | + |
| 22 | +async function main() { |
| 23 | + // Create ACP provider for OpenCode |
| 24 | + const provider = createACPProvider({ |
| 25 | + command: "opencode", |
| 26 | + args: ["acp"], |
| 27 | + env: { |
| 28 | + ...(process.env as Record<string, string>), |
| 29 | + }, |
| 30 | + session: { |
| 31 | + cwd: process.cwd(), |
| 32 | + mcpServers: [], |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + const prompt = process.env.PROMPT ?? |
| 37 | + "Run sleep 3 and then print 'Hello, OpenCode ACP!' to the console."; |
| 38 | + |
| 39 | + console.log({ prompt }); |
| 40 | + |
| 41 | + const model = provider.languageModel(); |
| 42 | + const { toolCalls } = streamText({ |
| 43 | + model, |
| 44 | + prompt, |
| 45 | + tools: provider.tools, |
| 46 | + onChunk: (arg: any) => { |
| 47 | + const { chunk } = arg; |
| 48 | + logChunkToConsole(chunk); |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + console.log( |
| 53 | + `Tool Calls: ${(await toolCalls).map((t: any) => t.toolName).join(", ")}`, |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +main().catch((error) => { |
| 58 | + console.error("❌ Error:", error); |
| 59 | + process.exit(1); |
| 60 | +}); |
0 commit comments