| import { readBody, HTTPError } from 'h3'; |
| import { DustClient } from '../dust-client.js'; |
|
|
| |
| |
| |
| export const chatCompletions = async (event) => { |
| try { |
| const body = await readBody(event); |
|
|
| |
| if (!body.messages || !Array.isArray(body.messages)) { |
| throw new HTTPError(400, 'Invalid request: messages array is required'); |
| } |
|
|
| if (body.messages.length === 0) { |
| throw new HTTPError(400, 'Invalid request: messages array cannot be empty'); |
| } |
|
|
| |
| const dustClient = new DustClient( |
| process.env.WORKSPACE_ID, |
| event.context.apiKey || process.env.DUST_API_KEY |
| ); |
|
|
| |
| if (body.stream === true) { |
| return handleStreamingResponse(event, dustClient, body); |
| } |
|
|
| |
| const result = await dustClient.chatCompletion(body); |
|
|
| return result; |
|
|
| } catch (error) { |
| console.error('Chat completion error:', error); |
|
|
| if (error instanceof HTTPError) { |
| throw error; |
| } |
|
|
| throw new HTTPError(500, `Internal server error: ${error.message}`); |
| } |
| }; |
|
|
| |
|
|
| |
| |
| |
| async function handleStreamingResponse(event, dustClient, body) { |
| |
| event.res.headers.set('Content-Type', 'text/event-stream'); |
| event.res.headers.set('Cache-Control', 'no-cache'); |
| event.res.headers.set('Connection', 'keep-alive'); |
|
|
| |
| return await dustClient.chatCompletion({ ...body, stream: true }); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| export const listModels = async (event) => { |
| try { |
| |
| const dustClient = new DustClient( |
| process.env.WORKSPACE_ID, |
| event.context.apiKey || process.env.DUST_API_KEY |
| ); |
|
|
| |
| const models = await dustClient.getModels(); |
| return models; |
|
|
| } catch (error) { |
| console.error('Failed to get models:', error); |
|
|
| |
| return { |
| object: "list", |
| data: [ |
| { |
| id: "dust", |
| object: "model", |
| created: Math.floor(Date.now() / 1000), |
| owned_by: "dust", |
| permission: [], |
| root: "dust", |
| parent: null, |
| name: "Default Dust Assistant", |
| description: "Default Dust assistant agent" |
| } |
| ] |
| }; |
| } |
| }; |
|
|
| |
| |
| |
| |
| export const getModel = async (event) => { |
| try { |
| const modelId = event.context.params?.model || 'dust'; |
|
|
| |
| const dustClient = new DustClient( |
| process.env.WORKSPACE_ID, |
| event.context.apiKey || process.env.DUST_API_KEY |
| ); |
|
|
| |
| let agent; |
| try { |
| agent = await dustClient.getAgentConfiguration(modelId); |
| } catch (error) { |
| |
| agent = await dustClient.findAgent(modelId); |
| } |
|
|
| if (!agent) { |
| throw new HTTPError(404, `Model '${modelId}' not found`); |
| } |
|
|
| |
| return { |
| id: agent.sId, |
| object: "model", |
| created: agent.versionCreatedAt ? Math.floor(new Date(agent.versionCreatedAt).getTime() / 1000) : Math.floor(Date.now() / 1000), |
| owned_by: "dust", |
| permission: [], |
| root: agent.sId, |
| parent: null, |
| name: agent.name, |
| description: agent.description, |
| scope: agent.scope, |
| model: agent.model, |
| actions: agent.actions?.length || 0, |
| maxStepsPerRun: agent.maxStepsPerRun, |
| visualizationEnabled: agent.visualizationEnabled |
| }; |
|
|
| } catch (error) { |
| console.error('Failed to get model:', error); |
|
|
| if (error instanceof HTTPError) { |
| throw error; |
| } |
|
|
| throw new HTTPError(500, `Failed to get model: ${error.message}`); |
| } |
| }; |
|
|