@tomflow/chatloop-tunnel-darwin-arm64 0.0.21 → 0.0.24
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/index.mjs +1 -1
- package/package.json +4 -1
- package/lib/api-aspect.mjs +0 -190
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {createApiAspect} from '
|
|
1
|
+
import {createApiAspect} from '@tomflow/chatloop-trace';
|
|
2
2
|
const apiAspect=createApiAspect('@tomflow/chatloop-tunnel-darwin-arm64');
|
|
3
3
|
export const subscribeTunnelApiLog=apiAspect.subscribe;
|
|
4
4
|
// Platform-only npm SDK: the binary carrier owns its own asset paths and hashes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomflow/chatloop-tunnel-darwin-arm64",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"description": "Chat Loop private bundled OpenAI tunnel-client v0.0.19 (arm64)",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -21,5 +21,8 @@
|
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|
|
23
23
|
".": "./index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@tomflow/chatloop-trace": "0.4.4"
|
|
24
27
|
}
|
|
25
28
|
}
|
package/lib/api-aspect.mjs
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// Canonical AOP observer, generated into independently installable npm packages and MV3.
|
|
2
|
-
// Soft evidence only: no parameters, results, URLs, secrets or model content.
|
|
3
|
-
const SAFE=/^[a-zA-Z][a-zA-Z0-9_.:-]{0,126}$/;
|
|
4
|
-
const PACKAGE=/^@[a-z0-9][a-z0-9._-]{0,45}\/[a-z0-9][a-z0-9._-]{0,80}$/;
|
|
5
|
-
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;
|
|
6
|
-
const safe=(s,fallback='api.unknown')=>typeof s==='string'&&SAFE.test(s)?s:fallback;
|
|
7
|
-
const code=e=>typeof e?.code==='string'&&/^[A-Z][A-Z0-9_]{1,95}$/.test(e.code)
|
|
8
|
-
?e.code:'API_FAILED';
|
|
9
|
-
const uid=()=>{
|
|
10
|
-
try{return globalThis.crypto.randomUUID()}catch{
|
|
11
|
-
const hex='0123456789abcdef',h=n=>Array.from({length:n},
|
|
12
|
-
()=>hex[Math.floor(Math.random()*16)]).join('');
|
|
13
|
-
return h(8)+'-'+h(4)+'-4'+h(3)+'-a'+h(3)+'-'+h(12);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
const schedule=typeof setImmediate==='function'?setImmediate:fn=>setTimeout(fn,0);
|
|
17
|
-
const clock=()=>typeof performance!=='undefined'&&typeof performance.now==='function'
|
|
18
|
-
?performance.now():Date.now();
|
|
19
|
-
const MAX_PENDING=4096,MAX_BATCH=128;
|
|
20
|
-
const CONTEXT_KEY=Symbol.for('chat-loop.observation.async-context.v1');
|
|
21
|
-
// Process-local correlation only, never an authorization or plugin registry.
|
|
22
|
-
const contextStore=()=>{
|
|
23
|
-
if(typeof process==='undefined'||typeof process.getBuiltinModule!=='function')return null;
|
|
24
|
-
try{
|
|
25
|
-
if(!process[CONTEXT_KEY]){
|
|
26
|
-
const {AsyncLocalStorage}=process.getBuiltinModule('node:async_hooks');
|
|
27
|
-
Object.defineProperty(process,CONTEXT_KEY,{
|
|
28
|
-
value:new AsyncLocalStorage(),enumerable:false,configurable:false
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
return process[CONTEXT_KEY];
|
|
32
|
-
}catch{return null}
|
|
33
|
-
};
|
|
34
|
-
const validParent=v=>v&&UUID.test(v.traceId||'')&&UUID.test(v.spanId||'');
|
|
35
|
-
export function currentApiTrace(){
|
|
36
|
-
try{
|
|
37
|
-
const local=contextStore()?.getStore();
|
|
38
|
-
if(validParent(local))return {traceId:local.traceId,spanId:local.spanId};
|
|
39
|
-
// Child-process context is sanitized correlation metadata, not trust.
|
|
40
|
-
const value=typeof process!=='undefined'?process.env?.CHAT_LOOP_API_PARENT:null;
|
|
41
|
-
if(typeof value!=='string'||value.length>180)return null;
|
|
42
|
-
const parsed=JSON.parse(value);
|
|
43
|
-
return validParent(parsed)?{traceId:parsed.traceId,spanId:parsed.spanId}:null;
|
|
44
|
-
}catch{return null}
|
|
45
|
-
}
|
|
46
|
-
export function withApiTraceParent(parent,fn){
|
|
47
|
-
if(typeof fn!=='function')throw TypeError('API_TRACE_CALLBACK_REQUIRED');
|
|
48
|
-
if(!validParent(parent))throw TypeError('API_TRACE_PARENT_INVALID');
|
|
49
|
-
const storage=contextStore();
|
|
50
|
-
return storage?storage.run({traceId:parent.traceId,spanId:parent.spanId},fn):fn();
|
|
51
|
-
}
|
|
52
|
-
export function createApiAspect(packageName,{sink}={}){
|
|
53
|
-
const packageId=typeof packageName==='string'&&PACKAGE.test(packageName)
|
|
54
|
-
?packageName:'unknown';
|
|
55
|
-
const queue=[],listeners=new Set(),
|
|
56
|
-
stats={emitted:0,delivered:0,dropped:0,sinkFailures:0};
|
|
57
|
-
let scheduled=false;
|
|
58
|
-
const output=typeof sink==='function'?sink:record=>{
|
|
59
|
-
if(listeners.size){
|
|
60
|
-
for(const listener of [...listeners]){
|
|
61
|
-
try{
|
|
62
|
-
const result=listener(record);
|
|
63
|
-
if(result&&typeof result.then==='function')
|
|
64
|
-
Promise.resolve(result).catch(()=>{stats.sinkFailures++});
|
|
65
|
-
}catch{stats.sinkFailures++}
|
|
66
|
-
}
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if(typeof process==='undefined'||!process.stderr?.write||
|
|
70
|
-
process.env.CHAT_LOOP_API_LOG_STDERR==='0')return;
|
|
71
|
-
process.stderr.write('CHAT_LOOP_API_LOG '+JSON.stringify(record)+'\n');
|
|
72
|
-
};
|
|
73
|
-
const flush=()=>{
|
|
74
|
-
scheduled=false;
|
|
75
|
-
for(let i=0;i<MAX_BATCH&&queue.length;i++){
|
|
76
|
-
const item=queue.shift();
|
|
77
|
-
try{
|
|
78
|
-
const result=output(item);
|
|
79
|
-
stats.delivered++;
|
|
80
|
-
if(result&&typeof result.then==='function')
|
|
81
|
-
Promise.resolve(result).catch(()=>{stats.sinkFailures++});
|
|
82
|
-
}catch{stats.sinkFailures++}
|
|
83
|
-
}
|
|
84
|
-
if(queue.length){scheduled=true;schedule(flush)}
|
|
85
|
-
};
|
|
86
|
-
const emit=event=>{
|
|
87
|
-
try{
|
|
88
|
-
if(queue.length>=MAX_PENDING){stats.dropped++;return}
|
|
89
|
-
queue.push(Object.freeze(event));stats.emitted++;
|
|
90
|
-
if(!scheduled){scheduled=true;schedule(flush)}
|
|
91
|
-
}catch{stats.dropped++}
|
|
92
|
-
};
|
|
93
|
-
function observe(name,fn,surface='sdk',options={}){
|
|
94
|
-
if(typeof fn!=='function')throw TypeError('API_ASPECT_TARGET_REQUIRED');
|
|
95
|
-
const api=safe(name),layer=safe(surface,'sdk');
|
|
96
|
-
return new Proxy(fn,{
|
|
97
|
-
get(target,key,receiver){
|
|
98
|
-
if(key===Symbol.for('chat-loop.api-observed.v1'))return true;
|
|
99
|
-
return Reflect.get(target,key,receiver);
|
|
100
|
-
},
|
|
101
|
-
apply(target,thisArg,args){
|
|
102
|
-
let started,base;
|
|
103
|
-
try{
|
|
104
|
-
// MCP requests are fresh user operations, not children of a
|
|
105
|
-
// long-lived CLI/server bootstrap event listener registration.
|
|
106
|
-
// Browser callbacks lack Node AsyncLocalStorage; a narrowly scoped
|
|
107
|
-
// explicit parent links the async reply to its actual dispatch span.
|
|
108
|
-
const explicit=validParent(options?.parent)?options.parent:null;
|
|
109
|
-
const callId=uid(),parent=explicit||(layer==='mcp'?null:currentApiTrace());
|
|
110
|
-
started=clock();
|
|
111
|
-
base={contract:'chat-loop.api-call.v1',package:packageId,
|
|
112
|
-
api,surface:layer,callId,
|
|
113
|
-
traceId:parent?.traceId||callId,spanId:callId,
|
|
114
|
-
parentSpanId:parent?.spanId||null};
|
|
115
|
-
}catch{/* Never fail a target due to tracing. */}
|
|
116
|
-
const mark=(phase,error)=>{
|
|
117
|
-
if(!base)return;
|
|
118
|
-
try{
|
|
119
|
-
emit({...base,phase,time:new Date().toISOString(),
|
|
120
|
-
durationMs:phase==='BEGIN'?null:Math.max(0,Math.round(clock()-started)),
|
|
121
|
-
errorCode:phase==='FAILED'?code(error):null});
|
|
122
|
-
}catch{/* Non-authoritative telemetry. */}
|
|
123
|
-
};
|
|
124
|
-
mark('BEGIN');
|
|
125
|
-
// Only sanitized correlation IDs may cross from the AOP boundary into
|
|
126
|
-
// the hosting callback. A faulty observer must never affect the call.
|
|
127
|
-
if(base&&typeof options?.onBegin==='function'){
|
|
128
|
-
try{options.onBegin(Object.freeze({traceId:base.traceId,spanId:base.spanId}));}
|
|
129
|
-
catch{stats.sinkFailures++}
|
|
130
|
-
}
|
|
131
|
-
const execute=()=>Reflect.apply(target,thisArg,args);
|
|
132
|
-
let result;
|
|
133
|
-
try{
|
|
134
|
-
const storage=base?contextStore():null;
|
|
135
|
-
result=storage?storage.run({traceId:base.traceId,spanId:base.spanId},execute)
|
|
136
|
-
:execute();
|
|
137
|
-
}catch(e){mark('FAILED',e);throw e}
|
|
138
|
-
const failed=value=>{
|
|
139
|
-
try{return (layer==='mcp'&&value?.isError===true)
|
|
140
|
-
||(layer==='browser'&&api.startsWith('browser.dispatch.')&&value===false)}
|
|
141
|
-
catch{stats.sinkFailures++;return false}
|
|
142
|
-
};
|
|
143
|
-
const failureCode=()=>({code:layer==='browser'
|
|
144
|
-
?'BROWSER_MESSAGE_UNHANDLED':'MCP_TOOL_ERROR'});
|
|
145
|
-
// Do not assimilate arbitrary user thenables for telemetry: invoking
|
|
146
|
-
// their then() could cause a second business side effect. Observe native
|
|
147
|
-
// Promises only, otherwise record the synchronous return boundary.
|
|
148
|
-
let nativePromise=false;
|
|
149
|
-
try{nativePromise=result instanceof Promise}catch{stats.sinkFailures++}
|
|
150
|
-
if(nativePromise){
|
|
151
|
-
// Preserve the exact native Promise object and rejection semantics.
|
|
152
|
-
try{Promise.resolve(result).then(
|
|
153
|
-
value=>{
|
|
154
|
-
const isError=failed(value);
|
|
155
|
-
mark(isError?'FAILED':'OK',
|
|
156
|
-
isError?failureCode():undefined);
|
|
157
|
-
},
|
|
158
|
-
error=>mark('FAILED',error)
|
|
159
|
-
).catch(()=>{stats.sinkFailures++});}
|
|
160
|
-
catch{stats.sinkFailures++}
|
|
161
|
-
return result;
|
|
162
|
-
}
|
|
163
|
-
const isError=failed(result);
|
|
164
|
-
mark(isError?'FAILED':'OK',
|
|
165
|
-
isError?failureCode():undefined);
|
|
166
|
-
return result;
|
|
167
|
-
}});
|
|
168
|
-
}
|
|
169
|
-
function methods(instance,prefix,surface='sdk'){
|
|
170
|
-
if(!instance||typeof instance!=='object')return instance;
|
|
171
|
-
const facade={...instance},cache=new Map();
|
|
172
|
-
return new Proxy(facade,{
|
|
173
|
-
get(object,key,receiver){
|
|
174
|
-
const v=Reflect.get(object,key,receiver);
|
|
175
|
-
if(typeof v!=='function'||typeof key!=='string')return v;
|
|
176
|
-
if(!cache.has(key))cache.set(key,observe(prefix+'.'+key,v,surface));
|
|
177
|
-
return cache.get(key);
|
|
178
|
-
},
|
|
179
|
-
set(){return false},defineProperty(){return false},deleteProperty(){return false}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
const subscribe=observe('subscribeApiLog',listener=>{
|
|
183
|
-
if(typeof listener!=='function')throw TypeError('API_LOG_LISTENER_REQUIRED');
|
|
184
|
-
listeners.add(listener);
|
|
185
|
-
return ()=>listeners.delete(listener);
|
|
186
|
-
},'internal');
|
|
187
|
-
return Object.freeze({observe,methods,subscribe,
|
|
188
|
-
health:()=>Object.freeze({...stats,pending:queue.length}),
|
|
189
|
-
current:currentApiTrace,withParent:withApiTraceParent});
|
|
190
|
-
}
|