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
6 changes: 5 additions & 1 deletion apps/mobile/src/features/chats/KinProjectScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { KinPlaceLists } from "./KinPlaceLists";
import { KinDocsUnder } from "./KinDocsList";
import { KinProjectPeopleSection } from "./KinProjectPeopleScreen";
import { kinClient } from "./state/kinClient";
import { useKinAskRecord } from "./state/useKinAskRecord";
import { kinHostConnected, useKinGate } from "./state/useKinHost";
import {
useKinProjectPage,
Expand Down Expand Up @@ -212,6 +213,7 @@ function KinProjectBody(
if (outcome.problem.kind === "conflict") props.onRefresh();
}
};
const asker = useKinAskRecord(props, { key: view.key, title: view.name });
const openTask = (task: KinRecordKey) =>
openRecord("KinWorkItem", { environmentId, orgId, task });
return (
Expand All @@ -229,6 +231,7 @@ function KinProjectBody(
bar: canChange
? {
actions: [
...asker.actions,
{
accessibilityLabel: KIN_PROJECTS.edit,
icon: "pencil",
Expand All @@ -242,6 +245,7 @@ function KinProjectBody(
},
],
menus: [
...asker.menus,
{
title: "More",
icon: "ellipsis",
Expand Down Expand Up @@ -273,7 +277,7 @@ function KinProjectBody(
},
],
}
: {},
: asker,
header: (
<>
<KinProjectSummary
Expand Down
8 changes: 3 additions & 5 deletions apps/mobile/src/features/chats/KinTasksList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,14 @@ export function KinTasksList(
title={props.title}
refresh={{ accessibilityLabel: "Refresh", onPress: refresh }}
actions={[
...(agent === null
// Inside a project, the project's own bar asks about the project instead.
...(agent === null || within !== undefined
? []
: [
{
accessibilityLabel: `Ask ${agent.name}`,
icon: "bubble.left" as const,
onPress: () =>
agent.ask(
kinWorkAskDraft("tasks", within === undefined ? narrowed : props.title),
),
onPress: () => agent.ask(kinWorkAskDraft("tasks", narrowed)),
},
]),
...(add === null
Expand Down
6 changes: 5 additions & 1 deletion apps/mobile/src/features/chats/KinWorkItemScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import { KinMakeDocumentStep, type KinMakeStep } from "./KinInvoice";
import { KinWorkFrame, KinWorkLoading, KinWorkTaskRow } from "./components/KinWorkPieces";
import { KinWorkAudienceRow } from "./KinWorkShareRow";
import { kinClient } from "./state/kinClient";
import { useKinAskRecord } from "./state/useKinAskRecord";
import {
useKinWork,
useKinWorkAnswer,
Expand Down Expand Up @@ -278,6 +279,8 @@ function KinWorkItemBody(props: ItemTarget) {
const choices = useKinWorkChoices(props, hostClients && canChange);
const now = new Date(useNowMinuteMs());
const back = () => navigation.goBack();
const task = state._tag === "ready" && state.item.list === undefined ? state.item : null;
const asker = useKinAskRecord(props, task === null ? null : { key: task.key, title: task.title });

if (state._tag !== "ready") {
return (
Expand Down Expand Up @@ -648,7 +651,8 @@ function KinWorkItemBody(props: ItemTarget) {
<KinWorkFrame
title={view.title}
onBack={back}
menus={menus}
actions={asker.actions}
menus={[...asker.menus, ...menus]}
form={edit !== null}
{...(footer === undefined ? {} : { footer })}
>
Expand Down
54 changes: 54 additions & 0 deletions apps/mobile/src/features/chats/state/useKinAskRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useAtomValue } from "@effect/atom-react";
import {
KIN_ASK_RECORD,
kinAskAgents,
kinAskRecordDraft,
} from "@t3tools/client-runtime/kin/kinAskRecord";
import * as Option from "effect/Option";
import { AsyncResult } from "effect/reactivity";

import type { AppBarAction, AppBarMenu } from "../../../components/ui/AppBar";
import { kinClient } from "./kinClient";
import { useKinOpenChat } from "./useKinOpenChat";
import type { KinWorkTarget } from "./useKinWork";

/**
* Ask an agent about a task or project, as app bar items: one agent is a button that asks it, several
* are a menu to pick from, none is nothing. The chat opens with the record named in its box.
*/
export function useKinAskRecord(
{ environmentId, orgId }: KinWorkTarget,
record: { readonly key: string; readonly title: string } | null,
): { readonly actions: ReadonlyArray<AppBarAction>; readonly menus: ReadonlyArray<AppBarMenu> } {
const openChat = useKinOpenChat();
const result = useAtomValue(kinClient.leads({ environmentId, input: { orgId } }));
const agents = record === null ? [] : kinAskAgents(Option.getOrNull(AsyncResult.value(result)));
const [only] = agents;
if (record === null || only === undefined) return { actions: [], menus: [] };
const draft = kinAskRecordDraft(record);
if (agents.length === 1)
return {
actions: [
{
accessibilityLabel: KIN_ASK_RECORD.ask(only.name),
icon: "bubble.left",
onPress: () => openChat(environmentId, orgId, only.chatId, draft),
},
],
menus: [],
};
return {
actions: [],
menus: [
{
title: KIN_ASK_RECORD.askWho,
icon: "bubble.left",
items: agents.map((agent) => ({
id: `ask-${agent.agentId}`,
title: agent.name,
onPress: () => openChat(environmentId, orgId, agent.chatId, draft),
})),
},
],
};
}
63 changes: 63 additions & 0 deletions apps/web/src/kin/KinAskRecord.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useAtomValue } from "@effect/atom-react";
import {
KIN_ASK_RECORD,
kinAskAgents,
kinAskRecordDraft,
type KinAskAgent,
} from "@t3tools/client-runtime/kin/kinAskRecord";
import * as Option from "effect/Option";
import { AsyncResult } from "effect/reactivity";
import { ChevronDownIcon, MessageSquareIcon } from "lucide-react";

import { Button } from "../components/ui/button";
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../components/ui/menu";
import { kinClient } from "./kinClient";
import { useKinOpenChat } from "./useKinOpenChat";
import type { KinWorkTarget } from "./useKinWork";

/**
* Ask an agent about a task or project, with the record named in the chat's box. One agent is asked
* directly; several are picked from a menu. Nothing shows when the viewer can message no agent.
*/
export function KinAskRecord(
props: KinWorkTarget & {
readonly record: { readonly key: string; readonly title: string };
readonly disabled?: boolean;
readonly onOpen?: () => void;
},
) {
const { environmentId, orgId } = props;
const openChat = useKinOpenChat();
const result = useAtomValue(kinClient.leads({ environmentId, input: { orgId } }));
const agents = kinAskAgents(Option.getOrNull(AsyncResult.value(result)));
const ask = (agent: KinAskAgent) => {
props.onOpen?.();
openChat(environmentId, orgId, agent.chatId, kinAskRecordDraft(props.record));
};
const [only] = agents;
if (only === undefined) return null;
if (agents.length === 1)
return (
<Button size="sm" variant="outline" disabled={props.disabled} onClick={() => ask(only)}>
<MessageSquareIcon />
{/* Agent names run to 80 characters; a long one must not push the header off the screen. */}
<span className="max-w-40 truncate">{KIN_ASK_RECORD.ask(only.name)}</span>
</Button>
);
return (
<Menu>
<MenuTrigger render={<Button size="sm" variant="outline" disabled={props.disabled} />}>
<MessageSquareIcon />
{KIN_ASK_RECORD.askWho}
<ChevronDownIcon />
</MenuTrigger>
<MenuPopup align="end">
{agents.map((agent) => (
<MenuItem key={agent.agentId} onClick={() => ask(agent)}>
{agent.name}
</MenuItem>
))}
</MenuPopup>
</Menu>
);
}
16 changes: 12 additions & 4 deletions apps/web/src/kin/KinWorkItemSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ import { KinWorkAudienceRow } from "./KinWorkShareRow";
import { KinWorkRow } from "./KinWorkTasks";
import { KinWorkSection as Section } from "./KinWorkParts";
import { KinWorkStateIcon } from "./KinWorkStateIcon";
import { KinAskRecord } from "./KinAskRecord";
import { kinClient } from "./kinClient";
import { openDestination } from "./KinNotifications";
import { useNowMinuteMs } from "./useKinClock";
Expand Down Expand Up @@ -569,8 +570,15 @@ function KinWorkItem(
{KIN_WORK_COPY.saving}
</span>
) : null}
{!more ? null : (
<div className="ms-auto">
<div className="ms-auto flex items-center gap-1">
<KinAskRecord
environmentId={environmentId}
orgId={orgId}
record={{ key: view.key, title: view.title }}
disabled={saving}
onOpen={props.onClose}
/>
{!more ? null : (
<Menu>
<MenuTrigger
render={
Expand Down Expand Up @@ -643,8 +651,8 @@ function KinWorkItem(
)}
</MenuPopup>
</Menu>
</div>
)}
)}
</div>
</div>
{edit !== null ? null : <Frame.Title>{view.title}</Frame.Title>}
{view.remarks.map((remark) => (
Expand Down
86 changes: 50 additions & 36 deletions apps/web/src/kin/KinWorkProjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
} from "../components/ui/sheet";
import { Switch } from "../components/ui/switch";
import { useAtomCommand } from "../state/use-atom-command";
import { KinAskRecord } from "./KinAskRecord";
import { KinAttentionSection } from "./KinAttention";
import { KinDocsUnder } from "./KinDocsList";
import { KinProjectNoteSection } from "./KinProjectNote";
Expand Down Expand Up @@ -172,8 +173,8 @@ function ProjectPage(
const summary = (
<div className="flex flex-col gap-6 pb-3">
<div className="flex flex-col gap-2">
<div className="flex items-start gap-2">
<div className="flex min-w-0 flex-1 flex-col gap-1">
<div className="flex flex-wrap items-start gap-2">
<div className="flex min-w-56 flex-1 flex-col gap-1">
<div className="flex min-w-0 flex-wrap items-center gap-2">
<h2 className="truncate text-lg font-medium text-foreground">{view.name}</h2>
{view.health === null ? null : <KinWorkHealthBadge health={view.health} />}
Expand Down Expand Up @@ -205,42 +206,55 @@ function ProjectPage(
<p className="text-sm text-muted-foreground">{view.terms}</p>
)}
</div>
{canChange ? (
<div className="flex shrink-0 items-center gap-1">
<Button size="sm" variant="outline" disabled={busy} onClick={() => setEditing(true)}>
<PencilIcon />
{KIN_PROJECTS.edit}
</Button>
<Menu>
<MenuTrigger
render={
<Button size="icon-sm" variant="ghost" aria-label="More" disabled={busy} />
}
<div className="flex shrink-0 items-center gap-1">
<KinAskRecord
environmentId={environmentId}
orgId={orgId}
record={{ key: view.key, title: view.name }}
disabled={busy}
/>
{canChange ? (
<>
<Button
size="sm"
variant="outline"
disabled={busy}
onClick={() => setEditing(true)}
>
<EllipsisIcon />
</MenuTrigger>
<MenuPopup align="end">
{view.closed === null ? (
<>
<MenuItem onClick={() => void change({ state: "done" })}>
{KIN_PROJECTS.done}
</MenuItem>
<MenuItem
variant="destructive"
onClick={() => void change({ state: "dropped" })}
>
{KIN_PROJECTS.drop}
<PencilIcon />
{KIN_PROJECTS.edit}
</Button>
<Menu>
<MenuTrigger
render={
<Button size="icon-sm" variant="ghost" aria-label="More" disabled={busy} />
}
>
<EllipsisIcon />
</MenuTrigger>
<MenuPopup align="end">
{view.closed === null ? (
<>
<MenuItem onClick={() => void change({ state: "done" })}>
{KIN_PROJECTS.done}
</MenuItem>
<MenuItem
variant="destructive"
onClick={() => void change({ state: "dropped" })}
>
{KIN_PROJECTS.drop}
</MenuItem>
</>
) : (
<MenuItem onClick={() => void change({ state: "open" })}>
{KIN_PROJECTS.reopen}
</MenuItem>
</>
) : (
<MenuItem onClick={() => void change({ state: "open" })}>
{KIN_PROJECTS.reopen}
</MenuItem>
)}
</MenuPopup>
</Menu>
</div>
) : null}
)}
</MenuPopup>
</Menu>
</>
) : null}
</div>
</div>
{view.why.length === 0 ? null : (
<p className="text-sm text-foreground">{view.why.join(" ")}</p>
Expand Down
4 changes: 4 additions & 0 deletions packages/client-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@
"types": "./src/kin/kinAnswers.ts",
"default": "./src/kin/kinAnswers.ts"
},
"./kin/kinAskRecord": {
"types": "./src/kin/kinAskRecord.ts",
"default": "./src/kin/kinAskRecord.ts"
},
"./kin/kinAttention": {
"types": "./src/kin/kinAttention.ts",
"default": "./src/kin/kinAttention.ts"
Expand Down
35 changes: 35 additions & 0 deletions packages/client-runtime/src/kin/kinAskRecord.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { KinChatId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import { kinAskAgents, kinAskRecordDraft } from "./kinAskRecord.ts";

describe("kinAskRecordDraft", () => {
it.each([
["a project", { key: "P-3", title: "Website relaunch" }, "About P-3 (Website relaunch): "],
["a record with no name", { key: "P-4", title: " " }, "About P-4: "],
])("names %s by key and name", (_name, record, expected) => {
expect(kinAskRecordDraft(record)).toBe(expected);
});
});

describe("kinAskAgents", () => {
const lead = (agentId: string, retired?: true) => ({
agentId,
name: agentId,
ownerChatId: KinChatId.make(`own-${agentId}`),
...(retired ? { retired: { at: "2026-01-01T00:00:00.000Z" } } : {}),
});
const contact = (agentId: string) => ({
agentId,
name: agentId,
chatId: KinChatId.make(`mine-${agentId}`),
});

it("reaches live own agents and teammates' agents in the viewer's own chats", () => {
const result = {
leads: [lead("ops"), lead("old", true)],
contacts: [contact("ops"), contact("sales")],
};
expect(kinAskAgents(result).map((agent) => agent.chatId)).toEqual(["own-ops", "mine-sales"]);
});
});
Loading