|
| 1 | +import { |
| 2 | + apiClientManager, |
| 3 | + type ApiRequestOptions, |
| 4 | + type CreatePromptOverrideRequestBody, |
| 5 | + type ListPromptsResponseBody, |
| 6 | + type ListPromptVersionsResponseBody, |
| 7 | + type PromptOkResponseBody, |
| 8 | + type PromptOverrideCreatedResponseBody, |
| 9 | + type ResolvePromptResponseBody, |
| 10 | + type UpdatePromptOverrideRequestBody, |
| 11 | +} from "@trigger.dev/core/v3"; |
| 12 | +import type { ResolvedPrompt } from "./prompt.js"; |
| 13 | + |
| 14 | +function makeToAISDKTelemetry( |
| 15 | + slug: string, |
| 16 | + promptId: string, |
| 17 | + version: number, |
| 18 | + labels: string[], |
| 19 | + model?: string, |
| 20 | + input?: string |
| 21 | +) { |
| 22 | + return function toAISDKTelemetry(additionalMetadata?: Record<string, string>) { |
| 23 | + return { |
| 24 | + experimental_telemetry: { |
| 25 | + isEnabled: true as const, |
| 26 | + metadata: { |
| 27 | + "prompt.slug": slug, |
| 28 | + "prompt.id": promptId, |
| 29 | + "prompt.version": String(version), |
| 30 | + "prompt.labels": labels.join(", "), |
| 31 | + ...(model ? { "prompt.model": model } : {}), |
| 32 | + ...(input ? { "prompt.input": input } : {}), |
| 33 | + ...additionalMetadata, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }; |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Resolve a prompt by slug, calling the API to get the current version's |
| 42 | + * compiled text. Works both inside and outside of a task context — requires |
| 43 | + * an API client to be configured (via `configure()` or task runtime). |
| 44 | + */ |
| 45 | +export async function resolvePrompt( |
| 46 | + slug: string, |
| 47 | + variables?: Record<string, unknown>, |
| 48 | + options?: { label?: string; version?: number; requestOptions?: ApiRequestOptions } |
| 49 | +): Promise<ResolvedPrompt> { |
| 50 | + const apiClient = apiClientManager.clientOrThrow(); |
| 51 | + const vars = variables ?? {}; |
| 52 | + const response = await apiClient.resolvePrompt(slug, { |
| 53 | + variables: vars, |
| 54 | + label: options?.label, |
| 55 | + version: options?.version, |
| 56 | + }); |
| 57 | + |
| 58 | + const data = response.data; |
| 59 | + const inputJson = Object.keys(vars).length > 0 ? JSON.stringify(vars) : undefined; |
| 60 | + |
| 61 | + return { |
| 62 | + promptId: data.promptId, |
| 63 | + version: data.version, |
| 64 | + labels: data.labels, |
| 65 | + text: data.text ?? "", |
| 66 | + model: data.model ?? undefined, |
| 67 | + config: (data.config as Record<string, unknown>) ?? undefined, |
| 68 | + toAISDKTelemetry: makeToAISDKTelemetry( |
| 69 | + data.slug, |
| 70 | + data.promptId, |
| 71 | + data.version, |
| 72 | + data.labels, |
| 73 | + data.model ?? undefined, |
| 74 | + inputJson |
| 75 | + ), |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +/** List all prompts in the current environment. */ |
| 80 | +export function listPrompts( |
| 81 | + requestOptions?: ApiRequestOptions |
| 82 | +): Promise<ListPromptsResponseBody> { |
| 83 | + const apiClient = apiClientManager.clientOrThrow(); |
| 84 | + return apiClient.listPrompts(requestOptions); |
| 85 | +} |
| 86 | + |
| 87 | +/** List all versions for a prompt. */ |
| 88 | +export function listPromptVersions( |
| 89 | + slug: string, |
| 90 | + requestOptions?: ApiRequestOptions |
| 91 | +): Promise<ListPromptVersionsResponseBody> { |
| 92 | + const apiClient = apiClientManager.clientOrThrow(); |
| 93 | + return apiClient.listPromptVersions(slug, requestOptions); |
| 94 | +} |
| 95 | + |
| 96 | +/** Promote a code-deployed version to be the current version. */ |
| 97 | +export async function promotePromptVersion( |
| 98 | + slug: string, |
| 99 | + version: number, |
| 100 | + requestOptions?: ApiRequestOptions |
| 101 | +): Promise<PromptOkResponseBody> { |
| 102 | + const apiClient = apiClientManager.clientOrThrow(); |
| 103 | + return apiClient.promotePromptVersion(slug, { version }, requestOptions); |
| 104 | +} |
| 105 | + |
| 106 | +/** Create an override — a dashboard/API edit that takes priority over the deployed version. */ |
| 107 | +export async function createPromptOverride( |
| 108 | + slug: string, |
| 109 | + body: CreatePromptOverrideRequestBody, |
| 110 | + requestOptions?: ApiRequestOptions |
| 111 | +): Promise<PromptOverrideCreatedResponseBody> { |
| 112 | + const apiClient = apiClientManager.clientOrThrow(); |
| 113 | + return apiClient.createPromptOverride(slug, body, requestOptions); |
| 114 | +} |
| 115 | + |
| 116 | +/** Update the active override's content or model. */ |
| 117 | +export async function updatePromptOverride( |
| 118 | + slug: string, |
| 119 | + body: UpdatePromptOverrideRequestBody, |
| 120 | + requestOptions?: ApiRequestOptions |
| 121 | +): Promise<PromptOkResponseBody> { |
| 122 | + const apiClient = apiClientManager.clientOrThrow(); |
| 123 | + return apiClient.updatePromptOverride(slug, body, requestOptions); |
| 124 | +} |
| 125 | + |
| 126 | +/** Remove the active override, reverting to the current deployed version. */ |
| 127 | +export async function removePromptOverride( |
| 128 | + slug: string, |
| 129 | + requestOptions?: ApiRequestOptions |
| 130 | +): Promise<PromptOkResponseBody> { |
| 131 | + const apiClient = apiClientManager.clientOrThrow(); |
| 132 | + return apiClient.removePromptOverride(slug, requestOptions); |
| 133 | +} |
| 134 | + |
| 135 | +/** Reactivate a previously removed override version. */ |
| 136 | +export async function reactivatePromptOverride( |
| 137 | + slug: string, |
| 138 | + version: number, |
| 139 | + requestOptions?: ApiRequestOptions |
| 140 | +): Promise<PromptOkResponseBody> { |
| 141 | + const apiClient = apiClientManager.clientOrThrow(); |
| 142 | + return apiClient.reactivatePromptOverride(slug, { version }, requestOptions); |
| 143 | +} |
0 commit comments