@tomflow/chatloop-tools 0.9.19 → 0.9.21
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 +8 -3
- package/index.mjs +49 -9
- package/lib/supervisor-observability.mjs +157 -0
- package/lib/supervisor-runtime.mjs +14 -111
- package/lib/trace-gateway.mjs +90 -11
- package/lib/trace-observation.mjs +17 -8
- package/node_modules/@tomflow/chatloop-context/README.md +240 -0
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +266 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
- package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +182 -0
- package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +233 -0
- package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +24 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +1989 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +67 -0
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +280 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +280 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +115 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +55 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +147 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +343 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +391 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +166 -0
- package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
- package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +312 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +115 -0
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
- package/node_modules/@tomflow/chatloop-context/package.json +40 -0
- package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
- package/node_modules/@tomflow/chatloop-trace/README.md +105 -0
- package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
- package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
- package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
- package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
- package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +265 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +205 -0
- package/node_modules/@tomflow/chatloop-trace/package.json +30 -0
- package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
- package/package.json +5 -1
- package/providers/form.mjs +85 -206
- package/providers/trace.mjs +5 -2
- package/ui/form/review-app.js +59 -44
- package/ui/form/review.html +1 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {makeTraceReader} from './trace-reader.mjs';
|
|
2
|
+
import {randomUUID} from 'node:crypto';
|
|
3
|
+
import {constants as FS,closeSync,fstatSync,lstatSync,openSync,writeSync,renameSync,unlinkSync} from 'node:fs';
|
|
4
|
+
import {dirname,isAbsolute} from 'node:path';
|
|
5
|
+
|
|
6
|
+
export const TRACE_SCHEMA_VERSION=2;
|
|
7
|
+
const UUID=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
8
|
+
const SCOPE=/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
|
|
9
|
+
const REF=/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/;
|
|
10
|
+
const EVENT=/^[a-z][a-z0-9_.-]{1,95}$/;
|
|
11
|
+
const CODE=/^[A-Z][A-Z0-9_]{1,95}$/;
|
|
12
|
+
const COMPONENTS=new Set(['supervisor','gateway','context','extension','native']);
|
|
13
|
+
const STATUSES=new Set(['BEGIN','CONFIRMED','FAILED','UNKNOWN','BLOCKED']);
|
|
14
|
+
const ALLOWED=new Set(['eventId','traceId','spanId','parentSpanId','timestamp','component',
|
|
15
|
+
'event','projectRef','taskRef','sessionRef','generation','requestId','actionId',
|
|
16
|
+
'status','errorCode','durationMs','evidenceRef','api','apiName']);
|
|
17
|
+
const fail=code=>Object.assign(new Error(code),{code});
|
|
18
|
+
let eventRules=null;
|
|
19
|
+
// Private composition port. The npm root supplies only the generated,
|
|
20
|
+
// source-reviewed event contracts; callers never register runtime plugins.
|
|
21
|
+
export function configureTraceEvents(definitions){
|
|
22
|
+
if(eventRules)throw fail('TRACE_EVENTS_ALREADY_CONFIGURED');
|
|
23
|
+
if(!Array.isArray(definitions)||!definitions.length)
|
|
24
|
+
throw fail('TRACE_EVENTS_INVALID');
|
|
25
|
+
const rules=new Map();
|
|
26
|
+
for(const rule of definitions){
|
|
27
|
+
if(!rule||typeof rule.id!=='string'||rules.has(rule.id)||
|
|
28
|
+
rule.schemaVersion!==TRACE_SCHEMA_VERSION||
|
|
29
|
+
typeof rule.validate!=='function'||typeof rule.errorCode!=='string')
|
|
30
|
+
throw fail('TRACE_EVENTS_INVALID');
|
|
31
|
+
rules.set(rule.id,rule);
|
|
32
|
+
}
|
|
33
|
+
if(!rules.has('context.api'))throw fail('TRACE_REQUIRED_EVENT_SCHEMA_MISSING');
|
|
34
|
+
eventRules=rules;
|
|
35
|
+
}
|
|
36
|
+
function eventRule(v){
|
|
37
|
+
if(!eventRules)throw fail('TRACE_EVENTS_NOT_COMPOSED');
|
|
38
|
+
return eventRules.get(v.event);
|
|
39
|
+
}
|
|
40
|
+
function ruleAccepts(rule,v){
|
|
41
|
+
try{
|
|
42
|
+
// A Provider validates a read-only projection. It must never rewrite the
|
|
43
|
+
// caller's scope, status, identity, or the on-disk event being diagnosed.
|
|
44
|
+
const view={...v};
|
|
45
|
+
if(object(view.api))view.api=Object.freeze({...view.api});
|
|
46
|
+
return rule.validate(Object.freeze(view))===true;
|
|
47
|
+
}catch{return false}
|
|
48
|
+
}
|
|
49
|
+
function validStoredFamily(v){
|
|
50
|
+
// A pre-existing API observation without the original caller's IDs cannot
|
|
51
|
+
// be promoted into trusted correlated evidence by the Reader.
|
|
52
|
+
if(!validApiCorrelation(v))return false;
|
|
53
|
+
// The reserved api field belongs solely to the Context API event, no
|
|
54
|
+
// matter how permissive an additional Provider's validator may become.
|
|
55
|
+
if(v.schemaVersion===TRACE_SCHEMA_VERSION&&v.event!=='context.api'
|
|
56
|
+
&&Object.hasOwn(v,'api'))return false;
|
|
57
|
+
const rule=eventRule(v);
|
|
58
|
+
return rule ? v.schemaVersion===rule.schemaVersion&&ruleAccepts(rule,v)
|
|
59
|
+
:!v.event.startsWith('typed.')&&v.api==null;
|
|
60
|
+
}
|
|
61
|
+
const object=v=>v!==null&&typeof v==='object'&&!Array.isArray(v);
|
|
62
|
+
const isId=v=>typeof v==='string'&&UUID.test(v);
|
|
63
|
+
const validApiCorrelation=v=>!['typed.api.call','context.api'].includes(v?.event)
|
|
64
|
+
||isId(v.traceId)&&isId(v.spanId)&&
|
|
65
|
+
(v.parentSpanId==null||isId(v.parentSpanId)&&v.parentSpanId!==v.spanId);
|
|
66
|
+
const safeScope=v=>typeof v==='string'&&SCOPE.test(v);
|
|
67
|
+
const safeRef=v=>typeof v==='string'&&REF.test(v);
|
|
68
|
+
const natural=v=>Number.isSafeInteger(v)&&v>=0;
|
|
69
|
+
const keys=(v,allowed)=>{
|
|
70
|
+
if(!object(v))throw fail('TRACE_INVALID_OBJECT');
|
|
71
|
+
for(const k of Object.keys(v))if(!allowed.has(k))throw fail('TRACE_INVALID_FIELD');
|
|
72
|
+
};
|
|
73
|
+
const bytes=v=>Buffer.byteLength(JSON.stringify(v),'utf8');
|
|
74
|
+
const nullable=(v,test,code)=>{
|
|
75
|
+
if(v!==undefined&&v!==null&&!test(v))throw fail(code);
|
|
76
|
+
return v??null;
|
|
77
|
+
};
|
|
78
|
+
export function createTrace(){return {traceId:randomUUID(),spanId:randomUUID()};}
|
|
79
|
+
function normalize(v,epoch,seq){
|
|
80
|
+
keys(v,ALLOWED);
|
|
81
|
+
if(!EVENT.test(v.event||'')||!COMPONENTS.has(v.component)||
|
|
82
|
+
!STATUSES.has(v.status))throw fail('TRACE_INVALID_EVENT');
|
|
83
|
+
// Event-family-specific contracts validate the same canonical v2 envelope
|
|
84
|
+
// in Writer and Reader. A generic event must not impersonate an API call.
|
|
85
|
+
if(v.event!=='context.api'&&Object.hasOwn(v,'api'))
|
|
86
|
+
throw fail('TRACE_INVALID_API');
|
|
87
|
+
if(v.event!=='typed.api.call'&&Object.hasOwn(v,'apiName'))
|
|
88
|
+
throw fail('TRACE_INVALID_API_USAGE');
|
|
89
|
+
const rule=eventRule(v);
|
|
90
|
+
if(!rule&&v.event.startsWith('typed.'))
|
|
91
|
+
throw fail('TRACE_EVENT_UNREGISTERED');
|
|
92
|
+
if(rule ? !ruleAccepts(rule,v) : v.api!==undefined)
|
|
93
|
+
throw fail(rule?.errorCode||'TRACE_INVALID_API');
|
|
94
|
+
// NEVER fabricate a trace/span for an API call. This check is shared by
|
|
95
|
+
// all six npm consumers through the sole Trace Writer/Reader contract.
|
|
96
|
+
if(!validApiCorrelation(v))throw fail('TRACE_INVALID_API_CORRELATION');
|
|
97
|
+
for(const k of ['eventId','traceId','spanId'])
|
|
98
|
+
if(v[k]!==undefined&&!isId(v[k]))throw fail('TRACE_INVALID_ID');
|
|
99
|
+
const entry={
|
|
100
|
+
time:v.timestamp===undefined?new Date().toISOString():v.timestamp,
|
|
101
|
+
schemaVersion:TRACE_SCHEMA_VERSION,eventId:v.eventId||randomUUID(),
|
|
102
|
+
traceId:v.traceId||randomUUID(),spanId:v.spanId||randomUUID(),
|
|
103
|
+
parentSpanId:nullable(v.parentSpanId,isId,'TRACE_INVALID_ID'),
|
|
104
|
+
writerEpoch:epoch,writerSeq:seq,component:v.component,event:v.event,
|
|
105
|
+
projectRef:nullable(v.projectRef,safeScope,'TRACE_INVALID_SCOPE'),
|
|
106
|
+
taskRef:nullable(v.taskRef,safeRef,'TRACE_INVALID_REFERENCE'),
|
|
107
|
+
sessionRef:nullable(v.sessionRef,safeRef,'TRACE_INVALID_REFERENCE'),
|
|
108
|
+
generation:nullable(v.generation,natural,'TRACE_INVALID_NUMBER'),
|
|
109
|
+
requestId:nullable(v.requestId,safeRef,'TRACE_INVALID_REFERENCE'),
|
|
110
|
+
actionId:nullable(v.actionId,safeRef,'TRACE_INVALID_REFERENCE'),
|
|
111
|
+
status:v.status,errorCode:nullable(v.errorCode,x=>CODE.test(x),'TRACE_INVALID_ERROR_CODE'),
|
|
112
|
+
durationMs:nullable(v.durationMs,natural,'TRACE_INVALID_NUMBER'),
|
|
113
|
+
api:v.api?{name:v.api.name,surface:v.api.surface,mode:v.api.mode}:null,
|
|
114
|
+
apiName:v.apiName??null,
|
|
115
|
+
evidenceRef:nullable(v.evidenceRef,safeRef,'TRACE_INVALID_REFERENCE')
|
|
116
|
+
};
|
|
117
|
+
if(typeof entry.time!=='string'||entry.time.length>40||
|
|
118
|
+
!Number.isFinite(Date.parse(entry.time)))throw fail('TRACE_INVALID_TIMESTAMP');
|
|
119
|
+
// JSONL must stay lean: omit absent optional columns, never serialize
|
|
120
|
+
// redundant null fields. Read projections can still expose stable nulls.
|
|
121
|
+
const compact=Object.fromEntries(Object.entries(entry).filter(([,v])=>v!==null));
|
|
122
|
+
if(bytes(compact)>4096)throw fail('TRACE_EVENT_TOO_LARGE');
|
|
123
|
+
return compact;
|
|
124
|
+
}
|
|
125
|
+
// Strict file owner, permissions, no directory creation and no symlink escapes.
|
|
126
|
+
function secureFile(filePath){
|
|
127
|
+
if(typeof filePath!=='string'||!isAbsolute(filePath))throw fail('TRACE_PATH_INVALID');
|
|
128
|
+
let dir;try{dir=lstatSync(dirname(filePath))}catch{throw fail('TRACE_DIRECTORY_UNAVAILABLE')}
|
|
129
|
+
if(!dir.isDirectory()||dir.isSymbolicLink()||(dir.mode&0o077)||
|
|
130
|
+
process.getuid&&dir.uid!==process.getuid())throw fail('TRACE_DIRECTORY_UNTRUSTED');
|
|
131
|
+
let st;
|
|
132
|
+
try{st=lstatSync(filePath)}
|
|
133
|
+
catch(e){if(e.code==='ENOENT')return null;throw e}
|
|
134
|
+
// existsSync follows symlinks: a dangling link must not be mistaken for an
|
|
135
|
+
// absent log or rotation segment and silently discarded from diagnosis.
|
|
136
|
+
if(!st.isFile()||st.isSymbolicLink()||(st.mode&0o077)||
|
|
137
|
+
process.getuid&&st.uid!==process.getuid())throw fail('TRACE_FILE_UNTRUSTED');
|
|
138
|
+
return st;
|
|
139
|
+
}
|
|
140
|
+
export function createTraceWriter({
|
|
141
|
+
filePath,enableRotation=false,rotateBytes=10*1024*1024,maxSegments=5
|
|
142
|
+
}){
|
|
143
|
+
if(typeof enableRotation!=='boolean'||!Number.isSafeInteger(rotateBytes)||
|
|
144
|
+
rotateBytes<512||rotateBytes>10*1024*1024||
|
|
145
|
+
!Number.isSafeInteger(maxSegments)||maxSegments<1||maxSegments>5)
|
|
146
|
+
throw fail('TRACE_ROTATION_INVALID');
|
|
147
|
+
const epoch=randomUUID();let seq=0,failures=0,lastError=null,rotations=0;
|
|
148
|
+
const rotateIfNeeded=lineBytes=>{
|
|
149
|
+
// Legacy data is never moved until an operator explicitly enables
|
|
150
|
+
// rotation after preserving its historical evidence.
|
|
151
|
+
if(!enableRotation)return;
|
|
152
|
+
const old=secureFile(filePath);
|
|
153
|
+
if(!old||old.size+lineBytes<=rotateBytes)return;
|
|
154
|
+
const oldest=filePath+'.'+maxSegments;
|
|
155
|
+
if(secureFile(oldest))unlinkSync(oldest);
|
|
156
|
+
for(let i=maxSegments-1;i>=1;i--){
|
|
157
|
+
const from=filePath+'.'+i;
|
|
158
|
+
if(secureFile(from))renameSync(from,filePath+'.'+(i+1));
|
|
159
|
+
}
|
|
160
|
+
renameSync(filePath,filePath+'.1');
|
|
161
|
+
rotations++;
|
|
162
|
+
};
|
|
163
|
+
function append(entry){
|
|
164
|
+
let fd;
|
|
165
|
+
try{
|
|
166
|
+
rotateIfNeeded(bytes(entry)+1);
|
|
167
|
+
secureFile(filePath);
|
|
168
|
+
fd=openSync(filePath,FS.O_WRONLY|FS.O_APPEND|FS.O_CREAT|FS.O_NOFOLLOW,0o600);
|
|
169
|
+
const st=fstatSync(fd);
|
|
170
|
+
if(!st.isFile()||(st.mode&0o077)||
|
|
171
|
+
process.getuid&&st.uid!==process.getuid())throw fail('TRACE_FILE_UNTRUSTED');
|
|
172
|
+
writeSync(fd,JSON.stringify(entry)+'\n');
|
|
173
|
+
seq++;lastError=null;
|
|
174
|
+
return {eventId:entry.eventId,traceId:entry.traceId,
|
|
175
|
+
writerEpoch:epoch,writerSeq:seq};
|
|
176
|
+
}catch(e){failures++;lastError=e.code||'TRACE_WRITE_FAILED';throw e}
|
|
177
|
+
finally{if(fd!==undefined)closeSync(fd)}
|
|
178
|
+
}
|
|
179
|
+
const record=v=>append(normalize(v,epoch,seq+1));
|
|
180
|
+
const recordControl=(event,detail={})=>{
|
|
181
|
+
// All sources now emit the same typed v2 event. Historical tool summaries,
|
|
182
|
+
// traceStatus aliases and arbitrary result columns are never written.
|
|
183
|
+
const status=detail?.status==='FAILED'||/(?:error|fatal)/.test(event)?'FAILED':
|
|
184
|
+
detail?.status==='UNKNOWN'||
|
|
185
|
+
['PARTIAL','DEGRADED','UNCONFIRMED','TIMEOUT'].includes(detail?.status)?'UNKNOWN':
|
|
186
|
+
detail?.status==='BLOCKED'||event.includes('blocked')?'BLOCKED':'CONFIRMED';
|
|
187
|
+
return record({event,component:'supervisor',status,
|
|
188
|
+
...(safeRef(detail?.requestId)?{requestId:detail.requestId}:{}),
|
|
189
|
+
...(typeof detail?.errorCode==='string'&&CODE.test(detail.errorCode)?
|
|
190
|
+
{errorCode:detail.errorCode}:{})
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
const health=()=>({status:lastError?'TRACE_DEGRADED':'OK',
|
|
194
|
+
errorCode:lastError,failures,writerEpoch:epoch,writerSeq:seq,
|
|
195
|
+
rotationEnabled:enableRotation,rotations});
|
|
196
|
+
return {record,recordControl,health};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Runtime event definitions remain bound to this exact module instance;
|
|
200
|
+
// dynamic import isolation in the Trace SDK must not be lost by refactoring.
|
|
201
|
+
export function createTraceReader(options){
|
|
202
|
+
return makeTraceReader(options,{TRACE_SCHEMA_VERSION,fail,validStoredFamily,
|
|
203
|
+
object,isId,safeScope,safeRef,natural,keys,secureFile,eventRule,
|
|
204
|
+
EVENT,CODE,COMPONENTS,STATUSES});
|
|
205
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tomflow/chatloop-trace",
|
|
3
|
+
"version": "0.4.4",
|
|
4
|
+
"description": "Trace SDK and scoped read-only diagnostics for Chat Loop",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.mjs",
|
|
17
|
+
"events.generated.mjs",
|
|
18
|
+
"events/",
|
|
19
|
+
"scripts/",
|
|
20
|
+
"lib/",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build:events": "node ./scripts/build-events.mjs",
|
|
25
|
+
"build:events:check": "node ./scripts/build-events.mjs --check",
|
|
26
|
+
"pretest": "node ./scripts/build-events.mjs --check",
|
|
27
|
+
"prepack": "node ./scripts/build-events.mjs --check",
|
|
28
|
+
"test": "node --test test/*.test.mjs"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Deterministic Trace event-family contracts. No runtime plugin discovery,
|
|
2
|
+
// user-controlled module loading or extra log writers.
|
|
3
|
+
import {createHash} from 'node:crypto';
|
|
4
|
+
import {readFileSync,readdirSync,writeFileSync} from 'node:fs';
|
|
5
|
+
import {dirname,join,resolve} from 'node:path';
|
|
6
|
+
import {fileURLToPath,pathToFileURL} from 'node:url';
|
|
7
|
+
const root=dirname(dirname(fileURLToPath(import.meta.url)));
|
|
8
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
9
|
+
const ident=/^[a-z][a-z0-9._-]{1,95}$/;
|
|
10
|
+
const errCode=/^TRACE_[A-Z0-9_]{1,95}$/;
|
|
11
|
+
export async function compileTraceEvents(source=join(root,'events')){
|
|
12
|
+
const files=readdirSync(source).filter(file=>file.endsWith('.mjs')).sort();
|
|
13
|
+
if(!files.length)throw fail('TRACE_EVENTS_EMPTY');
|
|
14
|
+
const ids=new Set(),entries=[];
|
|
15
|
+
let requiredApi=null;
|
|
16
|
+
for(const file of files){
|
|
17
|
+
if(!/^[a-z][a-z0-9._-]*\.mjs$/.test(file))
|
|
18
|
+
throw fail('TRACE_EVENT_FILENAME_INVALID');
|
|
19
|
+
const path=resolve(source,file);
|
|
20
|
+
const hash=createHash('sha256').update(readFileSync(path)).digest('hex');
|
|
21
|
+
const {capability:p}=await import(pathToFileURL(path).href+'?sha256='+hash);
|
|
22
|
+
if(!p||p.id!==file.slice(0,-4)||!ident.test(p.id)||ids.has(p.id)||
|
|
23
|
+
p.schemaVersion!==2||typeof p.validate!=='function'||
|
|
24
|
+
typeof p.description!=='string'||!p.description.trim()||
|
|
25
|
+
(p.usageName!==undefined&&typeof p.usageName!=='function')||
|
|
26
|
+
!errCode.test(p.errorCode||''))
|
|
27
|
+
throw fail('TRACE_EVENT_CAPABILITY_INVALID');
|
|
28
|
+
if(p.id!=='context.api'&&!p.id.startsWith('typed.'))
|
|
29
|
+
throw fail('TRACE_EVENT_NAMESPACE_INVALID');
|
|
30
|
+
ids.add(p.id);
|
|
31
|
+
if(p.id==='context.api')requiredApi=p;
|
|
32
|
+
entries.push(file);
|
|
33
|
+
}
|
|
34
|
+
if(!ids.has('context.api'))
|
|
35
|
+
throw fail('TRACE_REQUIRED_EVENT_SCHEMA_MISSING');
|
|
36
|
+
// The original sensitive Context API event cannot quietly become generic
|
|
37
|
+
// when a provider is removed, weakened, or mistakenly renamed.
|
|
38
|
+
const api=requiredApi;
|
|
39
|
+
const valid={component:'context',event:'context.api',status:'BEGIN',
|
|
40
|
+
api:{name:'task.open',surface:'sdk',mode:'read'}};
|
|
41
|
+
if(!api.validate(valid)||
|
|
42
|
+
api.validate({...valid,status:'UNKNOWN'})||
|
|
43
|
+
api.validate({...valid,component:'extension'})||
|
|
44
|
+
api.validate({...valid,api:{...valid.api,secret:'raw'}})||
|
|
45
|
+
api.validate({...valid,api:{...valid.api,name:'secret chat text'}}))
|
|
46
|
+
throw fail('TRACE_REQUIRED_EVENT_SCHEMA_INVALID');
|
|
47
|
+
const imports=entries.map((file,i)=>
|
|
48
|
+
"import {capability as e"+i+"} from './events/"+file+"';").join('\n');
|
|
49
|
+
return '// GENERATED by scripts/build-events.mjs — do not edit.\n'+imports+
|
|
50
|
+
'\nexport const eventCapabilities=Object.freeze(['+
|
|
51
|
+
entries.map((_,i)=>'e'+i).join(',')+']);\n';
|
|
52
|
+
}
|
|
53
|
+
async function main(){
|
|
54
|
+
const expected=await compileTraceEvents();
|
|
55
|
+
const target=join(root,'events.generated.mjs');
|
|
56
|
+
if(process.argv.includes('--check')){
|
|
57
|
+
let actual;
|
|
58
|
+
try{actual=readFileSync(target,'utf8')}catch{}
|
|
59
|
+
if(actual!==expected)throw fail('TRACE_EVENTS_BUILD_STALE');
|
|
60
|
+
if(process.env.npm_lifecycle_event!=='prepack')
|
|
61
|
+
console.log('TRACE_EVENTS_CURRENT');
|
|
62
|
+
}else{
|
|
63
|
+
writeFileSync(target,expected);
|
|
64
|
+
console.log('TRACE_EVENTS_BUILT');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if(process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url))
|
|
68
|
+
await main();
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.9.
|
|
6
|
+
"version": "0.9.21",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"description": "Chat Loop local tools: MCP gateway, isolated bundled tunnel-client and unified CLI",
|
|
9
9
|
"bin": {
|
|
@@ -35,6 +35,10 @@
|
|
|
35
35
|
"@tomflow/chatloop-trace": "0.4.4",
|
|
36
36
|
"@wonderwhy-er/desktop-commander": "0.2.44"
|
|
37
37
|
},
|
|
38
|
+
"bundleDependencies": [
|
|
39
|
+
"@tomflow/chatloop-context",
|
|
40
|
+
"@tomflow/chatloop-trace"
|
|
41
|
+
],
|
|
38
42
|
"engines": {
|
|
39
43
|
"node": ">=24"
|
|
40
44
|
},
|