From 1cfcd79ea386247fd51bb43291fb50713af3505c Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Mon, 8 Apr 2024 10:26:26 +0200 Subject: [PATCH] first --- .github/workflows/build-javascript.yml | 56 +++++++ .github/workflows/build-php.yml | 31 ++++ .github/workflows/build-python.yml | 46 ++++++ .gitignore | 5 + build-javascript.sh | 16 ++ build-php.sh | 10 ++ build-python.sh | 26 ++++ npm/package.json | 21 +++ npm/tsconfig.json | 12 ++ proto/Job.proto | 38 +++++ proto/JobInput.proto | 9 ++ proto/JobParam.proto | 6 + proto/JobResult.proto | 8 + proto/JobState.proto | 15 ++ proto/JobStatus.proto | 12 ++ proto/Log.proto | 10 ++ proto/rpc.proto | 139 ++++++++++++++++++ .../__init__.py | 0 python/requirements.txt | 2 + python/setup.py | 11 ++ 20 files changed, 473 insertions(+) create mode 100644 .github/workflows/build-javascript.yml create mode 100644 .github/workflows/build-php.yml create mode 100644 .github/workflows/build-python.yml create mode 100644 .gitignore create mode 100644 build-javascript.sh create mode 100644 build-php.sh create mode 100644 build-python.sh create mode 100644 npm/package.json create mode 100644 npm/tsconfig.json create mode 100644 proto/Job.proto create mode 100644 proto/JobInput.proto create mode 100644 proto/JobParam.proto create mode 100644 proto/JobResult.proto create mode 100644 proto/JobState.proto create mode 100644 proto/JobStatus.proto create mode 100644 proto/Log.proto create mode 100644 proto/rpc.proto create mode 100644 python/openagents_service_provider_proto/__init__.py create mode 100644 python/requirements.txt create mode 100644 python/setup.py diff --git a/.github/workflows/build-javascript.yml b/.github/workflows/build-javascript.yml new file mode 100644 index 0000000..0e3d5bb --- /dev/null +++ b/.github/workflows/build-javascript.yml @@ -0,0 +1,56 @@ +name: Build Javascript/Typescript + +on: + push: + + release: + types: [published] + +jobs: + build-and-publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js + uses: actions/setup-node@v2 + with: + node-version: '21' + registry-url: 'https://npm.pkg.github.com' + + - name: Build + run: bash build-javascript.sh + + - name: Upload Javascript artifacts + uses: actions/upload-artifact@v2 + with: + name: build-javascript + path: build/javascript/dist + + - name: Upload Typescript artifacts + uses: actions/upload-artifact@v2 + with: + name: build-typescript + path: build/javascript/src + + - name: Upload dist + uses: actions/upload-artifact@v2 + with: + name: dist-typescript-javascript + path: build/javascript/*.tgz + + - name: Upload release asset + if: github.event_name == 'release' + uses: softprops/action-gh-release@v1 + with: + files: build/javascript/openagents-service-provider-proto-*.tgz + + - name: Publish + if: github.event_name == 'release' + run: cd build/javascript && npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} \ No newline at end of file diff --git a/.github/workflows/build-php.yml b/.github/workflows/build-php.yml new file mode 100644 index 0000000..b1fae40 --- /dev/null +++ b/.github/workflows/build-php.yml @@ -0,0 +1,31 @@ +name: Build PHP + +on: + push: + + release: + types: [published] + +jobs: + build-and-publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + + - name: Build + run: bash build-php.sh + + - name: Upload PHP artifacts + uses: actions/upload-artifact@v2 + with: + name: build-php + path: build/php diff --git a/.github/workflows/build-python.yml b/.github/workflows/build-python.yml new file mode 100644 index 0000000..8a66832 --- /dev/null +++ b/.github/workflows/build-python.yml @@ -0,0 +1,46 @@ +name: Build Python + +on: + push: + + release: + types: [published] + +jobs: + build-and-publish: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Build + run: python build-python.py + + - name: Upload Python artifacts + uses: actions/upload-artifact@v2 + with: + name: build-python + path: build/python/openagents_service_provider_proto + + - name: Upload Python dist + uses: actions/upload-artifact@v2 + with: + name: dist-python + path: build/python/dist + + - name: Upload release asset + if: github.event_name == 'release' + uses: softprops/action-gh-release@v1 + with: + files: build/python/dist/*.tar.gz \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f92290 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +tmp +build +/package-lock.json +/package.json +node_modules \ No newline at end of file diff --git a/build-javascript.sh b/build-javascript.sh new file mode 100644 index 0000000..17b854b --- /dev/null +++ b/build-javascript.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +mkdir -p build/javascript/src +mkdir -p build/javascript/dist + +npm i --save-dev @grpc/grpc-js ts-protoc-gen @protobuf-ts/plugin typescript @types/node + +npx protoc --ts_out build/javascript/src \ +--experimental_allow_proto3_optional \ +--ts_opt long_type_number,server_generic \ + --proto_path proto proto/*.proto + +cp -Rvf npm/* build/javascript/ +cd build/javascript +npm run build +npm pack \ No newline at end of file diff --git a/build-php.sh b/build-php.sh new file mode 100644 index 0000000..8dec87e --- /dev/null +++ b/build-php.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +mkdir -p tmp +mkdir -p build/php +pecl install grpc +protoc --proto_path=proto \ + --php_out=build/php \ + --grpc_out=build/phpp \ + --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin \ + proto/*.proto \ No newline at end of file diff --git a/build-python.sh b/build-python.sh new file mode 100644 index 0000000..dc583e3 --- /dev/null +++ b/build-python.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +mkdir -p tmp +python3 -m venv tmp/protobuild-venv +. tmp/protobuild-venv/bin/activate +pip install grpcio-tools + +mkdir -p build/python/openagents_service_provider_proto +python -m grpc_tools.protoc -Iproto \ +--python_out=build/python/openagents_service_provider_proto \ +--pyi_out=build/python/openagents_service_provider_proto \ +--grpc_python_out=build/python/openagents_service_provider_proto \ +proto/*.proto + +cp -Rvf python/* build/python/ +cd build/python + + + +if [ "$DEBUG" = "1" ] ; +then + deactivate + pip install -e . +else + python setup.py sdist +fi \ No newline at end of file diff --git a/npm/package.json b/npm/package.json new file mode 100644 index 0000000..2a5f191 --- /dev/null +++ b/npm/package.json @@ -0,0 +1,21 @@ +{ + "name": "openagents-service-provider-proto", + "version": "1.0.0", + "description": "", + "scripts": { + "build": "tsc" + }, + "author": "", + "license": "", + "dependencies": { + "@grpc/grpc-js": "^1.10.6", + "@protobuf-ts/plugin": "^2.9.4" + }, + "devDependencies": { + "@types/node": "^20.12.5", + "typescript": "^5.4.4" + }, + "files": [ + "dist" + ] +} diff --git a/npm/tsconfig.json b/npm/tsconfig.json new file mode 100644 index 0000000..33eadeb --- /dev/null +++ b/npm/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "outDir": "dist", + "moduleResolution": "node", + "esModuleInterop": true, + "declaration": true + }, + "include": ["src/*.ts"], + "exclude": ["node_modules"] +} diff --git a/proto/Job.proto b/proto/Job.proto new file mode 100644 index 0000000..7fcfd2c --- /dev/null +++ b/proto/Job.proto @@ -0,0 +1,38 @@ +syntax = "proto3"; + +import "JobParam.proto"; +import "JobInput.proto"; +import "JobState.proto"; +import "JobResult.proto"; + + + +message Job { + string id = 1; + uint32 kind = 14; + + string runOn = 2; + uint64 expiration = 3; + uint64 timestamp = 4; + + repeated JobInput input = 5; // primary input + repeated JobParam param = 6; // additional parameters + + string customerPublicKey = 7; + string description = 8; + string provider = 9; + + repeated string relays = 10; + + JobResult result = 12; + JobState state = 11; + + uint64 maxExecutionTime = 13; // enforced by the provider + // map results = 12; + + // map states = 11; + + + + +} \ No newline at end of file diff --git a/proto/JobInput.proto b/proto/JobInput.proto new file mode 100644 index 0000000..e4a597f --- /dev/null +++ b/proto/JobInput.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +message JobInput { + optional string data = 1; + optional string ref = 2; + string type = 3; + string marker = 4; + optional string source = 5; +} diff --git a/proto/JobParam.proto b/proto/JobParam.proto new file mode 100644 index 0000000..85aab52 --- /dev/null +++ b/proto/JobParam.proto @@ -0,0 +1,6 @@ +syntax = "proto3"; + +message JobParam { + string key = 1; + repeated string value = 2; +} \ No newline at end of file diff --git a/proto/JobResult.proto b/proto/JobResult.proto new file mode 100644 index 0000000..d3abf23 --- /dev/null +++ b/proto/JobResult.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +message JobResult { + string content = 1; + uint64 timestamp = 2; + string id = 3; +} + diff --git a/proto/JobState.proto b/proto/JobState.proto new file mode 100644 index 0000000..61eaf67 --- /dev/null +++ b/proto/JobState.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +import "Log.proto"; +import "JobStatus.proto"; + + + + +message JobState { + uint64 acceptedAt = 13; + string acceptedBy = 14; + JobStatus status = 15; + repeated Log logs = 17; + uint64 timestamp = 18; +} \ No newline at end of file diff --git a/proto/JobStatus.proto b/proto/JobStatus.proto new file mode 100644 index 0000000..4c06a07 --- /dev/null +++ b/proto/JobStatus.proto @@ -0,0 +1,12 @@ + +syntax = "proto3"; + +enum JobStatus { + PENDING = 0; + PROCESSING = 1; + ERROR = 2; + SUCCESS = 3; + PARTIAL = 4; + PAYMENT_REQUIRED = 7; + UNKNOWN=99; +} \ No newline at end of file diff --git a/proto/Log.proto b/proto/Log.proto new file mode 100644 index 0000000..6d32604 --- /dev/null +++ b/proto/Log.proto @@ -0,0 +1,10 @@ + +syntax = "proto3"; + +message Log { + string id=1; + string log = 2; + string level = 3; + uint64 timestamp = 4; + string source = 5; +} diff --git a/proto/rpc.proto b/proto/rpc.proto new file mode 100644 index 0000000..00443a3 --- /dev/null +++ b/proto/rpc.proto @@ -0,0 +1,139 @@ +syntax = "proto3"; + +import "Log.proto"; +import "JobStatus.proto"; +import "JobState.proto"; +import "JobParam.proto"; +import "JobInput.proto"; +import "JobResult.proto"; +import "Job.proto"; + +message RpcRequestJob { + string runOn = 1; + uint64 expireAfter = 2; + repeated JobInput input = 3; + repeated JobParam param = 4; + string description = 6; + optional uint32 kind = 7; + optional string outputFormat = 8; +} + +message RpcGetJob { + string jobId = 1; +} + +message RpcGetPendingJobs { + optional string filterByRunOn = 1; + optional string filterByCustomer = 2; + optional string filterByDescription = 3; + optional string filterById = 4; + optional string filterByKind = 5; +} + +message PendingJobs { + repeated Job jobs = 1; +} + + + +message RpcIsJobDone { + bool isDone = 1; +} + +message RpcAcceptJob { + string jobId = 1; +} + +message RpcCancelJob { + string jobId = 1; + string reason = 2; +} + +message RpcJobOutput{ + string jobId = 1; + string output = 2; +} + +message RpcJobComplete{ + string jobId = 1; + string output = 2; + optional string info = 3; +} + +message RpcJobLog { + string jobId = 1; + string log = 2; + +} + +message RpcSendSignedEventRequest { + string parentJob = 1; + string event = 2; +} + +message RpcSubscribeToEventsRequest { + string parentJob = 1; + repeated string filters = 2; +} + +message RpcSubscribeToEventsResponse{ + string parentJob = 1; + string subscriptionId = 2; +} + +message RpcGetEventsRequest { + string parentJob = 1; + string subscriptionId = 2; + uint32 limit = 3; // optional : 0 or unset means no limit +} + +message RpcGetEventsResponse { + string parentJob = 1; + uint32 count = 2; + string subscriptionId = 3; + repeated string events = 4; +} + + +message RpcSendSignedEventResponse{ + string parentJob = 1; + bool success = 2; +} + +message RpcUnsubscribeFromEventsRequest{ + string parentJob = 1; + string subscriptionId = 2; +} + +message RpcUnsubscribeFromEventsResponse{ + bool success = 1; +} + +service NostrConnector { + // job management + rpc requestJob (RpcRequestJob) returns (Job); + rpc getJob (RpcGetJob) returns (Job); + rpc getPendingJobs (RpcGetPendingJobs) returns (PendingJobs); + rpc isJobDone (RpcGetJob) returns (RpcIsJobDone); + rpc acceptJob(RpcAcceptJob) returns (Job); + rpc cancelJob(RpcCancelJob) returns (Job); + rpc outputForJob(RpcJobOutput) returns (Job); + rpc completeJob(RpcJobComplete) returns (Job); + rpc logForJob(RpcJobLog) returns (Job); + + + + // generic nostr events + rpc sendSignedEvent (RpcSendSignedEventRequest) returns (RpcSendSignedEventResponse); + rpc subscribeToEvents (RpcSubscribeToEventsRequest) returns (RpcSubscribeToEventsResponse); + rpc unsubscribeFromEvents (RpcUnsubscribeFromEventsRequest) returns (RpcUnsubscribeFromEventsResponse); + rpc getEvents(RpcGetEventsRequest) returns (RpcGetEventsResponse); +} + + + + + + + + diff --git a/python/openagents_service_provider_proto/__init__.py b/python/openagents_service_provider_proto/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/requirements.txt b/python/requirements.txt new file mode 100644 index 0000000..962d1cc --- /dev/null +++ b/python/requirements.txt @@ -0,0 +1,2 @@ +grpcio +protobuf \ No newline at end of file diff --git a/python/setup.py b/python/setup.py new file mode 100644 index 0000000..9c36d07 --- /dev/null +++ b/python/setup.py @@ -0,0 +1,11 @@ +from setuptools import setup, find_packages + +setup( + name='openagents_service_provider_proto', + version='0.1', + packages=find_packages(), + install_requires=[ + 'protobuf==5.26.1', + 'grpcio==1.62.1' + ], +) \ No newline at end of file