This repo demonstrates how to build AI applications using @qwen-code/sdk. It contains two demos:
- CLI demo: an interactive terminal chatbot
- Next.js demo: a web chat app with streaming responses (SSE)
The SDK expects a qwen executable to be available on your machine.
After installation, verify it works:
qwen --versionInstall project dependencies (this repo uses @qwen-code/sdk@^0.1.1 as defined in package.json):
npm installThe interactive CLI chatbot (demo/cli-chatbot.ts) demonstrates:
- Multi-turn conversation via an async generator that continuously yields user messages
- Streaming output by printing partial deltas as they arrive
- Tool call display by printing tool-use blocks and tool results
- Tool permission control via the
canUseToolcallback
npm run start:cli- Start the program and you will see a prompt
- Type a message and press Enter
- The assistant responds in a streaming fashion
- If tools are used, the tool calls/results are printed
- Type
quit,exit, orqto exit
The CLI demo builds an async iterable of SDKUserMessage and passes it to the SDK query() API, then consumes the streamed SDKMessage output with for await ... of.
The Next.js 16 web chat app demonstrates:
- Client-side session id to group turns on the UI
- Streaming responses using Server-Sent Events (SSE)
- Multi-turn context by sending
historyand building a single prompt string server-side - Error handling for network/stream failures
Development:
npm run devProduction:
npm run build npm startThe app runs at http://localhost:3000.
Creates a new session id (demo mode; no server-side persistence).
Response example:
{ "sessionId": "550e8400-e29b-41d4-a716-446655440000", "status": "success", "message": "Session created successfully" }Note: the current UI initializes the session id on the client (see src/app/chat/page.tsx), so calling this endpoint is optional.
Sends a message and receives a streaming SSE response.
Request body:
{ "sessionId": "550e8400-e29b-41d4-a716-446655440000", "prompt": "Hello, please help me write a React component", "history": [ { "role": "user", "content": "..." }, { "role": "assistant", "content": "..." } ] }Response:
Returns an SSE stream where each event is a JSON-encoded SDK message (i.e., SDKMessage) in data: ...\n\n format. The server ends the stream for the current turn when it sees a type: "result" message.
qwen-code-sdk-chat-demo/ ├── demo/ │ └── cli-chatbot.ts # CLI chatbot demo ├── src/ │ ├── app/ │ │ ├── api/ │ │ │ └── chat/ │ │ │ ├── init/ # Session init API │ │ │ └── send/ # SSE streaming chat API │ │ ├── chat/ # Chat page │ │ └── page.tsx # Home page │ ├── components/ │ │ ├── chat/ # Chat UI components │ │ └── ui/ # Base UI components │ ├── lib/ │ │ ├── chat-session.ts # Advanced session manager (not wired by default) │ │ └── utils.ts # Utilities │ └── types/ │ └── chat.ts # App-level types (not required by the demos) ├── package.json └── README.md - Node.js 20+
- npm (or pnpm)
npm run dev- start Next.js dev servernpm run build- build production bundlenpm start- start production servernpm run lint- run ESLintnpm run start:cli- start the CLI chatbot demo
- Framework: Next.js 16 (App Router)
- Language: TypeScript
- UI: React 19, Tailwind CSS 4
- SDK:
@qwen-code/sdk(this repo uses^0.1.1)
This project is provided by Qwen Team for learning and reference.