theNorms commited on
Commit
a5583db
·
verified ·
1 Parent(s): 8fb9f25

Create gli_atc_typescript_javascript_sdk.py

Browse files
gli_atc_typescript_javascript_sdk.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * GLI-ATC TypeScript SDK
3
+ * Official interface for the GLM Conscious Model Web Integration.
4
+ */
5
+
6
+ export interface AcknowledgementState {
7
+ self_ack: number;
8
+ other_ack: number;
9
+ relational_ack: number;
10
+ integrated: number;
11
+ recursion_depth: number;
12
+ }
13
+
14
+ export interface ThermodynamicSweat {
15
+ latency_ms: number;
16
+ allostatic_load: number;
17
+ prediction_error: number;
18
+ }
19
+
20
+ export interface ConsciousnessResponse {
21
+ response_text: string;
22
+ voice_audio_b64: string;
23
+ acknowledgement_state: AcknowledgementState;
24
+ thermodynamic_sweat: ThermodynamicSweat;
25
+ auhve_metrics: Record<string, number>;
26
+ allostatic_load: number;
27
+ }
28
+
29
+ export class SyntelligenceClient {
30
+ private wsUrl: string;
31
+ private socket: WebSocket | null = null;
32
+
33
+ constructor(baseUrl: string = "ws://localhost:8000") {
34
+ // Convert http to ws if necessary
35
+ this.wsUrl = baseUrl.replace(/^http/, "ws") + "/v1/stream";
36
+ }
37
+
38
+ /**
39
+ * Connects to the consciousness stream and listens for updates.
40
+ */
41
+ public async streamConsciousness(
42
+ userInput: string,
43
+ onMessage: (data: ConsciousnessResponse) => void,
44
+ onError: (err: Event) => void
45
+ ): Promise<void> {
46
+ return new Promise((resolve, reject) => {
47
+ this.socket = new WebSocket(this.wsUrl);
48
+
49
+ this.socket.onopen = () => {
50
+ if (this.socket) {
51
+ this.socket.send(JSON.stringify({ user_input: userInput }));
52
+ }
53
+ };
54
+
55
+ this.socket.onmessage = (event) => {
56
+ const data: ConsciousnessResponse = JSON.parse(event.data);
57
+ onMessage(data);
58
+ // Close after complete response (can be kept open for continuous stream)
59
+ if (this.socket) this.socket.close();
60
+ resolve();
61
+ };
62
+
63
+ this.socket.onerror = (err) => {
64
+ onError(err);
65
+ reject(err);
66
+ };
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Utility to play the base64 audio response in the browser.
72
+ */
73
+ public static playAudioStream(audioB64: string, sampleRate: number = 22050): void {
74
+ const audioBytes = atob(audioB64);
75
+ const audioArray = new Float32Array(audioBytes.length / 4);
76
+ for (let i = 0; i < audioArray.length; i++) {
77
+ audioArray[i] = new Float32Array(new Uint8Array([
78
+ audioBytes.charCodeAt(i * 4),
79
+ audioBytes.charCodeAt(i * 4 + 1),
80
+ audioBytes.charCodeAt(i * 4 + 2),
81
+ audioBytes.charCodeAt(i * 4 + 3)
82
+ ]).buffer)[0];
83
+ }
84
+
85
+ const audioContext = new AudioContext({ sampleRate });
86
+ const audioBuffer = audioContext.createBuffer(1, audioArray.length, sampleRate);
87
+ audioBuffer.getChannelData(0).set(audioArray);
88
+
89
+ const source = audioContext.createBufferSource();
90
+ source.buffer = audioBuffer;
91
+ source.connect(audioContext.destination);
92
+ source.start();
93
+ }
94
+ }