-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
parseRequestBody()
, `transformPayloadForOpenAICompatibility()…
…`, `verifyAndParseRequest()`, `getUserMessage()`, `getUserConfirmation()` (#34)
- Loading branch information
Showing
8 changed files
with
448 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// @ts-check | ||
|
||
export * from "./lib/verification.js"; | ||
export * from "./lib/parse.js"; | ||
export * from "./lib/response.js"; | ||
export * from "./lib/verification.js"; |
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,57 @@ | ||
// @ts-check | ||
|
||
import { verifyRequestByKeyId } from "./verification.js"; | ||
|
||
/** @type {import('..').ParseRequestBodyInterface} */ | ||
export function parseRequestBody(body) { | ||
return JSON.parse(body); | ||
} | ||
|
||
/** @type {import('..').TransformPayloadForOpenAICompatibilityInterface} */ | ||
export function transformPayloadForOpenAICompatibility(payload) { | ||
return { | ||
messages: payload.messages.map((message) => { | ||
return { | ||
role: message.role, | ||
name: message.name, | ||
content: message.content, | ||
}; | ||
}), | ||
}; | ||
} | ||
|
||
/** @type {import('..').VerifyAndParseRequestInterface} */ | ||
export async function verifyAndParseRequest(body, signature, keyID, options) { | ||
const isValidRequest = await verifyRequestByKeyId( | ||
body, | ||
signature, | ||
keyID, | ||
options | ||
); | ||
|
||
return { | ||
isValidRequest, | ||
payload: parseRequestBody(body), | ||
}; | ||
} | ||
|
||
/** @type {import('..').GetUserMessageInterface} */ | ||
export function getUserMessage(payload) { | ||
return payload.messages[payload.messages.length - 1].content; | ||
} | ||
|
||
/** @type {import('..').GetUserConfirmationInterface} */ | ||
export function getUserConfirmation(payload) { | ||
const confirmation = | ||
payload.messages[payload.messages.length - 1].copilot_confirmations?.[0]; | ||
|
||
if (!confirmation) return; | ||
|
||
const { id, ...metadata } = confirmation.confirmation; | ||
|
||
return { | ||
accepted: confirmation.state === "accepted", | ||
id, | ||
metadata, | ||
}; | ||
} |
Oops, something went wrong.