-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
720 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type * as schema from "@ctrlplane/db/schema"; | ||
import { NextResponse } from "next/server"; | ||
import { openai } from "@ai-sdk/openai"; | ||
import { generateText } from "ai"; | ||
import { z } from "zod"; | ||
|
||
import { logger } from "@ctrlplane/logger"; | ||
|
||
import { env } from "~/env"; | ||
import { parseBody } from "../../body-parser"; | ||
import { request } from "../../middleware"; | ||
|
||
// Allow streaming responses up to 60 seconds | ||
export const maxDuration = 60; | ||
|
||
const bodySchema = z.object({ | ||
prompt: z.string(), | ||
}); | ||
|
||
export const POST = request() | ||
// .use(authn) | ||
.use(parseBody(bodySchema)) | ||
.handle<{ user: schema.User; body: z.infer<typeof bodySchema> }>( | ||
async (ctx) => { | ||
const { body } = ctx; | ||
|
||
try { | ||
console.log( | ||
`Processing AI command request with prompt: ${body.prompt}`, | ||
); | ||
|
||
if (!env.OPENAI_API_KEY) { | ||
logger.error("OPENAI_API_KEY environment variable is not set"); | ||
return NextResponse.json( | ||
{ error: "OPENAI_API_KEY is not set" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
|
||
logger.info("Streaming text from OpenAI..."); | ||
const { text } = await generateText({ | ||
model: openai("gpt-4-turbo"), | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: | ||
"You are a command-line assistant. Return only the shell command " + | ||
"that best matches the user's request, with no explanation or additional text:", | ||
}, | ||
{ | ||
role: "user", | ||
content: ` | ||
Task: ${body.prompt} | ||
Command: | ||
`, | ||
}, | ||
], | ||
}); | ||
|
||
logger.info(`Generated command response: ${text}`); | ||
|
||
return NextResponse.json({ | ||
text: text.trim().replace("`", ""), | ||
}); | ||
} catch (error) { | ||
console.error("Error processing AI command request:", error); | ||
return NextResponse.json( | ||
{ error: "Failed to process command request" }, | ||
{ status: 500 }, | ||
); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Swagger } from "atlassian-openapi"; | ||
|
||
export const openapi: Swagger.SwaggerV3 = { | ||
openapi: "3.0.0", | ||
info: { | ||
title: "Ctrlplane API", | ||
version: "1.0.0", | ||
}, | ||
paths: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Tx } from "@ctrlplane/db"; | ||
import { z } from "zod"; | ||
|
||
import { eq } from "@ctrlplane/db"; | ||
import * as schema from "@ctrlplane/db/schema"; | ||
|
||
import { authn } from "../auth"; | ||
import { parseBody } from "../body-parser"; | ||
import { request } from "../middleware"; | ||
|
||
const resourceToResource = z.object({ | ||
workspaceId: z.string().uuid(), | ||
fromType: z.literal("resource"), | ||
fromIdentifier: z.string(), | ||
toType: z.literal("resource"), | ||
toIdentifier: z.string(), | ||
type: z.literal("associated_with").or(z.literal("depends_on")), | ||
}); | ||
|
||
const deploymentToResource = z.object({ | ||
workspaceId: z.string().uuid(), | ||
deploymentId: z.string().uuid(), | ||
resourceIdentifier: z.string(), | ||
type: z.literal("created"), | ||
}); | ||
|
||
const bodySchema = z.union([resourceToResource, deploymentToResource]); | ||
|
||
const resourceToResourceRelationship = async ( | ||
db: Tx, | ||
body: z.infer<typeof resourceToResource>, | ||
) => { | ||
return Response.json( | ||
{ error: "Resources must be in the same workspace" }, | ||
{ status: 400 }, | ||
); | ||
}; | ||
|
||
export const POST = request() | ||
.use(authn) | ||
.use(parseBody(bodySchema)) | ||
.handle<{ body: z.infer<typeof bodySchema> }>(async (ctx) => { | ||
const { body, db } = ctx; | ||
|
||
return Response.json({}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.