Skip to content

Commit

Permalink
Maximize import compatibility in example (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherry authored Sep 25, 2024
1 parent 45cb43f commit 856ebe2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/sixty-timers-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/agents": patch
"livekit-agents-examples": minor
---

Maximize self-import compatibility
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ serve as a starting point for a completely custom agent application.
To join a LiveKit room that's already active, you can use the `connect` command:

```bash
bash my_agent.ts connect --room <my-room>
node my_agent.ts connect --room <my-room>
```

### FAQ
Expand Down
14 changes: 14 additions & 0 deletions agents/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ export interface Agent {
prewarm?: (proc: JobProcess) => unknown;
}

/** Helper to check if an object is an agent before running it.
*
* @internal
*/
export function isAgent(obj: unknown): obj is Agent {
return (
typeof obj === 'object' &&
obj !== null &&
'entry' in obj &&
typeof (obj as Agent).entry === 'function' &&
(('prewarm' in obj && typeof (obj as Agent).prewarm === 'function') || !('prewarm' in obj))
);
}

/**
* Helper to define an agent according to the required interface.
* @example A basic agent with entry and prewarm functions
Expand Down
11 changes: 9 additions & 2 deletions agents/src/ipc/job_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fork } from 'child_process';
import { EventEmitter, once } from 'events';
import type { Logger } from 'pino';
import { fileURLToPath } from 'url';
import type { Agent } from '../generator.js';
import { type Agent, isAgent } from '../generator.js';
import type { RunningJobInfo } from '../job.js';
import { JobContext } from '../job.js';
import { JobProcess } from '../job.js';
Expand Down Expand Up @@ -93,7 +93,14 @@ if (process.send) {
// [0] `node'
// [1] import.meta.filename
// [2] import.meta.filename of function containing entry file
const agent: Agent = await import(process.argv[2]).then((agent) => agent.default);
const moduleFile = process.argv[2];
const agent: Agent = await import(moduleFile).then((module) => {
const agent = module.default;
if (agent === undefined || !isAgent(agent)) {
throw new Error(`Unable to load agent: Missing or invalid default export in ${moduleFile}`);
}
return agent;
});
if (!agent.prewarm) {
agent.prewarm = defaultInitializeProcessFunc;
}
Expand Down
3 changes: 3 additions & 0 deletions agents/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ export class WorkerOptions {
logLevel?: string;
}) {
this.agent = agent;
if (!this.agent) {
throw new Error('No Agent file was passed to the worker');
}
this.requestFunc = requestFunc;
this.loadFunc = loadFunc;
this.loadThreshold = loadThreshold;
Expand Down
6 changes: 3 additions & 3 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "module",
"scripts": {
"build": "tsc",
"lint": "eslint -f unix \"src/**/*.ts\""
"lint": "eslint -f unix \"src/**/*.ts\"",
"minimal": "node dist/minimal_assistant.js dev"
},
"devDependencies": {
"typescript": "^5.0.0"
Expand All @@ -14,6 +15,5 @@
"@livekit/agents-plugin-openai": "workspace:*",
"@livekit/rtc-node": "^0.8.1",
"zod": "^3.23.8"
},
"version": null
}
}
3 changes: 2 additions & 1 deletion examples/src/minimal_assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
import { type JobContext, WorkerOptions, cli, defineAgent } from '@livekit/agents';
import { OmniAssistant, defaultConversationConfig } from '@livekit/agents-plugin-openai';
import { fileURLToPath } from 'node:url';
import { z } from 'zod';

export default defineAgent({
Expand Down Expand Up @@ -36,4 +37,4 @@ export default defineAgent({
},
});

cli.runApp(new WorkerOptions({ agent: import.meta.filename }));
cli.runApp(new WorkerOptions({ agent: fileURLToPath(import.meta.url) }));

0 comments on commit 856ebe2

Please sign in to comment.