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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hanzo/build",
"version": "0.2.134",
"version": "0.2.135",
"type": "module",
"license": "Apache-2.0 OR MIT",
"description": "Hanzo Build — say what you want, watch it built, deployed and running. The builder as a component, mounted by the Hanzo app at hanzo.ai.",
Expand Down
4 changes: 4 additions & 0 deletions src/api/pref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface Prefs {
callName?: string
/** What best describes their work. */
work?: string
/** Whether the recommended skills for their work were taken up or put off (customize/roles.tsx); unset until asked. */
roles?: 'done' | 'skipped'
/** What every coding run is told about how they like to work. */
instructions?: string
code?: Code
Expand All @@ -61,6 +63,7 @@ const THEMES: readonly Theme[] = ['system', 'light', 'dark']
const TEXTS: readonly Text[] = ['small', 'medium', 'large']
const MOTIONS: readonly Motion[] = ['system', 'reduced']
const MODES = ['build', 'plan'] as const
const ROLES = ['done', 'skipped'] as const

const obj = (v: unknown): Record<string, unknown> => (v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : {})
const text = (v: unknown): string | undefined => (typeof v === 'string' && v.trim() ? v : undefined)
Expand Down Expand Up @@ -113,6 +116,7 @@ export function prefs(raw: unknown): Prefs {
language: text(p.language),
callName: text(p.callName),
work: text(p.work),
roles: one(p.roles, ROLES),
instructions: text(p.instructions),
code: code(p.code),
side: side(p.side),
Expand Down
199 changes: 199 additions & 0 deletions src/customize/roles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/**
* Skills for a person's work: pick what you do, see the skills that suit it,
* add them all in one click or leave some out first.
*
* It stands in two places. In Customize › Skills it is always there, for anyone
* who wants it. As the first run's card (`first`) it asks once, only a person
* who has not said what they work on and has not put it off, and goes away when
* they add the skills or say not now: the work is kept on the account
* (`Prefs.work`, the one Settings › Account holds) and so is the answer
* (`Prefs.roles`), so it follows them to every device and never returns.
*
* Adding a skill switches it on for the org (`toggle`), which is an org admin's
* to do; a member reads what is recommended and is told who adds it.
*
* Its words are the reader's language (@hanzo/i18n `t`); a skill's name and
* description are the catalogue's, drawn as given.
*/
import { SizableText, XStack, YStack } from '@hanzo/gui'
import { useI18n } from '@hanzo/i18n/react'
import { Check } from '@hanzogui/lucide-icons-2'
import { Button } from '@hanzo/ui'
import { useEffect, useMemo, useState } from 'react'

import { active, brand, tool } from '../api/skills.ts'
import { toggle, type Tool } from '../api/tools.ts'
import { useHost, useHosted, useTarget } from '../host.tsx'
import { usePrefs, type PrefState } from '../prefs.tsx'
import { recommend, WORK } from '../roles.ts'
import { useLoad } from './load.ts'
import { say } from './say.ts'
import { FilterPills, Part } from './ui.tsx'

interface Notice {
text: string
error?: boolean
}

/** Drawn only where the builder's host and settings are mounted, so a chat drawn without them draws nothing. */
export function Roles({ first = false }: { first?: boolean }) {
const hosted = useHosted()
const state = usePrefs(false)
return hosted && state ? <Suggest first={first} state={state} /> : null
}

function Suggest({ first, state }: { first: boolean; state: PrefState }) {
const { t } = useI18n()
const host = useHost()
const target = useTarget()
const signed = Boolean(host.person)
const may = signed && host.admin
const kept = state?.prefs.work ?? ''
const [work, setWork] = useState(kept)
const shelf = useLoad(signed ? () => brand(target) : null, null, [target.api, signed])
const on = useLoad(signed ? () => active(target) : null, [] as Tool[], [target, signed])
const [left, setLeft] = useState<ReadonlySet<string>>(new Set())
const [busy, setBusy] = useState(false)
const [notice, setNotice] = useState<Notice | null>(null)
// The card stays up with its word once the skills are added, and goes on the next visit.
const [said, setSaid] = useState(false)

// The account's work arrives after the first frame; take it up unless the person has already chosen another.
useEffect(() => {
if (kept) setWork((now) => now || kept)
}, [kept])
useEffect(() => setLeft(new Set()), [work])

const lit = useMemo(() => new Set(on.value.map((x) => x.name)), [on.value])
const suited = useMemo(() => recommend(work, shelf.value), [work, shelf.value])
const fresh = suited.filter((e) => !lit.has(tool(e.name)))
const taken = fresh.filter((e) => !left.has(e.name))

// The first run's card: only a signed-in admin who has said nothing about their work and not put this off.
if (!signed) return null
if (first && !said && (!may || kept || state.prefs.roles || shelf.error)) return null

const choose = (id: string) => {
setWork(id)
setNotice(null)
}

const add = async () => {
setBusy(true)
setNotice(null)
try {
await toggle(target, taken.map((e) => tool(e.name)))
await state.save({ work, roles: 'done' })
on.reload()
setSaid(true)
setNotice({ text: t('{n} skills are on', { n: taken.length }) })
} catch (e) {
setNotice({ text: say(e, t), error: true })
} finally {
setBusy(false)
}
}

const later = async () => {
try {
await state.save({ roles: 'skipped' })
} catch (e) {
setNotice({ text: say(e, t), error: true })
}
}

// Put off, the card is gone; added, it is gone from the next visit.
if (first && state.prefs.roles === 'skipped') return null

return (
<Part
title={first ? t('What do you do?') : t('Recommended for your work')}
detail={t('Pick your work and Hanzo suggests the skills that suit it. Add them all in one click, or leave some out first.')}
>
<YStack gap="$3" data-slot="roles" data-first={first || undefined}>
<FilterPills label={t('Your work')} value={work} options={WORK.map((w) => ({ id: w.id, label: t(w.label) }))} onChange={choose} maxVisibleDesktop={11} mobileLimit={5} />
{work && !suited.length ? (
<SizableText size="$2" color="$soft" data-slot="roles-none">
{t('Nothing is recommended for this yet. Browse the skills to add any you like.')}
</SizableText>
) : null}
{suited.length ? (
<YStack gap="$1.5" role="group" aria-label={t('Recommended skills')}>
{suited.map((e) => {
const isOn = lit.has(tool(e.name))
const picked = !isOn && !left.has(e.name)
return (
<XStack
key={e.name}
render="button"
role="checkbox"
aria-checked={isOn || picked}
aria-disabled={isOn || !may || undefined}
data-slot="roles-skill"
data-skill={e.name}
onClick={() => {
if (isOn || !may) return
setLeft((s) => {
const next = new Set(s)
if (next.has(e.name)) next.delete(e.name)
else next.add(e.name)
return next
})
}}
items="flex-start"
gap="$2.5"
p="$2.5"
rounded="$3"
borderWidth={1}
borderColor={picked ? 'var(--border-strong)' : '$borderColor'}
bg={picked ? '$panel' : 'transparent'}
cursor={isOn || !may ? 'default' : 'pointer'}
$max-sm={{ minH: 44 }}
>
<XStack width={18} height={18} rounded={5} borderWidth={1} borderColor="var(--border-strong)" items="center" justify="center" bg={isOn || picked ? '$ink' : 'transparent'} shrink={0} mt={1}>
{isOn || picked ? <Check size={12} color="var(--background)" aria-hidden /> : null}
</XStack>
<YStack flex={1} minW={0} gap="$0.5" items="flex-start">
<SizableText size="$2" fontWeight="600" color="$ink" numberOfLines={1}>
{e.name}
</SizableText>
<SizableText size="$1" color="$soft" numberOfLines={2} text="start">
{e.description}
</SizableText>
</YStack>
{isOn ? (
<SizableText size="$1" color="$soft" shrink={0}>
{t('Added')}
</SizableText>
) : null}
</XStack>
)
})}
</YStack>
) : null}
{suited.length && !may ? (
<SizableText size="$2" color="$soft">
{t('An org admin adds skills. Ask them to add these.')}
</SizableText>
) : null}
{notice ? (
<SizableText size="$2" color={notice.error ? 'var(--state-error)' : '$soft'} role={notice.error ? 'alert' : 'status'}>
{notice.text}
</SizableText>
) : null}
<XStack gap="$2" items="center" flexWrap="wrap">
{suited.length ? (
<Button data-slot="roles-add" size="sm" disabled={!may || busy || !taken.length} onPress={() => void add()}>
{taken.length ? t('Add {n} skills', { n: taken.length }) : t('All added')}
</Button>
) : null}
{first ? (
<Button data-slot="roles-later" size="sm" variant="outline" borderColor="var(--border-strong)" disabled={busy} onPress={() => void later()}>
{t('Not now')}
</Button>
) : null}
</XStack>
</YStack>
</Part>
)
}
2 changes: 2 additions & 0 deletions src/customize/skills.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { active, authored, brand, document, nameOf, remove, tool, write, type Ca
import { toggle, type Tool } from '../api/tools.ts'
import { useHost, useTarget } from '../host.tsx'
import { useLoad } from './load.ts'
import { Roles } from './roles.tsx'
import { say } from './say.ts'
import { Add, Confirm, day, Empty, Failed, Field, FilterPills, Grid, Mark, matches, MenuTrigger, Part, Sheet, Soft, Tile, TilesSkeleton, type Pane, useCount, Visitor } from './ui.tsx'

Expand Down Expand Up @@ -172,6 +173,7 @@ export function Skills({ view, q, adding, onAdding, onView, onCount }: Pane) {
return (
<YStack gap="$3">
<StatusBanner notice={notice} />
{signed && !q.trim() ? <Roles /> : null}
<Part title={t('From Hanzo')} detail={t('Add one and every run in your organization can use it.')}>
{shelf.error && !shelf.value ? (
<Failed error={shelf.error} onRetry={shelf.reload} />
Expand Down
5 changes: 5 additions & 0 deletions src/host.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ export function useHost(): Host {
return h
}

/** The host, or null where none is mounted: a surface that may be drawn without one (the first run's card in Chat). */
export function useHosted(): Host | null {
return useContext(Context)
}

/** The host's way of showing a page, where one is mounted and has one. The side panel is drawn without a host too (hanzo.team's rooms). */
export function useBrowse(): Browse | null {
return useContext(Context)?.browse ?? null
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export type { Done, Folder, Local } from './local.ts'
export type { Session, Status } from './api/sessions.ts'
export { talks } from './api/talk.ts'
export { detect, type Detected } from './api/skills.ts'
export { Roles } from './customize/roles.tsx'
export { recommend, WORK } from './roles.ts'
export type { Read } from './data.ts'
export { href, parse, path, PLACES, route, SESSION, SLUG, type Route, type Screen } from './route.ts'

Expand Down
45 changes: 45 additions & 0 deletions src/roles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it } from 'vitest'

import type { Catalogue } from './api/skills.ts'
import { MOST, RECOMMENDED, recommend, WORK } from './roles.ts'

const every = (names: readonly string[]): Catalogue => ({
skills: names.map((name) => ({ name, description: `What ${name} does`, product: name.split('_')[0]! })),
products: [],
})

describe('the skills a work recommends', () => {
it('names a list for every work the account offers, and none for "something else"', () => {
for (const w of WORK) expect(w.id in RECOMMENDED, w.id).toBe(w.id !== 'other')
expect(Object.keys(RECOMMENDED).every((id) => WORK.some((w) => w.id === id))).toBe(true)
})

it('keeps each list within what one click can sensibly add, and free of repeats', () => {
for (const [id, names] of Object.entries(RECOMMENDED)) {
expect(names.length, id).toBeLessThanOrEqual(MOST)
expect(new Set(names).size, id).toBe(names.length)
}
})

it('offers the catalogue entries of the work, in the order the list gives them', () => {
const all = every(Object.values(RECOMMENDED).flat())
expect(recommend('marketing', all).map((e) => e.name)).toEqual([...RECOMMENDED.marketing!])
expect(recommend('marketing', all)[0]).toMatchObject({ description: 'What marketing_summary does', product: 'marketing' })
})

it('leaves out a skill the catalogue no longer serves, rather than offering what cannot be added', () => {
const all = every(RECOMMENDED.education!.filter((n) => n !== 'knowledge_graph'))
const names = recommend('education', all).map((e) => e.name)
expect(names).not.toContain('knowledge_graph')
expect(names).toEqual(RECOMMENDED.education!.filter((n) => n !== 'knowledge_graph'))
})

it('offers nothing for no work, an unknown one, "something else", or before the catalogue is read', () => {
const all = every(Object.values(RECOMMENDED).flat())
expect(recommend(undefined, all)).toEqual([])
expect(recommend('', all)).toEqual([])
expect(recommend('astronaut', all)).toEqual([])
expect(recommend('other', all)).toEqual([])
expect(recommend('marketing', null)).toEqual([])
})
})
52 changes: 52 additions & 0 deletions src/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* What a person's work recommends: the skills of Hanzo's catalogue that suit it,
* so a new person starts with the ones that matter to them switched on rather
* than a list of hundreds.
*
* The work is the account's own (Settings › Account, `Prefs.work`), one list for
* both. Each work names the catalogue skills that read the part of the business
* it lives in; `recommend` keeps the ones the catalogue serves today, in the
* order given, so a skill the platform renames is left out and never offered.
*/
import { phrase } from '@hanzo/i18n'

import type { Catalogue, Entry } from './api/skills.ts'

/** What a person may say they work on, each label English and marked (`phrase`), drawn through `t`. */
export const WORK = [
{ id: 'director', label: phrase('Director or executive') },
{ id: 'engineering', label: phrase('Software engineering') },
{ id: 'data', label: phrase('Data science') },
{ id: 'design', label: phrase('Design') },
{ id: 'product', label: phrase('Product management') },
{ id: 'research', label: phrase('Research') },
{ id: 'writing', label: phrase('Writing') },
{ id: 'education', label: phrase('Education') },
{ id: 'operations', label: phrase('Operations') },
{ id: 'marketing', label: phrase('Marketing and sales') },
{ id: 'other', label: phrase('Something else') },
]

/** The most skills one work recommends: a set small enough to read before it is added. */
export const MOST = 8

/** The catalogue skills each work is recommended, best first. 'other' has none: it is a way to skip. */
export const RECOMMENDED: Record<string, readonly string[]> = {
director: ['books_metrics', 'books_pnl', 'books_position', 'captable_summary', 'dataroom_documents', 'guide_suggest', 'team_members', 'billing_usage'],
engineering: ['git_repos', 'git_runs', 'code_ask', 'code_search', 'deploy_applications', 'ci_runs', 'project_deployments', 'sandbox_sandbox'],
data: ['dataset_dataset', 'dataset_lineage', 'eval_runs', 'eval_scores', 'knowledge_graph', 'metrics_query', 'experiment_experiment', 'research_runs'],
design: ['studio_documents', 'studio_templates', 'studio_brands', 'studio_flows', 'studio_runs', 'content_board', 'videos_content', 'library_library'],
product: ['project_project', 'task_board', 'task_issues', 'experiment_experiment', 'flag_defs', 'form_forms', 'guide_strategies', 'research_studies'],
research: ['research_papers', 'research_studies', 'research_evidence', 'research_experiments', 'research_compare', 'benchmark_compare', 'knowledge_files', 'websearch_search'],
writing: ['content_board', 'content_channels', 'content_lifecycle', 'translate_memory', 'library_library', 'team_docs', 'seo_rates', 'websearch_search'],
education: ['knowledge_files', 'knowledge_graph', 'library_library', 'meet_session', 'form_forms', 'content_board', 'translate_memory', 'team_docs'],
operations: ['task_board', 'team_members', 'books_inbox', 'tax_filings', 'compliance_status', 'billing_invoices', 'treasury_accounts', 'team_inbox'],
marketing: ['marketing_summary', 'marketing_campaigns', 'marketing_audiences', 'marketing_broadcasts', 'marketing_sequences', 'campaign_metrics', 'social_posts', 'ad_summary'],
}

/** The catalogue's skills that `work` recommends, in the order the list gives them, at most `MOST`. */
export function recommend(work: string | undefined, catalogue: Catalogue | null): Entry[] {
if (!work || !catalogue) return []
const served = new Map(catalogue.skills.map((e) => [e.name, e]))
return (RECOMMENDED[work] ?? []).flatMap((name) => served.get(name) ?? []).slice(0, MOST)
}
Loading
Loading