6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
141 lines
3.9 KiB
TypeScript
141 lines
3.9 KiB
TypeScript
import { TraceAttr } from '@/lib/copilot/generated/trace-attributes-v1'
|
|
import { fetchGo } from '@/lib/copilot/request/go/fetch'
|
|
import { getMothershipBaseURL } from '@/lib/copilot/server/agent-url'
|
|
import { env } from '@/lib/core/config/env'
|
|
|
|
/**
|
|
* Chat API key operations against the Sim Agent's `/api/validate-key/*`
|
|
* endpoints.
|
|
*
|
|
* The single issuer and reader for every caller: the settings UI, which
|
|
* authenticates by session, and the CLI token exchange, which authenticates by
|
|
* a redeemed authorization code. Routes stay thin so the surface survives the
|
|
* settings page being retired.
|
|
*/
|
|
|
|
export interface CopilotApiKeySummary {
|
|
id: string
|
|
/** Masked — the full key is only ever returned at creation. */
|
|
displayKey: string
|
|
name: string | null
|
|
createdAt: string | null
|
|
lastUsed: string | null
|
|
}
|
|
|
|
export interface GeneratedCopilotApiKey {
|
|
id: string
|
|
apiKey: string
|
|
}
|
|
|
|
/** Carries the upstream status so callers can mirror it instead of flattening to 500. */
|
|
export class CopilotApiKeyError extends Error {
|
|
constructor(
|
|
message: string,
|
|
readonly upstreamStatus?: number
|
|
) {
|
|
super(message)
|
|
this.name = 'CopilotApiKeyError'
|
|
}
|
|
}
|
|
|
|
interface ValidateKeyCall {
|
|
userId: string
|
|
path: string
|
|
operation: string
|
|
body: Record<string, unknown>
|
|
failure: string
|
|
attributes?: Record<string, string>
|
|
}
|
|
|
|
/**
|
|
* Shared request envelope. Every `/api/validate-key/*` endpoint is a POST
|
|
* carrying `userId`, authenticated with the service key when one is configured
|
|
* and traced under the same span naming.
|
|
*/
|
|
async function callValidateKey({
|
|
userId,
|
|
path,
|
|
operation,
|
|
body,
|
|
failure,
|
|
attributes,
|
|
}: ValidateKeyCall): Promise<unknown> {
|
|
const mothershipBaseURL = await getMothershipBaseURL({ userId })
|
|
|
|
const res = await fetchGo(`${mothershipBaseURL}/api/validate-key/${path}`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...(env.COPILOT_API_KEY ? { 'x-api-key': env.COPILOT_API_KEY } : {}),
|
|
},
|
|
body: JSON.stringify({ userId, ...body }),
|
|
spanName: `sim → go /api/validate-key/${path}`,
|
|
operation,
|
|
attributes: { [TraceAttr.UserId]: userId, ...attributes },
|
|
})
|
|
|
|
if (!res.ok) {
|
|
throw new CopilotApiKeyError(failure, res.status || 500)
|
|
}
|
|
|
|
return res.json().catch(() => null)
|
|
}
|
|
|
|
export async function listCopilotApiKeys(userId: string): Promise<CopilotApiKeySummary[]> {
|
|
const data = (await callValidateKey({
|
|
userId,
|
|
path: 'get-api-keys',
|
|
operation: 'get_api_keys',
|
|
body: {},
|
|
failure: 'Failed to get keys',
|
|
})) as
|
|
| { id: string; apiKey: string; name?: string; createdAt?: string; lastUsed?: string }[]
|
|
| null
|
|
|
|
if (!Array.isArray(data)) {
|
|
throw new CopilotApiKeyError('Invalid response from Sim Agent', 500)
|
|
}
|
|
|
|
return data.map((key) => ({
|
|
id: key.id,
|
|
displayKey: `•••••${(typeof key.apiKey === 'string' ? key.apiKey : '').slice(-6)}`,
|
|
name: key.name || null,
|
|
createdAt: key.createdAt || null,
|
|
lastUsed: key.lastUsed || null,
|
|
}))
|
|
}
|
|
|
|
export async function generateCopilotApiKey(
|
|
userId: string,
|
|
name: string
|
|
): Promise<GeneratedCopilotApiKey> {
|
|
const data = (await callValidateKey({
|
|
userId,
|
|
path: 'generate',
|
|
operation: 'generate_api_key',
|
|
body: { name },
|
|
failure: 'Failed to generate copilot API key',
|
|
})) as { apiKey?: string; id?: string } | null
|
|
|
|
if (!data?.apiKey) {
|
|
throw new CopilotApiKeyError('Invalid response from Sim Agent', 500)
|
|
}
|
|
|
|
return { id: data.id || 'new', apiKey: data.apiKey }
|
|
}
|
|
|
|
export async function deleteCopilotApiKey(userId: string, apiKeyId: string): Promise<void> {
|
|
const data = (await callValidateKey({
|
|
userId,
|
|
path: 'delete',
|
|
operation: 'delete_api_key',
|
|
body: { apiKeyId },
|
|
failure: 'Failed to delete key',
|
|
attributes: { [TraceAttr.ApiKeyId]: apiKeyId },
|
|
})) as { success?: boolean } | null
|
|
|
|
if (!data?.success) {
|
|
throw new CopilotApiKeyError('Invalid response from Sim Agent', 500)
|
|
}
|
|
}
|