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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"dependencies": {
"@bufbuild/protobuf": "^2.14.0",
"@gramaziokohler/compas-pb-ts": "^3.0.0",
"mqtt": "^5.10.0"
"mqtt": "^5.10.0",
"unique-names-generator": "^4.7.1"
},
"devDependencies": {
"@biomejs/biome": "2.2.3",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 40 additions & 2 deletions src/agents/AgentLauncher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { pbDump, pbLoadBytes } from "@gramaziokohler/compas-pb-ts";
import {
adjectives,
animals,
colors,
countries,
uniqueNamesGenerator,
} from "unique-names-generator";

import {
TaskAllocationMessage,
Expand All @@ -14,6 +21,38 @@ import type { MqttService } from "./MqttService";
import { ExecutionContext } from "./ExecutionContext";
import { Task } from "./Task";

/**
* A readable agent id, of the same shape as the Python launcher's
* `coolname.generate_slug(4)`: "brave-red-panda-of-japan". Agents are identified by this
* in logs and in the UI, so it needs to be recognisable at a glance rather than unique.
*/
function slug(value: string): string {
// dictionary entries are not all plain words: countries bring dots, accents and
// apostrophes ("U.S. Outlying Islands", "Cote d'Ivoire", "Aland Islands")
return value
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}

function memorableAgentId(): string {
const name = uniqueNamesGenerator({
dictionaries: [adjectives, colors, animals],
length: 3,
separator: "-",
style: "lowerCase",
});
const place = uniqueNamesGenerator({
dictionaries: [countries],
length: 1,
style: "lowerCase",
});

return `${slug(name)}-of-${slug(place)}`;
}

export class AgentLauncher {
private static instance: AgentLauncher;
private mqttService: MqttService;
Expand All @@ -31,8 +70,7 @@ export class AgentLauncher {
registerAntikytheraTypes();

this.mqttService = mqttService;
this.agentId =
agentId ?? `agent-${Math.random().toString(36).slice(2, 10)}`;
this.agentId = agentId ?? memorableAgentId();

console.log(`Initializing Agent Launcher ${this.agentId}`);

Expand Down
25 changes: 25 additions & 0 deletions test/agent-launcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,28 @@ describe("AgentLauncher", () => {
expect(mqtt.sentOn("antikythera/task/completed")).toBeUndefined();
});
});

describe("agent ids", () => {
it("are readable, in the shape the Python launcher produces", () => {
// e.g. "brave-red-panda-of-japan", matching coolname.generate_slug(4). Agents are
// identified by this in logs and in the UI, so a random string is not good enough.
const ids = Array.from({ length: 20 }, () =>
newLauncher(
new FakeMqttService(),
undefined as unknown as string,
).getAgentId(),
);

for (const id of ids) {
expect(id).toMatch(/^[a-z]+-[a-z]+-[a-z]+-of-[a-z-]+$/);
}
// and not all the same
expect(new Set(ids).size).toBeGreaterThan(1);
});

it("accepts an explicit id", () => {
expect(newLauncher(new FakeMqttService(), "given-name").getAgentId()).toBe(
"given-name",
);
});
});
Loading