@yaks/inbox 0.0.0-stage → 0.2.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/LICENSE +202 -0
  2. package/Person.tsx +275 -0
  3. package/Project.tsx +83 -0
  4. package/README.md +311 -2
  5. package/_dist/actions-browser.d.ts +8 -0
  6. package/_dist/actions-browser.d.ts.map +1 -0
  7. package/_dist/actions.d.ts +22 -0
  8. package/_dist/actions.d.ts.map +1 -0
  9. package/_dist/asks.d.ts +15 -0
  10. package/_dist/asks.d.ts.map +1 -0
  11. package/_dist/graph.d.ts +12 -0
  12. package/_dist/graph.d.ts.map +1 -0
  13. package/_dist/letters.d.ts +24 -0
  14. package/_dist/letters.d.ts.map +1 -0
  15. package/_dist/mail.d.ts +4 -0
  16. package/_dist/mail.d.ts.map +1 -0
  17. package/_dist/mod.d.ts +7 -0
  18. package/_dist/mod.d.ts.map +1 -0
  19. package/_dist/options.d.ts +18 -0
  20. package/_dist/options.d.ts.map +1 -0
  21. package/_dist/queries.d.ts +23 -0
  22. package/_dist/queries.d.ts.map +1 -0
  23. package/_dist/read.d.ts +18 -0
  24. package/_dist/read.d.ts.map +1 -0
  25. package/_dist/reader.d.ts +25 -0
  26. package/_dist/reader.d.ts.map +1 -0
  27. package/_dist/service.d.ts +8 -0
  28. package/_dist/service.d.ts.map +1 -0
  29. package/_dist/threads.d.ts +33 -0
  30. package/_dist/threads.d.ts.map +1 -0
  31. package/_dist/tools.d.ts +12 -0
  32. package/_dist/tools.d.ts.map +1 -0
  33. package/_dist/vocab.d.ts +5 -0
  34. package/_dist/vocab.d.ts.map +1 -0
  35. package/actions-browser.js +165 -0
  36. package/actions-browser.js.map +1 -0
  37. package/actions-browser.ts +158 -0
  38. package/actions.js +34 -0
  39. package/actions.js.map +1 -0
  40. package/actions.ts +53 -0
  41. package/asks.js +76 -0
  42. package/asks.js.map +1 -0
  43. package/asks.ts +94 -0
  44. package/deno.json +1 -0
  45. package/front.js +41 -0
  46. package/front.js.map +1 -0
  47. package/front.json +68 -0
  48. package/front.ts +38 -0
  49. package/graph.js +176 -0
  50. package/graph.js.map +1 -0
  51. package/graph.ts +182 -0
  52. package/inspect.tsx +22 -0
  53. package/letters.js +233 -0
  54. package/letters.js.map +1 -0
  55. package/letters.ts +325 -0
  56. package/mail.js +122 -0
  57. package/mail.js.map +1 -0
  58. package/mail.ts +148 -0
  59. package/mod.js +11 -0
  60. package/mod.js.map +1 -0
  61. package/mod.ts +24 -0
  62. package/options.js +15 -0
  63. package/options.js.map +1 -0
  64. package/options.ts +42 -0
  65. package/package.json +76 -4
  66. package/queries.js +240 -0
  67. package/queries.js.map +1 -0
  68. package/queries.ts +302 -0
  69. package/read.js +453 -0
  70. package/read.js.map +1 -0
  71. package/read.ts +476 -0
  72. package/reader.js +105 -0
  73. package/reader.js.map +1 -0
  74. package/reader.ts +147 -0
  75. package/service.js +29 -0
  76. package/service.js.map +1 -0
  77. package/service.ts +45 -0
  78. package/threads.js +182 -0
  79. package/threads.js.map +1 -0
  80. package/threads.ts +276 -0
  81. package/tools.js +101 -0
  82. package/tools.js.map +1 -0
  83. package/tools.ts +103 -0
  84. package/views.tsx +30 -0
  85. package/vocab.js +12 -0
  86. package/vocab.js.map +1 -0
  87. package/vocab.json +242 -0
  88. package/vocab.ts +11 -0
package/threads.ts ADDED
@@ -0,0 +1,276 @@
1
+ import { addressed, type Reader, type Row } from './reader.js'
2
+ import { threadOf } from '@yaks/notify'
3
+
4
+ export const lanes = ['Needs you', 'Replies', 'Updates', 'Recent'] as const
5
+ export type Lane = typeof lanes[number]
6
+ export type Direction = 'said' | 'received'
7
+ export type Search = {
8
+ text?: string
9
+ direction?: Direction
10
+ all?: boolean
11
+ lane?: Lane
12
+ /** Only the threads of candidates holding one of these components, so a
13
+ * door that shows only tasks and decisions never reads a conversation. */
14
+ within?: string[]
15
+ }
16
+ export type Thread<R extends Row = Row> = {
17
+ eid: string
18
+ row: R
19
+ latest: R
20
+ messages: R[]
21
+ lane: Lane
22
+ reason: string
23
+ blocking: boolean
24
+ at: string
25
+ unread: boolean
26
+ /** Its reader put it away, and nothing has happened on it since; only a
27
+ * read of every thread (`Search.all`) holds it. */
28
+ archived: boolean
29
+ }
30
+
31
+ let str = (v: unknown): string => String(v ?? '')
32
+ let newest = (...times: unknown[]) => {
33
+ let latest = ''
34
+ for (let time of times) {
35
+ let value = str(time)
36
+ if (value > latest) latest = value
37
+ }
38
+ return latest
39
+ }
40
+ let closed = (r: Row) => !!(r.comps.completed || r.comps.cancelled)
41
+ let states = [
42
+ 'completed',
43
+ 'cancelled',
44
+ 'failed',
45
+ 'blocked',
46
+ 'broken',
47
+ 'resolved',
48
+ 'regressed',
49
+ 'decided',
50
+ 'exit',
51
+ ]
52
+ export let stateAt = (r: Row): string =>
53
+ newest(r.comps.blocked?.since, ...states.map((s) => r.comps[s]?.at))
54
+ export let messageAt = (r: Row): string =>
55
+ newest(r.comps.created?.at, r.comps.mail?.at, r.comps.commit?.at)
56
+ /** Reading, archiving, delivery and arbitrary edits are not new activity. */
57
+ export let activityAt = (r: Row): string =>
58
+ newest(messageAt(r), stateAt(r), r.comps.bug?.last, r.comps.bug?.first)
59
+ export { threadOf } from '@yaks/notify'
60
+
61
+ export let saidBy = (who: Reader, r: Row, byId?: Map<string, Row>): boolean => {
62
+ if (r.comps.mail) return !!who.addrs?.has(str(r.comps.mail.from))
63
+ if (r.comps.entry) {
64
+ return !!who.actor &&
65
+ (r.comps.created?.by == who.actor ||
66
+ byId?.get(str(r.comps.entry.session))?.comps.session?.actor ==
67
+ who.actor) &&
68
+ !!r.comps.content &&
69
+ ![
70
+ 'output',
71
+ 'prompt',
72
+ 'notice',
73
+ 'reasoning',
74
+ 'result',
75
+ 'exception',
76
+ 'refusal',
77
+ 'ask',
78
+ 'call',
79
+ ].some((c) => !!r.comps[c])
80
+ }
81
+ return !!who.actor && r.comps.created?.by == who.actor
82
+ }
83
+
84
+ let words = (r: Row) =>
85
+ [
86
+ r.comps.doc?.title,
87
+ r.comps.doc?.body,
88
+ r.comps.content?.body,
89
+ r.comps.bug?.title,
90
+ r.comps.decision?.question,
91
+ ].map(str).join('\n').toLocaleLowerCase()
92
+ /** Follow reply ancestry, not every branch on an entity the person spoke on. */
93
+ let answers = (who: Reader, r: Row, byId: Map<string, Row>): boolean => {
94
+ let seen = new Set<string>([r.eid])
95
+ let ref = str(r.comps.comment?.reply_to ?? r.comps.mail?.reply_to)
96
+ while (ref && !seen.has(ref)) {
97
+ seen.add(ref)
98
+ let parent = byId.get(ref)
99
+ if (!parent || threadOf(parent, who, byId) != threadOf(r, who, byId)) {
100
+ return false
101
+ }
102
+ if (saidBy(who, parent, byId)) return true
103
+ ref = str(parent.comps.comment?.reply_to ?? parent.comps.mail?.reply_to)
104
+ }
105
+ return false
106
+ }
107
+
108
+ /** One row per thread, with lane precedence and blocking work first. */
109
+ export let threads = <R extends Row>(
110
+ all: R[],
111
+ who: Reader,
112
+ search: Search = {},
113
+ ): Thread<R>[] => {
114
+ let records = new Map(
115
+ all.filter((r) => !r.comps.mail_notice).map((r) => [r.eid, r]),
116
+ )
117
+ let byId = new Map(records)
118
+ for (let r of records.values()) {
119
+ for (let id of [r.comps.mail?.message_id, r.comps.notified?.message_id]) {
120
+ if (id) byId.set(str(id), r)
121
+ }
122
+ }
123
+ let groups = new Map<string, R[]>()
124
+ for (let r of records.values()) {
125
+ if (r.comps.edge || r.comps.subscription) continue
126
+ let eid = threadOf(r, who, byId)
127
+ let group = groups.get(eid) ?? []
128
+ group.push(r)
129
+ groups.set(eid, group)
130
+ }
131
+ let to = addressed(who)
132
+ let text = (search.text ?? '').trim().toLocaleLowerCase()
133
+ let out: Thread<R>[] = []
134
+ for (let [eid, group] of groups) {
135
+ if (who.muting?.has(eid)) continue
136
+ let root = byId.get(eid) ?? group[0]
137
+ let messages = group.filter((r) =>
138
+ r.comps.comment || (r.comps.mail && !r.comps.notification) ||
139
+ (r.comps.entry &&
140
+ (saidBy(who, r, byId) || (r.comps.output && !r.comps.reasoning)))
141
+ )
142
+ // A thread opens with its root: a reply said in the same millisecond
143
+ // as what it answers still follows it.
144
+ .sort((a, b) =>
145
+ messageAt(a).localeCompare(messageAt(b)) ||
146
+ +(a.eid != eid) - +(b.eid != eid) || a.eid.localeCompare(b.eid)
147
+ )
148
+ let own = messages.filter((r) => saidBy(who, r, byId))
149
+ let started = saidBy(who, root) ||
150
+ (!!who.actor && root.comps.session?.actor == who.actor)
151
+ let watched = !!who.watching?.has(eid) || !!who.claims?.has(eid)
152
+ let received = messages.filter((r) => !saidBy(who, r, byId))
153
+ let reply = received.filter((r) =>
154
+ answers(who, r, byId) || to(r) ||
155
+ (!!root.comps.conversation && started &&
156
+ r.comps.comment?.target == eid) ||
157
+ (!!r.comps.entry?.session && started && !!r.comps.output)
158
+ )
159
+ // A notification tells its reader what happened, unless they did it.
160
+ let told = !!root.comps.notification && to(root) && !started
161
+ let direct = group.some((r) => !r.comps.notification && to(r)) ||
162
+ (!root.comps.notification && root.comps.mail &&
163
+ root.comps.deliver?.to == who.actor)
164
+ let assigned = !!who.actor && root.comps.filed?.assignee == who.actor
165
+ let alert = !!root.comps.bug && !root.comps.resolved &&
166
+ (who.operator == true) &&
167
+ (!who.scope || !root.comps.bug.app || root.comps.bug.app == who.scope ||
168
+ watched || assigned)
169
+ let decision = !!root.comps.decision && !!who.actor &&
170
+ who.operator == true &&
171
+ (assigned ||
172
+ (!root.comps.filed?.assignee &&
173
+ (!who.scope || root.comps.filed?.project == who.scope || watched)))
174
+ let need =
175
+ !!(!root.comps.archived && !closed(root) && !root.comps.decided &&
176
+ ((assigned && !!root.comps.task) || decision || alert ||
177
+ (direct && group.some((r) => !!r.comps.knock))))
178
+ let changes = group.filter((r) =>
179
+ r.comps.commit ||
180
+ (r.comps.entry && (r.comps.exception || r.comps.refusal || r.comps.stop))
181
+ )
182
+ let update = told ||
183
+ (started || watched) && (!!stateAt(root) || !!changes.length)
184
+ let visible = [
185
+ 'doc',
186
+ 'task',
187
+ 'session',
188
+ 'design',
189
+ 'bug',
190
+ 'project',
191
+ 'mail',
192
+ 'decision',
193
+ ].some((name) => !!root.comps[name])
194
+ let recent = (started && visible) || !!own.length ||
195
+ (!!who.actor &&
196
+ (!!who.attention?.get(eid)?.comps.opened ||
197
+ root.comps.decided?.by == who.actor))
198
+ if (!need && !reply.length && !update && !recent && !direct) continue
199
+ let activity = need
200
+ ? group
201
+ : [root, ...own, ...reply, ...(started || watched ? changes : [])]
202
+ let at = newest(...activity.map(activityAt))
203
+ let personal = who.attention?.get(eid)?.comps ?? {}
204
+ let away = str(personal.archived?.at)
205
+ let archived = !!(root.comps.notification && root.comps.archived) ||
206
+ !!personal.archived && (!away || at <= away)
207
+ if (!search.all && archived) continue
208
+ // A decision's question and answer can have different authors. Treat the
209
+ // answer as its own words rather than attributing the whole root to them.
210
+ if (search.direction || text) {
211
+ let searchable = group.flatMap((r) => {
212
+ let mine = r == root ? started : saidBy(who, r, byId)
213
+ let includes = (said: boolean) =>
214
+ !search.direction || said == (search.direction == 'said')
215
+ return [
216
+ ...(includes(mine) ? [words(r)] : []),
217
+ ...(r.comps.decided && includes(r.comps.decided.by == who.actor)
218
+ ? [str(r.comps.decided.choice).toLocaleLowerCase()]
219
+ : []),
220
+ ]
221
+ })
222
+ if (!searchable.some((words) => !text || words.includes(text))) continue
223
+ }
224
+ let blocking = need &&
225
+ all.some((edge) =>
226
+ edge.comps.requires && edge.comps.edge?.to == eid &&
227
+ !!byId.get(str(edge.comps.edge.from))?.comps.task &&
228
+ !closed(byId.get(str(edge.comps.edge.from))!)
229
+ )
230
+ let lane: Lane = need
231
+ ? 'Needs you'
232
+ : reply.length || direct
233
+ ? 'Replies'
234
+ : update
235
+ ? 'Updates'
236
+ : 'Recent'
237
+ let latest = changes.concat(messages).sort((a, b) =>
238
+ activityAt(a).localeCompare(activityAt(b))
239
+ ).at(-1) ?? root
240
+ let attention = newest(
241
+ personal.opened?.at,
242
+ root.comps.decided?.at,
243
+ started ? messageAt(root) : '',
244
+ ...own.map(messageAt),
245
+ )
246
+ if (search.lane && lane != search.lane) {
247
+ continue
248
+ }
249
+ out.push({
250
+ eid,
251
+ row: root,
252
+ latest,
253
+ messages,
254
+ lane,
255
+ reason: need
256
+ ? alert ? 'alert' : root.comps.decision ? 'decision' : 'assigned to you'
257
+ : lane == 'Replies'
258
+ ? 'reply to your words'
259
+ : told
260
+ ? 'told you what happened'
261
+ : lane == 'Updates'
262
+ ? 'work changed state'
263
+ : 'had your attention',
264
+ blocking,
265
+ at,
266
+ unread: at > attention,
267
+ archived,
268
+ })
269
+ }
270
+ return out.sort((a, b) =>
271
+ Number(b.blocking) - Number(a.blocking) || b.at.localeCompare(a.at) ||
272
+ a.eid.localeCompare(b.eid)
273
+ )
274
+ }
275
+
276
+ export { attention } from '@yaks/notify'
package/tools.js ADDED
@@ -0,0 +1,101 @@
1
+ /** The inbox's tools. `inbox_new` returns rows the tool runner writes as the
2
+ * caller; it performs no I/O and neither starts nor feeds a session.
3
+ * `inbox_list` reads one row per thread through the shared policy, and
4
+ * `inbox_archive` puts a thread away until qualifying new activity, refreshing
5
+ * the mark atomically so a repeated archive gets a fresh time.
6
+ */ import { argsOf, who } from "@yaks/graph/call";
7
+ import { Refused } from "@yaks/graph/errors";
8
+ import { CallError } from "@yaks/tools/errors";
9
+ import { readInbox } from "./read.js";
10
+ import { ancestry, markAttention } from "@yaks/notify/attention";
11
+ import { readerAt } from "./reader.js";
12
+ import { threadOf } from "./threads.js";
13
+ /** How many threads a list returns when the caller gave no limit. */ export let PAGE = 50;
14
+ let str = (v)=>v == null ? '' : String(v);
15
+ let rows = (bundles)=>bundles.map((b)=>({
16
+ eid: b.entity.eid,
17
+ comps: Object.fromEntries(Object.entries(b).filter(([k, v])=>k != 'entity' && v && typeof v == 'object'))
18
+ }));
19
+ /** Whose inbox this is: the entity the arguments named, else whoever is
20
+ * asking. */ export let reader = (call)=>{
21
+ let asked = argsOf(call).who;
22
+ if (asked != null) return str(asked);
23
+ let me = who(call)?.by;
24
+ if (!me) throw new CallError('who', 'nobody is asking — say --who');
25
+ return me;
26
+ };
27
+ // The thread a letter belongs to for whoever is asking: up its `reply_to`
28
+ // chain, then the shared policy's thread of it.
29
+ let threadAt = async (graph, call, letter)=>{
30
+ let actor = reader(call);
31
+ let person = readerAt(rows(await graph.get([
32
+ actor
33
+ ])), actor);
34
+ let chain = await ancestry(graph, letter);
35
+ let all = rows(chain);
36
+ let byId = new Map(all.map((r)=>[
37
+ r.eid,
38
+ r
39
+ ]));
40
+ for (let r of all){
41
+ for (let id of [
42
+ r.comps.mail?.message_id,
43
+ r.comps.notified?.message_id
44
+ ]){
45
+ if (id) byId.set(str(id), r);
46
+ }
47
+ }
48
+ return threadOf(all[0], person, byId);
49
+ };
50
+ let mark = async (call, graph, how)=>{
51
+ let item = str(argsOf(call).item);
52
+ let [found] = await graph.get([
53
+ item
54
+ ]);
55
+ let target = found ? await threadAt(graph, call, found) : item;
56
+ return markAttention(graph, reader(call), target, how);
57
+ };
58
+ /** Implementations of the tools declared in the inbox vocabulary. */ export let runs = ()=>({
59
+ inbox_new: (call)=>{
60
+ let text = argsOf(call).text;
61
+ if (typeof text != 'string' || !text.trim()) {
62
+ throw new Refused('a conversation needs your words — say text');
63
+ }
64
+ return [
65
+ {
66
+ entity: {
67
+ eid: '$conversation'
68
+ },
69
+ conversation: {},
70
+ doc: {
71
+ title: text.split(/\r?\n/, 1)[0],
72
+ body: text
73
+ }
74
+ }
75
+ ];
76
+ },
77
+ inbox_list: async (call, graph)=>{
78
+ let args = argsOf(call);
79
+ let found = await readInbox(graph, graph.vocab, reader(call), {
80
+ all: !!args.all,
81
+ text: str(args.search),
82
+ direction: args.direction,
83
+ lane: args.lane
84
+ });
85
+ let n = args.limit == null ? PAGE : Number(args.limit);
86
+ let page = found.slice(0, n);
87
+ let entities = new Map((await graph.get(page.map((t)=>t.eid), [])).map((b)=>[
88
+ b.entity.eid,
89
+ b.entity
90
+ ]));
91
+ return page.map((t)=>({
92
+ entity: entities.get(t.eid) ?? {
93
+ eid: t.eid
94
+ },
95
+ ...t.row.comps
96
+ }));
97
+ },
98
+ inbox_archive: (call, graph)=>mark(call, graph, 'archived'),
99
+ inbox_open: (call, graph)=>mark(call, graph, 'opened')
100
+ });
101
+ //# sourceMappingURL=tools.js.map
package/tools.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"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"}
package/tools.ts ADDED
@@ -0,0 +1,103 @@
1
+ /** The inbox's tools. `inbox_new` returns rows the tool runner writes as the
2
+ * caller; it performs no I/O and neither starts nor feeds a session.
3
+ * `inbox_list` reads one row per thread through the shared policy, and
4
+ * `inbox_archive` puts a thread away until qualifying new activity, refreshing
5
+ * the mark atomically so a repeated archive gets a fresh time.
6
+ */
7
+ import type { Bundle, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
8
+ import { argsOf, who } from '@yaks/graph/call'
9
+ import { Refused } from '@yaks/graph/errors'
10
+ import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
11
+ import { CallError } from '@yaks/tools/errors'
12
+ import { readInbox } from './read.js'
13
+ import { ancestry, markAttention } from '@yaks/notify/attention'
14
+ import { readerAt, type Row } from './reader.js'
15
+ import { type Direction, type Lane, threadOf } from './threads.js'
16
+
17
+ /** How many threads a list returns when the caller gave no limit. */
18
+ export let PAGE = 50
19
+
20
+ let str = (v: unknown): string => v == null ? '' : String(v)
21
+
22
+ let rows = (bundles: Bundle[]): Row[] =>
23
+ bundles.map((b) => ({
24
+ eid: b.entity.eid,
25
+ comps: Object.fromEntries(
26
+ Object.entries(b).filter(([k, v]) =>
27
+ k != 'entity' && v && typeof v == 'object'
28
+ ),
29
+ ) as Row['comps'],
30
+ }))
31
+
32
+ /** Whose inbox this is: the entity the arguments named, else whoever is
33
+ * asking. */
34
+ export let reader = (call: Bundle): Eid => {
35
+ let asked = argsOf(call).who
36
+ if (asked != null) return str(asked)
37
+ let me = who(call)?.by
38
+ if (!me) throw new CallError('who', 'nobody is asking — say --who')
39
+ return me
40
+ }
41
+
42
+ // The thread a letter belongs to for whoever is asking: up its `reply_to`
43
+ // chain, then the shared policy's thread of it.
44
+ let threadAt = async (
45
+ graph: Graph,
46
+ call: Bundle,
47
+ letter: Bundle,
48
+ ): Promise<Eid> => {
49
+ let actor = reader(call)
50
+ let person = readerAt(rows(await graph.get([actor])), actor)
51
+ let chain = await ancestry(graph, letter)
52
+ let all = rows(chain)
53
+ let byId = new Map(all.map((r) => [r.eid, r]))
54
+ for (let r of all) {
55
+ for (let id of [r.comps.mail?.message_id, r.comps.notified?.message_id]) {
56
+ if (id) byId.set(str(id), r)
57
+ }
58
+ }
59
+ return threadOf(all[0], person, byId)
60
+ }
61
+
62
+ let mark = async (call: Bundle, graph: Graph, how: 'opened' | 'archived') => {
63
+ let item = str(argsOf(call).item)
64
+ let [found] = await graph.get([item])
65
+ let target = found ? await threadAt(graph, call, found) : item
66
+ return markAttention(graph, reader(call), target, how)
67
+ }
68
+
69
+ /** Implementations of the tools declared in the inbox vocabulary. */
70
+ export let runs = (): Runs => ({
71
+ inbox_new: (call): Bundle[] => {
72
+ let text = argsOf(call).text
73
+ if (typeof text != 'string' || !text.trim()) {
74
+ throw new Refused('a conversation needs your words — say text')
75
+ }
76
+ return [{
77
+ entity: { eid: '$conversation' },
78
+ conversation: {},
79
+ doc: { title: text.split(/\r?\n/, 1)[0], body: text },
80
+ }]
81
+ },
82
+
83
+ inbox_list: async (call, graph): Promise<Bundle[]> => {
84
+ let args = argsOf(call)
85
+ let found = await readInbox(graph, graph.vocab, reader(call), {
86
+ all: !!args.all,
87
+ text: str(args.search),
88
+ direction: args.direction as Direction | undefined,
89
+ lane: args.lane as Lane | undefined,
90
+ })
91
+ let n = args.limit == null ? PAGE : Number(args.limit)
92
+ let page = found.slice(0, n)
93
+ let entities = new Map((await graph.get(page.map((t) => t.eid), []))
94
+ .map((b) => [b.entity.eid, b.entity]))
95
+ return page.map((t) => ({
96
+ entity: entities.get(t.eid) ?? { eid: t.eid },
97
+ ...t.row.comps,
98
+ }))
99
+ },
100
+
101
+ inbox_archive: (call, graph) => mark(call, graph, 'archived'),
102
+ inbox_open: (call, graph) => mark(call, graph, 'opened'),
103
+ })
package/views.tsx ADDED
@@ -0,0 +1,30 @@
1
+ /** @jsxRuntime automatic *//** @jsxImportSourceTypes npm:react@19.3.0 *//** @jsxImportSource npm:react@19.3.0 */
2
+ /**
3
+ * What the inbox offers a browsing app, as a `./views` facet: the `Inbox`
4
+ * view of a person (./Person.tsx) and of a project (./Project.tsx), written
5
+ * against @yaks/inspect's `io` so any host that draws inspector views draws
6
+ * them; the owner's home page and an `Inbox` tab on what it draws, each
7
+ * saying how many threads wait unread there; and the glyph they wear. A
8
+ * config that leaves @yaks/inbox out loads none of it, and no page asks for
9
+ * an inbox summary.
10
+ *
11
+ * @module
12
+ */
13
+
14
+ // @ts-types="@yaks/ui/lucide"
15
+ import Glyph from 'npm:lucide@1.31.0/dist/esm/icons/inbox.mjs'
16
+ import type { View } from 'jsr:@yaks/inspect@0.2.36'
17
+ import { waiting } from './asks.ts'
18
+
19
+ /** A person's inbox and a project's, loaded by the inspector that draws them. */
20
+ export let inspectViews = (): Promise<View[]> =>
21
+ import('./inspect.tsx').then((m) => m.inspectViews)
22
+
23
+ /** `Inbox` as a tab on each entity it draws, wearing what waits there. */
24
+ export let tabs = [{ view: 'Inbox', icon: 'inbox', waiting }]
25
+
26
+ /** The owner's home page: their inbox. */
27
+ export let home = { name: 'Inbox', icon: 'inbox', view: 'Inbox', waiting }
28
+
29
+ /** The glyph the tab and the home page wear. */
30
+ export let icons = { inbox: Glyph }
package/vocab.js ADDED
@@ -0,0 +1,12 @@
1
+ /** Inbox declarations shared by hosts and browser readers. They carry no I/O. */ import doc from './vocab.json' with {
2
+ type: 'json'
3
+ };
4
+ import manifest from './package.json' with {
5
+ type: 'json'
6
+ };
7
+ /** The conversation mark and the inbox_new tool declaration. */ export let inboxDoc = doc;
8
+ /** The package description, from its manifest. */ export let description = manifest.description;
9
+ /** Every vocabulary document this package declares. */ export let docs = [
10
+ inboxDoc
11
+ ];
12
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,+EAA+E,GAE/E,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,8DAA8D,GAC9D,OAAO,IAAI,WAAqB,IAAe;AAC/C,gDAAgD,GAChD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AACrD,qDAAqD,GACrD,OAAO,IAAI,OAAmB;EAAC;CAAS,CAAA"}