- 预览
- 代码
- 文档
复制
import { Agent, run, Runner, setDefaultOpenAIClient, setOpenAIAPI, tool } from "@openai/agents";
import { OpenAI } from "openai";
import * as undici from "undici";
export function createOpenAIAgenticChatAgent() {
let proxyAgent = undefined;
if (process.env.http_proxy) {
proxyAgent = new undici.ProxyAgent(process.env.http_proxy!);
}
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
fetchOptions: {
dispatcher: proxyAgent,
}
});
setDefaultOpenAIClient(openai);
setOpenAIAPI('chat_completions');
const getCurrentTimeTool = tool({
name: 'get_current_time',
description: 'Get the current time',
parameters: {
type: 'object',
properties: {},
required: [],
},
execute: async () => {
const d = new Date();
return `${d.toString()} ${d.toISOString()}`;
}
});
const agent = new Agent({
name: 'Agent',
model: process.env.OPENAI_MODEL!,
instructions: 'You are a helpful assistant.',
tools: [getCurrentTimeTool],
});
const runner = new Runner({
tracingDisabled: true,
})
return {
agent,
runner,
}
// const stream = await runner.run(agent, 'What is the current time?', {
// stream: true,
// });
// const types = new Set<string>();
// for await (const event of stream) {
// console.log(event);
// types.add(event.type);
// }
// console.log(Array.from(types).join(', '));
}
// main().catch(console.error);
Agentic Chat - OpenAI Agent SDK (TypeScript)
What This Demo Shows
This demo showcases AG-Kit’s integration with the OpenAI Agent SDK:- OpenAI Agent SDK Runtime: Uses OpenAI’s Agent + Runner for execution
- Event Bridging: Streams SDK events as AG-UI events (text/tool calls)
- Frontend Tools: Same client tools as other frameworks (alert, color, location)
Technical Implementation
Backend (OpenAI Agent SDK):Agentwith model and instructionsRunnerstreaming enabled- Tools bound via SDK; client tools injected by AG-Kit adapter
useChathook +AgKitChatcomponent- Three built-in tools: alert, change-background-color, get-current-location
- URL parameter switching across frameworks