directus-extension-adk-connector 1.0.0
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 +1 -0
- package/dist/index.js +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# directus-adk-endpoint
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import e from"https";import{randomUUID as s}from"crypto";class r{constructor(s,r={}){if(this.appUrl=s||process.env.ADK_APP_URL,this.ignoreSslErrors=r.ignoreSslErrors??"true"===process.env.GOOGLE_ADK_IGNORE_SSL,!this.appUrl)throw new Error("Google ADK App URL is required. Set ADK_APP_URL environment variable or pass it to constructor.");this.ignoreSslErrors&&(this.httpsAgent=new e.Agent({rejectUnauthorized:!1}))}getFetchOptions(e={}){const s={...e,headers:{"Content-Type":"application/json",...e.headers}};return this.ignoreSslErrors&&this.appUrl.startsWith("https:")&&(s.agent=this.httpsAgent),s}async createSession(e,s,r,t={}){const a=`${this.appUrl}/apps/${e}/users/${s}/sessions/${r}`;try{const e=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({state:t})});if(!e.ok)throw new Error(`Failed to create session: ${e.status} ${e.statusText}`);return await e.json()}catch(e){throw new Error(`Session creation failed: ${e.message}`)}}async sendChatMessage(e,s,r,t,a=!1){const o=`${this.appUrl}/run_sse`,n={app_name:e,user_id:s,session_id:r,new_message:t,streaming:a};console.log("Sending chat message:",n);try{const e=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});if(!e.ok)throw new Error(`Failed to send chat message: ${e.status} ${e.statusText}`);return a?e:await e.json()}catch(e){throw new Error(`Chat message failed: ${e.message}`)}}async streamToSSE(e,s){console.log("Streaming ADK response...");const r=e.body.getReader(),t=new TextDecoder;try{for(;;){const{done:e,value:a}=await r.read();if(e)break;const o=t.decode(a);s.write(o)}}catch(e){console.error("Error streaming ADK response:",e),s.write(`data: {"error": "Streaming error: ${e.message}"}\n\n`)}finally{r.releaseLock()}}createMessage(e,s){return{role:e,parts:[{text:s}]}}}var t={id:"agentchat",handler:(e,{services:t,logger:a})=>{console.log("Initializing Google ADK Service"),a.info("Initializing Google ADK Service");const{UsersService:o}=t,n=new r;e.post("/stream",(async(e,r)=>{r.setHeader("Content-Type","text/event-stream"),r.setHeader("Cache-Control","no-cache"),r.setHeader("Connection","keep-alive"),console.log("Try to build UsersService.");const t=new o({schema:e.schema,accountability:e.accountability});try{console.log("Try to fetch readOne User.");const o=await t.readOne(e.accountability.user),{app_name:i,session_id:c,prompt:d,message:l,state:p={}}=e.body,g=i||"personal_brand_manager",h=o.id,u=c||`${s()}`;if(!c){console.log("No session_id provided, creating a new session:",u);try{const e=await n.createSession(g,h,u,p);console.log("ADK session created! sessionId:",e.id)}catch(e){return a.error("Failed to create ADK session:",e),r.write(`data: {"error": "Failed to create session: ${e.message}"}\n\n`),void r.end()}}let m;if(!d)return r.write("data: {\"error\": \"Either 'prompt' or 'message' is required\"}\n\n"),void r.end();m=n.createMessage("user",d);try{const e=await n.sendChatMessage(g,h,u,m,!0);console.log("ADK response received:",e),await n.streamToSSE(e,r)}catch(e){a.error("ADK streaming error:",e),r.write(`data: {"error": "Streaming failed: ${e.message}"}\n\n`)}}catch(e){a.error("User service error:",e),r.write(`data: {"error": "Service unavailable: ${e.message}"}\n\n`)}r.end()})),e.post("/session",(async(e,s)=>{if(!e.accountability?.user)return s.status(403).json({error:"Authentication required"});const r=new o({schema:e.schema,accountability:e.accountability});try{const t=await r.readOne(e.accountability.user),{app_name:a,session_id:o,state:i={}}=e.body;if(!a||!o)return s.status(400).json({error:"app_name and session_id are required"});const c=t.id||e.accountability.user,d=await n.createSession(a,c,o,i);s.json({success:!0,session_id:o,user_id:c,app_name:a,result:d})}catch(e){a.error("Session creation error:",e),s.status(500).json({error:e.message})}}))}};export{t as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "directus-extension-adk-connector",
|
|
3
|
+
"description": "Please enter a description for your extension",
|
|
4
|
+
"icon": "extension",
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"directus",
|
|
8
|
+
"directus-extension",
|
|
9
|
+
"directus-extension-endpoint",
|
|
10
|
+
"google-adk"
|
|
11
|
+
],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"directus:extension": {
|
|
17
|
+
"type": "endpoint",
|
|
18
|
+
"path": "dist/index.js",
|
|
19
|
+
"source": "src/index.js",
|
|
20
|
+
"host": "^10.10.0"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "directus-extension build",
|
|
24
|
+
"dev": "directus-extension build -w --no-minify",
|
|
25
|
+
"link": "directus-extension link",
|
|
26
|
+
"validate": "directus-extension validate"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@directus/extensions-sdk": "13.1.1"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@directus/errors": "^2.0.1",
|
|
33
|
+
"crypto": "^1.0.1"
|
|
34
|
+
},
|
|
35
|
+
"main": "index.js",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/elevenwaves/directus-adk-endpoint.git"
|
|
39
|
+
},
|
|
40
|
+
"author": "hennroja",
|
|
41
|
+
"license": "ISC",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/elevenwaves/directus-adk-endpoint/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/elevenwaves/directus-adk-endpoint#readme"
|
|
46
|
+
}
|