@yaks/phone 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 (81) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +251 -2
  3. package/_dist/call.d.ts +37 -0
  4. package/_dist/call.d.ts.map +1 -0
  5. package/_dist/effects.d.ts +5 -0
  6. package/_dist/effects.d.ts.map +1 -0
  7. package/_dist/mobile.d.ts +41 -0
  8. package/_dist/mobile.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +27 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/numbers.d.ts +150 -0
  12. package/_dist/numbers.d.ts.map +1 -0
  13. package/_dist/options.d.ts +21 -0
  14. package/_dist/options.d.ts.map +1 -0
  15. package/_dist/plugin.d.ts +54 -0
  16. package/_dist/plugin.d.ts.map +1 -0
  17. package/_dist/provider.d.ts +72 -0
  18. package/_dist/provider.d.ts.map +1 -0
  19. package/_dist/received.d.ts +24 -0
  20. package/_dist/received.d.ts.map +1 -0
  21. package/_dist/replies.d.ts +18 -0
  22. package/_dist/replies.d.ts.map +1 -0
  23. package/_dist/send.d.ts +16 -0
  24. package/_dist/send.d.ts.map +1 -0
  25. package/_dist/stash.d.ts +19 -0
  26. package/_dist/stash.d.ts.map +1 -0
  27. package/_dist/text.d.ts +102 -0
  28. package/_dist/text.d.ts.map +1 -0
  29. package/_dist/tools.d.ts +3 -0
  30. package/_dist/tools.d.ts.map +1 -0
  31. package/_dist/vocab.d.ts +8 -0
  32. package/_dist/vocab.d.ts.map +1 -0
  33. package/call.js +5 -0
  34. package/call.js.map +1 -0
  35. package/call.ts +53 -0
  36. package/deno.json +1 -0
  37. package/effects.js +30 -0
  38. package/effects.js.map +1 -0
  39. package/effects.ts +35 -0
  40. package/mobile.js +75 -0
  41. package/mobile.js.map +1 -0
  42. package/mobile.ts +72 -0
  43. package/mod.js +28 -0
  44. package/mod.js.map +1 -0
  45. package/mod.ts +78 -0
  46. package/numbers.js +72 -0
  47. package/numbers.js.map +1 -0
  48. package/numbers.ts +259 -0
  49. package/options.js +3 -0
  50. package/options.js.map +1 -0
  51. package/options.ts +23 -0
  52. package/package.json +44 -4
  53. package/plugin.js +155 -0
  54. package/plugin.js.map +1 -0
  55. package/plugin.ts +226 -0
  56. package/provider.js +48 -0
  57. package/provider.js.map +1 -0
  58. package/provider.ts +119 -0
  59. package/received.js +72 -0
  60. package/received.js.map +1 -0
  61. package/received.ts +76 -0
  62. package/recipients.js +58 -0
  63. package/replies.js +23 -0
  64. package/replies.js.map +1 -0
  65. package/replies.ts +38 -0
  66. package/send.js +136 -0
  67. package/send.js.map +1 -0
  68. package/send.ts +131 -0
  69. package/stash.js +26 -0
  70. package/stash.js.map +1 -0
  71. package/stash.ts +41 -0
  72. package/text.js +127 -0
  73. package/text.js.map +1 -0
  74. package/text.ts +192 -0
  75. package/tools.js +62 -0
  76. package/tools.js.map +1 -0
  77. package/tools.ts +55 -0
  78. package/vocab.js +18 -0
  79. package/vocab.js.map +1 -0
  80. package/vocab.json +245 -0
  81. package/vocab.ts +19 -0
package/text.js ADDED
@@ -0,0 +1,127 @@
1
+ // Texting's shapes: a text on its way out, one that arrived, what became of
2
+ // one that left, and the keywords a person texts to stop or ask for help.
3
+ // Providers translate their wire formats at the boundary.
4
+ import { eidOf, identityEid } from "@yaks/graph/identity";
5
+ import { phoneDoc } from "./vocab.js";
6
+ /** The eid of a person's `sms_consent`, derived from them. */ export let consentEid = (person)=>identityEid('sms_consent', [
7
+ person
8
+ ]);
9
+ let currentConsent = eidOf(phoneDoc, 'sms_consent');
10
+ let consentEids = (person)=>[
11
+ ...new Set([
12
+ consentEid(person),
13
+ currentConsent(person)
14
+ ])
15
+ ];
16
+ /** Read consent at either address used by a store. */ export let consented = async (tx, person)=>(await tx.get(consentEids(person), [
17
+ 'sms_consent'
18
+ ])).find((row)=>row.sms_consent != null);
19
+ /** Remove any consent the person gave, including a row written before UUID derivation. */ export let consentRemoved = (person)=>consentEids(person).map((eid)=>({
20
+ entity: {
21
+ eid
22
+ },
23
+ sms_consent: null
24
+ }));
25
+ /**
26
+ * Everyone in the thread a text was said in, sorted: its sender, the number
27
+ * it reached, and the rest of a group. A group's thread is the same whoever
28
+ * in it speaks.
29
+ *
30
+ * ```ts
31
+ * import { equal } from '@yaks/testing'
32
+ * import { thread } from '@yaks/phone'
33
+ * let ann = { from: '+12695550142', to: '+12695550100', group: ['+12695550199'] }
34
+ * let bo = { from: '+12695550199', to: '+12695550100', group: ['+12695550142'] }
35
+ * equal(thread(ann), thread(bo))
36
+ * equal(thread({ from: '+12695550142', to: '+12695550100' }).length, 2)
37
+ * ```
38
+ */ export let thread = (text)=>[
39
+ ...new Set([
40
+ text.from,
41
+ text.to,
42
+ ...text.group ?? []
43
+ ])
44
+ ].sort();
45
+ /** The rest of a group text's thread, besides its sender and the number it
46
+ * reached, from every number a provider says took part: none when nobody
47
+ * else did. */ export let othersIn = (text, numbers)=>{
48
+ let others = [
49
+ ...new Set(numbers)
50
+ ].filter((n)=>n && n != text.from && n != text.to);
51
+ return others.length ? others : undefined;
52
+ };
53
+ /** The carrier will not carry this text, and that is expected: a number not
54
+ * yet registered for business texting, or a person who replied STOP. The
55
+ * message is a sentence a person can read. */ export class Blocked extends Error {
56
+ code;
57
+ constructor(message, code = ''){
58
+ super(message);
59
+ this.name = 'Blocked';
60
+ this.code = code;
61
+ }
62
+ }
63
+ /** The provider refused the credentials it was given: the key is wrong, or
64
+ * the account does not let this address call. Nothing about the text is
65
+ * wrong; whoever holds the account mends it, and the message says how. */ export class Denied extends Error {
66
+ code;
67
+ constructor(message, code = ''){
68
+ super(message);
69
+ this.name = 'Denied';
70
+ this.code = code;
71
+ }
72
+ }
73
+ let WORDS = {
74
+ STOP: 'stop',
75
+ STOPALL: 'stop',
76
+ UNSUBSCRIBE: 'stop',
77
+ CANCEL: 'stop',
78
+ END: 'stop',
79
+ QUIT: 'stop',
80
+ HELP: 'help',
81
+ INFO: 'help',
82
+ JOIN: 'start',
83
+ START: 'start',
84
+ YES: 'start',
85
+ UNSTOP: 'start'
86
+ };
87
+ /**
88
+ * The keyword a text is, or null when it says something else. A keyword is
89
+ * the whole text, in any case, with any punctuation around it. YES opts in
90
+ * only someone who is `stopped`; from anyone else it answers a question and
91
+ * is no keyword.
92
+ *
93
+ * ```ts
94
+ * import { equal } from '@yaks/testing'
95
+ * import { keyword } from '@yaks/phone'
96
+ * equal(keyword(' Stop.'), 'stop')
97
+ * equal(['stopall', 'Unsubscribe', 'cancel', 'END', 'quit'].map((w) => keyword(w)), [
98
+ * 'stop', 'stop', 'stop', 'stop', 'stop',
99
+ * ])
100
+ * equal(keyword('help'), 'help')
101
+ * equal(keyword('join'), 'start')
102
+ * equal(keyword('YES'), 'start')
103
+ * equal(keyword('YES', false), null)
104
+ * equal(keyword('start', false), 'start')
105
+ * equal(keyword('stop the meeting notice'), null)
106
+ * ```
107
+ */ export let keyword = (text, stopped = true)=>{
108
+ let word = text.trim().replace(/^\W+|\W+$/g, '').toUpperCase();
109
+ return word == 'YES' && !stopped ? null : WORDS[word] ?? null;
110
+ };
111
+ /** Whether the person whose texts are `said`, oldest first, last said STOP. */ export let stoppedBy = (said)=>said.reduce((out, text)=>{
112
+ let word = keyword(text, out);
113
+ return word == 'stop' ? true : word == 'start' ? false : out;
114
+ }, false);
115
+ /**
116
+ * Whether a text reached the number that sent it. Nobody wrote it to that
117
+ * number: it is the line's own text coming back, and an answer to it would
118
+ * come back the same way, so a host that answers texts leaves it unread.
119
+ *
120
+ * ```ts
121
+ * import { equal } from '@yaks/testing'
122
+ * import { echo } from '@yaks/phone'
123
+ * equal(echo({ from: '+12695550100', to: '+12695550100' }), true)
124
+ * equal(echo({ from: '+12695550142', to: '+12695550100' }), false)
125
+ * ```
126
+ */ export let echo = (text)=>!!text.from && text.from == text.to;
127
+ //# sourceMappingURL=text.js.map
package/text.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text.js","sources":["./text.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,0EAA0E;AAC1E,0DAA0D;AAI1D,SAAS,KAAK,EAAE,WAAW,oCAAyC;AACpE,SAAS,QAAQ,qBAAoB;AAErC,4DAA4D,GAC5D,OAAO,IAAI,aAAa,CAAC,SACvB,YAAY,eAAe;IAAC;GAAO,EAAC;AAEtC,IAAI,iBAAiB,MAAM,UAAU;AACrC,IAAI,cAAc,CAChB,SACa;OAAI,IAAI,IAAI;MAAC,WAAW;MAAS,eAAe;KAAQ;GAAE;AAEzE,oDAAoD,GACpD,OAAO,IAAI,YAAY,OAAO,IAAyB,SACrD,CAAC,MAAM,GAAG,GAAG,CAAC,YAAY,SAAS;IAAC;GAAc,CAAC,EAAE,IAAI,CAAC,CAAC,MACzD,IAAI,WAAW,IAAI,MACpB;AAEH,wFAAwF,GACxF,OAAO,IAAI,iBAAiB,CAAC,SAC3B,YAAY,QAAQ,GAAG,CAAC,CAAC,MAAQ,CAAC;MAAE,QAAQ;QAAE;MAAI;MAAG,aAAa;IAAK,CAAC,GAAE;AAqC5E;;;;;;;;;;;;;CAaC,GACD,OAAO,IAAI,SAAS,CAClB,OACa;OAAI,IAAI,IAAI;MAAC,KAAK,IAAI;MAAE,KAAK,EAAE;SAAK,KAAK,KAAK,IAAI,EAAE;KAAC;GAAE,CAAC,IAAI,GAAE;AAE7E;;aAEa,GACb,OAAO,IAAI,WAAW,CACpB,MACA;EAEA,IAAI,SAAS;OAAI,IAAI,IAAI;GAAS,CAAC,MAAM,CAAC,CAAC,IACzC,KAAK,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE;EAErC,OAAO,OAAO,MAAM,GAAG,SAAS;AAClC,EAAC;AAUD;;4CAE4C,GAC5C,OAAO,MAAM,gBAAgB;EAC3B,KAAY;EACZ,YAAY,OAAe,EAAE,OAAO,EAAE,CAAE;IACtC,KAAK,CAAC;IACN,IAAI,CAAC,IAAI,GAAG;IACZ,IAAI,CAAC,IAAI,GAAG;EACd;AACF;AAEA;;wEAEwE,GACxE,OAAO,MAAM,eAAe;EAC1B,KAAY;EACZ,YAAY,OAAe,EAAE,OAAO,EAAE,CAAE;IACtC,KAAK,CAAC;IACN,IAAI,CAAC,IAAI,GAAG;IACZ,IAAI,CAAC,IAAI,GAAG;EACd;AACF;AAMA,IAAI,QAAiC;EACnC,MAAM;EACN,SAAS;EACT,aAAa;EACb,QAAQ;EACR,KAAK;EACL,MAAM;EACN,MAAM;EACN,MAAM;EACN,MAAM;EACN,OAAO;EACP,KAAK;EACL,QAAQ;AACV;AAEA;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,IAAI,UAAU,CAAC,MAAc,UAAU,IAAI;EAChD,IAAI,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,cAAc,IAAI,WAAW;EAC5D,OAAO,QAAQ,SAAS,CAAC,UAAU,OAAO,KAAK,CAAC,KAAK,IAAI;AAC3D,EAAC;AAED,6EAA6E,GAC7E,OAAO,IAAI,YAAY,CAAC,OACtB,KAAK,MAAM,CAAC,CAAC,KAAK;IAChB,IAAI,OAAO,QAAQ,MAAM;IACzB,OAAO,QAAQ,SAAS,OAAO,QAAQ,UAAU,QAAQ;EAC3D,GAAG,OAAM;AAEX;;;;;;;;;;;CAWC,GACD,OAAO,IAAI,OAAO,CAAC,OACjB,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,EAAE,CAAA"}
package/text.ts ADDED
@@ -0,0 +1,192 @@
1
+ // Texting's shapes: a text on its way out, one that arrived, what became of
2
+ // one that left, and the keywords a person texts to stop or ask for help.
3
+ // Providers translate their wire formats at the boundary.
4
+
5
+ import type { Texter } from 'jsr:@yaks/host@0.2.36/senders'
6
+ import type { Bundle, ReadTx } from 'jsr:@yaks/graph@0.2.36'
7
+ import { eidOf, identityEid } from '@yaks/graph/identity'
8
+ import { phoneDoc } from './vocab.js'
9
+
10
+ /** The eid of a person's `sms_consent`, derived from them. */
11
+ export let consentEid = (person: string): string =>
12
+ identityEid('sms_consent', [person])
13
+
14
+ let currentConsent = eidOf(phoneDoc, 'sms_consent')
15
+ let consentEids = (
16
+ person: string,
17
+ ): string[] => [...new Set([consentEid(person), currentConsent(person)])]
18
+
19
+ /** Read consent at either address used by a store. */
20
+ export let consented = async (tx: Pick<ReadTx, 'get'>, person: string) =>
21
+ (await tx.get(consentEids(person), ['sms_consent'])).find((row) =>
22
+ row.sms_consent != null
23
+ )
24
+
25
+ /** Remove any consent the person gave, including a row written before UUID derivation. */
26
+ export let consentRemoved = (person: string): Bundle[] =>
27
+ consentEids(person).map((eid) => ({ entity: { eid }, sms_consent: null }))
28
+
29
+ // A text and its receipt are the host's portable texter capability, so a
30
+ // host can lend one (`texters`) without knowing this package.
31
+ export type { Text, TextReceipt as Receipt } from 'jsr:@yaks/host@0.2.36/senders'
32
+
33
+ /** A sender resolves when the text is accepted and rejects when it is not:
34
+ * {@link Blocked} when the carrier will not carry it, through nobody's fault
35
+ * here; {@link Denied} when the provider refused the credentials; any other
36
+ * rejection is a failure. `from`, where the provider can say it, answers the
37
+ * number its texts go from when a text names none: the number people text to
38
+ * reach whoever sends through it. */
39
+ export type Sender = Texter & { from?: () => Promise<string> }
40
+
41
+ /** A text that arrived. */
42
+ export type Arrival = {
43
+ /** The transport it arrived on, when the host names one. */
44
+ via?: string
45
+ /** the provider's id for the message */
46
+ id: string
47
+ /** the number it came from */
48
+ from: string
49
+ /** the number it was sent to, or the RCS agent's id when `rcs` */
50
+ to: string
51
+ /** the words; for a tapped suggestion, its label */
52
+ text: string
53
+ /** when it arrived */
54
+ at: string
55
+ /** whether it came as RCS, so an answer goes back the same way */
56
+ rcs: boolean
57
+ /** pictures and files sent with it (MMS), each at a provider URL */
58
+ media: { url: string; type: string }[]
59
+ /** said in a group text: everyone else in it, besides its sender and the
60
+ * number it reached; absent for one said to that number alone */
61
+ group?: string[]
62
+ }
63
+
64
+ /**
65
+ * Everyone in the thread a text was said in, sorted: its sender, the number
66
+ * it reached, and the rest of a group. A group's thread is the same whoever
67
+ * in it speaks.
68
+ *
69
+ * ```ts
70
+ * import { equal } from '@yaks/testing'
71
+ * import { thread } from '@yaks/phone'
72
+ * let ann = { from: '+12695550142', to: '+12695550100', group: ['+12695550199'] }
73
+ * let bo = { from: '+12695550199', to: '+12695550100', group: ['+12695550142'] }
74
+ * equal(thread(ann), thread(bo))
75
+ * equal(thread({ from: '+12695550142', to: '+12695550100' }).length, 2)
76
+ * ```
77
+ */
78
+ export let thread = (
79
+ text: Pick<Arrival, 'from' | 'to' | 'group'>,
80
+ ): string[] => [...new Set([text.from, text.to, ...text.group ?? []])].sort()
81
+
82
+ /** The rest of a group text's thread, besides its sender and the number it
83
+ * reached, from every number a provider says took part: none when nobody
84
+ * else did. */
85
+ export let othersIn = (
86
+ text: Pick<Arrival, 'from' | 'to'>,
87
+ numbers: string[],
88
+ ): string[] | undefined => {
89
+ let others = [...new Set(numbers)].filter((n) =>
90
+ n && n != text.from && n != text.to
91
+ )
92
+ return others.length ? others : undefined
93
+ }
94
+
95
+ /** What became of a text that left, as the provider reports it later. */
96
+ export type Outcome = {
97
+ /** the id the receipt gave */
98
+ id: string
99
+ /** why it did not arrive; absent when it did */
100
+ failed?: { code: string; reason: string; blocked: boolean }
101
+ }
102
+
103
+ /** The carrier will not carry this text, and that is expected: a number not
104
+ * yet registered for business texting, or a person who replied STOP. The
105
+ * message is a sentence a person can read. */
106
+ export class Blocked extends Error {
107
+ code: string
108
+ constructor(message: string, code = '') {
109
+ super(message)
110
+ this.name = 'Blocked'
111
+ this.code = code
112
+ }
113
+ }
114
+
115
+ /** The provider refused the credentials it was given: the key is wrong, or
116
+ * the account does not let this address call. Nothing about the text is
117
+ * wrong; whoever holds the account mends it, and the message says how. */
118
+ export class Denied extends Error {
119
+ code: string
120
+ constructor(message: string, code = '') {
121
+ super(message)
122
+ this.name = 'Denied'
123
+ this.code = code
124
+ }
125
+ }
126
+
127
+ /** The keywords carriers require every business number to answer: `start`
128
+ * opts in (JOIN, START, YES), `stop` opts out, `help` asks who is texting. */
129
+ export type Keyword = 'stop' | 'help' | 'start'
130
+
131
+ let WORDS: Record<string, Keyword> = {
132
+ STOP: 'stop',
133
+ STOPALL: 'stop',
134
+ UNSUBSCRIBE: 'stop',
135
+ CANCEL: 'stop',
136
+ END: 'stop',
137
+ QUIT: 'stop',
138
+ HELP: 'help',
139
+ INFO: 'help',
140
+ JOIN: 'start',
141
+ START: 'start',
142
+ YES: 'start',
143
+ UNSTOP: 'start',
144
+ }
145
+
146
+ /**
147
+ * The keyword a text is, or null when it says something else. A keyword is
148
+ * the whole text, in any case, with any punctuation around it. YES opts in
149
+ * only someone who is `stopped`; from anyone else it answers a question and
150
+ * is no keyword.
151
+ *
152
+ * ```ts
153
+ * import { equal } from '@yaks/testing'
154
+ * import { keyword } from '@yaks/phone'
155
+ * equal(keyword(' Stop.'), 'stop')
156
+ * equal(['stopall', 'Unsubscribe', 'cancel', 'END', 'quit'].map((w) => keyword(w)), [
157
+ * 'stop', 'stop', 'stop', 'stop', 'stop',
158
+ * ])
159
+ * equal(keyword('help'), 'help')
160
+ * equal(keyword('join'), 'start')
161
+ * equal(keyword('YES'), 'start')
162
+ * equal(keyword('YES', false), null)
163
+ * equal(keyword('start', false), 'start')
164
+ * equal(keyword('stop the meeting notice'), null)
165
+ * ```
166
+ */
167
+ export let keyword = (text: string, stopped = true): Keyword | null => {
168
+ let word = text.trim().replace(/^\W+|\W+$/g, '').toUpperCase()
169
+ return word == 'YES' && !stopped ? null : WORDS[word] ?? null
170
+ }
171
+
172
+ /** Whether the person whose texts are `said`, oldest first, last said STOP. */
173
+ export let stoppedBy = (said: string[]): boolean =>
174
+ said.reduce((out, text) => {
175
+ let word = keyword(text, out)
176
+ return word == 'stop' ? true : word == 'start' ? false : out
177
+ }, false)
178
+
179
+ /**
180
+ * Whether a text reached the number that sent it. Nobody wrote it to that
181
+ * number: it is the line's own text coming back, and an answer to it would
182
+ * come back the same way, so a host that answers texts leaves it unread.
183
+ *
184
+ * ```ts
185
+ * import { equal } from '@yaks/testing'
186
+ * import { echo } from '@yaks/phone'
187
+ * equal(echo({ from: '+12695550100', to: '+12695550100' }), true)
188
+ * equal(echo({ from: '+12695550142', to: '+12695550100' }), false)
189
+ * ```
190
+ */
191
+ export let echo = (text: Pick<Arrival, 'from' | 'to'>): boolean =>
192
+ !!text.from && text.from == text.to
package/tools.js ADDED
@@ -0,0 +1,62 @@
1
+ // What an agent may ask for here: the `@yaks/phone/tools` entry point, the
2
+ // implementation behind `text_send` in ./vocab.json. It writes a text that
3
+ // asks to be sent (`deliver{to}`), and ./effects.ts hands it to the texter
4
+ // the config's `sender.via` names, so this file never talks to a provider.
5
+ import { argsOf, referenced } from "@yaks/graph/call";
6
+ import { CallError } from "@yaks/tools/errors";
7
+ import { line } from "@yaks/query";
8
+ import { consented } from "./text.js";
9
+ import { mobile } from "./mobile.js";
10
+ let str = (v)=>v == null ? '' : String(v);
11
+ // Who `to` names: a number someone here wears, or an id or name.
12
+ let reached = async (graph, to)=>{
13
+ let number = /[a-z]/i.test(to) ? null : mobile(to);
14
+ if (number) {
15
+ let [found] = await graph.read(line`.phone.number=${number}&.fields=phone.number`);
16
+ return found?.entity.eid;
17
+ }
18
+ let [eid] = await referenced(graph, [
19
+ to
20
+ ], 'phone');
21
+ return (await graph.get([
22
+ eid
23
+ ], [
24
+ 'phone'
25
+ ]))[0]?.entity.eid;
26
+ };
27
+ /** The implementations of the tools ./vocab.json declares. */ export let runs = ()=>({
28
+ text_send: async (call, graph)=>{
29
+ let args = argsOf(call);
30
+ let to = str(args.to).trim();
31
+ let body = str(args.body).trim();
32
+ if (!body) throw new CallError('body', 'a text needs words — say --body');
33
+ let person = to ? await reached(graph, to) : undefined;
34
+ let [row] = person ? await graph.get([
35
+ person
36
+ ], [
37
+ 'phone'
38
+ ]) : [];
39
+ let consent = person ? await consented(graph, person) : undefined;
40
+ if (!row?.phone?.number) {
41
+ throw new CallError('to', `nobody here has a phone number at ${to}`);
42
+ }
43
+ if (!consent?.sms_consent) {
44
+ throw new CallError('to', `${to} has not agreed to texts: only someone with an sms_consent is texted`);
45
+ }
46
+ return [
47
+ {
48
+ entity: {
49
+ eid: '$text'
50
+ },
51
+ doc: {
52
+ body
53
+ },
54
+ text: {},
55
+ deliver: {
56
+ to: person
57
+ }
58
+ }
59
+ ];
60
+ }
61
+ });
62
+ //# sourceMappingURL=tools.js.map
package/tools.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,2EAA2E;AAC3E,2EAA2E;AAC3E,2EAA2E;AAG3E,SAAS,MAAM,EAAE,UAAU,gCAAqC;AAEhE,SAAS,SAAS,kCAAuC;AACzD,SAAS,IAAI,2BAAgC;AAC7C,SAAS,SAAS,oBAAmB;AACrC,SAAS,MAAM,sBAAqB;AAEpC,IAAI,MAAM,CAAC,IAAuB,KAAK,OAAO,KAAK,OAAO;AAE1D,iEAAiE;AACjE,IAAI,UAAU,OAAO,OAAc;EACjC,IAAI,SAAS,SAAS,IAAI,CAAC,MAAM,OAAO,OAAO;EAC/C,IAAI,QAAQ;IACV,IAAI,CAAC,MAAM,GAAG,MAAM,MAAM,IAAI,CAC5B,IAAI,CAAC,cAAc,EAAE,OAAO,qBAAqB,CAAC;IAEpD,OAAO,OAAO,OAAO;EACvB;EACA,IAAI,CAAC,IAAI,GAAG,MAAM,WAAW,OAAO;IAAC;GAAG,EAAE;EAC1C,OAAO,CAAC,MAAM,MAAM,GAAG,CAAC;IAAC;GAAI,EAAE;IAAC;GAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,OAAO;AACxD;AAEA,4DAA4D,GAC5D,OAAO,IAAI,OAAO,IAAY,CAAC;IAC7B,WAAW,OAAO,MAAM;MACtB,IAAI,OAAO,OAAO;MAClB,IAAI,KAAK,IAAI,KAAK,EAAE,EAAE,IAAI;MAC1B,IAAI,OAAO,IAAI,KAAK,IAAI,EAAE,IAAI;MAC9B,IAAI,CAAC,MAAM,MAAM,IAAI,UAAU,QAAQ;MACvC,IAAI,SAAS,KAAK,MAAM,QAAQ,OAAO,MAAM;MAC7C,IAAI,CAAC,IAAI,GAAG,SAAS,MAAM,MAAM,GAAG,CAAC;QAAC;OAAO,EAAE;QAAC;OAAQ,IAAI,EAAE;MAC9D,IAAI,UAAU,SAAS,MAAM,UAAU,OAAO,UAAU;MACxD,IAAI,CAAE,KAAK,OAA4B,QAAQ;QAC7C,MAAM,IAAI,UAAU,MAAM,CAAC,kCAAkC,EAAE,IAAI;MACrE;MACA,IAAI,CAAC,SAAS,aAAa;QACzB,MAAM,IAAI,UACR,MACA,GAAG,GAAG,oEAAoE,CAAC;MAE/E;MACA,OAAO;QAAC;UACN,QAAQ;YAAE,KAAK;UAAQ;UACvB,KAAK;YAAE;UAAK;UACZ,MAAM,CAAC;UACP,SAAS;YAAE,IAAI;UAAO;QACxB;OAAE;IACJ;EACF,CAAC,EAAC"}
package/tools.ts ADDED
@@ -0,0 +1,55 @@
1
+ // What an agent may ask for here: the `@yaks/phone/tools` entry point, the
2
+ // implementation behind `text_send` in ./vocab.json. It writes a text that
3
+ // asks to be sent (`deliver{to}`), and ./effects.ts hands it to the texter
4
+ // the config's `sender.via` names, so this file never talks to a provider.
5
+
6
+ import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
7
+ import { argsOf, referenced } from '@yaks/graph/call'
8
+ import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
9
+ import { CallError } from '@yaks/tools/errors'
10
+ import { line } from '@yaks/query'
11
+ import { consented } from './text.js'
12
+ import { mobile } from './mobile.js'
13
+
14
+ let str = (v: unknown): string => v == null ? '' : String(v)
15
+
16
+ // Who `to` names: a number someone here wears, or an id or name.
17
+ let reached = async (graph: Graph, to: string): Promise<Eid | undefined> => {
18
+ let number = /[a-z]/i.test(to) ? null : mobile(to)
19
+ if (number) {
20
+ let [found] = await graph.read(
21
+ line`.phone.number=${number}&.fields=phone.number`,
22
+ )
23
+ return found?.entity.eid
24
+ }
25
+ let [eid] = await referenced(graph, [to], 'phone')
26
+ return (await graph.get([eid], ['phone']))[0]?.entity.eid
27
+ }
28
+
29
+ /** The implementations of the tools ./vocab.json declares. */
30
+ export let runs = (): Runs => ({
31
+ text_send: async (call, graph): Promise<Bundle[]> => {
32
+ let args = argsOf(call)
33
+ let to = str(args.to).trim()
34
+ let body = str(args.body).trim()
35
+ if (!body) throw new CallError('body', 'a text needs words — say --body')
36
+ let person = to ? await reached(graph, to) : undefined
37
+ let [row] = person ? await graph.get([person], ['phone']) : []
38
+ let consent = person ? await consented(graph, person) : undefined
39
+ if (!(row?.phone as Comp | undefined)?.number) {
40
+ throw new CallError('to', `nobody here has a phone number at ${to}`)
41
+ }
42
+ if (!consent?.sms_consent) {
43
+ throw new CallError(
44
+ 'to',
45
+ `${to} has not agreed to texts: only someone with an sms_consent is texted`,
46
+ )
47
+ }
48
+ return [{
49
+ entity: { eid: '$text' },
50
+ doc: { body },
51
+ text: {},
52
+ deliver: { to: person },
53
+ }]
54
+ },
55
+ })
package/vocab.js ADDED
@@ -0,0 +1,18 @@
1
+ // The component declarations, and nothing else: the module a server or a
2
+ // browser page imports at `@yaks/phone/vocab`. It reaches no storage, no SQL
3
+ // and no runtime.
4
+ import doc from './vocab.json' with {
5
+ type: 'json'
6
+ };
7
+ import manifest from './package.json' with {
8
+ type: 'json'
9
+ };
10
+ /** The phone vocabulary: `phone{number}`, `sms_consent{person, consent,
11
+ * at, space}` and `text{from, to, at, message_id, rcs}`, in the form
12
+ * `loadVocab` accepts. */ export let phoneDoc = doc;
13
+ /** What this package is: its manifest's description, the one place it is
14
+ * written, which a host stamps on each document here. */ export let description = manifest.description;
15
+ /** Every document this package declares. */ export let docs = [
16
+ phoneDoc
17
+ ];
18
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,6EAA6E;AAC7E,kBAAkB;AAGlB,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D;;wBAEwB,GACxB,OAAO,IAAI,WAAqB,IAAe;AAE/C;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,0CAA0C,GAC1C,OAAO,IAAI,OAAmB;EAAC;CAAS,CAAA"}