@yaks/tools 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 (138) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +870 -2
  3. package/_dist/access.d.ts +3 -0
  4. package/_dist/access.d.ts.map +1 -0
  5. package/_dist/app-view.d.ts +27 -0
  6. package/_dist/app-view.d.ts.map +1 -0
  7. package/_dist/args.d.ts +23 -0
  8. package/_dist/args.d.ts.map +1 -0
  9. package/_dist/check.d.ts +32 -0
  10. package/_dist/check.d.ts.map +1 -0
  11. package/_dist/declared.d.ts +66 -0
  12. package/_dist/declared.d.ts.map +1 -0
  13. package/_dist/display.d.ts +2 -0
  14. package/_dist/display.d.ts.map +1 -0
  15. package/_dist/errors.d.ts +46 -0
  16. package/_dist/errors.d.ts.map +1 -0
  17. package/_dist/fault.d.ts +4 -0
  18. package/_dist/fault.d.ts.map +1 -0
  19. package/_dist/invoke.d.ts +13 -0
  20. package/_dist/invoke.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +31 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/outlast.d.ts +89 -0
  24. package/_dist/outlast.d.ts.map +1 -0
  25. package/_dist/persist.d.ts +5 -0
  26. package/_dist/persist.d.ts.map +1 -0
  27. package/_dist/read.d.ts +4 -0
  28. package/_dist/read.d.ts.map +1 -0
  29. package/_dist/readable.d.ts +59 -0
  30. package/_dist/readable.d.ts.map +1 -0
  31. package/_dist/registry.d.ts +53 -0
  32. package/_dist/registry.d.ts.map +1 -0
  33. package/_dist/result.d.ts +154 -0
  34. package/_dist/result.d.ts.map +1 -0
  35. package/_dist/routes.d.ts +11 -0
  36. package/_dist/routes.d.ts.map +1 -0
  37. package/_dist/runner.d.ts +161 -0
  38. package/_dist/runner.d.ts.map +1 -0
  39. package/_dist/said.d.ts +4 -0
  40. package/_dist/said.d.ts.map +1 -0
  41. package/_dist/schema.d.ts +76 -0
  42. package/_dist/schema.d.ts.map +1 -0
  43. package/_dist/state.d.ts +9 -0
  44. package/_dist/state.d.ts.map +1 -0
  45. package/_dist/title.d.ts +20 -0
  46. package/_dist/title.d.ts.map +1 -0
  47. package/_dist/value.d.ts +43 -0
  48. package/_dist/value.d.ts.map +1 -0
  49. package/_dist/views.d.ts +5 -0
  50. package/_dist/views.d.ts.map +1 -0
  51. package/_dist/vocab.d.ts +9 -0
  52. package/_dist/vocab.d.ts.map +1 -0
  53. package/_dist/watch.d.ts +28 -0
  54. package/_dist/watch.d.ts.map +1 -0
  55. package/access.js +3 -0
  56. package/access.js.map +1 -0
  57. package/access.ts +11 -0
  58. package/app-view.js +26 -0
  59. package/app-view.js.map +1 -0
  60. package/app-view.ts +41 -0
  61. package/args.js +136 -0
  62. package/args.js.map +1 -0
  63. package/args.ts +187 -0
  64. package/body.js +14 -0
  65. package/body.js.map +1 -0
  66. package/body.ts +15 -0
  67. package/check.js +67 -0
  68. package/check.js.map +1 -0
  69. package/check.ts +92 -0
  70. package/declared.js +529 -0
  71. package/declared.js.map +1 -0
  72. package/declared.ts +719 -0
  73. package/deno.json +1 -0
  74. package/display.js +21 -0
  75. package/display.js.map +1 -0
  76. package/display.ts +39 -0
  77. package/errors.js +99 -0
  78. package/errors.js.map +1 -0
  79. package/errors.ts +127 -0
  80. package/fault.js +17 -0
  81. package/fault.js.map +1 -0
  82. package/fault.ts +24 -0
  83. package/invoke.js +207 -0
  84. package/invoke.js.map +1 -0
  85. package/invoke.ts +210 -0
  86. package/mod.js +31 -0
  87. package/mod.js.map +1 -0
  88. package/mod.ts +78 -0
  89. package/outlast.js +416 -0
  90. package/outlast.js.map +1 -0
  91. package/outlast.ts +551 -0
  92. package/package.json +109 -4
  93. package/persist.js +18 -0
  94. package/persist.js.map +1 -0
  95. package/persist.ts +26 -0
  96. package/read.js +50 -0
  97. package/read.js.map +1 -0
  98. package/read.ts +58 -0
  99. package/readable.js +118 -0
  100. package/readable.js.map +1 -0
  101. package/readable.ts +134 -0
  102. package/registry.js +162 -0
  103. package/registry.js.map +1 -0
  104. package/registry.ts +207 -0
  105. package/result.js +497 -0
  106. package/result.js.map +1 -0
  107. package/result.ts +540 -0
  108. package/routes.js +46 -0
  109. package/routes.js.map +1 -0
  110. package/routes.ts +50 -0
  111. package/runner.js +748 -0
  112. package/runner.js.map +1 -0
  113. package/runner.ts +910 -0
  114. package/said.js +15 -0
  115. package/said.js.map +1 -0
  116. package/said.ts +12 -0
  117. package/schema.js +137 -0
  118. package/schema.js.map +1 -0
  119. package/schema.ts +134 -0
  120. package/state.js +69 -0
  121. package/state.js.map +1 -0
  122. package/state.ts +108 -0
  123. package/title.js +222 -0
  124. package/title.js.map +1 -0
  125. package/title.ts +257 -0
  126. package/value.js +57 -0
  127. package/value.js.map +1 -0
  128. package/value.ts +74 -0
  129. package/views.js +131 -0
  130. package/views.js.map +1 -0
  131. package/views.ts +125 -0
  132. package/vocab.js +22 -0
  133. package/vocab.js.map +1 -0
  134. package/vocab.json +268 -0
  135. package/vocab.ts +22 -0
  136. package/watch.js +64 -0
  137. package/watch.js.map +1 -0
  138. package/watch.ts +74 -0
package/said.js ADDED
@@ -0,0 +1,15 @@
1
+ // The answer of a tool that has only words to give: one bundle, aimed at the
2
+ // call it answers.
3
+ /** What a tool says as its answer: `body`, lines joined by newlines, in a
4
+ * bundle whose `output.source` is the call. */ export let said = (call, body)=>({
5
+ entity: {
6
+ eid: '$said'
7
+ },
8
+ content: {
9
+ body: typeof body == 'string' ? body : body.join('\n')
10
+ },
11
+ output: {
12
+ source: call.entity.eid
13
+ }
14
+ });
15
+ //# sourceMappingURL=said.js.map
package/said.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"said.js","sources":["./said.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,mBAAmB;AAInB;6CAC6C,GAC7C,OAAO,IAAI,OAAO,CAAC,MAAc,OAAoC,CAAC;IACpE,QAAQ;MAAE,KAAK;IAAQ;IACvB,SAAS;MAAE,MAAM,OAAO,QAAQ,WAAW,OAAO,KAAK,IAAI,CAAC;IAAM;IAClE,QAAQ;MAAE,QAAQ,KAAK,MAAM,CAAC,GAAG;IAAC;EACpC,CAAC,EAAC"}
package/said.ts ADDED
@@ -0,0 +1,12 @@
1
+ // The answer of a tool that has only words to give: one bundle, aimed at the
2
+ // call it answers.
3
+
4
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
5
+
6
+ /** What a tool says as its answer: `body`, lines joined by newlines, in a
7
+ * bundle whose `output.source` is the call. */
8
+ export let said = (call: Bundle, body: string | string[]): Bundle => ({
9
+ entity: { eid: '$said' },
10
+ content: { body: typeof body == 'string' ? body : body.join('\n') },
11
+ output: { source: call.entity.eid },
12
+ })
package/schema.js ADDED
@@ -0,0 +1,137 @@
1
+ // Public input and output schemas shared by tool catalogs and every transport.
2
+ import { publicToolSchema } from "@yaks/vocab/tools";
3
+ // What a tool that declares no arguments is listed as taking: nothing.
4
+ let none = ()=>({
5
+ type: 'object',
6
+ additionalProperties: false
7
+ });
8
+ /**
9
+ * A tool's arguments as JSON Schema: its own `inputSchema`, published (the
10
+ * command-line `short` letters a CLI reads stay off the wire), or none for a
11
+ * tool that takes no arguments. This is exactly what `tools/list` sends, so
12
+ * anything else that needs a tool's argument grammar — a CLI mapping a
13
+ * command onto it, say — reads what an MCP client reads.
14
+ *
15
+ * ```ts
16
+ * import { assertEquals } from '@std/assert'
17
+ * import { inputSchemaOf } from '@yaks/tools/schema'
18
+ *
19
+ * let tool = {
20
+ * name: 'write',
21
+ * description: 'Write a doc',
22
+ * inputSchema: {
23
+ * type: 'object',
24
+ * properties: { title: { type: 'string', short: 't' } },
25
+ * },
26
+ * run: () => [],
27
+ * }
28
+ * assertEquals(inputSchemaOf(tool).properties, { title: { type: 'string' } })
29
+ * ```
30
+ */ export let inputSchemaOf = (tool)=>publicToolSchema(tool.inputSchema ?? none());
31
+ /**
32
+ * The bundle answer for a door whose rows say words of their own beside their
33
+ * components: {@link answerSchema}, with each of those words declared on the
34
+ * row under its own schema. A client checks every answer against the schema
35
+ * its tool is listed with, so a row word left undeclared reads to it as a
36
+ * component that is not an object, and the whole answer is refused. yaks.app's
37
+ * rows name what each entity is (`kind`), for one.
38
+ */ export let answerWith = (words)=>({
39
+ type: 'object',
40
+ properties: {
41
+ running: {
42
+ type: 'boolean'
43
+ },
44
+ result_handle: {
45
+ type: 'string'
46
+ },
47
+ result: {
48
+ type: 'array',
49
+ items: {
50
+ type: 'object',
51
+ properties: {
52
+ entity: {
53
+ type: 'object',
54
+ properties: {
55
+ eid: {
56
+ type: 'string'
57
+ }
58
+ },
59
+ required: [
60
+ 'eid'
61
+ ]
62
+ },
63
+ ...words
64
+ },
65
+ required: [
66
+ 'entity'
67
+ ],
68
+ // The `$` sugars a bundle may wear beside its components — `$alias`,
69
+ // the name a minted entity was asked for by, and the rest of
70
+ // @yaks/graph's — are not components and are not objects, so they are
71
+ // matched first and left open.
72
+ patternProperties: {
73
+ '^\\$': {}
74
+ },
75
+ additionalProperties: {
76
+ type: [
77
+ 'object',
78
+ 'null'
79
+ ]
80
+ }
81
+ }
82
+ }
83
+ },
84
+ required: [
85
+ 'result'
86
+ ]
87
+ });
88
+ /**
89
+ * The default output schema: returned bundles under `result`, shared by tool
90
+ * discovery and transports that send structured answers.
91
+ *
92
+ * Every tool whose answer is entities answers this one shape, so it is written
93
+ * once and listed on all of them, a plugin's tool included without declaring
94
+ * anything; a tool whose answer is not entities declares its own
95
+ * `outputSchema` (`graph_schema`, a vocabulary document). It says what a
96
+ * bundle is and not what any component holds: a fully typed bundle is tens of
97
+ * kilobytes of vocabulary, and `tools/list` would carry a copy per tool before
98
+ * the agent has asked its first question. What a component holds is
99
+ * `graph_schema`'s answer, asked for when it is wanted. It carries no prose
100
+ * for the same reason: it rides on every tool, and `graph_apply`'s
101
+ * description says what a bundle is once. A door whose rows carry words of
102
+ * their own lists {@link answerWith} instead.
103
+ */ export let answerSchema = answerWith({});
104
+ let saids = new WeakMap();
105
+ /**
106
+ * A tool's output schema as a door lists it when its structured answer also
107
+ * carries `said`: the words its text says beside the data (@yaks/tools/result
108
+ * `aside`) and any news the door adds, for a client that gives a model the
109
+ * structured answer alone. A schema whose answer is not an object has nowhere
110
+ * to say it, and is listed as declared. The same schema is answered with the
111
+ * same object, so a check compiled from it is compiled once.
112
+ *
113
+ * ```ts
114
+ * import { assertEquals } from '@std/assert'
115
+ * let listed = saying(answerSchema)
116
+ * assertEquals((listed.properties as Record<string, unknown>).said, { type: 'string' })
117
+ * assertEquals(saying(answerSchema), listed)
118
+ * ```
119
+ */ export let saying = (schema)=>{
120
+ if (schema.type != null && schema.type != 'object') return schema;
121
+ let held = saids.get(schema);
122
+ if (!held) {
123
+ held = {
124
+ ...schema,
125
+ type: 'object',
126
+ properties: {
127
+ ...schema.properties,
128
+ said: {
129
+ type: 'string'
130
+ }
131
+ }
132
+ };
133
+ saids.set(schema, held);
134
+ }
135
+ return held;
136
+ };
137
+ //# sourceMappingURL=schema.js.map
package/schema.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sources":["./schema.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAE/E,SAAS,gBAAgB,iCAAsC;AAE/D,uEAAuE;AACvE,IAAI,OAAO,IAAM,CAAC;IAAE,MAAM;IAAU,sBAAsB;EAAM,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;CAsBC,GACD,OAAO,IAAI,gBAAgB,CACzB,OAEA,iBAAiB,KAAK,WAAW,IAAI,QAGpC;AAIH;;;;;;;CAOC,GACD,OAAO,IAAI,aAAa,CACtB,QAC+C,CAAC;IAChD,MAAM;IACN,YAAY;MACV,SAAS;QAAE,MAAM;MAAU;MAC3B,eAAe;QAAE,MAAM;MAAS;MAChC,QAAQ;QACN,MAAM;QACN,OAAO;UACL,MAAM;UACN,YAAY;YACV,QAAQ;cACN,MAAM;cACN,YAAY;gBAAE,KAAK;kBAAE,MAAM;gBAAS;cAAE;cACtC,UAAU;gBAAC;eAAM;YACnB;YACA,GAAG,KAAK;UACV;UACA,UAAU;YAAC;WAAS;UACpB,qEAAqE;UACrE,6DAA6D;UAC7D,sEAAsE;UACtE,+BAA+B;UAC/B,mBAAmB;YAAE,QAAQ,CAAC;UAAE;UAChC,sBAAsB;YAAE,MAAM;cAAC;cAAU;aAAO;UAAC;QACnD;MACF;IACF;IACA,UAAU;MAAC;KAAS;EACtB,CAAC,EAAC;AAEF;;;;;;;;;;;;;;;CAeC,GACD,OAAO,IAAI,eACT,WAAW,CAAC,GAAE;AAEhB,IAAI,QAAQ,IAAI;AAEhB;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,SAAS,CAClB;EAEA,IAAI,OAAO,IAAI,IAAI,QAAQ,OAAO,IAAI,IAAI,UAAU,OAAO;EAC3D,IAAI,OAAO,MAAM,GAAG,CAAC;EACrB,IAAI,CAAC,MAAM;IACT,OAAO;MACL,GAAG,MAAM;MACT,MAAM;MACN,YAAY;QACV,GAAI,OAAO,UAAU;QACrB,MAAM;UAAE,MAAM;QAAS;MACzB;IACF;IACA,MAAM,GAAG,CAAC,QAAQ;EACpB;EACA,OAAO;AACT,EAAC"}
package/schema.ts ADDED
@@ -0,0 +1,134 @@
1
+ // Public input and output schemas shared by tool catalogs and every transport.
2
+ import type { Tool } from 'jsr:@yaks/graph@0.2.36'
3
+ import { publicToolSchema } from '@yaks/vocab/tools'
4
+
5
+ // What a tool that declares no arguments is listed as taking: nothing.
6
+ let none = () => ({ type: 'object', additionalProperties: false })
7
+
8
+ /**
9
+ * A tool's arguments as JSON Schema: its own `inputSchema`, published (the
10
+ * command-line `short` letters a CLI reads stay off the wire), or none for a
11
+ * tool that takes no arguments. This is exactly what `tools/list` sends, so
12
+ * anything else that needs a tool's argument grammar — a CLI mapping a
13
+ * command onto it, say — reads what an MCP client reads.
14
+ *
15
+ * ```ts
16
+ * import { assertEquals } from '@std/assert'
17
+ * import { inputSchemaOf } from '@yaks/tools/schema'
18
+ *
19
+ * let tool = {
20
+ * name: 'write',
21
+ * description: 'Write a doc',
22
+ * inputSchema: {
23
+ * type: 'object',
24
+ * properties: { title: { type: 'string', short: 't' } },
25
+ * },
26
+ * run: () => [],
27
+ * }
28
+ * assertEquals(inputSchemaOf(tool).properties, { title: { type: 'string' } })
29
+ * ```
30
+ */
31
+ export let inputSchemaOf = (
32
+ tool: Tool,
33
+ ): { type: 'object'; [key: string]: unknown } =>
34
+ publicToolSchema(tool.inputSchema ?? none()) as {
35
+ type: 'object'
36
+ [key: string]: unknown
37
+ }
38
+
39
+ type Json = { [key: string]: unknown }
40
+
41
+ /**
42
+ * The bundle answer for a door whose rows say words of their own beside their
43
+ * components: {@link answerSchema}, with each of those words declared on the
44
+ * row under its own schema. A client checks every answer against the schema
45
+ * its tool is listed with, so a row word left undeclared reads to it as a
46
+ * component that is not an object, and the whole answer is refused. yaks.app's
47
+ * rows name what each entity is (`kind`), for one.
48
+ */
49
+ export let answerWith = (
50
+ words: Record<string, Json>,
51
+ ): { type: 'object'; [key: string]: unknown } => ({
52
+ type: 'object',
53
+ properties: {
54
+ running: { type: 'boolean' },
55
+ result_handle: { type: 'string' },
56
+ result: {
57
+ type: 'array',
58
+ items: {
59
+ type: 'object',
60
+ properties: {
61
+ entity: {
62
+ type: 'object',
63
+ properties: { eid: { type: 'string' } },
64
+ required: ['eid'],
65
+ },
66
+ ...words,
67
+ },
68
+ required: ['entity'],
69
+ // The `$` sugars a bundle may wear beside its components — `$alias`,
70
+ // the name a minted entity was asked for by, and the rest of
71
+ // @yaks/graph's — are not components and are not objects, so they are
72
+ // matched first and left open.
73
+ patternProperties: { '^\\$': {} },
74
+ additionalProperties: { type: ['object', 'null'] },
75
+ },
76
+ },
77
+ },
78
+ required: ['result'],
79
+ })
80
+
81
+ /**
82
+ * The default output schema: returned bundles under `result`, shared by tool
83
+ * discovery and transports that send structured answers.
84
+ *
85
+ * Every tool whose answer is entities answers this one shape, so it is written
86
+ * once and listed on all of them, a plugin's tool included without declaring
87
+ * anything; a tool whose answer is not entities declares its own
88
+ * `outputSchema` (`graph_schema`, a vocabulary document). It says what a
89
+ * bundle is and not what any component holds: a fully typed bundle is tens of
90
+ * kilobytes of vocabulary, and `tools/list` would carry a copy per tool before
91
+ * the agent has asked its first question. What a component holds is
92
+ * `graph_schema`'s answer, asked for when it is wanted. It carries no prose
93
+ * for the same reason: it rides on every tool, and `graph_apply`'s
94
+ * description says what a bundle is once. A door whose rows carry words of
95
+ * their own lists {@link answerWith} instead.
96
+ */
97
+ export let answerSchema: { type: 'object'; [key: string]: unknown } =
98
+ answerWith({})
99
+
100
+ let saids = new WeakMap<object, Json>()
101
+
102
+ /**
103
+ * A tool's output schema as a door lists it when its structured answer also
104
+ * carries `said`: the words its text says beside the data (@yaks/tools/result
105
+ * `aside`) and any news the door adds, for a client that gives a model the
106
+ * structured answer alone. A schema whose answer is not an object has nowhere
107
+ * to say it, and is listed as declared. The same schema is answered with the
108
+ * same object, so a check compiled from it is compiled once.
109
+ *
110
+ * ```ts
111
+ * import { assertEquals } from '@std/assert'
112
+ * let listed = saying(answerSchema)
113
+ * assertEquals((listed.properties as Record<string, unknown>).said, { type: 'string' })
114
+ * assertEquals(saying(answerSchema), listed)
115
+ * ```
116
+ */
117
+ export let saying = (
118
+ schema: Json,
119
+ ): Json => {
120
+ if (schema.type != null && schema.type != 'object') return schema
121
+ let held = saids.get(schema)
122
+ if (!held) {
123
+ held = {
124
+ ...schema,
125
+ type: 'object',
126
+ properties: {
127
+ ...(schema.properties as Json | undefined),
128
+ said: { type: 'string' },
129
+ },
130
+ }
131
+ saids.set(schema, held)
132
+ }
133
+ return held
134
+ }
package/state.js ADDED
@@ -0,0 +1,69 @@
1
+ // A call's outcome comes from its interruption and explicitly named answers.
2
+ // The bundle rule and SQL expression describe the same facts.
3
+ import { and, col, eq, exists, lit, notNull, or, select, table, when } from "@yaks/sql/core";
4
+ let component = (b, name)=>typeof b[name] == 'object' && b[name] != null ? b[name] : undefined;
5
+ export let executionState = (call, answers = [])=>{
6
+ if (!call.execution) return null;
7
+ if (call.interrupted) return 'interrupted';
8
+ let own = answers.filter((b)=>component(b, 'output')?.source == call.entity.eid || component(b, 'result')?.call == call.entity.eid);
9
+ return own.some((b)=>b.refusal || b.exception) ? 'failed' : own.some((b)=>b.result) ? 'done' : component(call, 'execution')?.by != null ? 'running' : null;
10
+ };
11
+ export let executionDerived = (vocab)=>{
12
+ let has = (name, owner, prop = 'entity')=>vocab.comp(name) ? exists(select({
13
+ cols: [
14
+ lit(1)
15
+ ],
16
+ from: table(name, 's'),
17
+ where: eq(col(prop, 's'), owner)
18
+ })) : lit(false);
19
+ let named = (owner, field, comp)=>vocab.comp(comp) ? exists(select({
20
+ cols: [
21
+ lit(1)
22
+ ],
23
+ from: table(comp, 'a'),
24
+ where: and(eq(col(field, 'a'), owner), or(has('refusal', col('entity', 'a')), has('exception', col('entity', 'a'))))
25
+ })) : lit(false);
26
+ return {
27
+ 'execution.state': {
28
+ tag: 'text',
29
+ values: [
30
+ 'interrupted',
31
+ 'failed',
32
+ 'done',
33
+ 'running'
34
+ ],
35
+ deps: [
36
+ 'execution'
37
+ ],
38
+ expr: (owner)=>when([
39
+ [
40
+ has('interrupted', owner),
41
+ lit('interrupted')
42
+ ],
43
+ [
44
+ or(named(owner, 'source', 'output'), named(owner, 'call', 'result')),
45
+ lit('failed')
46
+ ],
47
+ [
48
+ has('result', owner, 'call'),
49
+ lit('done')
50
+ ],
51
+ [
52
+ exists(select({
53
+ cols: [
54
+ lit(1)
55
+ ],
56
+ from: table('execution', 's'),
57
+ where: and(eq(col('entity', 's'), owner), notNull(col('by', 's')))
58
+ })),
59
+ lit('running')
60
+ ]
61
+ ], lit(null))
62
+ }
63
+ };
64
+ };
65
+ export let executionComputed = {
66
+ 'execution.state': (call, among)=>executionState(call, among.list)
67
+ };
68
+ export { Interrupted } from "./errors.js";
69
+ //# sourceMappingURL=state.js.map
package/state.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sources":["./state.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8DAA8D;AAK9D,SACE,GAAG,EACH,GAAG,EAEH,EAAE,EACF,MAAM,EAEN,GAAG,EACH,OAAO,EACP,EAAE,EACF,MAAM,EACN,KAAK,EACL,IAAI,8BAC4B;AAElC,IAAI,YAAY,CAAC,GAAW,OAC1B,OAAO,CAAC,CAAC,KAAK,IAAI,YAAY,CAAC,CAAC,KAAK,IAAI,OAAO,CAAC,CAAC,KAAK,GAAW;AAEpE,OAAO,IAAI,iBAAiB,CAC1B,MACA,UAA6B,EAAE;EAE/B,IAAI,CAAC,KAAK,SAAS,EAAE,OAAO;EAC5B,IAAI,KAAK,WAAW,EAAE,OAAO;EAC7B,IAAI,MAAM,QAAQ,MAAM,CAAC,CAAC,IACxB,UAAU,GAAG,WAAW,UAAU,KAAK,MAAM,CAAC,GAAG,IACjD,UAAU,GAAG,WAAW,QAAQ,KAAK,MAAM,CAAC,GAAG;EAEjD,OAAO,IAAI,IAAI,CAAC,CAAC,IAAM,EAAE,OAAO,IAAI,EAAE,SAAS,IAC3C,WACA,IAAI,IAAI,CAAC,CAAC,IAAM,EAAE,MAAM,IACxB,SACA,UAAU,MAAM,cAAc,MAAM,OACpC,YACA;AACN,EAAC;AAED,OAAO,IAAI,mBAAmB,CAAC;EAC7B,IAAI,MAAM,CAAC,MAAc,OAAa,OAAO,QAAQ,GACnD,MAAM,IAAI,CAAC,QACP,OAAO,OAAO;MACd,MAAM;QAAC,IAAI;OAAG;MACd,MAAM,MAAM,MAAM;MAClB,OAAO,GAAG,IAAI,MAAM,MAAM;IAC5B,MACE,IAAI;EACV,IAAI,QAAQ,CAAC,OAAa,OAAe,OACvC,MAAM,IAAI,CAAC,QACP,OAAO,OAAO;MACd,MAAM;QAAC,IAAI;OAAG;MACd,MAAM,MAAM,MAAM;MAClB,OAAO,IACL,GAAG,IAAI,OAAO,MAAM,QACpB,GACE,IAAI,WAAW,IAAI,UAAU,OAC7B,IAAI,aAAa,IAAI,UAAU;IAGrC,MACE,IAAI;EACV,OAAO;IACL,mBAAmB;MACjB,KAAK;MACL,QAAQ;QAAC;QAAe;QAAU;QAAQ;OAAU;MACpD,MAAM;QAAC;OAAY;MACnB,MAAM,CAAC,QACL,KACE;UACE;YAAC,IAAI,eAAe;YAAQ,IAAI;WAAe;UAC/C;YACE,GACE,MAAM,OAAO,UAAU,WACvB,MAAM,OAAO,QAAQ;YAEvB,IAAI;WACL;UACD;YAAC,IAAI,UAAU,OAAO;YAAS,IAAI;WAAQ;UAC3C;YACE,OACE,OAAO;cACL,MAAM;gBAAC,IAAI;eAAG;cACd,MAAM,MAAM,aAAa;cACzB,OAAO,IACL,GAAG,IAAI,UAAU,MAAM,QACvB,QAAQ,IAAI,MAAM;YAEtB;YAEF,IAAI;WACL;SACF,EACD,IAAI;IAEV;EACF;AACF,EAAC;AAED,OAAO,IAAI,oBAA8B;EACvC,mBAAmB,CAAC,MAAM,QAAU,eAAe,MAAM,MAAM,IAAI;AACrE,EAAC;AAED,SAAS,WAAW,sBAAqB"}
package/state.ts ADDED
@@ -0,0 +1,108 @@
1
+ // A call's outcome comes from its interruption and explicitly named answers.
2
+ // The bundle rule and SQL expression describe the same facts.
3
+
4
+ import type { Bundle, Comp } from 'jsr:@yaks/graph@0.2.36'
5
+ import type { Computed } from 'jsr:@yaks/match@0.2.36'
6
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
7
+ import {
8
+ and,
9
+ col,
10
+ type Derived,
11
+ eq,
12
+ exists,
13
+ type Expr,
14
+ lit,
15
+ notNull,
16
+ or,
17
+ select,
18
+ table,
19
+ when,
20
+ } from '@yaks/sql/core'
21
+
22
+ let component = (b: Bundle, name: string): Comp | undefined =>
23
+ typeof b[name] == 'object' && b[name] != null ? b[name] as Comp : undefined
24
+
25
+ export let executionState = (
26
+ call: Bundle,
27
+ answers: readonly Bundle[] = [],
28
+ ): 'interrupted' | 'failed' | 'done' | 'running' | null => {
29
+ if (!call.execution) return null
30
+ if (call.interrupted) return 'interrupted'
31
+ let own = answers.filter((b) =>
32
+ component(b, 'output')?.source == call.entity.eid ||
33
+ component(b, 'result')?.call == call.entity.eid
34
+ )
35
+ return own.some((b) => b.refusal || b.exception)
36
+ ? 'failed'
37
+ : own.some((b) => b.result)
38
+ ? 'done'
39
+ : component(call, 'execution')?.by != null
40
+ ? 'running'
41
+ : null
42
+ }
43
+
44
+ export let executionDerived = (vocab: Vocab): Derived => {
45
+ let has = (name: string, owner: Expr, prop = 'entity') =>
46
+ vocab.comp(name)
47
+ ? exists(select({
48
+ cols: [lit(1)],
49
+ from: table(name, 's'),
50
+ where: eq(col(prop, 's'), owner),
51
+ }))
52
+ : lit(false)
53
+ let named = (owner: Expr, field: string, comp: string): Expr =>
54
+ vocab.comp(comp)
55
+ ? exists(select({
56
+ cols: [lit(1)],
57
+ from: table(comp, 'a'),
58
+ where: and(
59
+ eq(col(field, 'a'), owner),
60
+ or(
61
+ has('refusal', col('entity', 'a')),
62
+ has('exception', col('entity', 'a')),
63
+ ),
64
+ ),
65
+ }))
66
+ : lit(false)
67
+ return {
68
+ 'execution.state': {
69
+ tag: 'text',
70
+ values: ['interrupted', 'failed', 'done', 'running'],
71
+ deps: ['execution'],
72
+ expr: (owner) =>
73
+ when(
74
+ [
75
+ [has('interrupted', owner), lit('interrupted')],
76
+ [
77
+ or(
78
+ named(owner, 'source', 'output'),
79
+ named(owner, 'call', 'result'),
80
+ ),
81
+ lit('failed'),
82
+ ],
83
+ [has('result', owner, 'call'), lit('done')],
84
+ [
85
+ exists(
86
+ select({
87
+ cols: [lit(1)],
88
+ from: table('execution', 's'),
89
+ where: and(
90
+ eq(col('entity', 's'), owner),
91
+ notNull(col('by', 's')),
92
+ ),
93
+ }),
94
+ ),
95
+ lit('running'),
96
+ ],
97
+ ],
98
+ lit(null),
99
+ ),
100
+ },
101
+ }
102
+ }
103
+
104
+ export let executionComputed: Computed = {
105
+ 'execution.state': (call, among) => executionState(call, among.list),
106
+ }
107
+
108
+ export { Interrupted } from './errors.js'