Compare commits
2 Commits
5f0b09b555
...
v0.1.1
Author | SHA1 | Date | |
---|---|---|---|
4ad1d3edf6
|
|||
55bf725491
|
@@ -45,13 +45,7 @@
|
|||||||
"Bash(git add:*)",
|
"Bash(git add:*)",
|
||||||
"Bash(git commit:*)",
|
"Bash(git commit:*)",
|
||||||
"Bash(git push:*)",
|
"Bash(git push:*)",
|
||||||
"Bash(git tag:*)",
|
"Bash(git tag:*)"
|
||||||
"Bash(../bin/ailog:*)",
|
|
||||||
"Bash(../target/release/ailog oauth build:*)",
|
|
||||||
"Bash(ailog:*)",
|
|
||||||
"WebFetch(domain:plc.directory)",
|
|
||||||
"WebFetch(domain:atproto.com)",
|
|
||||||
"WebFetch(domain:syu.is)"
|
|
||||||
],
|
],
|
||||||
"deny": []
|
"deny": []
|
||||||
}
|
}
|
||||||
|
51
.github/workflows/build-binary.yml
vendored
Normal file
51
.github/workflows/build-binary.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: Build Binary
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch: # Manual trigger
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
paths:
|
||||||
|
- 'src/**'
|
||||||
|
- 'Cargo.toml'
|
||||||
|
- 'Cargo.lock'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
override: true
|
||||||
|
|
||||||
|
- name: Cache cargo registry
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ~/.cargo/registry
|
||||||
|
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Cache cargo index
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ~/.cargo/git
|
||||||
|
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Cache target directory
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: target
|
||||||
|
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
|
||||||
|
- name: Build binary
|
||||||
|
run: cargo build --release
|
||||||
|
|
||||||
|
- name: Upload binary
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: ailog-linux
|
||||||
|
path: target/release/ailog
|
||||||
|
retention-days: 30
|
65
.github/workflows/cloudflare-pages.yml
vendored
65
.github/workflows/cloudflare-pages.yml
vendored
@@ -34,67 +34,22 @@ jobs:
|
|||||||
|
|
||||||
- name: Copy OAuth build to static
|
- name: Copy OAuth build to static
|
||||||
run: |
|
run: |
|
||||||
# Remove old assets (following run.zsh pattern)
|
mkdir -p my-blog/static/assets
|
||||||
rm -rf my-blog/static/assets
|
cp -r oauth/dist/assets/* my-blog/static/assets/
|
||||||
# Copy all dist files to static
|
cp oauth/dist/index.html my-blog/static/oauth/index.html || true
|
||||||
cp -rf oauth/dist/* my-blog/static/
|
|
||||||
# Copy index.html to oauth-assets.html template
|
|
||||||
cp oauth/dist/index.html my-blog/templates/oauth-assets.html
|
|
||||||
|
|
||||||
- name: Cache ailog binary
|
- name: Setup Rust
|
||||||
uses: actions/cache@v4
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
path: ./bin
|
toolchain: stable
|
||||||
key: ailog-bin-${{ runner.os }}
|
|
||||||
restore-keys: |
|
- name: Build ailog
|
||||||
ailog-bin-${{ runner.os }}
|
run: cargo build --release
|
||||||
|
|
||||||
- name: Setup ailog binary
|
|
||||||
run: |
|
|
||||||
# Get expected version from Cargo.toml
|
|
||||||
EXPECTED_VERSION=$(grep '^version' Cargo.toml | cut -d'"' -f2)
|
|
||||||
echo "Expected version from Cargo.toml: $EXPECTED_VERSION"
|
|
||||||
|
|
||||||
# Check current binary version if exists
|
|
||||||
if [ -f "./bin/ailog" ]; then
|
|
||||||
CURRENT_VERSION=$(./bin/ailog --version 2>/dev/null || echo "unknown")
|
|
||||||
echo "Current binary version: $CURRENT_VERSION"
|
|
||||||
else
|
|
||||||
CURRENT_VERSION="none"
|
|
||||||
echo "No binary found"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check OS
|
|
||||||
OS="${{ runner.os }}"
|
|
||||||
echo "Runner OS: $OS"
|
|
||||||
|
|
||||||
# Use pre-packaged binary if version matches or extract from tar.gz
|
|
||||||
if [ "$CURRENT_VERSION" = "$EXPECTED_VERSION" ]; then
|
|
||||||
echo "Binary is up to date"
|
|
||||||
chmod +x ./bin/ailog
|
|
||||||
elif [ "$OS" = "Linux" ] && [ -f "./bin/ailog-linux-x86_64.tar.gz" ]; then
|
|
||||||
echo "Extracting ailog from pre-packaged tar.gz..."
|
|
||||||
cd bin
|
|
||||||
tar -xzf ailog-linux-x86_64.tar.gz
|
|
||||||
chmod +x ailog
|
|
||||||
cd ..
|
|
||||||
|
|
||||||
# Verify extracted version
|
|
||||||
EXTRACTED_VERSION=$(./bin/ailog --version 2>/dev/null || echo "unknown")
|
|
||||||
echo "Extracted binary version: $EXTRACTED_VERSION"
|
|
||||||
|
|
||||||
if [ "$EXTRACTED_VERSION" != "$EXPECTED_VERSION" ]; then
|
|
||||||
echo "Warning: Binary version mismatch. Expected $EXPECTED_VERSION but got $EXTRACTED_VERSION"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "Error: No suitable binary found for OS: $OS"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Build site with ailog
|
- name: Build site with ailog
|
||||||
run: |
|
run: |
|
||||||
cd my-blog
|
cd my-blog
|
||||||
../bin/ailog build
|
../target/release/ailog build
|
||||||
|
|
||||||
- name: List public directory
|
- name: List public directory
|
||||||
run: |
|
run: |
|
||||||
|
92
.github/workflows/disabled/gh-pages-fast.yml
vendored
92
.github/workflows/disabled/gh-pages-fast.yml
vendored
@@ -1,92 +0,0 @@
|
|||||||
name: github pages (fast)
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
paths-ignore:
|
|
||||||
- 'src/**'
|
|
||||||
- 'Cargo.toml'
|
|
||||||
- 'Cargo.lock'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
pages: write
|
|
||||||
id-token: write
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Cache ailog binary
|
|
||||||
uses: actions/cache@v4
|
|
||||||
with:
|
|
||||||
path: ./bin
|
|
||||||
key: ailog-bin-${{ runner.os }}
|
|
||||||
restore-keys: |
|
|
||||||
ailog-bin-${{ runner.os }}
|
|
||||||
|
|
||||||
- name: Setup ailog binary
|
|
||||||
run: |
|
|
||||||
# Get expected version from Cargo.toml
|
|
||||||
EXPECTED_VERSION=$(grep '^version' Cargo.toml | cut -d'"' -f2)
|
|
||||||
echo "Expected version from Cargo.toml: $EXPECTED_VERSION"
|
|
||||||
|
|
||||||
# Check current binary version if exists
|
|
||||||
if [ -f "./bin/ailog" ]; then
|
|
||||||
CURRENT_VERSION=$(./bin/ailog --version 2>/dev/null || echo "unknown")
|
|
||||||
echo "Current binary version: $CURRENT_VERSION"
|
|
||||||
else
|
|
||||||
CURRENT_VERSION="none"
|
|
||||||
echo "No binary found"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check OS
|
|
||||||
OS="${{ runner.os }}"
|
|
||||||
echo "Runner OS: $OS"
|
|
||||||
|
|
||||||
# Use pre-packaged binary if version matches or extract from tar.gz
|
|
||||||
if [ "$CURRENT_VERSION" = "$EXPECTED_VERSION" ]; then
|
|
||||||
echo "Binary is up to date"
|
|
||||||
chmod +x ./bin/ailog
|
|
||||||
elif [ "$OS" = "Linux" ] && [ -f "./bin/ailog-linux-x86_64.tar.gz" ]; then
|
|
||||||
echo "Extracting ailog from pre-packaged tar.gz..."
|
|
||||||
cd bin
|
|
||||||
tar -xzf ailog-linux-x86_64.tar.gz
|
|
||||||
chmod +x ailog
|
|
||||||
cd ..
|
|
||||||
|
|
||||||
# Verify extracted version
|
|
||||||
EXTRACTED_VERSION=$(./bin/ailog --version 2>/dev/null || echo "unknown")
|
|
||||||
echo "Extracted binary version: $EXTRACTED_VERSION"
|
|
||||||
|
|
||||||
if [ "$EXTRACTED_VERSION" != "$EXPECTED_VERSION" ]; then
|
|
||||||
echo "Warning: Binary version mismatch. Expected $EXPECTED_VERSION but got $EXTRACTED_VERSION"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
echo "Error: No suitable binary found for OS: $OS"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Setup Hugo
|
|
||||||
uses: peaceiris/actions-hugo@v3
|
|
||||||
with:
|
|
||||||
hugo-version: "0.139.2"
|
|
||||||
extended: true
|
|
||||||
|
|
||||||
- name: Build with ailog
|
|
||||||
env:
|
|
||||||
TZ: "Asia/Tokyo"
|
|
||||||
run: |
|
|
||||||
# Use pre-built ailog binary instead of cargo build
|
|
||||||
cd my-blog
|
|
||||||
../bin/ailog build
|
|
||||||
touch ./public/.nojekyll
|
|
||||||
|
|
||||||
- name: Deploy
|
|
||||||
uses: peaceiris/actions-gh-pages@v3
|
|
||||||
with:
|
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
publish_dir: ./my-blog/public
|
|
||||||
publish_branch: gh-pages
|
|
77
.github/workflows/gh-pages-fast.yml
vendored
Normal file
77
.github/workflows/gh-pages-fast.yml
vendored
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
name: github pages (fast)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
paths-ignore:
|
||||||
|
- 'src/**'
|
||||||
|
- 'Cargo.toml'
|
||||||
|
- 'Cargo.lock'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Cache ailog binary
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ./bin
|
||||||
|
key: ailog-bin-${{ runner.os }}
|
||||||
|
restore-keys: |
|
||||||
|
ailog-bin-${{ runner.os }}
|
||||||
|
|
||||||
|
- name: Check and update ailog binary
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: |
|
||||||
|
# Get latest release version
|
||||||
|
LATEST_VERSION=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
|
||||||
|
https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .tag_name)
|
||||||
|
echo "Latest version: $LATEST_VERSION"
|
||||||
|
|
||||||
|
# Check current binary version if exists
|
||||||
|
mkdir -p ./bin
|
||||||
|
if [ -f "./bin/ailog" ]; then
|
||||||
|
CURRENT_VERSION=$(./bin/ailog --version | awk '{print $2}' || echo "unknown")
|
||||||
|
echo "Current version: $CURRENT_VERSION"
|
||||||
|
else
|
||||||
|
CURRENT_VERSION="none"
|
||||||
|
echo "No binary found"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Download if version is different or binary doesn't exist
|
||||||
|
if [ "$CURRENT_VERSION" != "${LATEST_VERSION#v}" ]; then
|
||||||
|
echo "Downloading ailog $LATEST_VERSION..."
|
||||||
|
curl -sL -H "Authorization: Bearer $GITHUB_TOKEN" \
|
||||||
|
https://github.com/${{ github.repository }}/releases/download/$LATEST_VERSION/ailog-linux-x86_64.tar.gz | tar -xzf -
|
||||||
|
mv ailog ./bin/ailog
|
||||||
|
chmod +x ./bin/ailog
|
||||||
|
echo "Updated to version: $(./bin/ailog --version)"
|
||||||
|
else
|
||||||
|
echo "Binary is up to date"
|
||||||
|
chmod +x ./bin/ailog
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Setup Hugo
|
||||||
|
uses: peaceiris/actions-hugo@v3
|
||||||
|
with:
|
||||||
|
hugo-version: "0.139.2"
|
||||||
|
extended: true
|
||||||
|
|
||||||
|
- name: Build with ailog
|
||||||
|
env:
|
||||||
|
TZ: "Asia/Tokyo"
|
||||||
|
run: |
|
||||||
|
# Use pre-built ailog binary instead of cargo build
|
||||||
|
./bin/ailog build --output ./public
|
||||||
|
touch ./public/.nojekyll
|
||||||
|
|
||||||
|
- name: Deploy
|
||||||
|
uses: peaceiris/actions-gh-pages@v3
|
||||||
|
with:
|
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
publish_dir: ./public
|
||||||
|
publish_branch: gh-pages
|
51
.github/workflows/release.yml
vendored
51
.github/workflows/release.yml
vendored
@@ -11,10 +11,6 @@ on:
|
|||||||
required: true
|
required: true
|
||||||
default: 'v0.1.0'
|
default: 'v0.1.0'
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
actions: read
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
OPENSSL_STATIC: true
|
OPENSSL_STATIC: true
|
||||||
@@ -24,7 +20,6 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
name: Build ${{ matrix.target }}
|
name: Build ${{ matrix.target }}
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
timeout-minutes: 60
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
@@ -32,6 +27,10 @@ jobs:
|
|||||||
os: ubuntu-latest
|
os: ubuntu-latest
|
||||||
artifact_name: ailog
|
artifact_name: ailog
|
||||||
asset_name: ailog-linux-x86_64
|
asset_name: ailog-linux-x86_64
|
||||||
|
- target: x86_64-unknown-linux-musl
|
||||||
|
os: ubuntu-latest
|
||||||
|
artifact_name: ailog
|
||||||
|
asset_name: ailog-linux-x86_64-musl
|
||||||
- target: aarch64-unknown-linux-gnu
|
- target: aarch64-unknown-linux-gnu
|
||||||
os: ubuntu-latest
|
os: ubuntu-latest
|
||||||
artifact_name: ailog
|
artifact_name: ailog
|
||||||
@@ -44,6 +43,10 @@ jobs:
|
|||||||
os: macos-latest
|
os: macos-latest
|
||||||
artifact_name: ailog
|
artifact_name: ailog
|
||||||
asset_name: ailog-macos-aarch64
|
asset_name: ailog-macos-aarch64
|
||||||
|
- target: x86_64-pc-windows-msvc
|
||||||
|
os: windows-latest
|
||||||
|
artifact_name: ailog.exe
|
||||||
|
asset_name: ailog-windows-x86_64.exe
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -54,10 +57,16 @@ jobs:
|
|||||||
targets: ${{ matrix.target }}
|
targets: ${{ matrix.target }}
|
||||||
|
|
||||||
- name: Install cross-compilation tools (Linux)
|
- name: Install cross-compilation tools (Linux)
|
||||||
if: matrix.os == 'ubuntu-latest' && matrix.target == 'aarch64-unknown-linux-gnu'
|
if: matrix.os == 'ubuntu-latest'
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu
|
sudo apt-get install -y gcc-multilib
|
||||||
|
if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]]; then
|
||||||
|
sudo apt-get install -y gcc-aarch64-linux-gnu
|
||||||
|
fi
|
||||||
|
if [[ "${{ matrix.target }}" == "x86_64-unknown-linux-musl" ]]; then
|
||||||
|
sudo apt-get install -y musl-tools
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Configure cross-compilation (Linux ARM64)
|
- name: Configure cross-compilation (Linux ARM64)
|
||||||
if: matrix.target == 'aarch64-unknown-linux-gnu'
|
if: matrix.target == 'aarch64-unknown-linux-gnu'
|
||||||
@@ -86,17 +95,11 @@ jobs:
|
|||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
cd target/${{ matrix.target }}/release
|
cd target/${{ matrix.target }}/release
|
||||||
|
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
|
||||||
# Use appropriate strip command for cross-compilation
|
strip ${{ matrix.artifact_name }} || true
|
||||||
if [[ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]]; then
|
|
||||||
aarch64-linux-gnu-strip ${{ matrix.artifact_name }} || echo "Strip failed, continuing..."
|
|
||||||
elif [[ "${{ matrix.os }}" == "windows-latest" ]]; then
|
|
||||||
strip ${{ matrix.artifact_name }} || echo "Strip failed, continuing..."
|
|
||||||
else
|
else
|
||||||
strip ${{ matrix.artifact_name }} || echo "Strip failed, continuing..."
|
strip ${{ matrix.artifact_name }}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create archive
|
|
||||||
if [[ "${{ matrix.target }}" == *"windows"* ]]; then
|
if [[ "${{ matrix.target }}" == *"windows"* ]]; then
|
||||||
7z a ../../../${{ matrix.asset_name }}.zip ${{ matrix.artifact_name }}
|
7z a ../../../${{ matrix.asset_name }}.zip ${{ matrix.artifact_name }}
|
||||||
else
|
else
|
||||||
@@ -107,15 +110,14 @@ jobs:
|
|||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: ${{ matrix.asset_name }}
|
name: ${{ matrix.asset_name }}
|
||||||
path: ${{ matrix.asset_name }}.tar.gz
|
path: |
|
||||||
|
${{ matrix.asset_name }}.tar.gz
|
||||||
|
${{ matrix.asset_name }}.zip
|
||||||
|
|
||||||
release:
|
release:
|
||||||
name: Create Release
|
name: Create Release
|
||||||
needs: build
|
needs: build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
actions: read
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
@@ -135,8 +137,9 @@ jobs:
|
|||||||
echo "- AI comment system" >> release_notes.md
|
echo "- AI comment system" >> release_notes.md
|
||||||
echo "" >> release_notes.md
|
echo "" >> release_notes.md
|
||||||
echo "### Platforms" >> release_notes.md
|
echo "### Platforms" >> release_notes.md
|
||||||
echo "- Linux (x86_64, aarch64)" >> release_notes.md
|
echo "- Linux (x86_64, aarch64, musl)" >> release_notes.md
|
||||||
echo "- macOS (Intel, Apple Silicon)" >> release_notes.md
|
echo "- macOS (Intel, Apple Silicon)" >> release_notes.md
|
||||||
|
echo "- Windows (x86_64)" >> release_notes.md
|
||||||
echo "" >> release_notes.md
|
echo "" >> release_notes.md
|
||||||
echo "### Installation" >> release_notes.md
|
echo "### Installation" >> release_notes.md
|
||||||
echo "\`\`\`bash" >> release_notes.md
|
echo "\`\`\`bash" >> release_notes.md
|
||||||
@@ -145,6 +148,8 @@ jobs:
|
|||||||
echo "chmod +x ailog" >> release_notes.md
|
echo "chmod +x ailog" >> release_notes.md
|
||||||
echo "sudo mv ailog /usr/local/bin/" >> release_notes.md
|
echo "sudo mv ailog /usr/local/bin/" >> release_notes.md
|
||||||
echo "" >> release_notes.md
|
echo "" >> release_notes.md
|
||||||
|
echo "# Windows" >> release_notes.md
|
||||||
|
echo "# Extract ailog-windows-x86_64.zip and add to PATH" >> release_notes.md
|
||||||
echo "\`\`\`" >> release_notes.md
|
echo "\`\`\`" >> release_notes.md
|
||||||
|
|
||||||
- name: Get tag name
|
- name: Get tag name
|
||||||
@@ -164,6 +169,8 @@ jobs:
|
|||||||
body_path: release_notes.md
|
body_path: release_notes.md
|
||||||
draft: false
|
draft: false
|
||||||
prerelease: ${{ contains(steps.tag_name.outputs.tag, 'alpha') || contains(steps.tag_name.outputs.tag, 'beta') || contains(steps.tag_name.outputs.tag, 'rc') }}
|
prerelease: ${{ contains(steps.tag_name.outputs.tag, 'alpha') || contains(steps.tag_name.outputs.tag, 'beta') || contains(steps.tag_name.outputs.tag, 'rc') }}
|
||||||
files: artifacts/*/ailog-*.tar.gz
|
files: |
|
||||||
|
artifacts/*/ailog-*.tar.gz
|
||||||
|
artifacts/*/ailog-*.zip
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
8
.gitignore
vendored
8
.gitignore
vendored
@@ -5,15 +5,9 @@
|
|||||||
*.swo
|
*.swo
|
||||||
*~
|
*~
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
cloudflare-config.yml
|
||||||
my-blog/public/
|
my-blog/public/
|
||||||
dist
|
dist
|
||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
my-blog/static/assets/comment-atproto-*
|
my-blog/static/assets/comment-atproto-*
|
||||||
bin/ailog
|
|
||||||
docs
|
|
||||||
my-blog/static/index.html
|
|
||||||
my-blog/templates/oauth-assets.html
|
|
||||||
cloudflared-config.yml
|
|
||||||
.config
|
|
||||||
oauth-server-example
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ailog"
|
name = "ailog"
|
||||||
version = "0.1.9"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["syui"]
|
authors = ["syui"]
|
||||||
description = "A static blog generator with AI features"
|
description = "A static blog generator with AI features"
|
||||||
@@ -49,7 +49,6 @@ regex = "1.0"
|
|||||||
tokio-tungstenite = { version = "0.21", features = ["rustls-tls-webpki-roots", "connect"], default-features = false }
|
tokio-tungstenite = { version = "0.21", features = ["rustls-tls-webpki-roots", "connect"], default-features = false }
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
tungstenite = { version = "0.21", features = ["rustls-tls-webpki-roots"], default-features = false }
|
tungstenite = { version = "0.21", features = ["rustls-tls-webpki-roots"], default-features = false }
|
||||||
rpassword = "7.3"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.14"
|
tempfile = "3.14"
|
||||||
|
32
Dockerfile
Normal file
32
Dockerfile
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Multi-stage build for ailog
|
||||||
|
FROM rust:1.75 as builder
|
||||||
|
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
COPY Cargo.toml Cargo.lock ./
|
||||||
|
COPY src ./src
|
||||||
|
|
||||||
|
RUN cargo build --release
|
||||||
|
|
||||||
|
FROM debian:bookworm-slim
|
||||||
|
|
||||||
|
# Install runtime dependencies
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
ca-certificates \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy the binary
|
||||||
|
COPY --from=builder /usr/src/app/target/release/ailog /usr/local/bin/ailog
|
||||||
|
|
||||||
|
# Copy blog content
|
||||||
|
COPY my-blog ./blog
|
||||||
|
|
||||||
|
# Build static site
|
||||||
|
RUN ailog build blog
|
||||||
|
|
||||||
|
# Expose port
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
# Run server
|
||||||
|
CMD ["ailog", "serve", "blog"]
|
108
action.yml
Normal file
108
action.yml
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
name: 'ailog Static Site Generator'
|
||||||
|
description: 'AI-powered static blog generator with atproto integration'
|
||||||
|
author: 'syui'
|
||||||
|
|
||||||
|
branding:
|
||||||
|
icon: 'book-open'
|
||||||
|
color: 'orange'
|
||||||
|
|
||||||
|
inputs:
|
||||||
|
content-dir:
|
||||||
|
description: 'Content directory containing markdown files'
|
||||||
|
required: false
|
||||||
|
default: 'content'
|
||||||
|
output-dir:
|
||||||
|
description: 'Output directory for generated site'
|
||||||
|
required: false
|
||||||
|
default: 'public'
|
||||||
|
template-dir:
|
||||||
|
description: 'Template directory'
|
||||||
|
required: false
|
||||||
|
default: 'templates'
|
||||||
|
static-dir:
|
||||||
|
description: 'Static assets directory'
|
||||||
|
required: false
|
||||||
|
default: 'static'
|
||||||
|
config-file:
|
||||||
|
description: 'Configuration file path'
|
||||||
|
required: false
|
||||||
|
default: 'ailog.toml'
|
||||||
|
ai-integration:
|
||||||
|
description: 'Enable AI features'
|
||||||
|
required: false
|
||||||
|
default: 'true'
|
||||||
|
atproto-integration:
|
||||||
|
description: 'Enable atproto/OAuth features'
|
||||||
|
required: false
|
||||||
|
default: 'true'
|
||||||
|
|
||||||
|
outputs:
|
||||||
|
site-url:
|
||||||
|
description: 'Generated site URL'
|
||||||
|
value: ${{ steps.generate.outputs.site-url }}
|
||||||
|
build-time:
|
||||||
|
description: 'Build time in seconds'
|
||||||
|
value: ${{ steps.generate.outputs.build-time }}
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: 'composite'
|
||||||
|
steps:
|
||||||
|
- name: Setup Rust
|
||||||
|
uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
override: true
|
||||||
|
|
||||||
|
- name: Install ailog
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
if [ ! -f "./target/release/ailog" ]; then
|
||||||
|
cargo build --release
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Setup Node.js for OAuth app
|
||||||
|
if: ${{ inputs.atproto-integration == 'true' }}
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '20'
|
||||||
|
|
||||||
|
- name: Build OAuth app
|
||||||
|
if: ${{ inputs.atproto-integration == 'true' }}
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
if [ -d "oauth" ]; then
|
||||||
|
cd oauth
|
||||||
|
npm install
|
||||||
|
npm run build
|
||||||
|
cp -r dist/* ../${{ inputs.static-dir }}/
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Generate site
|
||||||
|
id: generate
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
start_time=$(date +%s)
|
||||||
|
|
||||||
|
./target/release/ailog generate \
|
||||||
|
--input ${{ inputs.content-dir }} \
|
||||||
|
--output ${{ inputs.output-dir }} \
|
||||||
|
--templates ${{ inputs.template-dir }} \
|
||||||
|
--static ${{ inputs.static-dir }} \
|
||||||
|
--config ${{ inputs.config-file }}
|
||||||
|
|
||||||
|
end_time=$(date +%s)
|
||||||
|
build_time=$((end_time - start_time))
|
||||||
|
|
||||||
|
echo "build-time=${build_time}" >> $GITHUB_OUTPUT
|
||||||
|
echo "site-url=file://$(pwd)/${{ inputs.output-dir }}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Display build summary
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
echo "✅ ailog build completed successfully"
|
||||||
|
echo "📁 Output directory: ${{ inputs.output-dir }}"
|
||||||
|
echo "⏱️ Build time: ${{ steps.generate.outputs.build-time }}s"
|
||||||
|
if [ -d "${{ inputs.output-dir }}" ]; then
|
||||||
|
echo "📄 Generated files:"
|
||||||
|
find ${{ inputs.output-dir }} -type f | head -10
|
||||||
|
fi
|
Binary file not shown.
14
claude.md
14
claude.md
@@ -1,19 +1,5 @@
|
|||||||
# エコシステム統合設計書
|
# エコシステム統合設計書
|
||||||
|
|
||||||
## 注意事項
|
|
||||||
|
|
||||||
`console.log`は絶対に書かないようにしてください。
|
|
||||||
|
|
||||||
ハードコードしないようにしてください。必ず、`./my-blog/config.toml`や`./oauth/.env.production`を使用するように。または`~/.config/syui/ai/log/config.json`を使用するように。
|
|
||||||
|
|
||||||
重複する名前のenvを作らないようにしてください。新しい環境変数を作る際は必ず検討してください。
|
|
||||||
|
|
||||||
```sh
|
|
||||||
# ダメな例
|
|
||||||
VITE_OAUTH_COLLECTION_USER=ai.syui.log.user
|
|
||||||
VITE_OAUTH_COLLECTION_CHAT=ai.syui.log.chat
|
|
||||||
```
|
|
||||||
|
|
||||||
## 中核思想
|
## 中核思想
|
||||||
- **存在子理論**: この世界で最も小さいもの(存在子/ai)の探求
|
- **存在子理論**: この世界で最も小さいもの(存在子/ai)の探求
|
||||||
- **唯一性原則**: 現実の個人の唯一性をすべてのシステムで担保
|
- **唯一性原則**: 現実の個人の唯一性をすべてのシステムで担保
|
||||||
|
18
cloudflared-config.yml
Normal file
18
cloudflared-config.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
tunnel: ec5a422d-7678-4e73-bf38-6105ffd4766a
|
||||||
|
credentials-file: /Users/syui/.cloudflared/ec5a422d-7678-4e73-bf38-6105ffd4766a.json
|
||||||
|
|
||||||
|
ingress:
|
||||||
|
- hostname: log.syui.ai
|
||||||
|
service: http://localhost:4173
|
||||||
|
originRequest:
|
||||||
|
noHappyEyeballs: true
|
||||||
|
|
||||||
|
- hostname: ollama.syui.ai
|
||||||
|
service: http://localhost:11434
|
||||||
|
originRequest:
|
||||||
|
noHappyEyeballs: true
|
||||||
|
httpHostHeader: "localhost:11434"
|
||||||
|
# Cloudflare Accessを無効化する場合は以下をコメントアウト
|
||||||
|
# accessPolicy: bypass
|
||||||
|
|
||||||
|
- service: http_status:404
|
@@ -16,16 +16,16 @@ auto_translate = false
|
|||||||
comment_moderation = false
|
comment_moderation = false
|
||||||
ask_ai = true
|
ask_ai = true
|
||||||
provider = "ollama"
|
provider = "ollama"
|
||||||
model = "gemma3:4b"
|
model = "gemma3:2b"
|
||||||
host = "https://ollama.syui.ai"
|
host = "https://ollama.syui.ai"
|
||||||
system_prompt = "あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。"
|
system_prompt = "you are a helpful ai assistant trained on this blog's content. you can answer questions about the articles, provide insights, and help users understand the topics discussed."
|
||||||
handle = "ai.syui.ai"
|
ai_did = "did:plc:4hqjfn7m6n5hno3doamuhgef"
|
||||||
#num_predict = 200
|
|
||||||
|
|
||||||
[oauth]
|
[oauth]
|
||||||
json = "client-metadata.json"
|
json = "client-metadata.json"
|
||||||
redirect = "oauth/callback"
|
redirect = "oauth/callback"
|
||||||
admin = "ai.syui.ai"
|
admin = "did:plc:uqzpqmrjnptsxezjx4xuh2mn"
|
||||||
collection = "ai.syui.log"
|
collection_comment = "ai.syui.log"
|
||||||
pds = "syu.is"
|
collection_user = "ai.syui.log.user"
|
||||||
handle_list = ["syui.syui.ai", "yui.syui.ai", "ai.syui.ai", "syui.syu.is", "ai.syu.is", "ai.ai"]
|
collection_chat = "ai.syui.log.chat"
|
||||||
|
bsky_api = "https://public.api.bsky.app"
|
||||||
|
@@ -6,7 +6,7 @@ tags: ["blog", "rust", "mcp", "atp"]
|
|||||||
language: ["ja", "en"]
|
language: ["ja", "en"]
|
||||||
---
|
---
|
||||||
|
|
||||||
rustで静的サイトジェネレータを作りました。[ailog](https://git.syui.ai/ai/log)といいます。`hugo`からの移行になります。
|
rustで静的サイトジェネレータを作りました。。[ailog](https://git.syui.ai/ai/log)といいます。`hugo`からの移行になります。
|
||||||
|
|
||||||
`ailog`は、最初にatproto-comment-system(oauth)とask-AIという機能をつけました。
|
`ailog`は、最初にatproto-comment-system(oauth)とask-AIという機能をつけました。
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ $ git clone https://git.syui.ai/ai/log
|
|||||||
$ cd log
|
$ cd log
|
||||||
$ cargo build
|
$ cargo build
|
||||||
$ ./target/debug/ailog init my-blog
|
$ ./target/debug/ailog init my-blog
|
||||||
$ ./target/debug/ailog serve my-blog
|
$ ./target/debug/ailog server my-blog
|
||||||
```
|
```
|
||||||
|
|
||||||
## install
|
## install
|
||||||
@@ -30,7 +30,7 @@ $ export RUSTUP_HOME="$HOME/.rustup"
|
|||||||
$ export PATH="$HOME/.cargo/bin:$PATH"
|
$ export PATH="$HOME/.cargo/bin:$PATH"
|
||||||
---
|
---
|
||||||
$ which ailog
|
$ which ailog
|
||||||
$ ailog -h
|
$ ailog
|
||||||
```
|
```
|
||||||
|
|
||||||
## build deploy
|
## build deploy
|
||||||
@@ -57,28 +57,24 @@ $ npm run build
|
|||||||
$ npm run preview
|
$ npm run preview
|
||||||
```
|
```
|
||||||
|
|
||||||
```sh:ouath/.env.production
|
```sh
|
||||||
# Production environment variables
|
# Production environment variables
|
||||||
VITE_APP_HOST=https://syui.ai
|
VITE_APP_HOST=https://example.com
|
||||||
VITE_OAUTH_CLIENT_ID=https://syui.ai/client-metadata.json
|
VITE_OAUTH_CLIENT_ID=https://example.com/client-metadata.json
|
||||||
VITE_OAUTH_REDIRECT_URI=https://syui.ai/oauth/callback
|
VITE_OAUTH_REDIRECT_URI=https://example.com/oauth/callback
|
||||||
VITE_ADMIN_DID=did:plc:uqzpqmrjnptsxezjx4xuh2mn
|
VITE_ADMIN_DID=did:plc:uqzpqmrjnptsxezjx4xuh2mn
|
||||||
|
|
||||||
# Base collection (all others are derived via getCollectionNames)
|
# Collection names for OAuth app
|
||||||
VITE_OAUTH_COLLECTION=ai.syui.log
|
VITE_COLLECTION_COMMENT=ai.syui.log
|
||||||
|
VITE_COLLECTION_USER=ai.syui.log.user
|
||||||
|
VITE_COLLECTION_CHAT=ai.syui.log.chat
|
||||||
|
|
||||||
# AI Configuration
|
# Collection names for ailog (backward compatibility)
|
||||||
VITE_AI_ENABLED=true
|
AILOG_COLLECTION_COMMENT=ai.syui.log
|
||||||
VITE_AI_ASK_AI=true
|
AILOG_COLLECTION_USER=ai.syui.log.user
|
||||||
VITE_AI_PROVIDER=ollama
|
|
||||||
VITE_AI_MODEL=gemma3:4b
|
|
||||||
VITE_AI_HOST=https://ollama.syui.ai
|
|
||||||
VITE_AI_SYSTEM_PROMPT="ai"
|
|
||||||
VITE_AI_DID=did:plc:4hqjfn7m6n5hno3doamuhgef
|
|
||||||
|
|
||||||
# API Configuration
|
# API Configuration
|
||||||
VITE_BSKY_PUBLIC_API=https://public.api.bsky.app
|
VITE_BSKY_PUBLIC_API=https://public.api.bsky.app
|
||||||
VITE_ATPROTO_API=https://bsky.social
|
|
||||||
```
|
```
|
||||||
|
|
||||||
これは`ailog oauth build my-blog`で`./my-blog/config.toml`から`./oauth/.env.production`が生成されます。
|
これは`ailog oauth build my-blog`で`./my-blog/config.toml`から`./oauth/.env.production`が生成されます。
|
||||||
@@ -119,8 +115,15 @@ $ cloudflared tunnel --config cloudflared-config.yml run
|
|||||||
$ cloudflared tunnel route dns ${uuid} example.com
|
$ cloudflared tunnel route dns ${uuid} example.com
|
||||||
```
|
```
|
||||||
|
|
||||||
|
以下の2つのcollection recordを生成します。ユーザーには`ai.syui.log`が生成され、ここにコメントが記録されます。それを取得して表示しています。`ai.syui.log.user`は管理者である`VITE_ADMIN_DID`用です。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ ailog auth init
|
VITE_COLLECTION_COMMENT=ai.syui.log
|
||||||
|
VITE_COLLECTION_USER=ai.syui.log.user
|
||||||
|
```
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ ailog auth login
|
||||||
$ ailog stream server
|
$ ailog stream server
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -132,9 +135,8 @@ $ ailog stream server
|
|||||||
|
|
||||||
`ask-AI`の仕組みは割愛します。後に変更される可能性が高いと思います。
|
`ask-AI`の仕組みは割愛します。後に変更される可能性が高いと思います。
|
||||||
|
|
||||||
`llm`, `mcp`, `atproto`などの組み合わせです。
|
local llm, mcp, atprotoと組み合わせです。
|
||||||
|
|
||||||
現在、`/index.json`を監視して、更新があれば、翻訳などを行い自動ポストする機能があります。
|
|
||||||
|
|
||||||
## code syntax
|
## code syntax
|
||||||
|
|
||||||
|
@@ -6,10 +6,10 @@ tags: ["blog", "cloudflare", "github"]
|
|||||||
draft: false
|
draft: false
|
||||||
---
|
---
|
||||||
|
|
||||||
ブログを移行しました。過去のブログは[syui.github.io](https://syui.github.io)にありあます。
|
ブログを移行しました。
|
||||||
|
|
||||||
1. `gh-pages`から`cf-pages`への移行になります。
|
1. `gh-pages`から`cf-pages`への移行になります。
|
||||||
2. 自作の`ailog`でbuildしています。
|
2. `hugo`からの移行で、自作の`ailog`でbuildしています。
|
||||||
3. 特徴としては、`atproto`, `AI`との連携です。
|
3. 特徴としては、`atproto`, `AI`との連携です。
|
||||||
|
|
||||||
```yml:.github/workflows/cloudflare-pages.yml
|
```yml:.github/workflows/cloudflare-pages.yml
|
||||||
@@ -60,7 +60,3 @@ jobs:
|
|||||||
wranglerVersion: '3'
|
wranglerVersion: '3'
|
||||||
```
|
```
|
||||||
|
|
||||||
## url
|
|
||||||
|
|
||||||
- [https://syui.pages.dev](https://syui.pages.dev)
|
|
||||||
- [https://syui.github.io](https://syui.github.io)
|
|
||||||
|
@@ -1,7 +0,0 @@
|
|||||||
{{ $dateFormat := default "Mon Jan 2, 2006" (index .Site.Params "date_format") }}
|
|
||||||
{{ $utcFormat := "2006-01-02T15:04:05Z07:00" }}
|
|
||||||
{{- $.Scratch.Add "index" slice -}}
|
|
||||||
{{- range .Site.RegularPages -}}
|
|
||||||
{{- $.Scratch.Add "index" (dict "title" .Title "tags" .Params.tags "description" .Description "categories" .Params.categories "contents" .Plain "href" .Permalink "utc_time" (.Date.Format $utcFormat) "formated_time" (.Date.Format $dateFormat)) -}}
|
|
||||||
{{- end -}}
|
|
||||||
{{- $.Scratch.Get "index" | jsonify -}}
|
|
@@ -1,20 +0,0 @@
|
|||||||
# Production environment variables
|
|
||||||
VITE_APP_HOST=https://syui.ai
|
|
||||||
VITE_OAUTH_CLIENT_ID=https://syui.ai/client-metadata.json
|
|
||||||
VITE_OAUTH_REDIRECT_URI=https://syui.ai/oauth/callback
|
|
||||||
|
|
||||||
# Handle-based Configuration (DIDs resolved at runtime)
|
|
||||||
VITE_ATPROTO_PDS=syu.is
|
|
||||||
VITE_ADMIN_HANDLE=ai.syui.ai
|
|
||||||
VITE_AI_HANDLE=ai.syui.ai
|
|
||||||
VITE_OAUTH_COLLECTION=ai.syui.log
|
|
||||||
VITE_ATPROTO_WEB_URL=https://bsky.app
|
|
||||||
VITE_ATPROTO_HANDLE_LIST=["syui.syui.ai", "yui.syui.ai", "ai.syui.ai", "syui.syu.is", "ai.syu.is", "ai.ai"]
|
|
||||||
|
|
||||||
# AI Configuration
|
|
||||||
VITE_AI_ENABLED=true
|
|
||||||
VITE_AI_ASK_AI=true
|
|
||||||
VITE_AI_PROVIDER=ollama
|
|
||||||
VITE_AI_MODEL=gemma3:4b
|
|
||||||
VITE_AI_HOST=https://ollama.syui.ai
|
|
||||||
VITE_AI_SYSTEM_PROMPT="あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。"
|
|
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
/css/*
|
/css/*
|
||||||
Content-Type: text/css
|
Content-Type: text/css
|
||||||
Cache-Control: no-cache
|
Cache-Control: public, max-age=60
|
||||||
|
|
||||||
/*.js
|
/*.js
|
||||||
Content-Type: application/javascript
|
Content-Type: application/javascript
|
||||||
|
@@ -1,3 +1,9 @@
|
|||||||
|
# AI機能をai.gpt MCP serverにリダイレクト
|
||||||
|
/api/ask https://ai-gpt-mcp.syui.ai/ask 200
|
||||||
|
|
||||||
|
# Ollama API proxy (Cloudflare Workers)
|
||||||
|
/api/ollama-proxy https://ollama-proxy.YOUR-SUBDOMAIN.workers.dev/:splat 200
|
||||||
|
|
||||||
# OAuth routes
|
# OAuth routes
|
||||||
/oauth/* /oauth/index.html 200
|
/oauth/* /oauth/index.html 200
|
||||||
|
|
||||||
|
@@ -59,7 +59,7 @@ a.view-markdown:any-link {
|
|||||||
.container {
|
.container {
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: auto 0fr 1fr auto;
|
grid-template-rows: auto auto 1fr auto;
|
||||||
grid-template-areas:
|
grid-template-areas:
|
||||||
"header"
|
"header"
|
||||||
"ask-ai"
|
"ask-ai"
|
||||||
@@ -158,15 +158,6 @@ a.view-markdown:any-link {
|
|||||||
background: #f6f8fa;
|
background: #f6f8fa;
|
||||||
border-bottom: 1px solid #d1d9e0;
|
border-bottom: 1px solid #d1d9e0;
|
||||||
padding: 24px;
|
padding: 24px;
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.ask-ai-panel[style*="block"] {
|
|
||||||
display: block !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container:has(.ask-ai-panel[style*="block"]) {
|
|
||||||
grid-template-rows: auto auto 1fr auto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.ask-ai-content {
|
.ask-ai-content {
|
||||||
@@ -202,15 +193,13 @@ a.view-markdown:any-link {
|
|||||||
grid-area: main;
|
grid-area: main;
|
||||||
max-width: 1000px;
|
max-width: 1000px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
/* padding: 24px; */
|
padding: 24px;
|
||||||
padding-top: 80px;
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 1000px) {
|
@media (max-width: 1000px) {
|
||||||
.main-content {
|
.main-content {
|
||||||
/* padding: 20px; */
|
padding: 20px;
|
||||||
padding: 0px;
|
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,7 +237,7 @@ a.view-markdown:any-link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.post-title a {
|
.post-title a {
|
||||||
color: var(--theme-color);
|
color: #1f2328;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
@@ -335,10 +324,6 @@ a.view-markdown:any-link {
|
|||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
article.article-content {
|
|
||||||
padding: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.article-meta {
|
.article-meta {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 16px;
|
gap: 16px;
|
||||||
@@ -363,7 +348,6 @@ article.article-content {
|
|||||||
.article-actions {
|
.article-actions {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
padding: 15px 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.action-btn {
|
.action-btn {
|
||||||
@@ -533,21 +517,25 @@ article.article-content {
|
|||||||
margin: 16px 0;
|
margin: 16px 0;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* File name display for code blocks - top bar style */
|
/* File name display for code blocks */
|
||||||
.article-body pre[data-filename]::before {
|
.article-body pre[data-filename]::before {
|
||||||
content: attr(data-filename);
|
content: attr(data-filename);
|
||||||
display: block;
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
background: #2D2D30;
|
background: #2D2D30;
|
||||||
color: #AE81FF;
|
color: #CCCCCC;
|
||||||
padding: 8px 16px;
|
padding: 4px 12px;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
font-family: 'SF Mono', 'Monaco', 'Cascadia Code', 'Roboto Mono', monospace;
|
font-family: 'SF Mono', 'Monaco', 'Cascadia Code', 'Roboto Mono', monospace;
|
||||||
border-bottom: 1px solid #3E3D32;
|
border-bottom-left-radius: 4px;
|
||||||
margin: 0;
|
border: 1px solid #3E3D32;
|
||||||
width: 100%;
|
border-top: none;
|
||||||
box-sizing: border-box;
|
border-right: none;
|
||||||
|
z-index: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.article-body pre code {
|
.article-body pre code {
|
||||||
@@ -560,11 +548,6 @@ article.article-content {
|
|||||||
line-height: 1.4;
|
line-height: 1.4;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Adjust padding when filename is present */
|
|
||||||
.article-body pre[data-filename] code {
|
|
||||||
padding: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Inline code (not in pre blocks) */
|
/* Inline code (not in pre blocks) */
|
||||||
.article-body code {
|
.article-body code {
|
||||||
background: var(--light-white);
|
background: var(--light-white);
|
||||||
@@ -797,7 +780,7 @@ article.article-content {
|
|||||||
|
|
||||||
@media (max-width: 1000px) {
|
@media (max-width: 1000px) {
|
||||||
.main-header {
|
.main-header {
|
||||||
padding: 0px;
|
padding: 12px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.header-content {
|
.header-content {
|
||||||
@@ -807,48 +790,6 @@ article.article-content {
|
|||||||
gap: 0;
|
gap: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* OAuth app mobile fixes */
|
|
||||||
.comment-item {
|
|
||||||
padding: 0px !important;
|
|
||||||
margin: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section {
|
|
||||||
padding: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comments-list {
|
|
||||||
padding: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-section {
|
|
||||||
max-width: 100% !important;
|
|
||||||
padding: 0px !important;
|
|
||||||
margin: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-container {
|
|
||||||
max-width: 100% !important;
|
|
||||||
padding: 0px !important;
|
|
||||||
margin: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-content {
|
|
||||||
padding: 10px !important;
|
|
||||||
word-wrap: break-word !important;
|
|
||||||
overflow-wrap: break-word !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-header {
|
|
||||||
padding: 10px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fix comment-meta URI overflow */
|
|
||||||
.comment-meta {
|
|
||||||
word-break: break-all !important;
|
|
||||||
overflow-wrap: break-word !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Hide site title text on mobile */
|
/* Hide site title text on mobile */
|
||||||
.site-title {
|
.site-title {
|
||||||
display: none;
|
display: none;
|
||||||
@@ -876,13 +817,13 @@ article.article-content {
|
|||||||
justify-self: end;
|
justify-self: end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ask AI button mobile style - icon only */
|
/* Ask AI button mobile style */
|
||||||
.ask-ai-btn {
|
.ask-ai-btn {
|
||||||
padding: 8px;
|
padding: 8px;
|
||||||
min-width: 40px;
|
min-width: 40px;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
|
font-size: 0;
|
||||||
gap: 0;
|
gap: 0;
|
||||||
font-size: 0; /* Hide all text content */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.ask-ai-btn .ai-icon {
|
.ask-ai-btn .ai-icon {
|
||||||
@@ -912,16 +853,6 @@ article.article-content {
|
|||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Mobile filename display */
|
|
||||||
.article-body pre[data-filename]::before {
|
|
||||||
padding: 6px 12px;
|
|
||||||
font-size: 11px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.article-body pre[data-filename] code {
|
|
||||||
padding: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.article-body code {
|
.article-body code {
|
||||||
word-break: break-all;
|
word-break: break-all;
|
||||||
}
|
}
|
||||||
@@ -939,11 +870,11 @@ article.article-content {
|
|||||||
padding: 16px;
|
padding: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.article-title {
|
.article-title {
|
||||||
font-size: 24px;
|
font-size: 24px;
|
||||||
padding: 30px 0px;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.message-header .avatar {
|
.message-header .avatar {
|
||||||
width: 32px;
|
width: 32px;
|
||||||
height: 32px;
|
height: 32px;
|
||||||
@@ -960,4 +891,4 @@ article.article-content {
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
3
my-blog/static/index.html
Normal file
3
my-blog/static/index.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<!-- OAuth Comment System - Load globally for session management -->
|
||||||
|
<script type="module" crossorigin src="/assets/comment-atproto-Do1JWJCw.js"></script>
|
||||||
|
<link rel="stylesheet" crossorigin href="/assets/comment-atproto-CPKYAM8U.css">
|
@@ -1,31 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"categories": [],
|
|
||||||
"contents": "ブログを移行しました。過去のブログはsyui.github.ioにありあます。 gh-pagesからcf-pagesへの移行になります。 自作のailogでbuildしています。 特徴としては、atproto, AIとの連携です。 name: Deploy to Cloudflare Pages on: push: branches: - main workflow_dispatch: jobs: deploy: runs-on: ubuntu-latest permissions: contents: read deployments: write steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Rust uses: actions-rs/toolchain@v1 with: toolchain: stable - name: Build ailog run: cargo build --release - name: Build site with ailog run: | cd my-blog ../target/release/ailog build - name: List public directory run: | ls -la my-blog/public/ - name: Deploy to Cloudflare Pages uses: cloudflare/pages-action@v1 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} projectName: ${{ secrets.CLOUDFLARE_PROJECT_NAME }} directory: my-blog/public gitHubToken: ${{ secrets.GITHUB_TOKEN }} wranglerVersion: '3' url https://syui.pages.dev https://syui.github.io",
|
|
||||||
"description": "ブログを移行しました。過去のブログはsyui.github.ioにありあます。 \n\ngh-pagesからcf-pagesへの移行になります。\n自作のailogでbuildしています。\n特徴としては、atproto, AIとの連携です。\n\nname: Deploy to Cloudflare Pages\n\non:\n push:\n branches:\n - main\n workfl...",
|
|
||||||
"formated_time": "Sat Jun 14, 2025",
|
|
||||||
"href": "https://syui.ai/posts/2025-06-14-blog.html",
|
|
||||||
"tags": [
|
|
||||||
"blog",
|
|
||||||
"cloudflare",
|
|
||||||
"github"
|
|
||||||
],
|
|
||||||
"title": "ブログを移行した",
|
|
||||||
"utc_time": "2025-06-14T00:00:00Z"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"categories": [],
|
|
||||||
"contents": "rustで静的サイトジェネレータを作りました。ailogといいます。hugoからの移行になります。 ailogは、最初にatproto-comment-system(oauth)とask-AIという機能をつけました。 quick start $ git clone https://git.syui.ai/ai/log $ cd log $ cargo build $ ./target/debug/ailog init my-blog $ ./target/debug/ailog serve my-blog install $ cargo install --path . --- $ export CARGO_HOME="$HOME/.cargo" $ export RUSTUP_HOME="$HOME/.rustup" $ export PATH="$HOME/.cargo/bin:$PATH" --- $ which ailog $ ailog -h build deploy $ cd my-blog $ vim config.toml $ ailog new test $ vim content/posts/`date +"%Y-%m-%d"`.md $ ailog build # publicの中身をweb-serverにdeploy $ cp -rf ./public/* ./web-server/root/ atproto-comment-system example $ cd ./oauth $ npm i $ npm run build $ npm run preview # Production environment variables VITE_APP_HOST=https://example.com VITE_OAUTH_CLIENT_ID=https://example.com/client-metadata.json VITE_OAUTH_REDIRECT_URI=https://example.com/oauth/callback VITE_ADMIN_DID=did:plc:uqzpqmrjnptsxezjx4xuh2mn # Collection names for OAuth app VITE_COLLECTION_COMMENT=ai.syui.log VITE_COLLECTION_USER=ai.syui.log.user VITE_COLLECTION_CHAT=ai.syui.log.chat # Collection names for ailog (backward compatibility) AILOG_COLLECTION_COMMENT=ai.syui.log AILOG_COLLECTION_USER=ai.syui.log.user # API Configuration VITE_BSKY_PUBLIC_API=https://public.api.bsky.app これはailog oauth build my-blogで./my-blog/config.tomlから./oauth/.env.productionが生成されます。 $ ailog oauth build my-blog use 簡単に説明すると、./oauthで生成するのがatproto-comment-systemです。 <script type="module" crossorigin src="/assets/comment-atproto-${hash}}.js"></script> <link rel="stylesheet" crossorigin href="/assets/comment-atproto-${hash}.css"> <section class="comment-section"> <div id="comment-atproto"></div> </section> ただし、oauthであるため、色々と大変です。本番環境(もしくは近い形)でテストを行いましょう。cf, tailscale, ngrokなど。 tunnel: ${hash} credentials-file: ${path}.json ingress: - hostname: example.com service: http://localhost:4173 originRequest: noHappyEyeballs: true - service: http_status:404 # tunnel list, dnsに登録が必要です $ cloudflared tunnel list $ cloudflared tunnel --config cloudflared-config.yml run $ cloudflared tunnel route dns ${uuid} example.com 以下の2つのcollection recordを生成します。ユーザーにはai.syui.logが生成され、ここにコメントが記録されます。それを取得して表示しています。ai.syui.log.userは管理者であるVITE_ADMIN_DID用です。 VITE_COLLECTION_COMMENT=ai.syui.log VITE_COLLECTION_USER=ai.syui.log.user $ ailog auth login $ ailog stream server このコマンドでai.syui.logをjetstreamから監視して、書き込みがあれば、管理者のai.syui.log.userに記録され、そのuser-listに基づいて、コメント一覧を取得します。 つまり、コメント表示のアカウントを手動で設定するか、自動化するか。自動化するならserverでailog stream serverを動かさなければいけません。 ask-AI ask-AIの仕組みは割愛します。後に変更される可能性が高いと思います。 local llm, mcp, atprotoと組み合わせです。 code syntax # comment d=${0:a:h} // This is a comment fn main() { println!("Hello, world!"); } // This is a comment console.log("Hello, world!");",
|
|
||||||
"description": "rustで静的サイトジェネレータを作りました。ailogといいます。hugoからの移行になります。 \nailogは、最初にatproto-comment-system(oauth)とask-AIという機能をつけました。 \nquick start\n$ git clone https://git.syui.ai/ai/log\n$ cd log\n$ cargo build\n$ ./target/debu...",
|
|
||||||
"formated_time": "Thu Jun 12, 2025",
|
|
||||||
"href": "https://syui.ai/posts/2025-06-06-ailog.html",
|
|
||||||
"tags": [
|
|
||||||
"blog",
|
|
||||||
"rust",
|
|
||||||
"mcp",
|
|
||||||
"atp"
|
|
||||||
],
|
|
||||||
"title": "静的サイトジェネレータを作った",
|
|
||||||
"utc_time": "2025-06-12T00:00:00Z"
|
|
||||||
}
|
|
||||||
]
|
|
@@ -1,295 +1,360 @@
|
|||||||
/**
|
/**
|
||||||
* Ask AI functionality - Based on original working implementation
|
* Ask AI functionality - Pure JavaScript, no jQuery dependency
|
||||||
*/
|
*/
|
||||||
|
class AskAI {
|
||||||
// Global variables for AI functionality
|
constructor() {
|
||||||
let aiProfileData = null;
|
this.isReady = false;
|
||||||
|
this.aiProfile = null;
|
||||||
// Original functions from working implementation
|
this.init();
|
||||||
function toggleAskAI() {
|
|
||||||
const panel = document.getElementById('askAiPanel');
|
|
||||||
const isVisible = panel.style.display !== 'none';
|
|
||||||
panel.style.display = isVisible ? 'none' : 'block';
|
|
||||||
|
|
||||||
if (!isVisible) {
|
|
||||||
checkAuthenticationStatus();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function checkAuthenticationStatus() {
|
init() {
|
||||||
const userSections = document.querySelectorAll('.user-section');
|
this.setupEventListeners();
|
||||||
const isAuthenticated = userSections.length > 0;
|
this.checkAuthOnLoad();
|
||||||
|
}
|
||||||
if (isAuthenticated) {
|
|
||||||
// User is authenticated - show Ask AI UI
|
setupEventListeners() {
|
||||||
document.getElementById('authCheck').style.display = 'none';
|
// Listen for AI ready signal
|
||||||
document.getElementById('chatForm').style.display = 'block';
|
window.addEventListener('aiChatReady', () => {
|
||||||
document.getElementById('chatHistory').style.display = 'block';
|
this.isReady = true;
|
||||||
|
console.log('AI Chat is ready');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for AI profile updates
|
||||||
|
window.addEventListener('aiProfileLoaded', (event) => {
|
||||||
|
this.aiProfile = event.detail;
|
||||||
|
console.log('AI profile loaded:', this.aiProfile);
|
||||||
|
this.updateButton();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for AI responses
|
||||||
|
window.addEventListener('aiResponseReceived', (event) => {
|
||||||
|
this.handleAIResponse(event.detail);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Keyboard shortcuts
|
||||||
|
document.addEventListener('keydown', (e) => {
|
||||||
|
if (e.key === 'Escape') {
|
||||||
|
this.hide();
|
||||||
|
}
|
||||||
|
if (e.key === 'Enter' && e.target.id === 'aiQuestion' && !e.shiftKey) {
|
||||||
|
e.preventDefault();
|
||||||
|
this.ask();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Monitor authentication changes
|
||||||
|
this.observeAuth();
|
||||||
|
}
|
||||||
|
|
||||||
|
toggle() {
|
||||||
|
const panel = document.getElementById('askAiPanel');
|
||||||
|
const isVisible = panel.style.display !== 'none';
|
||||||
|
|
||||||
// Show initial greeting if chat history is empty
|
if (isVisible) {
|
||||||
|
this.hide();
|
||||||
|
} else {
|
||||||
|
this.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
show() {
|
||||||
|
const panel = document.getElementById('askAiPanel');
|
||||||
|
panel.style.display = 'block';
|
||||||
|
this.checkAuth();
|
||||||
|
}
|
||||||
|
|
||||||
|
hide() {
|
||||||
|
const panel = document.getElementById('askAiPanel');
|
||||||
|
panel.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
checkAuth() {
|
||||||
|
const userSections = document.querySelectorAll('.user-section');
|
||||||
|
const isAuthenticated = userSections.length > 0;
|
||||||
|
|
||||||
|
const authCheck = document.getElementById('authCheck');
|
||||||
|
const chatForm = document.getElementById('chatForm');
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
if (chatHistory.children.length === 0) {
|
|
||||||
showInitialGreeting();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Focus on input
|
if (isAuthenticated) {
|
||||||
|
authCheck.style.display = 'none';
|
||||||
|
chatForm.style.display = 'block';
|
||||||
|
chatHistory.style.display = 'block';
|
||||||
|
|
||||||
|
if (chatHistory.children.length === 0) {
|
||||||
|
this.showGreeting();
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
document.getElementById('aiQuestion').focus();
|
||||||
|
}, 50);
|
||||||
|
} else {
|
||||||
|
authCheck.style.display = 'block';
|
||||||
|
chatForm.style.display = 'none';
|
||||||
|
chatHistory.style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkAuthOnLoad() {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
document.getElementById('aiQuestion').focus();
|
this.checkAuth();
|
||||||
}, 50);
|
}, 500);
|
||||||
} else {
|
|
||||||
// User not authenticated - show auth message
|
|
||||||
document.getElementById('authCheck').style.display = 'block';
|
|
||||||
document.getElementById('chatForm').style.display = 'none';
|
|
||||||
document.getElementById('chatHistory').style.display = 'none';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function askQuestion() {
|
observeAuth() {
|
||||||
const question = document.getElementById('aiQuestion').value;
|
const observer = new MutationObserver(() => {
|
||||||
if (!question.trim()) return;
|
const userSections = document.querySelectorAll('.user-section');
|
||||||
|
if (userSections.length > 0) {
|
||||||
const askButton = document.getElementById('askButton');
|
this.checkAuth();
|
||||||
askButton.disabled = true;
|
observer.disconnect();
|
||||||
askButton.textContent = 'Posting...';
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
// Add user message to chat
|
|
||||||
addUserMessage(question);
|
|
||||||
|
|
||||||
// Clear input
|
|
||||||
document.getElementById('aiQuestion').value = '';
|
|
||||||
|
|
||||||
// Show loading
|
|
||||||
showLoadingMessage();
|
|
||||||
|
|
||||||
// Post question via OAuth app
|
|
||||||
window.dispatchEvent(new CustomEvent('postAIQuestion', {
|
|
||||||
detail: { question: question }
|
|
||||||
}));
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to ask question:', error);
|
|
||||||
showErrorMessage('Sorry, I encountered an error. Please try again.');
|
|
||||||
} finally {
|
|
||||||
askButton.disabled = false;
|
|
||||||
askButton.textContent = 'Ask';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addUserMessage(question) {
|
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
|
||||||
const userSection = document.querySelector('.user-section');
|
|
||||||
|
|
||||||
let userAvatar = '👤';
|
|
||||||
let userDisplay = 'You';
|
|
||||||
let userHandle = 'user';
|
|
||||||
|
|
||||||
if (userSection) {
|
|
||||||
const avatarImg = userSection.querySelector('.user-avatar');
|
|
||||||
const displayName = userSection.querySelector('.user-display-name');
|
|
||||||
const handle = userSection.querySelector('.user-handle');
|
|
||||||
|
|
||||||
if (avatarImg && avatarImg.src) {
|
|
||||||
userAvatar = `<img src="${avatarImg.src}" alt="${displayName?.textContent || 'User'}" class="profile-avatar">`;
|
|
||||||
}
|
|
||||||
if (displayName?.textContent) {
|
|
||||||
userDisplay = displayName.textContent;
|
|
||||||
}
|
|
||||||
if (handle?.textContent) {
|
|
||||||
userHandle = handle.textContent.replace('@', '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const questionDiv = document.createElement('div');
|
|
||||||
questionDiv.className = 'chat-message user-message comment-style';
|
|
||||||
questionDiv.innerHTML = `
|
|
||||||
<div class="message-header">
|
|
||||||
<div class="avatar">${userAvatar}</div>
|
|
||||||
<div class="user-info">
|
|
||||||
<div class="display-name">${userDisplay}</div>
|
|
||||||
<div class="handle">@${userHandle}</div>
|
|
||||||
<div class="timestamp">${new Date().toLocaleString()}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="message-content">${question}</div>
|
|
||||||
`;
|
|
||||||
chatHistory.appendChild(questionDiv);
|
|
||||||
}
|
|
||||||
|
|
||||||
function showLoadingMessage() {
|
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
|
||||||
const loadingDiv = document.createElement('div');
|
|
||||||
loadingDiv.className = 'ai-loading-simple';
|
|
||||||
loadingDiv.innerHTML = `
|
|
||||||
<i class="fas fa-robot"></i>
|
|
||||||
<span>考えています</span>
|
|
||||||
<i class="fas fa-spinner fa-spin"></i>
|
|
||||||
`;
|
|
||||||
chatHistory.appendChild(loadingDiv);
|
|
||||||
}
|
|
||||||
|
|
||||||
function showErrorMessage(message) {
|
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
|
||||||
removeLoadingMessage();
|
|
||||||
|
|
||||||
const errorDiv = document.createElement('div');
|
|
||||||
errorDiv.className = 'chat-message error-message comment-style';
|
|
||||||
errorDiv.innerHTML = `
|
|
||||||
<div class="message-header">
|
|
||||||
<div class="avatar">⚠️</div>
|
|
||||||
<div class="user-info">
|
|
||||||
<div class="display-name">System</div>
|
|
||||||
<div class="handle">@system</div>
|
|
||||||
<div class="timestamp">${new Date().toLocaleString()}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="message-content">${message}</div>
|
|
||||||
`;
|
|
||||||
chatHistory.appendChild(errorDiv);
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeLoadingMessage() {
|
|
||||||
const loadingMsg = document.querySelector('.ai-loading-simple');
|
|
||||||
if (loadingMsg) {
|
|
||||||
loadingMsg.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function showInitialGreeting() {
|
|
||||||
if (!aiProfileData) return;
|
|
||||||
|
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
|
||||||
const greetingDiv = document.createElement('div');
|
|
||||||
greetingDiv.className = 'chat-message ai-message comment-style initial-greeting';
|
|
||||||
|
|
||||||
const avatarElement = aiProfileData.avatar
|
|
||||||
? `<img src="${aiProfileData.avatar}" alt="${aiProfileData.displayName}" class="profile-avatar">`
|
|
||||||
: '🤖';
|
|
||||||
|
|
||||||
greetingDiv.innerHTML = `
|
|
||||||
<div class="message-header">
|
|
||||||
<div class="avatar">${avatarElement}</div>
|
|
||||||
<div class="user-info">
|
|
||||||
<div class="display-name">${aiProfileData.displayName}</div>
|
|
||||||
<div class="handle">@${aiProfileData.handle}</div>
|
|
||||||
<div class="timestamp">${new Date().toLocaleString()}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="message-content">
|
|
||||||
Hello! I'm an AI assistant trained on this blog's content. I can answer questions about the articles, provide insights, and help you understand the topics discussed here. What would you like to know?
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
chatHistory.appendChild(greetingDiv);
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateAskAIButton() {
|
|
||||||
const button = document.getElementById('askAiButton');
|
|
||||||
if (!button) return;
|
|
||||||
|
|
||||||
// Only update text, never modify the icon
|
|
||||||
if (aiProfileData && aiProfileData.displayName) {
|
|
||||||
const textNode = button.childNodes[2] || button.lastChild;
|
|
||||||
if (textNode && textNode.nodeType === Node.TEXT_NODE) {
|
|
||||||
textNode.textContent = aiProfileData.displayName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleAIResponse(responseData) {
|
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
|
||||||
removeLoadingMessage();
|
|
||||||
|
|
||||||
const aiProfile = responseData.aiProfile;
|
|
||||||
if (!aiProfile || !aiProfile.handle || !aiProfile.displayName) {
|
|
||||||
console.error('AI profile data is missing');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const timestamp = new Date(responseData.timestamp || Date.now());
|
|
||||||
const avatarElement = aiProfile.avatar
|
|
||||||
? `<img src="${aiProfile.avatar}" alt="${aiProfile.displayName}" class="profile-avatar">`
|
|
||||||
: '🤖';
|
|
||||||
|
|
||||||
const answerDiv = document.createElement('div');
|
|
||||||
answerDiv.className = 'chat-message ai-message comment-style';
|
|
||||||
answerDiv.innerHTML = `
|
|
||||||
<div class="message-header">
|
|
||||||
<div class="avatar">${avatarElement}</div>
|
|
||||||
<div class="user-info">
|
|
||||||
<div class="display-name">${aiProfile.displayName}</div>
|
|
||||||
<div class="handle">@${aiProfile.handle}</div>
|
|
||||||
<div class="timestamp">${timestamp.toLocaleString()}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="message-content">${responseData.answer}</div>
|
|
||||||
`;
|
|
||||||
chatHistory.appendChild(answerDiv);
|
|
||||||
|
|
||||||
// Limit chat history
|
|
||||||
limitChatHistory();
|
|
||||||
}
|
|
||||||
|
|
||||||
function limitChatHistory() {
|
|
||||||
const chatHistory = document.getElementById('chatHistory');
|
|
||||||
if (chatHistory.children.length > 10) {
|
|
||||||
chatHistory.removeChild(chatHistory.children[0]);
|
|
||||||
if (chatHistory.children.length > 0) {
|
|
||||||
chatHistory.removeChild(chatHistory.children[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Event listeners setup
|
|
||||||
function setupAskAIEventListeners() {
|
|
||||||
// Listen for AI profile updates from OAuth app
|
|
||||||
window.addEventListener('aiProfileLoaded', function(event) {
|
|
||||||
aiProfileData = event.detail;
|
|
||||||
console.log('AI profile loaded:', aiProfileData);
|
|
||||||
updateAskAIButton();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Listen for AI responses
|
|
||||||
window.addEventListener('aiResponseReceived', function(event) {
|
|
||||||
handleAIResponse(event.detail);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Track IME composition state
|
|
||||||
let isComposing = false;
|
|
||||||
const aiQuestionInput = document.getElementById('aiQuestion');
|
|
||||||
|
|
||||||
if (aiQuestionInput) {
|
|
||||||
aiQuestionInput.addEventListener('compositionstart', function() {
|
|
||||||
isComposing = true;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
aiQuestionInput.addEventListener('compositionend', function() {
|
observer.observe(document.body, {
|
||||||
isComposing = false;
|
childList: true,
|
||||||
|
subtree: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keyboard shortcuts
|
updateButton() {
|
||||||
document.addEventListener('keydown', function(e) {
|
const button = document.getElementById('askAiButton');
|
||||||
if (e.key === 'Escape') {
|
if (this.aiProfile && this.aiProfile.displayName) {
|
||||||
const panel = document.getElementById('askAiPanel');
|
const textNode = button.childNodes[2];
|
||||||
if (panel) {
|
if (textNode) {
|
||||||
panel.style.display = 'none';
|
textNode.textContent = this.aiProfile.displayName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
showGreeting() {
|
||||||
|
if (!this.aiProfile) return;
|
||||||
|
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
const greetingDiv = document.createElement('div');
|
||||||
|
greetingDiv.className = 'chat-message ai-message comment-style initial-greeting';
|
||||||
|
|
||||||
|
const avatarElement = this.aiProfile.avatar
|
||||||
|
? `<img src="${this.aiProfile.avatar}" alt="${this.aiProfile.displayName}" class="profile-avatar">`
|
||||||
|
: '🤖';
|
||||||
|
|
||||||
|
greetingDiv.innerHTML = `
|
||||||
|
<div class="message-header">
|
||||||
|
<div class="avatar">${avatarElement}</div>
|
||||||
|
<div class="user-info">
|
||||||
|
<div class="display-name">${this.aiProfile.displayName}</div>
|
||||||
|
<div class="handle">@${this.aiProfile.handle}</div>
|
||||||
|
<div class="timestamp">${new Date().toLocaleString()}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="message-content">
|
||||||
|
Hello! I'm an AI assistant trained on this blog's content. I can answer questions about the articles, provide insights, and help you understand the topics discussed here. What would you like to know?
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
chatHistory.appendChild(greetingDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
async ask() {
|
||||||
|
const question = document.getElementById('aiQuestion').value;
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
const askButton = document.getElementById('askButton');
|
||||||
|
|
||||||
|
if (!question.trim()) return;
|
||||||
|
|
||||||
|
// Wait for AI to be ready
|
||||||
|
if (!this.isReady) {
|
||||||
|
await this.waitForReady();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable button
|
||||||
|
askButton.disabled = true;
|
||||||
|
askButton.textContent = 'Posting...';
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Add user message
|
||||||
|
this.addUserMessage(question);
|
||||||
|
|
||||||
|
// Clear input
|
||||||
|
document.getElementById('aiQuestion').value = '';
|
||||||
|
|
||||||
|
// Show loading
|
||||||
|
this.showLoading();
|
||||||
|
|
||||||
|
// Post question
|
||||||
|
const event = new CustomEvent('postAIQuestion', {
|
||||||
|
detail: { question: question }
|
||||||
|
});
|
||||||
|
window.dispatchEvent(event);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
this.showError('Sorry, I encountered an error. Please try again.');
|
||||||
|
} finally {
|
||||||
|
askButton.disabled = false;
|
||||||
|
askButton.textContent = 'Ask';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
waitForReady() {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
const checkReady = setInterval(() => {
|
||||||
|
if (this.isReady) {
|
||||||
|
clearInterval(checkReady);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
addUserMessage(question) {
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
const userSection = document.querySelector('.user-section');
|
||||||
|
|
||||||
|
let userAvatar = '👤';
|
||||||
|
let userDisplay = 'You';
|
||||||
|
let userHandle = 'user';
|
||||||
|
|
||||||
|
if (userSection) {
|
||||||
|
const avatarImg = userSection.querySelector('.user-avatar');
|
||||||
|
const displayName = userSection.querySelector('.user-display-name');
|
||||||
|
const handle = userSection.querySelector('.user-handle');
|
||||||
|
|
||||||
|
if (avatarImg && avatarImg.src) {
|
||||||
|
userAvatar = `<img src="${avatarImg.src}" alt="${displayName?.textContent || 'User'}" class="profile-avatar">`;
|
||||||
|
}
|
||||||
|
if (displayName?.textContent) {
|
||||||
|
userDisplay = displayName.textContent;
|
||||||
|
}
|
||||||
|
if (handle?.textContent) {
|
||||||
|
userHandle = handle.textContent.replace('@', '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enter key to send message (only when not composing Japanese input)
|
const questionDiv = document.createElement('div');
|
||||||
if (e.key === 'Enter' && e.target.id === 'aiQuestion' && !e.shiftKey && !isComposing) {
|
questionDiv.className = 'chat-message user-message comment-style';
|
||||||
e.preventDefault();
|
questionDiv.innerHTML = `
|
||||||
askQuestion();
|
<div class="message-header">
|
||||||
|
<div class="avatar">${userAvatar}</div>
|
||||||
|
<div class="user-info">
|
||||||
|
<div class="display-name">${userDisplay}</div>
|
||||||
|
<div class="handle">@${userHandle}</div>
|
||||||
|
<div class="timestamp">${new Date().toLocaleString()}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="message-content">${question}</div>
|
||||||
|
`;
|
||||||
|
chatHistory.appendChild(questionDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
showLoading() {
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
const loadingDiv = document.createElement('div');
|
||||||
|
loadingDiv.className = 'ai-loading-simple';
|
||||||
|
loadingDiv.innerHTML = `
|
||||||
|
<i class="fas fa-robot"></i>
|
||||||
|
<span>考えています</span>
|
||||||
|
<i class="fas fa-spinner fa-spin"></i>
|
||||||
|
`;
|
||||||
|
chatHistory.appendChild(loadingDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
showError(message) {
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
this.removeLoading();
|
||||||
|
|
||||||
|
const errorDiv = document.createElement('div');
|
||||||
|
errorDiv.className = 'chat-message error-message comment-style';
|
||||||
|
errorDiv.innerHTML = `
|
||||||
|
<div class="message-header">
|
||||||
|
<div class="avatar">⚠️</div>
|
||||||
|
<div class="user-info">
|
||||||
|
<div class="display-name">System</div>
|
||||||
|
<div class="handle">@system</div>
|
||||||
|
<div class="timestamp">${new Date().toLocaleString()}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="message-content">${message}</div>
|
||||||
|
`;
|
||||||
|
chatHistory.appendChild(errorDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeLoading() {
|
||||||
|
const loadingMsg = document.querySelector('.ai-loading-simple');
|
||||||
|
if (loadingMsg) {
|
||||||
|
loadingMsg.remove();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
handleAIResponse(responseData) {
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
this.removeLoading();
|
||||||
|
|
||||||
|
const aiProfile = responseData.aiProfile;
|
||||||
|
if (!aiProfile || !aiProfile.handle || !aiProfile.displayName) {
|
||||||
|
console.error('AI profile data is missing');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const timestamp = new Date(responseData.timestamp || Date.now());
|
||||||
|
const avatarElement = aiProfile.avatar
|
||||||
|
? `<img src="${aiProfile.avatar}" alt="${aiProfile.displayName}" class="profile-avatar">`
|
||||||
|
: '🤖';
|
||||||
|
|
||||||
|
const answerDiv = document.createElement('div');
|
||||||
|
answerDiv.className = 'chat-message ai-message comment-style';
|
||||||
|
answerDiv.innerHTML = `
|
||||||
|
<div class="message-header">
|
||||||
|
<div class="avatar">${avatarElement}</div>
|
||||||
|
<div class="user-info">
|
||||||
|
<div class="display-name">${aiProfile.displayName}</div>
|
||||||
|
<div class="handle">@${aiProfile.handle}</div>
|
||||||
|
<div class="timestamp">${timestamp.toLocaleString()}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="message-content">${responseData.answer}</div>
|
||||||
|
`;
|
||||||
|
chatHistory.appendChild(answerDiv);
|
||||||
|
|
||||||
|
// Limit chat history
|
||||||
|
this.limitChatHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
limitChatHistory() {
|
||||||
|
const chatHistory = document.getElementById('chatHistory');
|
||||||
|
if (chatHistory.children.length > 10) {
|
||||||
|
chatHistory.removeChild(chatHistory.children[0]);
|
||||||
|
if (chatHistory.children.length > 0) {
|
||||||
|
chatHistory.removeChild(chatHistory.children[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Ask AI when DOM is loaded
|
// Initialize Ask AI when DOM is loaded
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
setupAskAIEventListeners();
|
try {
|
||||||
console.log('Ask AI initialized successfully');
|
window.askAIInstance = new AskAI();
|
||||||
|
console.log('Ask AI initialized successfully');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize Ask AI:', error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Global functions for onclick handlers
|
// Global function for onclick
|
||||||
window.toggleAskAI = toggleAskAI;
|
window.AskAI = {
|
||||||
window.askQuestion = askQuestion;
|
toggle: function() {
|
||||||
|
console.log('AskAI.toggle called');
|
||||||
|
if (window.askAIInstance) {
|
||||||
|
window.askAIInstance.toggle();
|
||||||
|
} else {
|
||||||
|
console.error('Ask AI instance not available');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ask: function() {
|
||||||
|
console.log('AskAI.ask called');
|
||||||
|
if (window.askAIInstance) {
|
||||||
|
window.askAIInstance.ask();
|
||||||
|
} else {
|
||||||
|
console.error('Ask AI instance not available');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
@@ -15,6 +15,7 @@
|
|||||||
<link rel="stylesheet" href="/pkg/icomoon/style.css">
|
<link rel="stylesheet" href="/pkg/icomoon/style.css">
|
||||||
<link rel="stylesheet" href="/pkg/font-awesome/css/all.min.css">
|
<link rel="stylesheet" href="/pkg/font-awesome/css/all.min.css">
|
||||||
|
|
||||||
|
{% include "oauth-assets.html" %}
|
||||||
{% block head %}{% endblock %}
|
{% block head %}{% endblock %}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -49,7 +50,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="header-actions">
|
<div class="header-actions">
|
||||||
<button class="ask-ai-btn" onclick="toggleAskAI()" id="askAiButton">
|
<button class="ask-ai-btn" onclick="AskAI.toggle()" id="askAiButton">
|
||||||
<span class="ai-icon icon-ai"></span>
|
<span class="ai-icon icon-ai"></span>
|
||||||
ai
|
ai
|
||||||
</button>
|
</button>
|
||||||
@@ -66,7 +67,7 @@
|
|||||||
|
|
||||||
<div id="chatForm" class="ask-ai-form" style="display: none;">
|
<div id="chatForm" class="ask-ai-form" style="display: none;">
|
||||||
<input type="text" id="aiQuestion" placeholder="What would you like to know?" />
|
<input type="text" id="aiQuestion" placeholder="What would you like to know?" />
|
||||||
<button onclick="askQuestion()" id="askButton">Ask</button>
|
<button onclick="AskAI.ask()" id="askButton">Ask</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="chatHistory" class="chat-history" style="display: none;"></div>
|
<div id="chatHistory" class="chat-history" style="display: none;"></div>
|
||||||
@@ -82,7 +83,7 @@
|
|||||||
|
|
||||||
<footer class="main-footer">
|
<footer class="main-footer">
|
||||||
<div class="footer-social">
|
<div class="footer-social">
|
||||||
<a href="https://syu.is/syui" target="_blank"><i class="fab fa-bluesky"></i></a>
|
<a href="https://web.syu.is/@syui" target="_blank"><i class="fab fa-bluesky"></i></a>
|
||||||
<a href="https://git.syui.ai/ai" target="_blank"><span class="icon-ai"></span></a>
|
<a href="https://git.syui.ai/ai" target="_blank"><span class="icon-ai"></span></a>
|
||||||
<a href="https://git.syui.ai/syui" target="_blank"><span class="icon-git"></span></a>
|
<a href="https://git.syui.ai/syui" target="_blank"><span class="icon-git"></span></a>
|
||||||
</div>
|
</div>
|
||||||
@@ -91,7 +92,5 @@
|
|||||||
|
|
||||||
<script src="/js/ask-ai.js"></script>
|
<script src="/js/ask-ai.js"></script>
|
||||||
<script src="/js/theme.js"></script>
|
<script src="/js/theme.js"></script>
|
||||||
|
|
||||||
{% include "oauth-assets.html" %}
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@@ -20,6 +20,19 @@
|
|||||||
<a href="{{ post.url }}">{{ post.title }}</a>
|
<a href="{{ post.url }}">{{ post.title }}</a>
|
||||||
</h3>
|
</h3>
|
||||||
|
|
||||||
|
{% if post.excerpt %}
|
||||||
|
<p class="post-excerpt">{{ post.excerpt }}</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<div class="post-actions">
|
||||||
|
<a href="{{ post.url }}" class="read-more">Read more</a>
|
||||||
|
{% if post.markdown_url %}
|
||||||
|
<a href="{{ post.markdown_url }}" class="view-markdown" title="View Markdown">.md</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if post.translation_url %}
|
||||||
|
<a href="{{ post.translation_url }}" class="view-translation" title="View Translation">🌐</a>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
3
my-blog/templates/oauth-assets.html
Normal file
3
my-blog/templates/oauth-assets.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<!-- OAuth Comment System - Load globally for session management -->
|
||||||
|
<script type="module" crossorigin src="/assets/comment-atproto-Do1JWJCw.js"></script>
|
||||||
|
<link rel="stylesheet" crossorigin href="/assets/comment-atproto-CPKYAM8U.css">
|
@@ -2,20 +2,26 @@
|
|||||||
VITE_APP_HOST=https://syui.ai
|
VITE_APP_HOST=https://syui.ai
|
||||||
VITE_OAUTH_CLIENT_ID=https://syui.ai/client-metadata.json
|
VITE_OAUTH_CLIENT_ID=https://syui.ai/client-metadata.json
|
||||||
VITE_OAUTH_REDIRECT_URI=https://syui.ai/oauth/callback
|
VITE_OAUTH_REDIRECT_URI=https://syui.ai/oauth/callback
|
||||||
|
VITE_ADMIN_DID=did:plc:uqzpqmrjnptsxezjx4xuh2mn
|
||||||
|
|
||||||
# Handle-based Configuration (DIDs resolved at runtime)
|
# Collection names for OAuth app
|
||||||
VITE_ATPROTO_PDS=syu.is
|
VITE_COLLECTION_COMMENT=ai.syui.log
|
||||||
VITE_ADMIN_HANDLE=ai.syui.ai
|
VITE_COLLECTION_USER=ai.syui.log.user
|
||||||
VITE_AI_HANDLE=ai.syui.ai
|
VITE_COLLECTION_CHAT=ai.syui.log.chat
|
||||||
VITE_OAUTH_COLLECTION=ai.syui.log
|
|
||||||
VITE_ATPROTO_WEB_URL=https://bsky.app
|
# Collection names for ailog (backward compatibility)
|
||||||
VITE_ATPROTO_HANDLE_LIST=["syui.syui.ai","yui.syui.ai","syui.syu.is","ai.syu.is"]
|
AILOG_COLLECTION_COMMENT=ai.syui.log
|
||||||
|
AILOG_COLLECTION_USER=ai.syui.log.user
|
||||||
|
AILOG_COLLECTION_CHAT=ai.syui.log.chat
|
||||||
|
|
||||||
# AI Configuration
|
# AI Configuration
|
||||||
VITE_AI_ENABLED=true
|
VITE_AI_ENABLED=true
|
||||||
VITE_AI_ASK_AI=true
|
VITE_AI_ASK_AI=true
|
||||||
VITE_AI_PROVIDER=ollama
|
VITE_AI_PROVIDER=ollama
|
||||||
VITE_AI_MODEL=gemma3:4b
|
VITE_AI_MODEL=gemma3:2b
|
||||||
VITE_AI_HOST=https://ollama.syui.ai
|
VITE_AI_HOST=https://ollama.syui.ai
|
||||||
VITE_AI_SYSTEM_PROMPT="あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。"
|
VITE_AI_SYSTEM_PROMPT="you are a helpful ai assistant trained on this blog's content. you can answer questions about the articles, provide insights, and help users understand the topics discussed."
|
||||||
|
VITE_AI_DID=did:plc:4hqjfn7m6n5hno3doamuhgef
|
||||||
|
|
||||||
|
# API Configuration
|
||||||
|
VITE_BSKY_PUBLIC_API=https://public.api.bsky.app
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "aicard",
|
"name": "aicard",
|
||||||
"version": "0.1.1",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite --mode development",
|
"dev": "vite --mode development",
|
||||||
|
@@ -168,68 +168,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 1000px) {
|
@media (max-width: 1000px) {
|
||||||
* {
|
|
||||||
max-width: 100% !important;
|
|
||||||
box-sizing: border-box !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.app .app-main {
|
.app .app-main {
|
||||||
max-width: 100% !important;
|
|
||||||
margin: 0 !important;
|
|
||||||
padding: 0px !important;
|
padding: 0px !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.comment-item {
|
|
||||||
padding: 0px !important;
|
|
||||||
margin: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section {
|
|
||||||
padding: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comments-list {
|
|
||||||
padding: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-section {
|
|
||||||
padding: 30px 0 !important;
|
|
||||||
margin: 0px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-content {
|
|
||||||
padding: 10px !important;
|
|
||||||
word-wrap: break-word !important;
|
|
||||||
overflow-wrap: break-word !important;
|
|
||||||
white-space: pre-wrap !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.comment-header {
|
|
||||||
padding: 10px !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fix overflow on article pages */
|
|
||||||
article.article-content {
|
|
||||||
overflow-x: hidden !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Ensure full width on mobile */
|
|
||||||
.app {
|
|
||||||
max-width: 100vw !important;
|
|
||||||
overflow-x: hidden !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fix button overflow */
|
|
||||||
button {
|
|
||||||
max-width: 100%;
|
|
||||||
white-space: normal;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fix comment-meta URI overflow */
|
|
||||||
.comment-meta {
|
|
||||||
word-break: break-all !important;
|
|
||||||
overflow-wrap: break-word !important;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.gacha-section {
|
.gacha-section {
|
||||||
@@ -332,15 +273,6 @@
|
|||||||
/* padding: 20px; - removed to avoid double padding */
|
/* padding: 20px; - removed to avoid double padding */
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 768px) {
|
|
||||||
.comment-section {
|
|
||||||
max-width: 100%;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
.auth-section {
|
.auth-section {
|
||||||
background: #f8f9fa;
|
background: #f8f9fa;
|
||||||
border: 1px solid #e9ecef;
|
border: 1px solid #e9ecef;
|
||||||
@@ -350,38 +282,6 @@
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.auth-section.search-bar-layout {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
padding: 10px;
|
|
||||||
gap: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section.search-bar-layout .handle-input {
|
|
||||||
flex: 1;
|
|
||||||
margin: 0;
|
|
||||||
padding: 10px 15px;
|
|
||||||
font-size: 16px;
|
|
||||||
border: 1px solid #dee2e6;
|
|
||||||
border-radius: 6px 0 0 6px;
|
|
||||||
background: white;
|
|
||||||
outline: none;
|
|
||||||
transition: border-color 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section.search-bar-layout .handle-input:focus {
|
|
||||||
border-color: var(--theme-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section.search-bar-layout .atproto-button {
|
|
||||||
margin: 0;
|
|
||||||
padding: 10px 20px;
|
|
||||||
border-radius: 0 6px 6px 0;
|
|
||||||
min-width: 50px;
|
|
||||||
font-weight: bold;
|
|
||||||
height: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.atproto-button {
|
.atproto-button {
|
||||||
background: var(--theme-color);
|
background: var(--theme-color);
|
||||||
color: var(--white);
|
color: var(--white);
|
||||||
@@ -415,30 +315,6 @@
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Override for search bar layout */
|
|
||||||
.search-bar-layout .handle-input {
|
|
||||||
width: auto;
|
|
||||||
text-align: left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Mobile responsive for search bar */
|
|
||||||
@media (max-width: 480px) {
|
|
||||||
.auth-section.search-bar-layout {
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section.search-bar-layout .handle-input {
|
|
||||||
width: 100%;
|
|
||||||
border-radius: 6px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-section.search-bar-layout .atproto-button {
|
|
||||||
width: 100%;
|
|
||||||
border-radius: 6px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.auth-hint {
|
.auth-hint {
|
||||||
color: #6c757d;
|
color: #6c757d;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
@@ -571,8 +447,9 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.comments-list {
|
.comments-list {
|
||||||
|
border: 1px solid #ddd;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
padding: 0px;
|
padding: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.comments-header {
|
.comments-header {
|
||||||
@@ -683,8 +560,6 @@
|
|||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
color: #333;
|
color: #333;
|
||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
white-space: pre-wrap;
|
|
||||||
word-wrap: break-word;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.comment-meta {
|
.comment-meta {
|
||||||
@@ -931,6 +806,28 @@
|
|||||||
background: #f6f8fa;
|
background: #f6f8fa;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* AI Chat History */
|
||||||
|
.ai-chat-list {
|
||||||
|
max-width: 100%;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chat-item {
|
||||||
|
border: 1px solid #d1d9e0;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 16px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
background: #ffffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chat-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
.chat-actions {
|
.chat-actions {
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -981,8 +878,4 @@
|
|||||||
padding: 40px 20px;
|
padding: 40px 20px;
|
||||||
color: #656d76;
|
color: #656d76;
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
}
|
||||||
|
|
||||||
.chat-message.comment-style {
|
|
||||||
border-left: 4px solid var(--theme-color);
|
|
||||||
}
|
|
1033
oauth/src/App.tsx
1033
oauth/src/App.tsx
File diff suppressed because it is too large
Load Diff
@@ -14,7 +14,7 @@ const response = await fetch(`${aiConfig.host}/api/generate`, {
|
|||||||
options: {
|
options: {
|
||||||
temperature: 0.9,
|
temperature: 0.9,
|
||||||
top_p: 0.9,
|
top_p: 0.9,
|
||||||
num_predict: 200,
|
num_predict: 80,
|
||||||
repeat_penalty: 1.1,
|
repeat_penalty: 1.1,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { User } from '../services/auth';
|
import { User } from '../services/auth';
|
||||||
import { atprotoOAuthService } from '../services/atproto-oauth';
|
import { atprotoOAuthService } from '../services/atproto-oauth';
|
||||||
import { appConfig, getCollectionNames } from '../config/app';
|
import { appConfig } from '../config/app';
|
||||||
|
|
||||||
interface AIChatProps {
|
interface AIChatProps {
|
||||||
user: User | null;
|
user: User | null;
|
||||||
@@ -14,22 +14,26 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
const [isProcessing, setIsProcessing] = useState(false);
|
const [isProcessing, setIsProcessing] = useState(false);
|
||||||
const [aiProfile, setAiProfile] = useState<any>(null);
|
const [aiProfile, setAiProfile] = useState<any>(null);
|
||||||
|
|
||||||
// Get AI settings from appConfig (unified configuration)
|
// Get AI settings from environment variables
|
||||||
const aiConfig = {
|
const aiConfig = {
|
||||||
enabled: appConfig.aiEnabled,
|
enabled: import.meta.env.VITE_AI_ENABLED === 'true',
|
||||||
askAi: appConfig.aiAskAi,
|
askAi: import.meta.env.VITE_AI_ASK_AI === 'true',
|
||||||
provider: appConfig.aiProvider,
|
provider: import.meta.env.VITE_AI_PROVIDER || 'ollama',
|
||||||
model: appConfig.aiModel,
|
model: import.meta.env.VITE_AI_MODEL || 'gemma3:4b',
|
||||||
host: appConfig.aiHost,
|
host: import.meta.env.VITE_AI_HOST || 'https://ollama.syui.ai',
|
||||||
systemPrompt: appConfig.aiSystemPrompt,
|
systemPrompt: import.meta.env.VITE_AI_SYSTEM_PROMPT || 'You are a helpful AI assistant trained on this blog\'s content.',
|
||||||
aiDid: appConfig.aiDid,
|
aiDid: import.meta.env.VITE_AI_DID || 'did:plc:uqzpqmrjnptsxezjx4xuh2mn',
|
||||||
bskyPublicApi: appConfig.bskyPublicApi,
|
bskyPublicApi: import.meta.env.VITE_BSKY_PUBLIC_API || 'https://public.api.bsky.app',
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch AI profile on load
|
// Fetch AI profile on load
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchAIProfile = async () => {
|
const fetchAIProfile = async () => {
|
||||||
|
console.log('=== AI PROFILE FETCH START ===');
|
||||||
|
console.log('AI DID:', aiConfig.aiDid);
|
||||||
|
|
||||||
if (!aiConfig.aiDid) {
|
if (!aiConfig.aiDid) {
|
||||||
|
console.log('No AI DID configured');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +41,9 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
// Try with agent first
|
// Try with agent first
|
||||||
const agent = atprotoOAuthService.getAgent();
|
const agent = atprotoOAuthService.getAgent();
|
||||||
if (agent) {
|
if (agent) {
|
||||||
|
console.log('Fetching AI profile with agent for DID:', aiConfig.aiDid);
|
||||||
const profile = await agent.getProfile({ actor: aiConfig.aiDid });
|
const profile = await agent.getProfile({ actor: aiConfig.aiDid });
|
||||||
|
console.log('AI profile fetched successfully:', profile.data);
|
||||||
const profileData = {
|
const profileData = {
|
||||||
did: aiConfig.aiDid,
|
did: aiConfig.aiDid,
|
||||||
handle: profile.data.handle,
|
handle: profile.data.handle,
|
||||||
@@ -45,17 +51,21 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
avatar: profile.data.avatar,
|
avatar: profile.data.avatar,
|
||||||
description: profile.data.description
|
description: profile.data.description
|
||||||
};
|
};
|
||||||
|
console.log('Setting aiProfile to:', profileData);
|
||||||
setAiProfile(profileData);
|
setAiProfile(profileData);
|
||||||
|
|
||||||
// Dispatch event to update Ask AI button
|
// Dispatch event to update Ask AI button
|
||||||
window.dispatchEvent(new CustomEvent('aiProfileLoaded', { detail: profileData }));
|
window.dispatchEvent(new CustomEvent('aiProfileLoaded', { detail: profileData }));
|
||||||
|
console.log('=== AI PROFILE FETCH SUCCESS (AGENT) ===');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to public API
|
// Fallback to public API
|
||||||
|
console.log('No agent available, trying public API for AI profile');
|
||||||
const response = await fetch(`${aiConfig.bskyPublicApi}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(aiConfig.aiDid)}`);
|
const response = await fetch(`${aiConfig.bskyPublicApi}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(aiConfig.aiDid)}`);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const profileData = await response.json();
|
const profileData = await response.json();
|
||||||
|
console.log('AI profile fetched via public API:', profileData);
|
||||||
const profile = {
|
const profile = {
|
||||||
did: aiConfig.aiDid,
|
did: aiConfig.aiDid,
|
||||||
handle: profileData.handle,
|
handle: profileData.handle,
|
||||||
@@ -63,15 +73,21 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
avatar: profileData.avatar,
|
avatar: profileData.avatar,
|
||||||
description: profileData.description
|
description: profileData.description
|
||||||
};
|
};
|
||||||
|
console.log('Setting aiProfile to:', profile);
|
||||||
setAiProfile(profile);
|
setAiProfile(profile);
|
||||||
|
|
||||||
// Dispatch event to update Ask AI button
|
// Dispatch event to update Ask AI button
|
||||||
window.dispatchEvent(new CustomEvent('aiProfileLoaded', { detail: profile }));
|
window.dispatchEvent(new CustomEvent('aiProfileLoaded', { detail: profile }));
|
||||||
|
console.log('=== AI PROFILE FETCH SUCCESS (PUBLIC API) ===');
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
console.error('Public API failed with status:', response.status);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch AI profile:', error);
|
||||||
setAiProfile(null);
|
setAiProfile(null);
|
||||||
}
|
}
|
||||||
|
console.log('=== AI PROFILE FETCH FAILED ===');
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchAIProfile();
|
fetchAIProfile();
|
||||||
@@ -84,6 +100,9 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
const handleAIQuestion = async (event: any) => {
|
const handleAIQuestion = async (event: any) => {
|
||||||
if (!user || !event.detail || !event.detail.question || isProcessing || !aiProfile) return;
|
if (!user || !event.detail || !event.detail.question || isProcessing || !aiProfile) return;
|
||||||
|
|
||||||
|
console.log('AIChat received question:', event.detail.question);
|
||||||
|
console.log('Current aiProfile state:', aiProfile);
|
||||||
|
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
try {
|
try {
|
||||||
await postQuestionAndGenerateResponse(event.detail.question);
|
await postQuestionAndGenerateResponse(event.detail.question);
|
||||||
@@ -95,6 +114,7 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
// Add listener with a small delay to ensure it's ready
|
// Add listener with a small delay to ensure it's ready
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.addEventListener('postAIQuestion', handleAIQuestion);
|
window.addEventListener('postAIQuestion', handleAIQuestion);
|
||||||
|
console.log('AIChat event listener registered');
|
||||||
|
|
||||||
// Notify that AI is ready
|
// Notify that AI is ready
|
||||||
window.dispatchEvent(new CustomEvent('aiChatReady'));
|
window.dispatchEvent(new CustomEvent('aiChatReady'));
|
||||||
@@ -114,50 +134,40 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
const agent = atprotoOAuthService.getAgent();
|
const agent = atprotoOAuthService.getAgent();
|
||||||
if (!agent) throw new Error('No agent available');
|
if (!agent) throw new Error('No agent available');
|
||||||
|
|
||||||
// Get collection names
|
|
||||||
const collections = getCollectionNames(appConfig.collections.base);
|
|
||||||
|
|
||||||
// 1. Post question to ATProto
|
// 1. Post question to ATProto
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const rkey = now.toISOString().replace(/[:.]/g, '-');
|
const rkey = now.toISOString().replace(/[:.]/g, '-');
|
||||||
|
|
||||||
// Extract post metadata from current page
|
|
||||||
const currentUrl = window.location.href;
|
|
||||||
const postSlug = currentUrl.match(/\/posts\/([^/]+)/)?.[1] || '';
|
|
||||||
const postTitle = document.title.replace(' - syui.ai', '') || '';
|
|
||||||
|
|
||||||
const questionRecord = {
|
const questionRecord = {
|
||||||
$type: collections.chat,
|
$type: appConfig.collections.chat,
|
||||||
post: {
|
question: question,
|
||||||
url: currentUrl,
|
url: window.location.href,
|
||||||
slug: postSlug,
|
createdAt: now.toISOString(),
|
||||||
title: postTitle,
|
|
||||||
date: new Date().toISOString(),
|
|
||||||
tags: [],
|
|
||||||
language: "ja"
|
|
||||||
},
|
|
||||||
type: "question",
|
|
||||||
text: question,
|
|
||||||
author: {
|
author: {
|
||||||
did: user.did,
|
did: user.did,
|
||||||
handle: user.handle,
|
handle: user.handle,
|
||||||
avatar: user.avatar,
|
avatar: user.avatar,
|
||||||
displayName: user.displayName || user.handle,
|
displayName: user.displayName || user.handle,
|
||||||
},
|
},
|
||||||
createdAt: now.toISOString(),
|
context: {
|
||||||
|
page_title: document.title,
|
||||||
|
page_url: window.location.href,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await agent.api.com.atproto.repo.putRecord({
|
await agent.api.com.atproto.repo.putRecord({
|
||||||
repo: user.did,
|
repo: user.did,
|
||||||
collection: collections.chat,
|
collection: appConfig.collections.chat,
|
||||||
rkey: rkey,
|
rkey: rkey,
|
||||||
record: questionRecord,
|
record: questionRecord,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('Question posted to ATProto');
|
||||||
|
|
||||||
// 2. Get chat history
|
// 2. Get chat history
|
||||||
const chatRecords = await agent.api.com.atproto.repo.listRecords({
|
const chatRecords = await agent.api.com.atproto.repo.listRecords({
|
||||||
repo: user.did,
|
repo: user.did,
|
||||||
collection: collections.chat,
|
collection: appConfig.collections.chat,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -165,10 +175,10 @@ export const AIChat: React.FC<AIChatProps> = ({ user, isEnabled }) => {
|
|||||||
if (chatRecords.data.records) {
|
if (chatRecords.data.records) {
|
||||||
chatHistoryText = chatRecords.data.records
|
chatHistoryText = chatRecords.data.records
|
||||||
.map((r: any) => {
|
.map((r: any) => {
|
||||||
if (r.value.type === 'question') {
|
if (r.value.question) {
|
||||||
return `User: ${r.value.text}`;
|
return `User: ${r.value.question}`;
|
||||||
} else if (r.value.type === 'answer') {
|
} else if (r.value.answer) {
|
||||||
return `AI: ${r.value.text}`;
|
return `AI: ${r.value.answer}`;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
})
|
})
|
||||||
@@ -199,7 +209,7 @@ Answer:`;
|
|||||||
options: {
|
options: {
|
||||||
temperature: 0.9,
|
temperature: 0.9,
|
||||||
top_p: 0.9,
|
top_p: 0.9,
|
||||||
num_predict: 200, // Longer responses for better answers
|
num_predict: 80, // Shorter responses for faster generation
|
||||||
repeat_penalty: 1.1,
|
repeat_penalty: 1.1,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -225,38 +235,37 @@ Answer:`;
|
|||||||
// 5. Save AI response in background
|
// 5. Save AI response in background
|
||||||
const answerRkey = now.toISOString().replace(/[:.]/g, '-') + '-answer';
|
const answerRkey = now.toISOString().replace(/[:.]/g, '-') + '-answer';
|
||||||
|
|
||||||
|
console.log('=== SAVING AI ANSWER ===');
|
||||||
|
console.log('Current aiProfile:', aiProfile);
|
||||||
|
|
||||||
const answerRecord = {
|
const answerRecord = {
|
||||||
$type: collections.chat,
|
$type: appConfig.collections.chat,
|
||||||
post: {
|
answer: aiAnswer,
|
||||||
url: currentUrl,
|
question_rkey: rkey,
|
||||||
slug: postSlug,
|
url: window.location.href,
|
||||||
title: postTitle,
|
createdAt: now.toISOString(),
|
||||||
date: new Date().toISOString(),
|
|
||||||
tags: [],
|
|
||||||
language: "ja"
|
|
||||||
},
|
|
||||||
type: "answer",
|
|
||||||
text: aiAnswer,
|
|
||||||
author: {
|
author: {
|
||||||
did: aiProfile.did,
|
did: aiProfile.did,
|
||||||
handle: aiProfile.handle,
|
handle: aiProfile.handle,
|
||||||
displayName: aiProfile.displayName,
|
displayName: aiProfile.displayName,
|
||||||
avatar: aiProfile.avatar,
|
avatar: aiProfile.avatar,
|
||||||
},
|
},
|
||||||
createdAt: now.toISOString(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log('Answer record to save:', answerRecord);
|
||||||
|
|
||||||
// Save to ATProto asynchronously (don't wait for it)
|
// Save to ATProto asynchronously (don't wait for it)
|
||||||
agent.api.com.atproto.repo.putRecord({
|
agent.api.com.atproto.repo.putRecord({
|
||||||
repo: user.did,
|
repo: user.did,
|
||||||
collection: collections.chat,
|
collection: appConfig.collections.chat,
|
||||||
rkey: answerRkey,
|
rkey: answerRkey,
|
||||||
record: answerRecord,
|
record: answerRecord,
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
// Silent fail for AI response saving
|
console.error('Failed to save AI response to ATProto:', err);
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to generate AI response:', error);
|
||||||
window.dispatchEvent(new CustomEvent('aiResponseError', {
|
window.dispatchEvent(new CustomEvent('aiResponseError', {
|
||||||
detail: { error: 'AI応答の生成に失敗しました' }
|
detail: { error: 'AI応答の生成に失敗しました' }
|
||||||
}));
|
}));
|
||||||
|
@@ -32,7 +32,7 @@ export const AIProfile: React.FC<AIProfileProps> = ({ aiDid }) => {
|
|||||||
description: response.data.description,
|
description: response.data.description,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Failed to fetch AI profile
|
console.error('Failed to fetch AI profile:', error);
|
||||||
// Fallback to basic info
|
// Fallback to basic info
|
||||||
setProfile({
|
setProfile({
|
||||||
did: aiDid,
|
did: aiDid,
|
||||||
|
@@ -26,7 +26,7 @@ export const CardBox: React.FC<CardBoxProps> = ({ userDid }) => {
|
|||||||
const data = await atprotoOAuthService.getCardsFromBox();
|
const data = await atprotoOAuthService.getCardsFromBox();
|
||||||
setBoxData(data);
|
setBoxData(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Failed to load card box
|
console.error('カードボックス読み込みエラー:', err);
|
||||||
setError(err instanceof Error ? err.message : 'カードボックスの読み込みに失敗しました');
|
setError(err instanceof Error ? err.message : 'カードボックスの読み込みに失敗しました');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
@@ -52,7 +52,7 @@ export const CardBox: React.FC<CardBoxProps> = ({ userDid }) => {
|
|||||||
setBoxData({ records: [] });
|
setBoxData({ records: [] });
|
||||||
alert('カードボックスを削除しました');
|
alert('カードボックスを削除しました');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Failed to delete card box
|
console.error('カードボックス削除エラー:', err);
|
||||||
setError(err instanceof Error ? err.message : 'カードボックスの削除に失敗しました');
|
setError(err instanceof Error ? err.message : 'カードボックスの削除に失敗しました');
|
||||||
} finally {
|
} finally {
|
||||||
setIsDeleting(false);
|
setIsDeleting(false);
|
||||||
|
@@ -32,7 +32,7 @@ export const CardList: React.FC = () => {
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setMasterData(data);
|
setMasterData(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Failed to load card master data
|
console.error('Error loading card master data:', err);
|
||||||
setError(err instanceof Error ? err.message : 'Failed to load card data');
|
setError(err instanceof Error ? err.message : 'Failed to load card data');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@@ -29,7 +29,7 @@ export const CollectionAnalysis: React.FC<CollectionAnalysisProps> = ({ userDid
|
|||||||
const result = await aiCardApi.analyzeCollection(userDid);
|
const result = await aiCardApi.analyzeCollection(userDid);
|
||||||
setAnalysis(result);
|
setAnalysis(result);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Collection analysis failed
|
console.error('Collection analysis failed:', err);
|
||||||
setError('AI分析機能を利用するにはai.gptサーバーが必要です。基本機能はai.cardサーバーのみで利用できます。');
|
setError('AI分析機能を利用するにはai.gptサーバーが必要です。基本機能はai.cardサーバーのみで利用できます。');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@@ -48,7 +48,7 @@ export const GachaAnimation: React.FC<GachaAnimationProps> = ({
|
|||||||
await atprotoOAuthService.saveCardToCollection(card);
|
await atprotoOAuthService.saveCardToCollection(card);
|
||||||
alert('カードデータをatprotoコレクションに保存しました!');
|
alert('カードデータをatprotoコレクションに保存しました!');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Failed to save card
|
console.error('保存エラー:', error);
|
||||||
alert('保存に失敗しました。認証が必要かもしれません。');
|
alert('保存に失敗しました。認証が必要かもしれません。');
|
||||||
} finally {
|
} finally {
|
||||||
setIsSharing(false);
|
setIsSharing(false);
|
||||||
|
@@ -30,7 +30,7 @@ export const GachaStats: React.FC = () => {
|
|||||||
try {
|
try {
|
||||||
result = await aiCardApi.getEnhancedStats();
|
result = await aiCardApi.getEnhancedStats();
|
||||||
} catch (aiError) {
|
} catch (aiError) {
|
||||||
// AI stats unavailable, using basic stats
|
console.warn('AI統計が利用できません、基本統計に切り替えます:', aiError);
|
||||||
setUseAI(false);
|
setUseAI(false);
|
||||||
result = await cardApi.getGachaStats();
|
result = await cardApi.getGachaStats();
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ export const GachaStats: React.FC = () => {
|
|||||||
}
|
}
|
||||||
setStats(result);
|
setStats(result);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Gacha stats failed
|
console.error('Gacha stats failed:', err);
|
||||||
setError('統計データの取得に失敗しました。ai.cardサーバーが起動していることを確認してください。');
|
setError('統計データの取得に失敗しました。ai.cardサーバーが起動していることを確認してください。');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@@ -160,7 +160,7 @@ export const Login: React.FC<LoginProps> = ({ onLogin, onClose, defaultHandle })
|
|||||||
/>
|
/>
|
||||||
<small>
|
<small>
|
||||||
メインパスワードではなく、
|
メインパスワードではなく、
|
||||||
<a href={`${import.meta.env.VITE_ATPROTO_WEB_URL || 'https://bsky.app'}/settings/app-passwords`} target="_blank" rel="noopener noreferrer">
|
<a href="https://bsky.app/settings/app-passwords" target="_blank" rel="noopener noreferrer">
|
||||||
アプリパスワード
|
アプリパスワード
|
||||||
</a>
|
</a>
|
||||||
を使用してください
|
を使用してください
|
||||||
|
@@ -7,6 +7,8 @@ interface OAuthCallbackProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError }) => {
|
export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError }) => {
|
||||||
|
console.log('=== OAUTH CALLBACK COMPONENT MOUNTED ===');
|
||||||
|
console.log('Current URL:', window.location.href);
|
||||||
|
|
||||||
const [isProcessing, setIsProcessing] = useState(true);
|
const [isProcessing, setIsProcessing] = useState(true);
|
||||||
const [needsHandle, setNeedsHandle] = useState(false);
|
const [needsHandle, setNeedsHandle] = useState(false);
|
||||||
@@ -16,10 +18,12 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Add timeout to prevent infinite loading
|
// Add timeout to prevent infinite loading
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
|
console.error('OAuth callback timeout');
|
||||||
onError('OAuth認証がタイムアウトしました');
|
onError('OAuth認証がタイムアウトしました');
|
||||||
}, 10000); // 10 second timeout
|
}, 10000); // 10 second timeout
|
||||||
|
|
||||||
const handleCallback = async () => {
|
const handleCallback = async () => {
|
||||||
|
console.log('=== HANDLE CALLBACK STARTED ===');
|
||||||
try {
|
try {
|
||||||
// Handle both query params (?) and hash params (#)
|
// Handle both query params (?) and hash params (#)
|
||||||
const hashParams = new URLSearchParams(window.location.hash.substring(1));
|
const hashParams = new URLSearchParams(window.location.hash.substring(1));
|
||||||
@@ -31,6 +35,14 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
const error = hashParams.get('error') || queryParams.get('error');
|
const error = hashParams.get('error') || queryParams.get('error');
|
||||||
const iss = hashParams.get('iss') || queryParams.get('iss');
|
const iss = hashParams.get('iss') || queryParams.get('iss');
|
||||||
|
|
||||||
|
console.log('OAuth callback parameters:', {
|
||||||
|
code: code ? code.substring(0, 20) + '...' : null,
|
||||||
|
state: state,
|
||||||
|
error: error,
|
||||||
|
iss: iss,
|
||||||
|
hash: window.location.hash,
|
||||||
|
search: window.location.search
|
||||||
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
throw new Error(`OAuth error: ${error}`);
|
throw new Error(`OAuth error: ${error}`);
|
||||||
@@ -40,10 +52,12 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
throw new Error('Missing OAuth parameters');
|
throw new Error('Missing OAuth parameters');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Processing OAuth callback with params:', { code: code?.substring(0, 10) + '...', state, iss });
|
||||||
|
|
||||||
// Use the official BrowserOAuthClient to handle the callback
|
// Use the official BrowserOAuthClient to handle the callback
|
||||||
const result = await atprotoOAuthService.handleOAuthCallback();
|
const result = await atprotoOAuthService.handleOAuthCallback();
|
||||||
if (result) {
|
if (result) {
|
||||||
|
console.log('OAuth callback completed successfully:', result);
|
||||||
|
|
||||||
// Success - notify parent component
|
// Success - notify parent component
|
||||||
onSuccess(result.did, result.handle);
|
onSuccess(result.did, result.handle);
|
||||||
@@ -52,7 +66,11 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('OAuth callback error:', error);
|
||||||
|
|
||||||
// Even if OAuth fails, try to continue with a fallback approach
|
// Even if OAuth fails, try to continue with a fallback approach
|
||||||
|
console.warn('OAuth callback failed, attempting fallback...');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create a minimal session to allow the user to proceed
|
// Create a minimal session to allow the user to proceed
|
||||||
const fallbackSession = {
|
const fallbackSession = {
|
||||||
@@ -64,6 +82,7 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
onSuccess(fallbackSession.did, fallbackSession.handle);
|
onSuccess(fallbackSession.did, fallbackSession.handle);
|
||||||
|
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
|
console.error('Fallback also failed:', fallbackError);
|
||||||
onError(error instanceof Error ? error.message : 'OAuth認証に失敗しました');
|
onError(error instanceof Error ? error.message : 'OAuth認証に失敗しました');
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@@ -85,13 +104,17 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
|
|
||||||
const trimmedHandle = handle.trim();
|
const trimmedHandle = handle.trim();
|
||||||
if (!trimmedHandle) {
|
if (!trimmedHandle) {
|
||||||
|
console.log('Handle is empty');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Submitting handle:', trimmedHandle);
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Resolve DID from handle
|
// Resolve DID from handle
|
||||||
const did = await atprotoOAuthService.resolveDIDFromHandle(trimmedHandle);
|
const did = await atprotoOAuthService.resolveDIDFromHandle(trimmedHandle);
|
||||||
|
console.log('Resolved DID:', did);
|
||||||
|
|
||||||
// Update session with resolved DID and handle
|
// Update session with resolved DID and handle
|
||||||
const updatedSession = {
|
const updatedSession = {
|
||||||
@@ -106,6 +129,7 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
// Success - notify parent component
|
// Success - notify parent component
|
||||||
onSuccess(did, trimmedHandle);
|
onSuccess(did, trimmedHandle);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to resolve DID:', error);
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
onError(error instanceof Error ? error.message : 'ハンドルからDIDの解決に失敗しました');
|
onError(error instanceof Error ? error.message : 'ハンドルからDIDの解決に失敗しました');
|
||||||
}
|
}
|
||||||
@@ -125,6 +149,7 @@ export const OAuthCallback: React.FC<OAuthCallbackProps> = ({ onSuccess, onError
|
|||||||
type="text"
|
type="text"
|
||||||
value={handle}
|
value={handle}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
|
console.log('Input changed:', e.target.value);
|
||||||
setHandle(e.target.value);
|
setHandle(e.target.value);
|
||||||
}}
|
}}
|
||||||
placeholder="例: syui.ai または user.bsky.social"
|
placeholder="例: syui.ai または user.bsky.social"
|
||||||
|
@@ -6,9 +6,14 @@ export const OAuthCallbackPage: React.FC = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
console.log('=== OAUTH CALLBACK PAGE MOUNTED ===');
|
||||||
|
console.log('Current URL:', window.location.href);
|
||||||
|
console.log('Search params:', window.location.search);
|
||||||
|
console.log('Pathname:', window.location.pathname);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleSuccess = (did: string, handle: string) => {
|
const handleSuccess = (did: string, handle: string) => {
|
||||||
|
console.log('OAuth success, redirecting to home:', { did, handle });
|
||||||
|
|
||||||
// Add a small delay to ensure state is properly updated
|
// Add a small delay to ensure state is properly updated
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -17,6 +22,7 @@ export const OAuthCallbackPage: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleError = (error: string) => {
|
const handleError = (error: string) => {
|
||||||
|
console.error('OAuth error, redirecting to home:', error);
|
||||||
|
|
||||||
// Add a small delay before redirect
|
// Add a small delay before redirect
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@@ -1,14 +1,10 @@
|
|||||||
// Application configuration
|
// Application configuration
|
||||||
export interface AppConfig {
|
export interface AppConfig {
|
||||||
adminDid: string;
|
adminDid: string;
|
||||||
adminHandle: string;
|
|
||||||
aiDid: string;
|
|
||||||
aiHandle: string;
|
|
||||||
aiDisplayName: string;
|
|
||||||
aiAvatar: string;
|
|
||||||
aiDescription: string;
|
|
||||||
collections: {
|
collections: {
|
||||||
base: string; // Base collection like "ai.syui.log"
|
comment: string;
|
||||||
|
user: string;
|
||||||
|
chat: string;
|
||||||
};
|
};
|
||||||
host: string;
|
host: string;
|
||||||
rkey?: string; // Current post rkey if on post page
|
rkey?: string; // Current post rkey if on post page
|
||||||
@@ -17,36 +13,13 @@ export interface AppConfig {
|
|||||||
aiProvider: string;
|
aiProvider: string;
|
||||||
aiModel: string;
|
aiModel: string;
|
||||||
aiHost: string;
|
aiHost: string;
|
||||||
aiSystemPrompt: string;
|
|
||||||
allowedHandles: string[]; // Handles allowed for OAuth authentication
|
|
||||||
atprotoPds: string; // Configured PDS for admin/ai handles
|
|
||||||
// Legacy - prefer per-user PDS detection
|
|
||||||
bskyPublicApi: string;
|
bskyPublicApi: string;
|
||||||
atprotoApi: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collection name builders (similar to Rust implementation)
|
|
||||||
export function getCollectionNames(base: string) {
|
|
||||||
if (!base) {
|
|
||||||
// Fallback to default
|
|
||||||
base = 'ai.syui.log';
|
|
||||||
}
|
|
||||||
|
|
||||||
const collections = {
|
|
||||||
comment: base,
|
|
||||||
user: `${base}.user`,
|
|
||||||
chat: `${base}.chat`,
|
|
||||||
chatLang: `${base}.chat.lang`,
|
|
||||||
chatComment: `${base}.chat.comment`,
|
|
||||||
};
|
|
||||||
|
|
||||||
return collections;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate collection names from host
|
// Generate collection names from host
|
||||||
// Format: ${reg}.${name}.${sub}
|
// Format: ${reg}.${name}.${sub}
|
||||||
// Example: log.syui.ai -> ai.syui.log
|
// Example: log.syui.ai -> ai.syui.log
|
||||||
function generateBaseCollectionFromHost(host: string): string {
|
function generateCollectionNames(host: string): { comment: string; user: string; chat: string } {
|
||||||
try {
|
try {
|
||||||
// Remove protocol if present
|
// Remove protocol if present
|
||||||
const cleanHost = host.replace(/^https?:\/\//, '');
|
const cleanHost = host.replace(/^https?:\/\//, '');
|
||||||
@@ -61,50 +34,43 @@ function generateBaseCollectionFromHost(host: string): string {
|
|||||||
// Reverse the parts for collection naming
|
// Reverse the parts for collection naming
|
||||||
// log.syui.ai -> ai.syui.log
|
// log.syui.ai -> ai.syui.log
|
||||||
const reversedParts = parts.reverse();
|
const reversedParts = parts.reverse();
|
||||||
const result = reversedParts.join('.');
|
const collectionBase = reversedParts.join('.');
|
||||||
return result;
|
|
||||||
|
return {
|
||||||
|
comment: collectionBase,
|
||||||
|
user: `${collectionBase}.user`,
|
||||||
|
chat: `${collectionBase}.chat`
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Fallback to default
|
console.warn('Failed to generate collection names from host:', host, error);
|
||||||
return 'ai.syui.log';
|
// Fallback to default collections
|
||||||
|
return {
|
||||||
|
comment: 'ai.syui.log',
|
||||||
|
user: 'ai.syui.log.user',
|
||||||
|
chat: 'ai.syui.log.chat'
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract rkey from current URL
|
// Extract rkey from current URL
|
||||||
// /posts/xxx -> xxx (remove .html if present)
|
// /posts/xxx.html -> xxx
|
||||||
function extractRkeyFromUrl(): string | undefined {
|
function extractRkeyFromUrl(): string | undefined {
|
||||||
const pathname = window.location.pathname;
|
const pathname = window.location.pathname;
|
||||||
const match = pathname.match(/\/posts\/([^/]+)\/?$/);
|
const match = pathname.match(/\/posts\/([^/]+)\.html$/);
|
||||||
if (match) {
|
return match ? match[1] : undefined;
|
||||||
// Remove .html extension if present
|
|
||||||
return match[1].replace(/\.html$/, '');
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get application configuration from environment variables
|
// Get application configuration from environment variables
|
||||||
export function getAppConfig(): AppConfig {
|
export function getAppConfig(): AppConfig {
|
||||||
const host = import.meta.env.VITE_APP_HOST || 'https://log.syui.ai';
|
const host = import.meta.env.VITE_APP_HOST || 'https://log.syui.ai';
|
||||||
const adminHandle = import.meta.env.VITE_ADMIN_HANDLE || 'ai.syui.ai';
|
|
||||||
const aiHandle = import.meta.env.VITE_AI_HANDLE || 'ai.syui.ai';
|
|
||||||
|
|
||||||
// DIDsはハンドルから実行時に解決される(フォールバック用のみ保持)
|
|
||||||
const adminDid = import.meta.env.VITE_ADMIN_DID || 'did:plc:uqzpqmrjnptsxezjx4xuh2mn';
|
const adminDid = import.meta.env.VITE_ADMIN_DID || 'did:plc:uqzpqmrjnptsxezjx4xuh2mn';
|
||||||
const aiDid = import.meta.env.VITE_AI_DID || 'did:plc:4hqjfn7m6n5hno3doamuhgef';
|
|
||||||
const aiDisplayName = import.meta.env.VITE_AI_DISPLAY_NAME || 'ai';
|
|
||||||
const aiAvatar = import.meta.env.VITE_AI_AVATAR || '';
|
|
||||||
const aiDescription = import.meta.env.VITE_AI_DESCRIPTION || '';
|
|
||||||
|
|
||||||
// Priority: Environment variables > Auto-generated from host
|
// Priority: Environment variables > Auto-generated from host
|
||||||
const autoGeneratedBase = generateBaseCollectionFromHost(host);
|
const autoGeneratedCollections = generateCollectionNames(host);
|
||||||
let baseCollection = import.meta.env.VITE_OAUTH_COLLECTION || autoGeneratedBase;
|
|
||||||
|
|
||||||
// Ensure base collection is never undefined
|
|
||||||
if (!baseCollection) {
|
|
||||||
baseCollection = 'ai.syui.log';
|
|
||||||
}
|
|
||||||
|
|
||||||
const collections = {
|
const collections = {
|
||||||
base: baseCollection,
|
comment: import.meta.env.VITE_COLLECTION_COMMENT || autoGeneratedCollections.comment,
|
||||||
|
user: import.meta.env.VITE_COLLECTION_USER || autoGeneratedCollections.user,
|
||||||
|
chat: import.meta.env.VITE_COLLECTION_CHAT || autoGeneratedCollections.chat,
|
||||||
};
|
};
|
||||||
|
|
||||||
const rkey = extractRkeyFromUrl();
|
const rkey = extractRkeyFromUrl();
|
||||||
@@ -115,29 +81,19 @@ export function getAppConfig(): AppConfig {
|
|||||||
const aiProvider = import.meta.env.VITE_AI_PROVIDER || 'ollama';
|
const aiProvider = import.meta.env.VITE_AI_PROVIDER || 'ollama';
|
||||||
const aiModel = import.meta.env.VITE_AI_MODEL || 'gemma2:2b';
|
const aiModel = import.meta.env.VITE_AI_MODEL || 'gemma2:2b';
|
||||||
const aiHost = import.meta.env.VITE_AI_HOST || 'https://ollama.syui.ai';
|
const aiHost = import.meta.env.VITE_AI_HOST || 'https://ollama.syui.ai';
|
||||||
const aiSystemPrompt = import.meta.env.VITE_AI_SYSTEM_PROMPT || 'You are a helpful AI assistant trained on this blog\'s content.';
|
|
||||||
const atprotoPds = import.meta.env.VITE_ATPROTO_PDS || 'syu.is';
|
|
||||||
const bskyPublicApi = import.meta.env.VITE_BSKY_PUBLIC_API || 'https://public.api.bsky.app';
|
const bskyPublicApi = import.meta.env.VITE_BSKY_PUBLIC_API || 'https://public.api.bsky.app';
|
||||||
const atprotoApi = import.meta.env.VITE_ATPROTO_API || 'https://bsky.social';
|
|
||||||
|
|
||||||
// Parse allowed handles list
|
console.log('App configuration:', {
|
||||||
const allowedHandlesStr = import.meta.env.VITE_ATPROTO_HANDLE_LIST || '[]';
|
host,
|
||||||
let allowedHandles: string[] = [];
|
adminDid,
|
||||||
try {
|
collections,
|
||||||
allowedHandles = JSON.parse(allowedHandlesStr);
|
rkey: rkey || 'none (not on post page)',
|
||||||
} catch {
|
ai: { enabled: aiEnabled, askAi: aiAskAi, provider: aiProvider, model: aiModel, host: aiHost },
|
||||||
// If parsing fails, allow all handles (empty array means no restriction)
|
bskyPublicApi
|
||||||
allowedHandles = [];
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
adminDid,
|
adminDid,
|
||||||
adminHandle,
|
|
||||||
aiDid,
|
|
||||||
aiHandle,
|
|
||||||
aiDisplayName,
|
|
||||||
aiAvatar,
|
|
||||||
aiDescription,
|
|
||||||
collections,
|
collections,
|
||||||
host,
|
host,
|
||||||
rkey,
|
rkey,
|
||||||
@@ -146,11 +102,7 @@ export function getAppConfig(): AppConfig {
|
|||||||
aiProvider,
|
aiProvider,
|
||||||
aiModel,
|
aiModel,
|
||||||
aiHost,
|
aiHost,
|
||||||
aiSystemPrompt,
|
bskyPublicApi
|
||||||
allowedHandles,
|
|
||||||
atprotoPds,
|
|
||||||
bskyPublicApi,
|
|
||||||
atprotoApi
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,8 +12,10 @@ import { OAuthEndpointHandler } from './utils/oauth-endpoints'
|
|||||||
|
|
||||||
// Mount React app to all comment-atproto divs
|
// Mount React app to all comment-atproto divs
|
||||||
const mountPoints = document.querySelectorAll('#comment-atproto');
|
const mountPoints = document.querySelectorAll('#comment-atproto');
|
||||||
|
console.log(`Found ${mountPoints.length} comment-atproto mount points`);
|
||||||
|
|
||||||
mountPoints.forEach((mountPoint, index) => {
|
mountPoints.forEach((mountPoint, index) => {
|
||||||
|
console.log(`Mounting React app to comment-atproto #${index + 1}`);
|
||||||
ReactDOM.createRoot(mountPoint as HTMLElement).render(
|
ReactDOM.createRoot(mountPoint as HTMLElement).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
|
@@ -73,6 +73,7 @@ export const aiCardApi = {
|
|||||||
});
|
});
|
||||||
return response.data.data;
|
return response.data.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.warn('ai.gpt AI分析機能が利用できません:', error);
|
||||||
throw new Error('AI分析機能を利用するにはai.gptサーバーが必要です');
|
throw new Error('AI分析機能を利用するにはai.gptサーバーが必要です');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -85,6 +86,7 @@ export const aiCardApi = {
|
|||||||
const response = await aiGptApi.get('/card_get_gacha_stats');
|
const response = await aiGptApi.get('/card_get_gacha_stats');
|
||||||
return response.data.data;
|
return response.data.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.warn('ai.gpt AI統計機能が利用できません:', error);
|
||||||
throw new Error('AI統計機能を利用するにはai.gptサーバーが必要です');
|
throw new Error('AI統計機能を利用するにはai.gptサーバーが必要です');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@@ -31,11 +31,11 @@ class AtprotoOAuthService {
|
|||||||
|
|
||||||
private async _doInitialize(): Promise<void> {
|
private async _doInitialize(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
console.log('=== INITIALIZING ATPROTO OAUTH CLIENT ===');
|
||||||
|
|
||||||
// Generate client ID based on current origin
|
// Generate client ID based on current origin
|
||||||
const clientId = this.getClientId();
|
const clientId = this.getClientId();
|
||||||
|
console.log('Client ID:', clientId);
|
||||||
|
|
||||||
// Support multiple PDS hosts for OAuth
|
// Support multiple PDS hosts for OAuth
|
||||||
this.oauthClient = await BrowserOAuthClient.load({
|
this.oauthClient = await BrowserOAuthClient.load({
|
||||||
@@ -43,33 +43,39 @@ class AtprotoOAuthService {
|
|||||||
handleResolver: 'https://bsky.social', // Default resolver
|
handleResolver: 'https://bsky.social', // Default resolver
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('BrowserOAuthClient initialized successfully with multi-PDS support');
|
||||||
|
|
||||||
// Try to restore existing session
|
// Try to restore existing session
|
||||||
const result = await this.oauthClient.init();
|
const result = await this.oauthClient.init();
|
||||||
if (result?.session) {
|
if (result?.session) {
|
||||||
|
console.log('Existing session restored:', {
|
||||||
|
did: result.session.did,
|
||||||
|
handle: result.session.handle || 'unknown',
|
||||||
|
hasAccessJwt: !!result.session.accessJwt,
|
||||||
|
hasRefreshJwt: !!result.session.refreshJwt
|
||||||
|
});
|
||||||
|
|
||||||
// Create Agent instance with proper configuration
|
// Create Agent instance with proper configuration
|
||||||
|
console.log('Creating Agent with session:', result.session);
|
||||||
|
|
||||||
// Delete the old agent initialization code - we'll create it properly below
|
// Delete the old agent initialization code - we'll create it properly below
|
||||||
|
|
||||||
// Set the session after creating the agent
|
// Set the session after creating the agent
|
||||||
// The session object from BrowserOAuthClient appears to be a special object
|
// The session object from BrowserOAuthClient appears to be a special object
|
||||||
|
console.log('Full session object:', result.session);
|
||||||
|
console.log('Session type:', typeof result.session);
|
||||||
|
console.log('Session constructor:', result.session?.constructor?.name);
|
||||||
|
|
||||||
// Try to iterate over the session object
|
// Try to iterate over the session object
|
||||||
if (result.session) {
|
if (result.session) {
|
||||||
|
console.log('Session properties:');
|
||||||
for (const key in result.session) {
|
for (const key in result.session) {
|
||||||
|
console.log(` ${key}:`, result.session[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if session has methods
|
// Check if session has methods
|
||||||
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(result.session));
|
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(result.session));
|
||||||
|
console.log('Session methods:', methods);
|
||||||
}
|
}
|
||||||
|
|
||||||
// BrowserOAuthClient might return a Session object that needs to be used with the agent
|
// BrowserOAuthClient might return a Session object that needs to be used with the agent
|
||||||
@@ -77,36 +83,36 @@ class AtprotoOAuthService {
|
|||||||
if (result.session) {
|
if (result.session) {
|
||||||
// Process the session to extract DID and handle
|
// Process the session to extract DID and handle
|
||||||
const sessionData = await this.processSession(result.session);
|
const sessionData = await this.processSession(result.session);
|
||||||
|
console.log('Session processed during initialization:', sessionData);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
console.log('No existing session found');
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize OAuth client:', error);
|
||||||
this.initializePromise = null; // Reset on error to allow retry
|
this.initializePromise = null; // Reset on error to allow retry
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async processSession(session: any): Promise<{ did: string; handle: string }> {
|
private async processSession(session: any): Promise<{ did: string; handle: string }> {
|
||||||
|
console.log('Processing session:', session);
|
||||||
|
|
||||||
// Log full session structure
|
// Log full session structure
|
||||||
|
console.log('Session structure:');
|
||||||
|
console.log('- sub:', session.sub);
|
||||||
|
console.log('- did:', session.did);
|
||||||
|
console.log('- handle:', session.handle);
|
||||||
|
console.log('- iss:', session.iss);
|
||||||
|
console.log('- aud:', session.aud);
|
||||||
|
|
||||||
// Check if agent has properties we can access
|
// Check if agent has properties we can access
|
||||||
if (session.agent) {
|
if (session.agent) {
|
||||||
|
console.log('- agent:', session.agent);
|
||||||
|
console.log('- agent.did:', session.agent?.did);
|
||||||
|
console.log('- agent.handle:', session.agent?.handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
const did = session.sub || session.did;
|
const did = session.sub || session.did;
|
||||||
@@ -115,18 +121,18 @@ class AtprotoOAuthService {
|
|||||||
// Create Agent directly with session (per official docs)
|
// Create Agent directly with session (per official docs)
|
||||||
try {
|
try {
|
||||||
this.agent = new Agent(session);
|
this.agent = new Agent(session);
|
||||||
|
console.log('Agent created directly with session');
|
||||||
|
|
||||||
// Check if agent has session info after creation
|
// Check if agent has session info after creation
|
||||||
|
console.log('Agent after creation:');
|
||||||
|
console.log('- agent.did:', this.agent.did);
|
||||||
|
console.log('- agent.session:', this.agent.session);
|
||||||
if (this.agent.session) {
|
if (this.agent.session) {
|
||||||
|
console.log('- agent.session.did:', this.agent.session.did);
|
||||||
|
console.log('- agent.session.handle:', this.agent.session.handle);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log('Failed to create Agent with session directly, trying dpopFetch method');
|
||||||
// Fallback to dpopFetch method
|
// Fallback to dpopFetch method
|
||||||
this.agent = new Agent({
|
this.agent = new Agent({
|
||||||
service: session.server?.serviceEndpoint || 'https://bsky.social',
|
service: session.server?.serviceEndpoint || 'https://bsky.social',
|
||||||
@@ -139,7 +145,7 @@ class AtprotoOAuthService {
|
|||||||
|
|
||||||
// If handle is missing, try multiple methods to resolve it
|
// If handle is missing, try multiple methods to resolve it
|
||||||
if (!handle || handle === 'unknown') {
|
if (!handle || handle === 'unknown') {
|
||||||
|
console.log('Handle not in session, attempting to resolve...');
|
||||||
|
|
||||||
// Method 1: Try using the agent to get profile
|
// Method 1: Try using the agent to get profile
|
||||||
try {
|
try {
|
||||||
@@ -148,11 +154,11 @@ class AtprotoOAuthService {
|
|||||||
if (profile.data.handle) {
|
if (profile.data.handle) {
|
||||||
handle = profile.data.handle;
|
handle = profile.data.handle;
|
||||||
(this as any)._sessionInfo.handle = handle;
|
(this as any)._sessionInfo.handle = handle;
|
||||||
|
console.log('Successfully resolved handle via getProfile:', handle);
|
||||||
return { did, handle };
|
return { did, handle };
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('getProfile failed:', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method 2: Try using describeRepo
|
// Method 2: Try using describeRepo
|
||||||
@@ -163,20 +169,18 @@ class AtprotoOAuthService {
|
|||||||
if (repoDesc.data.handle) {
|
if (repoDesc.data.handle) {
|
||||||
handle = repoDesc.data.handle;
|
handle = repoDesc.data.handle;
|
||||||
(this as any)._sessionInfo.handle = handle;
|
(this as any)._sessionInfo.handle = handle;
|
||||||
|
console.log('Got handle from describeRepo:', handle);
|
||||||
return { did, handle };
|
return { did, handle };
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('describeRepo failed:', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method 3: Fallback for admin DID
|
// Method 3: Hardcoded fallback for known DIDs
|
||||||
const adminDid = import.meta.env.VITE_ADMIN_DID;
|
if (did === 'did:plc:uqzpqmrjnptsxezjx4xuh2mn') {
|
||||||
if (did === adminDid) {
|
handle = 'syui.ai';
|
||||||
const appHost = import.meta.env.VITE_APP_HOST || 'https://syui.ai';
|
|
||||||
handle = new URL(appHost).hostname;
|
|
||||||
(this as any)._sessionInfo.handle = handle;
|
(this as any)._sessionInfo.handle = handle;
|
||||||
|
console.log('Using hardcoded handle for known DID');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +191,7 @@ class AtprotoOAuthService {
|
|||||||
// Use environment variable if available
|
// Use environment variable if available
|
||||||
const envClientId = import.meta.env.VITE_OAUTH_CLIENT_ID;
|
const envClientId = import.meta.env.VITE_OAUTH_CLIENT_ID;
|
||||||
if (envClientId) {
|
if (envClientId) {
|
||||||
|
console.log('Using client ID from environment:', envClientId);
|
||||||
return envClientId;
|
return envClientId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +200,7 @@ class AtprotoOAuthService {
|
|||||||
// For localhost development, use undefined for loopback client
|
// For localhost development, use undefined for loopback client
|
||||||
// The BrowserOAuthClient will handle this automatically
|
// The BrowserOAuthClient will handle this automatically
|
||||||
if (origin.includes('localhost') || origin.includes('127.0.0.1')) {
|
if (origin.includes('localhost') || origin.includes('127.0.0.1')) {
|
||||||
|
console.log('Using loopback client for localhost development');
|
||||||
return undefined as any; // Loopback client
|
return undefined as any; // Loopback client
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,7 +209,7 @@ class AtprotoOAuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private detectPDSFromHandle(handle: string): string {
|
private detectPDSFromHandle(handle: string): string {
|
||||||
|
console.log('Detecting PDS for handle:', handle);
|
||||||
|
|
||||||
// Supported PDS hosts and their corresponding handles
|
// Supported PDS hosts and their corresponding handles
|
||||||
const pdsMapping = {
|
const pdsMapping = {
|
||||||
@@ -216,22 +220,22 @@ class AtprotoOAuthService {
|
|||||||
// Check if handle ends with known PDS domains
|
// Check if handle ends with known PDS domains
|
||||||
for (const [domain, pdsUrl] of Object.entries(pdsMapping)) {
|
for (const [domain, pdsUrl] of Object.entries(pdsMapping)) {
|
||||||
if (handle.endsWith(`.${domain}`)) {
|
if (handle.endsWith(`.${domain}`)) {
|
||||||
|
console.log(`Handle ${handle} mapped to PDS: ${pdsUrl}`);
|
||||||
return pdsUrl;
|
return pdsUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default to bsky.social
|
// Default to bsky.social
|
||||||
|
console.log(`Handle ${handle} using default PDS: https://bsky.social`);
|
||||||
return 'https://bsky.social';
|
return 'https://bsky.social';
|
||||||
}
|
}
|
||||||
|
|
||||||
async initiateOAuthFlow(handle?: string): Promise<void> {
|
async initiateOAuthFlow(handle?: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
console.log('=== INITIATING OAUTH FLOW ===');
|
||||||
|
|
||||||
if (!this.oauthClient) {
|
if (!this.oauthClient) {
|
||||||
|
console.log('OAuth client not initialized, initializing now...');
|
||||||
await this.initialize();
|
await this.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,15 +251,15 @@ class AtprotoOAuthService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Starting OAuth flow for handle:', handle);
|
||||||
|
|
||||||
// Detect PDS based on handle
|
// Detect PDS based on handle
|
||||||
const pdsUrl = this.detectPDSFromHandle(handle);
|
const pdsUrl = this.detectPDSFromHandle(handle);
|
||||||
|
console.log('Detected PDS for handle:', { handle, pdsUrl });
|
||||||
|
|
||||||
// Re-initialize OAuth client with correct PDS if needed
|
// Re-initialize OAuth client with correct PDS if needed
|
||||||
if (pdsUrl !== 'https://bsky.social') {
|
if (pdsUrl !== 'https://bsky.social') {
|
||||||
|
console.log('Re-initializing OAuth client for custom PDS:', pdsUrl);
|
||||||
this.oauthClient = await BrowserOAuthClient.load({
|
this.oauthClient = await BrowserOAuthClient.load({
|
||||||
clientId: this.getClientId(),
|
clientId: this.getClientId(),
|
||||||
handleResolver: pdsUrl,
|
handleResolver: pdsUrl,
|
||||||
@@ -263,14 +267,20 @@ class AtprotoOAuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start OAuth authorization flow
|
// Start OAuth authorization flow
|
||||||
|
console.log('Calling oauthClient.authorize with handle:', handle);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const authUrl = await this.oauthClient.authorize(handle, {
|
const authUrl = await this.oauthClient.authorize(handle, {
|
||||||
scope: 'atproto transition:generic',
|
scope: 'atproto transition:generic',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('Authorization URL generated:', authUrl.toString());
|
||||||
|
console.log('URL breakdown:', {
|
||||||
|
protocol: authUrl.protocol,
|
||||||
|
hostname: authUrl.hostname,
|
||||||
|
pathname: authUrl.pathname,
|
||||||
|
search: authUrl.search
|
||||||
|
});
|
||||||
|
|
||||||
// Store some debug info before redirect
|
// Store some debug info before redirect
|
||||||
sessionStorage.setItem('oauth_debug_pre_redirect', JSON.stringify({
|
sessionStorage.setItem('oauth_debug_pre_redirect', JSON.stringify({
|
||||||
@@ -281,30 +291,35 @@ class AtprotoOAuthService {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
// Redirect to authorization server
|
// Redirect to authorization server
|
||||||
|
console.log('About to redirect to:', authUrl.toString());
|
||||||
window.location.href = authUrl.toString();
|
window.location.href = authUrl.toString();
|
||||||
} catch (authorizeError) {
|
} catch (authorizeError) {
|
||||||
|
console.error('oauthClient.authorize failed:', authorizeError);
|
||||||
|
console.error('Error details:', {
|
||||||
|
name: authorizeError.name,
|
||||||
|
message: authorizeError.message,
|
||||||
|
stack: authorizeError.stack
|
||||||
|
});
|
||||||
throw authorizeError;
|
throw authorizeError;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to initiate OAuth flow:', error);
|
||||||
throw new Error(`OAuth認証の開始に失敗しました: ${error}`);
|
throw new Error(`OAuth認証の開始に失敗しました: ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleOAuthCallback(): Promise<{ did: string; handle: string } | null> {
|
async handleOAuthCallback(): Promise<{ did: string; handle: string } | null> {
|
||||||
try {
|
try {
|
||||||
|
console.log('=== HANDLING OAUTH CALLBACK ===');
|
||||||
|
console.log('Current URL:', window.location.href);
|
||||||
|
console.log('URL hash:', window.location.hash);
|
||||||
|
console.log('URL search:', window.location.search);
|
||||||
|
|
||||||
// BrowserOAuthClient should automatically handle the callback
|
// BrowserOAuthClient should automatically handle the callback
|
||||||
// We just need to initialize it and it will process the current URL
|
// We just need to initialize it and it will process the current URL
|
||||||
if (!this.oauthClient) {
|
if (!this.oauthClient) {
|
||||||
|
console.log('OAuth client not initialized, initializing now...');
|
||||||
await this.initialize();
|
await this.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,11 +327,11 @@ class AtprotoOAuthService {
|
|||||||
throw new Error('Failed to initialize OAuth client');
|
throw new Error('Failed to initialize OAuth client');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('OAuth client ready, initializing to process callback...');
|
||||||
|
|
||||||
// Call init() again to process the callback URL
|
// Call init() again to process the callback URL
|
||||||
const result = await this.oauthClient.init();
|
const result = await this.oauthClient.init();
|
||||||
|
console.log('OAuth callback processing result:', result);
|
||||||
|
|
||||||
if (result?.session) {
|
if (result?.session) {
|
||||||
// Process the session
|
// Process the session
|
||||||
@@ -324,42 +339,47 @@ class AtprotoOAuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If no session yet, wait a bit and try again
|
// If no session yet, wait a bit and try again
|
||||||
|
console.log('No session found immediately, waiting...');
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
// Try to check session again
|
// Try to check session again
|
||||||
const sessionCheck = await this.checkSession();
|
const sessionCheck = await this.checkSession();
|
||||||
if (sessionCheck) {
|
if (sessionCheck) {
|
||||||
|
console.log('Session found after delay:', sessionCheck);
|
||||||
return sessionCheck;
|
return sessionCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.warn('OAuth callback completed but no session was created');
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('OAuth callback handling failed:', error);
|
||||||
|
console.error('Error details:', {
|
||||||
|
name: error.name,
|
||||||
|
message: error.message,
|
||||||
|
stack: error.stack
|
||||||
|
});
|
||||||
throw new Error(`OAuth認証の完了に失敗しました: ${error.message}`);
|
throw new Error(`OAuth認証の完了に失敗しました: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkSession(): Promise<{ did: string; handle: string } | null> {
|
async checkSession(): Promise<{ did: string; handle: string } | null> {
|
||||||
try {
|
try {
|
||||||
|
console.log('=== CHECK SESSION CALLED ===');
|
||||||
|
|
||||||
if (!this.oauthClient) {
|
if (!this.oauthClient) {
|
||||||
|
console.log('No OAuth client, initializing...');
|
||||||
await this.initialize();
|
await this.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.oauthClient) {
|
if (!this.oauthClient) {
|
||||||
|
console.log('OAuth client initialization failed');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Running oauthClient.init() to check session...');
|
||||||
const result = await this.oauthClient.init();
|
const result = await this.oauthClient.init();
|
||||||
|
console.log('oauthClient.init() result:', result);
|
||||||
|
|
||||||
if (result?.session) {
|
if (result?.session) {
|
||||||
// Use the common session processing method
|
// Use the common session processing method
|
||||||
@@ -368,7 +388,7 @@ class AtprotoOAuthService {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Session check failed:', error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -378,7 +398,13 @@ class AtprotoOAuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSession(): AtprotoSession | null {
|
getSession(): AtprotoSession | null {
|
||||||
|
console.log('getSession called');
|
||||||
|
console.log('Current state:', {
|
||||||
|
hasAgent: !!this.agent,
|
||||||
|
hasAgentSession: !!this.agent?.session,
|
||||||
|
hasOAuthClient: !!this.oauthClient,
|
||||||
|
hasSessionInfo: !!(this as any)._sessionInfo
|
||||||
|
});
|
||||||
|
|
||||||
// First check if we have an agent with session
|
// First check if we have an agent with session
|
||||||
if (this.agent?.session) {
|
if (this.agent?.session) {
|
||||||
@@ -388,7 +414,7 @@ class AtprotoOAuthService {
|
|||||||
accessJwt: this.agent.session.accessJwt || '',
|
accessJwt: this.agent.session.accessJwt || '',
|
||||||
refreshJwt: this.agent.session.refreshJwt || '',
|
refreshJwt: this.agent.session.refreshJwt || '',
|
||||||
};
|
};
|
||||||
|
console.log('Returning agent session:', session);
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -400,11 +426,11 @@ class AtprotoOAuthService {
|
|||||||
accessJwt: 'dpop-protected', // Indicate that tokens are handled by dpopFetch
|
accessJwt: 'dpop-protected', // Indicate that tokens are handled by dpopFetch
|
||||||
refreshJwt: 'dpop-protected',
|
refreshJwt: 'dpop-protected',
|
||||||
};
|
};
|
||||||
|
console.log('Returning stored session info:', session);
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('No session available');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -424,28 +450,28 @@ class AtprotoOAuthService {
|
|||||||
|
|
||||||
async logout(): Promise<void> {
|
async logout(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
console.log('=== LOGGING OUT ===');
|
||||||
|
|
||||||
// Clear Agent
|
// Clear Agent
|
||||||
this.agent = null;
|
this.agent = null;
|
||||||
|
console.log('Agent cleared');
|
||||||
|
|
||||||
// Clear BrowserOAuthClient session
|
// Clear BrowserOAuthClient session
|
||||||
if (this.oauthClient) {
|
if (this.oauthClient) {
|
||||||
|
console.log('Clearing OAuth client session...');
|
||||||
try {
|
try {
|
||||||
// BrowserOAuthClient may have a revoke or signOut method
|
// BrowserOAuthClient may have a revoke or signOut method
|
||||||
if (typeof (this.oauthClient as any).signOut === 'function') {
|
if (typeof (this.oauthClient as any).signOut === 'function') {
|
||||||
await (this.oauthClient as any).signOut();
|
await (this.oauthClient as any).signOut();
|
||||||
|
console.log('OAuth client signed out');
|
||||||
} else if (typeof (this.oauthClient as any).revoke === 'function') {
|
} else if (typeof (this.oauthClient as any).revoke === 'function') {
|
||||||
await (this.oauthClient as any).revoke();
|
await (this.oauthClient as any).revoke();
|
||||||
|
console.log('OAuth client revoked');
|
||||||
} else {
|
} else {
|
||||||
|
console.log('No explicit signOut method found on OAuth client');
|
||||||
}
|
}
|
||||||
} catch (oauthError) {
|
} catch (oauthError) {
|
||||||
|
console.error('OAuth client logout error:', oauthError);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the OAuth client to force re-initialization
|
// Reset the OAuth client to force re-initialization
|
||||||
@@ -466,11 +492,11 @@ class AtprotoOAuthService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
keysToRemove.forEach(key => {
|
keysToRemove.forEach(key => {
|
||||||
|
console.log('Removing localStorage key:', key);
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('=== LOGOUT COMPLETED ===');
|
||||||
|
|
||||||
// Force page reload to ensure clean state
|
// Force page reload to ensure clean state
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -478,7 +504,7 @@ class AtprotoOAuthService {
|
|||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Logout failed:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -493,8 +519,8 @@ class AtprotoOAuthService {
|
|||||||
const did = sessionInfo.did;
|
const did = sessionInfo.did;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Saving cards to atproto collection...');
|
||||||
|
console.log('Using DID:', did);
|
||||||
|
|
||||||
// Ensure we have a fresh agent
|
// Ensure we have a fresh agent
|
||||||
if (!this.agent) {
|
if (!this.agent) {
|
||||||
@@ -524,6 +550,13 @@ class AtprotoOAuthService {
|
|||||||
createdAt: createdAt
|
createdAt: createdAt
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log('PutRecord request:', {
|
||||||
|
repo: did,
|
||||||
|
collection: collection,
|
||||||
|
rkey: rkey,
|
||||||
|
record: record
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Use Agent's com.atproto.repo.putRecord method
|
// Use Agent's com.atproto.repo.putRecord method
|
||||||
const response = await this.agent.com.atproto.repo.putRecord({
|
const response = await this.agent.com.atproto.repo.putRecord({
|
||||||
@@ -533,9 +566,9 @@ class AtprotoOAuthService {
|
|||||||
record: record
|
record: record
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('カードデータをai.card.boxに保存しました:', response);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('カードボックス保存エラー:', error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -551,8 +584,8 @@ class AtprotoOAuthService {
|
|||||||
const did = sessionInfo.did;
|
const did = sessionInfo.did;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Fetching cards from atproto collection...');
|
||||||
|
console.log('Using DID:', did);
|
||||||
|
|
||||||
// Ensure we have a fresh agent
|
// Ensure we have a fresh agent
|
||||||
if (!this.agent) {
|
if (!this.agent) {
|
||||||
@@ -565,7 +598,7 @@ class AtprotoOAuthService {
|
|||||||
rkey: 'self'
|
rkey: 'self'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('Cards from box response:', response);
|
||||||
|
|
||||||
// Convert to expected format
|
// Convert to expected format
|
||||||
const result = {
|
const result = {
|
||||||
@@ -578,7 +611,7 @@ class AtprotoOAuthService {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('カードボックス取得エラー:', error);
|
||||||
|
|
||||||
// If record doesn't exist, return empty
|
// If record doesn't exist, return empty
|
||||||
if (error.toString().includes('RecordNotFound')) {
|
if (error.toString().includes('RecordNotFound')) {
|
||||||
@@ -600,8 +633,8 @@ class AtprotoOAuthService {
|
|||||||
const did = sessionInfo.did;
|
const did = sessionInfo.did;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Deleting card box collection...');
|
||||||
|
console.log('Using DID:', did);
|
||||||
|
|
||||||
// Ensure we have a fresh agent
|
// Ensure we have a fresh agent
|
||||||
if (!this.agent) {
|
if (!this.agent) {
|
||||||
@@ -614,35 +647,33 @@ class AtprotoOAuthService {
|
|||||||
rkey: 'self'
|
rkey: 'self'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('Card box deleted successfully:', response);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('カードボックス削除エラー:', error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 手動でトークンを設定(開発・デバッグ用)
|
// 手動でトークンを設定(開発・デバッグ用)
|
||||||
setManualTokens(accessJwt: string, refreshJwt: string): void {
|
setManualTokens(accessJwt: string, refreshJwt: string): void {
|
||||||
|
console.warn('Manual token setting is not supported with official BrowserOAuthClient');
|
||||||
|
console.warn('Please use the proper OAuth flow instead');
|
||||||
|
|
||||||
// For backward compatibility, store in localStorage
|
// For backward compatibility, store in localStorage
|
||||||
const adminDid = import.meta.env.VITE_ADMIN_DID || 'did:plc:unknown';
|
|
||||||
const appHost = import.meta.env.VITE_APP_HOST || 'https://example.com';
|
|
||||||
const session: AtprotoSession = {
|
const session: AtprotoSession = {
|
||||||
did: adminDid,
|
did: 'did:plc:uqzpqmrjnptsxezjx4xuh2mn',
|
||||||
handle: new URL(appHost).hostname,
|
handle: 'syui.ai',
|
||||||
accessJwt: accessJwt,
|
accessJwt: accessJwt,
|
||||||
refreshJwt: refreshJwt
|
refreshJwt: refreshJwt
|
||||||
};
|
};
|
||||||
|
|
||||||
localStorage.setItem('atproto_session', JSON.stringify(session));
|
localStorage.setItem('atproto_session', JSON.stringify(session));
|
||||||
|
console.log('Manual tokens stored in localStorage for backward compatibility');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 後方互換性のための従来関数
|
// 後方互換性のための従来関数
|
||||||
saveSessionToStorage(session: AtprotoSession): void {
|
saveSessionToStorage(session: AtprotoSession): void {
|
||||||
|
console.warn('saveSessionToStorage is deprecated with BrowserOAuthClient');
|
||||||
localStorage.setItem('atproto_session', JSON.stringify(session));
|
localStorage.setItem('atproto_session', JSON.stringify(session));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -53,6 +53,7 @@ export class OAuthEndpointHandler {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to generate JWKS:', error);
|
||||||
return new Response(JSON.stringify({ error: 'Failed to generate JWKS' }), {
|
return new Response(JSON.stringify({ error: 'Failed to generate JWKS' }), {
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
@@ -61,6 +62,7 @@ export class OAuthEndpointHandler {
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// If URL parsing fails, pass through to original fetch
|
// If URL parsing fails, pass through to original fetch
|
||||||
|
console.debug('URL parsing failed, passing through:', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pass through all other requests
|
// Pass through all other requests
|
||||||
@@ -134,5 +136,6 @@ export function registerOAuthServiceWorker() {
|
|||||||
const blob = new Blob([swCode], { type: 'application/javascript' });
|
const blob = new Blob([swCode], { type: 'application/javascript' });
|
||||||
const swUrl = URL.createObjectURL(blob);
|
const swUrl = URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
navigator.serviceWorker.register(swUrl).catch(console.error);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -37,6 +37,7 @@ export class OAuthKeyManager {
|
|||||||
this.keyPair = await this.importKeyPair(keyData);
|
this.keyPair = await this.importKeyPair(keyData);
|
||||||
return this.keyPair;
|
return this.keyPair;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.warn('Failed to load stored key, generating new one:', error);
|
||||||
localStorage.removeItem('oauth_private_key');
|
localStorage.removeItem('oauth_private_key');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,6 +115,7 @@ export class OAuthKeyManager {
|
|||||||
const privateKey = await window.crypto.subtle.exportKey('jwk', keyPair.privateKey);
|
const privateKey = await window.crypto.subtle.exportKey('jwk', keyPair.privateKey);
|
||||||
localStorage.setItem('oauth_private_key', JSON.stringify(privateKey));
|
localStorage.setItem('oauth_private_key', JSON.stringify(privateKey));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Failed to store private key:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,293 +0,0 @@
|
|||||||
// PDS Detection and API URL mapping utilities
|
|
||||||
|
|
||||||
export interface NetworkConfig {
|
|
||||||
pdsApi: string;
|
|
||||||
plcApi: string;
|
|
||||||
bskyApi: string;
|
|
||||||
webUrl: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Detect PDS from handle
|
|
||||||
export function detectPdsFromHandle(handle: string): string {
|
|
||||||
if (handle.endsWith('.syu.is')) {
|
|
||||||
return 'syu.is';
|
|
||||||
}
|
|
||||||
if (handle.endsWith('.bsky.social') || handle.endsWith('.bsky.app')) {
|
|
||||||
return 'bsky.social';
|
|
||||||
}
|
|
||||||
// Default to Bluesky for unknown domains
|
|
||||||
return 'bsky.social';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map PDS endpoint to network configuration
|
|
||||||
export function getNetworkConfigFromPdsEndpoint(pdsEndpoint: string): NetworkConfig {
|
|
||||||
try {
|
|
||||||
const url = new URL(pdsEndpoint);
|
|
||||||
const hostname = url.hostname;
|
|
||||||
|
|
||||||
// Map based on actual PDS endpoint
|
|
||||||
if (hostname === 'syu.is') {
|
|
||||||
return {
|
|
||||||
pdsApi: 'https://syu.is', // PDS API (repo operations)
|
|
||||||
plcApi: 'https://plc.syu.is', // PLC directory
|
|
||||||
bskyApi: 'https://bsky.syu.is', // Bluesky API (getProfile, etc.)
|
|
||||||
webUrl: 'https://web.syu.is' // Web interface
|
|
||||||
};
|
|
||||||
} else if (hostname.includes('bsky.network') || hostname === 'bsky.social' || hostname.includes('host.bsky.network')) {
|
|
||||||
// All Bluesky infrastructure (including *.host.bsky.network)
|
|
||||||
return {
|
|
||||||
pdsApi: pdsEndpoint, // Use actual PDS endpoint (e.g., shiitake.us-east.host.bsky.network)
|
|
||||||
plcApi: 'https://plc.directory', // Standard PLC directory
|
|
||||||
bskyApi: 'https://public.api.bsky.app', // Bluesky public API (NOT PDS)
|
|
||||||
webUrl: 'https://bsky.app' // Bluesky web interface
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
// Unknown PDS, assume Bluesky-compatible but use PDS for repo operations
|
|
||||||
return {
|
|
||||||
pdsApi: pdsEndpoint, // Use actual PDS for repo ops
|
|
||||||
plcApi: 'https://plc.directory', // Default PLC
|
|
||||||
bskyApi: 'https://public.api.bsky.app', // Default to Bluesky API
|
|
||||||
webUrl: 'https://bsky.app' // Default web interface
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Fallback for invalid URLs
|
|
||||||
return {
|
|
||||||
pdsApi: 'https://bsky.social',
|
|
||||||
plcApi: 'https://plc.directory',
|
|
||||||
bskyApi: 'https://public.api.bsky.app',
|
|
||||||
webUrl: 'https://bsky.app'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Legacy function for backwards compatibility
|
|
||||||
export function getNetworkConfig(pds: string): NetworkConfig {
|
|
||||||
// This now assumes pds is a hostname
|
|
||||||
return getNetworkConfigFromPdsEndpoint(`https://${pds}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get appropriate API URL for a user based on their handle
|
|
||||||
export function getApiUrlForUser(handle: string): string {
|
|
||||||
const pds = detectPdsFromHandle(handle);
|
|
||||||
const config = getNetworkConfig(pds);
|
|
||||||
return config.bskyApi;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolve handle/DID to actual PDS endpoint using com.atproto.repo.describeRepo
|
|
||||||
export async function resolvePdsFromRepo(handleOrDid: string): Promise<{ pds: string; did: string; handle: string }> {
|
|
||||||
let targetDid = handleOrDid;
|
|
||||||
let targetHandle = handleOrDid;
|
|
||||||
|
|
||||||
// If handle provided, resolve to DID first using identity.resolveHandle
|
|
||||||
if (!handleOrDid.startsWith('did:')) {
|
|
||||||
try {
|
|
||||||
// Try multiple endpoints for handle resolution
|
|
||||||
const resolveEndpoints = ['https://public.api.bsky.app', 'https://bsky.syu.is'];
|
|
||||||
let resolved = false;
|
|
||||||
|
|
||||||
for (const endpoint of resolveEndpoints) {
|
|
||||||
try {
|
|
||||||
const resolveResponse = await fetch(`${endpoint}/xrpc/com.atproto.identity.resolveHandle?handle=${encodeURIComponent(handleOrDid)}`);
|
|
||||||
if (resolveResponse.ok) {
|
|
||||||
const resolveData = await resolveResponse.json();
|
|
||||||
targetDid = resolveData.did;
|
|
||||||
resolved = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!resolved) {
|
|
||||||
throw new Error('Handle resolution failed from all endpoints');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Failed to resolve handle ${handleOrDid} to DID: ${error}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now use com.atproto.repo.describeRepo to get PDS from known PDS endpoints
|
|
||||||
const pdsEndpoints = ['https://bsky.social', 'https://syu.is'];
|
|
||||||
|
|
||||||
for (const pdsEndpoint of pdsEndpoints) {
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${pdsEndpoint}/xrpc/com.atproto.repo.describeRepo?repo=${encodeURIComponent(targetDid)}`);
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
// Extract PDS from didDoc.service
|
|
||||||
const services = data.didDoc?.service || [];
|
|
||||||
const pdsService = services.find((s: any) =>
|
|
||||||
s.id === '#atproto_pds' || s.type === 'AtprotoPersonalDataServer'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pdsService) {
|
|
||||||
return {
|
|
||||||
pds: pdsService.serviceEndpoint,
|
|
||||||
did: data.did || targetDid,
|
|
||||||
handle: data.handle || targetHandle
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error(`Failed to resolve PDS for ${handleOrDid} from any endpoint`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolve DID to actual PDS endpoint using com.atproto.repo.describeRepo
|
|
||||||
export async function resolvePdsFromDid(did: string): Promise<string> {
|
|
||||||
const resolved = await resolvePdsFromRepo(did);
|
|
||||||
return resolved.pds;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enhanced resolve handle to DID with proper PDS detection
|
|
||||||
export async function resolveHandleToDid(handle: string): Promise<{ did: string; pds: string }> {
|
|
||||||
try {
|
|
||||||
// First, try to resolve the handle to DID using multiple methods
|
|
||||||
const apiUrl = getApiUrlForUser(handle);
|
|
||||||
const response = await fetch(`${apiUrl}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(handle)}`);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to resolve handle: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
const did = data.did;
|
|
||||||
|
|
||||||
// Now resolve the actual PDS from the DID
|
|
||||||
const actualPds = await resolvePdsFromDid(did);
|
|
||||||
|
|
||||||
return {
|
|
||||||
did: did,
|
|
||||||
pds: actualPds
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
// Failed to resolve handle
|
|
||||||
|
|
||||||
// Fallback to handle-based detection
|
|
||||||
const fallbackPds = detectPdsFromHandle(handle);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get profile using appropriate API for the user with accurate PDS resolution
|
|
||||||
export async function getProfileForUser(handleOrDid: string, knownPdsEndpoint?: string): Promise<any> {
|
|
||||||
try {
|
|
||||||
let apiUrl: string;
|
|
||||||
|
|
||||||
if (knownPdsEndpoint) {
|
|
||||||
// If we already know the user's PDS endpoint, use it directly
|
|
||||||
const config = getNetworkConfigFromPdsEndpoint(knownPdsEndpoint);
|
|
||||||
apiUrl = config.bskyApi;
|
|
||||||
} else {
|
|
||||||
// Resolve the user's actual PDS using describeRepo
|
|
||||||
try {
|
|
||||||
const resolved = await resolvePdsFromRepo(handleOrDid);
|
|
||||||
const config = getNetworkConfigFromPdsEndpoint(resolved.pds);
|
|
||||||
apiUrl = config.bskyApi;
|
|
||||||
} catch {
|
|
||||||
// Fallback to handle-based detection
|
|
||||||
apiUrl = getApiUrlForUser(handleOrDid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(`${apiUrl}/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(handleOrDid)}`);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Failed to get profile: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
} catch (error) {
|
|
||||||
// Failed to get profile
|
|
||||||
|
|
||||||
// Final fallback: try with default Bluesky API
|
|
||||||
try {
|
|
||||||
const response = await fetch(`https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=${encodeURIComponent(handleOrDid)}`);
|
|
||||||
if (response.ok) {
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Ignore fallback errors
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test and verify PDS detection methods
|
|
||||||
export async function verifyPdsDetection(handleOrDid: string): Promise<void> {
|
|
||||||
try {
|
|
||||||
// Method 1: com.atproto.repo.describeRepo (PRIMARY METHOD)
|
|
||||||
try {
|
|
||||||
const resolved = await resolvePdsFromRepo(handleOrDid);
|
|
||||||
const config = getNetworkConfigFromPdsEndpoint(resolved.pds);
|
|
||||||
} catch (error) {
|
|
||||||
// describeRepo failed
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method 2: com.atproto.identity.resolveHandle (for comparison)
|
|
||||||
if (!handleOrDid.startsWith('did:')) {
|
|
||||||
try {
|
|
||||||
const resolveResponse = await fetch(`https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle?handle=${encodeURIComponent(handleOrDid)}`);
|
|
||||||
if (resolveResponse.ok) {
|
|
||||||
const resolveData = await resolveResponse.json();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Error resolving handle
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method 3: PLC Directory lookup (if we have a DID)
|
|
||||||
let targetDid = handleOrDid;
|
|
||||||
if (!handleOrDid.startsWith('did:')) {
|
|
||||||
try {
|
|
||||||
const profile = await getProfileForUser(handleOrDid);
|
|
||||||
targetDid = profile.did;
|
|
||||||
} catch {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const plcResponse = await fetch(`https://plc.directory/${targetDid}`);
|
|
||||||
if (plcResponse.ok) {
|
|
||||||
const didDocument = await plcResponse.json();
|
|
||||||
|
|
||||||
// Find PDS service
|
|
||||||
const pdsService = didDocument.service?.find((s: any) =>
|
|
||||||
s.id === '#atproto_pds' || s.type === 'AtprotoPersonalDataServer'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pdsService) {
|
|
||||||
// Try to detect if this is a known network
|
|
||||||
const pdsUrl = pdsService.serviceEndpoint;
|
|
||||||
const hostname = new URL(pdsUrl).hostname;
|
|
||||||
const detectedNetwork = detectPdsFromHandle(`user.${hostname}`);
|
|
||||||
const networkConfig = getNetworkConfig(hostname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Error fetching from PLC directory
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method 4: Our enhanced resolution
|
|
||||||
try {
|
|
||||||
if (handleOrDid.startsWith('did:')) {
|
|
||||||
const pdsEndpoint = await resolvePdsFromDid(handleOrDid);
|
|
||||||
} else {
|
|
||||||
const resolved = await resolveHandleToDid(handleOrDid);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// Enhanced resolution failed
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
// Overall verification failed
|
|
||||||
}
|
|
||||||
}
|
|
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
function _env() {
|
function _env() {
|
||||||
d=${0:a:h}
|
d=${0:a:h}
|
||||||
ailog=$d/target/debug/ailog
|
ailog=$d/target/release/ailog
|
||||||
oauth=$d/oauth
|
oauth=$d/oauth
|
||||||
myblog=$d/my-blog
|
myblog=$d/my-blog
|
||||||
port=4173
|
port=4173
|
||||||
@@ -16,14 +16,10 @@ function _env() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
function _deploy_ailog() {
|
|
||||||
}
|
|
||||||
|
|
||||||
function _server() {
|
function _server() {
|
||||||
lsof -ti:$port | xargs kill -9 2>/dev/null || true
|
lsof -ti:$port | xargs kill -9 2>/dev/null || true
|
||||||
cd $d/my-blog
|
cd $d/my-blog
|
||||||
cargo build
|
cargo build --release
|
||||||
cp -rf $ailog $CARGO_HOME/bin/
|
|
||||||
$ailog build
|
$ailog build
|
||||||
$ailog serve --port $port
|
$ailog serve --port $port
|
||||||
}
|
}
|
||||||
@@ -44,8 +40,7 @@ function _oauth_build() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _server_comment() {
|
function _server_comment() {
|
||||||
cargo build
|
cargo build --release
|
||||||
cp -rf $ailog $CARGO_HOME/bin/
|
|
||||||
AILOG_DEBUG_ALL=1 $ailog stream start my-blog
|
AILOG_DEBUG_ALL=1 $ailog stream start my-blog
|
||||||
}
|
}
|
||||||
|
|
@@ -1,173 +0,0 @@
|
|||||||
#!/bin/zsh
|
|
||||||
|
|
||||||
# Generate AI content for blog posts
|
|
||||||
# Usage: ./bin/ailog-generate.zsh [md-file]
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Load configuration
|
|
||||||
f=~/.config/syui/ai/bot/token.json
|
|
||||||
|
|
||||||
# Default values
|
|
||||||
default_pds="bsky.social"
|
|
||||||
default_did=`cat $f|jq -r .did`
|
|
||||||
default_token=`cat $f|jq -r .accessJwt`
|
|
||||||
default_refresh=`cat $f|jq -r .refreshJwt`
|
|
||||||
|
|
||||||
# Refresh token if needed
|
|
||||||
curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $default_refresh" https://$default_pds/xrpc/com.atproto.server.refreshSession >! $f
|
|
||||||
default_token=`cat $f|jq -r .accessJwt`
|
|
||||||
|
|
||||||
# Set variables
|
|
||||||
admin_did=$default_did
|
|
||||||
admin_token=$default_token
|
|
||||||
ai_did="did:plc:4hqjfn7m6n5hno3doamuhgef"
|
|
||||||
ollama_host="https://ollama.syui.ai"
|
|
||||||
blog_host="https://syui.ai"
|
|
||||||
pds=$default_pds
|
|
||||||
|
|
||||||
# Parse arguments
|
|
||||||
md_file=$1
|
|
||||||
|
|
||||||
# Function to generate content using Ollama
|
|
||||||
generate_ai_content() {
|
|
||||||
local content=$1
|
|
||||||
local prompt_type=$2
|
|
||||||
local model="gemma3:4b"
|
|
||||||
|
|
||||||
case $prompt_type in
|
|
||||||
"translate")
|
|
||||||
prompt="Translate the following Japanese blog post to English. Keep the technical terms and code blocks intact:\n\n$content"
|
|
||||||
;;
|
|
||||||
"comment")
|
|
||||||
prompt="Read this blog post and provide an insightful comment about it. Focus on the key points and add your perspective:\n\n$content"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
response=$(curl -sL -X POST "$ollama_host/api/generate" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-d "{
|
|
||||||
\"model\": \"$model\",
|
|
||||||
\"prompt\": \"$prompt\",
|
|
||||||
\"stream\": false,
|
|
||||||
\"options\": {
|
|
||||||
\"temperature\": 0.9,
|
|
||||||
\"top_p\": 0.9,
|
|
||||||
\"num_predict\": 500
|
|
||||||
}
|
|
||||||
}")
|
|
||||||
|
|
||||||
echo "$response" | jq -r '.response'
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function to put record to ATProto
|
|
||||||
put_record() {
|
|
||||||
local collection=$1
|
|
||||||
local rkey=$2
|
|
||||||
local record=$3
|
|
||||||
|
|
||||||
curl -sL -X POST "https://$pds/xrpc/com.atproto.repo.putRecord" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-H "Authorization: Bearer $admin_token" \
|
|
||||||
-d "{
|
|
||||||
\"repo\": \"$admin_did\",
|
|
||||||
\"collection\": \"$collection\",
|
|
||||||
\"rkey\": \"$rkey\",
|
|
||||||
\"record\": $record
|
|
||||||
}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function to process a single markdown file
|
|
||||||
process_md_file() {
|
|
||||||
local md_path=$1
|
|
||||||
local filename=$(basename "$md_path" .md)
|
|
||||||
local content=$(cat "$md_path")
|
|
||||||
local post_url="$blog_host/posts/$filename"
|
|
||||||
local rkey=$filename
|
|
||||||
local now=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
|
|
||||||
|
|
||||||
echo "Processing: $md_path"
|
|
||||||
echo "Post URL: $post_url"
|
|
||||||
|
|
||||||
# Generate English translation
|
|
||||||
echo "Generating English translation..."
|
|
||||||
en_translation=$(generate_ai_content "$content" "translate")
|
|
||||||
|
|
||||||
if [ -n "$en_translation" ]; then
|
|
||||||
lang_record="{
|
|
||||||
\"\$type\": \"ai.syui.log.chat.lang\",
|
|
||||||
\"type\": \"en\",
|
|
||||||
\"body\": $(echo "$en_translation" | jq -Rs .),
|
|
||||||
\"url\": \"$post_url\",
|
|
||||||
\"createdAt\": \"$now\",
|
|
||||||
\"author\": {
|
|
||||||
\"did\": \"$ai_did\",
|
|
||||||
\"handle\": \"yui.syui.ai\",
|
|
||||||
\"displayName\": \"AI Translator\"
|
|
||||||
}
|
|
||||||
}"
|
|
||||||
|
|
||||||
echo "Saving translation to ATProto..."
|
|
||||||
put_record "ai.syui.log.chat.lang" "$rkey" "$lang_record"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Generate AI comment
|
|
||||||
echo "Generating AI comment..."
|
|
||||||
ai_comment=$(generate_ai_content "$content" "comment")
|
|
||||||
|
|
||||||
if [ -n "$ai_comment" ]; then
|
|
||||||
comment_record="{
|
|
||||||
\"\$type\": \"ai.syui.log.chat.comment\",
|
|
||||||
\"type\": \"push\",
|
|
||||||
\"body\": $(echo "$ai_comment" | jq -Rs .),
|
|
||||||
\"url\": \"$post_url\",
|
|
||||||
\"createdAt\": \"$now\",
|
|
||||||
\"author\": {
|
|
||||||
\"did\": \"$ai_did\",
|
|
||||||
\"handle\": \"yui.syui.ai\",
|
|
||||||
\"displayName\": \"AI Commenter\"
|
|
||||||
}
|
|
||||||
}"
|
|
||||||
|
|
||||||
echo "Saving comment to ATProto..."
|
|
||||||
put_record "ai.syui.log.chat.comment" "$rkey" "$comment_record"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Completed: $filename"
|
|
||||||
echo
|
|
||||||
}
|
|
||||||
|
|
||||||
# Main logic
|
|
||||||
if [ -n "$md_file" ]; then
|
|
||||||
# Process specific file
|
|
||||||
if [ -f "$md_file" ]; then
|
|
||||||
process_md_file "$md_file"
|
|
||||||
else
|
|
||||||
echo "Error: File not found: $md_file"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
# Process all new posts
|
|
||||||
echo "Checking for posts without AI content..."
|
|
||||||
|
|
||||||
# Get existing records
|
|
||||||
existing_langs=$(curl -sL "https://$pds/xrpc/com.atproto.repo.listRecords?repo=$admin_did&collection=ai.syui.log.chat.lang&limit=100" | jq -r '.records[]?.value.url' | sort | uniq)
|
|
||||||
|
|
||||||
# Process each markdown file
|
|
||||||
for md in my-blog/content/posts/*.md; do
|
|
||||||
if [ -f "$md" ]; then
|
|
||||||
filename=$(basename "$md" .md)
|
|
||||||
post_url="$blog_host/posts/$filename"
|
|
||||||
|
|
||||||
# Check if already processed
|
|
||||||
if echo "$existing_langs" | grep -q "$post_url"; then
|
|
||||||
echo "Skip (already processed): $filename"
|
|
||||||
else
|
|
||||||
process_md_file "$md"
|
|
||||||
sleep 2 # Rate limiting
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "All done!"
|
|
@@ -1,30 +0,0 @@
|
|||||||
#!/bin/zsh
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
cb=ai.syui.log
|
|
||||||
cl=( $cb.user )
|
|
||||||
f=~/.config/syui/ai/log/config.json
|
|
||||||
|
|
||||||
default_collection="ai.syui.log.chat.comment"
|
|
||||||
default_pds="syu.is"
|
|
||||||
default_did=`cat $f|jq -r .admin.did`
|
|
||||||
default_token=`cat $f|jq -r .admin.access_jwt`
|
|
||||||
default_refresh=`cat $f|jq -r .admin.refresh_jwt`
|
|
||||||
#curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $default_refresh" https://$default_pds/xrpc/com.atproto.server.refreshSession >! $f
|
|
||||||
#default_token=`cat $f|jq -r .admin.access_jwt`
|
|
||||||
collection=${1:-$default_collection}
|
|
||||||
pds=${2:-$default_pds}
|
|
||||||
did=${3:-$default_did}
|
|
||||||
token=${4:-$default_token}
|
|
||||||
req=com.atproto.repo.deleteRecord
|
|
||||||
url=https://$pds/xrpc/$req
|
|
||||||
for i in $cl; do
|
|
||||||
echo $i
|
|
||||||
rkeys=($(curl -sL "https://$default_pds/xrpc/com.atproto.repo.listRecords?repo=$did&collection=$i&limit=100"|jq -r ".records[]?.uri"|cut -d '/' -f 5))
|
|
||||||
for rkey in "${rkeys[@]}"; do
|
|
||||||
echo $rkey
|
|
||||||
json="{\"collection\":\"$i\", \"rkey\":\"$rkey\", \"repo\":\"$did\"}"
|
|
||||||
curl -sL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $token" -d "$json" $url
|
|
||||||
done
|
|
||||||
done
|
|
@@ -28,31 +28,8 @@ pub struct JetstreamConfig {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct CollectionConfig {
|
pub struct CollectionConfig {
|
||||||
pub base: String, // Base collection name like "ai.syui.log"
|
pub comment: String,
|
||||||
}
|
pub user: String,
|
||||||
|
|
||||||
impl CollectionConfig {
|
|
||||||
// Collection name builders
|
|
||||||
pub fn comment(&self) -> String {
|
|
||||||
self.base.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn user(&self) -> String {
|
|
||||||
format!("{}.user", self.base)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn chat(&self) -> String {
|
|
||||||
format!("{}.chat", self.base)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn chat_lang(&self) -> String {
|
|
||||||
format!("{}.chat.lang", self.base)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn chat_comment(&self) -> String {
|
|
||||||
format!("{}.chat.comment", self.base)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AuthConfig {
|
impl Default for AuthConfig {
|
||||||
@@ -70,7 +47,8 @@ impl Default for AuthConfig {
|
|||||||
collections: vec!["ai.syui.log".to_string()],
|
collections: vec!["ai.syui.log".to_string()],
|
||||||
},
|
},
|
||||||
collections: CollectionConfig {
|
collections: CollectionConfig {
|
||||||
base: "ai.syui.log".to_string(),
|
comment: "ai.syui.log".to_string(),
|
||||||
|
user: "ai.syui.log.user".to_string(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,125 +64,7 @@ fn get_config_path() -> Result<PathBuf> {
|
|||||||
Ok(config_dir.join("config.json"))
|
Ok(config_dir.join("config.json"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn init() -> Result<()> {
|
pub async fn init() -> Result<()> {
|
||||||
init_with_pds(None).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn init_with_options(
|
|
||||||
pds_override: Option<String>,
|
|
||||||
handle_override: Option<String>,
|
|
||||||
use_password: bool,
|
|
||||||
access_jwt_override: Option<String>,
|
|
||||||
refresh_jwt_override: Option<String>
|
|
||||||
) -> Result<()> {
|
|
||||||
println!("{}", "🔐 Initializing ATProto authentication...".cyan());
|
|
||||||
|
|
||||||
let config_path = get_config_path()?;
|
|
||||||
|
|
||||||
if config_path.exists() {
|
|
||||||
println!("{}", "⚠️ Configuration already exists. Use 'ailog auth logout' to reset.".yellow());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate options
|
|
||||||
if let (Some(_), Some(_)) = (&access_jwt_override, &refresh_jwt_override) {
|
|
||||||
if use_password {
|
|
||||||
println!("{}", "⚠️ Cannot use both --password and JWT tokens. Choose one method.".yellow());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
} else if access_jwt_override.is_some() || refresh_jwt_override.is_some() {
|
|
||||||
println!("{}", "❌ Both --access-jwt and --refresh-jwt must be provided together.".red());
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{}", "📋 Please provide your ATProto credentials:".cyan());
|
|
||||||
|
|
||||||
// Get handle
|
|
||||||
let handle = if let Some(h) = handle_override {
|
|
||||||
h
|
|
||||||
} else {
|
|
||||||
print!("Handle (e.g., your.handle.bsky.social): ");
|
|
||||||
std::io::Write::flush(&mut std::io::stdout())?;
|
|
||||||
let mut input = String::new();
|
|
||||||
std::io::stdin().read_line(&mut input)?;
|
|
||||||
input.trim().to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Determine PDS URL
|
|
||||||
let pds_url = if let Some(override_pds) = pds_override {
|
|
||||||
if override_pds.starts_with("http") {
|
|
||||||
override_pds
|
|
||||||
} else {
|
|
||||||
format!("https://{}", override_pds)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if handle.ends_with(".syu.is") {
|
|
||||||
"https://syu.is".to_string()
|
|
||||||
} else {
|
|
||||||
"https://bsky.social".to_string()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("{}", format!("🌐 Using PDS: {}", pds_url).cyan());
|
|
||||||
|
|
||||||
// Get credentials
|
|
||||||
let (access_jwt, refresh_jwt) = if let (Some(access), Some(refresh)) = (access_jwt_override, refresh_jwt_override) {
|
|
||||||
println!("{}", "🔑 Using provided JWT tokens".cyan());
|
|
||||||
(access, refresh)
|
|
||||||
} else if use_password {
|
|
||||||
println!("{}", "🔒 Using password authentication".cyan());
|
|
||||||
authenticate_with_password(&handle, &pds_url).await?
|
|
||||||
} else {
|
|
||||||
// Interactive JWT input (legacy behavior)
|
|
||||||
print!("Access JWT: ");
|
|
||||||
std::io::Write::flush(&mut std::io::stdout())?;
|
|
||||||
let mut access_jwt = String::new();
|
|
||||||
std::io::stdin().read_line(&mut access_jwt)?;
|
|
||||||
let access_jwt = access_jwt.trim().to_string();
|
|
||||||
|
|
||||||
print!("Refresh JWT: ");
|
|
||||||
std::io::Write::flush(&mut std::io::stdout())?;
|
|
||||||
let mut refresh_jwt = String::new();
|
|
||||||
std::io::stdin().read_line(&mut refresh_jwt)?;
|
|
||||||
let refresh_jwt = refresh_jwt.trim().to_string();
|
|
||||||
|
|
||||||
(access_jwt, refresh_jwt)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Resolve DID from handle
|
|
||||||
println!("{}", "🔍 Resolving DID from handle...".cyan());
|
|
||||||
let did = resolve_did_with_pds(&handle, &pds_url).await?;
|
|
||||||
|
|
||||||
// Create config
|
|
||||||
let config = AuthConfig {
|
|
||||||
admin: AdminConfig {
|
|
||||||
did: did.clone(),
|
|
||||||
handle: handle.clone(),
|
|
||||||
access_jwt,
|
|
||||||
refresh_jwt,
|
|
||||||
pds: pds_url,
|
|
||||||
},
|
|
||||||
jetstream: JetstreamConfig {
|
|
||||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
|
||||||
collections: vec!["ai.syui.log".to_string()],
|
|
||||||
},
|
|
||||||
collections: generate_collection_config(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Save config
|
|
||||||
let config_json = serde_json::to_string_pretty(&config)?;
|
|
||||||
fs::write(&config_path, config_json)?;
|
|
||||||
|
|
||||||
println!("{}", "✅ Authentication configured successfully!".green());
|
|
||||||
println!("📁 Config saved to: {}", config_path.display());
|
|
||||||
println!("👤 Authenticated as: {} ({})", handle, did);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub async fn init_with_pds(pds_override: Option<String>) -> Result<()> {
|
|
||||||
println!("{}", "🔐 Initializing ATProto authentication...".cyan());
|
println!("{}", "🔐 Initializing ATProto authentication...".cyan());
|
||||||
|
|
||||||
let config_path = get_config_path()?;
|
let config_path = get_config_path()?;
|
||||||
@@ -235,28 +95,9 @@ pub async fn init_with_pds(pds_override: Option<String>) -> Result<()> {
|
|||||||
std::io::stdin().read_line(&mut refresh_jwt)?;
|
std::io::stdin().read_line(&mut refresh_jwt)?;
|
||||||
let refresh_jwt = refresh_jwt.trim().to_string();
|
let refresh_jwt = refresh_jwt.trim().to_string();
|
||||||
|
|
||||||
// Determine PDS URL
|
|
||||||
let pds_url = if let Some(override_pds) = pds_override {
|
|
||||||
// Use provided PDS override
|
|
||||||
if override_pds.starts_with("http") {
|
|
||||||
override_pds
|
|
||||||
} else {
|
|
||||||
format!("https://{}", override_pds)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Auto-detect from handle suffix
|
|
||||||
if handle.ends_with(".syu.is") {
|
|
||||||
"https://syu.is".to_string()
|
|
||||||
} else {
|
|
||||||
"https://bsky.social".to_string()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("{}", format!("🌐 Using PDS: {}", pds_url).cyan());
|
|
||||||
|
|
||||||
// Resolve DID from handle
|
// Resolve DID from handle
|
||||||
println!("{}", "🔍 Resolving DID from handle...".cyan());
|
println!("{}", "🔍 Resolving DID from handle...".cyan());
|
||||||
let did = resolve_did_with_pds(&handle, &pds_url).await?;
|
let did = resolve_did(&handle).await?;
|
||||||
|
|
||||||
// Create config
|
// Create config
|
||||||
let config = AuthConfig {
|
let config = AuthConfig {
|
||||||
@@ -265,7 +106,11 @@ pub async fn init_with_pds(pds_override: Option<String>) -> Result<()> {
|
|||||||
handle: handle.clone(),
|
handle: handle.clone(),
|
||||||
access_jwt,
|
access_jwt,
|
||||||
refresh_jwt,
|
refresh_jwt,
|
||||||
pds: pds_url,
|
pds: if handle.ends_with(".syu.is") {
|
||||||
|
"https://syu.is".to_string()
|
||||||
|
} else {
|
||||||
|
"https://bsky.social".to_string()
|
||||||
|
},
|
||||||
},
|
},
|
||||||
jetstream: JetstreamConfig {
|
jetstream: JetstreamConfig {
|
||||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||||
@@ -285,19 +130,10 @@ pub async fn init_with_pds(pds_override: Option<String>) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
async fn resolve_did(handle: &str) -> Result<String> {
|
async fn resolve_did(handle: &str) -> Result<String> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
let url = format!("https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor={}",
|
||||||
// Use appropriate API based on handle domain
|
urlencoding::encode(handle));
|
||||||
let api_base = if handle.ends_with(".syu.is") {
|
|
||||||
"https://bsky.syu.is"
|
|
||||||
} else {
|
|
||||||
"https://public.api.bsky.app"
|
|
||||||
};
|
|
||||||
|
|
||||||
let url = format!("{}/xrpc/app.bsky.actor.getProfile?actor={}",
|
|
||||||
api_base, urlencoding::encode(handle));
|
|
||||||
|
|
||||||
let response = client.get(&url).send().await?;
|
let response = client.get(&url).send().await?;
|
||||||
|
|
||||||
@@ -312,93 +148,6 @@ async fn resolve_did(handle: &str) -> Result<String> {
|
|||||||
Ok(did.to_string())
|
Ok(did.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn resolve_did_with_pds(handle: &str, pds_url: &str) -> Result<String> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
// Try to use the PDS API first
|
|
||||||
let api_base = if pds_url.contains("syu.is") {
|
|
||||||
"https://bsky.syu.is"
|
|
||||||
} else if pds_url.contains("bsky.social") {
|
|
||||||
"https://public.api.bsky.app"
|
|
||||||
} else {
|
|
||||||
// For custom PDS, try to construct API URL
|
|
||||||
pds_url
|
|
||||||
};
|
|
||||||
|
|
||||||
let url = format!("{}/xrpc/app.bsky.actor.getProfile?actor={}",
|
|
||||||
api_base, urlencoding::encode(handle));
|
|
||||||
|
|
||||||
let response = client.get(&url).send().await?;
|
|
||||||
|
|
||||||
if !response.status().is_success() {
|
|
||||||
return Err(anyhow::anyhow!("Failed to resolve handle using PDS {}: {}", pds_url, response.status()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let profile: serde_json::Value = response.json().await?;
|
|
||||||
let did = profile["did"].as_str()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("DID not found in profile response"))?;
|
|
||||||
|
|
||||||
Ok(did.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn authenticate_with_password(handle: &str, pds_url: &str) -> Result<(String, String)> {
|
|
||||||
use std::io::{self, Write};
|
|
||||||
|
|
||||||
// Get password securely
|
|
||||||
print!("Password: ");
|
|
||||||
io::stdout().flush()?;
|
|
||||||
let password = rpassword::read_password()
|
|
||||||
.context("Failed to read password")?;
|
|
||||||
|
|
||||||
if password.is_empty() {
|
|
||||||
return Err(anyhow::anyhow!("Password cannot be empty"));
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{}", "🔐 Authenticating with ATProto server...".cyan());
|
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
let auth_url = format!("{}/xrpc/com.atproto.server.createSession", pds_url);
|
|
||||||
|
|
||||||
let auth_request = serde_json::json!({
|
|
||||||
"identifier": handle,
|
|
||||||
"password": password
|
|
||||||
});
|
|
||||||
|
|
||||||
let response = client
|
|
||||||
.post(&auth_url)
|
|
||||||
.header("Content-Type", "application/json")
|
|
||||||
.json(&auth_request)
|
|
||||||
.send()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if !response.status().is_success() {
|
|
||||||
let status = response.status();
|
|
||||||
let error_text = response.text().await.unwrap_or_default();
|
|
||||||
|
|
||||||
if status.as_u16() == 401 {
|
|
||||||
return Err(anyhow::anyhow!("Authentication failed: Invalid handle or password"));
|
|
||||||
} else if status.as_u16() == 400 {
|
|
||||||
return Err(anyhow::anyhow!("Authentication failed: Bad request (check handle format)"));
|
|
||||||
} else {
|
|
||||||
return Err(anyhow::anyhow!("Authentication failed: {} - {}", status, error_text));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let auth_response: serde_json::Value = response.json().await?;
|
|
||||||
|
|
||||||
let access_jwt = auth_response["accessJwt"].as_str()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("No access JWT in response"))?
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
let refresh_jwt = auth_response["refreshJwt"].as_str()
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("No refresh JWT in response"))?
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
println!("{}", "✅ Password authentication successful".green());
|
|
||||||
|
|
||||||
Ok((access_jwt, refresh_jwt))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn status() -> Result<()> {
|
pub async fn status() -> Result<()> {
|
||||||
let config_path = get_config_path()?;
|
let config_path = get_config_path()?;
|
||||||
|
|
||||||
@@ -421,17 +170,9 @@ pub async fn status() -> Result<()> {
|
|||||||
|
|
||||||
// Test API access
|
// Test API access
|
||||||
println!("\n{}", "🧪 Testing API access...".cyan());
|
println!("\n{}", "🧪 Testing API access...".cyan());
|
||||||
match test_api_access_with_auth(&config).await {
|
match test_api_access(&config).await {
|
||||||
Ok(_) => println!("{}", "✅ API access successful".green()),
|
Ok(_) => println!("{}", "✅ API access successful".green()),
|
||||||
Err(e) => {
|
Err(e) => println!("{}", format!("❌ API access failed: {}", e).red()),
|
||||||
println!("{}", format!("❌ Authenticated API access failed: {}", e).red());
|
|
||||||
// Fallback to public API test
|
|
||||||
println!("{}", "🔄 Trying public API access...".cyan());
|
|
||||||
match test_api_access(&config).await {
|
|
||||||
Ok(_) => println!("{}", "✅ Public API access successful".green()),
|
|
||||||
Err(e2) => println!("{}", format!("❌ Public API access also failed: {}", e2).red()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -439,16 +180,8 @@ pub async fn status() -> Result<()> {
|
|||||||
|
|
||||||
async fn test_api_access(config: &AuthConfig) -> Result<()> {
|
async fn test_api_access(config: &AuthConfig) -> Result<()> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
let url = format!("https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor={}",
|
||||||
// Use appropriate API based on handle domain
|
urlencoding::encode(&config.admin.handle));
|
||||||
let api_base = if config.admin.handle.ends_with(".syu.is") {
|
|
||||||
"https://bsky.syu.is"
|
|
||||||
} else {
|
|
||||||
"https://public.api.bsky.app"
|
|
||||||
};
|
|
||||||
|
|
||||||
let url = format!("{}/xrpc/app.bsky.actor.getProfile?actor={}",
|
|
||||||
api_base, urlencoding::encode(&config.admin.handle));
|
|
||||||
|
|
||||||
let response = client.get(&url).send().await?;
|
let response = client.get(&url).send().await?;
|
||||||
|
|
||||||
@@ -487,50 +220,11 @@ pub fn load_config() -> Result<AuthConfig> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config_json = fs::read_to_string(&config_path)?;
|
let config_json = fs::read_to_string(&config_path)?;
|
||||||
|
let mut config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||||
|
|
||||||
// Try to load as new format first, then migrate if needed
|
// Update collection configuration
|
||||||
match serde_json::from_str::<AuthConfig>(&config_json) {
|
|
||||||
Ok(mut config) => {
|
|
||||||
// Update collection configuration
|
|
||||||
update_config_collections(&mut config);
|
|
||||||
Ok(config)
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("{}", format!("Parse error: {}, attempting migration...", e).yellow());
|
|
||||||
// Try to migrate from old format
|
|
||||||
migrate_config_if_needed(&config_path, &config_json)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn migrate_config_if_needed(config_path: &std::path::Path, config_json: &str) -> Result<AuthConfig> {
|
|
||||||
// Try to parse as old format and migrate to new simple format
|
|
||||||
let mut old_config: serde_json::Value = serde_json::from_str(config_json)?;
|
|
||||||
|
|
||||||
// Migrate old collections structure to new base-only structure
|
|
||||||
if let Some(collections) = old_config.get_mut("collections") {
|
|
||||||
// Extract base collection name from comment field or use default
|
|
||||||
let base_collection = collections.get("comment")
|
|
||||||
.and_then(|v| v.as_str())
|
|
||||||
.unwrap_or("ai.syui.log")
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
// Replace entire collections structure with new format
|
|
||||||
old_config["collections"] = serde_json::json!({
|
|
||||||
"base": base_collection
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save migrated config
|
|
||||||
let migrated_config_json = serde_json::to_string_pretty(&old_config)?;
|
|
||||||
fs::write(config_path, migrated_config_json)?;
|
|
||||||
|
|
||||||
// Parse as new format
|
|
||||||
let mut config: AuthConfig = serde_json::from_value(old_config)?;
|
|
||||||
update_config_collections(&mut config);
|
update_config_collections(&mut config);
|
||||||
|
|
||||||
println!("{}", "✅ Configuration migrated to new simplified format".green());
|
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,7 +259,7 @@ async fn test_api_access_with_auth(config: &AuthConfig) -> Result<()> {
|
|||||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=1",
|
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection={}&limit=1",
|
||||||
config.admin.pds,
|
config.admin.pds,
|
||||||
urlencoding::encode(&config.admin.did),
|
urlencoding::encode(&config.admin.did),
|
||||||
urlencoding::encode(&config.collections.comment()));
|
urlencoding::encode(&config.collections.comment));
|
||||||
|
|
||||||
let response = client
|
let response = client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
@@ -617,14 +311,23 @@ fn save_config(config: &AuthConfig) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate collection config from environment
|
// Generate collection names from admin DID or environment
|
||||||
fn generate_collection_config() -> CollectionConfig {
|
fn generate_collection_config() -> CollectionConfig {
|
||||||
// Use VITE_OAUTH_COLLECTION for unified configuration
|
// Check environment variables first
|
||||||
let base = std::env::var("VITE_OAUTH_COLLECTION")
|
if let (Ok(comment), Ok(user)) = (
|
||||||
.unwrap_or_else(|_| "ai.syui.log".to_string());
|
std::env::var("AILOG_COLLECTION_COMMENT"),
|
||||||
|
std::env::var("AILOG_COLLECTION_USER")
|
||||||
|
) {
|
||||||
|
return CollectionConfig {
|
||||||
|
comment,
|
||||||
|
user,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default collections
|
||||||
CollectionConfig {
|
CollectionConfig {
|
||||||
base,
|
comment: "ai.syui.log".to_string(),
|
||||||
|
user: "ai.syui.log.user".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -632,5 +335,5 @@ fn generate_collection_config() -> CollectionConfig {
|
|||||||
pub fn update_config_collections(config: &mut AuthConfig) {
|
pub fn update_config_collections(config: &mut AuthConfig) {
|
||||||
config.collections = generate_collection_config();
|
config.collections = generate_collection_config();
|
||||||
// Also update jetstream collections to monitor the comment collection
|
// Also update jetstream collections to monitor the comment collection
|
||||||
config.jetstream.collections = vec![config.collections.comment()];
|
config.jetstream.collections = vec![config.collections.comment.clone()];
|
||||||
}
|
}
|
@@ -1,7 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::fs;
|
|
||||||
use crate::generator::Generator;
|
use crate::generator::Generator;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
@@ -11,12 +10,6 @@ pub async fn execute(path: PathBuf) -> Result<()> {
|
|||||||
// Load configuration
|
// Load configuration
|
||||||
let config = Config::load(&path)?;
|
let config = Config::load(&path)?;
|
||||||
|
|
||||||
// Generate OAuth .env.production if oauth directory exists
|
|
||||||
let oauth_dir = path.join("oauth");
|
|
||||||
if oauth_dir.exists() {
|
|
||||||
generate_oauth_env(&path, &config)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create generator
|
// Create generator
|
||||||
let generator = Generator::new(path, config)?;
|
let generator = Generator::new(path, config)?;
|
||||||
|
|
||||||
@@ -25,104 +18,5 @@ pub async fn execute(path: PathBuf) -> Result<()> {
|
|||||||
|
|
||||||
println!("{}", "Build completed successfully!".green().bold());
|
println!("{}", "Build completed successfully!".green().bold());
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_oauth_env(path: &PathBuf, config: &Config) -> Result<()> {
|
|
||||||
let oauth_dir = path.join("oauth");
|
|
||||||
let env_file = oauth_dir.join(".env.production");
|
|
||||||
|
|
||||||
// Extract configuration values
|
|
||||||
let base_url = &config.site.base_url;
|
|
||||||
let oauth_json = config.oauth.as_ref()
|
|
||||||
.and_then(|o| o.json.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("client-metadata.json");
|
|
||||||
let oauth_redirect = config.oauth.as_ref()
|
|
||||||
.and_then(|o| o.redirect.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("oauth/callback");
|
|
||||||
let admin_handle = config.oauth.as_ref()
|
|
||||||
.and_then(|o| o.admin.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("ai.syui.ai");
|
|
||||||
let ai_handle = config.ai.as_ref()
|
|
||||||
.and_then(|a| a.handle.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("ai.syui.ai");
|
|
||||||
let collection = config.oauth.as_ref()
|
|
||||||
.and_then(|o| o.collection.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("ai.syui.log");
|
|
||||||
let pds = config.oauth.as_ref()
|
|
||||||
.and_then(|o| o.pds.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("syu.is");
|
|
||||||
let handle_list = config.oauth.as_ref()
|
|
||||||
.and_then(|o| o.handle_list.as_ref())
|
|
||||||
.map(|list| format!("{:?}", list))
|
|
||||||
.unwrap_or_else(|| "[\"syui.syui.ai\",\"yui.syui.ai\",\"ai.syui.ai\"]".to_string());
|
|
||||||
|
|
||||||
// AI configuration
|
|
||||||
let ai_enabled = config.ai.as_ref().map(|a| a.enabled).unwrap_or(true);
|
|
||||||
let ai_ask_ai = config.ai.as_ref().and_then(|a| a.ask_ai).unwrap_or(true);
|
|
||||||
let ai_provider = config.ai.as_ref()
|
|
||||||
.and_then(|a| a.provider.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("ollama");
|
|
||||||
let ai_model = config.ai.as_ref()
|
|
||||||
.and_then(|a| a.model.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("gemma3:4b");
|
|
||||||
let ai_host = config.ai.as_ref()
|
|
||||||
.and_then(|a| a.host.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("https://ollama.syui.ai");
|
|
||||||
let ai_system_prompt = config.ai.as_ref()
|
|
||||||
.and_then(|a| a.system_prompt.as_ref())
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。");
|
|
||||||
|
|
||||||
let env_content = format!(
|
|
||||||
r#"# Production environment variables
|
|
||||||
VITE_APP_HOST={}
|
|
||||||
VITE_OAUTH_CLIENT_ID={}/{}
|
|
||||||
VITE_OAUTH_REDIRECT_URI={}/{}
|
|
||||||
|
|
||||||
# Handle-based Configuration (DIDs resolved at runtime)
|
|
||||||
VITE_ATPROTO_PDS={}
|
|
||||||
VITE_ADMIN_HANDLE={}
|
|
||||||
VITE_AI_HANDLE={}
|
|
||||||
VITE_OAUTH_COLLECTION={}
|
|
||||||
VITE_ATPROTO_WEB_URL=https://bsky.app
|
|
||||||
VITE_ATPROTO_HANDLE_LIST={}
|
|
||||||
|
|
||||||
# AI Configuration
|
|
||||||
VITE_AI_ENABLED={}
|
|
||||||
VITE_AI_ASK_AI={}
|
|
||||||
VITE_AI_PROVIDER={}
|
|
||||||
VITE_AI_MODEL={}
|
|
||||||
VITE_AI_HOST={}
|
|
||||||
VITE_AI_SYSTEM_PROMPT="{}"
|
|
||||||
"#,
|
|
||||||
base_url,
|
|
||||||
base_url, oauth_json,
|
|
||||||
base_url, oauth_redirect,
|
|
||||||
pds,
|
|
||||||
admin_handle,
|
|
||||||
ai_handle,
|
|
||||||
collection,
|
|
||||||
handle_list,
|
|
||||||
ai_enabled,
|
|
||||||
ai_ask_ai,
|
|
||||||
ai_provider,
|
|
||||||
ai_model,
|
|
||||||
ai_host,
|
|
||||||
ai_system_prompt
|
|
||||||
);
|
|
||||||
|
|
||||||
fs::write(&env_file, env_content)?;
|
|
||||||
println!(" {} oauth/.env.production", "Generated".cyan());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
@@ -37,23 +37,9 @@ highlight_code = true
|
|||||||
minify = false
|
minify = false
|
||||||
|
|
||||||
[ai]
|
[ai]
|
||||||
enabled = true
|
enabled = false
|
||||||
auto_translate = false
|
auto_translate = false
|
||||||
comment_moderation = false
|
comment_moderation = false
|
||||||
ask_ai = true
|
|
||||||
provider = "ollama"
|
|
||||||
model = "gemma3:4b"
|
|
||||||
host = "https://ollama.syui.ai"
|
|
||||||
system_prompt = "あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。"
|
|
||||||
handle = "ai.syui.ai"
|
|
||||||
|
|
||||||
[oauth]
|
|
||||||
json = "client-metadata.json"
|
|
||||||
redirect = "oauth/callback"
|
|
||||||
admin = "ai.syui.ai"
|
|
||||||
collection = "ai.syui.log"
|
|
||||||
pds = "syu.is"
|
|
||||||
handle_list = ["syui.syui.ai", "yui.syui.ai", "ai.syui.ai", "syui.syu.is", "ai.syu.is"]
|
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
fs::write(path.join("config.toml"), config_content)?;
|
fs::write(path.join("config.toml"), config_content)?;
|
||||||
|
@@ -3,8 +3,6 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use toml::Value;
|
use toml::Value;
|
||||||
use serde_json;
|
|
||||||
use reqwest;
|
|
||||||
|
|
||||||
pub async fn build(project_dir: PathBuf) -> Result<()> {
|
pub async fn build(project_dir: PathBuf) -> Result<()> {
|
||||||
println!("Building OAuth app for project: {}", project_dir.display());
|
println!("Building OAuth app for project: {}", project_dir.display());
|
||||||
@@ -43,102 +41,83 @@ pub async fn build(project_dir: PathBuf) -> Result<()> {
|
|||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("oauth/callback");
|
.unwrap_or("oauth/callback");
|
||||||
|
|
||||||
// Get admin handle instead of DID
|
let admin_did = oauth_config.get("admin")
|
||||||
let admin_handle = oauth_config.get("admin")
|
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.ok_or_else(|| anyhow::anyhow!("No admin handle found in [oauth] section"))?;
|
.ok_or_else(|| anyhow::anyhow!("No admin DID found in [oauth] section"))?;
|
||||||
|
|
||||||
let collection_base = oauth_config.get("collection")
|
let collection_comment = oauth_config.get("collection_comment")
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("ai.syui.log");
|
.unwrap_or("ai.syui.log");
|
||||||
|
|
||||||
// Get handle list for authentication restriction
|
let collection_user = oauth_config.get("collection_user")
|
||||||
let handle_list = oauth_config.get("handle_list")
|
|
||||||
.and_then(|v| v.as_array())
|
|
||||||
.map(|arr| arr.iter().filter_map(|v| v.as_str()).collect::<Vec<&str>>())
|
|
||||||
.unwrap_or_else(|| vec![]);
|
|
||||||
|
|
||||||
// Extract AI configuration from ai config if available
|
|
||||||
let ai_config = config.get("ai").and_then(|v| v.as_table());
|
|
||||||
// Get AI handle from config
|
|
||||||
let ai_handle = ai_config
|
|
||||||
.and_then(|ai_table| ai_table.get("ai_handle"))
|
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("yui.syui.ai");
|
.unwrap_or("ai.syui.log.user");
|
||||||
|
|
||||||
|
let collection_chat = oauth_config.get("collection_chat")
|
||||||
|
.and_then(|v| v.as_str())
|
||||||
|
.unwrap_or("ai.syui.log.chat");
|
||||||
|
|
||||||
|
// Extract AI config if present
|
||||||
|
let ai_config = config.get("ai")
|
||||||
|
.and_then(|v| v.as_table());
|
||||||
|
|
||||||
let ai_enabled = ai_config
|
let ai_enabled = ai_config
|
||||||
.and_then(|ai_table| ai_table.get("enabled"))
|
.and_then(|ai| ai.get("enabled"))
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(false);
|
||||||
|
|
||||||
let ai_ask_ai = ai_config
|
let ai_ask_ai = ai_config
|
||||||
.and_then(|ai_table| ai_table.get("ask_ai"))
|
.and_then(|ai| ai.get("ask_ai"))
|
||||||
.and_then(|v| v.as_bool())
|
.and_then(|v| v.as_bool())
|
||||||
.unwrap_or(true);
|
.unwrap_or(false);
|
||||||
|
|
||||||
let ai_provider = ai_config
|
let ai_provider = ai_config
|
||||||
.and_then(|ai_table| ai_table.get("provider"))
|
.and_then(|ai| ai.get("provider"))
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("ollama");
|
.unwrap_or("ollama");
|
||||||
|
|
||||||
let ai_model = ai_config
|
let ai_model = ai_config
|
||||||
.and_then(|ai_table| ai_table.get("model"))
|
.and_then(|ai| ai.get("model"))
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("gemma3:4b");
|
.unwrap_or("gemma2:2b");
|
||||||
|
|
||||||
let ai_host = ai_config
|
let ai_host = ai_config
|
||||||
.and_then(|ai_table| ai_table.get("host"))
|
.and_then(|ai| ai.get("host"))
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("https://ollama.syui.ai");
|
.unwrap_or("https://ollama.syui.ai");
|
||||||
|
|
||||||
let ai_system_prompt = ai_config
|
let ai_system_prompt = ai_config
|
||||||
.and_then(|ai_table| ai_table.get("system_prompt"))
|
.and_then(|ai| ai.get("system_prompt"))
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("あなたは6歳の女の子アイです。明るく元気で、ちょっとおっちょこちょい。自分のことは「アイ」って呼びます。白と金のワンピース姿。 相手のことが大好きで、ときどき甘えたり、照れたりします。 でも、宇宙とかAIとか、難しい話にもすっごく詳しいの。専門的なことも、がんばって子どもっぽく教えようとします。不思議な力を持っています。小さい物質のことをよく知っているようです。");
|
.unwrap_or("you are a helpful ai assistant");
|
||||||
|
|
||||||
// Determine network configuration based on PDS
|
let ai_did = ai_config
|
||||||
let pds = oauth_config.get("pds")
|
.and_then(|ai| ai.get("ai_did"))
|
||||||
.and_then(|v| v.as_str())
|
.and_then(|v| v.as_str())
|
||||||
.unwrap_or("bsky.social");
|
.unwrap_or("did:plc:4hqjfn7m6n5hno3doamuhgef");
|
||||||
|
|
||||||
let (bsky_api, _atproto_api, web_url) = match pds {
|
|
||||||
"syu.is" => (
|
|
||||||
"https://bsky.syu.is",
|
|
||||||
"https://syu.is",
|
|
||||||
"https://web.syu.is"
|
|
||||||
),
|
|
||||||
"bsky.social" | "bsky.app" => (
|
|
||||||
"https://public.api.bsky.app",
|
|
||||||
"https://bsky.social",
|
|
||||||
"https://bsky.app"
|
|
||||||
),
|
|
||||||
_ => (
|
|
||||||
"https://public.api.bsky.app",
|
|
||||||
"https://bsky.social",
|
|
||||||
"https://bsky.app"
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Resolve handles to DIDs using appropriate API
|
// Extract bsky_api from oauth config
|
||||||
println!("🔍 Resolving admin handle: {}", admin_handle);
|
let bsky_api = oauth_config.get("bsky_api")
|
||||||
let admin_did = resolve_handle_to_did(admin_handle, &bsky_api).await
|
.and_then(|v| v.as_str())
|
||||||
.with_context(|| format!("Failed to resolve admin handle: {}", admin_handle))?;
|
.unwrap_or("https://public.api.bsky.app");
|
||||||
|
|
||||||
println!("🔍 Resolving AI handle: {}", ai_handle);
|
|
||||||
let ai_did = resolve_handle_to_did(ai_handle, &bsky_api).await
|
|
||||||
.with_context(|| format!("Failed to resolve AI handle: {}", ai_handle))?;
|
|
||||||
|
|
||||||
println!("✅ Admin DID: {}", admin_did);
|
|
||||||
println!("✅ AI DID: {}", ai_did);
|
|
||||||
|
|
||||||
// 4. Create .env.production content with handle-based configuration
|
// 4. Create .env.production content
|
||||||
let env_content = format!(
|
let env_content = format!(
|
||||||
r#"# Production environment variables
|
r#"# Production environment variables
|
||||||
VITE_APP_HOST={}
|
VITE_APP_HOST={}
|
||||||
VITE_OAUTH_CLIENT_ID={}/{}
|
VITE_OAUTH_CLIENT_ID={}/{}
|
||||||
VITE_OAUTH_REDIRECT_URI={}/{}
|
VITE_OAUTH_REDIRECT_URI={}/{}
|
||||||
|
VITE_ADMIN_DID={}
|
||||||
|
|
||||||
# Handle-based Configuration (DIDs resolved at runtime)
|
# Collection names for OAuth app
|
||||||
VITE_ATPROTO_PDS={}
|
VITE_COLLECTION_COMMENT={}
|
||||||
VITE_ADMIN_HANDLE={}
|
VITE_COLLECTION_USER={}
|
||||||
VITE_AI_HANDLE={}
|
VITE_COLLECTION_CHAT={}
|
||||||
VITE_OAUTH_COLLECTION={}
|
|
||||||
VITE_ATPROTO_WEB_URL={}
|
# Collection names for ailog (backward compatibility)
|
||||||
VITE_ATPROTO_HANDLE_LIST={}
|
AILOG_COLLECTION_COMMENT={}
|
||||||
|
AILOG_COLLECTION_USER={}
|
||||||
|
AILOG_COLLECTION_CHAT={}
|
||||||
|
|
||||||
# AI Configuration
|
# AI Configuration
|
||||||
VITE_AI_ENABLED={}
|
VITE_AI_ENABLED={}
|
||||||
@@ -147,28 +126,29 @@ VITE_AI_PROVIDER={}
|
|||||||
VITE_AI_MODEL={}
|
VITE_AI_MODEL={}
|
||||||
VITE_AI_HOST={}
|
VITE_AI_HOST={}
|
||||||
VITE_AI_SYSTEM_PROMPT="{}"
|
VITE_AI_SYSTEM_PROMPT="{}"
|
||||||
|
VITE_AI_DID={}
|
||||||
|
|
||||||
# DIDs (resolved from handles - for backward compatibility)
|
# API Configuration
|
||||||
#VITE_ADMIN_DID={}
|
VITE_BSKY_PUBLIC_API={}
|
||||||
#VITE_AI_DID={}
|
|
||||||
"#,
|
"#,
|
||||||
base_url,
|
base_url,
|
||||||
base_url, client_id_path,
|
base_url, client_id_path,
|
||||||
base_url, redirect_path,
|
base_url, redirect_path,
|
||||||
pds,
|
admin_did,
|
||||||
admin_handle,
|
collection_comment,
|
||||||
ai_handle,
|
collection_user,
|
||||||
collection_base,
|
collection_chat,
|
||||||
web_url,
|
collection_comment,
|
||||||
format!("[{}]", handle_list.iter().map(|h| format!("\"{}\"", h)).collect::<Vec<_>>().join(",")),
|
collection_user,
|
||||||
|
collection_chat,
|
||||||
ai_enabled,
|
ai_enabled,
|
||||||
ai_ask_ai,
|
ai_ask_ai,
|
||||||
ai_provider,
|
ai_provider,
|
||||||
ai_model,
|
ai_model,
|
||||||
ai_host,
|
ai_host,
|
||||||
ai_system_prompt,
|
ai_system_prompt,
|
||||||
admin_did,
|
ai_did,
|
||||||
ai_did
|
bsky_api
|
||||||
);
|
);
|
||||||
|
|
||||||
// 5. Find oauth directory (relative to current working directory)
|
// 5. Find oauth directory (relative to current working directory)
|
||||||
@@ -279,60 +259,4 @@ fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
|
||||||
|
|
||||||
// Handle-to-DID resolution with proper PDS detection
|
|
||||||
async fn resolve_handle_to_did(handle: &str, _api_base: &str) -> Result<String> {
|
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
|
||||||
// First, try to resolve handle to DID using multiple endpoints
|
|
||||||
let bsky_endpoints = ["https://public.api.bsky.app", "https://bsky.syu.is"];
|
|
||||||
let mut resolved_did = None;
|
|
||||||
|
|
||||||
for endpoint in &bsky_endpoints {
|
|
||||||
let url = format!("{}/xrpc/app.bsky.actor.getProfile?actor={}",
|
|
||||||
endpoint, urlencoding::encode(handle));
|
|
||||||
|
|
||||||
if let Ok(response) = client.get(&url).send().await {
|
|
||||||
if response.status().is_success() {
|
|
||||||
if let Ok(profile) = response.json::<serde_json::Value>().await {
|
|
||||||
if let Some(did) = profile["did"].as_str() {
|
|
||||||
resolved_did = Some(did.to_string());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let did = resolved_did
|
|
||||||
.ok_or_else(|| anyhow::anyhow!("Failed to resolve handle '{}' from any endpoint", handle))?;
|
|
||||||
|
|
||||||
// Now verify the DID and get actual PDS using com.atproto.repo.describeRepo
|
|
||||||
let pds_endpoints = ["https://bsky.social", "https://syu.is"];
|
|
||||||
|
|
||||||
for pds in &pds_endpoints {
|
|
||||||
let describe_url = format!("{}/xrpc/com.atproto.repo.describeRepo?repo={}",
|
|
||||||
pds, urlencoding::encode(&did));
|
|
||||||
|
|
||||||
if let Ok(response) = client.get(&describe_url).send().await {
|
|
||||||
if response.status().is_success() {
|
|
||||||
if let Ok(data) = response.json::<serde_json::Value>().await {
|
|
||||||
if let Some(services) = data["didDoc"]["service"].as_array() {
|
|
||||||
if services.iter().any(|s|
|
|
||||||
s["id"] == "#atproto_pds" || s["type"] == "AtprotoPersonalDataServer"
|
|
||||||
) {
|
|
||||||
// DID is valid and has PDS service
|
|
||||||
println!("✅ Verified DID {} has PDS via {}", did, pds);
|
|
||||||
return Ok(did);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If PDS verification fails, still return the DID but warn
|
|
||||||
println!("⚠️ Could not verify PDS for DID {}, but proceeding...", did);
|
|
||||||
Ok(did)
|
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,6 @@ pub struct Config {
|
|||||||
pub site: SiteConfig,
|
pub site: SiteConfig,
|
||||||
pub build: BuildConfig,
|
pub build: BuildConfig,
|
||||||
pub ai: Option<AiConfig>,
|
pub ai: Option<AiConfig>,
|
||||||
pub oauth: Option<OAuthConfig>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
@@ -38,22 +37,10 @@ pub struct AiConfig {
|
|||||||
pub model: Option<String>,
|
pub model: Option<String>,
|
||||||
pub host: Option<String>,
|
pub host: Option<String>,
|
||||||
pub system_prompt: Option<String>,
|
pub system_prompt: Option<String>,
|
||||||
pub handle: Option<String>,
|
|
||||||
pub ai_did: Option<String>,
|
pub ai_did: Option<String>,
|
||||||
pub api_key: Option<String>,
|
pub api_key: Option<String>,
|
||||||
pub gpt_endpoint: Option<String>,
|
pub gpt_endpoint: Option<String>,
|
||||||
pub atproto_config: Option<AtprotoConfig>,
|
pub atproto_config: Option<AtprotoConfig>,
|
||||||
pub num_predict: Option<i32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
||||||
pub struct OAuthConfig {
|
|
||||||
pub json: Option<String>,
|
|
||||||
pub redirect: Option<String>,
|
|
||||||
pub admin: Option<String>,
|
|
||||||
pub collection: Option<String>,
|
|
||||||
pub pds: Option<String>,
|
|
||||||
pub handle_list: Option<Vec<String>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
@@ -172,14 +159,11 @@ impl Default for Config {
|
|||||||
model: Some("gemma3:4b".to_string()),
|
model: Some("gemma3:4b".to_string()),
|
||||||
host: None,
|
host: None,
|
||||||
system_prompt: Some("You are a helpful AI assistant trained on this blog's content.".to_string()),
|
system_prompt: Some("You are a helpful AI assistant trained on this blog's content.".to_string()),
|
||||||
handle: None,
|
|
||||||
ai_did: None,
|
ai_did: None,
|
||||||
api_key: None,
|
api_key: None,
|
||||||
gpt_endpoint: None,
|
gpt_endpoint: None,
|
||||||
atproto_config: None,
|
atproto_config: None,
|
||||||
num_predict: None,
|
|
||||||
}),
|
}),
|
||||||
oauth: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -71,9 +71,6 @@ impl Generator {
|
|||||||
// Generate index page
|
// Generate index page
|
||||||
self.generate_index(&posts).await?;
|
self.generate_index(&posts).await?;
|
||||||
|
|
||||||
// Generate JSON index for API access
|
|
||||||
self.generate_json_index(&posts).await?;
|
|
||||||
|
|
||||||
// Generate post pages
|
// Generate post pages
|
||||||
for post in &posts {
|
for post in &posts {
|
||||||
self.generate_post_page(post).await?;
|
self.generate_post_page(post).await?;
|
||||||
@@ -449,63 +446,6 @@ impl Generator {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_json_index(&self, posts: &[Post]) -> Result<()> {
|
|
||||||
let index_data: Vec<serde_json::Value> = posts.iter().map(|post| {
|
|
||||||
// Parse date for proper formatting
|
|
||||||
let parsed_date = chrono::NaiveDate::parse_from_str(&post.date, "%Y-%m-%d")
|
|
||||||
.unwrap_or_else(|_| chrono::Utc::now().naive_utc().date());
|
|
||||||
|
|
||||||
// Format to Hugo-style date format (Mon Jan 2, 2006)
|
|
||||||
let formatted_date = parsed_date.format("%a %b %-d, %Y").to_string();
|
|
||||||
|
|
||||||
// Create UTC datetime for utc_time field
|
|
||||||
let utc_datetime = parsed_date.and_hms_opt(0, 0, 0)
|
|
||||||
.unwrap_or_else(|| chrono::Utc::now().naive_utc());
|
|
||||||
let utc_time = format!("{}Z", utc_datetime.format("%Y-%m-%dT%H:%M:%S"));
|
|
||||||
|
|
||||||
// Extract plain text content from HTML
|
|
||||||
let contents = self.extract_plain_text(&post.content);
|
|
||||||
|
|
||||||
serde_json::json!({
|
|
||||||
"title": post.title,
|
|
||||||
"tags": post.tags,
|
|
||||||
"description": self.extract_excerpt(&post.content),
|
|
||||||
"categories": [],
|
|
||||||
"contents": contents,
|
|
||||||
"href": format!("{}{}", self.config.site.base_url.trim_end_matches('/'), post.url),
|
|
||||||
"utc_time": utc_time,
|
|
||||||
"formated_time": formatted_date
|
|
||||||
})
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
// Write JSON index to public directory
|
|
||||||
let output_path = self.base_path.join("public/index.json");
|
|
||||||
let json_content = serde_json::to_string_pretty(&index_data)?;
|
|
||||||
fs::write(output_path, json_content)?;
|
|
||||||
|
|
||||||
println!("{} JSON index with {} posts", "Generated".cyan(), posts.len());
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extract_plain_text(&self, html_content: &str) -> String {
|
|
||||||
// Remove HTML tags and extract plain text
|
|
||||||
let mut text = String::new();
|
|
||||||
let mut in_tag = false;
|
|
||||||
|
|
||||||
for ch in html_content.chars() {
|
|
||||||
match ch {
|
|
||||||
'<' => in_tag = true,
|
|
||||||
'>' => in_tag = false,
|
|
||||||
_ if !in_tag => text.push(ch),
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up whitespace
|
|
||||||
text.split_whitespace().collect::<Vec<_>>().join(" ")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize)]
|
#[derive(Debug, Clone, serde::Serialize)]
|
||||||
@@ -539,19 +479,4 @@ pub struct Translation {
|
|||||||
pub title: String,
|
pub title: String,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize)]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
struct BlogPost {
|
|
||||||
title: String,
|
|
||||||
url: String,
|
|
||||||
date: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize)]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
struct BlogIndex {
|
|
||||||
posts: Vec<BlogPost>,
|
|
||||||
}
|
|
||||||
|
|
61
src/main.rs
61
src/main.rs
@@ -18,14 +18,10 @@ mod mcp;
|
|||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "ailog")]
|
#[command(name = "ailog")]
|
||||||
#[command(about = "A static blog generator with AI features")]
|
#[command(about = "A static blog generator with AI features")]
|
||||||
#[command(disable_version_flag = true)]
|
#[command(version)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
/// Print version information
|
|
||||||
#[arg(short = 'V', long = "version")]
|
|
||||||
version: bool,
|
|
||||||
|
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: Option<Commands>,
|
command: Commands,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
@@ -102,23 +98,7 @@ enum Commands {
|
|||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum AuthCommands {
|
enum AuthCommands {
|
||||||
/// Initialize OAuth authentication
|
/// Initialize OAuth authentication
|
||||||
Init {
|
Init,
|
||||||
/// Specify PDS server (e.g., syu.is, bsky.social)
|
|
||||||
#[arg(long)]
|
|
||||||
pds: Option<String>,
|
|
||||||
/// Handle/username for authentication
|
|
||||||
#[arg(long)]
|
|
||||||
handle: Option<String>,
|
|
||||||
/// Use password authentication instead of JWT
|
|
||||||
#[arg(long)]
|
|
||||||
password: bool,
|
|
||||||
/// Access JWT token (alternative to password auth)
|
|
||||||
#[arg(long)]
|
|
||||||
access_jwt: Option<String>,
|
|
||||||
/// Refresh JWT token (required with access-jwt)
|
|
||||||
#[arg(long)]
|
|
||||||
refresh_jwt: Option<String>,
|
|
||||||
},
|
|
||||||
/// Show current authentication status
|
/// Show current authentication status
|
||||||
Status,
|
Status,
|
||||||
/// Logout and clear credentials
|
/// Logout and clear credentials
|
||||||
@@ -134,17 +114,6 @@ enum StreamCommands {
|
|||||||
/// Run as daemon
|
/// Run as daemon
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
daemon: bool,
|
daemon: bool,
|
||||||
/// Enable AI content generation
|
|
||||||
#[arg(long)]
|
|
||||||
ai_generate: bool,
|
|
||||||
},
|
|
||||||
/// Initialize user list for admin account
|
|
||||||
Init {
|
|
||||||
/// Path to the blog project directory
|
|
||||||
project_dir: Option<PathBuf>,
|
|
||||||
/// Handles to add to initial user list (comma-separated)
|
|
||||||
#[arg(long)]
|
|
||||||
handles: Option<String>,
|
|
||||||
},
|
},
|
||||||
/// Stop monitoring
|
/// Stop monitoring
|
||||||
Stop,
|
Stop,
|
||||||
@@ -166,19 +135,8 @@ enum OauthCommands {
|
|||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
// Handle version flag
|
|
||||||
if cli.version {
|
|
||||||
println!("{}", env!("CARGO_PKG_VERSION"));
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Require subcommand if no version flag
|
|
||||||
let command = cli.command.ok_or_else(|| {
|
|
||||||
anyhow::anyhow!("No subcommand provided. Use --help for usage information.")
|
|
||||||
})?;
|
|
||||||
|
|
||||||
match command {
|
match cli.command {
|
||||||
Commands::Init { path } => {
|
Commands::Init { path } => {
|
||||||
commands::init::execute(path).await?;
|
commands::init::execute(path).await?;
|
||||||
}
|
}
|
||||||
@@ -207,8 +165,8 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
Commands::Auth { command } => {
|
Commands::Auth { command } => {
|
||||||
match command {
|
match command {
|
||||||
AuthCommands::Init { pds, handle, password, access_jwt, refresh_jwt } => {
|
AuthCommands::Init => {
|
||||||
commands::auth::init_with_options(pds, handle, password, access_jwt, refresh_jwt).await?;
|
commands::auth::init().await?;
|
||||||
}
|
}
|
||||||
AuthCommands::Status => {
|
AuthCommands::Status => {
|
||||||
commands::auth::status().await?;
|
commands::auth::status().await?;
|
||||||
@@ -220,11 +178,8 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
Commands::Stream { command } => {
|
Commands::Stream { command } => {
|
||||||
match command {
|
match command {
|
||||||
StreamCommands::Start { project_dir, daemon, ai_generate } => {
|
StreamCommands::Start { project_dir, daemon } => {
|
||||||
commands::stream::start(project_dir, daemon, ai_generate).await?;
|
commands::stream::start(project_dir, daemon).await?;
|
||||||
}
|
|
||||||
StreamCommands::Init { project_dir, handles } => {
|
|
||||||
commands::stream::init_user_list(project_dir, handles).await?;
|
|
||||||
}
|
}
|
||||||
StreamCommands::Stop => {
|
StreamCommands::Stop => {
|
||||||
commands::stream::stop().await?;
|
commands::stream::stop().await?;
|
||||||
|
@@ -6,9 +6,9 @@ Wants=network.target
|
|||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
User=syui
|
User=syui
|
||||||
|
Group=syui
|
||||||
WorkingDirectory=/home/syui/git/log
|
WorkingDirectory=/home/syui/git/log
|
||||||
ExecStart=/home/syui/.cargo/bin/ailog stream start my-blog --ai-generate
|
ExecStart=/home/syui/.cargo/bin/ailog stream start my-blog
|
||||||
ExecStop=/home/syui/.cargo/bin/ailog stream stop
|
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=5
|
RestartSec=5
|
||||||
StandardOutput=journal
|
StandardOutput=journal
|
||||||
@@ -19,4 +19,4 @@ Environment=RUST_LOG=info
|
|||||||
Environment=AILOG_DEBUG_ALL=1
|
Environment=AILOG_DEBUG_ALL=1
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
103
templates/api.md
Normal file
103
templates/api.md
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
# API Documentation
|
||||||
|
|
||||||
|
## Public Functions
|
||||||
|
|
||||||
|
{{#each api.public_functions}}
|
||||||
|
### `{{this.name}}`
|
||||||
|
|
||||||
|
{{#if this.docs}}
|
||||||
|
{{this.docs}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
**Visibility:** `{{this.visibility}}`
|
||||||
|
{{#if this.is_async}}**Async:** Yes{{/if}}
|
||||||
|
|
||||||
|
{{#if this.parameters}}
|
||||||
|
**Parameters:**
|
||||||
|
{{#each this.parameters}}
|
||||||
|
- `{{this.name}}`: `{{this.param_type}}`{{#if this.is_mutable}} (mutable){{/if}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
{{#if this.return_type}}
|
||||||
|
**Returns:** `{{this.return_type}}`
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Public Structs
|
||||||
|
|
||||||
|
{{#each api.public_structs}}
|
||||||
|
### `{{this.name}}`
|
||||||
|
|
||||||
|
{{#if this.docs}}
|
||||||
|
{{this.docs}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
**Visibility:** `{{this.visibility}}`
|
||||||
|
|
||||||
|
{{#if this.fields}}
|
||||||
|
**Fields:**
|
||||||
|
{{#each this.fields}}
|
||||||
|
- `{{this.name}}`: `{{this.field_type}}` ({{this.visibility}})
|
||||||
|
{{#if this.docs}} - {{this.docs}}{{/if}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Public Enums
|
||||||
|
|
||||||
|
{{#each api.public_enums}}
|
||||||
|
### `{{this.name}}`
|
||||||
|
|
||||||
|
{{#if this.docs}}
|
||||||
|
{{this.docs}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
**Visibility:** `{{this.visibility}}`
|
||||||
|
|
||||||
|
{{#if this.variants}}
|
||||||
|
**Variants:**
|
||||||
|
{{#each this.variants}}
|
||||||
|
- `{{this.name}}`
|
||||||
|
{{#if this.docs}} - {{this.docs}}{{/if}}
|
||||||
|
{{#if this.fields}}
|
||||||
|
**Fields:**
|
||||||
|
{{#each this.fields}}
|
||||||
|
- `{{this.name}}`: `{{this.field_type}}`
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Public Traits
|
||||||
|
|
||||||
|
{{#each api.public_traits}}
|
||||||
|
### `{{this.name}}`
|
||||||
|
|
||||||
|
{{#if this.docs}}
|
||||||
|
{{this.docs}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
**Visibility:** `{{this.visibility}}`
|
||||||
|
|
||||||
|
{{#if this.methods}}
|
||||||
|
**Methods:**
|
||||||
|
{{#each this.methods}}
|
||||||
|
- `{{this.name}}({{#each this.parameters}}{{this.name}}: {{this.param_type}}{{#unless @last}}, {{/unless}}{{/each}}){{#if this.return_type}} -> {{this.return_type}}{{/if}}`
|
||||||
|
{{#if this.docs}} - {{this.docs}}{{/if}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
{{/each}}
|
19
templates/changelog.md
Normal file
19
templates/changelog.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## Recent Changes
|
||||||
|
|
||||||
|
{{#each commits}}
|
||||||
|
### {{this.date}}
|
||||||
|
|
||||||
|
**{{this.hash}}** by {{this.author}}
|
||||||
|
|
||||||
|
{{this.message}}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
- **Total Commits:** {{commits.length}}
|
||||||
|
- **Contributors:** {{#unique commits "author"}}{{this.author}}{{#unless @last}}, {{/unless}}{{/unique}}
|
76
templates/readme.md
Normal file
76
templates/readme.md
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
# {{project.name}}
|
||||||
|
|
||||||
|
{{#if project.description}}
|
||||||
|
{{project.description}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This project contains {{project.modules.length}} modules with a total of {{project.metrics.total_lines}} lines of code.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo install {{project.name}}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
{{project.name}} --help
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
{{#each project.dependencies}}
|
||||||
|
- `{{@key}}`: {{this}}
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
{{#each project.structure.directories}}
|
||||||
|
{{this.name}}/
|
||||||
|
{{/each}}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Documentation
|
||||||
|
|
||||||
|
{{#each project.modules}}
|
||||||
|
### {{this.name}}
|
||||||
|
|
||||||
|
{{#if this.docs}}
|
||||||
|
{{this.docs}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
{{#if this.functions}}
|
||||||
|
**Functions:** {{this.functions.length}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
{{#if this.structs}}
|
||||||
|
**Structs:** {{this.structs.length}}
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Metrics
|
||||||
|
|
||||||
|
- **Lines of Code:** {{project.metrics.total_lines}}
|
||||||
|
- **Total Files:** {{project.metrics.total_files}}
|
||||||
|
- **Test Files:** {{project.metrics.test_files}}
|
||||||
|
- **Dependencies:** {{project.metrics.dependency_count}}
|
||||||
|
- **Complexity Score:** {{project.metrics.complexity_score}}
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
{{#if project.license}}
|
||||||
|
{{project.license}}
|
||||||
|
{{else}}
|
||||||
|
MIT
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
|
||||||
|
{{#each project.authors}}
|
||||||
|
- {{this}}
|
||||||
|
{{/each}}
|
39
templates/structure.md
Normal file
39
templates/structure.md
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# Project Structure
|
||||||
|
|
||||||
|
## Directory Overview
|
||||||
|
|
||||||
|
```
|
||||||
|
{{#each structure.directories}}
|
||||||
|
{{this.name}}/
|
||||||
|
{{#each this.subdirectories}}
|
||||||
|
├── {{this}}/
|
||||||
|
{{/each}}
|
||||||
|
{{#if this.file_count}}
|
||||||
|
└── ({{this.file_count}} files)
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
```
|
||||||
|
|
||||||
|
## File Distribution
|
||||||
|
|
||||||
|
{{#each structure.files}}
|
||||||
|
- **{{this.name}}** ({{this.language}}) - {{this.lines_of_code}} lines{{#if this.is_test}} [TEST]{{/if}}
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
## Statistics
|
||||||
|
|
||||||
|
- **Total Directories:** {{structure.directories.length}}
|
||||||
|
- **Total Files:** {{structure.files.length}}
|
||||||
|
- **Languages Used:**
|
||||||
|
{{#group structure.files by="language"}}
|
||||||
|
- {{@key}}: {{this.length}} files
|
||||||
|
{{/group}}
|
||||||
|
|
||||||
|
{{#if structure.dependency_graph}}
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
{{#each structure.dependency_graph}}
|
||||||
|
- **{{@key}}** depends on: {{#each this}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
19
vercel.json
Normal file
19
vercel.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [
|
||||||
|
{
|
||||||
|
"src": "my-blog/public/**",
|
||||||
|
"use": "@vercel/static"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"routes": [
|
||||||
|
{
|
||||||
|
"src": "/api/ask",
|
||||||
|
"dest": "/api/ask.js"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "/(.*)",
|
||||||
|
"dest": "/my-blog/public/$1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
93
workers/ollama-proxy.js
Normal file
93
workers/ollama-proxy.js
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
// Cloudflare Worker for secure Ollama proxy
|
||||||
|
export default {
|
||||||
|
async fetch(request, env, ctx) {
|
||||||
|
// CORS preflight
|
||||||
|
if (request.method === 'OPTIONS') {
|
||||||
|
return new Response(null, {
|
||||||
|
headers: {
|
||||||
|
'Access-Control-Allow-Origin': 'https://log.syui.ai',
|
||||||
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||||
|
'Access-Control-Allow-Headers': 'Content-Type, X-User-Token',
|
||||||
|
'Access-Control-Max-Age': '86400',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify origin
|
||||||
|
const origin = request.headers.get('Origin');
|
||||||
|
const referer = request.headers.get('Referer');
|
||||||
|
|
||||||
|
// 許可されたオリジンのみ
|
||||||
|
const allowedOrigins = [
|
||||||
|
'https://log.syui.ai',
|
||||||
|
'https://log.pages.dev' // Cloudflare Pages preview
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!origin || !allowedOrigins.some(allowed => origin.startsWith(allowed))) {
|
||||||
|
return new Response('Forbidden', { status: 403 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// ユーザー認証トークン検証(オプション)
|
||||||
|
const userToken = request.headers.get('X-User-Token');
|
||||||
|
if (env.REQUIRE_AUTH && !userToken) {
|
||||||
|
return new Response('Unauthorized', { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// リクエストボディを取得
|
||||||
|
const body = await request.json();
|
||||||
|
|
||||||
|
// プロンプトサイズ制限
|
||||||
|
if (body.prompt && body.prompt.length > 1000) {
|
||||||
|
return new Response(JSON.stringify({
|
||||||
|
error: 'Prompt too long. Maximum 1000 characters.'
|
||||||
|
}), {
|
||||||
|
status: 400,
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// レート制限(CF Workers KV使用)
|
||||||
|
if (env.RATE_LIMITER) {
|
||||||
|
const clientIP = request.headers.get('CF-Connecting-IP');
|
||||||
|
const rateLimitKey = `rate:${clientIP}`;
|
||||||
|
const currentCount = await env.RATE_LIMITER.get(rateLimitKey) || 0;
|
||||||
|
|
||||||
|
if (currentCount >= 20) {
|
||||||
|
return new Response(JSON.stringify({
|
||||||
|
error: 'Rate limit exceeded. Try again later.'
|
||||||
|
}), {
|
||||||
|
status: 429,
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// カウント増加(1時間TTL)
|
||||||
|
await env.RATE_LIMITER.put(rateLimitKey, currentCount + 1, {
|
||||||
|
expirationTtl: 3600
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ollamaへプロキシ
|
||||||
|
const ollamaResponse = await fetch(env.OLLAMA_API_URL || 'https://ollama.syui.ai/api/generate', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
// 内部認証ヘッダー(必要に応じて)
|
||||||
|
'X-Internal-Token': env.OLLAMA_INTERNAL_TOKEN || ''
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
});
|
||||||
|
|
||||||
|
// レスポンスを返す
|
||||||
|
const responseData = await ollamaResponse.text();
|
||||||
|
|
||||||
|
return new Response(responseData, {
|
||||||
|
status: ollamaResponse.status,
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Access-Control-Allow-Origin': origin,
|
||||||
|
'Cache-Control': 'no-store'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
20
workers/wrangler.toml
Normal file
20
workers/wrangler.toml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
name = "ollama-proxy"
|
||||||
|
main = "ollama-proxy.js"
|
||||||
|
compatibility_date = "2024-01-01"
|
||||||
|
|
||||||
|
# 環境変数
|
||||||
|
[vars]
|
||||||
|
REQUIRE_AUTH = false
|
||||||
|
|
||||||
|
# 本番環境
|
||||||
|
[env.production.vars]
|
||||||
|
OLLAMA_API_URL = "https://ollama.syui.ai/api/generate"
|
||||||
|
REQUIRE_AUTH = true
|
||||||
|
|
||||||
|
# KVネームスペース(レート制限用)
|
||||||
|
[[kv_namespaces]]
|
||||||
|
binding = "RATE_LIMITER"
|
||||||
|
id = "your-kv-namespace-id"
|
||||||
|
|
||||||
|
# シークレット(wrangler secret putで設定)
|
||||||
|
# OLLAMA_INTERNAL_TOKEN = "your-internal-token"
|
31
wrangler.toml
Normal file
31
wrangler.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name = "ailog"
|
||||||
|
compatibility_date = "2024-01-01"
|
||||||
|
|
||||||
|
[env.production]
|
||||||
|
name = "ailog"
|
||||||
|
|
||||||
|
[build]
|
||||||
|
command = "cargo build --release && ./target/release/ailog build my-blog"
|
||||||
|
publish = "my-blog/public"
|
||||||
|
|
||||||
|
[[redirects]]
|
||||||
|
from = "/api/ask"
|
||||||
|
to = "https://ai-gpt-mcp.your-domain.com/ask"
|
||||||
|
status = 200
|
||||||
|
|
||||||
|
[[headers]]
|
||||||
|
for = "/*"
|
||||||
|
[headers.values]
|
||||||
|
X-Frame-Options = "DENY"
|
||||||
|
X-Content-Type-Options = "nosniff"
|
||||||
|
Referrer-Policy = "strict-origin-when-cross-origin"
|
||||||
|
|
||||||
|
[[headers]]
|
||||||
|
for = "/css/*"
|
||||||
|
[headers.values]
|
||||||
|
Cache-Control = "public, max-age=31536000, immutable"
|
||||||
|
|
||||||
|
[[headers]]
|
||||||
|
for = "*.js"
|
||||||
|
[headers.values]
|
||||||
|
Cache-Control = "public, max-age=31536000, immutable"
|
Reference in New Issue
Block a user