File size: 29,394 Bytes
9727889
633f9ef
 
 
 
 
 
9727889
 
 
 
 
 
0661b76
9727889
 
 
0661b76
 
 
633f9ef
0661b76
 
633f9ef
0661b76
 
 
 
 
 
 
 
 
 
 
633f9ef
 
 
0661b76
633f9ef
0661b76
 
 
 
 
 
 
 
 
633f9ef
 
 
 
0661b76
 
633f9ef
0661b76
633f9ef
 
0661b76
 
 
 
 
 
 
 
 
633f9ef
0661b76
 
 
 
 
 
 
 
633f9ef
 
 
 
 
0661b76
633f9ef
 
0661b76
 
 
 
633f9ef
 
0661b76
 
 
 
 
 
 
 
 
 
 
633f9ef
 
 
 
 
0661b76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0661b76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633f9ef
0661b76
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0661b76
 
9727889
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9727889
0661b76
633f9ef
 
 
 
0661b76
 
 
 
 
 
 
 
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
0661b76
 
633f9ef
 
 
 
 
 
 
 
 
 
 
0661b76
9727889
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0661b76
 
 
 
 
 
 
 
633f9ef
 
0661b76
 
633f9ef
0661b76
70766d2
 
 
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0661b76
 
633f9ef
 
0661b76
 
633f9ef
 
0661b76
 
 
 
 
 
 
633f9ef
 
0661b76
fabbc69
0661b76
 
 
 
 
 
 
 
 
633f9ef
 
 
 
 
 
 
 
0661b76
 
 
 
633f9ef
0661b76
 
 
 
633f9ef
0661b76
 
 
 
 
633f9ef
 
 
 
 
0661b76
 
633f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0661b76
 
 
 
633f9ef
0661b76
 
 
 
 
 
 
 
633f9ef
 
0661b76
 
 
 
 
633f9ef
 
0661b76
 
 
633f9ef
0661b76
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import gradio as gr
import sys
import os

# プロジェクトルートをパスに追加
sys.path.append('/workspaces/fastapi_django_main_live')

from mysite.libs.utilities import chat_with_interpreter, completion, process_file,no_process_file
from interpreter import interpreter
import mysite.interpreter.interpreter_config  # インポートするだけで設定が適用されます
import duckdb
import psycopg2
from dataclasses import dataclass, field
from typing import List, Optional, Tuple
from mysite.interpreter.process import no_process_file,process_file,process_nofile
#from controllers.gra_04_database.rides import test_set_lide
import requests
import sqlite3
import os
from datetime import datetime
from controllers.gra_03_programfromdocs.system_automation import SystemAutomation

# データベース設定
DB_PATH = "/workspaces/fastapi_django_main_live/prompts.db"

def init_db():
    """プロンプトデータベースの初期化"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS prompts (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                title TEXT NOT NULL,
                github_url TEXT,
                repository_name TEXT,
                system_type TEXT DEFAULT 'general',
                content TEXT NOT NULL,
                execution_status TEXT DEFAULT 'pending',
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ''')
        
        # デフォルトプロンプトの追加(初回のみ)
        cursor.execute('SELECT COUNT(*) FROM prompts')
        if cursor.fetchone()[0] == 0:
            default_prompts = [
                ("社員プロフィールシステム", "", "", "web_system", "社員プロフィール管理システム\n- ユーザー登録\n- プロフィール編集\n- 検索機能\n- 管理機能"),
                ("FastAPI + SQLAlchemy", "", "", "api_system", "FastAPIとSQLAlchemyを使用したAPIの作成\n- ユーザー管理\n- 認証機能\n- CRUD操作"),
                ("Gradio Interface", "", "", "interface_system", "Gradioインターフェースの作成\n- ファイルアップロード\n- チャット機能\n- データ表示"),
                ("LINE画像検索システム", "", "", "line_system", "LINEからの画像を検索するシステム\n- doPost受信\n- 画像保存\n- S3アップロード\n- シークレット管理"),
            ]
            
            for title, github_url, repo_name, system_type, content in default_prompts:
                cursor.execute(
                    'INSERT INTO prompts (title, github_url, repository_name, system_type, content) VALUES (?, ?, ?, ?, ?)',
                    (title, github_url, repo_name, system_type, content)
                )
        
        conn.commit()
        conn.close()
        print("✅ プロンプトデータベース初期化完了")
        
    except Exception as e:
        print(f"❌ データベース初期化エラー: {e}")

def save_prompt(title: str, content: str, github_url: str = "", system_type: str = "general") -> str:
    """プロンプトを保存"""
    try:
        if not title.strip() or not content.strip():
            return "❌ タイトルと内容は必須です"
        
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        # GitHubURLからリポジトリ名を抽出
        repo_name = ""
        if github_url:
            repo_name = github_url.split('/')[-1].replace('.git', '') if github_url.endswith('.git') else github_url.split('/')[-1]
        
        cursor.execute(
            'INSERT INTO prompts (title, github_url, repository_name, system_type, content) VALUES (?, ?, ?, ?, ?)',
            (title.strip(), github_url.strip(), repo_name, system_type, content.strip())
        )
        
        conn.commit()
        conn.close()
        print(f"✅ プロンプト保存: {title} (GitHub: {github_url})")
        return f"✅ プロンプト「{title}」を保存しました\n📁 リポジトリ: {repo_name}"
        
    except Exception as e:
        print(f"❌ プロンプト保存エラー: {e}")
        return f"❌ 保存エラー: {e}"

def get_prompts() -> List[Tuple]:
    """全プロンプトを取得"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            SELECT id, title, system_type, repository_name, execution_status, created_at 
            FROM prompts 
            ORDER BY created_at DESC
        ''')
        prompts = cursor.fetchall()
        
        conn.close()
        print(f"✅ プロンプト取得: {len(prompts)}件")
        return prompts
    except Exception as e:
        print(f"❌ プロンプト取得エラー: {e}")
        return []

def get_prompt_content(prompt_id: int) -> str:
    """指定IDのプロンプト内容を取得"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('SELECT content FROM prompts WHERE id = ?', (prompt_id,))
        result = cursor.fetchone()
        
        conn.close()
        
        if result:
            print(f"✅ プロンプト内容取得: ID {prompt_id}")
            return result[0]
        else:
            print(f"❌ プロンプトが見つかりません: ID {prompt_id}")
            return ""
            
    except Exception as e:
        print(f"❌ プロンプト内容取得エラー: {e}")
        return ""

def get_prompt_details(prompt_id: int) -> Tuple[str, str, str, str]:
    """指定IDのプロンプト詳細を取得"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('''
            SELECT content, github_url, system_type, repository_name 
            FROM prompts WHERE id = ?
        ''', (prompt_id,))
        result = cursor.fetchone()
        
        conn.close()
        
        if result:
            return result
        else:
            return "", "", "", ""
            
    except Exception as e:
        print(f"❌ プロンプト詳細取得エラー: {e}")
        return "", "", "", ""

def update_execution_status(prompt_id: int, status: str) -> None:
    """実行ステータスを更新"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute(
            'UPDATE prompts SET execution_status = ?, updated_at = ? WHERE id = ?',
            (status, datetime.now().isoformat(), prompt_id)
        )
        
        conn.commit()
        conn.close()
        print(f"✅ ステータス更新: ID {prompt_id} -> {status}")
        
    except Exception as e:
        print(f"❌ ステータス更新エラー: {e}")

def delete_prompt(prompt_id: int) -> str:
    """プロンプトを削除"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        cursor.execute('DELETE FROM prompts WHERE id = ?', (prompt_id,))
        
        if cursor.rowcount > 0:
            conn.commit()
            conn.close()
            print(f"✅ プロンプト削除: ID {prompt_id}")
            return f"✅ プロンプト ID {prompt_id} を削除しました"
        else:
            conn.close()
            return f"❌ プロンプト ID {prompt_id} が見つかりません"
            
    except Exception as e:
        print(f"❌ プロンプト削除エラー: {e}")
        return f"❌ 削除エラー: {e}"

def update_prompt_display():
    """プロンプト一覧の表示を更新"""
    prompts = get_prompts()
    if prompts:
        # テーブル形式でデータを準備
        table_data = []
        for prompt_id, title, system_type, repo_name, status, created_at in prompts:
            # 日時の表示を短くする
            date_str = created_at[:16] if created_at else ""
            # システムタイプのアイコンを追加
            type_icon = {
                'web_system': '🌐',
                'api_system': '🔗',
                'interface_system': '🖥️',
                'line_system': '📱',
                'general': '📄'
            }.get(system_type, '📄')
            
            # ステータスのアイコンを追加
            status_icon = {
                'pending': '⏳',
                'running': '🚀',
                'completed': '✅',
                'failed': '❌'
            }.get(status, '⏳')
            
            table_data.append([
                prompt_id, 
                f"{type_icon} {title}", 
                repo_name or "未設定",
                f"{status_icon} {status}",
                date_str
            ])
        return table_data
    return []

val = """
# 社員がプロフィールを登録・公開し、お互いに参照できるシステム

## 機能

## LINEのクレーム対応システムの作成
- クレームがあった用語をAPIでナレッジに登録するシステム
- APIキー agentキーをいれ
- 否定語に対する 文言に隊しての設定をする

### ユーザー登録

- ユーザー登録画面で、ユーザー名とパスワードを入力して登録ボタンを押すことにより、新規ユーザーを登録することができる。
- ユーザー名は、既存のユーザーと重複してはいけない。
- ユーザー登録に成功したら、ログイン済み状態として、ユーザー一覧画面へ遷移する。

### ログイン

- ログイン画面で、ユーザー名とパスワードを入力してログインボタンを押すことにより、ログインすることができる。
- ログインに成功したら、ユーザー一覧画面へ遷移する。

### チーム一覧・作成

- チームの一覧が、チームの作成日時降順で表示される。
- チーム名を入力して作成ボタンを押すと、チームが作成される。
- チームの作成後、本画面が再表示される。

### プロフィール編集

- 自身の`所属チーム`・`プロフィール`・`タグ`を編集できる。
- 所属チームは、既存チームからの選択式とする。
- プロフィールは自由入力とする。
- タグは自由入力で、複数入力できるようにする。

### ユーザー一覧・検索

- デフォルトでは全てのユーザーが一覧表示される。
- 検索条件を入力して検索ボタンを押すと、検索条件がプロフィールに部分一致するユーザーのみにフィルタリングできる。
- 一覧は、ユーザー登録日時の降順で表示される。
- 表示内容は、`ユーザー名`・`プロフィール`で、`プロフィール`は先頭10文字と三点リーダーを表示する。
- ユーザー名をクリックすると、そのユーザーのユーザー詳細画面へ遷移する。
- `チーム一覧へ`をクリックすると、チーム一覧画面へ遷移する。

### ユーザー詳細画面

- 特定のユーザーの、`ユーザー名`・`所属チーム`・`プロフィール`・`タグ`が表示される。
- プロフィールの表示はマークダウンに対応させる。
- `一覧へ`リンクをクリックすると、ユーザー一覧画面へ遷移する。

## あなたが作成するもの

バックエンドのプログラム一式を作成してください。
フロントエンドのプログラムは不要です。

- `/api`ディレクトリ以下に作成。
- Python/FastAPI/SQLAlchemyを使う。
- DBはSQLiteを使う。
- 必要に応じて外部ライブラリを使う。
- クラウドや外部サービス(外部API)は使わない。
- .gitignoreを含めること。
- バックエンド
@app.post("
def lumbda_function():

gradio_interface でメイン関数から読み込めるようにして

googleappsscript
ラインの画像検索システム

ファイルは1ファイルで作成して。
1ファイル1機能で難しくしたくない

1,lineからデータがくる
2,doPostで取得
3.typeがイメージの場合はドライブに保存
4,保存したデータをS3にアップロード
5.データはシークレットから取得
6,plantumlでフローの作成
7,システムドキュメントの作成

gradio は gradio_interface というBlock名で作成
fastapiはrouter の作成

"""

def send_to_google_chat(message: str):
    webhook_url = 'https://chat.googleapis.com/v1/spaces/AAAANwDF_KE/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=qSigSPSbTINJITgO30iGKnyeY48emcUJd9LST7FBLLY'
    headers = {'Content-Type': 'application/json; charset=UTF-8'}
    data = {'text': message}
    response = requests.post(webhook_url, headers=headers, json=data)
    response.raise_for_status()

def process_file_and_notify(*args, **kwargs):
    # 実行前にステータスを更新
    try:
        prompt_content = args[0] if args else ""
        if prompt_content.strip():
            # プロンプトIDを検索(完全一致で)
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute('SELECT id FROM prompts WHERE content = ?', (prompt_content,))
            result = cursor.fetchone()
            if result:
                update_execution_status(result[0], 'running')
            conn.close()
    except Exception as e:
        print(f"実行前ステータス更新エラー: {e}")
    
    # プロンプトを実行
    result = process_nofile(*args, **kwargs)
    
    # Google Chatに通知
    send_to_google_chat(f"🚀 システム生成完了\n```\n{result[:500]}...\n```")
    
    # プロンプト実行後、内容をデータベースに保存・更新
    try:
        prompt_content = args[0] if args else ""
        if prompt_content.strip():
            # 実行されたプロンプトのタイトルを生成(最初の行または最初の50文字)
            title_lines = prompt_content.strip().split('\n')
            title = title_lines[0][:50] if title_lines[0] else "実行されたプロンプト"
            if title.startswith('#'):
                title = title[1:].strip()
            
            # 既存のプロンプトか確認
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute('SELECT id FROM prompts WHERE content = ?', (prompt_content,))
            existing = cursor.fetchone()
            
            if existing:
                # 既存プロンプトのステータスを更新
                update_execution_status(existing[0], 'completed')
            else:
                # 新しい実行履歴として保存
                save_prompt(f"実行履歴: {title}", prompt_content, "", "execution_log")
            
            conn.close()
    except Exception as e:
        print(f"実行履歴保存エラー: {e}")
        # エラー時はステータスを失敗に更新
        try:
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute('SELECT id FROM prompts WHERE content = ?', (prompt_content,))
            result = cursor.fetchone()
            if result:
                update_execution_status(result[0], 'failed')
            conn.close()
        except:
            pass
    
    return result

def process_file_and_notify_enhanced(*args, **kwargs):
    """拡張版: プロンプト実行 + 自動GitHub連携"""
    # 実行前にステータスを更新
    try:
        prompt_content = args[0] if args else ""
        folder_name = args[1] if len(args) > 1 else "generated_systems"
        github_token = args[2] if len(args) > 2 else ""
        
        if prompt_content.strip():
            # プロンプトIDを検索(完全一致で)
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute('SELECT id FROM prompts WHERE content = ?', (prompt_content,))
            result = cursor.fetchone()
            if result:
                update_execution_status(result[0], 'running')
            conn.close()
    except Exception as e:
        print(f"実行前ステータス更新エラー: {e}")
    
    # プロンプトを実行
    result = process_nofile(*args, **kwargs)
    
    # 自動化パイプラインを実行
    enhanced_result = result
    if github_token and len(github_token) > 10:  # GitHub tokenが設定されている場合
        try:
            automation = SystemAutomation(github_token)
            
            # リポジトリ名を生成
            title_lines = prompt_content.strip().split('\n')
            repo_name = title_lines[0][:30] if title_lines[0] else "generated-system"
            repo_name = repo_name.replace('#', '').strip().replace(' ', '-').lower()
            
            # 生成されたフォルダのパス
            generated_folder = f"/workspaces/fastapi_django_main_live/{folder_name}"
            
            # 自動化パイプライン実行
            automation_result = automation.full_automation_pipeline(
                generated_folder,
                repo_name,
                f"GPT-ENGINEERで生成されたシステム: {repo_name}"
            )
            
            if automation_result['success']:
                enhanced_result += f"\n\n🚀 自動化完了!\n"
                enhanced_result += f"📁 GitHub: {automation_result['github_repo']['url']}\n"
                enhanced_result += f"🔧 統合されたController: {len(automation_result.get('controllers_found', []))}件"
                
                # Google Chatに詳細通知
                send_to_google_chat(f"""🎉 システム自動生成・統合完了!
                
📊 **生成システム**: {repo_name}
🔗 **GitHub**: {automation_result['github_repo']['url']}
🔧 **Controller統合**: {len(automation_result.get('controllers_found', []))}
📱 **ステータス**: 運用準備完了
""")
            else:
                enhanced_result += f"\n\n⚠️ 自動化エラー: {automation_result.get('error', '不明')}"
                
        except Exception as e:
            enhanced_result += f"\n\n❌ 自動化エラー: {str(e)}"
    else:
        # 従来の通知
        send_to_google_chat(f"🚀 システム生成完了\n```\n{result[:500]}...\n```")
    
    # プロンプト実行後、内容をデータベースに保存・更新
    try:
        prompt_content = args[0] if args else ""
        if prompt_content.strip():
            # 実行されたプロンプトのタイトルを生成(最初の行または最初の50文字)
            title_lines = prompt_content.strip().split('\n')
            title = title_lines[0][:50] if title_lines[0] else "実行されたプロンプト"
            if title.startswith('#'):
                title = title[1:].strip()
            
            # 既存のプロンプトか確認
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute('SELECT id FROM prompts WHERE content = ?', (prompt_content,))
            existing = cursor.fetchone()
            
            if existing:
                # 既存プロンプトのステータスを更新
                update_execution_status(existing[0], 'completed')
            else:
                # 新しい実行履歴として保存
                save_prompt(f"実行履歴: {title}", prompt_content, "", "execution_log")
            
            conn.close()
    except Exception as e:
        print(f"実行履歴保存エラー: {e}")
        # エラー時はステータスを失敗に更新
        try:
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute('SELECT id FROM prompts WHERE content = ?', (prompt_content,))
            result = cursor.fetchone()
            if result:
                update_execution_status(result[0], 'failed')
            conn.close()
        except:
            pass
    
    return enhanced_result

# ...existing code...

def load_prompt_to_textbox(evt: gr.SelectData):
    """テーブルクリック時にプロンプト内容をテキストボックスに読み込む"""
    try:
        if evt.index is not None and len(evt.index) >= 2:
            # テーブルの行インデックスから prompt_id を取得
            prompts = get_prompts()
            if evt.index[0] < len(prompts):
                prompt_id = prompts[evt.index[0]][0]  # 最初の列がID
                content, github_url, system_type, repo_name = get_prompt_details(prompt_id)
                return content, github_url, system_type
    except Exception as e:
        print(f"プロンプト読み込みエラー: {e}")
    return "", "", "general"

# 自動検出システム用のメタデータ
interface_title = "💾 プロンプト管理システム"
interface_description = "SQLite3ベースのプロンプト管理とコード生成"

# AI用の高度なプロンプトテンプレート
ai_system_prompts = {
    "microservice_api": """
# 高性能マイクロサービスAPI設計

## 要件
- FastAPI + SQLAlchemy + Alembic
- JWT認証、RBAC権限管理
- OpenAPI仕様書自動生成
- Redis キャッシュ、Celery非同期処理
- Docker コンテナ化
- CI/CD パイプライン(GitHub Actions)
- 監視・ログ・メトリクス(Prometheus + Grafana)

## アーキテクチャ
- Clean Architecture パターン
- Repository パターン
- 依存性注入(DI)
- イベント駆動設計

## セキュリティ
- OWASP準拠
- SQL injection防止
- CORS設定
- Rate limiting

## テスト
- 単体テスト(pytest)
- 統合テスト
- E2Eテスト
- カバレッジ90%以上

作成してください。
""",
    
    "ai_chat_system": """
# AI チャットシステム(RAG対応)

## 機能
- リアルタイムチャット(WebSocket)
- AI応答(OpenAI API, Claude API)
- RAG(Retrieval-Augmented Generation)
- ベクトルデータベース(Chroma, Pinecone)
- ファイルアップロード・解析
- 会話履歴管理
- ユーザー管理・認証

## 技術スタック
- Frontend: React + TypeScript + Tailwind CSS
- Backend: FastAPI + SQLAlchemy
- Vector DB: Chroma
- Cache: Redis
- Queue: Celery

## AI機能
- 文書の埋め込み生成
- セマンティック検索
- コンテキスト理解
- マルチモーダル対応(画像、PDF)

gradio_interface として作成してください。
""",

    "blockchain_dapp": """
# ブロックチェーン DApp開発

## 要件
- Solidity スマートコントラクト
- Web3.js フロントエンド
- MetaMask連携
- IPFS ファイルストレージ
- OpenZeppelin セキュリティ
- Hardhat 開発環境

## 機能
- NFT マーケットプレイス
- DAO ガバナンス
- DeFi プロトコル
- ステーキング機能

## セキュリティ
- リエントランシー攻撃防止
- オーバーフロー対策
- アクセス制御

作成してください。
""",

    "devops_infrastructure": """
# DevOps インフラストラクチャ

## 要件
- Kubernetes クラスター設計
- Terraform インフラコード
- Ansible 設定管理
- CI/CD パイプライン
- 監視・アラート
- ログ集約
- セキュリティ

## 技術
- AWS/GCP/Azure
- Docker/Podman
- GitLab/GitHub Actions
- Prometheus/Grafana
- ELK Stack
- Helm Charts

## セキュリティ
- Secret管理(Vault)
- ネットワークセキュリティ
- コンプライアンス

作成してください。
"""
}

def add_ai_system_prompts():
    """AI用の高度なシステムプロンプトを追加"""
    try:
        conn = sqlite3.connect(DB_PATH)
        cursor = conn.cursor()
        
        for title, content in ai_system_prompts.items():
            # 既存チェック
            cursor.execute('SELECT id FROM prompts WHERE title LIKE ?', (f"%{title}%",))
            if not cursor.fetchone():
                system_type = "ai_generated"
                github_url = f"https://github.com/ai-systems/{title.replace('_', '-')}"
                
                cursor.execute(
                    'INSERT INTO prompts (title, github_url, repository_name, system_type, content) VALUES (?, ?, ?, ?, ?)',
                    (f"🤖 AI: {title}", github_url, title.replace('_', '-'), system_type, content)
                )
                print(f"✅ AI プロンプト追加: {title}")
        
        conn.commit()
        conn.close()
        
    except Exception as e:
        print(f"❌ AI プロンプト追加エラー: {e}")

# データベース初期化
init_db()
# AI用の高度なプロンプトを追加
add_ai_system_prompts()

with gr.Blocks() as gradio_interface:
    gr.Markdown("# 🚀 プロンプト管理&自動システム生成")
    gr.Markdown("プロンプトでGPT-ENGINEERを使ってシステムを作成し、GitHubにアップして自動化")
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("## 📚 プロンプト一覧")
            
            # プロンプト一覧テーブル
            prompt_table = gr.Dataframe(
                headers=["ID", "タイトル", "リポジトリ", "ステータス", "作成日時"],
                datatype=["number", "str", "str", "str", "str"],
                value=update_prompt_display(),
                interactive=False
            )
            
            # 更新ボタン
            refresh_btn = gr.Button("🔄 一覧更新", variant="secondary")
            
            # プロンプト保存エリア
            gr.Markdown("## 💾 プロンプト保存")
            with gr.Row():
                save_title = gr.Textbox(label="タイトル", placeholder="プロンプトのタイトルを入力")
            with gr.Row():
                github_url_input = gr.Textbox(label="GitHub URL", placeholder="https://github.com/username/repository")
                system_type_dropdown = gr.Dropdown(
                    choices=["general", "web_system", "api_system", "interface_system", "line_system"],
                    value="general",
                    label="システムタイプ"
                )
            with gr.Row():
                save_btn = gr.Button("💾 保存", variant="primary")
            save_result = gr.Textbox(label="保存結果", interactive=False)
        
        with gr.Column(scale=2):
            gr.Markdown("## ⚡ プロンプト実行・システム生成")
            
            # メインのプロンプト入力エリア
            prompt_input = gr.Textbox(
                label="プロンプト内容", 
                lines=12,
                value=val,
                placeholder="プロンプトを入力するか、左の一覧からクリックして選択してください"
            )
            
            with gr.Row():
                selected_github_url = gr.Textbox(label="選択中のGitHub URL", interactive=False)
                selected_system_type = gr.Textbox(label="システムタイプ", interactive=False)
            
            with gr.Row():
                folder_name = gr.Textbox(label="フォルダ名", value="generated_systems")
                github_token = gr.Textbox(label="GitHub Token", value="***********************", type="password")
            
            execute_btn = gr.Button("🚀 システム生成実行", variant="primary", size="lg")
            
            with gr.Row():
                auto_github_checkbox = gr.Checkbox(label="🔄 GitHub自動連携", value=True)
                auto_integrate_checkbox = gr.Checkbox(label="🔧 Controller自動統合", value=True)
            
            result_output = gr.Textbox(label="実行結果", lines=8, interactive=False)
            
            gr.Markdown("## 📋 システム生成フロー")
            gr.Markdown("""
            1. **プロンプト入力** → GPT-ENGINEERでシステム生成
            2. **GitHubアップ** → 指定リポジトリに自動プッシュ  
            3. **Controller自動認識** → 新しいRouterが自動で利用可能に
            4. **Google Chat通知** → 生成完了をチームに通知
            """)
    
    # イベントハンドラー
    prompt_table.select(
        fn=load_prompt_to_textbox,
        outputs=[prompt_input, selected_github_url, selected_system_type]
    )
    
    refresh_btn.click(
        fn=update_prompt_display,
        outputs=prompt_table
    )
    
    save_btn.click(
        fn=lambda title, content, github_url, system_type: save_prompt(title, content, github_url, system_type),
        inputs=[save_title, prompt_input, github_url_input, system_type_dropdown],
        outputs=save_result
    ).then(
        fn=update_prompt_display,
        outputs=prompt_table
    ).then(
        fn=lambda: ("", "", "general"),
        outputs=[save_title, github_url_input, system_type_dropdown]
    )
    
    execute_btn.click(
        fn=process_file_and_notify_enhanced,
        inputs=[prompt_input, folder_name, github_token],
        outputs=result_output
    ).then(
        fn=update_prompt_display,
        outputs=prompt_table
    )