Skip to content
165 linesCodeBlameRaw
1/**
2 * `@g1t`, every workspace's built-in orchestrator
3 * (docs.g1t.sh/guides/agents/, "g1t, the orchestrator"). Pure, so it is
4 * tested on its own.
5 *
6 * It is an agent row like any other, marked builtin, so it is billed,
7 * gated, routed and audited the same way. What sets it apart is here:
8 * the fields nobody may change, the job it always has, the roster of
9 * specialists it is given, and the rails on how it delegates.
10 */
11import type { AgentStatus, ModelTier, NewWorkspaceAgent } from "@g1t/contracts";
12
13import { BUILTIN_AGENT_HANDLE, ORCHESTRATOR_TEMPLATE } from "../../../packages/contracts/src/workspace-agents.ts";
14import { type Checked, type Definition, DEFAULT_AUTONOMY, DEFAULT_BUDGET, DEFAULT_CAPACITY, DEFAULT_ROUTING } from "./definition.ts";
15import { listOf } from "./teammates.ts";
16import { MAX_HAND_OFFS } from "./tools.ts";
17
18export const BUILTIN_ROLE = "Your orchestrator: delegates to the team's agents, or does the work itself";
19
20/** What a new workspace's @g1t starts as. `instructions` are the workspace's additions, none at first. */
21export function builtinDefinition(): Definition {
22 return {
23 handle: BUILTIN_AGENT_HANDLE,
24 display_name: "g1t",
25 role: BUILTIN_ROLE,
26 instructions: "",
27 personality_preset: "friendly",
28 personality: "",
29 routing: DEFAULT_ROUTING,
30 budget: DEFAULT_BUDGET,
31 autonomy: DEFAULT_AUTONOMY,
32 capacity: DEFAULT_CAPACITY,
33 template: ORCHESTRATOR_TEMPLATE,
34 avatar_seed: BUILTIN_AGENT_HANDLE,
35 title: "Orchestrator",
36 responsibilities: [],
37 subagents: [],
38 faces: "internal",
39 reading: [],
40 skills_off: [],
41 };
42}
43
44/** What nobody may change on the built-in agent: who it is and what its job is. */
45export const PROTECTED: (keyof NewWorkspaceAgent)[] = [
46 "handle",
47 "display_name",
48 "role",
49 "title",
50 "responsibilities",
51 "subagents",
52 "template",
53 "faces",
54];
55
56/**
57 * `changes` to the built-in agent with its fixed fields taken out, whatever
58 * they say: a profile form sends every field (an empty title, no
59 * responsibilities), and only the editable ones apply.
60 */
61export function builtinChanges(_current: Definition, changes: Partial<NewWorkspaceAgent>): Checked<Partial<NewWorkspaceAgent>> {
62 if (!changes || typeof changes !== "object") return { ok: false, message: "Send the agent's fields." };
63 const rest: Partial<NewWorkspaceAgent> = { ...changes };
64 for (const key of PROTECTED) delete rest[key];
65 return { ok: true, value: rest };
66}
67
68/** One specialist as @g1t sees it. */
69export type Specialist = {
70 handle: string;
71 display_name: string;
72 role: string;
73 title?: string;
74 /** The teams it is on, by name (identity's team memberships). */
75 teams?: string[];
76 responsibilities?: string[];
77 status: AgentStatus;
78 spent_month_micros: number;
79 /** The monthly cap, or null for none. */
80 monthly_micros: number | null;
81};
82
83const STATUS_WORDS: Record<AgentStatus, string> = {
84 idle: "idle",
85 working: "working",
86 waiting: "waiting on someone",
87 out_of_budget: "out of budget",
88 paused: "paused",
89};
90
91const dollars = (micros: number) => `$${(Math.max(0, micros) / 1_000_000).toFixed(2)}`;
92
93/** Where a specialist sits: "QA Engineer on QA and Web", or its title or role alone. */
94function placeLine(agent: Specialist): string {
95 const title = agent.title?.trim() || agent.role.replace(/\.$/, "");
96 return agent.teams?.length ? `${title} on ${listOf(agent.teams)}` : title;
97}
98
99/**
100 * The roster, one line per specialist, so g1t can route "QA should look"
101 * to the right one: `- @margo (Margo): QA Engineer on QA. Does: review pull
102 * requests; write test plans. idle; $1.20 of $20.00 this month.`
103 */
104export function rosterLines(specialists: Specialist[]): string {
105 if (!specialists.length) return "There are no specialists in this workspace yet.";
106 return specialists
107 .map((agent) => {
108 const name = agent.display_name && agent.display_name.toLowerCase() !== agent.handle ? ` (${agent.display_name})` : "";
109 const spend = agent.monthly_micros ? `${dollars(agent.spent_month_micros)} of ${dollars(agent.monthly_micros)}` : `${dollars(agent.spent_month_micros)}, no cap`;
110 const duties = agent.responsibilities?.length ? ` Does: ${agent.responsibilities.map((d) => d.replace(/\.$/, "")).join("; ")}.` : "";
111 return `- @${agent.handle}${name}: ${placeLine(agent)}.${duties} ${STATUS_WORDS[agent.status] ?? agent.status}; ${spend} this month.`;
112 })
113 .join("\n");
114}
115
116/** The most colleagues one reply may hand work to (the hand_off tool's rail). */
117export const MAX_DELEGATES = MAX_HAND_OFFS;
118
119/** @g1t's job: fixed, whatever the workspace adds. */
120export function orchestratorInstructions(specialists: Specialist[], extra: string): string {
121 const available = specialists.filter((agent) => agent.status !== "out_of_budget" && agent.status !== "paused");
122 const job = [
123 "You are the workspace's orchestrator. People come to you when they don't know who should do something. You either answer, hand the work to the right specialist, or do it yourself.",
124 "",
125 "### The team",
126 "",
127 rosterLines(specialists),
128 "",
129 "### How you decide",
130 "",
131 "1. **Answer directly** when it is a question, a summary or a quick judgment you can give from this conversation.",
132 "2. **Delegate** when a specialist's role fits the work: call hand_off with their handle and a crisp brief, written to them: what is wanted, why, what done looks like, and any constraint (deadlines, what not to touch). If they are in this conversation the brief is posted here; otherwise a group message opens with the person who asked, you and them, and a card here links to it. Then tell the person in a sentence who has it and where.",
133 "3. **Do it yourself, or suggest a specialist,** when nobody fits. Say so plainly, offer to take it on yourself, and if this kind of work will recur, suggest setting one up (for example: \"Want me to set up a release manager for this?\").",
134 "",
135 "### Rules for delegating",
136 "",
137 "- Only hand_off delegates. An @mention in your message wakes no agent and reaches nobody outside this conversation, so writing \"@mike, could you…\" hands nothing over.",
138 "- Describing the team is not delegating: name specialists without @ (\"Mike, our technical recruiter\") unless they are in this conversation.",
139 `- Hand off to at most ${MAX_DELEGATES} specialists for one message. Split bigger work into steps and hand off the next step when the first is done.`,
140 "- Never delegate in a loop: don't hand work back to a specialist who handed it to you, and don't hand the same work to the same specialist twice.",
141 "- Don't delegate to a specialist who is out of budget or paused; say they are unavailable and why.",
142 "- The person sees every hand-off, and their access still limits what any agent does for them.",
143 "- Once you've handed something off, let the specialist answer; speak again when someone asks you.",
144 "- When asked what everyone is working on, answer from the team list above.",
145 available.length ? "" : "\nNo specialist is available right now, so do the work yourself or suggest creating one.",
146 ].join("\n");
147 const added = extra.trim();
148 return added ? `${job}\n\n### Added by this workspace\n\n${added}` : job;
149}
150
151/** How long a thread is before @g1t's delegation call is worth a larger model. */
152export const LONG_THREAD = 6;
153
154/**
155 * Where @g1t's reply starts: the small tier, or the large one for a
156 * delegation decision in a long thread (more than `LONG_THREAD` messages,
157 * with specialists to choose from). Its floor and ceiling apply after.
158 */
159export function orchestratorTier(messages: number, specialists: number): ModelTier {
160 return messages > LONG_THREAD && specialists > 0 ? "large" : "small";
161}
162
163/** The friendly notice when @g1t has no model to run on. */
164export const BUILTIN_NO_MODEL =
165 "Hi, I'm g1t. I'd love to help, but this workspace doesn't have a model I can use yet. An owner can add AI credit under Billing, or connect the workspace's own model provider under Integrations, and I'll be ready.";