Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/admin-api/access-keys/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ const validateCredentialFilenames = (
.map((item) => item.trim())
.filter(Boolean);

if (!normalized.length) {
throw new Error('At least one credential must be selected');
}

if (normalized.some((item) => !available.has(item))) {
throw new Error('Selected credentials must exist');
}
Expand Down
4 changes: 0 additions & 4 deletions app/admin-api/access-keys/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ const validateCredentialFilenames = (
.map((item) => item.trim())
.filter(Boolean);

if (!normalized.length) {
throw new Error('At least one credential must be selected');
}

if (normalized.some((item) => !available.has(item))) {
throw new Error('Selected credentials must exist');
}
Expand Down
11 changes: 11 additions & 0 deletions app/credentials/credential-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface CredentialCardProps {
current: CurrentCredentialInfo | null;
form: CredentialFormState;
onCredentialFirstMessageRoleToSystemChange: (value: boolean) => void;
onCredentialFirstSystemMessageRoleToUserChange: (value: boolean) => void;
onCredentialUpstreamProtocolChange: (value: 'chat' | 'responses') => void;
onDelete: () => void;
onEdit: () => void;
Expand All @@ -36,6 +37,7 @@ export const CredentialCard = ({
current,
form,
onCredentialFirstMessageRoleToSystemChange,
onCredentialFirstSystemMessageRoleToUserChange,
onCredentialUpstreamProtocolChange,
onDelete,
onEdit,
Expand Down Expand Up @@ -108,6 +110,9 @@ export const CredentialCard = ({
? text('credentials.credentialUpstreamResponsesTag')
: text('credentials.credentialUpstreamChatTag')}
</Tag>
{credential.first_system_message_role_to_user ? (
<Tag>{text('credentials.credentialFirstSystemAsUserTag')}</Tag>
) : null}
<Tag>
{credential.first_message_role_to_system
? text('credentials.credentialRoleAsSystemTag')
Expand Down Expand Up @@ -160,6 +165,12 @@ export const CredentialCard = ({
onChange={onCredentialFirstMessageRoleToSystemChange}
title={text('credentials.credentialRoleAsSystem')}
/>
<ToggleOption
checked={form.firstSystemMessageRoleToUser}
description={text('credentials.credentialFirstSystemAsUserHelp')}
onChange={onCredentialFirstSystemMessageRoleToUserChange}
title={text('credentials.credentialFirstSystemAsUser')}
/>
</div>
<Flexbox gap={8} horizontal>
<Button icon={X} onClick={onResetCredentialForm}>
Expand Down
5 changes: 5 additions & 0 deletions app/credentials/credential-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface CredentialGroupProps {
form: CredentialFormState;
items: CredentialSummary[];
onCredentialFirstMessageRoleToSystemChange: (value: boolean) => void;
onCredentialFirstSystemMessageRoleToUserChange: (value: boolean) => void;
onCredentialUpstreamProtocolChange: (value: 'chat' | 'responses') => void;
onDelete: (index: number) => void;
onEdit: (credential: CredentialSummary) => void;
Expand All @@ -27,6 +28,7 @@ export const CredentialGroup = ({
form,
items,
onCredentialFirstMessageRoleToSystemChange,
onCredentialFirstSystemMessageRoleToUserChange,
onCredentialUpstreamProtocolChange,
onDelete,
onEdit,
Expand All @@ -47,6 +49,9 @@ export const CredentialGroup = ({
onCredentialFirstMessageRoleToSystemChange={
onCredentialFirstMessageRoleToSystemChange
}
onCredentialFirstSystemMessageRoleToUserChange={
onCredentialFirstSystemMessageRoleToUserChange
}
onCredentialUpstreamProtocolChange={
onCredentialUpstreamProtocolChange
}
Expand Down
19 changes: 19 additions & 0 deletions app/credentials/credentials.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface CredentialSummary {
expires_in: number | null;
filename: string;
first_message_role_to_system: boolean;
first_system_message_role_to_user?: boolean;
has_refresh_token: boolean;
index: number;
is_expired: boolean;
Expand Down Expand Up @@ -93,6 +94,7 @@ export interface CredentialFormState {
bearerToken: string;
editingIndex: number | null;
firstMessageRoleToSystem: boolean;
firstSystemMessageRoleToUser: boolean;
upstreamProtocol: 'chat' | 'responses';
userId: string;
}
Expand Down Expand Up @@ -147,6 +149,7 @@ export const defaultCredentialsState: CredentialsState = {
bearerToken: '',
editingIndex: null,
firstMessageRoleToSystem: false,
firstSystemMessageRoleToUser: false,
upstreamProtocol: 'chat',
userId: '',
},
Expand Down Expand Up @@ -188,6 +191,7 @@ export interface CredentialsTabController {
onCallbackUrlChange: (value: string) => void;
onCopyAuthUrl: () => void;
onCredentialFirstMessageRoleToSystemChange: (value: boolean) => void;
onCredentialFirstSystemMessageRoleToUserChange: (value: boolean) => void;
onCredentialUpstreamProtocolChange: (value: 'chat' | 'responses') => void;
onCredentialTokenChange: (value: string) => void;
onCredentialUserIdChange: (value: string) => void;
Expand Down Expand Up @@ -235,6 +239,7 @@ const Credentials = () => {
onCallbackUrlChange,
onCopyAuthUrl,
onCredentialFirstMessageRoleToSystemChange,
onCredentialFirstSystemMessageRoleToUserChange,
onCredentialUpstreamProtocolChange,
onCredentialTokenChange,
onCredentialUserIdChange,
Expand Down Expand Up @@ -495,6 +500,14 @@ const Credentials = () => {
onChange={onCredentialFirstMessageRoleToSystemChange}
title={credentialsText('credentials.credentialRoleAsSystem')}
/>
<ToggleOption
checked={credentials.form.firstSystemMessageRoleToUser}
description={credentialsText(
'credentials.credentialFirstSystemAsUserHelp',
)}
onChange={onCredentialFirstSystemMessageRoleToUserChange}
title={credentialsText('credentials.credentialFirstSystemAsUser')}
/>
</div>
<Flexbox horizontal>
<Button icon={Save} onClick={onAddCredential} type="primary">
Expand Down Expand Up @@ -620,6 +633,9 @@ const Credentials = () => {
onCredentialFirstMessageRoleToSystemChange={
onCredentialFirstMessageRoleToSystemChange
}
onCredentialFirstSystemMessageRoleToUserChange={
onCredentialFirstSystemMessageRoleToUserChange
}
onCredentialUpstreamProtocolChange={
onCredentialUpstreamProtocolChange
}
Expand All @@ -635,6 +651,9 @@ const Credentials = () => {
onCredentialFirstMessageRoleToSystemChange={
onCredentialFirstMessageRoleToSystemChange
}
onCredentialFirstSystemMessageRoleToUserChange={
onCredentialFirstSystemMessageRoleToUserChange
}
onCredentialUpstreamProtocolChange={
onCredentialUpstreamProtocolChange
}
Expand Down
18 changes: 18 additions & 0 deletions app/page-shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,17 @@ const AdminPageLayoutContent = ({
index: credentials.form.editingIndex,
first_message_role_to_system:
credentials.form.firstMessageRoleToSystem,
first_system_message_role_to_user:
credentials.form.firstSystemMessageRoleToUser,
upstream_protocol: credentials.form.upstreamProtocol,
}
: {
access_token: credentials.form.bearerToken.trim(),
bearer_token: credentials.form.bearerToken.trim(),
first_message_role_to_system:
credentials.form.firstMessageRoleToSystem,
first_system_message_role_to_user:
credentials.form.firstSystemMessageRoleToUser,
upstream_protocol: credentials.form.upstreamProtocol,
user_id: credentials.form.userId.trim() || undefined,
},
Expand Down Expand Up @@ -1021,6 +1025,7 @@ const AdminPageLayoutContent = ({
bearerToken: '',
editingIndex: null,
firstMessageRoleToSystem: false,
firstSystemMessageRoleToUser: false,
upstreamProtocol: 'chat',
userId: '',
},
Expand Down Expand Up @@ -1736,6 +1741,15 @@ const AdminPageLayoutContent = ({
},
}));
},
onCredentialFirstSystemMessageRoleToUserChange: (value) => {
setCredentials((current) => ({
...current,
form: {
...current.form,
firstSystemMessageRoleToUser: value,
},
}));
},
onCredentialUpstreamProtocolChange: (value) => {
setCredentials((current) => ({
...current,
Expand Down Expand Up @@ -1774,6 +1788,9 @@ const AdminPageLayoutContent = ({
editingIndex: credential.index,
firstMessageRoleToSystem:
credential.first_message_role_to_system,
firstSystemMessageRoleToUser: Boolean(
credential.first_system_message_role_to_user,
),
upstreamProtocol: credential.upstream_protocol,
userId: credential.user_id ?? '',
},
Expand Down Expand Up @@ -1827,6 +1844,7 @@ const AdminPageLayoutContent = ({
bearerToken: '',
editingIndex: null,
firstMessageRoleToSystem: false,
firstSystemMessageRoleToUser: false,
upstreamProtocol: 'chat',
userId: '',
},
Expand Down
26 changes: 3 additions & 23 deletions lib/server/domain/access-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ const pruneAccessKeyStore = async (
changed = true;
}

if (!remainingFilenames.length) {
changed = true;
return null;
}

if (
remainingFilenames.some(
(filename, index) => filename !== record.credentialFilenames[index],
Expand All @@ -101,9 +96,7 @@ const pruneAccessKeyStore = async (
return {
changed,
store: {
accessKeys: accessKeys.filter(
(record): record is AccessKeyRecord => record !== null,
),
accessKeys,
},
};
};
Expand Down Expand Up @@ -297,10 +290,6 @@ export const createAccessKey = async ({
throw new Error('Access key name is required');
}

if (!normalizedCredentialFilenames.length) {
throw new Error('At least one credential must be selected');
}

return mutateAccessKeyStore((store) => {
const now = new Date().toISOString();
const record: AccessKeyRecord = {
Expand Down Expand Up @@ -338,10 +327,6 @@ export const updateAccessKey = async (
throw new Error('Access key name is required');
}

if (!normalizedCredentialFilenames.length) {
throw new Error('At least one credential must be selected');
}

return mutateAccessKeyStore((store) => {
const record = store.accessKeys.find((item) => item.id === id);

Expand Down Expand Up @@ -375,7 +360,7 @@ export const removeCredentialReferencesFromAccessKeys = async (
): Promise<boolean> => {
return mutateAccessKeyStore((store) => {
let changed = false;
const accessKeys = store.accessKeys.flatMap((record) => {
const accessKeys = store.accessKeys.map((record) => {
const credentialFilenames = normalizeCredentialFilenames(
record.credentialFilenames,
).filter((filename) => filename !== credentialFilename);
Expand All @@ -384,11 +369,6 @@ export const removeCredentialReferencesFromAccessKeys = async (
changed = true;
}

if (!credentialFilenames.length) {
changed = true;
return [];
}

if (
credentialFilenames.some(
(filename, index) => filename !== record.credentialFilenames[index],
Expand All @@ -397,7 +377,7 @@ export const removeCredentialReferencesFromAccessKeys = async (
changed = true;
}

return [{ ...record, credentialFilenames }];
return { ...record, credentialFilenames };
});

if (!changed) {
Expand Down
14 changes: 13 additions & 1 deletion lib/server/domain/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type CredentialData = Record<string, unknown> & {
responses_passthrough?: boolean;
upstream_protocol?: 'chat' | 'responses';
first_message_role_to_system?: boolean;
first_system_message_role_to_user?: boolean;
supported_models?: string;
};

Expand Down Expand Up @@ -368,7 +369,7 @@ const getEligibleRecords = (
records: CredentialRecord[],
allowedCredentialFilenames?: string[],
): CredentialRecord[] => {
const allowedSet = allowedCredentialFilenames?.length
const allowedSet = allowedCredentialFilenames
? new Set(allowedCredentialFilenames)
: null;

Expand Down Expand Up @@ -479,6 +480,9 @@ export const listCredentials = async (): Promise<{
first_message_role_to_system: getBooleanSetting(
record.data.first_message_role_to_system,
),
first_system_message_role_to_user: getBooleanSetting(
record.data.first_system_message_role_to_user,
),
user_id:
record.data.user_id ?? record.data.user_info?.email ?? 'unknown',
};
Expand Down Expand Up @@ -590,6 +594,10 @@ export const addCredential = async (
credentialData.first_message_role_to_system ??
existingPayload.first_message_role_to_system,
),
first_system_message_role_to_user: getBooleanSetting(
credentialData.first_system_message_role_to_user ??
existingPayload.first_system_message_role_to_user,
),
};

await writeStorageJson('credentials', jsonFilename, payload);
Expand Down Expand Up @@ -795,12 +803,16 @@ export const getCredentialProxySettings = (
credential: CredentialData | null | undefined,
): {
firstMessageRoleToSystem: boolean;
firstSystemMessageRoleToUser: boolean;
upstreamProtocol: 'chat' | 'responses';
} => {
return {
firstMessageRoleToSystem: getBooleanSetting(
credential?.first_message_role_to_system,
),
firstSystemMessageRoleToUser: getBooleanSetting(
credential?.first_system_message_role_to_user,
),
upstreamProtocol:
credential?.upstream_protocol === 'responses' ||
(credential?.upstream_protocol !== 'chat' &&
Expand Down
Loading
Loading