Add AI chat with skills management and model provider settings

- Model providers for OpenAI and Anthropic message formats (e.g. via
  Sub2API), with API keys encrypted at rest and never returned
- Skills tree with markdown editing, priorities, drafts, publishing and
  rollback; only published skills enter the chat prompt
- Chat page with tool calls over collected news and Binance USD-M
  klines, a persisted CONTEXT.md, reset, and stop-generation support
- Chat may only create skill drafts and update CONTEXT.md; publishing,
  deleting, moving and priority changes stay with the user
This commit is contained in:
eezd committed 2026-10-14 13:11:45 +08:00
1 parent f042f27013
commit 5db3ead883
34 files changed
+3196 -12

No files matched your search

@@ -0,0 +1,110 @@
"""ai chat and skills
Revision ID: 8349ae014ddd
Revises: d41b7c2e9a10
Create Date: 2026-10-14 04:43:32.644862
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = '8349ae014ddd'
down_revision: Union[str, Sequence[str], None] = 'd41b7c2e9a10'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('ai_providers',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=64), nullable=False),
sa.Column('protocol', sa.String(length=16), nullable=False),
sa.Column('base_url', sa.String(length=255), nullable=False),
sa.Column('api_key_encrypted', sa.LargeBinary(), nullable=False),
sa.Column('api_key_hint', sa.String(length=8), nullable=False),
sa.Column('model', sa.String(length=128), nullable=False),
sa.Column('max_tokens', sa.Integer(), nullable=False),
sa.Column('context_window', sa.Integer(), nullable=False),
sa.Column('temperature', sa.Float(), nullable=True),
sa.Column('is_default', sa.Boolean(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('chat_sessions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('ended_at', sa.DateTime(timezone=True), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('context_versions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('source', sa.String(length=8), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('skill_nodes',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('parent_id', sa.Integer(), nullable=True),
sa.Column('kind', sa.String(length=8), nullable=False),
sa.Column('name', sa.String(length=128), nullable=False),
sa.Column('sort_order', sa.Integer(), nullable=False),
sa.Column('priority', sa.Integer(), nullable=False),
sa.Column('enabled', sa.Boolean(), nullable=False),
sa.Column('published_version_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['parent_id'], ['skill_nodes.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('parent_id', 'name', postgresql_nulls_not_distinct=True)
)
op.create_table('chat_messages',
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('session_id', sa.Integer(), nullable=False),
sa.Column('role', sa.String(length=16), nullable=False),
sa.Column('content', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column('provider_id', sa.Integer(), nullable=True),
sa.Column('usage', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['provider_id'], ['ai_providers.id'], ondelete='SET NULL'),
sa.ForeignKeyConstraint(['session_id'], ['chat_sessions.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_chat_messages_session_id'), 'chat_messages', ['session_id'], unique=False)
op.create_table('skill_versions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('skill_id', sa.Integer(), nullable=False),
sa.Column('version_no', sa.Integer(), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('source', sa.String(length=8), nullable=False),
sa.Column('note', sa.Text(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['skill_id'], ['skill_nodes.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('skill_id', 'version_no')
)
# skill_nodes 与 skill_versions 互相引用,等两张表都建好再加这条外键
op.create_foreign_key('skill_nodes_published_version_id_fkey', 'skill_nodes', 'skill_versions', ['published_version_id'], ['id'])
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('skill_nodes_published_version_id_fkey', 'skill_nodes', type_='foreignkey')
op.drop_table('skill_versions')
op.drop_index(op.f('ix_chat_messages_session_id'), table_name='chat_messages')
op.drop_table('chat_messages')
op.drop_table('skill_nodes')
op.drop_table('context_versions')
op.drop_table('chat_sessions')
op.drop_table('ai_providers')
# ### end Alembic commands ###