@ibgib/core-gib 0.0.8 → 0.0.11
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.
- package/README.md +5 -10
- package/dist/assumptions.respec.d.mts +2 -0
- package/dist/assumptions.respec.d.mts.map +1 -0
- package/dist/assumptions.respec.mjs +41 -0
- package/dist/assumptions.respec.mjs.map +1 -0
- package/dist/core-constants.d.mts +0 -2
- package/dist/core-constants.d.mts.map +1 -1
- package/dist/core-constants.mjs +0 -2
- package/dist/core-constants.mjs.map +1 -1
- package/dist/core-helper.respec.d.mts +2 -0
- package/dist/core-helper.respec.d.mts.map +1 -0
- package/dist/core-helper.respec.mjs +53 -0
- package/dist/core-helper.respec.mjs.map +1 -0
- package/dist/core-types.d.mts +22 -0
- package/dist/core-types.d.mts.map +1 -1
- package/dist/respec-gib.node.d.mts +2 -0
- package/dist/respec-gib.node.d.mts.map +1 -0
- package/dist/respec-gib.node.mjs +211 -0
- package/dist/respec-gib.node.mjs.map +1 -0
- package/dist/spec-helper.node.respec.d.mts +12 -0
- package/dist/spec-helper.node.respec.d.mts.map +1 -0
- package/dist/spec-helper.node.respec.mjs +43 -0
- package/dist/spec-helper.node.respec.mjs.map +1 -0
- package/dist/witness/app/app-base-v1.d.mts +2 -2
- package/dist/witness/app/app-base-v1.d.mts.map +1 -1
- package/dist/witness/app/app-base-v1.mjs +23 -141
- package/dist/witness/app/app-base-v1.mjs.map +1 -1
- package/dist/witness/app/app-constants.d.mts +2 -0
- package/dist/witness/app/app-constants.d.mts.map +1 -1
- package/dist/witness/app/app-constants.mjs +2 -0
- package/dist/witness/app/app-constants.mjs.map +1 -1
- package/dist/witness/app/app-helper.d.mts.map +1 -1
- package/dist/witness/app/app-helper.mjs +2 -1
- package/dist/witness/app/app-helper.mjs.map +1 -1
- package/dist/witness/app/app-types.d.mts +2 -1
- package/dist/witness/app/app-types.d.mts.map +1 -1
- package/dist/witness/robbot/robbot-base-v1.d.mts +12 -164
- package/dist/witness/robbot/robbot-base-v1.d.mts.map +1 -1
- package/dist/witness/robbot/robbot-base-v1.mjs +41 -657
- package/dist/witness/robbot/robbot-base-v1.mjs.map +1 -1
- package/dist/witness/robbot/robbot-helper.mjs +3 -3
- package/dist/witness/robbot/robbot-helper.mjs.map +1 -1
- package/dist/witness/robbot/robbot-helper.respec.d.mts +2 -0
- package/dist/witness/robbot/robbot-helper.respec.d.mts.map +1 -0
- package/dist/witness/robbot/robbot-helper.respec.mjs +106 -0
- package/dist/witness/robbot/robbot-helper.respec.mjs.map +1 -0
- package/dist/witness/robbot/robbot-types.d.mts +6 -3
- package/dist/witness/robbot/robbot-types.d.mts.map +1 -1
- package/dist/witness/robbot/robbot-types.mjs +1 -0
- package/dist/witness/robbot/robbot-types.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/filesystem-space-v1.d.mts +1 -1
- package/dist/witness/space/filesystem-space/filesystem-space-v1.d.mts.map +1 -1
- package/dist/witness/space/filesystem-space/filesystem-space-v1.respec.d.mts +2 -0
- package/dist/witness/space/filesystem-space/filesystem-space-v1.respec.d.mts.map +1 -0
- package/dist/witness/space/filesystem-space/filesystem-space-v1.respec.mjs +107 -0
- package/dist/witness/space/filesystem-space/filesystem-space-v1.respec.mjs.map +1 -0
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.d.mts.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.mjs +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.mjs.map +1 -1
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.node.respec.d.mts +2 -0
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.node.respec.d.mts.map +1 -0
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.node.respec.mjs +129 -0
- package/dist/witness/space/filesystem-space/node-filesystem-space-v1.node.respec.mjs.map +1 -0
- package/dist/witness/space/inner-space/inner-space-v1.respec.d.mts +2 -0
- package/dist/witness/space/inner-space/inner-space-v1.respec.d.mts.map +1 -0
- package/dist/witness/space/inner-space/inner-space-v1.respec.mjs +56 -0
- package/dist/witness/space/inner-space/inner-space-v1.respec.mjs.map +1 -0
- package/dist/witness/space/metaspace/metaspace-base.d.mts +795 -0
- package/dist/witness/space/metaspace/metaspace-base.d.mts.map +1 -0
- package/dist/witness/space/metaspace/metaspace-base.mjs +3251 -0
- package/dist/witness/space/metaspace/metaspace-base.mjs.map +1 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.d.mts +4 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.d.mts.map +1 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs +117 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mjs.map +1 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.d.mts +34 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.d.mts.map +1 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs +76 -0
- package/dist/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mjs.map +1 -0
- package/dist/witness/space/metaspace/metaspace-types.d.mts +580 -0
- package/dist/witness/space/metaspace/metaspace-types.d.mts.map +1 -0
- package/dist/witness/space/metaspace/metaspace-types.mjs +6 -0
- package/dist/witness/space/metaspace/metaspace-types.mjs.map +1 -0
- package/dist/witness/space/space-helper.d.mts.map +1 -1
- package/dist/witness/space/space-helper.mjs +2 -1
- package/dist/witness/space/space-helper.mjs.map +1 -1
- package/dist/witness/space/{space-spec-helper.d.mts → space-respec-helper.d.mts} +15 -5
- package/dist/witness/space/space-respec-helper.d.mts.map +1 -0
- package/dist/witness/space/space-respec-helper.mjs +186 -0
- package/dist/witness/space/space-respec-helper.mjs.map +1 -0
- package/dist/witness/space/space-types.d.mts +2 -1
- package/dist/witness/space/space-types.d.mts.map +1 -1
- package/dist/witness/space/space-types.mjs.map +1 -1
- package/dist/witness/witness-base-v1.d.mts.map +1 -1
- package/dist/witness/witness-base-v1.mjs +7 -7
- package/dist/witness/witness-base-v1.mjs.map +1 -1
- package/dist/witness/witness-cmd/witness-cmd-types.d.mts +31 -0
- package/dist/witness/witness-cmd/witness-cmd-types.d.mts.map +1 -0
- package/dist/witness/witness-cmd/witness-cmd-types.mjs +2 -0
- package/dist/witness/witness-cmd/witness-cmd-types.mjs.map +1 -0
- package/dist/witness/witness-helper.d.mts +8 -0
- package/dist/witness/witness-helper.d.mts.map +1 -1
- package/dist/witness/witness-helper.mjs +30 -0
- package/dist/witness/witness-helper.mjs.map +1 -1
- package/dist/witness/witness-types.d.mts +26 -36
- package/dist/witness/witness-types.d.mts.map +1 -1
- package/dist/witness/witness-with-context/witness-with-context-base-v1.d.mts +235 -0
- package/dist/witness/witness-with-context/witness-with-context-base-v1.d.mts.map +1 -0
- package/dist/witness/witness-with-context/witness-with-context-base-v1.mjs +742 -0
- package/dist/witness/witness-with-context/witness-with-context-base-v1.mjs.map +1 -0
- package/dist/witness/witness-with-context/witness-with-context-types.d.mts +17 -0
- package/dist/witness/witness-with-context/witness-with-context-types.d.mts.map +1 -0
- package/dist/witness/witness-with-context/witness-with-context-types.mjs +2 -0
- package/dist/witness/witness-with-context/witness-with-context-types.mjs.map +1 -0
- package/package.json +12 -15
- package/src/{assumptions.spec.mts → assumptions.respec.mts} +11 -5
- package/src/core-constants.mts +0 -2
- package/src/core-helper.respec.mts +71 -0
- package/src/core-types.mts +20 -0
- package/src/respec-gib.node.mts +199 -0
- package/src/witness/app/app-base-v1.mts +19 -212
- package/src/witness/app/app-constants.mts +3 -0
- package/src/witness/app/app-helper.mts +2 -1
- package/src/witness/app/app-types.mts +1 -1
- package/src/witness/robbot/robbot-base-v1.mts +39 -690
- package/src/witness/robbot/robbot-helper.mts +1 -1
- package/src/witness/robbot/{robbot-helper.spec.mts → robbot-helper.respec.mts} +39 -33
- package/src/witness/robbot/robbot-types.mts +5 -3
- package/src/witness/space/filesystem-space/filesystem-space-v1.mts +1 -1
- package/src/witness/space/filesystem-space/{filesystem-space-v1.spec.mts → filesystem-space-v1.respec.mts} +17 -8
- package/src/witness/space/filesystem-space/node-filesystem-space-v1.mts +3 -11
- package/src/witness/space/filesystem-space/{node-filesystem-space-v1.node.spec.mts → node-filesystem-space-v1.node.respec.mts} +18 -14
- package/src/witness/space/inner-space/{inner-space-v1.spec.mts → inner-space-v1.respec.mts} +17 -8
- package/src/witness/space/metaspace/metaspace-base.mts +3702 -0
- package/src/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace-helper.mts +116 -0
- package/src/witness/space/metaspace/metaspace-innerspace/metaspace-innerspace.mts +88 -0
- package/src/witness/space/metaspace/metaspace-types.mts +614 -0
- package/src/witness/space/space-helper.mts +2 -1
- package/src/witness/space/{space-spec-helper.mts → space-respec-helper.mts} +76 -57
- package/src/witness/space/space-types.mts +2 -1
- package/src/witness/witness-base-v1.mts +8 -6
- package/src/witness/witness-cmd/witness-cmd-types.mts +38 -0
- package/src/witness/witness-helper.mts +31 -0
- package/src/witness/witness-types.mts +31 -41
- package/src/witness/witness-with-context/witness-with-context-base-v1.mts +809 -0
- package/src/witness/witness-with-context/witness-with-context-types.mts +24 -0
- package/tsconfig.json +1 -0
- package/dist/witness/space/space-spec-helper.d.mts.map +0 -1
- package/dist/witness/space/space-spec-helper.mjs +0 -179
- package/dist/witness/space/space-spec-helper.mjs.map +0 -1
- package/jasmine-browser.json +0 -18
- package/jasmine.json +0 -6
- package/src/core-helper.spec.mts +0 -64
- /package/src/{spec-helper.node.spec.mts → spec-helper.node.respec.mts} +0 -0
|
@@ -0,0 +1,809 @@
|
|
|
1
|
+
import { Subscription } from 'rxjs/index.js';
|
|
2
|
+
import { filter } from 'rxjs/operators';
|
|
3
|
+
|
|
4
|
+
import { Lex, clone, delay, getSaferSubstring, getTimestamp, getUUID, pretty } from '@ibgib/helper-gib';
|
|
5
|
+
import { Gib, Ib, IbGibAddr, V1, getIbGibAddr } from '@ibgib/ts-gib';
|
|
6
|
+
import { getGibInfo, IbGib_V1, IbGibRel8ns_V1 } from '@ibgib/ts-gib/dist/V1/index.mjs';
|
|
7
|
+
import { validateIbGibIntrinsically } from '@ibgib/ts-gib/dist/V1/validate-helper.mjs';
|
|
8
|
+
|
|
9
|
+
import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs';
|
|
10
|
+
import { WitnessBase_V1, } from '../witness-base-v1.mjs';
|
|
11
|
+
import { CommentIbGib_V1 } from '../../common/comment/comment-types.mjs';
|
|
12
|
+
import { PicIbGib_V1 } from '../../common/pic/pic-types.mjs';
|
|
13
|
+
import { argy_, isArg, resulty_ } from '../witness-helper.mjs';
|
|
14
|
+
import { IbGibLocalSpaceService_AllPowerful } from '../../core-types.mjs';
|
|
15
|
+
import { ErrorIbGib_V1 } from '../../common/error/error-types.mjs';
|
|
16
|
+
import { errorIbGib } from '../../common/error/error-helper.mjs';
|
|
17
|
+
import { createCommentIbGib, parseCommentIb } from '../../common/comment/comment-helper.mjs';
|
|
18
|
+
import { getFromSpace, getLatestAddrs } from '../space/space-helper.mjs';
|
|
19
|
+
import { getTjpAddr } from '../../common/other/ibgib-helper.mjs';
|
|
20
|
+
import { IbGibTimelineUpdateInfo } from '../../common/other/other-types.mjs';
|
|
21
|
+
import { WitnessArgIbGib, WitnessData_V1, WitnessRel8ns_V1 } from '../witness-types.mjs';
|
|
22
|
+
import { WitnessWithContextData_V1, WitnessWithContextRel8ns_V1 } from './witness-with-context-types.mjs';
|
|
23
|
+
|
|
24
|
+
const logalot = GLOBAL_LOG_A_LOT || true;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* ## distinguishing characteristics of robbots
|
|
28
|
+
*
|
|
29
|
+
* With any witness ibgib, we are concerned with interpreting an incoming
|
|
30
|
+
* arg ibgib and producing at least one result ibgib. Most often, there will
|
|
31
|
+
* be additional ibgibs created, either creating "new" ibgibs via `fork`
|
|
32
|
+
* transforms, or changing existing ibgibs through `mut8` and `rel8`
|
|
33
|
+
* transforms. And almost always these will be persisted in at least
|
|
34
|
+
* one space.
|
|
35
|
+
*
|
|
36
|
+
* But Robbots in particular are meant to be increasingly adaptable over time.
|
|
37
|
+
*
|
|
38
|
+
* This results in a couple of notes:
|
|
39
|
+
*
|
|
40
|
+
* 1. They should be able to handle any incoming ibgib, including primitives.
|
|
41
|
+
* 2. If they are learning robbots, then they will mutate internally at some rate.
|
|
42
|
+
* 3. Often robbots' output ibgib(s) will be intended for others'
|
|
43
|
+
* consumption, in service of others - be they humans or other
|
|
44
|
+
* biologically living organisms, other robbots or even later versions of
|
|
45
|
+
* themselves.
|
|
46
|
+
*
|
|
47
|
+
* So for example, one of the simplest robbots is one which simply echos the
|
|
48
|
+
* incoming ibgib arg.
|
|
49
|
+
|
|
50
|
+
* ## architecture
|
|
51
|
+
*
|
|
52
|
+
* At the base, any robbot should be able to handle any incoming ibgib. This is
|
|
53
|
+
* in contrast to, e.g., a Space, which is more rigid in design.
|
|
54
|
+
*
|
|
55
|
+
* Certainly there are robbot's who will be more rigid, but this will be an
|
|
56
|
+
* abstract class on top of this one, and all robbots should be able to react to
|
|
57
|
+
* all incoming ibgibs.
|
|
58
|
+
*
|
|
59
|
+
* Output will default to a simple ok^gib or (ROOT) ib^gib primitive...perhaps
|
|
60
|
+
* we'll go with ack^gib, but whatever it is, it's just an acknowledgement of
|
|
61
|
+
* input received. (I'll put it in the constants file).
|
|
62
|
+
*
|
|
63
|
+
* Side effects should occur in a parallel execution thread, which ideally would
|
|
64
|
+
* work in a completely parallel execution context (like a service worker). But
|
|
65
|
+
* then we have to deal with race conditions and the real solution there is to
|
|
66
|
+
* abstract to the robbot having its own space and the synchronization happening
|
|
67
|
+
* exactly like any other sync space.
|
|
68
|
+
*
|
|
69
|
+
* For now, we'll spin off a promise with some intermittent `await delay`
|
|
70
|
+
* calls if they end up called for, effectively the equivalent of the
|
|
71
|
+
* old-fashioned "ProcessMessages/DoEvents" hack.
|
|
72
|
+
*/
|
|
73
|
+
export abstract class WitnessWithContextBase_V1<
|
|
74
|
+
TOptionsData extends any = any,
|
|
75
|
+
TOptionsRel8ns extends IbGibRel8ns_V1 = IbGibRel8ns_V1,
|
|
76
|
+
TOptionsIbGib extends IbGib_V1<TOptionsData, TOptionsRel8ns>
|
|
77
|
+
= IbGib_V1<TOptionsData, TOptionsRel8ns>,
|
|
78
|
+
TResultData extends any = any,
|
|
79
|
+
TResultRel8ns extends IbGibRel8ns_V1 = IbGibRel8ns_V1,
|
|
80
|
+
TResultIbGib extends IbGib_V1<TResultData, TResultRel8ns> | ErrorIbGib_V1
|
|
81
|
+
= IbGib_V1<TResultData, TResultRel8ns>,
|
|
82
|
+
TData extends WitnessWithContextData_V1 = WitnessWithContextData_V1,
|
|
83
|
+
TRel8ns extends WitnessWithContextRel8ns_V1 = WitnessWithContextRel8ns_V1,
|
|
84
|
+
> extends WitnessBase_V1<
|
|
85
|
+
TOptionsData, TOptionsRel8ns, TOptionsIbGib,
|
|
86
|
+
TResultData, TResultRel8ns, TResultIbGib,
|
|
87
|
+
TData, TRel8ns> {
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Log context for convenience with logging. (Ignore if you don't want to use this.)
|
|
91
|
+
*
|
|
92
|
+
* Often used in conjunction with `logalot`.
|
|
93
|
+
*/
|
|
94
|
+
protected lc: string = `[${WitnessWithContextBase_V1.name}]`;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Reference to the local ibgibs service, which is one way at getting at the
|
|
98
|
+
* local user space.
|
|
99
|
+
*/
|
|
100
|
+
public ibgibsSvc: IbGibLocalSpaceService_AllPowerful | undefined;
|
|
101
|
+
|
|
102
|
+
protected _contextChangesSubscription: Subscription | undefined;
|
|
103
|
+
protected _currentWorkingContextIbGib: IbGib_V1 | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* when we get an update to the context, we want to know what the _new_
|
|
106
|
+
* children are in order to interpret comments from the user that may be
|
|
107
|
+
* directed at us.
|
|
108
|
+
*
|
|
109
|
+
* So we will get an initial snapshot of children that we will diff against.
|
|
110
|
+
* We could go via the dna, but ultimately a diff is what is needed.
|
|
111
|
+
*/
|
|
112
|
+
protected _currentWorkingContextIbGib_PriorChildrenAddrs: IbGibAddr[] = [];
|
|
113
|
+
|
|
114
|
+
protected _updatingContext: boolean = false;
|
|
115
|
+
|
|
116
|
+
constructor(initialData?: TData, initialRel8ns?: TRel8ns) {
|
|
117
|
+
super(initialData, initialRel8ns);
|
|
118
|
+
this.initialized = this.initialize();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
protected async initialize(): Promise<void> {
|
|
122
|
+
const lc = `${this.lc}[${this.initialize.name}]`;
|
|
123
|
+
try {
|
|
124
|
+
if (logalot) { console.log(`${lc} starting... (I: f0e65ab0f80046a59668ddfbf9f47a4a5)`); }
|
|
125
|
+
await super.initialize();
|
|
126
|
+
|
|
127
|
+
// await this.initialize_semanticHandlers();
|
|
128
|
+
// await this.initialize_lex();
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error(`${lc} ${error.message}`);
|
|
131
|
+
} finally {
|
|
132
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
protected async loadNewerSelfIfAvailable(): Promise<void> {
|
|
137
|
+
const lc = `${this.lc}[${this.loadNewerSelfIfAvailable.name}]`;
|
|
138
|
+
try {
|
|
139
|
+
if (logalot) { console.log(`${lc} starting... (I: 94755c3131f4dfa12d20fa38e2926522)`); }
|
|
140
|
+
if (this.ibgibsSvc) {
|
|
141
|
+
// check for newer version of self locally before executing
|
|
142
|
+
const thisAddr = getIbGibAddr({ ibGib: this });
|
|
143
|
+
const latestAddr = await this.ibgibsSvc.getLatestAddr({ ibGib: this });
|
|
144
|
+
if (latestAddr && latestAddr !== thisAddr) {
|
|
145
|
+
// this has a newer ibgib in its timeline
|
|
146
|
+
let resGet = await this.ibgibsSvc.get({ addr: latestAddr });
|
|
147
|
+
if (!resGet || !resGet?.success || (resGet?.ibGibs ?? []).length === 0) {
|
|
148
|
+
throw new Error(`could not get newer ibgib in timeline (E: 15fa346c8ac17edb96e4b0870104c122)`);
|
|
149
|
+
}
|
|
150
|
+
await this.loadIbGibDto(resGet.ibGibs![0] as IbGib_V1<TData, TRel8ns>);
|
|
151
|
+
const validationErrors = await this.validateThis();
|
|
152
|
+
if (validationErrors?.length > 0) { throw new Error(`validationErrors when loading newer version: ${pretty(validationErrors)} (E: 0d9f0684a1ff6af44e20a57130e3ac22)`); }
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
console.warn(`${lc} this.ibgibsSvc undefined (W: 44cc5bf1b14b4695b8de4c589787be06)`);
|
|
156
|
+
}
|
|
157
|
+
} catch (error) {
|
|
158
|
+
console.error(`${lc} ${error.message}`);
|
|
159
|
+
throw error;
|
|
160
|
+
} finally {
|
|
161
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* By default, this...
|
|
167
|
+
*
|
|
168
|
+
* * performs the raw {@link rel8} transform to the given `ibGib`.
|
|
169
|
+
* * persists the new ibgib's transform result in the given space.
|
|
170
|
+
* * registers the newer version of this robbot ibgib with the ibgibs svc.
|
|
171
|
+
*
|
|
172
|
+
* @see {@link ibGibs}
|
|
173
|
+
* @see {@link rel8nName}
|
|
174
|
+
* @see {@link ibgibsSvc}
|
|
175
|
+
* @see {@link space}
|
|
176
|
+
*
|
|
177
|
+
* ## notes
|
|
178
|
+
*
|
|
179
|
+
* * If there is no given `space`, then we will use the `ibgibsSvc` to get
|
|
180
|
+
* the local user space. If none, then we skip persistence.
|
|
181
|
+
* * If there is no `ibgibsSvc`, we won't register the new ibgibs locally.
|
|
182
|
+
*/
|
|
183
|
+
protected async rel8To({
|
|
184
|
+
ibGibs,
|
|
185
|
+
rel8nName,
|
|
186
|
+
linked,
|
|
187
|
+
ibgibsSvc,
|
|
188
|
+
// space,
|
|
189
|
+
}: {
|
|
190
|
+
/**
|
|
191
|
+
* The ibgib to which we are relating.
|
|
192
|
+
*/
|
|
193
|
+
ibGibs: IbGib_V1[],
|
|
194
|
+
/**
|
|
195
|
+
* If given, will use this as the rel8n name when performing the `rel8`
|
|
196
|
+
* transform.
|
|
197
|
+
*
|
|
198
|
+
* If not given, will use the `robbot.data`'s {@link RobbotData_V1.defaultRel8nName} value.
|
|
199
|
+
*/
|
|
200
|
+
rel8nName?: string,
|
|
201
|
+
/**
|
|
202
|
+
* If true, will include the `rel8nName` as a linked rel8n in the `rel8`
|
|
203
|
+
* transform.
|
|
204
|
+
*/
|
|
205
|
+
linked?: boolean,
|
|
206
|
+
/**
|
|
207
|
+
* If provided, will register the newly created ibgib.
|
|
208
|
+
*/
|
|
209
|
+
ibgibsSvc?: IbGibLocalSpaceService_AllPowerful,
|
|
210
|
+
// /**
|
|
211
|
+
// * If given (which atow is most likely the case), then the {@link TransformResult} will
|
|
212
|
+
// * be persisted in this `space`.
|
|
213
|
+
// */
|
|
214
|
+
// space?: IbGibSpaceAny,
|
|
215
|
+
}): Promise<void> {
|
|
216
|
+
const lc = `${this.lc}[${this.rel8To.name}]`;
|
|
217
|
+
try {
|
|
218
|
+
if (logalot) { console.log(`${lc} starting...`); }
|
|
219
|
+
|
|
220
|
+
// #region initialize, validate args and this
|
|
221
|
+
|
|
222
|
+
if ((ibGibs ?? []).length === 0) { throw new Error(`ibGibs required (E: 2fd13de0f025b170885bede4d7a50922)`); }
|
|
223
|
+
|
|
224
|
+
rel8nName = rel8nName || this.data?.defaultRel8nName;
|
|
225
|
+
if (!rel8nName) { throw new Error(`rel8nName required either as an arg or in this.data.defaultRel8nName (E: 43ab8ae63694a2a82cd8a70ed6b6b522)`); }
|
|
226
|
+
|
|
227
|
+
const thisValidationErrors = await this.validateThis();
|
|
228
|
+
if (thisValidationErrors?.length > 0) { throw new Error(`this is an invalid ibGib. thisValidationErrors: ${thisValidationErrors.join('|')} (E: 8f08716866cd13bf254222ee9e6a6722)`); }
|
|
229
|
+
|
|
230
|
+
ibgibsSvc = ibgibsSvc ?? this.ibgibsSvc;
|
|
231
|
+
|
|
232
|
+
if (!ibgibsSvc) {
|
|
233
|
+
// if (this.ibgibsSvc) {
|
|
234
|
+
// if (logalot) { console.log(`${lc} ibgibsSvc arg falsy, but we have a reference on this object, which we will use. (I: ee0d39a47ee8aee8ffd797721fea4322)`); }
|
|
235
|
+
// ibgibsSvc = this.ibgibsSvc;
|
|
236
|
+
// }
|
|
237
|
+
throw new Error(`either ibgibsSvc or this.ibgibsSvc required (E: b5f9453ddb394a2b76dec74c7304df22)`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// if (!space) {
|
|
241
|
+
// if (ibgibsSvc) {
|
|
242
|
+
// if (logalot) { console.log(`${lc} space arg falsy, but ibgibsSvc truthy, so we'll use ibgibsSvc's local user space for persistence. (I: 37a4b4c1406556cb23831671755b0d22)`); }
|
|
243
|
+
// space = await ibgibsSvc.getLocalUserSpace({ lock: true });
|
|
244
|
+
// }
|
|
245
|
+
// }
|
|
246
|
+
|
|
247
|
+
// if (!space) { throw new Error(`(UNEXPECTED) space required and ibgibsSvc couldn't get it? (E: a3b9f9b72f6f6f18883199a19d38c622)`); }
|
|
248
|
+
|
|
249
|
+
// #endregion initialize, validate args and this
|
|
250
|
+
|
|
251
|
+
// we want to rel8 only to the ibGibs whose timelines we're not
|
|
252
|
+
// already related to. So we look to see if we already have the tjpGib
|
|
253
|
+
// per our rel8nName.
|
|
254
|
+
// if (!this.rel8ns) { this.rel8ns = {} as any };
|
|
255
|
+
const alreadyRel8dAddrs = this.rel8ns ? this.rel8ns[rel8nName] ?? [] : [];
|
|
256
|
+
const alreadyRel8dTjpGibs = alreadyRel8dAddrs.map(x => getGibInfo({ ibGibAddr: x }).tjpGib);
|
|
257
|
+
const ibGibsNotYetRel8dByTjp = ibGibs.filter(x => {
|
|
258
|
+
const tjpGib = getGibInfo({ ibGibAddr: getIbGibAddr({ ibGib: x }) }).tjpGib;
|
|
259
|
+
return !alreadyRel8dTjpGibs.includes(tjpGib);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
if (ibGibsNotYetRel8dByTjp.length === 0) {
|
|
263
|
+
if (logalot) { console.log(`${lc} already rel8d to all incoming ibGib(s) via tjp. (I: 5e9d94a98ba262f146c0c0b765157922)`); }
|
|
264
|
+
return; /* <<<< returns early */
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// perform the raw ibgib rel8 transform
|
|
268
|
+
const addrs = ibGibsNotYetRel8dByTjp.map(x => getIbGibAddr({ ibGib: x }));
|
|
269
|
+
const resNewThis = await V1.rel8({
|
|
270
|
+
src: this.toIbGibDto(),
|
|
271
|
+
rel8nsToAddByAddr: { [rel8nName]: addrs },
|
|
272
|
+
linkedRel8ns: linked ? ["past", "ancestor", rel8nName] : ["past", "ancestor"],
|
|
273
|
+
dna: true,
|
|
274
|
+
nCounter: true,
|
|
275
|
+
});
|
|
276
|
+
const newThisIbGib = resNewThis.newIbGib as any as IbGib_V1<TData, TRel8ns>;
|
|
277
|
+
const newThisValidationErrors =
|
|
278
|
+
await validateIbGibIntrinsically({ ibGib: newThisIbGib as any });
|
|
279
|
+
if (newThisValidationErrors?.length ?? 0 > 0) { throw new Error(`new this ibgib would have validation errors. aborting. newThisValidationErrors: ${newThisValidationErrors!.join('|')} (E: eb816a27156c246c121ef55e37d59322)`); }
|
|
280
|
+
|
|
281
|
+
// if space is given, perform the persistence
|
|
282
|
+
// if (space) {
|
|
283
|
+
await ibgibsSvc.persistTransformResult({ resTransform: resNewThis });
|
|
284
|
+
// } else {
|
|
285
|
+
// if (logalot) { console.log(`${lc} space falsy, skipping persistence (I: 90aa3553e92ad1d02bce61f83648ea22)`); }
|
|
286
|
+
// }
|
|
287
|
+
|
|
288
|
+
// (in the future, need to revisit the ibgibs service to the idea of locality/ies).
|
|
289
|
+
await ibgibsSvc.registerNewIbGib({ ibGib: newThisIbGib });
|
|
290
|
+
|
|
291
|
+
// update this witness' primary ibGib properties (ib, gib, data, rel8ns).
|
|
292
|
+
// override `loadIbGibDto` to update secondary/derivative properties
|
|
293
|
+
await this.loadIbGibDto(newThisIbGib);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
console.error(`${lc} ${error.message}`);
|
|
296
|
+
throw error;
|
|
297
|
+
} finally {
|
|
298
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
protected async rel8ToContextIbGib({
|
|
303
|
+
ibGibToRel8,
|
|
304
|
+
ibGibAddrToRel8,
|
|
305
|
+
contextIbGib,
|
|
306
|
+
rel8nNames,
|
|
307
|
+
// space,
|
|
308
|
+
}: {
|
|
309
|
+
ibGibToRel8?: IbGib_V1,
|
|
310
|
+
ibGibAddrToRel8?: IbGibAddr,
|
|
311
|
+
contextIbGib: IbGib_V1,
|
|
312
|
+
rel8nNames: string[],
|
|
313
|
+
// space?: IbGibSpaceAny,
|
|
314
|
+
}): Promise<void> {
|
|
315
|
+
const lc = `${this.lc}[${this.rel8ToContextIbGib.name}]`;
|
|
316
|
+
try {
|
|
317
|
+
if (!ibGibToRel8 && !ibGibAddrToRel8) { throw new Error(`ibGibToRel8 or ibGibAddrToRel8 required (E: 3ee14659fd22355a5ba0e537a477be22)`); }
|
|
318
|
+
if (!contextIbGib) { throw new Error(`contextIbGib required (E: 85f27c7cbf713704c21084c141cd8822)`); }
|
|
319
|
+
if (!this.ibgibsSvc) { throw new Error(`this.ibgibsSvc required (E: 6a38c4274bdefc8d44cafd2d6faaa222)`); }
|
|
320
|
+
|
|
321
|
+
// space = space ?? await this.ibgibsSvc.getLocalUserSpace({ lock: true });
|
|
322
|
+
// if (!space) { throw new Error(`space required (E: 267ad87c148942cda641349df0bbbd22)`); }
|
|
323
|
+
|
|
324
|
+
if ((rel8nNames ?? []).length === 0) {
|
|
325
|
+
if (!this.data?.defaultRel8nName) { throw new Error(`either rel8nNames or this.data.defaultRel8nName required (E: a14ab4b3e479d9274c61bc5a30bc2222)`); }
|
|
326
|
+
rel8nNames = [this.data.defaultRel8nName];
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// set up the rel8ns to add
|
|
330
|
+
const rel8nsToAddByAddr: IbGibRel8ns_V1 = {};
|
|
331
|
+
ibGibAddrToRel8 = ibGibAddrToRel8 || getIbGibAddr({ ibGib: ibGibToRel8 });
|
|
332
|
+
rel8nNames.forEach((rel8nName) => { rel8nsToAddByAddr[rel8nName] = [ibGibAddrToRel8!]; });
|
|
333
|
+
|
|
334
|
+
// perform the rel8 transform and...
|
|
335
|
+
const resRel8ToContext =
|
|
336
|
+
await V1.rel8({
|
|
337
|
+
src: contextIbGib,
|
|
338
|
+
rel8nsToAddByAddr,
|
|
339
|
+
dna: true,
|
|
340
|
+
nCounter: true
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// ...persist it...
|
|
344
|
+
await this.ibgibsSvc.persistTransformResult({ resTransform: resRel8ToContext });
|
|
345
|
+
|
|
346
|
+
// ...register the context.
|
|
347
|
+
const { newIbGib: newContext } = resRel8ToContext;
|
|
348
|
+
await this.ibgibsSvc.registerNewIbGib({ ibGib: newContext });
|
|
349
|
+
} catch (error) {
|
|
350
|
+
console.error(`${lc} ${error.message}`);
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
protected async createCommentAndRel8ToContextIbGib({
|
|
356
|
+
text,
|
|
357
|
+
contextIbGib,
|
|
358
|
+
rel8nName,
|
|
359
|
+
}: {
|
|
360
|
+
text: string,
|
|
361
|
+
contextIbGib: IbGib_V1,
|
|
362
|
+
rel8nName: string,
|
|
363
|
+
ibgibsSvc?: IbGibLocalSpaceService_AllPowerful,
|
|
364
|
+
}): Promise<void> {
|
|
365
|
+
const lc = `${this.lc}[${this.createCommentAndRel8ToContextIbGib.name}]`;
|
|
366
|
+
try {
|
|
367
|
+
if (logalot) { console.log(`${lc} starting... (I: c3a005f7d323468a5b4e1b2710901d22)`); }
|
|
368
|
+
|
|
369
|
+
if (!this.ibgibsSvc) { throw new Error(`this.ibgibsSvc required (E: 5dbb1a7f0ff5469b8ce3cb1be175e521)`); }
|
|
370
|
+
|
|
371
|
+
// space = space ?? await this.ibgibsSvc.getLocalUserSpace({ lock: true });
|
|
372
|
+
// if (!space) { throw new Error(`(UNEXPECTED) space required and wasn't able to get it from ibgibsSvc? (E: 7159f9893a66c28a7e09b61384545622)`); }
|
|
373
|
+
let space = await this.ibgibsSvc.getLocalUserSpace({ lock: true });
|
|
374
|
+
|
|
375
|
+
/** tag this comment with metadata to show it came from this robbot */
|
|
376
|
+
let resComment = await createCommentIbGib({ text, addlMetadataText: this.getAddlMetadata(), saveInSpace: true, space });
|
|
377
|
+
|
|
378
|
+
// get again to be sure it's the latest space.
|
|
379
|
+
space = await this.ibgibsSvc.getLocalUserSpace({ lock: true });
|
|
380
|
+
|
|
381
|
+
const commentIbGib = resComment.newIbGib as CommentIbGib_V1;
|
|
382
|
+
if (!commentIbGib) { throw new Error(`(UNEXPECTED) failed to create comment? (E: 6d668f4e55198e654324622eabaac922)`); }
|
|
383
|
+
await this.ibgibsSvc.registerNewIbGib({ ibGib: commentIbGib });
|
|
384
|
+
|
|
385
|
+
await this.rel8ToContextIbGib({ ibGibToRel8: commentIbGib, contextIbGib, rel8nNames: ['comment'] });
|
|
386
|
+
await this.rel8To({
|
|
387
|
+
ibGibs: [commentIbGib],
|
|
388
|
+
rel8nName,
|
|
389
|
+
});
|
|
390
|
+
} catch (error) {
|
|
391
|
+
console.error(`${lc} ${error.message}`);
|
|
392
|
+
throw error;
|
|
393
|
+
} finally {
|
|
394
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* I'm abstracting getAddlMetadata and the first part is atow "robbot",
|
|
400
|
+
* e.g., * `robbot_classname_id_etc`. So "ThisAtom" is "robbot" for a
|
|
401
|
+
* robbot.
|
|
402
|
+
*
|
|
403
|
+
* override this in descending classes per use case.
|
|
404
|
+
*
|
|
405
|
+
* by default, this returns a safe substring of this.data.classname or "this".
|
|
406
|
+
*/
|
|
407
|
+
protected getThisAtom(): string {
|
|
408
|
+
return getSaferSubstring({ text: this.data?.classname ?? 'this', length: 16 });
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* used to help identify when comments are made by descending witnesses.
|
|
413
|
+
*
|
|
414
|
+
* override this with your particular use case's addl metadata.
|
|
415
|
+
*
|
|
416
|
+
* @example atow robbot is: `robbot_${this.data.classname}_${this.data.name}_${this.data.uuid.slice(0, 8)}`;
|
|
417
|
+
*
|
|
418
|
+
* @returns addlmetadata string to be used in comment ib's
|
|
419
|
+
*/
|
|
420
|
+
protected getAddlMetadata(): string {
|
|
421
|
+
const lc = `${this.lc}[${this.getAddlMetadata.name}]`;
|
|
422
|
+
try {
|
|
423
|
+
if (logalot) { console.log(`${lc} starting... (I: 7800732facf3783943fdf1b2423b0c22)`); }
|
|
424
|
+
if (!this.data) { throw new Error(`(UNEXPECTED) this.data falsy? (E: 7cb044b39b2b8e31f363f3b82f256323)`); }
|
|
425
|
+
const atom = this.getThisAtom();
|
|
426
|
+
if (!atom) { throw new Error(`(UNEXPECTED) atom couldn't be gotten? (E: b260de391356e04abd334e0cd8b09123)`); }
|
|
427
|
+
if (atom.includes(' ')) { throw new Error(`(UNEXPECTED) atom contains space? (E: 574d088eea13d3afe5fcd2999ab6b423)`); }
|
|
428
|
+
const classnameIsh = this.data.classname?.replace(/[_]/mg, '') || undefined;
|
|
429
|
+
const nameIsh = this.data.name?.slice(0, 8).replace(/[__]/mg, '') || undefined;
|
|
430
|
+
const idIsh = this.data.uuid?.slice(0, 8) || undefined;
|
|
431
|
+
const addlMetadataText = `${atom}_${classnameIsh}_${nameIsh}_${idIsh}`;
|
|
432
|
+
if (logalot) { console.log(`${lc} addlMetadataText: ${addlMetadataText} (I: 53c0044671dc99fb75635367d2e63c22)`); }
|
|
433
|
+
return addlMetadataText;
|
|
434
|
+
} catch (error) {
|
|
435
|
+
console.error(`${lc} ${error.message}`);
|
|
436
|
+
throw error;
|
|
437
|
+
} finally {
|
|
438
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* override this in concrete class for addlMetadata used (if used)...
|
|
444
|
+
*
|
|
445
|
+
* Also be sure to override `getAddlMetadata` (if used)
|
|
446
|
+
*
|
|
447
|
+
* ## notes
|
|
448
|
+
*
|
|
449
|
+
* I'm refactoring and this may need to come out, but it's used in RobbotBase atow.
|
|
450
|
+
*/
|
|
451
|
+
protected abstract parseAddlMetadataString<TParseResult>({ ib }: { ib: Ib }): TParseResult;
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* I originally created this just to extract the context from the arg, but
|
|
455
|
+
* I'm reusing it to get the latest context from the addr alone.
|
|
456
|
+
*/
|
|
457
|
+
protected async getContextIbGibFromArgOrAddr({
|
|
458
|
+
arg,
|
|
459
|
+
addr,
|
|
460
|
+
latest,
|
|
461
|
+
}: {
|
|
462
|
+
arg?: WitnessArgIbGib<IbGib_V1, TData, TRel8ns>,
|
|
463
|
+
addr?: IbGibAddr,
|
|
464
|
+
/**
|
|
465
|
+
* if true, after extracting the context from the arg, will get the
|
|
466
|
+
* latest ibgib (if there is a newer version).
|
|
467
|
+
*/
|
|
468
|
+
latest?: boolean,
|
|
469
|
+
}): Promise<IbGib_V1> {
|
|
470
|
+
const lc = `${this.lc}[${this.getContextIbGibFromArgOrAddr.name}]`;
|
|
471
|
+
try {
|
|
472
|
+
if (logalot) { console.log(`${lc} starting... (I: c13f7cb92133984048f606075efb8a22)`); }
|
|
473
|
+
let contextIbGib: IbGib_V1;
|
|
474
|
+
if (!arg && !addr) { throw new Error(`either arg or addr required. (E: 3f647b65742242fd9ba878521acf7c22)`); }
|
|
475
|
+
if (!this.ibgibsSvc) { throw new Error(`(UNEXPECTED) this.ibgibsSvc required (E: f0046290b0d66d28c4bbbf83d9d9f523)`); }
|
|
476
|
+
if (arg) {
|
|
477
|
+
if ((arg.ibGibs ?? []).length === 0) { throw new Error(`(UNEXPECTED) invalid arg? no context ibgib on arg (E: 89997eb4bdeb3885bee9de5d33ee0f22)`); }
|
|
478
|
+
if ((arg.ibGibs ?? []).length !== 1) { throw new Error(`(UNEXPECTED) invalid arg? only expected one ibgib on arg.ibGibs (E: 1a1498af668740fe9439f4953a74ea8a)`); }
|
|
479
|
+
contextIbGib = arg.ibGibs![0];
|
|
480
|
+
} else {
|
|
481
|
+
// addr provided
|
|
482
|
+
const resGet = await this.ibgibsSvc.get({ addr });
|
|
483
|
+
if (!resGet.success || resGet.ibGibs?.length !== 1) { throw new Error(`could not get context addr (${addr}) (E: 834492313512a45b23a7bebacdc48122)`); }
|
|
484
|
+
contextIbGib = resGet.ibGibs[0];
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (latest) {
|
|
488
|
+
const resLatestAddr = await this.ibgibsSvc.getLatestAddr({ ibGib: contextIbGib });
|
|
489
|
+
if (resLatestAddr !== getIbGibAddr({ ibGib: contextIbGib })) {
|
|
490
|
+
const resGet = await this.ibgibsSvc.get({ addr: resLatestAddr });
|
|
491
|
+
if (resGet.success && resGet.ibGibs?.length === 1) {
|
|
492
|
+
contextIbGib = resGet.ibGibs[0];
|
|
493
|
+
} else {
|
|
494
|
+
throw new Error(`unable to get resLatestAddr (${resLatestAddr}) (E: ce1e1297743e9a16c8f082321e52a122)`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return contextIbGib;
|
|
499
|
+
} catch (error) {
|
|
500
|
+
console.error(`${lc} ${error.message}`);
|
|
501
|
+
throw error;
|
|
502
|
+
} finally {
|
|
503
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* The context is the ibgib where we are interacting with the user(s) (only
|
|
509
|
+
* singlular atow).
|
|
510
|
+
*
|
|
511
|
+
* When we initialize, we are setting state on this robbot as well as subscribing
|
|
512
|
+
* to the context ibgib's updates in `this.ibgibsSvc`.
|
|
513
|
+
*
|
|
514
|
+
* if we already have a context, then this will check the new incoming
|
|
515
|
+
* context against it. If it's the same timeline, then this won't do
|
|
516
|
+
* anything. Otherwise, it will finalize the previous context.
|
|
517
|
+
*/
|
|
518
|
+
protected async initializeContext({
|
|
519
|
+
arg,
|
|
520
|
+
rel8nName,
|
|
521
|
+
}: {
|
|
522
|
+
arg: WitnessArgIbGib<IbGib_V1, TData, TRel8ns>,
|
|
523
|
+
/**
|
|
524
|
+
* If provided, will rel8 the context to this witness upon initialize completion.
|
|
525
|
+
*
|
|
526
|
+
* todo: refactor this to not have this in this function. I have this in here
|
|
527
|
+
* to trigger a compilation error
|
|
528
|
+
*
|
|
529
|
+
* originally this function was inside a robbot and part of the init is to rel8
|
|
530
|
+
* the context to the robbot to say it's taking part in that conversation context.
|
|
531
|
+
*/
|
|
532
|
+
rel8nName: string,
|
|
533
|
+
}): Promise<void> {
|
|
534
|
+
const lc = `${this.lc}[${this.initializeContext.name}]`;
|
|
535
|
+
try {
|
|
536
|
+
if (logalot) { console.log(`${lc} starting... (I: d93429c85b0a494388f66fba3eece922)`); }
|
|
537
|
+
|
|
538
|
+
// get context from arg just to compare the tjp's so we don't need
|
|
539
|
+
// the latest at this point
|
|
540
|
+
const incomingContext_NotLatest = await this.getContextIbGibFromArgOrAddr({ arg, latest: false });
|
|
541
|
+
if (this._currentWorkingContextIbGib) {
|
|
542
|
+
let currentTjpAddr = getTjpAddr({ ibGib: this._currentWorkingContextIbGib });
|
|
543
|
+
const incomingTjpAddr = getTjpAddr({ ibGib: incomingContext_NotLatest })
|
|
544
|
+
if (currentTjpAddr === incomingTjpAddr) {
|
|
545
|
+
console.warn(`${lc} initializing context but it's the same timeline (${currentTjpAddr}). (W: 7609f8f51172443183e0c93ad52bfe56)`);
|
|
546
|
+
return;
|
|
547
|
+
} else {
|
|
548
|
+
await this.finalizeContext({ arg });
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** used both now and when context ibgib is updated via observable */
|
|
553
|
+
const updatePriorChildren = () => {
|
|
554
|
+
this._currentWorkingContextIbGib_PriorChildrenAddrs = [
|
|
555
|
+
...this._currentWorkingContextIbGib?.rel8ns?.comment ?? [],
|
|
556
|
+
...this._currentWorkingContextIbGib?.rel8ns?.pic ?? [],
|
|
557
|
+
...this._currentWorkingContextIbGib?.rel8ns?.link ?? [],
|
|
558
|
+
];
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
// set the props
|
|
562
|
+
this._currentWorkingContextIbGib = await this.getContextIbGibFromArgOrAddr({ arg, latest: true });
|
|
563
|
+
updatePriorChildren();
|
|
564
|
+
|
|
565
|
+
// subscribe to context ibgib updates
|
|
566
|
+
const contextTjpAddr = getTjpAddr({ ibGib: this._currentWorkingContextIbGib });
|
|
567
|
+
if (!this.ibgibsSvc) { throw new Error(`this.ibgibsSvc falsy...not initialized? (E: 6e38bfdc5c2eb5fef884f0183889e823)`); }
|
|
568
|
+
this._contextChangesSubscription =
|
|
569
|
+
this.ibgibsSvc.latestObs.pipe(filter(x => x.tjpAddr === contextTjpAddr)).subscribe(async (update: any) => {
|
|
570
|
+
const currentAddr = getIbGibAddr({ ibGib: this._currentWorkingContextIbGib });
|
|
571
|
+
if (update.latestAddr !== currentAddr) {
|
|
572
|
+
if (logalot) { console.warn(`${lc} checking if new context is actually new...damn getLatestAddr maybe not working in ionic space... argh (W: 3d1a12154dfafb9c96d07e6f75f7a322)`); }
|
|
573
|
+
if (update.latestIbGib) {
|
|
574
|
+
let latestN_Supposedly = update.latestIbGib.data?.n ?? -1;
|
|
575
|
+
let currentN = this._currentWorkingContextIbGib?.data?.n ?? -2;
|
|
576
|
+
if (latestN_Supposedly <= currentN) {
|
|
577
|
+
console.warn(`${lc} latestN is not really the latest. latestN_Supposedly: ${latestN_Supposedly}, currentN: ${currentN} (W: 6792c9a596c44c03b262614ae79a715e)`)
|
|
578
|
+
return; /* <<<< returns early */
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (logalot) { console.log(`${lc} update to context.\ncurrentAddr: ${currentAddr}\nlatestAddr: ${update.latestAddr} (I: d0adcc392e6e974c9917730ebad51322)`); }
|
|
582
|
+
this._currentWorkingContextIbGib =
|
|
583
|
+
update.latestIbGib ??
|
|
584
|
+
await this.getContextIbGibFromArgOrAddr({ addr: update.latestAddr, latest: false }); // already latest
|
|
585
|
+
if (!this._updatingContext) {
|
|
586
|
+
await this.handleContextUpdate({ update });
|
|
587
|
+
updatePriorChildren();
|
|
588
|
+
} else {
|
|
589
|
+
if (logalot) { console.log(`${lc} already updating context (I: f856f9414627ab00418dccd285b55822)`); }
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
// rel8 to the context (conversation)
|
|
595
|
+
|
|
596
|
+
if (rel8nName) {
|
|
597
|
+
await this.rel8To({
|
|
598
|
+
ibGibs: [this._currentWorkingContextIbGib],
|
|
599
|
+
rel8nName,
|
|
600
|
+
// rel8nName: ROBBOT_CONTEXT_REL8N_NAME,
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// subscribe to receive updates to the context so we can participate
|
|
605
|
+
// in the conversation (i.e. interpret incoming ibgibs like commands
|
|
606
|
+
// if needed)
|
|
607
|
+
// let gibInfo = getGibInfo({ gib: this._currentWorkingContextIbGib.gib });
|
|
608
|
+
// if (gibInfo.tjpGib) {
|
|
609
|
+
// this.ibgibsSvc.latestObs
|
|
610
|
+
// .subscribe(update => {
|
|
611
|
+
// if (!update.tjpAddr) { return; /* <<<< returns early */ }
|
|
612
|
+
// if (getIbAndGib({ ibGibAddr: update.tjpAddr }).gib !== gibInfo.tjpGib) { return; /* <<<< returns early */ }
|
|
613
|
+
// if (update.latestAddr === getIbGibAddr({ ibGib: this._currentWorkingContextIbGib })) {
|
|
614
|
+
// if (logalot) { console.log(`${lc} already have that context... (I: a6e17ec40d620f0bd5b231db39eaa522)`); }
|
|
615
|
+
// return; /* <<<< returns early */
|
|
616
|
+
// }
|
|
617
|
+
// if (this._updatingContext) {
|
|
618
|
+
// if (logalot) { console.log(`${lc} already updating context (I: f856f9414627ab00418dccd285b55822)`); }
|
|
619
|
+
// return; /* <<<< returns early */
|
|
620
|
+
// }
|
|
621
|
+
// this.handleContextUpdate({ update });
|
|
622
|
+
// });
|
|
623
|
+
// }
|
|
624
|
+
} catch (error) {
|
|
625
|
+
console.error(`${lc} ${error.message}`);
|
|
626
|
+
throw error;
|
|
627
|
+
} finally {
|
|
628
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
protected async finalizeContext({
|
|
633
|
+
arg,
|
|
634
|
+
}: {
|
|
635
|
+
arg: WitnessArgIbGib<IbGib_V1, TData, TRel8ns>,
|
|
636
|
+
}): Promise<void> {
|
|
637
|
+
const lc = `${this.lc}[${this.finalizeContext.name}]`;
|
|
638
|
+
try {
|
|
639
|
+
if (logalot) { console.log(`${lc} starting... (I: dd53dfc745864dd19fde5f209ceb82c8)`); }
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
let tries = 0;
|
|
643
|
+
const maxTries = 100;
|
|
644
|
+
while (this._updatingContext && tries < maxTries) {
|
|
645
|
+
await delay(100);
|
|
646
|
+
tries++;
|
|
647
|
+
if (tries % 10 === 0) {
|
|
648
|
+
console.log(`${lc} already updating context and taking a litle while... waiting still. tries: ${tries}/${maxTries} (I: d45ab59af9ea43518432e34ddad95c19)`)
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
if (this._updatingContext) {
|
|
652
|
+
console.error(`${lc} previous call to updatingContext took too long. Ignoring flag and finalizing context. (E: 9a2dc4e1923442fa90fbeae72f358acd)`);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
this._updatingContext = true;
|
|
656
|
+
if (this._contextChangesSubscription) {
|
|
657
|
+
debugger;
|
|
658
|
+
this._contextChangesSubscription.unsubscribe();
|
|
659
|
+
delete this._contextChangesSubscription;
|
|
660
|
+
}
|
|
661
|
+
delete this._currentWorkingContextIbGib;
|
|
662
|
+
|
|
663
|
+
throw new Error(`not refactored yet. the proceeding code needs to be introduced in the robbot descending class. (E: 066aa414693672d868dbf953617aab23)`);
|
|
664
|
+
// if (this._currentWorkingContextIbGib) {
|
|
665
|
+
// await this.createCommentAndRel8ToContextIbGib({
|
|
666
|
+
// text: await this.getOutputText({ text: 'end of line' }),
|
|
667
|
+
// contextIbGib: this._currentWorkingContextIbGib,
|
|
668
|
+
// });
|
|
669
|
+
// delete this._currentWorkingContextIbGib;
|
|
670
|
+
// }
|
|
671
|
+
} catch (error) {
|
|
672
|
+
console.error(`${lc} ${error.message}`);
|
|
673
|
+
throw error;
|
|
674
|
+
} finally {
|
|
675
|
+
this._updatingContext = false;
|
|
676
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Handles a new ibgib in the current working context.
|
|
682
|
+
*
|
|
683
|
+
* If it originated from the robbot him/herself, then we will ignore it.
|
|
684
|
+
* Otherwise this is interpreted either as a "command" or something to remember.
|
|
685
|
+
*
|
|
686
|
+
* ## notes
|
|
687
|
+
*
|
|
688
|
+
* If the user says something and we're listening (this is triggered), then
|
|
689
|
+
* it should be either interpreted as a "command" or ignored. I put
|
|
690
|
+
* "command" in quotes because the word is a very short-sighted
|
|
691
|
+
* understanding of the conversational aspect. Or perhaps I speak to the
|
|
692
|
+
* connotations to current belief's regarding robbots and commands.
|
|
693
|
+
*
|
|
694
|
+
* This is complicated by the possibility in the future of multiple robbots
|
|
695
|
+
* being engaged in a conversation within the same context.
|
|
696
|
+
*/
|
|
697
|
+
protected async handleContextUpdate({ update }: { update: IbGibTimelineUpdateInfo }): Promise<void> {
|
|
698
|
+
const lc = `${this.lc}[${this.handleContextUpdate.name}]`;
|
|
699
|
+
// I don't see this as really being very likely in the near future,
|
|
700
|
+
// but putting in a wait delay in case there are multiple updates
|
|
701
|
+
while (this._updatingContext) {
|
|
702
|
+
console.warn(`${lc} already updating context? delaying... (W: 19d2ebeaaf2340fb91a7d6c717e9cb41)`);
|
|
703
|
+
await delay(1000);
|
|
704
|
+
}
|
|
705
|
+
this._updatingContext = true;
|
|
706
|
+
try {
|
|
707
|
+
if (logalot) { console.log(`${lc} starting... (I: 3eeaa40cad49094f125f9f5cd6ff6e22)`); }
|
|
708
|
+
|
|
709
|
+
// if it's caused by this robbot speaking, then we don't really need
|
|
710
|
+
// it. but if it's from the user, then we want to respond.
|
|
711
|
+
if (logalot) {
|
|
712
|
+
console.log(`${lc} update: (I: ad0abae7de472e3b4d3891ea0b937322)`);
|
|
713
|
+
console.table(update);
|
|
714
|
+
}
|
|
715
|
+
if (!update.latestIbGib) {
|
|
716
|
+
debugger;
|
|
717
|
+
throw new Error(`(UNEXPECTED) update.latestIbGib falsy? (E: e18a048d7e95757238396ddd84748f22)`);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
const newContext = update.latestIbGib;
|
|
721
|
+
const newChildrenIbGibs = await this.getNewChildrenIbGibs({ newContext });
|
|
722
|
+
// should normally be just one (would be very edge casey if not atow)
|
|
723
|
+
let newChild: IbGib_V1;
|
|
724
|
+
if (newChildrenIbGibs.length === 1) {
|
|
725
|
+
newChild = newChildrenIbGibs[0];
|
|
726
|
+
} else if (newChildrenIbGibs.length > 1) {
|
|
727
|
+
console.warn(`${lc} (UNEXPECTED) found multiple new children in conversation? Using only the last. (W: 02d82a8f755f418d95fa30f0f52ad58e)`);
|
|
728
|
+
newChild = newChildrenIbGibs[newChildrenIbGibs.length - 1];
|
|
729
|
+
} else {
|
|
730
|
+
// no new children, so maybe the user deleted something or who knows.
|
|
731
|
+
if (logalot) { console.log(`${lc} no new children in context update. returning early... (I: 31397b04965351ab29bb3f78cb709122)`); }
|
|
732
|
+
return; /* <<<< returns early */
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// we have a new chidl inthe context.
|
|
736
|
+
await this.handleNewContextChild({ newChild });
|
|
737
|
+
|
|
738
|
+
} catch (error) {
|
|
739
|
+
console.error(`${lc} ${error.message}`);
|
|
740
|
+
throw error;
|
|
741
|
+
} finally {
|
|
742
|
+
this._updatingContext = false;
|
|
743
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
async getNewChildrenIbGibs({ newContext }: { newContext: IbGib_V1 }): Promise<IbGib_V1[]> {
|
|
748
|
+
const lc = `${this.lc}[${this.getNewChildrenIbGibs.name}]`;
|
|
749
|
+
try {
|
|
750
|
+
if (logalot) { console.log(`${lc} starting... (I: 1b3d1cf908489087fa3b281f55b9a522)`); }
|
|
751
|
+
// get a diff of the new addrs vs. the old addrs.
|
|
752
|
+
/** all of the children addrs of the new context */
|
|
753
|
+
const newContextChildrenAddrs = [
|
|
754
|
+
...newContext.rel8ns?.comment ?? [],
|
|
755
|
+
...newContext.rel8ns?.pic ?? [],
|
|
756
|
+
...newContext.rel8ns?.link ?? [],
|
|
757
|
+
];
|
|
758
|
+
/** just the new addrs from the context */
|
|
759
|
+
const newChildrenAddrs = newContextChildrenAddrs.filter(x =>
|
|
760
|
+
!this._currentWorkingContextIbGib_PriorChildrenAddrs.includes(x)
|
|
761
|
+
);
|
|
762
|
+
if (newChildrenAddrs.length === 0) {
|
|
763
|
+
// no new children
|
|
764
|
+
if (logalot) { console.log(`${lc} no new children addrs in newContext. returning early... (I: 8f9c3658c194c472cb1e2bc19d847b22)`); }
|
|
765
|
+
return []; /* <<<< returns early */
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// get the latest addrs for those children
|
|
769
|
+
if (!this.ibgibsSvc) { throw new Error(`this.ibgibsSvc falsy. not initialized? (E: 1cc26ca7de7fadcdbaecbd6613350e23)`); }
|
|
770
|
+
const space = await this.ibgibsSvc.getLocalUserSpace({});
|
|
771
|
+
if (!space) { throw new Error(`couldn't get local user space? (E: f118f75e5852fc22bba3a6495beec723)`); }
|
|
772
|
+
const resLatestAddrs = await getLatestAddrs({ addrs: newChildrenAddrs, space, });
|
|
773
|
+
const latestAddrs = Object.values(resLatestAddrs?.data?.latestAddrsMap ?? {});
|
|
774
|
+
if (!resLatestAddrs?.data?.success || latestAddrs.length !== newChildrenAddrs.length) { throw new Error(`could not get latest addrs. (E: 2e1619e7e2e166696fe8ff78cb02cc22)`); }
|
|
775
|
+
|
|
776
|
+
// get the addrs' corresponding ibgibs
|
|
777
|
+
if (latestAddrs.some(x => x === null)) {
|
|
778
|
+
// some of the addrs were not found in the space
|
|
779
|
+
throw new Error(`some addrs were not found in the space. (E: 2216d8e5ad7fb600ceb025ed7c90f323)`);
|
|
780
|
+
}
|
|
781
|
+
const resGet = await this.ibgibsSvc.get({ addrs: latestAddrs as IbGibAddr[] });
|
|
782
|
+
if (!resGet.success || resGet.ibGibs?.length !== newChildrenAddrs.length) {
|
|
783
|
+
throw new Error(`failed to get newChildren with addrs: ${newChildrenAddrs.join('|')}. Error: ${resGet.errorMsg ?? 'unknown error 5737bd0996d5445b8bd80975bedc0d57'} (E: 05722e11350ec6ffffdb5c7d0caa2922)`);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
return resGet.ibGibs;
|
|
787
|
+
} catch (error) {
|
|
788
|
+
console.error(`${lc} ${error.message}`);
|
|
789
|
+
throw error;
|
|
790
|
+
} finally {
|
|
791
|
+
if (logalot) { console.log(`${lc} complete.`); }
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
protected async handleNewContextChild({ newChild }: { newChild: IbGib_V1 }): Promise<void> {
|
|
796
|
+
const lc = `${this.lc}[${this.handleNewContextChild.name}]`;
|
|
797
|
+
try {
|
|
798
|
+
throw new Error(`not implemented in base class (E: ea6b2b13d9504d93b33b70191d106084)`);
|
|
799
|
+
} catch (error) {
|
|
800
|
+
console.error(`${lc} ${error.message}`);
|
|
801
|
+
throw error;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// export interface IbGibRobbotAny
|
|
808
|
+
// extends RobbotBase_V1<any, any, any, any, any, any, any, any> {
|
|
809
|
+
// }
|