Skip to content

Add internal attributes definition helper #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ docs/
node_modules/
protocol/
src/proto/
src/room/attributes/
yarn.lock
pnpm-lock.yaml
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LogLevel, LoggerNames, getLogger, setLogExtension, setLogLevel } from '
import DefaultReconnectPolicy from './room/DefaultReconnectPolicy';
import type { ReconnectContext, ReconnectPolicy } from './room/ReconnectPolicy';
import Room, { ConnectionState } from './room/Room';
import * as attributesHelper from './room/attributes';
import LocalParticipant from './room/participant/LocalParticipant';
import Participant, { ConnectionQuality, ParticipantKind } from './room/participant/Participant';
import type { ParticipantTrackPermission } from './room/participant/ParticipantTrackPermission';
Expand Down Expand Up @@ -72,6 +73,8 @@ export type {
} from './room/types';
export * from './version';
export {
/** @internal */
attributesHelper,
ConnectionQuality,
ConnectionState,
CriticalTimers,
Expand Down
73 changes: 73 additions & 0 deletions src/room/attributes/agent_attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// This file is auto-generated. Do not edit manually.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the auto-generation occurs elsewhere, maybe worth updating these comments with a link to it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, currently restructuring the code gen anyways to support JSON parsing so holding off until that's done


const AgentStateAttribute = {
key: "lk.agent.state",
values: ["initializing", "idle", "listening", "thinking", "speaking"],
default: "idle",
} as const;

const PublishOnBehalfAttribute = {
key: "lk.publish_on_behalf",
values: [], // Any string value allowed
default: null,
} as const;

export const AgentAttributesRegistry = [
AgentStateAttribute,
PublishOnBehalfAttribute,
] as const;

/**
* Agent connection state
*/
export type AgentStateType = (typeof AgentStateAttribute.values)[number];
/**
* The identity of the user that the agent is publishing on behalf of
*/
export type PublishOnBehalfType = string;

export interface AgentAttributes {
/**
* Agent connection state
*/
[AgentStateAttribute.key]?: AgentStateType;
/**
* The identity of the user that the agent is publishing on behalf of
*/
[PublishOnBehalfAttribute.key]?: PublishOnBehalfType;
}

/**
* Parse a raw attribute map into a typed AgentAttributesRegistry
* @param attributes Raw attribute map (key-value pairs)
* @returns Typed AgentAttributes
*/
export function parseAgentAttributes(
attributes: Record<string, string>
): AgentAttributes {
const result: AgentAttributes = {};

// First add default values from registry
for (const attribute of AgentAttributesRegistry) {
if (attribute.default !== null) {
result[attribute.key] = attribute.default as any;
}
}

// Then override with provided values
for (const [key, value] of Object.entries(attributes)) {
for (const attribute of AgentAttributesRegistry) {
if (key === attribute.key) {
// For attributes with allowed values (non-empty array), validate the value
if (attribute.values.length > 0 && !(attribute.values as readonly string[]).includes(value)) {
// Skip invalid values
continue;
}
result[key] = value as any;
break;
}
}
}

return result;
}
4 changes: 4 additions & 0 deletions src/room/attributes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as agent from './agent_attr';
import * as transcription from './transcription_attr';

export { agent, transcription };
88 changes: 88 additions & 0 deletions src/room/attributes/transcription_attr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// This file is auto-generated. Do not edit manually.

const SegmentIdAttribute = {
key: "lk.segment_id",
values: [], // Any string value allowed
default: null,
} as const;

const TranscribedTrackIdAttribute = {
key: "lk.transcribed_track_id",
values: [], // Any string value allowed
default: null,
} as const;

const TranscriptionFinalAttribute = {
key: "lk.transcription_final",
values: ["true", "false"],
default: null,
} as const;

export const TranscriptionAttributesRegistry = [
SegmentIdAttribute,
TranscribedTrackIdAttribute,
TranscriptionFinalAttribute,
] as const;

/**
* The segment id of the transcription
*/
export type SegmentIdType = string;
/**
* The associated track id of the transcription
*/
export type TranscribedTrackIdType = string;
/**
* Whether the transcription is final
*/
export type TranscriptionFinalType = (typeof TranscriptionFinalAttribute.values)[number];

export interface TranscriptionAttributes {
/**
* The segment id of the transcription
*/
[SegmentIdAttribute.key]?: SegmentIdType;
/**
* The associated track id of the transcription
*/
[TranscribedTrackIdAttribute.key]?: TranscribedTrackIdType;
/**
* Whether the transcription is final
*/
[TranscriptionFinalAttribute.key]?: TranscriptionFinalType;
}

/**
* Parse a raw attribute map into a typed TranscriptionAttributesRegistry
* @param attributes Raw attribute map (key-value pairs)
* @returns Typed TranscriptionAttributes
*/
export function parseTranscriptionAttributes(
attributes: Record<string, string>
): TranscriptionAttributes {
const result: TranscriptionAttributes = {};

// First add default values from registry
for (const attribute of TranscriptionAttributesRegistry) {
if (attribute.default !== null) {
result[attribute.key] = attribute.default as any;
}
}

// Then override with provided values
for (const [key, value] of Object.entries(attributes)) {
for (const attribute of TranscriptionAttributesRegistry) {
if (key === attribute.key) {
// For attributes with allowed values (non-empty array), validate the value
if (attribute.values.length > 0 && !(attribute.values as readonly string[]).includes(value)) {
// Skip invalid values
continue;
}
result[key] = value as any;
break;
}
}
}

return result;
}
4 changes: 4 additions & 0 deletions src/utils/cloneDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export function cloneDeep<T>(value: T): T {
}

if (typeof structuredClone === 'function') {
if (typeof value === 'object' && value !== null) {
// ensure that the value is not a proxy by spreading it
return structuredClone({ ...value });
}
return structuredClone(value);
} else {
return JSON.parse(JSON.stringify(value)) as T;
Expand Down
Loading